target/arm: Rename gen_exception_insn to gen_exception_insn_el
[qemu/ar7.git] / target / arm / translate-a64.c
blob14bc80dba03ba2ccfd2ef8c066116d9b62bccf3b
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 case ARMMMUIdx_SE10_1:
115 case ARMMMUIdx_SE10_1_PAN:
116 useridx = ARMMMUIdx_SE10_0;
117 break;
118 case ARMMMUIdx_SE20_2:
119 case ARMMMUIdx_SE20_2_PAN:
120 useridx = ARMMMUIdx_SE20_0;
121 break;
122 default:
123 g_assert_not_reached();
126 return arm_to_core_mmu_idx(useridx);
129 static void set_btype_raw(int val)
131 tcg_gen_st_i32(tcg_constant_i32(val), cpu_env,
132 offsetof(CPUARMState, btype));
135 static void set_btype(DisasContext *s, int val)
137 /* BTYPE is a 2-bit field, and 0 should be done with reset_btype. */
138 tcg_debug_assert(val >= 1 && val <= 3);
139 set_btype_raw(val);
140 s->btype = -1;
143 static void reset_btype(DisasContext *s)
145 if (s->btype != 0) {
146 set_btype_raw(0);
147 s->btype = 0;
151 void gen_a64_set_pc_im(uint64_t val)
153 tcg_gen_movi_i64(cpu_pc, val);
157 * Handle Top Byte Ignore (TBI) bits.
159 * If address tagging is enabled via the TCR TBI bits:
160 * + for EL2 and EL3 there is only one TBI bit, and if it is set
161 * then the address is zero-extended, clearing bits [63:56]
162 * + for EL0 and EL1, TBI0 controls addresses with bit 55 == 0
163 * and TBI1 controls addressses with bit 55 == 1.
164 * If the appropriate TBI bit is set for the address then
165 * the address is sign-extended from bit 55 into bits [63:56]
167 * Here We have concatenated TBI{1,0} into tbi.
169 static void gen_top_byte_ignore(DisasContext *s, TCGv_i64 dst,
170 TCGv_i64 src, int tbi)
172 if (tbi == 0) {
173 /* Load unmodified address */
174 tcg_gen_mov_i64(dst, src);
175 } else if (!regime_has_2_ranges(s->mmu_idx)) {
176 /* Force tag byte to all zero */
177 tcg_gen_extract_i64(dst, src, 0, 56);
178 } else {
179 /* Sign-extend from bit 55. */
180 tcg_gen_sextract_i64(dst, src, 0, 56);
182 switch (tbi) {
183 case 1:
184 /* tbi0 but !tbi1: only use the extension if positive */
185 tcg_gen_and_i64(dst, dst, src);
186 break;
187 case 2:
188 /* !tbi0 but tbi1: only use the extension if negative */
189 tcg_gen_or_i64(dst, dst, src);
190 break;
191 case 3:
192 /* tbi0 and tbi1: always use the extension */
193 break;
194 default:
195 g_assert_not_reached();
200 static void gen_a64_set_pc(DisasContext *s, TCGv_i64 src)
203 * If address tagging is enabled for instructions via the TCR TBI bits,
204 * then loading an address into the PC will clear out any tag.
206 gen_top_byte_ignore(s, cpu_pc, src, s->tbii);
210 * Handle MTE and/or TBI.
212 * For TBI, ideally, we would do nothing. Proper behaviour on fault is
213 * for the tag to be present in the FAR_ELx register. But for user-only
214 * mode we do not have a TLB with which to implement this, so we must
215 * remove the top byte now.
217 * Always return a fresh temporary that we can increment independently
218 * of the write-back address.
221 TCGv_i64 clean_data_tbi(DisasContext *s, TCGv_i64 addr)
223 TCGv_i64 clean = new_tmp_a64(s);
224 #ifdef CONFIG_USER_ONLY
225 gen_top_byte_ignore(s, clean, addr, s->tbid);
226 #else
227 tcg_gen_mov_i64(clean, addr);
228 #endif
229 return clean;
232 /* Insert a zero tag into src, with the result at dst. */
233 static void gen_address_with_allocation_tag0(TCGv_i64 dst, TCGv_i64 src)
235 tcg_gen_andi_i64(dst, src, ~MAKE_64BIT_MASK(56, 4));
238 static void gen_probe_access(DisasContext *s, TCGv_i64 ptr,
239 MMUAccessType acc, int log2_size)
241 gen_helper_probe_access(cpu_env, ptr,
242 tcg_constant_i32(acc),
243 tcg_constant_i32(get_mem_index(s)),
244 tcg_constant_i32(1 << log2_size));
248 * For MTE, check a single logical or atomic access. This probes a single
249 * address, the exact one specified. The size and alignment of the access
250 * is not relevant to MTE, per se, but watchpoints do require the size,
251 * and we want to recognize those before making any other changes to state.
253 static TCGv_i64 gen_mte_check1_mmuidx(DisasContext *s, TCGv_i64 addr,
254 bool is_write, bool tag_checked,
255 int log2_size, bool is_unpriv,
256 int core_idx)
258 if (tag_checked && s->mte_active[is_unpriv]) {
259 TCGv_i64 ret;
260 int desc = 0;
262 desc = FIELD_DP32(desc, MTEDESC, MIDX, core_idx);
263 desc = FIELD_DP32(desc, MTEDESC, TBI, s->tbid);
264 desc = FIELD_DP32(desc, MTEDESC, TCMA, s->tcma);
265 desc = FIELD_DP32(desc, MTEDESC, WRITE, is_write);
266 desc = FIELD_DP32(desc, MTEDESC, SIZEM1, (1 << log2_size) - 1);
268 ret = new_tmp_a64(s);
269 gen_helper_mte_check(ret, cpu_env, tcg_constant_i32(desc), addr);
271 return ret;
273 return clean_data_tbi(s, addr);
276 TCGv_i64 gen_mte_check1(DisasContext *s, TCGv_i64 addr, bool is_write,
277 bool tag_checked, int log2_size)
279 return gen_mte_check1_mmuidx(s, addr, is_write, tag_checked, log2_size,
280 false, get_mem_index(s));
284 * For MTE, check multiple logical sequential accesses.
286 TCGv_i64 gen_mte_checkN(DisasContext *s, TCGv_i64 addr, bool is_write,
287 bool tag_checked, int size)
289 if (tag_checked && s->mte_active[0]) {
290 TCGv_i64 ret;
291 int desc = 0;
293 desc = FIELD_DP32(desc, MTEDESC, MIDX, get_mem_index(s));
294 desc = FIELD_DP32(desc, MTEDESC, TBI, s->tbid);
295 desc = FIELD_DP32(desc, MTEDESC, TCMA, s->tcma);
296 desc = FIELD_DP32(desc, MTEDESC, WRITE, is_write);
297 desc = FIELD_DP32(desc, MTEDESC, SIZEM1, size - 1);
299 ret = new_tmp_a64(s);
300 gen_helper_mte_check(ret, cpu_env, tcg_constant_i32(desc), addr);
302 return ret;
304 return clean_data_tbi(s, addr);
307 typedef struct DisasCompare64 {
308 TCGCond cond;
309 TCGv_i64 value;
310 } DisasCompare64;
312 static void a64_test_cc(DisasCompare64 *c64, int cc)
314 DisasCompare c32;
316 arm_test_cc(&c32, cc);
318 /* Sign-extend the 32-bit value so that the GE/LT comparisons work
319 * properly. The NE/EQ comparisons are also fine with this choice. */
320 c64->cond = c32.cond;
321 c64->value = tcg_temp_new_i64();
322 tcg_gen_ext_i32_i64(c64->value, c32.value);
324 arm_free_cc(&c32);
327 static void a64_free_cc(DisasCompare64 *c64)
329 tcg_temp_free_i64(c64->value);
332 static void gen_rebuild_hflags(DisasContext *s)
334 gen_helper_rebuild_hflags_a64(cpu_env, tcg_constant_i32(s->current_el));
337 static void gen_exception_internal(int excp)
339 assert(excp_is_internal(excp));
340 gen_helper_exception_internal(cpu_env, tcg_constant_i32(excp));
343 static void gen_exception_internal_insn(DisasContext *s, uint64_t pc, int excp)
345 gen_a64_set_pc_im(pc);
346 gen_exception_internal(excp);
347 s->base.is_jmp = DISAS_NORETURN;
350 static void gen_exception_bkpt_insn(DisasContext *s, uint32_t syndrome)
352 gen_a64_set_pc_im(s->pc_curr);
353 gen_helper_exception_bkpt_insn(cpu_env, tcg_constant_i32(syndrome));
354 s->base.is_jmp = DISAS_NORETURN;
357 static void gen_step_complete_exception(DisasContext *s)
359 /* We just completed step of an insn. Move from Active-not-pending
360 * to Active-pending, and then also take the swstep exception.
361 * This corresponds to making the (IMPDEF) choice to prioritize
362 * swstep exceptions over asynchronous exceptions taken to an exception
363 * level where debug is disabled. This choice has the advantage that
364 * we do not need to maintain internal state corresponding to the
365 * ISV/EX syndrome bits between completion of the step and generation
366 * of the exception, and our syndrome information is always correct.
368 gen_ss_advance(s);
369 gen_swstep_exception(s, 1, s->is_ldex);
370 s->base.is_jmp = DISAS_NORETURN;
373 static inline bool use_goto_tb(DisasContext *s, uint64_t dest)
375 if (s->ss_active) {
376 return false;
378 return translator_use_goto_tb(&s->base, dest);
381 static inline void gen_goto_tb(DisasContext *s, int n, uint64_t dest)
383 if (use_goto_tb(s, dest)) {
384 tcg_gen_goto_tb(n);
385 gen_a64_set_pc_im(dest);
386 tcg_gen_exit_tb(s->base.tb, n);
387 s->base.is_jmp = DISAS_NORETURN;
388 } else {
389 gen_a64_set_pc_im(dest);
390 if (s->ss_active) {
391 gen_step_complete_exception(s);
392 } else {
393 tcg_gen_lookup_and_goto_ptr();
394 s->base.is_jmp = DISAS_NORETURN;
399 static void init_tmp_a64_array(DisasContext *s)
401 #ifdef CONFIG_DEBUG_TCG
402 memset(s->tmp_a64, 0, sizeof(s->tmp_a64));
403 #endif
404 s->tmp_a64_count = 0;
407 static void free_tmp_a64(DisasContext *s)
409 int i;
410 for (i = 0; i < s->tmp_a64_count; i++) {
411 tcg_temp_free_i64(s->tmp_a64[i]);
413 init_tmp_a64_array(s);
416 TCGv_i64 new_tmp_a64(DisasContext *s)
418 assert(s->tmp_a64_count < TMP_A64_MAX);
419 return s->tmp_a64[s->tmp_a64_count++] = tcg_temp_new_i64();
422 TCGv_i64 new_tmp_a64_local(DisasContext *s)
424 assert(s->tmp_a64_count < TMP_A64_MAX);
425 return s->tmp_a64[s->tmp_a64_count++] = tcg_temp_local_new_i64();
428 TCGv_i64 new_tmp_a64_zero(DisasContext *s)
430 TCGv_i64 t = new_tmp_a64(s);
431 tcg_gen_movi_i64(t, 0);
432 return t;
436 * Register access functions
438 * These functions are used for directly accessing a register in where
439 * changes to the final register value are likely to be made. If you
440 * need to use a register for temporary calculation (e.g. index type
441 * operations) use the read_* form.
443 * B1.2.1 Register mappings
445 * In instruction register encoding 31 can refer to ZR (zero register) or
446 * the SP (stack pointer) depending on context. In QEMU's case we map SP
447 * to cpu_X[31] and ZR accesses to a temporary which can be discarded.
448 * This is the point of the _sp forms.
450 TCGv_i64 cpu_reg(DisasContext *s, int reg)
452 if (reg == 31) {
453 return new_tmp_a64_zero(s);
454 } else {
455 return cpu_X[reg];
459 /* register access for when 31 == SP */
460 TCGv_i64 cpu_reg_sp(DisasContext *s, int reg)
462 return cpu_X[reg];
465 /* read a cpu register in 32bit/64bit mode. Returns a TCGv_i64
466 * representing the register contents. This TCGv is an auto-freed
467 * temporary so it need not be explicitly freed, and may be modified.
469 TCGv_i64 read_cpu_reg(DisasContext *s, int reg, int sf)
471 TCGv_i64 v = new_tmp_a64(s);
472 if (reg != 31) {
473 if (sf) {
474 tcg_gen_mov_i64(v, cpu_X[reg]);
475 } else {
476 tcg_gen_ext32u_i64(v, cpu_X[reg]);
478 } else {
479 tcg_gen_movi_i64(v, 0);
481 return v;
484 TCGv_i64 read_cpu_reg_sp(DisasContext *s, int reg, int sf)
486 TCGv_i64 v = new_tmp_a64(s);
487 if (sf) {
488 tcg_gen_mov_i64(v, cpu_X[reg]);
489 } else {
490 tcg_gen_ext32u_i64(v, cpu_X[reg]);
492 return v;
495 /* Return the offset into CPUARMState of a slice (from
496 * the least significant end) of FP register Qn (ie
497 * Dn, Sn, Hn or Bn).
498 * (Note that this is not the same mapping as for A32; see cpu.h)
500 static inline int fp_reg_offset(DisasContext *s, int regno, MemOp size)
502 return vec_reg_offset(s, regno, 0, size);
505 /* Offset of the high half of the 128 bit vector Qn */
506 static inline int fp_reg_hi_offset(DisasContext *s, int regno)
508 return vec_reg_offset(s, regno, 1, MO_64);
511 /* Convenience accessors for reading and writing single and double
512 * FP registers. Writing clears the upper parts of the associated
513 * 128 bit vector register, as required by the architecture.
514 * Note that unlike the GP register accessors, the values returned
515 * by the read functions must be manually freed.
517 static TCGv_i64 read_fp_dreg(DisasContext *s, int reg)
519 TCGv_i64 v = tcg_temp_new_i64();
521 tcg_gen_ld_i64(v, cpu_env, fp_reg_offset(s, reg, MO_64));
522 return v;
525 static TCGv_i32 read_fp_sreg(DisasContext *s, int reg)
527 TCGv_i32 v = tcg_temp_new_i32();
529 tcg_gen_ld_i32(v, cpu_env, fp_reg_offset(s, reg, MO_32));
530 return v;
533 static TCGv_i32 read_fp_hreg(DisasContext *s, int reg)
535 TCGv_i32 v = tcg_temp_new_i32();
537 tcg_gen_ld16u_i32(v, cpu_env, fp_reg_offset(s, reg, MO_16));
538 return v;
541 /* Clear the bits above an N-bit vector, for N = (is_q ? 128 : 64).
542 * If SVE is not enabled, then there are only 128 bits in the vector.
544 static void clear_vec_high(DisasContext *s, bool is_q, int rd)
546 unsigned ofs = fp_reg_offset(s, rd, MO_64);
547 unsigned vsz = vec_full_reg_size(s);
549 /* Nop move, with side effect of clearing the tail. */
550 tcg_gen_gvec_mov(MO_64, ofs, ofs, is_q ? 16 : 8, vsz);
553 void write_fp_dreg(DisasContext *s, int reg, TCGv_i64 v)
555 unsigned ofs = fp_reg_offset(s, reg, MO_64);
557 tcg_gen_st_i64(v, cpu_env, ofs);
558 clear_vec_high(s, false, reg);
561 static void write_fp_sreg(DisasContext *s, int reg, TCGv_i32 v)
563 TCGv_i64 tmp = tcg_temp_new_i64();
565 tcg_gen_extu_i32_i64(tmp, v);
566 write_fp_dreg(s, reg, tmp);
567 tcg_temp_free_i64(tmp);
570 /* Expand a 2-operand AdvSIMD vector operation using an expander function. */
571 static void gen_gvec_fn2(DisasContext *s, bool is_q, int rd, int rn,
572 GVecGen2Fn *gvec_fn, int vece)
574 gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
575 is_q ? 16 : 8, vec_full_reg_size(s));
578 /* Expand a 2-operand + immediate AdvSIMD vector operation using
579 * an expander function.
581 static void gen_gvec_fn2i(DisasContext *s, bool is_q, int rd, int rn,
582 int64_t imm, GVecGen2iFn *gvec_fn, int vece)
584 gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
585 imm, is_q ? 16 : 8, vec_full_reg_size(s));
588 /* Expand a 3-operand AdvSIMD vector operation using an expander function. */
589 static void gen_gvec_fn3(DisasContext *s, bool is_q, int rd, int rn, int rm,
590 GVecGen3Fn *gvec_fn, int vece)
592 gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
593 vec_full_reg_offset(s, rm), is_q ? 16 : 8, vec_full_reg_size(s));
596 /* Expand a 4-operand AdvSIMD vector operation using an expander function. */
597 static void gen_gvec_fn4(DisasContext *s, bool is_q, int rd, int rn, int rm,
598 int rx, GVecGen4Fn *gvec_fn, int vece)
600 gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
601 vec_full_reg_offset(s, rm), vec_full_reg_offset(s, rx),
602 is_q ? 16 : 8, vec_full_reg_size(s));
605 /* Expand a 2-operand operation using an out-of-line helper. */
606 static void gen_gvec_op2_ool(DisasContext *s, bool is_q, int rd,
607 int rn, int data, gen_helper_gvec_2 *fn)
609 tcg_gen_gvec_2_ool(vec_full_reg_offset(s, rd),
610 vec_full_reg_offset(s, rn),
611 is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
614 /* Expand a 3-operand operation using an out-of-line helper. */
615 static void gen_gvec_op3_ool(DisasContext *s, bool is_q, int rd,
616 int rn, int rm, int data, gen_helper_gvec_3 *fn)
618 tcg_gen_gvec_3_ool(vec_full_reg_offset(s, rd),
619 vec_full_reg_offset(s, rn),
620 vec_full_reg_offset(s, rm),
621 is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
624 /* Expand a 3-operand + fpstatus pointer + simd data value operation using
625 * an out-of-line helper.
627 static void gen_gvec_op3_fpst(DisasContext *s, bool is_q, int rd, int rn,
628 int rm, bool is_fp16, int data,
629 gen_helper_gvec_3_ptr *fn)
631 TCGv_ptr fpst = fpstatus_ptr(is_fp16 ? FPST_FPCR_F16 : FPST_FPCR);
632 tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
633 vec_full_reg_offset(s, rn),
634 vec_full_reg_offset(s, rm), fpst,
635 is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
636 tcg_temp_free_ptr(fpst);
639 /* Expand a 3-operand + qc + operation using an out-of-line helper. */
640 static void gen_gvec_op3_qc(DisasContext *s, bool is_q, int rd, int rn,
641 int rm, gen_helper_gvec_3_ptr *fn)
643 TCGv_ptr qc_ptr = tcg_temp_new_ptr();
645 tcg_gen_addi_ptr(qc_ptr, cpu_env, offsetof(CPUARMState, vfp.qc));
646 tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
647 vec_full_reg_offset(s, rn),
648 vec_full_reg_offset(s, rm), qc_ptr,
649 is_q ? 16 : 8, vec_full_reg_size(s), 0, fn);
650 tcg_temp_free_ptr(qc_ptr);
653 /* Expand a 4-operand operation using an out-of-line helper. */
654 static void gen_gvec_op4_ool(DisasContext *s, bool is_q, int rd, int rn,
655 int rm, int ra, int data, gen_helper_gvec_4 *fn)
657 tcg_gen_gvec_4_ool(vec_full_reg_offset(s, rd),
658 vec_full_reg_offset(s, rn),
659 vec_full_reg_offset(s, rm),
660 vec_full_reg_offset(s, ra),
661 is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
665 * Expand a 4-operand + fpstatus pointer + simd data value operation using
666 * an out-of-line helper.
668 static void gen_gvec_op4_fpst(DisasContext *s, bool is_q, int rd, int rn,
669 int rm, int ra, bool is_fp16, int data,
670 gen_helper_gvec_4_ptr *fn)
672 TCGv_ptr fpst = fpstatus_ptr(is_fp16 ? FPST_FPCR_F16 : FPST_FPCR);
673 tcg_gen_gvec_4_ptr(vec_full_reg_offset(s, rd),
674 vec_full_reg_offset(s, rn),
675 vec_full_reg_offset(s, rm),
676 vec_full_reg_offset(s, ra), fpst,
677 is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
678 tcg_temp_free_ptr(fpst);
681 /* Set ZF and NF based on a 64 bit result. This is alas fiddlier
682 * than the 32 bit equivalent.
684 static inline void gen_set_NZ64(TCGv_i64 result)
686 tcg_gen_extr_i64_i32(cpu_ZF, cpu_NF, result);
687 tcg_gen_or_i32(cpu_ZF, cpu_ZF, cpu_NF);
690 /* Set NZCV as for a logical operation: NZ as per result, CV cleared. */
691 static inline void gen_logic_CC(int sf, TCGv_i64 result)
693 if (sf) {
694 gen_set_NZ64(result);
695 } else {
696 tcg_gen_extrl_i64_i32(cpu_ZF, result);
697 tcg_gen_mov_i32(cpu_NF, cpu_ZF);
699 tcg_gen_movi_i32(cpu_CF, 0);
700 tcg_gen_movi_i32(cpu_VF, 0);
703 /* dest = T0 + T1; compute C, N, V and Z flags */
704 static void gen_add_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
706 if (sf) {
707 TCGv_i64 result, flag, tmp;
708 result = tcg_temp_new_i64();
709 flag = tcg_temp_new_i64();
710 tmp = tcg_temp_new_i64();
712 tcg_gen_movi_i64(tmp, 0);
713 tcg_gen_add2_i64(result, flag, t0, tmp, t1, tmp);
715 tcg_gen_extrl_i64_i32(cpu_CF, flag);
717 gen_set_NZ64(result);
719 tcg_gen_xor_i64(flag, result, t0);
720 tcg_gen_xor_i64(tmp, t0, t1);
721 tcg_gen_andc_i64(flag, flag, tmp);
722 tcg_temp_free_i64(tmp);
723 tcg_gen_extrh_i64_i32(cpu_VF, flag);
725 tcg_gen_mov_i64(dest, result);
726 tcg_temp_free_i64(result);
727 tcg_temp_free_i64(flag);
728 } else {
729 /* 32 bit arithmetic */
730 TCGv_i32 t0_32 = tcg_temp_new_i32();
731 TCGv_i32 t1_32 = tcg_temp_new_i32();
732 TCGv_i32 tmp = tcg_temp_new_i32();
734 tcg_gen_movi_i32(tmp, 0);
735 tcg_gen_extrl_i64_i32(t0_32, t0);
736 tcg_gen_extrl_i64_i32(t1_32, t1);
737 tcg_gen_add2_i32(cpu_NF, cpu_CF, t0_32, tmp, t1_32, tmp);
738 tcg_gen_mov_i32(cpu_ZF, cpu_NF);
739 tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
740 tcg_gen_xor_i32(tmp, t0_32, t1_32);
741 tcg_gen_andc_i32(cpu_VF, cpu_VF, tmp);
742 tcg_gen_extu_i32_i64(dest, cpu_NF);
744 tcg_temp_free_i32(tmp);
745 tcg_temp_free_i32(t0_32);
746 tcg_temp_free_i32(t1_32);
750 /* dest = T0 - T1; compute C, N, V and Z flags */
751 static void gen_sub_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
753 if (sf) {
754 /* 64 bit arithmetic */
755 TCGv_i64 result, flag, tmp;
757 result = tcg_temp_new_i64();
758 flag = tcg_temp_new_i64();
759 tcg_gen_sub_i64(result, t0, t1);
761 gen_set_NZ64(result);
763 tcg_gen_setcond_i64(TCG_COND_GEU, flag, t0, t1);
764 tcg_gen_extrl_i64_i32(cpu_CF, flag);
766 tcg_gen_xor_i64(flag, result, t0);
767 tmp = tcg_temp_new_i64();
768 tcg_gen_xor_i64(tmp, t0, t1);
769 tcg_gen_and_i64(flag, flag, tmp);
770 tcg_temp_free_i64(tmp);
771 tcg_gen_extrh_i64_i32(cpu_VF, flag);
772 tcg_gen_mov_i64(dest, result);
773 tcg_temp_free_i64(flag);
774 tcg_temp_free_i64(result);
775 } else {
776 /* 32 bit arithmetic */
777 TCGv_i32 t0_32 = tcg_temp_new_i32();
778 TCGv_i32 t1_32 = tcg_temp_new_i32();
779 TCGv_i32 tmp;
781 tcg_gen_extrl_i64_i32(t0_32, t0);
782 tcg_gen_extrl_i64_i32(t1_32, t1);
783 tcg_gen_sub_i32(cpu_NF, t0_32, t1_32);
784 tcg_gen_mov_i32(cpu_ZF, cpu_NF);
785 tcg_gen_setcond_i32(TCG_COND_GEU, cpu_CF, t0_32, t1_32);
786 tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
787 tmp = tcg_temp_new_i32();
788 tcg_gen_xor_i32(tmp, t0_32, t1_32);
789 tcg_temp_free_i32(t0_32);
790 tcg_temp_free_i32(t1_32);
791 tcg_gen_and_i32(cpu_VF, cpu_VF, tmp);
792 tcg_temp_free_i32(tmp);
793 tcg_gen_extu_i32_i64(dest, cpu_NF);
797 /* dest = T0 + T1 + CF; do not compute flags. */
798 static void gen_adc(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
800 TCGv_i64 flag = tcg_temp_new_i64();
801 tcg_gen_extu_i32_i64(flag, cpu_CF);
802 tcg_gen_add_i64(dest, t0, t1);
803 tcg_gen_add_i64(dest, dest, flag);
804 tcg_temp_free_i64(flag);
806 if (!sf) {
807 tcg_gen_ext32u_i64(dest, dest);
811 /* dest = T0 + T1 + CF; compute C, N, V and Z flags. */
812 static void gen_adc_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
814 if (sf) {
815 TCGv_i64 result = tcg_temp_new_i64();
816 TCGv_i64 cf_64 = tcg_temp_new_i64();
817 TCGv_i64 vf_64 = tcg_temp_new_i64();
818 TCGv_i64 tmp = tcg_temp_new_i64();
819 TCGv_i64 zero = tcg_constant_i64(0);
821 tcg_gen_extu_i32_i64(cf_64, cpu_CF);
822 tcg_gen_add2_i64(result, cf_64, t0, zero, cf_64, zero);
823 tcg_gen_add2_i64(result, cf_64, result, cf_64, t1, zero);
824 tcg_gen_extrl_i64_i32(cpu_CF, cf_64);
825 gen_set_NZ64(result);
827 tcg_gen_xor_i64(vf_64, result, t0);
828 tcg_gen_xor_i64(tmp, t0, t1);
829 tcg_gen_andc_i64(vf_64, vf_64, tmp);
830 tcg_gen_extrh_i64_i32(cpu_VF, vf_64);
832 tcg_gen_mov_i64(dest, result);
834 tcg_temp_free_i64(tmp);
835 tcg_temp_free_i64(vf_64);
836 tcg_temp_free_i64(cf_64);
837 tcg_temp_free_i64(result);
838 } else {
839 TCGv_i32 t0_32 = tcg_temp_new_i32();
840 TCGv_i32 t1_32 = tcg_temp_new_i32();
841 TCGv_i32 tmp = tcg_temp_new_i32();
842 TCGv_i32 zero = tcg_constant_i32(0);
844 tcg_gen_extrl_i64_i32(t0_32, t0);
845 tcg_gen_extrl_i64_i32(t1_32, t1);
846 tcg_gen_add2_i32(cpu_NF, cpu_CF, t0_32, zero, cpu_CF, zero);
847 tcg_gen_add2_i32(cpu_NF, cpu_CF, cpu_NF, cpu_CF, t1_32, zero);
849 tcg_gen_mov_i32(cpu_ZF, cpu_NF);
850 tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
851 tcg_gen_xor_i32(tmp, t0_32, t1_32);
852 tcg_gen_andc_i32(cpu_VF, cpu_VF, tmp);
853 tcg_gen_extu_i32_i64(dest, cpu_NF);
855 tcg_temp_free_i32(tmp);
856 tcg_temp_free_i32(t1_32);
857 tcg_temp_free_i32(t0_32);
862 * Load/Store generators
866 * Store from GPR register to memory.
868 static void do_gpr_st_memidx(DisasContext *s, TCGv_i64 source,
869 TCGv_i64 tcg_addr, MemOp memop, int memidx,
870 bool iss_valid,
871 unsigned int iss_srt,
872 bool iss_sf, bool iss_ar)
874 memop = finalize_memop(s, memop);
875 tcg_gen_qemu_st_i64(source, tcg_addr, memidx, memop);
877 if (iss_valid) {
878 uint32_t syn;
880 syn = syn_data_abort_with_iss(0,
881 (memop & MO_SIZE),
882 false,
883 iss_srt,
884 iss_sf,
885 iss_ar,
886 0, 0, 0, 0, 0, false);
887 disas_set_insn_syndrome(s, syn);
891 static void do_gpr_st(DisasContext *s, TCGv_i64 source,
892 TCGv_i64 tcg_addr, MemOp memop,
893 bool iss_valid,
894 unsigned int iss_srt,
895 bool iss_sf, bool iss_ar)
897 do_gpr_st_memidx(s, source, tcg_addr, memop, get_mem_index(s),
898 iss_valid, iss_srt, iss_sf, iss_ar);
902 * Load from memory to GPR register
904 static void do_gpr_ld_memidx(DisasContext *s, TCGv_i64 dest, TCGv_i64 tcg_addr,
905 MemOp memop, bool extend, int memidx,
906 bool iss_valid, unsigned int iss_srt,
907 bool iss_sf, bool iss_ar)
909 memop = finalize_memop(s, memop);
910 tcg_gen_qemu_ld_i64(dest, tcg_addr, memidx, memop);
912 if (extend && (memop & MO_SIGN)) {
913 g_assert((memop & MO_SIZE) <= MO_32);
914 tcg_gen_ext32u_i64(dest, dest);
917 if (iss_valid) {
918 uint32_t syn;
920 syn = syn_data_abort_with_iss(0,
921 (memop & MO_SIZE),
922 (memop & MO_SIGN) != 0,
923 iss_srt,
924 iss_sf,
925 iss_ar,
926 0, 0, 0, 0, 0, false);
927 disas_set_insn_syndrome(s, syn);
931 static void do_gpr_ld(DisasContext *s, TCGv_i64 dest, TCGv_i64 tcg_addr,
932 MemOp memop, bool extend,
933 bool iss_valid, unsigned int iss_srt,
934 bool iss_sf, bool iss_ar)
936 do_gpr_ld_memidx(s, dest, tcg_addr, memop, extend, get_mem_index(s),
937 iss_valid, iss_srt, iss_sf, iss_ar);
941 * Store from FP register to memory
943 static void do_fp_st(DisasContext *s, int srcidx, TCGv_i64 tcg_addr, int size)
945 /* This writes the bottom N bits of a 128 bit wide vector to memory */
946 TCGv_i64 tmplo = tcg_temp_new_i64();
947 MemOp mop;
949 tcg_gen_ld_i64(tmplo, cpu_env, fp_reg_offset(s, srcidx, MO_64));
951 if (size < 4) {
952 mop = finalize_memop(s, size);
953 tcg_gen_qemu_st_i64(tmplo, tcg_addr, get_mem_index(s), mop);
954 } else {
955 bool be = s->be_data == MO_BE;
956 TCGv_i64 tcg_hiaddr = tcg_temp_new_i64();
957 TCGv_i64 tmphi = tcg_temp_new_i64();
959 tcg_gen_ld_i64(tmphi, cpu_env, fp_reg_hi_offset(s, srcidx));
961 mop = s->be_data | MO_UQ;
962 tcg_gen_qemu_st_i64(be ? tmphi : tmplo, tcg_addr, get_mem_index(s),
963 mop | (s->align_mem ? MO_ALIGN_16 : 0));
964 tcg_gen_addi_i64(tcg_hiaddr, tcg_addr, 8);
965 tcg_gen_qemu_st_i64(be ? tmplo : tmphi, tcg_hiaddr,
966 get_mem_index(s), mop);
968 tcg_temp_free_i64(tcg_hiaddr);
969 tcg_temp_free_i64(tmphi);
972 tcg_temp_free_i64(tmplo);
976 * Load from memory to FP register
978 static void do_fp_ld(DisasContext *s, int destidx, TCGv_i64 tcg_addr, int size)
980 /* This always zero-extends and writes to a full 128 bit wide vector */
981 TCGv_i64 tmplo = tcg_temp_new_i64();
982 TCGv_i64 tmphi = NULL;
983 MemOp mop;
985 if (size < 4) {
986 mop = finalize_memop(s, size);
987 tcg_gen_qemu_ld_i64(tmplo, tcg_addr, get_mem_index(s), mop);
988 } else {
989 bool be = s->be_data == MO_BE;
990 TCGv_i64 tcg_hiaddr;
992 tmphi = tcg_temp_new_i64();
993 tcg_hiaddr = tcg_temp_new_i64();
995 mop = s->be_data | MO_UQ;
996 tcg_gen_qemu_ld_i64(be ? tmphi : tmplo, tcg_addr, get_mem_index(s),
997 mop | (s->align_mem ? MO_ALIGN_16 : 0));
998 tcg_gen_addi_i64(tcg_hiaddr, tcg_addr, 8);
999 tcg_gen_qemu_ld_i64(be ? tmplo : tmphi, tcg_hiaddr,
1000 get_mem_index(s), mop);
1001 tcg_temp_free_i64(tcg_hiaddr);
1004 tcg_gen_st_i64(tmplo, cpu_env, fp_reg_offset(s, destidx, MO_64));
1005 tcg_temp_free_i64(tmplo);
1007 if (tmphi) {
1008 tcg_gen_st_i64(tmphi, cpu_env, fp_reg_hi_offset(s, destidx));
1009 tcg_temp_free_i64(tmphi);
1011 clear_vec_high(s, tmphi != NULL, destidx);
1015 * Vector load/store helpers.
1017 * The principal difference between this and a FP load is that we don't
1018 * zero extend as we are filling a partial chunk of the vector register.
1019 * These functions don't support 128 bit loads/stores, which would be
1020 * normal load/store operations.
1022 * The _i32 versions are useful when operating on 32 bit quantities
1023 * (eg for floating point single or using Neon helper functions).
1026 /* Get value of an element within a vector register */
1027 static void read_vec_element(DisasContext *s, TCGv_i64 tcg_dest, int srcidx,
1028 int element, MemOp memop)
1030 int vect_off = vec_reg_offset(s, srcidx, element, memop & MO_SIZE);
1031 switch ((unsigned)memop) {
1032 case MO_8:
1033 tcg_gen_ld8u_i64(tcg_dest, cpu_env, vect_off);
1034 break;
1035 case MO_16:
1036 tcg_gen_ld16u_i64(tcg_dest, cpu_env, vect_off);
1037 break;
1038 case MO_32:
1039 tcg_gen_ld32u_i64(tcg_dest, cpu_env, vect_off);
1040 break;
1041 case MO_8|MO_SIGN:
1042 tcg_gen_ld8s_i64(tcg_dest, cpu_env, vect_off);
1043 break;
1044 case MO_16|MO_SIGN:
1045 tcg_gen_ld16s_i64(tcg_dest, cpu_env, vect_off);
1046 break;
1047 case MO_32|MO_SIGN:
1048 tcg_gen_ld32s_i64(tcg_dest, cpu_env, vect_off);
1049 break;
1050 case MO_64:
1051 case MO_64|MO_SIGN:
1052 tcg_gen_ld_i64(tcg_dest, cpu_env, vect_off);
1053 break;
1054 default:
1055 g_assert_not_reached();
1059 static void read_vec_element_i32(DisasContext *s, TCGv_i32 tcg_dest, int srcidx,
1060 int element, MemOp memop)
1062 int vect_off = vec_reg_offset(s, srcidx, element, memop & MO_SIZE);
1063 switch (memop) {
1064 case MO_8:
1065 tcg_gen_ld8u_i32(tcg_dest, cpu_env, vect_off);
1066 break;
1067 case MO_16:
1068 tcg_gen_ld16u_i32(tcg_dest, cpu_env, vect_off);
1069 break;
1070 case MO_8|MO_SIGN:
1071 tcg_gen_ld8s_i32(tcg_dest, cpu_env, vect_off);
1072 break;
1073 case MO_16|MO_SIGN:
1074 tcg_gen_ld16s_i32(tcg_dest, cpu_env, vect_off);
1075 break;
1076 case MO_32:
1077 case MO_32|MO_SIGN:
1078 tcg_gen_ld_i32(tcg_dest, cpu_env, vect_off);
1079 break;
1080 default:
1081 g_assert_not_reached();
1085 /* Set value of an element within a vector register */
1086 static void write_vec_element(DisasContext *s, TCGv_i64 tcg_src, int destidx,
1087 int element, MemOp memop)
1089 int vect_off = vec_reg_offset(s, destidx, element, memop & MO_SIZE);
1090 switch (memop) {
1091 case MO_8:
1092 tcg_gen_st8_i64(tcg_src, cpu_env, vect_off);
1093 break;
1094 case MO_16:
1095 tcg_gen_st16_i64(tcg_src, cpu_env, vect_off);
1096 break;
1097 case MO_32:
1098 tcg_gen_st32_i64(tcg_src, cpu_env, vect_off);
1099 break;
1100 case MO_64:
1101 tcg_gen_st_i64(tcg_src, cpu_env, vect_off);
1102 break;
1103 default:
1104 g_assert_not_reached();
1108 static void write_vec_element_i32(DisasContext *s, TCGv_i32 tcg_src,
1109 int destidx, int element, MemOp memop)
1111 int vect_off = vec_reg_offset(s, destidx, element, memop & MO_SIZE);
1112 switch (memop) {
1113 case MO_8:
1114 tcg_gen_st8_i32(tcg_src, cpu_env, vect_off);
1115 break;
1116 case MO_16:
1117 tcg_gen_st16_i32(tcg_src, cpu_env, vect_off);
1118 break;
1119 case MO_32:
1120 tcg_gen_st_i32(tcg_src, cpu_env, vect_off);
1121 break;
1122 default:
1123 g_assert_not_reached();
1127 /* Store from vector register to memory */
1128 static void do_vec_st(DisasContext *s, int srcidx, int element,
1129 TCGv_i64 tcg_addr, MemOp mop)
1131 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
1133 read_vec_element(s, tcg_tmp, srcidx, element, mop & MO_SIZE);
1134 tcg_gen_qemu_st_i64(tcg_tmp, tcg_addr, get_mem_index(s), mop);
1136 tcg_temp_free_i64(tcg_tmp);
1139 /* Load from memory to vector register */
1140 static void do_vec_ld(DisasContext *s, int destidx, int element,
1141 TCGv_i64 tcg_addr, MemOp mop)
1143 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
1145 tcg_gen_qemu_ld_i64(tcg_tmp, tcg_addr, get_mem_index(s), mop);
1146 write_vec_element(s, tcg_tmp, destidx, element, mop & MO_SIZE);
1148 tcg_temp_free_i64(tcg_tmp);
1151 /* Check that FP/Neon access is enabled. If it is, return
1152 * true. If not, emit code to generate an appropriate exception,
1153 * and return false; the caller should not emit any code for
1154 * the instruction. Note that this check must happen after all
1155 * unallocated-encoding checks (otherwise the syndrome information
1156 * for the resulting exception will be incorrect).
1158 static bool fp_access_check(DisasContext *s)
1160 if (s->fp_excp_el) {
1161 assert(!s->fp_access_checked);
1162 s->fp_access_checked = true;
1164 gen_exception_insn_el(s, s->pc_curr, EXCP_UDEF,
1165 syn_fp_access_trap(1, 0xe, false, 0),
1166 s->fp_excp_el);
1167 return false;
1169 s->fp_access_checked = true;
1170 return true;
1173 /* Check that SVE access is enabled. If it is, return true.
1174 * If not, emit code to generate an appropriate exception and return false.
1176 bool sve_access_check(DisasContext *s)
1178 if (s->sve_excp_el) {
1179 assert(!s->sve_access_checked);
1180 s->sve_access_checked = true;
1182 gen_exception_insn_el(s, s->pc_curr, EXCP_UDEF,
1183 syn_sve_access_trap(), s->sve_excp_el);
1184 return false;
1186 s->sve_access_checked = true;
1187 return fp_access_check(s);
1191 * This utility function is for doing register extension with an
1192 * optional shift. You will likely want to pass a temporary for the
1193 * destination register. See DecodeRegExtend() in the ARM ARM.
1195 static void ext_and_shift_reg(TCGv_i64 tcg_out, TCGv_i64 tcg_in,
1196 int option, unsigned int shift)
1198 int extsize = extract32(option, 0, 2);
1199 bool is_signed = extract32(option, 2, 1);
1201 if (is_signed) {
1202 switch (extsize) {
1203 case 0:
1204 tcg_gen_ext8s_i64(tcg_out, tcg_in);
1205 break;
1206 case 1:
1207 tcg_gen_ext16s_i64(tcg_out, tcg_in);
1208 break;
1209 case 2:
1210 tcg_gen_ext32s_i64(tcg_out, tcg_in);
1211 break;
1212 case 3:
1213 tcg_gen_mov_i64(tcg_out, tcg_in);
1214 break;
1216 } else {
1217 switch (extsize) {
1218 case 0:
1219 tcg_gen_ext8u_i64(tcg_out, tcg_in);
1220 break;
1221 case 1:
1222 tcg_gen_ext16u_i64(tcg_out, tcg_in);
1223 break;
1224 case 2:
1225 tcg_gen_ext32u_i64(tcg_out, tcg_in);
1226 break;
1227 case 3:
1228 tcg_gen_mov_i64(tcg_out, tcg_in);
1229 break;
1233 if (shift) {
1234 tcg_gen_shli_i64(tcg_out, tcg_out, shift);
1238 static inline void gen_check_sp_alignment(DisasContext *s)
1240 /* The AArch64 architecture mandates that (if enabled via PSTATE
1241 * or SCTLR bits) there is a check that SP is 16-aligned on every
1242 * SP-relative load or store (with an exception generated if it is not).
1243 * In line with general QEMU practice regarding misaligned accesses,
1244 * we omit these checks for the sake of guest program performance.
1245 * This function is provided as a hook so we can more easily add these
1246 * checks in future (possibly as a "favour catching guest program bugs
1247 * over speed" user selectable option).
1252 * This provides a simple table based table lookup decoder. It is
1253 * intended to be used when the relevant bits for decode are too
1254 * awkwardly placed and switch/if based logic would be confusing and
1255 * deeply nested. Since it's a linear search through the table, tables
1256 * should be kept small.
1258 * It returns the first handler where insn & mask == pattern, or
1259 * NULL if there is no match.
1260 * The table is terminated by an empty mask (i.e. 0)
1262 static inline AArch64DecodeFn *lookup_disas_fn(const AArch64DecodeTable *table,
1263 uint32_t insn)
1265 const AArch64DecodeTable *tptr = table;
1267 while (tptr->mask) {
1268 if ((insn & tptr->mask) == tptr->pattern) {
1269 return tptr->disas_fn;
1271 tptr++;
1273 return NULL;
1277 * The instruction disassembly implemented here matches
1278 * the instruction encoding classifications in chapter C4
1279 * of the ARM Architecture Reference Manual (DDI0487B_a);
1280 * classification names and decode diagrams here should generally
1281 * match up with those in the manual.
1284 /* Unconditional branch (immediate)
1285 * 31 30 26 25 0
1286 * +----+-----------+-------------------------------------+
1287 * | op | 0 0 1 0 1 | imm26 |
1288 * +----+-----------+-------------------------------------+
1290 static void disas_uncond_b_imm(DisasContext *s, uint32_t insn)
1292 uint64_t addr = s->pc_curr + sextract32(insn, 0, 26) * 4;
1294 if (insn & (1U << 31)) {
1295 /* BL Branch with link */
1296 tcg_gen_movi_i64(cpu_reg(s, 30), s->base.pc_next);
1299 /* B Branch / BL Branch with link */
1300 reset_btype(s);
1301 gen_goto_tb(s, 0, addr);
1304 /* Compare and branch (immediate)
1305 * 31 30 25 24 23 5 4 0
1306 * +----+-------------+----+---------------------+--------+
1307 * | sf | 0 1 1 0 1 0 | op | imm19 | Rt |
1308 * +----+-------------+----+---------------------+--------+
1310 static void disas_comp_b_imm(DisasContext *s, uint32_t insn)
1312 unsigned int sf, op, rt;
1313 uint64_t addr;
1314 TCGLabel *label_match;
1315 TCGv_i64 tcg_cmp;
1317 sf = extract32(insn, 31, 1);
1318 op = extract32(insn, 24, 1); /* 0: CBZ; 1: CBNZ */
1319 rt = extract32(insn, 0, 5);
1320 addr = s->pc_curr + sextract32(insn, 5, 19) * 4;
1322 tcg_cmp = read_cpu_reg(s, rt, sf);
1323 label_match = gen_new_label();
1325 reset_btype(s);
1326 tcg_gen_brcondi_i64(op ? TCG_COND_NE : TCG_COND_EQ,
1327 tcg_cmp, 0, label_match);
1329 gen_goto_tb(s, 0, s->base.pc_next);
1330 gen_set_label(label_match);
1331 gen_goto_tb(s, 1, addr);
1334 /* Test and branch (immediate)
1335 * 31 30 25 24 23 19 18 5 4 0
1336 * +----+-------------+----+-------+-------------+------+
1337 * | b5 | 0 1 1 0 1 1 | op | b40 | imm14 | Rt |
1338 * +----+-------------+----+-------+-------------+------+
1340 static void disas_test_b_imm(DisasContext *s, uint32_t insn)
1342 unsigned int bit_pos, op, rt;
1343 uint64_t addr;
1344 TCGLabel *label_match;
1345 TCGv_i64 tcg_cmp;
1347 bit_pos = (extract32(insn, 31, 1) << 5) | extract32(insn, 19, 5);
1348 op = extract32(insn, 24, 1); /* 0: TBZ; 1: TBNZ */
1349 addr = s->pc_curr + sextract32(insn, 5, 14) * 4;
1350 rt = extract32(insn, 0, 5);
1352 tcg_cmp = tcg_temp_new_i64();
1353 tcg_gen_andi_i64(tcg_cmp, cpu_reg(s, rt), (1ULL << bit_pos));
1354 label_match = gen_new_label();
1356 reset_btype(s);
1357 tcg_gen_brcondi_i64(op ? TCG_COND_NE : TCG_COND_EQ,
1358 tcg_cmp, 0, label_match);
1359 tcg_temp_free_i64(tcg_cmp);
1360 gen_goto_tb(s, 0, s->base.pc_next);
1361 gen_set_label(label_match);
1362 gen_goto_tb(s, 1, addr);
1365 /* Conditional branch (immediate)
1366 * 31 25 24 23 5 4 3 0
1367 * +---------------+----+---------------------+----+------+
1368 * | 0 1 0 1 0 1 0 | o1 | imm19 | o0 | cond |
1369 * +---------------+----+---------------------+----+------+
1371 static void disas_cond_b_imm(DisasContext *s, uint32_t insn)
1373 unsigned int cond;
1374 uint64_t addr;
1376 if ((insn & (1 << 4)) || (insn & (1 << 24))) {
1377 unallocated_encoding(s);
1378 return;
1380 addr = s->pc_curr + sextract32(insn, 5, 19) * 4;
1381 cond = extract32(insn, 0, 4);
1383 reset_btype(s);
1384 if (cond < 0x0e) {
1385 /* genuinely conditional branches */
1386 TCGLabel *label_match = gen_new_label();
1387 arm_gen_test_cc(cond, label_match);
1388 gen_goto_tb(s, 0, s->base.pc_next);
1389 gen_set_label(label_match);
1390 gen_goto_tb(s, 1, addr);
1391 } else {
1392 /* 0xe and 0xf are both "always" conditions */
1393 gen_goto_tb(s, 0, addr);
1397 /* HINT instruction group, including various allocated HINTs */
1398 static void handle_hint(DisasContext *s, uint32_t insn,
1399 unsigned int op1, unsigned int op2, unsigned int crm)
1401 unsigned int selector = crm << 3 | op2;
1403 if (op1 != 3) {
1404 unallocated_encoding(s);
1405 return;
1408 switch (selector) {
1409 case 0b00000: /* NOP */
1410 break;
1411 case 0b00011: /* WFI */
1412 s->base.is_jmp = DISAS_WFI;
1413 break;
1414 case 0b00001: /* YIELD */
1415 /* When running in MTTCG we don't generate jumps to the yield and
1416 * WFE helpers as it won't affect the scheduling of other vCPUs.
1417 * If we wanted to more completely model WFE/SEV so we don't busy
1418 * spin unnecessarily we would need to do something more involved.
1420 if (!(tb_cflags(s->base.tb) & CF_PARALLEL)) {
1421 s->base.is_jmp = DISAS_YIELD;
1423 break;
1424 case 0b00010: /* WFE */
1425 if (!(tb_cflags(s->base.tb) & CF_PARALLEL)) {
1426 s->base.is_jmp = DISAS_WFE;
1428 break;
1429 case 0b00100: /* SEV */
1430 case 0b00101: /* SEVL */
1431 case 0b00110: /* DGH */
1432 /* we treat all as NOP at least for now */
1433 break;
1434 case 0b00111: /* XPACLRI */
1435 if (s->pauth_active) {
1436 gen_helper_xpaci(cpu_X[30], cpu_env, cpu_X[30]);
1438 break;
1439 case 0b01000: /* PACIA1716 */
1440 if (s->pauth_active) {
1441 gen_helper_pacia(cpu_X[17], cpu_env, cpu_X[17], cpu_X[16]);
1443 break;
1444 case 0b01010: /* PACIB1716 */
1445 if (s->pauth_active) {
1446 gen_helper_pacib(cpu_X[17], cpu_env, cpu_X[17], cpu_X[16]);
1448 break;
1449 case 0b01100: /* AUTIA1716 */
1450 if (s->pauth_active) {
1451 gen_helper_autia(cpu_X[17], cpu_env, cpu_X[17], cpu_X[16]);
1453 break;
1454 case 0b01110: /* AUTIB1716 */
1455 if (s->pauth_active) {
1456 gen_helper_autib(cpu_X[17], cpu_env, cpu_X[17], cpu_X[16]);
1458 break;
1459 case 0b10000: /* ESB */
1460 /* Without RAS, we must implement this as NOP. */
1461 if (dc_isar_feature(aa64_ras, s)) {
1463 * QEMU does not have a source of physical SErrors,
1464 * so we are only concerned with virtual SErrors.
1465 * The pseudocode in the ARM for this case is
1466 * if PSTATE.EL IN {EL0, EL1} && EL2Enabled() then
1467 * AArch64.vESBOperation();
1468 * Most of the condition can be evaluated at translation time.
1469 * Test for EL2 present, and defer test for SEL2 to runtime.
1471 if (s->current_el <= 1 && arm_dc_feature(s, ARM_FEATURE_EL2)) {
1472 gen_helper_vesb(cpu_env);
1475 break;
1476 case 0b11000: /* PACIAZ */
1477 if (s->pauth_active) {
1478 gen_helper_pacia(cpu_X[30], cpu_env, cpu_X[30],
1479 new_tmp_a64_zero(s));
1481 break;
1482 case 0b11001: /* PACIASP */
1483 if (s->pauth_active) {
1484 gen_helper_pacia(cpu_X[30], cpu_env, cpu_X[30], cpu_X[31]);
1486 break;
1487 case 0b11010: /* PACIBZ */
1488 if (s->pauth_active) {
1489 gen_helper_pacib(cpu_X[30], cpu_env, cpu_X[30],
1490 new_tmp_a64_zero(s));
1492 break;
1493 case 0b11011: /* PACIBSP */
1494 if (s->pauth_active) {
1495 gen_helper_pacib(cpu_X[30], cpu_env, cpu_X[30], cpu_X[31]);
1497 break;
1498 case 0b11100: /* AUTIAZ */
1499 if (s->pauth_active) {
1500 gen_helper_autia(cpu_X[30], cpu_env, cpu_X[30],
1501 new_tmp_a64_zero(s));
1503 break;
1504 case 0b11101: /* AUTIASP */
1505 if (s->pauth_active) {
1506 gen_helper_autia(cpu_X[30], cpu_env, cpu_X[30], cpu_X[31]);
1508 break;
1509 case 0b11110: /* AUTIBZ */
1510 if (s->pauth_active) {
1511 gen_helper_autib(cpu_X[30], cpu_env, cpu_X[30],
1512 new_tmp_a64_zero(s));
1514 break;
1515 case 0b11111: /* AUTIBSP */
1516 if (s->pauth_active) {
1517 gen_helper_autib(cpu_X[30], cpu_env, cpu_X[30], cpu_X[31]);
1519 break;
1520 default:
1521 /* default specified as NOP equivalent */
1522 break;
1526 static void gen_clrex(DisasContext *s, uint32_t insn)
1528 tcg_gen_movi_i64(cpu_exclusive_addr, -1);
1531 /* CLREX, DSB, DMB, ISB */
1532 static void handle_sync(DisasContext *s, uint32_t insn,
1533 unsigned int op1, unsigned int op2, unsigned int crm)
1535 TCGBar bar;
1537 if (op1 != 3) {
1538 unallocated_encoding(s);
1539 return;
1542 switch (op2) {
1543 case 2: /* CLREX */
1544 gen_clrex(s, insn);
1545 return;
1546 case 4: /* DSB */
1547 case 5: /* DMB */
1548 switch (crm & 3) {
1549 case 1: /* MBReqTypes_Reads */
1550 bar = TCG_BAR_SC | TCG_MO_LD_LD | TCG_MO_LD_ST;
1551 break;
1552 case 2: /* MBReqTypes_Writes */
1553 bar = TCG_BAR_SC | TCG_MO_ST_ST;
1554 break;
1555 default: /* MBReqTypes_All */
1556 bar = TCG_BAR_SC | TCG_MO_ALL;
1557 break;
1559 tcg_gen_mb(bar);
1560 return;
1561 case 6: /* ISB */
1562 /* We need to break the TB after this insn to execute
1563 * a self-modified code correctly and also to take
1564 * any pending interrupts immediately.
1566 reset_btype(s);
1567 gen_goto_tb(s, 0, s->base.pc_next);
1568 return;
1570 case 7: /* SB */
1571 if (crm != 0 || !dc_isar_feature(aa64_sb, s)) {
1572 goto do_unallocated;
1575 * TODO: There is no speculation barrier opcode for TCG;
1576 * MB and end the TB instead.
1578 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_SC);
1579 gen_goto_tb(s, 0, s->base.pc_next);
1580 return;
1582 default:
1583 do_unallocated:
1584 unallocated_encoding(s);
1585 return;
1589 static void gen_xaflag(void)
1591 TCGv_i32 z = tcg_temp_new_i32();
1593 tcg_gen_setcondi_i32(TCG_COND_EQ, z, cpu_ZF, 0);
1596 * (!C & !Z) << 31
1597 * (!(C | Z)) << 31
1598 * ~((C | Z) << 31)
1599 * ~-(C | Z)
1600 * (C | Z) - 1
1602 tcg_gen_or_i32(cpu_NF, cpu_CF, z);
1603 tcg_gen_subi_i32(cpu_NF, cpu_NF, 1);
1605 /* !(Z & C) */
1606 tcg_gen_and_i32(cpu_ZF, z, cpu_CF);
1607 tcg_gen_xori_i32(cpu_ZF, cpu_ZF, 1);
1609 /* (!C & Z) << 31 -> -(Z & ~C) */
1610 tcg_gen_andc_i32(cpu_VF, z, cpu_CF);
1611 tcg_gen_neg_i32(cpu_VF, cpu_VF);
1613 /* C | Z */
1614 tcg_gen_or_i32(cpu_CF, cpu_CF, z);
1616 tcg_temp_free_i32(z);
1619 static void gen_axflag(void)
1621 tcg_gen_sari_i32(cpu_VF, cpu_VF, 31); /* V ? -1 : 0 */
1622 tcg_gen_andc_i32(cpu_CF, cpu_CF, cpu_VF); /* C & !V */
1624 /* !(Z | V) -> !(!ZF | V) -> ZF & !V -> ZF & ~VF */
1625 tcg_gen_andc_i32(cpu_ZF, cpu_ZF, cpu_VF);
1627 tcg_gen_movi_i32(cpu_NF, 0);
1628 tcg_gen_movi_i32(cpu_VF, 0);
1631 /* MSR (immediate) - move immediate to processor state field */
1632 static void handle_msr_i(DisasContext *s, uint32_t insn,
1633 unsigned int op1, unsigned int op2, unsigned int crm)
1635 int op = op1 << 3 | op2;
1637 /* End the TB by default, chaining is ok. */
1638 s->base.is_jmp = DISAS_TOO_MANY;
1640 switch (op) {
1641 case 0x00: /* CFINV */
1642 if (crm != 0 || !dc_isar_feature(aa64_condm_4, s)) {
1643 goto do_unallocated;
1645 tcg_gen_xori_i32(cpu_CF, cpu_CF, 1);
1646 s->base.is_jmp = DISAS_NEXT;
1647 break;
1649 case 0x01: /* XAFlag */
1650 if (crm != 0 || !dc_isar_feature(aa64_condm_5, s)) {
1651 goto do_unallocated;
1653 gen_xaflag();
1654 s->base.is_jmp = DISAS_NEXT;
1655 break;
1657 case 0x02: /* AXFlag */
1658 if (crm != 0 || !dc_isar_feature(aa64_condm_5, s)) {
1659 goto do_unallocated;
1661 gen_axflag();
1662 s->base.is_jmp = DISAS_NEXT;
1663 break;
1665 case 0x03: /* UAO */
1666 if (!dc_isar_feature(aa64_uao, s) || s->current_el == 0) {
1667 goto do_unallocated;
1669 if (crm & 1) {
1670 set_pstate_bits(PSTATE_UAO);
1671 } else {
1672 clear_pstate_bits(PSTATE_UAO);
1674 gen_rebuild_hflags(s);
1675 break;
1677 case 0x04: /* PAN */
1678 if (!dc_isar_feature(aa64_pan, s) || s->current_el == 0) {
1679 goto do_unallocated;
1681 if (crm & 1) {
1682 set_pstate_bits(PSTATE_PAN);
1683 } else {
1684 clear_pstate_bits(PSTATE_PAN);
1686 gen_rebuild_hflags(s);
1687 break;
1689 case 0x05: /* SPSel */
1690 if (s->current_el == 0) {
1691 goto do_unallocated;
1693 gen_helper_msr_i_spsel(cpu_env, tcg_constant_i32(crm & PSTATE_SP));
1694 break;
1696 case 0x19: /* SSBS */
1697 if (!dc_isar_feature(aa64_ssbs, s)) {
1698 goto do_unallocated;
1700 if (crm & 1) {
1701 set_pstate_bits(PSTATE_SSBS);
1702 } else {
1703 clear_pstate_bits(PSTATE_SSBS);
1705 /* Don't need to rebuild hflags since SSBS is a nop */
1706 break;
1708 case 0x1a: /* DIT */
1709 if (!dc_isar_feature(aa64_dit, s)) {
1710 goto do_unallocated;
1712 if (crm & 1) {
1713 set_pstate_bits(PSTATE_DIT);
1714 } else {
1715 clear_pstate_bits(PSTATE_DIT);
1717 /* There's no need to rebuild hflags because DIT is a nop */
1718 break;
1720 case 0x1e: /* DAIFSet */
1721 gen_helper_msr_i_daifset(cpu_env, tcg_constant_i32(crm));
1722 break;
1724 case 0x1f: /* DAIFClear */
1725 gen_helper_msr_i_daifclear(cpu_env, tcg_constant_i32(crm));
1726 /* For DAIFClear, exit the cpu loop to re-evaluate pending IRQs. */
1727 s->base.is_jmp = DISAS_UPDATE_EXIT;
1728 break;
1730 case 0x1c: /* TCO */
1731 if (dc_isar_feature(aa64_mte, s)) {
1732 /* Full MTE is enabled -- set the TCO bit as directed. */
1733 if (crm & 1) {
1734 set_pstate_bits(PSTATE_TCO);
1735 } else {
1736 clear_pstate_bits(PSTATE_TCO);
1738 gen_rebuild_hflags(s);
1739 /* Many factors, including TCO, go into MTE_ACTIVE. */
1740 s->base.is_jmp = DISAS_UPDATE_NOCHAIN;
1741 } else if (dc_isar_feature(aa64_mte_insn_reg, s)) {
1742 /* Only "instructions accessible at EL0" -- PSTATE.TCO is WI. */
1743 s->base.is_jmp = DISAS_NEXT;
1744 } else {
1745 goto do_unallocated;
1747 break;
1749 default:
1750 do_unallocated:
1751 unallocated_encoding(s);
1752 return;
1756 static void gen_get_nzcv(TCGv_i64 tcg_rt)
1758 TCGv_i32 tmp = tcg_temp_new_i32();
1759 TCGv_i32 nzcv = tcg_temp_new_i32();
1761 /* build bit 31, N */
1762 tcg_gen_andi_i32(nzcv, cpu_NF, (1U << 31));
1763 /* build bit 30, Z */
1764 tcg_gen_setcondi_i32(TCG_COND_EQ, tmp, cpu_ZF, 0);
1765 tcg_gen_deposit_i32(nzcv, nzcv, tmp, 30, 1);
1766 /* build bit 29, C */
1767 tcg_gen_deposit_i32(nzcv, nzcv, cpu_CF, 29, 1);
1768 /* build bit 28, V */
1769 tcg_gen_shri_i32(tmp, cpu_VF, 31);
1770 tcg_gen_deposit_i32(nzcv, nzcv, tmp, 28, 1);
1771 /* generate result */
1772 tcg_gen_extu_i32_i64(tcg_rt, nzcv);
1774 tcg_temp_free_i32(nzcv);
1775 tcg_temp_free_i32(tmp);
1778 static void gen_set_nzcv(TCGv_i64 tcg_rt)
1780 TCGv_i32 nzcv = tcg_temp_new_i32();
1782 /* take NZCV from R[t] */
1783 tcg_gen_extrl_i64_i32(nzcv, tcg_rt);
1785 /* bit 31, N */
1786 tcg_gen_andi_i32(cpu_NF, nzcv, (1U << 31));
1787 /* bit 30, Z */
1788 tcg_gen_andi_i32(cpu_ZF, nzcv, (1 << 30));
1789 tcg_gen_setcondi_i32(TCG_COND_EQ, cpu_ZF, cpu_ZF, 0);
1790 /* bit 29, C */
1791 tcg_gen_andi_i32(cpu_CF, nzcv, (1 << 29));
1792 tcg_gen_shri_i32(cpu_CF, cpu_CF, 29);
1793 /* bit 28, V */
1794 tcg_gen_andi_i32(cpu_VF, nzcv, (1 << 28));
1795 tcg_gen_shli_i32(cpu_VF, cpu_VF, 3);
1796 tcg_temp_free_i32(nzcv);
1799 static void gen_sysreg_undef(DisasContext *s, bool isread,
1800 uint8_t op0, uint8_t op1, uint8_t op2,
1801 uint8_t crn, uint8_t crm, uint8_t rt)
1804 * Generate code to emit an UNDEF with correct syndrome
1805 * information for a failed system register access.
1806 * This is EC_UNCATEGORIZED (ie a standard UNDEF) in most cases,
1807 * but if FEAT_IDST is implemented then read accesses to registers
1808 * in the feature ID space are reported with the EC_SYSTEMREGISTERTRAP
1809 * syndrome.
1811 uint32_t syndrome;
1813 if (isread && dc_isar_feature(aa64_ids, s) &&
1814 arm_cpreg_encoding_in_idspace(op0, op1, op2, crn, crm)) {
1815 syndrome = syn_aa64_sysregtrap(op0, op1, op2, crn, crm, rt, isread);
1816 } else {
1817 syndrome = syn_uncategorized();
1819 gen_exception_insn_el(s, s->pc_curr, EXCP_UDEF, syndrome,
1820 default_exception_el(s));
1823 /* MRS - move from system register
1824 * MSR (register) - move to system register
1825 * SYS
1826 * SYSL
1827 * These are all essentially the same insn in 'read' and 'write'
1828 * versions, with varying op0 fields.
1830 static void handle_sys(DisasContext *s, uint32_t insn, bool isread,
1831 unsigned int op0, unsigned int op1, unsigned int op2,
1832 unsigned int crn, unsigned int crm, unsigned int rt)
1834 const ARMCPRegInfo *ri;
1835 TCGv_i64 tcg_rt;
1837 ri = get_arm_cp_reginfo(s->cp_regs,
1838 ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP,
1839 crn, crm, op0, op1, op2));
1841 if (!ri) {
1842 /* Unknown register; this might be a guest error or a QEMU
1843 * unimplemented feature.
1845 qemu_log_mask(LOG_UNIMP, "%s access to unsupported AArch64 "
1846 "system register op0:%d op1:%d crn:%d crm:%d op2:%d\n",
1847 isread ? "read" : "write", op0, op1, crn, crm, op2);
1848 gen_sysreg_undef(s, isread, op0, op1, op2, crn, crm, rt);
1849 return;
1852 /* Check access permissions */
1853 if (!cp_access_ok(s->current_el, ri, isread)) {
1854 gen_sysreg_undef(s, isread, op0, op1, op2, crn, crm, rt);
1855 return;
1858 if (ri->accessfn) {
1859 /* Emit code to perform further access permissions checks at
1860 * runtime; this may result in an exception.
1862 uint32_t syndrome;
1864 syndrome = syn_aa64_sysregtrap(op0, op1, op2, crn, crm, rt, isread);
1865 gen_a64_set_pc_im(s->pc_curr);
1866 gen_helper_access_check_cp_reg(cpu_env,
1867 tcg_constant_ptr(ri),
1868 tcg_constant_i32(syndrome),
1869 tcg_constant_i32(isread));
1870 } else if (ri->type & ARM_CP_RAISES_EXC) {
1872 * The readfn or writefn might raise an exception;
1873 * synchronize the CPU state in case it does.
1875 gen_a64_set_pc_im(s->pc_curr);
1878 /* Handle special cases first */
1879 switch (ri->type & ARM_CP_SPECIAL_MASK) {
1880 case 0:
1881 break;
1882 case ARM_CP_NOP:
1883 return;
1884 case ARM_CP_NZCV:
1885 tcg_rt = cpu_reg(s, rt);
1886 if (isread) {
1887 gen_get_nzcv(tcg_rt);
1888 } else {
1889 gen_set_nzcv(tcg_rt);
1891 return;
1892 case ARM_CP_CURRENTEL:
1893 /* Reads as current EL value from pstate, which is
1894 * guaranteed to be constant by the tb flags.
1896 tcg_rt = cpu_reg(s, rt);
1897 tcg_gen_movi_i64(tcg_rt, s->current_el << 2);
1898 return;
1899 case ARM_CP_DC_ZVA:
1900 /* Writes clear the aligned block of memory which rt points into. */
1901 if (s->mte_active[0]) {
1902 int desc = 0;
1904 desc = FIELD_DP32(desc, MTEDESC, MIDX, get_mem_index(s));
1905 desc = FIELD_DP32(desc, MTEDESC, TBI, s->tbid);
1906 desc = FIELD_DP32(desc, MTEDESC, TCMA, s->tcma);
1908 tcg_rt = new_tmp_a64(s);
1909 gen_helper_mte_check_zva(tcg_rt, cpu_env,
1910 tcg_constant_i32(desc), cpu_reg(s, rt));
1911 } else {
1912 tcg_rt = clean_data_tbi(s, cpu_reg(s, rt));
1914 gen_helper_dc_zva(cpu_env, tcg_rt);
1915 return;
1916 case ARM_CP_DC_GVA:
1918 TCGv_i64 clean_addr, tag;
1921 * DC_GVA, like DC_ZVA, requires that we supply the original
1922 * pointer for an invalid page. Probe that address first.
1924 tcg_rt = cpu_reg(s, rt);
1925 clean_addr = clean_data_tbi(s, tcg_rt);
1926 gen_probe_access(s, clean_addr, MMU_DATA_STORE, MO_8);
1928 if (s->ata) {
1929 /* Extract the tag from the register to match STZGM. */
1930 tag = tcg_temp_new_i64();
1931 tcg_gen_shri_i64(tag, tcg_rt, 56);
1932 gen_helper_stzgm_tags(cpu_env, clean_addr, tag);
1933 tcg_temp_free_i64(tag);
1936 return;
1937 case ARM_CP_DC_GZVA:
1939 TCGv_i64 clean_addr, tag;
1941 /* For DC_GZVA, we can rely on DC_ZVA for the proper fault. */
1942 tcg_rt = cpu_reg(s, rt);
1943 clean_addr = clean_data_tbi(s, tcg_rt);
1944 gen_helper_dc_zva(cpu_env, clean_addr);
1946 if (s->ata) {
1947 /* Extract the tag from the register to match STZGM. */
1948 tag = tcg_temp_new_i64();
1949 tcg_gen_shri_i64(tag, tcg_rt, 56);
1950 gen_helper_stzgm_tags(cpu_env, clean_addr, tag);
1951 tcg_temp_free_i64(tag);
1954 return;
1955 default:
1956 g_assert_not_reached();
1958 if ((ri->type & ARM_CP_FPU) && !fp_access_check(s)) {
1959 return;
1960 } else if ((ri->type & ARM_CP_SVE) && !sve_access_check(s)) {
1961 return;
1964 if ((tb_cflags(s->base.tb) & CF_USE_ICOUNT) && (ri->type & ARM_CP_IO)) {
1965 gen_io_start();
1968 tcg_rt = cpu_reg(s, rt);
1970 if (isread) {
1971 if (ri->type & ARM_CP_CONST) {
1972 tcg_gen_movi_i64(tcg_rt, ri->resetvalue);
1973 } else if (ri->readfn) {
1974 gen_helper_get_cp_reg64(tcg_rt, cpu_env, tcg_constant_ptr(ri));
1975 } else {
1976 tcg_gen_ld_i64(tcg_rt, cpu_env, ri->fieldoffset);
1978 } else {
1979 if (ri->type & ARM_CP_CONST) {
1980 /* If not forbidden by access permissions, treat as WI */
1981 return;
1982 } else if (ri->writefn) {
1983 gen_helper_set_cp_reg64(cpu_env, tcg_constant_ptr(ri), tcg_rt);
1984 } else {
1985 tcg_gen_st_i64(tcg_rt, cpu_env, ri->fieldoffset);
1989 if ((tb_cflags(s->base.tb) & CF_USE_ICOUNT) && (ri->type & ARM_CP_IO)) {
1990 /* I/O operations must end the TB here (whether read or write) */
1991 s->base.is_jmp = DISAS_UPDATE_EXIT;
1993 if (!isread && !(ri->type & ARM_CP_SUPPRESS_TB_END)) {
1995 * A write to any coprocessor regiser that ends a TB
1996 * must rebuild the hflags for the next TB.
1998 gen_rebuild_hflags(s);
2000 * We default to ending the TB on a coprocessor register write,
2001 * but allow this to be suppressed by the register definition
2002 * (usually only necessary to work around guest bugs).
2004 s->base.is_jmp = DISAS_UPDATE_EXIT;
2008 /* System
2009 * 31 22 21 20 19 18 16 15 12 11 8 7 5 4 0
2010 * +---------------------+---+-----+-----+-------+-------+-----+------+
2011 * | 1 1 0 1 0 1 0 1 0 0 | L | op0 | op1 | CRn | CRm | op2 | Rt |
2012 * +---------------------+---+-----+-----+-------+-------+-----+------+
2014 static void disas_system(DisasContext *s, uint32_t insn)
2016 unsigned int l, op0, op1, crn, crm, op2, rt;
2017 l = extract32(insn, 21, 1);
2018 op0 = extract32(insn, 19, 2);
2019 op1 = extract32(insn, 16, 3);
2020 crn = extract32(insn, 12, 4);
2021 crm = extract32(insn, 8, 4);
2022 op2 = extract32(insn, 5, 3);
2023 rt = extract32(insn, 0, 5);
2025 if (op0 == 0) {
2026 if (l || rt != 31) {
2027 unallocated_encoding(s);
2028 return;
2030 switch (crn) {
2031 case 2: /* HINT (including allocated hints like NOP, YIELD, etc) */
2032 handle_hint(s, insn, op1, op2, crm);
2033 break;
2034 case 3: /* CLREX, DSB, DMB, ISB */
2035 handle_sync(s, insn, op1, op2, crm);
2036 break;
2037 case 4: /* MSR (immediate) */
2038 handle_msr_i(s, insn, op1, op2, crm);
2039 break;
2040 default:
2041 unallocated_encoding(s);
2042 break;
2044 return;
2046 handle_sys(s, insn, l, op0, op1, op2, crn, crm, rt);
2049 /* Exception generation
2051 * 31 24 23 21 20 5 4 2 1 0
2052 * +-----------------+-----+------------------------+-----+----+
2053 * | 1 1 0 1 0 1 0 0 | opc | imm16 | op2 | LL |
2054 * +-----------------------+------------------------+----------+
2056 static void disas_exc(DisasContext *s, uint32_t insn)
2058 int opc = extract32(insn, 21, 3);
2059 int op2_ll = extract32(insn, 0, 5);
2060 int imm16 = extract32(insn, 5, 16);
2062 switch (opc) {
2063 case 0:
2064 /* For SVC, HVC and SMC we advance the single-step state
2065 * machine before taking the exception. This is architecturally
2066 * mandated, to ensure that single-stepping a system call
2067 * instruction works properly.
2069 switch (op2_ll) {
2070 case 1: /* SVC */
2071 gen_ss_advance(s);
2072 gen_exception_insn_el(s, s->base.pc_next, EXCP_SWI,
2073 syn_aa64_svc(imm16), default_exception_el(s));
2074 break;
2075 case 2: /* HVC */
2076 if (s->current_el == 0) {
2077 unallocated_encoding(s);
2078 break;
2080 /* The pre HVC helper handles cases when HVC gets trapped
2081 * as an undefined insn by runtime configuration.
2083 gen_a64_set_pc_im(s->pc_curr);
2084 gen_helper_pre_hvc(cpu_env);
2085 gen_ss_advance(s);
2086 gen_exception_insn_el(s, s->base.pc_next, EXCP_HVC,
2087 syn_aa64_hvc(imm16), 2);
2088 break;
2089 case 3: /* SMC */
2090 if (s->current_el == 0) {
2091 unallocated_encoding(s);
2092 break;
2094 gen_a64_set_pc_im(s->pc_curr);
2095 gen_helper_pre_smc(cpu_env, tcg_constant_i32(syn_aa64_smc(imm16)));
2096 gen_ss_advance(s);
2097 gen_exception_insn_el(s, s->base.pc_next, EXCP_SMC,
2098 syn_aa64_smc(imm16), 3);
2099 break;
2100 default:
2101 unallocated_encoding(s);
2102 break;
2104 break;
2105 case 1:
2106 if (op2_ll != 0) {
2107 unallocated_encoding(s);
2108 break;
2110 /* BRK */
2111 gen_exception_bkpt_insn(s, syn_aa64_bkpt(imm16));
2112 break;
2113 case 2:
2114 if (op2_ll != 0) {
2115 unallocated_encoding(s);
2116 break;
2118 /* HLT. This has two purposes.
2119 * Architecturally, it is an external halting debug instruction.
2120 * Since QEMU doesn't implement external debug, we treat this as
2121 * it is required for halting debug disabled: it will UNDEF.
2122 * Secondly, "HLT 0xf000" is the A64 semihosting syscall instruction.
2124 if (semihosting_enabled() && imm16 == 0xf000) {
2125 #ifndef CONFIG_USER_ONLY
2126 /* In system mode, don't allow userspace access to semihosting,
2127 * to provide some semblance of security (and for consistency
2128 * with our 32-bit semihosting).
2130 if (s->current_el == 0) {
2131 unallocated_encoding(s);
2132 break;
2134 #endif
2135 gen_exception_internal_insn(s, s->pc_curr, EXCP_SEMIHOST);
2136 } else {
2137 unallocated_encoding(s);
2139 break;
2140 case 5:
2141 if (op2_ll < 1 || op2_ll > 3) {
2142 unallocated_encoding(s);
2143 break;
2145 /* DCPS1, DCPS2, DCPS3 */
2146 unallocated_encoding(s);
2147 break;
2148 default:
2149 unallocated_encoding(s);
2150 break;
2154 /* Unconditional branch (register)
2155 * 31 25 24 21 20 16 15 10 9 5 4 0
2156 * +---------------+-------+-------+-------+------+-------+
2157 * | 1 1 0 1 0 1 1 | opc | op2 | op3 | Rn | op4 |
2158 * +---------------+-------+-------+-------+------+-------+
2160 static void disas_uncond_b_reg(DisasContext *s, uint32_t insn)
2162 unsigned int opc, op2, op3, rn, op4;
2163 unsigned btype_mod = 2; /* 0: BR, 1: BLR, 2: other */
2164 TCGv_i64 dst;
2165 TCGv_i64 modifier;
2167 opc = extract32(insn, 21, 4);
2168 op2 = extract32(insn, 16, 5);
2169 op3 = extract32(insn, 10, 6);
2170 rn = extract32(insn, 5, 5);
2171 op4 = extract32(insn, 0, 5);
2173 if (op2 != 0x1f) {
2174 goto do_unallocated;
2177 switch (opc) {
2178 case 0: /* BR */
2179 case 1: /* BLR */
2180 case 2: /* RET */
2181 btype_mod = opc;
2182 switch (op3) {
2183 case 0:
2184 /* BR, BLR, RET */
2185 if (op4 != 0) {
2186 goto do_unallocated;
2188 dst = cpu_reg(s, rn);
2189 break;
2191 case 2:
2192 case 3:
2193 if (!dc_isar_feature(aa64_pauth, s)) {
2194 goto do_unallocated;
2196 if (opc == 2) {
2197 /* RETAA, RETAB */
2198 if (rn != 0x1f || op4 != 0x1f) {
2199 goto do_unallocated;
2201 rn = 30;
2202 modifier = cpu_X[31];
2203 } else {
2204 /* BRAAZ, BRABZ, BLRAAZ, BLRABZ */
2205 if (op4 != 0x1f) {
2206 goto do_unallocated;
2208 modifier = new_tmp_a64_zero(s);
2210 if (s->pauth_active) {
2211 dst = new_tmp_a64(s);
2212 if (op3 == 2) {
2213 gen_helper_autia(dst, cpu_env, cpu_reg(s, rn), modifier);
2214 } else {
2215 gen_helper_autib(dst, cpu_env, cpu_reg(s, rn), modifier);
2217 } else {
2218 dst = cpu_reg(s, rn);
2220 break;
2222 default:
2223 goto do_unallocated;
2225 gen_a64_set_pc(s, dst);
2226 /* BLR also needs to load return address */
2227 if (opc == 1) {
2228 tcg_gen_movi_i64(cpu_reg(s, 30), s->base.pc_next);
2230 break;
2232 case 8: /* BRAA */
2233 case 9: /* BLRAA */
2234 if (!dc_isar_feature(aa64_pauth, s)) {
2235 goto do_unallocated;
2237 if ((op3 & ~1) != 2) {
2238 goto do_unallocated;
2240 btype_mod = opc & 1;
2241 if (s->pauth_active) {
2242 dst = new_tmp_a64(s);
2243 modifier = cpu_reg_sp(s, op4);
2244 if (op3 == 2) {
2245 gen_helper_autia(dst, cpu_env, cpu_reg(s, rn), modifier);
2246 } else {
2247 gen_helper_autib(dst, cpu_env, cpu_reg(s, rn), modifier);
2249 } else {
2250 dst = cpu_reg(s, rn);
2252 gen_a64_set_pc(s, dst);
2253 /* BLRAA also needs to load return address */
2254 if (opc == 9) {
2255 tcg_gen_movi_i64(cpu_reg(s, 30), s->base.pc_next);
2257 break;
2259 case 4: /* ERET */
2260 if (s->current_el == 0) {
2261 goto do_unallocated;
2263 switch (op3) {
2264 case 0: /* ERET */
2265 if (op4 != 0) {
2266 goto do_unallocated;
2268 dst = tcg_temp_new_i64();
2269 tcg_gen_ld_i64(dst, cpu_env,
2270 offsetof(CPUARMState, elr_el[s->current_el]));
2271 break;
2273 case 2: /* ERETAA */
2274 case 3: /* ERETAB */
2275 if (!dc_isar_feature(aa64_pauth, s)) {
2276 goto do_unallocated;
2278 if (rn != 0x1f || op4 != 0x1f) {
2279 goto do_unallocated;
2281 dst = tcg_temp_new_i64();
2282 tcg_gen_ld_i64(dst, cpu_env,
2283 offsetof(CPUARMState, elr_el[s->current_el]));
2284 if (s->pauth_active) {
2285 modifier = cpu_X[31];
2286 if (op3 == 2) {
2287 gen_helper_autia(dst, cpu_env, dst, modifier);
2288 } else {
2289 gen_helper_autib(dst, cpu_env, dst, modifier);
2292 break;
2294 default:
2295 goto do_unallocated;
2297 if (tb_cflags(s->base.tb) & CF_USE_ICOUNT) {
2298 gen_io_start();
2301 gen_helper_exception_return(cpu_env, dst);
2302 tcg_temp_free_i64(dst);
2303 /* Must exit loop to check un-masked IRQs */
2304 s->base.is_jmp = DISAS_EXIT;
2305 return;
2307 case 5: /* DRPS */
2308 if (op3 != 0 || op4 != 0 || rn != 0x1f) {
2309 goto do_unallocated;
2310 } else {
2311 unallocated_encoding(s);
2313 return;
2315 default:
2316 do_unallocated:
2317 unallocated_encoding(s);
2318 return;
2321 switch (btype_mod) {
2322 case 0: /* BR */
2323 if (dc_isar_feature(aa64_bti, s)) {
2324 /* BR to {x16,x17} or !guard -> 1, else 3. */
2325 set_btype(s, rn == 16 || rn == 17 || !s->guarded_page ? 1 : 3);
2327 break;
2329 case 1: /* BLR */
2330 if (dc_isar_feature(aa64_bti, s)) {
2331 /* BLR sets BTYPE to 2, regardless of source guarded page. */
2332 set_btype(s, 2);
2334 break;
2336 default: /* RET or none of the above. */
2337 /* BTYPE will be set to 0 by normal end-of-insn processing. */
2338 break;
2341 s->base.is_jmp = DISAS_JUMP;
2344 /* Branches, exception generating and system instructions */
2345 static void disas_b_exc_sys(DisasContext *s, uint32_t insn)
2347 switch (extract32(insn, 25, 7)) {
2348 case 0x0a: case 0x0b:
2349 case 0x4a: case 0x4b: /* Unconditional branch (immediate) */
2350 disas_uncond_b_imm(s, insn);
2351 break;
2352 case 0x1a: case 0x5a: /* Compare & branch (immediate) */
2353 disas_comp_b_imm(s, insn);
2354 break;
2355 case 0x1b: case 0x5b: /* Test & branch (immediate) */
2356 disas_test_b_imm(s, insn);
2357 break;
2358 case 0x2a: /* Conditional branch (immediate) */
2359 disas_cond_b_imm(s, insn);
2360 break;
2361 case 0x6a: /* Exception generation / System */
2362 if (insn & (1 << 24)) {
2363 if (extract32(insn, 22, 2) == 0) {
2364 disas_system(s, insn);
2365 } else {
2366 unallocated_encoding(s);
2368 } else {
2369 disas_exc(s, insn);
2371 break;
2372 case 0x6b: /* Unconditional branch (register) */
2373 disas_uncond_b_reg(s, insn);
2374 break;
2375 default:
2376 unallocated_encoding(s);
2377 break;
2382 * Load/Store exclusive instructions are implemented by remembering
2383 * the value/address loaded, and seeing if these are the same
2384 * when the store is performed. This is not actually the architecturally
2385 * mandated semantics, but it works for typical guest code sequences
2386 * and avoids having to monitor regular stores.
2388 * The store exclusive uses the atomic cmpxchg primitives to avoid
2389 * races in multi-threaded linux-user and when MTTCG softmmu is
2390 * enabled.
2392 static void gen_load_exclusive(DisasContext *s, int rt, int rt2,
2393 TCGv_i64 addr, int size, bool is_pair)
2395 int idx = get_mem_index(s);
2396 MemOp memop = s->be_data;
2398 g_assert(size <= 3);
2399 if (is_pair) {
2400 g_assert(size >= 2);
2401 if (size == 2) {
2402 /* The pair must be single-copy atomic for the doubleword. */
2403 memop |= MO_64 | MO_ALIGN;
2404 tcg_gen_qemu_ld_i64(cpu_exclusive_val, addr, idx, memop);
2405 if (s->be_data == MO_LE) {
2406 tcg_gen_extract_i64(cpu_reg(s, rt), cpu_exclusive_val, 0, 32);
2407 tcg_gen_extract_i64(cpu_reg(s, rt2), cpu_exclusive_val, 32, 32);
2408 } else {
2409 tcg_gen_extract_i64(cpu_reg(s, rt), cpu_exclusive_val, 32, 32);
2410 tcg_gen_extract_i64(cpu_reg(s, rt2), cpu_exclusive_val, 0, 32);
2412 } else {
2413 /* The pair must be single-copy atomic for *each* doubleword, not
2414 the entire quadword, however it must be quadword aligned. */
2415 memop |= MO_64;
2416 tcg_gen_qemu_ld_i64(cpu_exclusive_val, addr, idx,
2417 memop | MO_ALIGN_16);
2419 TCGv_i64 addr2 = tcg_temp_new_i64();
2420 tcg_gen_addi_i64(addr2, addr, 8);
2421 tcg_gen_qemu_ld_i64(cpu_exclusive_high, addr2, idx, memop);
2422 tcg_temp_free_i64(addr2);
2424 tcg_gen_mov_i64(cpu_reg(s, rt), cpu_exclusive_val);
2425 tcg_gen_mov_i64(cpu_reg(s, rt2), cpu_exclusive_high);
2427 } else {
2428 memop |= size | MO_ALIGN;
2429 tcg_gen_qemu_ld_i64(cpu_exclusive_val, addr, idx, memop);
2430 tcg_gen_mov_i64(cpu_reg(s, rt), cpu_exclusive_val);
2432 tcg_gen_mov_i64(cpu_exclusive_addr, addr);
2435 static void gen_store_exclusive(DisasContext *s, int rd, int rt, int rt2,
2436 TCGv_i64 addr, int size, int is_pair)
2438 /* if (env->exclusive_addr == addr && env->exclusive_val == [addr]
2439 * && (!is_pair || env->exclusive_high == [addr + datasize])) {
2440 * [addr] = {Rt};
2441 * if (is_pair) {
2442 * [addr + datasize] = {Rt2};
2444 * {Rd} = 0;
2445 * } else {
2446 * {Rd} = 1;
2448 * env->exclusive_addr = -1;
2450 TCGLabel *fail_label = gen_new_label();
2451 TCGLabel *done_label = gen_new_label();
2452 TCGv_i64 tmp;
2454 tcg_gen_brcond_i64(TCG_COND_NE, addr, cpu_exclusive_addr, fail_label);
2456 tmp = tcg_temp_new_i64();
2457 if (is_pair) {
2458 if (size == 2) {
2459 if (s->be_data == MO_LE) {
2460 tcg_gen_concat32_i64(tmp, cpu_reg(s, rt), cpu_reg(s, rt2));
2461 } else {
2462 tcg_gen_concat32_i64(tmp, cpu_reg(s, rt2), cpu_reg(s, rt));
2464 tcg_gen_atomic_cmpxchg_i64(tmp, cpu_exclusive_addr,
2465 cpu_exclusive_val, tmp,
2466 get_mem_index(s),
2467 MO_64 | MO_ALIGN | s->be_data);
2468 tcg_gen_setcond_i64(TCG_COND_NE, tmp, tmp, cpu_exclusive_val);
2469 } else if (tb_cflags(s->base.tb) & CF_PARALLEL) {
2470 if (!HAVE_CMPXCHG128) {
2471 gen_helper_exit_atomic(cpu_env);
2473 * Produce a result so we have a well-formed opcode
2474 * stream when the following (dead) code uses 'tmp'.
2475 * TCG will remove the dead ops for us.
2477 tcg_gen_movi_i64(tmp, 0);
2478 } else if (s->be_data == MO_LE) {
2479 gen_helper_paired_cmpxchg64_le_parallel(tmp, cpu_env,
2480 cpu_exclusive_addr,
2481 cpu_reg(s, rt),
2482 cpu_reg(s, rt2));
2483 } else {
2484 gen_helper_paired_cmpxchg64_be_parallel(tmp, cpu_env,
2485 cpu_exclusive_addr,
2486 cpu_reg(s, rt),
2487 cpu_reg(s, rt2));
2489 } else if (s->be_data == MO_LE) {
2490 gen_helper_paired_cmpxchg64_le(tmp, cpu_env, cpu_exclusive_addr,
2491 cpu_reg(s, rt), cpu_reg(s, rt2));
2492 } else {
2493 gen_helper_paired_cmpxchg64_be(tmp, cpu_env, cpu_exclusive_addr,
2494 cpu_reg(s, rt), cpu_reg(s, rt2));
2496 } else {
2497 tcg_gen_atomic_cmpxchg_i64(tmp, cpu_exclusive_addr, cpu_exclusive_val,
2498 cpu_reg(s, rt), get_mem_index(s),
2499 size | MO_ALIGN | s->be_data);
2500 tcg_gen_setcond_i64(TCG_COND_NE, tmp, tmp, cpu_exclusive_val);
2502 tcg_gen_mov_i64(cpu_reg(s, rd), tmp);
2503 tcg_temp_free_i64(tmp);
2504 tcg_gen_br(done_label);
2506 gen_set_label(fail_label);
2507 tcg_gen_movi_i64(cpu_reg(s, rd), 1);
2508 gen_set_label(done_label);
2509 tcg_gen_movi_i64(cpu_exclusive_addr, -1);
2512 static void gen_compare_and_swap(DisasContext *s, int rs, int rt,
2513 int rn, int size)
2515 TCGv_i64 tcg_rs = cpu_reg(s, rs);
2516 TCGv_i64 tcg_rt = cpu_reg(s, rt);
2517 int memidx = get_mem_index(s);
2518 TCGv_i64 clean_addr;
2520 if (rn == 31) {
2521 gen_check_sp_alignment(s);
2523 clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn), true, rn != 31, size);
2524 tcg_gen_atomic_cmpxchg_i64(tcg_rs, clean_addr, tcg_rs, tcg_rt, memidx,
2525 size | MO_ALIGN | s->be_data);
2528 static void gen_compare_and_swap_pair(DisasContext *s, int rs, int rt,
2529 int rn, int size)
2531 TCGv_i64 s1 = cpu_reg(s, rs);
2532 TCGv_i64 s2 = cpu_reg(s, rs + 1);
2533 TCGv_i64 t1 = cpu_reg(s, rt);
2534 TCGv_i64 t2 = cpu_reg(s, rt + 1);
2535 TCGv_i64 clean_addr;
2536 int memidx = get_mem_index(s);
2538 if (rn == 31) {
2539 gen_check_sp_alignment(s);
2542 /* This is a single atomic access, despite the "pair". */
2543 clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn), true, rn != 31, size + 1);
2545 if (size == 2) {
2546 TCGv_i64 cmp = tcg_temp_new_i64();
2547 TCGv_i64 val = tcg_temp_new_i64();
2549 if (s->be_data == MO_LE) {
2550 tcg_gen_concat32_i64(val, t1, t2);
2551 tcg_gen_concat32_i64(cmp, s1, s2);
2552 } else {
2553 tcg_gen_concat32_i64(val, t2, t1);
2554 tcg_gen_concat32_i64(cmp, s2, s1);
2557 tcg_gen_atomic_cmpxchg_i64(cmp, clean_addr, cmp, val, memidx,
2558 MO_64 | MO_ALIGN | s->be_data);
2559 tcg_temp_free_i64(val);
2561 if (s->be_data == MO_LE) {
2562 tcg_gen_extr32_i64(s1, s2, cmp);
2563 } else {
2564 tcg_gen_extr32_i64(s2, s1, cmp);
2566 tcg_temp_free_i64(cmp);
2567 } else if (tb_cflags(s->base.tb) & CF_PARALLEL) {
2568 if (HAVE_CMPXCHG128) {
2569 TCGv_i32 tcg_rs = tcg_constant_i32(rs);
2570 if (s->be_data == MO_LE) {
2571 gen_helper_casp_le_parallel(cpu_env, tcg_rs,
2572 clean_addr, t1, t2);
2573 } else {
2574 gen_helper_casp_be_parallel(cpu_env, tcg_rs,
2575 clean_addr, t1, t2);
2577 } else {
2578 gen_helper_exit_atomic(cpu_env);
2579 s->base.is_jmp = DISAS_NORETURN;
2581 } else {
2582 TCGv_i64 d1 = tcg_temp_new_i64();
2583 TCGv_i64 d2 = tcg_temp_new_i64();
2584 TCGv_i64 a2 = tcg_temp_new_i64();
2585 TCGv_i64 c1 = tcg_temp_new_i64();
2586 TCGv_i64 c2 = tcg_temp_new_i64();
2587 TCGv_i64 zero = tcg_constant_i64(0);
2589 /* Load the two words, in memory order. */
2590 tcg_gen_qemu_ld_i64(d1, clean_addr, memidx,
2591 MO_64 | MO_ALIGN_16 | s->be_data);
2592 tcg_gen_addi_i64(a2, clean_addr, 8);
2593 tcg_gen_qemu_ld_i64(d2, a2, memidx, MO_64 | s->be_data);
2595 /* Compare the two words, also in memory order. */
2596 tcg_gen_setcond_i64(TCG_COND_EQ, c1, d1, s1);
2597 tcg_gen_setcond_i64(TCG_COND_EQ, c2, d2, s2);
2598 tcg_gen_and_i64(c2, c2, c1);
2600 /* If compare equal, write back new data, else write back old data. */
2601 tcg_gen_movcond_i64(TCG_COND_NE, c1, c2, zero, t1, d1);
2602 tcg_gen_movcond_i64(TCG_COND_NE, c2, c2, zero, t2, d2);
2603 tcg_gen_qemu_st_i64(c1, clean_addr, memidx, MO_64 | s->be_data);
2604 tcg_gen_qemu_st_i64(c2, a2, memidx, MO_64 | s->be_data);
2605 tcg_temp_free_i64(a2);
2606 tcg_temp_free_i64(c1);
2607 tcg_temp_free_i64(c2);
2609 /* Write back the data from memory to Rs. */
2610 tcg_gen_mov_i64(s1, d1);
2611 tcg_gen_mov_i64(s2, d2);
2612 tcg_temp_free_i64(d1);
2613 tcg_temp_free_i64(d2);
2617 /* Update the Sixty-Four bit (SF) registersize. This logic is derived
2618 * from the ARMv8 specs for LDR (Shared decode for all encodings).
2620 static bool disas_ldst_compute_iss_sf(int size, bool is_signed, int opc)
2622 int opc0 = extract32(opc, 0, 1);
2623 int regsize;
2625 if (is_signed) {
2626 regsize = opc0 ? 32 : 64;
2627 } else {
2628 regsize = size == 3 ? 64 : 32;
2630 return regsize == 64;
2633 /* Load/store exclusive
2635 * 31 30 29 24 23 22 21 20 16 15 14 10 9 5 4 0
2636 * +-----+-------------+----+---+----+------+----+-------+------+------+
2637 * | sz | 0 0 1 0 0 0 | o2 | L | o1 | Rs | o0 | Rt2 | Rn | Rt |
2638 * +-----+-------------+----+---+----+------+----+-------+------+------+
2640 * sz: 00 -> 8 bit, 01 -> 16 bit, 10 -> 32 bit, 11 -> 64 bit
2641 * L: 0 -> store, 1 -> load
2642 * o2: 0 -> exclusive, 1 -> not
2643 * o1: 0 -> single register, 1 -> register pair
2644 * o0: 1 -> load-acquire/store-release, 0 -> not
2646 static void disas_ldst_excl(DisasContext *s, uint32_t insn)
2648 int rt = extract32(insn, 0, 5);
2649 int rn = extract32(insn, 5, 5);
2650 int rt2 = extract32(insn, 10, 5);
2651 int rs = extract32(insn, 16, 5);
2652 int is_lasr = extract32(insn, 15, 1);
2653 int o2_L_o1_o0 = extract32(insn, 21, 3) * 2 | is_lasr;
2654 int size = extract32(insn, 30, 2);
2655 TCGv_i64 clean_addr;
2657 switch (o2_L_o1_o0) {
2658 case 0x0: /* STXR */
2659 case 0x1: /* STLXR */
2660 if (rn == 31) {
2661 gen_check_sp_alignment(s);
2663 if (is_lasr) {
2664 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2666 clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn),
2667 true, rn != 31, size);
2668 gen_store_exclusive(s, rs, rt, rt2, clean_addr, size, false);
2669 return;
2671 case 0x4: /* LDXR */
2672 case 0x5: /* LDAXR */
2673 if (rn == 31) {
2674 gen_check_sp_alignment(s);
2676 clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn),
2677 false, rn != 31, size);
2678 s->is_ldex = true;
2679 gen_load_exclusive(s, rt, rt2, clean_addr, size, false);
2680 if (is_lasr) {
2681 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2683 return;
2685 case 0x8: /* STLLR */
2686 if (!dc_isar_feature(aa64_lor, s)) {
2687 break;
2689 /* StoreLORelease is the same as Store-Release for QEMU. */
2690 /* fall through */
2691 case 0x9: /* STLR */
2692 /* Generate ISS for non-exclusive accesses including LASR. */
2693 if (rn == 31) {
2694 gen_check_sp_alignment(s);
2696 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2697 clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn),
2698 true, rn != 31, size);
2699 /* TODO: ARMv8.4-LSE SCTLR.nAA */
2700 do_gpr_st(s, cpu_reg(s, rt), clean_addr, size | MO_ALIGN, true, rt,
2701 disas_ldst_compute_iss_sf(size, false, 0), is_lasr);
2702 return;
2704 case 0xc: /* LDLAR */
2705 if (!dc_isar_feature(aa64_lor, s)) {
2706 break;
2708 /* LoadLOAcquire is the same as Load-Acquire for QEMU. */
2709 /* fall through */
2710 case 0xd: /* LDAR */
2711 /* Generate ISS for non-exclusive accesses including LASR. */
2712 if (rn == 31) {
2713 gen_check_sp_alignment(s);
2715 clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn),
2716 false, rn != 31, size);
2717 /* TODO: ARMv8.4-LSE SCTLR.nAA */
2718 do_gpr_ld(s, cpu_reg(s, rt), clean_addr, size | MO_ALIGN, false, true,
2719 rt, disas_ldst_compute_iss_sf(size, false, 0), is_lasr);
2720 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2721 return;
2723 case 0x2: case 0x3: /* CASP / STXP */
2724 if (size & 2) { /* STXP / STLXP */
2725 if (rn == 31) {
2726 gen_check_sp_alignment(s);
2728 if (is_lasr) {
2729 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2731 clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn),
2732 true, rn != 31, size);
2733 gen_store_exclusive(s, rs, rt, rt2, clean_addr, size, true);
2734 return;
2736 if (rt2 == 31
2737 && ((rt | rs) & 1) == 0
2738 && dc_isar_feature(aa64_atomics, s)) {
2739 /* CASP / CASPL */
2740 gen_compare_and_swap_pair(s, rs, rt, rn, size | 2);
2741 return;
2743 break;
2745 case 0x6: case 0x7: /* CASPA / LDXP */
2746 if (size & 2) { /* LDXP / LDAXP */
2747 if (rn == 31) {
2748 gen_check_sp_alignment(s);
2750 clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn),
2751 false, rn != 31, size);
2752 s->is_ldex = true;
2753 gen_load_exclusive(s, rt, rt2, clean_addr, size, true);
2754 if (is_lasr) {
2755 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2757 return;
2759 if (rt2 == 31
2760 && ((rt | rs) & 1) == 0
2761 && dc_isar_feature(aa64_atomics, s)) {
2762 /* CASPA / CASPAL */
2763 gen_compare_and_swap_pair(s, rs, rt, rn, size | 2);
2764 return;
2766 break;
2768 case 0xa: /* CAS */
2769 case 0xb: /* CASL */
2770 case 0xe: /* CASA */
2771 case 0xf: /* CASAL */
2772 if (rt2 == 31 && dc_isar_feature(aa64_atomics, s)) {
2773 gen_compare_and_swap(s, rs, rt, rn, size);
2774 return;
2776 break;
2778 unallocated_encoding(s);
2782 * Load register (literal)
2784 * 31 30 29 27 26 25 24 23 5 4 0
2785 * +-----+-------+---+-----+-------------------+-------+
2786 * | opc | 0 1 1 | V | 0 0 | imm19 | Rt |
2787 * +-----+-------+---+-----+-------------------+-------+
2789 * V: 1 -> vector (simd/fp)
2790 * opc (non-vector): 00 -> 32 bit, 01 -> 64 bit,
2791 * 10-> 32 bit signed, 11 -> prefetch
2792 * opc (vector): 00 -> 32 bit, 01 -> 64 bit, 10 -> 128 bit (11 unallocated)
2794 static void disas_ld_lit(DisasContext *s, uint32_t insn)
2796 int rt = extract32(insn, 0, 5);
2797 int64_t imm = sextract32(insn, 5, 19) << 2;
2798 bool is_vector = extract32(insn, 26, 1);
2799 int opc = extract32(insn, 30, 2);
2800 bool is_signed = false;
2801 int size = 2;
2802 TCGv_i64 tcg_rt, clean_addr;
2804 if (is_vector) {
2805 if (opc == 3) {
2806 unallocated_encoding(s);
2807 return;
2809 size = 2 + opc;
2810 if (!fp_access_check(s)) {
2811 return;
2813 } else {
2814 if (opc == 3) {
2815 /* PRFM (literal) : prefetch */
2816 return;
2818 size = 2 + extract32(opc, 0, 1);
2819 is_signed = extract32(opc, 1, 1);
2822 tcg_rt = cpu_reg(s, rt);
2824 clean_addr = tcg_constant_i64(s->pc_curr + imm);
2825 if (is_vector) {
2826 do_fp_ld(s, rt, clean_addr, size);
2827 } else {
2828 /* Only unsigned 32bit loads target 32bit registers. */
2829 bool iss_sf = opc != 0;
2831 do_gpr_ld(s, tcg_rt, clean_addr, size + is_signed * MO_SIGN,
2832 false, true, rt, iss_sf, false);
2837 * LDNP (Load Pair - non-temporal hint)
2838 * LDP (Load Pair - non vector)
2839 * LDPSW (Load Pair Signed Word - non vector)
2840 * STNP (Store Pair - non-temporal hint)
2841 * STP (Store Pair - non vector)
2842 * LDNP (Load Pair of SIMD&FP - non-temporal hint)
2843 * LDP (Load Pair of SIMD&FP)
2844 * STNP (Store Pair of SIMD&FP - non-temporal hint)
2845 * STP (Store Pair of SIMD&FP)
2847 * 31 30 29 27 26 25 24 23 22 21 15 14 10 9 5 4 0
2848 * +-----+-------+---+---+-------+---+-----------------------------+
2849 * | opc | 1 0 1 | V | 0 | index | L | imm7 | Rt2 | Rn | Rt |
2850 * +-----+-------+---+---+-------+---+-------+-------+------+------+
2852 * opc: LDP/STP/LDNP/STNP 00 -> 32 bit, 10 -> 64 bit
2853 * LDPSW/STGP 01
2854 * LDP/STP/LDNP/STNP (SIMD) 00 -> 32 bit, 01 -> 64 bit, 10 -> 128 bit
2855 * V: 0 -> GPR, 1 -> Vector
2856 * idx: 00 -> signed offset with non-temporal hint, 01 -> post-index,
2857 * 10 -> signed offset, 11 -> pre-index
2858 * L: 0 -> Store 1 -> Load
2860 * Rt, Rt2 = GPR or SIMD registers to be stored
2861 * Rn = general purpose register containing address
2862 * imm7 = signed offset (multiple of 4 or 8 depending on size)
2864 static void disas_ldst_pair(DisasContext *s, uint32_t insn)
2866 int rt = extract32(insn, 0, 5);
2867 int rn = extract32(insn, 5, 5);
2868 int rt2 = extract32(insn, 10, 5);
2869 uint64_t offset = sextract64(insn, 15, 7);
2870 int index = extract32(insn, 23, 2);
2871 bool is_vector = extract32(insn, 26, 1);
2872 bool is_load = extract32(insn, 22, 1);
2873 int opc = extract32(insn, 30, 2);
2875 bool is_signed = false;
2876 bool postindex = false;
2877 bool wback = false;
2878 bool set_tag = false;
2880 TCGv_i64 clean_addr, dirty_addr;
2882 int size;
2884 if (opc == 3) {
2885 unallocated_encoding(s);
2886 return;
2889 if (is_vector) {
2890 size = 2 + opc;
2891 } else if (opc == 1 && !is_load) {
2892 /* STGP */
2893 if (!dc_isar_feature(aa64_mte_insn_reg, s) || index == 0) {
2894 unallocated_encoding(s);
2895 return;
2897 size = 3;
2898 set_tag = true;
2899 } else {
2900 size = 2 + extract32(opc, 1, 1);
2901 is_signed = extract32(opc, 0, 1);
2902 if (!is_load && is_signed) {
2903 unallocated_encoding(s);
2904 return;
2908 switch (index) {
2909 case 1: /* post-index */
2910 postindex = true;
2911 wback = true;
2912 break;
2913 case 0:
2914 /* signed offset with "non-temporal" hint. Since we don't emulate
2915 * caches we don't care about hints to the cache system about
2916 * data access patterns, and handle this identically to plain
2917 * signed offset.
2919 if (is_signed) {
2920 /* There is no non-temporal-hint version of LDPSW */
2921 unallocated_encoding(s);
2922 return;
2924 postindex = false;
2925 break;
2926 case 2: /* signed offset, rn not updated */
2927 postindex = false;
2928 break;
2929 case 3: /* pre-index */
2930 postindex = false;
2931 wback = true;
2932 break;
2935 if (is_vector && !fp_access_check(s)) {
2936 return;
2939 offset <<= (set_tag ? LOG2_TAG_GRANULE : size);
2941 if (rn == 31) {
2942 gen_check_sp_alignment(s);
2945 dirty_addr = read_cpu_reg_sp(s, rn, 1);
2946 if (!postindex) {
2947 tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
2950 if (set_tag) {
2951 if (!s->ata) {
2953 * TODO: We could rely on the stores below, at least for
2954 * system mode, if we arrange to add MO_ALIGN_16.
2956 gen_helper_stg_stub(cpu_env, dirty_addr);
2957 } else if (tb_cflags(s->base.tb) & CF_PARALLEL) {
2958 gen_helper_stg_parallel(cpu_env, dirty_addr, dirty_addr);
2959 } else {
2960 gen_helper_stg(cpu_env, dirty_addr, dirty_addr);
2964 clean_addr = gen_mte_checkN(s, dirty_addr, !is_load,
2965 (wback || rn != 31) && !set_tag, 2 << size);
2967 if (is_vector) {
2968 if (is_load) {
2969 do_fp_ld(s, rt, clean_addr, size);
2970 } else {
2971 do_fp_st(s, rt, clean_addr, size);
2973 tcg_gen_addi_i64(clean_addr, clean_addr, 1 << size);
2974 if (is_load) {
2975 do_fp_ld(s, rt2, clean_addr, size);
2976 } else {
2977 do_fp_st(s, rt2, clean_addr, size);
2979 } else {
2980 TCGv_i64 tcg_rt = cpu_reg(s, rt);
2981 TCGv_i64 tcg_rt2 = cpu_reg(s, rt2);
2983 if (is_load) {
2984 TCGv_i64 tmp = tcg_temp_new_i64();
2986 /* Do not modify tcg_rt before recognizing any exception
2987 * from the second load.
2989 do_gpr_ld(s, tmp, clean_addr, size + is_signed * MO_SIGN,
2990 false, false, 0, false, false);
2991 tcg_gen_addi_i64(clean_addr, clean_addr, 1 << size);
2992 do_gpr_ld(s, tcg_rt2, clean_addr, size + is_signed * MO_SIGN,
2993 false, false, 0, false, false);
2995 tcg_gen_mov_i64(tcg_rt, tmp);
2996 tcg_temp_free_i64(tmp);
2997 } else {
2998 do_gpr_st(s, tcg_rt, clean_addr, size,
2999 false, 0, false, false);
3000 tcg_gen_addi_i64(clean_addr, clean_addr, 1 << size);
3001 do_gpr_st(s, tcg_rt2, clean_addr, size,
3002 false, 0, false, false);
3006 if (wback) {
3007 if (postindex) {
3008 tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3010 tcg_gen_mov_i64(cpu_reg_sp(s, rn), dirty_addr);
3015 * Load/store (immediate post-indexed)
3016 * Load/store (immediate pre-indexed)
3017 * Load/store (unscaled immediate)
3019 * 31 30 29 27 26 25 24 23 22 21 20 12 11 10 9 5 4 0
3020 * +----+-------+---+-----+-----+---+--------+-----+------+------+
3021 * |size| 1 1 1 | V | 0 0 | opc | 0 | imm9 | idx | Rn | Rt |
3022 * +----+-------+---+-----+-----+---+--------+-----+------+------+
3024 * idx = 01 -> post-indexed, 11 pre-indexed, 00 unscaled imm. (no writeback)
3025 10 -> unprivileged
3026 * V = 0 -> non-vector
3027 * size: 00 -> 8 bit, 01 -> 16 bit, 10 -> 32 bit, 11 -> 64bit
3028 * opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
3030 static void disas_ldst_reg_imm9(DisasContext *s, uint32_t insn,
3031 int opc,
3032 int size,
3033 int rt,
3034 bool is_vector)
3036 int rn = extract32(insn, 5, 5);
3037 int imm9 = sextract32(insn, 12, 9);
3038 int idx = extract32(insn, 10, 2);
3039 bool is_signed = false;
3040 bool is_store = false;
3041 bool is_extended = false;
3042 bool is_unpriv = (idx == 2);
3043 bool iss_valid = !is_vector;
3044 bool post_index;
3045 bool writeback;
3046 int memidx;
3048 TCGv_i64 clean_addr, dirty_addr;
3050 if (is_vector) {
3051 size |= (opc & 2) << 1;
3052 if (size > 4 || is_unpriv) {
3053 unallocated_encoding(s);
3054 return;
3056 is_store = ((opc & 1) == 0);
3057 if (!fp_access_check(s)) {
3058 return;
3060 } else {
3061 if (size == 3 && opc == 2) {
3062 /* PRFM - prefetch */
3063 if (idx != 0) {
3064 unallocated_encoding(s);
3065 return;
3067 return;
3069 if (opc == 3 && size > 1) {
3070 unallocated_encoding(s);
3071 return;
3073 is_store = (opc == 0);
3074 is_signed = extract32(opc, 1, 1);
3075 is_extended = (size < 3) && extract32(opc, 0, 1);
3078 switch (idx) {
3079 case 0:
3080 case 2:
3081 post_index = false;
3082 writeback = false;
3083 break;
3084 case 1:
3085 post_index = true;
3086 writeback = true;
3087 break;
3088 case 3:
3089 post_index = false;
3090 writeback = true;
3091 break;
3092 default:
3093 g_assert_not_reached();
3096 if (rn == 31) {
3097 gen_check_sp_alignment(s);
3100 dirty_addr = read_cpu_reg_sp(s, rn, 1);
3101 if (!post_index) {
3102 tcg_gen_addi_i64(dirty_addr, dirty_addr, imm9);
3105 memidx = is_unpriv ? get_a64_user_mem_index(s) : get_mem_index(s);
3106 clean_addr = gen_mte_check1_mmuidx(s, dirty_addr, is_store,
3107 writeback || rn != 31,
3108 size, is_unpriv, memidx);
3110 if (is_vector) {
3111 if (is_store) {
3112 do_fp_st(s, rt, clean_addr, size);
3113 } else {
3114 do_fp_ld(s, rt, clean_addr, size);
3116 } else {
3117 TCGv_i64 tcg_rt = cpu_reg(s, rt);
3118 bool iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
3120 if (is_store) {
3121 do_gpr_st_memidx(s, tcg_rt, clean_addr, size, memidx,
3122 iss_valid, rt, iss_sf, false);
3123 } else {
3124 do_gpr_ld_memidx(s, tcg_rt, clean_addr, size + is_signed * MO_SIGN,
3125 is_extended, memidx,
3126 iss_valid, rt, iss_sf, false);
3130 if (writeback) {
3131 TCGv_i64 tcg_rn = cpu_reg_sp(s, rn);
3132 if (post_index) {
3133 tcg_gen_addi_i64(dirty_addr, dirty_addr, imm9);
3135 tcg_gen_mov_i64(tcg_rn, dirty_addr);
3140 * Load/store (register offset)
3142 * 31 30 29 27 26 25 24 23 22 21 20 16 15 13 12 11 10 9 5 4 0
3143 * +----+-------+---+-----+-----+---+------+-----+--+-----+----+----+
3144 * |size| 1 1 1 | V | 0 0 | opc | 1 | Rm | opt | S| 1 0 | Rn | Rt |
3145 * +----+-------+---+-----+-----+---+------+-----+--+-----+----+----+
3147 * For non-vector:
3148 * size: 00-> byte, 01 -> 16 bit, 10 -> 32bit, 11 -> 64bit
3149 * opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
3150 * For vector:
3151 * size is opc<1>:size<1:0> so 100 -> 128 bit; 110 and 111 unallocated
3152 * opc<0>: 0 -> store, 1 -> load
3153 * V: 1 -> vector/simd
3154 * opt: extend encoding (see DecodeRegExtend)
3155 * S: if S=1 then scale (essentially index by sizeof(size))
3156 * Rt: register to transfer into/out of
3157 * Rn: address register or SP for base
3158 * Rm: offset register or ZR for offset
3160 static void disas_ldst_reg_roffset(DisasContext *s, uint32_t insn,
3161 int opc,
3162 int size,
3163 int rt,
3164 bool is_vector)
3166 int rn = extract32(insn, 5, 5);
3167 int shift = extract32(insn, 12, 1);
3168 int rm = extract32(insn, 16, 5);
3169 int opt = extract32(insn, 13, 3);
3170 bool is_signed = false;
3171 bool is_store = false;
3172 bool is_extended = false;
3174 TCGv_i64 tcg_rm, clean_addr, dirty_addr;
3176 if (extract32(opt, 1, 1) == 0) {
3177 unallocated_encoding(s);
3178 return;
3181 if (is_vector) {
3182 size |= (opc & 2) << 1;
3183 if (size > 4) {
3184 unallocated_encoding(s);
3185 return;
3187 is_store = !extract32(opc, 0, 1);
3188 if (!fp_access_check(s)) {
3189 return;
3191 } else {
3192 if (size == 3 && opc == 2) {
3193 /* PRFM - prefetch */
3194 return;
3196 if (opc == 3 && size > 1) {
3197 unallocated_encoding(s);
3198 return;
3200 is_store = (opc == 0);
3201 is_signed = extract32(opc, 1, 1);
3202 is_extended = (size < 3) && extract32(opc, 0, 1);
3205 if (rn == 31) {
3206 gen_check_sp_alignment(s);
3208 dirty_addr = read_cpu_reg_sp(s, rn, 1);
3210 tcg_rm = read_cpu_reg(s, rm, 1);
3211 ext_and_shift_reg(tcg_rm, tcg_rm, opt, shift ? size : 0);
3213 tcg_gen_add_i64(dirty_addr, dirty_addr, tcg_rm);
3214 clean_addr = gen_mte_check1(s, dirty_addr, is_store, true, size);
3216 if (is_vector) {
3217 if (is_store) {
3218 do_fp_st(s, rt, clean_addr, size);
3219 } else {
3220 do_fp_ld(s, rt, clean_addr, size);
3222 } else {
3223 TCGv_i64 tcg_rt = cpu_reg(s, rt);
3224 bool iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
3225 if (is_store) {
3226 do_gpr_st(s, tcg_rt, clean_addr, size,
3227 true, rt, iss_sf, false);
3228 } else {
3229 do_gpr_ld(s, tcg_rt, clean_addr, size + is_signed * MO_SIGN,
3230 is_extended, true, rt, iss_sf, false);
3236 * Load/store (unsigned immediate)
3238 * 31 30 29 27 26 25 24 23 22 21 10 9 5
3239 * +----+-------+---+-----+-----+------------+-------+------+
3240 * |size| 1 1 1 | V | 0 1 | opc | imm12 | Rn | Rt |
3241 * +----+-------+---+-----+-----+------------+-------+------+
3243 * For non-vector:
3244 * size: 00-> byte, 01 -> 16 bit, 10 -> 32bit, 11 -> 64bit
3245 * opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
3246 * For vector:
3247 * size is opc<1>:size<1:0> so 100 -> 128 bit; 110 and 111 unallocated
3248 * opc<0>: 0 -> store, 1 -> load
3249 * Rn: base address register (inc SP)
3250 * Rt: target register
3252 static void disas_ldst_reg_unsigned_imm(DisasContext *s, uint32_t insn,
3253 int opc,
3254 int size,
3255 int rt,
3256 bool is_vector)
3258 int rn = extract32(insn, 5, 5);
3259 unsigned int imm12 = extract32(insn, 10, 12);
3260 unsigned int offset;
3262 TCGv_i64 clean_addr, dirty_addr;
3264 bool is_store;
3265 bool is_signed = false;
3266 bool is_extended = false;
3268 if (is_vector) {
3269 size |= (opc & 2) << 1;
3270 if (size > 4) {
3271 unallocated_encoding(s);
3272 return;
3274 is_store = !extract32(opc, 0, 1);
3275 if (!fp_access_check(s)) {
3276 return;
3278 } else {
3279 if (size == 3 && opc == 2) {
3280 /* PRFM - prefetch */
3281 return;
3283 if (opc == 3 && size > 1) {
3284 unallocated_encoding(s);
3285 return;
3287 is_store = (opc == 0);
3288 is_signed = extract32(opc, 1, 1);
3289 is_extended = (size < 3) && extract32(opc, 0, 1);
3292 if (rn == 31) {
3293 gen_check_sp_alignment(s);
3295 dirty_addr = read_cpu_reg_sp(s, rn, 1);
3296 offset = imm12 << size;
3297 tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3298 clean_addr = gen_mte_check1(s, dirty_addr, is_store, rn != 31, size);
3300 if (is_vector) {
3301 if (is_store) {
3302 do_fp_st(s, rt, clean_addr, size);
3303 } else {
3304 do_fp_ld(s, rt, clean_addr, size);
3306 } else {
3307 TCGv_i64 tcg_rt = cpu_reg(s, rt);
3308 bool iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
3309 if (is_store) {
3310 do_gpr_st(s, tcg_rt, clean_addr, size,
3311 true, rt, iss_sf, false);
3312 } else {
3313 do_gpr_ld(s, tcg_rt, clean_addr, size + is_signed * MO_SIGN,
3314 is_extended, true, rt, iss_sf, false);
3319 /* Atomic memory operations
3321 * 31 30 27 26 24 22 21 16 15 12 10 5 0
3322 * +------+-------+---+-----+-----+---+----+----+-----+-----+----+-----+
3323 * | size | 1 1 1 | V | 0 0 | A R | 1 | Rs | o3 | opc | 0 0 | Rn | Rt |
3324 * +------+-------+---+-----+-----+--------+----+-----+-----+----+-----+
3326 * Rt: the result register
3327 * Rn: base address or SP
3328 * Rs: the source register for the operation
3329 * V: vector flag (always 0 as of v8.3)
3330 * A: acquire flag
3331 * R: release flag
3333 static void disas_ldst_atomic(DisasContext *s, uint32_t insn,
3334 int size, int rt, bool is_vector)
3336 int rs = extract32(insn, 16, 5);
3337 int rn = extract32(insn, 5, 5);
3338 int o3_opc = extract32(insn, 12, 4);
3339 bool r = extract32(insn, 22, 1);
3340 bool a = extract32(insn, 23, 1);
3341 TCGv_i64 tcg_rs, tcg_rt, clean_addr;
3342 AtomicThreeOpFn *fn = NULL;
3343 MemOp mop = s->be_data | size | MO_ALIGN;
3345 if (is_vector || !dc_isar_feature(aa64_atomics, s)) {
3346 unallocated_encoding(s);
3347 return;
3349 switch (o3_opc) {
3350 case 000: /* LDADD */
3351 fn = tcg_gen_atomic_fetch_add_i64;
3352 break;
3353 case 001: /* LDCLR */
3354 fn = tcg_gen_atomic_fetch_and_i64;
3355 break;
3356 case 002: /* LDEOR */
3357 fn = tcg_gen_atomic_fetch_xor_i64;
3358 break;
3359 case 003: /* LDSET */
3360 fn = tcg_gen_atomic_fetch_or_i64;
3361 break;
3362 case 004: /* LDSMAX */
3363 fn = tcg_gen_atomic_fetch_smax_i64;
3364 mop |= MO_SIGN;
3365 break;
3366 case 005: /* LDSMIN */
3367 fn = tcg_gen_atomic_fetch_smin_i64;
3368 mop |= MO_SIGN;
3369 break;
3370 case 006: /* LDUMAX */
3371 fn = tcg_gen_atomic_fetch_umax_i64;
3372 break;
3373 case 007: /* LDUMIN */
3374 fn = tcg_gen_atomic_fetch_umin_i64;
3375 break;
3376 case 010: /* SWP */
3377 fn = tcg_gen_atomic_xchg_i64;
3378 break;
3379 case 014: /* LDAPR, LDAPRH, LDAPRB */
3380 if (!dc_isar_feature(aa64_rcpc_8_3, s) ||
3381 rs != 31 || a != 1 || r != 0) {
3382 unallocated_encoding(s);
3383 return;
3385 break;
3386 default:
3387 unallocated_encoding(s);
3388 return;
3391 if (rn == 31) {
3392 gen_check_sp_alignment(s);
3394 clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn), false, rn != 31, size);
3396 if (o3_opc == 014) {
3398 * LDAPR* are a special case because they are a simple load, not a
3399 * fetch-and-do-something op.
3400 * The architectural consistency requirements here are weaker than
3401 * full load-acquire (we only need "load-acquire processor consistent"),
3402 * but we choose to implement them as full LDAQ.
3404 do_gpr_ld(s, cpu_reg(s, rt), clean_addr, size, false,
3405 true, rt, disas_ldst_compute_iss_sf(size, false, 0), true);
3406 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
3407 return;
3410 tcg_rs = read_cpu_reg(s, rs, true);
3411 tcg_rt = cpu_reg(s, rt);
3413 if (o3_opc == 1) { /* LDCLR */
3414 tcg_gen_not_i64(tcg_rs, tcg_rs);
3417 /* The tcg atomic primitives are all full barriers. Therefore we
3418 * can ignore the Acquire and Release bits of this instruction.
3420 fn(tcg_rt, clean_addr, tcg_rs, get_mem_index(s), mop);
3422 if ((mop & MO_SIGN) && size != MO_64) {
3423 tcg_gen_ext32u_i64(tcg_rt, tcg_rt);
3428 * PAC memory operations
3430 * 31 30 27 26 24 22 21 12 11 10 5 0
3431 * +------+-------+---+-----+-----+---+--------+---+---+----+-----+
3432 * | size | 1 1 1 | V | 0 0 | M S | 1 | imm9 | W | 1 | Rn | Rt |
3433 * +------+-------+---+-----+-----+---+--------+---+---+----+-----+
3435 * Rt: the result register
3436 * Rn: base address or SP
3437 * V: vector flag (always 0 as of v8.3)
3438 * M: clear for key DA, set for key DB
3439 * W: pre-indexing flag
3440 * S: sign for imm9.
3442 static void disas_ldst_pac(DisasContext *s, uint32_t insn,
3443 int size, int rt, bool is_vector)
3445 int rn = extract32(insn, 5, 5);
3446 bool is_wback = extract32(insn, 11, 1);
3447 bool use_key_a = !extract32(insn, 23, 1);
3448 int offset;
3449 TCGv_i64 clean_addr, dirty_addr, tcg_rt;
3451 if (size != 3 || is_vector || !dc_isar_feature(aa64_pauth, s)) {
3452 unallocated_encoding(s);
3453 return;
3456 if (rn == 31) {
3457 gen_check_sp_alignment(s);
3459 dirty_addr = read_cpu_reg_sp(s, rn, 1);
3461 if (s->pauth_active) {
3462 if (use_key_a) {
3463 gen_helper_autda(dirty_addr, cpu_env, dirty_addr,
3464 new_tmp_a64_zero(s));
3465 } else {
3466 gen_helper_autdb(dirty_addr, cpu_env, dirty_addr,
3467 new_tmp_a64_zero(s));
3471 /* Form the 10-bit signed, scaled offset. */
3472 offset = (extract32(insn, 22, 1) << 9) | extract32(insn, 12, 9);
3473 offset = sextract32(offset << size, 0, 10 + size);
3474 tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3476 /* Note that "clean" and "dirty" here refer to TBI not PAC. */
3477 clean_addr = gen_mte_check1(s, dirty_addr, false,
3478 is_wback || rn != 31, size);
3480 tcg_rt = cpu_reg(s, rt);
3481 do_gpr_ld(s, tcg_rt, clean_addr, size,
3482 /* extend */ false, /* iss_valid */ !is_wback,
3483 /* iss_srt */ rt, /* iss_sf */ true, /* iss_ar */ false);
3485 if (is_wback) {
3486 tcg_gen_mov_i64(cpu_reg_sp(s, rn), dirty_addr);
3491 * LDAPR/STLR (unscaled immediate)
3493 * 31 30 24 22 21 12 10 5 0
3494 * +------+-------------+-----+---+--------+-----+----+-----+
3495 * | size | 0 1 1 0 0 1 | opc | 0 | imm9 | 0 0 | Rn | Rt |
3496 * +------+-------------+-----+---+--------+-----+----+-----+
3498 * Rt: source or destination register
3499 * Rn: base register
3500 * imm9: unscaled immediate offset
3501 * opc: 00: STLUR*, 01/10/11: various LDAPUR*
3502 * size: size of load/store
3504 static void disas_ldst_ldapr_stlr(DisasContext *s, uint32_t insn)
3506 int rt = extract32(insn, 0, 5);
3507 int rn = extract32(insn, 5, 5);
3508 int offset = sextract32(insn, 12, 9);
3509 int opc = extract32(insn, 22, 2);
3510 int size = extract32(insn, 30, 2);
3511 TCGv_i64 clean_addr, dirty_addr;
3512 bool is_store = false;
3513 bool extend = false;
3514 bool iss_sf;
3515 MemOp mop;
3517 if (!dc_isar_feature(aa64_rcpc_8_4, s)) {
3518 unallocated_encoding(s);
3519 return;
3522 /* TODO: ARMv8.4-LSE SCTLR.nAA */
3523 mop = size | MO_ALIGN;
3525 switch (opc) {
3526 case 0: /* STLURB */
3527 is_store = true;
3528 break;
3529 case 1: /* LDAPUR* */
3530 break;
3531 case 2: /* LDAPURS* 64-bit variant */
3532 if (size == 3) {
3533 unallocated_encoding(s);
3534 return;
3536 mop |= MO_SIGN;
3537 break;
3538 case 3: /* LDAPURS* 32-bit variant */
3539 if (size > 1) {
3540 unallocated_encoding(s);
3541 return;
3543 mop |= MO_SIGN;
3544 extend = true; /* zero-extend 32->64 after signed load */
3545 break;
3546 default:
3547 g_assert_not_reached();
3550 iss_sf = disas_ldst_compute_iss_sf(size, (mop & MO_SIGN) != 0, opc);
3552 if (rn == 31) {
3553 gen_check_sp_alignment(s);
3556 dirty_addr = read_cpu_reg_sp(s, rn, 1);
3557 tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3558 clean_addr = clean_data_tbi(s, dirty_addr);
3560 if (is_store) {
3561 /* Store-Release semantics */
3562 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
3563 do_gpr_st(s, cpu_reg(s, rt), clean_addr, mop, true, rt, iss_sf, true);
3564 } else {
3566 * Load-AcquirePC semantics; we implement as the slightly more
3567 * restrictive Load-Acquire.
3569 do_gpr_ld(s, cpu_reg(s, rt), clean_addr, mop,
3570 extend, true, rt, iss_sf, true);
3571 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
3575 /* Load/store register (all forms) */
3576 static void disas_ldst_reg(DisasContext *s, uint32_t insn)
3578 int rt = extract32(insn, 0, 5);
3579 int opc = extract32(insn, 22, 2);
3580 bool is_vector = extract32(insn, 26, 1);
3581 int size = extract32(insn, 30, 2);
3583 switch (extract32(insn, 24, 2)) {
3584 case 0:
3585 if (extract32(insn, 21, 1) == 0) {
3586 /* Load/store register (unscaled immediate)
3587 * Load/store immediate pre/post-indexed
3588 * Load/store register unprivileged
3590 disas_ldst_reg_imm9(s, insn, opc, size, rt, is_vector);
3591 return;
3593 switch (extract32(insn, 10, 2)) {
3594 case 0:
3595 disas_ldst_atomic(s, insn, size, rt, is_vector);
3596 return;
3597 case 2:
3598 disas_ldst_reg_roffset(s, insn, opc, size, rt, is_vector);
3599 return;
3600 default:
3601 disas_ldst_pac(s, insn, size, rt, is_vector);
3602 return;
3604 break;
3605 case 1:
3606 disas_ldst_reg_unsigned_imm(s, insn, opc, size, rt, is_vector);
3607 return;
3609 unallocated_encoding(s);
3612 /* AdvSIMD load/store multiple structures
3614 * 31 30 29 23 22 21 16 15 12 11 10 9 5 4 0
3615 * +---+---+---------------+---+-------------+--------+------+------+------+
3616 * | 0 | Q | 0 0 1 1 0 0 0 | L | 0 0 0 0 0 0 | opcode | size | Rn | Rt |
3617 * +---+---+---------------+---+-------------+--------+------+------+------+
3619 * AdvSIMD load/store multiple structures (post-indexed)
3621 * 31 30 29 23 22 21 20 16 15 12 11 10 9 5 4 0
3622 * +---+---+---------------+---+---+---------+--------+------+------+------+
3623 * | 0 | Q | 0 0 1 1 0 0 1 | L | 0 | Rm | opcode | size | Rn | Rt |
3624 * +---+---+---------------+---+---+---------+--------+------+------+------+
3626 * Rt: first (or only) SIMD&FP register to be transferred
3627 * Rn: base address or SP
3628 * Rm (post-index only): post-index register (when !31) or size dependent #imm
3630 static void disas_ldst_multiple_struct(DisasContext *s, uint32_t insn)
3632 int rt = extract32(insn, 0, 5);
3633 int rn = extract32(insn, 5, 5);
3634 int rm = extract32(insn, 16, 5);
3635 int size = extract32(insn, 10, 2);
3636 int opcode = extract32(insn, 12, 4);
3637 bool is_store = !extract32(insn, 22, 1);
3638 bool is_postidx = extract32(insn, 23, 1);
3639 bool is_q = extract32(insn, 30, 1);
3640 TCGv_i64 clean_addr, tcg_rn, tcg_ebytes;
3641 MemOp endian, align, mop;
3643 int total; /* total bytes */
3644 int elements; /* elements per vector */
3645 int rpt; /* num iterations */
3646 int selem; /* structure elements */
3647 int r;
3649 if (extract32(insn, 31, 1) || extract32(insn, 21, 1)) {
3650 unallocated_encoding(s);
3651 return;
3654 if (!is_postidx && rm != 0) {
3655 unallocated_encoding(s);
3656 return;
3659 /* From the shared decode logic */
3660 switch (opcode) {
3661 case 0x0:
3662 rpt = 1;
3663 selem = 4;
3664 break;
3665 case 0x2:
3666 rpt = 4;
3667 selem = 1;
3668 break;
3669 case 0x4:
3670 rpt = 1;
3671 selem = 3;
3672 break;
3673 case 0x6:
3674 rpt = 3;
3675 selem = 1;
3676 break;
3677 case 0x7:
3678 rpt = 1;
3679 selem = 1;
3680 break;
3681 case 0x8:
3682 rpt = 1;
3683 selem = 2;
3684 break;
3685 case 0xa:
3686 rpt = 2;
3687 selem = 1;
3688 break;
3689 default:
3690 unallocated_encoding(s);
3691 return;
3694 if (size == 3 && !is_q && selem != 1) {
3695 /* reserved */
3696 unallocated_encoding(s);
3697 return;
3700 if (!fp_access_check(s)) {
3701 return;
3704 if (rn == 31) {
3705 gen_check_sp_alignment(s);
3708 /* For our purposes, bytes are always little-endian. */
3709 endian = s->be_data;
3710 if (size == 0) {
3711 endian = MO_LE;
3714 total = rpt * selem * (is_q ? 16 : 8);
3715 tcg_rn = cpu_reg_sp(s, rn);
3718 * Issue the MTE check vs the logical repeat count, before we
3719 * promote consecutive little-endian elements below.
3721 clean_addr = gen_mte_checkN(s, tcg_rn, is_store, is_postidx || rn != 31,
3722 total);
3725 * Consecutive little-endian elements from a single register
3726 * can be promoted to a larger little-endian operation.
3728 align = MO_ALIGN;
3729 if (selem == 1 && endian == MO_LE) {
3730 align = pow2_align(size);
3731 size = 3;
3733 if (!s->align_mem) {
3734 align = 0;
3736 mop = endian | size | align;
3738 elements = (is_q ? 16 : 8) >> size;
3739 tcg_ebytes = tcg_constant_i64(1 << size);
3740 for (r = 0; r < rpt; r++) {
3741 int e;
3742 for (e = 0; e < elements; e++) {
3743 int xs;
3744 for (xs = 0; xs < selem; xs++) {
3745 int tt = (rt + r + xs) % 32;
3746 if (is_store) {
3747 do_vec_st(s, tt, e, clean_addr, mop);
3748 } else {
3749 do_vec_ld(s, tt, e, clean_addr, mop);
3751 tcg_gen_add_i64(clean_addr, clean_addr, tcg_ebytes);
3756 if (!is_store) {
3757 /* For non-quad operations, setting a slice of the low
3758 * 64 bits of the register clears the high 64 bits (in
3759 * the ARM ARM pseudocode this is implicit in the fact
3760 * that 'rval' is a 64 bit wide variable).
3761 * For quad operations, we might still need to zero the
3762 * high bits of SVE.
3764 for (r = 0; r < rpt * selem; r++) {
3765 int tt = (rt + r) % 32;
3766 clear_vec_high(s, is_q, tt);
3770 if (is_postidx) {
3771 if (rm == 31) {
3772 tcg_gen_addi_i64(tcg_rn, tcg_rn, total);
3773 } else {
3774 tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, rm));
3779 /* AdvSIMD load/store single structure
3781 * 31 30 29 23 22 21 20 16 15 13 12 11 10 9 5 4 0
3782 * +---+---+---------------+-----+-----------+-----+---+------+------+------+
3783 * | 0 | Q | 0 0 1 1 0 1 0 | L R | 0 0 0 0 0 | opc | S | size | Rn | Rt |
3784 * +---+---+---------------+-----+-----------+-----+---+------+------+------+
3786 * AdvSIMD load/store single structure (post-indexed)
3788 * 31 30 29 23 22 21 20 16 15 13 12 11 10 9 5 4 0
3789 * +---+---+---------------+-----+-----------+-----+---+------+------+------+
3790 * | 0 | Q | 0 0 1 1 0 1 1 | L R | Rm | opc | S | size | Rn | Rt |
3791 * +---+---+---------------+-----+-----------+-----+---+------+------+------+
3793 * Rt: first (or only) SIMD&FP register to be transferred
3794 * Rn: base address or SP
3795 * Rm (post-index only): post-index register (when !31) or size dependent #imm
3796 * index = encoded in Q:S:size dependent on size
3798 * lane_size = encoded in R, opc
3799 * transfer width = encoded in opc, S, size
3801 static void disas_ldst_single_struct(DisasContext *s, uint32_t insn)
3803 int rt = extract32(insn, 0, 5);
3804 int rn = extract32(insn, 5, 5);
3805 int rm = extract32(insn, 16, 5);
3806 int size = extract32(insn, 10, 2);
3807 int S = extract32(insn, 12, 1);
3808 int opc = extract32(insn, 13, 3);
3809 int R = extract32(insn, 21, 1);
3810 int is_load = extract32(insn, 22, 1);
3811 int is_postidx = extract32(insn, 23, 1);
3812 int is_q = extract32(insn, 30, 1);
3814 int scale = extract32(opc, 1, 2);
3815 int selem = (extract32(opc, 0, 1) << 1 | R) + 1;
3816 bool replicate = false;
3817 int index = is_q << 3 | S << 2 | size;
3818 int xs, total;
3819 TCGv_i64 clean_addr, tcg_rn, tcg_ebytes;
3820 MemOp mop;
3822 if (extract32(insn, 31, 1)) {
3823 unallocated_encoding(s);
3824 return;
3826 if (!is_postidx && rm != 0) {
3827 unallocated_encoding(s);
3828 return;
3831 switch (scale) {
3832 case 3:
3833 if (!is_load || S) {
3834 unallocated_encoding(s);
3835 return;
3837 scale = size;
3838 replicate = true;
3839 break;
3840 case 0:
3841 break;
3842 case 1:
3843 if (extract32(size, 0, 1)) {
3844 unallocated_encoding(s);
3845 return;
3847 index >>= 1;
3848 break;
3849 case 2:
3850 if (extract32(size, 1, 1)) {
3851 unallocated_encoding(s);
3852 return;
3854 if (!extract32(size, 0, 1)) {
3855 index >>= 2;
3856 } else {
3857 if (S) {
3858 unallocated_encoding(s);
3859 return;
3861 index >>= 3;
3862 scale = 3;
3864 break;
3865 default:
3866 g_assert_not_reached();
3869 if (!fp_access_check(s)) {
3870 return;
3873 if (rn == 31) {
3874 gen_check_sp_alignment(s);
3877 total = selem << scale;
3878 tcg_rn = cpu_reg_sp(s, rn);
3880 clean_addr = gen_mte_checkN(s, tcg_rn, !is_load, is_postidx || rn != 31,
3881 total);
3882 mop = finalize_memop(s, scale);
3884 tcg_ebytes = tcg_constant_i64(1 << scale);
3885 for (xs = 0; xs < selem; xs++) {
3886 if (replicate) {
3887 /* Load and replicate to all elements */
3888 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
3890 tcg_gen_qemu_ld_i64(tcg_tmp, clean_addr, get_mem_index(s), mop);
3891 tcg_gen_gvec_dup_i64(scale, vec_full_reg_offset(s, rt),
3892 (is_q + 1) * 8, vec_full_reg_size(s),
3893 tcg_tmp);
3894 tcg_temp_free_i64(tcg_tmp);
3895 } else {
3896 /* Load/store one element per register */
3897 if (is_load) {
3898 do_vec_ld(s, rt, index, clean_addr, mop);
3899 } else {
3900 do_vec_st(s, rt, index, clean_addr, mop);
3903 tcg_gen_add_i64(clean_addr, clean_addr, tcg_ebytes);
3904 rt = (rt + 1) % 32;
3907 if (is_postidx) {
3908 if (rm == 31) {
3909 tcg_gen_addi_i64(tcg_rn, tcg_rn, total);
3910 } else {
3911 tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, rm));
3917 * Load/Store memory tags
3919 * 31 30 29 24 22 21 12 10 5 0
3920 * +-----+-------------+-----+---+------+-----+------+------+
3921 * | 1 1 | 0 1 1 0 0 1 | op1 | 1 | imm9 | op2 | Rn | Rt |
3922 * +-----+-------------+-----+---+------+-----+------+------+
3924 static void disas_ldst_tag(DisasContext *s, uint32_t insn)
3926 int rt = extract32(insn, 0, 5);
3927 int rn = extract32(insn, 5, 5);
3928 uint64_t offset = sextract64(insn, 12, 9) << LOG2_TAG_GRANULE;
3929 int op2 = extract32(insn, 10, 2);
3930 int op1 = extract32(insn, 22, 2);
3931 bool is_load = false, is_pair = false, is_zero = false, is_mult = false;
3932 int index = 0;
3933 TCGv_i64 addr, clean_addr, tcg_rt;
3935 /* We checked insn bits [29:24,21] in the caller. */
3936 if (extract32(insn, 30, 2) != 3) {
3937 goto do_unallocated;
3941 * @index is a tri-state variable which has 3 states:
3942 * < 0 : post-index, writeback
3943 * = 0 : signed offset
3944 * > 0 : pre-index, writeback
3946 switch (op1) {
3947 case 0:
3948 if (op2 != 0) {
3949 /* STG */
3950 index = op2 - 2;
3951 } else {
3952 /* STZGM */
3953 if (s->current_el == 0 || offset != 0) {
3954 goto do_unallocated;
3956 is_mult = is_zero = true;
3958 break;
3959 case 1:
3960 if (op2 != 0) {
3961 /* STZG */
3962 is_zero = true;
3963 index = op2 - 2;
3964 } else {
3965 /* LDG */
3966 is_load = true;
3968 break;
3969 case 2:
3970 if (op2 != 0) {
3971 /* ST2G */
3972 is_pair = true;
3973 index = op2 - 2;
3974 } else {
3975 /* STGM */
3976 if (s->current_el == 0 || offset != 0) {
3977 goto do_unallocated;
3979 is_mult = true;
3981 break;
3982 case 3:
3983 if (op2 != 0) {
3984 /* STZ2G */
3985 is_pair = is_zero = true;
3986 index = op2 - 2;
3987 } else {
3988 /* LDGM */
3989 if (s->current_el == 0 || offset != 0) {
3990 goto do_unallocated;
3992 is_mult = is_load = true;
3994 break;
3996 default:
3997 do_unallocated:
3998 unallocated_encoding(s);
3999 return;
4002 if (is_mult
4003 ? !dc_isar_feature(aa64_mte, s)
4004 : !dc_isar_feature(aa64_mte_insn_reg, s)) {
4005 goto do_unallocated;
4008 if (rn == 31) {
4009 gen_check_sp_alignment(s);
4012 addr = read_cpu_reg_sp(s, rn, true);
4013 if (index >= 0) {
4014 /* pre-index or signed offset */
4015 tcg_gen_addi_i64(addr, addr, offset);
4018 if (is_mult) {
4019 tcg_rt = cpu_reg(s, rt);
4021 if (is_zero) {
4022 int size = 4 << s->dcz_blocksize;
4024 if (s->ata) {
4025 gen_helper_stzgm_tags(cpu_env, addr, tcg_rt);
4028 * The non-tags portion of STZGM is mostly like DC_ZVA,
4029 * except the alignment happens before the access.
4031 clean_addr = clean_data_tbi(s, addr);
4032 tcg_gen_andi_i64(clean_addr, clean_addr, -size);
4033 gen_helper_dc_zva(cpu_env, clean_addr);
4034 } else if (s->ata) {
4035 if (is_load) {
4036 gen_helper_ldgm(tcg_rt, cpu_env, addr);
4037 } else {
4038 gen_helper_stgm(cpu_env, addr, tcg_rt);
4040 } else {
4041 MMUAccessType acc = is_load ? MMU_DATA_LOAD : MMU_DATA_STORE;
4042 int size = 4 << GMID_EL1_BS;
4044 clean_addr = clean_data_tbi(s, addr);
4045 tcg_gen_andi_i64(clean_addr, clean_addr, -size);
4046 gen_probe_access(s, clean_addr, acc, size);
4048 if (is_load) {
4049 /* The result tags are zeros. */
4050 tcg_gen_movi_i64(tcg_rt, 0);
4053 return;
4056 if (is_load) {
4057 tcg_gen_andi_i64(addr, addr, -TAG_GRANULE);
4058 tcg_rt = cpu_reg(s, rt);
4059 if (s->ata) {
4060 gen_helper_ldg(tcg_rt, cpu_env, addr, tcg_rt);
4061 } else {
4062 clean_addr = clean_data_tbi(s, addr);
4063 gen_probe_access(s, clean_addr, MMU_DATA_LOAD, MO_8);
4064 gen_address_with_allocation_tag0(tcg_rt, addr);
4066 } else {
4067 tcg_rt = cpu_reg_sp(s, rt);
4068 if (!s->ata) {
4070 * For STG and ST2G, we need to check alignment and probe memory.
4071 * TODO: For STZG and STZ2G, we could rely on the stores below,
4072 * at least for system mode; user-only won't enforce alignment.
4074 if (is_pair) {
4075 gen_helper_st2g_stub(cpu_env, addr);
4076 } else {
4077 gen_helper_stg_stub(cpu_env, addr);
4079 } else if (tb_cflags(s->base.tb) & CF_PARALLEL) {
4080 if (is_pair) {
4081 gen_helper_st2g_parallel(cpu_env, addr, tcg_rt);
4082 } else {
4083 gen_helper_stg_parallel(cpu_env, addr, tcg_rt);
4085 } else {
4086 if (is_pair) {
4087 gen_helper_st2g(cpu_env, addr, tcg_rt);
4088 } else {
4089 gen_helper_stg(cpu_env, addr, tcg_rt);
4094 if (is_zero) {
4095 TCGv_i64 clean_addr = clean_data_tbi(s, addr);
4096 TCGv_i64 tcg_zero = tcg_constant_i64(0);
4097 int mem_index = get_mem_index(s);
4098 int i, n = (1 + is_pair) << LOG2_TAG_GRANULE;
4100 tcg_gen_qemu_st_i64(tcg_zero, clean_addr, mem_index,
4101 MO_UQ | MO_ALIGN_16);
4102 for (i = 8; i < n; i += 8) {
4103 tcg_gen_addi_i64(clean_addr, clean_addr, 8);
4104 tcg_gen_qemu_st_i64(tcg_zero, clean_addr, mem_index, MO_UQ);
4108 if (index != 0) {
4109 /* pre-index or post-index */
4110 if (index < 0) {
4111 /* post-index */
4112 tcg_gen_addi_i64(addr, addr, offset);
4114 tcg_gen_mov_i64(cpu_reg_sp(s, rn), addr);
4118 /* Loads and stores */
4119 static void disas_ldst(DisasContext *s, uint32_t insn)
4121 switch (extract32(insn, 24, 6)) {
4122 case 0x08: /* Load/store exclusive */
4123 disas_ldst_excl(s, insn);
4124 break;
4125 case 0x18: case 0x1c: /* Load register (literal) */
4126 disas_ld_lit(s, insn);
4127 break;
4128 case 0x28: case 0x29:
4129 case 0x2c: case 0x2d: /* Load/store pair (all forms) */
4130 disas_ldst_pair(s, insn);
4131 break;
4132 case 0x38: case 0x39:
4133 case 0x3c: case 0x3d: /* Load/store register (all forms) */
4134 disas_ldst_reg(s, insn);
4135 break;
4136 case 0x0c: /* AdvSIMD load/store multiple structures */
4137 disas_ldst_multiple_struct(s, insn);
4138 break;
4139 case 0x0d: /* AdvSIMD load/store single structure */
4140 disas_ldst_single_struct(s, insn);
4141 break;
4142 case 0x19:
4143 if (extract32(insn, 21, 1) != 0) {
4144 disas_ldst_tag(s, insn);
4145 } else if (extract32(insn, 10, 2) == 0) {
4146 disas_ldst_ldapr_stlr(s, insn);
4147 } else {
4148 unallocated_encoding(s);
4150 break;
4151 default:
4152 unallocated_encoding(s);
4153 break;
4157 /* PC-rel. addressing
4158 * 31 30 29 28 24 23 5 4 0
4159 * +----+-------+-----------+-------------------+------+
4160 * | op | immlo | 1 0 0 0 0 | immhi | Rd |
4161 * +----+-------+-----------+-------------------+------+
4163 static void disas_pc_rel_adr(DisasContext *s, uint32_t insn)
4165 unsigned int page, rd;
4166 uint64_t base;
4167 uint64_t offset;
4169 page = extract32(insn, 31, 1);
4170 /* SignExtend(immhi:immlo) -> offset */
4171 offset = sextract64(insn, 5, 19);
4172 offset = offset << 2 | extract32(insn, 29, 2);
4173 rd = extract32(insn, 0, 5);
4174 base = s->pc_curr;
4176 if (page) {
4177 /* ADRP (page based) */
4178 base &= ~0xfff;
4179 offset <<= 12;
4182 tcg_gen_movi_i64(cpu_reg(s, rd), base + offset);
4186 * Add/subtract (immediate)
4188 * 31 30 29 28 23 22 21 10 9 5 4 0
4189 * +--+--+--+-------------+--+-------------+-----+-----+
4190 * |sf|op| S| 1 0 0 0 1 0 |sh| imm12 | Rn | Rd |
4191 * +--+--+--+-------------+--+-------------+-----+-----+
4193 * sf: 0 -> 32bit, 1 -> 64bit
4194 * op: 0 -> add , 1 -> sub
4195 * S: 1 -> set flags
4196 * sh: 1 -> LSL imm by 12
4198 static void disas_add_sub_imm(DisasContext *s, uint32_t insn)
4200 int rd = extract32(insn, 0, 5);
4201 int rn = extract32(insn, 5, 5);
4202 uint64_t imm = extract32(insn, 10, 12);
4203 bool shift = extract32(insn, 22, 1);
4204 bool setflags = extract32(insn, 29, 1);
4205 bool sub_op = extract32(insn, 30, 1);
4206 bool is_64bit = extract32(insn, 31, 1);
4208 TCGv_i64 tcg_rn = cpu_reg_sp(s, rn);
4209 TCGv_i64 tcg_rd = setflags ? cpu_reg(s, rd) : cpu_reg_sp(s, rd);
4210 TCGv_i64 tcg_result;
4212 if (shift) {
4213 imm <<= 12;
4216 tcg_result = tcg_temp_new_i64();
4217 if (!setflags) {
4218 if (sub_op) {
4219 tcg_gen_subi_i64(tcg_result, tcg_rn, imm);
4220 } else {
4221 tcg_gen_addi_i64(tcg_result, tcg_rn, imm);
4223 } else {
4224 TCGv_i64 tcg_imm = tcg_constant_i64(imm);
4225 if (sub_op) {
4226 gen_sub_CC(is_64bit, tcg_result, tcg_rn, tcg_imm);
4227 } else {
4228 gen_add_CC(is_64bit, tcg_result, tcg_rn, tcg_imm);
4232 if (is_64bit) {
4233 tcg_gen_mov_i64(tcg_rd, tcg_result);
4234 } else {
4235 tcg_gen_ext32u_i64(tcg_rd, tcg_result);
4238 tcg_temp_free_i64(tcg_result);
4242 * Add/subtract (immediate, with tags)
4244 * 31 30 29 28 23 22 21 16 14 10 9 5 4 0
4245 * +--+--+--+-------------+--+---------+--+-------+-----+-----+
4246 * |sf|op| S| 1 0 0 0 1 1 |o2| uimm6 |o3| uimm4 | Rn | Rd |
4247 * +--+--+--+-------------+--+---------+--+-------+-----+-----+
4249 * op: 0 -> add, 1 -> sub
4251 static void disas_add_sub_imm_with_tags(DisasContext *s, uint32_t insn)
4253 int rd = extract32(insn, 0, 5);
4254 int rn = extract32(insn, 5, 5);
4255 int uimm4 = extract32(insn, 10, 4);
4256 int uimm6 = extract32(insn, 16, 6);
4257 bool sub_op = extract32(insn, 30, 1);
4258 TCGv_i64 tcg_rn, tcg_rd;
4259 int imm;
4261 /* Test all of sf=1, S=0, o2=0, o3=0. */
4262 if ((insn & 0xa040c000u) != 0x80000000u ||
4263 !dc_isar_feature(aa64_mte_insn_reg, s)) {
4264 unallocated_encoding(s);
4265 return;
4268 imm = uimm6 << LOG2_TAG_GRANULE;
4269 if (sub_op) {
4270 imm = -imm;
4273 tcg_rn = cpu_reg_sp(s, rn);
4274 tcg_rd = cpu_reg_sp(s, rd);
4276 if (s->ata) {
4277 gen_helper_addsubg(tcg_rd, cpu_env, tcg_rn,
4278 tcg_constant_i32(imm),
4279 tcg_constant_i32(uimm4));
4280 } else {
4281 tcg_gen_addi_i64(tcg_rd, tcg_rn, imm);
4282 gen_address_with_allocation_tag0(tcg_rd, tcg_rd);
4286 /* The input should be a value in the bottom e bits (with higher
4287 * bits zero); returns that value replicated into every element
4288 * of size e in a 64 bit integer.
4290 static uint64_t bitfield_replicate(uint64_t mask, unsigned int e)
4292 assert(e != 0);
4293 while (e < 64) {
4294 mask |= mask << e;
4295 e *= 2;
4297 return mask;
4300 /* Return a value with the bottom len bits set (where 0 < len <= 64) */
4301 static inline uint64_t bitmask64(unsigned int length)
4303 assert(length > 0 && length <= 64);
4304 return ~0ULL >> (64 - length);
4307 /* Simplified variant of pseudocode DecodeBitMasks() for the case where we
4308 * only require the wmask. Returns false if the imms/immr/immn are a reserved
4309 * value (ie should cause a guest UNDEF exception), and true if they are
4310 * valid, in which case the decoded bit pattern is written to result.
4312 bool logic_imm_decode_wmask(uint64_t *result, unsigned int immn,
4313 unsigned int imms, unsigned int immr)
4315 uint64_t mask;
4316 unsigned e, levels, s, r;
4317 int len;
4319 assert(immn < 2 && imms < 64 && immr < 64);
4321 /* The bit patterns we create here are 64 bit patterns which
4322 * are vectors of identical elements of size e = 2, 4, 8, 16, 32 or
4323 * 64 bits each. Each element contains the same value: a run
4324 * of between 1 and e-1 non-zero bits, rotated within the
4325 * element by between 0 and e-1 bits.
4327 * The element size and run length are encoded into immn (1 bit)
4328 * and imms (6 bits) as follows:
4329 * 64 bit elements: immn = 1, imms = <length of run - 1>
4330 * 32 bit elements: immn = 0, imms = 0 : <length of run - 1>
4331 * 16 bit elements: immn = 0, imms = 10 : <length of run - 1>
4332 * 8 bit elements: immn = 0, imms = 110 : <length of run - 1>
4333 * 4 bit elements: immn = 0, imms = 1110 : <length of run - 1>
4334 * 2 bit elements: immn = 0, imms = 11110 : <length of run - 1>
4335 * Notice that immn = 0, imms = 11111x is the only combination
4336 * not covered by one of the above options; this is reserved.
4337 * Further, <length of run - 1> all-ones is a reserved pattern.
4339 * In all cases the rotation is by immr % e (and immr is 6 bits).
4342 /* First determine the element size */
4343 len = 31 - clz32((immn << 6) | (~imms & 0x3f));
4344 if (len < 1) {
4345 /* This is the immn == 0, imms == 0x11111x case */
4346 return false;
4348 e = 1 << len;
4350 levels = e - 1;
4351 s = imms & levels;
4352 r = immr & levels;
4354 if (s == levels) {
4355 /* <length of run - 1> mustn't be all-ones. */
4356 return false;
4359 /* Create the value of one element: s+1 set bits rotated
4360 * by r within the element (which is e bits wide)...
4362 mask = bitmask64(s + 1);
4363 if (r) {
4364 mask = (mask >> r) | (mask << (e - r));
4365 mask &= bitmask64(e);
4367 /* ...then replicate the element over the whole 64 bit value */
4368 mask = bitfield_replicate(mask, e);
4369 *result = mask;
4370 return true;
4373 /* Logical (immediate)
4374 * 31 30 29 28 23 22 21 16 15 10 9 5 4 0
4375 * +----+-----+-------------+---+------+------+------+------+
4376 * | sf | opc | 1 0 0 1 0 0 | N | immr | imms | Rn | Rd |
4377 * +----+-----+-------------+---+------+------+------+------+
4379 static void disas_logic_imm(DisasContext *s, uint32_t insn)
4381 unsigned int sf, opc, is_n, immr, imms, rn, rd;
4382 TCGv_i64 tcg_rd, tcg_rn;
4383 uint64_t wmask;
4384 bool is_and = false;
4386 sf = extract32(insn, 31, 1);
4387 opc = extract32(insn, 29, 2);
4388 is_n = extract32(insn, 22, 1);
4389 immr = extract32(insn, 16, 6);
4390 imms = extract32(insn, 10, 6);
4391 rn = extract32(insn, 5, 5);
4392 rd = extract32(insn, 0, 5);
4394 if (!sf && is_n) {
4395 unallocated_encoding(s);
4396 return;
4399 if (opc == 0x3) { /* ANDS */
4400 tcg_rd = cpu_reg(s, rd);
4401 } else {
4402 tcg_rd = cpu_reg_sp(s, rd);
4404 tcg_rn = cpu_reg(s, rn);
4406 if (!logic_imm_decode_wmask(&wmask, is_n, imms, immr)) {
4407 /* some immediate field values are reserved */
4408 unallocated_encoding(s);
4409 return;
4412 if (!sf) {
4413 wmask &= 0xffffffff;
4416 switch (opc) {
4417 case 0x3: /* ANDS */
4418 case 0x0: /* AND */
4419 tcg_gen_andi_i64(tcg_rd, tcg_rn, wmask);
4420 is_and = true;
4421 break;
4422 case 0x1: /* ORR */
4423 tcg_gen_ori_i64(tcg_rd, tcg_rn, wmask);
4424 break;
4425 case 0x2: /* EOR */
4426 tcg_gen_xori_i64(tcg_rd, tcg_rn, wmask);
4427 break;
4428 default:
4429 assert(FALSE); /* must handle all above */
4430 break;
4433 if (!sf && !is_and) {
4434 /* zero extend final result; we know we can skip this for AND
4435 * since the immediate had the high 32 bits clear.
4437 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4440 if (opc == 3) { /* ANDS */
4441 gen_logic_CC(sf, tcg_rd);
4446 * Move wide (immediate)
4448 * 31 30 29 28 23 22 21 20 5 4 0
4449 * +--+-----+-------------+-----+----------------+------+
4450 * |sf| opc | 1 0 0 1 0 1 | hw | imm16 | Rd |
4451 * +--+-----+-------------+-----+----------------+------+
4453 * sf: 0 -> 32 bit, 1 -> 64 bit
4454 * opc: 00 -> N, 10 -> Z, 11 -> K
4455 * hw: shift/16 (0,16, and sf only 32, 48)
4457 static void disas_movw_imm(DisasContext *s, uint32_t insn)
4459 int rd = extract32(insn, 0, 5);
4460 uint64_t imm = extract32(insn, 5, 16);
4461 int sf = extract32(insn, 31, 1);
4462 int opc = extract32(insn, 29, 2);
4463 int pos = extract32(insn, 21, 2) << 4;
4464 TCGv_i64 tcg_rd = cpu_reg(s, rd);
4466 if (!sf && (pos >= 32)) {
4467 unallocated_encoding(s);
4468 return;
4471 switch (opc) {
4472 case 0: /* MOVN */
4473 case 2: /* MOVZ */
4474 imm <<= pos;
4475 if (opc == 0) {
4476 imm = ~imm;
4478 if (!sf) {
4479 imm &= 0xffffffffu;
4481 tcg_gen_movi_i64(tcg_rd, imm);
4482 break;
4483 case 3: /* MOVK */
4484 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_constant_i64(imm), pos, 16);
4485 if (!sf) {
4486 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4488 break;
4489 default:
4490 unallocated_encoding(s);
4491 break;
4495 /* Bitfield
4496 * 31 30 29 28 23 22 21 16 15 10 9 5 4 0
4497 * +----+-----+-------------+---+------+------+------+------+
4498 * | sf | opc | 1 0 0 1 1 0 | N | immr | imms | Rn | Rd |
4499 * +----+-----+-------------+---+------+------+------+------+
4501 static void disas_bitfield(DisasContext *s, uint32_t insn)
4503 unsigned int sf, n, opc, ri, si, rn, rd, bitsize, pos, len;
4504 TCGv_i64 tcg_rd, tcg_tmp;
4506 sf = extract32(insn, 31, 1);
4507 opc = extract32(insn, 29, 2);
4508 n = extract32(insn, 22, 1);
4509 ri = extract32(insn, 16, 6);
4510 si = extract32(insn, 10, 6);
4511 rn = extract32(insn, 5, 5);
4512 rd = extract32(insn, 0, 5);
4513 bitsize = sf ? 64 : 32;
4515 if (sf != n || ri >= bitsize || si >= bitsize || opc > 2) {
4516 unallocated_encoding(s);
4517 return;
4520 tcg_rd = cpu_reg(s, rd);
4522 /* Suppress the zero-extend for !sf. Since RI and SI are constrained
4523 to be smaller than bitsize, we'll never reference data outside the
4524 low 32-bits anyway. */
4525 tcg_tmp = read_cpu_reg(s, rn, 1);
4527 /* Recognize simple(r) extractions. */
4528 if (si >= ri) {
4529 /* Wd<s-r:0> = Wn<s:r> */
4530 len = (si - ri) + 1;
4531 if (opc == 0) { /* SBFM: ASR, SBFX, SXTB, SXTH, SXTW */
4532 tcg_gen_sextract_i64(tcg_rd, tcg_tmp, ri, len);
4533 goto done;
4534 } else if (opc == 2) { /* UBFM: UBFX, LSR, UXTB, UXTH */
4535 tcg_gen_extract_i64(tcg_rd, tcg_tmp, ri, len);
4536 return;
4538 /* opc == 1, BFXIL fall through to deposit */
4539 tcg_gen_shri_i64(tcg_tmp, tcg_tmp, ri);
4540 pos = 0;
4541 } else {
4542 /* Handle the ri > si case with a deposit
4543 * Wd<32+s-r,32-r> = Wn<s:0>
4545 len = si + 1;
4546 pos = (bitsize - ri) & (bitsize - 1);
4549 if (opc == 0 && len < ri) {
4550 /* SBFM: sign extend the destination field from len to fill
4551 the balance of the word. Let the deposit below insert all
4552 of those sign bits. */
4553 tcg_gen_sextract_i64(tcg_tmp, tcg_tmp, 0, len);
4554 len = ri;
4557 if (opc == 1) { /* BFM, BFXIL */
4558 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, pos, len);
4559 } else {
4560 /* SBFM or UBFM: We start with zero, and we haven't modified
4561 any bits outside bitsize, therefore the zero-extension
4562 below is unneeded. */
4563 tcg_gen_deposit_z_i64(tcg_rd, tcg_tmp, pos, len);
4564 return;
4567 done:
4568 if (!sf) { /* zero extend final result */
4569 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4573 /* Extract
4574 * 31 30 29 28 23 22 21 20 16 15 10 9 5 4 0
4575 * +----+------+-------------+---+----+------+--------+------+------+
4576 * | sf | op21 | 1 0 0 1 1 1 | N | o0 | Rm | imms | Rn | Rd |
4577 * +----+------+-------------+---+----+------+--------+------+------+
4579 static void disas_extract(DisasContext *s, uint32_t insn)
4581 unsigned int sf, n, rm, imm, rn, rd, bitsize, op21, op0;
4583 sf = extract32(insn, 31, 1);
4584 n = extract32(insn, 22, 1);
4585 rm = extract32(insn, 16, 5);
4586 imm = extract32(insn, 10, 6);
4587 rn = extract32(insn, 5, 5);
4588 rd = extract32(insn, 0, 5);
4589 op21 = extract32(insn, 29, 2);
4590 op0 = extract32(insn, 21, 1);
4591 bitsize = sf ? 64 : 32;
4593 if (sf != n || op21 || op0 || imm >= bitsize) {
4594 unallocated_encoding(s);
4595 } else {
4596 TCGv_i64 tcg_rd, tcg_rm, tcg_rn;
4598 tcg_rd = cpu_reg(s, rd);
4600 if (unlikely(imm == 0)) {
4601 /* tcg shl_i32/shl_i64 is undefined for 32/64 bit shifts,
4602 * so an extract from bit 0 is a special case.
4604 if (sf) {
4605 tcg_gen_mov_i64(tcg_rd, cpu_reg(s, rm));
4606 } else {
4607 tcg_gen_ext32u_i64(tcg_rd, cpu_reg(s, rm));
4609 } else {
4610 tcg_rm = cpu_reg(s, rm);
4611 tcg_rn = cpu_reg(s, rn);
4613 if (sf) {
4614 /* Specialization to ROR happens in EXTRACT2. */
4615 tcg_gen_extract2_i64(tcg_rd, tcg_rm, tcg_rn, imm);
4616 } else {
4617 TCGv_i32 t0 = tcg_temp_new_i32();
4619 tcg_gen_extrl_i64_i32(t0, tcg_rm);
4620 if (rm == rn) {
4621 tcg_gen_rotri_i32(t0, t0, imm);
4622 } else {
4623 TCGv_i32 t1 = tcg_temp_new_i32();
4624 tcg_gen_extrl_i64_i32(t1, tcg_rn);
4625 tcg_gen_extract2_i32(t0, t0, t1, imm);
4626 tcg_temp_free_i32(t1);
4628 tcg_gen_extu_i32_i64(tcg_rd, t0);
4629 tcg_temp_free_i32(t0);
4635 /* Data processing - immediate */
4636 static void disas_data_proc_imm(DisasContext *s, uint32_t insn)
4638 switch (extract32(insn, 23, 6)) {
4639 case 0x20: case 0x21: /* PC-rel. addressing */
4640 disas_pc_rel_adr(s, insn);
4641 break;
4642 case 0x22: /* Add/subtract (immediate) */
4643 disas_add_sub_imm(s, insn);
4644 break;
4645 case 0x23: /* Add/subtract (immediate, with tags) */
4646 disas_add_sub_imm_with_tags(s, insn);
4647 break;
4648 case 0x24: /* Logical (immediate) */
4649 disas_logic_imm(s, insn);
4650 break;
4651 case 0x25: /* Move wide (immediate) */
4652 disas_movw_imm(s, insn);
4653 break;
4654 case 0x26: /* Bitfield */
4655 disas_bitfield(s, insn);
4656 break;
4657 case 0x27: /* Extract */
4658 disas_extract(s, insn);
4659 break;
4660 default:
4661 unallocated_encoding(s);
4662 break;
4666 /* Shift a TCGv src by TCGv shift_amount, put result in dst.
4667 * Note that it is the caller's responsibility to ensure that the
4668 * shift amount is in range (ie 0..31 or 0..63) and provide the ARM
4669 * mandated semantics for out of range shifts.
4671 static void shift_reg(TCGv_i64 dst, TCGv_i64 src, int sf,
4672 enum a64_shift_type shift_type, TCGv_i64 shift_amount)
4674 switch (shift_type) {
4675 case A64_SHIFT_TYPE_LSL:
4676 tcg_gen_shl_i64(dst, src, shift_amount);
4677 break;
4678 case A64_SHIFT_TYPE_LSR:
4679 tcg_gen_shr_i64(dst, src, shift_amount);
4680 break;
4681 case A64_SHIFT_TYPE_ASR:
4682 if (!sf) {
4683 tcg_gen_ext32s_i64(dst, src);
4685 tcg_gen_sar_i64(dst, sf ? src : dst, shift_amount);
4686 break;
4687 case A64_SHIFT_TYPE_ROR:
4688 if (sf) {
4689 tcg_gen_rotr_i64(dst, src, shift_amount);
4690 } else {
4691 TCGv_i32 t0, t1;
4692 t0 = tcg_temp_new_i32();
4693 t1 = tcg_temp_new_i32();
4694 tcg_gen_extrl_i64_i32(t0, src);
4695 tcg_gen_extrl_i64_i32(t1, shift_amount);
4696 tcg_gen_rotr_i32(t0, t0, t1);
4697 tcg_gen_extu_i32_i64(dst, t0);
4698 tcg_temp_free_i32(t0);
4699 tcg_temp_free_i32(t1);
4701 break;
4702 default:
4703 assert(FALSE); /* all shift types should be handled */
4704 break;
4707 if (!sf) { /* zero extend final result */
4708 tcg_gen_ext32u_i64(dst, dst);
4712 /* Shift a TCGv src by immediate, put result in dst.
4713 * The shift amount must be in range (this should always be true as the
4714 * relevant instructions will UNDEF on bad shift immediates).
4716 static void shift_reg_imm(TCGv_i64 dst, TCGv_i64 src, int sf,
4717 enum a64_shift_type shift_type, unsigned int shift_i)
4719 assert(shift_i < (sf ? 64 : 32));
4721 if (shift_i == 0) {
4722 tcg_gen_mov_i64(dst, src);
4723 } else {
4724 shift_reg(dst, src, sf, shift_type, tcg_constant_i64(shift_i));
4728 /* Logical (shifted register)
4729 * 31 30 29 28 24 23 22 21 20 16 15 10 9 5 4 0
4730 * +----+-----+-----------+-------+---+------+--------+------+------+
4731 * | sf | opc | 0 1 0 1 0 | shift | N | Rm | imm6 | Rn | Rd |
4732 * +----+-----+-----------+-------+---+------+--------+------+------+
4734 static void disas_logic_reg(DisasContext *s, uint32_t insn)
4736 TCGv_i64 tcg_rd, tcg_rn, tcg_rm;
4737 unsigned int sf, opc, shift_type, invert, rm, shift_amount, rn, rd;
4739 sf = extract32(insn, 31, 1);
4740 opc = extract32(insn, 29, 2);
4741 shift_type = extract32(insn, 22, 2);
4742 invert = extract32(insn, 21, 1);
4743 rm = extract32(insn, 16, 5);
4744 shift_amount = extract32(insn, 10, 6);
4745 rn = extract32(insn, 5, 5);
4746 rd = extract32(insn, 0, 5);
4748 if (!sf && (shift_amount & (1 << 5))) {
4749 unallocated_encoding(s);
4750 return;
4753 tcg_rd = cpu_reg(s, rd);
4755 if (opc == 1 && shift_amount == 0 && shift_type == 0 && rn == 31) {
4756 /* Unshifted ORR and ORN with WZR/XZR is the standard encoding for
4757 * register-register MOV and MVN, so it is worth special casing.
4759 tcg_rm = cpu_reg(s, rm);
4760 if (invert) {
4761 tcg_gen_not_i64(tcg_rd, tcg_rm);
4762 if (!sf) {
4763 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4765 } else {
4766 if (sf) {
4767 tcg_gen_mov_i64(tcg_rd, tcg_rm);
4768 } else {
4769 tcg_gen_ext32u_i64(tcg_rd, tcg_rm);
4772 return;
4775 tcg_rm = read_cpu_reg(s, rm, sf);
4777 if (shift_amount) {
4778 shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, shift_amount);
4781 tcg_rn = cpu_reg(s, rn);
4783 switch (opc | (invert << 2)) {
4784 case 0: /* AND */
4785 case 3: /* ANDS */
4786 tcg_gen_and_i64(tcg_rd, tcg_rn, tcg_rm);
4787 break;
4788 case 1: /* ORR */
4789 tcg_gen_or_i64(tcg_rd, tcg_rn, tcg_rm);
4790 break;
4791 case 2: /* EOR */
4792 tcg_gen_xor_i64(tcg_rd, tcg_rn, tcg_rm);
4793 break;
4794 case 4: /* BIC */
4795 case 7: /* BICS */
4796 tcg_gen_andc_i64(tcg_rd, tcg_rn, tcg_rm);
4797 break;
4798 case 5: /* ORN */
4799 tcg_gen_orc_i64(tcg_rd, tcg_rn, tcg_rm);
4800 break;
4801 case 6: /* EON */
4802 tcg_gen_eqv_i64(tcg_rd, tcg_rn, tcg_rm);
4803 break;
4804 default:
4805 assert(FALSE);
4806 break;
4809 if (!sf) {
4810 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4813 if (opc == 3) {
4814 gen_logic_CC(sf, tcg_rd);
4819 * Add/subtract (extended register)
4821 * 31|30|29|28 24|23 22|21|20 16|15 13|12 10|9 5|4 0|
4822 * +--+--+--+-----------+-----+--+-------+------+------+----+----+
4823 * |sf|op| S| 0 1 0 1 1 | opt | 1| Rm |option| imm3 | Rn | Rd |
4824 * +--+--+--+-----------+-----+--+-------+------+------+----+----+
4826 * sf: 0 -> 32bit, 1 -> 64bit
4827 * op: 0 -> add , 1 -> sub
4828 * S: 1 -> set flags
4829 * opt: 00
4830 * option: extension type (see DecodeRegExtend)
4831 * imm3: optional shift to Rm
4833 * Rd = Rn + LSL(extend(Rm), amount)
4835 static void disas_add_sub_ext_reg(DisasContext *s, uint32_t insn)
4837 int rd = extract32(insn, 0, 5);
4838 int rn = extract32(insn, 5, 5);
4839 int imm3 = extract32(insn, 10, 3);
4840 int option = extract32(insn, 13, 3);
4841 int rm = extract32(insn, 16, 5);
4842 int opt = extract32(insn, 22, 2);
4843 bool setflags = extract32(insn, 29, 1);
4844 bool sub_op = extract32(insn, 30, 1);
4845 bool sf = extract32(insn, 31, 1);
4847 TCGv_i64 tcg_rm, tcg_rn; /* temps */
4848 TCGv_i64 tcg_rd;
4849 TCGv_i64 tcg_result;
4851 if (imm3 > 4 || opt != 0) {
4852 unallocated_encoding(s);
4853 return;
4856 /* non-flag setting ops may use SP */
4857 if (!setflags) {
4858 tcg_rd = cpu_reg_sp(s, rd);
4859 } else {
4860 tcg_rd = cpu_reg(s, rd);
4862 tcg_rn = read_cpu_reg_sp(s, rn, sf);
4864 tcg_rm = read_cpu_reg(s, rm, sf);
4865 ext_and_shift_reg(tcg_rm, tcg_rm, option, imm3);
4867 tcg_result = tcg_temp_new_i64();
4869 if (!setflags) {
4870 if (sub_op) {
4871 tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
4872 } else {
4873 tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
4875 } else {
4876 if (sub_op) {
4877 gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
4878 } else {
4879 gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
4883 if (sf) {
4884 tcg_gen_mov_i64(tcg_rd, tcg_result);
4885 } else {
4886 tcg_gen_ext32u_i64(tcg_rd, tcg_result);
4889 tcg_temp_free_i64(tcg_result);
4893 * Add/subtract (shifted register)
4895 * 31 30 29 28 24 23 22 21 20 16 15 10 9 5 4 0
4896 * +--+--+--+-----------+-----+--+-------+---------+------+------+
4897 * |sf|op| S| 0 1 0 1 1 |shift| 0| Rm | imm6 | Rn | Rd |
4898 * +--+--+--+-----------+-----+--+-------+---------+------+------+
4900 * sf: 0 -> 32bit, 1 -> 64bit
4901 * op: 0 -> add , 1 -> sub
4902 * S: 1 -> set flags
4903 * shift: 00 -> LSL, 01 -> LSR, 10 -> ASR, 11 -> RESERVED
4904 * imm6: Shift amount to apply to Rm before the add/sub
4906 static void disas_add_sub_reg(DisasContext *s, uint32_t insn)
4908 int rd = extract32(insn, 0, 5);
4909 int rn = extract32(insn, 5, 5);
4910 int imm6 = extract32(insn, 10, 6);
4911 int rm = extract32(insn, 16, 5);
4912 int shift_type = extract32(insn, 22, 2);
4913 bool setflags = extract32(insn, 29, 1);
4914 bool sub_op = extract32(insn, 30, 1);
4915 bool sf = extract32(insn, 31, 1);
4917 TCGv_i64 tcg_rd = cpu_reg(s, rd);
4918 TCGv_i64 tcg_rn, tcg_rm;
4919 TCGv_i64 tcg_result;
4921 if ((shift_type == 3) || (!sf && (imm6 > 31))) {
4922 unallocated_encoding(s);
4923 return;
4926 tcg_rn = read_cpu_reg(s, rn, sf);
4927 tcg_rm = read_cpu_reg(s, rm, sf);
4929 shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, imm6);
4931 tcg_result = tcg_temp_new_i64();
4933 if (!setflags) {
4934 if (sub_op) {
4935 tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
4936 } else {
4937 tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
4939 } else {
4940 if (sub_op) {
4941 gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
4942 } else {
4943 gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
4947 if (sf) {
4948 tcg_gen_mov_i64(tcg_rd, tcg_result);
4949 } else {
4950 tcg_gen_ext32u_i64(tcg_rd, tcg_result);
4953 tcg_temp_free_i64(tcg_result);
4956 /* Data-processing (3 source)
4958 * 31 30 29 28 24 23 21 20 16 15 14 10 9 5 4 0
4959 * +--+------+-----------+------+------+----+------+------+------+
4960 * |sf| op54 | 1 1 0 1 1 | op31 | Rm | o0 | Ra | Rn | Rd |
4961 * +--+------+-----------+------+------+----+------+------+------+
4963 static void disas_data_proc_3src(DisasContext *s, uint32_t insn)
4965 int rd = extract32(insn, 0, 5);
4966 int rn = extract32(insn, 5, 5);
4967 int ra = extract32(insn, 10, 5);
4968 int rm = extract32(insn, 16, 5);
4969 int op_id = (extract32(insn, 29, 3) << 4) |
4970 (extract32(insn, 21, 3) << 1) |
4971 extract32(insn, 15, 1);
4972 bool sf = extract32(insn, 31, 1);
4973 bool is_sub = extract32(op_id, 0, 1);
4974 bool is_high = extract32(op_id, 2, 1);
4975 bool is_signed = false;
4976 TCGv_i64 tcg_op1;
4977 TCGv_i64 tcg_op2;
4978 TCGv_i64 tcg_tmp;
4980 /* Note that op_id is sf:op54:op31:o0 so it includes the 32/64 size flag */
4981 switch (op_id) {
4982 case 0x42: /* SMADDL */
4983 case 0x43: /* SMSUBL */
4984 case 0x44: /* SMULH */
4985 is_signed = true;
4986 break;
4987 case 0x0: /* MADD (32bit) */
4988 case 0x1: /* MSUB (32bit) */
4989 case 0x40: /* MADD (64bit) */
4990 case 0x41: /* MSUB (64bit) */
4991 case 0x4a: /* UMADDL */
4992 case 0x4b: /* UMSUBL */
4993 case 0x4c: /* UMULH */
4994 break;
4995 default:
4996 unallocated_encoding(s);
4997 return;
5000 if (is_high) {
5001 TCGv_i64 low_bits = tcg_temp_new_i64(); /* low bits discarded */
5002 TCGv_i64 tcg_rd = cpu_reg(s, rd);
5003 TCGv_i64 tcg_rn = cpu_reg(s, rn);
5004 TCGv_i64 tcg_rm = cpu_reg(s, rm);
5006 if (is_signed) {
5007 tcg_gen_muls2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
5008 } else {
5009 tcg_gen_mulu2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
5012 tcg_temp_free_i64(low_bits);
5013 return;
5016 tcg_op1 = tcg_temp_new_i64();
5017 tcg_op2 = tcg_temp_new_i64();
5018 tcg_tmp = tcg_temp_new_i64();
5020 if (op_id < 0x42) {
5021 tcg_gen_mov_i64(tcg_op1, cpu_reg(s, rn));
5022 tcg_gen_mov_i64(tcg_op2, cpu_reg(s, rm));
5023 } else {
5024 if (is_signed) {
5025 tcg_gen_ext32s_i64(tcg_op1, cpu_reg(s, rn));
5026 tcg_gen_ext32s_i64(tcg_op2, cpu_reg(s, rm));
5027 } else {
5028 tcg_gen_ext32u_i64(tcg_op1, cpu_reg(s, rn));
5029 tcg_gen_ext32u_i64(tcg_op2, cpu_reg(s, rm));
5033 if (ra == 31 && !is_sub) {
5034 /* Special-case MADD with rA == XZR; it is the standard MUL alias */
5035 tcg_gen_mul_i64(cpu_reg(s, rd), tcg_op1, tcg_op2);
5036 } else {
5037 tcg_gen_mul_i64(tcg_tmp, tcg_op1, tcg_op2);
5038 if (is_sub) {
5039 tcg_gen_sub_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
5040 } else {
5041 tcg_gen_add_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
5045 if (!sf) {
5046 tcg_gen_ext32u_i64(cpu_reg(s, rd), cpu_reg(s, rd));
5049 tcg_temp_free_i64(tcg_op1);
5050 tcg_temp_free_i64(tcg_op2);
5051 tcg_temp_free_i64(tcg_tmp);
5054 /* Add/subtract (with carry)
5055 * 31 30 29 28 27 26 25 24 23 22 21 20 16 15 10 9 5 4 0
5056 * +--+--+--+------------------------+------+-------------+------+-----+
5057 * |sf|op| S| 1 1 0 1 0 0 0 0 | rm | 0 0 0 0 0 0 | Rn | Rd |
5058 * +--+--+--+------------------------+------+-------------+------+-----+
5061 static void disas_adc_sbc(DisasContext *s, uint32_t insn)
5063 unsigned int sf, op, setflags, rm, rn, rd;
5064 TCGv_i64 tcg_y, tcg_rn, tcg_rd;
5066 sf = extract32(insn, 31, 1);
5067 op = extract32(insn, 30, 1);
5068 setflags = extract32(insn, 29, 1);
5069 rm = extract32(insn, 16, 5);
5070 rn = extract32(insn, 5, 5);
5071 rd = extract32(insn, 0, 5);
5073 tcg_rd = cpu_reg(s, rd);
5074 tcg_rn = cpu_reg(s, rn);
5076 if (op) {
5077 tcg_y = new_tmp_a64(s);
5078 tcg_gen_not_i64(tcg_y, cpu_reg(s, rm));
5079 } else {
5080 tcg_y = cpu_reg(s, rm);
5083 if (setflags) {
5084 gen_adc_CC(sf, tcg_rd, tcg_rn, tcg_y);
5085 } else {
5086 gen_adc(sf, tcg_rd, tcg_rn, tcg_y);
5091 * Rotate right into flags
5092 * 31 30 29 21 15 10 5 4 0
5093 * +--+--+--+-----------------+--------+-----------+------+--+------+
5094 * |sf|op| S| 1 1 0 1 0 0 0 0 | imm6 | 0 0 0 0 1 | Rn |o2| mask |
5095 * +--+--+--+-----------------+--------+-----------+------+--+------+
5097 static void disas_rotate_right_into_flags(DisasContext *s, uint32_t insn)
5099 int mask = extract32(insn, 0, 4);
5100 int o2 = extract32(insn, 4, 1);
5101 int rn = extract32(insn, 5, 5);
5102 int imm6 = extract32(insn, 15, 6);
5103 int sf_op_s = extract32(insn, 29, 3);
5104 TCGv_i64 tcg_rn;
5105 TCGv_i32 nzcv;
5107 if (sf_op_s != 5 || o2 != 0 || !dc_isar_feature(aa64_condm_4, s)) {
5108 unallocated_encoding(s);
5109 return;
5112 tcg_rn = read_cpu_reg(s, rn, 1);
5113 tcg_gen_rotri_i64(tcg_rn, tcg_rn, imm6);
5115 nzcv = tcg_temp_new_i32();
5116 tcg_gen_extrl_i64_i32(nzcv, tcg_rn);
5118 if (mask & 8) { /* N */
5119 tcg_gen_shli_i32(cpu_NF, nzcv, 31 - 3);
5121 if (mask & 4) { /* Z */
5122 tcg_gen_not_i32(cpu_ZF, nzcv);
5123 tcg_gen_andi_i32(cpu_ZF, cpu_ZF, 4);
5125 if (mask & 2) { /* C */
5126 tcg_gen_extract_i32(cpu_CF, nzcv, 1, 1);
5128 if (mask & 1) { /* V */
5129 tcg_gen_shli_i32(cpu_VF, nzcv, 31 - 0);
5132 tcg_temp_free_i32(nzcv);
5136 * Evaluate into flags
5137 * 31 30 29 21 15 14 10 5 4 0
5138 * +--+--+--+-----------------+---------+----+---------+------+--+------+
5139 * |sf|op| S| 1 1 0 1 0 0 0 0 | opcode2 | sz | 0 0 1 0 | Rn |o3| mask |
5140 * +--+--+--+-----------------+---------+----+---------+------+--+------+
5142 static void disas_evaluate_into_flags(DisasContext *s, uint32_t insn)
5144 int o3_mask = extract32(insn, 0, 5);
5145 int rn = extract32(insn, 5, 5);
5146 int o2 = extract32(insn, 15, 6);
5147 int sz = extract32(insn, 14, 1);
5148 int sf_op_s = extract32(insn, 29, 3);
5149 TCGv_i32 tmp;
5150 int shift;
5152 if (sf_op_s != 1 || o2 != 0 || o3_mask != 0xd ||
5153 !dc_isar_feature(aa64_condm_4, s)) {
5154 unallocated_encoding(s);
5155 return;
5157 shift = sz ? 16 : 24; /* SETF16 or SETF8 */
5159 tmp = tcg_temp_new_i32();
5160 tcg_gen_extrl_i64_i32(tmp, cpu_reg(s, rn));
5161 tcg_gen_shli_i32(cpu_NF, tmp, shift);
5162 tcg_gen_shli_i32(cpu_VF, tmp, shift - 1);
5163 tcg_gen_mov_i32(cpu_ZF, cpu_NF);
5164 tcg_gen_xor_i32(cpu_VF, cpu_VF, cpu_NF);
5165 tcg_temp_free_i32(tmp);
5168 /* Conditional compare (immediate / register)
5169 * 31 30 29 28 27 26 25 24 23 22 21 20 16 15 12 11 10 9 5 4 3 0
5170 * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
5171 * |sf|op| S| 1 1 0 1 0 0 1 0 |imm5/rm | cond |i/r |o2| Rn |o3|nzcv |
5172 * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
5173 * [1] y [0] [0]
5175 static void disas_cc(DisasContext *s, uint32_t insn)
5177 unsigned int sf, op, y, cond, rn, nzcv, is_imm;
5178 TCGv_i32 tcg_t0, tcg_t1, tcg_t2;
5179 TCGv_i64 tcg_tmp, tcg_y, tcg_rn;
5180 DisasCompare c;
5182 if (!extract32(insn, 29, 1)) {
5183 unallocated_encoding(s);
5184 return;
5186 if (insn & (1 << 10 | 1 << 4)) {
5187 unallocated_encoding(s);
5188 return;
5190 sf = extract32(insn, 31, 1);
5191 op = extract32(insn, 30, 1);
5192 is_imm = extract32(insn, 11, 1);
5193 y = extract32(insn, 16, 5); /* y = rm (reg) or imm5 (imm) */
5194 cond = extract32(insn, 12, 4);
5195 rn = extract32(insn, 5, 5);
5196 nzcv = extract32(insn, 0, 4);
5198 /* Set T0 = !COND. */
5199 tcg_t0 = tcg_temp_new_i32();
5200 arm_test_cc(&c, cond);
5201 tcg_gen_setcondi_i32(tcg_invert_cond(c.cond), tcg_t0, c.value, 0);
5202 arm_free_cc(&c);
5204 /* Load the arguments for the new comparison. */
5205 if (is_imm) {
5206 tcg_y = new_tmp_a64(s);
5207 tcg_gen_movi_i64(tcg_y, y);
5208 } else {
5209 tcg_y = cpu_reg(s, y);
5211 tcg_rn = cpu_reg(s, rn);
5213 /* Set the flags for the new comparison. */
5214 tcg_tmp = tcg_temp_new_i64();
5215 if (op) {
5216 gen_sub_CC(sf, tcg_tmp, tcg_rn, tcg_y);
5217 } else {
5218 gen_add_CC(sf, tcg_tmp, tcg_rn, tcg_y);
5220 tcg_temp_free_i64(tcg_tmp);
5222 /* If COND was false, force the flags to #nzcv. Compute two masks
5223 * to help with this: T1 = (COND ? 0 : -1), T2 = (COND ? -1 : 0).
5224 * For tcg hosts that support ANDC, we can make do with just T1.
5225 * In either case, allow the tcg optimizer to delete any unused mask.
5227 tcg_t1 = tcg_temp_new_i32();
5228 tcg_t2 = tcg_temp_new_i32();
5229 tcg_gen_neg_i32(tcg_t1, tcg_t0);
5230 tcg_gen_subi_i32(tcg_t2, tcg_t0, 1);
5232 if (nzcv & 8) { /* N */
5233 tcg_gen_or_i32(cpu_NF, cpu_NF, tcg_t1);
5234 } else {
5235 if (TCG_TARGET_HAS_andc_i32) {
5236 tcg_gen_andc_i32(cpu_NF, cpu_NF, tcg_t1);
5237 } else {
5238 tcg_gen_and_i32(cpu_NF, cpu_NF, tcg_t2);
5241 if (nzcv & 4) { /* Z */
5242 if (TCG_TARGET_HAS_andc_i32) {
5243 tcg_gen_andc_i32(cpu_ZF, cpu_ZF, tcg_t1);
5244 } else {
5245 tcg_gen_and_i32(cpu_ZF, cpu_ZF, tcg_t2);
5247 } else {
5248 tcg_gen_or_i32(cpu_ZF, cpu_ZF, tcg_t0);
5250 if (nzcv & 2) { /* C */
5251 tcg_gen_or_i32(cpu_CF, cpu_CF, tcg_t0);
5252 } else {
5253 if (TCG_TARGET_HAS_andc_i32) {
5254 tcg_gen_andc_i32(cpu_CF, cpu_CF, tcg_t1);
5255 } else {
5256 tcg_gen_and_i32(cpu_CF, cpu_CF, tcg_t2);
5259 if (nzcv & 1) { /* V */
5260 tcg_gen_or_i32(cpu_VF, cpu_VF, tcg_t1);
5261 } else {
5262 if (TCG_TARGET_HAS_andc_i32) {
5263 tcg_gen_andc_i32(cpu_VF, cpu_VF, tcg_t1);
5264 } else {
5265 tcg_gen_and_i32(cpu_VF, cpu_VF, tcg_t2);
5268 tcg_temp_free_i32(tcg_t0);
5269 tcg_temp_free_i32(tcg_t1);
5270 tcg_temp_free_i32(tcg_t2);
5273 /* Conditional select
5274 * 31 30 29 28 21 20 16 15 12 11 10 9 5 4 0
5275 * +----+----+---+-----------------+------+------+-----+------+------+
5276 * | sf | op | S | 1 1 0 1 0 1 0 0 | Rm | cond | op2 | Rn | Rd |
5277 * +----+----+---+-----------------+------+------+-----+------+------+
5279 static void disas_cond_select(DisasContext *s, uint32_t insn)
5281 unsigned int sf, else_inv, rm, cond, else_inc, rn, rd;
5282 TCGv_i64 tcg_rd, zero;
5283 DisasCompare64 c;
5285 if (extract32(insn, 29, 1) || extract32(insn, 11, 1)) {
5286 /* S == 1 or op2<1> == 1 */
5287 unallocated_encoding(s);
5288 return;
5290 sf = extract32(insn, 31, 1);
5291 else_inv = extract32(insn, 30, 1);
5292 rm = extract32(insn, 16, 5);
5293 cond = extract32(insn, 12, 4);
5294 else_inc = extract32(insn, 10, 1);
5295 rn = extract32(insn, 5, 5);
5296 rd = extract32(insn, 0, 5);
5298 tcg_rd = cpu_reg(s, rd);
5300 a64_test_cc(&c, cond);
5301 zero = tcg_constant_i64(0);
5303 if (rn == 31 && rm == 31 && (else_inc ^ else_inv)) {
5304 /* CSET & CSETM. */
5305 tcg_gen_setcond_i64(tcg_invert_cond(c.cond), tcg_rd, c.value, zero);
5306 if (else_inv) {
5307 tcg_gen_neg_i64(tcg_rd, tcg_rd);
5309 } else {
5310 TCGv_i64 t_true = cpu_reg(s, rn);
5311 TCGv_i64 t_false = read_cpu_reg(s, rm, 1);
5312 if (else_inv && else_inc) {
5313 tcg_gen_neg_i64(t_false, t_false);
5314 } else if (else_inv) {
5315 tcg_gen_not_i64(t_false, t_false);
5316 } else if (else_inc) {
5317 tcg_gen_addi_i64(t_false, t_false, 1);
5319 tcg_gen_movcond_i64(c.cond, tcg_rd, c.value, zero, t_true, t_false);
5322 a64_free_cc(&c);
5324 if (!sf) {
5325 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
5329 static void handle_clz(DisasContext *s, unsigned int sf,
5330 unsigned int rn, unsigned int rd)
5332 TCGv_i64 tcg_rd, tcg_rn;
5333 tcg_rd = cpu_reg(s, rd);
5334 tcg_rn = cpu_reg(s, rn);
5336 if (sf) {
5337 tcg_gen_clzi_i64(tcg_rd, tcg_rn, 64);
5338 } else {
5339 TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
5340 tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
5341 tcg_gen_clzi_i32(tcg_tmp32, tcg_tmp32, 32);
5342 tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
5343 tcg_temp_free_i32(tcg_tmp32);
5347 static void handle_cls(DisasContext *s, unsigned int sf,
5348 unsigned int rn, unsigned int rd)
5350 TCGv_i64 tcg_rd, tcg_rn;
5351 tcg_rd = cpu_reg(s, rd);
5352 tcg_rn = cpu_reg(s, rn);
5354 if (sf) {
5355 tcg_gen_clrsb_i64(tcg_rd, tcg_rn);
5356 } else {
5357 TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
5358 tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
5359 tcg_gen_clrsb_i32(tcg_tmp32, tcg_tmp32);
5360 tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
5361 tcg_temp_free_i32(tcg_tmp32);
5365 static void handle_rbit(DisasContext *s, unsigned int sf,
5366 unsigned int rn, unsigned int rd)
5368 TCGv_i64 tcg_rd, tcg_rn;
5369 tcg_rd = cpu_reg(s, rd);
5370 tcg_rn = cpu_reg(s, rn);
5372 if (sf) {
5373 gen_helper_rbit64(tcg_rd, tcg_rn);
5374 } else {
5375 TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
5376 tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
5377 gen_helper_rbit(tcg_tmp32, tcg_tmp32);
5378 tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
5379 tcg_temp_free_i32(tcg_tmp32);
5383 /* REV with sf==1, opcode==3 ("REV64") */
5384 static void handle_rev64(DisasContext *s, unsigned int sf,
5385 unsigned int rn, unsigned int rd)
5387 if (!sf) {
5388 unallocated_encoding(s);
5389 return;
5391 tcg_gen_bswap64_i64(cpu_reg(s, rd), cpu_reg(s, rn));
5394 /* REV with sf==0, opcode==2
5395 * REV32 (sf==1, opcode==2)
5397 static void handle_rev32(DisasContext *s, unsigned int sf,
5398 unsigned int rn, unsigned int rd)
5400 TCGv_i64 tcg_rd = cpu_reg(s, rd);
5401 TCGv_i64 tcg_rn = cpu_reg(s, rn);
5403 if (sf) {
5404 tcg_gen_bswap64_i64(tcg_rd, tcg_rn);
5405 tcg_gen_rotri_i64(tcg_rd, tcg_rd, 32);
5406 } else {
5407 tcg_gen_bswap32_i64(tcg_rd, tcg_rn, TCG_BSWAP_OZ);
5411 /* REV16 (opcode==1) */
5412 static void handle_rev16(DisasContext *s, unsigned int sf,
5413 unsigned int rn, unsigned int rd)
5415 TCGv_i64 tcg_rd = cpu_reg(s, rd);
5416 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
5417 TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
5418 TCGv_i64 mask = tcg_constant_i64(sf ? 0x00ff00ff00ff00ffull : 0x00ff00ff);
5420 tcg_gen_shri_i64(tcg_tmp, tcg_rn, 8);
5421 tcg_gen_and_i64(tcg_rd, tcg_rn, mask);
5422 tcg_gen_and_i64(tcg_tmp, tcg_tmp, mask);
5423 tcg_gen_shli_i64(tcg_rd, tcg_rd, 8);
5424 tcg_gen_or_i64(tcg_rd, tcg_rd, tcg_tmp);
5426 tcg_temp_free_i64(tcg_tmp);
5429 /* Data-processing (1 source)
5430 * 31 30 29 28 21 20 16 15 10 9 5 4 0
5431 * +----+---+---+-----------------+---------+--------+------+------+
5432 * | sf | 1 | S | 1 1 0 1 0 1 1 0 | opcode2 | opcode | Rn | Rd |
5433 * +----+---+---+-----------------+---------+--------+------+------+
5435 static void disas_data_proc_1src(DisasContext *s, uint32_t insn)
5437 unsigned int sf, opcode, opcode2, rn, rd;
5438 TCGv_i64 tcg_rd;
5440 if (extract32(insn, 29, 1)) {
5441 unallocated_encoding(s);
5442 return;
5445 sf = extract32(insn, 31, 1);
5446 opcode = extract32(insn, 10, 6);
5447 opcode2 = extract32(insn, 16, 5);
5448 rn = extract32(insn, 5, 5);
5449 rd = extract32(insn, 0, 5);
5451 #define MAP(SF, O2, O1) ((SF) | (O1 << 1) | (O2 << 7))
5453 switch (MAP(sf, opcode2, opcode)) {
5454 case MAP(0, 0x00, 0x00): /* RBIT */
5455 case MAP(1, 0x00, 0x00):
5456 handle_rbit(s, sf, rn, rd);
5457 break;
5458 case MAP(0, 0x00, 0x01): /* REV16 */
5459 case MAP(1, 0x00, 0x01):
5460 handle_rev16(s, sf, rn, rd);
5461 break;
5462 case MAP(0, 0x00, 0x02): /* REV/REV32 */
5463 case MAP(1, 0x00, 0x02):
5464 handle_rev32(s, sf, rn, rd);
5465 break;
5466 case MAP(1, 0x00, 0x03): /* REV64 */
5467 handle_rev64(s, sf, rn, rd);
5468 break;
5469 case MAP(0, 0x00, 0x04): /* CLZ */
5470 case MAP(1, 0x00, 0x04):
5471 handle_clz(s, sf, rn, rd);
5472 break;
5473 case MAP(0, 0x00, 0x05): /* CLS */
5474 case MAP(1, 0x00, 0x05):
5475 handle_cls(s, sf, rn, rd);
5476 break;
5477 case MAP(1, 0x01, 0x00): /* PACIA */
5478 if (s->pauth_active) {
5479 tcg_rd = cpu_reg(s, rd);
5480 gen_helper_pacia(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5481 } else if (!dc_isar_feature(aa64_pauth, s)) {
5482 goto do_unallocated;
5484 break;
5485 case MAP(1, 0x01, 0x01): /* PACIB */
5486 if (s->pauth_active) {
5487 tcg_rd = cpu_reg(s, rd);
5488 gen_helper_pacib(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5489 } else if (!dc_isar_feature(aa64_pauth, s)) {
5490 goto do_unallocated;
5492 break;
5493 case MAP(1, 0x01, 0x02): /* PACDA */
5494 if (s->pauth_active) {
5495 tcg_rd = cpu_reg(s, rd);
5496 gen_helper_pacda(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5497 } else if (!dc_isar_feature(aa64_pauth, s)) {
5498 goto do_unallocated;
5500 break;
5501 case MAP(1, 0x01, 0x03): /* PACDB */
5502 if (s->pauth_active) {
5503 tcg_rd = cpu_reg(s, rd);
5504 gen_helper_pacdb(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5505 } else if (!dc_isar_feature(aa64_pauth, s)) {
5506 goto do_unallocated;
5508 break;
5509 case MAP(1, 0x01, 0x04): /* AUTIA */
5510 if (s->pauth_active) {
5511 tcg_rd = cpu_reg(s, rd);
5512 gen_helper_autia(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5513 } else if (!dc_isar_feature(aa64_pauth, s)) {
5514 goto do_unallocated;
5516 break;
5517 case MAP(1, 0x01, 0x05): /* AUTIB */
5518 if (s->pauth_active) {
5519 tcg_rd = cpu_reg(s, rd);
5520 gen_helper_autib(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5521 } else if (!dc_isar_feature(aa64_pauth, s)) {
5522 goto do_unallocated;
5524 break;
5525 case MAP(1, 0x01, 0x06): /* AUTDA */
5526 if (s->pauth_active) {
5527 tcg_rd = cpu_reg(s, rd);
5528 gen_helper_autda(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5529 } else if (!dc_isar_feature(aa64_pauth, s)) {
5530 goto do_unallocated;
5532 break;
5533 case MAP(1, 0x01, 0x07): /* AUTDB */
5534 if (s->pauth_active) {
5535 tcg_rd = cpu_reg(s, rd);
5536 gen_helper_autdb(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5537 } else if (!dc_isar_feature(aa64_pauth, s)) {
5538 goto do_unallocated;
5540 break;
5541 case MAP(1, 0x01, 0x08): /* PACIZA */
5542 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5543 goto do_unallocated;
5544 } else if (s->pauth_active) {
5545 tcg_rd = cpu_reg(s, rd);
5546 gen_helper_pacia(tcg_rd, cpu_env, tcg_rd, new_tmp_a64_zero(s));
5548 break;
5549 case MAP(1, 0x01, 0x09): /* PACIZB */
5550 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5551 goto do_unallocated;
5552 } else if (s->pauth_active) {
5553 tcg_rd = cpu_reg(s, rd);
5554 gen_helper_pacib(tcg_rd, cpu_env, tcg_rd, new_tmp_a64_zero(s));
5556 break;
5557 case MAP(1, 0x01, 0x0a): /* PACDZA */
5558 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5559 goto do_unallocated;
5560 } else if (s->pauth_active) {
5561 tcg_rd = cpu_reg(s, rd);
5562 gen_helper_pacda(tcg_rd, cpu_env, tcg_rd, new_tmp_a64_zero(s));
5564 break;
5565 case MAP(1, 0x01, 0x0b): /* PACDZB */
5566 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5567 goto do_unallocated;
5568 } else if (s->pauth_active) {
5569 tcg_rd = cpu_reg(s, rd);
5570 gen_helper_pacdb(tcg_rd, cpu_env, tcg_rd, new_tmp_a64_zero(s));
5572 break;
5573 case MAP(1, 0x01, 0x0c): /* AUTIZA */
5574 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5575 goto do_unallocated;
5576 } else if (s->pauth_active) {
5577 tcg_rd = cpu_reg(s, rd);
5578 gen_helper_autia(tcg_rd, cpu_env, tcg_rd, new_tmp_a64_zero(s));
5580 break;
5581 case MAP(1, 0x01, 0x0d): /* AUTIZB */
5582 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5583 goto do_unallocated;
5584 } else if (s->pauth_active) {
5585 tcg_rd = cpu_reg(s, rd);
5586 gen_helper_autib(tcg_rd, cpu_env, tcg_rd, new_tmp_a64_zero(s));
5588 break;
5589 case MAP(1, 0x01, 0x0e): /* AUTDZA */
5590 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5591 goto do_unallocated;
5592 } else if (s->pauth_active) {
5593 tcg_rd = cpu_reg(s, rd);
5594 gen_helper_autda(tcg_rd, cpu_env, tcg_rd, new_tmp_a64_zero(s));
5596 break;
5597 case MAP(1, 0x01, 0x0f): /* AUTDZB */
5598 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5599 goto do_unallocated;
5600 } else if (s->pauth_active) {
5601 tcg_rd = cpu_reg(s, rd);
5602 gen_helper_autdb(tcg_rd, cpu_env, tcg_rd, new_tmp_a64_zero(s));
5604 break;
5605 case MAP(1, 0x01, 0x10): /* XPACI */
5606 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5607 goto do_unallocated;
5608 } else if (s->pauth_active) {
5609 tcg_rd = cpu_reg(s, rd);
5610 gen_helper_xpaci(tcg_rd, cpu_env, tcg_rd);
5612 break;
5613 case MAP(1, 0x01, 0x11): /* XPACD */
5614 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5615 goto do_unallocated;
5616 } else if (s->pauth_active) {
5617 tcg_rd = cpu_reg(s, rd);
5618 gen_helper_xpacd(tcg_rd, cpu_env, tcg_rd);
5620 break;
5621 default:
5622 do_unallocated:
5623 unallocated_encoding(s);
5624 break;
5627 #undef MAP
5630 static void handle_div(DisasContext *s, bool is_signed, unsigned int sf,
5631 unsigned int rm, unsigned int rn, unsigned int rd)
5633 TCGv_i64 tcg_n, tcg_m, tcg_rd;
5634 tcg_rd = cpu_reg(s, rd);
5636 if (!sf && is_signed) {
5637 tcg_n = new_tmp_a64(s);
5638 tcg_m = new_tmp_a64(s);
5639 tcg_gen_ext32s_i64(tcg_n, cpu_reg(s, rn));
5640 tcg_gen_ext32s_i64(tcg_m, cpu_reg(s, rm));
5641 } else {
5642 tcg_n = read_cpu_reg(s, rn, sf);
5643 tcg_m = read_cpu_reg(s, rm, sf);
5646 if (is_signed) {
5647 gen_helper_sdiv64(tcg_rd, tcg_n, tcg_m);
5648 } else {
5649 gen_helper_udiv64(tcg_rd, tcg_n, tcg_m);
5652 if (!sf) { /* zero extend final result */
5653 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
5657 /* LSLV, LSRV, ASRV, RORV */
5658 static void handle_shift_reg(DisasContext *s,
5659 enum a64_shift_type shift_type, unsigned int sf,
5660 unsigned int rm, unsigned int rn, unsigned int rd)
5662 TCGv_i64 tcg_shift = tcg_temp_new_i64();
5663 TCGv_i64 tcg_rd = cpu_reg(s, rd);
5664 TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
5666 tcg_gen_andi_i64(tcg_shift, cpu_reg(s, rm), sf ? 63 : 31);
5667 shift_reg(tcg_rd, tcg_rn, sf, shift_type, tcg_shift);
5668 tcg_temp_free_i64(tcg_shift);
5671 /* CRC32[BHWX], CRC32C[BHWX] */
5672 static void handle_crc32(DisasContext *s,
5673 unsigned int sf, unsigned int sz, bool crc32c,
5674 unsigned int rm, unsigned int rn, unsigned int rd)
5676 TCGv_i64 tcg_acc, tcg_val;
5677 TCGv_i32 tcg_bytes;
5679 if (!dc_isar_feature(aa64_crc32, s)
5680 || (sf == 1 && sz != 3)
5681 || (sf == 0 && sz == 3)) {
5682 unallocated_encoding(s);
5683 return;
5686 if (sz == 3) {
5687 tcg_val = cpu_reg(s, rm);
5688 } else {
5689 uint64_t mask;
5690 switch (sz) {
5691 case 0:
5692 mask = 0xFF;
5693 break;
5694 case 1:
5695 mask = 0xFFFF;
5696 break;
5697 case 2:
5698 mask = 0xFFFFFFFF;
5699 break;
5700 default:
5701 g_assert_not_reached();
5703 tcg_val = new_tmp_a64(s);
5704 tcg_gen_andi_i64(tcg_val, cpu_reg(s, rm), mask);
5707 tcg_acc = cpu_reg(s, rn);
5708 tcg_bytes = tcg_constant_i32(1 << sz);
5710 if (crc32c) {
5711 gen_helper_crc32c_64(cpu_reg(s, rd), tcg_acc, tcg_val, tcg_bytes);
5712 } else {
5713 gen_helper_crc32_64(cpu_reg(s, rd), tcg_acc, tcg_val, tcg_bytes);
5717 /* Data-processing (2 source)
5718 * 31 30 29 28 21 20 16 15 10 9 5 4 0
5719 * +----+---+---+-----------------+------+--------+------+------+
5720 * | sf | 0 | S | 1 1 0 1 0 1 1 0 | Rm | opcode | Rn | Rd |
5721 * +----+---+---+-----------------+------+--------+------+------+
5723 static void disas_data_proc_2src(DisasContext *s, uint32_t insn)
5725 unsigned int sf, rm, opcode, rn, rd, setflag;
5726 sf = extract32(insn, 31, 1);
5727 setflag = extract32(insn, 29, 1);
5728 rm = extract32(insn, 16, 5);
5729 opcode = extract32(insn, 10, 6);
5730 rn = extract32(insn, 5, 5);
5731 rd = extract32(insn, 0, 5);
5733 if (setflag && opcode != 0) {
5734 unallocated_encoding(s);
5735 return;
5738 switch (opcode) {
5739 case 0: /* SUBP(S) */
5740 if (sf == 0 || !dc_isar_feature(aa64_mte_insn_reg, s)) {
5741 goto do_unallocated;
5742 } else {
5743 TCGv_i64 tcg_n, tcg_m, tcg_d;
5745 tcg_n = read_cpu_reg_sp(s, rn, true);
5746 tcg_m = read_cpu_reg_sp(s, rm, true);
5747 tcg_gen_sextract_i64(tcg_n, tcg_n, 0, 56);
5748 tcg_gen_sextract_i64(tcg_m, tcg_m, 0, 56);
5749 tcg_d = cpu_reg(s, rd);
5751 if (setflag) {
5752 gen_sub_CC(true, tcg_d, tcg_n, tcg_m);
5753 } else {
5754 tcg_gen_sub_i64(tcg_d, tcg_n, tcg_m);
5757 break;
5758 case 2: /* UDIV */
5759 handle_div(s, false, sf, rm, rn, rd);
5760 break;
5761 case 3: /* SDIV */
5762 handle_div(s, true, sf, rm, rn, rd);
5763 break;
5764 case 4: /* IRG */
5765 if (sf == 0 || !dc_isar_feature(aa64_mte_insn_reg, s)) {
5766 goto do_unallocated;
5768 if (s->ata) {
5769 gen_helper_irg(cpu_reg_sp(s, rd), cpu_env,
5770 cpu_reg_sp(s, rn), cpu_reg(s, rm));
5771 } else {
5772 gen_address_with_allocation_tag0(cpu_reg_sp(s, rd),
5773 cpu_reg_sp(s, rn));
5775 break;
5776 case 5: /* GMI */
5777 if (sf == 0 || !dc_isar_feature(aa64_mte_insn_reg, s)) {
5778 goto do_unallocated;
5779 } else {
5780 TCGv_i64 t = tcg_temp_new_i64();
5782 tcg_gen_extract_i64(t, cpu_reg_sp(s, rn), 56, 4);
5783 tcg_gen_shl_i64(t, tcg_constant_i64(1), t);
5784 tcg_gen_or_i64(cpu_reg(s, rd), cpu_reg(s, rm), t);
5786 tcg_temp_free_i64(t);
5788 break;
5789 case 8: /* LSLV */
5790 handle_shift_reg(s, A64_SHIFT_TYPE_LSL, sf, rm, rn, rd);
5791 break;
5792 case 9: /* LSRV */
5793 handle_shift_reg(s, A64_SHIFT_TYPE_LSR, sf, rm, rn, rd);
5794 break;
5795 case 10: /* ASRV */
5796 handle_shift_reg(s, A64_SHIFT_TYPE_ASR, sf, rm, rn, rd);
5797 break;
5798 case 11: /* RORV */
5799 handle_shift_reg(s, A64_SHIFT_TYPE_ROR, sf, rm, rn, rd);
5800 break;
5801 case 12: /* PACGA */
5802 if (sf == 0 || !dc_isar_feature(aa64_pauth, s)) {
5803 goto do_unallocated;
5805 gen_helper_pacga(cpu_reg(s, rd), cpu_env,
5806 cpu_reg(s, rn), cpu_reg_sp(s, rm));
5807 break;
5808 case 16:
5809 case 17:
5810 case 18:
5811 case 19:
5812 case 20:
5813 case 21:
5814 case 22:
5815 case 23: /* CRC32 */
5817 int sz = extract32(opcode, 0, 2);
5818 bool crc32c = extract32(opcode, 2, 1);
5819 handle_crc32(s, sf, sz, crc32c, rm, rn, rd);
5820 break;
5822 default:
5823 do_unallocated:
5824 unallocated_encoding(s);
5825 break;
5830 * Data processing - register
5831 * 31 30 29 28 25 21 20 16 10 0
5832 * +--+---+--+---+-------+-----+-------+-------+---------+
5833 * | |op0| |op1| 1 0 1 | op2 | | op3 | |
5834 * +--+---+--+---+-------+-----+-------+-------+---------+
5836 static void disas_data_proc_reg(DisasContext *s, uint32_t insn)
5838 int op0 = extract32(insn, 30, 1);
5839 int op1 = extract32(insn, 28, 1);
5840 int op2 = extract32(insn, 21, 4);
5841 int op3 = extract32(insn, 10, 6);
5843 if (!op1) {
5844 if (op2 & 8) {
5845 if (op2 & 1) {
5846 /* Add/sub (extended register) */
5847 disas_add_sub_ext_reg(s, insn);
5848 } else {
5849 /* Add/sub (shifted register) */
5850 disas_add_sub_reg(s, insn);
5852 } else {
5853 /* Logical (shifted register) */
5854 disas_logic_reg(s, insn);
5856 return;
5859 switch (op2) {
5860 case 0x0:
5861 switch (op3) {
5862 case 0x00: /* Add/subtract (with carry) */
5863 disas_adc_sbc(s, insn);
5864 break;
5866 case 0x01: /* Rotate right into flags */
5867 case 0x21:
5868 disas_rotate_right_into_flags(s, insn);
5869 break;
5871 case 0x02: /* Evaluate into flags */
5872 case 0x12:
5873 case 0x22:
5874 case 0x32:
5875 disas_evaluate_into_flags(s, insn);
5876 break;
5878 default:
5879 goto do_unallocated;
5881 break;
5883 case 0x2: /* Conditional compare */
5884 disas_cc(s, insn); /* both imm and reg forms */
5885 break;
5887 case 0x4: /* Conditional select */
5888 disas_cond_select(s, insn);
5889 break;
5891 case 0x6: /* Data-processing */
5892 if (op0) { /* (1 source) */
5893 disas_data_proc_1src(s, insn);
5894 } else { /* (2 source) */
5895 disas_data_proc_2src(s, insn);
5897 break;
5898 case 0x8 ... 0xf: /* (3 source) */
5899 disas_data_proc_3src(s, insn);
5900 break;
5902 default:
5903 do_unallocated:
5904 unallocated_encoding(s);
5905 break;
5909 static void handle_fp_compare(DisasContext *s, int size,
5910 unsigned int rn, unsigned int rm,
5911 bool cmp_with_zero, bool signal_all_nans)
5913 TCGv_i64 tcg_flags = tcg_temp_new_i64();
5914 TCGv_ptr fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
5916 if (size == MO_64) {
5917 TCGv_i64 tcg_vn, tcg_vm;
5919 tcg_vn = read_fp_dreg(s, rn);
5920 if (cmp_with_zero) {
5921 tcg_vm = tcg_constant_i64(0);
5922 } else {
5923 tcg_vm = read_fp_dreg(s, rm);
5925 if (signal_all_nans) {
5926 gen_helper_vfp_cmped_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5927 } else {
5928 gen_helper_vfp_cmpd_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5930 tcg_temp_free_i64(tcg_vn);
5931 tcg_temp_free_i64(tcg_vm);
5932 } else {
5933 TCGv_i32 tcg_vn = tcg_temp_new_i32();
5934 TCGv_i32 tcg_vm = tcg_temp_new_i32();
5936 read_vec_element_i32(s, tcg_vn, rn, 0, size);
5937 if (cmp_with_zero) {
5938 tcg_gen_movi_i32(tcg_vm, 0);
5939 } else {
5940 read_vec_element_i32(s, tcg_vm, rm, 0, size);
5943 switch (size) {
5944 case MO_32:
5945 if (signal_all_nans) {
5946 gen_helper_vfp_cmpes_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5947 } else {
5948 gen_helper_vfp_cmps_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5950 break;
5951 case MO_16:
5952 if (signal_all_nans) {
5953 gen_helper_vfp_cmpeh_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5954 } else {
5955 gen_helper_vfp_cmph_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5957 break;
5958 default:
5959 g_assert_not_reached();
5962 tcg_temp_free_i32(tcg_vn);
5963 tcg_temp_free_i32(tcg_vm);
5966 tcg_temp_free_ptr(fpst);
5968 gen_set_nzcv(tcg_flags);
5970 tcg_temp_free_i64(tcg_flags);
5973 /* Floating point compare
5974 * 31 30 29 28 24 23 22 21 20 16 15 14 13 10 9 5 4 0
5975 * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
5976 * | M | 0 | S | 1 1 1 1 0 | type | 1 | Rm | op | 1 0 0 0 | Rn | op2 |
5977 * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
5979 static void disas_fp_compare(DisasContext *s, uint32_t insn)
5981 unsigned int mos, type, rm, op, rn, opc, op2r;
5982 int size;
5984 mos = extract32(insn, 29, 3);
5985 type = extract32(insn, 22, 2);
5986 rm = extract32(insn, 16, 5);
5987 op = extract32(insn, 14, 2);
5988 rn = extract32(insn, 5, 5);
5989 opc = extract32(insn, 3, 2);
5990 op2r = extract32(insn, 0, 3);
5992 if (mos || op || op2r) {
5993 unallocated_encoding(s);
5994 return;
5997 switch (type) {
5998 case 0:
5999 size = MO_32;
6000 break;
6001 case 1:
6002 size = MO_64;
6003 break;
6004 case 3:
6005 size = MO_16;
6006 if (dc_isar_feature(aa64_fp16, s)) {
6007 break;
6009 /* fallthru */
6010 default:
6011 unallocated_encoding(s);
6012 return;
6015 if (!fp_access_check(s)) {
6016 return;
6019 handle_fp_compare(s, size, rn, rm, opc & 1, opc & 2);
6022 /* Floating point conditional compare
6023 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 3 0
6024 * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
6025 * | M | 0 | S | 1 1 1 1 0 | type | 1 | Rm | cond | 0 1 | Rn | op | nzcv |
6026 * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
6028 static void disas_fp_ccomp(DisasContext *s, uint32_t insn)
6030 unsigned int mos, type, rm, cond, rn, op, nzcv;
6031 TCGLabel *label_continue = NULL;
6032 int size;
6034 mos = extract32(insn, 29, 3);
6035 type = extract32(insn, 22, 2);
6036 rm = extract32(insn, 16, 5);
6037 cond = extract32(insn, 12, 4);
6038 rn = extract32(insn, 5, 5);
6039 op = extract32(insn, 4, 1);
6040 nzcv = extract32(insn, 0, 4);
6042 if (mos) {
6043 unallocated_encoding(s);
6044 return;
6047 switch (type) {
6048 case 0:
6049 size = MO_32;
6050 break;
6051 case 1:
6052 size = MO_64;
6053 break;
6054 case 3:
6055 size = MO_16;
6056 if (dc_isar_feature(aa64_fp16, s)) {
6057 break;
6059 /* fallthru */
6060 default:
6061 unallocated_encoding(s);
6062 return;
6065 if (!fp_access_check(s)) {
6066 return;
6069 if (cond < 0x0e) { /* not always */
6070 TCGLabel *label_match = gen_new_label();
6071 label_continue = gen_new_label();
6072 arm_gen_test_cc(cond, label_match);
6073 /* nomatch: */
6074 gen_set_nzcv(tcg_constant_i64(nzcv << 28));
6075 tcg_gen_br(label_continue);
6076 gen_set_label(label_match);
6079 handle_fp_compare(s, size, rn, rm, false, op);
6081 if (cond < 0x0e) {
6082 gen_set_label(label_continue);
6086 /* Floating point conditional select
6087 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 0
6088 * +---+---+---+-----------+------+---+------+------+-----+------+------+
6089 * | M | 0 | S | 1 1 1 1 0 | type | 1 | Rm | cond | 1 1 | Rn | Rd |
6090 * +---+---+---+-----------+------+---+------+------+-----+------+------+
6092 static void disas_fp_csel(DisasContext *s, uint32_t insn)
6094 unsigned int mos, type, rm, cond, rn, rd;
6095 TCGv_i64 t_true, t_false;
6096 DisasCompare64 c;
6097 MemOp sz;
6099 mos = extract32(insn, 29, 3);
6100 type = extract32(insn, 22, 2);
6101 rm = extract32(insn, 16, 5);
6102 cond = extract32(insn, 12, 4);
6103 rn = extract32(insn, 5, 5);
6104 rd = extract32(insn, 0, 5);
6106 if (mos) {
6107 unallocated_encoding(s);
6108 return;
6111 switch (type) {
6112 case 0:
6113 sz = MO_32;
6114 break;
6115 case 1:
6116 sz = MO_64;
6117 break;
6118 case 3:
6119 sz = MO_16;
6120 if (dc_isar_feature(aa64_fp16, s)) {
6121 break;
6123 /* fallthru */
6124 default:
6125 unallocated_encoding(s);
6126 return;
6129 if (!fp_access_check(s)) {
6130 return;
6133 /* Zero extend sreg & hreg inputs to 64 bits now. */
6134 t_true = tcg_temp_new_i64();
6135 t_false = tcg_temp_new_i64();
6136 read_vec_element(s, t_true, rn, 0, sz);
6137 read_vec_element(s, t_false, rm, 0, sz);
6139 a64_test_cc(&c, cond);
6140 tcg_gen_movcond_i64(c.cond, t_true, c.value, tcg_constant_i64(0),
6141 t_true, t_false);
6142 tcg_temp_free_i64(t_false);
6143 a64_free_cc(&c);
6145 /* Note that sregs & hregs write back zeros to the high bits,
6146 and we've already done the zero-extension. */
6147 write_fp_dreg(s, rd, t_true);
6148 tcg_temp_free_i64(t_true);
6151 /* Floating-point data-processing (1 source) - half precision */
6152 static void handle_fp_1src_half(DisasContext *s, int opcode, int rd, int rn)
6154 TCGv_ptr fpst = NULL;
6155 TCGv_i32 tcg_op = read_fp_hreg(s, rn);
6156 TCGv_i32 tcg_res = tcg_temp_new_i32();
6158 switch (opcode) {
6159 case 0x0: /* FMOV */
6160 tcg_gen_mov_i32(tcg_res, tcg_op);
6161 break;
6162 case 0x1: /* FABS */
6163 tcg_gen_andi_i32(tcg_res, tcg_op, 0x7fff);
6164 break;
6165 case 0x2: /* FNEG */
6166 tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
6167 break;
6168 case 0x3: /* FSQRT */
6169 fpst = fpstatus_ptr(FPST_FPCR_F16);
6170 gen_helper_sqrt_f16(tcg_res, tcg_op, fpst);
6171 break;
6172 case 0x8: /* FRINTN */
6173 case 0x9: /* FRINTP */
6174 case 0xa: /* FRINTM */
6175 case 0xb: /* FRINTZ */
6176 case 0xc: /* FRINTA */
6178 TCGv_i32 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(opcode & 7));
6179 fpst = fpstatus_ptr(FPST_FPCR_F16);
6181 gen_helper_set_rmode(tcg_rmode, tcg_rmode, fpst);
6182 gen_helper_advsimd_rinth(tcg_res, tcg_op, fpst);
6184 gen_helper_set_rmode(tcg_rmode, tcg_rmode, fpst);
6185 tcg_temp_free_i32(tcg_rmode);
6186 break;
6188 case 0xe: /* FRINTX */
6189 fpst = fpstatus_ptr(FPST_FPCR_F16);
6190 gen_helper_advsimd_rinth_exact(tcg_res, tcg_op, fpst);
6191 break;
6192 case 0xf: /* FRINTI */
6193 fpst = fpstatus_ptr(FPST_FPCR_F16);
6194 gen_helper_advsimd_rinth(tcg_res, tcg_op, fpst);
6195 break;
6196 default:
6197 g_assert_not_reached();
6200 write_fp_sreg(s, rd, tcg_res);
6202 if (fpst) {
6203 tcg_temp_free_ptr(fpst);
6205 tcg_temp_free_i32(tcg_op);
6206 tcg_temp_free_i32(tcg_res);
6209 /* Floating-point data-processing (1 source) - single precision */
6210 static void handle_fp_1src_single(DisasContext *s, int opcode, int rd, int rn)
6212 void (*gen_fpst)(TCGv_i32, TCGv_i32, TCGv_ptr);
6213 TCGv_i32 tcg_op, tcg_res;
6214 TCGv_ptr fpst;
6215 int rmode = -1;
6217 tcg_op = read_fp_sreg(s, rn);
6218 tcg_res = tcg_temp_new_i32();
6220 switch (opcode) {
6221 case 0x0: /* FMOV */
6222 tcg_gen_mov_i32(tcg_res, tcg_op);
6223 goto done;
6224 case 0x1: /* FABS */
6225 gen_helper_vfp_abss(tcg_res, tcg_op);
6226 goto done;
6227 case 0x2: /* FNEG */
6228 gen_helper_vfp_negs(tcg_res, tcg_op);
6229 goto done;
6230 case 0x3: /* FSQRT */
6231 gen_helper_vfp_sqrts(tcg_res, tcg_op, cpu_env);
6232 goto done;
6233 case 0x6: /* BFCVT */
6234 gen_fpst = gen_helper_bfcvt;
6235 break;
6236 case 0x8: /* FRINTN */
6237 case 0x9: /* FRINTP */
6238 case 0xa: /* FRINTM */
6239 case 0xb: /* FRINTZ */
6240 case 0xc: /* FRINTA */
6241 rmode = arm_rmode_to_sf(opcode & 7);
6242 gen_fpst = gen_helper_rints;
6243 break;
6244 case 0xe: /* FRINTX */
6245 gen_fpst = gen_helper_rints_exact;
6246 break;
6247 case 0xf: /* FRINTI */
6248 gen_fpst = gen_helper_rints;
6249 break;
6250 case 0x10: /* FRINT32Z */
6251 rmode = float_round_to_zero;
6252 gen_fpst = gen_helper_frint32_s;
6253 break;
6254 case 0x11: /* FRINT32X */
6255 gen_fpst = gen_helper_frint32_s;
6256 break;
6257 case 0x12: /* FRINT64Z */
6258 rmode = float_round_to_zero;
6259 gen_fpst = gen_helper_frint64_s;
6260 break;
6261 case 0x13: /* FRINT64X */
6262 gen_fpst = gen_helper_frint64_s;
6263 break;
6264 default:
6265 g_assert_not_reached();
6268 fpst = fpstatus_ptr(FPST_FPCR);
6269 if (rmode >= 0) {
6270 TCGv_i32 tcg_rmode = tcg_const_i32(rmode);
6271 gen_helper_set_rmode(tcg_rmode, tcg_rmode, fpst);
6272 gen_fpst(tcg_res, tcg_op, fpst);
6273 gen_helper_set_rmode(tcg_rmode, tcg_rmode, fpst);
6274 tcg_temp_free_i32(tcg_rmode);
6275 } else {
6276 gen_fpst(tcg_res, tcg_op, fpst);
6278 tcg_temp_free_ptr(fpst);
6280 done:
6281 write_fp_sreg(s, rd, tcg_res);
6282 tcg_temp_free_i32(tcg_op);
6283 tcg_temp_free_i32(tcg_res);
6286 /* Floating-point data-processing (1 source) - double precision */
6287 static void handle_fp_1src_double(DisasContext *s, int opcode, int rd, int rn)
6289 void (*gen_fpst)(TCGv_i64, TCGv_i64, TCGv_ptr);
6290 TCGv_i64 tcg_op, tcg_res;
6291 TCGv_ptr fpst;
6292 int rmode = -1;
6294 switch (opcode) {
6295 case 0x0: /* FMOV */
6296 gen_gvec_fn2(s, false, rd, rn, tcg_gen_gvec_mov, 0);
6297 return;
6300 tcg_op = read_fp_dreg(s, rn);
6301 tcg_res = tcg_temp_new_i64();
6303 switch (opcode) {
6304 case 0x1: /* FABS */
6305 gen_helper_vfp_absd(tcg_res, tcg_op);
6306 goto done;
6307 case 0x2: /* FNEG */
6308 gen_helper_vfp_negd(tcg_res, tcg_op);
6309 goto done;
6310 case 0x3: /* FSQRT */
6311 gen_helper_vfp_sqrtd(tcg_res, tcg_op, cpu_env);
6312 goto done;
6313 case 0x8: /* FRINTN */
6314 case 0x9: /* FRINTP */
6315 case 0xa: /* FRINTM */
6316 case 0xb: /* FRINTZ */
6317 case 0xc: /* FRINTA */
6318 rmode = arm_rmode_to_sf(opcode & 7);
6319 gen_fpst = gen_helper_rintd;
6320 break;
6321 case 0xe: /* FRINTX */
6322 gen_fpst = gen_helper_rintd_exact;
6323 break;
6324 case 0xf: /* FRINTI */
6325 gen_fpst = gen_helper_rintd;
6326 break;
6327 case 0x10: /* FRINT32Z */
6328 rmode = float_round_to_zero;
6329 gen_fpst = gen_helper_frint32_d;
6330 break;
6331 case 0x11: /* FRINT32X */
6332 gen_fpst = gen_helper_frint32_d;
6333 break;
6334 case 0x12: /* FRINT64Z */
6335 rmode = float_round_to_zero;
6336 gen_fpst = gen_helper_frint64_d;
6337 break;
6338 case 0x13: /* FRINT64X */
6339 gen_fpst = gen_helper_frint64_d;
6340 break;
6341 default:
6342 g_assert_not_reached();
6345 fpst = fpstatus_ptr(FPST_FPCR);
6346 if (rmode >= 0) {
6347 TCGv_i32 tcg_rmode = tcg_const_i32(rmode);
6348 gen_helper_set_rmode(tcg_rmode, tcg_rmode, fpst);
6349 gen_fpst(tcg_res, tcg_op, fpst);
6350 gen_helper_set_rmode(tcg_rmode, tcg_rmode, fpst);
6351 tcg_temp_free_i32(tcg_rmode);
6352 } else {
6353 gen_fpst(tcg_res, tcg_op, fpst);
6355 tcg_temp_free_ptr(fpst);
6357 done:
6358 write_fp_dreg(s, rd, tcg_res);
6359 tcg_temp_free_i64(tcg_op);
6360 tcg_temp_free_i64(tcg_res);
6363 static void handle_fp_fcvt(DisasContext *s, int opcode,
6364 int rd, int rn, int dtype, int ntype)
6366 switch (ntype) {
6367 case 0x0:
6369 TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
6370 if (dtype == 1) {
6371 /* Single to double */
6372 TCGv_i64 tcg_rd = tcg_temp_new_i64();
6373 gen_helper_vfp_fcvtds(tcg_rd, tcg_rn, cpu_env);
6374 write_fp_dreg(s, rd, tcg_rd);
6375 tcg_temp_free_i64(tcg_rd);
6376 } else {
6377 /* Single to half */
6378 TCGv_i32 tcg_rd = tcg_temp_new_i32();
6379 TCGv_i32 ahp = get_ahp_flag();
6380 TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6382 gen_helper_vfp_fcvt_f32_to_f16(tcg_rd, tcg_rn, fpst, ahp);
6383 /* write_fp_sreg is OK here because top half of tcg_rd is zero */
6384 write_fp_sreg(s, rd, tcg_rd);
6385 tcg_temp_free_i32(tcg_rd);
6386 tcg_temp_free_i32(ahp);
6387 tcg_temp_free_ptr(fpst);
6389 tcg_temp_free_i32(tcg_rn);
6390 break;
6392 case 0x1:
6394 TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
6395 TCGv_i32 tcg_rd = tcg_temp_new_i32();
6396 if (dtype == 0) {
6397 /* Double to single */
6398 gen_helper_vfp_fcvtsd(tcg_rd, tcg_rn, cpu_env);
6399 } else {
6400 TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6401 TCGv_i32 ahp = get_ahp_flag();
6402 /* Double to half */
6403 gen_helper_vfp_fcvt_f64_to_f16(tcg_rd, tcg_rn, fpst, ahp);
6404 /* write_fp_sreg is OK here because top half of tcg_rd is zero */
6405 tcg_temp_free_ptr(fpst);
6406 tcg_temp_free_i32(ahp);
6408 write_fp_sreg(s, rd, tcg_rd);
6409 tcg_temp_free_i32(tcg_rd);
6410 tcg_temp_free_i64(tcg_rn);
6411 break;
6413 case 0x3:
6415 TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
6416 TCGv_ptr tcg_fpst = fpstatus_ptr(FPST_FPCR);
6417 TCGv_i32 tcg_ahp = get_ahp_flag();
6418 tcg_gen_ext16u_i32(tcg_rn, tcg_rn);
6419 if (dtype == 0) {
6420 /* Half to single */
6421 TCGv_i32 tcg_rd = tcg_temp_new_i32();
6422 gen_helper_vfp_fcvt_f16_to_f32(tcg_rd, tcg_rn, tcg_fpst, tcg_ahp);
6423 write_fp_sreg(s, rd, tcg_rd);
6424 tcg_temp_free_i32(tcg_rd);
6425 } else {
6426 /* Half to double */
6427 TCGv_i64 tcg_rd = tcg_temp_new_i64();
6428 gen_helper_vfp_fcvt_f16_to_f64(tcg_rd, tcg_rn, tcg_fpst, tcg_ahp);
6429 write_fp_dreg(s, rd, tcg_rd);
6430 tcg_temp_free_i64(tcg_rd);
6432 tcg_temp_free_i32(tcg_rn);
6433 tcg_temp_free_ptr(tcg_fpst);
6434 tcg_temp_free_i32(tcg_ahp);
6435 break;
6437 default:
6438 g_assert_not_reached();
6442 /* Floating point data-processing (1 source)
6443 * 31 30 29 28 24 23 22 21 20 15 14 10 9 5 4 0
6444 * +---+---+---+-----------+------+---+--------+-----------+------+------+
6445 * | M | 0 | S | 1 1 1 1 0 | type | 1 | opcode | 1 0 0 0 0 | Rn | Rd |
6446 * +---+---+---+-----------+------+---+--------+-----------+------+------+
6448 static void disas_fp_1src(DisasContext *s, uint32_t insn)
6450 int mos = extract32(insn, 29, 3);
6451 int type = extract32(insn, 22, 2);
6452 int opcode = extract32(insn, 15, 6);
6453 int rn = extract32(insn, 5, 5);
6454 int rd = extract32(insn, 0, 5);
6456 if (mos) {
6457 goto do_unallocated;
6460 switch (opcode) {
6461 case 0x4: case 0x5: case 0x7:
6463 /* FCVT between half, single and double precision */
6464 int dtype = extract32(opcode, 0, 2);
6465 if (type == 2 || dtype == type) {
6466 goto do_unallocated;
6468 if (!fp_access_check(s)) {
6469 return;
6472 handle_fp_fcvt(s, opcode, rd, rn, dtype, type);
6473 break;
6476 case 0x10 ... 0x13: /* FRINT{32,64}{X,Z} */
6477 if (type > 1 || !dc_isar_feature(aa64_frint, s)) {
6478 goto do_unallocated;
6480 /* fall through */
6481 case 0x0 ... 0x3:
6482 case 0x8 ... 0xc:
6483 case 0xe ... 0xf:
6484 /* 32-to-32 and 64-to-64 ops */
6485 switch (type) {
6486 case 0:
6487 if (!fp_access_check(s)) {
6488 return;
6490 handle_fp_1src_single(s, opcode, rd, rn);
6491 break;
6492 case 1:
6493 if (!fp_access_check(s)) {
6494 return;
6496 handle_fp_1src_double(s, opcode, rd, rn);
6497 break;
6498 case 3:
6499 if (!dc_isar_feature(aa64_fp16, s)) {
6500 goto do_unallocated;
6503 if (!fp_access_check(s)) {
6504 return;
6506 handle_fp_1src_half(s, opcode, rd, rn);
6507 break;
6508 default:
6509 goto do_unallocated;
6511 break;
6513 case 0x6:
6514 switch (type) {
6515 case 1: /* BFCVT */
6516 if (!dc_isar_feature(aa64_bf16, s)) {
6517 goto do_unallocated;
6519 if (!fp_access_check(s)) {
6520 return;
6522 handle_fp_1src_single(s, opcode, rd, rn);
6523 break;
6524 default:
6525 goto do_unallocated;
6527 break;
6529 default:
6530 do_unallocated:
6531 unallocated_encoding(s);
6532 break;
6536 /* Floating-point data-processing (2 source) - single precision */
6537 static void handle_fp_2src_single(DisasContext *s, int opcode,
6538 int rd, int rn, int rm)
6540 TCGv_i32 tcg_op1;
6541 TCGv_i32 tcg_op2;
6542 TCGv_i32 tcg_res;
6543 TCGv_ptr fpst;
6545 tcg_res = tcg_temp_new_i32();
6546 fpst = fpstatus_ptr(FPST_FPCR);
6547 tcg_op1 = read_fp_sreg(s, rn);
6548 tcg_op2 = read_fp_sreg(s, rm);
6550 switch (opcode) {
6551 case 0x0: /* FMUL */
6552 gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
6553 break;
6554 case 0x1: /* FDIV */
6555 gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
6556 break;
6557 case 0x2: /* FADD */
6558 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
6559 break;
6560 case 0x3: /* FSUB */
6561 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
6562 break;
6563 case 0x4: /* FMAX */
6564 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
6565 break;
6566 case 0x5: /* FMIN */
6567 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
6568 break;
6569 case 0x6: /* FMAXNM */
6570 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
6571 break;
6572 case 0x7: /* FMINNM */
6573 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
6574 break;
6575 case 0x8: /* FNMUL */
6576 gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
6577 gen_helper_vfp_negs(tcg_res, tcg_res);
6578 break;
6581 write_fp_sreg(s, rd, tcg_res);
6583 tcg_temp_free_ptr(fpst);
6584 tcg_temp_free_i32(tcg_op1);
6585 tcg_temp_free_i32(tcg_op2);
6586 tcg_temp_free_i32(tcg_res);
6589 /* Floating-point data-processing (2 source) - double precision */
6590 static void handle_fp_2src_double(DisasContext *s, int opcode,
6591 int rd, int rn, int rm)
6593 TCGv_i64 tcg_op1;
6594 TCGv_i64 tcg_op2;
6595 TCGv_i64 tcg_res;
6596 TCGv_ptr fpst;
6598 tcg_res = tcg_temp_new_i64();
6599 fpst = fpstatus_ptr(FPST_FPCR);
6600 tcg_op1 = read_fp_dreg(s, rn);
6601 tcg_op2 = read_fp_dreg(s, rm);
6603 switch (opcode) {
6604 case 0x0: /* FMUL */
6605 gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
6606 break;
6607 case 0x1: /* FDIV */
6608 gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
6609 break;
6610 case 0x2: /* FADD */
6611 gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
6612 break;
6613 case 0x3: /* FSUB */
6614 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
6615 break;
6616 case 0x4: /* FMAX */
6617 gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
6618 break;
6619 case 0x5: /* FMIN */
6620 gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
6621 break;
6622 case 0x6: /* FMAXNM */
6623 gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
6624 break;
6625 case 0x7: /* FMINNM */
6626 gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
6627 break;
6628 case 0x8: /* FNMUL */
6629 gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
6630 gen_helper_vfp_negd(tcg_res, tcg_res);
6631 break;
6634 write_fp_dreg(s, rd, tcg_res);
6636 tcg_temp_free_ptr(fpst);
6637 tcg_temp_free_i64(tcg_op1);
6638 tcg_temp_free_i64(tcg_op2);
6639 tcg_temp_free_i64(tcg_res);
6642 /* Floating-point data-processing (2 source) - half precision */
6643 static void handle_fp_2src_half(DisasContext *s, int opcode,
6644 int rd, int rn, int rm)
6646 TCGv_i32 tcg_op1;
6647 TCGv_i32 tcg_op2;
6648 TCGv_i32 tcg_res;
6649 TCGv_ptr fpst;
6651 tcg_res = tcg_temp_new_i32();
6652 fpst = fpstatus_ptr(FPST_FPCR_F16);
6653 tcg_op1 = read_fp_hreg(s, rn);
6654 tcg_op2 = read_fp_hreg(s, rm);
6656 switch (opcode) {
6657 case 0x0: /* FMUL */
6658 gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
6659 break;
6660 case 0x1: /* FDIV */
6661 gen_helper_advsimd_divh(tcg_res, tcg_op1, tcg_op2, fpst);
6662 break;
6663 case 0x2: /* FADD */
6664 gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
6665 break;
6666 case 0x3: /* FSUB */
6667 gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
6668 break;
6669 case 0x4: /* FMAX */
6670 gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
6671 break;
6672 case 0x5: /* FMIN */
6673 gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
6674 break;
6675 case 0x6: /* FMAXNM */
6676 gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
6677 break;
6678 case 0x7: /* FMINNM */
6679 gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
6680 break;
6681 case 0x8: /* FNMUL */
6682 gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
6683 tcg_gen_xori_i32(tcg_res, tcg_res, 0x8000);
6684 break;
6685 default:
6686 g_assert_not_reached();
6689 write_fp_sreg(s, rd, tcg_res);
6691 tcg_temp_free_ptr(fpst);
6692 tcg_temp_free_i32(tcg_op1);
6693 tcg_temp_free_i32(tcg_op2);
6694 tcg_temp_free_i32(tcg_res);
6697 /* Floating point data-processing (2 source)
6698 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 0
6699 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
6700 * | M | 0 | S | 1 1 1 1 0 | type | 1 | Rm | opcode | 1 0 | Rn | Rd |
6701 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
6703 static void disas_fp_2src(DisasContext *s, uint32_t insn)
6705 int mos = extract32(insn, 29, 3);
6706 int type = extract32(insn, 22, 2);
6707 int rd = extract32(insn, 0, 5);
6708 int rn = extract32(insn, 5, 5);
6709 int rm = extract32(insn, 16, 5);
6710 int opcode = extract32(insn, 12, 4);
6712 if (opcode > 8 || mos) {
6713 unallocated_encoding(s);
6714 return;
6717 switch (type) {
6718 case 0:
6719 if (!fp_access_check(s)) {
6720 return;
6722 handle_fp_2src_single(s, opcode, rd, rn, rm);
6723 break;
6724 case 1:
6725 if (!fp_access_check(s)) {
6726 return;
6728 handle_fp_2src_double(s, opcode, rd, rn, rm);
6729 break;
6730 case 3:
6731 if (!dc_isar_feature(aa64_fp16, s)) {
6732 unallocated_encoding(s);
6733 return;
6735 if (!fp_access_check(s)) {
6736 return;
6738 handle_fp_2src_half(s, opcode, rd, rn, rm);
6739 break;
6740 default:
6741 unallocated_encoding(s);
6745 /* Floating-point data-processing (3 source) - single precision */
6746 static void handle_fp_3src_single(DisasContext *s, bool o0, bool o1,
6747 int rd, int rn, int rm, int ra)
6749 TCGv_i32 tcg_op1, tcg_op2, tcg_op3;
6750 TCGv_i32 tcg_res = tcg_temp_new_i32();
6751 TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6753 tcg_op1 = read_fp_sreg(s, rn);
6754 tcg_op2 = read_fp_sreg(s, rm);
6755 tcg_op3 = read_fp_sreg(s, ra);
6757 /* These are fused multiply-add, and must be done as one
6758 * floating point operation with no rounding between the
6759 * multiplication and addition steps.
6760 * NB that doing the negations here as separate steps is
6761 * correct : an input NaN should come out with its sign bit
6762 * flipped if it is a negated-input.
6764 if (o1 == true) {
6765 gen_helper_vfp_negs(tcg_op3, tcg_op3);
6768 if (o0 != o1) {
6769 gen_helper_vfp_negs(tcg_op1, tcg_op1);
6772 gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
6774 write_fp_sreg(s, rd, tcg_res);
6776 tcg_temp_free_ptr(fpst);
6777 tcg_temp_free_i32(tcg_op1);
6778 tcg_temp_free_i32(tcg_op2);
6779 tcg_temp_free_i32(tcg_op3);
6780 tcg_temp_free_i32(tcg_res);
6783 /* Floating-point data-processing (3 source) - double precision */
6784 static void handle_fp_3src_double(DisasContext *s, bool o0, bool o1,
6785 int rd, int rn, int rm, int ra)
6787 TCGv_i64 tcg_op1, tcg_op2, tcg_op3;
6788 TCGv_i64 tcg_res = tcg_temp_new_i64();
6789 TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6791 tcg_op1 = read_fp_dreg(s, rn);
6792 tcg_op2 = read_fp_dreg(s, rm);
6793 tcg_op3 = read_fp_dreg(s, ra);
6795 /* These are fused multiply-add, and must be done as one
6796 * floating point operation with no rounding between the
6797 * multiplication and addition steps.
6798 * NB that doing the negations here as separate steps is
6799 * correct : an input NaN should come out with its sign bit
6800 * flipped if it is a negated-input.
6802 if (o1 == true) {
6803 gen_helper_vfp_negd(tcg_op3, tcg_op3);
6806 if (o0 != o1) {
6807 gen_helper_vfp_negd(tcg_op1, tcg_op1);
6810 gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
6812 write_fp_dreg(s, rd, tcg_res);
6814 tcg_temp_free_ptr(fpst);
6815 tcg_temp_free_i64(tcg_op1);
6816 tcg_temp_free_i64(tcg_op2);
6817 tcg_temp_free_i64(tcg_op3);
6818 tcg_temp_free_i64(tcg_res);
6821 /* Floating-point data-processing (3 source) - half precision */
6822 static void handle_fp_3src_half(DisasContext *s, bool o0, bool o1,
6823 int rd, int rn, int rm, int ra)
6825 TCGv_i32 tcg_op1, tcg_op2, tcg_op3;
6826 TCGv_i32 tcg_res = tcg_temp_new_i32();
6827 TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR_F16);
6829 tcg_op1 = read_fp_hreg(s, rn);
6830 tcg_op2 = read_fp_hreg(s, rm);
6831 tcg_op3 = read_fp_hreg(s, ra);
6833 /* These are fused multiply-add, and must be done as one
6834 * floating point operation with no rounding between the
6835 * multiplication and addition steps.
6836 * NB that doing the negations here as separate steps is
6837 * correct : an input NaN should come out with its sign bit
6838 * flipped if it is a negated-input.
6840 if (o1 == true) {
6841 tcg_gen_xori_i32(tcg_op3, tcg_op3, 0x8000);
6844 if (o0 != o1) {
6845 tcg_gen_xori_i32(tcg_op1, tcg_op1, 0x8000);
6848 gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
6850 write_fp_sreg(s, rd, tcg_res);
6852 tcg_temp_free_ptr(fpst);
6853 tcg_temp_free_i32(tcg_op1);
6854 tcg_temp_free_i32(tcg_op2);
6855 tcg_temp_free_i32(tcg_op3);
6856 tcg_temp_free_i32(tcg_res);
6859 /* Floating point data-processing (3 source)
6860 * 31 30 29 28 24 23 22 21 20 16 15 14 10 9 5 4 0
6861 * +---+---+---+-----------+------+----+------+----+------+------+------+
6862 * | M | 0 | S | 1 1 1 1 1 | type | o1 | Rm | o0 | Ra | Rn | Rd |
6863 * +---+---+---+-----------+------+----+------+----+------+------+------+
6865 static void disas_fp_3src(DisasContext *s, uint32_t insn)
6867 int mos = extract32(insn, 29, 3);
6868 int type = extract32(insn, 22, 2);
6869 int rd = extract32(insn, 0, 5);
6870 int rn = extract32(insn, 5, 5);
6871 int ra = extract32(insn, 10, 5);
6872 int rm = extract32(insn, 16, 5);
6873 bool o0 = extract32(insn, 15, 1);
6874 bool o1 = extract32(insn, 21, 1);
6876 if (mos) {
6877 unallocated_encoding(s);
6878 return;
6881 switch (type) {
6882 case 0:
6883 if (!fp_access_check(s)) {
6884 return;
6886 handle_fp_3src_single(s, o0, o1, rd, rn, rm, ra);
6887 break;
6888 case 1:
6889 if (!fp_access_check(s)) {
6890 return;
6892 handle_fp_3src_double(s, o0, o1, rd, rn, rm, ra);
6893 break;
6894 case 3:
6895 if (!dc_isar_feature(aa64_fp16, s)) {
6896 unallocated_encoding(s);
6897 return;
6899 if (!fp_access_check(s)) {
6900 return;
6902 handle_fp_3src_half(s, o0, o1, rd, rn, rm, ra);
6903 break;
6904 default:
6905 unallocated_encoding(s);
6909 /* Floating point immediate
6910 * 31 30 29 28 24 23 22 21 20 13 12 10 9 5 4 0
6911 * +---+---+---+-----------+------+---+------------+-------+------+------+
6912 * | M | 0 | S | 1 1 1 1 0 | type | 1 | imm8 | 1 0 0 | imm5 | Rd |
6913 * +---+---+---+-----------+------+---+------------+-------+------+------+
6915 static void disas_fp_imm(DisasContext *s, uint32_t insn)
6917 int rd = extract32(insn, 0, 5);
6918 int imm5 = extract32(insn, 5, 5);
6919 int imm8 = extract32(insn, 13, 8);
6920 int type = extract32(insn, 22, 2);
6921 int mos = extract32(insn, 29, 3);
6922 uint64_t imm;
6923 MemOp sz;
6925 if (mos || imm5) {
6926 unallocated_encoding(s);
6927 return;
6930 switch (type) {
6931 case 0:
6932 sz = MO_32;
6933 break;
6934 case 1:
6935 sz = MO_64;
6936 break;
6937 case 3:
6938 sz = MO_16;
6939 if (dc_isar_feature(aa64_fp16, s)) {
6940 break;
6942 /* fallthru */
6943 default:
6944 unallocated_encoding(s);
6945 return;
6948 if (!fp_access_check(s)) {
6949 return;
6952 imm = vfp_expand_imm(sz, imm8);
6953 write_fp_dreg(s, rd, tcg_constant_i64(imm));
6956 /* Handle floating point <=> fixed point conversions. Note that we can
6957 * also deal with fp <=> integer conversions as a special case (scale == 64)
6958 * OPTME: consider handling that special case specially or at least skipping
6959 * the call to scalbn in the helpers for zero shifts.
6961 static void handle_fpfpcvt(DisasContext *s, int rd, int rn, int opcode,
6962 bool itof, int rmode, int scale, int sf, int type)
6964 bool is_signed = !(opcode & 1);
6965 TCGv_ptr tcg_fpstatus;
6966 TCGv_i32 tcg_shift, tcg_single;
6967 TCGv_i64 tcg_double;
6969 tcg_fpstatus = fpstatus_ptr(type == 3 ? FPST_FPCR_F16 : FPST_FPCR);
6971 tcg_shift = tcg_constant_i32(64 - scale);
6973 if (itof) {
6974 TCGv_i64 tcg_int = cpu_reg(s, rn);
6975 if (!sf) {
6976 TCGv_i64 tcg_extend = new_tmp_a64(s);
6978 if (is_signed) {
6979 tcg_gen_ext32s_i64(tcg_extend, tcg_int);
6980 } else {
6981 tcg_gen_ext32u_i64(tcg_extend, tcg_int);
6984 tcg_int = tcg_extend;
6987 switch (type) {
6988 case 1: /* float64 */
6989 tcg_double = tcg_temp_new_i64();
6990 if (is_signed) {
6991 gen_helper_vfp_sqtod(tcg_double, tcg_int,
6992 tcg_shift, tcg_fpstatus);
6993 } else {
6994 gen_helper_vfp_uqtod(tcg_double, tcg_int,
6995 tcg_shift, tcg_fpstatus);
6997 write_fp_dreg(s, rd, tcg_double);
6998 tcg_temp_free_i64(tcg_double);
6999 break;
7001 case 0: /* float32 */
7002 tcg_single = tcg_temp_new_i32();
7003 if (is_signed) {
7004 gen_helper_vfp_sqtos(tcg_single, tcg_int,
7005 tcg_shift, tcg_fpstatus);
7006 } else {
7007 gen_helper_vfp_uqtos(tcg_single, tcg_int,
7008 tcg_shift, tcg_fpstatus);
7010 write_fp_sreg(s, rd, tcg_single);
7011 tcg_temp_free_i32(tcg_single);
7012 break;
7014 case 3: /* float16 */
7015 tcg_single = tcg_temp_new_i32();
7016 if (is_signed) {
7017 gen_helper_vfp_sqtoh(tcg_single, tcg_int,
7018 tcg_shift, tcg_fpstatus);
7019 } else {
7020 gen_helper_vfp_uqtoh(tcg_single, tcg_int,
7021 tcg_shift, tcg_fpstatus);
7023 write_fp_sreg(s, rd, tcg_single);
7024 tcg_temp_free_i32(tcg_single);
7025 break;
7027 default:
7028 g_assert_not_reached();
7030 } else {
7031 TCGv_i64 tcg_int = cpu_reg(s, rd);
7032 TCGv_i32 tcg_rmode;
7034 if (extract32(opcode, 2, 1)) {
7035 /* There are too many rounding modes to all fit into rmode,
7036 * so FCVTA[US] is a special case.
7038 rmode = FPROUNDING_TIEAWAY;
7041 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rmode));
7043 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
7045 switch (type) {
7046 case 1: /* float64 */
7047 tcg_double = read_fp_dreg(s, rn);
7048 if (is_signed) {
7049 if (!sf) {
7050 gen_helper_vfp_tosld(tcg_int, tcg_double,
7051 tcg_shift, tcg_fpstatus);
7052 } else {
7053 gen_helper_vfp_tosqd(tcg_int, tcg_double,
7054 tcg_shift, tcg_fpstatus);
7056 } else {
7057 if (!sf) {
7058 gen_helper_vfp_tould(tcg_int, tcg_double,
7059 tcg_shift, tcg_fpstatus);
7060 } else {
7061 gen_helper_vfp_touqd(tcg_int, tcg_double,
7062 tcg_shift, tcg_fpstatus);
7065 if (!sf) {
7066 tcg_gen_ext32u_i64(tcg_int, tcg_int);
7068 tcg_temp_free_i64(tcg_double);
7069 break;
7071 case 0: /* float32 */
7072 tcg_single = read_fp_sreg(s, rn);
7073 if (sf) {
7074 if (is_signed) {
7075 gen_helper_vfp_tosqs(tcg_int, tcg_single,
7076 tcg_shift, tcg_fpstatus);
7077 } else {
7078 gen_helper_vfp_touqs(tcg_int, tcg_single,
7079 tcg_shift, tcg_fpstatus);
7081 } else {
7082 TCGv_i32 tcg_dest = tcg_temp_new_i32();
7083 if (is_signed) {
7084 gen_helper_vfp_tosls(tcg_dest, tcg_single,
7085 tcg_shift, tcg_fpstatus);
7086 } else {
7087 gen_helper_vfp_touls(tcg_dest, tcg_single,
7088 tcg_shift, tcg_fpstatus);
7090 tcg_gen_extu_i32_i64(tcg_int, tcg_dest);
7091 tcg_temp_free_i32(tcg_dest);
7093 tcg_temp_free_i32(tcg_single);
7094 break;
7096 case 3: /* float16 */
7097 tcg_single = read_fp_sreg(s, rn);
7098 if (sf) {
7099 if (is_signed) {
7100 gen_helper_vfp_tosqh(tcg_int, tcg_single,
7101 tcg_shift, tcg_fpstatus);
7102 } else {
7103 gen_helper_vfp_touqh(tcg_int, tcg_single,
7104 tcg_shift, tcg_fpstatus);
7106 } else {
7107 TCGv_i32 tcg_dest = tcg_temp_new_i32();
7108 if (is_signed) {
7109 gen_helper_vfp_toslh(tcg_dest, tcg_single,
7110 tcg_shift, tcg_fpstatus);
7111 } else {
7112 gen_helper_vfp_toulh(tcg_dest, tcg_single,
7113 tcg_shift, tcg_fpstatus);
7115 tcg_gen_extu_i32_i64(tcg_int, tcg_dest);
7116 tcg_temp_free_i32(tcg_dest);
7118 tcg_temp_free_i32(tcg_single);
7119 break;
7121 default:
7122 g_assert_not_reached();
7125 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
7126 tcg_temp_free_i32(tcg_rmode);
7129 tcg_temp_free_ptr(tcg_fpstatus);
7132 /* Floating point <-> fixed point conversions
7133 * 31 30 29 28 24 23 22 21 20 19 18 16 15 10 9 5 4 0
7134 * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
7135 * | sf | 0 | S | 1 1 1 1 0 | type | 0 | rmode | opcode | scale | Rn | Rd |
7136 * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
7138 static void disas_fp_fixed_conv(DisasContext *s, uint32_t insn)
7140 int rd = extract32(insn, 0, 5);
7141 int rn = extract32(insn, 5, 5);
7142 int scale = extract32(insn, 10, 6);
7143 int opcode = extract32(insn, 16, 3);
7144 int rmode = extract32(insn, 19, 2);
7145 int type = extract32(insn, 22, 2);
7146 bool sbit = extract32(insn, 29, 1);
7147 bool sf = extract32(insn, 31, 1);
7148 bool itof;
7150 if (sbit || (!sf && scale < 32)) {
7151 unallocated_encoding(s);
7152 return;
7155 switch (type) {
7156 case 0: /* float32 */
7157 case 1: /* float64 */
7158 break;
7159 case 3: /* float16 */
7160 if (dc_isar_feature(aa64_fp16, s)) {
7161 break;
7163 /* fallthru */
7164 default:
7165 unallocated_encoding(s);
7166 return;
7169 switch ((rmode << 3) | opcode) {
7170 case 0x2: /* SCVTF */
7171 case 0x3: /* UCVTF */
7172 itof = true;
7173 break;
7174 case 0x18: /* FCVTZS */
7175 case 0x19: /* FCVTZU */
7176 itof = false;
7177 break;
7178 default:
7179 unallocated_encoding(s);
7180 return;
7183 if (!fp_access_check(s)) {
7184 return;
7187 handle_fpfpcvt(s, rd, rn, opcode, itof, FPROUNDING_ZERO, scale, sf, type);
7190 static void handle_fmov(DisasContext *s, int rd, int rn, int type, bool itof)
7192 /* FMOV: gpr to or from float, double, or top half of quad fp reg,
7193 * without conversion.
7196 if (itof) {
7197 TCGv_i64 tcg_rn = cpu_reg(s, rn);
7198 TCGv_i64 tmp;
7200 switch (type) {
7201 case 0:
7202 /* 32 bit */
7203 tmp = tcg_temp_new_i64();
7204 tcg_gen_ext32u_i64(tmp, tcg_rn);
7205 write_fp_dreg(s, rd, tmp);
7206 tcg_temp_free_i64(tmp);
7207 break;
7208 case 1:
7209 /* 64 bit */
7210 write_fp_dreg(s, rd, tcg_rn);
7211 break;
7212 case 2:
7213 /* 64 bit to top half. */
7214 tcg_gen_st_i64(tcg_rn, cpu_env, fp_reg_hi_offset(s, rd));
7215 clear_vec_high(s, true, rd);
7216 break;
7217 case 3:
7218 /* 16 bit */
7219 tmp = tcg_temp_new_i64();
7220 tcg_gen_ext16u_i64(tmp, tcg_rn);
7221 write_fp_dreg(s, rd, tmp);
7222 tcg_temp_free_i64(tmp);
7223 break;
7224 default:
7225 g_assert_not_reached();
7227 } else {
7228 TCGv_i64 tcg_rd = cpu_reg(s, rd);
7230 switch (type) {
7231 case 0:
7232 /* 32 bit */
7233 tcg_gen_ld32u_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_32));
7234 break;
7235 case 1:
7236 /* 64 bit */
7237 tcg_gen_ld_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_64));
7238 break;
7239 case 2:
7240 /* 64 bits from top half */
7241 tcg_gen_ld_i64(tcg_rd, cpu_env, fp_reg_hi_offset(s, rn));
7242 break;
7243 case 3:
7244 /* 16 bit */
7245 tcg_gen_ld16u_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_16));
7246 break;
7247 default:
7248 g_assert_not_reached();
7253 static void handle_fjcvtzs(DisasContext *s, int rd, int rn)
7255 TCGv_i64 t = read_fp_dreg(s, rn);
7256 TCGv_ptr fpstatus = fpstatus_ptr(FPST_FPCR);
7258 gen_helper_fjcvtzs(t, t, fpstatus);
7260 tcg_temp_free_ptr(fpstatus);
7262 tcg_gen_ext32u_i64(cpu_reg(s, rd), t);
7263 tcg_gen_extrh_i64_i32(cpu_ZF, t);
7264 tcg_gen_movi_i32(cpu_CF, 0);
7265 tcg_gen_movi_i32(cpu_NF, 0);
7266 tcg_gen_movi_i32(cpu_VF, 0);
7268 tcg_temp_free_i64(t);
7271 /* Floating point <-> integer conversions
7272 * 31 30 29 28 24 23 22 21 20 19 18 16 15 10 9 5 4 0
7273 * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
7274 * | sf | 0 | S | 1 1 1 1 0 | type | 1 | rmode | opc | 0 0 0 0 0 0 | Rn | Rd |
7275 * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
7277 static void disas_fp_int_conv(DisasContext *s, uint32_t insn)
7279 int rd = extract32(insn, 0, 5);
7280 int rn = extract32(insn, 5, 5);
7281 int opcode = extract32(insn, 16, 3);
7282 int rmode = extract32(insn, 19, 2);
7283 int type = extract32(insn, 22, 2);
7284 bool sbit = extract32(insn, 29, 1);
7285 bool sf = extract32(insn, 31, 1);
7286 bool itof = false;
7288 if (sbit) {
7289 goto do_unallocated;
7292 switch (opcode) {
7293 case 2: /* SCVTF */
7294 case 3: /* UCVTF */
7295 itof = true;
7296 /* fallthru */
7297 case 4: /* FCVTAS */
7298 case 5: /* FCVTAU */
7299 if (rmode != 0) {
7300 goto do_unallocated;
7302 /* fallthru */
7303 case 0: /* FCVT[NPMZ]S */
7304 case 1: /* FCVT[NPMZ]U */
7305 switch (type) {
7306 case 0: /* float32 */
7307 case 1: /* float64 */
7308 break;
7309 case 3: /* float16 */
7310 if (!dc_isar_feature(aa64_fp16, s)) {
7311 goto do_unallocated;
7313 break;
7314 default:
7315 goto do_unallocated;
7317 if (!fp_access_check(s)) {
7318 return;
7320 handle_fpfpcvt(s, rd, rn, opcode, itof, rmode, 64, sf, type);
7321 break;
7323 default:
7324 switch (sf << 7 | type << 5 | rmode << 3 | opcode) {
7325 case 0b01100110: /* FMOV half <-> 32-bit int */
7326 case 0b01100111:
7327 case 0b11100110: /* FMOV half <-> 64-bit int */
7328 case 0b11100111:
7329 if (!dc_isar_feature(aa64_fp16, s)) {
7330 goto do_unallocated;
7332 /* fallthru */
7333 case 0b00000110: /* FMOV 32-bit */
7334 case 0b00000111:
7335 case 0b10100110: /* FMOV 64-bit */
7336 case 0b10100111:
7337 case 0b11001110: /* FMOV top half of 128-bit */
7338 case 0b11001111:
7339 if (!fp_access_check(s)) {
7340 return;
7342 itof = opcode & 1;
7343 handle_fmov(s, rd, rn, type, itof);
7344 break;
7346 case 0b00111110: /* FJCVTZS */
7347 if (!dc_isar_feature(aa64_jscvt, s)) {
7348 goto do_unallocated;
7349 } else if (fp_access_check(s)) {
7350 handle_fjcvtzs(s, rd, rn);
7352 break;
7354 default:
7355 do_unallocated:
7356 unallocated_encoding(s);
7357 return;
7359 break;
7363 /* FP-specific subcases of table C3-6 (SIMD and FP data processing)
7364 * 31 30 29 28 25 24 0
7365 * +---+---+---+---------+-----------------------------+
7366 * | | 0 | | 1 1 1 1 | |
7367 * +---+---+---+---------+-----------------------------+
7369 static void disas_data_proc_fp(DisasContext *s, uint32_t insn)
7371 if (extract32(insn, 24, 1)) {
7372 /* Floating point data-processing (3 source) */
7373 disas_fp_3src(s, insn);
7374 } else if (extract32(insn, 21, 1) == 0) {
7375 /* Floating point to fixed point conversions */
7376 disas_fp_fixed_conv(s, insn);
7377 } else {
7378 switch (extract32(insn, 10, 2)) {
7379 case 1:
7380 /* Floating point conditional compare */
7381 disas_fp_ccomp(s, insn);
7382 break;
7383 case 2:
7384 /* Floating point data-processing (2 source) */
7385 disas_fp_2src(s, insn);
7386 break;
7387 case 3:
7388 /* Floating point conditional select */
7389 disas_fp_csel(s, insn);
7390 break;
7391 case 0:
7392 switch (ctz32(extract32(insn, 12, 4))) {
7393 case 0: /* [15:12] == xxx1 */
7394 /* Floating point immediate */
7395 disas_fp_imm(s, insn);
7396 break;
7397 case 1: /* [15:12] == xx10 */
7398 /* Floating point compare */
7399 disas_fp_compare(s, insn);
7400 break;
7401 case 2: /* [15:12] == x100 */
7402 /* Floating point data-processing (1 source) */
7403 disas_fp_1src(s, insn);
7404 break;
7405 case 3: /* [15:12] == 1000 */
7406 unallocated_encoding(s);
7407 break;
7408 default: /* [15:12] == 0000 */
7409 /* Floating point <-> integer conversions */
7410 disas_fp_int_conv(s, insn);
7411 break;
7413 break;
7418 static void do_ext64(DisasContext *s, TCGv_i64 tcg_left, TCGv_i64 tcg_right,
7419 int pos)
7421 /* Extract 64 bits from the middle of two concatenated 64 bit
7422 * vector register slices left:right. The extracted bits start
7423 * at 'pos' bits into the right (least significant) side.
7424 * We return the result in tcg_right, and guarantee not to
7425 * trash tcg_left.
7427 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
7428 assert(pos > 0 && pos < 64);
7430 tcg_gen_shri_i64(tcg_right, tcg_right, pos);
7431 tcg_gen_shli_i64(tcg_tmp, tcg_left, 64 - pos);
7432 tcg_gen_or_i64(tcg_right, tcg_right, tcg_tmp);
7434 tcg_temp_free_i64(tcg_tmp);
7437 /* EXT
7438 * 31 30 29 24 23 22 21 20 16 15 14 11 10 9 5 4 0
7439 * +---+---+-------------+-----+---+------+---+------+---+------+------+
7440 * | 0 | Q | 1 0 1 1 1 0 | op2 | 0 | Rm | 0 | imm4 | 0 | Rn | Rd |
7441 * +---+---+-------------+-----+---+------+---+------+---+------+------+
7443 static void disas_simd_ext(DisasContext *s, uint32_t insn)
7445 int is_q = extract32(insn, 30, 1);
7446 int op2 = extract32(insn, 22, 2);
7447 int imm4 = extract32(insn, 11, 4);
7448 int rm = extract32(insn, 16, 5);
7449 int rn = extract32(insn, 5, 5);
7450 int rd = extract32(insn, 0, 5);
7451 int pos = imm4 << 3;
7452 TCGv_i64 tcg_resl, tcg_resh;
7454 if (op2 != 0 || (!is_q && extract32(imm4, 3, 1))) {
7455 unallocated_encoding(s);
7456 return;
7459 if (!fp_access_check(s)) {
7460 return;
7463 tcg_resh = tcg_temp_new_i64();
7464 tcg_resl = tcg_temp_new_i64();
7466 /* Vd gets bits starting at pos bits into Vm:Vn. This is
7467 * either extracting 128 bits from a 128:128 concatenation, or
7468 * extracting 64 bits from a 64:64 concatenation.
7470 if (!is_q) {
7471 read_vec_element(s, tcg_resl, rn, 0, MO_64);
7472 if (pos != 0) {
7473 read_vec_element(s, tcg_resh, rm, 0, MO_64);
7474 do_ext64(s, tcg_resh, tcg_resl, pos);
7476 } else {
7477 TCGv_i64 tcg_hh;
7478 typedef struct {
7479 int reg;
7480 int elt;
7481 } EltPosns;
7482 EltPosns eltposns[] = { {rn, 0}, {rn, 1}, {rm, 0}, {rm, 1} };
7483 EltPosns *elt = eltposns;
7485 if (pos >= 64) {
7486 elt++;
7487 pos -= 64;
7490 read_vec_element(s, tcg_resl, elt->reg, elt->elt, MO_64);
7491 elt++;
7492 read_vec_element(s, tcg_resh, elt->reg, elt->elt, MO_64);
7493 elt++;
7494 if (pos != 0) {
7495 do_ext64(s, tcg_resh, tcg_resl, pos);
7496 tcg_hh = tcg_temp_new_i64();
7497 read_vec_element(s, tcg_hh, elt->reg, elt->elt, MO_64);
7498 do_ext64(s, tcg_hh, tcg_resh, pos);
7499 tcg_temp_free_i64(tcg_hh);
7503 write_vec_element(s, tcg_resl, rd, 0, MO_64);
7504 tcg_temp_free_i64(tcg_resl);
7505 if (is_q) {
7506 write_vec_element(s, tcg_resh, rd, 1, MO_64);
7508 tcg_temp_free_i64(tcg_resh);
7509 clear_vec_high(s, is_q, rd);
7512 /* TBL/TBX
7513 * 31 30 29 24 23 22 21 20 16 15 14 13 12 11 10 9 5 4 0
7514 * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
7515 * | 0 | Q | 0 0 1 1 1 0 | op2 | 0 | Rm | 0 | len | op | 0 0 | Rn | Rd |
7516 * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
7518 static void disas_simd_tb(DisasContext *s, uint32_t insn)
7520 int op2 = extract32(insn, 22, 2);
7521 int is_q = extract32(insn, 30, 1);
7522 int rm = extract32(insn, 16, 5);
7523 int rn = extract32(insn, 5, 5);
7524 int rd = extract32(insn, 0, 5);
7525 int is_tbx = extract32(insn, 12, 1);
7526 int len = (extract32(insn, 13, 2) + 1) * 16;
7528 if (op2 != 0) {
7529 unallocated_encoding(s);
7530 return;
7533 if (!fp_access_check(s)) {
7534 return;
7537 tcg_gen_gvec_2_ptr(vec_full_reg_offset(s, rd),
7538 vec_full_reg_offset(s, rm), cpu_env,
7539 is_q ? 16 : 8, vec_full_reg_size(s),
7540 (len << 6) | (is_tbx << 5) | rn,
7541 gen_helper_simd_tblx);
7544 /* ZIP/UZP/TRN
7545 * 31 30 29 24 23 22 21 20 16 15 14 12 11 10 9 5 4 0
7546 * +---+---+-------------+------+---+------+---+------------------+------+
7547 * | 0 | Q | 0 0 1 1 1 0 | size | 0 | Rm | 0 | opc | 1 0 | Rn | Rd |
7548 * +---+---+-------------+------+---+------+---+------------------+------+
7550 static void disas_simd_zip_trn(DisasContext *s, uint32_t insn)
7552 int rd = extract32(insn, 0, 5);
7553 int rn = extract32(insn, 5, 5);
7554 int rm = extract32(insn, 16, 5);
7555 int size = extract32(insn, 22, 2);
7556 /* opc field bits [1:0] indicate ZIP/UZP/TRN;
7557 * bit 2 indicates 1 vs 2 variant of the insn.
7559 int opcode = extract32(insn, 12, 2);
7560 bool part = extract32(insn, 14, 1);
7561 bool is_q = extract32(insn, 30, 1);
7562 int esize = 8 << size;
7563 int i, ofs;
7564 int datasize = is_q ? 128 : 64;
7565 int elements = datasize / esize;
7566 TCGv_i64 tcg_res, tcg_resl, tcg_resh;
7568 if (opcode == 0 || (size == 3 && !is_q)) {
7569 unallocated_encoding(s);
7570 return;
7573 if (!fp_access_check(s)) {
7574 return;
7577 tcg_resl = tcg_const_i64(0);
7578 tcg_resh = is_q ? tcg_const_i64(0) : NULL;
7579 tcg_res = tcg_temp_new_i64();
7581 for (i = 0; i < elements; i++) {
7582 switch (opcode) {
7583 case 1: /* UZP1/2 */
7585 int midpoint = elements / 2;
7586 if (i < midpoint) {
7587 read_vec_element(s, tcg_res, rn, 2 * i + part, size);
7588 } else {
7589 read_vec_element(s, tcg_res, rm,
7590 2 * (i - midpoint) + part, size);
7592 break;
7594 case 2: /* TRN1/2 */
7595 if (i & 1) {
7596 read_vec_element(s, tcg_res, rm, (i & ~1) + part, size);
7597 } else {
7598 read_vec_element(s, tcg_res, rn, (i & ~1) + part, size);
7600 break;
7601 case 3: /* ZIP1/2 */
7603 int base = part * elements / 2;
7604 if (i & 1) {
7605 read_vec_element(s, tcg_res, rm, base + (i >> 1), size);
7606 } else {
7607 read_vec_element(s, tcg_res, rn, base + (i >> 1), size);
7609 break;
7611 default:
7612 g_assert_not_reached();
7615 ofs = i * esize;
7616 if (ofs < 64) {
7617 tcg_gen_shli_i64(tcg_res, tcg_res, ofs);
7618 tcg_gen_or_i64(tcg_resl, tcg_resl, tcg_res);
7619 } else {
7620 tcg_gen_shli_i64(tcg_res, tcg_res, ofs - 64);
7621 tcg_gen_or_i64(tcg_resh, tcg_resh, tcg_res);
7625 tcg_temp_free_i64(tcg_res);
7627 write_vec_element(s, tcg_resl, rd, 0, MO_64);
7628 tcg_temp_free_i64(tcg_resl);
7630 if (is_q) {
7631 write_vec_element(s, tcg_resh, rd, 1, MO_64);
7632 tcg_temp_free_i64(tcg_resh);
7634 clear_vec_high(s, is_q, rd);
7638 * do_reduction_op helper
7640 * This mirrors the Reduce() pseudocode in the ARM ARM. It is
7641 * important for correct NaN propagation that we do these
7642 * operations in exactly the order specified by the pseudocode.
7644 * This is a recursive function, TCG temps should be freed by the
7645 * calling function once it is done with the values.
7647 static TCGv_i32 do_reduction_op(DisasContext *s, int fpopcode, int rn,
7648 int esize, int size, int vmap, TCGv_ptr fpst)
7650 if (esize == size) {
7651 int element;
7652 MemOp msize = esize == 16 ? MO_16 : MO_32;
7653 TCGv_i32 tcg_elem;
7655 /* We should have one register left here */
7656 assert(ctpop8(vmap) == 1);
7657 element = ctz32(vmap);
7658 assert(element < 8);
7660 tcg_elem = tcg_temp_new_i32();
7661 read_vec_element_i32(s, tcg_elem, rn, element, msize);
7662 return tcg_elem;
7663 } else {
7664 int bits = size / 2;
7665 int shift = ctpop8(vmap) / 2;
7666 int vmap_lo = (vmap >> shift) & vmap;
7667 int vmap_hi = (vmap & ~vmap_lo);
7668 TCGv_i32 tcg_hi, tcg_lo, tcg_res;
7670 tcg_hi = do_reduction_op(s, fpopcode, rn, esize, bits, vmap_hi, fpst);
7671 tcg_lo = do_reduction_op(s, fpopcode, rn, esize, bits, vmap_lo, fpst);
7672 tcg_res = tcg_temp_new_i32();
7674 switch (fpopcode) {
7675 case 0x0c: /* fmaxnmv half-precision */
7676 gen_helper_advsimd_maxnumh(tcg_res, tcg_lo, tcg_hi, fpst);
7677 break;
7678 case 0x0f: /* fmaxv half-precision */
7679 gen_helper_advsimd_maxh(tcg_res, tcg_lo, tcg_hi, fpst);
7680 break;
7681 case 0x1c: /* fminnmv half-precision */
7682 gen_helper_advsimd_minnumh(tcg_res, tcg_lo, tcg_hi, fpst);
7683 break;
7684 case 0x1f: /* fminv half-precision */
7685 gen_helper_advsimd_minh(tcg_res, tcg_lo, tcg_hi, fpst);
7686 break;
7687 case 0x2c: /* fmaxnmv */
7688 gen_helper_vfp_maxnums(tcg_res, tcg_lo, tcg_hi, fpst);
7689 break;
7690 case 0x2f: /* fmaxv */
7691 gen_helper_vfp_maxs(tcg_res, tcg_lo, tcg_hi, fpst);
7692 break;
7693 case 0x3c: /* fminnmv */
7694 gen_helper_vfp_minnums(tcg_res, tcg_lo, tcg_hi, fpst);
7695 break;
7696 case 0x3f: /* fminv */
7697 gen_helper_vfp_mins(tcg_res, tcg_lo, tcg_hi, fpst);
7698 break;
7699 default:
7700 g_assert_not_reached();
7703 tcg_temp_free_i32(tcg_hi);
7704 tcg_temp_free_i32(tcg_lo);
7705 return tcg_res;
7709 /* AdvSIMD across lanes
7710 * 31 30 29 28 24 23 22 21 17 16 12 11 10 9 5 4 0
7711 * +---+---+---+-----------+------+-----------+--------+-----+------+------+
7712 * | 0 | Q | U | 0 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 | Rn | Rd |
7713 * +---+---+---+-----------+------+-----------+--------+-----+------+------+
7715 static void disas_simd_across_lanes(DisasContext *s, uint32_t insn)
7717 int rd = extract32(insn, 0, 5);
7718 int rn = extract32(insn, 5, 5);
7719 int size = extract32(insn, 22, 2);
7720 int opcode = extract32(insn, 12, 5);
7721 bool is_q = extract32(insn, 30, 1);
7722 bool is_u = extract32(insn, 29, 1);
7723 bool is_fp = false;
7724 bool is_min = false;
7725 int esize;
7726 int elements;
7727 int i;
7728 TCGv_i64 tcg_res, tcg_elt;
7730 switch (opcode) {
7731 case 0x1b: /* ADDV */
7732 if (is_u) {
7733 unallocated_encoding(s);
7734 return;
7736 /* fall through */
7737 case 0x3: /* SADDLV, UADDLV */
7738 case 0xa: /* SMAXV, UMAXV */
7739 case 0x1a: /* SMINV, UMINV */
7740 if (size == 3 || (size == 2 && !is_q)) {
7741 unallocated_encoding(s);
7742 return;
7744 break;
7745 case 0xc: /* FMAXNMV, FMINNMV */
7746 case 0xf: /* FMAXV, FMINV */
7747 /* Bit 1 of size field encodes min vs max and the actual size
7748 * depends on the encoding of the U bit. If not set (and FP16
7749 * enabled) then we do half-precision float instead of single
7750 * precision.
7752 is_min = extract32(size, 1, 1);
7753 is_fp = true;
7754 if (!is_u && dc_isar_feature(aa64_fp16, s)) {
7755 size = 1;
7756 } else if (!is_u || !is_q || extract32(size, 0, 1)) {
7757 unallocated_encoding(s);
7758 return;
7759 } else {
7760 size = 2;
7762 break;
7763 default:
7764 unallocated_encoding(s);
7765 return;
7768 if (!fp_access_check(s)) {
7769 return;
7772 esize = 8 << size;
7773 elements = (is_q ? 128 : 64) / esize;
7775 tcg_res = tcg_temp_new_i64();
7776 tcg_elt = tcg_temp_new_i64();
7778 /* These instructions operate across all lanes of a vector
7779 * to produce a single result. We can guarantee that a 64
7780 * bit intermediate is sufficient:
7781 * + for [US]ADDLV the maximum element size is 32 bits, and
7782 * the result type is 64 bits
7783 * + for FMAX*V, FMIN*V, ADDV the intermediate type is the
7784 * same as the element size, which is 32 bits at most
7785 * For the integer operations we can choose to work at 64
7786 * or 32 bits and truncate at the end; for simplicity
7787 * we use 64 bits always. The floating point
7788 * ops do require 32 bit intermediates, though.
7790 if (!is_fp) {
7791 read_vec_element(s, tcg_res, rn, 0, size | (is_u ? 0 : MO_SIGN));
7793 for (i = 1; i < elements; i++) {
7794 read_vec_element(s, tcg_elt, rn, i, size | (is_u ? 0 : MO_SIGN));
7796 switch (opcode) {
7797 case 0x03: /* SADDLV / UADDLV */
7798 case 0x1b: /* ADDV */
7799 tcg_gen_add_i64(tcg_res, tcg_res, tcg_elt);
7800 break;
7801 case 0x0a: /* SMAXV / UMAXV */
7802 if (is_u) {
7803 tcg_gen_umax_i64(tcg_res, tcg_res, tcg_elt);
7804 } else {
7805 tcg_gen_smax_i64(tcg_res, tcg_res, tcg_elt);
7807 break;
7808 case 0x1a: /* SMINV / UMINV */
7809 if (is_u) {
7810 tcg_gen_umin_i64(tcg_res, tcg_res, tcg_elt);
7811 } else {
7812 tcg_gen_smin_i64(tcg_res, tcg_res, tcg_elt);
7814 break;
7815 default:
7816 g_assert_not_reached();
7820 } else {
7821 /* Floating point vector reduction ops which work across 32
7822 * bit (single) or 16 bit (half-precision) intermediates.
7823 * Note that correct NaN propagation requires that we do these
7824 * operations in exactly the order specified by the pseudocode.
7826 TCGv_ptr fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
7827 int fpopcode = opcode | is_min << 4 | is_u << 5;
7828 int vmap = (1 << elements) - 1;
7829 TCGv_i32 tcg_res32 = do_reduction_op(s, fpopcode, rn, esize,
7830 (is_q ? 128 : 64), vmap, fpst);
7831 tcg_gen_extu_i32_i64(tcg_res, tcg_res32);
7832 tcg_temp_free_i32(tcg_res32);
7833 tcg_temp_free_ptr(fpst);
7836 tcg_temp_free_i64(tcg_elt);
7838 /* Now truncate the result to the width required for the final output */
7839 if (opcode == 0x03) {
7840 /* SADDLV, UADDLV: result is 2*esize */
7841 size++;
7844 switch (size) {
7845 case 0:
7846 tcg_gen_ext8u_i64(tcg_res, tcg_res);
7847 break;
7848 case 1:
7849 tcg_gen_ext16u_i64(tcg_res, tcg_res);
7850 break;
7851 case 2:
7852 tcg_gen_ext32u_i64(tcg_res, tcg_res);
7853 break;
7854 case 3:
7855 break;
7856 default:
7857 g_assert_not_reached();
7860 write_fp_dreg(s, rd, tcg_res);
7861 tcg_temp_free_i64(tcg_res);
7864 /* DUP (Element, Vector)
7866 * 31 30 29 21 20 16 15 10 9 5 4 0
7867 * +---+---+-------------------+--------+-------------+------+------+
7868 * | 0 | Q | 0 0 1 1 1 0 0 0 0 | imm5 | 0 0 0 0 0 1 | Rn | Rd |
7869 * +---+---+-------------------+--------+-------------+------+------+
7871 * size: encoded in imm5 (see ARM ARM LowestSetBit())
7873 static void handle_simd_dupe(DisasContext *s, int is_q, int rd, int rn,
7874 int imm5)
7876 int size = ctz32(imm5);
7877 int index;
7879 if (size > 3 || (size == 3 && !is_q)) {
7880 unallocated_encoding(s);
7881 return;
7884 if (!fp_access_check(s)) {
7885 return;
7888 index = imm5 >> (size + 1);
7889 tcg_gen_gvec_dup_mem(size, vec_full_reg_offset(s, rd),
7890 vec_reg_offset(s, rn, index, size),
7891 is_q ? 16 : 8, vec_full_reg_size(s));
7894 /* DUP (element, scalar)
7895 * 31 21 20 16 15 10 9 5 4 0
7896 * +-----------------------+--------+-------------+------+------+
7897 * | 0 1 0 1 1 1 1 0 0 0 0 | imm5 | 0 0 0 0 0 1 | Rn | Rd |
7898 * +-----------------------+--------+-------------+------+------+
7900 static void handle_simd_dupes(DisasContext *s, int rd, int rn,
7901 int imm5)
7903 int size = ctz32(imm5);
7904 int index;
7905 TCGv_i64 tmp;
7907 if (size > 3) {
7908 unallocated_encoding(s);
7909 return;
7912 if (!fp_access_check(s)) {
7913 return;
7916 index = imm5 >> (size + 1);
7918 /* This instruction just extracts the specified element and
7919 * zero-extends it into the bottom of the destination register.
7921 tmp = tcg_temp_new_i64();
7922 read_vec_element(s, tmp, rn, index, size);
7923 write_fp_dreg(s, rd, tmp);
7924 tcg_temp_free_i64(tmp);
7927 /* DUP (General)
7929 * 31 30 29 21 20 16 15 10 9 5 4 0
7930 * +---+---+-------------------+--------+-------------+------+------+
7931 * | 0 | Q | 0 0 1 1 1 0 0 0 0 | imm5 | 0 0 0 0 1 1 | Rn | Rd |
7932 * +---+---+-------------------+--------+-------------+------+------+
7934 * size: encoded in imm5 (see ARM ARM LowestSetBit())
7936 static void handle_simd_dupg(DisasContext *s, int is_q, int rd, int rn,
7937 int imm5)
7939 int size = ctz32(imm5);
7940 uint32_t dofs, oprsz, maxsz;
7942 if (size > 3 || ((size == 3) && !is_q)) {
7943 unallocated_encoding(s);
7944 return;
7947 if (!fp_access_check(s)) {
7948 return;
7951 dofs = vec_full_reg_offset(s, rd);
7952 oprsz = is_q ? 16 : 8;
7953 maxsz = vec_full_reg_size(s);
7955 tcg_gen_gvec_dup_i64(size, dofs, oprsz, maxsz, cpu_reg(s, rn));
7958 /* INS (Element)
7960 * 31 21 20 16 15 14 11 10 9 5 4 0
7961 * +-----------------------+--------+------------+---+------+------+
7962 * | 0 1 1 0 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 | Rn | Rd |
7963 * +-----------------------+--------+------------+---+------+------+
7965 * size: encoded in imm5 (see ARM ARM LowestSetBit())
7966 * index: encoded in imm5<4:size+1>
7968 static void handle_simd_inse(DisasContext *s, int rd, int rn,
7969 int imm4, int imm5)
7971 int size = ctz32(imm5);
7972 int src_index, dst_index;
7973 TCGv_i64 tmp;
7975 if (size > 3) {
7976 unallocated_encoding(s);
7977 return;
7980 if (!fp_access_check(s)) {
7981 return;
7984 dst_index = extract32(imm5, 1+size, 5);
7985 src_index = extract32(imm4, size, 4);
7987 tmp = tcg_temp_new_i64();
7989 read_vec_element(s, tmp, rn, src_index, size);
7990 write_vec_element(s, tmp, rd, dst_index, size);
7992 tcg_temp_free_i64(tmp);
7994 /* INS is considered a 128-bit write for SVE. */
7995 clear_vec_high(s, true, rd);
7999 /* INS (General)
8001 * 31 21 20 16 15 10 9 5 4 0
8002 * +-----------------------+--------+-------------+------+------+
8003 * | 0 1 0 0 1 1 1 0 0 0 0 | imm5 | 0 0 0 1 1 1 | Rn | Rd |
8004 * +-----------------------+--------+-------------+------+------+
8006 * size: encoded in imm5 (see ARM ARM LowestSetBit())
8007 * index: encoded in imm5<4:size+1>
8009 static void handle_simd_insg(DisasContext *s, int rd, int rn, int imm5)
8011 int size = ctz32(imm5);
8012 int idx;
8014 if (size > 3) {
8015 unallocated_encoding(s);
8016 return;
8019 if (!fp_access_check(s)) {
8020 return;
8023 idx = extract32(imm5, 1 + size, 4 - size);
8024 write_vec_element(s, cpu_reg(s, rn), rd, idx, size);
8026 /* INS is considered a 128-bit write for SVE. */
8027 clear_vec_high(s, true, rd);
8031 * UMOV (General)
8032 * SMOV (General)
8034 * 31 30 29 21 20 16 15 12 10 9 5 4 0
8035 * +---+---+-------------------+--------+-------------+------+------+
8036 * | 0 | Q | 0 0 1 1 1 0 0 0 0 | imm5 | 0 0 1 U 1 1 | Rn | Rd |
8037 * +---+---+-------------------+--------+-------------+------+------+
8039 * U: unsigned when set
8040 * size: encoded in imm5 (see ARM ARM LowestSetBit())
8042 static void handle_simd_umov_smov(DisasContext *s, int is_q, int is_signed,
8043 int rn, int rd, int imm5)
8045 int size = ctz32(imm5);
8046 int element;
8047 TCGv_i64 tcg_rd;
8049 /* Check for UnallocatedEncodings */
8050 if (is_signed) {
8051 if (size > 2 || (size == 2 && !is_q)) {
8052 unallocated_encoding(s);
8053 return;
8055 } else {
8056 if (size > 3
8057 || (size < 3 && is_q)
8058 || (size == 3 && !is_q)) {
8059 unallocated_encoding(s);
8060 return;
8064 if (!fp_access_check(s)) {
8065 return;
8068 element = extract32(imm5, 1+size, 4);
8070 tcg_rd = cpu_reg(s, rd);
8071 read_vec_element(s, tcg_rd, rn, element, size | (is_signed ? MO_SIGN : 0));
8072 if (is_signed && !is_q) {
8073 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
8077 /* AdvSIMD copy
8078 * 31 30 29 28 21 20 16 15 14 11 10 9 5 4 0
8079 * +---+---+----+-----------------+------+---+------+---+------+------+
8080 * | 0 | Q | op | 0 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 | Rn | Rd |
8081 * +---+---+----+-----------------+------+---+------+---+------+------+
8083 static void disas_simd_copy(DisasContext *s, uint32_t insn)
8085 int rd = extract32(insn, 0, 5);
8086 int rn = extract32(insn, 5, 5);
8087 int imm4 = extract32(insn, 11, 4);
8088 int op = extract32(insn, 29, 1);
8089 int is_q = extract32(insn, 30, 1);
8090 int imm5 = extract32(insn, 16, 5);
8092 if (op) {
8093 if (is_q) {
8094 /* INS (element) */
8095 handle_simd_inse(s, rd, rn, imm4, imm5);
8096 } else {
8097 unallocated_encoding(s);
8099 } else {
8100 switch (imm4) {
8101 case 0:
8102 /* DUP (element - vector) */
8103 handle_simd_dupe(s, is_q, rd, rn, imm5);
8104 break;
8105 case 1:
8106 /* DUP (general) */
8107 handle_simd_dupg(s, is_q, rd, rn, imm5);
8108 break;
8109 case 3:
8110 if (is_q) {
8111 /* INS (general) */
8112 handle_simd_insg(s, rd, rn, imm5);
8113 } else {
8114 unallocated_encoding(s);
8116 break;
8117 case 5:
8118 case 7:
8119 /* UMOV/SMOV (is_q indicates 32/64; imm4 indicates signedness) */
8120 handle_simd_umov_smov(s, is_q, (imm4 == 5), rn, rd, imm5);
8121 break;
8122 default:
8123 unallocated_encoding(s);
8124 break;
8129 /* AdvSIMD modified immediate
8130 * 31 30 29 28 19 18 16 15 12 11 10 9 5 4 0
8131 * +---+---+----+---------------------+-----+-------+----+---+-------+------+
8132 * | 0 | Q | op | 0 1 1 1 1 0 0 0 0 0 | abc | cmode | o2 | 1 | defgh | Rd |
8133 * +---+---+----+---------------------+-----+-------+----+---+-------+------+
8135 * There are a number of operations that can be carried out here:
8136 * MOVI - move (shifted) imm into register
8137 * MVNI - move inverted (shifted) imm into register
8138 * ORR - bitwise OR of (shifted) imm with register
8139 * BIC - bitwise clear of (shifted) imm with register
8140 * With ARMv8.2 we also have:
8141 * FMOV half-precision
8143 static void disas_simd_mod_imm(DisasContext *s, uint32_t insn)
8145 int rd = extract32(insn, 0, 5);
8146 int cmode = extract32(insn, 12, 4);
8147 int o2 = extract32(insn, 11, 1);
8148 uint64_t abcdefgh = extract32(insn, 5, 5) | (extract32(insn, 16, 3) << 5);
8149 bool is_neg = extract32(insn, 29, 1);
8150 bool is_q = extract32(insn, 30, 1);
8151 uint64_t imm = 0;
8153 if (o2 != 0 || ((cmode == 0xf) && is_neg && !is_q)) {
8154 /* Check for FMOV (vector, immediate) - half-precision */
8155 if (!(dc_isar_feature(aa64_fp16, s) && o2 && cmode == 0xf)) {
8156 unallocated_encoding(s);
8157 return;
8161 if (!fp_access_check(s)) {
8162 return;
8165 if (cmode == 15 && o2 && !is_neg) {
8166 /* FMOV (vector, immediate) - half-precision */
8167 imm = vfp_expand_imm(MO_16, abcdefgh);
8168 /* now duplicate across the lanes */
8169 imm = dup_const(MO_16, imm);
8170 } else {
8171 imm = asimd_imm_const(abcdefgh, cmode, is_neg);
8174 if (!((cmode & 0x9) == 0x1 || (cmode & 0xd) == 0x9)) {
8175 /* MOVI or MVNI, with MVNI negation handled above. */
8176 tcg_gen_gvec_dup_imm(MO_64, vec_full_reg_offset(s, rd), is_q ? 16 : 8,
8177 vec_full_reg_size(s), imm);
8178 } else {
8179 /* ORR or BIC, with BIC negation to AND handled above. */
8180 if (is_neg) {
8181 gen_gvec_fn2i(s, is_q, rd, rd, imm, tcg_gen_gvec_andi, MO_64);
8182 } else {
8183 gen_gvec_fn2i(s, is_q, rd, rd, imm, tcg_gen_gvec_ori, MO_64);
8188 /* AdvSIMD scalar copy
8189 * 31 30 29 28 21 20 16 15 14 11 10 9 5 4 0
8190 * +-----+----+-----------------+------+---+------+---+------+------+
8191 * | 0 1 | op | 1 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 | Rn | Rd |
8192 * +-----+----+-----------------+------+---+------+---+------+------+
8194 static void disas_simd_scalar_copy(DisasContext *s, uint32_t insn)
8196 int rd = extract32(insn, 0, 5);
8197 int rn = extract32(insn, 5, 5);
8198 int imm4 = extract32(insn, 11, 4);
8199 int imm5 = extract32(insn, 16, 5);
8200 int op = extract32(insn, 29, 1);
8202 if (op != 0 || imm4 != 0) {
8203 unallocated_encoding(s);
8204 return;
8207 /* DUP (element, scalar) */
8208 handle_simd_dupes(s, rd, rn, imm5);
8211 /* AdvSIMD scalar pairwise
8212 * 31 30 29 28 24 23 22 21 17 16 12 11 10 9 5 4 0
8213 * +-----+---+-----------+------+-----------+--------+-----+------+------+
8214 * | 0 1 | U | 1 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 | Rn | Rd |
8215 * +-----+---+-----------+------+-----------+--------+-----+------+------+
8217 static void disas_simd_scalar_pairwise(DisasContext *s, uint32_t insn)
8219 int u = extract32(insn, 29, 1);
8220 int size = extract32(insn, 22, 2);
8221 int opcode = extract32(insn, 12, 5);
8222 int rn = extract32(insn, 5, 5);
8223 int rd = extract32(insn, 0, 5);
8224 TCGv_ptr fpst;
8226 /* For some ops (the FP ones), size[1] is part of the encoding.
8227 * For ADDP strictly it is not but size[1] is always 1 for valid
8228 * encodings.
8230 opcode |= (extract32(size, 1, 1) << 5);
8232 switch (opcode) {
8233 case 0x3b: /* ADDP */
8234 if (u || size != 3) {
8235 unallocated_encoding(s);
8236 return;
8238 if (!fp_access_check(s)) {
8239 return;
8242 fpst = NULL;
8243 break;
8244 case 0xc: /* FMAXNMP */
8245 case 0xd: /* FADDP */
8246 case 0xf: /* FMAXP */
8247 case 0x2c: /* FMINNMP */
8248 case 0x2f: /* FMINP */
8249 /* FP op, size[0] is 32 or 64 bit*/
8250 if (!u) {
8251 if (!dc_isar_feature(aa64_fp16, s)) {
8252 unallocated_encoding(s);
8253 return;
8254 } else {
8255 size = MO_16;
8257 } else {
8258 size = extract32(size, 0, 1) ? MO_64 : MO_32;
8261 if (!fp_access_check(s)) {
8262 return;
8265 fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
8266 break;
8267 default:
8268 unallocated_encoding(s);
8269 return;
8272 if (size == MO_64) {
8273 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8274 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
8275 TCGv_i64 tcg_res = tcg_temp_new_i64();
8277 read_vec_element(s, tcg_op1, rn, 0, MO_64);
8278 read_vec_element(s, tcg_op2, rn, 1, MO_64);
8280 switch (opcode) {
8281 case 0x3b: /* ADDP */
8282 tcg_gen_add_i64(tcg_res, tcg_op1, tcg_op2);
8283 break;
8284 case 0xc: /* FMAXNMP */
8285 gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
8286 break;
8287 case 0xd: /* FADDP */
8288 gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
8289 break;
8290 case 0xf: /* FMAXP */
8291 gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
8292 break;
8293 case 0x2c: /* FMINNMP */
8294 gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
8295 break;
8296 case 0x2f: /* FMINP */
8297 gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
8298 break;
8299 default:
8300 g_assert_not_reached();
8303 write_fp_dreg(s, rd, tcg_res);
8305 tcg_temp_free_i64(tcg_op1);
8306 tcg_temp_free_i64(tcg_op2);
8307 tcg_temp_free_i64(tcg_res);
8308 } else {
8309 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
8310 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
8311 TCGv_i32 tcg_res = tcg_temp_new_i32();
8313 read_vec_element_i32(s, tcg_op1, rn, 0, size);
8314 read_vec_element_i32(s, tcg_op2, rn, 1, size);
8316 if (size == MO_16) {
8317 switch (opcode) {
8318 case 0xc: /* FMAXNMP */
8319 gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
8320 break;
8321 case 0xd: /* FADDP */
8322 gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
8323 break;
8324 case 0xf: /* FMAXP */
8325 gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
8326 break;
8327 case 0x2c: /* FMINNMP */
8328 gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
8329 break;
8330 case 0x2f: /* FMINP */
8331 gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
8332 break;
8333 default:
8334 g_assert_not_reached();
8336 } else {
8337 switch (opcode) {
8338 case 0xc: /* FMAXNMP */
8339 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
8340 break;
8341 case 0xd: /* FADDP */
8342 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
8343 break;
8344 case 0xf: /* FMAXP */
8345 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
8346 break;
8347 case 0x2c: /* FMINNMP */
8348 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
8349 break;
8350 case 0x2f: /* FMINP */
8351 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
8352 break;
8353 default:
8354 g_assert_not_reached();
8358 write_fp_sreg(s, rd, tcg_res);
8360 tcg_temp_free_i32(tcg_op1);
8361 tcg_temp_free_i32(tcg_op2);
8362 tcg_temp_free_i32(tcg_res);
8365 if (fpst) {
8366 tcg_temp_free_ptr(fpst);
8371 * Common SSHR[RA]/USHR[RA] - Shift right (optional rounding/accumulate)
8373 * This code is handles the common shifting code and is used by both
8374 * the vector and scalar code.
8376 static void handle_shri_with_rndacc(TCGv_i64 tcg_res, TCGv_i64 tcg_src,
8377 TCGv_i64 tcg_rnd, bool accumulate,
8378 bool is_u, int size, int shift)
8380 bool extended_result = false;
8381 bool round = tcg_rnd != NULL;
8382 int ext_lshift = 0;
8383 TCGv_i64 tcg_src_hi;
8385 if (round && size == 3) {
8386 extended_result = true;
8387 ext_lshift = 64 - shift;
8388 tcg_src_hi = tcg_temp_new_i64();
8389 } else if (shift == 64) {
8390 if (!accumulate && is_u) {
8391 /* result is zero */
8392 tcg_gen_movi_i64(tcg_res, 0);
8393 return;
8397 /* Deal with the rounding step */
8398 if (round) {
8399 if (extended_result) {
8400 TCGv_i64 tcg_zero = tcg_constant_i64(0);
8401 if (!is_u) {
8402 /* take care of sign extending tcg_res */
8403 tcg_gen_sari_i64(tcg_src_hi, tcg_src, 63);
8404 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
8405 tcg_src, tcg_src_hi,
8406 tcg_rnd, tcg_zero);
8407 } else {
8408 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
8409 tcg_src, tcg_zero,
8410 tcg_rnd, tcg_zero);
8412 } else {
8413 tcg_gen_add_i64(tcg_src, tcg_src, tcg_rnd);
8417 /* Now do the shift right */
8418 if (round && extended_result) {
8419 /* extended case, >64 bit precision required */
8420 if (ext_lshift == 0) {
8421 /* special case, only high bits matter */
8422 tcg_gen_mov_i64(tcg_src, tcg_src_hi);
8423 } else {
8424 tcg_gen_shri_i64(tcg_src, tcg_src, shift);
8425 tcg_gen_shli_i64(tcg_src_hi, tcg_src_hi, ext_lshift);
8426 tcg_gen_or_i64(tcg_src, tcg_src, tcg_src_hi);
8428 } else {
8429 if (is_u) {
8430 if (shift == 64) {
8431 /* essentially shifting in 64 zeros */
8432 tcg_gen_movi_i64(tcg_src, 0);
8433 } else {
8434 tcg_gen_shri_i64(tcg_src, tcg_src, shift);
8436 } else {
8437 if (shift == 64) {
8438 /* effectively extending the sign-bit */
8439 tcg_gen_sari_i64(tcg_src, tcg_src, 63);
8440 } else {
8441 tcg_gen_sari_i64(tcg_src, tcg_src, shift);
8446 if (accumulate) {
8447 tcg_gen_add_i64(tcg_res, tcg_res, tcg_src);
8448 } else {
8449 tcg_gen_mov_i64(tcg_res, tcg_src);
8452 if (extended_result) {
8453 tcg_temp_free_i64(tcg_src_hi);
8457 /* SSHR[RA]/USHR[RA] - Scalar shift right (optional rounding/accumulate) */
8458 static void handle_scalar_simd_shri(DisasContext *s,
8459 bool is_u, int immh, int immb,
8460 int opcode, int rn, int rd)
8462 const int size = 3;
8463 int immhb = immh << 3 | immb;
8464 int shift = 2 * (8 << size) - immhb;
8465 bool accumulate = false;
8466 bool round = false;
8467 bool insert = false;
8468 TCGv_i64 tcg_rn;
8469 TCGv_i64 tcg_rd;
8470 TCGv_i64 tcg_round;
8472 if (!extract32(immh, 3, 1)) {
8473 unallocated_encoding(s);
8474 return;
8477 if (!fp_access_check(s)) {
8478 return;
8481 switch (opcode) {
8482 case 0x02: /* SSRA / USRA (accumulate) */
8483 accumulate = true;
8484 break;
8485 case 0x04: /* SRSHR / URSHR (rounding) */
8486 round = true;
8487 break;
8488 case 0x06: /* SRSRA / URSRA (accum + rounding) */
8489 accumulate = round = true;
8490 break;
8491 case 0x08: /* SRI */
8492 insert = true;
8493 break;
8496 if (round) {
8497 tcg_round = tcg_constant_i64(1ULL << (shift - 1));
8498 } else {
8499 tcg_round = NULL;
8502 tcg_rn = read_fp_dreg(s, rn);
8503 tcg_rd = (accumulate || insert) ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
8505 if (insert) {
8506 /* shift count same as element size is valid but does nothing;
8507 * special case to avoid potential shift by 64.
8509 int esize = 8 << size;
8510 if (shift != esize) {
8511 tcg_gen_shri_i64(tcg_rn, tcg_rn, shift);
8512 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, 0, esize - shift);
8514 } else {
8515 handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
8516 accumulate, is_u, size, shift);
8519 write_fp_dreg(s, rd, tcg_rd);
8521 tcg_temp_free_i64(tcg_rn);
8522 tcg_temp_free_i64(tcg_rd);
8525 /* SHL/SLI - Scalar shift left */
8526 static void handle_scalar_simd_shli(DisasContext *s, bool insert,
8527 int immh, int immb, int opcode,
8528 int rn, int rd)
8530 int size = 32 - clz32(immh) - 1;
8531 int immhb = immh << 3 | immb;
8532 int shift = immhb - (8 << size);
8533 TCGv_i64 tcg_rn;
8534 TCGv_i64 tcg_rd;
8536 if (!extract32(immh, 3, 1)) {
8537 unallocated_encoding(s);
8538 return;
8541 if (!fp_access_check(s)) {
8542 return;
8545 tcg_rn = read_fp_dreg(s, rn);
8546 tcg_rd = insert ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
8548 if (insert) {
8549 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, shift, 64 - shift);
8550 } else {
8551 tcg_gen_shli_i64(tcg_rd, tcg_rn, shift);
8554 write_fp_dreg(s, rd, tcg_rd);
8556 tcg_temp_free_i64(tcg_rn);
8557 tcg_temp_free_i64(tcg_rd);
8560 /* SQSHRN/SQSHRUN - Saturating (signed/unsigned) shift right with
8561 * (signed/unsigned) narrowing */
8562 static void handle_vec_simd_sqshrn(DisasContext *s, bool is_scalar, bool is_q,
8563 bool is_u_shift, bool is_u_narrow,
8564 int immh, int immb, int opcode,
8565 int rn, int rd)
8567 int immhb = immh << 3 | immb;
8568 int size = 32 - clz32(immh) - 1;
8569 int esize = 8 << size;
8570 int shift = (2 * esize) - immhb;
8571 int elements = is_scalar ? 1 : (64 / esize);
8572 bool round = extract32(opcode, 0, 1);
8573 MemOp ldop = (size + 1) | (is_u_shift ? 0 : MO_SIGN);
8574 TCGv_i64 tcg_rn, tcg_rd, tcg_round;
8575 TCGv_i32 tcg_rd_narrowed;
8576 TCGv_i64 tcg_final;
8578 static NeonGenNarrowEnvFn * const signed_narrow_fns[4][2] = {
8579 { gen_helper_neon_narrow_sat_s8,
8580 gen_helper_neon_unarrow_sat8 },
8581 { gen_helper_neon_narrow_sat_s16,
8582 gen_helper_neon_unarrow_sat16 },
8583 { gen_helper_neon_narrow_sat_s32,
8584 gen_helper_neon_unarrow_sat32 },
8585 { NULL, NULL },
8587 static NeonGenNarrowEnvFn * const unsigned_narrow_fns[4] = {
8588 gen_helper_neon_narrow_sat_u8,
8589 gen_helper_neon_narrow_sat_u16,
8590 gen_helper_neon_narrow_sat_u32,
8591 NULL
8593 NeonGenNarrowEnvFn *narrowfn;
8595 int i;
8597 assert(size < 4);
8599 if (extract32(immh, 3, 1)) {
8600 unallocated_encoding(s);
8601 return;
8604 if (!fp_access_check(s)) {
8605 return;
8608 if (is_u_shift) {
8609 narrowfn = unsigned_narrow_fns[size];
8610 } else {
8611 narrowfn = signed_narrow_fns[size][is_u_narrow ? 1 : 0];
8614 tcg_rn = tcg_temp_new_i64();
8615 tcg_rd = tcg_temp_new_i64();
8616 tcg_rd_narrowed = tcg_temp_new_i32();
8617 tcg_final = tcg_const_i64(0);
8619 if (round) {
8620 tcg_round = tcg_constant_i64(1ULL << (shift - 1));
8621 } else {
8622 tcg_round = NULL;
8625 for (i = 0; i < elements; i++) {
8626 read_vec_element(s, tcg_rn, rn, i, ldop);
8627 handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
8628 false, is_u_shift, size+1, shift);
8629 narrowfn(tcg_rd_narrowed, cpu_env, tcg_rd);
8630 tcg_gen_extu_i32_i64(tcg_rd, tcg_rd_narrowed);
8631 tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
8634 if (!is_q) {
8635 write_vec_element(s, tcg_final, rd, 0, MO_64);
8636 } else {
8637 write_vec_element(s, tcg_final, rd, 1, MO_64);
8640 tcg_temp_free_i64(tcg_rn);
8641 tcg_temp_free_i64(tcg_rd);
8642 tcg_temp_free_i32(tcg_rd_narrowed);
8643 tcg_temp_free_i64(tcg_final);
8645 clear_vec_high(s, is_q, rd);
8648 /* SQSHLU, UQSHL, SQSHL: saturating left shifts */
8649 static void handle_simd_qshl(DisasContext *s, bool scalar, bool is_q,
8650 bool src_unsigned, bool dst_unsigned,
8651 int immh, int immb, int rn, int rd)
8653 int immhb = immh << 3 | immb;
8654 int size = 32 - clz32(immh) - 1;
8655 int shift = immhb - (8 << size);
8656 int pass;
8658 assert(immh != 0);
8659 assert(!(scalar && is_q));
8661 if (!scalar) {
8662 if (!is_q && extract32(immh, 3, 1)) {
8663 unallocated_encoding(s);
8664 return;
8667 /* Since we use the variable-shift helpers we must
8668 * replicate the shift count into each element of
8669 * the tcg_shift value.
8671 switch (size) {
8672 case 0:
8673 shift |= shift << 8;
8674 /* fall through */
8675 case 1:
8676 shift |= shift << 16;
8677 break;
8678 case 2:
8679 case 3:
8680 break;
8681 default:
8682 g_assert_not_reached();
8686 if (!fp_access_check(s)) {
8687 return;
8690 if (size == 3) {
8691 TCGv_i64 tcg_shift = tcg_constant_i64(shift);
8692 static NeonGenTwo64OpEnvFn * const fns[2][2] = {
8693 { gen_helper_neon_qshl_s64, gen_helper_neon_qshlu_s64 },
8694 { NULL, gen_helper_neon_qshl_u64 },
8696 NeonGenTwo64OpEnvFn *genfn = fns[src_unsigned][dst_unsigned];
8697 int maxpass = is_q ? 2 : 1;
8699 for (pass = 0; pass < maxpass; pass++) {
8700 TCGv_i64 tcg_op = tcg_temp_new_i64();
8702 read_vec_element(s, tcg_op, rn, pass, MO_64);
8703 genfn(tcg_op, cpu_env, tcg_op, tcg_shift);
8704 write_vec_element(s, tcg_op, rd, pass, MO_64);
8706 tcg_temp_free_i64(tcg_op);
8708 clear_vec_high(s, is_q, rd);
8709 } else {
8710 TCGv_i32 tcg_shift = tcg_constant_i32(shift);
8711 static NeonGenTwoOpEnvFn * const fns[2][2][3] = {
8713 { gen_helper_neon_qshl_s8,
8714 gen_helper_neon_qshl_s16,
8715 gen_helper_neon_qshl_s32 },
8716 { gen_helper_neon_qshlu_s8,
8717 gen_helper_neon_qshlu_s16,
8718 gen_helper_neon_qshlu_s32 }
8719 }, {
8720 { NULL, NULL, NULL },
8721 { gen_helper_neon_qshl_u8,
8722 gen_helper_neon_qshl_u16,
8723 gen_helper_neon_qshl_u32 }
8726 NeonGenTwoOpEnvFn *genfn = fns[src_unsigned][dst_unsigned][size];
8727 MemOp memop = scalar ? size : MO_32;
8728 int maxpass = scalar ? 1 : is_q ? 4 : 2;
8730 for (pass = 0; pass < maxpass; pass++) {
8731 TCGv_i32 tcg_op = tcg_temp_new_i32();
8733 read_vec_element_i32(s, tcg_op, rn, pass, memop);
8734 genfn(tcg_op, cpu_env, tcg_op, tcg_shift);
8735 if (scalar) {
8736 switch (size) {
8737 case 0:
8738 tcg_gen_ext8u_i32(tcg_op, tcg_op);
8739 break;
8740 case 1:
8741 tcg_gen_ext16u_i32(tcg_op, tcg_op);
8742 break;
8743 case 2:
8744 break;
8745 default:
8746 g_assert_not_reached();
8748 write_fp_sreg(s, rd, tcg_op);
8749 } else {
8750 write_vec_element_i32(s, tcg_op, rd, pass, MO_32);
8753 tcg_temp_free_i32(tcg_op);
8756 if (!scalar) {
8757 clear_vec_high(s, is_q, rd);
8762 /* Common vector code for handling integer to FP conversion */
8763 static void handle_simd_intfp_conv(DisasContext *s, int rd, int rn,
8764 int elements, int is_signed,
8765 int fracbits, int size)
8767 TCGv_ptr tcg_fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
8768 TCGv_i32 tcg_shift = NULL;
8770 MemOp mop = size | (is_signed ? MO_SIGN : 0);
8771 int pass;
8773 if (fracbits || size == MO_64) {
8774 tcg_shift = tcg_constant_i32(fracbits);
8777 if (size == MO_64) {
8778 TCGv_i64 tcg_int64 = tcg_temp_new_i64();
8779 TCGv_i64 tcg_double = tcg_temp_new_i64();
8781 for (pass = 0; pass < elements; pass++) {
8782 read_vec_element(s, tcg_int64, rn, pass, mop);
8784 if (is_signed) {
8785 gen_helper_vfp_sqtod(tcg_double, tcg_int64,
8786 tcg_shift, tcg_fpst);
8787 } else {
8788 gen_helper_vfp_uqtod(tcg_double, tcg_int64,
8789 tcg_shift, tcg_fpst);
8791 if (elements == 1) {
8792 write_fp_dreg(s, rd, tcg_double);
8793 } else {
8794 write_vec_element(s, tcg_double, rd, pass, MO_64);
8798 tcg_temp_free_i64(tcg_int64);
8799 tcg_temp_free_i64(tcg_double);
8801 } else {
8802 TCGv_i32 tcg_int32 = tcg_temp_new_i32();
8803 TCGv_i32 tcg_float = tcg_temp_new_i32();
8805 for (pass = 0; pass < elements; pass++) {
8806 read_vec_element_i32(s, tcg_int32, rn, pass, mop);
8808 switch (size) {
8809 case MO_32:
8810 if (fracbits) {
8811 if (is_signed) {
8812 gen_helper_vfp_sltos(tcg_float, tcg_int32,
8813 tcg_shift, tcg_fpst);
8814 } else {
8815 gen_helper_vfp_ultos(tcg_float, tcg_int32,
8816 tcg_shift, tcg_fpst);
8818 } else {
8819 if (is_signed) {
8820 gen_helper_vfp_sitos(tcg_float, tcg_int32, tcg_fpst);
8821 } else {
8822 gen_helper_vfp_uitos(tcg_float, tcg_int32, tcg_fpst);
8825 break;
8826 case MO_16:
8827 if (fracbits) {
8828 if (is_signed) {
8829 gen_helper_vfp_sltoh(tcg_float, tcg_int32,
8830 tcg_shift, tcg_fpst);
8831 } else {
8832 gen_helper_vfp_ultoh(tcg_float, tcg_int32,
8833 tcg_shift, tcg_fpst);
8835 } else {
8836 if (is_signed) {
8837 gen_helper_vfp_sitoh(tcg_float, tcg_int32, tcg_fpst);
8838 } else {
8839 gen_helper_vfp_uitoh(tcg_float, tcg_int32, tcg_fpst);
8842 break;
8843 default:
8844 g_assert_not_reached();
8847 if (elements == 1) {
8848 write_fp_sreg(s, rd, tcg_float);
8849 } else {
8850 write_vec_element_i32(s, tcg_float, rd, pass, size);
8854 tcg_temp_free_i32(tcg_int32);
8855 tcg_temp_free_i32(tcg_float);
8858 tcg_temp_free_ptr(tcg_fpst);
8860 clear_vec_high(s, elements << size == 16, rd);
8863 /* UCVTF/SCVTF - Integer to FP conversion */
8864 static void handle_simd_shift_intfp_conv(DisasContext *s, bool is_scalar,
8865 bool is_q, bool is_u,
8866 int immh, int immb, int opcode,
8867 int rn, int rd)
8869 int size, elements, fracbits;
8870 int immhb = immh << 3 | immb;
8872 if (immh & 8) {
8873 size = MO_64;
8874 if (!is_scalar && !is_q) {
8875 unallocated_encoding(s);
8876 return;
8878 } else if (immh & 4) {
8879 size = MO_32;
8880 } else if (immh & 2) {
8881 size = MO_16;
8882 if (!dc_isar_feature(aa64_fp16, s)) {
8883 unallocated_encoding(s);
8884 return;
8886 } else {
8887 /* immh == 0 would be a failure of the decode logic */
8888 g_assert(immh == 1);
8889 unallocated_encoding(s);
8890 return;
8893 if (is_scalar) {
8894 elements = 1;
8895 } else {
8896 elements = (8 << is_q) >> size;
8898 fracbits = (16 << size) - immhb;
8900 if (!fp_access_check(s)) {
8901 return;
8904 handle_simd_intfp_conv(s, rd, rn, elements, !is_u, fracbits, size);
8907 /* FCVTZS, FVCVTZU - FP to fixedpoint conversion */
8908 static void handle_simd_shift_fpint_conv(DisasContext *s, bool is_scalar,
8909 bool is_q, bool is_u,
8910 int immh, int immb, int rn, int rd)
8912 int immhb = immh << 3 | immb;
8913 int pass, size, fracbits;
8914 TCGv_ptr tcg_fpstatus;
8915 TCGv_i32 tcg_rmode, tcg_shift;
8917 if (immh & 0x8) {
8918 size = MO_64;
8919 if (!is_scalar && !is_q) {
8920 unallocated_encoding(s);
8921 return;
8923 } else if (immh & 0x4) {
8924 size = MO_32;
8925 } else if (immh & 0x2) {
8926 size = MO_16;
8927 if (!dc_isar_feature(aa64_fp16, s)) {
8928 unallocated_encoding(s);
8929 return;
8931 } else {
8932 /* Should have split out AdvSIMD modified immediate earlier. */
8933 assert(immh == 1);
8934 unallocated_encoding(s);
8935 return;
8938 if (!fp_access_check(s)) {
8939 return;
8942 assert(!(is_scalar && is_q));
8944 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(FPROUNDING_ZERO));
8945 tcg_fpstatus = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
8946 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
8947 fracbits = (16 << size) - immhb;
8948 tcg_shift = tcg_constant_i32(fracbits);
8950 if (size == MO_64) {
8951 int maxpass = is_scalar ? 1 : 2;
8953 for (pass = 0; pass < maxpass; pass++) {
8954 TCGv_i64 tcg_op = tcg_temp_new_i64();
8956 read_vec_element(s, tcg_op, rn, pass, MO_64);
8957 if (is_u) {
8958 gen_helper_vfp_touqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
8959 } else {
8960 gen_helper_vfp_tosqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
8962 write_vec_element(s, tcg_op, rd, pass, MO_64);
8963 tcg_temp_free_i64(tcg_op);
8965 clear_vec_high(s, is_q, rd);
8966 } else {
8967 void (*fn)(TCGv_i32, TCGv_i32, TCGv_i32, TCGv_ptr);
8968 int maxpass = is_scalar ? 1 : ((8 << is_q) >> size);
8970 switch (size) {
8971 case MO_16:
8972 if (is_u) {
8973 fn = gen_helper_vfp_touhh;
8974 } else {
8975 fn = gen_helper_vfp_toshh;
8977 break;
8978 case MO_32:
8979 if (is_u) {
8980 fn = gen_helper_vfp_touls;
8981 } else {
8982 fn = gen_helper_vfp_tosls;
8984 break;
8985 default:
8986 g_assert_not_reached();
8989 for (pass = 0; pass < maxpass; pass++) {
8990 TCGv_i32 tcg_op = tcg_temp_new_i32();
8992 read_vec_element_i32(s, tcg_op, rn, pass, size);
8993 fn(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
8994 if (is_scalar) {
8995 write_fp_sreg(s, rd, tcg_op);
8996 } else {
8997 write_vec_element_i32(s, tcg_op, rd, pass, size);
8999 tcg_temp_free_i32(tcg_op);
9001 if (!is_scalar) {
9002 clear_vec_high(s, is_q, rd);
9006 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
9007 tcg_temp_free_ptr(tcg_fpstatus);
9008 tcg_temp_free_i32(tcg_rmode);
9011 /* AdvSIMD scalar shift by immediate
9012 * 31 30 29 28 23 22 19 18 16 15 11 10 9 5 4 0
9013 * +-----+---+-------------+------+------+--------+---+------+------+
9014 * | 0 1 | U | 1 1 1 1 1 0 | immh | immb | opcode | 1 | Rn | Rd |
9015 * +-----+---+-------------+------+------+--------+---+------+------+
9017 * This is the scalar version so it works on a fixed sized registers
9019 static void disas_simd_scalar_shift_imm(DisasContext *s, uint32_t insn)
9021 int rd = extract32(insn, 0, 5);
9022 int rn = extract32(insn, 5, 5);
9023 int opcode = extract32(insn, 11, 5);
9024 int immb = extract32(insn, 16, 3);
9025 int immh = extract32(insn, 19, 4);
9026 bool is_u = extract32(insn, 29, 1);
9028 if (immh == 0) {
9029 unallocated_encoding(s);
9030 return;
9033 switch (opcode) {
9034 case 0x08: /* SRI */
9035 if (!is_u) {
9036 unallocated_encoding(s);
9037 return;
9039 /* fall through */
9040 case 0x00: /* SSHR / USHR */
9041 case 0x02: /* SSRA / USRA */
9042 case 0x04: /* SRSHR / URSHR */
9043 case 0x06: /* SRSRA / URSRA */
9044 handle_scalar_simd_shri(s, is_u, immh, immb, opcode, rn, rd);
9045 break;
9046 case 0x0a: /* SHL / SLI */
9047 handle_scalar_simd_shli(s, is_u, immh, immb, opcode, rn, rd);
9048 break;
9049 case 0x1c: /* SCVTF, UCVTF */
9050 handle_simd_shift_intfp_conv(s, true, false, is_u, immh, immb,
9051 opcode, rn, rd);
9052 break;
9053 case 0x10: /* SQSHRUN, SQSHRUN2 */
9054 case 0x11: /* SQRSHRUN, SQRSHRUN2 */
9055 if (!is_u) {
9056 unallocated_encoding(s);
9057 return;
9059 handle_vec_simd_sqshrn(s, true, false, false, true,
9060 immh, immb, opcode, rn, rd);
9061 break;
9062 case 0x12: /* SQSHRN, SQSHRN2, UQSHRN */
9063 case 0x13: /* SQRSHRN, SQRSHRN2, UQRSHRN, UQRSHRN2 */
9064 handle_vec_simd_sqshrn(s, true, false, is_u, is_u,
9065 immh, immb, opcode, rn, rd);
9066 break;
9067 case 0xc: /* SQSHLU */
9068 if (!is_u) {
9069 unallocated_encoding(s);
9070 return;
9072 handle_simd_qshl(s, true, false, false, true, immh, immb, rn, rd);
9073 break;
9074 case 0xe: /* SQSHL, UQSHL */
9075 handle_simd_qshl(s, true, false, is_u, is_u, immh, immb, rn, rd);
9076 break;
9077 case 0x1f: /* FCVTZS, FCVTZU */
9078 handle_simd_shift_fpint_conv(s, true, false, is_u, immh, immb, rn, rd);
9079 break;
9080 default:
9081 unallocated_encoding(s);
9082 break;
9086 /* AdvSIMD scalar three different
9087 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 0
9088 * +-----+---+-----------+------+---+------+--------+-----+------+------+
9089 * | 0 1 | U | 1 1 1 1 0 | size | 1 | Rm | opcode | 0 0 | Rn | Rd |
9090 * +-----+---+-----------+------+---+------+--------+-----+------+------+
9092 static void disas_simd_scalar_three_reg_diff(DisasContext *s, uint32_t insn)
9094 bool is_u = extract32(insn, 29, 1);
9095 int size = extract32(insn, 22, 2);
9096 int opcode = extract32(insn, 12, 4);
9097 int rm = extract32(insn, 16, 5);
9098 int rn = extract32(insn, 5, 5);
9099 int rd = extract32(insn, 0, 5);
9101 if (is_u) {
9102 unallocated_encoding(s);
9103 return;
9106 switch (opcode) {
9107 case 0x9: /* SQDMLAL, SQDMLAL2 */
9108 case 0xb: /* SQDMLSL, SQDMLSL2 */
9109 case 0xd: /* SQDMULL, SQDMULL2 */
9110 if (size == 0 || size == 3) {
9111 unallocated_encoding(s);
9112 return;
9114 break;
9115 default:
9116 unallocated_encoding(s);
9117 return;
9120 if (!fp_access_check(s)) {
9121 return;
9124 if (size == 2) {
9125 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
9126 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
9127 TCGv_i64 tcg_res = tcg_temp_new_i64();
9129 read_vec_element(s, tcg_op1, rn, 0, MO_32 | MO_SIGN);
9130 read_vec_element(s, tcg_op2, rm, 0, MO_32 | MO_SIGN);
9132 tcg_gen_mul_i64(tcg_res, tcg_op1, tcg_op2);
9133 gen_helper_neon_addl_saturate_s64(tcg_res, cpu_env, tcg_res, tcg_res);
9135 switch (opcode) {
9136 case 0xd: /* SQDMULL, SQDMULL2 */
9137 break;
9138 case 0xb: /* SQDMLSL, SQDMLSL2 */
9139 tcg_gen_neg_i64(tcg_res, tcg_res);
9140 /* fall through */
9141 case 0x9: /* SQDMLAL, SQDMLAL2 */
9142 read_vec_element(s, tcg_op1, rd, 0, MO_64);
9143 gen_helper_neon_addl_saturate_s64(tcg_res, cpu_env,
9144 tcg_res, tcg_op1);
9145 break;
9146 default:
9147 g_assert_not_reached();
9150 write_fp_dreg(s, rd, tcg_res);
9152 tcg_temp_free_i64(tcg_op1);
9153 tcg_temp_free_i64(tcg_op2);
9154 tcg_temp_free_i64(tcg_res);
9155 } else {
9156 TCGv_i32 tcg_op1 = read_fp_hreg(s, rn);
9157 TCGv_i32 tcg_op2 = read_fp_hreg(s, rm);
9158 TCGv_i64 tcg_res = tcg_temp_new_i64();
9160 gen_helper_neon_mull_s16(tcg_res, tcg_op1, tcg_op2);
9161 gen_helper_neon_addl_saturate_s32(tcg_res, cpu_env, tcg_res, tcg_res);
9163 switch (opcode) {
9164 case 0xd: /* SQDMULL, SQDMULL2 */
9165 break;
9166 case 0xb: /* SQDMLSL, SQDMLSL2 */
9167 gen_helper_neon_negl_u32(tcg_res, tcg_res);
9168 /* fall through */
9169 case 0x9: /* SQDMLAL, SQDMLAL2 */
9171 TCGv_i64 tcg_op3 = tcg_temp_new_i64();
9172 read_vec_element(s, tcg_op3, rd, 0, MO_32);
9173 gen_helper_neon_addl_saturate_s32(tcg_res, cpu_env,
9174 tcg_res, tcg_op3);
9175 tcg_temp_free_i64(tcg_op3);
9176 break;
9178 default:
9179 g_assert_not_reached();
9182 tcg_gen_ext32u_i64(tcg_res, tcg_res);
9183 write_fp_dreg(s, rd, tcg_res);
9185 tcg_temp_free_i32(tcg_op1);
9186 tcg_temp_free_i32(tcg_op2);
9187 tcg_temp_free_i64(tcg_res);
9191 static void handle_3same_64(DisasContext *s, int opcode, bool u,
9192 TCGv_i64 tcg_rd, TCGv_i64 tcg_rn, TCGv_i64 tcg_rm)
9194 /* Handle 64x64->64 opcodes which are shared between the scalar
9195 * and vector 3-same groups. We cover every opcode where size == 3
9196 * is valid in either the three-reg-same (integer, not pairwise)
9197 * or scalar-three-reg-same groups.
9199 TCGCond cond;
9201 switch (opcode) {
9202 case 0x1: /* SQADD */
9203 if (u) {
9204 gen_helper_neon_qadd_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9205 } else {
9206 gen_helper_neon_qadd_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9208 break;
9209 case 0x5: /* SQSUB */
9210 if (u) {
9211 gen_helper_neon_qsub_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9212 } else {
9213 gen_helper_neon_qsub_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9215 break;
9216 case 0x6: /* CMGT, CMHI */
9217 /* 64 bit integer comparison, result = test ? (2^64 - 1) : 0.
9218 * We implement this using setcond (test) and then negating.
9220 cond = u ? TCG_COND_GTU : TCG_COND_GT;
9221 do_cmop:
9222 tcg_gen_setcond_i64(cond, tcg_rd, tcg_rn, tcg_rm);
9223 tcg_gen_neg_i64(tcg_rd, tcg_rd);
9224 break;
9225 case 0x7: /* CMGE, CMHS */
9226 cond = u ? TCG_COND_GEU : TCG_COND_GE;
9227 goto do_cmop;
9228 case 0x11: /* CMTST, CMEQ */
9229 if (u) {
9230 cond = TCG_COND_EQ;
9231 goto do_cmop;
9233 gen_cmtst_i64(tcg_rd, tcg_rn, tcg_rm);
9234 break;
9235 case 0x8: /* SSHL, USHL */
9236 if (u) {
9237 gen_ushl_i64(tcg_rd, tcg_rn, tcg_rm);
9238 } else {
9239 gen_sshl_i64(tcg_rd, tcg_rn, tcg_rm);
9241 break;
9242 case 0x9: /* SQSHL, UQSHL */
9243 if (u) {
9244 gen_helper_neon_qshl_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9245 } else {
9246 gen_helper_neon_qshl_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9248 break;
9249 case 0xa: /* SRSHL, URSHL */
9250 if (u) {
9251 gen_helper_neon_rshl_u64(tcg_rd, tcg_rn, tcg_rm);
9252 } else {
9253 gen_helper_neon_rshl_s64(tcg_rd, tcg_rn, tcg_rm);
9255 break;
9256 case 0xb: /* SQRSHL, UQRSHL */
9257 if (u) {
9258 gen_helper_neon_qrshl_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9259 } else {
9260 gen_helper_neon_qrshl_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9262 break;
9263 case 0x10: /* ADD, SUB */
9264 if (u) {
9265 tcg_gen_sub_i64(tcg_rd, tcg_rn, tcg_rm);
9266 } else {
9267 tcg_gen_add_i64(tcg_rd, tcg_rn, tcg_rm);
9269 break;
9270 default:
9271 g_assert_not_reached();
9275 /* Handle the 3-same-operands float operations; shared by the scalar
9276 * and vector encodings. The caller must filter out any encodings
9277 * not allocated for the encoding it is dealing with.
9279 static void handle_3same_float(DisasContext *s, int size, int elements,
9280 int fpopcode, int rd, int rn, int rm)
9282 int pass;
9283 TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
9285 for (pass = 0; pass < elements; pass++) {
9286 if (size) {
9287 /* Double */
9288 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
9289 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
9290 TCGv_i64 tcg_res = tcg_temp_new_i64();
9292 read_vec_element(s, tcg_op1, rn, pass, MO_64);
9293 read_vec_element(s, tcg_op2, rm, pass, MO_64);
9295 switch (fpopcode) {
9296 case 0x39: /* FMLS */
9297 /* As usual for ARM, separate negation for fused multiply-add */
9298 gen_helper_vfp_negd(tcg_op1, tcg_op1);
9299 /* fall through */
9300 case 0x19: /* FMLA */
9301 read_vec_element(s, tcg_res, rd, pass, MO_64);
9302 gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2,
9303 tcg_res, fpst);
9304 break;
9305 case 0x18: /* FMAXNM */
9306 gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
9307 break;
9308 case 0x1a: /* FADD */
9309 gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
9310 break;
9311 case 0x1b: /* FMULX */
9312 gen_helper_vfp_mulxd(tcg_res, tcg_op1, tcg_op2, fpst);
9313 break;
9314 case 0x1c: /* FCMEQ */
9315 gen_helper_neon_ceq_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9316 break;
9317 case 0x1e: /* FMAX */
9318 gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
9319 break;
9320 case 0x1f: /* FRECPS */
9321 gen_helper_recpsf_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9322 break;
9323 case 0x38: /* FMINNM */
9324 gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
9325 break;
9326 case 0x3a: /* FSUB */
9327 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
9328 break;
9329 case 0x3e: /* FMIN */
9330 gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
9331 break;
9332 case 0x3f: /* FRSQRTS */
9333 gen_helper_rsqrtsf_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9334 break;
9335 case 0x5b: /* FMUL */
9336 gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
9337 break;
9338 case 0x5c: /* FCMGE */
9339 gen_helper_neon_cge_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9340 break;
9341 case 0x5d: /* FACGE */
9342 gen_helper_neon_acge_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9343 break;
9344 case 0x5f: /* FDIV */
9345 gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
9346 break;
9347 case 0x7a: /* FABD */
9348 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
9349 gen_helper_vfp_absd(tcg_res, tcg_res);
9350 break;
9351 case 0x7c: /* FCMGT */
9352 gen_helper_neon_cgt_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9353 break;
9354 case 0x7d: /* FACGT */
9355 gen_helper_neon_acgt_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9356 break;
9357 default:
9358 g_assert_not_reached();
9361 write_vec_element(s, tcg_res, rd, pass, MO_64);
9363 tcg_temp_free_i64(tcg_res);
9364 tcg_temp_free_i64(tcg_op1);
9365 tcg_temp_free_i64(tcg_op2);
9366 } else {
9367 /* Single */
9368 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
9369 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
9370 TCGv_i32 tcg_res = tcg_temp_new_i32();
9372 read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
9373 read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
9375 switch (fpopcode) {
9376 case 0x39: /* FMLS */
9377 /* As usual for ARM, separate negation for fused multiply-add */
9378 gen_helper_vfp_negs(tcg_op1, tcg_op1);
9379 /* fall through */
9380 case 0x19: /* FMLA */
9381 read_vec_element_i32(s, tcg_res, rd, pass, MO_32);
9382 gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2,
9383 tcg_res, fpst);
9384 break;
9385 case 0x1a: /* FADD */
9386 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
9387 break;
9388 case 0x1b: /* FMULX */
9389 gen_helper_vfp_mulxs(tcg_res, tcg_op1, tcg_op2, fpst);
9390 break;
9391 case 0x1c: /* FCMEQ */
9392 gen_helper_neon_ceq_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9393 break;
9394 case 0x1e: /* FMAX */
9395 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
9396 break;
9397 case 0x1f: /* FRECPS */
9398 gen_helper_recpsf_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9399 break;
9400 case 0x18: /* FMAXNM */
9401 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
9402 break;
9403 case 0x38: /* FMINNM */
9404 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
9405 break;
9406 case 0x3a: /* FSUB */
9407 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
9408 break;
9409 case 0x3e: /* FMIN */
9410 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
9411 break;
9412 case 0x3f: /* FRSQRTS */
9413 gen_helper_rsqrtsf_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9414 break;
9415 case 0x5b: /* FMUL */
9416 gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
9417 break;
9418 case 0x5c: /* FCMGE */
9419 gen_helper_neon_cge_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9420 break;
9421 case 0x5d: /* FACGE */
9422 gen_helper_neon_acge_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9423 break;
9424 case 0x5f: /* FDIV */
9425 gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
9426 break;
9427 case 0x7a: /* FABD */
9428 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
9429 gen_helper_vfp_abss(tcg_res, tcg_res);
9430 break;
9431 case 0x7c: /* FCMGT */
9432 gen_helper_neon_cgt_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9433 break;
9434 case 0x7d: /* FACGT */
9435 gen_helper_neon_acgt_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9436 break;
9437 default:
9438 g_assert_not_reached();
9441 if (elements == 1) {
9442 /* scalar single so clear high part */
9443 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
9445 tcg_gen_extu_i32_i64(tcg_tmp, tcg_res);
9446 write_vec_element(s, tcg_tmp, rd, pass, MO_64);
9447 tcg_temp_free_i64(tcg_tmp);
9448 } else {
9449 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
9452 tcg_temp_free_i32(tcg_res);
9453 tcg_temp_free_i32(tcg_op1);
9454 tcg_temp_free_i32(tcg_op2);
9458 tcg_temp_free_ptr(fpst);
9460 clear_vec_high(s, elements * (size ? 8 : 4) > 8, rd);
9463 /* AdvSIMD scalar three same
9464 * 31 30 29 28 24 23 22 21 20 16 15 11 10 9 5 4 0
9465 * +-----+---+-----------+------+---+------+--------+---+------+------+
9466 * | 0 1 | U | 1 1 1 1 0 | size | 1 | Rm | opcode | 1 | Rn | Rd |
9467 * +-----+---+-----------+------+---+------+--------+---+------+------+
9469 static void disas_simd_scalar_three_reg_same(DisasContext *s, uint32_t insn)
9471 int rd = extract32(insn, 0, 5);
9472 int rn = extract32(insn, 5, 5);
9473 int opcode = extract32(insn, 11, 5);
9474 int rm = extract32(insn, 16, 5);
9475 int size = extract32(insn, 22, 2);
9476 bool u = extract32(insn, 29, 1);
9477 TCGv_i64 tcg_rd;
9479 if (opcode >= 0x18) {
9480 /* Floating point: U, size[1] and opcode indicate operation */
9481 int fpopcode = opcode | (extract32(size, 1, 1) << 5) | (u << 6);
9482 switch (fpopcode) {
9483 case 0x1b: /* FMULX */
9484 case 0x1f: /* FRECPS */
9485 case 0x3f: /* FRSQRTS */
9486 case 0x5d: /* FACGE */
9487 case 0x7d: /* FACGT */
9488 case 0x1c: /* FCMEQ */
9489 case 0x5c: /* FCMGE */
9490 case 0x7c: /* FCMGT */
9491 case 0x7a: /* FABD */
9492 break;
9493 default:
9494 unallocated_encoding(s);
9495 return;
9498 if (!fp_access_check(s)) {
9499 return;
9502 handle_3same_float(s, extract32(size, 0, 1), 1, fpopcode, rd, rn, rm);
9503 return;
9506 switch (opcode) {
9507 case 0x1: /* SQADD, UQADD */
9508 case 0x5: /* SQSUB, UQSUB */
9509 case 0x9: /* SQSHL, UQSHL */
9510 case 0xb: /* SQRSHL, UQRSHL */
9511 break;
9512 case 0x8: /* SSHL, USHL */
9513 case 0xa: /* SRSHL, URSHL */
9514 case 0x6: /* CMGT, CMHI */
9515 case 0x7: /* CMGE, CMHS */
9516 case 0x11: /* CMTST, CMEQ */
9517 case 0x10: /* ADD, SUB (vector) */
9518 if (size != 3) {
9519 unallocated_encoding(s);
9520 return;
9522 break;
9523 case 0x16: /* SQDMULH, SQRDMULH (vector) */
9524 if (size != 1 && size != 2) {
9525 unallocated_encoding(s);
9526 return;
9528 break;
9529 default:
9530 unallocated_encoding(s);
9531 return;
9534 if (!fp_access_check(s)) {
9535 return;
9538 tcg_rd = tcg_temp_new_i64();
9540 if (size == 3) {
9541 TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
9542 TCGv_i64 tcg_rm = read_fp_dreg(s, rm);
9544 handle_3same_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rm);
9545 tcg_temp_free_i64(tcg_rn);
9546 tcg_temp_free_i64(tcg_rm);
9547 } else {
9548 /* Do a single operation on the lowest element in the vector.
9549 * We use the standard Neon helpers and rely on 0 OP 0 == 0 with
9550 * no side effects for all these operations.
9551 * OPTME: special-purpose helpers would avoid doing some
9552 * unnecessary work in the helper for the 8 and 16 bit cases.
9554 NeonGenTwoOpEnvFn *genenvfn;
9555 TCGv_i32 tcg_rn = tcg_temp_new_i32();
9556 TCGv_i32 tcg_rm = tcg_temp_new_i32();
9557 TCGv_i32 tcg_rd32 = tcg_temp_new_i32();
9559 read_vec_element_i32(s, tcg_rn, rn, 0, size);
9560 read_vec_element_i32(s, tcg_rm, rm, 0, size);
9562 switch (opcode) {
9563 case 0x1: /* SQADD, UQADD */
9565 static NeonGenTwoOpEnvFn * const fns[3][2] = {
9566 { gen_helper_neon_qadd_s8, gen_helper_neon_qadd_u8 },
9567 { gen_helper_neon_qadd_s16, gen_helper_neon_qadd_u16 },
9568 { gen_helper_neon_qadd_s32, gen_helper_neon_qadd_u32 },
9570 genenvfn = fns[size][u];
9571 break;
9573 case 0x5: /* SQSUB, UQSUB */
9575 static NeonGenTwoOpEnvFn * const fns[3][2] = {
9576 { gen_helper_neon_qsub_s8, gen_helper_neon_qsub_u8 },
9577 { gen_helper_neon_qsub_s16, gen_helper_neon_qsub_u16 },
9578 { gen_helper_neon_qsub_s32, gen_helper_neon_qsub_u32 },
9580 genenvfn = fns[size][u];
9581 break;
9583 case 0x9: /* SQSHL, UQSHL */
9585 static NeonGenTwoOpEnvFn * const fns[3][2] = {
9586 { gen_helper_neon_qshl_s8, gen_helper_neon_qshl_u8 },
9587 { gen_helper_neon_qshl_s16, gen_helper_neon_qshl_u16 },
9588 { gen_helper_neon_qshl_s32, gen_helper_neon_qshl_u32 },
9590 genenvfn = fns[size][u];
9591 break;
9593 case 0xb: /* SQRSHL, UQRSHL */
9595 static NeonGenTwoOpEnvFn * const fns[3][2] = {
9596 { gen_helper_neon_qrshl_s8, gen_helper_neon_qrshl_u8 },
9597 { gen_helper_neon_qrshl_s16, gen_helper_neon_qrshl_u16 },
9598 { gen_helper_neon_qrshl_s32, gen_helper_neon_qrshl_u32 },
9600 genenvfn = fns[size][u];
9601 break;
9603 case 0x16: /* SQDMULH, SQRDMULH */
9605 static NeonGenTwoOpEnvFn * const fns[2][2] = {
9606 { gen_helper_neon_qdmulh_s16, gen_helper_neon_qrdmulh_s16 },
9607 { gen_helper_neon_qdmulh_s32, gen_helper_neon_qrdmulh_s32 },
9609 assert(size == 1 || size == 2);
9610 genenvfn = fns[size - 1][u];
9611 break;
9613 default:
9614 g_assert_not_reached();
9617 genenvfn(tcg_rd32, cpu_env, tcg_rn, tcg_rm);
9618 tcg_gen_extu_i32_i64(tcg_rd, tcg_rd32);
9619 tcg_temp_free_i32(tcg_rd32);
9620 tcg_temp_free_i32(tcg_rn);
9621 tcg_temp_free_i32(tcg_rm);
9624 write_fp_dreg(s, rd, tcg_rd);
9626 tcg_temp_free_i64(tcg_rd);
9629 /* AdvSIMD scalar three same FP16
9630 * 31 30 29 28 24 23 22 21 20 16 15 14 13 11 10 9 5 4 0
9631 * +-----+---+-----------+---+-----+------+-----+--------+---+----+----+
9632 * | 0 1 | U | 1 1 1 1 0 | a | 1 0 | Rm | 0 0 | opcode | 1 | Rn | Rd |
9633 * +-----+---+-----------+---+-----+------+-----+--------+---+----+----+
9634 * v: 0101 1110 0100 0000 0000 0100 0000 0000 => 5e400400
9635 * m: 1101 1111 0110 0000 1100 0100 0000 0000 => df60c400
9637 static void disas_simd_scalar_three_reg_same_fp16(DisasContext *s,
9638 uint32_t insn)
9640 int rd = extract32(insn, 0, 5);
9641 int rn = extract32(insn, 5, 5);
9642 int opcode = extract32(insn, 11, 3);
9643 int rm = extract32(insn, 16, 5);
9644 bool u = extract32(insn, 29, 1);
9645 bool a = extract32(insn, 23, 1);
9646 int fpopcode = opcode | (a << 3) | (u << 4);
9647 TCGv_ptr fpst;
9648 TCGv_i32 tcg_op1;
9649 TCGv_i32 tcg_op2;
9650 TCGv_i32 tcg_res;
9652 switch (fpopcode) {
9653 case 0x03: /* FMULX */
9654 case 0x04: /* FCMEQ (reg) */
9655 case 0x07: /* FRECPS */
9656 case 0x0f: /* FRSQRTS */
9657 case 0x14: /* FCMGE (reg) */
9658 case 0x15: /* FACGE */
9659 case 0x1a: /* FABD */
9660 case 0x1c: /* FCMGT (reg) */
9661 case 0x1d: /* FACGT */
9662 break;
9663 default:
9664 unallocated_encoding(s);
9665 return;
9668 if (!dc_isar_feature(aa64_fp16, s)) {
9669 unallocated_encoding(s);
9672 if (!fp_access_check(s)) {
9673 return;
9676 fpst = fpstatus_ptr(FPST_FPCR_F16);
9678 tcg_op1 = read_fp_hreg(s, rn);
9679 tcg_op2 = read_fp_hreg(s, rm);
9680 tcg_res = tcg_temp_new_i32();
9682 switch (fpopcode) {
9683 case 0x03: /* FMULX */
9684 gen_helper_advsimd_mulxh(tcg_res, tcg_op1, tcg_op2, fpst);
9685 break;
9686 case 0x04: /* FCMEQ (reg) */
9687 gen_helper_advsimd_ceq_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9688 break;
9689 case 0x07: /* FRECPS */
9690 gen_helper_recpsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9691 break;
9692 case 0x0f: /* FRSQRTS */
9693 gen_helper_rsqrtsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9694 break;
9695 case 0x14: /* FCMGE (reg) */
9696 gen_helper_advsimd_cge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9697 break;
9698 case 0x15: /* FACGE */
9699 gen_helper_advsimd_acge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9700 break;
9701 case 0x1a: /* FABD */
9702 gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
9703 tcg_gen_andi_i32(tcg_res, tcg_res, 0x7fff);
9704 break;
9705 case 0x1c: /* FCMGT (reg) */
9706 gen_helper_advsimd_cgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9707 break;
9708 case 0x1d: /* FACGT */
9709 gen_helper_advsimd_acgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9710 break;
9711 default:
9712 g_assert_not_reached();
9715 write_fp_sreg(s, rd, tcg_res);
9718 tcg_temp_free_i32(tcg_res);
9719 tcg_temp_free_i32(tcg_op1);
9720 tcg_temp_free_i32(tcg_op2);
9721 tcg_temp_free_ptr(fpst);
9724 /* AdvSIMD scalar three same extra
9725 * 31 30 29 28 24 23 22 21 20 16 15 14 11 10 9 5 4 0
9726 * +-----+---+-----------+------+---+------+---+--------+---+----+----+
9727 * | 0 1 | U | 1 1 1 1 0 | size | 0 | Rm | 1 | opcode | 1 | Rn | Rd |
9728 * +-----+---+-----------+------+---+------+---+--------+---+----+----+
9730 static void disas_simd_scalar_three_reg_same_extra(DisasContext *s,
9731 uint32_t insn)
9733 int rd = extract32(insn, 0, 5);
9734 int rn = extract32(insn, 5, 5);
9735 int opcode = extract32(insn, 11, 4);
9736 int rm = extract32(insn, 16, 5);
9737 int size = extract32(insn, 22, 2);
9738 bool u = extract32(insn, 29, 1);
9739 TCGv_i32 ele1, ele2, ele3;
9740 TCGv_i64 res;
9741 bool feature;
9743 switch (u * 16 + opcode) {
9744 case 0x10: /* SQRDMLAH (vector) */
9745 case 0x11: /* SQRDMLSH (vector) */
9746 if (size != 1 && size != 2) {
9747 unallocated_encoding(s);
9748 return;
9750 feature = dc_isar_feature(aa64_rdm, s);
9751 break;
9752 default:
9753 unallocated_encoding(s);
9754 return;
9756 if (!feature) {
9757 unallocated_encoding(s);
9758 return;
9760 if (!fp_access_check(s)) {
9761 return;
9764 /* Do a single operation on the lowest element in the vector.
9765 * We use the standard Neon helpers and rely on 0 OP 0 == 0
9766 * with no side effects for all these operations.
9767 * OPTME: special-purpose helpers would avoid doing some
9768 * unnecessary work in the helper for the 16 bit cases.
9770 ele1 = tcg_temp_new_i32();
9771 ele2 = tcg_temp_new_i32();
9772 ele3 = tcg_temp_new_i32();
9774 read_vec_element_i32(s, ele1, rn, 0, size);
9775 read_vec_element_i32(s, ele2, rm, 0, size);
9776 read_vec_element_i32(s, ele3, rd, 0, size);
9778 switch (opcode) {
9779 case 0x0: /* SQRDMLAH */
9780 if (size == 1) {
9781 gen_helper_neon_qrdmlah_s16(ele3, cpu_env, ele1, ele2, ele3);
9782 } else {
9783 gen_helper_neon_qrdmlah_s32(ele3, cpu_env, ele1, ele2, ele3);
9785 break;
9786 case 0x1: /* SQRDMLSH */
9787 if (size == 1) {
9788 gen_helper_neon_qrdmlsh_s16(ele3, cpu_env, ele1, ele2, ele3);
9789 } else {
9790 gen_helper_neon_qrdmlsh_s32(ele3, cpu_env, ele1, ele2, ele3);
9792 break;
9793 default:
9794 g_assert_not_reached();
9796 tcg_temp_free_i32(ele1);
9797 tcg_temp_free_i32(ele2);
9799 res = tcg_temp_new_i64();
9800 tcg_gen_extu_i32_i64(res, ele3);
9801 tcg_temp_free_i32(ele3);
9803 write_fp_dreg(s, rd, res);
9804 tcg_temp_free_i64(res);
9807 static void handle_2misc_64(DisasContext *s, int opcode, bool u,
9808 TCGv_i64 tcg_rd, TCGv_i64 tcg_rn,
9809 TCGv_i32 tcg_rmode, TCGv_ptr tcg_fpstatus)
9811 /* Handle 64->64 opcodes which are shared between the scalar and
9812 * vector 2-reg-misc groups. We cover every integer opcode where size == 3
9813 * is valid in either group and also the double-precision fp ops.
9814 * The caller only need provide tcg_rmode and tcg_fpstatus if the op
9815 * requires them.
9817 TCGCond cond;
9819 switch (opcode) {
9820 case 0x4: /* CLS, CLZ */
9821 if (u) {
9822 tcg_gen_clzi_i64(tcg_rd, tcg_rn, 64);
9823 } else {
9824 tcg_gen_clrsb_i64(tcg_rd, tcg_rn);
9826 break;
9827 case 0x5: /* NOT */
9828 /* This opcode is shared with CNT and RBIT but we have earlier
9829 * enforced that size == 3 if and only if this is the NOT insn.
9831 tcg_gen_not_i64(tcg_rd, tcg_rn);
9832 break;
9833 case 0x7: /* SQABS, SQNEG */
9834 if (u) {
9835 gen_helper_neon_qneg_s64(tcg_rd, cpu_env, tcg_rn);
9836 } else {
9837 gen_helper_neon_qabs_s64(tcg_rd, cpu_env, tcg_rn);
9839 break;
9840 case 0xa: /* CMLT */
9841 /* 64 bit integer comparison against zero, result is
9842 * test ? (2^64 - 1) : 0. We implement via setcond(!test) and
9843 * subtracting 1.
9845 cond = TCG_COND_LT;
9846 do_cmop:
9847 tcg_gen_setcondi_i64(cond, tcg_rd, tcg_rn, 0);
9848 tcg_gen_neg_i64(tcg_rd, tcg_rd);
9849 break;
9850 case 0x8: /* CMGT, CMGE */
9851 cond = u ? TCG_COND_GE : TCG_COND_GT;
9852 goto do_cmop;
9853 case 0x9: /* CMEQ, CMLE */
9854 cond = u ? TCG_COND_LE : TCG_COND_EQ;
9855 goto do_cmop;
9856 case 0xb: /* ABS, NEG */
9857 if (u) {
9858 tcg_gen_neg_i64(tcg_rd, tcg_rn);
9859 } else {
9860 tcg_gen_abs_i64(tcg_rd, tcg_rn);
9862 break;
9863 case 0x2f: /* FABS */
9864 gen_helper_vfp_absd(tcg_rd, tcg_rn);
9865 break;
9866 case 0x6f: /* FNEG */
9867 gen_helper_vfp_negd(tcg_rd, tcg_rn);
9868 break;
9869 case 0x7f: /* FSQRT */
9870 gen_helper_vfp_sqrtd(tcg_rd, tcg_rn, cpu_env);
9871 break;
9872 case 0x1a: /* FCVTNS */
9873 case 0x1b: /* FCVTMS */
9874 case 0x1c: /* FCVTAS */
9875 case 0x3a: /* FCVTPS */
9876 case 0x3b: /* FCVTZS */
9877 gen_helper_vfp_tosqd(tcg_rd, tcg_rn, tcg_constant_i32(0), tcg_fpstatus);
9878 break;
9879 case 0x5a: /* FCVTNU */
9880 case 0x5b: /* FCVTMU */
9881 case 0x5c: /* FCVTAU */
9882 case 0x7a: /* FCVTPU */
9883 case 0x7b: /* FCVTZU */
9884 gen_helper_vfp_touqd(tcg_rd, tcg_rn, tcg_constant_i32(0), tcg_fpstatus);
9885 break;
9886 case 0x18: /* FRINTN */
9887 case 0x19: /* FRINTM */
9888 case 0x38: /* FRINTP */
9889 case 0x39: /* FRINTZ */
9890 case 0x58: /* FRINTA */
9891 case 0x79: /* FRINTI */
9892 gen_helper_rintd(tcg_rd, tcg_rn, tcg_fpstatus);
9893 break;
9894 case 0x59: /* FRINTX */
9895 gen_helper_rintd_exact(tcg_rd, tcg_rn, tcg_fpstatus);
9896 break;
9897 case 0x1e: /* FRINT32Z */
9898 case 0x5e: /* FRINT32X */
9899 gen_helper_frint32_d(tcg_rd, tcg_rn, tcg_fpstatus);
9900 break;
9901 case 0x1f: /* FRINT64Z */
9902 case 0x5f: /* FRINT64X */
9903 gen_helper_frint64_d(tcg_rd, tcg_rn, tcg_fpstatus);
9904 break;
9905 default:
9906 g_assert_not_reached();
9910 static void handle_2misc_fcmp_zero(DisasContext *s, int opcode,
9911 bool is_scalar, bool is_u, bool is_q,
9912 int size, int rn, int rd)
9914 bool is_double = (size == MO_64);
9915 TCGv_ptr fpst;
9917 if (!fp_access_check(s)) {
9918 return;
9921 fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
9923 if (is_double) {
9924 TCGv_i64 tcg_op = tcg_temp_new_i64();
9925 TCGv_i64 tcg_zero = tcg_constant_i64(0);
9926 TCGv_i64 tcg_res = tcg_temp_new_i64();
9927 NeonGenTwoDoubleOpFn *genfn;
9928 bool swap = false;
9929 int pass;
9931 switch (opcode) {
9932 case 0x2e: /* FCMLT (zero) */
9933 swap = true;
9934 /* fallthrough */
9935 case 0x2c: /* FCMGT (zero) */
9936 genfn = gen_helper_neon_cgt_f64;
9937 break;
9938 case 0x2d: /* FCMEQ (zero) */
9939 genfn = gen_helper_neon_ceq_f64;
9940 break;
9941 case 0x6d: /* FCMLE (zero) */
9942 swap = true;
9943 /* fall through */
9944 case 0x6c: /* FCMGE (zero) */
9945 genfn = gen_helper_neon_cge_f64;
9946 break;
9947 default:
9948 g_assert_not_reached();
9951 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
9952 read_vec_element(s, tcg_op, rn, pass, MO_64);
9953 if (swap) {
9954 genfn(tcg_res, tcg_zero, tcg_op, fpst);
9955 } else {
9956 genfn(tcg_res, tcg_op, tcg_zero, fpst);
9958 write_vec_element(s, tcg_res, rd, pass, MO_64);
9960 tcg_temp_free_i64(tcg_res);
9961 tcg_temp_free_i64(tcg_op);
9963 clear_vec_high(s, !is_scalar, rd);
9964 } else {
9965 TCGv_i32 tcg_op = tcg_temp_new_i32();
9966 TCGv_i32 tcg_zero = tcg_constant_i32(0);
9967 TCGv_i32 tcg_res = tcg_temp_new_i32();
9968 NeonGenTwoSingleOpFn *genfn;
9969 bool swap = false;
9970 int pass, maxpasses;
9972 if (size == MO_16) {
9973 switch (opcode) {
9974 case 0x2e: /* FCMLT (zero) */
9975 swap = true;
9976 /* fall through */
9977 case 0x2c: /* FCMGT (zero) */
9978 genfn = gen_helper_advsimd_cgt_f16;
9979 break;
9980 case 0x2d: /* FCMEQ (zero) */
9981 genfn = gen_helper_advsimd_ceq_f16;
9982 break;
9983 case 0x6d: /* FCMLE (zero) */
9984 swap = true;
9985 /* fall through */
9986 case 0x6c: /* FCMGE (zero) */
9987 genfn = gen_helper_advsimd_cge_f16;
9988 break;
9989 default:
9990 g_assert_not_reached();
9992 } else {
9993 switch (opcode) {
9994 case 0x2e: /* FCMLT (zero) */
9995 swap = true;
9996 /* fall through */
9997 case 0x2c: /* FCMGT (zero) */
9998 genfn = gen_helper_neon_cgt_f32;
9999 break;
10000 case 0x2d: /* FCMEQ (zero) */
10001 genfn = gen_helper_neon_ceq_f32;
10002 break;
10003 case 0x6d: /* FCMLE (zero) */
10004 swap = true;
10005 /* fall through */
10006 case 0x6c: /* FCMGE (zero) */
10007 genfn = gen_helper_neon_cge_f32;
10008 break;
10009 default:
10010 g_assert_not_reached();
10014 if (is_scalar) {
10015 maxpasses = 1;
10016 } else {
10017 int vector_size = 8 << is_q;
10018 maxpasses = vector_size >> size;
10021 for (pass = 0; pass < maxpasses; pass++) {
10022 read_vec_element_i32(s, tcg_op, rn, pass, size);
10023 if (swap) {
10024 genfn(tcg_res, tcg_zero, tcg_op, fpst);
10025 } else {
10026 genfn(tcg_res, tcg_op, tcg_zero, fpst);
10028 if (is_scalar) {
10029 write_fp_sreg(s, rd, tcg_res);
10030 } else {
10031 write_vec_element_i32(s, tcg_res, rd, pass, size);
10034 tcg_temp_free_i32(tcg_res);
10035 tcg_temp_free_i32(tcg_op);
10036 if (!is_scalar) {
10037 clear_vec_high(s, is_q, rd);
10041 tcg_temp_free_ptr(fpst);
10044 static void handle_2misc_reciprocal(DisasContext *s, int opcode,
10045 bool is_scalar, bool is_u, bool is_q,
10046 int size, int rn, int rd)
10048 bool is_double = (size == 3);
10049 TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
10051 if (is_double) {
10052 TCGv_i64 tcg_op = tcg_temp_new_i64();
10053 TCGv_i64 tcg_res = tcg_temp_new_i64();
10054 int pass;
10056 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
10057 read_vec_element(s, tcg_op, rn, pass, MO_64);
10058 switch (opcode) {
10059 case 0x3d: /* FRECPE */
10060 gen_helper_recpe_f64(tcg_res, tcg_op, fpst);
10061 break;
10062 case 0x3f: /* FRECPX */
10063 gen_helper_frecpx_f64(tcg_res, tcg_op, fpst);
10064 break;
10065 case 0x7d: /* FRSQRTE */
10066 gen_helper_rsqrte_f64(tcg_res, tcg_op, fpst);
10067 break;
10068 default:
10069 g_assert_not_reached();
10071 write_vec_element(s, tcg_res, rd, pass, MO_64);
10073 tcg_temp_free_i64(tcg_res);
10074 tcg_temp_free_i64(tcg_op);
10075 clear_vec_high(s, !is_scalar, rd);
10076 } else {
10077 TCGv_i32 tcg_op = tcg_temp_new_i32();
10078 TCGv_i32 tcg_res = tcg_temp_new_i32();
10079 int pass, maxpasses;
10081 if (is_scalar) {
10082 maxpasses = 1;
10083 } else {
10084 maxpasses = is_q ? 4 : 2;
10087 for (pass = 0; pass < maxpasses; pass++) {
10088 read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
10090 switch (opcode) {
10091 case 0x3c: /* URECPE */
10092 gen_helper_recpe_u32(tcg_res, tcg_op);
10093 break;
10094 case 0x3d: /* FRECPE */
10095 gen_helper_recpe_f32(tcg_res, tcg_op, fpst);
10096 break;
10097 case 0x3f: /* FRECPX */
10098 gen_helper_frecpx_f32(tcg_res, tcg_op, fpst);
10099 break;
10100 case 0x7d: /* FRSQRTE */
10101 gen_helper_rsqrte_f32(tcg_res, tcg_op, fpst);
10102 break;
10103 default:
10104 g_assert_not_reached();
10107 if (is_scalar) {
10108 write_fp_sreg(s, rd, tcg_res);
10109 } else {
10110 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
10113 tcg_temp_free_i32(tcg_res);
10114 tcg_temp_free_i32(tcg_op);
10115 if (!is_scalar) {
10116 clear_vec_high(s, is_q, rd);
10119 tcg_temp_free_ptr(fpst);
10122 static void handle_2misc_narrow(DisasContext *s, bool scalar,
10123 int opcode, bool u, bool is_q,
10124 int size, int rn, int rd)
10126 /* Handle 2-reg-misc ops which are narrowing (so each 2*size element
10127 * in the source becomes a size element in the destination).
10129 int pass;
10130 TCGv_i32 tcg_res[2];
10131 int destelt = is_q ? 2 : 0;
10132 int passes = scalar ? 1 : 2;
10134 if (scalar) {
10135 tcg_res[1] = tcg_constant_i32(0);
10138 for (pass = 0; pass < passes; pass++) {
10139 TCGv_i64 tcg_op = tcg_temp_new_i64();
10140 NeonGenNarrowFn *genfn = NULL;
10141 NeonGenNarrowEnvFn *genenvfn = NULL;
10143 if (scalar) {
10144 read_vec_element(s, tcg_op, rn, pass, size + 1);
10145 } else {
10146 read_vec_element(s, tcg_op, rn, pass, MO_64);
10148 tcg_res[pass] = tcg_temp_new_i32();
10150 switch (opcode) {
10151 case 0x12: /* XTN, SQXTUN */
10153 static NeonGenNarrowFn * const xtnfns[3] = {
10154 gen_helper_neon_narrow_u8,
10155 gen_helper_neon_narrow_u16,
10156 tcg_gen_extrl_i64_i32,
10158 static NeonGenNarrowEnvFn * const sqxtunfns[3] = {
10159 gen_helper_neon_unarrow_sat8,
10160 gen_helper_neon_unarrow_sat16,
10161 gen_helper_neon_unarrow_sat32,
10163 if (u) {
10164 genenvfn = sqxtunfns[size];
10165 } else {
10166 genfn = xtnfns[size];
10168 break;
10170 case 0x14: /* SQXTN, UQXTN */
10172 static NeonGenNarrowEnvFn * const fns[3][2] = {
10173 { gen_helper_neon_narrow_sat_s8,
10174 gen_helper_neon_narrow_sat_u8 },
10175 { gen_helper_neon_narrow_sat_s16,
10176 gen_helper_neon_narrow_sat_u16 },
10177 { gen_helper_neon_narrow_sat_s32,
10178 gen_helper_neon_narrow_sat_u32 },
10180 genenvfn = fns[size][u];
10181 break;
10183 case 0x16: /* FCVTN, FCVTN2 */
10184 /* 32 bit to 16 bit or 64 bit to 32 bit float conversion */
10185 if (size == 2) {
10186 gen_helper_vfp_fcvtsd(tcg_res[pass], tcg_op, cpu_env);
10187 } else {
10188 TCGv_i32 tcg_lo = tcg_temp_new_i32();
10189 TCGv_i32 tcg_hi = tcg_temp_new_i32();
10190 TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
10191 TCGv_i32 ahp = get_ahp_flag();
10193 tcg_gen_extr_i64_i32(tcg_lo, tcg_hi, tcg_op);
10194 gen_helper_vfp_fcvt_f32_to_f16(tcg_lo, tcg_lo, fpst, ahp);
10195 gen_helper_vfp_fcvt_f32_to_f16(tcg_hi, tcg_hi, fpst, ahp);
10196 tcg_gen_deposit_i32(tcg_res[pass], tcg_lo, tcg_hi, 16, 16);
10197 tcg_temp_free_i32(tcg_lo);
10198 tcg_temp_free_i32(tcg_hi);
10199 tcg_temp_free_ptr(fpst);
10200 tcg_temp_free_i32(ahp);
10202 break;
10203 case 0x36: /* BFCVTN, BFCVTN2 */
10205 TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
10206 gen_helper_bfcvt_pair(tcg_res[pass], tcg_op, fpst);
10207 tcg_temp_free_ptr(fpst);
10209 break;
10210 case 0x56: /* FCVTXN, FCVTXN2 */
10211 /* 64 bit to 32 bit float conversion
10212 * with von Neumann rounding (round to odd)
10214 assert(size == 2);
10215 gen_helper_fcvtx_f64_to_f32(tcg_res[pass], tcg_op, cpu_env);
10216 break;
10217 default:
10218 g_assert_not_reached();
10221 if (genfn) {
10222 genfn(tcg_res[pass], tcg_op);
10223 } else if (genenvfn) {
10224 genenvfn(tcg_res[pass], cpu_env, tcg_op);
10227 tcg_temp_free_i64(tcg_op);
10230 for (pass = 0; pass < 2; pass++) {
10231 write_vec_element_i32(s, tcg_res[pass], rd, destelt + pass, MO_32);
10232 tcg_temp_free_i32(tcg_res[pass]);
10234 clear_vec_high(s, is_q, rd);
10237 /* Remaining saturating accumulating ops */
10238 static void handle_2misc_satacc(DisasContext *s, bool is_scalar, bool is_u,
10239 bool is_q, int size, int rn, int rd)
10241 bool is_double = (size == 3);
10243 if (is_double) {
10244 TCGv_i64 tcg_rn = tcg_temp_new_i64();
10245 TCGv_i64 tcg_rd = tcg_temp_new_i64();
10246 int pass;
10248 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
10249 read_vec_element(s, tcg_rn, rn, pass, MO_64);
10250 read_vec_element(s, tcg_rd, rd, pass, MO_64);
10252 if (is_u) { /* USQADD */
10253 gen_helper_neon_uqadd_s64(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10254 } else { /* SUQADD */
10255 gen_helper_neon_sqadd_u64(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10257 write_vec_element(s, tcg_rd, rd, pass, MO_64);
10259 tcg_temp_free_i64(tcg_rd);
10260 tcg_temp_free_i64(tcg_rn);
10261 clear_vec_high(s, !is_scalar, rd);
10262 } else {
10263 TCGv_i32 tcg_rn = tcg_temp_new_i32();
10264 TCGv_i32 tcg_rd = tcg_temp_new_i32();
10265 int pass, maxpasses;
10267 if (is_scalar) {
10268 maxpasses = 1;
10269 } else {
10270 maxpasses = is_q ? 4 : 2;
10273 for (pass = 0; pass < maxpasses; pass++) {
10274 if (is_scalar) {
10275 read_vec_element_i32(s, tcg_rn, rn, pass, size);
10276 read_vec_element_i32(s, tcg_rd, rd, pass, size);
10277 } else {
10278 read_vec_element_i32(s, tcg_rn, rn, pass, MO_32);
10279 read_vec_element_i32(s, tcg_rd, rd, pass, MO_32);
10282 if (is_u) { /* USQADD */
10283 switch (size) {
10284 case 0:
10285 gen_helper_neon_uqadd_s8(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10286 break;
10287 case 1:
10288 gen_helper_neon_uqadd_s16(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10289 break;
10290 case 2:
10291 gen_helper_neon_uqadd_s32(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10292 break;
10293 default:
10294 g_assert_not_reached();
10296 } else { /* SUQADD */
10297 switch (size) {
10298 case 0:
10299 gen_helper_neon_sqadd_u8(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10300 break;
10301 case 1:
10302 gen_helper_neon_sqadd_u16(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10303 break;
10304 case 2:
10305 gen_helper_neon_sqadd_u32(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10306 break;
10307 default:
10308 g_assert_not_reached();
10312 if (is_scalar) {
10313 write_vec_element(s, tcg_constant_i64(0), rd, 0, MO_64);
10315 write_vec_element_i32(s, tcg_rd, rd, pass, MO_32);
10317 tcg_temp_free_i32(tcg_rd);
10318 tcg_temp_free_i32(tcg_rn);
10319 clear_vec_high(s, is_q, rd);
10323 /* AdvSIMD scalar two reg misc
10324 * 31 30 29 28 24 23 22 21 17 16 12 11 10 9 5 4 0
10325 * +-----+---+-----------+------+-----------+--------+-----+------+------+
10326 * | 0 1 | U | 1 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 | Rn | Rd |
10327 * +-----+---+-----------+------+-----------+--------+-----+------+------+
10329 static void disas_simd_scalar_two_reg_misc(DisasContext *s, uint32_t insn)
10331 int rd = extract32(insn, 0, 5);
10332 int rn = extract32(insn, 5, 5);
10333 int opcode = extract32(insn, 12, 5);
10334 int size = extract32(insn, 22, 2);
10335 bool u = extract32(insn, 29, 1);
10336 bool is_fcvt = false;
10337 int rmode;
10338 TCGv_i32 tcg_rmode;
10339 TCGv_ptr tcg_fpstatus;
10341 switch (opcode) {
10342 case 0x3: /* USQADD / SUQADD*/
10343 if (!fp_access_check(s)) {
10344 return;
10346 handle_2misc_satacc(s, true, u, false, size, rn, rd);
10347 return;
10348 case 0x7: /* SQABS / SQNEG */
10349 break;
10350 case 0xa: /* CMLT */
10351 if (u) {
10352 unallocated_encoding(s);
10353 return;
10355 /* fall through */
10356 case 0x8: /* CMGT, CMGE */
10357 case 0x9: /* CMEQ, CMLE */
10358 case 0xb: /* ABS, NEG */
10359 if (size != 3) {
10360 unallocated_encoding(s);
10361 return;
10363 break;
10364 case 0x12: /* SQXTUN */
10365 if (!u) {
10366 unallocated_encoding(s);
10367 return;
10369 /* fall through */
10370 case 0x14: /* SQXTN, UQXTN */
10371 if (size == 3) {
10372 unallocated_encoding(s);
10373 return;
10375 if (!fp_access_check(s)) {
10376 return;
10378 handle_2misc_narrow(s, true, opcode, u, false, size, rn, rd);
10379 return;
10380 case 0xc ... 0xf:
10381 case 0x16 ... 0x1d:
10382 case 0x1f:
10383 /* Floating point: U, size[1] and opcode indicate operation;
10384 * size[0] indicates single or double precision.
10386 opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
10387 size = extract32(size, 0, 1) ? 3 : 2;
10388 switch (opcode) {
10389 case 0x2c: /* FCMGT (zero) */
10390 case 0x2d: /* FCMEQ (zero) */
10391 case 0x2e: /* FCMLT (zero) */
10392 case 0x6c: /* FCMGE (zero) */
10393 case 0x6d: /* FCMLE (zero) */
10394 handle_2misc_fcmp_zero(s, opcode, true, u, true, size, rn, rd);
10395 return;
10396 case 0x1d: /* SCVTF */
10397 case 0x5d: /* UCVTF */
10399 bool is_signed = (opcode == 0x1d);
10400 if (!fp_access_check(s)) {
10401 return;
10403 handle_simd_intfp_conv(s, rd, rn, 1, is_signed, 0, size);
10404 return;
10406 case 0x3d: /* FRECPE */
10407 case 0x3f: /* FRECPX */
10408 case 0x7d: /* FRSQRTE */
10409 if (!fp_access_check(s)) {
10410 return;
10412 handle_2misc_reciprocal(s, opcode, true, u, true, size, rn, rd);
10413 return;
10414 case 0x1a: /* FCVTNS */
10415 case 0x1b: /* FCVTMS */
10416 case 0x3a: /* FCVTPS */
10417 case 0x3b: /* FCVTZS */
10418 case 0x5a: /* FCVTNU */
10419 case 0x5b: /* FCVTMU */
10420 case 0x7a: /* FCVTPU */
10421 case 0x7b: /* FCVTZU */
10422 is_fcvt = true;
10423 rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
10424 break;
10425 case 0x1c: /* FCVTAS */
10426 case 0x5c: /* FCVTAU */
10427 /* TIEAWAY doesn't fit in the usual rounding mode encoding */
10428 is_fcvt = true;
10429 rmode = FPROUNDING_TIEAWAY;
10430 break;
10431 case 0x56: /* FCVTXN, FCVTXN2 */
10432 if (size == 2) {
10433 unallocated_encoding(s);
10434 return;
10436 if (!fp_access_check(s)) {
10437 return;
10439 handle_2misc_narrow(s, true, opcode, u, false, size - 1, rn, rd);
10440 return;
10441 default:
10442 unallocated_encoding(s);
10443 return;
10445 break;
10446 default:
10447 unallocated_encoding(s);
10448 return;
10451 if (!fp_access_check(s)) {
10452 return;
10455 if (is_fcvt) {
10456 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rmode));
10457 tcg_fpstatus = fpstatus_ptr(FPST_FPCR);
10458 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
10459 } else {
10460 tcg_rmode = NULL;
10461 tcg_fpstatus = NULL;
10464 if (size == 3) {
10465 TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
10466 TCGv_i64 tcg_rd = tcg_temp_new_i64();
10468 handle_2misc_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rmode, tcg_fpstatus);
10469 write_fp_dreg(s, rd, tcg_rd);
10470 tcg_temp_free_i64(tcg_rd);
10471 tcg_temp_free_i64(tcg_rn);
10472 } else {
10473 TCGv_i32 tcg_rn = tcg_temp_new_i32();
10474 TCGv_i32 tcg_rd = tcg_temp_new_i32();
10476 read_vec_element_i32(s, tcg_rn, rn, 0, size);
10478 switch (opcode) {
10479 case 0x7: /* SQABS, SQNEG */
10481 NeonGenOneOpEnvFn *genfn;
10482 static NeonGenOneOpEnvFn * const fns[3][2] = {
10483 { gen_helper_neon_qabs_s8, gen_helper_neon_qneg_s8 },
10484 { gen_helper_neon_qabs_s16, gen_helper_neon_qneg_s16 },
10485 { gen_helper_neon_qabs_s32, gen_helper_neon_qneg_s32 },
10487 genfn = fns[size][u];
10488 genfn(tcg_rd, cpu_env, tcg_rn);
10489 break;
10491 case 0x1a: /* FCVTNS */
10492 case 0x1b: /* FCVTMS */
10493 case 0x1c: /* FCVTAS */
10494 case 0x3a: /* FCVTPS */
10495 case 0x3b: /* FCVTZS */
10496 gen_helper_vfp_tosls(tcg_rd, tcg_rn, tcg_constant_i32(0),
10497 tcg_fpstatus);
10498 break;
10499 case 0x5a: /* FCVTNU */
10500 case 0x5b: /* FCVTMU */
10501 case 0x5c: /* FCVTAU */
10502 case 0x7a: /* FCVTPU */
10503 case 0x7b: /* FCVTZU */
10504 gen_helper_vfp_touls(tcg_rd, tcg_rn, tcg_constant_i32(0),
10505 tcg_fpstatus);
10506 break;
10507 default:
10508 g_assert_not_reached();
10511 write_fp_sreg(s, rd, tcg_rd);
10512 tcg_temp_free_i32(tcg_rd);
10513 tcg_temp_free_i32(tcg_rn);
10516 if (is_fcvt) {
10517 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
10518 tcg_temp_free_i32(tcg_rmode);
10519 tcg_temp_free_ptr(tcg_fpstatus);
10523 /* SSHR[RA]/USHR[RA] - Vector shift right (optional rounding/accumulate) */
10524 static void handle_vec_simd_shri(DisasContext *s, bool is_q, bool is_u,
10525 int immh, int immb, int opcode, int rn, int rd)
10527 int size = 32 - clz32(immh) - 1;
10528 int immhb = immh << 3 | immb;
10529 int shift = 2 * (8 << size) - immhb;
10530 GVecGen2iFn *gvec_fn;
10532 if (extract32(immh, 3, 1) && !is_q) {
10533 unallocated_encoding(s);
10534 return;
10536 tcg_debug_assert(size <= 3);
10538 if (!fp_access_check(s)) {
10539 return;
10542 switch (opcode) {
10543 case 0x02: /* SSRA / USRA (accumulate) */
10544 gvec_fn = is_u ? gen_gvec_usra : gen_gvec_ssra;
10545 break;
10547 case 0x08: /* SRI */
10548 gvec_fn = gen_gvec_sri;
10549 break;
10551 case 0x00: /* SSHR / USHR */
10552 if (is_u) {
10553 if (shift == 8 << size) {
10554 /* Shift count the same size as element size produces zero. */
10555 tcg_gen_gvec_dup_imm(size, vec_full_reg_offset(s, rd),
10556 is_q ? 16 : 8, vec_full_reg_size(s), 0);
10557 return;
10559 gvec_fn = tcg_gen_gvec_shri;
10560 } else {
10561 /* Shift count the same size as element size produces all sign. */
10562 if (shift == 8 << size) {
10563 shift -= 1;
10565 gvec_fn = tcg_gen_gvec_sari;
10567 break;
10569 case 0x04: /* SRSHR / URSHR (rounding) */
10570 gvec_fn = is_u ? gen_gvec_urshr : gen_gvec_srshr;
10571 break;
10573 case 0x06: /* SRSRA / URSRA (accum + rounding) */
10574 gvec_fn = is_u ? gen_gvec_ursra : gen_gvec_srsra;
10575 break;
10577 default:
10578 g_assert_not_reached();
10581 gen_gvec_fn2i(s, is_q, rd, rn, shift, gvec_fn, size);
10584 /* SHL/SLI - Vector shift left */
10585 static void handle_vec_simd_shli(DisasContext *s, bool is_q, bool insert,
10586 int immh, int immb, int opcode, int rn, int rd)
10588 int size = 32 - clz32(immh) - 1;
10589 int immhb = immh << 3 | immb;
10590 int shift = immhb - (8 << size);
10592 /* Range of size is limited by decode: immh is a non-zero 4 bit field */
10593 assert(size >= 0 && size <= 3);
10595 if (extract32(immh, 3, 1) && !is_q) {
10596 unallocated_encoding(s);
10597 return;
10600 if (!fp_access_check(s)) {
10601 return;
10604 if (insert) {
10605 gen_gvec_fn2i(s, is_q, rd, rn, shift, gen_gvec_sli, size);
10606 } else {
10607 gen_gvec_fn2i(s, is_q, rd, rn, shift, tcg_gen_gvec_shli, size);
10611 /* USHLL/SHLL - Vector shift left with widening */
10612 static void handle_vec_simd_wshli(DisasContext *s, bool is_q, bool is_u,
10613 int immh, int immb, int opcode, int rn, int rd)
10615 int size = 32 - clz32(immh) - 1;
10616 int immhb = immh << 3 | immb;
10617 int shift = immhb - (8 << size);
10618 int dsize = 64;
10619 int esize = 8 << size;
10620 int elements = dsize/esize;
10621 TCGv_i64 tcg_rn = new_tmp_a64(s);
10622 TCGv_i64 tcg_rd = new_tmp_a64(s);
10623 int i;
10625 if (size >= 3) {
10626 unallocated_encoding(s);
10627 return;
10630 if (!fp_access_check(s)) {
10631 return;
10634 /* For the LL variants the store is larger than the load,
10635 * so if rd == rn we would overwrite parts of our input.
10636 * So load everything right now and use shifts in the main loop.
10638 read_vec_element(s, tcg_rn, rn, is_q ? 1 : 0, MO_64);
10640 for (i = 0; i < elements; i++) {
10641 tcg_gen_shri_i64(tcg_rd, tcg_rn, i * esize);
10642 ext_and_shift_reg(tcg_rd, tcg_rd, size | (!is_u << 2), 0);
10643 tcg_gen_shli_i64(tcg_rd, tcg_rd, shift);
10644 write_vec_element(s, tcg_rd, rd, i, size + 1);
10648 /* SHRN/RSHRN - Shift right with narrowing (and potential rounding) */
10649 static void handle_vec_simd_shrn(DisasContext *s, bool is_q,
10650 int immh, int immb, int opcode, int rn, int rd)
10652 int immhb = immh << 3 | immb;
10653 int size = 32 - clz32(immh) - 1;
10654 int dsize = 64;
10655 int esize = 8 << size;
10656 int elements = dsize/esize;
10657 int shift = (2 * esize) - immhb;
10658 bool round = extract32(opcode, 0, 1);
10659 TCGv_i64 tcg_rn, tcg_rd, tcg_final;
10660 TCGv_i64 tcg_round;
10661 int i;
10663 if (extract32(immh, 3, 1)) {
10664 unallocated_encoding(s);
10665 return;
10668 if (!fp_access_check(s)) {
10669 return;
10672 tcg_rn = tcg_temp_new_i64();
10673 tcg_rd = tcg_temp_new_i64();
10674 tcg_final = tcg_temp_new_i64();
10675 read_vec_element(s, tcg_final, rd, is_q ? 1 : 0, MO_64);
10677 if (round) {
10678 tcg_round = tcg_constant_i64(1ULL << (shift - 1));
10679 } else {
10680 tcg_round = NULL;
10683 for (i = 0; i < elements; i++) {
10684 read_vec_element(s, tcg_rn, rn, i, size+1);
10685 handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
10686 false, true, size+1, shift);
10688 tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
10691 if (!is_q) {
10692 write_vec_element(s, tcg_final, rd, 0, MO_64);
10693 } else {
10694 write_vec_element(s, tcg_final, rd, 1, MO_64);
10696 tcg_temp_free_i64(tcg_rn);
10697 tcg_temp_free_i64(tcg_rd);
10698 tcg_temp_free_i64(tcg_final);
10700 clear_vec_high(s, is_q, rd);
10704 /* AdvSIMD shift by immediate
10705 * 31 30 29 28 23 22 19 18 16 15 11 10 9 5 4 0
10706 * +---+---+---+-------------+------+------+--------+---+------+------+
10707 * | 0 | Q | U | 0 1 1 1 1 0 | immh | immb | opcode | 1 | Rn | Rd |
10708 * +---+---+---+-------------+------+------+--------+---+------+------+
10710 static void disas_simd_shift_imm(DisasContext *s, uint32_t insn)
10712 int rd = extract32(insn, 0, 5);
10713 int rn = extract32(insn, 5, 5);
10714 int opcode = extract32(insn, 11, 5);
10715 int immb = extract32(insn, 16, 3);
10716 int immh = extract32(insn, 19, 4);
10717 bool is_u = extract32(insn, 29, 1);
10718 bool is_q = extract32(insn, 30, 1);
10720 /* data_proc_simd[] has sent immh == 0 to disas_simd_mod_imm. */
10721 assert(immh != 0);
10723 switch (opcode) {
10724 case 0x08: /* SRI */
10725 if (!is_u) {
10726 unallocated_encoding(s);
10727 return;
10729 /* fall through */
10730 case 0x00: /* SSHR / USHR */
10731 case 0x02: /* SSRA / USRA (accumulate) */
10732 case 0x04: /* SRSHR / URSHR (rounding) */
10733 case 0x06: /* SRSRA / URSRA (accum + rounding) */
10734 handle_vec_simd_shri(s, is_q, is_u, immh, immb, opcode, rn, rd);
10735 break;
10736 case 0x0a: /* SHL / SLI */
10737 handle_vec_simd_shli(s, is_q, is_u, immh, immb, opcode, rn, rd);
10738 break;
10739 case 0x10: /* SHRN */
10740 case 0x11: /* RSHRN / SQRSHRUN */
10741 if (is_u) {
10742 handle_vec_simd_sqshrn(s, false, is_q, false, true, immh, immb,
10743 opcode, rn, rd);
10744 } else {
10745 handle_vec_simd_shrn(s, is_q, immh, immb, opcode, rn, rd);
10747 break;
10748 case 0x12: /* SQSHRN / UQSHRN */
10749 case 0x13: /* SQRSHRN / UQRSHRN */
10750 handle_vec_simd_sqshrn(s, false, is_q, is_u, is_u, immh, immb,
10751 opcode, rn, rd);
10752 break;
10753 case 0x14: /* SSHLL / USHLL */
10754 handle_vec_simd_wshli(s, is_q, is_u, immh, immb, opcode, rn, rd);
10755 break;
10756 case 0x1c: /* SCVTF / UCVTF */
10757 handle_simd_shift_intfp_conv(s, false, is_q, is_u, immh, immb,
10758 opcode, rn, rd);
10759 break;
10760 case 0xc: /* SQSHLU */
10761 if (!is_u) {
10762 unallocated_encoding(s);
10763 return;
10765 handle_simd_qshl(s, false, is_q, false, true, immh, immb, rn, rd);
10766 break;
10767 case 0xe: /* SQSHL, UQSHL */
10768 handle_simd_qshl(s, false, is_q, is_u, is_u, immh, immb, rn, rd);
10769 break;
10770 case 0x1f: /* FCVTZS/ FCVTZU */
10771 handle_simd_shift_fpint_conv(s, false, is_q, is_u, immh, immb, rn, rd);
10772 return;
10773 default:
10774 unallocated_encoding(s);
10775 return;
10779 /* Generate code to do a "long" addition or subtraction, ie one done in
10780 * TCGv_i64 on vector lanes twice the width specified by size.
10782 static void gen_neon_addl(int size, bool is_sub, TCGv_i64 tcg_res,
10783 TCGv_i64 tcg_op1, TCGv_i64 tcg_op2)
10785 static NeonGenTwo64OpFn * const fns[3][2] = {
10786 { gen_helper_neon_addl_u16, gen_helper_neon_subl_u16 },
10787 { gen_helper_neon_addl_u32, gen_helper_neon_subl_u32 },
10788 { tcg_gen_add_i64, tcg_gen_sub_i64 },
10790 NeonGenTwo64OpFn *genfn;
10791 assert(size < 3);
10793 genfn = fns[size][is_sub];
10794 genfn(tcg_res, tcg_op1, tcg_op2);
10797 static void handle_3rd_widening(DisasContext *s, int is_q, int is_u, int size,
10798 int opcode, int rd, int rn, int rm)
10800 /* 3-reg-different widening insns: 64 x 64 -> 128 */
10801 TCGv_i64 tcg_res[2];
10802 int pass, accop;
10804 tcg_res[0] = tcg_temp_new_i64();
10805 tcg_res[1] = tcg_temp_new_i64();
10807 /* Does this op do an adding accumulate, a subtracting accumulate,
10808 * or no accumulate at all?
10810 switch (opcode) {
10811 case 5:
10812 case 8:
10813 case 9:
10814 accop = 1;
10815 break;
10816 case 10:
10817 case 11:
10818 accop = -1;
10819 break;
10820 default:
10821 accop = 0;
10822 break;
10825 if (accop != 0) {
10826 read_vec_element(s, tcg_res[0], rd, 0, MO_64);
10827 read_vec_element(s, tcg_res[1], rd, 1, MO_64);
10830 /* size == 2 means two 32x32->64 operations; this is worth special
10831 * casing because we can generally handle it inline.
10833 if (size == 2) {
10834 for (pass = 0; pass < 2; pass++) {
10835 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10836 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
10837 TCGv_i64 tcg_passres;
10838 MemOp memop = MO_32 | (is_u ? 0 : MO_SIGN);
10840 int elt = pass + is_q * 2;
10842 read_vec_element(s, tcg_op1, rn, elt, memop);
10843 read_vec_element(s, tcg_op2, rm, elt, memop);
10845 if (accop == 0) {
10846 tcg_passres = tcg_res[pass];
10847 } else {
10848 tcg_passres = tcg_temp_new_i64();
10851 switch (opcode) {
10852 case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10853 tcg_gen_add_i64(tcg_passres, tcg_op1, tcg_op2);
10854 break;
10855 case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
10856 tcg_gen_sub_i64(tcg_passres, tcg_op1, tcg_op2);
10857 break;
10858 case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10859 case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10861 TCGv_i64 tcg_tmp1 = tcg_temp_new_i64();
10862 TCGv_i64 tcg_tmp2 = tcg_temp_new_i64();
10864 tcg_gen_sub_i64(tcg_tmp1, tcg_op1, tcg_op2);
10865 tcg_gen_sub_i64(tcg_tmp2, tcg_op2, tcg_op1);
10866 tcg_gen_movcond_i64(is_u ? TCG_COND_GEU : TCG_COND_GE,
10867 tcg_passres,
10868 tcg_op1, tcg_op2, tcg_tmp1, tcg_tmp2);
10869 tcg_temp_free_i64(tcg_tmp1);
10870 tcg_temp_free_i64(tcg_tmp2);
10871 break;
10873 case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10874 case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10875 case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
10876 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
10877 break;
10878 case 9: /* SQDMLAL, SQDMLAL2 */
10879 case 11: /* SQDMLSL, SQDMLSL2 */
10880 case 13: /* SQDMULL, SQDMULL2 */
10881 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
10882 gen_helper_neon_addl_saturate_s64(tcg_passres, cpu_env,
10883 tcg_passres, tcg_passres);
10884 break;
10885 default:
10886 g_assert_not_reached();
10889 if (opcode == 9 || opcode == 11) {
10890 /* saturating accumulate ops */
10891 if (accop < 0) {
10892 tcg_gen_neg_i64(tcg_passres, tcg_passres);
10894 gen_helper_neon_addl_saturate_s64(tcg_res[pass], cpu_env,
10895 tcg_res[pass], tcg_passres);
10896 } else if (accop > 0) {
10897 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
10898 } else if (accop < 0) {
10899 tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
10902 if (accop != 0) {
10903 tcg_temp_free_i64(tcg_passres);
10906 tcg_temp_free_i64(tcg_op1);
10907 tcg_temp_free_i64(tcg_op2);
10909 } else {
10910 /* size 0 or 1, generally helper functions */
10911 for (pass = 0; pass < 2; pass++) {
10912 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
10913 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
10914 TCGv_i64 tcg_passres;
10915 int elt = pass + is_q * 2;
10917 read_vec_element_i32(s, tcg_op1, rn, elt, MO_32);
10918 read_vec_element_i32(s, tcg_op2, rm, elt, MO_32);
10920 if (accop == 0) {
10921 tcg_passres = tcg_res[pass];
10922 } else {
10923 tcg_passres = tcg_temp_new_i64();
10926 switch (opcode) {
10927 case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10928 case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
10930 TCGv_i64 tcg_op2_64 = tcg_temp_new_i64();
10931 static NeonGenWidenFn * const widenfns[2][2] = {
10932 { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
10933 { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
10935 NeonGenWidenFn *widenfn = widenfns[size][is_u];
10937 widenfn(tcg_op2_64, tcg_op2);
10938 widenfn(tcg_passres, tcg_op1);
10939 gen_neon_addl(size, (opcode == 2), tcg_passres,
10940 tcg_passres, tcg_op2_64);
10941 tcg_temp_free_i64(tcg_op2_64);
10942 break;
10944 case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10945 case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10946 if (size == 0) {
10947 if (is_u) {
10948 gen_helper_neon_abdl_u16(tcg_passres, tcg_op1, tcg_op2);
10949 } else {
10950 gen_helper_neon_abdl_s16(tcg_passres, tcg_op1, tcg_op2);
10952 } else {
10953 if (is_u) {
10954 gen_helper_neon_abdl_u32(tcg_passres, tcg_op1, tcg_op2);
10955 } else {
10956 gen_helper_neon_abdl_s32(tcg_passres, tcg_op1, tcg_op2);
10959 break;
10960 case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10961 case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10962 case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
10963 if (size == 0) {
10964 if (is_u) {
10965 gen_helper_neon_mull_u8(tcg_passres, tcg_op1, tcg_op2);
10966 } else {
10967 gen_helper_neon_mull_s8(tcg_passres, tcg_op1, tcg_op2);
10969 } else {
10970 if (is_u) {
10971 gen_helper_neon_mull_u16(tcg_passres, tcg_op1, tcg_op2);
10972 } else {
10973 gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
10976 break;
10977 case 9: /* SQDMLAL, SQDMLAL2 */
10978 case 11: /* SQDMLSL, SQDMLSL2 */
10979 case 13: /* SQDMULL, SQDMULL2 */
10980 assert(size == 1);
10981 gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
10982 gen_helper_neon_addl_saturate_s32(tcg_passres, cpu_env,
10983 tcg_passres, tcg_passres);
10984 break;
10985 default:
10986 g_assert_not_reached();
10988 tcg_temp_free_i32(tcg_op1);
10989 tcg_temp_free_i32(tcg_op2);
10991 if (accop != 0) {
10992 if (opcode == 9 || opcode == 11) {
10993 /* saturating accumulate ops */
10994 if (accop < 0) {
10995 gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
10997 gen_helper_neon_addl_saturate_s32(tcg_res[pass], cpu_env,
10998 tcg_res[pass],
10999 tcg_passres);
11000 } else {
11001 gen_neon_addl(size, (accop < 0), tcg_res[pass],
11002 tcg_res[pass], tcg_passres);
11004 tcg_temp_free_i64(tcg_passres);
11009 write_vec_element(s, tcg_res[0], rd, 0, MO_64);
11010 write_vec_element(s, tcg_res[1], rd, 1, MO_64);
11011 tcg_temp_free_i64(tcg_res[0]);
11012 tcg_temp_free_i64(tcg_res[1]);
11015 static void handle_3rd_wide(DisasContext *s, int is_q, int is_u, int size,
11016 int opcode, int rd, int rn, int rm)
11018 TCGv_i64 tcg_res[2];
11019 int part = is_q ? 2 : 0;
11020 int pass;
11022 for (pass = 0; pass < 2; pass++) {
11023 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
11024 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11025 TCGv_i64 tcg_op2_wide = tcg_temp_new_i64();
11026 static NeonGenWidenFn * const widenfns[3][2] = {
11027 { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
11028 { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
11029 { tcg_gen_ext_i32_i64, tcg_gen_extu_i32_i64 },
11031 NeonGenWidenFn *widenfn = widenfns[size][is_u];
11033 read_vec_element(s, tcg_op1, rn, pass, MO_64);
11034 read_vec_element_i32(s, tcg_op2, rm, part + pass, MO_32);
11035 widenfn(tcg_op2_wide, tcg_op2);
11036 tcg_temp_free_i32(tcg_op2);
11037 tcg_res[pass] = tcg_temp_new_i64();
11038 gen_neon_addl(size, (opcode == 3),
11039 tcg_res[pass], tcg_op1, tcg_op2_wide);
11040 tcg_temp_free_i64(tcg_op1);
11041 tcg_temp_free_i64(tcg_op2_wide);
11044 for (pass = 0; pass < 2; pass++) {
11045 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
11046 tcg_temp_free_i64(tcg_res[pass]);
11050 static void do_narrow_round_high_u32(TCGv_i32 res, TCGv_i64 in)
11052 tcg_gen_addi_i64(in, in, 1U << 31);
11053 tcg_gen_extrh_i64_i32(res, in);
11056 static void handle_3rd_narrowing(DisasContext *s, int is_q, int is_u, int size,
11057 int opcode, int rd, int rn, int rm)
11059 TCGv_i32 tcg_res[2];
11060 int part = is_q ? 2 : 0;
11061 int pass;
11063 for (pass = 0; pass < 2; pass++) {
11064 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
11065 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
11066 TCGv_i64 tcg_wideres = tcg_temp_new_i64();
11067 static NeonGenNarrowFn * const narrowfns[3][2] = {
11068 { gen_helper_neon_narrow_high_u8,
11069 gen_helper_neon_narrow_round_high_u8 },
11070 { gen_helper_neon_narrow_high_u16,
11071 gen_helper_neon_narrow_round_high_u16 },
11072 { tcg_gen_extrh_i64_i32, do_narrow_round_high_u32 },
11074 NeonGenNarrowFn *gennarrow = narrowfns[size][is_u];
11076 read_vec_element(s, tcg_op1, rn, pass, MO_64);
11077 read_vec_element(s, tcg_op2, rm, pass, MO_64);
11079 gen_neon_addl(size, (opcode == 6), tcg_wideres, tcg_op1, tcg_op2);
11081 tcg_temp_free_i64(tcg_op1);
11082 tcg_temp_free_i64(tcg_op2);
11084 tcg_res[pass] = tcg_temp_new_i32();
11085 gennarrow(tcg_res[pass], tcg_wideres);
11086 tcg_temp_free_i64(tcg_wideres);
11089 for (pass = 0; pass < 2; pass++) {
11090 write_vec_element_i32(s, tcg_res[pass], rd, pass + part, MO_32);
11091 tcg_temp_free_i32(tcg_res[pass]);
11093 clear_vec_high(s, is_q, rd);
11096 /* AdvSIMD three different
11097 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 0
11098 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
11099 * | 0 | Q | U | 0 1 1 1 0 | size | 1 | Rm | opcode | 0 0 | Rn | Rd |
11100 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
11102 static void disas_simd_three_reg_diff(DisasContext *s, uint32_t insn)
11104 /* Instructions in this group fall into three basic classes
11105 * (in each case with the operation working on each element in
11106 * the input vectors):
11107 * (1) widening 64 x 64 -> 128 (with possibly Vd as an extra
11108 * 128 bit input)
11109 * (2) wide 64 x 128 -> 128
11110 * (3) narrowing 128 x 128 -> 64
11111 * Here we do initial decode, catch unallocated cases and
11112 * dispatch to separate functions for each class.
11114 int is_q = extract32(insn, 30, 1);
11115 int is_u = extract32(insn, 29, 1);
11116 int size = extract32(insn, 22, 2);
11117 int opcode = extract32(insn, 12, 4);
11118 int rm = extract32(insn, 16, 5);
11119 int rn = extract32(insn, 5, 5);
11120 int rd = extract32(insn, 0, 5);
11122 switch (opcode) {
11123 case 1: /* SADDW, SADDW2, UADDW, UADDW2 */
11124 case 3: /* SSUBW, SSUBW2, USUBW, USUBW2 */
11125 /* 64 x 128 -> 128 */
11126 if (size == 3) {
11127 unallocated_encoding(s);
11128 return;
11130 if (!fp_access_check(s)) {
11131 return;
11133 handle_3rd_wide(s, is_q, is_u, size, opcode, rd, rn, rm);
11134 break;
11135 case 4: /* ADDHN, ADDHN2, RADDHN, RADDHN2 */
11136 case 6: /* SUBHN, SUBHN2, RSUBHN, RSUBHN2 */
11137 /* 128 x 128 -> 64 */
11138 if (size == 3) {
11139 unallocated_encoding(s);
11140 return;
11142 if (!fp_access_check(s)) {
11143 return;
11145 handle_3rd_narrowing(s, is_q, is_u, size, opcode, rd, rn, rm);
11146 break;
11147 case 14: /* PMULL, PMULL2 */
11148 if (is_u) {
11149 unallocated_encoding(s);
11150 return;
11152 switch (size) {
11153 case 0: /* PMULL.P8 */
11154 if (!fp_access_check(s)) {
11155 return;
11157 /* The Q field specifies lo/hi half input for this insn. */
11158 gen_gvec_op3_ool(s, true, rd, rn, rm, is_q,
11159 gen_helper_neon_pmull_h);
11160 break;
11162 case 3: /* PMULL.P64 */
11163 if (!dc_isar_feature(aa64_pmull, s)) {
11164 unallocated_encoding(s);
11165 return;
11167 if (!fp_access_check(s)) {
11168 return;
11170 /* The Q field specifies lo/hi half input for this insn. */
11171 gen_gvec_op3_ool(s, true, rd, rn, rm, is_q,
11172 gen_helper_gvec_pmull_q);
11173 break;
11175 default:
11176 unallocated_encoding(s);
11177 break;
11179 return;
11180 case 9: /* SQDMLAL, SQDMLAL2 */
11181 case 11: /* SQDMLSL, SQDMLSL2 */
11182 case 13: /* SQDMULL, SQDMULL2 */
11183 if (is_u || size == 0) {
11184 unallocated_encoding(s);
11185 return;
11187 /* fall through */
11188 case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
11189 case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
11190 case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
11191 case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
11192 case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
11193 case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
11194 case 12: /* SMULL, SMULL2, UMULL, UMULL2 */
11195 /* 64 x 64 -> 128 */
11196 if (size == 3) {
11197 unallocated_encoding(s);
11198 return;
11200 if (!fp_access_check(s)) {
11201 return;
11204 handle_3rd_widening(s, is_q, is_u, size, opcode, rd, rn, rm);
11205 break;
11206 default:
11207 /* opcode 15 not allocated */
11208 unallocated_encoding(s);
11209 break;
11213 /* Logic op (opcode == 3) subgroup of C3.6.16. */
11214 static void disas_simd_3same_logic(DisasContext *s, uint32_t insn)
11216 int rd = extract32(insn, 0, 5);
11217 int rn = extract32(insn, 5, 5);
11218 int rm = extract32(insn, 16, 5);
11219 int size = extract32(insn, 22, 2);
11220 bool is_u = extract32(insn, 29, 1);
11221 bool is_q = extract32(insn, 30, 1);
11223 if (!fp_access_check(s)) {
11224 return;
11227 switch (size + 4 * is_u) {
11228 case 0: /* AND */
11229 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_and, 0);
11230 return;
11231 case 1: /* BIC */
11232 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_andc, 0);
11233 return;
11234 case 2: /* ORR */
11235 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_or, 0);
11236 return;
11237 case 3: /* ORN */
11238 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_orc, 0);
11239 return;
11240 case 4: /* EOR */
11241 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_xor, 0);
11242 return;
11244 case 5: /* BSL bitwise select */
11245 gen_gvec_fn4(s, is_q, rd, rd, rn, rm, tcg_gen_gvec_bitsel, 0);
11246 return;
11247 case 6: /* BIT, bitwise insert if true */
11248 gen_gvec_fn4(s, is_q, rd, rm, rn, rd, tcg_gen_gvec_bitsel, 0);
11249 return;
11250 case 7: /* BIF, bitwise insert if false */
11251 gen_gvec_fn4(s, is_q, rd, rm, rd, rn, tcg_gen_gvec_bitsel, 0);
11252 return;
11254 default:
11255 g_assert_not_reached();
11259 /* Pairwise op subgroup of C3.6.16.
11261 * This is called directly or via the handle_3same_float for float pairwise
11262 * operations where the opcode and size are calculated differently.
11264 static void handle_simd_3same_pair(DisasContext *s, int is_q, int u, int opcode,
11265 int size, int rn, int rm, int rd)
11267 TCGv_ptr fpst;
11268 int pass;
11270 /* Floating point operations need fpst */
11271 if (opcode >= 0x58) {
11272 fpst = fpstatus_ptr(FPST_FPCR);
11273 } else {
11274 fpst = NULL;
11277 if (!fp_access_check(s)) {
11278 return;
11281 /* These operations work on the concatenated rm:rn, with each pair of
11282 * adjacent elements being operated on to produce an element in the result.
11284 if (size == 3) {
11285 TCGv_i64 tcg_res[2];
11287 for (pass = 0; pass < 2; pass++) {
11288 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
11289 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
11290 int passreg = (pass == 0) ? rn : rm;
11292 read_vec_element(s, tcg_op1, passreg, 0, MO_64);
11293 read_vec_element(s, tcg_op2, passreg, 1, MO_64);
11294 tcg_res[pass] = tcg_temp_new_i64();
11296 switch (opcode) {
11297 case 0x17: /* ADDP */
11298 tcg_gen_add_i64(tcg_res[pass], tcg_op1, tcg_op2);
11299 break;
11300 case 0x58: /* FMAXNMP */
11301 gen_helper_vfp_maxnumd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11302 break;
11303 case 0x5a: /* FADDP */
11304 gen_helper_vfp_addd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11305 break;
11306 case 0x5e: /* FMAXP */
11307 gen_helper_vfp_maxd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11308 break;
11309 case 0x78: /* FMINNMP */
11310 gen_helper_vfp_minnumd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11311 break;
11312 case 0x7e: /* FMINP */
11313 gen_helper_vfp_mind(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11314 break;
11315 default:
11316 g_assert_not_reached();
11319 tcg_temp_free_i64(tcg_op1);
11320 tcg_temp_free_i64(tcg_op2);
11323 for (pass = 0; pass < 2; pass++) {
11324 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
11325 tcg_temp_free_i64(tcg_res[pass]);
11327 } else {
11328 int maxpass = is_q ? 4 : 2;
11329 TCGv_i32 tcg_res[4];
11331 for (pass = 0; pass < maxpass; pass++) {
11332 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11333 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11334 NeonGenTwoOpFn *genfn = NULL;
11335 int passreg = pass < (maxpass / 2) ? rn : rm;
11336 int passelt = (is_q && (pass & 1)) ? 2 : 0;
11338 read_vec_element_i32(s, tcg_op1, passreg, passelt, MO_32);
11339 read_vec_element_i32(s, tcg_op2, passreg, passelt + 1, MO_32);
11340 tcg_res[pass] = tcg_temp_new_i32();
11342 switch (opcode) {
11343 case 0x17: /* ADDP */
11345 static NeonGenTwoOpFn * const fns[3] = {
11346 gen_helper_neon_padd_u8,
11347 gen_helper_neon_padd_u16,
11348 tcg_gen_add_i32,
11350 genfn = fns[size];
11351 break;
11353 case 0x14: /* SMAXP, UMAXP */
11355 static NeonGenTwoOpFn * const fns[3][2] = {
11356 { gen_helper_neon_pmax_s8, gen_helper_neon_pmax_u8 },
11357 { gen_helper_neon_pmax_s16, gen_helper_neon_pmax_u16 },
11358 { tcg_gen_smax_i32, tcg_gen_umax_i32 },
11360 genfn = fns[size][u];
11361 break;
11363 case 0x15: /* SMINP, UMINP */
11365 static NeonGenTwoOpFn * const fns[3][2] = {
11366 { gen_helper_neon_pmin_s8, gen_helper_neon_pmin_u8 },
11367 { gen_helper_neon_pmin_s16, gen_helper_neon_pmin_u16 },
11368 { tcg_gen_smin_i32, tcg_gen_umin_i32 },
11370 genfn = fns[size][u];
11371 break;
11373 /* The FP operations are all on single floats (32 bit) */
11374 case 0x58: /* FMAXNMP */
11375 gen_helper_vfp_maxnums(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11376 break;
11377 case 0x5a: /* FADDP */
11378 gen_helper_vfp_adds(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11379 break;
11380 case 0x5e: /* FMAXP */
11381 gen_helper_vfp_maxs(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11382 break;
11383 case 0x78: /* FMINNMP */
11384 gen_helper_vfp_minnums(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11385 break;
11386 case 0x7e: /* FMINP */
11387 gen_helper_vfp_mins(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11388 break;
11389 default:
11390 g_assert_not_reached();
11393 /* FP ops called directly, otherwise call now */
11394 if (genfn) {
11395 genfn(tcg_res[pass], tcg_op1, tcg_op2);
11398 tcg_temp_free_i32(tcg_op1);
11399 tcg_temp_free_i32(tcg_op2);
11402 for (pass = 0; pass < maxpass; pass++) {
11403 write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_32);
11404 tcg_temp_free_i32(tcg_res[pass]);
11406 clear_vec_high(s, is_q, rd);
11409 if (fpst) {
11410 tcg_temp_free_ptr(fpst);
11414 /* Floating point op subgroup of C3.6.16. */
11415 static void disas_simd_3same_float(DisasContext *s, uint32_t insn)
11417 /* For floating point ops, the U, size[1] and opcode bits
11418 * together indicate the operation. size[0] indicates single
11419 * or double.
11421 int fpopcode = extract32(insn, 11, 5)
11422 | (extract32(insn, 23, 1) << 5)
11423 | (extract32(insn, 29, 1) << 6);
11424 int is_q = extract32(insn, 30, 1);
11425 int size = extract32(insn, 22, 1);
11426 int rm = extract32(insn, 16, 5);
11427 int rn = extract32(insn, 5, 5);
11428 int rd = extract32(insn, 0, 5);
11430 int datasize = is_q ? 128 : 64;
11431 int esize = 32 << size;
11432 int elements = datasize / esize;
11434 if (size == 1 && !is_q) {
11435 unallocated_encoding(s);
11436 return;
11439 switch (fpopcode) {
11440 case 0x58: /* FMAXNMP */
11441 case 0x5a: /* FADDP */
11442 case 0x5e: /* FMAXP */
11443 case 0x78: /* FMINNMP */
11444 case 0x7e: /* FMINP */
11445 if (size && !is_q) {
11446 unallocated_encoding(s);
11447 return;
11449 handle_simd_3same_pair(s, is_q, 0, fpopcode, size ? MO_64 : MO_32,
11450 rn, rm, rd);
11451 return;
11452 case 0x1b: /* FMULX */
11453 case 0x1f: /* FRECPS */
11454 case 0x3f: /* FRSQRTS */
11455 case 0x5d: /* FACGE */
11456 case 0x7d: /* FACGT */
11457 case 0x19: /* FMLA */
11458 case 0x39: /* FMLS */
11459 case 0x18: /* FMAXNM */
11460 case 0x1a: /* FADD */
11461 case 0x1c: /* FCMEQ */
11462 case 0x1e: /* FMAX */
11463 case 0x38: /* FMINNM */
11464 case 0x3a: /* FSUB */
11465 case 0x3e: /* FMIN */
11466 case 0x5b: /* FMUL */
11467 case 0x5c: /* FCMGE */
11468 case 0x5f: /* FDIV */
11469 case 0x7a: /* FABD */
11470 case 0x7c: /* FCMGT */
11471 if (!fp_access_check(s)) {
11472 return;
11474 handle_3same_float(s, size, elements, fpopcode, rd, rn, rm);
11475 return;
11477 case 0x1d: /* FMLAL */
11478 case 0x3d: /* FMLSL */
11479 case 0x59: /* FMLAL2 */
11480 case 0x79: /* FMLSL2 */
11481 if (size & 1 || !dc_isar_feature(aa64_fhm, s)) {
11482 unallocated_encoding(s);
11483 return;
11485 if (fp_access_check(s)) {
11486 int is_s = extract32(insn, 23, 1);
11487 int is_2 = extract32(insn, 29, 1);
11488 int data = (is_2 << 1) | is_s;
11489 tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
11490 vec_full_reg_offset(s, rn),
11491 vec_full_reg_offset(s, rm), cpu_env,
11492 is_q ? 16 : 8, vec_full_reg_size(s),
11493 data, gen_helper_gvec_fmlal_a64);
11495 return;
11497 default:
11498 unallocated_encoding(s);
11499 return;
11503 /* Integer op subgroup of C3.6.16. */
11504 static void disas_simd_3same_int(DisasContext *s, uint32_t insn)
11506 int is_q = extract32(insn, 30, 1);
11507 int u = extract32(insn, 29, 1);
11508 int size = extract32(insn, 22, 2);
11509 int opcode = extract32(insn, 11, 5);
11510 int rm = extract32(insn, 16, 5);
11511 int rn = extract32(insn, 5, 5);
11512 int rd = extract32(insn, 0, 5);
11513 int pass;
11514 TCGCond cond;
11516 switch (opcode) {
11517 case 0x13: /* MUL, PMUL */
11518 if (u && size != 0) {
11519 unallocated_encoding(s);
11520 return;
11522 /* fall through */
11523 case 0x0: /* SHADD, UHADD */
11524 case 0x2: /* SRHADD, URHADD */
11525 case 0x4: /* SHSUB, UHSUB */
11526 case 0xc: /* SMAX, UMAX */
11527 case 0xd: /* SMIN, UMIN */
11528 case 0xe: /* SABD, UABD */
11529 case 0xf: /* SABA, UABA */
11530 case 0x12: /* MLA, MLS */
11531 if (size == 3) {
11532 unallocated_encoding(s);
11533 return;
11535 break;
11536 case 0x16: /* SQDMULH, SQRDMULH */
11537 if (size == 0 || size == 3) {
11538 unallocated_encoding(s);
11539 return;
11541 break;
11542 default:
11543 if (size == 3 && !is_q) {
11544 unallocated_encoding(s);
11545 return;
11547 break;
11550 if (!fp_access_check(s)) {
11551 return;
11554 switch (opcode) {
11555 case 0x01: /* SQADD, UQADD */
11556 if (u) {
11557 gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uqadd_qc, size);
11558 } else {
11559 gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqadd_qc, size);
11561 return;
11562 case 0x05: /* SQSUB, UQSUB */
11563 if (u) {
11564 gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uqsub_qc, size);
11565 } else {
11566 gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqsub_qc, size);
11568 return;
11569 case 0x08: /* SSHL, USHL */
11570 if (u) {
11571 gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_ushl, size);
11572 } else {
11573 gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sshl, size);
11575 return;
11576 case 0x0c: /* SMAX, UMAX */
11577 if (u) {
11578 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_umax, size);
11579 } else {
11580 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_smax, size);
11582 return;
11583 case 0x0d: /* SMIN, UMIN */
11584 if (u) {
11585 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_umin, size);
11586 } else {
11587 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_smin, size);
11589 return;
11590 case 0xe: /* SABD, UABD */
11591 if (u) {
11592 gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uabd, size);
11593 } else {
11594 gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sabd, size);
11596 return;
11597 case 0xf: /* SABA, UABA */
11598 if (u) {
11599 gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uaba, size);
11600 } else {
11601 gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_saba, size);
11603 return;
11604 case 0x10: /* ADD, SUB */
11605 if (u) {
11606 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_sub, size);
11607 } else {
11608 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_add, size);
11610 return;
11611 case 0x13: /* MUL, PMUL */
11612 if (!u) { /* MUL */
11613 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_mul, size);
11614 } else { /* PMUL */
11615 gen_gvec_op3_ool(s, is_q, rd, rn, rm, 0, gen_helper_gvec_pmul_b);
11617 return;
11618 case 0x12: /* MLA, MLS */
11619 if (u) {
11620 gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_mls, size);
11621 } else {
11622 gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_mla, size);
11624 return;
11625 case 0x16: /* SQDMULH, SQRDMULH */
11627 static gen_helper_gvec_3_ptr * const fns[2][2] = {
11628 { gen_helper_neon_sqdmulh_h, gen_helper_neon_sqrdmulh_h },
11629 { gen_helper_neon_sqdmulh_s, gen_helper_neon_sqrdmulh_s },
11631 gen_gvec_op3_qc(s, is_q, rd, rn, rm, fns[size - 1][u]);
11633 return;
11634 case 0x11:
11635 if (!u) { /* CMTST */
11636 gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_cmtst, size);
11637 return;
11639 /* else CMEQ */
11640 cond = TCG_COND_EQ;
11641 goto do_gvec_cmp;
11642 case 0x06: /* CMGT, CMHI */
11643 cond = u ? TCG_COND_GTU : TCG_COND_GT;
11644 goto do_gvec_cmp;
11645 case 0x07: /* CMGE, CMHS */
11646 cond = u ? TCG_COND_GEU : TCG_COND_GE;
11647 do_gvec_cmp:
11648 tcg_gen_gvec_cmp(cond, size, vec_full_reg_offset(s, rd),
11649 vec_full_reg_offset(s, rn),
11650 vec_full_reg_offset(s, rm),
11651 is_q ? 16 : 8, vec_full_reg_size(s));
11652 return;
11655 if (size == 3) {
11656 assert(is_q);
11657 for (pass = 0; pass < 2; pass++) {
11658 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
11659 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
11660 TCGv_i64 tcg_res = tcg_temp_new_i64();
11662 read_vec_element(s, tcg_op1, rn, pass, MO_64);
11663 read_vec_element(s, tcg_op2, rm, pass, MO_64);
11665 handle_3same_64(s, opcode, u, tcg_res, tcg_op1, tcg_op2);
11667 write_vec_element(s, tcg_res, rd, pass, MO_64);
11669 tcg_temp_free_i64(tcg_res);
11670 tcg_temp_free_i64(tcg_op1);
11671 tcg_temp_free_i64(tcg_op2);
11673 } else {
11674 for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
11675 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11676 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11677 TCGv_i32 tcg_res = tcg_temp_new_i32();
11678 NeonGenTwoOpFn *genfn = NULL;
11679 NeonGenTwoOpEnvFn *genenvfn = NULL;
11681 read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
11682 read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
11684 switch (opcode) {
11685 case 0x0: /* SHADD, UHADD */
11687 static NeonGenTwoOpFn * const fns[3][2] = {
11688 { gen_helper_neon_hadd_s8, gen_helper_neon_hadd_u8 },
11689 { gen_helper_neon_hadd_s16, gen_helper_neon_hadd_u16 },
11690 { gen_helper_neon_hadd_s32, gen_helper_neon_hadd_u32 },
11692 genfn = fns[size][u];
11693 break;
11695 case 0x2: /* SRHADD, URHADD */
11697 static NeonGenTwoOpFn * const fns[3][2] = {
11698 { gen_helper_neon_rhadd_s8, gen_helper_neon_rhadd_u8 },
11699 { gen_helper_neon_rhadd_s16, gen_helper_neon_rhadd_u16 },
11700 { gen_helper_neon_rhadd_s32, gen_helper_neon_rhadd_u32 },
11702 genfn = fns[size][u];
11703 break;
11705 case 0x4: /* SHSUB, UHSUB */
11707 static NeonGenTwoOpFn * const fns[3][2] = {
11708 { gen_helper_neon_hsub_s8, gen_helper_neon_hsub_u8 },
11709 { gen_helper_neon_hsub_s16, gen_helper_neon_hsub_u16 },
11710 { gen_helper_neon_hsub_s32, gen_helper_neon_hsub_u32 },
11712 genfn = fns[size][u];
11713 break;
11715 case 0x9: /* SQSHL, UQSHL */
11717 static NeonGenTwoOpEnvFn * const fns[3][2] = {
11718 { gen_helper_neon_qshl_s8, gen_helper_neon_qshl_u8 },
11719 { gen_helper_neon_qshl_s16, gen_helper_neon_qshl_u16 },
11720 { gen_helper_neon_qshl_s32, gen_helper_neon_qshl_u32 },
11722 genenvfn = fns[size][u];
11723 break;
11725 case 0xa: /* SRSHL, URSHL */
11727 static NeonGenTwoOpFn * const fns[3][2] = {
11728 { gen_helper_neon_rshl_s8, gen_helper_neon_rshl_u8 },
11729 { gen_helper_neon_rshl_s16, gen_helper_neon_rshl_u16 },
11730 { gen_helper_neon_rshl_s32, gen_helper_neon_rshl_u32 },
11732 genfn = fns[size][u];
11733 break;
11735 case 0xb: /* SQRSHL, UQRSHL */
11737 static NeonGenTwoOpEnvFn * const fns[3][2] = {
11738 { gen_helper_neon_qrshl_s8, gen_helper_neon_qrshl_u8 },
11739 { gen_helper_neon_qrshl_s16, gen_helper_neon_qrshl_u16 },
11740 { gen_helper_neon_qrshl_s32, gen_helper_neon_qrshl_u32 },
11742 genenvfn = fns[size][u];
11743 break;
11745 default:
11746 g_assert_not_reached();
11749 if (genenvfn) {
11750 genenvfn(tcg_res, cpu_env, tcg_op1, tcg_op2);
11751 } else {
11752 genfn(tcg_res, tcg_op1, tcg_op2);
11755 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
11757 tcg_temp_free_i32(tcg_res);
11758 tcg_temp_free_i32(tcg_op1);
11759 tcg_temp_free_i32(tcg_op2);
11762 clear_vec_high(s, is_q, rd);
11765 /* AdvSIMD three same
11766 * 31 30 29 28 24 23 22 21 20 16 15 11 10 9 5 4 0
11767 * +---+---+---+-----------+------+---+------+--------+---+------+------+
11768 * | 0 | Q | U | 0 1 1 1 0 | size | 1 | Rm | opcode | 1 | Rn | Rd |
11769 * +---+---+---+-----------+------+---+------+--------+---+------+------+
11771 static void disas_simd_three_reg_same(DisasContext *s, uint32_t insn)
11773 int opcode = extract32(insn, 11, 5);
11775 switch (opcode) {
11776 case 0x3: /* logic ops */
11777 disas_simd_3same_logic(s, insn);
11778 break;
11779 case 0x17: /* ADDP */
11780 case 0x14: /* SMAXP, UMAXP */
11781 case 0x15: /* SMINP, UMINP */
11783 /* Pairwise operations */
11784 int is_q = extract32(insn, 30, 1);
11785 int u = extract32(insn, 29, 1);
11786 int size = extract32(insn, 22, 2);
11787 int rm = extract32(insn, 16, 5);
11788 int rn = extract32(insn, 5, 5);
11789 int rd = extract32(insn, 0, 5);
11790 if (opcode == 0x17) {
11791 if (u || (size == 3 && !is_q)) {
11792 unallocated_encoding(s);
11793 return;
11795 } else {
11796 if (size == 3) {
11797 unallocated_encoding(s);
11798 return;
11801 handle_simd_3same_pair(s, is_q, u, opcode, size, rn, rm, rd);
11802 break;
11804 case 0x18 ... 0x31:
11805 /* floating point ops, sz[1] and U are part of opcode */
11806 disas_simd_3same_float(s, insn);
11807 break;
11808 default:
11809 disas_simd_3same_int(s, insn);
11810 break;
11815 * Advanced SIMD three same (ARMv8.2 FP16 variants)
11817 * 31 30 29 28 24 23 22 21 20 16 15 14 13 11 10 9 5 4 0
11818 * +---+---+---+-----------+---------+------+-----+--------+---+------+------+
11819 * | 0 | Q | U | 0 1 1 1 0 | a | 1 0 | Rm | 0 0 | opcode | 1 | Rn | Rd |
11820 * +---+---+---+-----------+---------+------+-----+--------+---+------+------+
11822 * This includes FMULX, FCMEQ (register), FRECPS, FRSQRTS, FCMGE
11823 * (register), FACGE, FABD, FCMGT (register) and FACGT.
11826 static void disas_simd_three_reg_same_fp16(DisasContext *s, uint32_t insn)
11828 int opcode = extract32(insn, 11, 3);
11829 int u = extract32(insn, 29, 1);
11830 int a = extract32(insn, 23, 1);
11831 int is_q = extract32(insn, 30, 1);
11832 int rm = extract32(insn, 16, 5);
11833 int rn = extract32(insn, 5, 5);
11834 int rd = extract32(insn, 0, 5);
11836 * For these floating point ops, the U, a and opcode bits
11837 * together indicate the operation.
11839 int fpopcode = opcode | (a << 3) | (u << 4);
11840 int datasize = is_q ? 128 : 64;
11841 int elements = datasize / 16;
11842 bool pairwise;
11843 TCGv_ptr fpst;
11844 int pass;
11846 switch (fpopcode) {
11847 case 0x0: /* FMAXNM */
11848 case 0x1: /* FMLA */
11849 case 0x2: /* FADD */
11850 case 0x3: /* FMULX */
11851 case 0x4: /* FCMEQ */
11852 case 0x6: /* FMAX */
11853 case 0x7: /* FRECPS */
11854 case 0x8: /* FMINNM */
11855 case 0x9: /* FMLS */
11856 case 0xa: /* FSUB */
11857 case 0xe: /* FMIN */
11858 case 0xf: /* FRSQRTS */
11859 case 0x13: /* FMUL */
11860 case 0x14: /* FCMGE */
11861 case 0x15: /* FACGE */
11862 case 0x17: /* FDIV */
11863 case 0x1a: /* FABD */
11864 case 0x1c: /* FCMGT */
11865 case 0x1d: /* FACGT */
11866 pairwise = false;
11867 break;
11868 case 0x10: /* FMAXNMP */
11869 case 0x12: /* FADDP */
11870 case 0x16: /* FMAXP */
11871 case 0x18: /* FMINNMP */
11872 case 0x1e: /* FMINP */
11873 pairwise = true;
11874 break;
11875 default:
11876 unallocated_encoding(s);
11877 return;
11880 if (!dc_isar_feature(aa64_fp16, s)) {
11881 unallocated_encoding(s);
11882 return;
11885 if (!fp_access_check(s)) {
11886 return;
11889 fpst = fpstatus_ptr(FPST_FPCR_F16);
11891 if (pairwise) {
11892 int maxpass = is_q ? 8 : 4;
11893 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11894 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11895 TCGv_i32 tcg_res[8];
11897 for (pass = 0; pass < maxpass; pass++) {
11898 int passreg = pass < (maxpass / 2) ? rn : rm;
11899 int passelt = (pass << 1) & (maxpass - 1);
11901 read_vec_element_i32(s, tcg_op1, passreg, passelt, MO_16);
11902 read_vec_element_i32(s, tcg_op2, passreg, passelt + 1, MO_16);
11903 tcg_res[pass] = tcg_temp_new_i32();
11905 switch (fpopcode) {
11906 case 0x10: /* FMAXNMP */
11907 gen_helper_advsimd_maxnumh(tcg_res[pass], tcg_op1, tcg_op2,
11908 fpst);
11909 break;
11910 case 0x12: /* FADDP */
11911 gen_helper_advsimd_addh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11912 break;
11913 case 0x16: /* FMAXP */
11914 gen_helper_advsimd_maxh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11915 break;
11916 case 0x18: /* FMINNMP */
11917 gen_helper_advsimd_minnumh(tcg_res[pass], tcg_op1, tcg_op2,
11918 fpst);
11919 break;
11920 case 0x1e: /* FMINP */
11921 gen_helper_advsimd_minh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11922 break;
11923 default:
11924 g_assert_not_reached();
11928 for (pass = 0; pass < maxpass; pass++) {
11929 write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_16);
11930 tcg_temp_free_i32(tcg_res[pass]);
11933 tcg_temp_free_i32(tcg_op1);
11934 tcg_temp_free_i32(tcg_op2);
11936 } else {
11937 for (pass = 0; pass < elements; pass++) {
11938 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11939 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11940 TCGv_i32 tcg_res = tcg_temp_new_i32();
11942 read_vec_element_i32(s, tcg_op1, rn, pass, MO_16);
11943 read_vec_element_i32(s, tcg_op2, rm, pass, MO_16);
11945 switch (fpopcode) {
11946 case 0x0: /* FMAXNM */
11947 gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
11948 break;
11949 case 0x1: /* FMLA */
11950 read_vec_element_i32(s, tcg_res, rd, pass, MO_16);
11951 gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_res,
11952 fpst);
11953 break;
11954 case 0x2: /* FADD */
11955 gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
11956 break;
11957 case 0x3: /* FMULX */
11958 gen_helper_advsimd_mulxh(tcg_res, tcg_op1, tcg_op2, fpst);
11959 break;
11960 case 0x4: /* FCMEQ */
11961 gen_helper_advsimd_ceq_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11962 break;
11963 case 0x6: /* FMAX */
11964 gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
11965 break;
11966 case 0x7: /* FRECPS */
11967 gen_helper_recpsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11968 break;
11969 case 0x8: /* FMINNM */
11970 gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
11971 break;
11972 case 0x9: /* FMLS */
11973 /* As usual for ARM, separate negation for fused multiply-add */
11974 tcg_gen_xori_i32(tcg_op1, tcg_op1, 0x8000);
11975 read_vec_element_i32(s, tcg_res, rd, pass, MO_16);
11976 gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_res,
11977 fpst);
11978 break;
11979 case 0xa: /* FSUB */
11980 gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
11981 break;
11982 case 0xe: /* FMIN */
11983 gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
11984 break;
11985 case 0xf: /* FRSQRTS */
11986 gen_helper_rsqrtsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11987 break;
11988 case 0x13: /* FMUL */
11989 gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
11990 break;
11991 case 0x14: /* FCMGE */
11992 gen_helper_advsimd_cge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11993 break;
11994 case 0x15: /* FACGE */
11995 gen_helper_advsimd_acge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11996 break;
11997 case 0x17: /* FDIV */
11998 gen_helper_advsimd_divh(tcg_res, tcg_op1, tcg_op2, fpst);
11999 break;
12000 case 0x1a: /* FABD */
12001 gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
12002 tcg_gen_andi_i32(tcg_res, tcg_res, 0x7fff);
12003 break;
12004 case 0x1c: /* FCMGT */
12005 gen_helper_advsimd_cgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
12006 break;
12007 case 0x1d: /* FACGT */
12008 gen_helper_advsimd_acgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
12009 break;
12010 default:
12011 g_assert_not_reached();
12014 write_vec_element_i32(s, tcg_res, rd, pass, MO_16);
12015 tcg_temp_free_i32(tcg_res);
12016 tcg_temp_free_i32(tcg_op1);
12017 tcg_temp_free_i32(tcg_op2);
12021 tcg_temp_free_ptr(fpst);
12023 clear_vec_high(s, is_q, rd);
12026 /* AdvSIMD three same extra
12027 * 31 30 29 28 24 23 22 21 20 16 15 14 11 10 9 5 4 0
12028 * +---+---+---+-----------+------+---+------+---+--------+---+----+----+
12029 * | 0 | Q | U | 0 1 1 1 0 | size | 0 | Rm | 1 | opcode | 1 | Rn | Rd |
12030 * +---+---+---+-----------+------+---+------+---+--------+---+----+----+
12032 static void disas_simd_three_reg_same_extra(DisasContext *s, uint32_t insn)
12034 int rd = extract32(insn, 0, 5);
12035 int rn = extract32(insn, 5, 5);
12036 int opcode = extract32(insn, 11, 4);
12037 int rm = extract32(insn, 16, 5);
12038 int size = extract32(insn, 22, 2);
12039 bool u = extract32(insn, 29, 1);
12040 bool is_q = extract32(insn, 30, 1);
12041 bool feature;
12042 int rot;
12044 switch (u * 16 + opcode) {
12045 case 0x10: /* SQRDMLAH (vector) */
12046 case 0x11: /* SQRDMLSH (vector) */
12047 if (size != 1 && size != 2) {
12048 unallocated_encoding(s);
12049 return;
12051 feature = dc_isar_feature(aa64_rdm, s);
12052 break;
12053 case 0x02: /* SDOT (vector) */
12054 case 0x12: /* UDOT (vector) */
12055 if (size != MO_32) {
12056 unallocated_encoding(s);
12057 return;
12059 feature = dc_isar_feature(aa64_dp, s);
12060 break;
12061 case 0x03: /* USDOT */
12062 if (size != MO_32) {
12063 unallocated_encoding(s);
12064 return;
12066 feature = dc_isar_feature(aa64_i8mm, s);
12067 break;
12068 case 0x04: /* SMMLA */
12069 case 0x14: /* UMMLA */
12070 case 0x05: /* USMMLA */
12071 if (!is_q || size != MO_32) {
12072 unallocated_encoding(s);
12073 return;
12075 feature = dc_isar_feature(aa64_i8mm, s);
12076 break;
12077 case 0x18: /* FCMLA, #0 */
12078 case 0x19: /* FCMLA, #90 */
12079 case 0x1a: /* FCMLA, #180 */
12080 case 0x1b: /* FCMLA, #270 */
12081 case 0x1c: /* FCADD, #90 */
12082 case 0x1e: /* FCADD, #270 */
12083 if (size == 0
12084 || (size == 1 && !dc_isar_feature(aa64_fp16, s))
12085 || (size == 3 && !is_q)) {
12086 unallocated_encoding(s);
12087 return;
12089 feature = dc_isar_feature(aa64_fcma, s);
12090 break;
12091 case 0x1d: /* BFMMLA */
12092 if (size != MO_16 || !is_q) {
12093 unallocated_encoding(s);
12094 return;
12096 feature = dc_isar_feature(aa64_bf16, s);
12097 break;
12098 case 0x1f:
12099 switch (size) {
12100 case 1: /* BFDOT */
12101 case 3: /* BFMLAL{B,T} */
12102 feature = dc_isar_feature(aa64_bf16, s);
12103 break;
12104 default:
12105 unallocated_encoding(s);
12106 return;
12108 break;
12109 default:
12110 unallocated_encoding(s);
12111 return;
12113 if (!feature) {
12114 unallocated_encoding(s);
12115 return;
12117 if (!fp_access_check(s)) {
12118 return;
12121 switch (opcode) {
12122 case 0x0: /* SQRDMLAH (vector) */
12123 gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqrdmlah_qc, size);
12124 return;
12126 case 0x1: /* SQRDMLSH (vector) */
12127 gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqrdmlsh_qc, size);
12128 return;
12130 case 0x2: /* SDOT / UDOT */
12131 gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0,
12132 u ? gen_helper_gvec_udot_b : gen_helper_gvec_sdot_b);
12133 return;
12135 case 0x3: /* USDOT */
12136 gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0, gen_helper_gvec_usdot_b);
12137 return;
12139 case 0x04: /* SMMLA, UMMLA */
12140 gen_gvec_op4_ool(s, 1, rd, rn, rm, rd, 0,
12141 u ? gen_helper_gvec_ummla_b
12142 : gen_helper_gvec_smmla_b);
12143 return;
12144 case 0x05: /* USMMLA */
12145 gen_gvec_op4_ool(s, 1, rd, rn, rm, rd, 0, gen_helper_gvec_usmmla_b);
12146 return;
12148 case 0x8: /* FCMLA, #0 */
12149 case 0x9: /* FCMLA, #90 */
12150 case 0xa: /* FCMLA, #180 */
12151 case 0xb: /* FCMLA, #270 */
12152 rot = extract32(opcode, 0, 2);
12153 switch (size) {
12154 case 1:
12155 gen_gvec_op4_fpst(s, is_q, rd, rn, rm, rd, true, rot,
12156 gen_helper_gvec_fcmlah);
12157 break;
12158 case 2:
12159 gen_gvec_op4_fpst(s, is_q, rd, rn, rm, rd, false, rot,
12160 gen_helper_gvec_fcmlas);
12161 break;
12162 case 3:
12163 gen_gvec_op4_fpst(s, is_q, rd, rn, rm, rd, false, rot,
12164 gen_helper_gvec_fcmlad);
12165 break;
12166 default:
12167 g_assert_not_reached();
12169 return;
12171 case 0xc: /* FCADD, #90 */
12172 case 0xe: /* FCADD, #270 */
12173 rot = extract32(opcode, 1, 1);
12174 switch (size) {
12175 case 1:
12176 gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
12177 gen_helper_gvec_fcaddh);
12178 break;
12179 case 2:
12180 gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
12181 gen_helper_gvec_fcadds);
12182 break;
12183 case 3:
12184 gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
12185 gen_helper_gvec_fcaddd);
12186 break;
12187 default:
12188 g_assert_not_reached();
12190 return;
12192 case 0xd: /* BFMMLA */
12193 gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0, gen_helper_gvec_bfmmla);
12194 return;
12195 case 0xf:
12196 switch (size) {
12197 case 1: /* BFDOT */
12198 gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0, gen_helper_gvec_bfdot);
12199 break;
12200 case 3: /* BFMLAL{B,T} */
12201 gen_gvec_op4_fpst(s, 1, rd, rn, rm, rd, false, is_q,
12202 gen_helper_gvec_bfmlal);
12203 break;
12204 default:
12205 g_assert_not_reached();
12207 return;
12209 default:
12210 g_assert_not_reached();
12214 static void handle_2misc_widening(DisasContext *s, int opcode, bool is_q,
12215 int size, int rn, int rd)
12217 /* Handle 2-reg-misc ops which are widening (so each size element
12218 * in the source becomes a 2*size element in the destination.
12219 * The only instruction like this is FCVTL.
12221 int pass;
12223 if (size == 3) {
12224 /* 32 -> 64 bit fp conversion */
12225 TCGv_i64 tcg_res[2];
12226 int srcelt = is_q ? 2 : 0;
12228 for (pass = 0; pass < 2; pass++) {
12229 TCGv_i32 tcg_op = tcg_temp_new_i32();
12230 tcg_res[pass] = tcg_temp_new_i64();
12232 read_vec_element_i32(s, tcg_op, rn, srcelt + pass, MO_32);
12233 gen_helper_vfp_fcvtds(tcg_res[pass], tcg_op, cpu_env);
12234 tcg_temp_free_i32(tcg_op);
12236 for (pass = 0; pass < 2; pass++) {
12237 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
12238 tcg_temp_free_i64(tcg_res[pass]);
12240 } else {
12241 /* 16 -> 32 bit fp conversion */
12242 int srcelt = is_q ? 4 : 0;
12243 TCGv_i32 tcg_res[4];
12244 TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
12245 TCGv_i32 ahp = get_ahp_flag();
12247 for (pass = 0; pass < 4; pass++) {
12248 tcg_res[pass] = tcg_temp_new_i32();
12250 read_vec_element_i32(s, tcg_res[pass], rn, srcelt + pass, MO_16);
12251 gen_helper_vfp_fcvt_f16_to_f32(tcg_res[pass], tcg_res[pass],
12252 fpst, ahp);
12254 for (pass = 0; pass < 4; pass++) {
12255 write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_32);
12256 tcg_temp_free_i32(tcg_res[pass]);
12259 tcg_temp_free_ptr(fpst);
12260 tcg_temp_free_i32(ahp);
12264 static void handle_rev(DisasContext *s, int opcode, bool u,
12265 bool is_q, int size, int rn, int rd)
12267 int op = (opcode << 1) | u;
12268 int opsz = op + size;
12269 int grp_size = 3 - opsz;
12270 int dsize = is_q ? 128 : 64;
12271 int i;
12273 if (opsz >= 3) {
12274 unallocated_encoding(s);
12275 return;
12278 if (!fp_access_check(s)) {
12279 return;
12282 if (size == 0) {
12283 /* Special case bytes, use bswap op on each group of elements */
12284 int groups = dsize / (8 << grp_size);
12286 for (i = 0; i < groups; i++) {
12287 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
12289 read_vec_element(s, tcg_tmp, rn, i, grp_size);
12290 switch (grp_size) {
12291 case MO_16:
12292 tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp, TCG_BSWAP_IZ);
12293 break;
12294 case MO_32:
12295 tcg_gen_bswap32_i64(tcg_tmp, tcg_tmp, TCG_BSWAP_IZ);
12296 break;
12297 case MO_64:
12298 tcg_gen_bswap64_i64(tcg_tmp, tcg_tmp);
12299 break;
12300 default:
12301 g_assert_not_reached();
12303 write_vec_element(s, tcg_tmp, rd, i, grp_size);
12304 tcg_temp_free_i64(tcg_tmp);
12306 clear_vec_high(s, is_q, rd);
12307 } else {
12308 int revmask = (1 << grp_size) - 1;
12309 int esize = 8 << size;
12310 int elements = dsize / esize;
12311 TCGv_i64 tcg_rn = tcg_temp_new_i64();
12312 TCGv_i64 tcg_rd = tcg_const_i64(0);
12313 TCGv_i64 tcg_rd_hi = tcg_const_i64(0);
12315 for (i = 0; i < elements; i++) {
12316 int e_rev = (i & 0xf) ^ revmask;
12317 int off = e_rev * esize;
12318 read_vec_element(s, tcg_rn, rn, i, size);
12319 if (off >= 64) {
12320 tcg_gen_deposit_i64(tcg_rd_hi, tcg_rd_hi,
12321 tcg_rn, off - 64, esize);
12322 } else {
12323 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, off, esize);
12326 write_vec_element(s, tcg_rd, rd, 0, MO_64);
12327 write_vec_element(s, tcg_rd_hi, rd, 1, MO_64);
12329 tcg_temp_free_i64(tcg_rd_hi);
12330 tcg_temp_free_i64(tcg_rd);
12331 tcg_temp_free_i64(tcg_rn);
12335 static void handle_2misc_pairwise(DisasContext *s, int opcode, bool u,
12336 bool is_q, int size, int rn, int rd)
12338 /* Implement the pairwise operations from 2-misc:
12339 * SADDLP, UADDLP, SADALP, UADALP.
12340 * These all add pairs of elements in the input to produce a
12341 * double-width result element in the output (possibly accumulating).
12343 bool accum = (opcode == 0x6);
12344 int maxpass = is_q ? 2 : 1;
12345 int pass;
12346 TCGv_i64 tcg_res[2];
12348 if (size == 2) {
12349 /* 32 + 32 -> 64 op */
12350 MemOp memop = size + (u ? 0 : MO_SIGN);
12352 for (pass = 0; pass < maxpass; pass++) {
12353 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
12354 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
12356 tcg_res[pass] = tcg_temp_new_i64();
12358 read_vec_element(s, tcg_op1, rn, pass * 2, memop);
12359 read_vec_element(s, tcg_op2, rn, pass * 2 + 1, memop);
12360 tcg_gen_add_i64(tcg_res[pass], tcg_op1, tcg_op2);
12361 if (accum) {
12362 read_vec_element(s, tcg_op1, rd, pass, MO_64);
12363 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
12366 tcg_temp_free_i64(tcg_op1);
12367 tcg_temp_free_i64(tcg_op2);
12369 } else {
12370 for (pass = 0; pass < maxpass; pass++) {
12371 TCGv_i64 tcg_op = tcg_temp_new_i64();
12372 NeonGenOne64OpFn *genfn;
12373 static NeonGenOne64OpFn * const fns[2][2] = {
12374 { gen_helper_neon_addlp_s8, gen_helper_neon_addlp_u8 },
12375 { gen_helper_neon_addlp_s16, gen_helper_neon_addlp_u16 },
12378 genfn = fns[size][u];
12380 tcg_res[pass] = tcg_temp_new_i64();
12382 read_vec_element(s, tcg_op, rn, pass, MO_64);
12383 genfn(tcg_res[pass], tcg_op);
12385 if (accum) {
12386 read_vec_element(s, tcg_op, rd, pass, MO_64);
12387 if (size == 0) {
12388 gen_helper_neon_addl_u16(tcg_res[pass],
12389 tcg_res[pass], tcg_op);
12390 } else {
12391 gen_helper_neon_addl_u32(tcg_res[pass],
12392 tcg_res[pass], tcg_op);
12395 tcg_temp_free_i64(tcg_op);
12398 if (!is_q) {
12399 tcg_res[1] = tcg_constant_i64(0);
12401 for (pass = 0; pass < 2; pass++) {
12402 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
12403 tcg_temp_free_i64(tcg_res[pass]);
12407 static void handle_shll(DisasContext *s, bool is_q, int size, int rn, int rd)
12409 /* Implement SHLL and SHLL2 */
12410 int pass;
12411 int part = is_q ? 2 : 0;
12412 TCGv_i64 tcg_res[2];
12414 for (pass = 0; pass < 2; pass++) {
12415 static NeonGenWidenFn * const widenfns[3] = {
12416 gen_helper_neon_widen_u8,
12417 gen_helper_neon_widen_u16,
12418 tcg_gen_extu_i32_i64,
12420 NeonGenWidenFn *widenfn = widenfns[size];
12421 TCGv_i32 tcg_op = tcg_temp_new_i32();
12423 read_vec_element_i32(s, tcg_op, rn, part + pass, MO_32);
12424 tcg_res[pass] = tcg_temp_new_i64();
12425 widenfn(tcg_res[pass], tcg_op);
12426 tcg_gen_shli_i64(tcg_res[pass], tcg_res[pass], 8 << size);
12428 tcg_temp_free_i32(tcg_op);
12431 for (pass = 0; pass < 2; pass++) {
12432 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
12433 tcg_temp_free_i64(tcg_res[pass]);
12437 /* AdvSIMD two reg misc
12438 * 31 30 29 28 24 23 22 21 17 16 12 11 10 9 5 4 0
12439 * +---+---+---+-----------+------+-----------+--------+-----+------+------+
12440 * | 0 | Q | U | 0 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 | Rn | Rd |
12441 * +---+---+---+-----------+------+-----------+--------+-----+------+------+
12443 static void disas_simd_two_reg_misc(DisasContext *s, uint32_t insn)
12445 int size = extract32(insn, 22, 2);
12446 int opcode = extract32(insn, 12, 5);
12447 bool u = extract32(insn, 29, 1);
12448 bool is_q = extract32(insn, 30, 1);
12449 int rn = extract32(insn, 5, 5);
12450 int rd = extract32(insn, 0, 5);
12451 bool need_fpstatus = false;
12452 bool need_rmode = false;
12453 int rmode = -1;
12454 TCGv_i32 tcg_rmode;
12455 TCGv_ptr tcg_fpstatus;
12457 switch (opcode) {
12458 case 0x0: /* REV64, REV32 */
12459 case 0x1: /* REV16 */
12460 handle_rev(s, opcode, u, is_q, size, rn, rd);
12461 return;
12462 case 0x5: /* CNT, NOT, RBIT */
12463 if (u && size == 0) {
12464 /* NOT */
12465 break;
12466 } else if (u && size == 1) {
12467 /* RBIT */
12468 break;
12469 } else if (!u && size == 0) {
12470 /* CNT */
12471 break;
12473 unallocated_encoding(s);
12474 return;
12475 case 0x12: /* XTN, XTN2, SQXTUN, SQXTUN2 */
12476 case 0x14: /* SQXTN, SQXTN2, UQXTN, UQXTN2 */
12477 if (size == 3) {
12478 unallocated_encoding(s);
12479 return;
12481 if (!fp_access_check(s)) {
12482 return;
12485 handle_2misc_narrow(s, false, opcode, u, is_q, size, rn, rd);
12486 return;
12487 case 0x4: /* CLS, CLZ */
12488 if (size == 3) {
12489 unallocated_encoding(s);
12490 return;
12492 break;
12493 case 0x2: /* SADDLP, UADDLP */
12494 case 0x6: /* SADALP, UADALP */
12495 if (size == 3) {
12496 unallocated_encoding(s);
12497 return;
12499 if (!fp_access_check(s)) {
12500 return;
12502 handle_2misc_pairwise(s, opcode, u, is_q, size, rn, rd);
12503 return;
12504 case 0x13: /* SHLL, SHLL2 */
12505 if (u == 0 || size == 3) {
12506 unallocated_encoding(s);
12507 return;
12509 if (!fp_access_check(s)) {
12510 return;
12512 handle_shll(s, is_q, size, rn, rd);
12513 return;
12514 case 0xa: /* CMLT */
12515 if (u == 1) {
12516 unallocated_encoding(s);
12517 return;
12519 /* fall through */
12520 case 0x8: /* CMGT, CMGE */
12521 case 0x9: /* CMEQ, CMLE */
12522 case 0xb: /* ABS, NEG */
12523 if (size == 3 && !is_q) {
12524 unallocated_encoding(s);
12525 return;
12527 break;
12528 case 0x3: /* SUQADD, USQADD */
12529 if (size == 3 && !is_q) {
12530 unallocated_encoding(s);
12531 return;
12533 if (!fp_access_check(s)) {
12534 return;
12536 handle_2misc_satacc(s, false, u, is_q, size, rn, rd);
12537 return;
12538 case 0x7: /* SQABS, SQNEG */
12539 if (size == 3 && !is_q) {
12540 unallocated_encoding(s);
12541 return;
12543 break;
12544 case 0xc ... 0xf:
12545 case 0x16 ... 0x1f:
12547 /* Floating point: U, size[1] and opcode indicate operation;
12548 * size[0] indicates single or double precision.
12550 int is_double = extract32(size, 0, 1);
12551 opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
12552 size = is_double ? 3 : 2;
12553 switch (opcode) {
12554 case 0x2f: /* FABS */
12555 case 0x6f: /* FNEG */
12556 if (size == 3 && !is_q) {
12557 unallocated_encoding(s);
12558 return;
12560 break;
12561 case 0x1d: /* SCVTF */
12562 case 0x5d: /* UCVTF */
12564 bool is_signed = (opcode == 0x1d) ? true : false;
12565 int elements = is_double ? 2 : is_q ? 4 : 2;
12566 if (is_double && !is_q) {
12567 unallocated_encoding(s);
12568 return;
12570 if (!fp_access_check(s)) {
12571 return;
12573 handle_simd_intfp_conv(s, rd, rn, elements, is_signed, 0, size);
12574 return;
12576 case 0x2c: /* FCMGT (zero) */
12577 case 0x2d: /* FCMEQ (zero) */
12578 case 0x2e: /* FCMLT (zero) */
12579 case 0x6c: /* FCMGE (zero) */
12580 case 0x6d: /* FCMLE (zero) */
12581 if (size == 3 && !is_q) {
12582 unallocated_encoding(s);
12583 return;
12585 handle_2misc_fcmp_zero(s, opcode, false, u, is_q, size, rn, rd);
12586 return;
12587 case 0x7f: /* FSQRT */
12588 if (size == 3 && !is_q) {
12589 unallocated_encoding(s);
12590 return;
12592 break;
12593 case 0x1a: /* FCVTNS */
12594 case 0x1b: /* FCVTMS */
12595 case 0x3a: /* FCVTPS */
12596 case 0x3b: /* FCVTZS */
12597 case 0x5a: /* FCVTNU */
12598 case 0x5b: /* FCVTMU */
12599 case 0x7a: /* FCVTPU */
12600 case 0x7b: /* FCVTZU */
12601 need_fpstatus = true;
12602 need_rmode = true;
12603 rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
12604 if (size == 3 && !is_q) {
12605 unallocated_encoding(s);
12606 return;
12608 break;
12609 case 0x5c: /* FCVTAU */
12610 case 0x1c: /* FCVTAS */
12611 need_fpstatus = true;
12612 need_rmode = true;
12613 rmode = FPROUNDING_TIEAWAY;
12614 if (size == 3 && !is_q) {
12615 unallocated_encoding(s);
12616 return;
12618 break;
12619 case 0x3c: /* URECPE */
12620 if (size == 3) {
12621 unallocated_encoding(s);
12622 return;
12624 /* fall through */
12625 case 0x3d: /* FRECPE */
12626 case 0x7d: /* FRSQRTE */
12627 if (size == 3 && !is_q) {
12628 unallocated_encoding(s);
12629 return;
12631 if (!fp_access_check(s)) {
12632 return;
12634 handle_2misc_reciprocal(s, opcode, false, u, is_q, size, rn, rd);
12635 return;
12636 case 0x56: /* FCVTXN, FCVTXN2 */
12637 if (size == 2) {
12638 unallocated_encoding(s);
12639 return;
12641 /* fall through */
12642 case 0x16: /* FCVTN, FCVTN2 */
12643 /* handle_2misc_narrow does a 2*size -> size operation, but these
12644 * instructions encode the source size rather than dest size.
12646 if (!fp_access_check(s)) {
12647 return;
12649 handle_2misc_narrow(s, false, opcode, 0, is_q, size - 1, rn, rd);
12650 return;
12651 case 0x36: /* BFCVTN, BFCVTN2 */
12652 if (!dc_isar_feature(aa64_bf16, s) || size != 2) {
12653 unallocated_encoding(s);
12654 return;
12656 if (!fp_access_check(s)) {
12657 return;
12659 handle_2misc_narrow(s, false, opcode, 0, is_q, size - 1, rn, rd);
12660 return;
12661 case 0x17: /* FCVTL, FCVTL2 */
12662 if (!fp_access_check(s)) {
12663 return;
12665 handle_2misc_widening(s, opcode, is_q, size, rn, rd);
12666 return;
12667 case 0x18: /* FRINTN */
12668 case 0x19: /* FRINTM */
12669 case 0x38: /* FRINTP */
12670 case 0x39: /* FRINTZ */
12671 need_rmode = true;
12672 rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
12673 /* fall through */
12674 case 0x59: /* FRINTX */
12675 case 0x79: /* FRINTI */
12676 need_fpstatus = true;
12677 if (size == 3 && !is_q) {
12678 unallocated_encoding(s);
12679 return;
12681 break;
12682 case 0x58: /* FRINTA */
12683 need_rmode = true;
12684 rmode = FPROUNDING_TIEAWAY;
12685 need_fpstatus = true;
12686 if (size == 3 && !is_q) {
12687 unallocated_encoding(s);
12688 return;
12690 break;
12691 case 0x7c: /* URSQRTE */
12692 if (size == 3) {
12693 unallocated_encoding(s);
12694 return;
12696 break;
12697 case 0x1e: /* FRINT32Z */
12698 case 0x1f: /* FRINT64Z */
12699 need_rmode = true;
12700 rmode = FPROUNDING_ZERO;
12701 /* fall through */
12702 case 0x5e: /* FRINT32X */
12703 case 0x5f: /* FRINT64X */
12704 need_fpstatus = true;
12705 if ((size == 3 && !is_q) || !dc_isar_feature(aa64_frint, s)) {
12706 unallocated_encoding(s);
12707 return;
12709 break;
12710 default:
12711 unallocated_encoding(s);
12712 return;
12714 break;
12716 default:
12717 unallocated_encoding(s);
12718 return;
12721 if (!fp_access_check(s)) {
12722 return;
12725 if (need_fpstatus || need_rmode) {
12726 tcg_fpstatus = fpstatus_ptr(FPST_FPCR);
12727 } else {
12728 tcg_fpstatus = NULL;
12730 if (need_rmode) {
12731 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rmode));
12732 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
12733 } else {
12734 tcg_rmode = NULL;
12737 switch (opcode) {
12738 case 0x5:
12739 if (u && size == 0) { /* NOT */
12740 gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_not, 0);
12741 return;
12743 break;
12744 case 0x8: /* CMGT, CMGE */
12745 if (u) {
12746 gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_cge0, size);
12747 } else {
12748 gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_cgt0, size);
12750 return;
12751 case 0x9: /* CMEQ, CMLE */
12752 if (u) {
12753 gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_cle0, size);
12754 } else {
12755 gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_ceq0, size);
12757 return;
12758 case 0xa: /* CMLT */
12759 gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_clt0, size);
12760 return;
12761 case 0xb:
12762 if (u) { /* ABS, NEG */
12763 gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_neg, size);
12764 } else {
12765 gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_abs, size);
12767 return;
12770 if (size == 3) {
12771 /* All 64-bit element operations can be shared with scalar 2misc */
12772 int pass;
12774 /* Coverity claims (size == 3 && !is_q) has been eliminated
12775 * from all paths leading to here.
12777 tcg_debug_assert(is_q);
12778 for (pass = 0; pass < 2; pass++) {
12779 TCGv_i64 tcg_op = tcg_temp_new_i64();
12780 TCGv_i64 tcg_res = tcg_temp_new_i64();
12782 read_vec_element(s, tcg_op, rn, pass, MO_64);
12784 handle_2misc_64(s, opcode, u, tcg_res, tcg_op,
12785 tcg_rmode, tcg_fpstatus);
12787 write_vec_element(s, tcg_res, rd, pass, MO_64);
12789 tcg_temp_free_i64(tcg_res);
12790 tcg_temp_free_i64(tcg_op);
12792 } else {
12793 int pass;
12795 for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
12796 TCGv_i32 tcg_op = tcg_temp_new_i32();
12797 TCGv_i32 tcg_res = tcg_temp_new_i32();
12799 read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
12801 if (size == 2) {
12802 /* Special cases for 32 bit elements */
12803 switch (opcode) {
12804 case 0x4: /* CLS */
12805 if (u) {
12806 tcg_gen_clzi_i32(tcg_res, tcg_op, 32);
12807 } else {
12808 tcg_gen_clrsb_i32(tcg_res, tcg_op);
12810 break;
12811 case 0x7: /* SQABS, SQNEG */
12812 if (u) {
12813 gen_helper_neon_qneg_s32(tcg_res, cpu_env, tcg_op);
12814 } else {
12815 gen_helper_neon_qabs_s32(tcg_res, cpu_env, tcg_op);
12817 break;
12818 case 0x2f: /* FABS */
12819 gen_helper_vfp_abss(tcg_res, tcg_op);
12820 break;
12821 case 0x6f: /* FNEG */
12822 gen_helper_vfp_negs(tcg_res, tcg_op);
12823 break;
12824 case 0x7f: /* FSQRT */
12825 gen_helper_vfp_sqrts(tcg_res, tcg_op, cpu_env);
12826 break;
12827 case 0x1a: /* FCVTNS */
12828 case 0x1b: /* FCVTMS */
12829 case 0x1c: /* FCVTAS */
12830 case 0x3a: /* FCVTPS */
12831 case 0x3b: /* FCVTZS */
12832 gen_helper_vfp_tosls(tcg_res, tcg_op,
12833 tcg_constant_i32(0), tcg_fpstatus);
12834 break;
12835 case 0x5a: /* FCVTNU */
12836 case 0x5b: /* FCVTMU */
12837 case 0x5c: /* FCVTAU */
12838 case 0x7a: /* FCVTPU */
12839 case 0x7b: /* FCVTZU */
12840 gen_helper_vfp_touls(tcg_res, tcg_op,
12841 tcg_constant_i32(0), tcg_fpstatus);
12842 break;
12843 case 0x18: /* FRINTN */
12844 case 0x19: /* FRINTM */
12845 case 0x38: /* FRINTP */
12846 case 0x39: /* FRINTZ */
12847 case 0x58: /* FRINTA */
12848 case 0x79: /* FRINTI */
12849 gen_helper_rints(tcg_res, tcg_op, tcg_fpstatus);
12850 break;
12851 case 0x59: /* FRINTX */
12852 gen_helper_rints_exact(tcg_res, tcg_op, tcg_fpstatus);
12853 break;
12854 case 0x7c: /* URSQRTE */
12855 gen_helper_rsqrte_u32(tcg_res, tcg_op);
12856 break;
12857 case 0x1e: /* FRINT32Z */
12858 case 0x5e: /* FRINT32X */
12859 gen_helper_frint32_s(tcg_res, tcg_op, tcg_fpstatus);
12860 break;
12861 case 0x1f: /* FRINT64Z */
12862 case 0x5f: /* FRINT64X */
12863 gen_helper_frint64_s(tcg_res, tcg_op, tcg_fpstatus);
12864 break;
12865 default:
12866 g_assert_not_reached();
12868 } else {
12869 /* Use helpers for 8 and 16 bit elements */
12870 switch (opcode) {
12871 case 0x5: /* CNT, RBIT */
12872 /* For these two insns size is part of the opcode specifier
12873 * (handled earlier); they always operate on byte elements.
12875 if (u) {
12876 gen_helper_neon_rbit_u8(tcg_res, tcg_op);
12877 } else {
12878 gen_helper_neon_cnt_u8(tcg_res, tcg_op);
12880 break;
12881 case 0x7: /* SQABS, SQNEG */
12883 NeonGenOneOpEnvFn *genfn;
12884 static NeonGenOneOpEnvFn * const fns[2][2] = {
12885 { gen_helper_neon_qabs_s8, gen_helper_neon_qneg_s8 },
12886 { gen_helper_neon_qabs_s16, gen_helper_neon_qneg_s16 },
12888 genfn = fns[size][u];
12889 genfn(tcg_res, cpu_env, tcg_op);
12890 break;
12892 case 0x4: /* CLS, CLZ */
12893 if (u) {
12894 if (size == 0) {
12895 gen_helper_neon_clz_u8(tcg_res, tcg_op);
12896 } else {
12897 gen_helper_neon_clz_u16(tcg_res, tcg_op);
12899 } else {
12900 if (size == 0) {
12901 gen_helper_neon_cls_s8(tcg_res, tcg_op);
12902 } else {
12903 gen_helper_neon_cls_s16(tcg_res, tcg_op);
12906 break;
12907 default:
12908 g_assert_not_reached();
12912 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
12914 tcg_temp_free_i32(tcg_res);
12915 tcg_temp_free_i32(tcg_op);
12918 clear_vec_high(s, is_q, rd);
12920 if (need_rmode) {
12921 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
12922 tcg_temp_free_i32(tcg_rmode);
12924 if (need_fpstatus) {
12925 tcg_temp_free_ptr(tcg_fpstatus);
12929 /* AdvSIMD [scalar] two register miscellaneous (FP16)
12931 * 31 30 29 28 27 24 23 22 21 17 16 12 11 10 9 5 4 0
12932 * +---+---+---+---+---------+---+-------------+--------+-----+------+------+
12933 * | 0 | Q | U | S | 1 1 1 0 | a | 1 1 1 1 0 0 | opcode | 1 0 | Rn | Rd |
12934 * +---+---+---+---+---------+---+-------------+--------+-----+------+------+
12935 * mask: 1000 1111 0111 1110 0000 1100 0000 0000 0x8f7e 0c00
12936 * val: 0000 1110 0111 1000 0000 1000 0000 0000 0x0e78 0800
12938 * This actually covers two groups where scalar access is governed by
12939 * bit 28. A bunch of the instructions (float to integral) only exist
12940 * in the vector form and are un-allocated for the scalar decode. Also
12941 * in the scalar decode Q is always 1.
12943 static void disas_simd_two_reg_misc_fp16(DisasContext *s, uint32_t insn)
12945 int fpop, opcode, a, u;
12946 int rn, rd;
12947 bool is_q;
12948 bool is_scalar;
12949 bool only_in_vector = false;
12951 int pass;
12952 TCGv_i32 tcg_rmode = NULL;
12953 TCGv_ptr tcg_fpstatus = NULL;
12954 bool need_rmode = false;
12955 bool need_fpst = true;
12956 int rmode;
12958 if (!dc_isar_feature(aa64_fp16, s)) {
12959 unallocated_encoding(s);
12960 return;
12963 rd = extract32(insn, 0, 5);
12964 rn = extract32(insn, 5, 5);
12966 a = extract32(insn, 23, 1);
12967 u = extract32(insn, 29, 1);
12968 is_scalar = extract32(insn, 28, 1);
12969 is_q = extract32(insn, 30, 1);
12971 opcode = extract32(insn, 12, 5);
12972 fpop = deposit32(opcode, 5, 1, a);
12973 fpop = deposit32(fpop, 6, 1, u);
12975 switch (fpop) {
12976 case 0x1d: /* SCVTF */
12977 case 0x5d: /* UCVTF */
12979 int elements;
12981 if (is_scalar) {
12982 elements = 1;
12983 } else {
12984 elements = (is_q ? 8 : 4);
12987 if (!fp_access_check(s)) {
12988 return;
12990 handle_simd_intfp_conv(s, rd, rn, elements, !u, 0, MO_16);
12991 return;
12993 break;
12994 case 0x2c: /* FCMGT (zero) */
12995 case 0x2d: /* FCMEQ (zero) */
12996 case 0x2e: /* FCMLT (zero) */
12997 case 0x6c: /* FCMGE (zero) */
12998 case 0x6d: /* FCMLE (zero) */
12999 handle_2misc_fcmp_zero(s, fpop, is_scalar, 0, is_q, MO_16, rn, rd);
13000 return;
13001 case 0x3d: /* FRECPE */
13002 case 0x3f: /* FRECPX */
13003 break;
13004 case 0x18: /* FRINTN */
13005 need_rmode = true;
13006 only_in_vector = true;
13007 rmode = FPROUNDING_TIEEVEN;
13008 break;
13009 case 0x19: /* FRINTM */
13010 need_rmode = true;
13011 only_in_vector = true;
13012 rmode = FPROUNDING_NEGINF;
13013 break;
13014 case 0x38: /* FRINTP */
13015 need_rmode = true;
13016 only_in_vector = true;
13017 rmode = FPROUNDING_POSINF;
13018 break;
13019 case 0x39: /* FRINTZ */
13020 need_rmode = true;
13021 only_in_vector = true;
13022 rmode = FPROUNDING_ZERO;
13023 break;
13024 case 0x58: /* FRINTA */
13025 need_rmode = true;
13026 only_in_vector = true;
13027 rmode = FPROUNDING_TIEAWAY;
13028 break;
13029 case 0x59: /* FRINTX */
13030 case 0x79: /* FRINTI */
13031 only_in_vector = true;
13032 /* current rounding mode */
13033 break;
13034 case 0x1a: /* FCVTNS */
13035 need_rmode = true;
13036 rmode = FPROUNDING_TIEEVEN;
13037 break;
13038 case 0x1b: /* FCVTMS */
13039 need_rmode = true;
13040 rmode = FPROUNDING_NEGINF;
13041 break;
13042 case 0x1c: /* FCVTAS */
13043 need_rmode = true;
13044 rmode = FPROUNDING_TIEAWAY;
13045 break;
13046 case 0x3a: /* FCVTPS */
13047 need_rmode = true;
13048 rmode = FPROUNDING_POSINF;
13049 break;
13050 case 0x3b: /* FCVTZS */
13051 need_rmode = true;
13052 rmode = FPROUNDING_ZERO;
13053 break;
13054 case 0x5a: /* FCVTNU */
13055 need_rmode = true;
13056 rmode = FPROUNDING_TIEEVEN;
13057 break;
13058 case 0x5b: /* FCVTMU */
13059 need_rmode = true;
13060 rmode = FPROUNDING_NEGINF;
13061 break;
13062 case 0x5c: /* FCVTAU */
13063 need_rmode = true;
13064 rmode = FPROUNDING_TIEAWAY;
13065 break;
13066 case 0x7a: /* FCVTPU */
13067 need_rmode = true;
13068 rmode = FPROUNDING_POSINF;
13069 break;
13070 case 0x7b: /* FCVTZU */
13071 need_rmode = true;
13072 rmode = FPROUNDING_ZERO;
13073 break;
13074 case 0x2f: /* FABS */
13075 case 0x6f: /* FNEG */
13076 need_fpst = false;
13077 break;
13078 case 0x7d: /* FRSQRTE */
13079 case 0x7f: /* FSQRT (vector) */
13080 break;
13081 default:
13082 unallocated_encoding(s);
13083 return;
13087 /* Check additional constraints for the scalar encoding */
13088 if (is_scalar) {
13089 if (!is_q) {
13090 unallocated_encoding(s);
13091 return;
13093 /* FRINTxx is only in the vector form */
13094 if (only_in_vector) {
13095 unallocated_encoding(s);
13096 return;
13100 if (!fp_access_check(s)) {
13101 return;
13104 if (need_rmode || need_fpst) {
13105 tcg_fpstatus = fpstatus_ptr(FPST_FPCR_F16);
13108 if (need_rmode) {
13109 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rmode));
13110 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
13113 if (is_scalar) {
13114 TCGv_i32 tcg_op = read_fp_hreg(s, rn);
13115 TCGv_i32 tcg_res = tcg_temp_new_i32();
13117 switch (fpop) {
13118 case 0x1a: /* FCVTNS */
13119 case 0x1b: /* FCVTMS */
13120 case 0x1c: /* FCVTAS */
13121 case 0x3a: /* FCVTPS */
13122 case 0x3b: /* FCVTZS */
13123 gen_helper_advsimd_f16tosinth(tcg_res, tcg_op, tcg_fpstatus);
13124 break;
13125 case 0x3d: /* FRECPE */
13126 gen_helper_recpe_f16(tcg_res, tcg_op, tcg_fpstatus);
13127 break;
13128 case 0x3f: /* FRECPX */
13129 gen_helper_frecpx_f16(tcg_res, tcg_op, tcg_fpstatus);
13130 break;
13131 case 0x5a: /* FCVTNU */
13132 case 0x5b: /* FCVTMU */
13133 case 0x5c: /* FCVTAU */
13134 case 0x7a: /* FCVTPU */
13135 case 0x7b: /* FCVTZU */
13136 gen_helper_advsimd_f16touinth(tcg_res, tcg_op, tcg_fpstatus);
13137 break;
13138 case 0x6f: /* FNEG */
13139 tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
13140 break;
13141 case 0x7d: /* FRSQRTE */
13142 gen_helper_rsqrte_f16(tcg_res, tcg_op, tcg_fpstatus);
13143 break;
13144 default:
13145 g_assert_not_reached();
13148 /* limit any sign extension going on */
13149 tcg_gen_andi_i32(tcg_res, tcg_res, 0xffff);
13150 write_fp_sreg(s, rd, tcg_res);
13152 tcg_temp_free_i32(tcg_res);
13153 tcg_temp_free_i32(tcg_op);
13154 } else {
13155 for (pass = 0; pass < (is_q ? 8 : 4); pass++) {
13156 TCGv_i32 tcg_op = tcg_temp_new_i32();
13157 TCGv_i32 tcg_res = tcg_temp_new_i32();
13159 read_vec_element_i32(s, tcg_op, rn, pass, MO_16);
13161 switch (fpop) {
13162 case 0x1a: /* FCVTNS */
13163 case 0x1b: /* FCVTMS */
13164 case 0x1c: /* FCVTAS */
13165 case 0x3a: /* FCVTPS */
13166 case 0x3b: /* FCVTZS */
13167 gen_helper_advsimd_f16tosinth(tcg_res, tcg_op, tcg_fpstatus);
13168 break;
13169 case 0x3d: /* FRECPE */
13170 gen_helper_recpe_f16(tcg_res, tcg_op, tcg_fpstatus);
13171 break;
13172 case 0x5a: /* FCVTNU */
13173 case 0x5b: /* FCVTMU */
13174 case 0x5c: /* FCVTAU */
13175 case 0x7a: /* FCVTPU */
13176 case 0x7b: /* FCVTZU */
13177 gen_helper_advsimd_f16touinth(tcg_res, tcg_op, tcg_fpstatus);
13178 break;
13179 case 0x18: /* FRINTN */
13180 case 0x19: /* FRINTM */
13181 case 0x38: /* FRINTP */
13182 case 0x39: /* FRINTZ */
13183 case 0x58: /* FRINTA */
13184 case 0x79: /* FRINTI */
13185 gen_helper_advsimd_rinth(tcg_res, tcg_op, tcg_fpstatus);
13186 break;
13187 case 0x59: /* FRINTX */
13188 gen_helper_advsimd_rinth_exact(tcg_res, tcg_op, tcg_fpstatus);
13189 break;
13190 case 0x2f: /* FABS */
13191 tcg_gen_andi_i32(tcg_res, tcg_op, 0x7fff);
13192 break;
13193 case 0x6f: /* FNEG */
13194 tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
13195 break;
13196 case 0x7d: /* FRSQRTE */
13197 gen_helper_rsqrte_f16(tcg_res, tcg_op, tcg_fpstatus);
13198 break;
13199 case 0x7f: /* FSQRT */
13200 gen_helper_sqrt_f16(tcg_res, tcg_op, tcg_fpstatus);
13201 break;
13202 default:
13203 g_assert_not_reached();
13206 write_vec_element_i32(s, tcg_res, rd, pass, MO_16);
13208 tcg_temp_free_i32(tcg_res);
13209 tcg_temp_free_i32(tcg_op);
13212 clear_vec_high(s, is_q, rd);
13215 if (tcg_rmode) {
13216 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
13217 tcg_temp_free_i32(tcg_rmode);
13220 if (tcg_fpstatus) {
13221 tcg_temp_free_ptr(tcg_fpstatus);
13225 /* AdvSIMD scalar x indexed element
13226 * 31 30 29 28 24 23 22 21 20 19 16 15 12 11 10 9 5 4 0
13227 * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
13228 * | 0 1 | U | 1 1 1 1 1 | size | L | M | Rm | opc | H | 0 | Rn | Rd |
13229 * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
13230 * AdvSIMD vector x indexed element
13231 * 31 30 29 28 24 23 22 21 20 19 16 15 12 11 10 9 5 4 0
13232 * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
13233 * | 0 | Q | U | 0 1 1 1 1 | size | L | M | Rm | opc | H | 0 | Rn | Rd |
13234 * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
13236 static void disas_simd_indexed(DisasContext *s, uint32_t insn)
13238 /* This encoding has two kinds of instruction:
13239 * normal, where we perform elt x idxelt => elt for each
13240 * element in the vector
13241 * long, where we perform elt x idxelt and generate a result of
13242 * double the width of the input element
13243 * The long ops have a 'part' specifier (ie come in INSN, INSN2 pairs).
13245 bool is_scalar = extract32(insn, 28, 1);
13246 bool is_q = extract32(insn, 30, 1);
13247 bool u = extract32(insn, 29, 1);
13248 int size = extract32(insn, 22, 2);
13249 int l = extract32(insn, 21, 1);
13250 int m = extract32(insn, 20, 1);
13251 /* Note that the Rm field here is only 4 bits, not 5 as it usually is */
13252 int rm = extract32(insn, 16, 4);
13253 int opcode = extract32(insn, 12, 4);
13254 int h = extract32(insn, 11, 1);
13255 int rn = extract32(insn, 5, 5);
13256 int rd = extract32(insn, 0, 5);
13257 bool is_long = false;
13258 int is_fp = 0;
13259 bool is_fp16 = false;
13260 int index;
13261 TCGv_ptr fpst;
13263 switch (16 * u + opcode) {
13264 case 0x08: /* MUL */
13265 case 0x10: /* MLA */
13266 case 0x14: /* MLS */
13267 if (is_scalar) {
13268 unallocated_encoding(s);
13269 return;
13271 break;
13272 case 0x02: /* SMLAL, SMLAL2 */
13273 case 0x12: /* UMLAL, UMLAL2 */
13274 case 0x06: /* SMLSL, SMLSL2 */
13275 case 0x16: /* UMLSL, UMLSL2 */
13276 case 0x0a: /* SMULL, SMULL2 */
13277 case 0x1a: /* UMULL, UMULL2 */
13278 if (is_scalar) {
13279 unallocated_encoding(s);
13280 return;
13282 is_long = true;
13283 break;
13284 case 0x03: /* SQDMLAL, SQDMLAL2 */
13285 case 0x07: /* SQDMLSL, SQDMLSL2 */
13286 case 0x0b: /* SQDMULL, SQDMULL2 */
13287 is_long = true;
13288 break;
13289 case 0x0c: /* SQDMULH */
13290 case 0x0d: /* SQRDMULH */
13291 break;
13292 case 0x01: /* FMLA */
13293 case 0x05: /* FMLS */
13294 case 0x09: /* FMUL */
13295 case 0x19: /* FMULX */
13296 is_fp = 1;
13297 break;
13298 case 0x1d: /* SQRDMLAH */
13299 case 0x1f: /* SQRDMLSH */
13300 if (!dc_isar_feature(aa64_rdm, s)) {
13301 unallocated_encoding(s);
13302 return;
13304 break;
13305 case 0x0e: /* SDOT */
13306 case 0x1e: /* UDOT */
13307 if (is_scalar || size != MO_32 || !dc_isar_feature(aa64_dp, s)) {
13308 unallocated_encoding(s);
13309 return;
13311 break;
13312 case 0x0f:
13313 switch (size) {
13314 case 0: /* SUDOT */
13315 case 2: /* USDOT */
13316 if (is_scalar || !dc_isar_feature(aa64_i8mm, s)) {
13317 unallocated_encoding(s);
13318 return;
13320 size = MO_32;
13321 break;
13322 case 1: /* BFDOT */
13323 if (is_scalar || !dc_isar_feature(aa64_bf16, s)) {
13324 unallocated_encoding(s);
13325 return;
13327 size = MO_32;
13328 break;
13329 case 3: /* BFMLAL{B,T} */
13330 if (is_scalar || !dc_isar_feature(aa64_bf16, s)) {
13331 unallocated_encoding(s);
13332 return;
13334 /* can't set is_fp without other incorrect size checks */
13335 size = MO_16;
13336 break;
13337 default:
13338 unallocated_encoding(s);
13339 return;
13341 break;
13342 case 0x11: /* FCMLA #0 */
13343 case 0x13: /* FCMLA #90 */
13344 case 0x15: /* FCMLA #180 */
13345 case 0x17: /* FCMLA #270 */
13346 if (is_scalar || !dc_isar_feature(aa64_fcma, s)) {
13347 unallocated_encoding(s);
13348 return;
13350 is_fp = 2;
13351 break;
13352 case 0x00: /* FMLAL */
13353 case 0x04: /* FMLSL */
13354 case 0x18: /* FMLAL2 */
13355 case 0x1c: /* FMLSL2 */
13356 if (is_scalar || size != MO_32 || !dc_isar_feature(aa64_fhm, s)) {
13357 unallocated_encoding(s);
13358 return;
13360 size = MO_16;
13361 /* is_fp, but we pass cpu_env not fp_status. */
13362 break;
13363 default:
13364 unallocated_encoding(s);
13365 return;
13368 switch (is_fp) {
13369 case 1: /* normal fp */
13370 /* convert insn encoded size to MemOp size */
13371 switch (size) {
13372 case 0: /* half-precision */
13373 size = MO_16;
13374 is_fp16 = true;
13375 break;
13376 case MO_32: /* single precision */
13377 case MO_64: /* double precision */
13378 break;
13379 default:
13380 unallocated_encoding(s);
13381 return;
13383 break;
13385 case 2: /* complex fp */
13386 /* Each indexable element is a complex pair. */
13387 size += 1;
13388 switch (size) {
13389 case MO_32:
13390 if (h && !is_q) {
13391 unallocated_encoding(s);
13392 return;
13394 is_fp16 = true;
13395 break;
13396 case MO_64:
13397 break;
13398 default:
13399 unallocated_encoding(s);
13400 return;
13402 break;
13404 default: /* integer */
13405 switch (size) {
13406 case MO_8:
13407 case MO_64:
13408 unallocated_encoding(s);
13409 return;
13411 break;
13413 if (is_fp16 && !dc_isar_feature(aa64_fp16, s)) {
13414 unallocated_encoding(s);
13415 return;
13418 /* Given MemOp size, adjust register and indexing. */
13419 switch (size) {
13420 case MO_16:
13421 index = h << 2 | l << 1 | m;
13422 break;
13423 case MO_32:
13424 index = h << 1 | l;
13425 rm |= m << 4;
13426 break;
13427 case MO_64:
13428 if (l || !is_q) {
13429 unallocated_encoding(s);
13430 return;
13432 index = h;
13433 rm |= m << 4;
13434 break;
13435 default:
13436 g_assert_not_reached();
13439 if (!fp_access_check(s)) {
13440 return;
13443 if (is_fp) {
13444 fpst = fpstatus_ptr(is_fp16 ? FPST_FPCR_F16 : FPST_FPCR);
13445 } else {
13446 fpst = NULL;
13449 switch (16 * u + opcode) {
13450 case 0x0e: /* SDOT */
13451 case 0x1e: /* UDOT */
13452 gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
13453 u ? gen_helper_gvec_udot_idx_b
13454 : gen_helper_gvec_sdot_idx_b);
13455 return;
13456 case 0x0f:
13457 switch (extract32(insn, 22, 2)) {
13458 case 0: /* SUDOT */
13459 gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
13460 gen_helper_gvec_sudot_idx_b);
13461 return;
13462 case 1: /* BFDOT */
13463 gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
13464 gen_helper_gvec_bfdot_idx);
13465 return;
13466 case 2: /* USDOT */
13467 gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
13468 gen_helper_gvec_usdot_idx_b);
13469 return;
13470 case 3: /* BFMLAL{B,T} */
13471 gen_gvec_op4_fpst(s, 1, rd, rn, rm, rd, 0, (index << 1) | is_q,
13472 gen_helper_gvec_bfmlal_idx);
13473 return;
13475 g_assert_not_reached();
13476 case 0x11: /* FCMLA #0 */
13477 case 0x13: /* FCMLA #90 */
13478 case 0x15: /* FCMLA #180 */
13479 case 0x17: /* FCMLA #270 */
13481 int rot = extract32(insn, 13, 2);
13482 int data = (index << 2) | rot;
13483 tcg_gen_gvec_4_ptr(vec_full_reg_offset(s, rd),
13484 vec_full_reg_offset(s, rn),
13485 vec_full_reg_offset(s, rm),
13486 vec_full_reg_offset(s, rd), fpst,
13487 is_q ? 16 : 8, vec_full_reg_size(s), data,
13488 size == MO_64
13489 ? gen_helper_gvec_fcmlas_idx
13490 : gen_helper_gvec_fcmlah_idx);
13491 tcg_temp_free_ptr(fpst);
13493 return;
13495 case 0x00: /* FMLAL */
13496 case 0x04: /* FMLSL */
13497 case 0x18: /* FMLAL2 */
13498 case 0x1c: /* FMLSL2 */
13500 int is_s = extract32(opcode, 2, 1);
13501 int is_2 = u;
13502 int data = (index << 2) | (is_2 << 1) | is_s;
13503 tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
13504 vec_full_reg_offset(s, rn),
13505 vec_full_reg_offset(s, rm), cpu_env,
13506 is_q ? 16 : 8, vec_full_reg_size(s),
13507 data, gen_helper_gvec_fmlal_idx_a64);
13509 return;
13511 case 0x08: /* MUL */
13512 if (!is_long && !is_scalar) {
13513 static gen_helper_gvec_3 * const fns[3] = {
13514 gen_helper_gvec_mul_idx_h,
13515 gen_helper_gvec_mul_idx_s,
13516 gen_helper_gvec_mul_idx_d,
13518 tcg_gen_gvec_3_ool(vec_full_reg_offset(s, rd),
13519 vec_full_reg_offset(s, rn),
13520 vec_full_reg_offset(s, rm),
13521 is_q ? 16 : 8, vec_full_reg_size(s),
13522 index, fns[size - 1]);
13523 return;
13525 break;
13527 case 0x10: /* MLA */
13528 if (!is_long && !is_scalar) {
13529 static gen_helper_gvec_4 * const fns[3] = {
13530 gen_helper_gvec_mla_idx_h,
13531 gen_helper_gvec_mla_idx_s,
13532 gen_helper_gvec_mla_idx_d,
13534 tcg_gen_gvec_4_ool(vec_full_reg_offset(s, rd),
13535 vec_full_reg_offset(s, rn),
13536 vec_full_reg_offset(s, rm),
13537 vec_full_reg_offset(s, rd),
13538 is_q ? 16 : 8, vec_full_reg_size(s),
13539 index, fns[size - 1]);
13540 return;
13542 break;
13544 case 0x14: /* MLS */
13545 if (!is_long && !is_scalar) {
13546 static gen_helper_gvec_4 * const fns[3] = {
13547 gen_helper_gvec_mls_idx_h,
13548 gen_helper_gvec_mls_idx_s,
13549 gen_helper_gvec_mls_idx_d,
13551 tcg_gen_gvec_4_ool(vec_full_reg_offset(s, rd),
13552 vec_full_reg_offset(s, rn),
13553 vec_full_reg_offset(s, rm),
13554 vec_full_reg_offset(s, rd),
13555 is_q ? 16 : 8, vec_full_reg_size(s),
13556 index, fns[size - 1]);
13557 return;
13559 break;
13562 if (size == 3) {
13563 TCGv_i64 tcg_idx = tcg_temp_new_i64();
13564 int pass;
13566 assert(is_fp && is_q && !is_long);
13568 read_vec_element(s, tcg_idx, rm, index, MO_64);
13570 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
13571 TCGv_i64 tcg_op = tcg_temp_new_i64();
13572 TCGv_i64 tcg_res = tcg_temp_new_i64();
13574 read_vec_element(s, tcg_op, rn, pass, MO_64);
13576 switch (16 * u + opcode) {
13577 case 0x05: /* FMLS */
13578 /* As usual for ARM, separate negation for fused multiply-add */
13579 gen_helper_vfp_negd(tcg_op, tcg_op);
13580 /* fall through */
13581 case 0x01: /* FMLA */
13582 read_vec_element(s, tcg_res, rd, pass, MO_64);
13583 gen_helper_vfp_muladdd(tcg_res, tcg_op, tcg_idx, tcg_res, fpst);
13584 break;
13585 case 0x09: /* FMUL */
13586 gen_helper_vfp_muld(tcg_res, tcg_op, tcg_idx, fpst);
13587 break;
13588 case 0x19: /* FMULX */
13589 gen_helper_vfp_mulxd(tcg_res, tcg_op, tcg_idx, fpst);
13590 break;
13591 default:
13592 g_assert_not_reached();
13595 write_vec_element(s, tcg_res, rd, pass, MO_64);
13596 tcg_temp_free_i64(tcg_op);
13597 tcg_temp_free_i64(tcg_res);
13600 tcg_temp_free_i64(tcg_idx);
13601 clear_vec_high(s, !is_scalar, rd);
13602 } else if (!is_long) {
13603 /* 32 bit floating point, or 16 or 32 bit integer.
13604 * For the 16 bit scalar case we use the usual Neon helpers and
13605 * rely on the fact that 0 op 0 == 0 with no side effects.
13607 TCGv_i32 tcg_idx = tcg_temp_new_i32();
13608 int pass, maxpasses;
13610 if (is_scalar) {
13611 maxpasses = 1;
13612 } else {
13613 maxpasses = is_q ? 4 : 2;
13616 read_vec_element_i32(s, tcg_idx, rm, index, size);
13618 if (size == 1 && !is_scalar) {
13619 /* The simplest way to handle the 16x16 indexed ops is to duplicate
13620 * the index into both halves of the 32 bit tcg_idx and then use
13621 * the usual Neon helpers.
13623 tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
13626 for (pass = 0; pass < maxpasses; pass++) {
13627 TCGv_i32 tcg_op = tcg_temp_new_i32();
13628 TCGv_i32 tcg_res = tcg_temp_new_i32();
13630 read_vec_element_i32(s, tcg_op, rn, pass, is_scalar ? size : MO_32);
13632 switch (16 * u + opcode) {
13633 case 0x08: /* MUL */
13634 case 0x10: /* MLA */
13635 case 0x14: /* MLS */
13637 static NeonGenTwoOpFn * const fns[2][2] = {
13638 { gen_helper_neon_add_u16, gen_helper_neon_sub_u16 },
13639 { tcg_gen_add_i32, tcg_gen_sub_i32 },
13641 NeonGenTwoOpFn *genfn;
13642 bool is_sub = opcode == 0x4;
13644 if (size == 1) {
13645 gen_helper_neon_mul_u16(tcg_res, tcg_op, tcg_idx);
13646 } else {
13647 tcg_gen_mul_i32(tcg_res, tcg_op, tcg_idx);
13649 if (opcode == 0x8) {
13650 break;
13652 read_vec_element_i32(s, tcg_op, rd, pass, MO_32);
13653 genfn = fns[size - 1][is_sub];
13654 genfn(tcg_res, tcg_op, tcg_res);
13655 break;
13657 case 0x05: /* FMLS */
13658 case 0x01: /* FMLA */
13659 read_vec_element_i32(s, tcg_res, rd, pass,
13660 is_scalar ? size : MO_32);
13661 switch (size) {
13662 case 1:
13663 if (opcode == 0x5) {
13664 /* As usual for ARM, separate negation for fused
13665 * multiply-add */
13666 tcg_gen_xori_i32(tcg_op, tcg_op, 0x80008000);
13668 if (is_scalar) {
13669 gen_helper_advsimd_muladdh(tcg_res, tcg_op, tcg_idx,
13670 tcg_res, fpst);
13671 } else {
13672 gen_helper_advsimd_muladd2h(tcg_res, tcg_op, tcg_idx,
13673 tcg_res, fpst);
13675 break;
13676 case 2:
13677 if (opcode == 0x5) {
13678 /* As usual for ARM, separate negation for
13679 * fused multiply-add */
13680 tcg_gen_xori_i32(tcg_op, tcg_op, 0x80000000);
13682 gen_helper_vfp_muladds(tcg_res, tcg_op, tcg_idx,
13683 tcg_res, fpst);
13684 break;
13685 default:
13686 g_assert_not_reached();
13688 break;
13689 case 0x09: /* FMUL */
13690 switch (size) {
13691 case 1:
13692 if (is_scalar) {
13693 gen_helper_advsimd_mulh(tcg_res, tcg_op,
13694 tcg_idx, fpst);
13695 } else {
13696 gen_helper_advsimd_mul2h(tcg_res, tcg_op,
13697 tcg_idx, fpst);
13699 break;
13700 case 2:
13701 gen_helper_vfp_muls(tcg_res, tcg_op, tcg_idx, fpst);
13702 break;
13703 default:
13704 g_assert_not_reached();
13706 break;
13707 case 0x19: /* FMULX */
13708 switch (size) {
13709 case 1:
13710 if (is_scalar) {
13711 gen_helper_advsimd_mulxh(tcg_res, tcg_op,
13712 tcg_idx, fpst);
13713 } else {
13714 gen_helper_advsimd_mulx2h(tcg_res, tcg_op,
13715 tcg_idx, fpst);
13717 break;
13718 case 2:
13719 gen_helper_vfp_mulxs(tcg_res, tcg_op, tcg_idx, fpst);
13720 break;
13721 default:
13722 g_assert_not_reached();
13724 break;
13725 case 0x0c: /* SQDMULH */
13726 if (size == 1) {
13727 gen_helper_neon_qdmulh_s16(tcg_res, cpu_env,
13728 tcg_op, tcg_idx);
13729 } else {
13730 gen_helper_neon_qdmulh_s32(tcg_res, cpu_env,
13731 tcg_op, tcg_idx);
13733 break;
13734 case 0x0d: /* SQRDMULH */
13735 if (size == 1) {
13736 gen_helper_neon_qrdmulh_s16(tcg_res, cpu_env,
13737 tcg_op, tcg_idx);
13738 } else {
13739 gen_helper_neon_qrdmulh_s32(tcg_res, cpu_env,
13740 tcg_op, tcg_idx);
13742 break;
13743 case 0x1d: /* SQRDMLAH */
13744 read_vec_element_i32(s, tcg_res, rd, pass,
13745 is_scalar ? size : MO_32);
13746 if (size == 1) {
13747 gen_helper_neon_qrdmlah_s16(tcg_res, cpu_env,
13748 tcg_op, tcg_idx, tcg_res);
13749 } else {
13750 gen_helper_neon_qrdmlah_s32(tcg_res, cpu_env,
13751 tcg_op, tcg_idx, tcg_res);
13753 break;
13754 case 0x1f: /* SQRDMLSH */
13755 read_vec_element_i32(s, tcg_res, rd, pass,
13756 is_scalar ? size : MO_32);
13757 if (size == 1) {
13758 gen_helper_neon_qrdmlsh_s16(tcg_res, cpu_env,
13759 tcg_op, tcg_idx, tcg_res);
13760 } else {
13761 gen_helper_neon_qrdmlsh_s32(tcg_res, cpu_env,
13762 tcg_op, tcg_idx, tcg_res);
13764 break;
13765 default:
13766 g_assert_not_reached();
13769 if (is_scalar) {
13770 write_fp_sreg(s, rd, tcg_res);
13771 } else {
13772 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
13775 tcg_temp_free_i32(tcg_op);
13776 tcg_temp_free_i32(tcg_res);
13779 tcg_temp_free_i32(tcg_idx);
13780 clear_vec_high(s, is_q, rd);
13781 } else {
13782 /* long ops: 16x16->32 or 32x32->64 */
13783 TCGv_i64 tcg_res[2];
13784 int pass;
13785 bool satop = extract32(opcode, 0, 1);
13786 MemOp memop = MO_32;
13788 if (satop || !u) {
13789 memop |= MO_SIGN;
13792 if (size == 2) {
13793 TCGv_i64 tcg_idx = tcg_temp_new_i64();
13795 read_vec_element(s, tcg_idx, rm, index, memop);
13797 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
13798 TCGv_i64 tcg_op = tcg_temp_new_i64();
13799 TCGv_i64 tcg_passres;
13800 int passelt;
13802 if (is_scalar) {
13803 passelt = 0;
13804 } else {
13805 passelt = pass + (is_q * 2);
13808 read_vec_element(s, tcg_op, rn, passelt, memop);
13810 tcg_res[pass] = tcg_temp_new_i64();
13812 if (opcode == 0xa || opcode == 0xb) {
13813 /* Non-accumulating ops */
13814 tcg_passres = tcg_res[pass];
13815 } else {
13816 tcg_passres = tcg_temp_new_i64();
13819 tcg_gen_mul_i64(tcg_passres, tcg_op, tcg_idx);
13820 tcg_temp_free_i64(tcg_op);
13822 if (satop) {
13823 /* saturating, doubling */
13824 gen_helper_neon_addl_saturate_s64(tcg_passres, cpu_env,
13825 tcg_passres, tcg_passres);
13828 if (opcode == 0xa || opcode == 0xb) {
13829 continue;
13832 /* Accumulating op: handle accumulate step */
13833 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
13835 switch (opcode) {
13836 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
13837 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
13838 break;
13839 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
13840 tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
13841 break;
13842 case 0x7: /* SQDMLSL, SQDMLSL2 */
13843 tcg_gen_neg_i64(tcg_passres, tcg_passres);
13844 /* fall through */
13845 case 0x3: /* SQDMLAL, SQDMLAL2 */
13846 gen_helper_neon_addl_saturate_s64(tcg_res[pass], cpu_env,
13847 tcg_res[pass],
13848 tcg_passres);
13849 break;
13850 default:
13851 g_assert_not_reached();
13853 tcg_temp_free_i64(tcg_passres);
13855 tcg_temp_free_i64(tcg_idx);
13857 clear_vec_high(s, !is_scalar, rd);
13858 } else {
13859 TCGv_i32 tcg_idx = tcg_temp_new_i32();
13861 assert(size == 1);
13862 read_vec_element_i32(s, tcg_idx, rm, index, size);
13864 if (!is_scalar) {
13865 /* The simplest way to handle the 16x16 indexed ops is to
13866 * duplicate the index into both halves of the 32 bit tcg_idx
13867 * and then use the usual Neon helpers.
13869 tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
13872 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
13873 TCGv_i32 tcg_op = tcg_temp_new_i32();
13874 TCGv_i64 tcg_passres;
13876 if (is_scalar) {
13877 read_vec_element_i32(s, tcg_op, rn, pass, size);
13878 } else {
13879 read_vec_element_i32(s, tcg_op, rn,
13880 pass + (is_q * 2), MO_32);
13883 tcg_res[pass] = tcg_temp_new_i64();
13885 if (opcode == 0xa || opcode == 0xb) {
13886 /* Non-accumulating ops */
13887 tcg_passres = tcg_res[pass];
13888 } else {
13889 tcg_passres = tcg_temp_new_i64();
13892 if (memop & MO_SIGN) {
13893 gen_helper_neon_mull_s16(tcg_passres, tcg_op, tcg_idx);
13894 } else {
13895 gen_helper_neon_mull_u16(tcg_passres, tcg_op, tcg_idx);
13897 if (satop) {
13898 gen_helper_neon_addl_saturate_s32(tcg_passres, cpu_env,
13899 tcg_passres, tcg_passres);
13901 tcg_temp_free_i32(tcg_op);
13903 if (opcode == 0xa || opcode == 0xb) {
13904 continue;
13907 /* Accumulating op: handle accumulate step */
13908 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
13910 switch (opcode) {
13911 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
13912 gen_helper_neon_addl_u32(tcg_res[pass], tcg_res[pass],
13913 tcg_passres);
13914 break;
13915 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
13916 gen_helper_neon_subl_u32(tcg_res[pass], tcg_res[pass],
13917 tcg_passres);
13918 break;
13919 case 0x7: /* SQDMLSL, SQDMLSL2 */
13920 gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
13921 /* fall through */
13922 case 0x3: /* SQDMLAL, SQDMLAL2 */
13923 gen_helper_neon_addl_saturate_s32(tcg_res[pass], cpu_env,
13924 tcg_res[pass],
13925 tcg_passres);
13926 break;
13927 default:
13928 g_assert_not_reached();
13930 tcg_temp_free_i64(tcg_passres);
13932 tcg_temp_free_i32(tcg_idx);
13934 if (is_scalar) {
13935 tcg_gen_ext32u_i64(tcg_res[0], tcg_res[0]);
13939 if (is_scalar) {
13940 tcg_res[1] = tcg_constant_i64(0);
13943 for (pass = 0; pass < 2; pass++) {
13944 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
13945 tcg_temp_free_i64(tcg_res[pass]);
13949 if (fpst) {
13950 tcg_temp_free_ptr(fpst);
13954 /* Crypto AES
13955 * 31 24 23 22 21 17 16 12 11 10 9 5 4 0
13956 * +-----------------+------+-----------+--------+-----+------+------+
13957 * | 0 1 0 0 1 1 1 0 | size | 1 0 1 0 0 | opcode | 1 0 | Rn | Rd |
13958 * +-----------------+------+-----------+--------+-----+------+------+
13960 static void disas_crypto_aes(DisasContext *s, uint32_t insn)
13962 int size = extract32(insn, 22, 2);
13963 int opcode = extract32(insn, 12, 5);
13964 int rn = extract32(insn, 5, 5);
13965 int rd = extract32(insn, 0, 5);
13966 int decrypt;
13967 gen_helper_gvec_2 *genfn2 = NULL;
13968 gen_helper_gvec_3 *genfn3 = NULL;
13970 if (!dc_isar_feature(aa64_aes, s) || size != 0) {
13971 unallocated_encoding(s);
13972 return;
13975 switch (opcode) {
13976 case 0x4: /* AESE */
13977 decrypt = 0;
13978 genfn3 = gen_helper_crypto_aese;
13979 break;
13980 case 0x6: /* AESMC */
13981 decrypt = 0;
13982 genfn2 = gen_helper_crypto_aesmc;
13983 break;
13984 case 0x5: /* AESD */
13985 decrypt = 1;
13986 genfn3 = gen_helper_crypto_aese;
13987 break;
13988 case 0x7: /* AESIMC */
13989 decrypt = 1;
13990 genfn2 = gen_helper_crypto_aesmc;
13991 break;
13992 default:
13993 unallocated_encoding(s);
13994 return;
13997 if (!fp_access_check(s)) {
13998 return;
14000 if (genfn2) {
14001 gen_gvec_op2_ool(s, true, rd, rn, decrypt, genfn2);
14002 } else {
14003 gen_gvec_op3_ool(s, true, rd, rd, rn, decrypt, genfn3);
14007 /* Crypto three-reg SHA
14008 * 31 24 23 22 21 20 16 15 14 12 11 10 9 5 4 0
14009 * +-----------------+------+---+------+---+--------+-----+------+------+
14010 * | 0 1 0 1 1 1 1 0 | size | 0 | Rm | 0 | opcode | 0 0 | Rn | Rd |
14011 * +-----------------+------+---+------+---+--------+-----+------+------+
14013 static void disas_crypto_three_reg_sha(DisasContext *s, uint32_t insn)
14015 int size = extract32(insn, 22, 2);
14016 int opcode = extract32(insn, 12, 3);
14017 int rm = extract32(insn, 16, 5);
14018 int rn = extract32(insn, 5, 5);
14019 int rd = extract32(insn, 0, 5);
14020 gen_helper_gvec_3 *genfn;
14021 bool feature;
14023 if (size != 0) {
14024 unallocated_encoding(s);
14025 return;
14028 switch (opcode) {
14029 case 0: /* SHA1C */
14030 genfn = gen_helper_crypto_sha1c;
14031 feature = dc_isar_feature(aa64_sha1, s);
14032 break;
14033 case 1: /* SHA1P */
14034 genfn = gen_helper_crypto_sha1p;
14035 feature = dc_isar_feature(aa64_sha1, s);
14036 break;
14037 case 2: /* SHA1M */
14038 genfn = gen_helper_crypto_sha1m;
14039 feature = dc_isar_feature(aa64_sha1, s);
14040 break;
14041 case 3: /* SHA1SU0 */
14042 genfn = gen_helper_crypto_sha1su0;
14043 feature = dc_isar_feature(aa64_sha1, s);
14044 break;
14045 case 4: /* SHA256H */
14046 genfn = gen_helper_crypto_sha256h;
14047 feature = dc_isar_feature(aa64_sha256, s);
14048 break;
14049 case 5: /* SHA256H2 */
14050 genfn = gen_helper_crypto_sha256h2;
14051 feature = dc_isar_feature(aa64_sha256, s);
14052 break;
14053 case 6: /* SHA256SU1 */
14054 genfn = gen_helper_crypto_sha256su1;
14055 feature = dc_isar_feature(aa64_sha256, s);
14056 break;
14057 default:
14058 unallocated_encoding(s);
14059 return;
14062 if (!feature) {
14063 unallocated_encoding(s);
14064 return;
14067 if (!fp_access_check(s)) {
14068 return;
14070 gen_gvec_op3_ool(s, true, rd, rn, rm, 0, genfn);
14073 /* Crypto two-reg SHA
14074 * 31 24 23 22 21 17 16 12 11 10 9 5 4 0
14075 * +-----------------+------+-----------+--------+-----+------+------+
14076 * | 0 1 0 1 1 1 1 0 | size | 1 0 1 0 0 | opcode | 1 0 | Rn | Rd |
14077 * +-----------------+------+-----------+--------+-----+------+------+
14079 static void disas_crypto_two_reg_sha(DisasContext *s, uint32_t insn)
14081 int size = extract32(insn, 22, 2);
14082 int opcode = extract32(insn, 12, 5);
14083 int rn = extract32(insn, 5, 5);
14084 int rd = extract32(insn, 0, 5);
14085 gen_helper_gvec_2 *genfn;
14086 bool feature;
14088 if (size != 0) {
14089 unallocated_encoding(s);
14090 return;
14093 switch (opcode) {
14094 case 0: /* SHA1H */
14095 feature = dc_isar_feature(aa64_sha1, s);
14096 genfn = gen_helper_crypto_sha1h;
14097 break;
14098 case 1: /* SHA1SU1 */
14099 feature = dc_isar_feature(aa64_sha1, s);
14100 genfn = gen_helper_crypto_sha1su1;
14101 break;
14102 case 2: /* SHA256SU0 */
14103 feature = dc_isar_feature(aa64_sha256, s);
14104 genfn = gen_helper_crypto_sha256su0;
14105 break;
14106 default:
14107 unallocated_encoding(s);
14108 return;
14111 if (!feature) {
14112 unallocated_encoding(s);
14113 return;
14116 if (!fp_access_check(s)) {
14117 return;
14119 gen_gvec_op2_ool(s, true, rd, rn, 0, genfn);
14122 static void gen_rax1_i64(TCGv_i64 d, TCGv_i64 n, TCGv_i64 m)
14124 tcg_gen_rotli_i64(d, m, 1);
14125 tcg_gen_xor_i64(d, d, n);
14128 static void gen_rax1_vec(unsigned vece, TCGv_vec d, TCGv_vec n, TCGv_vec m)
14130 tcg_gen_rotli_vec(vece, d, m, 1);
14131 tcg_gen_xor_vec(vece, d, d, n);
14134 void gen_gvec_rax1(unsigned vece, uint32_t rd_ofs, uint32_t rn_ofs,
14135 uint32_t rm_ofs, uint32_t opr_sz, uint32_t max_sz)
14137 static const TCGOpcode vecop_list[] = { INDEX_op_rotli_vec, 0 };
14138 static const GVecGen3 op = {
14139 .fni8 = gen_rax1_i64,
14140 .fniv = gen_rax1_vec,
14141 .opt_opc = vecop_list,
14142 .fno = gen_helper_crypto_rax1,
14143 .vece = MO_64,
14145 tcg_gen_gvec_3(rd_ofs, rn_ofs, rm_ofs, opr_sz, max_sz, &op);
14148 /* Crypto three-reg SHA512
14149 * 31 21 20 16 15 14 13 12 11 10 9 5 4 0
14150 * +-----------------------+------+---+---+-----+--------+------+------+
14151 * | 1 1 0 0 1 1 1 0 0 1 1 | Rm | 1 | O | 0 0 | opcode | Rn | Rd |
14152 * +-----------------------+------+---+---+-----+--------+------+------+
14154 static void disas_crypto_three_reg_sha512(DisasContext *s, uint32_t insn)
14156 int opcode = extract32(insn, 10, 2);
14157 int o = extract32(insn, 14, 1);
14158 int rm = extract32(insn, 16, 5);
14159 int rn = extract32(insn, 5, 5);
14160 int rd = extract32(insn, 0, 5);
14161 bool feature;
14162 gen_helper_gvec_3 *oolfn = NULL;
14163 GVecGen3Fn *gvecfn = NULL;
14165 if (o == 0) {
14166 switch (opcode) {
14167 case 0: /* SHA512H */
14168 feature = dc_isar_feature(aa64_sha512, s);
14169 oolfn = gen_helper_crypto_sha512h;
14170 break;
14171 case 1: /* SHA512H2 */
14172 feature = dc_isar_feature(aa64_sha512, s);
14173 oolfn = gen_helper_crypto_sha512h2;
14174 break;
14175 case 2: /* SHA512SU1 */
14176 feature = dc_isar_feature(aa64_sha512, s);
14177 oolfn = gen_helper_crypto_sha512su1;
14178 break;
14179 case 3: /* RAX1 */
14180 feature = dc_isar_feature(aa64_sha3, s);
14181 gvecfn = gen_gvec_rax1;
14182 break;
14183 default:
14184 g_assert_not_reached();
14186 } else {
14187 switch (opcode) {
14188 case 0: /* SM3PARTW1 */
14189 feature = dc_isar_feature(aa64_sm3, s);
14190 oolfn = gen_helper_crypto_sm3partw1;
14191 break;
14192 case 1: /* SM3PARTW2 */
14193 feature = dc_isar_feature(aa64_sm3, s);
14194 oolfn = gen_helper_crypto_sm3partw2;
14195 break;
14196 case 2: /* SM4EKEY */
14197 feature = dc_isar_feature(aa64_sm4, s);
14198 oolfn = gen_helper_crypto_sm4ekey;
14199 break;
14200 default:
14201 unallocated_encoding(s);
14202 return;
14206 if (!feature) {
14207 unallocated_encoding(s);
14208 return;
14211 if (!fp_access_check(s)) {
14212 return;
14215 if (oolfn) {
14216 gen_gvec_op3_ool(s, true, rd, rn, rm, 0, oolfn);
14217 } else {
14218 gen_gvec_fn3(s, true, rd, rn, rm, gvecfn, MO_64);
14222 /* Crypto two-reg SHA512
14223 * 31 12 11 10 9 5 4 0
14224 * +-----------------------------------------+--------+------+------+
14225 * | 1 1 0 0 1 1 1 0 1 1 0 0 0 0 0 0 1 0 0 0 | opcode | Rn | Rd |
14226 * +-----------------------------------------+--------+------+------+
14228 static void disas_crypto_two_reg_sha512(DisasContext *s, uint32_t insn)
14230 int opcode = extract32(insn, 10, 2);
14231 int rn = extract32(insn, 5, 5);
14232 int rd = extract32(insn, 0, 5);
14233 bool feature;
14235 switch (opcode) {
14236 case 0: /* SHA512SU0 */
14237 feature = dc_isar_feature(aa64_sha512, s);
14238 break;
14239 case 1: /* SM4E */
14240 feature = dc_isar_feature(aa64_sm4, s);
14241 break;
14242 default:
14243 unallocated_encoding(s);
14244 return;
14247 if (!feature) {
14248 unallocated_encoding(s);
14249 return;
14252 if (!fp_access_check(s)) {
14253 return;
14256 switch (opcode) {
14257 case 0: /* SHA512SU0 */
14258 gen_gvec_op2_ool(s, true, rd, rn, 0, gen_helper_crypto_sha512su0);
14259 break;
14260 case 1: /* SM4E */
14261 gen_gvec_op3_ool(s, true, rd, rd, rn, 0, gen_helper_crypto_sm4e);
14262 break;
14263 default:
14264 g_assert_not_reached();
14268 /* Crypto four-register
14269 * 31 23 22 21 20 16 15 14 10 9 5 4 0
14270 * +-------------------+-----+------+---+------+------+------+
14271 * | 1 1 0 0 1 1 1 0 0 | Op0 | Rm | 0 | Ra | Rn | Rd |
14272 * +-------------------+-----+------+---+------+------+------+
14274 static void disas_crypto_four_reg(DisasContext *s, uint32_t insn)
14276 int op0 = extract32(insn, 21, 2);
14277 int rm = extract32(insn, 16, 5);
14278 int ra = extract32(insn, 10, 5);
14279 int rn = extract32(insn, 5, 5);
14280 int rd = extract32(insn, 0, 5);
14281 bool feature;
14283 switch (op0) {
14284 case 0: /* EOR3 */
14285 case 1: /* BCAX */
14286 feature = dc_isar_feature(aa64_sha3, s);
14287 break;
14288 case 2: /* SM3SS1 */
14289 feature = dc_isar_feature(aa64_sm3, s);
14290 break;
14291 default:
14292 unallocated_encoding(s);
14293 return;
14296 if (!feature) {
14297 unallocated_encoding(s);
14298 return;
14301 if (!fp_access_check(s)) {
14302 return;
14305 if (op0 < 2) {
14306 TCGv_i64 tcg_op1, tcg_op2, tcg_op3, tcg_res[2];
14307 int pass;
14309 tcg_op1 = tcg_temp_new_i64();
14310 tcg_op2 = tcg_temp_new_i64();
14311 tcg_op3 = tcg_temp_new_i64();
14312 tcg_res[0] = tcg_temp_new_i64();
14313 tcg_res[1] = tcg_temp_new_i64();
14315 for (pass = 0; pass < 2; pass++) {
14316 read_vec_element(s, tcg_op1, rn, pass, MO_64);
14317 read_vec_element(s, tcg_op2, rm, pass, MO_64);
14318 read_vec_element(s, tcg_op3, ra, pass, MO_64);
14320 if (op0 == 0) {
14321 /* EOR3 */
14322 tcg_gen_xor_i64(tcg_res[pass], tcg_op2, tcg_op3);
14323 } else {
14324 /* BCAX */
14325 tcg_gen_andc_i64(tcg_res[pass], tcg_op2, tcg_op3);
14327 tcg_gen_xor_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
14329 write_vec_element(s, tcg_res[0], rd, 0, MO_64);
14330 write_vec_element(s, tcg_res[1], rd, 1, MO_64);
14332 tcg_temp_free_i64(tcg_op1);
14333 tcg_temp_free_i64(tcg_op2);
14334 tcg_temp_free_i64(tcg_op3);
14335 tcg_temp_free_i64(tcg_res[0]);
14336 tcg_temp_free_i64(tcg_res[1]);
14337 } else {
14338 TCGv_i32 tcg_op1, tcg_op2, tcg_op3, tcg_res, tcg_zero;
14340 tcg_op1 = tcg_temp_new_i32();
14341 tcg_op2 = tcg_temp_new_i32();
14342 tcg_op3 = tcg_temp_new_i32();
14343 tcg_res = tcg_temp_new_i32();
14344 tcg_zero = tcg_constant_i32(0);
14346 read_vec_element_i32(s, tcg_op1, rn, 3, MO_32);
14347 read_vec_element_i32(s, tcg_op2, rm, 3, MO_32);
14348 read_vec_element_i32(s, tcg_op3, ra, 3, MO_32);
14350 tcg_gen_rotri_i32(tcg_res, tcg_op1, 20);
14351 tcg_gen_add_i32(tcg_res, tcg_res, tcg_op2);
14352 tcg_gen_add_i32(tcg_res, tcg_res, tcg_op3);
14353 tcg_gen_rotri_i32(tcg_res, tcg_res, 25);
14355 write_vec_element_i32(s, tcg_zero, rd, 0, MO_32);
14356 write_vec_element_i32(s, tcg_zero, rd, 1, MO_32);
14357 write_vec_element_i32(s, tcg_zero, rd, 2, MO_32);
14358 write_vec_element_i32(s, tcg_res, rd, 3, MO_32);
14360 tcg_temp_free_i32(tcg_op1);
14361 tcg_temp_free_i32(tcg_op2);
14362 tcg_temp_free_i32(tcg_op3);
14363 tcg_temp_free_i32(tcg_res);
14367 /* Crypto XAR
14368 * 31 21 20 16 15 10 9 5 4 0
14369 * +-----------------------+------+--------+------+------+
14370 * | 1 1 0 0 1 1 1 0 1 0 0 | Rm | imm6 | Rn | Rd |
14371 * +-----------------------+------+--------+------+------+
14373 static void disas_crypto_xar(DisasContext *s, uint32_t insn)
14375 int rm = extract32(insn, 16, 5);
14376 int imm6 = extract32(insn, 10, 6);
14377 int rn = extract32(insn, 5, 5);
14378 int rd = extract32(insn, 0, 5);
14380 if (!dc_isar_feature(aa64_sha3, s)) {
14381 unallocated_encoding(s);
14382 return;
14385 if (!fp_access_check(s)) {
14386 return;
14389 gen_gvec_xar(MO_64, vec_full_reg_offset(s, rd),
14390 vec_full_reg_offset(s, rn),
14391 vec_full_reg_offset(s, rm), imm6, 16,
14392 vec_full_reg_size(s));
14395 /* Crypto three-reg imm2
14396 * 31 21 20 16 15 14 13 12 11 10 9 5 4 0
14397 * +-----------------------+------+-----+------+--------+------+------+
14398 * | 1 1 0 0 1 1 1 0 0 1 0 | Rm | 1 0 | imm2 | opcode | Rn | Rd |
14399 * +-----------------------+------+-----+------+--------+------+------+
14401 static void disas_crypto_three_reg_imm2(DisasContext *s, uint32_t insn)
14403 static gen_helper_gvec_3 * const fns[4] = {
14404 gen_helper_crypto_sm3tt1a, gen_helper_crypto_sm3tt1b,
14405 gen_helper_crypto_sm3tt2a, gen_helper_crypto_sm3tt2b,
14407 int opcode = extract32(insn, 10, 2);
14408 int imm2 = extract32(insn, 12, 2);
14409 int rm = extract32(insn, 16, 5);
14410 int rn = extract32(insn, 5, 5);
14411 int rd = extract32(insn, 0, 5);
14413 if (!dc_isar_feature(aa64_sm3, s)) {
14414 unallocated_encoding(s);
14415 return;
14418 if (!fp_access_check(s)) {
14419 return;
14422 gen_gvec_op3_ool(s, true, rd, rn, rm, imm2, fns[opcode]);
14425 /* C3.6 Data processing - SIMD, inc Crypto
14427 * As the decode gets a little complex we are using a table based
14428 * approach for this part of the decode.
14430 static const AArch64DecodeTable data_proc_simd[] = {
14431 /* pattern , mask , fn */
14432 { 0x0e200400, 0x9f200400, disas_simd_three_reg_same },
14433 { 0x0e008400, 0x9f208400, disas_simd_three_reg_same_extra },
14434 { 0x0e200000, 0x9f200c00, disas_simd_three_reg_diff },
14435 { 0x0e200800, 0x9f3e0c00, disas_simd_two_reg_misc },
14436 { 0x0e300800, 0x9f3e0c00, disas_simd_across_lanes },
14437 { 0x0e000400, 0x9fe08400, disas_simd_copy },
14438 { 0x0f000000, 0x9f000400, disas_simd_indexed }, /* vector indexed */
14439 /* simd_mod_imm decode is a subset of simd_shift_imm, so must precede it */
14440 { 0x0f000400, 0x9ff80400, disas_simd_mod_imm },
14441 { 0x0f000400, 0x9f800400, disas_simd_shift_imm },
14442 { 0x0e000000, 0xbf208c00, disas_simd_tb },
14443 { 0x0e000800, 0xbf208c00, disas_simd_zip_trn },
14444 { 0x2e000000, 0xbf208400, disas_simd_ext },
14445 { 0x5e200400, 0xdf200400, disas_simd_scalar_three_reg_same },
14446 { 0x5e008400, 0xdf208400, disas_simd_scalar_three_reg_same_extra },
14447 { 0x5e200000, 0xdf200c00, disas_simd_scalar_three_reg_diff },
14448 { 0x5e200800, 0xdf3e0c00, disas_simd_scalar_two_reg_misc },
14449 { 0x5e300800, 0xdf3e0c00, disas_simd_scalar_pairwise },
14450 { 0x5e000400, 0xdfe08400, disas_simd_scalar_copy },
14451 { 0x5f000000, 0xdf000400, disas_simd_indexed }, /* scalar indexed */
14452 { 0x5f000400, 0xdf800400, disas_simd_scalar_shift_imm },
14453 { 0x4e280800, 0xff3e0c00, disas_crypto_aes },
14454 { 0x5e000000, 0xff208c00, disas_crypto_three_reg_sha },
14455 { 0x5e280800, 0xff3e0c00, disas_crypto_two_reg_sha },
14456 { 0xce608000, 0xffe0b000, disas_crypto_three_reg_sha512 },
14457 { 0xcec08000, 0xfffff000, disas_crypto_two_reg_sha512 },
14458 { 0xce000000, 0xff808000, disas_crypto_four_reg },
14459 { 0xce800000, 0xffe00000, disas_crypto_xar },
14460 { 0xce408000, 0xffe0c000, disas_crypto_three_reg_imm2 },
14461 { 0x0e400400, 0x9f60c400, disas_simd_three_reg_same_fp16 },
14462 { 0x0e780800, 0x8f7e0c00, disas_simd_two_reg_misc_fp16 },
14463 { 0x5e400400, 0xdf60c400, disas_simd_scalar_three_reg_same_fp16 },
14464 { 0x00000000, 0x00000000, NULL }
14467 static void disas_data_proc_simd(DisasContext *s, uint32_t insn)
14469 /* Note that this is called with all non-FP cases from
14470 * table C3-6 so it must UNDEF for entries not specifically
14471 * allocated to instructions in that table.
14473 AArch64DecodeFn *fn = lookup_disas_fn(&data_proc_simd[0], insn);
14474 if (fn) {
14475 fn(s, insn);
14476 } else {
14477 unallocated_encoding(s);
14481 /* C3.6 Data processing - SIMD and floating point */
14482 static void disas_data_proc_simd_fp(DisasContext *s, uint32_t insn)
14484 if (extract32(insn, 28, 1) == 1 && extract32(insn, 30, 1) == 0) {
14485 disas_data_proc_fp(s, insn);
14486 } else {
14487 /* SIMD, including crypto */
14488 disas_data_proc_simd(s, insn);
14493 * is_guarded_page:
14494 * @env: The cpu environment
14495 * @s: The DisasContext
14497 * Return true if the page is guarded.
14499 static bool is_guarded_page(CPUARMState *env, DisasContext *s)
14501 uint64_t addr = s->base.pc_first;
14502 #ifdef CONFIG_USER_ONLY
14503 return page_get_flags(addr) & PAGE_BTI;
14504 #else
14505 int mmu_idx = arm_to_core_mmu_idx(s->mmu_idx);
14506 unsigned int index = tlb_index(env, mmu_idx, addr);
14507 CPUTLBEntry *entry = tlb_entry(env, mmu_idx, addr);
14510 * We test this immediately after reading an insn, which means
14511 * that any normal page must be in the TLB. The only exception
14512 * would be for executing from flash or device memory, which
14513 * does not retain the TLB entry.
14515 * FIXME: Assume false for those, for now. We could use
14516 * arm_cpu_get_phys_page_attrs_debug to re-read the page
14517 * table entry even for that case.
14519 return (tlb_hit(entry->addr_code, addr) &&
14520 arm_tlb_bti_gp(&env_tlb(env)->d[mmu_idx].iotlb[index].attrs));
14521 #endif
14525 * btype_destination_ok:
14526 * @insn: The instruction at the branch destination
14527 * @bt: SCTLR_ELx.BT
14528 * @btype: PSTATE.BTYPE, and is non-zero
14530 * On a guarded page, there are a limited number of insns
14531 * that may be present at the branch target:
14532 * - branch target identifiers,
14533 * - paciasp, pacibsp,
14534 * - BRK insn
14535 * - HLT insn
14536 * Anything else causes a Branch Target Exception.
14538 * Return true if the branch is compatible, false to raise BTITRAP.
14540 static bool btype_destination_ok(uint32_t insn, bool bt, int btype)
14542 if ((insn & 0xfffff01fu) == 0xd503201fu) {
14543 /* HINT space */
14544 switch (extract32(insn, 5, 7)) {
14545 case 0b011001: /* PACIASP */
14546 case 0b011011: /* PACIBSP */
14548 * If SCTLR_ELx.BT, then PACI*SP are not compatible
14549 * with btype == 3. Otherwise all btype are ok.
14551 return !bt || btype != 3;
14552 case 0b100000: /* BTI */
14553 /* Not compatible with any btype. */
14554 return false;
14555 case 0b100010: /* BTI c */
14556 /* Not compatible with btype == 3 */
14557 return btype != 3;
14558 case 0b100100: /* BTI j */
14559 /* Not compatible with btype == 2 */
14560 return btype != 2;
14561 case 0b100110: /* BTI jc */
14562 /* Compatible with any btype. */
14563 return true;
14565 } else {
14566 switch (insn & 0xffe0001fu) {
14567 case 0xd4200000u: /* BRK */
14568 case 0xd4400000u: /* HLT */
14569 /* Give priority to the breakpoint exception. */
14570 return true;
14573 return false;
14576 static void aarch64_tr_init_disas_context(DisasContextBase *dcbase,
14577 CPUState *cpu)
14579 DisasContext *dc = container_of(dcbase, DisasContext, base);
14580 CPUARMState *env = cpu->env_ptr;
14581 ARMCPU *arm_cpu = env_archcpu(env);
14582 CPUARMTBFlags tb_flags = arm_tbflags_from_tb(dc->base.tb);
14583 int bound, core_mmu_idx;
14585 dc->isar = &arm_cpu->isar;
14586 dc->condjmp = 0;
14588 dc->aarch64 = true;
14589 /* If we are coming from secure EL0 in a system with a 32-bit EL3, then
14590 * there is no secure EL1, so we route exceptions to EL3.
14592 dc->secure_routed_to_el3 = arm_feature(env, ARM_FEATURE_EL3) &&
14593 !arm_el_is_aa64(env, 3);
14594 dc->thumb = false;
14595 dc->sctlr_b = 0;
14596 dc->be_data = EX_TBFLAG_ANY(tb_flags, BE_DATA) ? MO_BE : MO_LE;
14597 dc->condexec_mask = 0;
14598 dc->condexec_cond = 0;
14599 core_mmu_idx = EX_TBFLAG_ANY(tb_flags, MMUIDX);
14600 dc->mmu_idx = core_to_aa64_mmu_idx(core_mmu_idx);
14601 dc->tbii = EX_TBFLAG_A64(tb_flags, TBII);
14602 dc->tbid = EX_TBFLAG_A64(tb_flags, TBID);
14603 dc->tcma = EX_TBFLAG_A64(tb_flags, TCMA);
14604 dc->current_el = arm_mmu_idx_to_el(dc->mmu_idx);
14605 #if !defined(CONFIG_USER_ONLY)
14606 dc->user = (dc->current_el == 0);
14607 #endif
14608 dc->fp_excp_el = EX_TBFLAG_ANY(tb_flags, FPEXC_EL);
14609 dc->align_mem = EX_TBFLAG_ANY(tb_flags, ALIGN_MEM);
14610 dc->pstate_il = EX_TBFLAG_ANY(tb_flags, PSTATE__IL);
14611 dc->sve_excp_el = EX_TBFLAG_A64(tb_flags, SVEEXC_EL);
14612 dc->vl = (EX_TBFLAG_A64(tb_flags, VL) + 1) * 16;
14613 dc->pauth_active = EX_TBFLAG_A64(tb_flags, PAUTH_ACTIVE);
14614 dc->bt = EX_TBFLAG_A64(tb_flags, BT);
14615 dc->btype = EX_TBFLAG_A64(tb_flags, BTYPE);
14616 dc->unpriv = EX_TBFLAG_A64(tb_flags, UNPRIV);
14617 dc->ata = EX_TBFLAG_A64(tb_flags, ATA);
14618 dc->mte_active[0] = EX_TBFLAG_A64(tb_flags, MTE_ACTIVE);
14619 dc->mte_active[1] = EX_TBFLAG_A64(tb_flags, MTE0_ACTIVE);
14620 dc->vec_len = 0;
14621 dc->vec_stride = 0;
14622 dc->cp_regs = arm_cpu->cp_regs;
14623 dc->features = env->features;
14624 dc->dcz_blocksize = arm_cpu->dcz_blocksize;
14626 #ifdef CONFIG_USER_ONLY
14627 /* In sve_probe_page, we assume TBI is enabled. */
14628 tcg_debug_assert(dc->tbid & 1);
14629 #endif
14631 /* Single step state. The code-generation logic here is:
14632 * SS_ACTIVE == 0:
14633 * generate code with no special handling for single-stepping (except
14634 * that anything that can make us go to SS_ACTIVE == 1 must end the TB;
14635 * this happens anyway because those changes are all system register or
14636 * PSTATE writes).
14637 * SS_ACTIVE == 1, PSTATE.SS == 1: (active-not-pending)
14638 * emit code for one insn
14639 * emit code to clear PSTATE.SS
14640 * emit code to generate software step exception for completed step
14641 * end TB (as usual for having generated an exception)
14642 * SS_ACTIVE == 1, PSTATE.SS == 0: (active-pending)
14643 * emit code to generate a software step exception
14644 * end the TB
14646 dc->ss_active = EX_TBFLAG_ANY(tb_flags, SS_ACTIVE);
14647 dc->pstate_ss = EX_TBFLAG_ANY(tb_flags, PSTATE__SS);
14648 dc->is_ldex = false;
14649 dc->debug_target_el = EX_TBFLAG_ANY(tb_flags, DEBUG_TARGET_EL);
14651 /* Bound the number of insns to execute to those left on the page. */
14652 bound = -(dc->base.pc_first | TARGET_PAGE_MASK) / 4;
14654 /* If architectural single step active, limit to 1. */
14655 if (dc->ss_active) {
14656 bound = 1;
14658 dc->base.max_insns = MIN(dc->base.max_insns, bound);
14660 init_tmp_a64_array(dc);
14663 static void aarch64_tr_tb_start(DisasContextBase *db, CPUState *cpu)
14667 static void aarch64_tr_insn_start(DisasContextBase *dcbase, CPUState *cpu)
14669 DisasContext *dc = container_of(dcbase, DisasContext, base);
14671 tcg_gen_insn_start(dc->base.pc_next, 0, 0);
14672 dc->insn_start = tcg_last_op();
14675 static void aarch64_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)
14677 DisasContext *s = container_of(dcbase, DisasContext, base);
14678 CPUARMState *env = cpu->env_ptr;
14679 uint64_t pc = s->base.pc_next;
14680 uint32_t insn;
14682 /* Singlestep exceptions have the highest priority. */
14683 if (s->ss_active && !s->pstate_ss) {
14684 /* Singlestep state is Active-pending.
14685 * If we're in this state at the start of a TB then either
14686 * a) we just took an exception to an EL which is being debugged
14687 * and this is the first insn in the exception handler
14688 * b) debug exceptions were masked and we just unmasked them
14689 * without changing EL (eg by clearing PSTATE.D)
14690 * In either case we're going to take a swstep exception in the
14691 * "did not step an insn" case, and so the syndrome ISV and EX
14692 * bits should be zero.
14694 assert(s->base.num_insns == 1);
14695 gen_swstep_exception(s, 0, 0);
14696 s->base.is_jmp = DISAS_NORETURN;
14697 s->base.pc_next = pc + 4;
14698 return;
14701 if (pc & 3) {
14703 * PC alignment fault. This has priority over the instruction abort
14704 * that we would receive from a translation fault via arm_ldl_code.
14705 * This should only be possible after an indirect branch, at the
14706 * start of the TB.
14708 assert(s->base.num_insns == 1);
14709 gen_helper_exception_pc_alignment(cpu_env, tcg_constant_tl(pc));
14710 s->base.is_jmp = DISAS_NORETURN;
14711 s->base.pc_next = QEMU_ALIGN_UP(pc, 4);
14712 return;
14715 s->pc_curr = pc;
14716 insn = arm_ldl_code(env, &s->base, pc, s->sctlr_b);
14717 s->insn = insn;
14718 s->base.pc_next = pc + 4;
14720 s->fp_access_checked = false;
14721 s->sve_access_checked = false;
14723 if (s->pstate_il) {
14725 * Illegal execution state. This has priority over BTI
14726 * exceptions, but comes after instruction abort exceptions.
14728 gen_exception_insn_el(s, s->pc_curr, EXCP_UDEF,
14729 syn_illegalstate(), default_exception_el(s));
14730 return;
14733 if (dc_isar_feature(aa64_bti, s)) {
14734 if (s->base.num_insns == 1) {
14736 * At the first insn of the TB, compute s->guarded_page.
14737 * We delayed computing this until successfully reading
14738 * the first insn of the TB, above. This (mostly) ensures
14739 * that the softmmu tlb entry has been populated, and the
14740 * page table GP bit is available.
14742 * Note that we need to compute this even if btype == 0,
14743 * because this value is used for BR instructions later
14744 * where ENV is not available.
14746 s->guarded_page = is_guarded_page(env, s);
14748 /* First insn can have btype set to non-zero. */
14749 tcg_debug_assert(s->btype >= 0);
14752 * Note that the Branch Target Exception has fairly high
14753 * priority -- below debugging exceptions but above most
14754 * everything else. This allows us to handle this now
14755 * instead of waiting until the insn is otherwise decoded.
14757 if (s->btype != 0
14758 && s->guarded_page
14759 && !btype_destination_ok(insn, s->bt, s->btype)) {
14760 gen_exception_insn_el(s, s->pc_curr, EXCP_UDEF,
14761 syn_btitrap(s->btype),
14762 default_exception_el(s));
14763 return;
14765 } else {
14766 /* Not the first insn: btype must be 0. */
14767 tcg_debug_assert(s->btype == 0);
14771 switch (extract32(insn, 25, 4)) {
14772 case 0x0: case 0x1: case 0x3: /* UNALLOCATED */
14773 unallocated_encoding(s);
14774 break;
14775 case 0x2:
14776 if (!disas_sve(s, insn)) {
14777 unallocated_encoding(s);
14779 break;
14780 case 0x8: case 0x9: /* Data processing - immediate */
14781 disas_data_proc_imm(s, insn);
14782 break;
14783 case 0xa: case 0xb: /* Branch, exception generation and system insns */
14784 disas_b_exc_sys(s, insn);
14785 break;
14786 case 0x4:
14787 case 0x6:
14788 case 0xc:
14789 case 0xe: /* Loads and stores */
14790 disas_ldst(s, insn);
14791 break;
14792 case 0x5:
14793 case 0xd: /* Data processing - register */
14794 disas_data_proc_reg(s, insn);
14795 break;
14796 case 0x7:
14797 case 0xf: /* Data processing - SIMD and floating point */
14798 disas_data_proc_simd_fp(s, insn);
14799 break;
14800 default:
14801 assert(FALSE); /* all 15 cases should be handled above */
14802 break;
14805 /* if we allocated any temporaries, free them here */
14806 free_tmp_a64(s);
14809 * After execution of most insns, btype is reset to 0.
14810 * Note that we set btype == -1 when the insn sets btype.
14812 if (s->btype > 0 && s->base.is_jmp != DISAS_NORETURN) {
14813 reset_btype(s);
14816 translator_loop_temp_check(&s->base);
14819 static void aarch64_tr_tb_stop(DisasContextBase *dcbase, CPUState *cpu)
14821 DisasContext *dc = container_of(dcbase, DisasContext, base);
14823 if (unlikely(dc->ss_active)) {
14824 /* Note that this means single stepping WFI doesn't halt the CPU.
14825 * For conditional branch insns this is harmless unreachable code as
14826 * gen_goto_tb() has already handled emitting the debug exception
14827 * (and thus a tb-jump is not possible when singlestepping).
14829 switch (dc->base.is_jmp) {
14830 default:
14831 gen_a64_set_pc_im(dc->base.pc_next);
14832 /* fall through */
14833 case DISAS_EXIT:
14834 case DISAS_JUMP:
14835 gen_step_complete_exception(dc);
14836 break;
14837 case DISAS_NORETURN:
14838 break;
14840 } else {
14841 switch (dc->base.is_jmp) {
14842 case DISAS_NEXT:
14843 case DISAS_TOO_MANY:
14844 gen_goto_tb(dc, 1, dc->base.pc_next);
14845 break;
14846 default:
14847 case DISAS_UPDATE_EXIT:
14848 gen_a64_set_pc_im(dc->base.pc_next);
14849 /* fall through */
14850 case DISAS_EXIT:
14851 tcg_gen_exit_tb(NULL, 0);
14852 break;
14853 case DISAS_UPDATE_NOCHAIN:
14854 gen_a64_set_pc_im(dc->base.pc_next);
14855 /* fall through */
14856 case DISAS_JUMP:
14857 tcg_gen_lookup_and_goto_ptr();
14858 break;
14859 case DISAS_NORETURN:
14860 case DISAS_SWI:
14861 break;
14862 case DISAS_WFE:
14863 gen_a64_set_pc_im(dc->base.pc_next);
14864 gen_helper_wfe(cpu_env);
14865 break;
14866 case DISAS_YIELD:
14867 gen_a64_set_pc_im(dc->base.pc_next);
14868 gen_helper_yield(cpu_env);
14869 break;
14870 case DISAS_WFI:
14872 * This is a special case because we don't want to just halt
14873 * the CPU if trying to debug across a WFI.
14875 gen_a64_set_pc_im(dc->base.pc_next);
14876 gen_helper_wfi(cpu_env, tcg_constant_i32(4));
14878 * The helper doesn't necessarily throw an exception, but we
14879 * must go back to the main loop to check for interrupts anyway.
14881 tcg_gen_exit_tb(NULL, 0);
14882 break;
14887 static void aarch64_tr_disas_log(const DisasContextBase *dcbase,
14888 CPUState *cpu, FILE *logfile)
14890 DisasContext *dc = container_of(dcbase, DisasContext, base);
14892 fprintf(logfile, "IN: %s\n", lookup_symbol(dc->base.pc_first));
14893 target_disas(logfile, cpu, dc->base.pc_first, dc->base.tb->size);
14896 const TranslatorOps aarch64_translator_ops = {
14897 .init_disas_context = aarch64_tr_init_disas_context,
14898 .tb_start = aarch64_tr_tb_start,
14899 .insn_start = aarch64_tr_insn_start,
14900 .translate_insn = aarch64_tr_translate_insn,
14901 .tb_stop = aarch64_tr_tb_stop,
14902 .disas_log = aarch64_tr_disas_log,