target/arm: Implement bfloat widening fma (indexed)
[qemu/ar7.git] / target / arm / translate-a64.c
blob8713dfec17461e98c1b9d888599067194fab12ca
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"
31 #include "semihosting/semihost.h"
32 #include "exec/gen-icount.h"
34 #include "exec/helper-proto.h"
35 #include "exec/helper-gen.h"
36 #include "exec/log.h"
38 #include "trace-tcg.h"
39 #include "translate-a64.h"
40 #include "qemu/atomic128.h"
42 static TCGv_i64 cpu_X[32];
43 static TCGv_i64 cpu_pc;
45 /* Load/store exclusive handling */
46 static TCGv_i64 cpu_exclusive_high;
48 static const char *regnames[] = {
49 "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7",
50 "x8", "x9", "x10", "x11", "x12", "x13", "x14", "x15",
51 "x16", "x17", "x18", "x19", "x20", "x21", "x22", "x23",
52 "x24", "x25", "x26", "x27", "x28", "x29", "lr", "sp"
55 enum a64_shift_type {
56 A64_SHIFT_TYPE_LSL = 0,
57 A64_SHIFT_TYPE_LSR = 1,
58 A64_SHIFT_TYPE_ASR = 2,
59 A64_SHIFT_TYPE_ROR = 3
62 /* Table based decoder typedefs - used when the relevant bits for decode
63 * are too awkwardly scattered across the instruction (eg SIMD).
65 typedef void AArch64DecodeFn(DisasContext *s, uint32_t insn);
67 typedef struct AArch64DecodeTable {
68 uint32_t pattern;
69 uint32_t mask;
70 AArch64DecodeFn *disas_fn;
71 } AArch64DecodeTable;
73 /* initialize TCG globals. */
74 void a64_translate_init(void)
76 int i;
78 cpu_pc = tcg_global_mem_new_i64(cpu_env,
79 offsetof(CPUARMState, pc),
80 "pc");
81 for (i = 0; i < 32; i++) {
82 cpu_X[i] = tcg_global_mem_new_i64(cpu_env,
83 offsetof(CPUARMState, xregs[i]),
84 regnames[i]);
87 cpu_exclusive_high = tcg_global_mem_new_i64(cpu_env,
88 offsetof(CPUARMState, exclusive_high), "exclusive_high");
92 * Return the core mmu_idx to use for A64 "unprivileged load/store" insns
94 static int get_a64_user_mem_index(DisasContext *s)
97 * If AccType_UNPRIV is not used, the insn uses AccType_NORMAL,
98 * which is the usual mmu_idx for this cpu state.
100 ARMMMUIdx useridx = s->mmu_idx;
102 if (s->unpriv) {
104 * We have pre-computed the condition for AccType_UNPRIV.
105 * Therefore we should never get here with a mmu_idx for
106 * which we do not know the corresponding user mmu_idx.
108 switch (useridx) {
109 case ARMMMUIdx_E10_1:
110 case ARMMMUIdx_E10_1_PAN:
111 useridx = ARMMMUIdx_E10_0;
112 break;
113 case ARMMMUIdx_E20_2:
114 case ARMMMUIdx_E20_2_PAN:
115 useridx = ARMMMUIdx_E20_0;
116 break;
117 case ARMMMUIdx_SE10_1:
118 case ARMMMUIdx_SE10_1_PAN:
119 useridx = ARMMMUIdx_SE10_0;
120 break;
121 case ARMMMUIdx_SE20_2:
122 case ARMMMUIdx_SE20_2_PAN:
123 useridx = ARMMMUIdx_SE20_0;
124 break;
125 default:
126 g_assert_not_reached();
129 return arm_to_core_mmu_idx(useridx);
132 static void reset_btype(DisasContext *s)
134 if (s->btype != 0) {
135 TCGv_i32 zero = tcg_const_i32(0);
136 tcg_gen_st_i32(zero, cpu_env, offsetof(CPUARMState, btype));
137 tcg_temp_free_i32(zero);
138 s->btype = 0;
142 static void set_btype(DisasContext *s, int val)
144 TCGv_i32 tcg_val;
146 /* BTYPE is a 2-bit field, and 0 should be done with reset_btype. */
147 tcg_debug_assert(val >= 1 && val <= 3);
149 tcg_val = tcg_const_i32(val);
150 tcg_gen_st_i32(tcg_val, cpu_env, offsetof(CPUARMState, btype));
151 tcg_temp_free_i32(tcg_val);
152 s->btype = -1;
155 void gen_a64_set_pc_im(uint64_t val)
157 tcg_gen_movi_i64(cpu_pc, val);
161 * Handle Top Byte Ignore (TBI) bits.
163 * If address tagging is enabled via the TCR TBI bits:
164 * + for EL2 and EL3 there is only one TBI bit, and if it is set
165 * then the address is zero-extended, clearing bits [63:56]
166 * + for EL0 and EL1, TBI0 controls addresses with bit 55 == 0
167 * and TBI1 controls addressses with bit 55 == 1.
168 * If the appropriate TBI bit is set for the address then
169 * the address is sign-extended from bit 55 into bits [63:56]
171 * Here We have concatenated TBI{1,0} into tbi.
173 static void gen_top_byte_ignore(DisasContext *s, TCGv_i64 dst,
174 TCGv_i64 src, int tbi)
176 if (tbi == 0) {
177 /* Load unmodified address */
178 tcg_gen_mov_i64(dst, src);
179 } else if (!regime_has_2_ranges(s->mmu_idx)) {
180 /* Force tag byte to all zero */
181 tcg_gen_extract_i64(dst, src, 0, 56);
182 } else {
183 /* Sign-extend from bit 55. */
184 tcg_gen_sextract_i64(dst, src, 0, 56);
186 switch (tbi) {
187 case 1:
188 /* tbi0 but !tbi1: only use the extension if positive */
189 tcg_gen_and_i64(dst, dst, src);
190 break;
191 case 2:
192 /* !tbi0 but tbi1: only use the extension if negative */
193 tcg_gen_or_i64(dst, dst, src);
194 break;
195 case 3:
196 /* tbi0 and tbi1: always use the extension */
197 break;
198 default:
199 g_assert_not_reached();
204 static void gen_a64_set_pc(DisasContext *s, TCGv_i64 src)
207 * If address tagging is enabled for instructions via the TCR TBI bits,
208 * then loading an address into the PC will clear out any tag.
210 gen_top_byte_ignore(s, cpu_pc, src, s->tbii);
214 * Handle MTE and/or TBI.
216 * For TBI, ideally, we would do nothing. Proper behaviour on fault is
217 * for the tag to be present in the FAR_ELx register. But for user-only
218 * mode we do not have a TLB with which to implement this, so we must
219 * remove the top byte now.
221 * Always return a fresh temporary that we can increment independently
222 * of the write-back address.
225 TCGv_i64 clean_data_tbi(DisasContext *s, TCGv_i64 addr)
227 TCGv_i64 clean = new_tmp_a64(s);
228 #ifdef CONFIG_USER_ONLY
229 gen_top_byte_ignore(s, clean, addr, s->tbid);
230 #else
231 tcg_gen_mov_i64(clean, addr);
232 #endif
233 return clean;
236 /* Insert a zero tag into src, with the result at dst. */
237 static void gen_address_with_allocation_tag0(TCGv_i64 dst, TCGv_i64 src)
239 tcg_gen_andi_i64(dst, src, ~MAKE_64BIT_MASK(56, 4));
242 static void gen_probe_access(DisasContext *s, TCGv_i64 ptr,
243 MMUAccessType acc, int log2_size)
245 TCGv_i32 t_acc = tcg_const_i32(acc);
246 TCGv_i32 t_idx = tcg_const_i32(get_mem_index(s));
247 TCGv_i32 t_size = tcg_const_i32(1 << log2_size);
249 gen_helper_probe_access(cpu_env, ptr, t_acc, t_idx, t_size);
250 tcg_temp_free_i32(t_acc);
251 tcg_temp_free_i32(t_idx);
252 tcg_temp_free_i32(t_size);
256 * For MTE, check a single logical or atomic access. This probes a single
257 * address, the exact one specified. The size and alignment of the access
258 * is not relevant to MTE, per se, but watchpoints do require the size,
259 * and we want to recognize those before making any other changes to state.
261 static TCGv_i64 gen_mte_check1_mmuidx(DisasContext *s, TCGv_i64 addr,
262 bool is_write, bool tag_checked,
263 int log2_size, bool is_unpriv,
264 int core_idx)
266 if (tag_checked && s->mte_active[is_unpriv]) {
267 TCGv_i32 tcg_desc;
268 TCGv_i64 ret;
269 int desc = 0;
271 desc = FIELD_DP32(desc, MTEDESC, MIDX, core_idx);
272 desc = FIELD_DP32(desc, MTEDESC, TBI, s->tbid);
273 desc = FIELD_DP32(desc, MTEDESC, TCMA, s->tcma);
274 desc = FIELD_DP32(desc, MTEDESC, WRITE, is_write);
275 desc = FIELD_DP32(desc, MTEDESC, SIZEM1, (1 << log2_size) - 1);
276 tcg_desc = tcg_const_i32(desc);
278 ret = new_tmp_a64(s);
279 gen_helper_mte_check(ret, cpu_env, tcg_desc, addr);
280 tcg_temp_free_i32(tcg_desc);
282 return ret;
284 return clean_data_tbi(s, addr);
287 TCGv_i64 gen_mte_check1(DisasContext *s, TCGv_i64 addr, bool is_write,
288 bool tag_checked, int log2_size)
290 return gen_mte_check1_mmuidx(s, addr, is_write, tag_checked, log2_size,
291 false, get_mem_index(s));
295 * For MTE, check multiple logical sequential accesses.
297 TCGv_i64 gen_mte_checkN(DisasContext *s, TCGv_i64 addr, bool is_write,
298 bool tag_checked, int size)
300 if (tag_checked && s->mte_active[0]) {
301 TCGv_i32 tcg_desc;
302 TCGv_i64 ret;
303 int desc = 0;
305 desc = FIELD_DP32(desc, MTEDESC, MIDX, get_mem_index(s));
306 desc = FIELD_DP32(desc, MTEDESC, TBI, s->tbid);
307 desc = FIELD_DP32(desc, MTEDESC, TCMA, s->tcma);
308 desc = FIELD_DP32(desc, MTEDESC, WRITE, is_write);
309 desc = FIELD_DP32(desc, MTEDESC, SIZEM1, size - 1);
310 tcg_desc = tcg_const_i32(desc);
312 ret = new_tmp_a64(s);
313 gen_helper_mte_check(ret, cpu_env, tcg_desc, addr);
314 tcg_temp_free_i32(tcg_desc);
316 return ret;
318 return clean_data_tbi(s, addr);
321 typedef struct DisasCompare64 {
322 TCGCond cond;
323 TCGv_i64 value;
324 } DisasCompare64;
326 static void a64_test_cc(DisasCompare64 *c64, int cc)
328 DisasCompare c32;
330 arm_test_cc(&c32, cc);
332 /* Sign-extend the 32-bit value so that the GE/LT comparisons work
333 * properly. The NE/EQ comparisons are also fine with this choice. */
334 c64->cond = c32.cond;
335 c64->value = tcg_temp_new_i64();
336 tcg_gen_ext_i32_i64(c64->value, c32.value);
338 arm_free_cc(&c32);
341 static void a64_free_cc(DisasCompare64 *c64)
343 tcg_temp_free_i64(c64->value);
346 static void gen_exception_internal(int excp)
348 TCGv_i32 tcg_excp = tcg_const_i32(excp);
350 assert(excp_is_internal(excp));
351 gen_helper_exception_internal(cpu_env, tcg_excp);
352 tcg_temp_free_i32(tcg_excp);
355 static void gen_exception_internal_insn(DisasContext *s, uint64_t pc, int excp)
357 gen_a64_set_pc_im(pc);
358 gen_exception_internal(excp);
359 s->base.is_jmp = DISAS_NORETURN;
362 static void gen_exception_bkpt_insn(DisasContext *s, uint32_t syndrome)
364 TCGv_i32 tcg_syn;
366 gen_a64_set_pc_im(s->pc_curr);
367 tcg_syn = tcg_const_i32(syndrome);
368 gen_helper_exception_bkpt_insn(cpu_env, tcg_syn);
369 tcg_temp_free_i32(tcg_syn);
370 s->base.is_jmp = DISAS_NORETURN;
373 static void gen_step_complete_exception(DisasContext *s)
375 /* We just completed step of an insn. Move from Active-not-pending
376 * to Active-pending, and then also take the swstep exception.
377 * This corresponds to making the (IMPDEF) choice to prioritize
378 * swstep exceptions over asynchronous exceptions taken to an exception
379 * level where debug is disabled. This choice has the advantage that
380 * we do not need to maintain internal state corresponding to the
381 * ISV/EX syndrome bits between completion of the step and generation
382 * of the exception, and our syndrome information is always correct.
384 gen_ss_advance(s);
385 gen_swstep_exception(s, 1, s->is_ldex);
386 s->base.is_jmp = DISAS_NORETURN;
389 static inline bool use_goto_tb(DisasContext *s, int n, uint64_t dest)
391 /* No direct tb linking with singlestep (either QEMU's or the ARM
392 * debug architecture kind) or deterministic io
394 if (s->base.singlestep_enabled || s->ss_active ||
395 (tb_cflags(s->base.tb) & CF_LAST_IO)) {
396 return false;
399 #ifndef CONFIG_USER_ONLY
400 /* Only link tbs from inside the same guest page */
401 if ((s->base.tb->pc & TARGET_PAGE_MASK) != (dest & TARGET_PAGE_MASK)) {
402 return false;
404 #endif
406 return true;
409 static inline void gen_goto_tb(DisasContext *s, int n, uint64_t dest)
411 const TranslationBlock *tb;
413 tb = s->base.tb;
414 if (use_goto_tb(s, n, dest)) {
415 tcg_gen_goto_tb(n);
416 gen_a64_set_pc_im(dest);
417 tcg_gen_exit_tb(tb, n);
418 s->base.is_jmp = DISAS_NORETURN;
419 } else {
420 gen_a64_set_pc_im(dest);
421 if (s->ss_active) {
422 gen_step_complete_exception(s);
423 } else if (s->base.singlestep_enabled) {
424 gen_exception_internal(EXCP_DEBUG);
425 } else {
426 tcg_gen_lookup_and_goto_ptr();
427 s->base.is_jmp = DISAS_NORETURN;
432 static void init_tmp_a64_array(DisasContext *s)
434 #ifdef CONFIG_DEBUG_TCG
435 memset(s->tmp_a64, 0, sizeof(s->tmp_a64));
436 #endif
437 s->tmp_a64_count = 0;
440 static void free_tmp_a64(DisasContext *s)
442 int i;
443 for (i = 0; i < s->tmp_a64_count; i++) {
444 tcg_temp_free_i64(s->tmp_a64[i]);
446 init_tmp_a64_array(s);
449 TCGv_i64 new_tmp_a64(DisasContext *s)
451 assert(s->tmp_a64_count < TMP_A64_MAX);
452 return s->tmp_a64[s->tmp_a64_count++] = tcg_temp_new_i64();
455 TCGv_i64 new_tmp_a64_local(DisasContext *s)
457 assert(s->tmp_a64_count < TMP_A64_MAX);
458 return s->tmp_a64[s->tmp_a64_count++] = tcg_temp_local_new_i64();
461 TCGv_i64 new_tmp_a64_zero(DisasContext *s)
463 TCGv_i64 t = new_tmp_a64(s);
464 tcg_gen_movi_i64(t, 0);
465 return t;
469 * Register access functions
471 * These functions are used for directly accessing a register in where
472 * changes to the final register value are likely to be made. If you
473 * need to use a register for temporary calculation (e.g. index type
474 * operations) use the read_* form.
476 * B1.2.1 Register mappings
478 * In instruction register encoding 31 can refer to ZR (zero register) or
479 * the SP (stack pointer) depending on context. In QEMU's case we map SP
480 * to cpu_X[31] and ZR accesses to a temporary which can be discarded.
481 * This is the point of the _sp forms.
483 TCGv_i64 cpu_reg(DisasContext *s, int reg)
485 if (reg == 31) {
486 return new_tmp_a64_zero(s);
487 } else {
488 return cpu_X[reg];
492 /* register access for when 31 == SP */
493 TCGv_i64 cpu_reg_sp(DisasContext *s, int reg)
495 return cpu_X[reg];
498 /* read a cpu register in 32bit/64bit mode. Returns a TCGv_i64
499 * representing the register contents. This TCGv is an auto-freed
500 * temporary so it need not be explicitly freed, and may be modified.
502 TCGv_i64 read_cpu_reg(DisasContext *s, int reg, int sf)
504 TCGv_i64 v = new_tmp_a64(s);
505 if (reg != 31) {
506 if (sf) {
507 tcg_gen_mov_i64(v, cpu_X[reg]);
508 } else {
509 tcg_gen_ext32u_i64(v, cpu_X[reg]);
511 } else {
512 tcg_gen_movi_i64(v, 0);
514 return v;
517 TCGv_i64 read_cpu_reg_sp(DisasContext *s, int reg, int sf)
519 TCGv_i64 v = new_tmp_a64(s);
520 if (sf) {
521 tcg_gen_mov_i64(v, cpu_X[reg]);
522 } else {
523 tcg_gen_ext32u_i64(v, cpu_X[reg]);
525 return v;
528 /* Return the offset into CPUARMState of a slice (from
529 * the least significant end) of FP register Qn (ie
530 * Dn, Sn, Hn or Bn).
531 * (Note that this is not the same mapping as for A32; see cpu.h)
533 static inline int fp_reg_offset(DisasContext *s, int regno, MemOp size)
535 return vec_reg_offset(s, regno, 0, size);
538 /* Offset of the high half of the 128 bit vector Qn */
539 static inline int fp_reg_hi_offset(DisasContext *s, int regno)
541 return vec_reg_offset(s, regno, 1, MO_64);
544 /* Convenience accessors for reading and writing single and double
545 * FP registers. Writing clears the upper parts of the associated
546 * 128 bit vector register, as required by the architecture.
547 * Note that unlike the GP register accessors, the values returned
548 * by the read functions must be manually freed.
550 static TCGv_i64 read_fp_dreg(DisasContext *s, int reg)
552 TCGv_i64 v = tcg_temp_new_i64();
554 tcg_gen_ld_i64(v, cpu_env, fp_reg_offset(s, reg, MO_64));
555 return v;
558 static TCGv_i32 read_fp_sreg(DisasContext *s, int reg)
560 TCGv_i32 v = tcg_temp_new_i32();
562 tcg_gen_ld_i32(v, cpu_env, fp_reg_offset(s, reg, MO_32));
563 return v;
566 static TCGv_i32 read_fp_hreg(DisasContext *s, int reg)
568 TCGv_i32 v = tcg_temp_new_i32();
570 tcg_gen_ld16u_i32(v, cpu_env, fp_reg_offset(s, reg, MO_16));
571 return v;
574 /* Clear the bits above an N-bit vector, for N = (is_q ? 128 : 64).
575 * If SVE is not enabled, then there are only 128 bits in the vector.
577 static void clear_vec_high(DisasContext *s, bool is_q, int rd)
579 unsigned ofs = fp_reg_offset(s, rd, MO_64);
580 unsigned vsz = vec_full_reg_size(s);
582 /* Nop move, with side effect of clearing the tail. */
583 tcg_gen_gvec_mov(MO_64, ofs, ofs, is_q ? 16 : 8, vsz);
586 void write_fp_dreg(DisasContext *s, int reg, TCGv_i64 v)
588 unsigned ofs = fp_reg_offset(s, reg, MO_64);
590 tcg_gen_st_i64(v, cpu_env, ofs);
591 clear_vec_high(s, false, reg);
594 static void write_fp_sreg(DisasContext *s, int reg, TCGv_i32 v)
596 TCGv_i64 tmp = tcg_temp_new_i64();
598 tcg_gen_extu_i32_i64(tmp, v);
599 write_fp_dreg(s, reg, tmp);
600 tcg_temp_free_i64(tmp);
603 /* Expand a 2-operand AdvSIMD vector operation using an expander function. */
604 static void gen_gvec_fn2(DisasContext *s, bool is_q, int rd, int rn,
605 GVecGen2Fn *gvec_fn, int vece)
607 gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
608 is_q ? 16 : 8, vec_full_reg_size(s));
611 /* Expand a 2-operand + immediate AdvSIMD vector operation using
612 * an expander function.
614 static void gen_gvec_fn2i(DisasContext *s, bool is_q, int rd, int rn,
615 int64_t imm, GVecGen2iFn *gvec_fn, int vece)
617 gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
618 imm, is_q ? 16 : 8, vec_full_reg_size(s));
621 /* Expand a 3-operand AdvSIMD vector operation using an expander function. */
622 static void gen_gvec_fn3(DisasContext *s, bool is_q, int rd, int rn, int rm,
623 GVecGen3Fn *gvec_fn, int vece)
625 gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
626 vec_full_reg_offset(s, rm), is_q ? 16 : 8, vec_full_reg_size(s));
629 /* Expand a 4-operand AdvSIMD vector operation using an expander function. */
630 static void gen_gvec_fn4(DisasContext *s, bool is_q, int rd, int rn, int rm,
631 int rx, GVecGen4Fn *gvec_fn, int vece)
633 gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
634 vec_full_reg_offset(s, rm), vec_full_reg_offset(s, rx),
635 is_q ? 16 : 8, vec_full_reg_size(s));
638 /* Expand a 2-operand operation using an out-of-line helper. */
639 static void gen_gvec_op2_ool(DisasContext *s, bool is_q, int rd,
640 int rn, int data, gen_helper_gvec_2 *fn)
642 tcg_gen_gvec_2_ool(vec_full_reg_offset(s, rd),
643 vec_full_reg_offset(s, rn),
644 is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
647 /* Expand a 3-operand operation using an out-of-line helper. */
648 static void gen_gvec_op3_ool(DisasContext *s, bool is_q, int rd,
649 int rn, int rm, int data, gen_helper_gvec_3 *fn)
651 tcg_gen_gvec_3_ool(vec_full_reg_offset(s, rd),
652 vec_full_reg_offset(s, rn),
653 vec_full_reg_offset(s, rm),
654 is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
657 /* Expand a 3-operand + fpstatus pointer + simd data value operation using
658 * an out-of-line helper.
660 static void gen_gvec_op3_fpst(DisasContext *s, bool is_q, int rd, int rn,
661 int rm, bool is_fp16, int data,
662 gen_helper_gvec_3_ptr *fn)
664 TCGv_ptr fpst = fpstatus_ptr(is_fp16 ? FPST_FPCR_F16 : FPST_FPCR);
665 tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
666 vec_full_reg_offset(s, rn),
667 vec_full_reg_offset(s, rm), fpst,
668 is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
669 tcg_temp_free_ptr(fpst);
672 /* Expand a 3-operand + qc + operation using an out-of-line helper. */
673 static void gen_gvec_op3_qc(DisasContext *s, bool is_q, int rd, int rn,
674 int rm, gen_helper_gvec_3_ptr *fn)
676 TCGv_ptr qc_ptr = tcg_temp_new_ptr();
678 tcg_gen_addi_ptr(qc_ptr, cpu_env, offsetof(CPUARMState, vfp.qc));
679 tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
680 vec_full_reg_offset(s, rn),
681 vec_full_reg_offset(s, rm), qc_ptr,
682 is_q ? 16 : 8, vec_full_reg_size(s), 0, fn);
683 tcg_temp_free_ptr(qc_ptr);
686 /* Expand a 4-operand operation using an out-of-line helper. */
687 static void gen_gvec_op4_ool(DisasContext *s, bool is_q, int rd, int rn,
688 int rm, int ra, int data, gen_helper_gvec_4 *fn)
690 tcg_gen_gvec_4_ool(vec_full_reg_offset(s, rd),
691 vec_full_reg_offset(s, rn),
692 vec_full_reg_offset(s, rm),
693 vec_full_reg_offset(s, ra),
694 is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
698 * Expand a 4-operand + fpstatus pointer + simd data value operation using
699 * an out-of-line helper.
701 static void gen_gvec_op4_fpst(DisasContext *s, bool is_q, int rd, int rn,
702 int rm, int ra, bool is_fp16, int data,
703 gen_helper_gvec_4_ptr *fn)
705 TCGv_ptr fpst = fpstatus_ptr(is_fp16 ? FPST_FPCR_F16 : FPST_FPCR);
706 tcg_gen_gvec_4_ptr(vec_full_reg_offset(s, rd),
707 vec_full_reg_offset(s, rn),
708 vec_full_reg_offset(s, rm),
709 vec_full_reg_offset(s, ra), fpst,
710 is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
711 tcg_temp_free_ptr(fpst);
714 /* Set ZF and NF based on a 64 bit result. This is alas fiddlier
715 * than the 32 bit equivalent.
717 static inline void gen_set_NZ64(TCGv_i64 result)
719 tcg_gen_extr_i64_i32(cpu_ZF, cpu_NF, result);
720 tcg_gen_or_i32(cpu_ZF, cpu_ZF, cpu_NF);
723 /* Set NZCV as for a logical operation: NZ as per result, CV cleared. */
724 static inline void gen_logic_CC(int sf, TCGv_i64 result)
726 if (sf) {
727 gen_set_NZ64(result);
728 } else {
729 tcg_gen_extrl_i64_i32(cpu_ZF, result);
730 tcg_gen_mov_i32(cpu_NF, cpu_ZF);
732 tcg_gen_movi_i32(cpu_CF, 0);
733 tcg_gen_movi_i32(cpu_VF, 0);
736 /* dest = T0 + T1; compute C, N, V and Z flags */
737 static void gen_add_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
739 if (sf) {
740 TCGv_i64 result, flag, tmp;
741 result = tcg_temp_new_i64();
742 flag = tcg_temp_new_i64();
743 tmp = tcg_temp_new_i64();
745 tcg_gen_movi_i64(tmp, 0);
746 tcg_gen_add2_i64(result, flag, t0, tmp, t1, tmp);
748 tcg_gen_extrl_i64_i32(cpu_CF, flag);
750 gen_set_NZ64(result);
752 tcg_gen_xor_i64(flag, result, t0);
753 tcg_gen_xor_i64(tmp, t0, t1);
754 tcg_gen_andc_i64(flag, flag, tmp);
755 tcg_temp_free_i64(tmp);
756 tcg_gen_extrh_i64_i32(cpu_VF, flag);
758 tcg_gen_mov_i64(dest, result);
759 tcg_temp_free_i64(result);
760 tcg_temp_free_i64(flag);
761 } else {
762 /* 32 bit arithmetic */
763 TCGv_i32 t0_32 = tcg_temp_new_i32();
764 TCGv_i32 t1_32 = tcg_temp_new_i32();
765 TCGv_i32 tmp = tcg_temp_new_i32();
767 tcg_gen_movi_i32(tmp, 0);
768 tcg_gen_extrl_i64_i32(t0_32, t0);
769 tcg_gen_extrl_i64_i32(t1_32, t1);
770 tcg_gen_add2_i32(cpu_NF, cpu_CF, t0_32, tmp, t1_32, tmp);
771 tcg_gen_mov_i32(cpu_ZF, cpu_NF);
772 tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
773 tcg_gen_xor_i32(tmp, t0_32, t1_32);
774 tcg_gen_andc_i32(cpu_VF, cpu_VF, tmp);
775 tcg_gen_extu_i32_i64(dest, cpu_NF);
777 tcg_temp_free_i32(tmp);
778 tcg_temp_free_i32(t0_32);
779 tcg_temp_free_i32(t1_32);
783 /* dest = T0 - T1; compute C, N, V and Z flags */
784 static void gen_sub_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
786 if (sf) {
787 /* 64 bit arithmetic */
788 TCGv_i64 result, flag, tmp;
790 result = tcg_temp_new_i64();
791 flag = tcg_temp_new_i64();
792 tcg_gen_sub_i64(result, t0, t1);
794 gen_set_NZ64(result);
796 tcg_gen_setcond_i64(TCG_COND_GEU, flag, t0, t1);
797 tcg_gen_extrl_i64_i32(cpu_CF, flag);
799 tcg_gen_xor_i64(flag, result, t0);
800 tmp = tcg_temp_new_i64();
801 tcg_gen_xor_i64(tmp, t0, t1);
802 tcg_gen_and_i64(flag, flag, tmp);
803 tcg_temp_free_i64(tmp);
804 tcg_gen_extrh_i64_i32(cpu_VF, flag);
805 tcg_gen_mov_i64(dest, result);
806 tcg_temp_free_i64(flag);
807 tcg_temp_free_i64(result);
808 } else {
809 /* 32 bit arithmetic */
810 TCGv_i32 t0_32 = tcg_temp_new_i32();
811 TCGv_i32 t1_32 = tcg_temp_new_i32();
812 TCGv_i32 tmp;
814 tcg_gen_extrl_i64_i32(t0_32, t0);
815 tcg_gen_extrl_i64_i32(t1_32, t1);
816 tcg_gen_sub_i32(cpu_NF, t0_32, t1_32);
817 tcg_gen_mov_i32(cpu_ZF, cpu_NF);
818 tcg_gen_setcond_i32(TCG_COND_GEU, cpu_CF, t0_32, t1_32);
819 tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
820 tmp = tcg_temp_new_i32();
821 tcg_gen_xor_i32(tmp, t0_32, t1_32);
822 tcg_temp_free_i32(t0_32);
823 tcg_temp_free_i32(t1_32);
824 tcg_gen_and_i32(cpu_VF, cpu_VF, tmp);
825 tcg_temp_free_i32(tmp);
826 tcg_gen_extu_i32_i64(dest, cpu_NF);
830 /* dest = T0 + T1 + CF; do not compute flags. */
831 static void gen_adc(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
833 TCGv_i64 flag = tcg_temp_new_i64();
834 tcg_gen_extu_i32_i64(flag, cpu_CF);
835 tcg_gen_add_i64(dest, t0, t1);
836 tcg_gen_add_i64(dest, dest, flag);
837 tcg_temp_free_i64(flag);
839 if (!sf) {
840 tcg_gen_ext32u_i64(dest, dest);
844 /* dest = T0 + T1 + CF; compute C, N, V and Z flags. */
845 static void gen_adc_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
847 if (sf) {
848 TCGv_i64 result, cf_64, vf_64, tmp;
849 result = tcg_temp_new_i64();
850 cf_64 = tcg_temp_new_i64();
851 vf_64 = tcg_temp_new_i64();
852 tmp = tcg_const_i64(0);
854 tcg_gen_extu_i32_i64(cf_64, cpu_CF);
855 tcg_gen_add2_i64(result, cf_64, t0, tmp, cf_64, tmp);
856 tcg_gen_add2_i64(result, cf_64, result, cf_64, t1, tmp);
857 tcg_gen_extrl_i64_i32(cpu_CF, cf_64);
858 gen_set_NZ64(result);
860 tcg_gen_xor_i64(vf_64, result, t0);
861 tcg_gen_xor_i64(tmp, t0, t1);
862 tcg_gen_andc_i64(vf_64, vf_64, tmp);
863 tcg_gen_extrh_i64_i32(cpu_VF, vf_64);
865 tcg_gen_mov_i64(dest, result);
867 tcg_temp_free_i64(tmp);
868 tcg_temp_free_i64(vf_64);
869 tcg_temp_free_i64(cf_64);
870 tcg_temp_free_i64(result);
871 } else {
872 TCGv_i32 t0_32, t1_32, tmp;
873 t0_32 = tcg_temp_new_i32();
874 t1_32 = tcg_temp_new_i32();
875 tmp = tcg_const_i32(0);
877 tcg_gen_extrl_i64_i32(t0_32, t0);
878 tcg_gen_extrl_i64_i32(t1_32, t1);
879 tcg_gen_add2_i32(cpu_NF, cpu_CF, t0_32, tmp, cpu_CF, tmp);
880 tcg_gen_add2_i32(cpu_NF, cpu_CF, cpu_NF, cpu_CF, t1_32, tmp);
882 tcg_gen_mov_i32(cpu_ZF, cpu_NF);
883 tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
884 tcg_gen_xor_i32(tmp, t0_32, t1_32);
885 tcg_gen_andc_i32(cpu_VF, cpu_VF, tmp);
886 tcg_gen_extu_i32_i64(dest, cpu_NF);
888 tcg_temp_free_i32(tmp);
889 tcg_temp_free_i32(t1_32);
890 tcg_temp_free_i32(t0_32);
895 * Load/Store generators
899 * Store from GPR register to memory.
901 static void do_gpr_st_memidx(DisasContext *s, TCGv_i64 source,
902 TCGv_i64 tcg_addr, MemOp memop, int memidx,
903 bool iss_valid,
904 unsigned int iss_srt,
905 bool iss_sf, bool iss_ar)
907 memop = finalize_memop(s, memop);
908 tcg_gen_qemu_st_i64(source, tcg_addr, memidx, memop);
910 if (iss_valid) {
911 uint32_t syn;
913 syn = syn_data_abort_with_iss(0,
914 (memop & MO_SIZE),
915 false,
916 iss_srt,
917 iss_sf,
918 iss_ar,
919 0, 0, 0, 0, 0, false);
920 disas_set_insn_syndrome(s, syn);
924 static void do_gpr_st(DisasContext *s, TCGv_i64 source,
925 TCGv_i64 tcg_addr, MemOp memop,
926 bool iss_valid,
927 unsigned int iss_srt,
928 bool iss_sf, bool iss_ar)
930 do_gpr_st_memidx(s, source, tcg_addr, memop, get_mem_index(s),
931 iss_valid, iss_srt, iss_sf, iss_ar);
935 * Load from memory to GPR register
937 static void do_gpr_ld_memidx(DisasContext *s, TCGv_i64 dest, TCGv_i64 tcg_addr,
938 MemOp memop, bool extend, int memidx,
939 bool iss_valid, unsigned int iss_srt,
940 bool iss_sf, bool iss_ar)
942 memop = finalize_memop(s, memop);
943 tcg_gen_qemu_ld_i64(dest, tcg_addr, memidx, memop);
945 if (extend && (memop & MO_SIGN)) {
946 g_assert((memop & MO_SIZE) <= MO_32);
947 tcg_gen_ext32u_i64(dest, dest);
950 if (iss_valid) {
951 uint32_t syn;
953 syn = syn_data_abort_with_iss(0,
954 (memop & MO_SIZE),
955 (memop & MO_SIGN) != 0,
956 iss_srt,
957 iss_sf,
958 iss_ar,
959 0, 0, 0, 0, 0, false);
960 disas_set_insn_syndrome(s, syn);
964 static void do_gpr_ld(DisasContext *s, TCGv_i64 dest, TCGv_i64 tcg_addr,
965 MemOp memop, bool extend,
966 bool iss_valid, unsigned int iss_srt,
967 bool iss_sf, bool iss_ar)
969 do_gpr_ld_memidx(s, dest, tcg_addr, memop, extend, get_mem_index(s),
970 iss_valid, iss_srt, iss_sf, iss_ar);
974 * Store from FP register to memory
976 static void do_fp_st(DisasContext *s, int srcidx, TCGv_i64 tcg_addr, int size)
978 /* This writes the bottom N bits of a 128 bit wide vector to memory */
979 TCGv_i64 tmplo = tcg_temp_new_i64();
980 MemOp mop;
982 tcg_gen_ld_i64(tmplo, cpu_env, fp_reg_offset(s, srcidx, MO_64));
984 if (size < 4) {
985 mop = finalize_memop(s, size);
986 tcg_gen_qemu_st_i64(tmplo, tcg_addr, get_mem_index(s), mop);
987 } else {
988 bool be = s->be_data == MO_BE;
989 TCGv_i64 tcg_hiaddr = tcg_temp_new_i64();
990 TCGv_i64 tmphi = tcg_temp_new_i64();
992 tcg_gen_ld_i64(tmphi, cpu_env, fp_reg_hi_offset(s, srcidx));
994 mop = s->be_data | MO_Q;
995 tcg_gen_qemu_st_i64(be ? tmphi : tmplo, tcg_addr, get_mem_index(s),
996 mop | (s->align_mem ? MO_ALIGN_16 : 0));
997 tcg_gen_addi_i64(tcg_hiaddr, tcg_addr, 8);
998 tcg_gen_qemu_st_i64(be ? tmplo : tmphi, tcg_hiaddr,
999 get_mem_index(s), mop);
1001 tcg_temp_free_i64(tcg_hiaddr);
1002 tcg_temp_free_i64(tmphi);
1005 tcg_temp_free_i64(tmplo);
1009 * Load from memory to FP register
1011 static void do_fp_ld(DisasContext *s, int destidx, TCGv_i64 tcg_addr, int size)
1013 /* This always zero-extends and writes to a full 128 bit wide vector */
1014 TCGv_i64 tmplo = tcg_temp_new_i64();
1015 TCGv_i64 tmphi = NULL;
1016 MemOp mop;
1018 if (size < 4) {
1019 mop = finalize_memop(s, size);
1020 tcg_gen_qemu_ld_i64(tmplo, tcg_addr, get_mem_index(s), mop);
1021 } else {
1022 bool be = s->be_data == MO_BE;
1023 TCGv_i64 tcg_hiaddr;
1025 tmphi = tcg_temp_new_i64();
1026 tcg_hiaddr = tcg_temp_new_i64();
1028 mop = s->be_data | MO_Q;
1029 tcg_gen_qemu_ld_i64(be ? tmphi : tmplo, tcg_addr, get_mem_index(s),
1030 mop | (s->align_mem ? MO_ALIGN_16 : 0));
1031 tcg_gen_addi_i64(tcg_hiaddr, tcg_addr, 8);
1032 tcg_gen_qemu_ld_i64(be ? tmplo : tmphi, tcg_hiaddr,
1033 get_mem_index(s), mop);
1034 tcg_temp_free_i64(tcg_hiaddr);
1037 tcg_gen_st_i64(tmplo, cpu_env, fp_reg_offset(s, destidx, MO_64));
1038 tcg_temp_free_i64(tmplo);
1040 if (tmphi) {
1041 tcg_gen_st_i64(tmphi, cpu_env, fp_reg_hi_offset(s, destidx));
1042 tcg_temp_free_i64(tmphi);
1044 clear_vec_high(s, tmphi != NULL, destidx);
1048 * Vector load/store helpers.
1050 * The principal difference between this and a FP load is that we don't
1051 * zero extend as we are filling a partial chunk of the vector register.
1052 * These functions don't support 128 bit loads/stores, which would be
1053 * normal load/store operations.
1055 * The _i32 versions are useful when operating on 32 bit quantities
1056 * (eg for floating point single or using Neon helper functions).
1059 /* Get value of an element within a vector register */
1060 static void read_vec_element(DisasContext *s, TCGv_i64 tcg_dest, int srcidx,
1061 int element, MemOp memop)
1063 int vect_off = vec_reg_offset(s, srcidx, element, memop & MO_SIZE);
1064 switch (memop) {
1065 case MO_8:
1066 tcg_gen_ld8u_i64(tcg_dest, cpu_env, vect_off);
1067 break;
1068 case MO_16:
1069 tcg_gen_ld16u_i64(tcg_dest, cpu_env, vect_off);
1070 break;
1071 case MO_32:
1072 tcg_gen_ld32u_i64(tcg_dest, cpu_env, vect_off);
1073 break;
1074 case MO_8|MO_SIGN:
1075 tcg_gen_ld8s_i64(tcg_dest, cpu_env, vect_off);
1076 break;
1077 case MO_16|MO_SIGN:
1078 tcg_gen_ld16s_i64(tcg_dest, cpu_env, vect_off);
1079 break;
1080 case MO_32|MO_SIGN:
1081 tcg_gen_ld32s_i64(tcg_dest, cpu_env, vect_off);
1082 break;
1083 case MO_64:
1084 case MO_64|MO_SIGN:
1085 tcg_gen_ld_i64(tcg_dest, cpu_env, vect_off);
1086 break;
1087 default:
1088 g_assert_not_reached();
1092 static void read_vec_element_i32(DisasContext *s, TCGv_i32 tcg_dest, int srcidx,
1093 int element, MemOp memop)
1095 int vect_off = vec_reg_offset(s, srcidx, element, memop & MO_SIZE);
1096 switch (memop) {
1097 case MO_8:
1098 tcg_gen_ld8u_i32(tcg_dest, cpu_env, vect_off);
1099 break;
1100 case MO_16:
1101 tcg_gen_ld16u_i32(tcg_dest, cpu_env, vect_off);
1102 break;
1103 case MO_8|MO_SIGN:
1104 tcg_gen_ld8s_i32(tcg_dest, cpu_env, vect_off);
1105 break;
1106 case MO_16|MO_SIGN:
1107 tcg_gen_ld16s_i32(tcg_dest, cpu_env, vect_off);
1108 break;
1109 case MO_32:
1110 case MO_32|MO_SIGN:
1111 tcg_gen_ld_i32(tcg_dest, cpu_env, vect_off);
1112 break;
1113 default:
1114 g_assert_not_reached();
1118 /* Set value of an element within a vector register */
1119 static void write_vec_element(DisasContext *s, TCGv_i64 tcg_src, int destidx,
1120 int element, MemOp memop)
1122 int vect_off = vec_reg_offset(s, destidx, element, memop & MO_SIZE);
1123 switch (memop) {
1124 case MO_8:
1125 tcg_gen_st8_i64(tcg_src, cpu_env, vect_off);
1126 break;
1127 case MO_16:
1128 tcg_gen_st16_i64(tcg_src, cpu_env, vect_off);
1129 break;
1130 case MO_32:
1131 tcg_gen_st32_i64(tcg_src, cpu_env, vect_off);
1132 break;
1133 case MO_64:
1134 tcg_gen_st_i64(tcg_src, cpu_env, vect_off);
1135 break;
1136 default:
1137 g_assert_not_reached();
1141 static void write_vec_element_i32(DisasContext *s, TCGv_i32 tcg_src,
1142 int destidx, int element, MemOp memop)
1144 int vect_off = vec_reg_offset(s, destidx, element, memop & MO_SIZE);
1145 switch (memop) {
1146 case MO_8:
1147 tcg_gen_st8_i32(tcg_src, cpu_env, vect_off);
1148 break;
1149 case MO_16:
1150 tcg_gen_st16_i32(tcg_src, cpu_env, vect_off);
1151 break;
1152 case MO_32:
1153 tcg_gen_st_i32(tcg_src, cpu_env, vect_off);
1154 break;
1155 default:
1156 g_assert_not_reached();
1160 /* Store from vector register to memory */
1161 static void do_vec_st(DisasContext *s, int srcidx, int element,
1162 TCGv_i64 tcg_addr, MemOp mop)
1164 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
1166 read_vec_element(s, tcg_tmp, srcidx, element, mop & MO_SIZE);
1167 tcg_gen_qemu_st_i64(tcg_tmp, tcg_addr, get_mem_index(s), mop);
1169 tcg_temp_free_i64(tcg_tmp);
1172 /* Load from memory to vector register */
1173 static void do_vec_ld(DisasContext *s, int destidx, int element,
1174 TCGv_i64 tcg_addr, MemOp mop)
1176 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
1178 tcg_gen_qemu_ld_i64(tcg_tmp, tcg_addr, get_mem_index(s), mop);
1179 write_vec_element(s, tcg_tmp, destidx, element, mop & MO_SIZE);
1181 tcg_temp_free_i64(tcg_tmp);
1184 /* Check that FP/Neon access is enabled. If it is, return
1185 * true. If not, emit code to generate an appropriate exception,
1186 * and return false; the caller should not emit any code for
1187 * the instruction. Note that this check must happen after all
1188 * unallocated-encoding checks (otherwise the syndrome information
1189 * for the resulting exception will be incorrect).
1191 static bool fp_access_check(DisasContext *s)
1193 if (s->fp_excp_el) {
1194 assert(!s->fp_access_checked);
1195 s->fp_access_checked = true;
1197 gen_exception_insn(s, s->pc_curr, EXCP_UDEF,
1198 syn_fp_access_trap(1, 0xe, false), s->fp_excp_el);
1199 return false;
1201 s->fp_access_checked = true;
1202 return true;
1205 /* Check that SVE access is enabled. If it is, return true.
1206 * If not, emit code to generate an appropriate exception and return false.
1208 bool sve_access_check(DisasContext *s)
1210 if (s->sve_excp_el) {
1211 assert(!s->sve_access_checked);
1212 s->sve_access_checked = true;
1214 gen_exception_insn(s, s->pc_curr, EXCP_UDEF,
1215 syn_sve_access_trap(), s->sve_excp_el);
1216 return false;
1218 s->sve_access_checked = true;
1219 return fp_access_check(s);
1223 * This utility function is for doing register extension with an
1224 * optional shift. You will likely want to pass a temporary for the
1225 * destination register. See DecodeRegExtend() in the ARM ARM.
1227 static void ext_and_shift_reg(TCGv_i64 tcg_out, TCGv_i64 tcg_in,
1228 int option, unsigned int shift)
1230 int extsize = extract32(option, 0, 2);
1231 bool is_signed = extract32(option, 2, 1);
1233 if (is_signed) {
1234 switch (extsize) {
1235 case 0:
1236 tcg_gen_ext8s_i64(tcg_out, tcg_in);
1237 break;
1238 case 1:
1239 tcg_gen_ext16s_i64(tcg_out, tcg_in);
1240 break;
1241 case 2:
1242 tcg_gen_ext32s_i64(tcg_out, tcg_in);
1243 break;
1244 case 3:
1245 tcg_gen_mov_i64(tcg_out, tcg_in);
1246 break;
1248 } else {
1249 switch (extsize) {
1250 case 0:
1251 tcg_gen_ext8u_i64(tcg_out, tcg_in);
1252 break;
1253 case 1:
1254 tcg_gen_ext16u_i64(tcg_out, tcg_in);
1255 break;
1256 case 2:
1257 tcg_gen_ext32u_i64(tcg_out, tcg_in);
1258 break;
1259 case 3:
1260 tcg_gen_mov_i64(tcg_out, tcg_in);
1261 break;
1265 if (shift) {
1266 tcg_gen_shli_i64(tcg_out, tcg_out, shift);
1270 static inline void gen_check_sp_alignment(DisasContext *s)
1272 /* The AArch64 architecture mandates that (if enabled via PSTATE
1273 * or SCTLR bits) there is a check that SP is 16-aligned on every
1274 * SP-relative load or store (with an exception generated if it is not).
1275 * In line with general QEMU practice regarding misaligned accesses,
1276 * we omit these checks for the sake of guest program performance.
1277 * This function is provided as a hook so we can more easily add these
1278 * checks in future (possibly as a "favour catching guest program bugs
1279 * over speed" user selectable option).
1284 * This provides a simple table based table lookup decoder. It is
1285 * intended to be used when the relevant bits for decode are too
1286 * awkwardly placed and switch/if based logic would be confusing and
1287 * deeply nested. Since it's a linear search through the table, tables
1288 * should be kept small.
1290 * It returns the first handler where insn & mask == pattern, or
1291 * NULL if there is no match.
1292 * The table is terminated by an empty mask (i.e. 0)
1294 static inline AArch64DecodeFn *lookup_disas_fn(const AArch64DecodeTable *table,
1295 uint32_t insn)
1297 const AArch64DecodeTable *tptr = table;
1299 while (tptr->mask) {
1300 if ((insn & tptr->mask) == tptr->pattern) {
1301 return tptr->disas_fn;
1303 tptr++;
1305 return NULL;
1309 * The instruction disassembly implemented here matches
1310 * the instruction encoding classifications in chapter C4
1311 * of the ARM Architecture Reference Manual (DDI0487B_a);
1312 * classification names and decode diagrams here should generally
1313 * match up with those in the manual.
1316 /* Unconditional branch (immediate)
1317 * 31 30 26 25 0
1318 * +----+-----------+-------------------------------------+
1319 * | op | 0 0 1 0 1 | imm26 |
1320 * +----+-----------+-------------------------------------+
1322 static void disas_uncond_b_imm(DisasContext *s, uint32_t insn)
1324 uint64_t addr = s->pc_curr + sextract32(insn, 0, 26) * 4;
1326 if (insn & (1U << 31)) {
1327 /* BL Branch with link */
1328 tcg_gen_movi_i64(cpu_reg(s, 30), s->base.pc_next);
1331 /* B Branch / BL Branch with link */
1332 reset_btype(s);
1333 gen_goto_tb(s, 0, addr);
1336 /* Compare and branch (immediate)
1337 * 31 30 25 24 23 5 4 0
1338 * +----+-------------+----+---------------------+--------+
1339 * | sf | 0 1 1 0 1 0 | op | imm19 | Rt |
1340 * +----+-------------+----+---------------------+--------+
1342 static void disas_comp_b_imm(DisasContext *s, uint32_t insn)
1344 unsigned int sf, op, rt;
1345 uint64_t addr;
1346 TCGLabel *label_match;
1347 TCGv_i64 tcg_cmp;
1349 sf = extract32(insn, 31, 1);
1350 op = extract32(insn, 24, 1); /* 0: CBZ; 1: CBNZ */
1351 rt = extract32(insn, 0, 5);
1352 addr = s->pc_curr + sextract32(insn, 5, 19) * 4;
1354 tcg_cmp = read_cpu_reg(s, rt, sf);
1355 label_match = gen_new_label();
1357 reset_btype(s);
1358 tcg_gen_brcondi_i64(op ? TCG_COND_NE : TCG_COND_EQ,
1359 tcg_cmp, 0, label_match);
1361 gen_goto_tb(s, 0, s->base.pc_next);
1362 gen_set_label(label_match);
1363 gen_goto_tb(s, 1, addr);
1366 /* Test and branch (immediate)
1367 * 31 30 25 24 23 19 18 5 4 0
1368 * +----+-------------+----+-------+-------------+------+
1369 * | b5 | 0 1 1 0 1 1 | op | b40 | imm14 | Rt |
1370 * +----+-------------+----+-------+-------------+------+
1372 static void disas_test_b_imm(DisasContext *s, uint32_t insn)
1374 unsigned int bit_pos, op, rt;
1375 uint64_t addr;
1376 TCGLabel *label_match;
1377 TCGv_i64 tcg_cmp;
1379 bit_pos = (extract32(insn, 31, 1) << 5) | extract32(insn, 19, 5);
1380 op = extract32(insn, 24, 1); /* 0: TBZ; 1: TBNZ */
1381 addr = s->pc_curr + sextract32(insn, 5, 14) * 4;
1382 rt = extract32(insn, 0, 5);
1384 tcg_cmp = tcg_temp_new_i64();
1385 tcg_gen_andi_i64(tcg_cmp, cpu_reg(s, rt), (1ULL << bit_pos));
1386 label_match = gen_new_label();
1388 reset_btype(s);
1389 tcg_gen_brcondi_i64(op ? TCG_COND_NE : TCG_COND_EQ,
1390 tcg_cmp, 0, label_match);
1391 tcg_temp_free_i64(tcg_cmp);
1392 gen_goto_tb(s, 0, s->base.pc_next);
1393 gen_set_label(label_match);
1394 gen_goto_tb(s, 1, addr);
1397 /* Conditional branch (immediate)
1398 * 31 25 24 23 5 4 3 0
1399 * +---------------+----+---------------------+----+------+
1400 * | 0 1 0 1 0 1 0 | o1 | imm19 | o0 | cond |
1401 * +---------------+----+---------------------+----+------+
1403 static void disas_cond_b_imm(DisasContext *s, uint32_t insn)
1405 unsigned int cond;
1406 uint64_t addr;
1408 if ((insn & (1 << 4)) || (insn & (1 << 24))) {
1409 unallocated_encoding(s);
1410 return;
1412 addr = s->pc_curr + sextract32(insn, 5, 19) * 4;
1413 cond = extract32(insn, 0, 4);
1415 reset_btype(s);
1416 if (cond < 0x0e) {
1417 /* genuinely conditional branches */
1418 TCGLabel *label_match = gen_new_label();
1419 arm_gen_test_cc(cond, label_match);
1420 gen_goto_tb(s, 0, s->base.pc_next);
1421 gen_set_label(label_match);
1422 gen_goto_tb(s, 1, addr);
1423 } else {
1424 /* 0xe and 0xf are both "always" conditions */
1425 gen_goto_tb(s, 0, addr);
1429 /* HINT instruction group, including various allocated HINTs */
1430 static void handle_hint(DisasContext *s, uint32_t insn,
1431 unsigned int op1, unsigned int op2, unsigned int crm)
1433 unsigned int selector = crm << 3 | op2;
1435 if (op1 != 3) {
1436 unallocated_encoding(s);
1437 return;
1440 switch (selector) {
1441 case 0b00000: /* NOP */
1442 break;
1443 case 0b00011: /* WFI */
1444 s->base.is_jmp = DISAS_WFI;
1445 break;
1446 case 0b00001: /* YIELD */
1447 /* When running in MTTCG we don't generate jumps to the yield and
1448 * WFE helpers as it won't affect the scheduling of other vCPUs.
1449 * If we wanted to more completely model WFE/SEV so we don't busy
1450 * spin unnecessarily we would need to do something more involved.
1452 if (!(tb_cflags(s->base.tb) & CF_PARALLEL)) {
1453 s->base.is_jmp = DISAS_YIELD;
1455 break;
1456 case 0b00010: /* WFE */
1457 if (!(tb_cflags(s->base.tb) & CF_PARALLEL)) {
1458 s->base.is_jmp = DISAS_WFE;
1460 break;
1461 case 0b00100: /* SEV */
1462 case 0b00101: /* SEVL */
1463 /* we treat all as NOP at least for now */
1464 break;
1465 case 0b00111: /* XPACLRI */
1466 if (s->pauth_active) {
1467 gen_helper_xpaci(cpu_X[30], cpu_env, cpu_X[30]);
1469 break;
1470 case 0b01000: /* PACIA1716 */
1471 if (s->pauth_active) {
1472 gen_helper_pacia(cpu_X[17], cpu_env, cpu_X[17], cpu_X[16]);
1474 break;
1475 case 0b01010: /* PACIB1716 */
1476 if (s->pauth_active) {
1477 gen_helper_pacib(cpu_X[17], cpu_env, cpu_X[17], cpu_X[16]);
1479 break;
1480 case 0b01100: /* AUTIA1716 */
1481 if (s->pauth_active) {
1482 gen_helper_autia(cpu_X[17], cpu_env, cpu_X[17], cpu_X[16]);
1484 break;
1485 case 0b01110: /* AUTIB1716 */
1486 if (s->pauth_active) {
1487 gen_helper_autib(cpu_X[17], cpu_env, cpu_X[17], cpu_X[16]);
1489 break;
1490 case 0b11000: /* PACIAZ */
1491 if (s->pauth_active) {
1492 gen_helper_pacia(cpu_X[30], cpu_env, cpu_X[30],
1493 new_tmp_a64_zero(s));
1495 break;
1496 case 0b11001: /* PACIASP */
1497 if (s->pauth_active) {
1498 gen_helper_pacia(cpu_X[30], cpu_env, cpu_X[30], cpu_X[31]);
1500 break;
1501 case 0b11010: /* PACIBZ */
1502 if (s->pauth_active) {
1503 gen_helper_pacib(cpu_X[30], cpu_env, cpu_X[30],
1504 new_tmp_a64_zero(s));
1506 break;
1507 case 0b11011: /* PACIBSP */
1508 if (s->pauth_active) {
1509 gen_helper_pacib(cpu_X[30], cpu_env, cpu_X[30], cpu_X[31]);
1511 break;
1512 case 0b11100: /* AUTIAZ */
1513 if (s->pauth_active) {
1514 gen_helper_autia(cpu_X[30], cpu_env, cpu_X[30],
1515 new_tmp_a64_zero(s));
1517 break;
1518 case 0b11101: /* AUTIASP */
1519 if (s->pauth_active) {
1520 gen_helper_autia(cpu_X[30], cpu_env, cpu_X[30], cpu_X[31]);
1522 break;
1523 case 0b11110: /* AUTIBZ */
1524 if (s->pauth_active) {
1525 gen_helper_autib(cpu_X[30], cpu_env, cpu_X[30],
1526 new_tmp_a64_zero(s));
1528 break;
1529 case 0b11111: /* AUTIBSP */
1530 if (s->pauth_active) {
1531 gen_helper_autib(cpu_X[30], cpu_env, cpu_X[30], cpu_X[31]);
1533 break;
1534 default:
1535 /* default specified as NOP equivalent */
1536 break;
1540 static void gen_clrex(DisasContext *s, uint32_t insn)
1542 tcg_gen_movi_i64(cpu_exclusive_addr, -1);
1545 /* CLREX, DSB, DMB, ISB */
1546 static void handle_sync(DisasContext *s, uint32_t insn,
1547 unsigned int op1, unsigned int op2, unsigned int crm)
1549 TCGBar bar;
1551 if (op1 != 3) {
1552 unallocated_encoding(s);
1553 return;
1556 switch (op2) {
1557 case 2: /* CLREX */
1558 gen_clrex(s, insn);
1559 return;
1560 case 4: /* DSB */
1561 case 5: /* DMB */
1562 switch (crm & 3) {
1563 case 1: /* MBReqTypes_Reads */
1564 bar = TCG_BAR_SC | TCG_MO_LD_LD | TCG_MO_LD_ST;
1565 break;
1566 case 2: /* MBReqTypes_Writes */
1567 bar = TCG_BAR_SC | TCG_MO_ST_ST;
1568 break;
1569 default: /* MBReqTypes_All */
1570 bar = TCG_BAR_SC | TCG_MO_ALL;
1571 break;
1573 tcg_gen_mb(bar);
1574 return;
1575 case 6: /* ISB */
1576 /* We need to break the TB after this insn to execute
1577 * a self-modified code correctly and also to take
1578 * any pending interrupts immediately.
1580 reset_btype(s);
1581 gen_goto_tb(s, 0, s->base.pc_next);
1582 return;
1584 case 7: /* SB */
1585 if (crm != 0 || !dc_isar_feature(aa64_sb, s)) {
1586 goto do_unallocated;
1589 * TODO: There is no speculation barrier opcode for TCG;
1590 * MB and end the TB instead.
1592 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_SC);
1593 gen_goto_tb(s, 0, s->base.pc_next);
1594 return;
1596 default:
1597 do_unallocated:
1598 unallocated_encoding(s);
1599 return;
1603 static void gen_xaflag(void)
1605 TCGv_i32 z = tcg_temp_new_i32();
1607 tcg_gen_setcondi_i32(TCG_COND_EQ, z, cpu_ZF, 0);
1610 * (!C & !Z) << 31
1611 * (!(C | Z)) << 31
1612 * ~((C | Z) << 31)
1613 * ~-(C | Z)
1614 * (C | Z) - 1
1616 tcg_gen_or_i32(cpu_NF, cpu_CF, z);
1617 tcg_gen_subi_i32(cpu_NF, cpu_NF, 1);
1619 /* !(Z & C) */
1620 tcg_gen_and_i32(cpu_ZF, z, cpu_CF);
1621 tcg_gen_xori_i32(cpu_ZF, cpu_ZF, 1);
1623 /* (!C & Z) << 31 -> -(Z & ~C) */
1624 tcg_gen_andc_i32(cpu_VF, z, cpu_CF);
1625 tcg_gen_neg_i32(cpu_VF, cpu_VF);
1627 /* C | Z */
1628 tcg_gen_or_i32(cpu_CF, cpu_CF, z);
1630 tcg_temp_free_i32(z);
1633 static void gen_axflag(void)
1635 tcg_gen_sari_i32(cpu_VF, cpu_VF, 31); /* V ? -1 : 0 */
1636 tcg_gen_andc_i32(cpu_CF, cpu_CF, cpu_VF); /* C & !V */
1638 /* !(Z | V) -> !(!ZF | V) -> ZF & !V -> ZF & ~VF */
1639 tcg_gen_andc_i32(cpu_ZF, cpu_ZF, cpu_VF);
1641 tcg_gen_movi_i32(cpu_NF, 0);
1642 tcg_gen_movi_i32(cpu_VF, 0);
1645 /* MSR (immediate) - move immediate to processor state field */
1646 static void handle_msr_i(DisasContext *s, uint32_t insn,
1647 unsigned int op1, unsigned int op2, unsigned int crm)
1649 TCGv_i32 t1;
1650 int op = op1 << 3 | op2;
1652 /* End the TB by default, chaining is ok. */
1653 s->base.is_jmp = DISAS_TOO_MANY;
1655 switch (op) {
1656 case 0x00: /* CFINV */
1657 if (crm != 0 || !dc_isar_feature(aa64_condm_4, s)) {
1658 goto do_unallocated;
1660 tcg_gen_xori_i32(cpu_CF, cpu_CF, 1);
1661 s->base.is_jmp = DISAS_NEXT;
1662 break;
1664 case 0x01: /* XAFlag */
1665 if (crm != 0 || !dc_isar_feature(aa64_condm_5, s)) {
1666 goto do_unallocated;
1668 gen_xaflag();
1669 s->base.is_jmp = DISAS_NEXT;
1670 break;
1672 case 0x02: /* AXFlag */
1673 if (crm != 0 || !dc_isar_feature(aa64_condm_5, s)) {
1674 goto do_unallocated;
1676 gen_axflag();
1677 s->base.is_jmp = DISAS_NEXT;
1678 break;
1680 case 0x03: /* UAO */
1681 if (!dc_isar_feature(aa64_uao, s) || s->current_el == 0) {
1682 goto do_unallocated;
1684 if (crm & 1) {
1685 set_pstate_bits(PSTATE_UAO);
1686 } else {
1687 clear_pstate_bits(PSTATE_UAO);
1689 t1 = tcg_const_i32(s->current_el);
1690 gen_helper_rebuild_hflags_a64(cpu_env, t1);
1691 tcg_temp_free_i32(t1);
1692 break;
1694 case 0x04: /* PAN */
1695 if (!dc_isar_feature(aa64_pan, s) || s->current_el == 0) {
1696 goto do_unallocated;
1698 if (crm & 1) {
1699 set_pstate_bits(PSTATE_PAN);
1700 } else {
1701 clear_pstate_bits(PSTATE_PAN);
1703 t1 = tcg_const_i32(s->current_el);
1704 gen_helper_rebuild_hflags_a64(cpu_env, t1);
1705 tcg_temp_free_i32(t1);
1706 break;
1708 case 0x05: /* SPSel */
1709 if (s->current_el == 0) {
1710 goto do_unallocated;
1712 t1 = tcg_const_i32(crm & PSTATE_SP);
1713 gen_helper_msr_i_spsel(cpu_env, t1);
1714 tcg_temp_free_i32(t1);
1715 break;
1717 case 0x19: /* SSBS */
1718 if (!dc_isar_feature(aa64_ssbs, s)) {
1719 goto do_unallocated;
1721 if (crm & 1) {
1722 set_pstate_bits(PSTATE_SSBS);
1723 } else {
1724 clear_pstate_bits(PSTATE_SSBS);
1726 /* Don't need to rebuild hflags since SSBS is a nop */
1727 break;
1729 case 0x1a: /* DIT */
1730 if (!dc_isar_feature(aa64_dit, s)) {
1731 goto do_unallocated;
1733 if (crm & 1) {
1734 set_pstate_bits(PSTATE_DIT);
1735 } else {
1736 clear_pstate_bits(PSTATE_DIT);
1738 /* There's no need to rebuild hflags because DIT is a nop */
1739 break;
1741 case 0x1e: /* DAIFSet */
1742 t1 = tcg_const_i32(crm);
1743 gen_helper_msr_i_daifset(cpu_env, t1);
1744 tcg_temp_free_i32(t1);
1745 break;
1747 case 0x1f: /* DAIFClear */
1748 t1 = tcg_const_i32(crm);
1749 gen_helper_msr_i_daifclear(cpu_env, t1);
1750 tcg_temp_free_i32(t1);
1751 /* For DAIFClear, exit the cpu loop to re-evaluate pending IRQs. */
1752 s->base.is_jmp = DISAS_UPDATE_EXIT;
1753 break;
1755 case 0x1c: /* TCO */
1756 if (dc_isar_feature(aa64_mte, s)) {
1757 /* Full MTE is enabled -- set the TCO bit as directed. */
1758 if (crm & 1) {
1759 set_pstate_bits(PSTATE_TCO);
1760 } else {
1761 clear_pstate_bits(PSTATE_TCO);
1763 t1 = tcg_const_i32(s->current_el);
1764 gen_helper_rebuild_hflags_a64(cpu_env, t1);
1765 tcg_temp_free_i32(t1);
1766 /* Many factors, including TCO, go into MTE_ACTIVE. */
1767 s->base.is_jmp = DISAS_UPDATE_NOCHAIN;
1768 } else if (dc_isar_feature(aa64_mte_insn_reg, s)) {
1769 /* Only "instructions accessible at EL0" -- PSTATE.TCO is WI. */
1770 s->base.is_jmp = DISAS_NEXT;
1771 } else {
1772 goto do_unallocated;
1774 break;
1776 default:
1777 do_unallocated:
1778 unallocated_encoding(s);
1779 return;
1783 static void gen_get_nzcv(TCGv_i64 tcg_rt)
1785 TCGv_i32 tmp = tcg_temp_new_i32();
1786 TCGv_i32 nzcv = tcg_temp_new_i32();
1788 /* build bit 31, N */
1789 tcg_gen_andi_i32(nzcv, cpu_NF, (1U << 31));
1790 /* build bit 30, Z */
1791 tcg_gen_setcondi_i32(TCG_COND_EQ, tmp, cpu_ZF, 0);
1792 tcg_gen_deposit_i32(nzcv, nzcv, tmp, 30, 1);
1793 /* build bit 29, C */
1794 tcg_gen_deposit_i32(nzcv, nzcv, cpu_CF, 29, 1);
1795 /* build bit 28, V */
1796 tcg_gen_shri_i32(tmp, cpu_VF, 31);
1797 tcg_gen_deposit_i32(nzcv, nzcv, tmp, 28, 1);
1798 /* generate result */
1799 tcg_gen_extu_i32_i64(tcg_rt, nzcv);
1801 tcg_temp_free_i32(nzcv);
1802 tcg_temp_free_i32(tmp);
1805 static void gen_set_nzcv(TCGv_i64 tcg_rt)
1807 TCGv_i32 nzcv = tcg_temp_new_i32();
1809 /* take NZCV from R[t] */
1810 tcg_gen_extrl_i64_i32(nzcv, tcg_rt);
1812 /* bit 31, N */
1813 tcg_gen_andi_i32(cpu_NF, nzcv, (1U << 31));
1814 /* bit 30, Z */
1815 tcg_gen_andi_i32(cpu_ZF, nzcv, (1 << 30));
1816 tcg_gen_setcondi_i32(TCG_COND_EQ, cpu_ZF, cpu_ZF, 0);
1817 /* bit 29, C */
1818 tcg_gen_andi_i32(cpu_CF, nzcv, (1 << 29));
1819 tcg_gen_shri_i32(cpu_CF, cpu_CF, 29);
1820 /* bit 28, V */
1821 tcg_gen_andi_i32(cpu_VF, nzcv, (1 << 28));
1822 tcg_gen_shli_i32(cpu_VF, cpu_VF, 3);
1823 tcg_temp_free_i32(nzcv);
1826 /* MRS - move from system register
1827 * MSR (register) - move to system register
1828 * SYS
1829 * SYSL
1830 * These are all essentially the same insn in 'read' and 'write'
1831 * versions, with varying op0 fields.
1833 static void handle_sys(DisasContext *s, uint32_t insn, bool isread,
1834 unsigned int op0, unsigned int op1, unsigned int op2,
1835 unsigned int crn, unsigned int crm, unsigned int rt)
1837 const ARMCPRegInfo *ri;
1838 TCGv_i64 tcg_rt;
1840 ri = get_arm_cp_reginfo(s->cp_regs,
1841 ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP,
1842 crn, crm, op0, op1, op2));
1844 if (!ri) {
1845 /* Unknown register; this might be a guest error or a QEMU
1846 * unimplemented feature.
1848 qemu_log_mask(LOG_UNIMP, "%s access to unsupported AArch64 "
1849 "system register op0:%d op1:%d crn:%d crm:%d op2:%d\n",
1850 isread ? "read" : "write", op0, op1, crn, crm, op2);
1851 unallocated_encoding(s);
1852 return;
1855 /* Check access permissions */
1856 if (!cp_access_ok(s->current_el, ri, isread)) {
1857 unallocated_encoding(s);
1858 return;
1861 if (ri->accessfn) {
1862 /* Emit code to perform further access permissions checks at
1863 * runtime; this may result in an exception.
1865 TCGv_ptr tmpptr;
1866 TCGv_i32 tcg_syn, tcg_isread;
1867 uint32_t syndrome;
1869 gen_a64_set_pc_im(s->pc_curr);
1870 tmpptr = tcg_const_ptr(ri);
1871 syndrome = syn_aa64_sysregtrap(op0, op1, op2, crn, crm, rt, isread);
1872 tcg_syn = tcg_const_i32(syndrome);
1873 tcg_isread = tcg_const_i32(isread);
1874 gen_helper_access_check_cp_reg(cpu_env, tmpptr, tcg_syn, tcg_isread);
1875 tcg_temp_free_ptr(tmpptr);
1876 tcg_temp_free_i32(tcg_syn);
1877 tcg_temp_free_i32(tcg_isread);
1878 } else if (ri->type & ARM_CP_RAISES_EXC) {
1880 * The readfn or writefn might raise an exception;
1881 * synchronize the CPU state in case it does.
1883 gen_a64_set_pc_im(s->pc_curr);
1886 /* Handle special cases first */
1887 switch (ri->type & ~(ARM_CP_FLAG_MASK & ~ARM_CP_SPECIAL)) {
1888 case ARM_CP_NOP:
1889 return;
1890 case ARM_CP_NZCV:
1891 tcg_rt = cpu_reg(s, rt);
1892 if (isread) {
1893 gen_get_nzcv(tcg_rt);
1894 } else {
1895 gen_set_nzcv(tcg_rt);
1897 return;
1898 case ARM_CP_CURRENTEL:
1899 /* Reads as current EL value from pstate, which is
1900 * guaranteed to be constant by the tb flags.
1902 tcg_rt = cpu_reg(s, rt);
1903 tcg_gen_movi_i64(tcg_rt, s->current_el << 2);
1904 return;
1905 case ARM_CP_DC_ZVA:
1906 /* Writes clear the aligned block of memory which rt points into. */
1907 if (s->mte_active[0]) {
1908 TCGv_i32 t_desc;
1909 int desc = 0;
1911 desc = FIELD_DP32(desc, MTEDESC, MIDX, get_mem_index(s));
1912 desc = FIELD_DP32(desc, MTEDESC, TBI, s->tbid);
1913 desc = FIELD_DP32(desc, MTEDESC, TCMA, s->tcma);
1914 t_desc = tcg_const_i32(desc);
1916 tcg_rt = new_tmp_a64(s);
1917 gen_helper_mte_check_zva(tcg_rt, cpu_env, t_desc, cpu_reg(s, rt));
1918 tcg_temp_free_i32(t_desc);
1919 } else {
1920 tcg_rt = clean_data_tbi(s, cpu_reg(s, rt));
1922 gen_helper_dc_zva(cpu_env, tcg_rt);
1923 return;
1924 case ARM_CP_DC_GVA:
1926 TCGv_i64 clean_addr, tag;
1929 * DC_GVA, like DC_ZVA, requires that we supply the original
1930 * pointer for an invalid page. Probe that address first.
1932 tcg_rt = cpu_reg(s, rt);
1933 clean_addr = clean_data_tbi(s, tcg_rt);
1934 gen_probe_access(s, clean_addr, MMU_DATA_STORE, MO_8);
1936 if (s->ata) {
1937 /* Extract the tag from the register to match STZGM. */
1938 tag = tcg_temp_new_i64();
1939 tcg_gen_shri_i64(tag, tcg_rt, 56);
1940 gen_helper_stzgm_tags(cpu_env, clean_addr, tag);
1941 tcg_temp_free_i64(tag);
1944 return;
1945 case ARM_CP_DC_GZVA:
1947 TCGv_i64 clean_addr, tag;
1949 /* For DC_GZVA, we can rely on DC_ZVA for the proper fault. */
1950 tcg_rt = cpu_reg(s, rt);
1951 clean_addr = clean_data_tbi(s, tcg_rt);
1952 gen_helper_dc_zva(cpu_env, clean_addr);
1954 if (s->ata) {
1955 /* Extract the tag from the register to match STZGM. */
1956 tag = tcg_temp_new_i64();
1957 tcg_gen_shri_i64(tag, tcg_rt, 56);
1958 gen_helper_stzgm_tags(cpu_env, clean_addr, tag);
1959 tcg_temp_free_i64(tag);
1962 return;
1963 default:
1964 break;
1966 if ((ri->type & ARM_CP_FPU) && !fp_access_check(s)) {
1967 return;
1968 } else if ((ri->type & ARM_CP_SVE) && !sve_access_check(s)) {
1969 return;
1972 if ((tb_cflags(s->base.tb) & CF_USE_ICOUNT) && (ri->type & ARM_CP_IO)) {
1973 gen_io_start();
1976 tcg_rt = cpu_reg(s, rt);
1978 if (isread) {
1979 if (ri->type & ARM_CP_CONST) {
1980 tcg_gen_movi_i64(tcg_rt, ri->resetvalue);
1981 } else if (ri->readfn) {
1982 TCGv_ptr tmpptr;
1983 tmpptr = tcg_const_ptr(ri);
1984 gen_helper_get_cp_reg64(tcg_rt, cpu_env, tmpptr);
1985 tcg_temp_free_ptr(tmpptr);
1986 } else {
1987 tcg_gen_ld_i64(tcg_rt, cpu_env, ri->fieldoffset);
1989 } else {
1990 if (ri->type & ARM_CP_CONST) {
1991 /* If not forbidden by access permissions, treat as WI */
1992 return;
1993 } else if (ri->writefn) {
1994 TCGv_ptr tmpptr;
1995 tmpptr = tcg_const_ptr(ri);
1996 gen_helper_set_cp_reg64(cpu_env, tmpptr, tcg_rt);
1997 tcg_temp_free_ptr(tmpptr);
1998 } else {
1999 tcg_gen_st_i64(tcg_rt, cpu_env, ri->fieldoffset);
2003 if ((tb_cflags(s->base.tb) & CF_USE_ICOUNT) && (ri->type & ARM_CP_IO)) {
2004 /* I/O operations must end the TB here (whether read or write) */
2005 s->base.is_jmp = DISAS_UPDATE_EXIT;
2007 if (!isread && !(ri->type & ARM_CP_SUPPRESS_TB_END)) {
2009 * A write to any coprocessor regiser that ends a TB
2010 * must rebuild the hflags for the next TB.
2012 TCGv_i32 tcg_el = tcg_const_i32(s->current_el);
2013 gen_helper_rebuild_hflags_a64(cpu_env, tcg_el);
2014 tcg_temp_free_i32(tcg_el);
2016 * We default to ending the TB on a coprocessor register write,
2017 * but allow this to be suppressed by the register definition
2018 * (usually only necessary to work around guest bugs).
2020 s->base.is_jmp = DISAS_UPDATE_EXIT;
2024 /* System
2025 * 31 22 21 20 19 18 16 15 12 11 8 7 5 4 0
2026 * +---------------------+---+-----+-----+-------+-------+-----+------+
2027 * | 1 1 0 1 0 1 0 1 0 0 | L | op0 | op1 | CRn | CRm | op2 | Rt |
2028 * +---------------------+---+-----+-----+-------+-------+-----+------+
2030 static void disas_system(DisasContext *s, uint32_t insn)
2032 unsigned int l, op0, op1, crn, crm, op2, rt;
2033 l = extract32(insn, 21, 1);
2034 op0 = extract32(insn, 19, 2);
2035 op1 = extract32(insn, 16, 3);
2036 crn = extract32(insn, 12, 4);
2037 crm = extract32(insn, 8, 4);
2038 op2 = extract32(insn, 5, 3);
2039 rt = extract32(insn, 0, 5);
2041 if (op0 == 0) {
2042 if (l || rt != 31) {
2043 unallocated_encoding(s);
2044 return;
2046 switch (crn) {
2047 case 2: /* HINT (including allocated hints like NOP, YIELD, etc) */
2048 handle_hint(s, insn, op1, op2, crm);
2049 break;
2050 case 3: /* CLREX, DSB, DMB, ISB */
2051 handle_sync(s, insn, op1, op2, crm);
2052 break;
2053 case 4: /* MSR (immediate) */
2054 handle_msr_i(s, insn, op1, op2, crm);
2055 break;
2056 default:
2057 unallocated_encoding(s);
2058 break;
2060 return;
2062 handle_sys(s, insn, l, op0, op1, op2, crn, crm, rt);
2065 /* Exception generation
2067 * 31 24 23 21 20 5 4 2 1 0
2068 * +-----------------+-----+------------------------+-----+----+
2069 * | 1 1 0 1 0 1 0 0 | opc | imm16 | op2 | LL |
2070 * +-----------------------+------------------------+----------+
2072 static void disas_exc(DisasContext *s, uint32_t insn)
2074 int opc = extract32(insn, 21, 3);
2075 int op2_ll = extract32(insn, 0, 5);
2076 int imm16 = extract32(insn, 5, 16);
2077 TCGv_i32 tmp;
2079 switch (opc) {
2080 case 0:
2081 /* For SVC, HVC and SMC we advance the single-step state
2082 * machine before taking the exception. This is architecturally
2083 * mandated, to ensure that single-stepping a system call
2084 * instruction works properly.
2086 switch (op2_ll) {
2087 case 1: /* SVC */
2088 gen_ss_advance(s);
2089 gen_exception_insn(s, s->base.pc_next, EXCP_SWI,
2090 syn_aa64_svc(imm16), default_exception_el(s));
2091 break;
2092 case 2: /* HVC */
2093 if (s->current_el == 0) {
2094 unallocated_encoding(s);
2095 break;
2097 /* The pre HVC helper handles cases when HVC gets trapped
2098 * as an undefined insn by runtime configuration.
2100 gen_a64_set_pc_im(s->pc_curr);
2101 gen_helper_pre_hvc(cpu_env);
2102 gen_ss_advance(s);
2103 gen_exception_insn(s, s->base.pc_next, EXCP_HVC,
2104 syn_aa64_hvc(imm16), 2);
2105 break;
2106 case 3: /* SMC */
2107 if (s->current_el == 0) {
2108 unallocated_encoding(s);
2109 break;
2111 gen_a64_set_pc_im(s->pc_curr);
2112 tmp = tcg_const_i32(syn_aa64_smc(imm16));
2113 gen_helper_pre_smc(cpu_env, tmp);
2114 tcg_temp_free_i32(tmp);
2115 gen_ss_advance(s);
2116 gen_exception_insn(s, s->base.pc_next, EXCP_SMC,
2117 syn_aa64_smc(imm16), 3);
2118 break;
2119 default:
2120 unallocated_encoding(s);
2121 break;
2123 break;
2124 case 1:
2125 if (op2_ll != 0) {
2126 unallocated_encoding(s);
2127 break;
2129 /* BRK */
2130 gen_exception_bkpt_insn(s, syn_aa64_bkpt(imm16));
2131 break;
2132 case 2:
2133 if (op2_ll != 0) {
2134 unallocated_encoding(s);
2135 break;
2137 /* HLT. This has two purposes.
2138 * Architecturally, it is an external halting debug instruction.
2139 * Since QEMU doesn't implement external debug, we treat this as
2140 * it is required for halting debug disabled: it will UNDEF.
2141 * Secondly, "HLT 0xf000" is the A64 semihosting syscall instruction.
2143 if (semihosting_enabled() && imm16 == 0xf000) {
2144 #ifndef CONFIG_USER_ONLY
2145 /* In system mode, don't allow userspace access to semihosting,
2146 * to provide some semblance of security (and for consistency
2147 * with our 32-bit semihosting).
2149 if (s->current_el == 0) {
2150 unsupported_encoding(s, insn);
2151 break;
2153 #endif
2154 gen_exception_internal_insn(s, s->pc_curr, EXCP_SEMIHOST);
2155 } else {
2156 unsupported_encoding(s, insn);
2158 break;
2159 case 5:
2160 if (op2_ll < 1 || op2_ll > 3) {
2161 unallocated_encoding(s);
2162 break;
2164 /* DCPS1, DCPS2, DCPS3 */
2165 unsupported_encoding(s, insn);
2166 break;
2167 default:
2168 unallocated_encoding(s);
2169 break;
2173 /* Unconditional branch (register)
2174 * 31 25 24 21 20 16 15 10 9 5 4 0
2175 * +---------------+-------+-------+-------+------+-------+
2176 * | 1 1 0 1 0 1 1 | opc | op2 | op3 | Rn | op4 |
2177 * +---------------+-------+-------+-------+------+-------+
2179 static void disas_uncond_b_reg(DisasContext *s, uint32_t insn)
2181 unsigned int opc, op2, op3, rn, op4;
2182 unsigned btype_mod = 2; /* 0: BR, 1: BLR, 2: other */
2183 TCGv_i64 dst;
2184 TCGv_i64 modifier;
2186 opc = extract32(insn, 21, 4);
2187 op2 = extract32(insn, 16, 5);
2188 op3 = extract32(insn, 10, 6);
2189 rn = extract32(insn, 5, 5);
2190 op4 = extract32(insn, 0, 5);
2192 if (op2 != 0x1f) {
2193 goto do_unallocated;
2196 switch (opc) {
2197 case 0: /* BR */
2198 case 1: /* BLR */
2199 case 2: /* RET */
2200 btype_mod = opc;
2201 switch (op3) {
2202 case 0:
2203 /* BR, BLR, RET */
2204 if (op4 != 0) {
2205 goto do_unallocated;
2207 dst = cpu_reg(s, rn);
2208 break;
2210 case 2:
2211 case 3:
2212 if (!dc_isar_feature(aa64_pauth, s)) {
2213 goto do_unallocated;
2215 if (opc == 2) {
2216 /* RETAA, RETAB */
2217 if (rn != 0x1f || op4 != 0x1f) {
2218 goto do_unallocated;
2220 rn = 30;
2221 modifier = cpu_X[31];
2222 } else {
2223 /* BRAAZ, BRABZ, BLRAAZ, BLRABZ */
2224 if (op4 != 0x1f) {
2225 goto do_unallocated;
2227 modifier = new_tmp_a64_zero(s);
2229 if (s->pauth_active) {
2230 dst = new_tmp_a64(s);
2231 if (op3 == 2) {
2232 gen_helper_autia(dst, cpu_env, cpu_reg(s, rn), modifier);
2233 } else {
2234 gen_helper_autib(dst, cpu_env, cpu_reg(s, rn), modifier);
2236 } else {
2237 dst = cpu_reg(s, rn);
2239 break;
2241 default:
2242 goto do_unallocated;
2244 gen_a64_set_pc(s, dst);
2245 /* BLR also needs to load return address */
2246 if (opc == 1) {
2247 tcg_gen_movi_i64(cpu_reg(s, 30), s->base.pc_next);
2249 break;
2251 case 8: /* BRAA */
2252 case 9: /* BLRAA */
2253 if (!dc_isar_feature(aa64_pauth, s)) {
2254 goto do_unallocated;
2256 if ((op3 & ~1) != 2) {
2257 goto do_unallocated;
2259 btype_mod = opc & 1;
2260 if (s->pauth_active) {
2261 dst = new_tmp_a64(s);
2262 modifier = cpu_reg_sp(s, op4);
2263 if (op3 == 2) {
2264 gen_helper_autia(dst, cpu_env, cpu_reg(s, rn), modifier);
2265 } else {
2266 gen_helper_autib(dst, cpu_env, cpu_reg(s, rn), modifier);
2268 } else {
2269 dst = cpu_reg(s, rn);
2271 gen_a64_set_pc(s, dst);
2272 /* BLRAA also needs to load return address */
2273 if (opc == 9) {
2274 tcg_gen_movi_i64(cpu_reg(s, 30), s->base.pc_next);
2276 break;
2278 case 4: /* ERET */
2279 if (s->current_el == 0) {
2280 goto do_unallocated;
2282 switch (op3) {
2283 case 0: /* ERET */
2284 if (op4 != 0) {
2285 goto do_unallocated;
2287 dst = tcg_temp_new_i64();
2288 tcg_gen_ld_i64(dst, cpu_env,
2289 offsetof(CPUARMState, elr_el[s->current_el]));
2290 break;
2292 case 2: /* ERETAA */
2293 case 3: /* ERETAB */
2294 if (!dc_isar_feature(aa64_pauth, s)) {
2295 goto do_unallocated;
2297 if (rn != 0x1f || op4 != 0x1f) {
2298 goto do_unallocated;
2300 dst = tcg_temp_new_i64();
2301 tcg_gen_ld_i64(dst, cpu_env,
2302 offsetof(CPUARMState, elr_el[s->current_el]));
2303 if (s->pauth_active) {
2304 modifier = cpu_X[31];
2305 if (op3 == 2) {
2306 gen_helper_autia(dst, cpu_env, dst, modifier);
2307 } else {
2308 gen_helper_autib(dst, cpu_env, dst, modifier);
2311 break;
2313 default:
2314 goto do_unallocated;
2316 if (tb_cflags(s->base.tb) & CF_USE_ICOUNT) {
2317 gen_io_start();
2320 gen_helper_exception_return(cpu_env, dst);
2321 tcg_temp_free_i64(dst);
2322 /* Must exit loop to check un-masked IRQs */
2323 s->base.is_jmp = DISAS_EXIT;
2324 return;
2326 case 5: /* DRPS */
2327 if (op3 != 0 || op4 != 0 || rn != 0x1f) {
2328 goto do_unallocated;
2329 } else {
2330 unsupported_encoding(s, insn);
2332 return;
2334 default:
2335 do_unallocated:
2336 unallocated_encoding(s);
2337 return;
2340 switch (btype_mod) {
2341 case 0: /* BR */
2342 if (dc_isar_feature(aa64_bti, s)) {
2343 /* BR to {x16,x17} or !guard -> 1, else 3. */
2344 set_btype(s, rn == 16 || rn == 17 || !s->guarded_page ? 1 : 3);
2346 break;
2348 case 1: /* BLR */
2349 if (dc_isar_feature(aa64_bti, s)) {
2350 /* BLR sets BTYPE to 2, regardless of source guarded page. */
2351 set_btype(s, 2);
2353 break;
2355 default: /* RET or none of the above. */
2356 /* BTYPE will be set to 0 by normal end-of-insn processing. */
2357 break;
2360 s->base.is_jmp = DISAS_JUMP;
2363 /* Branches, exception generating and system instructions */
2364 static void disas_b_exc_sys(DisasContext *s, uint32_t insn)
2366 switch (extract32(insn, 25, 7)) {
2367 case 0x0a: case 0x0b:
2368 case 0x4a: case 0x4b: /* Unconditional branch (immediate) */
2369 disas_uncond_b_imm(s, insn);
2370 break;
2371 case 0x1a: case 0x5a: /* Compare & branch (immediate) */
2372 disas_comp_b_imm(s, insn);
2373 break;
2374 case 0x1b: case 0x5b: /* Test & branch (immediate) */
2375 disas_test_b_imm(s, insn);
2376 break;
2377 case 0x2a: /* Conditional branch (immediate) */
2378 disas_cond_b_imm(s, insn);
2379 break;
2380 case 0x6a: /* Exception generation / System */
2381 if (insn & (1 << 24)) {
2382 if (extract32(insn, 22, 2) == 0) {
2383 disas_system(s, insn);
2384 } else {
2385 unallocated_encoding(s);
2387 } else {
2388 disas_exc(s, insn);
2390 break;
2391 case 0x6b: /* Unconditional branch (register) */
2392 disas_uncond_b_reg(s, insn);
2393 break;
2394 default:
2395 unallocated_encoding(s);
2396 break;
2401 * Load/Store exclusive instructions are implemented by remembering
2402 * the value/address loaded, and seeing if these are the same
2403 * when the store is performed. This is not actually the architecturally
2404 * mandated semantics, but it works for typical guest code sequences
2405 * and avoids having to monitor regular stores.
2407 * The store exclusive uses the atomic cmpxchg primitives to avoid
2408 * races in multi-threaded linux-user and when MTTCG softmmu is
2409 * enabled.
2411 static void gen_load_exclusive(DisasContext *s, int rt, int rt2,
2412 TCGv_i64 addr, int size, bool is_pair)
2414 int idx = get_mem_index(s);
2415 MemOp memop = s->be_data;
2417 g_assert(size <= 3);
2418 if (is_pair) {
2419 g_assert(size >= 2);
2420 if (size == 2) {
2421 /* The pair must be single-copy atomic for the doubleword. */
2422 memop |= MO_64 | MO_ALIGN;
2423 tcg_gen_qemu_ld_i64(cpu_exclusive_val, addr, idx, memop);
2424 if (s->be_data == MO_LE) {
2425 tcg_gen_extract_i64(cpu_reg(s, rt), cpu_exclusive_val, 0, 32);
2426 tcg_gen_extract_i64(cpu_reg(s, rt2), cpu_exclusive_val, 32, 32);
2427 } else {
2428 tcg_gen_extract_i64(cpu_reg(s, rt), cpu_exclusive_val, 32, 32);
2429 tcg_gen_extract_i64(cpu_reg(s, rt2), cpu_exclusive_val, 0, 32);
2431 } else {
2432 /* The pair must be single-copy atomic for *each* doubleword, not
2433 the entire quadword, however it must be quadword aligned. */
2434 memop |= MO_64;
2435 tcg_gen_qemu_ld_i64(cpu_exclusive_val, addr, idx,
2436 memop | MO_ALIGN_16);
2438 TCGv_i64 addr2 = tcg_temp_new_i64();
2439 tcg_gen_addi_i64(addr2, addr, 8);
2440 tcg_gen_qemu_ld_i64(cpu_exclusive_high, addr2, idx, memop);
2441 tcg_temp_free_i64(addr2);
2443 tcg_gen_mov_i64(cpu_reg(s, rt), cpu_exclusive_val);
2444 tcg_gen_mov_i64(cpu_reg(s, rt2), cpu_exclusive_high);
2446 } else {
2447 memop |= size | MO_ALIGN;
2448 tcg_gen_qemu_ld_i64(cpu_exclusive_val, addr, idx, memop);
2449 tcg_gen_mov_i64(cpu_reg(s, rt), cpu_exclusive_val);
2451 tcg_gen_mov_i64(cpu_exclusive_addr, addr);
2454 static void gen_store_exclusive(DisasContext *s, int rd, int rt, int rt2,
2455 TCGv_i64 addr, int size, int is_pair)
2457 /* if (env->exclusive_addr == addr && env->exclusive_val == [addr]
2458 * && (!is_pair || env->exclusive_high == [addr + datasize])) {
2459 * [addr] = {Rt};
2460 * if (is_pair) {
2461 * [addr + datasize] = {Rt2};
2463 * {Rd} = 0;
2464 * } else {
2465 * {Rd} = 1;
2467 * env->exclusive_addr = -1;
2469 TCGLabel *fail_label = gen_new_label();
2470 TCGLabel *done_label = gen_new_label();
2471 TCGv_i64 tmp;
2473 tcg_gen_brcond_i64(TCG_COND_NE, addr, cpu_exclusive_addr, fail_label);
2475 tmp = tcg_temp_new_i64();
2476 if (is_pair) {
2477 if (size == 2) {
2478 if (s->be_data == MO_LE) {
2479 tcg_gen_concat32_i64(tmp, cpu_reg(s, rt), cpu_reg(s, rt2));
2480 } else {
2481 tcg_gen_concat32_i64(tmp, cpu_reg(s, rt2), cpu_reg(s, rt));
2483 tcg_gen_atomic_cmpxchg_i64(tmp, cpu_exclusive_addr,
2484 cpu_exclusive_val, tmp,
2485 get_mem_index(s),
2486 MO_64 | MO_ALIGN | s->be_data);
2487 tcg_gen_setcond_i64(TCG_COND_NE, tmp, tmp, cpu_exclusive_val);
2488 } else if (tb_cflags(s->base.tb) & CF_PARALLEL) {
2489 if (!HAVE_CMPXCHG128) {
2490 gen_helper_exit_atomic(cpu_env);
2491 s->base.is_jmp = DISAS_NORETURN;
2492 } else if (s->be_data == MO_LE) {
2493 gen_helper_paired_cmpxchg64_le_parallel(tmp, cpu_env,
2494 cpu_exclusive_addr,
2495 cpu_reg(s, rt),
2496 cpu_reg(s, rt2));
2497 } else {
2498 gen_helper_paired_cmpxchg64_be_parallel(tmp, cpu_env,
2499 cpu_exclusive_addr,
2500 cpu_reg(s, rt),
2501 cpu_reg(s, rt2));
2503 } else if (s->be_data == MO_LE) {
2504 gen_helper_paired_cmpxchg64_le(tmp, cpu_env, cpu_exclusive_addr,
2505 cpu_reg(s, rt), cpu_reg(s, rt2));
2506 } else {
2507 gen_helper_paired_cmpxchg64_be(tmp, cpu_env, cpu_exclusive_addr,
2508 cpu_reg(s, rt), cpu_reg(s, rt2));
2510 } else {
2511 tcg_gen_atomic_cmpxchg_i64(tmp, cpu_exclusive_addr, cpu_exclusive_val,
2512 cpu_reg(s, rt), get_mem_index(s),
2513 size | MO_ALIGN | s->be_data);
2514 tcg_gen_setcond_i64(TCG_COND_NE, tmp, tmp, cpu_exclusive_val);
2516 tcg_gen_mov_i64(cpu_reg(s, rd), tmp);
2517 tcg_temp_free_i64(tmp);
2518 tcg_gen_br(done_label);
2520 gen_set_label(fail_label);
2521 tcg_gen_movi_i64(cpu_reg(s, rd), 1);
2522 gen_set_label(done_label);
2523 tcg_gen_movi_i64(cpu_exclusive_addr, -1);
2526 static void gen_compare_and_swap(DisasContext *s, int rs, int rt,
2527 int rn, int size)
2529 TCGv_i64 tcg_rs = cpu_reg(s, rs);
2530 TCGv_i64 tcg_rt = cpu_reg(s, rt);
2531 int memidx = get_mem_index(s);
2532 TCGv_i64 clean_addr;
2534 if (rn == 31) {
2535 gen_check_sp_alignment(s);
2537 clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn), true, rn != 31, size);
2538 tcg_gen_atomic_cmpxchg_i64(tcg_rs, clean_addr, tcg_rs, tcg_rt, memidx,
2539 size | MO_ALIGN | s->be_data);
2542 static void gen_compare_and_swap_pair(DisasContext *s, int rs, int rt,
2543 int rn, int size)
2545 TCGv_i64 s1 = cpu_reg(s, rs);
2546 TCGv_i64 s2 = cpu_reg(s, rs + 1);
2547 TCGv_i64 t1 = cpu_reg(s, rt);
2548 TCGv_i64 t2 = cpu_reg(s, rt + 1);
2549 TCGv_i64 clean_addr;
2550 int memidx = get_mem_index(s);
2552 if (rn == 31) {
2553 gen_check_sp_alignment(s);
2556 /* This is a single atomic access, despite the "pair". */
2557 clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn), true, rn != 31, size + 1);
2559 if (size == 2) {
2560 TCGv_i64 cmp = tcg_temp_new_i64();
2561 TCGv_i64 val = tcg_temp_new_i64();
2563 if (s->be_data == MO_LE) {
2564 tcg_gen_concat32_i64(val, t1, t2);
2565 tcg_gen_concat32_i64(cmp, s1, s2);
2566 } else {
2567 tcg_gen_concat32_i64(val, t2, t1);
2568 tcg_gen_concat32_i64(cmp, s2, s1);
2571 tcg_gen_atomic_cmpxchg_i64(cmp, clean_addr, cmp, val, memidx,
2572 MO_64 | MO_ALIGN | s->be_data);
2573 tcg_temp_free_i64(val);
2575 if (s->be_data == MO_LE) {
2576 tcg_gen_extr32_i64(s1, s2, cmp);
2577 } else {
2578 tcg_gen_extr32_i64(s2, s1, cmp);
2580 tcg_temp_free_i64(cmp);
2581 } else if (tb_cflags(s->base.tb) & CF_PARALLEL) {
2582 if (HAVE_CMPXCHG128) {
2583 TCGv_i32 tcg_rs = tcg_const_i32(rs);
2584 if (s->be_data == MO_LE) {
2585 gen_helper_casp_le_parallel(cpu_env, tcg_rs,
2586 clean_addr, t1, t2);
2587 } else {
2588 gen_helper_casp_be_parallel(cpu_env, tcg_rs,
2589 clean_addr, t1, t2);
2591 tcg_temp_free_i32(tcg_rs);
2592 } else {
2593 gen_helper_exit_atomic(cpu_env);
2594 s->base.is_jmp = DISAS_NORETURN;
2596 } else {
2597 TCGv_i64 d1 = tcg_temp_new_i64();
2598 TCGv_i64 d2 = tcg_temp_new_i64();
2599 TCGv_i64 a2 = tcg_temp_new_i64();
2600 TCGv_i64 c1 = tcg_temp_new_i64();
2601 TCGv_i64 c2 = tcg_temp_new_i64();
2602 TCGv_i64 zero = tcg_const_i64(0);
2604 /* Load the two words, in memory order. */
2605 tcg_gen_qemu_ld_i64(d1, clean_addr, memidx,
2606 MO_64 | MO_ALIGN_16 | s->be_data);
2607 tcg_gen_addi_i64(a2, clean_addr, 8);
2608 tcg_gen_qemu_ld_i64(d2, a2, memidx, MO_64 | s->be_data);
2610 /* Compare the two words, also in memory order. */
2611 tcg_gen_setcond_i64(TCG_COND_EQ, c1, d1, s1);
2612 tcg_gen_setcond_i64(TCG_COND_EQ, c2, d2, s2);
2613 tcg_gen_and_i64(c2, c2, c1);
2615 /* If compare equal, write back new data, else write back old data. */
2616 tcg_gen_movcond_i64(TCG_COND_NE, c1, c2, zero, t1, d1);
2617 tcg_gen_movcond_i64(TCG_COND_NE, c2, c2, zero, t2, d2);
2618 tcg_gen_qemu_st_i64(c1, clean_addr, memidx, MO_64 | s->be_data);
2619 tcg_gen_qemu_st_i64(c2, a2, memidx, MO_64 | s->be_data);
2620 tcg_temp_free_i64(a2);
2621 tcg_temp_free_i64(c1);
2622 tcg_temp_free_i64(c2);
2623 tcg_temp_free_i64(zero);
2625 /* Write back the data from memory to Rs. */
2626 tcg_gen_mov_i64(s1, d1);
2627 tcg_gen_mov_i64(s2, d2);
2628 tcg_temp_free_i64(d1);
2629 tcg_temp_free_i64(d2);
2633 /* Update the Sixty-Four bit (SF) registersize. This logic is derived
2634 * from the ARMv8 specs for LDR (Shared decode for all encodings).
2636 static bool disas_ldst_compute_iss_sf(int size, bool is_signed, int opc)
2638 int opc0 = extract32(opc, 0, 1);
2639 int regsize;
2641 if (is_signed) {
2642 regsize = opc0 ? 32 : 64;
2643 } else {
2644 regsize = size == 3 ? 64 : 32;
2646 return regsize == 64;
2649 /* Load/store exclusive
2651 * 31 30 29 24 23 22 21 20 16 15 14 10 9 5 4 0
2652 * +-----+-------------+----+---+----+------+----+-------+------+------+
2653 * | sz | 0 0 1 0 0 0 | o2 | L | o1 | Rs | o0 | Rt2 | Rn | Rt |
2654 * +-----+-------------+----+---+----+------+----+-------+------+------+
2656 * sz: 00 -> 8 bit, 01 -> 16 bit, 10 -> 32 bit, 11 -> 64 bit
2657 * L: 0 -> store, 1 -> load
2658 * o2: 0 -> exclusive, 1 -> not
2659 * o1: 0 -> single register, 1 -> register pair
2660 * o0: 1 -> load-acquire/store-release, 0 -> not
2662 static void disas_ldst_excl(DisasContext *s, uint32_t insn)
2664 int rt = extract32(insn, 0, 5);
2665 int rn = extract32(insn, 5, 5);
2666 int rt2 = extract32(insn, 10, 5);
2667 int rs = extract32(insn, 16, 5);
2668 int is_lasr = extract32(insn, 15, 1);
2669 int o2_L_o1_o0 = extract32(insn, 21, 3) * 2 | is_lasr;
2670 int size = extract32(insn, 30, 2);
2671 TCGv_i64 clean_addr;
2673 switch (o2_L_o1_o0) {
2674 case 0x0: /* STXR */
2675 case 0x1: /* STLXR */
2676 if (rn == 31) {
2677 gen_check_sp_alignment(s);
2679 if (is_lasr) {
2680 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2682 clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn),
2683 true, rn != 31, size);
2684 gen_store_exclusive(s, rs, rt, rt2, clean_addr, size, false);
2685 return;
2687 case 0x4: /* LDXR */
2688 case 0x5: /* LDAXR */
2689 if (rn == 31) {
2690 gen_check_sp_alignment(s);
2692 clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn),
2693 false, rn != 31, size);
2694 s->is_ldex = true;
2695 gen_load_exclusive(s, rt, rt2, clean_addr, size, false);
2696 if (is_lasr) {
2697 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2699 return;
2701 case 0x8: /* STLLR */
2702 if (!dc_isar_feature(aa64_lor, s)) {
2703 break;
2705 /* StoreLORelease is the same as Store-Release for QEMU. */
2706 /* fall through */
2707 case 0x9: /* STLR */
2708 /* Generate ISS for non-exclusive accesses including LASR. */
2709 if (rn == 31) {
2710 gen_check_sp_alignment(s);
2712 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2713 clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn),
2714 true, rn != 31, size);
2715 /* TODO: ARMv8.4-LSE SCTLR.nAA */
2716 do_gpr_st(s, cpu_reg(s, rt), clean_addr, size | MO_ALIGN, true, rt,
2717 disas_ldst_compute_iss_sf(size, false, 0), is_lasr);
2718 return;
2720 case 0xc: /* LDLAR */
2721 if (!dc_isar_feature(aa64_lor, s)) {
2722 break;
2724 /* LoadLOAcquire is the same as Load-Acquire for QEMU. */
2725 /* fall through */
2726 case 0xd: /* LDAR */
2727 /* Generate ISS for non-exclusive accesses including LASR. */
2728 if (rn == 31) {
2729 gen_check_sp_alignment(s);
2731 clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn),
2732 false, rn != 31, size);
2733 /* TODO: ARMv8.4-LSE SCTLR.nAA */
2734 do_gpr_ld(s, cpu_reg(s, rt), clean_addr, size | MO_ALIGN, false, true,
2735 rt, disas_ldst_compute_iss_sf(size, false, 0), is_lasr);
2736 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2737 return;
2739 case 0x2: case 0x3: /* CASP / STXP */
2740 if (size & 2) { /* STXP / STLXP */
2741 if (rn == 31) {
2742 gen_check_sp_alignment(s);
2744 if (is_lasr) {
2745 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2747 clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn),
2748 true, rn != 31, size);
2749 gen_store_exclusive(s, rs, rt, rt2, clean_addr, size, true);
2750 return;
2752 if (rt2 == 31
2753 && ((rt | rs) & 1) == 0
2754 && dc_isar_feature(aa64_atomics, s)) {
2755 /* CASP / CASPL */
2756 gen_compare_and_swap_pair(s, rs, rt, rn, size | 2);
2757 return;
2759 break;
2761 case 0x6: case 0x7: /* CASPA / LDXP */
2762 if (size & 2) { /* LDXP / LDAXP */
2763 if (rn == 31) {
2764 gen_check_sp_alignment(s);
2766 clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn),
2767 false, rn != 31, size);
2768 s->is_ldex = true;
2769 gen_load_exclusive(s, rt, rt2, clean_addr, size, true);
2770 if (is_lasr) {
2771 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2773 return;
2775 if (rt2 == 31
2776 && ((rt | rs) & 1) == 0
2777 && dc_isar_feature(aa64_atomics, s)) {
2778 /* CASPA / CASPAL */
2779 gen_compare_and_swap_pair(s, rs, rt, rn, size | 2);
2780 return;
2782 break;
2784 case 0xa: /* CAS */
2785 case 0xb: /* CASL */
2786 case 0xe: /* CASA */
2787 case 0xf: /* CASAL */
2788 if (rt2 == 31 && dc_isar_feature(aa64_atomics, s)) {
2789 gen_compare_and_swap(s, rs, rt, rn, size);
2790 return;
2792 break;
2794 unallocated_encoding(s);
2798 * Load register (literal)
2800 * 31 30 29 27 26 25 24 23 5 4 0
2801 * +-----+-------+---+-----+-------------------+-------+
2802 * | opc | 0 1 1 | V | 0 0 | imm19 | Rt |
2803 * +-----+-------+---+-----+-------------------+-------+
2805 * V: 1 -> vector (simd/fp)
2806 * opc (non-vector): 00 -> 32 bit, 01 -> 64 bit,
2807 * 10-> 32 bit signed, 11 -> prefetch
2808 * opc (vector): 00 -> 32 bit, 01 -> 64 bit, 10 -> 128 bit (11 unallocated)
2810 static void disas_ld_lit(DisasContext *s, uint32_t insn)
2812 int rt = extract32(insn, 0, 5);
2813 int64_t imm = sextract32(insn, 5, 19) << 2;
2814 bool is_vector = extract32(insn, 26, 1);
2815 int opc = extract32(insn, 30, 2);
2816 bool is_signed = false;
2817 int size = 2;
2818 TCGv_i64 tcg_rt, clean_addr;
2820 if (is_vector) {
2821 if (opc == 3) {
2822 unallocated_encoding(s);
2823 return;
2825 size = 2 + opc;
2826 if (!fp_access_check(s)) {
2827 return;
2829 } else {
2830 if (opc == 3) {
2831 /* PRFM (literal) : prefetch */
2832 return;
2834 size = 2 + extract32(opc, 0, 1);
2835 is_signed = extract32(opc, 1, 1);
2838 tcg_rt = cpu_reg(s, rt);
2840 clean_addr = tcg_const_i64(s->pc_curr + imm);
2841 if (is_vector) {
2842 do_fp_ld(s, rt, clean_addr, size);
2843 } else {
2844 /* Only unsigned 32bit loads target 32bit registers. */
2845 bool iss_sf = opc != 0;
2847 do_gpr_ld(s, tcg_rt, clean_addr, size + is_signed * MO_SIGN,
2848 false, true, rt, iss_sf, false);
2850 tcg_temp_free_i64(clean_addr);
2854 * LDNP (Load Pair - non-temporal hint)
2855 * LDP (Load Pair - non vector)
2856 * LDPSW (Load Pair Signed Word - non vector)
2857 * STNP (Store Pair - non-temporal hint)
2858 * STP (Store Pair - non vector)
2859 * LDNP (Load Pair of SIMD&FP - non-temporal hint)
2860 * LDP (Load Pair of SIMD&FP)
2861 * STNP (Store Pair of SIMD&FP - non-temporal hint)
2862 * STP (Store Pair of SIMD&FP)
2864 * 31 30 29 27 26 25 24 23 22 21 15 14 10 9 5 4 0
2865 * +-----+-------+---+---+-------+---+-----------------------------+
2866 * | opc | 1 0 1 | V | 0 | index | L | imm7 | Rt2 | Rn | Rt |
2867 * +-----+-------+---+---+-------+---+-------+-------+------+------+
2869 * opc: LDP/STP/LDNP/STNP 00 -> 32 bit, 10 -> 64 bit
2870 * LDPSW/STGP 01
2871 * LDP/STP/LDNP/STNP (SIMD) 00 -> 32 bit, 01 -> 64 bit, 10 -> 128 bit
2872 * V: 0 -> GPR, 1 -> Vector
2873 * idx: 00 -> signed offset with non-temporal hint, 01 -> post-index,
2874 * 10 -> signed offset, 11 -> pre-index
2875 * L: 0 -> Store 1 -> Load
2877 * Rt, Rt2 = GPR or SIMD registers to be stored
2878 * Rn = general purpose register containing address
2879 * imm7 = signed offset (multiple of 4 or 8 depending on size)
2881 static void disas_ldst_pair(DisasContext *s, uint32_t insn)
2883 int rt = extract32(insn, 0, 5);
2884 int rn = extract32(insn, 5, 5);
2885 int rt2 = extract32(insn, 10, 5);
2886 uint64_t offset = sextract64(insn, 15, 7);
2887 int index = extract32(insn, 23, 2);
2888 bool is_vector = extract32(insn, 26, 1);
2889 bool is_load = extract32(insn, 22, 1);
2890 int opc = extract32(insn, 30, 2);
2892 bool is_signed = false;
2893 bool postindex = false;
2894 bool wback = false;
2895 bool set_tag = false;
2897 TCGv_i64 clean_addr, dirty_addr;
2899 int size;
2901 if (opc == 3) {
2902 unallocated_encoding(s);
2903 return;
2906 if (is_vector) {
2907 size = 2 + opc;
2908 } else if (opc == 1 && !is_load) {
2909 /* STGP */
2910 if (!dc_isar_feature(aa64_mte_insn_reg, s) || index == 0) {
2911 unallocated_encoding(s);
2912 return;
2914 size = 3;
2915 set_tag = true;
2916 } else {
2917 size = 2 + extract32(opc, 1, 1);
2918 is_signed = extract32(opc, 0, 1);
2919 if (!is_load && is_signed) {
2920 unallocated_encoding(s);
2921 return;
2925 switch (index) {
2926 case 1: /* post-index */
2927 postindex = true;
2928 wback = true;
2929 break;
2930 case 0:
2931 /* signed offset with "non-temporal" hint. Since we don't emulate
2932 * caches we don't care about hints to the cache system about
2933 * data access patterns, and handle this identically to plain
2934 * signed offset.
2936 if (is_signed) {
2937 /* There is no non-temporal-hint version of LDPSW */
2938 unallocated_encoding(s);
2939 return;
2941 postindex = false;
2942 break;
2943 case 2: /* signed offset, rn not updated */
2944 postindex = false;
2945 break;
2946 case 3: /* pre-index */
2947 postindex = false;
2948 wback = true;
2949 break;
2952 if (is_vector && !fp_access_check(s)) {
2953 return;
2956 offset <<= (set_tag ? LOG2_TAG_GRANULE : size);
2958 if (rn == 31) {
2959 gen_check_sp_alignment(s);
2962 dirty_addr = read_cpu_reg_sp(s, rn, 1);
2963 if (!postindex) {
2964 tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
2967 if (set_tag) {
2968 if (!s->ata) {
2970 * TODO: We could rely on the stores below, at least for
2971 * system mode, if we arrange to add MO_ALIGN_16.
2973 gen_helper_stg_stub(cpu_env, dirty_addr);
2974 } else if (tb_cflags(s->base.tb) & CF_PARALLEL) {
2975 gen_helper_stg_parallel(cpu_env, dirty_addr, dirty_addr);
2976 } else {
2977 gen_helper_stg(cpu_env, dirty_addr, dirty_addr);
2981 clean_addr = gen_mte_checkN(s, dirty_addr, !is_load,
2982 (wback || rn != 31) && !set_tag, 2 << size);
2984 if (is_vector) {
2985 if (is_load) {
2986 do_fp_ld(s, rt, clean_addr, size);
2987 } else {
2988 do_fp_st(s, rt, clean_addr, size);
2990 tcg_gen_addi_i64(clean_addr, clean_addr, 1 << size);
2991 if (is_load) {
2992 do_fp_ld(s, rt2, clean_addr, size);
2993 } else {
2994 do_fp_st(s, rt2, clean_addr, size);
2996 } else {
2997 TCGv_i64 tcg_rt = cpu_reg(s, rt);
2998 TCGv_i64 tcg_rt2 = cpu_reg(s, rt2);
3000 if (is_load) {
3001 TCGv_i64 tmp = tcg_temp_new_i64();
3003 /* Do not modify tcg_rt before recognizing any exception
3004 * from the second load.
3006 do_gpr_ld(s, tmp, clean_addr, size + is_signed * MO_SIGN,
3007 false, false, 0, false, false);
3008 tcg_gen_addi_i64(clean_addr, clean_addr, 1 << size);
3009 do_gpr_ld(s, tcg_rt2, clean_addr, size + is_signed * MO_SIGN,
3010 false, false, 0, false, false);
3012 tcg_gen_mov_i64(tcg_rt, tmp);
3013 tcg_temp_free_i64(tmp);
3014 } else {
3015 do_gpr_st(s, tcg_rt, clean_addr, size,
3016 false, 0, false, false);
3017 tcg_gen_addi_i64(clean_addr, clean_addr, 1 << size);
3018 do_gpr_st(s, tcg_rt2, clean_addr, size,
3019 false, 0, false, false);
3023 if (wback) {
3024 if (postindex) {
3025 tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3027 tcg_gen_mov_i64(cpu_reg_sp(s, rn), dirty_addr);
3032 * Load/store (immediate post-indexed)
3033 * Load/store (immediate pre-indexed)
3034 * Load/store (unscaled immediate)
3036 * 31 30 29 27 26 25 24 23 22 21 20 12 11 10 9 5 4 0
3037 * +----+-------+---+-----+-----+---+--------+-----+------+------+
3038 * |size| 1 1 1 | V | 0 0 | opc | 0 | imm9 | idx | Rn | Rt |
3039 * +----+-------+---+-----+-----+---+--------+-----+------+------+
3041 * idx = 01 -> post-indexed, 11 pre-indexed, 00 unscaled imm. (no writeback)
3042 10 -> unprivileged
3043 * V = 0 -> non-vector
3044 * size: 00 -> 8 bit, 01 -> 16 bit, 10 -> 32 bit, 11 -> 64bit
3045 * opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
3047 static void disas_ldst_reg_imm9(DisasContext *s, uint32_t insn,
3048 int opc,
3049 int size,
3050 int rt,
3051 bool is_vector)
3053 int rn = extract32(insn, 5, 5);
3054 int imm9 = sextract32(insn, 12, 9);
3055 int idx = extract32(insn, 10, 2);
3056 bool is_signed = false;
3057 bool is_store = false;
3058 bool is_extended = false;
3059 bool is_unpriv = (idx == 2);
3060 bool iss_valid = !is_vector;
3061 bool post_index;
3062 bool writeback;
3063 int memidx;
3065 TCGv_i64 clean_addr, dirty_addr;
3067 if (is_vector) {
3068 size |= (opc & 2) << 1;
3069 if (size > 4 || is_unpriv) {
3070 unallocated_encoding(s);
3071 return;
3073 is_store = ((opc & 1) == 0);
3074 if (!fp_access_check(s)) {
3075 return;
3077 } else {
3078 if (size == 3 && opc == 2) {
3079 /* PRFM - prefetch */
3080 if (idx != 0) {
3081 unallocated_encoding(s);
3082 return;
3084 return;
3086 if (opc == 3 && size > 1) {
3087 unallocated_encoding(s);
3088 return;
3090 is_store = (opc == 0);
3091 is_signed = extract32(opc, 1, 1);
3092 is_extended = (size < 3) && extract32(opc, 0, 1);
3095 switch (idx) {
3096 case 0:
3097 case 2:
3098 post_index = false;
3099 writeback = false;
3100 break;
3101 case 1:
3102 post_index = true;
3103 writeback = true;
3104 break;
3105 case 3:
3106 post_index = false;
3107 writeback = true;
3108 break;
3109 default:
3110 g_assert_not_reached();
3113 if (rn == 31) {
3114 gen_check_sp_alignment(s);
3117 dirty_addr = read_cpu_reg_sp(s, rn, 1);
3118 if (!post_index) {
3119 tcg_gen_addi_i64(dirty_addr, dirty_addr, imm9);
3122 memidx = is_unpriv ? get_a64_user_mem_index(s) : get_mem_index(s);
3123 clean_addr = gen_mte_check1_mmuidx(s, dirty_addr, is_store,
3124 writeback || rn != 31,
3125 size, is_unpriv, memidx);
3127 if (is_vector) {
3128 if (is_store) {
3129 do_fp_st(s, rt, clean_addr, size);
3130 } else {
3131 do_fp_ld(s, rt, clean_addr, size);
3133 } else {
3134 TCGv_i64 tcg_rt = cpu_reg(s, rt);
3135 bool iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
3137 if (is_store) {
3138 do_gpr_st_memidx(s, tcg_rt, clean_addr, size, memidx,
3139 iss_valid, rt, iss_sf, false);
3140 } else {
3141 do_gpr_ld_memidx(s, tcg_rt, clean_addr, size + is_signed * MO_SIGN,
3142 is_extended, memidx,
3143 iss_valid, rt, iss_sf, false);
3147 if (writeback) {
3148 TCGv_i64 tcg_rn = cpu_reg_sp(s, rn);
3149 if (post_index) {
3150 tcg_gen_addi_i64(dirty_addr, dirty_addr, imm9);
3152 tcg_gen_mov_i64(tcg_rn, dirty_addr);
3157 * Load/store (register offset)
3159 * 31 30 29 27 26 25 24 23 22 21 20 16 15 13 12 11 10 9 5 4 0
3160 * +----+-------+---+-----+-----+---+------+-----+--+-----+----+----+
3161 * |size| 1 1 1 | V | 0 0 | opc | 1 | Rm | opt | S| 1 0 | Rn | Rt |
3162 * +----+-------+---+-----+-----+---+------+-----+--+-----+----+----+
3164 * For non-vector:
3165 * size: 00-> byte, 01 -> 16 bit, 10 -> 32bit, 11 -> 64bit
3166 * opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
3167 * For vector:
3168 * size is opc<1>:size<1:0> so 100 -> 128 bit; 110 and 111 unallocated
3169 * opc<0>: 0 -> store, 1 -> load
3170 * V: 1 -> vector/simd
3171 * opt: extend encoding (see DecodeRegExtend)
3172 * S: if S=1 then scale (essentially index by sizeof(size))
3173 * Rt: register to transfer into/out of
3174 * Rn: address register or SP for base
3175 * Rm: offset register or ZR for offset
3177 static void disas_ldst_reg_roffset(DisasContext *s, uint32_t insn,
3178 int opc,
3179 int size,
3180 int rt,
3181 bool is_vector)
3183 int rn = extract32(insn, 5, 5);
3184 int shift = extract32(insn, 12, 1);
3185 int rm = extract32(insn, 16, 5);
3186 int opt = extract32(insn, 13, 3);
3187 bool is_signed = false;
3188 bool is_store = false;
3189 bool is_extended = false;
3191 TCGv_i64 tcg_rm, clean_addr, dirty_addr;
3193 if (extract32(opt, 1, 1) == 0) {
3194 unallocated_encoding(s);
3195 return;
3198 if (is_vector) {
3199 size |= (opc & 2) << 1;
3200 if (size > 4) {
3201 unallocated_encoding(s);
3202 return;
3204 is_store = !extract32(opc, 0, 1);
3205 if (!fp_access_check(s)) {
3206 return;
3208 } else {
3209 if (size == 3 && opc == 2) {
3210 /* PRFM - prefetch */
3211 return;
3213 if (opc == 3 && size > 1) {
3214 unallocated_encoding(s);
3215 return;
3217 is_store = (opc == 0);
3218 is_signed = extract32(opc, 1, 1);
3219 is_extended = (size < 3) && extract32(opc, 0, 1);
3222 if (rn == 31) {
3223 gen_check_sp_alignment(s);
3225 dirty_addr = read_cpu_reg_sp(s, rn, 1);
3227 tcg_rm = read_cpu_reg(s, rm, 1);
3228 ext_and_shift_reg(tcg_rm, tcg_rm, opt, shift ? size : 0);
3230 tcg_gen_add_i64(dirty_addr, dirty_addr, tcg_rm);
3231 clean_addr = gen_mte_check1(s, dirty_addr, is_store, true, size);
3233 if (is_vector) {
3234 if (is_store) {
3235 do_fp_st(s, rt, clean_addr, size);
3236 } else {
3237 do_fp_ld(s, rt, clean_addr, size);
3239 } else {
3240 TCGv_i64 tcg_rt = cpu_reg(s, rt);
3241 bool iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
3242 if (is_store) {
3243 do_gpr_st(s, tcg_rt, clean_addr, size,
3244 true, rt, iss_sf, false);
3245 } else {
3246 do_gpr_ld(s, tcg_rt, clean_addr, size + is_signed * MO_SIGN,
3247 is_extended, true, rt, iss_sf, false);
3253 * Load/store (unsigned immediate)
3255 * 31 30 29 27 26 25 24 23 22 21 10 9 5
3256 * +----+-------+---+-----+-----+------------+-------+------+
3257 * |size| 1 1 1 | V | 0 1 | opc | imm12 | Rn | Rt |
3258 * +----+-------+---+-----+-----+------------+-------+------+
3260 * For non-vector:
3261 * size: 00-> byte, 01 -> 16 bit, 10 -> 32bit, 11 -> 64bit
3262 * opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
3263 * For vector:
3264 * size is opc<1>:size<1:0> so 100 -> 128 bit; 110 and 111 unallocated
3265 * opc<0>: 0 -> store, 1 -> load
3266 * Rn: base address register (inc SP)
3267 * Rt: target register
3269 static void disas_ldst_reg_unsigned_imm(DisasContext *s, uint32_t insn,
3270 int opc,
3271 int size,
3272 int rt,
3273 bool is_vector)
3275 int rn = extract32(insn, 5, 5);
3276 unsigned int imm12 = extract32(insn, 10, 12);
3277 unsigned int offset;
3279 TCGv_i64 clean_addr, dirty_addr;
3281 bool is_store;
3282 bool is_signed = false;
3283 bool is_extended = false;
3285 if (is_vector) {
3286 size |= (opc & 2) << 1;
3287 if (size > 4) {
3288 unallocated_encoding(s);
3289 return;
3291 is_store = !extract32(opc, 0, 1);
3292 if (!fp_access_check(s)) {
3293 return;
3295 } else {
3296 if (size == 3 && opc == 2) {
3297 /* PRFM - prefetch */
3298 return;
3300 if (opc == 3 && size > 1) {
3301 unallocated_encoding(s);
3302 return;
3304 is_store = (opc == 0);
3305 is_signed = extract32(opc, 1, 1);
3306 is_extended = (size < 3) && extract32(opc, 0, 1);
3309 if (rn == 31) {
3310 gen_check_sp_alignment(s);
3312 dirty_addr = read_cpu_reg_sp(s, rn, 1);
3313 offset = imm12 << size;
3314 tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3315 clean_addr = gen_mte_check1(s, dirty_addr, is_store, rn != 31, size);
3317 if (is_vector) {
3318 if (is_store) {
3319 do_fp_st(s, rt, clean_addr, size);
3320 } else {
3321 do_fp_ld(s, rt, clean_addr, size);
3323 } else {
3324 TCGv_i64 tcg_rt = cpu_reg(s, rt);
3325 bool iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
3326 if (is_store) {
3327 do_gpr_st(s, tcg_rt, clean_addr, size,
3328 true, rt, iss_sf, false);
3329 } else {
3330 do_gpr_ld(s, tcg_rt, clean_addr, size + is_signed * MO_SIGN,
3331 is_extended, true, rt, iss_sf, false);
3336 /* Atomic memory operations
3338 * 31 30 27 26 24 22 21 16 15 12 10 5 0
3339 * +------+-------+---+-----+-----+---+----+----+-----+-----+----+-----+
3340 * | size | 1 1 1 | V | 0 0 | A R | 1 | Rs | o3 | opc | 0 0 | Rn | Rt |
3341 * +------+-------+---+-----+-----+--------+----+-----+-----+----+-----+
3343 * Rt: the result register
3344 * Rn: base address or SP
3345 * Rs: the source register for the operation
3346 * V: vector flag (always 0 as of v8.3)
3347 * A: acquire flag
3348 * R: release flag
3350 static void disas_ldst_atomic(DisasContext *s, uint32_t insn,
3351 int size, int rt, bool is_vector)
3353 int rs = extract32(insn, 16, 5);
3354 int rn = extract32(insn, 5, 5);
3355 int o3_opc = extract32(insn, 12, 4);
3356 bool r = extract32(insn, 22, 1);
3357 bool a = extract32(insn, 23, 1);
3358 TCGv_i64 tcg_rs, tcg_rt, clean_addr;
3359 AtomicThreeOpFn *fn = NULL;
3360 MemOp mop = s->be_data | size | MO_ALIGN;
3362 if (is_vector || !dc_isar_feature(aa64_atomics, s)) {
3363 unallocated_encoding(s);
3364 return;
3366 switch (o3_opc) {
3367 case 000: /* LDADD */
3368 fn = tcg_gen_atomic_fetch_add_i64;
3369 break;
3370 case 001: /* LDCLR */
3371 fn = tcg_gen_atomic_fetch_and_i64;
3372 break;
3373 case 002: /* LDEOR */
3374 fn = tcg_gen_atomic_fetch_xor_i64;
3375 break;
3376 case 003: /* LDSET */
3377 fn = tcg_gen_atomic_fetch_or_i64;
3378 break;
3379 case 004: /* LDSMAX */
3380 fn = tcg_gen_atomic_fetch_smax_i64;
3381 mop |= MO_SIGN;
3382 break;
3383 case 005: /* LDSMIN */
3384 fn = tcg_gen_atomic_fetch_smin_i64;
3385 mop |= MO_SIGN;
3386 break;
3387 case 006: /* LDUMAX */
3388 fn = tcg_gen_atomic_fetch_umax_i64;
3389 break;
3390 case 007: /* LDUMIN */
3391 fn = tcg_gen_atomic_fetch_umin_i64;
3392 break;
3393 case 010: /* SWP */
3394 fn = tcg_gen_atomic_xchg_i64;
3395 break;
3396 case 014: /* LDAPR, LDAPRH, LDAPRB */
3397 if (!dc_isar_feature(aa64_rcpc_8_3, s) ||
3398 rs != 31 || a != 1 || r != 0) {
3399 unallocated_encoding(s);
3400 return;
3402 break;
3403 default:
3404 unallocated_encoding(s);
3405 return;
3408 if (rn == 31) {
3409 gen_check_sp_alignment(s);
3411 clean_addr = gen_mte_check1(s, cpu_reg_sp(s, rn), false, rn != 31, size);
3413 if (o3_opc == 014) {
3415 * LDAPR* are a special case because they are a simple load, not a
3416 * fetch-and-do-something op.
3417 * The architectural consistency requirements here are weaker than
3418 * full load-acquire (we only need "load-acquire processor consistent"),
3419 * but we choose to implement them as full LDAQ.
3421 do_gpr_ld(s, cpu_reg(s, rt), clean_addr, size, false,
3422 true, rt, disas_ldst_compute_iss_sf(size, false, 0), true);
3423 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
3424 return;
3427 tcg_rs = read_cpu_reg(s, rs, true);
3428 tcg_rt = cpu_reg(s, rt);
3430 if (o3_opc == 1) { /* LDCLR */
3431 tcg_gen_not_i64(tcg_rs, tcg_rs);
3434 /* The tcg atomic primitives are all full barriers. Therefore we
3435 * can ignore the Acquire and Release bits of this instruction.
3437 fn(tcg_rt, clean_addr, tcg_rs, get_mem_index(s), mop);
3439 if ((mop & MO_SIGN) && size != MO_64) {
3440 tcg_gen_ext32u_i64(tcg_rt, tcg_rt);
3445 * PAC memory operations
3447 * 31 30 27 26 24 22 21 12 11 10 5 0
3448 * +------+-------+---+-----+-----+---+--------+---+---+----+-----+
3449 * | size | 1 1 1 | V | 0 0 | M S | 1 | imm9 | W | 1 | Rn | Rt |
3450 * +------+-------+---+-----+-----+---+--------+---+---+----+-----+
3452 * Rt: the result register
3453 * Rn: base address or SP
3454 * V: vector flag (always 0 as of v8.3)
3455 * M: clear for key DA, set for key DB
3456 * W: pre-indexing flag
3457 * S: sign for imm9.
3459 static void disas_ldst_pac(DisasContext *s, uint32_t insn,
3460 int size, int rt, bool is_vector)
3462 int rn = extract32(insn, 5, 5);
3463 bool is_wback = extract32(insn, 11, 1);
3464 bool use_key_a = !extract32(insn, 23, 1);
3465 int offset;
3466 TCGv_i64 clean_addr, dirty_addr, tcg_rt;
3468 if (size != 3 || is_vector || !dc_isar_feature(aa64_pauth, s)) {
3469 unallocated_encoding(s);
3470 return;
3473 if (rn == 31) {
3474 gen_check_sp_alignment(s);
3476 dirty_addr = read_cpu_reg_sp(s, rn, 1);
3478 if (s->pauth_active) {
3479 if (use_key_a) {
3480 gen_helper_autda(dirty_addr, cpu_env, dirty_addr,
3481 new_tmp_a64_zero(s));
3482 } else {
3483 gen_helper_autdb(dirty_addr, cpu_env, dirty_addr,
3484 new_tmp_a64_zero(s));
3488 /* Form the 10-bit signed, scaled offset. */
3489 offset = (extract32(insn, 22, 1) << 9) | extract32(insn, 12, 9);
3490 offset = sextract32(offset << size, 0, 10 + size);
3491 tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3493 /* Note that "clean" and "dirty" here refer to TBI not PAC. */
3494 clean_addr = gen_mte_check1(s, dirty_addr, false,
3495 is_wback || rn != 31, size);
3497 tcg_rt = cpu_reg(s, rt);
3498 do_gpr_ld(s, tcg_rt, clean_addr, size,
3499 /* extend */ false, /* iss_valid */ !is_wback,
3500 /* iss_srt */ rt, /* iss_sf */ true, /* iss_ar */ false);
3502 if (is_wback) {
3503 tcg_gen_mov_i64(cpu_reg_sp(s, rn), dirty_addr);
3508 * LDAPR/STLR (unscaled immediate)
3510 * 31 30 24 22 21 12 10 5 0
3511 * +------+-------------+-----+---+--------+-----+----+-----+
3512 * | size | 0 1 1 0 0 1 | opc | 0 | imm9 | 0 0 | Rn | Rt |
3513 * +------+-------------+-----+---+--------+-----+----+-----+
3515 * Rt: source or destination register
3516 * Rn: base register
3517 * imm9: unscaled immediate offset
3518 * opc: 00: STLUR*, 01/10/11: various LDAPUR*
3519 * size: size of load/store
3521 static void disas_ldst_ldapr_stlr(DisasContext *s, uint32_t insn)
3523 int rt = extract32(insn, 0, 5);
3524 int rn = extract32(insn, 5, 5);
3525 int offset = sextract32(insn, 12, 9);
3526 int opc = extract32(insn, 22, 2);
3527 int size = extract32(insn, 30, 2);
3528 TCGv_i64 clean_addr, dirty_addr;
3529 bool is_store = false;
3530 bool extend = false;
3531 bool iss_sf;
3532 MemOp mop;
3534 if (!dc_isar_feature(aa64_rcpc_8_4, s)) {
3535 unallocated_encoding(s);
3536 return;
3539 /* TODO: ARMv8.4-LSE SCTLR.nAA */
3540 mop = size | MO_ALIGN;
3542 switch (opc) {
3543 case 0: /* STLURB */
3544 is_store = true;
3545 break;
3546 case 1: /* LDAPUR* */
3547 break;
3548 case 2: /* LDAPURS* 64-bit variant */
3549 if (size == 3) {
3550 unallocated_encoding(s);
3551 return;
3553 mop |= MO_SIGN;
3554 break;
3555 case 3: /* LDAPURS* 32-bit variant */
3556 if (size > 1) {
3557 unallocated_encoding(s);
3558 return;
3560 mop |= MO_SIGN;
3561 extend = true; /* zero-extend 32->64 after signed load */
3562 break;
3563 default:
3564 g_assert_not_reached();
3567 iss_sf = disas_ldst_compute_iss_sf(size, (mop & MO_SIGN) != 0, opc);
3569 if (rn == 31) {
3570 gen_check_sp_alignment(s);
3573 dirty_addr = read_cpu_reg_sp(s, rn, 1);
3574 tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3575 clean_addr = clean_data_tbi(s, dirty_addr);
3577 if (is_store) {
3578 /* Store-Release semantics */
3579 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
3580 do_gpr_st(s, cpu_reg(s, rt), clean_addr, mop, true, rt, iss_sf, true);
3581 } else {
3583 * Load-AcquirePC semantics; we implement as the slightly more
3584 * restrictive Load-Acquire.
3586 do_gpr_ld(s, cpu_reg(s, rt), clean_addr, mop,
3587 extend, true, rt, iss_sf, true);
3588 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
3592 /* Load/store register (all forms) */
3593 static void disas_ldst_reg(DisasContext *s, uint32_t insn)
3595 int rt = extract32(insn, 0, 5);
3596 int opc = extract32(insn, 22, 2);
3597 bool is_vector = extract32(insn, 26, 1);
3598 int size = extract32(insn, 30, 2);
3600 switch (extract32(insn, 24, 2)) {
3601 case 0:
3602 if (extract32(insn, 21, 1) == 0) {
3603 /* Load/store register (unscaled immediate)
3604 * Load/store immediate pre/post-indexed
3605 * Load/store register unprivileged
3607 disas_ldst_reg_imm9(s, insn, opc, size, rt, is_vector);
3608 return;
3610 switch (extract32(insn, 10, 2)) {
3611 case 0:
3612 disas_ldst_atomic(s, insn, size, rt, is_vector);
3613 return;
3614 case 2:
3615 disas_ldst_reg_roffset(s, insn, opc, size, rt, is_vector);
3616 return;
3617 default:
3618 disas_ldst_pac(s, insn, size, rt, is_vector);
3619 return;
3621 break;
3622 case 1:
3623 disas_ldst_reg_unsigned_imm(s, insn, opc, size, rt, is_vector);
3624 return;
3626 unallocated_encoding(s);
3629 /* AdvSIMD load/store multiple structures
3631 * 31 30 29 23 22 21 16 15 12 11 10 9 5 4 0
3632 * +---+---+---------------+---+-------------+--------+------+------+------+
3633 * | 0 | Q | 0 0 1 1 0 0 0 | L | 0 0 0 0 0 0 | opcode | size | Rn | Rt |
3634 * +---+---+---------------+---+-------------+--------+------+------+------+
3636 * AdvSIMD load/store multiple structures (post-indexed)
3638 * 31 30 29 23 22 21 20 16 15 12 11 10 9 5 4 0
3639 * +---+---+---------------+---+---+---------+--------+------+------+------+
3640 * | 0 | Q | 0 0 1 1 0 0 1 | L | 0 | Rm | opcode | size | Rn | Rt |
3641 * +---+---+---------------+---+---+---------+--------+------+------+------+
3643 * Rt: first (or only) SIMD&FP register to be transferred
3644 * Rn: base address or SP
3645 * Rm (post-index only): post-index register (when !31) or size dependent #imm
3647 static void disas_ldst_multiple_struct(DisasContext *s, uint32_t insn)
3649 int rt = extract32(insn, 0, 5);
3650 int rn = extract32(insn, 5, 5);
3651 int rm = extract32(insn, 16, 5);
3652 int size = extract32(insn, 10, 2);
3653 int opcode = extract32(insn, 12, 4);
3654 bool is_store = !extract32(insn, 22, 1);
3655 bool is_postidx = extract32(insn, 23, 1);
3656 bool is_q = extract32(insn, 30, 1);
3657 TCGv_i64 clean_addr, tcg_rn, tcg_ebytes;
3658 MemOp endian, align, mop;
3660 int total; /* total bytes */
3661 int elements; /* elements per vector */
3662 int rpt; /* num iterations */
3663 int selem; /* structure elements */
3664 int r;
3666 if (extract32(insn, 31, 1) || extract32(insn, 21, 1)) {
3667 unallocated_encoding(s);
3668 return;
3671 if (!is_postidx && rm != 0) {
3672 unallocated_encoding(s);
3673 return;
3676 /* From the shared decode logic */
3677 switch (opcode) {
3678 case 0x0:
3679 rpt = 1;
3680 selem = 4;
3681 break;
3682 case 0x2:
3683 rpt = 4;
3684 selem = 1;
3685 break;
3686 case 0x4:
3687 rpt = 1;
3688 selem = 3;
3689 break;
3690 case 0x6:
3691 rpt = 3;
3692 selem = 1;
3693 break;
3694 case 0x7:
3695 rpt = 1;
3696 selem = 1;
3697 break;
3698 case 0x8:
3699 rpt = 1;
3700 selem = 2;
3701 break;
3702 case 0xa:
3703 rpt = 2;
3704 selem = 1;
3705 break;
3706 default:
3707 unallocated_encoding(s);
3708 return;
3711 if (size == 3 && !is_q && selem != 1) {
3712 /* reserved */
3713 unallocated_encoding(s);
3714 return;
3717 if (!fp_access_check(s)) {
3718 return;
3721 if (rn == 31) {
3722 gen_check_sp_alignment(s);
3725 /* For our purposes, bytes are always little-endian. */
3726 endian = s->be_data;
3727 if (size == 0) {
3728 endian = MO_LE;
3731 total = rpt * selem * (is_q ? 16 : 8);
3732 tcg_rn = cpu_reg_sp(s, rn);
3735 * Issue the MTE check vs the logical repeat count, before we
3736 * promote consecutive little-endian elements below.
3738 clean_addr = gen_mte_checkN(s, tcg_rn, is_store, is_postidx || rn != 31,
3739 total);
3742 * Consecutive little-endian elements from a single register
3743 * can be promoted to a larger little-endian operation.
3745 align = MO_ALIGN;
3746 if (selem == 1 && endian == MO_LE) {
3747 align = pow2_align(size);
3748 size = 3;
3750 if (!s->align_mem) {
3751 align = 0;
3753 mop = endian | size | align;
3755 elements = (is_q ? 16 : 8) >> size;
3756 tcg_ebytes = tcg_const_i64(1 << size);
3757 for (r = 0; r < rpt; r++) {
3758 int e;
3759 for (e = 0; e < elements; e++) {
3760 int xs;
3761 for (xs = 0; xs < selem; xs++) {
3762 int tt = (rt + r + xs) % 32;
3763 if (is_store) {
3764 do_vec_st(s, tt, e, clean_addr, mop);
3765 } else {
3766 do_vec_ld(s, tt, e, clean_addr, mop);
3768 tcg_gen_add_i64(clean_addr, clean_addr, tcg_ebytes);
3772 tcg_temp_free_i64(tcg_ebytes);
3774 if (!is_store) {
3775 /* For non-quad operations, setting a slice of the low
3776 * 64 bits of the register clears the high 64 bits (in
3777 * the ARM ARM pseudocode this is implicit in the fact
3778 * that 'rval' is a 64 bit wide variable).
3779 * For quad operations, we might still need to zero the
3780 * high bits of SVE.
3782 for (r = 0; r < rpt * selem; r++) {
3783 int tt = (rt + r) % 32;
3784 clear_vec_high(s, is_q, tt);
3788 if (is_postidx) {
3789 if (rm == 31) {
3790 tcg_gen_addi_i64(tcg_rn, tcg_rn, total);
3791 } else {
3792 tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, rm));
3797 /* AdvSIMD load/store single structure
3799 * 31 30 29 23 22 21 20 16 15 13 12 11 10 9 5 4 0
3800 * +---+---+---------------+-----+-----------+-----+---+------+------+------+
3801 * | 0 | Q | 0 0 1 1 0 1 0 | L R | 0 0 0 0 0 | opc | S | size | Rn | Rt |
3802 * +---+---+---------------+-----+-----------+-----+---+------+------+------+
3804 * AdvSIMD load/store single structure (post-indexed)
3806 * 31 30 29 23 22 21 20 16 15 13 12 11 10 9 5 4 0
3807 * +---+---+---------------+-----+-----------+-----+---+------+------+------+
3808 * | 0 | Q | 0 0 1 1 0 1 1 | L R | Rm | opc | S | size | Rn | Rt |
3809 * +---+---+---------------+-----+-----------+-----+---+------+------+------+
3811 * Rt: first (or only) SIMD&FP register to be transferred
3812 * Rn: base address or SP
3813 * Rm (post-index only): post-index register (when !31) or size dependent #imm
3814 * index = encoded in Q:S:size dependent on size
3816 * lane_size = encoded in R, opc
3817 * transfer width = encoded in opc, S, size
3819 static void disas_ldst_single_struct(DisasContext *s, uint32_t insn)
3821 int rt = extract32(insn, 0, 5);
3822 int rn = extract32(insn, 5, 5);
3823 int rm = extract32(insn, 16, 5);
3824 int size = extract32(insn, 10, 2);
3825 int S = extract32(insn, 12, 1);
3826 int opc = extract32(insn, 13, 3);
3827 int R = extract32(insn, 21, 1);
3828 int is_load = extract32(insn, 22, 1);
3829 int is_postidx = extract32(insn, 23, 1);
3830 int is_q = extract32(insn, 30, 1);
3832 int scale = extract32(opc, 1, 2);
3833 int selem = (extract32(opc, 0, 1) << 1 | R) + 1;
3834 bool replicate = false;
3835 int index = is_q << 3 | S << 2 | size;
3836 int xs, total;
3837 TCGv_i64 clean_addr, tcg_rn, tcg_ebytes;
3838 MemOp mop;
3840 if (extract32(insn, 31, 1)) {
3841 unallocated_encoding(s);
3842 return;
3844 if (!is_postidx && rm != 0) {
3845 unallocated_encoding(s);
3846 return;
3849 switch (scale) {
3850 case 3:
3851 if (!is_load || S) {
3852 unallocated_encoding(s);
3853 return;
3855 scale = size;
3856 replicate = true;
3857 break;
3858 case 0:
3859 break;
3860 case 1:
3861 if (extract32(size, 0, 1)) {
3862 unallocated_encoding(s);
3863 return;
3865 index >>= 1;
3866 break;
3867 case 2:
3868 if (extract32(size, 1, 1)) {
3869 unallocated_encoding(s);
3870 return;
3872 if (!extract32(size, 0, 1)) {
3873 index >>= 2;
3874 } else {
3875 if (S) {
3876 unallocated_encoding(s);
3877 return;
3879 index >>= 3;
3880 scale = 3;
3882 break;
3883 default:
3884 g_assert_not_reached();
3887 if (!fp_access_check(s)) {
3888 return;
3891 if (rn == 31) {
3892 gen_check_sp_alignment(s);
3895 total = selem << scale;
3896 tcg_rn = cpu_reg_sp(s, rn);
3898 clean_addr = gen_mte_checkN(s, tcg_rn, !is_load, is_postidx || rn != 31,
3899 total);
3900 mop = finalize_memop(s, scale);
3902 tcg_ebytes = tcg_const_i64(1 << scale);
3903 for (xs = 0; xs < selem; xs++) {
3904 if (replicate) {
3905 /* Load and replicate to all elements */
3906 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
3908 tcg_gen_qemu_ld_i64(tcg_tmp, clean_addr, get_mem_index(s), mop);
3909 tcg_gen_gvec_dup_i64(scale, vec_full_reg_offset(s, rt),
3910 (is_q + 1) * 8, vec_full_reg_size(s),
3911 tcg_tmp);
3912 tcg_temp_free_i64(tcg_tmp);
3913 } else {
3914 /* Load/store one element per register */
3915 if (is_load) {
3916 do_vec_ld(s, rt, index, clean_addr, mop);
3917 } else {
3918 do_vec_st(s, rt, index, clean_addr, mop);
3921 tcg_gen_add_i64(clean_addr, clean_addr, tcg_ebytes);
3922 rt = (rt + 1) % 32;
3924 tcg_temp_free_i64(tcg_ebytes);
3926 if (is_postidx) {
3927 if (rm == 31) {
3928 tcg_gen_addi_i64(tcg_rn, tcg_rn, total);
3929 } else {
3930 tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, rm));
3936 * Load/Store memory tags
3938 * 31 30 29 24 22 21 12 10 5 0
3939 * +-----+-------------+-----+---+------+-----+------+------+
3940 * | 1 1 | 0 1 1 0 0 1 | op1 | 1 | imm9 | op2 | Rn | Rt |
3941 * +-----+-------------+-----+---+------+-----+------+------+
3943 static void disas_ldst_tag(DisasContext *s, uint32_t insn)
3945 int rt = extract32(insn, 0, 5);
3946 int rn = extract32(insn, 5, 5);
3947 uint64_t offset = sextract64(insn, 12, 9) << LOG2_TAG_GRANULE;
3948 int op2 = extract32(insn, 10, 2);
3949 int op1 = extract32(insn, 22, 2);
3950 bool is_load = false, is_pair = false, is_zero = false, is_mult = false;
3951 int index = 0;
3952 TCGv_i64 addr, clean_addr, tcg_rt;
3954 /* We checked insn bits [29:24,21] in the caller. */
3955 if (extract32(insn, 30, 2) != 3) {
3956 goto do_unallocated;
3960 * @index is a tri-state variable which has 3 states:
3961 * < 0 : post-index, writeback
3962 * = 0 : signed offset
3963 * > 0 : pre-index, writeback
3965 switch (op1) {
3966 case 0:
3967 if (op2 != 0) {
3968 /* STG */
3969 index = op2 - 2;
3970 } else {
3971 /* STZGM */
3972 if (s->current_el == 0 || offset != 0) {
3973 goto do_unallocated;
3975 is_mult = is_zero = true;
3977 break;
3978 case 1:
3979 if (op2 != 0) {
3980 /* STZG */
3981 is_zero = true;
3982 index = op2 - 2;
3983 } else {
3984 /* LDG */
3985 is_load = true;
3987 break;
3988 case 2:
3989 if (op2 != 0) {
3990 /* ST2G */
3991 is_pair = true;
3992 index = op2 - 2;
3993 } else {
3994 /* STGM */
3995 if (s->current_el == 0 || offset != 0) {
3996 goto do_unallocated;
3998 is_mult = true;
4000 break;
4001 case 3:
4002 if (op2 != 0) {
4003 /* STZ2G */
4004 is_pair = is_zero = true;
4005 index = op2 - 2;
4006 } else {
4007 /* LDGM */
4008 if (s->current_el == 0 || offset != 0) {
4009 goto do_unallocated;
4011 is_mult = is_load = true;
4013 break;
4015 default:
4016 do_unallocated:
4017 unallocated_encoding(s);
4018 return;
4021 if (is_mult
4022 ? !dc_isar_feature(aa64_mte, s)
4023 : !dc_isar_feature(aa64_mte_insn_reg, s)) {
4024 goto do_unallocated;
4027 if (rn == 31) {
4028 gen_check_sp_alignment(s);
4031 addr = read_cpu_reg_sp(s, rn, true);
4032 if (index >= 0) {
4033 /* pre-index or signed offset */
4034 tcg_gen_addi_i64(addr, addr, offset);
4037 if (is_mult) {
4038 tcg_rt = cpu_reg(s, rt);
4040 if (is_zero) {
4041 int size = 4 << s->dcz_blocksize;
4043 if (s->ata) {
4044 gen_helper_stzgm_tags(cpu_env, addr, tcg_rt);
4047 * The non-tags portion of STZGM is mostly like DC_ZVA,
4048 * except the alignment happens before the access.
4050 clean_addr = clean_data_tbi(s, addr);
4051 tcg_gen_andi_i64(clean_addr, clean_addr, -size);
4052 gen_helper_dc_zva(cpu_env, clean_addr);
4053 } else if (s->ata) {
4054 if (is_load) {
4055 gen_helper_ldgm(tcg_rt, cpu_env, addr);
4056 } else {
4057 gen_helper_stgm(cpu_env, addr, tcg_rt);
4059 } else {
4060 MMUAccessType acc = is_load ? MMU_DATA_LOAD : MMU_DATA_STORE;
4061 int size = 4 << GMID_EL1_BS;
4063 clean_addr = clean_data_tbi(s, addr);
4064 tcg_gen_andi_i64(clean_addr, clean_addr, -size);
4065 gen_probe_access(s, clean_addr, acc, size);
4067 if (is_load) {
4068 /* The result tags are zeros. */
4069 tcg_gen_movi_i64(tcg_rt, 0);
4072 return;
4075 if (is_load) {
4076 tcg_gen_andi_i64(addr, addr, -TAG_GRANULE);
4077 tcg_rt = cpu_reg(s, rt);
4078 if (s->ata) {
4079 gen_helper_ldg(tcg_rt, cpu_env, addr, tcg_rt);
4080 } else {
4081 clean_addr = clean_data_tbi(s, addr);
4082 gen_probe_access(s, clean_addr, MMU_DATA_LOAD, MO_8);
4083 gen_address_with_allocation_tag0(tcg_rt, addr);
4085 } else {
4086 tcg_rt = cpu_reg_sp(s, rt);
4087 if (!s->ata) {
4089 * For STG and ST2G, we need to check alignment and probe memory.
4090 * TODO: For STZG and STZ2G, we could rely on the stores below,
4091 * at least for system mode; user-only won't enforce alignment.
4093 if (is_pair) {
4094 gen_helper_st2g_stub(cpu_env, addr);
4095 } else {
4096 gen_helper_stg_stub(cpu_env, addr);
4098 } else if (tb_cflags(s->base.tb) & CF_PARALLEL) {
4099 if (is_pair) {
4100 gen_helper_st2g_parallel(cpu_env, addr, tcg_rt);
4101 } else {
4102 gen_helper_stg_parallel(cpu_env, addr, tcg_rt);
4104 } else {
4105 if (is_pair) {
4106 gen_helper_st2g(cpu_env, addr, tcg_rt);
4107 } else {
4108 gen_helper_stg(cpu_env, addr, tcg_rt);
4113 if (is_zero) {
4114 TCGv_i64 clean_addr = clean_data_tbi(s, addr);
4115 TCGv_i64 tcg_zero = tcg_const_i64(0);
4116 int mem_index = get_mem_index(s);
4117 int i, n = (1 + is_pair) << LOG2_TAG_GRANULE;
4119 tcg_gen_qemu_st_i64(tcg_zero, clean_addr, mem_index,
4120 MO_Q | MO_ALIGN_16);
4121 for (i = 8; i < n; i += 8) {
4122 tcg_gen_addi_i64(clean_addr, clean_addr, 8);
4123 tcg_gen_qemu_st_i64(tcg_zero, clean_addr, mem_index, MO_Q);
4125 tcg_temp_free_i64(tcg_zero);
4128 if (index != 0) {
4129 /* pre-index or post-index */
4130 if (index < 0) {
4131 /* post-index */
4132 tcg_gen_addi_i64(addr, addr, offset);
4134 tcg_gen_mov_i64(cpu_reg_sp(s, rn), addr);
4138 /* Loads and stores */
4139 static void disas_ldst(DisasContext *s, uint32_t insn)
4141 switch (extract32(insn, 24, 6)) {
4142 case 0x08: /* Load/store exclusive */
4143 disas_ldst_excl(s, insn);
4144 break;
4145 case 0x18: case 0x1c: /* Load register (literal) */
4146 disas_ld_lit(s, insn);
4147 break;
4148 case 0x28: case 0x29:
4149 case 0x2c: case 0x2d: /* Load/store pair (all forms) */
4150 disas_ldst_pair(s, insn);
4151 break;
4152 case 0x38: case 0x39:
4153 case 0x3c: case 0x3d: /* Load/store register (all forms) */
4154 disas_ldst_reg(s, insn);
4155 break;
4156 case 0x0c: /* AdvSIMD load/store multiple structures */
4157 disas_ldst_multiple_struct(s, insn);
4158 break;
4159 case 0x0d: /* AdvSIMD load/store single structure */
4160 disas_ldst_single_struct(s, insn);
4161 break;
4162 case 0x19:
4163 if (extract32(insn, 21, 1) != 0) {
4164 disas_ldst_tag(s, insn);
4165 } else if (extract32(insn, 10, 2) == 0) {
4166 disas_ldst_ldapr_stlr(s, insn);
4167 } else {
4168 unallocated_encoding(s);
4170 break;
4171 default:
4172 unallocated_encoding(s);
4173 break;
4177 /* PC-rel. addressing
4178 * 31 30 29 28 24 23 5 4 0
4179 * +----+-------+-----------+-------------------+------+
4180 * | op | immlo | 1 0 0 0 0 | immhi | Rd |
4181 * +----+-------+-----------+-------------------+------+
4183 static void disas_pc_rel_adr(DisasContext *s, uint32_t insn)
4185 unsigned int page, rd;
4186 uint64_t base;
4187 uint64_t offset;
4189 page = extract32(insn, 31, 1);
4190 /* SignExtend(immhi:immlo) -> offset */
4191 offset = sextract64(insn, 5, 19);
4192 offset = offset << 2 | extract32(insn, 29, 2);
4193 rd = extract32(insn, 0, 5);
4194 base = s->pc_curr;
4196 if (page) {
4197 /* ADRP (page based) */
4198 base &= ~0xfff;
4199 offset <<= 12;
4202 tcg_gen_movi_i64(cpu_reg(s, rd), base + offset);
4206 * Add/subtract (immediate)
4208 * 31 30 29 28 23 22 21 10 9 5 4 0
4209 * +--+--+--+-------------+--+-------------+-----+-----+
4210 * |sf|op| S| 1 0 0 0 1 0 |sh| imm12 | Rn | Rd |
4211 * +--+--+--+-------------+--+-------------+-----+-----+
4213 * sf: 0 -> 32bit, 1 -> 64bit
4214 * op: 0 -> add , 1 -> sub
4215 * S: 1 -> set flags
4216 * sh: 1 -> LSL imm by 12
4218 static void disas_add_sub_imm(DisasContext *s, uint32_t insn)
4220 int rd = extract32(insn, 0, 5);
4221 int rn = extract32(insn, 5, 5);
4222 uint64_t imm = extract32(insn, 10, 12);
4223 bool shift = extract32(insn, 22, 1);
4224 bool setflags = extract32(insn, 29, 1);
4225 bool sub_op = extract32(insn, 30, 1);
4226 bool is_64bit = extract32(insn, 31, 1);
4228 TCGv_i64 tcg_rn = cpu_reg_sp(s, rn);
4229 TCGv_i64 tcg_rd = setflags ? cpu_reg(s, rd) : cpu_reg_sp(s, rd);
4230 TCGv_i64 tcg_result;
4232 if (shift) {
4233 imm <<= 12;
4236 tcg_result = tcg_temp_new_i64();
4237 if (!setflags) {
4238 if (sub_op) {
4239 tcg_gen_subi_i64(tcg_result, tcg_rn, imm);
4240 } else {
4241 tcg_gen_addi_i64(tcg_result, tcg_rn, imm);
4243 } else {
4244 TCGv_i64 tcg_imm = tcg_const_i64(imm);
4245 if (sub_op) {
4246 gen_sub_CC(is_64bit, tcg_result, tcg_rn, tcg_imm);
4247 } else {
4248 gen_add_CC(is_64bit, tcg_result, tcg_rn, tcg_imm);
4250 tcg_temp_free_i64(tcg_imm);
4253 if (is_64bit) {
4254 tcg_gen_mov_i64(tcg_rd, tcg_result);
4255 } else {
4256 tcg_gen_ext32u_i64(tcg_rd, tcg_result);
4259 tcg_temp_free_i64(tcg_result);
4263 * Add/subtract (immediate, with tags)
4265 * 31 30 29 28 23 22 21 16 14 10 9 5 4 0
4266 * +--+--+--+-------------+--+---------+--+-------+-----+-----+
4267 * |sf|op| S| 1 0 0 0 1 1 |o2| uimm6 |o3| uimm4 | Rn | Rd |
4268 * +--+--+--+-------------+--+---------+--+-------+-----+-----+
4270 * op: 0 -> add, 1 -> sub
4272 static void disas_add_sub_imm_with_tags(DisasContext *s, uint32_t insn)
4274 int rd = extract32(insn, 0, 5);
4275 int rn = extract32(insn, 5, 5);
4276 int uimm4 = extract32(insn, 10, 4);
4277 int uimm6 = extract32(insn, 16, 6);
4278 bool sub_op = extract32(insn, 30, 1);
4279 TCGv_i64 tcg_rn, tcg_rd;
4280 int imm;
4282 /* Test all of sf=1, S=0, o2=0, o3=0. */
4283 if ((insn & 0xa040c000u) != 0x80000000u ||
4284 !dc_isar_feature(aa64_mte_insn_reg, s)) {
4285 unallocated_encoding(s);
4286 return;
4289 imm = uimm6 << LOG2_TAG_GRANULE;
4290 if (sub_op) {
4291 imm = -imm;
4294 tcg_rn = cpu_reg_sp(s, rn);
4295 tcg_rd = cpu_reg_sp(s, rd);
4297 if (s->ata) {
4298 TCGv_i32 offset = tcg_const_i32(imm);
4299 TCGv_i32 tag_offset = tcg_const_i32(uimm4);
4301 gen_helper_addsubg(tcg_rd, cpu_env, tcg_rn, offset, tag_offset);
4302 tcg_temp_free_i32(tag_offset);
4303 tcg_temp_free_i32(offset);
4304 } else {
4305 tcg_gen_addi_i64(tcg_rd, tcg_rn, imm);
4306 gen_address_with_allocation_tag0(tcg_rd, tcg_rd);
4310 /* The input should be a value in the bottom e bits (with higher
4311 * bits zero); returns that value replicated into every element
4312 * of size e in a 64 bit integer.
4314 static uint64_t bitfield_replicate(uint64_t mask, unsigned int e)
4316 assert(e != 0);
4317 while (e < 64) {
4318 mask |= mask << e;
4319 e *= 2;
4321 return mask;
4324 /* Return a value with the bottom len bits set (where 0 < len <= 64) */
4325 static inline uint64_t bitmask64(unsigned int length)
4327 assert(length > 0 && length <= 64);
4328 return ~0ULL >> (64 - length);
4331 /* Simplified variant of pseudocode DecodeBitMasks() for the case where we
4332 * only require the wmask. Returns false if the imms/immr/immn are a reserved
4333 * value (ie should cause a guest UNDEF exception), and true if they are
4334 * valid, in which case the decoded bit pattern is written to result.
4336 bool logic_imm_decode_wmask(uint64_t *result, unsigned int immn,
4337 unsigned int imms, unsigned int immr)
4339 uint64_t mask;
4340 unsigned e, levels, s, r;
4341 int len;
4343 assert(immn < 2 && imms < 64 && immr < 64);
4345 /* The bit patterns we create here are 64 bit patterns which
4346 * are vectors of identical elements of size e = 2, 4, 8, 16, 32 or
4347 * 64 bits each. Each element contains the same value: a run
4348 * of between 1 and e-1 non-zero bits, rotated within the
4349 * element by between 0 and e-1 bits.
4351 * The element size and run length are encoded into immn (1 bit)
4352 * and imms (6 bits) as follows:
4353 * 64 bit elements: immn = 1, imms = <length of run - 1>
4354 * 32 bit elements: immn = 0, imms = 0 : <length of run - 1>
4355 * 16 bit elements: immn = 0, imms = 10 : <length of run - 1>
4356 * 8 bit elements: immn = 0, imms = 110 : <length of run - 1>
4357 * 4 bit elements: immn = 0, imms = 1110 : <length of run - 1>
4358 * 2 bit elements: immn = 0, imms = 11110 : <length of run - 1>
4359 * Notice that immn = 0, imms = 11111x is the only combination
4360 * not covered by one of the above options; this is reserved.
4361 * Further, <length of run - 1> all-ones is a reserved pattern.
4363 * In all cases the rotation is by immr % e (and immr is 6 bits).
4366 /* First determine the element size */
4367 len = 31 - clz32((immn << 6) | (~imms & 0x3f));
4368 if (len < 1) {
4369 /* This is the immn == 0, imms == 0x11111x case */
4370 return false;
4372 e = 1 << len;
4374 levels = e - 1;
4375 s = imms & levels;
4376 r = immr & levels;
4378 if (s == levels) {
4379 /* <length of run - 1> mustn't be all-ones. */
4380 return false;
4383 /* Create the value of one element: s+1 set bits rotated
4384 * by r within the element (which is e bits wide)...
4386 mask = bitmask64(s + 1);
4387 if (r) {
4388 mask = (mask >> r) | (mask << (e - r));
4389 mask &= bitmask64(e);
4391 /* ...then replicate the element over the whole 64 bit value */
4392 mask = bitfield_replicate(mask, e);
4393 *result = mask;
4394 return true;
4397 /* Logical (immediate)
4398 * 31 30 29 28 23 22 21 16 15 10 9 5 4 0
4399 * +----+-----+-------------+---+------+------+------+------+
4400 * | sf | opc | 1 0 0 1 0 0 | N | immr | imms | Rn | Rd |
4401 * +----+-----+-------------+---+------+------+------+------+
4403 static void disas_logic_imm(DisasContext *s, uint32_t insn)
4405 unsigned int sf, opc, is_n, immr, imms, rn, rd;
4406 TCGv_i64 tcg_rd, tcg_rn;
4407 uint64_t wmask;
4408 bool is_and = false;
4410 sf = extract32(insn, 31, 1);
4411 opc = extract32(insn, 29, 2);
4412 is_n = extract32(insn, 22, 1);
4413 immr = extract32(insn, 16, 6);
4414 imms = extract32(insn, 10, 6);
4415 rn = extract32(insn, 5, 5);
4416 rd = extract32(insn, 0, 5);
4418 if (!sf && is_n) {
4419 unallocated_encoding(s);
4420 return;
4423 if (opc == 0x3) { /* ANDS */
4424 tcg_rd = cpu_reg(s, rd);
4425 } else {
4426 tcg_rd = cpu_reg_sp(s, rd);
4428 tcg_rn = cpu_reg(s, rn);
4430 if (!logic_imm_decode_wmask(&wmask, is_n, imms, immr)) {
4431 /* some immediate field values are reserved */
4432 unallocated_encoding(s);
4433 return;
4436 if (!sf) {
4437 wmask &= 0xffffffff;
4440 switch (opc) {
4441 case 0x3: /* ANDS */
4442 case 0x0: /* AND */
4443 tcg_gen_andi_i64(tcg_rd, tcg_rn, wmask);
4444 is_and = true;
4445 break;
4446 case 0x1: /* ORR */
4447 tcg_gen_ori_i64(tcg_rd, tcg_rn, wmask);
4448 break;
4449 case 0x2: /* EOR */
4450 tcg_gen_xori_i64(tcg_rd, tcg_rn, wmask);
4451 break;
4452 default:
4453 assert(FALSE); /* must handle all above */
4454 break;
4457 if (!sf && !is_and) {
4458 /* zero extend final result; we know we can skip this for AND
4459 * since the immediate had the high 32 bits clear.
4461 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4464 if (opc == 3) { /* ANDS */
4465 gen_logic_CC(sf, tcg_rd);
4470 * Move wide (immediate)
4472 * 31 30 29 28 23 22 21 20 5 4 0
4473 * +--+-----+-------------+-----+----------------+------+
4474 * |sf| opc | 1 0 0 1 0 1 | hw | imm16 | Rd |
4475 * +--+-----+-------------+-----+----------------+------+
4477 * sf: 0 -> 32 bit, 1 -> 64 bit
4478 * opc: 00 -> N, 10 -> Z, 11 -> K
4479 * hw: shift/16 (0,16, and sf only 32, 48)
4481 static void disas_movw_imm(DisasContext *s, uint32_t insn)
4483 int rd = extract32(insn, 0, 5);
4484 uint64_t imm = extract32(insn, 5, 16);
4485 int sf = extract32(insn, 31, 1);
4486 int opc = extract32(insn, 29, 2);
4487 int pos = extract32(insn, 21, 2) << 4;
4488 TCGv_i64 tcg_rd = cpu_reg(s, rd);
4489 TCGv_i64 tcg_imm;
4491 if (!sf && (pos >= 32)) {
4492 unallocated_encoding(s);
4493 return;
4496 switch (opc) {
4497 case 0: /* MOVN */
4498 case 2: /* MOVZ */
4499 imm <<= pos;
4500 if (opc == 0) {
4501 imm = ~imm;
4503 if (!sf) {
4504 imm &= 0xffffffffu;
4506 tcg_gen_movi_i64(tcg_rd, imm);
4507 break;
4508 case 3: /* MOVK */
4509 tcg_imm = tcg_const_i64(imm);
4510 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_imm, pos, 16);
4511 tcg_temp_free_i64(tcg_imm);
4512 if (!sf) {
4513 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4515 break;
4516 default:
4517 unallocated_encoding(s);
4518 break;
4522 /* Bitfield
4523 * 31 30 29 28 23 22 21 16 15 10 9 5 4 0
4524 * +----+-----+-------------+---+------+------+------+------+
4525 * | sf | opc | 1 0 0 1 1 0 | N | immr | imms | Rn | Rd |
4526 * +----+-----+-------------+---+------+------+------+------+
4528 static void disas_bitfield(DisasContext *s, uint32_t insn)
4530 unsigned int sf, n, opc, ri, si, rn, rd, bitsize, pos, len;
4531 TCGv_i64 tcg_rd, tcg_tmp;
4533 sf = extract32(insn, 31, 1);
4534 opc = extract32(insn, 29, 2);
4535 n = extract32(insn, 22, 1);
4536 ri = extract32(insn, 16, 6);
4537 si = extract32(insn, 10, 6);
4538 rn = extract32(insn, 5, 5);
4539 rd = extract32(insn, 0, 5);
4540 bitsize = sf ? 64 : 32;
4542 if (sf != n || ri >= bitsize || si >= bitsize || opc > 2) {
4543 unallocated_encoding(s);
4544 return;
4547 tcg_rd = cpu_reg(s, rd);
4549 /* Suppress the zero-extend for !sf. Since RI and SI are constrained
4550 to be smaller than bitsize, we'll never reference data outside the
4551 low 32-bits anyway. */
4552 tcg_tmp = read_cpu_reg(s, rn, 1);
4554 /* Recognize simple(r) extractions. */
4555 if (si >= ri) {
4556 /* Wd<s-r:0> = Wn<s:r> */
4557 len = (si - ri) + 1;
4558 if (opc == 0) { /* SBFM: ASR, SBFX, SXTB, SXTH, SXTW */
4559 tcg_gen_sextract_i64(tcg_rd, tcg_tmp, ri, len);
4560 goto done;
4561 } else if (opc == 2) { /* UBFM: UBFX, LSR, UXTB, UXTH */
4562 tcg_gen_extract_i64(tcg_rd, tcg_tmp, ri, len);
4563 return;
4565 /* opc == 1, BFXIL fall through to deposit */
4566 tcg_gen_shri_i64(tcg_tmp, tcg_tmp, ri);
4567 pos = 0;
4568 } else {
4569 /* Handle the ri > si case with a deposit
4570 * Wd<32+s-r,32-r> = Wn<s:0>
4572 len = si + 1;
4573 pos = (bitsize - ri) & (bitsize - 1);
4576 if (opc == 0 && len < ri) {
4577 /* SBFM: sign extend the destination field from len to fill
4578 the balance of the word. Let the deposit below insert all
4579 of those sign bits. */
4580 tcg_gen_sextract_i64(tcg_tmp, tcg_tmp, 0, len);
4581 len = ri;
4584 if (opc == 1) { /* BFM, BFXIL */
4585 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, pos, len);
4586 } else {
4587 /* SBFM or UBFM: We start with zero, and we haven't modified
4588 any bits outside bitsize, therefore the zero-extension
4589 below is unneeded. */
4590 tcg_gen_deposit_z_i64(tcg_rd, tcg_tmp, pos, len);
4591 return;
4594 done:
4595 if (!sf) { /* zero extend final result */
4596 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4600 /* Extract
4601 * 31 30 29 28 23 22 21 20 16 15 10 9 5 4 0
4602 * +----+------+-------------+---+----+------+--------+------+------+
4603 * | sf | op21 | 1 0 0 1 1 1 | N | o0 | Rm | imms | Rn | Rd |
4604 * +----+------+-------------+---+----+------+--------+------+------+
4606 static void disas_extract(DisasContext *s, uint32_t insn)
4608 unsigned int sf, n, rm, imm, rn, rd, bitsize, op21, op0;
4610 sf = extract32(insn, 31, 1);
4611 n = extract32(insn, 22, 1);
4612 rm = extract32(insn, 16, 5);
4613 imm = extract32(insn, 10, 6);
4614 rn = extract32(insn, 5, 5);
4615 rd = extract32(insn, 0, 5);
4616 op21 = extract32(insn, 29, 2);
4617 op0 = extract32(insn, 21, 1);
4618 bitsize = sf ? 64 : 32;
4620 if (sf != n || op21 || op0 || imm >= bitsize) {
4621 unallocated_encoding(s);
4622 } else {
4623 TCGv_i64 tcg_rd, tcg_rm, tcg_rn;
4625 tcg_rd = cpu_reg(s, rd);
4627 if (unlikely(imm == 0)) {
4628 /* tcg shl_i32/shl_i64 is undefined for 32/64 bit shifts,
4629 * so an extract from bit 0 is a special case.
4631 if (sf) {
4632 tcg_gen_mov_i64(tcg_rd, cpu_reg(s, rm));
4633 } else {
4634 tcg_gen_ext32u_i64(tcg_rd, cpu_reg(s, rm));
4636 } else {
4637 tcg_rm = cpu_reg(s, rm);
4638 tcg_rn = cpu_reg(s, rn);
4640 if (sf) {
4641 /* Specialization to ROR happens in EXTRACT2. */
4642 tcg_gen_extract2_i64(tcg_rd, tcg_rm, tcg_rn, imm);
4643 } else {
4644 TCGv_i32 t0 = tcg_temp_new_i32();
4646 tcg_gen_extrl_i64_i32(t0, tcg_rm);
4647 if (rm == rn) {
4648 tcg_gen_rotri_i32(t0, t0, imm);
4649 } else {
4650 TCGv_i32 t1 = tcg_temp_new_i32();
4651 tcg_gen_extrl_i64_i32(t1, tcg_rn);
4652 tcg_gen_extract2_i32(t0, t0, t1, imm);
4653 tcg_temp_free_i32(t1);
4655 tcg_gen_extu_i32_i64(tcg_rd, t0);
4656 tcg_temp_free_i32(t0);
4662 /* Data processing - immediate */
4663 static void disas_data_proc_imm(DisasContext *s, uint32_t insn)
4665 switch (extract32(insn, 23, 6)) {
4666 case 0x20: case 0x21: /* PC-rel. addressing */
4667 disas_pc_rel_adr(s, insn);
4668 break;
4669 case 0x22: /* Add/subtract (immediate) */
4670 disas_add_sub_imm(s, insn);
4671 break;
4672 case 0x23: /* Add/subtract (immediate, with tags) */
4673 disas_add_sub_imm_with_tags(s, insn);
4674 break;
4675 case 0x24: /* Logical (immediate) */
4676 disas_logic_imm(s, insn);
4677 break;
4678 case 0x25: /* Move wide (immediate) */
4679 disas_movw_imm(s, insn);
4680 break;
4681 case 0x26: /* Bitfield */
4682 disas_bitfield(s, insn);
4683 break;
4684 case 0x27: /* Extract */
4685 disas_extract(s, insn);
4686 break;
4687 default:
4688 unallocated_encoding(s);
4689 break;
4693 /* Shift a TCGv src by TCGv shift_amount, put result in dst.
4694 * Note that it is the caller's responsibility to ensure that the
4695 * shift amount is in range (ie 0..31 or 0..63) and provide the ARM
4696 * mandated semantics for out of range shifts.
4698 static void shift_reg(TCGv_i64 dst, TCGv_i64 src, int sf,
4699 enum a64_shift_type shift_type, TCGv_i64 shift_amount)
4701 switch (shift_type) {
4702 case A64_SHIFT_TYPE_LSL:
4703 tcg_gen_shl_i64(dst, src, shift_amount);
4704 break;
4705 case A64_SHIFT_TYPE_LSR:
4706 tcg_gen_shr_i64(dst, src, shift_amount);
4707 break;
4708 case A64_SHIFT_TYPE_ASR:
4709 if (!sf) {
4710 tcg_gen_ext32s_i64(dst, src);
4712 tcg_gen_sar_i64(dst, sf ? src : dst, shift_amount);
4713 break;
4714 case A64_SHIFT_TYPE_ROR:
4715 if (sf) {
4716 tcg_gen_rotr_i64(dst, src, shift_amount);
4717 } else {
4718 TCGv_i32 t0, t1;
4719 t0 = tcg_temp_new_i32();
4720 t1 = tcg_temp_new_i32();
4721 tcg_gen_extrl_i64_i32(t0, src);
4722 tcg_gen_extrl_i64_i32(t1, shift_amount);
4723 tcg_gen_rotr_i32(t0, t0, t1);
4724 tcg_gen_extu_i32_i64(dst, t0);
4725 tcg_temp_free_i32(t0);
4726 tcg_temp_free_i32(t1);
4728 break;
4729 default:
4730 assert(FALSE); /* all shift types should be handled */
4731 break;
4734 if (!sf) { /* zero extend final result */
4735 tcg_gen_ext32u_i64(dst, dst);
4739 /* Shift a TCGv src by immediate, put result in dst.
4740 * The shift amount must be in range (this should always be true as the
4741 * relevant instructions will UNDEF on bad shift immediates).
4743 static void shift_reg_imm(TCGv_i64 dst, TCGv_i64 src, int sf,
4744 enum a64_shift_type shift_type, unsigned int shift_i)
4746 assert(shift_i < (sf ? 64 : 32));
4748 if (shift_i == 0) {
4749 tcg_gen_mov_i64(dst, src);
4750 } else {
4751 TCGv_i64 shift_const;
4753 shift_const = tcg_const_i64(shift_i);
4754 shift_reg(dst, src, sf, shift_type, shift_const);
4755 tcg_temp_free_i64(shift_const);
4759 /* Logical (shifted register)
4760 * 31 30 29 28 24 23 22 21 20 16 15 10 9 5 4 0
4761 * +----+-----+-----------+-------+---+------+--------+------+------+
4762 * | sf | opc | 0 1 0 1 0 | shift | N | Rm | imm6 | Rn | Rd |
4763 * +----+-----+-----------+-------+---+------+--------+------+------+
4765 static void disas_logic_reg(DisasContext *s, uint32_t insn)
4767 TCGv_i64 tcg_rd, tcg_rn, tcg_rm;
4768 unsigned int sf, opc, shift_type, invert, rm, shift_amount, rn, rd;
4770 sf = extract32(insn, 31, 1);
4771 opc = extract32(insn, 29, 2);
4772 shift_type = extract32(insn, 22, 2);
4773 invert = extract32(insn, 21, 1);
4774 rm = extract32(insn, 16, 5);
4775 shift_amount = extract32(insn, 10, 6);
4776 rn = extract32(insn, 5, 5);
4777 rd = extract32(insn, 0, 5);
4779 if (!sf && (shift_amount & (1 << 5))) {
4780 unallocated_encoding(s);
4781 return;
4784 tcg_rd = cpu_reg(s, rd);
4786 if (opc == 1 && shift_amount == 0 && shift_type == 0 && rn == 31) {
4787 /* Unshifted ORR and ORN with WZR/XZR is the standard encoding for
4788 * register-register MOV and MVN, so it is worth special casing.
4790 tcg_rm = cpu_reg(s, rm);
4791 if (invert) {
4792 tcg_gen_not_i64(tcg_rd, tcg_rm);
4793 if (!sf) {
4794 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4796 } else {
4797 if (sf) {
4798 tcg_gen_mov_i64(tcg_rd, tcg_rm);
4799 } else {
4800 tcg_gen_ext32u_i64(tcg_rd, tcg_rm);
4803 return;
4806 tcg_rm = read_cpu_reg(s, rm, sf);
4808 if (shift_amount) {
4809 shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, shift_amount);
4812 tcg_rn = cpu_reg(s, rn);
4814 switch (opc | (invert << 2)) {
4815 case 0: /* AND */
4816 case 3: /* ANDS */
4817 tcg_gen_and_i64(tcg_rd, tcg_rn, tcg_rm);
4818 break;
4819 case 1: /* ORR */
4820 tcg_gen_or_i64(tcg_rd, tcg_rn, tcg_rm);
4821 break;
4822 case 2: /* EOR */
4823 tcg_gen_xor_i64(tcg_rd, tcg_rn, tcg_rm);
4824 break;
4825 case 4: /* BIC */
4826 case 7: /* BICS */
4827 tcg_gen_andc_i64(tcg_rd, tcg_rn, tcg_rm);
4828 break;
4829 case 5: /* ORN */
4830 tcg_gen_orc_i64(tcg_rd, tcg_rn, tcg_rm);
4831 break;
4832 case 6: /* EON */
4833 tcg_gen_eqv_i64(tcg_rd, tcg_rn, tcg_rm);
4834 break;
4835 default:
4836 assert(FALSE);
4837 break;
4840 if (!sf) {
4841 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4844 if (opc == 3) {
4845 gen_logic_CC(sf, tcg_rd);
4850 * Add/subtract (extended register)
4852 * 31|30|29|28 24|23 22|21|20 16|15 13|12 10|9 5|4 0|
4853 * +--+--+--+-----------+-----+--+-------+------+------+----+----+
4854 * |sf|op| S| 0 1 0 1 1 | opt | 1| Rm |option| imm3 | Rn | Rd |
4855 * +--+--+--+-----------+-----+--+-------+------+------+----+----+
4857 * sf: 0 -> 32bit, 1 -> 64bit
4858 * op: 0 -> add , 1 -> sub
4859 * S: 1 -> set flags
4860 * opt: 00
4861 * option: extension type (see DecodeRegExtend)
4862 * imm3: optional shift to Rm
4864 * Rd = Rn + LSL(extend(Rm), amount)
4866 static void disas_add_sub_ext_reg(DisasContext *s, uint32_t insn)
4868 int rd = extract32(insn, 0, 5);
4869 int rn = extract32(insn, 5, 5);
4870 int imm3 = extract32(insn, 10, 3);
4871 int option = extract32(insn, 13, 3);
4872 int rm = extract32(insn, 16, 5);
4873 int opt = extract32(insn, 22, 2);
4874 bool setflags = extract32(insn, 29, 1);
4875 bool sub_op = extract32(insn, 30, 1);
4876 bool sf = extract32(insn, 31, 1);
4878 TCGv_i64 tcg_rm, tcg_rn; /* temps */
4879 TCGv_i64 tcg_rd;
4880 TCGv_i64 tcg_result;
4882 if (imm3 > 4 || opt != 0) {
4883 unallocated_encoding(s);
4884 return;
4887 /* non-flag setting ops may use SP */
4888 if (!setflags) {
4889 tcg_rd = cpu_reg_sp(s, rd);
4890 } else {
4891 tcg_rd = cpu_reg(s, rd);
4893 tcg_rn = read_cpu_reg_sp(s, rn, sf);
4895 tcg_rm = read_cpu_reg(s, rm, sf);
4896 ext_and_shift_reg(tcg_rm, tcg_rm, option, imm3);
4898 tcg_result = tcg_temp_new_i64();
4900 if (!setflags) {
4901 if (sub_op) {
4902 tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
4903 } else {
4904 tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
4906 } else {
4907 if (sub_op) {
4908 gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
4909 } else {
4910 gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
4914 if (sf) {
4915 tcg_gen_mov_i64(tcg_rd, tcg_result);
4916 } else {
4917 tcg_gen_ext32u_i64(tcg_rd, tcg_result);
4920 tcg_temp_free_i64(tcg_result);
4924 * Add/subtract (shifted register)
4926 * 31 30 29 28 24 23 22 21 20 16 15 10 9 5 4 0
4927 * +--+--+--+-----------+-----+--+-------+---------+------+------+
4928 * |sf|op| S| 0 1 0 1 1 |shift| 0| Rm | imm6 | Rn | Rd |
4929 * +--+--+--+-----------+-----+--+-------+---------+------+------+
4931 * sf: 0 -> 32bit, 1 -> 64bit
4932 * op: 0 -> add , 1 -> sub
4933 * S: 1 -> set flags
4934 * shift: 00 -> LSL, 01 -> LSR, 10 -> ASR, 11 -> RESERVED
4935 * imm6: Shift amount to apply to Rm before the add/sub
4937 static void disas_add_sub_reg(DisasContext *s, uint32_t insn)
4939 int rd = extract32(insn, 0, 5);
4940 int rn = extract32(insn, 5, 5);
4941 int imm6 = extract32(insn, 10, 6);
4942 int rm = extract32(insn, 16, 5);
4943 int shift_type = extract32(insn, 22, 2);
4944 bool setflags = extract32(insn, 29, 1);
4945 bool sub_op = extract32(insn, 30, 1);
4946 bool sf = extract32(insn, 31, 1);
4948 TCGv_i64 tcg_rd = cpu_reg(s, rd);
4949 TCGv_i64 tcg_rn, tcg_rm;
4950 TCGv_i64 tcg_result;
4952 if ((shift_type == 3) || (!sf && (imm6 > 31))) {
4953 unallocated_encoding(s);
4954 return;
4957 tcg_rn = read_cpu_reg(s, rn, sf);
4958 tcg_rm = read_cpu_reg(s, rm, sf);
4960 shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, imm6);
4962 tcg_result = tcg_temp_new_i64();
4964 if (!setflags) {
4965 if (sub_op) {
4966 tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
4967 } else {
4968 tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
4970 } else {
4971 if (sub_op) {
4972 gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
4973 } else {
4974 gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
4978 if (sf) {
4979 tcg_gen_mov_i64(tcg_rd, tcg_result);
4980 } else {
4981 tcg_gen_ext32u_i64(tcg_rd, tcg_result);
4984 tcg_temp_free_i64(tcg_result);
4987 /* Data-processing (3 source)
4989 * 31 30 29 28 24 23 21 20 16 15 14 10 9 5 4 0
4990 * +--+------+-----------+------+------+----+------+------+------+
4991 * |sf| op54 | 1 1 0 1 1 | op31 | Rm | o0 | Ra | Rn | Rd |
4992 * +--+------+-----------+------+------+----+------+------+------+
4994 static void disas_data_proc_3src(DisasContext *s, uint32_t insn)
4996 int rd = extract32(insn, 0, 5);
4997 int rn = extract32(insn, 5, 5);
4998 int ra = extract32(insn, 10, 5);
4999 int rm = extract32(insn, 16, 5);
5000 int op_id = (extract32(insn, 29, 3) << 4) |
5001 (extract32(insn, 21, 3) << 1) |
5002 extract32(insn, 15, 1);
5003 bool sf = extract32(insn, 31, 1);
5004 bool is_sub = extract32(op_id, 0, 1);
5005 bool is_high = extract32(op_id, 2, 1);
5006 bool is_signed = false;
5007 TCGv_i64 tcg_op1;
5008 TCGv_i64 tcg_op2;
5009 TCGv_i64 tcg_tmp;
5011 /* Note that op_id is sf:op54:op31:o0 so it includes the 32/64 size flag */
5012 switch (op_id) {
5013 case 0x42: /* SMADDL */
5014 case 0x43: /* SMSUBL */
5015 case 0x44: /* SMULH */
5016 is_signed = true;
5017 break;
5018 case 0x0: /* MADD (32bit) */
5019 case 0x1: /* MSUB (32bit) */
5020 case 0x40: /* MADD (64bit) */
5021 case 0x41: /* MSUB (64bit) */
5022 case 0x4a: /* UMADDL */
5023 case 0x4b: /* UMSUBL */
5024 case 0x4c: /* UMULH */
5025 break;
5026 default:
5027 unallocated_encoding(s);
5028 return;
5031 if (is_high) {
5032 TCGv_i64 low_bits = tcg_temp_new_i64(); /* low bits discarded */
5033 TCGv_i64 tcg_rd = cpu_reg(s, rd);
5034 TCGv_i64 tcg_rn = cpu_reg(s, rn);
5035 TCGv_i64 tcg_rm = cpu_reg(s, rm);
5037 if (is_signed) {
5038 tcg_gen_muls2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
5039 } else {
5040 tcg_gen_mulu2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
5043 tcg_temp_free_i64(low_bits);
5044 return;
5047 tcg_op1 = tcg_temp_new_i64();
5048 tcg_op2 = tcg_temp_new_i64();
5049 tcg_tmp = tcg_temp_new_i64();
5051 if (op_id < 0x42) {
5052 tcg_gen_mov_i64(tcg_op1, cpu_reg(s, rn));
5053 tcg_gen_mov_i64(tcg_op2, cpu_reg(s, rm));
5054 } else {
5055 if (is_signed) {
5056 tcg_gen_ext32s_i64(tcg_op1, cpu_reg(s, rn));
5057 tcg_gen_ext32s_i64(tcg_op2, cpu_reg(s, rm));
5058 } else {
5059 tcg_gen_ext32u_i64(tcg_op1, cpu_reg(s, rn));
5060 tcg_gen_ext32u_i64(tcg_op2, cpu_reg(s, rm));
5064 if (ra == 31 && !is_sub) {
5065 /* Special-case MADD with rA == XZR; it is the standard MUL alias */
5066 tcg_gen_mul_i64(cpu_reg(s, rd), tcg_op1, tcg_op2);
5067 } else {
5068 tcg_gen_mul_i64(tcg_tmp, tcg_op1, tcg_op2);
5069 if (is_sub) {
5070 tcg_gen_sub_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
5071 } else {
5072 tcg_gen_add_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
5076 if (!sf) {
5077 tcg_gen_ext32u_i64(cpu_reg(s, rd), cpu_reg(s, rd));
5080 tcg_temp_free_i64(tcg_op1);
5081 tcg_temp_free_i64(tcg_op2);
5082 tcg_temp_free_i64(tcg_tmp);
5085 /* Add/subtract (with carry)
5086 * 31 30 29 28 27 26 25 24 23 22 21 20 16 15 10 9 5 4 0
5087 * +--+--+--+------------------------+------+-------------+------+-----+
5088 * |sf|op| S| 1 1 0 1 0 0 0 0 | rm | 0 0 0 0 0 0 | Rn | Rd |
5089 * +--+--+--+------------------------+------+-------------+------+-----+
5092 static void disas_adc_sbc(DisasContext *s, uint32_t insn)
5094 unsigned int sf, op, setflags, rm, rn, rd;
5095 TCGv_i64 tcg_y, tcg_rn, tcg_rd;
5097 sf = extract32(insn, 31, 1);
5098 op = extract32(insn, 30, 1);
5099 setflags = extract32(insn, 29, 1);
5100 rm = extract32(insn, 16, 5);
5101 rn = extract32(insn, 5, 5);
5102 rd = extract32(insn, 0, 5);
5104 tcg_rd = cpu_reg(s, rd);
5105 tcg_rn = cpu_reg(s, rn);
5107 if (op) {
5108 tcg_y = new_tmp_a64(s);
5109 tcg_gen_not_i64(tcg_y, cpu_reg(s, rm));
5110 } else {
5111 tcg_y = cpu_reg(s, rm);
5114 if (setflags) {
5115 gen_adc_CC(sf, tcg_rd, tcg_rn, tcg_y);
5116 } else {
5117 gen_adc(sf, tcg_rd, tcg_rn, tcg_y);
5122 * Rotate right into flags
5123 * 31 30 29 21 15 10 5 4 0
5124 * +--+--+--+-----------------+--------+-----------+------+--+------+
5125 * |sf|op| S| 1 1 0 1 0 0 0 0 | imm6 | 0 0 0 0 1 | Rn |o2| mask |
5126 * +--+--+--+-----------------+--------+-----------+------+--+------+
5128 static void disas_rotate_right_into_flags(DisasContext *s, uint32_t insn)
5130 int mask = extract32(insn, 0, 4);
5131 int o2 = extract32(insn, 4, 1);
5132 int rn = extract32(insn, 5, 5);
5133 int imm6 = extract32(insn, 15, 6);
5134 int sf_op_s = extract32(insn, 29, 3);
5135 TCGv_i64 tcg_rn;
5136 TCGv_i32 nzcv;
5138 if (sf_op_s != 5 || o2 != 0 || !dc_isar_feature(aa64_condm_4, s)) {
5139 unallocated_encoding(s);
5140 return;
5143 tcg_rn = read_cpu_reg(s, rn, 1);
5144 tcg_gen_rotri_i64(tcg_rn, tcg_rn, imm6);
5146 nzcv = tcg_temp_new_i32();
5147 tcg_gen_extrl_i64_i32(nzcv, tcg_rn);
5149 if (mask & 8) { /* N */
5150 tcg_gen_shli_i32(cpu_NF, nzcv, 31 - 3);
5152 if (mask & 4) { /* Z */
5153 tcg_gen_not_i32(cpu_ZF, nzcv);
5154 tcg_gen_andi_i32(cpu_ZF, cpu_ZF, 4);
5156 if (mask & 2) { /* C */
5157 tcg_gen_extract_i32(cpu_CF, nzcv, 1, 1);
5159 if (mask & 1) { /* V */
5160 tcg_gen_shli_i32(cpu_VF, nzcv, 31 - 0);
5163 tcg_temp_free_i32(nzcv);
5167 * Evaluate into flags
5168 * 31 30 29 21 15 14 10 5 4 0
5169 * +--+--+--+-----------------+---------+----+---------+------+--+------+
5170 * |sf|op| S| 1 1 0 1 0 0 0 0 | opcode2 | sz | 0 0 1 0 | Rn |o3| mask |
5171 * +--+--+--+-----------------+---------+----+---------+------+--+------+
5173 static void disas_evaluate_into_flags(DisasContext *s, uint32_t insn)
5175 int o3_mask = extract32(insn, 0, 5);
5176 int rn = extract32(insn, 5, 5);
5177 int o2 = extract32(insn, 15, 6);
5178 int sz = extract32(insn, 14, 1);
5179 int sf_op_s = extract32(insn, 29, 3);
5180 TCGv_i32 tmp;
5181 int shift;
5183 if (sf_op_s != 1 || o2 != 0 || o3_mask != 0xd ||
5184 !dc_isar_feature(aa64_condm_4, s)) {
5185 unallocated_encoding(s);
5186 return;
5188 shift = sz ? 16 : 24; /* SETF16 or SETF8 */
5190 tmp = tcg_temp_new_i32();
5191 tcg_gen_extrl_i64_i32(tmp, cpu_reg(s, rn));
5192 tcg_gen_shli_i32(cpu_NF, tmp, shift);
5193 tcg_gen_shli_i32(cpu_VF, tmp, shift - 1);
5194 tcg_gen_mov_i32(cpu_ZF, cpu_NF);
5195 tcg_gen_xor_i32(cpu_VF, cpu_VF, cpu_NF);
5196 tcg_temp_free_i32(tmp);
5199 /* Conditional compare (immediate / register)
5200 * 31 30 29 28 27 26 25 24 23 22 21 20 16 15 12 11 10 9 5 4 3 0
5201 * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
5202 * |sf|op| S| 1 1 0 1 0 0 1 0 |imm5/rm | cond |i/r |o2| Rn |o3|nzcv |
5203 * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
5204 * [1] y [0] [0]
5206 static void disas_cc(DisasContext *s, uint32_t insn)
5208 unsigned int sf, op, y, cond, rn, nzcv, is_imm;
5209 TCGv_i32 tcg_t0, tcg_t1, tcg_t2;
5210 TCGv_i64 tcg_tmp, tcg_y, tcg_rn;
5211 DisasCompare c;
5213 if (!extract32(insn, 29, 1)) {
5214 unallocated_encoding(s);
5215 return;
5217 if (insn & (1 << 10 | 1 << 4)) {
5218 unallocated_encoding(s);
5219 return;
5221 sf = extract32(insn, 31, 1);
5222 op = extract32(insn, 30, 1);
5223 is_imm = extract32(insn, 11, 1);
5224 y = extract32(insn, 16, 5); /* y = rm (reg) or imm5 (imm) */
5225 cond = extract32(insn, 12, 4);
5226 rn = extract32(insn, 5, 5);
5227 nzcv = extract32(insn, 0, 4);
5229 /* Set T0 = !COND. */
5230 tcg_t0 = tcg_temp_new_i32();
5231 arm_test_cc(&c, cond);
5232 tcg_gen_setcondi_i32(tcg_invert_cond(c.cond), tcg_t0, c.value, 0);
5233 arm_free_cc(&c);
5235 /* Load the arguments for the new comparison. */
5236 if (is_imm) {
5237 tcg_y = new_tmp_a64(s);
5238 tcg_gen_movi_i64(tcg_y, y);
5239 } else {
5240 tcg_y = cpu_reg(s, y);
5242 tcg_rn = cpu_reg(s, rn);
5244 /* Set the flags for the new comparison. */
5245 tcg_tmp = tcg_temp_new_i64();
5246 if (op) {
5247 gen_sub_CC(sf, tcg_tmp, tcg_rn, tcg_y);
5248 } else {
5249 gen_add_CC(sf, tcg_tmp, tcg_rn, tcg_y);
5251 tcg_temp_free_i64(tcg_tmp);
5253 /* If COND was false, force the flags to #nzcv. Compute two masks
5254 * to help with this: T1 = (COND ? 0 : -1), T2 = (COND ? -1 : 0).
5255 * For tcg hosts that support ANDC, we can make do with just T1.
5256 * In either case, allow the tcg optimizer to delete any unused mask.
5258 tcg_t1 = tcg_temp_new_i32();
5259 tcg_t2 = tcg_temp_new_i32();
5260 tcg_gen_neg_i32(tcg_t1, tcg_t0);
5261 tcg_gen_subi_i32(tcg_t2, tcg_t0, 1);
5263 if (nzcv & 8) { /* N */
5264 tcg_gen_or_i32(cpu_NF, cpu_NF, tcg_t1);
5265 } else {
5266 if (TCG_TARGET_HAS_andc_i32) {
5267 tcg_gen_andc_i32(cpu_NF, cpu_NF, tcg_t1);
5268 } else {
5269 tcg_gen_and_i32(cpu_NF, cpu_NF, tcg_t2);
5272 if (nzcv & 4) { /* Z */
5273 if (TCG_TARGET_HAS_andc_i32) {
5274 tcg_gen_andc_i32(cpu_ZF, cpu_ZF, tcg_t1);
5275 } else {
5276 tcg_gen_and_i32(cpu_ZF, cpu_ZF, tcg_t2);
5278 } else {
5279 tcg_gen_or_i32(cpu_ZF, cpu_ZF, tcg_t0);
5281 if (nzcv & 2) { /* C */
5282 tcg_gen_or_i32(cpu_CF, cpu_CF, tcg_t0);
5283 } else {
5284 if (TCG_TARGET_HAS_andc_i32) {
5285 tcg_gen_andc_i32(cpu_CF, cpu_CF, tcg_t1);
5286 } else {
5287 tcg_gen_and_i32(cpu_CF, cpu_CF, tcg_t2);
5290 if (nzcv & 1) { /* V */
5291 tcg_gen_or_i32(cpu_VF, cpu_VF, tcg_t1);
5292 } else {
5293 if (TCG_TARGET_HAS_andc_i32) {
5294 tcg_gen_andc_i32(cpu_VF, cpu_VF, tcg_t1);
5295 } else {
5296 tcg_gen_and_i32(cpu_VF, cpu_VF, tcg_t2);
5299 tcg_temp_free_i32(tcg_t0);
5300 tcg_temp_free_i32(tcg_t1);
5301 tcg_temp_free_i32(tcg_t2);
5304 /* Conditional select
5305 * 31 30 29 28 21 20 16 15 12 11 10 9 5 4 0
5306 * +----+----+---+-----------------+------+------+-----+------+------+
5307 * | sf | op | S | 1 1 0 1 0 1 0 0 | Rm | cond | op2 | Rn | Rd |
5308 * +----+----+---+-----------------+------+------+-----+------+------+
5310 static void disas_cond_select(DisasContext *s, uint32_t insn)
5312 unsigned int sf, else_inv, rm, cond, else_inc, rn, rd;
5313 TCGv_i64 tcg_rd, zero;
5314 DisasCompare64 c;
5316 if (extract32(insn, 29, 1) || extract32(insn, 11, 1)) {
5317 /* S == 1 or op2<1> == 1 */
5318 unallocated_encoding(s);
5319 return;
5321 sf = extract32(insn, 31, 1);
5322 else_inv = extract32(insn, 30, 1);
5323 rm = extract32(insn, 16, 5);
5324 cond = extract32(insn, 12, 4);
5325 else_inc = extract32(insn, 10, 1);
5326 rn = extract32(insn, 5, 5);
5327 rd = extract32(insn, 0, 5);
5329 tcg_rd = cpu_reg(s, rd);
5331 a64_test_cc(&c, cond);
5332 zero = tcg_const_i64(0);
5334 if (rn == 31 && rm == 31 && (else_inc ^ else_inv)) {
5335 /* CSET & CSETM. */
5336 tcg_gen_setcond_i64(tcg_invert_cond(c.cond), tcg_rd, c.value, zero);
5337 if (else_inv) {
5338 tcg_gen_neg_i64(tcg_rd, tcg_rd);
5340 } else {
5341 TCGv_i64 t_true = cpu_reg(s, rn);
5342 TCGv_i64 t_false = read_cpu_reg(s, rm, 1);
5343 if (else_inv && else_inc) {
5344 tcg_gen_neg_i64(t_false, t_false);
5345 } else if (else_inv) {
5346 tcg_gen_not_i64(t_false, t_false);
5347 } else if (else_inc) {
5348 tcg_gen_addi_i64(t_false, t_false, 1);
5350 tcg_gen_movcond_i64(c.cond, tcg_rd, c.value, zero, t_true, t_false);
5353 tcg_temp_free_i64(zero);
5354 a64_free_cc(&c);
5356 if (!sf) {
5357 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
5361 static void handle_clz(DisasContext *s, unsigned int sf,
5362 unsigned int rn, unsigned int rd)
5364 TCGv_i64 tcg_rd, tcg_rn;
5365 tcg_rd = cpu_reg(s, rd);
5366 tcg_rn = cpu_reg(s, rn);
5368 if (sf) {
5369 tcg_gen_clzi_i64(tcg_rd, tcg_rn, 64);
5370 } else {
5371 TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
5372 tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
5373 tcg_gen_clzi_i32(tcg_tmp32, tcg_tmp32, 32);
5374 tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
5375 tcg_temp_free_i32(tcg_tmp32);
5379 static void handle_cls(DisasContext *s, unsigned int sf,
5380 unsigned int rn, unsigned int rd)
5382 TCGv_i64 tcg_rd, tcg_rn;
5383 tcg_rd = cpu_reg(s, rd);
5384 tcg_rn = cpu_reg(s, rn);
5386 if (sf) {
5387 tcg_gen_clrsb_i64(tcg_rd, tcg_rn);
5388 } else {
5389 TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
5390 tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
5391 tcg_gen_clrsb_i32(tcg_tmp32, tcg_tmp32);
5392 tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
5393 tcg_temp_free_i32(tcg_tmp32);
5397 static void handle_rbit(DisasContext *s, unsigned int sf,
5398 unsigned int rn, unsigned int rd)
5400 TCGv_i64 tcg_rd, tcg_rn;
5401 tcg_rd = cpu_reg(s, rd);
5402 tcg_rn = cpu_reg(s, rn);
5404 if (sf) {
5405 gen_helper_rbit64(tcg_rd, tcg_rn);
5406 } else {
5407 TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
5408 tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
5409 gen_helper_rbit(tcg_tmp32, tcg_tmp32);
5410 tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
5411 tcg_temp_free_i32(tcg_tmp32);
5415 /* REV with sf==1, opcode==3 ("REV64") */
5416 static void handle_rev64(DisasContext *s, unsigned int sf,
5417 unsigned int rn, unsigned int rd)
5419 if (!sf) {
5420 unallocated_encoding(s);
5421 return;
5423 tcg_gen_bswap64_i64(cpu_reg(s, rd), cpu_reg(s, rn));
5426 /* REV with sf==0, opcode==2
5427 * REV32 (sf==1, opcode==2)
5429 static void handle_rev32(DisasContext *s, unsigned int sf,
5430 unsigned int rn, unsigned int rd)
5432 TCGv_i64 tcg_rd = cpu_reg(s, rd);
5434 if (sf) {
5435 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
5436 TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
5438 /* bswap32_i64 requires zero high word */
5439 tcg_gen_ext32u_i64(tcg_tmp, tcg_rn);
5440 tcg_gen_bswap32_i64(tcg_rd, tcg_tmp);
5441 tcg_gen_shri_i64(tcg_tmp, tcg_rn, 32);
5442 tcg_gen_bswap32_i64(tcg_tmp, tcg_tmp);
5443 tcg_gen_concat32_i64(tcg_rd, tcg_rd, tcg_tmp);
5445 tcg_temp_free_i64(tcg_tmp);
5446 } else {
5447 tcg_gen_ext32u_i64(tcg_rd, cpu_reg(s, rn));
5448 tcg_gen_bswap32_i64(tcg_rd, tcg_rd);
5452 /* REV16 (opcode==1) */
5453 static void handle_rev16(DisasContext *s, unsigned int sf,
5454 unsigned int rn, unsigned int rd)
5456 TCGv_i64 tcg_rd = cpu_reg(s, rd);
5457 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
5458 TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
5459 TCGv_i64 mask = tcg_const_i64(sf ? 0x00ff00ff00ff00ffull : 0x00ff00ff);
5461 tcg_gen_shri_i64(tcg_tmp, tcg_rn, 8);
5462 tcg_gen_and_i64(tcg_rd, tcg_rn, mask);
5463 tcg_gen_and_i64(tcg_tmp, tcg_tmp, mask);
5464 tcg_gen_shli_i64(tcg_rd, tcg_rd, 8);
5465 tcg_gen_or_i64(tcg_rd, tcg_rd, tcg_tmp);
5467 tcg_temp_free_i64(mask);
5468 tcg_temp_free_i64(tcg_tmp);
5471 /* Data-processing (1 source)
5472 * 31 30 29 28 21 20 16 15 10 9 5 4 0
5473 * +----+---+---+-----------------+---------+--------+------+------+
5474 * | sf | 1 | S | 1 1 0 1 0 1 1 0 | opcode2 | opcode | Rn | Rd |
5475 * +----+---+---+-----------------+---------+--------+------+------+
5477 static void disas_data_proc_1src(DisasContext *s, uint32_t insn)
5479 unsigned int sf, opcode, opcode2, rn, rd;
5480 TCGv_i64 tcg_rd;
5482 if (extract32(insn, 29, 1)) {
5483 unallocated_encoding(s);
5484 return;
5487 sf = extract32(insn, 31, 1);
5488 opcode = extract32(insn, 10, 6);
5489 opcode2 = extract32(insn, 16, 5);
5490 rn = extract32(insn, 5, 5);
5491 rd = extract32(insn, 0, 5);
5493 #define MAP(SF, O2, O1) ((SF) | (O1 << 1) | (O2 << 7))
5495 switch (MAP(sf, opcode2, opcode)) {
5496 case MAP(0, 0x00, 0x00): /* RBIT */
5497 case MAP(1, 0x00, 0x00):
5498 handle_rbit(s, sf, rn, rd);
5499 break;
5500 case MAP(0, 0x00, 0x01): /* REV16 */
5501 case MAP(1, 0x00, 0x01):
5502 handle_rev16(s, sf, rn, rd);
5503 break;
5504 case MAP(0, 0x00, 0x02): /* REV/REV32 */
5505 case MAP(1, 0x00, 0x02):
5506 handle_rev32(s, sf, rn, rd);
5507 break;
5508 case MAP(1, 0x00, 0x03): /* REV64 */
5509 handle_rev64(s, sf, rn, rd);
5510 break;
5511 case MAP(0, 0x00, 0x04): /* CLZ */
5512 case MAP(1, 0x00, 0x04):
5513 handle_clz(s, sf, rn, rd);
5514 break;
5515 case MAP(0, 0x00, 0x05): /* CLS */
5516 case MAP(1, 0x00, 0x05):
5517 handle_cls(s, sf, rn, rd);
5518 break;
5519 case MAP(1, 0x01, 0x00): /* PACIA */
5520 if (s->pauth_active) {
5521 tcg_rd = cpu_reg(s, rd);
5522 gen_helper_pacia(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5523 } else if (!dc_isar_feature(aa64_pauth, s)) {
5524 goto do_unallocated;
5526 break;
5527 case MAP(1, 0x01, 0x01): /* PACIB */
5528 if (s->pauth_active) {
5529 tcg_rd = cpu_reg(s, rd);
5530 gen_helper_pacib(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5531 } else if (!dc_isar_feature(aa64_pauth, s)) {
5532 goto do_unallocated;
5534 break;
5535 case MAP(1, 0x01, 0x02): /* PACDA */
5536 if (s->pauth_active) {
5537 tcg_rd = cpu_reg(s, rd);
5538 gen_helper_pacda(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5539 } else if (!dc_isar_feature(aa64_pauth, s)) {
5540 goto do_unallocated;
5542 break;
5543 case MAP(1, 0x01, 0x03): /* PACDB */
5544 if (s->pauth_active) {
5545 tcg_rd = cpu_reg(s, rd);
5546 gen_helper_pacdb(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5547 } else if (!dc_isar_feature(aa64_pauth, s)) {
5548 goto do_unallocated;
5550 break;
5551 case MAP(1, 0x01, 0x04): /* AUTIA */
5552 if (s->pauth_active) {
5553 tcg_rd = cpu_reg(s, rd);
5554 gen_helper_autia(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5555 } else if (!dc_isar_feature(aa64_pauth, s)) {
5556 goto do_unallocated;
5558 break;
5559 case MAP(1, 0x01, 0x05): /* AUTIB */
5560 if (s->pauth_active) {
5561 tcg_rd = cpu_reg(s, rd);
5562 gen_helper_autib(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5563 } else if (!dc_isar_feature(aa64_pauth, s)) {
5564 goto do_unallocated;
5566 break;
5567 case MAP(1, 0x01, 0x06): /* AUTDA */
5568 if (s->pauth_active) {
5569 tcg_rd = cpu_reg(s, rd);
5570 gen_helper_autda(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5571 } else if (!dc_isar_feature(aa64_pauth, s)) {
5572 goto do_unallocated;
5574 break;
5575 case MAP(1, 0x01, 0x07): /* AUTDB */
5576 if (s->pauth_active) {
5577 tcg_rd = cpu_reg(s, rd);
5578 gen_helper_autdb(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5579 } else if (!dc_isar_feature(aa64_pauth, s)) {
5580 goto do_unallocated;
5582 break;
5583 case MAP(1, 0x01, 0x08): /* PACIZA */
5584 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5585 goto do_unallocated;
5586 } else if (s->pauth_active) {
5587 tcg_rd = cpu_reg(s, rd);
5588 gen_helper_pacia(tcg_rd, cpu_env, tcg_rd, new_tmp_a64_zero(s));
5590 break;
5591 case MAP(1, 0x01, 0x09): /* PACIZB */
5592 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5593 goto do_unallocated;
5594 } else if (s->pauth_active) {
5595 tcg_rd = cpu_reg(s, rd);
5596 gen_helper_pacib(tcg_rd, cpu_env, tcg_rd, new_tmp_a64_zero(s));
5598 break;
5599 case MAP(1, 0x01, 0x0a): /* PACDZA */
5600 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5601 goto do_unallocated;
5602 } else if (s->pauth_active) {
5603 tcg_rd = cpu_reg(s, rd);
5604 gen_helper_pacda(tcg_rd, cpu_env, tcg_rd, new_tmp_a64_zero(s));
5606 break;
5607 case MAP(1, 0x01, 0x0b): /* PACDZB */
5608 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5609 goto do_unallocated;
5610 } else if (s->pauth_active) {
5611 tcg_rd = cpu_reg(s, rd);
5612 gen_helper_pacdb(tcg_rd, cpu_env, tcg_rd, new_tmp_a64_zero(s));
5614 break;
5615 case MAP(1, 0x01, 0x0c): /* AUTIZA */
5616 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5617 goto do_unallocated;
5618 } else if (s->pauth_active) {
5619 tcg_rd = cpu_reg(s, rd);
5620 gen_helper_autia(tcg_rd, cpu_env, tcg_rd, new_tmp_a64_zero(s));
5622 break;
5623 case MAP(1, 0x01, 0x0d): /* AUTIZB */
5624 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5625 goto do_unallocated;
5626 } else if (s->pauth_active) {
5627 tcg_rd = cpu_reg(s, rd);
5628 gen_helper_autib(tcg_rd, cpu_env, tcg_rd, new_tmp_a64_zero(s));
5630 break;
5631 case MAP(1, 0x01, 0x0e): /* AUTDZA */
5632 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5633 goto do_unallocated;
5634 } else if (s->pauth_active) {
5635 tcg_rd = cpu_reg(s, rd);
5636 gen_helper_autda(tcg_rd, cpu_env, tcg_rd, new_tmp_a64_zero(s));
5638 break;
5639 case MAP(1, 0x01, 0x0f): /* AUTDZB */
5640 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5641 goto do_unallocated;
5642 } else if (s->pauth_active) {
5643 tcg_rd = cpu_reg(s, rd);
5644 gen_helper_autdb(tcg_rd, cpu_env, tcg_rd, new_tmp_a64_zero(s));
5646 break;
5647 case MAP(1, 0x01, 0x10): /* XPACI */
5648 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5649 goto do_unallocated;
5650 } else if (s->pauth_active) {
5651 tcg_rd = cpu_reg(s, rd);
5652 gen_helper_xpaci(tcg_rd, cpu_env, tcg_rd);
5654 break;
5655 case MAP(1, 0x01, 0x11): /* XPACD */
5656 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5657 goto do_unallocated;
5658 } else if (s->pauth_active) {
5659 tcg_rd = cpu_reg(s, rd);
5660 gen_helper_xpacd(tcg_rd, cpu_env, tcg_rd);
5662 break;
5663 default:
5664 do_unallocated:
5665 unallocated_encoding(s);
5666 break;
5669 #undef MAP
5672 static void handle_div(DisasContext *s, bool is_signed, unsigned int sf,
5673 unsigned int rm, unsigned int rn, unsigned int rd)
5675 TCGv_i64 tcg_n, tcg_m, tcg_rd;
5676 tcg_rd = cpu_reg(s, rd);
5678 if (!sf && is_signed) {
5679 tcg_n = new_tmp_a64(s);
5680 tcg_m = new_tmp_a64(s);
5681 tcg_gen_ext32s_i64(tcg_n, cpu_reg(s, rn));
5682 tcg_gen_ext32s_i64(tcg_m, cpu_reg(s, rm));
5683 } else {
5684 tcg_n = read_cpu_reg(s, rn, sf);
5685 tcg_m = read_cpu_reg(s, rm, sf);
5688 if (is_signed) {
5689 gen_helper_sdiv64(tcg_rd, tcg_n, tcg_m);
5690 } else {
5691 gen_helper_udiv64(tcg_rd, tcg_n, tcg_m);
5694 if (!sf) { /* zero extend final result */
5695 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
5699 /* LSLV, LSRV, ASRV, RORV */
5700 static void handle_shift_reg(DisasContext *s,
5701 enum a64_shift_type shift_type, unsigned int sf,
5702 unsigned int rm, unsigned int rn, unsigned int rd)
5704 TCGv_i64 tcg_shift = tcg_temp_new_i64();
5705 TCGv_i64 tcg_rd = cpu_reg(s, rd);
5706 TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
5708 tcg_gen_andi_i64(tcg_shift, cpu_reg(s, rm), sf ? 63 : 31);
5709 shift_reg(tcg_rd, tcg_rn, sf, shift_type, tcg_shift);
5710 tcg_temp_free_i64(tcg_shift);
5713 /* CRC32[BHWX], CRC32C[BHWX] */
5714 static void handle_crc32(DisasContext *s,
5715 unsigned int sf, unsigned int sz, bool crc32c,
5716 unsigned int rm, unsigned int rn, unsigned int rd)
5718 TCGv_i64 tcg_acc, tcg_val;
5719 TCGv_i32 tcg_bytes;
5721 if (!dc_isar_feature(aa64_crc32, s)
5722 || (sf == 1 && sz != 3)
5723 || (sf == 0 && sz == 3)) {
5724 unallocated_encoding(s);
5725 return;
5728 if (sz == 3) {
5729 tcg_val = cpu_reg(s, rm);
5730 } else {
5731 uint64_t mask;
5732 switch (sz) {
5733 case 0:
5734 mask = 0xFF;
5735 break;
5736 case 1:
5737 mask = 0xFFFF;
5738 break;
5739 case 2:
5740 mask = 0xFFFFFFFF;
5741 break;
5742 default:
5743 g_assert_not_reached();
5745 tcg_val = new_tmp_a64(s);
5746 tcg_gen_andi_i64(tcg_val, cpu_reg(s, rm), mask);
5749 tcg_acc = cpu_reg(s, rn);
5750 tcg_bytes = tcg_const_i32(1 << sz);
5752 if (crc32c) {
5753 gen_helper_crc32c_64(cpu_reg(s, rd), tcg_acc, tcg_val, tcg_bytes);
5754 } else {
5755 gen_helper_crc32_64(cpu_reg(s, rd), tcg_acc, tcg_val, tcg_bytes);
5758 tcg_temp_free_i32(tcg_bytes);
5761 /* Data-processing (2 source)
5762 * 31 30 29 28 21 20 16 15 10 9 5 4 0
5763 * +----+---+---+-----------------+------+--------+------+------+
5764 * | sf | 0 | S | 1 1 0 1 0 1 1 0 | Rm | opcode | Rn | Rd |
5765 * +----+---+---+-----------------+------+--------+------+------+
5767 static void disas_data_proc_2src(DisasContext *s, uint32_t insn)
5769 unsigned int sf, rm, opcode, rn, rd, setflag;
5770 sf = extract32(insn, 31, 1);
5771 setflag = extract32(insn, 29, 1);
5772 rm = extract32(insn, 16, 5);
5773 opcode = extract32(insn, 10, 6);
5774 rn = extract32(insn, 5, 5);
5775 rd = extract32(insn, 0, 5);
5777 if (setflag && opcode != 0) {
5778 unallocated_encoding(s);
5779 return;
5782 switch (opcode) {
5783 case 0: /* SUBP(S) */
5784 if (sf == 0 || !dc_isar_feature(aa64_mte_insn_reg, s)) {
5785 goto do_unallocated;
5786 } else {
5787 TCGv_i64 tcg_n, tcg_m, tcg_d;
5789 tcg_n = read_cpu_reg_sp(s, rn, true);
5790 tcg_m = read_cpu_reg_sp(s, rm, true);
5791 tcg_gen_sextract_i64(tcg_n, tcg_n, 0, 56);
5792 tcg_gen_sextract_i64(tcg_m, tcg_m, 0, 56);
5793 tcg_d = cpu_reg(s, rd);
5795 if (setflag) {
5796 gen_sub_CC(true, tcg_d, tcg_n, tcg_m);
5797 } else {
5798 tcg_gen_sub_i64(tcg_d, tcg_n, tcg_m);
5801 break;
5802 case 2: /* UDIV */
5803 handle_div(s, false, sf, rm, rn, rd);
5804 break;
5805 case 3: /* SDIV */
5806 handle_div(s, true, sf, rm, rn, rd);
5807 break;
5808 case 4: /* IRG */
5809 if (sf == 0 || !dc_isar_feature(aa64_mte_insn_reg, s)) {
5810 goto do_unallocated;
5812 if (s->ata) {
5813 gen_helper_irg(cpu_reg_sp(s, rd), cpu_env,
5814 cpu_reg_sp(s, rn), cpu_reg(s, rm));
5815 } else {
5816 gen_address_with_allocation_tag0(cpu_reg_sp(s, rd),
5817 cpu_reg_sp(s, rn));
5819 break;
5820 case 5: /* GMI */
5821 if (sf == 0 || !dc_isar_feature(aa64_mte_insn_reg, s)) {
5822 goto do_unallocated;
5823 } else {
5824 TCGv_i64 t1 = tcg_const_i64(1);
5825 TCGv_i64 t2 = tcg_temp_new_i64();
5827 tcg_gen_extract_i64(t2, cpu_reg_sp(s, rn), 56, 4);
5828 tcg_gen_shl_i64(t1, t1, t2);
5829 tcg_gen_or_i64(cpu_reg(s, rd), cpu_reg(s, rm), t1);
5831 tcg_temp_free_i64(t1);
5832 tcg_temp_free_i64(t2);
5834 break;
5835 case 8: /* LSLV */
5836 handle_shift_reg(s, A64_SHIFT_TYPE_LSL, sf, rm, rn, rd);
5837 break;
5838 case 9: /* LSRV */
5839 handle_shift_reg(s, A64_SHIFT_TYPE_LSR, sf, rm, rn, rd);
5840 break;
5841 case 10: /* ASRV */
5842 handle_shift_reg(s, A64_SHIFT_TYPE_ASR, sf, rm, rn, rd);
5843 break;
5844 case 11: /* RORV */
5845 handle_shift_reg(s, A64_SHIFT_TYPE_ROR, sf, rm, rn, rd);
5846 break;
5847 case 12: /* PACGA */
5848 if (sf == 0 || !dc_isar_feature(aa64_pauth, s)) {
5849 goto do_unallocated;
5851 gen_helper_pacga(cpu_reg(s, rd), cpu_env,
5852 cpu_reg(s, rn), cpu_reg_sp(s, rm));
5853 break;
5854 case 16:
5855 case 17:
5856 case 18:
5857 case 19:
5858 case 20:
5859 case 21:
5860 case 22:
5861 case 23: /* CRC32 */
5863 int sz = extract32(opcode, 0, 2);
5864 bool crc32c = extract32(opcode, 2, 1);
5865 handle_crc32(s, sf, sz, crc32c, rm, rn, rd);
5866 break;
5868 default:
5869 do_unallocated:
5870 unallocated_encoding(s);
5871 break;
5876 * Data processing - register
5877 * 31 30 29 28 25 21 20 16 10 0
5878 * +--+---+--+---+-------+-----+-------+-------+---------+
5879 * | |op0| |op1| 1 0 1 | op2 | | op3 | |
5880 * +--+---+--+---+-------+-----+-------+-------+---------+
5882 static void disas_data_proc_reg(DisasContext *s, uint32_t insn)
5884 int op0 = extract32(insn, 30, 1);
5885 int op1 = extract32(insn, 28, 1);
5886 int op2 = extract32(insn, 21, 4);
5887 int op3 = extract32(insn, 10, 6);
5889 if (!op1) {
5890 if (op2 & 8) {
5891 if (op2 & 1) {
5892 /* Add/sub (extended register) */
5893 disas_add_sub_ext_reg(s, insn);
5894 } else {
5895 /* Add/sub (shifted register) */
5896 disas_add_sub_reg(s, insn);
5898 } else {
5899 /* Logical (shifted register) */
5900 disas_logic_reg(s, insn);
5902 return;
5905 switch (op2) {
5906 case 0x0:
5907 switch (op3) {
5908 case 0x00: /* Add/subtract (with carry) */
5909 disas_adc_sbc(s, insn);
5910 break;
5912 case 0x01: /* Rotate right into flags */
5913 case 0x21:
5914 disas_rotate_right_into_flags(s, insn);
5915 break;
5917 case 0x02: /* Evaluate into flags */
5918 case 0x12:
5919 case 0x22:
5920 case 0x32:
5921 disas_evaluate_into_flags(s, insn);
5922 break;
5924 default:
5925 goto do_unallocated;
5927 break;
5929 case 0x2: /* Conditional compare */
5930 disas_cc(s, insn); /* both imm and reg forms */
5931 break;
5933 case 0x4: /* Conditional select */
5934 disas_cond_select(s, insn);
5935 break;
5937 case 0x6: /* Data-processing */
5938 if (op0) { /* (1 source) */
5939 disas_data_proc_1src(s, insn);
5940 } else { /* (2 source) */
5941 disas_data_proc_2src(s, insn);
5943 break;
5944 case 0x8 ... 0xf: /* (3 source) */
5945 disas_data_proc_3src(s, insn);
5946 break;
5948 default:
5949 do_unallocated:
5950 unallocated_encoding(s);
5951 break;
5955 static void handle_fp_compare(DisasContext *s, int size,
5956 unsigned int rn, unsigned int rm,
5957 bool cmp_with_zero, bool signal_all_nans)
5959 TCGv_i64 tcg_flags = tcg_temp_new_i64();
5960 TCGv_ptr fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
5962 if (size == MO_64) {
5963 TCGv_i64 tcg_vn, tcg_vm;
5965 tcg_vn = read_fp_dreg(s, rn);
5966 if (cmp_with_zero) {
5967 tcg_vm = tcg_const_i64(0);
5968 } else {
5969 tcg_vm = read_fp_dreg(s, rm);
5971 if (signal_all_nans) {
5972 gen_helper_vfp_cmped_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5973 } else {
5974 gen_helper_vfp_cmpd_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5976 tcg_temp_free_i64(tcg_vn);
5977 tcg_temp_free_i64(tcg_vm);
5978 } else {
5979 TCGv_i32 tcg_vn = tcg_temp_new_i32();
5980 TCGv_i32 tcg_vm = tcg_temp_new_i32();
5982 read_vec_element_i32(s, tcg_vn, rn, 0, size);
5983 if (cmp_with_zero) {
5984 tcg_gen_movi_i32(tcg_vm, 0);
5985 } else {
5986 read_vec_element_i32(s, tcg_vm, rm, 0, size);
5989 switch (size) {
5990 case MO_32:
5991 if (signal_all_nans) {
5992 gen_helper_vfp_cmpes_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5993 } else {
5994 gen_helper_vfp_cmps_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5996 break;
5997 case MO_16:
5998 if (signal_all_nans) {
5999 gen_helper_vfp_cmpeh_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
6000 } else {
6001 gen_helper_vfp_cmph_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
6003 break;
6004 default:
6005 g_assert_not_reached();
6008 tcg_temp_free_i32(tcg_vn);
6009 tcg_temp_free_i32(tcg_vm);
6012 tcg_temp_free_ptr(fpst);
6014 gen_set_nzcv(tcg_flags);
6016 tcg_temp_free_i64(tcg_flags);
6019 /* Floating point compare
6020 * 31 30 29 28 24 23 22 21 20 16 15 14 13 10 9 5 4 0
6021 * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
6022 * | M | 0 | S | 1 1 1 1 0 | type | 1 | Rm | op | 1 0 0 0 | Rn | op2 |
6023 * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
6025 static void disas_fp_compare(DisasContext *s, uint32_t insn)
6027 unsigned int mos, type, rm, op, rn, opc, op2r;
6028 int size;
6030 mos = extract32(insn, 29, 3);
6031 type = extract32(insn, 22, 2);
6032 rm = extract32(insn, 16, 5);
6033 op = extract32(insn, 14, 2);
6034 rn = extract32(insn, 5, 5);
6035 opc = extract32(insn, 3, 2);
6036 op2r = extract32(insn, 0, 3);
6038 if (mos || op || op2r) {
6039 unallocated_encoding(s);
6040 return;
6043 switch (type) {
6044 case 0:
6045 size = MO_32;
6046 break;
6047 case 1:
6048 size = MO_64;
6049 break;
6050 case 3:
6051 size = MO_16;
6052 if (dc_isar_feature(aa64_fp16, s)) {
6053 break;
6055 /* fallthru */
6056 default:
6057 unallocated_encoding(s);
6058 return;
6061 if (!fp_access_check(s)) {
6062 return;
6065 handle_fp_compare(s, size, rn, rm, opc & 1, opc & 2);
6068 /* Floating point conditional compare
6069 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 3 0
6070 * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
6071 * | M | 0 | S | 1 1 1 1 0 | type | 1 | Rm | cond | 0 1 | Rn | op | nzcv |
6072 * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
6074 static void disas_fp_ccomp(DisasContext *s, uint32_t insn)
6076 unsigned int mos, type, rm, cond, rn, op, nzcv;
6077 TCGv_i64 tcg_flags;
6078 TCGLabel *label_continue = NULL;
6079 int size;
6081 mos = extract32(insn, 29, 3);
6082 type = extract32(insn, 22, 2);
6083 rm = extract32(insn, 16, 5);
6084 cond = extract32(insn, 12, 4);
6085 rn = extract32(insn, 5, 5);
6086 op = extract32(insn, 4, 1);
6087 nzcv = extract32(insn, 0, 4);
6089 if (mos) {
6090 unallocated_encoding(s);
6091 return;
6094 switch (type) {
6095 case 0:
6096 size = MO_32;
6097 break;
6098 case 1:
6099 size = MO_64;
6100 break;
6101 case 3:
6102 size = MO_16;
6103 if (dc_isar_feature(aa64_fp16, s)) {
6104 break;
6106 /* fallthru */
6107 default:
6108 unallocated_encoding(s);
6109 return;
6112 if (!fp_access_check(s)) {
6113 return;
6116 if (cond < 0x0e) { /* not always */
6117 TCGLabel *label_match = gen_new_label();
6118 label_continue = gen_new_label();
6119 arm_gen_test_cc(cond, label_match);
6120 /* nomatch: */
6121 tcg_flags = tcg_const_i64(nzcv << 28);
6122 gen_set_nzcv(tcg_flags);
6123 tcg_temp_free_i64(tcg_flags);
6124 tcg_gen_br(label_continue);
6125 gen_set_label(label_match);
6128 handle_fp_compare(s, size, rn, rm, false, op);
6130 if (cond < 0x0e) {
6131 gen_set_label(label_continue);
6135 /* Floating point conditional select
6136 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 0
6137 * +---+---+---+-----------+------+---+------+------+-----+------+------+
6138 * | M | 0 | S | 1 1 1 1 0 | type | 1 | Rm | cond | 1 1 | Rn | Rd |
6139 * +---+---+---+-----------+------+---+------+------+-----+------+------+
6141 static void disas_fp_csel(DisasContext *s, uint32_t insn)
6143 unsigned int mos, type, rm, cond, rn, rd;
6144 TCGv_i64 t_true, t_false, t_zero;
6145 DisasCompare64 c;
6146 MemOp sz;
6148 mos = extract32(insn, 29, 3);
6149 type = extract32(insn, 22, 2);
6150 rm = extract32(insn, 16, 5);
6151 cond = extract32(insn, 12, 4);
6152 rn = extract32(insn, 5, 5);
6153 rd = extract32(insn, 0, 5);
6155 if (mos) {
6156 unallocated_encoding(s);
6157 return;
6160 switch (type) {
6161 case 0:
6162 sz = MO_32;
6163 break;
6164 case 1:
6165 sz = MO_64;
6166 break;
6167 case 3:
6168 sz = MO_16;
6169 if (dc_isar_feature(aa64_fp16, s)) {
6170 break;
6172 /* fallthru */
6173 default:
6174 unallocated_encoding(s);
6175 return;
6178 if (!fp_access_check(s)) {
6179 return;
6182 /* Zero extend sreg & hreg inputs to 64 bits now. */
6183 t_true = tcg_temp_new_i64();
6184 t_false = tcg_temp_new_i64();
6185 read_vec_element(s, t_true, rn, 0, sz);
6186 read_vec_element(s, t_false, rm, 0, sz);
6188 a64_test_cc(&c, cond);
6189 t_zero = tcg_const_i64(0);
6190 tcg_gen_movcond_i64(c.cond, t_true, c.value, t_zero, t_true, t_false);
6191 tcg_temp_free_i64(t_zero);
6192 tcg_temp_free_i64(t_false);
6193 a64_free_cc(&c);
6195 /* Note that sregs & hregs write back zeros to the high bits,
6196 and we've already done the zero-extension. */
6197 write_fp_dreg(s, rd, t_true);
6198 tcg_temp_free_i64(t_true);
6201 /* Floating-point data-processing (1 source) - half precision */
6202 static void handle_fp_1src_half(DisasContext *s, int opcode, int rd, int rn)
6204 TCGv_ptr fpst = NULL;
6205 TCGv_i32 tcg_op = read_fp_hreg(s, rn);
6206 TCGv_i32 tcg_res = tcg_temp_new_i32();
6208 switch (opcode) {
6209 case 0x0: /* FMOV */
6210 tcg_gen_mov_i32(tcg_res, tcg_op);
6211 break;
6212 case 0x1: /* FABS */
6213 tcg_gen_andi_i32(tcg_res, tcg_op, 0x7fff);
6214 break;
6215 case 0x2: /* FNEG */
6216 tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
6217 break;
6218 case 0x3: /* FSQRT */
6219 fpst = fpstatus_ptr(FPST_FPCR_F16);
6220 gen_helper_sqrt_f16(tcg_res, tcg_op, fpst);
6221 break;
6222 case 0x8: /* FRINTN */
6223 case 0x9: /* FRINTP */
6224 case 0xa: /* FRINTM */
6225 case 0xb: /* FRINTZ */
6226 case 0xc: /* FRINTA */
6228 TCGv_i32 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(opcode & 7));
6229 fpst = fpstatus_ptr(FPST_FPCR_F16);
6231 gen_helper_set_rmode(tcg_rmode, tcg_rmode, fpst);
6232 gen_helper_advsimd_rinth(tcg_res, tcg_op, fpst);
6234 gen_helper_set_rmode(tcg_rmode, tcg_rmode, fpst);
6235 tcg_temp_free_i32(tcg_rmode);
6236 break;
6238 case 0xe: /* FRINTX */
6239 fpst = fpstatus_ptr(FPST_FPCR_F16);
6240 gen_helper_advsimd_rinth_exact(tcg_res, tcg_op, fpst);
6241 break;
6242 case 0xf: /* FRINTI */
6243 fpst = fpstatus_ptr(FPST_FPCR_F16);
6244 gen_helper_advsimd_rinth(tcg_res, tcg_op, fpst);
6245 break;
6246 default:
6247 abort();
6250 write_fp_sreg(s, rd, tcg_res);
6252 if (fpst) {
6253 tcg_temp_free_ptr(fpst);
6255 tcg_temp_free_i32(tcg_op);
6256 tcg_temp_free_i32(tcg_res);
6259 /* Floating-point data-processing (1 source) - single precision */
6260 static void handle_fp_1src_single(DisasContext *s, int opcode, int rd, int rn)
6262 void (*gen_fpst)(TCGv_i32, TCGv_i32, TCGv_ptr);
6263 TCGv_i32 tcg_op, tcg_res;
6264 TCGv_ptr fpst;
6265 int rmode = -1;
6267 tcg_op = read_fp_sreg(s, rn);
6268 tcg_res = tcg_temp_new_i32();
6270 switch (opcode) {
6271 case 0x0: /* FMOV */
6272 tcg_gen_mov_i32(tcg_res, tcg_op);
6273 goto done;
6274 case 0x1: /* FABS */
6275 gen_helper_vfp_abss(tcg_res, tcg_op);
6276 goto done;
6277 case 0x2: /* FNEG */
6278 gen_helper_vfp_negs(tcg_res, tcg_op);
6279 goto done;
6280 case 0x3: /* FSQRT */
6281 gen_helper_vfp_sqrts(tcg_res, tcg_op, cpu_env);
6282 goto done;
6283 case 0x6: /* BFCVT */
6284 gen_fpst = gen_helper_bfcvt;
6285 break;
6286 case 0x8: /* FRINTN */
6287 case 0x9: /* FRINTP */
6288 case 0xa: /* FRINTM */
6289 case 0xb: /* FRINTZ */
6290 case 0xc: /* FRINTA */
6291 rmode = arm_rmode_to_sf(opcode & 7);
6292 gen_fpst = gen_helper_rints;
6293 break;
6294 case 0xe: /* FRINTX */
6295 gen_fpst = gen_helper_rints_exact;
6296 break;
6297 case 0xf: /* FRINTI */
6298 gen_fpst = gen_helper_rints;
6299 break;
6300 case 0x10: /* FRINT32Z */
6301 rmode = float_round_to_zero;
6302 gen_fpst = gen_helper_frint32_s;
6303 break;
6304 case 0x11: /* FRINT32X */
6305 gen_fpst = gen_helper_frint32_s;
6306 break;
6307 case 0x12: /* FRINT64Z */
6308 rmode = float_round_to_zero;
6309 gen_fpst = gen_helper_frint64_s;
6310 break;
6311 case 0x13: /* FRINT64X */
6312 gen_fpst = gen_helper_frint64_s;
6313 break;
6314 default:
6315 g_assert_not_reached();
6318 fpst = fpstatus_ptr(FPST_FPCR);
6319 if (rmode >= 0) {
6320 TCGv_i32 tcg_rmode = tcg_const_i32(rmode);
6321 gen_helper_set_rmode(tcg_rmode, tcg_rmode, fpst);
6322 gen_fpst(tcg_res, tcg_op, fpst);
6323 gen_helper_set_rmode(tcg_rmode, tcg_rmode, fpst);
6324 tcg_temp_free_i32(tcg_rmode);
6325 } else {
6326 gen_fpst(tcg_res, tcg_op, fpst);
6328 tcg_temp_free_ptr(fpst);
6330 done:
6331 write_fp_sreg(s, rd, tcg_res);
6332 tcg_temp_free_i32(tcg_op);
6333 tcg_temp_free_i32(tcg_res);
6336 /* Floating-point data-processing (1 source) - double precision */
6337 static void handle_fp_1src_double(DisasContext *s, int opcode, int rd, int rn)
6339 void (*gen_fpst)(TCGv_i64, TCGv_i64, TCGv_ptr);
6340 TCGv_i64 tcg_op, tcg_res;
6341 TCGv_ptr fpst;
6342 int rmode = -1;
6344 switch (opcode) {
6345 case 0x0: /* FMOV */
6346 gen_gvec_fn2(s, false, rd, rn, tcg_gen_gvec_mov, 0);
6347 return;
6350 tcg_op = read_fp_dreg(s, rn);
6351 tcg_res = tcg_temp_new_i64();
6353 switch (opcode) {
6354 case 0x1: /* FABS */
6355 gen_helper_vfp_absd(tcg_res, tcg_op);
6356 goto done;
6357 case 0x2: /* FNEG */
6358 gen_helper_vfp_negd(tcg_res, tcg_op);
6359 goto done;
6360 case 0x3: /* FSQRT */
6361 gen_helper_vfp_sqrtd(tcg_res, tcg_op, cpu_env);
6362 goto done;
6363 case 0x8: /* FRINTN */
6364 case 0x9: /* FRINTP */
6365 case 0xa: /* FRINTM */
6366 case 0xb: /* FRINTZ */
6367 case 0xc: /* FRINTA */
6368 rmode = arm_rmode_to_sf(opcode & 7);
6369 gen_fpst = gen_helper_rintd;
6370 break;
6371 case 0xe: /* FRINTX */
6372 gen_fpst = gen_helper_rintd_exact;
6373 break;
6374 case 0xf: /* FRINTI */
6375 gen_fpst = gen_helper_rintd;
6376 break;
6377 case 0x10: /* FRINT32Z */
6378 rmode = float_round_to_zero;
6379 gen_fpst = gen_helper_frint32_d;
6380 break;
6381 case 0x11: /* FRINT32X */
6382 gen_fpst = gen_helper_frint32_d;
6383 break;
6384 case 0x12: /* FRINT64Z */
6385 rmode = float_round_to_zero;
6386 gen_fpst = gen_helper_frint64_d;
6387 break;
6388 case 0x13: /* FRINT64X */
6389 gen_fpst = gen_helper_frint64_d;
6390 break;
6391 default:
6392 g_assert_not_reached();
6395 fpst = fpstatus_ptr(FPST_FPCR);
6396 if (rmode >= 0) {
6397 TCGv_i32 tcg_rmode = tcg_const_i32(rmode);
6398 gen_helper_set_rmode(tcg_rmode, tcg_rmode, fpst);
6399 gen_fpst(tcg_res, tcg_op, fpst);
6400 gen_helper_set_rmode(tcg_rmode, tcg_rmode, fpst);
6401 tcg_temp_free_i32(tcg_rmode);
6402 } else {
6403 gen_fpst(tcg_res, tcg_op, fpst);
6405 tcg_temp_free_ptr(fpst);
6407 done:
6408 write_fp_dreg(s, rd, tcg_res);
6409 tcg_temp_free_i64(tcg_op);
6410 tcg_temp_free_i64(tcg_res);
6413 static void handle_fp_fcvt(DisasContext *s, int opcode,
6414 int rd, int rn, int dtype, int ntype)
6416 switch (ntype) {
6417 case 0x0:
6419 TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
6420 if (dtype == 1) {
6421 /* Single to double */
6422 TCGv_i64 tcg_rd = tcg_temp_new_i64();
6423 gen_helper_vfp_fcvtds(tcg_rd, tcg_rn, cpu_env);
6424 write_fp_dreg(s, rd, tcg_rd);
6425 tcg_temp_free_i64(tcg_rd);
6426 } else {
6427 /* Single to half */
6428 TCGv_i32 tcg_rd = tcg_temp_new_i32();
6429 TCGv_i32 ahp = get_ahp_flag();
6430 TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6432 gen_helper_vfp_fcvt_f32_to_f16(tcg_rd, tcg_rn, fpst, ahp);
6433 /* write_fp_sreg is OK here because top half of tcg_rd is zero */
6434 write_fp_sreg(s, rd, tcg_rd);
6435 tcg_temp_free_i32(tcg_rd);
6436 tcg_temp_free_i32(ahp);
6437 tcg_temp_free_ptr(fpst);
6439 tcg_temp_free_i32(tcg_rn);
6440 break;
6442 case 0x1:
6444 TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
6445 TCGv_i32 tcg_rd = tcg_temp_new_i32();
6446 if (dtype == 0) {
6447 /* Double to single */
6448 gen_helper_vfp_fcvtsd(tcg_rd, tcg_rn, cpu_env);
6449 } else {
6450 TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6451 TCGv_i32 ahp = get_ahp_flag();
6452 /* Double to half */
6453 gen_helper_vfp_fcvt_f64_to_f16(tcg_rd, tcg_rn, fpst, ahp);
6454 /* write_fp_sreg is OK here because top half of tcg_rd is zero */
6455 tcg_temp_free_ptr(fpst);
6456 tcg_temp_free_i32(ahp);
6458 write_fp_sreg(s, rd, tcg_rd);
6459 tcg_temp_free_i32(tcg_rd);
6460 tcg_temp_free_i64(tcg_rn);
6461 break;
6463 case 0x3:
6465 TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
6466 TCGv_ptr tcg_fpst = fpstatus_ptr(FPST_FPCR);
6467 TCGv_i32 tcg_ahp = get_ahp_flag();
6468 tcg_gen_ext16u_i32(tcg_rn, tcg_rn);
6469 if (dtype == 0) {
6470 /* Half to single */
6471 TCGv_i32 tcg_rd = tcg_temp_new_i32();
6472 gen_helper_vfp_fcvt_f16_to_f32(tcg_rd, tcg_rn, tcg_fpst, tcg_ahp);
6473 write_fp_sreg(s, rd, tcg_rd);
6474 tcg_temp_free_i32(tcg_rd);
6475 } else {
6476 /* Half to double */
6477 TCGv_i64 tcg_rd = tcg_temp_new_i64();
6478 gen_helper_vfp_fcvt_f16_to_f64(tcg_rd, tcg_rn, tcg_fpst, tcg_ahp);
6479 write_fp_dreg(s, rd, tcg_rd);
6480 tcg_temp_free_i64(tcg_rd);
6482 tcg_temp_free_i32(tcg_rn);
6483 tcg_temp_free_ptr(tcg_fpst);
6484 tcg_temp_free_i32(tcg_ahp);
6485 break;
6487 default:
6488 abort();
6492 /* Floating point data-processing (1 source)
6493 * 31 30 29 28 24 23 22 21 20 15 14 10 9 5 4 0
6494 * +---+---+---+-----------+------+---+--------+-----------+------+------+
6495 * | M | 0 | S | 1 1 1 1 0 | type | 1 | opcode | 1 0 0 0 0 | Rn | Rd |
6496 * +---+---+---+-----------+------+---+--------+-----------+------+------+
6498 static void disas_fp_1src(DisasContext *s, uint32_t insn)
6500 int mos = extract32(insn, 29, 3);
6501 int type = extract32(insn, 22, 2);
6502 int opcode = extract32(insn, 15, 6);
6503 int rn = extract32(insn, 5, 5);
6504 int rd = extract32(insn, 0, 5);
6506 if (mos) {
6507 goto do_unallocated;
6510 switch (opcode) {
6511 case 0x4: case 0x5: case 0x7:
6513 /* FCVT between half, single and double precision */
6514 int dtype = extract32(opcode, 0, 2);
6515 if (type == 2 || dtype == type) {
6516 goto do_unallocated;
6518 if (!fp_access_check(s)) {
6519 return;
6522 handle_fp_fcvt(s, opcode, rd, rn, dtype, type);
6523 break;
6526 case 0x10 ... 0x13: /* FRINT{32,64}{X,Z} */
6527 if (type > 1 || !dc_isar_feature(aa64_frint, s)) {
6528 goto do_unallocated;
6530 /* fall through */
6531 case 0x0 ... 0x3:
6532 case 0x8 ... 0xc:
6533 case 0xe ... 0xf:
6534 /* 32-to-32 and 64-to-64 ops */
6535 switch (type) {
6536 case 0:
6537 if (!fp_access_check(s)) {
6538 return;
6540 handle_fp_1src_single(s, opcode, rd, rn);
6541 break;
6542 case 1:
6543 if (!fp_access_check(s)) {
6544 return;
6546 handle_fp_1src_double(s, opcode, rd, rn);
6547 break;
6548 case 3:
6549 if (!dc_isar_feature(aa64_fp16, s)) {
6550 goto do_unallocated;
6553 if (!fp_access_check(s)) {
6554 return;
6556 handle_fp_1src_half(s, opcode, rd, rn);
6557 break;
6558 default:
6559 goto do_unallocated;
6561 break;
6563 case 0x6:
6564 switch (type) {
6565 case 1: /* BFCVT */
6566 if (!dc_isar_feature(aa64_bf16, s)) {
6567 goto do_unallocated;
6569 if (!fp_access_check(s)) {
6570 return;
6572 handle_fp_1src_single(s, opcode, rd, rn);
6573 break;
6574 default:
6575 goto do_unallocated;
6577 break;
6579 default:
6580 do_unallocated:
6581 unallocated_encoding(s);
6582 break;
6586 /* Floating-point data-processing (2 source) - single precision */
6587 static void handle_fp_2src_single(DisasContext *s, int opcode,
6588 int rd, int rn, int rm)
6590 TCGv_i32 tcg_op1;
6591 TCGv_i32 tcg_op2;
6592 TCGv_i32 tcg_res;
6593 TCGv_ptr fpst;
6595 tcg_res = tcg_temp_new_i32();
6596 fpst = fpstatus_ptr(FPST_FPCR);
6597 tcg_op1 = read_fp_sreg(s, rn);
6598 tcg_op2 = read_fp_sreg(s, rm);
6600 switch (opcode) {
6601 case 0x0: /* FMUL */
6602 gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
6603 break;
6604 case 0x1: /* FDIV */
6605 gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
6606 break;
6607 case 0x2: /* FADD */
6608 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
6609 break;
6610 case 0x3: /* FSUB */
6611 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
6612 break;
6613 case 0x4: /* FMAX */
6614 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
6615 break;
6616 case 0x5: /* FMIN */
6617 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
6618 break;
6619 case 0x6: /* FMAXNM */
6620 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
6621 break;
6622 case 0x7: /* FMINNM */
6623 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
6624 break;
6625 case 0x8: /* FNMUL */
6626 gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
6627 gen_helper_vfp_negs(tcg_res, tcg_res);
6628 break;
6631 write_fp_sreg(s, rd, tcg_res);
6633 tcg_temp_free_ptr(fpst);
6634 tcg_temp_free_i32(tcg_op1);
6635 tcg_temp_free_i32(tcg_op2);
6636 tcg_temp_free_i32(tcg_res);
6639 /* Floating-point data-processing (2 source) - double precision */
6640 static void handle_fp_2src_double(DisasContext *s, int opcode,
6641 int rd, int rn, int rm)
6643 TCGv_i64 tcg_op1;
6644 TCGv_i64 tcg_op2;
6645 TCGv_i64 tcg_res;
6646 TCGv_ptr fpst;
6648 tcg_res = tcg_temp_new_i64();
6649 fpst = fpstatus_ptr(FPST_FPCR);
6650 tcg_op1 = read_fp_dreg(s, rn);
6651 tcg_op2 = read_fp_dreg(s, rm);
6653 switch (opcode) {
6654 case 0x0: /* FMUL */
6655 gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
6656 break;
6657 case 0x1: /* FDIV */
6658 gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
6659 break;
6660 case 0x2: /* FADD */
6661 gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
6662 break;
6663 case 0x3: /* FSUB */
6664 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
6665 break;
6666 case 0x4: /* FMAX */
6667 gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
6668 break;
6669 case 0x5: /* FMIN */
6670 gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
6671 break;
6672 case 0x6: /* FMAXNM */
6673 gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
6674 break;
6675 case 0x7: /* FMINNM */
6676 gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
6677 break;
6678 case 0x8: /* FNMUL */
6679 gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
6680 gen_helper_vfp_negd(tcg_res, tcg_res);
6681 break;
6684 write_fp_dreg(s, rd, tcg_res);
6686 tcg_temp_free_ptr(fpst);
6687 tcg_temp_free_i64(tcg_op1);
6688 tcg_temp_free_i64(tcg_op2);
6689 tcg_temp_free_i64(tcg_res);
6692 /* Floating-point data-processing (2 source) - half precision */
6693 static void handle_fp_2src_half(DisasContext *s, int opcode,
6694 int rd, int rn, int rm)
6696 TCGv_i32 tcg_op1;
6697 TCGv_i32 tcg_op2;
6698 TCGv_i32 tcg_res;
6699 TCGv_ptr fpst;
6701 tcg_res = tcg_temp_new_i32();
6702 fpst = fpstatus_ptr(FPST_FPCR_F16);
6703 tcg_op1 = read_fp_hreg(s, rn);
6704 tcg_op2 = read_fp_hreg(s, rm);
6706 switch (opcode) {
6707 case 0x0: /* FMUL */
6708 gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
6709 break;
6710 case 0x1: /* FDIV */
6711 gen_helper_advsimd_divh(tcg_res, tcg_op1, tcg_op2, fpst);
6712 break;
6713 case 0x2: /* FADD */
6714 gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
6715 break;
6716 case 0x3: /* FSUB */
6717 gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
6718 break;
6719 case 0x4: /* FMAX */
6720 gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
6721 break;
6722 case 0x5: /* FMIN */
6723 gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
6724 break;
6725 case 0x6: /* FMAXNM */
6726 gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
6727 break;
6728 case 0x7: /* FMINNM */
6729 gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
6730 break;
6731 case 0x8: /* FNMUL */
6732 gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
6733 tcg_gen_xori_i32(tcg_res, tcg_res, 0x8000);
6734 break;
6735 default:
6736 g_assert_not_reached();
6739 write_fp_sreg(s, rd, tcg_res);
6741 tcg_temp_free_ptr(fpst);
6742 tcg_temp_free_i32(tcg_op1);
6743 tcg_temp_free_i32(tcg_op2);
6744 tcg_temp_free_i32(tcg_res);
6747 /* Floating point data-processing (2 source)
6748 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 0
6749 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
6750 * | M | 0 | S | 1 1 1 1 0 | type | 1 | Rm | opcode | 1 0 | Rn | Rd |
6751 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
6753 static void disas_fp_2src(DisasContext *s, uint32_t insn)
6755 int mos = extract32(insn, 29, 3);
6756 int type = extract32(insn, 22, 2);
6757 int rd = extract32(insn, 0, 5);
6758 int rn = extract32(insn, 5, 5);
6759 int rm = extract32(insn, 16, 5);
6760 int opcode = extract32(insn, 12, 4);
6762 if (opcode > 8 || mos) {
6763 unallocated_encoding(s);
6764 return;
6767 switch (type) {
6768 case 0:
6769 if (!fp_access_check(s)) {
6770 return;
6772 handle_fp_2src_single(s, opcode, rd, rn, rm);
6773 break;
6774 case 1:
6775 if (!fp_access_check(s)) {
6776 return;
6778 handle_fp_2src_double(s, opcode, rd, rn, rm);
6779 break;
6780 case 3:
6781 if (!dc_isar_feature(aa64_fp16, s)) {
6782 unallocated_encoding(s);
6783 return;
6785 if (!fp_access_check(s)) {
6786 return;
6788 handle_fp_2src_half(s, opcode, rd, rn, rm);
6789 break;
6790 default:
6791 unallocated_encoding(s);
6795 /* Floating-point data-processing (3 source) - single precision */
6796 static void handle_fp_3src_single(DisasContext *s, bool o0, bool o1,
6797 int rd, int rn, int rm, int ra)
6799 TCGv_i32 tcg_op1, tcg_op2, tcg_op3;
6800 TCGv_i32 tcg_res = tcg_temp_new_i32();
6801 TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6803 tcg_op1 = read_fp_sreg(s, rn);
6804 tcg_op2 = read_fp_sreg(s, rm);
6805 tcg_op3 = read_fp_sreg(s, ra);
6807 /* These are fused multiply-add, and must be done as one
6808 * floating point operation with no rounding between the
6809 * multiplication and addition steps.
6810 * NB that doing the negations here as separate steps is
6811 * correct : an input NaN should come out with its sign bit
6812 * flipped if it is a negated-input.
6814 if (o1 == true) {
6815 gen_helper_vfp_negs(tcg_op3, tcg_op3);
6818 if (o0 != o1) {
6819 gen_helper_vfp_negs(tcg_op1, tcg_op1);
6822 gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
6824 write_fp_sreg(s, rd, tcg_res);
6826 tcg_temp_free_ptr(fpst);
6827 tcg_temp_free_i32(tcg_op1);
6828 tcg_temp_free_i32(tcg_op2);
6829 tcg_temp_free_i32(tcg_op3);
6830 tcg_temp_free_i32(tcg_res);
6833 /* Floating-point data-processing (3 source) - double precision */
6834 static void handle_fp_3src_double(DisasContext *s, bool o0, bool o1,
6835 int rd, int rn, int rm, int ra)
6837 TCGv_i64 tcg_op1, tcg_op2, tcg_op3;
6838 TCGv_i64 tcg_res = tcg_temp_new_i64();
6839 TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
6841 tcg_op1 = read_fp_dreg(s, rn);
6842 tcg_op2 = read_fp_dreg(s, rm);
6843 tcg_op3 = read_fp_dreg(s, ra);
6845 /* These are fused multiply-add, and must be done as one
6846 * floating point operation with no rounding between the
6847 * multiplication and addition steps.
6848 * NB that doing the negations here as separate steps is
6849 * correct : an input NaN should come out with its sign bit
6850 * flipped if it is a negated-input.
6852 if (o1 == true) {
6853 gen_helper_vfp_negd(tcg_op3, tcg_op3);
6856 if (o0 != o1) {
6857 gen_helper_vfp_negd(tcg_op1, tcg_op1);
6860 gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
6862 write_fp_dreg(s, rd, tcg_res);
6864 tcg_temp_free_ptr(fpst);
6865 tcg_temp_free_i64(tcg_op1);
6866 tcg_temp_free_i64(tcg_op2);
6867 tcg_temp_free_i64(tcg_op3);
6868 tcg_temp_free_i64(tcg_res);
6871 /* Floating-point data-processing (3 source) - half precision */
6872 static void handle_fp_3src_half(DisasContext *s, bool o0, bool o1,
6873 int rd, int rn, int rm, int ra)
6875 TCGv_i32 tcg_op1, tcg_op2, tcg_op3;
6876 TCGv_i32 tcg_res = tcg_temp_new_i32();
6877 TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR_F16);
6879 tcg_op1 = read_fp_hreg(s, rn);
6880 tcg_op2 = read_fp_hreg(s, rm);
6881 tcg_op3 = read_fp_hreg(s, ra);
6883 /* These are fused multiply-add, and must be done as one
6884 * floating point operation with no rounding between the
6885 * multiplication and addition steps.
6886 * NB that doing the negations here as separate steps is
6887 * correct : an input NaN should come out with its sign bit
6888 * flipped if it is a negated-input.
6890 if (o1 == true) {
6891 tcg_gen_xori_i32(tcg_op3, tcg_op3, 0x8000);
6894 if (o0 != o1) {
6895 tcg_gen_xori_i32(tcg_op1, tcg_op1, 0x8000);
6898 gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
6900 write_fp_sreg(s, rd, tcg_res);
6902 tcg_temp_free_ptr(fpst);
6903 tcg_temp_free_i32(tcg_op1);
6904 tcg_temp_free_i32(tcg_op2);
6905 tcg_temp_free_i32(tcg_op3);
6906 tcg_temp_free_i32(tcg_res);
6909 /* Floating point data-processing (3 source)
6910 * 31 30 29 28 24 23 22 21 20 16 15 14 10 9 5 4 0
6911 * +---+---+---+-----------+------+----+------+----+------+------+------+
6912 * | M | 0 | S | 1 1 1 1 1 | type | o1 | Rm | o0 | Ra | Rn | Rd |
6913 * +---+---+---+-----------+------+----+------+----+------+------+------+
6915 static void disas_fp_3src(DisasContext *s, uint32_t insn)
6917 int mos = extract32(insn, 29, 3);
6918 int type = extract32(insn, 22, 2);
6919 int rd = extract32(insn, 0, 5);
6920 int rn = extract32(insn, 5, 5);
6921 int ra = extract32(insn, 10, 5);
6922 int rm = extract32(insn, 16, 5);
6923 bool o0 = extract32(insn, 15, 1);
6924 bool o1 = extract32(insn, 21, 1);
6926 if (mos) {
6927 unallocated_encoding(s);
6928 return;
6931 switch (type) {
6932 case 0:
6933 if (!fp_access_check(s)) {
6934 return;
6936 handle_fp_3src_single(s, o0, o1, rd, rn, rm, ra);
6937 break;
6938 case 1:
6939 if (!fp_access_check(s)) {
6940 return;
6942 handle_fp_3src_double(s, o0, o1, rd, rn, rm, ra);
6943 break;
6944 case 3:
6945 if (!dc_isar_feature(aa64_fp16, s)) {
6946 unallocated_encoding(s);
6947 return;
6949 if (!fp_access_check(s)) {
6950 return;
6952 handle_fp_3src_half(s, o0, o1, rd, rn, rm, ra);
6953 break;
6954 default:
6955 unallocated_encoding(s);
6959 /* Floating point immediate
6960 * 31 30 29 28 24 23 22 21 20 13 12 10 9 5 4 0
6961 * +---+---+---+-----------+------+---+------------+-------+------+------+
6962 * | M | 0 | S | 1 1 1 1 0 | type | 1 | imm8 | 1 0 0 | imm5 | Rd |
6963 * +---+---+---+-----------+------+---+------------+-------+------+------+
6965 static void disas_fp_imm(DisasContext *s, uint32_t insn)
6967 int rd = extract32(insn, 0, 5);
6968 int imm5 = extract32(insn, 5, 5);
6969 int imm8 = extract32(insn, 13, 8);
6970 int type = extract32(insn, 22, 2);
6971 int mos = extract32(insn, 29, 3);
6972 uint64_t imm;
6973 TCGv_i64 tcg_res;
6974 MemOp sz;
6976 if (mos || imm5) {
6977 unallocated_encoding(s);
6978 return;
6981 switch (type) {
6982 case 0:
6983 sz = MO_32;
6984 break;
6985 case 1:
6986 sz = MO_64;
6987 break;
6988 case 3:
6989 sz = MO_16;
6990 if (dc_isar_feature(aa64_fp16, s)) {
6991 break;
6993 /* fallthru */
6994 default:
6995 unallocated_encoding(s);
6996 return;
6999 if (!fp_access_check(s)) {
7000 return;
7003 imm = vfp_expand_imm(sz, imm8);
7005 tcg_res = tcg_const_i64(imm);
7006 write_fp_dreg(s, rd, tcg_res);
7007 tcg_temp_free_i64(tcg_res);
7010 /* Handle floating point <=> fixed point conversions. Note that we can
7011 * also deal with fp <=> integer conversions as a special case (scale == 64)
7012 * OPTME: consider handling that special case specially or at least skipping
7013 * the call to scalbn in the helpers for zero shifts.
7015 static void handle_fpfpcvt(DisasContext *s, int rd, int rn, int opcode,
7016 bool itof, int rmode, int scale, int sf, int type)
7018 bool is_signed = !(opcode & 1);
7019 TCGv_ptr tcg_fpstatus;
7020 TCGv_i32 tcg_shift, tcg_single;
7021 TCGv_i64 tcg_double;
7023 tcg_fpstatus = fpstatus_ptr(type == 3 ? FPST_FPCR_F16 : FPST_FPCR);
7025 tcg_shift = tcg_const_i32(64 - scale);
7027 if (itof) {
7028 TCGv_i64 tcg_int = cpu_reg(s, rn);
7029 if (!sf) {
7030 TCGv_i64 tcg_extend = new_tmp_a64(s);
7032 if (is_signed) {
7033 tcg_gen_ext32s_i64(tcg_extend, tcg_int);
7034 } else {
7035 tcg_gen_ext32u_i64(tcg_extend, tcg_int);
7038 tcg_int = tcg_extend;
7041 switch (type) {
7042 case 1: /* float64 */
7043 tcg_double = tcg_temp_new_i64();
7044 if (is_signed) {
7045 gen_helper_vfp_sqtod(tcg_double, tcg_int,
7046 tcg_shift, tcg_fpstatus);
7047 } else {
7048 gen_helper_vfp_uqtod(tcg_double, tcg_int,
7049 tcg_shift, tcg_fpstatus);
7051 write_fp_dreg(s, rd, tcg_double);
7052 tcg_temp_free_i64(tcg_double);
7053 break;
7055 case 0: /* float32 */
7056 tcg_single = tcg_temp_new_i32();
7057 if (is_signed) {
7058 gen_helper_vfp_sqtos(tcg_single, tcg_int,
7059 tcg_shift, tcg_fpstatus);
7060 } else {
7061 gen_helper_vfp_uqtos(tcg_single, tcg_int,
7062 tcg_shift, tcg_fpstatus);
7064 write_fp_sreg(s, rd, tcg_single);
7065 tcg_temp_free_i32(tcg_single);
7066 break;
7068 case 3: /* float16 */
7069 tcg_single = tcg_temp_new_i32();
7070 if (is_signed) {
7071 gen_helper_vfp_sqtoh(tcg_single, tcg_int,
7072 tcg_shift, tcg_fpstatus);
7073 } else {
7074 gen_helper_vfp_uqtoh(tcg_single, tcg_int,
7075 tcg_shift, tcg_fpstatus);
7077 write_fp_sreg(s, rd, tcg_single);
7078 tcg_temp_free_i32(tcg_single);
7079 break;
7081 default:
7082 g_assert_not_reached();
7084 } else {
7085 TCGv_i64 tcg_int = cpu_reg(s, rd);
7086 TCGv_i32 tcg_rmode;
7088 if (extract32(opcode, 2, 1)) {
7089 /* There are too many rounding modes to all fit into rmode,
7090 * so FCVTA[US] is a special case.
7092 rmode = FPROUNDING_TIEAWAY;
7095 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rmode));
7097 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
7099 switch (type) {
7100 case 1: /* float64 */
7101 tcg_double = read_fp_dreg(s, rn);
7102 if (is_signed) {
7103 if (!sf) {
7104 gen_helper_vfp_tosld(tcg_int, tcg_double,
7105 tcg_shift, tcg_fpstatus);
7106 } else {
7107 gen_helper_vfp_tosqd(tcg_int, tcg_double,
7108 tcg_shift, tcg_fpstatus);
7110 } else {
7111 if (!sf) {
7112 gen_helper_vfp_tould(tcg_int, tcg_double,
7113 tcg_shift, tcg_fpstatus);
7114 } else {
7115 gen_helper_vfp_touqd(tcg_int, tcg_double,
7116 tcg_shift, tcg_fpstatus);
7119 if (!sf) {
7120 tcg_gen_ext32u_i64(tcg_int, tcg_int);
7122 tcg_temp_free_i64(tcg_double);
7123 break;
7125 case 0: /* float32 */
7126 tcg_single = read_fp_sreg(s, rn);
7127 if (sf) {
7128 if (is_signed) {
7129 gen_helper_vfp_tosqs(tcg_int, tcg_single,
7130 tcg_shift, tcg_fpstatus);
7131 } else {
7132 gen_helper_vfp_touqs(tcg_int, tcg_single,
7133 tcg_shift, tcg_fpstatus);
7135 } else {
7136 TCGv_i32 tcg_dest = tcg_temp_new_i32();
7137 if (is_signed) {
7138 gen_helper_vfp_tosls(tcg_dest, tcg_single,
7139 tcg_shift, tcg_fpstatus);
7140 } else {
7141 gen_helper_vfp_touls(tcg_dest, tcg_single,
7142 tcg_shift, tcg_fpstatus);
7144 tcg_gen_extu_i32_i64(tcg_int, tcg_dest);
7145 tcg_temp_free_i32(tcg_dest);
7147 tcg_temp_free_i32(tcg_single);
7148 break;
7150 case 3: /* float16 */
7151 tcg_single = read_fp_sreg(s, rn);
7152 if (sf) {
7153 if (is_signed) {
7154 gen_helper_vfp_tosqh(tcg_int, tcg_single,
7155 tcg_shift, tcg_fpstatus);
7156 } else {
7157 gen_helper_vfp_touqh(tcg_int, tcg_single,
7158 tcg_shift, tcg_fpstatus);
7160 } else {
7161 TCGv_i32 tcg_dest = tcg_temp_new_i32();
7162 if (is_signed) {
7163 gen_helper_vfp_toslh(tcg_dest, tcg_single,
7164 tcg_shift, tcg_fpstatus);
7165 } else {
7166 gen_helper_vfp_toulh(tcg_dest, tcg_single,
7167 tcg_shift, tcg_fpstatus);
7169 tcg_gen_extu_i32_i64(tcg_int, tcg_dest);
7170 tcg_temp_free_i32(tcg_dest);
7172 tcg_temp_free_i32(tcg_single);
7173 break;
7175 default:
7176 g_assert_not_reached();
7179 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
7180 tcg_temp_free_i32(tcg_rmode);
7183 tcg_temp_free_ptr(tcg_fpstatus);
7184 tcg_temp_free_i32(tcg_shift);
7187 /* Floating point <-> fixed point conversions
7188 * 31 30 29 28 24 23 22 21 20 19 18 16 15 10 9 5 4 0
7189 * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
7190 * | sf | 0 | S | 1 1 1 1 0 | type | 0 | rmode | opcode | scale | Rn | Rd |
7191 * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
7193 static void disas_fp_fixed_conv(DisasContext *s, uint32_t insn)
7195 int rd = extract32(insn, 0, 5);
7196 int rn = extract32(insn, 5, 5);
7197 int scale = extract32(insn, 10, 6);
7198 int opcode = extract32(insn, 16, 3);
7199 int rmode = extract32(insn, 19, 2);
7200 int type = extract32(insn, 22, 2);
7201 bool sbit = extract32(insn, 29, 1);
7202 bool sf = extract32(insn, 31, 1);
7203 bool itof;
7205 if (sbit || (!sf && scale < 32)) {
7206 unallocated_encoding(s);
7207 return;
7210 switch (type) {
7211 case 0: /* float32 */
7212 case 1: /* float64 */
7213 break;
7214 case 3: /* float16 */
7215 if (dc_isar_feature(aa64_fp16, s)) {
7216 break;
7218 /* fallthru */
7219 default:
7220 unallocated_encoding(s);
7221 return;
7224 switch ((rmode << 3) | opcode) {
7225 case 0x2: /* SCVTF */
7226 case 0x3: /* UCVTF */
7227 itof = true;
7228 break;
7229 case 0x18: /* FCVTZS */
7230 case 0x19: /* FCVTZU */
7231 itof = false;
7232 break;
7233 default:
7234 unallocated_encoding(s);
7235 return;
7238 if (!fp_access_check(s)) {
7239 return;
7242 handle_fpfpcvt(s, rd, rn, opcode, itof, FPROUNDING_ZERO, scale, sf, type);
7245 static void handle_fmov(DisasContext *s, int rd, int rn, int type, bool itof)
7247 /* FMOV: gpr to or from float, double, or top half of quad fp reg,
7248 * without conversion.
7251 if (itof) {
7252 TCGv_i64 tcg_rn = cpu_reg(s, rn);
7253 TCGv_i64 tmp;
7255 switch (type) {
7256 case 0:
7257 /* 32 bit */
7258 tmp = tcg_temp_new_i64();
7259 tcg_gen_ext32u_i64(tmp, tcg_rn);
7260 write_fp_dreg(s, rd, tmp);
7261 tcg_temp_free_i64(tmp);
7262 break;
7263 case 1:
7264 /* 64 bit */
7265 write_fp_dreg(s, rd, tcg_rn);
7266 break;
7267 case 2:
7268 /* 64 bit to top half. */
7269 tcg_gen_st_i64(tcg_rn, cpu_env, fp_reg_hi_offset(s, rd));
7270 clear_vec_high(s, true, rd);
7271 break;
7272 case 3:
7273 /* 16 bit */
7274 tmp = tcg_temp_new_i64();
7275 tcg_gen_ext16u_i64(tmp, tcg_rn);
7276 write_fp_dreg(s, rd, tmp);
7277 tcg_temp_free_i64(tmp);
7278 break;
7279 default:
7280 g_assert_not_reached();
7282 } else {
7283 TCGv_i64 tcg_rd = cpu_reg(s, rd);
7285 switch (type) {
7286 case 0:
7287 /* 32 bit */
7288 tcg_gen_ld32u_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_32));
7289 break;
7290 case 1:
7291 /* 64 bit */
7292 tcg_gen_ld_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_64));
7293 break;
7294 case 2:
7295 /* 64 bits from top half */
7296 tcg_gen_ld_i64(tcg_rd, cpu_env, fp_reg_hi_offset(s, rn));
7297 break;
7298 case 3:
7299 /* 16 bit */
7300 tcg_gen_ld16u_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_16));
7301 break;
7302 default:
7303 g_assert_not_reached();
7308 static void handle_fjcvtzs(DisasContext *s, int rd, int rn)
7310 TCGv_i64 t = read_fp_dreg(s, rn);
7311 TCGv_ptr fpstatus = fpstatus_ptr(FPST_FPCR);
7313 gen_helper_fjcvtzs(t, t, fpstatus);
7315 tcg_temp_free_ptr(fpstatus);
7317 tcg_gen_ext32u_i64(cpu_reg(s, rd), t);
7318 tcg_gen_extrh_i64_i32(cpu_ZF, t);
7319 tcg_gen_movi_i32(cpu_CF, 0);
7320 tcg_gen_movi_i32(cpu_NF, 0);
7321 tcg_gen_movi_i32(cpu_VF, 0);
7323 tcg_temp_free_i64(t);
7326 /* Floating point <-> integer conversions
7327 * 31 30 29 28 24 23 22 21 20 19 18 16 15 10 9 5 4 0
7328 * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
7329 * | sf | 0 | S | 1 1 1 1 0 | type | 1 | rmode | opc | 0 0 0 0 0 0 | Rn | Rd |
7330 * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
7332 static void disas_fp_int_conv(DisasContext *s, uint32_t insn)
7334 int rd = extract32(insn, 0, 5);
7335 int rn = extract32(insn, 5, 5);
7336 int opcode = extract32(insn, 16, 3);
7337 int rmode = extract32(insn, 19, 2);
7338 int type = extract32(insn, 22, 2);
7339 bool sbit = extract32(insn, 29, 1);
7340 bool sf = extract32(insn, 31, 1);
7341 bool itof = false;
7343 if (sbit) {
7344 goto do_unallocated;
7347 switch (opcode) {
7348 case 2: /* SCVTF */
7349 case 3: /* UCVTF */
7350 itof = true;
7351 /* fallthru */
7352 case 4: /* FCVTAS */
7353 case 5: /* FCVTAU */
7354 if (rmode != 0) {
7355 goto do_unallocated;
7357 /* fallthru */
7358 case 0: /* FCVT[NPMZ]S */
7359 case 1: /* FCVT[NPMZ]U */
7360 switch (type) {
7361 case 0: /* float32 */
7362 case 1: /* float64 */
7363 break;
7364 case 3: /* float16 */
7365 if (!dc_isar_feature(aa64_fp16, s)) {
7366 goto do_unallocated;
7368 break;
7369 default:
7370 goto do_unallocated;
7372 if (!fp_access_check(s)) {
7373 return;
7375 handle_fpfpcvt(s, rd, rn, opcode, itof, rmode, 64, sf, type);
7376 break;
7378 default:
7379 switch (sf << 7 | type << 5 | rmode << 3 | opcode) {
7380 case 0b01100110: /* FMOV half <-> 32-bit int */
7381 case 0b01100111:
7382 case 0b11100110: /* FMOV half <-> 64-bit int */
7383 case 0b11100111:
7384 if (!dc_isar_feature(aa64_fp16, s)) {
7385 goto do_unallocated;
7387 /* fallthru */
7388 case 0b00000110: /* FMOV 32-bit */
7389 case 0b00000111:
7390 case 0b10100110: /* FMOV 64-bit */
7391 case 0b10100111:
7392 case 0b11001110: /* FMOV top half of 128-bit */
7393 case 0b11001111:
7394 if (!fp_access_check(s)) {
7395 return;
7397 itof = opcode & 1;
7398 handle_fmov(s, rd, rn, type, itof);
7399 break;
7401 case 0b00111110: /* FJCVTZS */
7402 if (!dc_isar_feature(aa64_jscvt, s)) {
7403 goto do_unallocated;
7404 } else if (fp_access_check(s)) {
7405 handle_fjcvtzs(s, rd, rn);
7407 break;
7409 default:
7410 do_unallocated:
7411 unallocated_encoding(s);
7412 return;
7414 break;
7418 /* FP-specific subcases of table C3-6 (SIMD and FP data processing)
7419 * 31 30 29 28 25 24 0
7420 * +---+---+---+---------+-----------------------------+
7421 * | | 0 | | 1 1 1 1 | |
7422 * +---+---+---+---------+-----------------------------+
7424 static void disas_data_proc_fp(DisasContext *s, uint32_t insn)
7426 if (extract32(insn, 24, 1)) {
7427 /* Floating point data-processing (3 source) */
7428 disas_fp_3src(s, insn);
7429 } else if (extract32(insn, 21, 1) == 0) {
7430 /* Floating point to fixed point conversions */
7431 disas_fp_fixed_conv(s, insn);
7432 } else {
7433 switch (extract32(insn, 10, 2)) {
7434 case 1:
7435 /* Floating point conditional compare */
7436 disas_fp_ccomp(s, insn);
7437 break;
7438 case 2:
7439 /* Floating point data-processing (2 source) */
7440 disas_fp_2src(s, insn);
7441 break;
7442 case 3:
7443 /* Floating point conditional select */
7444 disas_fp_csel(s, insn);
7445 break;
7446 case 0:
7447 switch (ctz32(extract32(insn, 12, 4))) {
7448 case 0: /* [15:12] == xxx1 */
7449 /* Floating point immediate */
7450 disas_fp_imm(s, insn);
7451 break;
7452 case 1: /* [15:12] == xx10 */
7453 /* Floating point compare */
7454 disas_fp_compare(s, insn);
7455 break;
7456 case 2: /* [15:12] == x100 */
7457 /* Floating point data-processing (1 source) */
7458 disas_fp_1src(s, insn);
7459 break;
7460 case 3: /* [15:12] == 1000 */
7461 unallocated_encoding(s);
7462 break;
7463 default: /* [15:12] == 0000 */
7464 /* Floating point <-> integer conversions */
7465 disas_fp_int_conv(s, insn);
7466 break;
7468 break;
7473 static void do_ext64(DisasContext *s, TCGv_i64 tcg_left, TCGv_i64 tcg_right,
7474 int pos)
7476 /* Extract 64 bits from the middle of two concatenated 64 bit
7477 * vector register slices left:right. The extracted bits start
7478 * at 'pos' bits into the right (least significant) side.
7479 * We return the result in tcg_right, and guarantee not to
7480 * trash tcg_left.
7482 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
7483 assert(pos > 0 && pos < 64);
7485 tcg_gen_shri_i64(tcg_right, tcg_right, pos);
7486 tcg_gen_shli_i64(tcg_tmp, tcg_left, 64 - pos);
7487 tcg_gen_or_i64(tcg_right, tcg_right, tcg_tmp);
7489 tcg_temp_free_i64(tcg_tmp);
7492 /* EXT
7493 * 31 30 29 24 23 22 21 20 16 15 14 11 10 9 5 4 0
7494 * +---+---+-------------+-----+---+------+---+------+---+------+------+
7495 * | 0 | Q | 1 0 1 1 1 0 | op2 | 0 | Rm | 0 | imm4 | 0 | Rn | Rd |
7496 * +---+---+-------------+-----+---+------+---+------+---+------+------+
7498 static void disas_simd_ext(DisasContext *s, uint32_t insn)
7500 int is_q = extract32(insn, 30, 1);
7501 int op2 = extract32(insn, 22, 2);
7502 int imm4 = extract32(insn, 11, 4);
7503 int rm = extract32(insn, 16, 5);
7504 int rn = extract32(insn, 5, 5);
7505 int rd = extract32(insn, 0, 5);
7506 int pos = imm4 << 3;
7507 TCGv_i64 tcg_resl, tcg_resh;
7509 if (op2 != 0 || (!is_q && extract32(imm4, 3, 1))) {
7510 unallocated_encoding(s);
7511 return;
7514 if (!fp_access_check(s)) {
7515 return;
7518 tcg_resh = tcg_temp_new_i64();
7519 tcg_resl = tcg_temp_new_i64();
7521 /* Vd gets bits starting at pos bits into Vm:Vn. This is
7522 * either extracting 128 bits from a 128:128 concatenation, or
7523 * extracting 64 bits from a 64:64 concatenation.
7525 if (!is_q) {
7526 read_vec_element(s, tcg_resl, rn, 0, MO_64);
7527 if (pos != 0) {
7528 read_vec_element(s, tcg_resh, rm, 0, MO_64);
7529 do_ext64(s, tcg_resh, tcg_resl, pos);
7531 } else {
7532 TCGv_i64 tcg_hh;
7533 typedef struct {
7534 int reg;
7535 int elt;
7536 } EltPosns;
7537 EltPosns eltposns[] = { {rn, 0}, {rn, 1}, {rm, 0}, {rm, 1} };
7538 EltPosns *elt = eltposns;
7540 if (pos >= 64) {
7541 elt++;
7542 pos -= 64;
7545 read_vec_element(s, tcg_resl, elt->reg, elt->elt, MO_64);
7546 elt++;
7547 read_vec_element(s, tcg_resh, elt->reg, elt->elt, MO_64);
7548 elt++;
7549 if (pos != 0) {
7550 do_ext64(s, tcg_resh, tcg_resl, pos);
7551 tcg_hh = tcg_temp_new_i64();
7552 read_vec_element(s, tcg_hh, elt->reg, elt->elt, MO_64);
7553 do_ext64(s, tcg_hh, tcg_resh, pos);
7554 tcg_temp_free_i64(tcg_hh);
7558 write_vec_element(s, tcg_resl, rd, 0, MO_64);
7559 tcg_temp_free_i64(tcg_resl);
7560 if (is_q) {
7561 write_vec_element(s, tcg_resh, rd, 1, MO_64);
7563 tcg_temp_free_i64(tcg_resh);
7564 clear_vec_high(s, is_q, rd);
7567 /* TBL/TBX
7568 * 31 30 29 24 23 22 21 20 16 15 14 13 12 11 10 9 5 4 0
7569 * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
7570 * | 0 | Q | 0 0 1 1 1 0 | op2 | 0 | Rm | 0 | len | op | 0 0 | Rn | Rd |
7571 * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
7573 static void disas_simd_tb(DisasContext *s, uint32_t insn)
7575 int op2 = extract32(insn, 22, 2);
7576 int is_q = extract32(insn, 30, 1);
7577 int rm = extract32(insn, 16, 5);
7578 int rn = extract32(insn, 5, 5);
7579 int rd = extract32(insn, 0, 5);
7580 int is_tbx = extract32(insn, 12, 1);
7581 int len = (extract32(insn, 13, 2) + 1) * 16;
7583 if (op2 != 0) {
7584 unallocated_encoding(s);
7585 return;
7588 if (!fp_access_check(s)) {
7589 return;
7592 tcg_gen_gvec_2_ptr(vec_full_reg_offset(s, rd),
7593 vec_full_reg_offset(s, rm), cpu_env,
7594 is_q ? 16 : 8, vec_full_reg_size(s),
7595 (len << 6) | (is_tbx << 5) | rn,
7596 gen_helper_simd_tblx);
7599 /* ZIP/UZP/TRN
7600 * 31 30 29 24 23 22 21 20 16 15 14 12 11 10 9 5 4 0
7601 * +---+---+-------------+------+---+------+---+------------------+------+
7602 * | 0 | Q | 0 0 1 1 1 0 | size | 0 | Rm | 0 | opc | 1 0 | Rn | Rd |
7603 * +---+---+-------------+------+---+------+---+------------------+------+
7605 static void disas_simd_zip_trn(DisasContext *s, uint32_t insn)
7607 int rd = extract32(insn, 0, 5);
7608 int rn = extract32(insn, 5, 5);
7609 int rm = extract32(insn, 16, 5);
7610 int size = extract32(insn, 22, 2);
7611 /* opc field bits [1:0] indicate ZIP/UZP/TRN;
7612 * bit 2 indicates 1 vs 2 variant of the insn.
7614 int opcode = extract32(insn, 12, 2);
7615 bool part = extract32(insn, 14, 1);
7616 bool is_q = extract32(insn, 30, 1);
7617 int esize = 8 << size;
7618 int i, ofs;
7619 int datasize = is_q ? 128 : 64;
7620 int elements = datasize / esize;
7621 TCGv_i64 tcg_res, tcg_resl, tcg_resh;
7623 if (opcode == 0 || (size == 3 && !is_q)) {
7624 unallocated_encoding(s);
7625 return;
7628 if (!fp_access_check(s)) {
7629 return;
7632 tcg_resl = tcg_const_i64(0);
7633 tcg_resh = is_q ? tcg_const_i64(0) : NULL;
7634 tcg_res = tcg_temp_new_i64();
7636 for (i = 0; i < elements; i++) {
7637 switch (opcode) {
7638 case 1: /* UZP1/2 */
7640 int midpoint = elements / 2;
7641 if (i < midpoint) {
7642 read_vec_element(s, tcg_res, rn, 2 * i + part, size);
7643 } else {
7644 read_vec_element(s, tcg_res, rm,
7645 2 * (i - midpoint) + part, size);
7647 break;
7649 case 2: /* TRN1/2 */
7650 if (i & 1) {
7651 read_vec_element(s, tcg_res, rm, (i & ~1) + part, size);
7652 } else {
7653 read_vec_element(s, tcg_res, rn, (i & ~1) + part, size);
7655 break;
7656 case 3: /* ZIP1/2 */
7658 int base = part * elements / 2;
7659 if (i & 1) {
7660 read_vec_element(s, tcg_res, rm, base + (i >> 1), size);
7661 } else {
7662 read_vec_element(s, tcg_res, rn, base + (i >> 1), size);
7664 break;
7666 default:
7667 g_assert_not_reached();
7670 ofs = i * esize;
7671 if (ofs < 64) {
7672 tcg_gen_shli_i64(tcg_res, tcg_res, ofs);
7673 tcg_gen_or_i64(tcg_resl, tcg_resl, tcg_res);
7674 } else {
7675 tcg_gen_shli_i64(tcg_res, tcg_res, ofs - 64);
7676 tcg_gen_or_i64(tcg_resh, tcg_resh, tcg_res);
7680 tcg_temp_free_i64(tcg_res);
7682 write_vec_element(s, tcg_resl, rd, 0, MO_64);
7683 tcg_temp_free_i64(tcg_resl);
7685 if (is_q) {
7686 write_vec_element(s, tcg_resh, rd, 1, MO_64);
7687 tcg_temp_free_i64(tcg_resh);
7689 clear_vec_high(s, is_q, rd);
7693 * do_reduction_op helper
7695 * This mirrors the Reduce() pseudocode in the ARM ARM. It is
7696 * important for correct NaN propagation that we do these
7697 * operations in exactly the order specified by the pseudocode.
7699 * This is a recursive function, TCG temps should be freed by the
7700 * calling function once it is done with the values.
7702 static TCGv_i32 do_reduction_op(DisasContext *s, int fpopcode, int rn,
7703 int esize, int size, int vmap, TCGv_ptr fpst)
7705 if (esize == size) {
7706 int element;
7707 MemOp msize = esize == 16 ? MO_16 : MO_32;
7708 TCGv_i32 tcg_elem;
7710 /* We should have one register left here */
7711 assert(ctpop8(vmap) == 1);
7712 element = ctz32(vmap);
7713 assert(element < 8);
7715 tcg_elem = tcg_temp_new_i32();
7716 read_vec_element_i32(s, tcg_elem, rn, element, msize);
7717 return tcg_elem;
7718 } else {
7719 int bits = size / 2;
7720 int shift = ctpop8(vmap) / 2;
7721 int vmap_lo = (vmap >> shift) & vmap;
7722 int vmap_hi = (vmap & ~vmap_lo);
7723 TCGv_i32 tcg_hi, tcg_lo, tcg_res;
7725 tcg_hi = do_reduction_op(s, fpopcode, rn, esize, bits, vmap_hi, fpst);
7726 tcg_lo = do_reduction_op(s, fpopcode, rn, esize, bits, vmap_lo, fpst);
7727 tcg_res = tcg_temp_new_i32();
7729 switch (fpopcode) {
7730 case 0x0c: /* fmaxnmv half-precision */
7731 gen_helper_advsimd_maxnumh(tcg_res, tcg_lo, tcg_hi, fpst);
7732 break;
7733 case 0x0f: /* fmaxv half-precision */
7734 gen_helper_advsimd_maxh(tcg_res, tcg_lo, tcg_hi, fpst);
7735 break;
7736 case 0x1c: /* fminnmv half-precision */
7737 gen_helper_advsimd_minnumh(tcg_res, tcg_lo, tcg_hi, fpst);
7738 break;
7739 case 0x1f: /* fminv half-precision */
7740 gen_helper_advsimd_minh(tcg_res, tcg_lo, tcg_hi, fpst);
7741 break;
7742 case 0x2c: /* fmaxnmv */
7743 gen_helper_vfp_maxnums(tcg_res, tcg_lo, tcg_hi, fpst);
7744 break;
7745 case 0x2f: /* fmaxv */
7746 gen_helper_vfp_maxs(tcg_res, tcg_lo, tcg_hi, fpst);
7747 break;
7748 case 0x3c: /* fminnmv */
7749 gen_helper_vfp_minnums(tcg_res, tcg_lo, tcg_hi, fpst);
7750 break;
7751 case 0x3f: /* fminv */
7752 gen_helper_vfp_mins(tcg_res, tcg_lo, tcg_hi, fpst);
7753 break;
7754 default:
7755 g_assert_not_reached();
7758 tcg_temp_free_i32(tcg_hi);
7759 tcg_temp_free_i32(tcg_lo);
7760 return tcg_res;
7764 /* AdvSIMD across lanes
7765 * 31 30 29 28 24 23 22 21 17 16 12 11 10 9 5 4 0
7766 * +---+---+---+-----------+------+-----------+--------+-----+------+------+
7767 * | 0 | Q | U | 0 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 | Rn | Rd |
7768 * +---+---+---+-----------+------+-----------+--------+-----+------+------+
7770 static void disas_simd_across_lanes(DisasContext *s, uint32_t insn)
7772 int rd = extract32(insn, 0, 5);
7773 int rn = extract32(insn, 5, 5);
7774 int size = extract32(insn, 22, 2);
7775 int opcode = extract32(insn, 12, 5);
7776 bool is_q = extract32(insn, 30, 1);
7777 bool is_u = extract32(insn, 29, 1);
7778 bool is_fp = false;
7779 bool is_min = false;
7780 int esize;
7781 int elements;
7782 int i;
7783 TCGv_i64 tcg_res, tcg_elt;
7785 switch (opcode) {
7786 case 0x1b: /* ADDV */
7787 if (is_u) {
7788 unallocated_encoding(s);
7789 return;
7791 /* fall through */
7792 case 0x3: /* SADDLV, UADDLV */
7793 case 0xa: /* SMAXV, UMAXV */
7794 case 0x1a: /* SMINV, UMINV */
7795 if (size == 3 || (size == 2 && !is_q)) {
7796 unallocated_encoding(s);
7797 return;
7799 break;
7800 case 0xc: /* FMAXNMV, FMINNMV */
7801 case 0xf: /* FMAXV, FMINV */
7802 /* Bit 1 of size field encodes min vs max and the actual size
7803 * depends on the encoding of the U bit. If not set (and FP16
7804 * enabled) then we do half-precision float instead of single
7805 * precision.
7807 is_min = extract32(size, 1, 1);
7808 is_fp = true;
7809 if (!is_u && dc_isar_feature(aa64_fp16, s)) {
7810 size = 1;
7811 } else if (!is_u || !is_q || extract32(size, 0, 1)) {
7812 unallocated_encoding(s);
7813 return;
7814 } else {
7815 size = 2;
7817 break;
7818 default:
7819 unallocated_encoding(s);
7820 return;
7823 if (!fp_access_check(s)) {
7824 return;
7827 esize = 8 << size;
7828 elements = (is_q ? 128 : 64) / esize;
7830 tcg_res = tcg_temp_new_i64();
7831 tcg_elt = tcg_temp_new_i64();
7833 /* These instructions operate across all lanes of a vector
7834 * to produce a single result. We can guarantee that a 64
7835 * bit intermediate is sufficient:
7836 * + for [US]ADDLV the maximum element size is 32 bits, and
7837 * the result type is 64 bits
7838 * + for FMAX*V, FMIN*V, ADDV the intermediate type is the
7839 * same as the element size, which is 32 bits at most
7840 * For the integer operations we can choose to work at 64
7841 * or 32 bits and truncate at the end; for simplicity
7842 * we use 64 bits always. The floating point
7843 * ops do require 32 bit intermediates, though.
7845 if (!is_fp) {
7846 read_vec_element(s, tcg_res, rn, 0, size | (is_u ? 0 : MO_SIGN));
7848 for (i = 1; i < elements; i++) {
7849 read_vec_element(s, tcg_elt, rn, i, size | (is_u ? 0 : MO_SIGN));
7851 switch (opcode) {
7852 case 0x03: /* SADDLV / UADDLV */
7853 case 0x1b: /* ADDV */
7854 tcg_gen_add_i64(tcg_res, tcg_res, tcg_elt);
7855 break;
7856 case 0x0a: /* SMAXV / UMAXV */
7857 if (is_u) {
7858 tcg_gen_umax_i64(tcg_res, tcg_res, tcg_elt);
7859 } else {
7860 tcg_gen_smax_i64(tcg_res, tcg_res, tcg_elt);
7862 break;
7863 case 0x1a: /* SMINV / UMINV */
7864 if (is_u) {
7865 tcg_gen_umin_i64(tcg_res, tcg_res, tcg_elt);
7866 } else {
7867 tcg_gen_smin_i64(tcg_res, tcg_res, tcg_elt);
7869 break;
7870 default:
7871 g_assert_not_reached();
7875 } else {
7876 /* Floating point vector reduction ops which work across 32
7877 * bit (single) or 16 bit (half-precision) intermediates.
7878 * Note that correct NaN propagation requires that we do these
7879 * operations in exactly the order specified by the pseudocode.
7881 TCGv_ptr fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
7882 int fpopcode = opcode | is_min << 4 | is_u << 5;
7883 int vmap = (1 << elements) - 1;
7884 TCGv_i32 tcg_res32 = do_reduction_op(s, fpopcode, rn, esize,
7885 (is_q ? 128 : 64), vmap, fpst);
7886 tcg_gen_extu_i32_i64(tcg_res, tcg_res32);
7887 tcg_temp_free_i32(tcg_res32);
7888 tcg_temp_free_ptr(fpst);
7891 tcg_temp_free_i64(tcg_elt);
7893 /* Now truncate the result to the width required for the final output */
7894 if (opcode == 0x03) {
7895 /* SADDLV, UADDLV: result is 2*esize */
7896 size++;
7899 switch (size) {
7900 case 0:
7901 tcg_gen_ext8u_i64(tcg_res, tcg_res);
7902 break;
7903 case 1:
7904 tcg_gen_ext16u_i64(tcg_res, tcg_res);
7905 break;
7906 case 2:
7907 tcg_gen_ext32u_i64(tcg_res, tcg_res);
7908 break;
7909 case 3:
7910 break;
7911 default:
7912 g_assert_not_reached();
7915 write_fp_dreg(s, rd, tcg_res);
7916 tcg_temp_free_i64(tcg_res);
7919 /* DUP (Element, Vector)
7921 * 31 30 29 21 20 16 15 10 9 5 4 0
7922 * +---+---+-------------------+--------+-------------+------+------+
7923 * | 0 | Q | 0 0 1 1 1 0 0 0 0 | imm5 | 0 0 0 0 0 1 | Rn | Rd |
7924 * +---+---+-------------------+--------+-------------+------+------+
7926 * size: encoded in imm5 (see ARM ARM LowestSetBit())
7928 static void handle_simd_dupe(DisasContext *s, int is_q, int rd, int rn,
7929 int imm5)
7931 int size = ctz32(imm5);
7932 int index;
7934 if (size > 3 || (size == 3 && !is_q)) {
7935 unallocated_encoding(s);
7936 return;
7939 if (!fp_access_check(s)) {
7940 return;
7943 index = imm5 >> (size + 1);
7944 tcg_gen_gvec_dup_mem(size, vec_full_reg_offset(s, rd),
7945 vec_reg_offset(s, rn, index, size),
7946 is_q ? 16 : 8, vec_full_reg_size(s));
7949 /* DUP (element, scalar)
7950 * 31 21 20 16 15 10 9 5 4 0
7951 * +-----------------------+--------+-------------+------+------+
7952 * | 0 1 0 1 1 1 1 0 0 0 0 | imm5 | 0 0 0 0 0 1 | Rn | Rd |
7953 * +-----------------------+--------+-------------+------+------+
7955 static void handle_simd_dupes(DisasContext *s, int rd, int rn,
7956 int imm5)
7958 int size = ctz32(imm5);
7959 int index;
7960 TCGv_i64 tmp;
7962 if (size > 3) {
7963 unallocated_encoding(s);
7964 return;
7967 if (!fp_access_check(s)) {
7968 return;
7971 index = imm5 >> (size + 1);
7973 /* This instruction just extracts the specified element and
7974 * zero-extends it into the bottom of the destination register.
7976 tmp = tcg_temp_new_i64();
7977 read_vec_element(s, tmp, rn, index, size);
7978 write_fp_dreg(s, rd, tmp);
7979 tcg_temp_free_i64(tmp);
7982 /* DUP (General)
7984 * 31 30 29 21 20 16 15 10 9 5 4 0
7985 * +---+---+-------------------+--------+-------------+------+------+
7986 * | 0 | Q | 0 0 1 1 1 0 0 0 0 | imm5 | 0 0 0 0 1 1 | Rn | Rd |
7987 * +---+---+-------------------+--------+-------------+------+------+
7989 * size: encoded in imm5 (see ARM ARM LowestSetBit())
7991 static void handle_simd_dupg(DisasContext *s, int is_q, int rd, int rn,
7992 int imm5)
7994 int size = ctz32(imm5);
7995 uint32_t dofs, oprsz, maxsz;
7997 if (size > 3 || ((size == 3) && !is_q)) {
7998 unallocated_encoding(s);
7999 return;
8002 if (!fp_access_check(s)) {
8003 return;
8006 dofs = vec_full_reg_offset(s, rd);
8007 oprsz = is_q ? 16 : 8;
8008 maxsz = vec_full_reg_size(s);
8010 tcg_gen_gvec_dup_i64(size, dofs, oprsz, maxsz, cpu_reg(s, rn));
8013 /* INS (Element)
8015 * 31 21 20 16 15 14 11 10 9 5 4 0
8016 * +-----------------------+--------+------------+---+------+------+
8017 * | 0 1 1 0 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 | Rn | Rd |
8018 * +-----------------------+--------+------------+---+------+------+
8020 * size: encoded in imm5 (see ARM ARM LowestSetBit())
8021 * index: encoded in imm5<4:size+1>
8023 static void handle_simd_inse(DisasContext *s, int rd, int rn,
8024 int imm4, int imm5)
8026 int size = ctz32(imm5);
8027 int src_index, dst_index;
8028 TCGv_i64 tmp;
8030 if (size > 3) {
8031 unallocated_encoding(s);
8032 return;
8035 if (!fp_access_check(s)) {
8036 return;
8039 dst_index = extract32(imm5, 1+size, 5);
8040 src_index = extract32(imm4, size, 4);
8042 tmp = tcg_temp_new_i64();
8044 read_vec_element(s, tmp, rn, src_index, size);
8045 write_vec_element(s, tmp, rd, dst_index, size);
8047 tcg_temp_free_i64(tmp);
8049 /* INS is considered a 128-bit write for SVE. */
8050 clear_vec_high(s, true, rd);
8054 /* INS (General)
8056 * 31 21 20 16 15 10 9 5 4 0
8057 * +-----------------------+--------+-------------+------+------+
8058 * | 0 1 0 0 1 1 1 0 0 0 0 | imm5 | 0 0 0 1 1 1 | Rn | Rd |
8059 * +-----------------------+--------+-------------+------+------+
8061 * size: encoded in imm5 (see ARM ARM LowestSetBit())
8062 * index: encoded in imm5<4:size+1>
8064 static void handle_simd_insg(DisasContext *s, int rd, int rn, int imm5)
8066 int size = ctz32(imm5);
8067 int idx;
8069 if (size > 3) {
8070 unallocated_encoding(s);
8071 return;
8074 if (!fp_access_check(s)) {
8075 return;
8078 idx = extract32(imm5, 1 + size, 4 - size);
8079 write_vec_element(s, cpu_reg(s, rn), rd, idx, size);
8081 /* INS is considered a 128-bit write for SVE. */
8082 clear_vec_high(s, true, rd);
8086 * UMOV (General)
8087 * SMOV (General)
8089 * 31 30 29 21 20 16 15 12 10 9 5 4 0
8090 * +---+---+-------------------+--------+-------------+------+------+
8091 * | 0 | Q | 0 0 1 1 1 0 0 0 0 | imm5 | 0 0 1 U 1 1 | Rn | Rd |
8092 * +---+---+-------------------+--------+-------------+------+------+
8094 * U: unsigned when set
8095 * size: encoded in imm5 (see ARM ARM LowestSetBit())
8097 static void handle_simd_umov_smov(DisasContext *s, int is_q, int is_signed,
8098 int rn, int rd, int imm5)
8100 int size = ctz32(imm5);
8101 int element;
8102 TCGv_i64 tcg_rd;
8104 /* Check for UnallocatedEncodings */
8105 if (is_signed) {
8106 if (size > 2 || (size == 2 && !is_q)) {
8107 unallocated_encoding(s);
8108 return;
8110 } else {
8111 if (size > 3
8112 || (size < 3 && is_q)
8113 || (size == 3 && !is_q)) {
8114 unallocated_encoding(s);
8115 return;
8119 if (!fp_access_check(s)) {
8120 return;
8123 element = extract32(imm5, 1+size, 4);
8125 tcg_rd = cpu_reg(s, rd);
8126 read_vec_element(s, tcg_rd, rn, element, size | (is_signed ? MO_SIGN : 0));
8127 if (is_signed && !is_q) {
8128 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
8132 /* AdvSIMD copy
8133 * 31 30 29 28 21 20 16 15 14 11 10 9 5 4 0
8134 * +---+---+----+-----------------+------+---+------+---+------+------+
8135 * | 0 | Q | op | 0 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 | Rn | Rd |
8136 * +---+---+----+-----------------+------+---+------+---+------+------+
8138 static void disas_simd_copy(DisasContext *s, uint32_t insn)
8140 int rd = extract32(insn, 0, 5);
8141 int rn = extract32(insn, 5, 5);
8142 int imm4 = extract32(insn, 11, 4);
8143 int op = extract32(insn, 29, 1);
8144 int is_q = extract32(insn, 30, 1);
8145 int imm5 = extract32(insn, 16, 5);
8147 if (op) {
8148 if (is_q) {
8149 /* INS (element) */
8150 handle_simd_inse(s, rd, rn, imm4, imm5);
8151 } else {
8152 unallocated_encoding(s);
8154 } else {
8155 switch (imm4) {
8156 case 0:
8157 /* DUP (element - vector) */
8158 handle_simd_dupe(s, is_q, rd, rn, imm5);
8159 break;
8160 case 1:
8161 /* DUP (general) */
8162 handle_simd_dupg(s, is_q, rd, rn, imm5);
8163 break;
8164 case 3:
8165 if (is_q) {
8166 /* INS (general) */
8167 handle_simd_insg(s, rd, rn, imm5);
8168 } else {
8169 unallocated_encoding(s);
8171 break;
8172 case 5:
8173 case 7:
8174 /* UMOV/SMOV (is_q indicates 32/64; imm4 indicates signedness) */
8175 handle_simd_umov_smov(s, is_q, (imm4 == 5), rn, rd, imm5);
8176 break;
8177 default:
8178 unallocated_encoding(s);
8179 break;
8184 /* AdvSIMD modified immediate
8185 * 31 30 29 28 19 18 16 15 12 11 10 9 5 4 0
8186 * +---+---+----+---------------------+-----+-------+----+---+-------+------+
8187 * | 0 | Q | op | 0 1 1 1 1 0 0 0 0 0 | abc | cmode | o2 | 1 | defgh | Rd |
8188 * +---+---+----+---------------------+-----+-------+----+---+-------+------+
8190 * There are a number of operations that can be carried out here:
8191 * MOVI - move (shifted) imm into register
8192 * MVNI - move inverted (shifted) imm into register
8193 * ORR - bitwise OR of (shifted) imm with register
8194 * BIC - bitwise clear of (shifted) imm with register
8195 * With ARMv8.2 we also have:
8196 * FMOV half-precision
8198 static void disas_simd_mod_imm(DisasContext *s, uint32_t insn)
8200 int rd = extract32(insn, 0, 5);
8201 int cmode = extract32(insn, 12, 4);
8202 int cmode_3_1 = extract32(cmode, 1, 3);
8203 int cmode_0 = extract32(cmode, 0, 1);
8204 int o2 = extract32(insn, 11, 1);
8205 uint64_t abcdefgh = extract32(insn, 5, 5) | (extract32(insn, 16, 3) << 5);
8206 bool is_neg = extract32(insn, 29, 1);
8207 bool is_q = extract32(insn, 30, 1);
8208 uint64_t imm = 0;
8210 if (o2 != 0 || ((cmode == 0xf) && is_neg && !is_q)) {
8211 /* Check for FMOV (vector, immediate) - half-precision */
8212 if (!(dc_isar_feature(aa64_fp16, s) && o2 && cmode == 0xf)) {
8213 unallocated_encoding(s);
8214 return;
8218 if (!fp_access_check(s)) {
8219 return;
8222 /* See AdvSIMDExpandImm() in ARM ARM */
8223 switch (cmode_3_1) {
8224 case 0: /* Replicate(Zeros(24):imm8, 2) */
8225 case 1: /* Replicate(Zeros(16):imm8:Zeros(8), 2) */
8226 case 2: /* Replicate(Zeros(8):imm8:Zeros(16), 2) */
8227 case 3: /* Replicate(imm8:Zeros(24), 2) */
8229 int shift = cmode_3_1 * 8;
8230 imm = bitfield_replicate(abcdefgh << shift, 32);
8231 break;
8233 case 4: /* Replicate(Zeros(8):imm8, 4) */
8234 case 5: /* Replicate(imm8:Zeros(8), 4) */
8236 int shift = (cmode_3_1 & 0x1) * 8;
8237 imm = bitfield_replicate(abcdefgh << shift, 16);
8238 break;
8240 case 6:
8241 if (cmode_0) {
8242 /* Replicate(Zeros(8):imm8:Ones(16), 2) */
8243 imm = (abcdefgh << 16) | 0xffff;
8244 } else {
8245 /* Replicate(Zeros(16):imm8:Ones(8), 2) */
8246 imm = (abcdefgh << 8) | 0xff;
8248 imm = bitfield_replicate(imm, 32);
8249 break;
8250 case 7:
8251 if (!cmode_0 && !is_neg) {
8252 imm = bitfield_replicate(abcdefgh, 8);
8253 } else if (!cmode_0 && is_neg) {
8254 int i;
8255 imm = 0;
8256 for (i = 0; i < 8; i++) {
8257 if ((abcdefgh) & (1 << i)) {
8258 imm |= 0xffULL << (i * 8);
8261 } else if (cmode_0) {
8262 if (is_neg) {
8263 imm = (abcdefgh & 0x3f) << 48;
8264 if (abcdefgh & 0x80) {
8265 imm |= 0x8000000000000000ULL;
8267 if (abcdefgh & 0x40) {
8268 imm |= 0x3fc0000000000000ULL;
8269 } else {
8270 imm |= 0x4000000000000000ULL;
8272 } else {
8273 if (o2) {
8274 /* FMOV (vector, immediate) - half-precision */
8275 imm = vfp_expand_imm(MO_16, abcdefgh);
8276 /* now duplicate across the lanes */
8277 imm = bitfield_replicate(imm, 16);
8278 } else {
8279 imm = (abcdefgh & 0x3f) << 19;
8280 if (abcdefgh & 0x80) {
8281 imm |= 0x80000000;
8283 if (abcdefgh & 0x40) {
8284 imm |= 0x3e000000;
8285 } else {
8286 imm |= 0x40000000;
8288 imm |= (imm << 32);
8292 break;
8293 default:
8294 fprintf(stderr, "%s: cmode_3_1: %x\n", __func__, cmode_3_1);
8295 g_assert_not_reached();
8298 if (cmode_3_1 != 7 && is_neg) {
8299 imm = ~imm;
8302 if (!((cmode & 0x9) == 0x1 || (cmode & 0xd) == 0x9)) {
8303 /* MOVI or MVNI, with MVNI negation handled above. */
8304 tcg_gen_gvec_dup_imm(MO_64, vec_full_reg_offset(s, rd), is_q ? 16 : 8,
8305 vec_full_reg_size(s), imm);
8306 } else {
8307 /* ORR or BIC, with BIC negation to AND handled above. */
8308 if (is_neg) {
8309 gen_gvec_fn2i(s, is_q, rd, rd, imm, tcg_gen_gvec_andi, MO_64);
8310 } else {
8311 gen_gvec_fn2i(s, is_q, rd, rd, imm, tcg_gen_gvec_ori, MO_64);
8316 /* AdvSIMD scalar copy
8317 * 31 30 29 28 21 20 16 15 14 11 10 9 5 4 0
8318 * +-----+----+-----------------+------+---+------+---+------+------+
8319 * | 0 1 | op | 1 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 | Rn | Rd |
8320 * +-----+----+-----------------+------+---+------+---+------+------+
8322 static void disas_simd_scalar_copy(DisasContext *s, uint32_t insn)
8324 int rd = extract32(insn, 0, 5);
8325 int rn = extract32(insn, 5, 5);
8326 int imm4 = extract32(insn, 11, 4);
8327 int imm5 = extract32(insn, 16, 5);
8328 int op = extract32(insn, 29, 1);
8330 if (op != 0 || imm4 != 0) {
8331 unallocated_encoding(s);
8332 return;
8335 /* DUP (element, scalar) */
8336 handle_simd_dupes(s, rd, rn, imm5);
8339 /* AdvSIMD scalar pairwise
8340 * 31 30 29 28 24 23 22 21 17 16 12 11 10 9 5 4 0
8341 * +-----+---+-----------+------+-----------+--------+-----+------+------+
8342 * | 0 1 | U | 1 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 | Rn | Rd |
8343 * +-----+---+-----------+------+-----------+--------+-----+------+------+
8345 static void disas_simd_scalar_pairwise(DisasContext *s, uint32_t insn)
8347 int u = extract32(insn, 29, 1);
8348 int size = extract32(insn, 22, 2);
8349 int opcode = extract32(insn, 12, 5);
8350 int rn = extract32(insn, 5, 5);
8351 int rd = extract32(insn, 0, 5);
8352 TCGv_ptr fpst;
8354 /* For some ops (the FP ones), size[1] is part of the encoding.
8355 * For ADDP strictly it is not but size[1] is always 1 for valid
8356 * encodings.
8358 opcode |= (extract32(size, 1, 1) << 5);
8360 switch (opcode) {
8361 case 0x3b: /* ADDP */
8362 if (u || size != 3) {
8363 unallocated_encoding(s);
8364 return;
8366 if (!fp_access_check(s)) {
8367 return;
8370 fpst = NULL;
8371 break;
8372 case 0xc: /* FMAXNMP */
8373 case 0xd: /* FADDP */
8374 case 0xf: /* FMAXP */
8375 case 0x2c: /* FMINNMP */
8376 case 0x2f: /* FMINP */
8377 /* FP op, size[0] is 32 or 64 bit*/
8378 if (!u) {
8379 if (!dc_isar_feature(aa64_fp16, s)) {
8380 unallocated_encoding(s);
8381 return;
8382 } else {
8383 size = MO_16;
8385 } else {
8386 size = extract32(size, 0, 1) ? MO_64 : MO_32;
8389 if (!fp_access_check(s)) {
8390 return;
8393 fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
8394 break;
8395 default:
8396 unallocated_encoding(s);
8397 return;
8400 if (size == MO_64) {
8401 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8402 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
8403 TCGv_i64 tcg_res = tcg_temp_new_i64();
8405 read_vec_element(s, tcg_op1, rn, 0, MO_64);
8406 read_vec_element(s, tcg_op2, rn, 1, MO_64);
8408 switch (opcode) {
8409 case 0x3b: /* ADDP */
8410 tcg_gen_add_i64(tcg_res, tcg_op1, tcg_op2);
8411 break;
8412 case 0xc: /* FMAXNMP */
8413 gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
8414 break;
8415 case 0xd: /* FADDP */
8416 gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
8417 break;
8418 case 0xf: /* FMAXP */
8419 gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
8420 break;
8421 case 0x2c: /* FMINNMP */
8422 gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
8423 break;
8424 case 0x2f: /* FMINP */
8425 gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
8426 break;
8427 default:
8428 g_assert_not_reached();
8431 write_fp_dreg(s, rd, tcg_res);
8433 tcg_temp_free_i64(tcg_op1);
8434 tcg_temp_free_i64(tcg_op2);
8435 tcg_temp_free_i64(tcg_res);
8436 } else {
8437 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
8438 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
8439 TCGv_i32 tcg_res = tcg_temp_new_i32();
8441 read_vec_element_i32(s, tcg_op1, rn, 0, size);
8442 read_vec_element_i32(s, tcg_op2, rn, 1, size);
8444 if (size == MO_16) {
8445 switch (opcode) {
8446 case 0xc: /* FMAXNMP */
8447 gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
8448 break;
8449 case 0xd: /* FADDP */
8450 gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
8451 break;
8452 case 0xf: /* FMAXP */
8453 gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
8454 break;
8455 case 0x2c: /* FMINNMP */
8456 gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
8457 break;
8458 case 0x2f: /* FMINP */
8459 gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
8460 break;
8461 default:
8462 g_assert_not_reached();
8464 } else {
8465 switch (opcode) {
8466 case 0xc: /* FMAXNMP */
8467 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
8468 break;
8469 case 0xd: /* FADDP */
8470 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
8471 break;
8472 case 0xf: /* FMAXP */
8473 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
8474 break;
8475 case 0x2c: /* FMINNMP */
8476 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
8477 break;
8478 case 0x2f: /* FMINP */
8479 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
8480 break;
8481 default:
8482 g_assert_not_reached();
8486 write_fp_sreg(s, rd, tcg_res);
8488 tcg_temp_free_i32(tcg_op1);
8489 tcg_temp_free_i32(tcg_op2);
8490 tcg_temp_free_i32(tcg_res);
8493 if (fpst) {
8494 tcg_temp_free_ptr(fpst);
8499 * Common SSHR[RA]/USHR[RA] - Shift right (optional rounding/accumulate)
8501 * This code is handles the common shifting code and is used by both
8502 * the vector and scalar code.
8504 static void handle_shri_with_rndacc(TCGv_i64 tcg_res, TCGv_i64 tcg_src,
8505 TCGv_i64 tcg_rnd, bool accumulate,
8506 bool is_u, int size, int shift)
8508 bool extended_result = false;
8509 bool round = tcg_rnd != NULL;
8510 int ext_lshift = 0;
8511 TCGv_i64 tcg_src_hi;
8513 if (round && size == 3) {
8514 extended_result = true;
8515 ext_lshift = 64 - shift;
8516 tcg_src_hi = tcg_temp_new_i64();
8517 } else if (shift == 64) {
8518 if (!accumulate && is_u) {
8519 /* result is zero */
8520 tcg_gen_movi_i64(tcg_res, 0);
8521 return;
8525 /* Deal with the rounding step */
8526 if (round) {
8527 if (extended_result) {
8528 TCGv_i64 tcg_zero = tcg_const_i64(0);
8529 if (!is_u) {
8530 /* take care of sign extending tcg_res */
8531 tcg_gen_sari_i64(tcg_src_hi, tcg_src, 63);
8532 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
8533 tcg_src, tcg_src_hi,
8534 tcg_rnd, tcg_zero);
8535 } else {
8536 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
8537 tcg_src, tcg_zero,
8538 tcg_rnd, tcg_zero);
8540 tcg_temp_free_i64(tcg_zero);
8541 } else {
8542 tcg_gen_add_i64(tcg_src, tcg_src, tcg_rnd);
8546 /* Now do the shift right */
8547 if (round && extended_result) {
8548 /* extended case, >64 bit precision required */
8549 if (ext_lshift == 0) {
8550 /* special case, only high bits matter */
8551 tcg_gen_mov_i64(tcg_src, tcg_src_hi);
8552 } else {
8553 tcg_gen_shri_i64(tcg_src, tcg_src, shift);
8554 tcg_gen_shli_i64(tcg_src_hi, tcg_src_hi, ext_lshift);
8555 tcg_gen_or_i64(tcg_src, tcg_src, tcg_src_hi);
8557 } else {
8558 if (is_u) {
8559 if (shift == 64) {
8560 /* essentially shifting in 64 zeros */
8561 tcg_gen_movi_i64(tcg_src, 0);
8562 } else {
8563 tcg_gen_shri_i64(tcg_src, tcg_src, shift);
8565 } else {
8566 if (shift == 64) {
8567 /* effectively extending the sign-bit */
8568 tcg_gen_sari_i64(tcg_src, tcg_src, 63);
8569 } else {
8570 tcg_gen_sari_i64(tcg_src, tcg_src, shift);
8575 if (accumulate) {
8576 tcg_gen_add_i64(tcg_res, tcg_res, tcg_src);
8577 } else {
8578 tcg_gen_mov_i64(tcg_res, tcg_src);
8581 if (extended_result) {
8582 tcg_temp_free_i64(tcg_src_hi);
8586 /* SSHR[RA]/USHR[RA] - Scalar shift right (optional rounding/accumulate) */
8587 static void handle_scalar_simd_shri(DisasContext *s,
8588 bool is_u, int immh, int immb,
8589 int opcode, int rn, int rd)
8591 const int size = 3;
8592 int immhb = immh << 3 | immb;
8593 int shift = 2 * (8 << size) - immhb;
8594 bool accumulate = false;
8595 bool round = false;
8596 bool insert = false;
8597 TCGv_i64 tcg_rn;
8598 TCGv_i64 tcg_rd;
8599 TCGv_i64 tcg_round;
8601 if (!extract32(immh, 3, 1)) {
8602 unallocated_encoding(s);
8603 return;
8606 if (!fp_access_check(s)) {
8607 return;
8610 switch (opcode) {
8611 case 0x02: /* SSRA / USRA (accumulate) */
8612 accumulate = true;
8613 break;
8614 case 0x04: /* SRSHR / URSHR (rounding) */
8615 round = true;
8616 break;
8617 case 0x06: /* SRSRA / URSRA (accum + rounding) */
8618 accumulate = round = true;
8619 break;
8620 case 0x08: /* SRI */
8621 insert = true;
8622 break;
8625 if (round) {
8626 uint64_t round_const = 1ULL << (shift - 1);
8627 tcg_round = tcg_const_i64(round_const);
8628 } else {
8629 tcg_round = NULL;
8632 tcg_rn = read_fp_dreg(s, rn);
8633 tcg_rd = (accumulate || insert) ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
8635 if (insert) {
8636 /* shift count same as element size is valid but does nothing;
8637 * special case to avoid potential shift by 64.
8639 int esize = 8 << size;
8640 if (shift != esize) {
8641 tcg_gen_shri_i64(tcg_rn, tcg_rn, shift);
8642 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, 0, esize - shift);
8644 } else {
8645 handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
8646 accumulate, is_u, size, shift);
8649 write_fp_dreg(s, rd, tcg_rd);
8651 tcg_temp_free_i64(tcg_rn);
8652 tcg_temp_free_i64(tcg_rd);
8653 if (round) {
8654 tcg_temp_free_i64(tcg_round);
8658 /* SHL/SLI - Scalar shift left */
8659 static void handle_scalar_simd_shli(DisasContext *s, bool insert,
8660 int immh, int immb, int opcode,
8661 int rn, int rd)
8663 int size = 32 - clz32(immh) - 1;
8664 int immhb = immh << 3 | immb;
8665 int shift = immhb - (8 << size);
8666 TCGv_i64 tcg_rn;
8667 TCGv_i64 tcg_rd;
8669 if (!extract32(immh, 3, 1)) {
8670 unallocated_encoding(s);
8671 return;
8674 if (!fp_access_check(s)) {
8675 return;
8678 tcg_rn = read_fp_dreg(s, rn);
8679 tcg_rd = insert ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
8681 if (insert) {
8682 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, shift, 64 - shift);
8683 } else {
8684 tcg_gen_shli_i64(tcg_rd, tcg_rn, shift);
8687 write_fp_dreg(s, rd, tcg_rd);
8689 tcg_temp_free_i64(tcg_rn);
8690 tcg_temp_free_i64(tcg_rd);
8693 /* SQSHRN/SQSHRUN - Saturating (signed/unsigned) shift right with
8694 * (signed/unsigned) narrowing */
8695 static void handle_vec_simd_sqshrn(DisasContext *s, bool is_scalar, bool is_q,
8696 bool is_u_shift, bool is_u_narrow,
8697 int immh, int immb, int opcode,
8698 int rn, int rd)
8700 int immhb = immh << 3 | immb;
8701 int size = 32 - clz32(immh) - 1;
8702 int esize = 8 << size;
8703 int shift = (2 * esize) - immhb;
8704 int elements = is_scalar ? 1 : (64 / esize);
8705 bool round = extract32(opcode, 0, 1);
8706 MemOp ldop = (size + 1) | (is_u_shift ? 0 : MO_SIGN);
8707 TCGv_i64 tcg_rn, tcg_rd, tcg_round;
8708 TCGv_i32 tcg_rd_narrowed;
8709 TCGv_i64 tcg_final;
8711 static NeonGenNarrowEnvFn * const signed_narrow_fns[4][2] = {
8712 { gen_helper_neon_narrow_sat_s8,
8713 gen_helper_neon_unarrow_sat8 },
8714 { gen_helper_neon_narrow_sat_s16,
8715 gen_helper_neon_unarrow_sat16 },
8716 { gen_helper_neon_narrow_sat_s32,
8717 gen_helper_neon_unarrow_sat32 },
8718 { NULL, NULL },
8720 static NeonGenNarrowEnvFn * const unsigned_narrow_fns[4] = {
8721 gen_helper_neon_narrow_sat_u8,
8722 gen_helper_neon_narrow_sat_u16,
8723 gen_helper_neon_narrow_sat_u32,
8724 NULL
8726 NeonGenNarrowEnvFn *narrowfn;
8728 int i;
8730 assert(size < 4);
8732 if (extract32(immh, 3, 1)) {
8733 unallocated_encoding(s);
8734 return;
8737 if (!fp_access_check(s)) {
8738 return;
8741 if (is_u_shift) {
8742 narrowfn = unsigned_narrow_fns[size];
8743 } else {
8744 narrowfn = signed_narrow_fns[size][is_u_narrow ? 1 : 0];
8747 tcg_rn = tcg_temp_new_i64();
8748 tcg_rd = tcg_temp_new_i64();
8749 tcg_rd_narrowed = tcg_temp_new_i32();
8750 tcg_final = tcg_const_i64(0);
8752 if (round) {
8753 uint64_t round_const = 1ULL << (shift - 1);
8754 tcg_round = tcg_const_i64(round_const);
8755 } else {
8756 tcg_round = NULL;
8759 for (i = 0; i < elements; i++) {
8760 read_vec_element(s, tcg_rn, rn, i, ldop);
8761 handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
8762 false, is_u_shift, size+1, shift);
8763 narrowfn(tcg_rd_narrowed, cpu_env, tcg_rd);
8764 tcg_gen_extu_i32_i64(tcg_rd, tcg_rd_narrowed);
8765 tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
8768 if (!is_q) {
8769 write_vec_element(s, tcg_final, rd, 0, MO_64);
8770 } else {
8771 write_vec_element(s, tcg_final, rd, 1, MO_64);
8774 if (round) {
8775 tcg_temp_free_i64(tcg_round);
8777 tcg_temp_free_i64(tcg_rn);
8778 tcg_temp_free_i64(tcg_rd);
8779 tcg_temp_free_i32(tcg_rd_narrowed);
8780 tcg_temp_free_i64(tcg_final);
8782 clear_vec_high(s, is_q, rd);
8785 /* SQSHLU, UQSHL, SQSHL: saturating left shifts */
8786 static void handle_simd_qshl(DisasContext *s, bool scalar, bool is_q,
8787 bool src_unsigned, bool dst_unsigned,
8788 int immh, int immb, int rn, int rd)
8790 int immhb = immh << 3 | immb;
8791 int size = 32 - clz32(immh) - 1;
8792 int shift = immhb - (8 << size);
8793 int pass;
8795 assert(immh != 0);
8796 assert(!(scalar && is_q));
8798 if (!scalar) {
8799 if (!is_q && extract32(immh, 3, 1)) {
8800 unallocated_encoding(s);
8801 return;
8804 /* Since we use the variable-shift helpers we must
8805 * replicate the shift count into each element of
8806 * the tcg_shift value.
8808 switch (size) {
8809 case 0:
8810 shift |= shift << 8;
8811 /* fall through */
8812 case 1:
8813 shift |= shift << 16;
8814 break;
8815 case 2:
8816 case 3:
8817 break;
8818 default:
8819 g_assert_not_reached();
8823 if (!fp_access_check(s)) {
8824 return;
8827 if (size == 3) {
8828 TCGv_i64 tcg_shift = tcg_const_i64(shift);
8829 static NeonGenTwo64OpEnvFn * const fns[2][2] = {
8830 { gen_helper_neon_qshl_s64, gen_helper_neon_qshlu_s64 },
8831 { NULL, gen_helper_neon_qshl_u64 },
8833 NeonGenTwo64OpEnvFn *genfn = fns[src_unsigned][dst_unsigned];
8834 int maxpass = is_q ? 2 : 1;
8836 for (pass = 0; pass < maxpass; pass++) {
8837 TCGv_i64 tcg_op = tcg_temp_new_i64();
8839 read_vec_element(s, tcg_op, rn, pass, MO_64);
8840 genfn(tcg_op, cpu_env, tcg_op, tcg_shift);
8841 write_vec_element(s, tcg_op, rd, pass, MO_64);
8843 tcg_temp_free_i64(tcg_op);
8845 tcg_temp_free_i64(tcg_shift);
8846 clear_vec_high(s, is_q, rd);
8847 } else {
8848 TCGv_i32 tcg_shift = tcg_const_i32(shift);
8849 static NeonGenTwoOpEnvFn * const fns[2][2][3] = {
8851 { gen_helper_neon_qshl_s8,
8852 gen_helper_neon_qshl_s16,
8853 gen_helper_neon_qshl_s32 },
8854 { gen_helper_neon_qshlu_s8,
8855 gen_helper_neon_qshlu_s16,
8856 gen_helper_neon_qshlu_s32 }
8857 }, {
8858 { NULL, NULL, NULL },
8859 { gen_helper_neon_qshl_u8,
8860 gen_helper_neon_qshl_u16,
8861 gen_helper_neon_qshl_u32 }
8864 NeonGenTwoOpEnvFn *genfn = fns[src_unsigned][dst_unsigned][size];
8865 MemOp memop = scalar ? size : MO_32;
8866 int maxpass = scalar ? 1 : is_q ? 4 : 2;
8868 for (pass = 0; pass < maxpass; pass++) {
8869 TCGv_i32 tcg_op = tcg_temp_new_i32();
8871 read_vec_element_i32(s, tcg_op, rn, pass, memop);
8872 genfn(tcg_op, cpu_env, tcg_op, tcg_shift);
8873 if (scalar) {
8874 switch (size) {
8875 case 0:
8876 tcg_gen_ext8u_i32(tcg_op, tcg_op);
8877 break;
8878 case 1:
8879 tcg_gen_ext16u_i32(tcg_op, tcg_op);
8880 break;
8881 case 2:
8882 break;
8883 default:
8884 g_assert_not_reached();
8886 write_fp_sreg(s, rd, tcg_op);
8887 } else {
8888 write_vec_element_i32(s, tcg_op, rd, pass, MO_32);
8891 tcg_temp_free_i32(tcg_op);
8893 tcg_temp_free_i32(tcg_shift);
8895 if (!scalar) {
8896 clear_vec_high(s, is_q, rd);
8901 /* Common vector code for handling integer to FP conversion */
8902 static void handle_simd_intfp_conv(DisasContext *s, int rd, int rn,
8903 int elements, int is_signed,
8904 int fracbits, int size)
8906 TCGv_ptr tcg_fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
8907 TCGv_i32 tcg_shift = NULL;
8909 MemOp mop = size | (is_signed ? MO_SIGN : 0);
8910 int pass;
8912 if (fracbits || size == MO_64) {
8913 tcg_shift = tcg_const_i32(fracbits);
8916 if (size == MO_64) {
8917 TCGv_i64 tcg_int64 = tcg_temp_new_i64();
8918 TCGv_i64 tcg_double = tcg_temp_new_i64();
8920 for (pass = 0; pass < elements; pass++) {
8921 read_vec_element(s, tcg_int64, rn, pass, mop);
8923 if (is_signed) {
8924 gen_helper_vfp_sqtod(tcg_double, tcg_int64,
8925 tcg_shift, tcg_fpst);
8926 } else {
8927 gen_helper_vfp_uqtod(tcg_double, tcg_int64,
8928 tcg_shift, tcg_fpst);
8930 if (elements == 1) {
8931 write_fp_dreg(s, rd, tcg_double);
8932 } else {
8933 write_vec_element(s, tcg_double, rd, pass, MO_64);
8937 tcg_temp_free_i64(tcg_int64);
8938 tcg_temp_free_i64(tcg_double);
8940 } else {
8941 TCGv_i32 tcg_int32 = tcg_temp_new_i32();
8942 TCGv_i32 tcg_float = tcg_temp_new_i32();
8944 for (pass = 0; pass < elements; pass++) {
8945 read_vec_element_i32(s, tcg_int32, rn, pass, mop);
8947 switch (size) {
8948 case MO_32:
8949 if (fracbits) {
8950 if (is_signed) {
8951 gen_helper_vfp_sltos(tcg_float, tcg_int32,
8952 tcg_shift, tcg_fpst);
8953 } else {
8954 gen_helper_vfp_ultos(tcg_float, tcg_int32,
8955 tcg_shift, tcg_fpst);
8957 } else {
8958 if (is_signed) {
8959 gen_helper_vfp_sitos(tcg_float, tcg_int32, tcg_fpst);
8960 } else {
8961 gen_helper_vfp_uitos(tcg_float, tcg_int32, tcg_fpst);
8964 break;
8965 case MO_16:
8966 if (fracbits) {
8967 if (is_signed) {
8968 gen_helper_vfp_sltoh(tcg_float, tcg_int32,
8969 tcg_shift, tcg_fpst);
8970 } else {
8971 gen_helper_vfp_ultoh(tcg_float, tcg_int32,
8972 tcg_shift, tcg_fpst);
8974 } else {
8975 if (is_signed) {
8976 gen_helper_vfp_sitoh(tcg_float, tcg_int32, tcg_fpst);
8977 } else {
8978 gen_helper_vfp_uitoh(tcg_float, tcg_int32, tcg_fpst);
8981 break;
8982 default:
8983 g_assert_not_reached();
8986 if (elements == 1) {
8987 write_fp_sreg(s, rd, tcg_float);
8988 } else {
8989 write_vec_element_i32(s, tcg_float, rd, pass, size);
8993 tcg_temp_free_i32(tcg_int32);
8994 tcg_temp_free_i32(tcg_float);
8997 tcg_temp_free_ptr(tcg_fpst);
8998 if (tcg_shift) {
8999 tcg_temp_free_i32(tcg_shift);
9002 clear_vec_high(s, elements << size == 16, rd);
9005 /* UCVTF/SCVTF - Integer to FP conversion */
9006 static void handle_simd_shift_intfp_conv(DisasContext *s, bool is_scalar,
9007 bool is_q, bool is_u,
9008 int immh, int immb, int opcode,
9009 int rn, int rd)
9011 int size, elements, fracbits;
9012 int immhb = immh << 3 | immb;
9014 if (immh & 8) {
9015 size = MO_64;
9016 if (!is_scalar && !is_q) {
9017 unallocated_encoding(s);
9018 return;
9020 } else if (immh & 4) {
9021 size = MO_32;
9022 } else if (immh & 2) {
9023 size = MO_16;
9024 if (!dc_isar_feature(aa64_fp16, s)) {
9025 unallocated_encoding(s);
9026 return;
9028 } else {
9029 /* immh == 0 would be a failure of the decode logic */
9030 g_assert(immh == 1);
9031 unallocated_encoding(s);
9032 return;
9035 if (is_scalar) {
9036 elements = 1;
9037 } else {
9038 elements = (8 << is_q) >> size;
9040 fracbits = (16 << size) - immhb;
9042 if (!fp_access_check(s)) {
9043 return;
9046 handle_simd_intfp_conv(s, rd, rn, elements, !is_u, fracbits, size);
9049 /* FCVTZS, FVCVTZU - FP to fixedpoint conversion */
9050 static void handle_simd_shift_fpint_conv(DisasContext *s, bool is_scalar,
9051 bool is_q, bool is_u,
9052 int immh, int immb, int rn, int rd)
9054 int immhb = immh << 3 | immb;
9055 int pass, size, fracbits;
9056 TCGv_ptr tcg_fpstatus;
9057 TCGv_i32 tcg_rmode, tcg_shift;
9059 if (immh & 0x8) {
9060 size = MO_64;
9061 if (!is_scalar && !is_q) {
9062 unallocated_encoding(s);
9063 return;
9065 } else if (immh & 0x4) {
9066 size = MO_32;
9067 } else if (immh & 0x2) {
9068 size = MO_16;
9069 if (!dc_isar_feature(aa64_fp16, s)) {
9070 unallocated_encoding(s);
9071 return;
9073 } else {
9074 /* Should have split out AdvSIMD modified immediate earlier. */
9075 assert(immh == 1);
9076 unallocated_encoding(s);
9077 return;
9080 if (!fp_access_check(s)) {
9081 return;
9084 assert(!(is_scalar && is_q));
9086 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(FPROUNDING_ZERO));
9087 tcg_fpstatus = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
9088 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
9089 fracbits = (16 << size) - immhb;
9090 tcg_shift = tcg_const_i32(fracbits);
9092 if (size == MO_64) {
9093 int maxpass = is_scalar ? 1 : 2;
9095 for (pass = 0; pass < maxpass; pass++) {
9096 TCGv_i64 tcg_op = tcg_temp_new_i64();
9098 read_vec_element(s, tcg_op, rn, pass, MO_64);
9099 if (is_u) {
9100 gen_helper_vfp_touqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
9101 } else {
9102 gen_helper_vfp_tosqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
9104 write_vec_element(s, tcg_op, rd, pass, MO_64);
9105 tcg_temp_free_i64(tcg_op);
9107 clear_vec_high(s, is_q, rd);
9108 } else {
9109 void (*fn)(TCGv_i32, TCGv_i32, TCGv_i32, TCGv_ptr);
9110 int maxpass = is_scalar ? 1 : ((8 << is_q) >> size);
9112 switch (size) {
9113 case MO_16:
9114 if (is_u) {
9115 fn = gen_helper_vfp_touhh;
9116 } else {
9117 fn = gen_helper_vfp_toshh;
9119 break;
9120 case MO_32:
9121 if (is_u) {
9122 fn = gen_helper_vfp_touls;
9123 } else {
9124 fn = gen_helper_vfp_tosls;
9126 break;
9127 default:
9128 g_assert_not_reached();
9131 for (pass = 0; pass < maxpass; pass++) {
9132 TCGv_i32 tcg_op = tcg_temp_new_i32();
9134 read_vec_element_i32(s, tcg_op, rn, pass, size);
9135 fn(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
9136 if (is_scalar) {
9137 write_fp_sreg(s, rd, tcg_op);
9138 } else {
9139 write_vec_element_i32(s, tcg_op, rd, pass, size);
9141 tcg_temp_free_i32(tcg_op);
9143 if (!is_scalar) {
9144 clear_vec_high(s, is_q, rd);
9148 tcg_temp_free_ptr(tcg_fpstatus);
9149 tcg_temp_free_i32(tcg_shift);
9150 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
9151 tcg_temp_free_i32(tcg_rmode);
9154 /* AdvSIMD scalar shift by immediate
9155 * 31 30 29 28 23 22 19 18 16 15 11 10 9 5 4 0
9156 * +-----+---+-------------+------+------+--------+---+------+------+
9157 * | 0 1 | U | 1 1 1 1 1 0 | immh | immb | opcode | 1 | Rn | Rd |
9158 * +-----+---+-------------+------+------+--------+---+------+------+
9160 * This is the scalar version so it works on a fixed sized registers
9162 static void disas_simd_scalar_shift_imm(DisasContext *s, uint32_t insn)
9164 int rd = extract32(insn, 0, 5);
9165 int rn = extract32(insn, 5, 5);
9166 int opcode = extract32(insn, 11, 5);
9167 int immb = extract32(insn, 16, 3);
9168 int immh = extract32(insn, 19, 4);
9169 bool is_u = extract32(insn, 29, 1);
9171 if (immh == 0) {
9172 unallocated_encoding(s);
9173 return;
9176 switch (opcode) {
9177 case 0x08: /* SRI */
9178 if (!is_u) {
9179 unallocated_encoding(s);
9180 return;
9182 /* fall through */
9183 case 0x00: /* SSHR / USHR */
9184 case 0x02: /* SSRA / USRA */
9185 case 0x04: /* SRSHR / URSHR */
9186 case 0x06: /* SRSRA / URSRA */
9187 handle_scalar_simd_shri(s, is_u, immh, immb, opcode, rn, rd);
9188 break;
9189 case 0x0a: /* SHL / SLI */
9190 handle_scalar_simd_shli(s, is_u, immh, immb, opcode, rn, rd);
9191 break;
9192 case 0x1c: /* SCVTF, UCVTF */
9193 handle_simd_shift_intfp_conv(s, true, false, is_u, immh, immb,
9194 opcode, rn, rd);
9195 break;
9196 case 0x10: /* SQSHRUN, SQSHRUN2 */
9197 case 0x11: /* SQRSHRUN, SQRSHRUN2 */
9198 if (!is_u) {
9199 unallocated_encoding(s);
9200 return;
9202 handle_vec_simd_sqshrn(s, true, false, false, true,
9203 immh, immb, opcode, rn, rd);
9204 break;
9205 case 0x12: /* SQSHRN, SQSHRN2, UQSHRN */
9206 case 0x13: /* SQRSHRN, SQRSHRN2, UQRSHRN, UQRSHRN2 */
9207 handle_vec_simd_sqshrn(s, true, false, is_u, is_u,
9208 immh, immb, opcode, rn, rd);
9209 break;
9210 case 0xc: /* SQSHLU */
9211 if (!is_u) {
9212 unallocated_encoding(s);
9213 return;
9215 handle_simd_qshl(s, true, false, false, true, immh, immb, rn, rd);
9216 break;
9217 case 0xe: /* SQSHL, UQSHL */
9218 handle_simd_qshl(s, true, false, is_u, is_u, immh, immb, rn, rd);
9219 break;
9220 case 0x1f: /* FCVTZS, FCVTZU */
9221 handle_simd_shift_fpint_conv(s, true, false, is_u, immh, immb, rn, rd);
9222 break;
9223 default:
9224 unallocated_encoding(s);
9225 break;
9229 /* AdvSIMD scalar three different
9230 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 0
9231 * +-----+---+-----------+------+---+------+--------+-----+------+------+
9232 * | 0 1 | U | 1 1 1 1 0 | size | 1 | Rm | opcode | 0 0 | Rn | Rd |
9233 * +-----+---+-----------+------+---+------+--------+-----+------+------+
9235 static void disas_simd_scalar_three_reg_diff(DisasContext *s, uint32_t insn)
9237 bool is_u = extract32(insn, 29, 1);
9238 int size = extract32(insn, 22, 2);
9239 int opcode = extract32(insn, 12, 4);
9240 int rm = extract32(insn, 16, 5);
9241 int rn = extract32(insn, 5, 5);
9242 int rd = extract32(insn, 0, 5);
9244 if (is_u) {
9245 unallocated_encoding(s);
9246 return;
9249 switch (opcode) {
9250 case 0x9: /* SQDMLAL, SQDMLAL2 */
9251 case 0xb: /* SQDMLSL, SQDMLSL2 */
9252 case 0xd: /* SQDMULL, SQDMULL2 */
9253 if (size == 0 || size == 3) {
9254 unallocated_encoding(s);
9255 return;
9257 break;
9258 default:
9259 unallocated_encoding(s);
9260 return;
9263 if (!fp_access_check(s)) {
9264 return;
9267 if (size == 2) {
9268 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
9269 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
9270 TCGv_i64 tcg_res = tcg_temp_new_i64();
9272 read_vec_element(s, tcg_op1, rn, 0, MO_32 | MO_SIGN);
9273 read_vec_element(s, tcg_op2, rm, 0, MO_32 | MO_SIGN);
9275 tcg_gen_mul_i64(tcg_res, tcg_op1, tcg_op2);
9276 gen_helper_neon_addl_saturate_s64(tcg_res, cpu_env, tcg_res, tcg_res);
9278 switch (opcode) {
9279 case 0xd: /* SQDMULL, SQDMULL2 */
9280 break;
9281 case 0xb: /* SQDMLSL, SQDMLSL2 */
9282 tcg_gen_neg_i64(tcg_res, tcg_res);
9283 /* fall through */
9284 case 0x9: /* SQDMLAL, SQDMLAL2 */
9285 read_vec_element(s, tcg_op1, rd, 0, MO_64);
9286 gen_helper_neon_addl_saturate_s64(tcg_res, cpu_env,
9287 tcg_res, tcg_op1);
9288 break;
9289 default:
9290 g_assert_not_reached();
9293 write_fp_dreg(s, rd, tcg_res);
9295 tcg_temp_free_i64(tcg_op1);
9296 tcg_temp_free_i64(tcg_op2);
9297 tcg_temp_free_i64(tcg_res);
9298 } else {
9299 TCGv_i32 tcg_op1 = read_fp_hreg(s, rn);
9300 TCGv_i32 tcg_op2 = read_fp_hreg(s, rm);
9301 TCGv_i64 tcg_res = tcg_temp_new_i64();
9303 gen_helper_neon_mull_s16(tcg_res, tcg_op1, tcg_op2);
9304 gen_helper_neon_addl_saturate_s32(tcg_res, cpu_env, tcg_res, tcg_res);
9306 switch (opcode) {
9307 case 0xd: /* SQDMULL, SQDMULL2 */
9308 break;
9309 case 0xb: /* SQDMLSL, SQDMLSL2 */
9310 gen_helper_neon_negl_u32(tcg_res, tcg_res);
9311 /* fall through */
9312 case 0x9: /* SQDMLAL, SQDMLAL2 */
9314 TCGv_i64 tcg_op3 = tcg_temp_new_i64();
9315 read_vec_element(s, tcg_op3, rd, 0, MO_32);
9316 gen_helper_neon_addl_saturate_s32(tcg_res, cpu_env,
9317 tcg_res, tcg_op3);
9318 tcg_temp_free_i64(tcg_op3);
9319 break;
9321 default:
9322 g_assert_not_reached();
9325 tcg_gen_ext32u_i64(tcg_res, tcg_res);
9326 write_fp_dreg(s, rd, tcg_res);
9328 tcg_temp_free_i32(tcg_op1);
9329 tcg_temp_free_i32(tcg_op2);
9330 tcg_temp_free_i64(tcg_res);
9334 static void handle_3same_64(DisasContext *s, int opcode, bool u,
9335 TCGv_i64 tcg_rd, TCGv_i64 tcg_rn, TCGv_i64 tcg_rm)
9337 /* Handle 64x64->64 opcodes which are shared between the scalar
9338 * and vector 3-same groups. We cover every opcode where size == 3
9339 * is valid in either the three-reg-same (integer, not pairwise)
9340 * or scalar-three-reg-same groups.
9342 TCGCond cond;
9344 switch (opcode) {
9345 case 0x1: /* SQADD */
9346 if (u) {
9347 gen_helper_neon_qadd_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9348 } else {
9349 gen_helper_neon_qadd_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9351 break;
9352 case 0x5: /* SQSUB */
9353 if (u) {
9354 gen_helper_neon_qsub_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9355 } else {
9356 gen_helper_neon_qsub_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9358 break;
9359 case 0x6: /* CMGT, CMHI */
9360 /* 64 bit integer comparison, result = test ? (2^64 - 1) : 0.
9361 * We implement this using setcond (test) and then negating.
9363 cond = u ? TCG_COND_GTU : TCG_COND_GT;
9364 do_cmop:
9365 tcg_gen_setcond_i64(cond, tcg_rd, tcg_rn, tcg_rm);
9366 tcg_gen_neg_i64(tcg_rd, tcg_rd);
9367 break;
9368 case 0x7: /* CMGE, CMHS */
9369 cond = u ? TCG_COND_GEU : TCG_COND_GE;
9370 goto do_cmop;
9371 case 0x11: /* CMTST, CMEQ */
9372 if (u) {
9373 cond = TCG_COND_EQ;
9374 goto do_cmop;
9376 gen_cmtst_i64(tcg_rd, tcg_rn, tcg_rm);
9377 break;
9378 case 0x8: /* SSHL, USHL */
9379 if (u) {
9380 gen_ushl_i64(tcg_rd, tcg_rn, tcg_rm);
9381 } else {
9382 gen_sshl_i64(tcg_rd, tcg_rn, tcg_rm);
9384 break;
9385 case 0x9: /* SQSHL, UQSHL */
9386 if (u) {
9387 gen_helper_neon_qshl_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9388 } else {
9389 gen_helper_neon_qshl_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9391 break;
9392 case 0xa: /* SRSHL, URSHL */
9393 if (u) {
9394 gen_helper_neon_rshl_u64(tcg_rd, tcg_rn, tcg_rm);
9395 } else {
9396 gen_helper_neon_rshl_s64(tcg_rd, tcg_rn, tcg_rm);
9398 break;
9399 case 0xb: /* SQRSHL, UQRSHL */
9400 if (u) {
9401 gen_helper_neon_qrshl_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9402 } else {
9403 gen_helper_neon_qrshl_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
9405 break;
9406 case 0x10: /* ADD, SUB */
9407 if (u) {
9408 tcg_gen_sub_i64(tcg_rd, tcg_rn, tcg_rm);
9409 } else {
9410 tcg_gen_add_i64(tcg_rd, tcg_rn, tcg_rm);
9412 break;
9413 default:
9414 g_assert_not_reached();
9418 /* Handle the 3-same-operands float operations; shared by the scalar
9419 * and vector encodings. The caller must filter out any encodings
9420 * not allocated for the encoding it is dealing with.
9422 static void handle_3same_float(DisasContext *s, int size, int elements,
9423 int fpopcode, int rd, int rn, int rm)
9425 int pass;
9426 TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
9428 for (pass = 0; pass < elements; pass++) {
9429 if (size) {
9430 /* Double */
9431 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
9432 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
9433 TCGv_i64 tcg_res = tcg_temp_new_i64();
9435 read_vec_element(s, tcg_op1, rn, pass, MO_64);
9436 read_vec_element(s, tcg_op2, rm, pass, MO_64);
9438 switch (fpopcode) {
9439 case 0x39: /* FMLS */
9440 /* As usual for ARM, separate negation for fused multiply-add */
9441 gen_helper_vfp_negd(tcg_op1, tcg_op1);
9442 /* fall through */
9443 case 0x19: /* FMLA */
9444 read_vec_element(s, tcg_res, rd, pass, MO_64);
9445 gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2,
9446 tcg_res, fpst);
9447 break;
9448 case 0x18: /* FMAXNM */
9449 gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
9450 break;
9451 case 0x1a: /* FADD */
9452 gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
9453 break;
9454 case 0x1b: /* FMULX */
9455 gen_helper_vfp_mulxd(tcg_res, tcg_op1, tcg_op2, fpst);
9456 break;
9457 case 0x1c: /* FCMEQ */
9458 gen_helper_neon_ceq_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9459 break;
9460 case 0x1e: /* FMAX */
9461 gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
9462 break;
9463 case 0x1f: /* FRECPS */
9464 gen_helper_recpsf_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9465 break;
9466 case 0x38: /* FMINNM */
9467 gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
9468 break;
9469 case 0x3a: /* FSUB */
9470 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
9471 break;
9472 case 0x3e: /* FMIN */
9473 gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
9474 break;
9475 case 0x3f: /* FRSQRTS */
9476 gen_helper_rsqrtsf_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9477 break;
9478 case 0x5b: /* FMUL */
9479 gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
9480 break;
9481 case 0x5c: /* FCMGE */
9482 gen_helper_neon_cge_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9483 break;
9484 case 0x5d: /* FACGE */
9485 gen_helper_neon_acge_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9486 break;
9487 case 0x5f: /* FDIV */
9488 gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
9489 break;
9490 case 0x7a: /* FABD */
9491 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
9492 gen_helper_vfp_absd(tcg_res, tcg_res);
9493 break;
9494 case 0x7c: /* FCMGT */
9495 gen_helper_neon_cgt_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9496 break;
9497 case 0x7d: /* FACGT */
9498 gen_helper_neon_acgt_f64(tcg_res, tcg_op1, tcg_op2, fpst);
9499 break;
9500 default:
9501 g_assert_not_reached();
9504 write_vec_element(s, tcg_res, rd, pass, MO_64);
9506 tcg_temp_free_i64(tcg_res);
9507 tcg_temp_free_i64(tcg_op1);
9508 tcg_temp_free_i64(tcg_op2);
9509 } else {
9510 /* Single */
9511 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
9512 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
9513 TCGv_i32 tcg_res = tcg_temp_new_i32();
9515 read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
9516 read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
9518 switch (fpopcode) {
9519 case 0x39: /* FMLS */
9520 /* As usual for ARM, separate negation for fused multiply-add */
9521 gen_helper_vfp_negs(tcg_op1, tcg_op1);
9522 /* fall through */
9523 case 0x19: /* FMLA */
9524 read_vec_element_i32(s, tcg_res, rd, pass, MO_32);
9525 gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2,
9526 tcg_res, fpst);
9527 break;
9528 case 0x1a: /* FADD */
9529 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
9530 break;
9531 case 0x1b: /* FMULX */
9532 gen_helper_vfp_mulxs(tcg_res, tcg_op1, tcg_op2, fpst);
9533 break;
9534 case 0x1c: /* FCMEQ */
9535 gen_helper_neon_ceq_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9536 break;
9537 case 0x1e: /* FMAX */
9538 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
9539 break;
9540 case 0x1f: /* FRECPS */
9541 gen_helper_recpsf_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9542 break;
9543 case 0x18: /* FMAXNM */
9544 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
9545 break;
9546 case 0x38: /* FMINNM */
9547 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
9548 break;
9549 case 0x3a: /* FSUB */
9550 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
9551 break;
9552 case 0x3e: /* FMIN */
9553 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
9554 break;
9555 case 0x3f: /* FRSQRTS */
9556 gen_helper_rsqrtsf_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9557 break;
9558 case 0x5b: /* FMUL */
9559 gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
9560 break;
9561 case 0x5c: /* FCMGE */
9562 gen_helper_neon_cge_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9563 break;
9564 case 0x5d: /* FACGE */
9565 gen_helper_neon_acge_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9566 break;
9567 case 0x5f: /* FDIV */
9568 gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
9569 break;
9570 case 0x7a: /* FABD */
9571 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
9572 gen_helper_vfp_abss(tcg_res, tcg_res);
9573 break;
9574 case 0x7c: /* FCMGT */
9575 gen_helper_neon_cgt_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9576 break;
9577 case 0x7d: /* FACGT */
9578 gen_helper_neon_acgt_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9579 break;
9580 default:
9581 g_assert_not_reached();
9584 if (elements == 1) {
9585 /* scalar single so clear high part */
9586 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
9588 tcg_gen_extu_i32_i64(tcg_tmp, tcg_res);
9589 write_vec_element(s, tcg_tmp, rd, pass, MO_64);
9590 tcg_temp_free_i64(tcg_tmp);
9591 } else {
9592 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
9595 tcg_temp_free_i32(tcg_res);
9596 tcg_temp_free_i32(tcg_op1);
9597 tcg_temp_free_i32(tcg_op2);
9601 tcg_temp_free_ptr(fpst);
9603 clear_vec_high(s, elements * (size ? 8 : 4) > 8, rd);
9606 /* AdvSIMD scalar three same
9607 * 31 30 29 28 24 23 22 21 20 16 15 11 10 9 5 4 0
9608 * +-----+---+-----------+------+---+------+--------+---+------+------+
9609 * | 0 1 | U | 1 1 1 1 0 | size | 1 | Rm | opcode | 1 | Rn | Rd |
9610 * +-----+---+-----------+------+---+------+--------+---+------+------+
9612 static void disas_simd_scalar_three_reg_same(DisasContext *s, uint32_t insn)
9614 int rd = extract32(insn, 0, 5);
9615 int rn = extract32(insn, 5, 5);
9616 int opcode = extract32(insn, 11, 5);
9617 int rm = extract32(insn, 16, 5);
9618 int size = extract32(insn, 22, 2);
9619 bool u = extract32(insn, 29, 1);
9620 TCGv_i64 tcg_rd;
9622 if (opcode >= 0x18) {
9623 /* Floating point: U, size[1] and opcode indicate operation */
9624 int fpopcode = opcode | (extract32(size, 1, 1) << 5) | (u << 6);
9625 switch (fpopcode) {
9626 case 0x1b: /* FMULX */
9627 case 0x1f: /* FRECPS */
9628 case 0x3f: /* FRSQRTS */
9629 case 0x5d: /* FACGE */
9630 case 0x7d: /* FACGT */
9631 case 0x1c: /* FCMEQ */
9632 case 0x5c: /* FCMGE */
9633 case 0x7c: /* FCMGT */
9634 case 0x7a: /* FABD */
9635 break;
9636 default:
9637 unallocated_encoding(s);
9638 return;
9641 if (!fp_access_check(s)) {
9642 return;
9645 handle_3same_float(s, extract32(size, 0, 1), 1, fpopcode, rd, rn, rm);
9646 return;
9649 switch (opcode) {
9650 case 0x1: /* SQADD, UQADD */
9651 case 0x5: /* SQSUB, UQSUB */
9652 case 0x9: /* SQSHL, UQSHL */
9653 case 0xb: /* SQRSHL, UQRSHL */
9654 break;
9655 case 0x8: /* SSHL, USHL */
9656 case 0xa: /* SRSHL, URSHL */
9657 case 0x6: /* CMGT, CMHI */
9658 case 0x7: /* CMGE, CMHS */
9659 case 0x11: /* CMTST, CMEQ */
9660 case 0x10: /* ADD, SUB (vector) */
9661 if (size != 3) {
9662 unallocated_encoding(s);
9663 return;
9665 break;
9666 case 0x16: /* SQDMULH, SQRDMULH (vector) */
9667 if (size != 1 && size != 2) {
9668 unallocated_encoding(s);
9669 return;
9671 break;
9672 default:
9673 unallocated_encoding(s);
9674 return;
9677 if (!fp_access_check(s)) {
9678 return;
9681 tcg_rd = tcg_temp_new_i64();
9683 if (size == 3) {
9684 TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
9685 TCGv_i64 tcg_rm = read_fp_dreg(s, rm);
9687 handle_3same_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rm);
9688 tcg_temp_free_i64(tcg_rn);
9689 tcg_temp_free_i64(tcg_rm);
9690 } else {
9691 /* Do a single operation on the lowest element in the vector.
9692 * We use the standard Neon helpers and rely on 0 OP 0 == 0 with
9693 * no side effects for all these operations.
9694 * OPTME: special-purpose helpers would avoid doing some
9695 * unnecessary work in the helper for the 8 and 16 bit cases.
9697 NeonGenTwoOpEnvFn *genenvfn;
9698 TCGv_i32 tcg_rn = tcg_temp_new_i32();
9699 TCGv_i32 tcg_rm = tcg_temp_new_i32();
9700 TCGv_i32 tcg_rd32 = tcg_temp_new_i32();
9702 read_vec_element_i32(s, tcg_rn, rn, 0, size);
9703 read_vec_element_i32(s, tcg_rm, rm, 0, size);
9705 switch (opcode) {
9706 case 0x1: /* SQADD, UQADD */
9708 static NeonGenTwoOpEnvFn * const fns[3][2] = {
9709 { gen_helper_neon_qadd_s8, gen_helper_neon_qadd_u8 },
9710 { gen_helper_neon_qadd_s16, gen_helper_neon_qadd_u16 },
9711 { gen_helper_neon_qadd_s32, gen_helper_neon_qadd_u32 },
9713 genenvfn = fns[size][u];
9714 break;
9716 case 0x5: /* SQSUB, UQSUB */
9718 static NeonGenTwoOpEnvFn * const fns[3][2] = {
9719 { gen_helper_neon_qsub_s8, gen_helper_neon_qsub_u8 },
9720 { gen_helper_neon_qsub_s16, gen_helper_neon_qsub_u16 },
9721 { gen_helper_neon_qsub_s32, gen_helper_neon_qsub_u32 },
9723 genenvfn = fns[size][u];
9724 break;
9726 case 0x9: /* SQSHL, UQSHL */
9728 static NeonGenTwoOpEnvFn * const fns[3][2] = {
9729 { gen_helper_neon_qshl_s8, gen_helper_neon_qshl_u8 },
9730 { gen_helper_neon_qshl_s16, gen_helper_neon_qshl_u16 },
9731 { gen_helper_neon_qshl_s32, gen_helper_neon_qshl_u32 },
9733 genenvfn = fns[size][u];
9734 break;
9736 case 0xb: /* SQRSHL, UQRSHL */
9738 static NeonGenTwoOpEnvFn * const fns[3][2] = {
9739 { gen_helper_neon_qrshl_s8, gen_helper_neon_qrshl_u8 },
9740 { gen_helper_neon_qrshl_s16, gen_helper_neon_qrshl_u16 },
9741 { gen_helper_neon_qrshl_s32, gen_helper_neon_qrshl_u32 },
9743 genenvfn = fns[size][u];
9744 break;
9746 case 0x16: /* SQDMULH, SQRDMULH */
9748 static NeonGenTwoOpEnvFn * const fns[2][2] = {
9749 { gen_helper_neon_qdmulh_s16, gen_helper_neon_qrdmulh_s16 },
9750 { gen_helper_neon_qdmulh_s32, gen_helper_neon_qrdmulh_s32 },
9752 assert(size == 1 || size == 2);
9753 genenvfn = fns[size - 1][u];
9754 break;
9756 default:
9757 g_assert_not_reached();
9760 genenvfn(tcg_rd32, cpu_env, tcg_rn, tcg_rm);
9761 tcg_gen_extu_i32_i64(tcg_rd, tcg_rd32);
9762 tcg_temp_free_i32(tcg_rd32);
9763 tcg_temp_free_i32(tcg_rn);
9764 tcg_temp_free_i32(tcg_rm);
9767 write_fp_dreg(s, rd, tcg_rd);
9769 tcg_temp_free_i64(tcg_rd);
9772 /* AdvSIMD scalar three same FP16
9773 * 31 30 29 28 24 23 22 21 20 16 15 14 13 11 10 9 5 4 0
9774 * +-----+---+-----------+---+-----+------+-----+--------+---+----+----+
9775 * | 0 1 | U | 1 1 1 1 0 | a | 1 0 | Rm | 0 0 | opcode | 1 | Rn | Rd |
9776 * +-----+---+-----------+---+-----+------+-----+--------+---+----+----+
9777 * v: 0101 1110 0100 0000 0000 0100 0000 0000 => 5e400400
9778 * m: 1101 1111 0110 0000 1100 0100 0000 0000 => df60c400
9780 static void disas_simd_scalar_three_reg_same_fp16(DisasContext *s,
9781 uint32_t insn)
9783 int rd = extract32(insn, 0, 5);
9784 int rn = extract32(insn, 5, 5);
9785 int opcode = extract32(insn, 11, 3);
9786 int rm = extract32(insn, 16, 5);
9787 bool u = extract32(insn, 29, 1);
9788 bool a = extract32(insn, 23, 1);
9789 int fpopcode = opcode | (a << 3) | (u << 4);
9790 TCGv_ptr fpst;
9791 TCGv_i32 tcg_op1;
9792 TCGv_i32 tcg_op2;
9793 TCGv_i32 tcg_res;
9795 switch (fpopcode) {
9796 case 0x03: /* FMULX */
9797 case 0x04: /* FCMEQ (reg) */
9798 case 0x07: /* FRECPS */
9799 case 0x0f: /* FRSQRTS */
9800 case 0x14: /* FCMGE (reg) */
9801 case 0x15: /* FACGE */
9802 case 0x1a: /* FABD */
9803 case 0x1c: /* FCMGT (reg) */
9804 case 0x1d: /* FACGT */
9805 break;
9806 default:
9807 unallocated_encoding(s);
9808 return;
9811 if (!dc_isar_feature(aa64_fp16, s)) {
9812 unallocated_encoding(s);
9815 if (!fp_access_check(s)) {
9816 return;
9819 fpst = fpstatus_ptr(FPST_FPCR_F16);
9821 tcg_op1 = read_fp_hreg(s, rn);
9822 tcg_op2 = read_fp_hreg(s, rm);
9823 tcg_res = tcg_temp_new_i32();
9825 switch (fpopcode) {
9826 case 0x03: /* FMULX */
9827 gen_helper_advsimd_mulxh(tcg_res, tcg_op1, tcg_op2, fpst);
9828 break;
9829 case 0x04: /* FCMEQ (reg) */
9830 gen_helper_advsimd_ceq_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9831 break;
9832 case 0x07: /* FRECPS */
9833 gen_helper_recpsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9834 break;
9835 case 0x0f: /* FRSQRTS */
9836 gen_helper_rsqrtsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9837 break;
9838 case 0x14: /* FCMGE (reg) */
9839 gen_helper_advsimd_cge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9840 break;
9841 case 0x15: /* FACGE */
9842 gen_helper_advsimd_acge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9843 break;
9844 case 0x1a: /* FABD */
9845 gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
9846 tcg_gen_andi_i32(tcg_res, tcg_res, 0x7fff);
9847 break;
9848 case 0x1c: /* FCMGT (reg) */
9849 gen_helper_advsimd_cgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9850 break;
9851 case 0x1d: /* FACGT */
9852 gen_helper_advsimd_acgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9853 break;
9854 default:
9855 g_assert_not_reached();
9858 write_fp_sreg(s, rd, tcg_res);
9861 tcg_temp_free_i32(tcg_res);
9862 tcg_temp_free_i32(tcg_op1);
9863 tcg_temp_free_i32(tcg_op2);
9864 tcg_temp_free_ptr(fpst);
9867 /* AdvSIMD scalar three same extra
9868 * 31 30 29 28 24 23 22 21 20 16 15 14 11 10 9 5 4 0
9869 * +-----+---+-----------+------+---+------+---+--------+---+----+----+
9870 * | 0 1 | U | 1 1 1 1 0 | size | 0 | Rm | 1 | opcode | 1 | Rn | Rd |
9871 * +-----+---+-----------+------+---+------+---+--------+---+----+----+
9873 static void disas_simd_scalar_three_reg_same_extra(DisasContext *s,
9874 uint32_t insn)
9876 int rd = extract32(insn, 0, 5);
9877 int rn = extract32(insn, 5, 5);
9878 int opcode = extract32(insn, 11, 4);
9879 int rm = extract32(insn, 16, 5);
9880 int size = extract32(insn, 22, 2);
9881 bool u = extract32(insn, 29, 1);
9882 TCGv_i32 ele1, ele2, ele3;
9883 TCGv_i64 res;
9884 bool feature;
9886 switch (u * 16 + opcode) {
9887 case 0x10: /* SQRDMLAH (vector) */
9888 case 0x11: /* SQRDMLSH (vector) */
9889 if (size != 1 && size != 2) {
9890 unallocated_encoding(s);
9891 return;
9893 feature = dc_isar_feature(aa64_rdm, s);
9894 break;
9895 default:
9896 unallocated_encoding(s);
9897 return;
9899 if (!feature) {
9900 unallocated_encoding(s);
9901 return;
9903 if (!fp_access_check(s)) {
9904 return;
9907 /* Do a single operation on the lowest element in the vector.
9908 * We use the standard Neon helpers and rely on 0 OP 0 == 0
9909 * with no side effects for all these operations.
9910 * OPTME: special-purpose helpers would avoid doing some
9911 * unnecessary work in the helper for the 16 bit cases.
9913 ele1 = tcg_temp_new_i32();
9914 ele2 = tcg_temp_new_i32();
9915 ele3 = tcg_temp_new_i32();
9917 read_vec_element_i32(s, ele1, rn, 0, size);
9918 read_vec_element_i32(s, ele2, rm, 0, size);
9919 read_vec_element_i32(s, ele3, rd, 0, size);
9921 switch (opcode) {
9922 case 0x0: /* SQRDMLAH */
9923 if (size == 1) {
9924 gen_helper_neon_qrdmlah_s16(ele3, cpu_env, ele1, ele2, ele3);
9925 } else {
9926 gen_helper_neon_qrdmlah_s32(ele3, cpu_env, ele1, ele2, ele3);
9928 break;
9929 case 0x1: /* SQRDMLSH */
9930 if (size == 1) {
9931 gen_helper_neon_qrdmlsh_s16(ele3, cpu_env, ele1, ele2, ele3);
9932 } else {
9933 gen_helper_neon_qrdmlsh_s32(ele3, cpu_env, ele1, ele2, ele3);
9935 break;
9936 default:
9937 g_assert_not_reached();
9939 tcg_temp_free_i32(ele1);
9940 tcg_temp_free_i32(ele2);
9942 res = tcg_temp_new_i64();
9943 tcg_gen_extu_i32_i64(res, ele3);
9944 tcg_temp_free_i32(ele3);
9946 write_fp_dreg(s, rd, res);
9947 tcg_temp_free_i64(res);
9950 static void handle_2misc_64(DisasContext *s, int opcode, bool u,
9951 TCGv_i64 tcg_rd, TCGv_i64 tcg_rn,
9952 TCGv_i32 tcg_rmode, TCGv_ptr tcg_fpstatus)
9954 /* Handle 64->64 opcodes which are shared between the scalar and
9955 * vector 2-reg-misc groups. We cover every integer opcode where size == 3
9956 * is valid in either group and also the double-precision fp ops.
9957 * The caller only need provide tcg_rmode and tcg_fpstatus if the op
9958 * requires them.
9960 TCGCond cond;
9962 switch (opcode) {
9963 case 0x4: /* CLS, CLZ */
9964 if (u) {
9965 tcg_gen_clzi_i64(tcg_rd, tcg_rn, 64);
9966 } else {
9967 tcg_gen_clrsb_i64(tcg_rd, tcg_rn);
9969 break;
9970 case 0x5: /* NOT */
9971 /* This opcode is shared with CNT and RBIT but we have earlier
9972 * enforced that size == 3 if and only if this is the NOT insn.
9974 tcg_gen_not_i64(tcg_rd, tcg_rn);
9975 break;
9976 case 0x7: /* SQABS, SQNEG */
9977 if (u) {
9978 gen_helper_neon_qneg_s64(tcg_rd, cpu_env, tcg_rn);
9979 } else {
9980 gen_helper_neon_qabs_s64(tcg_rd, cpu_env, tcg_rn);
9982 break;
9983 case 0xa: /* CMLT */
9984 /* 64 bit integer comparison against zero, result is
9985 * test ? (2^64 - 1) : 0. We implement via setcond(!test) and
9986 * subtracting 1.
9988 cond = TCG_COND_LT;
9989 do_cmop:
9990 tcg_gen_setcondi_i64(cond, tcg_rd, tcg_rn, 0);
9991 tcg_gen_neg_i64(tcg_rd, tcg_rd);
9992 break;
9993 case 0x8: /* CMGT, CMGE */
9994 cond = u ? TCG_COND_GE : TCG_COND_GT;
9995 goto do_cmop;
9996 case 0x9: /* CMEQ, CMLE */
9997 cond = u ? TCG_COND_LE : TCG_COND_EQ;
9998 goto do_cmop;
9999 case 0xb: /* ABS, NEG */
10000 if (u) {
10001 tcg_gen_neg_i64(tcg_rd, tcg_rn);
10002 } else {
10003 tcg_gen_abs_i64(tcg_rd, tcg_rn);
10005 break;
10006 case 0x2f: /* FABS */
10007 gen_helper_vfp_absd(tcg_rd, tcg_rn);
10008 break;
10009 case 0x6f: /* FNEG */
10010 gen_helper_vfp_negd(tcg_rd, tcg_rn);
10011 break;
10012 case 0x7f: /* FSQRT */
10013 gen_helper_vfp_sqrtd(tcg_rd, tcg_rn, cpu_env);
10014 break;
10015 case 0x1a: /* FCVTNS */
10016 case 0x1b: /* FCVTMS */
10017 case 0x1c: /* FCVTAS */
10018 case 0x3a: /* FCVTPS */
10019 case 0x3b: /* FCVTZS */
10021 TCGv_i32 tcg_shift = tcg_const_i32(0);
10022 gen_helper_vfp_tosqd(tcg_rd, tcg_rn, tcg_shift, tcg_fpstatus);
10023 tcg_temp_free_i32(tcg_shift);
10024 break;
10026 case 0x5a: /* FCVTNU */
10027 case 0x5b: /* FCVTMU */
10028 case 0x5c: /* FCVTAU */
10029 case 0x7a: /* FCVTPU */
10030 case 0x7b: /* FCVTZU */
10032 TCGv_i32 tcg_shift = tcg_const_i32(0);
10033 gen_helper_vfp_touqd(tcg_rd, tcg_rn, tcg_shift, tcg_fpstatus);
10034 tcg_temp_free_i32(tcg_shift);
10035 break;
10037 case 0x18: /* FRINTN */
10038 case 0x19: /* FRINTM */
10039 case 0x38: /* FRINTP */
10040 case 0x39: /* FRINTZ */
10041 case 0x58: /* FRINTA */
10042 case 0x79: /* FRINTI */
10043 gen_helper_rintd(tcg_rd, tcg_rn, tcg_fpstatus);
10044 break;
10045 case 0x59: /* FRINTX */
10046 gen_helper_rintd_exact(tcg_rd, tcg_rn, tcg_fpstatus);
10047 break;
10048 case 0x1e: /* FRINT32Z */
10049 case 0x5e: /* FRINT32X */
10050 gen_helper_frint32_d(tcg_rd, tcg_rn, tcg_fpstatus);
10051 break;
10052 case 0x1f: /* FRINT64Z */
10053 case 0x5f: /* FRINT64X */
10054 gen_helper_frint64_d(tcg_rd, tcg_rn, tcg_fpstatus);
10055 break;
10056 default:
10057 g_assert_not_reached();
10061 static void handle_2misc_fcmp_zero(DisasContext *s, int opcode,
10062 bool is_scalar, bool is_u, bool is_q,
10063 int size, int rn, int rd)
10065 bool is_double = (size == MO_64);
10066 TCGv_ptr fpst;
10068 if (!fp_access_check(s)) {
10069 return;
10072 fpst = fpstatus_ptr(size == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
10074 if (is_double) {
10075 TCGv_i64 tcg_op = tcg_temp_new_i64();
10076 TCGv_i64 tcg_zero = tcg_const_i64(0);
10077 TCGv_i64 tcg_res = tcg_temp_new_i64();
10078 NeonGenTwoDoubleOpFn *genfn;
10079 bool swap = false;
10080 int pass;
10082 switch (opcode) {
10083 case 0x2e: /* FCMLT (zero) */
10084 swap = true;
10085 /* fallthrough */
10086 case 0x2c: /* FCMGT (zero) */
10087 genfn = gen_helper_neon_cgt_f64;
10088 break;
10089 case 0x2d: /* FCMEQ (zero) */
10090 genfn = gen_helper_neon_ceq_f64;
10091 break;
10092 case 0x6d: /* FCMLE (zero) */
10093 swap = true;
10094 /* fall through */
10095 case 0x6c: /* FCMGE (zero) */
10096 genfn = gen_helper_neon_cge_f64;
10097 break;
10098 default:
10099 g_assert_not_reached();
10102 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
10103 read_vec_element(s, tcg_op, rn, pass, MO_64);
10104 if (swap) {
10105 genfn(tcg_res, tcg_zero, tcg_op, fpst);
10106 } else {
10107 genfn(tcg_res, tcg_op, tcg_zero, fpst);
10109 write_vec_element(s, tcg_res, rd, pass, MO_64);
10111 tcg_temp_free_i64(tcg_res);
10112 tcg_temp_free_i64(tcg_zero);
10113 tcg_temp_free_i64(tcg_op);
10115 clear_vec_high(s, !is_scalar, rd);
10116 } else {
10117 TCGv_i32 tcg_op = tcg_temp_new_i32();
10118 TCGv_i32 tcg_zero = tcg_const_i32(0);
10119 TCGv_i32 tcg_res = tcg_temp_new_i32();
10120 NeonGenTwoSingleOpFn *genfn;
10121 bool swap = false;
10122 int pass, maxpasses;
10124 if (size == MO_16) {
10125 switch (opcode) {
10126 case 0x2e: /* FCMLT (zero) */
10127 swap = true;
10128 /* fall through */
10129 case 0x2c: /* FCMGT (zero) */
10130 genfn = gen_helper_advsimd_cgt_f16;
10131 break;
10132 case 0x2d: /* FCMEQ (zero) */
10133 genfn = gen_helper_advsimd_ceq_f16;
10134 break;
10135 case 0x6d: /* FCMLE (zero) */
10136 swap = true;
10137 /* fall through */
10138 case 0x6c: /* FCMGE (zero) */
10139 genfn = gen_helper_advsimd_cge_f16;
10140 break;
10141 default:
10142 g_assert_not_reached();
10144 } else {
10145 switch (opcode) {
10146 case 0x2e: /* FCMLT (zero) */
10147 swap = true;
10148 /* fall through */
10149 case 0x2c: /* FCMGT (zero) */
10150 genfn = gen_helper_neon_cgt_f32;
10151 break;
10152 case 0x2d: /* FCMEQ (zero) */
10153 genfn = gen_helper_neon_ceq_f32;
10154 break;
10155 case 0x6d: /* FCMLE (zero) */
10156 swap = true;
10157 /* fall through */
10158 case 0x6c: /* FCMGE (zero) */
10159 genfn = gen_helper_neon_cge_f32;
10160 break;
10161 default:
10162 g_assert_not_reached();
10166 if (is_scalar) {
10167 maxpasses = 1;
10168 } else {
10169 int vector_size = 8 << is_q;
10170 maxpasses = vector_size >> size;
10173 for (pass = 0; pass < maxpasses; pass++) {
10174 read_vec_element_i32(s, tcg_op, rn, pass, size);
10175 if (swap) {
10176 genfn(tcg_res, tcg_zero, tcg_op, fpst);
10177 } else {
10178 genfn(tcg_res, tcg_op, tcg_zero, fpst);
10180 if (is_scalar) {
10181 write_fp_sreg(s, rd, tcg_res);
10182 } else {
10183 write_vec_element_i32(s, tcg_res, rd, pass, size);
10186 tcg_temp_free_i32(tcg_res);
10187 tcg_temp_free_i32(tcg_zero);
10188 tcg_temp_free_i32(tcg_op);
10189 if (!is_scalar) {
10190 clear_vec_high(s, is_q, rd);
10194 tcg_temp_free_ptr(fpst);
10197 static void handle_2misc_reciprocal(DisasContext *s, int opcode,
10198 bool is_scalar, bool is_u, bool is_q,
10199 int size, int rn, int rd)
10201 bool is_double = (size == 3);
10202 TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
10204 if (is_double) {
10205 TCGv_i64 tcg_op = tcg_temp_new_i64();
10206 TCGv_i64 tcg_res = tcg_temp_new_i64();
10207 int pass;
10209 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
10210 read_vec_element(s, tcg_op, rn, pass, MO_64);
10211 switch (opcode) {
10212 case 0x3d: /* FRECPE */
10213 gen_helper_recpe_f64(tcg_res, tcg_op, fpst);
10214 break;
10215 case 0x3f: /* FRECPX */
10216 gen_helper_frecpx_f64(tcg_res, tcg_op, fpst);
10217 break;
10218 case 0x7d: /* FRSQRTE */
10219 gen_helper_rsqrte_f64(tcg_res, tcg_op, fpst);
10220 break;
10221 default:
10222 g_assert_not_reached();
10224 write_vec_element(s, tcg_res, rd, pass, MO_64);
10226 tcg_temp_free_i64(tcg_res);
10227 tcg_temp_free_i64(tcg_op);
10228 clear_vec_high(s, !is_scalar, rd);
10229 } else {
10230 TCGv_i32 tcg_op = tcg_temp_new_i32();
10231 TCGv_i32 tcg_res = tcg_temp_new_i32();
10232 int pass, maxpasses;
10234 if (is_scalar) {
10235 maxpasses = 1;
10236 } else {
10237 maxpasses = is_q ? 4 : 2;
10240 for (pass = 0; pass < maxpasses; pass++) {
10241 read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
10243 switch (opcode) {
10244 case 0x3c: /* URECPE */
10245 gen_helper_recpe_u32(tcg_res, tcg_op);
10246 break;
10247 case 0x3d: /* FRECPE */
10248 gen_helper_recpe_f32(tcg_res, tcg_op, fpst);
10249 break;
10250 case 0x3f: /* FRECPX */
10251 gen_helper_frecpx_f32(tcg_res, tcg_op, fpst);
10252 break;
10253 case 0x7d: /* FRSQRTE */
10254 gen_helper_rsqrte_f32(tcg_res, tcg_op, fpst);
10255 break;
10256 default:
10257 g_assert_not_reached();
10260 if (is_scalar) {
10261 write_fp_sreg(s, rd, tcg_res);
10262 } else {
10263 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
10266 tcg_temp_free_i32(tcg_res);
10267 tcg_temp_free_i32(tcg_op);
10268 if (!is_scalar) {
10269 clear_vec_high(s, is_q, rd);
10272 tcg_temp_free_ptr(fpst);
10275 static void handle_2misc_narrow(DisasContext *s, bool scalar,
10276 int opcode, bool u, bool is_q,
10277 int size, int rn, int rd)
10279 /* Handle 2-reg-misc ops which are narrowing (so each 2*size element
10280 * in the source becomes a size element in the destination).
10282 int pass;
10283 TCGv_i32 tcg_res[2];
10284 int destelt = is_q ? 2 : 0;
10285 int passes = scalar ? 1 : 2;
10287 if (scalar) {
10288 tcg_res[1] = tcg_const_i32(0);
10291 for (pass = 0; pass < passes; pass++) {
10292 TCGv_i64 tcg_op = tcg_temp_new_i64();
10293 NeonGenNarrowFn *genfn = NULL;
10294 NeonGenNarrowEnvFn *genenvfn = NULL;
10296 if (scalar) {
10297 read_vec_element(s, tcg_op, rn, pass, size + 1);
10298 } else {
10299 read_vec_element(s, tcg_op, rn, pass, MO_64);
10301 tcg_res[pass] = tcg_temp_new_i32();
10303 switch (opcode) {
10304 case 0x12: /* XTN, SQXTUN */
10306 static NeonGenNarrowFn * const xtnfns[3] = {
10307 gen_helper_neon_narrow_u8,
10308 gen_helper_neon_narrow_u16,
10309 tcg_gen_extrl_i64_i32,
10311 static NeonGenNarrowEnvFn * const sqxtunfns[3] = {
10312 gen_helper_neon_unarrow_sat8,
10313 gen_helper_neon_unarrow_sat16,
10314 gen_helper_neon_unarrow_sat32,
10316 if (u) {
10317 genenvfn = sqxtunfns[size];
10318 } else {
10319 genfn = xtnfns[size];
10321 break;
10323 case 0x14: /* SQXTN, UQXTN */
10325 static NeonGenNarrowEnvFn * const fns[3][2] = {
10326 { gen_helper_neon_narrow_sat_s8,
10327 gen_helper_neon_narrow_sat_u8 },
10328 { gen_helper_neon_narrow_sat_s16,
10329 gen_helper_neon_narrow_sat_u16 },
10330 { gen_helper_neon_narrow_sat_s32,
10331 gen_helper_neon_narrow_sat_u32 },
10333 genenvfn = fns[size][u];
10334 break;
10336 case 0x16: /* FCVTN, FCVTN2 */
10337 /* 32 bit to 16 bit or 64 bit to 32 bit float conversion */
10338 if (size == 2) {
10339 gen_helper_vfp_fcvtsd(tcg_res[pass], tcg_op, cpu_env);
10340 } else {
10341 TCGv_i32 tcg_lo = tcg_temp_new_i32();
10342 TCGv_i32 tcg_hi = tcg_temp_new_i32();
10343 TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
10344 TCGv_i32 ahp = get_ahp_flag();
10346 tcg_gen_extr_i64_i32(tcg_lo, tcg_hi, tcg_op);
10347 gen_helper_vfp_fcvt_f32_to_f16(tcg_lo, tcg_lo, fpst, ahp);
10348 gen_helper_vfp_fcvt_f32_to_f16(tcg_hi, tcg_hi, fpst, ahp);
10349 tcg_gen_deposit_i32(tcg_res[pass], tcg_lo, tcg_hi, 16, 16);
10350 tcg_temp_free_i32(tcg_lo);
10351 tcg_temp_free_i32(tcg_hi);
10352 tcg_temp_free_ptr(fpst);
10353 tcg_temp_free_i32(ahp);
10355 break;
10356 case 0x36: /* BFCVTN, BFCVTN2 */
10358 TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
10359 gen_helper_bfcvt_pair(tcg_res[pass], tcg_op, fpst);
10360 tcg_temp_free_ptr(fpst);
10362 break;
10363 case 0x56: /* FCVTXN, FCVTXN2 */
10364 /* 64 bit to 32 bit float conversion
10365 * with von Neumann rounding (round to odd)
10367 assert(size == 2);
10368 gen_helper_fcvtx_f64_to_f32(tcg_res[pass], tcg_op, cpu_env);
10369 break;
10370 default:
10371 g_assert_not_reached();
10374 if (genfn) {
10375 genfn(tcg_res[pass], tcg_op);
10376 } else if (genenvfn) {
10377 genenvfn(tcg_res[pass], cpu_env, tcg_op);
10380 tcg_temp_free_i64(tcg_op);
10383 for (pass = 0; pass < 2; pass++) {
10384 write_vec_element_i32(s, tcg_res[pass], rd, destelt + pass, MO_32);
10385 tcg_temp_free_i32(tcg_res[pass]);
10387 clear_vec_high(s, is_q, rd);
10390 /* Remaining saturating accumulating ops */
10391 static void handle_2misc_satacc(DisasContext *s, bool is_scalar, bool is_u,
10392 bool is_q, int size, int rn, int rd)
10394 bool is_double = (size == 3);
10396 if (is_double) {
10397 TCGv_i64 tcg_rn = tcg_temp_new_i64();
10398 TCGv_i64 tcg_rd = tcg_temp_new_i64();
10399 int pass;
10401 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
10402 read_vec_element(s, tcg_rn, rn, pass, MO_64);
10403 read_vec_element(s, tcg_rd, rd, pass, MO_64);
10405 if (is_u) { /* USQADD */
10406 gen_helper_neon_uqadd_s64(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10407 } else { /* SUQADD */
10408 gen_helper_neon_sqadd_u64(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10410 write_vec_element(s, tcg_rd, rd, pass, MO_64);
10412 tcg_temp_free_i64(tcg_rd);
10413 tcg_temp_free_i64(tcg_rn);
10414 clear_vec_high(s, !is_scalar, rd);
10415 } else {
10416 TCGv_i32 tcg_rn = tcg_temp_new_i32();
10417 TCGv_i32 tcg_rd = tcg_temp_new_i32();
10418 int pass, maxpasses;
10420 if (is_scalar) {
10421 maxpasses = 1;
10422 } else {
10423 maxpasses = is_q ? 4 : 2;
10426 for (pass = 0; pass < maxpasses; pass++) {
10427 if (is_scalar) {
10428 read_vec_element_i32(s, tcg_rn, rn, pass, size);
10429 read_vec_element_i32(s, tcg_rd, rd, pass, size);
10430 } else {
10431 read_vec_element_i32(s, tcg_rn, rn, pass, MO_32);
10432 read_vec_element_i32(s, tcg_rd, rd, pass, MO_32);
10435 if (is_u) { /* USQADD */
10436 switch (size) {
10437 case 0:
10438 gen_helper_neon_uqadd_s8(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10439 break;
10440 case 1:
10441 gen_helper_neon_uqadd_s16(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10442 break;
10443 case 2:
10444 gen_helper_neon_uqadd_s32(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10445 break;
10446 default:
10447 g_assert_not_reached();
10449 } else { /* SUQADD */
10450 switch (size) {
10451 case 0:
10452 gen_helper_neon_sqadd_u8(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10453 break;
10454 case 1:
10455 gen_helper_neon_sqadd_u16(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10456 break;
10457 case 2:
10458 gen_helper_neon_sqadd_u32(tcg_rd, cpu_env, tcg_rn, tcg_rd);
10459 break;
10460 default:
10461 g_assert_not_reached();
10465 if (is_scalar) {
10466 TCGv_i64 tcg_zero = tcg_const_i64(0);
10467 write_vec_element(s, tcg_zero, rd, 0, MO_64);
10468 tcg_temp_free_i64(tcg_zero);
10470 write_vec_element_i32(s, tcg_rd, rd, pass, MO_32);
10472 tcg_temp_free_i32(tcg_rd);
10473 tcg_temp_free_i32(tcg_rn);
10474 clear_vec_high(s, is_q, rd);
10478 /* AdvSIMD scalar two reg misc
10479 * 31 30 29 28 24 23 22 21 17 16 12 11 10 9 5 4 0
10480 * +-----+---+-----------+------+-----------+--------+-----+------+------+
10481 * | 0 1 | U | 1 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 | Rn | Rd |
10482 * +-----+---+-----------+------+-----------+--------+-----+------+------+
10484 static void disas_simd_scalar_two_reg_misc(DisasContext *s, uint32_t insn)
10486 int rd = extract32(insn, 0, 5);
10487 int rn = extract32(insn, 5, 5);
10488 int opcode = extract32(insn, 12, 5);
10489 int size = extract32(insn, 22, 2);
10490 bool u = extract32(insn, 29, 1);
10491 bool is_fcvt = false;
10492 int rmode;
10493 TCGv_i32 tcg_rmode;
10494 TCGv_ptr tcg_fpstatus;
10496 switch (opcode) {
10497 case 0x3: /* USQADD / SUQADD*/
10498 if (!fp_access_check(s)) {
10499 return;
10501 handle_2misc_satacc(s, true, u, false, size, rn, rd);
10502 return;
10503 case 0x7: /* SQABS / SQNEG */
10504 break;
10505 case 0xa: /* CMLT */
10506 if (u) {
10507 unallocated_encoding(s);
10508 return;
10510 /* fall through */
10511 case 0x8: /* CMGT, CMGE */
10512 case 0x9: /* CMEQ, CMLE */
10513 case 0xb: /* ABS, NEG */
10514 if (size != 3) {
10515 unallocated_encoding(s);
10516 return;
10518 break;
10519 case 0x12: /* SQXTUN */
10520 if (!u) {
10521 unallocated_encoding(s);
10522 return;
10524 /* fall through */
10525 case 0x14: /* SQXTN, UQXTN */
10526 if (size == 3) {
10527 unallocated_encoding(s);
10528 return;
10530 if (!fp_access_check(s)) {
10531 return;
10533 handle_2misc_narrow(s, true, opcode, u, false, size, rn, rd);
10534 return;
10535 case 0xc ... 0xf:
10536 case 0x16 ... 0x1d:
10537 case 0x1f:
10538 /* Floating point: U, size[1] and opcode indicate operation;
10539 * size[0] indicates single or double precision.
10541 opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
10542 size = extract32(size, 0, 1) ? 3 : 2;
10543 switch (opcode) {
10544 case 0x2c: /* FCMGT (zero) */
10545 case 0x2d: /* FCMEQ (zero) */
10546 case 0x2e: /* FCMLT (zero) */
10547 case 0x6c: /* FCMGE (zero) */
10548 case 0x6d: /* FCMLE (zero) */
10549 handle_2misc_fcmp_zero(s, opcode, true, u, true, size, rn, rd);
10550 return;
10551 case 0x1d: /* SCVTF */
10552 case 0x5d: /* UCVTF */
10554 bool is_signed = (opcode == 0x1d);
10555 if (!fp_access_check(s)) {
10556 return;
10558 handle_simd_intfp_conv(s, rd, rn, 1, is_signed, 0, size);
10559 return;
10561 case 0x3d: /* FRECPE */
10562 case 0x3f: /* FRECPX */
10563 case 0x7d: /* FRSQRTE */
10564 if (!fp_access_check(s)) {
10565 return;
10567 handle_2misc_reciprocal(s, opcode, true, u, true, size, rn, rd);
10568 return;
10569 case 0x1a: /* FCVTNS */
10570 case 0x1b: /* FCVTMS */
10571 case 0x3a: /* FCVTPS */
10572 case 0x3b: /* FCVTZS */
10573 case 0x5a: /* FCVTNU */
10574 case 0x5b: /* FCVTMU */
10575 case 0x7a: /* FCVTPU */
10576 case 0x7b: /* FCVTZU */
10577 is_fcvt = true;
10578 rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
10579 break;
10580 case 0x1c: /* FCVTAS */
10581 case 0x5c: /* FCVTAU */
10582 /* TIEAWAY doesn't fit in the usual rounding mode encoding */
10583 is_fcvt = true;
10584 rmode = FPROUNDING_TIEAWAY;
10585 break;
10586 case 0x56: /* FCVTXN, FCVTXN2 */
10587 if (size == 2) {
10588 unallocated_encoding(s);
10589 return;
10591 if (!fp_access_check(s)) {
10592 return;
10594 handle_2misc_narrow(s, true, opcode, u, false, size - 1, rn, rd);
10595 return;
10596 default:
10597 unallocated_encoding(s);
10598 return;
10600 break;
10601 default:
10602 unallocated_encoding(s);
10603 return;
10606 if (!fp_access_check(s)) {
10607 return;
10610 if (is_fcvt) {
10611 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rmode));
10612 tcg_fpstatus = fpstatus_ptr(FPST_FPCR);
10613 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
10614 } else {
10615 tcg_rmode = NULL;
10616 tcg_fpstatus = NULL;
10619 if (size == 3) {
10620 TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
10621 TCGv_i64 tcg_rd = tcg_temp_new_i64();
10623 handle_2misc_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rmode, tcg_fpstatus);
10624 write_fp_dreg(s, rd, tcg_rd);
10625 tcg_temp_free_i64(tcg_rd);
10626 tcg_temp_free_i64(tcg_rn);
10627 } else {
10628 TCGv_i32 tcg_rn = tcg_temp_new_i32();
10629 TCGv_i32 tcg_rd = tcg_temp_new_i32();
10631 read_vec_element_i32(s, tcg_rn, rn, 0, size);
10633 switch (opcode) {
10634 case 0x7: /* SQABS, SQNEG */
10636 NeonGenOneOpEnvFn *genfn;
10637 static NeonGenOneOpEnvFn * const fns[3][2] = {
10638 { gen_helper_neon_qabs_s8, gen_helper_neon_qneg_s8 },
10639 { gen_helper_neon_qabs_s16, gen_helper_neon_qneg_s16 },
10640 { gen_helper_neon_qabs_s32, gen_helper_neon_qneg_s32 },
10642 genfn = fns[size][u];
10643 genfn(tcg_rd, cpu_env, tcg_rn);
10644 break;
10646 case 0x1a: /* FCVTNS */
10647 case 0x1b: /* FCVTMS */
10648 case 0x1c: /* FCVTAS */
10649 case 0x3a: /* FCVTPS */
10650 case 0x3b: /* FCVTZS */
10652 TCGv_i32 tcg_shift = tcg_const_i32(0);
10653 gen_helper_vfp_tosls(tcg_rd, tcg_rn, tcg_shift, tcg_fpstatus);
10654 tcg_temp_free_i32(tcg_shift);
10655 break;
10657 case 0x5a: /* FCVTNU */
10658 case 0x5b: /* FCVTMU */
10659 case 0x5c: /* FCVTAU */
10660 case 0x7a: /* FCVTPU */
10661 case 0x7b: /* FCVTZU */
10663 TCGv_i32 tcg_shift = tcg_const_i32(0);
10664 gen_helper_vfp_touls(tcg_rd, tcg_rn, tcg_shift, tcg_fpstatus);
10665 tcg_temp_free_i32(tcg_shift);
10666 break;
10668 default:
10669 g_assert_not_reached();
10672 write_fp_sreg(s, rd, tcg_rd);
10673 tcg_temp_free_i32(tcg_rd);
10674 tcg_temp_free_i32(tcg_rn);
10677 if (is_fcvt) {
10678 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
10679 tcg_temp_free_i32(tcg_rmode);
10680 tcg_temp_free_ptr(tcg_fpstatus);
10684 /* SSHR[RA]/USHR[RA] - Vector shift right (optional rounding/accumulate) */
10685 static void handle_vec_simd_shri(DisasContext *s, bool is_q, bool is_u,
10686 int immh, int immb, int opcode, int rn, int rd)
10688 int size = 32 - clz32(immh) - 1;
10689 int immhb = immh << 3 | immb;
10690 int shift = 2 * (8 << size) - immhb;
10691 GVecGen2iFn *gvec_fn;
10693 if (extract32(immh, 3, 1) && !is_q) {
10694 unallocated_encoding(s);
10695 return;
10697 tcg_debug_assert(size <= 3);
10699 if (!fp_access_check(s)) {
10700 return;
10703 switch (opcode) {
10704 case 0x02: /* SSRA / USRA (accumulate) */
10705 gvec_fn = is_u ? gen_gvec_usra : gen_gvec_ssra;
10706 break;
10708 case 0x08: /* SRI */
10709 gvec_fn = gen_gvec_sri;
10710 break;
10712 case 0x00: /* SSHR / USHR */
10713 if (is_u) {
10714 if (shift == 8 << size) {
10715 /* Shift count the same size as element size produces zero. */
10716 tcg_gen_gvec_dup_imm(size, vec_full_reg_offset(s, rd),
10717 is_q ? 16 : 8, vec_full_reg_size(s), 0);
10718 return;
10720 gvec_fn = tcg_gen_gvec_shri;
10721 } else {
10722 /* Shift count the same size as element size produces all sign. */
10723 if (shift == 8 << size) {
10724 shift -= 1;
10726 gvec_fn = tcg_gen_gvec_sari;
10728 break;
10730 case 0x04: /* SRSHR / URSHR (rounding) */
10731 gvec_fn = is_u ? gen_gvec_urshr : gen_gvec_srshr;
10732 break;
10734 case 0x06: /* SRSRA / URSRA (accum + rounding) */
10735 gvec_fn = is_u ? gen_gvec_ursra : gen_gvec_srsra;
10736 break;
10738 default:
10739 g_assert_not_reached();
10742 gen_gvec_fn2i(s, is_q, rd, rn, shift, gvec_fn, size);
10745 /* SHL/SLI - Vector shift left */
10746 static void handle_vec_simd_shli(DisasContext *s, bool is_q, bool insert,
10747 int immh, int immb, int opcode, int rn, int rd)
10749 int size = 32 - clz32(immh) - 1;
10750 int immhb = immh << 3 | immb;
10751 int shift = immhb - (8 << size);
10753 /* Range of size is limited by decode: immh is a non-zero 4 bit field */
10754 assert(size >= 0 && size <= 3);
10756 if (extract32(immh, 3, 1) && !is_q) {
10757 unallocated_encoding(s);
10758 return;
10761 if (!fp_access_check(s)) {
10762 return;
10765 if (insert) {
10766 gen_gvec_fn2i(s, is_q, rd, rn, shift, gen_gvec_sli, size);
10767 } else {
10768 gen_gvec_fn2i(s, is_q, rd, rn, shift, tcg_gen_gvec_shli, size);
10772 /* USHLL/SHLL - Vector shift left with widening */
10773 static void handle_vec_simd_wshli(DisasContext *s, bool is_q, bool is_u,
10774 int immh, int immb, int opcode, int rn, int rd)
10776 int size = 32 - clz32(immh) - 1;
10777 int immhb = immh << 3 | immb;
10778 int shift = immhb - (8 << size);
10779 int dsize = 64;
10780 int esize = 8 << size;
10781 int elements = dsize/esize;
10782 TCGv_i64 tcg_rn = new_tmp_a64(s);
10783 TCGv_i64 tcg_rd = new_tmp_a64(s);
10784 int i;
10786 if (size >= 3) {
10787 unallocated_encoding(s);
10788 return;
10791 if (!fp_access_check(s)) {
10792 return;
10795 /* For the LL variants the store is larger than the load,
10796 * so if rd == rn we would overwrite parts of our input.
10797 * So load everything right now and use shifts in the main loop.
10799 read_vec_element(s, tcg_rn, rn, is_q ? 1 : 0, MO_64);
10801 for (i = 0; i < elements; i++) {
10802 tcg_gen_shri_i64(tcg_rd, tcg_rn, i * esize);
10803 ext_and_shift_reg(tcg_rd, tcg_rd, size | (!is_u << 2), 0);
10804 tcg_gen_shli_i64(tcg_rd, tcg_rd, shift);
10805 write_vec_element(s, tcg_rd, rd, i, size + 1);
10809 /* SHRN/RSHRN - Shift right with narrowing (and potential rounding) */
10810 static void handle_vec_simd_shrn(DisasContext *s, bool is_q,
10811 int immh, int immb, int opcode, int rn, int rd)
10813 int immhb = immh << 3 | immb;
10814 int size = 32 - clz32(immh) - 1;
10815 int dsize = 64;
10816 int esize = 8 << size;
10817 int elements = dsize/esize;
10818 int shift = (2 * esize) - immhb;
10819 bool round = extract32(opcode, 0, 1);
10820 TCGv_i64 tcg_rn, tcg_rd, tcg_final;
10821 TCGv_i64 tcg_round;
10822 int i;
10824 if (extract32(immh, 3, 1)) {
10825 unallocated_encoding(s);
10826 return;
10829 if (!fp_access_check(s)) {
10830 return;
10833 tcg_rn = tcg_temp_new_i64();
10834 tcg_rd = tcg_temp_new_i64();
10835 tcg_final = tcg_temp_new_i64();
10836 read_vec_element(s, tcg_final, rd, is_q ? 1 : 0, MO_64);
10838 if (round) {
10839 uint64_t round_const = 1ULL << (shift - 1);
10840 tcg_round = tcg_const_i64(round_const);
10841 } else {
10842 tcg_round = NULL;
10845 for (i = 0; i < elements; i++) {
10846 read_vec_element(s, tcg_rn, rn, i, size+1);
10847 handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
10848 false, true, size+1, shift);
10850 tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
10853 if (!is_q) {
10854 write_vec_element(s, tcg_final, rd, 0, MO_64);
10855 } else {
10856 write_vec_element(s, tcg_final, rd, 1, MO_64);
10858 if (round) {
10859 tcg_temp_free_i64(tcg_round);
10861 tcg_temp_free_i64(tcg_rn);
10862 tcg_temp_free_i64(tcg_rd);
10863 tcg_temp_free_i64(tcg_final);
10865 clear_vec_high(s, is_q, rd);
10869 /* AdvSIMD shift by immediate
10870 * 31 30 29 28 23 22 19 18 16 15 11 10 9 5 4 0
10871 * +---+---+---+-------------+------+------+--------+---+------+------+
10872 * | 0 | Q | U | 0 1 1 1 1 0 | immh | immb | opcode | 1 | Rn | Rd |
10873 * +---+---+---+-------------+------+------+--------+---+------+------+
10875 static void disas_simd_shift_imm(DisasContext *s, uint32_t insn)
10877 int rd = extract32(insn, 0, 5);
10878 int rn = extract32(insn, 5, 5);
10879 int opcode = extract32(insn, 11, 5);
10880 int immb = extract32(insn, 16, 3);
10881 int immh = extract32(insn, 19, 4);
10882 bool is_u = extract32(insn, 29, 1);
10883 bool is_q = extract32(insn, 30, 1);
10885 /* data_proc_simd[] has sent immh == 0 to disas_simd_mod_imm. */
10886 assert(immh != 0);
10888 switch (opcode) {
10889 case 0x08: /* SRI */
10890 if (!is_u) {
10891 unallocated_encoding(s);
10892 return;
10894 /* fall through */
10895 case 0x00: /* SSHR / USHR */
10896 case 0x02: /* SSRA / USRA (accumulate) */
10897 case 0x04: /* SRSHR / URSHR (rounding) */
10898 case 0x06: /* SRSRA / URSRA (accum + rounding) */
10899 handle_vec_simd_shri(s, is_q, is_u, immh, immb, opcode, rn, rd);
10900 break;
10901 case 0x0a: /* SHL / SLI */
10902 handle_vec_simd_shli(s, is_q, is_u, immh, immb, opcode, rn, rd);
10903 break;
10904 case 0x10: /* SHRN */
10905 case 0x11: /* RSHRN / SQRSHRUN */
10906 if (is_u) {
10907 handle_vec_simd_sqshrn(s, false, is_q, false, true, immh, immb,
10908 opcode, rn, rd);
10909 } else {
10910 handle_vec_simd_shrn(s, is_q, immh, immb, opcode, rn, rd);
10912 break;
10913 case 0x12: /* SQSHRN / UQSHRN */
10914 case 0x13: /* SQRSHRN / UQRSHRN */
10915 handle_vec_simd_sqshrn(s, false, is_q, is_u, is_u, immh, immb,
10916 opcode, rn, rd);
10917 break;
10918 case 0x14: /* SSHLL / USHLL */
10919 handle_vec_simd_wshli(s, is_q, is_u, immh, immb, opcode, rn, rd);
10920 break;
10921 case 0x1c: /* SCVTF / UCVTF */
10922 handle_simd_shift_intfp_conv(s, false, is_q, is_u, immh, immb,
10923 opcode, rn, rd);
10924 break;
10925 case 0xc: /* SQSHLU */
10926 if (!is_u) {
10927 unallocated_encoding(s);
10928 return;
10930 handle_simd_qshl(s, false, is_q, false, true, immh, immb, rn, rd);
10931 break;
10932 case 0xe: /* SQSHL, UQSHL */
10933 handle_simd_qshl(s, false, is_q, is_u, is_u, immh, immb, rn, rd);
10934 break;
10935 case 0x1f: /* FCVTZS/ FCVTZU */
10936 handle_simd_shift_fpint_conv(s, false, is_q, is_u, immh, immb, rn, rd);
10937 return;
10938 default:
10939 unallocated_encoding(s);
10940 return;
10944 /* Generate code to do a "long" addition or subtraction, ie one done in
10945 * TCGv_i64 on vector lanes twice the width specified by size.
10947 static void gen_neon_addl(int size, bool is_sub, TCGv_i64 tcg_res,
10948 TCGv_i64 tcg_op1, TCGv_i64 tcg_op2)
10950 static NeonGenTwo64OpFn * const fns[3][2] = {
10951 { gen_helper_neon_addl_u16, gen_helper_neon_subl_u16 },
10952 { gen_helper_neon_addl_u32, gen_helper_neon_subl_u32 },
10953 { tcg_gen_add_i64, tcg_gen_sub_i64 },
10955 NeonGenTwo64OpFn *genfn;
10956 assert(size < 3);
10958 genfn = fns[size][is_sub];
10959 genfn(tcg_res, tcg_op1, tcg_op2);
10962 static void handle_3rd_widening(DisasContext *s, int is_q, int is_u, int size,
10963 int opcode, int rd, int rn, int rm)
10965 /* 3-reg-different widening insns: 64 x 64 -> 128 */
10966 TCGv_i64 tcg_res[2];
10967 int pass, accop;
10969 tcg_res[0] = tcg_temp_new_i64();
10970 tcg_res[1] = tcg_temp_new_i64();
10972 /* Does this op do an adding accumulate, a subtracting accumulate,
10973 * or no accumulate at all?
10975 switch (opcode) {
10976 case 5:
10977 case 8:
10978 case 9:
10979 accop = 1;
10980 break;
10981 case 10:
10982 case 11:
10983 accop = -1;
10984 break;
10985 default:
10986 accop = 0;
10987 break;
10990 if (accop != 0) {
10991 read_vec_element(s, tcg_res[0], rd, 0, MO_64);
10992 read_vec_element(s, tcg_res[1], rd, 1, MO_64);
10995 /* size == 2 means two 32x32->64 operations; this is worth special
10996 * casing because we can generally handle it inline.
10998 if (size == 2) {
10999 for (pass = 0; pass < 2; pass++) {
11000 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
11001 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
11002 TCGv_i64 tcg_passres;
11003 MemOp memop = MO_32 | (is_u ? 0 : MO_SIGN);
11005 int elt = pass + is_q * 2;
11007 read_vec_element(s, tcg_op1, rn, elt, memop);
11008 read_vec_element(s, tcg_op2, rm, elt, memop);
11010 if (accop == 0) {
11011 tcg_passres = tcg_res[pass];
11012 } else {
11013 tcg_passres = tcg_temp_new_i64();
11016 switch (opcode) {
11017 case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
11018 tcg_gen_add_i64(tcg_passres, tcg_op1, tcg_op2);
11019 break;
11020 case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
11021 tcg_gen_sub_i64(tcg_passres, tcg_op1, tcg_op2);
11022 break;
11023 case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
11024 case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
11026 TCGv_i64 tcg_tmp1 = tcg_temp_new_i64();
11027 TCGv_i64 tcg_tmp2 = tcg_temp_new_i64();
11029 tcg_gen_sub_i64(tcg_tmp1, tcg_op1, tcg_op2);
11030 tcg_gen_sub_i64(tcg_tmp2, tcg_op2, tcg_op1);
11031 tcg_gen_movcond_i64(is_u ? TCG_COND_GEU : TCG_COND_GE,
11032 tcg_passres,
11033 tcg_op1, tcg_op2, tcg_tmp1, tcg_tmp2);
11034 tcg_temp_free_i64(tcg_tmp1);
11035 tcg_temp_free_i64(tcg_tmp2);
11036 break;
11038 case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
11039 case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
11040 case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
11041 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
11042 break;
11043 case 9: /* SQDMLAL, SQDMLAL2 */
11044 case 11: /* SQDMLSL, SQDMLSL2 */
11045 case 13: /* SQDMULL, SQDMULL2 */
11046 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
11047 gen_helper_neon_addl_saturate_s64(tcg_passres, cpu_env,
11048 tcg_passres, tcg_passres);
11049 break;
11050 default:
11051 g_assert_not_reached();
11054 if (opcode == 9 || opcode == 11) {
11055 /* saturating accumulate ops */
11056 if (accop < 0) {
11057 tcg_gen_neg_i64(tcg_passres, tcg_passres);
11059 gen_helper_neon_addl_saturate_s64(tcg_res[pass], cpu_env,
11060 tcg_res[pass], tcg_passres);
11061 } else if (accop > 0) {
11062 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
11063 } else if (accop < 0) {
11064 tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
11067 if (accop != 0) {
11068 tcg_temp_free_i64(tcg_passres);
11071 tcg_temp_free_i64(tcg_op1);
11072 tcg_temp_free_i64(tcg_op2);
11074 } else {
11075 /* size 0 or 1, generally helper functions */
11076 for (pass = 0; pass < 2; pass++) {
11077 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11078 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11079 TCGv_i64 tcg_passres;
11080 int elt = pass + is_q * 2;
11082 read_vec_element_i32(s, tcg_op1, rn, elt, MO_32);
11083 read_vec_element_i32(s, tcg_op2, rm, elt, MO_32);
11085 if (accop == 0) {
11086 tcg_passres = tcg_res[pass];
11087 } else {
11088 tcg_passres = tcg_temp_new_i64();
11091 switch (opcode) {
11092 case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
11093 case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
11095 TCGv_i64 tcg_op2_64 = tcg_temp_new_i64();
11096 static NeonGenWidenFn * const widenfns[2][2] = {
11097 { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
11098 { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
11100 NeonGenWidenFn *widenfn = widenfns[size][is_u];
11102 widenfn(tcg_op2_64, tcg_op2);
11103 widenfn(tcg_passres, tcg_op1);
11104 gen_neon_addl(size, (opcode == 2), tcg_passres,
11105 tcg_passres, tcg_op2_64);
11106 tcg_temp_free_i64(tcg_op2_64);
11107 break;
11109 case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
11110 case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
11111 if (size == 0) {
11112 if (is_u) {
11113 gen_helper_neon_abdl_u16(tcg_passres, tcg_op1, tcg_op2);
11114 } else {
11115 gen_helper_neon_abdl_s16(tcg_passres, tcg_op1, tcg_op2);
11117 } else {
11118 if (is_u) {
11119 gen_helper_neon_abdl_u32(tcg_passres, tcg_op1, tcg_op2);
11120 } else {
11121 gen_helper_neon_abdl_s32(tcg_passres, tcg_op1, tcg_op2);
11124 break;
11125 case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
11126 case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
11127 case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
11128 if (size == 0) {
11129 if (is_u) {
11130 gen_helper_neon_mull_u8(tcg_passres, tcg_op1, tcg_op2);
11131 } else {
11132 gen_helper_neon_mull_s8(tcg_passres, tcg_op1, tcg_op2);
11134 } else {
11135 if (is_u) {
11136 gen_helper_neon_mull_u16(tcg_passres, tcg_op1, tcg_op2);
11137 } else {
11138 gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
11141 break;
11142 case 9: /* SQDMLAL, SQDMLAL2 */
11143 case 11: /* SQDMLSL, SQDMLSL2 */
11144 case 13: /* SQDMULL, SQDMULL2 */
11145 assert(size == 1);
11146 gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
11147 gen_helper_neon_addl_saturate_s32(tcg_passres, cpu_env,
11148 tcg_passres, tcg_passres);
11149 break;
11150 default:
11151 g_assert_not_reached();
11153 tcg_temp_free_i32(tcg_op1);
11154 tcg_temp_free_i32(tcg_op2);
11156 if (accop != 0) {
11157 if (opcode == 9 || opcode == 11) {
11158 /* saturating accumulate ops */
11159 if (accop < 0) {
11160 gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
11162 gen_helper_neon_addl_saturate_s32(tcg_res[pass], cpu_env,
11163 tcg_res[pass],
11164 tcg_passres);
11165 } else {
11166 gen_neon_addl(size, (accop < 0), tcg_res[pass],
11167 tcg_res[pass], tcg_passres);
11169 tcg_temp_free_i64(tcg_passres);
11174 write_vec_element(s, tcg_res[0], rd, 0, MO_64);
11175 write_vec_element(s, tcg_res[1], rd, 1, MO_64);
11176 tcg_temp_free_i64(tcg_res[0]);
11177 tcg_temp_free_i64(tcg_res[1]);
11180 static void handle_3rd_wide(DisasContext *s, int is_q, int is_u, int size,
11181 int opcode, int rd, int rn, int rm)
11183 TCGv_i64 tcg_res[2];
11184 int part = is_q ? 2 : 0;
11185 int pass;
11187 for (pass = 0; pass < 2; pass++) {
11188 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
11189 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11190 TCGv_i64 tcg_op2_wide = tcg_temp_new_i64();
11191 static NeonGenWidenFn * const widenfns[3][2] = {
11192 { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
11193 { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
11194 { tcg_gen_ext_i32_i64, tcg_gen_extu_i32_i64 },
11196 NeonGenWidenFn *widenfn = widenfns[size][is_u];
11198 read_vec_element(s, tcg_op1, rn, pass, MO_64);
11199 read_vec_element_i32(s, tcg_op2, rm, part + pass, MO_32);
11200 widenfn(tcg_op2_wide, tcg_op2);
11201 tcg_temp_free_i32(tcg_op2);
11202 tcg_res[pass] = tcg_temp_new_i64();
11203 gen_neon_addl(size, (opcode == 3),
11204 tcg_res[pass], tcg_op1, tcg_op2_wide);
11205 tcg_temp_free_i64(tcg_op1);
11206 tcg_temp_free_i64(tcg_op2_wide);
11209 for (pass = 0; pass < 2; pass++) {
11210 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
11211 tcg_temp_free_i64(tcg_res[pass]);
11215 static void do_narrow_round_high_u32(TCGv_i32 res, TCGv_i64 in)
11217 tcg_gen_addi_i64(in, in, 1U << 31);
11218 tcg_gen_extrh_i64_i32(res, in);
11221 static void handle_3rd_narrowing(DisasContext *s, int is_q, int is_u, int size,
11222 int opcode, int rd, int rn, int rm)
11224 TCGv_i32 tcg_res[2];
11225 int part = is_q ? 2 : 0;
11226 int pass;
11228 for (pass = 0; pass < 2; pass++) {
11229 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
11230 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
11231 TCGv_i64 tcg_wideres = tcg_temp_new_i64();
11232 static NeonGenNarrowFn * const narrowfns[3][2] = {
11233 { gen_helper_neon_narrow_high_u8,
11234 gen_helper_neon_narrow_round_high_u8 },
11235 { gen_helper_neon_narrow_high_u16,
11236 gen_helper_neon_narrow_round_high_u16 },
11237 { tcg_gen_extrh_i64_i32, do_narrow_round_high_u32 },
11239 NeonGenNarrowFn *gennarrow = narrowfns[size][is_u];
11241 read_vec_element(s, tcg_op1, rn, pass, MO_64);
11242 read_vec_element(s, tcg_op2, rm, pass, MO_64);
11244 gen_neon_addl(size, (opcode == 6), tcg_wideres, tcg_op1, tcg_op2);
11246 tcg_temp_free_i64(tcg_op1);
11247 tcg_temp_free_i64(tcg_op2);
11249 tcg_res[pass] = tcg_temp_new_i32();
11250 gennarrow(tcg_res[pass], tcg_wideres);
11251 tcg_temp_free_i64(tcg_wideres);
11254 for (pass = 0; pass < 2; pass++) {
11255 write_vec_element_i32(s, tcg_res[pass], rd, pass + part, MO_32);
11256 tcg_temp_free_i32(tcg_res[pass]);
11258 clear_vec_high(s, is_q, rd);
11261 /* AdvSIMD three different
11262 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 0
11263 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
11264 * | 0 | Q | U | 0 1 1 1 0 | size | 1 | Rm | opcode | 0 0 | Rn | Rd |
11265 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
11267 static void disas_simd_three_reg_diff(DisasContext *s, uint32_t insn)
11269 /* Instructions in this group fall into three basic classes
11270 * (in each case with the operation working on each element in
11271 * the input vectors):
11272 * (1) widening 64 x 64 -> 128 (with possibly Vd as an extra
11273 * 128 bit input)
11274 * (2) wide 64 x 128 -> 128
11275 * (3) narrowing 128 x 128 -> 64
11276 * Here we do initial decode, catch unallocated cases and
11277 * dispatch to separate functions for each class.
11279 int is_q = extract32(insn, 30, 1);
11280 int is_u = extract32(insn, 29, 1);
11281 int size = extract32(insn, 22, 2);
11282 int opcode = extract32(insn, 12, 4);
11283 int rm = extract32(insn, 16, 5);
11284 int rn = extract32(insn, 5, 5);
11285 int rd = extract32(insn, 0, 5);
11287 switch (opcode) {
11288 case 1: /* SADDW, SADDW2, UADDW, UADDW2 */
11289 case 3: /* SSUBW, SSUBW2, USUBW, USUBW2 */
11290 /* 64 x 128 -> 128 */
11291 if (size == 3) {
11292 unallocated_encoding(s);
11293 return;
11295 if (!fp_access_check(s)) {
11296 return;
11298 handle_3rd_wide(s, is_q, is_u, size, opcode, rd, rn, rm);
11299 break;
11300 case 4: /* ADDHN, ADDHN2, RADDHN, RADDHN2 */
11301 case 6: /* SUBHN, SUBHN2, RSUBHN, RSUBHN2 */
11302 /* 128 x 128 -> 64 */
11303 if (size == 3) {
11304 unallocated_encoding(s);
11305 return;
11307 if (!fp_access_check(s)) {
11308 return;
11310 handle_3rd_narrowing(s, is_q, is_u, size, opcode, rd, rn, rm);
11311 break;
11312 case 14: /* PMULL, PMULL2 */
11313 if (is_u) {
11314 unallocated_encoding(s);
11315 return;
11317 switch (size) {
11318 case 0: /* PMULL.P8 */
11319 if (!fp_access_check(s)) {
11320 return;
11322 /* The Q field specifies lo/hi half input for this insn. */
11323 gen_gvec_op3_ool(s, true, rd, rn, rm, is_q,
11324 gen_helper_neon_pmull_h);
11325 break;
11327 case 3: /* PMULL.P64 */
11328 if (!dc_isar_feature(aa64_pmull, s)) {
11329 unallocated_encoding(s);
11330 return;
11332 if (!fp_access_check(s)) {
11333 return;
11335 /* The Q field specifies lo/hi half input for this insn. */
11336 gen_gvec_op3_ool(s, true, rd, rn, rm, is_q,
11337 gen_helper_gvec_pmull_q);
11338 break;
11340 default:
11341 unallocated_encoding(s);
11342 break;
11344 return;
11345 case 9: /* SQDMLAL, SQDMLAL2 */
11346 case 11: /* SQDMLSL, SQDMLSL2 */
11347 case 13: /* SQDMULL, SQDMULL2 */
11348 if (is_u || size == 0) {
11349 unallocated_encoding(s);
11350 return;
11352 /* fall through */
11353 case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
11354 case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
11355 case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
11356 case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
11357 case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
11358 case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
11359 case 12: /* SMULL, SMULL2, UMULL, UMULL2 */
11360 /* 64 x 64 -> 128 */
11361 if (size == 3) {
11362 unallocated_encoding(s);
11363 return;
11365 if (!fp_access_check(s)) {
11366 return;
11369 handle_3rd_widening(s, is_q, is_u, size, opcode, rd, rn, rm);
11370 break;
11371 default:
11372 /* opcode 15 not allocated */
11373 unallocated_encoding(s);
11374 break;
11378 /* Logic op (opcode == 3) subgroup of C3.6.16. */
11379 static void disas_simd_3same_logic(DisasContext *s, uint32_t insn)
11381 int rd = extract32(insn, 0, 5);
11382 int rn = extract32(insn, 5, 5);
11383 int rm = extract32(insn, 16, 5);
11384 int size = extract32(insn, 22, 2);
11385 bool is_u = extract32(insn, 29, 1);
11386 bool is_q = extract32(insn, 30, 1);
11388 if (!fp_access_check(s)) {
11389 return;
11392 switch (size + 4 * is_u) {
11393 case 0: /* AND */
11394 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_and, 0);
11395 return;
11396 case 1: /* BIC */
11397 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_andc, 0);
11398 return;
11399 case 2: /* ORR */
11400 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_or, 0);
11401 return;
11402 case 3: /* ORN */
11403 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_orc, 0);
11404 return;
11405 case 4: /* EOR */
11406 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_xor, 0);
11407 return;
11409 case 5: /* BSL bitwise select */
11410 gen_gvec_fn4(s, is_q, rd, rd, rn, rm, tcg_gen_gvec_bitsel, 0);
11411 return;
11412 case 6: /* BIT, bitwise insert if true */
11413 gen_gvec_fn4(s, is_q, rd, rm, rn, rd, tcg_gen_gvec_bitsel, 0);
11414 return;
11415 case 7: /* BIF, bitwise insert if false */
11416 gen_gvec_fn4(s, is_q, rd, rm, rd, rn, tcg_gen_gvec_bitsel, 0);
11417 return;
11419 default:
11420 g_assert_not_reached();
11424 /* Pairwise op subgroup of C3.6.16.
11426 * This is called directly or via the handle_3same_float for float pairwise
11427 * operations where the opcode and size are calculated differently.
11429 static void handle_simd_3same_pair(DisasContext *s, int is_q, int u, int opcode,
11430 int size, int rn, int rm, int rd)
11432 TCGv_ptr fpst;
11433 int pass;
11435 /* Floating point operations need fpst */
11436 if (opcode >= 0x58) {
11437 fpst = fpstatus_ptr(FPST_FPCR);
11438 } else {
11439 fpst = NULL;
11442 if (!fp_access_check(s)) {
11443 return;
11446 /* These operations work on the concatenated rm:rn, with each pair of
11447 * adjacent elements being operated on to produce an element in the result.
11449 if (size == 3) {
11450 TCGv_i64 tcg_res[2];
11452 for (pass = 0; pass < 2; pass++) {
11453 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
11454 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
11455 int passreg = (pass == 0) ? rn : rm;
11457 read_vec_element(s, tcg_op1, passreg, 0, MO_64);
11458 read_vec_element(s, tcg_op2, passreg, 1, MO_64);
11459 tcg_res[pass] = tcg_temp_new_i64();
11461 switch (opcode) {
11462 case 0x17: /* ADDP */
11463 tcg_gen_add_i64(tcg_res[pass], tcg_op1, tcg_op2);
11464 break;
11465 case 0x58: /* FMAXNMP */
11466 gen_helper_vfp_maxnumd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11467 break;
11468 case 0x5a: /* FADDP */
11469 gen_helper_vfp_addd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11470 break;
11471 case 0x5e: /* FMAXP */
11472 gen_helper_vfp_maxd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11473 break;
11474 case 0x78: /* FMINNMP */
11475 gen_helper_vfp_minnumd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11476 break;
11477 case 0x7e: /* FMINP */
11478 gen_helper_vfp_mind(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11479 break;
11480 default:
11481 g_assert_not_reached();
11484 tcg_temp_free_i64(tcg_op1);
11485 tcg_temp_free_i64(tcg_op2);
11488 for (pass = 0; pass < 2; pass++) {
11489 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
11490 tcg_temp_free_i64(tcg_res[pass]);
11492 } else {
11493 int maxpass = is_q ? 4 : 2;
11494 TCGv_i32 tcg_res[4];
11496 for (pass = 0; pass < maxpass; pass++) {
11497 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11498 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11499 NeonGenTwoOpFn *genfn = NULL;
11500 int passreg = pass < (maxpass / 2) ? rn : rm;
11501 int passelt = (is_q && (pass & 1)) ? 2 : 0;
11503 read_vec_element_i32(s, tcg_op1, passreg, passelt, MO_32);
11504 read_vec_element_i32(s, tcg_op2, passreg, passelt + 1, MO_32);
11505 tcg_res[pass] = tcg_temp_new_i32();
11507 switch (opcode) {
11508 case 0x17: /* ADDP */
11510 static NeonGenTwoOpFn * const fns[3] = {
11511 gen_helper_neon_padd_u8,
11512 gen_helper_neon_padd_u16,
11513 tcg_gen_add_i32,
11515 genfn = fns[size];
11516 break;
11518 case 0x14: /* SMAXP, UMAXP */
11520 static NeonGenTwoOpFn * const fns[3][2] = {
11521 { gen_helper_neon_pmax_s8, gen_helper_neon_pmax_u8 },
11522 { gen_helper_neon_pmax_s16, gen_helper_neon_pmax_u16 },
11523 { tcg_gen_smax_i32, tcg_gen_umax_i32 },
11525 genfn = fns[size][u];
11526 break;
11528 case 0x15: /* SMINP, UMINP */
11530 static NeonGenTwoOpFn * const fns[3][2] = {
11531 { gen_helper_neon_pmin_s8, gen_helper_neon_pmin_u8 },
11532 { gen_helper_neon_pmin_s16, gen_helper_neon_pmin_u16 },
11533 { tcg_gen_smin_i32, tcg_gen_umin_i32 },
11535 genfn = fns[size][u];
11536 break;
11538 /* The FP operations are all on single floats (32 bit) */
11539 case 0x58: /* FMAXNMP */
11540 gen_helper_vfp_maxnums(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11541 break;
11542 case 0x5a: /* FADDP */
11543 gen_helper_vfp_adds(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11544 break;
11545 case 0x5e: /* FMAXP */
11546 gen_helper_vfp_maxs(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11547 break;
11548 case 0x78: /* FMINNMP */
11549 gen_helper_vfp_minnums(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11550 break;
11551 case 0x7e: /* FMINP */
11552 gen_helper_vfp_mins(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11553 break;
11554 default:
11555 g_assert_not_reached();
11558 /* FP ops called directly, otherwise call now */
11559 if (genfn) {
11560 genfn(tcg_res[pass], tcg_op1, tcg_op2);
11563 tcg_temp_free_i32(tcg_op1);
11564 tcg_temp_free_i32(tcg_op2);
11567 for (pass = 0; pass < maxpass; pass++) {
11568 write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_32);
11569 tcg_temp_free_i32(tcg_res[pass]);
11571 clear_vec_high(s, is_q, rd);
11574 if (fpst) {
11575 tcg_temp_free_ptr(fpst);
11579 /* Floating point op subgroup of C3.6.16. */
11580 static void disas_simd_3same_float(DisasContext *s, uint32_t insn)
11582 /* For floating point ops, the U, size[1] and opcode bits
11583 * together indicate the operation. size[0] indicates single
11584 * or double.
11586 int fpopcode = extract32(insn, 11, 5)
11587 | (extract32(insn, 23, 1) << 5)
11588 | (extract32(insn, 29, 1) << 6);
11589 int is_q = extract32(insn, 30, 1);
11590 int size = extract32(insn, 22, 1);
11591 int rm = extract32(insn, 16, 5);
11592 int rn = extract32(insn, 5, 5);
11593 int rd = extract32(insn, 0, 5);
11595 int datasize = is_q ? 128 : 64;
11596 int esize = 32 << size;
11597 int elements = datasize / esize;
11599 if (size == 1 && !is_q) {
11600 unallocated_encoding(s);
11601 return;
11604 switch (fpopcode) {
11605 case 0x58: /* FMAXNMP */
11606 case 0x5a: /* FADDP */
11607 case 0x5e: /* FMAXP */
11608 case 0x78: /* FMINNMP */
11609 case 0x7e: /* FMINP */
11610 if (size && !is_q) {
11611 unallocated_encoding(s);
11612 return;
11614 handle_simd_3same_pair(s, is_q, 0, fpopcode, size ? MO_64 : MO_32,
11615 rn, rm, rd);
11616 return;
11617 case 0x1b: /* FMULX */
11618 case 0x1f: /* FRECPS */
11619 case 0x3f: /* FRSQRTS */
11620 case 0x5d: /* FACGE */
11621 case 0x7d: /* FACGT */
11622 case 0x19: /* FMLA */
11623 case 0x39: /* FMLS */
11624 case 0x18: /* FMAXNM */
11625 case 0x1a: /* FADD */
11626 case 0x1c: /* FCMEQ */
11627 case 0x1e: /* FMAX */
11628 case 0x38: /* FMINNM */
11629 case 0x3a: /* FSUB */
11630 case 0x3e: /* FMIN */
11631 case 0x5b: /* FMUL */
11632 case 0x5c: /* FCMGE */
11633 case 0x5f: /* FDIV */
11634 case 0x7a: /* FABD */
11635 case 0x7c: /* FCMGT */
11636 if (!fp_access_check(s)) {
11637 return;
11639 handle_3same_float(s, size, elements, fpopcode, rd, rn, rm);
11640 return;
11642 case 0x1d: /* FMLAL */
11643 case 0x3d: /* FMLSL */
11644 case 0x59: /* FMLAL2 */
11645 case 0x79: /* FMLSL2 */
11646 if (size & 1 || !dc_isar_feature(aa64_fhm, s)) {
11647 unallocated_encoding(s);
11648 return;
11650 if (fp_access_check(s)) {
11651 int is_s = extract32(insn, 23, 1);
11652 int is_2 = extract32(insn, 29, 1);
11653 int data = (is_2 << 1) | is_s;
11654 tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
11655 vec_full_reg_offset(s, rn),
11656 vec_full_reg_offset(s, rm), cpu_env,
11657 is_q ? 16 : 8, vec_full_reg_size(s),
11658 data, gen_helper_gvec_fmlal_a64);
11660 return;
11662 default:
11663 unallocated_encoding(s);
11664 return;
11668 /* Integer op subgroup of C3.6.16. */
11669 static void disas_simd_3same_int(DisasContext *s, uint32_t insn)
11671 int is_q = extract32(insn, 30, 1);
11672 int u = extract32(insn, 29, 1);
11673 int size = extract32(insn, 22, 2);
11674 int opcode = extract32(insn, 11, 5);
11675 int rm = extract32(insn, 16, 5);
11676 int rn = extract32(insn, 5, 5);
11677 int rd = extract32(insn, 0, 5);
11678 int pass;
11679 TCGCond cond;
11681 switch (opcode) {
11682 case 0x13: /* MUL, PMUL */
11683 if (u && size != 0) {
11684 unallocated_encoding(s);
11685 return;
11687 /* fall through */
11688 case 0x0: /* SHADD, UHADD */
11689 case 0x2: /* SRHADD, URHADD */
11690 case 0x4: /* SHSUB, UHSUB */
11691 case 0xc: /* SMAX, UMAX */
11692 case 0xd: /* SMIN, UMIN */
11693 case 0xe: /* SABD, UABD */
11694 case 0xf: /* SABA, UABA */
11695 case 0x12: /* MLA, MLS */
11696 if (size == 3) {
11697 unallocated_encoding(s);
11698 return;
11700 break;
11701 case 0x16: /* SQDMULH, SQRDMULH */
11702 if (size == 0 || size == 3) {
11703 unallocated_encoding(s);
11704 return;
11706 break;
11707 default:
11708 if (size == 3 && !is_q) {
11709 unallocated_encoding(s);
11710 return;
11712 break;
11715 if (!fp_access_check(s)) {
11716 return;
11719 switch (opcode) {
11720 case 0x01: /* SQADD, UQADD */
11721 if (u) {
11722 gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uqadd_qc, size);
11723 } else {
11724 gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqadd_qc, size);
11726 return;
11727 case 0x05: /* SQSUB, UQSUB */
11728 if (u) {
11729 gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uqsub_qc, size);
11730 } else {
11731 gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqsub_qc, size);
11733 return;
11734 case 0x08: /* SSHL, USHL */
11735 if (u) {
11736 gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_ushl, size);
11737 } else {
11738 gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sshl, size);
11740 return;
11741 case 0x0c: /* SMAX, UMAX */
11742 if (u) {
11743 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_umax, size);
11744 } else {
11745 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_smax, size);
11747 return;
11748 case 0x0d: /* SMIN, UMIN */
11749 if (u) {
11750 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_umin, size);
11751 } else {
11752 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_smin, size);
11754 return;
11755 case 0xe: /* SABD, UABD */
11756 if (u) {
11757 gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uabd, size);
11758 } else {
11759 gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sabd, size);
11761 return;
11762 case 0xf: /* SABA, UABA */
11763 if (u) {
11764 gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_uaba, size);
11765 } else {
11766 gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_saba, size);
11768 return;
11769 case 0x10: /* ADD, SUB */
11770 if (u) {
11771 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_sub, size);
11772 } else {
11773 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_add, size);
11775 return;
11776 case 0x13: /* MUL, PMUL */
11777 if (!u) { /* MUL */
11778 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_mul, size);
11779 } else { /* PMUL */
11780 gen_gvec_op3_ool(s, is_q, rd, rn, rm, 0, gen_helper_gvec_pmul_b);
11782 return;
11783 case 0x12: /* MLA, MLS */
11784 if (u) {
11785 gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_mls, size);
11786 } else {
11787 gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_mla, size);
11789 return;
11790 case 0x16: /* SQDMULH, SQRDMULH */
11792 static gen_helper_gvec_3_ptr * const fns[2][2] = {
11793 { gen_helper_neon_sqdmulh_h, gen_helper_neon_sqrdmulh_h },
11794 { gen_helper_neon_sqdmulh_s, gen_helper_neon_sqrdmulh_s },
11796 gen_gvec_op3_qc(s, is_q, rd, rn, rm, fns[size - 1][u]);
11798 return;
11799 case 0x11:
11800 if (!u) { /* CMTST */
11801 gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_cmtst, size);
11802 return;
11804 /* else CMEQ */
11805 cond = TCG_COND_EQ;
11806 goto do_gvec_cmp;
11807 case 0x06: /* CMGT, CMHI */
11808 cond = u ? TCG_COND_GTU : TCG_COND_GT;
11809 goto do_gvec_cmp;
11810 case 0x07: /* CMGE, CMHS */
11811 cond = u ? TCG_COND_GEU : TCG_COND_GE;
11812 do_gvec_cmp:
11813 tcg_gen_gvec_cmp(cond, size, vec_full_reg_offset(s, rd),
11814 vec_full_reg_offset(s, rn),
11815 vec_full_reg_offset(s, rm),
11816 is_q ? 16 : 8, vec_full_reg_size(s));
11817 return;
11820 if (size == 3) {
11821 assert(is_q);
11822 for (pass = 0; pass < 2; pass++) {
11823 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
11824 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
11825 TCGv_i64 tcg_res = tcg_temp_new_i64();
11827 read_vec_element(s, tcg_op1, rn, pass, MO_64);
11828 read_vec_element(s, tcg_op2, rm, pass, MO_64);
11830 handle_3same_64(s, opcode, u, tcg_res, tcg_op1, tcg_op2);
11832 write_vec_element(s, tcg_res, rd, pass, MO_64);
11834 tcg_temp_free_i64(tcg_res);
11835 tcg_temp_free_i64(tcg_op1);
11836 tcg_temp_free_i64(tcg_op2);
11838 } else {
11839 for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
11840 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11841 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11842 TCGv_i32 tcg_res = tcg_temp_new_i32();
11843 NeonGenTwoOpFn *genfn = NULL;
11844 NeonGenTwoOpEnvFn *genenvfn = NULL;
11846 read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
11847 read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
11849 switch (opcode) {
11850 case 0x0: /* SHADD, UHADD */
11852 static NeonGenTwoOpFn * const fns[3][2] = {
11853 { gen_helper_neon_hadd_s8, gen_helper_neon_hadd_u8 },
11854 { gen_helper_neon_hadd_s16, gen_helper_neon_hadd_u16 },
11855 { gen_helper_neon_hadd_s32, gen_helper_neon_hadd_u32 },
11857 genfn = fns[size][u];
11858 break;
11860 case 0x2: /* SRHADD, URHADD */
11862 static NeonGenTwoOpFn * const fns[3][2] = {
11863 { gen_helper_neon_rhadd_s8, gen_helper_neon_rhadd_u8 },
11864 { gen_helper_neon_rhadd_s16, gen_helper_neon_rhadd_u16 },
11865 { gen_helper_neon_rhadd_s32, gen_helper_neon_rhadd_u32 },
11867 genfn = fns[size][u];
11868 break;
11870 case 0x4: /* SHSUB, UHSUB */
11872 static NeonGenTwoOpFn * const fns[3][2] = {
11873 { gen_helper_neon_hsub_s8, gen_helper_neon_hsub_u8 },
11874 { gen_helper_neon_hsub_s16, gen_helper_neon_hsub_u16 },
11875 { gen_helper_neon_hsub_s32, gen_helper_neon_hsub_u32 },
11877 genfn = fns[size][u];
11878 break;
11880 case 0x9: /* SQSHL, UQSHL */
11882 static NeonGenTwoOpEnvFn * const fns[3][2] = {
11883 { gen_helper_neon_qshl_s8, gen_helper_neon_qshl_u8 },
11884 { gen_helper_neon_qshl_s16, gen_helper_neon_qshl_u16 },
11885 { gen_helper_neon_qshl_s32, gen_helper_neon_qshl_u32 },
11887 genenvfn = fns[size][u];
11888 break;
11890 case 0xa: /* SRSHL, URSHL */
11892 static NeonGenTwoOpFn * const fns[3][2] = {
11893 { gen_helper_neon_rshl_s8, gen_helper_neon_rshl_u8 },
11894 { gen_helper_neon_rshl_s16, gen_helper_neon_rshl_u16 },
11895 { gen_helper_neon_rshl_s32, gen_helper_neon_rshl_u32 },
11897 genfn = fns[size][u];
11898 break;
11900 case 0xb: /* SQRSHL, UQRSHL */
11902 static NeonGenTwoOpEnvFn * const fns[3][2] = {
11903 { gen_helper_neon_qrshl_s8, gen_helper_neon_qrshl_u8 },
11904 { gen_helper_neon_qrshl_s16, gen_helper_neon_qrshl_u16 },
11905 { gen_helper_neon_qrshl_s32, gen_helper_neon_qrshl_u32 },
11907 genenvfn = fns[size][u];
11908 break;
11910 default:
11911 g_assert_not_reached();
11914 if (genenvfn) {
11915 genenvfn(tcg_res, cpu_env, tcg_op1, tcg_op2);
11916 } else {
11917 genfn(tcg_res, tcg_op1, tcg_op2);
11920 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
11922 tcg_temp_free_i32(tcg_res);
11923 tcg_temp_free_i32(tcg_op1);
11924 tcg_temp_free_i32(tcg_op2);
11927 clear_vec_high(s, is_q, rd);
11930 /* AdvSIMD three same
11931 * 31 30 29 28 24 23 22 21 20 16 15 11 10 9 5 4 0
11932 * +---+---+---+-----------+------+---+------+--------+---+------+------+
11933 * | 0 | Q | U | 0 1 1 1 0 | size | 1 | Rm | opcode | 1 | Rn | Rd |
11934 * +---+---+---+-----------+------+---+------+--------+---+------+------+
11936 static void disas_simd_three_reg_same(DisasContext *s, uint32_t insn)
11938 int opcode = extract32(insn, 11, 5);
11940 switch (opcode) {
11941 case 0x3: /* logic ops */
11942 disas_simd_3same_logic(s, insn);
11943 break;
11944 case 0x17: /* ADDP */
11945 case 0x14: /* SMAXP, UMAXP */
11946 case 0x15: /* SMINP, UMINP */
11948 /* Pairwise operations */
11949 int is_q = extract32(insn, 30, 1);
11950 int u = extract32(insn, 29, 1);
11951 int size = extract32(insn, 22, 2);
11952 int rm = extract32(insn, 16, 5);
11953 int rn = extract32(insn, 5, 5);
11954 int rd = extract32(insn, 0, 5);
11955 if (opcode == 0x17) {
11956 if (u || (size == 3 && !is_q)) {
11957 unallocated_encoding(s);
11958 return;
11960 } else {
11961 if (size == 3) {
11962 unallocated_encoding(s);
11963 return;
11966 handle_simd_3same_pair(s, is_q, u, opcode, size, rn, rm, rd);
11967 break;
11969 case 0x18 ... 0x31:
11970 /* floating point ops, sz[1] and U are part of opcode */
11971 disas_simd_3same_float(s, insn);
11972 break;
11973 default:
11974 disas_simd_3same_int(s, insn);
11975 break;
11980 * Advanced SIMD three same (ARMv8.2 FP16 variants)
11982 * 31 30 29 28 24 23 22 21 20 16 15 14 13 11 10 9 5 4 0
11983 * +---+---+---+-----------+---------+------+-----+--------+---+------+------+
11984 * | 0 | Q | U | 0 1 1 1 0 | a | 1 0 | Rm | 0 0 | opcode | 1 | Rn | Rd |
11985 * +---+---+---+-----------+---------+------+-----+--------+---+------+------+
11987 * This includes FMULX, FCMEQ (register), FRECPS, FRSQRTS, FCMGE
11988 * (register), FACGE, FABD, FCMGT (register) and FACGT.
11991 static void disas_simd_three_reg_same_fp16(DisasContext *s, uint32_t insn)
11993 int opcode, fpopcode;
11994 int is_q, u, a, rm, rn, rd;
11995 int datasize, elements;
11996 int pass;
11997 TCGv_ptr fpst;
11998 bool pairwise = false;
12000 if (!dc_isar_feature(aa64_fp16, s)) {
12001 unallocated_encoding(s);
12002 return;
12005 if (!fp_access_check(s)) {
12006 return;
12009 /* For these floating point ops, the U, a and opcode bits
12010 * together indicate the operation.
12012 opcode = extract32(insn, 11, 3);
12013 u = extract32(insn, 29, 1);
12014 a = extract32(insn, 23, 1);
12015 is_q = extract32(insn, 30, 1);
12016 rm = extract32(insn, 16, 5);
12017 rn = extract32(insn, 5, 5);
12018 rd = extract32(insn, 0, 5);
12020 fpopcode = opcode | (a << 3) | (u << 4);
12021 datasize = is_q ? 128 : 64;
12022 elements = datasize / 16;
12024 switch (fpopcode) {
12025 case 0x10: /* FMAXNMP */
12026 case 0x12: /* FADDP */
12027 case 0x16: /* FMAXP */
12028 case 0x18: /* FMINNMP */
12029 case 0x1e: /* FMINP */
12030 pairwise = true;
12031 break;
12034 fpst = fpstatus_ptr(FPST_FPCR_F16);
12036 if (pairwise) {
12037 int maxpass = is_q ? 8 : 4;
12038 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
12039 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
12040 TCGv_i32 tcg_res[8];
12042 for (pass = 0; pass < maxpass; pass++) {
12043 int passreg = pass < (maxpass / 2) ? rn : rm;
12044 int passelt = (pass << 1) & (maxpass - 1);
12046 read_vec_element_i32(s, tcg_op1, passreg, passelt, MO_16);
12047 read_vec_element_i32(s, tcg_op2, passreg, passelt + 1, MO_16);
12048 tcg_res[pass] = tcg_temp_new_i32();
12050 switch (fpopcode) {
12051 case 0x10: /* FMAXNMP */
12052 gen_helper_advsimd_maxnumh(tcg_res[pass], tcg_op1, tcg_op2,
12053 fpst);
12054 break;
12055 case 0x12: /* FADDP */
12056 gen_helper_advsimd_addh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
12057 break;
12058 case 0x16: /* FMAXP */
12059 gen_helper_advsimd_maxh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
12060 break;
12061 case 0x18: /* FMINNMP */
12062 gen_helper_advsimd_minnumh(tcg_res[pass], tcg_op1, tcg_op2,
12063 fpst);
12064 break;
12065 case 0x1e: /* FMINP */
12066 gen_helper_advsimd_minh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
12067 break;
12068 default:
12069 g_assert_not_reached();
12073 for (pass = 0; pass < maxpass; pass++) {
12074 write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_16);
12075 tcg_temp_free_i32(tcg_res[pass]);
12078 tcg_temp_free_i32(tcg_op1);
12079 tcg_temp_free_i32(tcg_op2);
12081 } else {
12082 for (pass = 0; pass < elements; pass++) {
12083 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
12084 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
12085 TCGv_i32 tcg_res = tcg_temp_new_i32();
12087 read_vec_element_i32(s, tcg_op1, rn, pass, MO_16);
12088 read_vec_element_i32(s, tcg_op2, rm, pass, MO_16);
12090 switch (fpopcode) {
12091 case 0x0: /* FMAXNM */
12092 gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
12093 break;
12094 case 0x1: /* FMLA */
12095 read_vec_element_i32(s, tcg_res, rd, pass, MO_16);
12096 gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_res,
12097 fpst);
12098 break;
12099 case 0x2: /* FADD */
12100 gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
12101 break;
12102 case 0x3: /* FMULX */
12103 gen_helper_advsimd_mulxh(tcg_res, tcg_op1, tcg_op2, fpst);
12104 break;
12105 case 0x4: /* FCMEQ */
12106 gen_helper_advsimd_ceq_f16(tcg_res, tcg_op1, tcg_op2, fpst);
12107 break;
12108 case 0x6: /* FMAX */
12109 gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
12110 break;
12111 case 0x7: /* FRECPS */
12112 gen_helper_recpsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
12113 break;
12114 case 0x8: /* FMINNM */
12115 gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
12116 break;
12117 case 0x9: /* FMLS */
12118 /* As usual for ARM, separate negation for fused multiply-add */
12119 tcg_gen_xori_i32(tcg_op1, tcg_op1, 0x8000);
12120 read_vec_element_i32(s, tcg_res, rd, pass, MO_16);
12121 gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_res,
12122 fpst);
12123 break;
12124 case 0xa: /* FSUB */
12125 gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
12126 break;
12127 case 0xe: /* FMIN */
12128 gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
12129 break;
12130 case 0xf: /* FRSQRTS */
12131 gen_helper_rsqrtsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
12132 break;
12133 case 0x13: /* FMUL */
12134 gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
12135 break;
12136 case 0x14: /* FCMGE */
12137 gen_helper_advsimd_cge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
12138 break;
12139 case 0x15: /* FACGE */
12140 gen_helper_advsimd_acge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
12141 break;
12142 case 0x17: /* FDIV */
12143 gen_helper_advsimd_divh(tcg_res, tcg_op1, tcg_op2, fpst);
12144 break;
12145 case 0x1a: /* FABD */
12146 gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
12147 tcg_gen_andi_i32(tcg_res, tcg_res, 0x7fff);
12148 break;
12149 case 0x1c: /* FCMGT */
12150 gen_helper_advsimd_cgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
12151 break;
12152 case 0x1d: /* FACGT */
12153 gen_helper_advsimd_acgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
12154 break;
12155 default:
12156 fprintf(stderr, "%s: insn 0x%04x, fpop 0x%2x @ 0x%" PRIx64 "\n",
12157 __func__, insn, fpopcode, s->pc_curr);
12158 g_assert_not_reached();
12161 write_vec_element_i32(s, tcg_res, rd, pass, MO_16);
12162 tcg_temp_free_i32(tcg_res);
12163 tcg_temp_free_i32(tcg_op1);
12164 tcg_temp_free_i32(tcg_op2);
12168 tcg_temp_free_ptr(fpst);
12170 clear_vec_high(s, is_q, rd);
12173 /* AdvSIMD three same extra
12174 * 31 30 29 28 24 23 22 21 20 16 15 14 11 10 9 5 4 0
12175 * +---+---+---+-----------+------+---+------+---+--------+---+----+----+
12176 * | 0 | Q | U | 0 1 1 1 0 | size | 0 | Rm | 1 | opcode | 1 | Rn | Rd |
12177 * +---+---+---+-----------+------+---+------+---+--------+---+----+----+
12179 static void disas_simd_three_reg_same_extra(DisasContext *s, uint32_t insn)
12181 int rd = extract32(insn, 0, 5);
12182 int rn = extract32(insn, 5, 5);
12183 int opcode = extract32(insn, 11, 4);
12184 int rm = extract32(insn, 16, 5);
12185 int size = extract32(insn, 22, 2);
12186 bool u = extract32(insn, 29, 1);
12187 bool is_q = extract32(insn, 30, 1);
12188 bool feature;
12189 int rot;
12191 switch (u * 16 + opcode) {
12192 case 0x10: /* SQRDMLAH (vector) */
12193 case 0x11: /* SQRDMLSH (vector) */
12194 if (size != 1 && size != 2) {
12195 unallocated_encoding(s);
12196 return;
12198 feature = dc_isar_feature(aa64_rdm, s);
12199 break;
12200 case 0x02: /* SDOT (vector) */
12201 case 0x12: /* UDOT (vector) */
12202 if (size != MO_32) {
12203 unallocated_encoding(s);
12204 return;
12206 feature = dc_isar_feature(aa64_dp, s);
12207 break;
12208 case 0x03: /* USDOT */
12209 if (size != MO_32) {
12210 unallocated_encoding(s);
12211 return;
12213 feature = dc_isar_feature(aa64_i8mm, s);
12214 break;
12215 case 0x04: /* SMMLA */
12216 case 0x14: /* UMMLA */
12217 case 0x05: /* USMMLA */
12218 if (!is_q || size != MO_32) {
12219 unallocated_encoding(s);
12220 return;
12222 feature = dc_isar_feature(aa64_i8mm, s);
12223 break;
12224 case 0x18: /* FCMLA, #0 */
12225 case 0x19: /* FCMLA, #90 */
12226 case 0x1a: /* FCMLA, #180 */
12227 case 0x1b: /* FCMLA, #270 */
12228 case 0x1c: /* FCADD, #90 */
12229 case 0x1e: /* FCADD, #270 */
12230 if (size == 0
12231 || (size == 1 && !dc_isar_feature(aa64_fp16, s))
12232 || (size == 3 && !is_q)) {
12233 unallocated_encoding(s);
12234 return;
12236 feature = dc_isar_feature(aa64_fcma, s);
12237 break;
12238 case 0x1d: /* BFMMLA */
12239 if (size != MO_16 || !is_q) {
12240 unallocated_encoding(s);
12241 return;
12243 feature = dc_isar_feature(aa64_bf16, s);
12244 break;
12245 case 0x1f:
12246 switch (size) {
12247 case 1: /* BFDOT */
12248 case 3: /* BFMLAL{B,T} */
12249 feature = dc_isar_feature(aa64_bf16, s);
12250 break;
12251 default:
12252 unallocated_encoding(s);
12253 return;
12255 break;
12256 default:
12257 unallocated_encoding(s);
12258 return;
12260 if (!feature) {
12261 unallocated_encoding(s);
12262 return;
12264 if (!fp_access_check(s)) {
12265 return;
12268 switch (opcode) {
12269 case 0x0: /* SQRDMLAH (vector) */
12270 gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqrdmlah_qc, size);
12271 return;
12273 case 0x1: /* SQRDMLSH (vector) */
12274 gen_gvec_fn3(s, is_q, rd, rn, rm, gen_gvec_sqrdmlsh_qc, size);
12275 return;
12277 case 0x2: /* SDOT / UDOT */
12278 gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0,
12279 u ? gen_helper_gvec_udot_b : gen_helper_gvec_sdot_b);
12280 return;
12282 case 0x3: /* USDOT */
12283 gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0, gen_helper_gvec_usdot_b);
12284 return;
12286 case 0x04: /* SMMLA, UMMLA */
12287 gen_gvec_op4_ool(s, 1, rd, rn, rm, rd, 0,
12288 u ? gen_helper_gvec_ummla_b
12289 : gen_helper_gvec_smmla_b);
12290 return;
12291 case 0x05: /* USMMLA */
12292 gen_gvec_op4_ool(s, 1, rd, rn, rm, rd, 0, gen_helper_gvec_usmmla_b);
12293 return;
12295 case 0x8: /* FCMLA, #0 */
12296 case 0x9: /* FCMLA, #90 */
12297 case 0xa: /* FCMLA, #180 */
12298 case 0xb: /* FCMLA, #270 */
12299 rot = extract32(opcode, 0, 2);
12300 switch (size) {
12301 case 1:
12302 gen_gvec_op4_fpst(s, is_q, rd, rn, rm, rd, true, rot,
12303 gen_helper_gvec_fcmlah);
12304 break;
12305 case 2:
12306 gen_gvec_op4_fpst(s, is_q, rd, rn, rm, rd, false, rot,
12307 gen_helper_gvec_fcmlas);
12308 break;
12309 case 3:
12310 gen_gvec_op4_fpst(s, is_q, rd, rn, rm, rd, false, rot,
12311 gen_helper_gvec_fcmlad);
12312 break;
12313 default:
12314 g_assert_not_reached();
12316 return;
12318 case 0xc: /* FCADD, #90 */
12319 case 0xe: /* FCADD, #270 */
12320 rot = extract32(opcode, 1, 1);
12321 switch (size) {
12322 case 1:
12323 gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
12324 gen_helper_gvec_fcaddh);
12325 break;
12326 case 2:
12327 gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
12328 gen_helper_gvec_fcadds);
12329 break;
12330 case 3:
12331 gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
12332 gen_helper_gvec_fcaddd);
12333 break;
12334 default:
12335 g_assert_not_reached();
12337 return;
12339 case 0xd: /* BFMMLA */
12340 gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0, gen_helper_gvec_bfmmla);
12341 return;
12342 case 0xf:
12343 switch (size) {
12344 case 1: /* BFDOT */
12345 gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, 0, gen_helper_gvec_bfdot);
12346 break;
12347 case 3: /* BFMLAL{B,T} */
12348 gen_gvec_op4_fpst(s, 1, rd, rn, rm, rd, false, is_q,
12349 gen_helper_gvec_bfmlal);
12350 break;
12351 default:
12352 g_assert_not_reached();
12354 return;
12356 default:
12357 g_assert_not_reached();
12361 static void handle_2misc_widening(DisasContext *s, int opcode, bool is_q,
12362 int size, int rn, int rd)
12364 /* Handle 2-reg-misc ops which are widening (so each size element
12365 * in the source becomes a 2*size element in the destination.
12366 * The only instruction like this is FCVTL.
12368 int pass;
12370 if (size == 3) {
12371 /* 32 -> 64 bit fp conversion */
12372 TCGv_i64 tcg_res[2];
12373 int srcelt = is_q ? 2 : 0;
12375 for (pass = 0; pass < 2; pass++) {
12376 TCGv_i32 tcg_op = tcg_temp_new_i32();
12377 tcg_res[pass] = tcg_temp_new_i64();
12379 read_vec_element_i32(s, tcg_op, rn, srcelt + pass, MO_32);
12380 gen_helper_vfp_fcvtds(tcg_res[pass], tcg_op, cpu_env);
12381 tcg_temp_free_i32(tcg_op);
12383 for (pass = 0; pass < 2; pass++) {
12384 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
12385 tcg_temp_free_i64(tcg_res[pass]);
12387 } else {
12388 /* 16 -> 32 bit fp conversion */
12389 int srcelt = is_q ? 4 : 0;
12390 TCGv_i32 tcg_res[4];
12391 TCGv_ptr fpst = fpstatus_ptr(FPST_FPCR);
12392 TCGv_i32 ahp = get_ahp_flag();
12394 for (pass = 0; pass < 4; pass++) {
12395 tcg_res[pass] = tcg_temp_new_i32();
12397 read_vec_element_i32(s, tcg_res[pass], rn, srcelt + pass, MO_16);
12398 gen_helper_vfp_fcvt_f16_to_f32(tcg_res[pass], tcg_res[pass],
12399 fpst, ahp);
12401 for (pass = 0; pass < 4; pass++) {
12402 write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_32);
12403 tcg_temp_free_i32(tcg_res[pass]);
12406 tcg_temp_free_ptr(fpst);
12407 tcg_temp_free_i32(ahp);
12411 static void handle_rev(DisasContext *s, int opcode, bool u,
12412 bool is_q, int size, int rn, int rd)
12414 int op = (opcode << 1) | u;
12415 int opsz = op + size;
12416 int grp_size = 3 - opsz;
12417 int dsize = is_q ? 128 : 64;
12418 int i;
12420 if (opsz >= 3) {
12421 unallocated_encoding(s);
12422 return;
12425 if (!fp_access_check(s)) {
12426 return;
12429 if (size == 0) {
12430 /* Special case bytes, use bswap op on each group of elements */
12431 int groups = dsize / (8 << grp_size);
12433 for (i = 0; i < groups; i++) {
12434 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
12436 read_vec_element(s, tcg_tmp, rn, i, grp_size);
12437 switch (grp_size) {
12438 case MO_16:
12439 tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp);
12440 break;
12441 case MO_32:
12442 tcg_gen_bswap32_i64(tcg_tmp, tcg_tmp);
12443 break;
12444 case MO_64:
12445 tcg_gen_bswap64_i64(tcg_tmp, tcg_tmp);
12446 break;
12447 default:
12448 g_assert_not_reached();
12450 write_vec_element(s, tcg_tmp, rd, i, grp_size);
12451 tcg_temp_free_i64(tcg_tmp);
12453 clear_vec_high(s, is_q, rd);
12454 } else {
12455 int revmask = (1 << grp_size) - 1;
12456 int esize = 8 << size;
12457 int elements = dsize / esize;
12458 TCGv_i64 tcg_rn = tcg_temp_new_i64();
12459 TCGv_i64 tcg_rd = tcg_const_i64(0);
12460 TCGv_i64 tcg_rd_hi = tcg_const_i64(0);
12462 for (i = 0; i < elements; i++) {
12463 int e_rev = (i & 0xf) ^ revmask;
12464 int off = e_rev * esize;
12465 read_vec_element(s, tcg_rn, rn, i, size);
12466 if (off >= 64) {
12467 tcg_gen_deposit_i64(tcg_rd_hi, tcg_rd_hi,
12468 tcg_rn, off - 64, esize);
12469 } else {
12470 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, off, esize);
12473 write_vec_element(s, tcg_rd, rd, 0, MO_64);
12474 write_vec_element(s, tcg_rd_hi, rd, 1, MO_64);
12476 tcg_temp_free_i64(tcg_rd_hi);
12477 tcg_temp_free_i64(tcg_rd);
12478 tcg_temp_free_i64(tcg_rn);
12482 static void handle_2misc_pairwise(DisasContext *s, int opcode, bool u,
12483 bool is_q, int size, int rn, int rd)
12485 /* Implement the pairwise operations from 2-misc:
12486 * SADDLP, UADDLP, SADALP, UADALP.
12487 * These all add pairs of elements in the input to produce a
12488 * double-width result element in the output (possibly accumulating).
12490 bool accum = (opcode == 0x6);
12491 int maxpass = is_q ? 2 : 1;
12492 int pass;
12493 TCGv_i64 tcg_res[2];
12495 if (size == 2) {
12496 /* 32 + 32 -> 64 op */
12497 MemOp memop = size + (u ? 0 : MO_SIGN);
12499 for (pass = 0; pass < maxpass; pass++) {
12500 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
12501 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
12503 tcg_res[pass] = tcg_temp_new_i64();
12505 read_vec_element(s, tcg_op1, rn, pass * 2, memop);
12506 read_vec_element(s, tcg_op2, rn, pass * 2 + 1, memop);
12507 tcg_gen_add_i64(tcg_res[pass], tcg_op1, tcg_op2);
12508 if (accum) {
12509 read_vec_element(s, tcg_op1, rd, pass, MO_64);
12510 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
12513 tcg_temp_free_i64(tcg_op1);
12514 tcg_temp_free_i64(tcg_op2);
12516 } else {
12517 for (pass = 0; pass < maxpass; pass++) {
12518 TCGv_i64 tcg_op = tcg_temp_new_i64();
12519 NeonGenOne64OpFn *genfn;
12520 static NeonGenOne64OpFn * const fns[2][2] = {
12521 { gen_helper_neon_addlp_s8, gen_helper_neon_addlp_u8 },
12522 { gen_helper_neon_addlp_s16, gen_helper_neon_addlp_u16 },
12525 genfn = fns[size][u];
12527 tcg_res[pass] = tcg_temp_new_i64();
12529 read_vec_element(s, tcg_op, rn, pass, MO_64);
12530 genfn(tcg_res[pass], tcg_op);
12532 if (accum) {
12533 read_vec_element(s, tcg_op, rd, pass, MO_64);
12534 if (size == 0) {
12535 gen_helper_neon_addl_u16(tcg_res[pass],
12536 tcg_res[pass], tcg_op);
12537 } else {
12538 gen_helper_neon_addl_u32(tcg_res[pass],
12539 tcg_res[pass], tcg_op);
12542 tcg_temp_free_i64(tcg_op);
12545 if (!is_q) {
12546 tcg_res[1] = tcg_const_i64(0);
12548 for (pass = 0; pass < 2; pass++) {
12549 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
12550 tcg_temp_free_i64(tcg_res[pass]);
12554 static void handle_shll(DisasContext *s, bool is_q, int size, int rn, int rd)
12556 /* Implement SHLL and SHLL2 */
12557 int pass;
12558 int part = is_q ? 2 : 0;
12559 TCGv_i64 tcg_res[2];
12561 for (pass = 0; pass < 2; pass++) {
12562 static NeonGenWidenFn * const widenfns[3] = {
12563 gen_helper_neon_widen_u8,
12564 gen_helper_neon_widen_u16,
12565 tcg_gen_extu_i32_i64,
12567 NeonGenWidenFn *widenfn = widenfns[size];
12568 TCGv_i32 tcg_op = tcg_temp_new_i32();
12570 read_vec_element_i32(s, tcg_op, rn, part + pass, MO_32);
12571 tcg_res[pass] = tcg_temp_new_i64();
12572 widenfn(tcg_res[pass], tcg_op);
12573 tcg_gen_shli_i64(tcg_res[pass], tcg_res[pass], 8 << size);
12575 tcg_temp_free_i32(tcg_op);
12578 for (pass = 0; pass < 2; pass++) {
12579 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
12580 tcg_temp_free_i64(tcg_res[pass]);
12584 /* AdvSIMD two reg misc
12585 * 31 30 29 28 24 23 22 21 17 16 12 11 10 9 5 4 0
12586 * +---+---+---+-----------+------+-----------+--------+-----+------+------+
12587 * | 0 | Q | U | 0 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 | Rn | Rd |
12588 * +---+---+---+-----------+------+-----------+--------+-----+------+------+
12590 static void disas_simd_two_reg_misc(DisasContext *s, uint32_t insn)
12592 int size = extract32(insn, 22, 2);
12593 int opcode = extract32(insn, 12, 5);
12594 bool u = extract32(insn, 29, 1);
12595 bool is_q = extract32(insn, 30, 1);
12596 int rn = extract32(insn, 5, 5);
12597 int rd = extract32(insn, 0, 5);
12598 bool need_fpstatus = false;
12599 bool need_rmode = false;
12600 int rmode = -1;
12601 TCGv_i32 tcg_rmode;
12602 TCGv_ptr tcg_fpstatus;
12604 switch (opcode) {
12605 case 0x0: /* REV64, REV32 */
12606 case 0x1: /* REV16 */
12607 handle_rev(s, opcode, u, is_q, size, rn, rd);
12608 return;
12609 case 0x5: /* CNT, NOT, RBIT */
12610 if (u && size == 0) {
12611 /* NOT */
12612 break;
12613 } else if (u && size == 1) {
12614 /* RBIT */
12615 break;
12616 } else if (!u && size == 0) {
12617 /* CNT */
12618 break;
12620 unallocated_encoding(s);
12621 return;
12622 case 0x12: /* XTN, XTN2, SQXTUN, SQXTUN2 */
12623 case 0x14: /* SQXTN, SQXTN2, UQXTN, UQXTN2 */
12624 if (size == 3) {
12625 unallocated_encoding(s);
12626 return;
12628 if (!fp_access_check(s)) {
12629 return;
12632 handle_2misc_narrow(s, false, opcode, u, is_q, size, rn, rd);
12633 return;
12634 case 0x4: /* CLS, CLZ */
12635 if (size == 3) {
12636 unallocated_encoding(s);
12637 return;
12639 break;
12640 case 0x2: /* SADDLP, UADDLP */
12641 case 0x6: /* SADALP, UADALP */
12642 if (size == 3) {
12643 unallocated_encoding(s);
12644 return;
12646 if (!fp_access_check(s)) {
12647 return;
12649 handle_2misc_pairwise(s, opcode, u, is_q, size, rn, rd);
12650 return;
12651 case 0x13: /* SHLL, SHLL2 */
12652 if (u == 0 || size == 3) {
12653 unallocated_encoding(s);
12654 return;
12656 if (!fp_access_check(s)) {
12657 return;
12659 handle_shll(s, is_q, size, rn, rd);
12660 return;
12661 case 0xa: /* CMLT */
12662 if (u == 1) {
12663 unallocated_encoding(s);
12664 return;
12666 /* fall through */
12667 case 0x8: /* CMGT, CMGE */
12668 case 0x9: /* CMEQ, CMLE */
12669 case 0xb: /* ABS, NEG */
12670 if (size == 3 && !is_q) {
12671 unallocated_encoding(s);
12672 return;
12674 break;
12675 case 0x3: /* SUQADD, USQADD */
12676 if (size == 3 && !is_q) {
12677 unallocated_encoding(s);
12678 return;
12680 if (!fp_access_check(s)) {
12681 return;
12683 handle_2misc_satacc(s, false, u, is_q, size, rn, rd);
12684 return;
12685 case 0x7: /* SQABS, SQNEG */
12686 if (size == 3 && !is_q) {
12687 unallocated_encoding(s);
12688 return;
12690 break;
12691 case 0xc ... 0xf:
12692 case 0x16 ... 0x1f:
12694 /* Floating point: U, size[1] and opcode indicate operation;
12695 * size[0] indicates single or double precision.
12697 int is_double = extract32(size, 0, 1);
12698 opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
12699 size = is_double ? 3 : 2;
12700 switch (opcode) {
12701 case 0x2f: /* FABS */
12702 case 0x6f: /* FNEG */
12703 if (size == 3 && !is_q) {
12704 unallocated_encoding(s);
12705 return;
12707 break;
12708 case 0x1d: /* SCVTF */
12709 case 0x5d: /* UCVTF */
12711 bool is_signed = (opcode == 0x1d) ? true : false;
12712 int elements = is_double ? 2 : is_q ? 4 : 2;
12713 if (is_double && !is_q) {
12714 unallocated_encoding(s);
12715 return;
12717 if (!fp_access_check(s)) {
12718 return;
12720 handle_simd_intfp_conv(s, rd, rn, elements, is_signed, 0, size);
12721 return;
12723 case 0x2c: /* FCMGT (zero) */
12724 case 0x2d: /* FCMEQ (zero) */
12725 case 0x2e: /* FCMLT (zero) */
12726 case 0x6c: /* FCMGE (zero) */
12727 case 0x6d: /* FCMLE (zero) */
12728 if (size == 3 && !is_q) {
12729 unallocated_encoding(s);
12730 return;
12732 handle_2misc_fcmp_zero(s, opcode, false, u, is_q, size, rn, rd);
12733 return;
12734 case 0x7f: /* FSQRT */
12735 if (size == 3 && !is_q) {
12736 unallocated_encoding(s);
12737 return;
12739 break;
12740 case 0x1a: /* FCVTNS */
12741 case 0x1b: /* FCVTMS */
12742 case 0x3a: /* FCVTPS */
12743 case 0x3b: /* FCVTZS */
12744 case 0x5a: /* FCVTNU */
12745 case 0x5b: /* FCVTMU */
12746 case 0x7a: /* FCVTPU */
12747 case 0x7b: /* FCVTZU */
12748 need_fpstatus = true;
12749 need_rmode = true;
12750 rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
12751 if (size == 3 && !is_q) {
12752 unallocated_encoding(s);
12753 return;
12755 break;
12756 case 0x5c: /* FCVTAU */
12757 case 0x1c: /* FCVTAS */
12758 need_fpstatus = true;
12759 need_rmode = true;
12760 rmode = FPROUNDING_TIEAWAY;
12761 if (size == 3 && !is_q) {
12762 unallocated_encoding(s);
12763 return;
12765 break;
12766 case 0x3c: /* URECPE */
12767 if (size == 3) {
12768 unallocated_encoding(s);
12769 return;
12771 /* fall through */
12772 case 0x3d: /* FRECPE */
12773 case 0x7d: /* FRSQRTE */
12774 if (size == 3 && !is_q) {
12775 unallocated_encoding(s);
12776 return;
12778 if (!fp_access_check(s)) {
12779 return;
12781 handle_2misc_reciprocal(s, opcode, false, u, is_q, size, rn, rd);
12782 return;
12783 case 0x56: /* FCVTXN, FCVTXN2 */
12784 if (size == 2) {
12785 unallocated_encoding(s);
12786 return;
12788 /* fall through */
12789 case 0x16: /* FCVTN, FCVTN2 */
12790 /* handle_2misc_narrow does a 2*size -> size operation, but these
12791 * instructions encode the source size rather than dest size.
12793 if (!fp_access_check(s)) {
12794 return;
12796 handle_2misc_narrow(s, false, opcode, 0, is_q, size - 1, rn, rd);
12797 return;
12798 case 0x36: /* BFCVTN, BFCVTN2 */
12799 if (!dc_isar_feature(aa64_bf16, s) || size != 2) {
12800 unallocated_encoding(s);
12801 return;
12803 if (!fp_access_check(s)) {
12804 return;
12806 handle_2misc_narrow(s, false, opcode, 0, is_q, size - 1, rn, rd);
12807 return;
12808 case 0x17: /* FCVTL, FCVTL2 */
12809 if (!fp_access_check(s)) {
12810 return;
12812 handle_2misc_widening(s, opcode, is_q, size, rn, rd);
12813 return;
12814 case 0x18: /* FRINTN */
12815 case 0x19: /* FRINTM */
12816 case 0x38: /* FRINTP */
12817 case 0x39: /* FRINTZ */
12818 need_rmode = true;
12819 rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
12820 /* fall through */
12821 case 0x59: /* FRINTX */
12822 case 0x79: /* FRINTI */
12823 need_fpstatus = true;
12824 if (size == 3 && !is_q) {
12825 unallocated_encoding(s);
12826 return;
12828 break;
12829 case 0x58: /* FRINTA */
12830 need_rmode = true;
12831 rmode = FPROUNDING_TIEAWAY;
12832 need_fpstatus = true;
12833 if (size == 3 && !is_q) {
12834 unallocated_encoding(s);
12835 return;
12837 break;
12838 case 0x7c: /* URSQRTE */
12839 if (size == 3) {
12840 unallocated_encoding(s);
12841 return;
12843 break;
12844 case 0x1e: /* FRINT32Z */
12845 case 0x1f: /* FRINT64Z */
12846 need_rmode = true;
12847 rmode = FPROUNDING_ZERO;
12848 /* fall through */
12849 case 0x5e: /* FRINT32X */
12850 case 0x5f: /* FRINT64X */
12851 need_fpstatus = true;
12852 if ((size == 3 && !is_q) || !dc_isar_feature(aa64_frint, s)) {
12853 unallocated_encoding(s);
12854 return;
12856 break;
12857 default:
12858 unallocated_encoding(s);
12859 return;
12861 break;
12863 default:
12864 unallocated_encoding(s);
12865 return;
12868 if (!fp_access_check(s)) {
12869 return;
12872 if (need_fpstatus || need_rmode) {
12873 tcg_fpstatus = fpstatus_ptr(FPST_FPCR);
12874 } else {
12875 tcg_fpstatus = NULL;
12877 if (need_rmode) {
12878 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rmode));
12879 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
12880 } else {
12881 tcg_rmode = NULL;
12884 switch (opcode) {
12885 case 0x5:
12886 if (u && size == 0) { /* NOT */
12887 gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_not, 0);
12888 return;
12890 break;
12891 case 0x8: /* CMGT, CMGE */
12892 if (u) {
12893 gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_cge0, size);
12894 } else {
12895 gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_cgt0, size);
12897 return;
12898 case 0x9: /* CMEQ, CMLE */
12899 if (u) {
12900 gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_cle0, size);
12901 } else {
12902 gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_ceq0, size);
12904 return;
12905 case 0xa: /* CMLT */
12906 gen_gvec_fn2(s, is_q, rd, rn, gen_gvec_clt0, size);
12907 return;
12908 case 0xb:
12909 if (u) { /* ABS, NEG */
12910 gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_neg, size);
12911 } else {
12912 gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_abs, size);
12914 return;
12917 if (size == 3) {
12918 /* All 64-bit element operations can be shared with scalar 2misc */
12919 int pass;
12921 /* Coverity claims (size == 3 && !is_q) has been eliminated
12922 * from all paths leading to here.
12924 tcg_debug_assert(is_q);
12925 for (pass = 0; pass < 2; pass++) {
12926 TCGv_i64 tcg_op = tcg_temp_new_i64();
12927 TCGv_i64 tcg_res = tcg_temp_new_i64();
12929 read_vec_element(s, tcg_op, rn, pass, MO_64);
12931 handle_2misc_64(s, opcode, u, tcg_res, tcg_op,
12932 tcg_rmode, tcg_fpstatus);
12934 write_vec_element(s, tcg_res, rd, pass, MO_64);
12936 tcg_temp_free_i64(tcg_res);
12937 tcg_temp_free_i64(tcg_op);
12939 } else {
12940 int pass;
12942 for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
12943 TCGv_i32 tcg_op = tcg_temp_new_i32();
12944 TCGv_i32 tcg_res = tcg_temp_new_i32();
12946 read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
12948 if (size == 2) {
12949 /* Special cases for 32 bit elements */
12950 switch (opcode) {
12951 case 0x4: /* CLS */
12952 if (u) {
12953 tcg_gen_clzi_i32(tcg_res, tcg_op, 32);
12954 } else {
12955 tcg_gen_clrsb_i32(tcg_res, tcg_op);
12957 break;
12958 case 0x7: /* SQABS, SQNEG */
12959 if (u) {
12960 gen_helper_neon_qneg_s32(tcg_res, cpu_env, tcg_op);
12961 } else {
12962 gen_helper_neon_qabs_s32(tcg_res, cpu_env, tcg_op);
12964 break;
12965 case 0x2f: /* FABS */
12966 gen_helper_vfp_abss(tcg_res, tcg_op);
12967 break;
12968 case 0x6f: /* FNEG */
12969 gen_helper_vfp_negs(tcg_res, tcg_op);
12970 break;
12971 case 0x7f: /* FSQRT */
12972 gen_helper_vfp_sqrts(tcg_res, tcg_op, cpu_env);
12973 break;
12974 case 0x1a: /* FCVTNS */
12975 case 0x1b: /* FCVTMS */
12976 case 0x1c: /* FCVTAS */
12977 case 0x3a: /* FCVTPS */
12978 case 0x3b: /* FCVTZS */
12980 TCGv_i32 tcg_shift = tcg_const_i32(0);
12981 gen_helper_vfp_tosls(tcg_res, tcg_op,
12982 tcg_shift, tcg_fpstatus);
12983 tcg_temp_free_i32(tcg_shift);
12984 break;
12986 case 0x5a: /* FCVTNU */
12987 case 0x5b: /* FCVTMU */
12988 case 0x5c: /* FCVTAU */
12989 case 0x7a: /* FCVTPU */
12990 case 0x7b: /* FCVTZU */
12992 TCGv_i32 tcg_shift = tcg_const_i32(0);
12993 gen_helper_vfp_touls(tcg_res, tcg_op,
12994 tcg_shift, tcg_fpstatus);
12995 tcg_temp_free_i32(tcg_shift);
12996 break;
12998 case 0x18: /* FRINTN */
12999 case 0x19: /* FRINTM */
13000 case 0x38: /* FRINTP */
13001 case 0x39: /* FRINTZ */
13002 case 0x58: /* FRINTA */
13003 case 0x79: /* FRINTI */
13004 gen_helper_rints(tcg_res, tcg_op, tcg_fpstatus);
13005 break;
13006 case 0x59: /* FRINTX */
13007 gen_helper_rints_exact(tcg_res, tcg_op, tcg_fpstatus);
13008 break;
13009 case 0x7c: /* URSQRTE */
13010 gen_helper_rsqrte_u32(tcg_res, tcg_op);
13011 break;
13012 case 0x1e: /* FRINT32Z */
13013 case 0x5e: /* FRINT32X */
13014 gen_helper_frint32_s(tcg_res, tcg_op, tcg_fpstatus);
13015 break;
13016 case 0x1f: /* FRINT64Z */
13017 case 0x5f: /* FRINT64X */
13018 gen_helper_frint64_s(tcg_res, tcg_op, tcg_fpstatus);
13019 break;
13020 default:
13021 g_assert_not_reached();
13023 } else {
13024 /* Use helpers for 8 and 16 bit elements */
13025 switch (opcode) {
13026 case 0x5: /* CNT, RBIT */
13027 /* For these two insns size is part of the opcode specifier
13028 * (handled earlier); they always operate on byte elements.
13030 if (u) {
13031 gen_helper_neon_rbit_u8(tcg_res, tcg_op);
13032 } else {
13033 gen_helper_neon_cnt_u8(tcg_res, tcg_op);
13035 break;
13036 case 0x7: /* SQABS, SQNEG */
13038 NeonGenOneOpEnvFn *genfn;
13039 static NeonGenOneOpEnvFn * const fns[2][2] = {
13040 { gen_helper_neon_qabs_s8, gen_helper_neon_qneg_s8 },
13041 { gen_helper_neon_qabs_s16, gen_helper_neon_qneg_s16 },
13043 genfn = fns[size][u];
13044 genfn(tcg_res, cpu_env, tcg_op);
13045 break;
13047 case 0x4: /* CLS, CLZ */
13048 if (u) {
13049 if (size == 0) {
13050 gen_helper_neon_clz_u8(tcg_res, tcg_op);
13051 } else {
13052 gen_helper_neon_clz_u16(tcg_res, tcg_op);
13054 } else {
13055 if (size == 0) {
13056 gen_helper_neon_cls_s8(tcg_res, tcg_op);
13057 } else {
13058 gen_helper_neon_cls_s16(tcg_res, tcg_op);
13061 break;
13062 default:
13063 g_assert_not_reached();
13067 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
13069 tcg_temp_free_i32(tcg_res);
13070 tcg_temp_free_i32(tcg_op);
13073 clear_vec_high(s, is_q, rd);
13075 if (need_rmode) {
13076 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
13077 tcg_temp_free_i32(tcg_rmode);
13079 if (need_fpstatus) {
13080 tcg_temp_free_ptr(tcg_fpstatus);
13084 /* AdvSIMD [scalar] two register miscellaneous (FP16)
13086 * 31 30 29 28 27 24 23 22 21 17 16 12 11 10 9 5 4 0
13087 * +---+---+---+---+---------+---+-------------+--------+-----+------+------+
13088 * | 0 | Q | U | S | 1 1 1 0 | a | 1 1 1 1 0 0 | opcode | 1 0 | Rn | Rd |
13089 * +---+---+---+---+---------+---+-------------+--------+-----+------+------+
13090 * mask: 1000 1111 0111 1110 0000 1100 0000 0000 0x8f7e 0c00
13091 * val: 0000 1110 0111 1000 0000 1000 0000 0000 0x0e78 0800
13093 * This actually covers two groups where scalar access is governed by
13094 * bit 28. A bunch of the instructions (float to integral) only exist
13095 * in the vector form and are un-allocated for the scalar decode. Also
13096 * in the scalar decode Q is always 1.
13098 static void disas_simd_two_reg_misc_fp16(DisasContext *s, uint32_t insn)
13100 int fpop, opcode, a, u;
13101 int rn, rd;
13102 bool is_q;
13103 bool is_scalar;
13104 bool only_in_vector = false;
13106 int pass;
13107 TCGv_i32 tcg_rmode = NULL;
13108 TCGv_ptr tcg_fpstatus = NULL;
13109 bool need_rmode = false;
13110 bool need_fpst = true;
13111 int rmode;
13113 if (!dc_isar_feature(aa64_fp16, s)) {
13114 unallocated_encoding(s);
13115 return;
13118 rd = extract32(insn, 0, 5);
13119 rn = extract32(insn, 5, 5);
13121 a = extract32(insn, 23, 1);
13122 u = extract32(insn, 29, 1);
13123 is_scalar = extract32(insn, 28, 1);
13124 is_q = extract32(insn, 30, 1);
13126 opcode = extract32(insn, 12, 5);
13127 fpop = deposit32(opcode, 5, 1, a);
13128 fpop = deposit32(fpop, 6, 1, u);
13130 switch (fpop) {
13131 case 0x1d: /* SCVTF */
13132 case 0x5d: /* UCVTF */
13134 int elements;
13136 if (is_scalar) {
13137 elements = 1;
13138 } else {
13139 elements = (is_q ? 8 : 4);
13142 if (!fp_access_check(s)) {
13143 return;
13145 handle_simd_intfp_conv(s, rd, rn, elements, !u, 0, MO_16);
13146 return;
13148 break;
13149 case 0x2c: /* FCMGT (zero) */
13150 case 0x2d: /* FCMEQ (zero) */
13151 case 0x2e: /* FCMLT (zero) */
13152 case 0x6c: /* FCMGE (zero) */
13153 case 0x6d: /* FCMLE (zero) */
13154 handle_2misc_fcmp_zero(s, fpop, is_scalar, 0, is_q, MO_16, rn, rd);
13155 return;
13156 case 0x3d: /* FRECPE */
13157 case 0x3f: /* FRECPX */
13158 break;
13159 case 0x18: /* FRINTN */
13160 need_rmode = true;
13161 only_in_vector = true;
13162 rmode = FPROUNDING_TIEEVEN;
13163 break;
13164 case 0x19: /* FRINTM */
13165 need_rmode = true;
13166 only_in_vector = true;
13167 rmode = FPROUNDING_NEGINF;
13168 break;
13169 case 0x38: /* FRINTP */
13170 need_rmode = true;
13171 only_in_vector = true;
13172 rmode = FPROUNDING_POSINF;
13173 break;
13174 case 0x39: /* FRINTZ */
13175 need_rmode = true;
13176 only_in_vector = true;
13177 rmode = FPROUNDING_ZERO;
13178 break;
13179 case 0x58: /* FRINTA */
13180 need_rmode = true;
13181 only_in_vector = true;
13182 rmode = FPROUNDING_TIEAWAY;
13183 break;
13184 case 0x59: /* FRINTX */
13185 case 0x79: /* FRINTI */
13186 only_in_vector = true;
13187 /* current rounding mode */
13188 break;
13189 case 0x1a: /* FCVTNS */
13190 need_rmode = true;
13191 rmode = FPROUNDING_TIEEVEN;
13192 break;
13193 case 0x1b: /* FCVTMS */
13194 need_rmode = true;
13195 rmode = FPROUNDING_NEGINF;
13196 break;
13197 case 0x1c: /* FCVTAS */
13198 need_rmode = true;
13199 rmode = FPROUNDING_TIEAWAY;
13200 break;
13201 case 0x3a: /* FCVTPS */
13202 need_rmode = true;
13203 rmode = FPROUNDING_POSINF;
13204 break;
13205 case 0x3b: /* FCVTZS */
13206 need_rmode = true;
13207 rmode = FPROUNDING_ZERO;
13208 break;
13209 case 0x5a: /* FCVTNU */
13210 need_rmode = true;
13211 rmode = FPROUNDING_TIEEVEN;
13212 break;
13213 case 0x5b: /* FCVTMU */
13214 need_rmode = true;
13215 rmode = FPROUNDING_NEGINF;
13216 break;
13217 case 0x5c: /* FCVTAU */
13218 need_rmode = true;
13219 rmode = FPROUNDING_TIEAWAY;
13220 break;
13221 case 0x7a: /* FCVTPU */
13222 need_rmode = true;
13223 rmode = FPROUNDING_POSINF;
13224 break;
13225 case 0x7b: /* FCVTZU */
13226 need_rmode = true;
13227 rmode = FPROUNDING_ZERO;
13228 break;
13229 case 0x2f: /* FABS */
13230 case 0x6f: /* FNEG */
13231 need_fpst = false;
13232 break;
13233 case 0x7d: /* FRSQRTE */
13234 case 0x7f: /* FSQRT (vector) */
13235 break;
13236 default:
13237 fprintf(stderr, "%s: insn 0x%04x fpop 0x%2x\n", __func__, insn, fpop);
13238 g_assert_not_reached();
13242 /* Check additional constraints for the scalar encoding */
13243 if (is_scalar) {
13244 if (!is_q) {
13245 unallocated_encoding(s);
13246 return;
13248 /* FRINTxx is only in the vector form */
13249 if (only_in_vector) {
13250 unallocated_encoding(s);
13251 return;
13255 if (!fp_access_check(s)) {
13256 return;
13259 if (need_rmode || need_fpst) {
13260 tcg_fpstatus = fpstatus_ptr(FPST_FPCR_F16);
13263 if (need_rmode) {
13264 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rmode));
13265 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
13268 if (is_scalar) {
13269 TCGv_i32 tcg_op = read_fp_hreg(s, rn);
13270 TCGv_i32 tcg_res = tcg_temp_new_i32();
13272 switch (fpop) {
13273 case 0x1a: /* FCVTNS */
13274 case 0x1b: /* FCVTMS */
13275 case 0x1c: /* FCVTAS */
13276 case 0x3a: /* FCVTPS */
13277 case 0x3b: /* FCVTZS */
13278 gen_helper_advsimd_f16tosinth(tcg_res, tcg_op, tcg_fpstatus);
13279 break;
13280 case 0x3d: /* FRECPE */
13281 gen_helper_recpe_f16(tcg_res, tcg_op, tcg_fpstatus);
13282 break;
13283 case 0x3f: /* FRECPX */
13284 gen_helper_frecpx_f16(tcg_res, tcg_op, tcg_fpstatus);
13285 break;
13286 case 0x5a: /* FCVTNU */
13287 case 0x5b: /* FCVTMU */
13288 case 0x5c: /* FCVTAU */
13289 case 0x7a: /* FCVTPU */
13290 case 0x7b: /* FCVTZU */
13291 gen_helper_advsimd_f16touinth(tcg_res, tcg_op, tcg_fpstatus);
13292 break;
13293 case 0x6f: /* FNEG */
13294 tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
13295 break;
13296 case 0x7d: /* FRSQRTE */
13297 gen_helper_rsqrte_f16(tcg_res, tcg_op, tcg_fpstatus);
13298 break;
13299 default:
13300 g_assert_not_reached();
13303 /* limit any sign extension going on */
13304 tcg_gen_andi_i32(tcg_res, tcg_res, 0xffff);
13305 write_fp_sreg(s, rd, tcg_res);
13307 tcg_temp_free_i32(tcg_res);
13308 tcg_temp_free_i32(tcg_op);
13309 } else {
13310 for (pass = 0; pass < (is_q ? 8 : 4); pass++) {
13311 TCGv_i32 tcg_op = tcg_temp_new_i32();
13312 TCGv_i32 tcg_res = tcg_temp_new_i32();
13314 read_vec_element_i32(s, tcg_op, rn, pass, MO_16);
13316 switch (fpop) {
13317 case 0x1a: /* FCVTNS */
13318 case 0x1b: /* FCVTMS */
13319 case 0x1c: /* FCVTAS */
13320 case 0x3a: /* FCVTPS */
13321 case 0x3b: /* FCVTZS */
13322 gen_helper_advsimd_f16tosinth(tcg_res, tcg_op, tcg_fpstatus);
13323 break;
13324 case 0x3d: /* FRECPE */
13325 gen_helper_recpe_f16(tcg_res, tcg_op, tcg_fpstatus);
13326 break;
13327 case 0x5a: /* FCVTNU */
13328 case 0x5b: /* FCVTMU */
13329 case 0x5c: /* FCVTAU */
13330 case 0x7a: /* FCVTPU */
13331 case 0x7b: /* FCVTZU */
13332 gen_helper_advsimd_f16touinth(tcg_res, tcg_op, tcg_fpstatus);
13333 break;
13334 case 0x18: /* FRINTN */
13335 case 0x19: /* FRINTM */
13336 case 0x38: /* FRINTP */
13337 case 0x39: /* FRINTZ */
13338 case 0x58: /* FRINTA */
13339 case 0x79: /* FRINTI */
13340 gen_helper_advsimd_rinth(tcg_res, tcg_op, tcg_fpstatus);
13341 break;
13342 case 0x59: /* FRINTX */
13343 gen_helper_advsimd_rinth_exact(tcg_res, tcg_op, tcg_fpstatus);
13344 break;
13345 case 0x2f: /* FABS */
13346 tcg_gen_andi_i32(tcg_res, tcg_op, 0x7fff);
13347 break;
13348 case 0x6f: /* FNEG */
13349 tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
13350 break;
13351 case 0x7d: /* FRSQRTE */
13352 gen_helper_rsqrte_f16(tcg_res, tcg_op, tcg_fpstatus);
13353 break;
13354 case 0x7f: /* FSQRT */
13355 gen_helper_sqrt_f16(tcg_res, tcg_op, tcg_fpstatus);
13356 break;
13357 default:
13358 g_assert_not_reached();
13361 write_vec_element_i32(s, tcg_res, rd, pass, MO_16);
13363 tcg_temp_free_i32(tcg_res);
13364 tcg_temp_free_i32(tcg_op);
13367 clear_vec_high(s, is_q, rd);
13370 if (tcg_rmode) {
13371 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
13372 tcg_temp_free_i32(tcg_rmode);
13375 if (tcg_fpstatus) {
13376 tcg_temp_free_ptr(tcg_fpstatus);
13380 /* AdvSIMD scalar x indexed element
13381 * 31 30 29 28 24 23 22 21 20 19 16 15 12 11 10 9 5 4 0
13382 * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
13383 * | 0 1 | U | 1 1 1 1 1 | size | L | M | Rm | opc | H | 0 | Rn | Rd |
13384 * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
13385 * AdvSIMD vector x indexed element
13386 * 31 30 29 28 24 23 22 21 20 19 16 15 12 11 10 9 5 4 0
13387 * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
13388 * | 0 | Q | U | 0 1 1 1 1 | size | L | M | Rm | opc | H | 0 | Rn | Rd |
13389 * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
13391 static void disas_simd_indexed(DisasContext *s, uint32_t insn)
13393 /* This encoding has two kinds of instruction:
13394 * normal, where we perform elt x idxelt => elt for each
13395 * element in the vector
13396 * long, where we perform elt x idxelt and generate a result of
13397 * double the width of the input element
13398 * The long ops have a 'part' specifier (ie come in INSN, INSN2 pairs).
13400 bool is_scalar = extract32(insn, 28, 1);
13401 bool is_q = extract32(insn, 30, 1);
13402 bool u = extract32(insn, 29, 1);
13403 int size = extract32(insn, 22, 2);
13404 int l = extract32(insn, 21, 1);
13405 int m = extract32(insn, 20, 1);
13406 /* Note that the Rm field here is only 4 bits, not 5 as it usually is */
13407 int rm = extract32(insn, 16, 4);
13408 int opcode = extract32(insn, 12, 4);
13409 int h = extract32(insn, 11, 1);
13410 int rn = extract32(insn, 5, 5);
13411 int rd = extract32(insn, 0, 5);
13412 bool is_long = false;
13413 int is_fp = 0;
13414 bool is_fp16 = false;
13415 int index;
13416 TCGv_ptr fpst;
13418 switch (16 * u + opcode) {
13419 case 0x08: /* MUL */
13420 case 0x10: /* MLA */
13421 case 0x14: /* MLS */
13422 if (is_scalar) {
13423 unallocated_encoding(s);
13424 return;
13426 break;
13427 case 0x02: /* SMLAL, SMLAL2 */
13428 case 0x12: /* UMLAL, UMLAL2 */
13429 case 0x06: /* SMLSL, SMLSL2 */
13430 case 0x16: /* UMLSL, UMLSL2 */
13431 case 0x0a: /* SMULL, SMULL2 */
13432 case 0x1a: /* UMULL, UMULL2 */
13433 if (is_scalar) {
13434 unallocated_encoding(s);
13435 return;
13437 is_long = true;
13438 break;
13439 case 0x03: /* SQDMLAL, SQDMLAL2 */
13440 case 0x07: /* SQDMLSL, SQDMLSL2 */
13441 case 0x0b: /* SQDMULL, SQDMULL2 */
13442 is_long = true;
13443 break;
13444 case 0x0c: /* SQDMULH */
13445 case 0x0d: /* SQRDMULH */
13446 break;
13447 case 0x01: /* FMLA */
13448 case 0x05: /* FMLS */
13449 case 0x09: /* FMUL */
13450 case 0x19: /* FMULX */
13451 is_fp = 1;
13452 break;
13453 case 0x1d: /* SQRDMLAH */
13454 case 0x1f: /* SQRDMLSH */
13455 if (!dc_isar_feature(aa64_rdm, s)) {
13456 unallocated_encoding(s);
13457 return;
13459 break;
13460 case 0x0e: /* SDOT */
13461 case 0x1e: /* UDOT */
13462 if (is_scalar || size != MO_32 || !dc_isar_feature(aa64_dp, s)) {
13463 unallocated_encoding(s);
13464 return;
13466 break;
13467 case 0x0f:
13468 switch (size) {
13469 case 0: /* SUDOT */
13470 case 2: /* USDOT */
13471 if (is_scalar || !dc_isar_feature(aa64_i8mm, s)) {
13472 unallocated_encoding(s);
13473 return;
13475 size = MO_32;
13476 break;
13477 case 1: /* BFDOT */
13478 if (is_scalar || !dc_isar_feature(aa64_bf16, s)) {
13479 unallocated_encoding(s);
13480 return;
13482 size = MO_32;
13483 break;
13484 case 3: /* BFMLAL{B,T} */
13485 if (is_scalar || !dc_isar_feature(aa64_bf16, s)) {
13486 unallocated_encoding(s);
13487 return;
13489 /* can't set is_fp without other incorrect size checks */
13490 size = MO_16;
13491 break;
13492 default:
13493 unallocated_encoding(s);
13494 return;
13496 break;
13497 case 0x11: /* FCMLA #0 */
13498 case 0x13: /* FCMLA #90 */
13499 case 0x15: /* FCMLA #180 */
13500 case 0x17: /* FCMLA #270 */
13501 if (is_scalar || !dc_isar_feature(aa64_fcma, s)) {
13502 unallocated_encoding(s);
13503 return;
13505 is_fp = 2;
13506 break;
13507 case 0x00: /* FMLAL */
13508 case 0x04: /* FMLSL */
13509 case 0x18: /* FMLAL2 */
13510 case 0x1c: /* FMLSL2 */
13511 if (is_scalar || size != MO_32 || !dc_isar_feature(aa64_fhm, s)) {
13512 unallocated_encoding(s);
13513 return;
13515 size = MO_16;
13516 /* is_fp, but we pass cpu_env not fp_status. */
13517 break;
13518 default:
13519 unallocated_encoding(s);
13520 return;
13523 switch (is_fp) {
13524 case 1: /* normal fp */
13525 /* convert insn encoded size to MemOp size */
13526 switch (size) {
13527 case 0: /* half-precision */
13528 size = MO_16;
13529 is_fp16 = true;
13530 break;
13531 case MO_32: /* single precision */
13532 case MO_64: /* double precision */
13533 break;
13534 default:
13535 unallocated_encoding(s);
13536 return;
13538 break;
13540 case 2: /* complex fp */
13541 /* Each indexable element is a complex pair. */
13542 size += 1;
13543 switch (size) {
13544 case MO_32:
13545 if (h && !is_q) {
13546 unallocated_encoding(s);
13547 return;
13549 is_fp16 = true;
13550 break;
13551 case MO_64:
13552 break;
13553 default:
13554 unallocated_encoding(s);
13555 return;
13557 break;
13559 default: /* integer */
13560 switch (size) {
13561 case MO_8:
13562 case MO_64:
13563 unallocated_encoding(s);
13564 return;
13566 break;
13568 if (is_fp16 && !dc_isar_feature(aa64_fp16, s)) {
13569 unallocated_encoding(s);
13570 return;
13573 /* Given MemOp size, adjust register and indexing. */
13574 switch (size) {
13575 case MO_16:
13576 index = h << 2 | l << 1 | m;
13577 break;
13578 case MO_32:
13579 index = h << 1 | l;
13580 rm |= m << 4;
13581 break;
13582 case MO_64:
13583 if (l || !is_q) {
13584 unallocated_encoding(s);
13585 return;
13587 index = h;
13588 rm |= m << 4;
13589 break;
13590 default:
13591 g_assert_not_reached();
13594 if (!fp_access_check(s)) {
13595 return;
13598 if (is_fp) {
13599 fpst = fpstatus_ptr(is_fp16 ? FPST_FPCR_F16 : FPST_FPCR);
13600 } else {
13601 fpst = NULL;
13604 switch (16 * u + opcode) {
13605 case 0x0e: /* SDOT */
13606 case 0x1e: /* UDOT */
13607 gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
13608 u ? gen_helper_gvec_udot_idx_b
13609 : gen_helper_gvec_sdot_idx_b);
13610 return;
13611 case 0x0f:
13612 switch (extract32(insn, 22, 2)) {
13613 case 0: /* SUDOT */
13614 gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
13615 gen_helper_gvec_sudot_idx_b);
13616 return;
13617 case 1: /* BFDOT */
13618 gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
13619 gen_helper_gvec_bfdot_idx);
13620 return;
13621 case 2: /* USDOT */
13622 gen_gvec_op4_ool(s, is_q, rd, rn, rm, rd, index,
13623 gen_helper_gvec_usdot_idx_b);
13624 return;
13625 case 3: /* BFMLAL{B,T} */
13626 gen_gvec_op4_fpst(s, 1, rd, rn, rm, rd, 0, (index << 1) | is_q,
13627 gen_helper_gvec_bfmlal_idx);
13628 return;
13630 g_assert_not_reached();
13631 case 0x11: /* FCMLA #0 */
13632 case 0x13: /* FCMLA #90 */
13633 case 0x15: /* FCMLA #180 */
13634 case 0x17: /* FCMLA #270 */
13636 int rot = extract32(insn, 13, 2);
13637 int data = (index << 2) | rot;
13638 tcg_gen_gvec_4_ptr(vec_full_reg_offset(s, rd),
13639 vec_full_reg_offset(s, rn),
13640 vec_full_reg_offset(s, rm),
13641 vec_full_reg_offset(s, rd), fpst,
13642 is_q ? 16 : 8, vec_full_reg_size(s), data,
13643 size == MO_64
13644 ? gen_helper_gvec_fcmlas_idx
13645 : gen_helper_gvec_fcmlah_idx);
13646 tcg_temp_free_ptr(fpst);
13648 return;
13650 case 0x00: /* FMLAL */
13651 case 0x04: /* FMLSL */
13652 case 0x18: /* FMLAL2 */
13653 case 0x1c: /* FMLSL2 */
13655 int is_s = extract32(opcode, 2, 1);
13656 int is_2 = u;
13657 int data = (index << 2) | (is_2 << 1) | is_s;
13658 tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
13659 vec_full_reg_offset(s, rn),
13660 vec_full_reg_offset(s, rm), cpu_env,
13661 is_q ? 16 : 8, vec_full_reg_size(s),
13662 data, gen_helper_gvec_fmlal_idx_a64);
13664 return;
13666 case 0x08: /* MUL */
13667 if (!is_long && !is_scalar) {
13668 static gen_helper_gvec_3 * const fns[3] = {
13669 gen_helper_gvec_mul_idx_h,
13670 gen_helper_gvec_mul_idx_s,
13671 gen_helper_gvec_mul_idx_d,
13673 tcg_gen_gvec_3_ool(vec_full_reg_offset(s, rd),
13674 vec_full_reg_offset(s, rn),
13675 vec_full_reg_offset(s, rm),
13676 is_q ? 16 : 8, vec_full_reg_size(s),
13677 index, fns[size - 1]);
13678 return;
13680 break;
13682 case 0x10: /* MLA */
13683 if (!is_long && !is_scalar) {
13684 static gen_helper_gvec_4 * const fns[3] = {
13685 gen_helper_gvec_mla_idx_h,
13686 gen_helper_gvec_mla_idx_s,
13687 gen_helper_gvec_mla_idx_d,
13689 tcg_gen_gvec_4_ool(vec_full_reg_offset(s, rd),
13690 vec_full_reg_offset(s, rn),
13691 vec_full_reg_offset(s, rm),
13692 vec_full_reg_offset(s, rd),
13693 is_q ? 16 : 8, vec_full_reg_size(s),
13694 index, fns[size - 1]);
13695 return;
13697 break;
13699 case 0x14: /* MLS */
13700 if (!is_long && !is_scalar) {
13701 static gen_helper_gvec_4 * const fns[3] = {
13702 gen_helper_gvec_mls_idx_h,
13703 gen_helper_gvec_mls_idx_s,
13704 gen_helper_gvec_mls_idx_d,
13706 tcg_gen_gvec_4_ool(vec_full_reg_offset(s, rd),
13707 vec_full_reg_offset(s, rn),
13708 vec_full_reg_offset(s, rm),
13709 vec_full_reg_offset(s, rd),
13710 is_q ? 16 : 8, vec_full_reg_size(s),
13711 index, fns[size - 1]);
13712 return;
13714 break;
13717 if (size == 3) {
13718 TCGv_i64 tcg_idx = tcg_temp_new_i64();
13719 int pass;
13721 assert(is_fp && is_q && !is_long);
13723 read_vec_element(s, tcg_idx, rm, index, MO_64);
13725 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
13726 TCGv_i64 tcg_op = tcg_temp_new_i64();
13727 TCGv_i64 tcg_res = tcg_temp_new_i64();
13729 read_vec_element(s, tcg_op, rn, pass, MO_64);
13731 switch (16 * u + opcode) {
13732 case 0x05: /* FMLS */
13733 /* As usual for ARM, separate negation for fused multiply-add */
13734 gen_helper_vfp_negd(tcg_op, tcg_op);
13735 /* fall through */
13736 case 0x01: /* FMLA */
13737 read_vec_element(s, tcg_res, rd, pass, MO_64);
13738 gen_helper_vfp_muladdd(tcg_res, tcg_op, tcg_idx, tcg_res, fpst);
13739 break;
13740 case 0x09: /* FMUL */
13741 gen_helper_vfp_muld(tcg_res, tcg_op, tcg_idx, fpst);
13742 break;
13743 case 0x19: /* FMULX */
13744 gen_helper_vfp_mulxd(tcg_res, tcg_op, tcg_idx, fpst);
13745 break;
13746 default:
13747 g_assert_not_reached();
13750 write_vec_element(s, tcg_res, rd, pass, MO_64);
13751 tcg_temp_free_i64(tcg_op);
13752 tcg_temp_free_i64(tcg_res);
13755 tcg_temp_free_i64(tcg_idx);
13756 clear_vec_high(s, !is_scalar, rd);
13757 } else if (!is_long) {
13758 /* 32 bit floating point, or 16 or 32 bit integer.
13759 * For the 16 bit scalar case we use the usual Neon helpers and
13760 * rely on the fact that 0 op 0 == 0 with no side effects.
13762 TCGv_i32 tcg_idx = tcg_temp_new_i32();
13763 int pass, maxpasses;
13765 if (is_scalar) {
13766 maxpasses = 1;
13767 } else {
13768 maxpasses = is_q ? 4 : 2;
13771 read_vec_element_i32(s, tcg_idx, rm, index, size);
13773 if (size == 1 && !is_scalar) {
13774 /* The simplest way to handle the 16x16 indexed ops is to duplicate
13775 * the index into both halves of the 32 bit tcg_idx and then use
13776 * the usual Neon helpers.
13778 tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
13781 for (pass = 0; pass < maxpasses; pass++) {
13782 TCGv_i32 tcg_op = tcg_temp_new_i32();
13783 TCGv_i32 tcg_res = tcg_temp_new_i32();
13785 read_vec_element_i32(s, tcg_op, rn, pass, is_scalar ? size : MO_32);
13787 switch (16 * u + opcode) {
13788 case 0x08: /* MUL */
13789 case 0x10: /* MLA */
13790 case 0x14: /* MLS */
13792 static NeonGenTwoOpFn * const fns[2][2] = {
13793 { gen_helper_neon_add_u16, gen_helper_neon_sub_u16 },
13794 { tcg_gen_add_i32, tcg_gen_sub_i32 },
13796 NeonGenTwoOpFn *genfn;
13797 bool is_sub = opcode == 0x4;
13799 if (size == 1) {
13800 gen_helper_neon_mul_u16(tcg_res, tcg_op, tcg_idx);
13801 } else {
13802 tcg_gen_mul_i32(tcg_res, tcg_op, tcg_idx);
13804 if (opcode == 0x8) {
13805 break;
13807 read_vec_element_i32(s, tcg_op, rd, pass, MO_32);
13808 genfn = fns[size - 1][is_sub];
13809 genfn(tcg_res, tcg_op, tcg_res);
13810 break;
13812 case 0x05: /* FMLS */
13813 case 0x01: /* FMLA */
13814 read_vec_element_i32(s, tcg_res, rd, pass,
13815 is_scalar ? size : MO_32);
13816 switch (size) {
13817 case 1:
13818 if (opcode == 0x5) {
13819 /* As usual for ARM, separate negation for fused
13820 * multiply-add */
13821 tcg_gen_xori_i32(tcg_op, tcg_op, 0x80008000);
13823 if (is_scalar) {
13824 gen_helper_advsimd_muladdh(tcg_res, tcg_op, tcg_idx,
13825 tcg_res, fpst);
13826 } else {
13827 gen_helper_advsimd_muladd2h(tcg_res, tcg_op, tcg_idx,
13828 tcg_res, fpst);
13830 break;
13831 case 2:
13832 if (opcode == 0x5) {
13833 /* As usual for ARM, separate negation for
13834 * fused multiply-add */
13835 tcg_gen_xori_i32(tcg_op, tcg_op, 0x80000000);
13837 gen_helper_vfp_muladds(tcg_res, tcg_op, tcg_idx,
13838 tcg_res, fpst);
13839 break;
13840 default:
13841 g_assert_not_reached();
13843 break;
13844 case 0x09: /* FMUL */
13845 switch (size) {
13846 case 1:
13847 if (is_scalar) {
13848 gen_helper_advsimd_mulh(tcg_res, tcg_op,
13849 tcg_idx, fpst);
13850 } else {
13851 gen_helper_advsimd_mul2h(tcg_res, tcg_op,
13852 tcg_idx, fpst);
13854 break;
13855 case 2:
13856 gen_helper_vfp_muls(tcg_res, tcg_op, tcg_idx, fpst);
13857 break;
13858 default:
13859 g_assert_not_reached();
13861 break;
13862 case 0x19: /* FMULX */
13863 switch (size) {
13864 case 1:
13865 if (is_scalar) {
13866 gen_helper_advsimd_mulxh(tcg_res, tcg_op,
13867 tcg_idx, fpst);
13868 } else {
13869 gen_helper_advsimd_mulx2h(tcg_res, tcg_op,
13870 tcg_idx, fpst);
13872 break;
13873 case 2:
13874 gen_helper_vfp_mulxs(tcg_res, tcg_op, tcg_idx, fpst);
13875 break;
13876 default:
13877 g_assert_not_reached();
13879 break;
13880 case 0x0c: /* SQDMULH */
13881 if (size == 1) {
13882 gen_helper_neon_qdmulh_s16(tcg_res, cpu_env,
13883 tcg_op, tcg_idx);
13884 } else {
13885 gen_helper_neon_qdmulh_s32(tcg_res, cpu_env,
13886 tcg_op, tcg_idx);
13888 break;
13889 case 0x0d: /* SQRDMULH */
13890 if (size == 1) {
13891 gen_helper_neon_qrdmulh_s16(tcg_res, cpu_env,
13892 tcg_op, tcg_idx);
13893 } else {
13894 gen_helper_neon_qrdmulh_s32(tcg_res, cpu_env,
13895 tcg_op, tcg_idx);
13897 break;
13898 case 0x1d: /* SQRDMLAH */
13899 read_vec_element_i32(s, tcg_res, rd, pass,
13900 is_scalar ? size : MO_32);
13901 if (size == 1) {
13902 gen_helper_neon_qrdmlah_s16(tcg_res, cpu_env,
13903 tcg_op, tcg_idx, tcg_res);
13904 } else {
13905 gen_helper_neon_qrdmlah_s32(tcg_res, cpu_env,
13906 tcg_op, tcg_idx, tcg_res);
13908 break;
13909 case 0x1f: /* SQRDMLSH */
13910 read_vec_element_i32(s, tcg_res, rd, pass,
13911 is_scalar ? size : MO_32);
13912 if (size == 1) {
13913 gen_helper_neon_qrdmlsh_s16(tcg_res, cpu_env,
13914 tcg_op, tcg_idx, tcg_res);
13915 } else {
13916 gen_helper_neon_qrdmlsh_s32(tcg_res, cpu_env,
13917 tcg_op, tcg_idx, tcg_res);
13919 break;
13920 default:
13921 g_assert_not_reached();
13924 if (is_scalar) {
13925 write_fp_sreg(s, rd, tcg_res);
13926 } else {
13927 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
13930 tcg_temp_free_i32(tcg_op);
13931 tcg_temp_free_i32(tcg_res);
13934 tcg_temp_free_i32(tcg_idx);
13935 clear_vec_high(s, is_q, rd);
13936 } else {
13937 /* long ops: 16x16->32 or 32x32->64 */
13938 TCGv_i64 tcg_res[2];
13939 int pass;
13940 bool satop = extract32(opcode, 0, 1);
13941 MemOp memop = MO_32;
13943 if (satop || !u) {
13944 memop |= MO_SIGN;
13947 if (size == 2) {
13948 TCGv_i64 tcg_idx = tcg_temp_new_i64();
13950 read_vec_element(s, tcg_idx, rm, index, memop);
13952 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
13953 TCGv_i64 tcg_op = tcg_temp_new_i64();
13954 TCGv_i64 tcg_passres;
13955 int passelt;
13957 if (is_scalar) {
13958 passelt = 0;
13959 } else {
13960 passelt = pass + (is_q * 2);
13963 read_vec_element(s, tcg_op, rn, passelt, memop);
13965 tcg_res[pass] = tcg_temp_new_i64();
13967 if (opcode == 0xa || opcode == 0xb) {
13968 /* Non-accumulating ops */
13969 tcg_passres = tcg_res[pass];
13970 } else {
13971 tcg_passres = tcg_temp_new_i64();
13974 tcg_gen_mul_i64(tcg_passres, tcg_op, tcg_idx);
13975 tcg_temp_free_i64(tcg_op);
13977 if (satop) {
13978 /* saturating, doubling */
13979 gen_helper_neon_addl_saturate_s64(tcg_passres, cpu_env,
13980 tcg_passres, tcg_passres);
13983 if (opcode == 0xa || opcode == 0xb) {
13984 continue;
13987 /* Accumulating op: handle accumulate step */
13988 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
13990 switch (opcode) {
13991 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
13992 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
13993 break;
13994 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
13995 tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
13996 break;
13997 case 0x7: /* SQDMLSL, SQDMLSL2 */
13998 tcg_gen_neg_i64(tcg_passres, tcg_passres);
13999 /* fall through */
14000 case 0x3: /* SQDMLAL, SQDMLAL2 */
14001 gen_helper_neon_addl_saturate_s64(tcg_res[pass], cpu_env,
14002 tcg_res[pass],
14003 tcg_passres);
14004 break;
14005 default:
14006 g_assert_not_reached();
14008 tcg_temp_free_i64(tcg_passres);
14010 tcg_temp_free_i64(tcg_idx);
14012 clear_vec_high(s, !is_scalar, rd);
14013 } else {
14014 TCGv_i32 tcg_idx = tcg_temp_new_i32();
14016 assert(size == 1);
14017 read_vec_element_i32(s, tcg_idx, rm, index, size);
14019 if (!is_scalar) {
14020 /* The simplest way to handle the 16x16 indexed ops is to
14021 * duplicate the index into both halves of the 32 bit tcg_idx
14022 * and then use the usual Neon helpers.
14024 tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
14027 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
14028 TCGv_i32 tcg_op = tcg_temp_new_i32();
14029 TCGv_i64 tcg_passres;
14031 if (is_scalar) {
14032 read_vec_element_i32(s, tcg_op, rn, pass, size);
14033 } else {
14034 read_vec_element_i32(s, tcg_op, rn,
14035 pass + (is_q * 2), MO_32);
14038 tcg_res[pass] = tcg_temp_new_i64();
14040 if (opcode == 0xa || opcode == 0xb) {
14041 /* Non-accumulating ops */
14042 tcg_passres = tcg_res[pass];
14043 } else {
14044 tcg_passres = tcg_temp_new_i64();
14047 if (memop & MO_SIGN) {
14048 gen_helper_neon_mull_s16(tcg_passres, tcg_op, tcg_idx);
14049 } else {
14050 gen_helper_neon_mull_u16(tcg_passres, tcg_op, tcg_idx);
14052 if (satop) {
14053 gen_helper_neon_addl_saturate_s32(tcg_passres, cpu_env,
14054 tcg_passres, tcg_passres);
14056 tcg_temp_free_i32(tcg_op);
14058 if (opcode == 0xa || opcode == 0xb) {
14059 continue;
14062 /* Accumulating op: handle accumulate step */
14063 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
14065 switch (opcode) {
14066 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
14067 gen_helper_neon_addl_u32(tcg_res[pass], tcg_res[pass],
14068 tcg_passres);
14069 break;
14070 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
14071 gen_helper_neon_subl_u32(tcg_res[pass], tcg_res[pass],
14072 tcg_passres);
14073 break;
14074 case 0x7: /* SQDMLSL, SQDMLSL2 */
14075 gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
14076 /* fall through */
14077 case 0x3: /* SQDMLAL, SQDMLAL2 */
14078 gen_helper_neon_addl_saturate_s32(tcg_res[pass], cpu_env,
14079 tcg_res[pass],
14080 tcg_passres);
14081 break;
14082 default:
14083 g_assert_not_reached();
14085 tcg_temp_free_i64(tcg_passres);
14087 tcg_temp_free_i32(tcg_idx);
14089 if (is_scalar) {
14090 tcg_gen_ext32u_i64(tcg_res[0], tcg_res[0]);
14094 if (is_scalar) {
14095 tcg_res[1] = tcg_const_i64(0);
14098 for (pass = 0; pass < 2; pass++) {
14099 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
14100 tcg_temp_free_i64(tcg_res[pass]);
14104 if (fpst) {
14105 tcg_temp_free_ptr(fpst);
14109 /* Crypto AES
14110 * 31 24 23 22 21 17 16 12 11 10 9 5 4 0
14111 * +-----------------+------+-----------+--------+-----+------+------+
14112 * | 0 1 0 0 1 1 1 0 | size | 1 0 1 0 0 | opcode | 1 0 | Rn | Rd |
14113 * +-----------------+------+-----------+--------+-----+------+------+
14115 static void disas_crypto_aes(DisasContext *s, uint32_t insn)
14117 int size = extract32(insn, 22, 2);
14118 int opcode = extract32(insn, 12, 5);
14119 int rn = extract32(insn, 5, 5);
14120 int rd = extract32(insn, 0, 5);
14121 int decrypt;
14122 gen_helper_gvec_2 *genfn2 = NULL;
14123 gen_helper_gvec_3 *genfn3 = NULL;
14125 if (!dc_isar_feature(aa64_aes, s) || size != 0) {
14126 unallocated_encoding(s);
14127 return;
14130 switch (opcode) {
14131 case 0x4: /* AESE */
14132 decrypt = 0;
14133 genfn3 = gen_helper_crypto_aese;
14134 break;
14135 case 0x6: /* AESMC */
14136 decrypt = 0;
14137 genfn2 = gen_helper_crypto_aesmc;
14138 break;
14139 case 0x5: /* AESD */
14140 decrypt = 1;
14141 genfn3 = gen_helper_crypto_aese;
14142 break;
14143 case 0x7: /* AESIMC */
14144 decrypt = 1;
14145 genfn2 = gen_helper_crypto_aesmc;
14146 break;
14147 default:
14148 unallocated_encoding(s);
14149 return;
14152 if (!fp_access_check(s)) {
14153 return;
14155 if (genfn2) {
14156 gen_gvec_op2_ool(s, true, rd, rn, decrypt, genfn2);
14157 } else {
14158 gen_gvec_op3_ool(s, true, rd, rd, rn, decrypt, genfn3);
14162 /* Crypto three-reg SHA
14163 * 31 24 23 22 21 20 16 15 14 12 11 10 9 5 4 0
14164 * +-----------------+------+---+------+---+--------+-----+------+------+
14165 * | 0 1 0 1 1 1 1 0 | size | 0 | Rm | 0 | opcode | 0 0 | Rn | Rd |
14166 * +-----------------+------+---+------+---+--------+-----+------+------+
14168 static void disas_crypto_three_reg_sha(DisasContext *s, uint32_t insn)
14170 int size = extract32(insn, 22, 2);
14171 int opcode = extract32(insn, 12, 3);
14172 int rm = extract32(insn, 16, 5);
14173 int rn = extract32(insn, 5, 5);
14174 int rd = extract32(insn, 0, 5);
14175 gen_helper_gvec_3 *genfn;
14176 bool feature;
14178 if (size != 0) {
14179 unallocated_encoding(s);
14180 return;
14183 switch (opcode) {
14184 case 0: /* SHA1C */
14185 genfn = gen_helper_crypto_sha1c;
14186 feature = dc_isar_feature(aa64_sha1, s);
14187 break;
14188 case 1: /* SHA1P */
14189 genfn = gen_helper_crypto_sha1p;
14190 feature = dc_isar_feature(aa64_sha1, s);
14191 break;
14192 case 2: /* SHA1M */
14193 genfn = gen_helper_crypto_sha1m;
14194 feature = dc_isar_feature(aa64_sha1, s);
14195 break;
14196 case 3: /* SHA1SU0 */
14197 genfn = gen_helper_crypto_sha1su0;
14198 feature = dc_isar_feature(aa64_sha1, s);
14199 break;
14200 case 4: /* SHA256H */
14201 genfn = gen_helper_crypto_sha256h;
14202 feature = dc_isar_feature(aa64_sha256, s);
14203 break;
14204 case 5: /* SHA256H2 */
14205 genfn = gen_helper_crypto_sha256h2;
14206 feature = dc_isar_feature(aa64_sha256, s);
14207 break;
14208 case 6: /* SHA256SU1 */
14209 genfn = gen_helper_crypto_sha256su1;
14210 feature = dc_isar_feature(aa64_sha256, s);
14211 break;
14212 default:
14213 unallocated_encoding(s);
14214 return;
14217 if (!feature) {
14218 unallocated_encoding(s);
14219 return;
14222 if (!fp_access_check(s)) {
14223 return;
14225 gen_gvec_op3_ool(s, true, rd, rn, rm, 0, genfn);
14228 /* Crypto two-reg SHA
14229 * 31 24 23 22 21 17 16 12 11 10 9 5 4 0
14230 * +-----------------+------+-----------+--------+-----+------+------+
14231 * | 0 1 0 1 1 1 1 0 | size | 1 0 1 0 0 | opcode | 1 0 | Rn | Rd |
14232 * +-----------------+------+-----------+--------+-----+------+------+
14234 static void disas_crypto_two_reg_sha(DisasContext *s, uint32_t insn)
14236 int size = extract32(insn, 22, 2);
14237 int opcode = extract32(insn, 12, 5);
14238 int rn = extract32(insn, 5, 5);
14239 int rd = extract32(insn, 0, 5);
14240 gen_helper_gvec_2 *genfn;
14241 bool feature;
14243 if (size != 0) {
14244 unallocated_encoding(s);
14245 return;
14248 switch (opcode) {
14249 case 0: /* SHA1H */
14250 feature = dc_isar_feature(aa64_sha1, s);
14251 genfn = gen_helper_crypto_sha1h;
14252 break;
14253 case 1: /* SHA1SU1 */
14254 feature = dc_isar_feature(aa64_sha1, s);
14255 genfn = gen_helper_crypto_sha1su1;
14256 break;
14257 case 2: /* SHA256SU0 */
14258 feature = dc_isar_feature(aa64_sha256, s);
14259 genfn = gen_helper_crypto_sha256su0;
14260 break;
14261 default:
14262 unallocated_encoding(s);
14263 return;
14266 if (!feature) {
14267 unallocated_encoding(s);
14268 return;
14271 if (!fp_access_check(s)) {
14272 return;
14274 gen_gvec_op2_ool(s, true, rd, rn, 0, genfn);
14277 static void gen_rax1_i64(TCGv_i64 d, TCGv_i64 n, TCGv_i64 m)
14279 tcg_gen_rotli_i64(d, m, 1);
14280 tcg_gen_xor_i64(d, d, n);
14283 static void gen_rax1_vec(unsigned vece, TCGv_vec d, TCGv_vec n, TCGv_vec m)
14285 tcg_gen_rotli_vec(vece, d, m, 1);
14286 tcg_gen_xor_vec(vece, d, d, n);
14289 void gen_gvec_rax1(unsigned vece, uint32_t rd_ofs, uint32_t rn_ofs,
14290 uint32_t rm_ofs, uint32_t opr_sz, uint32_t max_sz)
14292 static const TCGOpcode vecop_list[] = { INDEX_op_rotli_vec, 0 };
14293 static const GVecGen3 op = {
14294 .fni8 = gen_rax1_i64,
14295 .fniv = gen_rax1_vec,
14296 .opt_opc = vecop_list,
14297 .fno = gen_helper_crypto_rax1,
14298 .vece = MO_64,
14300 tcg_gen_gvec_3(rd_ofs, rn_ofs, rm_ofs, opr_sz, max_sz, &op);
14303 /* Crypto three-reg SHA512
14304 * 31 21 20 16 15 14 13 12 11 10 9 5 4 0
14305 * +-----------------------+------+---+---+-----+--------+------+------+
14306 * | 1 1 0 0 1 1 1 0 0 1 1 | Rm | 1 | O | 0 0 | opcode | Rn | Rd |
14307 * +-----------------------+------+---+---+-----+--------+------+------+
14309 static void disas_crypto_three_reg_sha512(DisasContext *s, uint32_t insn)
14311 int opcode = extract32(insn, 10, 2);
14312 int o = extract32(insn, 14, 1);
14313 int rm = extract32(insn, 16, 5);
14314 int rn = extract32(insn, 5, 5);
14315 int rd = extract32(insn, 0, 5);
14316 bool feature;
14317 gen_helper_gvec_3 *oolfn = NULL;
14318 GVecGen3Fn *gvecfn = NULL;
14320 if (o == 0) {
14321 switch (opcode) {
14322 case 0: /* SHA512H */
14323 feature = dc_isar_feature(aa64_sha512, s);
14324 oolfn = gen_helper_crypto_sha512h;
14325 break;
14326 case 1: /* SHA512H2 */
14327 feature = dc_isar_feature(aa64_sha512, s);
14328 oolfn = gen_helper_crypto_sha512h2;
14329 break;
14330 case 2: /* SHA512SU1 */
14331 feature = dc_isar_feature(aa64_sha512, s);
14332 oolfn = gen_helper_crypto_sha512su1;
14333 break;
14334 case 3: /* RAX1 */
14335 feature = dc_isar_feature(aa64_sha3, s);
14336 gvecfn = gen_gvec_rax1;
14337 break;
14338 default:
14339 g_assert_not_reached();
14341 } else {
14342 switch (opcode) {
14343 case 0: /* SM3PARTW1 */
14344 feature = dc_isar_feature(aa64_sm3, s);
14345 oolfn = gen_helper_crypto_sm3partw1;
14346 break;
14347 case 1: /* SM3PARTW2 */
14348 feature = dc_isar_feature(aa64_sm3, s);
14349 oolfn = gen_helper_crypto_sm3partw2;
14350 break;
14351 case 2: /* SM4EKEY */
14352 feature = dc_isar_feature(aa64_sm4, s);
14353 oolfn = gen_helper_crypto_sm4ekey;
14354 break;
14355 default:
14356 unallocated_encoding(s);
14357 return;
14361 if (!feature) {
14362 unallocated_encoding(s);
14363 return;
14366 if (!fp_access_check(s)) {
14367 return;
14370 if (oolfn) {
14371 gen_gvec_op3_ool(s, true, rd, rn, rm, 0, oolfn);
14372 } else {
14373 gen_gvec_fn3(s, true, rd, rn, rm, gvecfn, MO_64);
14377 /* Crypto two-reg SHA512
14378 * 31 12 11 10 9 5 4 0
14379 * +-----------------------------------------+--------+------+------+
14380 * | 1 1 0 0 1 1 1 0 1 1 0 0 0 0 0 0 1 0 0 0 | opcode | Rn | Rd |
14381 * +-----------------------------------------+--------+------+------+
14383 static void disas_crypto_two_reg_sha512(DisasContext *s, uint32_t insn)
14385 int opcode = extract32(insn, 10, 2);
14386 int rn = extract32(insn, 5, 5);
14387 int rd = extract32(insn, 0, 5);
14388 bool feature;
14390 switch (opcode) {
14391 case 0: /* SHA512SU0 */
14392 feature = dc_isar_feature(aa64_sha512, s);
14393 break;
14394 case 1: /* SM4E */
14395 feature = dc_isar_feature(aa64_sm4, s);
14396 break;
14397 default:
14398 unallocated_encoding(s);
14399 return;
14402 if (!feature) {
14403 unallocated_encoding(s);
14404 return;
14407 if (!fp_access_check(s)) {
14408 return;
14411 switch (opcode) {
14412 case 0: /* SHA512SU0 */
14413 gen_gvec_op2_ool(s, true, rd, rn, 0, gen_helper_crypto_sha512su0);
14414 break;
14415 case 1: /* SM4E */
14416 gen_gvec_op3_ool(s, true, rd, rd, rn, 0, gen_helper_crypto_sm4e);
14417 break;
14418 default:
14419 g_assert_not_reached();
14423 /* Crypto four-register
14424 * 31 23 22 21 20 16 15 14 10 9 5 4 0
14425 * +-------------------+-----+------+---+------+------+------+
14426 * | 1 1 0 0 1 1 1 0 0 | Op0 | Rm | 0 | Ra | Rn | Rd |
14427 * +-------------------+-----+------+---+------+------+------+
14429 static void disas_crypto_four_reg(DisasContext *s, uint32_t insn)
14431 int op0 = extract32(insn, 21, 2);
14432 int rm = extract32(insn, 16, 5);
14433 int ra = extract32(insn, 10, 5);
14434 int rn = extract32(insn, 5, 5);
14435 int rd = extract32(insn, 0, 5);
14436 bool feature;
14438 switch (op0) {
14439 case 0: /* EOR3 */
14440 case 1: /* BCAX */
14441 feature = dc_isar_feature(aa64_sha3, s);
14442 break;
14443 case 2: /* SM3SS1 */
14444 feature = dc_isar_feature(aa64_sm3, s);
14445 break;
14446 default:
14447 unallocated_encoding(s);
14448 return;
14451 if (!feature) {
14452 unallocated_encoding(s);
14453 return;
14456 if (!fp_access_check(s)) {
14457 return;
14460 if (op0 < 2) {
14461 TCGv_i64 tcg_op1, tcg_op2, tcg_op3, tcg_res[2];
14462 int pass;
14464 tcg_op1 = tcg_temp_new_i64();
14465 tcg_op2 = tcg_temp_new_i64();
14466 tcg_op3 = tcg_temp_new_i64();
14467 tcg_res[0] = tcg_temp_new_i64();
14468 tcg_res[1] = tcg_temp_new_i64();
14470 for (pass = 0; pass < 2; pass++) {
14471 read_vec_element(s, tcg_op1, rn, pass, MO_64);
14472 read_vec_element(s, tcg_op2, rm, pass, MO_64);
14473 read_vec_element(s, tcg_op3, ra, pass, MO_64);
14475 if (op0 == 0) {
14476 /* EOR3 */
14477 tcg_gen_xor_i64(tcg_res[pass], tcg_op2, tcg_op3);
14478 } else {
14479 /* BCAX */
14480 tcg_gen_andc_i64(tcg_res[pass], tcg_op2, tcg_op3);
14482 tcg_gen_xor_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
14484 write_vec_element(s, tcg_res[0], rd, 0, MO_64);
14485 write_vec_element(s, tcg_res[1], rd, 1, MO_64);
14487 tcg_temp_free_i64(tcg_op1);
14488 tcg_temp_free_i64(tcg_op2);
14489 tcg_temp_free_i64(tcg_op3);
14490 tcg_temp_free_i64(tcg_res[0]);
14491 tcg_temp_free_i64(tcg_res[1]);
14492 } else {
14493 TCGv_i32 tcg_op1, tcg_op2, tcg_op3, tcg_res, tcg_zero;
14495 tcg_op1 = tcg_temp_new_i32();
14496 tcg_op2 = tcg_temp_new_i32();
14497 tcg_op3 = tcg_temp_new_i32();
14498 tcg_res = tcg_temp_new_i32();
14499 tcg_zero = tcg_const_i32(0);
14501 read_vec_element_i32(s, tcg_op1, rn, 3, MO_32);
14502 read_vec_element_i32(s, tcg_op2, rm, 3, MO_32);
14503 read_vec_element_i32(s, tcg_op3, ra, 3, MO_32);
14505 tcg_gen_rotri_i32(tcg_res, tcg_op1, 20);
14506 tcg_gen_add_i32(tcg_res, tcg_res, tcg_op2);
14507 tcg_gen_add_i32(tcg_res, tcg_res, tcg_op3);
14508 tcg_gen_rotri_i32(tcg_res, tcg_res, 25);
14510 write_vec_element_i32(s, tcg_zero, rd, 0, MO_32);
14511 write_vec_element_i32(s, tcg_zero, rd, 1, MO_32);
14512 write_vec_element_i32(s, tcg_zero, rd, 2, MO_32);
14513 write_vec_element_i32(s, tcg_res, rd, 3, MO_32);
14515 tcg_temp_free_i32(tcg_op1);
14516 tcg_temp_free_i32(tcg_op2);
14517 tcg_temp_free_i32(tcg_op3);
14518 tcg_temp_free_i32(tcg_res);
14519 tcg_temp_free_i32(tcg_zero);
14523 /* Crypto XAR
14524 * 31 21 20 16 15 10 9 5 4 0
14525 * +-----------------------+------+--------+------+------+
14526 * | 1 1 0 0 1 1 1 0 1 0 0 | Rm | imm6 | Rn | Rd |
14527 * +-----------------------+------+--------+------+------+
14529 static void disas_crypto_xar(DisasContext *s, uint32_t insn)
14531 int rm = extract32(insn, 16, 5);
14532 int imm6 = extract32(insn, 10, 6);
14533 int rn = extract32(insn, 5, 5);
14534 int rd = extract32(insn, 0, 5);
14536 if (!dc_isar_feature(aa64_sha3, s)) {
14537 unallocated_encoding(s);
14538 return;
14541 if (!fp_access_check(s)) {
14542 return;
14545 gen_gvec_xar(MO_64, vec_full_reg_offset(s, rd),
14546 vec_full_reg_offset(s, rn),
14547 vec_full_reg_offset(s, rm), imm6, 16,
14548 vec_full_reg_size(s));
14551 /* Crypto three-reg imm2
14552 * 31 21 20 16 15 14 13 12 11 10 9 5 4 0
14553 * +-----------------------+------+-----+------+--------+------+------+
14554 * | 1 1 0 0 1 1 1 0 0 1 0 | Rm | 1 0 | imm2 | opcode | Rn | Rd |
14555 * +-----------------------+------+-----+------+--------+------+------+
14557 static void disas_crypto_three_reg_imm2(DisasContext *s, uint32_t insn)
14559 static gen_helper_gvec_3 * const fns[4] = {
14560 gen_helper_crypto_sm3tt1a, gen_helper_crypto_sm3tt1b,
14561 gen_helper_crypto_sm3tt2a, gen_helper_crypto_sm3tt2b,
14563 int opcode = extract32(insn, 10, 2);
14564 int imm2 = extract32(insn, 12, 2);
14565 int rm = extract32(insn, 16, 5);
14566 int rn = extract32(insn, 5, 5);
14567 int rd = extract32(insn, 0, 5);
14569 if (!dc_isar_feature(aa64_sm3, s)) {
14570 unallocated_encoding(s);
14571 return;
14574 if (!fp_access_check(s)) {
14575 return;
14578 gen_gvec_op3_ool(s, true, rd, rn, rm, imm2, fns[opcode]);
14581 /* C3.6 Data processing - SIMD, inc Crypto
14583 * As the decode gets a little complex we are using a table based
14584 * approach for this part of the decode.
14586 static const AArch64DecodeTable data_proc_simd[] = {
14587 /* pattern , mask , fn */
14588 { 0x0e200400, 0x9f200400, disas_simd_three_reg_same },
14589 { 0x0e008400, 0x9f208400, disas_simd_three_reg_same_extra },
14590 { 0x0e200000, 0x9f200c00, disas_simd_three_reg_diff },
14591 { 0x0e200800, 0x9f3e0c00, disas_simd_two_reg_misc },
14592 { 0x0e300800, 0x9f3e0c00, disas_simd_across_lanes },
14593 { 0x0e000400, 0x9fe08400, disas_simd_copy },
14594 { 0x0f000000, 0x9f000400, disas_simd_indexed }, /* vector indexed */
14595 /* simd_mod_imm decode is a subset of simd_shift_imm, so must precede it */
14596 { 0x0f000400, 0x9ff80400, disas_simd_mod_imm },
14597 { 0x0f000400, 0x9f800400, disas_simd_shift_imm },
14598 { 0x0e000000, 0xbf208c00, disas_simd_tb },
14599 { 0x0e000800, 0xbf208c00, disas_simd_zip_trn },
14600 { 0x2e000000, 0xbf208400, disas_simd_ext },
14601 { 0x5e200400, 0xdf200400, disas_simd_scalar_three_reg_same },
14602 { 0x5e008400, 0xdf208400, disas_simd_scalar_three_reg_same_extra },
14603 { 0x5e200000, 0xdf200c00, disas_simd_scalar_three_reg_diff },
14604 { 0x5e200800, 0xdf3e0c00, disas_simd_scalar_two_reg_misc },
14605 { 0x5e300800, 0xdf3e0c00, disas_simd_scalar_pairwise },
14606 { 0x5e000400, 0xdfe08400, disas_simd_scalar_copy },
14607 { 0x5f000000, 0xdf000400, disas_simd_indexed }, /* scalar indexed */
14608 { 0x5f000400, 0xdf800400, disas_simd_scalar_shift_imm },
14609 { 0x4e280800, 0xff3e0c00, disas_crypto_aes },
14610 { 0x5e000000, 0xff208c00, disas_crypto_three_reg_sha },
14611 { 0x5e280800, 0xff3e0c00, disas_crypto_two_reg_sha },
14612 { 0xce608000, 0xffe0b000, disas_crypto_three_reg_sha512 },
14613 { 0xcec08000, 0xfffff000, disas_crypto_two_reg_sha512 },
14614 { 0xce000000, 0xff808000, disas_crypto_four_reg },
14615 { 0xce800000, 0xffe00000, disas_crypto_xar },
14616 { 0xce408000, 0xffe0c000, disas_crypto_three_reg_imm2 },
14617 { 0x0e400400, 0x9f60c400, disas_simd_three_reg_same_fp16 },
14618 { 0x0e780800, 0x8f7e0c00, disas_simd_two_reg_misc_fp16 },
14619 { 0x5e400400, 0xdf60c400, disas_simd_scalar_three_reg_same_fp16 },
14620 { 0x00000000, 0x00000000, NULL }
14623 static void disas_data_proc_simd(DisasContext *s, uint32_t insn)
14625 /* Note that this is called with all non-FP cases from
14626 * table C3-6 so it must UNDEF for entries not specifically
14627 * allocated to instructions in that table.
14629 AArch64DecodeFn *fn = lookup_disas_fn(&data_proc_simd[0], insn);
14630 if (fn) {
14631 fn(s, insn);
14632 } else {
14633 unallocated_encoding(s);
14637 /* C3.6 Data processing - SIMD and floating point */
14638 static void disas_data_proc_simd_fp(DisasContext *s, uint32_t insn)
14640 if (extract32(insn, 28, 1) == 1 && extract32(insn, 30, 1) == 0) {
14641 disas_data_proc_fp(s, insn);
14642 } else {
14643 /* SIMD, including crypto */
14644 disas_data_proc_simd(s, insn);
14649 * is_guarded_page:
14650 * @env: The cpu environment
14651 * @s: The DisasContext
14653 * Return true if the page is guarded.
14655 static bool is_guarded_page(CPUARMState *env, DisasContext *s)
14657 uint64_t addr = s->base.pc_first;
14658 #ifdef CONFIG_USER_ONLY
14659 return page_get_flags(addr) & PAGE_BTI;
14660 #else
14661 int mmu_idx = arm_to_core_mmu_idx(s->mmu_idx);
14662 unsigned int index = tlb_index(env, mmu_idx, addr);
14663 CPUTLBEntry *entry = tlb_entry(env, mmu_idx, addr);
14666 * We test this immediately after reading an insn, which means
14667 * that any normal page must be in the TLB. The only exception
14668 * would be for executing from flash or device memory, which
14669 * does not retain the TLB entry.
14671 * FIXME: Assume false for those, for now. We could use
14672 * arm_cpu_get_phys_page_attrs_debug to re-read the page
14673 * table entry even for that case.
14675 return (tlb_hit(entry->addr_code, addr) &&
14676 arm_tlb_bti_gp(&env_tlb(env)->d[mmu_idx].iotlb[index].attrs));
14677 #endif
14681 * btype_destination_ok:
14682 * @insn: The instruction at the branch destination
14683 * @bt: SCTLR_ELx.BT
14684 * @btype: PSTATE.BTYPE, and is non-zero
14686 * On a guarded page, there are a limited number of insns
14687 * that may be present at the branch target:
14688 * - branch target identifiers,
14689 * - paciasp, pacibsp,
14690 * - BRK insn
14691 * - HLT insn
14692 * Anything else causes a Branch Target Exception.
14694 * Return true if the branch is compatible, false to raise BTITRAP.
14696 static bool btype_destination_ok(uint32_t insn, bool bt, int btype)
14698 if ((insn & 0xfffff01fu) == 0xd503201fu) {
14699 /* HINT space */
14700 switch (extract32(insn, 5, 7)) {
14701 case 0b011001: /* PACIASP */
14702 case 0b011011: /* PACIBSP */
14704 * If SCTLR_ELx.BT, then PACI*SP are not compatible
14705 * with btype == 3. Otherwise all btype are ok.
14707 return !bt || btype != 3;
14708 case 0b100000: /* BTI */
14709 /* Not compatible with any btype. */
14710 return false;
14711 case 0b100010: /* BTI c */
14712 /* Not compatible with btype == 3 */
14713 return btype != 3;
14714 case 0b100100: /* BTI j */
14715 /* Not compatible with btype == 2 */
14716 return btype != 2;
14717 case 0b100110: /* BTI jc */
14718 /* Compatible with any btype. */
14719 return true;
14721 } else {
14722 switch (insn & 0xffe0001fu) {
14723 case 0xd4200000u: /* BRK */
14724 case 0xd4400000u: /* HLT */
14725 /* Give priority to the breakpoint exception. */
14726 return true;
14729 return false;
14732 /* C3.1 A64 instruction index by encoding */
14733 static void disas_a64_insn(CPUARMState *env, DisasContext *s)
14735 uint32_t insn;
14737 s->pc_curr = s->base.pc_next;
14738 insn = arm_ldl_code(env, s->base.pc_next, s->sctlr_b);
14739 s->insn = insn;
14740 s->base.pc_next += 4;
14742 s->fp_access_checked = false;
14743 s->sve_access_checked = false;
14745 if (dc_isar_feature(aa64_bti, s)) {
14746 if (s->base.num_insns == 1) {
14748 * At the first insn of the TB, compute s->guarded_page.
14749 * We delayed computing this until successfully reading
14750 * the first insn of the TB, above. This (mostly) ensures
14751 * that the softmmu tlb entry has been populated, and the
14752 * page table GP bit is available.
14754 * Note that we need to compute this even if btype == 0,
14755 * because this value is used for BR instructions later
14756 * where ENV is not available.
14758 s->guarded_page = is_guarded_page(env, s);
14760 /* First insn can have btype set to non-zero. */
14761 tcg_debug_assert(s->btype >= 0);
14764 * Note that the Branch Target Exception has fairly high
14765 * priority -- below debugging exceptions but above most
14766 * everything else. This allows us to handle this now
14767 * instead of waiting until the insn is otherwise decoded.
14769 if (s->btype != 0
14770 && s->guarded_page
14771 && !btype_destination_ok(insn, s->bt, s->btype)) {
14772 gen_exception_insn(s, s->pc_curr, EXCP_UDEF,
14773 syn_btitrap(s->btype),
14774 default_exception_el(s));
14775 return;
14777 } else {
14778 /* Not the first insn: btype must be 0. */
14779 tcg_debug_assert(s->btype == 0);
14783 switch (extract32(insn, 25, 4)) {
14784 case 0x0: case 0x1: case 0x3: /* UNALLOCATED */
14785 unallocated_encoding(s);
14786 break;
14787 case 0x2:
14788 if (!dc_isar_feature(aa64_sve, s) || !disas_sve(s, insn)) {
14789 unallocated_encoding(s);
14791 break;
14792 case 0x8: case 0x9: /* Data processing - immediate */
14793 disas_data_proc_imm(s, insn);
14794 break;
14795 case 0xa: case 0xb: /* Branch, exception generation and system insns */
14796 disas_b_exc_sys(s, insn);
14797 break;
14798 case 0x4:
14799 case 0x6:
14800 case 0xc:
14801 case 0xe: /* Loads and stores */
14802 disas_ldst(s, insn);
14803 break;
14804 case 0x5:
14805 case 0xd: /* Data processing - register */
14806 disas_data_proc_reg(s, insn);
14807 break;
14808 case 0x7:
14809 case 0xf: /* Data processing - SIMD and floating point */
14810 disas_data_proc_simd_fp(s, insn);
14811 break;
14812 default:
14813 assert(FALSE); /* all 15 cases should be handled above */
14814 break;
14817 /* if we allocated any temporaries, free them here */
14818 free_tmp_a64(s);
14821 * After execution of most insns, btype is reset to 0.
14822 * Note that we set btype == -1 when the insn sets btype.
14824 if (s->btype > 0 && s->base.is_jmp != DISAS_NORETURN) {
14825 reset_btype(s);
14829 static void aarch64_tr_init_disas_context(DisasContextBase *dcbase,
14830 CPUState *cpu)
14832 DisasContext *dc = container_of(dcbase, DisasContext, base);
14833 CPUARMState *env = cpu->env_ptr;
14834 ARMCPU *arm_cpu = env_archcpu(env);
14835 CPUARMTBFlags tb_flags = arm_tbflags_from_tb(dc->base.tb);
14836 int bound, core_mmu_idx;
14838 dc->isar = &arm_cpu->isar;
14839 dc->condjmp = 0;
14841 dc->aarch64 = 1;
14842 /* If we are coming from secure EL0 in a system with a 32-bit EL3, then
14843 * there is no secure EL1, so we route exceptions to EL3.
14845 dc->secure_routed_to_el3 = arm_feature(env, ARM_FEATURE_EL3) &&
14846 !arm_el_is_aa64(env, 3);
14847 dc->thumb = 0;
14848 dc->sctlr_b = 0;
14849 dc->be_data = EX_TBFLAG_ANY(tb_flags, BE_DATA) ? MO_BE : MO_LE;
14850 dc->condexec_mask = 0;
14851 dc->condexec_cond = 0;
14852 core_mmu_idx = EX_TBFLAG_ANY(tb_flags, MMUIDX);
14853 dc->mmu_idx = core_to_aa64_mmu_idx(core_mmu_idx);
14854 dc->tbii = EX_TBFLAG_A64(tb_flags, TBII);
14855 dc->tbid = EX_TBFLAG_A64(tb_flags, TBID);
14856 dc->tcma = EX_TBFLAG_A64(tb_flags, TCMA);
14857 dc->current_el = arm_mmu_idx_to_el(dc->mmu_idx);
14858 #if !defined(CONFIG_USER_ONLY)
14859 dc->user = (dc->current_el == 0);
14860 #endif
14861 dc->fp_excp_el = EX_TBFLAG_ANY(tb_flags, FPEXC_EL);
14862 dc->align_mem = EX_TBFLAG_ANY(tb_flags, ALIGN_MEM);
14863 dc->sve_excp_el = EX_TBFLAG_A64(tb_flags, SVEEXC_EL);
14864 dc->sve_len = (EX_TBFLAG_A64(tb_flags, ZCR_LEN) + 1) * 16;
14865 dc->pauth_active = EX_TBFLAG_A64(tb_flags, PAUTH_ACTIVE);
14866 dc->bt = EX_TBFLAG_A64(tb_flags, BT);
14867 dc->btype = EX_TBFLAG_A64(tb_flags, BTYPE);
14868 dc->unpriv = EX_TBFLAG_A64(tb_flags, UNPRIV);
14869 dc->ata = EX_TBFLAG_A64(tb_flags, ATA);
14870 dc->mte_active[0] = EX_TBFLAG_A64(tb_flags, MTE_ACTIVE);
14871 dc->mte_active[1] = EX_TBFLAG_A64(tb_flags, MTE0_ACTIVE);
14872 dc->vec_len = 0;
14873 dc->vec_stride = 0;
14874 dc->cp_regs = arm_cpu->cp_regs;
14875 dc->features = env->features;
14876 dc->dcz_blocksize = arm_cpu->dcz_blocksize;
14878 #ifdef CONFIG_USER_ONLY
14879 /* In sve_probe_page, we assume TBI is enabled. */
14880 tcg_debug_assert(dc->tbid & 1);
14881 #endif
14883 /* Single step state. The code-generation logic here is:
14884 * SS_ACTIVE == 0:
14885 * generate code with no special handling for single-stepping (except
14886 * that anything that can make us go to SS_ACTIVE == 1 must end the TB;
14887 * this happens anyway because those changes are all system register or
14888 * PSTATE writes).
14889 * SS_ACTIVE == 1, PSTATE.SS == 1: (active-not-pending)
14890 * emit code for one insn
14891 * emit code to clear PSTATE.SS
14892 * emit code to generate software step exception for completed step
14893 * end TB (as usual for having generated an exception)
14894 * SS_ACTIVE == 1, PSTATE.SS == 0: (active-pending)
14895 * emit code to generate a software step exception
14896 * end the TB
14898 dc->ss_active = EX_TBFLAG_ANY(tb_flags, SS_ACTIVE);
14899 dc->pstate_ss = EX_TBFLAG_ANY(tb_flags, PSTATE__SS);
14900 dc->is_ldex = false;
14901 dc->debug_target_el = EX_TBFLAG_ANY(tb_flags, DEBUG_TARGET_EL);
14903 /* Bound the number of insns to execute to those left on the page. */
14904 bound = -(dc->base.pc_first | TARGET_PAGE_MASK) / 4;
14906 /* If architectural single step active, limit to 1. */
14907 if (dc->ss_active) {
14908 bound = 1;
14910 dc->base.max_insns = MIN(dc->base.max_insns, bound);
14912 init_tmp_a64_array(dc);
14915 static void aarch64_tr_tb_start(DisasContextBase *db, CPUState *cpu)
14919 static void aarch64_tr_insn_start(DisasContextBase *dcbase, CPUState *cpu)
14921 DisasContext *dc = container_of(dcbase, DisasContext, base);
14923 tcg_gen_insn_start(dc->base.pc_next, 0, 0);
14924 dc->insn_start = tcg_last_op();
14927 static bool aarch64_tr_breakpoint_check(DisasContextBase *dcbase, CPUState *cpu,
14928 const CPUBreakpoint *bp)
14930 DisasContext *dc = container_of(dcbase, DisasContext, base);
14932 if (bp->flags & BP_CPU) {
14933 gen_a64_set_pc_im(dc->base.pc_next);
14934 gen_helper_check_breakpoints(cpu_env);
14935 /* End the TB early; it likely won't be executed */
14936 dc->base.is_jmp = DISAS_TOO_MANY;
14937 } else {
14938 gen_exception_internal_insn(dc, dc->base.pc_next, EXCP_DEBUG);
14939 /* The address covered by the breakpoint must be
14940 included in [tb->pc, tb->pc + tb->size) in order
14941 to for it to be properly cleared -- thus we
14942 increment the PC here so that the logic setting
14943 tb->size below does the right thing. */
14944 dc->base.pc_next += 4;
14945 dc->base.is_jmp = DISAS_NORETURN;
14948 return true;
14951 static void aarch64_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)
14953 DisasContext *dc = container_of(dcbase, DisasContext, base);
14954 CPUARMState *env = cpu->env_ptr;
14956 if (dc->ss_active && !dc->pstate_ss) {
14957 /* Singlestep state is Active-pending.
14958 * If we're in this state at the start of a TB then either
14959 * a) we just took an exception to an EL which is being debugged
14960 * and this is the first insn in the exception handler
14961 * b) debug exceptions were masked and we just unmasked them
14962 * without changing EL (eg by clearing PSTATE.D)
14963 * In either case we're going to take a swstep exception in the
14964 * "did not step an insn" case, and so the syndrome ISV and EX
14965 * bits should be zero.
14967 assert(dc->base.num_insns == 1);
14968 gen_swstep_exception(dc, 0, 0);
14969 dc->base.is_jmp = DISAS_NORETURN;
14970 } else {
14971 disas_a64_insn(env, dc);
14974 translator_loop_temp_check(&dc->base);
14977 static void aarch64_tr_tb_stop(DisasContextBase *dcbase, CPUState *cpu)
14979 DisasContext *dc = container_of(dcbase, DisasContext, base);
14981 if (unlikely(dc->base.singlestep_enabled || dc->ss_active)) {
14982 /* Note that this means single stepping WFI doesn't halt the CPU.
14983 * For conditional branch insns this is harmless unreachable code as
14984 * gen_goto_tb() has already handled emitting the debug exception
14985 * (and thus a tb-jump is not possible when singlestepping).
14987 switch (dc->base.is_jmp) {
14988 default:
14989 gen_a64_set_pc_im(dc->base.pc_next);
14990 /* fall through */
14991 case DISAS_EXIT:
14992 case DISAS_JUMP:
14993 if (dc->base.singlestep_enabled) {
14994 gen_exception_internal(EXCP_DEBUG);
14995 } else {
14996 gen_step_complete_exception(dc);
14998 break;
14999 case DISAS_NORETURN:
15000 break;
15002 } else {
15003 switch (dc->base.is_jmp) {
15004 case DISAS_NEXT:
15005 case DISAS_TOO_MANY:
15006 gen_goto_tb(dc, 1, dc->base.pc_next);
15007 break;
15008 default:
15009 case DISAS_UPDATE_EXIT:
15010 gen_a64_set_pc_im(dc->base.pc_next);
15011 /* fall through */
15012 case DISAS_EXIT:
15013 tcg_gen_exit_tb(NULL, 0);
15014 break;
15015 case DISAS_UPDATE_NOCHAIN:
15016 gen_a64_set_pc_im(dc->base.pc_next);
15017 /* fall through */
15018 case DISAS_JUMP:
15019 tcg_gen_lookup_and_goto_ptr();
15020 break;
15021 case DISAS_NORETURN:
15022 case DISAS_SWI:
15023 break;
15024 case DISAS_WFE:
15025 gen_a64_set_pc_im(dc->base.pc_next);
15026 gen_helper_wfe(cpu_env);
15027 break;
15028 case DISAS_YIELD:
15029 gen_a64_set_pc_im(dc->base.pc_next);
15030 gen_helper_yield(cpu_env);
15031 break;
15032 case DISAS_WFI:
15034 /* This is a special case because we don't want to just halt the CPU
15035 * if trying to debug across a WFI.
15037 TCGv_i32 tmp = tcg_const_i32(4);
15039 gen_a64_set_pc_im(dc->base.pc_next);
15040 gen_helper_wfi(cpu_env, tmp);
15041 tcg_temp_free_i32(tmp);
15042 /* The helper doesn't necessarily throw an exception, but we
15043 * must go back to the main loop to check for interrupts anyway.
15045 tcg_gen_exit_tb(NULL, 0);
15046 break;
15052 static void aarch64_tr_disas_log(const DisasContextBase *dcbase,
15053 CPUState *cpu)
15055 DisasContext *dc = container_of(dcbase, DisasContext, base);
15057 qemu_log("IN: %s\n", lookup_symbol(dc->base.pc_first));
15058 log_target_disas(cpu, dc->base.pc_first, dc->base.tb->size);
15061 const TranslatorOps aarch64_translator_ops = {
15062 .init_disas_context = aarch64_tr_init_disas_context,
15063 .tb_start = aarch64_tr_tb_start,
15064 .insn_start = aarch64_tr_insn_start,
15065 .breakpoint_check = aarch64_tr_breakpoint_check,
15066 .translate_insn = aarch64_tr_translate_insn,
15067 .tb_stop = aarch64_tr_tb_stop,
15068 .disas_log = aarch64_tr_disas_log,