vvfat: set status to odd fixes
[qemu.git] / target / arm / translate-a64.c
blob8c0764957c8a9736b849d149335a0dfb91b99737
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 * Handle MTE and/or TBI.
209 * For TBI, ideally, we would do nothing. Proper behaviour on fault is
210 * for the tag to be present in the FAR_ELx register. But for user-only
211 * mode we do not have a TLB with which to implement this, so we must
212 * remove the top byte now.
214 * Always return a fresh temporary that we can increment independently
215 * of the write-back address.
218 TCGv_i64 clean_data_tbi(DisasContext *s, TCGv_i64 addr)
220 TCGv_i64 clean = new_tmp_a64(s);
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 /* Insert a zero tag into src, with the result at dst. */
230 static void gen_address_with_allocation_tag0(TCGv_i64 dst, TCGv_i64 src)
232 tcg_gen_andi_i64(dst, src, ~MAKE_64BIT_MASK(56, 4));
235 static void gen_probe_access(DisasContext *s, TCGv_i64 ptr,
236 MMUAccessType acc, int log2_size)
238 TCGv_i32 t_acc = tcg_const_i32(acc);
239 TCGv_i32 t_idx = tcg_const_i32(get_mem_index(s));
240 TCGv_i32 t_size = tcg_const_i32(1 << log2_size);
242 gen_helper_probe_access(cpu_env, ptr, t_acc, t_idx, t_size);
243 tcg_temp_free_i32(t_acc);
244 tcg_temp_free_i32(t_idx);
245 tcg_temp_free_i32(t_size);
249 * For MTE, check a single logical or atomic access. This probes a single
250 * address, the exact one specified. The size and alignment of the access
251 * is not relevant to MTE, per se, but watchpoints do require the size,
252 * and we want to recognize those before making any other changes to state.
254 static TCGv_i64 gen_mte_check1_mmuidx(DisasContext *s, TCGv_i64 addr,
255 bool is_write, bool tag_checked,
256 int log2_size, bool is_unpriv,
257 int core_idx)
259 if (tag_checked && s->mte_active[is_unpriv]) {
260 TCGv_i32 tcg_desc;
261 TCGv_i64 ret;
262 int desc = 0;
264 desc = FIELD_DP32(desc, MTEDESC, MIDX, core_idx);
265 desc = FIELD_DP32(desc, MTEDESC, TBI, s->tbid);
266 desc = FIELD_DP32(desc, MTEDESC, TCMA, s->tcma);
267 desc = FIELD_DP32(desc, MTEDESC, WRITE, is_write);
268 desc = FIELD_DP32(desc, MTEDESC, ESIZE, 1 << log2_size);
269 tcg_desc = tcg_const_i32(desc);
271 ret = new_tmp_a64(s);
272 gen_helper_mte_check1(ret, cpu_env, tcg_desc, addr);
273 tcg_temp_free_i32(tcg_desc);
275 return ret;
277 return clean_data_tbi(s, addr);
280 TCGv_i64 gen_mte_check1(DisasContext *s, TCGv_i64 addr, bool is_write,
281 bool tag_checked, int log2_size)
283 return gen_mte_check1_mmuidx(s, addr, is_write, tag_checked, log2_size,
284 false, get_mem_index(s));
288 * For MTE, check multiple logical sequential accesses.
290 TCGv_i64 gen_mte_checkN(DisasContext *s, TCGv_i64 addr, bool is_write,
291 bool tag_checked, int log2_esize, int total_size)
293 if (tag_checked && s->mte_active[0] && total_size != (1 << log2_esize)) {
294 TCGv_i32 tcg_desc;
295 TCGv_i64 ret;
296 int desc = 0;
298 desc = FIELD_DP32(desc, MTEDESC, MIDX, get_mem_index(s));
299 desc = FIELD_DP32(desc, MTEDESC, TBI, s->tbid);
300 desc = FIELD_DP32(desc, MTEDESC, TCMA, s->tcma);
301 desc = FIELD_DP32(desc, MTEDESC, WRITE, is_write);
302 desc = FIELD_DP32(desc, MTEDESC, ESIZE, 1 << log2_esize);
303 desc = FIELD_DP32(desc, MTEDESC, TSIZE, total_size);
304 tcg_desc = tcg_const_i32(desc);
306 ret = new_tmp_a64(s);
307 gen_helper_mte_checkN(ret, cpu_env, tcg_desc, addr);
308 tcg_temp_free_i32(tcg_desc);
310 return ret;
312 return gen_mte_check1(s, addr, is_write, tag_checked, log2_esize);
315 typedef struct DisasCompare64 {
316 TCGCond cond;
317 TCGv_i64 value;
318 } DisasCompare64;
320 static void a64_test_cc(DisasCompare64 *c64, int cc)
322 DisasCompare c32;
324 arm_test_cc(&c32, cc);
326 /* Sign-extend the 32-bit value so that the GE/LT comparisons work
327 * properly. The NE/EQ comparisons are also fine with this choice. */
328 c64->cond = c32.cond;
329 c64->value = tcg_temp_new_i64();
330 tcg_gen_ext_i32_i64(c64->value, c32.value);
332 arm_free_cc(&c32);
335 static void a64_free_cc(DisasCompare64 *c64)
337 tcg_temp_free_i64(c64->value);
340 static void gen_exception_internal(int excp)
342 TCGv_i32 tcg_excp = tcg_const_i32(excp);
344 assert(excp_is_internal(excp));
345 gen_helper_exception_internal(cpu_env, tcg_excp);
346 tcg_temp_free_i32(tcg_excp);
349 static void gen_exception_internal_insn(DisasContext *s, uint64_t pc, int excp)
351 gen_a64_set_pc_im(pc);
352 gen_exception_internal(excp);
353 s->base.is_jmp = DISAS_NORETURN;
356 static void gen_exception_insn(DisasContext *s, uint64_t pc, int excp,
357 uint32_t syndrome, uint32_t target_el)
359 gen_a64_set_pc_im(pc);
360 gen_exception(excp, syndrome, target_el);
361 s->base.is_jmp = DISAS_NORETURN;
364 static void gen_exception_bkpt_insn(DisasContext *s, uint32_t syndrome)
366 TCGv_i32 tcg_syn;
368 gen_a64_set_pc_im(s->pc_curr);
369 tcg_syn = tcg_const_i32(syndrome);
370 gen_helper_exception_bkpt_insn(cpu_env, tcg_syn);
371 tcg_temp_free_i32(tcg_syn);
372 s->base.is_jmp = DISAS_NORETURN;
375 static void gen_step_complete_exception(DisasContext *s)
377 /* We just completed step of an insn. Move from Active-not-pending
378 * to Active-pending, and then also take the swstep exception.
379 * This corresponds to making the (IMPDEF) choice to prioritize
380 * swstep exceptions over asynchronous exceptions taken to an exception
381 * level where debug is disabled. This choice has the advantage that
382 * we do not need to maintain internal state corresponding to the
383 * ISV/EX syndrome bits between completion of the step and generation
384 * of the exception, and our syndrome information is always correct.
386 gen_ss_advance(s);
387 gen_swstep_exception(s, 1, s->is_ldex);
388 s->base.is_jmp = DISAS_NORETURN;
391 static inline bool use_goto_tb(DisasContext *s, int n, uint64_t dest)
393 /* No direct tb linking with singlestep (either QEMU's or the ARM
394 * debug architecture kind) or deterministic io
396 if (s->base.singlestep_enabled || s->ss_active ||
397 (tb_cflags(s->base.tb) & CF_LAST_IO)) {
398 return false;
401 #ifndef CONFIG_USER_ONLY
402 /* Only link tbs from inside the same guest page */
403 if ((s->base.tb->pc & TARGET_PAGE_MASK) != (dest & TARGET_PAGE_MASK)) {
404 return false;
406 #endif
408 return true;
411 static inline void gen_goto_tb(DisasContext *s, int n, uint64_t dest)
413 TranslationBlock *tb;
415 tb = s->base.tb;
416 if (use_goto_tb(s, n, dest)) {
417 tcg_gen_goto_tb(n);
418 gen_a64_set_pc_im(dest);
419 tcg_gen_exit_tb(tb, n);
420 s->base.is_jmp = DISAS_NORETURN;
421 } else {
422 gen_a64_set_pc_im(dest);
423 if (s->ss_active) {
424 gen_step_complete_exception(s);
425 } else if (s->base.singlestep_enabled) {
426 gen_exception_internal(EXCP_DEBUG);
427 } else {
428 tcg_gen_lookup_and_goto_ptr();
429 s->base.is_jmp = DISAS_NORETURN;
434 void unallocated_encoding(DisasContext *s)
436 /* Unallocated and reserved encodings are uncategorized */
437 gen_exception_insn(s, s->pc_curr, EXCP_UDEF, syn_uncategorized(),
438 default_exception_el(s));
441 static void init_tmp_a64_array(DisasContext *s)
443 #ifdef CONFIG_DEBUG_TCG
444 memset(s->tmp_a64, 0, sizeof(s->tmp_a64));
445 #endif
446 s->tmp_a64_count = 0;
449 static void free_tmp_a64(DisasContext *s)
451 int i;
452 for (i = 0; i < s->tmp_a64_count; i++) {
453 tcg_temp_free_i64(s->tmp_a64[i]);
455 init_tmp_a64_array(s);
458 TCGv_i64 new_tmp_a64(DisasContext *s)
460 assert(s->tmp_a64_count < TMP_A64_MAX);
461 return s->tmp_a64[s->tmp_a64_count++] = tcg_temp_new_i64();
464 TCGv_i64 new_tmp_a64_local(DisasContext *s)
466 assert(s->tmp_a64_count < TMP_A64_MAX);
467 return s->tmp_a64[s->tmp_a64_count++] = tcg_temp_local_new_i64();
470 TCGv_i64 new_tmp_a64_zero(DisasContext *s)
472 TCGv_i64 t = new_tmp_a64(s);
473 tcg_gen_movi_i64(t, 0);
474 return t;
478 * Register access functions
480 * These functions are used for directly accessing a register in where
481 * changes to the final register value are likely to be made. If you
482 * need to use a register for temporary calculation (e.g. index type
483 * operations) use the read_* form.
485 * B1.2.1 Register mappings
487 * In instruction register encoding 31 can refer to ZR (zero register) or
488 * the SP (stack pointer) depending on context. In QEMU's case we map SP
489 * to cpu_X[31] and ZR accesses to a temporary which can be discarded.
490 * This is the point of the _sp forms.
492 TCGv_i64 cpu_reg(DisasContext *s, int reg)
494 if (reg == 31) {
495 return new_tmp_a64_zero(s);
496 } else {
497 return cpu_X[reg];
501 /* register access for when 31 == SP */
502 TCGv_i64 cpu_reg_sp(DisasContext *s, int reg)
504 return cpu_X[reg];
507 /* read a cpu register in 32bit/64bit mode. Returns a TCGv_i64
508 * representing the register contents. This TCGv is an auto-freed
509 * temporary so it need not be explicitly freed, and may be modified.
511 TCGv_i64 read_cpu_reg(DisasContext *s, int reg, int sf)
513 TCGv_i64 v = new_tmp_a64(s);
514 if (reg != 31) {
515 if (sf) {
516 tcg_gen_mov_i64(v, cpu_X[reg]);
517 } else {
518 tcg_gen_ext32u_i64(v, cpu_X[reg]);
520 } else {
521 tcg_gen_movi_i64(v, 0);
523 return v;
526 TCGv_i64 read_cpu_reg_sp(DisasContext *s, int reg, int sf)
528 TCGv_i64 v = new_tmp_a64(s);
529 if (sf) {
530 tcg_gen_mov_i64(v, cpu_X[reg]);
531 } else {
532 tcg_gen_ext32u_i64(v, cpu_X[reg]);
534 return v;
537 /* Return the offset into CPUARMState of a slice (from
538 * the least significant end) of FP register Qn (ie
539 * Dn, Sn, Hn or Bn).
540 * (Note that this is not the same mapping as for A32; see cpu.h)
542 static inline int fp_reg_offset(DisasContext *s, int regno, MemOp size)
544 return vec_reg_offset(s, regno, 0, size);
547 /* Offset of the high half of the 128 bit vector Qn */
548 static inline int fp_reg_hi_offset(DisasContext *s, int regno)
550 return vec_reg_offset(s, regno, 1, MO_64);
553 /* Convenience accessors for reading and writing single and double
554 * FP registers. Writing clears the upper parts of the associated
555 * 128 bit vector register, as required by the architecture.
556 * Note that unlike the GP register accessors, the values returned
557 * by the read functions must be manually freed.
559 static TCGv_i64 read_fp_dreg(DisasContext *s, int reg)
561 TCGv_i64 v = tcg_temp_new_i64();
563 tcg_gen_ld_i64(v, cpu_env, fp_reg_offset(s, reg, MO_64));
564 return v;
567 static TCGv_i32 read_fp_sreg(DisasContext *s, int reg)
569 TCGv_i32 v = tcg_temp_new_i32();
571 tcg_gen_ld_i32(v, cpu_env, fp_reg_offset(s, reg, MO_32));
572 return v;
575 static TCGv_i32 read_fp_hreg(DisasContext *s, int reg)
577 TCGv_i32 v = tcg_temp_new_i32();
579 tcg_gen_ld16u_i32(v, cpu_env, fp_reg_offset(s, reg, MO_16));
580 return v;
583 /* Clear the bits above an N-bit vector, for N = (is_q ? 128 : 64).
584 * If SVE is not enabled, then there are only 128 bits in the vector.
586 static void clear_vec_high(DisasContext *s, bool is_q, int rd)
588 unsigned ofs = fp_reg_offset(s, rd, MO_64);
589 unsigned vsz = vec_full_reg_size(s);
591 /* Nop move, with side effect of clearing the tail. */
592 tcg_gen_gvec_mov(MO_64, ofs, ofs, is_q ? 16 : 8, vsz);
595 void write_fp_dreg(DisasContext *s, int reg, TCGv_i64 v)
597 unsigned ofs = fp_reg_offset(s, reg, MO_64);
599 tcg_gen_st_i64(v, cpu_env, ofs);
600 clear_vec_high(s, false, reg);
603 static void write_fp_sreg(DisasContext *s, int reg, TCGv_i32 v)
605 TCGv_i64 tmp = tcg_temp_new_i64();
607 tcg_gen_extu_i32_i64(tmp, v);
608 write_fp_dreg(s, reg, tmp);
609 tcg_temp_free_i64(tmp);
612 TCGv_ptr get_fpstatus_ptr(bool is_f16)
614 TCGv_ptr statusptr = tcg_temp_new_ptr();
615 int offset;
617 /* In A64 all instructions (both FP and Neon) use the FPCR; there
618 * is no equivalent of the A32 Neon "standard FPSCR value".
619 * However half-precision operations operate under a different
620 * FZ16 flag and use vfp.fp_status_f16 instead of vfp.fp_status.
622 if (is_f16) {
623 offset = offsetof(CPUARMState, vfp.fp_status_f16);
624 } else {
625 offset = offsetof(CPUARMState, vfp.fp_status);
627 tcg_gen_addi_ptr(statusptr, cpu_env, offset);
628 return statusptr;
631 /* Expand a 2-operand AdvSIMD vector operation using an expander function. */
632 static void gen_gvec_fn2(DisasContext *s, bool is_q, int rd, int rn,
633 GVecGen2Fn *gvec_fn, int vece)
635 gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
636 is_q ? 16 : 8, vec_full_reg_size(s));
639 /* Expand a 2-operand + immediate AdvSIMD vector operation using
640 * an expander function.
642 static void gen_gvec_fn2i(DisasContext *s, bool is_q, int rd, int rn,
643 int64_t imm, GVecGen2iFn *gvec_fn, int vece)
645 gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
646 imm, is_q ? 16 : 8, vec_full_reg_size(s));
649 /* Expand a 3-operand AdvSIMD vector operation using an expander function. */
650 static void gen_gvec_fn3(DisasContext *s, bool is_q, int rd, int rn, int rm,
651 GVecGen3Fn *gvec_fn, int vece)
653 gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
654 vec_full_reg_offset(s, rm), is_q ? 16 : 8, vec_full_reg_size(s));
657 /* Expand a 4-operand AdvSIMD vector operation using an expander function. */
658 static void gen_gvec_fn4(DisasContext *s, bool is_q, int rd, int rn, int rm,
659 int rx, GVecGen4Fn *gvec_fn, int vece)
661 gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
662 vec_full_reg_offset(s, rm), vec_full_reg_offset(s, rx),
663 is_q ? 16 : 8, vec_full_reg_size(s));
666 /* Expand a 2-operand operation using an out-of-line helper. */
667 static void gen_gvec_op2_ool(DisasContext *s, bool is_q, int rd,
668 int rn, int data, gen_helper_gvec_2 *fn)
670 tcg_gen_gvec_2_ool(vec_full_reg_offset(s, rd),
671 vec_full_reg_offset(s, rn),
672 is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
675 /* Expand a 3-operand operation using an out-of-line helper. */
676 static void gen_gvec_op3_ool(DisasContext *s, bool is_q, int rd,
677 int rn, int rm, int data, gen_helper_gvec_3 *fn)
679 tcg_gen_gvec_3_ool(vec_full_reg_offset(s, rd),
680 vec_full_reg_offset(s, rn),
681 vec_full_reg_offset(s, rm),
682 is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
685 /* Expand a 3-operand + fpstatus pointer + simd data value operation using
686 * an out-of-line helper.
688 static void gen_gvec_op3_fpst(DisasContext *s, bool is_q, int rd, int rn,
689 int rm, bool is_fp16, int data,
690 gen_helper_gvec_3_ptr *fn)
692 TCGv_ptr fpst = get_fpstatus_ptr(is_fp16);
693 tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
694 vec_full_reg_offset(s, rn),
695 vec_full_reg_offset(s, rm), fpst,
696 is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
697 tcg_temp_free_ptr(fpst);
700 /* Set ZF and NF based on a 64 bit result. This is alas fiddlier
701 * than the 32 bit equivalent.
703 static inline void gen_set_NZ64(TCGv_i64 result)
705 tcg_gen_extr_i64_i32(cpu_ZF, cpu_NF, result);
706 tcg_gen_or_i32(cpu_ZF, cpu_ZF, cpu_NF);
709 /* Set NZCV as for a logical operation: NZ as per result, CV cleared. */
710 static inline void gen_logic_CC(int sf, TCGv_i64 result)
712 if (sf) {
713 gen_set_NZ64(result);
714 } else {
715 tcg_gen_extrl_i64_i32(cpu_ZF, result);
716 tcg_gen_mov_i32(cpu_NF, cpu_ZF);
718 tcg_gen_movi_i32(cpu_CF, 0);
719 tcg_gen_movi_i32(cpu_VF, 0);
722 /* dest = T0 + T1; compute C, N, V and Z flags */
723 static void gen_add_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
725 if (sf) {
726 TCGv_i64 result, flag, tmp;
727 result = tcg_temp_new_i64();
728 flag = tcg_temp_new_i64();
729 tmp = tcg_temp_new_i64();
731 tcg_gen_movi_i64(tmp, 0);
732 tcg_gen_add2_i64(result, flag, t0, tmp, t1, tmp);
734 tcg_gen_extrl_i64_i32(cpu_CF, flag);
736 gen_set_NZ64(result);
738 tcg_gen_xor_i64(flag, result, t0);
739 tcg_gen_xor_i64(tmp, t0, t1);
740 tcg_gen_andc_i64(flag, flag, tmp);
741 tcg_temp_free_i64(tmp);
742 tcg_gen_extrh_i64_i32(cpu_VF, flag);
744 tcg_gen_mov_i64(dest, result);
745 tcg_temp_free_i64(result);
746 tcg_temp_free_i64(flag);
747 } else {
748 /* 32 bit arithmetic */
749 TCGv_i32 t0_32 = tcg_temp_new_i32();
750 TCGv_i32 t1_32 = tcg_temp_new_i32();
751 TCGv_i32 tmp = tcg_temp_new_i32();
753 tcg_gen_movi_i32(tmp, 0);
754 tcg_gen_extrl_i64_i32(t0_32, t0);
755 tcg_gen_extrl_i64_i32(t1_32, t1);
756 tcg_gen_add2_i32(cpu_NF, cpu_CF, t0_32, tmp, t1_32, tmp);
757 tcg_gen_mov_i32(cpu_ZF, cpu_NF);
758 tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
759 tcg_gen_xor_i32(tmp, t0_32, t1_32);
760 tcg_gen_andc_i32(cpu_VF, cpu_VF, tmp);
761 tcg_gen_extu_i32_i64(dest, cpu_NF);
763 tcg_temp_free_i32(tmp);
764 tcg_temp_free_i32(t0_32);
765 tcg_temp_free_i32(t1_32);
769 /* dest = T0 - T1; compute C, N, V and Z flags */
770 static void gen_sub_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
772 if (sf) {
773 /* 64 bit arithmetic */
774 TCGv_i64 result, flag, tmp;
776 result = tcg_temp_new_i64();
777 flag = tcg_temp_new_i64();
778 tcg_gen_sub_i64(result, t0, t1);
780 gen_set_NZ64(result);
782 tcg_gen_setcond_i64(TCG_COND_GEU, flag, t0, t1);
783 tcg_gen_extrl_i64_i32(cpu_CF, flag);
785 tcg_gen_xor_i64(flag, result, t0);
786 tmp = tcg_temp_new_i64();
787 tcg_gen_xor_i64(tmp, t0, t1);
788 tcg_gen_and_i64(flag, flag, tmp);
789 tcg_temp_free_i64(tmp);
790 tcg_gen_extrh_i64_i32(cpu_VF, flag);
791 tcg_gen_mov_i64(dest, result);
792 tcg_temp_free_i64(flag);
793 tcg_temp_free_i64(result);
794 } else {
795 /* 32 bit arithmetic */
796 TCGv_i32 t0_32 = tcg_temp_new_i32();
797 TCGv_i32 t1_32 = tcg_temp_new_i32();
798 TCGv_i32 tmp;
800 tcg_gen_extrl_i64_i32(t0_32, t0);
801 tcg_gen_extrl_i64_i32(t1_32, t1);
802 tcg_gen_sub_i32(cpu_NF, t0_32, t1_32);
803 tcg_gen_mov_i32(cpu_ZF, cpu_NF);
804 tcg_gen_setcond_i32(TCG_COND_GEU, cpu_CF, t0_32, t1_32);
805 tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
806 tmp = tcg_temp_new_i32();
807 tcg_gen_xor_i32(tmp, t0_32, t1_32);
808 tcg_temp_free_i32(t0_32);
809 tcg_temp_free_i32(t1_32);
810 tcg_gen_and_i32(cpu_VF, cpu_VF, tmp);
811 tcg_temp_free_i32(tmp);
812 tcg_gen_extu_i32_i64(dest, cpu_NF);
816 /* dest = T0 + T1 + CF; do not compute flags. */
817 static void gen_adc(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
819 TCGv_i64 flag = tcg_temp_new_i64();
820 tcg_gen_extu_i32_i64(flag, cpu_CF);
821 tcg_gen_add_i64(dest, t0, t1);
822 tcg_gen_add_i64(dest, dest, flag);
823 tcg_temp_free_i64(flag);
825 if (!sf) {
826 tcg_gen_ext32u_i64(dest, dest);
830 /* dest = T0 + T1 + CF; compute C, N, V and Z flags. */
831 static void gen_adc_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
833 if (sf) {
834 TCGv_i64 result, cf_64, vf_64, tmp;
835 result = tcg_temp_new_i64();
836 cf_64 = tcg_temp_new_i64();
837 vf_64 = tcg_temp_new_i64();
838 tmp = tcg_const_i64(0);
840 tcg_gen_extu_i32_i64(cf_64, cpu_CF);
841 tcg_gen_add2_i64(result, cf_64, t0, tmp, cf_64, tmp);
842 tcg_gen_add2_i64(result, cf_64, result, cf_64, t1, tmp);
843 tcg_gen_extrl_i64_i32(cpu_CF, cf_64);
844 gen_set_NZ64(result);
846 tcg_gen_xor_i64(vf_64, result, t0);
847 tcg_gen_xor_i64(tmp, t0, t1);
848 tcg_gen_andc_i64(vf_64, vf_64, tmp);
849 tcg_gen_extrh_i64_i32(cpu_VF, vf_64);
851 tcg_gen_mov_i64(dest, result);
853 tcg_temp_free_i64(tmp);
854 tcg_temp_free_i64(vf_64);
855 tcg_temp_free_i64(cf_64);
856 tcg_temp_free_i64(result);
857 } else {
858 TCGv_i32 t0_32, t1_32, tmp;
859 t0_32 = tcg_temp_new_i32();
860 t1_32 = tcg_temp_new_i32();
861 tmp = tcg_const_i32(0);
863 tcg_gen_extrl_i64_i32(t0_32, t0);
864 tcg_gen_extrl_i64_i32(t1_32, t1);
865 tcg_gen_add2_i32(cpu_NF, cpu_CF, t0_32, tmp, cpu_CF, tmp);
866 tcg_gen_add2_i32(cpu_NF, cpu_CF, cpu_NF, cpu_CF, t1_32, tmp);
868 tcg_gen_mov_i32(cpu_ZF, cpu_NF);
869 tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
870 tcg_gen_xor_i32(tmp, t0_32, t1_32);
871 tcg_gen_andc_i32(cpu_VF, cpu_VF, tmp);
872 tcg_gen_extu_i32_i64(dest, cpu_NF);
874 tcg_temp_free_i32(tmp);
875 tcg_temp_free_i32(t1_32);
876 tcg_temp_free_i32(t0_32);
881 * Load/Store generators
885 * Store from GPR register to memory.
887 static void do_gpr_st_memidx(DisasContext *s, TCGv_i64 source,
888 TCGv_i64 tcg_addr, int size, int memidx,
889 bool iss_valid,
890 unsigned int iss_srt,
891 bool iss_sf, bool iss_ar)
893 g_assert(size <= 3);
894 tcg_gen_qemu_st_i64(source, tcg_addr, memidx, s->be_data + size);
896 if (iss_valid) {
897 uint32_t syn;
899 syn = syn_data_abort_with_iss(0,
900 size,
901 false,
902 iss_srt,
903 iss_sf,
904 iss_ar,
905 0, 0, 0, 0, 0, false);
906 disas_set_insn_syndrome(s, syn);
910 static void do_gpr_st(DisasContext *s, TCGv_i64 source,
911 TCGv_i64 tcg_addr, int size,
912 bool iss_valid,
913 unsigned int iss_srt,
914 bool iss_sf, bool iss_ar)
916 do_gpr_st_memidx(s, source, tcg_addr, size, get_mem_index(s),
917 iss_valid, iss_srt, iss_sf, iss_ar);
921 * Load from memory to GPR register
923 static void do_gpr_ld_memidx(DisasContext *s,
924 TCGv_i64 dest, TCGv_i64 tcg_addr,
925 int size, bool is_signed,
926 bool extend, int memidx,
927 bool iss_valid, unsigned int iss_srt,
928 bool iss_sf, bool iss_ar)
930 MemOp memop = s->be_data + size;
932 g_assert(size <= 3);
934 if (is_signed) {
935 memop += MO_SIGN;
938 tcg_gen_qemu_ld_i64(dest, tcg_addr, memidx, memop);
940 if (extend && is_signed) {
941 g_assert(size < 3);
942 tcg_gen_ext32u_i64(dest, dest);
945 if (iss_valid) {
946 uint32_t syn;
948 syn = syn_data_abort_with_iss(0,
949 size,
950 is_signed,
951 iss_srt,
952 iss_sf,
953 iss_ar,
954 0, 0, 0, 0, 0, false);
955 disas_set_insn_syndrome(s, syn);
959 static void do_gpr_ld(DisasContext *s,
960 TCGv_i64 dest, TCGv_i64 tcg_addr,
961 int size, bool is_signed, bool extend,
962 bool iss_valid, unsigned int iss_srt,
963 bool iss_sf, bool iss_ar)
965 do_gpr_ld_memidx(s, dest, tcg_addr, size, is_signed, extend,
966 get_mem_index(s),
967 iss_valid, iss_srt, iss_sf, iss_ar);
971 * Store from FP register to memory
973 static void do_fp_st(DisasContext *s, int srcidx, TCGv_i64 tcg_addr, int size)
975 /* This writes the bottom N bits of a 128 bit wide vector to memory */
976 TCGv_i64 tmp = tcg_temp_new_i64();
977 tcg_gen_ld_i64(tmp, cpu_env, fp_reg_offset(s, srcidx, MO_64));
978 if (size < 4) {
979 tcg_gen_qemu_st_i64(tmp, tcg_addr, get_mem_index(s),
980 s->be_data + size);
981 } else {
982 bool be = s->be_data == MO_BE;
983 TCGv_i64 tcg_hiaddr = tcg_temp_new_i64();
985 tcg_gen_addi_i64(tcg_hiaddr, tcg_addr, 8);
986 tcg_gen_qemu_st_i64(tmp, be ? tcg_hiaddr : tcg_addr, get_mem_index(s),
987 s->be_data | MO_Q);
988 tcg_gen_ld_i64(tmp, cpu_env, fp_reg_hi_offset(s, srcidx));
989 tcg_gen_qemu_st_i64(tmp, be ? tcg_addr : tcg_hiaddr, get_mem_index(s),
990 s->be_data | MO_Q);
991 tcg_temp_free_i64(tcg_hiaddr);
994 tcg_temp_free_i64(tmp);
998 * Load from memory to FP register
1000 static void do_fp_ld(DisasContext *s, int destidx, TCGv_i64 tcg_addr, int size)
1002 /* This always zero-extends and writes to a full 128 bit wide vector */
1003 TCGv_i64 tmplo = tcg_temp_new_i64();
1004 TCGv_i64 tmphi = NULL;
1006 if (size < 4) {
1007 MemOp memop = s->be_data + size;
1008 tcg_gen_qemu_ld_i64(tmplo, tcg_addr, get_mem_index(s), memop);
1009 } else {
1010 bool be = s->be_data == MO_BE;
1011 TCGv_i64 tcg_hiaddr;
1013 tmphi = tcg_temp_new_i64();
1014 tcg_hiaddr = tcg_temp_new_i64();
1016 tcg_gen_addi_i64(tcg_hiaddr, tcg_addr, 8);
1017 tcg_gen_qemu_ld_i64(tmplo, be ? tcg_hiaddr : tcg_addr, get_mem_index(s),
1018 s->be_data | MO_Q);
1019 tcg_gen_qemu_ld_i64(tmphi, be ? tcg_addr : tcg_hiaddr, get_mem_index(s),
1020 s->be_data | MO_Q);
1021 tcg_temp_free_i64(tcg_hiaddr);
1024 tcg_gen_st_i64(tmplo, cpu_env, fp_reg_offset(s, destidx, MO_64));
1025 tcg_temp_free_i64(tmplo);
1027 if (tmphi) {
1028 tcg_gen_st_i64(tmphi, cpu_env, fp_reg_hi_offset(s, destidx));
1029 tcg_temp_free_i64(tmphi);
1031 clear_vec_high(s, tmphi != NULL, destidx);
1035 * Vector load/store helpers.
1037 * The principal difference between this and a FP load is that we don't
1038 * zero extend as we are filling a partial chunk of the vector register.
1039 * These functions don't support 128 bit loads/stores, which would be
1040 * normal load/store operations.
1042 * The _i32 versions are useful when operating on 32 bit quantities
1043 * (eg for floating point single or using Neon helper functions).
1046 /* Get value of an element within a vector register */
1047 static void read_vec_element(DisasContext *s, TCGv_i64 tcg_dest, int srcidx,
1048 int element, MemOp memop)
1050 int vect_off = vec_reg_offset(s, srcidx, element, memop & MO_SIZE);
1051 switch (memop) {
1052 case MO_8:
1053 tcg_gen_ld8u_i64(tcg_dest, cpu_env, vect_off);
1054 break;
1055 case MO_16:
1056 tcg_gen_ld16u_i64(tcg_dest, cpu_env, vect_off);
1057 break;
1058 case MO_32:
1059 tcg_gen_ld32u_i64(tcg_dest, cpu_env, vect_off);
1060 break;
1061 case MO_8|MO_SIGN:
1062 tcg_gen_ld8s_i64(tcg_dest, cpu_env, vect_off);
1063 break;
1064 case MO_16|MO_SIGN:
1065 tcg_gen_ld16s_i64(tcg_dest, cpu_env, vect_off);
1066 break;
1067 case MO_32|MO_SIGN:
1068 tcg_gen_ld32s_i64(tcg_dest, cpu_env, vect_off);
1069 break;
1070 case MO_64:
1071 case MO_64|MO_SIGN:
1072 tcg_gen_ld_i64(tcg_dest, cpu_env, vect_off);
1073 break;
1074 default:
1075 g_assert_not_reached();
1079 static void read_vec_element_i32(DisasContext *s, TCGv_i32 tcg_dest, int srcidx,
1080 int element, MemOp memop)
1082 int vect_off = vec_reg_offset(s, srcidx, element, memop & MO_SIZE);
1083 switch (memop) {
1084 case MO_8:
1085 tcg_gen_ld8u_i32(tcg_dest, cpu_env, vect_off);
1086 break;
1087 case MO_16:
1088 tcg_gen_ld16u_i32(tcg_dest, cpu_env, vect_off);
1089 break;
1090 case MO_8|MO_SIGN:
1091 tcg_gen_ld8s_i32(tcg_dest, cpu_env, vect_off);
1092 break;
1093 case MO_16|MO_SIGN:
1094 tcg_gen_ld16s_i32(tcg_dest, cpu_env, vect_off);
1095 break;
1096 case MO_32:
1097 case MO_32|MO_SIGN:
1098 tcg_gen_ld_i32(tcg_dest, cpu_env, vect_off);
1099 break;
1100 default:
1101 g_assert_not_reached();
1105 /* Set value of an element within a vector register */
1106 static void write_vec_element(DisasContext *s, TCGv_i64 tcg_src, int destidx,
1107 int element, MemOp memop)
1109 int vect_off = vec_reg_offset(s, destidx, element, memop & MO_SIZE);
1110 switch (memop) {
1111 case MO_8:
1112 tcg_gen_st8_i64(tcg_src, cpu_env, vect_off);
1113 break;
1114 case MO_16:
1115 tcg_gen_st16_i64(tcg_src, cpu_env, vect_off);
1116 break;
1117 case MO_32:
1118 tcg_gen_st32_i64(tcg_src, cpu_env, vect_off);
1119 break;
1120 case MO_64:
1121 tcg_gen_st_i64(tcg_src, cpu_env, vect_off);
1122 break;
1123 default:
1124 g_assert_not_reached();
1128 static void write_vec_element_i32(DisasContext *s, TCGv_i32 tcg_src,
1129 int destidx, int element, MemOp memop)
1131 int vect_off = vec_reg_offset(s, destidx, element, memop & MO_SIZE);
1132 switch (memop) {
1133 case MO_8:
1134 tcg_gen_st8_i32(tcg_src, cpu_env, vect_off);
1135 break;
1136 case MO_16:
1137 tcg_gen_st16_i32(tcg_src, cpu_env, vect_off);
1138 break;
1139 case MO_32:
1140 tcg_gen_st_i32(tcg_src, cpu_env, vect_off);
1141 break;
1142 default:
1143 g_assert_not_reached();
1147 /* Store from vector register to memory */
1148 static void do_vec_st(DisasContext *s, int srcidx, int element,
1149 TCGv_i64 tcg_addr, int size, MemOp endian)
1151 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
1153 read_vec_element(s, tcg_tmp, srcidx, element, size);
1154 tcg_gen_qemu_st_i64(tcg_tmp, tcg_addr, get_mem_index(s), endian | size);
1156 tcg_temp_free_i64(tcg_tmp);
1159 /* Load from memory to vector register */
1160 static void do_vec_ld(DisasContext *s, int destidx, int element,
1161 TCGv_i64 tcg_addr, int size, MemOp endian)
1163 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
1165 tcg_gen_qemu_ld_i64(tcg_tmp, tcg_addr, get_mem_index(s), endian | size);
1166 write_vec_element(s, tcg_tmp, destidx, element, size);
1168 tcg_temp_free_i64(tcg_tmp);
1171 /* Check that FP/Neon access is enabled. If it is, return
1172 * true. If not, emit code to generate an appropriate exception,
1173 * and return false; the caller should not emit any code for
1174 * the instruction. Note that this check must happen after all
1175 * unallocated-encoding checks (otherwise the syndrome information
1176 * for the resulting exception will be incorrect).
1178 static inline bool fp_access_check(DisasContext *s)
1180 assert(!s->fp_access_checked);
1181 s->fp_access_checked = true;
1183 if (!s->fp_excp_el) {
1184 return true;
1187 gen_exception_insn(s, s->pc_curr, EXCP_UDEF,
1188 syn_fp_access_trap(1, 0xe, false), s->fp_excp_el);
1189 return false;
1192 /* Check that SVE access is enabled. If it is, return true.
1193 * If not, emit code to generate an appropriate exception and return false.
1195 bool sve_access_check(DisasContext *s)
1197 if (s->sve_excp_el) {
1198 gen_exception_insn(s, s->pc_curr, EXCP_UDEF, syn_sve_access_trap(),
1199 s->sve_excp_el);
1200 return false;
1202 return fp_access_check(s);
1206 * This utility function is for doing register extension with an
1207 * optional shift. You will likely want to pass a temporary for the
1208 * destination register. See DecodeRegExtend() in the ARM ARM.
1210 static void ext_and_shift_reg(TCGv_i64 tcg_out, TCGv_i64 tcg_in,
1211 int option, unsigned int shift)
1213 int extsize = extract32(option, 0, 2);
1214 bool is_signed = extract32(option, 2, 1);
1216 if (is_signed) {
1217 switch (extsize) {
1218 case 0:
1219 tcg_gen_ext8s_i64(tcg_out, tcg_in);
1220 break;
1221 case 1:
1222 tcg_gen_ext16s_i64(tcg_out, tcg_in);
1223 break;
1224 case 2:
1225 tcg_gen_ext32s_i64(tcg_out, tcg_in);
1226 break;
1227 case 3:
1228 tcg_gen_mov_i64(tcg_out, tcg_in);
1229 break;
1231 } else {
1232 switch (extsize) {
1233 case 0:
1234 tcg_gen_ext8u_i64(tcg_out, tcg_in);
1235 break;
1236 case 1:
1237 tcg_gen_ext16u_i64(tcg_out, tcg_in);
1238 break;
1239 case 2:
1240 tcg_gen_ext32u_i64(tcg_out, tcg_in);
1241 break;
1242 case 3:
1243 tcg_gen_mov_i64(tcg_out, tcg_in);
1244 break;
1248 if (shift) {
1249 tcg_gen_shli_i64(tcg_out, tcg_out, shift);
1253 static inline void gen_check_sp_alignment(DisasContext *s)
1255 /* The AArch64 architecture mandates that (if enabled via PSTATE
1256 * or SCTLR bits) there is a check that SP is 16-aligned on every
1257 * SP-relative load or store (with an exception generated if it is not).
1258 * In line with general QEMU practice regarding misaligned accesses,
1259 * we omit these checks for the sake of guest program performance.
1260 * This function is provided as a hook so we can more easily add these
1261 * checks in future (possibly as a "favour catching guest program bugs
1262 * over speed" user selectable option).
1267 * This provides a simple table based table lookup decoder. It is
1268 * intended to be used when the relevant bits for decode are too
1269 * awkwardly placed and switch/if based logic would be confusing and
1270 * deeply nested. Since it's a linear search through the table, tables
1271 * should be kept small.
1273 * It returns the first handler where insn & mask == pattern, or
1274 * NULL if there is no match.
1275 * The table is terminated by an empty mask (i.e. 0)
1277 static inline AArch64DecodeFn *lookup_disas_fn(const AArch64DecodeTable *table,
1278 uint32_t insn)
1280 const AArch64DecodeTable *tptr = table;
1282 while (tptr->mask) {
1283 if ((insn & tptr->mask) == tptr->pattern) {
1284 return tptr->disas_fn;
1286 tptr++;
1288 return NULL;
1292 * The instruction disassembly implemented here matches
1293 * the instruction encoding classifications in chapter C4
1294 * of the ARM Architecture Reference Manual (DDI0487B_a);
1295 * classification names and decode diagrams here should generally
1296 * match up with those in the manual.
1299 /* Unconditional branch (immediate)
1300 * 31 30 26 25 0
1301 * +----+-----------+-------------------------------------+
1302 * | op | 0 0 1 0 1 | imm26 |
1303 * +----+-----------+-------------------------------------+
1305 static void disas_uncond_b_imm(DisasContext *s, uint32_t insn)
1307 uint64_t addr = s->pc_curr + sextract32(insn, 0, 26) * 4;
1309 if (insn & (1U << 31)) {
1310 /* BL Branch with link */
1311 tcg_gen_movi_i64(cpu_reg(s, 30), s->base.pc_next);
1314 /* B Branch / BL Branch with link */
1315 reset_btype(s);
1316 gen_goto_tb(s, 0, addr);
1319 /* Compare and branch (immediate)
1320 * 31 30 25 24 23 5 4 0
1321 * +----+-------------+----+---------------------+--------+
1322 * | sf | 0 1 1 0 1 0 | op | imm19 | Rt |
1323 * +----+-------------+----+---------------------+--------+
1325 static void disas_comp_b_imm(DisasContext *s, uint32_t insn)
1327 unsigned int sf, op, rt;
1328 uint64_t addr;
1329 TCGLabel *label_match;
1330 TCGv_i64 tcg_cmp;
1332 sf = extract32(insn, 31, 1);
1333 op = extract32(insn, 24, 1); /* 0: CBZ; 1: CBNZ */
1334 rt = extract32(insn, 0, 5);
1335 addr = s->pc_curr + sextract32(insn, 5, 19) * 4;
1337 tcg_cmp = read_cpu_reg(s, rt, sf);
1338 label_match = gen_new_label();
1340 reset_btype(s);
1341 tcg_gen_brcondi_i64(op ? TCG_COND_NE : TCG_COND_EQ,
1342 tcg_cmp, 0, label_match);
1344 gen_goto_tb(s, 0, s->base.pc_next);
1345 gen_set_label(label_match);
1346 gen_goto_tb(s, 1, addr);
1349 /* Test and branch (immediate)
1350 * 31 30 25 24 23 19 18 5 4 0
1351 * +----+-------------+----+-------+-------------+------+
1352 * | b5 | 0 1 1 0 1 1 | op | b40 | imm14 | Rt |
1353 * +----+-------------+----+-------+-------------+------+
1355 static void disas_test_b_imm(DisasContext *s, uint32_t insn)
1357 unsigned int bit_pos, op, rt;
1358 uint64_t addr;
1359 TCGLabel *label_match;
1360 TCGv_i64 tcg_cmp;
1362 bit_pos = (extract32(insn, 31, 1) << 5) | extract32(insn, 19, 5);
1363 op = extract32(insn, 24, 1); /* 0: TBZ; 1: TBNZ */
1364 addr = s->pc_curr + sextract32(insn, 5, 14) * 4;
1365 rt = extract32(insn, 0, 5);
1367 tcg_cmp = tcg_temp_new_i64();
1368 tcg_gen_andi_i64(tcg_cmp, cpu_reg(s, rt), (1ULL << bit_pos));
1369 label_match = gen_new_label();
1371 reset_btype(s);
1372 tcg_gen_brcondi_i64(op ? TCG_COND_NE : TCG_COND_EQ,
1373 tcg_cmp, 0, label_match);
1374 tcg_temp_free_i64(tcg_cmp);
1375 gen_goto_tb(s, 0, s->base.pc_next);
1376 gen_set_label(label_match);
1377 gen_goto_tb(s, 1, addr);
1380 /* Conditional branch (immediate)
1381 * 31 25 24 23 5 4 3 0
1382 * +---------------+----+---------------------+----+------+
1383 * | 0 1 0 1 0 1 0 | o1 | imm19 | o0 | cond |
1384 * +---------------+----+---------------------+----+------+
1386 static void disas_cond_b_imm(DisasContext *s, uint32_t insn)
1388 unsigned int cond;
1389 uint64_t addr;
1391 if ((insn & (1 << 4)) || (insn & (1 << 24))) {
1392 unallocated_encoding(s);
1393 return;
1395 addr = s->pc_curr + sextract32(insn, 5, 19) * 4;
1396 cond = extract32(insn, 0, 4);
1398 reset_btype(s);
1399 if (cond < 0x0e) {
1400 /* genuinely conditional branches */
1401 TCGLabel *label_match = gen_new_label();
1402 arm_gen_test_cc(cond, label_match);
1403 gen_goto_tb(s, 0, s->base.pc_next);
1404 gen_set_label(label_match);
1405 gen_goto_tb(s, 1, addr);
1406 } else {
1407 /* 0xe and 0xf are both "always" conditions */
1408 gen_goto_tb(s, 0, addr);
1412 /* HINT instruction group, including various allocated HINTs */
1413 static void handle_hint(DisasContext *s, uint32_t insn,
1414 unsigned int op1, unsigned int op2, unsigned int crm)
1416 unsigned int selector = crm << 3 | op2;
1418 if (op1 != 3) {
1419 unallocated_encoding(s);
1420 return;
1423 switch (selector) {
1424 case 0b00000: /* NOP */
1425 break;
1426 case 0b00011: /* WFI */
1427 s->base.is_jmp = DISAS_WFI;
1428 break;
1429 case 0b00001: /* YIELD */
1430 /* When running in MTTCG we don't generate jumps to the yield and
1431 * WFE helpers as it won't affect the scheduling of other vCPUs.
1432 * If we wanted to more completely model WFE/SEV so we don't busy
1433 * spin unnecessarily we would need to do something more involved.
1435 if (!(tb_cflags(s->base.tb) & CF_PARALLEL)) {
1436 s->base.is_jmp = DISAS_YIELD;
1438 break;
1439 case 0b00010: /* WFE */
1440 if (!(tb_cflags(s->base.tb) & CF_PARALLEL)) {
1441 s->base.is_jmp = DISAS_WFE;
1443 break;
1444 case 0b00100: /* SEV */
1445 case 0b00101: /* SEVL */
1446 /* we treat all as NOP at least for now */
1447 break;
1448 case 0b00111: /* XPACLRI */
1449 if (s->pauth_active) {
1450 gen_helper_xpaci(cpu_X[30], cpu_env, cpu_X[30]);
1452 break;
1453 case 0b01000: /* PACIA1716 */
1454 if (s->pauth_active) {
1455 gen_helper_pacia(cpu_X[17], cpu_env, cpu_X[17], cpu_X[16]);
1457 break;
1458 case 0b01010: /* PACIB1716 */
1459 if (s->pauth_active) {
1460 gen_helper_pacib(cpu_X[17], cpu_env, cpu_X[17], cpu_X[16]);
1462 break;
1463 case 0b01100: /* AUTIA1716 */
1464 if (s->pauth_active) {
1465 gen_helper_autia(cpu_X[17], cpu_env, cpu_X[17], cpu_X[16]);
1467 break;
1468 case 0b01110: /* AUTIB1716 */
1469 if (s->pauth_active) {
1470 gen_helper_autib(cpu_X[17], cpu_env, cpu_X[17], cpu_X[16]);
1472 break;
1473 case 0b11000: /* PACIAZ */
1474 if (s->pauth_active) {
1475 gen_helper_pacia(cpu_X[30], cpu_env, cpu_X[30],
1476 new_tmp_a64_zero(s));
1478 break;
1479 case 0b11001: /* PACIASP */
1480 if (s->pauth_active) {
1481 gen_helper_pacia(cpu_X[30], cpu_env, cpu_X[30], cpu_X[31]);
1483 break;
1484 case 0b11010: /* PACIBZ */
1485 if (s->pauth_active) {
1486 gen_helper_pacib(cpu_X[30], cpu_env, cpu_X[30],
1487 new_tmp_a64_zero(s));
1489 break;
1490 case 0b11011: /* PACIBSP */
1491 if (s->pauth_active) {
1492 gen_helper_pacib(cpu_X[30], cpu_env, cpu_X[30], cpu_X[31]);
1494 break;
1495 case 0b11100: /* AUTIAZ */
1496 if (s->pauth_active) {
1497 gen_helper_autia(cpu_X[30], cpu_env, cpu_X[30],
1498 new_tmp_a64_zero(s));
1500 break;
1501 case 0b11101: /* AUTIASP */
1502 if (s->pauth_active) {
1503 gen_helper_autia(cpu_X[30], cpu_env, cpu_X[30], cpu_X[31]);
1505 break;
1506 case 0b11110: /* AUTIBZ */
1507 if (s->pauth_active) {
1508 gen_helper_autib(cpu_X[30], cpu_env, cpu_X[30],
1509 new_tmp_a64_zero(s));
1511 break;
1512 case 0b11111: /* AUTIBSP */
1513 if (s->pauth_active) {
1514 gen_helper_autib(cpu_X[30], cpu_env, cpu_X[30], cpu_X[31]);
1516 break;
1517 default:
1518 /* default specified as NOP equivalent */
1519 break;
1523 static void gen_clrex(DisasContext *s, uint32_t insn)
1525 tcg_gen_movi_i64(cpu_exclusive_addr, -1);
1528 /* CLREX, DSB, DMB, ISB */
1529 static void handle_sync(DisasContext *s, uint32_t insn,
1530 unsigned int op1, unsigned int op2, unsigned int crm)
1532 TCGBar bar;
1534 if (op1 != 3) {
1535 unallocated_encoding(s);
1536 return;
1539 switch (op2) {
1540 case 2: /* CLREX */
1541 gen_clrex(s, insn);
1542 return;
1543 case 4: /* DSB */
1544 case 5: /* DMB */
1545 switch (crm & 3) {
1546 case 1: /* MBReqTypes_Reads */
1547 bar = TCG_BAR_SC | TCG_MO_LD_LD | TCG_MO_LD_ST;
1548 break;
1549 case 2: /* MBReqTypes_Writes */
1550 bar = TCG_BAR_SC | TCG_MO_ST_ST;
1551 break;
1552 default: /* MBReqTypes_All */
1553 bar = TCG_BAR_SC | TCG_MO_ALL;
1554 break;
1556 tcg_gen_mb(bar);
1557 return;
1558 case 6: /* ISB */
1559 /* We need to break the TB after this insn to execute
1560 * a self-modified code correctly and also to take
1561 * any pending interrupts immediately.
1563 reset_btype(s);
1564 gen_goto_tb(s, 0, s->base.pc_next);
1565 return;
1567 case 7: /* SB */
1568 if (crm != 0 || !dc_isar_feature(aa64_sb, s)) {
1569 goto do_unallocated;
1572 * TODO: There is no speculation barrier opcode for TCG;
1573 * MB and end the TB instead.
1575 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_SC);
1576 gen_goto_tb(s, 0, s->base.pc_next);
1577 return;
1579 default:
1580 do_unallocated:
1581 unallocated_encoding(s);
1582 return;
1586 static void gen_xaflag(void)
1588 TCGv_i32 z = tcg_temp_new_i32();
1590 tcg_gen_setcondi_i32(TCG_COND_EQ, z, cpu_ZF, 0);
1593 * (!C & !Z) << 31
1594 * (!(C | Z)) << 31
1595 * ~((C | Z) << 31)
1596 * ~-(C | Z)
1597 * (C | Z) - 1
1599 tcg_gen_or_i32(cpu_NF, cpu_CF, z);
1600 tcg_gen_subi_i32(cpu_NF, cpu_NF, 1);
1602 /* !(Z & C) */
1603 tcg_gen_and_i32(cpu_ZF, z, cpu_CF);
1604 tcg_gen_xori_i32(cpu_ZF, cpu_ZF, 1);
1606 /* (!C & Z) << 31 -> -(Z & ~C) */
1607 tcg_gen_andc_i32(cpu_VF, z, cpu_CF);
1608 tcg_gen_neg_i32(cpu_VF, cpu_VF);
1610 /* C | Z */
1611 tcg_gen_or_i32(cpu_CF, cpu_CF, z);
1613 tcg_temp_free_i32(z);
1616 static void gen_axflag(void)
1618 tcg_gen_sari_i32(cpu_VF, cpu_VF, 31); /* V ? -1 : 0 */
1619 tcg_gen_andc_i32(cpu_CF, cpu_CF, cpu_VF); /* C & !V */
1621 /* !(Z | V) -> !(!ZF | V) -> ZF & !V -> ZF & ~VF */
1622 tcg_gen_andc_i32(cpu_ZF, cpu_ZF, cpu_VF);
1624 tcg_gen_movi_i32(cpu_NF, 0);
1625 tcg_gen_movi_i32(cpu_VF, 0);
1628 /* MSR (immediate) - move immediate to processor state field */
1629 static void handle_msr_i(DisasContext *s, uint32_t insn,
1630 unsigned int op1, unsigned int op2, unsigned int crm)
1632 TCGv_i32 t1;
1633 int op = op1 << 3 | op2;
1635 /* End the TB by default, chaining is ok. */
1636 s->base.is_jmp = DISAS_TOO_MANY;
1638 switch (op) {
1639 case 0x00: /* CFINV */
1640 if (crm != 0 || !dc_isar_feature(aa64_condm_4, s)) {
1641 goto do_unallocated;
1643 tcg_gen_xori_i32(cpu_CF, cpu_CF, 1);
1644 s->base.is_jmp = DISAS_NEXT;
1645 break;
1647 case 0x01: /* XAFlag */
1648 if (crm != 0 || !dc_isar_feature(aa64_condm_5, s)) {
1649 goto do_unallocated;
1651 gen_xaflag();
1652 s->base.is_jmp = DISAS_NEXT;
1653 break;
1655 case 0x02: /* AXFlag */
1656 if (crm != 0 || !dc_isar_feature(aa64_condm_5, s)) {
1657 goto do_unallocated;
1659 gen_axflag();
1660 s->base.is_jmp = DISAS_NEXT;
1661 break;
1663 case 0x03: /* UAO */
1664 if (!dc_isar_feature(aa64_uao, s) || s->current_el == 0) {
1665 goto do_unallocated;
1667 if (crm & 1) {
1668 set_pstate_bits(PSTATE_UAO);
1669 } else {
1670 clear_pstate_bits(PSTATE_UAO);
1672 t1 = tcg_const_i32(s->current_el);
1673 gen_helper_rebuild_hflags_a64(cpu_env, t1);
1674 tcg_temp_free_i32(t1);
1675 break;
1677 case 0x04: /* PAN */
1678 if (!dc_isar_feature(aa64_pan, s) || s->current_el == 0) {
1679 goto do_unallocated;
1681 if (crm & 1) {
1682 set_pstate_bits(PSTATE_PAN);
1683 } else {
1684 clear_pstate_bits(PSTATE_PAN);
1686 t1 = tcg_const_i32(s->current_el);
1687 gen_helper_rebuild_hflags_a64(cpu_env, t1);
1688 tcg_temp_free_i32(t1);
1689 break;
1691 case 0x05: /* SPSel */
1692 if (s->current_el == 0) {
1693 goto do_unallocated;
1695 t1 = tcg_const_i32(crm & PSTATE_SP);
1696 gen_helper_msr_i_spsel(cpu_env, t1);
1697 tcg_temp_free_i32(t1);
1698 break;
1700 case 0x1e: /* DAIFSet */
1701 t1 = tcg_const_i32(crm);
1702 gen_helper_msr_i_daifset(cpu_env, t1);
1703 tcg_temp_free_i32(t1);
1704 break;
1706 case 0x1f: /* DAIFClear */
1707 t1 = tcg_const_i32(crm);
1708 gen_helper_msr_i_daifclear(cpu_env, t1);
1709 tcg_temp_free_i32(t1);
1710 /* For DAIFClear, exit the cpu loop to re-evaluate pending IRQs. */
1711 s->base.is_jmp = DISAS_UPDATE_EXIT;
1712 break;
1714 case 0x1c: /* TCO */
1715 if (dc_isar_feature(aa64_mte, s)) {
1716 /* Full MTE is enabled -- set the TCO bit as directed. */
1717 if (crm & 1) {
1718 set_pstate_bits(PSTATE_TCO);
1719 } else {
1720 clear_pstate_bits(PSTATE_TCO);
1722 t1 = tcg_const_i32(s->current_el);
1723 gen_helper_rebuild_hflags_a64(cpu_env, t1);
1724 tcg_temp_free_i32(t1);
1725 /* Many factors, including TCO, go into MTE_ACTIVE. */
1726 s->base.is_jmp = DISAS_UPDATE_NOCHAIN;
1727 } else if (dc_isar_feature(aa64_mte_insn_reg, s)) {
1728 /* Only "instructions accessible at EL0" -- PSTATE.TCO is WI. */
1729 s->base.is_jmp = DISAS_NEXT;
1730 } else {
1731 goto do_unallocated;
1733 break;
1735 default:
1736 do_unallocated:
1737 unallocated_encoding(s);
1738 return;
1742 static void gen_get_nzcv(TCGv_i64 tcg_rt)
1744 TCGv_i32 tmp = tcg_temp_new_i32();
1745 TCGv_i32 nzcv = tcg_temp_new_i32();
1747 /* build bit 31, N */
1748 tcg_gen_andi_i32(nzcv, cpu_NF, (1U << 31));
1749 /* build bit 30, Z */
1750 tcg_gen_setcondi_i32(TCG_COND_EQ, tmp, cpu_ZF, 0);
1751 tcg_gen_deposit_i32(nzcv, nzcv, tmp, 30, 1);
1752 /* build bit 29, C */
1753 tcg_gen_deposit_i32(nzcv, nzcv, cpu_CF, 29, 1);
1754 /* build bit 28, V */
1755 tcg_gen_shri_i32(tmp, cpu_VF, 31);
1756 tcg_gen_deposit_i32(nzcv, nzcv, tmp, 28, 1);
1757 /* generate result */
1758 tcg_gen_extu_i32_i64(tcg_rt, nzcv);
1760 tcg_temp_free_i32(nzcv);
1761 tcg_temp_free_i32(tmp);
1764 static void gen_set_nzcv(TCGv_i64 tcg_rt)
1766 TCGv_i32 nzcv = tcg_temp_new_i32();
1768 /* take NZCV from R[t] */
1769 tcg_gen_extrl_i64_i32(nzcv, tcg_rt);
1771 /* bit 31, N */
1772 tcg_gen_andi_i32(cpu_NF, nzcv, (1U << 31));
1773 /* bit 30, Z */
1774 tcg_gen_andi_i32(cpu_ZF, nzcv, (1 << 30));
1775 tcg_gen_setcondi_i32(TCG_COND_EQ, cpu_ZF, cpu_ZF, 0);
1776 /* bit 29, C */
1777 tcg_gen_andi_i32(cpu_CF, nzcv, (1 << 29));
1778 tcg_gen_shri_i32(cpu_CF, cpu_CF, 29);
1779 /* bit 28, V */
1780 tcg_gen_andi_i32(cpu_VF, nzcv, (1 << 28));
1781 tcg_gen_shli_i32(cpu_VF, cpu_VF, 3);
1782 tcg_temp_free_i32(nzcv);
1785 /* MRS - move from system register
1786 * MSR (register) - move to system register
1787 * SYS
1788 * SYSL
1789 * These are all essentially the same insn in 'read' and 'write'
1790 * versions, with varying op0 fields.
1792 static void handle_sys(DisasContext *s, uint32_t insn, bool isread,
1793 unsigned int op0, unsigned int op1, unsigned int op2,
1794 unsigned int crn, unsigned int crm, unsigned int rt)
1796 const ARMCPRegInfo *ri;
1797 TCGv_i64 tcg_rt;
1799 ri = get_arm_cp_reginfo(s->cp_regs,
1800 ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP,
1801 crn, crm, op0, op1, op2));
1803 if (!ri) {
1804 /* Unknown register; this might be a guest error or a QEMU
1805 * unimplemented feature.
1807 qemu_log_mask(LOG_UNIMP, "%s access to unsupported AArch64 "
1808 "system register op0:%d op1:%d crn:%d crm:%d op2:%d\n",
1809 isread ? "read" : "write", op0, op1, crn, crm, op2);
1810 unallocated_encoding(s);
1811 return;
1814 /* Check access permissions */
1815 if (!cp_access_ok(s->current_el, ri, isread)) {
1816 unallocated_encoding(s);
1817 return;
1820 if (ri->accessfn) {
1821 /* Emit code to perform further access permissions checks at
1822 * runtime; this may result in an exception.
1824 TCGv_ptr tmpptr;
1825 TCGv_i32 tcg_syn, tcg_isread;
1826 uint32_t syndrome;
1828 gen_a64_set_pc_im(s->pc_curr);
1829 tmpptr = tcg_const_ptr(ri);
1830 syndrome = syn_aa64_sysregtrap(op0, op1, op2, crn, crm, rt, isread);
1831 tcg_syn = tcg_const_i32(syndrome);
1832 tcg_isread = tcg_const_i32(isread);
1833 gen_helper_access_check_cp_reg(cpu_env, tmpptr, tcg_syn, tcg_isread);
1834 tcg_temp_free_ptr(tmpptr);
1835 tcg_temp_free_i32(tcg_syn);
1836 tcg_temp_free_i32(tcg_isread);
1837 } else if (ri->type & ARM_CP_RAISES_EXC) {
1839 * The readfn or writefn might raise an exception;
1840 * synchronize the CPU state in case it does.
1842 gen_a64_set_pc_im(s->pc_curr);
1845 /* Handle special cases first */
1846 switch (ri->type & ~(ARM_CP_FLAG_MASK & ~ARM_CP_SPECIAL)) {
1847 case ARM_CP_NOP:
1848 return;
1849 case ARM_CP_NZCV:
1850 tcg_rt = cpu_reg(s, rt);
1851 if (isread) {
1852 gen_get_nzcv(tcg_rt);
1853 } else {
1854 gen_set_nzcv(tcg_rt);
1856 return;
1857 case ARM_CP_CURRENTEL:
1858 /* Reads as current EL value from pstate, which is
1859 * guaranteed to be constant by the tb flags.
1861 tcg_rt = cpu_reg(s, rt);
1862 tcg_gen_movi_i64(tcg_rt, s->current_el << 2);
1863 return;
1864 case ARM_CP_DC_ZVA:
1865 /* Writes clear the aligned block of memory which rt points into. */
1866 if (s->mte_active[0]) {
1867 TCGv_i32 t_desc;
1868 int desc = 0;
1870 desc = FIELD_DP32(desc, MTEDESC, MIDX, get_mem_index(s));
1871 desc = FIELD_DP32(desc, MTEDESC, TBI, s->tbid);
1872 desc = FIELD_DP32(desc, MTEDESC, TCMA, s->tcma);
1873 t_desc = tcg_const_i32(desc);
1875 tcg_rt = new_tmp_a64(s);
1876 gen_helper_mte_check_zva(tcg_rt, cpu_env, t_desc, cpu_reg(s, rt));
1877 tcg_temp_free_i32(t_desc);
1878 } else {
1879 tcg_rt = clean_data_tbi(s, cpu_reg(s, rt));
1881 gen_helper_dc_zva(cpu_env, tcg_rt);
1882 return;
1883 case ARM_CP_DC_GVA:
1885 TCGv_i64 clean_addr, tag;
1888 * DC_GVA, like DC_ZVA, requires that we supply the original
1889 * pointer for an invalid page. Probe that address first.
1891 tcg_rt = cpu_reg(s, rt);
1892 clean_addr = clean_data_tbi(s, tcg_rt);
1893 gen_probe_access(s, clean_addr, MMU_DATA_STORE, MO_8);
1895 if (s->ata) {
1896 /* Extract the tag from the register to match STZGM. */
1897 tag = tcg_temp_new_i64();
1898 tcg_gen_shri_i64(tag, tcg_rt, 56);
1899 gen_helper_stzgm_tags(cpu_env, clean_addr, tag);
1900 tcg_temp_free_i64(tag);
1903 return;
1904 case ARM_CP_DC_GZVA:
1906 TCGv_i64 clean_addr, tag;
1908 /* For DC_GZVA, we can rely on DC_ZVA for the proper fault. */
1909 tcg_rt = cpu_reg(s, rt);
1910 clean_addr = clean_data_tbi(s, tcg_rt);
1911 gen_helper_dc_zva(cpu_env, clean_addr);
1913 if (s->ata) {
1914 /* Extract the tag from the register to match STZGM. */
1915 tag = tcg_temp_new_i64();
1916 tcg_gen_shri_i64(tag, tcg_rt, 56);
1917 gen_helper_stzgm_tags(cpu_env, clean_addr, tag);
1918 tcg_temp_free_i64(tag);
1921 return;
1922 default:
1923 break;
1925 if ((ri->type & ARM_CP_FPU) && !fp_access_check(s)) {
1926 return;
1927 } else if ((ri->type & ARM_CP_SVE) && !sve_access_check(s)) {
1928 return;
1931 if ((tb_cflags(s->base.tb) & CF_USE_ICOUNT) && (ri->type & ARM_CP_IO)) {
1932 gen_io_start();
1935 tcg_rt = cpu_reg(s, rt);
1937 if (isread) {
1938 if (ri->type & ARM_CP_CONST) {
1939 tcg_gen_movi_i64(tcg_rt, ri->resetvalue);
1940 } else if (ri->readfn) {
1941 TCGv_ptr tmpptr;
1942 tmpptr = tcg_const_ptr(ri);
1943 gen_helper_get_cp_reg64(tcg_rt, cpu_env, tmpptr);
1944 tcg_temp_free_ptr(tmpptr);
1945 } else {
1946 tcg_gen_ld_i64(tcg_rt, cpu_env, ri->fieldoffset);
1948 } else {
1949 if (ri->type & ARM_CP_CONST) {
1950 /* If not forbidden by access permissions, treat as WI */
1951 return;
1952 } else if (ri->writefn) {
1953 TCGv_ptr tmpptr;
1954 tmpptr = tcg_const_ptr(ri);
1955 gen_helper_set_cp_reg64(cpu_env, tmpptr, tcg_rt);
1956 tcg_temp_free_ptr(tmpptr);
1957 } else {
1958 tcg_gen_st_i64(tcg_rt, cpu_env, ri->fieldoffset);
1962 if ((tb_cflags(s->base.tb) & CF_USE_ICOUNT) && (ri->type & ARM_CP_IO)) {
1963 /* I/O operations must end the TB here (whether read or write) */
1964 s->base.is_jmp = DISAS_UPDATE_EXIT;
1966 if (!isread && !(ri->type & ARM_CP_SUPPRESS_TB_END)) {
1968 * A write to any coprocessor regiser that ends a TB
1969 * must rebuild the hflags for the next TB.
1971 TCGv_i32 tcg_el = tcg_const_i32(s->current_el);
1972 gen_helper_rebuild_hflags_a64(cpu_env, tcg_el);
1973 tcg_temp_free_i32(tcg_el);
1975 * We default to ending the TB on a coprocessor register write,
1976 * but allow this to be suppressed by the register definition
1977 * (usually only necessary to work around guest bugs).
1979 s->base.is_jmp = DISAS_UPDATE_EXIT;
1983 /* System
1984 * 31 22 21 20 19 18 16 15 12 11 8 7 5 4 0
1985 * +---------------------+---+-----+-----+-------+-------+-----+------+
1986 * | 1 1 0 1 0 1 0 1 0 0 | L | op0 | op1 | CRn | CRm | op2 | Rt |
1987 * +---------------------+---+-----+-----+-------+-------+-----+------+
1989 static void disas_system(DisasContext *s, uint32_t insn)
1991 unsigned int l, op0, op1, crn, crm, op2, rt;
1992 l = extract32(insn, 21, 1);
1993 op0 = extract32(insn, 19, 2);
1994 op1 = extract32(insn, 16, 3);
1995 crn = extract32(insn, 12, 4);
1996 crm = extract32(insn, 8, 4);
1997 op2 = extract32(insn, 5, 3);
1998 rt = extract32(insn, 0, 5);
2000 if (op0 == 0) {
2001 if (l || rt != 31) {
2002 unallocated_encoding(s);
2003 return;
2005 switch (crn) {
2006 case 2: /* HINT (including allocated hints like NOP, YIELD, etc) */
2007 handle_hint(s, insn, op1, op2, crm);
2008 break;
2009 case 3: /* CLREX, DSB, DMB, ISB */
2010 handle_sync(s, insn, op1, op2, crm);
2011 break;
2012 case 4: /* MSR (immediate) */
2013 handle_msr_i(s, insn, op1, op2, crm);
2014 break;
2015 default:
2016 unallocated_encoding(s);
2017 break;
2019 return;
2021 handle_sys(s, insn, l, op0, op1, op2, crn, crm, rt);
2024 /* Exception generation
2026 * 31 24 23 21 20 5 4 2 1 0
2027 * +-----------------+-----+------------------------+-----+----+
2028 * | 1 1 0 1 0 1 0 0 | opc | imm16 | op2 | LL |
2029 * +-----------------------+------------------------+----------+
2031 static void disas_exc(DisasContext *s, uint32_t insn)
2033 int opc = extract32(insn, 21, 3);
2034 int op2_ll = extract32(insn, 0, 5);
2035 int imm16 = extract32(insn, 5, 16);
2036 TCGv_i32 tmp;
2038 switch (opc) {
2039 case 0:
2040 /* For SVC, HVC and SMC we advance the single-step state
2041 * machine before taking the exception. This is architecturally
2042 * mandated, to ensure that single-stepping a system call
2043 * instruction works properly.
2045 switch (op2_ll) {
2046 case 1: /* SVC */
2047 gen_ss_advance(s);
2048 gen_exception_insn(s, s->base.pc_next, EXCP_SWI,
2049 syn_aa64_svc(imm16), default_exception_el(s));
2050 break;
2051 case 2: /* HVC */
2052 if (s->current_el == 0) {
2053 unallocated_encoding(s);
2054 break;
2056 /* The pre HVC helper handles cases when HVC gets trapped
2057 * as an undefined insn by runtime configuration.
2059 gen_a64_set_pc_im(s->pc_curr);
2060 gen_helper_pre_hvc(cpu_env);
2061 gen_ss_advance(s);
2062 gen_exception_insn(s, s->base.pc_next, EXCP_HVC,
2063 syn_aa64_hvc(imm16), 2);
2064 break;
2065 case 3: /* SMC */
2066 if (s->current_el == 0) {
2067 unallocated_encoding(s);
2068 break;
2070 gen_a64_set_pc_im(s->pc_curr);
2071 tmp = tcg_const_i32(syn_aa64_smc(imm16));
2072 gen_helper_pre_smc(cpu_env, tmp);
2073 tcg_temp_free_i32(tmp);
2074 gen_ss_advance(s);
2075 gen_exception_insn(s, s->base.pc_next, EXCP_SMC,
2076 syn_aa64_smc(imm16), 3);
2077 break;
2078 default:
2079 unallocated_encoding(s);
2080 break;
2082 break;
2083 case 1:
2084 if (op2_ll != 0) {
2085 unallocated_encoding(s);
2086 break;
2088 /* BRK */
2089 gen_exception_bkpt_insn(s, syn_aa64_bkpt(imm16));
2090 break;
2091 case 2:
2092 if (op2_ll != 0) {
2093 unallocated_encoding(s);
2094 break;
2096 /* HLT. This has two purposes.
2097 * Architecturally, it is an external halting debug instruction.
2098 * Since QEMU doesn't implement external debug, we treat this as
2099 * it is required for halting debug disabled: it will UNDEF.
2100 * Secondly, "HLT 0xf000" is the A64 semihosting syscall instruction.
2102 if (semihosting_enabled() && imm16 == 0xf000) {
2103 #ifndef CONFIG_USER_ONLY
2104 /* In system mode, don't allow userspace access to semihosting,
2105 * to provide some semblance of security (and for consistency
2106 * with our 32-bit semihosting).
2108 if (s->current_el == 0) {
2109 unsupported_encoding(s, insn);
2110 break;
2112 #endif
2113 gen_exception_internal_insn(s, s->pc_curr, EXCP_SEMIHOST);
2114 } else {
2115 unsupported_encoding(s, insn);
2117 break;
2118 case 5:
2119 if (op2_ll < 1 || op2_ll > 3) {
2120 unallocated_encoding(s);
2121 break;
2123 /* DCPS1, DCPS2, DCPS3 */
2124 unsupported_encoding(s, insn);
2125 break;
2126 default:
2127 unallocated_encoding(s);
2128 break;
2132 /* Unconditional branch (register)
2133 * 31 25 24 21 20 16 15 10 9 5 4 0
2134 * +---------------+-------+-------+-------+------+-------+
2135 * | 1 1 0 1 0 1 1 | opc | op2 | op3 | Rn | op4 |
2136 * +---------------+-------+-------+-------+------+-------+
2138 static void disas_uncond_b_reg(DisasContext *s, uint32_t insn)
2140 unsigned int opc, op2, op3, rn, op4;
2141 unsigned btype_mod = 2; /* 0: BR, 1: BLR, 2: other */
2142 TCGv_i64 dst;
2143 TCGv_i64 modifier;
2145 opc = extract32(insn, 21, 4);
2146 op2 = extract32(insn, 16, 5);
2147 op3 = extract32(insn, 10, 6);
2148 rn = extract32(insn, 5, 5);
2149 op4 = extract32(insn, 0, 5);
2151 if (op2 != 0x1f) {
2152 goto do_unallocated;
2155 switch (opc) {
2156 case 0: /* BR */
2157 case 1: /* BLR */
2158 case 2: /* RET */
2159 btype_mod = opc;
2160 switch (op3) {
2161 case 0:
2162 /* BR, BLR, RET */
2163 if (op4 != 0) {
2164 goto do_unallocated;
2166 dst = cpu_reg(s, rn);
2167 break;
2169 case 2:
2170 case 3:
2171 if (!dc_isar_feature(aa64_pauth, s)) {
2172 goto do_unallocated;
2174 if (opc == 2) {
2175 /* RETAA, RETAB */
2176 if (rn != 0x1f || op4 != 0x1f) {
2177 goto do_unallocated;
2179 rn = 30;
2180 modifier = cpu_X[31];
2181 } else {
2182 /* BRAAZ, BRABZ, BLRAAZ, BLRABZ */
2183 if (op4 != 0x1f) {
2184 goto do_unallocated;
2186 modifier = new_tmp_a64_zero(s);
2188 if (s->pauth_active) {
2189 dst = new_tmp_a64(s);
2190 if (op3 == 2) {
2191 gen_helper_autia(dst, cpu_env, cpu_reg(s, rn), modifier);
2192 } else {
2193 gen_helper_autib(dst, cpu_env, cpu_reg(s, rn), modifier);
2195 } else {
2196 dst = cpu_reg(s, rn);
2198 break;
2200 default:
2201 goto do_unallocated;
2203 gen_a64_set_pc(s, dst);
2204 /* BLR also needs to load return address */
2205 if (opc == 1) {
2206 tcg_gen_movi_i64(cpu_reg(s, 30), s->base.pc_next);
2208 break;
2210 case 8: /* BRAA */
2211 case 9: /* BLRAA */
2212 if (!dc_isar_feature(aa64_pauth, s)) {
2213 goto do_unallocated;
2215 if ((op3 & ~1) != 2) {
2216 goto do_unallocated;
2218 btype_mod = opc & 1;
2219 if (s->pauth_active) {
2220 dst = new_tmp_a64(s);
2221 modifier = cpu_reg_sp(s, op4);
2222 if (op3 == 2) {
2223 gen_helper_autia(dst, cpu_env, cpu_reg(s, rn), modifier);
2224 } else {
2225 gen_helper_autib(dst, cpu_env, cpu_reg(s, rn), modifier);
2227 } else {
2228 dst = cpu_reg(s, rn);
2230 gen_a64_set_pc(s, dst);
2231 /* BLRAA also needs to load return address */
2232 if (opc == 9) {
2233 tcg_gen_movi_i64(cpu_reg(s, 30), s->base.pc_next);
2235 break;
2237 case 4: /* ERET */
2238 if (s->current_el == 0) {
2239 goto do_unallocated;
2241 switch (op3) {
2242 case 0: /* ERET */
2243 if (op4 != 0) {
2244 goto do_unallocated;
2246 dst = tcg_temp_new_i64();
2247 tcg_gen_ld_i64(dst, cpu_env,
2248 offsetof(CPUARMState, elr_el[s->current_el]));
2249 break;
2251 case 2: /* ERETAA */
2252 case 3: /* ERETAB */
2253 if (!dc_isar_feature(aa64_pauth, s)) {
2254 goto do_unallocated;
2256 if (rn != 0x1f || op4 != 0x1f) {
2257 goto do_unallocated;
2259 dst = tcg_temp_new_i64();
2260 tcg_gen_ld_i64(dst, cpu_env,
2261 offsetof(CPUARMState, elr_el[s->current_el]));
2262 if (s->pauth_active) {
2263 modifier = cpu_X[31];
2264 if (op3 == 2) {
2265 gen_helper_autia(dst, cpu_env, dst, modifier);
2266 } else {
2267 gen_helper_autib(dst, cpu_env, dst, modifier);
2270 break;
2272 default:
2273 goto do_unallocated;
2275 if (tb_cflags(s->base.tb) & CF_USE_ICOUNT) {
2276 gen_io_start();
2279 gen_helper_exception_return(cpu_env, dst);
2280 tcg_temp_free_i64(dst);
2281 /* Must exit loop to check un-masked IRQs */
2282 s->base.is_jmp = DISAS_EXIT;
2283 return;
2285 case 5: /* DRPS */
2286 if (op3 != 0 || op4 != 0 || rn != 0x1f) {
2287 goto do_unallocated;
2288 } else {
2289 unsupported_encoding(s, insn);
2291 return;
2293 default:
2294 do_unallocated:
2295 unallocated_encoding(s);
2296 return;
2299 switch (btype_mod) {
2300 case 0: /* BR */
2301 if (dc_isar_feature(aa64_bti, s)) {
2302 /* BR to {x16,x17} or !guard -> 1, else 3. */
2303 set_btype(s, rn == 16 || rn == 17 || !s->guarded_page ? 1 : 3);
2305 break;
2307 case 1: /* BLR */
2308 if (dc_isar_feature(aa64_bti, s)) {
2309 /* BLR sets BTYPE to 2, regardless of source guarded page. */
2310 set_btype(s, 2);
2312 break;
2314 default: /* RET or none of the above. */
2315 /* BTYPE will be set to 0 by normal end-of-insn processing. */
2316 break;
2319 s->base.is_jmp = DISAS_JUMP;
2322 /* Branches, exception generating and system instructions */
2323 static void disas_b_exc_sys(DisasContext *s, uint32_t insn)
2325 switch (extract32(insn, 25, 7)) {
2326 case 0x0a: case 0x0b:
2327 case 0x4a: case 0x4b: /* Unconditional branch (immediate) */
2328 disas_uncond_b_imm(s, insn);
2329 break;
2330 case 0x1a: case 0x5a: /* Compare & branch (immediate) */
2331 disas_comp_b_imm(s, insn);
2332 break;
2333 case 0x1b: case 0x5b: /* Test & branch (immediate) */
2334 disas_test_b_imm(s, insn);
2335 break;
2336 case 0x2a: /* Conditional branch (immediate) */
2337 disas_cond_b_imm(s, insn);
2338 break;
2339 case 0x6a: /* Exception generation / System */
2340 if (insn & (1 << 24)) {
2341 if (extract32(insn, 22, 2) == 0) {
2342 disas_system(s, insn);
2343 } else {
2344 unallocated_encoding(s);
2346 } else {
2347 disas_exc(s, insn);
2349 break;
2350 case 0x6b: /* Unconditional branch (register) */
2351 disas_uncond_b_reg(s, insn);
2352 break;
2353 default:
2354 unallocated_encoding(s);
2355 break;
2360 * Load/Store exclusive instructions are implemented by remembering
2361 * the value/address loaded, and seeing if these are the same
2362 * when the store is performed. This is not actually the architecturally
2363 * mandated semantics, but it works for typical guest code sequences
2364 * and avoids having to monitor regular stores.
2366 * The store exclusive uses the atomic cmpxchg primitives to avoid
2367 * races in multi-threaded linux-user and when MTTCG softmmu is
2368 * enabled.
2370 static void gen_load_exclusive(DisasContext *s, int rt, int rt2,
2371 TCGv_i64 addr, int size, bool is_pair)
2373 int idx = get_mem_index(s);
2374 MemOp memop = s->be_data;
2376 g_assert(size <= 3);
2377 if (is_pair) {
2378 g_assert(size >= 2);
2379 if (size == 2) {
2380 /* The pair must be single-copy atomic for the doubleword. */
2381 memop |= MO_64 | MO_ALIGN;
2382 tcg_gen_qemu_ld_i64(cpu_exclusive_val, addr, idx, memop);
2383 if (s->be_data == MO_LE) {
2384 tcg_gen_extract_i64(cpu_reg(s, rt), cpu_exclusive_val, 0, 32);
2385 tcg_gen_extract_i64(cpu_reg(s, rt2), cpu_exclusive_val, 32, 32);
2386 } else {
2387 tcg_gen_extract_i64(cpu_reg(s, rt), cpu_exclusive_val, 32, 32);
2388 tcg_gen_extract_i64(cpu_reg(s, rt2), cpu_exclusive_val, 0, 32);
2390 } else {
2391 /* The pair must be single-copy atomic for *each* doubleword, not
2392 the entire quadword, however it must be quadword aligned. */
2393 memop |= MO_64;
2394 tcg_gen_qemu_ld_i64(cpu_exclusive_val, addr, idx,
2395 memop | MO_ALIGN_16);
2397 TCGv_i64 addr2 = tcg_temp_new_i64();
2398 tcg_gen_addi_i64(addr2, addr, 8);
2399 tcg_gen_qemu_ld_i64(cpu_exclusive_high, addr2, idx, memop);
2400 tcg_temp_free_i64(addr2);
2402 tcg_gen_mov_i64(cpu_reg(s, rt), cpu_exclusive_val);
2403 tcg_gen_mov_i64(cpu_reg(s, rt2), cpu_exclusive_high);
2405 } else {
2406 memop |= size | MO_ALIGN;
2407 tcg_gen_qemu_ld_i64(cpu_exclusive_val, addr, idx, memop);
2408 tcg_gen_mov_i64(cpu_reg(s, rt), cpu_exclusive_val);
2410 tcg_gen_mov_i64(cpu_exclusive_addr, addr);
2413 static void gen_store_exclusive(DisasContext *s, int rd, int rt, int rt2,
2414 TCGv_i64 addr, int size, int is_pair)
2416 /* if (env->exclusive_addr == addr && env->exclusive_val == [addr]
2417 * && (!is_pair || env->exclusive_high == [addr + datasize])) {
2418 * [addr] = {Rt};
2419 * if (is_pair) {
2420 * [addr + datasize] = {Rt2};
2422 * {Rd} = 0;
2423 * } else {
2424 * {Rd} = 1;
2426 * env->exclusive_addr = -1;
2428 TCGLabel *fail_label = gen_new_label();
2429 TCGLabel *done_label = gen_new_label();
2430 TCGv_i64 tmp;
2432 tcg_gen_brcond_i64(TCG_COND_NE, addr, cpu_exclusive_addr, fail_label);
2434 tmp = tcg_temp_new_i64();
2435 if (is_pair) {
2436 if (size == 2) {
2437 if (s->be_data == MO_LE) {
2438 tcg_gen_concat32_i64(tmp, cpu_reg(s, rt), cpu_reg(s, rt2));
2439 } else {
2440 tcg_gen_concat32_i64(tmp, cpu_reg(s, rt2), cpu_reg(s, rt));
2442 tcg_gen_atomic_cmpxchg_i64(tmp, cpu_exclusive_addr,
2443 cpu_exclusive_val, tmp,
2444 get_mem_index(s),
2445 MO_64 | MO_ALIGN | s->be_data);
2446 tcg_gen_setcond_i64(TCG_COND_NE, tmp, tmp, cpu_exclusive_val);
2447 } else if (tb_cflags(s->base.tb) & CF_PARALLEL) {
2448 if (!HAVE_CMPXCHG128) {
2449 gen_helper_exit_atomic(cpu_env);
2450 s->base.is_jmp = DISAS_NORETURN;
2451 } else if (s->be_data == MO_LE) {
2452 gen_helper_paired_cmpxchg64_le_parallel(tmp, cpu_env,
2453 cpu_exclusive_addr,
2454 cpu_reg(s, rt),
2455 cpu_reg(s, rt2));
2456 } else {
2457 gen_helper_paired_cmpxchg64_be_parallel(tmp, cpu_env,
2458 cpu_exclusive_addr,
2459 cpu_reg(s, rt),
2460 cpu_reg(s, rt2));
2462 } else if (s->be_data == MO_LE) {
2463 gen_helper_paired_cmpxchg64_le(tmp, cpu_env, cpu_exclusive_addr,
2464 cpu_reg(s, rt), cpu_reg(s, rt2));
2465 } else {
2466 gen_helper_paired_cmpxchg64_be(tmp, cpu_env, cpu_exclusive_addr,
2467 cpu_reg(s, rt), cpu_reg(s, rt2));
2469 } else {
2470 tcg_gen_atomic_cmpxchg_i64(tmp, cpu_exclusive_addr, cpu_exclusive_val,
2471 cpu_reg(s, rt), get_mem_index(s),
2472 size | MO_ALIGN | s->be_data);
2473 tcg_gen_setcond_i64(TCG_COND_NE, tmp, tmp, cpu_exclusive_val);
2475 tcg_gen_mov_i64(cpu_reg(s, rd), tmp);
2476 tcg_temp_free_i64(tmp);
2477 tcg_gen_br(done_label);
2479 gen_set_label(fail_label);
2480 tcg_gen_movi_i64(cpu_reg(s, rd), 1);
2481 gen_set_label(done_label);
2482 tcg_gen_movi_i64(cpu_exclusive_addr, -1);
2485 static void gen_compare_and_swap(DisasContext *s, int rs, int rt,
2486 int rn, int size)
2488 TCGv_i64 tcg_rs = cpu_reg(s, rs);
2489 TCGv_i64 tcg_rt = cpu_reg(s, rt);
2490 int memidx = get_mem_index(s);
2491 TCGv_i64 clean_addr;
2493 if (rn == 31) {
2494 gen_check_sp_alignment(s);
2496 clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn), true, rn != 31, size);
2497 tcg_gen_atomic_cmpxchg_i64(tcg_rs, clean_addr, tcg_rs, tcg_rt, memidx,
2498 size | MO_ALIGN | s->be_data);
2501 static void gen_compare_and_swap_pair(DisasContext *s, int rs, int rt,
2502 int rn, int size)
2504 TCGv_i64 s1 = cpu_reg(s, rs);
2505 TCGv_i64 s2 = cpu_reg(s, rs + 1);
2506 TCGv_i64 t1 = cpu_reg(s, rt);
2507 TCGv_i64 t2 = cpu_reg(s, rt + 1);
2508 TCGv_i64 clean_addr;
2509 int memidx = get_mem_index(s);
2511 if (rn == 31) {
2512 gen_check_sp_alignment(s);
2515 /* This is a single atomic access, despite the "pair". */
2516 clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn), true, rn != 31, size + 1);
2518 if (size == 2) {
2519 TCGv_i64 cmp = tcg_temp_new_i64();
2520 TCGv_i64 val = tcg_temp_new_i64();
2522 if (s->be_data == MO_LE) {
2523 tcg_gen_concat32_i64(val, t1, t2);
2524 tcg_gen_concat32_i64(cmp, s1, s2);
2525 } else {
2526 tcg_gen_concat32_i64(val, t2, t1);
2527 tcg_gen_concat32_i64(cmp, s2, s1);
2530 tcg_gen_atomic_cmpxchg_i64(cmp, clean_addr, cmp, val, memidx,
2531 MO_64 | MO_ALIGN | s->be_data);
2532 tcg_temp_free_i64(val);
2534 if (s->be_data == MO_LE) {
2535 tcg_gen_extr32_i64(s1, s2, cmp);
2536 } else {
2537 tcg_gen_extr32_i64(s2, s1, cmp);
2539 tcg_temp_free_i64(cmp);
2540 } else if (tb_cflags(s->base.tb) & CF_PARALLEL) {
2541 if (HAVE_CMPXCHG128) {
2542 TCGv_i32 tcg_rs = tcg_const_i32(rs);
2543 if (s->be_data == MO_LE) {
2544 gen_helper_casp_le_parallel(cpu_env, tcg_rs,
2545 clean_addr, t1, t2);
2546 } else {
2547 gen_helper_casp_be_parallel(cpu_env, tcg_rs,
2548 clean_addr, t1, t2);
2550 tcg_temp_free_i32(tcg_rs);
2551 } else {
2552 gen_helper_exit_atomic(cpu_env);
2553 s->base.is_jmp = DISAS_NORETURN;
2555 } else {
2556 TCGv_i64 d1 = tcg_temp_new_i64();
2557 TCGv_i64 d2 = tcg_temp_new_i64();
2558 TCGv_i64 a2 = tcg_temp_new_i64();
2559 TCGv_i64 c1 = tcg_temp_new_i64();
2560 TCGv_i64 c2 = tcg_temp_new_i64();
2561 TCGv_i64 zero = tcg_const_i64(0);
2563 /* Load the two words, in memory order. */
2564 tcg_gen_qemu_ld_i64(d1, clean_addr, memidx,
2565 MO_64 | MO_ALIGN_16 | s->be_data);
2566 tcg_gen_addi_i64(a2, clean_addr, 8);
2567 tcg_gen_qemu_ld_i64(d2, a2, memidx, MO_64 | s->be_data);
2569 /* Compare the two words, also in memory order. */
2570 tcg_gen_setcond_i64(TCG_COND_EQ, c1, d1, s1);
2571 tcg_gen_setcond_i64(TCG_COND_EQ, c2, d2, s2);
2572 tcg_gen_and_i64(c2, c2, c1);
2574 /* If compare equal, write back new data, else write back old data. */
2575 tcg_gen_movcond_i64(TCG_COND_NE, c1, c2, zero, t1, d1);
2576 tcg_gen_movcond_i64(TCG_COND_NE, c2, c2, zero, t2, d2);
2577 tcg_gen_qemu_st_i64(c1, clean_addr, memidx, MO_64 | s->be_data);
2578 tcg_gen_qemu_st_i64(c2, a2, memidx, MO_64 | s->be_data);
2579 tcg_temp_free_i64(a2);
2580 tcg_temp_free_i64(c1);
2581 tcg_temp_free_i64(c2);
2582 tcg_temp_free_i64(zero);
2584 /* Write back the data from memory to Rs. */
2585 tcg_gen_mov_i64(s1, d1);
2586 tcg_gen_mov_i64(s2, d2);
2587 tcg_temp_free_i64(d1);
2588 tcg_temp_free_i64(d2);
2592 /* Update the Sixty-Four bit (SF) registersize. This logic is derived
2593 * from the ARMv8 specs for LDR (Shared decode for all encodings).
2595 static bool disas_ldst_compute_iss_sf(int size, bool is_signed, int opc)
2597 int opc0 = extract32(opc, 0, 1);
2598 int regsize;
2600 if (is_signed) {
2601 regsize = opc0 ? 32 : 64;
2602 } else {
2603 regsize = size == 3 ? 64 : 32;
2605 return regsize == 64;
2608 /* Load/store exclusive
2610 * 31 30 29 24 23 22 21 20 16 15 14 10 9 5 4 0
2611 * +-----+-------------+----+---+----+------+----+-------+------+------+
2612 * | sz | 0 0 1 0 0 0 | o2 | L | o1 | Rs | o0 | Rt2 | Rn | Rt |
2613 * +-----+-------------+----+---+----+------+----+-------+------+------+
2615 * sz: 00 -> 8 bit, 01 -> 16 bit, 10 -> 32 bit, 11 -> 64 bit
2616 * L: 0 -> store, 1 -> load
2617 * o2: 0 -> exclusive, 1 -> not
2618 * o1: 0 -> single register, 1 -> register pair
2619 * o0: 1 -> load-acquire/store-release, 0 -> not
2621 static void disas_ldst_excl(DisasContext *s, uint32_t insn)
2623 int rt = extract32(insn, 0, 5);
2624 int rn = extract32(insn, 5, 5);
2625 int rt2 = extract32(insn, 10, 5);
2626 int rs = extract32(insn, 16, 5);
2627 int is_lasr = extract32(insn, 15, 1);
2628 int o2_L_o1_o0 = extract32(insn, 21, 3) * 2 | is_lasr;
2629 int size = extract32(insn, 30, 2);
2630 TCGv_i64 clean_addr;
2632 switch (o2_L_o1_o0) {
2633 case 0x0: /* STXR */
2634 case 0x1: /* STLXR */
2635 if (rn == 31) {
2636 gen_check_sp_alignment(s);
2638 if (is_lasr) {
2639 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2641 clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn),
2642 true, rn != 31, size);
2643 gen_store_exclusive(s, rs, rt, rt2, clean_addr, size, false);
2644 return;
2646 case 0x4: /* LDXR */
2647 case 0x5: /* LDAXR */
2648 if (rn == 31) {
2649 gen_check_sp_alignment(s);
2651 clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn),
2652 false, rn != 31, size);
2653 s->is_ldex = true;
2654 gen_load_exclusive(s, rt, rt2, clean_addr, size, false);
2655 if (is_lasr) {
2656 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2658 return;
2660 case 0x8: /* STLLR */
2661 if (!dc_isar_feature(aa64_lor, s)) {
2662 break;
2664 /* StoreLORelease is the same as Store-Release for QEMU. */
2665 /* fall through */
2666 case 0x9: /* STLR */
2667 /* Generate ISS for non-exclusive accesses including LASR. */
2668 if (rn == 31) {
2669 gen_check_sp_alignment(s);
2671 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2672 clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn),
2673 true, rn != 31, size);
2674 do_gpr_st(s, cpu_reg(s, rt), clean_addr, size, true, rt,
2675 disas_ldst_compute_iss_sf(size, false, 0), is_lasr);
2676 return;
2678 case 0xc: /* LDLAR */
2679 if (!dc_isar_feature(aa64_lor, s)) {
2680 break;
2682 /* LoadLOAcquire is the same as Load-Acquire for QEMU. */
2683 /* fall through */
2684 case 0xd: /* LDAR */
2685 /* Generate ISS for non-exclusive accesses including LASR. */
2686 if (rn == 31) {
2687 gen_check_sp_alignment(s);
2689 clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn),
2690 false, rn != 31, size);
2691 do_gpr_ld(s, cpu_reg(s, rt), clean_addr, size, false, false, true, rt,
2692 disas_ldst_compute_iss_sf(size, false, 0), is_lasr);
2693 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2694 return;
2696 case 0x2: case 0x3: /* CASP / STXP */
2697 if (size & 2) { /* STXP / STLXP */
2698 if (rn == 31) {
2699 gen_check_sp_alignment(s);
2701 if (is_lasr) {
2702 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2704 clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn),
2705 true, rn != 31, size);
2706 gen_store_exclusive(s, rs, rt, rt2, clean_addr, size, true);
2707 return;
2709 if (rt2 == 31
2710 && ((rt | rs) & 1) == 0
2711 && dc_isar_feature(aa64_atomics, s)) {
2712 /* CASP / CASPL */
2713 gen_compare_and_swap_pair(s, rs, rt, rn, size | 2);
2714 return;
2716 break;
2718 case 0x6: case 0x7: /* CASPA / LDXP */
2719 if (size & 2) { /* LDXP / LDAXP */
2720 if (rn == 31) {
2721 gen_check_sp_alignment(s);
2723 clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn),
2724 false, rn != 31, size);
2725 s->is_ldex = true;
2726 gen_load_exclusive(s, rt, rt2, clean_addr, size, true);
2727 if (is_lasr) {
2728 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2730 return;
2732 if (rt2 == 31
2733 && ((rt | rs) & 1) == 0
2734 && dc_isar_feature(aa64_atomics, s)) {
2735 /* CASPA / CASPAL */
2736 gen_compare_and_swap_pair(s, rs, rt, rn, size | 2);
2737 return;
2739 break;
2741 case 0xa: /* CAS */
2742 case 0xb: /* CASL */
2743 case 0xe: /* CASA */
2744 case 0xf: /* CASAL */
2745 if (rt2 == 31 && dc_isar_feature(aa64_atomics, s)) {
2746 gen_compare_and_swap(s, rs, rt, rn, size);
2747 return;
2749 break;
2751 unallocated_encoding(s);
2755 * Load register (literal)
2757 * 31 30 29 27 26 25 24 23 5 4 0
2758 * +-----+-------+---+-----+-------------------+-------+
2759 * | opc | 0 1 1 | V | 0 0 | imm19 | Rt |
2760 * +-----+-------+---+-----+-------------------+-------+
2762 * V: 1 -> vector (simd/fp)
2763 * opc (non-vector): 00 -> 32 bit, 01 -> 64 bit,
2764 * 10-> 32 bit signed, 11 -> prefetch
2765 * opc (vector): 00 -> 32 bit, 01 -> 64 bit, 10 -> 128 bit (11 unallocated)
2767 static void disas_ld_lit(DisasContext *s, uint32_t insn)
2769 int rt = extract32(insn, 0, 5);
2770 int64_t imm = sextract32(insn, 5, 19) << 2;
2771 bool is_vector = extract32(insn, 26, 1);
2772 int opc = extract32(insn, 30, 2);
2773 bool is_signed = false;
2774 int size = 2;
2775 TCGv_i64 tcg_rt, clean_addr;
2777 if (is_vector) {
2778 if (opc == 3) {
2779 unallocated_encoding(s);
2780 return;
2782 size = 2 + opc;
2783 if (!fp_access_check(s)) {
2784 return;
2786 } else {
2787 if (opc == 3) {
2788 /* PRFM (literal) : prefetch */
2789 return;
2791 size = 2 + extract32(opc, 0, 1);
2792 is_signed = extract32(opc, 1, 1);
2795 tcg_rt = cpu_reg(s, rt);
2797 clean_addr = tcg_const_i64(s->pc_curr + imm);
2798 if (is_vector) {
2799 do_fp_ld(s, rt, clean_addr, size);
2800 } else {
2801 /* Only unsigned 32bit loads target 32bit registers. */
2802 bool iss_sf = opc != 0;
2804 do_gpr_ld(s, tcg_rt, clean_addr, size, is_signed, false,
2805 true, rt, iss_sf, false);
2807 tcg_temp_free_i64(clean_addr);
2811 * LDNP (Load Pair - non-temporal hint)
2812 * LDP (Load Pair - non vector)
2813 * LDPSW (Load Pair Signed Word - non vector)
2814 * STNP (Store Pair - non-temporal hint)
2815 * STP (Store Pair - non vector)
2816 * LDNP (Load Pair of SIMD&FP - non-temporal hint)
2817 * LDP (Load Pair of SIMD&FP)
2818 * STNP (Store Pair of SIMD&FP - non-temporal hint)
2819 * STP (Store Pair of SIMD&FP)
2821 * 31 30 29 27 26 25 24 23 22 21 15 14 10 9 5 4 0
2822 * +-----+-------+---+---+-------+---+-----------------------------+
2823 * | opc | 1 0 1 | V | 0 | index | L | imm7 | Rt2 | Rn | Rt |
2824 * +-----+-------+---+---+-------+---+-------+-------+------+------+
2826 * opc: LDP/STP/LDNP/STNP 00 -> 32 bit, 10 -> 64 bit
2827 * LDPSW/STGP 01
2828 * LDP/STP/LDNP/STNP (SIMD) 00 -> 32 bit, 01 -> 64 bit, 10 -> 128 bit
2829 * V: 0 -> GPR, 1 -> Vector
2830 * idx: 00 -> signed offset with non-temporal hint, 01 -> post-index,
2831 * 10 -> signed offset, 11 -> pre-index
2832 * L: 0 -> Store 1 -> Load
2834 * Rt, Rt2 = GPR or SIMD registers to be stored
2835 * Rn = general purpose register containing address
2836 * imm7 = signed offset (multiple of 4 or 8 depending on size)
2838 static void disas_ldst_pair(DisasContext *s, uint32_t insn)
2840 int rt = extract32(insn, 0, 5);
2841 int rn = extract32(insn, 5, 5);
2842 int rt2 = extract32(insn, 10, 5);
2843 uint64_t offset = sextract64(insn, 15, 7);
2844 int index = extract32(insn, 23, 2);
2845 bool is_vector = extract32(insn, 26, 1);
2846 bool is_load = extract32(insn, 22, 1);
2847 int opc = extract32(insn, 30, 2);
2849 bool is_signed = false;
2850 bool postindex = false;
2851 bool wback = false;
2852 bool set_tag = false;
2854 TCGv_i64 clean_addr, dirty_addr;
2856 int size;
2858 if (opc == 3) {
2859 unallocated_encoding(s);
2860 return;
2863 if (is_vector) {
2864 size = 2 + opc;
2865 } else if (opc == 1 && !is_load) {
2866 /* STGP */
2867 if (!dc_isar_feature(aa64_mte_insn_reg, s) || index == 0) {
2868 unallocated_encoding(s);
2869 return;
2871 size = 3;
2872 set_tag = true;
2873 } else {
2874 size = 2 + extract32(opc, 1, 1);
2875 is_signed = extract32(opc, 0, 1);
2876 if (!is_load && is_signed) {
2877 unallocated_encoding(s);
2878 return;
2882 switch (index) {
2883 case 1: /* post-index */
2884 postindex = true;
2885 wback = true;
2886 break;
2887 case 0:
2888 /* signed offset with "non-temporal" hint. Since we don't emulate
2889 * caches we don't care about hints to the cache system about
2890 * data access patterns, and handle this identically to plain
2891 * signed offset.
2893 if (is_signed) {
2894 /* There is no non-temporal-hint version of LDPSW */
2895 unallocated_encoding(s);
2896 return;
2898 postindex = false;
2899 break;
2900 case 2: /* signed offset, rn not updated */
2901 postindex = false;
2902 break;
2903 case 3: /* pre-index */
2904 postindex = false;
2905 wback = true;
2906 break;
2909 if (is_vector && !fp_access_check(s)) {
2910 return;
2913 offset <<= (set_tag ? LOG2_TAG_GRANULE : size);
2915 if (rn == 31) {
2916 gen_check_sp_alignment(s);
2919 dirty_addr = read_cpu_reg_sp(s, rn, 1);
2920 if (!postindex) {
2921 tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
2924 if (set_tag) {
2925 if (!s->ata) {
2927 * TODO: We could rely on the stores below, at least for
2928 * system mode, if we arrange to add MO_ALIGN_16.
2930 gen_helper_stg_stub(cpu_env, dirty_addr);
2931 } else if (tb_cflags(s->base.tb) & CF_PARALLEL) {
2932 gen_helper_stg_parallel(cpu_env, dirty_addr, dirty_addr);
2933 } else {
2934 gen_helper_stg(cpu_env, dirty_addr, dirty_addr);
2938 clean_addr = gen_mte_checkN(s, dirty_addr, !is_load,
2939 (wback || rn != 31) && !set_tag,
2940 size, 2 << size);
2942 if (is_vector) {
2943 if (is_load) {
2944 do_fp_ld(s, rt, clean_addr, size);
2945 } else {
2946 do_fp_st(s, rt, clean_addr, size);
2948 tcg_gen_addi_i64(clean_addr, clean_addr, 1 << size);
2949 if (is_load) {
2950 do_fp_ld(s, rt2, clean_addr, size);
2951 } else {
2952 do_fp_st(s, rt2, clean_addr, size);
2954 } else {
2955 TCGv_i64 tcg_rt = cpu_reg(s, rt);
2956 TCGv_i64 tcg_rt2 = cpu_reg(s, rt2);
2958 if (is_load) {
2959 TCGv_i64 tmp = tcg_temp_new_i64();
2961 /* Do not modify tcg_rt before recognizing any exception
2962 * from the second load.
2964 do_gpr_ld(s, tmp, clean_addr, size, is_signed, false,
2965 false, 0, false, false);
2966 tcg_gen_addi_i64(clean_addr, clean_addr, 1 << size);
2967 do_gpr_ld(s, tcg_rt2, clean_addr, size, is_signed, false,
2968 false, 0, false, false);
2970 tcg_gen_mov_i64(tcg_rt, tmp);
2971 tcg_temp_free_i64(tmp);
2972 } else {
2973 do_gpr_st(s, tcg_rt, clean_addr, size,
2974 false, 0, false, false);
2975 tcg_gen_addi_i64(clean_addr, clean_addr, 1 << size);
2976 do_gpr_st(s, tcg_rt2, clean_addr, size,
2977 false, 0, false, false);
2981 if (wback) {
2982 if (postindex) {
2983 tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
2985 tcg_gen_mov_i64(cpu_reg_sp(s, rn), dirty_addr);
2990 * Load/store (immediate post-indexed)
2991 * Load/store (immediate pre-indexed)
2992 * Load/store (unscaled immediate)
2994 * 31 30 29 27 26 25 24 23 22 21 20 12 11 10 9 5 4 0
2995 * +----+-------+---+-----+-----+---+--------+-----+------+------+
2996 * |size| 1 1 1 | V | 0 0 | opc | 0 | imm9 | idx | Rn | Rt |
2997 * +----+-------+---+-----+-----+---+--------+-----+------+------+
2999 * idx = 01 -> post-indexed, 11 pre-indexed, 00 unscaled imm. (no writeback)
3000 10 -> unprivileged
3001 * V = 0 -> non-vector
3002 * size: 00 -> 8 bit, 01 -> 16 bit, 10 -> 32 bit, 11 -> 64bit
3003 * opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
3005 static void disas_ldst_reg_imm9(DisasContext *s, uint32_t insn,
3006 int opc,
3007 int size,
3008 int rt,
3009 bool is_vector)
3011 int rn = extract32(insn, 5, 5);
3012 int imm9 = sextract32(insn, 12, 9);
3013 int idx = extract32(insn, 10, 2);
3014 bool is_signed = false;
3015 bool is_store = false;
3016 bool is_extended = false;
3017 bool is_unpriv = (idx == 2);
3018 bool iss_valid = !is_vector;
3019 bool post_index;
3020 bool writeback;
3021 int memidx;
3023 TCGv_i64 clean_addr, dirty_addr;
3025 if (is_vector) {
3026 size |= (opc & 2) << 1;
3027 if (size > 4 || is_unpriv) {
3028 unallocated_encoding(s);
3029 return;
3031 is_store = ((opc & 1) == 0);
3032 if (!fp_access_check(s)) {
3033 return;
3035 } else {
3036 if (size == 3 && opc == 2) {
3037 /* PRFM - prefetch */
3038 if (idx != 0) {
3039 unallocated_encoding(s);
3040 return;
3042 return;
3044 if (opc == 3 && size > 1) {
3045 unallocated_encoding(s);
3046 return;
3048 is_store = (opc == 0);
3049 is_signed = extract32(opc, 1, 1);
3050 is_extended = (size < 3) && extract32(opc, 0, 1);
3053 switch (idx) {
3054 case 0:
3055 case 2:
3056 post_index = false;
3057 writeback = false;
3058 break;
3059 case 1:
3060 post_index = true;
3061 writeback = true;
3062 break;
3063 case 3:
3064 post_index = false;
3065 writeback = true;
3066 break;
3067 default:
3068 g_assert_not_reached();
3071 if (rn == 31) {
3072 gen_check_sp_alignment(s);
3075 dirty_addr = read_cpu_reg_sp(s, rn, 1);
3076 if (!post_index) {
3077 tcg_gen_addi_i64(dirty_addr, dirty_addr, imm9);
3080 memidx = is_unpriv ? get_a64_user_mem_index(s) : get_mem_index(s);
3081 clean_addr = gen_mte_check1_mmuidx(s, dirty_addr, is_store,
3082 writeback || rn != 31,
3083 size, is_unpriv, memidx);
3085 if (is_vector) {
3086 if (is_store) {
3087 do_fp_st(s, rt, clean_addr, size);
3088 } else {
3089 do_fp_ld(s, rt, clean_addr, size);
3091 } else {
3092 TCGv_i64 tcg_rt = cpu_reg(s, rt);
3093 bool iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
3095 if (is_store) {
3096 do_gpr_st_memidx(s, tcg_rt, clean_addr, size, memidx,
3097 iss_valid, rt, iss_sf, false);
3098 } else {
3099 do_gpr_ld_memidx(s, tcg_rt, clean_addr, size,
3100 is_signed, is_extended, memidx,
3101 iss_valid, rt, iss_sf, false);
3105 if (writeback) {
3106 TCGv_i64 tcg_rn = cpu_reg_sp(s, rn);
3107 if (post_index) {
3108 tcg_gen_addi_i64(dirty_addr, dirty_addr, imm9);
3110 tcg_gen_mov_i64(tcg_rn, dirty_addr);
3115 * Load/store (register offset)
3117 * 31 30 29 27 26 25 24 23 22 21 20 16 15 13 12 11 10 9 5 4 0
3118 * +----+-------+---+-----+-----+---+------+-----+--+-----+----+----+
3119 * |size| 1 1 1 | V | 0 0 | opc | 1 | Rm | opt | S| 1 0 | Rn | Rt |
3120 * +----+-------+---+-----+-----+---+------+-----+--+-----+----+----+
3122 * For non-vector:
3123 * size: 00-> byte, 01 -> 16 bit, 10 -> 32bit, 11 -> 64bit
3124 * opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
3125 * For vector:
3126 * size is opc<1>:size<1:0> so 100 -> 128 bit; 110 and 111 unallocated
3127 * opc<0>: 0 -> store, 1 -> load
3128 * V: 1 -> vector/simd
3129 * opt: extend encoding (see DecodeRegExtend)
3130 * S: if S=1 then scale (essentially index by sizeof(size))
3131 * Rt: register to transfer into/out of
3132 * Rn: address register or SP for base
3133 * Rm: offset register or ZR for offset
3135 static void disas_ldst_reg_roffset(DisasContext *s, uint32_t insn,
3136 int opc,
3137 int size,
3138 int rt,
3139 bool is_vector)
3141 int rn = extract32(insn, 5, 5);
3142 int shift = extract32(insn, 12, 1);
3143 int rm = extract32(insn, 16, 5);
3144 int opt = extract32(insn, 13, 3);
3145 bool is_signed = false;
3146 bool is_store = false;
3147 bool is_extended = false;
3149 TCGv_i64 tcg_rm, clean_addr, dirty_addr;
3151 if (extract32(opt, 1, 1) == 0) {
3152 unallocated_encoding(s);
3153 return;
3156 if (is_vector) {
3157 size |= (opc & 2) << 1;
3158 if (size > 4) {
3159 unallocated_encoding(s);
3160 return;
3162 is_store = !extract32(opc, 0, 1);
3163 if (!fp_access_check(s)) {
3164 return;
3166 } else {
3167 if (size == 3 && opc == 2) {
3168 /* PRFM - prefetch */
3169 return;
3171 if (opc == 3 && size > 1) {
3172 unallocated_encoding(s);
3173 return;
3175 is_store = (opc == 0);
3176 is_signed = extract32(opc, 1, 1);
3177 is_extended = (size < 3) && extract32(opc, 0, 1);
3180 if (rn == 31) {
3181 gen_check_sp_alignment(s);
3183 dirty_addr = read_cpu_reg_sp(s, rn, 1);
3185 tcg_rm = read_cpu_reg(s, rm, 1);
3186 ext_and_shift_reg(tcg_rm, tcg_rm, opt, shift ? size : 0);
3188 tcg_gen_add_i64(dirty_addr, dirty_addr, tcg_rm);
3189 clean_addr = gen_mte_check1(s, dirty_addr, is_store, true, size);
3191 if (is_vector) {
3192 if (is_store) {
3193 do_fp_st(s, rt, clean_addr, size);
3194 } else {
3195 do_fp_ld(s, rt, clean_addr, size);
3197 } else {
3198 TCGv_i64 tcg_rt = cpu_reg(s, rt);
3199 bool iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
3200 if (is_store) {
3201 do_gpr_st(s, tcg_rt, clean_addr, size,
3202 true, rt, iss_sf, false);
3203 } else {
3204 do_gpr_ld(s, tcg_rt, clean_addr, size,
3205 is_signed, is_extended,
3206 true, rt, iss_sf, false);
3212 * Load/store (unsigned immediate)
3214 * 31 30 29 27 26 25 24 23 22 21 10 9 5
3215 * +----+-------+---+-----+-----+------------+-------+------+
3216 * |size| 1 1 1 | V | 0 1 | opc | imm12 | Rn | Rt |
3217 * +----+-------+---+-----+-----+------------+-------+------+
3219 * For non-vector:
3220 * size: 00-> byte, 01 -> 16 bit, 10 -> 32bit, 11 -> 64bit
3221 * opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
3222 * For vector:
3223 * size is opc<1>:size<1:0> so 100 -> 128 bit; 110 and 111 unallocated
3224 * opc<0>: 0 -> store, 1 -> load
3225 * Rn: base address register (inc SP)
3226 * Rt: target register
3228 static void disas_ldst_reg_unsigned_imm(DisasContext *s, uint32_t insn,
3229 int opc,
3230 int size,
3231 int rt,
3232 bool is_vector)
3234 int rn = extract32(insn, 5, 5);
3235 unsigned int imm12 = extract32(insn, 10, 12);
3236 unsigned int offset;
3238 TCGv_i64 clean_addr, dirty_addr;
3240 bool is_store;
3241 bool is_signed = false;
3242 bool is_extended = false;
3244 if (is_vector) {
3245 size |= (opc & 2) << 1;
3246 if (size > 4) {
3247 unallocated_encoding(s);
3248 return;
3250 is_store = !extract32(opc, 0, 1);
3251 if (!fp_access_check(s)) {
3252 return;
3254 } else {
3255 if (size == 3 && opc == 2) {
3256 /* PRFM - prefetch */
3257 return;
3259 if (opc == 3 && size > 1) {
3260 unallocated_encoding(s);
3261 return;
3263 is_store = (opc == 0);
3264 is_signed = extract32(opc, 1, 1);
3265 is_extended = (size < 3) && extract32(opc, 0, 1);
3268 if (rn == 31) {
3269 gen_check_sp_alignment(s);
3271 dirty_addr = read_cpu_reg_sp(s, rn, 1);
3272 offset = imm12 << size;
3273 tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3274 clean_addr = gen_mte_check1(s, dirty_addr, is_store, rn != 31, size);
3276 if (is_vector) {
3277 if (is_store) {
3278 do_fp_st(s, rt, clean_addr, size);
3279 } else {
3280 do_fp_ld(s, rt, clean_addr, size);
3282 } else {
3283 TCGv_i64 tcg_rt = cpu_reg(s, rt);
3284 bool iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
3285 if (is_store) {
3286 do_gpr_st(s, tcg_rt, clean_addr, size,
3287 true, rt, iss_sf, false);
3288 } else {
3289 do_gpr_ld(s, tcg_rt, clean_addr, size, is_signed, is_extended,
3290 true, rt, iss_sf, false);
3295 /* Atomic memory operations
3297 * 31 30 27 26 24 22 21 16 15 12 10 5 0
3298 * +------+-------+---+-----+-----+---+----+----+-----+-----+----+-----+
3299 * | size | 1 1 1 | V | 0 0 | A R | 1 | Rs | o3 | opc | 0 0 | Rn | Rt |
3300 * +------+-------+---+-----+-----+--------+----+-----+-----+----+-----+
3302 * Rt: the result register
3303 * Rn: base address or SP
3304 * Rs: the source register for the operation
3305 * V: vector flag (always 0 as of v8.3)
3306 * A: acquire flag
3307 * R: release flag
3309 static void disas_ldst_atomic(DisasContext *s, uint32_t insn,
3310 int size, int rt, bool is_vector)
3312 int rs = extract32(insn, 16, 5);
3313 int rn = extract32(insn, 5, 5);
3314 int o3_opc = extract32(insn, 12, 4);
3315 bool r = extract32(insn, 22, 1);
3316 bool a = extract32(insn, 23, 1);
3317 TCGv_i64 tcg_rs, clean_addr;
3318 AtomicThreeOpFn *fn;
3320 if (is_vector || !dc_isar_feature(aa64_atomics, s)) {
3321 unallocated_encoding(s);
3322 return;
3324 switch (o3_opc) {
3325 case 000: /* LDADD */
3326 fn = tcg_gen_atomic_fetch_add_i64;
3327 break;
3328 case 001: /* LDCLR */
3329 fn = tcg_gen_atomic_fetch_and_i64;
3330 break;
3331 case 002: /* LDEOR */
3332 fn = tcg_gen_atomic_fetch_xor_i64;
3333 break;
3334 case 003: /* LDSET */
3335 fn = tcg_gen_atomic_fetch_or_i64;
3336 break;
3337 case 004: /* LDSMAX */
3338 fn = tcg_gen_atomic_fetch_smax_i64;
3339 break;
3340 case 005: /* LDSMIN */
3341 fn = tcg_gen_atomic_fetch_smin_i64;
3342 break;
3343 case 006: /* LDUMAX */
3344 fn = tcg_gen_atomic_fetch_umax_i64;
3345 break;
3346 case 007: /* LDUMIN */
3347 fn = tcg_gen_atomic_fetch_umin_i64;
3348 break;
3349 case 010: /* SWP */
3350 fn = tcg_gen_atomic_xchg_i64;
3351 break;
3352 case 014: /* LDAPR, LDAPRH, LDAPRB */
3353 if (!dc_isar_feature(aa64_rcpc_8_3, s) ||
3354 rs != 31 || a != 1 || r != 0) {
3355 unallocated_encoding(s);
3356 return;
3358 break;
3359 default:
3360 unallocated_encoding(s);
3361 return;
3364 if (rn == 31) {
3365 gen_check_sp_alignment(s);
3367 clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn), false, rn != 31, size);
3369 if (o3_opc == 014) {
3371 * LDAPR* are a special case because they are a simple load, not a
3372 * fetch-and-do-something op.
3373 * The architectural consistency requirements here are weaker than
3374 * full load-acquire (we only need "load-acquire processor consistent"),
3375 * but we choose to implement them as full LDAQ.
3377 do_gpr_ld(s, cpu_reg(s, rt), clean_addr, size, false, false,
3378 true, rt, disas_ldst_compute_iss_sf(size, false, 0), true);
3379 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
3380 return;
3383 tcg_rs = read_cpu_reg(s, rs, true);
3385 if (o3_opc == 1) { /* LDCLR */
3386 tcg_gen_not_i64(tcg_rs, tcg_rs);
3389 /* The tcg atomic primitives are all full barriers. Therefore we
3390 * can ignore the Acquire and Release bits of this instruction.
3392 fn(cpu_reg(s, rt), clean_addr, tcg_rs, get_mem_index(s),
3393 s->be_data | size | MO_ALIGN);
3397 * PAC memory operations
3399 * 31 30 27 26 24 22 21 12 11 10 5 0
3400 * +------+-------+---+-----+-----+---+--------+---+---+----+-----+
3401 * | size | 1 1 1 | V | 0 0 | M S | 1 | imm9 | W | 1 | Rn | Rt |
3402 * +------+-------+---+-----+-----+---+--------+---+---+----+-----+
3404 * Rt: the result register
3405 * Rn: base address or SP
3406 * V: vector flag (always 0 as of v8.3)
3407 * M: clear for key DA, set for key DB
3408 * W: pre-indexing flag
3409 * S: sign for imm9.
3411 static void disas_ldst_pac(DisasContext *s, uint32_t insn,
3412 int size, int rt, bool is_vector)
3414 int rn = extract32(insn, 5, 5);
3415 bool is_wback = extract32(insn, 11, 1);
3416 bool use_key_a = !extract32(insn, 23, 1);
3417 int offset;
3418 TCGv_i64 clean_addr, dirty_addr, tcg_rt;
3420 if (size != 3 || is_vector || !dc_isar_feature(aa64_pauth, s)) {
3421 unallocated_encoding(s);
3422 return;
3425 if (rn == 31) {
3426 gen_check_sp_alignment(s);
3428 dirty_addr = read_cpu_reg_sp(s, rn, 1);
3430 if (s->pauth_active) {
3431 if (use_key_a) {
3432 gen_helper_autda(dirty_addr, cpu_env, dirty_addr, cpu_X[31]);
3433 } else {
3434 gen_helper_autdb(dirty_addr, cpu_env, dirty_addr, cpu_X[31]);
3438 /* Form the 10-bit signed, scaled offset. */
3439 offset = (extract32(insn, 22, 1) << 9) | extract32(insn, 12, 9);
3440 offset = sextract32(offset << size, 0, 10 + size);
3441 tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3443 /* Note that "clean" and "dirty" here refer to TBI not PAC. */
3444 clean_addr = gen_mte_check1(s, dirty_addr, false,
3445 is_wback || rn != 31, size);
3447 tcg_rt = cpu_reg(s, rt);
3448 do_gpr_ld(s, tcg_rt, clean_addr, size, /* is_signed */ false,
3449 /* extend */ false, /* iss_valid */ !is_wback,
3450 /* iss_srt */ rt, /* iss_sf */ true, /* iss_ar */ false);
3452 if (is_wback) {
3453 tcg_gen_mov_i64(cpu_reg_sp(s, rn), dirty_addr);
3458 * LDAPR/STLR (unscaled immediate)
3460 * 31 30 24 22 21 12 10 5 0
3461 * +------+-------------+-----+---+--------+-----+----+-----+
3462 * | size | 0 1 1 0 0 1 | opc | 0 | imm9 | 0 0 | Rn | Rt |
3463 * +------+-------------+-----+---+--------+-----+----+-----+
3465 * Rt: source or destination register
3466 * Rn: base register
3467 * imm9: unscaled immediate offset
3468 * opc: 00: STLUR*, 01/10/11: various LDAPUR*
3469 * size: size of load/store
3471 static void disas_ldst_ldapr_stlr(DisasContext *s, uint32_t insn)
3473 int rt = extract32(insn, 0, 5);
3474 int rn = extract32(insn, 5, 5);
3475 int offset = sextract32(insn, 12, 9);
3476 int opc = extract32(insn, 22, 2);
3477 int size = extract32(insn, 30, 2);
3478 TCGv_i64 clean_addr, dirty_addr;
3479 bool is_store = false;
3480 bool is_signed = false;
3481 bool extend = false;
3482 bool iss_sf;
3484 if (!dc_isar_feature(aa64_rcpc_8_4, s)) {
3485 unallocated_encoding(s);
3486 return;
3489 switch (opc) {
3490 case 0: /* STLURB */
3491 is_store = true;
3492 break;
3493 case 1: /* LDAPUR* */
3494 break;
3495 case 2: /* LDAPURS* 64-bit variant */
3496 if (size == 3) {
3497 unallocated_encoding(s);
3498 return;
3500 is_signed = true;
3501 break;
3502 case 3: /* LDAPURS* 32-bit variant */
3503 if (size > 1) {
3504 unallocated_encoding(s);
3505 return;
3507 is_signed = true;
3508 extend = true; /* zero-extend 32->64 after signed load */
3509 break;
3510 default:
3511 g_assert_not_reached();
3514 iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
3516 if (rn == 31) {
3517 gen_check_sp_alignment(s);
3520 dirty_addr = read_cpu_reg_sp(s, rn, 1);
3521 tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3522 clean_addr = clean_data_tbi(s, dirty_addr);
3524 if (is_store) {
3525 /* Store-Release semantics */
3526 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
3527 do_gpr_st(s, cpu_reg(s, rt), clean_addr, size, true, rt, iss_sf, true);
3528 } else {
3530 * Load-AcquirePC semantics; we implement as the slightly more
3531 * restrictive Load-Acquire.
3533 do_gpr_ld(s, cpu_reg(s, rt), clean_addr, size, is_signed, extend,
3534 true, rt, iss_sf, true);
3535 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
3539 /* Load/store register (all forms) */
3540 static void disas_ldst_reg(DisasContext *s, uint32_t insn)
3542 int rt = extract32(insn, 0, 5);
3543 int opc = extract32(insn, 22, 2);
3544 bool is_vector = extract32(insn, 26, 1);
3545 int size = extract32(insn, 30, 2);
3547 switch (extract32(insn, 24, 2)) {
3548 case 0:
3549 if (extract32(insn, 21, 1) == 0) {
3550 /* Load/store register (unscaled immediate)
3551 * Load/store immediate pre/post-indexed
3552 * Load/store register unprivileged
3554 disas_ldst_reg_imm9(s, insn, opc, size, rt, is_vector);
3555 return;
3557 switch (extract32(insn, 10, 2)) {
3558 case 0:
3559 disas_ldst_atomic(s, insn, size, rt, is_vector);
3560 return;
3561 case 2:
3562 disas_ldst_reg_roffset(s, insn, opc, size, rt, is_vector);
3563 return;
3564 default:
3565 disas_ldst_pac(s, insn, size, rt, is_vector);
3566 return;
3568 break;
3569 case 1:
3570 disas_ldst_reg_unsigned_imm(s, insn, opc, size, rt, is_vector);
3571 return;
3573 unallocated_encoding(s);
3576 /* AdvSIMD load/store multiple structures
3578 * 31 30 29 23 22 21 16 15 12 11 10 9 5 4 0
3579 * +---+---+---------------+---+-------------+--------+------+------+------+
3580 * | 0 | Q | 0 0 1 1 0 0 0 | L | 0 0 0 0 0 0 | opcode | size | Rn | Rt |
3581 * +---+---+---------------+---+-------------+--------+------+------+------+
3583 * AdvSIMD load/store multiple structures (post-indexed)
3585 * 31 30 29 23 22 21 20 16 15 12 11 10 9 5 4 0
3586 * +---+---+---------------+---+---+---------+--------+------+------+------+
3587 * | 0 | Q | 0 0 1 1 0 0 1 | L | 0 | Rm | opcode | size | Rn | Rt |
3588 * +---+---+---------------+---+---+---------+--------+------+------+------+
3590 * Rt: first (or only) SIMD&FP register to be transferred
3591 * Rn: base address or SP
3592 * Rm (post-index only): post-index register (when !31) or size dependent #imm
3594 static void disas_ldst_multiple_struct(DisasContext *s, uint32_t insn)
3596 int rt = extract32(insn, 0, 5);
3597 int rn = extract32(insn, 5, 5);
3598 int rm = extract32(insn, 16, 5);
3599 int size = extract32(insn, 10, 2);
3600 int opcode = extract32(insn, 12, 4);
3601 bool is_store = !extract32(insn, 22, 1);
3602 bool is_postidx = extract32(insn, 23, 1);
3603 bool is_q = extract32(insn, 30, 1);
3604 TCGv_i64 clean_addr, tcg_rn, tcg_ebytes;
3605 MemOp endian = s->be_data;
3607 int total; /* total bytes */
3608 int elements; /* elements per vector */
3609 int rpt; /* num iterations */
3610 int selem; /* structure elements */
3611 int r;
3613 if (extract32(insn, 31, 1) || extract32(insn, 21, 1)) {
3614 unallocated_encoding(s);
3615 return;
3618 if (!is_postidx && rm != 0) {
3619 unallocated_encoding(s);
3620 return;
3623 /* From the shared decode logic */
3624 switch (opcode) {
3625 case 0x0:
3626 rpt = 1;
3627 selem = 4;
3628 break;
3629 case 0x2:
3630 rpt = 4;
3631 selem = 1;
3632 break;
3633 case 0x4:
3634 rpt = 1;
3635 selem = 3;
3636 break;
3637 case 0x6:
3638 rpt = 3;
3639 selem = 1;
3640 break;
3641 case 0x7:
3642 rpt = 1;
3643 selem = 1;
3644 break;
3645 case 0x8:
3646 rpt = 1;
3647 selem = 2;
3648 break;
3649 case 0xa:
3650 rpt = 2;
3651 selem = 1;
3652 break;
3653 default:
3654 unallocated_encoding(s);
3655 return;
3658 if (size == 3 && !is_q && selem != 1) {
3659 /* reserved */
3660 unallocated_encoding(s);
3661 return;
3664 if (!fp_access_check(s)) {
3665 return;
3668 if (rn == 31) {
3669 gen_check_sp_alignment(s);
3672 /* For our purposes, bytes are always little-endian. */
3673 if (size == 0) {
3674 endian = MO_LE;
3677 total = rpt * selem * (is_q ? 16 : 8);
3678 tcg_rn = cpu_reg_sp(s, rn);
3681 * Issue the MTE check vs the logical repeat count, before we
3682 * promote consecutive little-endian elements below.
3684 clean_addr = gen_mte_checkN(s, tcg_rn, is_store, is_postidx || rn != 31,
3685 size, total);
3688 * Consecutive little-endian elements from a single register
3689 * can be promoted to a larger little-endian operation.
3691 if (selem == 1 && endian == MO_LE) {
3692 size = 3;
3694 elements = (is_q ? 16 : 8) >> size;
3696 tcg_ebytes = tcg_const_i64(1 << size);
3697 for (r = 0; r < rpt; r++) {
3698 int e;
3699 for (e = 0; e < elements; e++) {
3700 int xs;
3701 for (xs = 0; xs < selem; xs++) {
3702 int tt = (rt + r + xs) % 32;
3703 if (is_store) {
3704 do_vec_st(s, tt, e, clean_addr, size, endian);
3705 } else {
3706 do_vec_ld(s, tt, e, clean_addr, size, endian);
3708 tcg_gen_add_i64(clean_addr, clean_addr, tcg_ebytes);
3712 tcg_temp_free_i64(tcg_ebytes);
3714 if (!is_store) {
3715 /* For non-quad operations, setting a slice of the low
3716 * 64 bits of the register clears the high 64 bits (in
3717 * the ARM ARM pseudocode this is implicit in the fact
3718 * that 'rval' is a 64 bit wide variable).
3719 * For quad operations, we might still need to zero the
3720 * high bits of SVE.
3722 for (r = 0; r < rpt * selem; r++) {
3723 int tt = (rt + r) % 32;
3724 clear_vec_high(s, is_q, tt);
3728 if (is_postidx) {
3729 if (rm == 31) {
3730 tcg_gen_addi_i64(tcg_rn, tcg_rn, total);
3731 } else {
3732 tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, rm));
3737 /* AdvSIMD load/store single structure
3739 * 31 30 29 23 22 21 20 16 15 13 12 11 10 9 5 4 0
3740 * +---+---+---------------+-----+-----------+-----+---+------+------+------+
3741 * | 0 | Q | 0 0 1 1 0 1 0 | L R | 0 0 0 0 0 | opc | S | size | Rn | Rt |
3742 * +---+---+---------------+-----+-----------+-----+---+------+------+------+
3744 * AdvSIMD load/store single structure (post-indexed)
3746 * 31 30 29 23 22 21 20 16 15 13 12 11 10 9 5 4 0
3747 * +---+---+---------------+-----+-----------+-----+---+------+------+------+
3748 * | 0 | Q | 0 0 1 1 0 1 1 | L R | Rm | opc | S | size | Rn | Rt |
3749 * +---+---+---------------+-----+-----------+-----+---+------+------+------+
3751 * Rt: first (or only) SIMD&FP register to be transferred
3752 * Rn: base address or SP
3753 * Rm (post-index only): post-index register (when !31) or size dependent #imm
3754 * index = encoded in Q:S:size dependent on size
3756 * lane_size = encoded in R, opc
3757 * transfer width = encoded in opc, S, size
3759 static void disas_ldst_single_struct(DisasContext *s, uint32_t insn)
3761 int rt = extract32(insn, 0, 5);
3762 int rn = extract32(insn, 5, 5);
3763 int rm = extract32(insn, 16, 5);
3764 int size = extract32(insn, 10, 2);
3765 int S = extract32(insn, 12, 1);
3766 int opc = extract32(insn, 13, 3);
3767 int R = extract32(insn, 21, 1);
3768 int is_load = extract32(insn, 22, 1);
3769 int is_postidx = extract32(insn, 23, 1);
3770 int is_q = extract32(insn, 30, 1);
3772 int scale = extract32(opc, 1, 2);
3773 int selem = (extract32(opc, 0, 1) << 1 | R) + 1;
3774 bool replicate = false;
3775 int index = is_q << 3 | S << 2 | size;
3776 int xs, total;
3777 TCGv_i64 clean_addr, tcg_rn, tcg_ebytes;
3779 if (extract32(insn, 31, 1)) {
3780 unallocated_encoding(s);
3781 return;
3783 if (!is_postidx && rm != 0) {
3784 unallocated_encoding(s);
3785 return;
3788 switch (scale) {
3789 case 3:
3790 if (!is_load || S) {
3791 unallocated_encoding(s);
3792 return;
3794 scale = size;
3795 replicate = true;
3796 break;
3797 case 0:
3798 break;
3799 case 1:
3800 if (extract32(size, 0, 1)) {
3801 unallocated_encoding(s);
3802 return;
3804 index >>= 1;
3805 break;
3806 case 2:
3807 if (extract32(size, 1, 1)) {
3808 unallocated_encoding(s);
3809 return;
3811 if (!extract32(size, 0, 1)) {
3812 index >>= 2;
3813 } else {
3814 if (S) {
3815 unallocated_encoding(s);
3816 return;
3818 index >>= 3;
3819 scale = 3;
3821 break;
3822 default:
3823 g_assert_not_reached();
3826 if (!fp_access_check(s)) {
3827 return;
3830 if (rn == 31) {
3831 gen_check_sp_alignment(s);
3834 total = selem << scale;
3835 tcg_rn = cpu_reg_sp(s, rn);
3837 clean_addr = gen_mte_checkN(s, tcg_rn, !is_load, is_postidx || rn != 31,
3838 scale, total);
3840 tcg_ebytes = tcg_const_i64(1 << scale);
3841 for (xs = 0; xs < selem; xs++) {
3842 if (replicate) {
3843 /* Load and replicate to all elements */
3844 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
3846 tcg_gen_qemu_ld_i64(tcg_tmp, clean_addr,
3847 get_mem_index(s), s->be_data + scale);
3848 tcg_gen_gvec_dup_i64(scale, vec_full_reg_offset(s, rt),
3849 (is_q + 1) * 8, vec_full_reg_size(s),
3850 tcg_tmp);
3851 tcg_temp_free_i64(tcg_tmp);
3852 } else {
3853 /* Load/store one element per register */
3854 if (is_load) {
3855 do_vec_ld(s, rt, index, clean_addr, scale, s->be_data);
3856 } else {
3857 do_vec_st(s, rt, index, clean_addr, scale, s->be_data);
3860 tcg_gen_add_i64(clean_addr, clean_addr, tcg_ebytes);
3861 rt = (rt + 1) % 32;
3863 tcg_temp_free_i64(tcg_ebytes);
3865 if (is_postidx) {
3866 if (rm == 31) {
3867 tcg_gen_addi_i64(tcg_rn, tcg_rn, total);
3868 } else {
3869 tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, rm));
3875 * Load/Store memory tags
3877 * 31 30 29 24 22 21 12 10 5 0
3878 * +-----+-------------+-----+---+------+-----+------+------+
3879 * | 1 1 | 0 1 1 0 0 1 | op1 | 1 | imm9 | op2 | Rn | Rt |
3880 * +-----+-------------+-----+---+------+-----+------+------+
3882 static void disas_ldst_tag(DisasContext *s, uint32_t insn)
3884 int rt = extract32(insn, 0, 5);
3885 int rn = extract32(insn, 5, 5);
3886 uint64_t offset = sextract64(insn, 12, 9) << LOG2_TAG_GRANULE;
3887 int op2 = extract32(insn, 10, 2);
3888 int op1 = extract32(insn, 22, 2);
3889 bool is_load = false, is_pair = false, is_zero = false, is_mult = false;
3890 int index = 0;
3891 TCGv_i64 addr, clean_addr, tcg_rt;
3893 /* We checked insn bits [29:24,21] in the caller. */
3894 if (extract32(insn, 30, 2) != 3) {
3895 goto do_unallocated;
3899 * @index is a tri-state variable which has 3 states:
3900 * < 0 : post-index, writeback
3901 * = 0 : signed offset
3902 * > 0 : pre-index, writeback
3904 switch (op1) {
3905 case 0:
3906 if (op2 != 0) {
3907 /* STG */
3908 index = op2 - 2;
3909 } else {
3910 /* STZGM */
3911 if (s->current_el == 0 || offset != 0) {
3912 goto do_unallocated;
3914 is_mult = is_zero = true;
3916 break;
3917 case 1:
3918 if (op2 != 0) {
3919 /* STZG */
3920 is_zero = true;
3921 index = op2 - 2;
3922 } else {
3923 /* LDG */
3924 is_load = true;
3926 break;
3927 case 2:
3928 if (op2 != 0) {
3929 /* ST2G */
3930 is_pair = true;
3931 index = op2 - 2;
3932 } else {
3933 /* STGM */
3934 if (s->current_el == 0 || offset != 0) {
3935 goto do_unallocated;
3937 is_mult = true;
3939 break;
3940 case 3:
3941 if (op2 != 0) {
3942 /* STZ2G */
3943 is_pair = is_zero = true;
3944 index = op2 - 2;
3945 } else {
3946 /* LDGM */
3947 if (s->current_el == 0 || offset != 0) {
3948 goto do_unallocated;
3950 is_mult = is_load = true;
3952 break;
3954 default:
3955 do_unallocated:
3956 unallocated_encoding(s);
3957 return;
3960 if (is_mult
3961 ? !dc_isar_feature(aa64_mte, s)
3962 : !dc_isar_feature(aa64_mte_insn_reg, s)) {
3963 goto do_unallocated;
3966 if (rn == 31) {
3967 gen_check_sp_alignment(s);
3970 addr = read_cpu_reg_sp(s, rn, true);
3971 if (index >= 0) {
3972 /* pre-index or signed offset */
3973 tcg_gen_addi_i64(addr, addr, offset);
3976 if (is_mult) {
3977 tcg_rt = cpu_reg(s, rt);
3979 if (is_zero) {
3980 int size = 4 << s->dcz_blocksize;
3982 if (s->ata) {
3983 gen_helper_stzgm_tags(cpu_env, addr, tcg_rt);
3986 * The non-tags portion of STZGM is mostly like DC_ZVA,
3987 * except the alignment happens before the access.
3989 clean_addr = clean_data_tbi(s, addr);
3990 tcg_gen_andi_i64(clean_addr, clean_addr, -size);
3991 gen_helper_dc_zva(cpu_env, clean_addr);
3992 } else if (s->ata) {
3993 if (is_load) {
3994 gen_helper_ldgm(tcg_rt, cpu_env, addr);
3995 } else {
3996 gen_helper_stgm(cpu_env, addr, tcg_rt);
3998 } else {
3999 MMUAccessType acc = is_load ? MMU_DATA_LOAD : MMU_DATA_STORE;
4000 int size = 4 << GMID_EL1_BS;
4002 clean_addr = clean_data_tbi(s, addr);
4003 tcg_gen_andi_i64(clean_addr, clean_addr, -size);
4004 gen_probe_access(s, clean_addr, acc, size);
4006 if (is_load) {
4007 /* The result tags are zeros. */
4008 tcg_gen_movi_i64(tcg_rt, 0);
4011 return;
4014 if (is_load) {
4015 tcg_gen_andi_i64(addr, addr, -TAG_GRANULE);
4016 tcg_rt = cpu_reg(s, rt);
4017 if (s->ata) {
4018 gen_helper_ldg(tcg_rt, cpu_env, addr, tcg_rt);
4019 } else {
4020 clean_addr = clean_data_tbi(s, addr);
4021 gen_probe_access(s, clean_addr, MMU_DATA_LOAD, MO_8);
4022 gen_address_with_allocation_tag0(tcg_rt, addr);
4024 } else {
4025 tcg_rt = cpu_reg_sp(s, rt);
4026 if (!s->ata) {
4028 * For STG and ST2G, we need to check alignment and probe memory.
4029 * TODO: For STZG and STZ2G, we could rely on the stores below,
4030 * at least for system mode; user-only won't enforce alignment.
4032 if (is_pair) {
4033 gen_helper_st2g_stub(cpu_env, addr);
4034 } else {
4035 gen_helper_stg_stub(cpu_env, addr);
4037 } else if (tb_cflags(s->base.tb) & CF_PARALLEL) {
4038 if (is_pair) {
4039 gen_helper_st2g_parallel(cpu_env, addr, tcg_rt);
4040 } else {
4041 gen_helper_stg_parallel(cpu_env, addr, tcg_rt);
4043 } else {
4044 if (is_pair) {
4045 gen_helper_st2g(cpu_env, addr, tcg_rt);
4046 } else {
4047 gen_helper_stg(cpu_env, addr, tcg_rt);
4052 if (is_zero) {
4053 TCGv_i64 clean_addr = clean_data_tbi(s, addr);
4054 TCGv_i64 tcg_zero = tcg_const_i64(0);
4055 int mem_index = get_mem_index(s);
4056 int i, n = (1 + is_pair) << LOG2_TAG_GRANULE;
4058 tcg_gen_qemu_st_i64(tcg_zero, clean_addr, mem_index,
4059 MO_Q | MO_ALIGN_16);
4060 for (i = 8; i < n; i += 8) {
4061 tcg_gen_addi_i64(clean_addr, clean_addr, 8);
4062 tcg_gen_qemu_st_i64(tcg_zero, clean_addr, mem_index, MO_Q);
4064 tcg_temp_free_i64(tcg_zero);
4067 if (index != 0) {
4068 /* pre-index or post-index */
4069 if (index < 0) {
4070 /* post-index */
4071 tcg_gen_addi_i64(addr, addr, offset);
4073 tcg_gen_mov_i64(cpu_reg_sp(s, rn), addr);
4077 /* Loads and stores */
4078 static void disas_ldst(DisasContext *s, uint32_t insn)
4080 switch (extract32(insn, 24, 6)) {
4081 case 0x08: /* Load/store exclusive */
4082 disas_ldst_excl(s, insn);
4083 break;
4084 case 0x18: case 0x1c: /* Load register (literal) */
4085 disas_ld_lit(s, insn);
4086 break;
4087 case 0x28: case 0x29:
4088 case 0x2c: case 0x2d: /* Load/store pair (all forms) */
4089 disas_ldst_pair(s, insn);
4090 break;
4091 case 0x38: case 0x39:
4092 case 0x3c: case 0x3d: /* Load/store register (all forms) */
4093 disas_ldst_reg(s, insn);
4094 break;
4095 case 0x0c: /* AdvSIMD load/store multiple structures */
4096 disas_ldst_multiple_struct(s, insn);
4097 break;
4098 case 0x0d: /* AdvSIMD load/store single structure */
4099 disas_ldst_single_struct(s, insn);
4100 break;
4101 case 0x19:
4102 if (extract32(insn, 21, 1) != 0) {
4103 disas_ldst_tag(s, insn);
4104 } else if (extract32(insn, 10, 2) == 0) {
4105 disas_ldst_ldapr_stlr(s, insn);
4106 } else {
4107 unallocated_encoding(s);
4109 break;
4110 default:
4111 unallocated_encoding(s);
4112 break;
4116 /* PC-rel. addressing
4117 * 31 30 29 28 24 23 5 4 0
4118 * +----+-------+-----------+-------------------+------+
4119 * | op | immlo | 1 0 0 0 0 | immhi | Rd |
4120 * +----+-------+-----------+-------------------+------+
4122 static void disas_pc_rel_adr(DisasContext *s, uint32_t insn)
4124 unsigned int page, rd;
4125 uint64_t base;
4126 uint64_t offset;
4128 page = extract32(insn, 31, 1);
4129 /* SignExtend(immhi:immlo) -> offset */
4130 offset = sextract64(insn, 5, 19);
4131 offset = offset << 2 | extract32(insn, 29, 2);
4132 rd = extract32(insn, 0, 5);
4133 base = s->pc_curr;
4135 if (page) {
4136 /* ADRP (page based) */
4137 base &= ~0xfff;
4138 offset <<= 12;
4141 tcg_gen_movi_i64(cpu_reg(s, rd), base + offset);
4145 * Add/subtract (immediate)
4147 * 31 30 29 28 23 22 21 10 9 5 4 0
4148 * +--+--+--+-------------+--+-------------+-----+-----+
4149 * |sf|op| S| 1 0 0 0 1 0 |sh| imm12 | Rn | Rd |
4150 * +--+--+--+-------------+--+-------------+-----+-----+
4152 * sf: 0 -> 32bit, 1 -> 64bit
4153 * op: 0 -> add , 1 -> sub
4154 * S: 1 -> set flags
4155 * sh: 1 -> LSL imm by 12
4157 static void disas_add_sub_imm(DisasContext *s, uint32_t insn)
4159 int rd = extract32(insn, 0, 5);
4160 int rn = extract32(insn, 5, 5);
4161 uint64_t imm = extract32(insn, 10, 12);
4162 bool shift = extract32(insn, 22, 1);
4163 bool setflags = extract32(insn, 29, 1);
4164 bool sub_op = extract32(insn, 30, 1);
4165 bool is_64bit = extract32(insn, 31, 1);
4167 TCGv_i64 tcg_rn = cpu_reg_sp(s, rn);
4168 TCGv_i64 tcg_rd = setflags ? cpu_reg(s, rd) : cpu_reg_sp(s, rd);
4169 TCGv_i64 tcg_result;
4171 if (shift) {
4172 imm <<= 12;
4175 tcg_result = tcg_temp_new_i64();
4176 if (!setflags) {
4177 if (sub_op) {
4178 tcg_gen_subi_i64(tcg_result, tcg_rn, imm);
4179 } else {
4180 tcg_gen_addi_i64(tcg_result, tcg_rn, imm);
4182 } else {
4183 TCGv_i64 tcg_imm = tcg_const_i64(imm);
4184 if (sub_op) {
4185 gen_sub_CC(is_64bit, tcg_result, tcg_rn, tcg_imm);
4186 } else {
4187 gen_add_CC(is_64bit, tcg_result, tcg_rn, tcg_imm);
4189 tcg_temp_free_i64(tcg_imm);
4192 if (is_64bit) {
4193 tcg_gen_mov_i64(tcg_rd, tcg_result);
4194 } else {
4195 tcg_gen_ext32u_i64(tcg_rd, tcg_result);
4198 tcg_temp_free_i64(tcg_result);
4202 * Add/subtract (immediate, with tags)
4204 * 31 30 29 28 23 22 21 16 14 10 9 5 4 0
4205 * +--+--+--+-------------+--+---------+--+-------+-----+-----+
4206 * |sf|op| S| 1 0 0 0 1 1 |o2| uimm6 |o3| uimm4 | Rn | Rd |
4207 * +--+--+--+-------------+--+---------+--+-------+-----+-----+
4209 * op: 0 -> add, 1 -> sub
4211 static void disas_add_sub_imm_with_tags(DisasContext *s, uint32_t insn)
4213 int rd = extract32(insn, 0, 5);
4214 int rn = extract32(insn, 5, 5);
4215 int uimm4 = extract32(insn, 10, 4);
4216 int uimm6 = extract32(insn, 16, 6);
4217 bool sub_op = extract32(insn, 30, 1);
4218 TCGv_i64 tcg_rn, tcg_rd;
4219 int imm;
4221 /* Test all of sf=1, S=0, o2=0, o3=0. */
4222 if ((insn & 0xa040c000u) != 0x80000000u ||
4223 !dc_isar_feature(aa64_mte_insn_reg, s)) {
4224 unallocated_encoding(s);
4225 return;
4228 imm = uimm6 << LOG2_TAG_GRANULE;
4229 if (sub_op) {
4230 imm = -imm;
4233 tcg_rn = cpu_reg_sp(s, rn);
4234 tcg_rd = cpu_reg_sp(s, rd);
4236 if (s->ata) {
4237 TCGv_i32 offset = tcg_const_i32(imm);
4238 TCGv_i32 tag_offset = tcg_const_i32(uimm4);
4240 gen_helper_addsubg(tcg_rd, cpu_env, tcg_rn, offset, tag_offset);
4241 tcg_temp_free_i32(tag_offset);
4242 tcg_temp_free_i32(offset);
4243 } else {
4244 tcg_gen_addi_i64(tcg_rd, tcg_rn, imm);
4245 gen_address_with_allocation_tag0(tcg_rd, tcg_rd);
4249 /* The input should be a value in the bottom e bits (with higher
4250 * bits zero); returns that value replicated into every element
4251 * of size e in a 64 bit integer.
4253 static uint64_t bitfield_replicate(uint64_t mask, unsigned int e)
4255 assert(e != 0);
4256 while (e < 64) {
4257 mask |= mask << e;
4258 e *= 2;
4260 return mask;
4263 /* Return a value with the bottom len bits set (where 0 < len <= 64) */
4264 static inline uint64_t bitmask64(unsigned int length)
4266 assert(length > 0 && length <= 64);
4267 return ~0ULL >> (64 - length);
4270 /* Simplified variant of pseudocode DecodeBitMasks() for the case where we
4271 * only require the wmask. Returns false if the imms/immr/immn are a reserved
4272 * value (ie should cause a guest UNDEF exception), and true if they are
4273 * valid, in which case the decoded bit pattern is written to result.
4275 bool logic_imm_decode_wmask(uint64_t *result, unsigned int immn,
4276 unsigned int imms, unsigned int immr)
4278 uint64_t mask;
4279 unsigned e, levels, s, r;
4280 int len;
4282 assert(immn < 2 && imms < 64 && immr < 64);
4284 /* The bit patterns we create here are 64 bit patterns which
4285 * are vectors of identical elements of size e = 2, 4, 8, 16, 32 or
4286 * 64 bits each. Each element contains the same value: a run
4287 * of between 1 and e-1 non-zero bits, rotated within the
4288 * element by between 0 and e-1 bits.
4290 * The element size and run length are encoded into immn (1 bit)
4291 * and imms (6 bits) as follows:
4292 * 64 bit elements: immn = 1, imms = <length of run - 1>
4293 * 32 bit elements: immn = 0, imms = 0 : <length of run - 1>
4294 * 16 bit elements: immn = 0, imms = 10 : <length of run - 1>
4295 * 8 bit elements: immn = 0, imms = 110 : <length of run - 1>
4296 * 4 bit elements: immn = 0, imms = 1110 : <length of run - 1>
4297 * 2 bit elements: immn = 0, imms = 11110 : <length of run - 1>
4298 * Notice that immn = 0, imms = 11111x is the only combination
4299 * not covered by one of the above options; this is reserved.
4300 * Further, <length of run - 1> all-ones is a reserved pattern.
4302 * In all cases the rotation is by immr % e (and immr is 6 bits).
4305 /* First determine the element size */
4306 len = 31 - clz32((immn << 6) | (~imms & 0x3f));
4307 if (len < 1) {
4308 /* This is the immn == 0, imms == 0x11111x case */
4309 return false;
4311 e = 1 << len;
4313 levels = e - 1;
4314 s = imms & levels;
4315 r = immr & levels;
4317 if (s == levels) {
4318 /* <length of run - 1> mustn't be all-ones. */
4319 return false;
4322 /* Create the value of one element: s+1 set bits rotated
4323 * by r within the element (which is e bits wide)...
4325 mask = bitmask64(s + 1);
4326 if (r) {
4327 mask = (mask >> r) | (mask << (e - r));
4328 mask &= bitmask64(e);
4330 /* ...then replicate the element over the whole 64 bit value */
4331 mask = bitfield_replicate(mask, e);
4332 *result = mask;
4333 return true;
4336 /* Logical (immediate)
4337 * 31 30 29 28 23 22 21 16 15 10 9 5 4 0
4338 * +----+-----+-------------+---+------+------+------+------+
4339 * | sf | opc | 1 0 0 1 0 0 | N | immr | imms | Rn | Rd |
4340 * +----+-----+-------------+---+------+------+------+------+
4342 static void disas_logic_imm(DisasContext *s, uint32_t insn)
4344 unsigned int sf, opc, is_n, immr, imms, rn, rd;
4345 TCGv_i64 tcg_rd, tcg_rn;
4346 uint64_t wmask;
4347 bool is_and = false;
4349 sf = extract32(insn, 31, 1);
4350 opc = extract32(insn, 29, 2);
4351 is_n = extract32(insn, 22, 1);
4352 immr = extract32(insn, 16, 6);
4353 imms = extract32(insn, 10, 6);
4354 rn = extract32(insn, 5, 5);
4355 rd = extract32(insn, 0, 5);
4357 if (!sf && is_n) {
4358 unallocated_encoding(s);
4359 return;
4362 if (opc == 0x3) { /* ANDS */
4363 tcg_rd = cpu_reg(s, rd);
4364 } else {
4365 tcg_rd = cpu_reg_sp(s, rd);
4367 tcg_rn = cpu_reg(s, rn);
4369 if (!logic_imm_decode_wmask(&wmask, is_n, imms, immr)) {
4370 /* some immediate field values are reserved */
4371 unallocated_encoding(s);
4372 return;
4375 if (!sf) {
4376 wmask &= 0xffffffff;
4379 switch (opc) {
4380 case 0x3: /* ANDS */
4381 case 0x0: /* AND */
4382 tcg_gen_andi_i64(tcg_rd, tcg_rn, wmask);
4383 is_and = true;
4384 break;
4385 case 0x1: /* ORR */
4386 tcg_gen_ori_i64(tcg_rd, tcg_rn, wmask);
4387 break;
4388 case 0x2: /* EOR */
4389 tcg_gen_xori_i64(tcg_rd, tcg_rn, wmask);
4390 break;
4391 default:
4392 assert(FALSE); /* must handle all above */
4393 break;
4396 if (!sf && !is_and) {
4397 /* zero extend final result; we know we can skip this for AND
4398 * since the immediate had the high 32 bits clear.
4400 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4403 if (opc == 3) { /* ANDS */
4404 gen_logic_CC(sf, tcg_rd);
4409 * Move wide (immediate)
4411 * 31 30 29 28 23 22 21 20 5 4 0
4412 * +--+-----+-------------+-----+----------------+------+
4413 * |sf| opc | 1 0 0 1 0 1 | hw | imm16 | Rd |
4414 * +--+-----+-------------+-----+----------------+------+
4416 * sf: 0 -> 32 bit, 1 -> 64 bit
4417 * opc: 00 -> N, 10 -> Z, 11 -> K
4418 * hw: shift/16 (0,16, and sf only 32, 48)
4420 static void disas_movw_imm(DisasContext *s, uint32_t insn)
4422 int rd = extract32(insn, 0, 5);
4423 uint64_t imm = extract32(insn, 5, 16);
4424 int sf = extract32(insn, 31, 1);
4425 int opc = extract32(insn, 29, 2);
4426 int pos = extract32(insn, 21, 2) << 4;
4427 TCGv_i64 tcg_rd = cpu_reg(s, rd);
4428 TCGv_i64 tcg_imm;
4430 if (!sf && (pos >= 32)) {
4431 unallocated_encoding(s);
4432 return;
4435 switch (opc) {
4436 case 0: /* MOVN */
4437 case 2: /* MOVZ */
4438 imm <<= pos;
4439 if (opc == 0) {
4440 imm = ~imm;
4442 if (!sf) {
4443 imm &= 0xffffffffu;
4445 tcg_gen_movi_i64(tcg_rd, imm);
4446 break;
4447 case 3: /* MOVK */
4448 tcg_imm = tcg_const_i64(imm);
4449 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_imm, pos, 16);
4450 tcg_temp_free_i64(tcg_imm);
4451 if (!sf) {
4452 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4454 break;
4455 default:
4456 unallocated_encoding(s);
4457 break;
4461 /* Bitfield
4462 * 31 30 29 28 23 22 21 16 15 10 9 5 4 0
4463 * +----+-----+-------------+---+------+------+------+------+
4464 * | sf | opc | 1 0 0 1 1 0 | N | immr | imms | Rn | Rd |
4465 * +----+-----+-------------+---+------+------+------+------+
4467 static void disas_bitfield(DisasContext *s, uint32_t insn)
4469 unsigned int sf, n, opc, ri, si, rn, rd, bitsize, pos, len;
4470 TCGv_i64 tcg_rd, tcg_tmp;
4472 sf = extract32(insn, 31, 1);
4473 opc = extract32(insn, 29, 2);
4474 n = extract32(insn, 22, 1);
4475 ri = extract32(insn, 16, 6);
4476 si = extract32(insn, 10, 6);
4477 rn = extract32(insn, 5, 5);
4478 rd = extract32(insn, 0, 5);
4479 bitsize = sf ? 64 : 32;
4481 if (sf != n || ri >= bitsize || si >= bitsize || opc > 2) {
4482 unallocated_encoding(s);
4483 return;
4486 tcg_rd = cpu_reg(s, rd);
4488 /* Suppress the zero-extend for !sf. Since RI and SI are constrained
4489 to be smaller than bitsize, we'll never reference data outside the
4490 low 32-bits anyway. */
4491 tcg_tmp = read_cpu_reg(s, rn, 1);
4493 /* Recognize simple(r) extractions. */
4494 if (si >= ri) {
4495 /* Wd<s-r:0> = Wn<s:r> */
4496 len = (si - ri) + 1;
4497 if (opc == 0) { /* SBFM: ASR, SBFX, SXTB, SXTH, SXTW */
4498 tcg_gen_sextract_i64(tcg_rd, tcg_tmp, ri, len);
4499 goto done;
4500 } else if (opc == 2) { /* UBFM: UBFX, LSR, UXTB, UXTH */
4501 tcg_gen_extract_i64(tcg_rd, tcg_tmp, ri, len);
4502 return;
4504 /* opc == 1, BFXIL fall through to deposit */
4505 tcg_gen_shri_i64(tcg_tmp, tcg_tmp, ri);
4506 pos = 0;
4507 } else {
4508 /* Handle the ri > si case with a deposit
4509 * Wd<32+s-r,32-r> = Wn<s:0>
4511 len = si + 1;
4512 pos = (bitsize - ri) & (bitsize - 1);
4515 if (opc == 0 && len < ri) {
4516 /* SBFM: sign extend the destination field from len to fill
4517 the balance of the word. Let the deposit below insert all
4518 of those sign bits. */
4519 tcg_gen_sextract_i64(tcg_tmp, tcg_tmp, 0, len);
4520 len = ri;
4523 if (opc == 1) { /* BFM, BFXIL */
4524 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, pos, len);
4525 } else {
4526 /* SBFM or UBFM: We start with zero, and we haven't modified
4527 any bits outside bitsize, therefore the zero-extension
4528 below is unneeded. */
4529 tcg_gen_deposit_z_i64(tcg_rd, tcg_tmp, pos, len);
4530 return;
4533 done:
4534 if (!sf) { /* zero extend final result */
4535 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4539 /* Extract
4540 * 31 30 29 28 23 22 21 20 16 15 10 9 5 4 0
4541 * +----+------+-------------+---+----+------+--------+------+------+
4542 * | sf | op21 | 1 0 0 1 1 1 | N | o0 | Rm | imms | Rn | Rd |
4543 * +----+------+-------------+---+----+------+--------+------+------+
4545 static void disas_extract(DisasContext *s, uint32_t insn)
4547 unsigned int sf, n, rm, imm, rn, rd, bitsize, op21, op0;
4549 sf = extract32(insn, 31, 1);
4550 n = extract32(insn, 22, 1);
4551 rm = extract32(insn, 16, 5);
4552 imm = extract32(insn, 10, 6);
4553 rn = extract32(insn, 5, 5);
4554 rd = extract32(insn, 0, 5);
4555 op21 = extract32(insn, 29, 2);
4556 op0 = extract32(insn, 21, 1);
4557 bitsize = sf ? 64 : 32;
4559 if (sf != n || op21 || op0 || imm >= bitsize) {
4560 unallocated_encoding(s);
4561 } else {
4562 TCGv_i64 tcg_rd, tcg_rm, tcg_rn;
4564 tcg_rd = cpu_reg(s, rd);
4566 if (unlikely(imm == 0)) {
4567 /* tcg shl_i32/shl_i64 is undefined for 32/64 bit shifts,
4568 * so an extract from bit 0 is a special case.
4570 if (sf) {
4571 tcg_gen_mov_i64(tcg_rd, cpu_reg(s, rm));
4572 } else {
4573 tcg_gen_ext32u_i64(tcg_rd, cpu_reg(s, rm));
4575 } else {
4576 tcg_rm = cpu_reg(s, rm);
4577 tcg_rn = cpu_reg(s, rn);
4579 if (sf) {
4580 /* Specialization to ROR happens in EXTRACT2. */
4581 tcg_gen_extract2_i64(tcg_rd, tcg_rm, tcg_rn, imm);
4582 } else {
4583 TCGv_i32 t0 = tcg_temp_new_i32();
4585 tcg_gen_extrl_i64_i32(t0, tcg_rm);
4586 if (rm == rn) {
4587 tcg_gen_rotri_i32(t0, t0, imm);
4588 } else {
4589 TCGv_i32 t1 = tcg_temp_new_i32();
4590 tcg_gen_extrl_i64_i32(t1, tcg_rn);
4591 tcg_gen_extract2_i32(t0, t0, t1, imm);
4592 tcg_temp_free_i32(t1);
4594 tcg_gen_extu_i32_i64(tcg_rd, t0);
4595 tcg_temp_free_i32(t0);
4601 /* Data processing - immediate */
4602 static void disas_data_proc_imm(DisasContext *s, uint32_t insn)
4604 switch (extract32(insn, 23, 6)) {
4605 case 0x20: case 0x21: /* PC-rel. addressing */
4606 disas_pc_rel_adr(s, insn);
4607 break;
4608 case 0x22: /* Add/subtract (immediate) */
4609 disas_add_sub_imm(s, insn);
4610 break;
4611 case 0x23: /* Add/subtract (immediate, with tags) */
4612 disas_add_sub_imm_with_tags(s, insn);
4613 break;
4614 case 0x24: /* Logical (immediate) */
4615 disas_logic_imm(s, insn);
4616 break;
4617 case 0x25: /* Move wide (immediate) */
4618 disas_movw_imm(s, insn);
4619 break;
4620 case 0x26: /* Bitfield */
4621 disas_bitfield(s, insn);
4622 break;
4623 case 0x27: /* Extract */
4624 disas_extract(s, insn);
4625 break;
4626 default:
4627 unallocated_encoding(s);
4628 break;
4632 /* Shift a TCGv src by TCGv shift_amount, put result in dst.
4633 * Note that it is the caller's responsibility to ensure that the
4634 * shift amount is in range (ie 0..31 or 0..63) and provide the ARM
4635 * mandated semantics for out of range shifts.
4637 static void shift_reg(TCGv_i64 dst, TCGv_i64 src, int sf,
4638 enum a64_shift_type shift_type, TCGv_i64 shift_amount)
4640 switch (shift_type) {
4641 case A64_SHIFT_TYPE_LSL:
4642 tcg_gen_shl_i64(dst, src, shift_amount);
4643 break;
4644 case A64_SHIFT_TYPE_LSR:
4645 tcg_gen_shr_i64(dst, src, shift_amount);
4646 break;
4647 case A64_SHIFT_TYPE_ASR:
4648 if (!sf) {
4649 tcg_gen_ext32s_i64(dst, src);
4651 tcg_gen_sar_i64(dst, sf ? src : dst, shift_amount);
4652 break;
4653 case A64_SHIFT_TYPE_ROR:
4654 if (sf) {
4655 tcg_gen_rotr_i64(dst, src, shift_amount);
4656 } else {
4657 TCGv_i32 t0, t1;
4658 t0 = tcg_temp_new_i32();
4659 t1 = tcg_temp_new_i32();
4660 tcg_gen_extrl_i64_i32(t0, src);
4661 tcg_gen_extrl_i64_i32(t1, shift_amount);
4662 tcg_gen_rotr_i32(t0, t0, t1);
4663 tcg_gen_extu_i32_i64(dst, t0);
4664 tcg_temp_free_i32(t0);
4665 tcg_temp_free_i32(t1);
4667 break;
4668 default:
4669 assert(FALSE); /* all shift types should be handled */
4670 break;
4673 if (!sf) { /* zero extend final result */
4674 tcg_gen_ext32u_i64(dst, dst);
4678 /* Shift a TCGv src by immediate, put result in dst.
4679 * The shift amount must be in range (this should always be true as the
4680 * relevant instructions will UNDEF on bad shift immediates).
4682 static void shift_reg_imm(TCGv_i64 dst, TCGv_i64 src, int sf,
4683 enum a64_shift_type shift_type, unsigned int shift_i)
4685 assert(shift_i < (sf ? 64 : 32));
4687 if (shift_i == 0) {
4688 tcg_gen_mov_i64(dst, src);
4689 } else {
4690 TCGv_i64 shift_const;
4692 shift_const = tcg_const_i64(shift_i);
4693 shift_reg(dst, src, sf, shift_type, shift_const);
4694 tcg_temp_free_i64(shift_const);
4698 /* Logical (shifted register)
4699 * 31 30 29 28 24 23 22 21 20 16 15 10 9 5 4 0
4700 * +----+-----+-----------+-------+---+------+--------+------+------+
4701 * | sf | opc | 0 1 0 1 0 | shift | N | Rm | imm6 | Rn | Rd |
4702 * +----+-----+-----------+-------+---+------+--------+------+------+
4704 static void disas_logic_reg(DisasContext *s, uint32_t insn)
4706 TCGv_i64 tcg_rd, tcg_rn, tcg_rm;
4707 unsigned int sf, opc, shift_type, invert, rm, shift_amount, rn, rd;
4709 sf = extract32(insn, 31, 1);
4710 opc = extract32(insn, 29, 2);
4711 shift_type = extract32(insn, 22, 2);
4712 invert = extract32(insn, 21, 1);
4713 rm = extract32(insn, 16, 5);
4714 shift_amount = extract32(insn, 10, 6);
4715 rn = extract32(insn, 5, 5);
4716 rd = extract32(insn, 0, 5);
4718 if (!sf && (shift_amount & (1 << 5))) {
4719 unallocated_encoding(s);
4720 return;
4723 tcg_rd = cpu_reg(s, rd);
4725 if (opc == 1 && shift_amount == 0 && shift_type == 0 && rn == 31) {
4726 /* Unshifted ORR and ORN with WZR/XZR is the standard encoding for
4727 * register-register MOV and MVN, so it is worth special casing.
4729 tcg_rm = cpu_reg(s, rm);
4730 if (invert) {
4731 tcg_gen_not_i64(tcg_rd, tcg_rm);
4732 if (!sf) {
4733 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4735 } else {
4736 if (sf) {
4737 tcg_gen_mov_i64(tcg_rd, tcg_rm);
4738 } else {
4739 tcg_gen_ext32u_i64(tcg_rd, tcg_rm);
4742 return;
4745 tcg_rm = read_cpu_reg(s, rm, sf);
4747 if (shift_amount) {
4748 shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, shift_amount);
4751 tcg_rn = cpu_reg(s, rn);
4753 switch (opc | (invert << 2)) {
4754 case 0: /* AND */
4755 case 3: /* ANDS */
4756 tcg_gen_and_i64(tcg_rd, tcg_rn, tcg_rm);
4757 break;
4758 case 1: /* ORR */
4759 tcg_gen_or_i64(tcg_rd, tcg_rn, tcg_rm);
4760 break;
4761 case 2: /* EOR */
4762 tcg_gen_xor_i64(tcg_rd, tcg_rn, tcg_rm);
4763 break;
4764 case 4: /* BIC */
4765 case 7: /* BICS */
4766 tcg_gen_andc_i64(tcg_rd, tcg_rn, tcg_rm);
4767 break;
4768 case 5: /* ORN */
4769 tcg_gen_orc_i64(tcg_rd, tcg_rn, tcg_rm);
4770 break;
4771 case 6: /* EON */
4772 tcg_gen_eqv_i64(tcg_rd, tcg_rn, tcg_rm);
4773 break;
4774 default:
4775 assert(FALSE);
4776 break;
4779 if (!sf) {
4780 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4783 if (opc == 3) {
4784 gen_logic_CC(sf, tcg_rd);
4789 * Add/subtract (extended register)
4791 * 31|30|29|28 24|23 22|21|20 16|15 13|12 10|9 5|4 0|
4792 * +--+--+--+-----------+-----+--+-------+------+------+----+----+
4793 * |sf|op| S| 0 1 0 1 1 | opt | 1| Rm |option| imm3 | Rn | Rd |
4794 * +--+--+--+-----------+-----+--+-------+------+------+----+----+
4796 * sf: 0 -> 32bit, 1 -> 64bit
4797 * op: 0 -> add , 1 -> sub
4798 * S: 1 -> set flags
4799 * opt: 00
4800 * option: extension type (see DecodeRegExtend)
4801 * imm3: optional shift to Rm
4803 * Rd = Rn + LSL(extend(Rm), amount)
4805 static void disas_add_sub_ext_reg(DisasContext *s, uint32_t insn)
4807 int rd = extract32(insn, 0, 5);
4808 int rn = extract32(insn, 5, 5);
4809 int imm3 = extract32(insn, 10, 3);
4810 int option = extract32(insn, 13, 3);
4811 int rm = extract32(insn, 16, 5);
4812 int opt = extract32(insn, 22, 2);
4813 bool setflags = extract32(insn, 29, 1);
4814 bool sub_op = extract32(insn, 30, 1);
4815 bool sf = extract32(insn, 31, 1);
4817 TCGv_i64 tcg_rm, tcg_rn; /* temps */
4818 TCGv_i64 tcg_rd;
4819 TCGv_i64 tcg_result;
4821 if (imm3 > 4 || opt != 0) {
4822 unallocated_encoding(s);
4823 return;
4826 /* non-flag setting ops may use SP */
4827 if (!setflags) {
4828 tcg_rd = cpu_reg_sp(s, rd);
4829 } else {
4830 tcg_rd = cpu_reg(s, rd);
4832 tcg_rn = read_cpu_reg_sp(s, rn, sf);
4834 tcg_rm = read_cpu_reg(s, rm, sf);
4835 ext_and_shift_reg(tcg_rm, tcg_rm, option, imm3);
4837 tcg_result = tcg_temp_new_i64();
4839 if (!setflags) {
4840 if (sub_op) {
4841 tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
4842 } else {
4843 tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
4845 } else {
4846 if (sub_op) {
4847 gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
4848 } else {
4849 gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
4853 if (sf) {
4854 tcg_gen_mov_i64(tcg_rd, tcg_result);
4855 } else {
4856 tcg_gen_ext32u_i64(tcg_rd, tcg_result);
4859 tcg_temp_free_i64(tcg_result);
4863 * Add/subtract (shifted register)
4865 * 31 30 29 28 24 23 22 21 20 16 15 10 9 5 4 0
4866 * +--+--+--+-----------+-----+--+-------+---------+------+------+
4867 * |sf|op| S| 0 1 0 1 1 |shift| 0| Rm | imm6 | Rn | Rd |
4868 * +--+--+--+-----------+-----+--+-------+---------+------+------+
4870 * sf: 0 -> 32bit, 1 -> 64bit
4871 * op: 0 -> add , 1 -> sub
4872 * S: 1 -> set flags
4873 * shift: 00 -> LSL, 01 -> LSR, 10 -> ASR, 11 -> RESERVED
4874 * imm6: Shift amount to apply to Rm before the add/sub
4876 static void disas_add_sub_reg(DisasContext *s, uint32_t insn)
4878 int rd = extract32(insn, 0, 5);
4879 int rn = extract32(insn, 5, 5);
4880 int imm6 = extract32(insn, 10, 6);
4881 int rm = extract32(insn, 16, 5);
4882 int shift_type = extract32(insn, 22, 2);
4883 bool setflags = extract32(insn, 29, 1);
4884 bool sub_op = extract32(insn, 30, 1);
4885 bool sf = extract32(insn, 31, 1);
4887 TCGv_i64 tcg_rd = cpu_reg(s, rd);
4888 TCGv_i64 tcg_rn, tcg_rm;
4889 TCGv_i64 tcg_result;
4891 if ((shift_type == 3) || (!sf && (imm6 > 31))) {
4892 unallocated_encoding(s);
4893 return;
4896 tcg_rn = read_cpu_reg(s, rn, sf);
4897 tcg_rm = read_cpu_reg(s, rm, sf);
4899 shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, imm6);
4901 tcg_result = tcg_temp_new_i64();
4903 if (!setflags) {
4904 if (sub_op) {
4905 tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
4906 } else {
4907 tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
4909 } else {
4910 if (sub_op) {
4911 gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
4912 } else {
4913 gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
4917 if (sf) {
4918 tcg_gen_mov_i64(tcg_rd, tcg_result);
4919 } else {
4920 tcg_gen_ext32u_i64(tcg_rd, tcg_result);
4923 tcg_temp_free_i64(tcg_result);
4926 /* Data-processing (3 source)
4928 * 31 30 29 28 24 23 21 20 16 15 14 10 9 5 4 0
4929 * +--+------+-----------+------+------+----+------+------+------+
4930 * |sf| op54 | 1 1 0 1 1 | op31 | Rm | o0 | Ra | Rn | Rd |
4931 * +--+------+-----------+------+------+----+------+------+------+
4933 static void disas_data_proc_3src(DisasContext *s, uint32_t insn)
4935 int rd = extract32(insn, 0, 5);
4936 int rn = extract32(insn, 5, 5);
4937 int ra = extract32(insn, 10, 5);
4938 int rm = extract32(insn, 16, 5);
4939 int op_id = (extract32(insn, 29, 3) << 4) |
4940 (extract32(insn, 21, 3) << 1) |
4941 extract32(insn, 15, 1);
4942 bool sf = extract32(insn, 31, 1);
4943 bool is_sub = extract32(op_id, 0, 1);
4944 bool is_high = extract32(op_id, 2, 1);
4945 bool is_signed = false;
4946 TCGv_i64 tcg_op1;
4947 TCGv_i64 tcg_op2;
4948 TCGv_i64 tcg_tmp;
4950 /* Note that op_id is sf:op54:op31:o0 so it includes the 32/64 size flag */
4951 switch (op_id) {
4952 case 0x42: /* SMADDL */
4953 case 0x43: /* SMSUBL */
4954 case 0x44: /* SMULH */
4955 is_signed = true;
4956 break;
4957 case 0x0: /* MADD (32bit) */
4958 case 0x1: /* MSUB (32bit) */
4959 case 0x40: /* MADD (64bit) */
4960 case 0x41: /* MSUB (64bit) */
4961 case 0x4a: /* UMADDL */
4962 case 0x4b: /* UMSUBL */
4963 case 0x4c: /* UMULH */
4964 break;
4965 default:
4966 unallocated_encoding(s);
4967 return;
4970 if (is_high) {
4971 TCGv_i64 low_bits = tcg_temp_new_i64(); /* low bits discarded */
4972 TCGv_i64 tcg_rd = cpu_reg(s, rd);
4973 TCGv_i64 tcg_rn = cpu_reg(s, rn);
4974 TCGv_i64 tcg_rm = cpu_reg(s, rm);
4976 if (is_signed) {
4977 tcg_gen_muls2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
4978 } else {
4979 tcg_gen_mulu2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
4982 tcg_temp_free_i64(low_bits);
4983 return;
4986 tcg_op1 = tcg_temp_new_i64();
4987 tcg_op2 = tcg_temp_new_i64();
4988 tcg_tmp = tcg_temp_new_i64();
4990 if (op_id < 0x42) {
4991 tcg_gen_mov_i64(tcg_op1, cpu_reg(s, rn));
4992 tcg_gen_mov_i64(tcg_op2, cpu_reg(s, rm));
4993 } else {
4994 if (is_signed) {
4995 tcg_gen_ext32s_i64(tcg_op1, cpu_reg(s, rn));
4996 tcg_gen_ext32s_i64(tcg_op2, cpu_reg(s, rm));
4997 } else {
4998 tcg_gen_ext32u_i64(tcg_op1, cpu_reg(s, rn));
4999 tcg_gen_ext32u_i64(tcg_op2, cpu_reg(s, rm));
5003 if (ra == 31 && !is_sub) {
5004 /* Special-case MADD with rA == XZR; it is the standard MUL alias */
5005 tcg_gen_mul_i64(cpu_reg(s, rd), tcg_op1, tcg_op2);
5006 } else {
5007 tcg_gen_mul_i64(tcg_tmp, tcg_op1, tcg_op2);
5008 if (is_sub) {
5009 tcg_gen_sub_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
5010 } else {
5011 tcg_gen_add_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
5015 if (!sf) {
5016 tcg_gen_ext32u_i64(cpu_reg(s, rd), cpu_reg(s, rd));
5019 tcg_temp_free_i64(tcg_op1);
5020 tcg_temp_free_i64(tcg_op2);
5021 tcg_temp_free_i64(tcg_tmp);
5024 /* Add/subtract (with carry)
5025 * 31 30 29 28 27 26 25 24 23 22 21 20 16 15 10 9 5 4 0
5026 * +--+--+--+------------------------+------+-------------+------+-----+
5027 * |sf|op| S| 1 1 0 1 0 0 0 0 | rm | 0 0 0 0 0 0 | Rn | Rd |
5028 * +--+--+--+------------------------+------+-------------+------+-----+
5031 static void disas_adc_sbc(DisasContext *s, uint32_t insn)
5033 unsigned int sf, op, setflags, rm, rn, rd;
5034 TCGv_i64 tcg_y, tcg_rn, tcg_rd;
5036 sf = extract32(insn, 31, 1);
5037 op = extract32(insn, 30, 1);
5038 setflags = extract32(insn, 29, 1);
5039 rm = extract32(insn, 16, 5);
5040 rn = extract32(insn, 5, 5);
5041 rd = extract32(insn, 0, 5);
5043 tcg_rd = cpu_reg(s, rd);
5044 tcg_rn = cpu_reg(s, rn);
5046 if (op) {
5047 tcg_y = new_tmp_a64(s);
5048 tcg_gen_not_i64(tcg_y, cpu_reg(s, rm));
5049 } else {
5050 tcg_y = cpu_reg(s, rm);
5053 if (setflags) {
5054 gen_adc_CC(sf, tcg_rd, tcg_rn, tcg_y);
5055 } else {
5056 gen_adc(sf, tcg_rd, tcg_rn, tcg_y);
5061 * Rotate right into flags
5062 * 31 30 29 21 15 10 5 4 0
5063 * +--+--+--+-----------------+--------+-----------+------+--+------+
5064 * |sf|op| S| 1 1 0 1 0 0 0 0 | imm6 | 0 0 0 0 1 | Rn |o2| mask |
5065 * +--+--+--+-----------------+--------+-----------+------+--+------+
5067 static void disas_rotate_right_into_flags(DisasContext *s, uint32_t insn)
5069 int mask = extract32(insn, 0, 4);
5070 int o2 = extract32(insn, 4, 1);
5071 int rn = extract32(insn, 5, 5);
5072 int imm6 = extract32(insn, 15, 6);
5073 int sf_op_s = extract32(insn, 29, 3);
5074 TCGv_i64 tcg_rn;
5075 TCGv_i32 nzcv;
5077 if (sf_op_s != 5 || o2 != 0 || !dc_isar_feature(aa64_condm_4, s)) {
5078 unallocated_encoding(s);
5079 return;
5082 tcg_rn = read_cpu_reg(s, rn, 1);
5083 tcg_gen_rotri_i64(tcg_rn, tcg_rn, imm6);
5085 nzcv = tcg_temp_new_i32();
5086 tcg_gen_extrl_i64_i32(nzcv, tcg_rn);
5088 if (mask & 8) { /* N */
5089 tcg_gen_shli_i32(cpu_NF, nzcv, 31 - 3);
5091 if (mask & 4) { /* Z */
5092 tcg_gen_not_i32(cpu_ZF, nzcv);
5093 tcg_gen_andi_i32(cpu_ZF, cpu_ZF, 4);
5095 if (mask & 2) { /* C */
5096 tcg_gen_extract_i32(cpu_CF, nzcv, 1, 1);
5098 if (mask & 1) { /* V */
5099 tcg_gen_shli_i32(cpu_VF, nzcv, 31 - 0);
5102 tcg_temp_free_i32(nzcv);
5106 * Evaluate into flags
5107 * 31 30 29 21 15 14 10 5 4 0
5108 * +--+--+--+-----------------+---------+----+---------+------+--+------+
5109 * |sf|op| S| 1 1 0 1 0 0 0 0 | opcode2 | sz | 0 0 1 0 | Rn |o3| mask |
5110 * +--+--+--+-----------------+---------+----+---------+------+--+------+
5112 static void disas_evaluate_into_flags(DisasContext *s, uint32_t insn)
5114 int o3_mask = extract32(insn, 0, 5);
5115 int rn = extract32(insn, 5, 5);
5116 int o2 = extract32(insn, 15, 6);
5117 int sz = extract32(insn, 14, 1);
5118 int sf_op_s = extract32(insn, 29, 3);
5119 TCGv_i32 tmp;
5120 int shift;
5122 if (sf_op_s != 1 || o2 != 0 || o3_mask != 0xd ||
5123 !dc_isar_feature(aa64_condm_4, s)) {
5124 unallocated_encoding(s);
5125 return;
5127 shift = sz ? 16 : 24; /* SETF16 or SETF8 */
5129 tmp = tcg_temp_new_i32();
5130 tcg_gen_extrl_i64_i32(tmp, cpu_reg(s, rn));
5131 tcg_gen_shli_i32(cpu_NF, tmp, shift);
5132 tcg_gen_shli_i32(cpu_VF, tmp, shift - 1);
5133 tcg_gen_mov_i32(cpu_ZF, cpu_NF);
5134 tcg_gen_xor_i32(cpu_VF, cpu_VF, cpu_NF);
5135 tcg_temp_free_i32(tmp);
5138 /* Conditional compare (immediate / register)
5139 * 31 30 29 28 27 26 25 24 23 22 21 20 16 15 12 11 10 9 5 4 3 0
5140 * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
5141 * |sf|op| S| 1 1 0 1 0 0 1 0 |imm5/rm | cond |i/r |o2| Rn |o3|nzcv |
5142 * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
5143 * [1] y [0] [0]
5145 static void disas_cc(DisasContext *s, uint32_t insn)
5147 unsigned int sf, op, y, cond, rn, nzcv, is_imm;
5148 TCGv_i32 tcg_t0, tcg_t1, tcg_t2;
5149 TCGv_i64 tcg_tmp, tcg_y, tcg_rn;
5150 DisasCompare c;
5152 if (!extract32(insn, 29, 1)) {
5153 unallocated_encoding(s);
5154 return;
5156 if (insn & (1 << 10 | 1 << 4)) {
5157 unallocated_encoding(s);
5158 return;
5160 sf = extract32(insn, 31, 1);
5161 op = extract32(insn, 30, 1);
5162 is_imm = extract32(insn, 11, 1);
5163 y = extract32(insn, 16, 5); /* y = rm (reg) or imm5 (imm) */
5164 cond = extract32(insn, 12, 4);
5165 rn = extract32(insn, 5, 5);
5166 nzcv = extract32(insn, 0, 4);
5168 /* Set T0 = !COND. */
5169 tcg_t0 = tcg_temp_new_i32();
5170 arm_test_cc(&c, cond);
5171 tcg_gen_setcondi_i32(tcg_invert_cond(c.cond), tcg_t0, c.value, 0);
5172 arm_free_cc(&c);
5174 /* Load the arguments for the new comparison. */
5175 if (is_imm) {
5176 tcg_y = new_tmp_a64(s);
5177 tcg_gen_movi_i64(tcg_y, y);
5178 } else {
5179 tcg_y = cpu_reg(s, y);
5181 tcg_rn = cpu_reg(s, rn);
5183 /* Set the flags for the new comparison. */
5184 tcg_tmp = tcg_temp_new_i64();
5185 if (op) {
5186 gen_sub_CC(sf, tcg_tmp, tcg_rn, tcg_y);
5187 } else {
5188 gen_add_CC(sf, tcg_tmp, tcg_rn, tcg_y);
5190 tcg_temp_free_i64(tcg_tmp);
5192 /* If COND was false, force the flags to #nzcv. Compute two masks
5193 * to help with this: T1 = (COND ? 0 : -1), T2 = (COND ? -1 : 0).
5194 * For tcg hosts that support ANDC, we can make do with just T1.
5195 * In either case, allow the tcg optimizer to delete any unused mask.
5197 tcg_t1 = tcg_temp_new_i32();
5198 tcg_t2 = tcg_temp_new_i32();
5199 tcg_gen_neg_i32(tcg_t1, tcg_t0);
5200 tcg_gen_subi_i32(tcg_t2, tcg_t0, 1);
5202 if (nzcv & 8) { /* N */
5203 tcg_gen_or_i32(cpu_NF, cpu_NF, tcg_t1);
5204 } else {
5205 if (TCG_TARGET_HAS_andc_i32) {
5206 tcg_gen_andc_i32(cpu_NF, cpu_NF, tcg_t1);
5207 } else {
5208 tcg_gen_and_i32(cpu_NF, cpu_NF, tcg_t2);
5211 if (nzcv & 4) { /* Z */
5212 if (TCG_TARGET_HAS_andc_i32) {
5213 tcg_gen_andc_i32(cpu_ZF, cpu_ZF, tcg_t1);
5214 } else {
5215 tcg_gen_and_i32(cpu_ZF, cpu_ZF, tcg_t2);
5217 } else {
5218 tcg_gen_or_i32(cpu_ZF, cpu_ZF, tcg_t0);
5220 if (nzcv & 2) { /* C */
5221 tcg_gen_or_i32(cpu_CF, cpu_CF, tcg_t0);
5222 } else {
5223 if (TCG_TARGET_HAS_andc_i32) {
5224 tcg_gen_andc_i32(cpu_CF, cpu_CF, tcg_t1);
5225 } else {
5226 tcg_gen_and_i32(cpu_CF, cpu_CF, tcg_t2);
5229 if (nzcv & 1) { /* V */
5230 tcg_gen_or_i32(cpu_VF, cpu_VF, tcg_t1);
5231 } else {
5232 if (TCG_TARGET_HAS_andc_i32) {
5233 tcg_gen_andc_i32(cpu_VF, cpu_VF, tcg_t1);
5234 } else {
5235 tcg_gen_and_i32(cpu_VF, cpu_VF, tcg_t2);
5238 tcg_temp_free_i32(tcg_t0);
5239 tcg_temp_free_i32(tcg_t1);
5240 tcg_temp_free_i32(tcg_t2);
5243 /* Conditional select
5244 * 31 30 29 28 21 20 16 15 12 11 10 9 5 4 0
5245 * +----+----+---+-----------------+------+------+-----+------+------+
5246 * | sf | op | S | 1 1 0 1 0 1 0 0 | Rm | cond | op2 | Rn | Rd |
5247 * +----+----+---+-----------------+------+------+-----+------+------+
5249 static void disas_cond_select(DisasContext *s, uint32_t insn)
5251 unsigned int sf, else_inv, rm, cond, else_inc, rn, rd;
5252 TCGv_i64 tcg_rd, zero;
5253 DisasCompare64 c;
5255 if (extract32(insn, 29, 1) || extract32(insn, 11, 1)) {
5256 /* S == 1 or op2<1> == 1 */
5257 unallocated_encoding(s);
5258 return;
5260 sf = extract32(insn, 31, 1);
5261 else_inv = extract32(insn, 30, 1);
5262 rm = extract32(insn, 16, 5);
5263 cond = extract32(insn, 12, 4);
5264 else_inc = extract32(insn, 10, 1);
5265 rn = extract32(insn, 5, 5);
5266 rd = extract32(insn, 0, 5);
5268 tcg_rd = cpu_reg(s, rd);
5270 a64_test_cc(&c, cond);
5271 zero = tcg_const_i64(0);
5273 if (rn == 31 && rm == 31 && (else_inc ^ else_inv)) {
5274 /* CSET & CSETM. */
5275 tcg_gen_setcond_i64(tcg_invert_cond(c.cond), tcg_rd, c.value, zero);
5276 if (else_inv) {
5277 tcg_gen_neg_i64(tcg_rd, tcg_rd);
5279 } else {
5280 TCGv_i64 t_true = cpu_reg(s, rn);
5281 TCGv_i64 t_false = read_cpu_reg(s, rm, 1);
5282 if (else_inv && else_inc) {
5283 tcg_gen_neg_i64(t_false, t_false);
5284 } else if (else_inv) {
5285 tcg_gen_not_i64(t_false, t_false);
5286 } else if (else_inc) {
5287 tcg_gen_addi_i64(t_false, t_false, 1);
5289 tcg_gen_movcond_i64(c.cond, tcg_rd, c.value, zero, t_true, t_false);
5292 tcg_temp_free_i64(zero);
5293 a64_free_cc(&c);
5295 if (!sf) {
5296 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
5300 static void handle_clz(DisasContext *s, unsigned int sf,
5301 unsigned int rn, unsigned int rd)
5303 TCGv_i64 tcg_rd, tcg_rn;
5304 tcg_rd = cpu_reg(s, rd);
5305 tcg_rn = cpu_reg(s, rn);
5307 if (sf) {
5308 tcg_gen_clzi_i64(tcg_rd, tcg_rn, 64);
5309 } else {
5310 TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
5311 tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
5312 tcg_gen_clzi_i32(tcg_tmp32, tcg_tmp32, 32);
5313 tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
5314 tcg_temp_free_i32(tcg_tmp32);
5318 static void handle_cls(DisasContext *s, unsigned int sf,
5319 unsigned int rn, unsigned int rd)
5321 TCGv_i64 tcg_rd, tcg_rn;
5322 tcg_rd = cpu_reg(s, rd);
5323 tcg_rn = cpu_reg(s, rn);
5325 if (sf) {
5326 tcg_gen_clrsb_i64(tcg_rd, tcg_rn);
5327 } else {
5328 TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
5329 tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
5330 tcg_gen_clrsb_i32(tcg_tmp32, tcg_tmp32);
5331 tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
5332 tcg_temp_free_i32(tcg_tmp32);
5336 static void handle_rbit(DisasContext *s, unsigned int sf,
5337 unsigned int rn, unsigned int rd)
5339 TCGv_i64 tcg_rd, tcg_rn;
5340 tcg_rd = cpu_reg(s, rd);
5341 tcg_rn = cpu_reg(s, rn);
5343 if (sf) {
5344 gen_helper_rbit64(tcg_rd, tcg_rn);
5345 } else {
5346 TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
5347 tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
5348 gen_helper_rbit(tcg_tmp32, tcg_tmp32);
5349 tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
5350 tcg_temp_free_i32(tcg_tmp32);
5354 /* REV with sf==1, opcode==3 ("REV64") */
5355 static void handle_rev64(DisasContext *s, unsigned int sf,
5356 unsigned int rn, unsigned int rd)
5358 if (!sf) {
5359 unallocated_encoding(s);
5360 return;
5362 tcg_gen_bswap64_i64(cpu_reg(s, rd), cpu_reg(s, rn));
5365 /* REV with sf==0, opcode==2
5366 * REV32 (sf==1, opcode==2)
5368 static void handle_rev32(DisasContext *s, unsigned int sf,
5369 unsigned int rn, unsigned int rd)
5371 TCGv_i64 tcg_rd = cpu_reg(s, rd);
5373 if (sf) {
5374 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
5375 TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
5377 /* bswap32_i64 requires zero high word */
5378 tcg_gen_ext32u_i64(tcg_tmp, tcg_rn);
5379 tcg_gen_bswap32_i64(tcg_rd, tcg_tmp);
5380 tcg_gen_shri_i64(tcg_tmp, tcg_rn, 32);
5381 tcg_gen_bswap32_i64(tcg_tmp, tcg_tmp);
5382 tcg_gen_concat32_i64(tcg_rd, tcg_rd, tcg_tmp);
5384 tcg_temp_free_i64(tcg_tmp);
5385 } else {
5386 tcg_gen_ext32u_i64(tcg_rd, cpu_reg(s, rn));
5387 tcg_gen_bswap32_i64(tcg_rd, tcg_rd);
5391 /* REV16 (opcode==1) */
5392 static void handle_rev16(DisasContext *s, unsigned int sf,
5393 unsigned int rn, unsigned int rd)
5395 TCGv_i64 tcg_rd = cpu_reg(s, rd);
5396 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
5397 TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
5398 TCGv_i64 mask = tcg_const_i64(sf ? 0x00ff00ff00ff00ffull : 0x00ff00ff);
5400 tcg_gen_shri_i64(tcg_tmp, tcg_rn, 8);
5401 tcg_gen_and_i64(tcg_rd, tcg_rn, mask);
5402 tcg_gen_and_i64(tcg_tmp, tcg_tmp, mask);
5403 tcg_gen_shli_i64(tcg_rd, tcg_rd, 8);
5404 tcg_gen_or_i64(tcg_rd, tcg_rd, tcg_tmp);
5406 tcg_temp_free_i64(mask);
5407 tcg_temp_free_i64(tcg_tmp);
5410 /* Data-processing (1 source)
5411 * 31 30 29 28 21 20 16 15 10 9 5 4 0
5412 * +----+---+---+-----------------+---------+--------+------+------+
5413 * | sf | 1 | S | 1 1 0 1 0 1 1 0 | opcode2 | opcode | Rn | Rd |
5414 * +----+---+---+-----------------+---------+--------+------+------+
5416 static void disas_data_proc_1src(DisasContext *s, uint32_t insn)
5418 unsigned int sf, opcode, opcode2, rn, rd;
5419 TCGv_i64 tcg_rd;
5421 if (extract32(insn, 29, 1)) {
5422 unallocated_encoding(s);
5423 return;
5426 sf = extract32(insn, 31, 1);
5427 opcode = extract32(insn, 10, 6);
5428 opcode2 = extract32(insn, 16, 5);
5429 rn = extract32(insn, 5, 5);
5430 rd = extract32(insn, 0, 5);
5432 #define MAP(SF, O2, O1) ((SF) | (O1 << 1) | (O2 << 7))
5434 switch (MAP(sf, opcode2, opcode)) {
5435 case MAP(0, 0x00, 0x00): /* RBIT */
5436 case MAP(1, 0x00, 0x00):
5437 handle_rbit(s, sf, rn, rd);
5438 break;
5439 case MAP(0, 0x00, 0x01): /* REV16 */
5440 case MAP(1, 0x00, 0x01):
5441 handle_rev16(s, sf, rn, rd);
5442 break;
5443 case MAP(0, 0x00, 0x02): /* REV/REV32 */
5444 case MAP(1, 0x00, 0x02):
5445 handle_rev32(s, sf, rn, rd);
5446 break;
5447 case MAP(1, 0x00, 0x03): /* REV64 */
5448 handle_rev64(s, sf, rn, rd);
5449 break;
5450 case MAP(0, 0x00, 0x04): /* CLZ */
5451 case MAP(1, 0x00, 0x04):
5452 handle_clz(s, sf, rn, rd);
5453 break;
5454 case MAP(0, 0x00, 0x05): /* CLS */
5455 case MAP(1, 0x00, 0x05):
5456 handle_cls(s, sf, rn, rd);
5457 break;
5458 case MAP(1, 0x01, 0x00): /* PACIA */
5459 if (s->pauth_active) {
5460 tcg_rd = cpu_reg(s, rd);
5461 gen_helper_pacia(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5462 } else if (!dc_isar_feature(aa64_pauth, s)) {
5463 goto do_unallocated;
5465 break;
5466 case MAP(1, 0x01, 0x01): /* PACIB */
5467 if (s->pauth_active) {
5468 tcg_rd = cpu_reg(s, rd);
5469 gen_helper_pacib(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5470 } else if (!dc_isar_feature(aa64_pauth, s)) {
5471 goto do_unallocated;
5473 break;
5474 case MAP(1, 0x01, 0x02): /* PACDA */
5475 if (s->pauth_active) {
5476 tcg_rd = cpu_reg(s, rd);
5477 gen_helper_pacda(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5478 } else if (!dc_isar_feature(aa64_pauth, s)) {
5479 goto do_unallocated;
5481 break;
5482 case MAP(1, 0x01, 0x03): /* PACDB */
5483 if (s->pauth_active) {
5484 tcg_rd = cpu_reg(s, rd);
5485 gen_helper_pacdb(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5486 } else if (!dc_isar_feature(aa64_pauth, s)) {
5487 goto do_unallocated;
5489 break;
5490 case MAP(1, 0x01, 0x04): /* AUTIA */
5491 if (s->pauth_active) {
5492 tcg_rd = cpu_reg(s, rd);
5493 gen_helper_autia(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5494 } else if (!dc_isar_feature(aa64_pauth, s)) {
5495 goto do_unallocated;
5497 break;
5498 case MAP(1, 0x01, 0x05): /* AUTIB */
5499 if (s->pauth_active) {
5500 tcg_rd = cpu_reg(s, rd);
5501 gen_helper_autib(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5502 } else if (!dc_isar_feature(aa64_pauth, s)) {
5503 goto do_unallocated;
5505 break;
5506 case MAP(1, 0x01, 0x06): /* AUTDA */
5507 if (s->pauth_active) {
5508 tcg_rd = cpu_reg(s, rd);
5509 gen_helper_autda(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5510 } else if (!dc_isar_feature(aa64_pauth, s)) {
5511 goto do_unallocated;
5513 break;
5514 case MAP(1, 0x01, 0x07): /* AUTDB */
5515 if (s->pauth_active) {
5516 tcg_rd = cpu_reg(s, rd);
5517 gen_helper_autdb(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5518 } else if (!dc_isar_feature(aa64_pauth, s)) {
5519 goto do_unallocated;
5521 break;
5522 case MAP(1, 0x01, 0x08): /* PACIZA */
5523 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5524 goto do_unallocated;
5525 } else if (s->pauth_active) {
5526 tcg_rd = cpu_reg(s, rd);
5527 gen_helper_pacia(tcg_rd, cpu_env, tcg_rd, new_tmp_a64_zero(s));
5529 break;
5530 case MAP(1, 0x01, 0x09): /* PACIZB */
5531 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5532 goto do_unallocated;
5533 } else if (s->pauth_active) {
5534 tcg_rd = cpu_reg(s, rd);
5535 gen_helper_pacib(tcg_rd, cpu_env, tcg_rd, new_tmp_a64_zero(s));
5537 break;
5538 case MAP(1, 0x01, 0x0a): /* PACDZA */
5539 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5540 goto do_unallocated;
5541 } else if (s->pauth_active) {
5542 tcg_rd = cpu_reg(s, rd);
5543 gen_helper_pacda(tcg_rd, cpu_env, tcg_rd, new_tmp_a64_zero(s));
5545 break;
5546 case MAP(1, 0x01, 0x0b): /* PACDZB */
5547 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5548 goto do_unallocated;
5549 } else if (s->pauth_active) {
5550 tcg_rd = cpu_reg(s, rd);
5551 gen_helper_pacdb(tcg_rd, cpu_env, tcg_rd, new_tmp_a64_zero(s));
5553 break;
5554 case MAP(1, 0x01, 0x0c): /* AUTIZA */
5555 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5556 goto do_unallocated;
5557 } else if (s->pauth_active) {
5558 tcg_rd = cpu_reg(s, rd);
5559 gen_helper_autia(tcg_rd, cpu_env, tcg_rd, new_tmp_a64_zero(s));
5561 break;
5562 case MAP(1, 0x01, 0x0d): /* AUTIZB */
5563 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5564 goto do_unallocated;
5565 } else if (s->pauth_active) {
5566 tcg_rd = cpu_reg(s, rd);
5567 gen_helper_autib(tcg_rd, cpu_env, tcg_rd, new_tmp_a64_zero(s));
5569 break;
5570 case MAP(1, 0x01, 0x0e): /* AUTDZA */
5571 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5572 goto do_unallocated;
5573 } else if (s->pauth_active) {
5574 tcg_rd = cpu_reg(s, rd);
5575 gen_helper_autda(tcg_rd, cpu_env, tcg_rd, new_tmp_a64_zero(s));
5577 break;
5578 case MAP(1, 0x01, 0x0f): /* AUTDZB */
5579 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5580 goto do_unallocated;
5581 } else if (s->pauth_active) {
5582 tcg_rd = cpu_reg(s, rd);
5583 gen_helper_autdb(tcg_rd, cpu_env, tcg_rd, new_tmp_a64_zero(s));
5585 break;
5586 case MAP(1, 0x01, 0x10): /* XPACI */
5587 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5588 goto do_unallocated;
5589 } else if (s->pauth_active) {
5590 tcg_rd = cpu_reg(s, rd);
5591 gen_helper_xpaci(tcg_rd, cpu_env, tcg_rd);
5593 break;
5594 case MAP(1, 0x01, 0x11): /* XPACD */
5595 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5596 goto do_unallocated;
5597 } else if (s->pauth_active) {
5598 tcg_rd = cpu_reg(s, rd);
5599 gen_helper_xpacd(tcg_rd, cpu_env, tcg_rd);
5601 break;
5602 default:
5603 do_unallocated:
5604 unallocated_encoding(s);
5605 break;
5608 #undef MAP
5611 static void handle_div(DisasContext *s, bool is_signed, unsigned int sf,
5612 unsigned int rm, unsigned int rn, unsigned int rd)
5614 TCGv_i64 tcg_n, tcg_m, tcg_rd;
5615 tcg_rd = cpu_reg(s, rd);
5617 if (!sf && is_signed) {
5618 tcg_n = new_tmp_a64(s);
5619 tcg_m = new_tmp_a64(s);
5620 tcg_gen_ext32s_i64(tcg_n, cpu_reg(s, rn));
5621 tcg_gen_ext32s_i64(tcg_m, cpu_reg(s, rm));
5622 } else {
5623 tcg_n = read_cpu_reg(s, rn, sf);
5624 tcg_m = read_cpu_reg(s, rm, sf);
5627 if (is_signed) {
5628 gen_helper_sdiv64(tcg_rd, tcg_n, tcg_m);
5629 } else {
5630 gen_helper_udiv64(tcg_rd, tcg_n, tcg_m);
5633 if (!sf) { /* zero extend final result */
5634 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
5638 /* LSLV, LSRV, ASRV, RORV */
5639 static void handle_shift_reg(DisasContext *s,
5640 enum a64_shift_type shift_type, unsigned int sf,
5641 unsigned int rm, unsigned int rn, unsigned int rd)
5643 TCGv_i64 tcg_shift = tcg_temp_new_i64();
5644 TCGv_i64 tcg_rd = cpu_reg(s, rd);
5645 TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
5647 tcg_gen_andi_i64(tcg_shift, cpu_reg(s, rm), sf ? 63 : 31);
5648 shift_reg(tcg_rd, tcg_rn, sf, shift_type, tcg_shift);
5649 tcg_temp_free_i64(tcg_shift);
5652 /* CRC32[BHWX], CRC32C[BHWX] */
5653 static void handle_crc32(DisasContext *s,
5654 unsigned int sf, unsigned int sz, bool crc32c,
5655 unsigned int rm, unsigned int rn, unsigned int rd)
5657 TCGv_i64 tcg_acc, tcg_val;
5658 TCGv_i32 tcg_bytes;
5660 if (!dc_isar_feature(aa64_crc32, s)
5661 || (sf == 1 && sz != 3)
5662 || (sf == 0 && sz == 3)) {
5663 unallocated_encoding(s);
5664 return;
5667 if (sz == 3) {
5668 tcg_val = cpu_reg(s, rm);
5669 } else {
5670 uint64_t mask;
5671 switch (sz) {
5672 case 0:
5673 mask = 0xFF;
5674 break;
5675 case 1:
5676 mask = 0xFFFF;
5677 break;
5678 case 2:
5679 mask = 0xFFFFFFFF;
5680 break;
5681 default:
5682 g_assert_not_reached();
5684 tcg_val = new_tmp_a64(s);
5685 tcg_gen_andi_i64(tcg_val, cpu_reg(s, rm), mask);
5688 tcg_acc = cpu_reg(s, rn);
5689 tcg_bytes = tcg_const_i32(1 << sz);
5691 if (crc32c) {
5692 gen_helper_crc32c_64(cpu_reg(s, rd), tcg_acc, tcg_val, tcg_bytes);
5693 } else {
5694 gen_helper_crc32_64(cpu_reg(s, rd), tcg_acc, tcg_val, tcg_bytes);
5697 tcg_temp_free_i32(tcg_bytes);
5700 /* Data-processing (2 source)
5701 * 31 30 29 28 21 20 16 15 10 9 5 4 0
5702 * +----+---+---+-----------------+------+--------+------+------+
5703 * | sf | 0 | S | 1 1 0 1 0 1 1 0 | Rm | opcode | Rn | Rd |
5704 * +----+---+---+-----------------+------+--------+------+------+
5706 static void disas_data_proc_2src(DisasContext *s, uint32_t insn)
5708 unsigned int sf, rm, opcode, rn, rd, setflag;
5709 sf = extract32(insn, 31, 1);
5710 setflag = extract32(insn, 29, 1);
5711 rm = extract32(insn, 16, 5);
5712 opcode = extract32(insn, 10, 6);
5713 rn = extract32(insn, 5, 5);
5714 rd = extract32(insn, 0, 5);
5716 if (setflag && opcode != 0) {
5717 unallocated_encoding(s);
5718 return;
5721 switch (opcode) {
5722 case 0: /* SUBP(S) */
5723 if (sf == 0 || !dc_isar_feature(aa64_mte_insn_reg, s)) {
5724 goto do_unallocated;
5725 } else {
5726 TCGv_i64 tcg_n, tcg_m, tcg_d;
5728 tcg_n = read_cpu_reg_sp(s, rn, true);
5729 tcg_m = read_cpu_reg_sp(s, rm, true);
5730 tcg_gen_sextract_i64(tcg_n, tcg_n, 0, 56);
5731 tcg_gen_sextract_i64(tcg_m, tcg_m, 0, 56);
5732 tcg_d = cpu_reg(s, rd);
5734 if (setflag) {
5735 gen_sub_CC(true, tcg_d, tcg_n, tcg_m);
5736 } else {
5737 tcg_gen_sub_i64(tcg_d, tcg_n, tcg_m);
5740 break;
5741 case 2: /* UDIV */
5742 handle_div(s, false, sf, rm, rn, rd);
5743 break;
5744 case 3: /* SDIV */
5745 handle_div(s, true, sf, rm, rn, rd);
5746 break;
5747 case 4: /* IRG */
5748 if (sf == 0 || !dc_isar_feature(aa64_mte_insn_reg, s)) {
5749 goto do_unallocated;
5751 if (s->ata) {
5752 gen_helper_irg(cpu_reg_sp(s, rd), cpu_env,
5753 cpu_reg_sp(s, rn), cpu_reg(s, rm));
5754 } else {
5755 gen_address_with_allocation_tag0(cpu_reg_sp(s, rd),
5756 cpu_reg_sp(s, rn));
5758 break;
5759 case 5: /* GMI */
5760 if (sf == 0 || !dc_isar_feature(aa64_mte_insn_reg, s)) {
5761 goto do_unallocated;
5762 } else {
5763 TCGv_i64 t1 = tcg_const_i64(1);
5764 TCGv_i64 t2 = tcg_temp_new_i64();
5766 tcg_gen_extract_i64(t2, cpu_reg_sp(s, rn), 56, 4);
5767 tcg_gen_shl_i64(t1, t1, t2);
5768 tcg_gen_or_i64(cpu_reg(s, rd), cpu_reg(s, rm), t1);
5770 tcg_temp_free_i64(t1);
5771 tcg_temp_free_i64(t2);
5773 break;
5774 case 8: /* LSLV */
5775 handle_shift_reg(s, A64_SHIFT_TYPE_LSL, sf, rm, rn, rd);
5776 break;
5777 case 9: /* LSRV */
5778 handle_shift_reg(s, A64_SHIFT_TYPE_LSR, sf, rm, rn, rd);
5779 break;
5780 case 10: /* ASRV */
5781 handle_shift_reg(s, A64_SHIFT_TYPE_ASR, sf, rm, rn, rd);
5782 break;
5783 case 11: /* RORV */
5784 handle_shift_reg(s, A64_SHIFT_TYPE_ROR, sf, rm, rn, rd);
5785 break;
5786 case 12: /* PACGA */
5787 if (sf == 0 || !dc_isar_feature(aa64_pauth, s)) {
5788 goto do_unallocated;
5790 gen_helper_pacga(cpu_reg(s, rd), cpu_env,
5791 cpu_reg(s, rn), cpu_reg_sp(s, rm));
5792 break;
5793 case 16:
5794 case 17:
5795 case 18:
5796 case 19:
5797 case 20:
5798 case 21:
5799 case 22:
5800 case 23: /* CRC32 */
5802 int sz = extract32(opcode, 0, 2);
5803 bool crc32c = extract32(opcode, 2, 1);
5804 handle_crc32(s, sf, sz, crc32c, rm, rn, rd);
5805 break;
5807 default:
5808 do_unallocated:
5809 unallocated_encoding(s);
5810 break;
5815 * Data processing - register
5816 * 31 30 29 28 25 21 20 16 10 0
5817 * +--+---+--+---+-------+-----+-------+-------+---------+
5818 * | |op0| |op1| 1 0 1 | op2 | | op3 | |
5819 * +--+---+--+---+-------+-----+-------+-------+---------+
5821 static void disas_data_proc_reg(DisasContext *s, uint32_t insn)
5823 int op0 = extract32(insn, 30, 1);
5824 int op1 = extract32(insn, 28, 1);
5825 int op2 = extract32(insn, 21, 4);
5826 int op3 = extract32(insn, 10, 6);
5828 if (!op1) {
5829 if (op2 & 8) {
5830 if (op2 & 1) {
5831 /* Add/sub (extended register) */
5832 disas_add_sub_ext_reg(s, insn);
5833 } else {
5834 /* Add/sub (shifted register) */
5835 disas_add_sub_reg(s, insn);
5837 } else {
5838 /* Logical (shifted register) */
5839 disas_logic_reg(s, insn);
5841 return;
5844 switch (op2) {
5845 case 0x0:
5846 switch (op3) {
5847 case 0x00: /* Add/subtract (with carry) */
5848 disas_adc_sbc(s, insn);
5849 break;
5851 case 0x01: /* Rotate right into flags */
5852 case 0x21:
5853 disas_rotate_right_into_flags(s, insn);
5854 break;
5856 case 0x02: /* Evaluate into flags */
5857 case 0x12:
5858 case 0x22:
5859 case 0x32:
5860 disas_evaluate_into_flags(s, insn);
5861 break;
5863 default:
5864 goto do_unallocated;
5866 break;
5868 case 0x2: /* Conditional compare */
5869 disas_cc(s, insn); /* both imm and reg forms */
5870 break;
5872 case 0x4: /* Conditional select */
5873 disas_cond_select(s, insn);
5874 break;
5876 case 0x6: /* Data-processing */
5877 if (op0) { /* (1 source) */
5878 disas_data_proc_1src(s, insn);
5879 } else { /* (2 source) */
5880 disas_data_proc_2src(s, insn);
5882 break;
5883 case 0x8 ... 0xf: /* (3 source) */
5884 disas_data_proc_3src(s, insn);
5885 break;
5887 default:
5888 do_unallocated:
5889 unallocated_encoding(s);
5890 break;
5894 static void handle_fp_compare(DisasContext *s, int size,
5895 unsigned int rn, unsigned int rm,
5896 bool cmp_with_zero, bool signal_all_nans)
5898 TCGv_i64 tcg_flags = tcg_temp_new_i64();
5899 TCGv_ptr fpst = get_fpstatus_ptr(size == MO_16);
5901 if (size == MO_64) {
5902 TCGv_i64 tcg_vn, tcg_vm;
5904 tcg_vn = read_fp_dreg(s, rn);
5905 if (cmp_with_zero) {
5906 tcg_vm = tcg_const_i64(0);
5907 } else {
5908 tcg_vm = read_fp_dreg(s, rm);
5910 if (signal_all_nans) {
5911 gen_helper_vfp_cmped_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5912 } else {
5913 gen_helper_vfp_cmpd_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5915 tcg_temp_free_i64(tcg_vn);
5916 tcg_temp_free_i64(tcg_vm);
5917 } else {
5918 TCGv_i32 tcg_vn = tcg_temp_new_i32();
5919 TCGv_i32 tcg_vm = tcg_temp_new_i32();
5921 read_vec_element_i32(s, tcg_vn, rn, 0, size);
5922 if (cmp_with_zero) {
5923 tcg_gen_movi_i32(tcg_vm, 0);
5924 } else {
5925 read_vec_element_i32(s, tcg_vm, rm, 0, size);
5928 switch (size) {
5929 case MO_32:
5930 if (signal_all_nans) {
5931 gen_helper_vfp_cmpes_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5932 } else {
5933 gen_helper_vfp_cmps_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5935 break;
5936 case MO_16:
5937 if (signal_all_nans) {
5938 gen_helper_vfp_cmpeh_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5939 } else {
5940 gen_helper_vfp_cmph_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5942 break;
5943 default:
5944 g_assert_not_reached();
5947 tcg_temp_free_i32(tcg_vn);
5948 tcg_temp_free_i32(tcg_vm);
5951 tcg_temp_free_ptr(fpst);
5953 gen_set_nzcv(tcg_flags);
5955 tcg_temp_free_i64(tcg_flags);
5958 /* Floating point compare
5959 * 31 30 29 28 24 23 22 21 20 16 15 14 13 10 9 5 4 0
5960 * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
5961 * | M | 0 | S | 1 1 1 1 0 | type | 1 | Rm | op | 1 0 0 0 | Rn | op2 |
5962 * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
5964 static void disas_fp_compare(DisasContext *s, uint32_t insn)
5966 unsigned int mos, type, rm, op, rn, opc, op2r;
5967 int size;
5969 mos = extract32(insn, 29, 3);
5970 type = extract32(insn, 22, 2);
5971 rm = extract32(insn, 16, 5);
5972 op = extract32(insn, 14, 2);
5973 rn = extract32(insn, 5, 5);
5974 opc = extract32(insn, 3, 2);
5975 op2r = extract32(insn, 0, 3);
5977 if (mos || op || op2r) {
5978 unallocated_encoding(s);
5979 return;
5982 switch (type) {
5983 case 0:
5984 size = MO_32;
5985 break;
5986 case 1:
5987 size = MO_64;
5988 break;
5989 case 3:
5990 size = MO_16;
5991 if (dc_isar_feature(aa64_fp16, s)) {
5992 break;
5994 /* fallthru */
5995 default:
5996 unallocated_encoding(s);
5997 return;
6000 if (!fp_access_check(s)) {
6001 return;
6004 handle_fp_compare(s, size, rn, rm, opc & 1, opc & 2);
6007 /* Floating point conditional compare
6008 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 3 0
6009 * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
6010 * | M | 0 | S | 1 1 1 1 0 | type | 1 | Rm | cond | 0 1 | Rn | op | nzcv |
6011 * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
6013 static void disas_fp_ccomp(DisasContext *s, uint32_t insn)
6015 unsigned int mos, type, rm, cond, rn, op, nzcv;
6016 TCGv_i64 tcg_flags;
6017 TCGLabel *label_continue = NULL;
6018 int size;
6020 mos = extract32(insn, 29, 3);
6021 type = extract32(insn, 22, 2);
6022 rm = extract32(insn, 16, 5);
6023 cond = extract32(insn, 12, 4);
6024 rn = extract32(insn, 5, 5);
6025 op = extract32(insn, 4, 1);
6026 nzcv = extract32(insn, 0, 4);
6028 if (mos) {
6029 unallocated_encoding(s);
6030 return;
6033 switch (type) {
6034 case 0:
6035 size = MO_32;
6036 break;
6037 case 1:
6038 size = MO_64;
6039 break;
6040 case 3:
6041 size = MO_16;
6042 if (dc_isar_feature(aa64_fp16, s)) {
6043 break;
6045 /* fallthru */
6046 default:
6047 unallocated_encoding(s);
6048 return;
6051 if (!fp_access_check(s)) {
6052 return;
6055 if (cond < 0x0e) { /* not always */
6056 TCGLabel *label_match = gen_new_label();
6057 label_continue = gen_new_label();
6058 arm_gen_test_cc(cond, label_match);
6059 /* nomatch: */
6060 tcg_flags = tcg_const_i64(nzcv << 28);
6061 gen_set_nzcv(tcg_flags);
6062 tcg_temp_free_i64(tcg_flags);
6063 tcg_gen_br(label_continue);
6064 gen_set_label(label_match);
6067 handle_fp_compare(s, size, rn, rm, false, op);
6069 if (cond < 0x0e) {
6070 gen_set_label(label_continue);
6074 /* Floating point conditional select
6075 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 0
6076 * +---+---+---+-----------+------+---+------+------+-----+------+------+
6077 * | M | 0 | S | 1 1 1 1 0 | type | 1 | Rm | cond | 1 1 | Rn | Rd |
6078 * +---+---+---+-----------+------+---+------+------+-----+------+------+
6080 static void disas_fp_csel(DisasContext *s, uint32_t insn)
6082 unsigned int mos, type, rm, cond, rn, rd;
6083 TCGv_i64 t_true, t_false, t_zero;
6084 DisasCompare64 c;
6085 MemOp sz;
6087 mos = extract32(insn, 29, 3);
6088 type = extract32(insn, 22, 2);
6089 rm = extract32(insn, 16, 5);
6090 cond = extract32(insn, 12, 4);
6091 rn = extract32(insn, 5, 5);
6092 rd = extract32(insn, 0, 5);
6094 if (mos) {
6095 unallocated_encoding(s);
6096 return;
6099 switch (type) {
6100 case 0:
6101 sz = MO_32;
6102 break;
6103 case 1:
6104 sz = MO_64;
6105 break;
6106 case 3:
6107 sz = MO_16;
6108 if (dc_isar_feature(aa64_fp16, s)) {
6109 break;
6111 /* fallthru */
6112 default:
6113 unallocated_encoding(s);
6114 return;
6117 if (!fp_access_check(s)) {
6118 return;
6121 /* Zero extend sreg & hreg inputs to 64 bits now. */
6122 t_true = tcg_temp_new_i64();
6123 t_false = tcg_temp_new_i64();
6124 read_vec_element(s, t_true, rn, 0, sz);
6125 read_vec_element(s, t_false, rm, 0, sz);
6127 a64_test_cc(&c, cond);
6128 t_zero = tcg_const_i64(0);
6129 tcg_gen_movcond_i64(c.cond, t_true, c.value, t_zero, t_true, t_false);
6130 tcg_temp_free_i64(t_zero);
6131 tcg_temp_free_i64(t_false);
6132 a64_free_cc(&c);
6134 /* Note that sregs & hregs write back zeros to the high bits,
6135 and we've already done the zero-extension. */
6136 write_fp_dreg(s, rd, t_true);
6137 tcg_temp_free_i64(t_true);
6140 /* Floating-point data-processing (1 source) - half precision */
6141 static void handle_fp_1src_half(DisasContext *s, int opcode, int rd, int rn)
6143 TCGv_ptr fpst = NULL;
6144 TCGv_i32 tcg_op = read_fp_hreg(s, rn);
6145 TCGv_i32 tcg_res = tcg_temp_new_i32();
6147 switch (opcode) {
6148 case 0x0: /* FMOV */
6149 tcg_gen_mov_i32(tcg_res, tcg_op);
6150 break;
6151 case 0x1: /* FABS */
6152 tcg_gen_andi_i32(tcg_res, tcg_op, 0x7fff);
6153 break;
6154 case 0x2: /* FNEG */
6155 tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
6156 break;
6157 case 0x3: /* FSQRT */
6158 fpst = get_fpstatus_ptr(true);
6159 gen_helper_sqrt_f16(tcg_res, tcg_op, fpst);
6160 break;
6161 case 0x8: /* FRINTN */
6162 case 0x9: /* FRINTP */
6163 case 0xa: /* FRINTM */
6164 case 0xb: /* FRINTZ */
6165 case 0xc: /* FRINTA */
6167 TCGv_i32 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(opcode & 7));
6168 fpst = get_fpstatus_ptr(true);
6170 gen_helper_set_rmode(tcg_rmode, tcg_rmode, fpst);
6171 gen_helper_advsimd_rinth(tcg_res, tcg_op, fpst);
6173 gen_helper_set_rmode(tcg_rmode, tcg_rmode, fpst);
6174 tcg_temp_free_i32(tcg_rmode);
6175 break;
6177 case 0xe: /* FRINTX */
6178 fpst = get_fpstatus_ptr(true);
6179 gen_helper_advsimd_rinth_exact(tcg_res, tcg_op, fpst);
6180 break;
6181 case 0xf: /* FRINTI */
6182 fpst = get_fpstatus_ptr(true);
6183 gen_helper_advsimd_rinth(tcg_res, tcg_op, fpst);
6184 break;
6185 default:
6186 abort();
6189 write_fp_sreg(s, rd, tcg_res);
6191 if (fpst) {
6192 tcg_temp_free_ptr(fpst);
6194 tcg_temp_free_i32(tcg_op);
6195 tcg_temp_free_i32(tcg_res);
6198 /* Floating-point data-processing (1 source) - single precision */
6199 static void handle_fp_1src_single(DisasContext *s, int opcode, int rd, int rn)
6201 void (*gen_fpst)(TCGv_i32, TCGv_i32, TCGv_ptr);
6202 TCGv_i32 tcg_op, tcg_res;
6203 TCGv_ptr fpst;
6204 int rmode = -1;
6206 tcg_op = read_fp_sreg(s, rn);
6207 tcg_res = tcg_temp_new_i32();
6209 switch (opcode) {
6210 case 0x0: /* FMOV */
6211 tcg_gen_mov_i32(tcg_res, tcg_op);
6212 goto done;
6213 case 0x1: /* FABS */
6214 gen_helper_vfp_abss(tcg_res, tcg_op);
6215 goto done;
6216 case 0x2: /* FNEG */
6217 gen_helper_vfp_negs(tcg_res, tcg_op);
6218 goto done;
6219 case 0x3: /* FSQRT */
6220 gen_helper_vfp_sqrts(tcg_res, tcg_op, cpu_env);
6221 goto done;
6222 case 0x8: /* FRINTN */
6223 case 0x9: /* FRINTP */
6224 case 0xa: /* FRINTM */
6225 case 0xb: /* FRINTZ */
6226 case 0xc: /* FRINTA */
6227 rmode = arm_rmode_to_sf(opcode & 7);
6228 gen_fpst = gen_helper_rints;
6229 break;
6230 case 0xe: /* FRINTX */
6231 gen_fpst = gen_helper_rints_exact;
6232 break;
6233 case 0xf: /* FRINTI */
6234 gen_fpst = gen_helper_rints;
6235 break;
6236 case 0x10: /* FRINT32Z */
6237 rmode = float_round_to_zero;
6238 gen_fpst = gen_helper_frint32_s;
6239 break;
6240 case 0x11: /* FRINT32X */
6241 gen_fpst = gen_helper_frint32_s;
6242 break;
6243 case 0x12: /* FRINT64Z */
6244 rmode = float_round_to_zero;
6245 gen_fpst = gen_helper_frint64_s;
6246 break;
6247 case 0x13: /* FRINT64X */
6248 gen_fpst = gen_helper_frint64_s;
6249 break;
6250 default:
6251 g_assert_not_reached();
6254 fpst = get_fpstatus_ptr(false);
6255 if (rmode >= 0) {
6256 TCGv_i32 tcg_rmode = tcg_const_i32(rmode);
6257 gen_helper_set_rmode(tcg_rmode, tcg_rmode, fpst);
6258 gen_fpst(tcg_res, tcg_op, fpst);
6259 gen_helper_set_rmode(tcg_rmode, tcg_rmode, fpst);
6260 tcg_temp_free_i32(tcg_rmode);
6261 } else {
6262 gen_fpst(tcg_res, tcg_op, fpst);
6264 tcg_temp_free_ptr(fpst);
6266 done:
6267 write_fp_sreg(s, rd, tcg_res);
6268 tcg_temp_free_i32(tcg_op);
6269 tcg_temp_free_i32(tcg_res);
6272 /* Floating-point data-processing (1 source) - double precision */
6273 static void handle_fp_1src_double(DisasContext *s, int opcode, int rd, int rn)
6275 void (*gen_fpst)(TCGv_i64, TCGv_i64, TCGv_ptr);
6276 TCGv_i64 tcg_op, tcg_res;
6277 TCGv_ptr fpst;
6278 int rmode = -1;
6280 switch (opcode) {
6281 case 0x0: /* FMOV */
6282 gen_gvec_fn2(s, false, rd, rn, tcg_gen_gvec_mov, 0);
6283 return;
6286 tcg_op = read_fp_dreg(s, rn);
6287 tcg_res = tcg_temp_new_i64();
6289 switch (opcode) {
6290 case 0x1: /* FABS */
6291 gen_helper_vfp_absd(tcg_res, tcg_op);
6292 goto done;
6293 case 0x2: /* FNEG */
6294 gen_helper_vfp_negd(tcg_res, tcg_op);
6295 goto done;
6296 case 0x3: /* FSQRT */
6297 gen_helper_vfp_sqrtd(tcg_res, tcg_op, cpu_env);
6298 goto done;
6299 case 0x8: /* FRINTN */
6300 case 0x9: /* FRINTP */
6301 case 0xa: /* FRINTM */
6302 case 0xb: /* FRINTZ */
6303 case 0xc: /* FRINTA */
6304 rmode = arm_rmode_to_sf(opcode & 7);
6305 gen_fpst = gen_helper_rintd;
6306 break;
6307 case 0xe: /* FRINTX */
6308 gen_fpst = gen_helper_rintd_exact;
6309 break;
6310 case 0xf: /* FRINTI */
6311 gen_fpst = gen_helper_rintd;
6312 break;
6313 case 0x10: /* FRINT32Z */
6314 rmode = float_round_to_zero;
6315 gen_fpst = gen_helper_frint32_d;
6316 break;
6317 case 0x11: /* FRINT32X */
6318 gen_fpst = gen_helper_frint32_d;
6319 break;
6320 case 0x12: /* FRINT64Z */
6321 rmode = float_round_to_zero;
6322 gen_fpst = gen_helper_frint64_d;
6323 break;
6324 case 0x13: /* FRINT64X */
6325 gen_fpst = gen_helper_frint64_d;
6326 break;
6327 default:
6328 g_assert_not_reached();
6331 fpst = get_fpstatus_ptr(false);
6332 if (rmode >= 0) {
6333 TCGv_i32 tcg_rmode = tcg_const_i32(rmode);
6334 gen_helper_set_rmode(tcg_rmode, tcg_rmode, fpst);
6335 gen_fpst(tcg_res, tcg_op, fpst);
6336 gen_helper_set_rmode(tcg_rmode, tcg_rmode, fpst);
6337 tcg_temp_free_i32(tcg_rmode);
6338 } else {
6339 gen_fpst(tcg_res, tcg_op, fpst);
6341 tcg_temp_free_ptr(fpst);
6343 done:
6344 write_fp_dreg(s, rd, tcg_res);
6345 tcg_temp_free_i64(tcg_op);
6346 tcg_temp_free_i64(tcg_res);
6349 static void handle_fp_fcvt(DisasContext *s, int opcode,
6350 int rd, int rn, int dtype, int ntype)
6352 switch (ntype) {
6353 case 0x0:
6355 TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
6356 if (dtype == 1) {
6357 /* Single to double */
6358 TCGv_i64 tcg_rd = tcg_temp_new_i64();
6359 gen_helper_vfp_fcvtds(tcg_rd, tcg_rn, cpu_env);
6360 write_fp_dreg(s, rd, tcg_rd);
6361 tcg_temp_free_i64(tcg_rd);
6362 } else {
6363 /* Single to half */
6364 TCGv_i32 tcg_rd = tcg_temp_new_i32();
6365 TCGv_i32 ahp = get_ahp_flag();
6366 TCGv_ptr fpst = get_fpstatus_ptr(false);
6368 gen_helper_vfp_fcvt_f32_to_f16(tcg_rd, tcg_rn, fpst, ahp);
6369 /* write_fp_sreg is OK here because top half of tcg_rd is zero */
6370 write_fp_sreg(s, rd, tcg_rd);
6371 tcg_temp_free_i32(tcg_rd);
6372 tcg_temp_free_i32(ahp);
6373 tcg_temp_free_ptr(fpst);
6375 tcg_temp_free_i32(tcg_rn);
6376 break;
6378 case 0x1:
6380 TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
6381 TCGv_i32 tcg_rd = tcg_temp_new_i32();
6382 if (dtype == 0) {
6383 /* Double to single */
6384 gen_helper_vfp_fcvtsd(tcg_rd, tcg_rn, cpu_env);
6385 } else {
6386 TCGv_ptr fpst = get_fpstatus_ptr(false);
6387 TCGv_i32 ahp = get_ahp_flag();
6388 /* Double to half */
6389 gen_helper_vfp_fcvt_f64_to_f16(tcg_rd, tcg_rn, fpst, ahp);
6390 /* write_fp_sreg is OK here because top half of tcg_rd is zero */
6391 tcg_temp_free_ptr(fpst);
6392 tcg_temp_free_i32(ahp);
6394 write_fp_sreg(s, rd, tcg_rd);
6395 tcg_temp_free_i32(tcg_rd);
6396 tcg_temp_free_i64(tcg_rn);
6397 break;
6399 case 0x3:
6401 TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
6402 TCGv_ptr tcg_fpst = get_fpstatus_ptr(false);
6403 TCGv_i32 tcg_ahp = get_ahp_flag();
6404 tcg_gen_ext16u_i32(tcg_rn, tcg_rn);
6405 if (dtype == 0) {
6406 /* Half to single */
6407 TCGv_i32 tcg_rd = tcg_temp_new_i32();
6408 gen_helper_vfp_fcvt_f16_to_f32(tcg_rd, tcg_rn, tcg_fpst, tcg_ahp);
6409 write_fp_sreg(s, rd, tcg_rd);
6410 tcg_temp_free_i32(tcg_rd);
6411 } else {
6412 /* Half to double */
6413 TCGv_i64 tcg_rd = tcg_temp_new_i64();
6414 gen_helper_vfp_fcvt_f16_to_f64(tcg_rd, tcg_rn, tcg_fpst, tcg_ahp);
6415 write_fp_dreg(s, rd, tcg_rd);
6416 tcg_temp_free_i64(tcg_rd);
6418 tcg_temp_free_i32(tcg_rn);
6419 tcg_temp_free_ptr(tcg_fpst);
6420 tcg_temp_free_i32(tcg_ahp);
6421 break;
6423 default:
6424 abort();
6428 /* Floating point data-processing (1 source)
6429 * 31 30 29 28 24 23 22 21 20 15 14 10 9 5 4 0
6430 * +---+---+---+-----------+------+---+--------+-----------+------+------+
6431 * | M | 0 | S | 1 1 1 1 0 | type | 1 | opcode | 1 0 0 0 0 | Rn | Rd |
6432 * +---+---+---+-----------+------+---+--------+-----------+------+------+
6434 static void disas_fp_1src(DisasContext *s, uint32_t insn)
6436 int mos = extract32(insn, 29, 3);
6437 int type = extract32(insn, 22, 2);
6438 int opcode = extract32(insn, 15, 6);
6439 int rn = extract32(insn, 5, 5);
6440 int rd = extract32(insn, 0, 5);
6442 if (mos) {
6443 unallocated_encoding(s);
6444 return;
6447 switch (opcode) {
6448 case 0x4: case 0x5: case 0x7:
6450 /* FCVT between half, single and double precision */
6451 int dtype = extract32(opcode, 0, 2);
6452 if (type == 2 || dtype == type) {
6453 unallocated_encoding(s);
6454 return;
6456 if (!fp_access_check(s)) {
6457 return;
6460 handle_fp_fcvt(s, opcode, rd, rn, dtype, type);
6461 break;
6464 case 0x10 ... 0x13: /* FRINT{32,64}{X,Z} */
6465 if (type > 1 || !dc_isar_feature(aa64_frint, s)) {
6466 unallocated_encoding(s);
6467 return;
6469 /* fall through */
6470 case 0x0 ... 0x3:
6471 case 0x8 ... 0xc:
6472 case 0xe ... 0xf:
6473 /* 32-to-32 and 64-to-64 ops */
6474 switch (type) {
6475 case 0:
6476 if (!fp_access_check(s)) {
6477 return;
6479 handle_fp_1src_single(s, opcode, rd, rn);
6480 break;
6481 case 1:
6482 if (!fp_access_check(s)) {
6483 return;
6485 handle_fp_1src_double(s, opcode, rd, rn);
6486 break;
6487 case 3:
6488 if (!dc_isar_feature(aa64_fp16, s)) {
6489 unallocated_encoding(s);
6490 return;
6493 if (!fp_access_check(s)) {
6494 return;
6496 handle_fp_1src_half(s, opcode, rd, rn);
6497 break;
6498 default:
6499 unallocated_encoding(s);
6501 break;
6503 default:
6504 unallocated_encoding(s);
6505 break;
6509 /* Floating-point data-processing (2 source) - single precision */
6510 static void handle_fp_2src_single(DisasContext *s, int opcode,
6511 int rd, int rn, int rm)
6513 TCGv_i32 tcg_op1;
6514 TCGv_i32 tcg_op2;
6515 TCGv_i32 tcg_res;
6516 TCGv_ptr fpst;
6518 tcg_res = tcg_temp_new_i32();
6519 fpst = get_fpstatus_ptr(false);
6520 tcg_op1 = read_fp_sreg(s, rn);
6521 tcg_op2 = read_fp_sreg(s, rm);
6523 switch (opcode) {
6524 case 0x0: /* FMUL */
6525 gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
6526 break;
6527 case 0x1: /* FDIV */
6528 gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
6529 break;
6530 case 0x2: /* FADD */
6531 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
6532 break;
6533 case 0x3: /* FSUB */
6534 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
6535 break;
6536 case 0x4: /* FMAX */
6537 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
6538 break;
6539 case 0x5: /* FMIN */
6540 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
6541 break;
6542 case 0x6: /* FMAXNM */
6543 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
6544 break;
6545 case 0x7: /* FMINNM */
6546 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
6547 break;
6548 case 0x8: /* FNMUL */
6549 gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
6550 gen_helper_vfp_negs(tcg_res, tcg_res);
6551 break;
6554 write_fp_sreg(s, rd, tcg_res);
6556 tcg_temp_free_ptr(fpst);
6557 tcg_temp_free_i32(tcg_op1);
6558 tcg_temp_free_i32(tcg_op2);
6559 tcg_temp_free_i32(tcg_res);
6562 /* Floating-point data-processing (2 source) - double precision */
6563 static void handle_fp_2src_double(DisasContext *s, int opcode,
6564 int rd, int rn, int rm)
6566 TCGv_i64 tcg_op1;
6567 TCGv_i64 tcg_op2;
6568 TCGv_i64 tcg_res;
6569 TCGv_ptr fpst;
6571 tcg_res = tcg_temp_new_i64();
6572 fpst = get_fpstatus_ptr(false);
6573 tcg_op1 = read_fp_dreg(s, rn);
6574 tcg_op2 = read_fp_dreg(s, rm);
6576 switch (opcode) {
6577 case 0x0: /* FMUL */
6578 gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
6579 break;
6580 case 0x1: /* FDIV */
6581 gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
6582 break;
6583 case 0x2: /* FADD */
6584 gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
6585 break;
6586 case 0x3: /* FSUB */
6587 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
6588 break;
6589 case 0x4: /* FMAX */
6590 gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
6591 break;
6592 case 0x5: /* FMIN */
6593 gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
6594 break;
6595 case 0x6: /* FMAXNM */
6596 gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
6597 break;
6598 case 0x7: /* FMINNM */
6599 gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
6600 break;
6601 case 0x8: /* FNMUL */
6602 gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
6603 gen_helper_vfp_negd(tcg_res, tcg_res);
6604 break;
6607 write_fp_dreg(s, rd, tcg_res);
6609 tcg_temp_free_ptr(fpst);
6610 tcg_temp_free_i64(tcg_op1);
6611 tcg_temp_free_i64(tcg_op2);
6612 tcg_temp_free_i64(tcg_res);
6615 /* Floating-point data-processing (2 source) - half precision */
6616 static void handle_fp_2src_half(DisasContext *s, int opcode,
6617 int rd, int rn, int rm)
6619 TCGv_i32 tcg_op1;
6620 TCGv_i32 tcg_op2;
6621 TCGv_i32 tcg_res;
6622 TCGv_ptr fpst;
6624 tcg_res = tcg_temp_new_i32();
6625 fpst = get_fpstatus_ptr(true);
6626 tcg_op1 = read_fp_hreg(s, rn);
6627 tcg_op2 = read_fp_hreg(s, rm);
6629 switch (opcode) {
6630 case 0x0: /* FMUL */
6631 gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
6632 break;
6633 case 0x1: /* FDIV */
6634 gen_helper_advsimd_divh(tcg_res, tcg_op1, tcg_op2, fpst);
6635 break;
6636 case 0x2: /* FADD */
6637 gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
6638 break;
6639 case 0x3: /* FSUB */
6640 gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
6641 break;
6642 case 0x4: /* FMAX */
6643 gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
6644 break;
6645 case 0x5: /* FMIN */
6646 gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
6647 break;
6648 case 0x6: /* FMAXNM */
6649 gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
6650 break;
6651 case 0x7: /* FMINNM */
6652 gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
6653 break;
6654 case 0x8: /* FNMUL */
6655 gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
6656 tcg_gen_xori_i32(tcg_res, tcg_res, 0x8000);
6657 break;
6658 default:
6659 g_assert_not_reached();
6662 write_fp_sreg(s, rd, tcg_res);
6664 tcg_temp_free_ptr(fpst);
6665 tcg_temp_free_i32(tcg_op1);
6666 tcg_temp_free_i32(tcg_op2);
6667 tcg_temp_free_i32(tcg_res);
6670 /* Floating point data-processing (2 source)
6671 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 0
6672 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
6673 * | M | 0 | S | 1 1 1 1 0 | type | 1 | Rm | opcode | 1 0 | Rn | Rd |
6674 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
6676 static void disas_fp_2src(DisasContext *s, uint32_t insn)
6678 int mos = extract32(insn, 29, 3);
6679 int type = extract32(insn, 22, 2);
6680 int rd = extract32(insn, 0, 5);
6681 int rn = extract32(insn, 5, 5);
6682 int rm = extract32(insn, 16, 5);
6683 int opcode = extract32(insn, 12, 4);
6685 if (opcode > 8 || mos) {
6686 unallocated_encoding(s);
6687 return;
6690 switch (type) {
6691 case 0:
6692 if (!fp_access_check(s)) {
6693 return;
6695 handle_fp_2src_single(s, opcode, rd, rn, rm);
6696 break;
6697 case 1:
6698 if (!fp_access_check(s)) {
6699 return;
6701 handle_fp_2src_double(s, opcode, rd, rn, rm);
6702 break;
6703 case 3:
6704 if (!dc_isar_feature(aa64_fp16, s)) {
6705 unallocated_encoding(s);
6706 return;
6708 if (!fp_access_check(s)) {
6709 return;
6711 handle_fp_2src_half(s, opcode, rd, rn, rm);
6712 break;
6713 default:
6714 unallocated_encoding(s);
6718 /* Floating-point data-processing (3 source) - single precision */
6719 static void handle_fp_3src_single(DisasContext *s, bool o0, bool o1,
6720 int rd, int rn, int rm, int ra)
6722 TCGv_i32 tcg_op1, tcg_op2, tcg_op3;
6723 TCGv_i32 tcg_res = tcg_temp_new_i32();
6724 TCGv_ptr fpst = get_fpstatus_ptr(false);
6726 tcg_op1 = read_fp_sreg(s, rn);
6727 tcg_op2 = read_fp_sreg(s, rm);
6728 tcg_op3 = read_fp_sreg(s, ra);
6730 /* These are fused multiply-add, and must be done as one
6731 * floating point operation with no rounding between the
6732 * multiplication and addition steps.
6733 * NB that doing the negations here as separate steps is
6734 * correct : an input NaN should come out with its sign bit
6735 * flipped if it is a negated-input.
6737 if (o1 == true) {
6738 gen_helper_vfp_negs(tcg_op3, tcg_op3);
6741 if (o0 != o1) {
6742 gen_helper_vfp_negs(tcg_op1, tcg_op1);
6745 gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
6747 write_fp_sreg(s, rd, tcg_res);
6749 tcg_temp_free_ptr(fpst);
6750 tcg_temp_free_i32(tcg_op1);
6751 tcg_temp_free_i32(tcg_op2);
6752 tcg_temp_free_i32(tcg_op3);
6753 tcg_temp_free_i32(tcg_res);
6756 /* Floating-point data-processing (3 source) - double precision */
6757 static void handle_fp_3src_double(DisasContext *s, bool o0, bool o1,
6758 int rd, int rn, int rm, int ra)
6760 TCGv_i64 tcg_op1, tcg_op2, tcg_op3;
6761 TCGv_i64 tcg_res = tcg_temp_new_i64();
6762 TCGv_ptr fpst = get_fpstatus_ptr(false);
6764 tcg_op1 = read_fp_dreg(s, rn);
6765 tcg_op2 = read_fp_dreg(s, rm);
6766 tcg_op3 = read_fp_dreg(s, ra);
6768 /* These are fused multiply-add, and must be done as one
6769 * floating point operation with no rounding between the
6770 * multiplication and addition steps.
6771 * NB that doing the negations here as separate steps is
6772 * correct : an input NaN should come out with its sign bit
6773 * flipped if it is a negated-input.
6775 if (o1 == true) {
6776 gen_helper_vfp_negd(tcg_op3, tcg_op3);
6779 if (o0 != o1) {
6780 gen_helper_vfp_negd(tcg_op1, tcg_op1);
6783 gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
6785 write_fp_dreg(s, rd, tcg_res);
6787 tcg_temp_free_ptr(fpst);
6788 tcg_temp_free_i64(tcg_op1);
6789 tcg_temp_free_i64(tcg_op2);
6790 tcg_temp_free_i64(tcg_op3);
6791 tcg_temp_free_i64(tcg_res);
6794 /* Floating-point data-processing (3 source) - half precision */
6795 static void handle_fp_3src_half(DisasContext *s, bool o0, bool o1,
6796 int rd, int rn, int rm, int ra)
6798 TCGv_i32 tcg_op1, tcg_op2, tcg_op3;
6799 TCGv_i32 tcg_res = tcg_temp_new_i32();
6800 TCGv_ptr fpst = get_fpstatus_ptr(true);
6802 tcg_op1 = read_fp_hreg(s, rn);
6803 tcg_op2 = read_fp_hreg(s, rm);
6804 tcg_op3 = read_fp_hreg(s, ra);
6806 /* These are fused multiply-add, and must be done as one
6807 * floating point operation with no rounding between the
6808 * multiplication and addition steps.
6809 * NB that doing the negations here as separate steps is
6810 * correct : an input NaN should come out with its sign bit
6811 * flipped if it is a negated-input.
6813 if (o1 == true) {
6814 tcg_gen_xori_i32(tcg_op3, tcg_op3, 0x8000);
6817 if (o0 != o1) {
6818 tcg_gen_xori_i32(tcg_op1, tcg_op1, 0x8000);
6821 gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
6823 write_fp_sreg(s, rd, tcg_res);
6825 tcg_temp_free_ptr(fpst);
6826 tcg_temp_free_i32(tcg_op1);
6827 tcg_temp_free_i32(tcg_op2);
6828 tcg_temp_free_i32(tcg_op3);
6829 tcg_temp_free_i32(tcg_res);
6832 /* Floating point data-processing (3 source)
6833 * 31 30 29 28 24 23 22 21 20 16 15 14 10 9 5 4 0
6834 * +---+---+---+-----------+------+----+------+----+------+------+------+
6835 * | M | 0 | S | 1 1 1 1 1 | type | o1 | Rm | o0 | Ra | Rn | Rd |
6836 * +---+---+---+-----------+------+----+------+----+------+------+------+
6838 static void disas_fp_3src(DisasContext *s, uint32_t insn)
6840 int mos = extract32(insn, 29, 3);
6841 int type = extract32(insn, 22, 2);
6842 int rd = extract32(insn, 0, 5);
6843 int rn = extract32(insn, 5, 5);
6844 int ra = extract32(insn, 10, 5);
6845 int rm = extract32(insn, 16, 5);
6846 bool o0 = extract32(insn, 15, 1);
6847 bool o1 = extract32(insn, 21, 1);
6849 if (mos) {
6850 unallocated_encoding(s);
6851 return;
6854 switch (type) {
6855 case 0:
6856 if (!fp_access_check(s)) {
6857 return;
6859 handle_fp_3src_single(s, o0, o1, rd, rn, rm, ra);
6860 break;
6861 case 1:
6862 if (!fp_access_check(s)) {
6863 return;
6865 handle_fp_3src_double(s, o0, o1, rd, rn, rm, ra);
6866 break;
6867 case 3:
6868 if (!dc_isar_feature(aa64_fp16, s)) {
6869 unallocated_encoding(s);
6870 return;
6872 if (!fp_access_check(s)) {
6873 return;
6875 handle_fp_3src_half(s, o0, o1, rd, rn, rm, ra);
6876 break;
6877 default:
6878 unallocated_encoding(s);
6882 /* Floating point immediate
6883 * 31 30 29 28 24 23 22 21 20 13 12 10 9 5 4 0
6884 * +---+---+---+-----------+------+---+------------+-------+------+------+
6885 * | M | 0 | S | 1 1 1 1 0 | type | 1 | imm8 | 1 0 0 | imm5 | Rd |
6886 * +---+---+---+-----------+------+---+------------+-------+------+------+
6888 static void disas_fp_imm(DisasContext *s, uint32_t insn)
6890 int rd = extract32(insn, 0, 5);
6891 int imm5 = extract32(insn, 5, 5);
6892 int imm8 = extract32(insn, 13, 8);
6893 int type = extract32(insn, 22, 2);
6894 int mos = extract32(insn, 29, 3);
6895 uint64_t imm;
6896 TCGv_i64 tcg_res;
6897 MemOp sz;
6899 if (mos || imm5) {
6900 unallocated_encoding(s);
6901 return;
6904 switch (type) {
6905 case 0:
6906 sz = MO_32;
6907 break;
6908 case 1:
6909 sz = MO_64;
6910 break;
6911 case 3:
6912 sz = MO_16;
6913 if (dc_isar_feature(aa64_fp16, s)) {
6914 break;
6916 /* fallthru */
6917 default:
6918 unallocated_encoding(s);
6919 return;
6922 if (!fp_access_check(s)) {
6923 return;
6926 imm = vfp_expand_imm(sz, imm8);
6928 tcg_res = tcg_const_i64(imm);
6929 write_fp_dreg(s, rd, tcg_res);
6930 tcg_temp_free_i64(tcg_res);
6933 /* Handle floating point <=> fixed point conversions. Note that we can
6934 * also deal with fp <=> integer conversions as a special case (scale == 64)
6935 * OPTME: consider handling that special case specially or at least skipping
6936 * the call to scalbn in the helpers for zero shifts.
6938 static void handle_fpfpcvt(DisasContext *s, int rd, int rn, int opcode,
6939 bool itof, int rmode, int scale, int sf, int type)
6941 bool is_signed = !(opcode & 1);
6942 TCGv_ptr tcg_fpstatus;
6943 TCGv_i32 tcg_shift, tcg_single;
6944 TCGv_i64 tcg_double;
6946 tcg_fpstatus = get_fpstatus_ptr(type == 3);
6948 tcg_shift = tcg_const_i32(64 - scale);
6950 if (itof) {
6951 TCGv_i64 tcg_int = cpu_reg(s, rn);
6952 if (!sf) {
6953 TCGv_i64 tcg_extend = new_tmp_a64(s);
6955 if (is_signed) {
6956 tcg_gen_ext32s_i64(tcg_extend, tcg_int);
6957 } else {
6958 tcg_gen_ext32u_i64(tcg_extend, tcg_int);
6961 tcg_int = tcg_extend;
6964 switch (type) {
6965 case 1: /* float64 */
6966 tcg_double = tcg_temp_new_i64();
6967 if (is_signed) {
6968 gen_helper_vfp_sqtod(tcg_double, tcg_int,
6969 tcg_shift, tcg_fpstatus);
6970 } else {
6971 gen_helper_vfp_uqtod(tcg_double, tcg_int,
6972 tcg_shift, tcg_fpstatus);
6974 write_fp_dreg(s, rd, tcg_double);
6975 tcg_temp_free_i64(tcg_double);
6976 break;
6978 case 0: /* float32 */
6979 tcg_single = tcg_temp_new_i32();
6980 if (is_signed) {
6981 gen_helper_vfp_sqtos(tcg_single, tcg_int,
6982 tcg_shift, tcg_fpstatus);
6983 } else {
6984 gen_helper_vfp_uqtos(tcg_single, tcg_int,
6985 tcg_shift, tcg_fpstatus);
6987 write_fp_sreg(s, rd, tcg_single);
6988 tcg_temp_free_i32(tcg_single);
6989 break;
6991 case 3: /* float16 */
6992 tcg_single = tcg_temp_new_i32();
6993 if (is_signed) {
6994 gen_helper_vfp_sqtoh(tcg_single, tcg_int,
6995 tcg_shift, tcg_fpstatus);
6996 } else {
6997 gen_helper_vfp_uqtoh(tcg_single, tcg_int,
6998 tcg_shift, tcg_fpstatus);
7000 write_fp_sreg(s, rd, tcg_single);
7001 tcg_temp_free_i32(tcg_single);
7002 break;
7004 default:
7005 g_assert_not_reached();
7007 } else {
7008 TCGv_i64 tcg_int = cpu_reg(s, rd);
7009 TCGv_i32 tcg_rmode;
7011 if (extract32(opcode, 2, 1)) {
7012 /* There are too many rounding modes to all fit into rmode,
7013 * so FCVTA[US] is a special case.
7015 rmode = FPROUNDING_TIEAWAY;
7018 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rmode));
7020 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
7022 switch (type) {
7023 case 1: /* float64 */
7024 tcg_double = read_fp_dreg(s, rn);
7025 if (is_signed) {
7026 if (!sf) {
7027 gen_helper_vfp_tosld(tcg_int, tcg_double,
7028 tcg_shift, tcg_fpstatus);
7029 } else {
7030 gen_helper_vfp_tosqd(tcg_int, tcg_double,
7031 tcg_shift, tcg_fpstatus);
7033 } else {
7034 if (!sf) {
7035 gen_helper_vfp_tould(tcg_int, tcg_double,
7036 tcg_shift, tcg_fpstatus);
7037 } else {
7038 gen_helper_vfp_touqd(tcg_int, tcg_double,
7039 tcg_shift, tcg_fpstatus);
7042 if (!sf) {
7043 tcg_gen_ext32u_i64(tcg_int, tcg_int);
7045 tcg_temp_free_i64(tcg_double);
7046 break;
7048 case 0: /* float32 */
7049 tcg_single = read_fp_sreg(s, rn);
7050 if (sf) {
7051 if (is_signed) {
7052 gen_helper_vfp_tosqs(tcg_int, tcg_single,
7053 tcg_shift, tcg_fpstatus);
7054 } else {
7055 gen_helper_vfp_touqs(tcg_int, tcg_single,
7056 tcg_shift, tcg_fpstatus);
7058 } else {
7059 TCGv_i32 tcg_dest = tcg_temp_new_i32();
7060 if (is_signed) {
7061 gen_helper_vfp_tosls(tcg_dest, tcg_single,
7062 tcg_shift, tcg_fpstatus);
7063 } else {
7064 gen_helper_vfp_touls(tcg_dest, tcg_single,
7065 tcg_shift, tcg_fpstatus);
7067 tcg_gen_extu_i32_i64(tcg_int, tcg_dest);
7068 tcg_temp_free_i32(tcg_dest);
7070 tcg_temp_free_i32(tcg_single);
7071 break;
7073 case 3: /* float16 */
7074 tcg_single = read_fp_sreg(s, rn);
7075 if (sf) {
7076 if (is_signed) {
7077 gen_helper_vfp_tosqh(tcg_int, tcg_single,
7078 tcg_shift, tcg_fpstatus);
7079 } else {
7080 gen_helper_vfp_touqh(tcg_int, tcg_single,
7081 tcg_shift, tcg_fpstatus);
7083 } else {
7084 TCGv_i32 tcg_dest = tcg_temp_new_i32();
7085 if (is_signed) {
7086 gen_helper_vfp_toslh(tcg_dest, tcg_single,
7087 tcg_shift, tcg_fpstatus);
7088 } else {
7089 gen_helper_vfp_toulh(tcg_dest, tcg_single,
7090 tcg_shift, tcg_fpstatus);
7092 tcg_gen_extu_i32_i64(tcg_int, tcg_dest);
7093 tcg_temp_free_i32(tcg_dest);
7095 tcg_temp_free_i32(tcg_single);
7096 break;
7098 default:
7099 g_assert_not_reached();
7102 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
7103 tcg_temp_free_i32(tcg_rmode);
7106 tcg_temp_free_ptr(tcg_fpstatus);
7107 tcg_temp_free_i32(tcg_shift);
7110 /* Floating point <-> fixed point conversions
7111 * 31 30 29 28 24 23 22 21 20 19 18 16 15 10 9 5 4 0
7112 * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
7113 * | sf | 0 | S | 1 1 1 1 0 | type | 0 | rmode | opcode | scale | Rn | Rd |
7114 * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
7116 static void disas_fp_fixed_conv(DisasContext *s, uint32_t insn)
7118 int rd = extract32(insn, 0, 5);
7119 int rn = extract32(insn, 5, 5);
7120 int scale = extract32(insn, 10, 6);
7121 int opcode = extract32(insn, 16, 3);
7122 int rmode = extract32(insn, 19, 2);
7123 int type = extract32(insn, 22, 2);
7124 bool sbit = extract32(insn, 29, 1);
7125 bool sf = extract32(insn, 31, 1);
7126 bool itof;
7128 if (sbit || (!sf && scale < 32)) {
7129 unallocated_encoding(s);
7130 return;
7133 switch (type) {
7134 case 0: /* float32 */
7135 case 1: /* float64 */
7136 break;
7137 case 3: /* float16 */
7138 if (dc_isar_feature(aa64_fp16, s)) {
7139 break;
7141 /* fallthru */
7142 default:
7143 unallocated_encoding(s);
7144 return;
7147 switch ((rmode << 3) | opcode) {
7148 case 0x2: /* SCVTF */
7149 case 0x3: /* UCVTF */
7150 itof = true;
7151 break;
7152 case 0x18: /* FCVTZS */
7153 case 0x19: /* FCVTZU */
7154 itof = false;
7155 break;
7156 default:
7157 unallocated_encoding(s);
7158 return;
7161 if (!fp_access_check(s)) {
7162 return;
7165 handle_fpfpcvt(s, rd, rn, opcode, itof, FPROUNDING_ZERO, scale, sf, type);
7168 static void handle_fmov(DisasContext *s, int rd, int rn, int type, bool itof)
7170 /* FMOV: gpr to or from float, double, or top half of quad fp reg,
7171 * without conversion.
7174 if (itof) {
7175 TCGv_i64 tcg_rn = cpu_reg(s, rn);
7176 TCGv_i64 tmp;
7178 switch (type) {
7179 case 0:
7180 /* 32 bit */
7181 tmp = tcg_temp_new_i64();
7182 tcg_gen_ext32u_i64(tmp, tcg_rn);
7183 write_fp_dreg(s, rd, tmp);
7184 tcg_temp_free_i64(tmp);
7185 break;
7186 case 1:
7187 /* 64 bit */
7188 write_fp_dreg(s, rd, tcg_rn);
7189 break;
7190 case 2:
7191 /* 64 bit to top half. */
7192 tcg_gen_st_i64(tcg_rn, cpu_env, fp_reg_hi_offset(s, rd));
7193 clear_vec_high(s, true, rd);
7194 break;
7195 case 3:
7196 /* 16 bit */
7197 tmp = tcg_temp_new_i64();
7198 tcg_gen_ext16u_i64(tmp, tcg_rn);
7199 write_fp_dreg(s, rd, tmp);
7200 tcg_temp_free_i64(tmp);
7201 break;
7202 default:
7203 g_assert_not_reached();
7205 } else {
7206 TCGv_i64 tcg_rd = cpu_reg(s, rd);
7208 switch (type) {
7209 case 0:
7210 /* 32 bit */
7211 tcg_gen_ld32u_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_32));
7212 break;
7213 case 1:
7214 /* 64 bit */
7215 tcg_gen_ld_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_64));
7216 break;
7217 case 2:
7218 /* 64 bits from top half */
7219 tcg_gen_ld_i64(tcg_rd, cpu_env, fp_reg_hi_offset(s, rn));
7220 break;
7221 case 3:
7222 /* 16 bit */
7223 tcg_gen_ld16u_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_16));
7224 break;
7225 default:
7226 g_assert_not_reached();
7231 static void handle_fjcvtzs(DisasContext *s, int rd, int rn)
7233 TCGv_i64 t = read_fp_dreg(s, rn);
7234 TCGv_ptr fpstatus = get_fpstatus_ptr(false);
7236 gen_helper_fjcvtzs(t, t, fpstatus);
7238 tcg_temp_free_ptr(fpstatus);
7240 tcg_gen_ext32u_i64(cpu_reg(s, rd), t);
7241 tcg_gen_extrh_i64_i32(cpu_ZF, t);
7242 tcg_gen_movi_i32(cpu_CF, 0);
7243 tcg_gen_movi_i32(cpu_NF, 0);
7244 tcg_gen_movi_i32(cpu_VF, 0);
7246 tcg_temp_free_i64(t);
7249 /* Floating point <-> integer conversions
7250 * 31 30 29 28 24 23 22 21 20 19 18 16 15 10 9 5 4 0
7251 * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
7252 * | sf | 0 | S | 1 1 1 1 0 | type | 1 | rmode | opc | 0 0 0 0 0 0 | Rn | Rd |
7253 * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
7255 static void disas_fp_int_conv(DisasContext *s, uint32_t insn)
7257 int rd = extract32(insn, 0, 5);
7258 int rn = extract32(insn, 5, 5);
7259 int opcode = extract32(insn, 16, 3);
7260 int rmode = extract32(insn, 19, 2);
7261 int type = extract32(insn, 22, 2);
7262 bool sbit = extract32(insn, 29, 1);
7263 bool sf = extract32(insn, 31, 1);
7264 bool itof = false;
7266 if (sbit) {
7267 goto do_unallocated;
7270 switch (opcode) {
7271 case 2: /* SCVTF */
7272 case 3: /* UCVTF */
7273 itof = true;
7274 /* fallthru */
7275 case 4: /* FCVTAS */
7276 case 5: /* FCVTAU */
7277 if (rmode != 0) {
7278 goto do_unallocated;
7280 /* fallthru */
7281 case 0: /* FCVT[NPMZ]S */
7282 case 1: /* FCVT[NPMZ]U */
7283 switch (type) {
7284 case 0: /* float32 */
7285 case 1: /* float64 */
7286 break;
7287 case 3: /* float16 */
7288 if (!dc_isar_feature(aa64_fp16, s)) {
7289 goto do_unallocated;
7291 break;
7292 default:
7293 goto do_unallocated;
7295 if (!fp_access_check(s)) {
7296 return;
7298 handle_fpfpcvt(s, rd, rn, opcode, itof, rmode, 64, sf, type);
7299 break;
7301 default:
7302 switch (sf << 7 | type << 5 | rmode << 3 | opcode) {
7303 case 0b01100110: /* FMOV half <-> 32-bit int */
7304 case 0b01100111:
7305 case 0b11100110: /* FMOV half <-> 64-bit int */
7306 case 0b11100111:
7307 if (!dc_isar_feature(aa64_fp16, s)) {
7308 goto do_unallocated;
7310 /* fallthru */
7311 case 0b00000110: /* FMOV 32-bit */
7312 case 0b00000111:
7313 case 0b10100110: /* FMOV 64-bit */
7314 case 0b10100111:
7315 case 0b11001110: /* FMOV top half of 128-bit */
7316 case 0b11001111:
7317 if (!fp_access_check(s)) {
7318 return;
7320 itof = opcode & 1;
7321 handle_fmov(s, rd, rn, type, itof);
7322 break;
7324 case 0b00111110: /* FJCVTZS */
7325 if (!dc_isar_feature(aa64_jscvt, s)) {
7326 goto do_unallocated;
7327 } else if (fp_access_check(s)) {
7328 handle_fjcvtzs(s, rd, rn);
7330 break;
7332 default:
7333 do_unallocated:
7334 unallocated_encoding(s);
7335 return;
7337 break;
7341 /* FP-specific subcases of table C3-6 (SIMD and FP data processing)
7342 * 31 30 29 28 25 24 0
7343 * +---+---+---+---------+-----------------------------+
7344 * | | 0 | | 1 1 1 1 | |
7345 * +---+---+---+---------+-----------------------------+
7347 static void disas_data_proc_fp(DisasContext *s, uint32_t insn)
7349 if (extract32(insn, 24, 1)) {
7350 /* Floating point data-processing (3 source) */
7351 disas_fp_3src(s, insn);
7352 } else if (extract32(insn, 21, 1) == 0) {
7353 /* Floating point to fixed point conversions */
7354 disas_fp_fixed_conv(s, insn);
7355 } else {
7356 switch (extract32(insn, 10, 2)) {
7357 case 1:
7358 /* Floating point conditional compare */
7359 disas_fp_ccomp(s, insn);
7360 break;
7361 case 2:
7362 /* Floating point data-processing (2 source) */
7363 disas_fp_2src(s, insn);
7364 break;
7365 case 3:
7366 /* Floating point conditional select */
7367 disas_fp_csel(s, insn);
7368 break;
7369 case 0:
7370 switch (ctz32(extract32(insn, 12, 4))) {
7371 case 0: /* [15:12] == xxx1 */
7372 /* Floating point immediate */
7373 disas_fp_imm(s, insn);
7374 break;
7375 case 1: /* [15:12] == xx10 */
7376 /* Floating point compare */
7377 disas_fp_compare(s, insn);
7378 break;
7379 case 2: /* [15:12] == x100 */
7380 /* Floating point data-processing (1 source) */
7381 disas_fp_1src(s, insn);
7382 break;
7383 case 3: /* [15:12] == 1000 */
7384 unallocated_encoding(s);
7385 break;
7386 default: /* [15:12] == 0000 */
7387 /* Floating point <-> integer conversions */
7388 disas_fp_int_conv(s, insn);
7389 break;
7391 break;
7396 static void do_ext64(DisasContext *s, TCGv_i64 tcg_left, TCGv_i64 tcg_right,
7397 int pos)
7399 /* Extract 64 bits from the middle of two concatenated 64 bit
7400 * vector register slices left:right. The extracted bits start
7401 * at 'pos' bits into the right (least significant) side.
7402 * We return the result in tcg_right, and guarantee not to
7403 * trash tcg_left.
7405 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
7406 assert(pos > 0 && pos < 64);
7408 tcg_gen_shri_i64(tcg_right, tcg_right, pos);
7409 tcg_gen_shli_i64(tcg_tmp, tcg_left, 64 - pos);
7410 tcg_gen_or_i64(tcg_right, tcg_right, tcg_tmp);
7412 tcg_temp_free_i64(tcg_tmp);
7415 /* EXT
7416 * 31 30 29 24 23 22 21 20 16 15 14 11 10 9 5 4 0
7417 * +---+---+-------------+-----+---+------+---+------+---+------+------+
7418 * | 0 | Q | 1 0 1 1 1 0 | op2 | 0 | Rm | 0 | imm4 | 0 | Rn | Rd |
7419 * +---+---+-------------+-----+---+------+---+------+---+------+------+
7421 static void disas_simd_ext(DisasContext *s, uint32_t insn)
7423 int is_q = extract32(insn, 30, 1);
7424 int op2 = extract32(insn, 22, 2);
7425 int imm4 = extract32(insn, 11, 4);
7426 int rm = extract32(insn, 16, 5);
7427 int rn = extract32(insn, 5, 5);
7428 int rd = extract32(insn, 0, 5);
7429 int pos = imm4 << 3;
7430 TCGv_i64 tcg_resl, tcg_resh;
7432 if (op2 != 0 || (!is_q && extract32(imm4, 3, 1))) {
7433 unallocated_encoding(s);
7434 return;
7437 if (!fp_access_check(s)) {
7438 return;
7441 tcg_resh = tcg_temp_new_i64();
7442 tcg_resl = tcg_temp_new_i64();
7444 /* Vd gets bits starting at pos bits into Vm:Vn. This is
7445 * either extracting 128 bits from a 128:128 concatenation, or
7446 * extracting 64 bits from a 64:64 concatenation.
7448 if (!is_q) {
7449 read_vec_element(s, tcg_resl, rn, 0, MO_64);
7450 if (pos != 0) {
7451 read_vec_element(s, tcg_resh, rm, 0, MO_64);
7452 do_ext64(s, tcg_resh, tcg_resl, pos);
7454 } else {
7455 TCGv_i64 tcg_hh;
7456 typedef struct {
7457 int reg;
7458 int elt;
7459 } EltPosns;
7460 EltPosns eltposns[] = { {rn, 0}, {rn, 1}, {rm, 0}, {rm, 1} };
7461 EltPosns *elt = eltposns;
7463 if (pos >= 64) {
7464 elt++;
7465 pos -= 64;
7468 read_vec_element(s, tcg_resl, elt->reg, elt->elt, MO_64);
7469 elt++;
7470 read_vec_element(s, tcg_resh, elt->reg, elt->elt, MO_64);
7471 elt++;
7472 if (pos != 0) {
7473 do_ext64(s, tcg_resh, tcg_resl, pos);
7474 tcg_hh = tcg_temp_new_i64();
7475 read_vec_element(s, tcg_hh, elt->reg, elt->elt, MO_64);
7476 do_ext64(s, tcg_hh, tcg_resh, pos);
7477 tcg_temp_free_i64(tcg_hh);
7481 write_vec_element(s, tcg_resl, rd, 0, MO_64);
7482 tcg_temp_free_i64(tcg_resl);
7483 if (is_q) {
7484 write_vec_element(s, tcg_resh, rd, 1, MO_64);
7486 tcg_temp_free_i64(tcg_resh);
7487 clear_vec_high(s, is_q, rd);
7490 /* TBL/TBX
7491 * 31 30 29 24 23 22 21 20 16 15 14 13 12 11 10 9 5 4 0
7492 * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
7493 * | 0 | Q | 0 0 1 1 1 0 | op2 | 0 | Rm | 0 | len | op | 0 0 | Rn | Rd |
7494 * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
7496 static void disas_simd_tb(DisasContext *s, uint32_t insn)
7498 int op2 = extract32(insn, 22, 2);
7499 int is_q = extract32(insn, 30, 1);
7500 int rm = extract32(insn, 16, 5);
7501 int rn = extract32(insn, 5, 5);
7502 int rd = extract32(insn, 0, 5);
7503 int is_tblx = extract32(insn, 12, 1);
7504 int len = extract32(insn, 13, 2);
7505 TCGv_i64 tcg_resl, tcg_resh, tcg_idx;
7506 TCGv_i32 tcg_regno, tcg_numregs;
7508 if (op2 != 0) {
7509 unallocated_encoding(s);
7510 return;
7513 if (!fp_access_check(s)) {
7514 return;
7517 /* This does a table lookup: for every byte element in the input
7518 * we index into a table formed from up to four vector registers,
7519 * and then the output is the result of the lookups. Our helper
7520 * function does the lookup operation for a single 64 bit part of
7521 * the input.
7523 tcg_resl = tcg_temp_new_i64();
7524 tcg_resh = NULL;
7526 if (is_tblx) {
7527 read_vec_element(s, tcg_resl, rd, 0, MO_64);
7528 } else {
7529 tcg_gen_movi_i64(tcg_resl, 0);
7532 if (is_q) {
7533 tcg_resh = tcg_temp_new_i64();
7534 if (is_tblx) {
7535 read_vec_element(s, tcg_resh, rd, 1, MO_64);
7536 } else {
7537 tcg_gen_movi_i64(tcg_resh, 0);
7541 tcg_idx = tcg_temp_new_i64();
7542 tcg_regno = tcg_const_i32(rn);
7543 tcg_numregs = tcg_const_i32(len + 1);
7544 read_vec_element(s, tcg_idx, rm, 0, MO_64);
7545 gen_helper_simd_tbl(tcg_resl, cpu_env, tcg_resl, tcg_idx,
7546 tcg_regno, tcg_numregs);
7547 if (is_q) {
7548 read_vec_element(s, tcg_idx, rm, 1, MO_64);
7549 gen_helper_simd_tbl(tcg_resh, cpu_env, tcg_resh, tcg_idx,
7550 tcg_regno, tcg_numregs);
7552 tcg_temp_free_i64(tcg_idx);
7553 tcg_temp_free_i32(tcg_regno);
7554 tcg_temp_free_i32(tcg_numregs);
7556 write_vec_element(s, tcg_resl, rd, 0, MO_64);
7557 tcg_temp_free_i64(tcg_resl);
7559 if (is_q) {
7560 write_vec_element(s, tcg_resh, rd, 1, MO_64);
7561 tcg_temp_free_i64(tcg_resh);
7563 clear_vec_high(s, is_q, rd);
7566 /* ZIP/UZP/TRN
7567 * 31 30 29 24 23 22 21 20 16 15 14 12 11 10 9 5 4 0
7568 * +---+---+-------------+------+---+------+---+------------------+------+
7569 * | 0 | Q | 0 0 1 1 1 0 | size | 0 | Rm | 0 | opc | 1 0 | Rn | Rd |
7570 * +---+---+-------------+------+---+------+---+------------------+------+
7572 static void disas_simd_zip_trn(DisasContext *s, uint32_t insn)
7574 int rd = extract32(insn, 0, 5);
7575 int rn = extract32(insn, 5, 5);
7576 int rm = extract32(insn, 16, 5);
7577 int size = extract32(insn, 22, 2);
7578 /* opc field bits [1:0] indicate ZIP/UZP/TRN;
7579 * bit 2 indicates 1 vs 2 variant of the insn.
7581 int opcode = extract32(insn, 12, 2);
7582 bool part = extract32(insn, 14, 1);
7583 bool is_q = extract32(insn, 30, 1);
7584 int esize = 8 << size;
7585 int i, ofs;
7586 int datasize = is_q ? 128 : 64;
7587 int elements = datasize / esize;
7588 TCGv_i64 tcg_res, tcg_resl, tcg_resh;
7590 if (opcode == 0 || (size == 3 && !is_q)) {
7591 unallocated_encoding(s);
7592 return;
7595 if (!fp_access_check(s)) {
7596 return;
7599 tcg_resl = tcg_const_i64(0);
7600 tcg_resh = is_q ? tcg_const_i64(0) : NULL;
7601 tcg_res = tcg_temp_new_i64();
7603 for (i = 0; i < elements; i++) {
7604 switch (opcode) {
7605 case 1: /* UZP1/2 */
7607 int midpoint = elements / 2;
7608 if (i < midpoint) {
7609 read_vec_element(s, tcg_res, rn, 2 * i + part, size);
7610 } else {
7611 read_vec_element(s, tcg_res, rm,
7612 2 * (i - midpoint) + part, size);
7614 break;
7616 case 2: /* TRN1/2 */
7617 if (i & 1) {
7618 read_vec_element(s, tcg_res, rm, (i & ~1) + part, size);
7619 } else {
7620 read_vec_element(s, tcg_res, rn, (i & ~1) + part, size);
7622 break;
7623 case 3: /* ZIP1/2 */
7625 int base = part * elements / 2;
7626 if (i & 1) {
7627 read_vec_element(s, tcg_res, rm, base + (i >> 1), size);
7628 } else {
7629 read_vec_element(s, tcg_res, rn, base + (i >> 1), size);
7631 break;
7633 default:
7634 g_assert_not_reached();
7637 ofs = i * esize;
7638 if (ofs < 64) {
7639 tcg_gen_shli_i64(tcg_res, tcg_res, ofs);
7640 tcg_gen_or_i64(tcg_resl, tcg_resl, tcg_res);
7641 } else {
7642 tcg_gen_shli_i64(tcg_res, tcg_res, ofs - 64);
7643 tcg_gen_or_i64(tcg_resh, tcg_resh, tcg_res);
7647 tcg_temp_free_i64(tcg_res);
7649 write_vec_element(s, tcg_resl, rd, 0, MO_64);
7650 tcg_temp_free_i64(tcg_resl);
7652 if (is_q) {
7653 write_vec_element(s, tcg_resh, rd, 1, MO_64);
7654 tcg_temp_free_i64(tcg_resh);
7656 clear_vec_high(s, is_q, rd);
7660 * do_reduction_op helper
7662 * This mirrors the Reduce() pseudocode in the ARM ARM. It is
7663 * important for correct NaN propagation that we do these
7664 * operations in exactly the order specified by the pseudocode.
7666 * This is a recursive function, TCG temps should be freed by the
7667 * calling function once it is done with the values.
7669 static TCGv_i32 do_reduction_op(DisasContext *s, int fpopcode, int rn,
7670 int esize, int size, int vmap, TCGv_ptr fpst)
7672 if (esize == size) {
7673 int element;
7674 MemOp msize = esize == 16 ? MO_16 : MO_32;
7675 TCGv_i32 tcg_elem;
7677 /* We should have one register left here */
7678 assert(ctpop8(vmap) == 1);
7679 element = ctz32(vmap);
7680 assert(element < 8);
7682 tcg_elem = tcg_temp_new_i32();
7683 read_vec_element_i32(s, tcg_elem, rn, element, msize);
7684 return tcg_elem;
7685 } else {
7686 int bits = size / 2;
7687 int shift = ctpop8(vmap) / 2;
7688 int vmap_lo = (vmap >> shift) & vmap;
7689 int vmap_hi = (vmap & ~vmap_lo);
7690 TCGv_i32 tcg_hi, tcg_lo, tcg_res;
7692 tcg_hi = do_reduction_op(s, fpopcode, rn, esize, bits, vmap_hi, fpst);
7693 tcg_lo = do_reduction_op(s, fpopcode, rn, esize, bits, vmap_lo, fpst);
7694 tcg_res = tcg_temp_new_i32();
7696 switch (fpopcode) {
7697 case 0x0c: /* fmaxnmv half-precision */
7698 gen_helper_advsimd_maxnumh(tcg_res, tcg_lo, tcg_hi, fpst);
7699 break;
7700 case 0x0f: /* fmaxv half-precision */
7701 gen_helper_advsimd_maxh(tcg_res, tcg_lo, tcg_hi, fpst);
7702 break;
7703 case 0x1c: /* fminnmv half-precision */
7704 gen_helper_advsimd_minnumh(tcg_res, tcg_lo, tcg_hi, fpst);
7705 break;
7706 case 0x1f: /* fminv half-precision */
7707 gen_helper_advsimd_minh(tcg_res, tcg_lo, tcg_hi, fpst);
7708 break;
7709 case 0x2c: /* fmaxnmv */
7710 gen_helper_vfp_maxnums(tcg_res, tcg_lo, tcg_hi, fpst);
7711 break;
7712 case 0x2f: /* fmaxv */
7713 gen_helper_vfp_maxs(tcg_res, tcg_lo, tcg_hi, fpst);
7714 break;
7715 case 0x3c: /* fminnmv */
7716 gen_helper_vfp_minnums(tcg_res, tcg_lo, tcg_hi, fpst);
7717 break;
7718 case 0x3f: /* fminv */
7719 gen_helper_vfp_mins(tcg_res, tcg_lo, tcg_hi, fpst);
7720 break;
7721 default:
7722 g_assert_not_reached();
7725 tcg_temp_free_i32(tcg_hi);
7726 tcg_temp_free_i32(tcg_lo);
7727 return tcg_res;
7731 /* AdvSIMD across lanes
7732 * 31 30 29 28 24 23 22 21 17 16 12 11 10 9 5 4 0
7733 * +---+---+---+-----------+------+-----------+--------+-----+------+------+
7734 * | 0 | Q | U | 0 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 | Rn | Rd |
7735 * +---+---+---+-----------+------+-----------+--------+-----+------+------+
7737 static void disas_simd_across_lanes(DisasContext *s, uint32_t insn)
7739 int rd = extract32(insn, 0, 5);
7740 int rn = extract32(insn, 5, 5);
7741 int size = extract32(insn, 22, 2);
7742 int opcode = extract32(insn, 12, 5);
7743 bool is_q = extract32(insn, 30, 1);
7744 bool is_u = extract32(insn, 29, 1);
7745 bool is_fp = false;
7746 bool is_min = false;
7747 int esize;
7748 int elements;
7749 int i;
7750 TCGv_i64 tcg_res, tcg_elt;
7752 switch (opcode) {
7753 case 0x1b: /* ADDV */
7754 if (is_u) {
7755 unallocated_encoding(s);
7756 return;
7758 /* fall through */
7759 case 0x3: /* SADDLV, UADDLV */
7760 case 0xa: /* SMAXV, UMAXV */
7761 case 0x1a: /* SMINV, UMINV */
7762 if (size == 3 || (size == 2 && !is_q)) {
7763 unallocated_encoding(s);
7764 return;
7766 break;
7767 case 0xc: /* FMAXNMV, FMINNMV */
7768 case 0xf: /* FMAXV, FMINV */
7769 /* Bit 1 of size field encodes min vs max and the actual size
7770 * depends on the encoding of the U bit. If not set (and FP16
7771 * enabled) then we do half-precision float instead of single
7772 * precision.
7774 is_min = extract32(size, 1, 1);
7775 is_fp = true;
7776 if (!is_u && dc_isar_feature(aa64_fp16, s)) {
7777 size = 1;
7778 } else if (!is_u || !is_q || extract32(size, 0, 1)) {
7779 unallocated_encoding(s);
7780 return;
7781 } else {
7782 size = 2;
7784 break;
7785 default:
7786 unallocated_encoding(s);
7787 return;
7790 if (!fp_access_check(s)) {
7791 return;
7794 esize = 8 << size;
7795 elements = (is_q ? 128 : 64) / esize;
7797 tcg_res = tcg_temp_new_i64();
7798 tcg_elt = tcg_temp_new_i64();
7800 /* These instructions operate across all lanes of a vector
7801 * to produce a single result. We can guarantee that a 64
7802 * bit intermediate is sufficient:
7803 * + for [US]ADDLV the maximum element size is 32 bits, and
7804 * the result type is 64 bits
7805 * + for FMAX*V, FMIN*V, ADDV the intermediate type is the
7806 * same as the element size, which is 32 bits at most
7807 * For the integer operations we can choose to work at 64
7808 * or 32 bits and truncate at the end; for simplicity
7809 * we use 64 bits always. The floating point
7810 * ops do require 32 bit intermediates, though.
7812 if (!is_fp) {
7813 read_vec_element(s, tcg_res, rn, 0, size | (is_u ? 0 : MO_SIGN));
7815 for (i = 1; i < elements; i++) {
7816 read_vec_element(s, tcg_elt, rn, i, size | (is_u ? 0 : MO_SIGN));
7818 switch (opcode) {
7819 case 0x03: /* SADDLV / UADDLV */
7820 case 0x1b: /* ADDV */
7821 tcg_gen_add_i64(tcg_res, tcg_res, tcg_elt);
7822 break;
7823 case 0x0a: /* SMAXV / UMAXV */
7824 if (is_u) {
7825 tcg_gen_umax_i64(tcg_res, tcg_res, tcg_elt);
7826 } else {
7827 tcg_gen_smax_i64(tcg_res, tcg_res, tcg_elt);
7829 break;
7830 case 0x1a: /* SMINV / UMINV */
7831 if (is_u) {
7832 tcg_gen_umin_i64(tcg_res, tcg_res, tcg_elt);
7833 } else {
7834 tcg_gen_smin_i64(tcg_res, tcg_res, tcg_elt);
7836 break;
7837 default:
7838 g_assert_not_reached();
7842 } else {
7843 /* Floating point vector reduction ops which work across 32
7844 * bit (single) or 16 bit (half-precision) intermediates.
7845 * Note that correct NaN propagation requires that we do these
7846 * operations in exactly the order specified by the pseudocode.
7848 TCGv_ptr fpst = get_fpstatus_ptr(size == MO_16);
7849 int fpopcode = opcode | is_min << 4 | is_u << 5;
7850 int vmap = (1 << elements) - 1;
7851 TCGv_i32 tcg_res32 = do_reduction_op(s, fpopcode, rn, esize,
7852 (is_q ? 128 : 64), vmap, fpst);
7853 tcg_gen_extu_i32_i64(tcg_res, tcg_res32);
7854 tcg_temp_free_i32(tcg_res32);
7855 tcg_temp_free_ptr(fpst);
7858 tcg_temp_free_i64(tcg_elt);
7860 /* Now truncate the result to the width required for the final output */
7861 if (opcode == 0x03) {
7862 /* SADDLV, UADDLV: result is 2*esize */
7863 size++;
7866 switch (size) {
7867 case 0:
7868 tcg_gen_ext8u_i64(tcg_res, tcg_res);
7869 break;
7870 case 1:
7871 tcg_gen_ext16u_i64(tcg_res, tcg_res);
7872 break;
7873 case 2:
7874 tcg_gen_ext32u_i64(tcg_res, tcg_res);
7875 break;
7876 case 3:
7877 break;
7878 default:
7879 g_assert_not_reached();
7882 write_fp_dreg(s, rd, tcg_res);
7883 tcg_temp_free_i64(tcg_res);
7886 /* DUP (Element, Vector)
7888 * 31 30 29 21 20 16 15 10 9 5 4 0
7889 * +---+---+-------------------+--------+-------------+------+------+
7890 * | 0 | Q | 0 0 1 1 1 0 0 0 0 | imm5 | 0 0 0 0 0 1 | Rn | Rd |
7891 * +---+---+-------------------+--------+-------------+------+------+
7893 * size: encoded in imm5 (see ARM ARM LowestSetBit())
7895 static void handle_simd_dupe(DisasContext *s, int is_q, int rd, int rn,
7896 int imm5)
7898 int size = ctz32(imm5);
7899 int index;
7901 if (size > 3 || (size == 3 && !is_q)) {
7902 unallocated_encoding(s);
7903 return;
7906 if (!fp_access_check(s)) {
7907 return;
7910 index = imm5 >> (size + 1);
7911 tcg_gen_gvec_dup_mem(size, vec_full_reg_offset(s, rd),
7912 vec_reg_offset(s, rn, index, size),
7913 is_q ? 16 : 8, vec_full_reg_size(s));
7916 /* DUP (element, scalar)
7917 * 31 21 20 16 15 10 9 5 4 0
7918 * +-----------------------+--------+-------------+------+------+
7919 * | 0 1 0 1 1 1 1 0 0 0 0 | imm5 | 0 0 0 0 0 1 | Rn | Rd |
7920 * +-----------------------+--------+-------------+------+------+
7922 static void handle_simd_dupes(DisasContext *s, int rd, int rn,
7923 int imm5)
7925 int size = ctz32(imm5);
7926 int index;
7927 TCGv_i64 tmp;
7929 if (size > 3) {
7930 unallocated_encoding(s);
7931 return;
7934 if (!fp_access_check(s)) {
7935 return;
7938 index = imm5 >> (size + 1);
7940 /* This instruction just extracts the specified element and
7941 * zero-extends it into the bottom of the destination register.
7943 tmp = tcg_temp_new_i64();
7944 read_vec_element(s, tmp, rn, index, size);
7945 write_fp_dreg(s, rd, tmp);
7946 tcg_temp_free_i64(tmp);
7949 /* DUP (General)
7951 * 31 30 29 21 20 16 15 10 9 5 4 0
7952 * +---+---+-------------------+--------+-------------+------+------+
7953 * | 0 | Q | 0 0 1 1 1 0 0 0 0 | imm5 | 0 0 0 0 1 1 | Rn | Rd |
7954 * +---+---+-------------------+--------+-------------+------+------+
7956 * size: encoded in imm5 (see ARM ARM LowestSetBit())
7958 static void handle_simd_dupg(DisasContext *s, int is_q, int rd, int rn,
7959 int imm5)
7961 int size = ctz32(imm5);
7962 uint32_t dofs, oprsz, maxsz;
7964 if (size > 3 || ((size == 3) && !is_q)) {
7965 unallocated_encoding(s);
7966 return;
7969 if (!fp_access_check(s)) {
7970 return;
7973 dofs = vec_full_reg_offset(s, rd);
7974 oprsz = is_q ? 16 : 8;
7975 maxsz = vec_full_reg_size(s);
7977 tcg_gen_gvec_dup_i64(size, dofs, oprsz, maxsz, cpu_reg(s, rn));
7980 /* INS (Element)
7982 * 31 21 20 16 15 14 11 10 9 5 4 0
7983 * +-----------------------+--------+------------+---+------+------+
7984 * | 0 1 1 0 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 | Rn | Rd |
7985 * +-----------------------+--------+------------+---+------+------+
7987 * size: encoded in imm5 (see ARM ARM LowestSetBit())
7988 * index: encoded in imm5<4:size+1>
7990 static void handle_simd_inse(DisasContext *s, int rd, int rn,
7991 int imm4, int imm5)
7993 int size = ctz32(imm5);
7994 int src_index, dst_index;
7995 TCGv_i64 tmp;
7997 if (size > 3) {
7998 unallocated_encoding(s);
7999 return;
8002 if (!fp_access_check(s)) {
8003 return;
8006 dst_index = extract32(imm5, 1+size, 5);
8007 src_index = extract32(imm4, size, 4);
8009 tmp = tcg_temp_new_i64();
8011 read_vec_element(s, tmp, rn, src_index, size);
8012 write_vec_element(s, tmp, rd, dst_index, size);
8014 tcg_temp_free_i64(tmp);
8016 /* INS is considered a 128-bit write for SVE. */
8017 clear_vec_high(s, true, rd);
8021 /* INS (General)
8023 * 31 21 20 16 15 10 9 5 4 0
8024 * +-----------------------+--------+-------------+------+------+
8025 * | 0 1 0 0 1 1 1 0 0 0 0 | imm5 | 0 0 0 1 1 1 | Rn | Rd |
8026 * +-----------------------+--------+-------------+------+------+
8028 * size: encoded in imm5 (see ARM ARM LowestSetBit())
8029 * index: encoded in imm5<4:size+1>
8031 static void handle_simd_insg(DisasContext *s, int rd, int rn, int imm5)
8033 int size = ctz32(imm5);
8034 int idx;
8036 if (size > 3) {
8037 unallocated_encoding(s);
8038 return;
8041 if (!fp_access_check(s)) {
8042 return;
8045 idx = extract32(imm5, 1 + size, 4 - size);
8046 write_vec_element(s, cpu_reg(s, rn), rd, idx, size);
8048 /* INS is considered a 128-bit write for SVE. */
8049 clear_vec_high(s, true, rd);
8053 * UMOV (General)
8054 * SMOV (General)
8056 * 31 30 29 21 20 16 15 12 10 9 5 4 0
8057 * +---+---+-------------------+--------+-------------+------+------+
8058 * | 0 | Q | 0 0 1 1 1 0 0 0 0 | imm5 | 0 0 1 U 1 1 | Rn | Rd |
8059 * +---+---+-------------------+--------+-------------+------+------+
8061 * U: unsigned when set
8062 * size: encoded in imm5 (see ARM ARM LowestSetBit())
8064 static void handle_simd_umov_smov(DisasContext *s, int is_q, int is_signed,
8065 int rn, int rd, int imm5)
8067 int size = ctz32(imm5);
8068 int element;
8069 TCGv_i64 tcg_rd;
8071 /* Check for UnallocatedEncodings */
8072 if (is_signed) {
8073 if (size > 2 || (size == 2 && !is_q)) {
8074 unallocated_encoding(s);
8075 return;
8077 } else {
8078 if (size > 3
8079 || (size < 3 && is_q)
8080 || (size == 3 && !is_q)) {
8081 unallocated_encoding(s);
8082 return;
8086 if (!fp_access_check(s)) {
8087 return;
8090 element = extract32(imm5, 1+size, 4);
8092 tcg_rd = cpu_reg(s, rd);
8093 read_vec_element(s, tcg_rd, rn, element, size | (is_signed ? MO_SIGN : 0));
8094 if (is_signed && !is_q) {
8095 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
8099 /* AdvSIMD copy
8100 * 31 30 29 28 21 20 16 15 14 11 10 9 5 4 0
8101 * +---+---+----+-----------------+------+---+------+---+------+------+
8102 * | 0 | Q | op | 0 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 | Rn | Rd |
8103 * +---+---+----+-----------------+------+---+------+---+------+------+
8105 static void disas_simd_copy(DisasContext *s, uint32_t insn)
8107 int rd = extract32(insn, 0, 5);
8108 int rn = extract32(insn, 5, 5);
8109 int imm4 = extract32(insn, 11, 4);
8110 int op = extract32(insn, 29, 1);
8111 int is_q = extract32(insn, 30, 1);
8112 int imm5 = extract32(insn, 16, 5);
8114 if (op) {
8115 if (is_q) {
8116 /* INS (element) */
8117 handle_simd_inse(s, rd, rn, imm4, imm5);
8118 } else {
8119 unallocated_encoding(s);
8121 } else {
8122 switch (imm4) {
8123 case 0:
8124 /* DUP (element - vector) */
8125 handle_simd_dupe(s, is_q, rd, rn, imm5);
8126 break;
8127 case 1:
8128 /* DUP (general) */
8129 handle_simd_dupg(s, is_q, rd, rn, imm5);
8130 break;
8131 case 3:
8132 if (is_q) {
8133 /* INS (general) */
8134 handle_simd_insg(s, rd, rn, imm5);
8135 } else {
8136 unallocated_encoding(s);
8138 break;
8139 case 5:
8140 case 7:
8141 /* UMOV/SMOV (is_q indicates 32/64; imm4 indicates signedness) */
8142 handle_simd_umov_smov(s, is_q, (imm4 == 5), rn, rd, imm5);
8143 break;
8144 default:
8145 unallocated_encoding(s);
8146 break;
8151 /* AdvSIMD modified immediate
8152 * 31 30 29 28 19 18 16 15 12 11 10 9 5 4 0
8153 * +---+---+----+---------------------+-----+-------+----+---+-------+------+
8154 * | 0 | Q | op | 0 1 1 1 1 0 0 0 0 0 | abc | cmode | o2 | 1 | defgh | Rd |
8155 * +---+---+----+---------------------+-----+-------+----+---+-------+------+
8157 * There are a number of operations that can be carried out here:
8158 * MOVI - move (shifted) imm into register
8159 * MVNI - move inverted (shifted) imm into register
8160 * ORR - bitwise OR of (shifted) imm with register
8161 * BIC - bitwise clear of (shifted) imm with register
8162 * With ARMv8.2 we also have:
8163 * FMOV half-precision
8165 static void disas_simd_mod_imm(DisasContext *s, uint32_t insn)
8167 int rd = extract32(insn, 0, 5);
8168 int cmode = extract32(insn, 12, 4);
8169 int cmode_3_1 = extract32(cmode, 1, 3);
8170 int cmode_0 = extract32(cmode, 0, 1);
8171 int o2 = extract32(insn, 11, 1);
8172 uint64_t abcdefgh = extract32(insn, 5, 5) | (extract32(insn, 16, 3) << 5);
8173 bool is_neg = extract32(insn, 29, 1);
8174 bool is_q = extract32(insn, 30, 1);
8175 uint64_t imm = 0;
8177 if (o2 != 0 || ((cmode == 0xf) && is_neg && !is_q)) {
8178 /* Check for FMOV (vector, immediate) - half-precision */
8179 if (!(dc_isar_feature(aa64_fp16, s) && o2 && cmode == 0xf)) {
8180 unallocated_encoding(s);
8181 return;
8185 if (!fp_access_check(s)) {
8186 return;
8189 /* See AdvSIMDExpandImm() in ARM ARM */
8190 switch (cmode_3_1) {
8191 case 0: /* Replicate(Zeros(24):imm8, 2) */
8192 case 1: /* Replicate(Zeros(16):imm8:Zeros(8), 2) */
8193 case 2: /* Replicate(Zeros(8):imm8:Zeros(16), 2) */
8194 case 3: /* Replicate(imm8:Zeros(24), 2) */
8196 int shift = cmode_3_1 * 8;
8197 imm = bitfield_replicate(abcdefgh << shift, 32);
8198 break;
8200 case 4: /* Replicate(Zeros(8):imm8, 4) */
8201 case 5: /* Replicate(imm8:Zeros(8), 4) */
8203 int shift = (cmode_3_1 & 0x1) * 8;
8204 imm = bitfield_replicate(abcdefgh << shift, 16);
8205 break;
8207 case 6:
8208 if (cmode_0) {
8209 /* Replicate(Zeros(8):imm8:Ones(16), 2) */
8210 imm = (abcdefgh << 16) | 0xffff;
8211 } else {
8212 /* Replicate(Zeros(16):imm8:Ones(8), 2) */
8213 imm = (abcdefgh << 8) | 0xff;
8215 imm = bitfield_replicate(imm, 32);
8216 break;
8217 case 7:
8218 if (!cmode_0 && !is_neg) {
8219 imm = bitfield_replicate(abcdefgh, 8);
8220 } else if (!cmode_0 && is_neg) {
8221 int i;
8222 imm = 0;
8223 for (i = 0; i < 8; i++) {
8224 if ((abcdefgh) & (1 << i)) {
8225 imm |= 0xffULL << (i * 8);
8228 } else if (cmode_0) {
8229 if (is_neg) {
8230 imm = (abcdefgh & 0x3f) << 48;
8231 if (abcdefgh & 0x80) {
8232 imm |= 0x8000000000000000ULL;
8234 if (abcdefgh & 0x40) {
8235 imm |= 0x3fc0000000000000ULL;
8236 } else {
8237 imm |= 0x4000000000000000ULL;
8239 } else {
8240 if (o2) {
8241 /* FMOV (vector, immediate) - half-precision */
8242 imm = vfp_expand_imm(MO_16, abcdefgh);
8243 /* now duplicate across the lanes */
8244 imm = bitfield_replicate(imm, 16);
8245 } else {
8246 imm = (abcdefgh & 0x3f) << 19;
8247 if (abcdefgh & 0x80) {
8248 imm |= 0x80000000;
8250 if (abcdefgh & 0x40) {
8251 imm |= 0x3e000000;
8252 } else {
8253 imm |= 0x40000000;
8255 imm |= (imm << 32);
8259 break;
8260 default:
8261 fprintf(stderr, "%s: cmode_3_1: %x\n", __func__, cmode_3_1);
8262 g_assert_not_reached();
8265 if (cmode_3_1 != 7 && is_neg) {
8266 imm = ~imm;
8269 if (!((cmode & 0x9) == 0x1 || (cmode & 0xd) == 0x9)) {
8270 /* MOVI or MVNI, with MVNI negation handled above. */
8271 tcg_gen_gvec_dup_imm(MO_64, vec_full_reg_offset(s, rd), is_q ? 16 : 8,
8272 vec_full_reg_size(s), imm);
8273 } else {
8274 /* ORR or BIC, with BIC negation to AND handled above. */
8275 if (is_neg) {
8276 gen_gvec_fn2i(s, is_q, rd, rd, imm, tcg_gen_gvec_andi, MO_64);
8277 } else {
8278 gen_gvec_fn2i(s, is_q, rd, rd, imm, tcg_gen_gvec_ori, MO_64);
8283 /* AdvSIMD scalar copy
8284 * 31 30 29 28 21 20 16 15 14 11 10 9 5 4 0
8285 * +-----+----+-----------------+------+---+------+---+------+------+
8286 * | 0 1 | op | 1 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 | Rn | Rd |
8287 * +-----+----+-----------------+------+---+------+---+------+------+
8289 static void disas_simd_scalar_copy(DisasContext *s, uint32_t insn)
8291 int rd = extract32(insn, 0, 5);
8292 int rn = extract32(insn, 5, 5);
8293 int imm4 = extract32(insn, 11, 4);
8294 int imm5 = extract32(insn, 16, 5);
8295 int op = extract32(insn, 29, 1);
8297 if (op != 0 || imm4 != 0) {
8298 unallocated_encoding(s);
8299 return;
8302 /* DUP (element, scalar) */
8303 handle_simd_dupes(s, rd, rn, imm5);
8306 /* AdvSIMD scalar pairwise
8307 * 31 30 29 28 24 23 22 21 17 16 12 11 10 9 5 4 0
8308 * +-----+---+-----------+------+-----------+--------+-----+------+------+
8309 * | 0 1 | U | 1 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 | Rn | Rd |
8310 * +-----+---+-----------+------+-----------+--------+-----+------+------+
8312 static void disas_simd_scalar_pairwise(DisasContext *s, uint32_t insn)
8314 int u = extract32(insn, 29, 1);
8315 int size = extract32(insn, 22, 2);
8316 int opcode = extract32(insn, 12, 5);
8317 int rn = extract32(insn, 5, 5);
8318 int rd = extract32(insn, 0, 5);
8319 TCGv_ptr fpst;
8321 /* For some ops (the FP ones), size[1] is part of the encoding.
8322 * For ADDP strictly it is not but size[1] is always 1 for valid
8323 * encodings.
8325 opcode |= (extract32(size, 1, 1) << 5);
8327 switch (opcode) {
8328 case 0x3b: /* ADDP */
8329 if (u || size != 3) {
8330 unallocated_encoding(s);
8331 return;
8333 if (!fp_access_check(s)) {
8334 return;
8337 fpst = NULL;
8338 break;
8339 case 0xc: /* FMAXNMP */
8340 case 0xd: /* FADDP */
8341 case 0xf: /* FMAXP */
8342 case 0x2c: /* FMINNMP */
8343 case 0x2f: /* FMINP */
8344 /* FP op, size[0] is 32 or 64 bit*/
8345 if (!u) {
8346 if (!dc_isar_feature(aa64_fp16, s)) {
8347 unallocated_encoding(s);
8348 return;
8349 } else {
8350 size = MO_16;
8352 } else {
8353 size = extract32(size, 0, 1) ? MO_64 : MO_32;
8356 if (!fp_access_check(s)) {
8357 return;
8360 fpst = get_fpstatus_ptr(size == MO_16);
8361 break;
8362 default:
8363 unallocated_encoding(s);
8364 return;
8367 if (size == MO_64) {
8368 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8369 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
8370 TCGv_i64 tcg_res = tcg_temp_new_i64();
8372 read_vec_element(s, tcg_op1, rn, 0, MO_64);
8373 read_vec_element(s, tcg_op2, rn, 1, MO_64);
8375 switch (opcode) {
8376 case 0x3b: /* ADDP */
8377 tcg_gen_add_i64(tcg_res, tcg_op1, tcg_op2);
8378 break;
8379 case 0xc: /* FMAXNMP */
8380 gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
8381 break;
8382 case 0xd: /* FADDP */
8383 gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
8384 break;
8385 case 0xf: /* FMAXP */
8386 gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
8387 break;
8388 case 0x2c: /* FMINNMP */
8389 gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
8390 break;
8391 case 0x2f: /* FMINP */
8392 gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
8393 break;
8394 default:
8395 g_assert_not_reached();
8398 write_fp_dreg(s, rd, tcg_res);
8400 tcg_temp_free_i64(tcg_op1);
8401 tcg_temp_free_i64(tcg_op2);
8402 tcg_temp_free_i64(tcg_res);
8403 } else {
8404 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
8405 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
8406 TCGv_i32 tcg_res = tcg_temp_new_i32();
8408 read_vec_element_i32(s, tcg_op1, rn, 0, size);
8409 read_vec_element_i32(s, tcg_op2, rn, 1, size);
8411 if (size == MO_16) {
8412 switch (opcode) {
8413 case 0xc: /* FMAXNMP */
8414 gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
8415 break;
8416 case 0xd: /* FADDP */
8417 gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
8418 break;
8419 case 0xf: /* FMAXP */
8420 gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
8421 break;
8422 case 0x2c: /* FMINNMP */
8423 gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
8424 break;
8425 case 0x2f: /* FMINP */
8426 gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
8427 break;
8428 default:
8429 g_assert_not_reached();
8431 } else {
8432 switch (opcode) {
8433 case 0xc: /* FMAXNMP */
8434 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
8435 break;
8436 case 0xd: /* FADDP */
8437 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
8438 break;
8439 case 0xf: /* FMAXP */
8440 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
8441 break;
8442 case 0x2c: /* FMINNMP */
8443 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
8444 break;
8445 case 0x2f: /* FMINP */
8446 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
8447 break;
8448 default:
8449 g_assert_not_reached();
8453 write_fp_sreg(s, rd, tcg_res);
8455 tcg_temp_free_i32(tcg_op1);
8456 tcg_temp_free_i32(tcg_op2);
8457 tcg_temp_free_i32(tcg_res);
8460 if (fpst) {
8461 tcg_temp_free_ptr(fpst);
8466 * Common SSHR[RA]/USHR[RA] - Shift right (optional rounding/accumulate)
8468 * This code is handles the common shifting code and is used by both
8469 * the vector and scalar code.
8471 static void handle_shri_with_rndacc(TCGv_i64 tcg_res, TCGv_i64 tcg_src,
8472 TCGv_i64 tcg_rnd, bool accumulate,
8473 bool is_u, int size, int shift)
8475 bool extended_result = false;
8476 bool round = tcg_rnd != NULL;
8477 int ext_lshift = 0;
8478 TCGv_i64 tcg_src_hi;
8480 if (round && size == 3) {
8481 extended_result = true;
8482 ext_lshift = 64 - shift;
8483 tcg_src_hi = tcg_temp_new_i64();
8484 } else if (shift == 64) {
8485 if (!accumulate && is_u) {
8486 /* result is zero */
8487 tcg_gen_movi_i64(tcg_res, 0);
8488 return;
8492 /* Deal with the rounding step */
8493 if (round) {
8494 if (extended_result) {
8495 TCGv_i64 tcg_zero = tcg_const_i64(0);
8496 if (!is_u) {
8497 /* take care of sign extending tcg_res */
8498 tcg_gen_sari_i64(tcg_src_hi, tcg_src, 63);
8499 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
8500 tcg_src, tcg_src_hi,
8501 tcg_rnd, tcg_zero);
8502 } else {
8503 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
8504 tcg_src, tcg_zero,
8505 tcg_rnd, tcg_zero);
8507 tcg_temp_free_i64(tcg_zero);
8508 } else {
8509 tcg_gen_add_i64(tcg_src, tcg_src, tcg_rnd);
8513 /* Now do the shift right */
8514 if (round && extended_result) {
8515 /* extended case, >64 bit precision required */
8516 if (ext_lshift == 0) {
8517 /* special case, only high bits matter */
8518 tcg_gen_mov_i64(tcg_src, tcg_src_hi);
8519 } else {
8520 tcg_gen_shri_i64(tcg_src, tcg_src, shift);
8521 tcg_gen_shli_i64(tcg_src_hi, tcg_src_hi, ext_lshift);
8522 tcg_gen_or_i64(tcg_src, tcg_src, tcg_src_hi);
8524 } else {
8525 if (is_u) {
8526 if (shift == 64) {
8527 /* essentially shifting in 64 zeros */
8528 tcg_gen_movi_i64(tcg_src, 0);
8529 } else {
8530 tcg_gen_shri_i64(tcg_src, tcg_src, shift);
8532 } else {
8533 if (shift == 64) {
8534 /* effectively extending the sign-bit */
8535 tcg_gen_sari_i64(tcg_src, tcg_src, 63);
8536 } else {
8537 tcg_gen_sari_i64(tcg_src, tcg_src, shift);
8542 if (accumulate) {
8543 tcg_gen_add_i64(tcg_res, tcg_res, tcg_src);
8544 } else {
8545 tcg_gen_mov_i64(tcg_res, tcg_src);
8548 if (extended_result) {
8549 tcg_temp_free_i64(tcg_src_hi);
8553 /* SSHR[RA]/USHR[RA] - Scalar shift right (optional rounding/accumulate) */
8554 static void handle_scalar_simd_shri(DisasContext *s,
8555 bool is_u, int immh, int immb,
8556 int opcode, int rn, int rd)
8558 const int size = 3;
8559 int immhb = immh << 3 | immb;
8560 int shift = 2 * (8 << size) - immhb;
8561 bool accumulate = false;
8562 bool round = false;
8563 bool insert = false;
8564 TCGv_i64 tcg_rn;
8565 TCGv_i64 tcg_rd;
8566 TCGv_i64 tcg_round;
8568 if (!extract32(immh, 3, 1)) {
8569 unallocated_encoding(s);
8570 return;
8573 if (!fp_access_check(s)) {
8574 return;
8577 switch (opcode) {
8578 case 0x02: /* SSRA / USRA (accumulate) */
8579 accumulate = true;
8580 break;
8581 case 0x04: /* SRSHR / URSHR (rounding) */
8582 round = true;
8583 break;
8584 case 0x06: /* SRSRA / URSRA (accum + rounding) */
8585 accumulate = round = true;
8586 break;
8587 case 0x08: /* SRI */
8588 insert = true;
8589 break;
8592 if (round) {
8593 uint64_t round_const = 1ULL << (shift - 1);
8594 tcg_round = tcg_const_i64(round_const);
8595 } else {
8596 tcg_round = NULL;
8599 tcg_rn = read_fp_dreg(s, rn);
8600 tcg_rd = (accumulate || insert) ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
8602 if (insert) {
8603 /* shift count same as element size is valid but does nothing;
8604 * special case to avoid potential shift by 64.
8606 int esize = 8 << size;
8607 if (shift != esize) {
8608 tcg_gen_shri_i64(tcg_rn, tcg_rn, shift);
8609 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, 0, esize - shift);
8611 } else {
8612 handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
8613 accumulate, is_u, size, shift);
8616 write_fp_dreg(s, rd, tcg_rd);
8618 tcg_temp_free_i64(tcg_rn);
8619 tcg_temp_free_i64(tcg_rd);
8620 if (round) {
8621 tcg_temp_free_i64(tcg_round);
8625 /* SHL/SLI - Scalar shift left */
8626 static void handle_scalar_simd_shli(DisasContext *s, bool insert,
8627 int immh, int immb, int opcode,
8628 int rn, int rd)
8630 int size = 32 - clz32(immh) - 1;
8631 int immhb = immh << 3 | immb;
8632 int shift = immhb - (8 << size);
8633 TCGv_i64 tcg_rn = new_tmp_a64(s);
8634 TCGv_i64 tcg_rd = new_tmp_a64(s);
8636 if (!extract32(immh, 3, 1)) {
8637 unallocated_encoding(s);
8638 return;
8641 if (!fp_access_check(s)) {
8642 return;
8645 tcg_rn = read_fp_dreg(s, rn);
8646 tcg_rd = insert ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
8648 if (insert) {
8649 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, shift, 64 - shift);
8650 } else {
8651 tcg_gen_shli_i64(tcg_rd, tcg_rn, shift);
8654 write_fp_dreg(s, rd, tcg_rd);
8656 tcg_temp_free_i64(tcg_rn);
8657 tcg_temp_free_i64(tcg_rd);
8660 /* SQSHRN/SQSHRUN - Saturating (signed/unsigned) shift right with
8661 * (signed/unsigned) narrowing */
8662 static void handle_vec_simd_sqshrn(DisasContext *s, bool is_scalar, bool is_q,
8663 bool is_u_shift, bool is_u_narrow,
8664 int immh, int immb, int opcode,
8665 int rn, int rd)
8667 int immhb = immh << 3 | immb;
8668 int size = 32 - clz32(immh) - 1;
8669 int esize = 8 << size;
8670 int shift = (2 * esize) - immhb;
8671 int elements = is_scalar ? 1 : (64 / esize);
8672 bool round = extract32(opcode, 0, 1);
8673 MemOp ldop = (size + 1) | (is_u_shift ? 0 : MO_SIGN);
8674 TCGv_i64 tcg_rn, tcg_rd, tcg_round;
8675 TCGv_i32 tcg_rd_narrowed;
8676 TCGv_i64 tcg_final;
8678 static NeonGenNarrowEnvFn * const signed_narrow_fns[4][2] = {
8679 { gen_helper_neon_narrow_sat_s8,
8680 gen_helper_neon_unarrow_sat8 },
8681 { gen_helper_neon_narrow_sat_s16,
8682 gen_helper_neon_unarrow_sat16 },
8683 { gen_helper_neon_narrow_sat_s32,
8684 gen_helper_neon_unarrow_sat32 },
8685 { NULL, NULL },
8687 static NeonGenNarrowEnvFn * const unsigned_narrow_fns[4] = {
8688 gen_helper_neon_narrow_sat_u8,
8689 gen_helper_neon_narrow_sat_u16,
8690 gen_helper_neon_narrow_sat_u32,
8691 NULL
8693 NeonGenNarrowEnvFn *narrowfn;
8695 int i;
8697 assert(size < 4);
8699 if (extract32(immh, 3, 1)) {
8700 unallocated_encoding(s);
8701 return;
8704 if (!fp_access_check(s)) {
8705 return;
8708 if (is_u_shift) {
8709 narrowfn = unsigned_narrow_fns[size];
8710 } else {
8711 narrowfn = signed_narrow_fns[size][is_u_narrow ? 1 : 0];
8714 tcg_rn = tcg_temp_new_i64();
8715 tcg_rd = tcg_temp_new_i64();
8716 tcg_rd_narrowed = tcg_temp_new_i32();
8717 tcg_final = tcg_const_i64(0);
8719 if (round) {
8720 uint64_t round_const = 1ULL << (shift - 1);
8721 tcg_round = tcg_const_i64(round_const);
8722 } else {
8723 tcg_round = NULL;
8726 for (i = 0; i < elements; i++) {
8727 read_vec_element(s, tcg_rn, rn, i, ldop);
8728 handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
8729 false, is_u_shift, size+1, shift);
8730 narrowfn(tcg_rd_narrowed, cpu_env, tcg_rd);
8731 tcg_gen_extu_i32_i64(tcg_rd, tcg_rd_narrowed);
8732 tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
8735 if (!is_q) {
8736 write_vec_element(s, tcg_final, rd, 0, MO_64);
8737 } else {
8738 write_vec_element(s, tcg_final, rd, 1, MO_64);
8741 if (round) {
8742 tcg_temp_free_i64(tcg_round);
8744 tcg_temp_free_i64(tcg_rn);
8745 tcg_temp_free_i64(tcg_rd);
8746 tcg_temp_free_i32(tcg_rd_narrowed);
8747 tcg_temp_free_i64(tcg_final);
8749 clear_vec_high(s, is_q, rd);
8752 /* SQSHLU, UQSHL, SQSHL: saturating left shifts */
8753 static void handle_simd_qshl(DisasContext *s, bool scalar, bool is_q,
8754 bool src_unsigned, bool dst_unsigned,
8755 int immh, int immb, int rn, int rd)
8757 int immhb = immh << 3 | immb;
8758 int size = 32 - clz32(immh) - 1;
8759 int shift = immhb - (8 << size);
8760 int pass;
8762 assert(immh != 0);
8763 assert(!(scalar && is_q));
8765 if (!scalar) {
8766 if (!is_q && extract32(immh, 3, 1)) {
8767 unallocated_encoding(s);
8768 return;
8771 /* Since we use the variable-shift helpers we must
8772 * replicate the shift count into each element of
8773 * the tcg_shift value.
8775 switch (size) {
8776 case 0:
8777 shift |= shift << 8;
8778 /* fall through */
8779 case 1:
8780 shift |= shift << 16;
8781 break;
8782 case 2:
8783 case 3:
8784 break;
8785 default:
8786 g_assert_not_reached();
8790 if (!fp_access_check(s)) {
8791 return;
8794 if (size == 3) {
8795 TCGv_i64 tcg_shift = tcg_const_i64(shift);
8796 static NeonGenTwo64OpEnvFn * const fns[2][2] = {
8797 { gen_helper_neon_qshl_s64, gen_helper_neon_qshlu_s64 },
8798 { NULL, gen_helper_neon_qshl_u64 },
8800 NeonGenTwo64OpEnvFn *genfn = fns[src_unsigned][dst_unsigned];
8801 int maxpass = is_q ? 2 : 1;
8803 for (pass = 0; pass < maxpass; pass++) {
8804 TCGv_i64 tcg_op = tcg_temp_new_i64();
8806 read_vec_element(s, tcg_op, rn, pass, MO_64);
8807 genfn(tcg_op, cpu_env, tcg_op, tcg_shift);
8808 write_vec_element(s, tcg_op, rd, pass, MO_64);
8810 tcg_temp_free_i64(tcg_op);
8812 tcg_temp_free_i64(tcg_shift);
8813 clear_vec_high(s, is_q, rd);
8814 } else {
8815 TCGv_i32 tcg_shift = tcg_const_i32(shift);
8816 static NeonGenTwoOpEnvFn * const fns[2][2][3] = {
8818 { gen_helper_neon_qshl_s8,
8819 gen_helper_neon_qshl_s16,
8820 gen_helper_neon_qshl_s32 },
8821 { gen_helper_neon_qshlu_s8,
8822 gen_helper_neon_qshlu_s16,
8823 gen_helper_neon_qshlu_s32 }
8824 }, {
8825 { NULL, NULL, NULL },
8826 { gen_helper_neon_qshl_u8,
8827 gen_helper_neon_qshl_u16,
8828 gen_helper_neon_qshl_u32 }
8831 NeonGenTwoOpEnvFn *genfn = fns[src_unsigned][dst_unsigned][size];
8832 MemOp memop = scalar ? size : MO_32;
8833 int maxpass = scalar ? 1 : is_q ? 4 : 2;
8835 for (pass = 0; pass < maxpass; pass++) {
8836 TCGv_i32 tcg_op = tcg_temp_new_i32();
8838 read_vec_element_i32(s, tcg_op, rn, pass, memop);
8839 genfn(tcg_op, cpu_env, tcg_op, tcg_shift);
8840 if (scalar) {
8841 switch (size) {
8842 case 0:
8843 tcg_gen_ext8u_i32(tcg_op, tcg_op);
8844 break;
8845 case 1:
8846 tcg_gen_ext16u_i32(tcg_op, tcg_op);
8847 break;
8848 case 2:
8849 break;
8850 default:
8851 g_assert_not_reached();
8853 write_fp_sreg(s, rd, tcg_op);
8854 } else {
8855 write_vec_element_i32(s, tcg_op, rd, pass, MO_32);
8858 tcg_temp_free_i32(tcg_op);
8860 tcg_temp_free_i32(tcg_shift);
8862 if (!scalar) {
8863 clear_vec_high(s, is_q, rd);
8868 /* Common vector code for handling integer to FP conversion */
8869 static void handle_simd_intfp_conv(DisasContext *s, int rd, int rn,
8870 int elements, int is_signed,
8871 int fracbits, int size)
8873 TCGv_ptr tcg_fpst = get_fpstatus_ptr(size == MO_16);
8874 TCGv_i32 tcg_shift = NULL;
8876 MemOp mop = size | (is_signed ? MO_SIGN : 0);
8877 int pass;
8879 if (fracbits || size == MO_64) {
8880 tcg_shift = tcg_const_i32(fracbits);
8883 if (size == MO_64) {
8884 TCGv_i64 tcg_int64 = tcg_temp_new_i64();
8885 TCGv_i64 tcg_double = tcg_temp_new_i64();
8887 for (pass = 0; pass < elements; pass++) {
8888 read_vec_element(s, tcg_int64, rn, pass, mop);
8890 if (is_signed) {
8891 gen_helper_vfp_sqtod(tcg_double, tcg_int64,
8892 tcg_shift, tcg_fpst);
8893 } else {
8894 gen_helper_vfp_uqtod(tcg_double, tcg_int64,
8895 tcg_shift, tcg_fpst);
8897 if (elements == 1) {
8898 write_fp_dreg(s, rd, tcg_double);
8899 } else {
8900 write_vec_element(s, tcg_double, rd, pass, MO_64);
8904 tcg_temp_free_i64(tcg_int64);
8905 tcg_temp_free_i64(tcg_double);
8907 } else {
8908 TCGv_i32 tcg_int32 = tcg_temp_new_i32();
8909 TCGv_i32 tcg_float = tcg_temp_new_i32();
8911 for (pass = 0; pass < elements; pass++) {
8912 read_vec_element_i32(s, tcg_int32, rn, pass, mop);
8914 switch (size) {
8915 case MO_32:
8916 if (fracbits) {
8917 if (is_signed) {
8918 gen_helper_vfp_sltos(tcg_float, tcg_int32,
8919 tcg_shift, tcg_fpst);
8920 } else {
8921 gen_helper_vfp_ultos(tcg_float, tcg_int32,
8922 tcg_shift, tcg_fpst);
8924 } else {
8925 if (is_signed) {
8926 gen_helper_vfp_sitos(tcg_float, tcg_int32, tcg_fpst);
8927 } else {
8928 gen_helper_vfp_uitos(tcg_float, tcg_int32, tcg_fpst);
8931 break;
8932 case MO_16:
8933 if (fracbits) {
8934 if (is_signed) {
8935 gen_helper_vfp_sltoh(tcg_float, tcg_int32,
8936 tcg_shift, tcg_fpst);
8937 } else {
8938 gen_helper_vfp_ultoh(tcg_float, tcg_int32,
8939 tcg_shift, tcg_fpst);
8941 } else {
8942 if (is_signed) {
8943 gen_helper_vfp_sitoh(tcg_float, tcg_int32, tcg_fpst);
8944 } else {
8945 gen_helper_vfp_uitoh(tcg_float, tcg_int32, tcg_fpst);
8948 break;
8949 default:
8950 g_assert_not_reached();
8953 if (elements == 1) {
8954 write_fp_sreg(s, rd, tcg_float);
8955 } else {
8956 write_vec_element_i32(s, tcg_float, rd, pass, size);
8960 tcg_temp_free_i32(tcg_int32);
8961 tcg_temp_free_i32(tcg_float);
8964 tcg_temp_free_ptr(tcg_fpst);
8965 if (tcg_shift) {
8966 tcg_temp_free_i32(tcg_shift);
8969 clear_vec_high(s, elements << size == 16, rd);
8972 /* UCVTF/SCVTF - Integer to FP conversion */
8973 static void handle_simd_shift_intfp_conv(DisasContext *s, bool is_scalar,
8974 bool is_q, bool is_u,
8975 int immh, int immb, int opcode,
8976 int rn, int rd)
8978 int size, elements, fracbits;
8979 int immhb = immh << 3 | immb;
8981 if (immh & 8) {
8982 size = MO_64;
8983 if (!is_scalar && !is_q) {
8984 unallocated_encoding(s);
8985 return;
8987 } else if (immh & 4) {
8988 size = MO_32;
8989 } else if (immh & 2) {
8990 size = MO_16;
8991 if (!dc_isar_feature(aa64_fp16, s)) {
8992 unallocated_encoding(s);
8993 return;
8995 } else {
8996 /* immh == 0 would be a failure of the decode logic */
8997 g_assert(immh == 1);
8998 unallocated_encoding(s);
8999 return;
9002 if (is_scalar) {
9003 elements = 1;
9004 } else {
9005 elements = (8 << is_q) >> size;
9007 fracbits = (16 << size) - immhb;
9009 if (!fp_access_check(s)) {
9010 return;
9013 handle_simd_intfp_conv(s, rd, rn, elements, !is_u, fracbits, size);
9016 /* FCVTZS, FVCVTZU - FP to fixedpoint conversion */
9017 static void handle_simd_shift_fpint_conv(DisasContext *s, bool is_scalar,
9018 bool is_q, bool is_u,
9019 int immh, int immb, int rn, int rd)
9021 int immhb = immh << 3 | immb;
9022 int pass, size, fracbits;
9023 TCGv_ptr tcg_fpstatus;
9024 TCGv_i32 tcg_rmode, tcg_shift;
9026 if (immh & 0x8) {
9027 size = MO_64;
9028 if (!is_scalar && !is_q) {
9029 unallocated_encoding(s);
9030 return;
9032 } else if (immh & 0x4) {
9033 size = MO_32;
9034 } else if (immh & 0x2) {
9035 size = MO_16;
9036 if (!dc_isar_feature(aa64_fp16, s)) {
9037 unallocated_encoding(s);
9038 return;
9040 } else {
9041 /* Should have split out AdvSIMD modified immediate earlier. */
9042 assert(immh == 1);
9043 unallocated_encoding(s);
9044 return;
9047 if (!fp_access_check(s)) {
9048 return;
9051 assert(!(is_scalar && is_q));
9053 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(FPROUNDING_ZERO));
9054 tcg_fpstatus = get_fpstatus_ptr(size == MO_16);
9055 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
9056 fracbits = (16 << size) - immhb;
9057 tcg_shift = tcg_const_i32(fracbits);
9059 if (size == MO_64) {
9060 int maxpass = is_scalar ? 1 : 2;
9062 for (pass = 0; pass < maxpass; pass++) {
9063 TCGv_i64 tcg_op = tcg_temp_new_i64();
9065 read_vec_element(s, tcg_op, rn, pass, MO_64);
9066 if (is_u) {
9067 gen_helper_vfp_touqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
9068 } else {
9069 gen_helper_vfp_tosqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
9071 write_vec_element(s, tcg_op, rd, pass, MO_64);
9072 tcg_temp_free_i64(tcg_op);
9074 clear_vec_high(s, is_q, rd);
9075 } else {
9076 void (*fn)(TCGv_i32, TCGv_i32, TCGv_i32, TCGv_ptr);
9077 int maxpass = is_scalar ? 1 : ((8 << is_q) >> size);
9079 switch (size) {
9080 case MO_16:
9081 if (is_u) {
9082 fn = gen_helper_vfp_touhh;
9083 } else {
9084 fn = gen_helper_vfp_toshh;
9086 break;
9087 case MO_32:
9088 if (is_u) {
9089 fn = gen_helper_vfp_touls;
9090 } else {
9091 fn = gen_helper_vfp_tosls;
9093 break;
9094 default:
9095 g_assert_not_reached();
9098 for (pass = 0; pass < maxpass; pass++) {
9099 TCGv_i32 tcg_op = tcg_temp_new_i32();
9101 read_vec_element_i32(s, tcg_op, rn, pass, size);
9102 fn(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
9103 if (is_scalar) {
9104 write_fp_sreg(s, rd, tcg_op);
9105 } else {
9106 write_vec_element_i32(s, tcg_op, rd, pass, size);
9108 tcg_temp_free_i32(tcg_op);
9110 if (!is_scalar) {
9111 clear_vec_high(s, is_q, rd);
9115 tcg_temp_free_ptr(tcg_fpstatus);
9116 tcg_temp_free_i32(tcg_shift);
9117 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
9118 tcg_temp_free_i32(tcg_rmode);
9121 /* AdvSIMD scalar shift by immediate
9122 * 31 30 29 28 23 22 19 18 16 15 11 10 9 5 4 0
9123 * +-----+---+-------------+------+------+--------+---+------+------+
9124 * | 0 1 | U | 1 1 1 1 1 0 | immh | immb | opcode | 1 | Rn | Rd |
9125 * +-----+---+-------------+------+------+--------+---+------+------+
9127 * This is the scalar version so it works on a fixed sized registers
9129 static void disas_simd_scalar_shift_imm(DisasContext *s, uint32_t insn)
9131 int rd = extract32(insn, 0, 5);
9132 int rn = extract32(insn, 5, 5);
9133 int opcode = extract32(insn, 11, 5);
9134 int immb = extract32(insn, 16, 3);
9135 int immh = extract32(insn, 19, 4);
9136 bool is_u = extract32(insn, 29, 1);
9138 if (immh == 0) {
9139 unallocated_encoding(s);
9140 return;
9143 switch (opcode) {
9144 case 0x08: /* SRI */
9145 if (!is_u) {
9146 unallocated_encoding(s);
9147 return;
9149 /* fall through */
9150 case 0x00: /* SSHR / USHR */
9151 case 0x02: /* SSRA / USRA */
9152 case 0x04: /* SRSHR / URSHR */
9153 case 0x06: /* SRSRA / URSRA */
9154 handle_scalar_simd_shri(s, is_u, immh, immb, opcode, rn, rd);
9155 break;
9156 case 0x0a: /* SHL / SLI */
9157 handle_scalar_simd_shli(s, is_u, immh, immb, opcode, rn, rd);
9158 break;
9159 case 0x1c: /* SCVTF, UCVTF */
9160 handle_simd_shift_intfp_conv(s, true, false, is_u, immh, immb,
9161 opcode, rn, rd);
9162 break;
9163 case 0x10: /* SQSHRUN, SQSHRUN2 */
9164 case 0x11: /* SQRSHRUN, SQRSHRUN2 */
9165 if (!is_u) {
9166 unallocated_encoding(s);
9167 return;
9169 handle_vec_simd_sqshrn(s, true, false, false, true,
9170 immh, immb, opcode, rn, rd);
9171 break;
9172 case 0x12: /* SQSHRN, SQSHRN2, UQSHRN */
9173 case 0x13: /* SQRSHRN, SQRSHRN2, UQRSHRN, UQRSHRN2 */
9174 handle_vec_simd_sqshrn(s, true, false, is_u, is_u,
9175 immh, immb, opcode, rn, rd);
9176 break;
9177 case 0xc: /* SQSHLU */
9178 if (!is_u) {
9179 unallocated_encoding(s);
9180 return;
9182 handle_simd_qshl(s, true, false, false, true, immh, immb, rn, rd);
9183 break;
9184 case 0xe: /* SQSHL, UQSHL */
9185 handle_simd_qshl(s, true, false, is_u, is_u, immh, immb, rn, rd);
9186 break;
9187 case 0x1f: /* FCVTZS, FCVTZU */
9188 handle_simd_shift_fpint_conv(s, true, false, is_u, immh, immb, rn, rd);
9189 break;
9190 default:
9191 unallocated_encoding(s);
9192 break;
9196 /* AdvSIMD scalar three different
9197 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 0
9198 * +-----+---+-----------+------+---+------+--------+-----+------+------+
9199 * | 0 1 | U | 1 1 1 1 0 | size | 1 | Rm | opcode | 0 0 | Rn | Rd |
9200 * +-----+---+-----------+------+---+------+--------+-----+------+------+
9202 static void disas_simd_scalar_three_reg_diff(DisasContext *s, uint32_t insn)
9204 bool is_u = extract32(insn, 29, 1);
9205 int size = extract32(insn, 22, 2);
9206 int opcode = extract32(insn, 12, 4);
9207 int rm = extract32(insn, 16, 5);
9208 int rn = extract32(insn, 5, 5);
9209 int rd = extract32(insn, 0, 5);
9211 if (is_u) {
9212 unallocated_encoding(s);
9213 return;
9216 switch (opcode) {
9217 case 0x9: /* SQDMLAL, SQDMLAL2 */
9218 case 0xb: /* SQDMLSL, SQDMLSL2 */
9219 case 0xd: /* SQDMULL, SQDMULL2 */
9220 if (size == 0 || size == 3) {
9221 unallocated_encoding(s);
9222 return;
9224 break;
9225 default:
9226 unallocated_encoding(s);
9227 return;
9230 if (!fp_access_check(s)) {
9231 return;
9234 if (size == 2) {
9235 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
9236 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
9237 TCGv_i64 tcg_res = tcg_temp_new_i64();
9239 read_vec_element(s, tcg_op1, rn, 0, MO_32 | MO_SIGN);
9240 read_vec_element(s, tcg_op2, rm, 0, MO_32 | MO_SIGN);
9242 tcg_gen_mul_i64(tcg_res, tcg_op1, tcg_op2);
9243 gen_helper_neon_addl_saturate_s64(tcg_res, cpu_env, tcg_res, tcg_res);
9245 switch (opcode) {
9246 case 0xd: /* SQDMULL, SQDMULL2 */
9247 break;
9248 case 0xb: /* SQDMLSL, SQDMLSL2 */
9249 tcg_gen_neg_i64(tcg_res, tcg_res);
9250 /* fall through */
9251 case 0x9: /* SQDMLAL, SQDMLAL2 */
9252 read_vec_element(s, tcg_op1, rd, 0, MO_64);
9253 gen_helper_neon_addl_saturate_s64(tcg_res, cpu_env,
9254 tcg_res, tcg_op1);
9255 break;
9256 default:
9257 g_assert_not_reached();
9260 write_fp_dreg(s, rd, tcg_res);
9262 tcg_temp_free_i64(tcg_op1);
9263 tcg_temp_free_i64(tcg_op2);
9264 tcg_temp_free_i64(tcg_res);
9265 } else {
9266 TCGv_i32 tcg_op1 = read_fp_hreg(s, rn);
9267 TCGv_i32 tcg_op2 = read_fp_hreg(s, rm);
9268 TCGv_i64 tcg_res = tcg_temp_new_i64();
9270 gen_helper_neon_mull_s16(tcg_res, tcg_op1, tcg_op2);
9271 gen_helper_neon_addl_saturate_s32(tcg_res, cpu_env, tcg_res, tcg_res);
9273 switch (opcode) {
9274 case 0xd: /* SQDMULL, SQDMULL2 */
9275 break;
9276 case 0xb: /* SQDMLSL, SQDMLSL2 */
9277 gen_helper_neon_negl_u32(tcg_res, tcg_res);
9278 /* fall through */
9279 case 0x9: /* SQDMLAL, SQDMLAL2 */
9281 TCGv_i64 tcg_op3 = tcg_temp_new_i64();
9282 read_vec_element(s, tcg_op3, rd, 0, MO_32);
9283 gen_helper_neon_addl_saturate_s32(tcg_res, cpu_env,
9284 tcg_res, tcg_op3);
9285 tcg_temp_free_i64(tcg_op3);
9286 break;
9288 default:
9289 g_assert_not_reached();
9292 tcg_gen_ext32u_i64(tcg_res, tcg_res);
9293 write_fp_dreg(s, rd, tcg_res);
9295 tcg_temp_free_i32(tcg_op1);
9296 tcg_temp_free_i32(tcg_op2);
9297 tcg_temp_free_i64(tcg_res);
9301 static void handle_3same_64(DisasContext *s, int opcode, bool u,
9302 TCGv_i64 tcg_rd, TCGv_i64 tcg_rn, TCGv_i64 tcg_rm)
9304 /* Handle 64x64->64 opcodes which are shared between the scalar
9305 * and vector 3-same groups. We cover every opcode where size == 3
9306 * is valid in either the three-reg-same (integer, not pairwise)
9307 * or scalar-three-reg-same groups.
9309 TCGCond cond;
9311 switch (opcode) {
9312 case 0x1: /* SQADD */
9313 if (u) {
9314 gen_helper_neon_qadd_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9315 } else {
9316 gen_helper_neon_qadd_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9318 break;
9319 case 0x5: /* SQSUB */
9320 if (u) {
9321 gen_helper_neon_qsub_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9322 } else {
9323 gen_helper_neon_qsub_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9325 break;
9326 case 0x6: /* CMGT, CMHI */
9327 /* 64 bit integer comparison, result = test ? (2^64 - 1) : 0.
9328 * We implement this using setcond (test) and then negating.
9330 cond = u ? TCG_COND_GTU : TCG_COND_GT;
9331 do_cmop:
9332 tcg_gen_setcond_i64(cond, tcg_rd, tcg_rn, tcg_rm);
9333 tcg_gen_neg_i64(tcg_rd, tcg_rd);
9334 break;
9335 case 0x7: /* CMGE, CMHS */
9336 cond = u ? TCG_COND_GEU : TCG_COND_GE;
9337 goto do_cmop;
9338 case 0x11: /* CMTST, CMEQ */
9339 if (u) {
9340 cond = TCG_COND_EQ;
9341 goto do_cmop;
9343 gen_cmtst_i64(tcg_rd, tcg_rn, tcg_rm);
9344 break;
9345 case 0x8: /* SSHL, USHL */
9346 if (u) {
9347 gen_ushl_i64(tcg_rd, tcg_rn, tcg_rm);
9348 } else {
9349 gen_sshl_i64(tcg_rd, tcg_rn, tcg_rm);
9351 break;
9352 case 0x9: /* SQSHL, UQSHL */
9353 if (u) {
9354 gen_helper_neon_qshl_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9355 } else {
9356 gen_helper_neon_qshl_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9358 break;
9359 case 0xa: /* SRSHL, URSHL */
9360 if (u) {
9361 gen_helper_neon_rshl_u64(tcg_rd, tcg_rn, tcg_rm);
9362 } else {
9363 gen_helper_neon_rshl_s64(tcg_rd, tcg_rn, tcg_rm);
9365 break;
9366 case 0xb: /* SQRSHL, UQRSHL */
9367 if (u) {
9368 gen_helper_neon_qrshl_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9369 } else {
9370 gen_helper_neon_qrshl_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9372 break;
9373 case 0x10: /* ADD, SUB */
9374 if (u) {
9375 tcg_gen_sub_i64(tcg_rd, tcg_rn, tcg_rm);
9376 } else {
9377 tcg_gen_add_i64(tcg_rd, tcg_rn, tcg_rm);
9379 break;
9380 default:
9381 g_assert_not_reached();
9385 /* Handle the 3-same-operands float operations; shared by the scalar
9386 * and vector encodings. The caller must filter out any encodings
9387 * not allocated for the encoding it is dealing with.
9389 static void handle_3same_float(DisasContext *s, int size, int elements,
9390 int fpopcode, int rd, int rn, int rm)
9392 int pass;
9393 TCGv_ptr fpst = get_fpstatus_ptr(false);
9395 for (pass = 0; pass < elements; pass++) {
9396 if (size) {
9397 /* Double */
9398 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
9399 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
9400 TCGv_i64 tcg_res = tcg_temp_new_i64();
9402 read_vec_element(s, tcg_op1, rn, pass, MO_64);
9403 read_vec_element(s, tcg_op2, rm, pass, MO_64);
9405 switch (fpopcode) {
9406 case 0x39: /* FMLS */
9407 /* As usual for ARM, separate negation for fused multiply-add */
9408 gen_helper_vfp_negd(tcg_op1, tcg_op1);
9409 /* fall through */
9410 case 0x19: /* FMLA */
9411 read_vec_element(s, tcg_res, rd, pass, MO_64);
9412 gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2,
9413 tcg_res, fpst);
9414 break;
9415 case 0x18: /* FMAXNM */
9416 gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
9417 break;
9418 case 0x1a: /* FADD */
9419 gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
9420 break;
9421 case 0x1b: /* FMULX */
9422 gen_helper_vfp_mulxd(tcg_res, tcg_op1, tcg_op2, fpst);
9423 break;
9424 case 0x1c: /* FCMEQ */
9425 gen_helper_neon_ceq_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9426 break;
9427 case 0x1e: /* FMAX */
9428 gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
9429 break;
9430 case 0x1f: /* FRECPS */
9431 gen_helper_recpsf_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9432 break;
9433 case 0x38: /* FMINNM */
9434 gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
9435 break;
9436 case 0x3a: /* FSUB */
9437 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
9438 break;
9439 case 0x3e: /* FMIN */
9440 gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
9441 break;
9442 case 0x3f: /* FRSQRTS */
9443 gen_helper_rsqrtsf_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9444 break;
9445 case 0x5b: /* FMUL */
9446 gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
9447 break;
9448 case 0x5c: /* FCMGE */
9449 gen_helper_neon_cge_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9450 break;
9451 case 0x5d: /* FACGE */
9452 gen_helper_neon_acge_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9453 break;
9454 case 0x5f: /* FDIV */
9455 gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
9456 break;
9457 case 0x7a: /* FABD */
9458 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
9459 gen_helper_vfp_absd(tcg_res, tcg_res);
9460 break;
9461 case 0x7c: /* FCMGT */
9462 gen_helper_neon_cgt_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9463 break;
9464 case 0x7d: /* FACGT */
9465 gen_helper_neon_acgt_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9466 break;
9467 default:
9468 g_assert_not_reached();
9471 write_vec_element(s, tcg_res, rd, pass, MO_64);
9473 tcg_temp_free_i64(tcg_res);
9474 tcg_temp_free_i64(tcg_op1);
9475 tcg_temp_free_i64(tcg_op2);
9476 } else {
9477 /* Single */
9478 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
9479 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
9480 TCGv_i32 tcg_res = tcg_temp_new_i32();
9482 read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
9483 read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
9485 switch (fpopcode) {
9486 case 0x39: /* FMLS */
9487 /* As usual for ARM, separate negation for fused multiply-add */
9488 gen_helper_vfp_negs(tcg_op1, tcg_op1);
9489 /* fall through */
9490 case 0x19: /* FMLA */
9491 read_vec_element_i32(s, tcg_res, rd, pass, MO_32);
9492 gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2,
9493 tcg_res, fpst);
9494 break;
9495 case 0x1a: /* FADD */
9496 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
9497 break;
9498 case 0x1b: /* FMULX */
9499 gen_helper_vfp_mulxs(tcg_res, tcg_op1, tcg_op2, fpst);
9500 break;
9501 case 0x1c: /* FCMEQ */
9502 gen_helper_neon_ceq_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9503 break;
9504 case 0x1e: /* FMAX */
9505 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
9506 break;
9507 case 0x1f: /* FRECPS */
9508 gen_helper_recpsf_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9509 break;
9510 case 0x18: /* FMAXNM */
9511 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
9512 break;
9513 case 0x38: /* FMINNM */
9514 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
9515 break;
9516 case 0x3a: /* FSUB */
9517 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
9518 break;
9519 case 0x3e: /* FMIN */
9520 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
9521 break;
9522 case 0x3f: /* FRSQRTS */
9523 gen_helper_rsqrtsf_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9524 break;
9525 case 0x5b: /* FMUL */
9526 gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
9527 break;
9528 case 0x5c: /* FCMGE */
9529 gen_helper_neon_cge_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9530 break;
9531 case 0x5d: /* FACGE */
9532 gen_helper_neon_acge_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9533 break;
9534 case 0x5f: /* FDIV */
9535 gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
9536 break;
9537 case 0x7a: /* FABD */
9538 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
9539 gen_helper_vfp_abss(tcg_res, tcg_res);
9540 break;
9541 case 0x7c: /* FCMGT */
9542 gen_helper_neon_cgt_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9543 break;
9544 case 0x7d: /* FACGT */
9545 gen_helper_neon_acgt_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9546 break;
9547 default:
9548 g_assert_not_reached();
9551 if (elements == 1) {
9552 /* scalar single so clear high part */
9553 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
9555 tcg_gen_extu_i32_i64(tcg_tmp, tcg_res);
9556 write_vec_element(s, tcg_tmp, rd, pass, MO_64);
9557 tcg_temp_free_i64(tcg_tmp);
9558 } else {
9559 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
9562 tcg_temp_free_i32(tcg_res);
9563 tcg_temp_free_i32(tcg_op1);
9564 tcg_temp_free_i32(tcg_op2);
9568 tcg_temp_free_ptr(fpst);
9570 clear_vec_high(s, elements * (size ? 8 : 4) > 8, rd);
9573 /* AdvSIMD scalar three same
9574 * 31 30 29 28 24 23 22 21 20 16 15 11 10 9 5 4 0
9575 * +-----+---+-----------+------+---+------+--------+---+------+------+
9576 * | 0 1 | U | 1 1 1 1 0 | size | 1 | Rm | opcode | 1 | Rn | Rd |
9577 * +-----+---+-----------+------+---+------+--------+---+------+------+
9579 static void disas_simd_scalar_three_reg_same(DisasContext *s, uint32_t insn)
9581 int rd = extract32(insn, 0, 5);
9582 int rn = extract32(insn, 5, 5);
9583 int opcode = extract32(insn, 11, 5);
9584 int rm = extract32(insn, 16, 5);
9585 int size = extract32(insn, 22, 2);
9586 bool u = extract32(insn, 29, 1);
9587 TCGv_i64 tcg_rd;
9589 if (opcode >= 0x18) {
9590 /* Floating point: U, size[1] and opcode indicate operation */
9591 int fpopcode = opcode | (extract32(size, 1, 1) << 5) | (u << 6);
9592 switch (fpopcode) {
9593 case 0x1b: /* FMULX */
9594 case 0x1f: /* FRECPS */
9595 case 0x3f: /* FRSQRTS */
9596 case 0x5d: /* FACGE */
9597 case 0x7d: /* FACGT */
9598 case 0x1c: /* FCMEQ */
9599 case 0x5c: /* FCMGE */
9600 case 0x7c: /* FCMGT */
9601 case 0x7a: /* FABD */
9602 break;
9603 default:
9604 unallocated_encoding(s);
9605 return;
9608 if (!fp_access_check(s)) {
9609 return;
9612 handle_3same_float(s, extract32(size, 0, 1), 1, fpopcode, rd, rn, rm);
9613 return;
9616 switch (opcode) {
9617 case 0x1: /* SQADD, UQADD */
9618 case 0x5: /* SQSUB, UQSUB */
9619 case 0x9: /* SQSHL, UQSHL */
9620 case 0xb: /* SQRSHL, UQRSHL */
9621 break;
9622 case 0x8: /* SSHL, USHL */
9623 case 0xa: /* SRSHL, URSHL */
9624 case 0x6: /* CMGT, CMHI */
9625 case 0x7: /* CMGE, CMHS */
9626 case 0x11: /* CMTST, CMEQ */
9627 case 0x10: /* ADD, SUB (vector) */
9628 if (size != 3) {
9629 unallocated_encoding(s);
9630 return;
9632 break;
9633 case 0x16: /* SQDMULH, SQRDMULH (vector) */
9634 if (size != 1 && size != 2) {
9635 unallocated_encoding(s);
9636 return;
9638 break;
9639 default:
9640 unallocated_encoding(s);
9641 return;
9644 if (!fp_access_check(s)) {
9645 return;
9648 tcg_rd = tcg_temp_new_i64();
9650 if (size == 3) {
9651 TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
9652 TCGv_i64 tcg_rm = read_fp_dreg(s, rm);
9654 handle_3same_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rm);
9655 tcg_temp_free_i64(tcg_rn);
9656 tcg_temp_free_i64(tcg_rm);
9657 } else {
9658 /* Do a single operation on the lowest element in the vector.
9659 * We use the standard Neon helpers and rely on 0 OP 0 == 0 with
9660 * no side effects for all these operations.
9661 * OPTME: special-purpose helpers would avoid doing some
9662 * unnecessary work in the helper for the 8 and 16 bit cases.
9664 NeonGenTwoOpEnvFn *genenvfn;
9665 TCGv_i32 tcg_rn = tcg_temp_new_i32();
9666 TCGv_i32 tcg_rm = tcg_temp_new_i32();
9667 TCGv_i32 tcg_rd32 = tcg_temp_new_i32();
9669 read_vec_element_i32(s, tcg_rn, rn, 0, size);
9670 read_vec_element_i32(s, tcg_rm, rm, 0, size);
9672 switch (opcode) {
9673 case 0x1: /* SQADD, UQADD */
9675 static NeonGenTwoOpEnvFn * const fns[3][2] = {
9676 { gen_helper_neon_qadd_s8, gen_helper_neon_qadd_u8 },
9677 { gen_helper_neon_qadd_s16, gen_helper_neon_qadd_u16 },
9678 { gen_helper_neon_qadd_s32, gen_helper_neon_qadd_u32 },
9680 genenvfn = fns[size][u];
9681 break;
9683 case 0x5: /* SQSUB, UQSUB */
9685 static NeonGenTwoOpEnvFn * const fns[3][2] = {
9686 { gen_helper_neon_qsub_s8, gen_helper_neon_qsub_u8 },
9687 { gen_helper_neon_qsub_s16, gen_helper_neon_qsub_u16 },
9688 { gen_helper_neon_qsub_s32, gen_helper_neon_qsub_u32 },
9690 genenvfn = fns[size][u];
9691 break;
9693 case 0x9: /* SQSHL, UQSHL */
9695 static NeonGenTwoOpEnvFn * const fns[3][2] = {
9696 { gen_helper_neon_qshl_s8, gen_helper_neon_qshl_u8 },
9697 { gen_helper_neon_qshl_s16, gen_helper_neon_qshl_u16 },
9698 { gen_helper_neon_qshl_s32, gen_helper_neon_qshl_u32 },
9700 genenvfn = fns[size][u];
9701 break;
9703 case 0xb: /* SQRSHL, UQRSHL */
9705 static NeonGenTwoOpEnvFn * const fns[3][2] = {
9706 { gen_helper_neon_qrshl_s8, gen_helper_neon_qrshl_u8 },
9707 { gen_helper_neon_qrshl_s16, gen_helper_neon_qrshl_u16 },
9708 { gen_helper_neon_qrshl_s32, gen_helper_neon_qrshl_u32 },
9710 genenvfn = fns[size][u];
9711 break;
9713 case 0x16: /* SQDMULH, SQRDMULH */
9715 static NeonGenTwoOpEnvFn * const fns[2][2] = {
9716 { gen_helper_neon_qdmulh_s16, gen_helper_neon_qrdmulh_s16 },
9717 { gen_helper_neon_qdmulh_s32, gen_helper_neon_qrdmulh_s32 },
9719 assert(size == 1 || size == 2);
9720 genenvfn = fns[size - 1][u];
9721 break;
9723 default:
9724 g_assert_not_reached();
9727 genenvfn(tcg_rd32, cpu_env, tcg_rn, tcg_rm);
9728 tcg_gen_extu_i32_i64(tcg_rd, tcg_rd32);
9729 tcg_temp_free_i32(tcg_rd32);
9730 tcg_temp_free_i32(tcg_rn);
9731 tcg_temp_free_i32(tcg_rm);
9734 write_fp_dreg(s, rd, tcg_rd);
9736 tcg_temp_free_i64(tcg_rd);
9739 /* AdvSIMD scalar three same FP16
9740 * 31 30 29 28 24 23 22 21 20 16 15 14 13 11 10 9 5 4 0
9741 * +-----+---+-----------+---+-----+------+-----+--------+---+----+----+
9742 * | 0 1 | U | 1 1 1 1 0 | a | 1 0 | Rm | 0 0 | opcode | 1 | Rn | Rd |
9743 * +-----+---+-----------+---+-----+------+-----+--------+---+----+----+
9744 * v: 0101 1110 0100 0000 0000 0100 0000 0000 => 5e400400
9745 * m: 1101 1111 0110 0000 1100 0100 0000 0000 => df60c400
9747 static void disas_simd_scalar_three_reg_same_fp16(DisasContext *s,
9748 uint32_t insn)
9750 int rd = extract32(insn, 0, 5);
9751 int rn = extract32(insn, 5, 5);
9752 int opcode = extract32(insn, 11, 3);
9753 int rm = extract32(insn, 16, 5);
9754 bool u = extract32(insn, 29, 1);
9755 bool a = extract32(insn, 23, 1);
9756 int fpopcode = opcode | (a << 3) | (u << 4);
9757 TCGv_ptr fpst;
9758 TCGv_i32 tcg_op1;
9759 TCGv_i32 tcg_op2;
9760 TCGv_i32 tcg_res;
9762 switch (fpopcode) {
9763 case 0x03: /* FMULX */
9764 case 0x04: /* FCMEQ (reg) */
9765 case 0x07: /* FRECPS */
9766 case 0x0f: /* FRSQRTS */
9767 case 0x14: /* FCMGE (reg) */
9768 case 0x15: /* FACGE */
9769 case 0x1a: /* FABD */
9770 case 0x1c: /* FCMGT (reg) */
9771 case 0x1d: /* FACGT */
9772 break;
9773 default:
9774 unallocated_encoding(s);
9775 return;
9778 if (!dc_isar_feature(aa64_fp16, s)) {
9779 unallocated_encoding(s);
9782 if (!fp_access_check(s)) {
9783 return;
9786 fpst = get_fpstatus_ptr(true);
9788 tcg_op1 = read_fp_hreg(s, rn);
9789 tcg_op2 = read_fp_hreg(s, rm);
9790 tcg_res = tcg_temp_new_i32();
9792 switch (fpopcode) {
9793 case 0x03: /* FMULX */
9794 gen_helper_advsimd_mulxh(tcg_res, tcg_op1, tcg_op2, fpst);
9795 break;
9796 case 0x04: /* FCMEQ (reg) */
9797 gen_helper_advsimd_ceq_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9798 break;
9799 case 0x07: /* FRECPS */
9800 gen_helper_recpsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9801 break;
9802 case 0x0f: /* FRSQRTS */
9803 gen_helper_rsqrtsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9804 break;
9805 case 0x14: /* FCMGE (reg) */
9806 gen_helper_advsimd_cge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9807 break;
9808 case 0x15: /* FACGE */
9809 gen_helper_advsimd_acge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9810 break;
9811 case 0x1a: /* FABD */
9812 gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
9813 tcg_gen_andi_i32(tcg_res, tcg_res, 0x7fff);
9814 break;
9815 case 0x1c: /* FCMGT (reg) */
9816 gen_helper_advsimd_cgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9817 break;
9818 case 0x1d: /* FACGT */
9819 gen_helper_advsimd_acgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9820 break;
9821 default:
9822 g_assert_not_reached();
9825 write_fp_sreg(s, rd, tcg_res);
9828 tcg_temp_free_i32(tcg_res);
9829 tcg_temp_free_i32(tcg_op1);
9830 tcg_temp_free_i32(tcg_op2);
9831 tcg_temp_free_ptr(fpst);
9834 /* AdvSIMD scalar three same extra
9835 * 31 30 29 28 24 23 22 21 20 16 15 14 11 10 9 5 4 0
9836 * +-----+---+-----------+------+---+------+---+--------+---+----+----+
9837 * | 0 1 | U | 1 1 1 1 0 | size | 0 | Rm | 1 | opcode | 1 | Rn | Rd |
9838 * +-----+---+-----------+------+---+------+---+--------+---+----+----+
9840 static void disas_simd_scalar_three_reg_same_extra(DisasContext *s,
9841 uint32_t insn)
9843 int rd = extract32(insn, 0, 5);
9844 int rn = extract32(insn, 5, 5);
9845 int opcode = extract32(insn, 11, 4);
9846 int rm = extract32(insn, 16, 5);
9847 int size = extract32(insn, 22, 2);
9848 bool u = extract32(insn, 29, 1);
9849 TCGv_i32 ele1, ele2, ele3;
9850 TCGv_i64 res;
9851 bool feature;
9853 switch (u * 16 + opcode) {
9854 case 0x10: /* SQRDMLAH (vector) */
9855 case 0x11: /* SQRDMLSH (vector) */
9856 if (size != 1 && size != 2) {
9857 unallocated_encoding(s);
9858 return;
9860 feature = dc_isar_feature(aa64_rdm, s);
9861 break;
9862 default:
9863 unallocated_encoding(s);
9864 return;
9866 if (!feature) {
9867 unallocated_encoding(s);
9868 return;
9870 if (!fp_access_check(s)) {
9871 return;
9874 /* Do a single operation on the lowest element in the vector.
9875 * We use the standard Neon helpers and rely on 0 OP 0 == 0
9876 * with no side effects for all these operations.
9877 * OPTME: special-purpose helpers would avoid doing some
9878 * unnecessary work in the helper for the 16 bit cases.
9880 ele1 = tcg_temp_new_i32();
9881 ele2 = tcg_temp_new_i32();
9882 ele3 = tcg_temp_new_i32();
9884 read_vec_element_i32(s, ele1, rn, 0, size);
9885 read_vec_element_i32(s, ele2, rm, 0, size);
9886 read_vec_element_i32(s, ele3, rd, 0, size);
9888 switch (opcode) {
9889 case 0x0: /* SQRDMLAH */
9890 if (size == 1) {
9891 gen_helper_neon_qrdmlah_s16(ele3, cpu_env, ele1, ele2, ele3);
9892 } else {
9893 gen_helper_neon_qrdmlah_s32(ele3, cpu_env, ele1, ele2, ele3);
9895 break;
9896 case 0x1: /* SQRDMLSH */
9897 if (size == 1) {
9898 gen_helper_neon_qrdmlsh_s16(ele3, cpu_env, ele1, ele2, ele3);
9899 } else {
9900 gen_helper_neon_qrdmlsh_s32(ele3, cpu_env, ele1, ele2, ele3);
9902 break;
9903 default:
9904 g_assert_not_reached();
9906 tcg_temp_free_i32(ele1);
9907 tcg_temp_free_i32(ele2);
9909 res = tcg_temp_new_i64();
9910 tcg_gen_extu_i32_i64(res, ele3);
9911 tcg_temp_free_i32(ele3);
9913 write_fp_dreg(s, rd, res);
9914 tcg_temp_free_i64(res);
9917 static void handle_2misc_64(DisasContext *s, int opcode, bool u,
9918 TCGv_i64 tcg_rd, TCGv_i64 tcg_rn,
9919 TCGv_i32 tcg_rmode, TCGv_ptr tcg_fpstatus)
9921 /* Handle 64->64 opcodes which are shared between the scalar and
9922 * vector 2-reg-misc groups. We cover every integer opcode where size == 3
9923 * is valid in either group and also the double-precision fp ops.
9924 * The caller only need provide tcg_rmode and tcg_fpstatus if the op
9925 * requires them.
9927 TCGCond cond;
9929 switch (opcode) {
9930 case 0x4: /* CLS, CLZ */
9931 if (u) {
9932 tcg_gen_clzi_i64(tcg_rd, tcg_rn, 64);
9933 } else {
9934 tcg_gen_clrsb_i64(tcg_rd, tcg_rn);
9936 break;
9937 case 0x5: /* NOT */
9938 /* This opcode is shared with CNT and RBIT but we have earlier
9939 * enforced that size == 3 if and only if this is the NOT insn.
9941 tcg_gen_not_i64(tcg_rd, tcg_rn);
9942 break;
9943 case 0x7: /* SQABS, SQNEG */
9944 if (u) {
9945 gen_helper_neon_qneg_s64(tcg_rd, cpu_env, tcg_rn);
9946 } else {
9947 gen_helper_neon_qabs_s64(tcg_rd, cpu_env, tcg_rn);
9949 break;
9950 case 0xa: /* CMLT */
9951 /* 64 bit integer comparison against zero, result is
9952 * test ? (2^64 - 1) : 0. We implement via setcond(!test) and
9953 * subtracting 1.
9955 cond = TCG_COND_LT;
9956 do_cmop:
9957 tcg_gen_setcondi_i64(cond, tcg_rd, tcg_rn, 0);
9958 tcg_gen_neg_i64(tcg_rd, tcg_rd);
9959 break;
9960 case 0x8: /* CMGT, CMGE */
9961 cond = u ? TCG_COND_GE : TCG_COND_GT;
9962 goto do_cmop;
9963 case 0x9: /* CMEQ, CMLE */
9964 cond = u ? TCG_COND_LE : TCG_COND_EQ;
9965 goto do_cmop;
9966 case 0xb: /* ABS, NEG */
9967 if (u) {
9968 tcg_gen_neg_i64(tcg_rd, tcg_rn);
9969 } else {
9970 tcg_gen_abs_i64(tcg_rd, tcg_rn);
9972 break;
9973 case 0x2f: /* FABS */
9974 gen_helper_vfp_absd(tcg_rd, tcg_rn);
9975 break;
9976 case 0x6f: /* FNEG */
9977 gen_helper_vfp_negd(tcg_rd, tcg_rn);
9978 break;
9979 case 0x7f: /* FSQRT */
9980 gen_helper_vfp_sqrtd(tcg_rd, tcg_rn, cpu_env);
9981 break;
9982 case 0x1a: /* FCVTNS */
9983 case 0x1b: /* FCVTMS */
9984 case 0x1c: /* FCVTAS */
9985 case 0x3a: /* FCVTPS */
9986 case 0x3b: /* FCVTZS */
9988 TCGv_i32 tcg_shift = tcg_const_i32(0);
9989 gen_helper_vfp_tosqd(tcg_rd, tcg_rn, tcg_shift, tcg_fpstatus);
9990 tcg_temp_free_i32(tcg_shift);
9991 break;
9993 case 0x5a: /* FCVTNU */
9994 case 0x5b: /* FCVTMU */
9995 case 0x5c: /* FCVTAU */
9996 case 0x7a: /* FCVTPU */
9997 case 0x7b: /* FCVTZU */
9999 TCGv_i32 tcg_shift = tcg_const_i32(0);
10000 gen_helper_vfp_touqd(tcg_rd, tcg_rn, tcg_shift, tcg_fpstatus);
10001 tcg_temp_free_i32(tcg_shift);
10002 break;
10004 case 0x18: /* FRINTN */
10005 case 0x19: /* FRINTM */
10006 case 0x38: /* FRINTP */
10007 case 0x39: /* FRINTZ */
10008 case 0x58: /* FRINTA */
10009 case 0x79: /* FRINTI */
10010 gen_helper_rintd(tcg_rd, tcg_rn, tcg_fpstatus);
10011 break;
10012 case 0x59: /* FRINTX */
10013 gen_helper_rintd_exact(tcg_rd, tcg_rn, tcg_fpstatus);
10014 break;
10015 case 0x1e: /* FRINT32Z */
10016 case 0x5e: /* FRINT32X */
10017 gen_helper_frint32_d(tcg_rd, tcg_rn, tcg_fpstatus);
10018 break;
10019 case 0x1f: /* FRINT64Z */
10020 case 0x5f: /* FRINT64X */
10021 gen_helper_frint64_d(tcg_rd, tcg_rn, tcg_fpstatus);
10022 break;
10023 default:
10024 g_assert_not_reached();
10028 static void handle_2misc_fcmp_zero(DisasContext *s, int opcode,
10029 bool is_scalar, bool is_u, bool is_q,
10030 int size, int rn, int rd)
10032 bool is_double = (size == MO_64);
10033 TCGv_ptr fpst;
10035 if (!fp_access_check(s)) {
10036 return;
10039 fpst = get_fpstatus_ptr(size == MO_16);
10041 if (is_double) {
10042 TCGv_i64 tcg_op = tcg_temp_new_i64();
10043 TCGv_i64 tcg_zero = tcg_const_i64(0);
10044 TCGv_i64 tcg_res = tcg_temp_new_i64();
10045 NeonGenTwoDoubleOpFn *genfn;
10046 bool swap = false;
10047 int pass;
10049 switch (opcode) {
10050 case 0x2e: /* FCMLT (zero) */
10051 swap = true;
10052 /* fallthrough */
10053 case 0x2c: /* FCMGT (zero) */
10054 genfn = gen_helper_neon_cgt_f64;
10055 break;
10056 case 0x2d: /* FCMEQ (zero) */
10057 genfn = gen_helper_neon_ceq_f64;
10058 break;
10059 case 0x6d: /* FCMLE (zero) */
10060 swap = true;
10061 /* fall through */
10062 case 0x6c: /* FCMGE (zero) */
10063 genfn = gen_helper_neon_cge_f64;
10064 break;
10065 default:
10066 g_assert_not_reached();
10069 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
10070 read_vec_element(s, tcg_op, rn, pass, MO_64);
10071 if (swap) {
10072 genfn(tcg_res, tcg_zero, tcg_op, fpst);
10073 } else {
10074 genfn(tcg_res, tcg_op, tcg_zero, fpst);
10076 write_vec_element(s, tcg_res, rd, pass, MO_64);
10078 tcg_temp_free_i64(tcg_res);
10079 tcg_temp_free_i64(tcg_zero);
10080 tcg_temp_free_i64(tcg_op);
10082 clear_vec_high(s, !is_scalar, rd);
10083 } else {
10084 TCGv_i32 tcg_op = tcg_temp_new_i32();
10085 TCGv_i32 tcg_zero = tcg_const_i32(0);
10086 TCGv_i32 tcg_res = tcg_temp_new_i32();
10087 NeonGenTwoSingleOpFn *genfn;
10088 bool swap = false;
10089 int pass, maxpasses;
10091 if (size == MO_16) {
10092 switch (opcode) {
10093 case 0x2e: /* FCMLT (zero) */
10094 swap = true;
10095 /* fall through */
10096 case 0x2c: /* FCMGT (zero) */
10097 genfn = gen_helper_advsimd_cgt_f16;
10098 break;
10099 case 0x2d: /* FCMEQ (zero) */
10100 genfn = gen_helper_advsimd_ceq_f16;
10101 break;
10102 case 0x6d: /* FCMLE (zero) */
10103 swap = true;
10104 /* fall through */
10105 case 0x6c: /* FCMGE (zero) */
10106 genfn = gen_helper_advsimd_cge_f16;
10107 break;
10108 default:
10109 g_assert_not_reached();
10111 } else {
10112 switch (opcode) {
10113 case 0x2e: /* FCMLT (zero) */
10114 swap = true;
10115 /* fall through */
10116 case 0x2c: /* FCMGT (zero) */
10117 genfn = gen_helper_neon_cgt_f32;
10118 break;
10119 case 0x2d: /* FCMEQ (zero) */
10120 genfn = gen_helper_neon_ceq_f32;
10121 break;
10122 case 0x6d: /* FCMLE (zero) */
10123 swap = true;
10124 /* fall through */
10125 case 0x6c: /* FCMGE (zero) */
10126 genfn = gen_helper_neon_cge_f32;
10127 break;
10128 default:
10129 g_assert_not_reached();
10133 if (is_scalar) {
10134 maxpasses = 1;
10135 } else {
10136 int vector_size = 8 << is_q;
10137 maxpasses = vector_size >> size;
10140 for (pass = 0; pass < maxpasses; pass++) {
10141 read_vec_element_i32(s, tcg_op, rn, pass, size);
10142 if (swap) {
10143 genfn(tcg_res, tcg_zero, tcg_op, fpst);
10144 } else {
10145 genfn(tcg_res, tcg_op, tcg_zero, fpst);
10147 if (is_scalar) {
10148 write_fp_sreg(s, rd, tcg_res);
10149 } else {
10150 write_vec_element_i32(s, tcg_res, rd, pass, size);
10153 tcg_temp_free_i32(tcg_res);
10154 tcg_temp_free_i32(tcg_zero);
10155 tcg_temp_free_i32(tcg_op);
10156 if (!is_scalar) {
10157 clear_vec_high(s, is_q, rd);
10161 tcg_temp_free_ptr(fpst);
10164 static void handle_2misc_reciprocal(DisasContext *s, int opcode,
10165 bool is_scalar, bool is_u, bool is_q,
10166 int size, int rn, int rd)
10168 bool is_double = (size == 3);
10169 TCGv_ptr fpst = get_fpstatus_ptr(false);
10171 if (is_double) {
10172 TCGv_i64 tcg_op = tcg_temp_new_i64();
10173 TCGv_i64 tcg_res = tcg_temp_new_i64();
10174 int pass;
10176 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
10177 read_vec_element(s, tcg_op, rn, pass, MO_64);
10178 switch (opcode) {
10179 case 0x3d: /* FRECPE */
10180 gen_helper_recpe_f64(tcg_res, tcg_op, fpst);
10181 break;
10182 case 0x3f: /* FRECPX */
10183 gen_helper_frecpx_f64(tcg_res, tcg_op, fpst);
10184 break;
10185 case 0x7d: /* FRSQRTE */
10186 gen_helper_rsqrte_f64(tcg_res, tcg_op, fpst);
10187 break;
10188 default:
10189 g_assert_not_reached();
10191 write_vec_element(s, tcg_res, rd, pass, MO_64);
10193 tcg_temp_free_i64(tcg_res);
10194 tcg_temp_free_i64(tcg_op);
10195 clear_vec_high(s, !is_scalar, rd);
10196 } else {
10197 TCGv_i32 tcg_op = tcg_temp_new_i32();
10198 TCGv_i32 tcg_res = tcg_temp_new_i32();
10199 int pass, maxpasses;
10201 if (is_scalar) {
10202 maxpasses = 1;
10203 } else {
10204 maxpasses = is_q ? 4 : 2;
10207 for (pass = 0; pass < maxpasses; pass++) {
10208 read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
10210 switch (opcode) {
10211 case 0x3c: /* URECPE */
10212 gen_helper_recpe_u32(tcg_res, tcg_op);
10213 break;
10214 case 0x3d: /* FRECPE */
10215 gen_helper_recpe_f32(tcg_res, tcg_op, fpst);
10216 break;
10217 case 0x3f: /* FRECPX */
10218 gen_helper_frecpx_f32(tcg_res, tcg_op, fpst);
10219 break;
10220 case 0x7d: /* FRSQRTE */
10221 gen_helper_rsqrte_f32(tcg_res, tcg_op, fpst);
10222 break;
10223 default:
10224 g_assert_not_reached();
10227 if (is_scalar) {
10228 write_fp_sreg(s, rd, tcg_res);
10229 } else {
10230 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
10233 tcg_temp_free_i32(tcg_res);
10234 tcg_temp_free_i32(tcg_op);
10235 if (!is_scalar) {
10236 clear_vec_high(s, is_q, rd);
10239 tcg_temp_free_ptr(fpst);
10242 static void handle_2misc_narrow(DisasContext *s, bool scalar,
10243 int opcode, bool u, bool is_q,
10244 int size, int rn, int rd)
10246 /* Handle 2-reg-misc ops which are narrowing (so each 2*size element
10247 * in the source becomes a size element in the destination).
10249 int pass;
10250 TCGv_i32 tcg_res[2];
10251 int destelt = is_q ? 2 : 0;
10252 int passes = scalar ? 1 : 2;
10254 if (scalar) {
10255 tcg_res[1] = tcg_const_i32(0);
10258 for (pass = 0; pass < passes; pass++) {
10259 TCGv_i64 tcg_op = tcg_temp_new_i64();
10260 NeonGenNarrowFn *genfn = NULL;
10261 NeonGenNarrowEnvFn *genenvfn = NULL;
10263 if (scalar) {
10264 read_vec_element(s, tcg_op, rn, pass, size + 1);
10265 } else {
10266 read_vec_element(s, tcg_op, rn, pass, MO_64);
10268 tcg_res[pass] = tcg_temp_new_i32();
10270 switch (opcode) {
10271 case 0x12: /* XTN, SQXTUN */
10273 static NeonGenNarrowFn * const xtnfns[3] = {
10274 gen_helper_neon_narrow_u8,
10275 gen_helper_neon_narrow_u16,
10276 tcg_gen_extrl_i64_i32,
10278 static NeonGenNarrowEnvFn * const sqxtunfns[3] = {
10279 gen_helper_neon_unarrow_sat8,
10280 gen_helper_neon_unarrow_sat16,
10281 gen_helper_neon_unarrow_sat32,
10283 if (u) {
10284 genenvfn = sqxtunfns[size];
10285 } else {
10286 genfn = xtnfns[size];
10288 break;
10290 case 0x14: /* SQXTN, UQXTN */
10292 static NeonGenNarrowEnvFn * const fns[3][2] = {
10293 { gen_helper_neon_narrow_sat_s8,
10294 gen_helper_neon_narrow_sat_u8 },
10295 { gen_helper_neon_narrow_sat_s16,
10296 gen_helper_neon_narrow_sat_u16 },
10297 { gen_helper_neon_narrow_sat_s32,
10298 gen_helper_neon_narrow_sat_u32 },
10300 genenvfn = fns[size][u];
10301 break;
10303 case 0x16: /* FCVTN, FCVTN2 */
10304 /* 32 bit to 16 bit or 64 bit to 32 bit float conversion */
10305 if (size == 2) {
10306 gen_helper_vfp_fcvtsd(tcg_res[pass], tcg_op, cpu_env);
10307 } else {
10308 TCGv_i32 tcg_lo = tcg_temp_new_i32();
10309 TCGv_i32 tcg_hi = tcg_temp_new_i32();
10310 TCGv_ptr fpst = get_fpstatus_ptr(false);
10311 TCGv_i32 ahp = get_ahp_flag();
10313 tcg_gen_extr_i64_i32(tcg_lo, tcg_hi, tcg_op);
10314 gen_helper_vfp_fcvt_f32_to_f16(tcg_lo, tcg_lo, fpst, ahp);
10315 gen_helper_vfp_fcvt_f32_to_f16(tcg_hi, tcg_hi, fpst, ahp);
10316 tcg_gen_deposit_i32(tcg_res[pass], tcg_lo, tcg_hi, 16, 16);
10317 tcg_temp_free_i32(tcg_lo);
10318 tcg_temp_free_i32(tcg_hi);
10319 tcg_temp_free_ptr(fpst);
10320 tcg_temp_free_i32(ahp);
10322 break;
10323 case 0x56: /* FCVTXN, FCVTXN2 */
10324 /* 64 bit to 32 bit float conversion
10325 * with von Neumann rounding (round to odd)
10327 assert(size == 2);
10328 gen_helper_fcvtx_f64_to_f32(tcg_res[pass], tcg_op, cpu_env);
10329 break;
10330 default:
10331 g_assert_not_reached();
10334 if (genfn) {
10335 genfn(tcg_res[pass], tcg_op);
10336 } else if (genenvfn) {
10337 genenvfn(tcg_res[pass], cpu_env, tcg_op);
10340 tcg_temp_free_i64(tcg_op);
10343 for (pass = 0; pass < 2; pass++) {
10344 write_vec_element_i32(s, tcg_res[pass], rd, destelt + pass, MO_32);
10345 tcg_temp_free_i32(tcg_res[pass]);
10347 clear_vec_high(s, is_q, rd);
10350 /* Remaining saturating accumulating ops */
10351 static void handle_2misc_satacc(DisasContext *s, bool is_scalar, bool is_u,
10352 bool is_q, int size, int rn, int rd)
10354 bool is_double = (size == 3);
10356 if (is_double) {
10357 TCGv_i64 tcg_rn = tcg_temp_new_i64();
10358 TCGv_i64 tcg_rd = tcg_temp_new_i64();
10359 int pass;
10361 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
10362 read_vec_element(s, tcg_rn, rn, pass, MO_64);
10363 read_vec_element(s, tcg_rd, rd, pass, MO_64);
10365 if (is_u) { /* USQADD */
10366 gen_helper_neon_uqadd_s64(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10367 } else { /* SUQADD */
10368 gen_helper_neon_sqadd_u64(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10370 write_vec_element(s, tcg_rd, rd, pass, MO_64);
10372 tcg_temp_free_i64(tcg_rd);
10373 tcg_temp_free_i64(tcg_rn);
10374 clear_vec_high(s, !is_scalar, rd);
10375 } else {
10376 TCGv_i32 tcg_rn = tcg_temp_new_i32();
10377 TCGv_i32 tcg_rd = tcg_temp_new_i32();
10378 int pass, maxpasses;
10380 if (is_scalar) {
10381 maxpasses = 1;
10382 } else {
10383 maxpasses = is_q ? 4 : 2;
10386 for (pass = 0; pass < maxpasses; pass++) {
10387 if (is_scalar) {
10388 read_vec_element_i32(s, tcg_rn, rn, pass, size);
10389 read_vec_element_i32(s, tcg_rd, rd, pass, size);
10390 } else {
10391 read_vec_element_i32(s, tcg_rn, rn, pass, MO_32);
10392 read_vec_element_i32(s, tcg_rd, rd, pass, MO_32);
10395 if (is_u) { /* USQADD */
10396 switch (size) {
10397 case 0:
10398 gen_helper_neon_uqadd_s8(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10399 break;
10400 case 1:
10401 gen_helper_neon_uqadd_s16(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10402 break;
10403 case 2:
10404 gen_helper_neon_uqadd_s32(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10405 break;
10406 default:
10407 g_assert_not_reached();
10409 } else { /* SUQADD */
10410 switch (size) {
10411 case 0:
10412 gen_helper_neon_sqadd_u8(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10413 break;
10414 case 1:
10415 gen_helper_neon_sqadd_u16(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10416 break;
10417 case 2:
10418 gen_helper_neon_sqadd_u32(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10419 break;
10420 default:
10421 g_assert_not_reached();
10425 if (is_scalar) {
10426 TCGv_i64 tcg_zero = tcg_const_i64(0);
10427 write_vec_element(s, tcg_zero, rd, 0, MO_64);
10428 tcg_temp_free_i64(tcg_zero);
10430 write_vec_element_i32(s, tcg_rd, rd, pass, MO_32);
10432 tcg_temp_free_i32(tcg_rd);
10433 tcg_temp_free_i32(tcg_rn);
10434 clear_vec_high(s, is_q, rd);
10438 /* AdvSIMD scalar two reg misc
10439 * 31 30 29 28 24 23 22 21 17 16 12 11 10 9 5 4 0
10440 * +-----+---+-----------+------+-----------+--------+-----+------+------+
10441 * | 0 1 | U | 1 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 | Rn | Rd |
10442 * +-----+---+-----------+------+-----------+--------+-----+------+------+
10444 static void disas_simd_scalar_two_reg_misc(DisasContext *s, uint32_t insn)
10446 int rd = extract32(insn, 0, 5);
10447 int rn = extract32(insn, 5, 5);
10448 int opcode = extract32(insn, 12, 5);
10449 int size = extract32(insn, 22, 2);
10450 bool u = extract32(insn, 29, 1);
10451 bool is_fcvt = false;
10452 int rmode;
10453 TCGv_i32 tcg_rmode;
10454 TCGv_ptr tcg_fpstatus;
10456 switch (opcode) {
10457 case 0x3: /* USQADD / SUQADD*/
10458 if (!fp_access_check(s)) {
10459 return;
10461 handle_2misc_satacc(s, true, u, false, size, rn, rd);
10462 return;
10463 case 0x7: /* SQABS / SQNEG */
10464 break;
10465 case 0xa: /* CMLT */
10466 if (u) {
10467 unallocated_encoding(s);
10468 return;
10470 /* fall through */
10471 case 0x8: /* CMGT, CMGE */
10472 case 0x9: /* CMEQ, CMLE */
10473 case 0xb: /* ABS, NEG */
10474 if (size != 3) {
10475 unallocated_encoding(s);
10476 return;
10478 break;
10479 case 0x12: /* SQXTUN */
10480 if (!u) {
10481 unallocated_encoding(s);
10482 return;
10484 /* fall through */
10485 case 0x14: /* SQXTN, UQXTN */
10486 if (size == 3) {
10487 unallocated_encoding(s);
10488 return;
10490 if (!fp_access_check(s)) {
10491 return;
10493 handle_2misc_narrow(s, true, opcode, u, false, size, rn, rd);
10494 return;
10495 case 0xc ... 0xf:
10496 case 0x16 ... 0x1d:
10497 case 0x1f:
10498 /* Floating point: U, size[1] and opcode indicate operation;
10499 * size[0] indicates single or double precision.
10501 opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
10502 size = extract32(size, 0, 1) ? 3 : 2;
10503 switch (opcode) {
10504 case 0x2c: /* FCMGT (zero) */
10505 case 0x2d: /* FCMEQ (zero) */
10506 case 0x2e: /* FCMLT (zero) */
10507 case 0x6c: /* FCMGE (zero) */
10508 case 0x6d: /* FCMLE (zero) */
10509 handle_2misc_fcmp_zero(s, opcode, true, u, true, size, rn, rd);
10510 return;
10511 case 0x1d: /* SCVTF */
10512 case 0x5d: /* UCVTF */
10514 bool is_signed = (opcode == 0x1d);
10515 if (!fp_access_check(s)) {
10516 return;
10518 handle_simd_intfp_conv(s, rd, rn, 1, is_signed, 0, size);
10519 return;
10521 case 0x3d: /* FRECPE */
10522 case 0x3f: /* FRECPX */
10523 case 0x7d: /* FRSQRTE */
10524 if (!fp_access_check(s)) {
10525 return;
10527 handle_2misc_reciprocal(s, opcode, true, u, true, size, rn, rd);
10528 return;
10529 case 0x1a: /* FCVTNS */
10530 case 0x1b: /* FCVTMS */
10531 case 0x3a: /* FCVTPS */
10532 case 0x3b: /* FCVTZS */
10533 case 0x5a: /* FCVTNU */
10534 case 0x5b: /* FCVTMU */
10535 case 0x7a: /* FCVTPU */
10536 case 0x7b: /* FCVTZU */
10537 is_fcvt = true;
10538 rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
10539 break;
10540 case 0x1c: /* FCVTAS */
10541 case 0x5c: /* FCVTAU */
10542 /* TIEAWAY doesn't fit in the usual rounding mode encoding */
10543 is_fcvt = true;
10544 rmode = FPROUNDING_TIEAWAY;
10545 break;
10546 case 0x56: /* FCVTXN, FCVTXN2 */
10547 if (size == 2) {
10548 unallocated_encoding(s);
10549 return;
10551 if (!fp_access_check(s)) {
10552 return;
10554 handle_2misc_narrow(s, true, opcode, u, false, size - 1, rn, rd);
10555 return;
10556 default:
10557 unallocated_encoding(s);
10558 return;
10560 break;
10561 default:
10562 unallocated_encoding(s);
10563 return;
10566 if (!fp_access_check(s)) {
10567 return;
10570 if (is_fcvt) {
10571 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rmode));
10572 tcg_fpstatus = get_fpstatus_ptr(false);
10573 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
10574 } else {
10575 tcg_rmode = NULL;
10576 tcg_fpstatus = NULL;
10579 if (size == 3) {
10580 TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
10581 TCGv_i64 tcg_rd = tcg_temp_new_i64();
10583 handle_2misc_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rmode, tcg_fpstatus);
10584 write_fp_dreg(s, rd, tcg_rd);
10585 tcg_temp_free_i64(tcg_rd);
10586 tcg_temp_free_i64(tcg_rn);
10587 } else {
10588 TCGv_i32 tcg_rn = tcg_temp_new_i32();
10589 TCGv_i32 tcg_rd = tcg_temp_new_i32();
10591 read_vec_element_i32(s, tcg_rn, rn, 0, size);
10593 switch (opcode) {
10594 case 0x7: /* SQABS, SQNEG */
10596 NeonGenOneOpEnvFn *genfn;
10597 static NeonGenOneOpEnvFn * const fns[3][2] = {
10598 { gen_helper_neon_qabs_s8, gen_helper_neon_qneg_s8 },
10599 { gen_helper_neon_qabs_s16, gen_helper_neon_qneg_s16 },
10600 { gen_helper_neon_qabs_s32, gen_helper_neon_qneg_s32 },
10602 genfn = fns[size][u];
10603 genfn(tcg_rd, cpu_env, tcg_rn);
10604 break;
10606 case 0x1a: /* FCVTNS */
10607 case 0x1b: /* FCVTMS */
10608 case 0x1c: /* FCVTAS */
10609 case 0x3a: /* FCVTPS */
10610 case 0x3b: /* FCVTZS */
10612 TCGv_i32 tcg_shift = tcg_const_i32(0);
10613 gen_helper_vfp_tosls(tcg_rd, tcg_rn, tcg_shift, tcg_fpstatus);
10614 tcg_temp_free_i32(tcg_shift);
10615 break;
10617 case 0x5a: /* FCVTNU */
10618 case 0x5b: /* FCVTMU */
10619 case 0x5c: /* FCVTAU */
10620 case 0x7a: /* FCVTPU */
10621 case 0x7b: /* FCVTZU */
10623 TCGv_i32 tcg_shift = tcg_const_i32(0);
10624 gen_helper_vfp_touls(tcg_rd, tcg_rn, tcg_shift, tcg_fpstatus);
10625 tcg_temp_free_i32(tcg_shift);
10626 break;
10628 default:
10629 g_assert_not_reached();
10632 write_fp_sreg(s, rd, tcg_rd);
10633 tcg_temp_free_i32(tcg_rd);
10634 tcg_temp_free_i32(tcg_rn);
10637 if (is_fcvt) {
10638 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
10639 tcg_temp_free_i32(tcg_rmode);
10640 tcg_temp_free_ptr(tcg_fpstatus);
10644 /* SSHR[RA]/USHR[RA] - Vector shift right (optional rounding/accumulate) */
10645 static void handle_vec_simd_shri(DisasContext *s, bool is_q, bool is_u,
10646 int immh, int immb, int opcode, int rn, int rd)
10648 int size = 32 - clz32(immh) - 1;
10649 int immhb = immh << 3 | immb;
10650 int shift = 2 * (8 << size) - immhb;
10651 GVecGen2iFn *gvec_fn;
10653 if (extract32(immh, 3, 1) && !is_q) {
10654 unallocated_encoding(s);
10655 return;
10657 tcg_debug_assert(size <= 3);
10659 if (!fp_access_check(s)) {
10660 return;
10663 switch (opcode) {
10664 case 0x02: /* SSRA / USRA (accumulate) */
10665 gvec_fn = is_u ? gen_gvec_usra : gen_gvec_ssra;
10666 break;
10668 case 0x08: /* SRI */
10669 gvec_fn = gen_gvec_sri;
10670 break;
10672 case 0x00: /* SSHR / USHR */
10673 if (is_u) {
10674 if (shift == 8 << size) {
10675 /* Shift count the same size as element size produces zero. */
10676 tcg_gen_gvec_dup_imm(size, vec_full_reg_offset(s, rd),
10677 is_q ? 16 : 8, vec_full_reg_size(s), 0);
10678 return;
10680 gvec_fn = tcg_gen_gvec_shri;
10681 } else {
10682 /* Shift count the same size as element size produces all sign. */
10683 if (shift == 8 << size) {
10684 shift -= 1;
10686 gvec_fn = tcg_gen_gvec_sari;
10688 break;
10690 case 0x04: /* SRSHR / URSHR (rounding) */
10691 gvec_fn = is_u ? gen_gvec_urshr : gen_gvec_srshr;
10692 break;
10694 case 0x06: /* SRSRA / URSRA (accum + rounding) */
10695 gvec_fn = is_u ? gen_gvec_ursra : gen_gvec_srsra;
10696 break;
10698 default:
10699 g_assert_not_reached();
10702 gen_gvec_fn2i(s, is_q, rd, rn, shift, gvec_fn, size);
10705 /* SHL/SLI - Vector shift left */
10706 static void handle_vec_simd_shli(DisasContext *s, bool is_q, bool insert,
10707 int immh, int immb, int opcode, int rn, int rd)
10709 int size = 32 - clz32(immh) - 1;
10710 int immhb = immh << 3 | immb;
10711 int shift = immhb - (8 << size);
10713 /* Range of size is limited by decode: immh is a non-zero 4 bit field */
10714 assert(size >= 0 && size <= 3);
10716 if (extract32(immh, 3, 1) && !is_q) {
10717 unallocated_encoding(s);
10718 return;
10721 if (!fp_access_check(s)) {
10722 return;
10725 if (insert) {
10726 gen_gvec_fn2i(s, is_q, rd, rn, shift, gen_gvec_sli, size);
10727 } else {
10728 gen_gvec_fn2i(s, is_q, rd, rn, shift, tcg_gen_gvec_shli, size);
10732 /* USHLL/SHLL - Vector shift left with widening */
10733 static void handle_vec_simd_wshli(DisasContext *s, bool is_q, bool is_u,
10734 int immh, int immb, int opcode, int rn, int rd)
10736 int size = 32 - clz32(immh) - 1;
10737 int immhb = immh << 3 | immb;
10738 int shift = immhb - (8 << size);
10739 int dsize = 64;
10740 int esize = 8 << size;
10741 int elements = dsize/esize;
10742 TCGv_i64 tcg_rn = new_tmp_a64(s);
10743 TCGv_i64 tcg_rd = new_tmp_a64(s);
10744 int i;
10746 if (size >= 3) {
10747 unallocated_encoding(s);
10748 return;
10751 if (!fp_access_check(s)) {
10752 return;
10755 /* For the LL variants the store is larger than the load,
10756 * so if rd == rn we would overwrite parts of our input.
10757 * So load everything right now and use shifts in the main loop.
10759 read_vec_element(s, tcg_rn, rn, is_q ? 1 : 0, MO_64);
10761 for (i = 0; i < elements; i++) {
10762 tcg_gen_shri_i64(tcg_rd, tcg_rn, i * esize);
10763 ext_and_shift_reg(tcg_rd, tcg_rd, size | (!is_u << 2), 0);
10764 tcg_gen_shli_i64(tcg_rd, tcg_rd, shift);
10765 write_vec_element(s, tcg_rd, rd, i, size + 1);
10769 /* SHRN/RSHRN - Shift right with narrowing (and potential rounding) */
10770 static void handle_vec_simd_shrn(DisasContext *s, bool is_q,
10771 int immh, int immb, int opcode, int rn, int rd)
10773 int immhb = immh << 3 | immb;
10774 int size = 32 - clz32(immh) - 1;
10775 int dsize = 64;
10776 int esize = 8 << size;
10777 int elements = dsize/esize;
10778 int shift = (2 * esize) - immhb;
10779 bool round = extract32(opcode, 0, 1);
10780 TCGv_i64 tcg_rn, tcg_rd, tcg_final;
10781 TCGv_i64 tcg_round;
10782 int i;
10784 if (extract32(immh, 3, 1)) {
10785 unallocated_encoding(s);
10786 return;
10789 if (!fp_access_check(s)) {
10790 return;
10793 tcg_rn = tcg_temp_new_i64();
10794 tcg_rd = tcg_temp_new_i64();
10795 tcg_final = tcg_temp_new_i64();
10796 read_vec_element(s, tcg_final, rd, is_q ? 1 : 0, MO_64);
10798 if (round) {
10799 uint64_t round_const = 1ULL << (shift - 1);
10800 tcg_round = tcg_const_i64(round_const);
10801 } else {
10802 tcg_round = NULL;
10805 for (i = 0; i < elements; i++) {
10806 read_vec_element(s, tcg_rn, rn, i, size+1);
10807 handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
10808 false, true, size+1, shift);
10810 tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
10813 if (!is_q) {
10814 write_vec_element(s, tcg_final, rd, 0, MO_64);
10815 } else {
10816 write_vec_element(s, tcg_final, rd, 1, MO_64);
10818 if (round) {
10819 tcg_temp_free_i64(tcg_round);
10821 tcg_temp_free_i64(tcg_rn);
10822 tcg_temp_free_i64(tcg_rd);
10823 tcg_temp_free_i64(tcg_final);
10825 clear_vec_high(s, is_q, rd);
10829 /* AdvSIMD shift by immediate
10830 * 31 30 29 28 23 22 19 18 16 15 11 10 9 5 4 0
10831 * +---+---+---+-------------+------+------+--------+---+------+------+
10832 * | 0 | Q | U | 0 1 1 1 1 0 | immh | immb | opcode | 1 | Rn | Rd |
10833 * +---+---+---+-------------+------+------+--------+---+------+------+
10835 static void disas_simd_shift_imm(DisasContext *s, uint32_t insn)
10837 int rd = extract32(insn, 0, 5);
10838 int rn = extract32(insn, 5, 5);
10839 int opcode = extract32(insn, 11, 5);
10840 int immb = extract32(insn, 16, 3);
10841 int immh = extract32(insn, 19, 4);
10842 bool is_u = extract32(insn, 29, 1);
10843 bool is_q = extract32(insn, 30, 1);
10845 /* data_proc_simd[] has sent immh == 0 to disas_simd_mod_imm. */
10846 assert(immh != 0);
10848 switch (opcode) {
10849 case 0x08: /* SRI */
10850 if (!is_u) {
10851 unallocated_encoding(s);
10852 return;
10854 /* fall through */
10855 case 0x00: /* SSHR / USHR */
10856 case 0x02: /* SSRA / USRA (accumulate) */
10857 case 0x04: /* SRSHR / URSHR (rounding) */
10858 case 0x06: /* SRSRA / URSRA (accum + rounding) */
10859 handle_vec_simd_shri(s, is_q, is_u, immh, immb, opcode, rn, rd);
10860 break;
10861 case 0x0a: /* SHL / SLI */
10862 handle_vec_simd_shli(s, is_q, is_u, immh, immb, opcode, rn, rd);
10863 break;
10864 case 0x10: /* SHRN */
10865 case 0x11: /* RSHRN / SQRSHRUN */
10866 if (is_u) {
10867 handle_vec_simd_sqshrn(s, false, is_q, false, true, immh, immb,
10868 opcode, rn, rd);
10869 } else {
10870 handle_vec_simd_shrn(s, is_q, immh, immb, opcode, rn, rd);
10872 break;
10873 case 0x12: /* SQSHRN / UQSHRN */
10874 case 0x13: /* SQRSHRN / UQRSHRN */
10875 handle_vec_simd_sqshrn(s, false, is_q, is_u, is_u, immh, immb,
10876 opcode, rn, rd);
10877 break;
10878 case 0x14: /* SSHLL / USHLL */
10879 handle_vec_simd_wshli(s, is_q, is_u, immh, immb, opcode, rn, rd);
10880 break;
10881 case 0x1c: /* SCVTF / UCVTF */
10882 handle_simd_shift_intfp_conv(s, false, is_q, is_u, immh, immb,
10883 opcode, rn, rd);
10884 break;
10885 case 0xc: /* SQSHLU */
10886 if (!is_u) {
10887 unallocated_encoding(s);
10888 return;
10890 handle_simd_qshl(s, false, is_q, false, true, immh, immb, rn, rd);
10891 break;
10892 case 0xe: /* SQSHL, UQSHL */
10893 handle_simd_qshl(s, false, is_q, is_u, is_u, immh, immb, rn, rd);
10894 break;
10895 case 0x1f: /* FCVTZS/ FCVTZU */
10896 handle_simd_shift_fpint_conv(s, false, is_q, is_u, immh, immb, rn, rd);
10897 return;
10898 default:
10899 unallocated_encoding(s);
10900 return;
10904 /* Generate code to do a "long" addition or subtraction, ie one done in
10905 * TCGv_i64 on vector lanes twice the width specified by size.
10907 static void gen_neon_addl(int size, bool is_sub, TCGv_i64 tcg_res,
10908 TCGv_i64 tcg_op1, TCGv_i64 tcg_op2)
10910 static NeonGenTwo64OpFn * const fns[3][2] = {
10911 { gen_helper_neon_addl_u16, gen_helper_neon_subl_u16 },
10912 { gen_helper_neon_addl_u32, gen_helper_neon_subl_u32 },
10913 { tcg_gen_add_i64, tcg_gen_sub_i64 },
10915 NeonGenTwo64OpFn *genfn;
10916 assert(size < 3);
10918 genfn = fns[size][is_sub];
10919 genfn(tcg_res, tcg_op1, tcg_op2);
10922 static void handle_3rd_widening(DisasContext *s, int is_q, int is_u, int size,
10923 int opcode, int rd, int rn, int rm)
10925 /* 3-reg-different widening insns: 64 x 64 -> 128 */
10926 TCGv_i64 tcg_res[2];
10927 int pass, accop;
10929 tcg_res[0] = tcg_temp_new_i64();
10930 tcg_res[1] = tcg_temp_new_i64();
10932 /* Does this op do an adding accumulate, a subtracting accumulate,
10933 * or no accumulate at all?
10935 switch (opcode) {
10936 case 5:
10937 case 8:
10938 case 9:
10939 accop = 1;
10940 break;
10941 case 10:
10942 case 11:
10943 accop = -1;
10944 break;
10945 default:
10946 accop = 0;
10947 break;
10950 if (accop != 0) {
10951 read_vec_element(s, tcg_res[0], rd, 0, MO_64);
10952 read_vec_element(s, tcg_res[1], rd, 1, MO_64);
10955 /* size == 2 means two 32x32->64 operations; this is worth special
10956 * casing because we can generally handle it inline.
10958 if (size == 2) {
10959 for (pass = 0; pass < 2; pass++) {
10960 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10961 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
10962 TCGv_i64 tcg_passres;
10963 MemOp memop = MO_32 | (is_u ? 0 : MO_SIGN);
10965 int elt = pass + is_q * 2;
10967 read_vec_element(s, tcg_op1, rn, elt, memop);
10968 read_vec_element(s, tcg_op2, rm, elt, memop);
10970 if (accop == 0) {
10971 tcg_passres = tcg_res[pass];
10972 } else {
10973 tcg_passres = tcg_temp_new_i64();
10976 switch (opcode) {
10977 case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10978 tcg_gen_add_i64(tcg_passres, tcg_op1, tcg_op2);
10979 break;
10980 case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
10981 tcg_gen_sub_i64(tcg_passres, tcg_op1, tcg_op2);
10982 break;
10983 case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10984 case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10986 TCGv_i64 tcg_tmp1 = tcg_temp_new_i64();
10987 TCGv_i64 tcg_tmp2 = tcg_temp_new_i64();
10989 tcg_gen_sub_i64(tcg_tmp1, tcg_op1, tcg_op2);
10990 tcg_gen_sub_i64(tcg_tmp2, tcg_op2, tcg_op1);
10991 tcg_gen_movcond_i64(is_u ? TCG_COND_GEU : TCG_COND_GE,
10992 tcg_passres,
10993 tcg_op1, tcg_op2, tcg_tmp1, tcg_tmp2);
10994 tcg_temp_free_i64(tcg_tmp1);
10995 tcg_temp_free_i64(tcg_tmp2);
10996 break;
10998 case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10999 case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
11000 case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
11001 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
11002 break;
11003 case 9: /* SQDMLAL, SQDMLAL2 */
11004 case 11: /* SQDMLSL, SQDMLSL2 */
11005 case 13: /* SQDMULL, SQDMULL2 */
11006 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
11007 gen_helper_neon_addl_saturate_s64(tcg_passres, cpu_env,
11008 tcg_passres, tcg_passres);
11009 break;
11010 default:
11011 g_assert_not_reached();
11014 if (opcode == 9 || opcode == 11) {
11015 /* saturating accumulate ops */
11016 if (accop < 0) {
11017 tcg_gen_neg_i64(tcg_passres, tcg_passres);
11019 gen_helper_neon_addl_saturate_s64(tcg_res[pass], cpu_env,
11020 tcg_res[pass], tcg_passres);
11021 } else if (accop > 0) {
11022 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
11023 } else if (accop < 0) {
11024 tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
11027 if (accop != 0) {
11028 tcg_temp_free_i64(tcg_passres);
11031 tcg_temp_free_i64(tcg_op1);
11032 tcg_temp_free_i64(tcg_op2);
11034 } else {
11035 /* size 0 or 1, generally helper functions */
11036 for (pass = 0; pass < 2; pass++) {
11037 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11038 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11039 TCGv_i64 tcg_passres;
11040 int elt = pass + is_q * 2;
11042 read_vec_element_i32(s, tcg_op1, rn, elt, MO_32);
11043 read_vec_element_i32(s, tcg_op2, rm, elt, MO_32);
11045 if (accop == 0) {
11046 tcg_passres = tcg_res[pass];
11047 } else {
11048 tcg_passres = tcg_temp_new_i64();
11051 switch (opcode) {
11052 case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
11053 case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
11055 TCGv_i64 tcg_op2_64 = tcg_temp_new_i64();
11056 static NeonGenWidenFn * const widenfns[2][2] = {
11057 { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
11058 { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
11060 NeonGenWidenFn *widenfn = widenfns[size][is_u];
11062 widenfn(tcg_op2_64, tcg_op2);
11063 widenfn(tcg_passres, tcg_op1);
11064 gen_neon_addl(size, (opcode == 2), tcg_passres,
11065 tcg_passres, tcg_op2_64);
11066 tcg_temp_free_i64(tcg_op2_64);
11067 break;
11069 case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
11070 case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
11071 if (size == 0) {
11072 if (is_u) {
11073 gen_helper_neon_abdl_u16(tcg_passres, tcg_op1, tcg_op2);
11074 } else {
11075 gen_helper_neon_abdl_s16(tcg_passres, tcg_op1, tcg_op2);
11077 } else {
11078 if (is_u) {
11079 gen_helper_neon_abdl_u32(tcg_passres, tcg_op1, tcg_op2);
11080 } else {
11081 gen_helper_neon_abdl_s32(tcg_passres, tcg_op1, tcg_op2);
11084 break;
11085 case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
11086 case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
11087 case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
11088 if (size == 0) {
11089 if (is_u) {
11090 gen_helper_neon_mull_u8(tcg_passres, tcg_op1, tcg_op2);
11091 } else {
11092 gen_helper_neon_mull_s8(tcg_passres, tcg_op1, tcg_op2);
11094 } else {
11095 if (is_u) {
11096 gen_helper_neon_mull_u16(tcg_passres, tcg_op1, tcg_op2);
11097 } else {
11098 gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
11101 break;
11102 case 9: /* SQDMLAL, SQDMLAL2 */
11103 case 11: /* SQDMLSL, SQDMLSL2 */
11104 case 13: /* SQDMULL, SQDMULL2 */
11105 assert(size == 1);
11106 gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
11107 gen_helper_neon_addl_saturate_s32(tcg_passres, cpu_env,
11108 tcg_passres, tcg_passres);
11109 break;
11110 default:
11111 g_assert_not_reached();
11113 tcg_temp_free_i32(tcg_op1);
11114 tcg_temp_free_i32(tcg_op2);
11116 if (accop != 0) {
11117 if (opcode == 9 || opcode == 11) {
11118 /* saturating accumulate ops */
11119 if (accop < 0) {
11120 gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
11122 gen_helper_neon_addl_saturate_s32(tcg_res[pass], cpu_env,
11123 tcg_res[pass],
11124 tcg_passres);
11125 } else {
11126 gen_neon_addl(size, (accop < 0), tcg_res[pass],
11127 tcg_res[pass], tcg_passres);
11129 tcg_temp_free_i64(tcg_passres);
11134 write_vec_element(s, tcg_res[0], rd, 0, MO_64);
11135 write_vec_element(s, tcg_res[1], rd, 1, MO_64);
11136 tcg_temp_free_i64(tcg_res[0]);
11137 tcg_temp_free_i64(tcg_res[1]);
11140 static void handle_3rd_wide(DisasContext *s, int is_q, int is_u, int size,
11141 int opcode, int rd, int rn, int rm)
11143 TCGv_i64 tcg_res[2];
11144 int part = is_q ? 2 : 0;
11145 int pass;
11147 for (pass = 0; pass < 2; pass++) {
11148 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
11149 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11150 TCGv_i64 tcg_op2_wide = tcg_temp_new_i64();
11151 static NeonGenWidenFn * const widenfns[3][2] = {
11152 { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
11153 { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
11154 { tcg_gen_ext_i32_i64, tcg_gen_extu_i32_i64 },
11156 NeonGenWidenFn *widenfn = widenfns[size][is_u];
11158 read_vec_element(s, tcg_op1, rn, pass, MO_64);
11159 read_vec_element_i32(s, tcg_op2, rm, part + pass, MO_32);
11160 widenfn(tcg_op2_wide, tcg_op2);
11161 tcg_temp_free_i32(tcg_op2);
11162 tcg_res[pass] = tcg_temp_new_i64();
11163 gen_neon_addl(size, (opcode == 3),
11164 tcg_res[pass], tcg_op1, tcg_op2_wide);
11165 tcg_temp_free_i64(tcg_op1);
11166 tcg_temp_free_i64(tcg_op2_wide);
11169 for (pass = 0; pass < 2; pass++) {
11170 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
11171 tcg_temp_free_i64(tcg_res[pass]);
11175 static void do_narrow_round_high_u32(TCGv_i32 res, TCGv_i64 in)
11177 tcg_gen_addi_i64(in, in, 1U << 31);
11178 tcg_gen_extrh_i64_i32(res, in);
11181 static void handle_3rd_narrowing(DisasContext *s, int is_q, int is_u, int size,
11182 int opcode, int rd, int rn, int rm)
11184 TCGv_i32 tcg_res[2];
11185 int part = is_q ? 2 : 0;
11186 int pass;
11188 for (pass = 0; pass < 2; pass++) {
11189 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
11190 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
11191 TCGv_i64 tcg_wideres = tcg_temp_new_i64();
11192 static NeonGenNarrowFn * const narrowfns[3][2] = {
11193 { gen_helper_neon_narrow_high_u8,
11194 gen_helper_neon_narrow_round_high_u8 },
11195 { gen_helper_neon_narrow_high_u16,
11196 gen_helper_neon_narrow_round_high_u16 },
11197 { tcg_gen_extrh_i64_i32, do_narrow_round_high_u32 },
11199 NeonGenNarrowFn *gennarrow = narrowfns[size][is_u];
11201 read_vec_element(s, tcg_op1, rn, pass, MO_64);
11202 read_vec_element(s, tcg_op2, rm, pass, MO_64);
11204 gen_neon_addl(size, (opcode == 6), tcg_wideres, tcg_op1, tcg_op2);
11206 tcg_temp_free_i64(tcg_op1);
11207 tcg_temp_free_i64(tcg_op2);
11209 tcg_res[pass] = tcg_temp_new_i32();
11210 gennarrow(tcg_res[pass], tcg_wideres);
11211 tcg_temp_free_i64(tcg_wideres);
11214 for (pass = 0; pass < 2; pass++) {
11215 write_vec_element_i32(s, tcg_res[pass], rd, pass + part, MO_32);
11216 tcg_temp_free_i32(tcg_res[pass]);
11218 clear_vec_high(s, is_q, rd);
11221 /* AdvSIMD three different
11222 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 0
11223 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
11224 * | 0 | Q | U | 0 1 1 1 0 | size | 1 | Rm | opcode | 0 0 | Rn | Rd |
11225 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
11227 static void disas_simd_three_reg_diff(DisasContext *s, uint32_t insn)
11229 /* Instructions in this group fall into three basic classes
11230 * (in each case with the operation working on each element in
11231 * the input vectors):
11232 * (1) widening 64 x 64 -> 128 (with possibly Vd as an extra
11233 * 128 bit input)
11234 * (2) wide 64 x 128 -> 128
11235 * (3) narrowing 128 x 128 -> 64
11236 * Here we do initial decode, catch unallocated cases and
11237 * dispatch to separate functions for each class.
11239 int is_q = extract32(insn, 30, 1);
11240 int is_u = extract32(insn, 29, 1);
11241 int size = extract32(insn, 22, 2);
11242 int opcode = extract32(insn, 12, 4);
11243 int rm = extract32(insn, 16, 5);
11244 int rn = extract32(insn, 5, 5);
11245 int rd = extract32(insn, 0, 5);
11247 switch (opcode) {
11248 case 1: /* SADDW, SADDW2, UADDW, UADDW2 */
11249 case 3: /* SSUBW, SSUBW2, USUBW, USUBW2 */
11250 /* 64 x 128 -> 128 */
11251 if (size == 3) {
11252 unallocated_encoding(s);
11253 return;
11255 if (!fp_access_check(s)) {
11256 return;
11258 handle_3rd_wide(s, is_q, is_u, size, opcode, rd, rn, rm);
11259 break;
11260 case 4: /* ADDHN, ADDHN2, RADDHN, RADDHN2 */
11261 case 6: /* SUBHN, SUBHN2, RSUBHN, RSUBHN2 */
11262 /* 128 x 128 -> 64 */
11263 if (size == 3) {
11264 unallocated_encoding(s);
11265 return;
11267 if (!fp_access_check(s)) {
11268 return;
11270 handle_3rd_narrowing(s, is_q, is_u, size, opcode, rd, rn, rm);
11271 break;
11272 case 14: /* PMULL, PMULL2 */
11273 if (is_u) {
11274 unallocated_encoding(s);
11275 return;
11277 switch (size) {
11278 case 0: /* PMULL.P8 */
11279 if (!fp_access_check(s)) {
11280 return;
11282 /* The Q field specifies lo/hi half input for this insn. */
11283 gen_gvec_op3_ool(s, true, rd, rn, rm, is_q,
11284 gen_helper_neon_pmull_h);
11285 break;
11287 case 3: /* PMULL.P64 */
11288 if (!dc_isar_feature(aa64_pmull, s)) {
11289 unallocated_encoding(s);
11290 return;
11292 if (!fp_access_check(s)) {
11293 return;
11295 /* The Q field specifies lo/hi half input for this insn. */
11296 gen_gvec_op3_ool(s, true, rd, rn, rm, is_q,
11297 gen_helper_gvec_pmull_q);
11298 break;
11300 default:
11301 unallocated_encoding(s);
11302 break;
11304 return;
11305 case 9: /* SQDMLAL, SQDMLAL2 */
11306 case 11: /* SQDMLSL, SQDMLSL2 */
11307 case 13: /* SQDMULL, SQDMULL2 */
11308 if (is_u || size == 0) {
11309 unallocated_encoding(s);
11310 return;
11312 /* fall through */
11313 case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
11314 case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
11315 case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
11316 case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
11317 case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
11318 case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
11319 case 12: /* SMULL, SMULL2, UMULL, UMULL2 */
11320 /* 64 x 64 -> 128 */
11321 if (size == 3) {
11322 unallocated_encoding(s);
11323 return;
11325 if (!fp_access_check(s)) {
11326 return;
11329 handle_3rd_widening(s, is_q, is_u, size, opcode, rd, rn, rm);
11330 break;
11331 default:
11332 /* opcode 15 not allocated */
11333 unallocated_encoding(s);
11334 break;
11338 /* Logic op (opcode == 3) subgroup of C3.6.16. */
11339 static void disas_simd_3same_logic(DisasContext *s, uint32_t insn)
11341 int rd = extract32(insn, 0, 5);
11342 int rn = extract32(insn, 5, 5);
11343 int rm = extract32(insn, 16, 5);
11344 int size = extract32(insn, 22, 2);
11345 bool is_u = extract32(insn, 29, 1);
11346 bool is_q = extract32(insn, 30, 1);
11348 if (!fp_access_check(s)) {
11349 return;
11352 switch (size + 4 * is_u) {
11353 case 0: /* AND */
11354 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_and, 0);
11355 return;
11356 case 1: /* BIC */
11357 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_andc, 0);
11358 return;
11359 case 2: /* ORR */
11360 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_or, 0);
11361 return;
11362 case 3: /* ORN */
11363 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_orc, 0);
11364 return;
11365 case 4: /* EOR */
11366 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_xor, 0);
11367 return;
11369 case 5: /* BSL bitwise select */
11370 gen_gvec_fn4(s, is_q, rd, rd, rn, rm, tcg_gen_gvec_bitsel, 0);
11371 return;
11372 case 6: /* BIT, bitwise insert if true */
11373 gen_gvec_fn4(s, is_q, rd, rm, rn, rd, tcg_gen_gvec_bitsel, 0);
11374 return;
11375 case 7: /* BIF, bitwise insert if false */
11376 gen_gvec_fn4(s, is_q, rd, rm, rd, rn, tcg_gen_gvec_bitsel, 0);
11377 return;
11379 default:
11380 g_assert_not_reached();
11384 /* Pairwise op subgroup of C3.6.16.
11386 * This is called directly or via the handle_3same_float for float pairwise
11387 * operations where the opcode and size are calculated differently.
11389 static void handle_simd_3same_pair(DisasContext *s, int is_q, int u, int opcode,
11390 int size, int rn, int rm, int rd)
11392 TCGv_ptr fpst;
11393 int pass;
11395 /* Floating point operations need fpst */
11396 if (opcode >= 0x58) {
11397 fpst = get_fpstatus_ptr(false);
11398 } else {
11399 fpst = NULL;
11402 if (!fp_access_check(s)) {
11403 return;
11406 /* These operations work on the concatenated rm:rn, with each pair of
11407 * adjacent elements being operated on to produce an element in the result.
11409 if (size == 3) {
11410 TCGv_i64 tcg_res[2];
11412 for (pass = 0; pass < 2; pass++) {
11413 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
11414 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
11415 int passreg = (pass == 0) ? rn : rm;
11417 read_vec_element(s, tcg_op1, passreg, 0, MO_64);
11418 read_vec_element(s, tcg_op2, passreg, 1, MO_64);
11419 tcg_res[pass] = tcg_temp_new_i64();
11421 switch (opcode) {
11422 case 0x17: /* ADDP */
11423 tcg_gen_add_i64(tcg_res[pass], tcg_op1, tcg_op2);
11424 break;
11425 case 0x58: /* FMAXNMP */
11426 gen_helper_vfp_maxnumd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11427 break;
11428 case 0x5a: /* FADDP */
11429 gen_helper_vfp_addd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11430 break;
11431 case 0x5e: /* FMAXP */
11432 gen_helper_vfp_maxd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11433 break;
11434 case 0x78: /* FMINNMP */
11435 gen_helper_vfp_minnumd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11436 break;
11437 case 0x7e: /* FMINP */
11438 gen_helper_vfp_mind(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11439 break;
11440 default:
11441 g_assert_not_reached();
11444 tcg_temp_free_i64(tcg_op1);
11445 tcg_temp_free_i64(tcg_op2);
11448 for (pass = 0; pass < 2; pass++) {
11449 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
11450 tcg_temp_free_i64(tcg_res[pass]);
11452 } else {
11453 int maxpass = is_q ? 4 : 2;
11454 TCGv_i32 tcg_res[4];
11456 for (pass = 0; pass < maxpass; pass++) {
11457 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11458 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11459 NeonGenTwoOpFn *genfn = NULL;
11460 int passreg = pass < (maxpass / 2) ? rn : rm;
11461 int passelt = (is_q && (pass & 1)) ? 2 : 0;
11463 read_vec_element_i32(s, tcg_op1, passreg, passelt, MO_32);
11464 read_vec_element_i32(s, tcg_op2, passreg, passelt + 1, MO_32);
11465 tcg_res[pass] = tcg_temp_new_i32();
11467 switch (opcode) {
11468 case 0x17: /* ADDP */
11470 static NeonGenTwoOpFn * const fns[3] = {
11471 gen_helper_neon_padd_u8,
11472 gen_helper_neon_padd_u16,
11473 tcg_gen_add_i32,
11475 genfn = fns[size];
11476 break;
11478 case 0x14: /* SMAXP, UMAXP */
11480 static NeonGenTwoOpFn * const fns[3][2] = {
11481 { gen_helper_neon_pmax_s8, gen_helper_neon_pmax_u8 },
11482 { gen_helper_neon_pmax_s16, gen_helper_neon_pmax_u16 },
11483 { tcg_gen_smax_i32, tcg_gen_umax_i32 },
11485 genfn = fns[size][u];
11486 break;
11488 case 0x15: /* SMINP, UMINP */
11490 static NeonGenTwoOpFn * const fns[3][2] = {
11491 { gen_helper_neon_pmin_s8, gen_helper_neon_pmin_u8 },
11492 { gen_helper_neon_pmin_s16, gen_helper_neon_pmin_u16 },
11493 { tcg_gen_smin_i32, tcg_gen_umin_i32 },
11495 genfn = fns[size][u];
11496 break;
11498 /* The FP operations are all on single floats (32 bit) */
11499 case 0x58: /* FMAXNMP */
11500 gen_helper_vfp_maxnums(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11501 break;
11502 case 0x5a: /* FADDP */
11503 gen_helper_vfp_adds(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11504 break;
11505 case 0x5e: /* FMAXP */
11506 gen_helper_vfp_maxs(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11507 break;
11508 case 0x78: /* FMINNMP */
11509 gen_helper_vfp_minnums(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11510 break;
11511 case 0x7e: /* FMINP */
11512 gen_helper_vfp_mins(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11513 break;
11514 default:
11515 g_assert_not_reached();
11518 /* FP ops called directly, otherwise call now */
11519 if (genfn) {
11520 genfn(tcg_res[pass], tcg_op1, tcg_op2);
11523 tcg_temp_free_i32(tcg_op1);
11524 tcg_temp_free_i32(tcg_op2);
11527 for (pass = 0; pass < maxpass; pass++) {
11528 write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_32);
11529 tcg_temp_free_i32(tcg_res[pass]);
11531 clear_vec_high(s, is_q, rd);
11534 if (fpst) {
11535 tcg_temp_free_ptr(fpst);
11539 /* Floating point op subgroup of C3.6.16. */
11540 static void disas_simd_3same_float(DisasContext *s, uint32_t insn)
11542 /* For floating point ops, the U, size[1] and opcode bits
11543 * together indicate the operation. size[0] indicates single
11544 * or double.
11546 int fpopcode = extract32(insn, 11, 5)
11547 | (extract32(insn, 23, 1) << 5)
11548 | (extract32(insn, 29, 1) << 6);
11549 int is_q = extract32(insn, 30, 1);
11550 int size = extract32(insn, 22, 1);
11551 int rm = extract32(insn, 16, 5);
11552 int rn = extract32(insn, 5, 5);
11553 int rd = extract32(insn, 0, 5);
11555 int datasize = is_q ? 128 : 64;
11556 int esize = 32 << size;
11557 int elements = datasize / esize;
11559 if (size == 1 && !is_q) {
11560 unallocated_encoding(s);
11561 return;
11564 switch (fpopcode) {
11565 case 0x58: /* FMAXNMP */
11566 case 0x5a: /* FADDP */
11567 case 0x5e: /* FMAXP */
11568 case 0x78: /* FMINNMP */
11569 case 0x7e: /* FMINP */
11570 if (size && !is_q) {
11571 unallocated_encoding(s);
11572 return;
11574 handle_simd_3same_pair(s, is_q, 0, fpopcode, size ? MO_64 : MO_32,
11575 rn, rm, rd);
11576 return;
11577 case 0x1b: /* FMULX */
11578 case 0x1f: /* FRECPS */
11579 case 0x3f: /* FRSQRTS */
11580 case 0x5d: /* FACGE */
11581 case 0x7d: /* FACGT */
11582 case 0x19: /* FMLA */
11583 case 0x39: /* FMLS */
11584 case 0x18: /* FMAXNM */
11585 case 0x1a: /* FADD */
11586 case 0x1c: /* FCMEQ */
11587 case 0x1e: /* FMAX */
11588 case 0x38: /* FMINNM */
11589 case 0x3a: /* FSUB */
11590 case 0x3e: /* FMIN */
11591 case 0x5b: /* FMUL */
11592 case 0x5c: /* FCMGE */
11593 case 0x5f: /* FDIV */
11594 case 0x7a: /* FABD */
11595 case 0x7c: /* FCMGT */
11596 if (!fp_access_check(s)) {
11597 return;
11599 handle_3same_float(s, size, elements, fpopcode, rd, rn, rm);
11600 return;
11602 case 0x1d: /* FMLAL */
11603 case 0x3d: /* FMLSL */
11604 case 0x59: /* FMLAL2 */
11605 case 0x79: /* FMLSL2 */
11606 if (size & 1 || !dc_isar_feature(aa64_fhm, s)) {
11607 unallocated_encoding(s);
11608 return;
11610 if (fp_access_check(s)) {
11611 int is_s = extract32(insn, 23, 1);
11612 int is_2 = extract32(insn, 29, 1);
11613 int data = (is_2 << 1) | is_s;
11614 tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
11615 vec_full_reg_offset(s, rn),
11616 vec_full_reg_offset(s, rm), cpu_env,
11617 is_q ? 16 : 8, vec_full_reg_size(s),
11618 data, gen_helper_gvec_fmlal_a64);
11620 return;
11622 default:
11623 unallocated_encoding(s);
11624 return;
11628 /* Integer op subgroup of C3.6.16. */
11629 static void disas_simd_3same_int(DisasContext *s, uint32_t insn)
11631 int is_q = extract32(insn, 30, 1);
11632 int u = extract32(insn, 29, 1);
11633 int size = extract32(insn, 22, 2);
11634 int opcode = extract32(insn, 11, 5);
11635 int rm = extract32(insn, 16, 5);
11636 int rn = extract32(insn, 5, 5);
11637 int rd = extract32(insn, 0, 5);
11638 int pass;
11639 TCGCond cond;
11641 switch (opcode) {
11642 case 0x13: /* MUL, PMUL */
11643 if (u && size != 0) {
11644 unallocated_encoding(s);
11645 return;
11647 /* fall through */
11648 case 0x0: /* SHADD, UHADD */
11649 case 0x2: /* SRHADD, URHADD */
11650 case 0x4: /* SHSUB, UHSUB */
11651 case 0xc: /* SMAX, UMAX */
11652 case 0xd: /* SMIN, UMIN */
11653 case 0xe: /* SABD, UABD */
11654 case 0xf: /* SABA, UABA */
11655 case 0x12: /* MLA, MLS */
11656 if (size == 3) {
11657 unallocated_encoding(s);
11658 return;
11660 break;
11661 case 0x16: /* SQDMULH, SQRDMULH */
11662 if (size == 0 || size == 3) {
11663 unallocated_encoding(s);
11664 return;
11666 break;
11667 default:
11668 if (size == 3 && !is_q) {
11669 unallocated_encoding(s);
11670 return;
11672 break;
11675 if (!fp_access_check(s)) {
11676 return;
11679 switch (opcode) {
11680 case 0x01: /* SQADD, UQADD */
11681 if (u) {
11682 gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uqadd_qc, size);
11683 } else {
11684 gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqadd_qc, size);
11686 return;
11687 case 0x05: /* SQSUB, UQSUB */
11688 if (u) {
11689 gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uqsub_qc, size);
11690 } else {
11691 gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqsub_qc, size);
11693 return;
11694 case 0x08: /* SSHL, USHL */
11695 if (u) {
11696 gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_ushl, size);
11697 } else {
11698 gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sshl, size);
11700 return;
11701 case 0x0c: /* SMAX, UMAX */
11702 if (u) {
11703 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_umax, size);
11704 } else {
11705 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_smax, size);
11707 return;
11708 case 0x0d: /* SMIN, UMIN */
11709 if (u) {
11710 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_umin, size);
11711 } else {
11712 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_smin, size);
11714 return;
11715 case 0xe: /* SABD, UABD */
11716 if (u) {
11717 gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uabd, size);
11718 } else {
11719 gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sabd, size);
11721 return;
11722 case 0xf: /* SABA, UABA */
11723 if (u) {
11724 gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uaba, size);
11725 } else {
11726 gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_saba, size);
11728 return;
11729 case 0x10: /* ADD, SUB */
11730 if (u) {
11731 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_sub, size);
11732 } else {
11733 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_add, size);
11735 return;
11736 case 0x13: /* MUL, PMUL */
11737 if (!u) { /* MUL */
11738 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_mul, size);
11739 } else { /* PMUL */
11740 gen_gvec_op3_ool(s, is_q, rd, rn, rm, 0, gen_helper_gvec_pmul_b);
11742 return;
11743 case 0x12: /* MLA, MLS */
11744 if (u) {
11745 gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_mls, size);
11746 } else {
11747 gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_mla, size);
11749 return;
11750 case 0x11:
11751 if (!u) { /* CMTST */
11752 gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_cmtst, size);
11753 return;
11755 /* else CMEQ */
11756 cond = TCG_COND_EQ;
11757 goto do_gvec_cmp;
11758 case 0x06: /* CMGT, CMHI */
11759 cond = u ? TCG_COND_GTU : TCG_COND_GT;
11760 goto do_gvec_cmp;
11761 case 0x07: /* CMGE, CMHS */
11762 cond = u ? TCG_COND_GEU : TCG_COND_GE;
11763 do_gvec_cmp:
11764 tcg_gen_gvec_cmp(cond, size, vec_full_reg_offset(s, rd),
11765 vec_full_reg_offset(s, rn),
11766 vec_full_reg_offset(s, rm),
11767 is_q ? 16 : 8, vec_full_reg_size(s));
11768 return;
11771 if (size == 3) {
11772 assert(is_q);
11773 for (pass = 0; pass < 2; pass++) {
11774 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
11775 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
11776 TCGv_i64 tcg_res = tcg_temp_new_i64();
11778 read_vec_element(s, tcg_op1, rn, pass, MO_64);
11779 read_vec_element(s, tcg_op2, rm, pass, MO_64);
11781 handle_3same_64(s, opcode, u, tcg_res, tcg_op1, tcg_op2);
11783 write_vec_element(s, tcg_res, rd, pass, MO_64);
11785 tcg_temp_free_i64(tcg_res);
11786 tcg_temp_free_i64(tcg_op1);
11787 tcg_temp_free_i64(tcg_op2);
11789 } else {
11790 for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
11791 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11792 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11793 TCGv_i32 tcg_res = tcg_temp_new_i32();
11794 NeonGenTwoOpFn *genfn = NULL;
11795 NeonGenTwoOpEnvFn *genenvfn = NULL;
11797 read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
11798 read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
11800 switch (opcode) {
11801 case 0x0: /* SHADD, UHADD */
11803 static NeonGenTwoOpFn * const fns[3][2] = {
11804 { gen_helper_neon_hadd_s8, gen_helper_neon_hadd_u8 },
11805 { gen_helper_neon_hadd_s16, gen_helper_neon_hadd_u16 },
11806 { gen_helper_neon_hadd_s32, gen_helper_neon_hadd_u32 },
11808 genfn = fns[size][u];
11809 break;
11811 case 0x2: /* SRHADD, URHADD */
11813 static NeonGenTwoOpFn * const fns[3][2] = {
11814 { gen_helper_neon_rhadd_s8, gen_helper_neon_rhadd_u8 },
11815 { gen_helper_neon_rhadd_s16, gen_helper_neon_rhadd_u16 },
11816 { gen_helper_neon_rhadd_s32, gen_helper_neon_rhadd_u32 },
11818 genfn = fns[size][u];
11819 break;
11821 case 0x4: /* SHSUB, UHSUB */
11823 static NeonGenTwoOpFn * const fns[3][2] = {
11824 { gen_helper_neon_hsub_s8, gen_helper_neon_hsub_u8 },
11825 { gen_helper_neon_hsub_s16, gen_helper_neon_hsub_u16 },
11826 { gen_helper_neon_hsub_s32, gen_helper_neon_hsub_u32 },
11828 genfn = fns[size][u];
11829 break;
11831 case 0x9: /* SQSHL, UQSHL */
11833 static NeonGenTwoOpEnvFn * const fns[3][2] = {
11834 { gen_helper_neon_qshl_s8, gen_helper_neon_qshl_u8 },
11835 { gen_helper_neon_qshl_s16, gen_helper_neon_qshl_u16 },
11836 { gen_helper_neon_qshl_s32, gen_helper_neon_qshl_u32 },
11838 genenvfn = fns[size][u];
11839 break;
11841 case 0xa: /* SRSHL, URSHL */
11843 static NeonGenTwoOpFn * const fns[3][2] = {
11844 { gen_helper_neon_rshl_s8, gen_helper_neon_rshl_u8 },
11845 { gen_helper_neon_rshl_s16, gen_helper_neon_rshl_u16 },
11846 { gen_helper_neon_rshl_s32, gen_helper_neon_rshl_u32 },
11848 genfn = fns[size][u];
11849 break;
11851 case 0xb: /* SQRSHL, UQRSHL */
11853 static NeonGenTwoOpEnvFn * const fns[3][2] = {
11854 { gen_helper_neon_qrshl_s8, gen_helper_neon_qrshl_u8 },
11855 { gen_helper_neon_qrshl_s16, gen_helper_neon_qrshl_u16 },
11856 { gen_helper_neon_qrshl_s32, gen_helper_neon_qrshl_u32 },
11858 genenvfn = fns[size][u];
11859 break;
11861 case 0x16: /* SQDMULH, SQRDMULH */
11863 static NeonGenTwoOpEnvFn * const fns[2][2] = {
11864 { gen_helper_neon_qdmulh_s16, gen_helper_neon_qrdmulh_s16 },
11865 { gen_helper_neon_qdmulh_s32, gen_helper_neon_qrdmulh_s32 },
11867 assert(size == 1 || size == 2);
11868 genenvfn = fns[size - 1][u];
11869 break;
11871 default:
11872 g_assert_not_reached();
11875 if (genenvfn) {
11876 genenvfn(tcg_res, cpu_env, tcg_op1, tcg_op2);
11877 } else {
11878 genfn(tcg_res, tcg_op1, tcg_op2);
11881 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
11883 tcg_temp_free_i32(tcg_res);
11884 tcg_temp_free_i32(tcg_op1);
11885 tcg_temp_free_i32(tcg_op2);
11888 clear_vec_high(s, is_q, rd);
11891 /* AdvSIMD three same
11892 * 31 30 29 28 24 23 22 21 20 16 15 11 10 9 5 4 0
11893 * +---+---+---+-----------+------+---+------+--------+---+------+------+
11894 * | 0 | Q | U | 0 1 1 1 0 | size | 1 | Rm | opcode | 1 | Rn | Rd |
11895 * +---+---+---+-----------+------+---+------+--------+---+------+------+
11897 static void disas_simd_three_reg_same(DisasContext *s, uint32_t insn)
11899 int opcode = extract32(insn, 11, 5);
11901 switch (opcode) {
11902 case 0x3: /* logic ops */
11903 disas_simd_3same_logic(s, insn);
11904 break;
11905 case 0x17: /* ADDP */
11906 case 0x14: /* SMAXP, UMAXP */
11907 case 0x15: /* SMINP, UMINP */
11909 /* Pairwise operations */
11910 int is_q = extract32(insn, 30, 1);
11911 int u = extract32(insn, 29, 1);
11912 int size = extract32(insn, 22, 2);
11913 int rm = extract32(insn, 16, 5);
11914 int rn = extract32(insn, 5, 5);
11915 int rd = extract32(insn, 0, 5);
11916 if (opcode == 0x17) {
11917 if (u || (size == 3 && !is_q)) {
11918 unallocated_encoding(s);
11919 return;
11921 } else {
11922 if (size == 3) {
11923 unallocated_encoding(s);
11924 return;
11927 handle_simd_3same_pair(s, is_q, u, opcode, size, rn, rm, rd);
11928 break;
11930 case 0x18 ... 0x31:
11931 /* floating point ops, sz[1] and U are part of opcode */
11932 disas_simd_3same_float(s, insn);
11933 break;
11934 default:
11935 disas_simd_3same_int(s, insn);
11936 break;
11941 * Advanced SIMD three same (ARMv8.2 FP16 variants)
11943 * 31 30 29 28 24 23 22 21 20 16 15 14 13 11 10 9 5 4 0
11944 * +---+---+---+-----------+---------+------+-----+--------+---+------+------+
11945 * | 0 | Q | U | 0 1 1 1 0 | a | 1 0 | Rm | 0 0 | opcode | 1 | Rn | Rd |
11946 * +---+---+---+-----------+---------+------+-----+--------+---+------+------+
11948 * This includes FMULX, FCMEQ (register), FRECPS, FRSQRTS, FCMGE
11949 * (register), FACGE, FABD, FCMGT (register) and FACGT.
11952 static void disas_simd_three_reg_same_fp16(DisasContext *s, uint32_t insn)
11954 int opcode, fpopcode;
11955 int is_q, u, a, rm, rn, rd;
11956 int datasize, elements;
11957 int pass;
11958 TCGv_ptr fpst;
11959 bool pairwise = false;
11961 if (!dc_isar_feature(aa64_fp16, s)) {
11962 unallocated_encoding(s);
11963 return;
11966 if (!fp_access_check(s)) {
11967 return;
11970 /* For these floating point ops, the U, a and opcode bits
11971 * together indicate the operation.
11973 opcode = extract32(insn, 11, 3);
11974 u = extract32(insn, 29, 1);
11975 a = extract32(insn, 23, 1);
11976 is_q = extract32(insn, 30, 1);
11977 rm = extract32(insn, 16, 5);
11978 rn = extract32(insn, 5, 5);
11979 rd = extract32(insn, 0, 5);
11981 fpopcode = opcode | (a << 3) | (u << 4);
11982 datasize = is_q ? 128 : 64;
11983 elements = datasize / 16;
11985 switch (fpopcode) {
11986 case 0x10: /* FMAXNMP */
11987 case 0x12: /* FADDP */
11988 case 0x16: /* FMAXP */
11989 case 0x18: /* FMINNMP */
11990 case 0x1e: /* FMINP */
11991 pairwise = true;
11992 break;
11995 fpst = get_fpstatus_ptr(true);
11997 if (pairwise) {
11998 int maxpass = is_q ? 8 : 4;
11999 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
12000 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
12001 TCGv_i32 tcg_res[8];
12003 for (pass = 0; pass < maxpass; pass++) {
12004 int passreg = pass < (maxpass / 2) ? rn : rm;
12005 int passelt = (pass << 1) & (maxpass - 1);
12007 read_vec_element_i32(s, tcg_op1, passreg, passelt, MO_16);
12008 read_vec_element_i32(s, tcg_op2, passreg, passelt + 1, MO_16);
12009 tcg_res[pass] = tcg_temp_new_i32();
12011 switch (fpopcode) {
12012 case 0x10: /* FMAXNMP */
12013 gen_helper_advsimd_maxnumh(tcg_res[pass], tcg_op1, tcg_op2,
12014 fpst);
12015 break;
12016 case 0x12: /* FADDP */
12017 gen_helper_advsimd_addh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
12018 break;
12019 case 0x16: /* FMAXP */
12020 gen_helper_advsimd_maxh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
12021 break;
12022 case 0x18: /* FMINNMP */
12023 gen_helper_advsimd_minnumh(tcg_res[pass], tcg_op1, tcg_op2,
12024 fpst);
12025 break;
12026 case 0x1e: /* FMINP */
12027 gen_helper_advsimd_minh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
12028 break;
12029 default:
12030 g_assert_not_reached();
12034 for (pass = 0; pass < maxpass; pass++) {
12035 write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_16);
12036 tcg_temp_free_i32(tcg_res[pass]);
12039 tcg_temp_free_i32(tcg_op1);
12040 tcg_temp_free_i32(tcg_op2);
12042 } else {
12043 for (pass = 0; pass < elements; pass++) {
12044 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
12045 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
12046 TCGv_i32 tcg_res = tcg_temp_new_i32();
12048 read_vec_element_i32(s, tcg_op1, rn, pass, MO_16);
12049 read_vec_element_i32(s, tcg_op2, rm, pass, MO_16);
12051 switch (fpopcode) {
12052 case 0x0: /* FMAXNM */
12053 gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
12054 break;
12055 case 0x1: /* FMLA */
12056 read_vec_element_i32(s, tcg_res, rd, pass, MO_16);
12057 gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_res,
12058 fpst);
12059 break;
12060 case 0x2: /* FADD */
12061 gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
12062 break;
12063 case 0x3: /* FMULX */
12064 gen_helper_advsimd_mulxh(tcg_res, tcg_op1, tcg_op2, fpst);
12065 break;
12066 case 0x4: /* FCMEQ */
12067 gen_helper_advsimd_ceq_f16(tcg_res, tcg_op1, tcg_op2, fpst);
12068 break;
12069 case 0x6: /* FMAX */
12070 gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
12071 break;
12072 case 0x7: /* FRECPS */
12073 gen_helper_recpsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
12074 break;
12075 case 0x8: /* FMINNM */
12076 gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
12077 break;
12078 case 0x9: /* FMLS */
12079 /* As usual for ARM, separate negation for fused multiply-add */
12080 tcg_gen_xori_i32(tcg_op1, tcg_op1, 0x8000);
12081 read_vec_element_i32(s, tcg_res, rd, pass, MO_16);
12082 gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_res,
12083 fpst);
12084 break;
12085 case 0xa: /* FSUB */
12086 gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
12087 break;
12088 case 0xe: /* FMIN */
12089 gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
12090 break;
12091 case 0xf: /* FRSQRTS */
12092 gen_helper_rsqrtsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
12093 break;
12094 case 0x13: /* FMUL */
12095 gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
12096 break;
12097 case 0x14: /* FCMGE */
12098 gen_helper_advsimd_cge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
12099 break;
12100 case 0x15: /* FACGE */
12101 gen_helper_advsimd_acge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
12102 break;
12103 case 0x17: /* FDIV */
12104 gen_helper_advsimd_divh(tcg_res, tcg_op1, tcg_op2, fpst);
12105 break;
12106 case 0x1a: /* FABD */
12107 gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
12108 tcg_gen_andi_i32(tcg_res, tcg_res, 0x7fff);
12109 break;
12110 case 0x1c: /* FCMGT */
12111 gen_helper_advsimd_cgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
12112 break;
12113 case 0x1d: /* FACGT */
12114 gen_helper_advsimd_acgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
12115 break;
12116 default:
12117 fprintf(stderr, "%s: insn %#04x, fpop %#2x @ %#" PRIx64 "\n",
12118 __func__, insn, fpopcode, s->pc_curr);
12119 g_assert_not_reached();
12122 write_vec_element_i32(s, tcg_res, rd, pass, MO_16);
12123 tcg_temp_free_i32(tcg_res);
12124 tcg_temp_free_i32(tcg_op1);
12125 tcg_temp_free_i32(tcg_op2);
12129 tcg_temp_free_ptr(fpst);
12131 clear_vec_high(s, is_q, rd);
12134 /* AdvSIMD three same extra
12135 * 31 30 29 28 24 23 22 21 20 16 15 14 11 10 9 5 4 0
12136 * +---+---+---+-----------+------+---+------+---+--------+---+----+----+
12137 * | 0 | Q | U | 0 1 1 1 0 | size | 0 | Rm | 1 | opcode | 1 | Rn | Rd |
12138 * +---+---+---+-----------+------+---+------+---+--------+---+----+----+
12140 static void disas_simd_three_reg_same_extra(DisasContext *s, uint32_t insn)
12142 int rd = extract32(insn, 0, 5);
12143 int rn = extract32(insn, 5, 5);
12144 int opcode = extract32(insn, 11, 4);
12145 int rm = extract32(insn, 16, 5);
12146 int size = extract32(insn, 22, 2);
12147 bool u = extract32(insn, 29, 1);
12148 bool is_q = extract32(insn, 30, 1);
12149 bool feature;
12150 int rot;
12152 switch (u * 16 + opcode) {
12153 case 0x10: /* SQRDMLAH (vector) */
12154 case 0x11: /* SQRDMLSH (vector) */
12155 if (size != 1 && size != 2) {
12156 unallocated_encoding(s);
12157 return;
12159 feature = dc_isar_feature(aa64_rdm, s);
12160 break;
12161 case 0x02: /* SDOT (vector) */
12162 case 0x12: /* UDOT (vector) */
12163 if (size != MO_32) {
12164 unallocated_encoding(s);
12165 return;
12167 feature = dc_isar_feature(aa64_dp, s);
12168 break;
12169 case 0x18: /* FCMLA, #0 */
12170 case 0x19: /* FCMLA, #90 */
12171 case 0x1a: /* FCMLA, #180 */
12172 case 0x1b: /* FCMLA, #270 */
12173 case 0x1c: /* FCADD, #90 */
12174 case 0x1e: /* FCADD, #270 */
12175 if (size == 0
12176 || (size == 1 && !dc_isar_feature(aa64_fp16, s))
12177 || (size == 3 && !is_q)) {
12178 unallocated_encoding(s);
12179 return;
12181 feature = dc_isar_feature(aa64_fcma, s);
12182 break;
12183 default:
12184 unallocated_encoding(s);
12185 return;
12187 if (!feature) {
12188 unallocated_encoding(s);
12189 return;
12191 if (!fp_access_check(s)) {
12192 return;
12195 switch (opcode) {
12196 case 0x0: /* SQRDMLAH (vector) */
12197 gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqrdmlah_qc, size);
12198 return;
12200 case 0x1: /* SQRDMLSH (vector) */
12201 gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqrdmlsh_qc, size);
12202 return;
12204 case 0x2: /* SDOT / UDOT */
12205 gen_gvec_op3_ool(s, is_q, rd, rn, rm, 0,
12206 u ? gen_helper_gvec_udot_b : gen_helper_gvec_sdot_b);
12207 return;
12209 case 0x8: /* FCMLA, #0 */
12210 case 0x9: /* FCMLA, #90 */
12211 case 0xa: /* FCMLA, #180 */
12212 case 0xb: /* FCMLA, #270 */
12213 rot = extract32(opcode, 0, 2);
12214 switch (size) {
12215 case 1:
12216 gen_gvec_op3_fpst(s, is_q, rd, rn, rm, true, rot,
12217 gen_helper_gvec_fcmlah);
12218 break;
12219 case 2:
12220 gen_gvec_op3_fpst(s, is_q, rd, rn, rm, false, rot,
12221 gen_helper_gvec_fcmlas);
12222 break;
12223 case 3:
12224 gen_gvec_op3_fpst(s, is_q, rd, rn, rm, false, rot,
12225 gen_helper_gvec_fcmlad);
12226 break;
12227 default:
12228 g_assert_not_reached();
12230 return;
12232 case 0xc: /* FCADD, #90 */
12233 case 0xe: /* FCADD, #270 */
12234 rot = extract32(opcode, 1, 1);
12235 switch (size) {
12236 case 1:
12237 gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
12238 gen_helper_gvec_fcaddh);
12239 break;
12240 case 2:
12241 gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
12242 gen_helper_gvec_fcadds);
12243 break;
12244 case 3:
12245 gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
12246 gen_helper_gvec_fcaddd);
12247 break;
12248 default:
12249 g_assert_not_reached();
12251 return;
12253 default:
12254 g_assert_not_reached();
12258 static void handle_2misc_widening(DisasContext *s, int opcode, bool is_q,
12259 int size, int rn, int rd)
12261 /* Handle 2-reg-misc ops which are widening (so each size element
12262 * in the source becomes a 2*size element in the destination.
12263 * The only instruction like this is FCVTL.
12265 int pass;
12267 if (size == 3) {
12268 /* 32 -> 64 bit fp conversion */
12269 TCGv_i64 tcg_res[2];
12270 int srcelt = is_q ? 2 : 0;
12272 for (pass = 0; pass < 2; pass++) {
12273 TCGv_i32 tcg_op = tcg_temp_new_i32();
12274 tcg_res[pass] = tcg_temp_new_i64();
12276 read_vec_element_i32(s, tcg_op, rn, srcelt + pass, MO_32);
12277 gen_helper_vfp_fcvtds(tcg_res[pass], tcg_op, cpu_env);
12278 tcg_temp_free_i32(tcg_op);
12280 for (pass = 0; pass < 2; pass++) {
12281 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
12282 tcg_temp_free_i64(tcg_res[pass]);
12284 } else {
12285 /* 16 -> 32 bit fp conversion */
12286 int srcelt = is_q ? 4 : 0;
12287 TCGv_i32 tcg_res[4];
12288 TCGv_ptr fpst = get_fpstatus_ptr(false);
12289 TCGv_i32 ahp = get_ahp_flag();
12291 for (pass = 0; pass < 4; pass++) {
12292 tcg_res[pass] = tcg_temp_new_i32();
12294 read_vec_element_i32(s, tcg_res[pass], rn, srcelt + pass, MO_16);
12295 gen_helper_vfp_fcvt_f16_to_f32(tcg_res[pass], tcg_res[pass],
12296 fpst, ahp);
12298 for (pass = 0; pass < 4; pass++) {
12299 write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_32);
12300 tcg_temp_free_i32(tcg_res[pass]);
12303 tcg_temp_free_ptr(fpst);
12304 tcg_temp_free_i32(ahp);
12308 static void handle_rev(DisasContext *s, int opcode, bool u,
12309 bool is_q, int size, int rn, int rd)
12311 int op = (opcode << 1) | u;
12312 int opsz = op + size;
12313 int grp_size = 3 - opsz;
12314 int dsize = is_q ? 128 : 64;
12315 int i;
12317 if (opsz >= 3) {
12318 unallocated_encoding(s);
12319 return;
12322 if (!fp_access_check(s)) {
12323 return;
12326 if (size == 0) {
12327 /* Special case bytes, use bswap op on each group of elements */
12328 int groups = dsize / (8 << grp_size);
12330 for (i = 0; i < groups; i++) {
12331 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
12333 read_vec_element(s, tcg_tmp, rn, i, grp_size);
12334 switch (grp_size) {
12335 case MO_16:
12336 tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp);
12337 break;
12338 case MO_32:
12339 tcg_gen_bswap32_i64(tcg_tmp, tcg_tmp);
12340 break;
12341 case MO_64:
12342 tcg_gen_bswap64_i64(tcg_tmp, tcg_tmp);
12343 break;
12344 default:
12345 g_assert_not_reached();
12347 write_vec_element(s, tcg_tmp, rd, i, grp_size);
12348 tcg_temp_free_i64(tcg_tmp);
12350 clear_vec_high(s, is_q, rd);
12351 } else {
12352 int revmask = (1 << grp_size) - 1;
12353 int esize = 8 << size;
12354 int elements = dsize / esize;
12355 TCGv_i64 tcg_rn = tcg_temp_new_i64();
12356 TCGv_i64 tcg_rd = tcg_const_i64(0);
12357 TCGv_i64 tcg_rd_hi = tcg_const_i64(0);
12359 for (i = 0; i < elements; i++) {
12360 int e_rev = (i & 0xf) ^ revmask;
12361 int off = e_rev * esize;
12362 read_vec_element(s, tcg_rn, rn, i, size);
12363 if (off >= 64) {
12364 tcg_gen_deposit_i64(tcg_rd_hi, tcg_rd_hi,
12365 tcg_rn, off - 64, esize);
12366 } else {
12367 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, off, esize);
12370 write_vec_element(s, tcg_rd, rd, 0, MO_64);
12371 write_vec_element(s, tcg_rd_hi, rd, 1, MO_64);
12373 tcg_temp_free_i64(tcg_rd_hi);
12374 tcg_temp_free_i64(tcg_rd);
12375 tcg_temp_free_i64(tcg_rn);
12379 static void handle_2misc_pairwise(DisasContext *s, int opcode, bool u,
12380 bool is_q, int size, int rn, int rd)
12382 /* Implement the pairwise operations from 2-misc:
12383 * SADDLP, UADDLP, SADALP, UADALP.
12384 * These all add pairs of elements in the input to produce a
12385 * double-width result element in the output (possibly accumulating).
12387 bool accum = (opcode == 0x6);
12388 int maxpass = is_q ? 2 : 1;
12389 int pass;
12390 TCGv_i64 tcg_res[2];
12392 if (size == 2) {
12393 /* 32 + 32 -> 64 op */
12394 MemOp memop = size + (u ? 0 : MO_SIGN);
12396 for (pass = 0; pass < maxpass; pass++) {
12397 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
12398 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
12400 tcg_res[pass] = tcg_temp_new_i64();
12402 read_vec_element(s, tcg_op1, rn, pass * 2, memop);
12403 read_vec_element(s, tcg_op2, rn, pass * 2 + 1, memop);
12404 tcg_gen_add_i64(tcg_res[pass], tcg_op1, tcg_op2);
12405 if (accum) {
12406 read_vec_element(s, tcg_op1, rd, pass, MO_64);
12407 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
12410 tcg_temp_free_i64(tcg_op1);
12411 tcg_temp_free_i64(tcg_op2);
12413 } else {
12414 for (pass = 0; pass < maxpass; pass++) {
12415 TCGv_i64 tcg_op = tcg_temp_new_i64();
12416 NeonGenOne64OpFn *genfn;
12417 static NeonGenOne64OpFn * const fns[2][2] = {
12418 { gen_helper_neon_addlp_s8, gen_helper_neon_addlp_u8 },
12419 { gen_helper_neon_addlp_s16, gen_helper_neon_addlp_u16 },
12422 genfn = fns[size][u];
12424 tcg_res[pass] = tcg_temp_new_i64();
12426 read_vec_element(s, tcg_op, rn, pass, MO_64);
12427 genfn(tcg_res[pass], tcg_op);
12429 if (accum) {
12430 read_vec_element(s, tcg_op, rd, pass, MO_64);
12431 if (size == 0) {
12432 gen_helper_neon_addl_u16(tcg_res[pass],
12433 tcg_res[pass], tcg_op);
12434 } else {
12435 gen_helper_neon_addl_u32(tcg_res[pass],
12436 tcg_res[pass], tcg_op);
12439 tcg_temp_free_i64(tcg_op);
12442 if (!is_q) {
12443 tcg_res[1] = tcg_const_i64(0);
12445 for (pass = 0; pass < 2; pass++) {
12446 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
12447 tcg_temp_free_i64(tcg_res[pass]);
12451 static void handle_shll(DisasContext *s, bool is_q, int size, int rn, int rd)
12453 /* Implement SHLL and SHLL2 */
12454 int pass;
12455 int part = is_q ? 2 : 0;
12456 TCGv_i64 tcg_res[2];
12458 for (pass = 0; pass < 2; pass++) {
12459 static NeonGenWidenFn * const widenfns[3] = {
12460 gen_helper_neon_widen_u8,
12461 gen_helper_neon_widen_u16,
12462 tcg_gen_extu_i32_i64,
12464 NeonGenWidenFn *widenfn = widenfns[size];
12465 TCGv_i32 tcg_op = tcg_temp_new_i32();
12467 read_vec_element_i32(s, tcg_op, rn, part + pass, MO_32);
12468 tcg_res[pass] = tcg_temp_new_i64();
12469 widenfn(tcg_res[pass], tcg_op);
12470 tcg_gen_shli_i64(tcg_res[pass], tcg_res[pass], 8 << size);
12472 tcg_temp_free_i32(tcg_op);
12475 for (pass = 0; pass < 2; pass++) {
12476 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
12477 tcg_temp_free_i64(tcg_res[pass]);
12481 /* AdvSIMD two reg misc
12482 * 31 30 29 28 24 23 22 21 17 16 12 11 10 9 5 4 0
12483 * +---+---+---+-----------+------+-----------+--------+-----+------+------+
12484 * | 0 | Q | U | 0 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 | Rn | Rd |
12485 * +---+---+---+-----------+------+-----------+--------+-----+------+------+
12487 static void disas_simd_two_reg_misc(DisasContext *s, uint32_t insn)
12489 int size = extract32(insn, 22, 2);
12490 int opcode = extract32(insn, 12, 5);
12491 bool u = extract32(insn, 29, 1);
12492 bool is_q = extract32(insn, 30, 1);
12493 int rn = extract32(insn, 5, 5);
12494 int rd = extract32(insn, 0, 5);
12495 bool need_fpstatus = false;
12496 bool need_rmode = false;
12497 int rmode = -1;
12498 TCGv_i32 tcg_rmode;
12499 TCGv_ptr tcg_fpstatus;
12501 switch (opcode) {
12502 case 0x0: /* REV64, REV32 */
12503 case 0x1: /* REV16 */
12504 handle_rev(s, opcode, u, is_q, size, rn, rd);
12505 return;
12506 case 0x5: /* CNT, NOT, RBIT */
12507 if (u && size == 0) {
12508 /* NOT */
12509 break;
12510 } else if (u && size == 1) {
12511 /* RBIT */
12512 break;
12513 } else if (!u && size == 0) {
12514 /* CNT */
12515 break;
12517 unallocated_encoding(s);
12518 return;
12519 case 0x12: /* XTN, XTN2, SQXTUN, SQXTUN2 */
12520 case 0x14: /* SQXTN, SQXTN2, UQXTN, UQXTN2 */
12521 if (size == 3) {
12522 unallocated_encoding(s);
12523 return;
12525 if (!fp_access_check(s)) {
12526 return;
12529 handle_2misc_narrow(s, false, opcode, u, is_q, size, rn, rd);
12530 return;
12531 case 0x4: /* CLS, CLZ */
12532 if (size == 3) {
12533 unallocated_encoding(s);
12534 return;
12536 break;
12537 case 0x2: /* SADDLP, UADDLP */
12538 case 0x6: /* SADALP, UADALP */
12539 if (size == 3) {
12540 unallocated_encoding(s);
12541 return;
12543 if (!fp_access_check(s)) {
12544 return;
12546 handle_2misc_pairwise(s, opcode, u, is_q, size, rn, rd);
12547 return;
12548 case 0x13: /* SHLL, SHLL2 */
12549 if (u == 0 || size == 3) {
12550 unallocated_encoding(s);
12551 return;
12553 if (!fp_access_check(s)) {
12554 return;
12556 handle_shll(s, is_q, size, rn, rd);
12557 return;
12558 case 0xa: /* CMLT */
12559 if (u == 1) {
12560 unallocated_encoding(s);
12561 return;
12563 /* fall through */
12564 case 0x8: /* CMGT, CMGE */
12565 case 0x9: /* CMEQ, CMLE */
12566 case 0xb: /* ABS, NEG */
12567 if (size == 3 && !is_q) {
12568 unallocated_encoding(s);
12569 return;
12571 break;
12572 case 0x3: /* SUQADD, USQADD */
12573 if (size == 3 && !is_q) {
12574 unallocated_encoding(s);
12575 return;
12577 if (!fp_access_check(s)) {
12578 return;
12580 handle_2misc_satacc(s, false, u, is_q, size, rn, rd);
12581 return;
12582 case 0x7: /* SQABS, SQNEG */
12583 if (size == 3 && !is_q) {
12584 unallocated_encoding(s);
12585 return;
12587 break;
12588 case 0xc ... 0xf:
12589 case 0x16 ... 0x1f:
12591 /* Floating point: U, size[1] and opcode indicate operation;
12592 * size[0] indicates single or double precision.
12594 int is_double = extract32(size, 0, 1);
12595 opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
12596 size = is_double ? 3 : 2;
12597 switch (opcode) {
12598 case 0x2f: /* FABS */
12599 case 0x6f: /* FNEG */
12600 if (size == 3 && !is_q) {
12601 unallocated_encoding(s);
12602 return;
12604 break;
12605 case 0x1d: /* SCVTF */
12606 case 0x5d: /* UCVTF */
12608 bool is_signed = (opcode == 0x1d) ? true : false;
12609 int elements = is_double ? 2 : is_q ? 4 : 2;
12610 if (is_double && !is_q) {
12611 unallocated_encoding(s);
12612 return;
12614 if (!fp_access_check(s)) {
12615 return;
12617 handle_simd_intfp_conv(s, rd, rn, elements, is_signed, 0, size);
12618 return;
12620 case 0x2c: /* FCMGT (zero) */
12621 case 0x2d: /* FCMEQ (zero) */
12622 case 0x2e: /* FCMLT (zero) */
12623 case 0x6c: /* FCMGE (zero) */
12624 case 0x6d: /* FCMLE (zero) */
12625 if (size == 3 && !is_q) {
12626 unallocated_encoding(s);
12627 return;
12629 handle_2misc_fcmp_zero(s, opcode, false, u, is_q, size, rn, rd);
12630 return;
12631 case 0x7f: /* FSQRT */
12632 if (size == 3 && !is_q) {
12633 unallocated_encoding(s);
12634 return;
12636 break;
12637 case 0x1a: /* FCVTNS */
12638 case 0x1b: /* FCVTMS */
12639 case 0x3a: /* FCVTPS */
12640 case 0x3b: /* FCVTZS */
12641 case 0x5a: /* FCVTNU */
12642 case 0x5b: /* FCVTMU */
12643 case 0x7a: /* FCVTPU */
12644 case 0x7b: /* FCVTZU */
12645 need_fpstatus = true;
12646 need_rmode = true;
12647 rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
12648 if (size == 3 && !is_q) {
12649 unallocated_encoding(s);
12650 return;
12652 break;
12653 case 0x5c: /* FCVTAU */
12654 case 0x1c: /* FCVTAS */
12655 need_fpstatus = true;
12656 need_rmode = true;
12657 rmode = FPROUNDING_TIEAWAY;
12658 if (size == 3 && !is_q) {
12659 unallocated_encoding(s);
12660 return;
12662 break;
12663 case 0x3c: /* URECPE */
12664 if (size == 3) {
12665 unallocated_encoding(s);
12666 return;
12668 /* fall through */
12669 case 0x3d: /* FRECPE */
12670 case 0x7d: /* FRSQRTE */
12671 if (size == 3 && !is_q) {
12672 unallocated_encoding(s);
12673 return;
12675 if (!fp_access_check(s)) {
12676 return;
12678 handle_2misc_reciprocal(s, opcode, false, u, is_q, size, rn, rd);
12679 return;
12680 case 0x56: /* FCVTXN, FCVTXN2 */
12681 if (size == 2) {
12682 unallocated_encoding(s);
12683 return;
12685 /* fall through */
12686 case 0x16: /* FCVTN, FCVTN2 */
12687 /* handle_2misc_narrow does a 2*size -> size operation, but these
12688 * instructions encode the source size rather than dest size.
12690 if (!fp_access_check(s)) {
12691 return;
12693 handle_2misc_narrow(s, false, opcode, 0, is_q, size - 1, rn, rd);
12694 return;
12695 case 0x17: /* FCVTL, FCVTL2 */
12696 if (!fp_access_check(s)) {
12697 return;
12699 handle_2misc_widening(s, opcode, is_q, size, rn, rd);
12700 return;
12701 case 0x18: /* FRINTN */
12702 case 0x19: /* FRINTM */
12703 case 0x38: /* FRINTP */
12704 case 0x39: /* FRINTZ */
12705 need_rmode = true;
12706 rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
12707 /* fall through */
12708 case 0x59: /* FRINTX */
12709 case 0x79: /* FRINTI */
12710 need_fpstatus = true;
12711 if (size == 3 && !is_q) {
12712 unallocated_encoding(s);
12713 return;
12715 break;
12716 case 0x58: /* FRINTA */
12717 need_rmode = true;
12718 rmode = FPROUNDING_TIEAWAY;
12719 need_fpstatus = true;
12720 if (size == 3 && !is_q) {
12721 unallocated_encoding(s);
12722 return;
12724 break;
12725 case 0x7c: /* URSQRTE */
12726 if (size == 3) {
12727 unallocated_encoding(s);
12728 return;
12730 break;
12731 case 0x1e: /* FRINT32Z */
12732 case 0x1f: /* FRINT64Z */
12733 need_rmode = true;
12734 rmode = FPROUNDING_ZERO;
12735 /* fall through */
12736 case 0x5e: /* FRINT32X */
12737 case 0x5f: /* FRINT64X */
12738 need_fpstatus = true;
12739 if ((size == 3 && !is_q) || !dc_isar_feature(aa64_frint, s)) {
12740 unallocated_encoding(s);
12741 return;
12743 break;
12744 default:
12745 unallocated_encoding(s);
12746 return;
12748 break;
12750 default:
12751 unallocated_encoding(s);
12752 return;
12755 if (!fp_access_check(s)) {
12756 return;
12759 if (need_fpstatus || need_rmode) {
12760 tcg_fpstatus = get_fpstatus_ptr(false);
12761 } else {
12762 tcg_fpstatus = NULL;
12764 if (need_rmode) {
12765 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rmode));
12766 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
12767 } else {
12768 tcg_rmode = NULL;
12771 switch (opcode) {
12772 case 0x5:
12773 if (u && size == 0) { /* NOT */
12774 gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_not, 0);
12775 return;
12777 break;
12778 case 0x8: /* CMGT, CMGE */
12779 if (u) {
12780 gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_cge0, size);
12781 } else {
12782 gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_cgt0, size);
12784 return;
12785 case 0x9: /* CMEQ, CMLE */
12786 if (u) {
12787 gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_cle0, size);
12788 } else {
12789 gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_ceq0, size);
12791 return;
12792 case 0xa: /* CMLT */
12793 gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_clt0, size);
12794 return;
12795 case 0xb:
12796 if (u) { /* ABS, NEG */
12797 gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_neg, size);
12798 } else {
12799 gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_abs, size);
12801 return;
12804 if (size == 3) {
12805 /* All 64-bit element operations can be shared with scalar 2misc */
12806 int pass;
12808 /* Coverity claims (size == 3 && !is_q) has been eliminated
12809 * from all paths leading to here.
12811 tcg_debug_assert(is_q);
12812 for (pass = 0; pass < 2; pass++) {
12813 TCGv_i64 tcg_op = tcg_temp_new_i64();
12814 TCGv_i64 tcg_res = tcg_temp_new_i64();
12816 read_vec_element(s, tcg_op, rn, pass, MO_64);
12818 handle_2misc_64(s, opcode, u, tcg_res, tcg_op,
12819 tcg_rmode, tcg_fpstatus);
12821 write_vec_element(s, tcg_res, rd, pass, MO_64);
12823 tcg_temp_free_i64(tcg_res);
12824 tcg_temp_free_i64(tcg_op);
12826 } else {
12827 int pass;
12829 for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
12830 TCGv_i32 tcg_op = tcg_temp_new_i32();
12831 TCGv_i32 tcg_res = tcg_temp_new_i32();
12833 read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
12835 if (size == 2) {
12836 /* Special cases for 32 bit elements */
12837 switch (opcode) {
12838 case 0x4: /* CLS */
12839 if (u) {
12840 tcg_gen_clzi_i32(tcg_res, tcg_op, 32);
12841 } else {
12842 tcg_gen_clrsb_i32(tcg_res, tcg_op);
12844 break;
12845 case 0x7: /* SQABS, SQNEG */
12846 if (u) {
12847 gen_helper_neon_qneg_s32(tcg_res, cpu_env, tcg_op);
12848 } else {
12849 gen_helper_neon_qabs_s32(tcg_res, cpu_env, tcg_op);
12851 break;
12852 case 0x2f: /* FABS */
12853 gen_helper_vfp_abss(tcg_res, tcg_op);
12854 break;
12855 case 0x6f: /* FNEG */
12856 gen_helper_vfp_negs(tcg_res, tcg_op);
12857 break;
12858 case 0x7f: /* FSQRT */
12859 gen_helper_vfp_sqrts(tcg_res, tcg_op, cpu_env);
12860 break;
12861 case 0x1a: /* FCVTNS */
12862 case 0x1b: /* FCVTMS */
12863 case 0x1c: /* FCVTAS */
12864 case 0x3a: /* FCVTPS */
12865 case 0x3b: /* FCVTZS */
12867 TCGv_i32 tcg_shift = tcg_const_i32(0);
12868 gen_helper_vfp_tosls(tcg_res, tcg_op,
12869 tcg_shift, tcg_fpstatus);
12870 tcg_temp_free_i32(tcg_shift);
12871 break;
12873 case 0x5a: /* FCVTNU */
12874 case 0x5b: /* FCVTMU */
12875 case 0x5c: /* FCVTAU */
12876 case 0x7a: /* FCVTPU */
12877 case 0x7b: /* FCVTZU */
12879 TCGv_i32 tcg_shift = tcg_const_i32(0);
12880 gen_helper_vfp_touls(tcg_res, tcg_op,
12881 tcg_shift, tcg_fpstatus);
12882 tcg_temp_free_i32(tcg_shift);
12883 break;
12885 case 0x18: /* FRINTN */
12886 case 0x19: /* FRINTM */
12887 case 0x38: /* FRINTP */
12888 case 0x39: /* FRINTZ */
12889 case 0x58: /* FRINTA */
12890 case 0x79: /* FRINTI */
12891 gen_helper_rints(tcg_res, tcg_op, tcg_fpstatus);
12892 break;
12893 case 0x59: /* FRINTX */
12894 gen_helper_rints_exact(tcg_res, tcg_op, tcg_fpstatus);
12895 break;
12896 case 0x7c: /* URSQRTE */
12897 gen_helper_rsqrte_u32(tcg_res, tcg_op);
12898 break;
12899 case 0x1e: /* FRINT32Z */
12900 case 0x5e: /* FRINT32X */
12901 gen_helper_frint32_s(tcg_res, tcg_op, tcg_fpstatus);
12902 break;
12903 case 0x1f: /* FRINT64Z */
12904 case 0x5f: /* FRINT64X */
12905 gen_helper_frint64_s(tcg_res, tcg_op, tcg_fpstatus);
12906 break;
12907 default:
12908 g_assert_not_reached();
12910 } else {
12911 /* Use helpers for 8 and 16 bit elements */
12912 switch (opcode) {
12913 case 0x5: /* CNT, RBIT */
12914 /* For these two insns size is part of the opcode specifier
12915 * (handled earlier); they always operate on byte elements.
12917 if (u) {
12918 gen_helper_neon_rbit_u8(tcg_res, tcg_op);
12919 } else {
12920 gen_helper_neon_cnt_u8(tcg_res, tcg_op);
12922 break;
12923 case 0x7: /* SQABS, SQNEG */
12925 NeonGenOneOpEnvFn *genfn;
12926 static NeonGenOneOpEnvFn * const fns[2][2] = {
12927 { gen_helper_neon_qabs_s8, gen_helper_neon_qneg_s8 },
12928 { gen_helper_neon_qabs_s16, gen_helper_neon_qneg_s16 },
12930 genfn = fns[size][u];
12931 genfn(tcg_res, cpu_env, tcg_op);
12932 break;
12934 case 0x4: /* CLS, CLZ */
12935 if (u) {
12936 if (size == 0) {
12937 gen_helper_neon_clz_u8(tcg_res, tcg_op);
12938 } else {
12939 gen_helper_neon_clz_u16(tcg_res, tcg_op);
12941 } else {
12942 if (size == 0) {
12943 gen_helper_neon_cls_s8(tcg_res, tcg_op);
12944 } else {
12945 gen_helper_neon_cls_s16(tcg_res, tcg_op);
12948 break;
12949 default:
12950 g_assert_not_reached();
12954 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
12956 tcg_temp_free_i32(tcg_res);
12957 tcg_temp_free_i32(tcg_op);
12960 clear_vec_high(s, is_q, rd);
12962 if (need_rmode) {
12963 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
12964 tcg_temp_free_i32(tcg_rmode);
12966 if (need_fpstatus) {
12967 tcg_temp_free_ptr(tcg_fpstatus);
12971 /* AdvSIMD [scalar] two register miscellaneous (FP16)
12973 * 31 30 29 28 27 24 23 22 21 17 16 12 11 10 9 5 4 0
12974 * +---+---+---+---+---------+---+-------------+--------+-----+------+------+
12975 * | 0 | Q | U | S | 1 1 1 0 | a | 1 1 1 1 0 0 | opcode | 1 0 | Rn | Rd |
12976 * +---+---+---+---+---------+---+-------------+--------+-----+------+------+
12977 * mask: 1000 1111 0111 1110 0000 1100 0000 0000 0x8f7e 0c00
12978 * val: 0000 1110 0111 1000 0000 1000 0000 0000 0x0e78 0800
12980 * This actually covers two groups where scalar access is governed by
12981 * bit 28. A bunch of the instructions (float to integral) only exist
12982 * in the vector form and are un-allocated for the scalar decode. Also
12983 * in the scalar decode Q is always 1.
12985 static void disas_simd_two_reg_misc_fp16(DisasContext *s, uint32_t insn)
12987 int fpop, opcode, a, u;
12988 int rn, rd;
12989 bool is_q;
12990 bool is_scalar;
12991 bool only_in_vector = false;
12993 int pass;
12994 TCGv_i32 tcg_rmode = NULL;
12995 TCGv_ptr tcg_fpstatus = NULL;
12996 bool need_rmode = false;
12997 bool need_fpst = true;
12998 int rmode;
13000 if (!dc_isar_feature(aa64_fp16, s)) {
13001 unallocated_encoding(s);
13002 return;
13005 rd = extract32(insn, 0, 5);
13006 rn = extract32(insn, 5, 5);
13008 a = extract32(insn, 23, 1);
13009 u = extract32(insn, 29, 1);
13010 is_scalar = extract32(insn, 28, 1);
13011 is_q = extract32(insn, 30, 1);
13013 opcode = extract32(insn, 12, 5);
13014 fpop = deposit32(opcode, 5, 1, a);
13015 fpop = deposit32(fpop, 6, 1, u);
13017 rd = extract32(insn, 0, 5);
13018 rn = extract32(insn, 5, 5);
13020 switch (fpop) {
13021 case 0x1d: /* SCVTF */
13022 case 0x5d: /* UCVTF */
13024 int elements;
13026 if (is_scalar) {
13027 elements = 1;
13028 } else {
13029 elements = (is_q ? 8 : 4);
13032 if (!fp_access_check(s)) {
13033 return;
13035 handle_simd_intfp_conv(s, rd, rn, elements, !u, 0, MO_16);
13036 return;
13038 break;
13039 case 0x2c: /* FCMGT (zero) */
13040 case 0x2d: /* FCMEQ (zero) */
13041 case 0x2e: /* FCMLT (zero) */
13042 case 0x6c: /* FCMGE (zero) */
13043 case 0x6d: /* FCMLE (zero) */
13044 handle_2misc_fcmp_zero(s, fpop, is_scalar, 0, is_q, MO_16, rn, rd);
13045 return;
13046 case 0x3d: /* FRECPE */
13047 case 0x3f: /* FRECPX */
13048 break;
13049 case 0x18: /* FRINTN */
13050 need_rmode = true;
13051 only_in_vector = true;
13052 rmode = FPROUNDING_TIEEVEN;
13053 break;
13054 case 0x19: /* FRINTM */
13055 need_rmode = true;
13056 only_in_vector = true;
13057 rmode = FPROUNDING_NEGINF;
13058 break;
13059 case 0x38: /* FRINTP */
13060 need_rmode = true;
13061 only_in_vector = true;
13062 rmode = FPROUNDING_POSINF;
13063 break;
13064 case 0x39: /* FRINTZ */
13065 need_rmode = true;
13066 only_in_vector = true;
13067 rmode = FPROUNDING_ZERO;
13068 break;
13069 case 0x58: /* FRINTA */
13070 need_rmode = true;
13071 only_in_vector = true;
13072 rmode = FPROUNDING_TIEAWAY;
13073 break;
13074 case 0x59: /* FRINTX */
13075 case 0x79: /* FRINTI */
13076 only_in_vector = true;
13077 /* current rounding mode */
13078 break;
13079 case 0x1a: /* FCVTNS */
13080 need_rmode = true;
13081 rmode = FPROUNDING_TIEEVEN;
13082 break;
13083 case 0x1b: /* FCVTMS */
13084 need_rmode = true;
13085 rmode = FPROUNDING_NEGINF;
13086 break;
13087 case 0x1c: /* FCVTAS */
13088 need_rmode = true;
13089 rmode = FPROUNDING_TIEAWAY;
13090 break;
13091 case 0x3a: /* FCVTPS */
13092 need_rmode = true;
13093 rmode = FPROUNDING_POSINF;
13094 break;
13095 case 0x3b: /* FCVTZS */
13096 need_rmode = true;
13097 rmode = FPROUNDING_ZERO;
13098 break;
13099 case 0x5a: /* FCVTNU */
13100 need_rmode = true;
13101 rmode = FPROUNDING_TIEEVEN;
13102 break;
13103 case 0x5b: /* FCVTMU */
13104 need_rmode = true;
13105 rmode = FPROUNDING_NEGINF;
13106 break;
13107 case 0x5c: /* FCVTAU */
13108 need_rmode = true;
13109 rmode = FPROUNDING_TIEAWAY;
13110 break;
13111 case 0x7a: /* FCVTPU */
13112 need_rmode = true;
13113 rmode = FPROUNDING_POSINF;
13114 break;
13115 case 0x7b: /* FCVTZU */
13116 need_rmode = true;
13117 rmode = FPROUNDING_ZERO;
13118 break;
13119 case 0x2f: /* FABS */
13120 case 0x6f: /* FNEG */
13121 need_fpst = false;
13122 break;
13123 case 0x7d: /* FRSQRTE */
13124 case 0x7f: /* FSQRT (vector) */
13125 break;
13126 default:
13127 fprintf(stderr, "%s: insn %#04x fpop %#2x\n", __func__, insn, fpop);
13128 g_assert_not_reached();
13132 /* Check additional constraints for the scalar encoding */
13133 if (is_scalar) {
13134 if (!is_q) {
13135 unallocated_encoding(s);
13136 return;
13138 /* FRINTxx is only in the vector form */
13139 if (only_in_vector) {
13140 unallocated_encoding(s);
13141 return;
13145 if (!fp_access_check(s)) {
13146 return;
13149 if (need_rmode || need_fpst) {
13150 tcg_fpstatus = get_fpstatus_ptr(true);
13153 if (need_rmode) {
13154 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rmode));
13155 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
13158 if (is_scalar) {
13159 TCGv_i32 tcg_op = read_fp_hreg(s, rn);
13160 TCGv_i32 tcg_res = tcg_temp_new_i32();
13162 switch (fpop) {
13163 case 0x1a: /* FCVTNS */
13164 case 0x1b: /* FCVTMS */
13165 case 0x1c: /* FCVTAS */
13166 case 0x3a: /* FCVTPS */
13167 case 0x3b: /* FCVTZS */
13168 gen_helper_advsimd_f16tosinth(tcg_res, tcg_op, tcg_fpstatus);
13169 break;
13170 case 0x3d: /* FRECPE */
13171 gen_helper_recpe_f16(tcg_res, tcg_op, tcg_fpstatus);
13172 break;
13173 case 0x3f: /* FRECPX */
13174 gen_helper_frecpx_f16(tcg_res, tcg_op, tcg_fpstatus);
13175 break;
13176 case 0x5a: /* FCVTNU */
13177 case 0x5b: /* FCVTMU */
13178 case 0x5c: /* FCVTAU */
13179 case 0x7a: /* FCVTPU */
13180 case 0x7b: /* FCVTZU */
13181 gen_helper_advsimd_f16touinth(tcg_res, tcg_op, tcg_fpstatus);
13182 break;
13183 case 0x6f: /* FNEG */
13184 tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
13185 break;
13186 case 0x7d: /* FRSQRTE */
13187 gen_helper_rsqrte_f16(tcg_res, tcg_op, tcg_fpstatus);
13188 break;
13189 default:
13190 g_assert_not_reached();
13193 /* limit any sign extension going on */
13194 tcg_gen_andi_i32(tcg_res, tcg_res, 0xffff);
13195 write_fp_sreg(s, rd, tcg_res);
13197 tcg_temp_free_i32(tcg_res);
13198 tcg_temp_free_i32(tcg_op);
13199 } else {
13200 for (pass = 0; pass < (is_q ? 8 : 4); pass++) {
13201 TCGv_i32 tcg_op = tcg_temp_new_i32();
13202 TCGv_i32 tcg_res = tcg_temp_new_i32();
13204 read_vec_element_i32(s, tcg_op, rn, pass, MO_16);
13206 switch (fpop) {
13207 case 0x1a: /* FCVTNS */
13208 case 0x1b: /* FCVTMS */
13209 case 0x1c: /* FCVTAS */
13210 case 0x3a: /* FCVTPS */
13211 case 0x3b: /* FCVTZS */
13212 gen_helper_advsimd_f16tosinth(tcg_res, tcg_op, tcg_fpstatus);
13213 break;
13214 case 0x3d: /* FRECPE */
13215 gen_helper_recpe_f16(tcg_res, tcg_op, tcg_fpstatus);
13216 break;
13217 case 0x5a: /* FCVTNU */
13218 case 0x5b: /* FCVTMU */
13219 case 0x5c: /* FCVTAU */
13220 case 0x7a: /* FCVTPU */
13221 case 0x7b: /* FCVTZU */
13222 gen_helper_advsimd_f16touinth(tcg_res, tcg_op, tcg_fpstatus);
13223 break;
13224 case 0x18: /* FRINTN */
13225 case 0x19: /* FRINTM */
13226 case 0x38: /* FRINTP */
13227 case 0x39: /* FRINTZ */
13228 case 0x58: /* FRINTA */
13229 case 0x79: /* FRINTI */
13230 gen_helper_advsimd_rinth(tcg_res, tcg_op, tcg_fpstatus);
13231 break;
13232 case 0x59: /* FRINTX */
13233 gen_helper_advsimd_rinth_exact(tcg_res, tcg_op, tcg_fpstatus);
13234 break;
13235 case 0x2f: /* FABS */
13236 tcg_gen_andi_i32(tcg_res, tcg_op, 0x7fff);
13237 break;
13238 case 0x6f: /* FNEG */
13239 tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
13240 break;
13241 case 0x7d: /* FRSQRTE */
13242 gen_helper_rsqrte_f16(tcg_res, tcg_op, tcg_fpstatus);
13243 break;
13244 case 0x7f: /* FSQRT */
13245 gen_helper_sqrt_f16(tcg_res, tcg_op, tcg_fpstatus);
13246 break;
13247 default:
13248 g_assert_not_reached();
13251 write_vec_element_i32(s, tcg_res, rd, pass, MO_16);
13253 tcg_temp_free_i32(tcg_res);
13254 tcg_temp_free_i32(tcg_op);
13257 clear_vec_high(s, is_q, rd);
13260 if (tcg_rmode) {
13261 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
13262 tcg_temp_free_i32(tcg_rmode);
13265 if (tcg_fpstatus) {
13266 tcg_temp_free_ptr(tcg_fpstatus);
13270 /* AdvSIMD scalar x indexed element
13271 * 31 30 29 28 24 23 22 21 20 19 16 15 12 11 10 9 5 4 0
13272 * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
13273 * | 0 1 | U | 1 1 1 1 1 | size | L | M | Rm | opc | H | 0 | Rn | Rd |
13274 * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
13275 * AdvSIMD vector x indexed element
13276 * 31 30 29 28 24 23 22 21 20 19 16 15 12 11 10 9 5 4 0
13277 * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
13278 * | 0 | Q | U | 0 1 1 1 1 | size | L | M | Rm | opc | H | 0 | Rn | Rd |
13279 * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
13281 static void disas_simd_indexed(DisasContext *s, uint32_t insn)
13283 /* This encoding has two kinds of instruction:
13284 * normal, where we perform elt x idxelt => elt for each
13285 * element in the vector
13286 * long, where we perform elt x idxelt and generate a result of
13287 * double the width of the input element
13288 * The long ops have a 'part' specifier (ie come in INSN, INSN2 pairs).
13290 bool is_scalar = extract32(insn, 28, 1);
13291 bool is_q = extract32(insn, 30, 1);
13292 bool u = extract32(insn, 29, 1);
13293 int size = extract32(insn, 22, 2);
13294 int l = extract32(insn, 21, 1);
13295 int m = extract32(insn, 20, 1);
13296 /* Note that the Rm field here is only 4 bits, not 5 as it usually is */
13297 int rm = extract32(insn, 16, 4);
13298 int opcode = extract32(insn, 12, 4);
13299 int h = extract32(insn, 11, 1);
13300 int rn = extract32(insn, 5, 5);
13301 int rd = extract32(insn, 0, 5);
13302 bool is_long = false;
13303 int is_fp = 0;
13304 bool is_fp16 = false;
13305 int index;
13306 TCGv_ptr fpst;
13308 switch (16 * u + opcode) {
13309 case 0x08: /* MUL */
13310 case 0x10: /* MLA */
13311 case 0x14: /* MLS */
13312 if (is_scalar) {
13313 unallocated_encoding(s);
13314 return;
13316 break;
13317 case 0x02: /* SMLAL, SMLAL2 */
13318 case 0x12: /* UMLAL, UMLAL2 */
13319 case 0x06: /* SMLSL, SMLSL2 */
13320 case 0x16: /* UMLSL, UMLSL2 */
13321 case 0x0a: /* SMULL, SMULL2 */
13322 case 0x1a: /* UMULL, UMULL2 */
13323 if (is_scalar) {
13324 unallocated_encoding(s);
13325 return;
13327 is_long = true;
13328 break;
13329 case 0x03: /* SQDMLAL, SQDMLAL2 */
13330 case 0x07: /* SQDMLSL, SQDMLSL2 */
13331 case 0x0b: /* SQDMULL, SQDMULL2 */
13332 is_long = true;
13333 break;
13334 case 0x0c: /* SQDMULH */
13335 case 0x0d: /* SQRDMULH */
13336 break;
13337 case 0x01: /* FMLA */
13338 case 0x05: /* FMLS */
13339 case 0x09: /* FMUL */
13340 case 0x19: /* FMULX */
13341 is_fp = 1;
13342 break;
13343 case 0x1d: /* SQRDMLAH */
13344 case 0x1f: /* SQRDMLSH */
13345 if (!dc_isar_feature(aa64_rdm, s)) {
13346 unallocated_encoding(s);
13347 return;
13349 break;
13350 case 0x0e: /* SDOT */
13351 case 0x1e: /* UDOT */
13352 if (is_scalar || size != MO_32 || !dc_isar_feature(aa64_dp, s)) {
13353 unallocated_encoding(s);
13354 return;
13356 break;
13357 case 0x11: /* FCMLA #0 */
13358 case 0x13: /* FCMLA #90 */
13359 case 0x15: /* FCMLA #180 */
13360 case 0x17: /* FCMLA #270 */
13361 if (is_scalar || !dc_isar_feature(aa64_fcma, s)) {
13362 unallocated_encoding(s);
13363 return;
13365 is_fp = 2;
13366 break;
13367 case 0x00: /* FMLAL */
13368 case 0x04: /* FMLSL */
13369 case 0x18: /* FMLAL2 */
13370 case 0x1c: /* FMLSL2 */
13371 if (is_scalar || size != MO_32 || !dc_isar_feature(aa64_fhm, s)) {
13372 unallocated_encoding(s);
13373 return;
13375 size = MO_16;
13376 /* is_fp, but we pass cpu_env not fp_status. */
13377 break;
13378 default:
13379 unallocated_encoding(s);
13380 return;
13383 switch (is_fp) {
13384 case 1: /* normal fp */
13385 /* convert insn encoded size to MemOp size */
13386 switch (size) {
13387 case 0: /* half-precision */
13388 size = MO_16;
13389 is_fp16 = true;
13390 break;
13391 case MO_32: /* single precision */
13392 case MO_64: /* double precision */
13393 break;
13394 default:
13395 unallocated_encoding(s);
13396 return;
13398 break;
13400 case 2: /* complex fp */
13401 /* Each indexable element is a complex pair. */
13402 size += 1;
13403 switch (size) {
13404 case MO_32:
13405 if (h && !is_q) {
13406 unallocated_encoding(s);
13407 return;
13409 is_fp16 = true;
13410 break;
13411 case MO_64:
13412 break;
13413 default:
13414 unallocated_encoding(s);
13415 return;
13417 break;
13419 default: /* integer */
13420 switch (size) {
13421 case MO_8:
13422 case MO_64:
13423 unallocated_encoding(s);
13424 return;
13426 break;
13428 if (is_fp16 && !dc_isar_feature(aa64_fp16, s)) {
13429 unallocated_encoding(s);
13430 return;
13433 /* Given MemOp size, adjust register and indexing. */
13434 switch (size) {
13435 case MO_16:
13436 index = h << 2 | l << 1 | m;
13437 break;
13438 case MO_32:
13439 index = h << 1 | l;
13440 rm |= m << 4;
13441 break;
13442 case MO_64:
13443 if (l || !is_q) {
13444 unallocated_encoding(s);
13445 return;
13447 index = h;
13448 rm |= m << 4;
13449 break;
13450 default:
13451 g_assert_not_reached();
13454 if (!fp_access_check(s)) {
13455 return;
13458 if (is_fp) {
13459 fpst = get_fpstatus_ptr(is_fp16);
13460 } else {
13461 fpst = NULL;
13464 switch (16 * u + opcode) {
13465 case 0x0e: /* SDOT */
13466 case 0x1e: /* UDOT */
13467 gen_gvec_op3_ool(s, is_q, rd, rn, rm, index,
13468 u ? gen_helper_gvec_udot_idx_b
13469 : gen_helper_gvec_sdot_idx_b);
13470 return;
13471 case 0x11: /* FCMLA #0 */
13472 case 0x13: /* FCMLA #90 */
13473 case 0x15: /* FCMLA #180 */
13474 case 0x17: /* FCMLA #270 */
13476 int rot = extract32(insn, 13, 2);
13477 int data = (index << 2) | rot;
13478 tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
13479 vec_full_reg_offset(s, rn),
13480 vec_full_reg_offset(s, rm), fpst,
13481 is_q ? 16 : 8, vec_full_reg_size(s), data,
13482 size == MO_64
13483 ? gen_helper_gvec_fcmlas_idx
13484 : gen_helper_gvec_fcmlah_idx);
13485 tcg_temp_free_ptr(fpst);
13487 return;
13489 case 0x00: /* FMLAL */
13490 case 0x04: /* FMLSL */
13491 case 0x18: /* FMLAL2 */
13492 case 0x1c: /* FMLSL2 */
13494 int is_s = extract32(opcode, 2, 1);
13495 int is_2 = u;
13496 int data = (index << 2) | (is_2 << 1) | is_s;
13497 tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
13498 vec_full_reg_offset(s, rn),
13499 vec_full_reg_offset(s, rm), cpu_env,
13500 is_q ? 16 : 8, vec_full_reg_size(s),
13501 data, gen_helper_gvec_fmlal_idx_a64);
13503 return;
13506 if (size == 3) {
13507 TCGv_i64 tcg_idx = tcg_temp_new_i64();
13508 int pass;
13510 assert(is_fp && is_q && !is_long);
13512 read_vec_element(s, tcg_idx, rm, index, MO_64);
13514 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
13515 TCGv_i64 tcg_op = tcg_temp_new_i64();
13516 TCGv_i64 tcg_res = tcg_temp_new_i64();
13518 read_vec_element(s, tcg_op, rn, pass, MO_64);
13520 switch (16 * u + opcode) {
13521 case 0x05: /* FMLS */
13522 /* As usual for ARM, separate negation for fused multiply-add */
13523 gen_helper_vfp_negd(tcg_op, tcg_op);
13524 /* fall through */
13525 case 0x01: /* FMLA */
13526 read_vec_element(s, tcg_res, rd, pass, MO_64);
13527 gen_helper_vfp_muladdd(tcg_res, tcg_op, tcg_idx, tcg_res, fpst);
13528 break;
13529 case 0x09: /* FMUL */
13530 gen_helper_vfp_muld(tcg_res, tcg_op, tcg_idx, fpst);
13531 break;
13532 case 0x19: /* FMULX */
13533 gen_helper_vfp_mulxd(tcg_res, tcg_op, tcg_idx, fpst);
13534 break;
13535 default:
13536 g_assert_not_reached();
13539 write_vec_element(s, tcg_res, rd, pass, MO_64);
13540 tcg_temp_free_i64(tcg_op);
13541 tcg_temp_free_i64(tcg_res);
13544 tcg_temp_free_i64(tcg_idx);
13545 clear_vec_high(s, !is_scalar, rd);
13546 } else if (!is_long) {
13547 /* 32 bit floating point, or 16 or 32 bit integer.
13548 * For the 16 bit scalar case we use the usual Neon helpers and
13549 * rely on the fact that 0 op 0 == 0 with no side effects.
13551 TCGv_i32 tcg_idx = tcg_temp_new_i32();
13552 int pass, maxpasses;
13554 if (is_scalar) {
13555 maxpasses = 1;
13556 } else {
13557 maxpasses = is_q ? 4 : 2;
13560 read_vec_element_i32(s, tcg_idx, rm, index, size);
13562 if (size == 1 && !is_scalar) {
13563 /* The simplest way to handle the 16x16 indexed ops is to duplicate
13564 * the index into both halves of the 32 bit tcg_idx and then use
13565 * the usual Neon helpers.
13567 tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
13570 for (pass = 0; pass < maxpasses; pass++) {
13571 TCGv_i32 tcg_op = tcg_temp_new_i32();
13572 TCGv_i32 tcg_res = tcg_temp_new_i32();
13574 read_vec_element_i32(s, tcg_op, rn, pass, is_scalar ? size : MO_32);
13576 switch (16 * u + opcode) {
13577 case 0x08: /* MUL */
13578 case 0x10: /* MLA */
13579 case 0x14: /* MLS */
13581 static NeonGenTwoOpFn * const fns[2][2] = {
13582 { gen_helper_neon_add_u16, gen_helper_neon_sub_u16 },
13583 { tcg_gen_add_i32, tcg_gen_sub_i32 },
13585 NeonGenTwoOpFn *genfn;
13586 bool is_sub = opcode == 0x4;
13588 if (size == 1) {
13589 gen_helper_neon_mul_u16(tcg_res, tcg_op, tcg_idx);
13590 } else {
13591 tcg_gen_mul_i32(tcg_res, tcg_op, tcg_idx);
13593 if (opcode == 0x8) {
13594 break;
13596 read_vec_element_i32(s, tcg_op, rd, pass, MO_32);
13597 genfn = fns[size - 1][is_sub];
13598 genfn(tcg_res, tcg_op, tcg_res);
13599 break;
13601 case 0x05: /* FMLS */
13602 case 0x01: /* FMLA */
13603 read_vec_element_i32(s, tcg_res, rd, pass,
13604 is_scalar ? size : MO_32);
13605 switch (size) {
13606 case 1:
13607 if (opcode == 0x5) {
13608 /* As usual for ARM, separate negation for fused
13609 * multiply-add */
13610 tcg_gen_xori_i32(tcg_op, tcg_op, 0x80008000);
13612 if (is_scalar) {
13613 gen_helper_advsimd_muladdh(tcg_res, tcg_op, tcg_idx,
13614 tcg_res, fpst);
13615 } else {
13616 gen_helper_advsimd_muladd2h(tcg_res, tcg_op, tcg_idx,
13617 tcg_res, fpst);
13619 break;
13620 case 2:
13621 if (opcode == 0x5) {
13622 /* As usual for ARM, separate negation for
13623 * fused multiply-add */
13624 tcg_gen_xori_i32(tcg_op, tcg_op, 0x80000000);
13626 gen_helper_vfp_muladds(tcg_res, tcg_op, tcg_idx,
13627 tcg_res, fpst);
13628 break;
13629 default:
13630 g_assert_not_reached();
13632 break;
13633 case 0x09: /* FMUL */
13634 switch (size) {
13635 case 1:
13636 if (is_scalar) {
13637 gen_helper_advsimd_mulh(tcg_res, tcg_op,
13638 tcg_idx, fpst);
13639 } else {
13640 gen_helper_advsimd_mul2h(tcg_res, tcg_op,
13641 tcg_idx, fpst);
13643 break;
13644 case 2:
13645 gen_helper_vfp_muls(tcg_res, tcg_op, tcg_idx, fpst);
13646 break;
13647 default:
13648 g_assert_not_reached();
13650 break;
13651 case 0x19: /* FMULX */
13652 switch (size) {
13653 case 1:
13654 if (is_scalar) {
13655 gen_helper_advsimd_mulxh(tcg_res, tcg_op,
13656 tcg_idx, fpst);
13657 } else {
13658 gen_helper_advsimd_mulx2h(tcg_res, tcg_op,
13659 tcg_idx, fpst);
13661 break;
13662 case 2:
13663 gen_helper_vfp_mulxs(tcg_res, tcg_op, tcg_idx, fpst);
13664 break;
13665 default:
13666 g_assert_not_reached();
13668 break;
13669 case 0x0c: /* SQDMULH */
13670 if (size == 1) {
13671 gen_helper_neon_qdmulh_s16(tcg_res, cpu_env,
13672 tcg_op, tcg_idx);
13673 } else {
13674 gen_helper_neon_qdmulh_s32(tcg_res, cpu_env,
13675 tcg_op, tcg_idx);
13677 break;
13678 case 0x0d: /* SQRDMULH */
13679 if (size == 1) {
13680 gen_helper_neon_qrdmulh_s16(tcg_res, cpu_env,
13681 tcg_op, tcg_idx);
13682 } else {
13683 gen_helper_neon_qrdmulh_s32(tcg_res, cpu_env,
13684 tcg_op, tcg_idx);
13686 break;
13687 case 0x1d: /* SQRDMLAH */
13688 read_vec_element_i32(s, tcg_res, rd, pass,
13689 is_scalar ? size : MO_32);
13690 if (size == 1) {
13691 gen_helper_neon_qrdmlah_s16(tcg_res, cpu_env,
13692 tcg_op, tcg_idx, tcg_res);
13693 } else {
13694 gen_helper_neon_qrdmlah_s32(tcg_res, cpu_env,
13695 tcg_op, tcg_idx, tcg_res);
13697 break;
13698 case 0x1f: /* SQRDMLSH */
13699 read_vec_element_i32(s, tcg_res, rd, pass,
13700 is_scalar ? size : MO_32);
13701 if (size == 1) {
13702 gen_helper_neon_qrdmlsh_s16(tcg_res, cpu_env,
13703 tcg_op, tcg_idx, tcg_res);
13704 } else {
13705 gen_helper_neon_qrdmlsh_s32(tcg_res, cpu_env,
13706 tcg_op, tcg_idx, tcg_res);
13708 break;
13709 default:
13710 g_assert_not_reached();
13713 if (is_scalar) {
13714 write_fp_sreg(s, rd, tcg_res);
13715 } else {
13716 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
13719 tcg_temp_free_i32(tcg_op);
13720 tcg_temp_free_i32(tcg_res);
13723 tcg_temp_free_i32(tcg_idx);
13724 clear_vec_high(s, is_q, rd);
13725 } else {
13726 /* long ops: 16x16->32 or 32x32->64 */
13727 TCGv_i64 tcg_res[2];
13728 int pass;
13729 bool satop = extract32(opcode, 0, 1);
13730 MemOp memop = MO_32;
13732 if (satop || !u) {
13733 memop |= MO_SIGN;
13736 if (size == 2) {
13737 TCGv_i64 tcg_idx = tcg_temp_new_i64();
13739 read_vec_element(s, tcg_idx, rm, index, memop);
13741 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
13742 TCGv_i64 tcg_op = tcg_temp_new_i64();
13743 TCGv_i64 tcg_passres;
13744 int passelt;
13746 if (is_scalar) {
13747 passelt = 0;
13748 } else {
13749 passelt = pass + (is_q * 2);
13752 read_vec_element(s, tcg_op, rn, passelt, memop);
13754 tcg_res[pass] = tcg_temp_new_i64();
13756 if (opcode == 0xa || opcode == 0xb) {
13757 /* Non-accumulating ops */
13758 tcg_passres = tcg_res[pass];
13759 } else {
13760 tcg_passres = tcg_temp_new_i64();
13763 tcg_gen_mul_i64(tcg_passres, tcg_op, tcg_idx);
13764 tcg_temp_free_i64(tcg_op);
13766 if (satop) {
13767 /* saturating, doubling */
13768 gen_helper_neon_addl_saturate_s64(tcg_passres, cpu_env,
13769 tcg_passres, tcg_passres);
13772 if (opcode == 0xa || opcode == 0xb) {
13773 continue;
13776 /* Accumulating op: handle accumulate step */
13777 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
13779 switch (opcode) {
13780 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
13781 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
13782 break;
13783 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
13784 tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
13785 break;
13786 case 0x7: /* SQDMLSL, SQDMLSL2 */
13787 tcg_gen_neg_i64(tcg_passres, tcg_passres);
13788 /* fall through */
13789 case 0x3: /* SQDMLAL, SQDMLAL2 */
13790 gen_helper_neon_addl_saturate_s64(tcg_res[pass], cpu_env,
13791 tcg_res[pass],
13792 tcg_passres);
13793 break;
13794 default:
13795 g_assert_not_reached();
13797 tcg_temp_free_i64(tcg_passres);
13799 tcg_temp_free_i64(tcg_idx);
13801 clear_vec_high(s, !is_scalar, rd);
13802 } else {
13803 TCGv_i32 tcg_idx = tcg_temp_new_i32();
13805 assert(size == 1);
13806 read_vec_element_i32(s, tcg_idx, rm, index, size);
13808 if (!is_scalar) {
13809 /* The simplest way to handle the 16x16 indexed ops is to
13810 * duplicate the index into both halves of the 32 bit tcg_idx
13811 * and then use the usual Neon helpers.
13813 tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
13816 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
13817 TCGv_i32 tcg_op = tcg_temp_new_i32();
13818 TCGv_i64 tcg_passres;
13820 if (is_scalar) {
13821 read_vec_element_i32(s, tcg_op, rn, pass, size);
13822 } else {
13823 read_vec_element_i32(s, tcg_op, rn,
13824 pass + (is_q * 2), MO_32);
13827 tcg_res[pass] = tcg_temp_new_i64();
13829 if (opcode == 0xa || opcode == 0xb) {
13830 /* Non-accumulating ops */
13831 tcg_passres = tcg_res[pass];
13832 } else {
13833 tcg_passres = tcg_temp_new_i64();
13836 if (memop & MO_SIGN) {
13837 gen_helper_neon_mull_s16(tcg_passres, tcg_op, tcg_idx);
13838 } else {
13839 gen_helper_neon_mull_u16(tcg_passres, tcg_op, tcg_idx);
13841 if (satop) {
13842 gen_helper_neon_addl_saturate_s32(tcg_passres, cpu_env,
13843 tcg_passres, tcg_passres);
13845 tcg_temp_free_i32(tcg_op);
13847 if (opcode == 0xa || opcode == 0xb) {
13848 continue;
13851 /* Accumulating op: handle accumulate step */
13852 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
13854 switch (opcode) {
13855 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
13856 gen_helper_neon_addl_u32(tcg_res[pass], tcg_res[pass],
13857 tcg_passres);
13858 break;
13859 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
13860 gen_helper_neon_subl_u32(tcg_res[pass], tcg_res[pass],
13861 tcg_passres);
13862 break;
13863 case 0x7: /* SQDMLSL, SQDMLSL2 */
13864 gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
13865 /* fall through */
13866 case 0x3: /* SQDMLAL, SQDMLAL2 */
13867 gen_helper_neon_addl_saturate_s32(tcg_res[pass], cpu_env,
13868 tcg_res[pass],
13869 tcg_passres);
13870 break;
13871 default:
13872 g_assert_not_reached();
13874 tcg_temp_free_i64(tcg_passres);
13876 tcg_temp_free_i32(tcg_idx);
13878 if (is_scalar) {
13879 tcg_gen_ext32u_i64(tcg_res[0], tcg_res[0]);
13883 if (is_scalar) {
13884 tcg_res[1] = tcg_const_i64(0);
13887 for (pass = 0; pass < 2; pass++) {
13888 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
13889 tcg_temp_free_i64(tcg_res[pass]);
13893 if (fpst) {
13894 tcg_temp_free_ptr(fpst);
13898 /* Crypto AES
13899 * 31 24 23 22 21 17 16 12 11 10 9 5 4 0
13900 * +-----------------+------+-----------+--------+-----+------+------+
13901 * | 0 1 0 0 1 1 1 0 | size | 1 0 1 0 0 | opcode | 1 0 | Rn | Rd |
13902 * +-----------------+------+-----------+--------+-----+------+------+
13904 static void disas_crypto_aes(DisasContext *s, uint32_t insn)
13906 int size = extract32(insn, 22, 2);
13907 int opcode = extract32(insn, 12, 5);
13908 int rn = extract32(insn, 5, 5);
13909 int rd = extract32(insn, 0, 5);
13910 int decrypt;
13911 gen_helper_gvec_2 *genfn2 = NULL;
13912 gen_helper_gvec_3 *genfn3 = NULL;
13914 if (!dc_isar_feature(aa64_aes, s) || size != 0) {
13915 unallocated_encoding(s);
13916 return;
13919 switch (opcode) {
13920 case 0x4: /* AESE */
13921 decrypt = 0;
13922 genfn3 = gen_helper_crypto_aese;
13923 break;
13924 case 0x6: /* AESMC */
13925 decrypt = 0;
13926 genfn2 = gen_helper_crypto_aesmc;
13927 break;
13928 case 0x5: /* AESD */
13929 decrypt = 1;
13930 genfn3 = gen_helper_crypto_aese;
13931 break;
13932 case 0x7: /* AESIMC */
13933 decrypt = 1;
13934 genfn2 = gen_helper_crypto_aesmc;
13935 break;
13936 default:
13937 unallocated_encoding(s);
13938 return;
13941 if (!fp_access_check(s)) {
13942 return;
13944 if (genfn2) {
13945 gen_gvec_op2_ool(s, true, rd, rn, decrypt, genfn2);
13946 } else {
13947 gen_gvec_op3_ool(s, true, rd, rd, rn, decrypt, genfn3);
13951 /* Crypto three-reg SHA
13952 * 31 24 23 22 21 20 16 15 14 12 11 10 9 5 4 0
13953 * +-----------------+------+---+------+---+--------+-----+------+------+
13954 * | 0 1 0 1 1 1 1 0 | size | 0 | Rm | 0 | opcode | 0 0 | Rn | Rd |
13955 * +-----------------+------+---+------+---+--------+-----+------+------+
13957 static void disas_crypto_three_reg_sha(DisasContext *s, uint32_t insn)
13959 int size = extract32(insn, 22, 2);
13960 int opcode = extract32(insn, 12, 3);
13961 int rm = extract32(insn, 16, 5);
13962 int rn = extract32(insn, 5, 5);
13963 int rd = extract32(insn, 0, 5);
13964 gen_helper_gvec_3 *genfn;
13965 bool feature;
13967 if (size != 0) {
13968 unallocated_encoding(s);
13969 return;
13972 switch (opcode) {
13973 case 0: /* SHA1C */
13974 genfn = gen_helper_crypto_sha1c;
13975 feature = dc_isar_feature(aa64_sha1, s);
13976 break;
13977 case 1: /* SHA1P */
13978 genfn = gen_helper_crypto_sha1p;
13979 feature = dc_isar_feature(aa64_sha1, s);
13980 break;
13981 case 2: /* SHA1M */
13982 genfn = gen_helper_crypto_sha1m;
13983 feature = dc_isar_feature(aa64_sha1, s);
13984 break;
13985 case 3: /* SHA1SU0 */
13986 genfn = gen_helper_crypto_sha1su0;
13987 feature = dc_isar_feature(aa64_sha1, s);
13988 break;
13989 case 4: /* SHA256H */
13990 genfn = gen_helper_crypto_sha256h;
13991 feature = dc_isar_feature(aa64_sha256, s);
13992 break;
13993 case 5: /* SHA256H2 */
13994 genfn = gen_helper_crypto_sha256h2;
13995 feature = dc_isar_feature(aa64_sha256, s);
13996 break;
13997 case 6: /* SHA256SU1 */
13998 genfn = gen_helper_crypto_sha256su1;
13999 feature = dc_isar_feature(aa64_sha256, s);
14000 break;
14001 default:
14002 unallocated_encoding(s);
14003 return;
14006 if (!feature) {
14007 unallocated_encoding(s);
14008 return;
14011 if (!fp_access_check(s)) {
14012 return;
14014 gen_gvec_op3_ool(s, true, rd, rn, rm, 0, genfn);
14017 /* Crypto two-reg SHA
14018 * 31 24 23 22 21 17 16 12 11 10 9 5 4 0
14019 * +-----------------+------+-----------+--------+-----+------+------+
14020 * | 0 1 0 1 1 1 1 0 | size | 1 0 1 0 0 | opcode | 1 0 | Rn | Rd |
14021 * +-----------------+------+-----------+--------+-----+------+------+
14023 static void disas_crypto_two_reg_sha(DisasContext *s, uint32_t insn)
14025 int size = extract32(insn, 22, 2);
14026 int opcode = extract32(insn, 12, 5);
14027 int rn = extract32(insn, 5, 5);
14028 int rd = extract32(insn, 0, 5);
14029 gen_helper_gvec_2 *genfn;
14030 bool feature;
14032 if (size != 0) {
14033 unallocated_encoding(s);
14034 return;
14037 switch (opcode) {
14038 case 0: /* SHA1H */
14039 feature = dc_isar_feature(aa64_sha1, s);
14040 genfn = gen_helper_crypto_sha1h;
14041 break;
14042 case 1: /* SHA1SU1 */
14043 feature = dc_isar_feature(aa64_sha1, s);
14044 genfn = gen_helper_crypto_sha1su1;
14045 break;
14046 case 2: /* SHA256SU0 */
14047 feature = dc_isar_feature(aa64_sha256, s);
14048 genfn = gen_helper_crypto_sha256su0;
14049 break;
14050 default:
14051 unallocated_encoding(s);
14052 return;
14055 if (!feature) {
14056 unallocated_encoding(s);
14057 return;
14060 if (!fp_access_check(s)) {
14061 return;
14063 gen_gvec_op2_ool(s, true, rd, rn, 0, genfn);
14066 static void gen_rax1_i64(TCGv_i64 d, TCGv_i64 n, TCGv_i64 m)
14068 tcg_gen_rotli_i64(d, m, 1);
14069 tcg_gen_xor_i64(d, d, n);
14072 static void gen_rax1_vec(unsigned vece, TCGv_vec d, TCGv_vec n, TCGv_vec m)
14074 tcg_gen_rotli_vec(vece, d, m, 1);
14075 tcg_gen_xor_vec(vece, d, d, n);
14078 void gen_gvec_rax1(unsigned vece, uint32_t rd_ofs, uint32_t rn_ofs,
14079 uint32_t rm_ofs, uint32_t opr_sz, uint32_t max_sz)
14081 static const TCGOpcode vecop_list[] = { INDEX_op_rotli_vec, 0 };
14082 static const GVecGen3 op = {
14083 .fni8 = gen_rax1_i64,
14084 .fniv = gen_rax1_vec,
14085 .opt_opc = vecop_list,
14086 .fno = gen_helper_crypto_rax1,
14087 .vece = MO_64,
14089 tcg_gen_gvec_3(rd_ofs, rn_ofs, rm_ofs, opr_sz, max_sz, &op);
14092 /* Crypto three-reg SHA512
14093 * 31 21 20 16 15 14 13 12 11 10 9 5 4 0
14094 * +-----------------------+------+---+---+-----+--------+------+------+
14095 * | 1 1 0 0 1 1 1 0 0 1 1 | Rm | 1 | O | 0 0 | opcode | Rn | Rd |
14096 * +-----------------------+------+---+---+-----+--------+------+------+
14098 static void disas_crypto_three_reg_sha512(DisasContext *s, uint32_t insn)
14100 int opcode = extract32(insn, 10, 2);
14101 int o = extract32(insn, 14, 1);
14102 int rm = extract32(insn, 16, 5);
14103 int rn = extract32(insn, 5, 5);
14104 int rd = extract32(insn, 0, 5);
14105 bool feature;
14106 gen_helper_gvec_3 *oolfn = NULL;
14107 GVecGen3Fn *gvecfn = NULL;
14109 if (o == 0) {
14110 switch (opcode) {
14111 case 0: /* SHA512H */
14112 feature = dc_isar_feature(aa64_sha512, s);
14113 oolfn = gen_helper_crypto_sha512h;
14114 break;
14115 case 1: /* SHA512H2 */
14116 feature = dc_isar_feature(aa64_sha512, s);
14117 oolfn = gen_helper_crypto_sha512h2;
14118 break;
14119 case 2: /* SHA512SU1 */
14120 feature = dc_isar_feature(aa64_sha512, s);
14121 oolfn = gen_helper_crypto_sha512su1;
14122 break;
14123 case 3: /* RAX1 */
14124 feature = dc_isar_feature(aa64_sha3, s);
14125 gvecfn = gen_gvec_rax1;
14126 break;
14127 default:
14128 g_assert_not_reached();
14130 } else {
14131 switch (opcode) {
14132 case 0: /* SM3PARTW1 */
14133 feature = dc_isar_feature(aa64_sm3, s);
14134 oolfn = gen_helper_crypto_sm3partw1;
14135 break;
14136 case 1: /* SM3PARTW2 */
14137 feature = dc_isar_feature(aa64_sm3, s);
14138 oolfn = gen_helper_crypto_sm3partw2;
14139 break;
14140 case 2: /* SM4EKEY */
14141 feature = dc_isar_feature(aa64_sm4, s);
14142 oolfn = gen_helper_crypto_sm4ekey;
14143 break;
14144 default:
14145 unallocated_encoding(s);
14146 return;
14150 if (!feature) {
14151 unallocated_encoding(s);
14152 return;
14155 if (!fp_access_check(s)) {
14156 return;
14159 if (oolfn) {
14160 gen_gvec_op3_ool(s, true, rd, rn, rm, 0, oolfn);
14161 } else {
14162 gen_gvec_fn3(s, true, rd, rn, rm, gvecfn, MO_64);
14166 /* Crypto two-reg SHA512
14167 * 31 12 11 10 9 5 4 0
14168 * +-----------------------------------------+--------+------+------+
14169 * | 1 1 0 0 1 1 1 0 1 1 0 0 0 0 0 0 1 0 0 0 | opcode | Rn | Rd |
14170 * +-----------------------------------------+--------+------+------+
14172 static void disas_crypto_two_reg_sha512(DisasContext *s, uint32_t insn)
14174 int opcode = extract32(insn, 10, 2);
14175 int rn = extract32(insn, 5, 5);
14176 int rd = extract32(insn, 0, 5);
14177 bool feature;
14179 switch (opcode) {
14180 case 0: /* SHA512SU0 */
14181 feature = dc_isar_feature(aa64_sha512, s);
14182 break;
14183 case 1: /* SM4E */
14184 feature = dc_isar_feature(aa64_sm4, s);
14185 break;
14186 default:
14187 unallocated_encoding(s);
14188 return;
14191 if (!feature) {
14192 unallocated_encoding(s);
14193 return;
14196 if (!fp_access_check(s)) {
14197 return;
14200 switch (opcode) {
14201 case 0: /* SHA512SU0 */
14202 gen_gvec_op2_ool(s, true, rd, rn, 0, gen_helper_crypto_sha512su0);
14203 break;
14204 case 1: /* SM4E */
14205 gen_gvec_op3_ool(s, true, rd, rd, rn, 0, gen_helper_crypto_sm4e);
14206 break;
14207 default:
14208 g_assert_not_reached();
14212 /* Crypto four-register
14213 * 31 23 22 21 20 16 15 14 10 9 5 4 0
14214 * +-------------------+-----+------+---+------+------+------+
14215 * | 1 1 0 0 1 1 1 0 0 | Op0 | Rm | 0 | Ra | Rn | Rd |
14216 * +-------------------+-----+------+---+------+------+------+
14218 static void disas_crypto_four_reg(DisasContext *s, uint32_t insn)
14220 int op0 = extract32(insn, 21, 2);
14221 int rm = extract32(insn, 16, 5);
14222 int ra = extract32(insn, 10, 5);
14223 int rn = extract32(insn, 5, 5);
14224 int rd = extract32(insn, 0, 5);
14225 bool feature;
14227 switch (op0) {
14228 case 0: /* EOR3 */
14229 case 1: /* BCAX */
14230 feature = dc_isar_feature(aa64_sha3, s);
14231 break;
14232 case 2: /* SM3SS1 */
14233 feature = dc_isar_feature(aa64_sm3, s);
14234 break;
14235 default:
14236 unallocated_encoding(s);
14237 return;
14240 if (!feature) {
14241 unallocated_encoding(s);
14242 return;
14245 if (!fp_access_check(s)) {
14246 return;
14249 if (op0 < 2) {
14250 TCGv_i64 tcg_op1, tcg_op2, tcg_op3, tcg_res[2];
14251 int pass;
14253 tcg_op1 = tcg_temp_new_i64();
14254 tcg_op2 = tcg_temp_new_i64();
14255 tcg_op3 = tcg_temp_new_i64();
14256 tcg_res[0] = tcg_temp_new_i64();
14257 tcg_res[1] = tcg_temp_new_i64();
14259 for (pass = 0; pass < 2; pass++) {
14260 read_vec_element(s, tcg_op1, rn, pass, MO_64);
14261 read_vec_element(s, tcg_op2, rm, pass, MO_64);
14262 read_vec_element(s, tcg_op3, ra, pass, MO_64);
14264 if (op0 == 0) {
14265 /* EOR3 */
14266 tcg_gen_xor_i64(tcg_res[pass], tcg_op2, tcg_op3);
14267 } else {
14268 /* BCAX */
14269 tcg_gen_andc_i64(tcg_res[pass], tcg_op2, tcg_op3);
14271 tcg_gen_xor_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
14273 write_vec_element(s, tcg_res[0], rd, 0, MO_64);
14274 write_vec_element(s, tcg_res[1], rd, 1, MO_64);
14276 tcg_temp_free_i64(tcg_op1);
14277 tcg_temp_free_i64(tcg_op2);
14278 tcg_temp_free_i64(tcg_op3);
14279 tcg_temp_free_i64(tcg_res[0]);
14280 tcg_temp_free_i64(tcg_res[1]);
14281 } else {
14282 TCGv_i32 tcg_op1, tcg_op2, tcg_op3, tcg_res, tcg_zero;
14284 tcg_op1 = tcg_temp_new_i32();
14285 tcg_op2 = tcg_temp_new_i32();
14286 tcg_op3 = tcg_temp_new_i32();
14287 tcg_res = tcg_temp_new_i32();
14288 tcg_zero = tcg_const_i32(0);
14290 read_vec_element_i32(s, tcg_op1, rn, 3, MO_32);
14291 read_vec_element_i32(s, tcg_op2, rm, 3, MO_32);
14292 read_vec_element_i32(s, tcg_op3, ra, 3, MO_32);
14294 tcg_gen_rotri_i32(tcg_res, tcg_op1, 20);
14295 tcg_gen_add_i32(tcg_res, tcg_res, tcg_op2);
14296 tcg_gen_add_i32(tcg_res, tcg_res, tcg_op3);
14297 tcg_gen_rotri_i32(tcg_res, tcg_res, 25);
14299 write_vec_element_i32(s, tcg_zero, rd, 0, MO_32);
14300 write_vec_element_i32(s, tcg_zero, rd, 1, MO_32);
14301 write_vec_element_i32(s, tcg_zero, rd, 2, MO_32);
14302 write_vec_element_i32(s, tcg_res, rd, 3, MO_32);
14304 tcg_temp_free_i32(tcg_op1);
14305 tcg_temp_free_i32(tcg_op2);
14306 tcg_temp_free_i32(tcg_op3);
14307 tcg_temp_free_i32(tcg_res);
14308 tcg_temp_free_i32(tcg_zero);
14312 /* Crypto XAR
14313 * 31 21 20 16 15 10 9 5 4 0
14314 * +-----------------------+------+--------+------+------+
14315 * | 1 1 0 0 1 1 1 0 1 0 0 | Rm | imm6 | Rn | Rd |
14316 * +-----------------------+------+--------+------+------+
14318 static void disas_crypto_xar(DisasContext *s, uint32_t insn)
14320 int rm = extract32(insn, 16, 5);
14321 int imm6 = extract32(insn, 10, 6);
14322 int rn = extract32(insn, 5, 5);
14323 int rd = extract32(insn, 0, 5);
14324 TCGv_i64 tcg_op1, tcg_op2, tcg_res[2];
14325 int pass;
14327 if (!dc_isar_feature(aa64_sha3, s)) {
14328 unallocated_encoding(s);
14329 return;
14332 if (!fp_access_check(s)) {
14333 return;
14336 tcg_op1 = tcg_temp_new_i64();
14337 tcg_op2 = tcg_temp_new_i64();
14338 tcg_res[0] = tcg_temp_new_i64();
14339 tcg_res[1] = tcg_temp_new_i64();
14341 for (pass = 0; pass < 2; pass++) {
14342 read_vec_element(s, tcg_op1, rn, pass, MO_64);
14343 read_vec_element(s, tcg_op2, rm, pass, MO_64);
14345 tcg_gen_xor_i64(tcg_res[pass], tcg_op1, tcg_op2);
14346 tcg_gen_rotri_i64(tcg_res[pass], tcg_res[pass], imm6);
14348 write_vec_element(s, tcg_res[0], rd, 0, MO_64);
14349 write_vec_element(s, tcg_res[1], rd, 1, MO_64);
14351 tcg_temp_free_i64(tcg_op1);
14352 tcg_temp_free_i64(tcg_op2);
14353 tcg_temp_free_i64(tcg_res[0]);
14354 tcg_temp_free_i64(tcg_res[1]);
14357 /* Crypto three-reg imm2
14358 * 31 21 20 16 15 14 13 12 11 10 9 5 4 0
14359 * +-----------------------+------+-----+------+--------+------+------+
14360 * | 1 1 0 0 1 1 1 0 0 1 0 | Rm | 1 0 | imm2 | opcode | Rn | Rd |
14361 * +-----------------------+------+-----+------+--------+------+------+
14363 static void disas_crypto_three_reg_imm2(DisasContext *s, uint32_t insn)
14365 static gen_helper_gvec_3 * const fns[4] = {
14366 gen_helper_crypto_sm3tt1a, gen_helper_crypto_sm3tt1b,
14367 gen_helper_crypto_sm3tt2a, gen_helper_crypto_sm3tt2b,
14369 int opcode = extract32(insn, 10, 2);
14370 int imm2 = extract32(insn, 12, 2);
14371 int rm = extract32(insn, 16, 5);
14372 int rn = extract32(insn, 5, 5);
14373 int rd = extract32(insn, 0, 5);
14375 if (!dc_isar_feature(aa64_sm3, s)) {
14376 unallocated_encoding(s);
14377 return;
14380 if (!fp_access_check(s)) {
14381 return;
14384 gen_gvec_op3_ool(s, true, rd, rn, rm, imm2, fns[opcode]);
14387 /* C3.6 Data processing - SIMD, inc Crypto
14389 * As the decode gets a little complex we are using a table based
14390 * approach for this part of the decode.
14392 static const AArch64DecodeTable data_proc_simd[] = {
14393 /* pattern , mask , fn */
14394 { 0x0e200400, 0x9f200400, disas_simd_three_reg_same },
14395 { 0x0e008400, 0x9f208400, disas_simd_three_reg_same_extra },
14396 { 0x0e200000, 0x9f200c00, disas_simd_three_reg_diff },
14397 { 0x0e200800, 0x9f3e0c00, disas_simd_two_reg_misc },
14398 { 0x0e300800, 0x9f3e0c00, disas_simd_across_lanes },
14399 { 0x0e000400, 0x9fe08400, disas_simd_copy },
14400 { 0x0f000000, 0x9f000400, disas_simd_indexed }, /* vector indexed */
14401 /* simd_mod_imm decode is a subset of simd_shift_imm, so must precede it */
14402 { 0x0f000400, 0x9ff80400, disas_simd_mod_imm },
14403 { 0x0f000400, 0x9f800400, disas_simd_shift_imm },
14404 { 0x0e000000, 0xbf208c00, disas_simd_tb },
14405 { 0x0e000800, 0xbf208c00, disas_simd_zip_trn },
14406 { 0x2e000000, 0xbf208400, disas_simd_ext },
14407 { 0x5e200400, 0xdf200400, disas_simd_scalar_three_reg_same },
14408 { 0x5e008400, 0xdf208400, disas_simd_scalar_three_reg_same_extra },
14409 { 0x5e200000, 0xdf200c00, disas_simd_scalar_three_reg_diff },
14410 { 0x5e200800, 0xdf3e0c00, disas_simd_scalar_two_reg_misc },
14411 { 0x5e300800, 0xdf3e0c00, disas_simd_scalar_pairwise },
14412 { 0x5e000400, 0xdfe08400, disas_simd_scalar_copy },
14413 { 0x5f000000, 0xdf000400, disas_simd_indexed }, /* scalar indexed */
14414 { 0x5f000400, 0xdf800400, disas_simd_scalar_shift_imm },
14415 { 0x4e280800, 0xff3e0c00, disas_crypto_aes },
14416 { 0x5e000000, 0xff208c00, disas_crypto_three_reg_sha },
14417 { 0x5e280800, 0xff3e0c00, disas_crypto_two_reg_sha },
14418 { 0xce608000, 0xffe0b000, disas_crypto_three_reg_sha512 },
14419 { 0xcec08000, 0xfffff000, disas_crypto_two_reg_sha512 },
14420 { 0xce000000, 0xff808000, disas_crypto_four_reg },
14421 { 0xce800000, 0xffe00000, disas_crypto_xar },
14422 { 0xce408000, 0xffe0c000, disas_crypto_three_reg_imm2 },
14423 { 0x0e400400, 0x9f60c400, disas_simd_three_reg_same_fp16 },
14424 { 0x0e780800, 0x8f7e0c00, disas_simd_two_reg_misc_fp16 },
14425 { 0x5e400400, 0xdf60c400, disas_simd_scalar_three_reg_same_fp16 },
14426 { 0x00000000, 0x00000000, NULL }
14429 static void disas_data_proc_simd(DisasContext *s, uint32_t insn)
14431 /* Note that this is called with all non-FP cases from
14432 * table C3-6 so it must UNDEF for entries not specifically
14433 * allocated to instructions in that table.
14435 AArch64DecodeFn *fn = lookup_disas_fn(&data_proc_simd[0], insn);
14436 if (fn) {
14437 fn(s, insn);
14438 } else {
14439 unallocated_encoding(s);
14443 /* C3.6 Data processing - SIMD and floating point */
14444 static void disas_data_proc_simd_fp(DisasContext *s, uint32_t insn)
14446 if (extract32(insn, 28, 1) == 1 && extract32(insn, 30, 1) == 0) {
14447 disas_data_proc_fp(s, insn);
14448 } else {
14449 /* SIMD, including crypto */
14450 disas_data_proc_simd(s, insn);
14455 * is_guarded_page:
14456 * @env: The cpu environment
14457 * @s: The DisasContext
14459 * Return true if the page is guarded.
14461 static bool is_guarded_page(CPUARMState *env, DisasContext *s)
14463 #ifdef CONFIG_USER_ONLY
14464 return false; /* FIXME */
14465 #else
14466 uint64_t addr = s->base.pc_first;
14467 int mmu_idx = arm_to_core_mmu_idx(s->mmu_idx);
14468 unsigned int index = tlb_index(env, mmu_idx, addr);
14469 CPUTLBEntry *entry = tlb_entry(env, mmu_idx, addr);
14472 * We test this immediately after reading an insn, which means
14473 * that any normal page must be in the TLB. The only exception
14474 * would be for executing from flash or device memory, which
14475 * does not retain the TLB entry.
14477 * FIXME: Assume false for those, for now. We could use
14478 * arm_cpu_get_phys_page_attrs_debug to re-read the page
14479 * table entry even for that case.
14481 return (tlb_hit(entry->addr_code, addr) &&
14482 arm_tlb_bti_gp(&env_tlb(env)->d[mmu_idx].iotlb[index].attrs));
14483 #endif
14487 * btype_destination_ok:
14488 * @insn: The instruction at the branch destination
14489 * @bt: SCTLR_ELx.BT
14490 * @btype: PSTATE.BTYPE, and is non-zero
14492 * On a guarded page, there are a limited number of insns
14493 * that may be present at the branch target:
14494 * - branch target identifiers,
14495 * - paciasp, pacibsp,
14496 * - BRK insn
14497 * - HLT insn
14498 * Anything else causes a Branch Target Exception.
14500 * Return true if the branch is compatible, false to raise BTITRAP.
14502 static bool btype_destination_ok(uint32_t insn, bool bt, int btype)
14504 if ((insn & 0xfffff01fu) == 0xd503201fu) {
14505 /* HINT space */
14506 switch (extract32(insn, 5, 7)) {
14507 case 0b011001: /* PACIASP */
14508 case 0b011011: /* PACIBSP */
14510 * If SCTLR_ELx.BT, then PACI*SP are not compatible
14511 * with btype == 3. Otherwise all btype are ok.
14513 return !bt || btype != 3;
14514 case 0b100000: /* BTI */
14515 /* Not compatible with any btype. */
14516 return false;
14517 case 0b100010: /* BTI c */
14518 /* Not compatible with btype == 3 */
14519 return btype != 3;
14520 case 0b100100: /* BTI j */
14521 /* Not compatible with btype == 2 */
14522 return btype != 2;
14523 case 0b100110: /* BTI jc */
14524 /* Compatible with any btype. */
14525 return true;
14527 } else {
14528 switch (insn & 0xffe0001fu) {
14529 case 0xd4200000u: /* BRK */
14530 case 0xd4400000u: /* HLT */
14531 /* Give priority to the breakpoint exception. */
14532 return true;
14535 return false;
14538 /* C3.1 A64 instruction index by encoding */
14539 static void disas_a64_insn(CPUARMState *env, DisasContext *s)
14541 uint32_t insn;
14543 s->pc_curr = s->base.pc_next;
14544 insn = arm_ldl_code(env, s->base.pc_next, s->sctlr_b);
14545 s->insn = insn;
14546 s->base.pc_next += 4;
14548 s->fp_access_checked = false;
14550 if (dc_isar_feature(aa64_bti, s)) {
14551 if (s->base.num_insns == 1) {
14553 * At the first insn of the TB, compute s->guarded_page.
14554 * We delayed computing this until successfully reading
14555 * the first insn of the TB, above. This (mostly) ensures
14556 * that the softmmu tlb entry has been populated, and the
14557 * page table GP bit is available.
14559 * Note that we need to compute this even if btype == 0,
14560 * because this value is used for BR instructions later
14561 * where ENV is not available.
14563 s->guarded_page = is_guarded_page(env, s);
14565 /* First insn can have btype set to non-zero. */
14566 tcg_debug_assert(s->btype >= 0);
14569 * Note that the Branch Target Exception has fairly high
14570 * priority -- below debugging exceptions but above most
14571 * everything else. This allows us to handle this now
14572 * instead of waiting until the insn is otherwise decoded.
14574 if (s->btype != 0
14575 && s->guarded_page
14576 && !btype_destination_ok(insn, s->bt, s->btype)) {
14577 gen_exception_insn(s, s->pc_curr, EXCP_UDEF,
14578 syn_btitrap(s->btype),
14579 default_exception_el(s));
14580 return;
14582 } else {
14583 /* Not the first insn: btype must be 0. */
14584 tcg_debug_assert(s->btype == 0);
14588 switch (extract32(insn, 25, 4)) {
14589 case 0x0: case 0x1: case 0x3: /* UNALLOCATED */
14590 unallocated_encoding(s);
14591 break;
14592 case 0x2:
14593 if (!dc_isar_feature(aa64_sve, s) || !disas_sve(s, insn)) {
14594 unallocated_encoding(s);
14596 break;
14597 case 0x8: case 0x9: /* Data processing - immediate */
14598 disas_data_proc_imm(s, insn);
14599 break;
14600 case 0xa: case 0xb: /* Branch, exception generation and system insns */
14601 disas_b_exc_sys(s, insn);
14602 break;
14603 case 0x4:
14604 case 0x6:
14605 case 0xc:
14606 case 0xe: /* Loads and stores */
14607 disas_ldst(s, insn);
14608 break;
14609 case 0x5:
14610 case 0xd: /* Data processing - register */
14611 disas_data_proc_reg(s, insn);
14612 break;
14613 case 0x7:
14614 case 0xf: /* Data processing - SIMD and floating point */
14615 disas_data_proc_simd_fp(s, insn);
14616 break;
14617 default:
14618 assert(FALSE); /* all 15 cases should be handled above */
14619 break;
14622 /* if we allocated any temporaries, free them here */
14623 free_tmp_a64(s);
14626 * After execution of most insns, btype is reset to 0.
14627 * Note that we set btype == -1 when the insn sets btype.
14629 if (s->btype > 0 && s->base.is_jmp != DISAS_NORETURN) {
14630 reset_btype(s);
14634 static void aarch64_tr_init_disas_context(DisasContextBase *dcbase,
14635 CPUState *cpu)
14637 DisasContext *dc = container_of(dcbase, DisasContext, base);
14638 CPUARMState *env = cpu->env_ptr;
14639 ARMCPU *arm_cpu = env_archcpu(env);
14640 uint32_t tb_flags = dc->base.tb->flags;
14641 int bound, core_mmu_idx;
14643 dc->isar = &arm_cpu->isar;
14644 dc->condjmp = 0;
14646 dc->aarch64 = 1;
14647 /* If we are coming from secure EL0 in a system with a 32-bit EL3, then
14648 * there is no secure EL1, so we route exceptions to EL3.
14650 dc->secure_routed_to_el3 = arm_feature(env, ARM_FEATURE_EL3) &&
14651 !arm_el_is_aa64(env, 3);
14652 dc->thumb = 0;
14653 dc->sctlr_b = 0;
14654 dc->be_data = FIELD_EX32(tb_flags, TBFLAG_ANY, BE_DATA) ? MO_BE : MO_LE;
14655 dc->condexec_mask = 0;
14656 dc->condexec_cond = 0;
14657 core_mmu_idx = FIELD_EX32(tb_flags, TBFLAG_ANY, MMUIDX);
14658 dc->mmu_idx = core_to_aa64_mmu_idx(core_mmu_idx);
14659 dc->tbii = FIELD_EX32(tb_flags, TBFLAG_A64, TBII);
14660 dc->tbid = FIELD_EX32(tb_flags, TBFLAG_A64, TBID);
14661 dc->tcma = FIELD_EX32(tb_flags, TBFLAG_A64, TCMA);
14662 dc->current_el = arm_mmu_idx_to_el(dc->mmu_idx);
14663 #if !defined(CONFIG_USER_ONLY)
14664 dc->user = (dc->current_el == 0);
14665 #endif
14666 dc->fp_excp_el = FIELD_EX32(tb_flags, TBFLAG_ANY, FPEXC_EL);
14667 dc->sve_excp_el = FIELD_EX32(tb_flags, TBFLAG_A64, SVEEXC_EL);
14668 dc->sve_len = (FIELD_EX32(tb_flags, TBFLAG_A64, ZCR_LEN) + 1) * 16;
14669 dc->pauth_active = FIELD_EX32(tb_flags, TBFLAG_A64, PAUTH_ACTIVE);
14670 dc->bt = FIELD_EX32(tb_flags, TBFLAG_A64, BT);
14671 dc->btype = FIELD_EX32(tb_flags, TBFLAG_A64, BTYPE);
14672 dc->unpriv = FIELD_EX32(tb_flags, TBFLAG_A64, UNPRIV);
14673 dc->ata = FIELD_EX32(tb_flags, TBFLAG_A64, ATA);
14674 dc->mte_active[0] = FIELD_EX32(tb_flags, TBFLAG_A64, MTE_ACTIVE);
14675 dc->mte_active[1] = FIELD_EX32(tb_flags, TBFLAG_A64, MTE0_ACTIVE);
14676 dc->vec_len = 0;
14677 dc->vec_stride = 0;
14678 dc->cp_regs = arm_cpu->cp_regs;
14679 dc->features = env->features;
14680 dc->dcz_blocksize = arm_cpu->dcz_blocksize;
14682 #ifdef CONFIG_USER_ONLY
14683 /* In sve_probe_page, we assume TBI is enabled. */
14684 tcg_debug_assert(dc->tbid & 1);
14685 #endif
14687 /* Single step state. The code-generation logic here is:
14688 * SS_ACTIVE == 0:
14689 * generate code with no special handling for single-stepping (except
14690 * that anything that can make us go to SS_ACTIVE == 1 must end the TB;
14691 * this happens anyway because those changes are all system register or
14692 * PSTATE writes).
14693 * SS_ACTIVE == 1, PSTATE.SS == 1: (active-not-pending)
14694 * emit code for one insn
14695 * emit code to clear PSTATE.SS
14696 * emit code to generate software step exception for completed step
14697 * end TB (as usual for having generated an exception)
14698 * SS_ACTIVE == 1, PSTATE.SS == 0: (active-pending)
14699 * emit code to generate a software step exception
14700 * end the TB
14702 dc->ss_active = FIELD_EX32(tb_flags, TBFLAG_ANY, SS_ACTIVE);
14703 dc->pstate_ss = FIELD_EX32(tb_flags, TBFLAG_ANY, PSTATE_SS);
14704 dc->is_ldex = false;
14705 dc->debug_target_el = FIELD_EX32(tb_flags, TBFLAG_ANY, DEBUG_TARGET_EL);
14707 /* Bound the number of insns to execute to those left on the page. */
14708 bound = -(dc->base.pc_first | TARGET_PAGE_MASK) / 4;
14710 /* If architectural single step active, limit to 1. */
14711 if (dc->ss_active) {
14712 bound = 1;
14714 dc->base.max_insns = MIN(dc->base.max_insns, bound);
14716 init_tmp_a64_array(dc);
14719 static void aarch64_tr_tb_start(DisasContextBase *db, CPUState *cpu)
14723 static void aarch64_tr_insn_start(DisasContextBase *dcbase, CPUState *cpu)
14725 DisasContext *dc = container_of(dcbase, DisasContext, base);
14727 tcg_gen_insn_start(dc->base.pc_next, 0, 0);
14728 dc->insn_start = tcg_last_op();
14731 static bool aarch64_tr_breakpoint_check(DisasContextBase *dcbase, CPUState *cpu,
14732 const CPUBreakpoint *bp)
14734 DisasContext *dc = container_of(dcbase, DisasContext, base);
14736 if (bp->flags & BP_CPU) {
14737 gen_a64_set_pc_im(dc->base.pc_next);
14738 gen_helper_check_breakpoints(cpu_env);
14739 /* End the TB early; it likely won't be executed */
14740 dc->base.is_jmp = DISAS_TOO_MANY;
14741 } else {
14742 gen_exception_internal_insn(dc, dc->base.pc_next, EXCP_DEBUG);
14743 /* The address covered by the breakpoint must be
14744 included in [tb->pc, tb->pc + tb->size) in order
14745 to for it to be properly cleared -- thus we
14746 increment the PC here so that the logic setting
14747 tb->size below does the right thing. */
14748 dc->base.pc_next += 4;
14749 dc->base.is_jmp = DISAS_NORETURN;
14752 return true;
14755 static void aarch64_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)
14757 DisasContext *dc = container_of(dcbase, DisasContext, base);
14758 CPUARMState *env = cpu->env_ptr;
14760 if (dc->ss_active && !dc->pstate_ss) {
14761 /* Singlestep state is Active-pending.
14762 * If we're in this state at the start of a TB then either
14763 * a) we just took an exception to an EL which is being debugged
14764 * and this is the first insn in the exception handler
14765 * b) debug exceptions were masked and we just unmasked them
14766 * without changing EL (eg by clearing PSTATE.D)
14767 * In either case we're going to take a swstep exception in the
14768 * "did not step an insn" case, and so the syndrome ISV and EX
14769 * bits should be zero.
14771 assert(dc->base.num_insns == 1);
14772 gen_swstep_exception(dc, 0, 0);
14773 dc->base.is_jmp = DISAS_NORETURN;
14774 } else {
14775 disas_a64_insn(env, dc);
14778 translator_loop_temp_check(&dc->base);
14781 static void aarch64_tr_tb_stop(DisasContextBase *dcbase, CPUState *cpu)
14783 DisasContext *dc = container_of(dcbase, DisasContext, base);
14785 if (unlikely(dc->base.singlestep_enabled || dc->ss_active)) {
14786 /* Note that this means single stepping WFI doesn't halt the CPU.
14787 * For conditional branch insns this is harmless unreachable code as
14788 * gen_goto_tb() has already handled emitting the debug exception
14789 * (and thus a tb-jump is not possible when singlestepping).
14791 switch (dc->base.is_jmp) {
14792 default:
14793 gen_a64_set_pc_im(dc->base.pc_next);
14794 /* fall through */
14795 case DISAS_EXIT:
14796 case DISAS_JUMP:
14797 if (dc->base.singlestep_enabled) {
14798 gen_exception_internal(EXCP_DEBUG);
14799 } else {
14800 gen_step_complete_exception(dc);
14802 break;
14803 case DISAS_NORETURN:
14804 break;
14806 } else {
14807 switch (dc->base.is_jmp) {
14808 case DISAS_NEXT:
14809 case DISAS_TOO_MANY:
14810 gen_goto_tb(dc, 1, dc->base.pc_next);
14811 break;
14812 default:
14813 case DISAS_UPDATE_EXIT:
14814 gen_a64_set_pc_im(dc->base.pc_next);
14815 /* fall through */
14816 case DISAS_EXIT:
14817 tcg_gen_exit_tb(NULL, 0);
14818 break;
14819 case DISAS_UPDATE_NOCHAIN:
14820 gen_a64_set_pc_im(dc->base.pc_next);
14821 /* fall through */
14822 case DISAS_JUMP:
14823 tcg_gen_lookup_and_goto_ptr();
14824 break;
14825 case DISAS_NORETURN:
14826 case DISAS_SWI:
14827 break;
14828 case DISAS_WFE:
14829 gen_a64_set_pc_im(dc->base.pc_next);
14830 gen_helper_wfe(cpu_env);
14831 break;
14832 case DISAS_YIELD:
14833 gen_a64_set_pc_im(dc->base.pc_next);
14834 gen_helper_yield(cpu_env);
14835 break;
14836 case DISAS_WFI:
14838 /* This is a special case because we don't want to just halt the CPU
14839 * if trying to debug across a WFI.
14841 TCGv_i32 tmp = tcg_const_i32(4);
14843 gen_a64_set_pc_im(dc->base.pc_next);
14844 gen_helper_wfi(cpu_env, tmp);
14845 tcg_temp_free_i32(tmp);
14846 /* The helper doesn't necessarily throw an exception, but we
14847 * must go back to the main loop to check for interrupts anyway.
14849 tcg_gen_exit_tb(NULL, 0);
14850 break;
14856 static void aarch64_tr_disas_log(const DisasContextBase *dcbase,
14857 CPUState *cpu)
14859 DisasContext *dc = container_of(dcbase, DisasContext, base);
14861 qemu_log("IN: %s\n", lookup_symbol(dc->base.pc_first));
14862 log_target_disas(cpu, dc->base.pc_first, dc->base.tb->size);
14865 const TranslatorOps aarch64_translator_ops = {
14866 .init_disas_context = aarch64_tr_init_disas_context,
14867 .tb_start = aarch64_tr_tb_start,
14868 .insn_start = aarch64_tr_insn_start,
14869 .breakpoint_check = aarch64_tr_breakpoint_check,
14870 .translate_insn = aarch64_tr_translate_insn,
14871 .tb_stop = aarch64_tr_tb_stop,
14872 .disas_log = aarch64_tr_disas_log,