target/arm: Remove arm_free_cc, a64_free_cc
[qemu/ar7.git] / target / arm / tcg / translate-a64.c
blobd1e677ca763e6eb5b1f59bdb1f7a1076d142e3cc
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.1 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"
30 #include "semihosting/semihost.h"
31 #include "exec/gen-icount.h"
32 #include "exec/helper-proto.h"
33 #include "exec/helper-gen.h"
34 #include "exec/log.h"
35 #include "cpregs.h"
36 #include "translate-a64.h"
37 #include "qemu/atomic128.h"
39 static TCGv_i64 cpu_X[32];
40 static TCGv_i64 cpu_pc;
42 /* Load/store exclusive handling */
43 static TCGv_i64 cpu_exclusive_high;
45 static const char *regnames[] = {
46 "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7",
47 "x8", "x9", "x10", "x11", "x12", "x13", "x14", "x15",
48 "x16", "x17", "x18", "x19", "x20", "x21", "x22", "x23",
49 "x24", "x25", "x26", "x27", "x28", "x29", "lr", "sp"
52 enum a64_shift_type {
53 A64_SHIFT_TYPE_LSL = 0,
54 A64_SHIFT_TYPE_LSR = 1,
55 A64_SHIFT_TYPE_ASR = 2,
56 A64_SHIFT_TYPE_ROR = 3
59 /* Table based decoder typedefs - used when the relevant bits for decode
60 * are too awkwardly scattered across the instruction (eg SIMD).
62 typedef void AArch64DecodeFn(DisasContext *s, uint32_t insn);
64 typedef struct AArch64DecodeTable {
65 uint32_t pattern;
66 uint32_t mask;
67 AArch64DecodeFn *disas_fn;
68 } AArch64DecodeTable;
70 /* initialize TCG globals. */
71 void a64_translate_init(void)
73 int i;
75 cpu_pc = tcg_global_mem_new_i64(cpu_env,
76 offsetof(CPUARMState, pc),
77 "pc");
78 for (i = 0; i < 32; i++) {
79 cpu_X[i] = tcg_global_mem_new_i64(cpu_env,
80 offsetof(CPUARMState, xregs[i]),
81 regnames[i]);
84 cpu_exclusive_high = tcg_global_mem_new_i64(cpu_env,
85 offsetof(CPUARMState, exclusive_high), "exclusive_high");
89 * Return the core mmu_idx to use for A64 "unprivileged load/store" insns
91 static int get_a64_user_mem_index(DisasContext *s)
94 * If AccType_UNPRIV is not used, the insn uses AccType_NORMAL,
95 * which is the usual mmu_idx for this cpu state.
97 ARMMMUIdx useridx = s->mmu_idx;
99 if (s->unpriv) {
101 * We have pre-computed the condition for AccType_UNPRIV.
102 * Therefore we should never get here with a mmu_idx for
103 * which we do not know the corresponding user mmu_idx.
105 switch (useridx) {
106 case ARMMMUIdx_E10_1:
107 case ARMMMUIdx_E10_1_PAN:
108 useridx = ARMMMUIdx_E10_0;
109 break;
110 case ARMMMUIdx_E20_2:
111 case ARMMMUIdx_E20_2_PAN:
112 useridx = ARMMMUIdx_E20_0;
113 break;
114 default:
115 g_assert_not_reached();
118 return arm_to_core_mmu_idx(useridx);
121 static void set_btype_raw(int val)
123 tcg_gen_st_i32(tcg_constant_i32(val), cpu_env,
124 offsetof(CPUARMState, btype));
127 static void set_btype(DisasContext *s, int val)
129 /* BTYPE is a 2-bit field, and 0 should be done with reset_btype. */
130 tcg_debug_assert(val >= 1 && val <= 3);
131 set_btype_raw(val);
132 s->btype = -1;
135 static void reset_btype(DisasContext *s)
137 if (s->btype != 0) {
138 set_btype_raw(0);
139 s->btype = 0;
143 static void gen_pc_plus_diff(DisasContext *s, TCGv_i64 dest, target_long diff)
145 assert(s->pc_save != -1);
146 if (tb_cflags(s->base.tb) & CF_PCREL) {
147 tcg_gen_addi_i64(dest, cpu_pc, (s->pc_curr - s->pc_save) + diff);
148 } else {
149 tcg_gen_movi_i64(dest, s->pc_curr + diff);
153 void gen_a64_update_pc(DisasContext *s, target_long diff)
155 gen_pc_plus_diff(s, cpu_pc, diff);
156 s->pc_save = s->pc_curr + diff;
160 * Handle Top Byte Ignore (TBI) bits.
162 * If address tagging is enabled via the TCR TBI bits:
163 * + for EL2 and EL3 there is only one TBI bit, and if it is set
164 * then the address is zero-extended, clearing bits [63:56]
165 * + for EL0 and EL1, TBI0 controls addresses with bit 55 == 0
166 * and TBI1 controls addressses with bit 55 == 1.
167 * If the appropriate TBI bit is set for the address then
168 * the address is sign-extended from bit 55 into bits [63:56]
170 * Here We have concatenated TBI{1,0} into tbi.
172 static void gen_top_byte_ignore(DisasContext *s, TCGv_i64 dst,
173 TCGv_i64 src, int tbi)
175 if (tbi == 0) {
176 /* Load unmodified address */
177 tcg_gen_mov_i64(dst, src);
178 } else if (!regime_has_2_ranges(s->mmu_idx)) {
179 /* Force tag byte to all zero */
180 tcg_gen_extract_i64(dst, src, 0, 56);
181 } else {
182 /* Sign-extend from bit 55. */
183 tcg_gen_sextract_i64(dst, src, 0, 56);
185 switch (tbi) {
186 case 1:
187 /* tbi0 but !tbi1: only use the extension if positive */
188 tcg_gen_and_i64(dst, dst, src);
189 break;
190 case 2:
191 /* !tbi0 but tbi1: only use the extension if negative */
192 tcg_gen_or_i64(dst, dst, src);
193 break;
194 case 3:
195 /* tbi0 and tbi1: always use the extension */
196 break;
197 default:
198 g_assert_not_reached();
203 static void gen_a64_set_pc(DisasContext *s, TCGv_i64 src)
206 * If address tagging is enabled for instructions via the TCR TBI bits,
207 * then loading an address into the PC will clear out any tag.
209 gen_top_byte_ignore(s, cpu_pc, src, s->tbii);
210 s->pc_save = -1;
214 * Handle MTE and/or TBI.
216 * For TBI, ideally, we would do nothing. Proper behaviour on fault is
217 * for the tag to be present in the FAR_ELx register. But for user-only
218 * mode we do not have a TLB with which to implement this, so we must
219 * remove the top byte now.
221 * Always return a fresh temporary that we can increment independently
222 * of the write-back address.
225 TCGv_i64 clean_data_tbi(DisasContext *s, TCGv_i64 addr)
227 TCGv_i64 clean = new_tmp_a64(s);
228 #ifdef CONFIG_USER_ONLY
229 gen_top_byte_ignore(s, clean, addr, s->tbid);
230 #else
231 tcg_gen_mov_i64(clean, addr);
232 #endif
233 return clean;
236 /* Insert a zero tag into src, with the result at dst. */
237 static void gen_address_with_allocation_tag0(TCGv_i64 dst, TCGv_i64 src)
239 tcg_gen_andi_i64(dst, src, ~MAKE_64BIT_MASK(56, 4));
242 static void gen_probe_access(DisasContext *s, TCGv_i64 ptr,
243 MMUAccessType acc, int log2_size)
245 gen_helper_probe_access(cpu_env, ptr,
246 tcg_constant_i32(acc),
247 tcg_constant_i32(get_mem_index(s)),
248 tcg_constant_i32(1 << log2_size));
252 * For MTE, check a single logical or atomic access. This probes a single
253 * address, the exact one specified. The size and alignment of the access
254 * is not relevant to MTE, per se, but watchpoints do require the size,
255 * and we want to recognize those before making any other changes to state.
257 static TCGv_i64 gen_mte_check1_mmuidx(DisasContext *s, TCGv_i64 addr,
258 bool is_write, bool tag_checked,
259 int log2_size, bool is_unpriv,
260 int core_idx)
262 if (tag_checked && s->mte_active[is_unpriv]) {
263 TCGv_i64 ret;
264 int desc = 0;
266 desc = FIELD_DP32(desc, MTEDESC, MIDX, core_idx);
267 desc = FIELD_DP32(desc, MTEDESC, TBI, s->tbid);
268 desc = FIELD_DP32(desc, MTEDESC, TCMA, s->tcma);
269 desc = FIELD_DP32(desc, MTEDESC, WRITE, is_write);
270 desc = FIELD_DP32(desc, MTEDESC, SIZEM1, (1 << log2_size) - 1);
272 ret = new_tmp_a64(s);
273 gen_helper_mte_check(ret, cpu_env, tcg_constant_i32(desc), addr);
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 size)
293 if (tag_checked && s->mte_active[0]) {
294 TCGv_i64 ret;
295 int desc = 0;
297 desc = FIELD_DP32(desc, MTEDESC, MIDX, get_mem_index(s));
298 desc = FIELD_DP32(desc, MTEDESC, TBI, s->tbid);
299 desc = FIELD_DP32(desc, MTEDESC, TCMA, s->tcma);
300 desc = FIELD_DP32(desc, MTEDESC, WRITE, is_write);
301 desc = FIELD_DP32(desc, MTEDESC, SIZEM1, size - 1);
303 ret = new_tmp_a64(s);
304 gen_helper_mte_check(ret, cpu_env, tcg_constant_i32(desc), addr);
306 return ret;
308 return clean_data_tbi(s, addr);
311 typedef struct DisasCompare64 {
312 TCGCond cond;
313 TCGv_i64 value;
314 } DisasCompare64;
316 static void a64_test_cc(DisasCompare64 *c64, int cc)
318 DisasCompare c32;
320 arm_test_cc(&c32, cc);
323 * Sign-extend the 32-bit value so that the GE/LT comparisons work
324 * properly. The NE/EQ comparisons are also fine with this choice.
326 c64->cond = c32.cond;
327 c64->value = tcg_temp_new_i64();
328 tcg_gen_ext_i32_i64(c64->value, c32.value);
331 static void gen_rebuild_hflags(DisasContext *s)
333 gen_helper_rebuild_hflags_a64(cpu_env, tcg_constant_i32(s->current_el));
336 static void gen_exception_internal(int excp)
338 assert(excp_is_internal(excp));
339 gen_helper_exception_internal(cpu_env, tcg_constant_i32(excp));
342 static void gen_exception_internal_insn(DisasContext *s, int excp)
344 gen_a64_update_pc(s, 0);
345 gen_exception_internal(excp);
346 s->base.is_jmp = DISAS_NORETURN;
349 static void gen_exception_bkpt_insn(DisasContext *s, uint32_t syndrome)
351 gen_a64_update_pc(s, 0);
352 gen_helper_exception_bkpt_insn(cpu_env, tcg_constant_i32(syndrome));
353 s->base.is_jmp = DISAS_NORETURN;
356 static void gen_step_complete_exception(DisasContext *s)
358 /* We just completed step of an insn. Move from Active-not-pending
359 * to Active-pending, and then also take the swstep exception.
360 * This corresponds to making the (IMPDEF) choice to prioritize
361 * swstep exceptions over asynchronous exceptions taken to an exception
362 * level where debug is disabled. This choice has the advantage that
363 * we do not need to maintain internal state corresponding to the
364 * ISV/EX syndrome bits between completion of the step and generation
365 * of the exception, and our syndrome information is always correct.
367 gen_ss_advance(s);
368 gen_swstep_exception(s, 1, s->is_ldex);
369 s->base.is_jmp = DISAS_NORETURN;
372 static inline bool use_goto_tb(DisasContext *s, uint64_t dest)
374 if (s->ss_active) {
375 return false;
377 return translator_use_goto_tb(&s->base, dest);
380 static void gen_goto_tb(DisasContext *s, int n, int64_t diff)
382 if (use_goto_tb(s, s->pc_curr + diff)) {
384 * For pcrel, the pc must always be up-to-date on entry to
385 * the linked TB, so that it can use simple additions for all
386 * further adjustments. For !pcrel, the linked TB is compiled
387 * to know its full virtual address, so we can delay the
388 * update to pc to the unlinked path. A long chain of links
389 * can thus avoid many updates to the PC.
391 if (tb_cflags(s->base.tb) & CF_PCREL) {
392 gen_a64_update_pc(s, diff);
393 tcg_gen_goto_tb(n);
394 } else {
395 tcg_gen_goto_tb(n);
396 gen_a64_update_pc(s, diff);
398 tcg_gen_exit_tb(s->base.tb, n);
399 s->base.is_jmp = DISAS_NORETURN;
400 } else {
401 gen_a64_update_pc(s, diff);
402 if (s->ss_active) {
403 gen_step_complete_exception(s);
404 } else {
405 tcg_gen_lookup_and_goto_ptr();
406 s->base.is_jmp = DISAS_NORETURN;
411 static void init_tmp_a64_array(DisasContext *s)
413 #ifdef CONFIG_DEBUG_TCG
414 memset(s->tmp_a64, 0, sizeof(s->tmp_a64));
415 #endif
416 s->tmp_a64_count = 0;
419 static void free_tmp_a64(DisasContext *s)
421 int i;
422 for (i = 0; i < s->tmp_a64_count; i++) {
423 tcg_temp_free_i64(s->tmp_a64[i]);
425 init_tmp_a64_array(s);
428 TCGv_i64 new_tmp_a64(DisasContext *s)
430 assert(s->tmp_a64_count < TMP_A64_MAX);
431 return s->tmp_a64[s->tmp_a64_count++] = tcg_temp_new_i64();
434 TCGv_i64 new_tmp_a64_zero(DisasContext *s)
436 TCGv_i64 t = new_tmp_a64(s);
437 tcg_gen_movi_i64(t, 0);
438 return t;
442 * Register access functions
444 * These functions are used for directly accessing a register in where
445 * changes to the final register value are likely to be made. If you
446 * need to use a register for temporary calculation (e.g. index type
447 * operations) use the read_* form.
449 * B1.2.1 Register mappings
451 * In instruction register encoding 31 can refer to ZR (zero register) or
452 * the SP (stack pointer) depending on context. In QEMU's case we map SP
453 * to cpu_X[31] and ZR accesses to a temporary which can be discarded.
454 * This is the point of the _sp forms.
456 TCGv_i64 cpu_reg(DisasContext *s, int reg)
458 if (reg == 31) {
459 return new_tmp_a64_zero(s);
460 } else {
461 return cpu_X[reg];
465 /* register access for when 31 == SP */
466 TCGv_i64 cpu_reg_sp(DisasContext *s, int reg)
468 return cpu_X[reg];
471 /* read a cpu register in 32bit/64bit mode. Returns a TCGv_i64
472 * representing the register contents. This TCGv is an auto-freed
473 * temporary so it need not be explicitly freed, and may be modified.
475 TCGv_i64 read_cpu_reg(DisasContext *s, int reg, int sf)
477 TCGv_i64 v = new_tmp_a64(s);
478 if (reg != 31) {
479 if (sf) {
480 tcg_gen_mov_i64(v, cpu_X[reg]);
481 } else {
482 tcg_gen_ext32u_i64(v, cpu_X[reg]);
484 } else {
485 tcg_gen_movi_i64(v, 0);
487 return v;
490 TCGv_i64 read_cpu_reg_sp(DisasContext *s, int reg, int sf)
492 TCGv_i64 v = new_tmp_a64(s);
493 if (sf) {
494 tcg_gen_mov_i64(v, cpu_X[reg]);
495 } else {
496 tcg_gen_ext32u_i64(v, cpu_X[reg]);
498 return v;
501 /* Return the offset into CPUARMState of a slice (from
502 * the least significant end) of FP register Qn (ie
503 * Dn, Sn, Hn or Bn).
504 * (Note that this is not the same mapping as for A32; see cpu.h)
506 static inline int fp_reg_offset(DisasContext *s, int regno, MemOp size)
508 return vec_reg_offset(s, regno, 0, size);
511 /* Offset of the high half of the 128 bit vector Qn */
512 static inline int fp_reg_hi_offset(DisasContext *s, int regno)
514 return vec_reg_offset(s, regno, 1, MO_64);
517 /* Convenience accessors for reading and writing single and double
518 * FP registers. Writing clears the upper parts of the associated
519 * 128 bit vector register, as required by the architecture.
520 * Note that unlike the GP register accessors, the values returned
521 * by the read functions must be manually freed.
523 static TCGv_i64 read_fp_dreg(DisasContext *s, int reg)
525 TCGv_i64 v = tcg_temp_new_i64();
527 tcg_gen_ld_i64(v, cpu_env, fp_reg_offset(s, reg, MO_64));
528 return v;
531 static TCGv_i32 read_fp_sreg(DisasContext *s, int reg)
533 TCGv_i32 v = tcg_temp_new_i32();
535 tcg_gen_ld_i32(v, cpu_env, fp_reg_offset(s, reg, MO_32));
536 return v;
539 static TCGv_i32 read_fp_hreg(DisasContext *s, int reg)
541 TCGv_i32 v = tcg_temp_new_i32();
543 tcg_gen_ld16u_i32(v, cpu_env, fp_reg_offset(s, reg, MO_16));
544 return v;
547 /* Clear the bits above an N-bit vector, for N = (is_q ? 128 : 64).
548 * If SVE is not enabled, then there are only 128 bits in the vector.
550 static void clear_vec_high(DisasContext *s, bool is_q, int rd)
552 unsigned ofs = fp_reg_offset(s, rd, MO_64);
553 unsigned vsz = vec_full_reg_size(s);
555 /* Nop move, with side effect of clearing the tail. */
556 tcg_gen_gvec_mov(MO_64, ofs, ofs, is_q ? 16 : 8, vsz);
559 void write_fp_dreg(DisasContext *s, int reg, TCGv_i64 v)
561 unsigned ofs = fp_reg_offset(s, reg, MO_64);
563 tcg_gen_st_i64(v, cpu_env, ofs);
564 clear_vec_high(s, false, reg);
567 static void write_fp_sreg(DisasContext *s, int reg, TCGv_i32 v)
569 TCGv_i64 tmp = tcg_temp_new_i64();
571 tcg_gen_extu_i32_i64(tmp, v);
572 write_fp_dreg(s, reg, tmp);
573 tcg_temp_free_i64(tmp);
576 /* Expand a 2-operand AdvSIMD vector operation using an expander function. */
577 static void gen_gvec_fn2(DisasContext *s, bool is_q, int rd, int rn,
578 GVecGen2Fn *gvec_fn, int vece)
580 gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
581 is_q ? 16 : 8, vec_full_reg_size(s));
584 /* Expand a 2-operand + immediate AdvSIMD vector operation using
585 * an expander function.
587 static void gen_gvec_fn2i(DisasContext *s, bool is_q, int rd, int rn,
588 int64_t imm, GVecGen2iFn *gvec_fn, int vece)
590 gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
591 imm, is_q ? 16 : 8, vec_full_reg_size(s));
594 /* Expand a 3-operand AdvSIMD vector operation using an expander function. */
595 static void gen_gvec_fn3(DisasContext *s, bool is_q, int rd, int rn, int rm,
596 GVecGen3Fn *gvec_fn, int vece)
598 gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
599 vec_full_reg_offset(s, rm), is_q ? 16 : 8, vec_full_reg_size(s));
602 /* Expand a 4-operand AdvSIMD vector operation using an expander function. */
603 static void gen_gvec_fn4(DisasContext *s, bool is_q, int rd, int rn, int rm,
604 int rx, GVecGen4Fn *gvec_fn, int vece)
606 gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
607 vec_full_reg_offset(s, rm), vec_full_reg_offset(s, rx),
608 is_q ? 16 : 8, vec_full_reg_size(s));
611 /* Expand a 2-operand operation using an out-of-line helper. */
612 static void gen_gvec_op2_ool(DisasContext *s, bool is_q, int rd,
613 int rn, int data, gen_helper_gvec_2 *fn)
615 tcg_gen_gvec_2_ool(vec_full_reg_offset(s, rd),
616 vec_full_reg_offset(s, rn),
617 is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
620 /* Expand a 3-operand operation using an out-of-line helper. */
621 static void gen_gvec_op3_ool(DisasContext *s, bool is_q, int rd,
622 int rn, int rm, int data, gen_helper_gvec_3 *fn)
624 tcg_gen_gvec_3_ool(vec_full_reg_offset(s, rd),
625 vec_full_reg_offset(s, rn),
626 vec_full_reg_offset(s, rm),
627 is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
630 /* Expand a 3-operand + fpstatus pointer + simd data value operation using
631 * an out-of-line helper.
633 static void gen_gvec_op3_fpst(DisasContext *s, bool is_q, int rd, int rn,
634 int rm, bool is_fp16, int data,
635 gen_helper_gvec_3_ptr *fn)
637 TCGv_ptr fpst = fpstatus_ptr(is_fp16 ? FPST_FPCR_F16 : FPST_FPCR);
638 tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
639 vec_full_reg_offset(s, rn),
640 vec_full_reg_offset(s, rm), fpst,
641 is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
642 tcg_temp_free_ptr(fpst);
645 /* Expand a 3-operand + qc + operation using an out-of-line helper. */
646 static void gen_gvec_op3_qc(DisasContext *s, bool is_q, int rd, int rn,
647 int rm, gen_helper_gvec_3_ptr *fn)
649 TCGv_ptr qc_ptr = tcg_temp_new_ptr();
651 tcg_gen_addi_ptr(qc_ptr, cpu_env, offsetof(CPUARMState, vfp.qc));
652 tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
653 vec_full_reg_offset(s, rn),
654 vec_full_reg_offset(s, rm), qc_ptr,
655 is_q ? 16 : 8, vec_full_reg_size(s), 0, fn);
656 tcg_temp_free_ptr(qc_ptr);
659 /* Expand a 4-operand operation using an out-of-line helper. */
660 static void gen_gvec_op4_ool(DisasContext *s, bool is_q, int rd, int rn,
661 int rm, int ra, int data, gen_helper_gvec_4 *fn)
663 tcg_gen_gvec_4_ool(vec_full_reg_offset(s, rd),
664 vec_full_reg_offset(s, rn),
665 vec_full_reg_offset(s, rm),
666 vec_full_reg_offset(s, ra),
667 is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
671 * Expand a 4-operand + fpstatus pointer + simd data value operation using
672 * an out-of-line helper.
674 static void gen_gvec_op4_fpst(DisasContext *s, bool is_q, int rd, int rn,
675 int rm, int ra, bool is_fp16, int data,
676 gen_helper_gvec_4_ptr *fn)
678 TCGv_ptr fpst = fpstatus_ptr(is_fp16 ? FPST_FPCR_F16 : FPST_FPCR);
679 tcg_gen_gvec_4_ptr(vec_full_reg_offset(s, rd),
680 vec_full_reg_offset(s, rn),
681 vec_full_reg_offset(s, rm),
682 vec_full_reg_offset(s, ra), fpst,
683 is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
684 tcg_temp_free_ptr(fpst);
687 /* Set ZF and NF based on a 64 bit result. This is alas fiddlier
688 * than the 32 bit equivalent.
690 static inline void gen_set_NZ64(TCGv_i64 result)
692 tcg_gen_extr_i64_i32(cpu_ZF, cpu_NF, result);
693 tcg_gen_or_i32(cpu_ZF, cpu_ZF, cpu_NF);
696 /* Set NZCV as for a logical operation: NZ as per result, CV cleared. */
697 static inline void gen_logic_CC(int sf, TCGv_i64 result)
699 if (sf) {
700 gen_set_NZ64(result);
701 } else {
702 tcg_gen_extrl_i64_i32(cpu_ZF, result);
703 tcg_gen_mov_i32(cpu_NF, cpu_ZF);
705 tcg_gen_movi_i32(cpu_CF, 0);
706 tcg_gen_movi_i32(cpu_VF, 0);
709 /* dest = T0 + T1; compute C, N, V and Z flags */
710 static void gen_add_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
712 if (sf) {
713 TCGv_i64 result, flag, tmp;
714 result = tcg_temp_new_i64();
715 flag = tcg_temp_new_i64();
716 tmp = tcg_temp_new_i64();
718 tcg_gen_movi_i64(tmp, 0);
719 tcg_gen_add2_i64(result, flag, t0, tmp, t1, tmp);
721 tcg_gen_extrl_i64_i32(cpu_CF, flag);
723 gen_set_NZ64(result);
725 tcg_gen_xor_i64(flag, result, t0);
726 tcg_gen_xor_i64(tmp, t0, t1);
727 tcg_gen_andc_i64(flag, flag, tmp);
728 tcg_temp_free_i64(tmp);
729 tcg_gen_extrh_i64_i32(cpu_VF, flag);
731 tcg_gen_mov_i64(dest, result);
732 tcg_temp_free_i64(result);
733 tcg_temp_free_i64(flag);
734 } else {
735 /* 32 bit arithmetic */
736 TCGv_i32 t0_32 = tcg_temp_new_i32();
737 TCGv_i32 t1_32 = tcg_temp_new_i32();
738 TCGv_i32 tmp = tcg_temp_new_i32();
740 tcg_gen_movi_i32(tmp, 0);
741 tcg_gen_extrl_i64_i32(t0_32, t0);
742 tcg_gen_extrl_i64_i32(t1_32, t1);
743 tcg_gen_add2_i32(cpu_NF, cpu_CF, t0_32, tmp, t1_32, tmp);
744 tcg_gen_mov_i32(cpu_ZF, cpu_NF);
745 tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
746 tcg_gen_xor_i32(tmp, t0_32, t1_32);
747 tcg_gen_andc_i32(cpu_VF, cpu_VF, tmp);
748 tcg_gen_extu_i32_i64(dest, cpu_NF);
750 tcg_temp_free_i32(tmp);
751 tcg_temp_free_i32(t0_32);
752 tcg_temp_free_i32(t1_32);
756 /* dest = T0 - T1; compute C, N, V and Z flags */
757 static void gen_sub_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
759 if (sf) {
760 /* 64 bit arithmetic */
761 TCGv_i64 result, flag, tmp;
763 result = tcg_temp_new_i64();
764 flag = tcg_temp_new_i64();
765 tcg_gen_sub_i64(result, t0, t1);
767 gen_set_NZ64(result);
769 tcg_gen_setcond_i64(TCG_COND_GEU, flag, t0, t1);
770 tcg_gen_extrl_i64_i32(cpu_CF, flag);
772 tcg_gen_xor_i64(flag, result, t0);
773 tmp = tcg_temp_new_i64();
774 tcg_gen_xor_i64(tmp, t0, t1);
775 tcg_gen_and_i64(flag, flag, tmp);
776 tcg_temp_free_i64(tmp);
777 tcg_gen_extrh_i64_i32(cpu_VF, flag);
778 tcg_gen_mov_i64(dest, result);
779 tcg_temp_free_i64(flag);
780 tcg_temp_free_i64(result);
781 } else {
782 /* 32 bit arithmetic */
783 TCGv_i32 t0_32 = tcg_temp_new_i32();
784 TCGv_i32 t1_32 = tcg_temp_new_i32();
785 TCGv_i32 tmp;
787 tcg_gen_extrl_i64_i32(t0_32, t0);
788 tcg_gen_extrl_i64_i32(t1_32, t1);
789 tcg_gen_sub_i32(cpu_NF, t0_32, t1_32);
790 tcg_gen_mov_i32(cpu_ZF, cpu_NF);
791 tcg_gen_setcond_i32(TCG_COND_GEU, cpu_CF, t0_32, t1_32);
792 tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
793 tmp = tcg_temp_new_i32();
794 tcg_gen_xor_i32(tmp, t0_32, t1_32);
795 tcg_temp_free_i32(t0_32);
796 tcg_temp_free_i32(t1_32);
797 tcg_gen_and_i32(cpu_VF, cpu_VF, tmp);
798 tcg_temp_free_i32(tmp);
799 tcg_gen_extu_i32_i64(dest, cpu_NF);
803 /* dest = T0 + T1 + CF; do not compute flags. */
804 static void gen_adc(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
806 TCGv_i64 flag = tcg_temp_new_i64();
807 tcg_gen_extu_i32_i64(flag, cpu_CF);
808 tcg_gen_add_i64(dest, t0, t1);
809 tcg_gen_add_i64(dest, dest, flag);
810 tcg_temp_free_i64(flag);
812 if (!sf) {
813 tcg_gen_ext32u_i64(dest, dest);
817 /* dest = T0 + T1 + CF; compute C, N, V and Z flags. */
818 static void gen_adc_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
820 if (sf) {
821 TCGv_i64 result = tcg_temp_new_i64();
822 TCGv_i64 cf_64 = tcg_temp_new_i64();
823 TCGv_i64 vf_64 = tcg_temp_new_i64();
824 TCGv_i64 tmp = tcg_temp_new_i64();
825 TCGv_i64 zero = tcg_constant_i64(0);
827 tcg_gen_extu_i32_i64(cf_64, cpu_CF);
828 tcg_gen_add2_i64(result, cf_64, t0, zero, cf_64, zero);
829 tcg_gen_add2_i64(result, cf_64, result, cf_64, t1, zero);
830 tcg_gen_extrl_i64_i32(cpu_CF, cf_64);
831 gen_set_NZ64(result);
833 tcg_gen_xor_i64(vf_64, result, t0);
834 tcg_gen_xor_i64(tmp, t0, t1);
835 tcg_gen_andc_i64(vf_64, vf_64, tmp);
836 tcg_gen_extrh_i64_i32(cpu_VF, vf_64);
838 tcg_gen_mov_i64(dest, result);
840 tcg_temp_free_i64(tmp);
841 tcg_temp_free_i64(vf_64);
842 tcg_temp_free_i64(cf_64);
843 tcg_temp_free_i64(result);
844 } else {
845 TCGv_i32 t0_32 = tcg_temp_new_i32();
846 TCGv_i32 t1_32 = tcg_temp_new_i32();
847 TCGv_i32 tmp = tcg_temp_new_i32();
848 TCGv_i32 zero = tcg_constant_i32(0);
850 tcg_gen_extrl_i64_i32(t0_32, t0);
851 tcg_gen_extrl_i64_i32(t1_32, t1);
852 tcg_gen_add2_i32(cpu_NF, cpu_CF, t0_32, zero, cpu_CF, zero);
853 tcg_gen_add2_i32(cpu_NF, cpu_CF, cpu_NF, cpu_CF, t1_32, zero);
855 tcg_gen_mov_i32(cpu_ZF, cpu_NF);
856 tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
857 tcg_gen_xor_i32(tmp, t0_32, t1_32);
858 tcg_gen_andc_i32(cpu_VF, cpu_VF, tmp);
859 tcg_gen_extu_i32_i64(dest, cpu_NF);
861 tcg_temp_free_i32(tmp);
862 tcg_temp_free_i32(t1_32);
863 tcg_temp_free_i32(t0_32);
868 * Load/Store generators
872 * Store from GPR register to memory.
874 static void do_gpr_st_memidx(DisasContext *s, TCGv_i64 source,
875 TCGv_i64 tcg_addr, MemOp memop, int memidx,
876 bool iss_valid,
877 unsigned int iss_srt,
878 bool iss_sf, bool iss_ar)
880 memop = finalize_memop(s, memop);
881 tcg_gen_qemu_st_i64(source, tcg_addr, memidx, memop);
883 if (iss_valid) {
884 uint32_t syn;
886 syn = syn_data_abort_with_iss(0,
887 (memop & MO_SIZE),
888 false,
889 iss_srt,
890 iss_sf,
891 iss_ar,
892 0, 0, 0, 0, 0, false);
893 disas_set_insn_syndrome(s, syn);
897 static void do_gpr_st(DisasContext *s, TCGv_i64 source,
898 TCGv_i64 tcg_addr, MemOp memop,
899 bool iss_valid,
900 unsigned int iss_srt,
901 bool iss_sf, bool iss_ar)
903 do_gpr_st_memidx(s, source, tcg_addr, memop, get_mem_index(s),
904 iss_valid, iss_srt, iss_sf, iss_ar);
908 * Load from memory to GPR register
910 static void do_gpr_ld_memidx(DisasContext *s, TCGv_i64 dest, TCGv_i64 tcg_addr,
911 MemOp memop, bool extend, int memidx,
912 bool iss_valid, unsigned int iss_srt,
913 bool iss_sf, bool iss_ar)
915 memop = finalize_memop(s, memop);
916 tcg_gen_qemu_ld_i64(dest, tcg_addr, memidx, memop);
918 if (extend && (memop & MO_SIGN)) {
919 g_assert((memop & MO_SIZE) <= MO_32);
920 tcg_gen_ext32u_i64(dest, dest);
923 if (iss_valid) {
924 uint32_t syn;
926 syn = syn_data_abort_with_iss(0,
927 (memop & MO_SIZE),
928 (memop & MO_SIGN) != 0,
929 iss_srt,
930 iss_sf,
931 iss_ar,
932 0, 0, 0, 0, 0, false);
933 disas_set_insn_syndrome(s, syn);
937 static void do_gpr_ld(DisasContext *s, TCGv_i64 dest, TCGv_i64 tcg_addr,
938 MemOp memop, bool extend,
939 bool iss_valid, unsigned int iss_srt,
940 bool iss_sf, bool iss_ar)
942 do_gpr_ld_memidx(s, dest, tcg_addr, memop, extend, get_mem_index(s),
943 iss_valid, iss_srt, iss_sf, iss_ar);
947 * Store from FP register to memory
949 static void do_fp_st(DisasContext *s, int srcidx, TCGv_i64 tcg_addr, int size)
951 /* This writes the bottom N bits of a 128 bit wide vector to memory */
952 TCGv_i64 tmplo = tcg_temp_new_i64();
953 MemOp mop;
955 tcg_gen_ld_i64(tmplo, cpu_env, fp_reg_offset(s, srcidx, MO_64));
957 if (size < 4) {
958 mop = finalize_memop(s, size);
959 tcg_gen_qemu_st_i64(tmplo, tcg_addr, get_mem_index(s), mop);
960 } else {
961 bool be = s->be_data == MO_BE;
962 TCGv_i64 tcg_hiaddr = tcg_temp_new_i64();
963 TCGv_i64 tmphi = tcg_temp_new_i64();
965 tcg_gen_ld_i64(tmphi, cpu_env, fp_reg_hi_offset(s, srcidx));
967 mop = s->be_data | MO_UQ;
968 tcg_gen_qemu_st_i64(be ? tmphi : tmplo, tcg_addr, get_mem_index(s),
969 mop | (s->align_mem ? MO_ALIGN_16 : 0));
970 tcg_gen_addi_i64(tcg_hiaddr, tcg_addr, 8);
971 tcg_gen_qemu_st_i64(be ? tmplo : tmphi, tcg_hiaddr,
972 get_mem_index(s), mop);
974 tcg_temp_free_i64(tcg_hiaddr);
975 tcg_temp_free_i64(tmphi);
978 tcg_temp_free_i64(tmplo);
982 * Load from memory to FP register
984 static void do_fp_ld(DisasContext *s, int destidx, TCGv_i64 tcg_addr, int size)
986 /* This always zero-extends and writes to a full 128 bit wide vector */
987 TCGv_i64 tmplo = tcg_temp_new_i64();
988 TCGv_i64 tmphi = NULL;
989 MemOp mop;
991 if (size < 4) {
992 mop = finalize_memop(s, size);
993 tcg_gen_qemu_ld_i64(tmplo, tcg_addr, get_mem_index(s), mop);
994 } else {
995 bool be = s->be_data == MO_BE;
996 TCGv_i64 tcg_hiaddr;
998 tmphi = tcg_temp_new_i64();
999 tcg_hiaddr = tcg_temp_new_i64();
1001 mop = s->be_data | MO_UQ;
1002 tcg_gen_qemu_ld_i64(be ? tmphi : tmplo, tcg_addr, get_mem_index(s),
1003 mop | (s->align_mem ? MO_ALIGN_16 : 0));
1004 tcg_gen_addi_i64(tcg_hiaddr, tcg_addr, 8);
1005 tcg_gen_qemu_ld_i64(be ? tmplo : tmphi, tcg_hiaddr,
1006 get_mem_index(s), mop);
1007 tcg_temp_free_i64(tcg_hiaddr);
1010 tcg_gen_st_i64(tmplo, cpu_env, fp_reg_offset(s, destidx, MO_64));
1011 tcg_temp_free_i64(tmplo);
1013 if (tmphi) {
1014 tcg_gen_st_i64(tmphi, cpu_env, fp_reg_hi_offset(s, destidx));
1015 tcg_temp_free_i64(tmphi);
1017 clear_vec_high(s, tmphi != NULL, destidx);
1021 * Vector load/store helpers.
1023 * The principal difference between this and a FP load is that we don't
1024 * zero extend as we are filling a partial chunk of the vector register.
1025 * These functions don't support 128 bit loads/stores, which would be
1026 * normal load/store operations.
1028 * The _i32 versions are useful when operating on 32 bit quantities
1029 * (eg for floating point single or using Neon helper functions).
1032 /* Get value of an element within a vector register */
1033 static void read_vec_element(DisasContext *s, TCGv_i64 tcg_dest, int srcidx,
1034 int element, MemOp memop)
1036 int vect_off = vec_reg_offset(s, srcidx, element, memop & MO_SIZE);
1037 switch ((unsigned)memop) {
1038 case MO_8:
1039 tcg_gen_ld8u_i64(tcg_dest, cpu_env, vect_off);
1040 break;
1041 case MO_16:
1042 tcg_gen_ld16u_i64(tcg_dest, cpu_env, vect_off);
1043 break;
1044 case MO_32:
1045 tcg_gen_ld32u_i64(tcg_dest, cpu_env, vect_off);
1046 break;
1047 case MO_8|MO_SIGN:
1048 tcg_gen_ld8s_i64(tcg_dest, cpu_env, vect_off);
1049 break;
1050 case MO_16|MO_SIGN:
1051 tcg_gen_ld16s_i64(tcg_dest, cpu_env, vect_off);
1052 break;
1053 case MO_32|MO_SIGN:
1054 tcg_gen_ld32s_i64(tcg_dest, cpu_env, vect_off);
1055 break;
1056 case MO_64:
1057 case MO_64|MO_SIGN:
1058 tcg_gen_ld_i64(tcg_dest, cpu_env, vect_off);
1059 break;
1060 default:
1061 g_assert_not_reached();
1065 static void read_vec_element_i32(DisasContext *s, TCGv_i32 tcg_dest, int srcidx,
1066 int element, MemOp memop)
1068 int vect_off = vec_reg_offset(s, srcidx, element, memop & MO_SIZE);
1069 switch (memop) {
1070 case MO_8:
1071 tcg_gen_ld8u_i32(tcg_dest, cpu_env, vect_off);
1072 break;
1073 case MO_16:
1074 tcg_gen_ld16u_i32(tcg_dest, cpu_env, vect_off);
1075 break;
1076 case MO_8|MO_SIGN:
1077 tcg_gen_ld8s_i32(tcg_dest, cpu_env, vect_off);
1078 break;
1079 case MO_16|MO_SIGN:
1080 tcg_gen_ld16s_i32(tcg_dest, cpu_env, vect_off);
1081 break;
1082 case MO_32:
1083 case MO_32|MO_SIGN:
1084 tcg_gen_ld_i32(tcg_dest, cpu_env, vect_off);
1085 break;
1086 default:
1087 g_assert_not_reached();
1091 /* Set value of an element within a vector register */
1092 static void write_vec_element(DisasContext *s, TCGv_i64 tcg_src, int destidx,
1093 int element, MemOp memop)
1095 int vect_off = vec_reg_offset(s, destidx, element, memop & MO_SIZE);
1096 switch (memop) {
1097 case MO_8:
1098 tcg_gen_st8_i64(tcg_src, cpu_env, vect_off);
1099 break;
1100 case MO_16:
1101 tcg_gen_st16_i64(tcg_src, cpu_env, vect_off);
1102 break;
1103 case MO_32:
1104 tcg_gen_st32_i64(tcg_src, cpu_env, vect_off);
1105 break;
1106 case MO_64:
1107 tcg_gen_st_i64(tcg_src, cpu_env, vect_off);
1108 break;
1109 default:
1110 g_assert_not_reached();
1114 static void write_vec_element_i32(DisasContext *s, TCGv_i32 tcg_src,
1115 int destidx, int element, MemOp memop)
1117 int vect_off = vec_reg_offset(s, destidx, element, memop & MO_SIZE);
1118 switch (memop) {
1119 case MO_8:
1120 tcg_gen_st8_i32(tcg_src, cpu_env, vect_off);
1121 break;
1122 case MO_16:
1123 tcg_gen_st16_i32(tcg_src, cpu_env, vect_off);
1124 break;
1125 case MO_32:
1126 tcg_gen_st_i32(tcg_src, cpu_env, vect_off);
1127 break;
1128 default:
1129 g_assert_not_reached();
1133 /* Store from vector register to memory */
1134 static void do_vec_st(DisasContext *s, int srcidx, int element,
1135 TCGv_i64 tcg_addr, MemOp mop)
1137 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
1139 read_vec_element(s, tcg_tmp, srcidx, element, mop & MO_SIZE);
1140 tcg_gen_qemu_st_i64(tcg_tmp, tcg_addr, get_mem_index(s), mop);
1142 tcg_temp_free_i64(tcg_tmp);
1145 /* Load from memory to vector register */
1146 static void do_vec_ld(DisasContext *s, int destidx, int element,
1147 TCGv_i64 tcg_addr, MemOp mop)
1149 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
1151 tcg_gen_qemu_ld_i64(tcg_tmp, tcg_addr, get_mem_index(s), mop);
1152 write_vec_element(s, tcg_tmp, destidx, element, mop & MO_SIZE);
1154 tcg_temp_free_i64(tcg_tmp);
1157 /* Check that FP/Neon access is enabled. If it is, return
1158 * true. If not, emit code to generate an appropriate exception,
1159 * and return false; the caller should not emit any code for
1160 * the instruction. Note that this check must happen after all
1161 * unallocated-encoding checks (otherwise the syndrome information
1162 * for the resulting exception will be incorrect).
1164 static bool fp_access_check_only(DisasContext *s)
1166 if (s->fp_excp_el) {
1167 assert(!s->fp_access_checked);
1168 s->fp_access_checked = true;
1170 gen_exception_insn_el(s, 0, EXCP_UDEF,
1171 syn_fp_access_trap(1, 0xe, false, 0),
1172 s->fp_excp_el);
1173 return false;
1175 s->fp_access_checked = true;
1176 return true;
1179 static bool fp_access_check(DisasContext *s)
1181 if (!fp_access_check_only(s)) {
1182 return false;
1184 if (s->sme_trap_nonstreaming && s->is_nonstreaming) {
1185 gen_exception_insn(s, 0, EXCP_UDEF,
1186 syn_smetrap(SME_ET_Streaming, false));
1187 return false;
1189 return true;
1193 * Check that SVE access is enabled. If it is, return true.
1194 * If not, emit code to generate an appropriate exception and return false.
1195 * This function corresponds to CheckSVEEnabled().
1197 bool sve_access_check(DisasContext *s)
1199 if (s->pstate_sm || !dc_isar_feature(aa64_sve, s)) {
1200 assert(dc_isar_feature(aa64_sme, s));
1201 if (!sme_sm_enabled_check(s)) {
1202 goto fail_exit;
1204 } else if (s->sve_excp_el) {
1205 gen_exception_insn_el(s, 0, EXCP_UDEF,
1206 syn_sve_access_trap(), s->sve_excp_el);
1207 goto fail_exit;
1209 s->sve_access_checked = true;
1210 return fp_access_check(s);
1212 fail_exit:
1213 /* Assert that we only raise one exception per instruction. */
1214 assert(!s->sve_access_checked);
1215 s->sve_access_checked = true;
1216 return false;
1220 * Check that SME access is enabled, raise an exception if not.
1221 * Note that this function corresponds to CheckSMEAccess and is
1222 * only used directly for cpregs.
1224 static bool sme_access_check(DisasContext *s)
1226 if (s->sme_excp_el) {
1227 gen_exception_insn_el(s, 0, EXCP_UDEF,
1228 syn_smetrap(SME_ET_AccessTrap, false),
1229 s->sme_excp_el);
1230 return false;
1232 return true;
1235 /* This function corresponds to CheckSMEEnabled. */
1236 bool sme_enabled_check(DisasContext *s)
1239 * Note that unlike sve_excp_el, we have not constrained sme_excp_el
1240 * to be zero when fp_excp_el has priority. This is because we need
1241 * sme_excp_el by itself for cpregs access checks.
1243 if (!s->fp_excp_el || s->sme_excp_el < s->fp_excp_el) {
1244 s->fp_access_checked = true;
1245 return sme_access_check(s);
1247 return fp_access_check_only(s);
1250 /* Common subroutine for CheckSMEAnd*Enabled. */
1251 bool sme_enabled_check_with_svcr(DisasContext *s, unsigned req)
1253 if (!sme_enabled_check(s)) {
1254 return false;
1256 if (FIELD_EX64(req, SVCR, SM) && !s->pstate_sm) {
1257 gen_exception_insn(s, 0, EXCP_UDEF,
1258 syn_smetrap(SME_ET_NotStreaming, false));
1259 return false;
1261 if (FIELD_EX64(req, SVCR, ZA) && !s->pstate_za) {
1262 gen_exception_insn(s, 0, EXCP_UDEF,
1263 syn_smetrap(SME_ET_InactiveZA, false));
1264 return false;
1266 return true;
1270 * This utility function is for doing register extension with an
1271 * optional shift. You will likely want to pass a temporary for the
1272 * destination register. See DecodeRegExtend() in the ARM ARM.
1274 static void ext_and_shift_reg(TCGv_i64 tcg_out, TCGv_i64 tcg_in,
1275 int option, unsigned int shift)
1277 int extsize = extract32(option, 0, 2);
1278 bool is_signed = extract32(option, 2, 1);
1280 if (is_signed) {
1281 switch (extsize) {
1282 case 0:
1283 tcg_gen_ext8s_i64(tcg_out, tcg_in);
1284 break;
1285 case 1:
1286 tcg_gen_ext16s_i64(tcg_out, tcg_in);
1287 break;
1288 case 2:
1289 tcg_gen_ext32s_i64(tcg_out, tcg_in);
1290 break;
1291 case 3:
1292 tcg_gen_mov_i64(tcg_out, tcg_in);
1293 break;
1295 } else {
1296 switch (extsize) {
1297 case 0:
1298 tcg_gen_ext8u_i64(tcg_out, tcg_in);
1299 break;
1300 case 1:
1301 tcg_gen_ext16u_i64(tcg_out, tcg_in);
1302 break;
1303 case 2:
1304 tcg_gen_ext32u_i64(tcg_out, tcg_in);
1305 break;
1306 case 3:
1307 tcg_gen_mov_i64(tcg_out, tcg_in);
1308 break;
1312 if (shift) {
1313 tcg_gen_shli_i64(tcg_out, tcg_out, shift);
1317 static inline void gen_check_sp_alignment(DisasContext *s)
1319 /* The AArch64 architecture mandates that (if enabled via PSTATE
1320 * or SCTLR bits) there is a check that SP is 16-aligned on every
1321 * SP-relative load or store (with an exception generated if it is not).
1322 * In line with general QEMU practice regarding misaligned accesses,
1323 * we omit these checks for the sake of guest program performance.
1324 * This function is provided as a hook so we can more easily add these
1325 * checks in future (possibly as a "favour catching guest program bugs
1326 * over speed" user selectable option).
1331 * This provides a simple table based table lookup decoder. It is
1332 * intended to be used when the relevant bits for decode are too
1333 * awkwardly placed and switch/if based logic would be confusing and
1334 * deeply nested. Since it's a linear search through the table, tables
1335 * should be kept small.
1337 * It returns the first handler where insn & mask == pattern, or
1338 * NULL if there is no match.
1339 * The table is terminated by an empty mask (i.e. 0)
1341 static inline AArch64DecodeFn *lookup_disas_fn(const AArch64DecodeTable *table,
1342 uint32_t insn)
1344 const AArch64DecodeTable *tptr = table;
1346 while (tptr->mask) {
1347 if ((insn & tptr->mask) == tptr->pattern) {
1348 return tptr->disas_fn;
1350 tptr++;
1352 return NULL;
1356 * The instruction disassembly implemented here matches
1357 * the instruction encoding classifications in chapter C4
1358 * of the ARM Architecture Reference Manual (DDI0487B_a);
1359 * classification names and decode diagrams here should generally
1360 * match up with those in the manual.
1363 /* Unconditional branch (immediate)
1364 * 31 30 26 25 0
1365 * +----+-----------+-------------------------------------+
1366 * | op | 0 0 1 0 1 | imm26 |
1367 * +----+-----------+-------------------------------------+
1369 static void disas_uncond_b_imm(DisasContext *s, uint32_t insn)
1371 int64_t diff = sextract32(insn, 0, 26) * 4;
1373 if (insn & (1U << 31)) {
1374 /* BL Branch with link */
1375 gen_pc_plus_diff(s, cpu_reg(s, 30), curr_insn_len(s));
1378 /* B Branch / BL Branch with link */
1379 reset_btype(s);
1380 gen_goto_tb(s, 0, diff);
1383 /* Compare and branch (immediate)
1384 * 31 30 25 24 23 5 4 0
1385 * +----+-------------+----+---------------------+--------+
1386 * | sf | 0 1 1 0 1 0 | op | imm19 | Rt |
1387 * +----+-------------+----+---------------------+--------+
1389 static void disas_comp_b_imm(DisasContext *s, uint32_t insn)
1391 unsigned int sf, op, rt;
1392 int64_t diff;
1393 DisasLabel match;
1394 TCGv_i64 tcg_cmp;
1396 sf = extract32(insn, 31, 1);
1397 op = extract32(insn, 24, 1); /* 0: CBZ; 1: CBNZ */
1398 rt = extract32(insn, 0, 5);
1399 diff = sextract32(insn, 5, 19) * 4;
1401 tcg_cmp = read_cpu_reg(s, rt, sf);
1402 reset_btype(s);
1404 match = gen_disas_label(s);
1405 tcg_gen_brcondi_i64(op ? TCG_COND_NE : TCG_COND_EQ,
1406 tcg_cmp, 0, match.label);
1407 gen_goto_tb(s, 0, 4);
1408 set_disas_label(s, match);
1409 gen_goto_tb(s, 1, diff);
1412 /* Test and branch (immediate)
1413 * 31 30 25 24 23 19 18 5 4 0
1414 * +----+-------------+----+-------+-------------+------+
1415 * | b5 | 0 1 1 0 1 1 | op | b40 | imm14 | Rt |
1416 * +----+-------------+----+-------+-------------+------+
1418 static void disas_test_b_imm(DisasContext *s, uint32_t insn)
1420 unsigned int bit_pos, op, rt;
1421 int64_t diff;
1422 DisasLabel match;
1423 TCGv_i64 tcg_cmp;
1425 bit_pos = (extract32(insn, 31, 1) << 5) | extract32(insn, 19, 5);
1426 op = extract32(insn, 24, 1); /* 0: TBZ; 1: TBNZ */
1427 diff = sextract32(insn, 5, 14) * 4;
1428 rt = extract32(insn, 0, 5);
1430 tcg_cmp = tcg_temp_new_i64();
1431 tcg_gen_andi_i64(tcg_cmp, cpu_reg(s, rt), (1ULL << bit_pos));
1433 reset_btype(s);
1435 match = gen_disas_label(s);
1436 tcg_gen_brcondi_i64(op ? TCG_COND_NE : TCG_COND_EQ,
1437 tcg_cmp, 0, match.label);
1438 tcg_temp_free_i64(tcg_cmp);
1439 gen_goto_tb(s, 0, 4);
1440 set_disas_label(s, match);
1441 gen_goto_tb(s, 1, diff);
1444 /* Conditional branch (immediate)
1445 * 31 25 24 23 5 4 3 0
1446 * +---------------+----+---------------------+----+------+
1447 * | 0 1 0 1 0 1 0 | o1 | imm19 | o0 | cond |
1448 * +---------------+----+---------------------+----+------+
1450 static void disas_cond_b_imm(DisasContext *s, uint32_t insn)
1452 unsigned int cond;
1453 int64_t diff;
1455 if ((insn & (1 << 4)) || (insn & (1 << 24))) {
1456 unallocated_encoding(s);
1457 return;
1459 diff = sextract32(insn, 5, 19) * 4;
1460 cond = extract32(insn, 0, 4);
1462 reset_btype(s);
1463 if (cond < 0x0e) {
1464 /* genuinely conditional branches */
1465 DisasLabel match = gen_disas_label(s);
1466 arm_gen_test_cc(cond, match.label);
1467 gen_goto_tb(s, 0, 4);
1468 set_disas_label(s, match);
1469 gen_goto_tb(s, 1, diff);
1470 } else {
1471 /* 0xe and 0xf are both "always" conditions */
1472 gen_goto_tb(s, 0, diff);
1476 /* HINT instruction group, including various allocated HINTs */
1477 static void handle_hint(DisasContext *s, uint32_t insn,
1478 unsigned int op1, unsigned int op2, unsigned int crm)
1480 unsigned int selector = crm << 3 | op2;
1482 if (op1 != 3) {
1483 unallocated_encoding(s);
1484 return;
1487 switch (selector) {
1488 case 0b00000: /* NOP */
1489 break;
1490 case 0b00011: /* WFI */
1491 s->base.is_jmp = DISAS_WFI;
1492 break;
1493 case 0b00001: /* YIELD */
1494 /* When running in MTTCG we don't generate jumps to the yield and
1495 * WFE helpers as it won't affect the scheduling of other vCPUs.
1496 * If we wanted to more completely model WFE/SEV so we don't busy
1497 * spin unnecessarily we would need to do something more involved.
1499 if (!(tb_cflags(s->base.tb) & CF_PARALLEL)) {
1500 s->base.is_jmp = DISAS_YIELD;
1502 break;
1503 case 0b00010: /* WFE */
1504 if (!(tb_cflags(s->base.tb) & CF_PARALLEL)) {
1505 s->base.is_jmp = DISAS_WFE;
1507 break;
1508 case 0b00100: /* SEV */
1509 case 0b00101: /* SEVL */
1510 case 0b00110: /* DGH */
1511 /* we treat all as NOP at least for now */
1512 break;
1513 case 0b00111: /* XPACLRI */
1514 if (s->pauth_active) {
1515 gen_helper_xpaci(cpu_X[30], cpu_env, cpu_X[30]);
1517 break;
1518 case 0b01000: /* PACIA1716 */
1519 if (s->pauth_active) {
1520 gen_helper_pacia(cpu_X[17], cpu_env, cpu_X[17], cpu_X[16]);
1522 break;
1523 case 0b01010: /* PACIB1716 */
1524 if (s->pauth_active) {
1525 gen_helper_pacib(cpu_X[17], cpu_env, cpu_X[17], cpu_X[16]);
1527 break;
1528 case 0b01100: /* AUTIA1716 */
1529 if (s->pauth_active) {
1530 gen_helper_autia(cpu_X[17], cpu_env, cpu_X[17], cpu_X[16]);
1532 break;
1533 case 0b01110: /* AUTIB1716 */
1534 if (s->pauth_active) {
1535 gen_helper_autib(cpu_X[17], cpu_env, cpu_X[17], cpu_X[16]);
1537 break;
1538 case 0b10000: /* ESB */
1539 /* Without RAS, we must implement this as NOP. */
1540 if (dc_isar_feature(aa64_ras, s)) {
1542 * QEMU does not have a source of physical SErrors,
1543 * so we are only concerned with virtual SErrors.
1544 * The pseudocode in the ARM for this case is
1545 * if PSTATE.EL IN {EL0, EL1} && EL2Enabled() then
1546 * AArch64.vESBOperation();
1547 * Most of the condition can be evaluated at translation time.
1548 * Test for EL2 present, and defer test for SEL2 to runtime.
1550 if (s->current_el <= 1 && arm_dc_feature(s, ARM_FEATURE_EL2)) {
1551 gen_helper_vesb(cpu_env);
1554 break;
1555 case 0b11000: /* PACIAZ */
1556 if (s->pauth_active) {
1557 gen_helper_pacia(cpu_X[30], cpu_env, cpu_X[30],
1558 new_tmp_a64_zero(s));
1560 break;
1561 case 0b11001: /* PACIASP */
1562 if (s->pauth_active) {
1563 gen_helper_pacia(cpu_X[30], cpu_env, cpu_X[30], cpu_X[31]);
1565 break;
1566 case 0b11010: /* PACIBZ */
1567 if (s->pauth_active) {
1568 gen_helper_pacib(cpu_X[30], cpu_env, cpu_X[30],
1569 new_tmp_a64_zero(s));
1571 break;
1572 case 0b11011: /* PACIBSP */
1573 if (s->pauth_active) {
1574 gen_helper_pacib(cpu_X[30], cpu_env, cpu_X[30], cpu_X[31]);
1576 break;
1577 case 0b11100: /* AUTIAZ */
1578 if (s->pauth_active) {
1579 gen_helper_autia(cpu_X[30], cpu_env, cpu_X[30],
1580 new_tmp_a64_zero(s));
1582 break;
1583 case 0b11101: /* AUTIASP */
1584 if (s->pauth_active) {
1585 gen_helper_autia(cpu_X[30], cpu_env, cpu_X[30], cpu_X[31]);
1587 break;
1588 case 0b11110: /* AUTIBZ */
1589 if (s->pauth_active) {
1590 gen_helper_autib(cpu_X[30], cpu_env, cpu_X[30],
1591 new_tmp_a64_zero(s));
1593 break;
1594 case 0b11111: /* AUTIBSP */
1595 if (s->pauth_active) {
1596 gen_helper_autib(cpu_X[30], cpu_env, cpu_X[30], cpu_X[31]);
1598 break;
1599 default:
1600 /* default specified as NOP equivalent */
1601 break;
1605 static void gen_clrex(DisasContext *s, uint32_t insn)
1607 tcg_gen_movi_i64(cpu_exclusive_addr, -1);
1610 /* CLREX, DSB, DMB, ISB */
1611 static void handle_sync(DisasContext *s, uint32_t insn,
1612 unsigned int op1, unsigned int op2, unsigned int crm)
1614 TCGBar bar;
1616 if (op1 != 3) {
1617 unallocated_encoding(s);
1618 return;
1621 switch (op2) {
1622 case 2: /* CLREX */
1623 gen_clrex(s, insn);
1624 return;
1625 case 4: /* DSB */
1626 case 5: /* DMB */
1627 switch (crm & 3) {
1628 case 1: /* MBReqTypes_Reads */
1629 bar = TCG_BAR_SC | TCG_MO_LD_LD | TCG_MO_LD_ST;
1630 break;
1631 case 2: /* MBReqTypes_Writes */
1632 bar = TCG_BAR_SC | TCG_MO_ST_ST;
1633 break;
1634 default: /* MBReqTypes_All */
1635 bar = TCG_BAR_SC | TCG_MO_ALL;
1636 break;
1638 tcg_gen_mb(bar);
1639 return;
1640 case 6: /* ISB */
1641 /* We need to break the TB after this insn to execute
1642 * a self-modified code correctly and also to take
1643 * any pending interrupts immediately.
1645 reset_btype(s);
1646 gen_goto_tb(s, 0, 4);
1647 return;
1649 case 7: /* SB */
1650 if (crm != 0 || !dc_isar_feature(aa64_sb, s)) {
1651 goto do_unallocated;
1654 * TODO: There is no speculation barrier opcode for TCG;
1655 * MB and end the TB instead.
1657 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_SC);
1658 gen_goto_tb(s, 0, 4);
1659 return;
1661 default:
1662 do_unallocated:
1663 unallocated_encoding(s);
1664 return;
1668 static void gen_xaflag(void)
1670 TCGv_i32 z = tcg_temp_new_i32();
1672 tcg_gen_setcondi_i32(TCG_COND_EQ, z, cpu_ZF, 0);
1675 * (!C & !Z) << 31
1676 * (!(C | Z)) << 31
1677 * ~((C | Z) << 31)
1678 * ~-(C | Z)
1679 * (C | Z) - 1
1681 tcg_gen_or_i32(cpu_NF, cpu_CF, z);
1682 tcg_gen_subi_i32(cpu_NF, cpu_NF, 1);
1684 /* !(Z & C) */
1685 tcg_gen_and_i32(cpu_ZF, z, cpu_CF);
1686 tcg_gen_xori_i32(cpu_ZF, cpu_ZF, 1);
1688 /* (!C & Z) << 31 -> -(Z & ~C) */
1689 tcg_gen_andc_i32(cpu_VF, z, cpu_CF);
1690 tcg_gen_neg_i32(cpu_VF, cpu_VF);
1692 /* C | Z */
1693 tcg_gen_or_i32(cpu_CF, cpu_CF, z);
1695 tcg_temp_free_i32(z);
1698 static void gen_axflag(void)
1700 tcg_gen_sari_i32(cpu_VF, cpu_VF, 31); /* V ? -1 : 0 */
1701 tcg_gen_andc_i32(cpu_CF, cpu_CF, cpu_VF); /* C & !V */
1703 /* !(Z | V) -> !(!ZF | V) -> ZF & !V -> ZF & ~VF */
1704 tcg_gen_andc_i32(cpu_ZF, cpu_ZF, cpu_VF);
1706 tcg_gen_movi_i32(cpu_NF, 0);
1707 tcg_gen_movi_i32(cpu_VF, 0);
1710 /* MSR (immediate) - move immediate to processor state field */
1711 static void handle_msr_i(DisasContext *s, uint32_t insn,
1712 unsigned int op1, unsigned int op2, unsigned int crm)
1714 int op = op1 << 3 | op2;
1716 /* End the TB by default, chaining is ok. */
1717 s->base.is_jmp = DISAS_TOO_MANY;
1719 switch (op) {
1720 case 0x00: /* CFINV */
1721 if (crm != 0 || !dc_isar_feature(aa64_condm_4, s)) {
1722 goto do_unallocated;
1724 tcg_gen_xori_i32(cpu_CF, cpu_CF, 1);
1725 s->base.is_jmp = DISAS_NEXT;
1726 break;
1728 case 0x01: /* XAFlag */
1729 if (crm != 0 || !dc_isar_feature(aa64_condm_5, s)) {
1730 goto do_unallocated;
1732 gen_xaflag();
1733 s->base.is_jmp = DISAS_NEXT;
1734 break;
1736 case 0x02: /* AXFlag */
1737 if (crm != 0 || !dc_isar_feature(aa64_condm_5, s)) {
1738 goto do_unallocated;
1740 gen_axflag();
1741 s->base.is_jmp = DISAS_NEXT;
1742 break;
1744 case 0x03: /* UAO */
1745 if (!dc_isar_feature(aa64_uao, s) || s->current_el == 0) {
1746 goto do_unallocated;
1748 if (crm & 1) {
1749 set_pstate_bits(PSTATE_UAO);
1750 } else {
1751 clear_pstate_bits(PSTATE_UAO);
1753 gen_rebuild_hflags(s);
1754 break;
1756 case 0x04: /* PAN */
1757 if (!dc_isar_feature(aa64_pan, s) || s->current_el == 0) {
1758 goto do_unallocated;
1760 if (crm & 1) {
1761 set_pstate_bits(PSTATE_PAN);
1762 } else {
1763 clear_pstate_bits(PSTATE_PAN);
1765 gen_rebuild_hflags(s);
1766 break;
1768 case 0x05: /* SPSel */
1769 if (s->current_el == 0) {
1770 goto do_unallocated;
1772 gen_helper_msr_i_spsel(cpu_env, tcg_constant_i32(crm & PSTATE_SP));
1773 break;
1775 case 0x19: /* SSBS */
1776 if (!dc_isar_feature(aa64_ssbs, s)) {
1777 goto do_unallocated;
1779 if (crm & 1) {
1780 set_pstate_bits(PSTATE_SSBS);
1781 } else {
1782 clear_pstate_bits(PSTATE_SSBS);
1784 /* Don't need to rebuild hflags since SSBS is a nop */
1785 break;
1787 case 0x1a: /* DIT */
1788 if (!dc_isar_feature(aa64_dit, s)) {
1789 goto do_unallocated;
1791 if (crm & 1) {
1792 set_pstate_bits(PSTATE_DIT);
1793 } else {
1794 clear_pstate_bits(PSTATE_DIT);
1796 /* There's no need to rebuild hflags because DIT is a nop */
1797 break;
1799 case 0x1e: /* DAIFSet */
1800 gen_helper_msr_i_daifset(cpu_env, tcg_constant_i32(crm));
1801 break;
1803 case 0x1f: /* DAIFClear */
1804 gen_helper_msr_i_daifclear(cpu_env, tcg_constant_i32(crm));
1805 /* For DAIFClear, exit the cpu loop to re-evaluate pending IRQs. */
1806 s->base.is_jmp = DISAS_UPDATE_EXIT;
1807 break;
1809 case 0x1c: /* TCO */
1810 if (dc_isar_feature(aa64_mte, s)) {
1811 /* Full MTE is enabled -- set the TCO bit as directed. */
1812 if (crm & 1) {
1813 set_pstate_bits(PSTATE_TCO);
1814 } else {
1815 clear_pstate_bits(PSTATE_TCO);
1817 gen_rebuild_hflags(s);
1818 /* Many factors, including TCO, go into MTE_ACTIVE. */
1819 s->base.is_jmp = DISAS_UPDATE_NOCHAIN;
1820 } else if (dc_isar_feature(aa64_mte_insn_reg, s)) {
1821 /* Only "instructions accessible at EL0" -- PSTATE.TCO is WI. */
1822 s->base.is_jmp = DISAS_NEXT;
1823 } else {
1824 goto do_unallocated;
1826 break;
1828 case 0x1b: /* SVCR* */
1829 if (!dc_isar_feature(aa64_sme, s) || crm < 2 || crm > 7) {
1830 goto do_unallocated;
1832 if (sme_access_check(s)) {
1833 int old = s->pstate_sm | (s->pstate_za << 1);
1834 int new = (crm & 1) * 3;
1835 int msk = (crm >> 1) & 3;
1837 if ((old ^ new) & msk) {
1838 /* At least one bit changes. */
1839 gen_helper_set_svcr(cpu_env, tcg_constant_i32(new),
1840 tcg_constant_i32(msk));
1841 } else {
1842 s->base.is_jmp = DISAS_NEXT;
1845 break;
1847 default:
1848 do_unallocated:
1849 unallocated_encoding(s);
1850 return;
1854 static void gen_get_nzcv(TCGv_i64 tcg_rt)
1856 TCGv_i32 tmp = tcg_temp_new_i32();
1857 TCGv_i32 nzcv = tcg_temp_new_i32();
1859 /* build bit 31, N */
1860 tcg_gen_andi_i32(nzcv, cpu_NF, (1U << 31));
1861 /* build bit 30, Z */
1862 tcg_gen_setcondi_i32(TCG_COND_EQ, tmp, cpu_ZF, 0);
1863 tcg_gen_deposit_i32(nzcv, nzcv, tmp, 30, 1);
1864 /* build bit 29, C */
1865 tcg_gen_deposit_i32(nzcv, nzcv, cpu_CF, 29, 1);
1866 /* build bit 28, V */
1867 tcg_gen_shri_i32(tmp, cpu_VF, 31);
1868 tcg_gen_deposit_i32(nzcv, nzcv, tmp, 28, 1);
1869 /* generate result */
1870 tcg_gen_extu_i32_i64(tcg_rt, nzcv);
1872 tcg_temp_free_i32(nzcv);
1873 tcg_temp_free_i32(tmp);
1876 static void gen_set_nzcv(TCGv_i64 tcg_rt)
1878 TCGv_i32 nzcv = tcg_temp_new_i32();
1880 /* take NZCV from R[t] */
1881 tcg_gen_extrl_i64_i32(nzcv, tcg_rt);
1883 /* bit 31, N */
1884 tcg_gen_andi_i32(cpu_NF, nzcv, (1U << 31));
1885 /* bit 30, Z */
1886 tcg_gen_andi_i32(cpu_ZF, nzcv, (1 << 30));
1887 tcg_gen_setcondi_i32(TCG_COND_EQ, cpu_ZF, cpu_ZF, 0);
1888 /* bit 29, C */
1889 tcg_gen_andi_i32(cpu_CF, nzcv, (1 << 29));
1890 tcg_gen_shri_i32(cpu_CF, cpu_CF, 29);
1891 /* bit 28, V */
1892 tcg_gen_andi_i32(cpu_VF, nzcv, (1 << 28));
1893 tcg_gen_shli_i32(cpu_VF, cpu_VF, 3);
1894 tcg_temp_free_i32(nzcv);
1897 static void gen_sysreg_undef(DisasContext *s, bool isread,
1898 uint8_t op0, uint8_t op1, uint8_t op2,
1899 uint8_t crn, uint8_t crm, uint8_t rt)
1902 * Generate code to emit an UNDEF with correct syndrome
1903 * information for a failed system register access.
1904 * This is EC_UNCATEGORIZED (ie a standard UNDEF) in most cases,
1905 * but if FEAT_IDST is implemented then read accesses to registers
1906 * in the feature ID space are reported with the EC_SYSTEMREGISTERTRAP
1907 * syndrome.
1909 uint32_t syndrome;
1911 if (isread && dc_isar_feature(aa64_ids, s) &&
1912 arm_cpreg_encoding_in_idspace(op0, op1, op2, crn, crm)) {
1913 syndrome = syn_aa64_sysregtrap(op0, op1, op2, crn, crm, rt, isread);
1914 } else {
1915 syndrome = syn_uncategorized();
1917 gen_exception_insn(s, 0, EXCP_UDEF, syndrome);
1920 /* MRS - move from system register
1921 * MSR (register) - move to system register
1922 * SYS
1923 * SYSL
1924 * These are all essentially the same insn in 'read' and 'write'
1925 * versions, with varying op0 fields.
1927 static void handle_sys(DisasContext *s, uint32_t insn, bool isread,
1928 unsigned int op0, unsigned int op1, unsigned int op2,
1929 unsigned int crn, unsigned int crm, unsigned int rt)
1931 uint32_t key = ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP,
1932 crn, crm, op0, op1, op2);
1933 const ARMCPRegInfo *ri = get_arm_cp_reginfo(s->cp_regs, key);
1934 TCGv_ptr tcg_ri = NULL;
1935 TCGv_i64 tcg_rt;
1937 if (!ri) {
1938 /* Unknown register; this might be a guest error or a QEMU
1939 * unimplemented feature.
1941 qemu_log_mask(LOG_UNIMP, "%s access to unsupported AArch64 "
1942 "system register op0:%d op1:%d crn:%d crm:%d op2:%d\n",
1943 isread ? "read" : "write", op0, op1, crn, crm, op2);
1944 gen_sysreg_undef(s, isread, op0, op1, op2, crn, crm, rt);
1945 return;
1948 /* Check access permissions */
1949 if (!cp_access_ok(s->current_el, ri, isread)) {
1950 gen_sysreg_undef(s, isread, op0, op1, op2, crn, crm, rt);
1951 return;
1954 if (ri->accessfn || (ri->fgt && s->fgt_active)) {
1955 /* Emit code to perform further access permissions checks at
1956 * runtime; this may result in an exception.
1958 uint32_t syndrome;
1960 syndrome = syn_aa64_sysregtrap(op0, op1, op2, crn, crm, rt, isread);
1961 gen_a64_update_pc(s, 0);
1962 tcg_ri = tcg_temp_new_ptr();
1963 gen_helper_access_check_cp_reg(tcg_ri, cpu_env,
1964 tcg_constant_i32(key),
1965 tcg_constant_i32(syndrome),
1966 tcg_constant_i32(isread));
1967 } else if (ri->type & ARM_CP_RAISES_EXC) {
1969 * The readfn or writefn might raise an exception;
1970 * synchronize the CPU state in case it does.
1972 gen_a64_update_pc(s, 0);
1975 /* Handle special cases first */
1976 switch (ri->type & ARM_CP_SPECIAL_MASK) {
1977 case 0:
1978 break;
1979 case ARM_CP_NOP:
1980 goto exit;
1981 case ARM_CP_NZCV:
1982 tcg_rt = cpu_reg(s, rt);
1983 if (isread) {
1984 gen_get_nzcv(tcg_rt);
1985 } else {
1986 gen_set_nzcv(tcg_rt);
1988 goto exit;
1989 case ARM_CP_CURRENTEL:
1990 /* Reads as current EL value from pstate, which is
1991 * guaranteed to be constant by the tb flags.
1993 tcg_rt = cpu_reg(s, rt);
1994 tcg_gen_movi_i64(tcg_rt, s->current_el << 2);
1995 goto exit;
1996 case ARM_CP_DC_ZVA:
1997 /* Writes clear the aligned block of memory which rt points into. */
1998 if (s->mte_active[0]) {
1999 int desc = 0;
2001 desc = FIELD_DP32(desc, MTEDESC, MIDX, get_mem_index(s));
2002 desc = FIELD_DP32(desc, MTEDESC, TBI, s->tbid);
2003 desc = FIELD_DP32(desc, MTEDESC, TCMA, s->tcma);
2005 tcg_rt = new_tmp_a64(s);
2006 gen_helper_mte_check_zva(tcg_rt, cpu_env,
2007 tcg_constant_i32(desc), cpu_reg(s, rt));
2008 } else {
2009 tcg_rt = clean_data_tbi(s, cpu_reg(s, rt));
2011 gen_helper_dc_zva(cpu_env, tcg_rt);
2012 goto exit;
2013 case ARM_CP_DC_GVA:
2015 TCGv_i64 clean_addr, tag;
2018 * DC_GVA, like DC_ZVA, requires that we supply the original
2019 * pointer for an invalid page. Probe that address first.
2021 tcg_rt = cpu_reg(s, rt);
2022 clean_addr = clean_data_tbi(s, tcg_rt);
2023 gen_probe_access(s, clean_addr, MMU_DATA_STORE, MO_8);
2025 if (s->ata) {
2026 /* Extract the tag from the register to match STZGM. */
2027 tag = tcg_temp_new_i64();
2028 tcg_gen_shri_i64(tag, tcg_rt, 56);
2029 gen_helper_stzgm_tags(cpu_env, clean_addr, tag);
2030 tcg_temp_free_i64(tag);
2033 goto exit;
2034 case ARM_CP_DC_GZVA:
2036 TCGv_i64 clean_addr, tag;
2038 /* For DC_GZVA, we can rely on DC_ZVA for the proper fault. */
2039 tcg_rt = cpu_reg(s, rt);
2040 clean_addr = clean_data_tbi(s, tcg_rt);
2041 gen_helper_dc_zva(cpu_env, clean_addr);
2043 if (s->ata) {
2044 /* Extract the tag from the register to match STZGM. */
2045 tag = tcg_temp_new_i64();
2046 tcg_gen_shri_i64(tag, tcg_rt, 56);
2047 gen_helper_stzgm_tags(cpu_env, clean_addr, tag);
2048 tcg_temp_free_i64(tag);
2051 goto exit;
2052 default:
2053 g_assert_not_reached();
2055 if ((ri->type & ARM_CP_FPU) && !fp_access_check_only(s)) {
2056 goto exit;
2057 } else if ((ri->type & ARM_CP_SVE) && !sve_access_check(s)) {
2058 goto exit;
2059 } else if ((ri->type & ARM_CP_SME) && !sme_access_check(s)) {
2060 goto exit;
2063 if ((tb_cflags(s->base.tb) & CF_USE_ICOUNT) && (ri->type & ARM_CP_IO)) {
2064 gen_io_start();
2067 tcg_rt = cpu_reg(s, rt);
2069 if (isread) {
2070 if (ri->type & ARM_CP_CONST) {
2071 tcg_gen_movi_i64(tcg_rt, ri->resetvalue);
2072 } else if (ri->readfn) {
2073 if (!tcg_ri) {
2074 tcg_ri = gen_lookup_cp_reg(key);
2076 gen_helper_get_cp_reg64(tcg_rt, cpu_env, tcg_ri);
2077 } else {
2078 tcg_gen_ld_i64(tcg_rt, cpu_env, ri->fieldoffset);
2080 } else {
2081 if (ri->type & ARM_CP_CONST) {
2082 /* If not forbidden by access permissions, treat as WI */
2083 goto exit;
2084 } else if (ri->writefn) {
2085 if (!tcg_ri) {
2086 tcg_ri = gen_lookup_cp_reg(key);
2088 gen_helper_set_cp_reg64(cpu_env, tcg_ri, tcg_rt);
2089 } else {
2090 tcg_gen_st_i64(tcg_rt, cpu_env, ri->fieldoffset);
2094 if ((tb_cflags(s->base.tb) & CF_USE_ICOUNT) && (ri->type & ARM_CP_IO)) {
2095 /* I/O operations must end the TB here (whether read or write) */
2096 s->base.is_jmp = DISAS_UPDATE_EXIT;
2098 if (!isread && !(ri->type & ARM_CP_SUPPRESS_TB_END)) {
2100 * A write to any coprocessor regiser that ends a TB
2101 * must rebuild the hflags for the next TB.
2103 gen_rebuild_hflags(s);
2105 * We default to ending the TB on a coprocessor register write,
2106 * but allow this to be suppressed by the register definition
2107 * (usually only necessary to work around guest bugs).
2109 s->base.is_jmp = DISAS_UPDATE_EXIT;
2112 exit:
2113 if (tcg_ri) {
2114 tcg_temp_free_ptr(tcg_ri);
2118 /* System
2119 * 31 22 21 20 19 18 16 15 12 11 8 7 5 4 0
2120 * +---------------------+---+-----+-----+-------+-------+-----+------+
2121 * | 1 1 0 1 0 1 0 1 0 0 | L | op0 | op1 | CRn | CRm | op2 | Rt |
2122 * +---------------------+---+-----+-----+-------+-------+-----+------+
2124 static void disas_system(DisasContext *s, uint32_t insn)
2126 unsigned int l, op0, op1, crn, crm, op2, rt;
2127 l = extract32(insn, 21, 1);
2128 op0 = extract32(insn, 19, 2);
2129 op1 = extract32(insn, 16, 3);
2130 crn = extract32(insn, 12, 4);
2131 crm = extract32(insn, 8, 4);
2132 op2 = extract32(insn, 5, 3);
2133 rt = extract32(insn, 0, 5);
2135 if (op0 == 0) {
2136 if (l || rt != 31) {
2137 unallocated_encoding(s);
2138 return;
2140 switch (crn) {
2141 case 2: /* HINT (including allocated hints like NOP, YIELD, etc) */
2142 handle_hint(s, insn, op1, op2, crm);
2143 break;
2144 case 3: /* CLREX, DSB, DMB, ISB */
2145 handle_sync(s, insn, op1, op2, crm);
2146 break;
2147 case 4: /* MSR (immediate) */
2148 handle_msr_i(s, insn, op1, op2, crm);
2149 break;
2150 default:
2151 unallocated_encoding(s);
2152 break;
2154 return;
2156 handle_sys(s, insn, l, op0, op1, op2, crn, crm, rt);
2159 /* Exception generation
2161 * 31 24 23 21 20 5 4 2 1 0
2162 * +-----------------+-----+------------------------+-----+----+
2163 * | 1 1 0 1 0 1 0 0 | opc | imm16 | op2 | LL |
2164 * +-----------------------+------------------------+----------+
2166 static void disas_exc(DisasContext *s, uint32_t insn)
2168 int opc = extract32(insn, 21, 3);
2169 int op2_ll = extract32(insn, 0, 5);
2170 int imm16 = extract32(insn, 5, 16);
2171 uint32_t syndrome;
2173 switch (opc) {
2174 case 0:
2175 /* For SVC, HVC and SMC we advance the single-step state
2176 * machine before taking the exception. This is architecturally
2177 * mandated, to ensure that single-stepping a system call
2178 * instruction works properly.
2180 switch (op2_ll) {
2181 case 1: /* SVC */
2182 syndrome = syn_aa64_svc(imm16);
2183 if (s->fgt_svc) {
2184 gen_exception_insn_el(s, 0, EXCP_UDEF, syndrome, 2);
2185 break;
2187 gen_ss_advance(s);
2188 gen_exception_insn(s, 4, EXCP_SWI, syndrome);
2189 break;
2190 case 2: /* HVC */
2191 if (s->current_el == 0) {
2192 unallocated_encoding(s);
2193 break;
2195 /* The pre HVC helper handles cases when HVC gets trapped
2196 * as an undefined insn by runtime configuration.
2198 gen_a64_update_pc(s, 0);
2199 gen_helper_pre_hvc(cpu_env);
2200 gen_ss_advance(s);
2201 gen_exception_insn_el(s, 4, EXCP_HVC, syn_aa64_hvc(imm16), 2);
2202 break;
2203 case 3: /* SMC */
2204 if (s->current_el == 0) {
2205 unallocated_encoding(s);
2206 break;
2208 gen_a64_update_pc(s, 0);
2209 gen_helper_pre_smc(cpu_env, tcg_constant_i32(syn_aa64_smc(imm16)));
2210 gen_ss_advance(s);
2211 gen_exception_insn_el(s, 4, EXCP_SMC, syn_aa64_smc(imm16), 3);
2212 break;
2213 default:
2214 unallocated_encoding(s);
2215 break;
2217 break;
2218 case 1:
2219 if (op2_ll != 0) {
2220 unallocated_encoding(s);
2221 break;
2223 /* BRK */
2224 gen_exception_bkpt_insn(s, syn_aa64_bkpt(imm16));
2225 break;
2226 case 2:
2227 if (op2_ll != 0) {
2228 unallocated_encoding(s);
2229 break;
2231 /* HLT. This has two purposes.
2232 * Architecturally, it is an external halting debug instruction.
2233 * Since QEMU doesn't implement external debug, we treat this as
2234 * it is required for halting debug disabled: it will UNDEF.
2235 * Secondly, "HLT 0xf000" is the A64 semihosting syscall instruction.
2237 if (semihosting_enabled(s->current_el == 0) && imm16 == 0xf000) {
2238 gen_exception_internal_insn(s, EXCP_SEMIHOST);
2239 } else {
2240 unallocated_encoding(s);
2242 break;
2243 case 5:
2244 if (op2_ll < 1 || op2_ll > 3) {
2245 unallocated_encoding(s);
2246 break;
2248 /* DCPS1, DCPS2, DCPS3 */
2249 unallocated_encoding(s);
2250 break;
2251 default:
2252 unallocated_encoding(s);
2253 break;
2257 /* Unconditional branch (register)
2258 * 31 25 24 21 20 16 15 10 9 5 4 0
2259 * +---------------+-------+-------+-------+------+-------+
2260 * | 1 1 0 1 0 1 1 | opc | op2 | op3 | Rn | op4 |
2261 * +---------------+-------+-------+-------+------+-------+
2263 static void disas_uncond_b_reg(DisasContext *s, uint32_t insn)
2265 unsigned int opc, op2, op3, rn, op4;
2266 unsigned btype_mod = 2; /* 0: BR, 1: BLR, 2: other */
2267 TCGv_i64 dst;
2268 TCGv_i64 modifier;
2270 opc = extract32(insn, 21, 4);
2271 op2 = extract32(insn, 16, 5);
2272 op3 = extract32(insn, 10, 6);
2273 rn = extract32(insn, 5, 5);
2274 op4 = extract32(insn, 0, 5);
2276 if (op2 != 0x1f) {
2277 goto do_unallocated;
2280 switch (opc) {
2281 case 0: /* BR */
2282 case 1: /* BLR */
2283 case 2: /* RET */
2284 btype_mod = opc;
2285 switch (op3) {
2286 case 0:
2287 /* BR, BLR, RET */
2288 if (op4 != 0) {
2289 goto do_unallocated;
2291 dst = cpu_reg(s, rn);
2292 break;
2294 case 2:
2295 case 3:
2296 if (!dc_isar_feature(aa64_pauth, s)) {
2297 goto do_unallocated;
2299 if (opc == 2) {
2300 /* RETAA, RETAB */
2301 if (rn != 0x1f || op4 != 0x1f) {
2302 goto do_unallocated;
2304 rn = 30;
2305 modifier = cpu_X[31];
2306 } else {
2307 /* BRAAZ, BRABZ, BLRAAZ, BLRABZ */
2308 if (op4 != 0x1f) {
2309 goto do_unallocated;
2311 modifier = new_tmp_a64_zero(s);
2313 if (s->pauth_active) {
2314 dst = new_tmp_a64(s);
2315 if (op3 == 2) {
2316 gen_helper_autia(dst, cpu_env, cpu_reg(s, rn), modifier);
2317 } else {
2318 gen_helper_autib(dst, cpu_env, cpu_reg(s, rn), modifier);
2320 } else {
2321 dst = cpu_reg(s, rn);
2323 break;
2325 default:
2326 goto do_unallocated;
2328 /* BLR also needs to load return address */
2329 if (opc == 1) {
2330 TCGv_i64 lr = cpu_reg(s, 30);
2331 if (dst == lr) {
2332 TCGv_i64 tmp = new_tmp_a64(s);
2333 tcg_gen_mov_i64(tmp, dst);
2334 dst = tmp;
2336 gen_pc_plus_diff(s, lr, curr_insn_len(s));
2338 gen_a64_set_pc(s, dst);
2339 break;
2341 case 8: /* BRAA */
2342 case 9: /* BLRAA */
2343 if (!dc_isar_feature(aa64_pauth, s)) {
2344 goto do_unallocated;
2346 if ((op3 & ~1) != 2) {
2347 goto do_unallocated;
2349 btype_mod = opc & 1;
2350 if (s->pauth_active) {
2351 dst = new_tmp_a64(s);
2352 modifier = cpu_reg_sp(s, op4);
2353 if (op3 == 2) {
2354 gen_helper_autia(dst, cpu_env, cpu_reg(s, rn), modifier);
2355 } else {
2356 gen_helper_autib(dst, cpu_env, cpu_reg(s, rn), modifier);
2358 } else {
2359 dst = cpu_reg(s, rn);
2361 /* BLRAA also needs to load return address */
2362 if (opc == 9) {
2363 TCGv_i64 lr = cpu_reg(s, 30);
2364 if (dst == lr) {
2365 TCGv_i64 tmp = new_tmp_a64(s);
2366 tcg_gen_mov_i64(tmp, dst);
2367 dst = tmp;
2369 gen_pc_plus_diff(s, lr, curr_insn_len(s));
2371 gen_a64_set_pc(s, dst);
2372 break;
2374 case 4: /* ERET */
2375 if (s->current_el == 0) {
2376 goto do_unallocated;
2378 switch (op3) {
2379 case 0: /* ERET */
2380 if (op4 != 0) {
2381 goto do_unallocated;
2383 if (s->fgt_eret) {
2384 gen_exception_insn_el(s, 0, EXCP_UDEF, syn_erettrap(op3), 2);
2385 return;
2387 dst = tcg_temp_new_i64();
2388 tcg_gen_ld_i64(dst, cpu_env,
2389 offsetof(CPUARMState, elr_el[s->current_el]));
2390 break;
2392 case 2: /* ERETAA */
2393 case 3: /* ERETAB */
2394 if (!dc_isar_feature(aa64_pauth, s)) {
2395 goto do_unallocated;
2397 if (rn != 0x1f || op4 != 0x1f) {
2398 goto do_unallocated;
2400 /* The FGT trap takes precedence over an auth trap. */
2401 if (s->fgt_eret) {
2402 gen_exception_insn_el(s, 0, EXCP_UDEF, syn_erettrap(op3), 2);
2403 return;
2405 dst = tcg_temp_new_i64();
2406 tcg_gen_ld_i64(dst, cpu_env,
2407 offsetof(CPUARMState, elr_el[s->current_el]));
2408 if (s->pauth_active) {
2409 modifier = cpu_X[31];
2410 if (op3 == 2) {
2411 gen_helper_autia(dst, cpu_env, dst, modifier);
2412 } else {
2413 gen_helper_autib(dst, cpu_env, dst, modifier);
2416 break;
2418 default:
2419 goto do_unallocated;
2421 if (tb_cflags(s->base.tb) & CF_USE_ICOUNT) {
2422 gen_io_start();
2425 gen_helper_exception_return(cpu_env, dst);
2426 tcg_temp_free_i64(dst);
2427 /* Must exit loop to check un-masked IRQs */
2428 s->base.is_jmp = DISAS_EXIT;
2429 return;
2431 case 5: /* DRPS */
2432 if (op3 != 0 || op4 != 0 || rn != 0x1f) {
2433 goto do_unallocated;
2434 } else {
2435 unallocated_encoding(s);
2437 return;
2439 default:
2440 do_unallocated:
2441 unallocated_encoding(s);
2442 return;
2445 switch (btype_mod) {
2446 case 0: /* BR */
2447 if (dc_isar_feature(aa64_bti, s)) {
2448 /* BR to {x16,x17} or !guard -> 1, else 3. */
2449 set_btype(s, rn == 16 || rn == 17 || !s->guarded_page ? 1 : 3);
2451 break;
2453 case 1: /* BLR */
2454 if (dc_isar_feature(aa64_bti, s)) {
2455 /* BLR sets BTYPE to 2, regardless of source guarded page. */
2456 set_btype(s, 2);
2458 break;
2460 default: /* RET or none of the above. */
2461 /* BTYPE will be set to 0 by normal end-of-insn processing. */
2462 break;
2465 s->base.is_jmp = DISAS_JUMP;
2468 /* Branches, exception generating and system instructions */
2469 static void disas_b_exc_sys(DisasContext *s, uint32_t insn)
2471 switch (extract32(insn, 25, 7)) {
2472 case 0x0a: case 0x0b:
2473 case 0x4a: case 0x4b: /* Unconditional branch (immediate) */
2474 disas_uncond_b_imm(s, insn);
2475 break;
2476 case 0x1a: case 0x5a: /* Compare & branch (immediate) */
2477 disas_comp_b_imm(s, insn);
2478 break;
2479 case 0x1b: case 0x5b: /* Test & branch (immediate) */
2480 disas_test_b_imm(s, insn);
2481 break;
2482 case 0x2a: /* Conditional branch (immediate) */
2483 disas_cond_b_imm(s, insn);
2484 break;
2485 case 0x6a: /* Exception generation / System */
2486 if (insn & (1 << 24)) {
2487 if (extract32(insn, 22, 2) == 0) {
2488 disas_system(s, insn);
2489 } else {
2490 unallocated_encoding(s);
2492 } else {
2493 disas_exc(s, insn);
2495 break;
2496 case 0x6b: /* Unconditional branch (register) */
2497 disas_uncond_b_reg(s, insn);
2498 break;
2499 default:
2500 unallocated_encoding(s);
2501 break;
2506 * Load/Store exclusive instructions are implemented by remembering
2507 * the value/address loaded, and seeing if these are the same
2508 * when the store is performed. This is not actually the architecturally
2509 * mandated semantics, but it works for typical guest code sequences
2510 * and avoids having to monitor regular stores.
2512 * The store exclusive uses the atomic cmpxchg primitives to avoid
2513 * races in multi-threaded linux-user and when MTTCG softmmu is
2514 * enabled.
2516 static void gen_load_exclusive(DisasContext *s, int rt, int rt2,
2517 TCGv_i64 addr, int size, bool is_pair)
2519 int idx = get_mem_index(s);
2520 MemOp memop = s->be_data;
2522 g_assert(size <= 3);
2523 if (is_pair) {
2524 g_assert(size >= 2);
2525 if (size == 2) {
2526 /* The pair must be single-copy atomic for the doubleword. */
2527 memop |= MO_64 | MO_ALIGN;
2528 tcg_gen_qemu_ld_i64(cpu_exclusive_val, addr, idx, memop);
2529 if (s->be_data == MO_LE) {
2530 tcg_gen_extract_i64(cpu_reg(s, rt), cpu_exclusive_val, 0, 32);
2531 tcg_gen_extract_i64(cpu_reg(s, rt2), cpu_exclusive_val, 32, 32);
2532 } else {
2533 tcg_gen_extract_i64(cpu_reg(s, rt), cpu_exclusive_val, 32, 32);
2534 tcg_gen_extract_i64(cpu_reg(s, rt2), cpu_exclusive_val, 0, 32);
2536 } else {
2537 /* The pair must be single-copy atomic for *each* doubleword, not
2538 the entire quadword, however it must be quadword aligned. */
2539 memop |= MO_64;
2540 tcg_gen_qemu_ld_i64(cpu_exclusive_val, addr, idx,
2541 memop | MO_ALIGN_16);
2543 TCGv_i64 addr2 = tcg_temp_new_i64();
2544 tcg_gen_addi_i64(addr2, addr, 8);
2545 tcg_gen_qemu_ld_i64(cpu_exclusive_high, addr2, idx, memop);
2546 tcg_temp_free_i64(addr2);
2548 tcg_gen_mov_i64(cpu_reg(s, rt), cpu_exclusive_val);
2549 tcg_gen_mov_i64(cpu_reg(s, rt2), cpu_exclusive_high);
2551 } else {
2552 memop |= size | MO_ALIGN;
2553 tcg_gen_qemu_ld_i64(cpu_exclusive_val, addr, idx, memop);
2554 tcg_gen_mov_i64(cpu_reg(s, rt), cpu_exclusive_val);
2556 tcg_gen_mov_i64(cpu_exclusive_addr, addr);
2559 static void gen_store_exclusive(DisasContext *s, int rd, int rt, int rt2,
2560 TCGv_i64 addr, int size, int is_pair)
2562 /* if (env->exclusive_addr == addr && env->exclusive_val == [addr]
2563 * && (!is_pair || env->exclusive_high == [addr + datasize])) {
2564 * [addr] = {Rt};
2565 * if (is_pair) {
2566 * [addr + datasize] = {Rt2};
2568 * {Rd} = 0;
2569 * } else {
2570 * {Rd} = 1;
2572 * env->exclusive_addr = -1;
2574 TCGLabel *fail_label = gen_new_label();
2575 TCGLabel *done_label = gen_new_label();
2576 TCGv_i64 tmp;
2578 tcg_gen_brcond_i64(TCG_COND_NE, addr, cpu_exclusive_addr, fail_label);
2580 tmp = tcg_temp_new_i64();
2581 if (is_pair) {
2582 if (size == 2) {
2583 if (s->be_data == MO_LE) {
2584 tcg_gen_concat32_i64(tmp, cpu_reg(s, rt), cpu_reg(s, rt2));
2585 } else {
2586 tcg_gen_concat32_i64(tmp, cpu_reg(s, rt2), cpu_reg(s, rt));
2588 tcg_gen_atomic_cmpxchg_i64(tmp, cpu_exclusive_addr,
2589 cpu_exclusive_val, tmp,
2590 get_mem_index(s),
2591 MO_64 | MO_ALIGN | s->be_data);
2592 tcg_gen_setcond_i64(TCG_COND_NE, tmp, tmp, cpu_exclusive_val);
2593 } else {
2594 TCGv_i128 t16 = tcg_temp_new_i128();
2595 TCGv_i128 c16 = tcg_temp_new_i128();
2596 TCGv_i64 a, b;
2598 if (s->be_data == MO_LE) {
2599 tcg_gen_concat_i64_i128(t16, cpu_reg(s, rt), cpu_reg(s, rt2));
2600 tcg_gen_concat_i64_i128(c16, cpu_exclusive_val,
2601 cpu_exclusive_high);
2602 } else {
2603 tcg_gen_concat_i64_i128(t16, cpu_reg(s, rt2), cpu_reg(s, rt));
2604 tcg_gen_concat_i64_i128(c16, cpu_exclusive_high,
2605 cpu_exclusive_val);
2608 tcg_gen_atomic_cmpxchg_i128(t16, cpu_exclusive_addr, c16, t16,
2609 get_mem_index(s),
2610 MO_128 | MO_ALIGN | s->be_data);
2611 tcg_temp_free_i128(c16);
2613 a = tcg_temp_new_i64();
2614 b = tcg_temp_new_i64();
2615 if (s->be_data == MO_LE) {
2616 tcg_gen_extr_i128_i64(a, b, t16);
2617 } else {
2618 tcg_gen_extr_i128_i64(b, a, t16);
2621 tcg_gen_xor_i64(a, a, cpu_exclusive_val);
2622 tcg_gen_xor_i64(b, b, cpu_exclusive_high);
2623 tcg_gen_or_i64(tmp, a, b);
2624 tcg_temp_free_i64(a);
2625 tcg_temp_free_i64(b);
2626 tcg_temp_free_i128(t16);
2628 tcg_gen_setcondi_i64(TCG_COND_NE, tmp, tmp, 0);
2630 } else {
2631 tcg_gen_atomic_cmpxchg_i64(tmp, cpu_exclusive_addr, cpu_exclusive_val,
2632 cpu_reg(s, rt), get_mem_index(s),
2633 size | MO_ALIGN | s->be_data);
2634 tcg_gen_setcond_i64(TCG_COND_NE, tmp, tmp, cpu_exclusive_val);
2636 tcg_gen_mov_i64(cpu_reg(s, rd), tmp);
2637 tcg_temp_free_i64(tmp);
2638 tcg_gen_br(done_label);
2640 gen_set_label(fail_label);
2641 tcg_gen_movi_i64(cpu_reg(s, rd), 1);
2642 gen_set_label(done_label);
2643 tcg_gen_movi_i64(cpu_exclusive_addr, -1);
2646 static void gen_compare_and_swap(DisasContext *s, int rs, int rt,
2647 int rn, int size)
2649 TCGv_i64 tcg_rs = cpu_reg(s, rs);
2650 TCGv_i64 tcg_rt = cpu_reg(s, rt);
2651 int memidx = get_mem_index(s);
2652 TCGv_i64 clean_addr;
2654 if (rn == 31) {
2655 gen_check_sp_alignment(s);
2657 clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn), true, rn != 31, size);
2658 tcg_gen_atomic_cmpxchg_i64(tcg_rs, clean_addr, tcg_rs, tcg_rt, memidx,
2659 size | MO_ALIGN | s->be_data);
2662 static void gen_compare_and_swap_pair(DisasContext *s, int rs, int rt,
2663 int rn, int size)
2665 TCGv_i64 s1 = cpu_reg(s, rs);
2666 TCGv_i64 s2 = cpu_reg(s, rs + 1);
2667 TCGv_i64 t1 = cpu_reg(s, rt);
2668 TCGv_i64 t2 = cpu_reg(s, rt + 1);
2669 TCGv_i64 clean_addr;
2670 int memidx = get_mem_index(s);
2672 if (rn == 31) {
2673 gen_check_sp_alignment(s);
2676 /* This is a single atomic access, despite the "pair". */
2677 clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn), true, rn != 31, size + 1);
2679 if (size == 2) {
2680 TCGv_i64 cmp = tcg_temp_new_i64();
2681 TCGv_i64 val = tcg_temp_new_i64();
2683 if (s->be_data == MO_LE) {
2684 tcg_gen_concat32_i64(val, t1, t2);
2685 tcg_gen_concat32_i64(cmp, s1, s2);
2686 } else {
2687 tcg_gen_concat32_i64(val, t2, t1);
2688 tcg_gen_concat32_i64(cmp, s2, s1);
2691 tcg_gen_atomic_cmpxchg_i64(cmp, clean_addr, cmp, val, memidx,
2692 MO_64 | MO_ALIGN | s->be_data);
2693 tcg_temp_free_i64(val);
2695 if (s->be_data == MO_LE) {
2696 tcg_gen_extr32_i64(s1, s2, cmp);
2697 } else {
2698 tcg_gen_extr32_i64(s2, s1, cmp);
2700 tcg_temp_free_i64(cmp);
2701 } else {
2702 TCGv_i128 cmp = tcg_temp_new_i128();
2703 TCGv_i128 val = tcg_temp_new_i128();
2705 if (s->be_data == MO_LE) {
2706 tcg_gen_concat_i64_i128(val, t1, t2);
2707 tcg_gen_concat_i64_i128(cmp, s1, s2);
2708 } else {
2709 tcg_gen_concat_i64_i128(val, t2, t1);
2710 tcg_gen_concat_i64_i128(cmp, s2, s1);
2713 tcg_gen_atomic_cmpxchg_i128(cmp, clean_addr, cmp, val, memidx,
2714 MO_128 | MO_ALIGN | s->be_data);
2715 tcg_temp_free_i128(val);
2717 if (s->be_data == MO_LE) {
2718 tcg_gen_extr_i128_i64(s1, s2, cmp);
2719 } else {
2720 tcg_gen_extr_i128_i64(s2, s1, cmp);
2722 tcg_temp_free_i128(cmp);
2726 /* Update the Sixty-Four bit (SF) registersize. This logic is derived
2727 * from the ARMv8 specs for LDR (Shared decode for all encodings).
2729 static bool disas_ldst_compute_iss_sf(int size, bool is_signed, int opc)
2731 int opc0 = extract32(opc, 0, 1);
2732 int regsize;
2734 if (is_signed) {
2735 regsize = opc0 ? 32 : 64;
2736 } else {
2737 regsize = size == 3 ? 64 : 32;
2739 return regsize == 64;
2742 /* Load/store exclusive
2744 * 31 30 29 24 23 22 21 20 16 15 14 10 9 5 4 0
2745 * +-----+-------------+----+---+----+------+----+-------+------+------+
2746 * | sz | 0 0 1 0 0 0 | o2 | L | o1 | Rs | o0 | Rt2 | Rn | Rt |
2747 * +-----+-------------+----+---+----+------+----+-------+------+------+
2749 * sz: 00 -> 8 bit, 01 -> 16 bit, 10 -> 32 bit, 11 -> 64 bit
2750 * L: 0 -> store, 1 -> load
2751 * o2: 0 -> exclusive, 1 -> not
2752 * o1: 0 -> single register, 1 -> register pair
2753 * o0: 1 -> load-acquire/store-release, 0 -> not
2755 static void disas_ldst_excl(DisasContext *s, uint32_t insn)
2757 int rt = extract32(insn, 0, 5);
2758 int rn = extract32(insn, 5, 5);
2759 int rt2 = extract32(insn, 10, 5);
2760 int rs = extract32(insn, 16, 5);
2761 int is_lasr = extract32(insn, 15, 1);
2762 int o2_L_o1_o0 = extract32(insn, 21, 3) * 2 | is_lasr;
2763 int size = extract32(insn, 30, 2);
2764 TCGv_i64 clean_addr;
2766 switch (o2_L_o1_o0) {
2767 case 0x0: /* STXR */
2768 case 0x1: /* STLXR */
2769 if (rn == 31) {
2770 gen_check_sp_alignment(s);
2772 if (is_lasr) {
2773 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2775 clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn),
2776 true, rn != 31, size);
2777 gen_store_exclusive(s, rs, rt, rt2, clean_addr, size, false);
2778 return;
2780 case 0x4: /* LDXR */
2781 case 0x5: /* LDAXR */
2782 if (rn == 31) {
2783 gen_check_sp_alignment(s);
2785 clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn),
2786 false, rn != 31, size);
2787 s->is_ldex = true;
2788 gen_load_exclusive(s, rt, rt2, clean_addr, size, false);
2789 if (is_lasr) {
2790 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2792 return;
2794 case 0x8: /* STLLR */
2795 if (!dc_isar_feature(aa64_lor, s)) {
2796 break;
2798 /* StoreLORelease is the same as Store-Release for QEMU. */
2799 /* fall through */
2800 case 0x9: /* STLR */
2801 /* Generate ISS for non-exclusive accesses including LASR. */
2802 if (rn == 31) {
2803 gen_check_sp_alignment(s);
2805 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2806 clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn),
2807 true, rn != 31, size);
2808 /* TODO: ARMv8.4-LSE SCTLR.nAA */
2809 do_gpr_st(s, cpu_reg(s, rt), clean_addr, size | MO_ALIGN, true, rt,
2810 disas_ldst_compute_iss_sf(size, false, 0), is_lasr);
2811 return;
2813 case 0xc: /* LDLAR */
2814 if (!dc_isar_feature(aa64_lor, s)) {
2815 break;
2817 /* LoadLOAcquire is the same as Load-Acquire for QEMU. */
2818 /* fall through */
2819 case 0xd: /* LDAR */
2820 /* Generate ISS for non-exclusive accesses including LASR. */
2821 if (rn == 31) {
2822 gen_check_sp_alignment(s);
2824 clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn),
2825 false, rn != 31, size);
2826 /* TODO: ARMv8.4-LSE SCTLR.nAA */
2827 do_gpr_ld(s, cpu_reg(s, rt), clean_addr, size | MO_ALIGN, false, true,
2828 rt, disas_ldst_compute_iss_sf(size, false, 0), is_lasr);
2829 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2830 return;
2832 case 0x2: case 0x3: /* CASP / STXP */
2833 if (size & 2) { /* STXP / STLXP */
2834 if (rn == 31) {
2835 gen_check_sp_alignment(s);
2837 if (is_lasr) {
2838 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2840 clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn),
2841 true, rn != 31, size);
2842 gen_store_exclusive(s, rs, rt, rt2, clean_addr, size, true);
2843 return;
2845 if (rt2 == 31
2846 && ((rt | rs) & 1) == 0
2847 && dc_isar_feature(aa64_atomics, s)) {
2848 /* CASP / CASPL */
2849 gen_compare_and_swap_pair(s, rs, rt, rn, size | 2);
2850 return;
2852 break;
2854 case 0x6: case 0x7: /* CASPA / LDXP */
2855 if (size & 2) { /* LDXP / LDAXP */
2856 if (rn == 31) {
2857 gen_check_sp_alignment(s);
2859 clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn),
2860 false, rn != 31, size);
2861 s->is_ldex = true;
2862 gen_load_exclusive(s, rt, rt2, clean_addr, size, true);
2863 if (is_lasr) {
2864 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2866 return;
2868 if (rt2 == 31
2869 && ((rt | rs) & 1) == 0
2870 && dc_isar_feature(aa64_atomics, s)) {
2871 /* CASPA / CASPAL */
2872 gen_compare_and_swap_pair(s, rs, rt, rn, size | 2);
2873 return;
2875 break;
2877 case 0xa: /* CAS */
2878 case 0xb: /* CASL */
2879 case 0xe: /* CASA */
2880 case 0xf: /* CASAL */
2881 if (rt2 == 31 && dc_isar_feature(aa64_atomics, s)) {
2882 gen_compare_and_swap(s, rs, rt, rn, size);
2883 return;
2885 break;
2887 unallocated_encoding(s);
2891 * Load register (literal)
2893 * 31 30 29 27 26 25 24 23 5 4 0
2894 * +-----+-------+---+-----+-------------------+-------+
2895 * | opc | 0 1 1 | V | 0 0 | imm19 | Rt |
2896 * +-----+-------+---+-----+-------------------+-------+
2898 * V: 1 -> vector (simd/fp)
2899 * opc (non-vector): 00 -> 32 bit, 01 -> 64 bit,
2900 * 10-> 32 bit signed, 11 -> prefetch
2901 * opc (vector): 00 -> 32 bit, 01 -> 64 bit, 10 -> 128 bit (11 unallocated)
2903 static void disas_ld_lit(DisasContext *s, uint32_t insn)
2905 int rt = extract32(insn, 0, 5);
2906 int64_t imm = sextract32(insn, 5, 19) << 2;
2907 bool is_vector = extract32(insn, 26, 1);
2908 int opc = extract32(insn, 30, 2);
2909 bool is_signed = false;
2910 int size = 2;
2911 TCGv_i64 tcg_rt, clean_addr;
2913 if (is_vector) {
2914 if (opc == 3) {
2915 unallocated_encoding(s);
2916 return;
2918 size = 2 + opc;
2919 if (!fp_access_check(s)) {
2920 return;
2922 } else {
2923 if (opc == 3) {
2924 /* PRFM (literal) : prefetch */
2925 return;
2927 size = 2 + extract32(opc, 0, 1);
2928 is_signed = extract32(opc, 1, 1);
2931 tcg_rt = cpu_reg(s, rt);
2933 clean_addr = new_tmp_a64(s);
2934 gen_pc_plus_diff(s, clean_addr, imm);
2935 if (is_vector) {
2936 do_fp_ld(s, rt, clean_addr, size);
2937 } else {
2938 /* Only unsigned 32bit loads target 32bit registers. */
2939 bool iss_sf = opc != 0;
2941 do_gpr_ld(s, tcg_rt, clean_addr, size + is_signed * MO_SIGN,
2942 false, true, rt, iss_sf, false);
2947 * LDNP (Load Pair - non-temporal hint)
2948 * LDP (Load Pair - non vector)
2949 * LDPSW (Load Pair Signed Word - non vector)
2950 * STNP (Store Pair - non-temporal hint)
2951 * STP (Store Pair - non vector)
2952 * LDNP (Load Pair of SIMD&FP - non-temporal hint)
2953 * LDP (Load Pair of SIMD&FP)
2954 * STNP (Store Pair of SIMD&FP - non-temporal hint)
2955 * STP (Store Pair of SIMD&FP)
2957 * 31 30 29 27 26 25 24 23 22 21 15 14 10 9 5 4 0
2958 * +-----+-------+---+---+-------+---+-----------------------------+
2959 * | opc | 1 0 1 | V | 0 | index | L | imm7 | Rt2 | Rn | Rt |
2960 * +-----+-------+---+---+-------+---+-------+-------+------+------+
2962 * opc: LDP/STP/LDNP/STNP 00 -> 32 bit, 10 -> 64 bit
2963 * LDPSW/STGP 01
2964 * LDP/STP/LDNP/STNP (SIMD) 00 -> 32 bit, 01 -> 64 bit, 10 -> 128 bit
2965 * V: 0 -> GPR, 1 -> Vector
2966 * idx: 00 -> signed offset with non-temporal hint, 01 -> post-index,
2967 * 10 -> signed offset, 11 -> pre-index
2968 * L: 0 -> Store 1 -> Load
2970 * Rt, Rt2 = GPR or SIMD registers to be stored
2971 * Rn = general purpose register containing address
2972 * imm7 = signed offset (multiple of 4 or 8 depending on size)
2974 static void disas_ldst_pair(DisasContext *s, uint32_t insn)
2976 int rt = extract32(insn, 0, 5);
2977 int rn = extract32(insn, 5, 5);
2978 int rt2 = extract32(insn, 10, 5);
2979 uint64_t offset = sextract64(insn, 15, 7);
2980 int index = extract32(insn, 23, 2);
2981 bool is_vector = extract32(insn, 26, 1);
2982 bool is_load = extract32(insn, 22, 1);
2983 int opc = extract32(insn, 30, 2);
2985 bool is_signed = false;
2986 bool postindex = false;
2987 bool wback = false;
2988 bool set_tag = false;
2990 TCGv_i64 clean_addr, dirty_addr;
2992 int size;
2994 if (opc == 3) {
2995 unallocated_encoding(s);
2996 return;
2999 if (is_vector) {
3000 size = 2 + opc;
3001 } else if (opc == 1 && !is_load) {
3002 /* STGP */
3003 if (!dc_isar_feature(aa64_mte_insn_reg, s) || index == 0) {
3004 unallocated_encoding(s);
3005 return;
3007 size = 3;
3008 set_tag = true;
3009 } else {
3010 size = 2 + extract32(opc, 1, 1);
3011 is_signed = extract32(opc, 0, 1);
3012 if (!is_load && is_signed) {
3013 unallocated_encoding(s);
3014 return;
3018 switch (index) {
3019 case 1: /* post-index */
3020 postindex = true;
3021 wback = true;
3022 break;
3023 case 0:
3024 /* signed offset with "non-temporal" hint. Since we don't emulate
3025 * caches we don't care about hints to the cache system about
3026 * data access patterns, and handle this identically to plain
3027 * signed offset.
3029 if (is_signed) {
3030 /* There is no non-temporal-hint version of LDPSW */
3031 unallocated_encoding(s);
3032 return;
3034 postindex = false;
3035 break;
3036 case 2: /* signed offset, rn not updated */
3037 postindex = false;
3038 break;
3039 case 3: /* pre-index */
3040 postindex = false;
3041 wback = true;
3042 break;
3045 if (is_vector && !fp_access_check(s)) {
3046 return;
3049 offset <<= (set_tag ? LOG2_TAG_GRANULE : size);
3051 if (rn == 31) {
3052 gen_check_sp_alignment(s);
3055 dirty_addr = read_cpu_reg_sp(s, rn, 1);
3056 if (!postindex) {
3057 tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3060 if (set_tag) {
3061 if (!s->ata) {
3063 * TODO: We could rely on the stores below, at least for
3064 * system mode, if we arrange to add MO_ALIGN_16.
3066 gen_helper_stg_stub(cpu_env, dirty_addr);
3067 } else if (tb_cflags(s->base.tb) & CF_PARALLEL) {
3068 gen_helper_stg_parallel(cpu_env, dirty_addr, dirty_addr);
3069 } else {
3070 gen_helper_stg(cpu_env, dirty_addr, dirty_addr);
3074 clean_addr = gen_mte_checkN(s, dirty_addr, !is_load,
3075 (wback || rn != 31) && !set_tag, 2 << size);
3077 if (is_vector) {
3078 if (is_load) {
3079 do_fp_ld(s, rt, clean_addr, size);
3080 } else {
3081 do_fp_st(s, rt, clean_addr, size);
3083 tcg_gen_addi_i64(clean_addr, clean_addr, 1 << size);
3084 if (is_load) {
3085 do_fp_ld(s, rt2, clean_addr, size);
3086 } else {
3087 do_fp_st(s, rt2, clean_addr, size);
3089 } else {
3090 TCGv_i64 tcg_rt = cpu_reg(s, rt);
3091 TCGv_i64 tcg_rt2 = cpu_reg(s, rt2);
3093 if (is_load) {
3094 TCGv_i64 tmp = tcg_temp_new_i64();
3096 /* Do not modify tcg_rt before recognizing any exception
3097 * from the second load.
3099 do_gpr_ld(s, tmp, clean_addr, size + is_signed * MO_SIGN,
3100 false, false, 0, false, false);
3101 tcg_gen_addi_i64(clean_addr, clean_addr, 1 << size);
3102 do_gpr_ld(s, tcg_rt2, clean_addr, size + is_signed * MO_SIGN,
3103 false, false, 0, false, false);
3105 tcg_gen_mov_i64(tcg_rt, tmp);
3106 tcg_temp_free_i64(tmp);
3107 } else {
3108 do_gpr_st(s, tcg_rt, clean_addr, size,
3109 false, 0, false, false);
3110 tcg_gen_addi_i64(clean_addr, clean_addr, 1 << size);
3111 do_gpr_st(s, tcg_rt2, clean_addr, size,
3112 false, 0, false, false);
3116 if (wback) {
3117 if (postindex) {
3118 tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3120 tcg_gen_mov_i64(cpu_reg_sp(s, rn), dirty_addr);
3125 * Load/store (immediate post-indexed)
3126 * Load/store (immediate pre-indexed)
3127 * Load/store (unscaled immediate)
3129 * 31 30 29 27 26 25 24 23 22 21 20 12 11 10 9 5 4 0
3130 * +----+-------+---+-----+-----+---+--------+-----+------+------+
3131 * |size| 1 1 1 | V | 0 0 | opc | 0 | imm9 | idx | Rn | Rt |
3132 * +----+-------+---+-----+-----+---+--------+-----+------+------+
3134 * idx = 01 -> post-indexed, 11 pre-indexed, 00 unscaled imm. (no writeback)
3135 10 -> unprivileged
3136 * V = 0 -> non-vector
3137 * size: 00 -> 8 bit, 01 -> 16 bit, 10 -> 32 bit, 11 -> 64bit
3138 * opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
3140 static void disas_ldst_reg_imm9(DisasContext *s, uint32_t insn,
3141 int opc,
3142 int size,
3143 int rt,
3144 bool is_vector)
3146 int rn = extract32(insn, 5, 5);
3147 int imm9 = sextract32(insn, 12, 9);
3148 int idx = extract32(insn, 10, 2);
3149 bool is_signed = false;
3150 bool is_store = false;
3151 bool is_extended = false;
3152 bool is_unpriv = (idx == 2);
3153 bool iss_valid;
3154 bool post_index;
3155 bool writeback;
3156 int memidx;
3158 TCGv_i64 clean_addr, dirty_addr;
3160 if (is_vector) {
3161 size |= (opc & 2) << 1;
3162 if (size > 4 || is_unpriv) {
3163 unallocated_encoding(s);
3164 return;
3166 is_store = ((opc & 1) == 0);
3167 if (!fp_access_check(s)) {
3168 return;
3170 } else {
3171 if (size == 3 && opc == 2) {
3172 /* PRFM - prefetch */
3173 if (idx != 0) {
3174 unallocated_encoding(s);
3175 return;
3177 return;
3179 if (opc == 3 && size > 1) {
3180 unallocated_encoding(s);
3181 return;
3183 is_store = (opc == 0);
3184 is_signed = extract32(opc, 1, 1);
3185 is_extended = (size < 3) && extract32(opc, 0, 1);
3188 switch (idx) {
3189 case 0:
3190 case 2:
3191 post_index = false;
3192 writeback = false;
3193 break;
3194 case 1:
3195 post_index = true;
3196 writeback = true;
3197 break;
3198 case 3:
3199 post_index = false;
3200 writeback = true;
3201 break;
3202 default:
3203 g_assert_not_reached();
3206 iss_valid = !is_vector && !writeback;
3208 if (rn == 31) {
3209 gen_check_sp_alignment(s);
3212 dirty_addr = read_cpu_reg_sp(s, rn, 1);
3213 if (!post_index) {
3214 tcg_gen_addi_i64(dirty_addr, dirty_addr, imm9);
3217 memidx = is_unpriv ? get_a64_user_mem_index(s) : get_mem_index(s);
3218 clean_addr = gen_mte_check1_mmuidx(s, dirty_addr, is_store,
3219 writeback || rn != 31,
3220 size, is_unpriv, memidx);
3222 if (is_vector) {
3223 if (is_store) {
3224 do_fp_st(s, rt, clean_addr, size);
3225 } else {
3226 do_fp_ld(s, rt, clean_addr, size);
3228 } else {
3229 TCGv_i64 tcg_rt = cpu_reg(s, rt);
3230 bool iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
3232 if (is_store) {
3233 do_gpr_st_memidx(s, tcg_rt, clean_addr, size, memidx,
3234 iss_valid, rt, iss_sf, false);
3235 } else {
3236 do_gpr_ld_memidx(s, tcg_rt, clean_addr, size + is_signed * MO_SIGN,
3237 is_extended, memidx,
3238 iss_valid, rt, iss_sf, false);
3242 if (writeback) {
3243 TCGv_i64 tcg_rn = cpu_reg_sp(s, rn);
3244 if (post_index) {
3245 tcg_gen_addi_i64(dirty_addr, dirty_addr, imm9);
3247 tcg_gen_mov_i64(tcg_rn, dirty_addr);
3252 * Load/store (register offset)
3254 * 31 30 29 27 26 25 24 23 22 21 20 16 15 13 12 11 10 9 5 4 0
3255 * +----+-------+---+-----+-----+---+------+-----+--+-----+----+----+
3256 * |size| 1 1 1 | V | 0 0 | opc | 1 | Rm | opt | S| 1 0 | Rn | Rt |
3257 * +----+-------+---+-----+-----+---+------+-----+--+-----+----+----+
3259 * For non-vector:
3260 * size: 00-> byte, 01 -> 16 bit, 10 -> 32bit, 11 -> 64bit
3261 * opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
3262 * For vector:
3263 * size is opc<1>:size<1:0> so 100 -> 128 bit; 110 and 111 unallocated
3264 * opc<0>: 0 -> store, 1 -> load
3265 * V: 1 -> vector/simd
3266 * opt: extend encoding (see DecodeRegExtend)
3267 * S: if S=1 then scale (essentially index by sizeof(size))
3268 * Rt: register to transfer into/out of
3269 * Rn: address register or SP for base
3270 * Rm: offset register or ZR for offset
3272 static void disas_ldst_reg_roffset(DisasContext *s, uint32_t insn,
3273 int opc,
3274 int size,
3275 int rt,
3276 bool is_vector)
3278 int rn = extract32(insn, 5, 5);
3279 int shift = extract32(insn, 12, 1);
3280 int rm = extract32(insn, 16, 5);
3281 int opt = extract32(insn, 13, 3);
3282 bool is_signed = false;
3283 bool is_store = false;
3284 bool is_extended = false;
3286 TCGv_i64 tcg_rm, clean_addr, dirty_addr;
3288 if (extract32(opt, 1, 1) == 0) {
3289 unallocated_encoding(s);
3290 return;
3293 if (is_vector) {
3294 size |= (opc & 2) << 1;
3295 if (size > 4) {
3296 unallocated_encoding(s);
3297 return;
3299 is_store = !extract32(opc, 0, 1);
3300 if (!fp_access_check(s)) {
3301 return;
3303 } else {
3304 if (size == 3 && opc == 2) {
3305 /* PRFM - prefetch */
3306 return;
3308 if (opc == 3 && size > 1) {
3309 unallocated_encoding(s);
3310 return;
3312 is_store = (opc == 0);
3313 is_signed = extract32(opc, 1, 1);
3314 is_extended = (size < 3) && extract32(opc, 0, 1);
3317 if (rn == 31) {
3318 gen_check_sp_alignment(s);
3320 dirty_addr = read_cpu_reg_sp(s, rn, 1);
3322 tcg_rm = read_cpu_reg(s, rm, 1);
3323 ext_and_shift_reg(tcg_rm, tcg_rm, opt, shift ? size : 0);
3325 tcg_gen_add_i64(dirty_addr, dirty_addr, tcg_rm);
3326 clean_addr = gen_mte_check1(s, dirty_addr, is_store, true, size);
3328 if (is_vector) {
3329 if (is_store) {
3330 do_fp_st(s, rt, clean_addr, size);
3331 } else {
3332 do_fp_ld(s, rt, clean_addr, size);
3334 } else {
3335 TCGv_i64 tcg_rt = cpu_reg(s, rt);
3336 bool iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
3337 if (is_store) {
3338 do_gpr_st(s, tcg_rt, clean_addr, size,
3339 true, rt, iss_sf, false);
3340 } else {
3341 do_gpr_ld(s, tcg_rt, clean_addr, size + is_signed * MO_SIGN,
3342 is_extended, true, rt, iss_sf, false);
3348 * Load/store (unsigned immediate)
3350 * 31 30 29 27 26 25 24 23 22 21 10 9 5
3351 * +----+-------+---+-----+-----+------------+-------+------+
3352 * |size| 1 1 1 | V | 0 1 | opc | imm12 | Rn | Rt |
3353 * +----+-------+---+-----+-----+------------+-------+------+
3355 * For non-vector:
3356 * size: 00-> byte, 01 -> 16 bit, 10 -> 32bit, 11 -> 64bit
3357 * opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
3358 * For vector:
3359 * size is opc<1>:size<1:0> so 100 -> 128 bit; 110 and 111 unallocated
3360 * opc<0>: 0 -> store, 1 -> load
3361 * Rn: base address register (inc SP)
3362 * Rt: target register
3364 static void disas_ldst_reg_unsigned_imm(DisasContext *s, uint32_t insn,
3365 int opc,
3366 int size,
3367 int rt,
3368 bool is_vector)
3370 int rn = extract32(insn, 5, 5);
3371 unsigned int imm12 = extract32(insn, 10, 12);
3372 unsigned int offset;
3374 TCGv_i64 clean_addr, dirty_addr;
3376 bool is_store;
3377 bool is_signed = false;
3378 bool is_extended = false;
3380 if (is_vector) {
3381 size |= (opc & 2) << 1;
3382 if (size > 4) {
3383 unallocated_encoding(s);
3384 return;
3386 is_store = !extract32(opc, 0, 1);
3387 if (!fp_access_check(s)) {
3388 return;
3390 } else {
3391 if (size == 3 && opc == 2) {
3392 /* PRFM - prefetch */
3393 return;
3395 if (opc == 3 && size > 1) {
3396 unallocated_encoding(s);
3397 return;
3399 is_store = (opc == 0);
3400 is_signed = extract32(opc, 1, 1);
3401 is_extended = (size < 3) && extract32(opc, 0, 1);
3404 if (rn == 31) {
3405 gen_check_sp_alignment(s);
3407 dirty_addr = read_cpu_reg_sp(s, rn, 1);
3408 offset = imm12 << size;
3409 tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3410 clean_addr = gen_mte_check1(s, dirty_addr, is_store, rn != 31, size);
3412 if (is_vector) {
3413 if (is_store) {
3414 do_fp_st(s, rt, clean_addr, size);
3415 } else {
3416 do_fp_ld(s, rt, clean_addr, size);
3418 } else {
3419 TCGv_i64 tcg_rt = cpu_reg(s, rt);
3420 bool iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
3421 if (is_store) {
3422 do_gpr_st(s, tcg_rt, clean_addr, size,
3423 true, rt, iss_sf, false);
3424 } else {
3425 do_gpr_ld(s, tcg_rt, clean_addr, size + is_signed * MO_SIGN,
3426 is_extended, true, rt, iss_sf, false);
3431 /* Atomic memory operations
3433 * 31 30 27 26 24 22 21 16 15 12 10 5 0
3434 * +------+-------+---+-----+-----+---+----+----+-----+-----+----+-----+
3435 * | size | 1 1 1 | V | 0 0 | A R | 1 | Rs | o3 | opc | 0 0 | Rn | Rt |
3436 * +------+-------+---+-----+-----+--------+----+-----+-----+----+-----+
3438 * Rt: the result register
3439 * Rn: base address or SP
3440 * Rs: the source register for the operation
3441 * V: vector flag (always 0 as of v8.3)
3442 * A: acquire flag
3443 * R: release flag
3445 static void disas_ldst_atomic(DisasContext *s, uint32_t insn,
3446 int size, int rt, bool is_vector)
3448 int rs = extract32(insn, 16, 5);
3449 int rn = extract32(insn, 5, 5);
3450 int o3_opc = extract32(insn, 12, 4);
3451 bool r = extract32(insn, 22, 1);
3452 bool a = extract32(insn, 23, 1);
3453 TCGv_i64 tcg_rs, tcg_rt, clean_addr;
3454 AtomicThreeOpFn *fn = NULL;
3455 MemOp mop = s->be_data | size | MO_ALIGN;
3457 if (is_vector || !dc_isar_feature(aa64_atomics, s)) {
3458 unallocated_encoding(s);
3459 return;
3461 switch (o3_opc) {
3462 case 000: /* LDADD */
3463 fn = tcg_gen_atomic_fetch_add_i64;
3464 break;
3465 case 001: /* LDCLR */
3466 fn = tcg_gen_atomic_fetch_and_i64;
3467 break;
3468 case 002: /* LDEOR */
3469 fn = tcg_gen_atomic_fetch_xor_i64;
3470 break;
3471 case 003: /* LDSET */
3472 fn = tcg_gen_atomic_fetch_or_i64;
3473 break;
3474 case 004: /* LDSMAX */
3475 fn = tcg_gen_atomic_fetch_smax_i64;
3476 mop |= MO_SIGN;
3477 break;
3478 case 005: /* LDSMIN */
3479 fn = tcg_gen_atomic_fetch_smin_i64;
3480 mop |= MO_SIGN;
3481 break;
3482 case 006: /* LDUMAX */
3483 fn = tcg_gen_atomic_fetch_umax_i64;
3484 break;
3485 case 007: /* LDUMIN */
3486 fn = tcg_gen_atomic_fetch_umin_i64;
3487 break;
3488 case 010: /* SWP */
3489 fn = tcg_gen_atomic_xchg_i64;
3490 break;
3491 case 014: /* LDAPR, LDAPRH, LDAPRB */
3492 if (!dc_isar_feature(aa64_rcpc_8_3, s) ||
3493 rs != 31 || a != 1 || r != 0) {
3494 unallocated_encoding(s);
3495 return;
3497 break;
3498 default:
3499 unallocated_encoding(s);
3500 return;
3503 if (rn == 31) {
3504 gen_check_sp_alignment(s);
3506 clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn), false, rn != 31, size);
3508 if (o3_opc == 014) {
3510 * LDAPR* are a special case because they are a simple load, not a
3511 * fetch-and-do-something op.
3512 * The architectural consistency requirements here are weaker than
3513 * full load-acquire (we only need "load-acquire processor consistent"),
3514 * but we choose to implement them as full LDAQ.
3516 do_gpr_ld(s, cpu_reg(s, rt), clean_addr, size, false,
3517 true, rt, disas_ldst_compute_iss_sf(size, false, 0), true);
3518 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
3519 return;
3522 tcg_rs = read_cpu_reg(s, rs, true);
3523 tcg_rt = cpu_reg(s, rt);
3525 if (o3_opc == 1) { /* LDCLR */
3526 tcg_gen_not_i64(tcg_rs, tcg_rs);
3529 /* The tcg atomic primitives are all full barriers. Therefore we
3530 * can ignore the Acquire and Release bits of this instruction.
3532 fn(tcg_rt, clean_addr, tcg_rs, get_mem_index(s), mop);
3534 if ((mop & MO_SIGN) && size != MO_64) {
3535 tcg_gen_ext32u_i64(tcg_rt, tcg_rt);
3540 * PAC memory operations
3542 * 31 30 27 26 24 22 21 12 11 10 5 0
3543 * +------+-------+---+-----+-----+---+--------+---+---+----+-----+
3544 * | size | 1 1 1 | V | 0 0 | M S | 1 | imm9 | W | 1 | Rn | Rt |
3545 * +------+-------+---+-----+-----+---+--------+---+---+----+-----+
3547 * Rt: the result register
3548 * Rn: base address or SP
3549 * V: vector flag (always 0 as of v8.3)
3550 * M: clear for key DA, set for key DB
3551 * W: pre-indexing flag
3552 * S: sign for imm9.
3554 static void disas_ldst_pac(DisasContext *s, uint32_t insn,
3555 int size, int rt, bool is_vector)
3557 int rn = extract32(insn, 5, 5);
3558 bool is_wback = extract32(insn, 11, 1);
3559 bool use_key_a = !extract32(insn, 23, 1);
3560 int offset;
3561 TCGv_i64 clean_addr, dirty_addr, tcg_rt;
3563 if (size != 3 || is_vector || !dc_isar_feature(aa64_pauth, s)) {
3564 unallocated_encoding(s);
3565 return;
3568 if (rn == 31) {
3569 gen_check_sp_alignment(s);
3571 dirty_addr = read_cpu_reg_sp(s, rn, 1);
3573 if (s->pauth_active) {
3574 if (use_key_a) {
3575 gen_helper_autda(dirty_addr, cpu_env, dirty_addr,
3576 new_tmp_a64_zero(s));
3577 } else {
3578 gen_helper_autdb(dirty_addr, cpu_env, dirty_addr,
3579 new_tmp_a64_zero(s));
3583 /* Form the 10-bit signed, scaled offset. */
3584 offset = (extract32(insn, 22, 1) << 9) | extract32(insn, 12, 9);
3585 offset = sextract32(offset << size, 0, 10 + size);
3586 tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3588 /* Note that "clean" and "dirty" here refer to TBI not PAC. */
3589 clean_addr = gen_mte_check1(s, dirty_addr, false,
3590 is_wback || rn != 31, size);
3592 tcg_rt = cpu_reg(s, rt);
3593 do_gpr_ld(s, tcg_rt, clean_addr, size,
3594 /* extend */ false, /* iss_valid */ !is_wback,
3595 /* iss_srt */ rt, /* iss_sf */ true, /* iss_ar */ false);
3597 if (is_wback) {
3598 tcg_gen_mov_i64(cpu_reg_sp(s, rn), dirty_addr);
3603 * LDAPR/STLR (unscaled immediate)
3605 * 31 30 24 22 21 12 10 5 0
3606 * +------+-------------+-----+---+--------+-----+----+-----+
3607 * | size | 0 1 1 0 0 1 | opc | 0 | imm9 | 0 0 | Rn | Rt |
3608 * +------+-------------+-----+---+--------+-----+----+-----+
3610 * Rt: source or destination register
3611 * Rn: base register
3612 * imm9: unscaled immediate offset
3613 * opc: 00: STLUR*, 01/10/11: various LDAPUR*
3614 * size: size of load/store
3616 static void disas_ldst_ldapr_stlr(DisasContext *s, uint32_t insn)
3618 int rt = extract32(insn, 0, 5);
3619 int rn = extract32(insn, 5, 5);
3620 int offset = sextract32(insn, 12, 9);
3621 int opc = extract32(insn, 22, 2);
3622 int size = extract32(insn, 30, 2);
3623 TCGv_i64 clean_addr, dirty_addr;
3624 bool is_store = false;
3625 bool extend = false;
3626 bool iss_sf;
3627 MemOp mop;
3629 if (!dc_isar_feature(aa64_rcpc_8_4, s)) {
3630 unallocated_encoding(s);
3631 return;
3634 /* TODO: ARMv8.4-LSE SCTLR.nAA */
3635 mop = size | MO_ALIGN;
3637 switch (opc) {
3638 case 0: /* STLURB */
3639 is_store = true;
3640 break;
3641 case 1: /* LDAPUR* */
3642 break;
3643 case 2: /* LDAPURS* 64-bit variant */
3644 if (size == 3) {
3645 unallocated_encoding(s);
3646 return;
3648 mop |= MO_SIGN;
3649 break;
3650 case 3: /* LDAPURS* 32-bit variant */
3651 if (size > 1) {
3652 unallocated_encoding(s);
3653 return;
3655 mop |= MO_SIGN;
3656 extend = true; /* zero-extend 32->64 after signed load */
3657 break;
3658 default:
3659 g_assert_not_reached();
3662 iss_sf = disas_ldst_compute_iss_sf(size, (mop & MO_SIGN) != 0, opc);
3664 if (rn == 31) {
3665 gen_check_sp_alignment(s);
3668 dirty_addr = read_cpu_reg_sp(s, rn, 1);
3669 tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3670 clean_addr = clean_data_tbi(s, dirty_addr);
3672 if (is_store) {
3673 /* Store-Release semantics */
3674 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
3675 do_gpr_st(s, cpu_reg(s, rt), clean_addr, mop, true, rt, iss_sf, true);
3676 } else {
3678 * Load-AcquirePC semantics; we implement as the slightly more
3679 * restrictive Load-Acquire.
3681 do_gpr_ld(s, cpu_reg(s, rt), clean_addr, mop,
3682 extend, true, rt, iss_sf, true);
3683 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
3687 /* Load/store register (all forms) */
3688 static void disas_ldst_reg(DisasContext *s, uint32_t insn)
3690 int rt = extract32(insn, 0, 5);
3691 int opc = extract32(insn, 22, 2);
3692 bool is_vector = extract32(insn, 26, 1);
3693 int size = extract32(insn, 30, 2);
3695 switch (extract32(insn, 24, 2)) {
3696 case 0:
3697 if (extract32(insn, 21, 1) == 0) {
3698 /* Load/store register (unscaled immediate)
3699 * Load/store immediate pre/post-indexed
3700 * Load/store register unprivileged
3702 disas_ldst_reg_imm9(s, insn, opc, size, rt, is_vector);
3703 return;
3705 switch (extract32(insn, 10, 2)) {
3706 case 0:
3707 disas_ldst_atomic(s, insn, size, rt, is_vector);
3708 return;
3709 case 2:
3710 disas_ldst_reg_roffset(s, insn, opc, size, rt, is_vector);
3711 return;
3712 default:
3713 disas_ldst_pac(s, insn, size, rt, is_vector);
3714 return;
3716 break;
3717 case 1:
3718 disas_ldst_reg_unsigned_imm(s, insn, opc, size, rt, is_vector);
3719 return;
3721 unallocated_encoding(s);
3724 /* AdvSIMD load/store multiple structures
3726 * 31 30 29 23 22 21 16 15 12 11 10 9 5 4 0
3727 * +---+---+---------------+---+-------------+--------+------+------+------+
3728 * | 0 | Q | 0 0 1 1 0 0 0 | L | 0 0 0 0 0 0 | opcode | size | Rn | Rt |
3729 * +---+---+---------------+---+-------------+--------+------+------+------+
3731 * AdvSIMD load/store multiple structures (post-indexed)
3733 * 31 30 29 23 22 21 20 16 15 12 11 10 9 5 4 0
3734 * +---+---+---------------+---+---+---------+--------+------+------+------+
3735 * | 0 | Q | 0 0 1 1 0 0 1 | L | 0 | Rm | opcode | size | Rn | Rt |
3736 * +---+---+---------------+---+---+---------+--------+------+------+------+
3738 * Rt: first (or only) SIMD&FP register to be transferred
3739 * Rn: base address or SP
3740 * Rm (post-index only): post-index register (when !31) or size dependent #imm
3742 static void disas_ldst_multiple_struct(DisasContext *s, uint32_t insn)
3744 int rt = extract32(insn, 0, 5);
3745 int rn = extract32(insn, 5, 5);
3746 int rm = extract32(insn, 16, 5);
3747 int size = extract32(insn, 10, 2);
3748 int opcode = extract32(insn, 12, 4);
3749 bool is_store = !extract32(insn, 22, 1);
3750 bool is_postidx = extract32(insn, 23, 1);
3751 bool is_q = extract32(insn, 30, 1);
3752 TCGv_i64 clean_addr, tcg_rn, tcg_ebytes;
3753 MemOp endian, align, mop;
3755 int total; /* total bytes */
3756 int elements; /* elements per vector */
3757 int rpt; /* num iterations */
3758 int selem; /* structure elements */
3759 int r;
3761 if (extract32(insn, 31, 1) || extract32(insn, 21, 1)) {
3762 unallocated_encoding(s);
3763 return;
3766 if (!is_postidx && rm != 0) {
3767 unallocated_encoding(s);
3768 return;
3771 /* From the shared decode logic */
3772 switch (opcode) {
3773 case 0x0:
3774 rpt = 1;
3775 selem = 4;
3776 break;
3777 case 0x2:
3778 rpt = 4;
3779 selem = 1;
3780 break;
3781 case 0x4:
3782 rpt = 1;
3783 selem = 3;
3784 break;
3785 case 0x6:
3786 rpt = 3;
3787 selem = 1;
3788 break;
3789 case 0x7:
3790 rpt = 1;
3791 selem = 1;
3792 break;
3793 case 0x8:
3794 rpt = 1;
3795 selem = 2;
3796 break;
3797 case 0xa:
3798 rpt = 2;
3799 selem = 1;
3800 break;
3801 default:
3802 unallocated_encoding(s);
3803 return;
3806 if (size == 3 && !is_q && selem != 1) {
3807 /* reserved */
3808 unallocated_encoding(s);
3809 return;
3812 if (!fp_access_check(s)) {
3813 return;
3816 if (rn == 31) {
3817 gen_check_sp_alignment(s);
3820 /* For our purposes, bytes are always little-endian. */
3821 endian = s->be_data;
3822 if (size == 0) {
3823 endian = MO_LE;
3826 total = rpt * selem * (is_q ? 16 : 8);
3827 tcg_rn = cpu_reg_sp(s, rn);
3830 * Issue the MTE check vs the logical repeat count, before we
3831 * promote consecutive little-endian elements below.
3833 clean_addr = gen_mte_checkN(s, tcg_rn, is_store, is_postidx || rn != 31,
3834 total);
3837 * Consecutive little-endian elements from a single register
3838 * can be promoted to a larger little-endian operation.
3840 align = MO_ALIGN;
3841 if (selem == 1 && endian == MO_LE) {
3842 align = pow2_align(size);
3843 size = 3;
3845 if (!s->align_mem) {
3846 align = 0;
3848 mop = endian | size | align;
3850 elements = (is_q ? 16 : 8) >> size;
3851 tcg_ebytes = tcg_constant_i64(1 << size);
3852 for (r = 0; r < rpt; r++) {
3853 int e;
3854 for (e = 0; e < elements; e++) {
3855 int xs;
3856 for (xs = 0; xs < selem; xs++) {
3857 int tt = (rt + r + xs) % 32;
3858 if (is_store) {
3859 do_vec_st(s, tt, e, clean_addr, mop);
3860 } else {
3861 do_vec_ld(s, tt, e, clean_addr, mop);
3863 tcg_gen_add_i64(clean_addr, clean_addr, tcg_ebytes);
3868 if (!is_store) {
3869 /* For non-quad operations, setting a slice of the low
3870 * 64 bits of the register clears the high 64 bits (in
3871 * the ARM ARM pseudocode this is implicit in the fact
3872 * that 'rval' is a 64 bit wide variable).
3873 * For quad operations, we might still need to zero the
3874 * high bits of SVE.
3876 for (r = 0; r < rpt * selem; r++) {
3877 int tt = (rt + r) % 32;
3878 clear_vec_high(s, is_q, tt);
3882 if (is_postidx) {
3883 if (rm == 31) {
3884 tcg_gen_addi_i64(tcg_rn, tcg_rn, total);
3885 } else {
3886 tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, rm));
3891 /* AdvSIMD load/store single structure
3893 * 31 30 29 23 22 21 20 16 15 13 12 11 10 9 5 4 0
3894 * +---+---+---------------+-----+-----------+-----+---+------+------+------+
3895 * | 0 | Q | 0 0 1 1 0 1 0 | L R | 0 0 0 0 0 | opc | S | size | Rn | Rt |
3896 * +---+---+---------------+-----+-----------+-----+---+------+------+------+
3898 * AdvSIMD load/store single structure (post-indexed)
3900 * 31 30 29 23 22 21 20 16 15 13 12 11 10 9 5 4 0
3901 * +---+---+---------------+-----+-----------+-----+---+------+------+------+
3902 * | 0 | Q | 0 0 1 1 0 1 1 | L R | Rm | opc | S | size | Rn | Rt |
3903 * +---+---+---------------+-----+-----------+-----+---+------+------+------+
3905 * Rt: first (or only) SIMD&FP register to be transferred
3906 * Rn: base address or SP
3907 * Rm (post-index only): post-index register (when !31) or size dependent #imm
3908 * index = encoded in Q:S:size dependent on size
3910 * lane_size = encoded in R, opc
3911 * transfer width = encoded in opc, S, size
3913 static void disas_ldst_single_struct(DisasContext *s, uint32_t insn)
3915 int rt = extract32(insn, 0, 5);
3916 int rn = extract32(insn, 5, 5);
3917 int rm = extract32(insn, 16, 5);
3918 int size = extract32(insn, 10, 2);
3919 int S = extract32(insn, 12, 1);
3920 int opc = extract32(insn, 13, 3);
3921 int R = extract32(insn, 21, 1);
3922 int is_load = extract32(insn, 22, 1);
3923 int is_postidx = extract32(insn, 23, 1);
3924 int is_q = extract32(insn, 30, 1);
3926 int scale = extract32(opc, 1, 2);
3927 int selem = (extract32(opc, 0, 1) << 1 | R) + 1;
3928 bool replicate = false;
3929 int index = is_q << 3 | S << 2 | size;
3930 int xs, total;
3931 TCGv_i64 clean_addr, tcg_rn, tcg_ebytes;
3932 MemOp mop;
3934 if (extract32(insn, 31, 1)) {
3935 unallocated_encoding(s);
3936 return;
3938 if (!is_postidx && rm != 0) {
3939 unallocated_encoding(s);
3940 return;
3943 switch (scale) {
3944 case 3:
3945 if (!is_load || S) {
3946 unallocated_encoding(s);
3947 return;
3949 scale = size;
3950 replicate = true;
3951 break;
3952 case 0:
3953 break;
3954 case 1:
3955 if (extract32(size, 0, 1)) {
3956 unallocated_encoding(s);
3957 return;
3959 index >>= 1;
3960 break;
3961 case 2:
3962 if (extract32(size, 1, 1)) {
3963 unallocated_encoding(s);
3964 return;
3966 if (!extract32(size, 0, 1)) {
3967 index >>= 2;
3968 } else {
3969 if (S) {
3970 unallocated_encoding(s);
3971 return;
3973 index >>= 3;
3974 scale = 3;
3976 break;
3977 default:
3978 g_assert_not_reached();
3981 if (!fp_access_check(s)) {
3982 return;
3985 if (rn == 31) {
3986 gen_check_sp_alignment(s);
3989 total = selem << scale;
3990 tcg_rn = cpu_reg_sp(s, rn);
3992 clean_addr = gen_mte_checkN(s, tcg_rn, !is_load, is_postidx || rn != 31,
3993 total);
3994 mop = finalize_memop(s, scale);
3996 tcg_ebytes = tcg_constant_i64(1 << scale);
3997 for (xs = 0; xs < selem; xs++) {
3998 if (replicate) {
3999 /* Load and replicate to all elements */
4000 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
4002 tcg_gen_qemu_ld_i64(tcg_tmp, clean_addr, get_mem_index(s), mop);
4003 tcg_gen_gvec_dup_i64(scale, vec_full_reg_offset(s, rt),
4004 (is_q + 1) * 8, vec_full_reg_size(s),
4005 tcg_tmp);
4006 tcg_temp_free_i64(tcg_tmp);
4007 } else {
4008 /* Load/store one element per register */
4009 if (is_load) {
4010 do_vec_ld(s, rt, index, clean_addr, mop);
4011 } else {
4012 do_vec_st(s, rt, index, clean_addr, mop);
4015 tcg_gen_add_i64(clean_addr, clean_addr, tcg_ebytes);
4016 rt = (rt + 1) % 32;
4019 if (is_postidx) {
4020 if (rm == 31) {
4021 tcg_gen_addi_i64(tcg_rn, tcg_rn, total);
4022 } else {
4023 tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, rm));
4029 * Load/Store memory tags
4031 * 31 30 29 24 22 21 12 10 5 0
4032 * +-----+-------------+-----+---+------+-----+------+------+
4033 * | 1 1 | 0 1 1 0 0 1 | op1 | 1 | imm9 | op2 | Rn | Rt |
4034 * +-----+-------------+-----+---+------+-----+------+------+
4036 static void disas_ldst_tag(DisasContext *s, uint32_t insn)
4038 int rt = extract32(insn, 0, 5);
4039 int rn = extract32(insn, 5, 5);
4040 uint64_t offset = sextract64(insn, 12, 9) << LOG2_TAG_GRANULE;
4041 int op2 = extract32(insn, 10, 2);
4042 int op1 = extract32(insn, 22, 2);
4043 bool is_load = false, is_pair = false, is_zero = false, is_mult = false;
4044 int index = 0;
4045 TCGv_i64 addr, clean_addr, tcg_rt;
4047 /* We checked insn bits [29:24,21] in the caller. */
4048 if (extract32(insn, 30, 2) != 3) {
4049 goto do_unallocated;
4053 * @index is a tri-state variable which has 3 states:
4054 * < 0 : post-index, writeback
4055 * = 0 : signed offset
4056 * > 0 : pre-index, writeback
4058 switch (op1) {
4059 case 0:
4060 if (op2 != 0) {
4061 /* STG */
4062 index = op2 - 2;
4063 } else {
4064 /* STZGM */
4065 if (s->current_el == 0 || offset != 0) {
4066 goto do_unallocated;
4068 is_mult = is_zero = true;
4070 break;
4071 case 1:
4072 if (op2 != 0) {
4073 /* STZG */
4074 is_zero = true;
4075 index = op2 - 2;
4076 } else {
4077 /* LDG */
4078 is_load = true;
4080 break;
4081 case 2:
4082 if (op2 != 0) {
4083 /* ST2G */
4084 is_pair = true;
4085 index = op2 - 2;
4086 } else {
4087 /* STGM */
4088 if (s->current_el == 0 || offset != 0) {
4089 goto do_unallocated;
4091 is_mult = true;
4093 break;
4094 case 3:
4095 if (op2 != 0) {
4096 /* STZ2G */
4097 is_pair = is_zero = true;
4098 index = op2 - 2;
4099 } else {
4100 /* LDGM */
4101 if (s->current_el == 0 || offset != 0) {
4102 goto do_unallocated;
4104 is_mult = is_load = true;
4106 break;
4108 default:
4109 do_unallocated:
4110 unallocated_encoding(s);
4111 return;
4114 if (is_mult
4115 ? !dc_isar_feature(aa64_mte, s)
4116 : !dc_isar_feature(aa64_mte_insn_reg, s)) {
4117 goto do_unallocated;
4120 if (rn == 31) {
4121 gen_check_sp_alignment(s);
4124 addr = read_cpu_reg_sp(s, rn, true);
4125 if (index >= 0) {
4126 /* pre-index or signed offset */
4127 tcg_gen_addi_i64(addr, addr, offset);
4130 if (is_mult) {
4131 tcg_rt = cpu_reg(s, rt);
4133 if (is_zero) {
4134 int size = 4 << s->dcz_blocksize;
4136 if (s->ata) {
4137 gen_helper_stzgm_tags(cpu_env, addr, tcg_rt);
4140 * The non-tags portion of STZGM is mostly like DC_ZVA,
4141 * except the alignment happens before the access.
4143 clean_addr = clean_data_tbi(s, addr);
4144 tcg_gen_andi_i64(clean_addr, clean_addr, -size);
4145 gen_helper_dc_zva(cpu_env, clean_addr);
4146 } else if (s->ata) {
4147 if (is_load) {
4148 gen_helper_ldgm(tcg_rt, cpu_env, addr);
4149 } else {
4150 gen_helper_stgm(cpu_env, addr, tcg_rt);
4152 } else {
4153 MMUAccessType acc = is_load ? MMU_DATA_LOAD : MMU_DATA_STORE;
4154 int size = 4 << GMID_EL1_BS;
4156 clean_addr = clean_data_tbi(s, addr);
4157 tcg_gen_andi_i64(clean_addr, clean_addr, -size);
4158 gen_probe_access(s, clean_addr, acc, size);
4160 if (is_load) {
4161 /* The result tags are zeros. */
4162 tcg_gen_movi_i64(tcg_rt, 0);
4165 return;
4168 if (is_load) {
4169 tcg_gen_andi_i64(addr, addr, -TAG_GRANULE);
4170 tcg_rt = cpu_reg(s, rt);
4171 if (s->ata) {
4172 gen_helper_ldg(tcg_rt, cpu_env, addr, tcg_rt);
4173 } else {
4174 clean_addr = clean_data_tbi(s, addr);
4175 gen_probe_access(s, clean_addr, MMU_DATA_LOAD, MO_8);
4176 gen_address_with_allocation_tag0(tcg_rt, addr);
4178 } else {
4179 tcg_rt = cpu_reg_sp(s, rt);
4180 if (!s->ata) {
4182 * For STG and ST2G, we need to check alignment and probe memory.
4183 * TODO: For STZG and STZ2G, we could rely on the stores below,
4184 * at least for system mode; user-only won't enforce alignment.
4186 if (is_pair) {
4187 gen_helper_st2g_stub(cpu_env, addr);
4188 } else {
4189 gen_helper_stg_stub(cpu_env, addr);
4191 } else if (tb_cflags(s->base.tb) & CF_PARALLEL) {
4192 if (is_pair) {
4193 gen_helper_st2g_parallel(cpu_env, addr, tcg_rt);
4194 } else {
4195 gen_helper_stg_parallel(cpu_env, addr, tcg_rt);
4197 } else {
4198 if (is_pair) {
4199 gen_helper_st2g(cpu_env, addr, tcg_rt);
4200 } else {
4201 gen_helper_stg(cpu_env, addr, tcg_rt);
4206 if (is_zero) {
4207 TCGv_i64 clean_addr = clean_data_tbi(s, addr);
4208 TCGv_i64 tcg_zero = tcg_constant_i64(0);
4209 int mem_index = get_mem_index(s);
4210 int i, n = (1 + is_pair) << LOG2_TAG_GRANULE;
4212 tcg_gen_qemu_st_i64(tcg_zero, clean_addr, mem_index,
4213 MO_UQ | MO_ALIGN_16);
4214 for (i = 8; i < n; i += 8) {
4215 tcg_gen_addi_i64(clean_addr, clean_addr, 8);
4216 tcg_gen_qemu_st_i64(tcg_zero, clean_addr, mem_index, MO_UQ);
4220 if (index != 0) {
4221 /* pre-index or post-index */
4222 if (index < 0) {
4223 /* post-index */
4224 tcg_gen_addi_i64(addr, addr, offset);
4226 tcg_gen_mov_i64(cpu_reg_sp(s, rn), addr);
4230 /* Loads and stores */
4231 static void disas_ldst(DisasContext *s, uint32_t insn)
4233 switch (extract32(insn, 24, 6)) {
4234 case 0x08: /* Load/store exclusive */
4235 disas_ldst_excl(s, insn);
4236 break;
4237 case 0x18: case 0x1c: /* Load register (literal) */
4238 disas_ld_lit(s, insn);
4239 break;
4240 case 0x28: case 0x29:
4241 case 0x2c: case 0x2d: /* Load/store pair (all forms) */
4242 disas_ldst_pair(s, insn);
4243 break;
4244 case 0x38: case 0x39:
4245 case 0x3c: case 0x3d: /* Load/store register (all forms) */
4246 disas_ldst_reg(s, insn);
4247 break;
4248 case 0x0c: /* AdvSIMD load/store multiple structures */
4249 disas_ldst_multiple_struct(s, insn);
4250 break;
4251 case 0x0d: /* AdvSIMD load/store single structure */
4252 disas_ldst_single_struct(s, insn);
4253 break;
4254 case 0x19:
4255 if (extract32(insn, 21, 1) != 0) {
4256 disas_ldst_tag(s, insn);
4257 } else if (extract32(insn, 10, 2) == 0) {
4258 disas_ldst_ldapr_stlr(s, insn);
4259 } else {
4260 unallocated_encoding(s);
4262 break;
4263 default:
4264 unallocated_encoding(s);
4265 break;
4269 /* PC-rel. addressing
4270 * 31 30 29 28 24 23 5 4 0
4271 * +----+-------+-----------+-------------------+------+
4272 * | op | immlo | 1 0 0 0 0 | immhi | Rd |
4273 * +----+-------+-----------+-------------------+------+
4275 static void disas_pc_rel_adr(DisasContext *s, uint32_t insn)
4277 unsigned int page, rd;
4278 int64_t offset;
4280 page = extract32(insn, 31, 1);
4281 /* SignExtend(immhi:immlo) -> offset */
4282 offset = sextract64(insn, 5, 19);
4283 offset = offset << 2 | extract32(insn, 29, 2);
4284 rd = extract32(insn, 0, 5);
4286 if (page) {
4287 /* ADRP (page based) */
4288 offset <<= 12;
4289 /* The page offset is ok for CF_PCREL. */
4290 offset -= s->pc_curr & 0xfff;
4293 gen_pc_plus_diff(s, cpu_reg(s, rd), offset);
4297 * Add/subtract (immediate)
4299 * 31 30 29 28 23 22 21 10 9 5 4 0
4300 * +--+--+--+-------------+--+-------------+-----+-----+
4301 * |sf|op| S| 1 0 0 0 1 0 |sh| imm12 | Rn | Rd |
4302 * +--+--+--+-------------+--+-------------+-----+-----+
4304 * sf: 0 -> 32bit, 1 -> 64bit
4305 * op: 0 -> add , 1 -> sub
4306 * S: 1 -> set flags
4307 * sh: 1 -> LSL imm by 12
4309 static void disas_add_sub_imm(DisasContext *s, uint32_t insn)
4311 int rd = extract32(insn, 0, 5);
4312 int rn = extract32(insn, 5, 5);
4313 uint64_t imm = extract32(insn, 10, 12);
4314 bool shift = extract32(insn, 22, 1);
4315 bool setflags = extract32(insn, 29, 1);
4316 bool sub_op = extract32(insn, 30, 1);
4317 bool is_64bit = extract32(insn, 31, 1);
4319 TCGv_i64 tcg_rn = cpu_reg_sp(s, rn);
4320 TCGv_i64 tcg_rd = setflags ? cpu_reg(s, rd) : cpu_reg_sp(s, rd);
4321 TCGv_i64 tcg_result;
4323 if (shift) {
4324 imm <<= 12;
4327 tcg_result = tcg_temp_new_i64();
4328 if (!setflags) {
4329 if (sub_op) {
4330 tcg_gen_subi_i64(tcg_result, tcg_rn, imm);
4331 } else {
4332 tcg_gen_addi_i64(tcg_result, tcg_rn, imm);
4334 } else {
4335 TCGv_i64 tcg_imm = tcg_constant_i64(imm);
4336 if (sub_op) {
4337 gen_sub_CC(is_64bit, tcg_result, tcg_rn, tcg_imm);
4338 } else {
4339 gen_add_CC(is_64bit, tcg_result, tcg_rn, tcg_imm);
4343 if (is_64bit) {
4344 tcg_gen_mov_i64(tcg_rd, tcg_result);
4345 } else {
4346 tcg_gen_ext32u_i64(tcg_rd, tcg_result);
4349 tcg_temp_free_i64(tcg_result);
4353 * Add/subtract (immediate, with tags)
4355 * 31 30 29 28 23 22 21 16 14 10 9 5 4 0
4356 * +--+--+--+-------------+--+---------+--+-------+-----+-----+
4357 * |sf|op| S| 1 0 0 0 1 1 |o2| uimm6 |o3| uimm4 | Rn | Rd |
4358 * +--+--+--+-------------+--+---------+--+-------+-----+-----+
4360 * op: 0 -> add, 1 -> sub
4362 static void disas_add_sub_imm_with_tags(DisasContext *s, uint32_t insn)
4364 int rd = extract32(insn, 0, 5);
4365 int rn = extract32(insn, 5, 5);
4366 int uimm4 = extract32(insn, 10, 4);
4367 int uimm6 = extract32(insn, 16, 6);
4368 bool sub_op = extract32(insn, 30, 1);
4369 TCGv_i64 tcg_rn, tcg_rd;
4370 int imm;
4372 /* Test all of sf=1, S=0, o2=0, o3=0. */
4373 if ((insn & 0xa040c000u) != 0x80000000u ||
4374 !dc_isar_feature(aa64_mte_insn_reg, s)) {
4375 unallocated_encoding(s);
4376 return;
4379 imm = uimm6 << LOG2_TAG_GRANULE;
4380 if (sub_op) {
4381 imm = -imm;
4384 tcg_rn = cpu_reg_sp(s, rn);
4385 tcg_rd = cpu_reg_sp(s, rd);
4387 if (s->ata) {
4388 gen_helper_addsubg(tcg_rd, cpu_env, tcg_rn,
4389 tcg_constant_i32(imm),
4390 tcg_constant_i32(uimm4));
4391 } else {
4392 tcg_gen_addi_i64(tcg_rd, tcg_rn, imm);
4393 gen_address_with_allocation_tag0(tcg_rd, tcg_rd);
4397 /* The input should be a value in the bottom e bits (with higher
4398 * bits zero); returns that value replicated into every element
4399 * of size e in a 64 bit integer.
4401 static uint64_t bitfield_replicate(uint64_t mask, unsigned int e)
4403 assert(e != 0);
4404 while (e < 64) {
4405 mask |= mask << e;
4406 e *= 2;
4408 return mask;
4411 /* Return a value with the bottom len bits set (where 0 < len <= 64) */
4412 static inline uint64_t bitmask64(unsigned int length)
4414 assert(length > 0 && length <= 64);
4415 return ~0ULL >> (64 - length);
4418 /* Simplified variant of pseudocode DecodeBitMasks() for the case where we
4419 * only require the wmask. Returns false if the imms/immr/immn are a reserved
4420 * value (ie should cause a guest UNDEF exception), and true if they are
4421 * valid, in which case the decoded bit pattern is written to result.
4423 bool logic_imm_decode_wmask(uint64_t *result, unsigned int immn,
4424 unsigned int imms, unsigned int immr)
4426 uint64_t mask;
4427 unsigned e, levels, s, r;
4428 int len;
4430 assert(immn < 2 && imms < 64 && immr < 64);
4432 /* The bit patterns we create here are 64 bit patterns which
4433 * are vectors of identical elements of size e = 2, 4, 8, 16, 32 or
4434 * 64 bits each. Each element contains the same value: a run
4435 * of between 1 and e-1 non-zero bits, rotated within the
4436 * element by between 0 and e-1 bits.
4438 * The element size and run length are encoded into immn (1 bit)
4439 * and imms (6 bits) as follows:
4440 * 64 bit elements: immn = 1, imms = <length of run - 1>
4441 * 32 bit elements: immn = 0, imms = 0 : <length of run - 1>
4442 * 16 bit elements: immn = 0, imms = 10 : <length of run - 1>
4443 * 8 bit elements: immn = 0, imms = 110 : <length of run - 1>
4444 * 4 bit elements: immn = 0, imms = 1110 : <length of run - 1>
4445 * 2 bit elements: immn = 0, imms = 11110 : <length of run - 1>
4446 * Notice that immn = 0, imms = 11111x is the only combination
4447 * not covered by one of the above options; this is reserved.
4448 * Further, <length of run - 1> all-ones is a reserved pattern.
4450 * In all cases the rotation is by immr % e (and immr is 6 bits).
4453 /* First determine the element size */
4454 len = 31 - clz32((immn << 6) | (~imms & 0x3f));
4455 if (len < 1) {
4456 /* This is the immn == 0, imms == 0x11111x case */
4457 return false;
4459 e = 1 << len;
4461 levels = e - 1;
4462 s = imms & levels;
4463 r = immr & levels;
4465 if (s == levels) {
4466 /* <length of run - 1> mustn't be all-ones. */
4467 return false;
4470 /* Create the value of one element: s+1 set bits rotated
4471 * by r within the element (which is e bits wide)...
4473 mask = bitmask64(s + 1);
4474 if (r) {
4475 mask = (mask >> r) | (mask << (e - r));
4476 mask &= bitmask64(e);
4478 /* ...then replicate the element over the whole 64 bit value */
4479 mask = bitfield_replicate(mask, e);
4480 *result = mask;
4481 return true;
4484 /* Logical (immediate)
4485 * 31 30 29 28 23 22 21 16 15 10 9 5 4 0
4486 * +----+-----+-------------+---+------+------+------+------+
4487 * | sf | opc | 1 0 0 1 0 0 | N | immr | imms | Rn | Rd |
4488 * +----+-----+-------------+---+------+------+------+------+
4490 static void disas_logic_imm(DisasContext *s, uint32_t insn)
4492 unsigned int sf, opc, is_n, immr, imms, rn, rd;
4493 TCGv_i64 tcg_rd, tcg_rn;
4494 uint64_t wmask;
4495 bool is_and = false;
4497 sf = extract32(insn, 31, 1);
4498 opc = extract32(insn, 29, 2);
4499 is_n = extract32(insn, 22, 1);
4500 immr = extract32(insn, 16, 6);
4501 imms = extract32(insn, 10, 6);
4502 rn = extract32(insn, 5, 5);
4503 rd = extract32(insn, 0, 5);
4505 if (!sf && is_n) {
4506 unallocated_encoding(s);
4507 return;
4510 if (opc == 0x3) { /* ANDS */
4511 tcg_rd = cpu_reg(s, rd);
4512 } else {
4513 tcg_rd = cpu_reg_sp(s, rd);
4515 tcg_rn = cpu_reg(s, rn);
4517 if (!logic_imm_decode_wmask(&wmask, is_n, imms, immr)) {
4518 /* some immediate field values are reserved */
4519 unallocated_encoding(s);
4520 return;
4523 if (!sf) {
4524 wmask &= 0xffffffff;
4527 switch (opc) {
4528 case 0x3: /* ANDS */
4529 case 0x0: /* AND */
4530 tcg_gen_andi_i64(tcg_rd, tcg_rn, wmask);
4531 is_and = true;
4532 break;
4533 case 0x1: /* ORR */
4534 tcg_gen_ori_i64(tcg_rd, tcg_rn, wmask);
4535 break;
4536 case 0x2: /* EOR */
4537 tcg_gen_xori_i64(tcg_rd, tcg_rn, wmask);
4538 break;
4539 default:
4540 assert(FALSE); /* must handle all above */
4541 break;
4544 if (!sf && !is_and) {
4545 /* zero extend final result; we know we can skip this for AND
4546 * since the immediate had the high 32 bits clear.
4548 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4551 if (opc == 3) { /* ANDS */
4552 gen_logic_CC(sf, tcg_rd);
4557 * Move wide (immediate)
4559 * 31 30 29 28 23 22 21 20 5 4 0
4560 * +--+-----+-------------+-----+----------------+------+
4561 * |sf| opc | 1 0 0 1 0 1 | hw | imm16 | Rd |
4562 * +--+-----+-------------+-----+----------------+------+
4564 * sf: 0 -> 32 bit, 1 -> 64 bit
4565 * opc: 00 -> N, 10 -> Z, 11 -> K
4566 * hw: shift/16 (0,16, and sf only 32, 48)
4568 static void disas_movw_imm(DisasContext *s, uint32_t insn)
4570 int rd = extract32(insn, 0, 5);
4571 uint64_t imm = extract32(insn, 5, 16);
4572 int sf = extract32(insn, 31, 1);
4573 int opc = extract32(insn, 29, 2);
4574 int pos = extract32(insn, 21, 2) << 4;
4575 TCGv_i64 tcg_rd = cpu_reg(s, rd);
4577 if (!sf && (pos >= 32)) {
4578 unallocated_encoding(s);
4579 return;
4582 switch (opc) {
4583 case 0: /* MOVN */
4584 case 2: /* MOVZ */
4585 imm <<= pos;
4586 if (opc == 0) {
4587 imm = ~imm;
4589 if (!sf) {
4590 imm &= 0xffffffffu;
4592 tcg_gen_movi_i64(tcg_rd, imm);
4593 break;
4594 case 3: /* MOVK */
4595 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_constant_i64(imm), pos, 16);
4596 if (!sf) {
4597 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4599 break;
4600 default:
4601 unallocated_encoding(s);
4602 break;
4606 /* Bitfield
4607 * 31 30 29 28 23 22 21 16 15 10 9 5 4 0
4608 * +----+-----+-------------+---+------+------+------+------+
4609 * | sf | opc | 1 0 0 1 1 0 | N | immr | imms | Rn | Rd |
4610 * +----+-----+-------------+---+------+------+------+------+
4612 static void disas_bitfield(DisasContext *s, uint32_t insn)
4614 unsigned int sf, n, opc, ri, si, rn, rd, bitsize, pos, len;
4615 TCGv_i64 tcg_rd, tcg_tmp;
4617 sf = extract32(insn, 31, 1);
4618 opc = extract32(insn, 29, 2);
4619 n = extract32(insn, 22, 1);
4620 ri = extract32(insn, 16, 6);
4621 si = extract32(insn, 10, 6);
4622 rn = extract32(insn, 5, 5);
4623 rd = extract32(insn, 0, 5);
4624 bitsize = sf ? 64 : 32;
4626 if (sf != n || ri >= bitsize || si >= bitsize || opc > 2) {
4627 unallocated_encoding(s);
4628 return;
4631 tcg_rd = cpu_reg(s, rd);
4633 /* Suppress the zero-extend for !sf. Since RI and SI are constrained
4634 to be smaller than bitsize, we'll never reference data outside the
4635 low 32-bits anyway. */
4636 tcg_tmp = read_cpu_reg(s, rn, 1);
4638 /* Recognize simple(r) extractions. */
4639 if (si >= ri) {
4640 /* Wd<s-r:0> = Wn<s:r> */
4641 len = (si - ri) + 1;
4642 if (opc == 0) { /* SBFM: ASR, SBFX, SXTB, SXTH, SXTW */
4643 tcg_gen_sextract_i64(tcg_rd, tcg_tmp, ri, len);
4644 goto done;
4645 } else if (opc == 2) { /* UBFM: UBFX, LSR, UXTB, UXTH */
4646 tcg_gen_extract_i64(tcg_rd, tcg_tmp, ri, len);
4647 return;
4649 /* opc == 1, BFXIL fall through to deposit */
4650 tcg_gen_shri_i64(tcg_tmp, tcg_tmp, ri);
4651 pos = 0;
4652 } else {
4653 /* Handle the ri > si case with a deposit
4654 * Wd<32+s-r,32-r> = Wn<s:0>
4656 len = si + 1;
4657 pos = (bitsize - ri) & (bitsize - 1);
4660 if (opc == 0 && len < ri) {
4661 /* SBFM: sign extend the destination field from len to fill
4662 the balance of the word. Let the deposit below insert all
4663 of those sign bits. */
4664 tcg_gen_sextract_i64(tcg_tmp, tcg_tmp, 0, len);
4665 len = ri;
4668 if (opc == 1) { /* BFM, BFXIL */
4669 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, pos, len);
4670 } else {
4671 /* SBFM or UBFM: We start with zero, and we haven't modified
4672 any bits outside bitsize, therefore the zero-extension
4673 below is unneeded. */
4674 tcg_gen_deposit_z_i64(tcg_rd, tcg_tmp, pos, len);
4675 return;
4678 done:
4679 if (!sf) { /* zero extend final result */
4680 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4684 /* Extract
4685 * 31 30 29 28 23 22 21 20 16 15 10 9 5 4 0
4686 * +----+------+-------------+---+----+------+--------+------+------+
4687 * | sf | op21 | 1 0 0 1 1 1 | N | o0 | Rm | imms | Rn | Rd |
4688 * +----+------+-------------+---+----+------+--------+------+------+
4690 static void disas_extract(DisasContext *s, uint32_t insn)
4692 unsigned int sf, n, rm, imm, rn, rd, bitsize, op21, op0;
4694 sf = extract32(insn, 31, 1);
4695 n = extract32(insn, 22, 1);
4696 rm = extract32(insn, 16, 5);
4697 imm = extract32(insn, 10, 6);
4698 rn = extract32(insn, 5, 5);
4699 rd = extract32(insn, 0, 5);
4700 op21 = extract32(insn, 29, 2);
4701 op0 = extract32(insn, 21, 1);
4702 bitsize = sf ? 64 : 32;
4704 if (sf != n || op21 || op0 || imm >= bitsize) {
4705 unallocated_encoding(s);
4706 } else {
4707 TCGv_i64 tcg_rd, tcg_rm, tcg_rn;
4709 tcg_rd = cpu_reg(s, rd);
4711 if (unlikely(imm == 0)) {
4712 /* tcg shl_i32/shl_i64 is undefined for 32/64 bit shifts,
4713 * so an extract from bit 0 is a special case.
4715 if (sf) {
4716 tcg_gen_mov_i64(tcg_rd, cpu_reg(s, rm));
4717 } else {
4718 tcg_gen_ext32u_i64(tcg_rd, cpu_reg(s, rm));
4720 } else {
4721 tcg_rm = cpu_reg(s, rm);
4722 tcg_rn = cpu_reg(s, rn);
4724 if (sf) {
4725 /* Specialization to ROR happens in EXTRACT2. */
4726 tcg_gen_extract2_i64(tcg_rd, tcg_rm, tcg_rn, imm);
4727 } else {
4728 TCGv_i32 t0 = tcg_temp_new_i32();
4730 tcg_gen_extrl_i64_i32(t0, tcg_rm);
4731 if (rm == rn) {
4732 tcg_gen_rotri_i32(t0, t0, imm);
4733 } else {
4734 TCGv_i32 t1 = tcg_temp_new_i32();
4735 tcg_gen_extrl_i64_i32(t1, tcg_rn);
4736 tcg_gen_extract2_i32(t0, t0, t1, imm);
4737 tcg_temp_free_i32(t1);
4739 tcg_gen_extu_i32_i64(tcg_rd, t0);
4740 tcg_temp_free_i32(t0);
4746 /* Data processing - immediate */
4747 static void disas_data_proc_imm(DisasContext *s, uint32_t insn)
4749 switch (extract32(insn, 23, 6)) {
4750 case 0x20: case 0x21: /* PC-rel. addressing */
4751 disas_pc_rel_adr(s, insn);
4752 break;
4753 case 0x22: /* Add/subtract (immediate) */
4754 disas_add_sub_imm(s, insn);
4755 break;
4756 case 0x23: /* Add/subtract (immediate, with tags) */
4757 disas_add_sub_imm_with_tags(s, insn);
4758 break;
4759 case 0x24: /* Logical (immediate) */
4760 disas_logic_imm(s, insn);
4761 break;
4762 case 0x25: /* Move wide (immediate) */
4763 disas_movw_imm(s, insn);
4764 break;
4765 case 0x26: /* Bitfield */
4766 disas_bitfield(s, insn);
4767 break;
4768 case 0x27: /* Extract */
4769 disas_extract(s, insn);
4770 break;
4771 default:
4772 unallocated_encoding(s);
4773 break;
4777 /* Shift a TCGv src by TCGv shift_amount, put result in dst.
4778 * Note that it is the caller's responsibility to ensure that the
4779 * shift amount is in range (ie 0..31 or 0..63) and provide the ARM
4780 * mandated semantics for out of range shifts.
4782 static void shift_reg(TCGv_i64 dst, TCGv_i64 src, int sf,
4783 enum a64_shift_type shift_type, TCGv_i64 shift_amount)
4785 switch (shift_type) {
4786 case A64_SHIFT_TYPE_LSL:
4787 tcg_gen_shl_i64(dst, src, shift_amount);
4788 break;
4789 case A64_SHIFT_TYPE_LSR:
4790 tcg_gen_shr_i64(dst, src, shift_amount);
4791 break;
4792 case A64_SHIFT_TYPE_ASR:
4793 if (!sf) {
4794 tcg_gen_ext32s_i64(dst, src);
4796 tcg_gen_sar_i64(dst, sf ? src : dst, shift_amount);
4797 break;
4798 case A64_SHIFT_TYPE_ROR:
4799 if (sf) {
4800 tcg_gen_rotr_i64(dst, src, shift_amount);
4801 } else {
4802 TCGv_i32 t0, t1;
4803 t0 = tcg_temp_new_i32();
4804 t1 = tcg_temp_new_i32();
4805 tcg_gen_extrl_i64_i32(t0, src);
4806 tcg_gen_extrl_i64_i32(t1, shift_amount);
4807 tcg_gen_rotr_i32(t0, t0, t1);
4808 tcg_gen_extu_i32_i64(dst, t0);
4809 tcg_temp_free_i32(t0);
4810 tcg_temp_free_i32(t1);
4812 break;
4813 default:
4814 assert(FALSE); /* all shift types should be handled */
4815 break;
4818 if (!sf) { /* zero extend final result */
4819 tcg_gen_ext32u_i64(dst, dst);
4823 /* Shift a TCGv src by immediate, put result in dst.
4824 * The shift amount must be in range (this should always be true as the
4825 * relevant instructions will UNDEF on bad shift immediates).
4827 static void shift_reg_imm(TCGv_i64 dst, TCGv_i64 src, int sf,
4828 enum a64_shift_type shift_type, unsigned int shift_i)
4830 assert(shift_i < (sf ? 64 : 32));
4832 if (shift_i == 0) {
4833 tcg_gen_mov_i64(dst, src);
4834 } else {
4835 shift_reg(dst, src, sf, shift_type, tcg_constant_i64(shift_i));
4839 /* Logical (shifted register)
4840 * 31 30 29 28 24 23 22 21 20 16 15 10 9 5 4 0
4841 * +----+-----+-----------+-------+---+------+--------+------+------+
4842 * | sf | opc | 0 1 0 1 0 | shift | N | Rm | imm6 | Rn | Rd |
4843 * +----+-----+-----------+-------+---+------+--------+------+------+
4845 static void disas_logic_reg(DisasContext *s, uint32_t insn)
4847 TCGv_i64 tcg_rd, tcg_rn, tcg_rm;
4848 unsigned int sf, opc, shift_type, invert, rm, shift_amount, rn, rd;
4850 sf = extract32(insn, 31, 1);
4851 opc = extract32(insn, 29, 2);
4852 shift_type = extract32(insn, 22, 2);
4853 invert = extract32(insn, 21, 1);
4854 rm = extract32(insn, 16, 5);
4855 shift_amount = extract32(insn, 10, 6);
4856 rn = extract32(insn, 5, 5);
4857 rd = extract32(insn, 0, 5);
4859 if (!sf && (shift_amount & (1 << 5))) {
4860 unallocated_encoding(s);
4861 return;
4864 tcg_rd = cpu_reg(s, rd);
4866 if (opc == 1 && shift_amount == 0 && shift_type == 0 && rn == 31) {
4867 /* Unshifted ORR and ORN with WZR/XZR is the standard encoding for
4868 * register-register MOV and MVN, so it is worth special casing.
4870 tcg_rm = cpu_reg(s, rm);
4871 if (invert) {
4872 tcg_gen_not_i64(tcg_rd, tcg_rm);
4873 if (!sf) {
4874 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4876 } else {
4877 if (sf) {
4878 tcg_gen_mov_i64(tcg_rd, tcg_rm);
4879 } else {
4880 tcg_gen_ext32u_i64(tcg_rd, tcg_rm);
4883 return;
4886 tcg_rm = read_cpu_reg(s, rm, sf);
4888 if (shift_amount) {
4889 shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, shift_amount);
4892 tcg_rn = cpu_reg(s, rn);
4894 switch (opc | (invert << 2)) {
4895 case 0: /* AND */
4896 case 3: /* ANDS */
4897 tcg_gen_and_i64(tcg_rd, tcg_rn, tcg_rm);
4898 break;
4899 case 1: /* ORR */
4900 tcg_gen_or_i64(tcg_rd, tcg_rn, tcg_rm);
4901 break;
4902 case 2: /* EOR */
4903 tcg_gen_xor_i64(tcg_rd, tcg_rn, tcg_rm);
4904 break;
4905 case 4: /* BIC */
4906 case 7: /* BICS */
4907 tcg_gen_andc_i64(tcg_rd, tcg_rn, tcg_rm);
4908 break;
4909 case 5: /* ORN */
4910 tcg_gen_orc_i64(tcg_rd, tcg_rn, tcg_rm);
4911 break;
4912 case 6: /* EON */
4913 tcg_gen_eqv_i64(tcg_rd, tcg_rn, tcg_rm);
4914 break;
4915 default:
4916 assert(FALSE);
4917 break;
4920 if (!sf) {
4921 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4924 if (opc == 3) {
4925 gen_logic_CC(sf, tcg_rd);
4930 * Add/subtract (extended register)
4932 * 31|30|29|28 24|23 22|21|20 16|15 13|12 10|9 5|4 0|
4933 * +--+--+--+-----------+-----+--+-------+------+------+----+----+
4934 * |sf|op| S| 0 1 0 1 1 | opt | 1| Rm |option| imm3 | Rn | Rd |
4935 * +--+--+--+-----------+-----+--+-------+------+------+----+----+
4937 * sf: 0 -> 32bit, 1 -> 64bit
4938 * op: 0 -> add , 1 -> sub
4939 * S: 1 -> set flags
4940 * opt: 00
4941 * option: extension type (see DecodeRegExtend)
4942 * imm3: optional shift to Rm
4944 * Rd = Rn + LSL(extend(Rm), amount)
4946 static void disas_add_sub_ext_reg(DisasContext *s, uint32_t insn)
4948 int rd = extract32(insn, 0, 5);
4949 int rn = extract32(insn, 5, 5);
4950 int imm3 = extract32(insn, 10, 3);
4951 int option = extract32(insn, 13, 3);
4952 int rm = extract32(insn, 16, 5);
4953 int opt = extract32(insn, 22, 2);
4954 bool setflags = extract32(insn, 29, 1);
4955 bool sub_op = extract32(insn, 30, 1);
4956 bool sf = extract32(insn, 31, 1);
4958 TCGv_i64 tcg_rm, tcg_rn; /* temps */
4959 TCGv_i64 tcg_rd;
4960 TCGv_i64 tcg_result;
4962 if (imm3 > 4 || opt != 0) {
4963 unallocated_encoding(s);
4964 return;
4967 /* non-flag setting ops may use SP */
4968 if (!setflags) {
4969 tcg_rd = cpu_reg_sp(s, rd);
4970 } else {
4971 tcg_rd = cpu_reg(s, rd);
4973 tcg_rn = read_cpu_reg_sp(s, rn, sf);
4975 tcg_rm = read_cpu_reg(s, rm, sf);
4976 ext_and_shift_reg(tcg_rm, tcg_rm, option, imm3);
4978 tcg_result = tcg_temp_new_i64();
4980 if (!setflags) {
4981 if (sub_op) {
4982 tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
4983 } else {
4984 tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
4986 } else {
4987 if (sub_op) {
4988 gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
4989 } else {
4990 gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
4994 if (sf) {
4995 tcg_gen_mov_i64(tcg_rd, tcg_result);
4996 } else {
4997 tcg_gen_ext32u_i64(tcg_rd, tcg_result);
5000 tcg_temp_free_i64(tcg_result);
5004 * Add/subtract (shifted register)
5006 * 31 30 29 28 24 23 22 21 20 16 15 10 9 5 4 0
5007 * +--+--+--+-----------+-----+--+-------+---------+------+------+
5008 * |sf|op| S| 0 1 0 1 1 |shift| 0| Rm | imm6 | Rn | Rd |
5009 * +--+--+--+-----------+-----+--+-------+---------+------+------+
5011 * sf: 0 -> 32bit, 1 -> 64bit
5012 * op: 0 -> add , 1 -> sub
5013 * S: 1 -> set flags
5014 * shift: 00 -> LSL, 01 -> LSR, 10 -> ASR, 11 -> RESERVED
5015 * imm6: Shift amount to apply to Rm before the add/sub
5017 static void disas_add_sub_reg(DisasContext *s, uint32_t insn)
5019 int rd = extract32(insn, 0, 5);
5020 int rn = extract32(insn, 5, 5);
5021 int imm6 = extract32(insn, 10, 6);
5022 int rm = extract32(insn, 16, 5);
5023 int shift_type = extract32(insn, 22, 2);
5024 bool setflags = extract32(insn, 29, 1);
5025 bool sub_op = extract32(insn, 30, 1);
5026 bool sf = extract32(insn, 31, 1);
5028 TCGv_i64 tcg_rd = cpu_reg(s, rd);
5029 TCGv_i64 tcg_rn, tcg_rm;
5030 TCGv_i64 tcg_result;
5032 if ((shift_type == 3) || (!sf && (imm6 > 31))) {
5033 unallocated_encoding(s);
5034 return;
5037 tcg_rn = read_cpu_reg(s, rn, sf);
5038 tcg_rm = read_cpu_reg(s, rm, sf);
5040 shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, imm6);
5042 tcg_result = tcg_temp_new_i64();
5044 if (!setflags) {
5045 if (sub_op) {
5046 tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
5047 } else {
5048 tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
5050 } else {
5051 if (sub_op) {
5052 gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
5053 } else {
5054 gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
5058 if (sf) {
5059 tcg_gen_mov_i64(tcg_rd, tcg_result);
5060 } else {
5061 tcg_gen_ext32u_i64(tcg_rd, tcg_result);
5064 tcg_temp_free_i64(tcg_result);
5067 /* Data-processing (3 source)
5069 * 31 30 29 28 24 23 21 20 16 15 14 10 9 5 4 0
5070 * +--+------+-----------+------+------+----+------+------+------+
5071 * |sf| op54 | 1 1 0 1 1 | op31 | Rm | o0 | Ra | Rn | Rd |
5072 * +--+------+-----------+------+------+----+------+------+------+
5074 static void disas_data_proc_3src(DisasContext *s, uint32_t insn)
5076 int rd = extract32(insn, 0, 5);
5077 int rn = extract32(insn, 5, 5);
5078 int ra = extract32(insn, 10, 5);
5079 int rm = extract32(insn, 16, 5);
5080 int op_id = (extract32(insn, 29, 3) << 4) |
5081 (extract32(insn, 21, 3) << 1) |
5082 extract32(insn, 15, 1);
5083 bool sf = extract32(insn, 31, 1);
5084 bool is_sub = extract32(op_id, 0, 1);
5085 bool is_high = extract32(op_id, 2, 1);
5086 bool is_signed = false;
5087 TCGv_i64 tcg_op1;
5088 TCGv_i64 tcg_op2;
5089 TCGv_i64 tcg_tmp;
5091 /* Note that op_id is sf:op54:op31:o0 so it includes the 32/64 size flag */
5092 switch (op_id) {
5093 case 0x42: /* SMADDL */
5094 case 0x43: /* SMSUBL */
5095 case 0x44: /* SMULH */
5096 is_signed = true;
5097 break;
5098 case 0x0: /* MADD (32bit) */
5099 case 0x1: /* MSUB (32bit) */
5100 case 0x40: /* MADD (64bit) */
5101 case 0x41: /* MSUB (64bit) */
5102 case 0x4a: /* UMADDL */
5103 case 0x4b: /* UMSUBL */
5104 case 0x4c: /* UMULH */
5105 break;
5106 default:
5107 unallocated_encoding(s);
5108 return;
5111 if (is_high) {
5112 TCGv_i64 low_bits = tcg_temp_new_i64(); /* low bits discarded */
5113 TCGv_i64 tcg_rd = cpu_reg(s, rd);
5114 TCGv_i64 tcg_rn = cpu_reg(s, rn);
5115 TCGv_i64 tcg_rm = cpu_reg(s, rm);
5117 if (is_signed) {
5118 tcg_gen_muls2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
5119 } else {
5120 tcg_gen_mulu2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
5123 tcg_temp_free_i64(low_bits);
5124 return;
5127 tcg_op1 = tcg_temp_new_i64();
5128 tcg_op2 = tcg_temp_new_i64();
5129 tcg_tmp = tcg_temp_new_i64();
5131 if (op_id < 0x42) {
5132 tcg_gen_mov_i64(tcg_op1, cpu_reg(s, rn));
5133 tcg_gen_mov_i64(tcg_op2, cpu_reg(s, rm));
5134 } else {
5135 if (is_signed) {
5136 tcg_gen_ext32s_i64(tcg_op1, cpu_reg(s, rn));
5137 tcg_gen_ext32s_i64(tcg_op2, cpu_reg(s, rm));
5138 } else {
5139 tcg_gen_ext32u_i64(tcg_op1, cpu_reg(s, rn));
5140 tcg_gen_ext32u_i64(tcg_op2, cpu_reg(s, rm));
5144 if (ra == 31 && !is_sub) {
5145 /* Special-case MADD with rA == XZR; it is the standard MUL alias */
5146 tcg_gen_mul_i64(cpu_reg(s, rd), tcg_op1, tcg_op2);
5147 } else {
5148 tcg_gen_mul_i64(tcg_tmp, tcg_op1, tcg_op2);
5149 if (is_sub) {
5150 tcg_gen_sub_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
5151 } else {
5152 tcg_gen_add_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
5156 if (!sf) {
5157 tcg_gen_ext32u_i64(cpu_reg(s, rd), cpu_reg(s, rd));
5160 tcg_temp_free_i64(tcg_op1);
5161 tcg_temp_free_i64(tcg_op2);
5162 tcg_temp_free_i64(tcg_tmp);
5165 /* Add/subtract (with carry)
5166 * 31 30 29 28 27 26 25 24 23 22 21 20 16 15 10 9 5 4 0
5167 * +--+--+--+------------------------+------+-------------+------+-----+
5168 * |sf|op| S| 1 1 0 1 0 0 0 0 | rm | 0 0 0 0 0 0 | Rn | Rd |
5169 * +--+--+--+------------------------+------+-------------+------+-----+
5172 static void disas_adc_sbc(DisasContext *s, uint32_t insn)
5174 unsigned int sf, op, setflags, rm, rn, rd;
5175 TCGv_i64 tcg_y, tcg_rn, tcg_rd;
5177 sf = extract32(insn, 31, 1);
5178 op = extract32(insn, 30, 1);
5179 setflags = extract32(insn, 29, 1);
5180 rm = extract32(insn, 16, 5);
5181 rn = extract32(insn, 5, 5);
5182 rd = extract32(insn, 0, 5);
5184 tcg_rd = cpu_reg(s, rd);
5185 tcg_rn = cpu_reg(s, rn);
5187 if (op) {
5188 tcg_y = new_tmp_a64(s);
5189 tcg_gen_not_i64(tcg_y, cpu_reg(s, rm));
5190 } else {
5191 tcg_y = cpu_reg(s, rm);
5194 if (setflags) {
5195 gen_adc_CC(sf, tcg_rd, tcg_rn, tcg_y);
5196 } else {
5197 gen_adc(sf, tcg_rd, tcg_rn, tcg_y);
5202 * Rotate right into flags
5203 * 31 30 29 21 15 10 5 4 0
5204 * +--+--+--+-----------------+--------+-----------+------+--+------+
5205 * |sf|op| S| 1 1 0 1 0 0 0 0 | imm6 | 0 0 0 0 1 | Rn |o2| mask |
5206 * +--+--+--+-----------------+--------+-----------+------+--+------+
5208 static void disas_rotate_right_into_flags(DisasContext *s, uint32_t insn)
5210 int mask = extract32(insn, 0, 4);
5211 int o2 = extract32(insn, 4, 1);
5212 int rn = extract32(insn, 5, 5);
5213 int imm6 = extract32(insn, 15, 6);
5214 int sf_op_s = extract32(insn, 29, 3);
5215 TCGv_i64 tcg_rn;
5216 TCGv_i32 nzcv;
5218 if (sf_op_s != 5 || o2 != 0 || !dc_isar_feature(aa64_condm_4, s)) {
5219 unallocated_encoding(s);
5220 return;
5223 tcg_rn = read_cpu_reg(s, rn, 1);
5224 tcg_gen_rotri_i64(tcg_rn, tcg_rn, imm6);
5226 nzcv = tcg_temp_new_i32();
5227 tcg_gen_extrl_i64_i32(nzcv, tcg_rn);
5229 if (mask & 8) { /* N */
5230 tcg_gen_shli_i32(cpu_NF, nzcv, 31 - 3);
5232 if (mask & 4) { /* Z */
5233 tcg_gen_not_i32(cpu_ZF, nzcv);
5234 tcg_gen_andi_i32(cpu_ZF, cpu_ZF, 4);
5236 if (mask & 2) { /* C */
5237 tcg_gen_extract_i32(cpu_CF, nzcv, 1, 1);
5239 if (mask & 1) { /* V */
5240 tcg_gen_shli_i32(cpu_VF, nzcv, 31 - 0);
5243 tcg_temp_free_i32(nzcv);
5247 * Evaluate into flags
5248 * 31 30 29 21 15 14 10 5 4 0
5249 * +--+--+--+-----------------+---------+----+---------+------+--+------+
5250 * |sf|op| S| 1 1 0 1 0 0 0 0 | opcode2 | sz | 0 0 1 0 | Rn |o3| mask |
5251 * +--+--+--+-----------------+---------+----+---------+------+--+------+
5253 static void disas_evaluate_into_flags(DisasContext *s, uint32_t insn)
5255 int o3_mask = extract32(insn, 0, 5);
5256 int rn = extract32(insn, 5, 5);
5257 int o2 = extract32(insn, 15, 6);
5258 int sz = extract32(insn, 14, 1);
5259 int sf_op_s = extract32(insn, 29, 3);
5260 TCGv_i32 tmp;
5261 int shift;
5263 if (sf_op_s != 1 || o2 != 0 || o3_mask != 0xd ||
5264 !dc_isar_feature(aa64_condm_4, s)) {
5265 unallocated_encoding(s);
5266 return;
5268 shift = sz ? 16 : 24; /* SETF16 or SETF8 */
5270 tmp = tcg_temp_new_i32();
5271 tcg_gen_extrl_i64_i32(tmp, cpu_reg(s, rn));
5272 tcg_gen_shli_i32(cpu_NF, tmp, shift);
5273 tcg_gen_shli_i32(cpu_VF, tmp, shift - 1);
5274 tcg_gen_mov_i32(cpu_ZF, cpu_NF);
5275 tcg_gen_xor_i32(cpu_VF, cpu_VF, cpu_NF);
5276 tcg_temp_free_i32(tmp);
5279 /* Conditional compare (immediate / register)
5280 * 31 30 29 28 27 26 25 24 23 22 21 20 16 15 12 11 10 9 5 4 3 0
5281 * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
5282 * |sf|op| S| 1 1 0 1 0 0 1 0 |imm5/rm | cond |i/r |o2| Rn |o3|nzcv |
5283 * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
5284 * [1] y [0] [0]
5286 static void disas_cc(DisasContext *s, uint32_t insn)
5288 unsigned int sf, op, y, cond, rn, nzcv, is_imm;
5289 TCGv_i32 tcg_t0, tcg_t1, tcg_t2;
5290 TCGv_i64 tcg_tmp, tcg_y, tcg_rn;
5291 DisasCompare c;
5293 if (!extract32(insn, 29, 1)) {
5294 unallocated_encoding(s);
5295 return;
5297 if (insn & (1 << 10 | 1 << 4)) {
5298 unallocated_encoding(s);
5299 return;
5301 sf = extract32(insn, 31, 1);
5302 op = extract32(insn, 30, 1);
5303 is_imm = extract32(insn, 11, 1);
5304 y = extract32(insn, 16, 5); /* y = rm (reg) or imm5 (imm) */
5305 cond = extract32(insn, 12, 4);
5306 rn = extract32(insn, 5, 5);
5307 nzcv = extract32(insn, 0, 4);
5309 /* Set T0 = !COND. */
5310 tcg_t0 = tcg_temp_new_i32();
5311 arm_test_cc(&c, cond);
5312 tcg_gen_setcondi_i32(tcg_invert_cond(c.cond), tcg_t0, c.value, 0);
5314 /* Load the arguments for the new comparison. */
5315 if (is_imm) {
5316 tcg_y = new_tmp_a64(s);
5317 tcg_gen_movi_i64(tcg_y, y);
5318 } else {
5319 tcg_y = cpu_reg(s, y);
5321 tcg_rn = cpu_reg(s, rn);
5323 /* Set the flags for the new comparison. */
5324 tcg_tmp = tcg_temp_new_i64();
5325 if (op) {
5326 gen_sub_CC(sf, tcg_tmp, tcg_rn, tcg_y);
5327 } else {
5328 gen_add_CC(sf, tcg_tmp, tcg_rn, tcg_y);
5330 tcg_temp_free_i64(tcg_tmp);
5332 /* If COND was false, force the flags to #nzcv. Compute two masks
5333 * to help with this: T1 = (COND ? 0 : -1), T2 = (COND ? -1 : 0).
5334 * For tcg hosts that support ANDC, we can make do with just T1.
5335 * In either case, allow the tcg optimizer to delete any unused mask.
5337 tcg_t1 = tcg_temp_new_i32();
5338 tcg_t2 = tcg_temp_new_i32();
5339 tcg_gen_neg_i32(tcg_t1, tcg_t0);
5340 tcg_gen_subi_i32(tcg_t2, tcg_t0, 1);
5342 if (nzcv & 8) { /* N */
5343 tcg_gen_or_i32(cpu_NF, cpu_NF, tcg_t1);
5344 } else {
5345 if (TCG_TARGET_HAS_andc_i32) {
5346 tcg_gen_andc_i32(cpu_NF, cpu_NF, tcg_t1);
5347 } else {
5348 tcg_gen_and_i32(cpu_NF, cpu_NF, tcg_t2);
5351 if (nzcv & 4) { /* Z */
5352 if (TCG_TARGET_HAS_andc_i32) {
5353 tcg_gen_andc_i32(cpu_ZF, cpu_ZF, tcg_t1);
5354 } else {
5355 tcg_gen_and_i32(cpu_ZF, cpu_ZF, tcg_t2);
5357 } else {
5358 tcg_gen_or_i32(cpu_ZF, cpu_ZF, tcg_t0);
5360 if (nzcv & 2) { /* C */
5361 tcg_gen_or_i32(cpu_CF, cpu_CF, tcg_t0);
5362 } else {
5363 if (TCG_TARGET_HAS_andc_i32) {
5364 tcg_gen_andc_i32(cpu_CF, cpu_CF, tcg_t1);
5365 } else {
5366 tcg_gen_and_i32(cpu_CF, cpu_CF, tcg_t2);
5369 if (nzcv & 1) { /* V */
5370 tcg_gen_or_i32(cpu_VF, cpu_VF, tcg_t1);
5371 } else {
5372 if (TCG_TARGET_HAS_andc_i32) {
5373 tcg_gen_andc_i32(cpu_VF, cpu_VF, tcg_t1);
5374 } else {
5375 tcg_gen_and_i32(cpu_VF, cpu_VF, tcg_t2);
5378 tcg_temp_free_i32(tcg_t0);
5379 tcg_temp_free_i32(tcg_t1);
5380 tcg_temp_free_i32(tcg_t2);
5383 /* Conditional select
5384 * 31 30 29 28 21 20 16 15 12 11 10 9 5 4 0
5385 * +----+----+---+-----------------+------+------+-----+------+------+
5386 * | sf | op | S | 1 1 0 1 0 1 0 0 | Rm | cond | op2 | Rn | Rd |
5387 * +----+----+---+-----------------+------+------+-----+------+------+
5389 static void disas_cond_select(DisasContext *s, uint32_t insn)
5391 unsigned int sf, else_inv, rm, cond, else_inc, rn, rd;
5392 TCGv_i64 tcg_rd, zero;
5393 DisasCompare64 c;
5395 if (extract32(insn, 29, 1) || extract32(insn, 11, 1)) {
5396 /* S == 1 or op2<1> == 1 */
5397 unallocated_encoding(s);
5398 return;
5400 sf = extract32(insn, 31, 1);
5401 else_inv = extract32(insn, 30, 1);
5402 rm = extract32(insn, 16, 5);
5403 cond = extract32(insn, 12, 4);
5404 else_inc = extract32(insn, 10, 1);
5405 rn = extract32(insn, 5, 5);
5406 rd = extract32(insn, 0, 5);
5408 tcg_rd = cpu_reg(s, rd);
5410 a64_test_cc(&c, cond);
5411 zero = tcg_constant_i64(0);
5413 if (rn == 31 && rm == 31 && (else_inc ^ else_inv)) {
5414 /* CSET & CSETM. */
5415 tcg_gen_setcond_i64(tcg_invert_cond(c.cond), tcg_rd, c.value, zero);
5416 if (else_inv) {
5417 tcg_gen_neg_i64(tcg_rd, tcg_rd);
5419 } else {
5420 TCGv_i64 t_true = cpu_reg(s, rn);
5421 TCGv_i64 t_false = read_cpu_reg(s, rm, 1);
5422 if (else_inv && else_inc) {
5423 tcg_gen_neg_i64(t_false, t_false);
5424 } else if (else_inv) {
5425 tcg_gen_not_i64(t_false, t_false);
5426 } else if (else_inc) {
5427 tcg_gen_addi_i64(t_false, t_false, 1);
5429 tcg_gen_movcond_i64(c.cond, tcg_rd, c.value, zero, t_true, t_false);
5432 if (!sf) {
5433 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
5437 static void handle_clz(DisasContext *s, unsigned int sf,
5438 unsigned int rn, unsigned int rd)
5440 TCGv_i64 tcg_rd, tcg_rn;
5441 tcg_rd = cpu_reg(s, rd);
5442 tcg_rn = cpu_reg(s, rn);
5444 if (sf) {
5445 tcg_gen_clzi_i64(tcg_rd, tcg_rn, 64);
5446 } else {
5447 TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
5448 tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
5449 tcg_gen_clzi_i32(tcg_tmp32, tcg_tmp32, 32);
5450 tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
5451 tcg_temp_free_i32(tcg_tmp32);
5455 static void handle_cls(DisasContext *s, unsigned int sf,
5456 unsigned int rn, unsigned int rd)
5458 TCGv_i64 tcg_rd, tcg_rn;
5459 tcg_rd = cpu_reg(s, rd);
5460 tcg_rn = cpu_reg(s, rn);
5462 if (sf) {
5463 tcg_gen_clrsb_i64(tcg_rd, tcg_rn);
5464 } else {
5465 TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
5466 tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
5467 tcg_gen_clrsb_i32(tcg_tmp32, tcg_tmp32);
5468 tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
5469 tcg_temp_free_i32(tcg_tmp32);
5473 static void handle_rbit(DisasContext *s, unsigned int sf,
5474 unsigned int rn, unsigned int rd)
5476 TCGv_i64 tcg_rd, tcg_rn;
5477 tcg_rd = cpu_reg(s, rd);
5478 tcg_rn = cpu_reg(s, rn);
5480 if (sf) {
5481 gen_helper_rbit64(tcg_rd, tcg_rn);
5482 } else {
5483 TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
5484 tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
5485 gen_helper_rbit(tcg_tmp32, tcg_tmp32);
5486 tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
5487 tcg_temp_free_i32(tcg_tmp32);
5491 /* REV with sf==1, opcode==3 ("REV64") */
5492 static void handle_rev64(DisasContext *s, unsigned int sf,
5493 unsigned int rn, unsigned int rd)
5495 if (!sf) {
5496 unallocated_encoding(s);
5497 return;
5499 tcg_gen_bswap64_i64(cpu_reg(s, rd), cpu_reg(s, rn));
5502 /* REV with sf==0, opcode==2
5503 * REV32 (sf==1, opcode==2)
5505 static void handle_rev32(DisasContext *s, unsigned int sf,
5506 unsigned int rn, unsigned int rd)
5508 TCGv_i64 tcg_rd = cpu_reg(s, rd);
5509 TCGv_i64 tcg_rn = cpu_reg(s, rn);
5511 if (sf) {
5512 tcg_gen_bswap64_i64(tcg_rd, tcg_rn);
5513 tcg_gen_rotri_i64(tcg_rd, tcg_rd, 32);
5514 } else {
5515 tcg_gen_bswap32_i64(tcg_rd, tcg_rn, TCG_BSWAP_OZ);
5519 /* REV16 (opcode==1) */
5520 static void handle_rev16(DisasContext *s, unsigned int sf,
5521 unsigned int rn, unsigned int rd)
5523 TCGv_i64 tcg_rd = cpu_reg(s, rd);
5524 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
5525 TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
5526 TCGv_i64 mask = tcg_constant_i64(sf ? 0x00ff00ff00ff00ffull : 0x00ff00ff);
5528 tcg_gen_shri_i64(tcg_tmp, tcg_rn, 8);
5529 tcg_gen_and_i64(tcg_rd, tcg_rn, mask);
5530 tcg_gen_and_i64(tcg_tmp, tcg_tmp, mask);
5531 tcg_gen_shli_i64(tcg_rd, tcg_rd, 8);
5532 tcg_gen_or_i64(tcg_rd, tcg_rd, tcg_tmp);
5534 tcg_temp_free_i64(tcg_tmp);
5537 /* Data-processing (1 source)
5538 * 31 30 29 28 21 20 16 15 10 9 5 4 0
5539 * +----+---+---+-----------------+---------+--------+------+------+
5540 * | sf | 1 | S | 1 1 0 1 0 1 1 0 | opcode2 | opcode | Rn | Rd |
5541 * +----+---+---+-----------------+---------+--------+------+------+
5543 static void disas_data_proc_1src(DisasContext *s, uint32_t insn)
5545 unsigned int sf, opcode, opcode2, rn, rd;
5546 TCGv_i64 tcg_rd;
5548 if (extract32(insn, 29, 1)) {
5549 unallocated_encoding(s);
5550 return;
5553 sf = extract32(insn, 31, 1);
5554 opcode = extract32(insn, 10, 6);
5555 opcode2 = extract32(insn, 16, 5);
5556 rn = extract32(insn, 5, 5);
5557 rd = extract32(insn, 0, 5);
5559 #define MAP(SF, O2, O1) ((SF) | (O1 << 1) | (O2 << 7))
5561 switch (MAP(sf, opcode2, opcode)) {
5562 case MAP(0, 0x00, 0x00): /* RBIT */
5563 case MAP(1, 0x00, 0x00):
5564 handle_rbit(s, sf, rn, rd);
5565 break;
5566 case MAP(0, 0x00, 0x01): /* REV16 */
5567 case MAP(1, 0x00, 0x01):
5568 handle_rev16(s, sf, rn, rd);
5569 break;
5570 case MAP(0, 0x00, 0x02): /* REV/REV32 */
5571 case MAP(1, 0x00, 0x02):
5572 handle_rev32(s, sf, rn, rd);
5573 break;
5574 case MAP(1, 0x00, 0x03): /* REV64 */
5575 handle_rev64(s, sf, rn, rd);
5576 break;
5577 case MAP(0, 0x00, 0x04): /* CLZ */
5578 case MAP(1, 0x00, 0x04):
5579 handle_clz(s, sf, rn, rd);
5580 break;
5581 case MAP(0, 0x00, 0x05): /* CLS */
5582 case MAP(1, 0x00, 0x05):
5583 handle_cls(s, sf, rn, rd);
5584 break;
5585 case MAP(1, 0x01, 0x00): /* PACIA */
5586 if (s->pauth_active) {
5587 tcg_rd = cpu_reg(s, rd);
5588 gen_helper_pacia(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5589 } else if (!dc_isar_feature(aa64_pauth, s)) {
5590 goto do_unallocated;
5592 break;
5593 case MAP(1, 0x01, 0x01): /* PACIB */
5594 if (s->pauth_active) {
5595 tcg_rd = cpu_reg(s, rd);
5596 gen_helper_pacib(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5597 } else if (!dc_isar_feature(aa64_pauth, s)) {
5598 goto do_unallocated;
5600 break;
5601 case MAP(1, 0x01, 0x02): /* PACDA */
5602 if (s->pauth_active) {
5603 tcg_rd = cpu_reg(s, rd);
5604 gen_helper_pacda(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5605 } else if (!dc_isar_feature(aa64_pauth, s)) {
5606 goto do_unallocated;
5608 break;
5609 case MAP(1, 0x01, 0x03): /* PACDB */
5610 if (s->pauth_active) {
5611 tcg_rd = cpu_reg(s, rd);
5612 gen_helper_pacdb(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5613 } else if (!dc_isar_feature(aa64_pauth, s)) {
5614 goto do_unallocated;
5616 break;
5617 case MAP(1, 0x01, 0x04): /* AUTIA */
5618 if (s->pauth_active) {
5619 tcg_rd = cpu_reg(s, rd);
5620 gen_helper_autia(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5621 } else if (!dc_isar_feature(aa64_pauth, s)) {
5622 goto do_unallocated;
5624 break;
5625 case MAP(1, 0x01, 0x05): /* AUTIB */
5626 if (s->pauth_active) {
5627 tcg_rd = cpu_reg(s, rd);
5628 gen_helper_autib(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5629 } else if (!dc_isar_feature(aa64_pauth, s)) {
5630 goto do_unallocated;
5632 break;
5633 case MAP(1, 0x01, 0x06): /* AUTDA */
5634 if (s->pauth_active) {
5635 tcg_rd = cpu_reg(s, rd);
5636 gen_helper_autda(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5637 } else if (!dc_isar_feature(aa64_pauth, s)) {
5638 goto do_unallocated;
5640 break;
5641 case MAP(1, 0x01, 0x07): /* AUTDB */
5642 if (s->pauth_active) {
5643 tcg_rd = cpu_reg(s, rd);
5644 gen_helper_autdb(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5645 } else if (!dc_isar_feature(aa64_pauth, s)) {
5646 goto do_unallocated;
5648 break;
5649 case MAP(1, 0x01, 0x08): /* PACIZA */
5650 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5651 goto do_unallocated;
5652 } else if (s->pauth_active) {
5653 tcg_rd = cpu_reg(s, rd);
5654 gen_helper_pacia(tcg_rd, cpu_env, tcg_rd, new_tmp_a64_zero(s));
5656 break;
5657 case MAP(1, 0x01, 0x09): /* PACIZB */
5658 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5659 goto do_unallocated;
5660 } else if (s->pauth_active) {
5661 tcg_rd = cpu_reg(s, rd);
5662 gen_helper_pacib(tcg_rd, cpu_env, tcg_rd, new_tmp_a64_zero(s));
5664 break;
5665 case MAP(1, 0x01, 0x0a): /* PACDZA */
5666 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5667 goto do_unallocated;
5668 } else if (s->pauth_active) {
5669 tcg_rd = cpu_reg(s, rd);
5670 gen_helper_pacda(tcg_rd, cpu_env, tcg_rd, new_tmp_a64_zero(s));
5672 break;
5673 case MAP(1, 0x01, 0x0b): /* PACDZB */
5674 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5675 goto do_unallocated;
5676 } else if (s->pauth_active) {
5677 tcg_rd = cpu_reg(s, rd);
5678 gen_helper_pacdb(tcg_rd, cpu_env, tcg_rd, new_tmp_a64_zero(s));
5680 break;
5681 case MAP(1, 0x01, 0x0c): /* AUTIZA */
5682 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5683 goto do_unallocated;
5684 } else if (s->pauth_active) {
5685 tcg_rd = cpu_reg(s, rd);
5686 gen_helper_autia(tcg_rd, cpu_env, tcg_rd, new_tmp_a64_zero(s));
5688 break;
5689 case MAP(1, 0x01, 0x0d): /* AUTIZB */
5690 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5691 goto do_unallocated;
5692 } else if (s->pauth_active) {
5693 tcg_rd = cpu_reg(s, rd);
5694 gen_helper_autib(tcg_rd, cpu_env, tcg_rd, new_tmp_a64_zero(s));
5696 break;
5697 case MAP(1, 0x01, 0x0e): /* AUTDZA */
5698 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5699 goto do_unallocated;
5700 } else if (s->pauth_active) {
5701 tcg_rd = cpu_reg(s, rd);
5702 gen_helper_autda(tcg_rd, cpu_env, tcg_rd, new_tmp_a64_zero(s));
5704 break;
5705 case MAP(1, 0x01, 0x0f): /* AUTDZB */
5706 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5707 goto do_unallocated;
5708 } else if (s->pauth_active) {
5709 tcg_rd = cpu_reg(s, rd);
5710 gen_helper_autdb(tcg_rd, cpu_env, tcg_rd, new_tmp_a64_zero(s));
5712 break;
5713 case MAP(1, 0x01, 0x10): /* XPACI */
5714 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5715 goto do_unallocated;
5716 } else if (s->pauth_active) {
5717 tcg_rd = cpu_reg(s, rd);
5718 gen_helper_xpaci(tcg_rd, cpu_env, tcg_rd);
5720 break;
5721 case MAP(1, 0x01, 0x11): /* XPACD */
5722 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5723 goto do_unallocated;
5724 } else if (s->pauth_active) {
5725 tcg_rd = cpu_reg(s, rd);
5726 gen_helper_xpacd(tcg_rd, cpu_env, tcg_rd);
5728 break;
5729 default:
5730 do_unallocated:
5731 unallocated_encoding(s);
5732 break;
5735 #undef MAP
5738 static void handle_div(DisasContext *s, bool is_signed, unsigned int sf,
5739 unsigned int rm, unsigned int rn, unsigned int rd)
5741 TCGv_i64 tcg_n, tcg_m, tcg_rd;
5742 tcg_rd = cpu_reg(s, rd);
5744 if (!sf && is_signed) {
5745 tcg_n = new_tmp_a64(s);
5746 tcg_m = new_tmp_a64(s);
5747 tcg_gen_ext32s_i64(tcg_n, cpu_reg(s, rn));
5748 tcg_gen_ext32s_i64(tcg_m, cpu_reg(s, rm));
5749 } else {
5750 tcg_n = read_cpu_reg(s, rn, sf);
5751 tcg_m = read_cpu_reg(s, rm, sf);
5754 if (is_signed) {
5755 gen_helper_sdiv64(tcg_rd, tcg_n, tcg_m);
5756 } else {
5757 gen_helper_udiv64(tcg_rd, tcg_n, tcg_m);
5760 if (!sf) { /* zero extend final result */
5761 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
5765 /* LSLV, LSRV, ASRV, RORV */
5766 static void handle_shift_reg(DisasContext *s,
5767 enum a64_shift_type shift_type, unsigned int sf,
5768 unsigned int rm, unsigned int rn, unsigned int rd)
5770 TCGv_i64 tcg_shift = tcg_temp_new_i64();
5771 TCGv_i64 tcg_rd = cpu_reg(s, rd);
5772 TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
5774 tcg_gen_andi_i64(tcg_shift, cpu_reg(s, rm), sf ? 63 : 31);
5775 shift_reg(tcg_rd, tcg_rn, sf, shift_type, tcg_shift);
5776 tcg_temp_free_i64(tcg_shift);
5779 /* CRC32[BHWX], CRC32C[BHWX] */
5780 static void handle_crc32(DisasContext *s,
5781 unsigned int sf, unsigned int sz, bool crc32c,
5782 unsigned int rm, unsigned int rn, unsigned int rd)
5784 TCGv_i64 tcg_acc, tcg_val;
5785 TCGv_i32 tcg_bytes;
5787 if (!dc_isar_feature(aa64_crc32, s)
5788 || (sf == 1 && sz != 3)
5789 || (sf == 0 && sz == 3)) {
5790 unallocated_encoding(s);
5791 return;
5794 if (sz == 3) {
5795 tcg_val = cpu_reg(s, rm);
5796 } else {
5797 uint64_t mask;
5798 switch (sz) {
5799 case 0:
5800 mask = 0xFF;
5801 break;
5802 case 1:
5803 mask = 0xFFFF;
5804 break;
5805 case 2:
5806 mask = 0xFFFFFFFF;
5807 break;
5808 default:
5809 g_assert_not_reached();
5811 tcg_val = new_tmp_a64(s);
5812 tcg_gen_andi_i64(tcg_val, cpu_reg(s, rm), mask);
5815 tcg_acc = cpu_reg(s, rn);
5816 tcg_bytes = tcg_constant_i32(1 << sz);
5818 if (crc32c) {
5819 gen_helper_crc32c_64(cpu_reg(s, rd), tcg_acc, tcg_val, tcg_bytes);
5820 } else {
5821 gen_helper_crc32_64(cpu_reg(s, rd), tcg_acc, tcg_val, tcg_bytes);
5825 /* Data-processing (2 source)
5826 * 31 30 29 28 21 20 16 15 10 9 5 4 0
5827 * +----+---+---+-----------------+------+--------+------+------+
5828 * | sf | 0 | S | 1 1 0 1 0 1 1 0 | Rm | opcode | Rn | Rd |
5829 * +----+---+---+-----------------+------+--------+------+------+
5831 static void disas_data_proc_2src(DisasContext *s, uint32_t insn)
5833 unsigned int sf, rm, opcode, rn, rd, setflag;
5834 sf = extract32(insn, 31, 1);
5835 setflag = extract32(insn, 29, 1);
5836 rm = extract32(insn, 16, 5);
5837 opcode = extract32(insn, 10, 6);
5838 rn = extract32(insn, 5, 5);
5839 rd = extract32(insn, 0, 5);
5841 if (setflag && opcode != 0) {
5842 unallocated_encoding(s);
5843 return;
5846 switch (opcode) {
5847 case 0: /* SUBP(S) */
5848 if (sf == 0 || !dc_isar_feature(aa64_mte_insn_reg, s)) {
5849 goto do_unallocated;
5850 } else {
5851 TCGv_i64 tcg_n, tcg_m, tcg_d;
5853 tcg_n = read_cpu_reg_sp(s, rn, true);
5854 tcg_m = read_cpu_reg_sp(s, rm, true);
5855 tcg_gen_sextract_i64(tcg_n, tcg_n, 0, 56);
5856 tcg_gen_sextract_i64(tcg_m, tcg_m, 0, 56);
5857 tcg_d = cpu_reg(s, rd);
5859 if (setflag) {
5860 gen_sub_CC(true, tcg_d, tcg_n, tcg_m);
5861 } else {
5862 tcg_gen_sub_i64(tcg_d, tcg_n, tcg_m);
5865 break;
5866 case 2: /* UDIV */
5867 handle_div(s, false, sf, rm, rn, rd);
5868 break;
5869 case 3: /* SDIV */
5870 handle_div(s, true, sf, rm, rn, rd);
5871 break;
5872 case 4: /* IRG */
5873 if (sf == 0 || !dc_isar_feature(aa64_mte_insn_reg, s)) {
5874 goto do_unallocated;
5876 if (s->ata) {
5877 gen_helper_irg(cpu_reg_sp(s, rd), cpu_env,
5878 cpu_reg_sp(s, rn), cpu_reg(s, rm));
5879 } else {
5880 gen_address_with_allocation_tag0(cpu_reg_sp(s, rd),
5881 cpu_reg_sp(s, rn));
5883 break;
5884 case 5: /* GMI */
5885 if (sf == 0 || !dc_isar_feature(aa64_mte_insn_reg, s)) {
5886 goto do_unallocated;
5887 } else {
5888 TCGv_i64 t = tcg_temp_new_i64();
5890 tcg_gen_extract_i64(t, cpu_reg_sp(s, rn), 56, 4);
5891 tcg_gen_shl_i64(t, tcg_constant_i64(1), t);
5892 tcg_gen_or_i64(cpu_reg(s, rd), cpu_reg(s, rm), t);
5894 tcg_temp_free_i64(t);
5896 break;
5897 case 8: /* LSLV */
5898 handle_shift_reg(s, A64_SHIFT_TYPE_LSL, sf, rm, rn, rd);
5899 break;
5900 case 9: /* LSRV */
5901 handle_shift_reg(s, A64_SHIFT_TYPE_LSR, sf, rm, rn, rd);
5902 break;
5903 case 10: /* ASRV */
5904 handle_shift_reg(s, A64_SHIFT_TYPE_ASR, sf, rm, rn, rd);
5905 break;
5906 case 11: /* RORV */
5907 handle_shift_reg(s, A64_SHIFT_TYPE_ROR, sf, rm, rn, rd);
5908 break;
5909 case 12: /* PACGA */
5910 if (sf == 0 || !dc_isar_feature(aa64_pauth, s)) {
5911 goto do_unallocated;
5913 gen_helper_pacga(cpu_reg(s, rd), cpu_env,
5914 cpu_reg(s, rn), cpu_reg_sp(s, rm));
5915 break;
5916 case 16:
5917 case 17:
5918 case 18:
5919 case 19:
5920 case 20:
5921 case 21:
5922 case 22:
5923 case 23: /* CRC32 */
5925 int sz = extract32(opcode, 0, 2);
5926 bool crc32c = extract32(opcode, 2, 1);
5927 handle_crc32(s, sf, sz, crc32c, rm, rn, rd);
5928 break;
5930 default:
5931 do_unallocated:
5932 unallocated_encoding(s);
5933 break;
5938 * Data processing - register
5939 * 31 30 29 28 25 21 20 16 10 0
5940 * +--+---+--+---+-------+-----+-------+-------+---------+
5941 * | |op0| |op1| 1 0 1 | op2 | | op3 | |
5942 * +--+---+--+---+-------+-----+-------+-------+---------+
5944 static void disas_data_proc_reg(DisasContext *s, uint32_t insn)
5946 int op0 = extract32(insn, 30, 1);
5947 int op1 = extract32(insn, 28, 1);
5948 int op2 = extract32(insn, 21, 4);
5949 int op3 = extract32(insn, 10, 6);
5951 if (!op1) {
5952 if (op2 & 8) {
5953 if (op2 & 1) {
5954 /* Add/sub (extended register) */
5955 disas_add_sub_ext_reg(s, insn);
5956 } else {
5957 /* Add/sub (shifted register) */
5958 disas_add_sub_reg(s, insn);
5960 } else {
5961 /* Logical (shifted register) */
5962 disas_logic_reg(s, insn);
5964 return;
5967 switch (op2) {
5968 case 0x0:
5969 switch (op3) {
5970 case 0x00: /* Add/subtract (with carry) */
5971 disas_adc_sbc(s, insn);
5972 break;
5974 case 0x01: /* Rotate right into flags */
5975 case 0x21:
5976 disas_rotate_right_into_flags(s, insn);
5977 break;
5979 case 0x02: /* Evaluate into flags */
5980 case 0x12:
5981 case 0x22:
5982 case 0x32:
5983 disas_evaluate_into_flags(s, insn);
5984 break;
5986 default:
5987 goto do_unallocated;
5989 break;
5991 case 0x2: /* Conditional compare */
5992 disas_cc(s, insn); /* both imm and reg forms */
5993 break;
5995 case 0x4: /* Conditional select */
5996 disas_cond_select(s, insn);
5997 break;
5999 case 0x6: /* Data-processing */
6000 if (op0) { /* (1 source) */
6001 disas_data_proc_1src(s, insn);
6002 } else { /* (2 source) */
6003 disas_data_proc_2src(s, insn);
6005 break;
6006 case 0x8 ... 0xf: /* (3 source) */
6007 disas_data_proc_3src(s, insn);
6008 break;
6010 default:
6011 do_unallocated:
6012 unallocated_encoding(s);
6013 break;
6017 static void handle_fp_compare(DisasContext *s, int size,
6018 unsigned int rn, unsigned int rm,
6019 bool cmp_with_zero, bool signal_all_nans)
6021 TCGv_i64 tcg_flags = tcg_temp_new_i64();
6022 TCGv_ptr fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
6024 if (size == MO_64) {
6025 TCGv_i64 tcg_vn, tcg_vm;
6027 tcg_vn = read_fp_dreg(s, rn);
6028 if (cmp_with_zero) {
6029 tcg_vm = tcg_constant_i64(0);
6030 } else {
6031 tcg_vm = read_fp_dreg(s, rm);
6033 if (signal_all_nans) {
6034 gen_helper_vfp_cmped_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
6035 } else {
6036 gen_helper_vfp_cmpd_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
6038 tcg_temp_free_i64(tcg_vn);
6039 tcg_temp_free_i64(tcg_vm);
6040 } else {
6041 TCGv_i32 tcg_vn = tcg_temp_new_i32();
6042 TCGv_i32 tcg_vm = tcg_temp_new_i32();
6044 read_vec_element_i32(s, tcg_vn, rn, 0, size);
6045 if (cmp_with_zero) {
6046 tcg_gen_movi_i32(tcg_vm, 0);
6047 } else {
6048 read_vec_element_i32(s, tcg_vm, rm, 0, size);
6051 switch (size) {
6052 case MO_32:
6053 if (signal_all_nans) {
6054 gen_helper_vfp_cmpes_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
6055 } else {
6056 gen_helper_vfp_cmps_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
6058 break;
6059 case MO_16:
6060 if (signal_all_nans) {
6061 gen_helper_vfp_cmpeh_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
6062 } else {
6063 gen_helper_vfp_cmph_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
6065 break;
6066 default:
6067 g_assert_not_reached();
6070 tcg_temp_free_i32(tcg_vn);
6071 tcg_temp_free_i32(tcg_vm);
6074 tcg_temp_free_ptr(fpst);
6076 gen_set_nzcv(tcg_flags);
6078 tcg_temp_free_i64(tcg_flags);
6081 /* Floating point compare
6082 * 31 30 29 28 24 23 22 21 20 16 15 14 13 10 9 5 4 0
6083 * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
6084 * | M | 0 | S | 1 1 1 1 0 | type | 1 | Rm | op | 1 0 0 0 | Rn | op2 |
6085 * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
6087 static void disas_fp_compare(DisasContext *s, uint32_t insn)
6089 unsigned int mos, type, rm, op, rn, opc, op2r;
6090 int size;
6092 mos = extract32(insn, 29, 3);
6093 type = extract32(insn, 22, 2);
6094 rm = extract32(insn, 16, 5);
6095 op = extract32(insn, 14, 2);
6096 rn = extract32(insn, 5, 5);
6097 opc = extract32(insn, 3, 2);
6098 op2r = extract32(insn, 0, 3);
6100 if (mos || op || op2r) {
6101 unallocated_encoding(s);
6102 return;
6105 switch (type) {
6106 case 0:
6107 size = MO_32;
6108 break;
6109 case 1:
6110 size = MO_64;
6111 break;
6112 case 3:
6113 size = MO_16;
6114 if (dc_isar_feature(aa64_fp16, s)) {
6115 break;
6117 /* fallthru */
6118 default:
6119 unallocated_encoding(s);
6120 return;
6123 if (!fp_access_check(s)) {
6124 return;
6127 handle_fp_compare(s, size, rn, rm, opc & 1, opc & 2);
6130 /* Floating point conditional compare
6131 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 3 0
6132 * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
6133 * | M | 0 | S | 1 1 1 1 0 | type | 1 | Rm | cond | 0 1 | Rn | op | nzcv |
6134 * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
6136 static void disas_fp_ccomp(DisasContext *s, uint32_t insn)
6138 unsigned int mos, type, rm, cond, rn, op, nzcv;
6139 TCGLabel *label_continue = NULL;
6140 int size;
6142 mos = extract32(insn, 29, 3);
6143 type = extract32(insn, 22, 2);
6144 rm = extract32(insn, 16, 5);
6145 cond = extract32(insn, 12, 4);
6146 rn = extract32(insn, 5, 5);
6147 op = extract32(insn, 4, 1);
6148 nzcv = extract32(insn, 0, 4);
6150 if (mos) {
6151 unallocated_encoding(s);
6152 return;
6155 switch (type) {
6156 case 0:
6157 size = MO_32;
6158 break;
6159 case 1:
6160 size = MO_64;
6161 break;
6162 case 3:
6163 size = MO_16;
6164 if (dc_isar_feature(aa64_fp16, s)) {
6165 break;
6167 /* fallthru */
6168 default:
6169 unallocated_encoding(s);
6170 return;
6173 if (!fp_access_check(s)) {
6174 return;
6177 if (cond < 0x0e) { /* not always */
6178 TCGLabel *label_match = gen_new_label();
6179 label_continue = gen_new_label();
6180 arm_gen_test_cc(cond, label_match);
6181 /* nomatch: */
6182 gen_set_nzcv(tcg_constant_i64(nzcv << 28));
6183 tcg_gen_br(label_continue);
6184 gen_set_label(label_match);
6187 handle_fp_compare(s, size, rn, rm, false, op);
6189 if (cond < 0x0e) {
6190 gen_set_label(label_continue);
6194 /* Floating point conditional select
6195 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 0
6196 * +---+---+---+-----------+------+---+------+------+-----+------+------+
6197 * | M | 0 | S | 1 1 1 1 0 | type | 1 | Rm | cond | 1 1 | Rn | Rd |
6198 * +---+---+---+-----------+------+---+------+------+-----+------+------+
6200 static void disas_fp_csel(DisasContext *s, uint32_t insn)
6202 unsigned int mos, type, rm, cond, rn, rd;
6203 TCGv_i64 t_true, t_false;
6204 DisasCompare64 c;
6205 MemOp sz;
6207 mos = extract32(insn, 29, 3);
6208 type = extract32(insn, 22, 2);
6209 rm = extract32(insn, 16, 5);
6210 cond = extract32(insn, 12, 4);
6211 rn = extract32(insn, 5, 5);
6212 rd = extract32(insn, 0, 5);
6214 if (mos) {
6215 unallocated_encoding(s);
6216 return;
6219 switch (type) {
6220 case 0:
6221 sz = MO_32;
6222 break;
6223 case 1:
6224 sz = MO_64;
6225 break;
6226 case 3:
6227 sz = MO_16;
6228 if (dc_isar_feature(aa64_fp16, s)) {
6229 break;
6231 /* fallthru */
6232 default:
6233 unallocated_encoding(s);
6234 return;
6237 if (!fp_access_check(s)) {
6238 return;
6241 /* Zero extend sreg & hreg inputs to 64 bits now. */
6242 t_true = tcg_temp_new_i64();
6243 t_false = tcg_temp_new_i64();
6244 read_vec_element(s, t_true, rn, 0, sz);
6245 read_vec_element(s, t_false, rm, 0, sz);
6247 a64_test_cc(&c, cond);
6248 tcg_gen_movcond_i64(c.cond, t_true, c.value, tcg_constant_i64(0),
6249 t_true, t_false);
6250 tcg_temp_free_i64(t_false);
6252 /* Note that sregs & hregs write back zeros to the high bits,
6253 and we've already done the zero-extension. */
6254 write_fp_dreg(s, rd, t_true);
6255 tcg_temp_free_i64(t_true);
6258 /* Floating-point data-processing (1 source) - half precision */
6259 static void handle_fp_1src_half(DisasContext *s, int opcode, int rd, int rn)
6261 TCGv_ptr fpst = NULL;
6262 TCGv_i32 tcg_op = read_fp_hreg(s, rn);
6263 TCGv_i32 tcg_res = tcg_temp_new_i32();
6265 switch (opcode) {
6266 case 0x0: /* FMOV */
6267 tcg_gen_mov_i32(tcg_res, tcg_op);
6268 break;
6269 case 0x1: /* FABS */
6270 tcg_gen_andi_i32(tcg_res, tcg_op, 0x7fff);
6271 break;
6272 case 0x2: /* FNEG */
6273 tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
6274 break;
6275 case 0x3: /* FSQRT */
6276 fpst = fpstatus_ptr(FPST_FPCR_F16);
6277 gen_helper_sqrt_f16(tcg_res, tcg_op, fpst);
6278 break;
6279 case 0x8: /* FRINTN */
6280 case 0x9: /* FRINTP */
6281 case 0xa: /* FRINTM */
6282 case 0xb: /* FRINTZ */
6283 case 0xc: /* FRINTA */
6285 TCGv_i32 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(opcode & 7));
6286 fpst = fpstatus_ptr(FPST_FPCR_F16);
6288 gen_helper_set_rmode(tcg_rmode, tcg_rmode, fpst);
6289 gen_helper_advsimd_rinth(tcg_res, tcg_op, fpst);
6291 gen_helper_set_rmode(tcg_rmode, tcg_rmode, fpst);
6292 tcg_temp_free_i32(tcg_rmode);
6293 break;
6295 case 0xe: /* FRINTX */
6296 fpst = fpstatus_ptr(FPST_FPCR_F16);
6297 gen_helper_advsimd_rinth_exact(tcg_res, tcg_op, fpst);
6298 break;
6299 case 0xf: /* FRINTI */
6300 fpst = fpstatus_ptr(FPST_FPCR_F16);
6301 gen_helper_advsimd_rinth(tcg_res, tcg_op, fpst);
6302 break;
6303 default:
6304 g_assert_not_reached();
6307 write_fp_sreg(s, rd, tcg_res);
6309 if (fpst) {
6310 tcg_temp_free_ptr(fpst);
6312 tcg_temp_free_i32(tcg_op);
6313 tcg_temp_free_i32(tcg_res);
6316 /* Floating-point data-processing (1 source) - single precision */
6317 static void handle_fp_1src_single(DisasContext *s, int opcode, int rd, int rn)
6319 void (*gen_fpst)(TCGv_i32, TCGv_i32, TCGv_ptr);
6320 TCGv_i32 tcg_op, tcg_res;
6321 TCGv_ptr fpst;
6322 int rmode = -1;
6324 tcg_op = read_fp_sreg(s, rn);
6325 tcg_res = tcg_temp_new_i32();
6327 switch (opcode) {
6328 case 0x0: /* FMOV */
6329 tcg_gen_mov_i32(tcg_res, tcg_op);
6330 goto done;
6331 case 0x1: /* FABS */
6332 gen_helper_vfp_abss(tcg_res, tcg_op);
6333 goto done;
6334 case 0x2: /* FNEG */
6335 gen_helper_vfp_negs(tcg_res, tcg_op);
6336 goto done;
6337 case 0x3: /* FSQRT */
6338 gen_helper_vfp_sqrts(tcg_res, tcg_op, cpu_env);
6339 goto done;
6340 case 0x6: /* BFCVT */
6341 gen_fpst = gen_helper_bfcvt;
6342 break;
6343 case 0x8: /* FRINTN */
6344 case 0x9: /* FRINTP */
6345 case 0xa: /* FRINTM */
6346 case 0xb: /* FRINTZ */
6347 case 0xc: /* FRINTA */
6348 rmode = arm_rmode_to_sf(opcode & 7);
6349 gen_fpst = gen_helper_rints;
6350 break;
6351 case 0xe: /* FRINTX */
6352 gen_fpst = gen_helper_rints_exact;
6353 break;
6354 case 0xf: /* FRINTI */
6355 gen_fpst = gen_helper_rints;
6356 break;
6357 case 0x10: /* FRINT32Z */
6358 rmode = float_round_to_zero;
6359 gen_fpst = gen_helper_frint32_s;
6360 break;
6361 case 0x11: /* FRINT32X */
6362 gen_fpst = gen_helper_frint32_s;
6363 break;
6364 case 0x12: /* FRINT64Z */
6365 rmode = float_round_to_zero;
6366 gen_fpst = gen_helper_frint64_s;
6367 break;
6368 case 0x13: /* FRINT64X */
6369 gen_fpst = gen_helper_frint64_s;
6370 break;
6371 default:
6372 g_assert_not_reached();
6375 fpst = fpstatus_ptr(FPST_FPCR);
6376 if (rmode >= 0) {
6377 TCGv_i32 tcg_rmode = tcg_const_i32(rmode);
6378 gen_helper_set_rmode(tcg_rmode, tcg_rmode, fpst);
6379 gen_fpst(tcg_res, tcg_op, fpst);
6380 gen_helper_set_rmode(tcg_rmode, tcg_rmode, fpst);
6381 tcg_temp_free_i32(tcg_rmode);
6382 } else {
6383 gen_fpst(tcg_res, tcg_op, fpst);
6385 tcg_temp_free_ptr(fpst);
6387 done:
6388 write_fp_sreg(s, rd, tcg_res);
6389 tcg_temp_free_i32(tcg_op);
6390 tcg_temp_free_i32(tcg_res);
6393 /* Floating-point data-processing (1 source) - double precision */
6394 static void handle_fp_1src_double(DisasContext *s, int opcode, int rd, int rn)
6396 void (*gen_fpst)(TCGv_i64, TCGv_i64, TCGv_ptr);
6397 TCGv_i64 tcg_op, tcg_res;
6398 TCGv_ptr fpst;
6399 int rmode = -1;
6401 switch (opcode) {
6402 case 0x0: /* FMOV */
6403 gen_gvec_fn2(s, false, rd, rn, tcg_gen_gvec_mov, 0);
6404 return;
6407 tcg_op = read_fp_dreg(s, rn);
6408 tcg_res = tcg_temp_new_i64();
6410 switch (opcode) {
6411 case 0x1: /* FABS */
6412 gen_helper_vfp_absd(tcg_res, tcg_op);
6413 goto done;
6414 case 0x2: /* FNEG */
6415 gen_helper_vfp_negd(tcg_res, tcg_op);
6416 goto done;
6417 case 0x3: /* FSQRT */
6418 gen_helper_vfp_sqrtd(tcg_res, tcg_op, cpu_env);
6419 goto done;
6420 case 0x8: /* FRINTN */
6421 case 0x9: /* FRINTP */
6422 case 0xa: /* FRINTM */
6423 case 0xb: /* FRINTZ */
6424 case 0xc: /* FRINTA */
6425 rmode = arm_rmode_to_sf(opcode & 7);
6426 gen_fpst = gen_helper_rintd;
6427 break;
6428 case 0xe: /* FRINTX */
6429 gen_fpst = gen_helper_rintd_exact;
6430 break;
6431 case 0xf: /* FRINTI */
6432 gen_fpst = gen_helper_rintd;
6433 break;
6434 case 0x10: /* FRINT32Z */
6435 rmode = float_round_to_zero;
6436 gen_fpst = gen_helper_frint32_d;
6437 break;
6438 case 0x11: /* FRINT32X */
6439 gen_fpst = gen_helper_frint32_d;
6440 break;
6441 case 0x12: /* FRINT64Z */
6442 rmode = float_round_to_zero;
6443 gen_fpst = gen_helper_frint64_d;
6444 break;
6445 case 0x13: /* FRINT64X */
6446 gen_fpst = gen_helper_frint64_d;
6447 break;
6448 default:
6449 g_assert_not_reached();
6452 fpst = fpstatus_ptr(FPST_FPCR);
6453 if (rmode >= 0) {
6454 TCGv_i32 tcg_rmode = tcg_const_i32(rmode);
6455 gen_helper_set_rmode(tcg_rmode, tcg_rmode, fpst);
6456 gen_fpst(tcg_res, tcg_op, fpst);
6457 gen_helper_set_rmode(tcg_rmode, tcg_rmode, fpst);
6458 tcg_temp_free_i32(tcg_rmode);
6459 } else {
6460 gen_fpst(tcg_res, tcg_op, fpst);
6462 tcg_temp_free_ptr(fpst);
6464 done:
6465 write_fp_dreg(s, rd, tcg_res);
6466 tcg_temp_free_i64(tcg_op);
6467 tcg_temp_free_i64(tcg_res);
6470 static void handle_fp_fcvt(DisasContext *s, int opcode,
6471 int rd, int rn, int dtype, int ntype)
6473 switch (ntype) {
6474 case 0x0:
6476 TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
6477 if (dtype == 1) {
6478 /* Single to double */
6479 TCGv_i64 tcg_rd = tcg_temp_new_i64();
6480 gen_helper_vfp_fcvtds(tcg_rd, tcg_rn, cpu_env);
6481 write_fp_dreg(s, rd, tcg_rd);
6482 tcg_temp_free_i64(tcg_rd);
6483 } else {
6484 /* Single to half */
6485 TCGv_i32 tcg_rd = tcg_temp_new_i32();
6486 TCGv_i32 ahp = get_ahp_flag();
6487 TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6489 gen_helper_vfp_fcvt_f32_to_f16(tcg_rd, tcg_rn, fpst, ahp);
6490 /* write_fp_sreg is OK here because top half of tcg_rd is zero */
6491 write_fp_sreg(s, rd, tcg_rd);
6492 tcg_temp_free_i32(tcg_rd);
6493 tcg_temp_free_i32(ahp);
6494 tcg_temp_free_ptr(fpst);
6496 tcg_temp_free_i32(tcg_rn);
6497 break;
6499 case 0x1:
6501 TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
6502 TCGv_i32 tcg_rd = tcg_temp_new_i32();
6503 if (dtype == 0) {
6504 /* Double to single */
6505 gen_helper_vfp_fcvtsd(tcg_rd, tcg_rn, cpu_env);
6506 } else {
6507 TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6508 TCGv_i32 ahp = get_ahp_flag();
6509 /* Double to half */
6510 gen_helper_vfp_fcvt_f64_to_f16(tcg_rd, tcg_rn, fpst, ahp);
6511 /* write_fp_sreg is OK here because top half of tcg_rd is zero */
6512 tcg_temp_free_ptr(fpst);
6513 tcg_temp_free_i32(ahp);
6515 write_fp_sreg(s, rd, tcg_rd);
6516 tcg_temp_free_i32(tcg_rd);
6517 tcg_temp_free_i64(tcg_rn);
6518 break;
6520 case 0x3:
6522 TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
6523 TCGv_ptr tcg_fpst = fpstatus_ptr(FPST_FPCR);
6524 TCGv_i32 tcg_ahp = get_ahp_flag();
6525 tcg_gen_ext16u_i32(tcg_rn, tcg_rn);
6526 if (dtype == 0) {
6527 /* Half to single */
6528 TCGv_i32 tcg_rd = tcg_temp_new_i32();
6529 gen_helper_vfp_fcvt_f16_to_f32(tcg_rd, tcg_rn, tcg_fpst, tcg_ahp);
6530 write_fp_sreg(s, rd, tcg_rd);
6531 tcg_temp_free_i32(tcg_rd);
6532 } else {
6533 /* Half to double */
6534 TCGv_i64 tcg_rd = tcg_temp_new_i64();
6535 gen_helper_vfp_fcvt_f16_to_f64(tcg_rd, tcg_rn, tcg_fpst, tcg_ahp);
6536 write_fp_dreg(s, rd, tcg_rd);
6537 tcg_temp_free_i64(tcg_rd);
6539 tcg_temp_free_i32(tcg_rn);
6540 tcg_temp_free_ptr(tcg_fpst);
6541 tcg_temp_free_i32(tcg_ahp);
6542 break;
6544 default:
6545 g_assert_not_reached();
6549 /* Floating point data-processing (1 source)
6550 * 31 30 29 28 24 23 22 21 20 15 14 10 9 5 4 0
6551 * +---+---+---+-----------+------+---+--------+-----------+------+------+
6552 * | M | 0 | S | 1 1 1 1 0 | type | 1 | opcode | 1 0 0 0 0 | Rn | Rd |
6553 * +---+---+---+-----------+------+---+--------+-----------+------+------+
6555 static void disas_fp_1src(DisasContext *s, uint32_t insn)
6557 int mos = extract32(insn, 29, 3);
6558 int type = extract32(insn, 22, 2);
6559 int opcode = extract32(insn, 15, 6);
6560 int rn = extract32(insn, 5, 5);
6561 int rd = extract32(insn, 0, 5);
6563 if (mos) {
6564 goto do_unallocated;
6567 switch (opcode) {
6568 case 0x4: case 0x5: case 0x7:
6570 /* FCVT between half, single and double precision */
6571 int dtype = extract32(opcode, 0, 2);
6572 if (type == 2 || dtype == type) {
6573 goto do_unallocated;
6575 if (!fp_access_check(s)) {
6576 return;
6579 handle_fp_fcvt(s, opcode, rd, rn, dtype, type);
6580 break;
6583 case 0x10 ... 0x13: /* FRINT{32,64}{X,Z} */
6584 if (type > 1 || !dc_isar_feature(aa64_frint, s)) {
6585 goto do_unallocated;
6587 /* fall through */
6588 case 0x0 ... 0x3:
6589 case 0x8 ... 0xc:
6590 case 0xe ... 0xf:
6591 /* 32-to-32 and 64-to-64 ops */
6592 switch (type) {
6593 case 0:
6594 if (!fp_access_check(s)) {
6595 return;
6597 handle_fp_1src_single(s, opcode, rd, rn);
6598 break;
6599 case 1:
6600 if (!fp_access_check(s)) {
6601 return;
6603 handle_fp_1src_double(s, opcode, rd, rn);
6604 break;
6605 case 3:
6606 if (!dc_isar_feature(aa64_fp16, s)) {
6607 goto do_unallocated;
6610 if (!fp_access_check(s)) {
6611 return;
6613 handle_fp_1src_half(s, opcode, rd, rn);
6614 break;
6615 default:
6616 goto do_unallocated;
6618 break;
6620 case 0x6:
6621 switch (type) {
6622 case 1: /* BFCVT */
6623 if (!dc_isar_feature(aa64_bf16, s)) {
6624 goto do_unallocated;
6626 if (!fp_access_check(s)) {
6627 return;
6629 handle_fp_1src_single(s, opcode, rd, rn);
6630 break;
6631 default:
6632 goto do_unallocated;
6634 break;
6636 default:
6637 do_unallocated:
6638 unallocated_encoding(s);
6639 break;
6643 /* Floating-point data-processing (2 source) - single precision */
6644 static void handle_fp_2src_single(DisasContext *s, int opcode,
6645 int rd, int rn, int rm)
6647 TCGv_i32 tcg_op1;
6648 TCGv_i32 tcg_op2;
6649 TCGv_i32 tcg_res;
6650 TCGv_ptr fpst;
6652 tcg_res = tcg_temp_new_i32();
6653 fpst = fpstatus_ptr(FPST_FPCR);
6654 tcg_op1 = read_fp_sreg(s, rn);
6655 tcg_op2 = read_fp_sreg(s, rm);
6657 switch (opcode) {
6658 case 0x0: /* FMUL */
6659 gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
6660 break;
6661 case 0x1: /* FDIV */
6662 gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
6663 break;
6664 case 0x2: /* FADD */
6665 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
6666 break;
6667 case 0x3: /* FSUB */
6668 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
6669 break;
6670 case 0x4: /* FMAX */
6671 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
6672 break;
6673 case 0x5: /* FMIN */
6674 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
6675 break;
6676 case 0x6: /* FMAXNM */
6677 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
6678 break;
6679 case 0x7: /* FMINNM */
6680 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
6681 break;
6682 case 0x8: /* FNMUL */
6683 gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
6684 gen_helper_vfp_negs(tcg_res, tcg_res);
6685 break;
6688 write_fp_sreg(s, rd, tcg_res);
6690 tcg_temp_free_ptr(fpst);
6691 tcg_temp_free_i32(tcg_op1);
6692 tcg_temp_free_i32(tcg_op2);
6693 tcg_temp_free_i32(tcg_res);
6696 /* Floating-point data-processing (2 source) - double precision */
6697 static void handle_fp_2src_double(DisasContext *s, int opcode,
6698 int rd, int rn, int rm)
6700 TCGv_i64 tcg_op1;
6701 TCGv_i64 tcg_op2;
6702 TCGv_i64 tcg_res;
6703 TCGv_ptr fpst;
6705 tcg_res = tcg_temp_new_i64();
6706 fpst = fpstatus_ptr(FPST_FPCR);
6707 tcg_op1 = read_fp_dreg(s, rn);
6708 tcg_op2 = read_fp_dreg(s, rm);
6710 switch (opcode) {
6711 case 0x0: /* FMUL */
6712 gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
6713 break;
6714 case 0x1: /* FDIV */
6715 gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
6716 break;
6717 case 0x2: /* FADD */
6718 gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
6719 break;
6720 case 0x3: /* FSUB */
6721 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
6722 break;
6723 case 0x4: /* FMAX */
6724 gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
6725 break;
6726 case 0x5: /* FMIN */
6727 gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
6728 break;
6729 case 0x6: /* FMAXNM */
6730 gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
6731 break;
6732 case 0x7: /* FMINNM */
6733 gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
6734 break;
6735 case 0x8: /* FNMUL */
6736 gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
6737 gen_helper_vfp_negd(tcg_res, tcg_res);
6738 break;
6741 write_fp_dreg(s, rd, tcg_res);
6743 tcg_temp_free_ptr(fpst);
6744 tcg_temp_free_i64(tcg_op1);
6745 tcg_temp_free_i64(tcg_op2);
6746 tcg_temp_free_i64(tcg_res);
6749 /* Floating-point data-processing (2 source) - half precision */
6750 static void handle_fp_2src_half(DisasContext *s, int opcode,
6751 int rd, int rn, int rm)
6753 TCGv_i32 tcg_op1;
6754 TCGv_i32 tcg_op2;
6755 TCGv_i32 tcg_res;
6756 TCGv_ptr fpst;
6758 tcg_res = tcg_temp_new_i32();
6759 fpst = fpstatus_ptr(FPST_FPCR_F16);
6760 tcg_op1 = read_fp_hreg(s, rn);
6761 tcg_op2 = read_fp_hreg(s, rm);
6763 switch (opcode) {
6764 case 0x0: /* FMUL */
6765 gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
6766 break;
6767 case 0x1: /* FDIV */
6768 gen_helper_advsimd_divh(tcg_res, tcg_op1, tcg_op2, fpst);
6769 break;
6770 case 0x2: /* FADD */
6771 gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
6772 break;
6773 case 0x3: /* FSUB */
6774 gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
6775 break;
6776 case 0x4: /* FMAX */
6777 gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
6778 break;
6779 case 0x5: /* FMIN */
6780 gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
6781 break;
6782 case 0x6: /* FMAXNM */
6783 gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
6784 break;
6785 case 0x7: /* FMINNM */
6786 gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
6787 break;
6788 case 0x8: /* FNMUL */
6789 gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
6790 tcg_gen_xori_i32(tcg_res, tcg_res, 0x8000);
6791 break;
6792 default:
6793 g_assert_not_reached();
6796 write_fp_sreg(s, rd, tcg_res);
6798 tcg_temp_free_ptr(fpst);
6799 tcg_temp_free_i32(tcg_op1);
6800 tcg_temp_free_i32(tcg_op2);
6801 tcg_temp_free_i32(tcg_res);
6804 /* Floating point data-processing (2 source)
6805 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 0
6806 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
6807 * | M | 0 | S | 1 1 1 1 0 | type | 1 | Rm | opcode | 1 0 | Rn | Rd |
6808 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
6810 static void disas_fp_2src(DisasContext *s, uint32_t insn)
6812 int mos = extract32(insn, 29, 3);
6813 int type = extract32(insn, 22, 2);
6814 int rd = extract32(insn, 0, 5);
6815 int rn = extract32(insn, 5, 5);
6816 int rm = extract32(insn, 16, 5);
6817 int opcode = extract32(insn, 12, 4);
6819 if (opcode > 8 || mos) {
6820 unallocated_encoding(s);
6821 return;
6824 switch (type) {
6825 case 0:
6826 if (!fp_access_check(s)) {
6827 return;
6829 handle_fp_2src_single(s, opcode, rd, rn, rm);
6830 break;
6831 case 1:
6832 if (!fp_access_check(s)) {
6833 return;
6835 handle_fp_2src_double(s, opcode, rd, rn, rm);
6836 break;
6837 case 3:
6838 if (!dc_isar_feature(aa64_fp16, s)) {
6839 unallocated_encoding(s);
6840 return;
6842 if (!fp_access_check(s)) {
6843 return;
6845 handle_fp_2src_half(s, opcode, rd, rn, rm);
6846 break;
6847 default:
6848 unallocated_encoding(s);
6852 /* Floating-point data-processing (3 source) - single precision */
6853 static void handle_fp_3src_single(DisasContext *s, bool o0, bool o1,
6854 int rd, int rn, int rm, int ra)
6856 TCGv_i32 tcg_op1, tcg_op2, tcg_op3;
6857 TCGv_i32 tcg_res = tcg_temp_new_i32();
6858 TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6860 tcg_op1 = read_fp_sreg(s, rn);
6861 tcg_op2 = read_fp_sreg(s, rm);
6862 tcg_op3 = read_fp_sreg(s, ra);
6864 /* These are fused multiply-add, and must be done as one
6865 * floating point operation with no rounding between the
6866 * multiplication and addition steps.
6867 * NB that doing the negations here as separate steps is
6868 * correct : an input NaN should come out with its sign bit
6869 * flipped if it is a negated-input.
6871 if (o1 == true) {
6872 gen_helper_vfp_negs(tcg_op3, tcg_op3);
6875 if (o0 != o1) {
6876 gen_helper_vfp_negs(tcg_op1, tcg_op1);
6879 gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
6881 write_fp_sreg(s, rd, tcg_res);
6883 tcg_temp_free_ptr(fpst);
6884 tcg_temp_free_i32(tcg_op1);
6885 tcg_temp_free_i32(tcg_op2);
6886 tcg_temp_free_i32(tcg_op3);
6887 tcg_temp_free_i32(tcg_res);
6890 /* Floating-point data-processing (3 source) - double precision */
6891 static void handle_fp_3src_double(DisasContext *s, bool o0, bool o1,
6892 int rd, int rn, int rm, int ra)
6894 TCGv_i64 tcg_op1, tcg_op2, tcg_op3;
6895 TCGv_i64 tcg_res = tcg_temp_new_i64();
6896 TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6898 tcg_op1 = read_fp_dreg(s, rn);
6899 tcg_op2 = read_fp_dreg(s, rm);
6900 tcg_op3 = read_fp_dreg(s, ra);
6902 /* These are fused multiply-add, and must be done as one
6903 * floating point operation with no rounding between the
6904 * multiplication and addition steps.
6905 * NB that doing the negations here as separate steps is
6906 * correct : an input NaN should come out with its sign bit
6907 * flipped if it is a negated-input.
6909 if (o1 == true) {
6910 gen_helper_vfp_negd(tcg_op3, tcg_op3);
6913 if (o0 != o1) {
6914 gen_helper_vfp_negd(tcg_op1, tcg_op1);
6917 gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
6919 write_fp_dreg(s, rd, tcg_res);
6921 tcg_temp_free_ptr(fpst);
6922 tcg_temp_free_i64(tcg_op1);
6923 tcg_temp_free_i64(tcg_op2);
6924 tcg_temp_free_i64(tcg_op3);
6925 tcg_temp_free_i64(tcg_res);
6928 /* Floating-point data-processing (3 source) - half precision */
6929 static void handle_fp_3src_half(DisasContext *s, bool o0, bool o1,
6930 int rd, int rn, int rm, int ra)
6932 TCGv_i32 tcg_op1, tcg_op2, tcg_op3;
6933 TCGv_i32 tcg_res = tcg_temp_new_i32();
6934 TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR_F16);
6936 tcg_op1 = read_fp_hreg(s, rn);
6937 tcg_op2 = read_fp_hreg(s, rm);
6938 tcg_op3 = read_fp_hreg(s, ra);
6940 /* These are fused multiply-add, and must be done as one
6941 * floating point operation with no rounding between the
6942 * multiplication and addition steps.
6943 * NB that doing the negations here as separate steps is
6944 * correct : an input NaN should come out with its sign bit
6945 * flipped if it is a negated-input.
6947 if (o1 == true) {
6948 tcg_gen_xori_i32(tcg_op3, tcg_op3, 0x8000);
6951 if (o0 != o1) {
6952 tcg_gen_xori_i32(tcg_op1, tcg_op1, 0x8000);
6955 gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
6957 write_fp_sreg(s, rd, tcg_res);
6959 tcg_temp_free_ptr(fpst);
6960 tcg_temp_free_i32(tcg_op1);
6961 tcg_temp_free_i32(tcg_op2);
6962 tcg_temp_free_i32(tcg_op3);
6963 tcg_temp_free_i32(tcg_res);
6966 /* Floating point data-processing (3 source)
6967 * 31 30 29 28 24 23 22 21 20 16 15 14 10 9 5 4 0
6968 * +---+---+---+-----------+------+----+------+----+------+------+------+
6969 * | M | 0 | S | 1 1 1 1 1 | type | o1 | Rm | o0 | Ra | Rn | Rd |
6970 * +---+---+---+-----------+------+----+------+----+------+------+------+
6972 static void disas_fp_3src(DisasContext *s, uint32_t insn)
6974 int mos = extract32(insn, 29, 3);
6975 int type = extract32(insn, 22, 2);
6976 int rd = extract32(insn, 0, 5);
6977 int rn = extract32(insn, 5, 5);
6978 int ra = extract32(insn, 10, 5);
6979 int rm = extract32(insn, 16, 5);
6980 bool o0 = extract32(insn, 15, 1);
6981 bool o1 = extract32(insn, 21, 1);
6983 if (mos) {
6984 unallocated_encoding(s);
6985 return;
6988 switch (type) {
6989 case 0:
6990 if (!fp_access_check(s)) {
6991 return;
6993 handle_fp_3src_single(s, o0, o1, rd, rn, rm, ra);
6994 break;
6995 case 1:
6996 if (!fp_access_check(s)) {
6997 return;
6999 handle_fp_3src_double(s, o0, o1, rd, rn, rm, ra);
7000 break;
7001 case 3:
7002 if (!dc_isar_feature(aa64_fp16, s)) {
7003 unallocated_encoding(s);
7004 return;
7006 if (!fp_access_check(s)) {
7007 return;
7009 handle_fp_3src_half(s, o0, o1, rd, rn, rm, ra);
7010 break;
7011 default:
7012 unallocated_encoding(s);
7016 /* Floating point immediate
7017 * 31 30 29 28 24 23 22 21 20 13 12 10 9 5 4 0
7018 * +---+---+---+-----------+------+---+------------+-------+------+------+
7019 * | M | 0 | S | 1 1 1 1 0 | type | 1 | imm8 | 1 0 0 | imm5 | Rd |
7020 * +---+---+---+-----------+------+---+------------+-------+------+------+
7022 static void disas_fp_imm(DisasContext *s, uint32_t insn)
7024 int rd = extract32(insn, 0, 5);
7025 int imm5 = extract32(insn, 5, 5);
7026 int imm8 = extract32(insn, 13, 8);
7027 int type = extract32(insn, 22, 2);
7028 int mos = extract32(insn, 29, 3);
7029 uint64_t imm;
7030 MemOp sz;
7032 if (mos || imm5) {
7033 unallocated_encoding(s);
7034 return;
7037 switch (type) {
7038 case 0:
7039 sz = MO_32;
7040 break;
7041 case 1:
7042 sz = MO_64;
7043 break;
7044 case 3:
7045 sz = MO_16;
7046 if (dc_isar_feature(aa64_fp16, s)) {
7047 break;
7049 /* fallthru */
7050 default:
7051 unallocated_encoding(s);
7052 return;
7055 if (!fp_access_check(s)) {
7056 return;
7059 imm = vfp_expand_imm(sz, imm8);
7060 write_fp_dreg(s, rd, tcg_constant_i64(imm));
7063 /* Handle floating point <=> fixed point conversions. Note that we can
7064 * also deal with fp <=> integer conversions as a special case (scale == 64)
7065 * OPTME: consider handling that special case specially or at least skipping
7066 * the call to scalbn in the helpers for zero shifts.
7068 static void handle_fpfpcvt(DisasContext *s, int rd, int rn, int opcode,
7069 bool itof, int rmode, int scale, int sf, int type)
7071 bool is_signed = !(opcode & 1);
7072 TCGv_ptr tcg_fpstatus;
7073 TCGv_i32 tcg_shift, tcg_single;
7074 TCGv_i64 tcg_double;
7076 tcg_fpstatus = fpstatus_ptr(type == 3 ? FPST_FPCR_F16 : FPST_FPCR);
7078 tcg_shift = tcg_constant_i32(64 - scale);
7080 if (itof) {
7081 TCGv_i64 tcg_int = cpu_reg(s, rn);
7082 if (!sf) {
7083 TCGv_i64 tcg_extend = new_tmp_a64(s);
7085 if (is_signed) {
7086 tcg_gen_ext32s_i64(tcg_extend, tcg_int);
7087 } else {
7088 tcg_gen_ext32u_i64(tcg_extend, tcg_int);
7091 tcg_int = tcg_extend;
7094 switch (type) {
7095 case 1: /* float64 */
7096 tcg_double = tcg_temp_new_i64();
7097 if (is_signed) {
7098 gen_helper_vfp_sqtod(tcg_double, tcg_int,
7099 tcg_shift, tcg_fpstatus);
7100 } else {
7101 gen_helper_vfp_uqtod(tcg_double, tcg_int,
7102 tcg_shift, tcg_fpstatus);
7104 write_fp_dreg(s, rd, tcg_double);
7105 tcg_temp_free_i64(tcg_double);
7106 break;
7108 case 0: /* float32 */
7109 tcg_single = tcg_temp_new_i32();
7110 if (is_signed) {
7111 gen_helper_vfp_sqtos(tcg_single, tcg_int,
7112 tcg_shift, tcg_fpstatus);
7113 } else {
7114 gen_helper_vfp_uqtos(tcg_single, tcg_int,
7115 tcg_shift, tcg_fpstatus);
7117 write_fp_sreg(s, rd, tcg_single);
7118 tcg_temp_free_i32(tcg_single);
7119 break;
7121 case 3: /* float16 */
7122 tcg_single = tcg_temp_new_i32();
7123 if (is_signed) {
7124 gen_helper_vfp_sqtoh(tcg_single, tcg_int,
7125 tcg_shift, tcg_fpstatus);
7126 } else {
7127 gen_helper_vfp_uqtoh(tcg_single, tcg_int,
7128 tcg_shift, tcg_fpstatus);
7130 write_fp_sreg(s, rd, tcg_single);
7131 tcg_temp_free_i32(tcg_single);
7132 break;
7134 default:
7135 g_assert_not_reached();
7137 } else {
7138 TCGv_i64 tcg_int = cpu_reg(s, rd);
7139 TCGv_i32 tcg_rmode;
7141 if (extract32(opcode, 2, 1)) {
7142 /* There are too many rounding modes to all fit into rmode,
7143 * so FCVTA[US] is a special case.
7145 rmode = FPROUNDING_TIEAWAY;
7148 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rmode));
7150 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
7152 switch (type) {
7153 case 1: /* float64 */
7154 tcg_double = read_fp_dreg(s, rn);
7155 if (is_signed) {
7156 if (!sf) {
7157 gen_helper_vfp_tosld(tcg_int, tcg_double,
7158 tcg_shift, tcg_fpstatus);
7159 } else {
7160 gen_helper_vfp_tosqd(tcg_int, tcg_double,
7161 tcg_shift, tcg_fpstatus);
7163 } else {
7164 if (!sf) {
7165 gen_helper_vfp_tould(tcg_int, tcg_double,
7166 tcg_shift, tcg_fpstatus);
7167 } else {
7168 gen_helper_vfp_touqd(tcg_int, tcg_double,
7169 tcg_shift, tcg_fpstatus);
7172 if (!sf) {
7173 tcg_gen_ext32u_i64(tcg_int, tcg_int);
7175 tcg_temp_free_i64(tcg_double);
7176 break;
7178 case 0: /* float32 */
7179 tcg_single = read_fp_sreg(s, rn);
7180 if (sf) {
7181 if (is_signed) {
7182 gen_helper_vfp_tosqs(tcg_int, tcg_single,
7183 tcg_shift, tcg_fpstatus);
7184 } else {
7185 gen_helper_vfp_touqs(tcg_int, tcg_single,
7186 tcg_shift, tcg_fpstatus);
7188 } else {
7189 TCGv_i32 tcg_dest = tcg_temp_new_i32();
7190 if (is_signed) {
7191 gen_helper_vfp_tosls(tcg_dest, tcg_single,
7192 tcg_shift, tcg_fpstatus);
7193 } else {
7194 gen_helper_vfp_touls(tcg_dest, tcg_single,
7195 tcg_shift, tcg_fpstatus);
7197 tcg_gen_extu_i32_i64(tcg_int, tcg_dest);
7198 tcg_temp_free_i32(tcg_dest);
7200 tcg_temp_free_i32(tcg_single);
7201 break;
7203 case 3: /* float16 */
7204 tcg_single = read_fp_sreg(s, rn);
7205 if (sf) {
7206 if (is_signed) {
7207 gen_helper_vfp_tosqh(tcg_int, tcg_single,
7208 tcg_shift, tcg_fpstatus);
7209 } else {
7210 gen_helper_vfp_touqh(tcg_int, tcg_single,
7211 tcg_shift, tcg_fpstatus);
7213 } else {
7214 TCGv_i32 tcg_dest = tcg_temp_new_i32();
7215 if (is_signed) {
7216 gen_helper_vfp_toslh(tcg_dest, tcg_single,
7217 tcg_shift, tcg_fpstatus);
7218 } else {
7219 gen_helper_vfp_toulh(tcg_dest, tcg_single,
7220 tcg_shift, tcg_fpstatus);
7222 tcg_gen_extu_i32_i64(tcg_int, tcg_dest);
7223 tcg_temp_free_i32(tcg_dest);
7225 tcg_temp_free_i32(tcg_single);
7226 break;
7228 default:
7229 g_assert_not_reached();
7232 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
7233 tcg_temp_free_i32(tcg_rmode);
7236 tcg_temp_free_ptr(tcg_fpstatus);
7239 /* Floating point <-> fixed point conversions
7240 * 31 30 29 28 24 23 22 21 20 19 18 16 15 10 9 5 4 0
7241 * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
7242 * | sf | 0 | S | 1 1 1 1 0 | type | 0 | rmode | opcode | scale | Rn | Rd |
7243 * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
7245 static void disas_fp_fixed_conv(DisasContext *s, uint32_t insn)
7247 int rd = extract32(insn, 0, 5);
7248 int rn = extract32(insn, 5, 5);
7249 int scale = extract32(insn, 10, 6);
7250 int opcode = extract32(insn, 16, 3);
7251 int rmode = extract32(insn, 19, 2);
7252 int type = extract32(insn, 22, 2);
7253 bool sbit = extract32(insn, 29, 1);
7254 bool sf = extract32(insn, 31, 1);
7255 bool itof;
7257 if (sbit || (!sf && scale < 32)) {
7258 unallocated_encoding(s);
7259 return;
7262 switch (type) {
7263 case 0: /* float32 */
7264 case 1: /* float64 */
7265 break;
7266 case 3: /* float16 */
7267 if (dc_isar_feature(aa64_fp16, s)) {
7268 break;
7270 /* fallthru */
7271 default:
7272 unallocated_encoding(s);
7273 return;
7276 switch ((rmode << 3) | opcode) {
7277 case 0x2: /* SCVTF */
7278 case 0x3: /* UCVTF */
7279 itof = true;
7280 break;
7281 case 0x18: /* FCVTZS */
7282 case 0x19: /* FCVTZU */
7283 itof = false;
7284 break;
7285 default:
7286 unallocated_encoding(s);
7287 return;
7290 if (!fp_access_check(s)) {
7291 return;
7294 handle_fpfpcvt(s, rd, rn, opcode, itof, FPROUNDING_ZERO, scale, sf, type);
7297 static void handle_fmov(DisasContext *s, int rd, int rn, int type, bool itof)
7299 /* FMOV: gpr to or from float, double, or top half of quad fp reg,
7300 * without conversion.
7303 if (itof) {
7304 TCGv_i64 tcg_rn = cpu_reg(s, rn);
7305 TCGv_i64 tmp;
7307 switch (type) {
7308 case 0:
7309 /* 32 bit */
7310 tmp = tcg_temp_new_i64();
7311 tcg_gen_ext32u_i64(tmp, tcg_rn);
7312 write_fp_dreg(s, rd, tmp);
7313 tcg_temp_free_i64(tmp);
7314 break;
7315 case 1:
7316 /* 64 bit */
7317 write_fp_dreg(s, rd, tcg_rn);
7318 break;
7319 case 2:
7320 /* 64 bit to top half. */
7321 tcg_gen_st_i64(tcg_rn, cpu_env, fp_reg_hi_offset(s, rd));
7322 clear_vec_high(s, true, rd);
7323 break;
7324 case 3:
7325 /* 16 bit */
7326 tmp = tcg_temp_new_i64();
7327 tcg_gen_ext16u_i64(tmp, tcg_rn);
7328 write_fp_dreg(s, rd, tmp);
7329 tcg_temp_free_i64(tmp);
7330 break;
7331 default:
7332 g_assert_not_reached();
7334 } else {
7335 TCGv_i64 tcg_rd = cpu_reg(s, rd);
7337 switch (type) {
7338 case 0:
7339 /* 32 bit */
7340 tcg_gen_ld32u_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_32));
7341 break;
7342 case 1:
7343 /* 64 bit */
7344 tcg_gen_ld_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_64));
7345 break;
7346 case 2:
7347 /* 64 bits from top half */
7348 tcg_gen_ld_i64(tcg_rd, cpu_env, fp_reg_hi_offset(s, rn));
7349 break;
7350 case 3:
7351 /* 16 bit */
7352 tcg_gen_ld16u_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_16));
7353 break;
7354 default:
7355 g_assert_not_reached();
7360 static void handle_fjcvtzs(DisasContext *s, int rd, int rn)
7362 TCGv_i64 t = read_fp_dreg(s, rn);
7363 TCGv_ptr fpstatus = fpstatus_ptr(FPST_FPCR);
7365 gen_helper_fjcvtzs(t, t, fpstatus);
7367 tcg_temp_free_ptr(fpstatus);
7369 tcg_gen_ext32u_i64(cpu_reg(s, rd), t);
7370 tcg_gen_extrh_i64_i32(cpu_ZF, t);
7371 tcg_gen_movi_i32(cpu_CF, 0);
7372 tcg_gen_movi_i32(cpu_NF, 0);
7373 tcg_gen_movi_i32(cpu_VF, 0);
7375 tcg_temp_free_i64(t);
7378 /* Floating point <-> integer conversions
7379 * 31 30 29 28 24 23 22 21 20 19 18 16 15 10 9 5 4 0
7380 * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
7381 * | sf | 0 | S | 1 1 1 1 0 | type | 1 | rmode | opc | 0 0 0 0 0 0 | Rn | Rd |
7382 * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
7384 static void disas_fp_int_conv(DisasContext *s, uint32_t insn)
7386 int rd = extract32(insn, 0, 5);
7387 int rn = extract32(insn, 5, 5);
7388 int opcode = extract32(insn, 16, 3);
7389 int rmode = extract32(insn, 19, 2);
7390 int type = extract32(insn, 22, 2);
7391 bool sbit = extract32(insn, 29, 1);
7392 bool sf = extract32(insn, 31, 1);
7393 bool itof = false;
7395 if (sbit) {
7396 goto do_unallocated;
7399 switch (opcode) {
7400 case 2: /* SCVTF */
7401 case 3: /* UCVTF */
7402 itof = true;
7403 /* fallthru */
7404 case 4: /* FCVTAS */
7405 case 5: /* FCVTAU */
7406 if (rmode != 0) {
7407 goto do_unallocated;
7409 /* fallthru */
7410 case 0: /* FCVT[NPMZ]S */
7411 case 1: /* FCVT[NPMZ]U */
7412 switch (type) {
7413 case 0: /* float32 */
7414 case 1: /* float64 */
7415 break;
7416 case 3: /* float16 */
7417 if (!dc_isar_feature(aa64_fp16, s)) {
7418 goto do_unallocated;
7420 break;
7421 default:
7422 goto do_unallocated;
7424 if (!fp_access_check(s)) {
7425 return;
7427 handle_fpfpcvt(s, rd, rn, opcode, itof, rmode, 64, sf, type);
7428 break;
7430 default:
7431 switch (sf << 7 | type << 5 | rmode << 3 | opcode) {
7432 case 0b01100110: /* FMOV half <-> 32-bit int */
7433 case 0b01100111:
7434 case 0b11100110: /* FMOV half <-> 64-bit int */
7435 case 0b11100111:
7436 if (!dc_isar_feature(aa64_fp16, s)) {
7437 goto do_unallocated;
7439 /* fallthru */
7440 case 0b00000110: /* FMOV 32-bit */
7441 case 0b00000111:
7442 case 0b10100110: /* FMOV 64-bit */
7443 case 0b10100111:
7444 case 0b11001110: /* FMOV top half of 128-bit */
7445 case 0b11001111:
7446 if (!fp_access_check(s)) {
7447 return;
7449 itof = opcode & 1;
7450 handle_fmov(s, rd, rn, type, itof);
7451 break;
7453 case 0b00111110: /* FJCVTZS */
7454 if (!dc_isar_feature(aa64_jscvt, s)) {
7455 goto do_unallocated;
7456 } else if (fp_access_check(s)) {
7457 handle_fjcvtzs(s, rd, rn);
7459 break;
7461 default:
7462 do_unallocated:
7463 unallocated_encoding(s);
7464 return;
7466 break;
7470 /* FP-specific subcases of table C3-6 (SIMD and FP data processing)
7471 * 31 30 29 28 25 24 0
7472 * +---+---+---+---------+-----------------------------+
7473 * | | 0 | | 1 1 1 1 | |
7474 * +---+---+---+---------+-----------------------------+
7476 static void disas_data_proc_fp(DisasContext *s, uint32_t insn)
7478 if (extract32(insn, 24, 1)) {
7479 /* Floating point data-processing (3 source) */
7480 disas_fp_3src(s, insn);
7481 } else if (extract32(insn, 21, 1) == 0) {
7482 /* Floating point to fixed point conversions */
7483 disas_fp_fixed_conv(s, insn);
7484 } else {
7485 switch (extract32(insn, 10, 2)) {
7486 case 1:
7487 /* Floating point conditional compare */
7488 disas_fp_ccomp(s, insn);
7489 break;
7490 case 2:
7491 /* Floating point data-processing (2 source) */
7492 disas_fp_2src(s, insn);
7493 break;
7494 case 3:
7495 /* Floating point conditional select */
7496 disas_fp_csel(s, insn);
7497 break;
7498 case 0:
7499 switch (ctz32(extract32(insn, 12, 4))) {
7500 case 0: /* [15:12] == xxx1 */
7501 /* Floating point immediate */
7502 disas_fp_imm(s, insn);
7503 break;
7504 case 1: /* [15:12] == xx10 */
7505 /* Floating point compare */
7506 disas_fp_compare(s, insn);
7507 break;
7508 case 2: /* [15:12] == x100 */
7509 /* Floating point data-processing (1 source) */
7510 disas_fp_1src(s, insn);
7511 break;
7512 case 3: /* [15:12] == 1000 */
7513 unallocated_encoding(s);
7514 break;
7515 default: /* [15:12] == 0000 */
7516 /* Floating point <-> integer conversions */
7517 disas_fp_int_conv(s, insn);
7518 break;
7520 break;
7525 static void do_ext64(DisasContext *s, TCGv_i64 tcg_left, TCGv_i64 tcg_right,
7526 int pos)
7528 /* Extract 64 bits from the middle of two concatenated 64 bit
7529 * vector register slices left:right. The extracted bits start
7530 * at 'pos' bits into the right (least significant) side.
7531 * We return the result in tcg_right, and guarantee not to
7532 * trash tcg_left.
7534 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
7535 assert(pos > 0 && pos < 64);
7537 tcg_gen_shri_i64(tcg_right, tcg_right, pos);
7538 tcg_gen_shli_i64(tcg_tmp, tcg_left, 64 - pos);
7539 tcg_gen_or_i64(tcg_right, tcg_right, tcg_tmp);
7541 tcg_temp_free_i64(tcg_tmp);
7544 /* EXT
7545 * 31 30 29 24 23 22 21 20 16 15 14 11 10 9 5 4 0
7546 * +---+---+-------------+-----+---+------+---+------+---+------+------+
7547 * | 0 | Q | 1 0 1 1 1 0 | op2 | 0 | Rm | 0 | imm4 | 0 | Rn | Rd |
7548 * +---+---+-------------+-----+---+------+---+------+---+------+------+
7550 static void disas_simd_ext(DisasContext *s, uint32_t insn)
7552 int is_q = extract32(insn, 30, 1);
7553 int op2 = extract32(insn, 22, 2);
7554 int imm4 = extract32(insn, 11, 4);
7555 int rm = extract32(insn, 16, 5);
7556 int rn = extract32(insn, 5, 5);
7557 int rd = extract32(insn, 0, 5);
7558 int pos = imm4 << 3;
7559 TCGv_i64 tcg_resl, tcg_resh;
7561 if (op2 != 0 || (!is_q && extract32(imm4, 3, 1))) {
7562 unallocated_encoding(s);
7563 return;
7566 if (!fp_access_check(s)) {
7567 return;
7570 tcg_resh = tcg_temp_new_i64();
7571 tcg_resl = tcg_temp_new_i64();
7573 /* Vd gets bits starting at pos bits into Vm:Vn. This is
7574 * either extracting 128 bits from a 128:128 concatenation, or
7575 * extracting 64 bits from a 64:64 concatenation.
7577 if (!is_q) {
7578 read_vec_element(s, tcg_resl, rn, 0, MO_64);
7579 if (pos != 0) {
7580 read_vec_element(s, tcg_resh, rm, 0, MO_64);
7581 do_ext64(s, tcg_resh, tcg_resl, pos);
7583 } else {
7584 TCGv_i64 tcg_hh;
7585 typedef struct {
7586 int reg;
7587 int elt;
7588 } EltPosns;
7589 EltPosns eltposns[] = { {rn, 0}, {rn, 1}, {rm, 0}, {rm, 1} };
7590 EltPosns *elt = eltposns;
7592 if (pos >= 64) {
7593 elt++;
7594 pos -= 64;
7597 read_vec_element(s, tcg_resl, elt->reg, elt->elt, MO_64);
7598 elt++;
7599 read_vec_element(s, tcg_resh, elt->reg, elt->elt, MO_64);
7600 elt++;
7601 if (pos != 0) {
7602 do_ext64(s, tcg_resh, tcg_resl, pos);
7603 tcg_hh = tcg_temp_new_i64();
7604 read_vec_element(s, tcg_hh, elt->reg, elt->elt, MO_64);
7605 do_ext64(s, tcg_hh, tcg_resh, pos);
7606 tcg_temp_free_i64(tcg_hh);
7610 write_vec_element(s, tcg_resl, rd, 0, MO_64);
7611 tcg_temp_free_i64(tcg_resl);
7612 if (is_q) {
7613 write_vec_element(s, tcg_resh, rd, 1, MO_64);
7615 tcg_temp_free_i64(tcg_resh);
7616 clear_vec_high(s, is_q, rd);
7619 /* TBL/TBX
7620 * 31 30 29 24 23 22 21 20 16 15 14 13 12 11 10 9 5 4 0
7621 * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
7622 * | 0 | Q | 0 0 1 1 1 0 | op2 | 0 | Rm | 0 | len | op | 0 0 | Rn | Rd |
7623 * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
7625 static void disas_simd_tb(DisasContext *s, uint32_t insn)
7627 int op2 = extract32(insn, 22, 2);
7628 int is_q = extract32(insn, 30, 1);
7629 int rm = extract32(insn, 16, 5);
7630 int rn = extract32(insn, 5, 5);
7631 int rd = extract32(insn, 0, 5);
7632 int is_tbx = extract32(insn, 12, 1);
7633 int len = (extract32(insn, 13, 2) + 1) * 16;
7635 if (op2 != 0) {
7636 unallocated_encoding(s);
7637 return;
7640 if (!fp_access_check(s)) {
7641 return;
7644 tcg_gen_gvec_2_ptr(vec_full_reg_offset(s, rd),
7645 vec_full_reg_offset(s, rm), cpu_env,
7646 is_q ? 16 : 8, vec_full_reg_size(s),
7647 (len << 6) | (is_tbx << 5) | rn,
7648 gen_helper_simd_tblx);
7651 /* ZIP/UZP/TRN
7652 * 31 30 29 24 23 22 21 20 16 15 14 12 11 10 9 5 4 0
7653 * +---+---+-------------+------+---+------+---+------------------+------+
7654 * | 0 | Q | 0 0 1 1 1 0 | size | 0 | Rm | 0 | opc | 1 0 | Rn | Rd |
7655 * +---+---+-------------+------+---+------+---+------------------+------+
7657 static void disas_simd_zip_trn(DisasContext *s, uint32_t insn)
7659 int rd = extract32(insn, 0, 5);
7660 int rn = extract32(insn, 5, 5);
7661 int rm = extract32(insn, 16, 5);
7662 int size = extract32(insn, 22, 2);
7663 /* opc field bits [1:0] indicate ZIP/UZP/TRN;
7664 * bit 2 indicates 1 vs 2 variant of the insn.
7666 int opcode = extract32(insn, 12, 2);
7667 bool part = extract32(insn, 14, 1);
7668 bool is_q = extract32(insn, 30, 1);
7669 int esize = 8 << size;
7670 int i, ofs;
7671 int datasize = is_q ? 128 : 64;
7672 int elements = datasize / esize;
7673 TCGv_i64 tcg_res, tcg_resl, tcg_resh;
7675 if (opcode == 0 || (size == 3 && !is_q)) {
7676 unallocated_encoding(s);
7677 return;
7680 if (!fp_access_check(s)) {
7681 return;
7684 tcg_resl = tcg_const_i64(0);
7685 tcg_resh = is_q ? tcg_const_i64(0) : NULL;
7686 tcg_res = tcg_temp_new_i64();
7688 for (i = 0; i < elements; i++) {
7689 switch (opcode) {
7690 case 1: /* UZP1/2 */
7692 int midpoint = elements / 2;
7693 if (i < midpoint) {
7694 read_vec_element(s, tcg_res, rn, 2 * i + part, size);
7695 } else {
7696 read_vec_element(s, tcg_res, rm,
7697 2 * (i - midpoint) + part, size);
7699 break;
7701 case 2: /* TRN1/2 */
7702 if (i & 1) {
7703 read_vec_element(s, tcg_res, rm, (i & ~1) + part, size);
7704 } else {
7705 read_vec_element(s, tcg_res, rn, (i & ~1) + part, size);
7707 break;
7708 case 3: /* ZIP1/2 */
7710 int base = part * elements / 2;
7711 if (i & 1) {
7712 read_vec_element(s, tcg_res, rm, base + (i >> 1), size);
7713 } else {
7714 read_vec_element(s, tcg_res, rn, base + (i >> 1), size);
7716 break;
7718 default:
7719 g_assert_not_reached();
7722 ofs = i * esize;
7723 if (ofs < 64) {
7724 tcg_gen_shli_i64(tcg_res, tcg_res, ofs);
7725 tcg_gen_or_i64(tcg_resl, tcg_resl, tcg_res);
7726 } else {
7727 tcg_gen_shli_i64(tcg_res, tcg_res, ofs - 64);
7728 tcg_gen_or_i64(tcg_resh, tcg_resh, tcg_res);
7732 tcg_temp_free_i64(tcg_res);
7734 write_vec_element(s, tcg_resl, rd, 0, MO_64);
7735 tcg_temp_free_i64(tcg_resl);
7737 if (is_q) {
7738 write_vec_element(s, tcg_resh, rd, 1, MO_64);
7739 tcg_temp_free_i64(tcg_resh);
7741 clear_vec_high(s, is_q, rd);
7745 * do_reduction_op helper
7747 * This mirrors the Reduce() pseudocode in the ARM ARM. It is
7748 * important for correct NaN propagation that we do these
7749 * operations in exactly the order specified by the pseudocode.
7751 * This is a recursive function, TCG temps should be freed by the
7752 * calling function once it is done with the values.
7754 static TCGv_i32 do_reduction_op(DisasContext *s, int fpopcode, int rn,
7755 int esize, int size, int vmap, TCGv_ptr fpst)
7757 if (esize == size) {
7758 int element;
7759 MemOp msize = esize == 16 ? MO_16 : MO_32;
7760 TCGv_i32 tcg_elem;
7762 /* We should have one register left here */
7763 assert(ctpop8(vmap) == 1);
7764 element = ctz32(vmap);
7765 assert(element < 8);
7767 tcg_elem = tcg_temp_new_i32();
7768 read_vec_element_i32(s, tcg_elem, rn, element, msize);
7769 return tcg_elem;
7770 } else {
7771 int bits = size / 2;
7772 int shift = ctpop8(vmap) / 2;
7773 int vmap_lo = (vmap >> shift) & vmap;
7774 int vmap_hi = (vmap & ~vmap_lo);
7775 TCGv_i32 tcg_hi, tcg_lo, tcg_res;
7777 tcg_hi = do_reduction_op(s, fpopcode, rn, esize, bits, vmap_hi, fpst);
7778 tcg_lo = do_reduction_op(s, fpopcode, rn, esize, bits, vmap_lo, fpst);
7779 tcg_res = tcg_temp_new_i32();
7781 switch (fpopcode) {
7782 case 0x0c: /* fmaxnmv half-precision */
7783 gen_helper_advsimd_maxnumh(tcg_res, tcg_lo, tcg_hi, fpst);
7784 break;
7785 case 0x0f: /* fmaxv half-precision */
7786 gen_helper_advsimd_maxh(tcg_res, tcg_lo, tcg_hi, fpst);
7787 break;
7788 case 0x1c: /* fminnmv half-precision */
7789 gen_helper_advsimd_minnumh(tcg_res, tcg_lo, tcg_hi, fpst);
7790 break;
7791 case 0x1f: /* fminv half-precision */
7792 gen_helper_advsimd_minh(tcg_res, tcg_lo, tcg_hi, fpst);
7793 break;
7794 case 0x2c: /* fmaxnmv */
7795 gen_helper_vfp_maxnums(tcg_res, tcg_lo, tcg_hi, fpst);
7796 break;
7797 case 0x2f: /* fmaxv */
7798 gen_helper_vfp_maxs(tcg_res, tcg_lo, tcg_hi, fpst);
7799 break;
7800 case 0x3c: /* fminnmv */
7801 gen_helper_vfp_minnums(tcg_res, tcg_lo, tcg_hi, fpst);
7802 break;
7803 case 0x3f: /* fminv */
7804 gen_helper_vfp_mins(tcg_res, tcg_lo, tcg_hi, fpst);
7805 break;
7806 default:
7807 g_assert_not_reached();
7810 tcg_temp_free_i32(tcg_hi);
7811 tcg_temp_free_i32(tcg_lo);
7812 return tcg_res;
7816 /* AdvSIMD across lanes
7817 * 31 30 29 28 24 23 22 21 17 16 12 11 10 9 5 4 0
7818 * +---+---+---+-----------+------+-----------+--------+-----+------+------+
7819 * | 0 | Q | U | 0 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 | Rn | Rd |
7820 * +---+---+---+-----------+------+-----------+--------+-----+------+------+
7822 static void disas_simd_across_lanes(DisasContext *s, uint32_t insn)
7824 int rd = extract32(insn, 0, 5);
7825 int rn = extract32(insn, 5, 5);
7826 int size = extract32(insn, 22, 2);
7827 int opcode = extract32(insn, 12, 5);
7828 bool is_q = extract32(insn, 30, 1);
7829 bool is_u = extract32(insn, 29, 1);
7830 bool is_fp = false;
7831 bool is_min = false;
7832 int esize;
7833 int elements;
7834 int i;
7835 TCGv_i64 tcg_res, tcg_elt;
7837 switch (opcode) {
7838 case 0x1b: /* ADDV */
7839 if (is_u) {
7840 unallocated_encoding(s);
7841 return;
7843 /* fall through */
7844 case 0x3: /* SADDLV, UADDLV */
7845 case 0xa: /* SMAXV, UMAXV */
7846 case 0x1a: /* SMINV, UMINV */
7847 if (size == 3 || (size == 2 && !is_q)) {
7848 unallocated_encoding(s);
7849 return;
7851 break;
7852 case 0xc: /* FMAXNMV, FMINNMV */
7853 case 0xf: /* FMAXV, FMINV */
7854 /* Bit 1 of size field encodes min vs max and the actual size
7855 * depends on the encoding of the U bit. If not set (and FP16
7856 * enabled) then we do half-precision float instead of single
7857 * precision.
7859 is_min = extract32(size, 1, 1);
7860 is_fp = true;
7861 if (!is_u && dc_isar_feature(aa64_fp16, s)) {
7862 size = 1;
7863 } else if (!is_u || !is_q || extract32(size, 0, 1)) {
7864 unallocated_encoding(s);
7865 return;
7866 } else {
7867 size = 2;
7869 break;
7870 default:
7871 unallocated_encoding(s);
7872 return;
7875 if (!fp_access_check(s)) {
7876 return;
7879 esize = 8 << size;
7880 elements = (is_q ? 128 : 64) / esize;
7882 tcg_res = tcg_temp_new_i64();
7883 tcg_elt = tcg_temp_new_i64();
7885 /* These instructions operate across all lanes of a vector
7886 * to produce a single result. We can guarantee that a 64
7887 * bit intermediate is sufficient:
7888 * + for [US]ADDLV the maximum element size is 32 bits, and
7889 * the result type is 64 bits
7890 * + for FMAX*V, FMIN*V, ADDV the intermediate type is the
7891 * same as the element size, which is 32 bits at most
7892 * For the integer operations we can choose to work at 64
7893 * or 32 bits and truncate at the end; for simplicity
7894 * we use 64 bits always. The floating point
7895 * ops do require 32 bit intermediates, though.
7897 if (!is_fp) {
7898 read_vec_element(s, tcg_res, rn, 0, size | (is_u ? 0 : MO_SIGN));
7900 for (i = 1; i < elements; i++) {
7901 read_vec_element(s, tcg_elt, rn, i, size | (is_u ? 0 : MO_SIGN));
7903 switch (opcode) {
7904 case 0x03: /* SADDLV / UADDLV */
7905 case 0x1b: /* ADDV */
7906 tcg_gen_add_i64(tcg_res, tcg_res, tcg_elt);
7907 break;
7908 case 0x0a: /* SMAXV / UMAXV */
7909 if (is_u) {
7910 tcg_gen_umax_i64(tcg_res, tcg_res, tcg_elt);
7911 } else {
7912 tcg_gen_smax_i64(tcg_res, tcg_res, tcg_elt);
7914 break;
7915 case 0x1a: /* SMINV / UMINV */
7916 if (is_u) {
7917 tcg_gen_umin_i64(tcg_res, tcg_res, tcg_elt);
7918 } else {
7919 tcg_gen_smin_i64(tcg_res, tcg_res, tcg_elt);
7921 break;
7922 default:
7923 g_assert_not_reached();
7927 } else {
7928 /* Floating point vector reduction ops which work across 32
7929 * bit (single) or 16 bit (half-precision) intermediates.
7930 * Note that correct NaN propagation requires that we do these
7931 * operations in exactly the order specified by the pseudocode.
7933 TCGv_ptr fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
7934 int fpopcode = opcode | is_min << 4 | is_u << 5;
7935 int vmap = (1 << elements) - 1;
7936 TCGv_i32 tcg_res32 = do_reduction_op(s, fpopcode, rn, esize,
7937 (is_q ? 128 : 64), vmap, fpst);
7938 tcg_gen_extu_i32_i64(tcg_res, tcg_res32);
7939 tcg_temp_free_i32(tcg_res32);
7940 tcg_temp_free_ptr(fpst);
7943 tcg_temp_free_i64(tcg_elt);
7945 /* Now truncate the result to the width required for the final output */
7946 if (opcode == 0x03) {
7947 /* SADDLV, UADDLV: result is 2*esize */
7948 size++;
7951 switch (size) {
7952 case 0:
7953 tcg_gen_ext8u_i64(tcg_res, tcg_res);
7954 break;
7955 case 1:
7956 tcg_gen_ext16u_i64(tcg_res, tcg_res);
7957 break;
7958 case 2:
7959 tcg_gen_ext32u_i64(tcg_res, tcg_res);
7960 break;
7961 case 3:
7962 break;
7963 default:
7964 g_assert_not_reached();
7967 write_fp_dreg(s, rd, tcg_res);
7968 tcg_temp_free_i64(tcg_res);
7971 /* DUP (Element, Vector)
7973 * 31 30 29 21 20 16 15 10 9 5 4 0
7974 * +---+---+-------------------+--------+-------------+------+------+
7975 * | 0 | Q | 0 0 1 1 1 0 0 0 0 | imm5 | 0 0 0 0 0 1 | Rn | Rd |
7976 * +---+---+-------------------+--------+-------------+------+------+
7978 * size: encoded in imm5 (see ARM ARM LowestSetBit())
7980 static void handle_simd_dupe(DisasContext *s, int is_q, int rd, int rn,
7981 int imm5)
7983 int size = ctz32(imm5);
7984 int index;
7986 if (size > 3 || (size == 3 && !is_q)) {
7987 unallocated_encoding(s);
7988 return;
7991 if (!fp_access_check(s)) {
7992 return;
7995 index = imm5 >> (size + 1);
7996 tcg_gen_gvec_dup_mem(size, vec_full_reg_offset(s, rd),
7997 vec_reg_offset(s, rn, index, size),
7998 is_q ? 16 : 8, vec_full_reg_size(s));
8001 /* DUP (element, scalar)
8002 * 31 21 20 16 15 10 9 5 4 0
8003 * +-----------------------+--------+-------------+------+------+
8004 * | 0 1 0 1 1 1 1 0 0 0 0 | imm5 | 0 0 0 0 0 1 | Rn | Rd |
8005 * +-----------------------+--------+-------------+------+------+
8007 static void handle_simd_dupes(DisasContext *s, int rd, int rn,
8008 int imm5)
8010 int size = ctz32(imm5);
8011 int index;
8012 TCGv_i64 tmp;
8014 if (size > 3) {
8015 unallocated_encoding(s);
8016 return;
8019 if (!fp_access_check(s)) {
8020 return;
8023 index = imm5 >> (size + 1);
8025 /* This instruction just extracts the specified element and
8026 * zero-extends it into the bottom of the destination register.
8028 tmp = tcg_temp_new_i64();
8029 read_vec_element(s, tmp, rn, index, size);
8030 write_fp_dreg(s, rd, tmp);
8031 tcg_temp_free_i64(tmp);
8034 /* DUP (General)
8036 * 31 30 29 21 20 16 15 10 9 5 4 0
8037 * +---+---+-------------------+--------+-------------+------+------+
8038 * | 0 | Q | 0 0 1 1 1 0 0 0 0 | imm5 | 0 0 0 0 1 1 | Rn | Rd |
8039 * +---+---+-------------------+--------+-------------+------+------+
8041 * size: encoded in imm5 (see ARM ARM LowestSetBit())
8043 static void handle_simd_dupg(DisasContext *s, int is_q, int rd, int rn,
8044 int imm5)
8046 int size = ctz32(imm5);
8047 uint32_t dofs, oprsz, maxsz;
8049 if (size > 3 || ((size == 3) && !is_q)) {
8050 unallocated_encoding(s);
8051 return;
8054 if (!fp_access_check(s)) {
8055 return;
8058 dofs = vec_full_reg_offset(s, rd);
8059 oprsz = is_q ? 16 : 8;
8060 maxsz = vec_full_reg_size(s);
8062 tcg_gen_gvec_dup_i64(size, dofs, oprsz, maxsz, cpu_reg(s, rn));
8065 /* INS (Element)
8067 * 31 21 20 16 15 14 11 10 9 5 4 0
8068 * +-----------------------+--------+------------+---+------+------+
8069 * | 0 1 1 0 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 | Rn | Rd |
8070 * +-----------------------+--------+------------+---+------+------+
8072 * size: encoded in imm5 (see ARM ARM LowestSetBit())
8073 * index: encoded in imm5<4:size+1>
8075 static void handle_simd_inse(DisasContext *s, int rd, int rn,
8076 int imm4, int imm5)
8078 int size = ctz32(imm5);
8079 int src_index, dst_index;
8080 TCGv_i64 tmp;
8082 if (size > 3) {
8083 unallocated_encoding(s);
8084 return;
8087 if (!fp_access_check(s)) {
8088 return;
8091 dst_index = extract32(imm5, 1+size, 5);
8092 src_index = extract32(imm4, size, 4);
8094 tmp = tcg_temp_new_i64();
8096 read_vec_element(s, tmp, rn, src_index, size);
8097 write_vec_element(s, tmp, rd, dst_index, size);
8099 tcg_temp_free_i64(tmp);
8101 /* INS is considered a 128-bit write for SVE. */
8102 clear_vec_high(s, true, rd);
8106 /* INS (General)
8108 * 31 21 20 16 15 10 9 5 4 0
8109 * +-----------------------+--------+-------------+------+------+
8110 * | 0 1 0 0 1 1 1 0 0 0 0 | imm5 | 0 0 0 1 1 1 | Rn | Rd |
8111 * +-----------------------+--------+-------------+------+------+
8113 * size: encoded in imm5 (see ARM ARM LowestSetBit())
8114 * index: encoded in imm5<4:size+1>
8116 static void handle_simd_insg(DisasContext *s, int rd, int rn, int imm5)
8118 int size = ctz32(imm5);
8119 int idx;
8121 if (size > 3) {
8122 unallocated_encoding(s);
8123 return;
8126 if (!fp_access_check(s)) {
8127 return;
8130 idx = extract32(imm5, 1 + size, 4 - size);
8131 write_vec_element(s, cpu_reg(s, rn), rd, idx, size);
8133 /* INS is considered a 128-bit write for SVE. */
8134 clear_vec_high(s, true, rd);
8138 * UMOV (General)
8139 * SMOV (General)
8141 * 31 30 29 21 20 16 15 12 10 9 5 4 0
8142 * +---+---+-------------------+--------+-------------+------+------+
8143 * | 0 | Q | 0 0 1 1 1 0 0 0 0 | imm5 | 0 0 1 U 1 1 | Rn | Rd |
8144 * +---+---+-------------------+--------+-------------+------+------+
8146 * U: unsigned when set
8147 * size: encoded in imm5 (see ARM ARM LowestSetBit())
8149 static void handle_simd_umov_smov(DisasContext *s, int is_q, int is_signed,
8150 int rn, int rd, int imm5)
8152 int size = ctz32(imm5);
8153 int element;
8154 TCGv_i64 tcg_rd;
8156 /* Check for UnallocatedEncodings */
8157 if (is_signed) {
8158 if (size > 2 || (size == 2 && !is_q)) {
8159 unallocated_encoding(s);
8160 return;
8162 } else {
8163 if (size > 3
8164 || (size < 3 && is_q)
8165 || (size == 3 && !is_q)) {
8166 unallocated_encoding(s);
8167 return;
8171 if (!fp_access_check(s)) {
8172 return;
8175 element = extract32(imm5, 1+size, 4);
8177 tcg_rd = cpu_reg(s, rd);
8178 read_vec_element(s, tcg_rd, rn, element, size | (is_signed ? MO_SIGN : 0));
8179 if (is_signed && !is_q) {
8180 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
8184 /* AdvSIMD copy
8185 * 31 30 29 28 21 20 16 15 14 11 10 9 5 4 0
8186 * +---+---+----+-----------------+------+---+------+---+------+------+
8187 * | 0 | Q | op | 0 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 | Rn | Rd |
8188 * +---+---+----+-----------------+------+---+------+---+------+------+
8190 static void disas_simd_copy(DisasContext *s, uint32_t insn)
8192 int rd = extract32(insn, 0, 5);
8193 int rn = extract32(insn, 5, 5);
8194 int imm4 = extract32(insn, 11, 4);
8195 int op = extract32(insn, 29, 1);
8196 int is_q = extract32(insn, 30, 1);
8197 int imm5 = extract32(insn, 16, 5);
8199 if (op) {
8200 if (is_q) {
8201 /* INS (element) */
8202 handle_simd_inse(s, rd, rn, imm4, imm5);
8203 } else {
8204 unallocated_encoding(s);
8206 } else {
8207 switch (imm4) {
8208 case 0:
8209 /* DUP (element - vector) */
8210 handle_simd_dupe(s, is_q, rd, rn, imm5);
8211 break;
8212 case 1:
8213 /* DUP (general) */
8214 handle_simd_dupg(s, is_q, rd, rn, imm5);
8215 break;
8216 case 3:
8217 if (is_q) {
8218 /* INS (general) */
8219 handle_simd_insg(s, rd, rn, imm5);
8220 } else {
8221 unallocated_encoding(s);
8223 break;
8224 case 5:
8225 case 7:
8226 /* UMOV/SMOV (is_q indicates 32/64; imm4 indicates signedness) */
8227 handle_simd_umov_smov(s, is_q, (imm4 == 5), rn, rd, imm5);
8228 break;
8229 default:
8230 unallocated_encoding(s);
8231 break;
8236 /* AdvSIMD modified immediate
8237 * 31 30 29 28 19 18 16 15 12 11 10 9 5 4 0
8238 * +---+---+----+---------------------+-----+-------+----+---+-------+------+
8239 * | 0 | Q | op | 0 1 1 1 1 0 0 0 0 0 | abc | cmode | o2 | 1 | defgh | Rd |
8240 * +---+---+----+---------------------+-----+-------+----+---+-------+------+
8242 * There are a number of operations that can be carried out here:
8243 * MOVI - move (shifted) imm into register
8244 * MVNI - move inverted (shifted) imm into register
8245 * ORR - bitwise OR of (shifted) imm with register
8246 * BIC - bitwise clear of (shifted) imm with register
8247 * With ARMv8.2 we also have:
8248 * FMOV half-precision
8250 static void disas_simd_mod_imm(DisasContext *s, uint32_t insn)
8252 int rd = extract32(insn, 0, 5);
8253 int cmode = extract32(insn, 12, 4);
8254 int o2 = extract32(insn, 11, 1);
8255 uint64_t abcdefgh = extract32(insn, 5, 5) | (extract32(insn, 16, 3) << 5);
8256 bool is_neg = extract32(insn, 29, 1);
8257 bool is_q = extract32(insn, 30, 1);
8258 uint64_t imm = 0;
8260 if (o2 != 0 || ((cmode == 0xf) && is_neg && !is_q)) {
8261 /* Check for FMOV (vector, immediate) - half-precision */
8262 if (!(dc_isar_feature(aa64_fp16, s) && o2 && cmode == 0xf)) {
8263 unallocated_encoding(s);
8264 return;
8268 if (!fp_access_check(s)) {
8269 return;
8272 if (cmode == 15 && o2 && !is_neg) {
8273 /* FMOV (vector, immediate) - half-precision */
8274 imm = vfp_expand_imm(MO_16, abcdefgh);
8275 /* now duplicate across the lanes */
8276 imm = dup_const(MO_16, imm);
8277 } else {
8278 imm = asimd_imm_const(abcdefgh, cmode, is_neg);
8281 if (!((cmode & 0x9) == 0x1 || (cmode & 0xd) == 0x9)) {
8282 /* MOVI or MVNI, with MVNI negation handled above. */
8283 tcg_gen_gvec_dup_imm(MO_64, vec_full_reg_offset(s, rd), is_q ? 16 : 8,
8284 vec_full_reg_size(s), imm);
8285 } else {
8286 /* ORR or BIC, with BIC negation to AND handled above. */
8287 if (is_neg) {
8288 gen_gvec_fn2i(s, is_q, rd, rd, imm, tcg_gen_gvec_andi, MO_64);
8289 } else {
8290 gen_gvec_fn2i(s, is_q, rd, rd, imm, tcg_gen_gvec_ori, MO_64);
8295 /* AdvSIMD scalar copy
8296 * 31 30 29 28 21 20 16 15 14 11 10 9 5 4 0
8297 * +-----+----+-----------------+------+---+------+---+------+------+
8298 * | 0 1 | op | 1 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 | Rn | Rd |
8299 * +-----+----+-----------------+------+---+------+---+------+------+
8301 static void disas_simd_scalar_copy(DisasContext *s, uint32_t insn)
8303 int rd = extract32(insn, 0, 5);
8304 int rn = extract32(insn, 5, 5);
8305 int imm4 = extract32(insn, 11, 4);
8306 int imm5 = extract32(insn, 16, 5);
8307 int op = extract32(insn, 29, 1);
8309 if (op != 0 || imm4 != 0) {
8310 unallocated_encoding(s);
8311 return;
8314 /* DUP (element, scalar) */
8315 handle_simd_dupes(s, rd, rn, imm5);
8318 /* AdvSIMD scalar pairwise
8319 * 31 30 29 28 24 23 22 21 17 16 12 11 10 9 5 4 0
8320 * +-----+---+-----------+------+-----------+--------+-----+------+------+
8321 * | 0 1 | U | 1 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 | Rn | Rd |
8322 * +-----+---+-----------+------+-----------+--------+-----+------+------+
8324 static void disas_simd_scalar_pairwise(DisasContext *s, uint32_t insn)
8326 int u = extract32(insn, 29, 1);
8327 int size = extract32(insn, 22, 2);
8328 int opcode = extract32(insn, 12, 5);
8329 int rn = extract32(insn, 5, 5);
8330 int rd = extract32(insn, 0, 5);
8331 TCGv_ptr fpst;
8333 /* For some ops (the FP ones), size[1] is part of the encoding.
8334 * For ADDP strictly it is not but size[1] is always 1 for valid
8335 * encodings.
8337 opcode |= (extract32(size, 1, 1) << 5);
8339 switch (opcode) {
8340 case 0x3b: /* ADDP */
8341 if (u || size != 3) {
8342 unallocated_encoding(s);
8343 return;
8345 if (!fp_access_check(s)) {
8346 return;
8349 fpst = NULL;
8350 break;
8351 case 0xc: /* FMAXNMP */
8352 case 0xd: /* FADDP */
8353 case 0xf: /* FMAXP */
8354 case 0x2c: /* FMINNMP */
8355 case 0x2f: /* FMINP */
8356 /* FP op, size[0] is 32 or 64 bit*/
8357 if (!u) {
8358 if (!dc_isar_feature(aa64_fp16, s)) {
8359 unallocated_encoding(s);
8360 return;
8361 } else {
8362 size = MO_16;
8364 } else {
8365 size = extract32(size, 0, 1) ? MO_64 : MO_32;
8368 if (!fp_access_check(s)) {
8369 return;
8372 fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
8373 break;
8374 default:
8375 unallocated_encoding(s);
8376 return;
8379 if (size == MO_64) {
8380 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8381 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
8382 TCGv_i64 tcg_res = tcg_temp_new_i64();
8384 read_vec_element(s, tcg_op1, rn, 0, MO_64);
8385 read_vec_element(s, tcg_op2, rn, 1, MO_64);
8387 switch (opcode) {
8388 case 0x3b: /* ADDP */
8389 tcg_gen_add_i64(tcg_res, tcg_op1, tcg_op2);
8390 break;
8391 case 0xc: /* FMAXNMP */
8392 gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
8393 break;
8394 case 0xd: /* FADDP */
8395 gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
8396 break;
8397 case 0xf: /* FMAXP */
8398 gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
8399 break;
8400 case 0x2c: /* FMINNMP */
8401 gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
8402 break;
8403 case 0x2f: /* FMINP */
8404 gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
8405 break;
8406 default:
8407 g_assert_not_reached();
8410 write_fp_dreg(s, rd, tcg_res);
8412 tcg_temp_free_i64(tcg_op1);
8413 tcg_temp_free_i64(tcg_op2);
8414 tcg_temp_free_i64(tcg_res);
8415 } else {
8416 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
8417 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
8418 TCGv_i32 tcg_res = tcg_temp_new_i32();
8420 read_vec_element_i32(s, tcg_op1, rn, 0, size);
8421 read_vec_element_i32(s, tcg_op2, rn, 1, size);
8423 if (size == MO_16) {
8424 switch (opcode) {
8425 case 0xc: /* FMAXNMP */
8426 gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
8427 break;
8428 case 0xd: /* FADDP */
8429 gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
8430 break;
8431 case 0xf: /* FMAXP */
8432 gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
8433 break;
8434 case 0x2c: /* FMINNMP */
8435 gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
8436 break;
8437 case 0x2f: /* FMINP */
8438 gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
8439 break;
8440 default:
8441 g_assert_not_reached();
8443 } else {
8444 switch (opcode) {
8445 case 0xc: /* FMAXNMP */
8446 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
8447 break;
8448 case 0xd: /* FADDP */
8449 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
8450 break;
8451 case 0xf: /* FMAXP */
8452 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
8453 break;
8454 case 0x2c: /* FMINNMP */
8455 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
8456 break;
8457 case 0x2f: /* FMINP */
8458 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
8459 break;
8460 default:
8461 g_assert_not_reached();
8465 write_fp_sreg(s, rd, tcg_res);
8467 tcg_temp_free_i32(tcg_op1);
8468 tcg_temp_free_i32(tcg_op2);
8469 tcg_temp_free_i32(tcg_res);
8472 if (fpst) {
8473 tcg_temp_free_ptr(fpst);
8478 * Common SSHR[RA]/USHR[RA] - Shift right (optional rounding/accumulate)
8480 * This code is handles the common shifting code and is used by both
8481 * the vector and scalar code.
8483 static void handle_shri_with_rndacc(TCGv_i64 tcg_res, TCGv_i64 tcg_src,
8484 TCGv_i64 tcg_rnd, bool accumulate,
8485 bool is_u, int size, int shift)
8487 bool extended_result = false;
8488 bool round = tcg_rnd != NULL;
8489 int ext_lshift = 0;
8490 TCGv_i64 tcg_src_hi;
8492 if (round && size == 3) {
8493 extended_result = true;
8494 ext_lshift = 64 - shift;
8495 tcg_src_hi = tcg_temp_new_i64();
8496 } else if (shift == 64) {
8497 if (!accumulate && is_u) {
8498 /* result is zero */
8499 tcg_gen_movi_i64(tcg_res, 0);
8500 return;
8504 /* Deal with the rounding step */
8505 if (round) {
8506 if (extended_result) {
8507 TCGv_i64 tcg_zero = tcg_constant_i64(0);
8508 if (!is_u) {
8509 /* take care of sign extending tcg_res */
8510 tcg_gen_sari_i64(tcg_src_hi, tcg_src, 63);
8511 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
8512 tcg_src, tcg_src_hi,
8513 tcg_rnd, tcg_zero);
8514 } else {
8515 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
8516 tcg_src, tcg_zero,
8517 tcg_rnd, tcg_zero);
8519 } else {
8520 tcg_gen_add_i64(tcg_src, tcg_src, tcg_rnd);
8524 /* Now do the shift right */
8525 if (round && extended_result) {
8526 /* extended case, >64 bit precision required */
8527 if (ext_lshift == 0) {
8528 /* special case, only high bits matter */
8529 tcg_gen_mov_i64(tcg_src, tcg_src_hi);
8530 } else {
8531 tcg_gen_shri_i64(tcg_src, tcg_src, shift);
8532 tcg_gen_shli_i64(tcg_src_hi, tcg_src_hi, ext_lshift);
8533 tcg_gen_or_i64(tcg_src, tcg_src, tcg_src_hi);
8535 } else {
8536 if (is_u) {
8537 if (shift == 64) {
8538 /* essentially shifting in 64 zeros */
8539 tcg_gen_movi_i64(tcg_src, 0);
8540 } else {
8541 tcg_gen_shri_i64(tcg_src, tcg_src, shift);
8543 } else {
8544 if (shift == 64) {
8545 /* effectively extending the sign-bit */
8546 tcg_gen_sari_i64(tcg_src, tcg_src, 63);
8547 } else {
8548 tcg_gen_sari_i64(tcg_src, tcg_src, shift);
8553 if (accumulate) {
8554 tcg_gen_add_i64(tcg_res, tcg_res, tcg_src);
8555 } else {
8556 tcg_gen_mov_i64(tcg_res, tcg_src);
8559 if (extended_result) {
8560 tcg_temp_free_i64(tcg_src_hi);
8564 /* SSHR[RA]/USHR[RA] - Scalar shift right (optional rounding/accumulate) */
8565 static void handle_scalar_simd_shri(DisasContext *s,
8566 bool is_u, int immh, int immb,
8567 int opcode, int rn, int rd)
8569 const int size = 3;
8570 int immhb = immh << 3 | immb;
8571 int shift = 2 * (8 << size) - immhb;
8572 bool accumulate = false;
8573 bool round = false;
8574 bool insert = false;
8575 TCGv_i64 tcg_rn;
8576 TCGv_i64 tcg_rd;
8577 TCGv_i64 tcg_round;
8579 if (!extract32(immh, 3, 1)) {
8580 unallocated_encoding(s);
8581 return;
8584 if (!fp_access_check(s)) {
8585 return;
8588 switch (opcode) {
8589 case 0x02: /* SSRA / USRA (accumulate) */
8590 accumulate = true;
8591 break;
8592 case 0x04: /* SRSHR / URSHR (rounding) */
8593 round = true;
8594 break;
8595 case 0x06: /* SRSRA / URSRA (accum + rounding) */
8596 accumulate = round = true;
8597 break;
8598 case 0x08: /* SRI */
8599 insert = true;
8600 break;
8603 if (round) {
8604 tcg_round = tcg_constant_i64(1ULL << (shift - 1));
8605 } else {
8606 tcg_round = NULL;
8609 tcg_rn = read_fp_dreg(s, rn);
8610 tcg_rd = (accumulate || insert) ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
8612 if (insert) {
8613 /* shift count same as element size is valid but does nothing;
8614 * special case to avoid potential shift by 64.
8616 int esize = 8 << size;
8617 if (shift != esize) {
8618 tcg_gen_shri_i64(tcg_rn, tcg_rn, shift);
8619 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, 0, esize - shift);
8621 } else {
8622 handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
8623 accumulate, is_u, size, shift);
8626 write_fp_dreg(s, rd, tcg_rd);
8628 tcg_temp_free_i64(tcg_rn);
8629 tcg_temp_free_i64(tcg_rd);
8632 /* SHL/SLI - Scalar shift left */
8633 static void handle_scalar_simd_shli(DisasContext *s, bool insert,
8634 int immh, int immb, int opcode,
8635 int rn, int rd)
8637 int size = 32 - clz32(immh) - 1;
8638 int immhb = immh << 3 | immb;
8639 int shift = immhb - (8 << size);
8640 TCGv_i64 tcg_rn;
8641 TCGv_i64 tcg_rd;
8643 if (!extract32(immh, 3, 1)) {
8644 unallocated_encoding(s);
8645 return;
8648 if (!fp_access_check(s)) {
8649 return;
8652 tcg_rn = read_fp_dreg(s, rn);
8653 tcg_rd = insert ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
8655 if (insert) {
8656 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, shift, 64 - shift);
8657 } else {
8658 tcg_gen_shli_i64(tcg_rd, tcg_rn, shift);
8661 write_fp_dreg(s, rd, tcg_rd);
8663 tcg_temp_free_i64(tcg_rn);
8664 tcg_temp_free_i64(tcg_rd);
8667 /* SQSHRN/SQSHRUN - Saturating (signed/unsigned) shift right with
8668 * (signed/unsigned) narrowing */
8669 static void handle_vec_simd_sqshrn(DisasContext *s, bool is_scalar, bool is_q,
8670 bool is_u_shift, bool is_u_narrow,
8671 int immh, int immb, int opcode,
8672 int rn, int rd)
8674 int immhb = immh << 3 | immb;
8675 int size = 32 - clz32(immh) - 1;
8676 int esize = 8 << size;
8677 int shift = (2 * esize) - immhb;
8678 int elements = is_scalar ? 1 : (64 / esize);
8679 bool round = extract32(opcode, 0, 1);
8680 MemOp ldop = (size + 1) | (is_u_shift ? 0 : MO_SIGN);
8681 TCGv_i64 tcg_rn, tcg_rd, tcg_round;
8682 TCGv_i32 tcg_rd_narrowed;
8683 TCGv_i64 tcg_final;
8685 static NeonGenNarrowEnvFn * const signed_narrow_fns[4][2] = {
8686 { gen_helper_neon_narrow_sat_s8,
8687 gen_helper_neon_unarrow_sat8 },
8688 { gen_helper_neon_narrow_sat_s16,
8689 gen_helper_neon_unarrow_sat16 },
8690 { gen_helper_neon_narrow_sat_s32,
8691 gen_helper_neon_unarrow_sat32 },
8692 { NULL, NULL },
8694 static NeonGenNarrowEnvFn * const unsigned_narrow_fns[4] = {
8695 gen_helper_neon_narrow_sat_u8,
8696 gen_helper_neon_narrow_sat_u16,
8697 gen_helper_neon_narrow_sat_u32,
8698 NULL
8700 NeonGenNarrowEnvFn *narrowfn;
8702 int i;
8704 assert(size < 4);
8706 if (extract32(immh, 3, 1)) {
8707 unallocated_encoding(s);
8708 return;
8711 if (!fp_access_check(s)) {
8712 return;
8715 if (is_u_shift) {
8716 narrowfn = unsigned_narrow_fns[size];
8717 } else {
8718 narrowfn = signed_narrow_fns[size][is_u_narrow ? 1 : 0];
8721 tcg_rn = tcg_temp_new_i64();
8722 tcg_rd = tcg_temp_new_i64();
8723 tcg_rd_narrowed = tcg_temp_new_i32();
8724 tcg_final = tcg_const_i64(0);
8726 if (round) {
8727 tcg_round = tcg_constant_i64(1ULL << (shift - 1));
8728 } else {
8729 tcg_round = NULL;
8732 for (i = 0; i < elements; i++) {
8733 read_vec_element(s, tcg_rn, rn, i, ldop);
8734 handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
8735 false, is_u_shift, size+1, shift);
8736 narrowfn(tcg_rd_narrowed, cpu_env, tcg_rd);
8737 tcg_gen_extu_i32_i64(tcg_rd, tcg_rd_narrowed);
8738 tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
8741 if (!is_q) {
8742 write_vec_element(s, tcg_final, rd, 0, MO_64);
8743 } else {
8744 write_vec_element(s, tcg_final, rd, 1, MO_64);
8747 tcg_temp_free_i64(tcg_rn);
8748 tcg_temp_free_i64(tcg_rd);
8749 tcg_temp_free_i32(tcg_rd_narrowed);
8750 tcg_temp_free_i64(tcg_final);
8752 clear_vec_high(s, is_q, rd);
8755 /* SQSHLU, UQSHL, SQSHL: saturating left shifts */
8756 static void handle_simd_qshl(DisasContext *s, bool scalar, bool is_q,
8757 bool src_unsigned, bool dst_unsigned,
8758 int immh, int immb, int rn, int rd)
8760 int immhb = immh << 3 | immb;
8761 int size = 32 - clz32(immh) - 1;
8762 int shift = immhb - (8 << size);
8763 int pass;
8765 assert(immh != 0);
8766 assert(!(scalar && is_q));
8768 if (!scalar) {
8769 if (!is_q && extract32(immh, 3, 1)) {
8770 unallocated_encoding(s);
8771 return;
8774 /* Since we use the variable-shift helpers we must
8775 * replicate the shift count into each element of
8776 * the tcg_shift value.
8778 switch (size) {
8779 case 0:
8780 shift |= shift << 8;
8781 /* fall through */
8782 case 1:
8783 shift |= shift << 16;
8784 break;
8785 case 2:
8786 case 3:
8787 break;
8788 default:
8789 g_assert_not_reached();
8793 if (!fp_access_check(s)) {
8794 return;
8797 if (size == 3) {
8798 TCGv_i64 tcg_shift = tcg_constant_i64(shift);
8799 static NeonGenTwo64OpEnvFn * const fns[2][2] = {
8800 { gen_helper_neon_qshl_s64, gen_helper_neon_qshlu_s64 },
8801 { NULL, gen_helper_neon_qshl_u64 },
8803 NeonGenTwo64OpEnvFn *genfn = fns[src_unsigned][dst_unsigned];
8804 int maxpass = is_q ? 2 : 1;
8806 for (pass = 0; pass < maxpass; pass++) {
8807 TCGv_i64 tcg_op = tcg_temp_new_i64();
8809 read_vec_element(s, tcg_op, rn, pass, MO_64);
8810 genfn(tcg_op, cpu_env, tcg_op, tcg_shift);
8811 write_vec_element(s, tcg_op, rd, pass, MO_64);
8813 tcg_temp_free_i64(tcg_op);
8815 clear_vec_high(s, is_q, rd);
8816 } else {
8817 TCGv_i32 tcg_shift = tcg_constant_i32(shift);
8818 static NeonGenTwoOpEnvFn * const fns[2][2][3] = {
8820 { gen_helper_neon_qshl_s8,
8821 gen_helper_neon_qshl_s16,
8822 gen_helper_neon_qshl_s32 },
8823 { gen_helper_neon_qshlu_s8,
8824 gen_helper_neon_qshlu_s16,
8825 gen_helper_neon_qshlu_s32 }
8826 }, {
8827 { NULL, NULL, NULL },
8828 { gen_helper_neon_qshl_u8,
8829 gen_helper_neon_qshl_u16,
8830 gen_helper_neon_qshl_u32 }
8833 NeonGenTwoOpEnvFn *genfn = fns[src_unsigned][dst_unsigned][size];
8834 MemOp memop = scalar ? size : MO_32;
8835 int maxpass = scalar ? 1 : is_q ? 4 : 2;
8837 for (pass = 0; pass < maxpass; pass++) {
8838 TCGv_i32 tcg_op = tcg_temp_new_i32();
8840 read_vec_element_i32(s, tcg_op, rn, pass, memop);
8841 genfn(tcg_op, cpu_env, tcg_op, tcg_shift);
8842 if (scalar) {
8843 switch (size) {
8844 case 0:
8845 tcg_gen_ext8u_i32(tcg_op, tcg_op);
8846 break;
8847 case 1:
8848 tcg_gen_ext16u_i32(tcg_op, tcg_op);
8849 break;
8850 case 2:
8851 break;
8852 default:
8853 g_assert_not_reached();
8855 write_fp_sreg(s, rd, tcg_op);
8856 } else {
8857 write_vec_element_i32(s, tcg_op, rd, pass, MO_32);
8860 tcg_temp_free_i32(tcg_op);
8863 if (!scalar) {
8864 clear_vec_high(s, is_q, rd);
8869 /* Common vector code for handling integer to FP conversion */
8870 static void handle_simd_intfp_conv(DisasContext *s, int rd, int rn,
8871 int elements, int is_signed,
8872 int fracbits, int size)
8874 TCGv_ptr tcg_fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
8875 TCGv_i32 tcg_shift = NULL;
8877 MemOp mop = size | (is_signed ? MO_SIGN : 0);
8878 int pass;
8880 if (fracbits || size == MO_64) {
8881 tcg_shift = tcg_constant_i32(fracbits);
8884 if (size == MO_64) {
8885 TCGv_i64 tcg_int64 = tcg_temp_new_i64();
8886 TCGv_i64 tcg_double = tcg_temp_new_i64();
8888 for (pass = 0; pass < elements; pass++) {
8889 read_vec_element(s, tcg_int64, rn, pass, mop);
8891 if (is_signed) {
8892 gen_helper_vfp_sqtod(tcg_double, tcg_int64,
8893 tcg_shift, tcg_fpst);
8894 } else {
8895 gen_helper_vfp_uqtod(tcg_double, tcg_int64,
8896 tcg_shift, tcg_fpst);
8898 if (elements == 1) {
8899 write_fp_dreg(s, rd, tcg_double);
8900 } else {
8901 write_vec_element(s, tcg_double, rd, pass, MO_64);
8905 tcg_temp_free_i64(tcg_int64);
8906 tcg_temp_free_i64(tcg_double);
8908 } else {
8909 TCGv_i32 tcg_int32 = tcg_temp_new_i32();
8910 TCGv_i32 tcg_float = tcg_temp_new_i32();
8912 for (pass = 0; pass < elements; pass++) {
8913 read_vec_element_i32(s, tcg_int32, rn, pass, mop);
8915 switch (size) {
8916 case MO_32:
8917 if (fracbits) {
8918 if (is_signed) {
8919 gen_helper_vfp_sltos(tcg_float, tcg_int32,
8920 tcg_shift, tcg_fpst);
8921 } else {
8922 gen_helper_vfp_ultos(tcg_float, tcg_int32,
8923 tcg_shift, tcg_fpst);
8925 } else {
8926 if (is_signed) {
8927 gen_helper_vfp_sitos(tcg_float, tcg_int32, tcg_fpst);
8928 } else {
8929 gen_helper_vfp_uitos(tcg_float, tcg_int32, tcg_fpst);
8932 break;
8933 case MO_16:
8934 if (fracbits) {
8935 if (is_signed) {
8936 gen_helper_vfp_sltoh(tcg_float, tcg_int32,
8937 tcg_shift, tcg_fpst);
8938 } else {
8939 gen_helper_vfp_ultoh(tcg_float, tcg_int32,
8940 tcg_shift, tcg_fpst);
8942 } else {
8943 if (is_signed) {
8944 gen_helper_vfp_sitoh(tcg_float, tcg_int32, tcg_fpst);
8945 } else {
8946 gen_helper_vfp_uitoh(tcg_float, tcg_int32, tcg_fpst);
8949 break;
8950 default:
8951 g_assert_not_reached();
8954 if (elements == 1) {
8955 write_fp_sreg(s, rd, tcg_float);
8956 } else {
8957 write_vec_element_i32(s, tcg_float, rd, pass, size);
8961 tcg_temp_free_i32(tcg_int32);
8962 tcg_temp_free_i32(tcg_float);
8965 tcg_temp_free_ptr(tcg_fpst);
8967 clear_vec_high(s, elements << size == 16, rd);
8970 /* UCVTF/SCVTF - Integer to FP conversion */
8971 static void handle_simd_shift_intfp_conv(DisasContext *s, bool is_scalar,
8972 bool is_q, bool is_u,
8973 int immh, int immb, int opcode,
8974 int rn, int rd)
8976 int size, elements, fracbits;
8977 int immhb = immh << 3 | immb;
8979 if (immh & 8) {
8980 size = MO_64;
8981 if (!is_scalar && !is_q) {
8982 unallocated_encoding(s);
8983 return;
8985 } else if (immh & 4) {
8986 size = MO_32;
8987 } else if (immh & 2) {
8988 size = MO_16;
8989 if (!dc_isar_feature(aa64_fp16, s)) {
8990 unallocated_encoding(s);
8991 return;
8993 } else {
8994 /* immh == 0 would be a failure of the decode logic */
8995 g_assert(immh == 1);
8996 unallocated_encoding(s);
8997 return;
9000 if (is_scalar) {
9001 elements = 1;
9002 } else {
9003 elements = (8 << is_q) >> size;
9005 fracbits = (16 << size) - immhb;
9007 if (!fp_access_check(s)) {
9008 return;
9011 handle_simd_intfp_conv(s, rd, rn, elements, !is_u, fracbits, size);
9014 /* FCVTZS, FVCVTZU - FP to fixedpoint conversion */
9015 static void handle_simd_shift_fpint_conv(DisasContext *s, bool is_scalar,
9016 bool is_q, bool is_u,
9017 int immh, int immb, int rn, int rd)
9019 int immhb = immh << 3 | immb;
9020 int pass, size, fracbits;
9021 TCGv_ptr tcg_fpstatus;
9022 TCGv_i32 tcg_rmode, tcg_shift;
9024 if (immh & 0x8) {
9025 size = MO_64;
9026 if (!is_scalar && !is_q) {
9027 unallocated_encoding(s);
9028 return;
9030 } else if (immh & 0x4) {
9031 size = MO_32;
9032 } else if (immh & 0x2) {
9033 size = MO_16;
9034 if (!dc_isar_feature(aa64_fp16, s)) {
9035 unallocated_encoding(s);
9036 return;
9038 } else {
9039 /* Should have split out AdvSIMD modified immediate earlier. */
9040 assert(immh == 1);
9041 unallocated_encoding(s);
9042 return;
9045 if (!fp_access_check(s)) {
9046 return;
9049 assert(!(is_scalar && is_q));
9051 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(FPROUNDING_ZERO));
9052 tcg_fpstatus = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
9053 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
9054 fracbits = (16 << size) - immhb;
9055 tcg_shift = tcg_constant_i32(fracbits);
9057 if (size == MO_64) {
9058 int maxpass = is_scalar ? 1 : 2;
9060 for (pass = 0; pass < maxpass; pass++) {
9061 TCGv_i64 tcg_op = tcg_temp_new_i64();
9063 read_vec_element(s, tcg_op, rn, pass, MO_64);
9064 if (is_u) {
9065 gen_helper_vfp_touqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
9066 } else {
9067 gen_helper_vfp_tosqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
9069 write_vec_element(s, tcg_op, rd, pass, MO_64);
9070 tcg_temp_free_i64(tcg_op);
9072 clear_vec_high(s, is_q, rd);
9073 } else {
9074 void (*fn)(TCGv_i32, TCGv_i32, TCGv_i32, TCGv_ptr);
9075 int maxpass = is_scalar ? 1 : ((8 << is_q) >> size);
9077 switch (size) {
9078 case MO_16:
9079 if (is_u) {
9080 fn = gen_helper_vfp_touhh;
9081 } else {
9082 fn = gen_helper_vfp_toshh;
9084 break;
9085 case MO_32:
9086 if (is_u) {
9087 fn = gen_helper_vfp_touls;
9088 } else {
9089 fn = gen_helper_vfp_tosls;
9091 break;
9092 default:
9093 g_assert_not_reached();
9096 for (pass = 0; pass < maxpass; pass++) {
9097 TCGv_i32 tcg_op = tcg_temp_new_i32();
9099 read_vec_element_i32(s, tcg_op, rn, pass, size);
9100 fn(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
9101 if (is_scalar) {
9102 write_fp_sreg(s, rd, tcg_op);
9103 } else {
9104 write_vec_element_i32(s, tcg_op, rd, pass, size);
9106 tcg_temp_free_i32(tcg_op);
9108 if (!is_scalar) {
9109 clear_vec_high(s, is_q, rd);
9113 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
9114 tcg_temp_free_ptr(tcg_fpstatus);
9115 tcg_temp_free_i32(tcg_rmode);
9118 /* AdvSIMD scalar shift by immediate
9119 * 31 30 29 28 23 22 19 18 16 15 11 10 9 5 4 0
9120 * +-----+---+-------------+------+------+--------+---+------+------+
9121 * | 0 1 | U | 1 1 1 1 1 0 | immh | immb | opcode | 1 | Rn | Rd |
9122 * +-----+---+-------------+------+------+--------+---+------+------+
9124 * This is the scalar version so it works on a fixed sized registers
9126 static void disas_simd_scalar_shift_imm(DisasContext *s, uint32_t insn)
9128 int rd = extract32(insn, 0, 5);
9129 int rn = extract32(insn, 5, 5);
9130 int opcode = extract32(insn, 11, 5);
9131 int immb = extract32(insn, 16, 3);
9132 int immh = extract32(insn, 19, 4);
9133 bool is_u = extract32(insn, 29, 1);
9135 if (immh == 0) {
9136 unallocated_encoding(s);
9137 return;
9140 switch (opcode) {
9141 case 0x08: /* SRI */
9142 if (!is_u) {
9143 unallocated_encoding(s);
9144 return;
9146 /* fall through */
9147 case 0x00: /* SSHR / USHR */
9148 case 0x02: /* SSRA / USRA */
9149 case 0x04: /* SRSHR / URSHR */
9150 case 0x06: /* SRSRA / URSRA */
9151 handle_scalar_simd_shri(s, is_u, immh, immb, opcode, rn, rd);
9152 break;
9153 case 0x0a: /* SHL / SLI */
9154 handle_scalar_simd_shli(s, is_u, immh, immb, opcode, rn, rd);
9155 break;
9156 case 0x1c: /* SCVTF, UCVTF */
9157 handle_simd_shift_intfp_conv(s, true, false, is_u, immh, immb,
9158 opcode, rn, rd);
9159 break;
9160 case 0x10: /* SQSHRUN, SQSHRUN2 */
9161 case 0x11: /* SQRSHRUN, SQRSHRUN2 */
9162 if (!is_u) {
9163 unallocated_encoding(s);
9164 return;
9166 handle_vec_simd_sqshrn(s, true, false, false, true,
9167 immh, immb, opcode, rn, rd);
9168 break;
9169 case 0x12: /* SQSHRN, SQSHRN2, UQSHRN */
9170 case 0x13: /* SQRSHRN, SQRSHRN2, UQRSHRN, UQRSHRN2 */
9171 handle_vec_simd_sqshrn(s, true, false, is_u, is_u,
9172 immh, immb, opcode, rn, rd);
9173 break;
9174 case 0xc: /* SQSHLU */
9175 if (!is_u) {
9176 unallocated_encoding(s);
9177 return;
9179 handle_simd_qshl(s, true, false, false, true, immh, immb, rn, rd);
9180 break;
9181 case 0xe: /* SQSHL, UQSHL */
9182 handle_simd_qshl(s, true, false, is_u, is_u, immh, immb, rn, rd);
9183 break;
9184 case 0x1f: /* FCVTZS, FCVTZU */
9185 handle_simd_shift_fpint_conv(s, true, false, is_u, immh, immb, rn, rd);
9186 break;
9187 default:
9188 unallocated_encoding(s);
9189 break;
9193 /* AdvSIMD scalar three different
9194 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 0
9195 * +-----+---+-----------+------+---+------+--------+-----+------+------+
9196 * | 0 1 | U | 1 1 1 1 0 | size | 1 | Rm | opcode | 0 0 | Rn | Rd |
9197 * +-----+---+-----------+------+---+------+--------+-----+------+------+
9199 static void disas_simd_scalar_three_reg_diff(DisasContext *s, uint32_t insn)
9201 bool is_u = extract32(insn, 29, 1);
9202 int size = extract32(insn, 22, 2);
9203 int opcode = extract32(insn, 12, 4);
9204 int rm = extract32(insn, 16, 5);
9205 int rn = extract32(insn, 5, 5);
9206 int rd = extract32(insn, 0, 5);
9208 if (is_u) {
9209 unallocated_encoding(s);
9210 return;
9213 switch (opcode) {
9214 case 0x9: /* SQDMLAL, SQDMLAL2 */
9215 case 0xb: /* SQDMLSL, SQDMLSL2 */
9216 case 0xd: /* SQDMULL, SQDMULL2 */
9217 if (size == 0 || size == 3) {
9218 unallocated_encoding(s);
9219 return;
9221 break;
9222 default:
9223 unallocated_encoding(s);
9224 return;
9227 if (!fp_access_check(s)) {
9228 return;
9231 if (size == 2) {
9232 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
9233 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
9234 TCGv_i64 tcg_res = tcg_temp_new_i64();
9236 read_vec_element(s, tcg_op1, rn, 0, MO_32 | MO_SIGN);
9237 read_vec_element(s, tcg_op2, rm, 0, MO_32 | MO_SIGN);
9239 tcg_gen_mul_i64(tcg_res, tcg_op1, tcg_op2);
9240 gen_helper_neon_addl_saturate_s64(tcg_res, cpu_env, tcg_res, tcg_res);
9242 switch (opcode) {
9243 case 0xd: /* SQDMULL, SQDMULL2 */
9244 break;
9245 case 0xb: /* SQDMLSL, SQDMLSL2 */
9246 tcg_gen_neg_i64(tcg_res, tcg_res);
9247 /* fall through */
9248 case 0x9: /* SQDMLAL, SQDMLAL2 */
9249 read_vec_element(s, tcg_op1, rd, 0, MO_64);
9250 gen_helper_neon_addl_saturate_s64(tcg_res, cpu_env,
9251 tcg_res, tcg_op1);
9252 break;
9253 default:
9254 g_assert_not_reached();
9257 write_fp_dreg(s, rd, tcg_res);
9259 tcg_temp_free_i64(tcg_op1);
9260 tcg_temp_free_i64(tcg_op2);
9261 tcg_temp_free_i64(tcg_res);
9262 } else {
9263 TCGv_i32 tcg_op1 = read_fp_hreg(s, rn);
9264 TCGv_i32 tcg_op2 = read_fp_hreg(s, rm);
9265 TCGv_i64 tcg_res = tcg_temp_new_i64();
9267 gen_helper_neon_mull_s16(tcg_res, tcg_op1, tcg_op2);
9268 gen_helper_neon_addl_saturate_s32(tcg_res, cpu_env, tcg_res, tcg_res);
9270 switch (opcode) {
9271 case 0xd: /* SQDMULL, SQDMULL2 */
9272 break;
9273 case 0xb: /* SQDMLSL, SQDMLSL2 */
9274 gen_helper_neon_negl_u32(tcg_res, tcg_res);
9275 /* fall through */
9276 case 0x9: /* SQDMLAL, SQDMLAL2 */
9278 TCGv_i64 tcg_op3 = tcg_temp_new_i64();
9279 read_vec_element(s, tcg_op3, rd, 0, MO_32);
9280 gen_helper_neon_addl_saturate_s32(tcg_res, cpu_env,
9281 tcg_res, tcg_op3);
9282 tcg_temp_free_i64(tcg_op3);
9283 break;
9285 default:
9286 g_assert_not_reached();
9289 tcg_gen_ext32u_i64(tcg_res, tcg_res);
9290 write_fp_dreg(s, rd, tcg_res);
9292 tcg_temp_free_i32(tcg_op1);
9293 tcg_temp_free_i32(tcg_op2);
9294 tcg_temp_free_i64(tcg_res);
9298 static void handle_3same_64(DisasContext *s, int opcode, bool u,
9299 TCGv_i64 tcg_rd, TCGv_i64 tcg_rn, TCGv_i64 tcg_rm)
9301 /* Handle 64x64->64 opcodes which are shared between the scalar
9302 * and vector 3-same groups. We cover every opcode where size == 3
9303 * is valid in either the three-reg-same (integer, not pairwise)
9304 * or scalar-three-reg-same groups.
9306 TCGCond cond;
9308 switch (opcode) {
9309 case 0x1: /* SQADD */
9310 if (u) {
9311 gen_helper_neon_qadd_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9312 } else {
9313 gen_helper_neon_qadd_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9315 break;
9316 case 0x5: /* SQSUB */
9317 if (u) {
9318 gen_helper_neon_qsub_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9319 } else {
9320 gen_helper_neon_qsub_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9322 break;
9323 case 0x6: /* CMGT, CMHI */
9324 /* 64 bit integer comparison, result = test ? (2^64 - 1) : 0.
9325 * We implement this using setcond (test) and then negating.
9327 cond = u ? TCG_COND_GTU : TCG_COND_GT;
9328 do_cmop:
9329 tcg_gen_setcond_i64(cond, tcg_rd, tcg_rn, tcg_rm);
9330 tcg_gen_neg_i64(tcg_rd, tcg_rd);
9331 break;
9332 case 0x7: /* CMGE, CMHS */
9333 cond = u ? TCG_COND_GEU : TCG_COND_GE;
9334 goto do_cmop;
9335 case 0x11: /* CMTST, CMEQ */
9336 if (u) {
9337 cond = TCG_COND_EQ;
9338 goto do_cmop;
9340 gen_cmtst_i64(tcg_rd, tcg_rn, tcg_rm);
9341 break;
9342 case 0x8: /* SSHL, USHL */
9343 if (u) {
9344 gen_ushl_i64(tcg_rd, tcg_rn, tcg_rm);
9345 } else {
9346 gen_sshl_i64(tcg_rd, tcg_rn, tcg_rm);
9348 break;
9349 case 0x9: /* SQSHL, UQSHL */
9350 if (u) {
9351 gen_helper_neon_qshl_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9352 } else {
9353 gen_helper_neon_qshl_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9355 break;
9356 case 0xa: /* SRSHL, URSHL */
9357 if (u) {
9358 gen_helper_neon_rshl_u64(tcg_rd, tcg_rn, tcg_rm);
9359 } else {
9360 gen_helper_neon_rshl_s64(tcg_rd, tcg_rn, tcg_rm);
9362 break;
9363 case 0xb: /* SQRSHL, UQRSHL */
9364 if (u) {
9365 gen_helper_neon_qrshl_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9366 } else {
9367 gen_helper_neon_qrshl_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9369 break;
9370 case 0x10: /* ADD, SUB */
9371 if (u) {
9372 tcg_gen_sub_i64(tcg_rd, tcg_rn, tcg_rm);
9373 } else {
9374 tcg_gen_add_i64(tcg_rd, tcg_rn, tcg_rm);
9376 break;
9377 default:
9378 g_assert_not_reached();
9382 /* Handle the 3-same-operands float operations; shared by the scalar
9383 * and vector encodings. The caller must filter out any encodings
9384 * not allocated for the encoding it is dealing with.
9386 static void handle_3same_float(DisasContext *s, int size, int elements,
9387 int fpopcode, int rd, int rn, int rm)
9389 int pass;
9390 TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
9392 for (pass = 0; pass < elements; pass++) {
9393 if (size) {
9394 /* Double */
9395 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
9396 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
9397 TCGv_i64 tcg_res = tcg_temp_new_i64();
9399 read_vec_element(s, tcg_op1, rn, pass, MO_64);
9400 read_vec_element(s, tcg_op2, rm, pass, MO_64);
9402 switch (fpopcode) {
9403 case 0x39: /* FMLS */
9404 /* As usual for ARM, separate negation for fused multiply-add */
9405 gen_helper_vfp_negd(tcg_op1, tcg_op1);
9406 /* fall through */
9407 case 0x19: /* FMLA */
9408 read_vec_element(s, tcg_res, rd, pass, MO_64);
9409 gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2,
9410 tcg_res, fpst);
9411 break;
9412 case 0x18: /* FMAXNM */
9413 gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
9414 break;
9415 case 0x1a: /* FADD */
9416 gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
9417 break;
9418 case 0x1b: /* FMULX */
9419 gen_helper_vfp_mulxd(tcg_res, tcg_op1, tcg_op2, fpst);
9420 break;
9421 case 0x1c: /* FCMEQ */
9422 gen_helper_neon_ceq_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9423 break;
9424 case 0x1e: /* FMAX */
9425 gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
9426 break;
9427 case 0x1f: /* FRECPS */
9428 gen_helper_recpsf_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9429 break;
9430 case 0x38: /* FMINNM */
9431 gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
9432 break;
9433 case 0x3a: /* FSUB */
9434 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
9435 break;
9436 case 0x3e: /* FMIN */
9437 gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
9438 break;
9439 case 0x3f: /* FRSQRTS */
9440 gen_helper_rsqrtsf_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9441 break;
9442 case 0x5b: /* FMUL */
9443 gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
9444 break;
9445 case 0x5c: /* FCMGE */
9446 gen_helper_neon_cge_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9447 break;
9448 case 0x5d: /* FACGE */
9449 gen_helper_neon_acge_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9450 break;
9451 case 0x5f: /* FDIV */
9452 gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
9453 break;
9454 case 0x7a: /* FABD */
9455 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
9456 gen_helper_vfp_absd(tcg_res, tcg_res);
9457 break;
9458 case 0x7c: /* FCMGT */
9459 gen_helper_neon_cgt_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9460 break;
9461 case 0x7d: /* FACGT */
9462 gen_helper_neon_acgt_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9463 break;
9464 default:
9465 g_assert_not_reached();
9468 write_vec_element(s, tcg_res, rd, pass, MO_64);
9470 tcg_temp_free_i64(tcg_res);
9471 tcg_temp_free_i64(tcg_op1);
9472 tcg_temp_free_i64(tcg_op2);
9473 } else {
9474 /* Single */
9475 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
9476 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
9477 TCGv_i32 tcg_res = tcg_temp_new_i32();
9479 read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
9480 read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
9482 switch (fpopcode) {
9483 case 0x39: /* FMLS */
9484 /* As usual for ARM, separate negation for fused multiply-add */
9485 gen_helper_vfp_negs(tcg_op1, tcg_op1);
9486 /* fall through */
9487 case 0x19: /* FMLA */
9488 read_vec_element_i32(s, tcg_res, rd, pass, MO_32);
9489 gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2,
9490 tcg_res, fpst);
9491 break;
9492 case 0x1a: /* FADD */
9493 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
9494 break;
9495 case 0x1b: /* FMULX */
9496 gen_helper_vfp_mulxs(tcg_res, tcg_op1, tcg_op2, fpst);
9497 break;
9498 case 0x1c: /* FCMEQ */
9499 gen_helper_neon_ceq_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9500 break;
9501 case 0x1e: /* FMAX */
9502 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
9503 break;
9504 case 0x1f: /* FRECPS */
9505 gen_helper_recpsf_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9506 break;
9507 case 0x18: /* FMAXNM */
9508 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
9509 break;
9510 case 0x38: /* FMINNM */
9511 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
9512 break;
9513 case 0x3a: /* FSUB */
9514 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
9515 break;
9516 case 0x3e: /* FMIN */
9517 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
9518 break;
9519 case 0x3f: /* FRSQRTS */
9520 gen_helper_rsqrtsf_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9521 break;
9522 case 0x5b: /* FMUL */
9523 gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
9524 break;
9525 case 0x5c: /* FCMGE */
9526 gen_helper_neon_cge_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9527 break;
9528 case 0x5d: /* FACGE */
9529 gen_helper_neon_acge_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9530 break;
9531 case 0x5f: /* FDIV */
9532 gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
9533 break;
9534 case 0x7a: /* FABD */
9535 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
9536 gen_helper_vfp_abss(tcg_res, tcg_res);
9537 break;
9538 case 0x7c: /* FCMGT */
9539 gen_helper_neon_cgt_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9540 break;
9541 case 0x7d: /* FACGT */
9542 gen_helper_neon_acgt_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9543 break;
9544 default:
9545 g_assert_not_reached();
9548 if (elements == 1) {
9549 /* scalar single so clear high part */
9550 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
9552 tcg_gen_extu_i32_i64(tcg_tmp, tcg_res);
9553 write_vec_element(s, tcg_tmp, rd, pass, MO_64);
9554 tcg_temp_free_i64(tcg_tmp);
9555 } else {
9556 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
9559 tcg_temp_free_i32(tcg_res);
9560 tcg_temp_free_i32(tcg_op1);
9561 tcg_temp_free_i32(tcg_op2);
9565 tcg_temp_free_ptr(fpst);
9567 clear_vec_high(s, elements * (size ? 8 : 4) > 8, rd);
9570 /* AdvSIMD scalar three same
9571 * 31 30 29 28 24 23 22 21 20 16 15 11 10 9 5 4 0
9572 * +-----+---+-----------+------+---+------+--------+---+------+------+
9573 * | 0 1 | U | 1 1 1 1 0 | size | 1 | Rm | opcode | 1 | Rn | Rd |
9574 * +-----+---+-----------+------+---+------+--------+---+------+------+
9576 static void disas_simd_scalar_three_reg_same(DisasContext *s, uint32_t insn)
9578 int rd = extract32(insn, 0, 5);
9579 int rn = extract32(insn, 5, 5);
9580 int opcode = extract32(insn, 11, 5);
9581 int rm = extract32(insn, 16, 5);
9582 int size = extract32(insn, 22, 2);
9583 bool u = extract32(insn, 29, 1);
9584 TCGv_i64 tcg_rd;
9586 if (opcode >= 0x18) {
9587 /* Floating point: U, size[1] and opcode indicate operation */
9588 int fpopcode = opcode | (extract32(size, 1, 1) << 5) | (u << 6);
9589 switch (fpopcode) {
9590 case 0x1b: /* FMULX */
9591 case 0x1f: /* FRECPS */
9592 case 0x3f: /* FRSQRTS */
9593 case 0x5d: /* FACGE */
9594 case 0x7d: /* FACGT */
9595 case 0x1c: /* FCMEQ */
9596 case 0x5c: /* FCMGE */
9597 case 0x7c: /* FCMGT */
9598 case 0x7a: /* FABD */
9599 break;
9600 default:
9601 unallocated_encoding(s);
9602 return;
9605 if (!fp_access_check(s)) {
9606 return;
9609 handle_3same_float(s, extract32(size, 0, 1), 1, fpopcode, rd, rn, rm);
9610 return;
9613 switch (opcode) {
9614 case 0x1: /* SQADD, UQADD */
9615 case 0x5: /* SQSUB, UQSUB */
9616 case 0x9: /* SQSHL, UQSHL */
9617 case 0xb: /* SQRSHL, UQRSHL */
9618 break;
9619 case 0x8: /* SSHL, USHL */
9620 case 0xa: /* SRSHL, URSHL */
9621 case 0x6: /* CMGT, CMHI */
9622 case 0x7: /* CMGE, CMHS */
9623 case 0x11: /* CMTST, CMEQ */
9624 case 0x10: /* ADD, SUB (vector) */
9625 if (size != 3) {
9626 unallocated_encoding(s);
9627 return;
9629 break;
9630 case 0x16: /* SQDMULH, SQRDMULH (vector) */
9631 if (size != 1 && size != 2) {
9632 unallocated_encoding(s);
9633 return;
9635 break;
9636 default:
9637 unallocated_encoding(s);
9638 return;
9641 if (!fp_access_check(s)) {
9642 return;
9645 tcg_rd = tcg_temp_new_i64();
9647 if (size == 3) {
9648 TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
9649 TCGv_i64 tcg_rm = read_fp_dreg(s, rm);
9651 handle_3same_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rm);
9652 tcg_temp_free_i64(tcg_rn);
9653 tcg_temp_free_i64(tcg_rm);
9654 } else {
9655 /* Do a single operation on the lowest element in the vector.
9656 * We use the standard Neon helpers and rely on 0 OP 0 == 0 with
9657 * no side effects for all these operations.
9658 * OPTME: special-purpose helpers would avoid doing some
9659 * unnecessary work in the helper for the 8 and 16 bit cases.
9661 NeonGenTwoOpEnvFn *genenvfn;
9662 TCGv_i32 tcg_rn = tcg_temp_new_i32();
9663 TCGv_i32 tcg_rm = tcg_temp_new_i32();
9664 TCGv_i32 tcg_rd32 = tcg_temp_new_i32();
9666 read_vec_element_i32(s, tcg_rn, rn, 0, size);
9667 read_vec_element_i32(s, tcg_rm, rm, 0, size);
9669 switch (opcode) {
9670 case 0x1: /* SQADD, UQADD */
9672 static NeonGenTwoOpEnvFn * const fns[3][2] = {
9673 { gen_helper_neon_qadd_s8, gen_helper_neon_qadd_u8 },
9674 { gen_helper_neon_qadd_s16, gen_helper_neon_qadd_u16 },
9675 { gen_helper_neon_qadd_s32, gen_helper_neon_qadd_u32 },
9677 genenvfn = fns[size][u];
9678 break;
9680 case 0x5: /* SQSUB, UQSUB */
9682 static NeonGenTwoOpEnvFn * const fns[3][2] = {
9683 { gen_helper_neon_qsub_s8, gen_helper_neon_qsub_u8 },
9684 { gen_helper_neon_qsub_s16, gen_helper_neon_qsub_u16 },
9685 { gen_helper_neon_qsub_s32, gen_helper_neon_qsub_u32 },
9687 genenvfn = fns[size][u];
9688 break;
9690 case 0x9: /* SQSHL, UQSHL */
9692 static NeonGenTwoOpEnvFn * const fns[3][2] = {
9693 { gen_helper_neon_qshl_s8, gen_helper_neon_qshl_u8 },
9694 { gen_helper_neon_qshl_s16, gen_helper_neon_qshl_u16 },
9695 { gen_helper_neon_qshl_s32, gen_helper_neon_qshl_u32 },
9697 genenvfn = fns[size][u];
9698 break;
9700 case 0xb: /* SQRSHL, UQRSHL */
9702 static NeonGenTwoOpEnvFn * const fns[3][2] = {
9703 { gen_helper_neon_qrshl_s8, gen_helper_neon_qrshl_u8 },
9704 { gen_helper_neon_qrshl_s16, gen_helper_neon_qrshl_u16 },
9705 { gen_helper_neon_qrshl_s32, gen_helper_neon_qrshl_u32 },
9707 genenvfn = fns[size][u];
9708 break;
9710 case 0x16: /* SQDMULH, SQRDMULH */
9712 static NeonGenTwoOpEnvFn * const fns[2][2] = {
9713 { gen_helper_neon_qdmulh_s16, gen_helper_neon_qrdmulh_s16 },
9714 { gen_helper_neon_qdmulh_s32, gen_helper_neon_qrdmulh_s32 },
9716 assert(size == 1 || size == 2);
9717 genenvfn = fns[size - 1][u];
9718 break;
9720 default:
9721 g_assert_not_reached();
9724 genenvfn(tcg_rd32, cpu_env, tcg_rn, tcg_rm);
9725 tcg_gen_extu_i32_i64(tcg_rd, tcg_rd32);
9726 tcg_temp_free_i32(tcg_rd32);
9727 tcg_temp_free_i32(tcg_rn);
9728 tcg_temp_free_i32(tcg_rm);
9731 write_fp_dreg(s, rd, tcg_rd);
9733 tcg_temp_free_i64(tcg_rd);
9736 /* AdvSIMD scalar three same FP16
9737 * 31 30 29 28 24 23 22 21 20 16 15 14 13 11 10 9 5 4 0
9738 * +-----+---+-----------+---+-----+------+-----+--------+---+----+----+
9739 * | 0 1 | U | 1 1 1 1 0 | a | 1 0 | Rm | 0 0 | opcode | 1 | Rn | Rd |
9740 * +-----+---+-----------+---+-----+------+-----+--------+---+----+----+
9741 * v: 0101 1110 0100 0000 0000 0100 0000 0000 => 5e400400
9742 * m: 1101 1111 0110 0000 1100 0100 0000 0000 => df60c400
9744 static void disas_simd_scalar_three_reg_same_fp16(DisasContext *s,
9745 uint32_t insn)
9747 int rd = extract32(insn, 0, 5);
9748 int rn = extract32(insn, 5, 5);
9749 int opcode = extract32(insn, 11, 3);
9750 int rm = extract32(insn, 16, 5);
9751 bool u = extract32(insn, 29, 1);
9752 bool a = extract32(insn, 23, 1);
9753 int fpopcode = opcode | (a << 3) | (u << 4);
9754 TCGv_ptr fpst;
9755 TCGv_i32 tcg_op1;
9756 TCGv_i32 tcg_op2;
9757 TCGv_i32 tcg_res;
9759 switch (fpopcode) {
9760 case 0x03: /* FMULX */
9761 case 0x04: /* FCMEQ (reg) */
9762 case 0x07: /* FRECPS */
9763 case 0x0f: /* FRSQRTS */
9764 case 0x14: /* FCMGE (reg) */
9765 case 0x15: /* FACGE */
9766 case 0x1a: /* FABD */
9767 case 0x1c: /* FCMGT (reg) */
9768 case 0x1d: /* FACGT */
9769 break;
9770 default:
9771 unallocated_encoding(s);
9772 return;
9775 if (!dc_isar_feature(aa64_fp16, s)) {
9776 unallocated_encoding(s);
9779 if (!fp_access_check(s)) {
9780 return;
9783 fpst = fpstatus_ptr(FPST_FPCR_F16);
9785 tcg_op1 = read_fp_hreg(s, rn);
9786 tcg_op2 = read_fp_hreg(s, rm);
9787 tcg_res = tcg_temp_new_i32();
9789 switch (fpopcode) {
9790 case 0x03: /* FMULX */
9791 gen_helper_advsimd_mulxh(tcg_res, tcg_op1, tcg_op2, fpst);
9792 break;
9793 case 0x04: /* FCMEQ (reg) */
9794 gen_helper_advsimd_ceq_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9795 break;
9796 case 0x07: /* FRECPS */
9797 gen_helper_recpsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9798 break;
9799 case 0x0f: /* FRSQRTS */
9800 gen_helper_rsqrtsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9801 break;
9802 case 0x14: /* FCMGE (reg) */
9803 gen_helper_advsimd_cge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9804 break;
9805 case 0x15: /* FACGE */
9806 gen_helper_advsimd_acge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9807 break;
9808 case 0x1a: /* FABD */
9809 gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
9810 tcg_gen_andi_i32(tcg_res, tcg_res, 0x7fff);
9811 break;
9812 case 0x1c: /* FCMGT (reg) */
9813 gen_helper_advsimd_cgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9814 break;
9815 case 0x1d: /* FACGT */
9816 gen_helper_advsimd_acgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9817 break;
9818 default:
9819 g_assert_not_reached();
9822 write_fp_sreg(s, rd, tcg_res);
9825 tcg_temp_free_i32(tcg_res);
9826 tcg_temp_free_i32(tcg_op1);
9827 tcg_temp_free_i32(tcg_op2);
9828 tcg_temp_free_ptr(fpst);
9831 /* AdvSIMD scalar three same extra
9832 * 31 30 29 28 24 23 22 21 20 16 15 14 11 10 9 5 4 0
9833 * +-----+---+-----------+------+---+------+---+--------+---+----+----+
9834 * | 0 1 | U | 1 1 1 1 0 | size | 0 | Rm | 1 | opcode | 1 | Rn | Rd |
9835 * +-----+---+-----------+------+---+------+---+--------+---+----+----+
9837 static void disas_simd_scalar_three_reg_same_extra(DisasContext *s,
9838 uint32_t insn)
9840 int rd = extract32(insn, 0, 5);
9841 int rn = extract32(insn, 5, 5);
9842 int opcode = extract32(insn, 11, 4);
9843 int rm = extract32(insn, 16, 5);
9844 int size = extract32(insn, 22, 2);
9845 bool u = extract32(insn, 29, 1);
9846 TCGv_i32 ele1, ele2, ele3;
9847 TCGv_i64 res;
9848 bool feature;
9850 switch (u * 16 + opcode) {
9851 case 0x10: /* SQRDMLAH (vector) */
9852 case 0x11: /* SQRDMLSH (vector) */
9853 if (size != 1 && size != 2) {
9854 unallocated_encoding(s);
9855 return;
9857 feature = dc_isar_feature(aa64_rdm, s);
9858 break;
9859 default:
9860 unallocated_encoding(s);
9861 return;
9863 if (!feature) {
9864 unallocated_encoding(s);
9865 return;
9867 if (!fp_access_check(s)) {
9868 return;
9871 /* Do a single operation on the lowest element in the vector.
9872 * We use the standard Neon helpers and rely on 0 OP 0 == 0
9873 * with no side effects for all these operations.
9874 * OPTME: special-purpose helpers would avoid doing some
9875 * unnecessary work in the helper for the 16 bit cases.
9877 ele1 = tcg_temp_new_i32();
9878 ele2 = tcg_temp_new_i32();
9879 ele3 = tcg_temp_new_i32();
9881 read_vec_element_i32(s, ele1, rn, 0, size);
9882 read_vec_element_i32(s, ele2, rm, 0, size);
9883 read_vec_element_i32(s, ele3, rd, 0, size);
9885 switch (opcode) {
9886 case 0x0: /* SQRDMLAH */
9887 if (size == 1) {
9888 gen_helper_neon_qrdmlah_s16(ele3, cpu_env, ele1, ele2, ele3);
9889 } else {
9890 gen_helper_neon_qrdmlah_s32(ele3, cpu_env, ele1, ele2, ele3);
9892 break;
9893 case 0x1: /* SQRDMLSH */
9894 if (size == 1) {
9895 gen_helper_neon_qrdmlsh_s16(ele3, cpu_env, ele1, ele2, ele3);
9896 } else {
9897 gen_helper_neon_qrdmlsh_s32(ele3, cpu_env, ele1, ele2, ele3);
9899 break;
9900 default:
9901 g_assert_not_reached();
9903 tcg_temp_free_i32(ele1);
9904 tcg_temp_free_i32(ele2);
9906 res = tcg_temp_new_i64();
9907 tcg_gen_extu_i32_i64(res, ele3);
9908 tcg_temp_free_i32(ele3);
9910 write_fp_dreg(s, rd, res);
9911 tcg_temp_free_i64(res);
9914 static void handle_2misc_64(DisasContext *s, int opcode, bool u,
9915 TCGv_i64 tcg_rd, TCGv_i64 tcg_rn,
9916 TCGv_i32 tcg_rmode, TCGv_ptr tcg_fpstatus)
9918 /* Handle 64->64 opcodes which are shared between the scalar and
9919 * vector 2-reg-misc groups. We cover every integer opcode where size == 3
9920 * is valid in either group and also the double-precision fp ops.
9921 * The caller only need provide tcg_rmode and tcg_fpstatus if the op
9922 * requires them.
9924 TCGCond cond;
9926 switch (opcode) {
9927 case 0x4: /* CLS, CLZ */
9928 if (u) {
9929 tcg_gen_clzi_i64(tcg_rd, tcg_rn, 64);
9930 } else {
9931 tcg_gen_clrsb_i64(tcg_rd, tcg_rn);
9933 break;
9934 case 0x5: /* NOT */
9935 /* This opcode is shared with CNT and RBIT but we have earlier
9936 * enforced that size == 3 if and only if this is the NOT insn.
9938 tcg_gen_not_i64(tcg_rd, tcg_rn);
9939 break;
9940 case 0x7: /* SQABS, SQNEG */
9941 if (u) {
9942 gen_helper_neon_qneg_s64(tcg_rd, cpu_env, tcg_rn);
9943 } else {
9944 gen_helper_neon_qabs_s64(tcg_rd, cpu_env, tcg_rn);
9946 break;
9947 case 0xa: /* CMLT */
9948 /* 64 bit integer comparison against zero, result is
9949 * test ? (2^64 - 1) : 0. We implement via setcond(!test) and
9950 * subtracting 1.
9952 cond = TCG_COND_LT;
9953 do_cmop:
9954 tcg_gen_setcondi_i64(cond, tcg_rd, tcg_rn, 0);
9955 tcg_gen_neg_i64(tcg_rd, tcg_rd);
9956 break;
9957 case 0x8: /* CMGT, CMGE */
9958 cond = u ? TCG_COND_GE : TCG_COND_GT;
9959 goto do_cmop;
9960 case 0x9: /* CMEQ, CMLE */
9961 cond = u ? TCG_COND_LE : TCG_COND_EQ;
9962 goto do_cmop;
9963 case 0xb: /* ABS, NEG */
9964 if (u) {
9965 tcg_gen_neg_i64(tcg_rd, tcg_rn);
9966 } else {
9967 tcg_gen_abs_i64(tcg_rd, tcg_rn);
9969 break;
9970 case 0x2f: /* FABS */
9971 gen_helper_vfp_absd(tcg_rd, tcg_rn);
9972 break;
9973 case 0x6f: /* FNEG */
9974 gen_helper_vfp_negd(tcg_rd, tcg_rn);
9975 break;
9976 case 0x7f: /* FSQRT */
9977 gen_helper_vfp_sqrtd(tcg_rd, tcg_rn, cpu_env);
9978 break;
9979 case 0x1a: /* FCVTNS */
9980 case 0x1b: /* FCVTMS */
9981 case 0x1c: /* FCVTAS */
9982 case 0x3a: /* FCVTPS */
9983 case 0x3b: /* FCVTZS */
9984 gen_helper_vfp_tosqd(tcg_rd, tcg_rn, tcg_constant_i32(0), tcg_fpstatus);
9985 break;
9986 case 0x5a: /* FCVTNU */
9987 case 0x5b: /* FCVTMU */
9988 case 0x5c: /* FCVTAU */
9989 case 0x7a: /* FCVTPU */
9990 case 0x7b: /* FCVTZU */
9991 gen_helper_vfp_touqd(tcg_rd, tcg_rn, tcg_constant_i32(0), tcg_fpstatus);
9992 break;
9993 case 0x18: /* FRINTN */
9994 case 0x19: /* FRINTM */
9995 case 0x38: /* FRINTP */
9996 case 0x39: /* FRINTZ */
9997 case 0x58: /* FRINTA */
9998 case 0x79: /* FRINTI */
9999 gen_helper_rintd(tcg_rd, tcg_rn, tcg_fpstatus);
10000 break;
10001 case 0x59: /* FRINTX */
10002 gen_helper_rintd_exact(tcg_rd, tcg_rn, tcg_fpstatus);
10003 break;
10004 case 0x1e: /* FRINT32Z */
10005 case 0x5e: /* FRINT32X */
10006 gen_helper_frint32_d(tcg_rd, tcg_rn, tcg_fpstatus);
10007 break;
10008 case 0x1f: /* FRINT64Z */
10009 case 0x5f: /* FRINT64X */
10010 gen_helper_frint64_d(tcg_rd, tcg_rn, tcg_fpstatus);
10011 break;
10012 default:
10013 g_assert_not_reached();
10017 static void handle_2misc_fcmp_zero(DisasContext *s, int opcode,
10018 bool is_scalar, bool is_u, bool is_q,
10019 int size, int rn, int rd)
10021 bool is_double = (size == MO_64);
10022 TCGv_ptr fpst;
10024 if (!fp_access_check(s)) {
10025 return;
10028 fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
10030 if (is_double) {
10031 TCGv_i64 tcg_op = tcg_temp_new_i64();
10032 TCGv_i64 tcg_zero = tcg_constant_i64(0);
10033 TCGv_i64 tcg_res = tcg_temp_new_i64();
10034 NeonGenTwoDoubleOpFn *genfn;
10035 bool swap = false;
10036 int pass;
10038 switch (opcode) {
10039 case 0x2e: /* FCMLT (zero) */
10040 swap = true;
10041 /* fallthrough */
10042 case 0x2c: /* FCMGT (zero) */
10043 genfn = gen_helper_neon_cgt_f64;
10044 break;
10045 case 0x2d: /* FCMEQ (zero) */
10046 genfn = gen_helper_neon_ceq_f64;
10047 break;
10048 case 0x6d: /* FCMLE (zero) */
10049 swap = true;
10050 /* fall through */
10051 case 0x6c: /* FCMGE (zero) */
10052 genfn = gen_helper_neon_cge_f64;
10053 break;
10054 default:
10055 g_assert_not_reached();
10058 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
10059 read_vec_element(s, tcg_op, rn, pass, MO_64);
10060 if (swap) {
10061 genfn(tcg_res, tcg_zero, tcg_op, fpst);
10062 } else {
10063 genfn(tcg_res, tcg_op, tcg_zero, fpst);
10065 write_vec_element(s, tcg_res, rd, pass, MO_64);
10067 tcg_temp_free_i64(tcg_res);
10068 tcg_temp_free_i64(tcg_op);
10070 clear_vec_high(s, !is_scalar, rd);
10071 } else {
10072 TCGv_i32 tcg_op = tcg_temp_new_i32();
10073 TCGv_i32 tcg_zero = tcg_constant_i32(0);
10074 TCGv_i32 tcg_res = tcg_temp_new_i32();
10075 NeonGenTwoSingleOpFn *genfn;
10076 bool swap = false;
10077 int pass, maxpasses;
10079 if (size == MO_16) {
10080 switch (opcode) {
10081 case 0x2e: /* FCMLT (zero) */
10082 swap = true;
10083 /* fall through */
10084 case 0x2c: /* FCMGT (zero) */
10085 genfn = gen_helper_advsimd_cgt_f16;
10086 break;
10087 case 0x2d: /* FCMEQ (zero) */
10088 genfn = gen_helper_advsimd_ceq_f16;
10089 break;
10090 case 0x6d: /* FCMLE (zero) */
10091 swap = true;
10092 /* fall through */
10093 case 0x6c: /* FCMGE (zero) */
10094 genfn = gen_helper_advsimd_cge_f16;
10095 break;
10096 default:
10097 g_assert_not_reached();
10099 } else {
10100 switch (opcode) {
10101 case 0x2e: /* FCMLT (zero) */
10102 swap = true;
10103 /* fall through */
10104 case 0x2c: /* FCMGT (zero) */
10105 genfn = gen_helper_neon_cgt_f32;
10106 break;
10107 case 0x2d: /* FCMEQ (zero) */
10108 genfn = gen_helper_neon_ceq_f32;
10109 break;
10110 case 0x6d: /* FCMLE (zero) */
10111 swap = true;
10112 /* fall through */
10113 case 0x6c: /* FCMGE (zero) */
10114 genfn = gen_helper_neon_cge_f32;
10115 break;
10116 default:
10117 g_assert_not_reached();
10121 if (is_scalar) {
10122 maxpasses = 1;
10123 } else {
10124 int vector_size = 8 << is_q;
10125 maxpasses = vector_size >> size;
10128 for (pass = 0; pass < maxpasses; pass++) {
10129 read_vec_element_i32(s, tcg_op, rn, pass, size);
10130 if (swap) {
10131 genfn(tcg_res, tcg_zero, tcg_op, fpst);
10132 } else {
10133 genfn(tcg_res, tcg_op, tcg_zero, fpst);
10135 if (is_scalar) {
10136 write_fp_sreg(s, rd, tcg_res);
10137 } else {
10138 write_vec_element_i32(s, tcg_res, rd, pass, size);
10141 tcg_temp_free_i32(tcg_res);
10142 tcg_temp_free_i32(tcg_op);
10143 if (!is_scalar) {
10144 clear_vec_high(s, is_q, rd);
10148 tcg_temp_free_ptr(fpst);
10151 static void handle_2misc_reciprocal(DisasContext *s, int opcode,
10152 bool is_scalar, bool is_u, bool is_q,
10153 int size, int rn, int rd)
10155 bool is_double = (size == 3);
10156 TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
10158 if (is_double) {
10159 TCGv_i64 tcg_op = tcg_temp_new_i64();
10160 TCGv_i64 tcg_res = tcg_temp_new_i64();
10161 int pass;
10163 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
10164 read_vec_element(s, tcg_op, rn, pass, MO_64);
10165 switch (opcode) {
10166 case 0x3d: /* FRECPE */
10167 gen_helper_recpe_f64(tcg_res, tcg_op, fpst);
10168 break;
10169 case 0x3f: /* FRECPX */
10170 gen_helper_frecpx_f64(tcg_res, tcg_op, fpst);
10171 break;
10172 case 0x7d: /* FRSQRTE */
10173 gen_helper_rsqrte_f64(tcg_res, tcg_op, fpst);
10174 break;
10175 default:
10176 g_assert_not_reached();
10178 write_vec_element(s, tcg_res, rd, pass, MO_64);
10180 tcg_temp_free_i64(tcg_res);
10181 tcg_temp_free_i64(tcg_op);
10182 clear_vec_high(s, !is_scalar, rd);
10183 } else {
10184 TCGv_i32 tcg_op = tcg_temp_new_i32();
10185 TCGv_i32 tcg_res = tcg_temp_new_i32();
10186 int pass, maxpasses;
10188 if (is_scalar) {
10189 maxpasses = 1;
10190 } else {
10191 maxpasses = is_q ? 4 : 2;
10194 for (pass = 0; pass < maxpasses; pass++) {
10195 read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
10197 switch (opcode) {
10198 case 0x3c: /* URECPE */
10199 gen_helper_recpe_u32(tcg_res, tcg_op);
10200 break;
10201 case 0x3d: /* FRECPE */
10202 gen_helper_recpe_f32(tcg_res, tcg_op, fpst);
10203 break;
10204 case 0x3f: /* FRECPX */
10205 gen_helper_frecpx_f32(tcg_res, tcg_op, fpst);
10206 break;
10207 case 0x7d: /* FRSQRTE */
10208 gen_helper_rsqrte_f32(tcg_res, tcg_op, fpst);
10209 break;
10210 default:
10211 g_assert_not_reached();
10214 if (is_scalar) {
10215 write_fp_sreg(s, rd, tcg_res);
10216 } else {
10217 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
10220 tcg_temp_free_i32(tcg_res);
10221 tcg_temp_free_i32(tcg_op);
10222 if (!is_scalar) {
10223 clear_vec_high(s, is_q, rd);
10226 tcg_temp_free_ptr(fpst);
10229 static void handle_2misc_narrow(DisasContext *s, bool scalar,
10230 int opcode, bool u, bool is_q,
10231 int size, int rn, int rd)
10233 /* Handle 2-reg-misc ops which are narrowing (so each 2*size element
10234 * in the source becomes a size element in the destination).
10236 int pass;
10237 TCGv_i32 tcg_res[2];
10238 int destelt = is_q ? 2 : 0;
10239 int passes = scalar ? 1 : 2;
10241 if (scalar) {
10242 tcg_res[1] = tcg_constant_i32(0);
10245 for (pass = 0; pass < passes; pass++) {
10246 TCGv_i64 tcg_op = tcg_temp_new_i64();
10247 NeonGenNarrowFn *genfn = NULL;
10248 NeonGenNarrowEnvFn *genenvfn = NULL;
10250 if (scalar) {
10251 read_vec_element(s, tcg_op, rn, pass, size + 1);
10252 } else {
10253 read_vec_element(s, tcg_op, rn, pass, MO_64);
10255 tcg_res[pass] = tcg_temp_new_i32();
10257 switch (opcode) {
10258 case 0x12: /* XTN, SQXTUN */
10260 static NeonGenNarrowFn * const xtnfns[3] = {
10261 gen_helper_neon_narrow_u8,
10262 gen_helper_neon_narrow_u16,
10263 tcg_gen_extrl_i64_i32,
10265 static NeonGenNarrowEnvFn * const sqxtunfns[3] = {
10266 gen_helper_neon_unarrow_sat8,
10267 gen_helper_neon_unarrow_sat16,
10268 gen_helper_neon_unarrow_sat32,
10270 if (u) {
10271 genenvfn = sqxtunfns[size];
10272 } else {
10273 genfn = xtnfns[size];
10275 break;
10277 case 0x14: /* SQXTN, UQXTN */
10279 static NeonGenNarrowEnvFn * const fns[3][2] = {
10280 { gen_helper_neon_narrow_sat_s8,
10281 gen_helper_neon_narrow_sat_u8 },
10282 { gen_helper_neon_narrow_sat_s16,
10283 gen_helper_neon_narrow_sat_u16 },
10284 { gen_helper_neon_narrow_sat_s32,
10285 gen_helper_neon_narrow_sat_u32 },
10287 genenvfn = fns[size][u];
10288 break;
10290 case 0x16: /* FCVTN, FCVTN2 */
10291 /* 32 bit to 16 bit or 64 bit to 32 bit float conversion */
10292 if (size == 2) {
10293 gen_helper_vfp_fcvtsd(tcg_res[pass], tcg_op, cpu_env);
10294 } else {
10295 TCGv_i32 tcg_lo = tcg_temp_new_i32();
10296 TCGv_i32 tcg_hi = tcg_temp_new_i32();
10297 TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
10298 TCGv_i32 ahp = get_ahp_flag();
10300 tcg_gen_extr_i64_i32(tcg_lo, tcg_hi, tcg_op);
10301 gen_helper_vfp_fcvt_f32_to_f16(tcg_lo, tcg_lo, fpst, ahp);
10302 gen_helper_vfp_fcvt_f32_to_f16(tcg_hi, tcg_hi, fpst, ahp);
10303 tcg_gen_deposit_i32(tcg_res[pass], tcg_lo, tcg_hi, 16, 16);
10304 tcg_temp_free_i32(tcg_lo);
10305 tcg_temp_free_i32(tcg_hi);
10306 tcg_temp_free_ptr(fpst);
10307 tcg_temp_free_i32(ahp);
10309 break;
10310 case 0x36: /* BFCVTN, BFCVTN2 */
10312 TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
10313 gen_helper_bfcvt_pair(tcg_res[pass], tcg_op, fpst);
10314 tcg_temp_free_ptr(fpst);
10316 break;
10317 case 0x56: /* FCVTXN, FCVTXN2 */
10318 /* 64 bit to 32 bit float conversion
10319 * with von Neumann rounding (round to odd)
10321 assert(size == 2);
10322 gen_helper_fcvtx_f64_to_f32(tcg_res[pass], tcg_op, cpu_env);
10323 break;
10324 default:
10325 g_assert_not_reached();
10328 if (genfn) {
10329 genfn(tcg_res[pass], tcg_op);
10330 } else if (genenvfn) {
10331 genenvfn(tcg_res[pass], cpu_env, tcg_op);
10334 tcg_temp_free_i64(tcg_op);
10337 for (pass = 0; pass < 2; pass++) {
10338 write_vec_element_i32(s, tcg_res[pass], rd, destelt + pass, MO_32);
10339 tcg_temp_free_i32(tcg_res[pass]);
10341 clear_vec_high(s, is_q, rd);
10344 /* Remaining saturating accumulating ops */
10345 static void handle_2misc_satacc(DisasContext *s, bool is_scalar, bool is_u,
10346 bool is_q, int size, int rn, int rd)
10348 bool is_double = (size == 3);
10350 if (is_double) {
10351 TCGv_i64 tcg_rn = tcg_temp_new_i64();
10352 TCGv_i64 tcg_rd = tcg_temp_new_i64();
10353 int pass;
10355 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
10356 read_vec_element(s, tcg_rn, rn, pass, MO_64);
10357 read_vec_element(s, tcg_rd, rd, pass, MO_64);
10359 if (is_u) { /* USQADD */
10360 gen_helper_neon_uqadd_s64(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10361 } else { /* SUQADD */
10362 gen_helper_neon_sqadd_u64(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10364 write_vec_element(s, tcg_rd, rd, pass, MO_64);
10366 tcg_temp_free_i64(tcg_rd);
10367 tcg_temp_free_i64(tcg_rn);
10368 clear_vec_high(s, !is_scalar, rd);
10369 } else {
10370 TCGv_i32 tcg_rn = tcg_temp_new_i32();
10371 TCGv_i32 tcg_rd = tcg_temp_new_i32();
10372 int pass, maxpasses;
10374 if (is_scalar) {
10375 maxpasses = 1;
10376 } else {
10377 maxpasses = is_q ? 4 : 2;
10380 for (pass = 0; pass < maxpasses; pass++) {
10381 if (is_scalar) {
10382 read_vec_element_i32(s, tcg_rn, rn, pass, size);
10383 read_vec_element_i32(s, tcg_rd, rd, pass, size);
10384 } else {
10385 read_vec_element_i32(s, tcg_rn, rn, pass, MO_32);
10386 read_vec_element_i32(s, tcg_rd, rd, pass, MO_32);
10389 if (is_u) { /* USQADD */
10390 switch (size) {
10391 case 0:
10392 gen_helper_neon_uqadd_s8(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10393 break;
10394 case 1:
10395 gen_helper_neon_uqadd_s16(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10396 break;
10397 case 2:
10398 gen_helper_neon_uqadd_s32(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10399 break;
10400 default:
10401 g_assert_not_reached();
10403 } else { /* SUQADD */
10404 switch (size) {
10405 case 0:
10406 gen_helper_neon_sqadd_u8(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10407 break;
10408 case 1:
10409 gen_helper_neon_sqadd_u16(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10410 break;
10411 case 2:
10412 gen_helper_neon_sqadd_u32(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10413 break;
10414 default:
10415 g_assert_not_reached();
10419 if (is_scalar) {
10420 write_vec_element(s, tcg_constant_i64(0), rd, 0, MO_64);
10422 write_vec_element_i32(s, tcg_rd, rd, pass, MO_32);
10424 tcg_temp_free_i32(tcg_rd);
10425 tcg_temp_free_i32(tcg_rn);
10426 clear_vec_high(s, is_q, rd);
10430 /* AdvSIMD scalar two reg misc
10431 * 31 30 29 28 24 23 22 21 17 16 12 11 10 9 5 4 0
10432 * +-----+---+-----------+------+-----------+--------+-----+------+------+
10433 * | 0 1 | U | 1 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 | Rn | Rd |
10434 * +-----+---+-----------+------+-----------+--------+-----+------+------+
10436 static void disas_simd_scalar_two_reg_misc(DisasContext *s, uint32_t insn)
10438 int rd = extract32(insn, 0, 5);
10439 int rn = extract32(insn, 5, 5);
10440 int opcode = extract32(insn, 12, 5);
10441 int size = extract32(insn, 22, 2);
10442 bool u = extract32(insn, 29, 1);
10443 bool is_fcvt = false;
10444 int rmode;
10445 TCGv_i32 tcg_rmode;
10446 TCGv_ptr tcg_fpstatus;
10448 switch (opcode) {
10449 case 0x3: /* USQADD / SUQADD*/
10450 if (!fp_access_check(s)) {
10451 return;
10453 handle_2misc_satacc(s, true, u, false, size, rn, rd);
10454 return;
10455 case 0x7: /* SQABS / SQNEG */
10456 break;
10457 case 0xa: /* CMLT */
10458 if (u) {
10459 unallocated_encoding(s);
10460 return;
10462 /* fall through */
10463 case 0x8: /* CMGT, CMGE */
10464 case 0x9: /* CMEQ, CMLE */
10465 case 0xb: /* ABS, NEG */
10466 if (size != 3) {
10467 unallocated_encoding(s);
10468 return;
10470 break;
10471 case 0x12: /* SQXTUN */
10472 if (!u) {
10473 unallocated_encoding(s);
10474 return;
10476 /* fall through */
10477 case 0x14: /* SQXTN, UQXTN */
10478 if (size == 3) {
10479 unallocated_encoding(s);
10480 return;
10482 if (!fp_access_check(s)) {
10483 return;
10485 handle_2misc_narrow(s, true, opcode, u, false, size, rn, rd);
10486 return;
10487 case 0xc ... 0xf:
10488 case 0x16 ... 0x1d:
10489 case 0x1f:
10490 /* Floating point: U, size[1] and opcode indicate operation;
10491 * size[0] indicates single or double precision.
10493 opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
10494 size = extract32(size, 0, 1) ? 3 : 2;
10495 switch (opcode) {
10496 case 0x2c: /* FCMGT (zero) */
10497 case 0x2d: /* FCMEQ (zero) */
10498 case 0x2e: /* FCMLT (zero) */
10499 case 0x6c: /* FCMGE (zero) */
10500 case 0x6d: /* FCMLE (zero) */
10501 handle_2misc_fcmp_zero(s, opcode, true, u, true, size, rn, rd);
10502 return;
10503 case 0x1d: /* SCVTF */
10504 case 0x5d: /* UCVTF */
10506 bool is_signed = (opcode == 0x1d);
10507 if (!fp_access_check(s)) {
10508 return;
10510 handle_simd_intfp_conv(s, rd, rn, 1, is_signed, 0, size);
10511 return;
10513 case 0x3d: /* FRECPE */
10514 case 0x3f: /* FRECPX */
10515 case 0x7d: /* FRSQRTE */
10516 if (!fp_access_check(s)) {
10517 return;
10519 handle_2misc_reciprocal(s, opcode, true, u, true, size, rn, rd);
10520 return;
10521 case 0x1a: /* FCVTNS */
10522 case 0x1b: /* FCVTMS */
10523 case 0x3a: /* FCVTPS */
10524 case 0x3b: /* FCVTZS */
10525 case 0x5a: /* FCVTNU */
10526 case 0x5b: /* FCVTMU */
10527 case 0x7a: /* FCVTPU */
10528 case 0x7b: /* FCVTZU */
10529 is_fcvt = true;
10530 rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
10531 break;
10532 case 0x1c: /* FCVTAS */
10533 case 0x5c: /* FCVTAU */
10534 /* TIEAWAY doesn't fit in the usual rounding mode encoding */
10535 is_fcvt = true;
10536 rmode = FPROUNDING_TIEAWAY;
10537 break;
10538 case 0x56: /* FCVTXN, FCVTXN2 */
10539 if (size == 2) {
10540 unallocated_encoding(s);
10541 return;
10543 if (!fp_access_check(s)) {
10544 return;
10546 handle_2misc_narrow(s, true, opcode, u, false, size - 1, rn, rd);
10547 return;
10548 default:
10549 unallocated_encoding(s);
10550 return;
10552 break;
10553 default:
10554 unallocated_encoding(s);
10555 return;
10558 if (!fp_access_check(s)) {
10559 return;
10562 if (is_fcvt) {
10563 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rmode));
10564 tcg_fpstatus = fpstatus_ptr(FPST_FPCR);
10565 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
10566 } else {
10567 tcg_rmode = NULL;
10568 tcg_fpstatus = NULL;
10571 if (size == 3) {
10572 TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
10573 TCGv_i64 tcg_rd = tcg_temp_new_i64();
10575 handle_2misc_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rmode, tcg_fpstatus);
10576 write_fp_dreg(s, rd, tcg_rd);
10577 tcg_temp_free_i64(tcg_rd);
10578 tcg_temp_free_i64(tcg_rn);
10579 } else {
10580 TCGv_i32 tcg_rn = tcg_temp_new_i32();
10581 TCGv_i32 tcg_rd = tcg_temp_new_i32();
10583 read_vec_element_i32(s, tcg_rn, rn, 0, size);
10585 switch (opcode) {
10586 case 0x7: /* SQABS, SQNEG */
10588 NeonGenOneOpEnvFn *genfn;
10589 static NeonGenOneOpEnvFn * const fns[3][2] = {
10590 { gen_helper_neon_qabs_s8, gen_helper_neon_qneg_s8 },
10591 { gen_helper_neon_qabs_s16, gen_helper_neon_qneg_s16 },
10592 { gen_helper_neon_qabs_s32, gen_helper_neon_qneg_s32 },
10594 genfn = fns[size][u];
10595 genfn(tcg_rd, cpu_env, tcg_rn);
10596 break;
10598 case 0x1a: /* FCVTNS */
10599 case 0x1b: /* FCVTMS */
10600 case 0x1c: /* FCVTAS */
10601 case 0x3a: /* FCVTPS */
10602 case 0x3b: /* FCVTZS */
10603 gen_helper_vfp_tosls(tcg_rd, tcg_rn, tcg_constant_i32(0),
10604 tcg_fpstatus);
10605 break;
10606 case 0x5a: /* FCVTNU */
10607 case 0x5b: /* FCVTMU */
10608 case 0x5c: /* FCVTAU */
10609 case 0x7a: /* FCVTPU */
10610 case 0x7b: /* FCVTZU */
10611 gen_helper_vfp_touls(tcg_rd, tcg_rn, tcg_constant_i32(0),
10612 tcg_fpstatus);
10613 break;
10614 default:
10615 g_assert_not_reached();
10618 write_fp_sreg(s, rd, tcg_rd);
10619 tcg_temp_free_i32(tcg_rd);
10620 tcg_temp_free_i32(tcg_rn);
10623 if (is_fcvt) {
10624 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
10625 tcg_temp_free_i32(tcg_rmode);
10626 tcg_temp_free_ptr(tcg_fpstatus);
10630 /* SSHR[RA]/USHR[RA] - Vector shift right (optional rounding/accumulate) */
10631 static void handle_vec_simd_shri(DisasContext *s, bool is_q, bool is_u,
10632 int immh, int immb, int opcode, int rn, int rd)
10634 int size = 32 - clz32(immh) - 1;
10635 int immhb = immh << 3 | immb;
10636 int shift = 2 * (8 << size) - immhb;
10637 GVecGen2iFn *gvec_fn;
10639 if (extract32(immh, 3, 1) && !is_q) {
10640 unallocated_encoding(s);
10641 return;
10643 tcg_debug_assert(size <= 3);
10645 if (!fp_access_check(s)) {
10646 return;
10649 switch (opcode) {
10650 case 0x02: /* SSRA / USRA (accumulate) */
10651 gvec_fn = is_u ? gen_gvec_usra : gen_gvec_ssra;
10652 break;
10654 case 0x08: /* SRI */
10655 gvec_fn = gen_gvec_sri;
10656 break;
10658 case 0x00: /* SSHR / USHR */
10659 if (is_u) {
10660 if (shift == 8 << size) {
10661 /* Shift count the same size as element size produces zero. */
10662 tcg_gen_gvec_dup_imm(size, vec_full_reg_offset(s, rd),
10663 is_q ? 16 : 8, vec_full_reg_size(s), 0);
10664 return;
10666 gvec_fn = tcg_gen_gvec_shri;
10667 } else {
10668 /* Shift count the same size as element size produces all sign. */
10669 if (shift == 8 << size) {
10670 shift -= 1;
10672 gvec_fn = tcg_gen_gvec_sari;
10674 break;
10676 case 0x04: /* SRSHR / URSHR (rounding) */
10677 gvec_fn = is_u ? gen_gvec_urshr : gen_gvec_srshr;
10678 break;
10680 case 0x06: /* SRSRA / URSRA (accum + rounding) */
10681 gvec_fn = is_u ? gen_gvec_ursra : gen_gvec_srsra;
10682 break;
10684 default:
10685 g_assert_not_reached();
10688 gen_gvec_fn2i(s, is_q, rd, rn, shift, gvec_fn, size);
10691 /* SHL/SLI - Vector shift left */
10692 static void handle_vec_simd_shli(DisasContext *s, bool is_q, bool insert,
10693 int immh, int immb, int opcode, int rn, int rd)
10695 int size = 32 - clz32(immh) - 1;
10696 int immhb = immh << 3 | immb;
10697 int shift = immhb - (8 << size);
10699 /* Range of size is limited by decode: immh is a non-zero 4 bit field */
10700 assert(size >= 0 && size <= 3);
10702 if (extract32(immh, 3, 1) && !is_q) {
10703 unallocated_encoding(s);
10704 return;
10707 if (!fp_access_check(s)) {
10708 return;
10711 if (insert) {
10712 gen_gvec_fn2i(s, is_q, rd, rn, shift, gen_gvec_sli, size);
10713 } else {
10714 gen_gvec_fn2i(s, is_q, rd, rn, shift, tcg_gen_gvec_shli, size);
10718 /* USHLL/SHLL - Vector shift left with widening */
10719 static void handle_vec_simd_wshli(DisasContext *s, bool is_q, bool is_u,
10720 int immh, int immb, int opcode, int rn, int rd)
10722 int size = 32 - clz32(immh) - 1;
10723 int immhb = immh << 3 | immb;
10724 int shift = immhb - (8 << size);
10725 int dsize = 64;
10726 int esize = 8 << size;
10727 int elements = dsize/esize;
10728 TCGv_i64 tcg_rn = new_tmp_a64(s);
10729 TCGv_i64 tcg_rd = new_tmp_a64(s);
10730 int i;
10732 if (size >= 3) {
10733 unallocated_encoding(s);
10734 return;
10737 if (!fp_access_check(s)) {
10738 return;
10741 /* For the LL variants the store is larger than the load,
10742 * so if rd == rn we would overwrite parts of our input.
10743 * So load everything right now and use shifts in the main loop.
10745 read_vec_element(s, tcg_rn, rn, is_q ? 1 : 0, MO_64);
10747 for (i = 0; i < elements; i++) {
10748 tcg_gen_shri_i64(tcg_rd, tcg_rn, i * esize);
10749 ext_and_shift_reg(tcg_rd, tcg_rd, size | (!is_u << 2), 0);
10750 tcg_gen_shli_i64(tcg_rd, tcg_rd, shift);
10751 write_vec_element(s, tcg_rd, rd, i, size + 1);
10755 /* SHRN/RSHRN - Shift right with narrowing (and potential rounding) */
10756 static void handle_vec_simd_shrn(DisasContext *s, bool is_q,
10757 int immh, int immb, int opcode, int rn, int rd)
10759 int immhb = immh << 3 | immb;
10760 int size = 32 - clz32(immh) - 1;
10761 int dsize = 64;
10762 int esize = 8 << size;
10763 int elements = dsize/esize;
10764 int shift = (2 * esize) - immhb;
10765 bool round = extract32(opcode, 0, 1);
10766 TCGv_i64 tcg_rn, tcg_rd, tcg_final;
10767 TCGv_i64 tcg_round;
10768 int i;
10770 if (extract32(immh, 3, 1)) {
10771 unallocated_encoding(s);
10772 return;
10775 if (!fp_access_check(s)) {
10776 return;
10779 tcg_rn = tcg_temp_new_i64();
10780 tcg_rd = tcg_temp_new_i64();
10781 tcg_final = tcg_temp_new_i64();
10782 read_vec_element(s, tcg_final, rd, is_q ? 1 : 0, MO_64);
10784 if (round) {
10785 tcg_round = tcg_constant_i64(1ULL << (shift - 1));
10786 } else {
10787 tcg_round = NULL;
10790 for (i = 0; i < elements; i++) {
10791 read_vec_element(s, tcg_rn, rn, i, size+1);
10792 handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
10793 false, true, size+1, shift);
10795 tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
10798 if (!is_q) {
10799 write_vec_element(s, tcg_final, rd, 0, MO_64);
10800 } else {
10801 write_vec_element(s, tcg_final, rd, 1, MO_64);
10803 tcg_temp_free_i64(tcg_rn);
10804 tcg_temp_free_i64(tcg_rd);
10805 tcg_temp_free_i64(tcg_final);
10807 clear_vec_high(s, is_q, rd);
10811 /* AdvSIMD shift by immediate
10812 * 31 30 29 28 23 22 19 18 16 15 11 10 9 5 4 0
10813 * +---+---+---+-------------+------+------+--------+---+------+------+
10814 * | 0 | Q | U | 0 1 1 1 1 0 | immh | immb | opcode | 1 | Rn | Rd |
10815 * +---+---+---+-------------+------+------+--------+---+------+------+
10817 static void disas_simd_shift_imm(DisasContext *s, uint32_t insn)
10819 int rd = extract32(insn, 0, 5);
10820 int rn = extract32(insn, 5, 5);
10821 int opcode = extract32(insn, 11, 5);
10822 int immb = extract32(insn, 16, 3);
10823 int immh = extract32(insn, 19, 4);
10824 bool is_u = extract32(insn, 29, 1);
10825 bool is_q = extract32(insn, 30, 1);
10827 /* data_proc_simd[] has sent immh == 0 to disas_simd_mod_imm. */
10828 assert(immh != 0);
10830 switch (opcode) {
10831 case 0x08: /* SRI */
10832 if (!is_u) {
10833 unallocated_encoding(s);
10834 return;
10836 /* fall through */
10837 case 0x00: /* SSHR / USHR */
10838 case 0x02: /* SSRA / USRA (accumulate) */
10839 case 0x04: /* SRSHR / URSHR (rounding) */
10840 case 0x06: /* SRSRA / URSRA (accum + rounding) */
10841 handle_vec_simd_shri(s, is_q, is_u, immh, immb, opcode, rn, rd);
10842 break;
10843 case 0x0a: /* SHL / SLI */
10844 handle_vec_simd_shli(s, is_q, is_u, immh, immb, opcode, rn, rd);
10845 break;
10846 case 0x10: /* SHRN */
10847 case 0x11: /* RSHRN / SQRSHRUN */
10848 if (is_u) {
10849 handle_vec_simd_sqshrn(s, false, is_q, false, true, immh, immb,
10850 opcode, rn, rd);
10851 } else {
10852 handle_vec_simd_shrn(s, is_q, immh, immb, opcode, rn, rd);
10854 break;
10855 case 0x12: /* SQSHRN / UQSHRN */
10856 case 0x13: /* SQRSHRN / UQRSHRN */
10857 handle_vec_simd_sqshrn(s, false, is_q, is_u, is_u, immh, immb,
10858 opcode, rn, rd);
10859 break;
10860 case 0x14: /* SSHLL / USHLL */
10861 handle_vec_simd_wshli(s, is_q, is_u, immh, immb, opcode, rn, rd);
10862 break;
10863 case 0x1c: /* SCVTF / UCVTF */
10864 handle_simd_shift_intfp_conv(s, false, is_q, is_u, immh, immb,
10865 opcode, rn, rd);
10866 break;
10867 case 0xc: /* SQSHLU */
10868 if (!is_u) {
10869 unallocated_encoding(s);
10870 return;
10872 handle_simd_qshl(s, false, is_q, false, true, immh, immb, rn, rd);
10873 break;
10874 case 0xe: /* SQSHL, UQSHL */
10875 handle_simd_qshl(s, false, is_q, is_u, is_u, immh, immb, rn, rd);
10876 break;
10877 case 0x1f: /* FCVTZS/ FCVTZU */
10878 handle_simd_shift_fpint_conv(s, false, is_q, is_u, immh, immb, rn, rd);
10879 return;
10880 default:
10881 unallocated_encoding(s);
10882 return;
10886 /* Generate code to do a "long" addition or subtraction, ie one done in
10887 * TCGv_i64 on vector lanes twice the width specified by size.
10889 static void gen_neon_addl(int size, bool is_sub, TCGv_i64 tcg_res,
10890 TCGv_i64 tcg_op1, TCGv_i64 tcg_op2)
10892 static NeonGenTwo64OpFn * const fns[3][2] = {
10893 { gen_helper_neon_addl_u16, gen_helper_neon_subl_u16 },
10894 { gen_helper_neon_addl_u32, gen_helper_neon_subl_u32 },
10895 { tcg_gen_add_i64, tcg_gen_sub_i64 },
10897 NeonGenTwo64OpFn *genfn;
10898 assert(size < 3);
10900 genfn = fns[size][is_sub];
10901 genfn(tcg_res, tcg_op1, tcg_op2);
10904 static void handle_3rd_widening(DisasContext *s, int is_q, int is_u, int size,
10905 int opcode, int rd, int rn, int rm)
10907 /* 3-reg-different widening insns: 64 x 64 -> 128 */
10908 TCGv_i64 tcg_res[2];
10909 int pass, accop;
10911 tcg_res[0] = tcg_temp_new_i64();
10912 tcg_res[1] = tcg_temp_new_i64();
10914 /* Does this op do an adding accumulate, a subtracting accumulate,
10915 * or no accumulate at all?
10917 switch (opcode) {
10918 case 5:
10919 case 8:
10920 case 9:
10921 accop = 1;
10922 break;
10923 case 10:
10924 case 11:
10925 accop = -1;
10926 break;
10927 default:
10928 accop = 0;
10929 break;
10932 if (accop != 0) {
10933 read_vec_element(s, tcg_res[0], rd, 0, MO_64);
10934 read_vec_element(s, tcg_res[1], rd, 1, MO_64);
10937 /* size == 2 means two 32x32->64 operations; this is worth special
10938 * casing because we can generally handle it inline.
10940 if (size == 2) {
10941 for (pass = 0; pass < 2; pass++) {
10942 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10943 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
10944 TCGv_i64 tcg_passres;
10945 MemOp memop = MO_32 | (is_u ? 0 : MO_SIGN);
10947 int elt = pass + is_q * 2;
10949 read_vec_element(s, tcg_op1, rn, elt, memop);
10950 read_vec_element(s, tcg_op2, rm, elt, memop);
10952 if (accop == 0) {
10953 tcg_passres = tcg_res[pass];
10954 } else {
10955 tcg_passres = tcg_temp_new_i64();
10958 switch (opcode) {
10959 case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10960 tcg_gen_add_i64(tcg_passres, tcg_op1, tcg_op2);
10961 break;
10962 case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
10963 tcg_gen_sub_i64(tcg_passres, tcg_op1, tcg_op2);
10964 break;
10965 case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10966 case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10968 TCGv_i64 tcg_tmp1 = tcg_temp_new_i64();
10969 TCGv_i64 tcg_tmp2 = tcg_temp_new_i64();
10971 tcg_gen_sub_i64(tcg_tmp1, tcg_op1, tcg_op2);
10972 tcg_gen_sub_i64(tcg_tmp2, tcg_op2, tcg_op1);
10973 tcg_gen_movcond_i64(is_u ? TCG_COND_GEU : TCG_COND_GE,
10974 tcg_passres,
10975 tcg_op1, tcg_op2, tcg_tmp1, tcg_tmp2);
10976 tcg_temp_free_i64(tcg_tmp1);
10977 tcg_temp_free_i64(tcg_tmp2);
10978 break;
10980 case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10981 case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10982 case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
10983 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
10984 break;
10985 case 9: /* SQDMLAL, SQDMLAL2 */
10986 case 11: /* SQDMLSL, SQDMLSL2 */
10987 case 13: /* SQDMULL, SQDMULL2 */
10988 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
10989 gen_helper_neon_addl_saturate_s64(tcg_passres, cpu_env,
10990 tcg_passres, tcg_passres);
10991 break;
10992 default:
10993 g_assert_not_reached();
10996 if (opcode == 9 || opcode == 11) {
10997 /* saturating accumulate ops */
10998 if (accop < 0) {
10999 tcg_gen_neg_i64(tcg_passres, tcg_passres);
11001 gen_helper_neon_addl_saturate_s64(tcg_res[pass], cpu_env,
11002 tcg_res[pass], tcg_passres);
11003 } else if (accop > 0) {
11004 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
11005 } else if (accop < 0) {
11006 tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
11009 if (accop != 0) {
11010 tcg_temp_free_i64(tcg_passres);
11013 tcg_temp_free_i64(tcg_op1);
11014 tcg_temp_free_i64(tcg_op2);
11016 } else {
11017 /* size 0 or 1, generally helper functions */
11018 for (pass = 0; pass < 2; pass++) {
11019 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11020 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11021 TCGv_i64 tcg_passres;
11022 int elt = pass + is_q * 2;
11024 read_vec_element_i32(s, tcg_op1, rn, elt, MO_32);
11025 read_vec_element_i32(s, tcg_op2, rm, elt, MO_32);
11027 if (accop == 0) {
11028 tcg_passres = tcg_res[pass];
11029 } else {
11030 tcg_passres = tcg_temp_new_i64();
11033 switch (opcode) {
11034 case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
11035 case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
11037 TCGv_i64 tcg_op2_64 = tcg_temp_new_i64();
11038 static NeonGenWidenFn * const widenfns[2][2] = {
11039 { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
11040 { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
11042 NeonGenWidenFn *widenfn = widenfns[size][is_u];
11044 widenfn(tcg_op2_64, tcg_op2);
11045 widenfn(tcg_passres, tcg_op1);
11046 gen_neon_addl(size, (opcode == 2), tcg_passres,
11047 tcg_passres, tcg_op2_64);
11048 tcg_temp_free_i64(tcg_op2_64);
11049 break;
11051 case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
11052 case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
11053 if (size == 0) {
11054 if (is_u) {
11055 gen_helper_neon_abdl_u16(tcg_passres, tcg_op1, tcg_op2);
11056 } else {
11057 gen_helper_neon_abdl_s16(tcg_passres, tcg_op1, tcg_op2);
11059 } else {
11060 if (is_u) {
11061 gen_helper_neon_abdl_u32(tcg_passres, tcg_op1, tcg_op2);
11062 } else {
11063 gen_helper_neon_abdl_s32(tcg_passres, tcg_op1, tcg_op2);
11066 break;
11067 case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
11068 case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
11069 case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
11070 if (size == 0) {
11071 if (is_u) {
11072 gen_helper_neon_mull_u8(tcg_passres, tcg_op1, tcg_op2);
11073 } else {
11074 gen_helper_neon_mull_s8(tcg_passres, tcg_op1, tcg_op2);
11076 } else {
11077 if (is_u) {
11078 gen_helper_neon_mull_u16(tcg_passres, tcg_op1, tcg_op2);
11079 } else {
11080 gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
11083 break;
11084 case 9: /* SQDMLAL, SQDMLAL2 */
11085 case 11: /* SQDMLSL, SQDMLSL2 */
11086 case 13: /* SQDMULL, SQDMULL2 */
11087 assert(size == 1);
11088 gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
11089 gen_helper_neon_addl_saturate_s32(tcg_passres, cpu_env,
11090 tcg_passres, tcg_passres);
11091 break;
11092 default:
11093 g_assert_not_reached();
11095 tcg_temp_free_i32(tcg_op1);
11096 tcg_temp_free_i32(tcg_op2);
11098 if (accop != 0) {
11099 if (opcode == 9 || opcode == 11) {
11100 /* saturating accumulate ops */
11101 if (accop < 0) {
11102 gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
11104 gen_helper_neon_addl_saturate_s32(tcg_res[pass], cpu_env,
11105 tcg_res[pass],
11106 tcg_passres);
11107 } else {
11108 gen_neon_addl(size, (accop < 0), tcg_res[pass],
11109 tcg_res[pass], tcg_passres);
11111 tcg_temp_free_i64(tcg_passres);
11116 write_vec_element(s, tcg_res[0], rd, 0, MO_64);
11117 write_vec_element(s, tcg_res[1], rd, 1, MO_64);
11118 tcg_temp_free_i64(tcg_res[0]);
11119 tcg_temp_free_i64(tcg_res[1]);
11122 static void handle_3rd_wide(DisasContext *s, int is_q, int is_u, int size,
11123 int opcode, int rd, int rn, int rm)
11125 TCGv_i64 tcg_res[2];
11126 int part = is_q ? 2 : 0;
11127 int pass;
11129 for (pass = 0; pass < 2; pass++) {
11130 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
11131 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11132 TCGv_i64 tcg_op2_wide = tcg_temp_new_i64();
11133 static NeonGenWidenFn * const widenfns[3][2] = {
11134 { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
11135 { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
11136 { tcg_gen_ext_i32_i64, tcg_gen_extu_i32_i64 },
11138 NeonGenWidenFn *widenfn = widenfns[size][is_u];
11140 read_vec_element(s, tcg_op1, rn, pass, MO_64);
11141 read_vec_element_i32(s, tcg_op2, rm, part + pass, MO_32);
11142 widenfn(tcg_op2_wide, tcg_op2);
11143 tcg_temp_free_i32(tcg_op2);
11144 tcg_res[pass] = tcg_temp_new_i64();
11145 gen_neon_addl(size, (opcode == 3),
11146 tcg_res[pass], tcg_op1, tcg_op2_wide);
11147 tcg_temp_free_i64(tcg_op1);
11148 tcg_temp_free_i64(tcg_op2_wide);
11151 for (pass = 0; pass < 2; pass++) {
11152 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
11153 tcg_temp_free_i64(tcg_res[pass]);
11157 static void do_narrow_round_high_u32(TCGv_i32 res, TCGv_i64 in)
11159 tcg_gen_addi_i64(in, in, 1U << 31);
11160 tcg_gen_extrh_i64_i32(res, in);
11163 static void handle_3rd_narrowing(DisasContext *s, int is_q, int is_u, int size,
11164 int opcode, int rd, int rn, int rm)
11166 TCGv_i32 tcg_res[2];
11167 int part = is_q ? 2 : 0;
11168 int pass;
11170 for (pass = 0; pass < 2; pass++) {
11171 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
11172 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
11173 TCGv_i64 tcg_wideres = tcg_temp_new_i64();
11174 static NeonGenNarrowFn * const narrowfns[3][2] = {
11175 { gen_helper_neon_narrow_high_u8,
11176 gen_helper_neon_narrow_round_high_u8 },
11177 { gen_helper_neon_narrow_high_u16,
11178 gen_helper_neon_narrow_round_high_u16 },
11179 { tcg_gen_extrh_i64_i32, do_narrow_round_high_u32 },
11181 NeonGenNarrowFn *gennarrow = narrowfns[size][is_u];
11183 read_vec_element(s, tcg_op1, rn, pass, MO_64);
11184 read_vec_element(s, tcg_op2, rm, pass, MO_64);
11186 gen_neon_addl(size, (opcode == 6), tcg_wideres, tcg_op1, tcg_op2);
11188 tcg_temp_free_i64(tcg_op1);
11189 tcg_temp_free_i64(tcg_op2);
11191 tcg_res[pass] = tcg_temp_new_i32();
11192 gennarrow(tcg_res[pass], tcg_wideres);
11193 tcg_temp_free_i64(tcg_wideres);
11196 for (pass = 0; pass < 2; pass++) {
11197 write_vec_element_i32(s, tcg_res[pass], rd, pass + part, MO_32);
11198 tcg_temp_free_i32(tcg_res[pass]);
11200 clear_vec_high(s, is_q, rd);
11203 /* AdvSIMD three different
11204 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 0
11205 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
11206 * | 0 | Q | U | 0 1 1 1 0 | size | 1 | Rm | opcode | 0 0 | Rn | Rd |
11207 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
11209 static void disas_simd_three_reg_diff(DisasContext *s, uint32_t insn)
11211 /* Instructions in this group fall into three basic classes
11212 * (in each case with the operation working on each element in
11213 * the input vectors):
11214 * (1) widening 64 x 64 -> 128 (with possibly Vd as an extra
11215 * 128 bit input)
11216 * (2) wide 64 x 128 -> 128
11217 * (3) narrowing 128 x 128 -> 64
11218 * Here we do initial decode, catch unallocated cases and
11219 * dispatch to separate functions for each class.
11221 int is_q = extract32(insn, 30, 1);
11222 int is_u = extract32(insn, 29, 1);
11223 int size = extract32(insn, 22, 2);
11224 int opcode = extract32(insn, 12, 4);
11225 int rm = extract32(insn, 16, 5);
11226 int rn = extract32(insn, 5, 5);
11227 int rd = extract32(insn, 0, 5);
11229 switch (opcode) {
11230 case 1: /* SADDW, SADDW2, UADDW, UADDW2 */
11231 case 3: /* SSUBW, SSUBW2, USUBW, USUBW2 */
11232 /* 64 x 128 -> 128 */
11233 if (size == 3) {
11234 unallocated_encoding(s);
11235 return;
11237 if (!fp_access_check(s)) {
11238 return;
11240 handle_3rd_wide(s, is_q, is_u, size, opcode, rd, rn, rm);
11241 break;
11242 case 4: /* ADDHN, ADDHN2, RADDHN, RADDHN2 */
11243 case 6: /* SUBHN, SUBHN2, RSUBHN, RSUBHN2 */
11244 /* 128 x 128 -> 64 */
11245 if (size == 3) {
11246 unallocated_encoding(s);
11247 return;
11249 if (!fp_access_check(s)) {
11250 return;
11252 handle_3rd_narrowing(s, is_q, is_u, size, opcode, rd, rn, rm);
11253 break;
11254 case 14: /* PMULL, PMULL2 */
11255 if (is_u) {
11256 unallocated_encoding(s);
11257 return;
11259 switch (size) {
11260 case 0: /* PMULL.P8 */
11261 if (!fp_access_check(s)) {
11262 return;
11264 /* The Q field specifies lo/hi half input for this insn. */
11265 gen_gvec_op3_ool(s, true, rd, rn, rm, is_q,
11266 gen_helper_neon_pmull_h);
11267 break;
11269 case 3: /* PMULL.P64 */
11270 if (!dc_isar_feature(aa64_pmull, s)) {
11271 unallocated_encoding(s);
11272 return;
11274 if (!fp_access_check(s)) {
11275 return;
11277 /* The Q field specifies lo/hi half input for this insn. */
11278 gen_gvec_op3_ool(s, true, rd, rn, rm, is_q,
11279 gen_helper_gvec_pmull_q);
11280 break;
11282 default:
11283 unallocated_encoding(s);
11284 break;
11286 return;
11287 case 9: /* SQDMLAL, SQDMLAL2 */
11288 case 11: /* SQDMLSL, SQDMLSL2 */
11289 case 13: /* SQDMULL, SQDMULL2 */
11290 if (is_u || size == 0) {
11291 unallocated_encoding(s);
11292 return;
11294 /* fall through */
11295 case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
11296 case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
11297 case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
11298 case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
11299 case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
11300 case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
11301 case 12: /* SMULL, SMULL2, UMULL, UMULL2 */
11302 /* 64 x 64 -> 128 */
11303 if (size == 3) {
11304 unallocated_encoding(s);
11305 return;
11307 if (!fp_access_check(s)) {
11308 return;
11311 handle_3rd_widening(s, is_q, is_u, size, opcode, rd, rn, rm);
11312 break;
11313 default:
11314 /* opcode 15 not allocated */
11315 unallocated_encoding(s);
11316 break;
11320 /* Logic op (opcode == 3) subgroup of C3.6.16. */
11321 static void disas_simd_3same_logic(DisasContext *s, uint32_t insn)
11323 int rd = extract32(insn, 0, 5);
11324 int rn = extract32(insn, 5, 5);
11325 int rm = extract32(insn, 16, 5);
11326 int size = extract32(insn, 22, 2);
11327 bool is_u = extract32(insn, 29, 1);
11328 bool is_q = extract32(insn, 30, 1);
11330 if (!fp_access_check(s)) {
11331 return;
11334 switch (size + 4 * is_u) {
11335 case 0: /* AND */
11336 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_and, 0);
11337 return;
11338 case 1: /* BIC */
11339 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_andc, 0);
11340 return;
11341 case 2: /* ORR */
11342 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_or, 0);
11343 return;
11344 case 3: /* ORN */
11345 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_orc, 0);
11346 return;
11347 case 4: /* EOR */
11348 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_xor, 0);
11349 return;
11351 case 5: /* BSL bitwise select */
11352 gen_gvec_fn4(s, is_q, rd, rd, rn, rm, tcg_gen_gvec_bitsel, 0);
11353 return;
11354 case 6: /* BIT, bitwise insert if true */
11355 gen_gvec_fn4(s, is_q, rd, rm, rn, rd, tcg_gen_gvec_bitsel, 0);
11356 return;
11357 case 7: /* BIF, bitwise insert if false */
11358 gen_gvec_fn4(s, is_q, rd, rm, rd, rn, tcg_gen_gvec_bitsel, 0);
11359 return;
11361 default:
11362 g_assert_not_reached();
11366 /* Pairwise op subgroup of C3.6.16.
11368 * This is called directly or via the handle_3same_float for float pairwise
11369 * operations where the opcode and size are calculated differently.
11371 static void handle_simd_3same_pair(DisasContext *s, int is_q, int u, int opcode,
11372 int size, int rn, int rm, int rd)
11374 TCGv_ptr fpst;
11375 int pass;
11377 /* Floating point operations need fpst */
11378 if (opcode >= 0x58) {
11379 fpst = fpstatus_ptr(FPST_FPCR);
11380 } else {
11381 fpst = NULL;
11384 if (!fp_access_check(s)) {
11385 return;
11388 /* These operations work on the concatenated rm:rn, with each pair of
11389 * adjacent elements being operated on to produce an element in the result.
11391 if (size == 3) {
11392 TCGv_i64 tcg_res[2];
11394 for (pass = 0; pass < 2; pass++) {
11395 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
11396 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
11397 int passreg = (pass == 0) ? rn : rm;
11399 read_vec_element(s, tcg_op1, passreg, 0, MO_64);
11400 read_vec_element(s, tcg_op2, passreg, 1, MO_64);
11401 tcg_res[pass] = tcg_temp_new_i64();
11403 switch (opcode) {
11404 case 0x17: /* ADDP */
11405 tcg_gen_add_i64(tcg_res[pass], tcg_op1, tcg_op2);
11406 break;
11407 case 0x58: /* FMAXNMP */
11408 gen_helper_vfp_maxnumd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11409 break;
11410 case 0x5a: /* FADDP */
11411 gen_helper_vfp_addd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11412 break;
11413 case 0x5e: /* FMAXP */
11414 gen_helper_vfp_maxd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11415 break;
11416 case 0x78: /* FMINNMP */
11417 gen_helper_vfp_minnumd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11418 break;
11419 case 0x7e: /* FMINP */
11420 gen_helper_vfp_mind(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11421 break;
11422 default:
11423 g_assert_not_reached();
11426 tcg_temp_free_i64(tcg_op1);
11427 tcg_temp_free_i64(tcg_op2);
11430 for (pass = 0; pass < 2; pass++) {
11431 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
11432 tcg_temp_free_i64(tcg_res[pass]);
11434 } else {
11435 int maxpass = is_q ? 4 : 2;
11436 TCGv_i32 tcg_res[4];
11438 for (pass = 0; pass < maxpass; pass++) {
11439 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11440 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11441 NeonGenTwoOpFn *genfn = NULL;
11442 int passreg = pass < (maxpass / 2) ? rn : rm;
11443 int passelt = (is_q && (pass & 1)) ? 2 : 0;
11445 read_vec_element_i32(s, tcg_op1, passreg, passelt, MO_32);
11446 read_vec_element_i32(s, tcg_op2, passreg, passelt + 1, MO_32);
11447 tcg_res[pass] = tcg_temp_new_i32();
11449 switch (opcode) {
11450 case 0x17: /* ADDP */
11452 static NeonGenTwoOpFn * const fns[3] = {
11453 gen_helper_neon_padd_u8,
11454 gen_helper_neon_padd_u16,
11455 tcg_gen_add_i32,
11457 genfn = fns[size];
11458 break;
11460 case 0x14: /* SMAXP, UMAXP */
11462 static NeonGenTwoOpFn * const fns[3][2] = {
11463 { gen_helper_neon_pmax_s8, gen_helper_neon_pmax_u8 },
11464 { gen_helper_neon_pmax_s16, gen_helper_neon_pmax_u16 },
11465 { tcg_gen_smax_i32, tcg_gen_umax_i32 },
11467 genfn = fns[size][u];
11468 break;
11470 case 0x15: /* SMINP, UMINP */
11472 static NeonGenTwoOpFn * const fns[3][2] = {
11473 { gen_helper_neon_pmin_s8, gen_helper_neon_pmin_u8 },
11474 { gen_helper_neon_pmin_s16, gen_helper_neon_pmin_u16 },
11475 { tcg_gen_smin_i32, tcg_gen_umin_i32 },
11477 genfn = fns[size][u];
11478 break;
11480 /* The FP operations are all on single floats (32 bit) */
11481 case 0x58: /* FMAXNMP */
11482 gen_helper_vfp_maxnums(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11483 break;
11484 case 0x5a: /* FADDP */
11485 gen_helper_vfp_adds(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11486 break;
11487 case 0x5e: /* FMAXP */
11488 gen_helper_vfp_maxs(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11489 break;
11490 case 0x78: /* FMINNMP */
11491 gen_helper_vfp_minnums(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11492 break;
11493 case 0x7e: /* FMINP */
11494 gen_helper_vfp_mins(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11495 break;
11496 default:
11497 g_assert_not_reached();
11500 /* FP ops called directly, otherwise call now */
11501 if (genfn) {
11502 genfn(tcg_res[pass], tcg_op1, tcg_op2);
11505 tcg_temp_free_i32(tcg_op1);
11506 tcg_temp_free_i32(tcg_op2);
11509 for (pass = 0; pass < maxpass; pass++) {
11510 write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_32);
11511 tcg_temp_free_i32(tcg_res[pass]);
11513 clear_vec_high(s, is_q, rd);
11516 if (fpst) {
11517 tcg_temp_free_ptr(fpst);
11521 /* Floating point op subgroup of C3.6.16. */
11522 static void disas_simd_3same_float(DisasContext *s, uint32_t insn)
11524 /* For floating point ops, the U, size[1] and opcode bits
11525 * together indicate the operation. size[0] indicates single
11526 * or double.
11528 int fpopcode = extract32(insn, 11, 5)
11529 | (extract32(insn, 23, 1) << 5)
11530 | (extract32(insn, 29, 1) << 6);
11531 int is_q = extract32(insn, 30, 1);
11532 int size = extract32(insn, 22, 1);
11533 int rm = extract32(insn, 16, 5);
11534 int rn = extract32(insn, 5, 5);
11535 int rd = extract32(insn, 0, 5);
11537 int datasize = is_q ? 128 : 64;
11538 int esize = 32 << size;
11539 int elements = datasize / esize;
11541 if (size == 1 && !is_q) {
11542 unallocated_encoding(s);
11543 return;
11546 switch (fpopcode) {
11547 case 0x58: /* FMAXNMP */
11548 case 0x5a: /* FADDP */
11549 case 0x5e: /* FMAXP */
11550 case 0x78: /* FMINNMP */
11551 case 0x7e: /* FMINP */
11552 if (size && !is_q) {
11553 unallocated_encoding(s);
11554 return;
11556 handle_simd_3same_pair(s, is_q, 0, fpopcode, size ? MO_64 : MO_32,
11557 rn, rm, rd);
11558 return;
11559 case 0x1b: /* FMULX */
11560 case 0x1f: /* FRECPS */
11561 case 0x3f: /* FRSQRTS */
11562 case 0x5d: /* FACGE */
11563 case 0x7d: /* FACGT */
11564 case 0x19: /* FMLA */
11565 case 0x39: /* FMLS */
11566 case 0x18: /* FMAXNM */
11567 case 0x1a: /* FADD */
11568 case 0x1c: /* FCMEQ */
11569 case 0x1e: /* FMAX */
11570 case 0x38: /* FMINNM */
11571 case 0x3a: /* FSUB */
11572 case 0x3e: /* FMIN */
11573 case 0x5b: /* FMUL */
11574 case 0x5c: /* FCMGE */
11575 case 0x5f: /* FDIV */
11576 case 0x7a: /* FABD */
11577 case 0x7c: /* FCMGT */
11578 if (!fp_access_check(s)) {
11579 return;
11581 handle_3same_float(s, size, elements, fpopcode, rd, rn, rm);
11582 return;
11584 case 0x1d: /* FMLAL */
11585 case 0x3d: /* FMLSL */
11586 case 0x59: /* FMLAL2 */
11587 case 0x79: /* FMLSL2 */
11588 if (size & 1 || !dc_isar_feature(aa64_fhm, s)) {
11589 unallocated_encoding(s);
11590 return;
11592 if (fp_access_check(s)) {
11593 int is_s = extract32(insn, 23, 1);
11594 int is_2 = extract32(insn, 29, 1);
11595 int data = (is_2 << 1) | is_s;
11596 tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
11597 vec_full_reg_offset(s, rn),
11598 vec_full_reg_offset(s, rm), cpu_env,
11599 is_q ? 16 : 8, vec_full_reg_size(s),
11600 data, gen_helper_gvec_fmlal_a64);
11602 return;
11604 default:
11605 unallocated_encoding(s);
11606 return;
11610 /* Integer op subgroup of C3.6.16. */
11611 static void disas_simd_3same_int(DisasContext *s, uint32_t insn)
11613 int is_q = extract32(insn, 30, 1);
11614 int u = extract32(insn, 29, 1);
11615 int size = extract32(insn, 22, 2);
11616 int opcode = extract32(insn, 11, 5);
11617 int rm = extract32(insn, 16, 5);
11618 int rn = extract32(insn, 5, 5);
11619 int rd = extract32(insn, 0, 5);
11620 int pass;
11621 TCGCond cond;
11623 switch (opcode) {
11624 case 0x13: /* MUL, PMUL */
11625 if (u && size != 0) {
11626 unallocated_encoding(s);
11627 return;
11629 /* fall through */
11630 case 0x0: /* SHADD, UHADD */
11631 case 0x2: /* SRHADD, URHADD */
11632 case 0x4: /* SHSUB, UHSUB */
11633 case 0xc: /* SMAX, UMAX */
11634 case 0xd: /* SMIN, UMIN */
11635 case 0xe: /* SABD, UABD */
11636 case 0xf: /* SABA, UABA */
11637 case 0x12: /* MLA, MLS */
11638 if (size == 3) {
11639 unallocated_encoding(s);
11640 return;
11642 break;
11643 case 0x16: /* SQDMULH, SQRDMULH */
11644 if (size == 0 || size == 3) {
11645 unallocated_encoding(s);
11646 return;
11648 break;
11649 default:
11650 if (size == 3 && !is_q) {
11651 unallocated_encoding(s);
11652 return;
11654 break;
11657 if (!fp_access_check(s)) {
11658 return;
11661 switch (opcode) {
11662 case 0x01: /* SQADD, UQADD */
11663 if (u) {
11664 gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uqadd_qc, size);
11665 } else {
11666 gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqadd_qc, size);
11668 return;
11669 case 0x05: /* SQSUB, UQSUB */
11670 if (u) {
11671 gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uqsub_qc, size);
11672 } else {
11673 gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqsub_qc, size);
11675 return;
11676 case 0x08: /* SSHL, USHL */
11677 if (u) {
11678 gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_ushl, size);
11679 } else {
11680 gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sshl, size);
11682 return;
11683 case 0x0c: /* SMAX, UMAX */
11684 if (u) {
11685 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_umax, size);
11686 } else {
11687 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_smax, size);
11689 return;
11690 case 0x0d: /* SMIN, UMIN */
11691 if (u) {
11692 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_umin, size);
11693 } else {
11694 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_smin, size);
11696 return;
11697 case 0xe: /* SABD, UABD */
11698 if (u) {
11699 gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uabd, size);
11700 } else {
11701 gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sabd, size);
11703 return;
11704 case 0xf: /* SABA, UABA */
11705 if (u) {
11706 gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uaba, size);
11707 } else {
11708 gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_saba, size);
11710 return;
11711 case 0x10: /* ADD, SUB */
11712 if (u) {
11713 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_sub, size);
11714 } else {
11715 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_add, size);
11717 return;
11718 case 0x13: /* MUL, PMUL */
11719 if (!u) { /* MUL */
11720 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_mul, size);
11721 } else { /* PMUL */
11722 gen_gvec_op3_ool(s, is_q, rd, rn, rm, 0, gen_helper_gvec_pmul_b);
11724 return;
11725 case 0x12: /* MLA, MLS */
11726 if (u) {
11727 gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_mls, size);
11728 } else {
11729 gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_mla, size);
11731 return;
11732 case 0x16: /* SQDMULH, SQRDMULH */
11734 static gen_helper_gvec_3_ptr * const fns[2][2] = {
11735 { gen_helper_neon_sqdmulh_h, gen_helper_neon_sqrdmulh_h },
11736 { gen_helper_neon_sqdmulh_s, gen_helper_neon_sqrdmulh_s },
11738 gen_gvec_op3_qc(s, is_q, rd, rn, rm, fns[size - 1][u]);
11740 return;
11741 case 0x11:
11742 if (!u) { /* CMTST */
11743 gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_cmtst, size);
11744 return;
11746 /* else CMEQ */
11747 cond = TCG_COND_EQ;
11748 goto do_gvec_cmp;
11749 case 0x06: /* CMGT, CMHI */
11750 cond = u ? TCG_COND_GTU : TCG_COND_GT;
11751 goto do_gvec_cmp;
11752 case 0x07: /* CMGE, CMHS */
11753 cond = u ? TCG_COND_GEU : TCG_COND_GE;
11754 do_gvec_cmp:
11755 tcg_gen_gvec_cmp(cond, size, vec_full_reg_offset(s, rd),
11756 vec_full_reg_offset(s, rn),
11757 vec_full_reg_offset(s, rm),
11758 is_q ? 16 : 8, vec_full_reg_size(s));
11759 return;
11762 if (size == 3) {
11763 assert(is_q);
11764 for (pass = 0; pass < 2; pass++) {
11765 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
11766 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
11767 TCGv_i64 tcg_res = tcg_temp_new_i64();
11769 read_vec_element(s, tcg_op1, rn, pass, MO_64);
11770 read_vec_element(s, tcg_op2, rm, pass, MO_64);
11772 handle_3same_64(s, opcode, u, tcg_res, tcg_op1, tcg_op2);
11774 write_vec_element(s, tcg_res, rd, pass, MO_64);
11776 tcg_temp_free_i64(tcg_res);
11777 tcg_temp_free_i64(tcg_op1);
11778 tcg_temp_free_i64(tcg_op2);
11780 } else {
11781 for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
11782 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11783 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11784 TCGv_i32 tcg_res = tcg_temp_new_i32();
11785 NeonGenTwoOpFn *genfn = NULL;
11786 NeonGenTwoOpEnvFn *genenvfn = NULL;
11788 read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
11789 read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
11791 switch (opcode) {
11792 case 0x0: /* SHADD, UHADD */
11794 static NeonGenTwoOpFn * const fns[3][2] = {
11795 { gen_helper_neon_hadd_s8, gen_helper_neon_hadd_u8 },
11796 { gen_helper_neon_hadd_s16, gen_helper_neon_hadd_u16 },
11797 { gen_helper_neon_hadd_s32, gen_helper_neon_hadd_u32 },
11799 genfn = fns[size][u];
11800 break;
11802 case 0x2: /* SRHADD, URHADD */
11804 static NeonGenTwoOpFn * const fns[3][2] = {
11805 { gen_helper_neon_rhadd_s8, gen_helper_neon_rhadd_u8 },
11806 { gen_helper_neon_rhadd_s16, gen_helper_neon_rhadd_u16 },
11807 { gen_helper_neon_rhadd_s32, gen_helper_neon_rhadd_u32 },
11809 genfn = fns[size][u];
11810 break;
11812 case 0x4: /* SHSUB, UHSUB */
11814 static NeonGenTwoOpFn * const fns[3][2] = {
11815 { gen_helper_neon_hsub_s8, gen_helper_neon_hsub_u8 },
11816 { gen_helper_neon_hsub_s16, gen_helper_neon_hsub_u16 },
11817 { gen_helper_neon_hsub_s32, gen_helper_neon_hsub_u32 },
11819 genfn = fns[size][u];
11820 break;
11822 case 0x9: /* SQSHL, UQSHL */
11824 static NeonGenTwoOpEnvFn * const fns[3][2] = {
11825 { gen_helper_neon_qshl_s8, gen_helper_neon_qshl_u8 },
11826 { gen_helper_neon_qshl_s16, gen_helper_neon_qshl_u16 },
11827 { gen_helper_neon_qshl_s32, gen_helper_neon_qshl_u32 },
11829 genenvfn = fns[size][u];
11830 break;
11832 case 0xa: /* SRSHL, URSHL */
11834 static NeonGenTwoOpFn * const fns[3][2] = {
11835 { gen_helper_neon_rshl_s8, gen_helper_neon_rshl_u8 },
11836 { gen_helper_neon_rshl_s16, gen_helper_neon_rshl_u16 },
11837 { gen_helper_neon_rshl_s32, gen_helper_neon_rshl_u32 },
11839 genfn = fns[size][u];
11840 break;
11842 case 0xb: /* SQRSHL, UQRSHL */
11844 static NeonGenTwoOpEnvFn * const fns[3][2] = {
11845 { gen_helper_neon_qrshl_s8, gen_helper_neon_qrshl_u8 },
11846 { gen_helper_neon_qrshl_s16, gen_helper_neon_qrshl_u16 },
11847 { gen_helper_neon_qrshl_s32, gen_helper_neon_qrshl_u32 },
11849 genenvfn = fns[size][u];
11850 break;
11852 default:
11853 g_assert_not_reached();
11856 if (genenvfn) {
11857 genenvfn(tcg_res, cpu_env, tcg_op1, tcg_op2);
11858 } else {
11859 genfn(tcg_res, tcg_op1, tcg_op2);
11862 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
11864 tcg_temp_free_i32(tcg_res);
11865 tcg_temp_free_i32(tcg_op1);
11866 tcg_temp_free_i32(tcg_op2);
11869 clear_vec_high(s, is_q, rd);
11872 /* AdvSIMD three same
11873 * 31 30 29 28 24 23 22 21 20 16 15 11 10 9 5 4 0
11874 * +---+---+---+-----------+------+---+------+--------+---+------+------+
11875 * | 0 | Q | U | 0 1 1 1 0 | size | 1 | Rm | opcode | 1 | Rn | Rd |
11876 * +---+---+---+-----------+------+---+------+--------+---+------+------+
11878 static void disas_simd_three_reg_same(DisasContext *s, uint32_t insn)
11880 int opcode = extract32(insn, 11, 5);
11882 switch (opcode) {
11883 case 0x3: /* logic ops */
11884 disas_simd_3same_logic(s, insn);
11885 break;
11886 case 0x17: /* ADDP */
11887 case 0x14: /* SMAXP, UMAXP */
11888 case 0x15: /* SMINP, UMINP */
11890 /* Pairwise operations */
11891 int is_q = extract32(insn, 30, 1);
11892 int u = extract32(insn, 29, 1);
11893 int size = extract32(insn, 22, 2);
11894 int rm = extract32(insn, 16, 5);
11895 int rn = extract32(insn, 5, 5);
11896 int rd = extract32(insn, 0, 5);
11897 if (opcode == 0x17) {
11898 if (u || (size == 3 && !is_q)) {
11899 unallocated_encoding(s);
11900 return;
11902 } else {
11903 if (size == 3) {
11904 unallocated_encoding(s);
11905 return;
11908 handle_simd_3same_pair(s, is_q, u, opcode, size, rn, rm, rd);
11909 break;
11911 case 0x18 ... 0x31:
11912 /* floating point ops, sz[1] and U are part of opcode */
11913 disas_simd_3same_float(s, insn);
11914 break;
11915 default:
11916 disas_simd_3same_int(s, insn);
11917 break;
11922 * Advanced SIMD three same (ARMv8.2 FP16 variants)
11924 * 31 30 29 28 24 23 22 21 20 16 15 14 13 11 10 9 5 4 0
11925 * +---+---+---+-----------+---------+------+-----+--------+---+------+------+
11926 * | 0 | Q | U | 0 1 1 1 0 | a | 1 0 | Rm | 0 0 | opcode | 1 | Rn | Rd |
11927 * +---+---+---+-----------+---------+------+-----+--------+---+------+------+
11929 * This includes FMULX, FCMEQ (register), FRECPS, FRSQRTS, FCMGE
11930 * (register), FACGE, FABD, FCMGT (register) and FACGT.
11933 static void disas_simd_three_reg_same_fp16(DisasContext *s, uint32_t insn)
11935 int opcode = extract32(insn, 11, 3);
11936 int u = extract32(insn, 29, 1);
11937 int a = extract32(insn, 23, 1);
11938 int is_q = extract32(insn, 30, 1);
11939 int rm = extract32(insn, 16, 5);
11940 int rn = extract32(insn, 5, 5);
11941 int rd = extract32(insn, 0, 5);
11943 * For these floating point ops, the U, a and opcode bits
11944 * together indicate the operation.
11946 int fpopcode = opcode | (a << 3) | (u << 4);
11947 int datasize = is_q ? 128 : 64;
11948 int elements = datasize / 16;
11949 bool pairwise;
11950 TCGv_ptr fpst;
11951 int pass;
11953 switch (fpopcode) {
11954 case 0x0: /* FMAXNM */
11955 case 0x1: /* FMLA */
11956 case 0x2: /* FADD */
11957 case 0x3: /* FMULX */
11958 case 0x4: /* FCMEQ */
11959 case 0x6: /* FMAX */
11960 case 0x7: /* FRECPS */
11961 case 0x8: /* FMINNM */
11962 case 0x9: /* FMLS */
11963 case 0xa: /* FSUB */
11964 case 0xe: /* FMIN */
11965 case 0xf: /* FRSQRTS */
11966 case 0x13: /* FMUL */
11967 case 0x14: /* FCMGE */
11968 case 0x15: /* FACGE */
11969 case 0x17: /* FDIV */
11970 case 0x1a: /* FABD */
11971 case 0x1c: /* FCMGT */
11972 case 0x1d: /* FACGT */
11973 pairwise = false;
11974 break;
11975 case 0x10: /* FMAXNMP */
11976 case 0x12: /* FADDP */
11977 case 0x16: /* FMAXP */
11978 case 0x18: /* FMINNMP */
11979 case 0x1e: /* FMINP */
11980 pairwise = true;
11981 break;
11982 default:
11983 unallocated_encoding(s);
11984 return;
11987 if (!dc_isar_feature(aa64_fp16, s)) {
11988 unallocated_encoding(s);
11989 return;
11992 if (!fp_access_check(s)) {
11993 return;
11996 fpst = fpstatus_ptr(FPST_FPCR_F16);
11998 if (pairwise) {
11999 int maxpass = is_q ? 8 : 4;
12000 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
12001 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
12002 TCGv_i32 tcg_res[8];
12004 for (pass = 0; pass < maxpass; pass++) {
12005 int passreg = pass < (maxpass / 2) ? rn : rm;
12006 int passelt = (pass << 1) & (maxpass - 1);
12008 read_vec_element_i32(s, tcg_op1, passreg, passelt, MO_16);
12009 read_vec_element_i32(s, tcg_op2, passreg, passelt + 1, MO_16);
12010 tcg_res[pass] = tcg_temp_new_i32();
12012 switch (fpopcode) {
12013 case 0x10: /* FMAXNMP */
12014 gen_helper_advsimd_maxnumh(tcg_res[pass], tcg_op1, tcg_op2,
12015 fpst);
12016 break;
12017 case 0x12: /* FADDP */
12018 gen_helper_advsimd_addh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
12019 break;
12020 case 0x16: /* FMAXP */
12021 gen_helper_advsimd_maxh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
12022 break;
12023 case 0x18: /* FMINNMP */
12024 gen_helper_advsimd_minnumh(tcg_res[pass], tcg_op1, tcg_op2,
12025 fpst);
12026 break;
12027 case 0x1e: /* FMINP */
12028 gen_helper_advsimd_minh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
12029 break;
12030 default:
12031 g_assert_not_reached();
12035 for (pass = 0; pass < maxpass; pass++) {
12036 write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_16);
12037 tcg_temp_free_i32(tcg_res[pass]);
12040 tcg_temp_free_i32(tcg_op1);
12041 tcg_temp_free_i32(tcg_op2);
12043 } else {
12044 for (pass = 0; pass < elements; pass++) {
12045 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
12046 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
12047 TCGv_i32 tcg_res = tcg_temp_new_i32();
12049 read_vec_element_i32(s, tcg_op1, rn, pass, MO_16);
12050 read_vec_element_i32(s, tcg_op2, rm, pass, MO_16);
12052 switch (fpopcode) {
12053 case 0x0: /* FMAXNM */
12054 gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
12055 break;
12056 case 0x1: /* FMLA */
12057 read_vec_element_i32(s, tcg_res, rd, pass, MO_16);
12058 gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_res,
12059 fpst);
12060 break;
12061 case 0x2: /* FADD */
12062 gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
12063 break;
12064 case 0x3: /* FMULX */
12065 gen_helper_advsimd_mulxh(tcg_res, tcg_op1, tcg_op2, fpst);
12066 break;
12067 case 0x4: /* FCMEQ */
12068 gen_helper_advsimd_ceq_f16(tcg_res, tcg_op1, tcg_op2, fpst);
12069 break;
12070 case 0x6: /* FMAX */
12071 gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
12072 break;
12073 case 0x7: /* FRECPS */
12074 gen_helper_recpsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
12075 break;
12076 case 0x8: /* FMINNM */
12077 gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
12078 break;
12079 case 0x9: /* FMLS */
12080 /* As usual for ARM, separate negation for fused multiply-add */
12081 tcg_gen_xori_i32(tcg_op1, tcg_op1, 0x8000);
12082 read_vec_element_i32(s, tcg_res, rd, pass, MO_16);
12083 gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_res,
12084 fpst);
12085 break;
12086 case 0xa: /* FSUB */
12087 gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
12088 break;
12089 case 0xe: /* FMIN */
12090 gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
12091 break;
12092 case 0xf: /* FRSQRTS */
12093 gen_helper_rsqrtsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
12094 break;
12095 case 0x13: /* FMUL */
12096 gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
12097 break;
12098 case 0x14: /* FCMGE */
12099 gen_helper_advsimd_cge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
12100 break;
12101 case 0x15: /* FACGE */
12102 gen_helper_advsimd_acge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
12103 break;
12104 case 0x17: /* FDIV */
12105 gen_helper_advsimd_divh(tcg_res, tcg_op1, tcg_op2, fpst);
12106 break;
12107 case 0x1a: /* FABD */
12108 gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
12109 tcg_gen_andi_i32(tcg_res, tcg_res, 0x7fff);
12110 break;
12111 case 0x1c: /* FCMGT */
12112 gen_helper_advsimd_cgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
12113 break;
12114 case 0x1d: /* FACGT */
12115 gen_helper_advsimd_acgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
12116 break;
12117 default:
12118 g_assert_not_reached();
12121 write_vec_element_i32(s, tcg_res, rd, pass, MO_16);
12122 tcg_temp_free_i32(tcg_res);
12123 tcg_temp_free_i32(tcg_op1);
12124 tcg_temp_free_i32(tcg_op2);
12128 tcg_temp_free_ptr(fpst);
12130 clear_vec_high(s, is_q, rd);
12133 /* AdvSIMD three same extra
12134 * 31 30 29 28 24 23 22 21 20 16 15 14 11 10 9 5 4 0
12135 * +---+---+---+-----------+------+---+------+---+--------+---+----+----+
12136 * | 0 | Q | U | 0 1 1 1 0 | size | 0 | Rm | 1 | opcode | 1 | Rn | Rd |
12137 * +---+---+---+-----------+------+---+------+---+--------+---+----+----+
12139 static void disas_simd_three_reg_same_extra(DisasContext *s, uint32_t insn)
12141 int rd = extract32(insn, 0, 5);
12142 int rn = extract32(insn, 5, 5);
12143 int opcode = extract32(insn, 11, 4);
12144 int rm = extract32(insn, 16, 5);
12145 int size = extract32(insn, 22, 2);
12146 bool u = extract32(insn, 29, 1);
12147 bool is_q = extract32(insn, 30, 1);
12148 bool feature;
12149 int rot;
12151 switch (u * 16 + opcode) {
12152 case 0x10: /* SQRDMLAH (vector) */
12153 case 0x11: /* SQRDMLSH (vector) */
12154 if (size != 1 && size != 2) {
12155 unallocated_encoding(s);
12156 return;
12158 feature = dc_isar_feature(aa64_rdm, s);
12159 break;
12160 case 0x02: /* SDOT (vector) */
12161 case 0x12: /* UDOT (vector) */
12162 if (size != MO_32) {
12163 unallocated_encoding(s);
12164 return;
12166 feature = dc_isar_feature(aa64_dp, s);
12167 break;
12168 case 0x03: /* USDOT */
12169 if (size != MO_32) {
12170 unallocated_encoding(s);
12171 return;
12173 feature = dc_isar_feature(aa64_i8mm, s);
12174 break;
12175 case 0x04: /* SMMLA */
12176 case 0x14: /* UMMLA */
12177 case 0x05: /* USMMLA */
12178 if (!is_q || size != MO_32) {
12179 unallocated_encoding(s);
12180 return;
12182 feature = dc_isar_feature(aa64_i8mm, s);
12183 break;
12184 case 0x18: /* FCMLA, #0 */
12185 case 0x19: /* FCMLA, #90 */
12186 case 0x1a: /* FCMLA, #180 */
12187 case 0x1b: /* FCMLA, #270 */
12188 case 0x1c: /* FCADD, #90 */
12189 case 0x1e: /* FCADD, #270 */
12190 if (size == 0
12191 || (size == 1 && !dc_isar_feature(aa64_fp16, s))
12192 || (size == 3 && !is_q)) {
12193 unallocated_encoding(s);
12194 return;
12196 feature = dc_isar_feature(aa64_fcma, s);
12197 break;
12198 case 0x1d: /* BFMMLA */
12199 if (size != MO_16 || !is_q) {
12200 unallocated_encoding(s);
12201 return;
12203 feature = dc_isar_feature(aa64_bf16, s);
12204 break;
12205 case 0x1f:
12206 switch (size) {
12207 case 1: /* BFDOT */
12208 case 3: /* BFMLAL{B,T} */
12209 feature = dc_isar_feature(aa64_bf16, s);
12210 break;
12211 default:
12212 unallocated_encoding(s);
12213 return;
12215 break;
12216 default:
12217 unallocated_encoding(s);
12218 return;
12220 if (!feature) {
12221 unallocated_encoding(s);
12222 return;
12224 if (!fp_access_check(s)) {
12225 return;
12228 switch (opcode) {
12229 case 0x0: /* SQRDMLAH (vector) */
12230 gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqrdmlah_qc, size);
12231 return;
12233 case 0x1: /* SQRDMLSH (vector) */
12234 gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqrdmlsh_qc, size);
12235 return;
12237 case 0x2: /* SDOT / UDOT */
12238 gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0,
12239 u ? gen_helper_gvec_udot_b : gen_helper_gvec_sdot_b);
12240 return;
12242 case 0x3: /* USDOT */
12243 gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0, gen_helper_gvec_usdot_b);
12244 return;
12246 case 0x04: /* SMMLA, UMMLA */
12247 gen_gvec_op4_ool(s, 1, rd, rn, rm, rd, 0,
12248 u ? gen_helper_gvec_ummla_b
12249 : gen_helper_gvec_smmla_b);
12250 return;
12251 case 0x05: /* USMMLA */
12252 gen_gvec_op4_ool(s, 1, rd, rn, rm, rd, 0, gen_helper_gvec_usmmla_b);
12253 return;
12255 case 0x8: /* FCMLA, #0 */
12256 case 0x9: /* FCMLA, #90 */
12257 case 0xa: /* FCMLA, #180 */
12258 case 0xb: /* FCMLA, #270 */
12259 rot = extract32(opcode, 0, 2);
12260 switch (size) {
12261 case 1:
12262 gen_gvec_op4_fpst(s, is_q, rd, rn, rm, rd, true, rot,
12263 gen_helper_gvec_fcmlah);
12264 break;
12265 case 2:
12266 gen_gvec_op4_fpst(s, is_q, rd, rn, rm, rd, false, rot,
12267 gen_helper_gvec_fcmlas);
12268 break;
12269 case 3:
12270 gen_gvec_op4_fpst(s, is_q, rd, rn, rm, rd, false, rot,
12271 gen_helper_gvec_fcmlad);
12272 break;
12273 default:
12274 g_assert_not_reached();
12276 return;
12278 case 0xc: /* FCADD, #90 */
12279 case 0xe: /* FCADD, #270 */
12280 rot = extract32(opcode, 1, 1);
12281 switch (size) {
12282 case 1:
12283 gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
12284 gen_helper_gvec_fcaddh);
12285 break;
12286 case 2:
12287 gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
12288 gen_helper_gvec_fcadds);
12289 break;
12290 case 3:
12291 gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
12292 gen_helper_gvec_fcaddd);
12293 break;
12294 default:
12295 g_assert_not_reached();
12297 return;
12299 case 0xd: /* BFMMLA */
12300 gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0, gen_helper_gvec_bfmmla);
12301 return;
12302 case 0xf:
12303 switch (size) {
12304 case 1: /* BFDOT */
12305 gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0, gen_helper_gvec_bfdot);
12306 break;
12307 case 3: /* BFMLAL{B,T} */
12308 gen_gvec_op4_fpst(s, 1, rd, rn, rm, rd, false, is_q,
12309 gen_helper_gvec_bfmlal);
12310 break;
12311 default:
12312 g_assert_not_reached();
12314 return;
12316 default:
12317 g_assert_not_reached();
12321 static void handle_2misc_widening(DisasContext *s, int opcode, bool is_q,
12322 int size, int rn, int rd)
12324 /* Handle 2-reg-misc ops which are widening (so each size element
12325 * in the source becomes a 2*size element in the destination.
12326 * The only instruction like this is FCVTL.
12328 int pass;
12330 if (size == 3) {
12331 /* 32 -> 64 bit fp conversion */
12332 TCGv_i64 tcg_res[2];
12333 int srcelt = is_q ? 2 : 0;
12335 for (pass = 0; pass < 2; pass++) {
12336 TCGv_i32 tcg_op = tcg_temp_new_i32();
12337 tcg_res[pass] = tcg_temp_new_i64();
12339 read_vec_element_i32(s, tcg_op, rn, srcelt + pass, MO_32);
12340 gen_helper_vfp_fcvtds(tcg_res[pass], tcg_op, cpu_env);
12341 tcg_temp_free_i32(tcg_op);
12343 for (pass = 0; pass < 2; pass++) {
12344 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
12345 tcg_temp_free_i64(tcg_res[pass]);
12347 } else {
12348 /* 16 -> 32 bit fp conversion */
12349 int srcelt = is_q ? 4 : 0;
12350 TCGv_i32 tcg_res[4];
12351 TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
12352 TCGv_i32 ahp = get_ahp_flag();
12354 for (pass = 0; pass < 4; pass++) {
12355 tcg_res[pass] = tcg_temp_new_i32();
12357 read_vec_element_i32(s, tcg_res[pass], rn, srcelt + pass, MO_16);
12358 gen_helper_vfp_fcvt_f16_to_f32(tcg_res[pass], tcg_res[pass],
12359 fpst, ahp);
12361 for (pass = 0; pass < 4; pass++) {
12362 write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_32);
12363 tcg_temp_free_i32(tcg_res[pass]);
12366 tcg_temp_free_ptr(fpst);
12367 tcg_temp_free_i32(ahp);
12371 static void handle_rev(DisasContext *s, int opcode, bool u,
12372 bool is_q, int size, int rn, int rd)
12374 int op = (opcode << 1) | u;
12375 int opsz = op + size;
12376 int grp_size = 3 - opsz;
12377 int dsize = is_q ? 128 : 64;
12378 int i;
12380 if (opsz >= 3) {
12381 unallocated_encoding(s);
12382 return;
12385 if (!fp_access_check(s)) {
12386 return;
12389 if (size == 0) {
12390 /* Special case bytes, use bswap op on each group of elements */
12391 int groups = dsize / (8 << grp_size);
12393 for (i = 0; i < groups; i++) {
12394 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
12396 read_vec_element(s, tcg_tmp, rn, i, grp_size);
12397 switch (grp_size) {
12398 case MO_16:
12399 tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp, TCG_BSWAP_IZ);
12400 break;
12401 case MO_32:
12402 tcg_gen_bswap32_i64(tcg_tmp, tcg_tmp, TCG_BSWAP_IZ);
12403 break;
12404 case MO_64:
12405 tcg_gen_bswap64_i64(tcg_tmp, tcg_tmp);
12406 break;
12407 default:
12408 g_assert_not_reached();
12410 write_vec_element(s, tcg_tmp, rd, i, grp_size);
12411 tcg_temp_free_i64(tcg_tmp);
12413 clear_vec_high(s, is_q, rd);
12414 } else {
12415 int revmask = (1 << grp_size) - 1;
12416 int esize = 8 << size;
12417 int elements = dsize / esize;
12418 TCGv_i64 tcg_rn = tcg_temp_new_i64();
12419 TCGv_i64 tcg_rd = tcg_const_i64(0);
12420 TCGv_i64 tcg_rd_hi = tcg_const_i64(0);
12422 for (i = 0; i < elements; i++) {
12423 int e_rev = (i & 0xf) ^ revmask;
12424 int off = e_rev * esize;
12425 read_vec_element(s, tcg_rn, rn, i, size);
12426 if (off >= 64) {
12427 tcg_gen_deposit_i64(tcg_rd_hi, tcg_rd_hi,
12428 tcg_rn, off - 64, esize);
12429 } else {
12430 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, off, esize);
12433 write_vec_element(s, tcg_rd, rd, 0, MO_64);
12434 write_vec_element(s, tcg_rd_hi, rd, 1, MO_64);
12436 tcg_temp_free_i64(tcg_rd_hi);
12437 tcg_temp_free_i64(tcg_rd);
12438 tcg_temp_free_i64(tcg_rn);
12442 static void handle_2misc_pairwise(DisasContext *s, int opcode, bool u,
12443 bool is_q, int size, int rn, int rd)
12445 /* Implement the pairwise operations from 2-misc:
12446 * SADDLP, UADDLP, SADALP, UADALP.
12447 * These all add pairs of elements in the input to produce a
12448 * double-width result element in the output (possibly accumulating).
12450 bool accum = (opcode == 0x6);
12451 int maxpass = is_q ? 2 : 1;
12452 int pass;
12453 TCGv_i64 tcg_res[2];
12455 if (size == 2) {
12456 /* 32 + 32 -> 64 op */
12457 MemOp memop = size + (u ? 0 : MO_SIGN);
12459 for (pass = 0; pass < maxpass; pass++) {
12460 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
12461 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
12463 tcg_res[pass] = tcg_temp_new_i64();
12465 read_vec_element(s, tcg_op1, rn, pass * 2, memop);
12466 read_vec_element(s, tcg_op2, rn, pass * 2 + 1, memop);
12467 tcg_gen_add_i64(tcg_res[pass], tcg_op1, tcg_op2);
12468 if (accum) {
12469 read_vec_element(s, tcg_op1, rd, pass, MO_64);
12470 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
12473 tcg_temp_free_i64(tcg_op1);
12474 tcg_temp_free_i64(tcg_op2);
12476 } else {
12477 for (pass = 0; pass < maxpass; pass++) {
12478 TCGv_i64 tcg_op = tcg_temp_new_i64();
12479 NeonGenOne64OpFn *genfn;
12480 static NeonGenOne64OpFn * const fns[2][2] = {
12481 { gen_helper_neon_addlp_s8, gen_helper_neon_addlp_u8 },
12482 { gen_helper_neon_addlp_s16, gen_helper_neon_addlp_u16 },
12485 genfn = fns[size][u];
12487 tcg_res[pass] = tcg_temp_new_i64();
12489 read_vec_element(s, tcg_op, rn, pass, MO_64);
12490 genfn(tcg_res[pass], tcg_op);
12492 if (accum) {
12493 read_vec_element(s, tcg_op, rd, pass, MO_64);
12494 if (size == 0) {
12495 gen_helper_neon_addl_u16(tcg_res[pass],
12496 tcg_res[pass], tcg_op);
12497 } else {
12498 gen_helper_neon_addl_u32(tcg_res[pass],
12499 tcg_res[pass], tcg_op);
12502 tcg_temp_free_i64(tcg_op);
12505 if (!is_q) {
12506 tcg_res[1] = tcg_constant_i64(0);
12508 for (pass = 0; pass < 2; pass++) {
12509 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
12510 tcg_temp_free_i64(tcg_res[pass]);
12514 static void handle_shll(DisasContext *s, bool is_q, int size, int rn, int rd)
12516 /* Implement SHLL and SHLL2 */
12517 int pass;
12518 int part = is_q ? 2 : 0;
12519 TCGv_i64 tcg_res[2];
12521 for (pass = 0; pass < 2; pass++) {
12522 static NeonGenWidenFn * const widenfns[3] = {
12523 gen_helper_neon_widen_u8,
12524 gen_helper_neon_widen_u16,
12525 tcg_gen_extu_i32_i64,
12527 NeonGenWidenFn *widenfn = widenfns[size];
12528 TCGv_i32 tcg_op = tcg_temp_new_i32();
12530 read_vec_element_i32(s, tcg_op, rn, part + pass, MO_32);
12531 tcg_res[pass] = tcg_temp_new_i64();
12532 widenfn(tcg_res[pass], tcg_op);
12533 tcg_gen_shli_i64(tcg_res[pass], tcg_res[pass], 8 << size);
12535 tcg_temp_free_i32(tcg_op);
12538 for (pass = 0; pass < 2; pass++) {
12539 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
12540 tcg_temp_free_i64(tcg_res[pass]);
12544 /* AdvSIMD two reg misc
12545 * 31 30 29 28 24 23 22 21 17 16 12 11 10 9 5 4 0
12546 * +---+---+---+-----------+------+-----------+--------+-----+------+------+
12547 * | 0 | Q | U | 0 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 | Rn | Rd |
12548 * +---+---+---+-----------+------+-----------+--------+-----+------+------+
12550 static void disas_simd_two_reg_misc(DisasContext *s, uint32_t insn)
12552 int size = extract32(insn, 22, 2);
12553 int opcode = extract32(insn, 12, 5);
12554 bool u = extract32(insn, 29, 1);
12555 bool is_q = extract32(insn, 30, 1);
12556 int rn = extract32(insn, 5, 5);
12557 int rd = extract32(insn, 0, 5);
12558 bool need_fpstatus = false;
12559 bool need_rmode = false;
12560 int rmode = -1;
12561 TCGv_i32 tcg_rmode;
12562 TCGv_ptr tcg_fpstatus;
12564 switch (opcode) {
12565 case 0x0: /* REV64, REV32 */
12566 case 0x1: /* REV16 */
12567 handle_rev(s, opcode, u, is_q, size, rn, rd);
12568 return;
12569 case 0x5: /* CNT, NOT, RBIT */
12570 if (u && size == 0) {
12571 /* NOT */
12572 break;
12573 } else if (u && size == 1) {
12574 /* RBIT */
12575 break;
12576 } else if (!u && size == 0) {
12577 /* CNT */
12578 break;
12580 unallocated_encoding(s);
12581 return;
12582 case 0x12: /* XTN, XTN2, SQXTUN, SQXTUN2 */
12583 case 0x14: /* SQXTN, SQXTN2, UQXTN, UQXTN2 */
12584 if (size == 3) {
12585 unallocated_encoding(s);
12586 return;
12588 if (!fp_access_check(s)) {
12589 return;
12592 handle_2misc_narrow(s, false, opcode, u, is_q, size, rn, rd);
12593 return;
12594 case 0x4: /* CLS, CLZ */
12595 if (size == 3) {
12596 unallocated_encoding(s);
12597 return;
12599 break;
12600 case 0x2: /* SADDLP, UADDLP */
12601 case 0x6: /* SADALP, UADALP */
12602 if (size == 3) {
12603 unallocated_encoding(s);
12604 return;
12606 if (!fp_access_check(s)) {
12607 return;
12609 handle_2misc_pairwise(s, opcode, u, is_q, size, rn, rd);
12610 return;
12611 case 0x13: /* SHLL, SHLL2 */
12612 if (u == 0 || size == 3) {
12613 unallocated_encoding(s);
12614 return;
12616 if (!fp_access_check(s)) {
12617 return;
12619 handle_shll(s, is_q, size, rn, rd);
12620 return;
12621 case 0xa: /* CMLT */
12622 if (u == 1) {
12623 unallocated_encoding(s);
12624 return;
12626 /* fall through */
12627 case 0x8: /* CMGT, CMGE */
12628 case 0x9: /* CMEQ, CMLE */
12629 case 0xb: /* ABS, NEG */
12630 if (size == 3 && !is_q) {
12631 unallocated_encoding(s);
12632 return;
12634 break;
12635 case 0x3: /* SUQADD, USQADD */
12636 if (size == 3 && !is_q) {
12637 unallocated_encoding(s);
12638 return;
12640 if (!fp_access_check(s)) {
12641 return;
12643 handle_2misc_satacc(s, false, u, is_q, size, rn, rd);
12644 return;
12645 case 0x7: /* SQABS, SQNEG */
12646 if (size == 3 && !is_q) {
12647 unallocated_encoding(s);
12648 return;
12650 break;
12651 case 0xc ... 0xf:
12652 case 0x16 ... 0x1f:
12654 /* Floating point: U, size[1] and opcode indicate operation;
12655 * size[0] indicates single or double precision.
12657 int is_double = extract32(size, 0, 1);
12658 opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
12659 size = is_double ? 3 : 2;
12660 switch (opcode) {
12661 case 0x2f: /* FABS */
12662 case 0x6f: /* FNEG */
12663 if (size == 3 && !is_q) {
12664 unallocated_encoding(s);
12665 return;
12667 break;
12668 case 0x1d: /* SCVTF */
12669 case 0x5d: /* UCVTF */
12671 bool is_signed = (opcode == 0x1d) ? true : false;
12672 int elements = is_double ? 2 : is_q ? 4 : 2;
12673 if (is_double && !is_q) {
12674 unallocated_encoding(s);
12675 return;
12677 if (!fp_access_check(s)) {
12678 return;
12680 handle_simd_intfp_conv(s, rd, rn, elements, is_signed, 0, size);
12681 return;
12683 case 0x2c: /* FCMGT (zero) */
12684 case 0x2d: /* FCMEQ (zero) */
12685 case 0x2e: /* FCMLT (zero) */
12686 case 0x6c: /* FCMGE (zero) */
12687 case 0x6d: /* FCMLE (zero) */
12688 if (size == 3 && !is_q) {
12689 unallocated_encoding(s);
12690 return;
12692 handle_2misc_fcmp_zero(s, opcode, false, u, is_q, size, rn, rd);
12693 return;
12694 case 0x7f: /* FSQRT */
12695 if (size == 3 && !is_q) {
12696 unallocated_encoding(s);
12697 return;
12699 break;
12700 case 0x1a: /* FCVTNS */
12701 case 0x1b: /* FCVTMS */
12702 case 0x3a: /* FCVTPS */
12703 case 0x3b: /* FCVTZS */
12704 case 0x5a: /* FCVTNU */
12705 case 0x5b: /* FCVTMU */
12706 case 0x7a: /* FCVTPU */
12707 case 0x7b: /* FCVTZU */
12708 need_fpstatus = true;
12709 need_rmode = true;
12710 rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
12711 if (size == 3 && !is_q) {
12712 unallocated_encoding(s);
12713 return;
12715 break;
12716 case 0x5c: /* FCVTAU */
12717 case 0x1c: /* FCVTAS */
12718 need_fpstatus = true;
12719 need_rmode = true;
12720 rmode = FPROUNDING_TIEAWAY;
12721 if (size == 3 && !is_q) {
12722 unallocated_encoding(s);
12723 return;
12725 break;
12726 case 0x3c: /* URECPE */
12727 if (size == 3) {
12728 unallocated_encoding(s);
12729 return;
12731 /* fall through */
12732 case 0x3d: /* FRECPE */
12733 case 0x7d: /* FRSQRTE */
12734 if (size == 3 && !is_q) {
12735 unallocated_encoding(s);
12736 return;
12738 if (!fp_access_check(s)) {
12739 return;
12741 handle_2misc_reciprocal(s, opcode, false, u, is_q, size, rn, rd);
12742 return;
12743 case 0x56: /* FCVTXN, FCVTXN2 */
12744 if (size == 2) {
12745 unallocated_encoding(s);
12746 return;
12748 /* fall through */
12749 case 0x16: /* FCVTN, FCVTN2 */
12750 /* handle_2misc_narrow does a 2*size -> size operation, but these
12751 * instructions encode the source size rather than dest size.
12753 if (!fp_access_check(s)) {
12754 return;
12756 handle_2misc_narrow(s, false, opcode, 0, is_q, size - 1, rn, rd);
12757 return;
12758 case 0x36: /* BFCVTN, BFCVTN2 */
12759 if (!dc_isar_feature(aa64_bf16, s) || size != 2) {
12760 unallocated_encoding(s);
12761 return;
12763 if (!fp_access_check(s)) {
12764 return;
12766 handle_2misc_narrow(s, false, opcode, 0, is_q, size - 1, rn, rd);
12767 return;
12768 case 0x17: /* FCVTL, FCVTL2 */
12769 if (!fp_access_check(s)) {
12770 return;
12772 handle_2misc_widening(s, opcode, is_q, size, rn, rd);
12773 return;
12774 case 0x18: /* FRINTN */
12775 case 0x19: /* FRINTM */
12776 case 0x38: /* FRINTP */
12777 case 0x39: /* FRINTZ */
12778 need_rmode = true;
12779 rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
12780 /* fall through */
12781 case 0x59: /* FRINTX */
12782 case 0x79: /* FRINTI */
12783 need_fpstatus = true;
12784 if (size == 3 && !is_q) {
12785 unallocated_encoding(s);
12786 return;
12788 break;
12789 case 0x58: /* FRINTA */
12790 need_rmode = true;
12791 rmode = FPROUNDING_TIEAWAY;
12792 need_fpstatus = true;
12793 if (size == 3 && !is_q) {
12794 unallocated_encoding(s);
12795 return;
12797 break;
12798 case 0x7c: /* URSQRTE */
12799 if (size == 3) {
12800 unallocated_encoding(s);
12801 return;
12803 break;
12804 case 0x1e: /* FRINT32Z */
12805 case 0x1f: /* FRINT64Z */
12806 need_rmode = true;
12807 rmode = FPROUNDING_ZERO;
12808 /* fall through */
12809 case 0x5e: /* FRINT32X */
12810 case 0x5f: /* FRINT64X */
12811 need_fpstatus = true;
12812 if ((size == 3 && !is_q) || !dc_isar_feature(aa64_frint, s)) {
12813 unallocated_encoding(s);
12814 return;
12816 break;
12817 default:
12818 unallocated_encoding(s);
12819 return;
12821 break;
12823 default:
12824 unallocated_encoding(s);
12825 return;
12828 if (!fp_access_check(s)) {
12829 return;
12832 if (need_fpstatus || need_rmode) {
12833 tcg_fpstatus = fpstatus_ptr(FPST_FPCR);
12834 } else {
12835 tcg_fpstatus = NULL;
12837 if (need_rmode) {
12838 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rmode));
12839 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
12840 } else {
12841 tcg_rmode = NULL;
12844 switch (opcode) {
12845 case 0x5:
12846 if (u && size == 0) { /* NOT */
12847 gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_not, 0);
12848 return;
12850 break;
12851 case 0x8: /* CMGT, CMGE */
12852 if (u) {
12853 gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_cge0, size);
12854 } else {
12855 gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_cgt0, size);
12857 return;
12858 case 0x9: /* CMEQ, CMLE */
12859 if (u) {
12860 gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_cle0, size);
12861 } else {
12862 gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_ceq0, size);
12864 return;
12865 case 0xa: /* CMLT */
12866 gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_clt0, size);
12867 return;
12868 case 0xb:
12869 if (u) { /* ABS, NEG */
12870 gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_neg, size);
12871 } else {
12872 gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_abs, size);
12874 return;
12877 if (size == 3) {
12878 /* All 64-bit element operations can be shared with scalar 2misc */
12879 int pass;
12881 /* Coverity claims (size == 3 && !is_q) has been eliminated
12882 * from all paths leading to here.
12884 tcg_debug_assert(is_q);
12885 for (pass = 0; pass < 2; pass++) {
12886 TCGv_i64 tcg_op = tcg_temp_new_i64();
12887 TCGv_i64 tcg_res = tcg_temp_new_i64();
12889 read_vec_element(s, tcg_op, rn, pass, MO_64);
12891 handle_2misc_64(s, opcode, u, tcg_res, tcg_op,
12892 tcg_rmode, tcg_fpstatus);
12894 write_vec_element(s, tcg_res, rd, pass, MO_64);
12896 tcg_temp_free_i64(tcg_res);
12897 tcg_temp_free_i64(tcg_op);
12899 } else {
12900 int pass;
12902 for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
12903 TCGv_i32 tcg_op = tcg_temp_new_i32();
12904 TCGv_i32 tcg_res = tcg_temp_new_i32();
12906 read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
12908 if (size == 2) {
12909 /* Special cases for 32 bit elements */
12910 switch (opcode) {
12911 case 0x4: /* CLS */
12912 if (u) {
12913 tcg_gen_clzi_i32(tcg_res, tcg_op, 32);
12914 } else {
12915 tcg_gen_clrsb_i32(tcg_res, tcg_op);
12917 break;
12918 case 0x7: /* SQABS, SQNEG */
12919 if (u) {
12920 gen_helper_neon_qneg_s32(tcg_res, cpu_env, tcg_op);
12921 } else {
12922 gen_helper_neon_qabs_s32(tcg_res, cpu_env, tcg_op);
12924 break;
12925 case 0x2f: /* FABS */
12926 gen_helper_vfp_abss(tcg_res, tcg_op);
12927 break;
12928 case 0x6f: /* FNEG */
12929 gen_helper_vfp_negs(tcg_res, tcg_op);
12930 break;
12931 case 0x7f: /* FSQRT */
12932 gen_helper_vfp_sqrts(tcg_res, tcg_op, cpu_env);
12933 break;
12934 case 0x1a: /* FCVTNS */
12935 case 0x1b: /* FCVTMS */
12936 case 0x1c: /* FCVTAS */
12937 case 0x3a: /* FCVTPS */
12938 case 0x3b: /* FCVTZS */
12939 gen_helper_vfp_tosls(tcg_res, tcg_op,
12940 tcg_constant_i32(0), tcg_fpstatus);
12941 break;
12942 case 0x5a: /* FCVTNU */
12943 case 0x5b: /* FCVTMU */
12944 case 0x5c: /* FCVTAU */
12945 case 0x7a: /* FCVTPU */
12946 case 0x7b: /* FCVTZU */
12947 gen_helper_vfp_touls(tcg_res, tcg_op,
12948 tcg_constant_i32(0), tcg_fpstatus);
12949 break;
12950 case 0x18: /* FRINTN */
12951 case 0x19: /* FRINTM */
12952 case 0x38: /* FRINTP */
12953 case 0x39: /* FRINTZ */
12954 case 0x58: /* FRINTA */
12955 case 0x79: /* FRINTI */
12956 gen_helper_rints(tcg_res, tcg_op, tcg_fpstatus);
12957 break;
12958 case 0x59: /* FRINTX */
12959 gen_helper_rints_exact(tcg_res, tcg_op, tcg_fpstatus);
12960 break;
12961 case 0x7c: /* URSQRTE */
12962 gen_helper_rsqrte_u32(tcg_res, tcg_op);
12963 break;
12964 case 0x1e: /* FRINT32Z */
12965 case 0x5e: /* FRINT32X */
12966 gen_helper_frint32_s(tcg_res, tcg_op, tcg_fpstatus);
12967 break;
12968 case 0x1f: /* FRINT64Z */
12969 case 0x5f: /* FRINT64X */
12970 gen_helper_frint64_s(tcg_res, tcg_op, tcg_fpstatus);
12971 break;
12972 default:
12973 g_assert_not_reached();
12975 } else {
12976 /* Use helpers for 8 and 16 bit elements */
12977 switch (opcode) {
12978 case 0x5: /* CNT, RBIT */
12979 /* For these two insns size is part of the opcode specifier
12980 * (handled earlier); they always operate on byte elements.
12982 if (u) {
12983 gen_helper_neon_rbit_u8(tcg_res, tcg_op);
12984 } else {
12985 gen_helper_neon_cnt_u8(tcg_res, tcg_op);
12987 break;
12988 case 0x7: /* SQABS, SQNEG */
12990 NeonGenOneOpEnvFn *genfn;
12991 static NeonGenOneOpEnvFn * const fns[2][2] = {
12992 { gen_helper_neon_qabs_s8, gen_helper_neon_qneg_s8 },
12993 { gen_helper_neon_qabs_s16, gen_helper_neon_qneg_s16 },
12995 genfn = fns[size][u];
12996 genfn(tcg_res, cpu_env, tcg_op);
12997 break;
12999 case 0x4: /* CLS, CLZ */
13000 if (u) {
13001 if (size == 0) {
13002 gen_helper_neon_clz_u8(tcg_res, tcg_op);
13003 } else {
13004 gen_helper_neon_clz_u16(tcg_res, tcg_op);
13006 } else {
13007 if (size == 0) {
13008 gen_helper_neon_cls_s8(tcg_res, tcg_op);
13009 } else {
13010 gen_helper_neon_cls_s16(tcg_res, tcg_op);
13013 break;
13014 default:
13015 g_assert_not_reached();
13019 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
13021 tcg_temp_free_i32(tcg_res);
13022 tcg_temp_free_i32(tcg_op);
13025 clear_vec_high(s, is_q, rd);
13027 if (need_rmode) {
13028 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
13029 tcg_temp_free_i32(tcg_rmode);
13031 if (need_fpstatus) {
13032 tcg_temp_free_ptr(tcg_fpstatus);
13036 /* AdvSIMD [scalar] two register miscellaneous (FP16)
13038 * 31 30 29 28 27 24 23 22 21 17 16 12 11 10 9 5 4 0
13039 * +---+---+---+---+---------+---+-------------+--------+-----+------+------+
13040 * | 0 | Q | U | S | 1 1 1 0 | a | 1 1 1 1 0 0 | opcode | 1 0 | Rn | Rd |
13041 * +---+---+---+---+---------+---+-------------+--------+-----+------+------+
13042 * mask: 1000 1111 0111 1110 0000 1100 0000 0000 0x8f7e 0c00
13043 * val: 0000 1110 0111 1000 0000 1000 0000 0000 0x0e78 0800
13045 * This actually covers two groups where scalar access is governed by
13046 * bit 28. A bunch of the instructions (float to integral) only exist
13047 * in the vector form and are un-allocated for the scalar decode. Also
13048 * in the scalar decode Q is always 1.
13050 static void disas_simd_two_reg_misc_fp16(DisasContext *s, uint32_t insn)
13052 int fpop, opcode, a, u;
13053 int rn, rd;
13054 bool is_q;
13055 bool is_scalar;
13056 bool only_in_vector = false;
13058 int pass;
13059 TCGv_i32 tcg_rmode = NULL;
13060 TCGv_ptr tcg_fpstatus = NULL;
13061 bool need_rmode = false;
13062 bool need_fpst = true;
13063 int rmode;
13065 if (!dc_isar_feature(aa64_fp16, s)) {
13066 unallocated_encoding(s);
13067 return;
13070 rd = extract32(insn, 0, 5);
13071 rn = extract32(insn, 5, 5);
13073 a = extract32(insn, 23, 1);
13074 u = extract32(insn, 29, 1);
13075 is_scalar = extract32(insn, 28, 1);
13076 is_q = extract32(insn, 30, 1);
13078 opcode = extract32(insn, 12, 5);
13079 fpop = deposit32(opcode, 5, 1, a);
13080 fpop = deposit32(fpop, 6, 1, u);
13082 switch (fpop) {
13083 case 0x1d: /* SCVTF */
13084 case 0x5d: /* UCVTF */
13086 int elements;
13088 if (is_scalar) {
13089 elements = 1;
13090 } else {
13091 elements = (is_q ? 8 : 4);
13094 if (!fp_access_check(s)) {
13095 return;
13097 handle_simd_intfp_conv(s, rd, rn, elements, !u, 0, MO_16);
13098 return;
13100 break;
13101 case 0x2c: /* FCMGT (zero) */
13102 case 0x2d: /* FCMEQ (zero) */
13103 case 0x2e: /* FCMLT (zero) */
13104 case 0x6c: /* FCMGE (zero) */
13105 case 0x6d: /* FCMLE (zero) */
13106 handle_2misc_fcmp_zero(s, fpop, is_scalar, 0, is_q, MO_16, rn, rd);
13107 return;
13108 case 0x3d: /* FRECPE */
13109 case 0x3f: /* FRECPX */
13110 break;
13111 case 0x18: /* FRINTN */
13112 need_rmode = true;
13113 only_in_vector = true;
13114 rmode = FPROUNDING_TIEEVEN;
13115 break;
13116 case 0x19: /* FRINTM */
13117 need_rmode = true;
13118 only_in_vector = true;
13119 rmode = FPROUNDING_NEGINF;
13120 break;
13121 case 0x38: /* FRINTP */
13122 need_rmode = true;
13123 only_in_vector = true;
13124 rmode = FPROUNDING_POSINF;
13125 break;
13126 case 0x39: /* FRINTZ */
13127 need_rmode = true;
13128 only_in_vector = true;
13129 rmode = FPROUNDING_ZERO;
13130 break;
13131 case 0x58: /* FRINTA */
13132 need_rmode = true;
13133 only_in_vector = true;
13134 rmode = FPROUNDING_TIEAWAY;
13135 break;
13136 case 0x59: /* FRINTX */
13137 case 0x79: /* FRINTI */
13138 only_in_vector = true;
13139 /* current rounding mode */
13140 break;
13141 case 0x1a: /* FCVTNS */
13142 need_rmode = true;
13143 rmode = FPROUNDING_TIEEVEN;
13144 break;
13145 case 0x1b: /* FCVTMS */
13146 need_rmode = true;
13147 rmode = FPROUNDING_NEGINF;
13148 break;
13149 case 0x1c: /* FCVTAS */
13150 need_rmode = true;
13151 rmode = FPROUNDING_TIEAWAY;
13152 break;
13153 case 0x3a: /* FCVTPS */
13154 need_rmode = true;
13155 rmode = FPROUNDING_POSINF;
13156 break;
13157 case 0x3b: /* FCVTZS */
13158 need_rmode = true;
13159 rmode = FPROUNDING_ZERO;
13160 break;
13161 case 0x5a: /* FCVTNU */
13162 need_rmode = true;
13163 rmode = FPROUNDING_TIEEVEN;
13164 break;
13165 case 0x5b: /* FCVTMU */
13166 need_rmode = true;
13167 rmode = FPROUNDING_NEGINF;
13168 break;
13169 case 0x5c: /* FCVTAU */
13170 need_rmode = true;
13171 rmode = FPROUNDING_TIEAWAY;
13172 break;
13173 case 0x7a: /* FCVTPU */
13174 need_rmode = true;
13175 rmode = FPROUNDING_POSINF;
13176 break;
13177 case 0x7b: /* FCVTZU */
13178 need_rmode = true;
13179 rmode = FPROUNDING_ZERO;
13180 break;
13181 case 0x2f: /* FABS */
13182 case 0x6f: /* FNEG */
13183 need_fpst = false;
13184 break;
13185 case 0x7d: /* FRSQRTE */
13186 case 0x7f: /* FSQRT (vector) */
13187 break;
13188 default:
13189 unallocated_encoding(s);
13190 return;
13194 /* Check additional constraints for the scalar encoding */
13195 if (is_scalar) {
13196 if (!is_q) {
13197 unallocated_encoding(s);
13198 return;
13200 /* FRINTxx is only in the vector form */
13201 if (only_in_vector) {
13202 unallocated_encoding(s);
13203 return;
13207 if (!fp_access_check(s)) {
13208 return;
13211 if (need_rmode || need_fpst) {
13212 tcg_fpstatus = fpstatus_ptr(FPST_FPCR_F16);
13215 if (need_rmode) {
13216 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rmode));
13217 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
13220 if (is_scalar) {
13221 TCGv_i32 tcg_op = read_fp_hreg(s, rn);
13222 TCGv_i32 tcg_res = tcg_temp_new_i32();
13224 switch (fpop) {
13225 case 0x1a: /* FCVTNS */
13226 case 0x1b: /* FCVTMS */
13227 case 0x1c: /* FCVTAS */
13228 case 0x3a: /* FCVTPS */
13229 case 0x3b: /* FCVTZS */
13230 gen_helper_advsimd_f16tosinth(tcg_res, tcg_op, tcg_fpstatus);
13231 break;
13232 case 0x3d: /* FRECPE */
13233 gen_helper_recpe_f16(tcg_res, tcg_op, tcg_fpstatus);
13234 break;
13235 case 0x3f: /* FRECPX */
13236 gen_helper_frecpx_f16(tcg_res, tcg_op, tcg_fpstatus);
13237 break;
13238 case 0x5a: /* FCVTNU */
13239 case 0x5b: /* FCVTMU */
13240 case 0x5c: /* FCVTAU */
13241 case 0x7a: /* FCVTPU */
13242 case 0x7b: /* FCVTZU */
13243 gen_helper_advsimd_f16touinth(tcg_res, tcg_op, tcg_fpstatus);
13244 break;
13245 case 0x6f: /* FNEG */
13246 tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
13247 break;
13248 case 0x7d: /* FRSQRTE */
13249 gen_helper_rsqrte_f16(tcg_res, tcg_op, tcg_fpstatus);
13250 break;
13251 default:
13252 g_assert_not_reached();
13255 /* limit any sign extension going on */
13256 tcg_gen_andi_i32(tcg_res, tcg_res, 0xffff);
13257 write_fp_sreg(s, rd, tcg_res);
13259 tcg_temp_free_i32(tcg_res);
13260 tcg_temp_free_i32(tcg_op);
13261 } else {
13262 for (pass = 0; pass < (is_q ? 8 : 4); pass++) {
13263 TCGv_i32 tcg_op = tcg_temp_new_i32();
13264 TCGv_i32 tcg_res = tcg_temp_new_i32();
13266 read_vec_element_i32(s, tcg_op, rn, pass, MO_16);
13268 switch (fpop) {
13269 case 0x1a: /* FCVTNS */
13270 case 0x1b: /* FCVTMS */
13271 case 0x1c: /* FCVTAS */
13272 case 0x3a: /* FCVTPS */
13273 case 0x3b: /* FCVTZS */
13274 gen_helper_advsimd_f16tosinth(tcg_res, tcg_op, tcg_fpstatus);
13275 break;
13276 case 0x3d: /* FRECPE */
13277 gen_helper_recpe_f16(tcg_res, tcg_op, tcg_fpstatus);
13278 break;
13279 case 0x5a: /* FCVTNU */
13280 case 0x5b: /* FCVTMU */
13281 case 0x5c: /* FCVTAU */
13282 case 0x7a: /* FCVTPU */
13283 case 0x7b: /* FCVTZU */
13284 gen_helper_advsimd_f16touinth(tcg_res, tcg_op, tcg_fpstatus);
13285 break;
13286 case 0x18: /* FRINTN */
13287 case 0x19: /* FRINTM */
13288 case 0x38: /* FRINTP */
13289 case 0x39: /* FRINTZ */
13290 case 0x58: /* FRINTA */
13291 case 0x79: /* FRINTI */
13292 gen_helper_advsimd_rinth(tcg_res, tcg_op, tcg_fpstatus);
13293 break;
13294 case 0x59: /* FRINTX */
13295 gen_helper_advsimd_rinth_exact(tcg_res, tcg_op, tcg_fpstatus);
13296 break;
13297 case 0x2f: /* FABS */
13298 tcg_gen_andi_i32(tcg_res, tcg_op, 0x7fff);
13299 break;
13300 case 0x6f: /* FNEG */
13301 tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
13302 break;
13303 case 0x7d: /* FRSQRTE */
13304 gen_helper_rsqrte_f16(tcg_res, tcg_op, tcg_fpstatus);
13305 break;
13306 case 0x7f: /* FSQRT */
13307 gen_helper_sqrt_f16(tcg_res, tcg_op, tcg_fpstatus);
13308 break;
13309 default:
13310 g_assert_not_reached();
13313 write_vec_element_i32(s, tcg_res, rd, pass, MO_16);
13315 tcg_temp_free_i32(tcg_res);
13316 tcg_temp_free_i32(tcg_op);
13319 clear_vec_high(s, is_q, rd);
13322 if (tcg_rmode) {
13323 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
13324 tcg_temp_free_i32(tcg_rmode);
13327 if (tcg_fpstatus) {
13328 tcg_temp_free_ptr(tcg_fpstatus);
13332 /* AdvSIMD scalar x indexed element
13333 * 31 30 29 28 24 23 22 21 20 19 16 15 12 11 10 9 5 4 0
13334 * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
13335 * | 0 1 | U | 1 1 1 1 1 | size | L | M | Rm | opc | H | 0 | Rn | Rd |
13336 * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
13337 * AdvSIMD vector x indexed element
13338 * 31 30 29 28 24 23 22 21 20 19 16 15 12 11 10 9 5 4 0
13339 * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
13340 * | 0 | Q | U | 0 1 1 1 1 | size | L | M | Rm | opc | H | 0 | Rn | Rd |
13341 * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
13343 static void disas_simd_indexed(DisasContext *s, uint32_t insn)
13345 /* This encoding has two kinds of instruction:
13346 * normal, where we perform elt x idxelt => elt for each
13347 * element in the vector
13348 * long, where we perform elt x idxelt and generate a result of
13349 * double the width of the input element
13350 * The long ops have a 'part' specifier (ie come in INSN, INSN2 pairs).
13352 bool is_scalar = extract32(insn, 28, 1);
13353 bool is_q = extract32(insn, 30, 1);
13354 bool u = extract32(insn, 29, 1);
13355 int size = extract32(insn, 22, 2);
13356 int l = extract32(insn, 21, 1);
13357 int m = extract32(insn, 20, 1);
13358 /* Note that the Rm field here is only 4 bits, not 5 as it usually is */
13359 int rm = extract32(insn, 16, 4);
13360 int opcode = extract32(insn, 12, 4);
13361 int h = extract32(insn, 11, 1);
13362 int rn = extract32(insn, 5, 5);
13363 int rd = extract32(insn, 0, 5);
13364 bool is_long = false;
13365 int is_fp = 0;
13366 bool is_fp16 = false;
13367 int index;
13368 TCGv_ptr fpst;
13370 switch (16 * u + opcode) {
13371 case 0x08: /* MUL */
13372 case 0x10: /* MLA */
13373 case 0x14: /* MLS */
13374 if (is_scalar) {
13375 unallocated_encoding(s);
13376 return;
13378 break;
13379 case 0x02: /* SMLAL, SMLAL2 */
13380 case 0x12: /* UMLAL, UMLAL2 */
13381 case 0x06: /* SMLSL, SMLSL2 */
13382 case 0x16: /* UMLSL, UMLSL2 */
13383 case 0x0a: /* SMULL, SMULL2 */
13384 case 0x1a: /* UMULL, UMULL2 */
13385 if (is_scalar) {
13386 unallocated_encoding(s);
13387 return;
13389 is_long = true;
13390 break;
13391 case 0x03: /* SQDMLAL, SQDMLAL2 */
13392 case 0x07: /* SQDMLSL, SQDMLSL2 */
13393 case 0x0b: /* SQDMULL, SQDMULL2 */
13394 is_long = true;
13395 break;
13396 case 0x0c: /* SQDMULH */
13397 case 0x0d: /* SQRDMULH */
13398 break;
13399 case 0x01: /* FMLA */
13400 case 0x05: /* FMLS */
13401 case 0x09: /* FMUL */
13402 case 0x19: /* FMULX */
13403 is_fp = 1;
13404 break;
13405 case 0x1d: /* SQRDMLAH */
13406 case 0x1f: /* SQRDMLSH */
13407 if (!dc_isar_feature(aa64_rdm, s)) {
13408 unallocated_encoding(s);
13409 return;
13411 break;
13412 case 0x0e: /* SDOT */
13413 case 0x1e: /* UDOT */
13414 if (is_scalar || size != MO_32 || !dc_isar_feature(aa64_dp, s)) {
13415 unallocated_encoding(s);
13416 return;
13418 break;
13419 case 0x0f:
13420 switch (size) {
13421 case 0: /* SUDOT */
13422 case 2: /* USDOT */
13423 if (is_scalar || !dc_isar_feature(aa64_i8mm, s)) {
13424 unallocated_encoding(s);
13425 return;
13427 size = MO_32;
13428 break;
13429 case 1: /* BFDOT */
13430 if (is_scalar || !dc_isar_feature(aa64_bf16, s)) {
13431 unallocated_encoding(s);
13432 return;
13434 size = MO_32;
13435 break;
13436 case 3: /* BFMLAL{B,T} */
13437 if (is_scalar || !dc_isar_feature(aa64_bf16, s)) {
13438 unallocated_encoding(s);
13439 return;
13441 /* can't set is_fp without other incorrect size checks */
13442 size = MO_16;
13443 break;
13444 default:
13445 unallocated_encoding(s);
13446 return;
13448 break;
13449 case 0x11: /* FCMLA #0 */
13450 case 0x13: /* FCMLA #90 */
13451 case 0x15: /* FCMLA #180 */
13452 case 0x17: /* FCMLA #270 */
13453 if (is_scalar || !dc_isar_feature(aa64_fcma, s)) {
13454 unallocated_encoding(s);
13455 return;
13457 is_fp = 2;
13458 break;
13459 case 0x00: /* FMLAL */
13460 case 0x04: /* FMLSL */
13461 case 0x18: /* FMLAL2 */
13462 case 0x1c: /* FMLSL2 */
13463 if (is_scalar || size != MO_32 || !dc_isar_feature(aa64_fhm, s)) {
13464 unallocated_encoding(s);
13465 return;
13467 size = MO_16;
13468 /* is_fp, but we pass cpu_env not fp_status. */
13469 break;
13470 default:
13471 unallocated_encoding(s);
13472 return;
13475 switch (is_fp) {
13476 case 1: /* normal fp */
13477 /* convert insn encoded size to MemOp size */
13478 switch (size) {
13479 case 0: /* half-precision */
13480 size = MO_16;
13481 is_fp16 = true;
13482 break;
13483 case MO_32: /* single precision */
13484 case MO_64: /* double precision */
13485 break;
13486 default:
13487 unallocated_encoding(s);
13488 return;
13490 break;
13492 case 2: /* complex fp */
13493 /* Each indexable element is a complex pair. */
13494 size += 1;
13495 switch (size) {
13496 case MO_32:
13497 if (h && !is_q) {
13498 unallocated_encoding(s);
13499 return;
13501 is_fp16 = true;
13502 break;
13503 case MO_64:
13504 break;
13505 default:
13506 unallocated_encoding(s);
13507 return;
13509 break;
13511 default: /* integer */
13512 switch (size) {
13513 case MO_8:
13514 case MO_64:
13515 unallocated_encoding(s);
13516 return;
13518 break;
13520 if (is_fp16 && !dc_isar_feature(aa64_fp16, s)) {
13521 unallocated_encoding(s);
13522 return;
13525 /* Given MemOp size, adjust register and indexing. */
13526 switch (size) {
13527 case MO_16:
13528 index = h << 2 | l << 1 | m;
13529 break;
13530 case MO_32:
13531 index = h << 1 | l;
13532 rm |= m << 4;
13533 break;
13534 case MO_64:
13535 if (l || !is_q) {
13536 unallocated_encoding(s);
13537 return;
13539 index = h;
13540 rm |= m << 4;
13541 break;
13542 default:
13543 g_assert_not_reached();
13546 if (!fp_access_check(s)) {
13547 return;
13550 if (is_fp) {
13551 fpst = fpstatus_ptr(is_fp16 ? FPST_FPCR_F16 : FPST_FPCR);
13552 } else {
13553 fpst = NULL;
13556 switch (16 * u + opcode) {
13557 case 0x0e: /* SDOT */
13558 case 0x1e: /* UDOT */
13559 gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
13560 u ? gen_helper_gvec_udot_idx_b
13561 : gen_helper_gvec_sdot_idx_b);
13562 return;
13563 case 0x0f:
13564 switch (extract32(insn, 22, 2)) {
13565 case 0: /* SUDOT */
13566 gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
13567 gen_helper_gvec_sudot_idx_b);
13568 return;
13569 case 1: /* BFDOT */
13570 gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
13571 gen_helper_gvec_bfdot_idx);
13572 return;
13573 case 2: /* USDOT */
13574 gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
13575 gen_helper_gvec_usdot_idx_b);
13576 return;
13577 case 3: /* BFMLAL{B,T} */
13578 gen_gvec_op4_fpst(s, 1, rd, rn, rm, rd, 0, (index << 1) | is_q,
13579 gen_helper_gvec_bfmlal_idx);
13580 return;
13582 g_assert_not_reached();
13583 case 0x11: /* FCMLA #0 */
13584 case 0x13: /* FCMLA #90 */
13585 case 0x15: /* FCMLA #180 */
13586 case 0x17: /* FCMLA #270 */
13588 int rot = extract32(insn, 13, 2);
13589 int data = (index << 2) | rot;
13590 tcg_gen_gvec_4_ptr(vec_full_reg_offset(s, rd),
13591 vec_full_reg_offset(s, rn),
13592 vec_full_reg_offset(s, rm),
13593 vec_full_reg_offset(s, rd), fpst,
13594 is_q ? 16 : 8, vec_full_reg_size(s), data,
13595 size == MO_64
13596 ? gen_helper_gvec_fcmlas_idx
13597 : gen_helper_gvec_fcmlah_idx);
13598 tcg_temp_free_ptr(fpst);
13600 return;
13602 case 0x00: /* FMLAL */
13603 case 0x04: /* FMLSL */
13604 case 0x18: /* FMLAL2 */
13605 case 0x1c: /* FMLSL2 */
13607 int is_s = extract32(opcode, 2, 1);
13608 int is_2 = u;
13609 int data = (index << 2) | (is_2 << 1) | is_s;
13610 tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
13611 vec_full_reg_offset(s, rn),
13612 vec_full_reg_offset(s, rm), cpu_env,
13613 is_q ? 16 : 8, vec_full_reg_size(s),
13614 data, gen_helper_gvec_fmlal_idx_a64);
13616 return;
13618 case 0x08: /* MUL */
13619 if (!is_long && !is_scalar) {
13620 static gen_helper_gvec_3 * const fns[3] = {
13621 gen_helper_gvec_mul_idx_h,
13622 gen_helper_gvec_mul_idx_s,
13623 gen_helper_gvec_mul_idx_d,
13625 tcg_gen_gvec_3_ool(vec_full_reg_offset(s, rd),
13626 vec_full_reg_offset(s, rn),
13627 vec_full_reg_offset(s, rm),
13628 is_q ? 16 : 8, vec_full_reg_size(s),
13629 index, fns[size - 1]);
13630 return;
13632 break;
13634 case 0x10: /* MLA */
13635 if (!is_long && !is_scalar) {
13636 static gen_helper_gvec_4 * const fns[3] = {
13637 gen_helper_gvec_mla_idx_h,
13638 gen_helper_gvec_mla_idx_s,
13639 gen_helper_gvec_mla_idx_d,
13641 tcg_gen_gvec_4_ool(vec_full_reg_offset(s, rd),
13642 vec_full_reg_offset(s, rn),
13643 vec_full_reg_offset(s, rm),
13644 vec_full_reg_offset(s, rd),
13645 is_q ? 16 : 8, vec_full_reg_size(s),
13646 index, fns[size - 1]);
13647 return;
13649 break;
13651 case 0x14: /* MLS */
13652 if (!is_long && !is_scalar) {
13653 static gen_helper_gvec_4 * const fns[3] = {
13654 gen_helper_gvec_mls_idx_h,
13655 gen_helper_gvec_mls_idx_s,
13656 gen_helper_gvec_mls_idx_d,
13658 tcg_gen_gvec_4_ool(vec_full_reg_offset(s, rd),
13659 vec_full_reg_offset(s, rn),
13660 vec_full_reg_offset(s, rm),
13661 vec_full_reg_offset(s, rd),
13662 is_q ? 16 : 8, vec_full_reg_size(s),
13663 index, fns[size - 1]);
13664 return;
13666 break;
13669 if (size == 3) {
13670 TCGv_i64 tcg_idx = tcg_temp_new_i64();
13671 int pass;
13673 assert(is_fp && is_q && !is_long);
13675 read_vec_element(s, tcg_idx, rm, index, MO_64);
13677 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
13678 TCGv_i64 tcg_op = tcg_temp_new_i64();
13679 TCGv_i64 tcg_res = tcg_temp_new_i64();
13681 read_vec_element(s, tcg_op, rn, pass, MO_64);
13683 switch (16 * u + opcode) {
13684 case 0x05: /* FMLS */
13685 /* As usual for ARM, separate negation for fused multiply-add */
13686 gen_helper_vfp_negd(tcg_op, tcg_op);
13687 /* fall through */
13688 case 0x01: /* FMLA */
13689 read_vec_element(s, tcg_res, rd, pass, MO_64);
13690 gen_helper_vfp_muladdd(tcg_res, tcg_op, tcg_idx, tcg_res, fpst);
13691 break;
13692 case 0x09: /* FMUL */
13693 gen_helper_vfp_muld(tcg_res, tcg_op, tcg_idx, fpst);
13694 break;
13695 case 0x19: /* FMULX */
13696 gen_helper_vfp_mulxd(tcg_res, tcg_op, tcg_idx, fpst);
13697 break;
13698 default:
13699 g_assert_not_reached();
13702 write_vec_element(s, tcg_res, rd, pass, MO_64);
13703 tcg_temp_free_i64(tcg_op);
13704 tcg_temp_free_i64(tcg_res);
13707 tcg_temp_free_i64(tcg_idx);
13708 clear_vec_high(s, !is_scalar, rd);
13709 } else if (!is_long) {
13710 /* 32 bit floating point, or 16 or 32 bit integer.
13711 * For the 16 bit scalar case we use the usual Neon helpers and
13712 * rely on the fact that 0 op 0 == 0 with no side effects.
13714 TCGv_i32 tcg_idx = tcg_temp_new_i32();
13715 int pass, maxpasses;
13717 if (is_scalar) {
13718 maxpasses = 1;
13719 } else {
13720 maxpasses = is_q ? 4 : 2;
13723 read_vec_element_i32(s, tcg_idx, rm, index, size);
13725 if (size == 1 && !is_scalar) {
13726 /* The simplest way to handle the 16x16 indexed ops is to duplicate
13727 * the index into both halves of the 32 bit tcg_idx and then use
13728 * the usual Neon helpers.
13730 tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
13733 for (pass = 0; pass < maxpasses; pass++) {
13734 TCGv_i32 tcg_op = tcg_temp_new_i32();
13735 TCGv_i32 tcg_res = tcg_temp_new_i32();
13737 read_vec_element_i32(s, tcg_op, rn, pass, is_scalar ? size : MO_32);
13739 switch (16 * u + opcode) {
13740 case 0x08: /* MUL */
13741 case 0x10: /* MLA */
13742 case 0x14: /* MLS */
13744 static NeonGenTwoOpFn * const fns[2][2] = {
13745 { gen_helper_neon_add_u16, gen_helper_neon_sub_u16 },
13746 { tcg_gen_add_i32, tcg_gen_sub_i32 },
13748 NeonGenTwoOpFn *genfn;
13749 bool is_sub = opcode == 0x4;
13751 if (size == 1) {
13752 gen_helper_neon_mul_u16(tcg_res, tcg_op, tcg_idx);
13753 } else {
13754 tcg_gen_mul_i32(tcg_res, tcg_op, tcg_idx);
13756 if (opcode == 0x8) {
13757 break;
13759 read_vec_element_i32(s, tcg_op, rd, pass, MO_32);
13760 genfn = fns[size - 1][is_sub];
13761 genfn(tcg_res, tcg_op, tcg_res);
13762 break;
13764 case 0x05: /* FMLS */
13765 case 0x01: /* FMLA */
13766 read_vec_element_i32(s, tcg_res, rd, pass,
13767 is_scalar ? size : MO_32);
13768 switch (size) {
13769 case 1:
13770 if (opcode == 0x5) {
13771 /* As usual for ARM, separate negation for fused
13772 * multiply-add */
13773 tcg_gen_xori_i32(tcg_op, tcg_op, 0x80008000);
13775 if (is_scalar) {
13776 gen_helper_advsimd_muladdh(tcg_res, tcg_op, tcg_idx,
13777 tcg_res, fpst);
13778 } else {
13779 gen_helper_advsimd_muladd2h(tcg_res, tcg_op, tcg_idx,
13780 tcg_res, fpst);
13782 break;
13783 case 2:
13784 if (opcode == 0x5) {
13785 /* As usual for ARM, separate negation for
13786 * fused multiply-add */
13787 tcg_gen_xori_i32(tcg_op, tcg_op, 0x80000000);
13789 gen_helper_vfp_muladds(tcg_res, tcg_op, tcg_idx,
13790 tcg_res, fpst);
13791 break;
13792 default:
13793 g_assert_not_reached();
13795 break;
13796 case 0x09: /* FMUL */
13797 switch (size) {
13798 case 1:
13799 if (is_scalar) {
13800 gen_helper_advsimd_mulh(tcg_res, tcg_op,
13801 tcg_idx, fpst);
13802 } else {
13803 gen_helper_advsimd_mul2h(tcg_res, tcg_op,
13804 tcg_idx, fpst);
13806 break;
13807 case 2:
13808 gen_helper_vfp_muls(tcg_res, tcg_op, tcg_idx, fpst);
13809 break;
13810 default:
13811 g_assert_not_reached();
13813 break;
13814 case 0x19: /* FMULX */
13815 switch (size) {
13816 case 1:
13817 if (is_scalar) {
13818 gen_helper_advsimd_mulxh(tcg_res, tcg_op,
13819 tcg_idx, fpst);
13820 } else {
13821 gen_helper_advsimd_mulx2h(tcg_res, tcg_op,
13822 tcg_idx, fpst);
13824 break;
13825 case 2:
13826 gen_helper_vfp_mulxs(tcg_res, tcg_op, tcg_idx, fpst);
13827 break;
13828 default:
13829 g_assert_not_reached();
13831 break;
13832 case 0x0c: /* SQDMULH */
13833 if (size == 1) {
13834 gen_helper_neon_qdmulh_s16(tcg_res, cpu_env,
13835 tcg_op, tcg_idx);
13836 } else {
13837 gen_helper_neon_qdmulh_s32(tcg_res, cpu_env,
13838 tcg_op, tcg_idx);
13840 break;
13841 case 0x0d: /* SQRDMULH */
13842 if (size == 1) {
13843 gen_helper_neon_qrdmulh_s16(tcg_res, cpu_env,
13844 tcg_op, tcg_idx);
13845 } else {
13846 gen_helper_neon_qrdmulh_s32(tcg_res, cpu_env,
13847 tcg_op, tcg_idx);
13849 break;
13850 case 0x1d: /* SQRDMLAH */
13851 read_vec_element_i32(s, tcg_res, rd, pass,
13852 is_scalar ? size : MO_32);
13853 if (size == 1) {
13854 gen_helper_neon_qrdmlah_s16(tcg_res, cpu_env,
13855 tcg_op, tcg_idx, tcg_res);
13856 } else {
13857 gen_helper_neon_qrdmlah_s32(tcg_res, cpu_env,
13858 tcg_op, tcg_idx, tcg_res);
13860 break;
13861 case 0x1f: /* SQRDMLSH */
13862 read_vec_element_i32(s, tcg_res, rd, pass,
13863 is_scalar ? size : MO_32);
13864 if (size == 1) {
13865 gen_helper_neon_qrdmlsh_s16(tcg_res, cpu_env,
13866 tcg_op, tcg_idx, tcg_res);
13867 } else {
13868 gen_helper_neon_qrdmlsh_s32(tcg_res, cpu_env,
13869 tcg_op, tcg_idx, tcg_res);
13871 break;
13872 default:
13873 g_assert_not_reached();
13876 if (is_scalar) {
13877 write_fp_sreg(s, rd, tcg_res);
13878 } else {
13879 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
13882 tcg_temp_free_i32(tcg_op);
13883 tcg_temp_free_i32(tcg_res);
13886 tcg_temp_free_i32(tcg_idx);
13887 clear_vec_high(s, is_q, rd);
13888 } else {
13889 /* long ops: 16x16->32 or 32x32->64 */
13890 TCGv_i64 tcg_res[2];
13891 int pass;
13892 bool satop = extract32(opcode, 0, 1);
13893 MemOp memop = MO_32;
13895 if (satop || !u) {
13896 memop |= MO_SIGN;
13899 if (size == 2) {
13900 TCGv_i64 tcg_idx = tcg_temp_new_i64();
13902 read_vec_element(s, tcg_idx, rm, index, memop);
13904 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
13905 TCGv_i64 tcg_op = tcg_temp_new_i64();
13906 TCGv_i64 tcg_passres;
13907 int passelt;
13909 if (is_scalar) {
13910 passelt = 0;
13911 } else {
13912 passelt = pass + (is_q * 2);
13915 read_vec_element(s, tcg_op, rn, passelt, memop);
13917 tcg_res[pass] = tcg_temp_new_i64();
13919 if (opcode == 0xa || opcode == 0xb) {
13920 /* Non-accumulating ops */
13921 tcg_passres = tcg_res[pass];
13922 } else {
13923 tcg_passres = tcg_temp_new_i64();
13926 tcg_gen_mul_i64(tcg_passres, tcg_op, tcg_idx);
13927 tcg_temp_free_i64(tcg_op);
13929 if (satop) {
13930 /* saturating, doubling */
13931 gen_helper_neon_addl_saturate_s64(tcg_passres, cpu_env,
13932 tcg_passres, tcg_passres);
13935 if (opcode == 0xa || opcode == 0xb) {
13936 continue;
13939 /* Accumulating op: handle accumulate step */
13940 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
13942 switch (opcode) {
13943 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
13944 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
13945 break;
13946 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
13947 tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
13948 break;
13949 case 0x7: /* SQDMLSL, SQDMLSL2 */
13950 tcg_gen_neg_i64(tcg_passres, tcg_passres);
13951 /* fall through */
13952 case 0x3: /* SQDMLAL, SQDMLAL2 */
13953 gen_helper_neon_addl_saturate_s64(tcg_res[pass], cpu_env,
13954 tcg_res[pass],
13955 tcg_passres);
13956 break;
13957 default:
13958 g_assert_not_reached();
13960 tcg_temp_free_i64(tcg_passres);
13962 tcg_temp_free_i64(tcg_idx);
13964 clear_vec_high(s, !is_scalar, rd);
13965 } else {
13966 TCGv_i32 tcg_idx = tcg_temp_new_i32();
13968 assert(size == 1);
13969 read_vec_element_i32(s, tcg_idx, rm, index, size);
13971 if (!is_scalar) {
13972 /* The simplest way to handle the 16x16 indexed ops is to
13973 * duplicate the index into both halves of the 32 bit tcg_idx
13974 * and then use the usual Neon helpers.
13976 tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
13979 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
13980 TCGv_i32 tcg_op = tcg_temp_new_i32();
13981 TCGv_i64 tcg_passres;
13983 if (is_scalar) {
13984 read_vec_element_i32(s, tcg_op, rn, pass, size);
13985 } else {
13986 read_vec_element_i32(s, tcg_op, rn,
13987 pass + (is_q * 2), MO_32);
13990 tcg_res[pass] = tcg_temp_new_i64();
13992 if (opcode == 0xa || opcode == 0xb) {
13993 /* Non-accumulating ops */
13994 tcg_passres = tcg_res[pass];
13995 } else {
13996 tcg_passres = tcg_temp_new_i64();
13999 if (memop & MO_SIGN) {
14000 gen_helper_neon_mull_s16(tcg_passres, tcg_op, tcg_idx);
14001 } else {
14002 gen_helper_neon_mull_u16(tcg_passres, tcg_op, tcg_idx);
14004 if (satop) {
14005 gen_helper_neon_addl_saturate_s32(tcg_passres, cpu_env,
14006 tcg_passres, tcg_passres);
14008 tcg_temp_free_i32(tcg_op);
14010 if (opcode == 0xa || opcode == 0xb) {
14011 continue;
14014 /* Accumulating op: handle accumulate step */
14015 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
14017 switch (opcode) {
14018 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
14019 gen_helper_neon_addl_u32(tcg_res[pass], tcg_res[pass],
14020 tcg_passres);
14021 break;
14022 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
14023 gen_helper_neon_subl_u32(tcg_res[pass], tcg_res[pass],
14024 tcg_passres);
14025 break;
14026 case 0x7: /* SQDMLSL, SQDMLSL2 */
14027 gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
14028 /* fall through */
14029 case 0x3: /* SQDMLAL, SQDMLAL2 */
14030 gen_helper_neon_addl_saturate_s32(tcg_res[pass], cpu_env,
14031 tcg_res[pass],
14032 tcg_passres);
14033 break;
14034 default:
14035 g_assert_not_reached();
14037 tcg_temp_free_i64(tcg_passres);
14039 tcg_temp_free_i32(tcg_idx);
14041 if (is_scalar) {
14042 tcg_gen_ext32u_i64(tcg_res[0], tcg_res[0]);
14046 if (is_scalar) {
14047 tcg_res[1] = tcg_constant_i64(0);
14050 for (pass = 0; pass < 2; pass++) {
14051 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
14052 tcg_temp_free_i64(tcg_res[pass]);
14056 if (fpst) {
14057 tcg_temp_free_ptr(fpst);
14061 /* Crypto AES
14062 * 31 24 23 22 21 17 16 12 11 10 9 5 4 0
14063 * +-----------------+------+-----------+--------+-----+------+------+
14064 * | 0 1 0 0 1 1 1 0 | size | 1 0 1 0 0 | opcode | 1 0 | Rn | Rd |
14065 * +-----------------+------+-----------+--------+-----+------+------+
14067 static void disas_crypto_aes(DisasContext *s, uint32_t insn)
14069 int size = extract32(insn, 22, 2);
14070 int opcode = extract32(insn, 12, 5);
14071 int rn = extract32(insn, 5, 5);
14072 int rd = extract32(insn, 0, 5);
14073 int decrypt;
14074 gen_helper_gvec_2 *genfn2 = NULL;
14075 gen_helper_gvec_3 *genfn3 = NULL;
14077 if (!dc_isar_feature(aa64_aes, s) || size != 0) {
14078 unallocated_encoding(s);
14079 return;
14082 switch (opcode) {
14083 case 0x4: /* AESE */
14084 decrypt = 0;
14085 genfn3 = gen_helper_crypto_aese;
14086 break;
14087 case 0x6: /* AESMC */
14088 decrypt = 0;
14089 genfn2 = gen_helper_crypto_aesmc;
14090 break;
14091 case 0x5: /* AESD */
14092 decrypt = 1;
14093 genfn3 = gen_helper_crypto_aese;
14094 break;
14095 case 0x7: /* AESIMC */
14096 decrypt = 1;
14097 genfn2 = gen_helper_crypto_aesmc;
14098 break;
14099 default:
14100 unallocated_encoding(s);
14101 return;
14104 if (!fp_access_check(s)) {
14105 return;
14107 if (genfn2) {
14108 gen_gvec_op2_ool(s, true, rd, rn, decrypt, genfn2);
14109 } else {
14110 gen_gvec_op3_ool(s, true, rd, rd, rn, decrypt, genfn3);
14114 /* Crypto three-reg SHA
14115 * 31 24 23 22 21 20 16 15 14 12 11 10 9 5 4 0
14116 * +-----------------+------+---+------+---+--------+-----+------+------+
14117 * | 0 1 0 1 1 1 1 0 | size | 0 | Rm | 0 | opcode | 0 0 | Rn | Rd |
14118 * +-----------------+------+---+------+---+--------+-----+------+------+
14120 static void disas_crypto_three_reg_sha(DisasContext *s, uint32_t insn)
14122 int size = extract32(insn, 22, 2);
14123 int opcode = extract32(insn, 12, 3);
14124 int rm = extract32(insn, 16, 5);
14125 int rn = extract32(insn, 5, 5);
14126 int rd = extract32(insn, 0, 5);
14127 gen_helper_gvec_3 *genfn;
14128 bool feature;
14130 if (size != 0) {
14131 unallocated_encoding(s);
14132 return;
14135 switch (opcode) {
14136 case 0: /* SHA1C */
14137 genfn = gen_helper_crypto_sha1c;
14138 feature = dc_isar_feature(aa64_sha1, s);
14139 break;
14140 case 1: /* SHA1P */
14141 genfn = gen_helper_crypto_sha1p;
14142 feature = dc_isar_feature(aa64_sha1, s);
14143 break;
14144 case 2: /* SHA1M */
14145 genfn = gen_helper_crypto_sha1m;
14146 feature = dc_isar_feature(aa64_sha1, s);
14147 break;
14148 case 3: /* SHA1SU0 */
14149 genfn = gen_helper_crypto_sha1su0;
14150 feature = dc_isar_feature(aa64_sha1, s);
14151 break;
14152 case 4: /* SHA256H */
14153 genfn = gen_helper_crypto_sha256h;
14154 feature = dc_isar_feature(aa64_sha256, s);
14155 break;
14156 case 5: /* SHA256H2 */
14157 genfn = gen_helper_crypto_sha256h2;
14158 feature = dc_isar_feature(aa64_sha256, s);
14159 break;
14160 case 6: /* SHA256SU1 */
14161 genfn = gen_helper_crypto_sha256su1;
14162 feature = dc_isar_feature(aa64_sha256, s);
14163 break;
14164 default:
14165 unallocated_encoding(s);
14166 return;
14169 if (!feature) {
14170 unallocated_encoding(s);
14171 return;
14174 if (!fp_access_check(s)) {
14175 return;
14177 gen_gvec_op3_ool(s, true, rd, rn, rm, 0, genfn);
14180 /* Crypto two-reg SHA
14181 * 31 24 23 22 21 17 16 12 11 10 9 5 4 0
14182 * +-----------------+------+-----------+--------+-----+------+------+
14183 * | 0 1 0 1 1 1 1 0 | size | 1 0 1 0 0 | opcode | 1 0 | Rn | Rd |
14184 * +-----------------+------+-----------+--------+-----+------+------+
14186 static void disas_crypto_two_reg_sha(DisasContext *s, uint32_t insn)
14188 int size = extract32(insn, 22, 2);
14189 int opcode = extract32(insn, 12, 5);
14190 int rn = extract32(insn, 5, 5);
14191 int rd = extract32(insn, 0, 5);
14192 gen_helper_gvec_2 *genfn;
14193 bool feature;
14195 if (size != 0) {
14196 unallocated_encoding(s);
14197 return;
14200 switch (opcode) {
14201 case 0: /* SHA1H */
14202 feature = dc_isar_feature(aa64_sha1, s);
14203 genfn = gen_helper_crypto_sha1h;
14204 break;
14205 case 1: /* SHA1SU1 */
14206 feature = dc_isar_feature(aa64_sha1, s);
14207 genfn = gen_helper_crypto_sha1su1;
14208 break;
14209 case 2: /* SHA256SU0 */
14210 feature = dc_isar_feature(aa64_sha256, s);
14211 genfn = gen_helper_crypto_sha256su0;
14212 break;
14213 default:
14214 unallocated_encoding(s);
14215 return;
14218 if (!feature) {
14219 unallocated_encoding(s);
14220 return;
14223 if (!fp_access_check(s)) {
14224 return;
14226 gen_gvec_op2_ool(s, true, rd, rn, 0, genfn);
14229 static void gen_rax1_i64(TCGv_i64 d, TCGv_i64 n, TCGv_i64 m)
14231 tcg_gen_rotli_i64(d, m, 1);
14232 tcg_gen_xor_i64(d, d, n);
14235 static void gen_rax1_vec(unsigned vece, TCGv_vec d, TCGv_vec n, TCGv_vec m)
14237 tcg_gen_rotli_vec(vece, d, m, 1);
14238 tcg_gen_xor_vec(vece, d, d, n);
14241 void gen_gvec_rax1(unsigned vece, uint32_t rd_ofs, uint32_t rn_ofs,
14242 uint32_t rm_ofs, uint32_t opr_sz, uint32_t max_sz)
14244 static const TCGOpcode vecop_list[] = { INDEX_op_rotli_vec, 0 };
14245 static const GVecGen3 op = {
14246 .fni8 = gen_rax1_i64,
14247 .fniv = gen_rax1_vec,
14248 .opt_opc = vecop_list,
14249 .fno = gen_helper_crypto_rax1,
14250 .vece = MO_64,
14252 tcg_gen_gvec_3(rd_ofs, rn_ofs, rm_ofs, opr_sz, max_sz, &op);
14255 /* Crypto three-reg SHA512
14256 * 31 21 20 16 15 14 13 12 11 10 9 5 4 0
14257 * +-----------------------+------+---+---+-----+--------+------+------+
14258 * | 1 1 0 0 1 1 1 0 0 1 1 | Rm | 1 | O | 0 0 | opcode | Rn | Rd |
14259 * +-----------------------+------+---+---+-----+--------+------+------+
14261 static void disas_crypto_three_reg_sha512(DisasContext *s, uint32_t insn)
14263 int opcode = extract32(insn, 10, 2);
14264 int o = extract32(insn, 14, 1);
14265 int rm = extract32(insn, 16, 5);
14266 int rn = extract32(insn, 5, 5);
14267 int rd = extract32(insn, 0, 5);
14268 bool feature;
14269 gen_helper_gvec_3 *oolfn = NULL;
14270 GVecGen3Fn *gvecfn = NULL;
14272 if (o == 0) {
14273 switch (opcode) {
14274 case 0: /* SHA512H */
14275 feature = dc_isar_feature(aa64_sha512, s);
14276 oolfn = gen_helper_crypto_sha512h;
14277 break;
14278 case 1: /* SHA512H2 */
14279 feature = dc_isar_feature(aa64_sha512, s);
14280 oolfn = gen_helper_crypto_sha512h2;
14281 break;
14282 case 2: /* SHA512SU1 */
14283 feature = dc_isar_feature(aa64_sha512, s);
14284 oolfn = gen_helper_crypto_sha512su1;
14285 break;
14286 case 3: /* RAX1 */
14287 feature = dc_isar_feature(aa64_sha3, s);
14288 gvecfn = gen_gvec_rax1;
14289 break;
14290 default:
14291 g_assert_not_reached();
14293 } else {
14294 switch (opcode) {
14295 case 0: /* SM3PARTW1 */
14296 feature = dc_isar_feature(aa64_sm3, s);
14297 oolfn = gen_helper_crypto_sm3partw1;
14298 break;
14299 case 1: /* SM3PARTW2 */
14300 feature = dc_isar_feature(aa64_sm3, s);
14301 oolfn = gen_helper_crypto_sm3partw2;
14302 break;
14303 case 2: /* SM4EKEY */
14304 feature = dc_isar_feature(aa64_sm4, s);
14305 oolfn = gen_helper_crypto_sm4ekey;
14306 break;
14307 default:
14308 unallocated_encoding(s);
14309 return;
14313 if (!feature) {
14314 unallocated_encoding(s);
14315 return;
14318 if (!fp_access_check(s)) {
14319 return;
14322 if (oolfn) {
14323 gen_gvec_op3_ool(s, true, rd, rn, rm, 0, oolfn);
14324 } else {
14325 gen_gvec_fn3(s, true, rd, rn, rm, gvecfn, MO_64);
14329 /* Crypto two-reg SHA512
14330 * 31 12 11 10 9 5 4 0
14331 * +-----------------------------------------+--------+------+------+
14332 * | 1 1 0 0 1 1 1 0 1 1 0 0 0 0 0 0 1 0 0 0 | opcode | Rn | Rd |
14333 * +-----------------------------------------+--------+------+------+
14335 static void disas_crypto_two_reg_sha512(DisasContext *s, uint32_t insn)
14337 int opcode = extract32(insn, 10, 2);
14338 int rn = extract32(insn, 5, 5);
14339 int rd = extract32(insn, 0, 5);
14340 bool feature;
14342 switch (opcode) {
14343 case 0: /* SHA512SU0 */
14344 feature = dc_isar_feature(aa64_sha512, s);
14345 break;
14346 case 1: /* SM4E */
14347 feature = dc_isar_feature(aa64_sm4, s);
14348 break;
14349 default:
14350 unallocated_encoding(s);
14351 return;
14354 if (!feature) {
14355 unallocated_encoding(s);
14356 return;
14359 if (!fp_access_check(s)) {
14360 return;
14363 switch (opcode) {
14364 case 0: /* SHA512SU0 */
14365 gen_gvec_op2_ool(s, true, rd, rn, 0, gen_helper_crypto_sha512su0);
14366 break;
14367 case 1: /* SM4E */
14368 gen_gvec_op3_ool(s, true, rd, rd, rn, 0, gen_helper_crypto_sm4e);
14369 break;
14370 default:
14371 g_assert_not_reached();
14375 /* Crypto four-register
14376 * 31 23 22 21 20 16 15 14 10 9 5 4 0
14377 * +-------------------+-----+------+---+------+------+------+
14378 * | 1 1 0 0 1 1 1 0 0 | Op0 | Rm | 0 | Ra | Rn | Rd |
14379 * +-------------------+-----+------+---+------+------+------+
14381 static void disas_crypto_four_reg(DisasContext *s, uint32_t insn)
14383 int op0 = extract32(insn, 21, 2);
14384 int rm = extract32(insn, 16, 5);
14385 int ra = extract32(insn, 10, 5);
14386 int rn = extract32(insn, 5, 5);
14387 int rd = extract32(insn, 0, 5);
14388 bool feature;
14390 switch (op0) {
14391 case 0: /* EOR3 */
14392 case 1: /* BCAX */
14393 feature = dc_isar_feature(aa64_sha3, s);
14394 break;
14395 case 2: /* SM3SS1 */
14396 feature = dc_isar_feature(aa64_sm3, s);
14397 break;
14398 default:
14399 unallocated_encoding(s);
14400 return;
14403 if (!feature) {
14404 unallocated_encoding(s);
14405 return;
14408 if (!fp_access_check(s)) {
14409 return;
14412 if (op0 < 2) {
14413 TCGv_i64 tcg_op1, tcg_op2, tcg_op3, tcg_res[2];
14414 int pass;
14416 tcg_op1 = tcg_temp_new_i64();
14417 tcg_op2 = tcg_temp_new_i64();
14418 tcg_op3 = tcg_temp_new_i64();
14419 tcg_res[0] = tcg_temp_new_i64();
14420 tcg_res[1] = tcg_temp_new_i64();
14422 for (pass = 0; pass < 2; pass++) {
14423 read_vec_element(s, tcg_op1, rn, pass, MO_64);
14424 read_vec_element(s, tcg_op2, rm, pass, MO_64);
14425 read_vec_element(s, tcg_op3, ra, pass, MO_64);
14427 if (op0 == 0) {
14428 /* EOR3 */
14429 tcg_gen_xor_i64(tcg_res[pass], tcg_op2, tcg_op3);
14430 } else {
14431 /* BCAX */
14432 tcg_gen_andc_i64(tcg_res[pass], tcg_op2, tcg_op3);
14434 tcg_gen_xor_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
14436 write_vec_element(s, tcg_res[0], rd, 0, MO_64);
14437 write_vec_element(s, tcg_res[1], rd, 1, MO_64);
14439 tcg_temp_free_i64(tcg_op1);
14440 tcg_temp_free_i64(tcg_op2);
14441 tcg_temp_free_i64(tcg_op3);
14442 tcg_temp_free_i64(tcg_res[0]);
14443 tcg_temp_free_i64(tcg_res[1]);
14444 } else {
14445 TCGv_i32 tcg_op1, tcg_op2, tcg_op3, tcg_res, tcg_zero;
14447 tcg_op1 = tcg_temp_new_i32();
14448 tcg_op2 = tcg_temp_new_i32();
14449 tcg_op3 = tcg_temp_new_i32();
14450 tcg_res = tcg_temp_new_i32();
14451 tcg_zero = tcg_constant_i32(0);
14453 read_vec_element_i32(s, tcg_op1, rn, 3, MO_32);
14454 read_vec_element_i32(s, tcg_op2, rm, 3, MO_32);
14455 read_vec_element_i32(s, tcg_op3, ra, 3, MO_32);
14457 tcg_gen_rotri_i32(tcg_res, tcg_op1, 20);
14458 tcg_gen_add_i32(tcg_res, tcg_res, tcg_op2);
14459 tcg_gen_add_i32(tcg_res, tcg_res, tcg_op3);
14460 tcg_gen_rotri_i32(tcg_res, tcg_res, 25);
14462 write_vec_element_i32(s, tcg_zero, rd, 0, MO_32);
14463 write_vec_element_i32(s, tcg_zero, rd, 1, MO_32);
14464 write_vec_element_i32(s, tcg_zero, rd, 2, MO_32);
14465 write_vec_element_i32(s, tcg_res, rd, 3, MO_32);
14467 tcg_temp_free_i32(tcg_op1);
14468 tcg_temp_free_i32(tcg_op2);
14469 tcg_temp_free_i32(tcg_op3);
14470 tcg_temp_free_i32(tcg_res);
14474 /* Crypto XAR
14475 * 31 21 20 16 15 10 9 5 4 0
14476 * +-----------------------+------+--------+------+------+
14477 * | 1 1 0 0 1 1 1 0 1 0 0 | Rm | imm6 | Rn | Rd |
14478 * +-----------------------+------+--------+------+------+
14480 static void disas_crypto_xar(DisasContext *s, uint32_t insn)
14482 int rm = extract32(insn, 16, 5);
14483 int imm6 = extract32(insn, 10, 6);
14484 int rn = extract32(insn, 5, 5);
14485 int rd = extract32(insn, 0, 5);
14487 if (!dc_isar_feature(aa64_sha3, s)) {
14488 unallocated_encoding(s);
14489 return;
14492 if (!fp_access_check(s)) {
14493 return;
14496 gen_gvec_xar(MO_64, vec_full_reg_offset(s, rd),
14497 vec_full_reg_offset(s, rn),
14498 vec_full_reg_offset(s, rm), imm6, 16,
14499 vec_full_reg_size(s));
14502 /* Crypto three-reg imm2
14503 * 31 21 20 16 15 14 13 12 11 10 9 5 4 0
14504 * +-----------------------+------+-----+------+--------+------+------+
14505 * | 1 1 0 0 1 1 1 0 0 1 0 | Rm | 1 0 | imm2 | opcode | Rn | Rd |
14506 * +-----------------------+------+-----+------+--------+------+------+
14508 static void disas_crypto_three_reg_imm2(DisasContext *s, uint32_t insn)
14510 static gen_helper_gvec_3 * const fns[4] = {
14511 gen_helper_crypto_sm3tt1a, gen_helper_crypto_sm3tt1b,
14512 gen_helper_crypto_sm3tt2a, gen_helper_crypto_sm3tt2b,
14514 int opcode = extract32(insn, 10, 2);
14515 int imm2 = extract32(insn, 12, 2);
14516 int rm = extract32(insn, 16, 5);
14517 int rn = extract32(insn, 5, 5);
14518 int rd = extract32(insn, 0, 5);
14520 if (!dc_isar_feature(aa64_sm3, s)) {
14521 unallocated_encoding(s);
14522 return;
14525 if (!fp_access_check(s)) {
14526 return;
14529 gen_gvec_op3_ool(s, true, rd, rn, rm, imm2, fns[opcode]);
14532 /* C3.6 Data processing - SIMD, inc Crypto
14534 * As the decode gets a little complex we are using a table based
14535 * approach for this part of the decode.
14537 static const AArch64DecodeTable data_proc_simd[] = {
14538 /* pattern , mask , fn */
14539 { 0x0e200400, 0x9f200400, disas_simd_three_reg_same },
14540 { 0x0e008400, 0x9f208400, disas_simd_three_reg_same_extra },
14541 { 0x0e200000, 0x9f200c00, disas_simd_three_reg_diff },
14542 { 0x0e200800, 0x9f3e0c00, disas_simd_two_reg_misc },
14543 { 0x0e300800, 0x9f3e0c00, disas_simd_across_lanes },
14544 { 0x0e000400, 0x9fe08400, disas_simd_copy },
14545 { 0x0f000000, 0x9f000400, disas_simd_indexed }, /* vector indexed */
14546 /* simd_mod_imm decode is a subset of simd_shift_imm, so must precede it */
14547 { 0x0f000400, 0x9ff80400, disas_simd_mod_imm },
14548 { 0x0f000400, 0x9f800400, disas_simd_shift_imm },
14549 { 0x0e000000, 0xbf208c00, disas_simd_tb },
14550 { 0x0e000800, 0xbf208c00, disas_simd_zip_trn },
14551 { 0x2e000000, 0xbf208400, disas_simd_ext },
14552 { 0x5e200400, 0xdf200400, disas_simd_scalar_three_reg_same },
14553 { 0x5e008400, 0xdf208400, disas_simd_scalar_three_reg_same_extra },
14554 { 0x5e200000, 0xdf200c00, disas_simd_scalar_three_reg_diff },
14555 { 0x5e200800, 0xdf3e0c00, disas_simd_scalar_two_reg_misc },
14556 { 0x5e300800, 0xdf3e0c00, disas_simd_scalar_pairwise },
14557 { 0x5e000400, 0xdfe08400, disas_simd_scalar_copy },
14558 { 0x5f000000, 0xdf000400, disas_simd_indexed }, /* scalar indexed */
14559 { 0x5f000400, 0xdf800400, disas_simd_scalar_shift_imm },
14560 { 0x4e280800, 0xff3e0c00, disas_crypto_aes },
14561 { 0x5e000000, 0xff208c00, disas_crypto_three_reg_sha },
14562 { 0x5e280800, 0xff3e0c00, disas_crypto_two_reg_sha },
14563 { 0xce608000, 0xffe0b000, disas_crypto_three_reg_sha512 },
14564 { 0xcec08000, 0xfffff000, disas_crypto_two_reg_sha512 },
14565 { 0xce000000, 0xff808000, disas_crypto_four_reg },
14566 { 0xce800000, 0xffe00000, disas_crypto_xar },
14567 { 0xce408000, 0xffe0c000, disas_crypto_three_reg_imm2 },
14568 { 0x0e400400, 0x9f60c400, disas_simd_three_reg_same_fp16 },
14569 { 0x0e780800, 0x8f7e0c00, disas_simd_two_reg_misc_fp16 },
14570 { 0x5e400400, 0xdf60c400, disas_simd_scalar_three_reg_same_fp16 },
14571 { 0x00000000, 0x00000000, NULL }
14574 static void disas_data_proc_simd(DisasContext *s, uint32_t insn)
14576 /* Note that this is called with all non-FP cases from
14577 * table C3-6 so it must UNDEF for entries not specifically
14578 * allocated to instructions in that table.
14580 AArch64DecodeFn *fn = lookup_disas_fn(&data_proc_simd[0], insn);
14581 if (fn) {
14582 fn(s, insn);
14583 } else {
14584 unallocated_encoding(s);
14588 /* C3.6 Data processing - SIMD and floating point */
14589 static void disas_data_proc_simd_fp(DisasContext *s, uint32_t insn)
14591 if (extract32(insn, 28, 1) == 1 && extract32(insn, 30, 1) == 0) {
14592 disas_data_proc_fp(s, insn);
14593 } else {
14594 /* SIMD, including crypto */
14595 disas_data_proc_simd(s, insn);
14600 * Include the generated SME FA64 decoder.
14603 #include "decode-sme-fa64.c.inc"
14605 static bool trans_OK(DisasContext *s, arg_OK *a)
14607 return true;
14610 static bool trans_FAIL(DisasContext *s, arg_OK *a)
14612 s->is_nonstreaming = true;
14613 return true;
14617 * is_guarded_page:
14618 * @env: The cpu environment
14619 * @s: The DisasContext
14621 * Return true if the page is guarded.
14623 static bool is_guarded_page(CPUARMState *env, DisasContext *s)
14625 uint64_t addr = s->base.pc_first;
14626 #ifdef CONFIG_USER_ONLY
14627 return page_get_flags(addr) & PAGE_BTI;
14628 #else
14629 CPUTLBEntryFull *full;
14630 void *host;
14631 int mmu_idx = arm_to_core_mmu_idx(s->mmu_idx);
14632 int flags;
14635 * We test this immediately after reading an insn, which means
14636 * that the TLB entry must be present and valid, and thus this
14637 * access will never raise an exception.
14639 flags = probe_access_full(env, addr, 0, MMU_INST_FETCH, mmu_idx,
14640 false, &host, &full, 0);
14641 assert(!(flags & TLB_INVALID_MASK));
14643 return full->guarded;
14644 #endif
14648 * btype_destination_ok:
14649 * @insn: The instruction at the branch destination
14650 * @bt: SCTLR_ELx.BT
14651 * @btype: PSTATE.BTYPE, and is non-zero
14653 * On a guarded page, there are a limited number of insns
14654 * that may be present at the branch target:
14655 * - branch target identifiers,
14656 * - paciasp, pacibsp,
14657 * - BRK insn
14658 * - HLT insn
14659 * Anything else causes a Branch Target Exception.
14661 * Return true if the branch is compatible, false to raise BTITRAP.
14663 static bool btype_destination_ok(uint32_t insn, bool bt, int btype)
14665 if ((insn & 0xfffff01fu) == 0xd503201fu) {
14666 /* HINT space */
14667 switch (extract32(insn, 5, 7)) {
14668 case 0b011001: /* PACIASP */
14669 case 0b011011: /* PACIBSP */
14671 * If SCTLR_ELx.BT, then PACI*SP are not compatible
14672 * with btype == 3. Otherwise all btype are ok.
14674 return !bt || btype != 3;
14675 case 0b100000: /* BTI */
14676 /* Not compatible with any btype. */
14677 return false;
14678 case 0b100010: /* BTI c */
14679 /* Not compatible with btype == 3 */
14680 return btype != 3;
14681 case 0b100100: /* BTI j */
14682 /* Not compatible with btype == 2 */
14683 return btype != 2;
14684 case 0b100110: /* BTI jc */
14685 /* Compatible with any btype. */
14686 return true;
14688 } else {
14689 switch (insn & 0xffe0001fu) {
14690 case 0xd4200000u: /* BRK */
14691 case 0xd4400000u: /* HLT */
14692 /* Give priority to the breakpoint exception. */
14693 return true;
14696 return false;
14699 static void aarch64_tr_init_disas_context(DisasContextBase *dcbase,
14700 CPUState *cpu)
14702 DisasContext *dc = container_of(dcbase, DisasContext, base);
14703 CPUARMState *env = cpu->env_ptr;
14704 ARMCPU *arm_cpu = env_archcpu(env);
14705 CPUARMTBFlags tb_flags = arm_tbflags_from_tb(dc->base.tb);
14706 int bound, core_mmu_idx;
14708 dc->isar = &arm_cpu->isar;
14709 dc->condjmp = 0;
14710 dc->pc_save = dc->base.pc_first;
14711 dc->aarch64 = true;
14712 dc->thumb = false;
14713 dc->sctlr_b = 0;
14714 dc->be_data = EX_TBFLAG_ANY(tb_flags, BE_DATA) ? MO_BE : MO_LE;
14715 dc->condexec_mask = 0;
14716 dc->condexec_cond = 0;
14717 core_mmu_idx = EX_TBFLAG_ANY(tb_flags, MMUIDX);
14718 dc->mmu_idx = core_to_aa64_mmu_idx(core_mmu_idx);
14719 dc->tbii = EX_TBFLAG_A64(tb_flags, TBII);
14720 dc->tbid = EX_TBFLAG_A64(tb_flags, TBID);
14721 dc->tcma = EX_TBFLAG_A64(tb_flags, TCMA);
14722 dc->current_el = arm_mmu_idx_to_el(dc->mmu_idx);
14723 #if !defined(CONFIG_USER_ONLY)
14724 dc->user = (dc->current_el == 0);
14725 #endif
14726 dc->fp_excp_el = EX_TBFLAG_ANY(tb_flags, FPEXC_EL);
14727 dc->align_mem = EX_TBFLAG_ANY(tb_flags, ALIGN_MEM);
14728 dc->pstate_il = EX_TBFLAG_ANY(tb_flags, PSTATE__IL);
14729 dc->fgt_active = EX_TBFLAG_ANY(tb_flags, FGT_ACTIVE);
14730 dc->fgt_svc = EX_TBFLAG_ANY(tb_flags, FGT_SVC);
14731 dc->fgt_eret = EX_TBFLAG_A64(tb_flags, FGT_ERET);
14732 dc->sve_excp_el = EX_TBFLAG_A64(tb_flags, SVEEXC_EL);
14733 dc->sme_excp_el = EX_TBFLAG_A64(tb_flags, SMEEXC_EL);
14734 dc->vl = (EX_TBFLAG_A64(tb_flags, VL) + 1) * 16;
14735 dc->svl = (EX_TBFLAG_A64(tb_flags, SVL) + 1) * 16;
14736 dc->pauth_active = EX_TBFLAG_A64(tb_flags, PAUTH_ACTIVE);
14737 dc->bt = EX_TBFLAG_A64(tb_flags, BT);
14738 dc->btype = EX_TBFLAG_A64(tb_flags, BTYPE);
14739 dc->unpriv = EX_TBFLAG_A64(tb_flags, UNPRIV);
14740 dc->ata = EX_TBFLAG_A64(tb_flags, ATA);
14741 dc->mte_active[0] = EX_TBFLAG_A64(tb_flags, MTE_ACTIVE);
14742 dc->mte_active[1] = EX_TBFLAG_A64(tb_flags, MTE0_ACTIVE);
14743 dc->pstate_sm = EX_TBFLAG_A64(tb_flags, PSTATE_SM);
14744 dc->pstate_za = EX_TBFLAG_A64(tb_flags, PSTATE_ZA);
14745 dc->sme_trap_nonstreaming = EX_TBFLAG_A64(tb_flags, SME_TRAP_NONSTREAMING);
14746 dc->vec_len = 0;
14747 dc->vec_stride = 0;
14748 dc->cp_regs = arm_cpu->cp_regs;
14749 dc->features = env->features;
14750 dc->dcz_blocksize = arm_cpu->dcz_blocksize;
14752 #ifdef CONFIG_USER_ONLY
14753 /* In sve_probe_page, we assume TBI is enabled. */
14754 tcg_debug_assert(dc->tbid & 1);
14755 #endif
14757 /* Single step state. The code-generation logic here is:
14758 * SS_ACTIVE == 0:
14759 * generate code with no special handling for single-stepping (except
14760 * that anything that can make us go to SS_ACTIVE == 1 must end the TB;
14761 * this happens anyway because those changes are all system register or
14762 * PSTATE writes).
14763 * SS_ACTIVE == 1, PSTATE.SS == 1: (active-not-pending)
14764 * emit code for one insn
14765 * emit code to clear PSTATE.SS
14766 * emit code to generate software step exception for completed step
14767 * end TB (as usual for having generated an exception)
14768 * SS_ACTIVE == 1, PSTATE.SS == 0: (active-pending)
14769 * emit code to generate a software step exception
14770 * end the TB
14772 dc->ss_active = EX_TBFLAG_ANY(tb_flags, SS_ACTIVE);
14773 dc->pstate_ss = EX_TBFLAG_ANY(tb_flags, PSTATE__SS);
14774 dc->is_ldex = false;
14776 /* Bound the number of insns to execute to those left on the page. */
14777 bound = -(dc->base.pc_first | TARGET_PAGE_MASK) / 4;
14779 /* If architectural single step active, limit to 1. */
14780 if (dc->ss_active) {
14781 bound = 1;
14783 dc->base.max_insns = MIN(dc->base.max_insns, bound);
14785 init_tmp_a64_array(dc);
14788 static void aarch64_tr_tb_start(DisasContextBase *db, CPUState *cpu)
14792 static void aarch64_tr_insn_start(DisasContextBase *dcbase, CPUState *cpu)
14794 DisasContext *dc = container_of(dcbase, DisasContext, base);
14795 target_ulong pc_arg = dc->base.pc_next;
14797 if (tb_cflags(dcbase->tb) & CF_PCREL) {
14798 pc_arg &= ~TARGET_PAGE_MASK;
14800 tcg_gen_insn_start(pc_arg, 0, 0);
14801 dc->insn_start = tcg_last_op();
14804 static void aarch64_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)
14806 DisasContext *s = container_of(dcbase, DisasContext, base);
14807 CPUARMState *env = cpu->env_ptr;
14808 uint64_t pc = s->base.pc_next;
14809 uint32_t insn;
14811 /* Singlestep exceptions have the highest priority. */
14812 if (s->ss_active && !s->pstate_ss) {
14813 /* Singlestep state is Active-pending.
14814 * If we're in this state at the start of a TB then either
14815 * a) we just took an exception to an EL which is being debugged
14816 * and this is the first insn in the exception handler
14817 * b) debug exceptions were masked and we just unmasked them
14818 * without changing EL (eg by clearing PSTATE.D)
14819 * In either case we're going to take a swstep exception in the
14820 * "did not step an insn" case, and so the syndrome ISV and EX
14821 * bits should be zero.
14823 assert(s->base.num_insns == 1);
14824 gen_swstep_exception(s, 0, 0);
14825 s->base.is_jmp = DISAS_NORETURN;
14826 s->base.pc_next = pc + 4;
14827 return;
14830 if (pc & 3) {
14832 * PC alignment fault. This has priority over the instruction abort
14833 * that we would receive from a translation fault via arm_ldl_code.
14834 * This should only be possible after an indirect branch, at the
14835 * start of the TB.
14837 assert(s->base.num_insns == 1);
14838 gen_helper_exception_pc_alignment(cpu_env, tcg_constant_tl(pc));
14839 s->base.is_jmp = DISAS_NORETURN;
14840 s->base.pc_next = QEMU_ALIGN_UP(pc, 4);
14841 return;
14844 s->pc_curr = pc;
14845 insn = arm_ldl_code(env, &s->base, pc, s->sctlr_b);
14846 s->insn = insn;
14847 s->base.pc_next = pc + 4;
14849 s->fp_access_checked = false;
14850 s->sve_access_checked = false;
14852 if (s->pstate_il) {
14854 * Illegal execution state. This has priority over BTI
14855 * exceptions, but comes after instruction abort exceptions.
14857 gen_exception_insn(s, 0, EXCP_UDEF, syn_illegalstate());
14858 return;
14861 if (dc_isar_feature(aa64_bti, s)) {
14862 if (s->base.num_insns == 1) {
14864 * At the first insn of the TB, compute s->guarded_page.
14865 * We delayed computing this until successfully reading
14866 * the first insn of the TB, above. This (mostly) ensures
14867 * that the softmmu tlb entry has been populated, and the
14868 * page table GP bit is available.
14870 * Note that we need to compute this even if btype == 0,
14871 * because this value is used for BR instructions later
14872 * where ENV is not available.
14874 s->guarded_page = is_guarded_page(env, s);
14876 /* First insn can have btype set to non-zero. */
14877 tcg_debug_assert(s->btype >= 0);
14880 * Note that the Branch Target Exception has fairly high
14881 * priority -- below debugging exceptions but above most
14882 * everything else. This allows us to handle this now
14883 * instead of waiting until the insn is otherwise decoded.
14885 if (s->btype != 0
14886 && s->guarded_page
14887 && !btype_destination_ok(insn, s->bt, s->btype)) {
14888 gen_exception_insn(s, 0, EXCP_UDEF, syn_btitrap(s->btype));
14889 return;
14891 } else {
14892 /* Not the first insn: btype must be 0. */
14893 tcg_debug_assert(s->btype == 0);
14897 s->is_nonstreaming = false;
14898 if (s->sme_trap_nonstreaming) {
14899 disas_sme_fa64(s, insn);
14902 switch (extract32(insn, 25, 4)) {
14903 case 0x0:
14904 if (!extract32(insn, 31, 1) || !disas_sme(s, insn)) {
14905 unallocated_encoding(s);
14907 break;
14908 case 0x1: case 0x3: /* UNALLOCATED */
14909 unallocated_encoding(s);
14910 break;
14911 case 0x2:
14912 if (!disas_sve(s, insn)) {
14913 unallocated_encoding(s);
14915 break;
14916 case 0x8: case 0x9: /* Data processing - immediate */
14917 disas_data_proc_imm(s, insn);
14918 break;
14919 case 0xa: case 0xb: /* Branch, exception generation and system insns */
14920 disas_b_exc_sys(s, insn);
14921 break;
14922 case 0x4:
14923 case 0x6:
14924 case 0xc:
14925 case 0xe: /* Loads and stores */
14926 disas_ldst(s, insn);
14927 break;
14928 case 0x5:
14929 case 0xd: /* Data processing - register */
14930 disas_data_proc_reg(s, insn);
14931 break;
14932 case 0x7:
14933 case 0xf: /* Data processing - SIMD and floating point */
14934 disas_data_proc_simd_fp(s, insn);
14935 break;
14936 default:
14937 assert(FALSE); /* all 15 cases should be handled above */
14938 break;
14941 /* if we allocated any temporaries, free them here */
14942 free_tmp_a64(s);
14945 * After execution of most insns, btype is reset to 0.
14946 * Note that we set btype == -1 when the insn sets btype.
14948 if (s->btype > 0 && s->base.is_jmp != DISAS_NORETURN) {
14949 reset_btype(s);
14953 static void aarch64_tr_tb_stop(DisasContextBase *dcbase, CPUState *cpu)
14955 DisasContext *dc = container_of(dcbase, DisasContext, base);
14957 if (unlikely(dc->ss_active)) {
14958 /* Note that this means single stepping WFI doesn't halt the CPU.
14959 * For conditional branch insns this is harmless unreachable code as
14960 * gen_goto_tb() has already handled emitting the debug exception
14961 * (and thus a tb-jump is not possible when singlestepping).
14963 switch (dc->base.is_jmp) {
14964 default:
14965 gen_a64_update_pc(dc, 4);
14966 /* fall through */
14967 case DISAS_EXIT:
14968 case DISAS_JUMP:
14969 gen_step_complete_exception(dc);
14970 break;
14971 case DISAS_NORETURN:
14972 break;
14974 } else {
14975 switch (dc->base.is_jmp) {
14976 case DISAS_NEXT:
14977 case DISAS_TOO_MANY:
14978 gen_goto_tb(dc, 1, 4);
14979 break;
14980 default:
14981 case DISAS_UPDATE_EXIT:
14982 gen_a64_update_pc(dc, 4);
14983 /* fall through */
14984 case DISAS_EXIT:
14985 tcg_gen_exit_tb(NULL, 0);
14986 break;
14987 case DISAS_UPDATE_NOCHAIN:
14988 gen_a64_update_pc(dc, 4);
14989 /* fall through */
14990 case DISAS_JUMP:
14991 tcg_gen_lookup_and_goto_ptr();
14992 break;
14993 case DISAS_NORETURN:
14994 case DISAS_SWI:
14995 break;
14996 case DISAS_WFE:
14997 gen_a64_update_pc(dc, 4);
14998 gen_helper_wfe(cpu_env);
14999 break;
15000 case DISAS_YIELD:
15001 gen_a64_update_pc(dc, 4);
15002 gen_helper_yield(cpu_env);
15003 break;
15004 case DISAS_WFI:
15006 * This is a special case because we don't want to just halt
15007 * the CPU if trying to debug across a WFI.
15009 gen_a64_update_pc(dc, 4);
15010 gen_helper_wfi(cpu_env, tcg_constant_i32(4));
15012 * The helper doesn't necessarily throw an exception, but we
15013 * must go back to the main loop to check for interrupts anyway.
15015 tcg_gen_exit_tb(NULL, 0);
15016 break;
15021 static void aarch64_tr_disas_log(const DisasContextBase *dcbase,
15022 CPUState *cpu, FILE *logfile)
15024 DisasContext *dc = container_of(dcbase, DisasContext, base);
15026 fprintf(logfile, "IN: %s\n", lookup_symbol(dc->base.pc_first));
15027 target_disas(logfile, cpu, dc->base.pc_first, dc->base.tb->size);
15030 const TranslatorOps aarch64_translator_ops = {
15031 .init_disas_context = aarch64_tr_init_disas_context,
15032 .tb_start = aarch64_tr_tb_start,
15033 .insn_start = aarch64_tr_insn_start,
15034 .translate_insn = aarch64_tr_translate_insn,
15035 .tb_stop = aarch64_tr_tb_stop,
15036 .disas_log = aarch64_tr_disas_log,