target/arm: Fix capitalization in NeonGenTwo{Single, Double}OPFn typedefs
[qemu/ar7.git] / target / arm / translate-a64.c
blob1204098498171f7816cfdb810c5abc4e39ec6cb0
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/tcg-op.h"
24 #include "tcg/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 /* initialize TCG globals. */
74 void a64_translate_init(void)
76 int i;
78 cpu_pc = tcg_global_mem_new_i64(cpu_env,
79 offsetof(CPUARMState, pc),
80 "pc");
81 for (i = 0; i < 32; i++) {
82 cpu_X[i] = tcg_global_mem_new_i64(cpu_env,
83 offsetof(CPUARMState, xregs[i]),
84 regnames[i]);
87 cpu_exclusive_high = tcg_global_mem_new_i64(cpu_env,
88 offsetof(CPUARMState, exclusive_high), "exclusive_high");
92 * Return the core mmu_idx to use for A64 "unprivileged load/store" insns
94 static int get_a64_user_mem_index(DisasContext *s)
97 * If AccType_UNPRIV is not used, the insn uses AccType_NORMAL,
98 * which is the usual mmu_idx for this cpu state.
100 ARMMMUIdx useridx = s->mmu_idx;
102 if (s->unpriv) {
104 * We have pre-computed the condition for AccType_UNPRIV.
105 * Therefore we should never get here with a mmu_idx for
106 * which we do not know the corresponding user mmu_idx.
108 switch (useridx) {
109 case ARMMMUIdx_E10_1:
110 case ARMMMUIdx_E10_1_PAN:
111 useridx = ARMMMUIdx_E10_0;
112 break;
113 case ARMMMUIdx_E20_2:
114 case ARMMMUIdx_E20_2_PAN:
115 useridx = ARMMMUIdx_E20_0;
116 break;
117 case ARMMMUIdx_SE10_1:
118 case ARMMMUIdx_SE10_1_PAN:
119 useridx = ARMMMUIdx_SE10_0;
120 break;
121 default:
122 g_assert_not_reached();
125 return arm_to_core_mmu_idx(useridx);
128 static void reset_btype(DisasContext *s)
130 if (s->btype != 0) {
131 TCGv_i32 zero = tcg_const_i32(0);
132 tcg_gen_st_i32(zero, cpu_env, offsetof(CPUARMState, btype));
133 tcg_temp_free_i32(zero);
134 s->btype = 0;
138 static void set_btype(DisasContext *s, int val)
140 TCGv_i32 tcg_val;
142 /* BTYPE is a 2-bit field, and 0 should be done with reset_btype. */
143 tcg_debug_assert(val >= 1 && val <= 3);
145 tcg_val = tcg_const_i32(val);
146 tcg_gen_st_i32(tcg_val, cpu_env, offsetof(CPUARMState, btype));
147 tcg_temp_free_i32(tcg_val);
148 s->btype = -1;
151 void gen_a64_set_pc_im(uint64_t val)
153 tcg_gen_movi_i64(cpu_pc, val);
157 * Handle Top Byte Ignore (TBI) bits.
159 * If address tagging is enabled via the TCR TBI bits:
160 * + for EL2 and EL3 there is only one TBI bit, and if it is set
161 * then the address is zero-extended, clearing bits [63:56]
162 * + for EL0 and EL1, TBI0 controls addresses with bit 55 == 0
163 * and TBI1 controls addressses with bit 55 == 1.
164 * If the appropriate TBI bit is set for the address then
165 * the address is sign-extended from bit 55 into bits [63:56]
167 * Here We have concatenated TBI{1,0} into tbi.
169 static void gen_top_byte_ignore(DisasContext *s, TCGv_i64 dst,
170 TCGv_i64 src, int tbi)
172 if (tbi == 0) {
173 /* Load unmodified address */
174 tcg_gen_mov_i64(dst, src);
175 } else if (!regime_has_2_ranges(s->mmu_idx)) {
176 /* Force tag byte to all zero */
177 tcg_gen_extract_i64(dst, src, 0, 56);
178 } else {
179 /* Sign-extend from bit 55. */
180 tcg_gen_sextract_i64(dst, src, 0, 56);
182 if (tbi != 3) {
183 TCGv_i64 tcg_zero = tcg_const_i64(0);
186 * The two TBI bits differ.
187 * If tbi0, then !tbi1: only use the extension if positive.
188 * if !tbi0, then tbi1: only use the extension if negative.
190 tcg_gen_movcond_i64(tbi == 1 ? TCG_COND_GE : TCG_COND_LT,
191 dst, dst, tcg_zero, dst, src);
192 tcg_temp_free_i64(tcg_zero);
197 static void gen_a64_set_pc(DisasContext *s, TCGv_i64 src)
200 * If address tagging is enabled for instructions via the TCR TBI bits,
201 * then loading an address into the PC will clear out any tag.
203 gen_top_byte_ignore(s, cpu_pc, src, s->tbii);
207 * Return a "clean" address for ADDR according to TBID.
208 * This is always a fresh temporary, as we need to be able to
209 * increment this independently of a dirty write-back address.
211 static TCGv_i64 clean_data_tbi(DisasContext *s, TCGv_i64 addr)
213 TCGv_i64 clean = new_tmp_a64(s);
215 * In order to get the correct value in the FAR_ELx register,
216 * we must present the memory subsystem with the "dirty" address
217 * including the TBI. In system mode we can make this work via
218 * the TLB, dropping the TBI during translation. But for user-only
219 * mode we don't have that option, and must remove the top byte now.
221 #ifdef CONFIG_USER_ONLY
222 gen_top_byte_ignore(s, clean, addr, s->tbid);
223 #else
224 tcg_gen_mov_i64(clean, addr);
225 #endif
226 return clean;
229 typedef struct DisasCompare64 {
230 TCGCond cond;
231 TCGv_i64 value;
232 } DisasCompare64;
234 static void a64_test_cc(DisasCompare64 *c64, int cc)
236 DisasCompare c32;
238 arm_test_cc(&c32, cc);
240 /* Sign-extend the 32-bit value so that the GE/LT comparisons work
241 * properly. The NE/EQ comparisons are also fine with this choice. */
242 c64->cond = c32.cond;
243 c64->value = tcg_temp_new_i64();
244 tcg_gen_ext_i32_i64(c64->value, c32.value);
246 arm_free_cc(&c32);
249 static void a64_free_cc(DisasCompare64 *c64)
251 tcg_temp_free_i64(c64->value);
254 static void gen_exception_internal(int excp)
256 TCGv_i32 tcg_excp = tcg_const_i32(excp);
258 assert(excp_is_internal(excp));
259 gen_helper_exception_internal(cpu_env, tcg_excp);
260 tcg_temp_free_i32(tcg_excp);
263 static void gen_exception_internal_insn(DisasContext *s, uint64_t pc, int excp)
265 gen_a64_set_pc_im(pc);
266 gen_exception_internal(excp);
267 s->base.is_jmp = DISAS_NORETURN;
270 static void gen_exception_insn(DisasContext *s, uint64_t pc, int excp,
271 uint32_t syndrome, uint32_t target_el)
273 gen_a64_set_pc_im(pc);
274 gen_exception(excp, syndrome, target_el);
275 s->base.is_jmp = DISAS_NORETURN;
278 static void gen_exception_bkpt_insn(DisasContext *s, uint32_t syndrome)
280 TCGv_i32 tcg_syn;
282 gen_a64_set_pc_im(s->pc_curr);
283 tcg_syn = tcg_const_i32(syndrome);
284 gen_helper_exception_bkpt_insn(cpu_env, tcg_syn);
285 tcg_temp_free_i32(tcg_syn);
286 s->base.is_jmp = DISAS_NORETURN;
289 static void gen_step_complete_exception(DisasContext *s)
291 /* We just completed step of an insn. Move from Active-not-pending
292 * to Active-pending, and then also take the swstep exception.
293 * This corresponds to making the (IMPDEF) choice to prioritize
294 * swstep exceptions over asynchronous exceptions taken to an exception
295 * level where debug is disabled. This choice has the advantage that
296 * we do not need to maintain internal state corresponding to the
297 * ISV/EX syndrome bits between completion of the step and generation
298 * of the exception, and our syndrome information is always correct.
300 gen_ss_advance(s);
301 gen_swstep_exception(s, 1, s->is_ldex);
302 s->base.is_jmp = DISAS_NORETURN;
305 static inline bool use_goto_tb(DisasContext *s, int n, uint64_t dest)
307 /* No direct tb linking with singlestep (either QEMU's or the ARM
308 * debug architecture kind) or deterministic io
310 if (s->base.singlestep_enabled || s->ss_active ||
311 (tb_cflags(s->base.tb) & CF_LAST_IO)) {
312 return false;
315 #ifndef CONFIG_USER_ONLY
316 /* Only link tbs from inside the same guest page */
317 if ((s->base.tb->pc & TARGET_PAGE_MASK) != (dest & TARGET_PAGE_MASK)) {
318 return false;
320 #endif
322 return true;
325 static inline void gen_goto_tb(DisasContext *s, int n, uint64_t dest)
327 TranslationBlock *tb;
329 tb = s->base.tb;
330 if (use_goto_tb(s, n, dest)) {
331 tcg_gen_goto_tb(n);
332 gen_a64_set_pc_im(dest);
333 tcg_gen_exit_tb(tb, n);
334 s->base.is_jmp = DISAS_NORETURN;
335 } else {
336 gen_a64_set_pc_im(dest);
337 if (s->ss_active) {
338 gen_step_complete_exception(s);
339 } else if (s->base.singlestep_enabled) {
340 gen_exception_internal(EXCP_DEBUG);
341 } else {
342 tcg_gen_lookup_and_goto_ptr();
343 s->base.is_jmp = DISAS_NORETURN;
348 void unallocated_encoding(DisasContext *s)
350 /* Unallocated and reserved encodings are uncategorized */
351 gen_exception_insn(s, s->pc_curr, EXCP_UDEF, syn_uncategorized(),
352 default_exception_el(s));
355 static void init_tmp_a64_array(DisasContext *s)
357 #ifdef CONFIG_DEBUG_TCG
358 memset(s->tmp_a64, 0, sizeof(s->tmp_a64));
359 #endif
360 s->tmp_a64_count = 0;
363 static void free_tmp_a64(DisasContext *s)
365 int i;
366 for (i = 0; i < s->tmp_a64_count; i++) {
367 tcg_temp_free_i64(s->tmp_a64[i]);
369 init_tmp_a64_array(s);
372 TCGv_i64 new_tmp_a64(DisasContext *s)
374 assert(s->tmp_a64_count < TMP_A64_MAX);
375 return s->tmp_a64[s->tmp_a64_count++] = tcg_temp_new_i64();
378 TCGv_i64 new_tmp_a64_zero(DisasContext *s)
380 TCGv_i64 t = new_tmp_a64(s);
381 tcg_gen_movi_i64(t, 0);
382 return t;
386 * Register access functions
388 * These functions are used for directly accessing a register in where
389 * changes to the final register value are likely to be made. If you
390 * need to use a register for temporary calculation (e.g. index type
391 * operations) use the read_* form.
393 * B1.2.1 Register mappings
395 * In instruction register encoding 31 can refer to ZR (zero register) or
396 * the SP (stack pointer) depending on context. In QEMU's case we map SP
397 * to cpu_X[31] and ZR accesses to a temporary which can be discarded.
398 * This is the point of the _sp forms.
400 TCGv_i64 cpu_reg(DisasContext *s, int reg)
402 if (reg == 31) {
403 return new_tmp_a64_zero(s);
404 } else {
405 return cpu_X[reg];
409 /* register access for when 31 == SP */
410 TCGv_i64 cpu_reg_sp(DisasContext *s, int reg)
412 return cpu_X[reg];
415 /* read a cpu register in 32bit/64bit mode. Returns a TCGv_i64
416 * representing the register contents. This TCGv is an auto-freed
417 * temporary so it need not be explicitly freed, and may be modified.
419 TCGv_i64 read_cpu_reg(DisasContext *s, int reg, int sf)
421 TCGv_i64 v = new_tmp_a64(s);
422 if (reg != 31) {
423 if (sf) {
424 tcg_gen_mov_i64(v, cpu_X[reg]);
425 } else {
426 tcg_gen_ext32u_i64(v, cpu_X[reg]);
428 } else {
429 tcg_gen_movi_i64(v, 0);
431 return v;
434 TCGv_i64 read_cpu_reg_sp(DisasContext *s, int reg, int sf)
436 TCGv_i64 v = new_tmp_a64(s);
437 if (sf) {
438 tcg_gen_mov_i64(v, cpu_X[reg]);
439 } else {
440 tcg_gen_ext32u_i64(v, cpu_X[reg]);
442 return v;
445 /* Return the offset into CPUARMState of a slice (from
446 * the least significant end) of FP register Qn (ie
447 * Dn, Sn, Hn or Bn).
448 * (Note that this is not the same mapping as for A32; see cpu.h)
450 static inline int fp_reg_offset(DisasContext *s, int regno, MemOp size)
452 return vec_reg_offset(s, regno, 0, size);
455 /* Offset of the high half of the 128 bit vector Qn */
456 static inline int fp_reg_hi_offset(DisasContext *s, int regno)
458 return vec_reg_offset(s, regno, 1, MO_64);
461 /* Convenience accessors for reading and writing single and double
462 * FP registers. Writing clears the upper parts of the associated
463 * 128 bit vector register, as required by the architecture.
464 * Note that unlike the GP register accessors, the values returned
465 * by the read functions must be manually freed.
467 static TCGv_i64 read_fp_dreg(DisasContext *s, int reg)
469 TCGv_i64 v = tcg_temp_new_i64();
471 tcg_gen_ld_i64(v, cpu_env, fp_reg_offset(s, reg, MO_64));
472 return v;
475 static TCGv_i32 read_fp_sreg(DisasContext *s, int reg)
477 TCGv_i32 v = tcg_temp_new_i32();
479 tcg_gen_ld_i32(v, cpu_env, fp_reg_offset(s, reg, MO_32));
480 return v;
483 static TCGv_i32 read_fp_hreg(DisasContext *s, int reg)
485 TCGv_i32 v = tcg_temp_new_i32();
487 tcg_gen_ld16u_i32(v, cpu_env, fp_reg_offset(s, reg, MO_16));
488 return v;
491 /* Clear the bits above an N-bit vector, for N = (is_q ? 128 : 64).
492 * If SVE is not enabled, then there are only 128 bits in the vector.
494 static void clear_vec_high(DisasContext *s, bool is_q, int rd)
496 unsigned ofs = fp_reg_offset(s, rd, MO_64);
497 unsigned vsz = vec_full_reg_size(s);
499 /* Nop move, with side effect of clearing the tail. */
500 tcg_gen_gvec_mov(MO_64, ofs, ofs, is_q ? 16 : 8, vsz);
503 void write_fp_dreg(DisasContext *s, int reg, TCGv_i64 v)
505 unsigned ofs = fp_reg_offset(s, reg, MO_64);
507 tcg_gen_st_i64(v, cpu_env, ofs);
508 clear_vec_high(s, false, reg);
511 static void write_fp_sreg(DisasContext *s, int reg, TCGv_i32 v)
513 TCGv_i64 tmp = tcg_temp_new_i64();
515 tcg_gen_extu_i32_i64(tmp, v);
516 write_fp_dreg(s, reg, tmp);
517 tcg_temp_free_i64(tmp);
520 TCGv_ptr get_fpstatus_ptr(bool is_f16)
522 TCGv_ptr statusptr = tcg_temp_new_ptr();
523 int offset;
525 /* In A64 all instructions (both FP and Neon) use the FPCR; there
526 * is no equivalent of the A32 Neon "standard FPSCR value".
527 * However half-precision operations operate under a different
528 * FZ16 flag and use vfp.fp_status_f16 instead of vfp.fp_status.
530 if (is_f16) {
531 offset = offsetof(CPUARMState, vfp.fp_status_f16);
532 } else {
533 offset = offsetof(CPUARMState, vfp.fp_status);
535 tcg_gen_addi_ptr(statusptr, cpu_env, offset);
536 return statusptr;
539 /* Expand a 2-operand AdvSIMD vector operation using an expander function. */
540 static void gen_gvec_fn2(DisasContext *s, bool is_q, int rd, int rn,
541 GVecGen2Fn *gvec_fn, int vece)
543 gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
544 is_q ? 16 : 8, vec_full_reg_size(s));
547 /* Expand a 2-operand + immediate AdvSIMD vector operation using
548 * an expander function.
550 static void gen_gvec_fn2i(DisasContext *s, bool is_q, int rd, int rn,
551 int64_t imm, GVecGen2iFn *gvec_fn, int vece)
553 gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
554 imm, is_q ? 16 : 8, vec_full_reg_size(s));
557 /* Expand a 3-operand AdvSIMD vector operation using an expander function. */
558 static void gen_gvec_fn3(DisasContext *s, bool is_q, int rd, int rn, int rm,
559 GVecGen3Fn *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), is_q ? 16 : 8, vec_full_reg_size(s));
565 /* Expand a 4-operand AdvSIMD vector operation using an expander function. */
566 static void gen_gvec_fn4(DisasContext *s, bool is_q, int rd, int rn, int rm,
567 int rx, GVecGen4Fn *gvec_fn, int vece)
569 gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
570 vec_full_reg_offset(s, rm), vec_full_reg_offset(s, rx),
571 is_q ? 16 : 8, vec_full_reg_size(s));
574 /* Expand a 2-operand operation using an out-of-line helper. */
575 static void gen_gvec_op2_ool(DisasContext *s, bool is_q, int rd,
576 int rn, int data, gen_helper_gvec_2 *fn)
578 tcg_gen_gvec_2_ool(vec_full_reg_offset(s, rd),
579 vec_full_reg_offset(s, rn),
580 is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
583 /* Expand a 3-operand operation using an out-of-line helper. */
584 static void gen_gvec_op3_ool(DisasContext *s, bool is_q, int rd,
585 int rn, int rm, int data, gen_helper_gvec_3 *fn)
587 tcg_gen_gvec_3_ool(vec_full_reg_offset(s, rd),
588 vec_full_reg_offset(s, rn),
589 vec_full_reg_offset(s, rm),
590 is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
593 /* Expand a 3-operand + fpstatus pointer + simd data value operation using
594 * an out-of-line helper.
596 static void gen_gvec_op3_fpst(DisasContext *s, bool is_q, int rd, int rn,
597 int rm, bool is_fp16, int data,
598 gen_helper_gvec_3_ptr *fn)
600 TCGv_ptr fpst = get_fpstatus_ptr(is_fp16);
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), fpst,
604 is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
605 tcg_temp_free_ptr(fpst);
608 /* Set ZF and NF based on a 64 bit result. This is alas fiddlier
609 * than the 32 bit equivalent.
611 static inline void gen_set_NZ64(TCGv_i64 result)
613 tcg_gen_extr_i64_i32(cpu_ZF, cpu_NF, result);
614 tcg_gen_or_i32(cpu_ZF, cpu_ZF, cpu_NF);
617 /* Set NZCV as for a logical operation: NZ as per result, CV cleared. */
618 static inline void gen_logic_CC(int sf, TCGv_i64 result)
620 if (sf) {
621 gen_set_NZ64(result);
622 } else {
623 tcg_gen_extrl_i64_i32(cpu_ZF, result);
624 tcg_gen_mov_i32(cpu_NF, cpu_ZF);
626 tcg_gen_movi_i32(cpu_CF, 0);
627 tcg_gen_movi_i32(cpu_VF, 0);
630 /* dest = T0 + T1; compute C, N, V and Z flags */
631 static void gen_add_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
633 if (sf) {
634 TCGv_i64 result, flag, tmp;
635 result = tcg_temp_new_i64();
636 flag = tcg_temp_new_i64();
637 tmp = tcg_temp_new_i64();
639 tcg_gen_movi_i64(tmp, 0);
640 tcg_gen_add2_i64(result, flag, t0, tmp, t1, tmp);
642 tcg_gen_extrl_i64_i32(cpu_CF, flag);
644 gen_set_NZ64(result);
646 tcg_gen_xor_i64(flag, result, t0);
647 tcg_gen_xor_i64(tmp, t0, t1);
648 tcg_gen_andc_i64(flag, flag, tmp);
649 tcg_temp_free_i64(tmp);
650 tcg_gen_extrh_i64_i32(cpu_VF, flag);
652 tcg_gen_mov_i64(dest, result);
653 tcg_temp_free_i64(result);
654 tcg_temp_free_i64(flag);
655 } else {
656 /* 32 bit arithmetic */
657 TCGv_i32 t0_32 = tcg_temp_new_i32();
658 TCGv_i32 t1_32 = tcg_temp_new_i32();
659 TCGv_i32 tmp = tcg_temp_new_i32();
661 tcg_gen_movi_i32(tmp, 0);
662 tcg_gen_extrl_i64_i32(t0_32, t0);
663 tcg_gen_extrl_i64_i32(t1_32, t1);
664 tcg_gen_add2_i32(cpu_NF, cpu_CF, t0_32, tmp, t1_32, tmp);
665 tcg_gen_mov_i32(cpu_ZF, cpu_NF);
666 tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
667 tcg_gen_xor_i32(tmp, t0_32, t1_32);
668 tcg_gen_andc_i32(cpu_VF, cpu_VF, tmp);
669 tcg_gen_extu_i32_i64(dest, cpu_NF);
671 tcg_temp_free_i32(tmp);
672 tcg_temp_free_i32(t0_32);
673 tcg_temp_free_i32(t1_32);
677 /* dest = T0 - T1; compute C, N, V and Z flags */
678 static void gen_sub_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
680 if (sf) {
681 /* 64 bit arithmetic */
682 TCGv_i64 result, flag, tmp;
684 result = tcg_temp_new_i64();
685 flag = tcg_temp_new_i64();
686 tcg_gen_sub_i64(result, t0, t1);
688 gen_set_NZ64(result);
690 tcg_gen_setcond_i64(TCG_COND_GEU, flag, t0, t1);
691 tcg_gen_extrl_i64_i32(cpu_CF, flag);
693 tcg_gen_xor_i64(flag, result, t0);
694 tmp = tcg_temp_new_i64();
695 tcg_gen_xor_i64(tmp, t0, t1);
696 tcg_gen_and_i64(flag, flag, tmp);
697 tcg_temp_free_i64(tmp);
698 tcg_gen_extrh_i64_i32(cpu_VF, flag);
699 tcg_gen_mov_i64(dest, result);
700 tcg_temp_free_i64(flag);
701 tcg_temp_free_i64(result);
702 } else {
703 /* 32 bit arithmetic */
704 TCGv_i32 t0_32 = tcg_temp_new_i32();
705 TCGv_i32 t1_32 = tcg_temp_new_i32();
706 TCGv_i32 tmp;
708 tcg_gen_extrl_i64_i32(t0_32, t0);
709 tcg_gen_extrl_i64_i32(t1_32, t1);
710 tcg_gen_sub_i32(cpu_NF, t0_32, t1_32);
711 tcg_gen_mov_i32(cpu_ZF, cpu_NF);
712 tcg_gen_setcond_i32(TCG_COND_GEU, cpu_CF, t0_32, t1_32);
713 tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
714 tmp = tcg_temp_new_i32();
715 tcg_gen_xor_i32(tmp, t0_32, t1_32);
716 tcg_temp_free_i32(t0_32);
717 tcg_temp_free_i32(t1_32);
718 tcg_gen_and_i32(cpu_VF, cpu_VF, tmp);
719 tcg_temp_free_i32(tmp);
720 tcg_gen_extu_i32_i64(dest, cpu_NF);
724 /* dest = T0 + T1 + CF; do not compute flags. */
725 static void gen_adc(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
727 TCGv_i64 flag = tcg_temp_new_i64();
728 tcg_gen_extu_i32_i64(flag, cpu_CF);
729 tcg_gen_add_i64(dest, t0, t1);
730 tcg_gen_add_i64(dest, dest, flag);
731 tcg_temp_free_i64(flag);
733 if (!sf) {
734 tcg_gen_ext32u_i64(dest, dest);
738 /* dest = T0 + T1 + CF; compute C, N, V and Z flags. */
739 static void gen_adc_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
741 if (sf) {
742 TCGv_i64 result, cf_64, vf_64, tmp;
743 result = tcg_temp_new_i64();
744 cf_64 = tcg_temp_new_i64();
745 vf_64 = tcg_temp_new_i64();
746 tmp = tcg_const_i64(0);
748 tcg_gen_extu_i32_i64(cf_64, cpu_CF);
749 tcg_gen_add2_i64(result, cf_64, t0, tmp, cf_64, tmp);
750 tcg_gen_add2_i64(result, cf_64, result, cf_64, t1, tmp);
751 tcg_gen_extrl_i64_i32(cpu_CF, cf_64);
752 gen_set_NZ64(result);
754 tcg_gen_xor_i64(vf_64, result, t0);
755 tcg_gen_xor_i64(tmp, t0, t1);
756 tcg_gen_andc_i64(vf_64, vf_64, tmp);
757 tcg_gen_extrh_i64_i32(cpu_VF, vf_64);
759 tcg_gen_mov_i64(dest, result);
761 tcg_temp_free_i64(tmp);
762 tcg_temp_free_i64(vf_64);
763 tcg_temp_free_i64(cf_64);
764 tcg_temp_free_i64(result);
765 } else {
766 TCGv_i32 t0_32, t1_32, tmp;
767 t0_32 = tcg_temp_new_i32();
768 t1_32 = tcg_temp_new_i32();
769 tmp = tcg_const_i32(0);
771 tcg_gen_extrl_i64_i32(t0_32, t0);
772 tcg_gen_extrl_i64_i32(t1_32, t1);
773 tcg_gen_add2_i32(cpu_NF, cpu_CF, t0_32, tmp, cpu_CF, tmp);
774 tcg_gen_add2_i32(cpu_NF, cpu_CF, cpu_NF, cpu_CF, t1_32, tmp);
776 tcg_gen_mov_i32(cpu_ZF, cpu_NF);
777 tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
778 tcg_gen_xor_i32(tmp, t0_32, t1_32);
779 tcg_gen_andc_i32(cpu_VF, cpu_VF, tmp);
780 tcg_gen_extu_i32_i64(dest, cpu_NF);
782 tcg_temp_free_i32(tmp);
783 tcg_temp_free_i32(t1_32);
784 tcg_temp_free_i32(t0_32);
789 * Load/Store generators
793 * Store from GPR register to memory.
795 static void do_gpr_st_memidx(DisasContext *s, TCGv_i64 source,
796 TCGv_i64 tcg_addr, int size, int memidx,
797 bool iss_valid,
798 unsigned int iss_srt,
799 bool iss_sf, bool iss_ar)
801 g_assert(size <= 3);
802 tcg_gen_qemu_st_i64(source, tcg_addr, memidx, s->be_data + size);
804 if (iss_valid) {
805 uint32_t syn;
807 syn = syn_data_abort_with_iss(0,
808 size,
809 false,
810 iss_srt,
811 iss_sf,
812 iss_ar,
813 0, 0, 0, 0, 0, false);
814 disas_set_insn_syndrome(s, syn);
818 static void do_gpr_st(DisasContext *s, TCGv_i64 source,
819 TCGv_i64 tcg_addr, int size,
820 bool iss_valid,
821 unsigned int iss_srt,
822 bool iss_sf, bool iss_ar)
824 do_gpr_st_memidx(s, source, tcg_addr, size, get_mem_index(s),
825 iss_valid, iss_srt, iss_sf, iss_ar);
829 * Load from memory to GPR register
831 static void do_gpr_ld_memidx(DisasContext *s,
832 TCGv_i64 dest, TCGv_i64 tcg_addr,
833 int size, bool is_signed,
834 bool extend, int memidx,
835 bool iss_valid, unsigned int iss_srt,
836 bool iss_sf, bool iss_ar)
838 MemOp memop = s->be_data + size;
840 g_assert(size <= 3);
842 if (is_signed) {
843 memop += MO_SIGN;
846 tcg_gen_qemu_ld_i64(dest, tcg_addr, memidx, memop);
848 if (extend && is_signed) {
849 g_assert(size < 3);
850 tcg_gen_ext32u_i64(dest, dest);
853 if (iss_valid) {
854 uint32_t syn;
856 syn = syn_data_abort_with_iss(0,
857 size,
858 is_signed,
859 iss_srt,
860 iss_sf,
861 iss_ar,
862 0, 0, 0, 0, 0, false);
863 disas_set_insn_syndrome(s, syn);
867 static void do_gpr_ld(DisasContext *s,
868 TCGv_i64 dest, TCGv_i64 tcg_addr,
869 int size, bool is_signed, bool extend,
870 bool iss_valid, unsigned int iss_srt,
871 bool iss_sf, bool iss_ar)
873 do_gpr_ld_memidx(s, dest, tcg_addr, size, is_signed, extend,
874 get_mem_index(s),
875 iss_valid, iss_srt, iss_sf, iss_ar);
879 * Store from FP register to memory
881 static void do_fp_st(DisasContext *s, int srcidx, TCGv_i64 tcg_addr, int size)
883 /* This writes the bottom N bits of a 128 bit wide vector to memory */
884 TCGv_i64 tmp = tcg_temp_new_i64();
885 tcg_gen_ld_i64(tmp, cpu_env, fp_reg_offset(s, srcidx, MO_64));
886 if (size < 4) {
887 tcg_gen_qemu_st_i64(tmp, tcg_addr, get_mem_index(s),
888 s->be_data + size);
889 } else {
890 bool be = s->be_data == MO_BE;
891 TCGv_i64 tcg_hiaddr = tcg_temp_new_i64();
893 tcg_gen_addi_i64(tcg_hiaddr, tcg_addr, 8);
894 tcg_gen_qemu_st_i64(tmp, be ? tcg_hiaddr : tcg_addr, get_mem_index(s),
895 s->be_data | MO_Q);
896 tcg_gen_ld_i64(tmp, cpu_env, fp_reg_hi_offset(s, srcidx));
897 tcg_gen_qemu_st_i64(tmp, be ? tcg_addr : tcg_hiaddr, get_mem_index(s),
898 s->be_data | MO_Q);
899 tcg_temp_free_i64(tcg_hiaddr);
902 tcg_temp_free_i64(tmp);
906 * Load from memory to FP register
908 static void do_fp_ld(DisasContext *s, int destidx, TCGv_i64 tcg_addr, int size)
910 /* This always zero-extends and writes to a full 128 bit wide vector */
911 TCGv_i64 tmplo = tcg_temp_new_i64();
912 TCGv_i64 tmphi = NULL;
914 if (size < 4) {
915 MemOp memop = s->be_data + size;
916 tcg_gen_qemu_ld_i64(tmplo, tcg_addr, get_mem_index(s), memop);
917 } else {
918 bool be = s->be_data == MO_BE;
919 TCGv_i64 tcg_hiaddr;
921 tmphi = tcg_temp_new_i64();
922 tcg_hiaddr = tcg_temp_new_i64();
924 tcg_gen_addi_i64(tcg_hiaddr, tcg_addr, 8);
925 tcg_gen_qemu_ld_i64(tmplo, be ? tcg_hiaddr : tcg_addr, get_mem_index(s),
926 s->be_data | MO_Q);
927 tcg_gen_qemu_ld_i64(tmphi, be ? tcg_addr : tcg_hiaddr, get_mem_index(s),
928 s->be_data | MO_Q);
929 tcg_temp_free_i64(tcg_hiaddr);
932 tcg_gen_st_i64(tmplo, cpu_env, fp_reg_offset(s, destidx, MO_64));
933 tcg_temp_free_i64(tmplo);
935 if (tmphi) {
936 tcg_gen_st_i64(tmphi, cpu_env, fp_reg_hi_offset(s, destidx));
937 tcg_temp_free_i64(tmphi);
939 clear_vec_high(s, tmphi != NULL, destidx);
943 * Vector load/store helpers.
945 * The principal difference between this and a FP load is that we don't
946 * zero extend as we are filling a partial chunk of the vector register.
947 * These functions don't support 128 bit loads/stores, which would be
948 * normal load/store operations.
950 * The _i32 versions are useful when operating on 32 bit quantities
951 * (eg for floating point single or using Neon helper functions).
954 /* Get value of an element within a vector register */
955 static void read_vec_element(DisasContext *s, TCGv_i64 tcg_dest, int srcidx,
956 int element, MemOp memop)
958 int vect_off = vec_reg_offset(s, srcidx, element, memop & MO_SIZE);
959 switch (memop) {
960 case MO_8:
961 tcg_gen_ld8u_i64(tcg_dest, cpu_env, vect_off);
962 break;
963 case MO_16:
964 tcg_gen_ld16u_i64(tcg_dest, cpu_env, vect_off);
965 break;
966 case MO_32:
967 tcg_gen_ld32u_i64(tcg_dest, cpu_env, vect_off);
968 break;
969 case MO_8|MO_SIGN:
970 tcg_gen_ld8s_i64(tcg_dest, cpu_env, vect_off);
971 break;
972 case MO_16|MO_SIGN:
973 tcg_gen_ld16s_i64(tcg_dest, cpu_env, vect_off);
974 break;
975 case MO_32|MO_SIGN:
976 tcg_gen_ld32s_i64(tcg_dest, cpu_env, vect_off);
977 break;
978 case MO_64:
979 case MO_64|MO_SIGN:
980 tcg_gen_ld_i64(tcg_dest, cpu_env, vect_off);
981 break;
982 default:
983 g_assert_not_reached();
987 static void read_vec_element_i32(DisasContext *s, TCGv_i32 tcg_dest, int srcidx,
988 int element, MemOp memop)
990 int vect_off = vec_reg_offset(s, srcidx, element, memop & MO_SIZE);
991 switch (memop) {
992 case MO_8:
993 tcg_gen_ld8u_i32(tcg_dest, cpu_env, vect_off);
994 break;
995 case MO_16:
996 tcg_gen_ld16u_i32(tcg_dest, cpu_env, vect_off);
997 break;
998 case MO_8|MO_SIGN:
999 tcg_gen_ld8s_i32(tcg_dest, cpu_env, vect_off);
1000 break;
1001 case MO_16|MO_SIGN:
1002 tcg_gen_ld16s_i32(tcg_dest, cpu_env, vect_off);
1003 break;
1004 case MO_32:
1005 case MO_32|MO_SIGN:
1006 tcg_gen_ld_i32(tcg_dest, cpu_env, vect_off);
1007 break;
1008 default:
1009 g_assert_not_reached();
1013 /* Set value of an element within a vector register */
1014 static void write_vec_element(DisasContext *s, TCGv_i64 tcg_src, int destidx,
1015 int element, MemOp memop)
1017 int vect_off = vec_reg_offset(s, destidx, element, memop & MO_SIZE);
1018 switch (memop) {
1019 case MO_8:
1020 tcg_gen_st8_i64(tcg_src, cpu_env, vect_off);
1021 break;
1022 case MO_16:
1023 tcg_gen_st16_i64(tcg_src, cpu_env, vect_off);
1024 break;
1025 case MO_32:
1026 tcg_gen_st32_i64(tcg_src, cpu_env, vect_off);
1027 break;
1028 case MO_64:
1029 tcg_gen_st_i64(tcg_src, cpu_env, vect_off);
1030 break;
1031 default:
1032 g_assert_not_reached();
1036 static void write_vec_element_i32(DisasContext *s, TCGv_i32 tcg_src,
1037 int destidx, int element, MemOp memop)
1039 int vect_off = vec_reg_offset(s, destidx, element, memop & MO_SIZE);
1040 switch (memop) {
1041 case MO_8:
1042 tcg_gen_st8_i32(tcg_src, cpu_env, vect_off);
1043 break;
1044 case MO_16:
1045 tcg_gen_st16_i32(tcg_src, cpu_env, vect_off);
1046 break;
1047 case MO_32:
1048 tcg_gen_st_i32(tcg_src, cpu_env, vect_off);
1049 break;
1050 default:
1051 g_assert_not_reached();
1055 /* Store from vector register to memory */
1056 static void do_vec_st(DisasContext *s, int srcidx, int element,
1057 TCGv_i64 tcg_addr, int size, MemOp endian)
1059 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
1061 read_vec_element(s, tcg_tmp, srcidx, element, size);
1062 tcg_gen_qemu_st_i64(tcg_tmp, tcg_addr, get_mem_index(s), endian | size);
1064 tcg_temp_free_i64(tcg_tmp);
1067 /* Load from memory to vector register */
1068 static void do_vec_ld(DisasContext *s, int destidx, int element,
1069 TCGv_i64 tcg_addr, int size, MemOp endian)
1071 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
1073 tcg_gen_qemu_ld_i64(tcg_tmp, tcg_addr, get_mem_index(s), endian | size);
1074 write_vec_element(s, tcg_tmp, destidx, element, size);
1076 tcg_temp_free_i64(tcg_tmp);
1079 /* Check that FP/Neon access is enabled. If it is, return
1080 * true. If not, emit code to generate an appropriate exception,
1081 * and return false; the caller should not emit any code for
1082 * the instruction. Note that this check must happen after all
1083 * unallocated-encoding checks (otherwise the syndrome information
1084 * for the resulting exception will be incorrect).
1086 static inline bool fp_access_check(DisasContext *s)
1088 assert(!s->fp_access_checked);
1089 s->fp_access_checked = true;
1091 if (!s->fp_excp_el) {
1092 return true;
1095 gen_exception_insn(s, s->pc_curr, EXCP_UDEF,
1096 syn_fp_access_trap(1, 0xe, false), s->fp_excp_el);
1097 return false;
1100 /* Check that SVE access is enabled. If it is, return true.
1101 * If not, emit code to generate an appropriate exception and return false.
1103 bool sve_access_check(DisasContext *s)
1105 if (s->sve_excp_el) {
1106 gen_exception_insn(s, s->pc_curr, EXCP_UDEF, syn_sve_access_trap(),
1107 s->sve_excp_el);
1108 return false;
1110 return fp_access_check(s);
1114 * This utility function is for doing register extension with an
1115 * optional shift. You will likely want to pass a temporary for the
1116 * destination register. See DecodeRegExtend() in the ARM ARM.
1118 static void ext_and_shift_reg(TCGv_i64 tcg_out, TCGv_i64 tcg_in,
1119 int option, unsigned int shift)
1121 int extsize = extract32(option, 0, 2);
1122 bool is_signed = extract32(option, 2, 1);
1124 if (is_signed) {
1125 switch (extsize) {
1126 case 0:
1127 tcg_gen_ext8s_i64(tcg_out, tcg_in);
1128 break;
1129 case 1:
1130 tcg_gen_ext16s_i64(tcg_out, tcg_in);
1131 break;
1132 case 2:
1133 tcg_gen_ext32s_i64(tcg_out, tcg_in);
1134 break;
1135 case 3:
1136 tcg_gen_mov_i64(tcg_out, tcg_in);
1137 break;
1139 } else {
1140 switch (extsize) {
1141 case 0:
1142 tcg_gen_ext8u_i64(tcg_out, tcg_in);
1143 break;
1144 case 1:
1145 tcg_gen_ext16u_i64(tcg_out, tcg_in);
1146 break;
1147 case 2:
1148 tcg_gen_ext32u_i64(tcg_out, tcg_in);
1149 break;
1150 case 3:
1151 tcg_gen_mov_i64(tcg_out, tcg_in);
1152 break;
1156 if (shift) {
1157 tcg_gen_shli_i64(tcg_out, tcg_out, shift);
1161 static inline void gen_check_sp_alignment(DisasContext *s)
1163 /* The AArch64 architecture mandates that (if enabled via PSTATE
1164 * or SCTLR bits) there is a check that SP is 16-aligned on every
1165 * SP-relative load or store (with an exception generated if it is not).
1166 * In line with general QEMU practice regarding misaligned accesses,
1167 * we omit these checks for the sake of guest program performance.
1168 * This function is provided as a hook so we can more easily add these
1169 * checks in future (possibly as a "favour catching guest program bugs
1170 * over speed" user selectable option).
1175 * This provides a simple table based table lookup decoder. It is
1176 * intended to be used when the relevant bits for decode are too
1177 * awkwardly placed and switch/if based logic would be confusing and
1178 * deeply nested. Since it's a linear search through the table, tables
1179 * should be kept small.
1181 * It returns the first handler where insn & mask == pattern, or
1182 * NULL if there is no match.
1183 * The table is terminated by an empty mask (i.e. 0)
1185 static inline AArch64DecodeFn *lookup_disas_fn(const AArch64DecodeTable *table,
1186 uint32_t insn)
1188 const AArch64DecodeTable *tptr = table;
1190 while (tptr->mask) {
1191 if ((insn & tptr->mask) == tptr->pattern) {
1192 return tptr->disas_fn;
1194 tptr++;
1196 return NULL;
1200 * The instruction disassembly implemented here matches
1201 * the instruction encoding classifications in chapter C4
1202 * of the ARM Architecture Reference Manual (DDI0487B_a);
1203 * classification names and decode diagrams here should generally
1204 * match up with those in the manual.
1207 /* Unconditional branch (immediate)
1208 * 31 30 26 25 0
1209 * +----+-----------+-------------------------------------+
1210 * | op | 0 0 1 0 1 | imm26 |
1211 * +----+-----------+-------------------------------------+
1213 static void disas_uncond_b_imm(DisasContext *s, uint32_t insn)
1215 uint64_t addr = s->pc_curr + sextract32(insn, 0, 26) * 4;
1217 if (insn & (1U << 31)) {
1218 /* BL Branch with link */
1219 tcg_gen_movi_i64(cpu_reg(s, 30), s->base.pc_next);
1222 /* B Branch / BL Branch with link */
1223 reset_btype(s);
1224 gen_goto_tb(s, 0, addr);
1227 /* Compare and branch (immediate)
1228 * 31 30 25 24 23 5 4 0
1229 * +----+-------------+----+---------------------+--------+
1230 * | sf | 0 1 1 0 1 0 | op | imm19 | Rt |
1231 * +----+-------------+----+---------------------+--------+
1233 static void disas_comp_b_imm(DisasContext *s, uint32_t insn)
1235 unsigned int sf, op, rt;
1236 uint64_t addr;
1237 TCGLabel *label_match;
1238 TCGv_i64 tcg_cmp;
1240 sf = extract32(insn, 31, 1);
1241 op = extract32(insn, 24, 1); /* 0: CBZ; 1: CBNZ */
1242 rt = extract32(insn, 0, 5);
1243 addr = s->pc_curr + sextract32(insn, 5, 19) * 4;
1245 tcg_cmp = read_cpu_reg(s, rt, sf);
1246 label_match = gen_new_label();
1248 reset_btype(s);
1249 tcg_gen_brcondi_i64(op ? TCG_COND_NE : TCG_COND_EQ,
1250 tcg_cmp, 0, label_match);
1252 gen_goto_tb(s, 0, s->base.pc_next);
1253 gen_set_label(label_match);
1254 gen_goto_tb(s, 1, addr);
1257 /* Test and branch (immediate)
1258 * 31 30 25 24 23 19 18 5 4 0
1259 * +----+-------------+----+-------+-------------+------+
1260 * | b5 | 0 1 1 0 1 1 | op | b40 | imm14 | Rt |
1261 * +----+-------------+----+-------+-------------+------+
1263 static void disas_test_b_imm(DisasContext *s, uint32_t insn)
1265 unsigned int bit_pos, op, rt;
1266 uint64_t addr;
1267 TCGLabel *label_match;
1268 TCGv_i64 tcg_cmp;
1270 bit_pos = (extract32(insn, 31, 1) << 5) | extract32(insn, 19, 5);
1271 op = extract32(insn, 24, 1); /* 0: TBZ; 1: TBNZ */
1272 addr = s->pc_curr + sextract32(insn, 5, 14) * 4;
1273 rt = extract32(insn, 0, 5);
1275 tcg_cmp = tcg_temp_new_i64();
1276 tcg_gen_andi_i64(tcg_cmp, cpu_reg(s, rt), (1ULL << bit_pos));
1277 label_match = gen_new_label();
1279 reset_btype(s);
1280 tcg_gen_brcondi_i64(op ? TCG_COND_NE : TCG_COND_EQ,
1281 tcg_cmp, 0, label_match);
1282 tcg_temp_free_i64(tcg_cmp);
1283 gen_goto_tb(s, 0, s->base.pc_next);
1284 gen_set_label(label_match);
1285 gen_goto_tb(s, 1, addr);
1288 /* Conditional branch (immediate)
1289 * 31 25 24 23 5 4 3 0
1290 * +---------------+----+---------------------+----+------+
1291 * | 0 1 0 1 0 1 0 | o1 | imm19 | o0 | cond |
1292 * +---------------+----+---------------------+----+------+
1294 static void disas_cond_b_imm(DisasContext *s, uint32_t insn)
1296 unsigned int cond;
1297 uint64_t addr;
1299 if ((insn & (1 << 4)) || (insn & (1 << 24))) {
1300 unallocated_encoding(s);
1301 return;
1303 addr = s->pc_curr + sextract32(insn, 5, 19) * 4;
1304 cond = extract32(insn, 0, 4);
1306 reset_btype(s);
1307 if (cond < 0x0e) {
1308 /* genuinely conditional branches */
1309 TCGLabel *label_match = gen_new_label();
1310 arm_gen_test_cc(cond, label_match);
1311 gen_goto_tb(s, 0, s->base.pc_next);
1312 gen_set_label(label_match);
1313 gen_goto_tb(s, 1, addr);
1314 } else {
1315 /* 0xe and 0xf are both "always" conditions */
1316 gen_goto_tb(s, 0, addr);
1320 /* HINT instruction group, including various allocated HINTs */
1321 static void handle_hint(DisasContext *s, uint32_t insn,
1322 unsigned int op1, unsigned int op2, unsigned int crm)
1324 unsigned int selector = crm << 3 | op2;
1326 if (op1 != 3) {
1327 unallocated_encoding(s);
1328 return;
1331 switch (selector) {
1332 case 0b00000: /* NOP */
1333 break;
1334 case 0b00011: /* WFI */
1335 s->base.is_jmp = DISAS_WFI;
1336 break;
1337 case 0b00001: /* YIELD */
1338 /* When running in MTTCG we don't generate jumps to the yield and
1339 * WFE helpers as it won't affect the scheduling of other vCPUs.
1340 * If we wanted to more completely model WFE/SEV so we don't busy
1341 * spin unnecessarily we would need to do something more involved.
1343 if (!(tb_cflags(s->base.tb) & CF_PARALLEL)) {
1344 s->base.is_jmp = DISAS_YIELD;
1346 break;
1347 case 0b00010: /* WFE */
1348 if (!(tb_cflags(s->base.tb) & CF_PARALLEL)) {
1349 s->base.is_jmp = DISAS_WFE;
1351 break;
1352 case 0b00100: /* SEV */
1353 case 0b00101: /* SEVL */
1354 /* we treat all as NOP at least for now */
1355 break;
1356 case 0b00111: /* XPACLRI */
1357 if (s->pauth_active) {
1358 gen_helper_xpaci(cpu_X[30], cpu_env, cpu_X[30]);
1360 break;
1361 case 0b01000: /* PACIA1716 */
1362 if (s->pauth_active) {
1363 gen_helper_pacia(cpu_X[17], cpu_env, cpu_X[17], cpu_X[16]);
1365 break;
1366 case 0b01010: /* PACIB1716 */
1367 if (s->pauth_active) {
1368 gen_helper_pacib(cpu_X[17], cpu_env, cpu_X[17], cpu_X[16]);
1370 break;
1371 case 0b01100: /* AUTIA1716 */
1372 if (s->pauth_active) {
1373 gen_helper_autia(cpu_X[17], cpu_env, cpu_X[17], cpu_X[16]);
1375 break;
1376 case 0b01110: /* AUTIB1716 */
1377 if (s->pauth_active) {
1378 gen_helper_autib(cpu_X[17], cpu_env, cpu_X[17], cpu_X[16]);
1380 break;
1381 case 0b11000: /* PACIAZ */
1382 if (s->pauth_active) {
1383 gen_helper_pacia(cpu_X[30], cpu_env, cpu_X[30],
1384 new_tmp_a64_zero(s));
1386 break;
1387 case 0b11001: /* PACIASP */
1388 if (s->pauth_active) {
1389 gen_helper_pacia(cpu_X[30], cpu_env, cpu_X[30], cpu_X[31]);
1391 break;
1392 case 0b11010: /* PACIBZ */
1393 if (s->pauth_active) {
1394 gen_helper_pacib(cpu_X[30], cpu_env, cpu_X[30],
1395 new_tmp_a64_zero(s));
1397 break;
1398 case 0b11011: /* PACIBSP */
1399 if (s->pauth_active) {
1400 gen_helper_pacib(cpu_X[30], cpu_env, cpu_X[30], cpu_X[31]);
1402 break;
1403 case 0b11100: /* AUTIAZ */
1404 if (s->pauth_active) {
1405 gen_helper_autia(cpu_X[30], cpu_env, cpu_X[30],
1406 new_tmp_a64_zero(s));
1408 break;
1409 case 0b11101: /* AUTIASP */
1410 if (s->pauth_active) {
1411 gen_helper_autia(cpu_X[30], cpu_env, cpu_X[30], cpu_X[31]);
1413 break;
1414 case 0b11110: /* AUTIBZ */
1415 if (s->pauth_active) {
1416 gen_helper_autib(cpu_X[30], cpu_env, cpu_X[30],
1417 new_tmp_a64_zero(s));
1419 break;
1420 case 0b11111: /* AUTIBSP */
1421 if (s->pauth_active) {
1422 gen_helper_autib(cpu_X[30], cpu_env, cpu_X[30], cpu_X[31]);
1424 break;
1425 default:
1426 /* default specified as NOP equivalent */
1427 break;
1431 static void gen_clrex(DisasContext *s, uint32_t insn)
1433 tcg_gen_movi_i64(cpu_exclusive_addr, -1);
1436 /* CLREX, DSB, DMB, ISB */
1437 static void handle_sync(DisasContext *s, uint32_t insn,
1438 unsigned int op1, unsigned int op2, unsigned int crm)
1440 TCGBar bar;
1442 if (op1 != 3) {
1443 unallocated_encoding(s);
1444 return;
1447 switch (op2) {
1448 case 2: /* CLREX */
1449 gen_clrex(s, insn);
1450 return;
1451 case 4: /* DSB */
1452 case 5: /* DMB */
1453 switch (crm & 3) {
1454 case 1: /* MBReqTypes_Reads */
1455 bar = TCG_BAR_SC | TCG_MO_LD_LD | TCG_MO_LD_ST;
1456 break;
1457 case 2: /* MBReqTypes_Writes */
1458 bar = TCG_BAR_SC | TCG_MO_ST_ST;
1459 break;
1460 default: /* MBReqTypes_All */
1461 bar = TCG_BAR_SC | TCG_MO_ALL;
1462 break;
1464 tcg_gen_mb(bar);
1465 return;
1466 case 6: /* ISB */
1467 /* We need to break the TB after this insn to execute
1468 * a self-modified code correctly and also to take
1469 * any pending interrupts immediately.
1471 reset_btype(s);
1472 gen_goto_tb(s, 0, s->base.pc_next);
1473 return;
1475 case 7: /* SB */
1476 if (crm != 0 || !dc_isar_feature(aa64_sb, s)) {
1477 goto do_unallocated;
1480 * TODO: There is no speculation barrier opcode for TCG;
1481 * MB and end the TB instead.
1483 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_SC);
1484 gen_goto_tb(s, 0, s->base.pc_next);
1485 return;
1487 default:
1488 do_unallocated:
1489 unallocated_encoding(s);
1490 return;
1494 static void gen_xaflag(void)
1496 TCGv_i32 z = tcg_temp_new_i32();
1498 tcg_gen_setcondi_i32(TCG_COND_EQ, z, cpu_ZF, 0);
1501 * (!C & !Z) << 31
1502 * (!(C | Z)) << 31
1503 * ~((C | Z) << 31)
1504 * ~-(C | Z)
1505 * (C | Z) - 1
1507 tcg_gen_or_i32(cpu_NF, cpu_CF, z);
1508 tcg_gen_subi_i32(cpu_NF, cpu_NF, 1);
1510 /* !(Z & C) */
1511 tcg_gen_and_i32(cpu_ZF, z, cpu_CF);
1512 tcg_gen_xori_i32(cpu_ZF, cpu_ZF, 1);
1514 /* (!C & Z) << 31 -> -(Z & ~C) */
1515 tcg_gen_andc_i32(cpu_VF, z, cpu_CF);
1516 tcg_gen_neg_i32(cpu_VF, cpu_VF);
1518 /* C | Z */
1519 tcg_gen_or_i32(cpu_CF, cpu_CF, z);
1521 tcg_temp_free_i32(z);
1524 static void gen_axflag(void)
1526 tcg_gen_sari_i32(cpu_VF, cpu_VF, 31); /* V ? -1 : 0 */
1527 tcg_gen_andc_i32(cpu_CF, cpu_CF, cpu_VF); /* C & !V */
1529 /* !(Z | V) -> !(!ZF | V) -> ZF & !V -> ZF & ~VF */
1530 tcg_gen_andc_i32(cpu_ZF, cpu_ZF, cpu_VF);
1532 tcg_gen_movi_i32(cpu_NF, 0);
1533 tcg_gen_movi_i32(cpu_VF, 0);
1536 /* MSR (immediate) - move immediate to processor state field */
1537 static void handle_msr_i(DisasContext *s, uint32_t insn,
1538 unsigned int op1, unsigned int op2, unsigned int crm)
1540 TCGv_i32 t1;
1541 int op = op1 << 3 | op2;
1543 /* End the TB by default, chaining is ok. */
1544 s->base.is_jmp = DISAS_TOO_MANY;
1546 switch (op) {
1547 case 0x00: /* CFINV */
1548 if (crm != 0 || !dc_isar_feature(aa64_condm_4, s)) {
1549 goto do_unallocated;
1551 tcg_gen_xori_i32(cpu_CF, cpu_CF, 1);
1552 s->base.is_jmp = DISAS_NEXT;
1553 break;
1555 case 0x01: /* XAFlag */
1556 if (crm != 0 || !dc_isar_feature(aa64_condm_5, s)) {
1557 goto do_unallocated;
1559 gen_xaflag();
1560 s->base.is_jmp = DISAS_NEXT;
1561 break;
1563 case 0x02: /* AXFlag */
1564 if (crm != 0 || !dc_isar_feature(aa64_condm_5, s)) {
1565 goto do_unallocated;
1567 gen_axflag();
1568 s->base.is_jmp = DISAS_NEXT;
1569 break;
1571 case 0x03: /* UAO */
1572 if (!dc_isar_feature(aa64_uao, s) || s->current_el == 0) {
1573 goto do_unallocated;
1575 if (crm & 1) {
1576 set_pstate_bits(PSTATE_UAO);
1577 } else {
1578 clear_pstate_bits(PSTATE_UAO);
1580 t1 = tcg_const_i32(s->current_el);
1581 gen_helper_rebuild_hflags_a64(cpu_env, t1);
1582 tcg_temp_free_i32(t1);
1583 break;
1585 case 0x04: /* PAN */
1586 if (!dc_isar_feature(aa64_pan, s) || s->current_el == 0) {
1587 goto do_unallocated;
1589 if (crm & 1) {
1590 set_pstate_bits(PSTATE_PAN);
1591 } else {
1592 clear_pstate_bits(PSTATE_PAN);
1594 t1 = tcg_const_i32(s->current_el);
1595 gen_helper_rebuild_hflags_a64(cpu_env, t1);
1596 tcg_temp_free_i32(t1);
1597 break;
1599 case 0x05: /* SPSel */
1600 if (s->current_el == 0) {
1601 goto do_unallocated;
1603 t1 = tcg_const_i32(crm & PSTATE_SP);
1604 gen_helper_msr_i_spsel(cpu_env, t1);
1605 tcg_temp_free_i32(t1);
1606 break;
1608 case 0x1e: /* DAIFSet */
1609 t1 = tcg_const_i32(crm);
1610 gen_helper_msr_i_daifset(cpu_env, t1);
1611 tcg_temp_free_i32(t1);
1612 break;
1614 case 0x1f: /* DAIFClear */
1615 t1 = tcg_const_i32(crm);
1616 gen_helper_msr_i_daifclear(cpu_env, t1);
1617 tcg_temp_free_i32(t1);
1618 /* For DAIFClear, exit the cpu loop to re-evaluate pending IRQs. */
1619 s->base.is_jmp = DISAS_UPDATE;
1620 break;
1622 default:
1623 do_unallocated:
1624 unallocated_encoding(s);
1625 return;
1629 static void gen_get_nzcv(TCGv_i64 tcg_rt)
1631 TCGv_i32 tmp = tcg_temp_new_i32();
1632 TCGv_i32 nzcv = tcg_temp_new_i32();
1634 /* build bit 31, N */
1635 tcg_gen_andi_i32(nzcv, cpu_NF, (1U << 31));
1636 /* build bit 30, Z */
1637 tcg_gen_setcondi_i32(TCG_COND_EQ, tmp, cpu_ZF, 0);
1638 tcg_gen_deposit_i32(nzcv, nzcv, tmp, 30, 1);
1639 /* build bit 29, C */
1640 tcg_gen_deposit_i32(nzcv, nzcv, cpu_CF, 29, 1);
1641 /* build bit 28, V */
1642 tcg_gen_shri_i32(tmp, cpu_VF, 31);
1643 tcg_gen_deposit_i32(nzcv, nzcv, tmp, 28, 1);
1644 /* generate result */
1645 tcg_gen_extu_i32_i64(tcg_rt, nzcv);
1647 tcg_temp_free_i32(nzcv);
1648 tcg_temp_free_i32(tmp);
1651 static void gen_set_nzcv(TCGv_i64 tcg_rt)
1653 TCGv_i32 nzcv = tcg_temp_new_i32();
1655 /* take NZCV from R[t] */
1656 tcg_gen_extrl_i64_i32(nzcv, tcg_rt);
1658 /* bit 31, N */
1659 tcg_gen_andi_i32(cpu_NF, nzcv, (1U << 31));
1660 /* bit 30, Z */
1661 tcg_gen_andi_i32(cpu_ZF, nzcv, (1 << 30));
1662 tcg_gen_setcondi_i32(TCG_COND_EQ, cpu_ZF, cpu_ZF, 0);
1663 /* bit 29, C */
1664 tcg_gen_andi_i32(cpu_CF, nzcv, (1 << 29));
1665 tcg_gen_shri_i32(cpu_CF, cpu_CF, 29);
1666 /* bit 28, V */
1667 tcg_gen_andi_i32(cpu_VF, nzcv, (1 << 28));
1668 tcg_gen_shli_i32(cpu_VF, cpu_VF, 3);
1669 tcg_temp_free_i32(nzcv);
1672 /* MRS - move from system register
1673 * MSR (register) - move to system register
1674 * SYS
1675 * SYSL
1676 * These are all essentially the same insn in 'read' and 'write'
1677 * versions, with varying op0 fields.
1679 static void handle_sys(DisasContext *s, uint32_t insn, bool isread,
1680 unsigned int op0, unsigned int op1, unsigned int op2,
1681 unsigned int crn, unsigned int crm, unsigned int rt)
1683 const ARMCPRegInfo *ri;
1684 TCGv_i64 tcg_rt;
1686 ri = get_arm_cp_reginfo(s->cp_regs,
1687 ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP,
1688 crn, crm, op0, op1, op2));
1690 if (!ri) {
1691 /* Unknown register; this might be a guest error or a QEMU
1692 * unimplemented feature.
1694 qemu_log_mask(LOG_UNIMP, "%s access to unsupported AArch64 "
1695 "system register op0:%d op1:%d crn:%d crm:%d op2:%d\n",
1696 isread ? "read" : "write", op0, op1, crn, crm, op2);
1697 unallocated_encoding(s);
1698 return;
1701 /* Check access permissions */
1702 if (!cp_access_ok(s->current_el, ri, isread)) {
1703 unallocated_encoding(s);
1704 return;
1707 if (ri->accessfn) {
1708 /* Emit code to perform further access permissions checks at
1709 * runtime; this may result in an exception.
1711 TCGv_ptr tmpptr;
1712 TCGv_i32 tcg_syn, tcg_isread;
1713 uint32_t syndrome;
1715 gen_a64_set_pc_im(s->pc_curr);
1716 tmpptr = tcg_const_ptr(ri);
1717 syndrome = syn_aa64_sysregtrap(op0, op1, op2, crn, crm, rt, isread);
1718 tcg_syn = tcg_const_i32(syndrome);
1719 tcg_isread = tcg_const_i32(isread);
1720 gen_helper_access_check_cp_reg(cpu_env, tmpptr, tcg_syn, tcg_isread);
1721 tcg_temp_free_ptr(tmpptr);
1722 tcg_temp_free_i32(tcg_syn);
1723 tcg_temp_free_i32(tcg_isread);
1724 } else if (ri->type & ARM_CP_RAISES_EXC) {
1726 * The readfn or writefn might raise an exception;
1727 * synchronize the CPU state in case it does.
1729 gen_a64_set_pc_im(s->pc_curr);
1732 /* Handle special cases first */
1733 switch (ri->type & ~(ARM_CP_FLAG_MASK & ~ARM_CP_SPECIAL)) {
1734 case ARM_CP_NOP:
1735 return;
1736 case ARM_CP_NZCV:
1737 tcg_rt = cpu_reg(s, rt);
1738 if (isread) {
1739 gen_get_nzcv(tcg_rt);
1740 } else {
1741 gen_set_nzcv(tcg_rt);
1743 return;
1744 case ARM_CP_CURRENTEL:
1745 /* Reads as current EL value from pstate, which is
1746 * guaranteed to be constant by the tb flags.
1748 tcg_rt = cpu_reg(s, rt);
1749 tcg_gen_movi_i64(tcg_rt, s->current_el << 2);
1750 return;
1751 case ARM_CP_DC_ZVA:
1752 /* Writes clear the aligned block of memory which rt points into. */
1753 tcg_rt = clean_data_tbi(s, cpu_reg(s, rt));
1754 gen_helper_dc_zva(cpu_env, tcg_rt);
1755 return;
1756 default:
1757 break;
1759 if ((ri->type & ARM_CP_FPU) && !fp_access_check(s)) {
1760 return;
1761 } else if ((ri->type & ARM_CP_SVE) && !sve_access_check(s)) {
1762 return;
1765 if ((tb_cflags(s->base.tb) & CF_USE_ICOUNT) && (ri->type & ARM_CP_IO)) {
1766 gen_io_start();
1769 tcg_rt = cpu_reg(s, rt);
1771 if (isread) {
1772 if (ri->type & ARM_CP_CONST) {
1773 tcg_gen_movi_i64(tcg_rt, ri->resetvalue);
1774 } else if (ri->readfn) {
1775 TCGv_ptr tmpptr;
1776 tmpptr = tcg_const_ptr(ri);
1777 gen_helper_get_cp_reg64(tcg_rt, cpu_env, tmpptr);
1778 tcg_temp_free_ptr(tmpptr);
1779 } else {
1780 tcg_gen_ld_i64(tcg_rt, cpu_env, ri->fieldoffset);
1782 } else {
1783 if (ri->type & ARM_CP_CONST) {
1784 /* If not forbidden by access permissions, treat as WI */
1785 return;
1786 } else if (ri->writefn) {
1787 TCGv_ptr tmpptr;
1788 tmpptr = tcg_const_ptr(ri);
1789 gen_helper_set_cp_reg64(cpu_env, tmpptr, tcg_rt);
1790 tcg_temp_free_ptr(tmpptr);
1791 } else {
1792 tcg_gen_st_i64(tcg_rt, cpu_env, ri->fieldoffset);
1796 if ((tb_cflags(s->base.tb) & CF_USE_ICOUNT) && (ri->type & ARM_CP_IO)) {
1797 /* I/O operations must end the TB here (whether read or write) */
1798 s->base.is_jmp = DISAS_UPDATE;
1800 if (!isread && !(ri->type & ARM_CP_SUPPRESS_TB_END)) {
1802 * A write to any coprocessor regiser that ends a TB
1803 * must rebuild the hflags for the next TB.
1805 TCGv_i32 tcg_el = tcg_const_i32(s->current_el);
1806 gen_helper_rebuild_hflags_a64(cpu_env, tcg_el);
1807 tcg_temp_free_i32(tcg_el);
1809 * We default to ending the TB on a coprocessor register write,
1810 * but allow this to be suppressed by the register definition
1811 * (usually only necessary to work around guest bugs).
1813 s->base.is_jmp = DISAS_UPDATE;
1817 /* System
1818 * 31 22 21 20 19 18 16 15 12 11 8 7 5 4 0
1819 * +---------------------+---+-----+-----+-------+-------+-----+------+
1820 * | 1 1 0 1 0 1 0 1 0 0 | L | op0 | op1 | CRn | CRm | op2 | Rt |
1821 * +---------------------+---+-----+-----+-------+-------+-----+------+
1823 static void disas_system(DisasContext *s, uint32_t insn)
1825 unsigned int l, op0, op1, crn, crm, op2, rt;
1826 l = extract32(insn, 21, 1);
1827 op0 = extract32(insn, 19, 2);
1828 op1 = extract32(insn, 16, 3);
1829 crn = extract32(insn, 12, 4);
1830 crm = extract32(insn, 8, 4);
1831 op2 = extract32(insn, 5, 3);
1832 rt = extract32(insn, 0, 5);
1834 if (op0 == 0) {
1835 if (l || rt != 31) {
1836 unallocated_encoding(s);
1837 return;
1839 switch (crn) {
1840 case 2: /* HINT (including allocated hints like NOP, YIELD, etc) */
1841 handle_hint(s, insn, op1, op2, crm);
1842 break;
1843 case 3: /* CLREX, DSB, DMB, ISB */
1844 handle_sync(s, insn, op1, op2, crm);
1845 break;
1846 case 4: /* MSR (immediate) */
1847 handle_msr_i(s, insn, op1, op2, crm);
1848 break;
1849 default:
1850 unallocated_encoding(s);
1851 break;
1853 return;
1855 handle_sys(s, insn, l, op0, op1, op2, crn, crm, rt);
1858 /* Exception generation
1860 * 31 24 23 21 20 5 4 2 1 0
1861 * +-----------------+-----+------------------------+-----+----+
1862 * | 1 1 0 1 0 1 0 0 | opc | imm16 | op2 | LL |
1863 * +-----------------------+------------------------+----------+
1865 static void disas_exc(DisasContext *s, uint32_t insn)
1867 int opc = extract32(insn, 21, 3);
1868 int op2_ll = extract32(insn, 0, 5);
1869 int imm16 = extract32(insn, 5, 16);
1870 TCGv_i32 tmp;
1872 switch (opc) {
1873 case 0:
1874 /* For SVC, HVC and SMC we advance the single-step state
1875 * machine before taking the exception. This is architecturally
1876 * mandated, to ensure that single-stepping a system call
1877 * instruction works properly.
1879 switch (op2_ll) {
1880 case 1: /* SVC */
1881 gen_ss_advance(s);
1882 gen_exception_insn(s, s->base.pc_next, EXCP_SWI,
1883 syn_aa64_svc(imm16), default_exception_el(s));
1884 break;
1885 case 2: /* HVC */
1886 if (s->current_el == 0) {
1887 unallocated_encoding(s);
1888 break;
1890 /* The pre HVC helper handles cases when HVC gets trapped
1891 * as an undefined insn by runtime configuration.
1893 gen_a64_set_pc_im(s->pc_curr);
1894 gen_helper_pre_hvc(cpu_env);
1895 gen_ss_advance(s);
1896 gen_exception_insn(s, s->base.pc_next, EXCP_HVC,
1897 syn_aa64_hvc(imm16), 2);
1898 break;
1899 case 3: /* SMC */
1900 if (s->current_el == 0) {
1901 unallocated_encoding(s);
1902 break;
1904 gen_a64_set_pc_im(s->pc_curr);
1905 tmp = tcg_const_i32(syn_aa64_smc(imm16));
1906 gen_helper_pre_smc(cpu_env, tmp);
1907 tcg_temp_free_i32(tmp);
1908 gen_ss_advance(s);
1909 gen_exception_insn(s, s->base.pc_next, EXCP_SMC,
1910 syn_aa64_smc(imm16), 3);
1911 break;
1912 default:
1913 unallocated_encoding(s);
1914 break;
1916 break;
1917 case 1:
1918 if (op2_ll != 0) {
1919 unallocated_encoding(s);
1920 break;
1922 /* BRK */
1923 gen_exception_bkpt_insn(s, syn_aa64_bkpt(imm16));
1924 break;
1925 case 2:
1926 if (op2_ll != 0) {
1927 unallocated_encoding(s);
1928 break;
1930 /* HLT. This has two purposes.
1931 * Architecturally, it is an external halting debug instruction.
1932 * Since QEMU doesn't implement external debug, we treat this as
1933 * it is required for halting debug disabled: it will UNDEF.
1934 * Secondly, "HLT 0xf000" is the A64 semihosting syscall instruction.
1936 if (semihosting_enabled() && imm16 == 0xf000) {
1937 #ifndef CONFIG_USER_ONLY
1938 /* In system mode, don't allow userspace access to semihosting,
1939 * to provide some semblance of security (and for consistency
1940 * with our 32-bit semihosting).
1942 if (s->current_el == 0) {
1943 unsupported_encoding(s, insn);
1944 break;
1946 #endif
1947 gen_exception_internal_insn(s, s->pc_curr, EXCP_SEMIHOST);
1948 } else {
1949 unsupported_encoding(s, insn);
1951 break;
1952 case 5:
1953 if (op2_ll < 1 || op2_ll > 3) {
1954 unallocated_encoding(s);
1955 break;
1957 /* DCPS1, DCPS2, DCPS3 */
1958 unsupported_encoding(s, insn);
1959 break;
1960 default:
1961 unallocated_encoding(s);
1962 break;
1966 /* Unconditional branch (register)
1967 * 31 25 24 21 20 16 15 10 9 5 4 0
1968 * +---------------+-------+-------+-------+------+-------+
1969 * | 1 1 0 1 0 1 1 | opc | op2 | op3 | Rn | op4 |
1970 * +---------------+-------+-------+-------+------+-------+
1972 static void disas_uncond_b_reg(DisasContext *s, uint32_t insn)
1974 unsigned int opc, op2, op3, rn, op4;
1975 unsigned btype_mod = 2; /* 0: BR, 1: BLR, 2: other */
1976 TCGv_i64 dst;
1977 TCGv_i64 modifier;
1979 opc = extract32(insn, 21, 4);
1980 op2 = extract32(insn, 16, 5);
1981 op3 = extract32(insn, 10, 6);
1982 rn = extract32(insn, 5, 5);
1983 op4 = extract32(insn, 0, 5);
1985 if (op2 != 0x1f) {
1986 goto do_unallocated;
1989 switch (opc) {
1990 case 0: /* BR */
1991 case 1: /* BLR */
1992 case 2: /* RET */
1993 btype_mod = opc;
1994 switch (op3) {
1995 case 0:
1996 /* BR, BLR, RET */
1997 if (op4 != 0) {
1998 goto do_unallocated;
2000 dst = cpu_reg(s, rn);
2001 break;
2003 case 2:
2004 case 3:
2005 if (!dc_isar_feature(aa64_pauth, s)) {
2006 goto do_unallocated;
2008 if (opc == 2) {
2009 /* RETAA, RETAB */
2010 if (rn != 0x1f || op4 != 0x1f) {
2011 goto do_unallocated;
2013 rn = 30;
2014 modifier = cpu_X[31];
2015 } else {
2016 /* BRAAZ, BRABZ, BLRAAZ, BLRABZ */
2017 if (op4 != 0x1f) {
2018 goto do_unallocated;
2020 modifier = new_tmp_a64_zero(s);
2022 if (s->pauth_active) {
2023 dst = new_tmp_a64(s);
2024 if (op3 == 2) {
2025 gen_helper_autia(dst, cpu_env, cpu_reg(s, rn), modifier);
2026 } else {
2027 gen_helper_autib(dst, cpu_env, cpu_reg(s, rn), modifier);
2029 } else {
2030 dst = cpu_reg(s, rn);
2032 break;
2034 default:
2035 goto do_unallocated;
2037 gen_a64_set_pc(s, dst);
2038 /* BLR also needs to load return address */
2039 if (opc == 1) {
2040 tcg_gen_movi_i64(cpu_reg(s, 30), s->base.pc_next);
2042 break;
2044 case 8: /* BRAA */
2045 case 9: /* BLRAA */
2046 if (!dc_isar_feature(aa64_pauth, s)) {
2047 goto do_unallocated;
2049 if ((op3 & ~1) != 2) {
2050 goto do_unallocated;
2052 btype_mod = opc & 1;
2053 if (s->pauth_active) {
2054 dst = new_tmp_a64(s);
2055 modifier = cpu_reg_sp(s, op4);
2056 if (op3 == 2) {
2057 gen_helper_autia(dst, cpu_env, cpu_reg(s, rn), modifier);
2058 } else {
2059 gen_helper_autib(dst, cpu_env, cpu_reg(s, rn), modifier);
2061 } else {
2062 dst = cpu_reg(s, rn);
2064 gen_a64_set_pc(s, dst);
2065 /* BLRAA also needs to load return address */
2066 if (opc == 9) {
2067 tcg_gen_movi_i64(cpu_reg(s, 30), s->base.pc_next);
2069 break;
2071 case 4: /* ERET */
2072 if (s->current_el == 0) {
2073 goto do_unallocated;
2075 switch (op3) {
2076 case 0: /* ERET */
2077 if (op4 != 0) {
2078 goto do_unallocated;
2080 dst = tcg_temp_new_i64();
2081 tcg_gen_ld_i64(dst, cpu_env,
2082 offsetof(CPUARMState, elr_el[s->current_el]));
2083 break;
2085 case 2: /* ERETAA */
2086 case 3: /* ERETAB */
2087 if (!dc_isar_feature(aa64_pauth, s)) {
2088 goto do_unallocated;
2090 if (rn != 0x1f || op4 != 0x1f) {
2091 goto do_unallocated;
2093 dst = tcg_temp_new_i64();
2094 tcg_gen_ld_i64(dst, cpu_env,
2095 offsetof(CPUARMState, elr_el[s->current_el]));
2096 if (s->pauth_active) {
2097 modifier = cpu_X[31];
2098 if (op3 == 2) {
2099 gen_helper_autia(dst, cpu_env, dst, modifier);
2100 } else {
2101 gen_helper_autib(dst, cpu_env, dst, modifier);
2104 break;
2106 default:
2107 goto do_unallocated;
2109 if (tb_cflags(s->base.tb) & CF_USE_ICOUNT) {
2110 gen_io_start();
2113 gen_helper_exception_return(cpu_env, dst);
2114 tcg_temp_free_i64(dst);
2115 /* Must exit loop to check un-masked IRQs */
2116 s->base.is_jmp = DISAS_EXIT;
2117 return;
2119 case 5: /* DRPS */
2120 if (op3 != 0 || op4 != 0 || rn != 0x1f) {
2121 goto do_unallocated;
2122 } else {
2123 unsupported_encoding(s, insn);
2125 return;
2127 default:
2128 do_unallocated:
2129 unallocated_encoding(s);
2130 return;
2133 switch (btype_mod) {
2134 case 0: /* BR */
2135 if (dc_isar_feature(aa64_bti, s)) {
2136 /* BR to {x16,x17} or !guard -> 1, else 3. */
2137 set_btype(s, rn == 16 || rn == 17 || !s->guarded_page ? 1 : 3);
2139 break;
2141 case 1: /* BLR */
2142 if (dc_isar_feature(aa64_bti, s)) {
2143 /* BLR sets BTYPE to 2, regardless of source guarded page. */
2144 set_btype(s, 2);
2146 break;
2148 default: /* RET or none of the above. */
2149 /* BTYPE will be set to 0 by normal end-of-insn processing. */
2150 break;
2153 s->base.is_jmp = DISAS_JUMP;
2156 /* Branches, exception generating and system instructions */
2157 static void disas_b_exc_sys(DisasContext *s, uint32_t insn)
2159 switch (extract32(insn, 25, 7)) {
2160 case 0x0a: case 0x0b:
2161 case 0x4a: case 0x4b: /* Unconditional branch (immediate) */
2162 disas_uncond_b_imm(s, insn);
2163 break;
2164 case 0x1a: case 0x5a: /* Compare & branch (immediate) */
2165 disas_comp_b_imm(s, insn);
2166 break;
2167 case 0x1b: case 0x5b: /* Test & branch (immediate) */
2168 disas_test_b_imm(s, insn);
2169 break;
2170 case 0x2a: /* Conditional branch (immediate) */
2171 disas_cond_b_imm(s, insn);
2172 break;
2173 case 0x6a: /* Exception generation / System */
2174 if (insn & (1 << 24)) {
2175 if (extract32(insn, 22, 2) == 0) {
2176 disas_system(s, insn);
2177 } else {
2178 unallocated_encoding(s);
2180 } else {
2181 disas_exc(s, insn);
2183 break;
2184 case 0x6b: /* Unconditional branch (register) */
2185 disas_uncond_b_reg(s, insn);
2186 break;
2187 default:
2188 unallocated_encoding(s);
2189 break;
2194 * Load/Store exclusive instructions are implemented by remembering
2195 * the value/address loaded, and seeing if these are the same
2196 * when the store is performed. This is not actually the architecturally
2197 * mandated semantics, but it works for typical guest code sequences
2198 * and avoids having to monitor regular stores.
2200 * The store exclusive uses the atomic cmpxchg primitives to avoid
2201 * races in multi-threaded linux-user and when MTTCG softmmu is
2202 * enabled.
2204 static void gen_load_exclusive(DisasContext *s, int rt, int rt2,
2205 TCGv_i64 addr, int size, bool is_pair)
2207 int idx = get_mem_index(s);
2208 MemOp memop = s->be_data;
2210 g_assert(size <= 3);
2211 if (is_pair) {
2212 g_assert(size >= 2);
2213 if (size == 2) {
2214 /* The pair must be single-copy atomic for the doubleword. */
2215 memop |= MO_64 | MO_ALIGN;
2216 tcg_gen_qemu_ld_i64(cpu_exclusive_val, addr, idx, memop);
2217 if (s->be_data == MO_LE) {
2218 tcg_gen_extract_i64(cpu_reg(s, rt), cpu_exclusive_val, 0, 32);
2219 tcg_gen_extract_i64(cpu_reg(s, rt2), cpu_exclusive_val, 32, 32);
2220 } else {
2221 tcg_gen_extract_i64(cpu_reg(s, rt), cpu_exclusive_val, 32, 32);
2222 tcg_gen_extract_i64(cpu_reg(s, rt2), cpu_exclusive_val, 0, 32);
2224 } else {
2225 /* The pair must be single-copy atomic for *each* doubleword, not
2226 the entire quadword, however it must be quadword aligned. */
2227 memop |= MO_64;
2228 tcg_gen_qemu_ld_i64(cpu_exclusive_val, addr, idx,
2229 memop | MO_ALIGN_16);
2231 TCGv_i64 addr2 = tcg_temp_new_i64();
2232 tcg_gen_addi_i64(addr2, addr, 8);
2233 tcg_gen_qemu_ld_i64(cpu_exclusive_high, addr2, idx, memop);
2234 tcg_temp_free_i64(addr2);
2236 tcg_gen_mov_i64(cpu_reg(s, rt), cpu_exclusive_val);
2237 tcg_gen_mov_i64(cpu_reg(s, rt2), cpu_exclusive_high);
2239 } else {
2240 memop |= size | MO_ALIGN;
2241 tcg_gen_qemu_ld_i64(cpu_exclusive_val, addr, idx, memop);
2242 tcg_gen_mov_i64(cpu_reg(s, rt), cpu_exclusive_val);
2244 tcg_gen_mov_i64(cpu_exclusive_addr, addr);
2247 static void gen_store_exclusive(DisasContext *s, int rd, int rt, int rt2,
2248 TCGv_i64 addr, int size, int is_pair)
2250 /* if (env->exclusive_addr == addr && env->exclusive_val == [addr]
2251 * && (!is_pair || env->exclusive_high == [addr + datasize])) {
2252 * [addr] = {Rt};
2253 * if (is_pair) {
2254 * [addr + datasize] = {Rt2};
2256 * {Rd} = 0;
2257 * } else {
2258 * {Rd} = 1;
2260 * env->exclusive_addr = -1;
2262 TCGLabel *fail_label = gen_new_label();
2263 TCGLabel *done_label = gen_new_label();
2264 TCGv_i64 tmp;
2266 tcg_gen_brcond_i64(TCG_COND_NE, addr, cpu_exclusive_addr, fail_label);
2268 tmp = tcg_temp_new_i64();
2269 if (is_pair) {
2270 if (size == 2) {
2271 if (s->be_data == MO_LE) {
2272 tcg_gen_concat32_i64(tmp, cpu_reg(s, rt), cpu_reg(s, rt2));
2273 } else {
2274 tcg_gen_concat32_i64(tmp, cpu_reg(s, rt2), cpu_reg(s, rt));
2276 tcg_gen_atomic_cmpxchg_i64(tmp, cpu_exclusive_addr,
2277 cpu_exclusive_val, tmp,
2278 get_mem_index(s),
2279 MO_64 | MO_ALIGN | s->be_data);
2280 tcg_gen_setcond_i64(TCG_COND_NE, tmp, tmp, cpu_exclusive_val);
2281 } else if (tb_cflags(s->base.tb) & CF_PARALLEL) {
2282 if (!HAVE_CMPXCHG128) {
2283 gen_helper_exit_atomic(cpu_env);
2284 s->base.is_jmp = DISAS_NORETURN;
2285 } else if (s->be_data == MO_LE) {
2286 gen_helper_paired_cmpxchg64_le_parallel(tmp, cpu_env,
2287 cpu_exclusive_addr,
2288 cpu_reg(s, rt),
2289 cpu_reg(s, rt2));
2290 } else {
2291 gen_helper_paired_cmpxchg64_be_parallel(tmp, cpu_env,
2292 cpu_exclusive_addr,
2293 cpu_reg(s, rt),
2294 cpu_reg(s, rt2));
2296 } else if (s->be_data == MO_LE) {
2297 gen_helper_paired_cmpxchg64_le(tmp, cpu_env, cpu_exclusive_addr,
2298 cpu_reg(s, rt), cpu_reg(s, rt2));
2299 } else {
2300 gen_helper_paired_cmpxchg64_be(tmp, cpu_env, cpu_exclusive_addr,
2301 cpu_reg(s, rt), cpu_reg(s, rt2));
2303 } else {
2304 tcg_gen_atomic_cmpxchg_i64(tmp, cpu_exclusive_addr, cpu_exclusive_val,
2305 cpu_reg(s, rt), get_mem_index(s),
2306 size | MO_ALIGN | s->be_data);
2307 tcg_gen_setcond_i64(TCG_COND_NE, tmp, tmp, cpu_exclusive_val);
2309 tcg_gen_mov_i64(cpu_reg(s, rd), tmp);
2310 tcg_temp_free_i64(tmp);
2311 tcg_gen_br(done_label);
2313 gen_set_label(fail_label);
2314 tcg_gen_movi_i64(cpu_reg(s, rd), 1);
2315 gen_set_label(done_label);
2316 tcg_gen_movi_i64(cpu_exclusive_addr, -1);
2319 static void gen_compare_and_swap(DisasContext *s, int rs, int rt,
2320 int rn, int size)
2322 TCGv_i64 tcg_rs = cpu_reg(s, rs);
2323 TCGv_i64 tcg_rt = cpu_reg(s, rt);
2324 int memidx = get_mem_index(s);
2325 TCGv_i64 clean_addr;
2327 if (rn == 31) {
2328 gen_check_sp_alignment(s);
2330 clean_addr = clean_data_tbi(s, cpu_reg_sp(s, rn));
2331 tcg_gen_atomic_cmpxchg_i64(tcg_rs, clean_addr, tcg_rs, tcg_rt, memidx,
2332 size | MO_ALIGN | s->be_data);
2335 static void gen_compare_and_swap_pair(DisasContext *s, int rs, int rt,
2336 int rn, int size)
2338 TCGv_i64 s1 = cpu_reg(s, rs);
2339 TCGv_i64 s2 = cpu_reg(s, rs + 1);
2340 TCGv_i64 t1 = cpu_reg(s, rt);
2341 TCGv_i64 t2 = cpu_reg(s, rt + 1);
2342 TCGv_i64 clean_addr;
2343 int memidx = get_mem_index(s);
2345 if (rn == 31) {
2346 gen_check_sp_alignment(s);
2348 clean_addr = clean_data_tbi(s, cpu_reg_sp(s, rn));
2350 if (size == 2) {
2351 TCGv_i64 cmp = tcg_temp_new_i64();
2352 TCGv_i64 val = tcg_temp_new_i64();
2354 if (s->be_data == MO_LE) {
2355 tcg_gen_concat32_i64(val, t1, t2);
2356 tcg_gen_concat32_i64(cmp, s1, s2);
2357 } else {
2358 tcg_gen_concat32_i64(val, t2, t1);
2359 tcg_gen_concat32_i64(cmp, s2, s1);
2362 tcg_gen_atomic_cmpxchg_i64(cmp, clean_addr, cmp, val, memidx,
2363 MO_64 | MO_ALIGN | s->be_data);
2364 tcg_temp_free_i64(val);
2366 if (s->be_data == MO_LE) {
2367 tcg_gen_extr32_i64(s1, s2, cmp);
2368 } else {
2369 tcg_gen_extr32_i64(s2, s1, cmp);
2371 tcg_temp_free_i64(cmp);
2372 } else if (tb_cflags(s->base.tb) & CF_PARALLEL) {
2373 if (HAVE_CMPXCHG128) {
2374 TCGv_i32 tcg_rs = tcg_const_i32(rs);
2375 if (s->be_data == MO_LE) {
2376 gen_helper_casp_le_parallel(cpu_env, tcg_rs,
2377 clean_addr, t1, t2);
2378 } else {
2379 gen_helper_casp_be_parallel(cpu_env, tcg_rs,
2380 clean_addr, t1, t2);
2382 tcg_temp_free_i32(tcg_rs);
2383 } else {
2384 gen_helper_exit_atomic(cpu_env);
2385 s->base.is_jmp = DISAS_NORETURN;
2387 } else {
2388 TCGv_i64 d1 = tcg_temp_new_i64();
2389 TCGv_i64 d2 = tcg_temp_new_i64();
2390 TCGv_i64 a2 = tcg_temp_new_i64();
2391 TCGv_i64 c1 = tcg_temp_new_i64();
2392 TCGv_i64 c2 = tcg_temp_new_i64();
2393 TCGv_i64 zero = tcg_const_i64(0);
2395 /* Load the two words, in memory order. */
2396 tcg_gen_qemu_ld_i64(d1, clean_addr, memidx,
2397 MO_64 | MO_ALIGN_16 | s->be_data);
2398 tcg_gen_addi_i64(a2, clean_addr, 8);
2399 tcg_gen_qemu_ld_i64(d2, a2, memidx, MO_64 | s->be_data);
2401 /* Compare the two words, also in memory order. */
2402 tcg_gen_setcond_i64(TCG_COND_EQ, c1, d1, s1);
2403 tcg_gen_setcond_i64(TCG_COND_EQ, c2, d2, s2);
2404 tcg_gen_and_i64(c2, c2, c1);
2406 /* If compare equal, write back new data, else write back old data. */
2407 tcg_gen_movcond_i64(TCG_COND_NE, c1, c2, zero, t1, d1);
2408 tcg_gen_movcond_i64(TCG_COND_NE, c2, c2, zero, t2, d2);
2409 tcg_gen_qemu_st_i64(c1, clean_addr, memidx, MO_64 | s->be_data);
2410 tcg_gen_qemu_st_i64(c2, a2, memidx, MO_64 | s->be_data);
2411 tcg_temp_free_i64(a2);
2412 tcg_temp_free_i64(c1);
2413 tcg_temp_free_i64(c2);
2414 tcg_temp_free_i64(zero);
2416 /* Write back the data from memory to Rs. */
2417 tcg_gen_mov_i64(s1, d1);
2418 tcg_gen_mov_i64(s2, d2);
2419 tcg_temp_free_i64(d1);
2420 tcg_temp_free_i64(d2);
2424 /* Update the Sixty-Four bit (SF) registersize. This logic is derived
2425 * from the ARMv8 specs for LDR (Shared decode for all encodings).
2427 static bool disas_ldst_compute_iss_sf(int size, bool is_signed, int opc)
2429 int opc0 = extract32(opc, 0, 1);
2430 int regsize;
2432 if (is_signed) {
2433 regsize = opc0 ? 32 : 64;
2434 } else {
2435 regsize = size == 3 ? 64 : 32;
2437 return regsize == 64;
2440 /* Load/store exclusive
2442 * 31 30 29 24 23 22 21 20 16 15 14 10 9 5 4 0
2443 * +-----+-------------+----+---+----+------+----+-------+------+------+
2444 * | sz | 0 0 1 0 0 0 | o2 | L | o1 | Rs | o0 | Rt2 | Rn | Rt |
2445 * +-----+-------------+----+---+----+------+----+-------+------+------+
2447 * sz: 00 -> 8 bit, 01 -> 16 bit, 10 -> 32 bit, 11 -> 64 bit
2448 * L: 0 -> store, 1 -> load
2449 * o2: 0 -> exclusive, 1 -> not
2450 * o1: 0 -> single register, 1 -> register pair
2451 * o0: 1 -> load-acquire/store-release, 0 -> not
2453 static void disas_ldst_excl(DisasContext *s, uint32_t insn)
2455 int rt = extract32(insn, 0, 5);
2456 int rn = extract32(insn, 5, 5);
2457 int rt2 = extract32(insn, 10, 5);
2458 int rs = extract32(insn, 16, 5);
2459 int is_lasr = extract32(insn, 15, 1);
2460 int o2_L_o1_o0 = extract32(insn, 21, 3) * 2 | is_lasr;
2461 int size = extract32(insn, 30, 2);
2462 TCGv_i64 clean_addr;
2464 switch (o2_L_o1_o0) {
2465 case 0x0: /* STXR */
2466 case 0x1: /* STLXR */
2467 if (rn == 31) {
2468 gen_check_sp_alignment(s);
2470 if (is_lasr) {
2471 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2473 clean_addr = clean_data_tbi(s, cpu_reg_sp(s, rn));
2474 gen_store_exclusive(s, rs, rt, rt2, clean_addr, size, false);
2475 return;
2477 case 0x4: /* LDXR */
2478 case 0x5: /* LDAXR */
2479 if (rn == 31) {
2480 gen_check_sp_alignment(s);
2482 clean_addr = clean_data_tbi(s, cpu_reg_sp(s, rn));
2483 s->is_ldex = true;
2484 gen_load_exclusive(s, rt, rt2, clean_addr, size, false);
2485 if (is_lasr) {
2486 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2488 return;
2490 case 0x8: /* STLLR */
2491 if (!dc_isar_feature(aa64_lor, s)) {
2492 break;
2494 /* StoreLORelease is the same as Store-Release for QEMU. */
2495 /* fall through */
2496 case 0x9: /* STLR */
2497 /* Generate ISS for non-exclusive accesses including LASR. */
2498 if (rn == 31) {
2499 gen_check_sp_alignment(s);
2501 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2502 clean_addr = clean_data_tbi(s, cpu_reg_sp(s, rn));
2503 do_gpr_st(s, cpu_reg(s, rt), clean_addr, size, true, rt,
2504 disas_ldst_compute_iss_sf(size, false, 0), is_lasr);
2505 return;
2507 case 0xc: /* LDLAR */
2508 if (!dc_isar_feature(aa64_lor, s)) {
2509 break;
2511 /* LoadLOAcquire is the same as Load-Acquire for QEMU. */
2512 /* fall through */
2513 case 0xd: /* LDAR */
2514 /* Generate ISS for non-exclusive accesses including LASR. */
2515 if (rn == 31) {
2516 gen_check_sp_alignment(s);
2518 clean_addr = clean_data_tbi(s, cpu_reg_sp(s, rn));
2519 do_gpr_ld(s, cpu_reg(s, rt), clean_addr, size, false, false, true, rt,
2520 disas_ldst_compute_iss_sf(size, false, 0), is_lasr);
2521 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2522 return;
2524 case 0x2: case 0x3: /* CASP / STXP */
2525 if (size & 2) { /* STXP / STLXP */
2526 if (rn == 31) {
2527 gen_check_sp_alignment(s);
2529 if (is_lasr) {
2530 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2532 clean_addr = clean_data_tbi(s, cpu_reg_sp(s, rn));
2533 gen_store_exclusive(s, rs, rt, rt2, clean_addr, size, true);
2534 return;
2536 if (rt2 == 31
2537 && ((rt | rs) & 1) == 0
2538 && dc_isar_feature(aa64_atomics, s)) {
2539 /* CASP / CASPL */
2540 gen_compare_and_swap_pair(s, rs, rt, rn, size | 2);
2541 return;
2543 break;
2545 case 0x6: case 0x7: /* CASPA / LDXP */
2546 if (size & 2) { /* LDXP / LDAXP */
2547 if (rn == 31) {
2548 gen_check_sp_alignment(s);
2550 clean_addr = clean_data_tbi(s, cpu_reg_sp(s, rn));
2551 s->is_ldex = true;
2552 gen_load_exclusive(s, rt, rt2, clean_addr, size, true);
2553 if (is_lasr) {
2554 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2556 return;
2558 if (rt2 == 31
2559 && ((rt | rs) & 1) == 0
2560 && dc_isar_feature(aa64_atomics, s)) {
2561 /* CASPA / CASPAL */
2562 gen_compare_and_swap_pair(s, rs, rt, rn, size | 2);
2563 return;
2565 break;
2567 case 0xa: /* CAS */
2568 case 0xb: /* CASL */
2569 case 0xe: /* CASA */
2570 case 0xf: /* CASAL */
2571 if (rt2 == 31 && dc_isar_feature(aa64_atomics, s)) {
2572 gen_compare_and_swap(s, rs, rt, rn, size);
2573 return;
2575 break;
2577 unallocated_encoding(s);
2581 * Load register (literal)
2583 * 31 30 29 27 26 25 24 23 5 4 0
2584 * +-----+-------+---+-----+-------------------+-------+
2585 * | opc | 0 1 1 | V | 0 0 | imm19 | Rt |
2586 * +-----+-------+---+-----+-------------------+-------+
2588 * V: 1 -> vector (simd/fp)
2589 * opc (non-vector): 00 -> 32 bit, 01 -> 64 bit,
2590 * 10-> 32 bit signed, 11 -> prefetch
2591 * opc (vector): 00 -> 32 bit, 01 -> 64 bit, 10 -> 128 bit (11 unallocated)
2593 static void disas_ld_lit(DisasContext *s, uint32_t insn)
2595 int rt = extract32(insn, 0, 5);
2596 int64_t imm = sextract32(insn, 5, 19) << 2;
2597 bool is_vector = extract32(insn, 26, 1);
2598 int opc = extract32(insn, 30, 2);
2599 bool is_signed = false;
2600 int size = 2;
2601 TCGv_i64 tcg_rt, clean_addr;
2603 if (is_vector) {
2604 if (opc == 3) {
2605 unallocated_encoding(s);
2606 return;
2608 size = 2 + opc;
2609 if (!fp_access_check(s)) {
2610 return;
2612 } else {
2613 if (opc == 3) {
2614 /* PRFM (literal) : prefetch */
2615 return;
2617 size = 2 + extract32(opc, 0, 1);
2618 is_signed = extract32(opc, 1, 1);
2621 tcg_rt = cpu_reg(s, rt);
2623 clean_addr = tcg_const_i64(s->pc_curr + imm);
2624 if (is_vector) {
2625 do_fp_ld(s, rt, clean_addr, size);
2626 } else {
2627 /* Only unsigned 32bit loads target 32bit registers. */
2628 bool iss_sf = opc != 0;
2630 do_gpr_ld(s, tcg_rt, clean_addr, size, is_signed, false,
2631 true, rt, iss_sf, false);
2633 tcg_temp_free_i64(clean_addr);
2637 * LDNP (Load Pair - non-temporal hint)
2638 * LDP (Load Pair - non vector)
2639 * LDPSW (Load Pair Signed Word - non vector)
2640 * STNP (Store Pair - non-temporal hint)
2641 * STP (Store Pair - non vector)
2642 * LDNP (Load Pair of SIMD&FP - non-temporal hint)
2643 * LDP (Load Pair of SIMD&FP)
2644 * STNP (Store Pair of SIMD&FP - non-temporal hint)
2645 * STP (Store Pair of SIMD&FP)
2647 * 31 30 29 27 26 25 24 23 22 21 15 14 10 9 5 4 0
2648 * +-----+-------+---+---+-------+---+-----------------------------+
2649 * | opc | 1 0 1 | V | 0 | index | L | imm7 | Rt2 | Rn | Rt |
2650 * +-----+-------+---+---+-------+---+-------+-------+------+------+
2652 * opc: LDP/STP/LDNP/STNP 00 -> 32 bit, 10 -> 64 bit
2653 * LDPSW 01
2654 * LDP/STP/LDNP/STNP (SIMD) 00 -> 32 bit, 01 -> 64 bit, 10 -> 128 bit
2655 * V: 0 -> GPR, 1 -> Vector
2656 * idx: 00 -> signed offset with non-temporal hint, 01 -> post-index,
2657 * 10 -> signed offset, 11 -> pre-index
2658 * L: 0 -> Store 1 -> Load
2660 * Rt, Rt2 = GPR or SIMD registers to be stored
2661 * Rn = general purpose register containing address
2662 * imm7 = signed offset (multiple of 4 or 8 depending on size)
2664 static void disas_ldst_pair(DisasContext *s, uint32_t insn)
2666 int rt = extract32(insn, 0, 5);
2667 int rn = extract32(insn, 5, 5);
2668 int rt2 = extract32(insn, 10, 5);
2669 uint64_t offset = sextract64(insn, 15, 7);
2670 int index = extract32(insn, 23, 2);
2671 bool is_vector = extract32(insn, 26, 1);
2672 bool is_load = extract32(insn, 22, 1);
2673 int opc = extract32(insn, 30, 2);
2675 bool is_signed = false;
2676 bool postindex = false;
2677 bool wback = false;
2679 TCGv_i64 clean_addr, dirty_addr;
2681 int size;
2683 if (opc == 3) {
2684 unallocated_encoding(s);
2685 return;
2688 if (is_vector) {
2689 size = 2 + opc;
2690 } else {
2691 size = 2 + extract32(opc, 1, 1);
2692 is_signed = extract32(opc, 0, 1);
2693 if (!is_load && is_signed) {
2694 unallocated_encoding(s);
2695 return;
2699 switch (index) {
2700 case 1: /* post-index */
2701 postindex = true;
2702 wback = true;
2703 break;
2704 case 0:
2705 /* signed offset with "non-temporal" hint. Since we don't emulate
2706 * caches we don't care about hints to the cache system about
2707 * data access patterns, and handle this identically to plain
2708 * signed offset.
2710 if (is_signed) {
2711 /* There is no non-temporal-hint version of LDPSW */
2712 unallocated_encoding(s);
2713 return;
2715 postindex = false;
2716 break;
2717 case 2: /* signed offset, rn not updated */
2718 postindex = false;
2719 break;
2720 case 3: /* pre-index */
2721 postindex = false;
2722 wback = true;
2723 break;
2726 if (is_vector && !fp_access_check(s)) {
2727 return;
2730 offset <<= size;
2732 if (rn == 31) {
2733 gen_check_sp_alignment(s);
2736 dirty_addr = read_cpu_reg_sp(s, rn, 1);
2737 if (!postindex) {
2738 tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
2740 clean_addr = clean_data_tbi(s, dirty_addr);
2742 if (is_vector) {
2743 if (is_load) {
2744 do_fp_ld(s, rt, clean_addr, size);
2745 } else {
2746 do_fp_st(s, rt, clean_addr, size);
2748 tcg_gen_addi_i64(clean_addr, clean_addr, 1 << size);
2749 if (is_load) {
2750 do_fp_ld(s, rt2, clean_addr, size);
2751 } else {
2752 do_fp_st(s, rt2, clean_addr, size);
2754 } else {
2755 TCGv_i64 tcg_rt = cpu_reg(s, rt);
2756 TCGv_i64 tcg_rt2 = cpu_reg(s, rt2);
2758 if (is_load) {
2759 TCGv_i64 tmp = tcg_temp_new_i64();
2761 /* Do not modify tcg_rt before recognizing any exception
2762 * from the second load.
2764 do_gpr_ld(s, tmp, clean_addr, size, is_signed, false,
2765 false, 0, false, false);
2766 tcg_gen_addi_i64(clean_addr, clean_addr, 1 << size);
2767 do_gpr_ld(s, tcg_rt2, clean_addr, size, is_signed, false,
2768 false, 0, false, false);
2770 tcg_gen_mov_i64(tcg_rt, tmp);
2771 tcg_temp_free_i64(tmp);
2772 } else {
2773 do_gpr_st(s, tcg_rt, clean_addr, size,
2774 false, 0, false, false);
2775 tcg_gen_addi_i64(clean_addr, clean_addr, 1 << size);
2776 do_gpr_st(s, tcg_rt2, clean_addr, size,
2777 false, 0, false, false);
2781 if (wback) {
2782 if (postindex) {
2783 tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
2785 tcg_gen_mov_i64(cpu_reg_sp(s, rn), dirty_addr);
2790 * Load/store (immediate post-indexed)
2791 * Load/store (immediate pre-indexed)
2792 * Load/store (unscaled immediate)
2794 * 31 30 29 27 26 25 24 23 22 21 20 12 11 10 9 5 4 0
2795 * +----+-------+---+-----+-----+---+--------+-----+------+------+
2796 * |size| 1 1 1 | V | 0 0 | opc | 0 | imm9 | idx | Rn | Rt |
2797 * +----+-------+---+-----+-----+---+--------+-----+------+------+
2799 * idx = 01 -> post-indexed, 11 pre-indexed, 00 unscaled imm. (no writeback)
2800 10 -> unprivileged
2801 * V = 0 -> non-vector
2802 * size: 00 -> 8 bit, 01 -> 16 bit, 10 -> 32 bit, 11 -> 64bit
2803 * opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
2805 static void disas_ldst_reg_imm9(DisasContext *s, uint32_t insn,
2806 int opc,
2807 int size,
2808 int rt,
2809 bool is_vector)
2811 int rn = extract32(insn, 5, 5);
2812 int imm9 = sextract32(insn, 12, 9);
2813 int idx = extract32(insn, 10, 2);
2814 bool is_signed = false;
2815 bool is_store = false;
2816 bool is_extended = false;
2817 bool is_unpriv = (idx == 2);
2818 bool iss_valid = !is_vector;
2819 bool post_index;
2820 bool writeback;
2822 TCGv_i64 clean_addr, dirty_addr;
2824 if (is_vector) {
2825 size |= (opc & 2) << 1;
2826 if (size > 4 || is_unpriv) {
2827 unallocated_encoding(s);
2828 return;
2830 is_store = ((opc & 1) == 0);
2831 if (!fp_access_check(s)) {
2832 return;
2834 } else {
2835 if (size == 3 && opc == 2) {
2836 /* PRFM - prefetch */
2837 if (idx != 0) {
2838 unallocated_encoding(s);
2839 return;
2841 return;
2843 if (opc == 3 && size > 1) {
2844 unallocated_encoding(s);
2845 return;
2847 is_store = (opc == 0);
2848 is_signed = extract32(opc, 1, 1);
2849 is_extended = (size < 3) && extract32(opc, 0, 1);
2852 switch (idx) {
2853 case 0:
2854 case 2:
2855 post_index = false;
2856 writeback = false;
2857 break;
2858 case 1:
2859 post_index = true;
2860 writeback = true;
2861 break;
2862 case 3:
2863 post_index = false;
2864 writeback = true;
2865 break;
2866 default:
2867 g_assert_not_reached();
2870 if (rn == 31) {
2871 gen_check_sp_alignment(s);
2874 dirty_addr = read_cpu_reg_sp(s, rn, 1);
2875 if (!post_index) {
2876 tcg_gen_addi_i64(dirty_addr, dirty_addr, imm9);
2878 clean_addr = clean_data_tbi(s, dirty_addr);
2880 if (is_vector) {
2881 if (is_store) {
2882 do_fp_st(s, rt, clean_addr, size);
2883 } else {
2884 do_fp_ld(s, rt, clean_addr, size);
2886 } else {
2887 TCGv_i64 tcg_rt = cpu_reg(s, rt);
2888 int memidx = is_unpriv ? get_a64_user_mem_index(s) : get_mem_index(s);
2889 bool iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
2891 if (is_store) {
2892 do_gpr_st_memidx(s, tcg_rt, clean_addr, size, memidx,
2893 iss_valid, rt, iss_sf, false);
2894 } else {
2895 do_gpr_ld_memidx(s, tcg_rt, clean_addr, size,
2896 is_signed, is_extended, memidx,
2897 iss_valid, rt, iss_sf, false);
2901 if (writeback) {
2902 TCGv_i64 tcg_rn = cpu_reg_sp(s, rn);
2903 if (post_index) {
2904 tcg_gen_addi_i64(dirty_addr, dirty_addr, imm9);
2906 tcg_gen_mov_i64(tcg_rn, dirty_addr);
2911 * Load/store (register offset)
2913 * 31 30 29 27 26 25 24 23 22 21 20 16 15 13 12 11 10 9 5 4 0
2914 * +----+-------+---+-----+-----+---+------+-----+--+-----+----+----+
2915 * |size| 1 1 1 | V | 0 0 | opc | 1 | Rm | opt | S| 1 0 | Rn | Rt |
2916 * +----+-------+---+-----+-----+---+------+-----+--+-----+----+----+
2918 * For non-vector:
2919 * size: 00-> byte, 01 -> 16 bit, 10 -> 32bit, 11 -> 64bit
2920 * opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
2921 * For vector:
2922 * size is opc<1>:size<1:0> so 100 -> 128 bit; 110 and 111 unallocated
2923 * opc<0>: 0 -> store, 1 -> load
2924 * V: 1 -> vector/simd
2925 * opt: extend encoding (see DecodeRegExtend)
2926 * S: if S=1 then scale (essentially index by sizeof(size))
2927 * Rt: register to transfer into/out of
2928 * Rn: address register or SP for base
2929 * Rm: offset register or ZR for offset
2931 static void disas_ldst_reg_roffset(DisasContext *s, uint32_t insn,
2932 int opc,
2933 int size,
2934 int rt,
2935 bool is_vector)
2937 int rn = extract32(insn, 5, 5);
2938 int shift = extract32(insn, 12, 1);
2939 int rm = extract32(insn, 16, 5);
2940 int opt = extract32(insn, 13, 3);
2941 bool is_signed = false;
2942 bool is_store = false;
2943 bool is_extended = false;
2945 TCGv_i64 tcg_rm, clean_addr, dirty_addr;
2947 if (extract32(opt, 1, 1) == 0) {
2948 unallocated_encoding(s);
2949 return;
2952 if (is_vector) {
2953 size |= (opc & 2) << 1;
2954 if (size > 4) {
2955 unallocated_encoding(s);
2956 return;
2958 is_store = !extract32(opc, 0, 1);
2959 if (!fp_access_check(s)) {
2960 return;
2962 } else {
2963 if (size == 3 && opc == 2) {
2964 /* PRFM - prefetch */
2965 return;
2967 if (opc == 3 && size > 1) {
2968 unallocated_encoding(s);
2969 return;
2971 is_store = (opc == 0);
2972 is_signed = extract32(opc, 1, 1);
2973 is_extended = (size < 3) && extract32(opc, 0, 1);
2976 if (rn == 31) {
2977 gen_check_sp_alignment(s);
2979 dirty_addr = read_cpu_reg_sp(s, rn, 1);
2981 tcg_rm = read_cpu_reg(s, rm, 1);
2982 ext_and_shift_reg(tcg_rm, tcg_rm, opt, shift ? size : 0);
2984 tcg_gen_add_i64(dirty_addr, dirty_addr, tcg_rm);
2985 clean_addr = clean_data_tbi(s, dirty_addr);
2987 if (is_vector) {
2988 if (is_store) {
2989 do_fp_st(s, rt, clean_addr, size);
2990 } else {
2991 do_fp_ld(s, rt, clean_addr, size);
2993 } else {
2994 TCGv_i64 tcg_rt = cpu_reg(s, rt);
2995 bool iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
2996 if (is_store) {
2997 do_gpr_st(s, tcg_rt, clean_addr, size,
2998 true, rt, iss_sf, false);
2999 } else {
3000 do_gpr_ld(s, tcg_rt, clean_addr, size,
3001 is_signed, is_extended,
3002 true, rt, iss_sf, false);
3008 * Load/store (unsigned immediate)
3010 * 31 30 29 27 26 25 24 23 22 21 10 9 5
3011 * +----+-------+---+-----+-----+------------+-------+------+
3012 * |size| 1 1 1 | V | 0 1 | opc | imm12 | Rn | Rt |
3013 * +----+-------+---+-----+-----+------------+-------+------+
3015 * For non-vector:
3016 * size: 00-> byte, 01 -> 16 bit, 10 -> 32bit, 11 -> 64bit
3017 * opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
3018 * For vector:
3019 * size is opc<1>:size<1:0> so 100 -> 128 bit; 110 and 111 unallocated
3020 * opc<0>: 0 -> store, 1 -> load
3021 * Rn: base address register (inc SP)
3022 * Rt: target register
3024 static void disas_ldst_reg_unsigned_imm(DisasContext *s, uint32_t insn,
3025 int opc,
3026 int size,
3027 int rt,
3028 bool is_vector)
3030 int rn = extract32(insn, 5, 5);
3031 unsigned int imm12 = extract32(insn, 10, 12);
3032 unsigned int offset;
3034 TCGv_i64 clean_addr, dirty_addr;
3036 bool is_store;
3037 bool is_signed = false;
3038 bool is_extended = false;
3040 if (is_vector) {
3041 size |= (opc & 2) << 1;
3042 if (size > 4) {
3043 unallocated_encoding(s);
3044 return;
3046 is_store = !extract32(opc, 0, 1);
3047 if (!fp_access_check(s)) {
3048 return;
3050 } else {
3051 if (size == 3 && opc == 2) {
3052 /* PRFM - prefetch */
3053 return;
3055 if (opc == 3 && size > 1) {
3056 unallocated_encoding(s);
3057 return;
3059 is_store = (opc == 0);
3060 is_signed = extract32(opc, 1, 1);
3061 is_extended = (size < 3) && extract32(opc, 0, 1);
3064 if (rn == 31) {
3065 gen_check_sp_alignment(s);
3067 dirty_addr = read_cpu_reg_sp(s, rn, 1);
3068 offset = imm12 << size;
3069 tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3070 clean_addr = clean_data_tbi(s, dirty_addr);
3072 if (is_vector) {
3073 if (is_store) {
3074 do_fp_st(s, rt, clean_addr, size);
3075 } else {
3076 do_fp_ld(s, rt, clean_addr, size);
3078 } else {
3079 TCGv_i64 tcg_rt = cpu_reg(s, rt);
3080 bool iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
3081 if (is_store) {
3082 do_gpr_st(s, tcg_rt, clean_addr, size,
3083 true, rt, iss_sf, false);
3084 } else {
3085 do_gpr_ld(s, tcg_rt, clean_addr, size, is_signed, is_extended,
3086 true, rt, iss_sf, false);
3091 /* Atomic memory operations
3093 * 31 30 27 26 24 22 21 16 15 12 10 5 0
3094 * +------+-------+---+-----+-----+---+----+----+-----+-----+----+-----+
3095 * | size | 1 1 1 | V | 0 0 | A R | 1 | Rs | o3 | opc | 0 0 | Rn | Rt |
3096 * +------+-------+---+-----+-----+--------+----+-----+-----+----+-----+
3098 * Rt: the result register
3099 * Rn: base address or SP
3100 * Rs: the source register for the operation
3101 * V: vector flag (always 0 as of v8.3)
3102 * A: acquire flag
3103 * R: release flag
3105 static void disas_ldst_atomic(DisasContext *s, uint32_t insn,
3106 int size, int rt, bool is_vector)
3108 int rs = extract32(insn, 16, 5);
3109 int rn = extract32(insn, 5, 5);
3110 int o3_opc = extract32(insn, 12, 4);
3111 bool r = extract32(insn, 22, 1);
3112 bool a = extract32(insn, 23, 1);
3113 TCGv_i64 tcg_rs, clean_addr;
3114 AtomicThreeOpFn *fn;
3116 if (is_vector || !dc_isar_feature(aa64_atomics, s)) {
3117 unallocated_encoding(s);
3118 return;
3120 switch (o3_opc) {
3121 case 000: /* LDADD */
3122 fn = tcg_gen_atomic_fetch_add_i64;
3123 break;
3124 case 001: /* LDCLR */
3125 fn = tcg_gen_atomic_fetch_and_i64;
3126 break;
3127 case 002: /* LDEOR */
3128 fn = tcg_gen_atomic_fetch_xor_i64;
3129 break;
3130 case 003: /* LDSET */
3131 fn = tcg_gen_atomic_fetch_or_i64;
3132 break;
3133 case 004: /* LDSMAX */
3134 fn = tcg_gen_atomic_fetch_smax_i64;
3135 break;
3136 case 005: /* LDSMIN */
3137 fn = tcg_gen_atomic_fetch_smin_i64;
3138 break;
3139 case 006: /* LDUMAX */
3140 fn = tcg_gen_atomic_fetch_umax_i64;
3141 break;
3142 case 007: /* LDUMIN */
3143 fn = tcg_gen_atomic_fetch_umin_i64;
3144 break;
3145 case 010: /* SWP */
3146 fn = tcg_gen_atomic_xchg_i64;
3147 break;
3148 case 014: /* LDAPR, LDAPRH, LDAPRB */
3149 if (!dc_isar_feature(aa64_rcpc_8_3, s) ||
3150 rs != 31 || a != 1 || r != 0) {
3151 unallocated_encoding(s);
3152 return;
3154 break;
3155 default:
3156 unallocated_encoding(s);
3157 return;
3160 if (rn == 31) {
3161 gen_check_sp_alignment(s);
3163 clean_addr = clean_data_tbi(s, cpu_reg_sp(s, rn));
3165 if (o3_opc == 014) {
3167 * LDAPR* are a special case because they are a simple load, not a
3168 * fetch-and-do-something op.
3169 * The architectural consistency requirements here are weaker than
3170 * full load-acquire (we only need "load-acquire processor consistent"),
3171 * but we choose to implement them as full LDAQ.
3173 do_gpr_ld(s, cpu_reg(s, rt), clean_addr, size, false, false,
3174 true, rt, disas_ldst_compute_iss_sf(size, false, 0), true);
3175 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
3176 return;
3179 tcg_rs = read_cpu_reg(s, rs, true);
3181 if (o3_opc == 1) { /* LDCLR */
3182 tcg_gen_not_i64(tcg_rs, tcg_rs);
3185 /* The tcg atomic primitives are all full barriers. Therefore we
3186 * can ignore the Acquire and Release bits of this instruction.
3188 fn(cpu_reg(s, rt), clean_addr, tcg_rs, get_mem_index(s),
3189 s->be_data | size | MO_ALIGN);
3193 * PAC memory operations
3195 * 31 30 27 26 24 22 21 12 11 10 5 0
3196 * +------+-------+---+-----+-----+---+--------+---+---+----+-----+
3197 * | size | 1 1 1 | V | 0 0 | M S | 1 | imm9 | W | 1 | Rn | Rt |
3198 * +------+-------+---+-----+-----+---+--------+---+---+----+-----+
3200 * Rt: the result register
3201 * Rn: base address or SP
3202 * V: vector flag (always 0 as of v8.3)
3203 * M: clear for key DA, set for key DB
3204 * W: pre-indexing flag
3205 * S: sign for imm9.
3207 static void disas_ldst_pac(DisasContext *s, uint32_t insn,
3208 int size, int rt, bool is_vector)
3210 int rn = extract32(insn, 5, 5);
3211 bool is_wback = extract32(insn, 11, 1);
3212 bool use_key_a = !extract32(insn, 23, 1);
3213 int offset;
3214 TCGv_i64 clean_addr, dirty_addr, tcg_rt;
3216 if (size != 3 || is_vector || !dc_isar_feature(aa64_pauth, s)) {
3217 unallocated_encoding(s);
3218 return;
3221 if (rn == 31) {
3222 gen_check_sp_alignment(s);
3224 dirty_addr = read_cpu_reg_sp(s, rn, 1);
3226 if (s->pauth_active) {
3227 if (use_key_a) {
3228 gen_helper_autda(dirty_addr, cpu_env, dirty_addr, cpu_X[31]);
3229 } else {
3230 gen_helper_autdb(dirty_addr, cpu_env, dirty_addr, cpu_X[31]);
3234 /* Form the 10-bit signed, scaled offset. */
3235 offset = (extract32(insn, 22, 1) << 9) | extract32(insn, 12, 9);
3236 offset = sextract32(offset << size, 0, 10 + size);
3237 tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3239 /* Note that "clean" and "dirty" here refer to TBI not PAC. */
3240 clean_addr = clean_data_tbi(s, dirty_addr);
3242 tcg_rt = cpu_reg(s, rt);
3243 do_gpr_ld(s, tcg_rt, clean_addr, size, /* is_signed */ false,
3244 /* extend */ false, /* iss_valid */ !is_wback,
3245 /* iss_srt */ rt, /* iss_sf */ true, /* iss_ar */ false);
3247 if (is_wback) {
3248 tcg_gen_mov_i64(cpu_reg_sp(s, rn), dirty_addr);
3253 * LDAPR/STLR (unscaled immediate)
3255 * 31 30 24 22 21 12 10 5 0
3256 * +------+-------------+-----+---+--------+-----+----+-----+
3257 * | size | 0 1 1 0 0 1 | opc | 0 | imm9 | 0 0 | Rn | Rt |
3258 * +------+-------------+-----+---+--------+-----+----+-----+
3260 * Rt: source or destination register
3261 * Rn: base register
3262 * imm9: unscaled immediate offset
3263 * opc: 00: STLUR*, 01/10/11: various LDAPUR*
3264 * size: size of load/store
3266 static void disas_ldst_ldapr_stlr(DisasContext *s, uint32_t insn)
3268 int rt = extract32(insn, 0, 5);
3269 int rn = extract32(insn, 5, 5);
3270 int offset = sextract32(insn, 12, 9);
3271 int opc = extract32(insn, 22, 2);
3272 int size = extract32(insn, 30, 2);
3273 TCGv_i64 clean_addr, dirty_addr;
3274 bool is_store = false;
3275 bool is_signed = false;
3276 bool extend = false;
3277 bool iss_sf;
3279 if (!dc_isar_feature(aa64_rcpc_8_4, s)) {
3280 unallocated_encoding(s);
3281 return;
3284 switch (opc) {
3285 case 0: /* STLURB */
3286 is_store = true;
3287 break;
3288 case 1: /* LDAPUR* */
3289 break;
3290 case 2: /* LDAPURS* 64-bit variant */
3291 if (size == 3) {
3292 unallocated_encoding(s);
3293 return;
3295 is_signed = true;
3296 break;
3297 case 3: /* LDAPURS* 32-bit variant */
3298 if (size > 1) {
3299 unallocated_encoding(s);
3300 return;
3302 is_signed = true;
3303 extend = true; /* zero-extend 32->64 after signed load */
3304 break;
3305 default:
3306 g_assert_not_reached();
3309 iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
3311 if (rn == 31) {
3312 gen_check_sp_alignment(s);
3315 dirty_addr = read_cpu_reg_sp(s, rn, 1);
3316 tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3317 clean_addr = clean_data_tbi(s, dirty_addr);
3319 if (is_store) {
3320 /* Store-Release semantics */
3321 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
3322 do_gpr_st(s, cpu_reg(s, rt), clean_addr, size, true, rt, iss_sf, true);
3323 } else {
3325 * Load-AcquirePC semantics; we implement as the slightly more
3326 * restrictive Load-Acquire.
3328 do_gpr_ld(s, cpu_reg(s, rt), clean_addr, size, is_signed, extend,
3329 true, rt, iss_sf, true);
3330 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
3334 /* Load/store register (all forms) */
3335 static void disas_ldst_reg(DisasContext *s, uint32_t insn)
3337 int rt = extract32(insn, 0, 5);
3338 int opc = extract32(insn, 22, 2);
3339 bool is_vector = extract32(insn, 26, 1);
3340 int size = extract32(insn, 30, 2);
3342 switch (extract32(insn, 24, 2)) {
3343 case 0:
3344 if (extract32(insn, 21, 1) == 0) {
3345 /* Load/store register (unscaled immediate)
3346 * Load/store immediate pre/post-indexed
3347 * Load/store register unprivileged
3349 disas_ldst_reg_imm9(s, insn, opc, size, rt, is_vector);
3350 return;
3352 switch (extract32(insn, 10, 2)) {
3353 case 0:
3354 disas_ldst_atomic(s, insn, size, rt, is_vector);
3355 return;
3356 case 2:
3357 disas_ldst_reg_roffset(s, insn, opc, size, rt, is_vector);
3358 return;
3359 default:
3360 disas_ldst_pac(s, insn, size, rt, is_vector);
3361 return;
3363 break;
3364 case 1:
3365 disas_ldst_reg_unsigned_imm(s, insn, opc, size, rt, is_vector);
3366 return;
3368 unallocated_encoding(s);
3371 /* AdvSIMD load/store multiple structures
3373 * 31 30 29 23 22 21 16 15 12 11 10 9 5 4 0
3374 * +---+---+---------------+---+-------------+--------+------+------+------+
3375 * | 0 | Q | 0 0 1 1 0 0 0 | L | 0 0 0 0 0 0 | opcode | size | Rn | Rt |
3376 * +---+---+---------------+---+-------------+--------+------+------+------+
3378 * AdvSIMD load/store multiple structures (post-indexed)
3380 * 31 30 29 23 22 21 20 16 15 12 11 10 9 5 4 0
3381 * +---+---+---------------+---+---+---------+--------+------+------+------+
3382 * | 0 | Q | 0 0 1 1 0 0 1 | L | 0 | Rm | opcode | size | Rn | Rt |
3383 * +---+---+---------------+---+---+---------+--------+------+------+------+
3385 * Rt: first (or only) SIMD&FP register to be transferred
3386 * Rn: base address or SP
3387 * Rm (post-index only): post-index register (when !31) or size dependent #imm
3389 static void disas_ldst_multiple_struct(DisasContext *s, uint32_t insn)
3391 int rt = extract32(insn, 0, 5);
3392 int rn = extract32(insn, 5, 5);
3393 int rm = extract32(insn, 16, 5);
3394 int size = extract32(insn, 10, 2);
3395 int opcode = extract32(insn, 12, 4);
3396 bool is_store = !extract32(insn, 22, 1);
3397 bool is_postidx = extract32(insn, 23, 1);
3398 bool is_q = extract32(insn, 30, 1);
3399 TCGv_i64 clean_addr, tcg_rn, tcg_ebytes;
3400 MemOp endian = s->be_data;
3402 int ebytes; /* bytes per element */
3403 int elements; /* elements per vector */
3404 int rpt; /* num iterations */
3405 int selem; /* structure elements */
3406 int r;
3408 if (extract32(insn, 31, 1) || extract32(insn, 21, 1)) {
3409 unallocated_encoding(s);
3410 return;
3413 if (!is_postidx && rm != 0) {
3414 unallocated_encoding(s);
3415 return;
3418 /* From the shared decode logic */
3419 switch (opcode) {
3420 case 0x0:
3421 rpt = 1;
3422 selem = 4;
3423 break;
3424 case 0x2:
3425 rpt = 4;
3426 selem = 1;
3427 break;
3428 case 0x4:
3429 rpt = 1;
3430 selem = 3;
3431 break;
3432 case 0x6:
3433 rpt = 3;
3434 selem = 1;
3435 break;
3436 case 0x7:
3437 rpt = 1;
3438 selem = 1;
3439 break;
3440 case 0x8:
3441 rpt = 1;
3442 selem = 2;
3443 break;
3444 case 0xa:
3445 rpt = 2;
3446 selem = 1;
3447 break;
3448 default:
3449 unallocated_encoding(s);
3450 return;
3453 if (size == 3 && !is_q && selem != 1) {
3454 /* reserved */
3455 unallocated_encoding(s);
3456 return;
3459 if (!fp_access_check(s)) {
3460 return;
3463 if (rn == 31) {
3464 gen_check_sp_alignment(s);
3467 /* For our purposes, bytes are always little-endian. */
3468 if (size == 0) {
3469 endian = MO_LE;
3472 /* Consecutive little-endian elements from a single register
3473 * can be promoted to a larger little-endian operation.
3475 if (selem == 1 && endian == MO_LE) {
3476 size = 3;
3478 ebytes = 1 << size;
3479 elements = (is_q ? 16 : 8) / ebytes;
3481 tcg_rn = cpu_reg_sp(s, rn);
3482 clean_addr = clean_data_tbi(s, tcg_rn);
3483 tcg_ebytes = tcg_const_i64(ebytes);
3485 for (r = 0; r < rpt; r++) {
3486 int e;
3487 for (e = 0; e < elements; e++) {
3488 int xs;
3489 for (xs = 0; xs < selem; xs++) {
3490 int tt = (rt + r + xs) % 32;
3491 if (is_store) {
3492 do_vec_st(s, tt, e, clean_addr, size, endian);
3493 } else {
3494 do_vec_ld(s, tt, e, clean_addr, size, endian);
3496 tcg_gen_add_i64(clean_addr, clean_addr, tcg_ebytes);
3500 tcg_temp_free_i64(tcg_ebytes);
3502 if (!is_store) {
3503 /* For non-quad operations, setting a slice of the low
3504 * 64 bits of the register clears the high 64 bits (in
3505 * the ARM ARM pseudocode this is implicit in the fact
3506 * that 'rval' is a 64 bit wide variable).
3507 * For quad operations, we might still need to zero the
3508 * high bits of SVE.
3510 for (r = 0; r < rpt * selem; r++) {
3511 int tt = (rt + r) % 32;
3512 clear_vec_high(s, is_q, tt);
3516 if (is_postidx) {
3517 if (rm == 31) {
3518 tcg_gen_addi_i64(tcg_rn, tcg_rn, rpt * elements * selem * ebytes);
3519 } else {
3520 tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, rm));
3525 /* AdvSIMD load/store single structure
3527 * 31 30 29 23 22 21 20 16 15 13 12 11 10 9 5 4 0
3528 * +---+---+---------------+-----+-----------+-----+---+------+------+------+
3529 * | 0 | Q | 0 0 1 1 0 1 0 | L R | 0 0 0 0 0 | opc | S | size | Rn | Rt |
3530 * +---+---+---------------+-----+-----------+-----+---+------+------+------+
3532 * AdvSIMD load/store single structure (post-indexed)
3534 * 31 30 29 23 22 21 20 16 15 13 12 11 10 9 5 4 0
3535 * +---+---+---------------+-----+-----------+-----+---+------+------+------+
3536 * | 0 | Q | 0 0 1 1 0 1 1 | L R | Rm | opc | S | size | Rn | Rt |
3537 * +---+---+---------------+-----+-----------+-----+---+------+------+------+
3539 * Rt: first (or only) SIMD&FP register to be transferred
3540 * Rn: base address or SP
3541 * Rm (post-index only): post-index register (when !31) or size dependent #imm
3542 * index = encoded in Q:S:size dependent on size
3544 * lane_size = encoded in R, opc
3545 * transfer width = encoded in opc, S, size
3547 static void disas_ldst_single_struct(DisasContext *s, uint32_t insn)
3549 int rt = extract32(insn, 0, 5);
3550 int rn = extract32(insn, 5, 5);
3551 int rm = extract32(insn, 16, 5);
3552 int size = extract32(insn, 10, 2);
3553 int S = extract32(insn, 12, 1);
3554 int opc = extract32(insn, 13, 3);
3555 int R = extract32(insn, 21, 1);
3556 int is_load = extract32(insn, 22, 1);
3557 int is_postidx = extract32(insn, 23, 1);
3558 int is_q = extract32(insn, 30, 1);
3560 int scale = extract32(opc, 1, 2);
3561 int selem = (extract32(opc, 0, 1) << 1 | R) + 1;
3562 bool replicate = false;
3563 int index = is_q << 3 | S << 2 | size;
3564 int ebytes, xs;
3565 TCGv_i64 clean_addr, tcg_rn, tcg_ebytes;
3567 if (extract32(insn, 31, 1)) {
3568 unallocated_encoding(s);
3569 return;
3571 if (!is_postidx && rm != 0) {
3572 unallocated_encoding(s);
3573 return;
3576 switch (scale) {
3577 case 3:
3578 if (!is_load || S) {
3579 unallocated_encoding(s);
3580 return;
3582 scale = size;
3583 replicate = true;
3584 break;
3585 case 0:
3586 break;
3587 case 1:
3588 if (extract32(size, 0, 1)) {
3589 unallocated_encoding(s);
3590 return;
3592 index >>= 1;
3593 break;
3594 case 2:
3595 if (extract32(size, 1, 1)) {
3596 unallocated_encoding(s);
3597 return;
3599 if (!extract32(size, 0, 1)) {
3600 index >>= 2;
3601 } else {
3602 if (S) {
3603 unallocated_encoding(s);
3604 return;
3606 index >>= 3;
3607 scale = 3;
3609 break;
3610 default:
3611 g_assert_not_reached();
3614 if (!fp_access_check(s)) {
3615 return;
3618 ebytes = 1 << scale;
3620 if (rn == 31) {
3621 gen_check_sp_alignment(s);
3624 tcg_rn = cpu_reg_sp(s, rn);
3625 clean_addr = clean_data_tbi(s, tcg_rn);
3626 tcg_ebytes = tcg_const_i64(ebytes);
3628 for (xs = 0; xs < selem; xs++) {
3629 if (replicate) {
3630 /* Load and replicate to all elements */
3631 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
3633 tcg_gen_qemu_ld_i64(tcg_tmp, clean_addr,
3634 get_mem_index(s), s->be_data + scale);
3635 tcg_gen_gvec_dup_i64(scale, vec_full_reg_offset(s, rt),
3636 (is_q + 1) * 8, vec_full_reg_size(s),
3637 tcg_tmp);
3638 tcg_temp_free_i64(tcg_tmp);
3639 } else {
3640 /* Load/store one element per register */
3641 if (is_load) {
3642 do_vec_ld(s, rt, index, clean_addr, scale, s->be_data);
3643 } else {
3644 do_vec_st(s, rt, index, clean_addr, scale, s->be_data);
3647 tcg_gen_add_i64(clean_addr, clean_addr, tcg_ebytes);
3648 rt = (rt + 1) % 32;
3650 tcg_temp_free_i64(tcg_ebytes);
3652 if (is_postidx) {
3653 if (rm == 31) {
3654 tcg_gen_addi_i64(tcg_rn, tcg_rn, selem * ebytes);
3655 } else {
3656 tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, rm));
3661 /* Loads and stores */
3662 static void disas_ldst(DisasContext *s, uint32_t insn)
3664 switch (extract32(insn, 24, 6)) {
3665 case 0x08: /* Load/store exclusive */
3666 disas_ldst_excl(s, insn);
3667 break;
3668 case 0x18: case 0x1c: /* Load register (literal) */
3669 disas_ld_lit(s, insn);
3670 break;
3671 case 0x28: case 0x29:
3672 case 0x2c: case 0x2d: /* Load/store pair (all forms) */
3673 disas_ldst_pair(s, insn);
3674 break;
3675 case 0x38: case 0x39:
3676 case 0x3c: case 0x3d: /* Load/store register (all forms) */
3677 disas_ldst_reg(s, insn);
3678 break;
3679 case 0x0c: /* AdvSIMD load/store multiple structures */
3680 disas_ldst_multiple_struct(s, insn);
3681 break;
3682 case 0x0d: /* AdvSIMD load/store single structure */
3683 disas_ldst_single_struct(s, insn);
3684 break;
3685 case 0x19: /* LDAPR/STLR (unscaled immediate) */
3686 if (extract32(insn, 10, 2) != 0 ||
3687 extract32(insn, 21, 1) != 0) {
3688 unallocated_encoding(s);
3689 break;
3691 disas_ldst_ldapr_stlr(s, insn);
3692 break;
3693 default:
3694 unallocated_encoding(s);
3695 break;
3699 /* PC-rel. addressing
3700 * 31 30 29 28 24 23 5 4 0
3701 * +----+-------+-----------+-------------------+------+
3702 * | op | immlo | 1 0 0 0 0 | immhi | Rd |
3703 * +----+-------+-----------+-------------------+------+
3705 static void disas_pc_rel_adr(DisasContext *s, uint32_t insn)
3707 unsigned int page, rd;
3708 uint64_t base;
3709 uint64_t offset;
3711 page = extract32(insn, 31, 1);
3712 /* SignExtend(immhi:immlo) -> offset */
3713 offset = sextract64(insn, 5, 19);
3714 offset = offset << 2 | extract32(insn, 29, 2);
3715 rd = extract32(insn, 0, 5);
3716 base = s->pc_curr;
3718 if (page) {
3719 /* ADRP (page based) */
3720 base &= ~0xfff;
3721 offset <<= 12;
3724 tcg_gen_movi_i64(cpu_reg(s, rd), base + offset);
3728 * Add/subtract (immediate)
3730 * 31 30 29 28 24 23 22 21 10 9 5 4 0
3731 * +--+--+--+-----------+-----+-------------+-----+-----+
3732 * |sf|op| S| 1 0 0 0 1 |shift| imm12 | Rn | Rd |
3733 * +--+--+--+-----------+-----+-------------+-----+-----+
3735 * sf: 0 -> 32bit, 1 -> 64bit
3736 * op: 0 -> add , 1 -> sub
3737 * S: 1 -> set flags
3738 * shift: 00 -> LSL imm by 0, 01 -> LSL imm by 12
3740 static void disas_add_sub_imm(DisasContext *s, uint32_t insn)
3742 int rd = extract32(insn, 0, 5);
3743 int rn = extract32(insn, 5, 5);
3744 uint64_t imm = extract32(insn, 10, 12);
3745 int shift = extract32(insn, 22, 2);
3746 bool setflags = extract32(insn, 29, 1);
3747 bool sub_op = extract32(insn, 30, 1);
3748 bool is_64bit = extract32(insn, 31, 1);
3750 TCGv_i64 tcg_rn = cpu_reg_sp(s, rn);
3751 TCGv_i64 tcg_rd = setflags ? cpu_reg(s, rd) : cpu_reg_sp(s, rd);
3752 TCGv_i64 tcg_result;
3754 switch (shift) {
3755 case 0x0:
3756 break;
3757 case 0x1:
3758 imm <<= 12;
3759 break;
3760 default:
3761 unallocated_encoding(s);
3762 return;
3765 tcg_result = tcg_temp_new_i64();
3766 if (!setflags) {
3767 if (sub_op) {
3768 tcg_gen_subi_i64(tcg_result, tcg_rn, imm);
3769 } else {
3770 tcg_gen_addi_i64(tcg_result, tcg_rn, imm);
3772 } else {
3773 TCGv_i64 tcg_imm = tcg_const_i64(imm);
3774 if (sub_op) {
3775 gen_sub_CC(is_64bit, tcg_result, tcg_rn, tcg_imm);
3776 } else {
3777 gen_add_CC(is_64bit, tcg_result, tcg_rn, tcg_imm);
3779 tcg_temp_free_i64(tcg_imm);
3782 if (is_64bit) {
3783 tcg_gen_mov_i64(tcg_rd, tcg_result);
3784 } else {
3785 tcg_gen_ext32u_i64(tcg_rd, tcg_result);
3788 tcg_temp_free_i64(tcg_result);
3791 /* The input should be a value in the bottom e bits (with higher
3792 * bits zero); returns that value replicated into every element
3793 * of size e in a 64 bit integer.
3795 static uint64_t bitfield_replicate(uint64_t mask, unsigned int e)
3797 assert(e != 0);
3798 while (e < 64) {
3799 mask |= mask << e;
3800 e *= 2;
3802 return mask;
3805 /* Return a value with the bottom len bits set (where 0 < len <= 64) */
3806 static inline uint64_t bitmask64(unsigned int length)
3808 assert(length > 0 && length <= 64);
3809 return ~0ULL >> (64 - length);
3812 /* Simplified variant of pseudocode DecodeBitMasks() for the case where we
3813 * only require the wmask. Returns false if the imms/immr/immn are a reserved
3814 * value (ie should cause a guest UNDEF exception), and true if they are
3815 * valid, in which case the decoded bit pattern is written to result.
3817 bool logic_imm_decode_wmask(uint64_t *result, unsigned int immn,
3818 unsigned int imms, unsigned int immr)
3820 uint64_t mask;
3821 unsigned e, levels, s, r;
3822 int len;
3824 assert(immn < 2 && imms < 64 && immr < 64);
3826 /* The bit patterns we create here are 64 bit patterns which
3827 * are vectors of identical elements of size e = 2, 4, 8, 16, 32 or
3828 * 64 bits each. Each element contains the same value: a run
3829 * of between 1 and e-1 non-zero bits, rotated within the
3830 * element by between 0 and e-1 bits.
3832 * The element size and run length are encoded into immn (1 bit)
3833 * and imms (6 bits) as follows:
3834 * 64 bit elements: immn = 1, imms = <length of run - 1>
3835 * 32 bit elements: immn = 0, imms = 0 : <length of run - 1>
3836 * 16 bit elements: immn = 0, imms = 10 : <length of run - 1>
3837 * 8 bit elements: immn = 0, imms = 110 : <length of run - 1>
3838 * 4 bit elements: immn = 0, imms = 1110 : <length of run - 1>
3839 * 2 bit elements: immn = 0, imms = 11110 : <length of run - 1>
3840 * Notice that immn = 0, imms = 11111x is the only combination
3841 * not covered by one of the above options; this is reserved.
3842 * Further, <length of run - 1> all-ones is a reserved pattern.
3844 * In all cases the rotation is by immr % e (and immr is 6 bits).
3847 /* First determine the element size */
3848 len = 31 - clz32((immn << 6) | (~imms & 0x3f));
3849 if (len < 1) {
3850 /* This is the immn == 0, imms == 0x11111x case */
3851 return false;
3853 e = 1 << len;
3855 levels = e - 1;
3856 s = imms & levels;
3857 r = immr & levels;
3859 if (s == levels) {
3860 /* <length of run - 1> mustn't be all-ones. */
3861 return false;
3864 /* Create the value of one element: s+1 set bits rotated
3865 * by r within the element (which is e bits wide)...
3867 mask = bitmask64(s + 1);
3868 if (r) {
3869 mask = (mask >> r) | (mask << (e - r));
3870 mask &= bitmask64(e);
3872 /* ...then replicate the element over the whole 64 bit value */
3873 mask = bitfield_replicate(mask, e);
3874 *result = mask;
3875 return true;
3878 /* Logical (immediate)
3879 * 31 30 29 28 23 22 21 16 15 10 9 5 4 0
3880 * +----+-----+-------------+---+------+------+------+------+
3881 * | sf | opc | 1 0 0 1 0 0 | N | immr | imms | Rn | Rd |
3882 * +----+-----+-------------+---+------+------+------+------+
3884 static void disas_logic_imm(DisasContext *s, uint32_t insn)
3886 unsigned int sf, opc, is_n, immr, imms, rn, rd;
3887 TCGv_i64 tcg_rd, tcg_rn;
3888 uint64_t wmask;
3889 bool is_and = false;
3891 sf = extract32(insn, 31, 1);
3892 opc = extract32(insn, 29, 2);
3893 is_n = extract32(insn, 22, 1);
3894 immr = extract32(insn, 16, 6);
3895 imms = extract32(insn, 10, 6);
3896 rn = extract32(insn, 5, 5);
3897 rd = extract32(insn, 0, 5);
3899 if (!sf && is_n) {
3900 unallocated_encoding(s);
3901 return;
3904 if (opc == 0x3) { /* ANDS */
3905 tcg_rd = cpu_reg(s, rd);
3906 } else {
3907 tcg_rd = cpu_reg_sp(s, rd);
3909 tcg_rn = cpu_reg(s, rn);
3911 if (!logic_imm_decode_wmask(&wmask, is_n, imms, immr)) {
3912 /* some immediate field values are reserved */
3913 unallocated_encoding(s);
3914 return;
3917 if (!sf) {
3918 wmask &= 0xffffffff;
3921 switch (opc) {
3922 case 0x3: /* ANDS */
3923 case 0x0: /* AND */
3924 tcg_gen_andi_i64(tcg_rd, tcg_rn, wmask);
3925 is_and = true;
3926 break;
3927 case 0x1: /* ORR */
3928 tcg_gen_ori_i64(tcg_rd, tcg_rn, wmask);
3929 break;
3930 case 0x2: /* EOR */
3931 tcg_gen_xori_i64(tcg_rd, tcg_rn, wmask);
3932 break;
3933 default:
3934 assert(FALSE); /* must handle all above */
3935 break;
3938 if (!sf && !is_and) {
3939 /* zero extend final result; we know we can skip this for AND
3940 * since the immediate had the high 32 bits clear.
3942 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3945 if (opc == 3) { /* ANDS */
3946 gen_logic_CC(sf, tcg_rd);
3951 * Move wide (immediate)
3953 * 31 30 29 28 23 22 21 20 5 4 0
3954 * +--+-----+-------------+-----+----------------+------+
3955 * |sf| opc | 1 0 0 1 0 1 | hw | imm16 | Rd |
3956 * +--+-----+-------------+-----+----------------+------+
3958 * sf: 0 -> 32 bit, 1 -> 64 bit
3959 * opc: 00 -> N, 10 -> Z, 11 -> K
3960 * hw: shift/16 (0,16, and sf only 32, 48)
3962 static void disas_movw_imm(DisasContext *s, uint32_t insn)
3964 int rd = extract32(insn, 0, 5);
3965 uint64_t imm = extract32(insn, 5, 16);
3966 int sf = extract32(insn, 31, 1);
3967 int opc = extract32(insn, 29, 2);
3968 int pos = extract32(insn, 21, 2) << 4;
3969 TCGv_i64 tcg_rd = cpu_reg(s, rd);
3970 TCGv_i64 tcg_imm;
3972 if (!sf && (pos >= 32)) {
3973 unallocated_encoding(s);
3974 return;
3977 switch (opc) {
3978 case 0: /* MOVN */
3979 case 2: /* MOVZ */
3980 imm <<= pos;
3981 if (opc == 0) {
3982 imm = ~imm;
3984 if (!sf) {
3985 imm &= 0xffffffffu;
3987 tcg_gen_movi_i64(tcg_rd, imm);
3988 break;
3989 case 3: /* MOVK */
3990 tcg_imm = tcg_const_i64(imm);
3991 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_imm, pos, 16);
3992 tcg_temp_free_i64(tcg_imm);
3993 if (!sf) {
3994 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3996 break;
3997 default:
3998 unallocated_encoding(s);
3999 break;
4003 /* Bitfield
4004 * 31 30 29 28 23 22 21 16 15 10 9 5 4 0
4005 * +----+-----+-------------+---+------+------+------+------+
4006 * | sf | opc | 1 0 0 1 1 0 | N | immr | imms | Rn | Rd |
4007 * +----+-----+-------------+---+------+------+------+------+
4009 static void disas_bitfield(DisasContext *s, uint32_t insn)
4011 unsigned int sf, n, opc, ri, si, rn, rd, bitsize, pos, len;
4012 TCGv_i64 tcg_rd, tcg_tmp;
4014 sf = extract32(insn, 31, 1);
4015 opc = extract32(insn, 29, 2);
4016 n = extract32(insn, 22, 1);
4017 ri = extract32(insn, 16, 6);
4018 si = extract32(insn, 10, 6);
4019 rn = extract32(insn, 5, 5);
4020 rd = extract32(insn, 0, 5);
4021 bitsize = sf ? 64 : 32;
4023 if (sf != n || ri >= bitsize || si >= bitsize || opc > 2) {
4024 unallocated_encoding(s);
4025 return;
4028 tcg_rd = cpu_reg(s, rd);
4030 /* Suppress the zero-extend for !sf. Since RI and SI are constrained
4031 to be smaller than bitsize, we'll never reference data outside the
4032 low 32-bits anyway. */
4033 tcg_tmp = read_cpu_reg(s, rn, 1);
4035 /* Recognize simple(r) extractions. */
4036 if (si >= ri) {
4037 /* Wd<s-r:0> = Wn<s:r> */
4038 len = (si - ri) + 1;
4039 if (opc == 0) { /* SBFM: ASR, SBFX, SXTB, SXTH, SXTW */
4040 tcg_gen_sextract_i64(tcg_rd, tcg_tmp, ri, len);
4041 goto done;
4042 } else if (opc == 2) { /* UBFM: UBFX, LSR, UXTB, UXTH */
4043 tcg_gen_extract_i64(tcg_rd, tcg_tmp, ri, len);
4044 return;
4046 /* opc == 1, BFXIL fall through to deposit */
4047 tcg_gen_shri_i64(tcg_tmp, tcg_tmp, ri);
4048 pos = 0;
4049 } else {
4050 /* Handle the ri > si case with a deposit
4051 * Wd<32+s-r,32-r> = Wn<s:0>
4053 len = si + 1;
4054 pos = (bitsize - ri) & (bitsize - 1);
4057 if (opc == 0 && len < ri) {
4058 /* SBFM: sign extend the destination field from len to fill
4059 the balance of the word. Let the deposit below insert all
4060 of those sign bits. */
4061 tcg_gen_sextract_i64(tcg_tmp, tcg_tmp, 0, len);
4062 len = ri;
4065 if (opc == 1) { /* BFM, BFXIL */
4066 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, pos, len);
4067 } else {
4068 /* SBFM or UBFM: We start with zero, and we haven't modified
4069 any bits outside bitsize, therefore the zero-extension
4070 below is unneeded. */
4071 tcg_gen_deposit_z_i64(tcg_rd, tcg_tmp, pos, len);
4072 return;
4075 done:
4076 if (!sf) { /* zero extend final result */
4077 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4081 /* Extract
4082 * 31 30 29 28 23 22 21 20 16 15 10 9 5 4 0
4083 * +----+------+-------------+---+----+------+--------+------+------+
4084 * | sf | op21 | 1 0 0 1 1 1 | N | o0 | Rm | imms | Rn | Rd |
4085 * +----+------+-------------+---+----+------+--------+------+------+
4087 static void disas_extract(DisasContext *s, uint32_t insn)
4089 unsigned int sf, n, rm, imm, rn, rd, bitsize, op21, op0;
4091 sf = extract32(insn, 31, 1);
4092 n = extract32(insn, 22, 1);
4093 rm = extract32(insn, 16, 5);
4094 imm = extract32(insn, 10, 6);
4095 rn = extract32(insn, 5, 5);
4096 rd = extract32(insn, 0, 5);
4097 op21 = extract32(insn, 29, 2);
4098 op0 = extract32(insn, 21, 1);
4099 bitsize = sf ? 64 : 32;
4101 if (sf != n || op21 || op0 || imm >= bitsize) {
4102 unallocated_encoding(s);
4103 } else {
4104 TCGv_i64 tcg_rd, tcg_rm, tcg_rn;
4106 tcg_rd = cpu_reg(s, rd);
4108 if (unlikely(imm == 0)) {
4109 /* tcg shl_i32/shl_i64 is undefined for 32/64 bit shifts,
4110 * so an extract from bit 0 is a special case.
4112 if (sf) {
4113 tcg_gen_mov_i64(tcg_rd, cpu_reg(s, rm));
4114 } else {
4115 tcg_gen_ext32u_i64(tcg_rd, cpu_reg(s, rm));
4117 } else {
4118 tcg_rm = cpu_reg(s, rm);
4119 tcg_rn = cpu_reg(s, rn);
4121 if (sf) {
4122 /* Specialization to ROR happens in EXTRACT2. */
4123 tcg_gen_extract2_i64(tcg_rd, tcg_rm, tcg_rn, imm);
4124 } else {
4125 TCGv_i32 t0 = tcg_temp_new_i32();
4127 tcg_gen_extrl_i64_i32(t0, tcg_rm);
4128 if (rm == rn) {
4129 tcg_gen_rotri_i32(t0, t0, imm);
4130 } else {
4131 TCGv_i32 t1 = tcg_temp_new_i32();
4132 tcg_gen_extrl_i64_i32(t1, tcg_rn);
4133 tcg_gen_extract2_i32(t0, t0, t1, imm);
4134 tcg_temp_free_i32(t1);
4136 tcg_gen_extu_i32_i64(tcg_rd, t0);
4137 tcg_temp_free_i32(t0);
4143 /* Data processing - immediate */
4144 static void disas_data_proc_imm(DisasContext *s, uint32_t insn)
4146 switch (extract32(insn, 23, 6)) {
4147 case 0x20: case 0x21: /* PC-rel. addressing */
4148 disas_pc_rel_adr(s, insn);
4149 break;
4150 case 0x22: case 0x23: /* Add/subtract (immediate) */
4151 disas_add_sub_imm(s, insn);
4152 break;
4153 case 0x24: /* Logical (immediate) */
4154 disas_logic_imm(s, insn);
4155 break;
4156 case 0x25: /* Move wide (immediate) */
4157 disas_movw_imm(s, insn);
4158 break;
4159 case 0x26: /* Bitfield */
4160 disas_bitfield(s, insn);
4161 break;
4162 case 0x27: /* Extract */
4163 disas_extract(s, insn);
4164 break;
4165 default:
4166 unallocated_encoding(s);
4167 break;
4171 /* Shift a TCGv src by TCGv shift_amount, put result in dst.
4172 * Note that it is the caller's responsibility to ensure that the
4173 * shift amount is in range (ie 0..31 or 0..63) and provide the ARM
4174 * mandated semantics for out of range shifts.
4176 static void shift_reg(TCGv_i64 dst, TCGv_i64 src, int sf,
4177 enum a64_shift_type shift_type, TCGv_i64 shift_amount)
4179 switch (shift_type) {
4180 case A64_SHIFT_TYPE_LSL:
4181 tcg_gen_shl_i64(dst, src, shift_amount);
4182 break;
4183 case A64_SHIFT_TYPE_LSR:
4184 tcg_gen_shr_i64(dst, src, shift_amount);
4185 break;
4186 case A64_SHIFT_TYPE_ASR:
4187 if (!sf) {
4188 tcg_gen_ext32s_i64(dst, src);
4190 tcg_gen_sar_i64(dst, sf ? src : dst, shift_amount);
4191 break;
4192 case A64_SHIFT_TYPE_ROR:
4193 if (sf) {
4194 tcg_gen_rotr_i64(dst, src, shift_amount);
4195 } else {
4196 TCGv_i32 t0, t1;
4197 t0 = tcg_temp_new_i32();
4198 t1 = tcg_temp_new_i32();
4199 tcg_gen_extrl_i64_i32(t0, src);
4200 tcg_gen_extrl_i64_i32(t1, shift_amount);
4201 tcg_gen_rotr_i32(t0, t0, t1);
4202 tcg_gen_extu_i32_i64(dst, t0);
4203 tcg_temp_free_i32(t0);
4204 tcg_temp_free_i32(t1);
4206 break;
4207 default:
4208 assert(FALSE); /* all shift types should be handled */
4209 break;
4212 if (!sf) { /* zero extend final result */
4213 tcg_gen_ext32u_i64(dst, dst);
4217 /* Shift a TCGv src by immediate, put result in dst.
4218 * The shift amount must be in range (this should always be true as the
4219 * relevant instructions will UNDEF on bad shift immediates).
4221 static void shift_reg_imm(TCGv_i64 dst, TCGv_i64 src, int sf,
4222 enum a64_shift_type shift_type, unsigned int shift_i)
4224 assert(shift_i < (sf ? 64 : 32));
4226 if (shift_i == 0) {
4227 tcg_gen_mov_i64(dst, src);
4228 } else {
4229 TCGv_i64 shift_const;
4231 shift_const = tcg_const_i64(shift_i);
4232 shift_reg(dst, src, sf, shift_type, shift_const);
4233 tcg_temp_free_i64(shift_const);
4237 /* Logical (shifted register)
4238 * 31 30 29 28 24 23 22 21 20 16 15 10 9 5 4 0
4239 * +----+-----+-----------+-------+---+------+--------+------+------+
4240 * | sf | opc | 0 1 0 1 0 | shift | N | Rm | imm6 | Rn | Rd |
4241 * +----+-----+-----------+-------+---+------+--------+------+------+
4243 static void disas_logic_reg(DisasContext *s, uint32_t insn)
4245 TCGv_i64 tcg_rd, tcg_rn, tcg_rm;
4246 unsigned int sf, opc, shift_type, invert, rm, shift_amount, rn, rd;
4248 sf = extract32(insn, 31, 1);
4249 opc = extract32(insn, 29, 2);
4250 shift_type = extract32(insn, 22, 2);
4251 invert = extract32(insn, 21, 1);
4252 rm = extract32(insn, 16, 5);
4253 shift_amount = extract32(insn, 10, 6);
4254 rn = extract32(insn, 5, 5);
4255 rd = extract32(insn, 0, 5);
4257 if (!sf && (shift_amount & (1 << 5))) {
4258 unallocated_encoding(s);
4259 return;
4262 tcg_rd = cpu_reg(s, rd);
4264 if (opc == 1 && shift_amount == 0 && shift_type == 0 && rn == 31) {
4265 /* Unshifted ORR and ORN with WZR/XZR is the standard encoding for
4266 * register-register MOV and MVN, so it is worth special casing.
4268 tcg_rm = cpu_reg(s, rm);
4269 if (invert) {
4270 tcg_gen_not_i64(tcg_rd, tcg_rm);
4271 if (!sf) {
4272 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4274 } else {
4275 if (sf) {
4276 tcg_gen_mov_i64(tcg_rd, tcg_rm);
4277 } else {
4278 tcg_gen_ext32u_i64(tcg_rd, tcg_rm);
4281 return;
4284 tcg_rm = read_cpu_reg(s, rm, sf);
4286 if (shift_amount) {
4287 shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, shift_amount);
4290 tcg_rn = cpu_reg(s, rn);
4292 switch (opc | (invert << 2)) {
4293 case 0: /* AND */
4294 case 3: /* ANDS */
4295 tcg_gen_and_i64(tcg_rd, tcg_rn, tcg_rm);
4296 break;
4297 case 1: /* ORR */
4298 tcg_gen_or_i64(tcg_rd, tcg_rn, tcg_rm);
4299 break;
4300 case 2: /* EOR */
4301 tcg_gen_xor_i64(tcg_rd, tcg_rn, tcg_rm);
4302 break;
4303 case 4: /* BIC */
4304 case 7: /* BICS */
4305 tcg_gen_andc_i64(tcg_rd, tcg_rn, tcg_rm);
4306 break;
4307 case 5: /* ORN */
4308 tcg_gen_orc_i64(tcg_rd, tcg_rn, tcg_rm);
4309 break;
4310 case 6: /* EON */
4311 tcg_gen_eqv_i64(tcg_rd, tcg_rn, tcg_rm);
4312 break;
4313 default:
4314 assert(FALSE);
4315 break;
4318 if (!sf) {
4319 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4322 if (opc == 3) {
4323 gen_logic_CC(sf, tcg_rd);
4328 * Add/subtract (extended register)
4330 * 31|30|29|28 24|23 22|21|20 16|15 13|12 10|9 5|4 0|
4331 * +--+--+--+-----------+-----+--+-------+------+------+----+----+
4332 * |sf|op| S| 0 1 0 1 1 | opt | 1| Rm |option| imm3 | Rn | Rd |
4333 * +--+--+--+-----------+-----+--+-------+------+------+----+----+
4335 * sf: 0 -> 32bit, 1 -> 64bit
4336 * op: 0 -> add , 1 -> sub
4337 * S: 1 -> set flags
4338 * opt: 00
4339 * option: extension type (see DecodeRegExtend)
4340 * imm3: optional shift to Rm
4342 * Rd = Rn + LSL(extend(Rm), amount)
4344 static void disas_add_sub_ext_reg(DisasContext *s, uint32_t insn)
4346 int rd = extract32(insn, 0, 5);
4347 int rn = extract32(insn, 5, 5);
4348 int imm3 = extract32(insn, 10, 3);
4349 int option = extract32(insn, 13, 3);
4350 int rm = extract32(insn, 16, 5);
4351 int opt = extract32(insn, 22, 2);
4352 bool setflags = extract32(insn, 29, 1);
4353 bool sub_op = extract32(insn, 30, 1);
4354 bool sf = extract32(insn, 31, 1);
4356 TCGv_i64 tcg_rm, tcg_rn; /* temps */
4357 TCGv_i64 tcg_rd;
4358 TCGv_i64 tcg_result;
4360 if (imm3 > 4 || opt != 0) {
4361 unallocated_encoding(s);
4362 return;
4365 /* non-flag setting ops may use SP */
4366 if (!setflags) {
4367 tcg_rd = cpu_reg_sp(s, rd);
4368 } else {
4369 tcg_rd = cpu_reg(s, rd);
4371 tcg_rn = read_cpu_reg_sp(s, rn, sf);
4373 tcg_rm = read_cpu_reg(s, rm, sf);
4374 ext_and_shift_reg(tcg_rm, tcg_rm, option, imm3);
4376 tcg_result = tcg_temp_new_i64();
4378 if (!setflags) {
4379 if (sub_op) {
4380 tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
4381 } else {
4382 tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
4384 } else {
4385 if (sub_op) {
4386 gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
4387 } else {
4388 gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
4392 if (sf) {
4393 tcg_gen_mov_i64(tcg_rd, tcg_result);
4394 } else {
4395 tcg_gen_ext32u_i64(tcg_rd, tcg_result);
4398 tcg_temp_free_i64(tcg_result);
4402 * Add/subtract (shifted register)
4404 * 31 30 29 28 24 23 22 21 20 16 15 10 9 5 4 0
4405 * +--+--+--+-----------+-----+--+-------+---------+------+------+
4406 * |sf|op| S| 0 1 0 1 1 |shift| 0| Rm | imm6 | Rn | Rd |
4407 * +--+--+--+-----------+-----+--+-------+---------+------+------+
4409 * sf: 0 -> 32bit, 1 -> 64bit
4410 * op: 0 -> add , 1 -> sub
4411 * S: 1 -> set flags
4412 * shift: 00 -> LSL, 01 -> LSR, 10 -> ASR, 11 -> RESERVED
4413 * imm6: Shift amount to apply to Rm before the add/sub
4415 static void disas_add_sub_reg(DisasContext *s, uint32_t insn)
4417 int rd = extract32(insn, 0, 5);
4418 int rn = extract32(insn, 5, 5);
4419 int imm6 = extract32(insn, 10, 6);
4420 int rm = extract32(insn, 16, 5);
4421 int shift_type = extract32(insn, 22, 2);
4422 bool setflags = extract32(insn, 29, 1);
4423 bool sub_op = extract32(insn, 30, 1);
4424 bool sf = extract32(insn, 31, 1);
4426 TCGv_i64 tcg_rd = cpu_reg(s, rd);
4427 TCGv_i64 tcg_rn, tcg_rm;
4428 TCGv_i64 tcg_result;
4430 if ((shift_type == 3) || (!sf && (imm6 > 31))) {
4431 unallocated_encoding(s);
4432 return;
4435 tcg_rn = read_cpu_reg(s, rn, sf);
4436 tcg_rm = read_cpu_reg(s, rm, sf);
4438 shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, imm6);
4440 tcg_result = tcg_temp_new_i64();
4442 if (!setflags) {
4443 if (sub_op) {
4444 tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
4445 } else {
4446 tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
4448 } else {
4449 if (sub_op) {
4450 gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
4451 } else {
4452 gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
4456 if (sf) {
4457 tcg_gen_mov_i64(tcg_rd, tcg_result);
4458 } else {
4459 tcg_gen_ext32u_i64(tcg_rd, tcg_result);
4462 tcg_temp_free_i64(tcg_result);
4465 /* Data-processing (3 source)
4467 * 31 30 29 28 24 23 21 20 16 15 14 10 9 5 4 0
4468 * +--+------+-----------+------+------+----+------+------+------+
4469 * |sf| op54 | 1 1 0 1 1 | op31 | Rm | o0 | Ra | Rn | Rd |
4470 * +--+------+-----------+------+------+----+------+------+------+
4472 static void disas_data_proc_3src(DisasContext *s, uint32_t insn)
4474 int rd = extract32(insn, 0, 5);
4475 int rn = extract32(insn, 5, 5);
4476 int ra = extract32(insn, 10, 5);
4477 int rm = extract32(insn, 16, 5);
4478 int op_id = (extract32(insn, 29, 3) << 4) |
4479 (extract32(insn, 21, 3) << 1) |
4480 extract32(insn, 15, 1);
4481 bool sf = extract32(insn, 31, 1);
4482 bool is_sub = extract32(op_id, 0, 1);
4483 bool is_high = extract32(op_id, 2, 1);
4484 bool is_signed = false;
4485 TCGv_i64 tcg_op1;
4486 TCGv_i64 tcg_op2;
4487 TCGv_i64 tcg_tmp;
4489 /* Note that op_id is sf:op54:op31:o0 so it includes the 32/64 size flag */
4490 switch (op_id) {
4491 case 0x42: /* SMADDL */
4492 case 0x43: /* SMSUBL */
4493 case 0x44: /* SMULH */
4494 is_signed = true;
4495 break;
4496 case 0x0: /* MADD (32bit) */
4497 case 0x1: /* MSUB (32bit) */
4498 case 0x40: /* MADD (64bit) */
4499 case 0x41: /* MSUB (64bit) */
4500 case 0x4a: /* UMADDL */
4501 case 0x4b: /* UMSUBL */
4502 case 0x4c: /* UMULH */
4503 break;
4504 default:
4505 unallocated_encoding(s);
4506 return;
4509 if (is_high) {
4510 TCGv_i64 low_bits = tcg_temp_new_i64(); /* low bits discarded */
4511 TCGv_i64 tcg_rd = cpu_reg(s, rd);
4512 TCGv_i64 tcg_rn = cpu_reg(s, rn);
4513 TCGv_i64 tcg_rm = cpu_reg(s, rm);
4515 if (is_signed) {
4516 tcg_gen_muls2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
4517 } else {
4518 tcg_gen_mulu2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
4521 tcg_temp_free_i64(low_bits);
4522 return;
4525 tcg_op1 = tcg_temp_new_i64();
4526 tcg_op2 = tcg_temp_new_i64();
4527 tcg_tmp = tcg_temp_new_i64();
4529 if (op_id < 0x42) {
4530 tcg_gen_mov_i64(tcg_op1, cpu_reg(s, rn));
4531 tcg_gen_mov_i64(tcg_op2, cpu_reg(s, rm));
4532 } else {
4533 if (is_signed) {
4534 tcg_gen_ext32s_i64(tcg_op1, cpu_reg(s, rn));
4535 tcg_gen_ext32s_i64(tcg_op2, cpu_reg(s, rm));
4536 } else {
4537 tcg_gen_ext32u_i64(tcg_op1, cpu_reg(s, rn));
4538 tcg_gen_ext32u_i64(tcg_op2, cpu_reg(s, rm));
4542 if (ra == 31 && !is_sub) {
4543 /* Special-case MADD with rA == XZR; it is the standard MUL alias */
4544 tcg_gen_mul_i64(cpu_reg(s, rd), tcg_op1, tcg_op2);
4545 } else {
4546 tcg_gen_mul_i64(tcg_tmp, tcg_op1, tcg_op2);
4547 if (is_sub) {
4548 tcg_gen_sub_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
4549 } else {
4550 tcg_gen_add_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
4554 if (!sf) {
4555 tcg_gen_ext32u_i64(cpu_reg(s, rd), cpu_reg(s, rd));
4558 tcg_temp_free_i64(tcg_op1);
4559 tcg_temp_free_i64(tcg_op2);
4560 tcg_temp_free_i64(tcg_tmp);
4563 /* Add/subtract (with carry)
4564 * 31 30 29 28 27 26 25 24 23 22 21 20 16 15 10 9 5 4 0
4565 * +--+--+--+------------------------+------+-------------+------+-----+
4566 * |sf|op| S| 1 1 0 1 0 0 0 0 | rm | 0 0 0 0 0 0 | Rn | Rd |
4567 * +--+--+--+------------------------+------+-------------+------+-----+
4570 static void disas_adc_sbc(DisasContext *s, uint32_t insn)
4572 unsigned int sf, op, setflags, rm, rn, rd;
4573 TCGv_i64 tcg_y, tcg_rn, tcg_rd;
4575 sf = extract32(insn, 31, 1);
4576 op = extract32(insn, 30, 1);
4577 setflags = extract32(insn, 29, 1);
4578 rm = extract32(insn, 16, 5);
4579 rn = extract32(insn, 5, 5);
4580 rd = extract32(insn, 0, 5);
4582 tcg_rd = cpu_reg(s, rd);
4583 tcg_rn = cpu_reg(s, rn);
4585 if (op) {
4586 tcg_y = new_tmp_a64(s);
4587 tcg_gen_not_i64(tcg_y, cpu_reg(s, rm));
4588 } else {
4589 tcg_y = cpu_reg(s, rm);
4592 if (setflags) {
4593 gen_adc_CC(sf, tcg_rd, tcg_rn, tcg_y);
4594 } else {
4595 gen_adc(sf, tcg_rd, tcg_rn, tcg_y);
4600 * Rotate right into flags
4601 * 31 30 29 21 15 10 5 4 0
4602 * +--+--+--+-----------------+--------+-----------+------+--+------+
4603 * |sf|op| S| 1 1 0 1 0 0 0 0 | imm6 | 0 0 0 0 1 | Rn |o2| mask |
4604 * +--+--+--+-----------------+--------+-----------+------+--+------+
4606 static void disas_rotate_right_into_flags(DisasContext *s, uint32_t insn)
4608 int mask = extract32(insn, 0, 4);
4609 int o2 = extract32(insn, 4, 1);
4610 int rn = extract32(insn, 5, 5);
4611 int imm6 = extract32(insn, 15, 6);
4612 int sf_op_s = extract32(insn, 29, 3);
4613 TCGv_i64 tcg_rn;
4614 TCGv_i32 nzcv;
4616 if (sf_op_s != 5 || o2 != 0 || !dc_isar_feature(aa64_condm_4, s)) {
4617 unallocated_encoding(s);
4618 return;
4621 tcg_rn = read_cpu_reg(s, rn, 1);
4622 tcg_gen_rotri_i64(tcg_rn, tcg_rn, imm6);
4624 nzcv = tcg_temp_new_i32();
4625 tcg_gen_extrl_i64_i32(nzcv, tcg_rn);
4627 if (mask & 8) { /* N */
4628 tcg_gen_shli_i32(cpu_NF, nzcv, 31 - 3);
4630 if (mask & 4) { /* Z */
4631 tcg_gen_not_i32(cpu_ZF, nzcv);
4632 tcg_gen_andi_i32(cpu_ZF, cpu_ZF, 4);
4634 if (mask & 2) { /* C */
4635 tcg_gen_extract_i32(cpu_CF, nzcv, 1, 1);
4637 if (mask & 1) { /* V */
4638 tcg_gen_shli_i32(cpu_VF, nzcv, 31 - 0);
4641 tcg_temp_free_i32(nzcv);
4645 * Evaluate into flags
4646 * 31 30 29 21 15 14 10 5 4 0
4647 * +--+--+--+-----------------+---------+----+---------+------+--+------+
4648 * |sf|op| S| 1 1 0 1 0 0 0 0 | opcode2 | sz | 0 0 1 0 | Rn |o3| mask |
4649 * +--+--+--+-----------------+---------+----+---------+------+--+------+
4651 static void disas_evaluate_into_flags(DisasContext *s, uint32_t insn)
4653 int o3_mask = extract32(insn, 0, 5);
4654 int rn = extract32(insn, 5, 5);
4655 int o2 = extract32(insn, 15, 6);
4656 int sz = extract32(insn, 14, 1);
4657 int sf_op_s = extract32(insn, 29, 3);
4658 TCGv_i32 tmp;
4659 int shift;
4661 if (sf_op_s != 1 || o2 != 0 || o3_mask != 0xd ||
4662 !dc_isar_feature(aa64_condm_4, s)) {
4663 unallocated_encoding(s);
4664 return;
4666 shift = sz ? 16 : 24; /* SETF16 or SETF8 */
4668 tmp = tcg_temp_new_i32();
4669 tcg_gen_extrl_i64_i32(tmp, cpu_reg(s, rn));
4670 tcg_gen_shli_i32(cpu_NF, tmp, shift);
4671 tcg_gen_shli_i32(cpu_VF, tmp, shift - 1);
4672 tcg_gen_mov_i32(cpu_ZF, cpu_NF);
4673 tcg_gen_xor_i32(cpu_VF, cpu_VF, cpu_NF);
4674 tcg_temp_free_i32(tmp);
4677 /* Conditional compare (immediate / register)
4678 * 31 30 29 28 27 26 25 24 23 22 21 20 16 15 12 11 10 9 5 4 3 0
4679 * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
4680 * |sf|op| S| 1 1 0 1 0 0 1 0 |imm5/rm | cond |i/r |o2| Rn |o3|nzcv |
4681 * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
4682 * [1] y [0] [0]
4684 static void disas_cc(DisasContext *s, uint32_t insn)
4686 unsigned int sf, op, y, cond, rn, nzcv, is_imm;
4687 TCGv_i32 tcg_t0, tcg_t1, tcg_t2;
4688 TCGv_i64 tcg_tmp, tcg_y, tcg_rn;
4689 DisasCompare c;
4691 if (!extract32(insn, 29, 1)) {
4692 unallocated_encoding(s);
4693 return;
4695 if (insn & (1 << 10 | 1 << 4)) {
4696 unallocated_encoding(s);
4697 return;
4699 sf = extract32(insn, 31, 1);
4700 op = extract32(insn, 30, 1);
4701 is_imm = extract32(insn, 11, 1);
4702 y = extract32(insn, 16, 5); /* y = rm (reg) or imm5 (imm) */
4703 cond = extract32(insn, 12, 4);
4704 rn = extract32(insn, 5, 5);
4705 nzcv = extract32(insn, 0, 4);
4707 /* Set T0 = !COND. */
4708 tcg_t0 = tcg_temp_new_i32();
4709 arm_test_cc(&c, cond);
4710 tcg_gen_setcondi_i32(tcg_invert_cond(c.cond), tcg_t0, c.value, 0);
4711 arm_free_cc(&c);
4713 /* Load the arguments for the new comparison. */
4714 if (is_imm) {
4715 tcg_y = new_tmp_a64(s);
4716 tcg_gen_movi_i64(tcg_y, y);
4717 } else {
4718 tcg_y = cpu_reg(s, y);
4720 tcg_rn = cpu_reg(s, rn);
4722 /* Set the flags for the new comparison. */
4723 tcg_tmp = tcg_temp_new_i64();
4724 if (op) {
4725 gen_sub_CC(sf, tcg_tmp, tcg_rn, tcg_y);
4726 } else {
4727 gen_add_CC(sf, tcg_tmp, tcg_rn, tcg_y);
4729 tcg_temp_free_i64(tcg_tmp);
4731 /* If COND was false, force the flags to #nzcv. Compute two masks
4732 * to help with this: T1 = (COND ? 0 : -1), T2 = (COND ? -1 : 0).
4733 * For tcg hosts that support ANDC, we can make do with just T1.
4734 * In either case, allow the tcg optimizer to delete any unused mask.
4736 tcg_t1 = tcg_temp_new_i32();
4737 tcg_t2 = tcg_temp_new_i32();
4738 tcg_gen_neg_i32(tcg_t1, tcg_t0);
4739 tcg_gen_subi_i32(tcg_t2, tcg_t0, 1);
4741 if (nzcv & 8) { /* N */
4742 tcg_gen_or_i32(cpu_NF, cpu_NF, tcg_t1);
4743 } else {
4744 if (TCG_TARGET_HAS_andc_i32) {
4745 tcg_gen_andc_i32(cpu_NF, cpu_NF, tcg_t1);
4746 } else {
4747 tcg_gen_and_i32(cpu_NF, cpu_NF, tcg_t2);
4750 if (nzcv & 4) { /* Z */
4751 if (TCG_TARGET_HAS_andc_i32) {
4752 tcg_gen_andc_i32(cpu_ZF, cpu_ZF, tcg_t1);
4753 } else {
4754 tcg_gen_and_i32(cpu_ZF, cpu_ZF, tcg_t2);
4756 } else {
4757 tcg_gen_or_i32(cpu_ZF, cpu_ZF, tcg_t0);
4759 if (nzcv & 2) { /* C */
4760 tcg_gen_or_i32(cpu_CF, cpu_CF, tcg_t0);
4761 } else {
4762 if (TCG_TARGET_HAS_andc_i32) {
4763 tcg_gen_andc_i32(cpu_CF, cpu_CF, tcg_t1);
4764 } else {
4765 tcg_gen_and_i32(cpu_CF, cpu_CF, tcg_t2);
4768 if (nzcv & 1) { /* V */
4769 tcg_gen_or_i32(cpu_VF, cpu_VF, tcg_t1);
4770 } else {
4771 if (TCG_TARGET_HAS_andc_i32) {
4772 tcg_gen_andc_i32(cpu_VF, cpu_VF, tcg_t1);
4773 } else {
4774 tcg_gen_and_i32(cpu_VF, cpu_VF, tcg_t2);
4777 tcg_temp_free_i32(tcg_t0);
4778 tcg_temp_free_i32(tcg_t1);
4779 tcg_temp_free_i32(tcg_t2);
4782 /* Conditional select
4783 * 31 30 29 28 21 20 16 15 12 11 10 9 5 4 0
4784 * +----+----+---+-----------------+------+------+-----+------+------+
4785 * | sf | op | S | 1 1 0 1 0 1 0 0 | Rm | cond | op2 | Rn | Rd |
4786 * +----+----+---+-----------------+------+------+-----+------+------+
4788 static void disas_cond_select(DisasContext *s, uint32_t insn)
4790 unsigned int sf, else_inv, rm, cond, else_inc, rn, rd;
4791 TCGv_i64 tcg_rd, zero;
4792 DisasCompare64 c;
4794 if (extract32(insn, 29, 1) || extract32(insn, 11, 1)) {
4795 /* S == 1 or op2<1> == 1 */
4796 unallocated_encoding(s);
4797 return;
4799 sf = extract32(insn, 31, 1);
4800 else_inv = extract32(insn, 30, 1);
4801 rm = extract32(insn, 16, 5);
4802 cond = extract32(insn, 12, 4);
4803 else_inc = extract32(insn, 10, 1);
4804 rn = extract32(insn, 5, 5);
4805 rd = extract32(insn, 0, 5);
4807 tcg_rd = cpu_reg(s, rd);
4809 a64_test_cc(&c, cond);
4810 zero = tcg_const_i64(0);
4812 if (rn == 31 && rm == 31 && (else_inc ^ else_inv)) {
4813 /* CSET & CSETM. */
4814 tcg_gen_setcond_i64(tcg_invert_cond(c.cond), tcg_rd, c.value, zero);
4815 if (else_inv) {
4816 tcg_gen_neg_i64(tcg_rd, tcg_rd);
4818 } else {
4819 TCGv_i64 t_true = cpu_reg(s, rn);
4820 TCGv_i64 t_false = read_cpu_reg(s, rm, 1);
4821 if (else_inv && else_inc) {
4822 tcg_gen_neg_i64(t_false, t_false);
4823 } else if (else_inv) {
4824 tcg_gen_not_i64(t_false, t_false);
4825 } else if (else_inc) {
4826 tcg_gen_addi_i64(t_false, t_false, 1);
4828 tcg_gen_movcond_i64(c.cond, tcg_rd, c.value, zero, t_true, t_false);
4831 tcg_temp_free_i64(zero);
4832 a64_free_cc(&c);
4834 if (!sf) {
4835 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4839 static void handle_clz(DisasContext *s, unsigned int sf,
4840 unsigned int rn, unsigned int rd)
4842 TCGv_i64 tcg_rd, tcg_rn;
4843 tcg_rd = cpu_reg(s, rd);
4844 tcg_rn = cpu_reg(s, rn);
4846 if (sf) {
4847 tcg_gen_clzi_i64(tcg_rd, tcg_rn, 64);
4848 } else {
4849 TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
4850 tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
4851 tcg_gen_clzi_i32(tcg_tmp32, tcg_tmp32, 32);
4852 tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
4853 tcg_temp_free_i32(tcg_tmp32);
4857 static void handle_cls(DisasContext *s, unsigned int sf,
4858 unsigned int rn, unsigned int rd)
4860 TCGv_i64 tcg_rd, tcg_rn;
4861 tcg_rd = cpu_reg(s, rd);
4862 tcg_rn = cpu_reg(s, rn);
4864 if (sf) {
4865 tcg_gen_clrsb_i64(tcg_rd, tcg_rn);
4866 } else {
4867 TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
4868 tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
4869 tcg_gen_clrsb_i32(tcg_tmp32, tcg_tmp32);
4870 tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
4871 tcg_temp_free_i32(tcg_tmp32);
4875 static void handle_rbit(DisasContext *s, unsigned int sf,
4876 unsigned int rn, unsigned int rd)
4878 TCGv_i64 tcg_rd, tcg_rn;
4879 tcg_rd = cpu_reg(s, rd);
4880 tcg_rn = cpu_reg(s, rn);
4882 if (sf) {
4883 gen_helper_rbit64(tcg_rd, tcg_rn);
4884 } else {
4885 TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
4886 tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
4887 gen_helper_rbit(tcg_tmp32, tcg_tmp32);
4888 tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
4889 tcg_temp_free_i32(tcg_tmp32);
4893 /* REV with sf==1, opcode==3 ("REV64") */
4894 static void handle_rev64(DisasContext *s, unsigned int sf,
4895 unsigned int rn, unsigned int rd)
4897 if (!sf) {
4898 unallocated_encoding(s);
4899 return;
4901 tcg_gen_bswap64_i64(cpu_reg(s, rd), cpu_reg(s, rn));
4904 /* REV with sf==0, opcode==2
4905 * REV32 (sf==1, opcode==2)
4907 static void handle_rev32(DisasContext *s, unsigned int sf,
4908 unsigned int rn, unsigned int rd)
4910 TCGv_i64 tcg_rd = cpu_reg(s, rd);
4912 if (sf) {
4913 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
4914 TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
4916 /* bswap32_i64 requires zero high word */
4917 tcg_gen_ext32u_i64(tcg_tmp, tcg_rn);
4918 tcg_gen_bswap32_i64(tcg_rd, tcg_tmp);
4919 tcg_gen_shri_i64(tcg_tmp, tcg_rn, 32);
4920 tcg_gen_bswap32_i64(tcg_tmp, tcg_tmp);
4921 tcg_gen_concat32_i64(tcg_rd, tcg_rd, tcg_tmp);
4923 tcg_temp_free_i64(tcg_tmp);
4924 } else {
4925 tcg_gen_ext32u_i64(tcg_rd, cpu_reg(s, rn));
4926 tcg_gen_bswap32_i64(tcg_rd, tcg_rd);
4930 /* REV16 (opcode==1) */
4931 static void handle_rev16(DisasContext *s, unsigned int sf,
4932 unsigned int rn, unsigned int rd)
4934 TCGv_i64 tcg_rd = cpu_reg(s, rd);
4935 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
4936 TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
4937 TCGv_i64 mask = tcg_const_i64(sf ? 0x00ff00ff00ff00ffull : 0x00ff00ff);
4939 tcg_gen_shri_i64(tcg_tmp, tcg_rn, 8);
4940 tcg_gen_and_i64(tcg_rd, tcg_rn, mask);
4941 tcg_gen_and_i64(tcg_tmp, tcg_tmp, mask);
4942 tcg_gen_shli_i64(tcg_rd, tcg_rd, 8);
4943 tcg_gen_or_i64(tcg_rd, tcg_rd, tcg_tmp);
4945 tcg_temp_free_i64(mask);
4946 tcg_temp_free_i64(tcg_tmp);
4949 /* Data-processing (1 source)
4950 * 31 30 29 28 21 20 16 15 10 9 5 4 0
4951 * +----+---+---+-----------------+---------+--------+------+------+
4952 * | sf | 1 | S | 1 1 0 1 0 1 1 0 | opcode2 | opcode | Rn | Rd |
4953 * +----+---+---+-----------------+---------+--------+------+------+
4955 static void disas_data_proc_1src(DisasContext *s, uint32_t insn)
4957 unsigned int sf, opcode, opcode2, rn, rd;
4958 TCGv_i64 tcg_rd;
4960 if (extract32(insn, 29, 1)) {
4961 unallocated_encoding(s);
4962 return;
4965 sf = extract32(insn, 31, 1);
4966 opcode = extract32(insn, 10, 6);
4967 opcode2 = extract32(insn, 16, 5);
4968 rn = extract32(insn, 5, 5);
4969 rd = extract32(insn, 0, 5);
4971 #define MAP(SF, O2, O1) ((SF) | (O1 << 1) | (O2 << 7))
4973 switch (MAP(sf, opcode2, opcode)) {
4974 case MAP(0, 0x00, 0x00): /* RBIT */
4975 case MAP(1, 0x00, 0x00):
4976 handle_rbit(s, sf, rn, rd);
4977 break;
4978 case MAP(0, 0x00, 0x01): /* REV16 */
4979 case MAP(1, 0x00, 0x01):
4980 handle_rev16(s, sf, rn, rd);
4981 break;
4982 case MAP(0, 0x00, 0x02): /* REV/REV32 */
4983 case MAP(1, 0x00, 0x02):
4984 handle_rev32(s, sf, rn, rd);
4985 break;
4986 case MAP(1, 0x00, 0x03): /* REV64 */
4987 handle_rev64(s, sf, rn, rd);
4988 break;
4989 case MAP(0, 0x00, 0x04): /* CLZ */
4990 case MAP(1, 0x00, 0x04):
4991 handle_clz(s, sf, rn, rd);
4992 break;
4993 case MAP(0, 0x00, 0x05): /* CLS */
4994 case MAP(1, 0x00, 0x05):
4995 handle_cls(s, sf, rn, rd);
4996 break;
4997 case MAP(1, 0x01, 0x00): /* PACIA */
4998 if (s->pauth_active) {
4999 tcg_rd = cpu_reg(s, rd);
5000 gen_helper_pacia(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5001 } else if (!dc_isar_feature(aa64_pauth, s)) {
5002 goto do_unallocated;
5004 break;
5005 case MAP(1, 0x01, 0x01): /* PACIB */
5006 if (s->pauth_active) {
5007 tcg_rd = cpu_reg(s, rd);
5008 gen_helper_pacib(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5009 } else if (!dc_isar_feature(aa64_pauth, s)) {
5010 goto do_unallocated;
5012 break;
5013 case MAP(1, 0x01, 0x02): /* PACDA */
5014 if (s->pauth_active) {
5015 tcg_rd = cpu_reg(s, rd);
5016 gen_helper_pacda(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5017 } else if (!dc_isar_feature(aa64_pauth, s)) {
5018 goto do_unallocated;
5020 break;
5021 case MAP(1, 0x01, 0x03): /* PACDB */
5022 if (s->pauth_active) {
5023 tcg_rd = cpu_reg(s, rd);
5024 gen_helper_pacdb(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5025 } else if (!dc_isar_feature(aa64_pauth, s)) {
5026 goto do_unallocated;
5028 break;
5029 case MAP(1, 0x01, 0x04): /* AUTIA */
5030 if (s->pauth_active) {
5031 tcg_rd = cpu_reg(s, rd);
5032 gen_helper_autia(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5033 } else if (!dc_isar_feature(aa64_pauth, s)) {
5034 goto do_unallocated;
5036 break;
5037 case MAP(1, 0x01, 0x05): /* AUTIB */
5038 if (s->pauth_active) {
5039 tcg_rd = cpu_reg(s, rd);
5040 gen_helper_autib(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5041 } else if (!dc_isar_feature(aa64_pauth, s)) {
5042 goto do_unallocated;
5044 break;
5045 case MAP(1, 0x01, 0x06): /* AUTDA */
5046 if (s->pauth_active) {
5047 tcg_rd = cpu_reg(s, rd);
5048 gen_helper_autda(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5049 } else if (!dc_isar_feature(aa64_pauth, s)) {
5050 goto do_unallocated;
5052 break;
5053 case MAP(1, 0x01, 0x07): /* AUTDB */
5054 if (s->pauth_active) {
5055 tcg_rd = cpu_reg(s, rd);
5056 gen_helper_autdb(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5057 } else if (!dc_isar_feature(aa64_pauth, s)) {
5058 goto do_unallocated;
5060 break;
5061 case MAP(1, 0x01, 0x08): /* PACIZA */
5062 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5063 goto do_unallocated;
5064 } else if (s->pauth_active) {
5065 tcg_rd = cpu_reg(s, rd);
5066 gen_helper_pacia(tcg_rd, cpu_env, tcg_rd, new_tmp_a64_zero(s));
5068 break;
5069 case MAP(1, 0x01, 0x09): /* PACIZB */
5070 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5071 goto do_unallocated;
5072 } else if (s->pauth_active) {
5073 tcg_rd = cpu_reg(s, rd);
5074 gen_helper_pacib(tcg_rd, cpu_env, tcg_rd, new_tmp_a64_zero(s));
5076 break;
5077 case MAP(1, 0x01, 0x0a): /* PACDZA */
5078 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5079 goto do_unallocated;
5080 } else if (s->pauth_active) {
5081 tcg_rd = cpu_reg(s, rd);
5082 gen_helper_pacda(tcg_rd, cpu_env, tcg_rd, new_tmp_a64_zero(s));
5084 break;
5085 case MAP(1, 0x01, 0x0b): /* PACDZB */
5086 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5087 goto do_unallocated;
5088 } else if (s->pauth_active) {
5089 tcg_rd = cpu_reg(s, rd);
5090 gen_helper_pacdb(tcg_rd, cpu_env, tcg_rd, new_tmp_a64_zero(s));
5092 break;
5093 case MAP(1, 0x01, 0x0c): /* AUTIZA */
5094 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5095 goto do_unallocated;
5096 } else if (s->pauth_active) {
5097 tcg_rd = cpu_reg(s, rd);
5098 gen_helper_autia(tcg_rd, cpu_env, tcg_rd, new_tmp_a64_zero(s));
5100 break;
5101 case MAP(1, 0x01, 0x0d): /* AUTIZB */
5102 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5103 goto do_unallocated;
5104 } else if (s->pauth_active) {
5105 tcg_rd = cpu_reg(s, rd);
5106 gen_helper_autib(tcg_rd, cpu_env, tcg_rd, new_tmp_a64_zero(s));
5108 break;
5109 case MAP(1, 0x01, 0x0e): /* AUTDZA */
5110 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5111 goto do_unallocated;
5112 } else if (s->pauth_active) {
5113 tcg_rd = cpu_reg(s, rd);
5114 gen_helper_autda(tcg_rd, cpu_env, tcg_rd, new_tmp_a64_zero(s));
5116 break;
5117 case MAP(1, 0x01, 0x0f): /* AUTDZB */
5118 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5119 goto do_unallocated;
5120 } else if (s->pauth_active) {
5121 tcg_rd = cpu_reg(s, rd);
5122 gen_helper_autdb(tcg_rd, cpu_env, tcg_rd, new_tmp_a64_zero(s));
5124 break;
5125 case MAP(1, 0x01, 0x10): /* XPACI */
5126 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5127 goto do_unallocated;
5128 } else if (s->pauth_active) {
5129 tcg_rd = cpu_reg(s, rd);
5130 gen_helper_xpaci(tcg_rd, cpu_env, tcg_rd);
5132 break;
5133 case MAP(1, 0x01, 0x11): /* XPACD */
5134 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5135 goto do_unallocated;
5136 } else if (s->pauth_active) {
5137 tcg_rd = cpu_reg(s, rd);
5138 gen_helper_xpacd(tcg_rd, cpu_env, tcg_rd);
5140 break;
5141 default:
5142 do_unallocated:
5143 unallocated_encoding(s);
5144 break;
5147 #undef MAP
5150 static void handle_div(DisasContext *s, bool is_signed, unsigned int sf,
5151 unsigned int rm, unsigned int rn, unsigned int rd)
5153 TCGv_i64 tcg_n, tcg_m, tcg_rd;
5154 tcg_rd = cpu_reg(s, rd);
5156 if (!sf && is_signed) {
5157 tcg_n = new_tmp_a64(s);
5158 tcg_m = new_tmp_a64(s);
5159 tcg_gen_ext32s_i64(tcg_n, cpu_reg(s, rn));
5160 tcg_gen_ext32s_i64(tcg_m, cpu_reg(s, rm));
5161 } else {
5162 tcg_n = read_cpu_reg(s, rn, sf);
5163 tcg_m = read_cpu_reg(s, rm, sf);
5166 if (is_signed) {
5167 gen_helper_sdiv64(tcg_rd, tcg_n, tcg_m);
5168 } else {
5169 gen_helper_udiv64(tcg_rd, tcg_n, tcg_m);
5172 if (!sf) { /* zero extend final result */
5173 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
5177 /* LSLV, LSRV, ASRV, RORV */
5178 static void handle_shift_reg(DisasContext *s,
5179 enum a64_shift_type shift_type, unsigned int sf,
5180 unsigned int rm, unsigned int rn, unsigned int rd)
5182 TCGv_i64 tcg_shift = tcg_temp_new_i64();
5183 TCGv_i64 tcg_rd = cpu_reg(s, rd);
5184 TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
5186 tcg_gen_andi_i64(tcg_shift, cpu_reg(s, rm), sf ? 63 : 31);
5187 shift_reg(tcg_rd, tcg_rn, sf, shift_type, tcg_shift);
5188 tcg_temp_free_i64(tcg_shift);
5191 /* CRC32[BHWX], CRC32C[BHWX] */
5192 static void handle_crc32(DisasContext *s,
5193 unsigned int sf, unsigned int sz, bool crc32c,
5194 unsigned int rm, unsigned int rn, unsigned int rd)
5196 TCGv_i64 tcg_acc, tcg_val;
5197 TCGv_i32 tcg_bytes;
5199 if (!dc_isar_feature(aa64_crc32, s)
5200 || (sf == 1 && sz != 3)
5201 || (sf == 0 && sz == 3)) {
5202 unallocated_encoding(s);
5203 return;
5206 if (sz == 3) {
5207 tcg_val = cpu_reg(s, rm);
5208 } else {
5209 uint64_t mask;
5210 switch (sz) {
5211 case 0:
5212 mask = 0xFF;
5213 break;
5214 case 1:
5215 mask = 0xFFFF;
5216 break;
5217 case 2:
5218 mask = 0xFFFFFFFF;
5219 break;
5220 default:
5221 g_assert_not_reached();
5223 tcg_val = new_tmp_a64(s);
5224 tcg_gen_andi_i64(tcg_val, cpu_reg(s, rm), mask);
5227 tcg_acc = cpu_reg(s, rn);
5228 tcg_bytes = tcg_const_i32(1 << sz);
5230 if (crc32c) {
5231 gen_helper_crc32c_64(cpu_reg(s, rd), tcg_acc, tcg_val, tcg_bytes);
5232 } else {
5233 gen_helper_crc32_64(cpu_reg(s, rd), tcg_acc, tcg_val, tcg_bytes);
5236 tcg_temp_free_i32(tcg_bytes);
5239 /* Data-processing (2 source)
5240 * 31 30 29 28 21 20 16 15 10 9 5 4 0
5241 * +----+---+---+-----------------+------+--------+------+------+
5242 * | sf | 0 | S | 1 1 0 1 0 1 1 0 | Rm | opcode | Rn | Rd |
5243 * +----+---+---+-----------------+------+--------+------+------+
5245 static void disas_data_proc_2src(DisasContext *s, uint32_t insn)
5247 unsigned int sf, rm, opcode, rn, rd;
5248 sf = extract32(insn, 31, 1);
5249 rm = extract32(insn, 16, 5);
5250 opcode = extract32(insn, 10, 6);
5251 rn = extract32(insn, 5, 5);
5252 rd = extract32(insn, 0, 5);
5254 if (extract32(insn, 29, 1)) {
5255 unallocated_encoding(s);
5256 return;
5259 switch (opcode) {
5260 case 2: /* UDIV */
5261 handle_div(s, false, sf, rm, rn, rd);
5262 break;
5263 case 3: /* SDIV */
5264 handle_div(s, true, sf, rm, rn, rd);
5265 break;
5266 case 8: /* LSLV */
5267 handle_shift_reg(s, A64_SHIFT_TYPE_LSL, sf, rm, rn, rd);
5268 break;
5269 case 9: /* LSRV */
5270 handle_shift_reg(s, A64_SHIFT_TYPE_LSR, sf, rm, rn, rd);
5271 break;
5272 case 10: /* ASRV */
5273 handle_shift_reg(s, A64_SHIFT_TYPE_ASR, sf, rm, rn, rd);
5274 break;
5275 case 11: /* RORV */
5276 handle_shift_reg(s, A64_SHIFT_TYPE_ROR, sf, rm, rn, rd);
5277 break;
5278 case 12: /* PACGA */
5279 if (sf == 0 || !dc_isar_feature(aa64_pauth, s)) {
5280 goto do_unallocated;
5282 gen_helper_pacga(cpu_reg(s, rd), cpu_env,
5283 cpu_reg(s, rn), cpu_reg_sp(s, rm));
5284 break;
5285 case 16:
5286 case 17:
5287 case 18:
5288 case 19:
5289 case 20:
5290 case 21:
5291 case 22:
5292 case 23: /* CRC32 */
5294 int sz = extract32(opcode, 0, 2);
5295 bool crc32c = extract32(opcode, 2, 1);
5296 handle_crc32(s, sf, sz, crc32c, rm, rn, rd);
5297 break;
5299 default:
5300 do_unallocated:
5301 unallocated_encoding(s);
5302 break;
5307 * Data processing - register
5308 * 31 30 29 28 25 21 20 16 10 0
5309 * +--+---+--+---+-------+-----+-------+-------+---------+
5310 * | |op0| |op1| 1 0 1 | op2 | | op3 | |
5311 * +--+---+--+---+-------+-----+-------+-------+---------+
5313 static void disas_data_proc_reg(DisasContext *s, uint32_t insn)
5315 int op0 = extract32(insn, 30, 1);
5316 int op1 = extract32(insn, 28, 1);
5317 int op2 = extract32(insn, 21, 4);
5318 int op3 = extract32(insn, 10, 6);
5320 if (!op1) {
5321 if (op2 & 8) {
5322 if (op2 & 1) {
5323 /* Add/sub (extended register) */
5324 disas_add_sub_ext_reg(s, insn);
5325 } else {
5326 /* Add/sub (shifted register) */
5327 disas_add_sub_reg(s, insn);
5329 } else {
5330 /* Logical (shifted register) */
5331 disas_logic_reg(s, insn);
5333 return;
5336 switch (op2) {
5337 case 0x0:
5338 switch (op3) {
5339 case 0x00: /* Add/subtract (with carry) */
5340 disas_adc_sbc(s, insn);
5341 break;
5343 case 0x01: /* Rotate right into flags */
5344 case 0x21:
5345 disas_rotate_right_into_flags(s, insn);
5346 break;
5348 case 0x02: /* Evaluate into flags */
5349 case 0x12:
5350 case 0x22:
5351 case 0x32:
5352 disas_evaluate_into_flags(s, insn);
5353 break;
5355 default:
5356 goto do_unallocated;
5358 break;
5360 case 0x2: /* Conditional compare */
5361 disas_cc(s, insn); /* both imm and reg forms */
5362 break;
5364 case 0x4: /* Conditional select */
5365 disas_cond_select(s, insn);
5366 break;
5368 case 0x6: /* Data-processing */
5369 if (op0) { /* (1 source) */
5370 disas_data_proc_1src(s, insn);
5371 } else { /* (2 source) */
5372 disas_data_proc_2src(s, insn);
5374 break;
5375 case 0x8 ... 0xf: /* (3 source) */
5376 disas_data_proc_3src(s, insn);
5377 break;
5379 default:
5380 do_unallocated:
5381 unallocated_encoding(s);
5382 break;
5386 static void handle_fp_compare(DisasContext *s, int size,
5387 unsigned int rn, unsigned int rm,
5388 bool cmp_with_zero, bool signal_all_nans)
5390 TCGv_i64 tcg_flags = tcg_temp_new_i64();
5391 TCGv_ptr fpst = get_fpstatus_ptr(size == MO_16);
5393 if (size == MO_64) {
5394 TCGv_i64 tcg_vn, tcg_vm;
5396 tcg_vn = read_fp_dreg(s, rn);
5397 if (cmp_with_zero) {
5398 tcg_vm = tcg_const_i64(0);
5399 } else {
5400 tcg_vm = read_fp_dreg(s, rm);
5402 if (signal_all_nans) {
5403 gen_helper_vfp_cmped_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5404 } else {
5405 gen_helper_vfp_cmpd_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5407 tcg_temp_free_i64(tcg_vn);
5408 tcg_temp_free_i64(tcg_vm);
5409 } else {
5410 TCGv_i32 tcg_vn = tcg_temp_new_i32();
5411 TCGv_i32 tcg_vm = tcg_temp_new_i32();
5413 read_vec_element_i32(s, tcg_vn, rn, 0, size);
5414 if (cmp_with_zero) {
5415 tcg_gen_movi_i32(tcg_vm, 0);
5416 } else {
5417 read_vec_element_i32(s, tcg_vm, rm, 0, size);
5420 switch (size) {
5421 case MO_32:
5422 if (signal_all_nans) {
5423 gen_helper_vfp_cmpes_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5424 } else {
5425 gen_helper_vfp_cmps_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5427 break;
5428 case MO_16:
5429 if (signal_all_nans) {
5430 gen_helper_vfp_cmpeh_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5431 } else {
5432 gen_helper_vfp_cmph_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5434 break;
5435 default:
5436 g_assert_not_reached();
5439 tcg_temp_free_i32(tcg_vn);
5440 tcg_temp_free_i32(tcg_vm);
5443 tcg_temp_free_ptr(fpst);
5445 gen_set_nzcv(tcg_flags);
5447 tcg_temp_free_i64(tcg_flags);
5450 /* Floating point compare
5451 * 31 30 29 28 24 23 22 21 20 16 15 14 13 10 9 5 4 0
5452 * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
5453 * | M | 0 | S | 1 1 1 1 0 | type | 1 | Rm | op | 1 0 0 0 | Rn | op2 |
5454 * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
5456 static void disas_fp_compare(DisasContext *s, uint32_t insn)
5458 unsigned int mos, type, rm, op, rn, opc, op2r;
5459 int size;
5461 mos = extract32(insn, 29, 3);
5462 type = extract32(insn, 22, 2);
5463 rm = extract32(insn, 16, 5);
5464 op = extract32(insn, 14, 2);
5465 rn = extract32(insn, 5, 5);
5466 opc = extract32(insn, 3, 2);
5467 op2r = extract32(insn, 0, 3);
5469 if (mos || op || op2r) {
5470 unallocated_encoding(s);
5471 return;
5474 switch (type) {
5475 case 0:
5476 size = MO_32;
5477 break;
5478 case 1:
5479 size = MO_64;
5480 break;
5481 case 3:
5482 size = MO_16;
5483 if (dc_isar_feature(aa64_fp16, s)) {
5484 break;
5486 /* fallthru */
5487 default:
5488 unallocated_encoding(s);
5489 return;
5492 if (!fp_access_check(s)) {
5493 return;
5496 handle_fp_compare(s, size, rn, rm, opc & 1, opc & 2);
5499 /* Floating point conditional compare
5500 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 3 0
5501 * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
5502 * | M | 0 | S | 1 1 1 1 0 | type | 1 | Rm | cond | 0 1 | Rn | op | nzcv |
5503 * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
5505 static void disas_fp_ccomp(DisasContext *s, uint32_t insn)
5507 unsigned int mos, type, rm, cond, rn, op, nzcv;
5508 TCGv_i64 tcg_flags;
5509 TCGLabel *label_continue = NULL;
5510 int size;
5512 mos = extract32(insn, 29, 3);
5513 type = extract32(insn, 22, 2);
5514 rm = extract32(insn, 16, 5);
5515 cond = extract32(insn, 12, 4);
5516 rn = extract32(insn, 5, 5);
5517 op = extract32(insn, 4, 1);
5518 nzcv = extract32(insn, 0, 4);
5520 if (mos) {
5521 unallocated_encoding(s);
5522 return;
5525 switch (type) {
5526 case 0:
5527 size = MO_32;
5528 break;
5529 case 1:
5530 size = MO_64;
5531 break;
5532 case 3:
5533 size = MO_16;
5534 if (dc_isar_feature(aa64_fp16, s)) {
5535 break;
5537 /* fallthru */
5538 default:
5539 unallocated_encoding(s);
5540 return;
5543 if (!fp_access_check(s)) {
5544 return;
5547 if (cond < 0x0e) { /* not always */
5548 TCGLabel *label_match = gen_new_label();
5549 label_continue = gen_new_label();
5550 arm_gen_test_cc(cond, label_match);
5551 /* nomatch: */
5552 tcg_flags = tcg_const_i64(nzcv << 28);
5553 gen_set_nzcv(tcg_flags);
5554 tcg_temp_free_i64(tcg_flags);
5555 tcg_gen_br(label_continue);
5556 gen_set_label(label_match);
5559 handle_fp_compare(s, size, rn, rm, false, op);
5561 if (cond < 0x0e) {
5562 gen_set_label(label_continue);
5566 /* Floating point conditional select
5567 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 0
5568 * +---+---+---+-----------+------+---+------+------+-----+------+------+
5569 * | M | 0 | S | 1 1 1 1 0 | type | 1 | Rm | cond | 1 1 | Rn | Rd |
5570 * +---+---+---+-----------+------+---+------+------+-----+------+------+
5572 static void disas_fp_csel(DisasContext *s, uint32_t insn)
5574 unsigned int mos, type, rm, cond, rn, rd;
5575 TCGv_i64 t_true, t_false, t_zero;
5576 DisasCompare64 c;
5577 MemOp sz;
5579 mos = extract32(insn, 29, 3);
5580 type = extract32(insn, 22, 2);
5581 rm = extract32(insn, 16, 5);
5582 cond = extract32(insn, 12, 4);
5583 rn = extract32(insn, 5, 5);
5584 rd = extract32(insn, 0, 5);
5586 if (mos) {
5587 unallocated_encoding(s);
5588 return;
5591 switch (type) {
5592 case 0:
5593 sz = MO_32;
5594 break;
5595 case 1:
5596 sz = MO_64;
5597 break;
5598 case 3:
5599 sz = MO_16;
5600 if (dc_isar_feature(aa64_fp16, s)) {
5601 break;
5603 /* fallthru */
5604 default:
5605 unallocated_encoding(s);
5606 return;
5609 if (!fp_access_check(s)) {
5610 return;
5613 /* Zero extend sreg & hreg inputs to 64 bits now. */
5614 t_true = tcg_temp_new_i64();
5615 t_false = tcg_temp_new_i64();
5616 read_vec_element(s, t_true, rn, 0, sz);
5617 read_vec_element(s, t_false, rm, 0, sz);
5619 a64_test_cc(&c, cond);
5620 t_zero = tcg_const_i64(0);
5621 tcg_gen_movcond_i64(c.cond, t_true, c.value, t_zero, t_true, t_false);
5622 tcg_temp_free_i64(t_zero);
5623 tcg_temp_free_i64(t_false);
5624 a64_free_cc(&c);
5626 /* Note that sregs & hregs write back zeros to the high bits,
5627 and we've already done the zero-extension. */
5628 write_fp_dreg(s, rd, t_true);
5629 tcg_temp_free_i64(t_true);
5632 /* Floating-point data-processing (1 source) - half precision */
5633 static void handle_fp_1src_half(DisasContext *s, int opcode, int rd, int rn)
5635 TCGv_ptr fpst = NULL;
5636 TCGv_i32 tcg_op = read_fp_hreg(s, rn);
5637 TCGv_i32 tcg_res = tcg_temp_new_i32();
5639 switch (opcode) {
5640 case 0x0: /* FMOV */
5641 tcg_gen_mov_i32(tcg_res, tcg_op);
5642 break;
5643 case 0x1: /* FABS */
5644 tcg_gen_andi_i32(tcg_res, tcg_op, 0x7fff);
5645 break;
5646 case 0x2: /* FNEG */
5647 tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
5648 break;
5649 case 0x3: /* FSQRT */
5650 fpst = get_fpstatus_ptr(true);
5651 gen_helper_sqrt_f16(tcg_res, tcg_op, fpst);
5652 break;
5653 case 0x8: /* FRINTN */
5654 case 0x9: /* FRINTP */
5655 case 0xa: /* FRINTM */
5656 case 0xb: /* FRINTZ */
5657 case 0xc: /* FRINTA */
5659 TCGv_i32 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(opcode & 7));
5660 fpst = get_fpstatus_ptr(true);
5662 gen_helper_set_rmode(tcg_rmode, tcg_rmode, fpst);
5663 gen_helper_advsimd_rinth(tcg_res, tcg_op, fpst);
5665 gen_helper_set_rmode(tcg_rmode, tcg_rmode, fpst);
5666 tcg_temp_free_i32(tcg_rmode);
5667 break;
5669 case 0xe: /* FRINTX */
5670 fpst = get_fpstatus_ptr(true);
5671 gen_helper_advsimd_rinth_exact(tcg_res, tcg_op, fpst);
5672 break;
5673 case 0xf: /* FRINTI */
5674 fpst = get_fpstatus_ptr(true);
5675 gen_helper_advsimd_rinth(tcg_res, tcg_op, fpst);
5676 break;
5677 default:
5678 abort();
5681 write_fp_sreg(s, rd, tcg_res);
5683 if (fpst) {
5684 tcg_temp_free_ptr(fpst);
5686 tcg_temp_free_i32(tcg_op);
5687 tcg_temp_free_i32(tcg_res);
5690 /* Floating-point data-processing (1 source) - single precision */
5691 static void handle_fp_1src_single(DisasContext *s, int opcode, int rd, int rn)
5693 void (*gen_fpst)(TCGv_i32, TCGv_i32, TCGv_ptr);
5694 TCGv_i32 tcg_op, tcg_res;
5695 TCGv_ptr fpst;
5696 int rmode = -1;
5698 tcg_op = read_fp_sreg(s, rn);
5699 tcg_res = tcg_temp_new_i32();
5701 switch (opcode) {
5702 case 0x0: /* FMOV */
5703 tcg_gen_mov_i32(tcg_res, tcg_op);
5704 goto done;
5705 case 0x1: /* FABS */
5706 gen_helper_vfp_abss(tcg_res, tcg_op);
5707 goto done;
5708 case 0x2: /* FNEG */
5709 gen_helper_vfp_negs(tcg_res, tcg_op);
5710 goto done;
5711 case 0x3: /* FSQRT */
5712 gen_helper_vfp_sqrts(tcg_res, tcg_op, cpu_env);
5713 goto done;
5714 case 0x8: /* FRINTN */
5715 case 0x9: /* FRINTP */
5716 case 0xa: /* FRINTM */
5717 case 0xb: /* FRINTZ */
5718 case 0xc: /* FRINTA */
5719 rmode = arm_rmode_to_sf(opcode & 7);
5720 gen_fpst = gen_helper_rints;
5721 break;
5722 case 0xe: /* FRINTX */
5723 gen_fpst = gen_helper_rints_exact;
5724 break;
5725 case 0xf: /* FRINTI */
5726 gen_fpst = gen_helper_rints;
5727 break;
5728 case 0x10: /* FRINT32Z */
5729 rmode = float_round_to_zero;
5730 gen_fpst = gen_helper_frint32_s;
5731 break;
5732 case 0x11: /* FRINT32X */
5733 gen_fpst = gen_helper_frint32_s;
5734 break;
5735 case 0x12: /* FRINT64Z */
5736 rmode = float_round_to_zero;
5737 gen_fpst = gen_helper_frint64_s;
5738 break;
5739 case 0x13: /* FRINT64X */
5740 gen_fpst = gen_helper_frint64_s;
5741 break;
5742 default:
5743 g_assert_not_reached();
5746 fpst = get_fpstatus_ptr(false);
5747 if (rmode >= 0) {
5748 TCGv_i32 tcg_rmode = tcg_const_i32(rmode);
5749 gen_helper_set_rmode(tcg_rmode, tcg_rmode, fpst);
5750 gen_fpst(tcg_res, tcg_op, fpst);
5751 gen_helper_set_rmode(tcg_rmode, tcg_rmode, fpst);
5752 tcg_temp_free_i32(tcg_rmode);
5753 } else {
5754 gen_fpst(tcg_res, tcg_op, fpst);
5756 tcg_temp_free_ptr(fpst);
5758 done:
5759 write_fp_sreg(s, rd, tcg_res);
5760 tcg_temp_free_i32(tcg_op);
5761 tcg_temp_free_i32(tcg_res);
5764 /* Floating-point data-processing (1 source) - double precision */
5765 static void handle_fp_1src_double(DisasContext *s, int opcode, int rd, int rn)
5767 void (*gen_fpst)(TCGv_i64, TCGv_i64, TCGv_ptr);
5768 TCGv_i64 tcg_op, tcg_res;
5769 TCGv_ptr fpst;
5770 int rmode = -1;
5772 switch (opcode) {
5773 case 0x0: /* FMOV */
5774 gen_gvec_fn2(s, false, rd, rn, tcg_gen_gvec_mov, 0);
5775 return;
5778 tcg_op = read_fp_dreg(s, rn);
5779 tcg_res = tcg_temp_new_i64();
5781 switch (opcode) {
5782 case 0x1: /* FABS */
5783 gen_helper_vfp_absd(tcg_res, tcg_op);
5784 goto done;
5785 case 0x2: /* FNEG */
5786 gen_helper_vfp_negd(tcg_res, tcg_op);
5787 goto done;
5788 case 0x3: /* FSQRT */
5789 gen_helper_vfp_sqrtd(tcg_res, tcg_op, cpu_env);
5790 goto done;
5791 case 0x8: /* FRINTN */
5792 case 0x9: /* FRINTP */
5793 case 0xa: /* FRINTM */
5794 case 0xb: /* FRINTZ */
5795 case 0xc: /* FRINTA */
5796 rmode = arm_rmode_to_sf(opcode & 7);
5797 gen_fpst = gen_helper_rintd;
5798 break;
5799 case 0xe: /* FRINTX */
5800 gen_fpst = gen_helper_rintd_exact;
5801 break;
5802 case 0xf: /* FRINTI */
5803 gen_fpst = gen_helper_rintd;
5804 break;
5805 case 0x10: /* FRINT32Z */
5806 rmode = float_round_to_zero;
5807 gen_fpst = gen_helper_frint32_d;
5808 break;
5809 case 0x11: /* FRINT32X */
5810 gen_fpst = gen_helper_frint32_d;
5811 break;
5812 case 0x12: /* FRINT64Z */
5813 rmode = float_round_to_zero;
5814 gen_fpst = gen_helper_frint64_d;
5815 break;
5816 case 0x13: /* FRINT64X */
5817 gen_fpst = gen_helper_frint64_d;
5818 break;
5819 default:
5820 g_assert_not_reached();
5823 fpst = get_fpstatus_ptr(false);
5824 if (rmode >= 0) {
5825 TCGv_i32 tcg_rmode = tcg_const_i32(rmode);
5826 gen_helper_set_rmode(tcg_rmode, tcg_rmode, fpst);
5827 gen_fpst(tcg_res, tcg_op, fpst);
5828 gen_helper_set_rmode(tcg_rmode, tcg_rmode, fpst);
5829 tcg_temp_free_i32(tcg_rmode);
5830 } else {
5831 gen_fpst(tcg_res, tcg_op, fpst);
5833 tcg_temp_free_ptr(fpst);
5835 done:
5836 write_fp_dreg(s, rd, tcg_res);
5837 tcg_temp_free_i64(tcg_op);
5838 tcg_temp_free_i64(tcg_res);
5841 static void handle_fp_fcvt(DisasContext *s, int opcode,
5842 int rd, int rn, int dtype, int ntype)
5844 switch (ntype) {
5845 case 0x0:
5847 TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
5848 if (dtype == 1) {
5849 /* Single to double */
5850 TCGv_i64 tcg_rd = tcg_temp_new_i64();
5851 gen_helper_vfp_fcvtds(tcg_rd, tcg_rn, cpu_env);
5852 write_fp_dreg(s, rd, tcg_rd);
5853 tcg_temp_free_i64(tcg_rd);
5854 } else {
5855 /* Single to half */
5856 TCGv_i32 tcg_rd = tcg_temp_new_i32();
5857 TCGv_i32 ahp = get_ahp_flag();
5858 TCGv_ptr fpst = get_fpstatus_ptr(false);
5860 gen_helper_vfp_fcvt_f32_to_f16(tcg_rd, tcg_rn, fpst, ahp);
5861 /* write_fp_sreg is OK here because top half of tcg_rd is zero */
5862 write_fp_sreg(s, rd, tcg_rd);
5863 tcg_temp_free_i32(tcg_rd);
5864 tcg_temp_free_i32(ahp);
5865 tcg_temp_free_ptr(fpst);
5867 tcg_temp_free_i32(tcg_rn);
5868 break;
5870 case 0x1:
5872 TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
5873 TCGv_i32 tcg_rd = tcg_temp_new_i32();
5874 if (dtype == 0) {
5875 /* Double to single */
5876 gen_helper_vfp_fcvtsd(tcg_rd, tcg_rn, cpu_env);
5877 } else {
5878 TCGv_ptr fpst = get_fpstatus_ptr(false);
5879 TCGv_i32 ahp = get_ahp_flag();
5880 /* Double to half */
5881 gen_helper_vfp_fcvt_f64_to_f16(tcg_rd, tcg_rn, fpst, ahp);
5882 /* write_fp_sreg is OK here because top half of tcg_rd is zero */
5883 tcg_temp_free_ptr(fpst);
5884 tcg_temp_free_i32(ahp);
5886 write_fp_sreg(s, rd, tcg_rd);
5887 tcg_temp_free_i32(tcg_rd);
5888 tcg_temp_free_i64(tcg_rn);
5889 break;
5891 case 0x3:
5893 TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
5894 TCGv_ptr tcg_fpst = get_fpstatus_ptr(false);
5895 TCGv_i32 tcg_ahp = get_ahp_flag();
5896 tcg_gen_ext16u_i32(tcg_rn, tcg_rn);
5897 if (dtype == 0) {
5898 /* Half to single */
5899 TCGv_i32 tcg_rd = tcg_temp_new_i32();
5900 gen_helper_vfp_fcvt_f16_to_f32(tcg_rd, tcg_rn, tcg_fpst, tcg_ahp);
5901 write_fp_sreg(s, rd, tcg_rd);
5902 tcg_temp_free_i32(tcg_rd);
5903 } else {
5904 /* Half to double */
5905 TCGv_i64 tcg_rd = tcg_temp_new_i64();
5906 gen_helper_vfp_fcvt_f16_to_f64(tcg_rd, tcg_rn, tcg_fpst, tcg_ahp);
5907 write_fp_dreg(s, rd, tcg_rd);
5908 tcg_temp_free_i64(tcg_rd);
5910 tcg_temp_free_i32(tcg_rn);
5911 tcg_temp_free_ptr(tcg_fpst);
5912 tcg_temp_free_i32(tcg_ahp);
5913 break;
5915 default:
5916 abort();
5920 /* Floating point data-processing (1 source)
5921 * 31 30 29 28 24 23 22 21 20 15 14 10 9 5 4 0
5922 * +---+---+---+-----------+------+---+--------+-----------+------+------+
5923 * | M | 0 | S | 1 1 1 1 0 | type | 1 | opcode | 1 0 0 0 0 | Rn | Rd |
5924 * +---+---+---+-----------+------+---+--------+-----------+------+------+
5926 static void disas_fp_1src(DisasContext *s, uint32_t insn)
5928 int mos = extract32(insn, 29, 3);
5929 int type = extract32(insn, 22, 2);
5930 int opcode = extract32(insn, 15, 6);
5931 int rn = extract32(insn, 5, 5);
5932 int rd = extract32(insn, 0, 5);
5934 if (mos) {
5935 unallocated_encoding(s);
5936 return;
5939 switch (opcode) {
5940 case 0x4: case 0x5: case 0x7:
5942 /* FCVT between half, single and double precision */
5943 int dtype = extract32(opcode, 0, 2);
5944 if (type == 2 || dtype == type) {
5945 unallocated_encoding(s);
5946 return;
5948 if (!fp_access_check(s)) {
5949 return;
5952 handle_fp_fcvt(s, opcode, rd, rn, dtype, type);
5953 break;
5956 case 0x10 ... 0x13: /* FRINT{32,64}{X,Z} */
5957 if (type > 1 || !dc_isar_feature(aa64_frint, s)) {
5958 unallocated_encoding(s);
5959 return;
5961 /* fall through */
5962 case 0x0 ... 0x3:
5963 case 0x8 ... 0xc:
5964 case 0xe ... 0xf:
5965 /* 32-to-32 and 64-to-64 ops */
5966 switch (type) {
5967 case 0:
5968 if (!fp_access_check(s)) {
5969 return;
5971 handle_fp_1src_single(s, opcode, rd, rn);
5972 break;
5973 case 1:
5974 if (!fp_access_check(s)) {
5975 return;
5977 handle_fp_1src_double(s, opcode, rd, rn);
5978 break;
5979 case 3:
5980 if (!dc_isar_feature(aa64_fp16, s)) {
5981 unallocated_encoding(s);
5982 return;
5985 if (!fp_access_check(s)) {
5986 return;
5988 handle_fp_1src_half(s, opcode, rd, rn);
5989 break;
5990 default:
5991 unallocated_encoding(s);
5993 break;
5995 default:
5996 unallocated_encoding(s);
5997 break;
6001 /* Floating-point data-processing (2 source) - single precision */
6002 static void handle_fp_2src_single(DisasContext *s, int opcode,
6003 int rd, int rn, int rm)
6005 TCGv_i32 tcg_op1;
6006 TCGv_i32 tcg_op2;
6007 TCGv_i32 tcg_res;
6008 TCGv_ptr fpst;
6010 tcg_res = tcg_temp_new_i32();
6011 fpst = get_fpstatus_ptr(false);
6012 tcg_op1 = read_fp_sreg(s, rn);
6013 tcg_op2 = read_fp_sreg(s, rm);
6015 switch (opcode) {
6016 case 0x0: /* FMUL */
6017 gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
6018 break;
6019 case 0x1: /* FDIV */
6020 gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
6021 break;
6022 case 0x2: /* FADD */
6023 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
6024 break;
6025 case 0x3: /* FSUB */
6026 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
6027 break;
6028 case 0x4: /* FMAX */
6029 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
6030 break;
6031 case 0x5: /* FMIN */
6032 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
6033 break;
6034 case 0x6: /* FMAXNM */
6035 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
6036 break;
6037 case 0x7: /* FMINNM */
6038 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
6039 break;
6040 case 0x8: /* FNMUL */
6041 gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
6042 gen_helper_vfp_negs(tcg_res, tcg_res);
6043 break;
6046 write_fp_sreg(s, rd, tcg_res);
6048 tcg_temp_free_ptr(fpst);
6049 tcg_temp_free_i32(tcg_op1);
6050 tcg_temp_free_i32(tcg_op2);
6051 tcg_temp_free_i32(tcg_res);
6054 /* Floating-point data-processing (2 source) - double precision */
6055 static void handle_fp_2src_double(DisasContext *s, int opcode,
6056 int rd, int rn, int rm)
6058 TCGv_i64 tcg_op1;
6059 TCGv_i64 tcg_op2;
6060 TCGv_i64 tcg_res;
6061 TCGv_ptr fpst;
6063 tcg_res = tcg_temp_new_i64();
6064 fpst = get_fpstatus_ptr(false);
6065 tcg_op1 = read_fp_dreg(s, rn);
6066 tcg_op2 = read_fp_dreg(s, rm);
6068 switch (opcode) {
6069 case 0x0: /* FMUL */
6070 gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
6071 break;
6072 case 0x1: /* FDIV */
6073 gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
6074 break;
6075 case 0x2: /* FADD */
6076 gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
6077 break;
6078 case 0x3: /* FSUB */
6079 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
6080 break;
6081 case 0x4: /* FMAX */
6082 gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
6083 break;
6084 case 0x5: /* FMIN */
6085 gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
6086 break;
6087 case 0x6: /* FMAXNM */
6088 gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
6089 break;
6090 case 0x7: /* FMINNM */
6091 gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
6092 break;
6093 case 0x8: /* FNMUL */
6094 gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
6095 gen_helper_vfp_negd(tcg_res, tcg_res);
6096 break;
6099 write_fp_dreg(s, rd, tcg_res);
6101 tcg_temp_free_ptr(fpst);
6102 tcg_temp_free_i64(tcg_op1);
6103 tcg_temp_free_i64(tcg_op2);
6104 tcg_temp_free_i64(tcg_res);
6107 /* Floating-point data-processing (2 source) - half precision */
6108 static void handle_fp_2src_half(DisasContext *s, int opcode,
6109 int rd, int rn, int rm)
6111 TCGv_i32 tcg_op1;
6112 TCGv_i32 tcg_op2;
6113 TCGv_i32 tcg_res;
6114 TCGv_ptr fpst;
6116 tcg_res = tcg_temp_new_i32();
6117 fpst = get_fpstatus_ptr(true);
6118 tcg_op1 = read_fp_hreg(s, rn);
6119 tcg_op2 = read_fp_hreg(s, rm);
6121 switch (opcode) {
6122 case 0x0: /* FMUL */
6123 gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
6124 break;
6125 case 0x1: /* FDIV */
6126 gen_helper_advsimd_divh(tcg_res, tcg_op1, tcg_op2, fpst);
6127 break;
6128 case 0x2: /* FADD */
6129 gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
6130 break;
6131 case 0x3: /* FSUB */
6132 gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
6133 break;
6134 case 0x4: /* FMAX */
6135 gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
6136 break;
6137 case 0x5: /* FMIN */
6138 gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
6139 break;
6140 case 0x6: /* FMAXNM */
6141 gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
6142 break;
6143 case 0x7: /* FMINNM */
6144 gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
6145 break;
6146 case 0x8: /* FNMUL */
6147 gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
6148 tcg_gen_xori_i32(tcg_res, tcg_res, 0x8000);
6149 break;
6150 default:
6151 g_assert_not_reached();
6154 write_fp_sreg(s, rd, tcg_res);
6156 tcg_temp_free_ptr(fpst);
6157 tcg_temp_free_i32(tcg_op1);
6158 tcg_temp_free_i32(tcg_op2);
6159 tcg_temp_free_i32(tcg_res);
6162 /* Floating point data-processing (2 source)
6163 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 0
6164 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
6165 * | M | 0 | S | 1 1 1 1 0 | type | 1 | Rm | opcode | 1 0 | Rn | Rd |
6166 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
6168 static void disas_fp_2src(DisasContext *s, uint32_t insn)
6170 int mos = extract32(insn, 29, 3);
6171 int type = extract32(insn, 22, 2);
6172 int rd = extract32(insn, 0, 5);
6173 int rn = extract32(insn, 5, 5);
6174 int rm = extract32(insn, 16, 5);
6175 int opcode = extract32(insn, 12, 4);
6177 if (opcode > 8 || mos) {
6178 unallocated_encoding(s);
6179 return;
6182 switch (type) {
6183 case 0:
6184 if (!fp_access_check(s)) {
6185 return;
6187 handle_fp_2src_single(s, opcode, rd, rn, rm);
6188 break;
6189 case 1:
6190 if (!fp_access_check(s)) {
6191 return;
6193 handle_fp_2src_double(s, opcode, rd, rn, rm);
6194 break;
6195 case 3:
6196 if (!dc_isar_feature(aa64_fp16, s)) {
6197 unallocated_encoding(s);
6198 return;
6200 if (!fp_access_check(s)) {
6201 return;
6203 handle_fp_2src_half(s, opcode, rd, rn, rm);
6204 break;
6205 default:
6206 unallocated_encoding(s);
6210 /* Floating-point data-processing (3 source) - single precision */
6211 static void handle_fp_3src_single(DisasContext *s, bool o0, bool o1,
6212 int rd, int rn, int rm, int ra)
6214 TCGv_i32 tcg_op1, tcg_op2, tcg_op3;
6215 TCGv_i32 tcg_res = tcg_temp_new_i32();
6216 TCGv_ptr fpst = get_fpstatus_ptr(false);
6218 tcg_op1 = read_fp_sreg(s, rn);
6219 tcg_op2 = read_fp_sreg(s, rm);
6220 tcg_op3 = read_fp_sreg(s, ra);
6222 /* These are fused multiply-add, and must be done as one
6223 * floating point operation with no rounding between the
6224 * multiplication and addition steps.
6225 * NB that doing the negations here as separate steps is
6226 * correct : an input NaN should come out with its sign bit
6227 * flipped if it is a negated-input.
6229 if (o1 == true) {
6230 gen_helper_vfp_negs(tcg_op3, tcg_op3);
6233 if (o0 != o1) {
6234 gen_helper_vfp_negs(tcg_op1, tcg_op1);
6237 gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
6239 write_fp_sreg(s, rd, tcg_res);
6241 tcg_temp_free_ptr(fpst);
6242 tcg_temp_free_i32(tcg_op1);
6243 tcg_temp_free_i32(tcg_op2);
6244 tcg_temp_free_i32(tcg_op3);
6245 tcg_temp_free_i32(tcg_res);
6248 /* Floating-point data-processing (3 source) - double precision */
6249 static void handle_fp_3src_double(DisasContext *s, bool o0, bool o1,
6250 int rd, int rn, int rm, int ra)
6252 TCGv_i64 tcg_op1, tcg_op2, tcg_op3;
6253 TCGv_i64 tcg_res = tcg_temp_new_i64();
6254 TCGv_ptr fpst = get_fpstatus_ptr(false);
6256 tcg_op1 = read_fp_dreg(s, rn);
6257 tcg_op2 = read_fp_dreg(s, rm);
6258 tcg_op3 = read_fp_dreg(s, ra);
6260 /* These are fused multiply-add, and must be done as one
6261 * floating point operation with no rounding between the
6262 * multiplication and addition steps.
6263 * NB that doing the negations here as separate steps is
6264 * correct : an input NaN should come out with its sign bit
6265 * flipped if it is a negated-input.
6267 if (o1 == true) {
6268 gen_helper_vfp_negd(tcg_op3, tcg_op3);
6271 if (o0 != o1) {
6272 gen_helper_vfp_negd(tcg_op1, tcg_op1);
6275 gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
6277 write_fp_dreg(s, rd, tcg_res);
6279 tcg_temp_free_ptr(fpst);
6280 tcg_temp_free_i64(tcg_op1);
6281 tcg_temp_free_i64(tcg_op2);
6282 tcg_temp_free_i64(tcg_op3);
6283 tcg_temp_free_i64(tcg_res);
6286 /* Floating-point data-processing (3 source) - half precision */
6287 static void handle_fp_3src_half(DisasContext *s, bool o0, bool o1,
6288 int rd, int rn, int rm, int ra)
6290 TCGv_i32 tcg_op1, tcg_op2, tcg_op3;
6291 TCGv_i32 tcg_res = tcg_temp_new_i32();
6292 TCGv_ptr fpst = get_fpstatus_ptr(true);
6294 tcg_op1 = read_fp_hreg(s, rn);
6295 tcg_op2 = read_fp_hreg(s, rm);
6296 tcg_op3 = read_fp_hreg(s, ra);
6298 /* These are fused multiply-add, and must be done as one
6299 * floating point operation with no rounding between the
6300 * multiplication and addition steps.
6301 * NB that doing the negations here as separate steps is
6302 * correct : an input NaN should come out with its sign bit
6303 * flipped if it is a negated-input.
6305 if (o1 == true) {
6306 tcg_gen_xori_i32(tcg_op3, tcg_op3, 0x8000);
6309 if (o0 != o1) {
6310 tcg_gen_xori_i32(tcg_op1, tcg_op1, 0x8000);
6313 gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
6315 write_fp_sreg(s, rd, tcg_res);
6317 tcg_temp_free_ptr(fpst);
6318 tcg_temp_free_i32(tcg_op1);
6319 tcg_temp_free_i32(tcg_op2);
6320 tcg_temp_free_i32(tcg_op3);
6321 tcg_temp_free_i32(tcg_res);
6324 /* Floating point data-processing (3 source)
6325 * 31 30 29 28 24 23 22 21 20 16 15 14 10 9 5 4 0
6326 * +---+---+---+-----------+------+----+------+----+------+------+------+
6327 * | M | 0 | S | 1 1 1 1 1 | type | o1 | Rm | o0 | Ra | Rn | Rd |
6328 * +---+---+---+-----------+------+----+------+----+------+------+------+
6330 static void disas_fp_3src(DisasContext *s, uint32_t insn)
6332 int mos = extract32(insn, 29, 3);
6333 int type = extract32(insn, 22, 2);
6334 int rd = extract32(insn, 0, 5);
6335 int rn = extract32(insn, 5, 5);
6336 int ra = extract32(insn, 10, 5);
6337 int rm = extract32(insn, 16, 5);
6338 bool o0 = extract32(insn, 15, 1);
6339 bool o1 = extract32(insn, 21, 1);
6341 if (mos) {
6342 unallocated_encoding(s);
6343 return;
6346 switch (type) {
6347 case 0:
6348 if (!fp_access_check(s)) {
6349 return;
6351 handle_fp_3src_single(s, o0, o1, rd, rn, rm, ra);
6352 break;
6353 case 1:
6354 if (!fp_access_check(s)) {
6355 return;
6357 handle_fp_3src_double(s, o0, o1, rd, rn, rm, ra);
6358 break;
6359 case 3:
6360 if (!dc_isar_feature(aa64_fp16, s)) {
6361 unallocated_encoding(s);
6362 return;
6364 if (!fp_access_check(s)) {
6365 return;
6367 handle_fp_3src_half(s, o0, o1, rd, rn, rm, ra);
6368 break;
6369 default:
6370 unallocated_encoding(s);
6374 /* Floating point immediate
6375 * 31 30 29 28 24 23 22 21 20 13 12 10 9 5 4 0
6376 * +---+---+---+-----------+------+---+------------+-------+------+------+
6377 * | M | 0 | S | 1 1 1 1 0 | type | 1 | imm8 | 1 0 0 | imm5 | Rd |
6378 * +---+---+---+-----------+------+---+------------+-------+------+------+
6380 static void disas_fp_imm(DisasContext *s, uint32_t insn)
6382 int rd = extract32(insn, 0, 5);
6383 int imm5 = extract32(insn, 5, 5);
6384 int imm8 = extract32(insn, 13, 8);
6385 int type = extract32(insn, 22, 2);
6386 int mos = extract32(insn, 29, 3);
6387 uint64_t imm;
6388 TCGv_i64 tcg_res;
6389 MemOp sz;
6391 if (mos || imm5) {
6392 unallocated_encoding(s);
6393 return;
6396 switch (type) {
6397 case 0:
6398 sz = MO_32;
6399 break;
6400 case 1:
6401 sz = MO_64;
6402 break;
6403 case 3:
6404 sz = MO_16;
6405 if (dc_isar_feature(aa64_fp16, s)) {
6406 break;
6408 /* fallthru */
6409 default:
6410 unallocated_encoding(s);
6411 return;
6414 if (!fp_access_check(s)) {
6415 return;
6418 imm = vfp_expand_imm(sz, imm8);
6420 tcg_res = tcg_const_i64(imm);
6421 write_fp_dreg(s, rd, tcg_res);
6422 tcg_temp_free_i64(tcg_res);
6425 /* Handle floating point <=> fixed point conversions. Note that we can
6426 * also deal with fp <=> integer conversions as a special case (scale == 64)
6427 * OPTME: consider handling that special case specially or at least skipping
6428 * the call to scalbn in the helpers for zero shifts.
6430 static void handle_fpfpcvt(DisasContext *s, int rd, int rn, int opcode,
6431 bool itof, int rmode, int scale, int sf, int type)
6433 bool is_signed = !(opcode & 1);
6434 TCGv_ptr tcg_fpstatus;
6435 TCGv_i32 tcg_shift, tcg_single;
6436 TCGv_i64 tcg_double;
6438 tcg_fpstatus = get_fpstatus_ptr(type == 3);
6440 tcg_shift = tcg_const_i32(64 - scale);
6442 if (itof) {
6443 TCGv_i64 tcg_int = cpu_reg(s, rn);
6444 if (!sf) {
6445 TCGv_i64 tcg_extend = new_tmp_a64(s);
6447 if (is_signed) {
6448 tcg_gen_ext32s_i64(tcg_extend, tcg_int);
6449 } else {
6450 tcg_gen_ext32u_i64(tcg_extend, tcg_int);
6453 tcg_int = tcg_extend;
6456 switch (type) {
6457 case 1: /* float64 */
6458 tcg_double = tcg_temp_new_i64();
6459 if (is_signed) {
6460 gen_helper_vfp_sqtod(tcg_double, tcg_int,
6461 tcg_shift, tcg_fpstatus);
6462 } else {
6463 gen_helper_vfp_uqtod(tcg_double, tcg_int,
6464 tcg_shift, tcg_fpstatus);
6466 write_fp_dreg(s, rd, tcg_double);
6467 tcg_temp_free_i64(tcg_double);
6468 break;
6470 case 0: /* float32 */
6471 tcg_single = tcg_temp_new_i32();
6472 if (is_signed) {
6473 gen_helper_vfp_sqtos(tcg_single, tcg_int,
6474 tcg_shift, tcg_fpstatus);
6475 } else {
6476 gen_helper_vfp_uqtos(tcg_single, tcg_int,
6477 tcg_shift, tcg_fpstatus);
6479 write_fp_sreg(s, rd, tcg_single);
6480 tcg_temp_free_i32(tcg_single);
6481 break;
6483 case 3: /* float16 */
6484 tcg_single = tcg_temp_new_i32();
6485 if (is_signed) {
6486 gen_helper_vfp_sqtoh(tcg_single, tcg_int,
6487 tcg_shift, tcg_fpstatus);
6488 } else {
6489 gen_helper_vfp_uqtoh(tcg_single, tcg_int,
6490 tcg_shift, tcg_fpstatus);
6492 write_fp_sreg(s, rd, tcg_single);
6493 tcg_temp_free_i32(tcg_single);
6494 break;
6496 default:
6497 g_assert_not_reached();
6499 } else {
6500 TCGv_i64 tcg_int = cpu_reg(s, rd);
6501 TCGv_i32 tcg_rmode;
6503 if (extract32(opcode, 2, 1)) {
6504 /* There are too many rounding modes to all fit into rmode,
6505 * so FCVTA[US] is a special case.
6507 rmode = FPROUNDING_TIEAWAY;
6510 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rmode));
6512 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
6514 switch (type) {
6515 case 1: /* float64 */
6516 tcg_double = read_fp_dreg(s, rn);
6517 if (is_signed) {
6518 if (!sf) {
6519 gen_helper_vfp_tosld(tcg_int, tcg_double,
6520 tcg_shift, tcg_fpstatus);
6521 } else {
6522 gen_helper_vfp_tosqd(tcg_int, tcg_double,
6523 tcg_shift, tcg_fpstatus);
6525 } else {
6526 if (!sf) {
6527 gen_helper_vfp_tould(tcg_int, tcg_double,
6528 tcg_shift, tcg_fpstatus);
6529 } else {
6530 gen_helper_vfp_touqd(tcg_int, tcg_double,
6531 tcg_shift, tcg_fpstatus);
6534 if (!sf) {
6535 tcg_gen_ext32u_i64(tcg_int, tcg_int);
6537 tcg_temp_free_i64(tcg_double);
6538 break;
6540 case 0: /* float32 */
6541 tcg_single = read_fp_sreg(s, rn);
6542 if (sf) {
6543 if (is_signed) {
6544 gen_helper_vfp_tosqs(tcg_int, tcg_single,
6545 tcg_shift, tcg_fpstatus);
6546 } else {
6547 gen_helper_vfp_touqs(tcg_int, tcg_single,
6548 tcg_shift, tcg_fpstatus);
6550 } else {
6551 TCGv_i32 tcg_dest = tcg_temp_new_i32();
6552 if (is_signed) {
6553 gen_helper_vfp_tosls(tcg_dest, tcg_single,
6554 tcg_shift, tcg_fpstatus);
6555 } else {
6556 gen_helper_vfp_touls(tcg_dest, tcg_single,
6557 tcg_shift, tcg_fpstatus);
6559 tcg_gen_extu_i32_i64(tcg_int, tcg_dest);
6560 tcg_temp_free_i32(tcg_dest);
6562 tcg_temp_free_i32(tcg_single);
6563 break;
6565 case 3: /* float16 */
6566 tcg_single = read_fp_sreg(s, rn);
6567 if (sf) {
6568 if (is_signed) {
6569 gen_helper_vfp_tosqh(tcg_int, tcg_single,
6570 tcg_shift, tcg_fpstatus);
6571 } else {
6572 gen_helper_vfp_touqh(tcg_int, tcg_single,
6573 tcg_shift, tcg_fpstatus);
6575 } else {
6576 TCGv_i32 tcg_dest = tcg_temp_new_i32();
6577 if (is_signed) {
6578 gen_helper_vfp_toslh(tcg_dest, tcg_single,
6579 tcg_shift, tcg_fpstatus);
6580 } else {
6581 gen_helper_vfp_toulh(tcg_dest, tcg_single,
6582 tcg_shift, tcg_fpstatus);
6584 tcg_gen_extu_i32_i64(tcg_int, tcg_dest);
6585 tcg_temp_free_i32(tcg_dest);
6587 tcg_temp_free_i32(tcg_single);
6588 break;
6590 default:
6591 g_assert_not_reached();
6594 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
6595 tcg_temp_free_i32(tcg_rmode);
6598 tcg_temp_free_ptr(tcg_fpstatus);
6599 tcg_temp_free_i32(tcg_shift);
6602 /* Floating point <-> fixed point 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 | 0 | rmode | opcode | scale | Rn | Rd |
6606 * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
6608 static void disas_fp_fixed_conv(DisasContext *s, uint32_t insn)
6610 int rd = extract32(insn, 0, 5);
6611 int rn = extract32(insn, 5, 5);
6612 int scale = extract32(insn, 10, 6);
6613 int opcode = extract32(insn, 16, 3);
6614 int rmode = extract32(insn, 19, 2);
6615 int type = extract32(insn, 22, 2);
6616 bool sbit = extract32(insn, 29, 1);
6617 bool sf = extract32(insn, 31, 1);
6618 bool itof;
6620 if (sbit || (!sf && scale < 32)) {
6621 unallocated_encoding(s);
6622 return;
6625 switch (type) {
6626 case 0: /* float32 */
6627 case 1: /* float64 */
6628 break;
6629 case 3: /* float16 */
6630 if (dc_isar_feature(aa64_fp16, s)) {
6631 break;
6633 /* fallthru */
6634 default:
6635 unallocated_encoding(s);
6636 return;
6639 switch ((rmode << 3) | opcode) {
6640 case 0x2: /* SCVTF */
6641 case 0x3: /* UCVTF */
6642 itof = true;
6643 break;
6644 case 0x18: /* FCVTZS */
6645 case 0x19: /* FCVTZU */
6646 itof = false;
6647 break;
6648 default:
6649 unallocated_encoding(s);
6650 return;
6653 if (!fp_access_check(s)) {
6654 return;
6657 handle_fpfpcvt(s, rd, rn, opcode, itof, FPROUNDING_ZERO, scale, sf, type);
6660 static void handle_fmov(DisasContext *s, int rd, int rn, int type, bool itof)
6662 /* FMOV: gpr to or from float, double, or top half of quad fp reg,
6663 * without conversion.
6666 if (itof) {
6667 TCGv_i64 tcg_rn = cpu_reg(s, rn);
6668 TCGv_i64 tmp;
6670 switch (type) {
6671 case 0:
6672 /* 32 bit */
6673 tmp = tcg_temp_new_i64();
6674 tcg_gen_ext32u_i64(tmp, tcg_rn);
6675 write_fp_dreg(s, rd, tmp);
6676 tcg_temp_free_i64(tmp);
6677 break;
6678 case 1:
6679 /* 64 bit */
6680 write_fp_dreg(s, rd, tcg_rn);
6681 break;
6682 case 2:
6683 /* 64 bit to top half. */
6684 tcg_gen_st_i64(tcg_rn, cpu_env, fp_reg_hi_offset(s, rd));
6685 clear_vec_high(s, true, rd);
6686 break;
6687 case 3:
6688 /* 16 bit */
6689 tmp = tcg_temp_new_i64();
6690 tcg_gen_ext16u_i64(tmp, tcg_rn);
6691 write_fp_dreg(s, rd, tmp);
6692 tcg_temp_free_i64(tmp);
6693 break;
6694 default:
6695 g_assert_not_reached();
6697 } else {
6698 TCGv_i64 tcg_rd = cpu_reg(s, rd);
6700 switch (type) {
6701 case 0:
6702 /* 32 bit */
6703 tcg_gen_ld32u_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_32));
6704 break;
6705 case 1:
6706 /* 64 bit */
6707 tcg_gen_ld_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_64));
6708 break;
6709 case 2:
6710 /* 64 bits from top half */
6711 tcg_gen_ld_i64(tcg_rd, cpu_env, fp_reg_hi_offset(s, rn));
6712 break;
6713 case 3:
6714 /* 16 bit */
6715 tcg_gen_ld16u_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_16));
6716 break;
6717 default:
6718 g_assert_not_reached();
6723 static void handle_fjcvtzs(DisasContext *s, int rd, int rn)
6725 TCGv_i64 t = read_fp_dreg(s, rn);
6726 TCGv_ptr fpstatus = get_fpstatus_ptr(false);
6728 gen_helper_fjcvtzs(t, t, fpstatus);
6730 tcg_temp_free_ptr(fpstatus);
6732 tcg_gen_ext32u_i64(cpu_reg(s, rd), t);
6733 tcg_gen_extrh_i64_i32(cpu_ZF, t);
6734 tcg_gen_movi_i32(cpu_CF, 0);
6735 tcg_gen_movi_i32(cpu_NF, 0);
6736 tcg_gen_movi_i32(cpu_VF, 0);
6738 tcg_temp_free_i64(t);
6741 /* Floating point <-> integer conversions
6742 * 31 30 29 28 24 23 22 21 20 19 18 16 15 10 9 5 4 0
6743 * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
6744 * | sf | 0 | S | 1 1 1 1 0 | type | 1 | rmode | opc | 0 0 0 0 0 0 | Rn | Rd |
6745 * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
6747 static void disas_fp_int_conv(DisasContext *s, uint32_t insn)
6749 int rd = extract32(insn, 0, 5);
6750 int rn = extract32(insn, 5, 5);
6751 int opcode = extract32(insn, 16, 3);
6752 int rmode = extract32(insn, 19, 2);
6753 int type = extract32(insn, 22, 2);
6754 bool sbit = extract32(insn, 29, 1);
6755 bool sf = extract32(insn, 31, 1);
6756 bool itof = false;
6758 if (sbit) {
6759 goto do_unallocated;
6762 switch (opcode) {
6763 case 2: /* SCVTF */
6764 case 3: /* UCVTF */
6765 itof = true;
6766 /* fallthru */
6767 case 4: /* FCVTAS */
6768 case 5: /* FCVTAU */
6769 if (rmode != 0) {
6770 goto do_unallocated;
6772 /* fallthru */
6773 case 0: /* FCVT[NPMZ]S */
6774 case 1: /* FCVT[NPMZ]U */
6775 switch (type) {
6776 case 0: /* float32 */
6777 case 1: /* float64 */
6778 break;
6779 case 3: /* float16 */
6780 if (!dc_isar_feature(aa64_fp16, s)) {
6781 goto do_unallocated;
6783 break;
6784 default:
6785 goto do_unallocated;
6787 if (!fp_access_check(s)) {
6788 return;
6790 handle_fpfpcvt(s, rd, rn, opcode, itof, rmode, 64, sf, type);
6791 break;
6793 default:
6794 switch (sf << 7 | type << 5 | rmode << 3 | opcode) {
6795 case 0b01100110: /* FMOV half <-> 32-bit int */
6796 case 0b01100111:
6797 case 0b11100110: /* FMOV half <-> 64-bit int */
6798 case 0b11100111:
6799 if (!dc_isar_feature(aa64_fp16, s)) {
6800 goto do_unallocated;
6802 /* fallthru */
6803 case 0b00000110: /* FMOV 32-bit */
6804 case 0b00000111:
6805 case 0b10100110: /* FMOV 64-bit */
6806 case 0b10100111:
6807 case 0b11001110: /* FMOV top half of 128-bit */
6808 case 0b11001111:
6809 if (!fp_access_check(s)) {
6810 return;
6812 itof = opcode & 1;
6813 handle_fmov(s, rd, rn, type, itof);
6814 break;
6816 case 0b00111110: /* FJCVTZS */
6817 if (!dc_isar_feature(aa64_jscvt, s)) {
6818 goto do_unallocated;
6819 } else if (fp_access_check(s)) {
6820 handle_fjcvtzs(s, rd, rn);
6822 break;
6824 default:
6825 do_unallocated:
6826 unallocated_encoding(s);
6827 return;
6829 break;
6833 /* FP-specific subcases of table C3-6 (SIMD and FP data processing)
6834 * 31 30 29 28 25 24 0
6835 * +---+---+---+---------+-----------------------------+
6836 * | | 0 | | 1 1 1 1 | |
6837 * +---+---+---+---------+-----------------------------+
6839 static void disas_data_proc_fp(DisasContext *s, uint32_t insn)
6841 if (extract32(insn, 24, 1)) {
6842 /* Floating point data-processing (3 source) */
6843 disas_fp_3src(s, insn);
6844 } else if (extract32(insn, 21, 1) == 0) {
6845 /* Floating point to fixed point conversions */
6846 disas_fp_fixed_conv(s, insn);
6847 } else {
6848 switch (extract32(insn, 10, 2)) {
6849 case 1:
6850 /* Floating point conditional compare */
6851 disas_fp_ccomp(s, insn);
6852 break;
6853 case 2:
6854 /* Floating point data-processing (2 source) */
6855 disas_fp_2src(s, insn);
6856 break;
6857 case 3:
6858 /* Floating point conditional select */
6859 disas_fp_csel(s, insn);
6860 break;
6861 case 0:
6862 switch (ctz32(extract32(insn, 12, 4))) {
6863 case 0: /* [15:12] == xxx1 */
6864 /* Floating point immediate */
6865 disas_fp_imm(s, insn);
6866 break;
6867 case 1: /* [15:12] == xx10 */
6868 /* Floating point compare */
6869 disas_fp_compare(s, insn);
6870 break;
6871 case 2: /* [15:12] == x100 */
6872 /* Floating point data-processing (1 source) */
6873 disas_fp_1src(s, insn);
6874 break;
6875 case 3: /* [15:12] == 1000 */
6876 unallocated_encoding(s);
6877 break;
6878 default: /* [15:12] == 0000 */
6879 /* Floating point <-> integer conversions */
6880 disas_fp_int_conv(s, insn);
6881 break;
6883 break;
6888 static void do_ext64(DisasContext *s, TCGv_i64 tcg_left, TCGv_i64 tcg_right,
6889 int pos)
6891 /* Extract 64 bits from the middle of two concatenated 64 bit
6892 * vector register slices left:right. The extracted bits start
6893 * at 'pos' bits into the right (least significant) side.
6894 * We return the result in tcg_right, and guarantee not to
6895 * trash tcg_left.
6897 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
6898 assert(pos > 0 && pos < 64);
6900 tcg_gen_shri_i64(tcg_right, tcg_right, pos);
6901 tcg_gen_shli_i64(tcg_tmp, tcg_left, 64 - pos);
6902 tcg_gen_or_i64(tcg_right, tcg_right, tcg_tmp);
6904 tcg_temp_free_i64(tcg_tmp);
6907 /* EXT
6908 * 31 30 29 24 23 22 21 20 16 15 14 11 10 9 5 4 0
6909 * +---+---+-------------+-----+---+------+---+------+---+------+------+
6910 * | 0 | Q | 1 0 1 1 1 0 | op2 | 0 | Rm | 0 | imm4 | 0 | Rn | Rd |
6911 * +---+---+-------------+-----+---+------+---+------+---+------+------+
6913 static void disas_simd_ext(DisasContext *s, uint32_t insn)
6915 int is_q = extract32(insn, 30, 1);
6916 int op2 = extract32(insn, 22, 2);
6917 int imm4 = extract32(insn, 11, 4);
6918 int rm = extract32(insn, 16, 5);
6919 int rn = extract32(insn, 5, 5);
6920 int rd = extract32(insn, 0, 5);
6921 int pos = imm4 << 3;
6922 TCGv_i64 tcg_resl, tcg_resh;
6924 if (op2 != 0 || (!is_q && extract32(imm4, 3, 1))) {
6925 unallocated_encoding(s);
6926 return;
6929 if (!fp_access_check(s)) {
6930 return;
6933 tcg_resh = tcg_temp_new_i64();
6934 tcg_resl = tcg_temp_new_i64();
6936 /* Vd gets bits starting at pos bits into Vm:Vn. This is
6937 * either extracting 128 bits from a 128:128 concatenation, or
6938 * extracting 64 bits from a 64:64 concatenation.
6940 if (!is_q) {
6941 read_vec_element(s, tcg_resl, rn, 0, MO_64);
6942 if (pos != 0) {
6943 read_vec_element(s, tcg_resh, rm, 0, MO_64);
6944 do_ext64(s, tcg_resh, tcg_resl, pos);
6946 } else {
6947 TCGv_i64 tcg_hh;
6948 typedef struct {
6949 int reg;
6950 int elt;
6951 } EltPosns;
6952 EltPosns eltposns[] = { {rn, 0}, {rn, 1}, {rm, 0}, {rm, 1} };
6953 EltPosns *elt = eltposns;
6955 if (pos >= 64) {
6956 elt++;
6957 pos -= 64;
6960 read_vec_element(s, tcg_resl, elt->reg, elt->elt, MO_64);
6961 elt++;
6962 read_vec_element(s, tcg_resh, elt->reg, elt->elt, MO_64);
6963 elt++;
6964 if (pos != 0) {
6965 do_ext64(s, tcg_resh, tcg_resl, pos);
6966 tcg_hh = tcg_temp_new_i64();
6967 read_vec_element(s, tcg_hh, elt->reg, elt->elt, MO_64);
6968 do_ext64(s, tcg_hh, tcg_resh, pos);
6969 tcg_temp_free_i64(tcg_hh);
6973 write_vec_element(s, tcg_resl, rd, 0, MO_64);
6974 tcg_temp_free_i64(tcg_resl);
6975 if (is_q) {
6976 write_vec_element(s, tcg_resh, rd, 1, MO_64);
6978 tcg_temp_free_i64(tcg_resh);
6979 clear_vec_high(s, is_q, rd);
6982 /* TBL/TBX
6983 * 31 30 29 24 23 22 21 20 16 15 14 13 12 11 10 9 5 4 0
6984 * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
6985 * | 0 | Q | 0 0 1 1 1 0 | op2 | 0 | Rm | 0 | len | op | 0 0 | Rn | Rd |
6986 * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
6988 static void disas_simd_tb(DisasContext *s, uint32_t insn)
6990 int op2 = extract32(insn, 22, 2);
6991 int is_q = extract32(insn, 30, 1);
6992 int rm = extract32(insn, 16, 5);
6993 int rn = extract32(insn, 5, 5);
6994 int rd = extract32(insn, 0, 5);
6995 int is_tblx = extract32(insn, 12, 1);
6996 int len = extract32(insn, 13, 2);
6997 TCGv_i64 tcg_resl, tcg_resh, tcg_idx;
6998 TCGv_i32 tcg_regno, tcg_numregs;
7000 if (op2 != 0) {
7001 unallocated_encoding(s);
7002 return;
7005 if (!fp_access_check(s)) {
7006 return;
7009 /* This does a table lookup: for every byte element in the input
7010 * we index into a table formed from up to four vector registers,
7011 * and then the output is the result of the lookups. Our helper
7012 * function does the lookup operation for a single 64 bit part of
7013 * the input.
7015 tcg_resl = tcg_temp_new_i64();
7016 tcg_resh = NULL;
7018 if (is_tblx) {
7019 read_vec_element(s, tcg_resl, rd, 0, MO_64);
7020 } else {
7021 tcg_gen_movi_i64(tcg_resl, 0);
7024 if (is_q) {
7025 tcg_resh = tcg_temp_new_i64();
7026 if (is_tblx) {
7027 read_vec_element(s, tcg_resh, rd, 1, MO_64);
7028 } else {
7029 tcg_gen_movi_i64(tcg_resh, 0);
7033 tcg_idx = tcg_temp_new_i64();
7034 tcg_regno = tcg_const_i32(rn);
7035 tcg_numregs = tcg_const_i32(len + 1);
7036 read_vec_element(s, tcg_idx, rm, 0, MO_64);
7037 gen_helper_simd_tbl(tcg_resl, cpu_env, tcg_resl, tcg_idx,
7038 tcg_regno, tcg_numregs);
7039 if (is_q) {
7040 read_vec_element(s, tcg_idx, rm, 1, MO_64);
7041 gen_helper_simd_tbl(tcg_resh, cpu_env, tcg_resh, tcg_idx,
7042 tcg_regno, tcg_numregs);
7044 tcg_temp_free_i64(tcg_idx);
7045 tcg_temp_free_i32(tcg_regno);
7046 tcg_temp_free_i32(tcg_numregs);
7048 write_vec_element(s, tcg_resl, rd, 0, MO_64);
7049 tcg_temp_free_i64(tcg_resl);
7051 if (is_q) {
7052 write_vec_element(s, tcg_resh, rd, 1, MO_64);
7053 tcg_temp_free_i64(tcg_resh);
7055 clear_vec_high(s, is_q, rd);
7058 /* ZIP/UZP/TRN
7059 * 31 30 29 24 23 22 21 20 16 15 14 12 11 10 9 5 4 0
7060 * +---+---+-------------+------+---+------+---+------------------+------+
7061 * | 0 | Q | 0 0 1 1 1 0 | size | 0 | Rm | 0 | opc | 1 0 | Rn | Rd |
7062 * +---+---+-------------+------+---+------+---+------------------+------+
7064 static void disas_simd_zip_trn(DisasContext *s, uint32_t insn)
7066 int rd = extract32(insn, 0, 5);
7067 int rn = extract32(insn, 5, 5);
7068 int rm = extract32(insn, 16, 5);
7069 int size = extract32(insn, 22, 2);
7070 /* opc field bits [1:0] indicate ZIP/UZP/TRN;
7071 * bit 2 indicates 1 vs 2 variant of the insn.
7073 int opcode = extract32(insn, 12, 2);
7074 bool part = extract32(insn, 14, 1);
7075 bool is_q = extract32(insn, 30, 1);
7076 int esize = 8 << size;
7077 int i, ofs;
7078 int datasize = is_q ? 128 : 64;
7079 int elements = datasize / esize;
7080 TCGv_i64 tcg_res, tcg_resl, tcg_resh;
7082 if (opcode == 0 || (size == 3 && !is_q)) {
7083 unallocated_encoding(s);
7084 return;
7087 if (!fp_access_check(s)) {
7088 return;
7091 tcg_resl = tcg_const_i64(0);
7092 tcg_resh = is_q ? tcg_const_i64(0) : NULL;
7093 tcg_res = tcg_temp_new_i64();
7095 for (i = 0; i < elements; i++) {
7096 switch (opcode) {
7097 case 1: /* UZP1/2 */
7099 int midpoint = elements / 2;
7100 if (i < midpoint) {
7101 read_vec_element(s, tcg_res, rn, 2 * i + part, size);
7102 } else {
7103 read_vec_element(s, tcg_res, rm,
7104 2 * (i - midpoint) + part, size);
7106 break;
7108 case 2: /* TRN1/2 */
7109 if (i & 1) {
7110 read_vec_element(s, tcg_res, rm, (i & ~1) + part, size);
7111 } else {
7112 read_vec_element(s, tcg_res, rn, (i & ~1) + part, size);
7114 break;
7115 case 3: /* ZIP1/2 */
7117 int base = part * elements / 2;
7118 if (i & 1) {
7119 read_vec_element(s, tcg_res, rm, base + (i >> 1), size);
7120 } else {
7121 read_vec_element(s, tcg_res, rn, base + (i >> 1), size);
7123 break;
7125 default:
7126 g_assert_not_reached();
7129 ofs = i * esize;
7130 if (ofs < 64) {
7131 tcg_gen_shli_i64(tcg_res, tcg_res, ofs);
7132 tcg_gen_or_i64(tcg_resl, tcg_resl, tcg_res);
7133 } else {
7134 tcg_gen_shli_i64(tcg_res, tcg_res, ofs - 64);
7135 tcg_gen_or_i64(tcg_resh, tcg_resh, tcg_res);
7139 tcg_temp_free_i64(tcg_res);
7141 write_vec_element(s, tcg_resl, rd, 0, MO_64);
7142 tcg_temp_free_i64(tcg_resl);
7144 if (is_q) {
7145 write_vec_element(s, tcg_resh, rd, 1, MO_64);
7146 tcg_temp_free_i64(tcg_resh);
7148 clear_vec_high(s, is_q, rd);
7152 * do_reduction_op helper
7154 * This mirrors the Reduce() pseudocode in the ARM ARM. It is
7155 * important for correct NaN propagation that we do these
7156 * operations in exactly the order specified by the pseudocode.
7158 * This is a recursive function, TCG temps should be freed by the
7159 * calling function once it is done with the values.
7161 static TCGv_i32 do_reduction_op(DisasContext *s, int fpopcode, int rn,
7162 int esize, int size, int vmap, TCGv_ptr fpst)
7164 if (esize == size) {
7165 int element;
7166 MemOp msize = esize == 16 ? MO_16 : MO_32;
7167 TCGv_i32 tcg_elem;
7169 /* We should have one register left here */
7170 assert(ctpop8(vmap) == 1);
7171 element = ctz32(vmap);
7172 assert(element < 8);
7174 tcg_elem = tcg_temp_new_i32();
7175 read_vec_element_i32(s, tcg_elem, rn, element, msize);
7176 return tcg_elem;
7177 } else {
7178 int bits = size / 2;
7179 int shift = ctpop8(vmap) / 2;
7180 int vmap_lo = (vmap >> shift) & vmap;
7181 int vmap_hi = (vmap & ~vmap_lo);
7182 TCGv_i32 tcg_hi, tcg_lo, tcg_res;
7184 tcg_hi = do_reduction_op(s, fpopcode, rn, esize, bits, vmap_hi, fpst);
7185 tcg_lo = do_reduction_op(s, fpopcode, rn, esize, bits, vmap_lo, fpst);
7186 tcg_res = tcg_temp_new_i32();
7188 switch (fpopcode) {
7189 case 0x0c: /* fmaxnmv half-precision */
7190 gen_helper_advsimd_maxnumh(tcg_res, tcg_lo, tcg_hi, fpst);
7191 break;
7192 case 0x0f: /* fmaxv half-precision */
7193 gen_helper_advsimd_maxh(tcg_res, tcg_lo, tcg_hi, fpst);
7194 break;
7195 case 0x1c: /* fminnmv half-precision */
7196 gen_helper_advsimd_minnumh(tcg_res, tcg_lo, tcg_hi, fpst);
7197 break;
7198 case 0x1f: /* fminv half-precision */
7199 gen_helper_advsimd_minh(tcg_res, tcg_lo, tcg_hi, fpst);
7200 break;
7201 case 0x2c: /* fmaxnmv */
7202 gen_helper_vfp_maxnums(tcg_res, tcg_lo, tcg_hi, fpst);
7203 break;
7204 case 0x2f: /* fmaxv */
7205 gen_helper_vfp_maxs(tcg_res, tcg_lo, tcg_hi, fpst);
7206 break;
7207 case 0x3c: /* fminnmv */
7208 gen_helper_vfp_minnums(tcg_res, tcg_lo, tcg_hi, fpst);
7209 break;
7210 case 0x3f: /* fminv */
7211 gen_helper_vfp_mins(tcg_res, tcg_lo, tcg_hi, fpst);
7212 break;
7213 default:
7214 g_assert_not_reached();
7217 tcg_temp_free_i32(tcg_hi);
7218 tcg_temp_free_i32(tcg_lo);
7219 return tcg_res;
7223 /* AdvSIMD across lanes
7224 * 31 30 29 28 24 23 22 21 17 16 12 11 10 9 5 4 0
7225 * +---+---+---+-----------+------+-----------+--------+-----+------+------+
7226 * | 0 | Q | U | 0 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 | Rn | Rd |
7227 * +---+---+---+-----------+------+-----------+--------+-----+------+------+
7229 static void disas_simd_across_lanes(DisasContext *s, uint32_t insn)
7231 int rd = extract32(insn, 0, 5);
7232 int rn = extract32(insn, 5, 5);
7233 int size = extract32(insn, 22, 2);
7234 int opcode = extract32(insn, 12, 5);
7235 bool is_q = extract32(insn, 30, 1);
7236 bool is_u = extract32(insn, 29, 1);
7237 bool is_fp = false;
7238 bool is_min = false;
7239 int esize;
7240 int elements;
7241 int i;
7242 TCGv_i64 tcg_res, tcg_elt;
7244 switch (opcode) {
7245 case 0x1b: /* ADDV */
7246 if (is_u) {
7247 unallocated_encoding(s);
7248 return;
7250 /* fall through */
7251 case 0x3: /* SADDLV, UADDLV */
7252 case 0xa: /* SMAXV, UMAXV */
7253 case 0x1a: /* SMINV, UMINV */
7254 if (size == 3 || (size == 2 && !is_q)) {
7255 unallocated_encoding(s);
7256 return;
7258 break;
7259 case 0xc: /* FMAXNMV, FMINNMV */
7260 case 0xf: /* FMAXV, FMINV */
7261 /* Bit 1 of size field encodes min vs max and the actual size
7262 * depends on the encoding of the U bit. If not set (and FP16
7263 * enabled) then we do half-precision float instead of single
7264 * precision.
7266 is_min = extract32(size, 1, 1);
7267 is_fp = true;
7268 if (!is_u && dc_isar_feature(aa64_fp16, s)) {
7269 size = 1;
7270 } else if (!is_u || !is_q || extract32(size, 0, 1)) {
7271 unallocated_encoding(s);
7272 return;
7273 } else {
7274 size = 2;
7276 break;
7277 default:
7278 unallocated_encoding(s);
7279 return;
7282 if (!fp_access_check(s)) {
7283 return;
7286 esize = 8 << size;
7287 elements = (is_q ? 128 : 64) / esize;
7289 tcg_res = tcg_temp_new_i64();
7290 tcg_elt = tcg_temp_new_i64();
7292 /* These instructions operate across all lanes of a vector
7293 * to produce a single result. We can guarantee that a 64
7294 * bit intermediate is sufficient:
7295 * + for [US]ADDLV the maximum element size is 32 bits, and
7296 * the result type is 64 bits
7297 * + for FMAX*V, FMIN*V, ADDV the intermediate type is the
7298 * same as the element size, which is 32 bits at most
7299 * For the integer operations we can choose to work at 64
7300 * or 32 bits and truncate at the end; for simplicity
7301 * we use 64 bits always. The floating point
7302 * ops do require 32 bit intermediates, though.
7304 if (!is_fp) {
7305 read_vec_element(s, tcg_res, rn, 0, size | (is_u ? 0 : MO_SIGN));
7307 for (i = 1; i < elements; i++) {
7308 read_vec_element(s, tcg_elt, rn, i, size | (is_u ? 0 : MO_SIGN));
7310 switch (opcode) {
7311 case 0x03: /* SADDLV / UADDLV */
7312 case 0x1b: /* ADDV */
7313 tcg_gen_add_i64(tcg_res, tcg_res, tcg_elt);
7314 break;
7315 case 0x0a: /* SMAXV / UMAXV */
7316 if (is_u) {
7317 tcg_gen_umax_i64(tcg_res, tcg_res, tcg_elt);
7318 } else {
7319 tcg_gen_smax_i64(tcg_res, tcg_res, tcg_elt);
7321 break;
7322 case 0x1a: /* SMINV / UMINV */
7323 if (is_u) {
7324 tcg_gen_umin_i64(tcg_res, tcg_res, tcg_elt);
7325 } else {
7326 tcg_gen_smin_i64(tcg_res, tcg_res, tcg_elt);
7328 break;
7329 default:
7330 g_assert_not_reached();
7334 } else {
7335 /* Floating point vector reduction ops which work across 32
7336 * bit (single) or 16 bit (half-precision) intermediates.
7337 * Note that correct NaN propagation requires that we do these
7338 * operations in exactly the order specified by the pseudocode.
7340 TCGv_ptr fpst = get_fpstatus_ptr(size == MO_16);
7341 int fpopcode = opcode | is_min << 4 | is_u << 5;
7342 int vmap = (1 << elements) - 1;
7343 TCGv_i32 tcg_res32 = do_reduction_op(s, fpopcode, rn, esize,
7344 (is_q ? 128 : 64), vmap, fpst);
7345 tcg_gen_extu_i32_i64(tcg_res, tcg_res32);
7346 tcg_temp_free_i32(tcg_res32);
7347 tcg_temp_free_ptr(fpst);
7350 tcg_temp_free_i64(tcg_elt);
7352 /* Now truncate the result to the width required for the final output */
7353 if (opcode == 0x03) {
7354 /* SADDLV, UADDLV: result is 2*esize */
7355 size++;
7358 switch (size) {
7359 case 0:
7360 tcg_gen_ext8u_i64(tcg_res, tcg_res);
7361 break;
7362 case 1:
7363 tcg_gen_ext16u_i64(tcg_res, tcg_res);
7364 break;
7365 case 2:
7366 tcg_gen_ext32u_i64(tcg_res, tcg_res);
7367 break;
7368 case 3:
7369 break;
7370 default:
7371 g_assert_not_reached();
7374 write_fp_dreg(s, rd, tcg_res);
7375 tcg_temp_free_i64(tcg_res);
7378 /* DUP (Element, Vector)
7380 * 31 30 29 21 20 16 15 10 9 5 4 0
7381 * +---+---+-------------------+--------+-------------+------+------+
7382 * | 0 | Q | 0 0 1 1 1 0 0 0 0 | imm5 | 0 0 0 0 0 1 | Rn | Rd |
7383 * +---+---+-------------------+--------+-------------+------+------+
7385 * size: encoded in imm5 (see ARM ARM LowestSetBit())
7387 static void handle_simd_dupe(DisasContext *s, int is_q, int rd, int rn,
7388 int imm5)
7390 int size = ctz32(imm5);
7391 int index;
7393 if (size > 3 || (size == 3 && !is_q)) {
7394 unallocated_encoding(s);
7395 return;
7398 if (!fp_access_check(s)) {
7399 return;
7402 index = imm5 >> (size + 1);
7403 tcg_gen_gvec_dup_mem(size, vec_full_reg_offset(s, rd),
7404 vec_reg_offset(s, rn, index, size),
7405 is_q ? 16 : 8, vec_full_reg_size(s));
7408 /* DUP (element, scalar)
7409 * 31 21 20 16 15 10 9 5 4 0
7410 * +-----------------------+--------+-------------+------+------+
7411 * | 0 1 0 1 1 1 1 0 0 0 0 | imm5 | 0 0 0 0 0 1 | Rn | Rd |
7412 * +-----------------------+--------+-------------+------+------+
7414 static void handle_simd_dupes(DisasContext *s, int rd, int rn,
7415 int imm5)
7417 int size = ctz32(imm5);
7418 int index;
7419 TCGv_i64 tmp;
7421 if (size > 3) {
7422 unallocated_encoding(s);
7423 return;
7426 if (!fp_access_check(s)) {
7427 return;
7430 index = imm5 >> (size + 1);
7432 /* This instruction just extracts the specified element and
7433 * zero-extends it into the bottom of the destination register.
7435 tmp = tcg_temp_new_i64();
7436 read_vec_element(s, tmp, rn, index, size);
7437 write_fp_dreg(s, rd, tmp);
7438 tcg_temp_free_i64(tmp);
7441 /* DUP (General)
7443 * 31 30 29 21 20 16 15 10 9 5 4 0
7444 * +---+---+-------------------+--------+-------------+------+------+
7445 * | 0 | Q | 0 0 1 1 1 0 0 0 0 | imm5 | 0 0 0 0 1 1 | Rn | Rd |
7446 * +---+---+-------------------+--------+-------------+------+------+
7448 * size: encoded in imm5 (see ARM ARM LowestSetBit())
7450 static void handle_simd_dupg(DisasContext *s, int is_q, int rd, int rn,
7451 int imm5)
7453 int size = ctz32(imm5);
7454 uint32_t dofs, oprsz, maxsz;
7456 if (size > 3 || ((size == 3) && !is_q)) {
7457 unallocated_encoding(s);
7458 return;
7461 if (!fp_access_check(s)) {
7462 return;
7465 dofs = vec_full_reg_offset(s, rd);
7466 oprsz = is_q ? 16 : 8;
7467 maxsz = vec_full_reg_size(s);
7469 tcg_gen_gvec_dup_i64(size, dofs, oprsz, maxsz, cpu_reg(s, rn));
7472 /* INS (Element)
7474 * 31 21 20 16 15 14 11 10 9 5 4 0
7475 * +-----------------------+--------+------------+---+------+------+
7476 * | 0 1 1 0 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 | Rn | Rd |
7477 * +-----------------------+--------+------------+---+------+------+
7479 * size: encoded in imm5 (see ARM ARM LowestSetBit())
7480 * index: encoded in imm5<4:size+1>
7482 static void handle_simd_inse(DisasContext *s, int rd, int rn,
7483 int imm4, int imm5)
7485 int size = ctz32(imm5);
7486 int src_index, dst_index;
7487 TCGv_i64 tmp;
7489 if (size > 3) {
7490 unallocated_encoding(s);
7491 return;
7494 if (!fp_access_check(s)) {
7495 return;
7498 dst_index = extract32(imm5, 1+size, 5);
7499 src_index = extract32(imm4, size, 4);
7501 tmp = tcg_temp_new_i64();
7503 read_vec_element(s, tmp, rn, src_index, size);
7504 write_vec_element(s, tmp, rd, dst_index, size);
7506 tcg_temp_free_i64(tmp);
7508 /* INS is considered a 128-bit write for SVE. */
7509 clear_vec_high(s, true, rd);
7513 /* INS (General)
7515 * 31 21 20 16 15 10 9 5 4 0
7516 * +-----------------------+--------+-------------+------+------+
7517 * | 0 1 0 0 1 1 1 0 0 0 0 | imm5 | 0 0 0 1 1 1 | Rn | Rd |
7518 * +-----------------------+--------+-------------+------+------+
7520 * size: encoded in imm5 (see ARM ARM LowestSetBit())
7521 * index: encoded in imm5<4:size+1>
7523 static void handle_simd_insg(DisasContext *s, int rd, int rn, int imm5)
7525 int size = ctz32(imm5);
7526 int idx;
7528 if (size > 3) {
7529 unallocated_encoding(s);
7530 return;
7533 if (!fp_access_check(s)) {
7534 return;
7537 idx = extract32(imm5, 1 + size, 4 - size);
7538 write_vec_element(s, cpu_reg(s, rn), rd, idx, size);
7540 /* INS is considered a 128-bit write for SVE. */
7541 clear_vec_high(s, true, rd);
7545 * UMOV (General)
7546 * SMOV (General)
7548 * 31 30 29 21 20 16 15 12 10 9 5 4 0
7549 * +---+---+-------------------+--------+-------------+------+------+
7550 * | 0 | Q | 0 0 1 1 1 0 0 0 0 | imm5 | 0 0 1 U 1 1 | Rn | Rd |
7551 * +---+---+-------------------+--------+-------------+------+------+
7553 * U: unsigned when set
7554 * size: encoded in imm5 (see ARM ARM LowestSetBit())
7556 static void handle_simd_umov_smov(DisasContext *s, int is_q, int is_signed,
7557 int rn, int rd, int imm5)
7559 int size = ctz32(imm5);
7560 int element;
7561 TCGv_i64 tcg_rd;
7563 /* Check for UnallocatedEncodings */
7564 if (is_signed) {
7565 if (size > 2 || (size == 2 && !is_q)) {
7566 unallocated_encoding(s);
7567 return;
7569 } else {
7570 if (size > 3
7571 || (size < 3 && is_q)
7572 || (size == 3 && !is_q)) {
7573 unallocated_encoding(s);
7574 return;
7578 if (!fp_access_check(s)) {
7579 return;
7582 element = extract32(imm5, 1+size, 4);
7584 tcg_rd = cpu_reg(s, rd);
7585 read_vec_element(s, tcg_rd, rn, element, size | (is_signed ? MO_SIGN : 0));
7586 if (is_signed && !is_q) {
7587 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
7591 /* AdvSIMD copy
7592 * 31 30 29 28 21 20 16 15 14 11 10 9 5 4 0
7593 * +---+---+----+-----------------+------+---+------+---+------+------+
7594 * | 0 | Q | op | 0 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 | Rn | Rd |
7595 * +---+---+----+-----------------+------+---+------+---+------+------+
7597 static void disas_simd_copy(DisasContext *s, uint32_t insn)
7599 int rd = extract32(insn, 0, 5);
7600 int rn = extract32(insn, 5, 5);
7601 int imm4 = extract32(insn, 11, 4);
7602 int op = extract32(insn, 29, 1);
7603 int is_q = extract32(insn, 30, 1);
7604 int imm5 = extract32(insn, 16, 5);
7606 if (op) {
7607 if (is_q) {
7608 /* INS (element) */
7609 handle_simd_inse(s, rd, rn, imm4, imm5);
7610 } else {
7611 unallocated_encoding(s);
7613 } else {
7614 switch (imm4) {
7615 case 0:
7616 /* DUP (element - vector) */
7617 handle_simd_dupe(s, is_q, rd, rn, imm5);
7618 break;
7619 case 1:
7620 /* DUP (general) */
7621 handle_simd_dupg(s, is_q, rd, rn, imm5);
7622 break;
7623 case 3:
7624 if (is_q) {
7625 /* INS (general) */
7626 handle_simd_insg(s, rd, rn, imm5);
7627 } else {
7628 unallocated_encoding(s);
7630 break;
7631 case 5:
7632 case 7:
7633 /* UMOV/SMOV (is_q indicates 32/64; imm4 indicates signedness) */
7634 handle_simd_umov_smov(s, is_q, (imm4 == 5), rn, rd, imm5);
7635 break;
7636 default:
7637 unallocated_encoding(s);
7638 break;
7643 /* AdvSIMD modified immediate
7644 * 31 30 29 28 19 18 16 15 12 11 10 9 5 4 0
7645 * +---+---+----+---------------------+-----+-------+----+---+-------+------+
7646 * | 0 | Q | op | 0 1 1 1 1 0 0 0 0 0 | abc | cmode | o2 | 1 | defgh | Rd |
7647 * +---+---+----+---------------------+-----+-------+----+---+-------+------+
7649 * There are a number of operations that can be carried out here:
7650 * MOVI - move (shifted) imm into register
7651 * MVNI - move inverted (shifted) imm into register
7652 * ORR - bitwise OR of (shifted) imm with register
7653 * BIC - bitwise clear of (shifted) imm with register
7654 * With ARMv8.2 we also have:
7655 * FMOV half-precision
7657 static void disas_simd_mod_imm(DisasContext *s, uint32_t insn)
7659 int rd = extract32(insn, 0, 5);
7660 int cmode = extract32(insn, 12, 4);
7661 int cmode_3_1 = extract32(cmode, 1, 3);
7662 int cmode_0 = extract32(cmode, 0, 1);
7663 int o2 = extract32(insn, 11, 1);
7664 uint64_t abcdefgh = extract32(insn, 5, 5) | (extract32(insn, 16, 3) << 5);
7665 bool is_neg = extract32(insn, 29, 1);
7666 bool is_q = extract32(insn, 30, 1);
7667 uint64_t imm = 0;
7669 if (o2 != 0 || ((cmode == 0xf) && is_neg && !is_q)) {
7670 /* Check for FMOV (vector, immediate) - half-precision */
7671 if (!(dc_isar_feature(aa64_fp16, s) && o2 && cmode == 0xf)) {
7672 unallocated_encoding(s);
7673 return;
7677 if (!fp_access_check(s)) {
7678 return;
7681 /* See AdvSIMDExpandImm() in ARM ARM */
7682 switch (cmode_3_1) {
7683 case 0: /* Replicate(Zeros(24):imm8, 2) */
7684 case 1: /* Replicate(Zeros(16):imm8:Zeros(8), 2) */
7685 case 2: /* Replicate(Zeros(8):imm8:Zeros(16), 2) */
7686 case 3: /* Replicate(imm8:Zeros(24), 2) */
7688 int shift = cmode_3_1 * 8;
7689 imm = bitfield_replicate(abcdefgh << shift, 32);
7690 break;
7692 case 4: /* Replicate(Zeros(8):imm8, 4) */
7693 case 5: /* Replicate(imm8:Zeros(8), 4) */
7695 int shift = (cmode_3_1 & 0x1) * 8;
7696 imm = bitfield_replicate(abcdefgh << shift, 16);
7697 break;
7699 case 6:
7700 if (cmode_0) {
7701 /* Replicate(Zeros(8):imm8:Ones(16), 2) */
7702 imm = (abcdefgh << 16) | 0xffff;
7703 } else {
7704 /* Replicate(Zeros(16):imm8:Ones(8), 2) */
7705 imm = (abcdefgh << 8) | 0xff;
7707 imm = bitfield_replicate(imm, 32);
7708 break;
7709 case 7:
7710 if (!cmode_0 && !is_neg) {
7711 imm = bitfield_replicate(abcdefgh, 8);
7712 } else if (!cmode_0 && is_neg) {
7713 int i;
7714 imm = 0;
7715 for (i = 0; i < 8; i++) {
7716 if ((abcdefgh) & (1 << i)) {
7717 imm |= 0xffULL << (i * 8);
7720 } else if (cmode_0) {
7721 if (is_neg) {
7722 imm = (abcdefgh & 0x3f) << 48;
7723 if (abcdefgh & 0x80) {
7724 imm |= 0x8000000000000000ULL;
7726 if (abcdefgh & 0x40) {
7727 imm |= 0x3fc0000000000000ULL;
7728 } else {
7729 imm |= 0x4000000000000000ULL;
7731 } else {
7732 if (o2) {
7733 /* FMOV (vector, immediate) - half-precision */
7734 imm = vfp_expand_imm(MO_16, abcdefgh);
7735 /* now duplicate across the lanes */
7736 imm = bitfield_replicate(imm, 16);
7737 } else {
7738 imm = (abcdefgh & 0x3f) << 19;
7739 if (abcdefgh & 0x80) {
7740 imm |= 0x80000000;
7742 if (abcdefgh & 0x40) {
7743 imm |= 0x3e000000;
7744 } else {
7745 imm |= 0x40000000;
7747 imm |= (imm << 32);
7751 break;
7752 default:
7753 fprintf(stderr, "%s: cmode_3_1: %x\n", __func__, cmode_3_1);
7754 g_assert_not_reached();
7757 if (cmode_3_1 != 7 && is_neg) {
7758 imm = ~imm;
7761 if (!((cmode & 0x9) == 0x1 || (cmode & 0xd) == 0x9)) {
7762 /* MOVI or MVNI, with MVNI negation handled above. */
7763 tcg_gen_gvec_dup_imm(MO_64, vec_full_reg_offset(s, rd), is_q ? 16 : 8,
7764 vec_full_reg_size(s), imm);
7765 } else {
7766 /* ORR or BIC, with BIC negation to AND handled above. */
7767 if (is_neg) {
7768 gen_gvec_fn2i(s, is_q, rd, rd, imm, tcg_gen_gvec_andi, MO_64);
7769 } else {
7770 gen_gvec_fn2i(s, is_q, rd, rd, imm, tcg_gen_gvec_ori, MO_64);
7775 /* AdvSIMD scalar copy
7776 * 31 30 29 28 21 20 16 15 14 11 10 9 5 4 0
7777 * +-----+----+-----------------+------+---+------+---+------+------+
7778 * | 0 1 | op | 1 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 | Rn | Rd |
7779 * +-----+----+-----------------+------+---+------+---+------+------+
7781 static void disas_simd_scalar_copy(DisasContext *s, uint32_t insn)
7783 int rd = extract32(insn, 0, 5);
7784 int rn = extract32(insn, 5, 5);
7785 int imm4 = extract32(insn, 11, 4);
7786 int imm5 = extract32(insn, 16, 5);
7787 int op = extract32(insn, 29, 1);
7789 if (op != 0 || imm4 != 0) {
7790 unallocated_encoding(s);
7791 return;
7794 /* DUP (element, scalar) */
7795 handle_simd_dupes(s, rd, rn, imm5);
7798 /* AdvSIMD scalar pairwise
7799 * 31 30 29 28 24 23 22 21 17 16 12 11 10 9 5 4 0
7800 * +-----+---+-----------+------+-----------+--------+-----+------+------+
7801 * | 0 1 | U | 1 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 | Rn | Rd |
7802 * +-----+---+-----------+------+-----------+--------+-----+------+------+
7804 static void disas_simd_scalar_pairwise(DisasContext *s, uint32_t insn)
7806 int u = extract32(insn, 29, 1);
7807 int size = extract32(insn, 22, 2);
7808 int opcode = extract32(insn, 12, 5);
7809 int rn = extract32(insn, 5, 5);
7810 int rd = extract32(insn, 0, 5);
7811 TCGv_ptr fpst;
7813 /* For some ops (the FP ones), size[1] is part of the encoding.
7814 * For ADDP strictly it is not but size[1] is always 1 for valid
7815 * encodings.
7817 opcode |= (extract32(size, 1, 1) << 5);
7819 switch (opcode) {
7820 case 0x3b: /* ADDP */
7821 if (u || size != 3) {
7822 unallocated_encoding(s);
7823 return;
7825 if (!fp_access_check(s)) {
7826 return;
7829 fpst = NULL;
7830 break;
7831 case 0xc: /* FMAXNMP */
7832 case 0xd: /* FADDP */
7833 case 0xf: /* FMAXP */
7834 case 0x2c: /* FMINNMP */
7835 case 0x2f: /* FMINP */
7836 /* FP op, size[0] is 32 or 64 bit*/
7837 if (!u) {
7838 if (!dc_isar_feature(aa64_fp16, s)) {
7839 unallocated_encoding(s);
7840 return;
7841 } else {
7842 size = MO_16;
7844 } else {
7845 size = extract32(size, 0, 1) ? MO_64 : MO_32;
7848 if (!fp_access_check(s)) {
7849 return;
7852 fpst = get_fpstatus_ptr(size == MO_16);
7853 break;
7854 default:
7855 unallocated_encoding(s);
7856 return;
7859 if (size == MO_64) {
7860 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
7861 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
7862 TCGv_i64 tcg_res = tcg_temp_new_i64();
7864 read_vec_element(s, tcg_op1, rn, 0, MO_64);
7865 read_vec_element(s, tcg_op2, rn, 1, MO_64);
7867 switch (opcode) {
7868 case 0x3b: /* ADDP */
7869 tcg_gen_add_i64(tcg_res, tcg_op1, tcg_op2);
7870 break;
7871 case 0xc: /* FMAXNMP */
7872 gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
7873 break;
7874 case 0xd: /* FADDP */
7875 gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
7876 break;
7877 case 0xf: /* FMAXP */
7878 gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
7879 break;
7880 case 0x2c: /* FMINNMP */
7881 gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
7882 break;
7883 case 0x2f: /* FMINP */
7884 gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
7885 break;
7886 default:
7887 g_assert_not_reached();
7890 write_fp_dreg(s, rd, tcg_res);
7892 tcg_temp_free_i64(tcg_op1);
7893 tcg_temp_free_i64(tcg_op2);
7894 tcg_temp_free_i64(tcg_res);
7895 } else {
7896 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
7897 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
7898 TCGv_i32 tcg_res = tcg_temp_new_i32();
7900 read_vec_element_i32(s, tcg_op1, rn, 0, size);
7901 read_vec_element_i32(s, tcg_op2, rn, 1, size);
7903 if (size == MO_16) {
7904 switch (opcode) {
7905 case 0xc: /* FMAXNMP */
7906 gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
7907 break;
7908 case 0xd: /* FADDP */
7909 gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
7910 break;
7911 case 0xf: /* FMAXP */
7912 gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
7913 break;
7914 case 0x2c: /* FMINNMP */
7915 gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
7916 break;
7917 case 0x2f: /* FMINP */
7918 gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
7919 break;
7920 default:
7921 g_assert_not_reached();
7923 } else {
7924 switch (opcode) {
7925 case 0xc: /* FMAXNMP */
7926 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
7927 break;
7928 case 0xd: /* FADDP */
7929 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
7930 break;
7931 case 0xf: /* FMAXP */
7932 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
7933 break;
7934 case 0x2c: /* FMINNMP */
7935 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
7936 break;
7937 case 0x2f: /* FMINP */
7938 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
7939 break;
7940 default:
7941 g_assert_not_reached();
7945 write_fp_sreg(s, rd, tcg_res);
7947 tcg_temp_free_i32(tcg_op1);
7948 tcg_temp_free_i32(tcg_op2);
7949 tcg_temp_free_i32(tcg_res);
7952 if (fpst) {
7953 tcg_temp_free_ptr(fpst);
7958 * Common SSHR[RA]/USHR[RA] - Shift right (optional rounding/accumulate)
7960 * This code is handles the common shifting code and is used by both
7961 * the vector and scalar code.
7963 static void handle_shri_with_rndacc(TCGv_i64 tcg_res, TCGv_i64 tcg_src,
7964 TCGv_i64 tcg_rnd, bool accumulate,
7965 bool is_u, int size, int shift)
7967 bool extended_result = false;
7968 bool round = tcg_rnd != NULL;
7969 int ext_lshift = 0;
7970 TCGv_i64 tcg_src_hi;
7972 if (round && size == 3) {
7973 extended_result = true;
7974 ext_lshift = 64 - shift;
7975 tcg_src_hi = tcg_temp_new_i64();
7976 } else if (shift == 64) {
7977 if (!accumulate && is_u) {
7978 /* result is zero */
7979 tcg_gen_movi_i64(tcg_res, 0);
7980 return;
7984 /* Deal with the rounding step */
7985 if (round) {
7986 if (extended_result) {
7987 TCGv_i64 tcg_zero = tcg_const_i64(0);
7988 if (!is_u) {
7989 /* take care of sign extending tcg_res */
7990 tcg_gen_sari_i64(tcg_src_hi, tcg_src, 63);
7991 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
7992 tcg_src, tcg_src_hi,
7993 tcg_rnd, tcg_zero);
7994 } else {
7995 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
7996 tcg_src, tcg_zero,
7997 tcg_rnd, tcg_zero);
7999 tcg_temp_free_i64(tcg_zero);
8000 } else {
8001 tcg_gen_add_i64(tcg_src, tcg_src, tcg_rnd);
8005 /* Now do the shift right */
8006 if (round && extended_result) {
8007 /* extended case, >64 bit precision required */
8008 if (ext_lshift == 0) {
8009 /* special case, only high bits matter */
8010 tcg_gen_mov_i64(tcg_src, tcg_src_hi);
8011 } else {
8012 tcg_gen_shri_i64(tcg_src, tcg_src, shift);
8013 tcg_gen_shli_i64(tcg_src_hi, tcg_src_hi, ext_lshift);
8014 tcg_gen_or_i64(tcg_src, tcg_src, tcg_src_hi);
8016 } else {
8017 if (is_u) {
8018 if (shift == 64) {
8019 /* essentially shifting in 64 zeros */
8020 tcg_gen_movi_i64(tcg_src, 0);
8021 } else {
8022 tcg_gen_shri_i64(tcg_src, tcg_src, shift);
8024 } else {
8025 if (shift == 64) {
8026 /* effectively extending the sign-bit */
8027 tcg_gen_sari_i64(tcg_src, tcg_src, 63);
8028 } else {
8029 tcg_gen_sari_i64(tcg_src, tcg_src, shift);
8034 if (accumulate) {
8035 tcg_gen_add_i64(tcg_res, tcg_res, tcg_src);
8036 } else {
8037 tcg_gen_mov_i64(tcg_res, tcg_src);
8040 if (extended_result) {
8041 tcg_temp_free_i64(tcg_src_hi);
8045 /* SSHR[RA]/USHR[RA] - Scalar shift right (optional rounding/accumulate) */
8046 static void handle_scalar_simd_shri(DisasContext *s,
8047 bool is_u, int immh, int immb,
8048 int opcode, int rn, int rd)
8050 const int size = 3;
8051 int immhb = immh << 3 | immb;
8052 int shift = 2 * (8 << size) - immhb;
8053 bool accumulate = false;
8054 bool round = false;
8055 bool insert = false;
8056 TCGv_i64 tcg_rn;
8057 TCGv_i64 tcg_rd;
8058 TCGv_i64 tcg_round;
8060 if (!extract32(immh, 3, 1)) {
8061 unallocated_encoding(s);
8062 return;
8065 if (!fp_access_check(s)) {
8066 return;
8069 switch (opcode) {
8070 case 0x02: /* SSRA / USRA (accumulate) */
8071 accumulate = true;
8072 break;
8073 case 0x04: /* SRSHR / URSHR (rounding) */
8074 round = true;
8075 break;
8076 case 0x06: /* SRSRA / URSRA (accum + rounding) */
8077 accumulate = round = true;
8078 break;
8079 case 0x08: /* SRI */
8080 insert = true;
8081 break;
8084 if (round) {
8085 uint64_t round_const = 1ULL << (shift - 1);
8086 tcg_round = tcg_const_i64(round_const);
8087 } else {
8088 tcg_round = NULL;
8091 tcg_rn = read_fp_dreg(s, rn);
8092 tcg_rd = (accumulate || insert) ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
8094 if (insert) {
8095 /* shift count same as element size is valid but does nothing;
8096 * special case to avoid potential shift by 64.
8098 int esize = 8 << size;
8099 if (shift != esize) {
8100 tcg_gen_shri_i64(tcg_rn, tcg_rn, shift);
8101 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, 0, esize - shift);
8103 } else {
8104 handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
8105 accumulate, is_u, size, shift);
8108 write_fp_dreg(s, rd, tcg_rd);
8110 tcg_temp_free_i64(tcg_rn);
8111 tcg_temp_free_i64(tcg_rd);
8112 if (round) {
8113 tcg_temp_free_i64(tcg_round);
8117 /* SHL/SLI - Scalar shift left */
8118 static void handle_scalar_simd_shli(DisasContext *s, bool insert,
8119 int immh, int immb, int opcode,
8120 int rn, int rd)
8122 int size = 32 - clz32(immh) - 1;
8123 int immhb = immh << 3 | immb;
8124 int shift = immhb - (8 << size);
8125 TCGv_i64 tcg_rn = new_tmp_a64(s);
8126 TCGv_i64 tcg_rd = new_tmp_a64(s);
8128 if (!extract32(immh, 3, 1)) {
8129 unallocated_encoding(s);
8130 return;
8133 if (!fp_access_check(s)) {
8134 return;
8137 tcg_rn = read_fp_dreg(s, rn);
8138 tcg_rd = insert ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
8140 if (insert) {
8141 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, shift, 64 - shift);
8142 } else {
8143 tcg_gen_shli_i64(tcg_rd, tcg_rn, shift);
8146 write_fp_dreg(s, rd, tcg_rd);
8148 tcg_temp_free_i64(tcg_rn);
8149 tcg_temp_free_i64(tcg_rd);
8152 /* SQSHRN/SQSHRUN - Saturating (signed/unsigned) shift right with
8153 * (signed/unsigned) narrowing */
8154 static void handle_vec_simd_sqshrn(DisasContext *s, bool is_scalar, bool is_q,
8155 bool is_u_shift, bool is_u_narrow,
8156 int immh, int immb, int opcode,
8157 int rn, int rd)
8159 int immhb = immh << 3 | immb;
8160 int size = 32 - clz32(immh) - 1;
8161 int esize = 8 << size;
8162 int shift = (2 * esize) - immhb;
8163 int elements = is_scalar ? 1 : (64 / esize);
8164 bool round = extract32(opcode, 0, 1);
8165 MemOp ldop = (size + 1) | (is_u_shift ? 0 : MO_SIGN);
8166 TCGv_i64 tcg_rn, tcg_rd, tcg_round;
8167 TCGv_i32 tcg_rd_narrowed;
8168 TCGv_i64 tcg_final;
8170 static NeonGenNarrowEnvFn * const signed_narrow_fns[4][2] = {
8171 { gen_helper_neon_narrow_sat_s8,
8172 gen_helper_neon_unarrow_sat8 },
8173 { gen_helper_neon_narrow_sat_s16,
8174 gen_helper_neon_unarrow_sat16 },
8175 { gen_helper_neon_narrow_sat_s32,
8176 gen_helper_neon_unarrow_sat32 },
8177 { NULL, NULL },
8179 static NeonGenNarrowEnvFn * const unsigned_narrow_fns[4] = {
8180 gen_helper_neon_narrow_sat_u8,
8181 gen_helper_neon_narrow_sat_u16,
8182 gen_helper_neon_narrow_sat_u32,
8183 NULL
8185 NeonGenNarrowEnvFn *narrowfn;
8187 int i;
8189 assert(size < 4);
8191 if (extract32(immh, 3, 1)) {
8192 unallocated_encoding(s);
8193 return;
8196 if (!fp_access_check(s)) {
8197 return;
8200 if (is_u_shift) {
8201 narrowfn = unsigned_narrow_fns[size];
8202 } else {
8203 narrowfn = signed_narrow_fns[size][is_u_narrow ? 1 : 0];
8206 tcg_rn = tcg_temp_new_i64();
8207 tcg_rd = tcg_temp_new_i64();
8208 tcg_rd_narrowed = tcg_temp_new_i32();
8209 tcg_final = tcg_const_i64(0);
8211 if (round) {
8212 uint64_t round_const = 1ULL << (shift - 1);
8213 tcg_round = tcg_const_i64(round_const);
8214 } else {
8215 tcg_round = NULL;
8218 for (i = 0; i < elements; i++) {
8219 read_vec_element(s, tcg_rn, rn, i, ldop);
8220 handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
8221 false, is_u_shift, size+1, shift);
8222 narrowfn(tcg_rd_narrowed, cpu_env, tcg_rd);
8223 tcg_gen_extu_i32_i64(tcg_rd, tcg_rd_narrowed);
8224 tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
8227 if (!is_q) {
8228 write_vec_element(s, tcg_final, rd, 0, MO_64);
8229 } else {
8230 write_vec_element(s, tcg_final, rd, 1, MO_64);
8233 if (round) {
8234 tcg_temp_free_i64(tcg_round);
8236 tcg_temp_free_i64(tcg_rn);
8237 tcg_temp_free_i64(tcg_rd);
8238 tcg_temp_free_i32(tcg_rd_narrowed);
8239 tcg_temp_free_i64(tcg_final);
8241 clear_vec_high(s, is_q, rd);
8244 /* SQSHLU, UQSHL, SQSHL: saturating left shifts */
8245 static void handle_simd_qshl(DisasContext *s, bool scalar, bool is_q,
8246 bool src_unsigned, bool dst_unsigned,
8247 int immh, int immb, int rn, int rd)
8249 int immhb = immh << 3 | immb;
8250 int size = 32 - clz32(immh) - 1;
8251 int shift = immhb - (8 << size);
8252 int pass;
8254 assert(immh != 0);
8255 assert(!(scalar && is_q));
8257 if (!scalar) {
8258 if (!is_q && extract32(immh, 3, 1)) {
8259 unallocated_encoding(s);
8260 return;
8263 /* Since we use the variable-shift helpers we must
8264 * replicate the shift count into each element of
8265 * the tcg_shift value.
8267 switch (size) {
8268 case 0:
8269 shift |= shift << 8;
8270 /* fall through */
8271 case 1:
8272 shift |= shift << 16;
8273 break;
8274 case 2:
8275 case 3:
8276 break;
8277 default:
8278 g_assert_not_reached();
8282 if (!fp_access_check(s)) {
8283 return;
8286 if (size == 3) {
8287 TCGv_i64 tcg_shift = tcg_const_i64(shift);
8288 static NeonGenTwo64OpEnvFn * const fns[2][2] = {
8289 { gen_helper_neon_qshl_s64, gen_helper_neon_qshlu_s64 },
8290 { NULL, gen_helper_neon_qshl_u64 },
8292 NeonGenTwo64OpEnvFn *genfn = fns[src_unsigned][dst_unsigned];
8293 int maxpass = is_q ? 2 : 1;
8295 for (pass = 0; pass < maxpass; pass++) {
8296 TCGv_i64 tcg_op = tcg_temp_new_i64();
8298 read_vec_element(s, tcg_op, rn, pass, MO_64);
8299 genfn(tcg_op, cpu_env, tcg_op, tcg_shift);
8300 write_vec_element(s, tcg_op, rd, pass, MO_64);
8302 tcg_temp_free_i64(tcg_op);
8304 tcg_temp_free_i64(tcg_shift);
8305 clear_vec_high(s, is_q, rd);
8306 } else {
8307 TCGv_i32 tcg_shift = tcg_const_i32(shift);
8308 static NeonGenTwoOpEnvFn * const fns[2][2][3] = {
8310 { gen_helper_neon_qshl_s8,
8311 gen_helper_neon_qshl_s16,
8312 gen_helper_neon_qshl_s32 },
8313 { gen_helper_neon_qshlu_s8,
8314 gen_helper_neon_qshlu_s16,
8315 gen_helper_neon_qshlu_s32 }
8316 }, {
8317 { NULL, NULL, NULL },
8318 { gen_helper_neon_qshl_u8,
8319 gen_helper_neon_qshl_u16,
8320 gen_helper_neon_qshl_u32 }
8323 NeonGenTwoOpEnvFn *genfn = fns[src_unsigned][dst_unsigned][size];
8324 MemOp memop = scalar ? size : MO_32;
8325 int maxpass = scalar ? 1 : is_q ? 4 : 2;
8327 for (pass = 0; pass < maxpass; pass++) {
8328 TCGv_i32 tcg_op = tcg_temp_new_i32();
8330 read_vec_element_i32(s, tcg_op, rn, pass, memop);
8331 genfn(tcg_op, cpu_env, tcg_op, tcg_shift);
8332 if (scalar) {
8333 switch (size) {
8334 case 0:
8335 tcg_gen_ext8u_i32(tcg_op, tcg_op);
8336 break;
8337 case 1:
8338 tcg_gen_ext16u_i32(tcg_op, tcg_op);
8339 break;
8340 case 2:
8341 break;
8342 default:
8343 g_assert_not_reached();
8345 write_fp_sreg(s, rd, tcg_op);
8346 } else {
8347 write_vec_element_i32(s, tcg_op, rd, pass, MO_32);
8350 tcg_temp_free_i32(tcg_op);
8352 tcg_temp_free_i32(tcg_shift);
8354 if (!scalar) {
8355 clear_vec_high(s, is_q, rd);
8360 /* Common vector code for handling integer to FP conversion */
8361 static void handle_simd_intfp_conv(DisasContext *s, int rd, int rn,
8362 int elements, int is_signed,
8363 int fracbits, int size)
8365 TCGv_ptr tcg_fpst = get_fpstatus_ptr(size == MO_16);
8366 TCGv_i32 tcg_shift = NULL;
8368 MemOp mop = size | (is_signed ? MO_SIGN : 0);
8369 int pass;
8371 if (fracbits || size == MO_64) {
8372 tcg_shift = tcg_const_i32(fracbits);
8375 if (size == MO_64) {
8376 TCGv_i64 tcg_int64 = tcg_temp_new_i64();
8377 TCGv_i64 tcg_double = tcg_temp_new_i64();
8379 for (pass = 0; pass < elements; pass++) {
8380 read_vec_element(s, tcg_int64, rn, pass, mop);
8382 if (is_signed) {
8383 gen_helper_vfp_sqtod(tcg_double, tcg_int64,
8384 tcg_shift, tcg_fpst);
8385 } else {
8386 gen_helper_vfp_uqtod(tcg_double, tcg_int64,
8387 tcg_shift, tcg_fpst);
8389 if (elements == 1) {
8390 write_fp_dreg(s, rd, tcg_double);
8391 } else {
8392 write_vec_element(s, tcg_double, rd, pass, MO_64);
8396 tcg_temp_free_i64(tcg_int64);
8397 tcg_temp_free_i64(tcg_double);
8399 } else {
8400 TCGv_i32 tcg_int32 = tcg_temp_new_i32();
8401 TCGv_i32 tcg_float = tcg_temp_new_i32();
8403 for (pass = 0; pass < elements; pass++) {
8404 read_vec_element_i32(s, tcg_int32, rn, pass, mop);
8406 switch (size) {
8407 case MO_32:
8408 if (fracbits) {
8409 if (is_signed) {
8410 gen_helper_vfp_sltos(tcg_float, tcg_int32,
8411 tcg_shift, tcg_fpst);
8412 } else {
8413 gen_helper_vfp_ultos(tcg_float, tcg_int32,
8414 tcg_shift, tcg_fpst);
8416 } else {
8417 if (is_signed) {
8418 gen_helper_vfp_sitos(tcg_float, tcg_int32, tcg_fpst);
8419 } else {
8420 gen_helper_vfp_uitos(tcg_float, tcg_int32, tcg_fpst);
8423 break;
8424 case MO_16:
8425 if (fracbits) {
8426 if (is_signed) {
8427 gen_helper_vfp_sltoh(tcg_float, tcg_int32,
8428 tcg_shift, tcg_fpst);
8429 } else {
8430 gen_helper_vfp_ultoh(tcg_float, tcg_int32,
8431 tcg_shift, tcg_fpst);
8433 } else {
8434 if (is_signed) {
8435 gen_helper_vfp_sitoh(tcg_float, tcg_int32, tcg_fpst);
8436 } else {
8437 gen_helper_vfp_uitoh(tcg_float, tcg_int32, tcg_fpst);
8440 break;
8441 default:
8442 g_assert_not_reached();
8445 if (elements == 1) {
8446 write_fp_sreg(s, rd, tcg_float);
8447 } else {
8448 write_vec_element_i32(s, tcg_float, rd, pass, size);
8452 tcg_temp_free_i32(tcg_int32);
8453 tcg_temp_free_i32(tcg_float);
8456 tcg_temp_free_ptr(tcg_fpst);
8457 if (tcg_shift) {
8458 tcg_temp_free_i32(tcg_shift);
8461 clear_vec_high(s, elements << size == 16, rd);
8464 /* UCVTF/SCVTF - Integer to FP conversion */
8465 static void handle_simd_shift_intfp_conv(DisasContext *s, bool is_scalar,
8466 bool is_q, bool is_u,
8467 int immh, int immb, int opcode,
8468 int rn, int rd)
8470 int size, elements, fracbits;
8471 int immhb = immh << 3 | immb;
8473 if (immh & 8) {
8474 size = MO_64;
8475 if (!is_scalar && !is_q) {
8476 unallocated_encoding(s);
8477 return;
8479 } else if (immh & 4) {
8480 size = MO_32;
8481 } else if (immh & 2) {
8482 size = MO_16;
8483 if (!dc_isar_feature(aa64_fp16, s)) {
8484 unallocated_encoding(s);
8485 return;
8487 } else {
8488 /* immh == 0 would be a failure of the decode logic */
8489 g_assert(immh == 1);
8490 unallocated_encoding(s);
8491 return;
8494 if (is_scalar) {
8495 elements = 1;
8496 } else {
8497 elements = (8 << is_q) >> size;
8499 fracbits = (16 << size) - immhb;
8501 if (!fp_access_check(s)) {
8502 return;
8505 handle_simd_intfp_conv(s, rd, rn, elements, !is_u, fracbits, size);
8508 /* FCVTZS, FVCVTZU - FP to fixedpoint conversion */
8509 static void handle_simd_shift_fpint_conv(DisasContext *s, bool is_scalar,
8510 bool is_q, bool is_u,
8511 int immh, int immb, int rn, int rd)
8513 int immhb = immh << 3 | immb;
8514 int pass, size, fracbits;
8515 TCGv_ptr tcg_fpstatus;
8516 TCGv_i32 tcg_rmode, tcg_shift;
8518 if (immh & 0x8) {
8519 size = MO_64;
8520 if (!is_scalar && !is_q) {
8521 unallocated_encoding(s);
8522 return;
8524 } else if (immh & 0x4) {
8525 size = MO_32;
8526 } else if (immh & 0x2) {
8527 size = MO_16;
8528 if (!dc_isar_feature(aa64_fp16, s)) {
8529 unallocated_encoding(s);
8530 return;
8532 } else {
8533 /* Should have split out AdvSIMD modified immediate earlier. */
8534 assert(immh == 1);
8535 unallocated_encoding(s);
8536 return;
8539 if (!fp_access_check(s)) {
8540 return;
8543 assert(!(is_scalar && is_q));
8545 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(FPROUNDING_ZERO));
8546 tcg_fpstatus = get_fpstatus_ptr(size == MO_16);
8547 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
8548 fracbits = (16 << size) - immhb;
8549 tcg_shift = tcg_const_i32(fracbits);
8551 if (size == MO_64) {
8552 int maxpass = is_scalar ? 1 : 2;
8554 for (pass = 0; pass < maxpass; pass++) {
8555 TCGv_i64 tcg_op = tcg_temp_new_i64();
8557 read_vec_element(s, tcg_op, rn, pass, MO_64);
8558 if (is_u) {
8559 gen_helper_vfp_touqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
8560 } else {
8561 gen_helper_vfp_tosqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
8563 write_vec_element(s, tcg_op, rd, pass, MO_64);
8564 tcg_temp_free_i64(tcg_op);
8566 clear_vec_high(s, is_q, rd);
8567 } else {
8568 void (*fn)(TCGv_i32, TCGv_i32, TCGv_i32, TCGv_ptr);
8569 int maxpass = is_scalar ? 1 : ((8 << is_q) >> size);
8571 switch (size) {
8572 case MO_16:
8573 if (is_u) {
8574 fn = gen_helper_vfp_touhh;
8575 } else {
8576 fn = gen_helper_vfp_toshh;
8578 break;
8579 case MO_32:
8580 if (is_u) {
8581 fn = gen_helper_vfp_touls;
8582 } else {
8583 fn = gen_helper_vfp_tosls;
8585 break;
8586 default:
8587 g_assert_not_reached();
8590 for (pass = 0; pass < maxpass; pass++) {
8591 TCGv_i32 tcg_op = tcg_temp_new_i32();
8593 read_vec_element_i32(s, tcg_op, rn, pass, size);
8594 fn(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
8595 if (is_scalar) {
8596 write_fp_sreg(s, rd, tcg_op);
8597 } else {
8598 write_vec_element_i32(s, tcg_op, rd, pass, size);
8600 tcg_temp_free_i32(tcg_op);
8602 if (!is_scalar) {
8603 clear_vec_high(s, is_q, rd);
8607 tcg_temp_free_ptr(tcg_fpstatus);
8608 tcg_temp_free_i32(tcg_shift);
8609 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
8610 tcg_temp_free_i32(tcg_rmode);
8613 /* AdvSIMD scalar shift by immediate
8614 * 31 30 29 28 23 22 19 18 16 15 11 10 9 5 4 0
8615 * +-----+---+-------------+------+------+--------+---+------+------+
8616 * | 0 1 | U | 1 1 1 1 1 0 | immh | immb | opcode | 1 | Rn | Rd |
8617 * +-----+---+-------------+------+------+--------+---+------+------+
8619 * This is the scalar version so it works on a fixed sized registers
8621 static void disas_simd_scalar_shift_imm(DisasContext *s, uint32_t insn)
8623 int rd = extract32(insn, 0, 5);
8624 int rn = extract32(insn, 5, 5);
8625 int opcode = extract32(insn, 11, 5);
8626 int immb = extract32(insn, 16, 3);
8627 int immh = extract32(insn, 19, 4);
8628 bool is_u = extract32(insn, 29, 1);
8630 if (immh == 0) {
8631 unallocated_encoding(s);
8632 return;
8635 switch (opcode) {
8636 case 0x08: /* SRI */
8637 if (!is_u) {
8638 unallocated_encoding(s);
8639 return;
8641 /* fall through */
8642 case 0x00: /* SSHR / USHR */
8643 case 0x02: /* SSRA / USRA */
8644 case 0x04: /* SRSHR / URSHR */
8645 case 0x06: /* SRSRA / URSRA */
8646 handle_scalar_simd_shri(s, is_u, immh, immb, opcode, rn, rd);
8647 break;
8648 case 0x0a: /* SHL / SLI */
8649 handle_scalar_simd_shli(s, is_u, immh, immb, opcode, rn, rd);
8650 break;
8651 case 0x1c: /* SCVTF, UCVTF */
8652 handle_simd_shift_intfp_conv(s, true, false, is_u, immh, immb,
8653 opcode, rn, rd);
8654 break;
8655 case 0x10: /* SQSHRUN, SQSHRUN2 */
8656 case 0x11: /* SQRSHRUN, SQRSHRUN2 */
8657 if (!is_u) {
8658 unallocated_encoding(s);
8659 return;
8661 handle_vec_simd_sqshrn(s, true, false, false, true,
8662 immh, immb, opcode, rn, rd);
8663 break;
8664 case 0x12: /* SQSHRN, SQSHRN2, UQSHRN */
8665 case 0x13: /* SQRSHRN, SQRSHRN2, UQRSHRN, UQRSHRN2 */
8666 handle_vec_simd_sqshrn(s, true, false, is_u, is_u,
8667 immh, immb, opcode, rn, rd);
8668 break;
8669 case 0xc: /* SQSHLU */
8670 if (!is_u) {
8671 unallocated_encoding(s);
8672 return;
8674 handle_simd_qshl(s, true, false, false, true, immh, immb, rn, rd);
8675 break;
8676 case 0xe: /* SQSHL, UQSHL */
8677 handle_simd_qshl(s, true, false, is_u, is_u, immh, immb, rn, rd);
8678 break;
8679 case 0x1f: /* FCVTZS, FCVTZU */
8680 handle_simd_shift_fpint_conv(s, true, false, is_u, immh, immb, rn, rd);
8681 break;
8682 default:
8683 unallocated_encoding(s);
8684 break;
8688 /* AdvSIMD scalar three different
8689 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 0
8690 * +-----+---+-----------+------+---+------+--------+-----+------+------+
8691 * | 0 1 | U | 1 1 1 1 0 | size | 1 | Rm | opcode | 0 0 | Rn | Rd |
8692 * +-----+---+-----------+------+---+------+--------+-----+------+------+
8694 static void disas_simd_scalar_three_reg_diff(DisasContext *s, uint32_t insn)
8696 bool is_u = extract32(insn, 29, 1);
8697 int size = extract32(insn, 22, 2);
8698 int opcode = extract32(insn, 12, 4);
8699 int rm = extract32(insn, 16, 5);
8700 int rn = extract32(insn, 5, 5);
8701 int rd = extract32(insn, 0, 5);
8703 if (is_u) {
8704 unallocated_encoding(s);
8705 return;
8708 switch (opcode) {
8709 case 0x9: /* SQDMLAL, SQDMLAL2 */
8710 case 0xb: /* SQDMLSL, SQDMLSL2 */
8711 case 0xd: /* SQDMULL, SQDMULL2 */
8712 if (size == 0 || size == 3) {
8713 unallocated_encoding(s);
8714 return;
8716 break;
8717 default:
8718 unallocated_encoding(s);
8719 return;
8722 if (!fp_access_check(s)) {
8723 return;
8726 if (size == 2) {
8727 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8728 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
8729 TCGv_i64 tcg_res = tcg_temp_new_i64();
8731 read_vec_element(s, tcg_op1, rn, 0, MO_32 | MO_SIGN);
8732 read_vec_element(s, tcg_op2, rm, 0, MO_32 | MO_SIGN);
8734 tcg_gen_mul_i64(tcg_res, tcg_op1, tcg_op2);
8735 gen_helper_neon_addl_saturate_s64(tcg_res, cpu_env, tcg_res, tcg_res);
8737 switch (opcode) {
8738 case 0xd: /* SQDMULL, SQDMULL2 */
8739 break;
8740 case 0xb: /* SQDMLSL, SQDMLSL2 */
8741 tcg_gen_neg_i64(tcg_res, tcg_res);
8742 /* fall through */
8743 case 0x9: /* SQDMLAL, SQDMLAL2 */
8744 read_vec_element(s, tcg_op1, rd, 0, MO_64);
8745 gen_helper_neon_addl_saturate_s64(tcg_res, cpu_env,
8746 tcg_res, tcg_op1);
8747 break;
8748 default:
8749 g_assert_not_reached();
8752 write_fp_dreg(s, rd, tcg_res);
8754 tcg_temp_free_i64(tcg_op1);
8755 tcg_temp_free_i64(tcg_op2);
8756 tcg_temp_free_i64(tcg_res);
8757 } else {
8758 TCGv_i32 tcg_op1 = read_fp_hreg(s, rn);
8759 TCGv_i32 tcg_op2 = read_fp_hreg(s, rm);
8760 TCGv_i64 tcg_res = tcg_temp_new_i64();
8762 gen_helper_neon_mull_s16(tcg_res, tcg_op1, tcg_op2);
8763 gen_helper_neon_addl_saturate_s32(tcg_res, cpu_env, tcg_res, tcg_res);
8765 switch (opcode) {
8766 case 0xd: /* SQDMULL, SQDMULL2 */
8767 break;
8768 case 0xb: /* SQDMLSL, SQDMLSL2 */
8769 gen_helper_neon_negl_u32(tcg_res, tcg_res);
8770 /* fall through */
8771 case 0x9: /* SQDMLAL, SQDMLAL2 */
8773 TCGv_i64 tcg_op3 = tcg_temp_new_i64();
8774 read_vec_element(s, tcg_op3, rd, 0, MO_32);
8775 gen_helper_neon_addl_saturate_s32(tcg_res, cpu_env,
8776 tcg_res, tcg_op3);
8777 tcg_temp_free_i64(tcg_op3);
8778 break;
8780 default:
8781 g_assert_not_reached();
8784 tcg_gen_ext32u_i64(tcg_res, tcg_res);
8785 write_fp_dreg(s, rd, tcg_res);
8787 tcg_temp_free_i32(tcg_op1);
8788 tcg_temp_free_i32(tcg_op2);
8789 tcg_temp_free_i64(tcg_res);
8793 static void handle_3same_64(DisasContext *s, int opcode, bool u,
8794 TCGv_i64 tcg_rd, TCGv_i64 tcg_rn, TCGv_i64 tcg_rm)
8796 /* Handle 64x64->64 opcodes which are shared between the scalar
8797 * and vector 3-same groups. We cover every opcode where size == 3
8798 * is valid in either the three-reg-same (integer, not pairwise)
8799 * or scalar-three-reg-same groups.
8801 TCGCond cond;
8803 switch (opcode) {
8804 case 0x1: /* SQADD */
8805 if (u) {
8806 gen_helper_neon_qadd_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
8807 } else {
8808 gen_helper_neon_qadd_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
8810 break;
8811 case 0x5: /* SQSUB */
8812 if (u) {
8813 gen_helper_neon_qsub_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
8814 } else {
8815 gen_helper_neon_qsub_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
8817 break;
8818 case 0x6: /* CMGT, CMHI */
8819 /* 64 bit integer comparison, result = test ? (2^64 - 1) : 0.
8820 * We implement this using setcond (test) and then negating.
8822 cond = u ? TCG_COND_GTU : TCG_COND_GT;
8823 do_cmop:
8824 tcg_gen_setcond_i64(cond, tcg_rd, tcg_rn, tcg_rm);
8825 tcg_gen_neg_i64(tcg_rd, tcg_rd);
8826 break;
8827 case 0x7: /* CMGE, CMHS */
8828 cond = u ? TCG_COND_GEU : TCG_COND_GE;
8829 goto do_cmop;
8830 case 0x11: /* CMTST, CMEQ */
8831 if (u) {
8832 cond = TCG_COND_EQ;
8833 goto do_cmop;
8835 gen_cmtst_i64(tcg_rd, tcg_rn, tcg_rm);
8836 break;
8837 case 0x8: /* SSHL, USHL */
8838 if (u) {
8839 gen_ushl_i64(tcg_rd, tcg_rn, tcg_rm);
8840 } else {
8841 gen_sshl_i64(tcg_rd, tcg_rn, tcg_rm);
8843 break;
8844 case 0x9: /* SQSHL, UQSHL */
8845 if (u) {
8846 gen_helper_neon_qshl_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
8847 } else {
8848 gen_helper_neon_qshl_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
8850 break;
8851 case 0xa: /* SRSHL, URSHL */
8852 if (u) {
8853 gen_helper_neon_rshl_u64(tcg_rd, tcg_rn, tcg_rm);
8854 } else {
8855 gen_helper_neon_rshl_s64(tcg_rd, tcg_rn, tcg_rm);
8857 break;
8858 case 0xb: /* SQRSHL, UQRSHL */
8859 if (u) {
8860 gen_helper_neon_qrshl_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
8861 } else {
8862 gen_helper_neon_qrshl_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
8864 break;
8865 case 0x10: /* ADD, SUB */
8866 if (u) {
8867 tcg_gen_sub_i64(tcg_rd, tcg_rn, tcg_rm);
8868 } else {
8869 tcg_gen_add_i64(tcg_rd, tcg_rn, tcg_rm);
8871 break;
8872 default:
8873 g_assert_not_reached();
8877 /* Handle the 3-same-operands float operations; shared by the scalar
8878 * and vector encodings. The caller must filter out any encodings
8879 * not allocated for the encoding it is dealing with.
8881 static void handle_3same_float(DisasContext *s, int size, int elements,
8882 int fpopcode, int rd, int rn, int rm)
8884 int pass;
8885 TCGv_ptr fpst = get_fpstatus_ptr(false);
8887 for (pass = 0; pass < elements; pass++) {
8888 if (size) {
8889 /* Double */
8890 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8891 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
8892 TCGv_i64 tcg_res = tcg_temp_new_i64();
8894 read_vec_element(s, tcg_op1, rn, pass, MO_64);
8895 read_vec_element(s, tcg_op2, rm, pass, MO_64);
8897 switch (fpopcode) {
8898 case 0x39: /* FMLS */
8899 /* As usual for ARM, separate negation for fused multiply-add */
8900 gen_helper_vfp_negd(tcg_op1, tcg_op1);
8901 /* fall through */
8902 case 0x19: /* FMLA */
8903 read_vec_element(s, tcg_res, rd, pass, MO_64);
8904 gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2,
8905 tcg_res, fpst);
8906 break;
8907 case 0x18: /* FMAXNM */
8908 gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
8909 break;
8910 case 0x1a: /* FADD */
8911 gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
8912 break;
8913 case 0x1b: /* FMULX */
8914 gen_helper_vfp_mulxd(tcg_res, tcg_op1, tcg_op2, fpst);
8915 break;
8916 case 0x1c: /* FCMEQ */
8917 gen_helper_neon_ceq_f64(tcg_res, tcg_op1, tcg_op2, fpst);
8918 break;
8919 case 0x1e: /* FMAX */
8920 gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
8921 break;
8922 case 0x1f: /* FRECPS */
8923 gen_helper_recpsf_f64(tcg_res, tcg_op1, tcg_op2, fpst);
8924 break;
8925 case 0x38: /* FMINNM */
8926 gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
8927 break;
8928 case 0x3a: /* FSUB */
8929 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
8930 break;
8931 case 0x3e: /* FMIN */
8932 gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
8933 break;
8934 case 0x3f: /* FRSQRTS */
8935 gen_helper_rsqrtsf_f64(tcg_res, tcg_op1, tcg_op2, fpst);
8936 break;
8937 case 0x5b: /* FMUL */
8938 gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
8939 break;
8940 case 0x5c: /* FCMGE */
8941 gen_helper_neon_cge_f64(tcg_res, tcg_op1, tcg_op2, fpst);
8942 break;
8943 case 0x5d: /* FACGE */
8944 gen_helper_neon_acge_f64(tcg_res, tcg_op1, tcg_op2, fpst);
8945 break;
8946 case 0x5f: /* FDIV */
8947 gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
8948 break;
8949 case 0x7a: /* FABD */
8950 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
8951 gen_helper_vfp_absd(tcg_res, tcg_res);
8952 break;
8953 case 0x7c: /* FCMGT */
8954 gen_helper_neon_cgt_f64(tcg_res, tcg_op1, tcg_op2, fpst);
8955 break;
8956 case 0x7d: /* FACGT */
8957 gen_helper_neon_acgt_f64(tcg_res, tcg_op1, tcg_op2, fpst);
8958 break;
8959 default:
8960 g_assert_not_reached();
8963 write_vec_element(s, tcg_res, rd, pass, MO_64);
8965 tcg_temp_free_i64(tcg_res);
8966 tcg_temp_free_i64(tcg_op1);
8967 tcg_temp_free_i64(tcg_op2);
8968 } else {
8969 /* Single */
8970 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
8971 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
8972 TCGv_i32 tcg_res = tcg_temp_new_i32();
8974 read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
8975 read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
8977 switch (fpopcode) {
8978 case 0x39: /* FMLS */
8979 /* As usual for ARM, separate negation for fused multiply-add */
8980 gen_helper_vfp_negs(tcg_op1, tcg_op1);
8981 /* fall through */
8982 case 0x19: /* FMLA */
8983 read_vec_element_i32(s, tcg_res, rd, pass, MO_32);
8984 gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2,
8985 tcg_res, fpst);
8986 break;
8987 case 0x1a: /* FADD */
8988 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
8989 break;
8990 case 0x1b: /* FMULX */
8991 gen_helper_vfp_mulxs(tcg_res, tcg_op1, tcg_op2, fpst);
8992 break;
8993 case 0x1c: /* FCMEQ */
8994 gen_helper_neon_ceq_f32(tcg_res, tcg_op1, tcg_op2, fpst);
8995 break;
8996 case 0x1e: /* FMAX */
8997 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
8998 break;
8999 case 0x1f: /* FRECPS */
9000 gen_helper_recpsf_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9001 break;
9002 case 0x18: /* FMAXNM */
9003 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
9004 break;
9005 case 0x38: /* FMINNM */
9006 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
9007 break;
9008 case 0x3a: /* FSUB */
9009 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
9010 break;
9011 case 0x3e: /* FMIN */
9012 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
9013 break;
9014 case 0x3f: /* FRSQRTS */
9015 gen_helper_rsqrtsf_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9016 break;
9017 case 0x5b: /* FMUL */
9018 gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
9019 break;
9020 case 0x5c: /* FCMGE */
9021 gen_helper_neon_cge_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9022 break;
9023 case 0x5d: /* FACGE */
9024 gen_helper_neon_acge_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9025 break;
9026 case 0x5f: /* FDIV */
9027 gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
9028 break;
9029 case 0x7a: /* FABD */
9030 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
9031 gen_helper_vfp_abss(tcg_res, tcg_res);
9032 break;
9033 case 0x7c: /* FCMGT */
9034 gen_helper_neon_cgt_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9035 break;
9036 case 0x7d: /* FACGT */
9037 gen_helper_neon_acgt_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9038 break;
9039 default:
9040 g_assert_not_reached();
9043 if (elements == 1) {
9044 /* scalar single so clear high part */
9045 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
9047 tcg_gen_extu_i32_i64(tcg_tmp, tcg_res);
9048 write_vec_element(s, tcg_tmp, rd, pass, MO_64);
9049 tcg_temp_free_i64(tcg_tmp);
9050 } else {
9051 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
9054 tcg_temp_free_i32(tcg_res);
9055 tcg_temp_free_i32(tcg_op1);
9056 tcg_temp_free_i32(tcg_op2);
9060 tcg_temp_free_ptr(fpst);
9062 clear_vec_high(s, elements * (size ? 8 : 4) > 8, rd);
9065 /* AdvSIMD scalar three same
9066 * 31 30 29 28 24 23 22 21 20 16 15 11 10 9 5 4 0
9067 * +-----+---+-----------+------+---+------+--------+---+------+------+
9068 * | 0 1 | U | 1 1 1 1 0 | size | 1 | Rm | opcode | 1 | Rn | Rd |
9069 * +-----+---+-----------+------+---+------+--------+---+------+------+
9071 static void disas_simd_scalar_three_reg_same(DisasContext *s, uint32_t insn)
9073 int rd = extract32(insn, 0, 5);
9074 int rn = extract32(insn, 5, 5);
9075 int opcode = extract32(insn, 11, 5);
9076 int rm = extract32(insn, 16, 5);
9077 int size = extract32(insn, 22, 2);
9078 bool u = extract32(insn, 29, 1);
9079 TCGv_i64 tcg_rd;
9081 if (opcode >= 0x18) {
9082 /* Floating point: U, size[1] and opcode indicate operation */
9083 int fpopcode = opcode | (extract32(size, 1, 1) << 5) | (u << 6);
9084 switch (fpopcode) {
9085 case 0x1b: /* FMULX */
9086 case 0x1f: /* FRECPS */
9087 case 0x3f: /* FRSQRTS */
9088 case 0x5d: /* FACGE */
9089 case 0x7d: /* FACGT */
9090 case 0x1c: /* FCMEQ */
9091 case 0x5c: /* FCMGE */
9092 case 0x7c: /* FCMGT */
9093 case 0x7a: /* FABD */
9094 break;
9095 default:
9096 unallocated_encoding(s);
9097 return;
9100 if (!fp_access_check(s)) {
9101 return;
9104 handle_3same_float(s, extract32(size, 0, 1), 1, fpopcode, rd, rn, rm);
9105 return;
9108 switch (opcode) {
9109 case 0x1: /* SQADD, UQADD */
9110 case 0x5: /* SQSUB, UQSUB */
9111 case 0x9: /* SQSHL, UQSHL */
9112 case 0xb: /* SQRSHL, UQRSHL */
9113 break;
9114 case 0x8: /* SSHL, USHL */
9115 case 0xa: /* SRSHL, URSHL */
9116 case 0x6: /* CMGT, CMHI */
9117 case 0x7: /* CMGE, CMHS */
9118 case 0x11: /* CMTST, CMEQ */
9119 case 0x10: /* ADD, SUB (vector) */
9120 if (size != 3) {
9121 unallocated_encoding(s);
9122 return;
9124 break;
9125 case 0x16: /* SQDMULH, SQRDMULH (vector) */
9126 if (size != 1 && size != 2) {
9127 unallocated_encoding(s);
9128 return;
9130 break;
9131 default:
9132 unallocated_encoding(s);
9133 return;
9136 if (!fp_access_check(s)) {
9137 return;
9140 tcg_rd = tcg_temp_new_i64();
9142 if (size == 3) {
9143 TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
9144 TCGv_i64 tcg_rm = read_fp_dreg(s, rm);
9146 handle_3same_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rm);
9147 tcg_temp_free_i64(tcg_rn);
9148 tcg_temp_free_i64(tcg_rm);
9149 } else {
9150 /* Do a single operation on the lowest element in the vector.
9151 * We use the standard Neon helpers and rely on 0 OP 0 == 0 with
9152 * no side effects for all these operations.
9153 * OPTME: special-purpose helpers would avoid doing some
9154 * unnecessary work in the helper for the 8 and 16 bit cases.
9156 NeonGenTwoOpEnvFn *genenvfn;
9157 TCGv_i32 tcg_rn = tcg_temp_new_i32();
9158 TCGv_i32 tcg_rm = tcg_temp_new_i32();
9159 TCGv_i32 tcg_rd32 = tcg_temp_new_i32();
9161 read_vec_element_i32(s, tcg_rn, rn, 0, size);
9162 read_vec_element_i32(s, tcg_rm, rm, 0, size);
9164 switch (opcode) {
9165 case 0x1: /* SQADD, UQADD */
9167 static NeonGenTwoOpEnvFn * const fns[3][2] = {
9168 { gen_helper_neon_qadd_s8, gen_helper_neon_qadd_u8 },
9169 { gen_helper_neon_qadd_s16, gen_helper_neon_qadd_u16 },
9170 { gen_helper_neon_qadd_s32, gen_helper_neon_qadd_u32 },
9172 genenvfn = fns[size][u];
9173 break;
9175 case 0x5: /* SQSUB, UQSUB */
9177 static NeonGenTwoOpEnvFn * const fns[3][2] = {
9178 { gen_helper_neon_qsub_s8, gen_helper_neon_qsub_u8 },
9179 { gen_helper_neon_qsub_s16, gen_helper_neon_qsub_u16 },
9180 { gen_helper_neon_qsub_s32, gen_helper_neon_qsub_u32 },
9182 genenvfn = fns[size][u];
9183 break;
9185 case 0x9: /* SQSHL, UQSHL */
9187 static NeonGenTwoOpEnvFn * const fns[3][2] = {
9188 { gen_helper_neon_qshl_s8, gen_helper_neon_qshl_u8 },
9189 { gen_helper_neon_qshl_s16, gen_helper_neon_qshl_u16 },
9190 { gen_helper_neon_qshl_s32, gen_helper_neon_qshl_u32 },
9192 genenvfn = fns[size][u];
9193 break;
9195 case 0xb: /* SQRSHL, UQRSHL */
9197 static NeonGenTwoOpEnvFn * const fns[3][2] = {
9198 { gen_helper_neon_qrshl_s8, gen_helper_neon_qrshl_u8 },
9199 { gen_helper_neon_qrshl_s16, gen_helper_neon_qrshl_u16 },
9200 { gen_helper_neon_qrshl_s32, gen_helper_neon_qrshl_u32 },
9202 genenvfn = fns[size][u];
9203 break;
9205 case 0x16: /* SQDMULH, SQRDMULH */
9207 static NeonGenTwoOpEnvFn * const fns[2][2] = {
9208 { gen_helper_neon_qdmulh_s16, gen_helper_neon_qrdmulh_s16 },
9209 { gen_helper_neon_qdmulh_s32, gen_helper_neon_qrdmulh_s32 },
9211 assert(size == 1 || size == 2);
9212 genenvfn = fns[size - 1][u];
9213 break;
9215 default:
9216 g_assert_not_reached();
9219 genenvfn(tcg_rd32, cpu_env, tcg_rn, tcg_rm);
9220 tcg_gen_extu_i32_i64(tcg_rd, tcg_rd32);
9221 tcg_temp_free_i32(tcg_rd32);
9222 tcg_temp_free_i32(tcg_rn);
9223 tcg_temp_free_i32(tcg_rm);
9226 write_fp_dreg(s, rd, tcg_rd);
9228 tcg_temp_free_i64(tcg_rd);
9231 /* AdvSIMD scalar three same FP16
9232 * 31 30 29 28 24 23 22 21 20 16 15 14 13 11 10 9 5 4 0
9233 * +-----+---+-----------+---+-----+------+-----+--------+---+----+----+
9234 * | 0 1 | U | 1 1 1 1 0 | a | 1 0 | Rm | 0 0 | opcode | 1 | Rn | Rd |
9235 * +-----+---+-----------+---+-----+------+-----+--------+---+----+----+
9236 * v: 0101 1110 0100 0000 0000 0100 0000 0000 => 5e400400
9237 * m: 1101 1111 0110 0000 1100 0100 0000 0000 => df60c400
9239 static void disas_simd_scalar_three_reg_same_fp16(DisasContext *s,
9240 uint32_t insn)
9242 int rd = extract32(insn, 0, 5);
9243 int rn = extract32(insn, 5, 5);
9244 int opcode = extract32(insn, 11, 3);
9245 int rm = extract32(insn, 16, 5);
9246 bool u = extract32(insn, 29, 1);
9247 bool a = extract32(insn, 23, 1);
9248 int fpopcode = opcode | (a << 3) | (u << 4);
9249 TCGv_ptr fpst;
9250 TCGv_i32 tcg_op1;
9251 TCGv_i32 tcg_op2;
9252 TCGv_i32 tcg_res;
9254 switch (fpopcode) {
9255 case 0x03: /* FMULX */
9256 case 0x04: /* FCMEQ (reg) */
9257 case 0x07: /* FRECPS */
9258 case 0x0f: /* FRSQRTS */
9259 case 0x14: /* FCMGE (reg) */
9260 case 0x15: /* FACGE */
9261 case 0x1a: /* FABD */
9262 case 0x1c: /* FCMGT (reg) */
9263 case 0x1d: /* FACGT */
9264 break;
9265 default:
9266 unallocated_encoding(s);
9267 return;
9270 if (!dc_isar_feature(aa64_fp16, s)) {
9271 unallocated_encoding(s);
9274 if (!fp_access_check(s)) {
9275 return;
9278 fpst = get_fpstatus_ptr(true);
9280 tcg_op1 = read_fp_hreg(s, rn);
9281 tcg_op2 = read_fp_hreg(s, rm);
9282 tcg_res = tcg_temp_new_i32();
9284 switch (fpopcode) {
9285 case 0x03: /* FMULX */
9286 gen_helper_advsimd_mulxh(tcg_res, tcg_op1, tcg_op2, fpst);
9287 break;
9288 case 0x04: /* FCMEQ (reg) */
9289 gen_helper_advsimd_ceq_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9290 break;
9291 case 0x07: /* FRECPS */
9292 gen_helper_recpsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9293 break;
9294 case 0x0f: /* FRSQRTS */
9295 gen_helper_rsqrtsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9296 break;
9297 case 0x14: /* FCMGE (reg) */
9298 gen_helper_advsimd_cge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9299 break;
9300 case 0x15: /* FACGE */
9301 gen_helper_advsimd_acge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9302 break;
9303 case 0x1a: /* FABD */
9304 gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
9305 tcg_gen_andi_i32(tcg_res, tcg_res, 0x7fff);
9306 break;
9307 case 0x1c: /* FCMGT (reg) */
9308 gen_helper_advsimd_cgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9309 break;
9310 case 0x1d: /* FACGT */
9311 gen_helper_advsimd_acgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9312 break;
9313 default:
9314 g_assert_not_reached();
9317 write_fp_sreg(s, rd, tcg_res);
9320 tcg_temp_free_i32(tcg_res);
9321 tcg_temp_free_i32(tcg_op1);
9322 tcg_temp_free_i32(tcg_op2);
9323 tcg_temp_free_ptr(fpst);
9326 /* AdvSIMD scalar three same extra
9327 * 31 30 29 28 24 23 22 21 20 16 15 14 11 10 9 5 4 0
9328 * +-----+---+-----------+------+---+------+---+--------+---+----+----+
9329 * | 0 1 | U | 1 1 1 1 0 | size | 0 | Rm | 1 | opcode | 1 | Rn | Rd |
9330 * +-----+---+-----------+------+---+------+---+--------+---+----+----+
9332 static void disas_simd_scalar_three_reg_same_extra(DisasContext *s,
9333 uint32_t insn)
9335 int rd = extract32(insn, 0, 5);
9336 int rn = extract32(insn, 5, 5);
9337 int opcode = extract32(insn, 11, 4);
9338 int rm = extract32(insn, 16, 5);
9339 int size = extract32(insn, 22, 2);
9340 bool u = extract32(insn, 29, 1);
9341 TCGv_i32 ele1, ele2, ele3;
9342 TCGv_i64 res;
9343 bool feature;
9345 switch (u * 16 + opcode) {
9346 case 0x10: /* SQRDMLAH (vector) */
9347 case 0x11: /* SQRDMLSH (vector) */
9348 if (size != 1 && size != 2) {
9349 unallocated_encoding(s);
9350 return;
9352 feature = dc_isar_feature(aa64_rdm, s);
9353 break;
9354 default:
9355 unallocated_encoding(s);
9356 return;
9358 if (!feature) {
9359 unallocated_encoding(s);
9360 return;
9362 if (!fp_access_check(s)) {
9363 return;
9366 /* Do a single operation on the lowest element in the vector.
9367 * We use the standard Neon helpers and rely on 0 OP 0 == 0
9368 * with no side effects for all these operations.
9369 * OPTME: special-purpose helpers would avoid doing some
9370 * unnecessary work in the helper for the 16 bit cases.
9372 ele1 = tcg_temp_new_i32();
9373 ele2 = tcg_temp_new_i32();
9374 ele3 = tcg_temp_new_i32();
9376 read_vec_element_i32(s, ele1, rn, 0, size);
9377 read_vec_element_i32(s, ele2, rm, 0, size);
9378 read_vec_element_i32(s, ele3, rd, 0, size);
9380 switch (opcode) {
9381 case 0x0: /* SQRDMLAH */
9382 if (size == 1) {
9383 gen_helper_neon_qrdmlah_s16(ele3, cpu_env, ele1, ele2, ele3);
9384 } else {
9385 gen_helper_neon_qrdmlah_s32(ele3, cpu_env, ele1, ele2, ele3);
9387 break;
9388 case 0x1: /* SQRDMLSH */
9389 if (size == 1) {
9390 gen_helper_neon_qrdmlsh_s16(ele3, cpu_env, ele1, ele2, ele3);
9391 } else {
9392 gen_helper_neon_qrdmlsh_s32(ele3, cpu_env, ele1, ele2, ele3);
9394 break;
9395 default:
9396 g_assert_not_reached();
9398 tcg_temp_free_i32(ele1);
9399 tcg_temp_free_i32(ele2);
9401 res = tcg_temp_new_i64();
9402 tcg_gen_extu_i32_i64(res, ele3);
9403 tcg_temp_free_i32(ele3);
9405 write_fp_dreg(s, rd, res);
9406 tcg_temp_free_i64(res);
9409 static void handle_2misc_64(DisasContext *s, int opcode, bool u,
9410 TCGv_i64 tcg_rd, TCGv_i64 tcg_rn,
9411 TCGv_i32 tcg_rmode, TCGv_ptr tcg_fpstatus)
9413 /* Handle 64->64 opcodes which are shared between the scalar and
9414 * vector 2-reg-misc groups. We cover every integer opcode where size == 3
9415 * is valid in either group and also the double-precision fp ops.
9416 * The caller only need provide tcg_rmode and tcg_fpstatus if the op
9417 * requires them.
9419 TCGCond cond;
9421 switch (opcode) {
9422 case 0x4: /* CLS, CLZ */
9423 if (u) {
9424 tcg_gen_clzi_i64(tcg_rd, tcg_rn, 64);
9425 } else {
9426 tcg_gen_clrsb_i64(tcg_rd, tcg_rn);
9428 break;
9429 case 0x5: /* NOT */
9430 /* This opcode is shared with CNT and RBIT but we have earlier
9431 * enforced that size == 3 if and only if this is the NOT insn.
9433 tcg_gen_not_i64(tcg_rd, tcg_rn);
9434 break;
9435 case 0x7: /* SQABS, SQNEG */
9436 if (u) {
9437 gen_helper_neon_qneg_s64(tcg_rd, cpu_env, tcg_rn);
9438 } else {
9439 gen_helper_neon_qabs_s64(tcg_rd, cpu_env, tcg_rn);
9441 break;
9442 case 0xa: /* CMLT */
9443 /* 64 bit integer comparison against zero, result is
9444 * test ? (2^64 - 1) : 0. We implement via setcond(!test) and
9445 * subtracting 1.
9447 cond = TCG_COND_LT;
9448 do_cmop:
9449 tcg_gen_setcondi_i64(cond, tcg_rd, tcg_rn, 0);
9450 tcg_gen_neg_i64(tcg_rd, tcg_rd);
9451 break;
9452 case 0x8: /* CMGT, CMGE */
9453 cond = u ? TCG_COND_GE : TCG_COND_GT;
9454 goto do_cmop;
9455 case 0x9: /* CMEQ, CMLE */
9456 cond = u ? TCG_COND_LE : TCG_COND_EQ;
9457 goto do_cmop;
9458 case 0xb: /* ABS, NEG */
9459 if (u) {
9460 tcg_gen_neg_i64(tcg_rd, tcg_rn);
9461 } else {
9462 tcg_gen_abs_i64(tcg_rd, tcg_rn);
9464 break;
9465 case 0x2f: /* FABS */
9466 gen_helper_vfp_absd(tcg_rd, tcg_rn);
9467 break;
9468 case 0x6f: /* FNEG */
9469 gen_helper_vfp_negd(tcg_rd, tcg_rn);
9470 break;
9471 case 0x7f: /* FSQRT */
9472 gen_helper_vfp_sqrtd(tcg_rd, tcg_rn, cpu_env);
9473 break;
9474 case 0x1a: /* FCVTNS */
9475 case 0x1b: /* FCVTMS */
9476 case 0x1c: /* FCVTAS */
9477 case 0x3a: /* FCVTPS */
9478 case 0x3b: /* FCVTZS */
9480 TCGv_i32 tcg_shift = tcg_const_i32(0);
9481 gen_helper_vfp_tosqd(tcg_rd, tcg_rn, tcg_shift, tcg_fpstatus);
9482 tcg_temp_free_i32(tcg_shift);
9483 break;
9485 case 0x5a: /* FCVTNU */
9486 case 0x5b: /* FCVTMU */
9487 case 0x5c: /* FCVTAU */
9488 case 0x7a: /* FCVTPU */
9489 case 0x7b: /* FCVTZU */
9491 TCGv_i32 tcg_shift = tcg_const_i32(0);
9492 gen_helper_vfp_touqd(tcg_rd, tcg_rn, tcg_shift, tcg_fpstatus);
9493 tcg_temp_free_i32(tcg_shift);
9494 break;
9496 case 0x18: /* FRINTN */
9497 case 0x19: /* FRINTM */
9498 case 0x38: /* FRINTP */
9499 case 0x39: /* FRINTZ */
9500 case 0x58: /* FRINTA */
9501 case 0x79: /* FRINTI */
9502 gen_helper_rintd(tcg_rd, tcg_rn, tcg_fpstatus);
9503 break;
9504 case 0x59: /* FRINTX */
9505 gen_helper_rintd_exact(tcg_rd, tcg_rn, tcg_fpstatus);
9506 break;
9507 case 0x1e: /* FRINT32Z */
9508 case 0x5e: /* FRINT32X */
9509 gen_helper_frint32_d(tcg_rd, tcg_rn, tcg_fpstatus);
9510 break;
9511 case 0x1f: /* FRINT64Z */
9512 case 0x5f: /* FRINT64X */
9513 gen_helper_frint64_d(tcg_rd, tcg_rn, tcg_fpstatus);
9514 break;
9515 default:
9516 g_assert_not_reached();
9520 static void handle_2misc_fcmp_zero(DisasContext *s, int opcode,
9521 bool is_scalar, bool is_u, bool is_q,
9522 int size, int rn, int rd)
9524 bool is_double = (size == MO_64);
9525 TCGv_ptr fpst;
9527 if (!fp_access_check(s)) {
9528 return;
9531 fpst = get_fpstatus_ptr(size == MO_16);
9533 if (is_double) {
9534 TCGv_i64 tcg_op = tcg_temp_new_i64();
9535 TCGv_i64 tcg_zero = tcg_const_i64(0);
9536 TCGv_i64 tcg_res = tcg_temp_new_i64();
9537 NeonGenTwoDoubleOpFn *genfn;
9538 bool swap = false;
9539 int pass;
9541 switch (opcode) {
9542 case 0x2e: /* FCMLT (zero) */
9543 swap = true;
9544 /* fallthrough */
9545 case 0x2c: /* FCMGT (zero) */
9546 genfn = gen_helper_neon_cgt_f64;
9547 break;
9548 case 0x2d: /* FCMEQ (zero) */
9549 genfn = gen_helper_neon_ceq_f64;
9550 break;
9551 case 0x6d: /* FCMLE (zero) */
9552 swap = true;
9553 /* fall through */
9554 case 0x6c: /* FCMGE (zero) */
9555 genfn = gen_helper_neon_cge_f64;
9556 break;
9557 default:
9558 g_assert_not_reached();
9561 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
9562 read_vec_element(s, tcg_op, rn, pass, MO_64);
9563 if (swap) {
9564 genfn(tcg_res, tcg_zero, tcg_op, fpst);
9565 } else {
9566 genfn(tcg_res, tcg_op, tcg_zero, fpst);
9568 write_vec_element(s, tcg_res, rd, pass, MO_64);
9570 tcg_temp_free_i64(tcg_res);
9571 tcg_temp_free_i64(tcg_zero);
9572 tcg_temp_free_i64(tcg_op);
9574 clear_vec_high(s, !is_scalar, rd);
9575 } else {
9576 TCGv_i32 tcg_op = tcg_temp_new_i32();
9577 TCGv_i32 tcg_zero = tcg_const_i32(0);
9578 TCGv_i32 tcg_res = tcg_temp_new_i32();
9579 NeonGenTwoSingleOpFn *genfn;
9580 bool swap = false;
9581 int pass, maxpasses;
9583 if (size == MO_16) {
9584 switch (opcode) {
9585 case 0x2e: /* FCMLT (zero) */
9586 swap = true;
9587 /* fall through */
9588 case 0x2c: /* FCMGT (zero) */
9589 genfn = gen_helper_advsimd_cgt_f16;
9590 break;
9591 case 0x2d: /* FCMEQ (zero) */
9592 genfn = gen_helper_advsimd_ceq_f16;
9593 break;
9594 case 0x6d: /* FCMLE (zero) */
9595 swap = true;
9596 /* fall through */
9597 case 0x6c: /* FCMGE (zero) */
9598 genfn = gen_helper_advsimd_cge_f16;
9599 break;
9600 default:
9601 g_assert_not_reached();
9603 } else {
9604 switch (opcode) {
9605 case 0x2e: /* FCMLT (zero) */
9606 swap = true;
9607 /* fall through */
9608 case 0x2c: /* FCMGT (zero) */
9609 genfn = gen_helper_neon_cgt_f32;
9610 break;
9611 case 0x2d: /* FCMEQ (zero) */
9612 genfn = gen_helper_neon_ceq_f32;
9613 break;
9614 case 0x6d: /* FCMLE (zero) */
9615 swap = true;
9616 /* fall through */
9617 case 0x6c: /* FCMGE (zero) */
9618 genfn = gen_helper_neon_cge_f32;
9619 break;
9620 default:
9621 g_assert_not_reached();
9625 if (is_scalar) {
9626 maxpasses = 1;
9627 } else {
9628 int vector_size = 8 << is_q;
9629 maxpasses = vector_size >> size;
9632 for (pass = 0; pass < maxpasses; pass++) {
9633 read_vec_element_i32(s, tcg_op, rn, pass, size);
9634 if (swap) {
9635 genfn(tcg_res, tcg_zero, tcg_op, fpst);
9636 } else {
9637 genfn(tcg_res, tcg_op, tcg_zero, fpst);
9639 if (is_scalar) {
9640 write_fp_sreg(s, rd, tcg_res);
9641 } else {
9642 write_vec_element_i32(s, tcg_res, rd, pass, size);
9645 tcg_temp_free_i32(tcg_res);
9646 tcg_temp_free_i32(tcg_zero);
9647 tcg_temp_free_i32(tcg_op);
9648 if (!is_scalar) {
9649 clear_vec_high(s, is_q, rd);
9653 tcg_temp_free_ptr(fpst);
9656 static void handle_2misc_reciprocal(DisasContext *s, int opcode,
9657 bool is_scalar, bool is_u, bool is_q,
9658 int size, int rn, int rd)
9660 bool is_double = (size == 3);
9661 TCGv_ptr fpst = get_fpstatus_ptr(false);
9663 if (is_double) {
9664 TCGv_i64 tcg_op = tcg_temp_new_i64();
9665 TCGv_i64 tcg_res = tcg_temp_new_i64();
9666 int pass;
9668 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
9669 read_vec_element(s, tcg_op, rn, pass, MO_64);
9670 switch (opcode) {
9671 case 0x3d: /* FRECPE */
9672 gen_helper_recpe_f64(tcg_res, tcg_op, fpst);
9673 break;
9674 case 0x3f: /* FRECPX */
9675 gen_helper_frecpx_f64(tcg_res, tcg_op, fpst);
9676 break;
9677 case 0x7d: /* FRSQRTE */
9678 gen_helper_rsqrte_f64(tcg_res, tcg_op, fpst);
9679 break;
9680 default:
9681 g_assert_not_reached();
9683 write_vec_element(s, tcg_res, rd, pass, MO_64);
9685 tcg_temp_free_i64(tcg_res);
9686 tcg_temp_free_i64(tcg_op);
9687 clear_vec_high(s, !is_scalar, rd);
9688 } else {
9689 TCGv_i32 tcg_op = tcg_temp_new_i32();
9690 TCGv_i32 tcg_res = tcg_temp_new_i32();
9691 int pass, maxpasses;
9693 if (is_scalar) {
9694 maxpasses = 1;
9695 } else {
9696 maxpasses = is_q ? 4 : 2;
9699 for (pass = 0; pass < maxpasses; pass++) {
9700 read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
9702 switch (opcode) {
9703 case 0x3c: /* URECPE */
9704 gen_helper_recpe_u32(tcg_res, tcg_op);
9705 break;
9706 case 0x3d: /* FRECPE */
9707 gen_helper_recpe_f32(tcg_res, tcg_op, fpst);
9708 break;
9709 case 0x3f: /* FRECPX */
9710 gen_helper_frecpx_f32(tcg_res, tcg_op, fpst);
9711 break;
9712 case 0x7d: /* FRSQRTE */
9713 gen_helper_rsqrte_f32(tcg_res, tcg_op, fpst);
9714 break;
9715 default:
9716 g_assert_not_reached();
9719 if (is_scalar) {
9720 write_fp_sreg(s, rd, tcg_res);
9721 } else {
9722 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
9725 tcg_temp_free_i32(tcg_res);
9726 tcg_temp_free_i32(tcg_op);
9727 if (!is_scalar) {
9728 clear_vec_high(s, is_q, rd);
9731 tcg_temp_free_ptr(fpst);
9734 static void handle_2misc_narrow(DisasContext *s, bool scalar,
9735 int opcode, bool u, bool is_q,
9736 int size, int rn, int rd)
9738 /* Handle 2-reg-misc ops which are narrowing (so each 2*size element
9739 * in the source becomes a size element in the destination).
9741 int pass;
9742 TCGv_i32 tcg_res[2];
9743 int destelt = is_q ? 2 : 0;
9744 int passes = scalar ? 1 : 2;
9746 if (scalar) {
9747 tcg_res[1] = tcg_const_i32(0);
9750 for (pass = 0; pass < passes; pass++) {
9751 TCGv_i64 tcg_op = tcg_temp_new_i64();
9752 NeonGenNarrowFn *genfn = NULL;
9753 NeonGenNarrowEnvFn *genenvfn = NULL;
9755 if (scalar) {
9756 read_vec_element(s, tcg_op, rn, pass, size + 1);
9757 } else {
9758 read_vec_element(s, tcg_op, rn, pass, MO_64);
9760 tcg_res[pass] = tcg_temp_new_i32();
9762 switch (opcode) {
9763 case 0x12: /* XTN, SQXTUN */
9765 static NeonGenNarrowFn * const xtnfns[3] = {
9766 gen_helper_neon_narrow_u8,
9767 gen_helper_neon_narrow_u16,
9768 tcg_gen_extrl_i64_i32,
9770 static NeonGenNarrowEnvFn * const sqxtunfns[3] = {
9771 gen_helper_neon_unarrow_sat8,
9772 gen_helper_neon_unarrow_sat16,
9773 gen_helper_neon_unarrow_sat32,
9775 if (u) {
9776 genenvfn = sqxtunfns[size];
9777 } else {
9778 genfn = xtnfns[size];
9780 break;
9782 case 0x14: /* SQXTN, UQXTN */
9784 static NeonGenNarrowEnvFn * const fns[3][2] = {
9785 { gen_helper_neon_narrow_sat_s8,
9786 gen_helper_neon_narrow_sat_u8 },
9787 { gen_helper_neon_narrow_sat_s16,
9788 gen_helper_neon_narrow_sat_u16 },
9789 { gen_helper_neon_narrow_sat_s32,
9790 gen_helper_neon_narrow_sat_u32 },
9792 genenvfn = fns[size][u];
9793 break;
9795 case 0x16: /* FCVTN, FCVTN2 */
9796 /* 32 bit to 16 bit or 64 bit to 32 bit float conversion */
9797 if (size == 2) {
9798 gen_helper_vfp_fcvtsd(tcg_res[pass], tcg_op, cpu_env);
9799 } else {
9800 TCGv_i32 tcg_lo = tcg_temp_new_i32();
9801 TCGv_i32 tcg_hi = tcg_temp_new_i32();
9802 TCGv_ptr fpst = get_fpstatus_ptr(false);
9803 TCGv_i32 ahp = get_ahp_flag();
9805 tcg_gen_extr_i64_i32(tcg_lo, tcg_hi, tcg_op);
9806 gen_helper_vfp_fcvt_f32_to_f16(tcg_lo, tcg_lo, fpst, ahp);
9807 gen_helper_vfp_fcvt_f32_to_f16(tcg_hi, tcg_hi, fpst, ahp);
9808 tcg_gen_deposit_i32(tcg_res[pass], tcg_lo, tcg_hi, 16, 16);
9809 tcg_temp_free_i32(tcg_lo);
9810 tcg_temp_free_i32(tcg_hi);
9811 tcg_temp_free_ptr(fpst);
9812 tcg_temp_free_i32(ahp);
9814 break;
9815 case 0x56: /* FCVTXN, FCVTXN2 */
9816 /* 64 bit to 32 bit float conversion
9817 * with von Neumann rounding (round to odd)
9819 assert(size == 2);
9820 gen_helper_fcvtx_f64_to_f32(tcg_res[pass], tcg_op, cpu_env);
9821 break;
9822 default:
9823 g_assert_not_reached();
9826 if (genfn) {
9827 genfn(tcg_res[pass], tcg_op);
9828 } else if (genenvfn) {
9829 genenvfn(tcg_res[pass], cpu_env, tcg_op);
9832 tcg_temp_free_i64(tcg_op);
9835 for (pass = 0; pass < 2; pass++) {
9836 write_vec_element_i32(s, tcg_res[pass], rd, destelt + pass, MO_32);
9837 tcg_temp_free_i32(tcg_res[pass]);
9839 clear_vec_high(s, is_q, rd);
9842 /* Remaining saturating accumulating ops */
9843 static void handle_2misc_satacc(DisasContext *s, bool is_scalar, bool is_u,
9844 bool is_q, int size, int rn, int rd)
9846 bool is_double = (size == 3);
9848 if (is_double) {
9849 TCGv_i64 tcg_rn = tcg_temp_new_i64();
9850 TCGv_i64 tcg_rd = tcg_temp_new_i64();
9851 int pass;
9853 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
9854 read_vec_element(s, tcg_rn, rn, pass, MO_64);
9855 read_vec_element(s, tcg_rd, rd, pass, MO_64);
9857 if (is_u) { /* USQADD */
9858 gen_helper_neon_uqadd_s64(tcg_rd, cpu_env, tcg_rn, tcg_rd);
9859 } else { /* SUQADD */
9860 gen_helper_neon_sqadd_u64(tcg_rd, cpu_env, tcg_rn, tcg_rd);
9862 write_vec_element(s, tcg_rd, rd, pass, MO_64);
9864 tcg_temp_free_i64(tcg_rd);
9865 tcg_temp_free_i64(tcg_rn);
9866 clear_vec_high(s, !is_scalar, rd);
9867 } else {
9868 TCGv_i32 tcg_rn = tcg_temp_new_i32();
9869 TCGv_i32 tcg_rd = tcg_temp_new_i32();
9870 int pass, maxpasses;
9872 if (is_scalar) {
9873 maxpasses = 1;
9874 } else {
9875 maxpasses = is_q ? 4 : 2;
9878 for (pass = 0; pass < maxpasses; pass++) {
9879 if (is_scalar) {
9880 read_vec_element_i32(s, tcg_rn, rn, pass, size);
9881 read_vec_element_i32(s, tcg_rd, rd, pass, size);
9882 } else {
9883 read_vec_element_i32(s, tcg_rn, rn, pass, MO_32);
9884 read_vec_element_i32(s, tcg_rd, rd, pass, MO_32);
9887 if (is_u) { /* USQADD */
9888 switch (size) {
9889 case 0:
9890 gen_helper_neon_uqadd_s8(tcg_rd, cpu_env, tcg_rn, tcg_rd);
9891 break;
9892 case 1:
9893 gen_helper_neon_uqadd_s16(tcg_rd, cpu_env, tcg_rn, tcg_rd);
9894 break;
9895 case 2:
9896 gen_helper_neon_uqadd_s32(tcg_rd, cpu_env, tcg_rn, tcg_rd);
9897 break;
9898 default:
9899 g_assert_not_reached();
9901 } else { /* SUQADD */
9902 switch (size) {
9903 case 0:
9904 gen_helper_neon_sqadd_u8(tcg_rd, cpu_env, tcg_rn, tcg_rd);
9905 break;
9906 case 1:
9907 gen_helper_neon_sqadd_u16(tcg_rd, cpu_env, tcg_rn, tcg_rd);
9908 break;
9909 case 2:
9910 gen_helper_neon_sqadd_u32(tcg_rd, cpu_env, tcg_rn, tcg_rd);
9911 break;
9912 default:
9913 g_assert_not_reached();
9917 if (is_scalar) {
9918 TCGv_i64 tcg_zero = tcg_const_i64(0);
9919 write_vec_element(s, tcg_zero, rd, 0, MO_64);
9920 tcg_temp_free_i64(tcg_zero);
9922 write_vec_element_i32(s, tcg_rd, rd, pass, MO_32);
9924 tcg_temp_free_i32(tcg_rd);
9925 tcg_temp_free_i32(tcg_rn);
9926 clear_vec_high(s, is_q, rd);
9930 /* AdvSIMD scalar two reg misc
9931 * 31 30 29 28 24 23 22 21 17 16 12 11 10 9 5 4 0
9932 * +-----+---+-----------+------+-----------+--------+-----+------+------+
9933 * | 0 1 | U | 1 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 | Rn | Rd |
9934 * +-----+---+-----------+------+-----------+--------+-----+------+------+
9936 static void disas_simd_scalar_two_reg_misc(DisasContext *s, uint32_t insn)
9938 int rd = extract32(insn, 0, 5);
9939 int rn = extract32(insn, 5, 5);
9940 int opcode = extract32(insn, 12, 5);
9941 int size = extract32(insn, 22, 2);
9942 bool u = extract32(insn, 29, 1);
9943 bool is_fcvt = false;
9944 int rmode;
9945 TCGv_i32 tcg_rmode;
9946 TCGv_ptr tcg_fpstatus;
9948 switch (opcode) {
9949 case 0x3: /* USQADD / SUQADD*/
9950 if (!fp_access_check(s)) {
9951 return;
9953 handle_2misc_satacc(s, true, u, false, size, rn, rd);
9954 return;
9955 case 0x7: /* SQABS / SQNEG */
9956 break;
9957 case 0xa: /* CMLT */
9958 if (u) {
9959 unallocated_encoding(s);
9960 return;
9962 /* fall through */
9963 case 0x8: /* CMGT, CMGE */
9964 case 0x9: /* CMEQ, CMLE */
9965 case 0xb: /* ABS, NEG */
9966 if (size != 3) {
9967 unallocated_encoding(s);
9968 return;
9970 break;
9971 case 0x12: /* SQXTUN */
9972 if (!u) {
9973 unallocated_encoding(s);
9974 return;
9976 /* fall through */
9977 case 0x14: /* SQXTN, UQXTN */
9978 if (size == 3) {
9979 unallocated_encoding(s);
9980 return;
9982 if (!fp_access_check(s)) {
9983 return;
9985 handle_2misc_narrow(s, true, opcode, u, false, size, rn, rd);
9986 return;
9987 case 0xc ... 0xf:
9988 case 0x16 ... 0x1d:
9989 case 0x1f:
9990 /* Floating point: U, size[1] and opcode indicate operation;
9991 * size[0] indicates single or double precision.
9993 opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
9994 size = extract32(size, 0, 1) ? 3 : 2;
9995 switch (opcode) {
9996 case 0x2c: /* FCMGT (zero) */
9997 case 0x2d: /* FCMEQ (zero) */
9998 case 0x2e: /* FCMLT (zero) */
9999 case 0x6c: /* FCMGE (zero) */
10000 case 0x6d: /* FCMLE (zero) */
10001 handle_2misc_fcmp_zero(s, opcode, true, u, true, size, rn, rd);
10002 return;
10003 case 0x1d: /* SCVTF */
10004 case 0x5d: /* UCVTF */
10006 bool is_signed = (opcode == 0x1d);
10007 if (!fp_access_check(s)) {
10008 return;
10010 handle_simd_intfp_conv(s, rd, rn, 1, is_signed, 0, size);
10011 return;
10013 case 0x3d: /* FRECPE */
10014 case 0x3f: /* FRECPX */
10015 case 0x7d: /* FRSQRTE */
10016 if (!fp_access_check(s)) {
10017 return;
10019 handle_2misc_reciprocal(s, opcode, true, u, true, size, rn, rd);
10020 return;
10021 case 0x1a: /* FCVTNS */
10022 case 0x1b: /* FCVTMS */
10023 case 0x3a: /* FCVTPS */
10024 case 0x3b: /* FCVTZS */
10025 case 0x5a: /* FCVTNU */
10026 case 0x5b: /* FCVTMU */
10027 case 0x7a: /* FCVTPU */
10028 case 0x7b: /* FCVTZU */
10029 is_fcvt = true;
10030 rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
10031 break;
10032 case 0x1c: /* FCVTAS */
10033 case 0x5c: /* FCVTAU */
10034 /* TIEAWAY doesn't fit in the usual rounding mode encoding */
10035 is_fcvt = true;
10036 rmode = FPROUNDING_TIEAWAY;
10037 break;
10038 case 0x56: /* FCVTXN, FCVTXN2 */
10039 if (size == 2) {
10040 unallocated_encoding(s);
10041 return;
10043 if (!fp_access_check(s)) {
10044 return;
10046 handle_2misc_narrow(s, true, opcode, u, false, size - 1, rn, rd);
10047 return;
10048 default:
10049 unallocated_encoding(s);
10050 return;
10052 break;
10053 default:
10054 unallocated_encoding(s);
10055 return;
10058 if (!fp_access_check(s)) {
10059 return;
10062 if (is_fcvt) {
10063 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rmode));
10064 tcg_fpstatus = get_fpstatus_ptr(false);
10065 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
10066 } else {
10067 tcg_rmode = NULL;
10068 tcg_fpstatus = NULL;
10071 if (size == 3) {
10072 TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
10073 TCGv_i64 tcg_rd = tcg_temp_new_i64();
10075 handle_2misc_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rmode, tcg_fpstatus);
10076 write_fp_dreg(s, rd, tcg_rd);
10077 tcg_temp_free_i64(tcg_rd);
10078 tcg_temp_free_i64(tcg_rn);
10079 } else {
10080 TCGv_i32 tcg_rn = tcg_temp_new_i32();
10081 TCGv_i32 tcg_rd = tcg_temp_new_i32();
10083 read_vec_element_i32(s, tcg_rn, rn, 0, size);
10085 switch (opcode) {
10086 case 0x7: /* SQABS, SQNEG */
10088 NeonGenOneOpEnvFn *genfn;
10089 static NeonGenOneOpEnvFn * const fns[3][2] = {
10090 { gen_helper_neon_qabs_s8, gen_helper_neon_qneg_s8 },
10091 { gen_helper_neon_qabs_s16, gen_helper_neon_qneg_s16 },
10092 { gen_helper_neon_qabs_s32, gen_helper_neon_qneg_s32 },
10094 genfn = fns[size][u];
10095 genfn(tcg_rd, cpu_env, tcg_rn);
10096 break;
10098 case 0x1a: /* FCVTNS */
10099 case 0x1b: /* FCVTMS */
10100 case 0x1c: /* FCVTAS */
10101 case 0x3a: /* FCVTPS */
10102 case 0x3b: /* FCVTZS */
10104 TCGv_i32 tcg_shift = tcg_const_i32(0);
10105 gen_helper_vfp_tosls(tcg_rd, tcg_rn, tcg_shift, tcg_fpstatus);
10106 tcg_temp_free_i32(tcg_shift);
10107 break;
10109 case 0x5a: /* FCVTNU */
10110 case 0x5b: /* FCVTMU */
10111 case 0x5c: /* FCVTAU */
10112 case 0x7a: /* FCVTPU */
10113 case 0x7b: /* FCVTZU */
10115 TCGv_i32 tcg_shift = tcg_const_i32(0);
10116 gen_helper_vfp_touls(tcg_rd, tcg_rn, tcg_shift, tcg_fpstatus);
10117 tcg_temp_free_i32(tcg_shift);
10118 break;
10120 default:
10121 g_assert_not_reached();
10124 write_fp_sreg(s, rd, tcg_rd);
10125 tcg_temp_free_i32(tcg_rd);
10126 tcg_temp_free_i32(tcg_rn);
10129 if (is_fcvt) {
10130 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
10131 tcg_temp_free_i32(tcg_rmode);
10132 tcg_temp_free_ptr(tcg_fpstatus);
10136 /* SSHR[RA]/USHR[RA] - Vector shift right (optional rounding/accumulate) */
10137 static void handle_vec_simd_shri(DisasContext *s, bool is_q, bool is_u,
10138 int immh, int immb, int opcode, int rn, int rd)
10140 int size = 32 - clz32(immh) - 1;
10141 int immhb = immh << 3 | immb;
10142 int shift = 2 * (8 << size) - immhb;
10143 GVecGen2iFn *gvec_fn;
10145 if (extract32(immh, 3, 1) && !is_q) {
10146 unallocated_encoding(s);
10147 return;
10149 tcg_debug_assert(size <= 3);
10151 if (!fp_access_check(s)) {
10152 return;
10155 switch (opcode) {
10156 case 0x02: /* SSRA / USRA (accumulate) */
10157 gvec_fn = is_u ? gen_gvec_usra : gen_gvec_ssra;
10158 break;
10160 case 0x08: /* SRI */
10161 gvec_fn = gen_gvec_sri;
10162 break;
10164 case 0x00: /* SSHR / USHR */
10165 if (is_u) {
10166 if (shift == 8 << size) {
10167 /* Shift count the same size as element size produces zero. */
10168 tcg_gen_gvec_dup_imm(size, vec_full_reg_offset(s, rd),
10169 is_q ? 16 : 8, vec_full_reg_size(s), 0);
10170 return;
10172 gvec_fn = tcg_gen_gvec_shri;
10173 } else {
10174 /* Shift count the same size as element size produces all sign. */
10175 if (shift == 8 << size) {
10176 shift -= 1;
10178 gvec_fn = tcg_gen_gvec_sari;
10180 break;
10182 case 0x04: /* SRSHR / URSHR (rounding) */
10183 gvec_fn = is_u ? gen_gvec_urshr : gen_gvec_srshr;
10184 break;
10186 case 0x06: /* SRSRA / URSRA (accum + rounding) */
10187 gvec_fn = is_u ? gen_gvec_ursra : gen_gvec_srsra;
10188 break;
10190 default:
10191 g_assert_not_reached();
10194 gen_gvec_fn2i(s, is_q, rd, rn, shift, gvec_fn, size);
10197 /* SHL/SLI - Vector shift left */
10198 static void handle_vec_simd_shli(DisasContext *s, bool is_q, bool insert,
10199 int immh, int immb, int opcode, int rn, int rd)
10201 int size = 32 - clz32(immh) - 1;
10202 int immhb = immh << 3 | immb;
10203 int shift = immhb - (8 << size);
10205 /* Range of size is limited by decode: immh is a non-zero 4 bit field */
10206 assert(size >= 0 && size <= 3);
10208 if (extract32(immh, 3, 1) && !is_q) {
10209 unallocated_encoding(s);
10210 return;
10213 if (!fp_access_check(s)) {
10214 return;
10217 if (insert) {
10218 gen_gvec_fn2i(s, is_q, rd, rn, shift, gen_gvec_sli, size);
10219 } else {
10220 gen_gvec_fn2i(s, is_q, rd, rn, shift, tcg_gen_gvec_shli, size);
10224 /* USHLL/SHLL - Vector shift left with widening */
10225 static void handle_vec_simd_wshli(DisasContext *s, bool is_q, bool is_u,
10226 int immh, int immb, int opcode, int rn, int rd)
10228 int size = 32 - clz32(immh) - 1;
10229 int immhb = immh << 3 | immb;
10230 int shift = immhb - (8 << size);
10231 int dsize = 64;
10232 int esize = 8 << size;
10233 int elements = dsize/esize;
10234 TCGv_i64 tcg_rn = new_tmp_a64(s);
10235 TCGv_i64 tcg_rd = new_tmp_a64(s);
10236 int i;
10238 if (size >= 3) {
10239 unallocated_encoding(s);
10240 return;
10243 if (!fp_access_check(s)) {
10244 return;
10247 /* For the LL variants the store is larger than the load,
10248 * so if rd == rn we would overwrite parts of our input.
10249 * So load everything right now and use shifts in the main loop.
10251 read_vec_element(s, tcg_rn, rn, is_q ? 1 : 0, MO_64);
10253 for (i = 0; i < elements; i++) {
10254 tcg_gen_shri_i64(tcg_rd, tcg_rn, i * esize);
10255 ext_and_shift_reg(tcg_rd, tcg_rd, size | (!is_u << 2), 0);
10256 tcg_gen_shli_i64(tcg_rd, tcg_rd, shift);
10257 write_vec_element(s, tcg_rd, rd, i, size + 1);
10261 /* SHRN/RSHRN - Shift right with narrowing (and potential rounding) */
10262 static void handle_vec_simd_shrn(DisasContext *s, bool is_q,
10263 int immh, int immb, int opcode, int rn, int rd)
10265 int immhb = immh << 3 | immb;
10266 int size = 32 - clz32(immh) - 1;
10267 int dsize = 64;
10268 int esize = 8 << size;
10269 int elements = dsize/esize;
10270 int shift = (2 * esize) - immhb;
10271 bool round = extract32(opcode, 0, 1);
10272 TCGv_i64 tcg_rn, tcg_rd, tcg_final;
10273 TCGv_i64 tcg_round;
10274 int i;
10276 if (extract32(immh, 3, 1)) {
10277 unallocated_encoding(s);
10278 return;
10281 if (!fp_access_check(s)) {
10282 return;
10285 tcg_rn = tcg_temp_new_i64();
10286 tcg_rd = tcg_temp_new_i64();
10287 tcg_final = tcg_temp_new_i64();
10288 read_vec_element(s, tcg_final, rd, is_q ? 1 : 0, MO_64);
10290 if (round) {
10291 uint64_t round_const = 1ULL << (shift - 1);
10292 tcg_round = tcg_const_i64(round_const);
10293 } else {
10294 tcg_round = NULL;
10297 for (i = 0; i < elements; i++) {
10298 read_vec_element(s, tcg_rn, rn, i, size+1);
10299 handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
10300 false, true, size+1, shift);
10302 tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
10305 if (!is_q) {
10306 write_vec_element(s, tcg_final, rd, 0, MO_64);
10307 } else {
10308 write_vec_element(s, tcg_final, rd, 1, MO_64);
10310 if (round) {
10311 tcg_temp_free_i64(tcg_round);
10313 tcg_temp_free_i64(tcg_rn);
10314 tcg_temp_free_i64(tcg_rd);
10315 tcg_temp_free_i64(tcg_final);
10317 clear_vec_high(s, is_q, rd);
10321 /* AdvSIMD shift by immediate
10322 * 31 30 29 28 23 22 19 18 16 15 11 10 9 5 4 0
10323 * +---+---+---+-------------+------+------+--------+---+------+------+
10324 * | 0 | Q | U | 0 1 1 1 1 0 | immh | immb | opcode | 1 | Rn | Rd |
10325 * +---+---+---+-------------+------+------+--------+---+------+------+
10327 static void disas_simd_shift_imm(DisasContext *s, uint32_t insn)
10329 int rd = extract32(insn, 0, 5);
10330 int rn = extract32(insn, 5, 5);
10331 int opcode = extract32(insn, 11, 5);
10332 int immb = extract32(insn, 16, 3);
10333 int immh = extract32(insn, 19, 4);
10334 bool is_u = extract32(insn, 29, 1);
10335 bool is_q = extract32(insn, 30, 1);
10337 /* data_proc_simd[] has sent immh == 0 to disas_simd_mod_imm. */
10338 assert(immh != 0);
10340 switch (opcode) {
10341 case 0x08: /* SRI */
10342 if (!is_u) {
10343 unallocated_encoding(s);
10344 return;
10346 /* fall through */
10347 case 0x00: /* SSHR / USHR */
10348 case 0x02: /* SSRA / USRA (accumulate) */
10349 case 0x04: /* SRSHR / URSHR (rounding) */
10350 case 0x06: /* SRSRA / URSRA (accum + rounding) */
10351 handle_vec_simd_shri(s, is_q, is_u, immh, immb, opcode, rn, rd);
10352 break;
10353 case 0x0a: /* SHL / SLI */
10354 handle_vec_simd_shli(s, is_q, is_u, immh, immb, opcode, rn, rd);
10355 break;
10356 case 0x10: /* SHRN */
10357 case 0x11: /* RSHRN / SQRSHRUN */
10358 if (is_u) {
10359 handle_vec_simd_sqshrn(s, false, is_q, false, true, immh, immb,
10360 opcode, rn, rd);
10361 } else {
10362 handle_vec_simd_shrn(s, is_q, immh, immb, opcode, rn, rd);
10364 break;
10365 case 0x12: /* SQSHRN / UQSHRN */
10366 case 0x13: /* SQRSHRN / UQRSHRN */
10367 handle_vec_simd_sqshrn(s, false, is_q, is_u, is_u, immh, immb,
10368 opcode, rn, rd);
10369 break;
10370 case 0x14: /* SSHLL / USHLL */
10371 handle_vec_simd_wshli(s, is_q, is_u, immh, immb, opcode, rn, rd);
10372 break;
10373 case 0x1c: /* SCVTF / UCVTF */
10374 handle_simd_shift_intfp_conv(s, false, is_q, is_u, immh, immb,
10375 opcode, rn, rd);
10376 break;
10377 case 0xc: /* SQSHLU */
10378 if (!is_u) {
10379 unallocated_encoding(s);
10380 return;
10382 handle_simd_qshl(s, false, is_q, false, true, immh, immb, rn, rd);
10383 break;
10384 case 0xe: /* SQSHL, UQSHL */
10385 handle_simd_qshl(s, false, is_q, is_u, is_u, immh, immb, rn, rd);
10386 break;
10387 case 0x1f: /* FCVTZS/ FCVTZU */
10388 handle_simd_shift_fpint_conv(s, false, is_q, is_u, immh, immb, rn, rd);
10389 return;
10390 default:
10391 unallocated_encoding(s);
10392 return;
10396 /* Generate code to do a "long" addition or subtraction, ie one done in
10397 * TCGv_i64 on vector lanes twice the width specified by size.
10399 static void gen_neon_addl(int size, bool is_sub, TCGv_i64 tcg_res,
10400 TCGv_i64 tcg_op1, TCGv_i64 tcg_op2)
10402 static NeonGenTwo64OpFn * const fns[3][2] = {
10403 { gen_helper_neon_addl_u16, gen_helper_neon_subl_u16 },
10404 { gen_helper_neon_addl_u32, gen_helper_neon_subl_u32 },
10405 { tcg_gen_add_i64, tcg_gen_sub_i64 },
10407 NeonGenTwo64OpFn *genfn;
10408 assert(size < 3);
10410 genfn = fns[size][is_sub];
10411 genfn(tcg_res, tcg_op1, tcg_op2);
10414 static void handle_3rd_widening(DisasContext *s, int is_q, int is_u, int size,
10415 int opcode, int rd, int rn, int rm)
10417 /* 3-reg-different widening insns: 64 x 64 -> 128 */
10418 TCGv_i64 tcg_res[2];
10419 int pass, accop;
10421 tcg_res[0] = tcg_temp_new_i64();
10422 tcg_res[1] = tcg_temp_new_i64();
10424 /* Does this op do an adding accumulate, a subtracting accumulate,
10425 * or no accumulate at all?
10427 switch (opcode) {
10428 case 5:
10429 case 8:
10430 case 9:
10431 accop = 1;
10432 break;
10433 case 10:
10434 case 11:
10435 accop = -1;
10436 break;
10437 default:
10438 accop = 0;
10439 break;
10442 if (accop != 0) {
10443 read_vec_element(s, tcg_res[0], rd, 0, MO_64);
10444 read_vec_element(s, tcg_res[1], rd, 1, MO_64);
10447 /* size == 2 means two 32x32->64 operations; this is worth special
10448 * casing because we can generally handle it inline.
10450 if (size == 2) {
10451 for (pass = 0; pass < 2; pass++) {
10452 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10453 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
10454 TCGv_i64 tcg_passres;
10455 MemOp memop = MO_32 | (is_u ? 0 : MO_SIGN);
10457 int elt = pass + is_q * 2;
10459 read_vec_element(s, tcg_op1, rn, elt, memop);
10460 read_vec_element(s, tcg_op2, rm, elt, memop);
10462 if (accop == 0) {
10463 tcg_passres = tcg_res[pass];
10464 } else {
10465 tcg_passres = tcg_temp_new_i64();
10468 switch (opcode) {
10469 case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10470 tcg_gen_add_i64(tcg_passres, tcg_op1, tcg_op2);
10471 break;
10472 case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
10473 tcg_gen_sub_i64(tcg_passres, tcg_op1, tcg_op2);
10474 break;
10475 case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10476 case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10478 TCGv_i64 tcg_tmp1 = tcg_temp_new_i64();
10479 TCGv_i64 tcg_tmp2 = tcg_temp_new_i64();
10481 tcg_gen_sub_i64(tcg_tmp1, tcg_op1, tcg_op2);
10482 tcg_gen_sub_i64(tcg_tmp2, tcg_op2, tcg_op1);
10483 tcg_gen_movcond_i64(is_u ? TCG_COND_GEU : TCG_COND_GE,
10484 tcg_passres,
10485 tcg_op1, tcg_op2, tcg_tmp1, tcg_tmp2);
10486 tcg_temp_free_i64(tcg_tmp1);
10487 tcg_temp_free_i64(tcg_tmp2);
10488 break;
10490 case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10491 case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10492 case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
10493 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
10494 break;
10495 case 9: /* SQDMLAL, SQDMLAL2 */
10496 case 11: /* SQDMLSL, SQDMLSL2 */
10497 case 13: /* SQDMULL, SQDMULL2 */
10498 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
10499 gen_helper_neon_addl_saturate_s64(tcg_passres, cpu_env,
10500 tcg_passres, tcg_passres);
10501 break;
10502 default:
10503 g_assert_not_reached();
10506 if (opcode == 9 || opcode == 11) {
10507 /* saturating accumulate ops */
10508 if (accop < 0) {
10509 tcg_gen_neg_i64(tcg_passres, tcg_passres);
10511 gen_helper_neon_addl_saturate_s64(tcg_res[pass], cpu_env,
10512 tcg_res[pass], tcg_passres);
10513 } else if (accop > 0) {
10514 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
10515 } else if (accop < 0) {
10516 tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
10519 if (accop != 0) {
10520 tcg_temp_free_i64(tcg_passres);
10523 tcg_temp_free_i64(tcg_op1);
10524 tcg_temp_free_i64(tcg_op2);
10526 } else {
10527 /* size 0 or 1, generally helper functions */
10528 for (pass = 0; pass < 2; pass++) {
10529 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
10530 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
10531 TCGv_i64 tcg_passres;
10532 int elt = pass + is_q * 2;
10534 read_vec_element_i32(s, tcg_op1, rn, elt, MO_32);
10535 read_vec_element_i32(s, tcg_op2, rm, elt, MO_32);
10537 if (accop == 0) {
10538 tcg_passres = tcg_res[pass];
10539 } else {
10540 tcg_passres = tcg_temp_new_i64();
10543 switch (opcode) {
10544 case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10545 case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
10547 TCGv_i64 tcg_op2_64 = tcg_temp_new_i64();
10548 static NeonGenWidenFn * const widenfns[2][2] = {
10549 { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
10550 { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
10552 NeonGenWidenFn *widenfn = widenfns[size][is_u];
10554 widenfn(tcg_op2_64, tcg_op2);
10555 widenfn(tcg_passres, tcg_op1);
10556 gen_neon_addl(size, (opcode == 2), tcg_passres,
10557 tcg_passres, tcg_op2_64);
10558 tcg_temp_free_i64(tcg_op2_64);
10559 break;
10561 case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10562 case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10563 if (size == 0) {
10564 if (is_u) {
10565 gen_helper_neon_abdl_u16(tcg_passres, tcg_op1, tcg_op2);
10566 } else {
10567 gen_helper_neon_abdl_s16(tcg_passres, tcg_op1, tcg_op2);
10569 } else {
10570 if (is_u) {
10571 gen_helper_neon_abdl_u32(tcg_passres, tcg_op1, tcg_op2);
10572 } else {
10573 gen_helper_neon_abdl_s32(tcg_passres, tcg_op1, tcg_op2);
10576 break;
10577 case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10578 case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10579 case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
10580 if (size == 0) {
10581 if (is_u) {
10582 gen_helper_neon_mull_u8(tcg_passres, tcg_op1, tcg_op2);
10583 } else {
10584 gen_helper_neon_mull_s8(tcg_passres, tcg_op1, tcg_op2);
10586 } else {
10587 if (is_u) {
10588 gen_helper_neon_mull_u16(tcg_passres, tcg_op1, tcg_op2);
10589 } else {
10590 gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
10593 break;
10594 case 9: /* SQDMLAL, SQDMLAL2 */
10595 case 11: /* SQDMLSL, SQDMLSL2 */
10596 case 13: /* SQDMULL, SQDMULL2 */
10597 assert(size == 1);
10598 gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
10599 gen_helper_neon_addl_saturate_s32(tcg_passres, cpu_env,
10600 tcg_passres, tcg_passres);
10601 break;
10602 default:
10603 g_assert_not_reached();
10605 tcg_temp_free_i32(tcg_op1);
10606 tcg_temp_free_i32(tcg_op2);
10608 if (accop != 0) {
10609 if (opcode == 9 || opcode == 11) {
10610 /* saturating accumulate ops */
10611 if (accop < 0) {
10612 gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
10614 gen_helper_neon_addl_saturate_s32(tcg_res[pass], cpu_env,
10615 tcg_res[pass],
10616 tcg_passres);
10617 } else {
10618 gen_neon_addl(size, (accop < 0), tcg_res[pass],
10619 tcg_res[pass], tcg_passres);
10621 tcg_temp_free_i64(tcg_passres);
10626 write_vec_element(s, tcg_res[0], rd, 0, MO_64);
10627 write_vec_element(s, tcg_res[1], rd, 1, MO_64);
10628 tcg_temp_free_i64(tcg_res[0]);
10629 tcg_temp_free_i64(tcg_res[1]);
10632 static void handle_3rd_wide(DisasContext *s, int is_q, int is_u, int size,
10633 int opcode, int rd, int rn, int rm)
10635 TCGv_i64 tcg_res[2];
10636 int part = is_q ? 2 : 0;
10637 int pass;
10639 for (pass = 0; pass < 2; pass++) {
10640 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10641 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
10642 TCGv_i64 tcg_op2_wide = tcg_temp_new_i64();
10643 static NeonGenWidenFn * const widenfns[3][2] = {
10644 { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
10645 { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
10646 { tcg_gen_ext_i32_i64, tcg_gen_extu_i32_i64 },
10648 NeonGenWidenFn *widenfn = widenfns[size][is_u];
10650 read_vec_element(s, tcg_op1, rn, pass, MO_64);
10651 read_vec_element_i32(s, tcg_op2, rm, part + pass, MO_32);
10652 widenfn(tcg_op2_wide, tcg_op2);
10653 tcg_temp_free_i32(tcg_op2);
10654 tcg_res[pass] = tcg_temp_new_i64();
10655 gen_neon_addl(size, (opcode == 3),
10656 tcg_res[pass], tcg_op1, tcg_op2_wide);
10657 tcg_temp_free_i64(tcg_op1);
10658 tcg_temp_free_i64(tcg_op2_wide);
10661 for (pass = 0; pass < 2; pass++) {
10662 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
10663 tcg_temp_free_i64(tcg_res[pass]);
10667 static void do_narrow_round_high_u32(TCGv_i32 res, TCGv_i64 in)
10669 tcg_gen_addi_i64(in, in, 1U << 31);
10670 tcg_gen_extrh_i64_i32(res, in);
10673 static void handle_3rd_narrowing(DisasContext *s, int is_q, int is_u, int size,
10674 int opcode, int rd, int rn, int rm)
10676 TCGv_i32 tcg_res[2];
10677 int part = is_q ? 2 : 0;
10678 int pass;
10680 for (pass = 0; pass < 2; pass++) {
10681 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10682 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
10683 TCGv_i64 tcg_wideres = tcg_temp_new_i64();
10684 static NeonGenNarrowFn * const narrowfns[3][2] = {
10685 { gen_helper_neon_narrow_high_u8,
10686 gen_helper_neon_narrow_round_high_u8 },
10687 { gen_helper_neon_narrow_high_u16,
10688 gen_helper_neon_narrow_round_high_u16 },
10689 { tcg_gen_extrh_i64_i32, do_narrow_round_high_u32 },
10691 NeonGenNarrowFn *gennarrow = narrowfns[size][is_u];
10693 read_vec_element(s, tcg_op1, rn, pass, MO_64);
10694 read_vec_element(s, tcg_op2, rm, pass, MO_64);
10696 gen_neon_addl(size, (opcode == 6), tcg_wideres, tcg_op1, tcg_op2);
10698 tcg_temp_free_i64(tcg_op1);
10699 tcg_temp_free_i64(tcg_op2);
10701 tcg_res[pass] = tcg_temp_new_i32();
10702 gennarrow(tcg_res[pass], tcg_wideres);
10703 tcg_temp_free_i64(tcg_wideres);
10706 for (pass = 0; pass < 2; pass++) {
10707 write_vec_element_i32(s, tcg_res[pass], rd, pass + part, MO_32);
10708 tcg_temp_free_i32(tcg_res[pass]);
10710 clear_vec_high(s, is_q, rd);
10713 /* AdvSIMD three different
10714 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 0
10715 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
10716 * | 0 | Q | U | 0 1 1 1 0 | size | 1 | Rm | opcode | 0 0 | Rn | Rd |
10717 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
10719 static void disas_simd_three_reg_diff(DisasContext *s, uint32_t insn)
10721 /* Instructions in this group fall into three basic classes
10722 * (in each case with the operation working on each element in
10723 * the input vectors):
10724 * (1) widening 64 x 64 -> 128 (with possibly Vd as an extra
10725 * 128 bit input)
10726 * (2) wide 64 x 128 -> 128
10727 * (3) narrowing 128 x 128 -> 64
10728 * Here we do initial decode, catch unallocated cases and
10729 * dispatch to separate functions for each class.
10731 int is_q = extract32(insn, 30, 1);
10732 int is_u = extract32(insn, 29, 1);
10733 int size = extract32(insn, 22, 2);
10734 int opcode = extract32(insn, 12, 4);
10735 int rm = extract32(insn, 16, 5);
10736 int rn = extract32(insn, 5, 5);
10737 int rd = extract32(insn, 0, 5);
10739 switch (opcode) {
10740 case 1: /* SADDW, SADDW2, UADDW, UADDW2 */
10741 case 3: /* SSUBW, SSUBW2, USUBW, USUBW2 */
10742 /* 64 x 128 -> 128 */
10743 if (size == 3) {
10744 unallocated_encoding(s);
10745 return;
10747 if (!fp_access_check(s)) {
10748 return;
10750 handle_3rd_wide(s, is_q, is_u, size, opcode, rd, rn, rm);
10751 break;
10752 case 4: /* ADDHN, ADDHN2, RADDHN, RADDHN2 */
10753 case 6: /* SUBHN, SUBHN2, RSUBHN, RSUBHN2 */
10754 /* 128 x 128 -> 64 */
10755 if (size == 3) {
10756 unallocated_encoding(s);
10757 return;
10759 if (!fp_access_check(s)) {
10760 return;
10762 handle_3rd_narrowing(s, is_q, is_u, size, opcode, rd, rn, rm);
10763 break;
10764 case 14: /* PMULL, PMULL2 */
10765 if (is_u) {
10766 unallocated_encoding(s);
10767 return;
10769 switch (size) {
10770 case 0: /* PMULL.P8 */
10771 if (!fp_access_check(s)) {
10772 return;
10774 /* The Q field specifies lo/hi half input for this insn. */
10775 gen_gvec_op3_ool(s, true, rd, rn, rm, is_q,
10776 gen_helper_neon_pmull_h);
10777 break;
10779 case 3: /* PMULL.P64 */
10780 if (!dc_isar_feature(aa64_pmull, s)) {
10781 unallocated_encoding(s);
10782 return;
10784 if (!fp_access_check(s)) {
10785 return;
10787 /* The Q field specifies lo/hi half input for this insn. */
10788 gen_gvec_op3_ool(s, true, rd, rn, rm, is_q,
10789 gen_helper_gvec_pmull_q);
10790 break;
10792 default:
10793 unallocated_encoding(s);
10794 break;
10796 return;
10797 case 9: /* SQDMLAL, SQDMLAL2 */
10798 case 11: /* SQDMLSL, SQDMLSL2 */
10799 case 13: /* SQDMULL, SQDMULL2 */
10800 if (is_u || size == 0) {
10801 unallocated_encoding(s);
10802 return;
10804 /* fall through */
10805 case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10806 case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
10807 case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10808 case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10809 case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10810 case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10811 case 12: /* SMULL, SMULL2, UMULL, UMULL2 */
10812 /* 64 x 64 -> 128 */
10813 if (size == 3) {
10814 unallocated_encoding(s);
10815 return;
10817 if (!fp_access_check(s)) {
10818 return;
10821 handle_3rd_widening(s, is_q, is_u, size, opcode, rd, rn, rm);
10822 break;
10823 default:
10824 /* opcode 15 not allocated */
10825 unallocated_encoding(s);
10826 break;
10830 /* Logic op (opcode == 3) subgroup of C3.6.16. */
10831 static void disas_simd_3same_logic(DisasContext *s, uint32_t insn)
10833 int rd = extract32(insn, 0, 5);
10834 int rn = extract32(insn, 5, 5);
10835 int rm = extract32(insn, 16, 5);
10836 int size = extract32(insn, 22, 2);
10837 bool is_u = extract32(insn, 29, 1);
10838 bool is_q = extract32(insn, 30, 1);
10840 if (!fp_access_check(s)) {
10841 return;
10844 switch (size + 4 * is_u) {
10845 case 0: /* AND */
10846 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_and, 0);
10847 return;
10848 case 1: /* BIC */
10849 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_andc, 0);
10850 return;
10851 case 2: /* ORR */
10852 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_or, 0);
10853 return;
10854 case 3: /* ORN */
10855 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_orc, 0);
10856 return;
10857 case 4: /* EOR */
10858 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_xor, 0);
10859 return;
10861 case 5: /* BSL bitwise select */
10862 gen_gvec_fn4(s, is_q, rd, rd, rn, rm, tcg_gen_gvec_bitsel, 0);
10863 return;
10864 case 6: /* BIT, bitwise insert if true */
10865 gen_gvec_fn4(s, is_q, rd, rm, rn, rd, tcg_gen_gvec_bitsel, 0);
10866 return;
10867 case 7: /* BIF, bitwise insert if false */
10868 gen_gvec_fn4(s, is_q, rd, rm, rd, rn, tcg_gen_gvec_bitsel, 0);
10869 return;
10871 default:
10872 g_assert_not_reached();
10876 /* Pairwise op subgroup of C3.6.16.
10878 * This is called directly or via the handle_3same_float for float pairwise
10879 * operations where the opcode and size are calculated differently.
10881 static void handle_simd_3same_pair(DisasContext *s, int is_q, int u, int opcode,
10882 int size, int rn, int rm, int rd)
10884 TCGv_ptr fpst;
10885 int pass;
10887 /* Floating point operations need fpst */
10888 if (opcode >= 0x58) {
10889 fpst = get_fpstatus_ptr(false);
10890 } else {
10891 fpst = NULL;
10894 if (!fp_access_check(s)) {
10895 return;
10898 /* These operations work on the concatenated rm:rn, with each pair of
10899 * adjacent elements being operated on to produce an element in the result.
10901 if (size == 3) {
10902 TCGv_i64 tcg_res[2];
10904 for (pass = 0; pass < 2; pass++) {
10905 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10906 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
10907 int passreg = (pass == 0) ? rn : rm;
10909 read_vec_element(s, tcg_op1, passreg, 0, MO_64);
10910 read_vec_element(s, tcg_op2, passreg, 1, MO_64);
10911 tcg_res[pass] = tcg_temp_new_i64();
10913 switch (opcode) {
10914 case 0x17: /* ADDP */
10915 tcg_gen_add_i64(tcg_res[pass], tcg_op1, tcg_op2);
10916 break;
10917 case 0x58: /* FMAXNMP */
10918 gen_helper_vfp_maxnumd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10919 break;
10920 case 0x5a: /* FADDP */
10921 gen_helper_vfp_addd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10922 break;
10923 case 0x5e: /* FMAXP */
10924 gen_helper_vfp_maxd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10925 break;
10926 case 0x78: /* FMINNMP */
10927 gen_helper_vfp_minnumd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10928 break;
10929 case 0x7e: /* FMINP */
10930 gen_helper_vfp_mind(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10931 break;
10932 default:
10933 g_assert_not_reached();
10936 tcg_temp_free_i64(tcg_op1);
10937 tcg_temp_free_i64(tcg_op2);
10940 for (pass = 0; pass < 2; pass++) {
10941 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
10942 tcg_temp_free_i64(tcg_res[pass]);
10944 } else {
10945 int maxpass = is_q ? 4 : 2;
10946 TCGv_i32 tcg_res[4];
10948 for (pass = 0; pass < maxpass; pass++) {
10949 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
10950 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
10951 NeonGenTwoOpFn *genfn = NULL;
10952 int passreg = pass < (maxpass / 2) ? rn : rm;
10953 int passelt = (is_q && (pass & 1)) ? 2 : 0;
10955 read_vec_element_i32(s, tcg_op1, passreg, passelt, MO_32);
10956 read_vec_element_i32(s, tcg_op2, passreg, passelt + 1, MO_32);
10957 tcg_res[pass] = tcg_temp_new_i32();
10959 switch (opcode) {
10960 case 0x17: /* ADDP */
10962 static NeonGenTwoOpFn * const fns[3] = {
10963 gen_helper_neon_padd_u8,
10964 gen_helper_neon_padd_u16,
10965 tcg_gen_add_i32,
10967 genfn = fns[size];
10968 break;
10970 case 0x14: /* SMAXP, UMAXP */
10972 static NeonGenTwoOpFn * const fns[3][2] = {
10973 { gen_helper_neon_pmax_s8, gen_helper_neon_pmax_u8 },
10974 { gen_helper_neon_pmax_s16, gen_helper_neon_pmax_u16 },
10975 { tcg_gen_smax_i32, tcg_gen_umax_i32 },
10977 genfn = fns[size][u];
10978 break;
10980 case 0x15: /* SMINP, UMINP */
10982 static NeonGenTwoOpFn * const fns[3][2] = {
10983 { gen_helper_neon_pmin_s8, gen_helper_neon_pmin_u8 },
10984 { gen_helper_neon_pmin_s16, gen_helper_neon_pmin_u16 },
10985 { tcg_gen_smin_i32, tcg_gen_umin_i32 },
10987 genfn = fns[size][u];
10988 break;
10990 /* The FP operations are all on single floats (32 bit) */
10991 case 0x58: /* FMAXNMP */
10992 gen_helper_vfp_maxnums(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10993 break;
10994 case 0x5a: /* FADDP */
10995 gen_helper_vfp_adds(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10996 break;
10997 case 0x5e: /* FMAXP */
10998 gen_helper_vfp_maxs(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10999 break;
11000 case 0x78: /* FMINNMP */
11001 gen_helper_vfp_minnums(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11002 break;
11003 case 0x7e: /* FMINP */
11004 gen_helper_vfp_mins(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11005 break;
11006 default:
11007 g_assert_not_reached();
11010 /* FP ops called directly, otherwise call now */
11011 if (genfn) {
11012 genfn(tcg_res[pass], tcg_op1, tcg_op2);
11015 tcg_temp_free_i32(tcg_op1);
11016 tcg_temp_free_i32(tcg_op2);
11019 for (pass = 0; pass < maxpass; pass++) {
11020 write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_32);
11021 tcg_temp_free_i32(tcg_res[pass]);
11023 clear_vec_high(s, is_q, rd);
11026 if (fpst) {
11027 tcg_temp_free_ptr(fpst);
11031 /* Floating point op subgroup of C3.6.16. */
11032 static void disas_simd_3same_float(DisasContext *s, uint32_t insn)
11034 /* For floating point ops, the U, size[1] and opcode bits
11035 * together indicate the operation. size[0] indicates single
11036 * or double.
11038 int fpopcode = extract32(insn, 11, 5)
11039 | (extract32(insn, 23, 1) << 5)
11040 | (extract32(insn, 29, 1) << 6);
11041 int is_q = extract32(insn, 30, 1);
11042 int size = extract32(insn, 22, 1);
11043 int rm = extract32(insn, 16, 5);
11044 int rn = extract32(insn, 5, 5);
11045 int rd = extract32(insn, 0, 5);
11047 int datasize = is_q ? 128 : 64;
11048 int esize = 32 << size;
11049 int elements = datasize / esize;
11051 if (size == 1 && !is_q) {
11052 unallocated_encoding(s);
11053 return;
11056 switch (fpopcode) {
11057 case 0x58: /* FMAXNMP */
11058 case 0x5a: /* FADDP */
11059 case 0x5e: /* FMAXP */
11060 case 0x78: /* FMINNMP */
11061 case 0x7e: /* FMINP */
11062 if (size && !is_q) {
11063 unallocated_encoding(s);
11064 return;
11066 handle_simd_3same_pair(s, is_q, 0, fpopcode, size ? MO_64 : MO_32,
11067 rn, rm, rd);
11068 return;
11069 case 0x1b: /* FMULX */
11070 case 0x1f: /* FRECPS */
11071 case 0x3f: /* FRSQRTS */
11072 case 0x5d: /* FACGE */
11073 case 0x7d: /* FACGT */
11074 case 0x19: /* FMLA */
11075 case 0x39: /* FMLS */
11076 case 0x18: /* FMAXNM */
11077 case 0x1a: /* FADD */
11078 case 0x1c: /* FCMEQ */
11079 case 0x1e: /* FMAX */
11080 case 0x38: /* FMINNM */
11081 case 0x3a: /* FSUB */
11082 case 0x3e: /* FMIN */
11083 case 0x5b: /* FMUL */
11084 case 0x5c: /* FCMGE */
11085 case 0x5f: /* FDIV */
11086 case 0x7a: /* FABD */
11087 case 0x7c: /* FCMGT */
11088 if (!fp_access_check(s)) {
11089 return;
11091 handle_3same_float(s, size, elements, fpopcode, rd, rn, rm);
11092 return;
11094 case 0x1d: /* FMLAL */
11095 case 0x3d: /* FMLSL */
11096 case 0x59: /* FMLAL2 */
11097 case 0x79: /* FMLSL2 */
11098 if (size & 1 || !dc_isar_feature(aa64_fhm, s)) {
11099 unallocated_encoding(s);
11100 return;
11102 if (fp_access_check(s)) {
11103 int is_s = extract32(insn, 23, 1);
11104 int is_2 = extract32(insn, 29, 1);
11105 int data = (is_2 << 1) | is_s;
11106 tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
11107 vec_full_reg_offset(s, rn),
11108 vec_full_reg_offset(s, rm), cpu_env,
11109 is_q ? 16 : 8, vec_full_reg_size(s),
11110 data, gen_helper_gvec_fmlal_a64);
11112 return;
11114 default:
11115 unallocated_encoding(s);
11116 return;
11120 /* Integer op subgroup of C3.6.16. */
11121 static void disas_simd_3same_int(DisasContext *s, uint32_t insn)
11123 int is_q = extract32(insn, 30, 1);
11124 int u = extract32(insn, 29, 1);
11125 int size = extract32(insn, 22, 2);
11126 int opcode = extract32(insn, 11, 5);
11127 int rm = extract32(insn, 16, 5);
11128 int rn = extract32(insn, 5, 5);
11129 int rd = extract32(insn, 0, 5);
11130 int pass;
11131 TCGCond cond;
11133 switch (opcode) {
11134 case 0x13: /* MUL, PMUL */
11135 if (u && size != 0) {
11136 unallocated_encoding(s);
11137 return;
11139 /* fall through */
11140 case 0x0: /* SHADD, UHADD */
11141 case 0x2: /* SRHADD, URHADD */
11142 case 0x4: /* SHSUB, UHSUB */
11143 case 0xc: /* SMAX, UMAX */
11144 case 0xd: /* SMIN, UMIN */
11145 case 0xe: /* SABD, UABD */
11146 case 0xf: /* SABA, UABA */
11147 case 0x12: /* MLA, MLS */
11148 if (size == 3) {
11149 unallocated_encoding(s);
11150 return;
11152 break;
11153 case 0x16: /* SQDMULH, SQRDMULH */
11154 if (size == 0 || size == 3) {
11155 unallocated_encoding(s);
11156 return;
11158 break;
11159 default:
11160 if (size == 3 && !is_q) {
11161 unallocated_encoding(s);
11162 return;
11164 break;
11167 if (!fp_access_check(s)) {
11168 return;
11171 switch (opcode) {
11172 case 0x01: /* SQADD, UQADD */
11173 if (u) {
11174 gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uqadd_qc, size);
11175 } else {
11176 gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqadd_qc, size);
11178 return;
11179 case 0x05: /* SQSUB, UQSUB */
11180 if (u) {
11181 gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uqsub_qc, size);
11182 } else {
11183 gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqsub_qc, size);
11185 return;
11186 case 0x08: /* SSHL, USHL */
11187 if (u) {
11188 gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_ushl, size);
11189 } else {
11190 gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sshl, size);
11192 return;
11193 case 0x0c: /* SMAX, UMAX */
11194 if (u) {
11195 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_umax, size);
11196 } else {
11197 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_smax, size);
11199 return;
11200 case 0x0d: /* SMIN, UMIN */
11201 if (u) {
11202 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_umin, size);
11203 } else {
11204 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_smin, size);
11206 return;
11207 case 0xe: /* SABD, UABD */
11208 if (u) {
11209 gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uabd, size);
11210 } else {
11211 gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sabd, size);
11213 return;
11214 case 0xf: /* SABA, UABA */
11215 if (u) {
11216 gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uaba, size);
11217 } else {
11218 gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_saba, size);
11220 return;
11221 case 0x10: /* ADD, SUB */
11222 if (u) {
11223 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_sub, size);
11224 } else {
11225 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_add, size);
11227 return;
11228 case 0x13: /* MUL, PMUL */
11229 if (!u) { /* MUL */
11230 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_mul, size);
11231 } else { /* PMUL */
11232 gen_gvec_op3_ool(s, is_q, rd, rn, rm, 0, gen_helper_gvec_pmul_b);
11234 return;
11235 case 0x12: /* MLA, MLS */
11236 if (u) {
11237 gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_mls, size);
11238 } else {
11239 gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_mla, size);
11241 return;
11242 case 0x11:
11243 if (!u) { /* CMTST */
11244 gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_cmtst, size);
11245 return;
11247 /* else CMEQ */
11248 cond = TCG_COND_EQ;
11249 goto do_gvec_cmp;
11250 case 0x06: /* CMGT, CMHI */
11251 cond = u ? TCG_COND_GTU : TCG_COND_GT;
11252 goto do_gvec_cmp;
11253 case 0x07: /* CMGE, CMHS */
11254 cond = u ? TCG_COND_GEU : TCG_COND_GE;
11255 do_gvec_cmp:
11256 tcg_gen_gvec_cmp(cond, size, vec_full_reg_offset(s, rd),
11257 vec_full_reg_offset(s, rn),
11258 vec_full_reg_offset(s, rm),
11259 is_q ? 16 : 8, vec_full_reg_size(s));
11260 return;
11263 if (size == 3) {
11264 assert(is_q);
11265 for (pass = 0; pass < 2; pass++) {
11266 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
11267 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
11268 TCGv_i64 tcg_res = tcg_temp_new_i64();
11270 read_vec_element(s, tcg_op1, rn, pass, MO_64);
11271 read_vec_element(s, tcg_op2, rm, pass, MO_64);
11273 handle_3same_64(s, opcode, u, tcg_res, tcg_op1, tcg_op2);
11275 write_vec_element(s, tcg_res, rd, pass, MO_64);
11277 tcg_temp_free_i64(tcg_res);
11278 tcg_temp_free_i64(tcg_op1);
11279 tcg_temp_free_i64(tcg_op2);
11281 } else {
11282 for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
11283 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11284 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11285 TCGv_i32 tcg_res = tcg_temp_new_i32();
11286 NeonGenTwoOpFn *genfn = NULL;
11287 NeonGenTwoOpEnvFn *genenvfn = NULL;
11289 read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
11290 read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
11292 switch (opcode) {
11293 case 0x0: /* SHADD, UHADD */
11295 static NeonGenTwoOpFn * const fns[3][2] = {
11296 { gen_helper_neon_hadd_s8, gen_helper_neon_hadd_u8 },
11297 { gen_helper_neon_hadd_s16, gen_helper_neon_hadd_u16 },
11298 { gen_helper_neon_hadd_s32, gen_helper_neon_hadd_u32 },
11300 genfn = fns[size][u];
11301 break;
11303 case 0x2: /* SRHADD, URHADD */
11305 static NeonGenTwoOpFn * const fns[3][2] = {
11306 { gen_helper_neon_rhadd_s8, gen_helper_neon_rhadd_u8 },
11307 { gen_helper_neon_rhadd_s16, gen_helper_neon_rhadd_u16 },
11308 { gen_helper_neon_rhadd_s32, gen_helper_neon_rhadd_u32 },
11310 genfn = fns[size][u];
11311 break;
11313 case 0x4: /* SHSUB, UHSUB */
11315 static NeonGenTwoOpFn * const fns[3][2] = {
11316 { gen_helper_neon_hsub_s8, gen_helper_neon_hsub_u8 },
11317 { gen_helper_neon_hsub_s16, gen_helper_neon_hsub_u16 },
11318 { gen_helper_neon_hsub_s32, gen_helper_neon_hsub_u32 },
11320 genfn = fns[size][u];
11321 break;
11323 case 0x9: /* SQSHL, UQSHL */
11325 static NeonGenTwoOpEnvFn * const fns[3][2] = {
11326 { gen_helper_neon_qshl_s8, gen_helper_neon_qshl_u8 },
11327 { gen_helper_neon_qshl_s16, gen_helper_neon_qshl_u16 },
11328 { gen_helper_neon_qshl_s32, gen_helper_neon_qshl_u32 },
11330 genenvfn = fns[size][u];
11331 break;
11333 case 0xa: /* SRSHL, URSHL */
11335 static NeonGenTwoOpFn * const fns[3][2] = {
11336 { gen_helper_neon_rshl_s8, gen_helper_neon_rshl_u8 },
11337 { gen_helper_neon_rshl_s16, gen_helper_neon_rshl_u16 },
11338 { gen_helper_neon_rshl_s32, gen_helper_neon_rshl_u32 },
11340 genfn = fns[size][u];
11341 break;
11343 case 0xb: /* SQRSHL, UQRSHL */
11345 static NeonGenTwoOpEnvFn * const fns[3][2] = {
11346 { gen_helper_neon_qrshl_s8, gen_helper_neon_qrshl_u8 },
11347 { gen_helper_neon_qrshl_s16, gen_helper_neon_qrshl_u16 },
11348 { gen_helper_neon_qrshl_s32, gen_helper_neon_qrshl_u32 },
11350 genenvfn = fns[size][u];
11351 break;
11353 case 0x16: /* SQDMULH, SQRDMULH */
11355 static NeonGenTwoOpEnvFn * const fns[2][2] = {
11356 { gen_helper_neon_qdmulh_s16, gen_helper_neon_qrdmulh_s16 },
11357 { gen_helper_neon_qdmulh_s32, gen_helper_neon_qrdmulh_s32 },
11359 assert(size == 1 || size == 2);
11360 genenvfn = fns[size - 1][u];
11361 break;
11363 default:
11364 g_assert_not_reached();
11367 if (genenvfn) {
11368 genenvfn(tcg_res, cpu_env, tcg_op1, tcg_op2);
11369 } else {
11370 genfn(tcg_res, tcg_op1, tcg_op2);
11373 if (opcode == 0xf) {
11374 /* SABA, UABA: accumulating ops */
11375 static NeonGenTwoOpFn * const fns[3] = {
11376 gen_helper_neon_add_u8,
11377 gen_helper_neon_add_u16,
11378 tcg_gen_add_i32,
11381 read_vec_element_i32(s, tcg_op1, rd, pass, MO_32);
11382 fns[size](tcg_res, tcg_op1, tcg_res);
11385 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
11387 tcg_temp_free_i32(tcg_res);
11388 tcg_temp_free_i32(tcg_op1);
11389 tcg_temp_free_i32(tcg_op2);
11392 clear_vec_high(s, is_q, rd);
11395 /* AdvSIMD three same
11396 * 31 30 29 28 24 23 22 21 20 16 15 11 10 9 5 4 0
11397 * +---+---+---+-----------+------+---+------+--------+---+------+------+
11398 * | 0 | Q | U | 0 1 1 1 0 | size | 1 | Rm | opcode | 1 | Rn | Rd |
11399 * +---+---+---+-----------+------+---+------+--------+---+------+------+
11401 static void disas_simd_three_reg_same(DisasContext *s, uint32_t insn)
11403 int opcode = extract32(insn, 11, 5);
11405 switch (opcode) {
11406 case 0x3: /* logic ops */
11407 disas_simd_3same_logic(s, insn);
11408 break;
11409 case 0x17: /* ADDP */
11410 case 0x14: /* SMAXP, UMAXP */
11411 case 0x15: /* SMINP, UMINP */
11413 /* Pairwise operations */
11414 int is_q = extract32(insn, 30, 1);
11415 int u = extract32(insn, 29, 1);
11416 int size = extract32(insn, 22, 2);
11417 int rm = extract32(insn, 16, 5);
11418 int rn = extract32(insn, 5, 5);
11419 int rd = extract32(insn, 0, 5);
11420 if (opcode == 0x17) {
11421 if (u || (size == 3 && !is_q)) {
11422 unallocated_encoding(s);
11423 return;
11425 } else {
11426 if (size == 3) {
11427 unallocated_encoding(s);
11428 return;
11431 handle_simd_3same_pair(s, is_q, u, opcode, size, rn, rm, rd);
11432 break;
11434 case 0x18 ... 0x31:
11435 /* floating point ops, sz[1] and U are part of opcode */
11436 disas_simd_3same_float(s, insn);
11437 break;
11438 default:
11439 disas_simd_3same_int(s, insn);
11440 break;
11445 * Advanced SIMD three same (ARMv8.2 FP16 variants)
11447 * 31 30 29 28 24 23 22 21 20 16 15 14 13 11 10 9 5 4 0
11448 * +---+---+---+-----------+---------+------+-----+--------+---+------+------+
11449 * | 0 | Q | U | 0 1 1 1 0 | a | 1 0 | Rm | 0 0 | opcode | 1 | Rn | Rd |
11450 * +---+---+---+-----------+---------+------+-----+--------+---+------+------+
11452 * This includes FMULX, FCMEQ (register), FRECPS, FRSQRTS, FCMGE
11453 * (register), FACGE, FABD, FCMGT (register) and FACGT.
11456 static void disas_simd_three_reg_same_fp16(DisasContext *s, uint32_t insn)
11458 int opcode, fpopcode;
11459 int is_q, u, a, rm, rn, rd;
11460 int datasize, elements;
11461 int pass;
11462 TCGv_ptr fpst;
11463 bool pairwise = false;
11465 if (!dc_isar_feature(aa64_fp16, s)) {
11466 unallocated_encoding(s);
11467 return;
11470 if (!fp_access_check(s)) {
11471 return;
11474 /* For these floating point ops, the U, a and opcode bits
11475 * together indicate the operation.
11477 opcode = extract32(insn, 11, 3);
11478 u = extract32(insn, 29, 1);
11479 a = extract32(insn, 23, 1);
11480 is_q = extract32(insn, 30, 1);
11481 rm = extract32(insn, 16, 5);
11482 rn = extract32(insn, 5, 5);
11483 rd = extract32(insn, 0, 5);
11485 fpopcode = opcode | (a << 3) | (u << 4);
11486 datasize = is_q ? 128 : 64;
11487 elements = datasize / 16;
11489 switch (fpopcode) {
11490 case 0x10: /* FMAXNMP */
11491 case 0x12: /* FADDP */
11492 case 0x16: /* FMAXP */
11493 case 0x18: /* FMINNMP */
11494 case 0x1e: /* FMINP */
11495 pairwise = true;
11496 break;
11499 fpst = get_fpstatus_ptr(true);
11501 if (pairwise) {
11502 int maxpass = is_q ? 8 : 4;
11503 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11504 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11505 TCGv_i32 tcg_res[8];
11507 for (pass = 0; pass < maxpass; pass++) {
11508 int passreg = pass < (maxpass / 2) ? rn : rm;
11509 int passelt = (pass << 1) & (maxpass - 1);
11511 read_vec_element_i32(s, tcg_op1, passreg, passelt, MO_16);
11512 read_vec_element_i32(s, tcg_op2, passreg, passelt + 1, MO_16);
11513 tcg_res[pass] = tcg_temp_new_i32();
11515 switch (fpopcode) {
11516 case 0x10: /* FMAXNMP */
11517 gen_helper_advsimd_maxnumh(tcg_res[pass], tcg_op1, tcg_op2,
11518 fpst);
11519 break;
11520 case 0x12: /* FADDP */
11521 gen_helper_advsimd_addh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11522 break;
11523 case 0x16: /* FMAXP */
11524 gen_helper_advsimd_maxh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11525 break;
11526 case 0x18: /* FMINNMP */
11527 gen_helper_advsimd_minnumh(tcg_res[pass], tcg_op1, tcg_op2,
11528 fpst);
11529 break;
11530 case 0x1e: /* FMINP */
11531 gen_helper_advsimd_minh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11532 break;
11533 default:
11534 g_assert_not_reached();
11538 for (pass = 0; pass < maxpass; pass++) {
11539 write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_16);
11540 tcg_temp_free_i32(tcg_res[pass]);
11543 tcg_temp_free_i32(tcg_op1);
11544 tcg_temp_free_i32(tcg_op2);
11546 } else {
11547 for (pass = 0; pass < elements; pass++) {
11548 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11549 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11550 TCGv_i32 tcg_res = tcg_temp_new_i32();
11552 read_vec_element_i32(s, tcg_op1, rn, pass, MO_16);
11553 read_vec_element_i32(s, tcg_op2, rm, pass, MO_16);
11555 switch (fpopcode) {
11556 case 0x0: /* FMAXNM */
11557 gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
11558 break;
11559 case 0x1: /* FMLA */
11560 read_vec_element_i32(s, tcg_res, rd, pass, MO_16);
11561 gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_res,
11562 fpst);
11563 break;
11564 case 0x2: /* FADD */
11565 gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
11566 break;
11567 case 0x3: /* FMULX */
11568 gen_helper_advsimd_mulxh(tcg_res, tcg_op1, tcg_op2, fpst);
11569 break;
11570 case 0x4: /* FCMEQ */
11571 gen_helper_advsimd_ceq_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11572 break;
11573 case 0x6: /* FMAX */
11574 gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
11575 break;
11576 case 0x7: /* FRECPS */
11577 gen_helper_recpsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11578 break;
11579 case 0x8: /* FMINNM */
11580 gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
11581 break;
11582 case 0x9: /* FMLS */
11583 /* As usual for ARM, separate negation for fused multiply-add */
11584 tcg_gen_xori_i32(tcg_op1, tcg_op1, 0x8000);
11585 read_vec_element_i32(s, tcg_res, rd, pass, MO_16);
11586 gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_res,
11587 fpst);
11588 break;
11589 case 0xa: /* FSUB */
11590 gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
11591 break;
11592 case 0xe: /* FMIN */
11593 gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
11594 break;
11595 case 0xf: /* FRSQRTS */
11596 gen_helper_rsqrtsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11597 break;
11598 case 0x13: /* FMUL */
11599 gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
11600 break;
11601 case 0x14: /* FCMGE */
11602 gen_helper_advsimd_cge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11603 break;
11604 case 0x15: /* FACGE */
11605 gen_helper_advsimd_acge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11606 break;
11607 case 0x17: /* FDIV */
11608 gen_helper_advsimd_divh(tcg_res, tcg_op1, tcg_op2, fpst);
11609 break;
11610 case 0x1a: /* FABD */
11611 gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
11612 tcg_gen_andi_i32(tcg_res, tcg_res, 0x7fff);
11613 break;
11614 case 0x1c: /* FCMGT */
11615 gen_helper_advsimd_cgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11616 break;
11617 case 0x1d: /* FACGT */
11618 gen_helper_advsimd_acgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11619 break;
11620 default:
11621 fprintf(stderr, "%s: insn %#04x, fpop %#2x @ %#" PRIx64 "\n",
11622 __func__, insn, fpopcode, s->pc_curr);
11623 g_assert_not_reached();
11626 write_vec_element_i32(s, tcg_res, rd, pass, MO_16);
11627 tcg_temp_free_i32(tcg_res);
11628 tcg_temp_free_i32(tcg_op1);
11629 tcg_temp_free_i32(tcg_op2);
11633 tcg_temp_free_ptr(fpst);
11635 clear_vec_high(s, is_q, rd);
11638 /* AdvSIMD three same extra
11639 * 31 30 29 28 24 23 22 21 20 16 15 14 11 10 9 5 4 0
11640 * +---+---+---+-----------+------+---+------+---+--------+---+----+----+
11641 * | 0 | Q | U | 0 1 1 1 0 | size | 0 | Rm | 1 | opcode | 1 | Rn | Rd |
11642 * +---+---+---+-----------+------+---+------+---+--------+---+----+----+
11644 static void disas_simd_three_reg_same_extra(DisasContext *s, uint32_t insn)
11646 int rd = extract32(insn, 0, 5);
11647 int rn = extract32(insn, 5, 5);
11648 int opcode = extract32(insn, 11, 4);
11649 int rm = extract32(insn, 16, 5);
11650 int size = extract32(insn, 22, 2);
11651 bool u = extract32(insn, 29, 1);
11652 bool is_q = extract32(insn, 30, 1);
11653 bool feature;
11654 int rot;
11656 switch (u * 16 + opcode) {
11657 case 0x10: /* SQRDMLAH (vector) */
11658 case 0x11: /* SQRDMLSH (vector) */
11659 if (size != 1 && size != 2) {
11660 unallocated_encoding(s);
11661 return;
11663 feature = dc_isar_feature(aa64_rdm, s);
11664 break;
11665 case 0x02: /* SDOT (vector) */
11666 case 0x12: /* UDOT (vector) */
11667 if (size != MO_32) {
11668 unallocated_encoding(s);
11669 return;
11671 feature = dc_isar_feature(aa64_dp, s);
11672 break;
11673 case 0x18: /* FCMLA, #0 */
11674 case 0x19: /* FCMLA, #90 */
11675 case 0x1a: /* FCMLA, #180 */
11676 case 0x1b: /* FCMLA, #270 */
11677 case 0x1c: /* FCADD, #90 */
11678 case 0x1e: /* FCADD, #270 */
11679 if (size == 0
11680 || (size == 1 && !dc_isar_feature(aa64_fp16, s))
11681 || (size == 3 && !is_q)) {
11682 unallocated_encoding(s);
11683 return;
11685 feature = dc_isar_feature(aa64_fcma, s);
11686 break;
11687 default:
11688 unallocated_encoding(s);
11689 return;
11691 if (!feature) {
11692 unallocated_encoding(s);
11693 return;
11695 if (!fp_access_check(s)) {
11696 return;
11699 switch (opcode) {
11700 case 0x0: /* SQRDMLAH (vector) */
11701 gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqrdmlah_qc, size);
11702 return;
11704 case 0x1: /* SQRDMLSH (vector) */
11705 gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqrdmlsh_qc, size);
11706 return;
11708 case 0x2: /* SDOT / UDOT */
11709 gen_gvec_op3_ool(s, is_q, rd, rn, rm, 0,
11710 u ? gen_helper_gvec_udot_b : gen_helper_gvec_sdot_b);
11711 return;
11713 case 0x8: /* FCMLA, #0 */
11714 case 0x9: /* FCMLA, #90 */
11715 case 0xa: /* FCMLA, #180 */
11716 case 0xb: /* FCMLA, #270 */
11717 rot = extract32(opcode, 0, 2);
11718 switch (size) {
11719 case 1:
11720 gen_gvec_op3_fpst(s, is_q, rd, rn, rm, true, rot,
11721 gen_helper_gvec_fcmlah);
11722 break;
11723 case 2:
11724 gen_gvec_op3_fpst(s, is_q, rd, rn, rm, false, rot,
11725 gen_helper_gvec_fcmlas);
11726 break;
11727 case 3:
11728 gen_gvec_op3_fpst(s, is_q, rd, rn, rm, false, rot,
11729 gen_helper_gvec_fcmlad);
11730 break;
11731 default:
11732 g_assert_not_reached();
11734 return;
11736 case 0xc: /* FCADD, #90 */
11737 case 0xe: /* FCADD, #270 */
11738 rot = extract32(opcode, 1, 1);
11739 switch (size) {
11740 case 1:
11741 gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
11742 gen_helper_gvec_fcaddh);
11743 break;
11744 case 2:
11745 gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
11746 gen_helper_gvec_fcadds);
11747 break;
11748 case 3:
11749 gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
11750 gen_helper_gvec_fcaddd);
11751 break;
11752 default:
11753 g_assert_not_reached();
11755 return;
11757 default:
11758 g_assert_not_reached();
11762 static void handle_2misc_widening(DisasContext *s, int opcode, bool is_q,
11763 int size, int rn, int rd)
11765 /* Handle 2-reg-misc ops which are widening (so each size element
11766 * in the source becomes a 2*size element in the destination.
11767 * The only instruction like this is FCVTL.
11769 int pass;
11771 if (size == 3) {
11772 /* 32 -> 64 bit fp conversion */
11773 TCGv_i64 tcg_res[2];
11774 int srcelt = is_q ? 2 : 0;
11776 for (pass = 0; pass < 2; pass++) {
11777 TCGv_i32 tcg_op = tcg_temp_new_i32();
11778 tcg_res[pass] = tcg_temp_new_i64();
11780 read_vec_element_i32(s, tcg_op, rn, srcelt + pass, MO_32);
11781 gen_helper_vfp_fcvtds(tcg_res[pass], tcg_op, cpu_env);
11782 tcg_temp_free_i32(tcg_op);
11784 for (pass = 0; pass < 2; pass++) {
11785 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
11786 tcg_temp_free_i64(tcg_res[pass]);
11788 } else {
11789 /* 16 -> 32 bit fp conversion */
11790 int srcelt = is_q ? 4 : 0;
11791 TCGv_i32 tcg_res[4];
11792 TCGv_ptr fpst = get_fpstatus_ptr(false);
11793 TCGv_i32 ahp = get_ahp_flag();
11795 for (pass = 0; pass < 4; pass++) {
11796 tcg_res[pass] = tcg_temp_new_i32();
11798 read_vec_element_i32(s, tcg_res[pass], rn, srcelt + pass, MO_16);
11799 gen_helper_vfp_fcvt_f16_to_f32(tcg_res[pass], tcg_res[pass],
11800 fpst, ahp);
11802 for (pass = 0; pass < 4; pass++) {
11803 write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_32);
11804 tcg_temp_free_i32(tcg_res[pass]);
11807 tcg_temp_free_ptr(fpst);
11808 tcg_temp_free_i32(ahp);
11812 static void handle_rev(DisasContext *s, int opcode, bool u,
11813 bool is_q, int size, int rn, int rd)
11815 int op = (opcode << 1) | u;
11816 int opsz = op + size;
11817 int grp_size = 3 - opsz;
11818 int dsize = is_q ? 128 : 64;
11819 int i;
11821 if (opsz >= 3) {
11822 unallocated_encoding(s);
11823 return;
11826 if (!fp_access_check(s)) {
11827 return;
11830 if (size == 0) {
11831 /* Special case bytes, use bswap op on each group of elements */
11832 int groups = dsize / (8 << grp_size);
11834 for (i = 0; i < groups; i++) {
11835 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
11837 read_vec_element(s, tcg_tmp, rn, i, grp_size);
11838 switch (grp_size) {
11839 case MO_16:
11840 tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp);
11841 break;
11842 case MO_32:
11843 tcg_gen_bswap32_i64(tcg_tmp, tcg_tmp);
11844 break;
11845 case MO_64:
11846 tcg_gen_bswap64_i64(tcg_tmp, tcg_tmp);
11847 break;
11848 default:
11849 g_assert_not_reached();
11851 write_vec_element(s, tcg_tmp, rd, i, grp_size);
11852 tcg_temp_free_i64(tcg_tmp);
11854 clear_vec_high(s, is_q, rd);
11855 } else {
11856 int revmask = (1 << grp_size) - 1;
11857 int esize = 8 << size;
11858 int elements = dsize / esize;
11859 TCGv_i64 tcg_rn = tcg_temp_new_i64();
11860 TCGv_i64 tcg_rd = tcg_const_i64(0);
11861 TCGv_i64 tcg_rd_hi = tcg_const_i64(0);
11863 for (i = 0; i < elements; i++) {
11864 int e_rev = (i & 0xf) ^ revmask;
11865 int off = e_rev * esize;
11866 read_vec_element(s, tcg_rn, rn, i, size);
11867 if (off >= 64) {
11868 tcg_gen_deposit_i64(tcg_rd_hi, tcg_rd_hi,
11869 tcg_rn, off - 64, esize);
11870 } else {
11871 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, off, esize);
11874 write_vec_element(s, tcg_rd, rd, 0, MO_64);
11875 write_vec_element(s, tcg_rd_hi, rd, 1, MO_64);
11877 tcg_temp_free_i64(tcg_rd_hi);
11878 tcg_temp_free_i64(tcg_rd);
11879 tcg_temp_free_i64(tcg_rn);
11883 static void handle_2misc_pairwise(DisasContext *s, int opcode, bool u,
11884 bool is_q, int size, int rn, int rd)
11886 /* Implement the pairwise operations from 2-misc:
11887 * SADDLP, UADDLP, SADALP, UADALP.
11888 * These all add pairs of elements in the input to produce a
11889 * double-width result element in the output (possibly accumulating).
11891 bool accum = (opcode == 0x6);
11892 int maxpass = is_q ? 2 : 1;
11893 int pass;
11894 TCGv_i64 tcg_res[2];
11896 if (size == 2) {
11897 /* 32 + 32 -> 64 op */
11898 MemOp memop = size + (u ? 0 : MO_SIGN);
11900 for (pass = 0; pass < maxpass; pass++) {
11901 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
11902 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
11904 tcg_res[pass] = tcg_temp_new_i64();
11906 read_vec_element(s, tcg_op1, rn, pass * 2, memop);
11907 read_vec_element(s, tcg_op2, rn, pass * 2 + 1, memop);
11908 tcg_gen_add_i64(tcg_res[pass], tcg_op1, tcg_op2);
11909 if (accum) {
11910 read_vec_element(s, tcg_op1, rd, pass, MO_64);
11911 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
11914 tcg_temp_free_i64(tcg_op1);
11915 tcg_temp_free_i64(tcg_op2);
11917 } else {
11918 for (pass = 0; pass < maxpass; pass++) {
11919 TCGv_i64 tcg_op = tcg_temp_new_i64();
11920 NeonGenOne64OpFn *genfn;
11921 static NeonGenOne64OpFn * const fns[2][2] = {
11922 { gen_helper_neon_addlp_s8, gen_helper_neon_addlp_u8 },
11923 { gen_helper_neon_addlp_s16, gen_helper_neon_addlp_u16 },
11926 genfn = fns[size][u];
11928 tcg_res[pass] = tcg_temp_new_i64();
11930 read_vec_element(s, tcg_op, rn, pass, MO_64);
11931 genfn(tcg_res[pass], tcg_op);
11933 if (accum) {
11934 read_vec_element(s, tcg_op, rd, pass, MO_64);
11935 if (size == 0) {
11936 gen_helper_neon_addl_u16(tcg_res[pass],
11937 tcg_res[pass], tcg_op);
11938 } else {
11939 gen_helper_neon_addl_u32(tcg_res[pass],
11940 tcg_res[pass], tcg_op);
11943 tcg_temp_free_i64(tcg_op);
11946 if (!is_q) {
11947 tcg_res[1] = tcg_const_i64(0);
11949 for (pass = 0; pass < 2; pass++) {
11950 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
11951 tcg_temp_free_i64(tcg_res[pass]);
11955 static void handle_shll(DisasContext *s, bool is_q, int size, int rn, int rd)
11957 /* Implement SHLL and SHLL2 */
11958 int pass;
11959 int part = is_q ? 2 : 0;
11960 TCGv_i64 tcg_res[2];
11962 for (pass = 0; pass < 2; pass++) {
11963 static NeonGenWidenFn * const widenfns[3] = {
11964 gen_helper_neon_widen_u8,
11965 gen_helper_neon_widen_u16,
11966 tcg_gen_extu_i32_i64,
11968 NeonGenWidenFn *widenfn = widenfns[size];
11969 TCGv_i32 tcg_op = tcg_temp_new_i32();
11971 read_vec_element_i32(s, tcg_op, rn, part + pass, MO_32);
11972 tcg_res[pass] = tcg_temp_new_i64();
11973 widenfn(tcg_res[pass], tcg_op);
11974 tcg_gen_shli_i64(tcg_res[pass], tcg_res[pass], 8 << size);
11976 tcg_temp_free_i32(tcg_op);
11979 for (pass = 0; pass < 2; pass++) {
11980 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
11981 tcg_temp_free_i64(tcg_res[pass]);
11985 /* AdvSIMD two reg misc
11986 * 31 30 29 28 24 23 22 21 17 16 12 11 10 9 5 4 0
11987 * +---+---+---+-----------+------+-----------+--------+-----+------+------+
11988 * | 0 | Q | U | 0 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 | Rn | Rd |
11989 * +---+---+---+-----------+------+-----------+--------+-----+------+------+
11991 static void disas_simd_two_reg_misc(DisasContext *s, uint32_t insn)
11993 int size = extract32(insn, 22, 2);
11994 int opcode = extract32(insn, 12, 5);
11995 bool u = extract32(insn, 29, 1);
11996 bool is_q = extract32(insn, 30, 1);
11997 int rn = extract32(insn, 5, 5);
11998 int rd = extract32(insn, 0, 5);
11999 bool need_fpstatus = false;
12000 bool need_rmode = false;
12001 int rmode = -1;
12002 TCGv_i32 tcg_rmode;
12003 TCGv_ptr tcg_fpstatus;
12005 switch (opcode) {
12006 case 0x0: /* REV64, REV32 */
12007 case 0x1: /* REV16 */
12008 handle_rev(s, opcode, u, is_q, size, rn, rd);
12009 return;
12010 case 0x5: /* CNT, NOT, RBIT */
12011 if (u && size == 0) {
12012 /* NOT */
12013 break;
12014 } else if (u && size == 1) {
12015 /* RBIT */
12016 break;
12017 } else if (!u && size == 0) {
12018 /* CNT */
12019 break;
12021 unallocated_encoding(s);
12022 return;
12023 case 0x12: /* XTN, XTN2, SQXTUN, SQXTUN2 */
12024 case 0x14: /* SQXTN, SQXTN2, UQXTN, UQXTN2 */
12025 if (size == 3) {
12026 unallocated_encoding(s);
12027 return;
12029 if (!fp_access_check(s)) {
12030 return;
12033 handle_2misc_narrow(s, false, opcode, u, is_q, size, rn, rd);
12034 return;
12035 case 0x4: /* CLS, CLZ */
12036 if (size == 3) {
12037 unallocated_encoding(s);
12038 return;
12040 break;
12041 case 0x2: /* SADDLP, UADDLP */
12042 case 0x6: /* SADALP, UADALP */
12043 if (size == 3) {
12044 unallocated_encoding(s);
12045 return;
12047 if (!fp_access_check(s)) {
12048 return;
12050 handle_2misc_pairwise(s, opcode, u, is_q, size, rn, rd);
12051 return;
12052 case 0x13: /* SHLL, SHLL2 */
12053 if (u == 0 || size == 3) {
12054 unallocated_encoding(s);
12055 return;
12057 if (!fp_access_check(s)) {
12058 return;
12060 handle_shll(s, is_q, size, rn, rd);
12061 return;
12062 case 0xa: /* CMLT */
12063 if (u == 1) {
12064 unallocated_encoding(s);
12065 return;
12067 /* fall through */
12068 case 0x8: /* CMGT, CMGE */
12069 case 0x9: /* CMEQ, CMLE */
12070 case 0xb: /* ABS, NEG */
12071 if (size == 3 && !is_q) {
12072 unallocated_encoding(s);
12073 return;
12075 break;
12076 case 0x3: /* SUQADD, USQADD */
12077 if (size == 3 && !is_q) {
12078 unallocated_encoding(s);
12079 return;
12081 if (!fp_access_check(s)) {
12082 return;
12084 handle_2misc_satacc(s, false, u, is_q, size, rn, rd);
12085 return;
12086 case 0x7: /* SQABS, SQNEG */
12087 if (size == 3 && !is_q) {
12088 unallocated_encoding(s);
12089 return;
12091 break;
12092 case 0xc ... 0xf:
12093 case 0x16 ... 0x1f:
12095 /* Floating point: U, size[1] and opcode indicate operation;
12096 * size[0] indicates single or double precision.
12098 int is_double = extract32(size, 0, 1);
12099 opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
12100 size = is_double ? 3 : 2;
12101 switch (opcode) {
12102 case 0x2f: /* FABS */
12103 case 0x6f: /* FNEG */
12104 if (size == 3 && !is_q) {
12105 unallocated_encoding(s);
12106 return;
12108 break;
12109 case 0x1d: /* SCVTF */
12110 case 0x5d: /* UCVTF */
12112 bool is_signed = (opcode == 0x1d) ? true : false;
12113 int elements = is_double ? 2 : is_q ? 4 : 2;
12114 if (is_double && !is_q) {
12115 unallocated_encoding(s);
12116 return;
12118 if (!fp_access_check(s)) {
12119 return;
12121 handle_simd_intfp_conv(s, rd, rn, elements, is_signed, 0, size);
12122 return;
12124 case 0x2c: /* FCMGT (zero) */
12125 case 0x2d: /* FCMEQ (zero) */
12126 case 0x2e: /* FCMLT (zero) */
12127 case 0x6c: /* FCMGE (zero) */
12128 case 0x6d: /* FCMLE (zero) */
12129 if (size == 3 && !is_q) {
12130 unallocated_encoding(s);
12131 return;
12133 handle_2misc_fcmp_zero(s, opcode, false, u, is_q, size, rn, rd);
12134 return;
12135 case 0x7f: /* FSQRT */
12136 if (size == 3 && !is_q) {
12137 unallocated_encoding(s);
12138 return;
12140 break;
12141 case 0x1a: /* FCVTNS */
12142 case 0x1b: /* FCVTMS */
12143 case 0x3a: /* FCVTPS */
12144 case 0x3b: /* FCVTZS */
12145 case 0x5a: /* FCVTNU */
12146 case 0x5b: /* FCVTMU */
12147 case 0x7a: /* FCVTPU */
12148 case 0x7b: /* FCVTZU */
12149 need_fpstatus = true;
12150 need_rmode = true;
12151 rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
12152 if (size == 3 && !is_q) {
12153 unallocated_encoding(s);
12154 return;
12156 break;
12157 case 0x5c: /* FCVTAU */
12158 case 0x1c: /* FCVTAS */
12159 need_fpstatus = true;
12160 need_rmode = true;
12161 rmode = FPROUNDING_TIEAWAY;
12162 if (size == 3 && !is_q) {
12163 unallocated_encoding(s);
12164 return;
12166 break;
12167 case 0x3c: /* URECPE */
12168 if (size == 3) {
12169 unallocated_encoding(s);
12170 return;
12172 /* fall through */
12173 case 0x3d: /* FRECPE */
12174 case 0x7d: /* FRSQRTE */
12175 if (size == 3 && !is_q) {
12176 unallocated_encoding(s);
12177 return;
12179 if (!fp_access_check(s)) {
12180 return;
12182 handle_2misc_reciprocal(s, opcode, false, u, is_q, size, rn, rd);
12183 return;
12184 case 0x56: /* FCVTXN, FCVTXN2 */
12185 if (size == 2) {
12186 unallocated_encoding(s);
12187 return;
12189 /* fall through */
12190 case 0x16: /* FCVTN, FCVTN2 */
12191 /* handle_2misc_narrow does a 2*size -> size operation, but these
12192 * instructions encode the source size rather than dest size.
12194 if (!fp_access_check(s)) {
12195 return;
12197 handle_2misc_narrow(s, false, opcode, 0, is_q, size - 1, rn, rd);
12198 return;
12199 case 0x17: /* FCVTL, FCVTL2 */
12200 if (!fp_access_check(s)) {
12201 return;
12203 handle_2misc_widening(s, opcode, is_q, size, rn, rd);
12204 return;
12205 case 0x18: /* FRINTN */
12206 case 0x19: /* FRINTM */
12207 case 0x38: /* FRINTP */
12208 case 0x39: /* FRINTZ */
12209 need_rmode = true;
12210 rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
12211 /* fall through */
12212 case 0x59: /* FRINTX */
12213 case 0x79: /* FRINTI */
12214 need_fpstatus = true;
12215 if (size == 3 && !is_q) {
12216 unallocated_encoding(s);
12217 return;
12219 break;
12220 case 0x58: /* FRINTA */
12221 need_rmode = true;
12222 rmode = FPROUNDING_TIEAWAY;
12223 need_fpstatus = true;
12224 if (size == 3 && !is_q) {
12225 unallocated_encoding(s);
12226 return;
12228 break;
12229 case 0x7c: /* URSQRTE */
12230 if (size == 3) {
12231 unallocated_encoding(s);
12232 return;
12234 break;
12235 case 0x1e: /* FRINT32Z */
12236 case 0x1f: /* FRINT64Z */
12237 need_rmode = true;
12238 rmode = FPROUNDING_ZERO;
12239 /* fall through */
12240 case 0x5e: /* FRINT32X */
12241 case 0x5f: /* FRINT64X */
12242 need_fpstatus = true;
12243 if ((size == 3 && !is_q) || !dc_isar_feature(aa64_frint, s)) {
12244 unallocated_encoding(s);
12245 return;
12247 break;
12248 default:
12249 unallocated_encoding(s);
12250 return;
12252 break;
12254 default:
12255 unallocated_encoding(s);
12256 return;
12259 if (!fp_access_check(s)) {
12260 return;
12263 if (need_fpstatus || need_rmode) {
12264 tcg_fpstatus = get_fpstatus_ptr(false);
12265 } else {
12266 tcg_fpstatus = NULL;
12268 if (need_rmode) {
12269 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rmode));
12270 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
12271 } else {
12272 tcg_rmode = NULL;
12275 switch (opcode) {
12276 case 0x5:
12277 if (u && size == 0) { /* NOT */
12278 gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_not, 0);
12279 return;
12281 break;
12282 case 0x8: /* CMGT, CMGE */
12283 if (u) {
12284 gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_cge0, size);
12285 } else {
12286 gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_cgt0, size);
12288 return;
12289 case 0x9: /* CMEQ, CMLE */
12290 if (u) {
12291 gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_cle0, size);
12292 } else {
12293 gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_ceq0, size);
12295 return;
12296 case 0xa: /* CMLT */
12297 gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_clt0, size);
12298 return;
12299 case 0xb:
12300 if (u) { /* ABS, NEG */
12301 gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_neg, size);
12302 } else {
12303 gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_abs, size);
12305 return;
12308 if (size == 3) {
12309 /* All 64-bit element operations can be shared with scalar 2misc */
12310 int pass;
12312 /* Coverity claims (size == 3 && !is_q) has been eliminated
12313 * from all paths leading to here.
12315 tcg_debug_assert(is_q);
12316 for (pass = 0; pass < 2; pass++) {
12317 TCGv_i64 tcg_op = tcg_temp_new_i64();
12318 TCGv_i64 tcg_res = tcg_temp_new_i64();
12320 read_vec_element(s, tcg_op, rn, pass, MO_64);
12322 handle_2misc_64(s, opcode, u, tcg_res, tcg_op,
12323 tcg_rmode, tcg_fpstatus);
12325 write_vec_element(s, tcg_res, rd, pass, MO_64);
12327 tcg_temp_free_i64(tcg_res);
12328 tcg_temp_free_i64(tcg_op);
12330 } else {
12331 int pass;
12333 for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
12334 TCGv_i32 tcg_op = tcg_temp_new_i32();
12335 TCGv_i32 tcg_res = tcg_temp_new_i32();
12337 read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
12339 if (size == 2) {
12340 /* Special cases for 32 bit elements */
12341 switch (opcode) {
12342 case 0x4: /* CLS */
12343 if (u) {
12344 tcg_gen_clzi_i32(tcg_res, tcg_op, 32);
12345 } else {
12346 tcg_gen_clrsb_i32(tcg_res, tcg_op);
12348 break;
12349 case 0x7: /* SQABS, SQNEG */
12350 if (u) {
12351 gen_helper_neon_qneg_s32(tcg_res, cpu_env, tcg_op);
12352 } else {
12353 gen_helper_neon_qabs_s32(tcg_res, cpu_env, tcg_op);
12355 break;
12356 case 0x2f: /* FABS */
12357 gen_helper_vfp_abss(tcg_res, tcg_op);
12358 break;
12359 case 0x6f: /* FNEG */
12360 gen_helper_vfp_negs(tcg_res, tcg_op);
12361 break;
12362 case 0x7f: /* FSQRT */
12363 gen_helper_vfp_sqrts(tcg_res, tcg_op, cpu_env);
12364 break;
12365 case 0x1a: /* FCVTNS */
12366 case 0x1b: /* FCVTMS */
12367 case 0x1c: /* FCVTAS */
12368 case 0x3a: /* FCVTPS */
12369 case 0x3b: /* FCVTZS */
12371 TCGv_i32 tcg_shift = tcg_const_i32(0);
12372 gen_helper_vfp_tosls(tcg_res, tcg_op,
12373 tcg_shift, tcg_fpstatus);
12374 tcg_temp_free_i32(tcg_shift);
12375 break;
12377 case 0x5a: /* FCVTNU */
12378 case 0x5b: /* FCVTMU */
12379 case 0x5c: /* FCVTAU */
12380 case 0x7a: /* FCVTPU */
12381 case 0x7b: /* FCVTZU */
12383 TCGv_i32 tcg_shift = tcg_const_i32(0);
12384 gen_helper_vfp_touls(tcg_res, tcg_op,
12385 tcg_shift, tcg_fpstatus);
12386 tcg_temp_free_i32(tcg_shift);
12387 break;
12389 case 0x18: /* FRINTN */
12390 case 0x19: /* FRINTM */
12391 case 0x38: /* FRINTP */
12392 case 0x39: /* FRINTZ */
12393 case 0x58: /* FRINTA */
12394 case 0x79: /* FRINTI */
12395 gen_helper_rints(tcg_res, tcg_op, tcg_fpstatus);
12396 break;
12397 case 0x59: /* FRINTX */
12398 gen_helper_rints_exact(tcg_res, tcg_op, tcg_fpstatus);
12399 break;
12400 case 0x7c: /* URSQRTE */
12401 gen_helper_rsqrte_u32(tcg_res, tcg_op);
12402 break;
12403 case 0x1e: /* FRINT32Z */
12404 case 0x5e: /* FRINT32X */
12405 gen_helper_frint32_s(tcg_res, tcg_op, tcg_fpstatus);
12406 break;
12407 case 0x1f: /* FRINT64Z */
12408 case 0x5f: /* FRINT64X */
12409 gen_helper_frint64_s(tcg_res, tcg_op, tcg_fpstatus);
12410 break;
12411 default:
12412 g_assert_not_reached();
12414 } else {
12415 /* Use helpers for 8 and 16 bit elements */
12416 switch (opcode) {
12417 case 0x5: /* CNT, RBIT */
12418 /* For these two insns size is part of the opcode specifier
12419 * (handled earlier); they always operate on byte elements.
12421 if (u) {
12422 gen_helper_neon_rbit_u8(tcg_res, tcg_op);
12423 } else {
12424 gen_helper_neon_cnt_u8(tcg_res, tcg_op);
12426 break;
12427 case 0x7: /* SQABS, SQNEG */
12429 NeonGenOneOpEnvFn *genfn;
12430 static NeonGenOneOpEnvFn * const fns[2][2] = {
12431 { gen_helper_neon_qabs_s8, gen_helper_neon_qneg_s8 },
12432 { gen_helper_neon_qabs_s16, gen_helper_neon_qneg_s16 },
12434 genfn = fns[size][u];
12435 genfn(tcg_res, cpu_env, tcg_op);
12436 break;
12438 case 0x4: /* CLS, CLZ */
12439 if (u) {
12440 if (size == 0) {
12441 gen_helper_neon_clz_u8(tcg_res, tcg_op);
12442 } else {
12443 gen_helper_neon_clz_u16(tcg_res, tcg_op);
12445 } else {
12446 if (size == 0) {
12447 gen_helper_neon_cls_s8(tcg_res, tcg_op);
12448 } else {
12449 gen_helper_neon_cls_s16(tcg_res, tcg_op);
12452 break;
12453 default:
12454 g_assert_not_reached();
12458 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
12460 tcg_temp_free_i32(tcg_res);
12461 tcg_temp_free_i32(tcg_op);
12464 clear_vec_high(s, is_q, rd);
12466 if (need_rmode) {
12467 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
12468 tcg_temp_free_i32(tcg_rmode);
12470 if (need_fpstatus) {
12471 tcg_temp_free_ptr(tcg_fpstatus);
12475 /* AdvSIMD [scalar] two register miscellaneous (FP16)
12477 * 31 30 29 28 27 24 23 22 21 17 16 12 11 10 9 5 4 0
12478 * +---+---+---+---+---------+---+-------------+--------+-----+------+------+
12479 * | 0 | Q | U | S | 1 1 1 0 | a | 1 1 1 1 0 0 | opcode | 1 0 | Rn | Rd |
12480 * +---+---+---+---+---------+---+-------------+--------+-----+------+------+
12481 * mask: 1000 1111 0111 1110 0000 1100 0000 0000 0x8f7e 0c00
12482 * val: 0000 1110 0111 1000 0000 1000 0000 0000 0x0e78 0800
12484 * This actually covers two groups where scalar access is governed by
12485 * bit 28. A bunch of the instructions (float to integral) only exist
12486 * in the vector form and are un-allocated for the scalar decode. Also
12487 * in the scalar decode Q is always 1.
12489 static void disas_simd_two_reg_misc_fp16(DisasContext *s, uint32_t insn)
12491 int fpop, opcode, a, u;
12492 int rn, rd;
12493 bool is_q;
12494 bool is_scalar;
12495 bool only_in_vector = false;
12497 int pass;
12498 TCGv_i32 tcg_rmode = NULL;
12499 TCGv_ptr tcg_fpstatus = NULL;
12500 bool need_rmode = false;
12501 bool need_fpst = true;
12502 int rmode;
12504 if (!dc_isar_feature(aa64_fp16, s)) {
12505 unallocated_encoding(s);
12506 return;
12509 rd = extract32(insn, 0, 5);
12510 rn = extract32(insn, 5, 5);
12512 a = extract32(insn, 23, 1);
12513 u = extract32(insn, 29, 1);
12514 is_scalar = extract32(insn, 28, 1);
12515 is_q = extract32(insn, 30, 1);
12517 opcode = extract32(insn, 12, 5);
12518 fpop = deposit32(opcode, 5, 1, a);
12519 fpop = deposit32(fpop, 6, 1, u);
12521 rd = extract32(insn, 0, 5);
12522 rn = extract32(insn, 5, 5);
12524 switch (fpop) {
12525 case 0x1d: /* SCVTF */
12526 case 0x5d: /* UCVTF */
12528 int elements;
12530 if (is_scalar) {
12531 elements = 1;
12532 } else {
12533 elements = (is_q ? 8 : 4);
12536 if (!fp_access_check(s)) {
12537 return;
12539 handle_simd_intfp_conv(s, rd, rn, elements, !u, 0, MO_16);
12540 return;
12542 break;
12543 case 0x2c: /* FCMGT (zero) */
12544 case 0x2d: /* FCMEQ (zero) */
12545 case 0x2e: /* FCMLT (zero) */
12546 case 0x6c: /* FCMGE (zero) */
12547 case 0x6d: /* FCMLE (zero) */
12548 handle_2misc_fcmp_zero(s, fpop, is_scalar, 0, is_q, MO_16, rn, rd);
12549 return;
12550 case 0x3d: /* FRECPE */
12551 case 0x3f: /* FRECPX */
12552 break;
12553 case 0x18: /* FRINTN */
12554 need_rmode = true;
12555 only_in_vector = true;
12556 rmode = FPROUNDING_TIEEVEN;
12557 break;
12558 case 0x19: /* FRINTM */
12559 need_rmode = true;
12560 only_in_vector = true;
12561 rmode = FPROUNDING_NEGINF;
12562 break;
12563 case 0x38: /* FRINTP */
12564 need_rmode = true;
12565 only_in_vector = true;
12566 rmode = FPROUNDING_POSINF;
12567 break;
12568 case 0x39: /* FRINTZ */
12569 need_rmode = true;
12570 only_in_vector = true;
12571 rmode = FPROUNDING_ZERO;
12572 break;
12573 case 0x58: /* FRINTA */
12574 need_rmode = true;
12575 only_in_vector = true;
12576 rmode = FPROUNDING_TIEAWAY;
12577 break;
12578 case 0x59: /* FRINTX */
12579 case 0x79: /* FRINTI */
12580 only_in_vector = true;
12581 /* current rounding mode */
12582 break;
12583 case 0x1a: /* FCVTNS */
12584 need_rmode = true;
12585 rmode = FPROUNDING_TIEEVEN;
12586 break;
12587 case 0x1b: /* FCVTMS */
12588 need_rmode = true;
12589 rmode = FPROUNDING_NEGINF;
12590 break;
12591 case 0x1c: /* FCVTAS */
12592 need_rmode = true;
12593 rmode = FPROUNDING_TIEAWAY;
12594 break;
12595 case 0x3a: /* FCVTPS */
12596 need_rmode = true;
12597 rmode = FPROUNDING_POSINF;
12598 break;
12599 case 0x3b: /* FCVTZS */
12600 need_rmode = true;
12601 rmode = FPROUNDING_ZERO;
12602 break;
12603 case 0x5a: /* FCVTNU */
12604 need_rmode = true;
12605 rmode = FPROUNDING_TIEEVEN;
12606 break;
12607 case 0x5b: /* FCVTMU */
12608 need_rmode = true;
12609 rmode = FPROUNDING_NEGINF;
12610 break;
12611 case 0x5c: /* FCVTAU */
12612 need_rmode = true;
12613 rmode = FPROUNDING_TIEAWAY;
12614 break;
12615 case 0x7a: /* FCVTPU */
12616 need_rmode = true;
12617 rmode = FPROUNDING_POSINF;
12618 break;
12619 case 0x7b: /* FCVTZU */
12620 need_rmode = true;
12621 rmode = FPROUNDING_ZERO;
12622 break;
12623 case 0x2f: /* FABS */
12624 case 0x6f: /* FNEG */
12625 need_fpst = false;
12626 break;
12627 case 0x7d: /* FRSQRTE */
12628 case 0x7f: /* FSQRT (vector) */
12629 break;
12630 default:
12631 fprintf(stderr, "%s: insn %#04x fpop %#2x\n", __func__, insn, fpop);
12632 g_assert_not_reached();
12636 /* Check additional constraints for the scalar encoding */
12637 if (is_scalar) {
12638 if (!is_q) {
12639 unallocated_encoding(s);
12640 return;
12642 /* FRINTxx is only in the vector form */
12643 if (only_in_vector) {
12644 unallocated_encoding(s);
12645 return;
12649 if (!fp_access_check(s)) {
12650 return;
12653 if (need_rmode || need_fpst) {
12654 tcg_fpstatus = get_fpstatus_ptr(true);
12657 if (need_rmode) {
12658 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rmode));
12659 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
12662 if (is_scalar) {
12663 TCGv_i32 tcg_op = read_fp_hreg(s, rn);
12664 TCGv_i32 tcg_res = tcg_temp_new_i32();
12666 switch (fpop) {
12667 case 0x1a: /* FCVTNS */
12668 case 0x1b: /* FCVTMS */
12669 case 0x1c: /* FCVTAS */
12670 case 0x3a: /* FCVTPS */
12671 case 0x3b: /* FCVTZS */
12672 gen_helper_advsimd_f16tosinth(tcg_res, tcg_op, tcg_fpstatus);
12673 break;
12674 case 0x3d: /* FRECPE */
12675 gen_helper_recpe_f16(tcg_res, tcg_op, tcg_fpstatus);
12676 break;
12677 case 0x3f: /* FRECPX */
12678 gen_helper_frecpx_f16(tcg_res, tcg_op, tcg_fpstatus);
12679 break;
12680 case 0x5a: /* FCVTNU */
12681 case 0x5b: /* FCVTMU */
12682 case 0x5c: /* FCVTAU */
12683 case 0x7a: /* FCVTPU */
12684 case 0x7b: /* FCVTZU */
12685 gen_helper_advsimd_f16touinth(tcg_res, tcg_op, tcg_fpstatus);
12686 break;
12687 case 0x6f: /* FNEG */
12688 tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
12689 break;
12690 case 0x7d: /* FRSQRTE */
12691 gen_helper_rsqrte_f16(tcg_res, tcg_op, tcg_fpstatus);
12692 break;
12693 default:
12694 g_assert_not_reached();
12697 /* limit any sign extension going on */
12698 tcg_gen_andi_i32(tcg_res, tcg_res, 0xffff);
12699 write_fp_sreg(s, rd, tcg_res);
12701 tcg_temp_free_i32(tcg_res);
12702 tcg_temp_free_i32(tcg_op);
12703 } else {
12704 for (pass = 0; pass < (is_q ? 8 : 4); pass++) {
12705 TCGv_i32 tcg_op = tcg_temp_new_i32();
12706 TCGv_i32 tcg_res = tcg_temp_new_i32();
12708 read_vec_element_i32(s, tcg_op, rn, pass, MO_16);
12710 switch (fpop) {
12711 case 0x1a: /* FCVTNS */
12712 case 0x1b: /* FCVTMS */
12713 case 0x1c: /* FCVTAS */
12714 case 0x3a: /* FCVTPS */
12715 case 0x3b: /* FCVTZS */
12716 gen_helper_advsimd_f16tosinth(tcg_res, tcg_op, tcg_fpstatus);
12717 break;
12718 case 0x3d: /* FRECPE */
12719 gen_helper_recpe_f16(tcg_res, tcg_op, tcg_fpstatus);
12720 break;
12721 case 0x5a: /* FCVTNU */
12722 case 0x5b: /* FCVTMU */
12723 case 0x5c: /* FCVTAU */
12724 case 0x7a: /* FCVTPU */
12725 case 0x7b: /* FCVTZU */
12726 gen_helper_advsimd_f16touinth(tcg_res, tcg_op, tcg_fpstatus);
12727 break;
12728 case 0x18: /* FRINTN */
12729 case 0x19: /* FRINTM */
12730 case 0x38: /* FRINTP */
12731 case 0x39: /* FRINTZ */
12732 case 0x58: /* FRINTA */
12733 case 0x79: /* FRINTI */
12734 gen_helper_advsimd_rinth(tcg_res, tcg_op, tcg_fpstatus);
12735 break;
12736 case 0x59: /* FRINTX */
12737 gen_helper_advsimd_rinth_exact(tcg_res, tcg_op, tcg_fpstatus);
12738 break;
12739 case 0x2f: /* FABS */
12740 tcg_gen_andi_i32(tcg_res, tcg_op, 0x7fff);
12741 break;
12742 case 0x6f: /* FNEG */
12743 tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
12744 break;
12745 case 0x7d: /* FRSQRTE */
12746 gen_helper_rsqrte_f16(tcg_res, tcg_op, tcg_fpstatus);
12747 break;
12748 case 0x7f: /* FSQRT */
12749 gen_helper_sqrt_f16(tcg_res, tcg_op, tcg_fpstatus);
12750 break;
12751 default:
12752 g_assert_not_reached();
12755 write_vec_element_i32(s, tcg_res, rd, pass, MO_16);
12757 tcg_temp_free_i32(tcg_res);
12758 tcg_temp_free_i32(tcg_op);
12761 clear_vec_high(s, is_q, rd);
12764 if (tcg_rmode) {
12765 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
12766 tcg_temp_free_i32(tcg_rmode);
12769 if (tcg_fpstatus) {
12770 tcg_temp_free_ptr(tcg_fpstatus);
12774 /* AdvSIMD scalar x indexed element
12775 * 31 30 29 28 24 23 22 21 20 19 16 15 12 11 10 9 5 4 0
12776 * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
12777 * | 0 1 | U | 1 1 1 1 1 | size | L | M | Rm | opc | H | 0 | Rn | Rd |
12778 * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
12779 * AdvSIMD vector x indexed element
12780 * 31 30 29 28 24 23 22 21 20 19 16 15 12 11 10 9 5 4 0
12781 * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
12782 * | 0 | Q | U | 0 1 1 1 1 | size | L | M | Rm | opc | H | 0 | Rn | Rd |
12783 * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
12785 static void disas_simd_indexed(DisasContext *s, uint32_t insn)
12787 /* This encoding has two kinds of instruction:
12788 * normal, where we perform elt x idxelt => elt for each
12789 * element in the vector
12790 * long, where we perform elt x idxelt and generate a result of
12791 * double the width of the input element
12792 * The long ops have a 'part' specifier (ie come in INSN, INSN2 pairs).
12794 bool is_scalar = extract32(insn, 28, 1);
12795 bool is_q = extract32(insn, 30, 1);
12796 bool u = extract32(insn, 29, 1);
12797 int size = extract32(insn, 22, 2);
12798 int l = extract32(insn, 21, 1);
12799 int m = extract32(insn, 20, 1);
12800 /* Note that the Rm field here is only 4 bits, not 5 as it usually is */
12801 int rm = extract32(insn, 16, 4);
12802 int opcode = extract32(insn, 12, 4);
12803 int h = extract32(insn, 11, 1);
12804 int rn = extract32(insn, 5, 5);
12805 int rd = extract32(insn, 0, 5);
12806 bool is_long = false;
12807 int is_fp = 0;
12808 bool is_fp16 = false;
12809 int index;
12810 TCGv_ptr fpst;
12812 switch (16 * u + opcode) {
12813 case 0x08: /* MUL */
12814 case 0x10: /* MLA */
12815 case 0x14: /* MLS */
12816 if (is_scalar) {
12817 unallocated_encoding(s);
12818 return;
12820 break;
12821 case 0x02: /* SMLAL, SMLAL2 */
12822 case 0x12: /* UMLAL, UMLAL2 */
12823 case 0x06: /* SMLSL, SMLSL2 */
12824 case 0x16: /* UMLSL, UMLSL2 */
12825 case 0x0a: /* SMULL, SMULL2 */
12826 case 0x1a: /* UMULL, UMULL2 */
12827 if (is_scalar) {
12828 unallocated_encoding(s);
12829 return;
12831 is_long = true;
12832 break;
12833 case 0x03: /* SQDMLAL, SQDMLAL2 */
12834 case 0x07: /* SQDMLSL, SQDMLSL2 */
12835 case 0x0b: /* SQDMULL, SQDMULL2 */
12836 is_long = true;
12837 break;
12838 case 0x0c: /* SQDMULH */
12839 case 0x0d: /* SQRDMULH */
12840 break;
12841 case 0x01: /* FMLA */
12842 case 0x05: /* FMLS */
12843 case 0x09: /* FMUL */
12844 case 0x19: /* FMULX */
12845 is_fp = 1;
12846 break;
12847 case 0x1d: /* SQRDMLAH */
12848 case 0x1f: /* SQRDMLSH */
12849 if (!dc_isar_feature(aa64_rdm, s)) {
12850 unallocated_encoding(s);
12851 return;
12853 break;
12854 case 0x0e: /* SDOT */
12855 case 0x1e: /* UDOT */
12856 if (is_scalar || size != MO_32 || !dc_isar_feature(aa64_dp, s)) {
12857 unallocated_encoding(s);
12858 return;
12860 break;
12861 case 0x11: /* FCMLA #0 */
12862 case 0x13: /* FCMLA #90 */
12863 case 0x15: /* FCMLA #180 */
12864 case 0x17: /* FCMLA #270 */
12865 if (is_scalar || !dc_isar_feature(aa64_fcma, s)) {
12866 unallocated_encoding(s);
12867 return;
12869 is_fp = 2;
12870 break;
12871 case 0x00: /* FMLAL */
12872 case 0x04: /* FMLSL */
12873 case 0x18: /* FMLAL2 */
12874 case 0x1c: /* FMLSL2 */
12875 if (is_scalar || size != MO_32 || !dc_isar_feature(aa64_fhm, s)) {
12876 unallocated_encoding(s);
12877 return;
12879 size = MO_16;
12880 /* is_fp, but we pass cpu_env not fp_status. */
12881 break;
12882 default:
12883 unallocated_encoding(s);
12884 return;
12887 switch (is_fp) {
12888 case 1: /* normal fp */
12889 /* convert insn encoded size to MemOp size */
12890 switch (size) {
12891 case 0: /* half-precision */
12892 size = MO_16;
12893 is_fp16 = true;
12894 break;
12895 case MO_32: /* single precision */
12896 case MO_64: /* double precision */
12897 break;
12898 default:
12899 unallocated_encoding(s);
12900 return;
12902 break;
12904 case 2: /* complex fp */
12905 /* Each indexable element is a complex pair. */
12906 size += 1;
12907 switch (size) {
12908 case MO_32:
12909 if (h && !is_q) {
12910 unallocated_encoding(s);
12911 return;
12913 is_fp16 = true;
12914 break;
12915 case MO_64:
12916 break;
12917 default:
12918 unallocated_encoding(s);
12919 return;
12921 break;
12923 default: /* integer */
12924 switch (size) {
12925 case MO_8:
12926 case MO_64:
12927 unallocated_encoding(s);
12928 return;
12930 break;
12932 if (is_fp16 && !dc_isar_feature(aa64_fp16, s)) {
12933 unallocated_encoding(s);
12934 return;
12937 /* Given MemOp size, adjust register and indexing. */
12938 switch (size) {
12939 case MO_16:
12940 index = h << 2 | l << 1 | m;
12941 break;
12942 case MO_32:
12943 index = h << 1 | l;
12944 rm |= m << 4;
12945 break;
12946 case MO_64:
12947 if (l || !is_q) {
12948 unallocated_encoding(s);
12949 return;
12951 index = h;
12952 rm |= m << 4;
12953 break;
12954 default:
12955 g_assert_not_reached();
12958 if (!fp_access_check(s)) {
12959 return;
12962 if (is_fp) {
12963 fpst = get_fpstatus_ptr(is_fp16);
12964 } else {
12965 fpst = NULL;
12968 switch (16 * u + opcode) {
12969 case 0x0e: /* SDOT */
12970 case 0x1e: /* UDOT */
12971 gen_gvec_op3_ool(s, is_q, rd, rn, rm, index,
12972 u ? gen_helper_gvec_udot_idx_b
12973 : gen_helper_gvec_sdot_idx_b);
12974 return;
12975 case 0x11: /* FCMLA #0 */
12976 case 0x13: /* FCMLA #90 */
12977 case 0x15: /* FCMLA #180 */
12978 case 0x17: /* FCMLA #270 */
12980 int rot = extract32(insn, 13, 2);
12981 int data = (index << 2) | rot;
12982 tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
12983 vec_full_reg_offset(s, rn),
12984 vec_full_reg_offset(s, rm), fpst,
12985 is_q ? 16 : 8, vec_full_reg_size(s), data,
12986 size == MO_64
12987 ? gen_helper_gvec_fcmlas_idx
12988 : gen_helper_gvec_fcmlah_idx);
12989 tcg_temp_free_ptr(fpst);
12991 return;
12993 case 0x00: /* FMLAL */
12994 case 0x04: /* FMLSL */
12995 case 0x18: /* FMLAL2 */
12996 case 0x1c: /* FMLSL2 */
12998 int is_s = extract32(opcode, 2, 1);
12999 int is_2 = u;
13000 int data = (index << 2) | (is_2 << 1) | is_s;
13001 tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
13002 vec_full_reg_offset(s, rn),
13003 vec_full_reg_offset(s, rm), cpu_env,
13004 is_q ? 16 : 8, vec_full_reg_size(s),
13005 data, gen_helper_gvec_fmlal_idx_a64);
13007 return;
13010 if (size == 3) {
13011 TCGv_i64 tcg_idx = tcg_temp_new_i64();
13012 int pass;
13014 assert(is_fp && is_q && !is_long);
13016 read_vec_element(s, tcg_idx, rm, index, MO_64);
13018 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
13019 TCGv_i64 tcg_op = tcg_temp_new_i64();
13020 TCGv_i64 tcg_res = tcg_temp_new_i64();
13022 read_vec_element(s, tcg_op, rn, pass, MO_64);
13024 switch (16 * u + opcode) {
13025 case 0x05: /* FMLS */
13026 /* As usual for ARM, separate negation for fused multiply-add */
13027 gen_helper_vfp_negd(tcg_op, tcg_op);
13028 /* fall through */
13029 case 0x01: /* FMLA */
13030 read_vec_element(s, tcg_res, rd, pass, MO_64);
13031 gen_helper_vfp_muladdd(tcg_res, tcg_op, tcg_idx, tcg_res, fpst);
13032 break;
13033 case 0x09: /* FMUL */
13034 gen_helper_vfp_muld(tcg_res, tcg_op, tcg_idx, fpst);
13035 break;
13036 case 0x19: /* FMULX */
13037 gen_helper_vfp_mulxd(tcg_res, tcg_op, tcg_idx, fpst);
13038 break;
13039 default:
13040 g_assert_not_reached();
13043 write_vec_element(s, tcg_res, rd, pass, MO_64);
13044 tcg_temp_free_i64(tcg_op);
13045 tcg_temp_free_i64(tcg_res);
13048 tcg_temp_free_i64(tcg_idx);
13049 clear_vec_high(s, !is_scalar, rd);
13050 } else if (!is_long) {
13051 /* 32 bit floating point, or 16 or 32 bit integer.
13052 * For the 16 bit scalar case we use the usual Neon helpers and
13053 * rely on the fact that 0 op 0 == 0 with no side effects.
13055 TCGv_i32 tcg_idx = tcg_temp_new_i32();
13056 int pass, maxpasses;
13058 if (is_scalar) {
13059 maxpasses = 1;
13060 } else {
13061 maxpasses = is_q ? 4 : 2;
13064 read_vec_element_i32(s, tcg_idx, rm, index, size);
13066 if (size == 1 && !is_scalar) {
13067 /* The simplest way to handle the 16x16 indexed ops is to duplicate
13068 * the index into both halves of the 32 bit tcg_idx and then use
13069 * the usual Neon helpers.
13071 tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
13074 for (pass = 0; pass < maxpasses; pass++) {
13075 TCGv_i32 tcg_op = tcg_temp_new_i32();
13076 TCGv_i32 tcg_res = tcg_temp_new_i32();
13078 read_vec_element_i32(s, tcg_op, rn, pass, is_scalar ? size : MO_32);
13080 switch (16 * u + opcode) {
13081 case 0x08: /* MUL */
13082 case 0x10: /* MLA */
13083 case 0x14: /* MLS */
13085 static NeonGenTwoOpFn * const fns[2][2] = {
13086 { gen_helper_neon_add_u16, gen_helper_neon_sub_u16 },
13087 { tcg_gen_add_i32, tcg_gen_sub_i32 },
13089 NeonGenTwoOpFn *genfn;
13090 bool is_sub = opcode == 0x4;
13092 if (size == 1) {
13093 gen_helper_neon_mul_u16(tcg_res, tcg_op, tcg_idx);
13094 } else {
13095 tcg_gen_mul_i32(tcg_res, tcg_op, tcg_idx);
13097 if (opcode == 0x8) {
13098 break;
13100 read_vec_element_i32(s, tcg_op, rd, pass, MO_32);
13101 genfn = fns[size - 1][is_sub];
13102 genfn(tcg_res, tcg_op, tcg_res);
13103 break;
13105 case 0x05: /* FMLS */
13106 case 0x01: /* FMLA */
13107 read_vec_element_i32(s, tcg_res, rd, pass,
13108 is_scalar ? size : MO_32);
13109 switch (size) {
13110 case 1:
13111 if (opcode == 0x5) {
13112 /* As usual for ARM, separate negation for fused
13113 * multiply-add */
13114 tcg_gen_xori_i32(tcg_op, tcg_op, 0x80008000);
13116 if (is_scalar) {
13117 gen_helper_advsimd_muladdh(tcg_res, tcg_op, tcg_idx,
13118 tcg_res, fpst);
13119 } else {
13120 gen_helper_advsimd_muladd2h(tcg_res, tcg_op, tcg_idx,
13121 tcg_res, fpst);
13123 break;
13124 case 2:
13125 if (opcode == 0x5) {
13126 /* As usual for ARM, separate negation for
13127 * fused multiply-add */
13128 tcg_gen_xori_i32(tcg_op, tcg_op, 0x80000000);
13130 gen_helper_vfp_muladds(tcg_res, tcg_op, tcg_idx,
13131 tcg_res, fpst);
13132 break;
13133 default:
13134 g_assert_not_reached();
13136 break;
13137 case 0x09: /* FMUL */
13138 switch (size) {
13139 case 1:
13140 if (is_scalar) {
13141 gen_helper_advsimd_mulh(tcg_res, tcg_op,
13142 tcg_idx, fpst);
13143 } else {
13144 gen_helper_advsimd_mul2h(tcg_res, tcg_op,
13145 tcg_idx, fpst);
13147 break;
13148 case 2:
13149 gen_helper_vfp_muls(tcg_res, tcg_op, tcg_idx, fpst);
13150 break;
13151 default:
13152 g_assert_not_reached();
13154 break;
13155 case 0x19: /* FMULX */
13156 switch (size) {
13157 case 1:
13158 if (is_scalar) {
13159 gen_helper_advsimd_mulxh(tcg_res, tcg_op,
13160 tcg_idx, fpst);
13161 } else {
13162 gen_helper_advsimd_mulx2h(tcg_res, tcg_op,
13163 tcg_idx, fpst);
13165 break;
13166 case 2:
13167 gen_helper_vfp_mulxs(tcg_res, tcg_op, tcg_idx, fpst);
13168 break;
13169 default:
13170 g_assert_not_reached();
13172 break;
13173 case 0x0c: /* SQDMULH */
13174 if (size == 1) {
13175 gen_helper_neon_qdmulh_s16(tcg_res, cpu_env,
13176 tcg_op, tcg_idx);
13177 } else {
13178 gen_helper_neon_qdmulh_s32(tcg_res, cpu_env,
13179 tcg_op, tcg_idx);
13181 break;
13182 case 0x0d: /* SQRDMULH */
13183 if (size == 1) {
13184 gen_helper_neon_qrdmulh_s16(tcg_res, cpu_env,
13185 tcg_op, tcg_idx);
13186 } else {
13187 gen_helper_neon_qrdmulh_s32(tcg_res, cpu_env,
13188 tcg_op, tcg_idx);
13190 break;
13191 case 0x1d: /* SQRDMLAH */
13192 read_vec_element_i32(s, tcg_res, rd, pass,
13193 is_scalar ? size : MO_32);
13194 if (size == 1) {
13195 gen_helper_neon_qrdmlah_s16(tcg_res, cpu_env,
13196 tcg_op, tcg_idx, tcg_res);
13197 } else {
13198 gen_helper_neon_qrdmlah_s32(tcg_res, cpu_env,
13199 tcg_op, tcg_idx, tcg_res);
13201 break;
13202 case 0x1f: /* SQRDMLSH */
13203 read_vec_element_i32(s, tcg_res, rd, pass,
13204 is_scalar ? size : MO_32);
13205 if (size == 1) {
13206 gen_helper_neon_qrdmlsh_s16(tcg_res, cpu_env,
13207 tcg_op, tcg_idx, tcg_res);
13208 } else {
13209 gen_helper_neon_qrdmlsh_s32(tcg_res, cpu_env,
13210 tcg_op, tcg_idx, tcg_res);
13212 break;
13213 default:
13214 g_assert_not_reached();
13217 if (is_scalar) {
13218 write_fp_sreg(s, rd, tcg_res);
13219 } else {
13220 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
13223 tcg_temp_free_i32(tcg_op);
13224 tcg_temp_free_i32(tcg_res);
13227 tcg_temp_free_i32(tcg_idx);
13228 clear_vec_high(s, is_q, rd);
13229 } else {
13230 /* long ops: 16x16->32 or 32x32->64 */
13231 TCGv_i64 tcg_res[2];
13232 int pass;
13233 bool satop = extract32(opcode, 0, 1);
13234 MemOp memop = MO_32;
13236 if (satop || !u) {
13237 memop |= MO_SIGN;
13240 if (size == 2) {
13241 TCGv_i64 tcg_idx = tcg_temp_new_i64();
13243 read_vec_element(s, tcg_idx, rm, index, memop);
13245 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
13246 TCGv_i64 tcg_op = tcg_temp_new_i64();
13247 TCGv_i64 tcg_passres;
13248 int passelt;
13250 if (is_scalar) {
13251 passelt = 0;
13252 } else {
13253 passelt = pass + (is_q * 2);
13256 read_vec_element(s, tcg_op, rn, passelt, memop);
13258 tcg_res[pass] = tcg_temp_new_i64();
13260 if (opcode == 0xa || opcode == 0xb) {
13261 /* Non-accumulating ops */
13262 tcg_passres = tcg_res[pass];
13263 } else {
13264 tcg_passres = tcg_temp_new_i64();
13267 tcg_gen_mul_i64(tcg_passres, tcg_op, tcg_idx);
13268 tcg_temp_free_i64(tcg_op);
13270 if (satop) {
13271 /* saturating, doubling */
13272 gen_helper_neon_addl_saturate_s64(tcg_passres, cpu_env,
13273 tcg_passres, tcg_passres);
13276 if (opcode == 0xa || opcode == 0xb) {
13277 continue;
13280 /* Accumulating op: handle accumulate step */
13281 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
13283 switch (opcode) {
13284 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
13285 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
13286 break;
13287 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
13288 tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
13289 break;
13290 case 0x7: /* SQDMLSL, SQDMLSL2 */
13291 tcg_gen_neg_i64(tcg_passres, tcg_passres);
13292 /* fall through */
13293 case 0x3: /* SQDMLAL, SQDMLAL2 */
13294 gen_helper_neon_addl_saturate_s64(tcg_res[pass], cpu_env,
13295 tcg_res[pass],
13296 tcg_passres);
13297 break;
13298 default:
13299 g_assert_not_reached();
13301 tcg_temp_free_i64(tcg_passres);
13303 tcg_temp_free_i64(tcg_idx);
13305 clear_vec_high(s, !is_scalar, rd);
13306 } else {
13307 TCGv_i32 tcg_idx = tcg_temp_new_i32();
13309 assert(size == 1);
13310 read_vec_element_i32(s, tcg_idx, rm, index, size);
13312 if (!is_scalar) {
13313 /* The simplest way to handle the 16x16 indexed ops is to
13314 * duplicate the index into both halves of the 32 bit tcg_idx
13315 * and then use the usual Neon helpers.
13317 tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
13320 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
13321 TCGv_i32 tcg_op = tcg_temp_new_i32();
13322 TCGv_i64 tcg_passres;
13324 if (is_scalar) {
13325 read_vec_element_i32(s, tcg_op, rn, pass, size);
13326 } else {
13327 read_vec_element_i32(s, tcg_op, rn,
13328 pass + (is_q * 2), MO_32);
13331 tcg_res[pass] = tcg_temp_new_i64();
13333 if (opcode == 0xa || opcode == 0xb) {
13334 /* Non-accumulating ops */
13335 tcg_passres = tcg_res[pass];
13336 } else {
13337 tcg_passres = tcg_temp_new_i64();
13340 if (memop & MO_SIGN) {
13341 gen_helper_neon_mull_s16(tcg_passres, tcg_op, tcg_idx);
13342 } else {
13343 gen_helper_neon_mull_u16(tcg_passres, tcg_op, tcg_idx);
13345 if (satop) {
13346 gen_helper_neon_addl_saturate_s32(tcg_passres, cpu_env,
13347 tcg_passres, tcg_passres);
13349 tcg_temp_free_i32(tcg_op);
13351 if (opcode == 0xa || opcode == 0xb) {
13352 continue;
13355 /* Accumulating op: handle accumulate step */
13356 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
13358 switch (opcode) {
13359 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
13360 gen_helper_neon_addl_u32(tcg_res[pass], tcg_res[pass],
13361 tcg_passres);
13362 break;
13363 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
13364 gen_helper_neon_subl_u32(tcg_res[pass], tcg_res[pass],
13365 tcg_passres);
13366 break;
13367 case 0x7: /* SQDMLSL, SQDMLSL2 */
13368 gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
13369 /* fall through */
13370 case 0x3: /* SQDMLAL, SQDMLAL2 */
13371 gen_helper_neon_addl_saturate_s32(tcg_res[pass], cpu_env,
13372 tcg_res[pass],
13373 tcg_passres);
13374 break;
13375 default:
13376 g_assert_not_reached();
13378 tcg_temp_free_i64(tcg_passres);
13380 tcg_temp_free_i32(tcg_idx);
13382 if (is_scalar) {
13383 tcg_gen_ext32u_i64(tcg_res[0], tcg_res[0]);
13387 if (is_scalar) {
13388 tcg_res[1] = tcg_const_i64(0);
13391 for (pass = 0; pass < 2; pass++) {
13392 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
13393 tcg_temp_free_i64(tcg_res[pass]);
13397 if (fpst) {
13398 tcg_temp_free_ptr(fpst);
13402 /* Crypto AES
13403 * 31 24 23 22 21 17 16 12 11 10 9 5 4 0
13404 * +-----------------+------+-----------+--------+-----+------+------+
13405 * | 0 1 0 0 1 1 1 0 | size | 1 0 1 0 0 | opcode | 1 0 | Rn | Rd |
13406 * +-----------------+------+-----------+--------+-----+------+------+
13408 static void disas_crypto_aes(DisasContext *s, uint32_t insn)
13410 int size = extract32(insn, 22, 2);
13411 int opcode = extract32(insn, 12, 5);
13412 int rn = extract32(insn, 5, 5);
13413 int rd = extract32(insn, 0, 5);
13414 int decrypt;
13415 gen_helper_gvec_2 *genfn2 = NULL;
13416 gen_helper_gvec_3 *genfn3 = NULL;
13418 if (!dc_isar_feature(aa64_aes, s) || size != 0) {
13419 unallocated_encoding(s);
13420 return;
13423 switch (opcode) {
13424 case 0x4: /* AESE */
13425 decrypt = 0;
13426 genfn3 = gen_helper_crypto_aese;
13427 break;
13428 case 0x6: /* AESMC */
13429 decrypt = 0;
13430 genfn2 = gen_helper_crypto_aesmc;
13431 break;
13432 case 0x5: /* AESD */
13433 decrypt = 1;
13434 genfn3 = gen_helper_crypto_aese;
13435 break;
13436 case 0x7: /* AESIMC */
13437 decrypt = 1;
13438 genfn2 = gen_helper_crypto_aesmc;
13439 break;
13440 default:
13441 unallocated_encoding(s);
13442 return;
13445 if (!fp_access_check(s)) {
13446 return;
13448 if (genfn2) {
13449 gen_gvec_op2_ool(s, true, rd, rn, decrypt, genfn2);
13450 } else {
13451 gen_gvec_op3_ool(s, true, rd, rd, rn, decrypt, genfn3);
13455 /* Crypto three-reg SHA
13456 * 31 24 23 22 21 20 16 15 14 12 11 10 9 5 4 0
13457 * +-----------------+------+---+------+---+--------+-----+------+------+
13458 * | 0 1 0 1 1 1 1 0 | size | 0 | Rm | 0 | opcode | 0 0 | Rn | Rd |
13459 * +-----------------+------+---+------+---+--------+-----+------+------+
13461 static void disas_crypto_three_reg_sha(DisasContext *s, uint32_t insn)
13463 int size = extract32(insn, 22, 2);
13464 int opcode = extract32(insn, 12, 3);
13465 int rm = extract32(insn, 16, 5);
13466 int rn = extract32(insn, 5, 5);
13467 int rd = extract32(insn, 0, 5);
13468 gen_helper_gvec_3 *genfn;
13469 bool feature;
13471 if (size != 0) {
13472 unallocated_encoding(s);
13473 return;
13476 switch (opcode) {
13477 case 0: /* SHA1C */
13478 genfn = gen_helper_crypto_sha1c;
13479 feature = dc_isar_feature(aa64_sha1, s);
13480 break;
13481 case 1: /* SHA1P */
13482 genfn = gen_helper_crypto_sha1p;
13483 feature = dc_isar_feature(aa64_sha1, s);
13484 break;
13485 case 2: /* SHA1M */
13486 genfn = gen_helper_crypto_sha1m;
13487 feature = dc_isar_feature(aa64_sha1, s);
13488 break;
13489 case 3: /* SHA1SU0 */
13490 genfn = gen_helper_crypto_sha1su0;
13491 feature = dc_isar_feature(aa64_sha1, s);
13492 break;
13493 case 4: /* SHA256H */
13494 genfn = gen_helper_crypto_sha256h;
13495 feature = dc_isar_feature(aa64_sha256, s);
13496 break;
13497 case 5: /* SHA256H2 */
13498 genfn = gen_helper_crypto_sha256h2;
13499 feature = dc_isar_feature(aa64_sha256, s);
13500 break;
13501 case 6: /* SHA256SU1 */
13502 genfn = gen_helper_crypto_sha256su1;
13503 feature = dc_isar_feature(aa64_sha256, s);
13504 break;
13505 default:
13506 unallocated_encoding(s);
13507 return;
13510 if (!feature) {
13511 unallocated_encoding(s);
13512 return;
13515 if (!fp_access_check(s)) {
13516 return;
13518 gen_gvec_op3_ool(s, true, rd, rn, rm, 0, genfn);
13521 /* Crypto two-reg SHA
13522 * 31 24 23 22 21 17 16 12 11 10 9 5 4 0
13523 * +-----------------+------+-----------+--------+-----+------+------+
13524 * | 0 1 0 1 1 1 1 0 | size | 1 0 1 0 0 | opcode | 1 0 | Rn | Rd |
13525 * +-----------------+------+-----------+--------+-----+------+------+
13527 static void disas_crypto_two_reg_sha(DisasContext *s, uint32_t insn)
13529 int size = extract32(insn, 22, 2);
13530 int opcode = extract32(insn, 12, 5);
13531 int rn = extract32(insn, 5, 5);
13532 int rd = extract32(insn, 0, 5);
13533 gen_helper_gvec_2 *genfn;
13534 bool feature;
13536 if (size != 0) {
13537 unallocated_encoding(s);
13538 return;
13541 switch (opcode) {
13542 case 0: /* SHA1H */
13543 feature = dc_isar_feature(aa64_sha1, s);
13544 genfn = gen_helper_crypto_sha1h;
13545 break;
13546 case 1: /* SHA1SU1 */
13547 feature = dc_isar_feature(aa64_sha1, s);
13548 genfn = gen_helper_crypto_sha1su1;
13549 break;
13550 case 2: /* SHA256SU0 */
13551 feature = dc_isar_feature(aa64_sha256, s);
13552 genfn = gen_helper_crypto_sha256su0;
13553 break;
13554 default:
13555 unallocated_encoding(s);
13556 return;
13559 if (!feature) {
13560 unallocated_encoding(s);
13561 return;
13564 if (!fp_access_check(s)) {
13565 return;
13567 gen_gvec_op2_ool(s, true, rd, rn, 0, genfn);
13570 static void gen_rax1_i64(TCGv_i64 d, TCGv_i64 n, TCGv_i64 m)
13572 tcg_gen_rotli_i64(d, m, 1);
13573 tcg_gen_xor_i64(d, d, n);
13576 static void gen_rax1_vec(unsigned vece, TCGv_vec d, TCGv_vec n, TCGv_vec m)
13578 tcg_gen_rotli_vec(vece, d, m, 1);
13579 tcg_gen_xor_vec(vece, d, d, n);
13582 void gen_gvec_rax1(unsigned vece, uint32_t rd_ofs, uint32_t rn_ofs,
13583 uint32_t rm_ofs, uint32_t opr_sz, uint32_t max_sz)
13585 static const TCGOpcode vecop_list[] = { INDEX_op_rotli_vec, 0 };
13586 static const GVecGen3 op = {
13587 .fni8 = gen_rax1_i64,
13588 .fniv = gen_rax1_vec,
13589 .opt_opc = vecop_list,
13590 .fno = gen_helper_crypto_rax1,
13591 .vece = MO_64,
13593 tcg_gen_gvec_3(rd_ofs, rn_ofs, rm_ofs, opr_sz, max_sz, &op);
13596 /* Crypto three-reg SHA512
13597 * 31 21 20 16 15 14 13 12 11 10 9 5 4 0
13598 * +-----------------------+------+---+---+-----+--------+------+------+
13599 * | 1 1 0 0 1 1 1 0 0 1 1 | Rm | 1 | O | 0 0 | opcode | Rn | Rd |
13600 * +-----------------------+------+---+---+-----+--------+------+------+
13602 static void disas_crypto_three_reg_sha512(DisasContext *s, uint32_t insn)
13604 int opcode = extract32(insn, 10, 2);
13605 int o = extract32(insn, 14, 1);
13606 int rm = extract32(insn, 16, 5);
13607 int rn = extract32(insn, 5, 5);
13608 int rd = extract32(insn, 0, 5);
13609 bool feature;
13610 gen_helper_gvec_3 *oolfn = NULL;
13611 GVecGen3Fn *gvecfn = NULL;
13613 if (o == 0) {
13614 switch (opcode) {
13615 case 0: /* SHA512H */
13616 feature = dc_isar_feature(aa64_sha512, s);
13617 oolfn = gen_helper_crypto_sha512h;
13618 break;
13619 case 1: /* SHA512H2 */
13620 feature = dc_isar_feature(aa64_sha512, s);
13621 oolfn = gen_helper_crypto_sha512h2;
13622 break;
13623 case 2: /* SHA512SU1 */
13624 feature = dc_isar_feature(aa64_sha512, s);
13625 oolfn = gen_helper_crypto_sha512su1;
13626 break;
13627 case 3: /* RAX1 */
13628 feature = dc_isar_feature(aa64_sha3, s);
13629 gvecfn = gen_gvec_rax1;
13630 break;
13631 default:
13632 g_assert_not_reached();
13634 } else {
13635 switch (opcode) {
13636 case 0: /* SM3PARTW1 */
13637 feature = dc_isar_feature(aa64_sm3, s);
13638 oolfn = gen_helper_crypto_sm3partw1;
13639 break;
13640 case 1: /* SM3PARTW2 */
13641 feature = dc_isar_feature(aa64_sm3, s);
13642 oolfn = gen_helper_crypto_sm3partw2;
13643 break;
13644 case 2: /* SM4EKEY */
13645 feature = dc_isar_feature(aa64_sm4, s);
13646 oolfn = gen_helper_crypto_sm4ekey;
13647 break;
13648 default:
13649 unallocated_encoding(s);
13650 return;
13654 if (!feature) {
13655 unallocated_encoding(s);
13656 return;
13659 if (!fp_access_check(s)) {
13660 return;
13663 if (oolfn) {
13664 gen_gvec_op3_ool(s, true, rd, rn, rm, 0, oolfn);
13665 } else {
13666 gen_gvec_fn3(s, true, rd, rn, rm, gvecfn, MO_64);
13670 /* Crypto two-reg SHA512
13671 * 31 12 11 10 9 5 4 0
13672 * +-----------------------------------------+--------+------+------+
13673 * | 1 1 0 0 1 1 1 0 1 1 0 0 0 0 0 0 1 0 0 0 | opcode | Rn | Rd |
13674 * +-----------------------------------------+--------+------+------+
13676 static void disas_crypto_two_reg_sha512(DisasContext *s, uint32_t insn)
13678 int opcode = extract32(insn, 10, 2);
13679 int rn = extract32(insn, 5, 5);
13680 int rd = extract32(insn, 0, 5);
13681 bool feature;
13683 switch (opcode) {
13684 case 0: /* SHA512SU0 */
13685 feature = dc_isar_feature(aa64_sha512, s);
13686 break;
13687 case 1: /* SM4E */
13688 feature = dc_isar_feature(aa64_sm4, s);
13689 break;
13690 default:
13691 unallocated_encoding(s);
13692 return;
13695 if (!feature) {
13696 unallocated_encoding(s);
13697 return;
13700 if (!fp_access_check(s)) {
13701 return;
13704 switch (opcode) {
13705 case 0: /* SHA512SU0 */
13706 gen_gvec_op2_ool(s, true, rd, rn, 0, gen_helper_crypto_sha512su0);
13707 break;
13708 case 1: /* SM4E */
13709 gen_gvec_op3_ool(s, true, rd, rd, rn, 0, gen_helper_crypto_sm4e);
13710 break;
13711 default:
13712 g_assert_not_reached();
13716 /* Crypto four-register
13717 * 31 23 22 21 20 16 15 14 10 9 5 4 0
13718 * +-------------------+-----+------+---+------+------+------+
13719 * | 1 1 0 0 1 1 1 0 0 | Op0 | Rm | 0 | Ra | Rn | Rd |
13720 * +-------------------+-----+------+---+------+------+------+
13722 static void disas_crypto_four_reg(DisasContext *s, uint32_t insn)
13724 int op0 = extract32(insn, 21, 2);
13725 int rm = extract32(insn, 16, 5);
13726 int ra = extract32(insn, 10, 5);
13727 int rn = extract32(insn, 5, 5);
13728 int rd = extract32(insn, 0, 5);
13729 bool feature;
13731 switch (op0) {
13732 case 0: /* EOR3 */
13733 case 1: /* BCAX */
13734 feature = dc_isar_feature(aa64_sha3, s);
13735 break;
13736 case 2: /* SM3SS1 */
13737 feature = dc_isar_feature(aa64_sm3, s);
13738 break;
13739 default:
13740 unallocated_encoding(s);
13741 return;
13744 if (!feature) {
13745 unallocated_encoding(s);
13746 return;
13749 if (!fp_access_check(s)) {
13750 return;
13753 if (op0 < 2) {
13754 TCGv_i64 tcg_op1, tcg_op2, tcg_op3, tcg_res[2];
13755 int pass;
13757 tcg_op1 = tcg_temp_new_i64();
13758 tcg_op2 = tcg_temp_new_i64();
13759 tcg_op3 = tcg_temp_new_i64();
13760 tcg_res[0] = tcg_temp_new_i64();
13761 tcg_res[1] = tcg_temp_new_i64();
13763 for (pass = 0; pass < 2; pass++) {
13764 read_vec_element(s, tcg_op1, rn, pass, MO_64);
13765 read_vec_element(s, tcg_op2, rm, pass, MO_64);
13766 read_vec_element(s, tcg_op3, ra, pass, MO_64);
13768 if (op0 == 0) {
13769 /* EOR3 */
13770 tcg_gen_xor_i64(tcg_res[pass], tcg_op2, tcg_op3);
13771 } else {
13772 /* BCAX */
13773 tcg_gen_andc_i64(tcg_res[pass], tcg_op2, tcg_op3);
13775 tcg_gen_xor_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
13777 write_vec_element(s, tcg_res[0], rd, 0, MO_64);
13778 write_vec_element(s, tcg_res[1], rd, 1, MO_64);
13780 tcg_temp_free_i64(tcg_op1);
13781 tcg_temp_free_i64(tcg_op2);
13782 tcg_temp_free_i64(tcg_op3);
13783 tcg_temp_free_i64(tcg_res[0]);
13784 tcg_temp_free_i64(tcg_res[1]);
13785 } else {
13786 TCGv_i32 tcg_op1, tcg_op2, tcg_op3, tcg_res, tcg_zero;
13788 tcg_op1 = tcg_temp_new_i32();
13789 tcg_op2 = tcg_temp_new_i32();
13790 tcg_op3 = tcg_temp_new_i32();
13791 tcg_res = tcg_temp_new_i32();
13792 tcg_zero = tcg_const_i32(0);
13794 read_vec_element_i32(s, tcg_op1, rn, 3, MO_32);
13795 read_vec_element_i32(s, tcg_op2, rm, 3, MO_32);
13796 read_vec_element_i32(s, tcg_op3, ra, 3, MO_32);
13798 tcg_gen_rotri_i32(tcg_res, tcg_op1, 20);
13799 tcg_gen_add_i32(tcg_res, tcg_res, tcg_op2);
13800 tcg_gen_add_i32(tcg_res, tcg_res, tcg_op3);
13801 tcg_gen_rotri_i32(tcg_res, tcg_res, 25);
13803 write_vec_element_i32(s, tcg_zero, rd, 0, MO_32);
13804 write_vec_element_i32(s, tcg_zero, rd, 1, MO_32);
13805 write_vec_element_i32(s, tcg_zero, rd, 2, MO_32);
13806 write_vec_element_i32(s, tcg_res, rd, 3, MO_32);
13808 tcg_temp_free_i32(tcg_op1);
13809 tcg_temp_free_i32(tcg_op2);
13810 tcg_temp_free_i32(tcg_op3);
13811 tcg_temp_free_i32(tcg_res);
13812 tcg_temp_free_i32(tcg_zero);
13816 /* Crypto XAR
13817 * 31 21 20 16 15 10 9 5 4 0
13818 * +-----------------------+------+--------+------+------+
13819 * | 1 1 0 0 1 1 1 0 1 0 0 | Rm | imm6 | Rn | Rd |
13820 * +-----------------------+------+--------+------+------+
13822 static void disas_crypto_xar(DisasContext *s, uint32_t insn)
13824 int rm = extract32(insn, 16, 5);
13825 int imm6 = extract32(insn, 10, 6);
13826 int rn = extract32(insn, 5, 5);
13827 int rd = extract32(insn, 0, 5);
13828 TCGv_i64 tcg_op1, tcg_op2, tcg_res[2];
13829 int pass;
13831 if (!dc_isar_feature(aa64_sha3, s)) {
13832 unallocated_encoding(s);
13833 return;
13836 if (!fp_access_check(s)) {
13837 return;
13840 tcg_op1 = tcg_temp_new_i64();
13841 tcg_op2 = tcg_temp_new_i64();
13842 tcg_res[0] = tcg_temp_new_i64();
13843 tcg_res[1] = tcg_temp_new_i64();
13845 for (pass = 0; pass < 2; pass++) {
13846 read_vec_element(s, tcg_op1, rn, pass, MO_64);
13847 read_vec_element(s, tcg_op2, rm, pass, MO_64);
13849 tcg_gen_xor_i64(tcg_res[pass], tcg_op1, tcg_op2);
13850 tcg_gen_rotri_i64(tcg_res[pass], tcg_res[pass], imm6);
13852 write_vec_element(s, tcg_res[0], rd, 0, MO_64);
13853 write_vec_element(s, tcg_res[1], rd, 1, MO_64);
13855 tcg_temp_free_i64(tcg_op1);
13856 tcg_temp_free_i64(tcg_op2);
13857 tcg_temp_free_i64(tcg_res[0]);
13858 tcg_temp_free_i64(tcg_res[1]);
13861 /* Crypto three-reg imm2
13862 * 31 21 20 16 15 14 13 12 11 10 9 5 4 0
13863 * +-----------------------+------+-----+------+--------+------+------+
13864 * | 1 1 0 0 1 1 1 0 0 1 0 | Rm | 1 0 | imm2 | opcode | Rn | Rd |
13865 * +-----------------------+------+-----+------+--------+------+------+
13867 static void disas_crypto_three_reg_imm2(DisasContext *s, uint32_t insn)
13869 static gen_helper_gvec_3 * const fns[4] = {
13870 gen_helper_crypto_sm3tt1a, gen_helper_crypto_sm3tt1b,
13871 gen_helper_crypto_sm3tt2a, gen_helper_crypto_sm3tt2b,
13873 int opcode = extract32(insn, 10, 2);
13874 int imm2 = extract32(insn, 12, 2);
13875 int rm = extract32(insn, 16, 5);
13876 int rn = extract32(insn, 5, 5);
13877 int rd = extract32(insn, 0, 5);
13879 if (!dc_isar_feature(aa64_sm3, s)) {
13880 unallocated_encoding(s);
13881 return;
13884 if (!fp_access_check(s)) {
13885 return;
13888 gen_gvec_op3_ool(s, true, rd, rn, rm, imm2, fns[opcode]);
13891 /* C3.6 Data processing - SIMD, inc Crypto
13893 * As the decode gets a little complex we are using a table based
13894 * approach for this part of the decode.
13896 static const AArch64DecodeTable data_proc_simd[] = {
13897 /* pattern , mask , fn */
13898 { 0x0e200400, 0x9f200400, disas_simd_three_reg_same },
13899 { 0x0e008400, 0x9f208400, disas_simd_three_reg_same_extra },
13900 { 0x0e200000, 0x9f200c00, disas_simd_three_reg_diff },
13901 { 0x0e200800, 0x9f3e0c00, disas_simd_two_reg_misc },
13902 { 0x0e300800, 0x9f3e0c00, disas_simd_across_lanes },
13903 { 0x0e000400, 0x9fe08400, disas_simd_copy },
13904 { 0x0f000000, 0x9f000400, disas_simd_indexed }, /* vector indexed */
13905 /* simd_mod_imm decode is a subset of simd_shift_imm, so must precede it */
13906 { 0x0f000400, 0x9ff80400, disas_simd_mod_imm },
13907 { 0x0f000400, 0x9f800400, disas_simd_shift_imm },
13908 { 0x0e000000, 0xbf208c00, disas_simd_tb },
13909 { 0x0e000800, 0xbf208c00, disas_simd_zip_trn },
13910 { 0x2e000000, 0xbf208400, disas_simd_ext },
13911 { 0x5e200400, 0xdf200400, disas_simd_scalar_three_reg_same },
13912 { 0x5e008400, 0xdf208400, disas_simd_scalar_three_reg_same_extra },
13913 { 0x5e200000, 0xdf200c00, disas_simd_scalar_three_reg_diff },
13914 { 0x5e200800, 0xdf3e0c00, disas_simd_scalar_two_reg_misc },
13915 { 0x5e300800, 0xdf3e0c00, disas_simd_scalar_pairwise },
13916 { 0x5e000400, 0xdfe08400, disas_simd_scalar_copy },
13917 { 0x5f000000, 0xdf000400, disas_simd_indexed }, /* scalar indexed */
13918 { 0x5f000400, 0xdf800400, disas_simd_scalar_shift_imm },
13919 { 0x4e280800, 0xff3e0c00, disas_crypto_aes },
13920 { 0x5e000000, 0xff208c00, disas_crypto_three_reg_sha },
13921 { 0x5e280800, 0xff3e0c00, disas_crypto_two_reg_sha },
13922 { 0xce608000, 0xffe0b000, disas_crypto_three_reg_sha512 },
13923 { 0xcec08000, 0xfffff000, disas_crypto_two_reg_sha512 },
13924 { 0xce000000, 0xff808000, disas_crypto_four_reg },
13925 { 0xce800000, 0xffe00000, disas_crypto_xar },
13926 { 0xce408000, 0xffe0c000, disas_crypto_three_reg_imm2 },
13927 { 0x0e400400, 0x9f60c400, disas_simd_three_reg_same_fp16 },
13928 { 0x0e780800, 0x8f7e0c00, disas_simd_two_reg_misc_fp16 },
13929 { 0x5e400400, 0xdf60c400, disas_simd_scalar_three_reg_same_fp16 },
13930 { 0x00000000, 0x00000000, NULL }
13933 static void disas_data_proc_simd(DisasContext *s, uint32_t insn)
13935 /* Note that this is called with all non-FP cases from
13936 * table C3-6 so it must UNDEF for entries not specifically
13937 * allocated to instructions in that table.
13939 AArch64DecodeFn *fn = lookup_disas_fn(&data_proc_simd[0], insn);
13940 if (fn) {
13941 fn(s, insn);
13942 } else {
13943 unallocated_encoding(s);
13947 /* C3.6 Data processing - SIMD and floating point */
13948 static void disas_data_proc_simd_fp(DisasContext *s, uint32_t insn)
13950 if (extract32(insn, 28, 1) == 1 && extract32(insn, 30, 1) == 0) {
13951 disas_data_proc_fp(s, insn);
13952 } else {
13953 /* SIMD, including crypto */
13954 disas_data_proc_simd(s, insn);
13959 * is_guarded_page:
13960 * @env: The cpu environment
13961 * @s: The DisasContext
13963 * Return true if the page is guarded.
13965 static bool is_guarded_page(CPUARMState *env, DisasContext *s)
13967 #ifdef CONFIG_USER_ONLY
13968 return false; /* FIXME */
13969 #else
13970 uint64_t addr = s->base.pc_first;
13971 int mmu_idx = arm_to_core_mmu_idx(s->mmu_idx);
13972 unsigned int index = tlb_index(env, mmu_idx, addr);
13973 CPUTLBEntry *entry = tlb_entry(env, mmu_idx, addr);
13976 * We test this immediately after reading an insn, which means
13977 * that any normal page must be in the TLB. The only exception
13978 * would be for executing from flash or device memory, which
13979 * does not retain the TLB entry.
13981 * FIXME: Assume false for those, for now. We could use
13982 * arm_cpu_get_phys_page_attrs_debug to re-read the page
13983 * table entry even for that case.
13985 return (tlb_hit(entry->addr_code, addr) &&
13986 env_tlb(env)->d[mmu_idx].iotlb[index].attrs.target_tlb_bit0);
13987 #endif
13991 * btype_destination_ok:
13992 * @insn: The instruction at the branch destination
13993 * @bt: SCTLR_ELx.BT
13994 * @btype: PSTATE.BTYPE, and is non-zero
13996 * On a guarded page, there are a limited number of insns
13997 * that may be present at the branch target:
13998 * - branch target identifiers,
13999 * - paciasp, pacibsp,
14000 * - BRK insn
14001 * - HLT insn
14002 * Anything else causes a Branch Target Exception.
14004 * Return true if the branch is compatible, false to raise BTITRAP.
14006 static bool btype_destination_ok(uint32_t insn, bool bt, int btype)
14008 if ((insn & 0xfffff01fu) == 0xd503201fu) {
14009 /* HINT space */
14010 switch (extract32(insn, 5, 7)) {
14011 case 0b011001: /* PACIASP */
14012 case 0b011011: /* PACIBSP */
14014 * If SCTLR_ELx.BT, then PACI*SP are not compatible
14015 * with btype == 3. Otherwise all btype are ok.
14017 return !bt || btype != 3;
14018 case 0b100000: /* BTI */
14019 /* Not compatible with any btype. */
14020 return false;
14021 case 0b100010: /* BTI c */
14022 /* Not compatible with btype == 3 */
14023 return btype != 3;
14024 case 0b100100: /* BTI j */
14025 /* Not compatible with btype == 2 */
14026 return btype != 2;
14027 case 0b100110: /* BTI jc */
14028 /* Compatible with any btype. */
14029 return true;
14031 } else {
14032 switch (insn & 0xffe0001fu) {
14033 case 0xd4200000u: /* BRK */
14034 case 0xd4400000u: /* HLT */
14035 /* Give priority to the breakpoint exception. */
14036 return true;
14039 return false;
14042 /* C3.1 A64 instruction index by encoding */
14043 static void disas_a64_insn(CPUARMState *env, DisasContext *s)
14045 uint32_t insn;
14047 s->pc_curr = s->base.pc_next;
14048 insn = arm_ldl_code(env, s->base.pc_next, s->sctlr_b);
14049 s->insn = insn;
14050 s->base.pc_next += 4;
14052 s->fp_access_checked = false;
14054 if (dc_isar_feature(aa64_bti, s)) {
14055 if (s->base.num_insns == 1) {
14057 * At the first insn of the TB, compute s->guarded_page.
14058 * We delayed computing this until successfully reading
14059 * the first insn of the TB, above. This (mostly) ensures
14060 * that the softmmu tlb entry has been populated, and the
14061 * page table GP bit is available.
14063 * Note that we need to compute this even if btype == 0,
14064 * because this value is used for BR instructions later
14065 * where ENV is not available.
14067 s->guarded_page = is_guarded_page(env, s);
14069 /* First insn can have btype set to non-zero. */
14070 tcg_debug_assert(s->btype >= 0);
14073 * Note that the Branch Target Exception has fairly high
14074 * priority -- below debugging exceptions but above most
14075 * everything else. This allows us to handle this now
14076 * instead of waiting until the insn is otherwise decoded.
14078 if (s->btype != 0
14079 && s->guarded_page
14080 && !btype_destination_ok(insn, s->bt, s->btype)) {
14081 gen_exception_insn(s, s->pc_curr, EXCP_UDEF,
14082 syn_btitrap(s->btype),
14083 default_exception_el(s));
14084 return;
14086 } else {
14087 /* Not the first insn: btype must be 0. */
14088 tcg_debug_assert(s->btype == 0);
14092 switch (extract32(insn, 25, 4)) {
14093 case 0x0: case 0x1: case 0x3: /* UNALLOCATED */
14094 unallocated_encoding(s);
14095 break;
14096 case 0x2:
14097 if (!dc_isar_feature(aa64_sve, s) || !disas_sve(s, insn)) {
14098 unallocated_encoding(s);
14100 break;
14101 case 0x8: case 0x9: /* Data processing - immediate */
14102 disas_data_proc_imm(s, insn);
14103 break;
14104 case 0xa: case 0xb: /* Branch, exception generation and system insns */
14105 disas_b_exc_sys(s, insn);
14106 break;
14107 case 0x4:
14108 case 0x6:
14109 case 0xc:
14110 case 0xe: /* Loads and stores */
14111 disas_ldst(s, insn);
14112 break;
14113 case 0x5:
14114 case 0xd: /* Data processing - register */
14115 disas_data_proc_reg(s, insn);
14116 break;
14117 case 0x7:
14118 case 0xf: /* Data processing - SIMD and floating point */
14119 disas_data_proc_simd_fp(s, insn);
14120 break;
14121 default:
14122 assert(FALSE); /* all 15 cases should be handled above */
14123 break;
14126 /* if we allocated any temporaries, free them here */
14127 free_tmp_a64(s);
14130 * After execution of most insns, btype is reset to 0.
14131 * Note that we set btype == -1 when the insn sets btype.
14133 if (s->btype > 0 && s->base.is_jmp != DISAS_NORETURN) {
14134 reset_btype(s);
14138 static void aarch64_tr_init_disas_context(DisasContextBase *dcbase,
14139 CPUState *cpu)
14141 DisasContext *dc = container_of(dcbase, DisasContext, base);
14142 CPUARMState *env = cpu->env_ptr;
14143 ARMCPU *arm_cpu = env_archcpu(env);
14144 uint32_t tb_flags = dc->base.tb->flags;
14145 int bound, core_mmu_idx;
14147 dc->isar = &arm_cpu->isar;
14148 dc->condjmp = 0;
14150 dc->aarch64 = 1;
14151 /* If we are coming from secure EL0 in a system with a 32-bit EL3, then
14152 * there is no secure EL1, so we route exceptions to EL3.
14154 dc->secure_routed_to_el3 = arm_feature(env, ARM_FEATURE_EL3) &&
14155 !arm_el_is_aa64(env, 3);
14156 dc->thumb = 0;
14157 dc->sctlr_b = 0;
14158 dc->be_data = FIELD_EX32(tb_flags, TBFLAG_ANY, BE_DATA) ? MO_BE : MO_LE;
14159 dc->condexec_mask = 0;
14160 dc->condexec_cond = 0;
14161 core_mmu_idx = FIELD_EX32(tb_flags, TBFLAG_ANY, MMUIDX);
14162 dc->mmu_idx = core_to_aa64_mmu_idx(core_mmu_idx);
14163 dc->tbii = FIELD_EX32(tb_flags, TBFLAG_A64, TBII);
14164 dc->tbid = FIELD_EX32(tb_flags, TBFLAG_A64, TBID);
14165 dc->current_el = arm_mmu_idx_to_el(dc->mmu_idx);
14166 #if !defined(CONFIG_USER_ONLY)
14167 dc->user = (dc->current_el == 0);
14168 #endif
14169 dc->fp_excp_el = FIELD_EX32(tb_flags, TBFLAG_ANY, FPEXC_EL);
14170 dc->sve_excp_el = FIELD_EX32(tb_flags, TBFLAG_A64, SVEEXC_EL);
14171 dc->sve_len = (FIELD_EX32(tb_flags, TBFLAG_A64, ZCR_LEN) + 1) * 16;
14172 dc->pauth_active = FIELD_EX32(tb_flags, TBFLAG_A64, PAUTH_ACTIVE);
14173 dc->bt = FIELD_EX32(tb_flags, TBFLAG_A64, BT);
14174 dc->btype = FIELD_EX32(tb_flags, TBFLAG_A64, BTYPE);
14175 dc->unpriv = FIELD_EX32(tb_flags, TBFLAG_A64, UNPRIV);
14176 dc->vec_len = 0;
14177 dc->vec_stride = 0;
14178 dc->cp_regs = arm_cpu->cp_regs;
14179 dc->features = env->features;
14181 /* Single step state. The code-generation logic here is:
14182 * SS_ACTIVE == 0:
14183 * generate code with no special handling for single-stepping (except
14184 * that anything that can make us go to SS_ACTIVE == 1 must end the TB;
14185 * this happens anyway because those changes are all system register or
14186 * PSTATE writes).
14187 * SS_ACTIVE == 1, PSTATE.SS == 1: (active-not-pending)
14188 * emit code for one insn
14189 * emit code to clear PSTATE.SS
14190 * emit code to generate software step exception for completed step
14191 * end TB (as usual for having generated an exception)
14192 * SS_ACTIVE == 1, PSTATE.SS == 0: (active-pending)
14193 * emit code to generate a software step exception
14194 * end the TB
14196 dc->ss_active = FIELD_EX32(tb_flags, TBFLAG_ANY, SS_ACTIVE);
14197 dc->pstate_ss = FIELD_EX32(tb_flags, TBFLAG_ANY, PSTATE_SS);
14198 dc->is_ldex = false;
14199 dc->debug_target_el = FIELD_EX32(tb_flags, TBFLAG_ANY, DEBUG_TARGET_EL);
14201 /* Bound the number of insns to execute to those left on the page. */
14202 bound = -(dc->base.pc_first | TARGET_PAGE_MASK) / 4;
14204 /* If architectural single step active, limit to 1. */
14205 if (dc->ss_active) {
14206 bound = 1;
14208 dc->base.max_insns = MIN(dc->base.max_insns, bound);
14210 init_tmp_a64_array(dc);
14213 static void aarch64_tr_tb_start(DisasContextBase *db, CPUState *cpu)
14217 static void aarch64_tr_insn_start(DisasContextBase *dcbase, CPUState *cpu)
14219 DisasContext *dc = container_of(dcbase, DisasContext, base);
14221 tcg_gen_insn_start(dc->base.pc_next, 0, 0);
14222 dc->insn_start = tcg_last_op();
14225 static bool aarch64_tr_breakpoint_check(DisasContextBase *dcbase, CPUState *cpu,
14226 const CPUBreakpoint *bp)
14228 DisasContext *dc = container_of(dcbase, DisasContext, base);
14230 if (bp->flags & BP_CPU) {
14231 gen_a64_set_pc_im(dc->base.pc_next);
14232 gen_helper_check_breakpoints(cpu_env);
14233 /* End the TB early; it likely won't be executed */
14234 dc->base.is_jmp = DISAS_TOO_MANY;
14235 } else {
14236 gen_exception_internal_insn(dc, dc->base.pc_next, EXCP_DEBUG);
14237 /* The address covered by the breakpoint must be
14238 included in [tb->pc, tb->pc + tb->size) in order
14239 to for it to be properly cleared -- thus we
14240 increment the PC here so that the logic setting
14241 tb->size below does the right thing. */
14242 dc->base.pc_next += 4;
14243 dc->base.is_jmp = DISAS_NORETURN;
14246 return true;
14249 static void aarch64_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)
14251 DisasContext *dc = container_of(dcbase, DisasContext, base);
14252 CPUARMState *env = cpu->env_ptr;
14254 if (dc->ss_active && !dc->pstate_ss) {
14255 /* Singlestep state is Active-pending.
14256 * If we're in this state at the start of a TB then either
14257 * a) we just took an exception to an EL which is being debugged
14258 * and this is the first insn in the exception handler
14259 * b) debug exceptions were masked and we just unmasked them
14260 * without changing EL (eg by clearing PSTATE.D)
14261 * In either case we're going to take a swstep exception in the
14262 * "did not step an insn" case, and so the syndrome ISV and EX
14263 * bits should be zero.
14265 assert(dc->base.num_insns == 1);
14266 gen_swstep_exception(dc, 0, 0);
14267 dc->base.is_jmp = DISAS_NORETURN;
14268 } else {
14269 disas_a64_insn(env, dc);
14272 translator_loop_temp_check(&dc->base);
14275 static void aarch64_tr_tb_stop(DisasContextBase *dcbase, CPUState *cpu)
14277 DisasContext *dc = container_of(dcbase, DisasContext, base);
14279 if (unlikely(dc->base.singlestep_enabled || dc->ss_active)) {
14280 /* Note that this means single stepping WFI doesn't halt the CPU.
14281 * For conditional branch insns this is harmless unreachable code as
14282 * gen_goto_tb() has already handled emitting the debug exception
14283 * (and thus a tb-jump is not possible when singlestepping).
14285 switch (dc->base.is_jmp) {
14286 default:
14287 gen_a64_set_pc_im(dc->base.pc_next);
14288 /* fall through */
14289 case DISAS_EXIT:
14290 case DISAS_JUMP:
14291 if (dc->base.singlestep_enabled) {
14292 gen_exception_internal(EXCP_DEBUG);
14293 } else {
14294 gen_step_complete_exception(dc);
14296 break;
14297 case DISAS_NORETURN:
14298 break;
14300 } else {
14301 switch (dc->base.is_jmp) {
14302 case DISAS_NEXT:
14303 case DISAS_TOO_MANY:
14304 gen_goto_tb(dc, 1, dc->base.pc_next);
14305 break;
14306 default:
14307 case DISAS_UPDATE:
14308 gen_a64_set_pc_im(dc->base.pc_next);
14309 /* fall through */
14310 case DISAS_EXIT:
14311 tcg_gen_exit_tb(NULL, 0);
14312 break;
14313 case DISAS_JUMP:
14314 tcg_gen_lookup_and_goto_ptr();
14315 break;
14316 case DISAS_NORETURN:
14317 case DISAS_SWI:
14318 break;
14319 case DISAS_WFE:
14320 gen_a64_set_pc_im(dc->base.pc_next);
14321 gen_helper_wfe(cpu_env);
14322 break;
14323 case DISAS_YIELD:
14324 gen_a64_set_pc_im(dc->base.pc_next);
14325 gen_helper_yield(cpu_env);
14326 break;
14327 case DISAS_WFI:
14329 /* This is a special case because we don't want to just halt the CPU
14330 * if trying to debug across a WFI.
14332 TCGv_i32 tmp = tcg_const_i32(4);
14334 gen_a64_set_pc_im(dc->base.pc_next);
14335 gen_helper_wfi(cpu_env, tmp);
14336 tcg_temp_free_i32(tmp);
14337 /* The helper doesn't necessarily throw an exception, but we
14338 * must go back to the main loop to check for interrupts anyway.
14340 tcg_gen_exit_tb(NULL, 0);
14341 break;
14347 static void aarch64_tr_disas_log(const DisasContextBase *dcbase,
14348 CPUState *cpu)
14350 DisasContext *dc = container_of(dcbase, DisasContext, base);
14352 qemu_log("IN: %s\n", lookup_symbol(dc->base.pc_first));
14353 log_target_disas(cpu, dc->base.pc_first, dc->base.tb->size);
14356 const TranslatorOps aarch64_translator_ops = {
14357 .init_disas_context = aarch64_tr_init_disas_context,
14358 .tb_start = aarch64_tr_tb_start,
14359 .insn_start = aarch64_tr_insn_start,
14360 .breakpoint_check = aarch64_tr_breakpoint_check,
14361 .translate_insn = aarch64_tr_translate_insn,
14362 .tb_stop = aarch64_tr_tb_stop,
14363 .disas_log = aarch64_tr_disas_log,