]> git.proxmox.com Git - mirror_qemu.git/blame - target/arm/translate-a64.c
Merge remote-tracking branch 'remotes/vivier/tags/q800-for-5.0-pull-request' into...
[mirror_qemu.git] / target / arm / translate-a64.c
CommitLineData
14ade10f
AG
1/*
2 * AArch64 translation
3 *
4 * Copyright (c) 2013 Alexander Graf <agraf@suse.de>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
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.
15 *
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/>.
18 */
74c21bd0 19#include "qemu/osdep.h"
14ade10f
AG
20
21#include "cpu.h"
63c91552 22#include "exec/exec-all.h"
dcb32f1d
PMD
23#include "tcg/tcg-op.h"
24#include "tcg/tcg-op-gvec.h"
14ade10f 25#include "qemu/log.h"
1d854765 26#include "arm_ldst.h"
14ade10f 27#include "translate.h"
ccd38087 28#include "internals.h"
14ade10f
AG
29#include "qemu/host-utils.h"
30
f1672e6f 31#include "hw/semihosting/semihost.h"
40f860cd
PM
32#include "exec/gen-icount.h"
33
2ef6175a
RH
34#include "exec/helper-proto.h"
35#include "exec/helper-gen.h"
508127e2 36#include "exec/log.h"
14ade10f 37
a7e30d84 38#include "trace-tcg.h"
8c71baed 39#include "translate-a64.h"
62823083 40#include "qemu/atomic128.h"
a7e30d84 41
14ade10f
AG
42static TCGv_i64 cpu_X[32];
43static TCGv_i64 cpu_pc;
14ade10f 44
fa2ef212 45/* Load/store exclusive handling */
fa2ef212 46static TCGv_i64 cpu_exclusive_high;
fa2ef212 47
14ade10f
AG
48static 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"
53};
54
832ffa1c
AG
55enum 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
60};
61
384b26fb
AB
62/* Table based decoder typedefs - used when the relevant bits for decode
63 * are too awkwardly scattered across the instruction (eg SIMD).
64 */
65typedef void AArch64DecodeFn(DisasContext *s, uint32_t insn);
66
67typedef struct AArch64DecodeTable {
68 uint32_t pattern;
69 uint32_t mask;
70 AArch64DecodeFn *disas_fn;
71} AArch64DecodeTable;
72
1f8a73af 73/* Function prototype for gen_ functions for calling Neon helpers */
0a79bc87 74typedef void NeonGenOneOpEnvFn(TCGv_i32, TCGv_ptr, TCGv_i32);
1f8a73af 75typedef void NeonGenTwoOpFn(TCGv_i32, TCGv_i32, TCGv_i32);
6d9571f7 76typedef void NeonGenTwoOpEnvFn(TCGv_i32, TCGv_ptr, TCGv_i32, TCGv_i32);
70d7f984 77typedef void NeonGenTwo64OpFn(TCGv_i64, TCGv_i64, TCGv_i64);
a847f32c 78typedef void NeonGenTwo64OpEnvFn(TCGv_i64, TCGv_ptr, TCGv_i64, TCGv_i64);
d980fd59
PM
79typedef void NeonGenNarrowFn(TCGv_i32, TCGv_i64);
80typedef void NeonGenNarrowEnvFn(TCGv_i32, TCGv_ptr, TCGv_i64);
70d7f984 81typedef void NeonGenWidenFn(TCGv_i64, TCGv_i32);
8908f4d1
AB
82typedef void NeonGenTwoSingleOPFn(TCGv_i32, TCGv_i32, TCGv_i32, TCGv_ptr);
83typedef void NeonGenTwoDoubleOPFn(TCGv_i64, TCGv_i64, TCGv_i64, TCGv_ptr);
6781fa11 84typedef void NeonGenOneOpFn(TCGv_i64, TCGv_i64);
1a66ac61
RH
85typedef void CryptoTwoOpFn(TCGv_ptr, TCGv_ptr);
86typedef void CryptoThreeOpIntFn(TCGv_ptr, TCGv_ptr, TCGv_i32);
87typedef void CryptoThreeOpFn(TCGv_ptr, TCGv_ptr, TCGv_ptr);
14776ab5 88typedef void AtomicThreeOpFn(TCGv_i64, TCGv_i64, TCGv_i64, TCGArg, MemOp);
1f8a73af 89
14ade10f
AG
90/* initialize TCG globals. */
91void a64_translate_init(void)
92{
93 int i;
94
e1ccc054 95 cpu_pc = tcg_global_mem_new_i64(cpu_env,
14ade10f
AG
96 offsetof(CPUARMState, pc),
97 "pc");
98 for (i = 0; i < 32; i++) {
e1ccc054 99 cpu_X[i] = tcg_global_mem_new_i64(cpu_env,
14ade10f
AG
100 offsetof(CPUARMState, xregs[i]),
101 regnames[i]);
102 }
103
e1ccc054 104 cpu_exclusive_high = tcg_global_mem_new_i64(cpu_env,
fa2ef212 105 offsetof(CPUARMState, exclusive_high), "exclusive_high");
14ade10f
AG
106}
107
cc28fc30
RH
108/*
109 * Return the core mmu_idx to use for A64 "unprivileged load/store" insns
110 */
111static int get_a64_user_mem_index(DisasContext *s)
579d21cc 112{
cc28fc30
RH
113 /*
114 * If AccType_UNPRIV is not used, the insn uses AccType_NORMAL,
115 * which is the usual mmu_idx for this cpu state.
579d21cc 116 */
cc28fc30 117 ARMMMUIdx useridx = s->mmu_idx;
8bd5c820 118
cc28fc30
RH
119 if (s->unpriv) {
120 /*
121 * We have pre-computed the condition for AccType_UNPRIV.
122 * Therefore we should never get here with a mmu_idx for
123 * which we do not know the corresponding user mmu_idx.
124 */
125 switch (useridx) {
126 case ARMMMUIdx_E10_1:
452ef8cb 127 case ARMMMUIdx_E10_1_PAN:
cc28fc30
RH
128 useridx = ARMMMUIdx_E10_0;
129 break;
130 case ARMMMUIdx_E20_2:
452ef8cb 131 case ARMMMUIdx_E20_2_PAN:
cc28fc30
RH
132 useridx = ARMMMUIdx_E20_0;
133 break;
134 case ARMMMUIdx_SE10_1:
452ef8cb 135 case ARMMMUIdx_SE10_1_PAN:
cc28fc30
RH
136 useridx = ARMMMUIdx_SE10_0;
137 break;
138 default:
139 g_assert_not_reached();
140 }
579d21cc 141 }
8bd5c820 142 return arm_to_core_mmu_idx(useridx);
579d21cc
PM
143}
144
51bf0d7a
RH
145static void reset_btype(DisasContext *s)
146{
147 if (s->btype != 0) {
148 TCGv_i32 zero = tcg_const_i32(0);
149 tcg_gen_st_i32(zero, cpu_env, offsetof(CPUARMState, btype));
150 tcg_temp_free_i32(zero);
151 s->btype = 0;
152 }
153}
154
001d47b6
RH
155static void set_btype(DisasContext *s, int val)
156{
157 TCGv_i32 tcg_val;
158
159 /* BTYPE is a 2-bit field, and 0 should be done with reset_btype. */
160 tcg_debug_assert(val >= 1 && val <= 3);
161
162 tcg_val = tcg_const_i32(val);
163 tcg_gen_st_i32(tcg_val, cpu_env, offsetof(CPUARMState, btype));
164 tcg_temp_free_i32(tcg_val);
165 s->btype = -1;
166}
167
14ade10f
AG
168void gen_a64_set_pc_im(uint64_t val)
169{
170 tcg_gen_movi_i64(cpu_pc, val);
171}
172
4a9ee99d
RH
173/*
174 * Handle Top Byte Ignore (TBI) bits.
6feecb8b 175 *
4a9ee99d 176 * If address tagging is enabled via the TCR TBI bits:
6feecb8b
TH
177 * + for EL2 and EL3 there is only one TBI bit, and if it is set
178 * then the address is zero-extended, clearing bits [63:56]
179 * + for EL0 and EL1, TBI0 controls addresses with bit 55 == 0
180 * and TBI1 controls addressses with bit 55 == 1.
181 * If the appropriate TBI bit is set for the address then
182 * the address is sign-extended from bit 55 into bits [63:56]
183 *
4a9ee99d 184 * Here We have concatenated TBI{1,0} into tbi.
6feecb8b 185 */
4a9ee99d
RH
186static void gen_top_byte_ignore(DisasContext *s, TCGv_i64 dst,
187 TCGv_i64 src, int tbi)
6feecb8b 188{
4a9ee99d
RH
189 if (tbi == 0) {
190 /* Load unmodified address */
191 tcg_gen_mov_i64(dst, src);
339370b9 192 } else if (!regime_has_2_ranges(s->mmu_idx)) {
4a9ee99d
RH
193 /* Force tag byte to all zero */
194 tcg_gen_extract_i64(dst, src, 0, 56);
195 } else {
196 /* Sign-extend from bit 55. */
197 tcg_gen_sextract_i64(dst, src, 0, 56);
6feecb8b 198
4a9ee99d
RH
199 if (tbi != 3) {
200 TCGv_i64 tcg_zero = tcg_const_i64(0);
6feecb8b 201
4a9ee99d
RH
202 /*
203 * The two TBI bits differ.
204 * If tbi0, then !tbi1: only use the extension if positive.
205 * if !tbi0, then tbi1: only use the extension if negative.
206 */
207 tcg_gen_movcond_i64(tbi == 1 ? TCG_COND_GE : TCG_COND_LT,
208 dst, dst, tcg_zero, dst, src);
209 tcg_temp_free_i64(tcg_zero);
6feecb8b
TH
210 }
211 }
4a9ee99d 212}
8733d762 213
4a9ee99d
RH
214static void gen_a64_set_pc(DisasContext *s, TCGv_i64 src)
215{
216 /*
217 * If address tagging is enabled for instructions via the TCR TBI bits,
218 * then loading an address into the PC will clear out any tag.
219 */
220 gen_top_byte_ignore(s, cpu_pc, src, s->tbii);
6feecb8b
TH
221}
222
3a471103
RH
223/*
224 * Return a "clean" address for ADDR according to TBID.
225 * This is always a fresh temporary, as we need to be able to
226 * increment this independently of a dirty write-back address.
227 */
228static TCGv_i64 clean_data_tbi(DisasContext *s, TCGv_i64 addr)
229{
230 TCGv_i64 clean = new_tmp_a64(s);
231 gen_top_byte_ignore(s, clean, addr, s->tbid);
232 return clean;
233}
234
259cb684
RH
235typedef struct DisasCompare64 {
236 TCGCond cond;
237 TCGv_i64 value;
238} DisasCompare64;
239
240static void a64_test_cc(DisasCompare64 *c64, int cc)
241{
242 DisasCompare c32;
243
244 arm_test_cc(&c32, cc);
245
246 /* Sign-extend the 32-bit value so that the GE/LT comparisons work
247 * properly. The NE/EQ comparisons are also fine with this choice. */
248 c64->cond = c32.cond;
249 c64->value = tcg_temp_new_i64();
250 tcg_gen_ext_i32_i64(c64->value, c32.value);
251
252 arm_free_cc(&c32);
253}
254
255static void a64_free_cc(DisasCompare64 *c64)
256{
257 tcg_temp_free_i64(c64->value);
258}
259
d4a2dc67 260static void gen_exception_internal(int excp)
14ade10f 261{
d4a2dc67
PM
262 TCGv_i32 tcg_excp = tcg_const_i32(excp);
263
264 assert(excp_is_internal(excp));
265 gen_helper_exception_internal(cpu_env, tcg_excp);
266 tcg_temp_free_i32(tcg_excp);
267}
268
aee828e7 269static void gen_exception_internal_insn(DisasContext *s, uint64_t pc, int excp)
d4a2dc67 270{
aee828e7 271 gen_a64_set_pc_im(pc);
d4a2dc67 272 gen_exception_internal(excp);
dcba3a8d 273 s->base.is_jmp = DISAS_NORETURN;
14ade10f
AG
274}
275
a767fac8 276static void gen_exception_insn(DisasContext *s, uint64_t pc, int excp,
73710361 277 uint32_t syndrome, uint32_t target_el)
14ade10f 278{
a767fac8 279 gen_a64_set_pc_im(pc);
73710361 280 gen_exception(excp, syndrome, target_el);
dcba3a8d 281 s->base.is_jmp = DISAS_NORETURN;
40f860cd
PM
282}
283
06bcbda3 284static void gen_exception_bkpt_insn(DisasContext *s, uint32_t syndrome)
c900a2e6
PM
285{
286 TCGv_i32 tcg_syn;
287
06bcbda3 288 gen_a64_set_pc_im(s->pc_curr);
c900a2e6
PM
289 tcg_syn = tcg_const_i32(syndrome);
290 gen_helper_exception_bkpt_insn(cpu_env, tcg_syn);
291 tcg_temp_free_i32(tcg_syn);
292 s->base.is_jmp = DISAS_NORETURN;
293}
294
7ea47fe7
PM
295static void gen_step_complete_exception(DisasContext *s)
296{
297 /* We just completed step of an insn. Move from Active-not-pending
298 * to Active-pending, and then also take the swstep exception.
299 * This corresponds to making the (IMPDEF) choice to prioritize
300 * swstep exceptions over asynchronous exceptions taken to an exception
301 * level where debug is disabled. This choice has the advantage that
302 * we do not need to maintain internal state corresponding to the
303 * ISV/EX syndrome bits between completion of the step and generation
304 * of the exception, and our syndrome information is always correct.
305 */
306 gen_ss_advance(s);
c1d5f50f 307 gen_swstep_exception(s, 1, s->is_ldex);
dcba3a8d 308 s->base.is_jmp = DISAS_NORETURN;
7ea47fe7
PM
309}
310
40f860cd
PM
311static inline bool use_goto_tb(DisasContext *s, int n, uint64_t dest)
312{
7ea47fe7
PM
313 /* No direct tb linking with singlestep (either QEMU's or the ARM
314 * debug architecture kind) or deterministic io
315 */
c5a49c63
EC
316 if (s->base.singlestep_enabled || s->ss_active ||
317 (tb_cflags(s->base.tb) & CF_LAST_IO)) {
40f860cd
PM
318 return false;
319 }
320
90aa39a1 321#ifndef CONFIG_USER_ONLY
40f860cd 322 /* Only link tbs from inside the same guest page */
dcba3a8d 323 if ((s->base.tb->pc & TARGET_PAGE_MASK) != (dest & TARGET_PAGE_MASK)) {
40f860cd
PM
324 return false;
325 }
90aa39a1 326#endif
40f860cd
PM
327
328 return true;
329}
330
331static inline void gen_goto_tb(DisasContext *s, int n, uint64_t dest)
332{
333 TranslationBlock *tb;
334
dcba3a8d 335 tb = s->base.tb;
40f860cd
PM
336 if (use_goto_tb(s, n, dest)) {
337 tcg_gen_goto_tb(n);
338 gen_a64_set_pc_im(dest);
07ea28b4 339 tcg_gen_exit_tb(tb, n);
dcba3a8d 340 s->base.is_jmp = DISAS_NORETURN;
40f860cd
PM
341 } else {
342 gen_a64_set_pc_im(dest);
7ea47fe7
PM
343 if (s->ss_active) {
344 gen_step_complete_exception(s);
dcba3a8d 345 } else if (s->base.singlestep_enabled) {
d4a2dc67 346 gen_exception_internal(EXCP_DEBUG);
cc9c1ed1 347 } else {
7f11636d 348 tcg_gen_lookup_and_goto_ptr();
dcba3a8d 349 s->base.is_jmp = DISAS_NORETURN;
40f860cd 350 }
40f860cd 351 }
14ade10f
AG
352}
353
429a71d6
RH
354void unallocated_encoding(DisasContext *s)
355{
356 /* Unallocated and reserved encodings are uncategorized */
357 gen_exception_insn(s, s->pc_curr, EXCP_UDEF, syn_uncategorized(),
358 default_exception_el(s));
359}
360
11e169de
AG
361static void init_tmp_a64_array(DisasContext *s)
362{
363#ifdef CONFIG_DEBUG_TCG
f764718d 364 memset(s->tmp_a64, 0, sizeof(s->tmp_a64));
11e169de
AG
365#endif
366 s->tmp_a64_count = 0;
367}
368
369static void free_tmp_a64(DisasContext *s)
370{
371 int i;
372 for (i = 0; i < s->tmp_a64_count; i++) {
373 tcg_temp_free_i64(s->tmp_a64[i]);
374 }
375 init_tmp_a64_array(s);
376}
377
8c71baed 378TCGv_i64 new_tmp_a64(DisasContext *s)
11e169de
AG
379{
380 assert(s->tmp_a64_count < TMP_A64_MAX);
381 return s->tmp_a64[s->tmp_a64_count++] = tcg_temp_new_i64();
382}
383
8c71baed 384TCGv_i64 new_tmp_a64_zero(DisasContext *s)
11e169de
AG
385{
386 TCGv_i64 t = new_tmp_a64(s);
387 tcg_gen_movi_i64(t, 0);
388 return t;
389}
390
71b46089
AG
391/*
392 * Register access functions
393 *
394 * These functions are used for directly accessing a register in where
395 * changes to the final register value are likely to be made. If you
396 * need to use a register for temporary calculation (e.g. index type
397 * operations) use the read_* form.
398 *
399 * B1.2.1 Register mappings
400 *
401 * In instruction register encoding 31 can refer to ZR (zero register) or
402 * the SP (stack pointer) depending on context. In QEMU's case we map SP
403 * to cpu_X[31] and ZR accesses to a temporary which can be discarded.
404 * This is the point of the _sp forms.
405 */
8c71baed 406TCGv_i64 cpu_reg(DisasContext *s, int reg)
11e169de
AG
407{
408 if (reg == 31) {
409 return new_tmp_a64_zero(s);
410 } else {
411 return cpu_X[reg];
412 }
413}
414
71b46089 415/* register access for when 31 == SP */
8c71baed 416TCGv_i64 cpu_reg_sp(DisasContext *s, int reg)
71b46089
AG
417{
418 return cpu_X[reg];
419}
420
60e53388
AG
421/* read a cpu register in 32bit/64bit mode. Returns a TCGv_i64
422 * representing the register contents. This TCGv is an auto-freed
423 * temporary so it need not be explicitly freed, and may be modified.
424 */
8c71baed 425TCGv_i64 read_cpu_reg(DisasContext *s, int reg, int sf)
60e53388
AG
426{
427 TCGv_i64 v = new_tmp_a64(s);
428 if (reg != 31) {
429 if (sf) {
430 tcg_gen_mov_i64(v, cpu_X[reg]);
431 } else {
432 tcg_gen_ext32u_i64(v, cpu_X[reg]);
433 }
434 } else {
435 tcg_gen_movi_i64(v, 0);
436 }
437 return v;
438}
439
8c71baed 440TCGv_i64 read_cpu_reg_sp(DisasContext *s, int reg, int sf)
4a08d475
PM
441{
442 TCGv_i64 v = new_tmp_a64(s);
443 if (sf) {
444 tcg_gen_mov_i64(v, cpu_X[reg]);
445 } else {
446 tcg_gen_ext32u_i64(v, cpu_X[reg]);
447 }
448 return v;
449}
450
e2f90565
PM
451/* Return the offset into CPUARMState of a slice (from
452 * the least significant end) of FP register Qn (ie
453 * Dn, Sn, Hn or Bn).
454 * (Note that this is not the same mapping as for A32; see cpu.h)
455 */
14776ab5 456static inline int fp_reg_offset(DisasContext *s, int regno, MemOp size)
e2f90565 457{
9a2b5256 458 return vec_reg_offset(s, regno, 0, size);
e2f90565
PM
459}
460
461/* Offset of the high half of the 128 bit vector Qn */
90e49638 462static inline int fp_reg_hi_offset(DisasContext *s, int regno)
e2f90565 463{
9a2b5256 464 return vec_reg_offset(s, regno, 1, MO_64);
e2f90565
PM
465}
466
ec73d2e0
AG
467/* Convenience accessors for reading and writing single and double
468 * FP registers. Writing clears the upper parts of the associated
469 * 128 bit vector register, as required by the architecture.
470 * Note that unlike the GP register accessors, the values returned
471 * by the read functions must be manually freed.
472 */
473static TCGv_i64 read_fp_dreg(DisasContext *s, int reg)
474{
475 TCGv_i64 v = tcg_temp_new_i64();
476
90e49638 477 tcg_gen_ld_i64(v, cpu_env, fp_reg_offset(s, reg, MO_64));
ec73d2e0
AG
478 return v;
479}
480
481static TCGv_i32 read_fp_sreg(DisasContext *s, int reg)
482{
483 TCGv_i32 v = tcg_temp_new_i32();
484
90e49638 485 tcg_gen_ld_i32(v, cpu_env, fp_reg_offset(s, reg, MO_32));
ec73d2e0
AG
486 return v;
487}
488
3d99d931
RH
489static TCGv_i32 read_fp_hreg(DisasContext *s, int reg)
490{
491 TCGv_i32 v = tcg_temp_new_i32();
492
493 tcg_gen_ld16u_i32(v, cpu_env, fp_reg_offset(s, reg, MO_16));
494 return v;
495}
496
4ff55bcb
RH
497/* Clear the bits above an N-bit vector, for N = (is_q ? 128 : 64).
498 * If SVE is not enabled, then there are only 128 bits in the vector.
499 */
500static void clear_vec_high(DisasContext *s, bool is_q, int rd)
501{
502 unsigned ofs = fp_reg_offset(s, rd, MO_64);
503 unsigned vsz = vec_full_reg_size(s);
504
505 if (!is_q) {
506 TCGv_i64 tcg_zero = tcg_const_i64(0);
507 tcg_gen_st_i64(tcg_zero, cpu_env, ofs + 8);
508 tcg_temp_free_i64(tcg_zero);
509 }
510 if (vsz > 16) {
511 tcg_gen_gvec_dup8i(ofs + 16, vsz - 16, vsz - 16, 0);
512 }
513}
514
8c71baed 515void write_fp_dreg(DisasContext *s, int reg, TCGv_i64 v)
ec73d2e0 516{
4ff55bcb 517 unsigned ofs = fp_reg_offset(s, reg, MO_64);
ec73d2e0 518
4ff55bcb
RH
519 tcg_gen_st_i64(v, cpu_env, ofs);
520 clear_vec_high(s, false, reg);
ec73d2e0
AG
521}
522
523static void write_fp_sreg(DisasContext *s, int reg, TCGv_i32 v)
524{
525 TCGv_i64 tmp = tcg_temp_new_i64();
526
527 tcg_gen_extu_i32_i64(tmp, v);
528 write_fp_dreg(s, reg, tmp);
529 tcg_temp_free_i64(tmp);
530}
531
8c71baed 532TCGv_ptr get_fpstatus_ptr(bool is_f16)
ec73d2e0
AG
533{
534 TCGv_ptr statusptr = tcg_temp_new_ptr();
535 int offset;
536
d81ce0ef
AB
537 /* In A64 all instructions (both FP and Neon) use the FPCR; there
538 * is no equivalent of the A32 Neon "standard FPSCR value".
539 * However half-precision operations operate under a different
540 * FZ16 flag and use vfp.fp_status_f16 instead of vfp.fp_status.
ec73d2e0 541 */
d81ce0ef
AB
542 if (is_f16) {
543 offset = offsetof(CPUARMState, vfp.fp_status_f16);
544 } else {
545 offset = offsetof(CPUARMState, vfp.fp_status);
546 }
ec73d2e0
AG
547 tcg_gen_addi_ptr(statusptr, cpu_env, offset);
548 return statusptr;
549}
550
377ef731
RH
551/* Expand a 2-operand AdvSIMD vector operation using an expander function. */
552static void gen_gvec_fn2(DisasContext *s, bool is_q, int rd, int rn,
553 GVecGen2Fn *gvec_fn, int vece)
554{
555 gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
556 is_q ? 16 : 8, vec_full_reg_size(s));
557}
558
cdb45a60
RH
559/* Expand a 2-operand + immediate AdvSIMD vector operation using
560 * an expander function.
561 */
562static void gen_gvec_fn2i(DisasContext *s, bool is_q, int rd, int rn,
563 int64_t imm, GVecGen2iFn *gvec_fn, int vece)
564{
565 gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
566 imm, is_q ? 16 : 8, vec_full_reg_size(s));
567}
568
bc48092f
RH
569/* Expand a 3-operand AdvSIMD vector operation using an expander function. */
570static void gen_gvec_fn3(DisasContext *s, bool is_q, int rd, int rn, int rm,
571 GVecGen3Fn *gvec_fn, int vece)
572{
573 gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
574 vec_full_reg_offset(s, rm), is_q ? 16 : 8, vec_full_reg_size(s));
575}
576
3a7a2b4e
RH
577/* Expand a 4-operand AdvSIMD vector operation using an expander function. */
578static void gen_gvec_fn4(DisasContext *s, bool is_q, int rd, int rn, int rm,
579 int rx, GVecGen4Fn *gvec_fn, int vece)
580{
581 gvec_fn(vece, vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
582 vec_full_reg_offset(s, rm), vec_full_reg_offset(s, rx),
583 is_q ? 16 : 8, vec_full_reg_size(s));
584}
585
cdb45a60
RH
586/* Expand a 2-operand + immediate AdvSIMD vector operation using
587 * an op descriptor.
588 */
589static void gen_gvec_op2i(DisasContext *s, bool is_q, int rd,
590 int rn, int64_t imm, const GVecGen2i *gvec_op)
591{
592 tcg_gen_gvec_2i(vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
593 is_q ? 16 : 8, vec_full_reg_size(s), imm, gvec_op);
594}
595
bc48092f
RH
596/* Expand a 3-operand AdvSIMD vector operation using an op descriptor. */
597static void gen_gvec_op3(DisasContext *s, bool is_q, int rd,
598 int rn, int rm, const GVecGen3 *gvec_op)
599{
600 tcg_gen_gvec_3(vec_full_reg_offset(s, rd), vec_full_reg_offset(s, rn),
601 vec_full_reg_offset(s, rm), is_q ? 16 : 8,
602 vec_full_reg_size(s), gvec_op);
603}
604
26c470a7
RH
605/* Expand a 3-operand operation using an out-of-line helper. */
606static void gen_gvec_op3_ool(DisasContext *s, bool is_q, int rd,
607 int rn, int rm, int data, gen_helper_gvec_3 *fn)
608{
609 tcg_gen_gvec_3_ool(vec_full_reg_offset(s, rd),
610 vec_full_reg_offset(s, rn),
611 vec_full_reg_offset(s, rm),
612 is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
613}
614
e7186d82
RH
615/* Expand a 3-operand + env pointer operation using
616 * an out-of-line helper.
617 */
618static void gen_gvec_op3_env(DisasContext *s, bool is_q, int rd,
619 int rn, int rm, gen_helper_gvec_3_ptr *fn)
620{
621 tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
622 vec_full_reg_offset(s, rn),
623 vec_full_reg_offset(s, rm), cpu_env,
624 is_q ? 16 : 8, vec_full_reg_size(s), 0, fn);
625}
626
1695cd61
RH
627/* Expand a 3-operand + fpstatus pointer + simd data value operation using
628 * an out-of-line helper.
629 */
630static void gen_gvec_op3_fpst(DisasContext *s, bool is_q, int rd, int rn,
631 int rm, bool is_fp16, int data,
632 gen_helper_gvec_3_ptr *fn)
633{
634 TCGv_ptr fpst = get_fpstatus_ptr(is_fp16);
635 tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
636 vec_full_reg_offset(s, rn),
637 vec_full_reg_offset(s, rm), fpst,
638 is_q ? 16 : 8, vec_full_reg_size(s), data, fn);
639 tcg_temp_free_ptr(fpst);
640}
641
832ffa1c
AG
642/* Set ZF and NF based on a 64 bit result. This is alas fiddlier
643 * than the 32 bit equivalent.
644 */
645static inline void gen_set_NZ64(TCGv_i64 result)
646{
7cb36e18
RH
647 tcg_gen_extr_i64_i32(cpu_ZF, cpu_NF, result);
648 tcg_gen_or_i32(cpu_ZF, cpu_ZF, cpu_NF);
832ffa1c
AG
649}
650
651/* Set NZCV as for a logical operation: NZ as per result, CV cleared. */
652static inline void gen_logic_CC(int sf, TCGv_i64 result)
653{
654 if (sf) {
655 gen_set_NZ64(result);
656 } else {
ecc7b3aa 657 tcg_gen_extrl_i64_i32(cpu_ZF, result);
7cb36e18 658 tcg_gen_mov_i32(cpu_NF, cpu_ZF);
832ffa1c
AG
659 }
660 tcg_gen_movi_i32(cpu_CF, 0);
661 tcg_gen_movi_i32(cpu_VF, 0);
662}
663
b0ff21b4
AB
664/* dest = T0 + T1; compute C, N, V and Z flags */
665static void gen_add_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
666{
667 if (sf) {
668 TCGv_i64 result, flag, tmp;
669 result = tcg_temp_new_i64();
670 flag = tcg_temp_new_i64();
671 tmp = tcg_temp_new_i64();
672
673 tcg_gen_movi_i64(tmp, 0);
674 tcg_gen_add2_i64(result, flag, t0, tmp, t1, tmp);
675
ecc7b3aa 676 tcg_gen_extrl_i64_i32(cpu_CF, flag);
b0ff21b4
AB
677
678 gen_set_NZ64(result);
679
680 tcg_gen_xor_i64(flag, result, t0);
681 tcg_gen_xor_i64(tmp, t0, t1);
682 tcg_gen_andc_i64(flag, flag, tmp);
683 tcg_temp_free_i64(tmp);
7cb36e18 684 tcg_gen_extrh_i64_i32(cpu_VF, flag);
b0ff21b4
AB
685
686 tcg_gen_mov_i64(dest, result);
687 tcg_temp_free_i64(result);
688 tcg_temp_free_i64(flag);
689 } else {
690 /* 32 bit arithmetic */
691 TCGv_i32 t0_32 = tcg_temp_new_i32();
692 TCGv_i32 t1_32 = tcg_temp_new_i32();
693 TCGv_i32 tmp = tcg_temp_new_i32();
694
695 tcg_gen_movi_i32(tmp, 0);
ecc7b3aa
RH
696 tcg_gen_extrl_i64_i32(t0_32, t0);
697 tcg_gen_extrl_i64_i32(t1_32, t1);
b0ff21b4
AB
698 tcg_gen_add2_i32(cpu_NF, cpu_CF, t0_32, tmp, t1_32, tmp);
699 tcg_gen_mov_i32(cpu_ZF, cpu_NF);
700 tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
701 tcg_gen_xor_i32(tmp, t0_32, t1_32);
702 tcg_gen_andc_i32(cpu_VF, cpu_VF, tmp);
703 tcg_gen_extu_i32_i64(dest, cpu_NF);
704
705 tcg_temp_free_i32(tmp);
706 tcg_temp_free_i32(t0_32);
707 tcg_temp_free_i32(t1_32);
708 }
709}
710
711/* dest = T0 - T1; compute C, N, V and Z flags */
712static void gen_sub_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
713{
714 if (sf) {
715 /* 64 bit arithmetic */
716 TCGv_i64 result, flag, tmp;
717
718 result = tcg_temp_new_i64();
719 flag = tcg_temp_new_i64();
720 tcg_gen_sub_i64(result, t0, t1);
721
722 gen_set_NZ64(result);
723
724 tcg_gen_setcond_i64(TCG_COND_GEU, flag, t0, t1);
ecc7b3aa 725 tcg_gen_extrl_i64_i32(cpu_CF, flag);
b0ff21b4
AB
726
727 tcg_gen_xor_i64(flag, result, t0);
728 tmp = tcg_temp_new_i64();
729 tcg_gen_xor_i64(tmp, t0, t1);
730 tcg_gen_and_i64(flag, flag, tmp);
731 tcg_temp_free_i64(tmp);
7cb36e18 732 tcg_gen_extrh_i64_i32(cpu_VF, flag);
b0ff21b4
AB
733 tcg_gen_mov_i64(dest, result);
734 tcg_temp_free_i64(flag);
735 tcg_temp_free_i64(result);
736 } else {
737 /* 32 bit arithmetic */
738 TCGv_i32 t0_32 = tcg_temp_new_i32();
739 TCGv_i32 t1_32 = tcg_temp_new_i32();
740 TCGv_i32 tmp;
741
ecc7b3aa
RH
742 tcg_gen_extrl_i64_i32(t0_32, t0);
743 tcg_gen_extrl_i64_i32(t1_32, t1);
b0ff21b4
AB
744 tcg_gen_sub_i32(cpu_NF, t0_32, t1_32);
745 tcg_gen_mov_i32(cpu_ZF, cpu_NF);
746 tcg_gen_setcond_i32(TCG_COND_GEU, cpu_CF, t0_32, t1_32);
747 tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
748 tmp = tcg_temp_new_i32();
749 tcg_gen_xor_i32(tmp, t0_32, t1_32);
750 tcg_temp_free_i32(t0_32);
751 tcg_temp_free_i32(t1_32);
752 tcg_gen_and_i32(cpu_VF, cpu_VF, tmp);
753 tcg_temp_free_i32(tmp);
754 tcg_gen_extu_i32_i64(dest, cpu_NF);
755 }
756}
757
643dbb07
CF
758/* dest = T0 + T1 + CF; do not compute flags. */
759static void gen_adc(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
760{
761 TCGv_i64 flag = tcg_temp_new_i64();
762 tcg_gen_extu_i32_i64(flag, cpu_CF);
763 tcg_gen_add_i64(dest, t0, t1);
764 tcg_gen_add_i64(dest, dest, flag);
765 tcg_temp_free_i64(flag);
766
767 if (!sf) {
768 tcg_gen_ext32u_i64(dest, dest);
769 }
770}
771
772/* dest = T0 + T1 + CF; compute C, N, V and Z flags. */
773static void gen_adc_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
774{
775 if (sf) {
776 TCGv_i64 result, cf_64, vf_64, tmp;
777 result = tcg_temp_new_i64();
778 cf_64 = tcg_temp_new_i64();
779 vf_64 = tcg_temp_new_i64();
780 tmp = tcg_const_i64(0);
781
782 tcg_gen_extu_i32_i64(cf_64, cpu_CF);
783 tcg_gen_add2_i64(result, cf_64, t0, tmp, cf_64, tmp);
784 tcg_gen_add2_i64(result, cf_64, result, cf_64, t1, tmp);
ecc7b3aa 785 tcg_gen_extrl_i64_i32(cpu_CF, cf_64);
643dbb07
CF
786 gen_set_NZ64(result);
787
788 tcg_gen_xor_i64(vf_64, result, t0);
789 tcg_gen_xor_i64(tmp, t0, t1);
790 tcg_gen_andc_i64(vf_64, vf_64, tmp);
7cb36e18 791 tcg_gen_extrh_i64_i32(cpu_VF, vf_64);
643dbb07
CF
792
793 tcg_gen_mov_i64(dest, result);
794
795 tcg_temp_free_i64(tmp);
796 tcg_temp_free_i64(vf_64);
797 tcg_temp_free_i64(cf_64);
798 tcg_temp_free_i64(result);
799 } else {
800 TCGv_i32 t0_32, t1_32, tmp;
801 t0_32 = tcg_temp_new_i32();
802 t1_32 = tcg_temp_new_i32();
803 tmp = tcg_const_i32(0);
804
ecc7b3aa
RH
805 tcg_gen_extrl_i64_i32(t0_32, t0);
806 tcg_gen_extrl_i64_i32(t1_32, t1);
643dbb07
CF
807 tcg_gen_add2_i32(cpu_NF, cpu_CF, t0_32, tmp, cpu_CF, tmp);
808 tcg_gen_add2_i32(cpu_NF, cpu_CF, cpu_NF, cpu_CF, t1_32, tmp);
809
810 tcg_gen_mov_i32(cpu_ZF, cpu_NF);
811 tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
812 tcg_gen_xor_i32(tmp, t0_32, t1_32);
813 tcg_gen_andc_i32(cpu_VF, cpu_VF, tmp);
814 tcg_gen_extu_i32_i64(dest, cpu_NF);
815
816 tcg_temp_free_i32(tmp);
817 tcg_temp_free_i32(t1_32);
818 tcg_temp_free_i32(t0_32);
819 }
820}
821
4a08d475
PM
822/*
823 * Load/Store generators
824 */
825
826/*
60510aed 827 * Store from GPR register to memory.
4a08d475 828 */
60510aed 829static void do_gpr_st_memidx(DisasContext *s, TCGv_i64 source,
aaa1f954
EI
830 TCGv_i64 tcg_addr, int size, int memidx,
831 bool iss_valid,
832 unsigned int iss_srt,
833 bool iss_sf, bool iss_ar)
60510aed
PM
834{
835 g_assert(size <= 3);
aa6489da 836 tcg_gen_qemu_st_i64(source, tcg_addr, memidx, s->be_data + size);
aaa1f954
EI
837
838 if (iss_valid) {
839 uint32_t syn;
840
841 syn = syn_data_abort_with_iss(0,
842 size,
843 false,
844 iss_srt,
845 iss_sf,
846 iss_ar,
847 0, 0, 0, 0, 0, false);
848 disas_set_insn_syndrome(s, syn);
849 }
60510aed
PM
850}
851
4a08d475 852static void do_gpr_st(DisasContext *s, TCGv_i64 source,
aaa1f954
EI
853 TCGv_i64 tcg_addr, int size,
854 bool iss_valid,
855 unsigned int iss_srt,
856 bool iss_sf, bool iss_ar)
4a08d475 857{
aaa1f954
EI
858 do_gpr_st_memidx(s, source, tcg_addr, size, get_mem_index(s),
859 iss_valid, iss_srt, iss_sf, iss_ar);
4a08d475
PM
860}
861
862/*
863 * Load from memory to GPR register
864 */
aaa1f954
EI
865static void do_gpr_ld_memidx(DisasContext *s,
866 TCGv_i64 dest, TCGv_i64 tcg_addr,
867 int size, bool is_signed,
868 bool extend, int memidx,
869 bool iss_valid, unsigned int iss_srt,
870 bool iss_sf, bool iss_ar)
4a08d475 871{
14776ab5 872 MemOp memop = s->be_data + size;
4a08d475
PM
873
874 g_assert(size <= 3);
875
876 if (is_signed) {
877 memop += MO_SIGN;
878 }
879
60510aed 880 tcg_gen_qemu_ld_i64(dest, tcg_addr, memidx, memop);
4a08d475
PM
881
882 if (extend && is_signed) {
883 g_assert(size < 3);
884 tcg_gen_ext32u_i64(dest, dest);
885 }
aaa1f954
EI
886
887 if (iss_valid) {
888 uint32_t syn;
889
890 syn = syn_data_abort_with_iss(0,
891 size,
892 is_signed,
893 iss_srt,
894 iss_sf,
895 iss_ar,
896 0, 0, 0, 0, 0, false);
897 disas_set_insn_syndrome(s, syn);
898 }
4a08d475
PM
899}
900
aaa1f954
EI
901static void do_gpr_ld(DisasContext *s,
902 TCGv_i64 dest, TCGv_i64 tcg_addr,
903 int size, bool is_signed, bool extend,
904 bool iss_valid, unsigned int iss_srt,
905 bool iss_sf, bool iss_ar)
60510aed
PM
906{
907 do_gpr_ld_memidx(s, dest, tcg_addr, size, is_signed, extend,
aaa1f954
EI
908 get_mem_index(s),
909 iss_valid, iss_srt, iss_sf, iss_ar);
60510aed
PM
910}
911
4a08d475
PM
912/*
913 * Store from FP register to memory
914 */
915static void do_fp_st(DisasContext *s, int srcidx, TCGv_i64 tcg_addr, int size)
916{
917 /* This writes the bottom N bits of a 128 bit wide vector to memory */
4a08d475 918 TCGv_i64 tmp = tcg_temp_new_i64();
90e49638 919 tcg_gen_ld_i64(tmp, cpu_env, fp_reg_offset(s, srcidx, MO_64));
4a08d475 920 if (size < 4) {
aa6489da
PC
921 tcg_gen_qemu_st_i64(tmp, tcg_addr, get_mem_index(s),
922 s->be_data + size);
4a08d475 923 } else {
aa6489da 924 bool be = s->be_data == MO_BE;
4a08d475 925 TCGv_i64 tcg_hiaddr = tcg_temp_new_i64();
aa6489da 926
4a08d475 927 tcg_gen_addi_i64(tcg_hiaddr, tcg_addr, 8);
aa6489da
PC
928 tcg_gen_qemu_st_i64(tmp, be ? tcg_hiaddr : tcg_addr, get_mem_index(s),
929 s->be_data | MO_Q);
930 tcg_gen_ld_i64(tmp, cpu_env, fp_reg_hi_offset(s, srcidx));
931 tcg_gen_qemu_st_i64(tmp, be ? tcg_addr : tcg_hiaddr, get_mem_index(s),
932 s->be_data | MO_Q);
4a08d475
PM
933 tcg_temp_free_i64(tcg_hiaddr);
934 }
935
936 tcg_temp_free_i64(tmp);
937}
938
939/*
940 * Load from memory to FP register
941 */
942static void do_fp_ld(DisasContext *s, int destidx, TCGv_i64 tcg_addr, int size)
943{
944 /* This always zero-extends and writes to a full 128 bit wide vector */
4a08d475
PM
945 TCGv_i64 tmplo = tcg_temp_new_i64();
946 TCGv_i64 tmphi;
947
948 if (size < 4) {
14776ab5 949 MemOp memop = s->be_data + size;
4a08d475
PM
950 tmphi = tcg_const_i64(0);
951 tcg_gen_qemu_ld_i64(tmplo, tcg_addr, get_mem_index(s), memop);
952 } else {
aa6489da 953 bool be = s->be_data == MO_BE;
4a08d475 954 TCGv_i64 tcg_hiaddr;
aa6489da 955
4a08d475
PM
956 tmphi = tcg_temp_new_i64();
957 tcg_hiaddr = tcg_temp_new_i64();
958
4a08d475 959 tcg_gen_addi_i64(tcg_hiaddr, tcg_addr, 8);
aa6489da
PC
960 tcg_gen_qemu_ld_i64(tmplo, be ? tcg_hiaddr : tcg_addr, get_mem_index(s),
961 s->be_data | MO_Q);
962 tcg_gen_qemu_ld_i64(tmphi, be ? tcg_addr : tcg_hiaddr, get_mem_index(s),
963 s->be_data | MO_Q);
4a08d475
PM
964 tcg_temp_free_i64(tcg_hiaddr);
965 }
966
90e49638
PM
967 tcg_gen_st_i64(tmplo, cpu_env, fp_reg_offset(s, destidx, MO_64));
968 tcg_gen_st_i64(tmphi, cpu_env, fp_reg_hi_offset(s, destidx));
4a08d475
PM
969
970 tcg_temp_free_i64(tmplo);
971 tcg_temp_free_i64(tmphi);
4ff55bcb
RH
972
973 clear_vec_high(s, true, destidx);
4a08d475
PM
974}
975
72430bf5
AB
976/*
977 * Vector load/store helpers.
978 *
979 * The principal difference between this and a FP load is that we don't
980 * zero extend as we are filling a partial chunk of the vector register.
981 * These functions don't support 128 bit loads/stores, which would be
982 * normal load/store operations.
a08582f4
PM
983 *
984 * The _i32 versions are useful when operating on 32 bit quantities
985 * (eg for floating point single or using Neon helper functions).
72430bf5
AB
986 */
987
988/* Get value of an element within a vector register */
989static void read_vec_element(DisasContext *s, TCGv_i64 tcg_dest, int srcidx,
14776ab5 990 int element, MemOp memop)
72430bf5 991{
90e49638 992 int vect_off = vec_reg_offset(s, srcidx, element, memop & MO_SIZE);
72430bf5
AB
993 switch (memop) {
994 case MO_8:
995 tcg_gen_ld8u_i64(tcg_dest, cpu_env, vect_off);
996 break;
997 case MO_16:
998 tcg_gen_ld16u_i64(tcg_dest, cpu_env, vect_off);
999 break;
1000 case MO_32:
1001 tcg_gen_ld32u_i64(tcg_dest, cpu_env, vect_off);
1002 break;
1003 case MO_8|MO_SIGN:
1004 tcg_gen_ld8s_i64(tcg_dest, cpu_env, vect_off);
1005 break;
1006 case MO_16|MO_SIGN:
1007 tcg_gen_ld16s_i64(tcg_dest, cpu_env, vect_off);
1008 break;
1009 case MO_32|MO_SIGN:
1010 tcg_gen_ld32s_i64(tcg_dest, cpu_env, vect_off);
1011 break;
1012 case MO_64:
1013 case MO_64|MO_SIGN:
1014 tcg_gen_ld_i64(tcg_dest, cpu_env, vect_off);
1015 break;
1016 default:
1017 g_assert_not_reached();
1018 }
1019}
1020
a08582f4 1021static void read_vec_element_i32(DisasContext *s, TCGv_i32 tcg_dest, int srcidx,
14776ab5 1022 int element, MemOp memop)
a08582f4 1023{
90e49638 1024 int vect_off = vec_reg_offset(s, srcidx, element, memop & MO_SIZE);
a08582f4
PM
1025 switch (memop) {
1026 case MO_8:
1027 tcg_gen_ld8u_i32(tcg_dest, cpu_env, vect_off);
1028 break;
1029 case MO_16:
1030 tcg_gen_ld16u_i32(tcg_dest, cpu_env, vect_off);
1031 break;
1032 case MO_8|MO_SIGN:
1033 tcg_gen_ld8s_i32(tcg_dest, cpu_env, vect_off);
1034 break;
1035 case MO_16|MO_SIGN:
1036 tcg_gen_ld16s_i32(tcg_dest, cpu_env, vect_off);
1037 break;
1038 case MO_32:
1039 case MO_32|MO_SIGN:
1040 tcg_gen_ld_i32(tcg_dest, cpu_env, vect_off);
1041 break;
1042 default:
1043 g_assert_not_reached();
1044 }
1045}
1046
72430bf5
AB
1047/* Set value of an element within a vector register */
1048static void write_vec_element(DisasContext *s, TCGv_i64 tcg_src, int destidx,
14776ab5 1049 int element, MemOp memop)
72430bf5 1050{
90e49638 1051 int vect_off = vec_reg_offset(s, destidx, element, memop & MO_SIZE);
72430bf5
AB
1052 switch (memop) {
1053 case MO_8:
1054 tcg_gen_st8_i64(tcg_src, cpu_env, vect_off);
1055 break;
1056 case MO_16:
1057 tcg_gen_st16_i64(tcg_src, cpu_env, vect_off);
1058 break;
1059 case MO_32:
1060 tcg_gen_st32_i64(tcg_src, cpu_env, vect_off);
1061 break;
1062 case MO_64:
1063 tcg_gen_st_i64(tcg_src, cpu_env, vect_off);
1064 break;
1065 default:
1066 g_assert_not_reached();
1067 }
1068}
1069
1f8a73af 1070static void write_vec_element_i32(DisasContext *s, TCGv_i32 tcg_src,
14776ab5 1071 int destidx, int element, MemOp memop)
1f8a73af 1072{
90e49638 1073 int vect_off = vec_reg_offset(s, destidx, element, memop & MO_SIZE);
1f8a73af
PM
1074 switch (memop) {
1075 case MO_8:
1076 tcg_gen_st8_i32(tcg_src, cpu_env, vect_off);
1077 break;
1078 case MO_16:
1079 tcg_gen_st16_i32(tcg_src, cpu_env, vect_off);
1080 break;
1081 case MO_32:
1082 tcg_gen_st_i32(tcg_src, cpu_env, vect_off);
1083 break;
1084 default:
1085 g_assert_not_reached();
1086 }
1087}
1088
72430bf5
AB
1089/* Store from vector register to memory */
1090static void do_vec_st(DisasContext *s, int srcidx, int element,
14776ab5 1091 TCGv_i64 tcg_addr, int size, MemOp endian)
72430bf5 1092{
72430bf5
AB
1093 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
1094
1095 read_vec_element(s, tcg_tmp, srcidx, element, size);
87f9a7f0 1096 tcg_gen_qemu_st_i64(tcg_tmp, tcg_addr, get_mem_index(s), endian | size);
72430bf5
AB
1097
1098 tcg_temp_free_i64(tcg_tmp);
1099}
1100
1101/* Load from memory to vector register */
1102static void do_vec_ld(DisasContext *s, int destidx, int element,
14776ab5 1103 TCGv_i64 tcg_addr, int size, MemOp endian)
72430bf5 1104{
72430bf5
AB
1105 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
1106
87f9a7f0 1107 tcg_gen_qemu_ld_i64(tcg_tmp, tcg_addr, get_mem_index(s), endian | size);
72430bf5
AB
1108 write_vec_element(s, tcg_tmp, destidx, element, size);
1109
1110 tcg_temp_free_i64(tcg_tmp);
1111}
1112
8c6afa6a
PM
1113/* Check that FP/Neon access is enabled. If it is, return
1114 * true. If not, emit code to generate an appropriate exception,
1115 * and return false; the caller should not emit any code for
1116 * the instruction. Note that this check must happen after all
1117 * unallocated-encoding checks (otherwise the syndrome information
1118 * for the resulting exception will be incorrect).
1119 */
1120static inline bool fp_access_check(DisasContext *s)
1121{
90e49638
PM
1122 assert(!s->fp_access_checked);
1123 s->fp_access_checked = true;
1124
9dbbc748 1125 if (!s->fp_excp_el) {
8c6afa6a
PM
1126 return true;
1127 }
1128
a767fac8
RH
1129 gen_exception_insn(s, s->pc_curr, EXCP_UDEF,
1130 syn_fp_access_trap(1, 0xe, false), s->fp_excp_el);
8c6afa6a
PM
1131 return false;
1132}
1133
490aa7f1
RH
1134/* Check that SVE access is enabled. If it is, return true.
1135 * If not, emit code to generate an appropriate exception and return false.
1136 */
8c71baed 1137bool sve_access_check(DisasContext *s)
490aa7f1
RH
1138{
1139 if (s->sve_excp_el) {
a767fac8 1140 gen_exception_insn(s, s->pc_curr, EXCP_UDEF, syn_sve_access_trap(),
490aa7f1
RH
1141 s->sve_excp_el);
1142 return false;
1143 }
8c71baed 1144 return fp_access_check(s);
490aa7f1
RH
1145}
1146
229b7a05
AB
1147/*
1148 * This utility function is for doing register extension with an
1149 * optional shift. You will likely want to pass a temporary for the
1150 * destination register. See DecodeRegExtend() in the ARM ARM.
1151 */
1152static void ext_and_shift_reg(TCGv_i64 tcg_out, TCGv_i64 tcg_in,
1153 int option, unsigned int shift)
1154{
1155 int extsize = extract32(option, 0, 2);
1156 bool is_signed = extract32(option, 2, 1);
1157
1158 if (is_signed) {
1159 switch (extsize) {
1160 case 0:
1161 tcg_gen_ext8s_i64(tcg_out, tcg_in);
1162 break;
1163 case 1:
1164 tcg_gen_ext16s_i64(tcg_out, tcg_in);
1165 break;
1166 case 2:
1167 tcg_gen_ext32s_i64(tcg_out, tcg_in);
1168 break;
1169 case 3:
1170 tcg_gen_mov_i64(tcg_out, tcg_in);
1171 break;
1172 }
1173 } else {
1174 switch (extsize) {
1175 case 0:
1176 tcg_gen_ext8u_i64(tcg_out, tcg_in);
1177 break;
1178 case 1:
1179 tcg_gen_ext16u_i64(tcg_out, tcg_in);
1180 break;
1181 case 2:
1182 tcg_gen_ext32u_i64(tcg_out, tcg_in);
1183 break;
1184 case 3:
1185 tcg_gen_mov_i64(tcg_out, tcg_in);
1186 break;
1187 }
1188 }
1189
1190 if (shift) {
1191 tcg_gen_shli_i64(tcg_out, tcg_out, shift);
1192 }
1193}
1194
4a08d475
PM
1195static inline void gen_check_sp_alignment(DisasContext *s)
1196{
1197 /* The AArch64 architecture mandates that (if enabled via PSTATE
1198 * or SCTLR bits) there is a check that SP is 16-aligned on every
1199 * SP-relative load or store (with an exception generated if it is not).
1200 * In line with general QEMU practice regarding misaligned accesses,
1201 * we omit these checks for the sake of guest program performance.
1202 * This function is provided as a hook so we can more easily add these
1203 * checks in future (possibly as a "favour catching guest program bugs
1204 * over speed" user selectable option).
1205 */
1206}
1207
384b26fb
AB
1208/*
1209 * This provides a simple table based table lookup decoder. It is
1210 * intended to be used when the relevant bits for decode are too
1211 * awkwardly placed and switch/if based logic would be confusing and
1212 * deeply nested. Since it's a linear search through the table, tables
1213 * should be kept small.
1214 *
1215 * It returns the first handler where insn & mask == pattern, or
1216 * NULL if there is no match.
1217 * The table is terminated by an empty mask (i.e. 0)
1218 */
1219static inline AArch64DecodeFn *lookup_disas_fn(const AArch64DecodeTable *table,
1220 uint32_t insn)
1221{
1222 const AArch64DecodeTable *tptr = table;
1223
1224 while (tptr->mask) {
1225 if ((insn & tptr->mask) == tptr->pattern) {
1226 return tptr->disas_fn;
1227 }
1228 tptr++;
1229 }
1230 return NULL;
1231}
1232
ad7ee8a2 1233/*
4ce31af4
PM
1234 * The instruction disassembly implemented here matches
1235 * the instruction encoding classifications in chapter C4
1236 * of the ARM Architecture Reference Manual (DDI0487B_a);
1237 * classification names and decode diagrams here should generally
1238 * match up with those in the manual.
ad7ee8a2
CF
1239 */
1240
4ce31af4 1241/* Unconditional branch (immediate)
11e169de
AG
1242 * 31 30 26 25 0
1243 * +----+-----------+-------------------------------------+
1244 * | op | 0 0 1 0 1 | imm26 |
1245 * +----+-----------+-------------------------------------+
1246 */
ad7ee8a2
CF
1247static void disas_uncond_b_imm(DisasContext *s, uint32_t insn)
1248{
43722a6d 1249 uint64_t addr = s->pc_curr + sextract32(insn, 0, 26) * 4;
11e169de 1250
1743d55c 1251 if (insn & (1U << 31)) {
4ce31af4 1252 /* BL Branch with link */
a0415916 1253 tcg_gen_movi_i64(cpu_reg(s, 30), s->base.pc_next);
11e169de
AG
1254 }
1255
4ce31af4 1256 /* B Branch / BL Branch with link */
35862270 1257 reset_btype(s);
11e169de 1258 gen_goto_tb(s, 0, addr);
ad7ee8a2
CF
1259}
1260
4ce31af4 1261/* Compare and branch (immediate)
60e53388
AG
1262 * 31 30 25 24 23 5 4 0
1263 * +----+-------------+----+---------------------+--------+
1264 * | sf | 0 1 1 0 1 0 | op | imm19 | Rt |
1265 * +----+-------------+----+---------------------+--------+
1266 */
ad7ee8a2
CF
1267static void disas_comp_b_imm(DisasContext *s, uint32_t insn)
1268{
60e53388
AG
1269 unsigned int sf, op, rt;
1270 uint64_t addr;
42a268c2 1271 TCGLabel *label_match;
60e53388
AG
1272 TCGv_i64 tcg_cmp;
1273
1274 sf = extract32(insn, 31, 1);
1275 op = extract32(insn, 24, 1); /* 0: CBZ; 1: CBNZ */
1276 rt = extract32(insn, 0, 5);
43722a6d 1277 addr = s->pc_curr + sextract32(insn, 5, 19) * 4;
60e53388
AG
1278
1279 tcg_cmp = read_cpu_reg(s, rt, sf);
1280 label_match = gen_new_label();
1281
35862270 1282 reset_btype(s);
60e53388
AG
1283 tcg_gen_brcondi_i64(op ? TCG_COND_NE : TCG_COND_EQ,
1284 tcg_cmp, 0, label_match);
1285
a0415916 1286 gen_goto_tb(s, 0, s->base.pc_next);
60e53388
AG
1287 gen_set_label(label_match);
1288 gen_goto_tb(s, 1, addr);
ad7ee8a2
CF
1289}
1290
4ce31af4 1291/* Test and branch (immediate)
db0f7958
AG
1292 * 31 30 25 24 23 19 18 5 4 0
1293 * +----+-------------+----+-------+-------------+------+
1294 * | b5 | 0 1 1 0 1 1 | op | b40 | imm14 | Rt |
1295 * +----+-------------+----+-------+-------------+------+
1296 */
ad7ee8a2
CF
1297static void disas_test_b_imm(DisasContext *s, uint32_t insn)
1298{
db0f7958
AG
1299 unsigned int bit_pos, op, rt;
1300 uint64_t addr;
42a268c2 1301 TCGLabel *label_match;
db0f7958
AG
1302 TCGv_i64 tcg_cmp;
1303
1304 bit_pos = (extract32(insn, 31, 1) << 5) | extract32(insn, 19, 5);
1305 op = extract32(insn, 24, 1); /* 0: TBZ; 1: TBNZ */
43722a6d 1306 addr = s->pc_curr + sextract32(insn, 5, 14) * 4;
db0f7958
AG
1307 rt = extract32(insn, 0, 5);
1308
1309 tcg_cmp = tcg_temp_new_i64();
1310 tcg_gen_andi_i64(tcg_cmp, cpu_reg(s, rt), (1ULL << bit_pos));
1311 label_match = gen_new_label();
35862270
RH
1312
1313 reset_btype(s);
db0f7958
AG
1314 tcg_gen_brcondi_i64(op ? TCG_COND_NE : TCG_COND_EQ,
1315 tcg_cmp, 0, label_match);
1316 tcg_temp_free_i64(tcg_cmp);
a0415916 1317 gen_goto_tb(s, 0, s->base.pc_next);
db0f7958
AG
1318 gen_set_label(label_match);
1319 gen_goto_tb(s, 1, addr);
ad7ee8a2
CF
1320}
1321
4ce31af4 1322/* Conditional branch (immediate)
39fb730a
AG
1323 * 31 25 24 23 5 4 3 0
1324 * +---------------+----+---------------------+----+------+
1325 * | 0 1 0 1 0 1 0 | o1 | imm19 | o0 | cond |
1326 * +---------------+----+---------------------+----+------+
1327 */
ad7ee8a2
CF
1328static void disas_cond_b_imm(DisasContext *s, uint32_t insn)
1329{
39fb730a
AG
1330 unsigned int cond;
1331 uint64_t addr;
1332
1333 if ((insn & (1 << 4)) || (insn & (1 << 24))) {
1334 unallocated_encoding(s);
1335 return;
1336 }
43722a6d 1337 addr = s->pc_curr + sextract32(insn, 5, 19) * 4;
39fb730a
AG
1338 cond = extract32(insn, 0, 4);
1339
35862270 1340 reset_btype(s);
39fb730a
AG
1341 if (cond < 0x0e) {
1342 /* genuinely conditional branches */
42a268c2 1343 TCGLabel *label_match = gen_new_label();
39fb730a 1344 arm_gen_test_cc(cond, label_match);
a0415916 1345 gen_goto_tb(s, 0, s->base.pc_next);
39fb730a
AG
1346 gen_set_label(label_match);
1347 gen_goto_tb(s, 1, addr);
1348 } else {
1349 /* 0xe and 0xf are both "always" conditions */
1350 gen_goto_tb(s, 0, addr);
1351 }
ad7ee8a2
CF
1352}
1353
4ce31af4 1354/* HINT instruction group, including various allocated HINTs */
87462e0f
CF
1355static void handle_hint(DisasContext *s, uint32_t insn,
1356 unsigned int op1, unsigned int op2, unsigned int crm)
1357{
1358 unsigned int selector = crm << 3 | op2;
1359
1360 if (op1 != 3) {
1361 unallocated_encoding(s);
1362 return;
1363 }
1364
1365 switch (selector) {
7c94c834
RH
1366 case 0b00000: /* NOP */
1367 break;
1368 case 0b00011: /* WFI */
dcba3a8d 1369 s->base.is_jmp = DISAS_WFI;
7c94c834
RH
1370 break;
1371 case 0b00001: /* YIELD */
2399d4e7
EC
1372 /* When running in MTTCG we don't generate jumps to the yield and
1373 * WFE helpers as it won't affect the scheduling of other vCPUs.
1374 * If we wanted to more completely model WFE/SEV so we don't busy
1375 * spin unnecessarily we would need to do something more involved.
1376 */
2399d4e7 1377 if (!(tb_cflags(s->base.tb) & CF_PARALLEL)) {
dcba3a8d 1378 s->base.is_jmp = DISAS_YIELD;
c22edfeb 1379 }
7c94c834
RH
1380 break;
1381 case 0b00010: /* WFE */
2399d4e7 1382 if (!(tb_cflags(s->base.tb) & CF_PARALLEL)) {
dcba3a8d 1383 s->base.is_jmp = DISAS_WFE;
c22edfeb 1384 }
7c94c834
RH
1385 break;
1386 case 0b00100: /* SEV */
1387 case 0b00101: /* SEVL */
87462e0f 1388 /* we treat all as NOP at least for now */
7c94c834
RH
1389 break;
1390 case 0b00111: /* XPACLRI */
1391 if (s->pauth_active) {
1392 gen_helper_xpaci(cpu_X[30], cpu_env, cpu_X[30]);
1393 }
1394 break;
1395 case 0b01000: /* PACIA1716 */
1396 if (s->pauth_active) {
1397 gen_helper_pacia(cpu_X[17], cpu_env, cpu_X[17], cpu_X[16]);
1398 }
1399 break;
1400 case 0b01010: /* PACIB1716 */
1401 if (s->pauth_active) {
1402 gen_helper_pacib(cpu_X[17], cpu_env, cpu_X[17], cpu_X[16]);
1403 }
1404 break;
1405 case 0b01100: /* AUTIA1716 */
1406 if (s->pauth_active) {
1407 gen_helper_autia(cpu_X[17], cpu_env, cpu_X[17], cpu_X[16]);
1408 }
1409 break;
1410 case 0b01110: /* AUTIB1716 */
1411 if (s->pauth_active) {
1412 gen_helper_autib(cpu_X[17], cpu_env, cpu_X[17], cpu_X[16]);
1413 }
1414 break;
1415 case 0b11000: /* PACIAZ */
1416 if (s->pauth_active) {
1417 gen_helper_pacia(cpu_X[30], cpu_env, cpu_X[30],
1418 new_tmp_a64_zero(s));
1419 }
1420 break;
1421 case 0b11001: /* PACIASP */
1422 if (s->pauth_active) {
1423 gen_helper_pacia(cpu_X[30], cpu_env, cpu_X[30], cpu_X[31]);
1424 }
1425 break;
1426 case 0b11010: /* PACIBZ */
1427 if (s->pauth_active) {
1428 gen_helper_pacib(cpu_X[30], cpu_env, cpu_X[30],
1429 new_tmp_a64_zero(s));
1430 }
1431 break;
1432 case 0b11011: /* PACIBSP */
1433 if (s->pauth_active) {
1434 gen_helper_pacib(cpu_X[30], cpu_env, cpu_X[30], cpu_X[31]);
1435 }
1436 break;
1437 case 0b11100: /* AUTIAZ */
1438 if (s->pauth_active) {
1439 gen_helper_autia(cpu_X[30], cpu_env, cpu_X[30],
1440 new_tmp_a64_zero(s));
1441 }
1442 break;
1443 case 0b11101: /* AUTIASP */
1444 if (s->pauth_active) {
1445 gen_helper_autia(cpu_X[30], cpu_env, cpu_X[30], cpu_X[31]);
1446 }
1447 break;
1448 case 0b11110: /* AUTIBZ */
1449 if (s->pauth_active) {
1450 gen_helper_autib(cpu_X[30], cpu_env, cpu_X[30],
1451 new_tmp_a64_zero(s));
1452 }
1453 break;
1454 case 0b11111: /* AUTIBSP */
1455 if (s->pauth_active) {
1456 gen_helper_autib(cpu_X[30], cpu_env, cpu_X[30], cpu_X[31]);
1457 }
1458 break;
87462e0f
CF
1459 default:
1460 /* default specified as NOP equivalent */
7c94c834 1461 break;
87462e0f
CF
1462 }
1463}
1464
fa2ef212
MM
1465static void gen_clrex(DisasContext *s, uint32_t insn)
1466{
1467 tcg_gen_movi_i64(cpu_exclusive_addr, -1);
1468}
1469
87462e0f
CF
1470/* CLREX, DSB, DMB, ISB */
1471static void handle_sync(DisasContext *s, uint32_t insn,
1472 unsigned int op1, unsigned int op2, unsigned int crm)
1473{
ce1bd93f
PK
1474 TCGBar bar;
1475
87462e0f
CF
1476 if (op1 != 3) {
1477 unallocated_encoding(s);
1478 return;
1479 }
1480
1481 switch (op2) {
1482 case 2: /* CLREX */
fa2ef212 1483 gen_clrex(s, insn);
87462e0f
CF
1484 return;
1485 case 4: /* DSB */
1486 case 5: /* DMB */
ce1bd93f
PK
1487 switch (crm & 3) {
1488 case 1: /* MBReqTypes_Reads */
1489 bar = TCG_BAR_SC | TCG_MO_LD_LD | TCG_MO_LD_ST;
1490 break;
1491 case 2: /* MBReqTypes_Writes */
1492 bar = TCG_BAR_SC | TCG_MO_ST_ST;
1493 break;
1494 default: /* MBReqTypes_All */
1495 bar = TCG_BAR_SC | TCG_MO_ALL;
1496 break;
1497 }
1498 tcg_gen_mb(bar);
87462e0f 1499 return;
6df99dec
SS
1500 case 6: /* ISB */
1501 /* We need to break the TB after this insn to execute
1502 * a self-modified code correctly and also to take
1503 * any pending interrupts immediately.
1504 */
35862270 1505 reset_btype(s);
a0415916 1506 gen_goto_tb(s, 0, s->base.pc_next);
6df99dec 1507 return;
9888bd1e
RH
1508
1509 case 7: /* SB */
1510 if (crm != 0 || !dc_isar_feature(aa64_sb, s)) {
1511 goto do_unallocated;
1512 }
1513 /*
1514 * TODO: There is no speculation barrier opcode for TCG;
1515 * MB and end the TB instead.
1516 */
1517 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_SC);
a0415916 1518 gen_goto_tb(s, 0, s->base.pc_next);
9888bd1e
RH
1519 return;
1520
87462e0f 1521 default:
9888bd1e 1522 do_unallocated:
87462e0f
CF
1523 unallocated_encoding(s);
1524 return;
1525 }
1526}
1527
5ef84f11
RH
1528static void gen_xaflag(void)
1529{
1530 TCGv_i32 z = tcg_temp_new_i32();
1531
1532 tcg_gen_setcondi_i32(TCG_COND_EQ, z, cpu_ZF, 0);
1533
1534 /*
1535 * (!C & !Z) << 31
1536 * (!(C | Z)) << 31
1537 * ~((C | Z) << 31)
1538 * ~-(C | Z)
1539 * (C | Z) - 1
1540 */
1541 tcg_gen_or_i32(cpu_NF, cpu_CF, z);
1542 tcg_gen_subi_i32(cpu_NF, cpu_NF, 1);
1543
1544 /* !(Z & C) */
1545 tcg_gen_and_i32(cpu_ZF, z, cpu_CF);
1546 tcg_gen_xori_i32(cpu_ZF, cpu_ZF, 1);
1547
1548 /* (!C & Z) << 31 -> -(Z & ~C) */
1549 tcg_gen_andc_i32(cpu_VF, z, cpu_CF);
1550 tcg_gen_neg_i32(cpu_VF, cpu_VF);
1551
1552 /* C | Z */
1553 tcg_gen_or_i32(cpu_CF, cpu_CF, z);
1554
1555 tcg_temp_free_i32(z);
1556}
1557
1558static void gen_axflag(void)
1559{
1560 tcg_gen_sari_i32(cpu_VF, cpu_VF, 31); /* V ? -1 : 0 */
1561 tcg_gen_andc_i32(cpu_CF, cpu_CF, cpu_VF); /* C & !V */
1562
1563 /* !(Z | V) -> !(!ZF | V) -> ZF & !V -> ZF & ~VF */
1564 tcg_gen_andc_i32(cpu_ZF, cpu_ZF, cpu_VF);
1565
1566 tcg_gen_movi_i32(cpu_NF, 0);
1567 tcg_gen_movi_i32(cpu_VF, 0);
1568}
1569
4ce31af4 1570/* MSR (immediate) - move immediate to processor state field */
87462e0f
CF
1571static void handle_msr_i(DisasContext *s, uint32_t insn,
1572 unsigned int op1, unsigned int op2, unsigned int crm)
1573{
ff730e96 1574 TCGv_i32 t1;
9cfa0b4e 1575 int op = op1 << 3 | op2;
ff730e96
RH
1576
1577 /* End the TB by default, chaining is ok. */
1578 s->base.is_jmp = DISAS_TOO_MANY;
1579
9cfa0b4e 1580 switch (op) {
b89d9c98
RH
1581 case 0x00: /* CFINV */
1582 if (crm != 0 || !dc_isar_feature(aa64_condm_4, s)) {
1583 goto do_unallocated;
1584 }
1585 tcg_gen_xori_i32(cpu_CF, cpu_CF, 1);
1586 s->base.is_jmp = DISAS_NEXT;
1587 break;
1588
5ef84f11
RH
1589 case 0x01: /* XAFlag */
1590 if (crm != 0 || !dc_isar_feature(aa64_condm_5, s)) {
1591 goto do_unallocated;
1592 }
1593 gen_xaflag();
1594 s->base.is_jmp = DISAS_NEXT;
1595 break;
1596
1597 case 0x02: /* AXFlag */
1598 if (crm != 0 || !dc_isar_feature(aa64_condm_5, s)) {
1599 goto do_unallocated;
1600 }
1601 gen_axflag();
1602 s->base.is_jmp = DISAS_NEXT;
1603 break;
1604
9eeb7a1c
RH
1605 case 0x03: /* UAO */
1606 if (!dc_isar_feature(aa64_uao, s) || s->current_el == 0) {
1607 goto do_unallocated;
1608 }
1609 if (crm & 1) {
1610 set_pstate_bits(PSTATE_UAO);
1611 } else {
1612 clear_pstate_bits(PSTATE_UAO);
1613 }
1614 t1 = tcg_const_i32(s->current_el);
1615 gen_helper_rebuild_hflags_a64(cpu_env, t1);
1616 tcg_temp_free_i32(t1);
1617 break;
1618
220f508f
RH
1619 case 0x04: /* PAN */
1620 if (!dc_isar_feature(aa64_pan, s) || s->current_el == 0) {
1621 goto do_unallocated;
1622 }
1623 if (crm & 1) {
1624 set_pstate_bits(PSTATE_PAN);
1625 } else {
1626 clear_pstate_bits(PSTATE_PAN);
1627 }
1628 t1 = tcg_const_i32(s->current_el);
1629 gen_helper_rebuild_hflags_a64(cpu_env, t1);
1630 tcg_temp_free_i32(t1);
1631 break;
1632
9cfa0b4e 1633 case 0x05: /* SPSel */
dcbff19b 1634 if (s->current_el == 0) {
ff730e96 1635 goto do_unallocated;
9cfa0b4e 1636 }
ff730e96
RH
1637 t1 = tcg_const_i32(crm & PSTATE_SP);
1638 gen_helper_msr_i_spsel(cpu_env, t1);
1639 tcg_temp_free_i32(t1);
1640 break;
1641
9cfa0b4e 1642 case 0x1e: /* DAIFSet */
ff730e96
RH
1643 t1 = tcg_const_i32(crm);
1644 gen_helper_msr_i_daifset(cpu_env, t1);
1645 tcg_temp_free_i32(t1);
1646 break;
1647
9cfa0b4e 1648 case 0x1f: /* DAIFClear */
ff730e96
RH
1649 t1 = tcg_const_i32(crm);
1650 gen_helper_msr_i_daifclear(cpu_env, t1);
1651 tcg_temp_free_i32(t1);
8da54b25 1652 /* For DAIFClear, exit the cpu loop to re-evaluate pending IRQs. */
ff730e96 1653 s->base.is_jmp = DISAS_UPDATE;
9cfa0b4e 1654 break;
ff730e96 1655
9cfa0b4e 1656 default:
ff730e96 1657 do_unallocated:
9cfa0b4e
PM
1658 unallocated_encoding(s);
1659 return;
1660 }
87462e0f
CF
1661}
1662
b0d2b7d0
PM
1663static void gen_get_nzcv(TCGv_i64 tcg_rt)
1664{
1665 TCGv_i32 tmp = tcg_temp_new_i32();
1666 TCGv_i32 nzcv = tcg_temp_new_i32();
1667
1668 /* build bit 31, N */
1743d55c 1669 tcg_gen_andi_i32(nzcv, cpu_NF, (1U << 31));
b0d2b7d0
PM
1670 /* build bit 30, Z */
1671 tcg_gen_setcondi_i32(TCG_COND_EQ, tmp, cpu_ZF, 0);
1672 tcg_gen_deposit_i32(nzcv, nzcv, tmp, 30, 1);
1673 /* build bit 29, C */
1674 tcg_gen_deposit_i32(nzcv, nzcv, cpu_CF, 29, 1);
1675 /* build bit 28, V */
1676 tcg_gen_shri_i32(tmp, cpu_VF, 31);
1677 tcg_gen_deposit_i32(nzcv, nzcv, tmp, 28, 1);
1678 /* generate result */
1679 tcg_gen_extu_i32_i64(tcg_rt, nzcv);
1680
1681 tcg_temp_free_i32(nzcv);
1682 tcg_temp_free_i32(tmp);
1683}
1684
1685static void gen_set_nzcv(TCGv_i64 tcg_rt)
b0d2b7d0
PM
1686{
1687 TCGv_i32 nzcv = tcg_temp_new_i32();
1688
1689 /* take NZCV from R[t] */
ecc7b3aa 1690 tcg_gen_extrl_i64_i32(nzcv, tcg_rt);
b0d2b7d0
PM
1691
1692 /* bit 31, N */
1743d55c 1693 tcg_gen_andi_i32(cpu_NF, nzcv, (1U << 31));
b0d2b7d0
PM
1694 /* bit 30, Z */
1695 tcg_gen_andi_i32(cpu_ZF, nzcv, (1 << 30));
1696 tcg_gen_setcondi_i32(TCG_COND_EQ, cpu_ZF, cpu_ZF, 0);
1697 /* bit 29, C */
1698 tcg_gen_andi_i32(cpu_CF, nzcv, (1 << 29));
1699 tcg_gen_shri_i32(cpu_CF, cpu_CF, 29);
1700 /* bit 28, V */
1701 tcg_gen_andi_i32(cpu_VF, nzcv, (1 << 28));
1702 tcg_gen_shli_i32(cpu_VF, cpu_VF, 3);
1703 tcg_temp_free_i32(nzcv);
1704}
1705
4ce31af4
PM
1706/* MRS - move from system register
1707 * MSR (register) - move to system register
1708 * SYS
1709 * SYSL
fea50522
PM
1710 * These are all essentially the same insn in 'read' and 'write'
1711 * versions, with varying op0 fields.
1712 */
1713static void handle_sys(DisasContext *s, uint32_t insn, bool isread,
1714 unsigned int op0, unsigned int op1, unsigned int op2,
87462e0f
CF
1715 unsigned int crn, unsigned int crm, unsigned int rt)
1716{
fea50522
PM
1717 const ARMCPRegInfo *ri;
1718 TCGv_i64 tcg_rt;
87462e0f 1719
fea50522
PM
1720 ri = get_arm_cp_reginfo(s->cp_regs,
1721 ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP,
1722 crn, crm, op0, op1, op2));
87462e0f 1723
fea50522 1724 if (!ri) {
626187d8
PM
1725 /* Unknown register; this might be a guest error or a QEMU
1726 * unimplemented feature.
1727 */
1728 qemu_log_mask(LOG_UNIMP, "%s access to unsupported AArch64 "
1729 "system register op0:%d op1:%d crn:%d crm:%d op2:%d\n",
1730 isread ? "read" : "write", op0, op1, crn, crm, op2);
fea50522
PM
1731 unallocated_encoding(s);
1732 return;
1733 }
1734
1735 /* Check access permissions */
dcbff19b 1736 if (!cp_access_ok(s->current_el, ri, isread)) {
fea50522
PM
1737 unallocated_encoding(s);
1738 return;
1739 }
1740
f59df3f2
PM
1741 if (ri->accessfn) {
1742 /* Emit code to perform further access permissions checks at
1743 * runtime; this may result in an exception.
1744 */
1745 TCGv_ptr tmpptr;
3f208fd7 1746 TCGv_i32 tcg_syn, tcg_isread;
8bcbf37c
PM
1747 uint32_t syndrome;
1748
43722a6d 1749 gen_a64_set_pc_im(s->pc_curr);
f59df3f2 1750 tmpptr = tcg_const_ptr(ri);
8bcbf37c
PM
1751 syndrome = syn_aa64_sysregtrap(op0, op1, op2, crn, crm, rt, isread);
1752 tcg_syn = tcg_const_i32(syndrome);
3f208fd7
PM
1753 tcg_isread = tcg_const_i32(isread);
1754 gen_helper_access_check_cp_reg(cpu_env, tmpptr, tcg_syn, tcg_isread);
f59df3f2 1755 tcg_temp_free_ptr(tmpptr);
8bcbf37c 1756 tcg_temp_free_i32(tcg_syn);
3f208fd7 1757 tcg_temp_free_i32(tcg_isread);
37ff584c
PM
1758 } else if (ri->type & ARM_CP_RAISES_EXC) {
1759 /*
1760 * The readfn or writefn might raise an exception;
1761 * synchronize the CPU state in case it does.
1762 */
1763 gen_a64_set_pc_im(s->pc_curr);
f59df3f2
PM
1764 }
1765
fea50522
PM
1766 /* Handle special cases first */
1767 switch (ri->type & ~(ARM_CP_FLAG_MASK & ~ARM_CP_SPECIAL)) {
1768 case ARM_CP_NOP:
1769 return;
b0d2b7d0
PM
1770 case ARM_CP_NZCV:
1771 tcg_rt = cpu_reg(s, rt);
1772 if (isread) {
1773 gen_get_nzcv(tcg_rt);
1774 } else {
1775 gen_set_nzcv(tcg_rt);
1776 }
1777 return;
0eef9d98
PM
1778 case ARM_CP_CURRENTEL:
1779 /* Reads as current EL value from pstate, which is
1780 * guaranteed to be constant by the tb flags.
1781 */
1782 tcg_rt = cpu_reg(s, rt);
dcbff19b 1783 tcg_gen_movi_i64(tcg_rt, s->current_el << 2);
0eef9d98 1784 return;
aca3f40b
PM
1785 case ARM_CP_DC_ZVA:
1786 /* Writes clear the aligned block of memory which rt points into. */
597d61a3 1787 tcg_rt = clean_data_tbi(s, cpu_reg(s, rt));
aca3f40b
PM
1788 gen_helper_dc_zva(cpu_env, tcg_rt);
1789 return;
fea50522
PM
1790 default:
1791 break;
1792 }
fe03d45f
RH
1793 if ((ri->type & ARM_CP_FPU) && !fp_access_check(s)) {
1794 return;
11d7870b
RH
1795 } else if ((ri->type & ARM_CP_SVE) && !sve_access_check(s)) {
1796 return;
fe03d45f 1797 }
fea50522 1798
c5a49c63 1799 if ((tb_cflags(s->base.tb) & CF_USE_ICOUNT) && (ri->type & ARM_CP_IO)) {
fea50522
PM
1800 gen_io_start();
1801 }
1802
1803 tcg_rt = cpu_reg(s, rt);
1804
1805 if (isread) {
1806 if (ri->type & ARM_CP_CONST) {
1807 tcg_gen_movi_i64(tcg_rt, ri->resetvalue);
1808 } else if (ri->readfn) {
1809 TCGv_ptr tmpptr;
fea50522
PM
1810 tmpptr = tcg_const_ptr(ri);
1811 gen_helper_get_cp_reg64(tcg_rt, cpu_env, tmpptr);
1812 tcg_temp_free_ptr(tmpptr);
1813 } else {
1814 tcg_gen_ld_i64(tcg_rt, cpu_env, ri->fieldoffset);
1815 }
1816 } else {
1817 if (ri->type & ARM_CP_CONST) {
1818 /* If not forbidden by access permissions, treat as WI */
1819 return;
1820 } else if (ri->writefn) {
1821 TCGv_ptr tmpptr;
fea50522
PM
1822 tmpptr = tcg_const_ptr(ri);
1823 gen_helper_set_cp_reg64(cpu_env, tmpptr, tcg_rt);
1824 tcg_temp_free_ptr(tmpptr);
1825 } else {
1826 tcg_gen_st_i64(tcg_rt, cpu_env, ri->fieldoffset);
1827 }
1828 }
1829
c5a49c63 1830 if ((tb_cflags(s->base.tb) & CF_USE_ICOUNT) && (ri->type & ARM_CP_IO)) {
fea50522 1831 /* I/O operations must end the TB here (whether read or write) */
dcba3a8d 1832 s->base.is_jmp = DISAS_UPDATE;
69d66864
RH
1833 }
1834 if (!isread && !(ri->type & ARM_CP_SUPPRESS_TB_END)) {
1835 /*
1836 * A write to any coprocessor regiser that ends a TB
1837 * must rebuild the hflags for the next TB.
1838 */
1839 TCGv_i32 tcg_el = tcg_const_i32(s->current_el);
1840 gen_helper_rebuild_hflags_a64(cpu_env, tcg_el);
1841 tcg_temp_free_i32(tcg_el);
1842 /*
1843 * We default to ending the TB on a coprocessor register write,
fea50522
PM
1844 * but allow this to be suppressed by the register definition
1845 * (usually only necessary to work around guest bugs).
1846 */
dcba3a8d 1847 s->base.is_jmp = DISAS_UPDATE;
fea50522 1848 }
ad7ee8a2
CF
1849}
1850
4ce31af4 1851/* System
87462e0f
CF
1852 * 31 22 21 20 19 18 16 15 12 11 8 7 5 4 0
1853 * +---------------------+---+-----+-----+-------+-------+-----+------+
1854 * | 1 1 0 1 0 1 0 1 0 0 | L | op0 | op1 | CRn | CRm | op2 | Rt |
1855 * +---------------------+---+-----+-----+-------+-------+-----+------+
1856 */
1857static void disas_system(DisasContext *s, uint32_t insn)
1858{
1859 unsigned int l, op0, op1, crn, crm, op2, rt;
1860 l = extract32(insn, 21, 1);
1861 op0 = extract32(insn, 19, 2);
1862 op1 = extract32(insn, 16, 3);
1863 crn = extract32(insn, 12, 4);
1864 crm = extract32(insn, 8, 4);
1865 op2 = extract32(insn, 5, 3);
1866 rt = extract32(insn, 0, 5);
1867
1868 if (op0 == 0) {
1869 if (l || rt != 31) {
1870 unallocated_encoding(s);
1871 return;
1872 }
1873 switch (crn) {
4ce31af4 1874 case 2: /* HINT (including allocated hints like NOP, YIELD, etc) */
87462e0f
CF
1875 handle_hint(s, insn, op1, op2, crm);
1876 break;
1877 case 3: /* CLREX, DSB, DMB, ISB */
1878 handle_sync(s, insn, op1, op2, crm);
1879 break;
4ce31af4 1880 case 4: /* MSR (immediate) */
87462e0f
CF
1881 handle_msr_i(s, insn, op1, op2, crm);
1882 break;
1883 default:
1884 unallocated_encoding(s);
1885 break;
1886 }
1887 return;
1888 }
fea50522 1889 handle_sys(s, insn, l, op0, op1, op2, crn, crm, rt);
87462e0f
CF
1890}
1891
4ce31af4 1892/* Exception generation
9618e809
AG
1893 *
1894 * 31 24 23 21 20 5 4 2 1 0
1895 * +-----------------+-----+------------------------+-----+----+
1896 * | 1 1 0 1 0 1 0 0 | opc | imm16 | op2 | LL |
1897 * +-----------------------+------------------------+----------+
1898 */
ad7ee8a2
CF
1899static void disas_exc(DisasContext *s, uint32_t insn)
1900{
9618e809
AG
1901 int opc = extract32(insn, 21, 3);
1902 int op2_ll = extract32(insn, 0, 5);
d4a2dc67 1903 int imm16 = extract32(insn, 5, 16);
e0d6e6a5 1904 TCGv_i32 tmp;
9618e809
AG
1905
1906 switch (opc) {
1907 case 0:
7ea47fe7
PM
1908 /* For SVC, HVC and SMC we advance the single-step state
1909 * machine before taking the exception. This is architecturally
1910 * mandated, to ensure that single-stepping a system call
1911 * instruction works properly.
1912 */
35979d71 1913 switch (op2_ll) {
957956b3 1914 case 1: /* SVC */
35979d71 1915 gen_ss_advance(s);
a767fac8
RH
1916 gen_exception_insn(s, s->base.pc_next, EXCP_SWI,
1917 syn_aa64_svc(imm16), default_exception_el(s));
35979d71 1918 break;
957956b3 1919 case 2: /* HVC */
dcbff19b 1920 if (s->current_el == 0) {
35979d71
EI
1921 unallocated_encoding(s);
1922 break;
1923 }
1924 /* The pre HVC helper handles cases when HVC gets trapped
1925 * as an undefined insn by runtime configuration.
1926 */
43722a6d 1927 gen_a64_set_pc_im(s->pc_curr);
35979d71
EI
1928 gen_helper_pre_hvc(cpu_env);
1929 gen_ss_advance(s);
a767fac8
RH
1930 gen_exception_insn(s, s->base.pc_next, EXCP_HVC,
1931 syn_aa64_hvc(imm16), 2);
35979d71 1932 break;
957956b3 1933 case 3: /* SMC */
dcbff19b 1934 if (s->current_el == 0) {
e0d6e6a5
EI
1935 unallocated_encoding(s);
1936 break;
1937 }
43722a6d 1938 gen_a64_set_pc_im(s->pc_curr);
e0d6e6a5
EI
1939 tmp = tcg_const_i32(syn_aa64_smc(imm16));
1940 gen_helper_pre_smc(cpu_env, tmp);
1941 tcg_temp_free_i32(tmp);
1942 gen_ss_advance(s);
a767fac8
RH
1943 gen_exception_insn(s, s->base.pc_next, EXCP_SMC,
1944 syn_aa64_smc(imm16), 3);
e0d6e6a5 1945 break;
35979d71
EI
1946 default:
1947 unallocated_encoding(s);
1948 break;
1949 }
9618e809
AG
1950 break;
1951 case 1:
1952 if (op2_ll != 0) {
1953 unallocated_encoding(s);
1954 break;
1955 }
1956 /* BRK */
06bcbda3 1957 gen_exception_bkpt_insn(s, syn_aa64_bkpt(imm16));
9618e809
AG
1958 break;
1959 case 2:
1960 if (op2_ll != 0) {
1961 unallocated_encoding(s);
1962 break;
1963 }
8012c84f
PM
1964 /* HLT. This has two purposes.
1965 * Architecturally, it is an external halting debug instruction.
1966 * Since QEMU doesn't implement external debug, we treat this as
1967 * it is required for halting debug disabled: it will UNDEF.
1968 * Secondly, "HLT 0xf000" is the A64 semihosting syscall instruction.
1969 */
1970 if (semihosting_enabled() && imm16 == 0xf000) {
1971#ifndef CONFIG_USER_ONLY
1972 /* In system mode, don't allow userspace access to semihosting,
1973 * to provide some semblance of security (and for consistency
1974 * with our 32-bit semihosting).
1975 */
1976 if (s->current_el == 0) {
1977 unsupported_encoding(s, insn);
1978 break;
1979 }
1980#endif
4ff5ef9e 1981 gen_exception_internal_insn(s, s->pc_curr, EXCP_SEMIHOST);
8012c84f
PM
1982 } else {
1983 unsupported_encoding(s, insn);
1984 }
9618e809
AG
1985 break;
1986 case 5:
1987 if (op2_ll < 1 || op2_ll > 3) {
1988 unallocated_encoding(s);
1989 break;
1990 }
1991 /* DCPS1, DCPS2, DCPS3 */
1992 unsupported_encoding(s, insn);
1993 break;
1994 default:
1995 unallocated_encoding(s);
1996 break;
1997 }
ad7ee8a2
CF
1998}
1999
4ce31af4 2000/* Unconditional branch (register)
b001c8c3
AG
2001 * 31 25 24 21 20 16 15 10 9 5 4 0
2002 * +---------------+-------+-------+-------+------+-------+
2003 * | 1 1 0 1 0 1 1 | opc | op2 | op3 | Rn | op4 |
2004 * +---------------+-------+-------+-------+------+-------+
2005 */
ad7ee8a2
CF
2006static void disas_uncond_b_reg(DisasContext *s, uint32_t insn)
2007{
b001c8c3 2008 unsigned int opc, op2, op3, rn, op4;
001d47b6 2009 unsigned btype_mod = 2; /* 0: BR, 1: BLR, 2: other */
d9f482a0 2010 TCGv_i64 dst;
561c0a33 2011 TCGv_i64 modifier;
b001c8c3
AG
2012
2013 opc = extract32(insn, 21, 4);
2014 op2 = extract32(insn, 16, 5);
2015 op3 = extract32(insn, 10, 6);
2016 rn = extract32(insn, 5, 5);
2017 op4 = extract32(insn, 0, 5);
2018
f7cf3bfc
RH
2019 if (op2 != 0x1f) {
2020 goto do_unallocated;
b001c8c3
AG
2021 }
2022
2023 switch (opc) {
2024 case 0: /* BR */
b001c8c3 2025 case 1: /* BLR */
6feecb8b 2026 case 2: /* RET */
001d47b6 2027 btype_mod = opc;
f7cf3bfc
RH
2028 switch (op3) {
2029 case 0:
561c0a33 2030 /* BR, BLR, RET */
f7cf3bfc
RH
2031 if (op4 != 0) {
2032 goto do_unallocated;
2033 }
2034 dst = cpu_reg(s, rn);
2035 break;
2036
561c0a33
RH
2037 case 2:
2038 case 3:
2039 if (!dc_isar_feature(aa64_pauth, s)) {
2040 goto do_unallocated;
2041 }
2042 if (opc == 2) {
2043 /* RETAA, RETAB */
2044 if (rn != 0x1f || op4 != 0x1f) {
2045 goto do_unallocated;
2046 }
2047 rn = 30;
2048 modifier = cpu_X[31];
2049 } else {
2050 /* BRAAZ, BRABZ, BLRAAZ, BLRABZ */
2051 if (op4 != 0x1f) {
2052 goto do_unallocated;
2053 }
2054 modifier = new_tmp_a64_zero(s);
2055 }
2056 if (s->pauth_active) {
2057 dst = new_tmp_a64(s);
2058 if (op3 == 2) {
2059 gen_helper_autia(dst, cpu_env, cpu_reg(s, rn), modifier);
2060 } else {
2061 gen_helper_autib(dst, cpu_env, cpu_reg(s, rn), modifier);
2062 }
2063 } else {
2064 dst = cpu_reg(s, rn);
2065 }
2066 break;
2067
f7cf3bfc
RH
2068 default:
2069 goto do_unallocated;
2070 }
f7cf3bfc 2071 gen_a64_set_pc(s, dst);
6feecb8b
TH
2072 /* BLR also needs to load return address */
2073 if (opc == 1) {
a0415916 2074 tcg_gen_movi_i64(cpu_reg(s, 30), s->base.pc_next);
6feecb8b 2075 }
b001c8c3 2076 break;
f7cf3bfc 2077
561c0a33
RH
2078 case 8: /* BRAA */
2079 case 9: /* BLRAA */
2080 if (!dc_isar_feature(aa64_pauth, s)) {
2081 goto do_unallocated;
2082 }
1cf86a86 2083 if ((op3 & ~1) != 2) {
561c0a33
RH
2084 goto do_unallocated;
2085 }
001d47b6 2086 btype_mod = opc & 1;
561c0a33
RH
2087 if (s->pauth_active) {
2088 dst = new_tmp_a64(s);
2089 modifier = cpu_reg_sp(s, op4);
2090 if (op3 == 2) {
2091 gen_helper_autia(dst, cpu_env, cpu_reg(s, rn), modifier);
2092 } else {
2093 gen_helper_autib(dst, cpu_env, cpu_reg(s, rn), modifier);
2094 }
2095 } else {
2096 dst = cpu_reg(s, rn);
2097 }
2098 gen_a64_set_pc(s, dst);
2099 /* BLRAA also needs to load return address */
2100 if (opc == 9) {
a0415916 2101 tcg_gen_movi_i64(cpu_reg(s, 30), s->base.pc_next);
561c0a33
RH
2102 }
2103 break;
2104
b001c8c3 2105 case 4: /* ERET */
dcbff19b 2106 if (s->current_el == 0) {
f7cf3bfc
RH
2107 goto do_unallocated;
2108 }
2109 switch (op3) {
561c0a33 2110 case 0: /* ERET */
f7cf3bfc
RH
2111 if (op4 != 0) {
2112 goto do_unallocated;
2113 }
2114 dst = tcg_temp_new_i64();
2115 tcg_gen_ld_i64(dst, cpu_env,
2116 offsetof(CPUARMState, elr_el[s->current_el]));
2117 break;
2118
561c0a33
RH
2119 case 2: /* ERETAA */
2120 case 3: /* ERETAB */
2121 if (!dc_isar_feature(aa64_pauth, s)) {
2122 goto do_unallocated;
2123 }
2124 if (rn != 0x1f || op4 != 0x1f) {
2125 goto do_unallocated;
2126 }
2127 dst = tcg_temp_new_i64();
2128 tcg_gen_ld_i64(dst, cpu_env,
2129 offsetof(CPUARMState, elr_el[s->current_el]));
2130 if (s->pauth_active) {
2131 modifier = cpu_X[31];
2132 if (op3 == 2) {
2133 gen_helper_autia(dst, cpu_env, dst, modifier);
2134 } else {
2135 gen_helper_autib(dst, cpu_env, dst, modifier);
2136 }
2137 }
2138 break;
2139
f7cf3bfc
RH
2140 default:
2141 goto do_unallocated;
14c521d4 2142 }
e69ad9df
AL
2143 if (tb_cflags(s->base.tb) & CF_USE_ICOUNT) {
2144 gen_io_start();
2145 }
f7cf3bfc 2146
d9f482a0
RH
2147 gen_helper_exception_return(cpu_env, dst);
2148 tcg_temp_free_i64(dst);
b29fd33d 2149 /* Must exit loop to check un-masked IRQs */
dcba3a8d 2150 s->base.is_jmp = DISAS_EXIT;
52e60cdd 2151 return;
f7cf3bfc 2152
b001c8c3 2153 case 5: /* DRPS */
f7cf3bfc
RH
2154 if (op3 != 0 || op4 != 0 || rn != 0x1f) {
2155 goto do_unallocated;
b001c8c3
AG
2156 } else {
2157 unsupported_encoding(s, insn);
2158 }
2159 return;
f7cf3bfc 2160
b001c8c3 2161 default:
f7cf3bfc 2162 do_unallocated:
b001c8c3
AG
2163 unallocated_encoding(s);
2164 return;
2165 }
2166
001d47b6
RH
2167 switch (btype_mod) {
2168 case 0: /* BR */
2169 if (dc_isar_feature(aa64_bti, s)) {
2170 /* BR to {x16,x17} or !guard -> 1, else 3. */
2171 set_btype(s, rn == 16 || rn == 17 || !s->guarded_page ? 1 : 3);
2172 }
2173 break;
2174
2175 case 1: /* BLR */
2176 if (dc_isar_feature(aa64_bti, s)) {
2177 /* BLR sets BTYPE to 2, regardless of source guarded page. */
2178 set_btype(s, 2);
2179 }
2180 break;
2181
2182 default: /* RET or none of the above. */
2183 /* BTYPE will be set to 0 by normal end-of-insn processing. */
2184 break;
2185 }
2186
dcba3a8d 2187 s->base.is_jmp = DISAS_JUMP;
ad7ee8a2
CF
2188}
2189
4ce31af4 2190/* Branches, exception generating and system instructions */
ad7ee8a2
CF
2191static void disas_b_exc_sys(DisasContext *s, uint32_t insn)
2192{
2193 switch (extract32(insn, 25, 7)) {
2194 case 0x0a: case 0x0b:
2195 case 0x4a: case 0x4b: /* Unconditional branch (immediate) */
2196 disas_uncond_b_imm(s, insn);
2197 break;
2198 case 0x1a: case 0x5a: /* Compare & branch (immediate) */
2199 disas_comp_b_imm(s, insn);
2200 break;
2201 case 0x1b: case 0x5b: /* Test & branch (immediate) */
2202 disas_test_b_imm(s, insn);
2203 break;
2204 case 0x2a: /* Conditional branch (immediate) */
2205 disas_cond_b_imm(s, insn);
2206 break;
2207 case 0x6a: /* Exception generation / System */
2208 if (insn & (1 << 24)) {
08d5e3bd
PM
2209 if (extract32(insn, 22, 2) == 0) {
2210 disas_system(s, insn);
2211 } else {
2212 unallocated_encoding(s);
2213 }
ad7ee8a2
CF
2214 } else {
2215 disas_exc(s, insn);
2216 }
2217 break;
2218 case 0x6b: /* Unconditional branch (register) */
2219 disas_uncond_b_reg(s, insn);
2220 break;
2221 default:
2222 unallocated_encoding(s);
2223 break;
2224 }
2225}
2226
5460da50
AB
2227/*
2228 * Load/Store exclusive instructions are implemented by remembering
2229 * the value/address loaded, and seeing if these are the same
2230 * when the store is performed. This is not actually the architecturally
2231 * mandated semantics, but it works for typical guest code sequences
2232 * and avoids having to monitor regular stores.
2233 *
2234 * The store exclusive uses the atomic cmpxchg primitives to avoid
2235 * races in multi-threaded linux-user and when MTTCG softmmu is
2236 * enabled.
2237 */
fa2ef212
MM
2238static void gen_load_exclusive(DisasContext *s, int rt, int rt2,
2239 TCGv_i64 addr, int size, bool is_pair)
2240{
19514cde 2241 int idx = get_mem_index(s);
14776ab5 2242 MemOp memop = s->be_data;
fa2ef212
MM
2243
2244 g_assert(size <= 3);
fa2ef212 2245 if (is_pair) {
5460da50 2246 g_assert(size >= 2);
19514cde
RH
2247 if (size == 2) {
2248 /* The pair must be single-copy atomic for the doubleword. */
4a2fdb78 2249 memop |= MO_64 | MO_ALIGN;
19514cde
RH
2250 tcg_gen_qemu_ld_i64(cpu_exclusive_val, addr, idx, memop);
2251 if (s->be_data == MO_LE) {
2252 tcg_gen_extract_i64(cpu_reg(s, rt), cpu_exclusive_val, 0, 32);
2253 tcg_gen_extract_i64(cpu_reg(s, rt2), cpu_exclusive_val, 32, 32);
2254 } else {
2255 tcg_gen_extract_i64(cpu_reg(s, rt), cpu_exclusive_val, 32, 32);
2256 tcg_gen_extract_i64(cpu_reg(s, rt2), cpu_exclusive_val, 0, 32);
2257 }
2258 } else {
4a2fdb78
AF
2259 /* The pair must be single-copy atomic for *each* doubleword, not
2260 the entire quadword, however it must be quadword aligned. */
19514cde 2261 memop |= MO_64;
4a2fdb78
AF
2262 tcg_gen_qemu_ld_i64(cpu_exclusive_val, addr, idx,
2263 memop | MO_ALIGN_16);
19514cde
RH
2264
2265 TCGv_i64 addr2 = tcg_temp_new_i64();
2266 tcg_gen_addi_i64(addr2, addr, 8);
2267 tcg_gen_qemu_ld_i64(cpu_exclusive_high, addr2, idx, memop);
2268 tcg_temp_free_i64(addr2);
2269
2270 tcg_gen_mov_i64(cpu_reg(s, rt), cpu_exclusive_val);
2271 tcg_gen_mov_i64(cpu_reg(s, rt2), cpu_exclusive_high);
2272 }
2273 } else {
4a2fdb78 2274 memop |= size | MO_ALIGN;
19514cde
RH
2275 tcg_gen_qemu_ld_i64(cpu_exclusive_val, addr, idx, memop);
2276 tcg_gen_mov_i64(cpu_reg(s, rt), cpu_exclusive_val);
fa2ef212 2277 }
fa2ef212
MM
2278 tcg_gen_mov_i64(cpu_exclusive_addr, addr);
2279}
2280
fa2ef212 2281static void gen_store_exclusive(DisasContext *s, int rd, int rt, int rt2,
37e29a64 2282 TCGv_i64 addr, int size, int is_pair)
fa2ef212 2283{
d324b36a
PM
2284 /* if (env->exclusive_addr == addr && env->exclusive_val == [addr]
2285 * && (!is_pair || env->exclusive_high == [addr + datasize])) {
2286 * [addr] = {Rt};
2287 * if (is_pair) {
2288 * [addr + datasize] = {Rt2};
2289 * }
2290 * {Rd} = 0;
2291 * } else {
2292 * {Rd} = 1;
2293 * }
2294 * env->exclusive_addr = -1;
2295 */
42a268c2
RH
2296 TCGLabel *fail_label = gen_new_label();
2297 TCGLabel *done_label = gen_new_label();
d324b36a
PM
2298 TCGv_i64 tmp;
2299
d324b36a
PM
2300 tcg_gen_brcond_i64(TCG_COND_NE, addr, cpu_exclusive_addr, fail_label);
2301
2302 tmp = tcg_temp_new_i64();
d324b36a 2303 if (is_pair) {
1dd089d0 2304 if (size == 2) {
19514cde
RH
2305 if (s->be_data == MO_LE) {
2306 tcg_gen_concat32_i64(tmp, cpu_reg(s, rt), cpu_reg(s, rt2));
2307 } else {
2308 tcg_gen_concat32_i64(tmp, cpu_reg(s, rt2), cpu_reg(s, rt));
2309 }
37e29a64
RH
2310 tcg_gen_atomic_cmpxchg_i64(tmp, cpu_exclusive_addr,
2311 cpu_exclusive_val, tmp,
1dd089d0 2312 get_mem_index(s),
955fd0ad 2313 MO_64 | MO_ALIGN | s->be_data);
19514cde 2314 tcg_gen_setcond_i64(TCG_COND_NE, tmp, tmp, cpu_exclusive_val);
62823083
RH
2315 } else if (tb_cflags(s->base.tb) & CF_PARALLEL) {
2316 if (!HAVE_CMPXCHG128) {
2317 gen_helper_exit_atomic(cpu_env);
2318 s->base.is_jmp = DISAS_NORETURN;
2319 } else if (s->be_data == MO_LE) {
2399d4e7
EC
2320 gen_helper_paired_cmpxchg64_le_parallel(tmp, cpu_env,
2321 cpu_exclusive_addr,
2322 cpu_reg(s, rt),
2323 cpu_reg(s, rt2));
2324 } else {
2399d4e7
EC
2325 gen_helper_paired_cmpxchg64_be_parallel(tmp, cpu_env,
2326 cpu_exclusive_addr,
2327 cpu_reg(s, rt),
2328 cpu_reg(s, rt2));
2399d4e7 2329 }
62823083
RH
2330 } else if (s->be_data == MO_LE) {
2331 gen_helper_paired_cmpxchg64_le(tmp, cpu_env, cpu_exclusive_addr,
2332 cpu_reg(s, rt), cpu_reg(s, rt2));
2333 } else {
2334 gen_helper_paired_cmpxchg64_be(tmp, cpu_env, cpu_exclusive_addr,
2335 cpu_reg(s, rt), cpu_reg(s, rt2));
1dd089d0
EC
2336 }
2337 } else {
37e29a64
RH
2338 tcg_gen_atomic_cmpxchg_i64(tmp, cpu_exclusive_addr, cpu_exclusive_val,
2339 cpu_reg(s, rt), get_mem_index(s),
1dd089d0
EC
2340 size | MO_ALIGN | s->be_data);
2341 tcg_gen_setcond_i64(TCG_COND_NE, tmp, tmp, cpu_exclusive_val);
d324b36a 2342 }
1dd089d0
EC
2343 tcg_gen_mov_i64(cpu_reg(s, rd), tmp);
2344 tcg_temp_free_i64(tmp);
d324b36a 2345 tcg_gen_br(done_label);
1dd089d0 2346
d324b36a
PM
2347 gen_set_label(fail_label);
2348 tcg_gen_movi_i64(cpu_reg(s, rd), 1);
2349 gen_set_label(done_label);
2350 tcg_gen_movi_i64(cpu_exclusive_addr, -1);
fa2ef212 2351}
fa2ef212 2352
44ac14b0
RH
2353static void gen_compare_and_swap(DisasContext *s, int rs, int rt,
2354 int rn, int size)
2355{
2356 TCGv_i64 tcg_rs = cpu_reg(s, rs);
2357 TCGv_i64 tcg_rt = cpu_reg(s, rt);
2358 int memidx = get_mem_index(s);
3a471103 2359 TCGv_i64 clean_addr;
44ac14b0
RH
2360
2361 if (rn == 31) {
2362 gen_check_sp_alignment(s);
2363 }
3a471103
RH
2364 clean_addr = clean_data_tbi(s, cpu_reg_sp(s, rn));
2365 tcg_gen_atomic_cmpxchg_i64(tcg_rs, clean_addr, tcg_rs, tcg_rt, memidx,
44ac14b0
RH
2366 size | MO_ALIGN | s->be_data);
2367}
2368
2369static void gen_compare_and_swap_pair(DisasContext *s, int rs, int rt,
2370 int rn, int size)
2371{
2372 TCGv_i64 s1 = cpu_reg(s, rs);
2373 TCGv_i64 s2 = cpu_reg(s, rs + 1);
2374 TCGv_i64 t1 = cpu_reg(s, rt);
2375 TCGv_i64 t2 = cpu_reg(s, rt + 1);
3a471103 2376 TCGv_i64 clean_addr;
44ac14b0
RH
2377 int memidx = get_mem_index(s);
2378
2379 if (rn == 31) {
2380 gen_check_sp_alignment(s);
2381 }
3a471103 2382 clean_addr = clean_data_tbi(s, cpu_reg_sp(s, rn));
44ac14b0
RH
2383
2384 if (size == 2) {
2385 TCGv_i64 cmp = tcg_temp_new_i64();
2386 TCGv_i64 val = tcg_temp_new_i64();
2387
2388 if (s->be_data == MO_LE) {
2389 tcg_gen_concat32_i64(val, t1, t2);
2390 tcg_gen_concat32_i64(cmp, s1, s2);
2391 } else {
2392 tcg_gen_concat32_i64(val, t2, t1);
2393 tcg_gen_concat32_i64(cmp, s2, s1);
2394 }
2395
3a471103 2396 tcg_gen_atomic_cmpxchg_i64(cmp, clean_addr, cmp, val, memidx,
44ac14b0
RH
2397 MO_64 | MO_ALIGN | s->be_data);
2398 tcg_temp_free_i64(val);
2399
2400 if (s->be_data == MO_LE) {
2401 tcg_gen_extr32_i64(s1, s2, cmp);
2402 } else {
2403 tcg_gen_extr32_i64(s2, s1, cmp);
2404 }
2405 tcg_temp_free_i64(cmp);
2406 } else if (tb_cflags(s->base.tb) & CF_PARALLEL) {
62823083
RH
2407 if (HAVE_CMPXCHG128) {
2408 TCGv_i32 tcg_rs = tcg_const_i32(rs);
2409 if (s->be_data == MO_LE) {
3a471103
RH
2410 gen_helper_casp_le_parallel(cpu_env, tcg_rs,
2411 clean_addr, t1, t2);
62823083 2412 } else {
3a471103
RH
2413 gen_helper_casp_be_parallel(cpu_env, tcg_rs,
2414 clean_addr, t1, t2);
62823083
RH
2415 }
2416 tcg_temp_free_i32(tcg_rs);
44ac14b0 2417 } else {
62823083
RH
2418 gen_helper_exit_atomic(cpu_env);
2419 s->base.is_jmp = DISAS_NORETURN;
44ac14b0 2420 }
44ac14b0
RH
2421 } else {
2422 TCGv_i64 d1 = tcg_temp_new_i64();
2423 TCGv_i64 d2 = tcg_temp_new_i64();
2424 TCGv_i64 a2 = tcg_temp_new_i64();
2425 TCGv_i64 c1 = tcg_temp_new_i64();
2426 TCGv_i64 c2 = tcg_temp_new_i64();
2427 TCGv_i64 zero = tcg_const_i64(0);
2428
2429 /* Load the two words, in memory order. */
3a471103 2430 tcg_gen_qemu_ld_i64(d1, clean_addr, memidx,
44ac14b0 2431 MO_64 | MO_ALIGN_16 | s->be_data);
3a471103 2432 tcg_gen_addi_i64(a2, clean_addr, 8);
a036f530 2433 tcg_gen_qemu_ld_i64(d2, a2, memidx, MO_64 | s->be_data);
44ac14b0
RH
2434
2435 /* Compare the two words, also in memory order. */
2436 tcg_gen_setcond_i64(TCG_COND_EQ, c1, d1, s1);
2437 tcg_gen_setcond_i64(TCG_COND_EQ, c2, d2, s2);
2438 tcg_gen_and_i64(c2, c2, c1);
2439
2440 /* If compare equal, write back new data, else write back old data. */
2441 tcg_gen_movcond_i64(TCG_COND_NE, c1, c2, zero, t1, d1);
2442 tcg_gen_movcond_i64(TCG_COND_NE, c2, c2, zero, t2, d2);
3a471103 2443 tcg_gen_qemu_st_i64(c1, clean_addr, memidx, MO_64 | s->be_data);
44ac14b0
RH
2444 tcg_gen_qemu_st_i64(c2, a2, memidx, MO_64 | s->be_data);
2445 tcg_temp_free_i64(a2);
2446 tcg_temp_free_i64(c1);
2447 tcg_temp_free_i64(c2);
2448 tcg_temp_free_i64(zero);
2449
2450 /* Write back the data from memory to Rs. */
2451 tcg_gen_mov_i64(s1, d1);
2452 tcg_gen_mov_i64(s2, d2);
2453 tcg_temp_free_i64(d1);
2454 tcg_temp_free_i64(d2);
2455 }
2456}
2457
aaa1f954
EI
2458/* Update the Sixty-Four bit (SF) registersize. This logic is derived
2459 * from the ARMv8 specs for LDR (Shared decode for all encodings).
2460 */
2461static bool disas_ldst_compute_iss_sf(int size, bool is_signed, int opc)
2462{
2463 int opc0 = extract32(opc, 0, 1);
2464 int regsize;
2465
2466 if (is_signed) {
2467 regsize = opc0 ? 32 : 64;
2468 } else {
2469 regsize = size == 3 ? 64 : 32;
2470 }
2471 return regsize == 64;
2472}
2473
4ce31af4 2474/* Load/store exclusive
fa2ef212
MM
2475 *
2476 * 31 30 29 24 23 22 21 20 16 15 14 10 9 5 4 0
2477 * +-----+-------------+----+---+----+------+----+-------+------+------+
2478 * | sz | 0 0 1 0 0 0 | o2 | L | o1 | Rs | o0 | Rt2 | Rn | Rt |
2479 * +-----+-------------+----+---+----+------+----+-------+------+------+
2480 *
2481 * sz: 00 -> 8 bit, 01 -> 16 bit, 10 -> 32 bit, 11 -> 64 bit
2482 * L: 0 -> store, 1 -> load
2483 * o2: 0 -> exclusive, 1 -> not
2484 * o1: 0 -> single register, 1 -> register pair
2485 * o0: 1 -> load-acquire/store-release, 0 -> not
fa2ef212 2486 */
ad7ee8a2
CF
2487static void disas_ldst_excl(DisasContext *s, uint32_t insn)
2488{
fa2ef212
MM
2489 int rt = extract32(insn, 0, 5);
2490 int rn = extract32(insn, 5, 5);
2491 int rt2 = extract32(insn, 10, 5);
fa2ef212 2492 int rs = extract32(insn, 16, 5);
68412d2e
RH
2493 int is_lasr = extract32(insn, 15, 1);
2494 int o2_L_o1_o0 = extract32(insn, 21, 3) * 2 | is_lasr;
fa2ef212 2495 int size = extract32(insn, 30, 2);
3a471103 2496 TCGv_i64 clean_addr;
fa2ef212 2497
68412d2e
RH
2498 switch (o2_L_o1_o0) {
2499 case 0x0: /* STXR */
2500 case 0x1: /* STLXR */
2501 if (rn == 31) {
2502 gen_check_sp_alignment(s);
2503 }
2504 if (is_lasr) {
2505 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2506 }
3a471103
RH
2507 clean_addr = clean_data_tbi(s, cpu_reg_sp(s, rn));
2508 gen_store_exclusive(s, rs, rt, rt2, clean_addr, size, false);
fa2ef212 2509 return;
fa2ef212 2510
68412d2e
RH
2511 case 0x4: /* LDXR */
2512 case 0x5: /* LDAXR */
2513 if (rn == 31) {
2514 gen_check_sp_alignment(s);
2515 }
3a471103 2516 clean_addr = clean_data_tbi(s, cpu_reg_sp(s, rn));
68412d2e 2517 s->is_ldex = true;
3a471103 2518 gen_load_exclusive(s, rt, rt2, clean_addr, size, false);
68412d2e
RH
2519 if (is_lasr) {
2520 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2521 }
2522 return;
fa2ef212 2523
2d7137c1
RH
2524 case 0x8: /* STLLR */
2525 if (!dc_isar_feature(aa64_lor, s)) {
2526 break;
2527 }
2528 /* StoreLORelease is the same as Store-Release for QEMU. */
2529 /* fall through */
68412d2e
RH
2530 case 0x9: /* STLR */
2531 /* Generate ISS for non-exclusive accesses including LASR. */
2532 if (rn == 31) {
2533 gen_check_sp_alignment(s);
2534 }
2535 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
3a471103
RH
2536 clean_addr = clean_data_tbi(s, cpu_reg_sp(s, rn));
2537 do_gpr_st(s, cpu_reg(s, rt), clean_addr, size, true, rt,
68412d2e
RH
2538 disas_ldst_compute_iss_sf(size, false, 0), is_lasr);
2539 return;
fa2ef212 2540
2d7137c1
RH
2541 case 0xc: /* LDLAR */
2542 if (!dc_isar_feature(aa64_lor, s)) {
2543 break;
2544 }
2545 /* LoadLOAcquire is the same as Load-Acquire for QEMU. */
2546 /* fall through */
68412d2e
RH
2547 case 0xd: /* LDAR */
2548 /* Generate ISS for non-exclusive accesses including LASR. */
2549 if (rn == 31) {
2550 gen_check_sp_alignment(s);
2551 }
3a471103
RH
2552 clean_addr = clean_data_tbi(s, cpu_reg_sp(s, rn));
2553 do_gpr_ld(s, cpu_reg(s, rt), clean_addr, size, false, false, true, rt,
68412d2e
RH
2554 disas_ldst_compute_iss_sf(size, false, 0), is_lasr);
2555 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2556 return;
2557
2558 case 0x2: case 0x3: /* CASP / STXP */
2559 if (size & 2) { /* STXP / STLXP */
2560 if (rn == 31) {
2561 gen_check_sp_alignment(s);
ce1bd93f 2562 }
ce1bd93f
PK
2563 if (is_lasr) {
2564 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
2565 }
3a471103
RH
2566 clean_addr = clean_data_tbi(s, cpu_reg_sp(s, rn));
2567 gen_store_exclusive(s, rs, rt, rt2, clean_addr, size, true);
68412d2e 2568 return;
fa2ef212 2569 }
44ac14b0
RH
2570 if (rt2 == 31
2571 && ((rt | rs) & 1) == 0
962fcbf2 2572 && dc_isar_feature(aa64_atomics, s)) {
44ac14b0
RH
2573 /* CASP / CASPL */
2574 gen_compare_and_swap_pair(s, rs, rt, rn, size | 2);
2575 return;
2576 }
68412d2e 2577 break;
aaa1f954 2578
44ac14b0 2579 case 0x6: case 0x7: /* CASPA / LDXP */
68412d2e
RH
2580 if (size & 2) { /* LDXP / LDAXP */
2581 if (rn == 31) {
2582 gen_check_sp_alignment(s);
ce1bd93f 2583 }
3a471103 2584 clean_addr = clean_data_tbi(s, cpu_reg_sp(s, rn));
68412d2e 2585 s->is_ldex = true;
3a471103 2586 gen_load_exclusive(s, rt, rt2, clean_addr, size, true);
ce1bd93f
PK
2587 if (is_lasr) {
2588 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
2589 }
68412d2e 2590 return;
fa2ef212 2591 }
44ac14b0
RH
2592 if (rt2 == 31
2593 && ((rt | rs) & 1) == 0
962fcbf2 2594 && dc_isar_feature(aa64_atomics, s)) {
44ac14b0
RH
2595 /* CASPA / CASPAL */
2596 gen_compare_and_swap_pair(s, rs, rt, rn, size | 2);
2597 return;
fa2ef212 2598 }
68412d2e
RH
2599 break;
2600
2601 case 0xa: /* CAS */
2602 case 0xb: /* CASL */
2603 case 0xe: /* CASA */
2604 case 0xf: /* CASAL */
962fcbf2 2605 if (rt2 == 31 && dc_isar_feature(aa64_atomics, s)) {
44ac14b0
RH
2606 gen_compare_and_swap(s, rs, rt, rn, size);
2607 return;
2608 }
68412d2e 2609 break;
fa2ef212 2610 }
68412d2e 2611 unallocated_encoding(s);
ad7ee8a2
CF
2612}
2613
32b64e86 2614/*
4ce31af4 2615 * Load register (literal)
32b64e86
AG
2616 *
2617 * 31 30 29 27 26 25 24 23 5 4 0
2618 * +-----+-------+---+-----+-------------------+-------+
2619 * | opc | 0 1 1 | V | 0 0 | imm19 | Rt |
2620 * +-----+-------+---+-----+-------------------+-------+
2621 *
2622 * V: 1 -> vector (simd/fp)
2623 * opc (non-vector): 00 -> 32 bit, 01 -> 64 bit,
2624 * 10-> 32 bit signed, 11 -> prefetch
2625 * opc (vector): 00 -> 32 bit, 01 -> 64 bit, 10 -> 128 bit (11 unallocated)
2626 */
ad7ee8a2
CF
2627static void disas_ld_lit(DisasContext *s, uint32_t insn)
2628{
32b64e86
AG
2629 int rt = extract32(insn, 0, 5);
2630 int64_t imm = sextract32(insn, 5, 19) << 2;
2631 bool is_vector = extract32(insn, 26, 1);
2632 int opc = extract32(insn, 30, 2);
2633 bool is_signed = false;
2634 int size = 2;
3a471103 2635 TCGv_i64 tcg_rt, clean_addr;
32b64e86
AG
2636
2637 if (is_vector) {
2638 if (opc == 3) {
2639 unallocated_encoding(s);
2640 return;
2641 }
2642 size = 2 + opc;
8c6afa6a
PM
2643 if (!fp_access_check(s)) {
2644 return;
2645 }
32b64e86
AG
2646 } else {
2647 if (opc == 3) {
2648 /* PRFM (literal) : prefetch */
2649 return;
2650 }
2651 size = 2 + extract32(opc, 0, 1);
2652 is_signed = extract32(opc, 1, 1);
2653 }
2654
2655 tcg_rt = cpu_reg(s, rt);
2656
43722a6d 2657 clean_addr = tcg_const_i64(s->pc_curr + imm);
32b64e86 2658 if (is_vector) {
3a471103 2659 do_fp_ld(s, rt, clean_addr, size);
32b64e86 2660 } else {
aaa1f954 2661 /* Only unsigned 32bit loads target 32bit registers. */
173ff585 2662 bool iss_sf = opc != 0;
aaa1f954 2663
3a471103 2664 do_gpr_ld(s, tcg_rt, clean_addr, size, is_signed, false,
aaa1f954 2665 true, rt, iss_sf, false);
32b64e86 2666 }
3a471103 2667 tcg_temp_free_i64(clean_addr);
ad7ee8a2
CF
2668}
2669
4a08d475 2670/*
4ce31af4
PM
2671 * LDNP (Load Pair - non-temporal hint)
2672 * LDP (Load Pair - non vector)
2673 * LDPSW (Load Pair Signed Word - non vector)
2674 * STNP (Store Pair - non-temporal hint)
2675 * STP (Store Pair - non vector)
2676 * LDNP (Load Pair of SIMD&FP - non-temporal hint)
2677 * LDP (Load Pair of SIMD&FP)
2678 * STNP (Store Pair of SIMD&FP - non-temporal hint)
2679 * STP (Store Pair of SIMD&FP)
4a08d475
PM
2680 *
2681 * 31 30 29 27 26 25 24 23 22 21 15 14 10 9 5 4 0
2682 * +-----+-------+---+---+-------+---+-----------------------------+
2683 * | opc | 1 0 1 | V | 0 | index | L | imm7 | Rt2 | Rn | Rt |
2684 * +-----+-------+---+---+-------+---+-------+-------+------+------+
2685 *
2686 * opc: LDP/STP/LDNP/STNP 00 -> 32 bit, 10 -> 64 bit
2687 * LDPSW 01
2688 * LDP/STP/LDNP/STNP (SIMD) 00 -> 32 bit, 01 -> 64 bit, 10 -> 128 bit
2689 * V: 0 -> GPR, 1 -> Vector
2690 * idx: 00 -> signed offset with non-temporal hint, 01 -> post-index,
2691 * 10 -> signed offset, 11 -> pre-index
2692 * L: 0 -> Store 1 -> Load
2693 *
2694 * Rt, Rt2 = GPR or SIMD registers to be stored
2695 * Rn = general purpose register containing address
2696 * imm7 = signed offset (multiple of 4 or 8 depending on size)
2697 */
ad7ee8a2
CF
2698static void disas_ldst_pair(DisasContext *s, uint32_t insn)
2699{
4a08d475
PM
2700 int rt = extract32(insn, 0, 5);
2701 int rn = extract32(insn, 5, 5);
2702 int rt2 = extract32(insn, 10, 5);
c2ebd862 2703 uint64_t offset = sextract64(insn, 15, 7);
4a08d475
PM
2704 int index = extract32(insn, 23, 2);
2705 bool is_vector = extract32(insn, 26, 1);
2706 bool is_load = extract32(insn, 22, 1);
2707 int opc = extract32(insn, 30, 2);
2708
2709 bool is_signed = false;
2710 bool postindex = false;
2711 bool wback = false;
2712
3a471103
RH
2713 TCGv_i64 clean_addr, dirty_addr;
2714
4a08d475
PM
2715 int size;
2716
2717 if (opc == 3) {
2718 unallocated_encoding(s);
2719 return;
2720 }
2721
2722 if (is_vector) {
2723 size = 2 + opc;
2724 } else {
2725 size = 2 + extract32(opc, 1, 1);
2726 is_signed = extract32(opc, 0, 1);
2727 if (!is_load && is_signed) {
2728 unallocated_encoding(s);
2729 return;
2730 }
2731 }
2732
2733 switch (index) {
2734 case 1: /* post-index */
2735 postindex = true;
2736 wback = true;
2737 break;
2738 case 0:
2739 /* signed offset with "non-temporal" hint. Since we don't emulate
2740 * caches we don't care about hints to the cache system about
2741 * data access patterns, and handle this identically to plain
2742 * signed offset.
2743 */
2744 if (is_signed) {
2745 /* There is no non-temporal-hint version of LDPSW */
2746 unallocated_encoding(s);
2747 return;
2748 }
2749 postindex = false;
2750 break;
2751 case 2: /* signed offset, rn not updated */
2752 postindex = false;
2753 break;
2754 case 3: /* pre-index */
2755 postindex = false;
2756 wback = true;
2757 break;
2758 }
2759
8c6afa6a
PM
2760 if (is_vector && !fp_access_check(s)) {
2761 return;
2762 }
2763
4a08d475
PM
2764 offset <<= size;
2765
2766 if (rn == 31) {
2767 gen_check_sp_alignment(s);
2768 }
2769
3a471103 2770 dirty_addr = read_cpu_reg_sp(s, rn, 1);
4a08d475 2771 if (!postindex) {
3a471103 2772 tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
4a08d475 2773 }
3a471103 2774 clean_addr = clean_data_tbi(s, dirty_addr);
4a08d475
PM
2775
2776 if (is_vector) {
2777 if (is_load) {
3a471103 2778 do_fp_ld(s, rt, clean_addr, size);
4a08d475 2779 } else {
3a471103 2780 do_fp_st(s, rt, clean_addr, size);
4a08d475 2781 }
3a471103 2782 tcg_gen_addi_i64(clean_addr, clean_addr, 1 << size);
4a08d475 2783 if (is_load) {
3a471103 2784 do_fp_ld(s, rt2, clean_addr, size);
4a08d475 2785 } else {
3a471103 2786 do_fp_st(s, rt2, clean_addr, size);
4a08d475
PM
2787 }
2788 } else {
3e4d91b9 2789 TCGv_i64 tcg_rt = cpu_reg(s, rt);
4a08d475 2790 TCGv_i64 tcg_rt2 = cpu_reg(s, rt2);
3e4d91b9 2791
4a08d475 2792 if (is_load) {
3e4d91b9
RH
2793 TCGv_i64 tmp = tcg_temp_new_i64();
2794
2795 /* Do not modify tcg_rt before recognizing any exception
2796 * from the second load.
2797 */
3a471103 2798 do_gpr_ld(s, tmp, clean_addr, size, is_signed, false,
3e4d91b9 2799 false, 0, false, false);
3a471103
RH
2800 tcg_gen_addi_i64(clean_addr, clean_addr, 1 << size);
2801 do_gpr_ld(s, tcg_rt2, clean_addr, size, is_signed, false,
aaa1f954 2802 false, 0, false, false);
3e4d91b9
RH
2803
2804 tcg_gen_mov_i64(tcg_rt, tmp);
2805 tcg_temp_free_i64(tmp);
4a08d475 2806 } else {
3a471103 2807 do_gpr_st(s, tcg_rt, clean_addr, size,
3e4d91b9 2808 false, 0, false, false);
3a471103
RH
2809 tcg_gen_addi_i64(clean_addr, clean_addr, 1 << size);
2810 do_gpr_st(s, tcg_rt2, clean_addr, size,
aaa1f954 2811 false, 0, false, false);
4a08d475
PM
2812 }
2813 }
2814
2815 if (wback) {
2816 if (postindex) {
3a471103 2817 tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
4a08d475 2818 }
3a471103 2819 tcg_gen_mov_i64(cpu_reg_sp(s, rn), dirty_addr);
4a08d475 2820 }
ad7ee8a2
CF
2821}
2822
a5e94a9d 2823/*
4ce31af4
PM
2824 * Load/store (immediate post-indexed)
2825 * Load/store (immediate pre-indexed)
2826 * Load/store (unscaled immediate)
a5e94a9d
AB
2827 *
2828 * 31 30 29 27 26 25 24 23 22 21 20 12 11 10 9 5 4 0
2829 * +----+-------+---+-----+-----+---+--------+-----+------+------+
2830 * |size| 1 1 1 | V | 0 0 | opc | 0 | imm9 | idx | Rn | Rt |
2831 * +----+-------+---+-----+-----+---+--------+-----+------+------+
2832 *
2833 * idx = 01 -> post-indexed, 11 pre-indexed, 00 unscaled imm. (no writeback)
60510aed 2834 10 -> unprivileged
a5e94a9d
AB
2835 * V = 0 -> non-vector
2836 * size: 00 -> 8 bit, 01 -> 16 bit, 10 -> 32 bit, 11 -> 64bit
2837 * opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
2838 */
cd694521
EI
2839static void disas_ldst_reg_imm9(DisasContext *s, uint32_t insn,
2840 int opc,
2841 int size,
2842 int rt,
2843 bool is_vector)
a5e94a9d 2844{
a5e94a9d
AB
2845 int rn = extract32(insn, 5, 5);
2846 int imm9 = sextract32(insn, 12, 9);
a5e94a9d
AB
2847 int idx = extract32(insn, 10, 2);
2848 bool is_signed = false;
2849 bool is_store = false;
2850 bool is_extended = false;
60510aed 2851 bool is_unpriv = (idx == 2);
aaa1f954 2852 bool iss_valid = !is_vector;
a5e94a9d
AB
2853 bool post_index;
2854 bool writeback;
2855
3a471103 2856 TCGv_i64 clean_addr, dirty_addr;
a5e94a9d
AB
2857
2858 if (is_vector) {
2859 size |= (opc & 2) << 1;
60510aed 2860 if (size > 4 || is_unpriv) {
a5e94a9d
AB
2861 unallocated_encoding(s);
2862 return;
2863 }
2864 is_store = ((opc & 1) == 0);
8c6afa6a
PM
2865 if (!fp_access_check(s)) {
2866 return;
2867 }
a5e94a9d
AB
2868 } else {
2869 if (size == 3 && opc == 2) {
2870 /* PRFM - prefetch */
a80c4256 2871 if (idx != 0) {
60510aed
PM
2872 unallocated_encoding(s);
2873 return;
2874 }
a5e94a9d
AB
2875 return;
2876 }
2877 if (opc == 3 && size > 1) {
2878 unallocated_encoding(s);
2879 return;
2880 }
2881 is_store = (opc == 0);
026a19c3
EI
2882 is_signed = extract32(opc, 1, 1);
2883 is_extended = (size < 3) && extract32(opc, 0, 1);
a5e94a9d
AB
2884 }
2885
2886 switch (idx) {
2887 case 0:
60510aed 2888 case 2:
a5e94a9d
AB
2889 post_index = false;
2890 writeback = false;
2891 break;
2892 case 1:
2893 post_index = true;
2894 writeback = true;
2895 break;
2896 case 3:
2897 post_index = false;
2898 writeback = true;
2899 break;
5ca66278
EC
2900 default:
2901 g_assert_not_reached();
a5e94a9d
AB
2902 }
2903
2904 if (rn == 31) {
2905 gen_check_sp_alignment(s);
2906 }
a5e94a9d 2907
3a471103 2908 dirty_addr = read_cpu_reg_sp(s, rn, 1);
a5e94a9d 2909 if (!post_index) {
3a471103 2910 tcg_gen_addi_i64(dirty_addr, dirty_addr, imm9);
a5e94a9d 2911 }
3a471103 2912 clean_addr = clean_data_tbi(s, dirty_addr);
a5e94a9d
AB
2913
2914 if (is_vector) {
2915 if (is_store) {
3a471103 2916 do_fp_st(s, rt, clean_addr, size);
a5e94a9d 2917 } else {
3a471103 2918 do_fp_ld(s, rt, clean_addr, size);
a5e94a9d
AB
2919 }
2920 } else {
2921 TCGv_i64 tcg_rt = cpu_reg(s, rt);
579d21cc 2922 int memidx = is_unpriv ? get_a64_user_mem_index(s) : get_mem_index(s);
aaa1f954 2923 bool iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
60510aed 2924
a5e94a9d 2925 if (is_store) {
3a471103 2926 do_gpr_st_memidx(s, tcg_rt, clean_addr, size, memidx,
aaa1f954 2927 iss_valid, rt, iss_sf, false);
a5e94a9d 2928 } else {
3a471103 2929 do_gpr_ld_memidx(s, tcg_rt, clean_addr, size,
aaa1f954
EI
2930 is_signed, is_extended, memidx,
2931 iss_valid, rt, iss_sf, false);
a5e94a9d
AB
2932 }
2933 }
2934
2935 if (writeback) {
2936 TCGv_i64 tcg_rn = cpu_reg_sp(s, rn);
2937 if (post_index) {
3a471103 2938 tcg_gen_addi_i64(dirty_addr, dirty_addr, imm9);
a5e94a9d 2939 }
3a471103 2940 tcg_gen_mov_i64(tcg_rn, dirty_addr);
a5e94a9d
AB
2941 }
2942}
2943
229b7a05 2944/*
4ce31af4 2945 * Load/store (register offset)
229b7a05
AB
2946 *
2947 * 31 30 29 27 26 25 24 23 22 21 20 16 15 13 12 11 10 9 5 4 0
2948 * +----+-------+---+-----+-----+---+------+-----+--+-----+----+----+
2949 * |size| 1 1 1 | V | 0 0 | opc | 1 | Rm | opt | S| 1 0 | Rn | Rt |
2950 * +----+-------+---+-----+-----+---+------+-----+--+-----+----+----+
2951 *
2952 * For non-vector:
2953 * size: 00-> byte, 01 -> 16 bit, 10 -> 32bit, 11 -> 64bit
2954 * opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
2955 * For vector:
2956 * size is opc<1>:size<1:0> so 100 -> 128 bit; 110 and 111 unallocated
2957 * opc<0>: 0 -> store, 1 -> load
2958 * V: 1 -> vector/simd
2959 * opt: extend encoding (see DecodeRegExtend)
2960 * S: if S=1 then scale (essentially index by sizeof(size))
2961 * Rt: register to transfer into/out of
2962 * Rn: address register or SP for base
2963 * Rm: offset register or ZR for offset
2964 */
cd694521
EI
2965static void disas_ldst_reg_roffset(DisasContext *s, uint32_t insn,
2966 int opc,
2967 int size,
2968 int rt,
2969 bool is_vector)
229b7a05 2970{
229b7a05
AB
2971 int rn = extract32(insn, 5, 5);
2972 int shift = extract32(insn, 12, 1);
2973 int rm = extract32(insn, 16, 5);
229b7a05 2974 int opt = extract32(insn, 13, 3);
229b7a05
AB
2975 bool is_signed = false;
2976 bool is_store = false;
2977 bool is_extended = false;
229b7a05 2978
3a471103 2979 TCGv_i64 tcg_rm, clean_addr, dirty_addr;
229b7a05
AB
2980
2981 if (extract32(opt, 1, 1) == 0) {
2982 unallocated_encoding(s);
2983 return;
2984 }
2985
2986 if (is_vector) {
2987 size |= (opc & 2) << 1;
2988 if (size > 4) {
2989 unallocated_encoding(s);
2990 return;
2991 }
2992 is_store = !extract32(opc, 0, 1);
8c6afa6a
PM
2993 if (!fp_access_check(s)) {
2994 return;
2995 }
229b7a05
AB
2996 } else {
2997 if (size == 3 && opc == 2) {
2998 /* PRFM - prefetch */
2999 return;
3000 }
3001 if (opc == 3 && size > 1) {
3002 unallocated_encoding(s);
3003 return;
3004 }
3005 is_store = (opc == 0);
3006 is_signed = extract32(opc, 1, 1);
3007 is_extended = (size < 3) && extract32(opc, 0, 1);
3008 }
3009
3010 if (rn == 31) {
3011 gen_check_sp_alignment(s);
3012 }
3a471103 3013 dirty_addr = read_cpu_reg_sp(s, rn, 1);
229b7a05
AB
3014
3015 tcg_rm = read_cpu_reg(s, rm, 1);
3016 ext_and_shift_reg(tcg_rm, tcg_rm, opt, shift ? size : 0);
3017
3a471103
RH
3018 tcg_gen_add_i64(dirty_addr, dirty_addr, tcg_rm);
3019 clean_addr = clean_data_tbi(s, dirty_addr);
229b7a05
AB
3020
3021 if (is_vector) {
3022 if (is_store) {
3a471103 3023 do_fp_st(s, rt, clean_addr, size);
229b7a05 3024 } else {
3a471103 3025 do_fp_ld(s, rt, clean_addr, size);
229b7a05
AB
3026 }
3027 } else {
3028 TCGv_i64 tcg_rt = cpu_reg(s, rt);
aaa1f954 3029 bool iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
229b7a05 3030 if (is_store) {
3a471103 3031 do_gpr_st(s, tcg_rt, clean_addr, size,
aaa1f954 3032 true, rt, iss_sf, false);
229b7a05 3033 } else {
3a471103 3034 do_gpr_ld(s, tcg_rt, clean_addr, size,
aaa1f954
EI
3035 is_signed, is_extended,
3036 true, rt, iss_sf, false);
229b7a05
AB
3037 }
3038 }
3039}
3040
d5612f10 3041/*
4ce31af4 3042 * Load/store (unsigned immediate)
d5612f10
AB
3043 *
3044 * 31 30 29 27 26 25 24 23 22 21 10 9 5
3045 * +----+-------+---+-----+-----+------------+-------+------+
3046 * |size| 1 1 1 | V | 0 1 | opc | imm12 | Rn | Rt |
3047 * +----+-------+---+-----+-----+------------+-------+------+
3048 *
3049 * For non-vector:
3050 * size: 00-> byte, 01 -> 16 bit, 10 -> 32bit, 11 -> 64bit
3051 * opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
3052 * For vector:
3053 * size is opc<1>:size<1:0> so 100 -> 128 bit; 110 and 111 unallocated
3054 * opc<0>: 0 -> store, 1 -> load
3055 * Rn: base address register (inc SP)
3056 * Rt: target register
3057 */
cd694521
EI
3058static void disas_ldst_reg_unsigned_imm(DisasContext *s, uint32_t insn,
3059 int opc,
3060 int size,
3061 int rt,
3062 bool is_vector)
d5612f10 3063{
d5612f10
AB
3064 int rn = extract32(insn, 5, 5);
3065 unsigned int imm12 = extract32(insn, 10, 12);
d5612f10
AB
3066 unsigned int offset;
3067
3a471103 3068 TCGv_i64 clean_addr, dirty_addr;
d5612f10
AB
3069
3070 bool is_store;
3071 bool is_signed = false;
3072 bool is_extended = false;
3073
3074 if (is_vector) {
3075 size |= (opc & 2) << 1;
3076 if (size > 4) {
3077 unallocated_encoding(s);
3078 return;
3079 }
3080 is_store = !extract32(opc, 0, 1);
8c6afa6a
PM
3081 if (!fp_access_check(s)) {
3082 return;
3083 }
d5612f10
AB
3084 } else {
3085 if (size == 3 && opc == 2) {
3086 /* PRFM - prefetch */
3087 return;
3088 }
3089 if (opc == 3 && size > 1) {
3090 unallocated_encoding(s);
3091 return;
3092 }
3093 is_store = (opc == 0);
3094 is_signed = extract32(opc, 1, 1);
3095 is_extended = (size < 3) && extract32(opc, 0, 1);
3096 }
3097
3098 if (rn == 31) {
3099 gen_check_sp_alignment(s);
3100 }
3a471103 3101 dirty_addr = read_cpu_reg_sp(s, rn, 1);
d5612f10 3102 offset = imm12 << size;
3a471103
RH
3103 tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3104 clean_addr = clean_data_tbi(s, dirty_addr);
d5612f10
AB
3105
3106 if (is_vector) {
3107 if (is_store) {
3a471103 3108 do_fp_st(s, rt, clean_addr, size);
d5612f10 3109 } else {
3a471103 3110 do_fp_ld(s, rt, clean_addr, size);
d5612f10
AB
3111 }
3112 } else {
3113 TCGv_i64 tcg_rt = cpu_reg(s, rt);
aaa1f954 3114 bool iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
d5612f10 3115 if (is_store) {
3a471103 3116 do_gpr_st(s, tcg_rt, clean_addr, size,
aaa1f954 3117 true, rt, iss_sf, false);
d5612f10 3118 } else {
3a471103 3119 do_gpr_ld(s, tcg_rt, clean_addr, size, is_signed, is_extended,
aaa1f954 3120 true, rt, iss_sf, false);
d5612f10
AB
3121 }
3122 }
3123}
3124
68412d2e
RH
3125/* Atomic memory operations
3126 *
3127 * 31 30 27 26 24 22 21 16 15 12 10 5 0
3128 * +------+-------+---+-----+-----+---+----+----+-----+-----+----+-----+
3129 * | size | 1 1 1 | V | 0 0 | A R | 1 | Rs | o3 | opc | 0 0 | Rn | Rt |
3130 * +------+-------+---+-----+-----+--------+----+-----+-----+----+-----+
3131 *
3132 * Rt: the result register
3133 * Rn: base address or SP
3134 * Rs: the source register for the operation
3135 * V: vector flag (always 0 as of v8.3)
3136 * A: acquire flag
3137 * R: release flag
3138 */
3139static void disas_ldst_atomic(DisasContext *s, uint32_t insn,
3140 int size, int rt, bool is_vector)
3141{
3142 int rs = extract32(insn, 16, 5);
3143 int rn = extract32(insn, 5, 5);
3144 int o3_opc = extract32(insn, 12, 4);
2677cf9f
PM
3145 bool r = extract32(insn, 22, 1);
3146 bool a = extract32(insn, 23, 1);
3a471103 3147 TCGv_i64 tcg_rs, clean_addr;
74608ea4 3148 AtomicThreeOpFn *fn;
68412d2e 3149
962fcbf2 3150 if (is_vector || !dc_isar_feature(aa64_atomics, s)) {
68412d2e
RH
3151 unallocated_encoding(s);
3152 return;
3153 }
3154 switch (o3_opc) {
3155 case 000: /* LDADD */
74608ea4
RH
3156 fn = tcg_gen_atomic_fetch_add_i64;
3157 break;
68412d2e 3158 case 001: /* LDCLR */
74608ea4
RH
3159 fn = tcg_gen_atomic_fetch_and_i64;
3160 break;
68412d2e 3161 case 002: /* LDEOR */
74608ea4
RH
3162 fn = tcg_gen_atomic_fetch_xor_i64;
3163 break;
68412d2e 3164 case 003: /* LDSET */
74608ea4
RH
3165 fn = tcg_gen_atomic_fetch_or_i64;
3166 break;
68412d2e 3167 case 004: /* LDSMAX */
74608ea4
RH
3168 fn = tcg_gen_atomic_fetch_smax_i64;
3169 break;
68412d2e 3170 case 005: /* LDSMIN */
74608ea4
RH
3171 fn = tcg_gen_atomic_fetch_smin_i64;
3172 break;
68412d2e 3173 case 006: /* LDUMAX */
74608ea4
RH
3174 fn = tcg_gen_atomic_fetch_umax_i64;
3175 break;
68412d2e 3176 case 007: /* LDUMIN */
74608ea4
RH
3177 fn = tcg_gen_atomic_fetch_umin_i64;
3178 break;
68412d2e 3179 case 010: /* SWP */
74608ea4
RH
3180 fn = tcg_gen_atomic_xchg_i64;
3181 break;
2677cf9f
PM
3182 case 014: /* LDAPR, LDAPRH, LDAPRB */
3183 if (!dc_isar_feature(aa64_rcpc_8_3, s) ||
3184 rs != 31 || a != 1 || r != 0) {
3185 unallocated_encoding(s);
3186 return;
3187 }
3188 break;
68412d2e
RH
3189 default:
3190 unallocated_encoding(s);
3191 return;
3192 }
68412d2e 3193
74608ea4
RH
3194 if (rn == 31) {
3195 gen_check_sp_alignment(s);
3196 }
3a471103 3197 clean_addr = clean_data_tbi(s, cpu_reg_sp(s, rn));
2677cf9f
PM
3198
3199 if (o3_opc == 014) {
3200 /*
3201 * LDAPR* are a special case because they are a simple load, not a
3202 * fetch-and-do-something op.
3203 * The architectural consistency requirements here are weaker than
3204 * full load-acquire (we only need "load-acquire processor consistent"),
3205 * but we choose to implement them as full LDAQ.
3206 */
3207 do_gpr_ld(s, cpu_reg(s, rt), clean_addr, size, false, false,
3208 true, rt, disas_ldst_compute_iss_sf(size, false, 0), true);
3209 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
3210 return;
3211 }
3212
74608ea4
RH
3213 tcg_rs = read_cpu_reg(s, rs, true);
3214
3215 if (o3_opc == 1) { /* LDCLR */
3216 tcg_gen_not_i64(tcg_rs, tcg_rs);
3217 }
3218
3219 /* The tcg atomic primitives are all full barriers. Therefore we
3220 * can ignore the Acquire and Release bits of this instruction.
3221 */
3a471103 3222 fn(cpu_reg(s, rt), clean_addr, tcg_rs, get_mem_index(s),
74608ea4 3223 s->be_data | size | MO_ALIGN);
68412d2e
RH
3224}
3225
bd889f48
RH
3226/*
3227 * PAC memory operations
3228 *
3229 * 31 30 27 26 24 22 21 12 11 10 5 0
3230 * +------+-------+---+-----+-----+---+--------+---+---+----+-----+
3231 * | size | 1 1 1 | V | 0 0 | M S | 1 | imm9 | W | 1 | Rn | Rt |
3232 * +------+-------+---+-----+-----+---+--------+---+---+----+-----+
3233 *
3234 * Rt: the result register
3235 * Rn: base address or SP
3236 * V: vector flag (always 0 as of v8.3)
3237 * M: clear for key DA, set for key DB
3238 * W: pre-indexing flag
3239 * S: sign for imm9.
3240 */
3241static void disas_ldst_pac(DisasContext *s, uint32_t insn,
3242 int size, int rt, bool is_vector)
3243{
3244 int rn = extract32(insn, 5, 5);
3245 bool is_wback = extract32(insn, 11, 1);
3246 bool use_key_a = !extract32(insn, 23, 1);
3247 int offset;
3a471103 3248 TCGv_i64 clean_addr, dirty_addr, tcg_rt;
bd889f48
RH
3249
3250 if (size != 3 || is_vector || !dc_isar_feature(aa64_pauth, s)) {
3251 unallocated_encoding(s);
3252 return;
3253 }
3254
3255 if (rn == 31) {
3256 gen_check_sp_alignment(s);
3257 }
3a471103 3258 dirty_addr = read_cpu_reg_sp(s, rn, 1);
bd889f48
RH
3259
3260 if (s->pauth_active) {
3261 if (use_key_a) {
3a471103 3262 gen_helper_autda(dirty_addr, cpu_env, dirty_addr, cpu_X[31]);
bd889f48 3263 } else {
3a471103 3264 gen_helper_autdb(dirty_addr, cpu_env, dirty_addr, cpu_X[31]);
bd889f48
RH
3265 }
3266 }
3267
3268 /* Form the 10-bit signed, scaled offset. */
3269 offset = (extract32(insn, 22, 1) << 9) | extract32(insn, 12, 9);
3270 offset = sextract32(offset << size, 0, 10 + size);
3a471103 3271 tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
bd889f48 3272
3a471103
RH
3273 /* Note that "clean" and "dirty" here refer to TBI not PAC. */
3274 clean_addr = clean_data_tbi(s, dirty_addr);
bd889f48 3275
3a471103
RH
3276 tcg_rt = cpu_reg(s, rt);
3277 do_gpr_ld(s, tcg_rt, clean_addr, size, /* is_signed */ false,
bd889f48
RH
3278 /* extend */ false, /* iss_valid */ !is_wback,
3279 /* iss_srt */ rt, /* iss_sf */ true, /* iss_ar */ false);
3280
3281 if (is_wback) {
3a471103 3282 tcg_gen_mov_i64(cpu_reg_sp(s, rn), dirty_addr);
bd889f48
RH
3283 }
3284}
3285
a1229109
PM
3286/*
3287 * LDAPR/STLR (unscaled immediate)
3288 *
3289 * 31 30 24 22 21 12 10 5 0
3290 * +------+-------------+-----+---+--------+-----+----+-----+
3291 * | size | 0 1 1 0 0 1 | opc | 0 | imm9 | 0 0 | Rn | Rt |
3292 * +------+-------------+-----+---+--------+-----+----+-----+
3293 *
3294 * Rt: source or destination register
3295 * Rn: base register
3296 * imm9: unscaled immediate offset
3297 * opc: 00: STLUR*, 01/10/11: various LDAPUR*
3298 * size: size of load/store
3299 */
3300static void disas_ldst_ldapr_stlr(DisasContext *s, uint32_t insn)
3301{
3302 int rt = extract32(insn, 0, 5);
3303 int rn = extract32(insn, 5, 5);
3304 int offset = sextract32(insn, 12, 9);
3305 int opc = extract32(insn, 22, 2);
3306 int size = extract32(insn, 30, 2);
3307 TCGv_i64 clean_addr, dirty_addr;
3308 bool is_store = false;
3309 bool is_signed = false;
3310 bool extend = false;
3311 bool iss_sf;
3312
3313 if (!dc_isar_feature(aa64_rcpc_8_4, s)) {
3314 unallocated_encoding(s);
3315 return;
3316 }
3317
3318 switch (opc) {
3319 case 0: /* STLURB */
3320 is_store = true;
3321 break;
3322 case 1: /* LDAPUR* */
3323 break;
3324 case 2: /* LDAPURS* 64-bit variant */
3325 if (size == 3) {
3326 unallocated_encoding(s);
3327 return;
3328 }
3329 is_signed = true;
3330 break;
3331 case 3: /* LDAPURS* 32-bit variant */
3332 if (size > 1) {
3333 unallocated_encoding(s);
3334 return;
3335 }
3336 is_signed = true;
3337 extend = true; /* zero-extend 32->64 after signed load */
3338 break;
3339 default:
3340 g_assert_not_reached();
3341 }
3342
3343 iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
3344
3345 if (rn == 31) {
3346 gen_check_sp_alignment(s);
3347 }
3348
3349 dirty_addr = read_cpu_reg_sp(s, rn, 1);
3350 tcg_gen_addi_i64(dirty_addr, dirty_addr, offset);
3351 clean_addr = clean_data_tbi(s, dirty_addr);
3352
3353 if (is_store) {
3354 /* Store-Release semantics */
3355 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
3356 do_gpr_st(s, cpu_reg(s, rt), clean_addr, size, true, rt, iss_sf, true);
3357 } else {
3358 /*
3359 * Load-AcquirePC semantics; we implement as the slightly more
3360 * restrictive Load-Acquire.
3361 */
3362 do_gpr_ld(s, cpu_reg(s, rt), clean_addr, size, is_signed, extend,
3363 true, rt, iss_sf, true);
3364 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
3365 }
3366}
3367
ad7ee8a2
CF
3368/* Load/store register (all forms) */
3369static void disas_ldst_reg(DisasContext *s, uint32_t insn)
3370{
cd694521
EI
3371 int rt = extract32(insn, 0, 5);
3372 int opc = extract32(insn, 22, 2);
3373 bool is_vector = extract32(insn, 26, 1);
3374 int size = extract32(insn, 30, 2);
3375
d5612f10
AB
3376 switch (extract32(insn, 24, 2)) {
3377 case 0:
68412d2e 3378 if (extract32(insn, 21, 1) == 0) {
60510aed
PM
3379 /* Load/store register (unscaled immediate)
3380 * Load/store immediate pre/post-indexed
3381 * Load/store register unprivileged
3382 */
cd694521 3383 disas_ldst_reg_imm9(s, insn, opc, size, rt, is_vector);
68412d2e
RH
3384 return;
3385 }
3386 switch (extract32(insn, 10, 2)) {
3387 case 0:
3388 disas_ldst_atomic(s, insn, size, rt, is_vector);
3389 return;
3390 case 2:
3391 disas_ldst_reg_roffset(s, insn, opc, size, rt, is_vector);
3392 return;
bd889f48
RH
3393 default:
3394 disas_ldst_pac(s, insn, size, rt, is_vector);
3395 return;
229b7a05 3396 }
d5612f10
AB
3397 break;
3398 case 1:
cd694521 3399 disas_ldst_reg_unsigned_imm(s, insn, opc, size, rt, is_vector);
68412d2e 3400 return;
d5612f10 3401 }
68412d2e 3402 unallocated_encoding(s);
ad7ee8a2
CF
3403}
3404
4ce31af4 3405/* AdvSIMD load/store multiple structures
72430bf5
AB
3406 *
3407 * 31 30 29 23 22 21 16 15 12 11 10 9 5 4 0
3408 * +---+---+---------------+---+-------------+--------+------+------+------+
3409 * | 0 | Q | 0 0 1 1 0 0 0 | L | 0 0 0 0 0 0 | opcode | size | Rn | Rt |
3410 * +---+---+---------------+---+-------------+--------+------+------+------+
3411 *
4ce31af4 3412 * AdvSIMD load/store multiple structures (post-indexed)
72430bf5
AB
3413 *
3414 * 31 30 29 23 22 21 20 16 15 12 11 10 9 5 4 0
3415 * +---+---+---------------+---+---+---------+--------+------+------+------+
3416 * | 0 | Q | 0 0 1 1 0 0 1 | L | 0 | Rm | opcode | size | Rn | Rt |
3417 * +---+---+---------------+---+---+---------+--------+------+------+------+
3418 *
3419 * Rt: first (or only) SIMD&FP register to be transferred
3420 * Rn: base address or SP
3421 * Rm (post-index only): post-index register (when !31) or size dependent #imm
3422 */
ad7ee8a2
CF
3423static void disas_ldst_multiple_struct(DisasContext *s, uint32_t insn)
3424{
72430bf5
AB
3425 int rt = extract32(insn, 0, 5);
3426 int rn = extract32(insn, 5, 5);
e1f22081 3427 int rm = extract32(insn, 16, 5);
72430bf5
AB
3428 int size = extract32(insn, 10, 2);
3429 int opcode = extract32(insn, 12, 4);
3430 bool is_store = !extract32(insn, 22, 1);
3431 bool is_postidx = extract32(insn, 23, 1);
3432 bool is_q = extract32(insn, 30, 1);
3a471103 3433 TCGv_i64 clean_addr, tcg_rn, tcg_ebytes;
14776ab5 3434 MemOp endian = s->be_data;
72430bf5 3435
87f9a7f0
RH
3436 int ebytes; /* bytes per element */
3437 int elements; /* elements per vector */
72430bf5
AB
3438 int rpt; /* num iterations */
3439 int selem; /* structure elements */
3440 int r;
3441
3442 if (extract32(insn, 31, 1) || extract32(insn, 21, 1)) {
3443 unallocated_encoding(s);
3444 return;
3445 }
3446
e1f22081
PM
3447 if (!is_postidx && rm != 0) {
3448 unallocated_encoding(s);
3449 return;
3450 }
3451
72430bf5
AB
3452 /* From the shared decode logic */
3453 switch (opcode) {
3454 case 0x0:
3455 rpt = 1;
3456 selem = 4;
3457 break;
3458 case 0x2:
3459 rpt = 4;
3460 selem = 1;
3461 break;
3462 case 0x4:
3463 rpt = 1;
3464 selem = 3;
3465 break;
3466 case 0x6:
3467 rpt = 3;
3468 selem = 1;
3469 break;
3470 case 0x7:
3471 rpt = 1;
3472 selem = 1;
3473 break;
3474 case 0x8:
3475 rpt = 1;
3476 selem = 2;
3477 break;
3478 case 0xa:
3479 rpt = 2;
3480 selem = 1;
3481 break;
3482 default:
3483 unallocated_encoding(s);
3484 return;
3485 }
3486
3487 if (size == 3 && !is_q && selem != 1) {
3488 /* reserved */
3489 unallocated_encoding(s);
3490 return;
3491 }
3492
8c6afa6a
PM
3493 if (!fp_access_check(s)) {
3494 return;
3495 }
3496
72430bf5
AB
3497 if (rn == 31) {
3498 gen_check_sp_alignment(s);
3499 }
3500
87f9a7f0
RH
3501 /* For our purposes, bytes are always little-endian. */
3502 if (size == 0) {
3503 endian = MO_LE;
3504 }
3505
3506 /* Consecutive little-endian elements from a single register
3507 * can be promoted to a larger little-endian operation.
3508 */
3509 if (selem == 1 && endian == MO_LE) {
3510 size = 3;
3511 }
3512 ebytes = 1 << size;
3513 elements = (is_q ? 16 : 8) / ebytes;
3514
72430bf5 3515 tcg_rn = cpu_reg_sp(s, rn);
3a471103 3516 clean_addr = clean_data_tbi(s, tcg_rn);
a7d8143a 3517 tcg_ebytes = tcg_const_i64(ebytes);
72430bf5
AB
3518
3519 for (r = 0; r < rpt; r++) {
3520 int e;
3521 for (e = 0; e < elements; e++) {
72430bf5
AB
3522 int xs;
3523 for (xs = 0; xs < selem; xs++) {
87f9a7f0 3524 int tt = (rt + r + xs) % 32;
72430bf5 3525 if (is_store) {
3a471103 3526 do_vec_st(s, tt, e, clean_addr, size, endian);
72430bf5 3527 } else {
3a471103 3528 do_vec_ld(s, tt, e, clean_addr, size, endian);
72430bf5 3529 }
3a471103 3530 tcg_gen_add_i64(clean_addr, clean_addr, tcg_ebytes);
72430bf5
AB
3531 }
3532 }
3533 }
3a471103 3534 tcg_temp_free_i64(tcg_ebytes);
72430bf5 3535
87f9a7f0
RH
3536 if (!is_store) {
3537 /* For non-quad operations, setting a slice of the low
3538 * 64 bits of the register clears the high 64 bits (in
3539 * the ARM ARM pseudocode this is implicit in the fact
3540 * that 'rval' is a 64 bit wide variable).
3541 * For quad operations, we might still need to zero the
3542 * high bits of SVE.
3543 */
3544 for (r = 0; r < rpt * selem; r++) {
3545 int tt = (rt + r) % 32;
3546 clear_vec_high(s, is_q, tt);
3547 }
3548 }
3549
72430bf5 3550 if (is_postidx) {
72430bf5 3551 if (rm == 31) {
3a471103 3552 tcg_gen_addi_i64(tcg_rn, tcg_rn, rpt * elements * selem * ebytes);
72430bf5
AB
3553 } else {
3554 tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, rm));
3555 }
3556 }
ad7ee8a2
CF
3557}
3558
4ce31af4 3559/* AdvSIMD load/store single structure
df54e47d
PM
3560 *
3561 * 31 30 29 23 22 21 20 16 15 13 12 11 10 9 5 4 0
3562 * +---+---+---------------+-----+-----------+-----+---+------+------+------+
3563 * | 0 | Q | 0 0 1 1 0 1 0 | L R | 0 0 0 0 0 | opc | S | size | Rn | Rt |
3564 * +---+---+---------------+-----+-----------+-----+---+------+------+------+
3565 *
4ce31af4 3566 * AdvSIMD load/store single structure (post-indexed)
df54e47d
PM
3567 *
3568 * 31 30 29 23 22 21 20 16 15 13 12 11 10 9 5 4 0
3569 * +---+---+---------------+-----+-----------+-----+---+------+------+------+
3570 * | 0 | Q | 0 0 1 1 0 1 1 | L R | Rm | opc | S | size | Rn | Rt |
3571 * +---+---+---------------+-----+-----------+-----+---+------+------+------+
3572 *
3573 * Rt: first (or only) SIMD&FP register to be transferred
3574 * Rn: base address or SP
3575 * Rm (post-index only): post-index register (when !31) or size dependent #imm
3576 * index = encoded in Q:S:size dependent on size
3577 *
3578 * lane_size = encoded in R, opc
3579 * transfer width = encoded in opc, S, size
3580 */
ad7ee8a2
CF
3581static void disas_ldst_single_struct(DisasContext *s, uint32_t insn)
3582{
df54e47d
PM
3583 int rt = extract32(insn, 0, 5);
3584 int rn = extract32(insn, 5, 5);
9c72b68a 3585 int rm = extract32(insn, 16, 5);
df54e47d
PM
3586 int size = extract32(insn, 10, 2);
3587 int S = extract32(insn, 12, 1);
3588 int opc = extract32(insn, 13, 3);
3589 int R = extract32(insn, 21, 1);
3590 int is_load = extract32(insn, 22, 1);
3591 int is_postidx = extract32(insn, 23, 1);
3592 int is_q = extract32(insn, 30, 1);
3593
3594 int scale = extract32(opc, 1, 2);
3595 int selem = (extract32(opc, 0, 1) << 1 | R) + 1;
3596 bool replicate = false;
3597 int index = is_q << 3 | S << 2 | size;
3598 int ebytes, xs;
3a471103 3599 TCGv_i64 clean_addr, tcg_rn, tcg_ebytes;
df54e47d 3600
9c72b68a
PM
3601 if (extract32(insn, 31, 1)) {
3602 unallocated_encoding(s);
3603 return;
3604 }
3605 if (!is_postidx && rm != 0) {
3606 unallocated_encoding(s);
3607 return;
3608 }
3609
df54e47d
PM
3610 switch (scale) {
3611 case 3:
3612 if (!is_load || S) {
3613 unallocated_encoding(s);
3614 return;
3615 }
3616 scale = size;
3617 replicate = true;
3618 break;
3619 case 0:
3620 break;
3621 case 1:
3622 if (extract32(size, 0, 1)) {
3623 unallocated_encoding(s);
3624 return;
3625 }
3626 index >>= 1;
3627 break;
3628 case 2:
3629 if (extract32(size, 1, 1)) {
3630 unallocated_encoding(s);
3631 return;
3632 }
3633 if (!extract32(size, 0, 1)) {
3634 index >>= 2;
3635 } else {
3636 if (S) {
3637 unallocated_encoding(s);
3638 return;
3639 }
3640 index >>= 3;
3641 scale = 3;
3642 }
3643 break;
3644 default:
3645 g_assert_not_reached();
3646 }
3647
8c6afa6a
PM
3648 if (!fp_access_check(s)) {
3649 return;
3650 }
3651
df54e47d
PM
3652 ebytes = 1 << scale;
3653
3654 if (rn == 31) {
3655 gen_check_sp_alignment(s);
3656 }
3657
3658 tcg_rn = cpu_reg_sp(s, rn);
3a471103 3659 clean_addr = clean_data_tbi(s, tcg_rn);
a7d8143a 3660 tcg_ebytes = tcg_const_i64(ebytes);
df54e47d
PM
3661
3662 for (xs = 0; xs < selem; xs++) {
3663 if (replicate) {
3664 /* Load and replicate to all elements */
df54e47d
PM
3665 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
3666
3a471103 3667 tcg_gen_qemu_ld_i64(tcg_tmp, clean_addr,
aa6489da 3668 get_mem_index(s), s->be_data + scale);
10e0b33c
RH
3669 tcg_gen_gvec_dup_i64(scale, vec_full_reg_offset(s, rt),
3670 (is_q + 1) * 8, vec_full_reg_size(s),
3671 tcg_tmp);
df54e47d
PM
3672 tcg_temp_free_i64(tcg_tmp);
3673 } else {
3674 /* Load/store one element per register */
3675 if (is_load) {
3a471103 3676 do_vec_ld(s, rt, index, clean_addr, scale, s->be_data);
df54e47d 3677 } else {
3a471103 3678 do_vec_st(s, rt, index, clean_addr, scale, s->be_data);
df54e47d
PM
3679 }
3680 }
3a471103 3681 tcg_gen_add_i64(clean_addr, clean_addr, tcg_ebytes);
df54e47d
PM
3682 rt = (rt + 1) % 32;
3683 }
3a471103 3684 tcg_temp_free_i64(tcg_ebytes);
df54e47d
PM
3685
3686 if (is_postidx) {
df54e47d 3687 if (rm == 31) {
3a471103 3688 tcg_gen_addi_i64(tcg_rn, tcg_rn, selem * ebytes);
df54e47d
PM
3689 } else {
3690 tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, rm));
3691 }
3692 }
ad7ee8a2
CF
3693}
3694
4ce31af4 3695/* Loads and stores */
ad7ee8a2
CF
3696static void disas_ldst(DisasContext *s, uint32_t insn)
3697{
3698 switch (extract32(insn, 24, 6)) {
3699 case 0x08: /* Load/store exclusive */
3700 disas_ldst_excl(s, insn);
3701 break;
3702 case 0x18: case 0x1c: /* Load register (literal) */
3703 disas_ld_lit(s, insn);
3704 break;
3705 case 0x28: case 0x29:
3706 case 0x2c: case 0x2d: /* Load/store pair (all forms) */
3707 disas_ldst_pair(s, insn);
3708 break;
3709 case 0x38: case 0x39:
3710 case 0x3c: case 0x3d: /* Load/store register (all forms) */
3711 disas_ldst_reg(s, insn);
3712 break;
3713 case 0x0c: /* AdvSIMD load/store multiple structures */
3714 disas_ldst_multiple_struct(s, insn);
3715 break;
3716 case 0x0d: /* AdvSIMD load/store single structure */
3717 disas_ldst_single_struct(s, insn);
3718 break;
a1229109
PM
3719 case 0x19: /* LDAPR/STLR (unscaled immediate) */
3720 if (extract32(insn, 10, 2) != 0 ||
3721 extract32(insn, 21, 1) != 0) {
3722 unallocated_encoding(s);
3723 break;
3724 }
3725 disas_ldst_ldapr_stlr(s, insn);
3726 break;
ad7ee8a2
CF
3727 default:
3728 unallocated_encoding(s);
3729 break;
3730 }
3731}
3732
4ce31af4 3733/* PC-rel. addressing
15bfe8b6
AG
3734 * 31 30 29 28 24 23 5 4 0
3735 * +----+-------+-----------+-------------------+------+
3736 * | op | immlo | 1 0 0 0 0 | immhi | Rd |
3737 * +----+-------+-----------+-------------------+------+
3738 */
ad7ee8a2
CF
3739static void disas_pc_rel_adr(DisasContext *s, uint32_t insn)
3740{
15bfe8b6
AG
3741 unsigned int page, rd;
3742 uint64_t base;
037e1d00 3743 uint64_t offset;
15bfe8b6
AG
3744
3745 page = extract32(insn, 31, 1);
3746 /* SignExtend(immhi:immlo) -> offset */
037e1d00
PM
3747 offset = sextract64(insn, 5, 19);
3748 offset = offset << 2 | extract32(insn, 29, 2);
15bfe8b6 3749 rd = extract32(insn, 0, 5);
43722a6d 3750 base = s->pc_curr;
15bfe8b6
AG
3751
3752 if (page) {
3753 /* ADRP (page based) */
3754 base &= ~0xfff;
3755 offset <<= 12;
3756 }
3757
3758 tcg_gen_movi_i64(cpu_reg(s, rd), base + offset);
ad7ee8a2
CF
3759}
3760
b0ff21b4 3761/*
4ce31af4 3762 * Add/subtract (immediate)
b0ff21b4
AB
3763 *
3764 * 31 30 29 28 24 23 22 21 10 9 5 4 0
3765 * +--+--+--+-----------+-----+-------------+-----+-----+
3766 * |sf|op| S| 1 0 0 0 1 |shift| imm12 | Rn | Rd |
3767 * +--+--+--+-----------+-----+-------------+-----+-----+
3768 *
3769 * sf: 0 -> 32bit, 1 -> 64bit
3770 * op: 0 -> add , 1 -> sub
3771 * S: 1 -> set flags
3772 * shift: 00 -> LSL imm by 0, 01 -> LSL imm by 12
3773 */
ad7ee8a2
CF
3774static void disas_add_sub_imm(DisasContext *s, uint32_t insn)
3775{
b0ff21b4
AB
3776 int rd = extract32(insn, 0, 5);
3777 int rn = extract32(insn, 5, 5);
3778 uint64_t imm = extract32(insn, 10, 12);
3779 int shift = extract32(insn, 22, 2);
3780 bool setflags = extract32(insn, 29, 1);
3781 bool sub_op = extract32(insn, 30, 1);
3782 bool is_64bit = extract32(insn, 31, 1);
3783
3784 TCGv_i64 tcg_rn = cpu_reg_sp(s, rn);
3785 TCGv_i64 tcg_rd = setflags ? cpu_reg(s, rd) : cpu_reg_sp(s, rd);
3786 TCGv_i64 tcg_result;
3787
3788 switch (shift) {
3789 case 0x0:
3790 break;
3791 case 0x1:
3792 imm <<= 12;
3793 break;
3794 default:
3795 unallocated_encoding(s);
3796 return;
3797 }
3798
3799 tcg_result = tcg_temp_new_i64();
3800 if (!setflags) {
3801 if (sub_op) {
3802 tcg_gen_subi_i64(tcg_result, tcg_rn, imm);
3803 } else {
3804 tcg_gen_addi_i64(tcg_result, tcg_rn, imm);
3805 }
3806 } else {
3807 TCGv_i64 tcg_imm = tcg_const_i64(imm);
3808 if (sub_op) {
3809 gen_sub_CC(is_64bit, tcg_result, tcg_rn, tcg_imm);
3810 } else {
3811 gen_add_CC(is_64bit, tcg_result, tcg_rn, tcg_imm);
3812 }
3813 tcg_temp_free_i64(tcg_imm);
3814 }
3815
3816 if (is_64bit) {
3817 tcg_gen_mov_i64(tcg_rd, tcg_result);
3818 } else {
3819 tcg_gen_ext32u_i64(tcg_rd, tcg_result);
3820 }
3821
3822 tcg_temp_free_i64(tcg_result);
ad7ee8a2
CF
3823}
3824
71b46089
AG
3825/* The input should be a value in the bottom e bits (with higher
3826 * bits zero); returns that value replicated into every element
3827 * of size e in a 64 bit integer.
3828 */
3829static uint64_t bitfield_replicate(uint64_t mask, unsigned int e)
3830{
3831 assert(e != 0);
3832 while (e < 64) {
3833 mask |= mask << e;
3834 e *= 2;
3835 }
3836 return mask;
3837}
3838
3839/* Return a value with the bottom len bits set (where 0 < len <= 64) */
3840static inline uint64_t bitmask64(unsigned int length)
3841{
3842 assert(length > 0 && length <= 64);
3843 return ~0ULL >> (64 - length);
3844}
3845
3846/* Simplified variant of pseudocode DecodeBitMasks() for the case where we
3847 * only require the wmask. Returns false if the imms/immr/immn are a reserved
3848 * value (ie should cause a guest UNDEF exception), and true if they are
3849 * valid, in which case the decoded bit pattern is written to result.
3850 */
8c71baed
RH
3851bool logic_imm_decode_wmask(uint64_t *result, unsigned int immn,
3852 unsigned int imms, unsigned int immr)
71b46089
AG
3853{
3854 uint64_t mask;
3855 unsigned e, levels, s, r;
3856 int len;
3857
3858 assert(immn < 2 && imms < 64 && immr < 64);
3859
3860 /* The bit patterns we create here are 64 bit patterns which
3861 * are vectors of identical elements of size e = 2, 4, 8, 16, 32 or
3862 * 64 bits each. Each element contains the same value: a run
3863 * of between 1 and e-1 non-zero bits, rotated within the
3864 * element by between 0 and e-1 bits.
3865 *
3866 * The element size and run length are encoded into immn (1 bit)
3867 * and imms (6 bits) as follows:
3868 * 64 bit elements: immn = 1, imms = <length of run - 1>
3869 * 32 bit elements: immn = 0, imms = 0 : <length of run - 1>
3870 * 16 bit elements: immn = 0, imms = 10 : <length of run - 1>
3871 * 8 bit elements: immn = 0, imms = 110 : <length of run - 1>
3872 * 4 bit elements: immn = 0, imms = 1110 : <length of run - 1>
3873 * 2 bit elements: immn = 0, imms = 11110 : <length of run - 1>
3874 * Notice that immn = 0, imms = 11111x is the only combination
3875 * not covered by one of the above options; this is reserved.
3876 * Further, <length of run - 1> all-ones is a reserved pattern.
3877 *
3878 * In all cases the rotation is by immr % e (and immr is 6 bits).
3879 */
3880
3881 /* First determine the element size */
3882 len = 31 - clz32((immn << 6) | (~imms & 0x3f));
3883 if (len < 1) {
3884 /* This is the immn == 0, imms == 0x11111x case */
3885 return false;
3886 }
3887 e = 1 << len;
3888
3889 levels = e - 1;
3890 s = imms & levels;
3891 r = immr & levels;
3892
3893 if (s == levels) {
3894 /* <length of run - 1> mustn't be all-ones. */
3895 return false;
3896 }
3897
3898 /* Create the value of one element: s+1 set bits rotated
3899 * by r within the element (which is e bits wide)...
3900 */
3901 mask = bitmask64(s + 1);
e167adc9
PM
3902 if (r) {
3903 mask = (mask >> r) | (mask << (e - r));
3904 mask &= bitmask64(e);
3905 }
71b46089
AG
3906 /* ...then replicate the element over the whole 64 bit value */
3907 mask = bitfield_replicate(mask, e);
3908 *result = mask;
3909 return true;
3910}
3911
4ce31af4 3912/* Logical (immediate)
71b46089
AG
3913 * 31 30 29 28 23 22 21 16 15 10 9 5 4 0
3914 * +----+-----+-------------+---+------+------+------+------+
3915 * | sf | opc | 1 0 0 1 0 0 | N | immr | imms | Rn | Rd |
3916 * +----+-----+-------------+---+------+------+------+------+
3917 */
ad7ee8a2
CF
3918static void disas_logic_imm(DisasContext *s, uint32_t insn)
3919{
71b46089
AG
3920 unsigned int sf, opc, is_n, immr, imms, rn, rd;
3921 TCGv_i64 tcg_rd, tcg_rn;
3922 uint64_t wmask;
3923 bool is_and = false;
3924
3925 sf = extract32(insn, 31, 1);
3926 opc = extract32(insn, 29, 2);
3927 is_n = extract32(insn, 22, 1);
3928 immr = extract32(insn, 16, 6);
3929 imms = extract32(insn, 10, 6);
3930 rn = extract32(insn, 5, 5);
3931 rd = extract32(insn, 0, 5);
3932
3933 if (!sf && is_n) {
3934 unallocated_encoding(s);
3935 return;
3936 }
3937
3938 if (opc == 0x3) { /* ANDS */
3939 tcg_rd = cpu_reg(s, rd);
3940 } else {
3941 tcg_rd = cpu_reg_sp(s, rd);
3942 }
3943 tcg_rn = cpu_reg(s, rn);
3944
3945 if (!logic_imm_decode_wmask(&wmask, is_n, imms, immr)) {
3946 /* some immediate field values are reserved */
3947 unallocated_encoding(s);
3948 return;
3949 }
3950
3951 if (!sf) {
3952 wmask &= 0xffffffff;
3953 }
3954
3955 switch (opc) {
3956 case 0x3: /* ANDS */
3957 case 0x0: /* AND */
3958 tcg_gen_andi_i64(tcg_rd, tcg_rn, wmask);
3959 is_and = true;
3960 break;
3961 case 0x1: /* ORR */
3962 tcg_gen_ori_i64(tcg_rd, tcg_rn, wmask);
3963 break;
3964 case 0x2: /* EOR */
3965 tcg_gen_xori_i64(tcg_rd, tcg_rn, wmask);
3966 break;
3967 default:
3968 assert(FALSE); /* must handle all above */
3969 break;
3970 }
3971
3972 if (!sf && !is_and) {
3973 /* zero extend final result; we know we can skip this for AND
3974 * since the immediate had the high 32 bits clear.
3975 */
3976 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3977 }
3978
3979 if (opc == 3) { /* ANDS */
3980 gen_logic_CC(sf, tcg_rd);
3981 }
ad7ee8a2
CF
3982}
3983
ed6ec679 3984/*
4ce31af4 3985 * Move wide (immediate)
ed6ec679
AB
3986 *
3987 * 31 30 29 28 23 22 21 20 5 4 0
3988 * +--+-----+-------------+-----+----------------+------+
3989 * |sf| opc | 1 0 0 1 0 1 | hw | imm16 | Rd |
3990 * +--+-----+-------------+-----+----------------+------+
3991 *
3992 * sf: 0 -> 32 bit, 1 -> 64 bit
3993 * opc: 00 -> N, 10 -> Z, 11 -> K
3994 * hw: shift/16 (0,16, and sf only 32, 48)
3995 */
ad7ee8a2
CF
3996static void disas_movw_imm(DisasContext *s, uint32_t insn)
3997{
ed6ec679
AB
3998 int rd = extract32(insn, 0, 5);
3999 uint64_t imm = extract32(insn, 5, 16);
4000 int sf = extract32(insn, 31, 1);
4001 int opc = extract32(insn, 29, 2);
4002 int pos = extract32(insn, 21, 2) << 4;
4003 TCGv_i64 tcg_rd = cpu_reg(s, rd);
4004 TCGv_i64 tcg_imm;
4005
4006 if (!sf && (pos >= 32)) {
4007 unallocated_encoding(s);
4008 return;
4009 }
4010
4011 switch (opc) {
4012 case 0: /* MOVN */
4013 case 2: /* MOVZ */
4014 imm <<= pos;
4015 if (opc == 0) {
4016 imm = ~imm;
4017 }
4018 if (!sf) {
4019 imm &= 0xffffffffu;
4020 }
4021 tcg_gen_movi_i64(tcg_rd, imm);
4022 break;
4023 case 3: /* MOVK */
4024 tcg_imm = tcg_const_i64(imm);
4025 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_imm, pos, 16);
4026 tcg_temp_free_i64(tcg_imm);
4027 if (!sf) {
4028 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4029 }
4030 break;
4031 default:
4032 unallocated_encoding(s);
4033 break;
4034 }
ad7ee8a2
CF
4035}
4036
4ce31af4 4037/* Bitfield
88077742
CF
4038 * 31 30 29 28 23 22 21 16 15 10 9 5 4 0
4039 * +----+-----+-------------+---+------+------+------+------+
4040 * | sf | opc | 1 0 0 1 1 0 | N | immr | imms | Rn | Rd |
4041 * +----+-----+-------------+---+------+------+------+------+
4042 */
ad7ee8a2
CF
4043static void disas_bitfield(DisasContext *s, uint32_t insn)
4044{
88077742
CF
4045 unsigned int sf, n, opc, ri, si, rn, rd, bitsize, pos, len;
4046 TCGv_i64 tcg_rd, tcg_tmp;
4047
4048 sf = extract32(insn, 31, 1);
4049 opc = extract32(insn, 29, 2);
4050 n = extract32(insn, 22, 1);
4051 ri = extract32(insn, 16, 6);
4052 si = extract32(insn, 10, 6);
4053 rn = extract32(insn, 5, 5);
4054 rd = extract32(insn, 0, 5);
4055 bitsize = sf ? 64 : 32;
4056
4057 if (sf != n || ri >= bitsize || si >= bitsize || opc > 2) {
4058 unallocated_encoding(s);
4059 return;
4060 }
4061
4062 tcg_rd = cpu_reg(s, rd);
d3a77b42
RH
4063
4064 /* Suppress the zero-extend for !sf. Since RI and SI are constrained
4065 to be smaller than bitsize, we'll never reference data outside the
4066 low 32-bits anyway. */
4067 tcg_tmp = read_cpu_reg(s, rn, 1);
88077742 4068
59a71b4c 4069 /* Recognize simple(r) extractions. */
86c9ab27 4070 if (si >= ri) {
59a71b4c
RH
4071 /* Wd<s-r:0> = Wn<s:r> */
4072 len = (si - ri) + 1;
4073 if (opc == 0) { /* SBFM: ASR, SBFX, SXTB, SXTH, SXTW */
4074 tcg_gen_sextract_i64(tcg_rd, tcg_tmp, ri, len);
ef60151b 4075 goto done;
59a71b4c
RH
4076 } else if (opc == 2) { /* UBFM: UBFX, LSR, UXTB, UXTH */
4077 tcg_gen_extract_i64(tcg_rd, tcg_tmp, ri, len);
9924e858
RH
4078 return;
4079 }
87eb65a3
RH
4080 /* opc == 1, BFXIL fall through to deposit */
4081 tcg_gen_shri_i64(tcg_tmp, tcg_tmp, ri);
88077742 4082 pos = 0;
88077742 4083 } else {
59a71b4c
RH
4084 /* Handle the ri > si case with a deposit
4085 * Wd<32+s-r,32-r> = Wn<s:0>
4086 */
88077742 4087 len = si + 1;
59a71b4c 4088 pos = (bitsize - ri) & (bitsize - 1);
88077742
CF
4089 }
4090
59a71b4c
RH
4091 if (opc == 0 && len < ri) {
4092 /* SBFM: sign extend the destination field from len to fill
4093 the balance of the word. Let the deposit below insert all
4094 of those sign bits. */
4095 tcg_gen_sextract_i64(tcg_tmp, tcg_tmp, 0, len);
4096 len = ri;
4097 }
88077742 4098
87eb65a3 4099 if (opc == 1) { /* BFM, BFXIL */
59a71b4c
RH
4100 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, pos, len);
4101 } else {
4102 /* SBFM or UBFM: We start with zero, and we haven't modified
4103 any bits outside bitsize, therefore the zero-extension
4104 below is unneeded. */
4105 tcg_gen_deposit_z_i64(tcg_rd, tcg_tmp, pos, len);
4106 return;
88077742
CF
4107 }
4108
ef60151b 4109 done:
88077742
CF
4110 if (!sf) { /* zero extend final result */
4111 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4112 }
ad7ee8a2
CF
4113}
4114
4ce31af4 4115/* Extract
e801de93
AG
4116 * 31 30 29 28 23 22 21 20 16 15 10 9 5 4 0
4117 * +----+------+-------------+---+----+------+--------+------+------+
4118 * | sf | op21 | 1 0 0 1 1 1 | N | o0 | Rm | imms | Rn | Rd |
4119 * +----+------+-------------+---+----+------+--------+------+------+
4120 */
ad7ee8a2
CF
4121static void disas_extract(DisasContext *s, uint32_t insn)
4122{
e801de93
AG
4123 unsigned int sf, n, rm, imm, rn, rd, bitsize, op21, op0;
4124
4125 sf = extract32(insn, 31, 1);
4126 n = extract32(insn, 22, 1);
4127 rm = extract32(insn, 16, 5);
4128 imm = extract32(insn, 10, 6);
4129 rn = extract32(insn, 5, 5);
4130 rd = extract32(insn, 0, 5);
4131 op21 = extract32(insn, 29, 2);
4132 op0 = extract32(insn, 21, 1);
4133 bitsize = sf ? 64 : 32;
4134
4135 if (sf != n || op21 || op0 || imm >= bitsize) {
4136 unallocated_encoding(s);
4137 } else {
4138 TCGv_i64 tcg_rd, tcg_rm, tcg_rn;
4139
4140 tcg_rd = cpu_reg(s, rd);
4141
8fb0ad8e 4142 if (unlikely(imm == 0)) {
e801de93
AG
4143 /* tcg shl_i32/shl_i64 is undefined for 32/64 bit shifts,
4144 * so an extract from bit 0 is a special case.
4145 */
4146 if (sf) {
4147 tcg_gen_mov_i64(tcg_rd, cpu_reg(s, rm));
4148 } else {
4149 tcg_gen_ext32u_i64(tcg_rd, cpu_reg(s, rm));
4150 }
80ac954c 4151 } else {
8fb0ad8e 4152 tcg_rm = cpu_reg(s, rm);
80ac954c
RH
4153 tcg_rn = cpu_reg(s, rn);
4154
8fb0ad8e 4155 if (sf) {
80ac954c
RH
4156 /* Specialization to ROR happens in EXTRACT2. */
4157 tcg_gen_extract2_i64(tcg_rd, tcg_rm, tcg_rn, imm);
8fb0ad8e 4158 } else {
80ac954c
RH
4159 TCGv_i32 t0 = tcg_temp_new_i32();
4160
4161 tcg_gen_extrl_i64_i32(t0, tcg_rm);
4162 if (rm == rn) {
4163 tcg_gen_rotri_i32(t0, t0, imm);
4164 } else {
4165 TCGv_i32 t1 = tcg_temp_new_i32();
4166 tcg_gen_extrl_i64_i32(t1, tcg_rn);
4167 tcg_gen_extract2_i32(t0, t0, t1, imm);
4168 tcg_temp_free_i32(t1);
4169 }
4170 tcg_gen_extu_i32_i64(tcg_rd, t0);
4171 tcg_temp_free_i32(t0);
8fb0ad8e 4172 }
e801de93 4173 }
e801de93 4174 }
ad7ee8a2
CF
4175}
4176
4ce31af4 4177/* Data processing - immediate */
ad7ee8a2
CF
4178static void disas_data_proc_imm(DisasContext *s, uint32_t insn)
4179{
4180 switch (extract32(insn, 23, 6)) {
4181 case 0x20: case 0x21: /* PC-rel. addressing */
4182 disas_pc_rel_adr(s, insn);
4183 break;
4184 case 0x22: case 0x23: /* Add/subtract (immediate) */
4185 disas_add_sub_imm(s, insn);
4186 break;
4187 case 0x24: /* Logical (immediate) */
4188 disas_logic_imm(s, insn);
4189 break;
4190 case 0x25: /* Move wide (immediate) */
4191 disas_movw_imm(s, insn);
4192 break;
4193 case 0x26: /* Bitfield */
4194 disas_bitfield(s, insn);
4195 break;
4196 case 0x27: /* Extract */
4197 disas_extract(s, insn);
4198 break;
4199 default:
4200 unallocated_encoding(s);
4201 break;
4202 }
4203}
4204
832ffa1c
AG
4205/* Shift a TCGv src by TCGv shift_amount, put result in dst.
4206 * Note that it is the caller's responsibility to ensure that the
4207 * shift amount is in range (ie 0..31 or 0..63) and provide the ARM
4208 * mandated semantics for out of range shifts.
4209 */
4210static void shift_reg(TCGv_i64 dst, TCGv_i64 src, int sf,
4211 enum a64_shift_type shift_type, TCGv_i64 shift_amount)
4212{
4213 switch (shift_type) {
4214 case A64_SHIFT_TYPE_LSL:
4215 tcg_gen_shl_i64(dst, src, shift_amount);
4216 break;
4217 case A64_SHIFT_TYPE_LSR:
4218 tcg_gen_shr_i64(dst, src, shift_amount);
4219 break;
4220 case A64_SHIFT_TYPE_ASR:
4221 if (!sf) {
4222 tcg_gen_ext32s_i64(dst, src);
4223 }
4224 tcg_gen_sar_i64(dst, sf ? src : dst, shift_amount);
4225 break;
4226 case A64_SHIFT_TYPE_ROR:
4227 if (sf) {
4228 tcg_gen_rotr_i64(dst, src, shift_amount);
4229 } else {
4230 TCGv_i32 t0, t1;
4231 t0 = tcg_temp_new_i32();
4232 t1 = tcg_temp_new_i32();
ecc7b3aa
RH
4233 tcg_gen_extrl_i64_i32(t0, src);
4234 tcg_gen_extrl_i64_i32(t1, shift_amount);
832ffa1c
AG
4235 tcg_gen_rotr_i32(t0, t0, t1);
4236 tcg_gen_extu_i32_i64(dst, t0);
4237 tcg_temp_free_i32(t0);
4238 tcg_temp_free_i32(t1);
4239 }
4240 break;
4241 default:
4242 assert(FALSE); /* all shift types should be handled */
4243 break;
4244 }
4245
4246 if (!sf) { /* zero extend final result */
4247 tcg_gen_ext32u_i64(dst, dst);
4248 }
4249}
4250
4251/* Shift a TCGv src by immediate, put result in dst.
4252 * The shift amount must be in range (this should always be true as the
4253 * relevant instructions will UNDEF on bad shift immediates).
4254 */
4255static void shift_reg_imm(TCGv_i64 dst, TCGv_i64 src, int sf,
4256 enum a64_shift_type shift_type, unsigned int shift_i)
4257{
4258 assert(shift_i < (sf ? 64 : 32));
4259
4260 if (shift_i == 0) {
4261 tcg_gen_mov_i64(dst, src);
4262 } else {
4263 TCGv_i64 shift_const;
4264
4265 shift_const = tcg_const_i64(shift_i);
4266 shift_reg(dst, src, sf, shift_type, shift_const);
4267 tcg_temp_free_i64(shift_const);
4268 }
4269}
4270
4ce31af4 4271/* Logical (shifted register)
832ffa1c
AG
4272 * 31 30 29 28 24 23 22 21 20 16 15 10 9 5 4 0
4273 * +----+-----+-----------+-------+---+------+--------+------+------+
4274 * | sf | opc | 0 1 0 1 0 | shift | N | Rm | imm6 | Rn | Rd |
4275 * +----+-----+-----------+-------+---+------+--------+------+------+
4276 */
ad7ee8a2
CF
4277static void disas_logic_reg(DisasContext *s, uint32_t insn)
4278{
832ffa1c
AG
4279 TCGv_i64 tcg_rd, tcg_rn, tcg_rm;
4280 unsigned int sf, opc, shift_type, invert, rm, shift_amount, rn, rd;
4281
4282 sf = extract32(insn, 31, 1);
4283 opc = extract32(insn, 29, 2);
4284 shift_type = extract32(insn, 22, 2);
4285 invert = extract32(insn, 21, 1);
4286 rm = extract32(insn, 16, 5);
4287 shift_amount = extract32(insn, 10, 6);
4288 rn = extract32(insn, 5, 5);
4289 rd = extract32(insn, 0, 5);
4290
4291 if (!sf && (shift_amount & (1 << 5))) {
4292 unallocated_encoding(s);
4293 return;
4294 }
4295
4296 tcg_rd = cpu_reg(s, rd);
4297
4298 if (opc == 1 && shift_amount == 0 && shift_type == 0 && rn == 31) {
4299 /* Unshifted ORR and ORN with WZR/XZR is the standard encoding for
4300 * register-register MOV and MVN, so it is worth special casing.
4301 */
4302 tcg_rm = cpu_reg(s, rm);
4303 if (invert) {
4304 tcg_gen_not_i64(tcg_rd, tcg_rm);
4305 if (!sf) {
4306 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4307 }
4308 } else {
4309 if (sf) {
4310 tcg_gen_mov_i64(tcg_rd, tcg_rm);
4311 } else {
4312 tcg_gen_ext32u_i64(tcg_rd, tcg_rm);
4313 }
4314 }
4315 return;
4316 }
4317
4318 tcg_rm = read_cpu_reg(s, rm, sf);
4319
4320 if (shift_amount) {
4321 shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, shift_amount);
4322 }
4323
4324 tcg_rn = cpu_reg(s, rn);
4325
4326 switch (opc | (invert << 2)) {
4327 case 0: /* AND */
4328 case 3: /* ANDS */
4329 tcg_gen_and_i64(tcg_rd, tcg_rn, tcg_rm);
4330 break;
4331 case 1: /* ORR */
4332 tcg_gen_or_i64(tcg_rd, tcg_rn, tcg_rm);
4333 break;
4334 case 2: /* EOR */
4335 tcg_gen_xor_i64(tcg_rd, tcg_rn, tcg_rm);
4336 break;
4337 case 4: /* BIC */
4338 case 7: /* BICS */
4339 tcg_gen_andc_i64(tcg_rd, tcg_rn, tcg_rm);
4340 break;
4341 case 5: /* ORN */
4342 tcg_gen_orc_i64(tcg_rd, tcg_rn, tcg_rm);
4343 break;
4344 case 6: /* EON */
4345 tcg_gen_eqv_i64(tcg_rd, tcg_rn, tcg_rm);
4346 break;
4347 default:
4348 assert(FALSE);
4349 break;
4350 }
4351
4352 if (!sf) {
4353 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4354 }
4355
4356 if (opc == 3) {
4357 gen_logic_CC(sf, tcg_rd);
4358 }
ad7ee8a2
CF
4359}
4360
b0ff21b4 4361/*
4ce31af4 4362 * Add/subtract (extended register)
b0ff21b4
AB
4363 *
4364 * 31|30|29|28 24|23 22|21|20 16|15 13|12 10|9 5|4 0|
4365 * +--+--+--+-----------+-----+--+-------+------+------+----+----+
4366 * |sf|op| S| 0 1 0 1 1 | opt | 1| Rm |option| imm3 | Rn | Rd |
4367 * +--+--+--+-----------+-----+--+-------+------+------+----+----+
4368 *
4369 * sf: 0 -> 32bit, 1 -> 64bit
4370 * op: 0 -> add , 1 -> sub
4371 * S: 1 -> set flags
4372 * opt: 00
4373 * option: extension type (see DecodeRegExtend)
4374 * imm3: optional shift to Rm
4375 *
4376 * Rd = Rn + LSL(extend(Rm), amount)
4377 */
ad7ee8a2
CF
4378static void disas_add_sub_ext_reg(DisasContext *s, uint32_t insn)
4379{
b0ff21b4
AB
4380 int rd = extract32(insn, 0, 5);
4381 int rn = extract32(insn, 5, 5);
4382 int imm3 = extract32(insn, 10, 3);
4383 int option = extract32(insn, 13, 3);
4384 int rm = extract32(insn, 16, 5);
4f611066 4385 int opt = extract32(insn, 22, 2);
b0ff21b4
AB
4386 bool setflags = extract32(insn, 29, 1);
4387 bool sub_op = extract32(insn, 30, 1);
4388 bool sf = extract32(insn, 31, 1);
4389
4390 TCGv_i64 tcg_rm, tcg_rn; /* temps */
4391 TCGv_i64 tcg_rd;
4392 TCGv_i64 tcg_result;
4393
4f611066 4394 if (imm3 > 4 || opt != 0) {
b0ff21b4
AB
4395 unallocated_encoding(s);
4396 return;
4397 }
4398
4399 /* non-flag setting ops may use SP */
4400 if (!setflags) {
b0ff21b4
AB
4401 tcg_rd = cpu_reg_sp(s, rd);
4402 } else {
b0ff21b4
AB
4403 tcg_rd = cpu_reg(s, rd);
4404 }
cf4ab1af 4405 tcg_rn = read_cpu_reg_sp(s, rn, sf);
b0ff21b4
AB
4406
4407 tcg_rm = read_cpu_reg(s, rm, sf);
4408 ext_and_shift_reg(tcg_rm, tcg_rm, option, imm3);
4409
4410 tcg_result = tcg_temp_new_i64();
4411
4412 if (!setflags) {
4413 if (sub_op) {
4414 tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
4415 } else {
4416 tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
4417 }
4418 } else {
4419 if (sub_op) {
4420 gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
4421 } else {
4422 gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
4423 }
4424 }
4425
4426 if (sf) {
4427 tcg_gen_mov_i64(tcg_rd, tcg_result);
4428 } else {
4429 tcg_gen_ext32u_i64(tcg_rd, tcg_result);
4430 }
4431
4432 tcg_temp_free_i64(tcg_result);
ad7ee8a2
CF
4433}
4434
b0ff21b4 4435/*
4ce31af4 4436 * Add/subtract (shifted register)
b0ff21b4
AB
4437 *
4438 * 31 30 29 28 24 23 22 21 20 16 15 10 9 5 4 0
4439 * +--+--+--+-----------+-----+--+-------+---------+------+------+
4440 * |sf|op| S| 0 1 0 1 1 |shift| 0| Rm | imm6 | Rn | Rd |
4441 * +--+--+--+-----------+-----+--+-------+---------+------+------+
4442 *
4443 * sf: 0 -> 32bit, 1 -> 64bit
4444 * op: 0 -> add , 1 -> sub
4445 * S: 1 -> set flags
4446 * shift: 00 -> LSL, 01 -> LSR, 10 -> ASR, 11 -> RESERVED
4447 * imm6: Shift amount to apply to Rm before the add/sub
4448 */
ad7ee8a2
CF
4449static void disas_add_sub_reg(DisasContext *s, uint32_t insn)
4450{
b0ff21b4
AB
4451 int rd = extract32(insn, 0, 5);
4452 int rn = extract32(insn, 5, 5);
4453 int imm6 = extract32(insn, 10, 6);
4454 int rm = extract32(insn, 16, 5);
4455 int shift_type = extract32(insn, 22, 2);
4456 bool setflags = extract32(insn, 29, 1);
4457 bool sub_op = extract32(insn, 30, 1);
4458 bool sf = extract32(insn, 31, 1);
4459
4460 TCGv_i64 tcg_rd = cpu_reg(s, rd);
4461 TCGv_i64 tcg_rn, tcg_rm;
4462 TCGv_i64 tcg_result;
4463
4464 if ((shift_type == 3) || (!sf && (imm6 > 31))) {
4465 unallocated_encoding(s);
4466 return;
4467 }
4468
4469 tcg_rn = read_cpu_reg(s, rn, sf);
4470 tcg_rm = read_cpu_reg(s, rm, sf);
4471
4472 shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, imm6);
4473
4474 tcg_result = tcg_temp_new_i64();
4475
4476 if (!setflags) {
4477 if (sub_op) {
4478 tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
4479 } else {
4480 tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
4481 }
4482 } else {
4483 if (sub_op) {
4484 gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
4485 } else {
4486 gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
4487 }
4488 }
4489
4490 if (sf) {
4491 tcg_gen_mov_i64(tcg_rd, tcg_result);
4492 } else {
4493 tcg_gen_ext32u_i64(tcg_rd, tcg_result);
4494 }
4495
4496 tcg_temp_free_i64(tcg_result);
ad7ee8a2
CF
4497}
4498
4ce31af4
PM
4499/* Data-processing (3 source)
4500 *
4501 * 31 30 29 28 24 23 21 20 16 15 14 10 9 5 4 0
4502 * +--+------+-----------+------+------+----+------+------+------+
4503 * |sf| op54 | 1 1 0 1 1 | op31 | Rm | o0 | Ra | Rn | Rd |
4504 * +--+------+-----------+------+------+----+------+------+------+
52c8b9af 4505 */
ad7ee8a2
CF
4506static void disas_data_proc_3src(DisasContext *s, uint32_t insn)
4507{
52c8b9af
AG
4508 int rd = extract32(insn, 0, 5);
4509 int rn = extract32(insn, 5, 5);
4510 int ra = extract32(insn, 10, 5);
4511 int rm = extract32(insn, 16, 5);
4512 int op_id = (extract32(insn, 29, 3) << 4) |
4513 (extract32(insn, 21, 3) << 1) |
4514 extract32(insn, 15, 1);
4515 bool sf = extract32(insn, 31, 1);
4516 bool is_sub = extract32(op_id, 0, 1);
4517 bool is_high = extract32(op_id, 2, 1);
4518 bool is_signed = false;
4519 TCGv_i64 tcg_op1;
4520 TCGv_i64 tcg_op2;
4521 TCGv_i64 tcg_tmp;
4522
4523 /* Note that op_id is sf:op54:op31:o0 so it includes the 32/64 size flag */
4524 switch (op_id) {
4525 case 0x42: /* SMADDL */
4526 case 0x43: /* SMSUBL */
4527 case 0x44: /* SMULH */
4528 is_signed = true;
4529 break;
4530 case 0x0: /* MADD (32bit) */
4531 case 0x1: /* MSUB (32bit) */
4532 case 0x40: /* MADD (64bit) */
4533 case 0x41: /* MSUB (64bit) */
4534 case 0x4a: /* UMADDL */
4535 case 0x4b: /* UMSUBL */
4536 case 0x4c: /* UMULH */
4537 break;
4538 default:
4539 unallocated_encoding(s);
4540 return;
4541 }
4542
4543 if (is_high) {
4544 TCGv_i64 low_bits = tcg_temp_new_i64(); /* low bits discarded */
4545 TCGv_i64 tcg_rd = cpu_reg(s, rd);
4546 TCGv_i64 tcg_rn = cpu_reg(s, rn);
4547 TCGv_i64 tcg_rm = cpu_reg(s, rm);
4548
4549 if (is_signed) {
4550 tcg_gen_muls2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
4551 } else {
4552 tcg_gen_mulu2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
4553 }
4554
4555 tcg_temp_free_i64(low_bits);
4556 return;
4557 }
4558
4559 tcg_op1 = tcg_temp_new_i64();
4560 tcg_op2 = tcg_temp_new_i64();
4561 tcg_tmp = tcg_temp_new_i64();
4562
4563 if (op_id < 0x42) {
4564 tcg_gen_mov_i64(tcg_op1, cpu_reg(s, rn));
4565 tcg_gen_mov_i64(tcg_op2, cpu_reg(s, rm));
4566 } else {
4567 if (is_signed) {
4568 tcg_gen_ext32s_i64(tcg_op1, cpu_reg(s, rn));
4569 tcg_gen_ext32s_i64(tcg_op2, cpu_reg(s, rm));
4570 } else {
4571 tcg_gen_ext32u_i64(tcg_op1, cpu_reg(s, rn));
4572 tcg_gen_ext32u_i64(tcg_op2, cpu_reg(s, rm));
4573 }
4574 }
4575
4576 if (ra == 31 && !is_sub) {
4577 /* Special-case MADD with rA == XZR; it is the standard MUL alias */
4578 tcg_gen_mul_i64(cpu_reg(s, rd), tcg_op1, tcg_op2);
4579 } else {
4580 tcg_gen_mul_i64(tcg_tmp, tcg_op1, tcg_op2);
4581 if (is_sub) {
4582 tcg_gen_sub_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
4583 } else {
4584 tcg_gen_add_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
4585 }
4586 }
4587
4588 if (!sf) {
4589 tcg_gen_ext32u_i64(cpu_reg(s, rd), cpu_reg(s, rd));
4590 }
4591
4592 tcg_temp_free_i64(tcg_op1);
4593 tcg_temp_free_i64(tcg_op2);
4594 tcg_temp_free_i64(tcg_tmp);
ad7ee8a2
CF
4595}
4596
4ce31af4 4597/* Add/subtract (with carry)
2fba34f7
RH
4598 * 31 30 29 28 27 26 25 24 23 22 21 20 16 15 10 9 5 4 0
4599 * +--+--+--+------------------------+------+-------------+------+-----+
4600 * |sf|op| S| 1 1 0 1 0 0 0 0 | rm | 0 0 0 0 0 0 | Rn | Rd |
4601 * +--+--+--+------------------------+------+-------------+------+-----+
643dbb07
CF
4602 */
4603
ad7ee8a2
CF
4604static void disas_adc_sbc(DisasContext *s, uint32_t insn)
4605{
643dbb07
CF
4606 unsigned int sf, op, setflags, rm, rn, rd;
4607 TCGv_i64 tcg_y, tcg_rn, tcg_rd;
4608
643dbb07
CF
4609 sf = extract32(insn, 31, 1);
4610 op = extract32(insn, 30, 1);
4611 setflags = extract32(insn, 29, 1);
4612 rm = extract32(insn, 16, 5);
4613 rn = extract32(insn, 5, 5);
4614 rd = extract32(insn, 0, 5);
4615
4616 tcg_rd = cpu_reg(s, rd);
4617 tcg_rn = cpu_reg(s, rn);
4618
4619 if (op) {
4620 tcg_y = new_tmp_a64(s);
4621 tcg_gen_not_i64(tcg_y, cpu_reg(s, rm));
4622 } else {
4623 tcg_y = cpu_reg(s, rm);
4624 }
4625
4626 if (setflags) {
4627 gen_adc_CC(sf, tcg_rd, tcg_rn, tcg_y);
4628 } else {
4629 gen_adc(sf, tcg_rd, tcg_rn, tcg_y);
4630 }
ad7ee8a2
CF
4631}
4632
b89d9c98
RH
4633/*
4634 * Rotate right into flags
4635 * 31 30 29 21 15 10 5 4 0
4636 * +--+--+--+-----------------+--------+-----------+------+--+------+
4637 * |sf|op| S| 1 1 0 1 0 0 0 0 | imm6 | 0 0 0 0 1 | Rn |o2| mask |
4638 * +--+--+--+-----------------+--------+-----------+------+--+------+
4639 */
4640static void disas_rotate_right_into_flags(DisasContext *s, uint32_t insn)
4641{
4642 int mask = extract32(insn, 0, 4);
4643 int o2 = extract32(insn, 4, 1);
4644 int rn = extract32(insn, 5, 5);
4645 int imm6 = extract32(insn, 15, 6);
4646 int sf_op_s = extract32(insn, 29, 3);
4647 TCGv_i64 tcg_rn;
4648 TCGv_i32 nzcv;
4649
4650 if (sf_op_s != 5 || o2 != 0 || !dc_isar_feature(aa64_condm_4, s)) {
4651 unallocated_encoding(s);
4652 return;
4653 }
4654
4655 tcg_rn = read_cpu_reg(s, rn, 1);
4656 tcg_gen_rotri_i64(tcg_rn, tcg_rn, imm6);
4657
4658 nzcv = tcg_temp_new_i32();
4659 tcg_gen_extrl_i64_i32(nzcv, tcg_rn);
4660
4661 if (mask & 8) { /* N */
4662 tcg_gen_shli_i32(cpu_NF, nzcv, 31 - 3);
4663 }
4664 if (mask & 4) { /* Z */
4665 tcg_gen_not_i32(cpu_ZF, nzcv);
4666 tcg_gen_andi_i32(cpu_ZF, cpu_ZF, 4);
4667 }
4668 if (mask & 2) { /* C */
4669 tcg_gen_extract_i32(cpu_CF, nzcv, 1, 1);
4670 }
4671 if (mask & 1) { /* V */
4672 tcg_gen_shli_i32(cpu_VF, nzcv, 31 - 0);
4673 }
4674
4675 tcg_temp_free_i32(nzcv);
4676}
4677
4678/*
4679 * Evaluate into flags
4680 * 31 30 29 21 15 14 10 5 4 0
4681 * +--+--+--+-----------------+---------+----+---------+------+--+------+
4682 * |sf|op| S| 1 1 0 1 0 0 0 0 | opcode2 | sz | 0 0 1 0 | Rn |o3| mask |
4683 * +--+--+--+-----------------+---------+----+---------+------+--+------+
4684 */
4685static void disas_evaluate_into_flags(DisasContext *s, uint32_t insn)
4686{
4687 int o3_mask = extract32(insn, 0, 5);
4688 int rn = extract32(insn, 5, 5);
4689 int o2 = extract32(insn, 15, 6);
4690 int sz = extract32(insn, 14, 1);
4691 int sf_op_s = extract32(insn, 29, 3);
4692 TCGv_i32 tmp;
4693 int shift;
4694
4695 if (sf_op_s != 1 || o2 != 0 || o3_mask != 0xd ||
4696 !dc_isar_feature(aa64_condm_4, s)) {
4697 unallocated_encoding(s);
4698 return;
4699 }
4700 shift = sz ? 16 : 24; /* SETF16 or SETF8 */
4701
4702 tmp = tcg_temp_new_i32();
4703 tcg_gen_extrl_i64_i32(tmp, cpu_reg(s, rn));
4704 tcg_gen_shli_i32(cpu_NF, tmp, shift);
4705 tcg_gen_shli_i32(cpu_VF, tmp, shift - 1);
4706 tcg_gen_mov_i32(cpu_ZF, cpu_NF);
4707 tcg_gen_xor_i32(cpu_VF, cpu_VF, cpu_NF);
4708 tcg_temp_free_i32(tmp);
4709}
4710
4ce31af4 4711/* Conditional compare (immediate / register)
750813cf
CF
4712 * 31 30 29 28 27 26 25 24 23 22 21 20 16 15 12 11 10 9 5 4 3 0
4713 * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
4714 * |sf|op| S| 1 1 0 1 0 0 1 0 |imm5/rm | cond |i/r |o2| Rn |o3|nzcv |
4715 * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
4716 * [1] y [0] [0]
4717 */
4718static void disas_cc(DisasContext *s, uint32_t insn)
ad7ee8a2 4719{
750813cf 4720 unsigned int sf, op, y, cond, rn, nzcv, is_imm;
7dd03d77 4721 TCGv_i32 tcg_t0, tcg_t1, tcg_t2;
750813cf 4722 TCGv_i64 tcg_tmp, tcg_y, tcg_rn;
7dd03d77 4723 DisasCompare c;
ad7ee8a2 4724
750813cf
CF
4725 if (!extract32(insn, 29, 1)) {
4726 unallocated_encoding(s);
4727 return;
4728 }
4729 if (insn & (1 << 10 | 1 << 4)) {
4730 unallocated_encoding(s);
4731 return;
4732 }
4733 sf = extract32(insn, 31, 1);
4734 op = extract32(insn, 30, 1);
4735 is_imm = extract32(insn, 11, 1);
4736 y = extract32(insn, 16, 5); /* y = rm (reg) or imm5 (imm) */
4737 cond = extract32(insn, 12, 4);
4738 rn = extract32(insn, 5, 5);
4739 nzcv = extract32(insn, 0, 4);
4740
7dd03d77
RH
4741 /* Set T0 = !COND. */
4742 tcg_t0 = tcg_temp_new_i32();
4743 arm_test_cc(&c, cond);
4744 tcg_gen_setcondi_i32(tcg_invert_cond(c.cond), tcg_t0, c.value, 0);
4745 arm_free_cc(&c);
4746
4747 /* Load the arguments for the new comparison. */
750813cf
CF
4748 if (is_imm) {
4749 tcg_y = new_tmp_a64(s);
4750 tcg_gen_movi_i64(tcg_y, y);
4751 } else {
4752 tcg_y = cpu_reg(s, y);
4753 }
4754 tcg_rn = cpu_reg(s, rn);
4755
7dd03d77 4756 /* Set the flags for the new comparison. */
750813cf
CF
4757 tcg_tmp = tcg_temp_new_i64();
4758 if (op) {
4759 gen_sub_CC(sf, tcg_tmp, tcg_rn, tcg_y);
4760 } else {
4761 gen_add_CC(sf, tcg_tmp, tcg_rn, tcg_y);
4762 }
4763 tcg_temp_free_i64(tcg_tmp);
4764
7dd03d77
RH
4765 /* If COND was false, force the flags to #nzcv. Compute two masks
4766 * to help with this: T1 = (COND ? 0 : -1), T2 = (COND ? -1 : 0).
4767 * For tcg hosts that support ANDC, we can make do with just T1.
4768 * In either case, allow the tcg optimizer to delete any unused mask.
4769 */
4770 tcg_t1 = tcg_temp_new_i32();
4771 tcg_t2 = tcg_temp_new_i32();
4772 tcg_gen_neg_i32(tcg_t1, tcg_t0);
4773 tcg_gen_subi_i32(tcg_t2, tcg_t0, 1);
4774
4775 if (nzcv & 8) { /* N */
4776 tcg_gen_or_i32(cpu_NF, cpu_NF, tcg_t1);
4777 } else {
4778 if (TCG_TARGET_HAS_andc_i32) {
4779 tcg_gen_andc_i32(cpu_NF, cpu_NF, tcg_t1);
4780 } else {
4781 tcg_gen_and_i32(cpu_NF, cpu_NF, tcg_t2);
4782 }
4783 }
4784 if (nzcv & 4) { /* Z */
4785 if (TCG_TARGET_HAS_andc_i32) {
4786 tcg_gen_andc_i32(cpu_ZF, cpu_ZF, tcg_t1);
4787 } else {
4788 tcg_gen_and_i32(cpu_ZF, cpu_ZF, tcg_t2);
4789 }
4790 } else {
4791 tcg_gen_or_i32(cpu_ZF, cpu_ZF, tcg_t0);
4792 }
4793 if (nzcv & 2) { /* C */
4794 tcg_gen_or_i32(cpu_CF, cpu_CF, tcg_t0);
4795 } else {
4796 if (TCG_TARGET_HAS_andc_i32) {
4797 tcg_gen_andc_i32(cpu_CF, cpu_CF, tcg_t1);
4798 } else {
4799 tcg_gen_and_i32(cpu_CF, cpu_CF, tcg_t2);
4800 }
4801 }
4802 if (nzcv & 1) { /* V */
4803 tcg_gen_or_i32(cpu_VF, cpu_VF, tcg_t1);
4804 } else {
4805 if (TCG_TARGET_HAS_andc_i32) {
4806 tcg_gen_andc_i32(cpu_VF, cpu_VF, tcg_t1);
4807 } else {
4808 tcg_gen_and_i32(cpu_VF, cpu_VF, tcg_t2);
4809 }
750813cf 4810 }
7dd03d77
RH
4811 tcg_temp_free_i32(tcg_t0);
4812 tcg_temp_free_i32(tcg_t1);
4813 tcg_temp_free_i32(tcg_t2);
ad7ee8a2
CF
4814}
4815
4ce31af4 4816/* Conditional select
e952d8c7
CF
4817 * 31 30 29 28 21 20 16 15 12 11 10 9 5 4 0
4818 * +----+----+---+-----------------+------+------+-----+------+------+
4819 * | sf | op | S | 1 1 0 1 0 1 0 0 | Rm | cond | op2 | Rn | Rd |
4820 * +----+----+---+-----------------+------+------+-----+------+------+
4821 */
ad7ee8a2
CF
4822static void disas_cond_select(DisasContext *s, uint32_t insn)
4823{
e952d8c7 4824 unsigned int sf, else_inv, rm, cond, else_inc, rn, rd;
259cb684
RH
4825 TCGv_i64 tcg_rd, zero;
4826 DisasCompare64 c;
e952d8c7
CF
4827
4828 if (extract32(insn, 29, 1) || extract32(insn, 11, 1)) {
4829 /* S == 1 or op2<1> == 1 */
4830 unallocated_encoding(s);
4831 return;
4832 }
4833 sf = extract32(insn, 31, 1);
4834 else_inv = extract32(insn, 30, 1);
4835 rm = extract32(insn, 16, 5);
4836 cond = extract32(insn, 12, 4);
4837 else_inc = extract32(insn, 10, 1);
4838 rn = extract32(insn, 5, 5);
4839 rd = extract32(insn, 0, 5);
4840
e952d8c7
CF
4841 tcg_rd = cpu_reg(s, rd);
4842
259cb684
RH
4843 a64_test_cc(&c, cond);
4844 zero = tcg_const_i64(0);
e952d8c7 4845
259cb684
RH
4846 if (rn == 31 && rm == 31 && (else_inc ^ else_inv)) {
4847 /* CSET & CSETM. */
4848 tcg_gen_setcond_i64(tcg_invert_cond(c.cond), tcg_rd, c.value, zero);
4849 if (else_inv) {
4850 tcg_gen_neg_i64(tcg_rd, tcg_rd);
4851 }
4852 } else {
4853 TCGv_i64 t_true = cpu_reg(s, rn);
4854 TCGv_i64 t_false = read_cpu_reg(s, rm, 1);
e952d8c7 4855 if (else_inv && else_inc) {
259cb684 4856 tcg_gen_neg_i64(t_false, t_false);
e952d8c7 4857 } else if (else_inv) {
259cb684 4858 tcg_gen_not_i64(t_false, t_false);
e952d8c7 4859 } else if (else_inc) {
259cb684 4860 tcg_gen_addi_i64(t_false, t_false, 1);
e952d8c7 4861 }
259cb684
RH
4862 tcg_gen_movcond_i64(c.cond, tcg_rd, c.value, zero, t_true, t_false);
4863 }
4864
4865 tcg_temp_free_i64(zero);
4866 a64_free_cc(&c);
4867
4868 if (!sf) {
4869 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
e952d8c7 4870 }
ad7ee8a2
CF
4871}
4872
680ead21
CF
4873static void handle_clz(DisasContext *s, unsigned int sf,
4874 unsigned int rn, unsigned int rd)
4875{
4876 TCGv_i64 tcg_rd, tcg_rn;
4877 tcg_rd = cpu_reg(s, rd);
4878 tcg_rn = cpu_reg(s, rn);
4879
4880 if (sf) {
7539a012 4881 tcg_gen_clzi_i64(tcg_rd, tcg_rn, 64);
680ead21
CF
4882 } else {
4883 TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
ecc7b3aa 4884 tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
7539a012 4885 tcg_gen_clzi_i32(tcg_tmp32, tcg_tmp32, 32);
680ead21
CF
4886 tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
4887 tcg_temp_free_i32(tcg_tmp32);
4888 }
4889}
4890
e80c5020
CF
4891static void handle_cls(DisasContext *s, unsigned int sf,
4892 unsigned int rn, unsigned int rd)
4893{
4894 TCGv_i64 tcg_rd, tcg_rn;
4895 tcg_rd = cpu_reg(s, rd);
4896 tcg_rn = cpu_reg(s, rn);
4897
4898 if (sf) {
bc21dbcc 4899 tcg_gen_clrsb_i64(tcg_rd, tcg_rn);
e80c5020
CF
4900 } else {
4901 TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
ecc7b3aa 4902 tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
bc21dbcc 4903 tcg_gen_clrsb_i32(tcg_tmp32, tcg_tmp32);
e80c5020
CF
4904 tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
4905 tcg_temp_free_i32(tcg_tmp32);
4906 }
4907}
4908
82e14b02
AG
4909static void handle_rbit(DisasContext *s, unsigned int sf,
4910 unsigned int rn, unsigned int rd)
4911{
4912 TCGv_i64 tcg_rd, tcg_rn;
4913 tcg_rd = cpu_reg(s, rd);
4914 tcg_rn = cpu_reg(s, rn);
4915
4916 if (sf) {
4917 gen_helper_rbit64(tcg_rd, tcg_rn);
4918 } else {
4919 TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
ecc7b3aa 4920 tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
82e14b02
AG
4921 gen_helper_rbit(tcg_tmp32, tcg_tmp32);
4922 tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
4923 tcg_temp_free_i32(tcg_tmp32);
4924 }
4925}
4926
4ce31af4 4927/* REV with sf==1, opcode==3 ("REV64") */
45323209
CF
4928static void handle_rev64(DisasContext *s, unsigned int sf,
4929 unsigned int rn, unsigned int rd)
4930{
4931 if (!sf) {
4932 unallocated_encoding(s);
4933 return;
4934 }
4935 tcg_gen_bswap64_i64(cpu_reg(s, rd), cpu_reg(s, rn));
4936}
4937
4ce31af4
PM
4938/* REV with sf==0, opcode==2
4939 * REV32 (sf==1, opcode==2)
45323209
CF
4940 */
4941static void handle_rev32(DisasContext *s, unsigned int sf,
4942 unsigned int rn, unsigned int rd)
4943{
4944 TCGv_i64 tcg_rd = cpu_reg(s, rd);
4945
4946 if (sf) {
4947 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
4948 TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
4949
4950 /* bswap32_i64 requires zero high word */
4951 tcg_gen_ext32u_i64(tcg_tmp, tcg_rn);
4952 tcg_gen_bswap32_i64(tcg_rd, tcg_tmp);
4953 tcg_gen_shri_i64(tcg_tmp, tcg_rn, 32);
4954 tcg_gen_bswap32_i64(tcg_tmp, tcg_tmp);
4955 tcg_gen_concat32_i64(tcg_rd, tcg_rd, tcg_tmp);
4956
4957 tcg_temp_free_i64(tcg_tmp);
4958 } else {
4959 tcg_gen_ext32u_i64(tcg_rd, cpu_reg(s, rn));
4960 tcg_gen_bswap32_i64(tcg_rd, tcg_rd);
4961 }
4962}
4963
4ce31af4 4964/* REV16 (opcode==1) */
45323209
CF
4965static void handle_rev16(DisasContext *s, unsigned int sf,
4966 unsigned int rn, unsigned int rd)
4967{
4968 TCGv_i64 tcg_rd = cpu_reg(s, rd);
4969 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
4970 TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
abb1066d 4971 TCGv_i64 mask = tcg_const_i64(sf ? 0x00ff00ff00ff00ffull : 0x00ff00ff);
45323209 4972
abb1066d
RH
4973 tcg_gen_shri_i64(tcg_tmp, tcg_rn, 8);
4974 tcg_gen_and_i64(tcg_rd, tcg_rn, mask);
4975 tcg_gen_and_i64(tcg_tmp, tcg_tmp, mask);
4976 tcg_gen_shli_i64(tcg_rd, tcg_rd, 8);
4977 tcg_gen_or_i64(tcg_rd, tcg_rd, tcg_tmp);
45323209 4978
e4256c3c 4979 tcg_temp_free_i64(mask);
45323209
CF
4980 tcg_temp_free_i64(tcg_tmp);
4981}
4982
4ce31af4 4983/* Data-processing (1 source)
680ead21
CF
4984 * 31 30 29 28 21 20 16 15 10 9 5 4 0
4985 * +----+---+---+-----------------+---------+--------+------+------+
4986 * | sf | 1 | S | 1 1 0 1 0 1 1 0 | opcode2 | opcode | Rn | Rd |
4987 * +----+---+---+-----------------+---------+--------+------+------+
4988 */
ad7ee8a2
CF
4989static void disas_data_proc_1src(DisasContext *s, uint32_t insn)
4990{
18de2813 4991 unsigned int sf, opcode, opcode2, rn, rd;
95ebd99d 4992 TCGv_i64 tcg_rd;
680ead21 4993
18de2813 4994 if (extract32(insn, 29, 1)) {
680ead21
CF
4995 unallocated_encoding(s);
4996 return;
4997 }
4998
4999 sf = extract32(insn, 31, 1);
5000 opcode = extract32(insn, 10, 6);
18de2813 5001 opcode2 = extract32(insn, 16, 5);
680ead21
CF
5002 rn = extract32(insn, 5, 5);
5003 rd = extract32(insn, 0, 5);
5004
18de2813
RH
5005#define MAP(SF, O2, O1) ((SF) | (O1 << 1) | (O2 << 7))
5006
5007 switch (MAP(sf, opcode2, opcode)) {
5008 case MAP(0, 0x00, 0x00): /* RBIT */
5009 case MAP(1, 0x00, 0x00):
82e14b02
AG
5010 handle_rbit(s, sf, rn, rd);
5011 break;
18de2813
RH
5012 case MAP(0, 0x00, 0x01): /* REV16 */
5013 case MAP(1, 0x00, 0x01):
45323209
CF
5014 handle_rev16(s, sf, rn, rd);
5015 break;
18de2813
RH
5016 case MAP(0, 0x00, 0x02): /* REV/REV32 */
5017 case MAP(1, 0x00, 0x02):
45323209
CF
5018 handle_rev32(s, sf, rn, rd);
5019 break;
18de2813 5020 case MAP(1, 0x00, 0x03): /* REV64 */
45323209 5021 handle_rev64(s, sf, rn, rd);
680ead21 5022 break;
18de2813
RH
5023 case MAP(0, 0x00, 0x04): /* CLZ */
5024 case MAP(1, 0x00, 0x04):
680ead21
CF
5025 handle_clz(s, sf, rn, rd);
5026 break;
18de2813
RH
5027 case MAP(0, 0x00, 0x05): /* CLS */
5028 case MAP(1, 0x00, 0x05):
e80c5020 5029 handle_cls(s, sf, rn, rd);
680ead21 5030 break;
95ebd99d
RH
5031 case MAP(1, 0x01, 0x00): /* PACIA */
5032 if (s->pauth_active) {
5033 tcg_rd = cpu_reg(s, rd);
5034 gen_helper_pacia(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5035 } else if (!dc_isar_feature(aa64_pauth, s)) {
5036 goto do_unallocated;
5037 }
5038 break;
5039 case MAP(1, 0x01, 0x01): /* PACIB */
5040 if (s->pauth_active) {
5041 tcg_rd = cpu_reg(s, rd);
5042 gen_helper_pacib(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5043 } else if (!dc_isar_feature(aa64_pauth, s)) {
5044 goto do_unallocated;
5045 }
5046 break;
5047 case MAP(1, 0x01, 0x02): /* PACDA */
5048 if (s->pauth_active) {
5049 tcg_rd = cpu_reg(s, rd);
5050 gen_helper_pacda(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5051 } else if (!dc_isar_feature(aa64_pauth, s)) {
5052 goto do_unallocated;
5053 }
5054 break;
5055 case MAP(1, 0x01, 0x03): /* PACDB */
5056 if (s->pauth_active) {
5057 tcg_rd = cpu_reg(s, rd);
5058 gen_helper_pacdb(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5059 } else if (!dc_isar_feature(aa64_pauth, s)) {
5060 goto do_unallocated;
5061 }
5062 break;
5063 case MAP(1, 0x01, 0x04): /* AUTIA */
5064 if (s->pauth_active) {
5065 tcg_rd = cpu_reg(s, rd);
5066 gen_helper_autia(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5067 } else if (!dc_isar_feature(aa64_pauth, s)) {
5068 goto do_unallocated;
5069 }
5070 break;
5071 case MAP(1, 0x01, 0x05): /* AUTIB */
5072 if (s->pauth_active) {
5073 tcg_rd = cpu_reg(s, rd);
5074 gen_helper_autib(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5075 } else if (!dc_isar_feature(aa64_pauth, s)) {
5076 goto do_unallocated;
5077 }
5078 break;
5079 case MAP(1, 0x01, 0x06): /* AUTDA */
5080 if (s->pauth_active) {
5081 tcg_rd = cpu_reg(s, rd);
5082 gen_helper_autda(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5083 } else if (!dc_isar_feature(aa64_pauth, s)) {
5084 goto do_unallocated;
5085 }
5086 break;
5087 case MAP(1, 0x01, 0x07): /* AUTDB */
5088 if (s->pauth_active) {
5089 tcg_rd = cpu_reg(s, rd);
5090 gen_helper_autdb(tcg_rd, cpu_env, tcg_rd, cpu_reg_sp(s, rn));
5091 } else if (!dc_isar_feature(aa64_pauth, s)) {
5092 goto do_unallocated;
5093 }
5094 break;
5095 case MAP(1, 0x01, 0x08): /* PACIZA */
5096 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5097 goto do_unallocated;
5098 } else if (s->pauth_active) {
5099 tcg_rd = cpu_reg(s, rd);
5100 gen_helper_pacia(tcg_rd, cpu_env, tcg_rd, new_tmp_a64_zero(s));
5101 }
5102 break;
5103 case MAP(1, 0x01, 0x09): /* PACIZB */
5104 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5105 goto do_unallocated;
5106 } else if (s->pauth_active) {
5107 tcg_rd = cpu_reg(s, rd);
5108 gen_helper_pacib(tcg_rd, cpu_env, tcg_rd, new_tmp_a64_zero(s));
5109 }
5110 break;
5111 case MAP(1, 0x01, 0x0a): /* PACDZA */
5112 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5113 goto do_unallocated;
5114 } else if (s->pauth_active) {
5115 tcg_rd = cpu_reg(s, rd);
5116 gen_helper_pacda(tcg_rd, cpu_env, tcg_rd, new_tmp_a64_zero(s));
5117 }
5118 break;
5119 case MAP(1, 0x01, 0x0b): /* PACDZB */
5120 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5121 goto do_unallocated;
5122 } else if (s->pauth_active) {
5123 tcg_rd = cpu_reg(s, rd);
5124 gen_helper_pacdb(tcg_rd, cpu_env, tcg_rd, new_tmp_a64_zero(s));
5125 }
5126 break;
5127 case MAP(1, 0x01, 0x0c): /* AUTIZA */
5128 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5129 goto do_unallocated;
5130 } else if (s->pauth_active) {
5131 tcg_rd = cpu_reg(s, rd);
5132 gen_helper_autia(tcg_rd, cpu_env, tcg_rd, new_tmp_a64_zero(s));
5133 }
5134 break;
5135 case MAP(1, 0x01, 0x0d): /* AUTIZB */
5136 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5137 goto do_unallocated;
5138 } else if (s->pauth_active) {
5139 tcg_rd = cpu_reg(s, rd);
5140 gen_helper_autib(tcg_rd, cpu_env, tcg_rd, new_tmp_a64_zero(s));
5141 }
5142 break;
5143 case MAP(1, 0x01, 0x0e): /* AUTDZA */
5144 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5145 goto do_unallocated;
5146 } else if (s->pauth_active) {
5147 tcg_rd = cpu_reg(s, rd);
5148 gen_helper_autda(tcg_rd, cpu_env, tcg_rd, new_tmp_a64_zero(s));
5149 }
5150 break;
5151 case MAP(1, 0x01, 0x0f): /* AUTDZB */
5152 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5153 goto do_unallocated;
5154 } else if (s->pauth_active) {
5155 tcg_rd = cpu_reg(s, rd);
5156 gen_helper_autdb(tcg_rd, cpu_env, tcg_rd, new_tmp_a64_zero(s));
5157 }
5158 break;
5159 case MAP(1, 0x01, 0x10): /* XPACI */
5160 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5161 goto do_unallocated;
5162 } else if (s->pauth_active) {
5163 tcg_rd = cpu_reg(s, rd);
5164 gen_helper_xpaci(tcg_rd, cpu_env, tcg_rd);
5165 }
5166 break;
5167 case MAP(1, 0x01, 0x11): /* XPACD */
5168 if (!dc_isar_feature(aa64_pauth, s) || rn != 31) {
5169 goto do_unallocated;
5170 } else if (s->pauth_active) {
5171 tcg_rd = cpu_reg(s, rd);
5172 gen_helper_xpacd(tcg_rd, cpu_env, tcg_rd);
5173 }
5174 break;
18de2813 5175 default:
95ebd99d 5176 do_unallocated:
18de2813
RH
5177 unallocated_encoding(s);
5178 break;
680ead21 5179 }
18de2813
RH
5180
5181#undef MAP
ad7ee8a2
CF
5182}
5183
8220e911
AG
5184static void handle_div(DisasContext *s, bool is_signed, unsigned int sf,
5185 unsigned int rm, unsigned int rn, unsigned int rd)
5186{
5187 TCGv_i64 tcg_n, tcg_m, tcg_rd;
5188 tcg_rd = cpu_reg(s, rd);
5189
5190 if (!sf && is_signed) {
5191 tcg_n = new_tmp_a64(s);
5192 tcg_m = new_tmp_a64(s);
5193 tcg_gen_ext32s_i64(tcg_n, cpu_reg(s, rn));
5194 tcg_gen_ext32s_i64(tcg_m, cpu_reg(s, rm));
5195 } else {
5196 tcg_n = read_cpu_reg(s, rn, sf);
5197 tcg_m = read_cpu_reg(s, rm, sf);
5198 }
5199
5200 if (is_signed) {
5201 gen_helper_sdiv64(tcg_rd, tcg_n, tcg_m);
5202 } else {
5203 gen_helper_udiv64(tcg_rd, tcg_n, tcg_m);
5204 }
5205
5206 if (!sf) { /* zero extend final result */
5207 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
5208 }
5209}
5210
4ce31af4 5211/* LSLV, LSRV, ASRV, RORV */
6c1adc91
AG
5212static void handle_shift_reg(DisasContext *s,
5213 enum a64_shift_type shift_type, unsigned int sf,
5214 unsigned int rm, unsigned int rn, unsigned int rd)
5215{
5216 TCGv_i64 tcg_shift = tcg_temp_new_i64();
5217 TCGv_i64 tcg_rd = cpu_reg(s, rd);
5218 TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
5219
5220 tcg_gen_andi_i64(tcg_shift, cpu_reg(s, rm), sf ? 63 : 31);
5221 shift_reg(tcg_rd, tcg_rn, sf, shift_type, tcg_shift);
5222 tcg_temp_free_i64(tcg_shift);
5223}
5224
130f2e7d
PM
5225/* CRC32[BHWX], CRC32C[BHWX] */
5226static void handle_crc32(DisasContext *s,
5227 unsigned int sf, unsigned int sz, bool crc32c,
5228 unsigned int rm, unsigned int rn, unsigned int rd)
5229{
5230 TCGv_i64 tcg_acc, tcg_val;
5231 TCGv_i32 tcg_bytes;
5232
962fcbf2 5233 if (!dc_isar_feature(aa64_crc32, s)
130f2e7d
PM
5234 || (sf == 1 && sz != 3)
5235 || (sf == 0 && sz == 3)) {
5236 unallocated_encoding(s);
5237 return;
5238 }
5239
5240 if (sz == 3) {
5241 tcg_val = cpu_reg(s, rm);
5242 } else {
5243 uint64_t mask;
5244 switch (sz) {
5245 case 0:
5246 mask = 0xFF;
5247 break;
5248 case 1:
5249 mask = 0xFFFF;
5250 break;
5251 case 2:
5252 mask = 0xFFFFFFFF;
5253 break;
5254 default:
5255 g_assert_not_reached();
5256 }
5257 tcg_val = new_tmp_a64(s);
5258 tcg_gen_andi_i64(tcg_val, cpu_reg(s, rm), mask);
5259 }
5260
5261 tcg_acc = cpu_reg(s, rn);
5262 tcg_bytes = tcg_const_i32(1 << sz);
5263
5264 if (crc32c) {
5265 gen_helper_crc32c_64(cpu_reg(s, rd), tcg_acc, tcg_val, tcg_bytes);
5266 } else {
5267 gen_helper_crc32_64(cpu_reg(s, rd), tcg_acc, tcg_val, tcg_bytes);
5268 }
5269
5270 tcg_temp_free_i32(tcg_bytes);
5271}
5272
4ce31af4 5273/* Data-processing (2 source)
8220e911
AG
5274 * 31 30 29 28 21 20 16 15 10 9 5 4 0
5275 * +----+---+---+-----------------+------+--------+------+------+
5276 * | sf | 0 | S | 1 1 0 1 0 1 1 0 | Rm | opcode | Rn | Rd |
5277 * +----+---+---+-----------------+------+--------+------+------+
5278 */
ad7ee8a2
CF
5279static void disas_data_proc_2src(DisasContext *s, uint32_t insn)
5280{
8220e911
AG
5281 unsigned int sf, rm, opcode, rn, rd;
5282 sf = extract32(insn, 31, 1);
5283 rm = extract32(insn, 16, 5);
5284 opcode = extract32(insn, 10, 6);
5285 rn = extract32(insn, 5, 5);
5286 rd = extract32(insn, 0, 5);
5287
5288 if (extract32(insn, 29, 1)) {
5289 unallocated_encoding(s);
5290 return;
5291 }
5292
5293 switch (opcode) {
5294 case 2: /* UDIV */
5295 handle_div(s, false, sf, rm, rn, rd);
5296 break;
5297 case 3: /* SDIV */
5298 handle_div(s, true, sf, rm, rn, rd);
5299 break;
5300 case 8: /* LSLV */
6c1adc91
AG
5301 handle_shift_reg(s, A64_SHIFT_TYPE_LSL, sf, rm, rn, rd);
5302 break;
8220e911 5303 case 9: /* LSRV */
6c1adc91
AG
5304 handle_shift_reg(s, A64_SHIFT_TYPE_LSR, sf, rm, rn, rd);
5305 break;
8220e911 5306 case 10: /* ASRV */
6c1adc91
AG
5307 handle_shift_reg(s, A64_SHIFT_TYPE_ASR, sf, rm, rn, rd);
5308 break;
8220e911 5309 case 11: /* RORV */
6c1adc91
AG
5310 handle_shift_reg(s, A64_SHIFT_TYPE_ROR, sf, rm, rn, rd);
5311 break;
b6342a9f
RH
5312 case 12: /* PACGA */
5313 if (sf == 0 || !dc_isar_feature(aa64_pauth, s)) {
5314 goto do_unallocated;
5315 }
5316 gen_helper_pacga(cpu_reg(s, rd), cpu_env,
5317 cpu_reg(s, rn), cpu_reg_sp(s, rm));
5318 break;
8220e911
AG
5319 case 16:
5320 case 17:
5321 case 18:
5322 case 19:
5323 case 20:
5324 case 21:
5325 case 22:
5326 case 23: /* CRC32 */
130f2e7d
PM
5327 {
5328 int sz = extract32(opcode, 0, 2);
5329 bool crc32c = extract32(opcode, 2, 1);
5330 handle_crc32(s, sf, sz, crc32c, rm, rn, rd);
8220e911 5331 break;
130f2e7d 5332 }
8220e911 5333 default:
b6342a9f 5334 do_unallocated:
8220e911
AG
5335 unallocated_encoding(s);
5336 break;
5337 }
ad7ee8a2
CF
5338}
5339
2fba34f7
RH
5340/*
5341 * Data processing - register
5342 * 31 30 29 28 25 21 20 16 10 0
5343 * +--+---+--+---+-------+-----+-------+-------+---------+
5344 * | |op0| |op1| 1 0 1 | op2 | | op3 | |
5345 * +--+---+--+---+-------+-----+-------+-------+---------+
5346 */
ad7ee8a2
CF
5347static void disas_data_proc_reg(DisasContext *s, uint32_t insn)
5348{
2fba34f7
RH
5349 int op0 = extract32(insn, 30, 1);
5350 int op1 = extract32(insn, 28, 1);
5351 int op2 = extract32(insn, 21, 4);
5352 int op3 = extract32(insn, 10, 6);
5353
5354 if (!op1) {
5355 if (op2 & 8) {
5356 if (op2 & 1) {
5357 /* Add/sub (extended register) */
5358 disas_add_sub_ext_reg(s, insn);
5359 } else {
5360 /* Add/sub (shifted register) */
5361 disas_add_sub_reg(s, insn);
5362 }
ad7ee8a2 5363 } else {
2fba34f7
RH
5364 /* Logical (shifted register) */
5365 disas_logic_reg(s, insn);
ad7ee8a2 5366 }
2fba34f7
RH
5367 return;
5368 }
5369
5370 switch (op2) {
5371 case 0x0:
5372 switch (op3) {
5373 case 0x00: /* Add/subtract (with carry) */
ad7ee8a2
CF
5374 disas_adc_sbc(s, insn);
5375 break;
2fba34f7 5376
b89d9c98
RH
5377 case 0x01: /* Rotate right into flags */
5378 case 0x21:
5379 disas_rotate_right_into_flags(s, insn);
5380 break;
5381
5382 case 0x02: /* Evaluate into flags */
5383 case 0x12:
5384 case 0x22:
5385 case 0x32:
5386 disas_evaluate_into_flags(s, insn);
5387 break;
5388
ad7ee8a2 5389 default:
2fba34f7 5390 goto do_unallocated;
ad7ee8a2
CF
5391 }
5392 break;
2fba34f7
RH
5393
5394 case 0x2: /* Conditional compare */
5395 disas_cc(s, insn); /* both imm and reg forms */
5396 break;
5397
5398 case 0x4: /* Conditional select */
5399 disas_cond_select(s, insn);
5400 break;
5401
5402 case 0x6: /* Data-processing */
5403 if (op0) { /* (1 source) */
5404 disas_data_proc_1src(s, insn);
5405 } else { /* (2 source) */
5406 disas_data_proc_2src(s, insn);
5407 }
5408 break;
5409 case 0x8 ... 0xf: /* (3 source) */
5410 disas_data_proc_3src(s, insn);
5411 break;
5412
ad7ee8a2 5413 default:
2fba34f7 5414 do_unallocated:
ad7ee8a2
CF
5415 unallocated_encoding(s);
5416 break;
5417 }
5418}
5419
7a192925 5420static void handle_fp_compare(DisasContext *s, int size,
da7dafe7
CF
5421 unsigned int rn, unsigned int rm,
5422 bool cmp_with_zero, bool signal_all_nans)
5423{
5424 TCGv_i64 tcg_flags = tcg_temp_new_i64();
7a192925 5425 TCGv_ptr fpst = get_fpstatus_ptr(size == MO_16);
da7dafe7 5426
7a192925 5427 if (size == MO_64) {
da7dafe7
CF
5428 TCGv_i64 tcg_vn, tcg_vm;
5429
5430 tcg_vn = read_fp_dreg(s, rn);
5431 if (cmp_with_zero) {
5432 tcg_vm = tcg_const_i64(0);
5433 } else {
5434 tcg_vm = read_fp_dreg(s, rm);
5435 }
5436 if (signal_all_nans) {
5437 gen_helper_vfp_cmped_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5438 } else {
5439 gen_helper_vfp_cmpd_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5440 }
5441 tcg_temp_free_i64(tcg_vn);
5442 tcg_temp_free_i64(tcg_vm);
5443 } else {
7a192925
AB
5444 TCGv_i32 tcg_vn = tcg_temp_new_i32();
5445 TCGv_i32 tcg_vm = tcg_temp_new_i32();
da7dafe7 5446
7a192925 5447 read_vec_element_i32(s, tcg_vn, rn, 0, size);
da7dafe7 5448 if (cmp_with_zero) {
7a192925 5449 tcg_gen_movi_i32(tcg_vm, 0);
da7dafe7 5450 } else {
7a192925 5451 read_vec_element_i32(s, tcg_vm, rm, 0, size);
da7dafe7 5452 }
7a192925
AB
5453
5454 switch (size) {
5455 case MO_32:
5456 if (signal_all_nans) {
5457 gen_helper_vfp_cmpes_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5458 } else {
5459 gen_helper_vfp_cmps_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5460 }
5461 break;
5462 case MO_16:
5463 if (signal_all_nans) {
5464 gen_helper_vfp_cmpeh_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5465 } else {
5466 gen_helper_vfp_cmph_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
5467 }
5468 break;
5469 default:
5470 g_assert_not_reached();
da7dafe7 5471 }
7a192925 5472
da7dafe7
CF
5473 tcg_temp_free_i32(tcg_vn);
5474 tcg_temp_free_i32(tcg_vm);
5475 }
5476
5477 tcg_temp_free_ptr(fpst);
5478
5479 gen_set_nzcv(tcg_flags);
5480
5481 tcg_temp_free_i64(tcg_flags);
5482}
5483
4ce31af4 5484/* Floating point compare
faa0ba46
PM
5485 * 31 30 29 28 24 23 22 21 20 16 15 14 13 10 9 5 4 0
5486 * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
5487 * | M | 0 | S | 1 1 1 1 0 | type | 1 | Rm | op | 1 0 0 0 | Rn | op2 |
5488 * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
5489 */
5490static void disas_fp_compare(DisasContext *s, uint32_t insn)
5491{
da7dafe7 5492 unsigned int mos, type, rm, op, rn, opc, op2r;
7a192925 5493 int size;
da7dafe7
CF
5494
5495 mos = extract32(insn, 29, 3);
7a192925 5496 type = extract32(insn, 22, 2);
da7dafe7
CF
5497 rm = extract32(insn, 16, 5);
5498 op = extract32(insn, 14, 2);
5499 rn = extract32(insn, 5, 5);
5500 opc = extract32(insn, 3, 2);
5501 op2r = extract32(insn, 0, 3);
5502
7a192925
AB
5503 if (mos || op || op2r) {
5504 unallocated_encoding(s);
5505 return;
5506 }
5507
5508 switch (type) {
5509 case 0:
5510 size = MO_32;
5511 break;
5512 case 1:
5513 size = MO_64;
5514 break;
5515 case 3:
5516 size = MO_16;
5763190f 5517 if (dc_isar_feature(aa64_fp16, s)) {
7a192925
AB
5518 break;
5519 }
5520 /* fallthru */
5521 default:
da7dafe7
CF
5522 unallocated_encoding(s);
5523 return;
5524 }
5525
8c6afa6a
PM
5526 if (!fp_access_check(s)) {
5527 return;
5528 }
5529
7a192925 5530 handle_fp_compare(s, size, rn, rm, opc & 1, opc & 2);
faa0ba46
PM
5531}
5532
4ce31af4 5533/* Floating point conditional compare
faa0ba46
PM
5534 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 3 0
5535 * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
5536 * | M | 0 | S | 1 1 1 1 0 | type | 1 | Rm | cond | 0 1 | Rn | op | nzcv |
5537 * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
5538 */
5539static void disas_fp_ccomp(DisasContext *s, uint32_t insn)
5540{
513f1d76
CF
5541 unsigned int mos, type, rm, cond, rn, op, nzcv;
5542 TCGv_i64 tcg_flags;
42a268c2 5543 TCGLabel *label_continue = NULL;
7a192925 5544 int size;
513f1d76
CF
5545
5546 mos = extract32(insn, 29, 3);
7a192925 5547 type = extract32(insn, 22, 2);
513f1d76
CF
5548 rm = extract32(insn, 16, 5);
5549 cond = extract32(insn, 12, 4);
5550 rn = extract32(insn, 5, 5);
5551 op = extract32(insn, 4, 1);
5552 nzcv = extract32(insn, 0, 4);
5553
7a192925
AB
5554 if (mos) {
5555 unallocated_encoding(s);
5556 return;
5557 }
5558
5559 switch (type) {
5560 case 0:
5561 size = MO_32;
5562 break;
5563 case 1:
5564 size = MO_64;
5565 break;
5566 case 3:
5567 size = MO_16;
5763190f 5568 if (dc_isar_feature(aa64_fp16, s)) {
7a192925
AB
5569 break;
5570 }
5571 /* fallthru */
5572 default:
513f1d76
CF
5573 unallocated_encoding(s);
5574 return;
5575 }
5576
8c6afa6a
PM
5577 if (!fp_access_check(s)) {
5578 return;
5579 }
5580
513f1d76 5581 if (cond < 0x0e) { /* not always */
42a268c2 5582 TCGLabel *label_match = gen_new_label();
513f1d76
CF
5583 label_continue = gen_new_label();
5584 arm_gen_test_cc(cond, label_match);
5585 /* nomatch: */
5586 tcg_flags = tcg_const_i64(nzcv << 28);
5587 gen_set_nzcv(tcg_flags);
5588 tcg_temp_free_i64(tcg_flags);
5589 tcg_gen_br(label_continue);
5590 gen_set_label(label_match);
5591 }
5592
7a192925 5593 handle_fp_compare(s, size, rn, rm, false, op);
513f1d76
CF
5594
5595 if (cond < 0x0e) {
5596 gen_set_label(label_continue);
5597 }
faa0ba46
PM
5598}
5599
4ce31af4 5600/* Floating point conditional select
faa0ba46
PM
5601 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 0
5602 * +---+---+---+-----------+------+---+------+------+-----+------+------+
5603 * | M | 0 | S | 1 1 1 1 0 | type | 1 | Rm | cond | 1 1 | Rn | Rd |
5604 * +---+---+---+-----------+------+---+------+------+-----+------+------+
5605 */
5606static void disas_fp_csel(DisasContext *s, uint32_t insn)
5607{
5640ff62 5608 unsigned int mos, type, rm, cond, rn, rd;
6e061029
RH
5609 TCGv_i64 t_true, t_false, t_zero;
5610 DisasCompare64 c;
14776ab5 5611 MemOp sz;
5640ff62
CF
5612
5613 mos = extract32(insn, 29, 3);
ace97fee 5614 type = extract32(insn, 22, 2);
5640ff62
CF
5615 rm = extract32(insn, 16, 5);
5616 cond = extract32(insn, 12, 4);
5617 rn = extract32(insn, 5, 5);
5618 rd = extract32(insn, 0, 5);
5619
ace97fee
AB
5620 if (mos) {
5621 unallocated_encoding(s);
5622 return;
5623 }
5624
5625 switch (type) {
5626 case 0:
5627 sz = MO_32;
5628 break;
5629 case 1:
5630 sz = MO_64;
5631 break;
5632 case 3:
5633 sz = MO_16;
5763190f 5634 if (dc_isar_feature(aa64_fp16, s)) {
ace97fee
AB
5635 break;
5636 }
5637 /* fallthru */
5638 default:
5640ff62
CF
5639 unallocated_encoding(s);
5640 return;
5641 }
5642
8c6afa6a
PM
5643 if (!fp_access_check(s)) {
5644 return;
5645 }
5646
ace97fee 5647 /* Zero extend sreg & hreg inputs to 64 bits now. */
6e061029
RH
5648 t_true = tcg_temp_new_i64();
5649 t_false = tcg_temp_new_i64();
ace97fee
AB
5650 read_vec_element(s, t_true, rn, 0, sz);
5651 read_vec_element(s, t_false, rm, 0, sz);
5640ff62 5652
6e061029
RH
5653 a64_test_cc(&c, cond);
5654 t_zero = tcg_const_i64(0);
5655 tcg_gen_movcond_i64(c.cond, t_true, c.value, t_zero, t_true, t_false);
5656 tcg_temp_free_i64(t_zero);
5657 tcg_temp_free_i64(t_false);
5658 a64_free_cc(&c);
5640ff62 5659
ace97fee 5660 /* Note that sregs & hregs write back zeros to the high bits,
6e061029
RH
5661 and we've already done the zero-extension. */
5662 write_fp_dreg(s, rd, t_true);
5663 tcg_temp_free_i64(t_true);
faa0ba46
PM
5664}
5665
c2c08713
AB
5666/* Floating-point data-processing (1 source) - half precision */
5667static void handle_fp_1src_half(DisasContext *s, int opcode, int rd, int rn)
5668{
5669 TCGv_ptr fpst = NULL;
3d99d931 5670 TCGv_i32 tcg_op = read_fp_hreg(s, rn);
c2c08713
AB
5671 TCGv_i32 tcg_res = tcg_temp_new_i32();
5672
c2c08713
AB
5673 switch (opcode) {
5674 case 0x0: /* FMOV */
5675 tcg_gen_mov_i32(tcg_res, tcg_op);
5676 break;
5677 case 0x1: /* FABS */
5678 tcg_gen_andi_i32(tcg_res, tcg_op, 0x7fff);
5679 break;
5680 case 0x2: /* FNEG */
5681 tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
5682 break;
5683 case 0x3: /* FSQRT */
905edee9
AB
5684 fpst = get_fpstatus_ptr(true);
5685 gen_helper_sqrt_f16(tcg_res, tcg_op, fpst);
c2c08713
AB
5686 break;
5687 case 0x8: /* FRINTN */
5688 case 0x9: /* FRINTP */
5689 case 0xa: /* FRINTM */
5690 case 0xb: /* FRINTZ */
5691 case 0xc: /* FRINTA */
5692 {
5693 TCGv_i32 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(opcode & 7));
5694 fpst = get_fpstatus_ptr(true);
5695
5696 gen_helper_set_rmode(tcg_rmode, tcg_rmode, fpst);
5697 gen_helper_advsimd_rinth(tcg_res, tcg_op, fpst);
5698
5699 gen_helper_set_rmode(tcg_rmode, tcg_rmode, fpst);
5700 tcg_temp_free_i32(tcg_rmode);
5701 break;
5702 }
5703 case 0xe: /* FRINTX */
5704 fpst = get_fpstatus_ptr(true);
5705 gen_helper_advsimd_rinth_exact(tcg_res, tcg_op, fpst);
5706 break;
5707 case 0xf: /* FRINTI */
5708 fpst = get_fpstatus_ptr(true);
5709 gen_helper_advsimd_rinth(tcg_res, tcg_op, fpst);
5710 break;
5711 default:
5712 abort();
5713 }
5714
5715 write_fp_sreg(s, rd, tcg_res);
5716
5717 if (fpst) {
5718 tcg_temp_free_ptr(fpst);
5719 }
5720 tcg_temp_free_i32(tcg_op);
5721 tcg_temp_free_i32(tcg_res);
5722}
5723
4ce31af4 5724/* Floating-point data-processing (1 source) - single precision */
d9b0848d
PM
5725static void handle_fp_1src_single(DisasContext *s, int opcode, int rd, int rn)
5726{
0e4db23d
RH
5727 void (*gen_fpst)(TCGv_i32, TCGv_i32, TCGv_ptr);
5728 TCGv_i32 tcg_op, tcg_res;
d9b0848d 5729 TCGv_ptr fpst;
0e4db23d 5730 int rmode = -1;
d9b0848d 5731
d9b0848d
PM
5732 tcg_op = read_fp_sreg(s, rn);
5733 tcg_res = tcg_temp_new_i32();
5734
5735 switch (opcode) {
5736 case 0x0: /* FMOV */
5737 tcg_gen_mov_i32(tcg_res, tcg_op);
0e4db23d 5738 goto done;
d9b0848d
PM
5739 case 0x1: /* FABS */
5740 gen_helper_vfp_abss(tcg_res, tcg_op);
0e4db23d 5741 goto done;
d9b0848d
PM
5742 case 0x2: /* FNEG */
5743 gen_helper_vfp_negs(tcg_res, tcg_op);
0e4db23d 5744 goto done;
d9b0848d
PM
5745 case 0x3: /* FSQRT */
5746 gen_helper_vfp_sqrts(tcg_res, tcg_op, cpu_env);
0e4db23d 5747 goto done;
d9b0848d
PM
5748 case 0x8: /* FRINTN */
5749 case 0x9: /* FRINTP */
5750 case 0xa: /* FRINTM */
5751 case 0xb: /* FRINTZ */
5752 case 0xc: /* FRINTA */
0e4db23d
RH
5753 rmode = arm_rmode_to_sf(opcode & 7);
5754 gen_fpst = gen_helper_rints;
d9b0848d 5755 break;
d9b0848d 5756 case 0xe: /* FRINTX */
0e4db23d 5757 gen_fpst = gen_helper_rints_exact;
d9b0848d
PM
5758 break;
5759 case 0xf: /* FRINTI */
0e4db23d 5760 gen_fpst = gen_helper_rints;
d9b0848d 5761 break;
6bea2563
RH
5762 case 0x10: /* FRINT32Z */
5763 rmode = float_round_to_zero;
5764 gen_fpst = gen_helper_frint32_s;
5765 break;
5766 case 0x11: /* FRINT32X */
5767 gen_fpst = gen_helper_frint32_s;
5768 break;
5769 case 0x12: /* FRINT64Z */
5770 rmode = float_round_to_zero;
5771 gen_fpst = gen_helper_frint64_s;
5772 break;
5773 case 0x13: /* FRINT64X */
5774 gen_fpst = gen_helper_frint64_s;
5775 break;
d9b0848d 5776 default:
0e4db23d 5777 g_assert_not_reached();
d9b0848d
PM
5778 }
5779
0e4db23d
RH
5780 fpst = get_fpstatus_ptr(false);
5781 if (rmode >= 0) {
5782 TCGv_i32 tcg_rmode = tcg_const_i32(rmode);
5783 gen_helper_set_rmode(tcg_rmode, tcg_rmode, fpst);
5784 gen_fpst(tcg_res, tcg_op, fpst);
5785 gen_helper_set_rmode(tcg_rmode, tcg_rmode, fpst);
5786 tcg_temp_free_i32(tcg_rmode);
5787 } else {
5788 gen_fpst(tcg_res, tcg_op, fpst);
5789 }
d9b0848d 5790 tcg_temp_free_ptr(fpst);
0e4db23d
RH
5791
5792 done:
5793 write_fp_sreg(s, rd, tcg_res);
d9b0848d
PM
5794 tcg_temp_free_i32(tcg_op);
5795 tcg_temp_free_i32(tcg_res);
5796}
5797
4ce31af4 5798/* Floating-point data-processing (1 source) - double precision */
d9b0848d
PM
5799static void handle_fp_1src_double(DisasContext *s, int opcode, int rd, int rn)
5800{
0e4db23d
RH
5801 void (*gen_fpst)(TCGv_i64, TCGv_i64, TCGv_ptr);
5802 TCGv_i64 tcg_op, tcg_res;
d9b0848d 5803 TCGv_ptr fpst;
0e4db23d 5804 int rmode = -1;
d9b0848d 5805
377ef731
RH
5806 switch (opcode) {
5807 case 0x0: /* FMOV */
5808 gen_gvec_fn2(s, false, rd, rn, tcg_gen_gvec_mov, 0);
5809 return;
5810 }
5811
d9b0848d
PM
5812 tcg_op = read_fp_dreg(s, rn);
5813 tcg_res = tcg_temp_new_i64();
5814
5815 switch (opcode) {
d9b0848d
PM
5816 case 0x1: /* FABS */
5817 gen_helper_vfp_absd(tcg_res, tcg_op);
0e4db23d 5818 goto done;
d9b0848d
PM
5819 case 0x2: /* FNEG */
5820 gen_helper_vfp_negd(tcg_res, tcg_op);
0e4db23d 5821 goto done;
d9b0848d
PM
5822 case 0x3: /* FSQRT */
5823 gen_helper_vfp_sqrtd(tcg_res, tcg_op, cpu_env);
0e4db23d 5824 goto done;
d9b0848d
PM
5825 case 0x8: /* FRINTN */
5826 case 0x9: /* FRINTP */
5827 case 0xa: /* FRINTM */
5828 case 0xb: /* FRINTZ */
5829 case 0xc: /* FRINTA */
0e4db23d
RH
5830 rmode = arm_rmode_to_sf(opcode & 7);
5831 gen_fpst = gen_helper_rintd;
d9b0848d 5832 break;
d9b0848d 5833 case 0xe: /* FRINTX */
0e4db23d 5834 gen_fpst = gen_helper_rintd_exact;
d9b0848d
PM
5835 break;
5836 case 0xf: /* FRINTI */
0e4db23d 5837 gen_fpst = gen_helper_rintd;
d9b0848d 5838 break;
6bea2563
RH
5839 case 0x10: /* FRINT32Z */
5840 rmode = float_round_to_zero;
5841 gen_fpst = gen_helper_frint32_d;
5842 break;
5843 case 0x11: /* FRINT32X */
5844 gen_fpst = gen_helper_frint32_d;
5845 break;
5846 case 0x12: /* FRINT64Z */
5847 rmode = float_round_to_zero;
5848 gen_fpst = gen_helper_frint64_d;
5849 break;
5850 case 0x13: /* FRINT64X */
5851 gen_fpst = gen_helper_frint64_d;
5852 break;
d9b0848d 5853 default:
0e4db23d 5854 g_assert_not_reached();
d9b0848d
PM
5855 }
5856
0e4db23d
RH
5857 fpst = get_fpstatus_ptr(false);
5858 if (rmode >= 0) {
5859 TCGv_i32 tcg_rmode = tcg_const_i32(rmode);
5860 gen_helper_set_rmode(tcg_rmode, tcg_rmode, fpst);
5861 gen_fpst(tcg_res, tcg_op, fpst);
5862 gen_helper_set_rmode(tcg_rmode, tcg_rmode, fpst);
5863 tcg_temp_free_i32(tcg_rmode);
5864 } else {
5865 gen_fpst(tcg_res, tcg_op, fpst);
5866 }
d9b0848d 5867 tcg_temp_free_ptr(fpst);
0e4db23d
RH
5868
5869 done:
5870 write_fp_dreg(s, rd, tcg_res);
d9b0848d
PM
5871 tcg_temp_free_i64(tcg_op);
5872 tcg_temp_free_i64(tcg_res);
5873}
5874
8900aad2
PM
5875static void handle_fp_fcvt(DisasContext *s, int opcode,
5876 int rd, int rn, int dtype, int ntype)
5877{
5878 switch (ntype) {
5879 case 0x0:
5880 {
5881 TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
5882 if (dtype == 1) {
5883 /* Single to double */
5884 TCGv_i64 tcg_rd = tcg_temp_new_i64();
5885 gen_helper_vfp_fcvtds(tcg_rd, tcg_rn, cpu_env);
5886 write_fp_dreg(s, rd, tcg_rd);
5887 tcg_temp_free_i64(tcg_rd);
5888 } else {
5889 /* Single to half */
5890 TCGv_i32 tcg_rd = tcg_temp_new_i32();
486624fc
AB
5891 TCGv_i32 ahp = get_ahp_flag();
5892 TCGv_ptr fpst = get_fpstatus_ptr(false);
5893
5894 gen_helper_vfp_fcvt_f32_to_f16(tcg_rd, tcg_rn, fpst, ahp);
8900aad2
PM
5895 /* write_fp_sreg is OK here because top half of tcg_rd is zero */
5896 write_fp_sreg(s, rd, tcg_rd);
5897 tcg_temp_free_i32(tcg_rd);
486624fc
AB
5898 tcg_temp_free_i32(ahp);
5899 tcg_temp_free_ptr(fpst);
8900aad2
PM
5900 }
5901 tcg_temp_free_i32(tcg_rn);
5902 break;
5903 }
5904 case 0x1:
5905 {
5906 TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
5907 TCGv_i32 tcg_rd = tcg_temp_new_i32();
5908 if (dtype == 0) {
5909 /* Double to single */
5910 gen_helper_vfp_fcvtsd(tcg_rd, tcg_rn, cpu_env);
5911 } else {
486624fc
AB
5912 TCGv_ptr fpst = get_fpstatus_ptr(false);
5913 TCGv_i32 ahp = get_ahp_flag();
8900aad2 5914 /* Double to half */
486624fc 5915 gen_helper_vfp_fcvt_f64_to_f16(tcg_rd, tcg_rn, fpst, ahp);
8900aad2 5916 /* write_fp_sreg is OK here because top half of tcg_rd is zero */
486624fc
AB
5917 tcg_temp_free_ptr(fpst);
5918 tcg_temp_free_i32(ahp);
8900aad2
PM
5919 }
5920 write_fp_sreg(s, rd, tcg_rd);
5921 tcg_temp_free_i32(tcg_rd);
5922 tcg_temp_free_i64(tcg_rn);
5923 break;
5924 }
5925 case 0x3:
5926 {
5927 TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
486624fc
AB
5928 TCGv_ptr tcg_fpst = get_fpstatus_ptr(false);
5929 TCGv_i32 tcg_ahp = get_ahp_flag();
8900aad2
PM
5930 tcg_gen_ext16u_i32(tcg_rn, tcg_rn);
5931 if (dtype == 0) {
5932 /* Half to single */
5933 TCGv_i32 tcg_rd = tcg_temp_new_i32();
486624fc 5934 gen_helper_vfp_fcvt_f16_to_f32(tcg_rd, tcg_rn, tcg_fpst, tcg_ahp);
8900aad2
PM
5935 write_fp_sreg(s, rd, tcg_rd);
5936 tcg_temp_free_i32(tcg_rd);
5937 } else {
5938 /* Half to double */
5939 TCGv_i64 tcg_rd = tcg_temp_new_i64();
486624fc 5940 gen_helper_vfp_fcvt_f16_to_f64(tcg_rd, tcg_rn, tcg_fpst, tcg_ahp);
8900aad2
PM
5941 write_fp_dreg(s, rd, tcg_rd);
5942 tcg_temp_free_i64(tcg_rd);
5943 }
5944 tcg_temp_free_i32(tcg_rn);
aeab8e5e
AB
5945 tcg_temp_free_ptr(tcg_fpst);
5946 tcg_temp_free_i32(tcg_ahp);
8900aad2
PM
5947 break;
5948 }
5949 default:
5950 abort();
5951 }
5952}
5953
4ce31af4 5954/* Floating point data-processing (1 source)
faa0ba46
PM
5955 * 31 30 29 28 24 23 22 21 20 15 14 10 9 5 4 0
5956 * +---+---+---+-----------+------+---+--------+-----------+------+------+
5957 * | M | 0 | S | 1 1 1 1 0 | type | 1 | opcode | 1 0 0 0 0 | Rn | Rd |
5958 * +---+---+---+-----------+------+---+--------+-----------+------+------+
5959 */
5960static void disas_fp_1src(DisasContext *s, uint32_t insn)
5961{
c1e20801 5962 int mos = extract32(insn, 29, 3);
d9b0848d
PM
5963 int type = extract32(insn, 22, 2);
5964 int opcode = extract32(insn, 15, 6);
5965 int rn = extract32(insn, 5, 5);
5966 int rd = extract32(insn, 0, 5);
5967
c1e20801
PM
5968 if (mos) {
5969 unallocated_encoding(s);
5970 return;
5971 }
5972
d9b0848d
PM
5973 switch (opcode) {
5974 case 0x4: case 0x5: case 0x7:
8900aad2 5975 {
d9b0848d 5976 /* FCVT between half, single and double precision */
8900aad2
PM
5977 int dtype = extract32(opcode, 0, 2);
5978 if (type == 2 || dtype == type) {
5979 unallocated_encoding(s);
5980 return;
5981 }
8c6afa6a
PM
5982 if (!fp_access_check(s)) {
5983 return;
5984 }
5985
8900aad2 5986 handle_fp_fcvt(s, opcode, rd, rn, dtype, type);
d9b0848d 5987 break;
8900aad2 5988 }
6bea2563
RH
5989
5990 case 0x10 ... 0x13: /* FRINT{32,64}{X,Z} */
5991 if (type > 1 || !dc_isar_feature(aa64_frint, s)) {
5992 unallocated_encoding(s);
5993 return;
5994 }
5995 /* fall through */
d9b0848d
PM
5996 case 0x0 ... 0x3:
5997 case 0x8 ... 0xc:
5998 case 0xe ... 0xf:
5999 /* 32-to-32 and 64-to-64 ops */
6000 switch (type) {
6001 case 0:
8c6afa6a
PM
6002 if (!fp_access_check(s)) {
6003 return;
6004 }
d9b0848d
PM
6005 handle_fp_1src_single(s, opcode, rd, rn);
6006 break;
6007 case 1:
8c6afa6a
PM
6008 if (!fp_access_check(s)) {
6009 return;
6010 }
d9b0848d
PM
6011 handle_fp_1src_double(s, opcode, rd, rn);
6012 break;
c2c08713 6013 case 3:
5763190f 6014 if (!dc_isar_feature(aa64_fp16, s)) {
c2c08713
AB
6015 unallocated_encoding(s);
6016 return;
6017 }
6018
6019 if (!fp_access_check(s)) {
6020 return;
6021 }
c2c08713
AB
6022 handle_fp_1src_half(s, opcode, rd, rn);
6023 break;
d9b0848d
PM
6024 default:
6025 unallocated_encoding(s);
6026 }
6027 break;
6bea2563 6028
d9b0848d
PM
6029 default:
6030 unallocated_encoding(s);
6031 break;
6032 }
faa0ba46
PM
6033}
6034
4ce31af4 6035/* Floating-point data-processing (2 source) - single precision */
ec73d2e0
AG
6036static void handle_fp_2src_single(DisasContext *s, int opcode,
6037 int rd, int rn, int rm)
6038{
6039 TCGv_i32 tcg_op1;
6040 TCGv_i32 tcg_op2;
6041 TCGv_i32 tcg_res;
6042 TCGv_ptr fpst;
6043
6044 tcg_res = tcg_temp_new_i32();
d81ce0ef 6045 fpst = get_fpstatus_ptr(false);
ec73d2e0
AG
6046 tcg_op1 = read_fp_sreg(s, rn);
6047 tcg_op2 = read_fp_sreg(s, rm);
6048
6049 switch (opcode) {
6050 case 0x0: /* FMUL */
6051 gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
6052 break;
6053 case 0x1: /* FDIV */
6054 gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
6055 break;
6056 case 0x2: /* FADD */
6057 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
6058 break;
6059 case 0x3: /* FSUB */
6060 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
6061 break;
6062 case 0x4: /* FMAX */
6063 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
6064 break;
6065 case 0x5: /* FMIN */
6066 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
6067 break;
6068 case 0x6: /* FMAXNM */
6069 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
6070 break;
6071 case 0x7: /* FMINNM */
6072 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
6073 break;
6074 case 0x8: /* FNMUL */
6075 gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
6076 gen_helper_vfp_negs(tcg_res, tcg_res);
6077 break;
6078 }
6079
6080 write_fp_sreg(s, rd, tcg_res);
6081
6082 tcg_temp_free_ptr(fpst);
6083 tcg_temp_free_i32(tcg_op1);
6084 tcg_temp_free_i32(tcg_op2);
6085 tcg_temp_free_i32(tcg_res);
6086}
6087
4ce31af4 6088/* Floating-point data-processing (2 source) - double precision */
ec73d2e0
AG
6089static void handle_fp_2src_double(DisasContext *s, int opcode,
6090 int rd, int rn, int rm)
6091{
6092 TCGv_i64 tcg_op1;
6093 TCGv_i64 tcg_op2;
6094 TCGv_i64 tcg_res;
6095 TCGv_ptr fpst;
6096
6097 tcg_res = tcg_temp_new_i64();
d81ce0ef 6098 fpst = get_fpstatus_ptr(false);
ec73d2e0
AG
6099 tcg_op1 = read_fp_dreg(s, rn);
6100 tcg_op2 = read_fp_dreg(s, rm);
6101
6102 switch (opcode) {
6103 case 0x0: /* FMUL */
6104 gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
6105 break;
6106 case 0x1: /* FDIV */
6107 gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
6108 break;
6109 case 0x2: /* FADD */
6110 gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
6111 break;
6112 case 0x3: /* FSUB */
6113 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
6114 break;
6115 case 0x4: /* FMAX */
6116 gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
6117 break;
6118 case 0x5: /* FMIN */
6119 gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
6120 break;
6121 case 0x6: /* FMAXNM */
6122 gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
6123 break;
6124 case 0x7: /* FMINNM */
6125 gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
6126 break;
6127 case 0x8: /* FNMUL */
6128 gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
6129 gen_helper_vfp_negd(tcg_res, tcg_res);
6130 break;
6131 }
6132
6133 write_fp_dreg(s, rd, tcg_res);
6134
6135 tcg_temp_free_ptr(fpst);
6136 tcg_temp_free_i64(tcg_op1);
6137 tcg_temp_free_i64(tcg_op2);
6138 tcg_temp_free_i64(tcg_res);
6139}
6140
b8f5171c
RH
6141/* Floating-point data-processing (2 source) - half precision */
6142static void handle_fp_2src_half(DisasContext *s, int opcode,
6143 int rd, int rn, int rm)
6144{
6145 TCGv_i32 tcg_op1;
6146 TCGv_i32 tcg_op2;
6147 TCGv_i32 tcg_res;
6148 TCGv_ptr fpst;
6149
6150 tcg_res = tcg_temp_new_i32();
6151 fpst = get_fpstatus_ptr(true);
6152 tcg_op1 = read_fp_hreg(s, rn);
6153 tcg_op2 = read_fp_hreg(s, rm);
6154
6155 switch (opcode) {
6156 case 0x0: /* FMUL */
6157 gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
6158 break;
6159 case 0x1: /* FDIV */
6160 gen_helper_advsimd_divh(tcg_res, tcg_op1, tcg_op2, fpst);
6161 break;
6162 case 0x2: /* FADD */
6163 gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
6164 break;
6165 case 0x3: /* FSUB */
6166 gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
6167 break;
6168 case 0x4: /* FMAX */
6169 gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
6170 break;
6171 case 0x5: /* FMIN */
6172 gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
6173 break;
6174 case 0x6: /* FMAXNM */
6175 gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
6176 break;
6177 case 0x7: /* FMINNM */
6178 gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
6179 break;
6180 case 0x8: /* FNMUL */
6181 gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
6182 tcg_gen_xori_i32(tcg_res, tcg_res, 0x8000);
6183 break;
6184 default:
6185 g_assert_not_reached();
6186 }
6187
6188 write_fp_sreg(s, rd, tcg_res);
6189
6190 tcg_temp_free_ptr(fpst);
6191 tcg_temp_free_i32(tcg_op1);
6192 tcg_temp_free_i32(tcg_op2);
6193 tcg_temp_free_i32(tcg_res);
6194}
6195
4ce31af4 6196/* Floating point data-processing (2 source)
faa0ba46
PM
6197 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 0
6198 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
6199 * | M | 0 | S | 1 1 1 1 0 | type | 1 | Rm | opcode | 1 0 | Rn | Rd |
6200 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
6201 */
6202static void disas_fp_2src(DisasContext *s, uint32_t insn)
6203{
c1e20801 6204 int mos = extract32(insn, 29, 3);
ec73d2e0
AG
6205 int type = extract32(insn, 22, 2);
6206 int rd = extract32(insn, 0, 5);
6207 int rn = extract32(insn, 5, 5);
6208 int rm = extract32(insn, 16, 5);
6209 int opcode = extract32(insn, 12, 4);
6210
c1e20801 6211 if (opcode > 8 || mos) {
ec73d2e0
AG
6212 unallocated_encoding(s);
6213 return;
6214 }
6215
6216 switch (type) {
6217 case 0:
8c6afa6a
PM
6218 if (!fp_access_check(s)) {
6219 return;
6220 }
ec73d2e0
AG
6221 handle_fp_2src_single(s, opcode, rd, rn, rm);
6222 break;
6223 case 1:
8c6afa6a
PM
6224 if (!fp_access_check(s)) {
6225 return;
6226 }
ec73d2e0
AG
6227 handle_fp_2src_double(s, opcode, rd, rn, rm);
6228 break;
b8f5171c 6229 case 3:
5763190f 6230 if (!dc_isar_feature(aa64_fp16, s)) {
b8f5171c
RH
6231 unallocated_encoding(s);
6232 return;
6233 }
6234 if (!fp_access_check(s)) {
6235 return;
6236 }
6237 handle_fp_2src_half(s, opcode, rd, rn, rm);
6238 break;
ec73d2e0
AG
6239 default:
6240 unallocated_encoding(s);
6241 }
faa0ba46
PM
6242}
6243
4ce31af4 6244/* Floating-point data-processing (3 source) - single precision */
6a30667f
AG
6245static void handle_fp_3src_single(DisasContext *s, bool o0, bool o1,
6246 int rd, int rn, int rm, int ra)
6247{
6248 TCGv_i32 tcg_op1, tcg_op2, tcg_op3;
6249 TCGv_i32 tcg_res = tcg_temp_new_i32();
d81ce0ef 6250 TCGv_ptr fpst = get_fpstatus_ptr(false);
6a30667f
AG
6251
6252 tcg_op1 = read_fp_sreg(s, rn);
6253 tcg_op2 = read_fp_sreg(s, rm);
6254 tcg_op3 = read_fp_sreg(s, ra);
6255
6256 /* These are fused multiply-add, and must be done as one
6257 * floating point operation with no rounding between the
6258 * multiplication and addition steps.
6259 * NB that doing the negations here as separate steps is
6260 * correct : an input NaN should come out with its sign bit
6261 * flipped if it is a negated-input.
6262 */
6263 if (o1 == true) {
6264 gen_helper_vfp_negs(tcg_op3, tcg_op3);
6265 }
6266
6267 if (o0 != o1) {
6268 gen_helper_vfp_negs(tcg_op1, tcg_op1);
6269 }
6270
6271 gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
6272
6273 write_fp_sreg(s, rd, tcg_res);
6274
6275 tcg_temp_free_ptr(fpst);
6276 tcg_temp_free_i32(tcg_op1);
6277 tcg_temp_free_i32(tcg_op2);
6278 tcg_temp_free_i32(tcg_op3);
6279 tcg_temp_free_i32(tcg_res);
6280}
6281
4ce31af4 6282/* Floating-point data-processing (3 source) - double precision */
6a30667f
AG
6283static void handle_fp_3src_double(DisasContext *s, bool o0, bool o1,
6284 int rd, int rn, int rm, int ra)
6285{
6286 TCGv_i64 tcg_op1, tcg_op2, tcg_op3;
6287 TCGv_i64 tcg_res = tcg_temp_new_i64();
d81ce0ef 6288 TCGv_ptr fpst = get_fpstatus_ptr(false);
6a30667f
AG
6289
6290 tcg_op1 = read_fp_dreg(s, rn);
6291 tcg_op2 = read_fp_dreg(s, rm);
6292 tcg_op3 = read_fp_dreg(s, ra);
6293
6294 /* These are fused multiply-add, and must be done as one
6295 * floating point operation with no rounding between the
6296 * multiplication and addition steps.
6297 * NB that doing the negations here as separate steps is
6298 * correct : an input NaN should come out with its sign bit
6299 * flipped if it is a negated-input.
6300 */
6301 if (o1 == true) {
6302 gen_helper_vfp_negd(tcg_op3, tcg_op3);
6303 }
6304
6305 if (o0 != o1) {
6306 gen_helper_vfp_negd(tcg_op1, tcg_op1);
6307 }
6308
6309 gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
6310
6311 write_fp_dreg(s, rd, tcg_res);
6312
6313 tcg_temp_free_ptr(fpst);
6314 tcg_temp_free_i64(tcg_op1);
6315 tcg_temp_free_i64(tcg_op2);
6316 tcg_temp_free_i64(tcg_op3);
6317 tcg_temp_free_i64(tcg_res);
6318}
6319
95f9864f
RH
6320/* Floating-point data-processing (3 source) - half precision */
6321static void handle_fp_3src_half(DisasContext *s, bool o0, bool o1,
6322 int rd, int rn, int rm, int ra)
6323{
6324 TCGv_i32 tcg_op1, tcg_op2, tcg_op3;
6325 TCGv_i32 tcg_res = tcg_temp_new_i32();
6326 TCGv_ptr fpst = get_fpstatus_ptr(true);
6327
6328 tcg_op1 = read_fp_hreg(s, rn);
6329 tcg_op2 = read_fp_hreg(s, rm);
6330 tcg_op3 = read_fp_hreg(s, ra);
6331
6332 /* These are fused multiply-add, and must be done as one
6333 * floating point operation with no rounding between the
6334 * multiplication and addition steps.
6335 * NB that doing the negations here as separate steps is
6336 * correct : an input NaN should come out with its sign bit
6337 * flipped if it is a negated-input.
6338 */
6339 if (o1 == true) {
6340 tcg_gen_xori_i32(tcg_op3, tcg_op3, 0x8000);
6341 }
6342
6343 if (o0 != o1) {
6344 tcg_gen_xori_i32(tcg_op1, tcg_op1, 0x8000);
6345 }
6346
6347 gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
6348
6349 write_fp_sreg(s, rd, tcg_res);
6350
6351 tcg_temp_free_ptr(fpst);
6352 tcg_temp_free_i32(tcg_op1);
6353 tcg_temp_free_i32(tcg_op2);
6354 tcg_temp_free_i32(tcg_op3);
6355 tcg_temp_free_i32(tcg_res);
6356}
6357
4ce31af4 6358/* Floating point data-processing (3 source)
faa0ba46
PM
6359 * 31 30 29 28 24 23 22 21 20 16 15 14 10 9 5 4 0
6360 * +---+---+---+-----------+------+----+------+----+------+------+------+
6361 * | M | 0 | S | 1 1 1 1 1 | type | o1 | Rm | o0 | Ra | Rn | Rd |
6362 * +---+---+---+-----------+------+----+------+----+------+------+------+
6363 */
6364static void disas_fp_3src(DisasContext *s, uint32_t insn)
6365{
c1e20801 6366 int mos = extract32(insn, 29, 3);
6a30667f
AG
6367 int type = extract32(insn, 22, 2);
6368 int rd = extract32(insn, 0, 5);
6369 int rn = extract32(insn, 5, 5);
6370 int ra = extract32(insn, 10, 5);
6371 int rm = extract32(insn, 16, 5);
6372 bool o0 = extract32(insn, 15, 1);
6373 bool o1 = extract32(insn, 21, 1);
6374
c1e20801
PM
6375 if (mos) {
6376 unallocated_encoding(s);
6377 return;
6378 }
6379
6a30667f
AG
6380 switch (type) {
6381 case 0:
8c6afa6a
PM
6382 if (!fp_access_check(s)) {
6383 return;
6384 }
6a30667f
AG
6385 handle_fp_3src_single(s, o0, o1, rd, rn, rm, ra);
6386 break;
6387 case 1:
8c6afa6a
PM
6388 if (!fp_access_check(s)) {
6389 return;
6390 }
6a30667f
AG
6391 handle_fp_3src_double(s, o0, o1, rd, rn, rm, ra);
6392 break;
95f9864f 6393 case 3:
5763190f 6394 if (!dc_isar_feature(aa64_fp16, s)) {
95f9864f
RH
6395 unallocated_encoding(s);
6396 return;
6397 }
6398 if (!fp_access_check(s)) {
6399 return;
6400 }
6401 handle_fp_3src_half(s, o0, o1, rd, rn, rm, ra);
6402 break;
6a30667f
AG
6403 default:
6404 unallocated_encoding(s);
6405 }
faa0ba46
PM
6406}
6407
4ce31af4 6408/* Floating point immediate
faa0ba46
PM
6409 * 31 30 29 28 24 23 22 21 20 13 12 10 9 5 4 0
6410 * +---+---+---+-----------+------+---+------------+-------+------+------+
6411 * | M | 0 | S | 1 1 1 1 0 | type | 1 | imm8 | 1 0 0 | imm5 | Rd |
6412 * +---+---+---+-----------+------+---+------------+-------+------+------+
6413 */
6414static void disas_fp_imm(DisasContext *s, uint32_t insn)
6415{
6163f868 6416 int rd = extract32(insn, 0, 5);
c1e20801 6417 int imm5 = extract32(insn, 5, 5);
6163f868 6418 int imm8 = extract32(insn, 13, 8);
6ba28ddb 6419 int type = extract32(insn, 22, 2);
c1e20801 6420 int mos = extract32(insn, 29, 3);
6163f868
AG
6421 uint64_t imm;
6422 TCGv_i64 tcg_res;
14776ab5 6423 MemOp sz;
6163f868 6424
c1e20801
PM
6425 if (mos || imm5) {
6426 unallocated_encoding(s);
6427 return;
6428 }
6429
6ba28ddb
AB
6430 switch (type) {
6431 case 0:
6432 sz = MO_32;
6433 break;
6434 case 1:
6435 sz = MO_64;
6436 break;
6437 case 3:
6438 sz = MO_16;
5763190f 6439 if (dc_isar_feature(aa64_fp16, s)) {
6ba28ddb
AB
6440 break;
6441 }
6442 /* fallthru */
6443 default:
6163f868
AG
6444 unallocated_encoding(s);
6445 return;
6446 }
6447
8c6afa6a
PM
6448 if (!fp_access_check(s)) {
6449 return;
6450 }
6451
6ba28ddb 6452 imm = vfp_expand_imm(sz, imm8);
6163f868
AG
6453
6454 tcg_res = tcg_const_i64(imm);
6455 write_fp_dreg(s, rd, tcg_res);
6456 tcg_temp_free_i64(tcg_res);
faa0ba46
PM
6457}
6458
52a1f6a3
AG
6459/* Handle floating point <=> fixed point conversions. Note that we can
6460 * also deal with fp <=> integer conversions as a special case (scale == 64)
6461 * OPTME: consider handling that special case specially or at least skipping
6462 * the call to scalbn in the helpers for zero shifts.
6463 */
6464static void handle_fpfpcvt(DisasContext *s, int rd, int rn, int opcode,
6465 bool itof, int rmode, int scale, int sf, int type)
6466{
6467 bool is_signed = !(opcode & 1);
52a1f6a3 6468 TCGv_ptr tcg_fpstatus;
564a0632
RH
6469 TCGv_i32 tcg_shift, tcg_single;
6470 TCGv_i64 tcg_double;
52a1f6a3 6471
564a0632 6472 tcg_fpstatus = get_fpstatus_ptr(type == 3);
52a1f6a3
AG
6473
6474 tcg_shift = tcg_const_i32(64 - scale);
6475
6476 if (itof) {
6477 TCGv_i64 tcg_int = cpu_reg(s, rn);
6478 if (!sf) {
6479 TCGv_i64 tcg_extend = new_tmp_a64(s);
6480
6481 if (is_signed) {
6482 tcg_gen_ext32s_i64(tcg_extend, tcg_int);
6483 } else {
6484 tcg_gen_ext32u_i64(tcg_extend, tcg_int);
6485 }
6486
6487 tcg_int = tcg_extend;
6488 }
6489
564a0632
RH
6490 switch (type) {
6491 case 1: /* float64 */
6492 tcg_double = tcg_temp_new_i64();
52a1f6a3
AG
6493 if (is_signed) {
6494 gen_helper_vfp_sqtod(tcg_double, tcg_int,
6495 tcg_shift, tcg_fpstatus);
6496 } else {
6497 gen_helper_vfp_uqtod(tcg_double, tcg_int,
6498 tcg_shift, tcg_fpstatus);
6499 }
6500 write_fp_dreg(s, rd, tcg_double);
6501 tcg_temp_free_i64(tcg_double);
564a0632
RH
6502 break;
6503
6504 case 0: /* float32 */
6505 tcg_single = tcg_temp_new_i32();
52a1f6a3
AG
6506 if (is_signed) {
6507 gen_helper_vfp_sqtos(tcg_single, tcg_int,
6508 tcg_shift, tcg_fpstatus);
6509 } else {
6510 gen_helper_vfp_uqtos(tcg_single, tcg_int,
6511 tcg_shift, tcg_fpstatus);
6512 }
6513 write_fp_sreg(s, rd, tcg_single);
6514 tcg_temp_free_i32(tcg_single);
564a0632
RH
6515 break;
6516
6517 case 3: /* float16 */
6518 tcg_single = tcg_temp_new_i32();
6519 if (is_signed) {
6520 gen_helper_vfp_sqtoh(tcg_single, tcg_int,
6521 tcg_shift, tcg_fpstatus);
6522 } else {
6523 gen_helper_vfp_uqtoh(tcg_single, tcg_int,
6524 tcg_shift, tcg_fpstatus);
6525 }
6526 write_fp_sreg(s, rd, tcg_single);
6527 tcg_temp_free_i32(tcg_single);
6528 break;
6529
6530 default:
6531 g_assert_not_reached();
52a1f6a3
AG
6532 }
6533 } else {
6534 TCGv_i64 tcg_int = cpu_reg(s, rd);
6535 TCGv_i32 tcg_rmode;
6536
6537 if (extract32(opcode, 2, 1)) {
6538 /* There are too many rounding modes to all fit into rmode,
6539 * so FCVTA[US] is a special case.
6540 */
6541 rmode = FPROUNDING_TIEAWAY;
6542 }
6543
6544 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rmode));
6545
9b049916 6546 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
52a1f6a3 6547
564a0632
RH
6548 switch (type) {
6549 case 1: /* float64 */
6550 tcg_double = read_fp_dreg(s, rn);
52a1f6a3
AG
6551 if (is_signed) {
6552 if (!sf) {
6553 gen_helper_vfp_tosld(tcg_int, tcg_double,
6554 tcg_shift, tcg_fpstatus);
6555 } else {
6556 gen_helper_vfp_tosqd(tcg_int, tcg_double,
6557 tcg_shift, tcg_fpstatus);
6558 }
6559 } else {
6560 if (!sf) {
6561 gen_helper_vfp_tould(tcg_int, tcg_double,
6562 tcg_shift, tcg_fpstatus);
6563 } else {
6564 gen_helper_vfp_touqd(tcg_int, tcg_double,
6565 tcg_shift, tcg_fpstatus);
6566 }
6567 }
564a0632
RH
6568 if (!sf) {
6569 tcg_gen_ext32u_i64(tcg_int, tcg_int);
6570 }
52a1f6a3 6571 tcg_temp_free_i64(tcg_double);
564a0632
RH
6572 break;
6573
6574 case 0: /* float32 */
6575 tcg_single = read_fp_sreg(s, rn);
52a1f6a3
AG
6576 if (sf) {
6577 if (is_signed) {
6578 gen_helper_vfp_tosqs(tcg_int, tcg_single,
6579 tcg_shift, tcg_fpstatus);
6580 } else {
6581 gen_helper_vfp_touqs(tcg_int, tcg_single,
6582 tcg_shift, tcg_fpstatus);
6583 }
6584 } else {
6585 TCGv_i32 tcg_dest = tcg_temp_new_i32();
6586 if (is_signed) {
6587 gen_helper_vfp_tosls(tcg_dest, tcg_single,
6588 tcg_shift, tcg_fpstatus);
6589 } else {
6590 gen_helper_vfp_touls(tcg_dest, tcg_single,
6591 tcg_shift, tcg_fpstatus);
6592 }
6593 tcg_gen_extu_i32_i64(tcg_int, tcg_dest);
6594 tcg_temp_free_i32(tcg_dest);
6595 }
6596 tcg_temp_free_i32(tcg_single);
564a0632
RH
6597 break;
6598
6599 case 3: /* float16 */
6600 tcg_single = read_fp_sreg(s, rn);
6601 if (sf) {
6602 if (is_signed) {
6603 gen_helper_vfp_tosqh(tcg_int, tcg_single,
6604 tcg_shift, tcg_fpstatus);
6605 } else {
6606 gen_helper_vfp_touqh(tcg_int, tcg_single,
6607 tcg_shift, tcg_fpstatus);
6608 }
6609 } else {
6610 TCGv_i32 tcg_dest = tcg_temp_new_i32();
6611 if (is_signed) {
6612 gen_helper_vfp_toslh(tcg_dest, tcg_single,
6613 tcg_shift, tcg_fpstatus);
6614 } else {
6615 gen_helper_vfp_toulh(tcg_dest, tcg_single,
6616 tcg_shift, tcg_fpstatus);
6617 }
6618 tcg_gen_extu_i32_i64(tcg_int, tcg_dest);
6619 tcg_temp_free_i32(tcg_dest);
6620 }
6621 tcg_temp_free_i32(tcg_single);
6622 break;
6623
6624 default:
6625 g_assert_not_reached();
52a1f6a3
AG
6626 }
6627
9b049916 6628 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
52a1f6a3 6629 tcg_temp_free_i32(tcg_rmode);
52a1f6a3
AG
6630 }
6631
6632 tcg_temp_free_ptr(tcg_fpstatus);
6633 tcg_temp_free_i32(tcg_shift);
6634}
6635
4ce31af4 6636/* Floating point <-> fixed point conversions
faa0ba46
PM
6637 * 31 30 29 28 24 23 22 21 20 19 18 16 15 10 9 5 4 0
6638 * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
6639 * | sf | 0 | S | 1 1 1 1 0 | type | 0 | rmode | opcode | scale | Rn | Rd |
6640 * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
6641 */
6642static void disas_fp_fixed_conv(DisasContext *s, uint32_t insn)
6643{
52a1f6a3
AG
6644 int rd = extract32(insn, 0, 5);
6645 int rn = extract32(insn, 5, 5);
6646 int scale = extract32(insn, 10, 6);
6647 int opcode = extract32(insn, 16, 3);
6648 int rmode = extract32(insn, 19, 2);
6649 int type = extract32(insn, 22, 2);
6650 bool sbit = extract32(insn, 29, 1);
6651 bool sf = extract32(insn, 31, 1);
6652 bool itof;
6653
27527280
RH
6654 if (sbit || (!sf && scale < 32)) {
6655 unallocated_encoding(s);
6656 return;
6657 }
6658
6659 switch (type) {
6660 case 0: /* float32 */
6661 case 1: /* float64 */
6662 break;
6663 case 3: /* float16 */
5763190f 6664 if (dc_isar_feature(aa64_fp16, s)) {
27527280
RH
6665 break;
6666 }
6667 /* fallthru */
6668 default:
52a1f6a3
AG
6669 unallocated_encoding(s);
6670 return;
6671 }
6672
6673 switch ((rmode << 3) | opcode) {
6674 case 0x2: /* SCVTF */
6675 case 0x3: /* UCVTF */
6676 itof = true;
6677 break;
6678 case 0x18: /* FCVTZS */
6679 case 0x19: /* FCVTZU */
6680 itof = false;
6681 break;
6682 default:
6683 unallocated_encoding(s);
6684 return;
6685 }
6686
8c6afa6a
PM
6687 if (!fp_access_check(s)) {
6688 return;
6689 }
6690
52a1f6a3 6691 handle_fpfpcvt(s, rd, rn, opcode, itof, FPROUNDING_ZERO, scale, sf, type);
faa0ba46
PM
6692}
6693
ce5458e8
PM
6694static void handle_fmov(DisasContext *s, int rd, int rn, int type, bool itof)
6695{
6696 /* FMOV: gpr to or from float, double, or top half of quad fp reg,
6697 * without conversion.
6698 */
6699
6700 if (itof) {
ce5458e8 6701 TCGv_i64 tcg_rn = cpu_reg(s, rn);
9a9f1f59 6702 TCGv_i64 tmp;
ce5458e8
PM
6703
6704 switch (type) {
6705 case 0:
ce5458e8 6706 /* 32 bit */
9a9f1f59 6707 tmp = tcg_temp_new_i64();
ce5458e8 6708 tcg_gen_ext32u_i64(tmp, tcg_rn);
9a9f1f59 6709 write_fp_dreg(s, rd, tmp);
ce5458e8
PM
6710 tcg_temp_free_i64(tmp);
6711 break;
ce5458e8 6712 case 1:
ce5458e8 6713 /* 64 bit */
9a9f1f59 6714 write_fp_dreg(s, rd, tcg_rn);
ce5458e8 6715 break;
ce5458e8
PM
6716 case 2:
6717 /* 64 bit to top half. */
90e49638 6718 tcg_gen_st_i64(tcg_rn, cpu_env, fp_reg_hi_offset(s, rd));
9a9f1f59 6719 clear_vec_high(s, true, rd);
ce5458e8 6720 break;
68130236
RH
6721 case 3:
6722 /* 16 bit */
6723 tmp = tcg_temp_new_i64();
6724 tcg_gen_ext16u_i64(tmp, tcg_rn);
6725 write_fp_dreg(s, rd, tmp);
6726 tcg_temp_free_i64(tmp);
6727 break;
6728 default:
6729 g_assert_not_reached();
ce5458e8
PM
6730 }
6731 } else {
ce5458e8
PM
6732 TCGv_i64 tcg_rd = cpu_reg(s, rd);
6733
6734 switch (type) {
6735 case 0:
6736 /* 32 bit */
90e49638 6737 tcg_gen_ld32u_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_32));
ce5458e8 6738 break;
ce5458e8
PM
6739 case 1:
6740 /* 64 bit */
90e49638 6741 tcg_gen_ld_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_64));
e2f90565
PM
6742 break;
6743 case 2:
6744 /* 64 bits from top half */
90e49638 6745 tcg_gen_ld_i64(tcg_rd, cpu_env, fp_reg_hi_offset(s, rn));
ce5458e8 6746 break;
68130236
RH
6747 case 3:
6748 /* 16 bit */
6749 tcg_gen_ld16u_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_16));
6750 break;
6751 default:
6752 g_assert_not_reached();
ce5458e8
PM
6753 }
6754 }
6755}
6756
6c1f6f27
RH
6757static void handle_fjcvtzs(DisasContext *s, int rd, int rn)
6758{
6759 TCGv_i64 t = read_fp_dreg(s, rn);
6760 TCGv_ptr fpstatus = get_fpstatus_ptr(false);
6761
6762 gen_helper_fjcvtzs(t, t, fpstatus);
6763
6764 tcg_temp_free_ptr(fpstatus);
6765
6766 tcg_gen_ext32u_i64(cpu_reg(s, rd), t);
6767 tcg_gen_extrh_i64_i32(cpu_ZF, t);
6768 tcg_gen_movi_i32(cpu_CF, 0);
6769 tcg_gen_movi_i32(cpu_NF, 0);
6770 tcg_gen_movi_i32(cpu_VF, 0);
6771
6772 tcg_temp_free_i64(t);
6773}
6774
4ce31af4 6775/* Floating point <-> integer conversions
faa0ba46
PM
6776 * 31 30 29 28 24 23 22 21 20 19 18 16 15 10 9 5 4 0
6777 * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
c436d406 6778 * | sf | 0 | S | 1 1 1 1 0 | type | 1 | rmode | opc | 0 0 0 0 0 0 | Rn | Rd |
faa0ba46
PM
6779 * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
6780 */
6781static void disas_fp_int_conv(DisasContext *s, uint32_t insn)
6782{
ce5458e8
PM
6783 int rd = extract32(insn, 0, 5);
6784 int rn = extract32(insn, 5, 5);
6785 int opcode = extract32(insn, 16, 3);
6786 int rmode = extract32(insn, 19, 2);
6787 int type = extract32(insn, 22, 2);
6788 bool sbit = extract32(insn, 29, 1);
6789 bool sf = extract32(insn, 31, 1);
3c3ff684 6790 bool itof = false;
ce5458e8 6791
c436d406 6792 if (sbit) {
3c3ff684 6793 goto do_unallocated;
c436d406
WN
6794 }
6795
3c3ff684
RH
6796 switch (opcode) {
6797 case 2: /* SCVTF */
6798 case 3: /* UCVTF */
6799 itof = true;
6800 /* fallthru */
6801 case 4: /* FCVTAS */
6802 case 5: /* FCVTAU */
6803 if (rmode != 0) {
6804 goto do_unallocated;
c436d406 6805 }
3c3ff684
RH
6806 /* fallthru */
6807 case 0: /* FCVT[NPMZ]S */
6808 case 1: /* FCVT[NPMZ]U */
6809 switch (type) {
6810 case 0: /* float32 */
6811 case 1: /* float64 */
ce5458e8 6812 break;
3c3ff684
RH
6813 case 3: /* float16 */
6814 if (!dc_isar_feature(aa64_fp16, s)) {
6815 goto do_unallocated;
68130236 6816 }
3c3ff684 6817 break;
ce5458e8 6818 default:
3c3ff684 6819 goto do_unallocated;
ce5458e8 6820 }
8c6afa6a
PM
6821 if (!fp_access_check(s)) {
6822 return;
6823 }
3c3ff684
RH
6824 handle_fpfpcvt(s, rd, rn, opcode, itof, rmode, 64, sf, type);
6825 break;
c436d406 6826
3c3ff684
RH
6827 default:
6828 switch (sf << 7 | type << 5 | rmode << 3 | opcode) {
6829 case 0b01100110: /* FMOV half <-> 32-bit int */
6830 case 0b01100111:
6831 case 0b11100110: /* FMOV half <-> 64-bit int */
6832 case 0b11100111:
6833 if (!dc_isar_feature(aa64_fp16, s)) {
6834 goto do_unallocated;
564a0632
RH
6835 }
6836 /* fallthru */
3c3ff684
RH
6837 case 0b00000110: /* FMOV 32-bit */
6838 case 0b00000111:
6839 case 0b10100110: /* FMOV 64-bit */
6840 case 0b10100111:
6841 case 0b11001110: /* FMOV top half of 128-bit */
6842 case 0b11001111:
6843 if (!fp_access_check(s)) {
6844 return;
6845 }
6846 itof = opcode & 1;
6847 handle_fmov(s, rd, rn, type, itof);
6848 break;
6849
6c1f6f27
RH
6850 case 0b00111110: /* FJCVTZS */
6851 if (!dc_isar_feature(aa64_jscvt, s)) {
6852 goto do_unallocated;
6853 } else if (fp_access_check(s)) {
6854 handle_fjcvtzs(s, rd, rn);
6855 }
6856 break;
6857
564a0632 6858 default:
3c3ff684 6859 do_unallocated:
c436d406
WN
6860 unallocated_encoding(s);
6861 return;
6862 }
3c3ff684 6863 break;
ce5458e8 6864 }
faa0ba46
PM
6865}
6866
6867/* FP-specific subcases of table C3-6 (SIMD and FP data processing)
6868 * 31 30 29 28 25 24 0
6869 * +---+---+---+---------+-----------------------------+
6870 * | | 0 | | 1 1 1 1 | |
6871 * +---+---+---+---------+-----------------------------+
6872 */
6873static void disas_data_proc_fp(DisasContext *s, uint32_t insn)
6874{
6875 if (extract32(insn, 24, 1)) {
6876 /* Floating point data-processing (3 source) */
6877 disas_fp_3src(s, insn);
6878 } else if (extract32(insn, 21, 1) == 0) {
6879 /* Floating point to fixed point conversions */
6880 disas_fp_fixed_conv(s, insn);
6881 } else {
6882 switch (extract32(insn, 10, 2)) {
6883 case 1:
6884 /* Floating point conditional compare */
6885 disas_fp_ccomp(s, insn);
6886 break;
6887 case 2:
6888 /* Floating point data-processing (2 source) */
6889 disas_fp_2src(s, insn);
6890 break;
6891 case 3:
6892 /* Floating point conditional select */
6893 disas_fp_csel(s, insn);
6894 break;
6895 case 0:
6896 switch (ctz32(extract32(insn, 12, 4))) {
6897 case 0: /* [15:12] == xxx1 */
6898 /* Floating point immediate */
6899 disas_fp_imm(s, insn);
6900 break;
6901 case 1: /* [15:12] == xx10 */
6902 /* Floating point compare */
6903 disas_fp_compare(s, insn);
6904 break;
6905 case 2: /* [15:12] == x100 */
6906 /* Floating point data-processing (1 source) */
6907 disas_fp_1src(s, insn);
6908 break;
6909 case 3: /* [15:12] == 1000 */
6910 unallocated_encoding(s);
6911 break;
6912 default: /* [15:12] == 0000 */
6913 /* Floating point <-> integer conversions */
6914 disas_fp_int_conv(s, insn);
6915 break;
6916 }
6917 break;
6918 }
6919 }
6920}
6921
5c73747f
PM
6922static void do_ext64(DisasContext *s, TCGv_i64 tcg_left, TCGv_i64 tcg_right,
6923 int pos)
6924{
6925 /* Extract 64 bits from the middle of two concatenated 64 bit
6926 * vector register slices left:right. The extracted bits start
6927 * at 'pos' bits into the right (least significant) side.
6928 * We return the result in tcg_right, and guarantee not to
6929 * trash tcg_left.
6930 */
6931 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
6932 assert(pos > 0 && pos < 64);
6933
6934 tcg_gen_shri_i64(tcg_right, tcg_right, pos);
6935 tcg_gen_shli_i64(tcg_tmp, tcg_left, 64 - pos);
6936 tcg_gen_or_i64(tcg_right, tcg_right, tcg_tmp);
6937
6938 tcg_temp_free_i64(tcg_tmp);
6939}
6940
4ce31af4 6941/* EXT
384b26fb
AB
6942 * 31 30 29 24 23 22 21 20 16 15 14 11 10 9 5 4 0
6943 * +---+---+-------------+-----+---+------+---+------+---+------+------+
6944 * | 0 | Q | 1 0 1 1 1 0 | op2 | 0 | Rm | 0 | imm4 | 0 | Rn | Rd |
6945 * +---+---+-------------+-----+---+------+---+------+---+------+------+
6946 */
6947static void disas_simd_ext(DisasContext *s, uint32_t insn)
6948{
5c73747f
PM
6949 int is_q = extract32(insn, 30, 1);
6950 int op2 = extract32(insn, 22, 2);
6951 int imm4 = extract32(insn, 11, 4);
6952 int rm = extract32(insn, 16, 5);
6953 int rn = extract32(insn, 5, 5);
6954 int rd = extract32(insn, 0, 5);
6955 int pos = imm4 << 3;
6956 TCGv_i64 tcg_resl, tcg_resh;
6957
6958 if (op2 != 0 || (!is_q && extract32(imm4, 3, 1))) {
6959 unallocated_encoding(s);
6960 return;
6961 }
6962
8c6afa6a
PM
6963 if (!fp_access_check(s)) {
6964 return;
6965 }
6966
5c73747f
PM
6967 tcg_resh = tcg_temp_new_i64();
6968 tcg_resl = tcg_temp_new_i64();
6969
6970 /* Vd gets bits starting at pos bits into Vm:Vn. This is
6971 * either extracting 128 bits from a 128:128 concatenation, or
6972 * extracting 64 bits from a 64:64 concatenation.
6973 */
6974 if (!is_q) {
6975 read_vec_element(s, tcg_resl, rn, 0, MO_64);
6976 if (pos != 0) {
6977 read_vec_element(s, tcg_resh, rm, 0, MO_64);
6978 do_ext64(s, tcg_resh, tcg_resl, pos);
6979 }
6980 tcg_gen_movi_i64(tcg_resh, 0);
6981 } else {
6982 TCGv_i64 tcg_hh;
6983 typedef struct {
6984 int reg;
6985 int elt;
6986 } EltPosns;
6987 EltPosns eltposns[] = { {rn, 0}, {rn, 1}, {rm, 0}, {rm, 1} };
6988 EltPosns *elt = eltposns;
6989
6990 if (pos >= 64) {
6991 elt++;
6992 pos -= 64;
6993 }
6994
6995 read_vec_element(s, tcg_resl, elt->reg, elt->elt, MO_64);
6996 elt++;
6997 read_vec_element(s, tcg_resh, elt->reg, elt->elt, MO_64);
6998 elt++;
6999 if (pos != 0) {
7000 do_ext64(s, tcg_resh, tcg_resl, pos);
7001 tcg_hh = tcg_temp_new_i64();
7002 read_vec_element(s, tcg_hh, elt->reg, elt->elt, MO_64);
7003 do_ext64(s, tcg_hh, tcg_resh, pos);
7004 tcg_temp_free_i64(tcg_hh);
7005 }
7006 }
7007
7008 write_vec_element(s, tcg_resl, rd, 0, MO_64);
7009 tcg_temp_free_i64(tcg_resl);
7010 write_vec_element(s, tcg_resh, rd, 1, MO_64);
7011 tcg_temp_free_i64(tcg_resh);
78cedfab 7012 clear_vec_high(s, true, rd);
384b26fb
AB
7013}
7014
4ce31af4 7015/* TBL/TBX
384b26fb
AB
7016 * 31 30 29 24 23 22 21 20 16 15 14 13 12 11 10 9 5 4 0
7017 * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
7018 * | 0 | Q | 0 0 1 1 1 0 | op2 | 0 | Rm | 0 | len | op | 0 0 | Rn | Rd |
7019 * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
7020 */
7021static void disas_simd_tb(DisasContext *s, uint32_t insn)
7022{
7c51048f
MM
7023 int op2 = extract32(insn, 22, 2);
7024 int is_q = extract32(insn, 30, 1);
7025 int rm = extract32(insn, 16, 5);
7026 int rn = extract32(insn, 5, 5);
7027 int rd = extract32(insn, 0, 5);
7028 int is_tblx = extract32(insn, 12, 1);
7029 int len = extract32(insn, 13, 2);
7030 TCGv_i64 tcg_resl, tcg_resh, tcg_idx;
7031 TCGv_i32 tcg_regno, tcg_numregs;
7032
7033 if (op2 != 0) {
7034 unallocated_encoding(s);
7035 return;
7036 }
7037
8c6afa6a
PM
7038 if (!fp_access_check(s)) {
7039 return;
7040 }
7041
7c51048f
MM
7042 /* This does a table lookup: for every byte element in the input
7043 * we index into a table formed from up to four vector registers,
7044 * and then the output is the result of the lookups. Our helper
7045 * function does the lookup operation for a single 64 bit part of
7046 * the input.
7047 */
7048 tcg_resl = tcg_temp_new_i64();
7049 tcg_resh = tcg_temp_new_i64();
7050
7051 if (is_tblx) {
7052 read_vec_element(s, tcg_resl, rd, 0, MO_64);
7053 } else {
7054 tcg_gen_movi_i64(tcg_resl, 0);
7055 }
7056 if (is_tblx && is_q) {
7057 read_vec_element(s, tcg_resh, rd, 1, MO_64);
7058 } else {
7059 tcg_gen_movi_i64(tcg_resh, 0);
7060 }
7061
7062 tcg_idx = tcg_temp_new_i64();
7063 tcg_regno = tcg_const_i32(rn);
7064 tcg_numregs = tcg_const_i32(len + 1);
7065 read_vec_element(s, tcg_idx, rm, 0, MO_64);
7066 gen_helper_simd_tbl(tcg_resl, cpu_env, tcg_resl, tcg_idx,
7067 tcg_regno, tcg_numregs);
7068 if (is_q) {
7069 read_vec_element(s, tcg_idx, rm, 1, MO_64);
7070 gen_helper_simd_tbl(tcg_resh, cpu_env, tcg_resh, tcg_idx,
7071 tcg_regno, tcg_numregs);
7072 }
7073 tcg_temp_free_i64(tcg_idx);
7074 tcg_temp_free_i32(tcg_regno);
7075 tcg_temp_free_i32(tcg_numregs);
7076
7077 write_vec_element(s, tcg_resl, rd, 0, MO_64);
7078 tcg_temp_free_i64(tcg_resl);
7079 write_vec_element(s, tcg_resh, rd, 1, MO_64);
7080 tcg_temp_free_i64(tcg_resh);
263273bc 7081 clear_vec_high(s, true, rd);
384b26fb
AB
7082}
7083
4ce31af4 7084/* ZIP/UZP/TRN
384b26fb
AB
7085 * 31 30 29 24 23 22 21 20 16 15 14 12 11 10 9 5 4 0
7086 * +---+---+-------------+------+---+------+---+------------------+------+
7087 * | 0 | Q | 0 0 1 1 1 0 | size | 0 | Rm | 0 | opc | 1 0 | Rn | Rd |
7088 * +---+---+-------------+------+---+------+---+------------------+------+
7089 */
7090static void disas_simd_zip_trn(DisasContext *s, uint32_t insn)
7091{
5fa5469c
MM
7092 int rd = extract32(insn, 0, 5);
7093 int rn = extract32(insn, 5, 5);
7094 int rm = extract32(insn, 16, 5);
7095 int size = extract32(insn, 22, 2);
7096 /* opc field bits [1:0] indicate ZIP/UZP/TRN;
7097 * bit 2 indicates 1 vs 2 variant of the insn.
7098 */
7099 int opcode = extract32(insn, 12, 2);
7100 bool part = extract32(insn, 14, 1);
7101 bool is_q = extract32(insn, 30, 1);
7102 int esize = 8 << size;
7103 int i, ofs;
7104 int datasize = is_q ? 128 : 64;
7105 int elements = datasize / esize;
7106 TCGv_i64 tcg_res, tcg_resl, tcg_resh;
7107
7108 if (opcode == 0 || (size == 3 && !is_q)) {
7109 unallocated_encoding(s);
7110 return;
7111 }
7112
8c6afa6a
PM
7113 if (!fp_access_check(s)) {
7114 return;
7115 }
7116
5fa5469c
MM
7117 tcg_resl = tcg_const_i64(0);
7118 tcg_resh = tcg_const_i64(0);
7119 tcg_res = tcg_temp_new_i64();
7120
7121 for (i = 0; i < elements; i++) {
7122 switch (opcode) {
7123 case 1: /* UZP1/2 */
7124 {
7125 int midpoint = elements / 2;
7126 if (i < midpoint) {
7127 read_vec_element(s, tcg_res, rn, 2 * i + part, size);
7128 } else {
7129 read_vec_element(s, tcg_res, rm,
7130 2 * (i - midpoint) + part, size);
7131 }
7132 break;
7133 }
7134 case 2: /* TRN1/2 */
7135 if (i & 1) {
7136 read_vec_element(s, tcg_res, rm, (i & ~1) + part, size);
7137 } else {
7138 read_vec_element(s, tcg_res, rn, (i & ~1) + part, size);
7139 }
7140 break;
7141 case 3: /* ZIP1/2 */
7142 {
7143 int base = part * elements / 2;
7144 if (i & 1) {
7145 read_vec_element(s, tcg_res, rm, base + (i >> 1), size);
7146 } else {
7147 read_vec_element(s, tcg_res, rn, base + (i >> 1), size);
7148 }
7149 break;
7150 }
7151 default:
7152 g_assert_not_reached();
7153 }
7154
7155 ofs = i * esize;
7156 if (ofs < 64) {
7157 tcg_gen_shli_i64(tcg_res, tcg_res, ofs);
7158 tcg_gen_or_i64(tcg_resl, tcg_resl, tcg_res);
7159 } else {
7160 tcg_gen_shli_i64(tcg_res, tcg_res, ofs - 64);
7161 tcg_gen_or_i64(tcg_resh, tcg_resh, tcg_res);
7162 }
7163 }
7164
7165 tcg_temp_free_i64(tcg_res);
7166
7167 write_vec_element(s, tcg_resl, rd, 0, MO_64);
7168 tcg_temp_free_i64(tcg_resl);
7169 write_vec_element(s, tcg_resh, rd, 1, MO_64);
7170 tcg_temp_free_i64(tcg_resh);
33649de6 7171 clear_vec_high(s, true, rd);
384b26fb
AB
7172}
7173
807cdd50
AB
7174/*
7175 * do_reduction_op helper
7176 *
7177 * This mirrors the Reduce() pseudocode in the ARM ARM. It is
7178 * important for correct NaN propagation that we do these
7179 * operations in exactly the order specified by the pseudocode.
7180 *
7181 * This is a recursive function, TCG temps should be freed by the
7182 * calling function once it is done with the values.
7183 */
7184static TCGv_i32 do_reduction_op(DisasContext *s, int fpopcode, int rn,
7185 int esize, int size, int vmap, TCGv_ptr fpst)
7186{
7187 if (esize == size) {
7188 int element;
14776ab5 7189 MemOp msize = esize == 16 ? MO_16 : MO_32;
807cdd50
AB
7190 TCGv_i32 tcg_elem;
7191
7192 /* We should have one register left here */
7193 assert(ctpop8(vmap) == 1);
7194 element = ctz32(vmap);
7195 assert(element < 8);
7196
7197 tcg_elem = tcg_temp_new_i32();
7198 read_vec_element_i32(s, tcg_elem, rn, element, msize);
7199 return tcg_elem;
4a0ff1ce 7200 } else {
807cdd50
AB
7201 int bits = size / 2;
7202 int shift = ctpop8(vmap) / 2;
7203 int vmap_lo = (vmap >> shift) & vmap;
7204 int vmap_hi = (vmap & ~vmap_lo);
7205 TCGv_i32 tcg_hi, tcg_lo, tcg_res;
7206
7207 tcg_hi = do_reduction_op(s, fpopcode, rn, esize, bits, vmap_hi, fpst);
7208 tcg_lo = do_reduction_op(s, fpopcode, rn, esize, bits, vmap_lo, fpst);
7209 tcg_res = tcg_temp_new_i32();
7210
7211 switch (fpopcode) {
7212 case 0x0c: /* fmaxnmv half-precision */
7213 gen_helper_advsimd_maxnumh(tcg_res, tcg_lo, tcg_hi, fpst);
7214 break;
7215 case 0x0f: /* fmaxv half-precision */
7216 gen_helper_advsimd_maxh(tcg_res, tcg_lo, tcg_hi, fpst);
7217 break;
7218 case 0x1c: /* fminnmv half-precision */
7219 gen_helper_advsimd_minnumh(tcg_res, tcg_lo, tcg_hi, fpst);
7220 break;
7221 case 0x1f: /* fminv half-precision */
7222 gen_helper_advsimd_minh(tcg_res, tcg_lo, tcg_hi, fpst);
7223 break;
7224 case 0x2c: /* fmaxnmv */
7225 gen_helper_vfp_maxnums(tcg_res, tcg_lo, tcg_hi, fpst);
7226 break;
7227 case 0x2f: /* fmaxv */
7228 gen_helper_vfp_maxs(tcg_res, tcg_lo, tcg_hi, fpst);
7229 break;
7230 case 0x3c: /* fminnmv */
7231 gen_helper_vfp_minnums(tcg_res, tcg_lo, tcg_hi, fpst);
7232 break;
7233 case 0x3f: /* fminv */
7234 gen_helper_vfp_mins(tcg_res, tcg_lo, tcg_hi, fpst);
7235 break;
7236 default:
7237 g_assert_not_reached();
4a0ff1ce 7238 }
807cdd50
AB
7239
7240 tcg_temp_free_i32(tcg_hi);
7241 tcg_temp_free_i32(tcg_lo);
7242 return tcg_res;
4a0ff1ce
MM
7243 }
7244}
7245
4ce31af4 7246/* AdvSIMD across lanes
384b26fb
AB
7247 * 31 30 29 28 24 23 22 21 17 16 12 11 10 9 5 4 0
7248 * +---+---+---+-----------+------+-----------+--------+-----+------+------+
7249 * | 0 | Q | U | 0 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 | Rn | Rd |
7250 * +---+---+---+-----------+------+-----------+--------+-----+------+------+
7251 */
7252static void disas_simd_across_lanes(DisasContext *s, uint32_t insn)
7253{
4a0ff1ce
MM
7254 int rd = extract32(insn, 0, 5);
7255 int rn = extract32(insn, 5, 5);
7256 int size = extract32(insn, 22, 2);
7257 int opcode = extract32(insn, 12, 5);
7258 bool is_q = extract32(insn, 30, 1);
7259 bool is_u = extract32(insn, 29, 1);
7260 bool is_fp = false;
7261 bool is_min = false;
7262 int esize;
7263 int elements;
7264 int i;
7265 TCGv_i64 tcg_res, tcg_elt;
7266
7267 switch (opcode) {
7268 case 0x1b: /* ADDV */
7269 if (is_u) {
7270 unallocated_encoding(s);
7271 return;
7272 }
7273 /* fall through */
7274 case 0x3: /* SADDLV, UADDLV */
7275 case 0xa: /* SMAXV, UMAXV */
7276 case 0x1a: /* SMINV, UMINV */
7277 if (size == 3 || (size == 2 && !is_q)) {
7278 unallocated_encoding(s);
7279 return;
7280 }
7281 break;
7282 case 0xc: /* FMAXNMV, FMINNMV */
7283 case 0xf: /* FMAXV, FMINV */
807cdd50
AB
7284 /* Bit 1 of size field encodes min vs max and the actual size
7285 * depends on the encoding of the U bit. If not set (and FP16
7286 * enabled) then we do half-precision float instead of single
7287 * precision.
4a0ff1ce
MM
7288 */
7289 is_min = extract32(size, 1, 1);
7290 is_fp = true;
5763190f 7291 if (!is_u && dc_isar_feature(aa64_fp16, s)) {
807cdd50
AB
7292 size = 1;
7293 } else if (!is_u || !is_q || extract32(size, 0, 1)) {
7294 unallocated_encoding(s);
7295 return;
7296 } else {
7297 size = 2;
7298 }
4a0ff1ce
MM
7299 break;
7300 default:
7301 unallocated_encoding(s);
7302 return;
7303 }
7304
8c6afa6a
PM
7305 if (!fp_access_check(s)) {
7306 return;
7307 }
7308
4a0ff1ce
MM
7309 esize = 8 << size;
7310 elements = (is_q ? 128 : 64) / esize;
7311
7312 tcg_res = tcg_temp_new_i64();
7313 tcg_elt = tcg_temp_new_i64();
7314
7315 /* These instructions operate across all lanes of a vector
7316 * to produce a single result. We can guarantee that a 64
7317 * bit intermediate is sufficient:
7318 * + for [US]ADDLV the maximum element size is 32 bits, and
7319 * the result type is 64 bits
7320 * + for FMAX*V, FMIN*V, ADDV the intermediate type is the
7321 * same as the element size, which is 32 bits at most
7322 * For the integer operations we can choose to work at 64
7323 * or 32 bits and truncate at the end; for simplicity
7324 * we use 64 bits always. The floating point
7325 * ops do require 32 bit intermediates, though.
7326 */
7327 if (!is_fp) {
7328 read_vec_element(s, tcg_res, rn, 0, size | (is_u ? 0 : MO_SIGN));
7329
7330 for (i = 1; i < elements; i++) {
7331 read_vec_element(s, tcg_elt, rn, i, size | (is_u ? 0 : MO_SIGN));
7332
7333 switch (opcode) {
7334 case 0x03: /* SADDLV / UADDLV */
7335 case 0x1b: /* ADDV */
7336 tcg_gen_add_i64(tcg_res, tcg_res, tcg_elt);
7337 break;
7338 case 0x0a: /* SMAXV / UMAXV */
ecb8ab8d
RH
7339 if (is_u) {
7340 tcg_gen_umax_i64(tcg_res, tcg_res, tcg_elt);
7341 } else {
7342 tcg_gen_smax_i64(tcg_res, tcg_res, tcg_elt);
7343 }
4a0ff1ce
MM
7344 break;
7345 case 0x1a: /* SMINV / UMINV */
ecb8ab8d
RH
7346 if (is_u) {
7347 tcg_gen_umin_i64(tcg_res, tcg_res, tcg_elt);
7348 } else {
7349 tcg_gen_smin_i64(tcg_res, tcg_res, tcg_elt);
7350 }
4a0ff1ce
MM
7351 break;
7352 default:
7353 g_assert_not_reached();
7354 }
7355
7356 }
7357 } else {
807cdd50
AB
7358 /* Floating point vector reduction ops which work across 32
7359 * bit (single) or 16 bit (half-precision) intermediates.
4a0ff1ce
MM
7360 * Note that correct NaN propagation requires that we do these
7361 * operations in exactly the order specified by the pseudocode.
7362 */
807cdd50
AB
7363 TCGv_ptr fpst = get_fpstatus_ptr(size == MO_16);
7364 int fpopcode = opcode | is_min << 4 | is_u << 5;
7365 int vmap = (1 << elements) - 1;
7366 TCGv_i32 tcg_res32 = do_reduction_op(s, fpopcode, rn, esize,
7367 (is_q ? 128 : 64), vmap, fpst);
7368 tcg_gen_extu_i32_i64(tcg_res, tcg_res32);
7369 tcg_temp_free_i32(tcg_res32);
4a0ff1ce
MM
7370 tcg_temp_free_ptr(fpst);
7371 }
7372
7373 tcg_temp_free_i64(tcg_elt);
7374
7375 /* Now truncate the result to the width required for the final output */
7376 if (opcode == 0x03) {
7377 /* SADDLV, UADDLV: result is 2*esize */
7378 size++;
7379 }
7380
7381 switch (size) {
7382 case 0:
7383 tcg_gen_ext8u_i64(tcg_res, tcg_res);
7384 break;
7385 case 1:
7386 tcg_gen_ext16u_i64(tcg_res, tcg_res);
7387 break;
7388 case 2:
7389 tcg_gen_ext32u_i64(tcg_res, tcg_res);
7390 break;
7391 case 3:
7392 break;
7393 default:
7394 g_assert_not_reached();
7395 }
7396
7397 write_fp_dreg(s, rd, tcg_res);
7398 tcg_temp_free_i64(tcg_res);
384b26fb
AB
7399}
7400
4ce31af4 7401/* DUP (Element, Vector)
67bb9389
AB
7402 *
7403 * 31 30 29 21 20 16 15 10 9 5 4 0
7404 * +---+---+-------------------+--------+-------------+------+------+
7405 * | 0 | Q | 0 0 1 1 1 0 0 0 0 | imm5 | 0 0 0 0 0 1 | Rn | Rd |
7406 * +---+---+-------------------+--------+-------------+------+------+
7407 *
7408 * size: encoded in imm5 (see ARM ARM LowestSetBit())
7409 */
7410static void handle_simd_dupe(DisasContext *s, int is_q, int rd, int rn,
7411 int imm5)
7412{
7413 int size = ctz32(imm5);
861a1ded 7414 int index = imm5 >> (size + 1);
67bb9389
AB
7415
7416 if (size > 3 || (size == 3 && !is_q)) {
7417 unallocated_encoding(s);
7418 return;
7419 }
7420
8c6afa6a
PM
7421 if (!fp_access_check(s)) {
7422 return;
7423 }
7424
861a1ded
RH
7425 tcg_gen_gvec_dup_mem(size, vec_full_reg_offset(s, rd),
7426 vec_reg_offset(s, rn, index, size),
7427 is_q ? 16 : 8, vec_full_reg_size(s));
67bb9389
AB
7428}
7429
4ce31af4 7430/* DUP (element, scalar)
360a6f2d
PM
7431 * 31 21 20 16 15 10 9 5 4 0
7432 * +-----------------------+--------+-------------+------+------+
7433 * | 0 1 0 1 1 1 1 0 0 0 0 | imm5 | 0 0 0 0 0 1 | Rn | Rd |
7434 * +-----------------------+--------+-------------+------+------+
7435 */
7436static void handle_simd_dupes(DisasContext *s, int rd, int rn,
7437 int imm5)
7438{
7439 int size = ctz32(imm5);
7440 int index;
7441 TCGv_i64 tmp;
7442
7443 if (size > 3) {
7444 unallocated_encoding(s);
7445 return;
7446 }
7447
8c6afa6a
PM
7448 if (!fp_access_check(s)) {
7449 return;
7450 }
7451
360a6f2d
PM
7452 index = imm5 >> (size + 1);
7453
7454 /* This instruction just extracts the specified element and
7455 * zero-extends it into the bottom of the destination register.
7456 */
7457 tmp = tcg_temp_new_i64();
7458 read_vec_element(s, tmp, rn, index, size);
7459 write_fp_dreg(s, rd, tmp);
7460 tcg_temp_free_i64(tmp);
7461}
7462
4ce31af4 7463/* DUP (General)
67bb9389
AB
7464 *
7465 * 31 30 29 21 20 16 15 10 9 5 4 0
7466 * +---+---+-------------------+--------+-------------+------+------+
7467 * | 0 | Q | 0 0 1 1 1 0 0 0 0 | imm5 | 0 0 0 0 1 1 | Rn | Rd |
7468 * +---+---+-------------------+--------+-------------+------+------+
7469 *
7470 * size: encoded in imm5 (see ARM ARM LowestSetBit())
7471 */
7472static void handle_simd_dupg(DisasContext *s, int is_q, int rd, int rn,
7473 int imm5)
7474{
7475 int size = ctz32(imm5);
861a1ded 7476 uint32_t dofs, oprsz, maxsz;
67bb9389
AB
7477
7478 if (size > 3 || ((size == 3) && !is_q)) {
7479 unallocated_encoding(s);
7480 return;
7481 }
8c6afa6a
PM
7482
7483 if (!fp_access_check(s)) {
7484 return;
7485 }
7486
861a1ded
RH
7487 dofs = vec_full_reg_offset(s, rd);
7488 oprsz = is_q ? 16 : 8;
7489 maxsz = vec_full_reg_size(s);
7490
7491 tcg_gen_gvec_dup_i64(size, dofs, oprsz, maxsz, cpu_reg(s, rn));
67bb9389
AB
7492}
7493
4ce31af4 7494/* INS (Element)
67bb9389
AB
7495 *
7496 * 31 21 20 16 15 14 11 10 9 5 4 0
7497 * +-----------------------+--------+------------+---+------+------+
7498 * | 0 1 1 0 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 | Rn | Rd |
7499 * +-----------------------+--------+------------+---+------+------+
7500 *
7501 * size: encoded in imm5 (see ARM ARM LowestSetBit())
7502 * index: encoded in imm5<4:size+1>
7503 */
7504static void handle_simd_inse(DisasContext *s, int rd, int rn,
7505 int imm4, int imm5)
7506{
7507 int size = ctz32(imm5);
7508 int src_index, dst_index;
7509 TCGv_i64 tmp;
7510
7511 if (size > 3) {
7512 unallocated_encoding(s);
7513 return;
7514 }
8c6afa6a
PM
7515
7516 if (!fp_access_check(s)) {
7517 return;
7518 }
7519
67bb9389
AB
7520 dst_index = extract32(imm5, 1+size, 5);
7521 src_index = extract32(imm4, size, 4);
7522
7523 tmp = tcg_temp_new_i64();
7524
7525 read_vec_element(s, tmp, rn, src_index, size);
7526 write_vec_element(s, tmp, rd, dst_index, size);
7527
7528 tcg_temp_free_i64(tmp);
528dc354
RH
7529
7530 /* INS is considered a 128-bit write for SVE. */
7531 clear_vec_high(s, true, rd);
67bb9389
AB
7532}
7533
7534
4ce31af4 7535/* INS (General)
67bb9389
AB
7536 *
7537 * 31 21 20 16 15 10 9 5 4 0
7538 * +-----------------------+--------+-------------+------+------+
7539 * | 0 1 0 0 1 1 1 0 0 0 0 | imm5 | 0 0 0 1 1 1 | Rn | Rd |
7540 * +-----------------------+--------+-------------+------+------+
7541 *
7542 * size: encoded in imm5 (see ARM ARM LowestSetBit())
7543 * index: encoded in imm5<4:size+1>
7544 */
7545static void handle_simd_insg(DisasContext *s, int rd, int rn, int imm5)
7546{
7547 int size = ctz32(imm5);
7548 int idx;
7549
7550 if (size > 3) {
7551 unallocated_encoding(s);
7552 return;
7553 }
7554
8c6afa6a
PM
7555 if (!fp_access_check(s)) {
7556 return;
7557 }
7558
67bb9389
AB
7559 idx = extract32(imm5, 1 + size, 4 - size);
7560 write_vec_element(s, cpu_reg(s, rn), rd, idx, size);
528dc354
RH
7561
7562 /* INS is considered a 128-bit write for SVE. */
7563 clear_vec_high(s, true, rd);
67bb9389
AB
7564}
7565
7566/*
4ce31af4
PM
7567 * UMOV (General)
7568 * SMOV (General)
67bb9389
AB
7569 *
7570 * 31 30 29 21 20 16 15 12 10 9 5 4 0
7571 * +---+---+-------------------+--------+-------------+------+------+
7572 * | 0 | Q | 0 0 1 1 1 0 0 0 0 | imm5 | 0 0 1 U 1 1 | Rn | Rd |
7573 * +---+---+-------------------+--------+-------------+------+------+
7574 *
7575 * U: unsigned when set
7576 * size: encoded in imm5 (see ARM ARM LowestSetBit())
7577 */
7578static void handle_simd_umov_smov(DisasContext *s, int is_q, int is_signed,
7579 int rn, int rd, int imm5)
7580{
7581 int size = ctz32(imm5);
7582 int element;
7583 TCGv_i64 tcg_rd;
7584
7585 /* Check for UnallocatedEncodings */
7586 if (is_signed) {
7587 if (size > 2 || (size == 2 && !is_q)) {
7588 unallocated_encoding(s);
7589 return;
7590 }
7591 } else {
7592 if (size > 3
7593 || (size < 3 && is_q)
7594 || (size == 3 && !is_q)) {
7595 unallocated_encoding(s);
7596 return;
7597 }
7598 }
8c6afa6a
PM
7599
7600 if (!fp_access_check(s)) {
7601 return;
7602 }
7603
67bb9389
AB
7604 element = extract32(imm5, 1+size, 4);
7605
7606 tcg_rd = cpu_reg(s, rd);
7607 read_vec_element(s, tcg_rd, rn, element, size | (is_signed ? MO_SIGN : 0));
7608 if (is_signed && !is_q) {
7609 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
7610 }
7611}
7612
4ce31af4 7613/* AdvSIMD copy
384b26fb
AB
7614 * 31 30 29 28 21 20 16 15 14 11 10 9 5 4 0
7615 * +---+---+----+-----------------+------+---+------+---+------+------+
7616 * | 0 | Q | op | 0 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 | Rn | Rd |
7617 * +---+---+----+-----------------+------+---+------+---+------+------+
7618 */
7619static void disas_simd_copy(DisasContext *s, uint32_t insn)
7620{
67bb9389
AB
7621 int rd = extract32(insn, 0, 5);
7622 int rn = extract32(insn, 5, 5);
7623 int imm4 = extract32(insn, 11, 4);
7624 int op = extract32(insn, 29, 1);
7625 int is_q = extract32(insn, 30, 1);
7626 int imm5 = extract32(insn, 16, 5);
7627
7628 if (op) {
7629 if (is_q) {
7630 /* INS (element) */
7631 handle_simd_inse(s, rd, rn, imm4, imm5);
7632 } else {
7633 unallocated_encoding(s);
7634 }
7635 } else {
7636 switch (imm4) {
7637 case 0:
7638 /* DUP (element - vector) */
7639 handle_simd_dupe(s, is_q, rd, rn, imm5);
7640 break;
7641 case 1:
7642 /* DUP (general) */
7643 handle_simd_dupg(s, is_q, rd, rn, imm5);
7644 break;
7645 case 3:
7646 if (is_q) {
7647 /* INS (general) */
7648 handle_simd_insg(s, rd, rn, imm5);
7649 } else {
7650 unallocated_encoding(s);
7651 }
7652 break;
7653 case 5:
7654 case 7:
7655 /* UMOV/SMOV (is_q indicates 32/64; imm4 indicates signedness) */
7656 handle_simd_umov_smov(s, is_q, (imm4 == 5), rn, rd, imm5);
7657 break;
7658 default:
7659 unallocated_encoding(s);
7660 break;
7661 }
7662 }
384b26fb
AB
7663}
7664
4ce31af4 7665/* AdvSIMD modified immediate
384b26fb
AB
7666 * 31 30 29 28 19 18 16 15 12 11 10 9 5 4 0
7667 * +---+---+----+---------------------+-----+-------+----+---+-------+------+
7668 * | 0 | Q | op | 0 1 1 1 1 0 0 0 0 0 | abc | cmode | o2 | 1 | defgh | Rd |
7669 * +---+---+----+---------------------+-----+-------+----+---+-------+------+
f3f8c4f4
AB
7670 *
7671 * There are a number of operations that can be carried out here:
7672 * MOVI - move (shifted) imm into register
7673 * MVNI - move inverted (shifted) imm into register
7674 * ORR - bitwise OR of (shifted) imm with register
7675 * BIC - bitwise clear of (shifted) imm with register
70b4e6a4
AB
7676 * With ARMv8.2 we also have:
7677 * FMOV half-precision
384b26fb
AB
7678 */
7679static void disas_simd_mod_imm(DisasContext *s, uint32_t insn)
7680{
f3f8c4f4
AB
7681 int rd = extract32(insn, 0, 5);
7682 int cmode = extract32(insn, 12, 4);
7683 int cmode_3_1 = extract32(cmode, 1, 3);
7684 int cmode_0 = extract32(cmode, 0, 1);
7685 int o2 = extract32(insn, 11, 1);
7686 uint64_t abcdefgh = extract32(insn, 5, 5) | (extract32(insn, 16, 3) << 5);
7687 bool is_neg = extract32(insn, 29, 1);
7688 bool is_q = extract32(insn, 30, 1);
7689 uint64_t imm = 0;
f3f8c4f4
AB
7690
7691 if (o2 != 0 || ((cmode == 0xf) && is_neg && !is_q)) {
70b4e6a4 7692 /* Check for FMOV (vector, immediate) - half-precision */
5763190f 7693 if (!(dc_isar_feature(aa64_fp16, s) && o2 && cmode == 0xf)) {
70b4e6a4
AB
7694 unallocated_encoding(s);
7695 return;
7696 }
f3f8c4f4
AB
7697 }
7698
8c6afa6a
PM
7699 if (!fp_access_check(s)) {
7700 return;
7701 }
7702
f3f8c4f4
AB
7703 /* See AdvSIMDExpandImm() in ARM ARM */
7704 switch (cmode_3_1) {
7705 case 0: /* Replicate(Zeros(24):imm8, 2) */
7706 case 1: /* Replicate(Zeros(16):imm8:Zeros(8), 2) */
7707 case 2: /* Replicate(Zeros(8):imm8:Zeros(16), 2) */
7708 case 3: /* Replicate(imm8:Zeros(24), 2) */
7709 {
7710 int shift = cmode_3_1 * 8;
7711 imm = bitfield_replicate(abcdefgh << shift, 32);
7712 break;
7713 }
7714 case 4: /* Replicate(Zeros(8):imm8, 4) */
7715 case 5: /* Replicate(imm8:Zeros(8), 4) */
7716 {
7717 int shift = (cmode_3_1 & 0x1) * 8;
7718 imm = bitfield_replicate(abcdefgh << shift, 16);
7719 break;
7720 }
7721 case 6:
7722 if (cmode_0) {
7723 /* Replicate(Zeros(8):imm8:Ones(16), 2) */
7724 imm = (abcdefgh << 16) | 0xffff;
7725 } else {
7726 /* Replicate(Zeros(16):imm8:Ones(8), 2) */
7727 imm = (abcdefgh << 8) | 0xff;
7728 }
7729 imm = bitfield_replicate(imm, 32);
7730 break;
7731 case 7:
7732 if (!cmode_0 && !is_neg) {
7733 imm = bitfield_replicate(abcdefgh, 8);
7734 } else if (!cmode_0 && is_neg) {
7735 int i;
7736 imm = 0;
7737 for (i = 0; i < 8; i++) {
7738 if ((abcdefgh) & (1 << i)) {
7739 imm |= 0xffULL << (i * 8);
7740 }
7741 }
7742 } else if (cmode_0) {
7743 if (is_neg) {
7744 imm = (abcdefgh & 0x3f) << 48;
7745 if (abcdefgh & 0x80) {
7746 imm |= 0x8000000000000000ULL;
7747 }
7748 if (abcdefgh & 0x40) {
7749 imm |= 0x3fc0000000000000ULL;
7750 } else {
7751 imm |= 0x4000000000000000ULL;
7752 }
7753 } else {
70b4e6a4
AB
7754 if (o2) {
7755 /* FMOV (vector, immediate) - half-precision */
7756 imm = vfp_expand_imm(MO_16, abcdefgh);
7757 /* now duplicate across the lanes */
7758 imm = bitfield_replicate(imm, 16);
f3f8c4f4 7759 } else {
70b4e6a4
AB
7760 imm = (abcdefgh & 0x3f) << 19;
7761 if (abcdefgh & 0x80) {
7762 imm |= 0x80000000;
7763 }
7764 if (abcdefgh & 0x40) {
7765 imm |= 0x3e000000;
7766 } else {
7767 imm |= 0x40000000;
7768 }
7769 imm |= (imm << 32);
f3f8c4f4 7770 }
f3f8c4f4
AB
7771 }
7772 }
7773 break;
70b4e6a4
AB
7774 default:
7775 fprintf(stderr, "%s: cmode_3_1: %x\n", __func__, cmode_3_1);
7776 g_assert_not_reached();
f3f8c4f4
AB
7777 }
7778
7779 if (cmode_3_1 != 7 && is_neg) {
7780 imm = ~imm;
7781 }
7782
861a1ded
RH
7783 if (!((cmode & 0x9) == 0x1 || (cmode & 0xd) == 0x9)) {
7784 /* MOVI or MVNI, with MVNI negation handled above. */
7785 tcg_gen_gvec_dup64i(vec_full_reg_offset(s, rd), is_q ? 16 : 8,
7786 vec_full_reg_size(s), imm);
7787 } else {
064e265d
RH
7788 /* ORR or BIC, with BIC negation to AND handled above. */
7789 if (is_neg) {
7790 gen_gvec_fn2i(s, is_q, rd, rd, imm, tcg_gen_gvec_andi, MO_64);
7791 } else {
7792 gen_gvec_fn2i(s, is_q, rd, rd, imm, tcg_gen_gvec_ori, MO_64);
f3f8c4f4 7793 }
861a1ded 7794 }
384b26fb
AB
7795}
7796
4ce31af4 7797/* AdvSIMD scalar copy
384b26fb
AB
7798 * 31 30 29 28 21 20 16 15 14 11 10 9 5 4 0
7799 * +-----+----+-----------------+------+---+------+---+------+------+
7800 * | 0 1 | op | 1 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 | Rn | Rd |
7801 * +-----+----+-----------------+------+---+------+---+------+------+
7802 */
7803static void disas_simd_scalar_copy(DisasContext *s, uint32_t insn)
7804{
360a6f2d
PM
7805 int rd = extract32(insn, 0, 5);
7806 int rn = extract32(insn, 5, 5);
7807 int imm4 = extract32(insn, 11, 4);
7808 int imm5 = extract32(insn, 16, 5);
7809 int op = extract32(insn, 29, 1);
7810
7811 if (op != 0 || imm4 != 0) {
7812 unallocated_encoding(s);
7813 return;
7814 }
7815
7816 /* DUP (element, scalar) */
7817 handle_simd_dupes(s, rd, rn, imm5);
384b26fb
AB
7818}
7819
4ce31af4 7820/* AdvSIMD scalar pairwise
384b26fb
AB
7821 * 31 30 29 28 24 23 22 21 17 16 12 11 10 9 5 4 0
7822 * +-----+---+-----------+------+-----------+--------+-----+------+------+
7823 * | 0 1 | U | 1 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 | Rn | Rd |
7824 * +-----+---+-----------+------+-----------+--------+-----+------+------+
7825 */
7826static void disas_simd_scalar_pairwise(DisasContext *s, uint32_t insn)
7827{
3720a7ea
PM
7828 int u = extract32(insn, 29, 1);
7829 int size = extract32(insn, 22, 2);
7830 int opcode = extract32(insn, 12, 5);
7831 int rn = extract32(insn, 5, 5);
7832 int rd = extract32(insn, 0, 5);
7833 TCGv_ptr fpst;
7834
7835 /* For some ops (the FP ones), size[1] is part of the encoding.
7836 * For ADDP strictly it is not but size[1] is always 1 for valid
7837 * encodings.
7838 */
7839 opcode |= (extract32(size, 1, 1) << 5);
7840
7841 switch (opcode) {
7842 case 0x3b: /* ADDP */
7843 if (u || size != 3) {
7844 unallocated_encoding(s);
7845 return;
7846 }
8c6afa6a
PM
7847 if (!fp_access_check(s)) {
7848 return;
7849 }
7850
f764718d 7851 fpst = NULL;
3720a7ea
PM
7852 break;
7853 case 0xc: /* FMAXNMP */
7854 case 0xd: /* FADDP */
7855 case 0xf: /* FMAXP */
7856 case 0x2c: /* FMINNMP */
7857 case 0x2f: /* FMINP */
5c36d895 7858 /* FP op, size[0] is 32 or 64 bit*/
3720a7ea 7859 if (!u) {
5763190f 7860 if (!dc_isar_feature(aa64_fp16, s)) {
5c36d895
AB
7861 unallocated_encoding(s);
7862 return;
7863 } else {
7864 size = MO_16;
7865 }
7866 } else {
7867 size = extract32(size, 0, 1) ? MO_64 : MO_32;
3720a7ea 7868 }
5c36d895 7869
8c6afa6a
PM
7870 if (!fp_access_check(s)) {
7871 return;
7872 }
7873
5c36d895 7874 fpst = get_fpstatus_ptr(size == MO_16);
3720a7ea
PM
7875 break;
7876 default:
7877 unallocated_encoding(s);
7878 return;
7879 }
7880
5c36d895 7881 if (size == MO_64) {
3720a7ea
PM
7882 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
7883 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
7884 TCGv_i64 tcg_res = tcg_temp_new_i64();
7885
7886 read_vec_element(s, tcg_op1, rn, 0, MO_64);
7887 read_vec_element(s, tcg_op2, rn, 1, MO_64);
7888
7889 switch (opcode) {
7890 case 0x3b: /* ADDP */
7891 tcg_gen_add_i64(tcg_res, tcg_op1, tcg_op2);
7892 break;
7893 case 0xc: /* FMAXNMP */
7894 gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
7895 break;
7896 case 0xd: /* FADDP */
7897 gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
7898 break;
7899 case 0xf: /* FMAXP */
7900 gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
7901 break;
7902 case 0x2c: /* FMINNMP */
7903 gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
7904 break;
7905 case 0x2f: /* FMINP */
7906 gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
7907 break;
7908 default:
7909 g_assert_not_reached();
7910 }
7911
7912 write_fp_dreg(s, rd, tcg_res);
7913
7914 tcg_temp_free_i64(tcg_op1);
7915 tcg_temp_free_i64(tcg_op2);
7916 tcg_temp_free_i64(tcg_res);
7917 } else {
7918 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
7919 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
7920 TCGv_i32 tcg_res = tcg_temp_new_i32();
7921
5c36d895
AB
7922 read_vec_element_i32(s, tcg_op1, rn, 0, size);
7923 read_vec_element_i32(s, tcg_op2, rn, 1, size);
3720a7ea 7924
5c36d895
AB
7925 if (size == MO_16) {
7926 switch (opcode) {
7927 case 0xc: /* FMAXNMP */
7928 gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
7929 break;
7930 case 0xd: /* FADDP */
7931 gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
7932 break;
7933 case 0xf: /* FMAXP */
7934 gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
7935 break;
7936 case 0x2c: /* FMINNMP */
7937 gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
7938 break;
7939 case 0x2f: /* FMINP */
7940 gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
7941 break;
7942 default:
7943 g_assert_not_reached();
7944 }
7945 } else {
7946 switch (opcode) {
7947 case 0xc: /* FMAXNMP */
7948 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
7949 break;
7950 case 0xd: /* FADDP */
7951 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
7952 break;
7953 case 0xf: /* FMAXP */
7954 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
7955 break;
7956 case 0x2c: /* FMINNMP */
7957 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
7958 break;
7959 case 0x2f: /* FMINP */
7960 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
7961 break;
7962 default:
7963 g_assert_not_reached();
7964 }
3720a7ea
PM
7965 }
7966
7967 write_fp_sreg(s, rd, tcg_res);
7968
7969 tcg_temp_free_i32(tcg_op1);
7970 tcg_temp_free_i32(tcg_op2);
7971 tcg_temp_free_i32(tcg_res);
7972 }
7973
f764718d 7974 if (fpst) {
3720a7ea
PM
7975 tcg_temp_free_ptr(fpst);
7976 }
384b26fb
AB
7977}
7978
4d1cef84
AB
7979/*
7980 * Common SSHR[RA]/USHR[RA] - Shift right (optional rounding/accumulate)
7981 *
7982 * This code is handles the common shifting code and is used by both
7983 * the vector and scalar code.
7984 */
7985static void handle_shri_with_rndacc(TCGv_i64 tcg_res, TCGv_i64 tcg_src,
7986 TCGv_i64 tcg_rnd, bool accumulate,
7987 bool is_u, int size, int shift)
7988{
7989 bool extended_result = false;
f764718d 7990 bool round = tcg_rnd != NULL;
4d1cef84
AB
7991 int ext_lshift = 0;
7992 TCGv_i64 tcg_src_hi;
7993
7994 if (round && size == 3) {
7995 extended_result = true;
7996 ext_lshift = 64 - shift;
7997 tcg_src_hi = tcg_temp_new_i64();
7998 } else if (shift == 64) {
7999 if (!accumulate && is_u) {
8000 /* result is zero */
8001 tcg_gen_movi_i64(tcg_res, 0);
8002 return;
8003 }
8004 }
8005
8006 /* Deal with the rounding step */
8007 if (round) {
8008 if (extended_result) {
8009 TCGv_i64 tcg_zero = tcg_const_i64(0);
8010 if (!is_u) {
8011 /* take care of sign extending tcg_res */
8012 tcg_gen_sari_i64(tcg_src_hi, tcg_src, 63);
8013 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
8014 tcg_src, tcg_src_hi,
8015 tcg_rnd, tcg_zero);
8016 } else {
8017 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
8018 tcg_src, tcg_zero,
8019 tcg_rnd, tcg_zero);
8020 }
8021 tcg_temp_free_i64(tcg_zero);
8022 } else {
8023 tcg_gen_add_i64(tcg_src, tcg_src, tcg_rnd);
8024 }
8025 }
8026
8027 /* Now do the shift right */
8028 if (round && extended_result) {
8029 /* extended case, >64 bit precision required */
8030 if (ext_lshift == 0) {
8031 /* special case, only high bits matter */
8032 tcg_gen_mov_i64(tcg_src, tcg_src_hi);
8033 } else {
8034 tcg_gen_shri_i64(tcg_src, tcg_src, shift);
8035 tcg_gen_shli_i64(tcg_src_hi, tcg_src_hi, ext_lshift);
8036 tcg_gen_or_i64(tcg_src, tcg_src, tcg_src_hi);
8037 }
8038 } else {
8039 if (is_u) {
8040 if (shift == 64) {
8041 /* essentially shifting in 64 zeros */
8042 tcg_gen_movi_i64(tcg_src, 0);
8043 } else {
8044 tcg_gen_shri_i64(tcg_src, tcg_src, shift);
8045 }
8046 } else {
8047 if (shift == 64) {
8048 /* effectively extending the sign-bit */
8049 tcg_gen_sari_i64(tcg_src, tcg_src, 63);
8050 } else {
8051 tcg_gen_sari_i64(tcg_src, tcg_src, shift);
8052 }
8053 }
8054 }
8055
8056 if (accumulate) {
8057 tcg_gen_add_i64(tcg_res, tcg_res, tcg_src);
8058 } else {
8059 tcg_gen_mov_i64(tcg_res, tcg_src);
8060 }
8061
8062 if (extended_result) {
8063 tcg_temp_free_i64(tcg_src_hi);
8064 }
8065}
8066
4d1cef84
AB
8067/* SSHR[RA]/USHR[RA] - Scalar shift right (optional rounding/accumulate) */
8068static void handle_scalar_simd_shri(DisasContext *s,
8069 bool is_u, int immh, int immb,
8070 int opcode, int rn, int rd)
8071{
8072 const int size = 3;
8073 int immhb = immh << 3 | immb;
8074 int shift = 2 * (8 << size) - immhb;
8075 bool accumulate = false;
8076 bool round = false;
37a706ad 8077 bool insert = false;
4d1cef84
AB
8078 TCGv_i64 tcg_rn;
8079 TCGv_i64 tcg_rd;
8080 TCGv_i64 tcg_round;
8081
8082 if (!extract32(immh, 3, 1)) {
8083 unallocated_encoding(s);
8084 return;
8085 }
8086
8c6afa6a
PM
8087 if (!fp_access_check(s)) {
8088 return;
8089 }
8090
4d1cef84
AB
8091 switch (opcode) {
8092 case 0x02: /* SSRA / USRA (accumulate) */
8093 accumulate = true;
8094 break;
8095 case 0x04: /* SRSHR / URSHR (rounding) */
8096 round = true;
8097 break;
8098 case 0x06: /* SRSRA / URSRA (accum + rounding) */
8099 accumulate = round = true;
8100 break;
37a706ad
PM
8101 case 0x08: /* SRI */
8102 insert = true;
8103 break;
4d1cef84
AB
8104 }
8105
8106 if (round) {
8107 uint64_t round_const = 1ULL << (shift - 1);
8108 tcg_round = tcg_const_i64(round_const);
8109 } else {
f764718d 8110 tcg_round = NULL;
4d1cef84
AB
8111 }
8112
8113 tcg_rn = read_fp_dreg(s, rn);
37a706ad 8114 tcg_rd = (accumulate || insert) ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
4d1cef84 8115
37a706ad 8116 if (insert) {
cdb45a60
RH
8117 /* shift count same as element size is valid but does nothing;
8118 * special case to avoid potential shift by 64.
8119 */
8120 int esize = 8 << size;
8121 if (shift != esize) {
8122 tcg_gen_shri_i64(tcg_rn, tcg_rn, shift);
8123 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, 0, esize - shift);
8124 }
37a706ad
PM
8125 } else {
8126 handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
8127 accumulate, is_u, size, shift);
8128 }
4d1cef84
AB
8129
8130 write_fp_dreg(s, rd, tcg_rd);
8131
8132 tcg_temp_free_i64(tcg_rn);
8133 tcg_temp_free_i64(tcg_rd);
8134 if (round) {
8135 tcg_temp_free_i64(tcg_round);
8136 }
8137}
8138
8139/* SHL/SLI - Scalar shift left */
8140static void handle_scalar_simd_shli(DisasContext *s, bool insert,
8141 int immh, int immb, int opcode,
8142 int rn, int rd)
8143{
8144 int size = 32 - clz32(immh) - 1;
8145 int immhb = immh << 3 | immb;
8146 int shift = immhb - (8 << size);
8147 TCGv_i64 tcg_rn = new_tmp_a64(s);
8148 TCGv_i64 tcg_rd = new_tmp_a64(s);
8149
8150 if (!extract32(immh, 3, 1)) {
8151 unallocated_encoding(s);
8152 return;
8153 }
8154
8c6afa6a
PM
8155 if (!fp_access_check(s)) {
8156 return;
8157 }
8158
4d1cef84
AB
8159 tcg_rn = read_fp_dreg(s, rn);
8160 tcg_rd = insert ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
8161
cdb45a60
RH
8162 if (insert) {
8163 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, shift, 64 - shift);
8164 } else {
8165 tcg_gen_shli_i64(tcg_rd, tcg_rn, shift);
8166 }
4d1cef84
AB
8167
8168 write_fp_dreg(s, rd, tcg_rd);
8169
8170 tcg_temp_free_i64(tcg_rn);
8171 tcg_temp_free_i64(tcg_rd);
8172}
8173
c1b876b2
AB
8174/* SQSHRN/SQSHRUN - Saturating (signed/unsigned) shift right with
8175 * (signed/unsigned) narrowing */
8176static void handle_vec_simd_sqshrn(DisasContext *s, bool is_scalar, bool is_q,
8177 bool is_u_shift, bool is_u_narrow,
8178 int immh, int immb, int opcode,
8179 int rn, int rd)
8180{
8181 int immhb = immh << 3 | immb;
8182 int size = 32 - clz32(immh) - 1;
8183 int esize = 8 << size;
8184 int shift = (2 * esize) - immhb;
8185 int elements = is_scalar ? 1 : (64 / esize);
8186 bool round = extract32(opcode, 0, 1);
14776ab5 8187 MemOp ldop = (size + 1) | (is_u_shift ? 0 : MO_SIGN);
c1b876b2
AB
8188 TCGv_i64 tcg_rn, tcg_rd, tcg_round;
8189 TCGv_i32 tcg_rd_narrowed;
8190 TCGv_i64 tcg_final;
8191
8192 static NeonGenNarrowEnvFn * const signed_narrow_fns[4][2] = {
8193 { gen_helper_neon_narrow_sat_s8,
8194 gen_helper_neon_unarrow_sat8 },
8195 { gen_helper_neon_narrow_sat_s16,
8196 gen_helper_neon_unarrow_sat16 },
8197 { gen_helper_neon_narrow_sat_s32,
8198 gen_helper_neon_unarrow_sat32 },
8199 { NULL, NULL },
8200 };
8201 static NeonGenNarrowEnvFn * const unsigned_narrow_fns[4] = {
8202 gen_helper_neon_narrow_sat_u8,
8203 gen_helper_neon_narrow_sat_u16,
8204 gen_helper_neon_narrow_sat_u32,
8205 NULL
8206 };
8207 NeonGenNarrowEnvFn *narrowfn;
8208
8209 int i;
8210
8211 assert(size < 4);
8212
8213 if (extract32(immh, 3, 1)) {
8214 unallocated_encoding(s);
8215 return;
8216 }
8217
8c6afa6a
PM
8218 if (!fp_access_check(s)) {
8219 return;
8220 }
8221
c1b876b2
AB
8222 if (is_u_shift) {
8223 narrowfn = unsigned_narrow_fns[size];
8224 } else {
8225 narrowfn = signed_narrow_fns[size][is_u_narrow ? 1 : 0];
8226 }
8227
8228 tcg_rn = tcg_temp_new_i64();
8229 tcg_rd = tcg_temp_new_i64();
8230 tcg_rd_narrowed = tcg_temp_new_i32();
8231 tcg_final = tcg_const_i64(0);
8232
8233 if (round) {
8234 uint64_t round_const = 1ULL << (shift - 1);
8235 tcg_round = tcg_const_i64(round_const);
8236 } else {
f764718d 8237 tcg_round = NULL;
c1b876b2
AB
8238 }
8239
8240 for (i = 0; i < elements; i++) {
8241 read_vec_element(s, tcg_rn, rn, i, ldop);
8242 handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
8243 false, is_u_shift, size+1, shift);
8244 narrowfn(tcg_rd_narrowed, cpu_env, tcg_rd);
8245 tcg_gen_extu_i32_i64(tcg_rd, tcg_rd_narrowed);
8246 tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
8247 }
8248
8249 if (!is_q) {
c1b876b2
AB
8250 write_vec_element(s, tcg_final, rd, 0, MO_64);
8251 } else {
8252 write_vec_element(s, tcg_final, rd, 1, MO_64);
8253 }
8254
8255 if (round) {
8256 tcg_temp_free_i64(tcg_round);
8257 }
8258 tcg_temp_free_i64(tcg_rn);
8259 tcg_temp_free_i64(tcg_rd);
8260 tcg_temp_free_i32(tcg_rd_narrowed);
8261 tcg_temp_free_i64(tcg_final);
4ff55bcb
RH
8262
8263 clear_vec_high(s, is_q, rd);
c1b876b2
AB
8264}
8265
a847f32c
PM
8266/* SQSHLU, UQSHL, SQSHL: saturating left shifts */
8267static void handle_simd_qshl(DisasContext *s, bool scalar, bool is_q,
8268 bool src_unsigned, bool dst_unsigned,
8269 int immh, int immb, int rn, int rd)
8270{
8271 int immhb = immh << 3 | immb;
8272 int size = 32 - clz32(immh) - 1;
8273 int shift = immhb - (8 << size);
8274 int pass;
8275
8276 assert(immh != 0);
8277 assert(!(scalar && is_q));
8278
8279 if (!scalar) {
8280 if (!is_q && extract32(immh, 3, 1)) {
8281 unallocated_encoding(s);
8282 return;
8283 }
8284
8285 /* Since we use the variable-shift helpers we must
8286 * replicate the shift count into each element of
8287 * the tcg_shift value.
8288 */
8289 switch (size) {
8290 case 0:
8291 shift |= shift << 8;
8292 /* fall through */
8293 case 1:
8294 shift |= shift << 16;
8295 break;
8296 case 2:
8297 case 3:
8298 break;
8299 default:
8300 g_assert_not_reached();
8301 }
8302 }
8303
8c6afa6a
PM
8304 if (!fp_access_check(s)) {
8305 return;
8306 }
8307
a847f32c
PM
8308 if (size == 3) {
8309 TCGv_i64 tcg_shift = tcg_const_i64(shift);
8310 static NeonGenTwo64OpEnvFn * const fns[2][2] = {
8311 { gen_helper_neon_qshl_s64, gen_helper_neon_qshlu_s64 },
8312 { NULL, gen_helper_neon_qshl_u64 },
8313 };
8314 NeonGenTwo64OpEnvFn *genfn = fns[src_unsigned][dst_unsigned];
8315 int maxpass = is_q ? 2 : 1;
8316
8317 for (pass = 0; pass < maxpass; pass++) {
8318 TCGv_i64 tcg_op = tcg_temp_new_i64();
8319
8320 read_vec_element(s, tcg_op, rn, pass, MO_64);
8321 genfn(tcg_op, cpu_env, tcg_op, tcg_shift);
8322 write_vec_element(s, tcg_op, rd, pass, MO_64);
8323
8324 tcg_temp_free_i64(tcg_op);
8325 }
8326 tcg_temp_free_i64(tcg_shift);
4ff55bcb 8327 clear_vec_high(s, is_q, rd);
a847f32c
PM
8328 } else {
8329 TCGv_i32 tcg_shift = tcg_const_i32(shift);
8330 static NeonGenTwoOpEnvFn * const fns[2][2][3] = {
8331 {
8332 { gen_helper_neon_qshl_s8,
8333 gen_helper_neon_qshl_s16,
8334 gen_helper_neon_qshl_s32 },
8335 { gen_helper_neon_qshlu_s8,
8336 gen_helper_neon_qshlu_s16,
8337 gen_helper_neon_qshlu_s32 }
8338 }, {
8339 { NULL, NULL, NULL },
8340 { gen_helper_neon_qshl_u8,
8341 gen_helper_neon_qshl_u16,
8342 gen_helper_neon_qshl_u32 }
8343 }
8344 };
8345 NeonGenTwoOpEnvFn *genfn = fns[src_unsigned][dst_unsigned][size];
14776ab5 8346 MemOp memop = scalar ? size : MO_32;
a847f32c
PM
8347 int maxpass = scalar ? 1 : is_q ? 4 : 2;
8348
8349 for (pass = 0; pass < maxpass; pass++) {
8350 TCGv_i32 tcg_op = tcg_temp_new_i32();
8351
8352 read_vec_element_i32(s, tcg_op, rn, pass, memop);
8353 genfn(tcg_op, cpu_env, tcg_op, tcg_shift);
8354 if (scalar) {
8355 switch (size) {
8356 case 0:
8357 tcg_gen_ext8u_i32(tcg_op, tcg_op);
8358 break;
8359 case 1:
8360 tcg_gen_ext16u_i32(tcg_op, tcg_op);
8361 break;
8362 case 2:
8363 break;
8364 default:
8365 g_assert_not_reached();
8366 }
8367 write_fp_sreg(s, rd, tcg_op);
8368 } else {
8369 write_vec_element_i32(s, tcg_op, rd, pass, MO_32);
8370 }
8371
8372 tcg_temp_free_i32(tcg_op);
8373 }
8374 tcg_temp_free_i32(tcg_shift);
8375
4ff55bcb
RH
8376 if (!scalar) {
8377 clear_vec_high(s, is_q, rd);
a847f32c
PM
8378 }
8379 }
8380}
8381
10113b69
AB
8382/* Common vector code for handling integer to FP conversion */
8383static void handle_simd_intfp_conv(DisasContext *s, int rd, int rn,
8384 int elements, int is_signed,
8385 int fracbits, int size)
8386{
93193190
AB
8387 TCGv_ptr tcg_fpst = get_fpstatus_ptr(size == MO_16);
8388 TCGv_i32 tcg_shift = NULL;
8389
14776ab5 8390 MemOp mop = size | (is_signed ? MO_SIGN : 0);
10113b69
AB
8391 int pass;
8392
93193190
AB
8393 if (fracbits || size == MO_64) {
8394 tcg_shift = tcg_const_i32(fracbits);
8395 }
8396
8397 if (size == MO_64) {
8398 TCGv_i64 tcg_int64 = tcg_temp_new_i64();
8399 TCGv_i64 tcg_double = tcg_temp_new_i64();
8400
8401 for (pass = 0; pass < elements; pass++) {
8402 read_vec_element(s, tcg_int64, rn, pass, mop);
10113b69 8403
10113b69 8404 if (is_signed) {
93193190 8405 gen_helper_vfp_sqtod(tcg_double, tcg_int64,
10113b69
AB
8406 tcg_shift, tcg_fpst);
8407 } else {
93193190 8408 gen_helper_vfp_uqtod(tcg_double, tcg_int64,
10113b69
AB
8409 tcg_shift, tcg_fpst);
8410 }
8411 if (elements == 1) {
8412 write_fp_dreg(s, rd, tcg_double);
8413 } else {
8414 write_vec_element(s, tcg_double, rd, pass, MO_64);
8415 }
93193190
AB
8416 }
8417
8418 tcg_temp_free_i64(tcg_int64);
8419 tcg_temp_free_i64(tcg_double);
8420
8421 } else {
8422 TCGv_i32 tcg_int32 = tcg_temp_new_i32();
8423 TCGv_i32 tcg_float = tcg_temp_new_i32();
8424
8425 for (pass = 0; pass < elements; pass++) {
8426 read_vec_element_i32(s, tcg_int32, rn, pass, mop);
8427
8428 switch (size) {
8429 case MO_32:
8430 if (fracbits) {
8431 if (is_signed) {
8432 gen_helper_vfp_sltos(tcg_float, tcg_int32,
8433 tcg_shift, tcg_fpst);
8434 } else {
8435 gen_helper_vfp_ultos(tcg_float, tcg_int32,
8436 tcg_shift, tcg_fpst);
8437 }
8438 } else {
8439 if (is_signed) {
8440 gen_helper_vfp_sitos(tcg_float, tcg_int32, tcg_fpst);
8441 } else {
8442 gen_helper_vfp_uitos(tcg_float, tcg_int32, tcg_fpst);
8443 }
8444 }
8445 break;
8446 case MO_16:
8447 if (fracbits) {
8448 if (is_signed) {
8449 gen_helper_vfp_sltoh(tcg_float, tcg_int32,
8450 tcg_shift, tcg_fpst);
8451 } else {
8452 gen_helper_vfp_ultoh(tcg_float, tcg_int32,
8453 tcg_shift, tcg_fpst);
8454 }
8455 } else {
8456 if (is_signed) {
8457 gen_helper_vfp_sitoh(tcg_float, tcg_int32, tcg_fpst);
8458 } else {
8459 gen_helper_vfp_uitoh(tcg_float, tcg_int32, tcg_fpst);
8460 }
8461 }
8462 break;
8463 default:
8464 g_assert_not_reached();
10113b69 8465 }
93193190 8466
10113b69 8467 if (elements == 1) {
93193190 8468 write_fp_sreg(s, rd, tcg_float);
10113b69 8469 } else {
93193190 8470 write_vec_element_i32(s, tcg_float, rd, pass, size);
10113b69 8471 }
10113b69 8472 }
93193190
AB
8473
8474 tcg_temp_free_i32(tcg_int32);
8475 tcg_temp_free_i32(tcg_float);
10113b69
AB
8476 }
8477
10113b69 8478 tcg_temp_free_ptr(tcg_fpst);
93193190
AB
8479 if (tcg_shift) {
8480 tcg_temp_free_i32(tcg_shift);
8481 }
4ff55bcb
RH
8482
8483 clear_vec_high(s, elements << size == 16, rd);
10113b69
AB
8484}
8485
8486/* UCVTF/SCVTF - Integer to FP conversion */
8487static void handle_simd_shift_intfp_conv(DisasContext *s, bool is_scalar,
8488 bool is_q, bool is_u,
8489 int immh, int immb, int opcode,
8490 int rn, int rd)
8491{
a6117fae 8492 int size, elements, fracbits;
10113b69 8493 int immhb = immh << 3 | immb;
10113b69 8494
a6117fae
RH
8495 if (immh & 8) {
8496 size = MO_64;
8497 if (!is_scalar && !is_q) {
8498 unallocated_encoding(s);
8499 return;
8500 }
8501 } else if (immh & 4) {
8502 size = MO_32;
8503 } else if (immh & 2) {
8504 size = MO_16;
5763190f 8505 if (!dc_isar_feature(aa64_fp16, s)) {
a6117fae
RH
8506 unallocated_encoding(s);
8507 return;
8508 }
8509 } else {
8510 /* immh == 0 would be a failure of the decode logic */
8511 g_assert(immh == 1);
10113b69
AB
8512 unallocated_encoding(s);
8513 return;
8514 }
8515
8516 if (is_scalar) {
8517 elements = 1;
8518 } else {
a6117fae 8519 elements = (8 << is_q) >> size;
10113b69 8520 }
a6117fae 8521 fracbits = (16 << size) - immhb;
8c6afa6a
PM
8522
8523 if (!fp_access_check(s)) {
8524 return;
8525 }
8526
10113b69
AB
8527 handle_simd_intfp_conv(s, rd, rn, elements, !is_u, fracbits, size);
8528}
8529
2ed3ea11
PM
8530/* FCVTZS, FVCVTZU - FP to fixedpoint conversion */
8531static void handle_simd_shift_fpint_conv(DisasContext *s, bool is_scalar,
8532 bool is_q, bool is_u,
8533 int immh, int immb, int rn, int rd)
8534{
2ed3ea11 8535 int immhb = immh << 3 | immb;
d0ba8e74 8536 int pass, size, fracbits;
2ed3ea11
PM
8537 TCGv_ptr tcg_fpstatus;
8538 TCGv_i32 tcg_rmode, tcg_shift;
8539
d0ba8e74
RH
8540 if (immh & 0x8) {
8541 size = MO_64;
8542 if (!is_scalar && !is_q) {
8543 unallocated_encoding(s);
8544 return;
8545 }
8546 } else if (immh & 0x4) {
8547 size = MO_32;
8548 } else if (immh & 0x2) {
8549 size = MO_16;
5763190f 8550 if (!dc_isar_feature(aa64_fp16, s)) {
d0ba8e74
RH
8551 unallocated_encoding(s);
8552 return;
8553 }
8554 } else {
8555 /* Should have split out AdvSIMD modified immediate earlier. */
8556 assert(immh == 1);
2ed3ea11
PM
8557 unallocated_encoding(s);
8558 return;
8559 }
8560
8c6afa6a
PM
8561 if (!fp_access_check(s)) {
8562 return;
8563 }
8564
2ed3ea11
PM
8565 assert(!(is_scalar && is_q));
8566
8567 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(FPROUNDING_ZERO));
d0ba8e74 8568 tcg_fpstatus = get_fpstatus_ptr(size == MO_16);
9b049916 8569 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
d0ba8e74 8570 fracbits = (16 << size) - immhb;
2ed3ea11
PM
8571 tcg_shift = tcg_const_i32(fracbits);
8572
d0ba8e74 8573 if (size == MO_64) {
4063452e 8574 int maxpass = is_scalar ? 1 : 2;
2ed3ea11
PM
8575
8576 for (pass = 0; pass < maxpass; pass++) {
8577 TCGv_i64 tcg_op = tcg_temp_new_i64();
8578
8579 read_vec_element(s, tcg_op, rn, pass, MO_64);
8580 if (is_u) {
8581 gen_helper_vfp_touqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
8582 } else {
8583 gen_helper_vfp_tosqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
8584 }
8585 write_vec_element(s, tcg_op, rd, pass, MO_64);
8586 tcg_temp_free_i64(tcg_op);
8587 }
4ff55bcb 8588 clear_vec_high(s, is_q, rd);
2ed3ea11 8589 } else {
d0ba8e74
RH
8590 void (*fn)(TCGv_i32, TCGv_i32, TCGv_i32, TCGv_ptr);
8591 int maxpass = is_scalar ? 1 : ((8 << is_q) >> size);
2ed3ea11 8592
d0ba8e74
RH
8593 switch (size) {
8594 case MO_16:
2ed3ea11 8595 if (is_u) {
88808a02 8596 fn = gen_helper_vfp_touhh;
2ed3ea11 8597 } else {
88808a02 8598 fn = gen_helper_vfp_toshh;
2ed3ea11 8599 }
d0ba8e74
RH
8600 break;
8601 case MO_32:
2ed3ea11 8602 if (is_u) {
d0ba8e74 8603 fn = gen_helper_vfp_touls;
2ed3ea11 8604 } else {
d0ba8e74 8605 fn = gen_helper_vfp_tosls;
2ed3ea11 8606 }
d0ba8e74
RH
8607 break;
8608 default:
8609 g_assert_not_reached();
8610 }
8611
8612 for (pass = 0; pass < maxpass; pass++) {
8613 TCGv_i32 tcg_op = tcg_temp_new_i32();
8614
8615 read_vec_element_i32(s, tcg_op, rn, pass, size);
8616 fn(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
2ed3ea11
PM
8617 if (is_scalar) {
8618 write_fp_sreg(s, rd, tcg_op);
8619 } else {
d0ba8e74 8620 write_vec_element_i32(s, tcg_op, rd, pass, size);
2ed3ea11
PM
8621 }
8622 tcg_temp_free_i32(tcg_op);
8623 }
4ff55bcb
RH
8624 if (!is_scalar) {
8625 clear_vec_high(s, is_q, rd);
2ed3ea11
PM
8626 }
8627 }
8628
8629 tcg_temp_free_ptr(tcg_fpstatus);
8630 tcg_temp_free_i32(tcg_shift);
9b049916 8631 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
2ed3ea11
PM
8632 tcg_temp_free_i32(tcg_rmode);
8633}
8634
4ce31af4 8635/* AdvSIMD scalar shift by immediate
384b26fb
AB
8636 * 31 30 29 28 23 22 19 18 16 15 11 10 9 5 4 0
8637 * +-----+---+-------------+------+------+--------+---+------+------+
8638 * | 0 1 | U | 1 1 1 1 1 0 | immh | immb | opcode | 1 | Rn | Rd |
8639 * +-----+---+-------------+------+------+--------+---+------+------+
4d1cef84
AB
8640 *
8641 * This is the scalar version so it works on a fixed sized registers
384b26fb
AB
8642 */
8643static void disas_simd_scalar_shift_imm(DisasContext *s, uint32_t insn)
8644{
4d1cef84
AB
8645 int rd = extract32(insn, 0, 5);
8646 int rn = extract32(insn, 5, 5);
8647 int opcode = extract32(insn, 11, 5);
8648 int immb = extract32(insn, 16, 3);
8649 int immh = extract32(insn, 19, 4);
8650 bool is_u = extract32(insn, 29, 1);
8651
c1b876b2
AB
8652 if (immh == 0) {
8653 unallocated_encoding(s);
8654 return;
8655 }
8656
4d1cef84 8657 switch (opcode) {
37a706ad
PM
8658 case 0x08: /* SRI */
8659 if (!is_u) {
8660 unallocated_encoding(s);
8661 return;
8662 }
8663 /* fall through */
4d1cef84
AB
8664 case 0x00: /* SSHR / USHR */
8665 case 0x02: /* SSRA / USRA */
8666 case 0x04: /* SRSHR / URSHR */
8667 case 0x06: /* SRSRA / URSRA */
8668 handle_scalar_simd_shri(s, is_u, immh, immb, opcode, rn, rd);
8669 break;
8670 case 0x0a: /* SHL / SLI */
8671 handle_scalar_simd_shli(s, is_u, immh, immb, opcode, rn, rd);
8672 break;
10113b69
AB
8673 case 0x1c: /* SCVTF, UCVTF */
8674 handle_simd_shift_intfp_conv(s, true, false, is_u, immh, immb,
8675 opcode, rn, rd);
8676 break;
c1b876b2
AB
8677 case 0x10: /* SQSHRUN, SQSHRUN2 */
8678 case 0x11: /* SQRSHRUN, SQRSHRUN2 */
8679 if (!is_u) {
8680 unallocated_encoding(s);
8681 return;
8682 }
8683 handle_vec_simd_sqshrn(s, true, false, false, true,
8684 immh, immb, opcode, rn, rd);
8685 break;
8686 case 0x12: /* SQSHRN, SQSHRN2, UQSHRN */
8687 case 0x13: /* SQRSHRN, SQRSHRN2, UQRSHRN, UQRSHRN2 */
8688 handle_vec_simd_sqshrn(s, true, false, is_u, is_u,
8689 immh, immb, opcode, rn, rd);
8690 break;
a566da1b 8691 case 0xc: /* SQSHLU */
a847f32c
PM
8692 if (!is_u) {
8693 unallocated_encoding(s);
8694 return;
8695 }
8696 handle_simd_qshl(s, true, false, false, true, immh, immb, rn, rd);
8697 break;
a566da1b 8698 case 0xe: /* SQSHL, UQSHL */
a847f32c
PM
8699 handle_simd_qshl(s, true, false, is_u, is_u, immh, immb, rn, rd);
8700 break;
a566da1b 8701 case 0x1f: /* FCVTZS, FCVTZU */
2ed3ea11 8702 handle_simd_shift_fpint_conv(s, true, false, is_u, immh, immb, rn, rd);
4d1cef84 8703 break;
a566da1b
PM
8704 default:
8705 unallocated_encoding(s);
8706 break;
4d1cef84 8707 }
384b26fb
AB
8708}
8709
4ce31af4 8710/* AdvSIMD scalar three different
384b26fb
AB
8711 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 0
8712 * +-----+---+-----------+------+---+------+--------+-----+------+------+
8713 * | 0 1 | U | 1 1 1 1 0 | size | 1 | Rm | opcode | 0 0 | Rn | Rd |
8714 * +-----+---+-----------+------+---+------+--------+-----+------+------+
8715 */
8716static void disas_simd_scalar_three_reg_diff(DisasContext *s, uint32_t insn)
8717{
b033cd3d
PM
8718 bool is_u = extract32(insn, 29, 1);
8719 int size = extract32(insn, 22, 2);
8720 int opcode = extract32(insn, 12, 4);
8721 int rm = extract32(insn, 16, 5);
8722 int rn = extract32(insn, 5, 5);
8723 int rd = extract32(insn, 0, 5);
8724
8725 if (is_u) {
8726 unallocated_encoding(s);
8727 return;
8728 }
8729
8730 switch (opcode) {
8731 case 0x9: /* SQDMLAL, SQDMLAL2 */
8732 case 0xb: /* SQDMLSL, SQDMLSL2 */
8733 case 0xd: /* SQDMULL, SQDMULL2 */
8734 if (size == 0 || size == 3) {
8735 unallocated_encoding(s);
8736 return;
8737 }
8738 break;
8739 default:
8740 unallocated_encoding(s);
8741 return;
8742 }
8743
8c6afa6a
PM
8744 if (!fp_access_check(s)) {
8745 return;
8746 }
8747
b033cd3d
PM
8748 if (size == 2) {
8749 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8750 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
8751 TCGv_i64 tcg_res = tcg_temp_new_i64();
8752
8753 read_vec_element(s, tcg_op1, rn, 0, MO_32 | MO_SIGN);
8754 read_vec_element(s, tcg_op2, rm, 0, MO_32 | MO_SIGN);
8755
8756 tcg_gen_mul_i64(tcg_res, tcg_op1, tcg_op2);
8757 gen_helper_neon_addl_saturate_s64(tcg_res, cpu_env, tcg_res, tcg_res);
8758
8759 switch (opcode) {
8760 case 0xd: /* SQDMULL, SQDMULL2 */
8761 break;
8762 case 0xb: /* SQDMLSL, SQDMLSL2 */
8763 tcg_gen_neg_i64(tcg_res, tcg_res);
8764 /* fall through */
8765 case 0x9: /* SQDMLAL, SQDMLAL2 */
8766 read_vec_element(s, tcg_op1, rd, 0, MO_64);
8767 gen_helper_neon_addl_saturate_s64(tcg_res, cpu_env,
8768 tcg_res, tcg_op1);
8769 break;
8770 default:
8771 g_assert_not_reached();
8772 }
8773
8774 write_fp_dreg(s, rd, tcg_res);
8775
8776 tcg_temp_free_i64(tcg_op1);
8777 tcg_temp_free_i64(tcg_op2);
8778 tcg_temp_free_i64(tcg_res);
8779 } else {
3d99d931
RH
8780 TCGv_i32 tcg_op1 = read_fp_hreg(s, rn);
8781 TCGv_i32 tcg_op2 = read_fp_hreg(s, rm);
b033cd3d
PM
8782 TCGv_i64 tcg_res = tcg_temp_new_i64();
8783
b033cd3d
PM
8784 gen_helper_neon_mull_s16(tcg_res, tcg_op1, tcg_op2);
8785 gen_helper_neon_addl_saturate_s32(tcg_res, cpu_env, tcg_res, tcg_res);
8786
8787 switch (opcode) {
8788 case 0xd: /* SQDMULL, SQDMULL2 */
8789 break;
8790 case 0xb: /* SQDMLSL, SQDMLSL2 */
8791 gen_helper_neon_negl_u32(tcg_res, tcg_res);
8792 /* fall through */
8793 case 0x9: /* SQDMLAL, SQDMLAL2 */
8794 {
8795 TCGv_i64 tcg_op3 = tcg_temp_new_i64();
8796 read_vec_element(s, tcg_op3, rd, 0, MO_32);
8797 gen_helper_neon_addl_saturate_s32(tcg_res, cpu_env,
8798 tcg_res, tcg_op3);
8799 tcg_temp_free_i64(tcg_op3);
8800 break;
8801 }
8802 default:
8803 g_assert_not_reached();
8804 }
8805
8806 tcg_gen_ext32u_i64(tcg_res, tcg_res);
8807 write_fp_dreg(s, rd, tcg_res);
8808
8809 tcg_temp_free_i32(tcg_op1);
8810 tcg_temp_free_i32(tcg_op2);
8811 tcg_temp_free_i64(tcg_res);
8812 }
384b26fb
AB
8813}
8814
b305dba6
PM
8815static void handle_3same_64(DisasContext *s, int opcode, bool u,
8816 TCGv_i64 tcg_rd, TCGv_i64 tcg_rn, TCGv_i64 tcg_rm)
8817{
8818 /* Handle 64x64->64 opcodes which are shared between the scalar
8819 * and vector 3-same groups. We cover every opcode where size == 3
8820 * is valid in either the three-reg-same (integer, not pairwise)
3840d219 8821 * or scalar-three-reg-same groups.
b305dba6
PM
8822 */
8823 TCGCond cond;
8824
8825 switch (opcode) {
6d9571f7
PM
8826 case 0x1: /* SQADD */
8827 if (u) {
8828 gen_helper_neon_qadd_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
8829 } else {
8830 gen_helper_neon_qadd_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
8831 }
8832 break;
8833 case 0x5: /* SQSUB */
8834 if (u) {
8835 gen_helper_neon_qsub_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
8836 } else {
8837 gen_helper_neon_qsub_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
8838 }
8839 break;
b305dba6
PM
8840 case 0x6: /* CMGT, CMHI */
8841 /* 64 bit integer comparison, result = test ? (2^64 - 1) : 0.
8842 * We implement this using setcond (test) and then negating.
8843 */
8844 cond = u ? TCG_COND_GTU : TCG_COND_GT;
8845 do_cmop:
8846 tcg_gen_setcond_i64(cond, tcg_rd, tcg_rn, tcg_rm);
8847 tcg_gen_neg_i64(tcg_rd, tcg_rd);
8848 break;
8849 case 0x7: /* CMGE, CMHS */
8850 cond = u ? TCG_COND_GEU : TCG_COND_GE;
8851 goto do_cmop;
8852 case 0x11: /* CMTST, CMEQ */
8853 if (u) {
8854 cond = TCG_COND_EQ;
8855 goto do_cmop;
8856 }
79d61de6 8857 gen_cmtst_i64(tcg_rd, tcg_rn, tcg_rm);
b305dba6 8858 break;
6d9571f7 8859 case 0x8: /* SSHL, USHL */
b305dba6 8860 if (u) {
87b74e8b 8861 gen_ushl_i64(tcg_rd, tcg_rn, tcg_rm);
b305dba6 8862 } else {
87b74e8b 8863 gen_sshl_i64(tcg_rd, tcg_rn, tcg_rm);
b305dba6
PM
8864 }
8865 break;
b305dba6 8866 case 0x9: /* SQSHL, UQSHL */
6d9571f7
PM
8867 if (u) {
8868 gen_helper_neon_qshl_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
8869 } else {
8870 gen_helper_neon_qshl_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
8871 }
8872 break;
b305dba6 8873 case 0xa: /* SRSHL, URSHL */
6d9571f7
PM
8874 if (u) {
8875 gen_helper_neon_rshl_u64(tcg_rd, tcg_rn, tcg_rm);
8876 } else {
8877 gen_helper_neon_rshl_s64(tcg_rd, tcg_rn, tcg_rm);
8878 }
8879 break;
b305dba6 8880 case 0xb: /* SQRSHL, UQRSHL */
6d9571f7
PM
8881 if (u) {
8882 gen_helper_neon_qrshl_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
8883 } else {
8884 gen_helper_neon_qrshl_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
8885 }
8886 break;
8887 case 0x10: /* ADD, SUB */
8888 if (u) {
8889 tcg_gen_sub_i64(tcg_rd, tcg_rn, tcg_rm);
8890 } else {
8891 tcg_gen_add_i64(tcg_rd, tcg_rn, tcg_rm);
8892 }
8893 break;
b305dba6
PM
8894 default:
8895 g_assert_not_reached();
8896 }
8897}
8898
845ea09a
PM
8899/* Handle the 3-same-operands float operations; shared by the scalar
8900 * and vector encodings. The caller must filter out any encodings
8901 * not allocated for the encoding it is dealing with.
8902 */
8903static void handle_3same_float(DisasContext *s, int size, int elements,
8904 int fpopcode, int rd, int rn, int rm)
8905{
8906 int pass;
d81ce0ef 8907 TCGv_ptr fpst = get_fpstatus_ptr(false);
845ea09a
PM
8908
8909 for (pass = 0; pass < elements; pass++) {
8910 if (size) {
8911 /* Double */
8912 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8913 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
8914 TCGv_i64 tcg_res = tcg_temp_new_i64();
8915
8916 read_vec_element(s, tcg_op1, rn, pass, MO_64);
8917 read_vec_element(s, tcg_op2, rm, pass, MO_64);
8918
8919 switch (fpopcode) {
057d5f62
PM
8920 case 0x39: /* FMLS */
8921 /* As usual for ARM, separate negation for fused multiply-add */
8922 gen_helper_vfp_negd(tcg_op1, tcg_op1);
8923 /* fall through */
8924 case 0x19: /* FMLA */
8925 read_vec_element(s, tcg_res, rd, pass, MO_64);
8926 gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2,
8927 tcg_res, fpst);
8928 break;
845ea09a
PM
8929 case 0x18: /* FMAXNM */
8930 gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
8931 break;
8932 case 0x1a: /* FADD */
8933 gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
8934 break;
057d5f62
PM
8935 case 0x1b: /* FMULX */
8936 gen_helper_vfp_mulxd(tcg_res, tcg_op1, tcg_op2, fpst);
8937 break;
8908f4d1
AB
8938 case 0x1c: /* FCMEQ */
8939 gen_helper_neon_ceq_f64(tcg_res, tcg_op1, tcg_op2, fpst);
8940 break;
845ea09a
PM
8941 case 0x1e: /* FMAX */
8942 gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
8943 break;
057d5f62
PM
8944 case 0x1f: /* FRECPS */
8945 gen_helper_recpsf_f64(tcg_res, tcg_op1, tcg_op2, fpst);
8946 break;
845ea09a
PM
8947 case 0x38: /* FMINNM */
8948 gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
8949 break;
8950 case 0x3a: /* FSUB */
8951 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
8952 break;
8953 case 0x3e: /* FMIN */
8954 gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
8955 break;
057d5f62
PM
8956 case 0x3f: /* FRSQRTS */
8957 gen_helper_rsqrtsf_f64(tcg_res, tcg_op1, tcg_op2, fpst);
8958 break;
845ea09a
PM
8959 case 0x5b: /* FMUL */
8960 gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
8961 break;
8908f4d1
AB
8962 case 0x5c: /* FCMGE */
8963 gen_helper_neon_cge_f64(tcg_res, tcg_op1, tcg_op2, fpst);
8964 break;
057d5f62
PM
8965 case 0x5d: /* FACGE */
8966 gen_helper_neon_acge_f64(tcg_res, tcg_op1, tcg_op2, fpst);
8967 break;
845ea09a
PM
8968 case 0x5f: /* FDIV */
8969 gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
8970 break;
8971 case 0x7a: /* FABD */
8972 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
8973 gen_helper_vfp_absd(tcg_res, tcg_res);
8974 break;
8908f4d1
AB
8975 case 0x7c: /* FCMGT */
8976 gen_helper_neon_cgt_f64(tcg_res, tcg_op1, tcg_op2, fpst);
8977 break;
057d5f62
PM
8978 case 0x7d: /* FACGT */
8979 gen_helper_neon_acgt_f64(tcg_res, tcg_op1, tcg_op2, fpst);
8980 break;
845ea09a
PM
8981 default:
8982 g_assert_not_reached();
8983 }
8984
8985 write_vec_element(s, tcg_res, rd, pass, MO_64);
8986
8987 tcg_temp_free_i64(tcg_res);
8988 tcg_temp_free_i64(tcg_op1);
8989 tcg_temp_free_i64(tcg_op2);
8990 } else {
8991 /* Single */
8992 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
8993 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
8994 TCGv_i32 tcg_res = tcg_temp_new_i32();
8995
8996 read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
8997 read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
8998
8999 switch (fpopcode) {
057d5f62
PM
9000 case 0x39: /* FMLS */
9001 /* As usual for ARM, separate negation for fused multiply-add */
9002 gen_helper_vfp_negs(tcg_op1, tcg_op1);
9003 /* fall through */
9004 case 0x19: /* FMLA */
9005 read_vec_element_i32(s, tcg_res, rd, pass, MO_32);
9006 gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2,
9007 tcg_res, fpst);
9008 break;
845ea09a
PM
9009 case 0x1a: /* FADD */
9010 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
9011 break;
057d5f62
PM
9012 case 0x1b: /* FMULX */
9013 gen_helper_vfp_mulxs(tcg_res, tcg_op1, tcg_op2, fpst);
9014 break;
8908f4d1
AB
9015 case 0x1c: /* FCMEQ */
9016 gen_helper_neon_ceq_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9017 break;
845ea09a
PM
9018 case 0x1e: /* FMAX */
9019 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
9020 break;
057d5f62
PM
9021 case 0x1f: /* FRECPS */
9022 gen_helper_recpsf_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9023 break;
845ea09a
PM
9024 case 0x18: /* FMAXNM */
9025 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
9026 break;
9027 case 0x38: /* FMINNM */
9028 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
9029 break;
9030 case 0x3a: /* FSUB */
9031 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
9032 break;
9033 case 0x3e: /* FMIN */
9034 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
9035 break;
057d5f62
PM
9036 case 0x3f: /* FRSQRTS */
9037 gen_helper_rsqrtsf_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9038 break;
845ea09a
PM
9039 case 0x5b: /* FMUL */
9040 gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
9041 break;
8908f4d1
AB
9042 case 0x5c: /* FCMGE */
9043 gen_helper_neon_cge_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9044 break;
057d5f62
PM
9045 case 0x5d: /* FACGE */
9046 gen_helper_neon_acge_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9047 break;
845ea09a
PM
9048 case 0x5f: /* FDIV */
9049 gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
9050 break;
9051 case 0x7a: /* FABD */
9052 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
9053 gen_helper_vfp_abss(tcg_res, tcg_res);
9054 break;
8908f4d1
AB
9055 case 0x7c: /* FCMGT */
9056 gen_helper_neon_cgt_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9057 break;
057d5f62
PM
9058 case 0x7d: /* FACGT */
9059 gen_helper_neon_acgt_f32(tcg_res, tcg_op1, tcg_op2, fpst);
9060 break;
845ea09a
PM
9061 default:
9062 g_assert_not_reached();
9063 }
9064
9065 if (elements == 1) {
9066 /* scalar single so clear high part */
9067 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
9068
9069 tcg_gen_extu_i32_i64(tcg_tmp, tcg_res);
9070 write_vec_element(s, tcg_tmp, rd, pass, MO_64);
9071 tcg_temp_free_i64(tcg_tmp);
9072 } else {
9073 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
9074 }
9075
9076 tcg_temp_free_i32(tcg_res);
9077 tcg_temp_free_i32(tcg_op1);
9078 tcg_temp_free_i32(tcg_op2);
9079 }
9080 }
9081
9082 tcg_temp_free_ptr(fpst);
9083
4ff55bcb 9084 clear_vec_high(s, elements * (size ? 8 : 4) > 8, rd);
845ea09a
PM
9085}
9086
4ce31af4 9087/* AdvSIMD scalar three same
384b26fb
AB
9088 * 31 30 29 28 24 23 22 21 20 16 15 11 10 9 5 4 0
9089 * +-----+---+-----------+------+---+------+--------+---+------+------+
9090 * | 0 1 | U | 1 1 1 1 0 | size | 1 | Rm | opcode | 1 | Rn | Rd |
9091 * +-----+---+-----------+------+---+------+--------+---+------+------+
9092 */
9093static void disas_simd_scalar_three_reg_same(DisasContext *s, uint32_t insn)
9094{
b305dba6
PM
9095 int rd = extract32(insn, 0, 5);
9096 int rn = extract32(insn, 5, 5);
9097 int opcode = extract32(insn, 11, 5);
9098 int rm = extract32(insn, 16, 5);
9099 int size = extract32(insn, 22, 2);
9100 bool u = extract32(insn, 29, 1);
b305dba6
PM
9101 TCGv_i64 tcg_rd;
9102
9103 if (opcode >= 0x18) {
9104 /* Floating point: U, size[1] and opcode indicate operation */
9105 int fpopcode = opcode | (extract32(size, 1, 1) << 5) | (u << 6);
9106 switch (fpopcode) {
9107 case 0x1b: /* FMULX */
b305dba6
PM
9108 case 0x1f: /* FRECPS */
9109 case 0x3f: /* FRSQRTS */
b305dba6 9110 case 0x5d: /* FACGE */
b305dba6 9111 case 0x7d: /* FACGT */
8908f4d1
AB
9112 case 0x1c: /* FCMEQ */
9113 case 0x5c: /* FCMGE */
9114 case 0x7c: /* FCMGT */
845ea09a
PM
9115 case 0x7a: /* FABD */
9116 break;
b305dba6
PM
9117 default:
9118 unallocated_encoding(s);
9119 return;
9120 }
845ea09a 9121
8c6afa6a
PM
9122 if (!fp_access_check(s)) {
9123 return;
9124 }
9125
845ea09a
PM
9126 handle_3same_float(s, extract32(size, 0, 1), 1, fpopcode, rd, rn, rm);
9127 return;
b305dba6
PM
9128 }
9129
9130 switch (opcode) {
9131 case 0x1: /* SQADD, UQADD */
9132 case 0x5: /* SQSUB, UQSUB */
c0b2b5fa
PM
9133 case 0x9: /* SQSHL, UQSHL */
9134 case 0xb: /* SQRSHL, UQRSHL */
9135 break;
6d9571f7
PM
9136 case 0x8: /* SSHL, USHL */
9137 case 0xa: /* SRSHL, URSHL */
b305dba6
PM
9138 case 0x6: /* CMGT, CMHI */
9139 case 0x7: /* CMGE, CMHS */
9140 case 0x11: /* CMTST, CMEQ */
9141 case 0x10: /* ADD, SUB (vector) */
9142 if (size != 3) {
9143 unallocated_encoding(s);
9144 return;
9145 }
9146 break;
b305dba6
PM
9147 case 0x16: /* SQDMULH, SQRDMULH (vector) */
9148 if (size != 1 && size != 2) {
9149 unallocated_encoding(s);
9150 return;
9151 }
c0b2b5fa 9152 break;
b305dba6
PM
9153 default:
9154 unallocated_encoding(s);
9155 return;
9156 }
9157
8c6afa6a
PM
9158 if (!fp_access_check(s)) {
9159 return;
9160 }
9161
b305dba6
PM
9162 tcg_rd = tcg_temp_new_i64();
9163
c0b2b5fa
PM
9164 if (size == 3) {
9165 TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
9166 TCGv_i64 tcg_rm = read_fp_dreg(s, rm);
9167
9168 handle_3same_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rm);
9169 tcg_temp_free_i64(tcg_rn);
9170 tcg_temp_free_i64(tcg_rm);
9171 } else {
9172 /* Do a single operation on the lowest element in the vector.
9173 * We use the standard Neon helpers and rely on 0 OP 0 == 0 with
9174 * no side effects for all these operations.
9175 * OPTME: special-purpose helpers would avoid doing some
9176 * unnecessary work in the helper for the 8 and 16 bit cases.
9177 */
9178 NeonGenTwoOpEnvFn *genenvfn;
9179 TCGv_i32 tcg_rn = tcg_temp_new_i32();
9180 TCGv_i32 tcg_rm = tcg_temp_new_i32();
9181 TCGv_i32 tcg_rd32 = tcg_temp_new_i32();
9182
9183 read_vec_element_i32(s, tcg_rn, rn, 0, size);
9184 read_vec_element_i32(s, tcg_rm, rm, 0, size);
9185
9186 switch (opcode) {
9187 case 0x1: /* SQADD, UQADD */
9188 {
9189 static NeonGenTwoOpEnvFn * const fns[3][2] = {
9190 { gen_helper_neon_qadd_s8, gen_helper_neon_qadd_u8 },
9191 { gen_helper_neon_qadd_s16, gen_helper_neon_qadd_u16 },
9192 { gen_helper_neon_qadd_s32, gen_helper_neon_qadd_u32 },
9193 };
9194 genenvfn = fns[size][u];
9195 break;
9196 }
9197 case 0x5: /* SQSUB, UQSUB */
9198 {
9199 static NeonGenTwoOpEnvFn * const fns[3][2] = {
9200 { gen_helper_neon_qsub_s8, gen_helper_neon_qsub_u8 },
9201 { gen_helper_neon_qsub_s16, gen_helper_neon_qsub_u16 },
9202 { gen_helper_neon_qsub_s32, gen_helper_neon_qsub_u32 },
9203 };
9204 genenvfn = fns[size][u];
9205 break;
9206 }
9207 case 0x9: /* SQSHL, UQSHL */
9208 {
9209 static NeonGenTwoOpEnvFn * const fns[3][2] = {
9210 { gen_helper_neon_qshl_s8, gen_helper_neon_qshl_u8 },
9211 { gen_helper_neon_qshl_s16, gen_helper_neon_qshl_u16 },
9212 { gen_helper_neon_qshl_s32, gen_helper_neon_qshl_u32 },
9213 };
9214 genenvfn = fns[size][u];
9215 break;
9216 }
9217 case 0xb: /* SQRSHL, UQRSHL */
9218 {
9219 static NeonGenTwoOpEnvFn * const fns[3][2] = {
9220 { gen_helper_neon_qrshl_s8, gen_helper_neon_qrshl_u8 },
9221 { gen_helper_neon_qrshl_s16, gen_helper_neon_qrshl_u16 },
9222 { gen_helper_neon_qrshl_s32, gen_helper_neon_qrshl_u32 },
9223 };
9224 genenvfn = fns[size][u];
9225 break;
9226 }
9227 case 0x16: /* SQDMULH, SQRDMULH */
9228 {
9229 static NeonGenTwoOpEnvFn * const fns[2][2] = {
9230 { gen_helper_neon_qdmulh_s16, gen_helper_neon_qrdmulh_s16 },
9231 { gen_helper_neon_qdmulh_s32, gen_helper_neon_qrdmulh_s32 },
9232 };
9233 assert(size == 1 || size == 2);
9234 genenvfn = fns[size - 1][u];
9235 break;
9236 }
9237 default:
9238 g_assert_not_reached();
9239 }
9240
9241 genenvfn(tcg_rd32, cpu_env, tcg_rn, tcg_rm);
9242 tcg_gen_extu_i32_i64(tcg_rd, tcg_rd32);
9243 tcg_temp_free_i32(tcg_rd32);
9244 tcg_temp_free_i32(tcg_rn);
9245 tcg_temp_free_i32(tcg_rm);
9246 }
b305dba6
PM
9247
9248 write_fp_dreg(s, rd, tcg_rd);
9249
b305dba6 9250 tcg_temp_free_i64(tcg_rd);
384b26fb
AB
9251}
9252
7c93b774
AB
9253/* AdvSIMD scalar three same FP16
9254 * 31 30 29 28 24 23 22 21 20 16 15 14 13 11 10 9 5 4 0
9255 * +-----+---+-----------+---+-----+------+-----+--------+---+----+----+
9256 * | 0 1 | U | 1 1 1 1 0 | a | 1 0 | Rm | 0 0 | opcode | 1 | Rn | Rd |
9257 * +-----+---+-----------+---+-----+------+-----+--------+---+----+----+
9258 * v: 0101 1110 0100 0000 0000 0100 0000 0000 => 5e400400
9259 * m: 1101 1111 0110 0000 1100 0100 0000 0000 => df60c400
9260 */
9261static void disas_simd_scalar_three_reg_same_fp16(DisasContext *s,
9262 uint32_t insn)
9263{
9264 int rd = extract32(insn, 0, 5);
9265 int rn = extract32(insn, 5, 5);
9266 int opcode = extract32(insn, 11, 3);
9267 int rm = extract32(insn, 16, 5);
9268 bool u = extract32(insn, 29, 1);
9269 bool a = extract32(insn, 23, 1);
9270 int fpopcode = opcode | (a << 3) | (u << 4);
9271 TCGv_ptr fpst;
9272 TCGv_i32 tcg_op1;
9273 TCGv_i32 tcg_op2;
9274 TCGv_i32 tcg_res;
9275
9276 switch (fpopcode) {
9277 case 0x03: /* FMULX */
9278 case 0x04: /* FCMEQ (reg) */
9279 case 0x07: /* FRECPS */
9280 case 0x0f: /* FRSQRTS */
9281 case 0x14: /* FCMGE (reg) */
9282 case 0x15: /* FACGE */
9283 case 0x1a: /* FABD */
9284 case 0x1c: /* FCMGT (reg) */
9285 case 0x1d: /* FACGT */
9286 break;
9287 default:
9288 unallocated_encoding(s);
9289 return;
9290 }
9291
5763190f 9292 if (!dc_isar_feature(aa64_fp16, s)) {
7c93b774
AB
9293 unallocated_encoding(s);
9294 }
9295
9296 if (!fp_access_check(s)) {
9297 return;
9298 }
9299
9300 fpst = get_fpstatus_ptr(true);
9301
3d99d931
RH
9302 tcg_op1 = read_fp_hreg(s, rn);
9303 tcg_op2 = read_fp_hreg(s, rm);
7c93b774
AB
9304 tcg_res = tcg_temp_new_i32();
9305
7c93b774
AB
9306 switch (fpopcode) {
9307 case 0x03: /* FMULX */
9308 gen_helper_advsimd_mulxh(tcg_res, tcg_op1, tcg_op2, fpst);
9309 break;
9310 case 0x04: /* FCMEQ (reg) */
9311 gen_helper_advsimd_ceq_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9312 break;
9313 case 0x07: /* FRECPS */
9314 gen_helper_recpsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9315 break;
9316 case 0x0f: /* FRSQRTS */
9317 gen_helper_rsqrtsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9318 break;
9319 case 0x14: /* FCMGE (reg) */
9320 gen_helper_advsimd_cge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9321 break;
9322 case 0x15: /* FACGE */
9323 gen_helper_advsimd_acge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9324 break;
9325 case 0x1a: /* FABD */
9326 gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
9327 tcg_gen_andi_i32(tcg_res, tcg_res, 0x7fff);
9328 break;
9329 case 0x1c: /* FCMGT (reg) */
9330 gen_helper_advsimd_cgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9331 break;
9332 case 0x1d: /* FACGT */
9333 gen_helper_advsimd_acgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
9334 break;
9335 default:
9336 g_assert_not_reached();
9337 }
9338
9339 write_fp_sreg(s, rd, tcg_res);
9340
9341
9342 tcg_temp_free_i32(tcg_res);
9343 tcg_temp_free_i32(tcg_op1);
9344 tcg_temp_free_i32(tcg_op2);
9345 tcg_temp_free_ptr(fpst);
9346}
9347
d9061ec3
RH
9348/* AdvSIMD scalar three same extra
9349 * 31 30 29 28 24 23 22 21 20 16 15 14 11 10 9 5 4 0
9350 * +-----+---+-----------+------+---+------+---+--------+---+----+----+
9351 * | 0 1 | U | 1 1 1 1 0 | size | 0 | Rm | 1 | opcode | 1 | Rn | Rd |
9352 * +-----+---+-----------+------+---+------+---+--------+---+----+----+
9353 */
9354static void disas_simd_scalar_three_reg_same_extra(DisasContext *s,
9355 uint32_t insn)
9356{
9357 int rd = extract32(insn, 0, 5);
9358 int rn = extract32(insn, 5, 5);
9359 int opcode = extract32(insn, 11, 4);
9360 int rm = extract32(insn, 16, 5);
9361 int size = extract32(insn, 22, 2);
9362 bool u = extract32(insn, 29, 1);
9363 TCGv_i32 ele1, ele2, ele3;
9364 TCGv_i64 res;
962fcbf2 9365 bool feature;
d9061ec3
RH
9366
9367 switch (u * 16 + opcode) {
9368 case 0x10: /* SQRDMLAH (vector) */
9369 case 0x11: /* SQRDMLSH (vector) */
9370 if (size != 1 && size != 2) {
9371 unallocated_encoding(s);
9372 return;
9373 }
962fcbf2 9374 feature = dc_isar_feature(aa64_rdm, s);
d9061ec3
RH
9375 break;
9376 default:
9377 unallocated_encoding(s);
9378 return;
9379 }
962fcbf2 9380 if (!feature) {
d9061ec3
RH
9381 unallocated_encoding(s);
9382 return;
9383 }
9384 if (!fp_access_check(s)) {
9385 return;
9386 }
9387
9388 /* Do a single operation on the lowest element in the vector.
9389 * We use the standard Neon helpers and rely on 0 OP 0 == 0
9390 * with no side effects for all these operations.
9391 * OPTME: special-purpose helpers would avoid doing some
9392 * unnecessary work in the helper for the 16 bit cases.
9393 */
9394 ele1 = tcg_temp_new_i32();
9395 ele2 = tcg_temp_new_i32();
9396 ele3 = tcg_temp_new_i32();
9397
9398 read_vec_element_i32(s, ele1, rn, 0, size);
9399 read_vec_element_i32(s, ele2, rm, 0, size);
9400 read_vec_element_i32(s, ele3, rd, 0, size);
9401
9402 switch (opcode) {
9403 case 0x0: /* SQRDMLAH */
9404 if (size == 1) {
9405 gen_helper_neon_qrdmlah_s16(ele3, cpu_env, ele1, ele2, ele3);
9406 } else {
9407 gen_helper_neon_qrdmlah_s32(ele3, cpu_env, ele1, ele2, ele3);
9408 }
9409 break;
9410 case 0x1: /* SQRDMLSH */
9411 if (size == 1) {
9412 gen_helper_neon_qrdmlsh_s16(ele3, cpu_env, ele1, ele2, ele3);
9413 } else {
9414 gen_helper_neon_qrdmlsh_s32(ele3, cpu_env, ele1, ele2, ele3);
9415 }
9416 break;
9417 default:
9418 g_assert_not_reached();
9419 }
9420 tcg_temp_free_i32(ele1);
9421 tcg_temp_free_i32(ele2);
9422
9423 res = tcg_temp_new_i64();
9424 tcg_gen_extu_i32_i64(res, ele3);
9425 tcg_temp_free_i32(ele3);
9426
9427 write_fp_dreg(s, rd, res);
9428 tcg_temp_free_i64(res);
9429}
9430
effa8e06 9431static void handle_2misc_64(DisasContext *s, int opcode, bool u,
04c7c6c2
PM
9432 TCGv_i64 tcg_rd, TCGv_i64 tcg_rn,
9433 TCGv_i32 tcg_rmode, TCGv_ptr tcg_fpstatus)
effa8e06
PM
9434{
9435 /* Handle 64->64 opcodes which are shared between the scalar and
9436 * vector 2-reg-misc groups. We cover every integer opcode where size == 3
f93d0138 9437 * is valid in either group and also the double-precision fp ops.
04c7c6c2
PM
9438 * The caller only need provide tcg_rmode and tcg_fpstatus if the op
9439 * requires them.
effa8e06
PM
9440 */
9441 TCGCond cond;
9442
9443 switch (opcode) {
b05c3068
AB
9444 case 0x4: /* CLS, CLZ */
9445 if (u) {
7539a012 9446 tcg_gen_clzi_i64(tcg_rd, tcg_rn, 64);
b05c3068 9447 } else {
bc21dbcc 9448 tcg_gen_clrsb_i64(tcg_rd, tcg_rn);
b05c3068
AB
9449 }
9450 break;
86cbc418
PM
9451 case 0x5: /* NOT */
9452 /* This opcode is shared with CNT and RBIT but we have earlier
9453 * enforced that size == 3 if and only if this is the NOT insn.
9454 */
9455 tcg_gen_not_i64(tcg_rd, tcg_rn);
9456 break;
0a79bc87
AB
9457 case 0x7: /* SQABS, SQNEG */
9458 if (u) {
9459 gen_helper_neon_qneg_s64(tcg_rd, cpu_env, tcg_rn);
9460 } else {
9461 gen_helper_neon_qabs_s64(tcg_rd, cpu_env, tcg_rn);
9462 }
9463 break;
effa8e06
PM
9464 case 0xa: /* CMLT */
9465 /* 64 bit integer comparison against zero, result is
9466 * test ? (2^64 - 1) : 0. We implement via setcond(!test) and
9467 * subtracting 1.
9468 */
9469 cond = TCG_COND_LT;
9470 do_cmop:
9471 tcg_gen_setcondi_i64(cond, tcg_rd, tcg_rn, 0);
9472 tcg_gen_neg_i64(tcg_rd, tcg_rd);
9473 break;
9474 case 0x8: /* CMGT, CMGE */
9475 cond = u ? TCG_COND_GE : TCG_COND_GT;
9476 goto do_cmop;
9477 case 0x9: /* CMEQ, CMLE */
9478 cond = u ? TCG_COND_LE : TCG_COND_EQ;
9479 goto do_cmop;
9480 case 0xb: /* ABS, NEG */
9481 if (u) {
9482 tcg_gen_neg_i64(tcg_rd, tcg_rn);
9483 } else {
4e027a71 9484 tcg_gen_abs_i64(tcg_rd, tcg_rn);
effa8e06
PM
9485 }
9486 break;
f93d0138
PM
9487 case 0x2f: /* FABS */
9488 gen_helper_vfp_absd(tcg_rd, tcg_rn);
9489 break;
9490 case 0x6f: /* FNEG */
9491 gen_helper_vfp_negd(tcg_rd, tcg_rn);
9492 break;
f612537e
AB
9493 case 0x7f: /* FSQRT */
9494 gen_helper_vfp_sqrtd(tcg_rd, tcg_rn, cpu_env);
9495 break;
04c7c6c2
PM
9496 case 0x1a: /* FCVTNS */
9497 case 0x1b: /* FCVTMS */
9498 case 0x1c: /* FCVTAS */
9499 case 0x3a: /* FCVTPS */
9500 case 0x3b: /* FCVTZS */
9501 {
9502 TCGv_i32 tcg_shift = tcg_const_i32(0);
9503 gen_helper_vfp_tosqd(tcg_rd, tcg_rn, tcg_shift, tcg_fpstatus);
9504 tcg_temp_free_i32(tcg_shift);
9505 break;
9506 }
9507 case 0x5a: /* FCVTNU */
9508 case 0x5b: /* FCVTMU */
9509 case 0x5c: /* FCVTAU */
9510 case 0x7a: /* FCVTPU */
9511 case 0x7b: /* FCVTZU */
9512 {
9513 TCGv_i32 tcg_shift = tcg_const_i32(0);
9514 gen_helper_vfp_touqd(tcg_rd, tcg_rn, tcg_shift, tcg_fpstatus);
9515 tcg_temp_free_i32(tcg_shift);
9516 break;
9517 }
03df01ed
PM
9518 case 0x18: /* FRINTN */
9519 case 0x19: /* FRINTM */
9520 case 0x38: /* FRINTP */
9521 case 0x39: /* FRINTZ */
9522 case 0x58: /* FRINTA */
9523 case 0x79: /* FRINTI */
9524 gen_helper_rintd(tcg_rd, tcg_rn, tcg_fpstatus);
9525 break;
9526 case 0x59: /* FRINTX */
9527 gen_helper_rintd_exact(tcg_rd, tcg_rn, tcg_fpstatus);
9528 break;
6bea2563
RH
9529 case 0x1e: /* FRINT32Z */
9530 case 0x5e: /* FRINT32X */
9531 gen_helper_frint32_d(tcg_rd, tcg_rn, tcg_fpstatus);
9532 break;
9533 case 0x1f: /* FRINT64Z */
9534 case 0x5f: /* FRINT64X */
9535 gen_helper_frint64_d(tcg_rd, tcg_rn, tcg_fpstatus);
9536 break;
effa8e06
PM
9537 default:
9538 g_assert_not_reached();
9539 }
9540}
9541
8908f4d1
AB
9542static void handle_2misc_fcmp_zero(DisasContext *s, int opcode,
9543 bool is_scalar, bool is_u, bool is_q,
9544 int size, int rn, int rd)
9545{
7d4dd1a7 9546 bool is_double = (size == MO_64);
8c6afa6a
PM
9547 TCGv_ptr fpst;
9548
9549 if (!fp_access_check(s)) {
9550 return;
9551 }
9552
7d4dd1a7 9553 fpst = get_fpstatus_ptr(size == MO_16);
8908f4d1
AB
9554
9555 if (is_double) {
9556 TCGv_i64 tcg_op = tcg_temp_new_i64();
9557 TCGv_i64 tcg_zero = tcg_const_i64(0);
9558 TCGv_i64 tcg_res = tcg_temp_new_i64();
9559 NeonGenTwoDoubleOPFn *genfn;
9560 bool swap = false;
9561 int pass;
9562
9563 switch (opcode) {
9564 case 0x2e: /* FCMLT (zero) */
9565 swap = true;
9566 /* fallthrough */
9567 case 0x2c: /* FCMGT (zero) */
9568 genfn = gen_helper_neon_cgt_f64;
9569 break;
9570 case 0x2d: /* FCMEQ (zero) */
9571 genfn = gen_helper_neon_ceq_f64;
9572 break;
9573 case 0x6d: /* FCMLE (zero) */
9574 swap = true;
9575 /* fall through */
9576 case 0x6c: /* FCMGE (zero) */
9577 genfn = gen_helper_neon_cge_f64;
9578 break;
9579 default:
9580 g_assert_not_reached();
9581 }
9582
9583 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
9584 read_vec_element(s, tcg_op, rn, pass, MO_64);
9585 if (swap) {
9586 genfn(tcg_res, tcg_zero, tcg_op, fpst);
9587 } else {
9588 genfn(tcg_res, tcg_op, tcg_zero, fpst);
9589 }
9590 write_vec_element(s, tcg_res, rd, pass, MO_64);
9591 }
8908f4d1
AB
9592 tcg_temp_free_i64(tcg_res);
9593 tcg_temp_free_i64(tcg_zero);
9594 tcg_temp_free_i64(tcg_op);
4ff55bcb
RH
9595
9596 clear_vec_high(s, !is_scalar, rd);
8908f4d1
AB
9597 } else {
9598 TCGv_i32 tcg_op = tcg_temp_new_i32();
9599 TCGv_i32 tcg_zero = tcg_const_i32(0);
9600 TCGv_i32 tcg_res = tcg_temp_new_i32();
9601 NeonGenTwoSingleOPFn *genfn;
9602 bool swap = false;
9603 int pass, maxpasses;
9604
7d4dd1a7
AB
9605 if (size == MO_16) {
9606 switch (opcode) {
9607 case 0x2e: /* FCMLT (zero) */
9608 swap = true;
9609 /* fall through */
9610 case 0x2c: /* FCMGT (zero) */
9611 genfn = gen_helper_advsimd_cgt_f16;
9612 break;
9613 case 0x2d: /* FCMEQ (zero) */
9614 genfn = gen_helper_advsimd_ceq_f16;
9615 break;
9616 case 0x6d: /* FCMLE (zero) */
9617 swap = true;
9618 /* fall through */
9619 case 0x6c: /* FCMGE (zero) */
9620 genfn = gen_helper_advsimd_cge_f16;
9621 break;
9622 default:
9623 g_assert_not_reached();
9624 }
9625 } else {
9626 switch (opcode) {
9627 case 0x2e: /* FCMLT (zero) */
9628 swap = true;
9629 /* fall through */
9630 case 0x2c: /* FCMGT (zero) */
9631 genfn = gen_helper_neon_cgt_f32;
9632 break;
9633 case 0x2d: /* FCMEQ (zero) */
9634 genfn = gen_helper_neon_ceq_f32;
9635 break;
9636 case 0x6d: /* FCMLE (zero) */
9637 swap = true;
9638 /* fall through */
9639 case 0x6c: /* FCMGE (zero) */
9640 genfn = gen_helper_neon_cge_f32;
9641 break;
9642 default:
9643 g_assert_not_reached();
9644 }
8908f4d1
AB
9645 }
9646
9647 if (is_scalar) {
9648 maxpasses = 1;
9649 } else {
7d4dd1a7
AB
9650 int vector_size = 8 << is_q;
9651 maxpasses = vector_size >> size;
8908f4d1
AB
9652 }
9653
9654 for (pass = 0; pass < maxpasses; pass++) {
7d4dd1a7 9655 read_vec_element_i32(s, tcg_op, rn, pass, size);
8908f4d1
AB
9656 if (swap) {
9657 genfn(tcg_res, tcg_zero, tcg_op, fpst);
9658 } else {
9659 genfn(tcg_res, tcg_op, tcg_zero, fpst);
9660 }
9661 if (is_scalar) {
9662 write_fp_sreg(s, rd, tcg_res);
9663 } else {
7d4dd1a7 9664 write_vec_element_i32(s, tcg_res, rd, pass, size);
8908f4d1
AB
9665 }
9666 }
9667 tcg_temp_free_i32(tcg_res);
9668 tcg_temp_free_i32(tcg_zero);
9669 tcg_temp_free_i32(tcg_op);
4ff55bcb
RH
9670 if (!is_scalar) {
9671 clear_vec_high(s, is_q, rd);
8908f4d1
AB
9672 }
9673 }
9674
9675 tcg_temp_free_ptr(fpst);
9676}
9677
8f0c6758
AB
9678static void handle_2misc_reciprocal(DisasContext *s, int opcode,
9679 bool is_scalar, bool is_u, bool is_q,
9680 int size, int rn, int rd)
9681{
9682 bool is_double = (size == 3);
d81ce0ef 9683 TCGv_ptr fpst = get_fpstatus_ptr(false);
8f0c6758
AB
9684
9685 if (is_double) {
9686 TCGv_i64 tcg_op = tcg_temp_new_i64();
9687 TCGv_i64 tcg_res = tcg_temp_new_i64();
9688 int pass;
9689
9690 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
9691 read_vec_element(s, tcg_op, rn, pass, MO_64);
9692 switch (opcode) {
b6d4443a
AB
9693 case 0x3d: /* FRECPE */
9694 gen_helper_recpe_f64(tcg_res, tcg_op, fpst);
9695 break;
8f0c6758
AB
9696 case 0x3f: /* FRECPX */
9697 gen_helper_frecpx_f64(tcg_res, tcg_op, fpst);
9698 break;
c2fb418e
AB
9699 case 0x7d: /* FRSQRTE */
9700 gen_helper_rsqrte_f64(tcg_res, tcg_op, fpst);
9701 break;
8f0c6758
AB
9702 default:
9703 g_assert_not_reached();
9704 }
9705 write_vec_element(s, tcg_res, rd, pass, MO_64);
9706 }
8f0c6758
AB
9707 tcg_temp_free_i64(tcg_res);
9708 tcg_temp_free_i64(tcg_op);
4ff55bcb 9709 clear_vec_high(s, !is_scalar, rd);
8f0c6758
AB
9710 } else {
9711 TCGv_i32 tcg_op = tcg_temp_new_i32();
9712 TCGv_i32 tcg_res = tcg_temp_new_i32();
9713 int pass, maxpasses;
9714
9715 if (is_scalar) {
9716 maxpasses = 1;
9717 } else {
9718 maxpasses = is_q ? 4 : 2;
9719 }
9720
9721 for (pass = 0; pass < maxpasses; pass++) {
9722 read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
9723
9724 switch (opcode) {
b6d4443a
AB
9725 case 0x3c: /* URECPE */
9726 gen_helper_recpe_u32(tcg_res, tcg_op, fpst);
9727 break;
9728 case 0x3d: /* FRECPE */
9729 gen_helper_recpe_f32(tcg_res, tcg_op, fpst);
9730 break;
8f0c6758
AB
9731 case 0x3f: /* FRECPX */
9732 gen_helper_frecpx_f32(tcg_res, tcg_op, fpst);
9733 break;
c2fb418e
AB
9734 case 0x7d: /* FRSQRTE */
9735 gen_helper_rsqrte_f32(tcg_res, tcg_op, fpst);
9736 break;
8f0c6758
AB
9737 default:
9738 g_assert_not_reached();
9739 }
9740
9741 if (is_scalar) {
9742 write_fp_sreg(s, rd, tcg_res);
9743 } else {
9744 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
9745 }
9746 }
9747 tcg_temp_free_i32(tcg_res);
9748 tcg_temp_free_i32(tcg_op);
4ff55bcb
RH
9749 if (!is_scalar) {
9750 clear_vec_high(s, is_q, rd);
8f0c6758
AB
9751 }
9752 }
9753 tcg_temp_free_ptr(fpst);
9754}
9755
5201c136
AB
9756static void handle_2misc_narrow(DisasContext *s, bool scalar,
9757 int opcode, bool u, bool is_q,
8b092ca9
AB
9758 int size, int rn, int rd)
9759{
9760 /* Handle 2-reg-misc ops which are narrowing (so each 2*size element
9761 * in the source becomes a size element in the destination).
9762 */
9763 int pass;
9764 TCGv_i32 tcg_res[2];
9765 int destelt = is_q ? 2 : 0;
5201c136 9766 int passes = scalar ? 1 : 2;
8b092ca9 9767
5201c136
AB
9768 if (scalar) {
9769 tcg_res[1] = tcg_const_i32(0);
9770 }
9771
9772 for (pass = 0; pass < passes; pass++) {
8b092ca9
AB
9773 TCGv_i64 tcg_op = tcg_temp_new_i64();
9774 NeonGenNarrowFn *genfn = NULL;
9775 NeonGenNarrowEnvFn *genenvfn = NULL;
9776
5201c136
AB
9777 if (scalar) {
9778 read_vec_element(s, tcg_op, rn, pass, size + 1);
9779 } else {
9780 read_vec_element(s, tcg_op, rn, pass, MO_64);
9781 }
8b092ca9
AB
9782 tcg_res[pass] = tcg_temp_new_i32();
9783
9784 switch (opcode) {
9785 case 0x12: /* XTN, SQXTUN */
9786 {
9787 static NeonGenNarrowFn * const xtnfns[3] = {
9788 gen_helper_neon_narrow_u8,
9789 gen_helper_neon_narrow_u16,
ecc7b3aa 9790 tcg_gen_extrl_i64_i32,
8b092ca9
AB
9791 };
9792 static NeonGenNarrowEnvFn * const sqxtunfns[3] = {
9793 gen_helper_neon_unarrow_sat8,
9794 gen_helper_neon_unarrow_sat16,
9795 gen_helper_neon_unarrow_sat32,
9796 };
9797 if (u) {
9798 genenvfn = sqxtunfns[size];
9799 } else {
9800 genfn = xtnfns[size];
9801 }
9802 break;
9803 }
9804 case 0x14: /* SQXTN, UQXTN */
9805 {
9806 static NeonGenNarrowEnvFn * const fns[3][2] = {
9807 { gen_helper_neon_narrow_sat_s8,
9808 gen_helper_neon_narrow_sat_u8 },
9809 { gen_helper_neon_narrow_sat_s16,
9810 gen_helper_neon_narrow_sat_u16 },
9811 { gen_helper_neon_narrow_sat_s32,
9812 gen_helper_neon_narrow_sat_u32 },
9813 };
9814 genenvfn = fns[size][u];
9815 break;
9816 }
9817 case 0x16: /* FCVTN, FCVTN2 */
9818 /* 32 bit to 16 bit or 64 bit to 32 bit float conversion */
9819 if (size == 2) {
9820 gen_helper_vfp_fcvtsd(tcg_res[pass], tcg_op, cpu_env);
9821 } else {
9822 TCGv_i32 tcg_lo = tcg_temp_new_i32();
9823 TCGv_i32 tcg_hi = tcg_temp_new_i32();
486624fc
AB
9824 TCGv_ptr fpst = get_fpstatus_ptr(false);
9825 TCGv_i32 ahp = get_ahp_flag();
9826
7cb36e18 9827 tcg_gen_extr_i64_i32(tcg_lo, tcg_hi, tcg_op);
486624fc
AB
9828 gen_helper_vfp_fcvt_f32_to_f16(tcg_lo, tcg_lo, fpst, ahp);
9829 gen_helper_vfp_fcvt_f32_to_f16(tcg_hi, tcg_hi, fpst, ahp);
8b092ca9
AB
9830 tcg_gen_deposit_i32(tcg_res[pass], tcg_lo, tcg_hi, 16, 16);
9831 tcg_temp_free_i32(tcg_lo);
9832 tcg_temp_free_i32(tcg_hi);
486624fc
AB
9833 tcg_temp_free_ptr(fpst);
9834 tcg_temp_free_i32(ahp);
8b092ca9
AB
9835 }
9836 break;
5553955e
PM
9837 case 0x56: /* FCVTXN, FCVTXN2 */
9838 /* 64 bit to 32 bit float conversion
9839 * with von Neumann rounding (round to odd)
9840 */
9841 assert(size == 2);
9842 gen_helper_fcvtx_f64_to_f32(tcg_res[pass], tcg_op, cpu_env);
9843 break;
8b092ca9
AB
9844 default:
9845 g_assert_not_reached();
9846 }
9847
9848 if (genfn) {
9849 genfn(tcg_res[pass], tcg_op);
9850 } else if (genenvfn) {
9851 genenvfn(tcg_res[pass], cpu_env, tcg_op);
9852 }
9853
9854 tcg_temp_free_i64(tcg_op);
9855 }
9856
9857 for (pass = 0; pass < 2; pass++) {
9858 write_vec_element_i32(s, tcg_res[pass], rd, destelt + pass, MO_32);
9859 tcg_temp_free_i32(tcg_res[pass]);
9860 }
4ff55bcb 9861 clear_vec_high(s, is_q, rd);
8b092ca9
AB
9862}
9863
09e03735
AB
9864/* Remaining saturating accumulating ops */
9865static void handle_2misc_satacc(DisasContext *s, bool is_scalar, bool is_u,
9866 bool is_q, int size, int rn, int rd)
9867{
9868 bool is_double = (size == 3);
9869
9870 if (is_double) {
9871 TCGv_i64 tcg_rn = tcg_temp_new_i64();
9872 TCGv_i64 tcg_rd = tcg_temp_new_i64();
9873 int pass;
9874
9875 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
9876 read_vec_element(s, tcg_rn, rn, pass, MO_64);
9877 read_vec_element(s, tcg_rd, rd, pass, MO_64);
9878
9879 if (is_u) { /* USQADD */
9880 gen_helper_neon_uqadd_s64(tcg_rd, cpu_env, tcg_rn, tcg_rd);
9881 } else { /* SUQADD */
9882 gen_helper_neon_sqadd_u64(tcg_rd, cpu_env, tcg_rn, tcg_rd);
9883 }
9884 write_vec_element(s, tcg_rd, rd, pass, MO_64);
9885 }
09e03735
AB
9886 tcg_temp_free_i64(tcg_rd);
9887 tcg_temp_free_i64(tcg_rn);
4ff55bcb 9888 clear_vec_high(s, !is_scalar, rd);
09e03735
AB
9889 } else {
9890 TCGv_i32 tcg_rn = tcg_temp_new_i32();
9891 TCGv_i32 tcg_rd = tcg_temp_new_i32();
9892 int pass, maxpasses;
9893
9894 if (is_scalar) {
9895 maxpasses = 1;
9896 } else {
9897 maxpasses = is_q ? 4 : 2;
9898 }
9899
9900 for (pass = 0; pass < maxpasses; pass++) {
9901 if (is_scalar) {
9902 read_vec_element_i32(s, tcg_rn, rn, pass, size);
9903 read_vec_element_i32(s, tcg_rd, rd, pass, size);
9904 } else {
9905 read_vec_element_i32(s, tcg_rn, rn, pass, MO_32);
9906 read_vec_element_i32(s, tcg_rd, rd, pass, MO_32);
9907 }
9908
9909 if (is_u) { /* USQADD */
9910 switch (size) {
9911 case 0:
9912 gen_helper_neon_uqadd_s8(tcg_rd, cpu_env, tcg_rn, tcg_rd);
9913 break;
9914 case 1:
9915 gen_helper_neon_uqadd_s16(tcg_rd, cpu_env, tcg_rn, tcg_rd);
9916 break;
9917 case 2:
9918 gen_helper_neon_uqadd_s32(tcg_rd, cpu_env, tcg_rn, tcg_rd);
9919 break;
9920 default:
9921 g_assert_not_reached();
9922 }
9923 } else { /* SUQADD */
9924 switch (size) {
9925 case 0:
9926 gen_helper_neon_sqadd_u8(tcg_rd, cpu_env, tcg_rn, tcg_rd);
9927 break;
9928 case 1:
9929 gen_helper_neon_sqadd_u16(tcg_rd, cpu_env, tcg_rn, tcg_rd);
9930 break;
9931 case 2:
9932 gen_helper_neon_sqadd_u32(tcg_rd, cpu_env, tcg_rn, tcg_rd);
9933 break;
9934 default:
9935 g_assert_not_reached();
9936 }
9937 }
9938
9939 if (is_scalar) {
9940 TCGv_i64 tcg_zero = tcg_const_i64(0);
9941 write_vec_element(s, tcg_zero, rd, 0, MO_64);
9942 tcg_temp_free_i64(tcg_zero);
9943 }
9944 write_vec_element_i32(s, tcg_rd, rd, pass, MO_32);
9945 }
09e03735
AB
9946 tcg_temp_free_i32(tcg_rd);
9947 tcg_temp_free_i32(tcg_rn);
4ff55bcb 9948 clear_vec_high(s, is_q, rd);
09e03735
AB
9949 }
9950}
9951
4ce31af4 9952/* AdvSIMD scalar two reg misc
384b26fb
AB
9953 * 31 30 29 28 24 23 22 21 17 16 12 11 10 9 5 4 0
9954 * +-----+---+-----------+------+-----------+--------+-----+------+------+
9955 * | 0 1 | U | 1 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 | Rn | Rd |
9956 * +-----+---+-----------+------+-----------+--------+-----+------+------+
9957 */
9958static void disas_simd_scalar_two_reg_misc(DisasContext *s, uint32_t insn)
9959{
effa8e06
PM
9960 int rd = extract32(insn, 0, 5);
9961 int rn = extract32(insn, 5, 5);
9962 int opcode = extract32(insn, 12, 5);
9963 int size = extract32(insn, 22, 2);
9964 bool u = extract32(insn, 29, 1);
04c7c6c2
PM
9965 bool is_fcvt = false;
9966 int rmode;
9967 TCGv_i32 tcg_rmode;
9968 TCGv_ptr tcg_fpstatus;
effa8e06
PM
9969
9970 switch (opcode) {
09e03735 9971 case 0x3: /* USQADD / SUQADD*/
8c6afa6a
PM
9972 if (!fp_access_check(s)) {
9973 return;
9974 }
09e03735
AB
9975 handle_2misc_satacc(s, true, u, false, size, rn, rd);
9976 return;
0a79bc87
AB
9977 case 0x7: /* SQABS / SQNEG */
9978 break;
effa8e06
PM
9979 case 0xa: /* CMLT */
9980 if (u) {
9981 unallocated_encoding(s);
9982 return;
9983 }
9984 /* fall through */
9985 case 0x8: /* CMGT, CMGE */
9986 case 0x9: /* CMEQ, CMLE */
9987 case 0xb: /* ABS, NEG */
9988 if (size != 3) {
9989 unallocated_encoding(s);
9990 return;
9991 }
9992 break;
5201c136 9993 case 0x12: /* SQXTUN */
e44a90c5 9994 if (!u) {
5201c136
AB
9995 unallocated_encoding(s);
9996 return;
9997 }
9998 /* fall through */
9999 case 0x14: /* SQXTN, UQXTN */
10000 if (size == 3) {
10001 unallocated_encoding(s);
10002 return;
10003 }
8c6afa6a
PM
10004 if (!fp_access_check(s)) {
10005 return;
10006 }
5201c136
AB
10007 handle_2misc_narrow(s, true, opcode, u, false, size, rn, rd);
10008 return;
8908f4d1
AB
10009 case 0xc ... 0xf:
10010 case 0x16 ... 0x1d:
10011 case 0x1f:
10012 /* Floating point: U, size[1] and opcode indicate operation;
10013 * size[0] indicates single or double precision.
10014 */
10015 opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
10016 size = extract32(size, 0, 1) ? 3 : 2;
10017 switch (opcode) {
10018 case 0x2c: /* FCMGT (zero) */
10019 case 0x2d: /* FCMEQ (zero) */
10020 case 0x2e: /* FCMLT (zero) */
10021 case 0x6c: /* FCMGE (zero) */
10022 case 0x6d: /* FCMLE (zero) */
10023 handle_2misc_fcmp_zero(s, opcode, true, u, true, size, rn, rd);
10024 return;
10113b69
AB
10025 case 0x1d: /* SCVTF */
10026 case 0x5d: /* UCVTF */
10027 {
10028 bool is_signed = (opcode == 0x1d);
8c6afa6a
PM
10029 if (!fp_access_check(s)) {
10030 return;
10031 }
10113b69
AB
10032 handle_simd_intfp_conv(s, rd, rn, 1, is_signed, 0, size);
10033 return;
10034 }
b6d4443a 10035 case 0x3d: /* FRECPE */
8f0c6758 10036 case 0x3f: /* FRECPX */
c2fb418e 10037 case 0x7d: /* FRSQRTE */
8c6afa6a
PM
10038 if (!fp_access_check(s)) {
10039 return;
10040 }
8f0c6758
AB
10041 handle_2misc_reciprocal(s, opcode, true, u, true, size, rn, rd);
10042 return;
8908f4d1
AB
10043 case 0x1a: /* FCVTNS */
10044 case 0x1b: /* FCVTMS */
8908f4d1
AB
10045 case 0x3a: /* FCVTPS */
10046 case 0x3b: /* FCVTZS */
8908f4d1
AB
10047 case 0x5a: /* FCVTNU */
10048 case 0x5b: /* FCVTMU */
8908f4d1
AB
10049 case 0x7a: /* FCVTPU */
10050 case 0x7b: /* FCVTZU */
04c7c6c2
PM
10051 is_fcvt = true;
10052 rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
10053 break;
10054 case 0x1c: /* FCVTAS */
10055 case 0x5c: /* FCVTAU */
10056 /* TIEAWAY doesn't fit in the usual rounding mode encoding */
10057 is_fcvt = true;
10058 rmode = FPROUNDING_TIEAWAY;
10059 break;
04c7c6c2 10060 case 0x56: /* FCVTXN, FCVTXN2 */
5553955e
PM
10061 if (size == 2) {
10062 unallocated_encoding(s);
10063 return;
10064 }
8c6afa6a
PM
10065 if (!fp_access_check(s)) {
10066 return;
10067 }
5553955e
PM
10068 handle_2misc_narrow(s, true, opcode, u, false, size - 1, rn, rd);
10069 return;
8908f4d1
AB
10070 default:
10071 unallocated_encoding(s);
10072 return;
10073 }
10074 break;
effa8e06 10075 default:
09e03735 10076 unallocated_encoding(s);
effa8e06
PM
10077 return;
10078 }
10079
8c6afa6a
PM
10080 if (!fp_access_check(s)) {
10081 return;
10082 }
10083
04c7c6c2
PM
10084 if (is_fcvt) {
10085 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rmode));
d81ce0ef 10086 tcg_fpstatus = get_fpstatus_ptr(false);
9b049916 10087 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
04c7c6c2 10088 } else {
f764718d
RH
10089 tcg_rmode = NULL;
10090 tcg_fpstatus = NULL;
04c7c6c2
PM
10091 }
10092
effa8e06
PM
10093 if (size == 3) {
10094 TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
10095 TCGv_i64 tcg_rd = tcg_temp_new_i64();
10096
04c7c6c2 10097 handle_2misc_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rmode, tcg_fpstatus);
effa8e06
PM
10098 write_fp_dreg(s, rd, tcg_rd);
10099 tcg_temp_free_i64(tcg_rd);
10100 tcg_temp_free_i64(tcg_rn);
0a79bc87
AB
10101 } else {
10102 TCGv_i32 tcg_rn = tcg_temp_new_i32();
04c7c6c2
PM
10103 TCGv_i32 tcg_rd = tcg_temp_new_i32();
10104
0a79bc87
AB
10105 read_vec_element_i32(s, tcg_rn, rn, 0, size);
10106
04c7c6c2 10107 switch (opcode) {
0a79bc87
AB
10108 case 0x7: /* SQABS, SQNEG */
10109 {
10110 NeonGenOneOpEnvFn *genfn;
10111 static NeonGenOneOpEnvFn * const fns[3][2] = {
10112 { gen_helper_neon_qabs_s8, gen_helper_neon_qneg_s8 },
10113 { gen_helper_neon_qabs_s16, gen_helper_neon_qneg_s16 },
10114 { gen_helper_neon_qabs_s32, gen_helper_neon_qneg_s32 },
10115 };
10116 genfn = fns[size][u];
10117 genfn(tcg_rd, cpu_env, tcg_rn);
10118 break;
10119 }
04c7c6c2
PM
10120 case 0x1a: /* FCVTNS */
10121 case 0x1b: /* FCVTMS */
10122 case 0x1c: /* FCVTAS */
10123 case 0x3a: /* FCVTPS */
10124 case 0x3b: /* FCVTZS */
10125 {
10126 TCGv_i32 tcg_shift = tcg_const_i32(0);
10127 gen_helper_vfp_tosls(tcg_rd, tcg_rn, tcg_shift, tcg_fpstatus);
10128 tcg_temp_free_i32(tcg_shift);
10129 break;
10130 }
10131 case 0x5a: /* FCVTNU */
10132 case 0x5b: /* FCVTMU */
10133 case 0x5c: /* FCVTAU */
10134 case 0x7a: /* FCVTPU */
10135 case 0x7b: /* FCVTZU */
10136 {
10137 TCGv_i32 tcg_shift = tcg_const_i32(0);
10138 gen_helper_vfp_touls(tcg_rd, tcg_rn, tcg_shift, tcg_fpstatus);
10139 tcg_temp_free_i32(tcg_shift);
10140 break;
10141 }
10142 default:
10143 g_assert_not_reached();
10144 }
10145
10146 write_fp_sreg(s, rd, tcg_rd);
10147 tcg_temp_free_i32(tcg_rd);
10148 tcg_temp_free_i32(tcg_rn);
effa8e06 10149 }
04c7c6c2
PM
10150
10151 if (is_fcvt) {
9b049916 10152 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
04c7c6c2
PM
10153 tcg_temp_free_i32(tcg_rmode);
10154 tcg_temp_free_ptr(tcg_fpstatus);
10155 }
384b26fb
AB
10156}
10157
4d1cef84
AB
10158/* SSHR[RA]/USHR[RA] - Vector shift right (optional rounding/accumulate) */
10159static void handle_vec_simd_shri(DisasContext *s, bool is_q, bool is_u,
10160 int immh, int immb, int opcode, int rn, int rd)
10161{
10162 int size = 32 - clz32(immh) - 1;
10163 int immhb = immh << 3 | immb;
10164 int shift = 2 * (8 << size) - immhb;
10165 bool accumulate = false;
4d1cef84
AB
10166 int dsize = is_q ? 128 : 64;
10167 int esize = 8 << size;
10168 int elements = dsize/esize;
14776ab5 10169 MemOp memop = size | (is_u ? 0 : MO_SIGN);
4d1cef84
AB
10170 TCGv_i64 tcg_rn = new_tmp_a64(s);
10171 TCGv_i64 tcg_rd = new_tmp_a64(s);
10172 TCGv_i64 tcg_round;
cdb45a60 10173 uint64_t round_const;
4d1cef84
AB
10174 int i;
10175
10176 if (extract32(immh, 3, 1) && !is_q) {
10177 unallocated_encoding(s);
10178 return;
10179 }
8dae4697 10180 tcg_debug_assert(size <= 3);
4d1cef84 10181
8c6afa6a
PM
10182 if (!fp_access_check(s)) {
10183 return;
10184 }
10185
4d1cef84
AB
10186 switch (opcode) {
10187 case 0x02: /* SSRA / USRA (accumulate) */
cdb45a60
RH
10188 if (is_u) {
10189 /* Shift count same as element size produces zero to add. */
10190 if (shift == 8 << size) {
10191 goto done;
10192 }
10193 gen_gvec_op2i(s, is_q, rd, rn, shift, &usra_op[size]);
10194 } else {
10195 /* Shift count same as element size produces all sign to add. */
10196 if (shift == 8 << size) {
10197 shift -= 1;
10198 }
10199 gen_gvec_op2i(s, is_q, rd, rn, shift, &ssra_op[size]);
10200 }
10201 return;
10202 case 0x08: /* SRI */
10203 /* Shift count same as element size is valid but does nothing. */
10204 if (shift == 8 << size) {
10205 goto done;
10206 }
10207 gen_gvec_op2i(s, is_q, rd, rn, shift, &sri_op[size]);
10208 return;
10209
10210 case 0x00: /* SSHR / USHR */
10211 if (is_u) {
10212 if (shift == 8 << size) {
10213 /* Shift count the same size as element size produces zero. */
10214 tcg_gen_gvec_dup8i(vec_full_reg_offset(s, rd),
10215 is_q ? 16 : 8, vec_full_reg_size(s), 0);
10216 } else {
10217 gen_gvec_fn2i(s, is_q, rd, rn, shift, tcg_gen_gvec_shri, size);
10218 }
10219 } else {
10220 /* Shift count the same size as element size produces all sign. */
10221 if (shift == 8 << size) {
10222 shift -= 1;
10223 }
10224 gen_gvec_fn2i(s, is_q, rd, rn, shift, tcg_gen_gvec_sari, size);
10225 }
10226 return;
10227
4d1cef84 10228 case 0x04: /* SRSHR / URSHR (rounding) */
4d1cef84
AB
10229 break;
10230 case 0x06: /* SRSRA / URSRA (accum + rounding) */
cdb45a60 10231 accumulate = true;
37a706ad 10232 break;
cdb45a60
RH
10233 default:
10234 g_assert_not_reached();
4d1cef84
AB
10235 }
10236
cdb45a60
RH
10237 round_const = 1ULL << (shift - 1);
10238 tcg_round = tcg_const_i64(round_const);
4d1cef84
AB
10239
10240 for (i = 0; i < elements; i++) {
10241 read_vec_element(s, tcg_rn, rn, i, memop);
cdb45a60 10242 if (accumulate) {
4d1cef84
AB
10243 read_vec_element(s, tcg_rd, rd, i, memop);
10244 }
10245
cdb45a60
RH
10246 handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
10247 accumulate, is_u, size, shift);
4d1cef84
AB
10248
10249 write_vec_element(s, tcg_rd, rd, i, size);
10250 }
cdb45a60 10251 tcg_temp_free_i64(tcg_round);
4d1cef84 10252
cdb45a60 10253 done:
4ff55bcb 10254 clear_vec_high(s, is_q, rd);
cdb45a60 10255}
4d1cef84 10256
4d1cef84
AB
10257/* SHL/SLI - Vector shift left */
10258static void handle_vec_simd_shli(DisasContext *s, bool is_q, bool insert,
cdb45a60 10259 int immh, int immb, int opcode, int rn, int rd)
4d1cef84
AB
10260{
10261 int size = 32 - clz32(immh) - 1;
10262 int immhb = immh << 3 | immb;
10263 int shift = immhb - (8 << size);
4d1cef84 10264
f6c98f91
PM
10265 /* Range of size is limited by decode: immh is a non-zero 4 bit field */
10266 assert(size >= 0 && size <= 3);
4d1cef84 10267
f6c98f91 10268 if (extract32(immh, 3, 1) && !is_q) {
4d1cef84
AB
10269 unallocated_encoding(s);
10270 return;
10271 }
10272
8c6afa6a
PM
10273 if (!fp_access_check(s)) {
10274 return;
10275 }
10276
cdb45a60 10277 if (insert) {
f3cd8218 10278 gen_gvec_op2i(s, is_q, rd, rn, shift, &sli_op[size]);
cdb45a60
RH
10279 } else {
10280 gen_gvec_fn2i(s, is_q, rd, rn, shift, tcg_gen_gvec_shli, size);
4d1cef84
AB
10281 }
10282}
10283
10284/* USHLL/SHLL - Vector shift left with widening */
10285static void handle_vec_simd_wshli(DisasContext *s, bool is_q, bool is_u,
10286 int immh, int immb, int opcode, int rn, int rd)
10287{
10288 int size = 32 - clz32(immh) - 1;
10289 int immhb = immh << 3 | immb;
10290 int shift = immhb - (8 << size);
10291 int dsize = 64;
10292 int esize = 8 << size;
10293 int elements = dsize/esize;
10294 TCGv_i64 tcg_rn = new_tmp_a64(s);
10295 TCGv_i64 tcg_rd = new_tmp_a64(s);
10296 int i;
10297
10298 if (size >= 3) {
10299 unallocated_encoding(s);
10300 return;
10301 }
10302
8c6afa6a
PM
10303 if (!fp_access_check(s)) {
10304 return;
10305 }
10306
4d1cef84
AB
10307 /* For the LL variants the store is larger than the load,
10308 * so if rd == rn we would overwrite parts of our input.
10309 * So load everything right now and use shifts in the main loop.
10310 */
10311 read_vec_element(s, tcg_rn, rn, is_q ? 1 : 0, MO_64);
10312
10313 for (i = 0; i < elements; i++) {
10314 tcg_gen_shri_i64(tcg_rd, tcg_rn, i * esize);
10315 ext_and_shift_reg(tcg_rd, tcg_rd, size | (!is_u << 2), 0);
10316 tcg_gen_shli_i64(tcg_rd, tcg_rd, shift);
10317 write_vec_element(s, tcg_rd, rd, i, size + 1);
10318 }
10319}
10320
c1b876b2
AB
10321/* SHRN/RSHRN - Shift right with narrowing (and potential rounding) */
10322static void handle_vec_simd_shrn(DisasContext *s, bool is_q,
10323 int immh, int immb, int opcode, int rn, int rd)
10324{
10325 int immhb = immh << 3 | immb;
10326 int size = 32 - clz32(immh) - 1;
10327 int dsize = 64;
10328 int esize = 8 << size;
10329 int elements = dsize/esize;
10330 int shift = (2 * esize) - immhb;
10331 bool round = extract32(opcode, 0, 1);
10332 TCGv_i64 tcg_rn, tcg_rd, tcg_final;
10333 TCGv_i64 tcg_round;
10334 int i;
10335
10336 if (extract32(immh, 3, 1)) {
10337 unallocated_encoding(s);
10338 return;
10339 }
10340
8c6afa6a
PM
10341 if (!fp_access_check(s)) {
10342 return;
10343 }
10344
c1b876b2
AB
10345 tcg_rn = tcg_temp_new_i64();
10346 tcg_rd = tcg_temp_new_i64();
10347 tcg_final = tcg_temp_new_i64();
10348 read_vec_element(s, tcg_final, rd, is_q ? 1 : 0, MO_64);
10349
10350 if (round) {
10351 uint64_t round_const = 1ULL << (shift - 1);
10352 tcg_round = tcg_const_i64(round_const);
10353 } else {
f764718d 10354 tcg_round = NULL;
c1b876b2
AB
10355 }
10356
10357 for (i = 0; i < elements; i++) {
10358 read_vec_element(s, tcg_rn, rn, i, size+1);
10359 handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
10360 false, true, size+1, shift);
10361
10362 tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
10363 }
10364
10365 if (!is_q) {
c1b876b2
AB
10366 write_vec_element(s, tcg_final, rd, 0, MO_64);
10367 } else {
10368 write_vec_element(s, tcg_final, rd, 1, MO_64);
10369 }
c1b876b2
AB
10370 if (round) {
10371 tcg_temp_free_i64(tcg_round);
10372 }
10373 tcg_temp_free_i64(tcg_rn);
10374 tcg_temp_free_i64(tcg_rd);
10375 tcg_temp_free_i64(tcg_final);
4ff55bcb
RH
10376
10377 clear_vec_high(s, is_q, rd);
c1b876b2
AB
10378}
10379
10380
4ce31af4 10381/* AdvSIMD shift by immediate
384b26fb
AB
10382 * 31 30 29 28 23 22 19 18 16 15 11 10 9 5 4 0
10383 * +---+---+---+-------------+------+------+--------+---+------+------+
10384 * | 0 | Q | U | 0 1 1 1 1 0 | immh | immb | opcode | 1 | Rn | Rd |
10385 * +---+---+---+-------------+------+------+--------+---+------+------+
10386 */
10387static void disas_simd_shift_imm(DisasContext *s, uint32_t insn)
10388{
4d1cef84
AB
10389 int rd = extract32(insn, 0, 5);
10390 int rn = extract32(insn, 5, 5);
10391 int opcode = extract32(insn, 11, 5);
10392 int immb = extract32(insn, 16, 3);
10393 int immh = extract32(insn, 19, 4);
10394 bool is_u = extract32(insn, 29, 1);
10395 bool is_q = extract32(insn, 30, 1);
10396
10397 switch (opcode) {
37a706ad
PM
10398 case 0x08: /* SRI */
10399 if (!is_u) {
10400 unallocated_encoding(s);
10401 return;
10402 }
10403 /* fall through */
4d1cef84
AB
10404 case 0x00: /* SSHR / USHR */
10405 case 0x02: /* SSRA / USRA (accumulate) */
10406 case 0x04: /* SRSHR / URSHR (rounding) */
10407 case 0x06: /* SRSRA / URSRA (accum + rounding) */
10408 handle_vec_simd_shri(s, is_q, is_u, immh, immb, opcode, rn, rd);
10409 break;
10410 case 0x0a: /* SHL / SLI */
10411 handle_vec_simd_shli(s, is_q, is_u, immh, immb, opcode, rn, rd);
10412 break;
c1b876b2
AB
10413 case 0x10: /* SHRN */
10414 case 0x11: /* RSHRN / SQRSHRUN */
10415 if (is_u) {
10416 handle_vec_simd_sqshrn(s, false, is_q, false, true, immh, immb,
10417 opcode, rn, rd);
10418 } else {
10419 handle_vec_simd_shrn(s, is_q, immh, immb, opcode, rn, rd);
10420 }
10421 break;
10422 case 0x12: /* SQSHRN / UQSHRN */
10423 case 0x13: /* SQRSHRN / UQRSHRN */
10424 handle_vec_simd_sqshrn(s, false, is_q, is_u, is_u, immh, immb,
10425 opcode, rn, rd);
10426 break;
4d1cef84
AB
10427 case 0x14: /* SSHLL / USHLL */
10428 handle_vec_simd_wshli(s, is_q, is_u, immh, immb, opcode, rn, rd);
10429 break;
10113b69
AB
10430 case 0x1c: /* SCVTF / UCVTF */
10431 handle_simd_shift_intfp_conv(s, false, is_q, is_u, immh, immb,
10432 opcode, rn, rd);
10433 break;
a566da1b 10434 case 0xc: /* SQSHLU */
a847f32c
PM
10435 if (!is_u) {
10436 unallocated_encoding(s);
10437 return;
10438 }
10439 handle_simd_qshl(s, false, is_q, false, true, immh, immb, rn, rd);
10440 break;
a566da1b 10441 case 0xe: /* SQSHL, UQSHL */
a847f32c
PM
10442 handle_simd_qshl(s, false, is_q, is_u, is_u, immh, immb, rn, rd);
10443 break;
10113b69 10444 case 0x1f: /* FCVTZS/ FCVTZU */
2ed3ea11 10445 handle_simd_shift_fpint_conv(s, false, is_q, is_u, immh, immb, rn, rd);
10113b69 10446 return;
4d1cef84 10447 default:
a566da1b 10448 unallocated_encoding(s);
4d1cef84
AB
10449 return;
10450 }
384b26fb
AB
10451}
10452
70d7f984
PM
10453/* Generate code to do a "long" addition or subtraction, ie one done in
10454 * TCGv_i64 on vector lanes twice the width specified by size.
10455 */
10456static void gen_neon_addl(int size, bool is_sub, TCGv_i64 tcg_res,
10457 TCGv_i64 tcg_op1, TCGv_i64 tcg_op2)
10458{
10459 static NeonGenTwo64OpFn * const fns[3][2] = {
10460 { gen_helper_neon_addl_u16, gen_helper_neon_subl_u16 },
10461 { gen_helper_neon_addl_u32, gen_helper_neon_subl_u32 },
10462 { tcg_gen_add_i64, tcg_gen_sub_i64 },
10463 };
10464 NeonGenTwo64OpFn *genfn;
10465 assert(size < 3);
10466
10467 genfn = fns[size][is_sub];
10468 genfn(tcg_res, tcg_op1, tcg_op2);
10469}
10470
a08582f4
PM
10471static void handle_3rd_widening(DisasContext *s, int is_q, int is_u, int size,
10472 int opcode, int rd, int rn, int rm)
10473{
10474 /* 3-reg-different widening insns: 64 x 64 -> 128 */
10475 TCGv_i64 tcg_res[2];
10476 int pass, accop;
10477
10478 tcg_res[0] = tcg_temp_new_i64();
10479 tcg_res[1] = tcg_temp_new_i64();
10480
10481 /* Does this op do an adding accumulate, a subtracting accumulate,
10482 * or no accumulate at all?
10483 */
10484 switch (opcode) {
10485 case 5:
10486 case 8:
10487 case 9:
10488 accop = 1;
10489 break;
10490 case 10:
10491 case 11:
10492 accop = -1;
10493 break;
10494 default:
10495 accop = 0;
10496 break;
10497 }
10498
10499 if (accop != 0) {
10500 read_vec_element(s, tcg_res[0], rd, 0, MO_64);
10501 read_vec_element(s, tcg_res[1], rd, 1, MO_64);
10502 }
10503
10504 /* size == 2 means two 32x32->64 operations; this is worth special
10505 * casing because we can generally handle it inline.
10506 */
10507 if (size == 2) {
10508 for (pass = 0; pass < 2; pass++) {
10509 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10510 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
10511 TCGv_i64 tcg_passres;
14776ab5 10512 MemOp memop = MO_32 | (is_u ? 0 : MO_SIGN);
a08582f4
PM
10513
10514 int elt = pass + is_q * 2;
10515
10516 read_vec_element(s, tcg_op1, rn, elt, memop);
10517 read_vec_element(s, tcg_op2, rm, elt, memop);
10518
10519 if (accop == 0) {
10520 tcg_passres = tcg_res[pass];
10521 } else {
10522 tcg_passres = tcg_temp_new_i64();
10523 }
10524
10525 switch (opcode) {
70d7f984
PM
10526 case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10527 tcg_gen_add_i64(tcg_passres, tcg_op1, tcg_op2);
10528 break;
10529 case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
10530 tcg_gen_sub_i64(tcg_passres, tcg_op1, tcg_op2);
10531 break;
0ae39320
PM
10532 case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10533 case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10534 {
10535 TCGv_i64 tcg_tmp1 = tcg_temp_new_i64();
10536 TCGv_i64 tcg_tmp2 = tcg_temp_new_i64();
10537
10538 tcg_gen_sub_i64(tcg_tmp1, tcg_op1, tcg_op2);
10539 tcg_gen_sub_i64(tcg_tmp2, tcg_op2, tcg_op1);
10540 tcg_gen_movcond_i64(is_u ? TCG_COND_GEU : TCG_COND_GE,
10541 tcg_passres,
10542 tcg_op1, tcg_op2, tcg_tmp1, tcg_tmp2);
10543 tcg_temp_free_i64(tcg_tmp1);
10544 tcg_temp_free_i64(tcg_tmp2);
10545 break;
10546 }
a08582f4
PM
10547 case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10548 case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10549 case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
10550 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
10551 break;
70d7f984
PM
10552 case 9: /* SQDMLAL, SQDMLAL2 */
10553 case 11: /* SQDMLSL, SQDMLSL2 */
10554 case 13: /* SQDMULL, SQDMULL2 */
10555 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
10556 gen_helper_neon_addl_saturate_s64(tcg_passres, cpu_env,
10557 tcg_passres, tcg_passres);
10558 break;
a08582f4
PM
10559 default:
10560 g_assert_not_reached();
10561 }
10562
70d7f984
PM
10563 if (opcode == 9 || opcode == 11) {
10564 /* saturating accumulate ops */
10565 if (accop < 0) {
10566 tcg_gen_neg_i64(tcg_passres, tcg_passres);
10567 }
10568 gen_helper_neon_addl_saturate_s64(tcg_res[pass], cpu_env,
10569 tcg_res[pass], tcg_passres);
10570 } else if (accop > 0) {
a08582f4 10571 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
a08582f4
PM
10572 } else if (accop < 0) {
10573 tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
70d7f984
PM
10574 }
10575
10576 if (accop != 0) {
a08582f4
PM
10577 tcg_temp_free_i64(tcg_passres);
10578 }
10579
10580 tcg_temp_free_i64(tcg_op1);
10581 tcg_temp_free_i64(tcg_op2);
10582 }
10583 } else {
10584 /* size 0 or 1, generally helper functions */
10585 for (pass = 0; pass < 2; pass++) {
10586 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
10587 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
10588 TCGv_i64 tcg_passres;
10589 int elt = pass + is_q * 2;
10590
10591 read_vec_element_i32(s, tcg_op1, rn, elt, MO_32);
10592 read_vec_element_i32(s, tcg_op2, rm, elt, MO_32);
10593
10594 if (accop == 0) {
10595 tcg_passres = tcg_res[pass];
10596 } else {
10597 tcg_passres = tcg_temp_new_i64();
10598 }
10599
10600 switch (opcode) {
70d7f984
PM
10601 case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10602 case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
10603 {
10604 TCGv_i64 tcg_op2_64 = tcg_temp_new_i64();
10605 static NeonGenWidenFn * const widenfns[2][2] = {
10606 { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
10607 { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
10608 };
10609 NeonGenWidenFn *widenfn = widenfns[size][is_u];
10610
10611 widenfn(tcg_op2_64, tcg_op2);
10612 widenfn(tcg_passres, tcg_op1);
10613 gen_neon_addl(size, (opcode == 2), tcg_passres,
10614 tcg_passres, tcg_op2_64);
10615 tcg_temp_free_i64(tcg_op2_64);
10616 break;
10617 }
0ae39320
PM
10618 case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10619 case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10620 if (size == 0) {
10621 if (is_u) {
10622 gen_helper_neon_abdl_u16(tcg_passres, tcg_op1, tcg_op2);
10623 } else {
10624 gen_helper_neon_abdl_s16(tcg_passres, tcg_op1, tcg_op2);
10625 }
10626 } else {
10627 if (is_u) {
10628 gen_helper_neon_abdl_u32(tcg_passres, tcg_op1, tcg_op2);
10629 } else {
10630 gen_helper_neon_abdl_s32(tcg_passres, tcg_op1, tcg_op2);
10631 }
10632 }
10633 break;
a08582f4
PM
10634 case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10635 case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10636 case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
10637 if (size == 0) {
10638 if (is_u) {
10639 gen_helper_neon_mull_u8(tcg_passres, tcg_op1, tcg_op2);
10640 } else {
10641 gen_helper_neon_mull_s8(tcg_passres, tcg_op1, tcg_op2);
10642 }
10643 } else {
10644 if (is_u) {
10645 gen_helper_neon_mull_u16(tcg_passres, tcg_op1, tcg_op2);
10646 } else {
10647 gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
10648 }
10649 }
10650 break;
70d7f984
PM
10651 case 9: /* SQDMLAL, SQDMLAL2 */
10652 case 11: /* SQDMLSL, SQDMLSL2 */
10653 case 13: /* SQDMULL, SQDMULL2 */
10654 assert(size == 1);
10655 gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
10656 gen_helper_neon_addl_saturate_s32(tcg_passres, cpu_env,
10657 tcg_passres, tcg_passres);
10658 break;
a08582f4
PM
10659 default:
10660 g_assert_not_reached();
10661 }
10662 tcg_temp_free_i32(tcg_op1);
10663 tcg_temp_free_i32(tcg_op2);
10664
70d7f984
PM
10665 if (accop != 0) {
10666 if (opcode == 9 || opcode == 11) {
10667 /* saturating accumulate ops */
10668 if (accop < 0) {
10669 gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
10670 }
10671 gen_helper_neon_addl_saturate_s32(tcg_res[pass], cpu_env,
10672 tcg_res[pass],
10673 tcg_passres);
a08582f4 10674 } else {
70d7f984
PM
10675 gen_neon_addl(size, (accop < 0), tcg_res[pass],
10676 tcg_res[pass], tcg_passres);
a08582f4
PM
10677 }
10678 tcg_temp_free_i64(tcg_passres);
10679 }
10680 }
10681 }
10682
10683 write_vec_element(s, tcg_res[0], rd, 0, MO_64);
10684 write_vec_element(s, tcg_res[1], rd, 1, MO_64);
10685 tcg_temp_free_i64(tcg_res[0]);
10686 tcg_temp_free_i64(tcg_res[1]);
10687}
10688
dfc15c7c
PM
10689static void handle_3rd_wide(DisasContext *s, int is_q, int is_u, int size,
10690 int opcode, int rd, int rn, int rm)
10691{
10692 TCGv_i64 tcg_res[2];
10693 int part = is_q ? 2 : 0;
10694 int pass;
10695
10696 for (pass = 0; pass < 2; pass++) {
10697 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10698 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
10699 TCGv_i64 tcg_op2_wide = tcg_temp_new_i64();
10700 static NeonGenWidenFn * const widenfns[3][2] = {
10701 { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
10702 { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
10703 { tcg_gen_ext_i32_i64, tcg_gen_extu_i32_i64 },
10704 };
10705 NeonGenWidenFn *widenfn = widenfns[size][is_u];
10706
10707 read_vec_element(s, tcg_op1, rn, pass, MO_64);
10708 read_vec_element_i32(s, tcg_op2, rm, part + pass, MO_32);
10709 widenfn(tcg_op2_wide, tcg_op2);
10710 tcg_temp_free_i32(tcg_op2);
10711 tcg_res[pass] = tcg_temp_new_i64();
10712 gen_neon_addl(size, (opcode == 3),
10713 tcg_res[pass], tcg_op1, tcg_op2_wide);
10714 tcg_temp_free_i64(tcg_op1);
10715 tcg_temp_free_i64(tcg_op2_wide);
10716 }
10717
10718 for (pass = 0; pass < 2; pass++) {
10719 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
10720 tcg_temp_free_i64(tcg_res[pass]);
10721 }
10722}
10723
e4b998d4
PM
10724static void do_narrow_round_high_u32(TCGv_i32 res, TCGv_i64 in)
10725{
10726 tcg_gen_addi_i64(in, in, 1U << 31);
7cb36e18 10727 tcg_gen_extrh_i64_i32(res, in);
e4b998d4
PM
10728}
10729
10730static void handle_3rd_narrowing(DisasContext *s, int is_q, int is_u, int size,
10731 int opcode, int rd, int rn, int rm)
10732{
10733 TCGv_i32 tcg_res[2];
10734 int part = is_q ? 2 : 0;
10735 int pass;
10736
10737 for (pass = 0; pass < 2; pass++) {
10738 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10739 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
10740 TCGv_i64 tcg_wideres = tcg_temp_new_i64();
10741 static NeonGenNarrowFn * const narrowfns[3][2] = {
10742 { gen_helper_neon_narrow_high_u8,
10743 gen_helper_neon_narrow_round_high_u8 },
10744 { gen_helper_neon_narrow_high_u16,
10745 gen_helper_neon_narrow_round_high_u16 },
7cb36e18 10746 { tcg_gen_extrh_i64_i32, do_narrow_round_high_u32 },
e4b998d4
PM
10747 };
10748 NeonGenNarrowFn *gennarrow = narrowfns[size][is_u];
10749
10750 read_vec_element(s, tcg_op1, rn, pass, MO_64);
10751 read_vec_element(s, tcg_op2, rm, pass, MO_64);
10752
10753 gen_neon_addl(size, (opcode == 6), tcg_wideres, tcg_op1, tcg_op2);
10754
10755 tcg_temp_free_i64(tcg_op1);
10756 tcg_temp_free_i64(tcg_op2);
10757
10758 tcg_res[pass] = tcg_temp_new_i32();
10759 gennarrow(tcg_res[pass], tcg_wideres);
10760 tcg_temp_free_i64(tcg_wideres);
10761 }
10762
10763 for (pass = 0; pass < 2; pass++) {
10764 write_vec_element_i32(s, tcg_res[pass], rd, pass + part, MO_32);
10765 tcg_temp_free_i32(tcg_res[pass]);
10766 }
4ff55bcb 10767 clear_vec_high(s, is_q, rd);
e4b998d4
PM
10768}
10769
4ce31af4 10770/* AdvSIMD three different
384b26fb
AB
10771 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 0
10772 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
10773 * | 0 | Q | U | 0 1 1 1 0 | size | 1 | Rm | opcode | 0 0 | Rn | Rd |
10774 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
10775 */
10776static void disas_simd_three_reg_diff(DisasContext *s, uint32_t insn)
10777{
a08582f4
PM
10778 /* Instructions in this group fall into three basic classes
10779 * (in each case with the operation working on each element in
10780 * the input vectors):
10781 * (1) widening 64 x 64 -> 128 (with possibly Vd as an extra
10782 * 128 bit input)
10783 * (2) wide 64 x 128 -> 128
10784 * (3) narrowing 128 x 128 -> 64
10785 * Here we do initial decode, catch unallocated cases and
10786 * dispatch to separate functions for each class.
10787 */
10788 int is_q = extract32(insn, 30, 1);
10789 int is_u = extract32(insn, 29, 1);
10790 int size = extract32(insn, 22, 2);
10791 int opcode = extract32(insn, 12, 4);
10792 int rm = extract32(insn, 16, 5);
10793 int rn = extract32(insn, 5, 5);
10794 int rd = extract32(insn, 0, 5);
10795
10796 switch (opcode) {
10797 case 1: /* SADDW, SADDW2, UADDW, UADDW2 */
10798 case 3: /* SSUBW, SSUBW2, USUBW, USUBW2 */
10799 /* 64 x 128 -> 128 */
dfc15c7c
PM
10800 if (size == 3) {
10801 unallocated_encoding(s);
10802 return;
10803 }
8c6afa6a
PM
10804 if (!fp_access_check(s)) {
10805 return;
10806 }
dfc15c7c 10807 handle_3rd_wide(s, is_q, is_u, size, opcode, rd, rn, rm);
a08582f4
PM
10808 break;
10809 case 4: /* ADDHN, ADDHN2, RADDHN, RADDHN2 */
10810 case 6: /* SUBHN, SUBHN2, RSUBHN, RSUBHN2 */
10811 /* 128 x 128 -> 64 */
e4b998d4
PM
10812 if (size == 3) {
10813 unallocated_encoding(s);
10814 return;
10815 }
8c6afa6a
PM
10816 if (!fp_access_check(s)) {
10817 return;
10818 }
e4b998d4 10819 handle_3rd_narrowing(s, is_q, is_u, size, opcode, rd, rn, rm);
a08582f4 10820 break;
70d7f984 10821 case 14: /* PMULL, PMULL2 */
e7e96fc5 10822 if (is_u) {
70d7f984
PM
10823 unallocated_encoding(s);
10824 return;
10825 }
e7e96fc5
RH
10826 switch (size) {
10827 case 0: /* PMULL.P8 */
10828 if (!fp_access_check(s)) {
10829 return;
10830 }
10831 /* The Q field specifies lo/hi half input for this insn. */
10832 gen_gvec_op3_ool(s, true, rd, rn, rm, is_q,
10833 gen_helper_neon_pmull_h);
10834 break;
10835
10836 case 3: /* PMULL.P64 */
962fcbf2 10837 if (!dc_isar_feature(aa64_pmull, s)) {
a984e42c
PM
10838 unallocated_encoding(s);
10839 return;
10840 }
8c6afa6a
PM
10841 if (!fp_access_check(s)) {
10842 return;
10843 }
b9ed510e
RH
10844 /* The Q field specifies lo/hi half input for this insn. */
10845 gen_gvec_op3_ool(s, true, rd, rn, rm, is_q,
10846 gen_helper_gvec_pmull_q);
e7e96fc5
RH
10847 break;
10848
10849 default:
10850 unallocated_encoding(s);
10851 break;
a984e42c 10852 }
e7e96fc5 10853 return;
13caf1fd
PM
10854 case 9: /* SQDMLAL, SQDMLAL2 */
10855 case 11: /* SQDMLSL, SQDMLSL2 */
10856 case 13: /* SQDMULL, SQDMULL2 */
70d7f984 10857 if (is_u || size == 0) {
a08582f4
PM
10858 unallocated_encoding(s);
10859 return;
10860 }
10861 /* fall through */
13caf1fd
PM
10862 case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
10863 case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
13caf1fd
PM
10864 case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
10865 case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
10866 case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10867 case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10868 case 12: /* SMULL, SMULL2, UMULL, UMULL2 */
a08582f4
PM
10869 /* 64 x 64 -> 128 */
10870 if (size == 3) {
10871 unallocated_encoding(s);
10872 return;
10873 }
8c6afa6a
PM
10874 if (!fp_access_check(s)) {
10875 return;
10876 }
10877
a08582f4
PM
10878 handle_3rd_widening(s, is_q, is_u, size, opcode, rd, rn, rm);
10879 break;
10880 default:
10881 /* opcode 15 not allocated */
10882 unallocated_encoding(s);
10883 break;
10884 }
384b26fb
AB
10885}
10886
e1cea114
PM
10887/* Logic op (opcode == 3) subgroup of C3.6.16. */
10888static void disas_simd_3same_logic(DisasContext *s, uint32_t insn)
10889{
956d272e
PM
10890 int rd = extract32(insn, 0, 5);
10891 int rn = extract32(insn, 5, 5);
10892 int rm = extract32(insn, 16, 5);
10893 int size = extract32(insn, 22, 2);
10894 bool is_u = extract32(insn, 29, 1);
10895 bool is_q = extract32(insn, 30, 1);
956d272e 10896
8c6afa6a
PM
10897 if (!fp_access_check(s)) {
10898 return;
10899 }
10900
bc48092f
RH
10901 switch (size + 4 * is_u) {
10902 case 0: /* AND */
10903 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_and, 0);
10904 return;
10905 case 1: /* BIC */
10906 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_andc, 0);
10907 return;
10908 case 2: /* ORR */
2900847f 10909 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_or, 0);
bc48092f
RH
10910 return;
10911 case 3: /* ORN */
10912 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_orc, 0);
10913 return;
10914 case 4: /* EOR */
10915 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_xor, 0);
10916 return;
956d272e 10917
bc48092f 10918 case 5: /* BSL bitwise select */
3a7a2b4e 10919 gen_gvec_fn4(s, is_q, rd, rd, rn, rm, tcg_gen_gvec_bitsel, 0);
bc48092f
RH
10920 return;
10921 case 6: /* BIT, bitwise insert if true */
3a7a2b4e 10922 gen_gvec_fn4(s, is_q, rd, rm, rn, rd, tcg_gen_gvec_bitsel, 0);
bc48092f
RH
10923 return;
10924 case 7: /* BIF, bitwise insert if false */
3a7a2b4e 10925 gen_gvec_fn4(s, is_q, rd, rm, rd, rn, tcg_gen_gvec_bitsel, 0);
bc48092f 10926 return;
956d272e 10927
bc48092f
RH
10928 default:
10929 g_assert_not_reached();
956d272e 10930 }
e1cea114
PM
10931}
10932
bc242f9b
AB
10933/* Pairwise op subgroup of C3.6.16.
10934 *
10935 * This is called directly or via the handle_3same_float for float pairwise
10936 * operations where the opcode and size are calculated differently.
10937 */
10938static void handle_simd_3same_pair(DisasContext *s, int is_q, int u, int opcode,
10939 int size, int rn, int rm, int rd)
e1cea114 10940{
bc242f9b 10941 TCGv_ptr fpst;
0173a005
PM
10942 int pass;
10943
bc242f9b
AB
10944 /* Floating point operations need fpst */
10945 if (opcode >= 0x58) {
d81ce0ef 10946 fpst = get_fpstatus_ptr(false);
bc242f9b 10947 } else {
f764718d 10948 fpst = NULL;
0173a005
PM
10949 }
10950
8c6afa6a
PM
10951 if (!fp_access_check(s)) {
10952 return;
10953 }
10954
0173a005
PM
10955 /* These operations work on the concatenated rm:rn, with each pair of
10956 * adjacent elements being operated on to produce an element in the result.
10957 */
10958 if (size == 3) {
10959 TCGv_i64 tcg_res[2];
10960
10961 for (pass = 0; pass < 2; pass++) {
10962 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
10963 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
10964 int passreg = (pass == 0) ? rn : rm;
10965
10966 read_vec_element(s, tcg_op1, passreg, 0, MO_64);
10967 read_vec_element(s, tcg_op2, passreg, 1, MO_64);
10968 tcg_res[pass] = tcg_temp_new_i64();
10969
bc242f9b
AB
10970 switch (opcode) {
10971 case 0x17: /* ADDP */
10972 tcg_gen_add_i64(tcg_res[pass], tcg_op1, tcg_op2);
10973 break;
10974 case 0x58: /* FMAXNMP */
10975 gen_helper_vfp_maxnumd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10976 break;
10977 case 0x5a: /* FADDP */
10978 gen_helper_vfp_addd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10979 break;
10980 case 0x5e: /* FMAXP */
10981 gen_helper_vfp_maxd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10982 break;
10983 case 0x78: /* FMINNMP */
10984 gen_helper_vfp_minnumd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10985 break;
10986 case 0x7e: /* FMINP */
10987 gen_helper_vfp_mind(tcg_res[pass], tcg_op1, tcg_op2, fpst);
10988 break;
10989 default:
10990 g_assert_not_reached();
10991 }
0173a005
PM
10992
10993 tcg_temp_free_i64(tcg_op1);
10994 tcg_temp_free_i64(tcg_op2);
10995 }
10996
10997 for (pass = 0; pass < 2; pass++) {
10998 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
10999 tcg_temp_free_i64(tcg_res[pass]);
11000 }
11001 } else {
11002 int maxpass = is_q ? 4 : 2;
11003 TCGv_i32 tcg_res[4];
11004
11005 for (pass = 0; pass < maxpass; pass++) {
11006 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11007 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
bc242f9b 11008 NeonGenTwoOpFn *genfn = NULL;
0173a005
PM
11009 int passreg = pass < (maxpass / 2) ? rn : rm;
11010 int passelt = (is_q && (pass & 1)) ? 2 : 0;
11011
11012 read_vec_element_i32(s, tcg_op1, passreg, passelt, MO_32);
11013 read_vec_element_i32(s, tcg_op2, passreg, passelt + 1, MO_32);
11014 tcg_res[pass] = tcg_temp_new_i32();
11015
11016 switch (opcode) {
11017 case 0x17: /* ADDP */
11018 {
11019 static NeonGenTwoOpFn * const fns[3] = {
11020 gen_helper_neon_padd_u8,
11021 gen_helper_neon_padd_u16,
11022 tcg_gen_add_i32,
11023 };
11024 genfn = fns[size];
11025 break;
11026 }
11027 case 0x14: /* SMAXP, UMAXP */
11028 {
11029 static NeonGenTwoOpFn * const fns[3][2] = {
11030 { gen_helper_neon_pmax_s8, gen_helper_neon_pmax_u8 },
11031 { gen_helper_neon_pmax_s16, gen_helper_neon_pmax_u16 },
ecb8ab8d 11032 { tcg_gen_smax_i32, tcg_gen_umax_i32 },
0173a005
PM
11033 };
11034 genfn = fns[size][u];
11035 break;
11036 }
11037 case 0x15: /* SMINP, UMINP */
11038 {
11039 static NeonGenTwoOpFn * const fns[3][2] = {
11040 { gen_helper_neon_pmin_s8, gen_helper_neon_pmin_u8 },
11041 { gen_helper_neon_pmin_s16, gen_helper_neon_pmin_u16 },
ecb8ab8d 11042 { tcg_gen_smin_i32, tcg_gen_umin_i32 },
0173a005
PM
11043 };
11044 genfn = fns[size][u];
11045 break;
11046 }
bc242f9b
AB
11047 /* The FP operations are all on single floats (32 bit) */
11048 case 0x58: /* FMAXNMP */
11049 gen_helper_vfp_maxnums(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11050 break;
11051 case 0x5a: /* FADDP */
11052 gen_helper_vfp_adds(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11053 break;
11054 case 0x5e: /* FMAXP */
11055 gen_helper_vfp_maxs(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11056 break;
11057 case 0x78: /* FMINNMP */
11058 gen_helper_vfp_minnums(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11059 break;
11060 case 0x7e: /* FMINP */
11061 gen_helper_vfp_mins(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11062 break;
0173a005
PM
11063 default:
11064 g_assert_not_reached();
11065 }
11066
bc242f9b
AB
11067 /* FP ops called directly, otherwise call now */
11068 if (genfn) {
11069 genfn(tcg_res[pass], tcg_op1, tcg_op2);
11070 }
0173a005
PM
11071
11072 tcg_temp_free_i32(tcg_op1);
11073 tcg_temp_free_i32(tcg_op2);
11074 }
11075
11076 for (pass = 0; pass < maxpass; pass++) {
11077 write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_32);
11078 tcg_temp_free_i32(tcg_res[pass]);
11079 }
4ff55bcb 11080 clear_vec_high(s, is_q, rd);
0173a005 11081 }
bc242f9b 11082
f764718d 11083 if (fpst) {
bc242f9b
AB
11084 tcg_temp_free_ptr(fpst);
11085 }
e1cea114
PM
11086}
11087
11088/* Floating point op subgroup of C3.6.16. */
11089static void disas_simd_3same_float(DisasContext *s, uint32_t insn)
11090{
845ea09a
PM
11091 /* For floating point ops, the U, size[1] and opcode bits
11092 * together indicate the operation. size[0] indicates single
11093 * or double.
11094 */
11095 int fpopcode = extract32(insn, 11, 5)
11096 | (extract32(insn, 23, 1) << 5)
11097 | (extract32(insn, 29, 1) << 6);
11098 int is_q = extract32(insn, 30, 1);
11099 int size = extract32(insn, 22, 1);
11100 int rm = extract32(insn, 16, 5);
11101 int rn = extract32(insn, 5, 5);
11102 int rd = extract32(insn, 0, 5);
11103
11104 int datasize = is_q ? 128 : 64;
11105 int esize = 32 << size;
11106 int elements = datasize / esize;
11107
11108 if (size == 1 && !is_q) {
11109 unallocated_encoding(s);
11110 return;
11111 }
11112
11113 switch (fpopcode) {
11114 case 0x58: /* FMAXNMP */
11115 case 0x5a: /* FADDP */
11116 case 0x5e: /* FMAXP */
11117 case 0x78: /* FMINNMP */
11118 case 0x7e: /* FMINP */
bc242f9b
AB
11119 if (size && !is_q) {
11120 unallocated_encoding(s);
11121 return;
11122 }
11123 handle_simd_3same_pair(s, is_q, 0, fpopcode, size ? MO_64 : MO_32,
11124 rn, rm, rd);
845ea09a
PM
11125 return;
11126 case 0x1b: /* FMULX */
845ea09a
PM
11127 case 0x1f: /* FRECPS */
11128 case 0x3f: /* FRSQRTS */
845ea09a 11129 case 0x5d: /* FACGE */
845ea09a
PM
11130 case 0x7d: /* FACGT */
11131 case 0x19: /* FMLA */
11132 case 0x39: /* FMLS */
845ea09a
PM
11133 case 0x18: /* FMAXNM */
11134 case 0x1a: /* FADD */
8908f4d1 11135 case 0x1c: /* FCMEQ */
845ea09a
PM
11136 case 0x1e: /* FMAX */
11137 case 0x38: /* FMINNM */
11138 case 0x3a: /* FSUB */
11139 case 0x3e: /* FMIN */
11140 case 0x5b: /* FMUL */
8908f4d1 11141 case 0x5c: /* FCMGE */
845ea09a
PM
11142 case 0x5f: /* FDIV */
11143 case 0x7a: /* FABD */
8908f4d1 11144 case 0x7c: /* FCMGT */
8c6afa6a
PM
11145 if (!fp_access_check(s)) {
11146 return;
11147 }
845ea09a
PM
11148 handle_3same_float(s, size, elements, fpopcode, rd, rn, rm);
11149 return;
0caa5af8
RH
11150
11151 case 0x1d: /* FMLAL */
11152 case 0x3d: /* FMLSL */
11153 case 0x59: /* FMLAL2 */
11154 case 0x79: /* FMLSL2 */
11155 if (size & 1 || !dc_isar_feature(aa64_fhm, s)) {
11156 unallocated_encoding(s);
11157 return;
11158 }
11159 if (fp_access_check(s)) {
11160 int is_s = extract32(insn, 23, 1);
11161 int is_2 = extract32(insn, 29, 1);
11162 int data = (is_2 << 1) | is_s;
11163 tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
11164 vec_full_reg_offset(s, rn),
11165 vec_full_reg_offset(s, rm), cpu_env,
11166 is_q ? 16 : 8, vec_full_reg_size(s),
11167 data, gen_helper_gvec_fmlal_a64);
11168 }
11169 return;
11170
845ea09a
PM
11171 default:
11172 unallocated_encoding(s);
11173 return;
11174 }
e1cea114
PM
11175}
11176
11177/* Integer op subgroup of C3.6.16. */
11178static void disas_simd_3same_int(DisasContext *s, uint32_t insn)
11179{
1f8a73af
PM
11180 int is_q = extract32(insn, 30, 1);
11181 int u = extract32(insn, 29, 1);
11182 int size = extract32(insn, 22, 2);
11183 int opcode = extract32(insn, 11, 5);
11184 int rm = extract32(insn, 16, 5);
11185 int rn = extract32(insn, 5, 5);
11186 int rd = extract32(insn, 0, 5);
11187 int pass;
79d61de6 11188 TCGCond cond;
1f8a73af
PM
11189
11190 switch (opcode) {
11191 case 0x13: /* MUL, PMUL */
11192 if (u && size != 0) {
11193 unallocated_encoding(s);
11194 return;
11195 }
11196 /* fall through */
11197 case 0x0: /* SHADD, UHADD */
11198 case 0x2: /* SRHADD, URHADD */
11199 case 0x4: /* SHSUB, UHSUB */
11200 case 0xc: /* SMAX, UMAX */
11201 case 0xd: /* SMIN, UMIN */
11202 case 0xe: /* SABD, UABD */
11203 case 0xf: /* SABA, UABA */
11204 case 0x12: /* MLA, MLS */
11205 if (size == 3) {
11206 unallocated_encoding(s);
11207 return;
11208 }
8b12a0cf 11209 break;
1f8a73af
PM
11210 case 0x16: /* SQDMULH, SQRDMULH */
11211 if (size == 0 || size == 3) {
11212 unallocated_encoding(s);
11213 return;
11214 }
8b12a0cf 11215 break;
1f8a73af
PM
11216 default:
11217 if (size == 3 && !is_q) {
11218 unallocated_encoding(s);
11219 return;
11220 }
11221 break;
11222 }
11223
8c6afa6a
PM
11224 if (!fp_access_check(s)) {
11225 return;
11226 }
11227
bc48092f 11228 switch (opcode) {
89e68b57
RH
11229 case 0x01: /* SQADD, UQADD */
11230 tcg_gen_gvec_4(vec_full_reg_offset(s, rd),
11231 offsetof(CPUARMState, vfp.qc),
11232 vec_full_reg_offset(s, rn),
11233 vec_full_reg_offset(s, rm),
11234 is_q ? 16 : 8, vec_full_reg_size(s),
11235 (u ? uqadd_op : sqadd_op) + size);
11236 return;
11237 case 0x05: /* SQSUB, UQSUB */
11238 tcg_gen_gvec_4(vec_full_reg_offset(s, rd),
11239 offsetof(CPUARMState, vfp.qc),
11240 vec_full_reg_offset(s, rn),
11241 vec_full_reg_offset(s, rm),
11242 is_q ? 16 : 8, vec_full_reg_size(s),
11243 (u ? uqsub_op : sqsub_op) + size);
11244 return;
87b74e8b
RH
11245 case 0x08: /* SSHL, USHL */
11246 gen_gvec_op3(s, is_q, rd, rn, rm,
11247 u ? &ushl_op[size] : &sshl_op[size]);
11248 return;
264d2a48
RH
11249 case 0x0c: /* SMAX, UMAX */
11250 if (u) {
11251 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_umax, size);
11252 } else {
11253 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_smax, size);
11254 }
11255 return;
11256 case 0x0d: /* SMIN, UMIN */
11257 if (u) {
11258 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_umin, size);
11259 } else {
11260 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_smin, size);
11261 }
11262 return;
bc48092f
RH
11263 case 0x10: /* ADD, SUB */
11264 if (u) {
11265 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_sub, size);
11266 } else {
11267 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_add, size);
11268 }
11269 return;
0c7c55c4
RH
11270 case 0x13: /* MUL, PMUL */
11271 if (!u) { /* MUL */
11272 gen_gvec_fn3(s, is_q, rd, rn, rm, tcg_gen_gvec_mul, size);
a21bb78e
RH
11273 } else { /* PMUL */
11274 gen_gvec_op3_ool(s, is_q, rd, rn, rm, 0, gen_helper_gvec_pmul_b);
0c7c55c4 11275 }
a21bb78e 11276 return;
0c7c55c4
RH
11277 case 0x12: /* MLA, MLS */
11278 if (u) {
11279 gen_gvec_op3(s, is_q, rd, rn, rm, &mls_op[size]);
11280 } else {
11281 gen_gvec_op3(s, is_q, rd, rn, rm, &mla_op[size]);
11282 }
11283 return;
79d61de6
RH
11284 case 0x11:
11285 if (!u) { /* CMTST */
11286 gen_gvec_op3(s, is_q, rd, rn, rm, &cmtst_op[size]);
11287 return;
11288 }
11289 /* else CMEQ */
11290 cond = TCG_COND_EQ;
11291 goto do_gvec_cmp;
11292 case 0x06: /* CMGT, CMHI */
11293 cond = u ? TCG_COND_GTU : TCG_COND_GT;
11294 goto do_gvec_cmp;
11295 case 0x07: /* CMGE, CMHS */
11296 cond = u ? TCG_COND_GEU : TCG_COND_GE;
11297 do_gvec_cmp:
11298 tcg_gen_gvec_cmp(cond, size, vec_full_reg_offset(s, rd),
11299 vec_full_reg_offset(s, rn),
11300 vec_full_reg_offset(s, rm),
11301 is_q ? 16 : 8, vec_full_reg_size(s));
11302 return;
bc48092f
RH
11303 }
11304
1f8a73af 11305 if (size == 3) {
220ad4ca
PM
11306 assert(is_q);
11307 for (pass = 0; pass < 2; pass++) {
1f8a73af
PM
11308 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
11309 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
11310 TCGv_i64 tcg_res = tcg_temp_new_i64();
11311
11312 read_vec_element(s, tcg_op1, rn, pass, MO_64);
11313 read_vec_element(s, tcg_op2, rm, pass, MO_64);
11314
11315 handle_3same_64(s, opcode, u, tcg_res, tcg_op1, tcg_op2);
11316
11317 write_vec_element(s, tcg_res, rd, pass, MO_64);
11318
11319 tcg_temp_free_i64(tcg_res);
11320 tcg_temp_free_i64(tcg_op1);
11321 tcg_temp_free_i64(tcg_op2);
11322 }
11323 } else {
11324 for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
11325 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11326 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11327 TCGv_i32 tcg_res = tcg_temp_new_i32();
6d9571f7
PM
11328 NeonGenTwoOpFn *genfn = NULL;
11329 NeonGenTwoOpEnvFn *genenvfn = NULL;
1f8a73af
PM
11330
11331 read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
11332 read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
11333
11334 switch (opcode) {
8b12a0cf
PM
11335 case 0x0: /* SHADD, UHADD */
11336 {
11337 static NeonGenTwoOpFn * const fns[3][2] = {
11338 { gen_helper_neon_hadd_s8, gen_helper_neon_hadd_u8 },
11339 { gen_helper_neon_hadd_s16, gen_helper_neon_hadd_u16 },
11340 { gen_helper_neon_hadd_s32, gen_helper_neon_hadd_u32 },
11341 };
11342 genfn = fns[size][u];
11343 break;
11344 }
8b12a0cf
PM
11345 case 0x2: /* SRHADD, URHADD */
11346 {
11347 static NeonGenTwoOpFn * const fns[3][2] = {
11348 { gen_helper_neon_rhadd_s8, gen_helper_neon_rhadd_u8 },
11349 { gen_helper_neon_rhadd_s16, gen_helper_neon_rhadd_u16 },
11350 { gen_helper_neon_rhadd_s32, gen_helper_neon_rhadd_u32 },
11351 };
11352 genfn = fns[size][u];
11353 break;
11354 }
11355 case 0x4: /* SHSUB, UHSUB */
11356 {
11357 static NeonGenTwoOpFn * const fns[3][2] = {
11358 { gen_helper_neon_hsub_s8, gen_helper_neon_hsub_u8 },
11359 { gen_helper_neon_hsub_s16, gen_helper_neon_hsub_u16 },
11360 { gen_helper_neon_hsub_s32, gen_helper_neon_hsub_u32 },
11361 };
11362 genfn = fns[size][u];
11363 break;
11364 }
6d9571f7
PM
11365 case 0x9: /* SQSHL, UQSHL */
11366 {
11367 static NeonGenTwoOpEnvFn * const fns[3][2] = {
11368 { gen_helper_neon_qshl_s8, gen_helper_neon_qshl_u8 },
11369 { gen_helper_neon_qshl_s16, gen_helper_neon_qshl_u16 },
11370 { gen_helper_neon_qshl_s32, gen_helper_neon_qshl_u32 },
11371 };
11372 genenvfn = fns[size][u];
11373 break;
11374 }
11375 case 0xa: /* SRSHL, URSHL */
11376 {
11377 static NeonGenTwoOpFn * const fns[3][2] = {
11378 { gen_helper_neon_rshl_s8, gen_helper_neon_rshl_u8 },
11379 { gen_helper_neon_rshl_s16, gen_helper_neon_rshl_u16 },
11380 { gen_helper_neon_rshl_s32, gen_helper_neon_rshl_u32 },
11381 };
11382 genfn = fns[size][u];
11383 break;
11384 }
11385 case 0xb: /* SQRSHL, UQRSHL */
11386 {
11387 static NeonGenTwoOpEnvFn * const fns[3][2] = {
11388 { gen_helper_neon_qrshl_s8, gen_helper_neon_qrshl_u8 },
11389 { gen_helper_neon_qrshl_s16, gen_helper_neon_qrshl_u16 },
11390 { gen_helper_neon_qrshl_s32, gen_helper_neon_qrshl_u32 },
11391 };
11392 genenvfn = fns[size][u];
11393 break;
11394 }
8b12a0cf
PM
11395 case 0xe: /* SABD, UABD */
11396 case 0xf: /* SABA, UABA */
11397 {
11398 static NeonGenTwoOpFn * const fns[3][2] = {
11399 { gen_helper_neon_abd_s8, gen_helper_neon_abd_u8 },
11400 { gen_helper_neon_abd_s16, gen_helper_neon_abd_u16 },
11401 { gen_helper_neon_abd_s32, gen_helper_neon_abd_u32 },
11402 };
11403 genfn = fns[size][u];
11404 break;
11405 }
8b12a0cf
PM
11406 case 0x16: /* SQDMULH, SQRDMULH */
11407 {
11408 static NeonGenTwoOpEnvFn * const fns[2][2] = {
11409 { gen_helper_neon_qdmulh_s16, gen_helper_neon_qrdmulh_s16 },
11410 { gen_helper_neon_qdmulh_s32, gen_helper_neon_qrdmulh_s32 },
11411 };
11412 assert(size == 1 || size == 2);
11413 genenvfn = fns[size - 1][u];
11414 break;
11415 }
1f8a73af
PM
11416 default:
11417 g_assert_not_reached();
11418 }
11419
6d9571f7
PM
11420 if (genenvfn) {
11421 genenvfn(tcg_res, cpu_env, tcg_op1, tcg_op2);
11422 } else {
11423 genfn(tcg_res, tcg_op1, tcg_op2);
11424 }
1f8a73af 11425
0c7c55c4
RH
11426 if (opcode == 0xf) {
11427 /* SABA, UABA: accumulating ops */
11428 static NeonGenTwoOpFn * const fns[3] = {
11429 gen_helper_neon_add_u8,
11430 gen_helper_neon_add_u16,
11431 tcg_gen_add_i32,
8b12a0cf 11432 };
8b12a0cf 11433
8b12a0cf 11434 read_vec_element_i32(s, tcg_op1, rd, pass, MO_32);
0c7c55c4 11435 fns[size](tcg_res, tcg_op1, tcg_res);
8b12a0cf
PM
11436 }
11437
1f8a73af
PM
11438 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
11439
11440 tcg_temp_free_i32(tcg_res);
11441 tcg_temp_free_i32(tcg_op1);
11442 tcg_temp_free_i32(tcg_op2);
11443 }
11444 }
4ff55bcb 11445 clear_vec_high(s, is_q, rd);
e1cea114
PM
11446}
11447
4ce31af4 11448/* AdvSIMD three same
384b26fb
AB
11449 * 31 30 29 28 24 23 22 21 20 16 15 11 10 9 5 4 0
11450 * +---+---+---+-----------+------+---+------+--------+---+------+------+
11451 * | 0 | Q | U | 0 1 1 1 0 | size | 1 | Rm | opcode | 1 | Rn | Rd |
11452 * +---+---+---+-----------+------+---+------+--------+---+------+------+
11453 */
11454static void disas_simd_three_reg_same(DisasContext *s, uint32_t insn)
11455{
e1cea114
PM
11456 int opcode = extract32(insn, 11, 5);
11457
11458 switch (opcode) {
11459 case 0x3: /* logic ops */
11460 disas_simd_3same_logic(s, insn);
11461 break;
11462 case 0x17: /* ADDP */
11463 case 0x14: /* SMAXP, UMAXP */
11464 case 0x15: /* SMINP, UMINP */
bc242f9b 11465 {
e1cea114 11466 /* Pairwise operations */
bc242f9b
AB
11467 int is_q = extract32(insn, 30, 1);
11468 int u = extract32(insn, 29, 1);
11469 int size = extract32(insn, 22, 2);
11470 int rm = extract32(insn, 16, 5);
11471 int rn = extract32(insn, 5, 5);
11472 int rd = extract32(insn, 0, 5);
11473 if (opcode == 0x17) {
11474 if (u || (size == 3 && !is_q)) {
11475 unallocated_encoding(s);
11476 return;
11477 }
11478 } else {
11479 if (size == 3) {
11480 unallocated_encoding(s);
11481 return;
11482 }
11483 }
11484 handle_simd_3same_pair(s, is_q, u, opcode, size, rn, rm, rd);
e1cea114 11485 break;
bc242f9b 11486 }
e1cea114
PM
11487 case 0x18 ... 0x31:
11488 /* floating point ops, sz[1] and U are part of opcode */
11489 disas_simd_3same_float(s, insn);
11490 break;
11491 default:
11492 disas_simd_3same_int(s, insn);
11493 break;
11494 }
384b26fb
AB
11495}
11496
376e8d6c
AB
11497/*
11498 * Advanced SIMD three same (ARMv8.2 FP16 variants)
11499 *
11500 * 31 30 29 28 24 23 22 21 20 16 15 14 13 11 10 9 5 4 0
11501 * +---+---+---+-----------+---------+------+-----+--------+---+------+------+
11502 * | 0 | Q | U | 0 1 1 1 0 | a | 1 0 | Rm | 0 0 | opcode | 1 | Rn | Rd |
11503 * +---+---+---+-----------+---------+------+-----+--------+---+------+------+
11504 *
11505 * This includes FMULX, FCMEQ (register), FRECPS, FRSQRTS, FCMGE
11506 * (register), FACGE, FABD, FCMGT (register) and FACGT.
11507 *
11508 */
11509static void disas_simd_three_reg_same_fp16(DisasContext *s, uint32_t insn)
11510{
11511 int opcode, fpopcode;
11512 int is_q, u, a, rm, rn, rd;
11513 int datasize, elements;
11514 int pass;
11515 TCGv_ptr fpst;
7a2c6e61 11516 bool pairwise = false;
376e8d6c 11517
5763190f 11518 if (!dc_isar_feature(aa64_fp16, s)) {
376e8d6c
AB
11519 unallocated_encoding(s);
11520 return;
11521 }
11522
11523 if (!fp_access_check(s)) {
11524 return;
11525 }
11526
11527 /* For these floating point ops, the U, a and opcode bits
11528 * together indicate the operation.
11529 */
11530 opcode = extract32(insn, 11, 3);
11531 u = extract32(insn, 29, 1);
11532 a = extract32(insn, 23, 1);
11533 is_q = extract32(insn, 30, 1);
11534 rm = extract32(insn, 16, 5);
11535 rn = extract32(insn, 5, 5);
11536 rd = extract32(insn, 0, 5);
11537
11538 fpopcode = opcode | (a << 3) | (u << 4);
11539 datasize = is_q ? 128 : 64;
11540 elements = datasize / 16;
11541
7a2c6e61
AB
11542 switch (fpopcode) {
11543 case 0x10: /* FMAXNMP */
11544 case 0x12: /* FADDP */
11545 case 0x16: /* FMAXP */
11546 case 0x18: /* FMINNMP */
11547 case 0x1e: /* FMINP */
11548 pairwise = true;
11549 break;
11550 }
11551
376e8d6c
AB
11552 fpst = get_fpstatus_ptr(true);
11553
7a2c6e61
AB
11554 if (pairwise) {
11555 int maxpass = is_q ? 8 : 4;
376e8d6c
AB
11556 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11557 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
7a2c6e61 11558 TCGv_i32 tcg_res[8];
376e8d6c 11559
7a2c6e61
AB
11560 for (pass = 0; pass < maxpass; pass++) {
11561 int passreg = pass < (maxpass / 2) ? rn : rm;
11562 int passelt = (pass << 1) & (maxpass - 1);
376e8d6c 11563
7a2c6e61
AB
11564 read_vec_element_i32(s, tcg_op1, passreg, passelt, MO_16);
11565 read_vec_element_i32(s, tcg_op2, passreg, passelt + 1, MO_16);
11566 tcg_res[pass] = tcg_temp_new_i32();
11567
11568 switch (fpopcode) {
11569 case 0x10: /* FMAXNMP */
11570 gen_helper_advsimd_maxnumh(tcg_res[pass], tcg_op1, tcg_op2,
11571 fpst);
11572 break;
11573 case 0x12: /* FADDP */
11574 gen_helper_advsimd_addh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11575 break;
11576 case 0x16: /* FMAXP */
11577 gen_helper_advsimd_maxh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11578 break;
11579 case 0x18: /* FMINNMP */
11580 gen_helper_advsimd_minnumh(tcg_res[pass], tcg_op1, tcg_op2,
11581 fpst);
11582 break;
11583 case 0x1e: /* FMINP */
11584 gen_helper_advsimd_minh(tcg_res[pass], tcg_op1, tcg_op2, fpst);
11585 break;
11586 default:
11587 g_assert_not_reached();
11588 }
11589 }
11590
11591 for (pass = 0; pass < maxpass; pass++) {
11592 write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_16);
11593 tcg_temp_free_i32(tcg_res[pass]);
376e8d6c
AB
11594 }
11595
376e8d6c
AB
11596 tcg_temp_free_i32(tcg_op1);
11597 tcg_temp_free_i32(tcg_op2);
7a2c6e61
AB
11598
11599 } else {
11600 for (pass = 0; pass < elements; pass++) {
11601 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
11602 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
11603 TCGv_i32 tcg_res = tcg_temp_new_i32();
11604
11605 read_vec_element_i32(s, tcg_op1, rn, pass, MO_16);
11606 read_vec_element_i32(s, tcg_op2, rm, pass, MO_16);
11607
11608 switch (fpopcode) {
11609 case 0x0: /* FMAXNM */
11610 gen_helper_advsimd_maxnumh(tcg_res, tcg_op1, tcg_op2, fpst);
11611 break;
11612 case 0x1: /* FMLA */
11613 read_vec_element_i32(s, tcg_res, rd, pass, MO_16);
11614 gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_res,
11615 fpst);
11616 break;
11617 case 0x2: /* FADD */
11618 gen_helper_advsimd_addh(tcg_res, tcg_op1, tcg_op2, fpst);
11619 break;
11620 case 0x3: /* FMULX */
11621 gen_helper_advsimd_mulxh(tcg_res, tcg_op1, tcg_op2, fpst);
11622 break;
11623 case 0x4: /* FCMEQ */
11624 gen_helper_advsimd_ceq_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11625 break;
11626 case 0x6: /* FMAX */
11627 gen_helper_advsimd_maxh(tcg_res, tcg_op1, tcg_op2, fpst);
11628 break;
11629 case 0x7: /* FRECPS */
11630 gen_helper_recpsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11631 break;
11632 case 0x8: /* FMINNM */
11633 gen_helper_advsimd_minnumh(tcg_res, tcg_op1, tcg_op2, fpst);
11634 break;
11635 case 0x9: /* FMLS */
11636 /* As usual for ARM, separate negation for fused multiply-add */
11637 tcg_gen_xori_i32(tcg_op1, tcg_op1, 0x8000);
11638 read_vec_element_i32(s, tcg_res, rd, pass, MO_16);
11639 gen_helper_advsimd_muladdh(tcg_res, tcg_op1, tcg_op2, tcg_res,
11640 fpst);
11641 break;
11642 case 0xa: /* FSUB */
11643 gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
11644 break;
11645 case 0xe: /* FMIN */
11646 gen_helper_advsimd_minh(tcg_res, tcg_op1, tcg_op2, fpst);
11647 break;
11648 case 0xf: /* FRSQRTS */
11649 gen_helper_rsqrtsf_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11650 break;
11651 case 0x13: /* FMUL */
11652 gen_helper_advsimd_mulh(tcg_res, tcg_op1, tcg_op2, fpst);
11653 break;
11654 case 0x14: /* FCMGE */
11655 gen_helper_advsimd_cge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11656 break;
11657 case 0x15: /* FACGE */
11658 gen_helper_advsimd_acge_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11659 break;
11660 case 0x17: /* FDIV */
11661 gen_helper_advsimd_divh(tcg_res, tcg_op1, tcg_op2, fpst);
11662 break;
11663 case 0x1a: /* FABD */
11664 gen_helper_advsimd_subh(tcg_res, tcg_op1, tcg_op2, fpst);
11665 tcg_gen_andi_i32(tcg_res, tcg_res, 0x7fff);
11666 break;
11667 case 0x1c: /* FCMGT */
11668 gen_helper_advsimd_cgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11669 break;
11670 case 0x1d: /* FACGT */
11671 gen_helper_advsimd_acgt_f16(tcg_res, tcg_op1, tcg_op2, fpst);
11672 break;
11673 default:
11674 fprintf(stderr, "%s: insn %#04x, fpop %#2x @ %#" PRIx64 "\n",
43722a6d 11675 __func__, insn, fpopcode, s->pc_curr);
7a2c6e61
AB
11676 g_assert_not_reached();
11677 }
11678
11679 write_vec_element_i32(s, tcg_res, rd, pass, MO_16);
11680 tcg_temp_free_i32(tcg_res);
11681 tcg_temp_free_i32(tcg_op1);
11682 tcg_temp_free_i32(tcg_op2);
11683 }
376e8d6c
AB
11684 }
11685
11686 tcg_temp_free_ptr(fpst);
11687
11688 clear_vec_high(s, is_q, rd);
11689}
11690
e7186d82
RH
11691/* AdvSIMD three same extra
11692 * 31 30 29 28 24 23 22 21 20 16 15 14 11 10 9 5 4 0
11693 * +---+---+---+-----------+------+---+------+---+--------+---+----+----+
11694 * | 0 | Q | U | 0 1 1 1 0 | size | 0 | Rm | 1 | opcode | 1 | Rn | Rd |
11695 * +---+---+---+-----------+------+---+------+---+--------+---+----+----+
11696 */
11697static void disas_simd_three_reg_same_extra(DisasContext *s, uint32_t insn)
11698{
11699 int rd = extract32(insn, 0, 5);
11700 int rn = extract32(insn, 5, 5);
11701 int opcode = extract32(insn, 11, 4);
11702 int rm = extract32(insn, 16, 5);
11703 int size = extract32(insn, 22, 2);
11704 bool u = extract32(insn, 29, 1);
11705 bool is_q = extract32(insn, 30, 1);
962fcbf2
RH
11706 bool feature;
11707 int rot;
e7186d82
RH
11708
11709 switch (u * 16 + opcode) {
11710 case 0x10: /* SQRDMLAH (vector) */
11711 case 0x11: /* SQRDMLSH (vector) */
11712 if (size != 1 && size != 2) {
11713 unallocated_encoding(s);
11714 return;
11715 }
962fcbf2 11716 feature = dc_isar_feature(aa64_rdm, s);
e7186d82 11717 break;
26c470a7
RH
11718 case 0x02: /* SDOT (vector) */
11719 case 0x12: /* UDOT (vector) */
11720 if (size != MO_32) {
11721 unallocated_encoding(s);
11722 return;
11723 }
962fcbf2 11724 feature = dc_isar_feature(aa64_dp, s);
26c470a7 11725 break;
b8a4a96d
RH
11726 case 0x18: /* FCMLA, #0 */
11727 case 0x19: /* FCMLA, #90 */
11728 case 0x1a: /* FCMLA, #180 */
11729 case 0x1b: /* FCMLA, #270 */
11730 case 0x1c: /* FCADD, #90 */
11731 case 0x1e: /* FCADD, #270 */
1695cd61 11732 if (size == 0
5763190f 11733 || (size == 1 && !dc_isar_feature(aa64_fp16, s))
1695cd61
RH
11734 || (size == 3 && !is_q)) {
11735 unallocated_encoding(s);
11736 return;
11737 }
962fcbf2 11738 feature = dc_isar_feature(aa64_fcma, s);
1695cd61 11739 break;
e7186d82
RH
11740 default:
11741 unallocated_encoding(s);
11742 return;
11743 }
962fcbf2 11744 if (!feature) {
e7186d82
RH
11745 unallocated_encoding(s);
11746 return;
11747 }
11748 if (!fp_access_check(s)) {
11749 return;
11750 }
11751
11752 switch (opcode) {
11753 case 0x0: /* SQRDMLAH (vector) */
11754 switch (size) {
11755 case 1:
11756 gen_gvec_op3_env(s, is_q, rd, rn, rm, gen_helper_gvec_qrdmlah_s16);
11757 break;
11758 case 2:
11759 gen_gvec_op3_env(s, is_q, rd, rn, rm, gen_helper_gvec_qrdmlah_s32);
11760 break;
11761 default:
11762 g_assert_not_reached();
11763 }
11764 return;
11765
11766 case 0x1: /* SQRDMLSH (vector) */
11767 switch (size) {
11768 case 1:
11769 gen_gvec_op3_env(s, is_q, rd, rn, rm, gen_helper_gvec_qrdmlsh_s16);
11770 break;
11771 case 2:
11772 gen_gvec_op3_env(s, is_q, rd, rn, rm, gen_helper_gvec_qrdmlsh_s32);
11773 break;
11774 default:
11775 g_assert_not_reached();
11776 }
11777 return;
11778
26c470a7
RH
11779 case 0x2: /* SDOT / UDOT */
11780 gen_gvec_op3_ool(s, is_q, rd, rn, rm, 0,
11781 u ? gen_helper_gvec_udot_b : gen_helper_gvec_sdot_b);
11782 return;
11783
d17b7cdc
RH
11784 case 0x8: /* FCMLA, #0 */
11785 case 0x9: /* FCMLA, #90 */
11786 case 0xa: /* FCMLA, #180 */
11787 case 0xb: /* FCMLA, #270 */
11788 rot = extract32(opcode, 0, 2);
11789 switch (size) {
11790 case 1:
11791 gen_gvec_op3_fpst(s, is_q, rd, rn, rm, true, rot,
11792 gen_helper_gvec_fcmlah);
11793 break;
11794 case 2:
11795 gen_gvec_op3_fpst(s, is_q, rd, rn, rm, false, rot,
11796 gen_helper_gvec_fcmlas);
11797 break;
11798 case 3:
11799 gen_gvec_op3_fpst(s, is_q, rd, rn, rm, false, rot,
11800 gen_helper_gvec_fcmlad);
11801 break;
11802 default:
11803 g_assert_not_reached();
11804 }
11805 return;
11806
1695cd61
RH
11807 case 0xc: /* FCADD, #90 */
11808 case 0xe: /* FCADD, #270 */
11809 rot = extract32(opcode, 1, 1);
11810 switch (size) {
11811 case 1:
11812 gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
11813 gen_helper_gvec_fcaddh);
11814 break;
11815 case 2:
11816 gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
11817 gen_helper_gvec_fcadds);
11818 break;
11819 case 3:
11820 gen_gvec_op3_fpst(s, is_q, rd, rn, rm, size == 1, rot,
11821 gen_helper_gvec_fcaddd);
11822 break;
11823 default:
11824 g_assert_not_reached();
11825 }
11826 return;
11827
e7186d82
RH
11828 default:
11829 g_assert_not_reached();
11830 }
11831}
11832
931c8cc2
PM
11833static void handle_2misc_widening(DisasContext *s, int opcode, bool is_q,
11834 int size, int rn, int rd)
11835{
11836 /* Handle 2-reg-misc ops which are widening (so each size element
11837 * in the source becomes a 2*size element in the destination.
11838 * The only instruction like this is FCVTL.
11839 */
11840 int pass;
11841
11842 if (size == 3) {
11843 /* 32 -> 64 bit fp conversion */
11844 TCGv_i64 tcg_res[2];
11845 int srcelt = is_q ? 2 : 0;
11846
11847 for (pass = 0; pass < 2; pass++) {
11848 TCGv_i32 tcg_op = tcg_temp_new_i32();
11849 tcg_res[pass] = tcg_temp_new_i64();
11850
11851 read_vec_element_i32(s, tcg_op, rn, srcelt + pass, MO_32);
11852 gen_helper_vfp_fcvtds(tcg_res[pass], tcg_op, cpu_env);
11853 tcg_temp_free_i32(tcg_op);
11854 }
11855 for (pass = 0; pass < 2; pass++) {
11856 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
11857 tcg_temp_free_i64(tcg_res[pass]);
11858 }
11859 } else {
11860 /* 16 -> 32 bit fp conversion */
11861 int srcelt = is_q ? 4 : 0;
11862 TCGv_i32 tcg_res[4];
486624fc
AB
11863 TCGv_ptr fpst = get_fpstatus_ptr(false);
11864 TCGv_i32 ahp = get_ahp_flag();
931c8cc2
PM
11865
11866 for (pass = 0; pass < 4; pass++) {
11867 tcg_res[pass] = tcg_temp_new_i32();
11868
11869 read_vec_element_i32(s, tcg_res[pass], rn, srcelt + pass, MO_16);
11870 gen_helper_vfp_fcvt_f16_to_f32(tcg_res[pass], tcg_res[pass],
486624fc 11871 fpst, ahp);
931c8cc2
PM
11872 }
11873 for (pass = 0; pass < 4; pass++) {
11874 write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_32);
11875 tcg_temp_free_i32(tcg_res[pass]);
11876 }
486624fc
AB
11877
11878 tcg_temp_free_ptr(fpst);
11879 tcg_temp_free_i32(ahp);
931c8cc2
PM
11880 }
11881}
11882
39d82118
AB
11883static void handle_rev(DisasContext *s, int opcode, bool u,
11884 bool is_q, int size, int rn, int rd)
11885{
11886 int op = (opcode << 1) | u;
11887 int opsz = op + size;
11888 int grp_size = 3 - opsz;
11889 int dsize = is_q ? 128 : 64;
11890 int i;
11891
11892 if (opsz >= 3) {
11893 unallocated_encoding(s);
11894 return;
11895 }
11896
8c6afa6a
PM
11897 if (!fp_access_check(s)) {
11898 return;
11899 }
11900
39d82118
AB
11901 if (size == 0) {
11902 /* Special case bytes, use bswap op on each group of elements */
11903 int groups = dsize / (8 << grp_size);
11904
11905 for (i = 0; i < groups; i++) {
11906 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
11907
11908 read_vec_element(s, tcg_tmp, rn, i, grp_size);
11909 switch (grp_size) {
11910 case MO_16:
11911 tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp);
11912 break;
11913 case MO_32:
11914 tcg_gen_bswap32_i64(tcg_tmp, tcg_tmp);
11915 break;
11916 case MO_64:
11917 tcg_gen_bswap64_i64(tcg_tmp, tcg_tmp);
11918 break;
11919 default:
11920 g_assert_not_reached();
11921 }
11922 write_vec_element(s, tcg_tmp, rd, i, grp_size);
11923 tcg_temp_free_i64(tcg_tmp);
11924 }
4ff55bcb 11925 clear_vec_high(s, is_q, rd);
39d82118
AB
11926 } else {
11927 int revmask = (1 << grp_size) - 1;
11928 int esize = 8 << size;
11929 int elements = dsize / esize;
11930 TCGv_i64 tcg_rn = tcg_temp_new_i64();
11931 TCGv_i64 tcg_rd = tcg_const_i64(0);
11932 TCGv_i64 tcg_rd_hi = tcg_const_i64(0);
11933
11934 for (i = 0; i < elements; i++) {
11935 int e_rev = (i & 0xf) ^ revmask;
11936 int off = e_rev * esize;
11937 read_vec_element(s, tcg_rn, rn, i, size);
11938 if (off >= 64) {
11939 tcg_gen_deposit_i64(tcg_rd_hi, tcg_rd_hi,
11940 tcg_rn, off - 64, esize);
11941 } else {
11942 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, off, esize);
11943 }
11944 }
11945 write_vec_element(s, tcg_rd, rd, 0, MO_64);
11946 write_vec_element(s, tcg_rd_hi, rd, 1, MO_64);
11947
11948 tcg_temp_free_i64(tcg_rd_hi);
11949 tcg_temp_free_i64(tcg_rd);
11950 tcg_temp_free_i64(tcg_rn);
11951 }
11952}
11953
6781fa11
PM
11954static void handle_2misc_pairwise(DisasContext *s, int opcode, bool u,
11955 bool is_q, int size, int rn, int rd)
11956{
11957 /* Implement the pairwise operations from 2-misc:
11958 * SADDLP, UADDLP, SADALP, UADALP.
11959 * These all add pairs of elements in the input to produce a
11960 * double-width result element in the output (possibly accumulating).
11961 */
11962 bool accum = (opcode == 0x6);
11963 int maxpass = is_q ? 2 : 1;
11964 int pass;
11965 TCGv_i64 tcg_res[2];
11966
11967 if (size == 2) {
11968 /* 32 + 32 -> 64 op */
14776ab5 11969 MemOp memop = size + (u ? 0 : MO_SIGN);
6781fa11
PM
11970
11971 for (pass = 0; pass < maxpass; pass++) {
11972 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
11973 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
11974
11975 tcg_res[pass] = tcg_temp_new_i64();
11976
11977 read_vec_element(s, tcg_op1, rn, pass * 2, memop);
11978 read_vec_element(s, tcg_op2, rn, pass * 2 + 1, memop);
11979 tcg_gen_add_i64(tcg_res[pass], tcg_op1, tcg_op2);
11980 if (accum) {
11981 read_vec_element(s, tcg_op1, rd, pass, MO_64);
11982 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
11983 }
11984
11985 tcg_temp_free_i64(tcg_op1);
11986 tcg_temp_free_i64(tcg_op2);
11987 }
11988 } else {
11989 for (pass = 0; pass < maxpass; pass++) {
11990 TCGv_i64 tcg_op = tcg_temp_new_i64();
11991 NeonGenOneOpFn *genfn;
11992 static NeonGenOneOpFn * const fns[2][2] = {
11993 { gen_helper_neon_addlp_s8, gen_helper_neon_addlp_u8 },
11994 { gen_helper_neon_addlp_s16, gen_helper_neon_addlp_u16 },
11995 };
11996
11997 genfn = fns[size][u];
11998
11999 tcg_res[pass] = tcg_temp_new_i64();
12000
12001 read_vec_element(s, tcg_op, rn, pass, MO_64);
12002 genfn(tcg_res[pass], tcg_op);
12003
12004 if (accum) {
12005 read_vec_element(s, tcg_op, rd, pass, MO_64);
12006 if (size == 0) {
12007 gen_helper_neon_addl_u16(tcg_res[pass],
12008 tcg_res[pass], tcg_op);
12009 } else {
12010 gen_helper_neon_addl_u32(tcg_res[pass],
12011 tcg_res[pass], tcg_op);
12012 }
12013 }
12014 tcg_temp_free_i64(tcg_op);
12015 }
12016 }
12017 if (!is_q) {
12018 tcg_res[1] = tcg_const_i64(0);
12019 }
12020 for (pass = 0; pass < 2; pass++) {
12021 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
12022 tcg_temp_free_i64(tcg_res[pass]);
12023 }
12024}
12025
73a81d10
PM
12026static void handle_shll(DisasContext *s, bool is_q, int size, int rn, int rd)
12027{
12028 /* Implement SHLL and SHLL2 */
12029 int pass;
12030 int part = is_q ? 2 : 0;
12031 TCGv_i64 tcg_res[2];
12032
12033 for (pass = 0; pass < 2; pass++) {
12034 static NeonGenWidenFn * const widenfns[3] = {
12035 gen_helper_neon_widen_u8,
12036 gen_helper_neon_widen_u16,
12037 tcg_gen_extu_i32_i64,
12038 };
12039 NeonGenWidenFn *widenfn = widenfns[size];
12040 TCGv_i32 tcg_op = tcg_temp_new_i32();
12041
12042 read_vec_element_i32(s, tcg_op, rn, part + pass, MO_32);
12043 tcg_res[pass] = tcg_temp_new_i64();
12044 widenfn(tcg_res[pass], tcg_op);
12045 tcg_gen_shli_i64(tcg_res[pass], tcg_res[pass], 8 << size);
12046
12047 tcg_temp_free_i32(tcg_op);
12048 }
12049
12050 for (pass = 0; pass < 2; pass++) {
12051 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
12052 tcg_temp_free_i64(tcg_res[pass]);
12053 }
12054}
12055
4ce31af4 12056/* AdvSIMD two reg misc
384b26fb
AB
12057 * 31 30 29 28 24 23 22 21 17 16 12 11 10 9 5 4 0
12058 * +---+---+---+-----------+------+-----------+--------+-----+------+------+
12059 * | 0 | Q | U | 0 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 | Rn | Rd |
12060 * +---+---+---+-----------+------+-----------+--------+-----+------+------+
12061 */
12062static void disas_simd_two_reg_misc(DisasContext *s, uint32_t insn)
12063{
45aecc6d
PM
12064 int size = extract32(insn, 22, 2);
12065 int opcode = extract32(insn, 12, 5);
12066 bool u = extract32(insn, 29, 1);
12067 bool is_q = extract32(insn, 30, 1);
94b6c911
PM
12068 int rn = extract32(insn, 5, 5);
12069 int rd = extract32(insn, 0, 5);
04c7c6c2
PM
12070 bool need_fpstatus = false;
12071 bool need_rmode = false;
12072 int rmode = -1;
12073 TCGv_i32 tcg_rmode;
12074 TCGv_ptr tcg_fpstatus;
45aecc6d
PM
12075
12076 switch (opcode) {
12077 case 0x0: /* REV64, REV32 */
12078 case 0x1: /* REV16 */
39d82118 12079 handle_rev(s, opcode, u, is_q, size, rn, rd);
45aecc6d 12080 return;
86cbc418
PM
12081 case 0x5: /* CNT, NOT, RBIT */
12082 if (u && size == 0) {
377ef731 12083 /* NOT */
86cbc418
PM
12084 break;
12085 } else if (u && size == 1) {
12086 /* RBIT */
12087 break;
12088 } else if (!u && size == 0) {
12089 /* CNT */
12090 break;
45aecc6d 12091 }
86cbc418 12092 unallocated_encoding(s);
45aecc6d 12093 return;
d980fd59
PM
12094 case 0x12: /* XTN, XTN2, SQXTUN, SQXTUN2 */
12095 case 0x14: /* SQXTN, SQXTN2, UQXTN, UQXTN2 */
12096 if (size == 3) {
12097 unallocated_encoding(s);
12098 return;
12099 }
8c6afa6a
PM
12100 if (!fp_access_check(s)) {
12101 return;
12102 }
12103
5201c136 12104 handle_2misc_narrow(s, false, opcode, u, is_q, size, rn, rd);
d980fd59 12105 return;
45aecc6d 12106 case 0x4: /* CLS, CLZ */
b05c3068
AB
12107 if (size == 3) {
12108 unallocated_encoding(s);
12109 return;
12110 }
12111 break;
12112 case 0x2: /* SADDLP, UADDLP */
45aecc6d 12113 case 0x6: /* SADALP, UADALP */
45aecc6d
PM
12114 if (size == 3) {
12115 unallocated_encoding(s);
12116 return;
12117 }
8c6afa6a
PM
12118 if (!fp_access_check(s)) {
12119 return;
12120 }
6781fa11 12121 handle_2misc_pairwise(s, opcode, u, is_q, size, rn, rd);
45aecc6d
PM
12122 return;
12123 case 0x13: /* SHLL, SHLL2 */
12124 if (u == 0 || size == 3) {
12125 unallocated_encoding(s);
12126 return;
12127 }
8c6afa6a
PM
12128 if (!fp_access_check(s)) {
12129 return;
12130 }
73a81d10 12131 handle_shll(s, is_q, size, rn, rd);
45aecc6d
PM
12132 return;
12133 case 0xa: /* CMLT */
12134 if (u == 1) {
12135 unallocated_encoding(s);
12136 return;
12137 }
12138 /* fall through */
45aecc6d
PM
12139 case 0x8: /* CMGT, CMGE */
12140 case 0x9: /* CMEQ, CMLE */
12141 case 0xb: /* ABS, NEG */
94b6c911
PM
12142 if (size == 3 && !is_q) {
12143 unallocated_encoding(s);
12144 return;
12145 }
12146 break;
12147 case 0x3: /* SUQADD, USQADD */
09e03735
AB
12148 if (size == 3 && !is_q) {
12149 unallocated_encoding(s);
12150 return;
12151 }
8c6afa6a
PM
12152 if (!fp_access_check(s)) {
12153 return;
12154 }
09e03735
AB
12155 handle_2misc_satacc(s, false, u, is_q, size, rn, rd);
12156 return;
94b6c911 12157 case 0x7: /* SQABS, SQNEG */
45aecc6d
PM
12158 if (size == 3 && !is_q) {
12159 unallocated_encoding(s);
12160 return;
12161 }
0a79bc87 12162 break;
45aecc6d 12163 case 0xc ... 0xf:
6bea2563 12164 case 0x16 ... 0x1f:
45aecc6d
PM
12165 {
12166 /* Floating point: U, size[1] and opcode indicate operation;
12167 * size[0] indicates single or double precision.
12168 */
10113b69 12169 int is_double = extract32(size, 0, 1);
45aecc6d 12170 opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
10113b69 12171 size = is_double ? 3 : 2;
45aecc6d 12172 switch (opcode) {
f93d0138
PM
12173 case 0x2f: /* FABS */
12174 case 0x6f: /* FNEG */
12175 if (size == 3 && !is_q) {
12176 unallocated_encoding(s);
12177 return;
12178 }
12179 break;
10113b69
AB
12180 case 0x1d: /* SCVTF */
12181 case 0x5d: /* UCVTF */
12182 {
12183 bool is_signed = (opcode == 0x1d) ? true : false;
12184 int elements = is_double ? 2 : is_q ? 4 : 2;
12185 if (is_double && !is_q) {
12186 unallocated_encoding(s);
12187 return;
12188 }
8c6afa6a
PM
12189 if (!fp_access_check(s)) {
12190 return;
12191 }
10113b69
AB
12192 handle_simd_intfp_conv(s, rd, rn, elements, is_signed, 0, size);
12193 return;
12194 }
8908f4d1
AB
12195 case 0x2c: /* FCMGT (zero) */
12196 case 0x2d: /* FCMEQ (zero) */
12197 case 0x2e: /* FCMLT (zero) */
12198 case 0x6c: /* FCMGE (zero) */
12199 case 0x6d: /* FCMLE (zero) */
12200 if (size == 3 && !is_q) {
12201 unallocated_encoding(s);
12202 return;
12203 }
12204 handle_2misc_fcmp_zero(s, opcode, false, u, is_q, size, rn, rd);
12205 return;
f612537e
AB
12206 case 0x7f: /* FSQRT */
12207 if (size == 3 && !is_q) {
12208 unallocated_encoding(s);
12209 return;
12210 }
12211 break;
04c7c6c2
PM
12212 case 0x1a: /* FCVTNS */
12213 case 0x1b: /* FCVTMS */
12214 case 0x3a: /* FCVTPS */
12215 case 0x3b: /* FCVTZS */
12216 case 0x5a: /* FCVTNU */
12217 case 0x5b: /* FCVTMU */
12218 case 0x7a: /* FCVTPU */
12219 case 0x7b: /* FCVTZU */
12220 need_fpstatus = true;
12221 need_rmode = true;
12222 rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
12223 if (size == 3 && !is_q) {
12224 unallocated_encoding(s);
12225 return;
12226 }
12227 break;
12228 case 0x5c: /* FCVTAU */
12229 case 0x1c: /* FCVTAS */
12230 need_fpstatus = true;
12231 need_rmode = true;
12232 rmode = FPROUNDING_TIEAWAY;
12233 if (size == 3 && !is_q) {
12234 unallocated_encoding(s);
12235 return;
12236 }
12237 break;
b6d4443a
AB
12238 case 0x3c: /* URECPE */
12239 if (size == 3) {
12240 unallocated_encoding(s);
12241 return;
12242 }
12243 /* fall through */
12244 case 0x3d: /* FRECPE */
c2fb418e
AB
12245 case 0x7d: /* FRSQRTE */
12246 if (size == 3 && !is_q) {
12247 unallocated_encoding(s);
12248 return;
12249 }
8c6afa6a
PM
12250 if (!fp_access_check(s)) {
12251 return;
12252 }
b6d4443a
AB
12253 handle_2misc_reciprocal(s, opcode, false, u, is_q, size, rn, rd);
12254 return;
5553955e
PM
12255 case 0x56: /* FCVTXN, FCVTXN2 */
12256 if (size == 2) {
12257 unallocated_encoding(s);
12258 return;
12259 }
12260 /* fall through */
45aecc6d 12261 case 0x16: /* FCVTN, FCVTN2 */
261a5b4d
PM
12262 /* handle_2misc_narrow does a 2*size -> size operation, but these
12263 * instructions encode the source size rather than dest size.
12264 */
8c6afa6a
PM
12265 if (!fp_access_check(s)) {
12266 return;
12267 }
5201c136 12268 handle_2misc_narrow(s, false, opcode, 0, is_q, size - 1, rn, rd);
261a5b4d 12269 return;
45aecc6d 12270 case 0x17: /* FCVTL, FCVTL2 */
8c6afa6a
PM
12271 if (!fp_access_check(s)) {
12272 return;
12273 }
931c8cc2
PM
12274 handle_2misc_widening(s, opcode, is_q, size, rn, rd);
12275 return;
45aecc6d
PM
12276 case 0x18: /* FRINTN */
12277 case 0x19: /* FRINTM */
45aecc6d
PM
12278 case 0x38: /* FRINTP */
12279 case 0x39: /* FRINTZ */
03df01ed
PM
12280 need_rmode = true;
12281 rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
12282 /* fall through */
12283 case 0x59: /* FRINTX */
12284 case 0x79: /* FRINTI */
12285 need_fpstatus = true;
12286 if (size == 3 && !is_q) {
12287 unallocated_encoding(s);
12288 return;
12289 }
12290 break;
12291 case 0x58: /* FRINTA */
12292 need_rmode = true;
12293 rmode = FPROUNDING_TIEAWAY;
12294 need_fpstatus = true;
12295 if (size == 3 && !is_q) {
12296 unallocated_encoding(s);
12297 return;
12298 }
12299 break;
45aecc6d 12300 case 0x7c: /* URSQRTE */
c2fb418e
AB
12301 if (size == 3) {
12302 unallocated_encoding(s);
12303 return;
12304 }
12305 need_fpstatus = true;
12306 break;
6bea2563
RH
12307 case 0x1e: /* FRINT32Z */
12308 case 0x1f: /* FRINT64Z */
12309 need_rmode = true;
12310 rmode = FPROUNDING_ZERO;
12311 /* fall through */
12312 case 0x5e: /* FRINT32X */
12313 case 0x5f: /* FRINT64X */
12314 need_fpstatus = true;
12315 if ((size == 3 && !is_q) || !dc_isar_feature(aa64_frint, s)) {
12316 unallocated_encoding(s);
12317 return;
12318 }
12319 break;
45aecc6d
PM
12320 default:
12321 unallocated_encoding(s);
12322 return;
12323 }
12324 break;
12325 }
12326 default:
12327 unallocated_encoding(s);
12328 return;
12329 }
94b6c911 12330
8c6afa6a
PM
12331 if (!fp_access_check(s)) {
12332 return;
12333 }
12334
9b049916 12335 if (need_fpstatus || need_rmode) {
d81ce0ef 12336 tcg_fpstatus = get_fpstatus_ptr(false);
04c7c6c2 12337 } else {
f764718d 12338 tcg_fpstatus = NULL;
04c7c6c2
PM
12339 }
12340 if (need_rmode) {
12341 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rmode));
9b049916 12342 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
04c7c6c2 12343 } else {
f764718d 12344 tcg_rmode = NULL;
04c7c6c2
PM
12345 }
12346
377ef731
RH
12347 switch (opcode) {
12348 case 0x5:
12349 if (u && size == 0) { /* NOT */
12350 gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_not, 0);
12351 return;
12352 }
12353 break;
12354 case 0xb:
4e027a71 12355 if (u) { /* ABS, NEG */
377ef731 12356 gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_neg, size);
4e027a71
RH
12357 } else {
12358 gen_gvec_fn2(s, is_q, rd, rn, tcg_gen_gvec_abs, size);
377ef731 12359 }
4e027a71 12360 return;
377ef731
RH
12361 }
12362
94b6c911
PM
12363 if (size == 3) {
12364 /* All 64-bit element operations can be shared with scalar 2misc */
12365 int pass;
12366
a8766e31
RH
12367 /* Coverity claims (size == 3 && !is_q) has been eliminated
12368 * from all paths leading to here.
12369 */
12370 tcg_debug_assert(is_q);
12371 for (pass = 0; pass < 2; pass++) {
94b6c911
PM
12372 TCGv_i64 tcg_op = tcg_temp_new_i64();
12373 TCGv_i64 tcg_res = tcg_temp_new_i64();
12374
12375 read_vec_element(s, tcg_op, rn, pass, MO_64);
12376
04c7c6c2
PM
12377 handle_2misc_64(s, opcode, u, tcg_res, tcg_op,
12378 tcg_rmode, tcg_fpstatus);
94b6c911
PM
12379
12380 write_vec_element(s, tcg_res, rd, pass, MO_64);
12381
12382 tcg_temp_free_i64(tcg_res);
12383 tcg_temp_free_i64(tcg_op);
12384 }
12385 } else {
12386 int pass;
12387
12388 for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
12389 TCGv_i32 tcg_op = tcg_temp_new_i32();
12390 TCGv_i32 tcg_res = tcg_temp_new_i32();
12391 TCGCond cond;
12392
12393 read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
12394
12395 if (size == 2) {
12396 /* Special cases for 32 bit elements */
12397 switch (opcode) {
12398 case 0xa: /* CMLT */
12399 /* 32 bit integer comparison against zero, result is
12400 * test ? (2^32 - 1) : 0. We implement via setcond(test)
12401 * and inverting.
12402 */
12403 cond = TCG_COND_LT;
12404 do_cmop:
12405 tcg_gen_setcondi_i32(cond, tcg_res, tcg_op, 0);
12406 tcg_gen_neg_i32(tcg_res, tcg_res);
12407 break;
12408 case 0x8: /* CMGT, CMGE */
12409 cond = u ? TCG_COND_GE : TCG_COND_GT;
12410 goto do_cmop;
12411 case 0x9: /* CMEQ, CMLE */
12412 cond = u ? TCG_COND_LE : TCG_COND_EQ;
12413 goto do_cmop;
b05c3068
AB
12414 case 0x4: /* CLS */
12415 if (u) {
7539a012 12416 tcg_gen_clzi_i32(tcg_res, tcg_op, 32);
b05c3068 12417 } else {
bc21dbcc 12418 tcg_gen_clrsb_i32(tcg_res, tcg_op);
b05c3068
AB
12419 }
12420 break;
0a79bc87
AB
12421 case 0x7: /* SQABS, SQNEG */
12422 if (u) {
12423 gen_helper_neon_qneg_s32(tcg_res, cpu_env, tcg_op);
12424 } else {
12425 gen_helper_neon_qabs_s32(tcg_res, cpu_env, tcg_op);
12426 }
12427 break;
f93d0138
PM
12428 case 0x2f: /* FABS */
12429 gen_helper_vfp_abss(tcg_res, tcg_op);
12430 break;
12431 case 0x6f: /* FNEG */
12432 gen_helper_vfp_negs(tcg_res, tcg_op);
12433 break;
f612537e
AB
12434 case 0x7f: /* FSQRT */
12435 gen_helper_vfp_sqrts(tcg_res, tcg_op, cpu_env);
12436 break;
04c7c6c2
PM
12437 case 0x1a: /* FCVTNS */
12438 case 0x1b: /* FCVTMS */
12439 case 0x1c: /* FCVTAS */
12440 case 0x3a: /* FCVTPS */
12441 case 0x3b: /* FCVTZS */
12442 {
12443 TCGv_i32 tcg_shift = tcg_const_i32(0);
12444 gen_helper_vfp_tosls(tcg_res, tcg_op,
12445 tcg_shift, tcg_fpstatus);
12446 tcg_temp_free_i32(tcg_shift);
12447 break;
12448 }
12449 case 0x5a: /* FCVTNU */
12450 case 0x5b: /* FCVTMU */
12451 case 0x5c: /* FCVTAU */
12452 case 0x7a: /* FCVTPU */
12453 case 0x7b: /* FCVTZU */
12454 {
12455 TCGv_i32 tcg_shift = tcg_const_i32(0);
12456 gen_helper_vfp_touls(tcg_res, tcg_op,
12457 tcg_shift, tcg_fpstatus);
12458 tcg_temp_free_i32(tcg_shift);
12459 break;
12460 }
03df01ed
PM
12461 case 0x18: /* FRINTN */
12462 case 0x19: /* FRINTM */
12463 case 0x38: /* FRINTP */
12464 case 0x39: /* FRINTZ */
12465 case 0x58: /* FRINTA */
12466 case 0x79: /* FRINTI */
12467 gen_helper_rints(tcg_res, tcg_op, tcg_fpstatus);
12468 break;
12469 case 0x59: /* FRINTX */
12470 gen_helper_rints_exact(tcg_res, tcg_op, tcg_fpstatus);
12471 break;
c2fb418e
AB
12472 case 0x7c: /* URSQRTE */
12473 gen_helper_rsqrte_u32(tcg_res, tcg_op, tcg_fpstatus);
12474 break;
6bea2563
RH
12475 case 0x1e: /* FRINT32Z */
12476 case 0x5e: /* FRINT32X */
12477 gen_helper_frint32_s(tcg_res, tcg_op, tcg_fpstatus);
12478 break;
12479 case 0x1f: /* FRINT64Z */
12480 case 0x5f: /* FRINT64X */
12481 gen_helper_frint64_s(tcg_res, tcg_op, tcg_fpstatus);
12482 break;
94b6c911
PM
12483 default:
12484 g_assert_not_reached();
12485 }
12486 } else {
12487 /* Use helpers for 8 and 16 bit elements */
12488 switch (opcode) {
86cbc418
PM
12489 case 0x5: /* CNT, RBIT */
12490 /* For these two insns size is part of the opcode specifier
12491 * (handled earlier); they always operate on byte elements.
12492 */
12493 if (u) {
12494 gen_helper_neon_rbit_u8(tcg_res, tcg_op);
12495 } else {
12496 gen_helper_neon_cnt_u8(tcg_res, tcg_op);
12497 }
12498 break;
0a79bc87
AB
12499 case 0x7: /* SQABS, SQNEG */
12500 {
12501 NeonGenOneOpEnvFn *genfn;
12502 static NeonGenOneOpEnvFn * const fns[2][2] = {
12503 { gen_helper_neon_qabs_s8, gen_helper_neon_qneg_s8 },
12504 { gen_helper_neon_qabs_s16, gen_helper_neon_qneg_s16 },
12505 };
12506 genfn = fns[size][u];
12507 genfn(tcg_res, cpu_env, tcg_op);
12508 break;
12509 }
94b6c911
PM
12510 case 0x8: /* CMGT, CMGE */
12511 case 0x9: /* CMEQ, CMLE */
12512 case 0xa: /* CMLT */
12513 {
12514 static NeonGenTwoOpFn * const fns[3][2] = {
12515 { gen_helper_neon_cgt_s8, gen_helper_neon_cgt_s16 },
12516 { gen_helper_neon_cge_s8, gen_helper_neon_cge_s16 },
12517 { gen_helper_neon_ceq_u8, gen_helper_neon_ceq_u16 },
12518 };
12519 NeonGenTwoOpFn *genfn;
12520 int comp;
12521 bool reverse;
12522 TCGv_i32 tcg_zero = tcg_const_i32(0);
12523
12524 /* comp = index into [CMGT, CMGE, CMEQ, CMLE, CMLT] */
12525 comp = (opcode - 0x8) * 2 + u;
12526 /* ...but LE, LT are implemented as reverse GE, GT */
12527 reverse = (comp > 2);
12528 if (reverse) {
12529 comp = 4 - comp;
12530 }
12531 genfn = fns[comp][size];
12532 if (reverse) {
12533 genfn(tcg_res, tcg_zero, tcg_op);
12534 } else {
12535 genfn(tcg_res, tcg_op, tcg_zero);
12536 }
12537 tcg_temp_free_i32(tcg_zero);
12538 break;
12539 }
b05c3068
AB
12540 case 0x4: /* CLS, CLZ */
12541 if (u) {
12542 if (size == 0) {
12543 gen_helper_neon_clz_u8(tcg_res, tcg_op);
12544 } else {
12545 gen_helper_neon_clz_u16(tcg_res, tcg_op);
12546 }
12547 } else {
12548 if (size == 0) {
12549 gen_helper_neon_cls_s8(tcg_res, tcg_op);
12550 } else {
12551 gen_helper_neon_cls_s16(tcg_res, tcg_op);
12552 }
12553 }
12554 break;
94b6c911
PM
12555 default:
12556 g_assert_not_reached();
12557 }
12558 }
12559
12560 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
12561
12562 tcg_temp_free_i32(tcg_res);
12563 tcg_temp_free_i32(tcg_op);
12564 }
12565 }
4ff55bcb 12566 clear_vec_high(s, is_q, rd);
04c7c6c2
PM
12567
12568 if (need_rmode) {
9b049916 12569 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
04c7c6c2
PM
12570 tcg_temp_free_i32(tcg_rmode);
12571 }
12572 if (need_fpstatus) {
12573 tcg_temp_free_ptr(tcg_fpstatus);
12574 }
384b26fb
AB
12575}
12576
5d432be6
AB
12577/* AdvSIMD [scalar] two register miscellaneous (FP16)
12578 *
12579 * 31 30 29 28 27 24 23 22 21 17 16 12 11 10 9 5 4 0
12580 * +---+---+---+---+---------+---+-------------+--------+-----+------+------+
12581 * | 0 | Q | U | S | 1 1 1 0 | a | 1 1 1 1 0 0 | opcode | 1 0 | Rn | Rd |
12582 * +---+---+---+---+---------+---+-------------+--------+-----+------+------+
12583 * mask: 1000 1111 0111 1110 0000 1100 0000 0000 0x8f7e 0c00
12584 * val: 0000 1110 0111 1000 0000 1000 0000 0000 0x0e78 0800
12585 *
12586 * This actually covers two groups where scalar access is governed by
12587 * bit 28. A bunch of the instructions (float to integral) only exist
12588 * in the vector form and are un-allocated for the scalar decode. Also
12589 * in the scalar decode Q is always 1.
12590 */
12591static void disas_simd_two_reg_misc_fp16(DisasContext *s, uint32_t insn)
12592{
6109aea2
AB
12593 int fpop, opcode, a, u;
12594 int rn, rd;
12595 bool is_q;
12596 bool is_scalar;
12597 bool only_in_vector = false;
12598
12599 int pass;
12600 TCGv_i32 tcg_rmode = NULL;
12601 TCGv_ptr tcg_fpstatus = NULL;
12602 bool need_rmode = false;
15f8a233 12603 bool need_fpst = true;
6109aea2 12604 int rmode;
5d432be6 12605
5763190f 12606 if (!dc_isar_feature(aa64_fp16, s)) {
5d432be6
AB
12607 unallocated_encoding(s);
12608 return;
12609 }
12610
6109aea2
AB
12611 rd = extract32(insn, 0, 5);
12612 rn = extract32(insn, 5, 5);
5d432be6 12613
5d432be6 12614 a = extract32(insn, 23, 1);
6109aea2
AB
12615 u = extract32(insn, 29, 1);
12616 is_scalar = extract32(insn, 28, 1);
12617 is_q = extract32(insn, 30, 1);
12618
12619 opcode = extract32(insn, 12, 5);
5d432be6 12620 fpop = deposit32(opcode, 5, 1, a);
6109aea2 12621 fpop = deposit32(fpop, 6, 1, u);
5d432be6 12622
7d4dd1a7
AB
12623 rd = extract32(insn, 0, 5);
12624 rn = extract32(insn, 5, 5);
12625
5d432be6 12626 switch (fpop) {
93193190
AB
12627 case 0x1d: /* SCVTF */
12628 case 0x5d: /* UCVTF */
12629 {
12630 int elements;
12631
12632 if (is_scalar) {
12633 elements = 1;
12634 } else {
12635 elements = (is_q ? 8 : 4);
12636 }
12637
12638 if (!fp_access_check(s)) {
12639 return;
12640 }
12641 handle_simd_intfp_conv(s, rd, rn, elements, !u, 0, MO_16);
12642 return;
12643 }
7d4dd1a7
AB
12644 break;
12645 case 0x2c: /* FCMGT (zero) */
12646 case 0x2d: /* FCMEQ (zero) */
12647 case 0x2e: /* FCMLT (zero) */
12648 case 0x6c: /* FCMGE (zero) */
12649 case 0x6d: /* FCMLE (zero) */
12650 handle_2misc_fcmp_zero(s, fpop, is_scalar, 0, is_q, MO_16, rn, rd);
12651 return;
fbd06e1e 12652 case 0x3d: /* FRECPE */
98695028 12653 case 0x3f: /* FRECPX */
fbd06e1e 12654 break;
6109aea2
AB
12655 case 0x18: /* FRINTN */
12656 need_rmode = true;
12657 only_in_vector = true;
12658 rmode = FPROUNDING_TIEEVEN;
12659 break;
12660 case 0x19: /* FRINTM */
12661 need_rmode = true;
12662 only_in_vector = true;
12663 rmode = FPROUNDING_NEGINF;
12664 break;
12665 case 0x38: /* FRINTP */
12666 need_rmode = true;
12667 only_in_vector = true;
12668 rmode = FPROUNDING_POSINF;
12669 break;
12670 case 0x39: /* FRINTZ */
12671 need_rmode = true;
12672 only_in_vector = true;
12673 rmode = FPROUNDING_ZERO;
12674 break;
12675 case 0x58: /* FRINTA */
12676 need_rmode = true;
12677 only_in_vector = true;
12678 rmode = FPROUNDING_TIEAWAY;
12679 break;
12680 case 0x59: /* FRINTX */
12681 case 0x79: /* FRINTI */
12682 only_in_vector = true;
12683 /* current rounding mode */
12684 break;
2df58130
AB
12685 case 0x1a: /* FCVTNS */
12686 need_rmode = true;
12687 rmode = FPROUNDING_TIEEVEN;
12688 break;
12689 case 0x1b: /* FCVTMS */
12690 need_rmode = true;
12691 rmode = FPROUNDING_NEGINF;
12692 break;
12693 case 0x1c: /* FCVTAS */
12694 need_rmode = true;
12695 rmode = FPROUNDING_TIEAWAY;
12696 break;
12697 case 0x3a: /* FCVTPS */
12698 need_rmode = true;
12699 rmode = FPROUNDING_POSINF;
12700 break;
12701 case 0x3b: /* FCVTZS */
12702 need_rmode = true;
12703 rmode = FPROUNDING_ZERO;
12704 break;
12705 case 0x5a: /* FCVTNU */
12706 need_rmode = true;
12707 rmode = FPROUNDING_TIEEVEN;
12708 break;
12709 case 0x5b: /* FCVTMU */
12710 need_rmode = true;
12711 rmode = FPROUNDING_NEGINF;
12712 break;
12713 case 0x5c: /* FCVTAU */
12714 need_rmode = true;
12715 rmode = FPROUNDING_TIEAWAY;
12716 break;
12717 case 0x7a: /* FCVTPU */
12718 need_rmode = true;
12719 rmode = FPROUNDING_POSINF;
12720 break;
12721 case 0x7b: /* FCVTZU */
12722 need_rmode = true;
12723 rmode = FPROUNDING_ZERO;
12724 break;
15f8a233
AB
12725 case 0x2f: /* FABS */
12726 case 0x6f: /* FNEG */
12727 need_fpst = false;
12728 break;
c625ff95 12729 case 0x7d: /* FRSQRTE */
b96a54c7
AB
12730 case 0x7f: /* FSQRT (vector) */
12731 break;
5d432be6
AB
12732 default:
12733 fprintf(stderr, "%s: insn %#04x fpop %#2x\n", __func__, insn, fpop);
12734 g_assert_not_reached();
12735 }
12736
6109aea2
AB
12737
12738 /* Check additional constraints for the scalar encoding */
12739 if (is_scalar) {
12740 if (!is_q) {
12741 unallocated_encoding(s);
12742 return;
12743 }
12744 /* FRINTxx is only in the vector form */
12745 if (only_in_vector) {
12746 unallocated_encoding(s);
12747 return;
12748 }
12749 }
12750
12751 if (!fp_access_check(s)) {
12752 return;
12753 }
12754
15f8a233 12755 if (need_rmode || need_fpst) {
6109aea2
AB
12756 tcg_fpstatus = get_fpstatus_ptr(true);
12757 }
12758
12759 if (need_rmode) {
12760 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rmode));
12761 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
12762 }
12763
12764 if (is_scalar) {
3d99d931 12765 TCGv_i32 tcg_op = read_fp_hreg(s, rn);
2df58130
AB
12766 TCGv_i32 tcg_res = tcg_temp_new_i32();
12767
2df58130
AB
12768 switch (fpop) {
12769 case 0x1a: /* FCVTNS */
12770 case 0x1b: /* FCVTMS */
12771 case 0x1c: /* FCVTAS */
12772 case 0x3a: /* FCVTPS */
12773 case 0x3b: /* FCVTZS */
12774 gen_helper_advsimd_f16tosinth(tcg_res, tcg_op, tcg_fpstatus);
12775 break;
fbd06e1e
AB
12776 case 0x3d: /* FRECPE */
12777 gen_helper_recpe_f16(tcg_res, tcg_op, tcg_fpstatus);
12778 break;
98695028
AB
12779 case 0x3f: /* FRECPX */
12780 gen_helper_frecpx_f16(tcg_res, tcg_op, tcg_fpstatus);
12781 break;
2df58130
AB
12782 case 0x5a: /* FCVTNU */
12783 case 0x5b: /* FCVTMU */
12784 case 0x5c: /* FCVTAU */
12785 case 0x7a: /* FCVTPU */
12786 case 0x7b: /* FCVTZU */
12787 gen_helper_advsimd_f16touinth(tcg_res, tcg_op, tcg_fpstatus);
12788 break;
15f8a233
AB
12789 case 0x6f: /* FNEG */
12790 tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
12791 break;
c625ff95
AB
12792 case 0x7d: /* FRSQRTE */
12793 gen_helper_rsqrte_f16(tcg_res, tcg_op, tcg_fpstatus);
12794 break;
2df58130
AB
12795 default:
12796 g_assert_not_reached();
12797 }
12798
12799 /* limit any sign extension going on */
12800 tcg_gen_andi_i32(tcg_res, tcg_res, 0xffff);
12801 write_fp_sreg(s, rd, tcg_res);
12802
12803 tcg_temp_free_i32(tcg_res);
12804 tcg_temp_free_i32(tcg_op);
6109aea2
AB
12805 } else {
12806 for (pass = 0; pass < (is_q ? 8 : 4); pass++) {
12807 TCGv_i32 tcg_op = tcg_temp_new_i32();
12808 TCGv_i32 tcg_res = tcg_temp_new_i32();
12809
12810 read_vec_element_i32(s, tcg_op, rn, pass, MO_16);
12811
12812 switch (fpop) {
2df58130
AB
12813 case 0x1a: /* FCVTNS */
12814 case 0x1b: /* FCVTMS */
12815 case 0x1c: /* FCVTAS */
12816 case 0x3a: /* FCVTPS */
12817 case 0x3b: /* FCVTZS */
12818 gen_helper_advsimd_f16tosinth(tcg_res, tcg_op, tcg_fpstatus);
12819 break;
fbd06e1e
AB
12820 case 0x3d: /* FRECPE */
12821 gen_helper_recpe_f16(tcg_res, tcg_op, tcg_fpstatus);
12822 break;
2df58130
AB
12823 case 0x5a: /* FCVTNU */
12824 case 0x5b: /* FCVTMU */
12825 case 0x5c: /* FCVTAU */
12826 case 0x7a: /* FCVTPU */
12827 case 0x7b: /* FCVTZU */
12828 gen_helper_advsimd_f16touinth(tcg_res, tcg_op, tcg_fpstatus);
12829 break;
6109aea2
AB
12830 case 0x18: /* FRINTN */
12831 case 0x19: /* FRINTM */
12832 case 0x38: /* FRINTP */
12833 case 0x39: /* FRINTZ */
12834 case 0x58: /* FRINTA */
12835 case 0x79: /* FRINTI */
12836 gen_helper_advsimd_rinth(tcg_res, tcg_op, tcg_fpstatus);
12837 break;
12838 case 0x59: /* FRINTX */
12839 gen_helper_advsimd_rinth_exact(tcg_res, tcg_op, tcg_fpstatus);
12840 break;
15f8a233
AB
12841 case 0x2f: /* FABS */
12842 tcg_gen_andi_i32(tcg_res, tcg_op, 0x7fff);
12843 break;
12844 case 0x6f: /* FNEG */
12845 tcg_gen_xori_i32(tcg_res, tcg_op, 0x8000);
12846 break;
c625ff95
AB
12847 case 0x7d: /* FRSQRTE */
12848 gen_helper_rsqrte_f16(tcg_res, tcg_op, tcg_fpstatus);
12849 break;
b96a54c7
AB
12850 case 0x7f: /* FSQRT */
12851 gen_helper_sqrt_f16(tcg_res, tcg_op, tcg_fpstatus);
12852 break;
6109aea2
AB
12853 default:
12854 g_assert_not_reached();
12855 }
12856
12857 write_vec_element_i32(s, tcg_res, rd, pass, MO_16);
12858
12859 tcg_temp_free_i32(tcg_res);
12860 tcg_temp_free_i32(tcg_op);
12861 }
12862
12863 clear_vec_high(s, is_q, rd);
12864 }
12865
12866 if (tcg_rmode) {
12867 gen_helper_set_rmode(tcg_rmode, tcg_rmode, tcg_fpstatus);
12868 tcg_temp_free_i32(tcg_rmode);
12869 }
12870
12871 if (tcg_fpstatus) {
12872 tcg_temp_free_ptr(tcg_fpstatus);
12873 }
5d432be6
AB
12874}
12875
4ce31af4 12876/* AdvSIMD scalar x indexed element
9f82e0ff
PM
12877 * 31 30 29 28 24 23 22 21 20 19 16 15 12 11 10 9 5 4 0
12878 * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
12879 * | 0 1 | U | 1 1 1 1 1 | size | L | M | Rm | opc | H | 0 | Rn | Rd |
12880 * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
4ce31af4 12881 * AdvSIMD vector x indexed element
384b26fb
AB
12882 * 31 30 29 28 24 23 22 21 20 19 16 15 12 11 10 9 5 4 0
12883 * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
12884 * | 0 | Q | U | 0 1 1 1 1 | size | L | M | Rm | opc | H | 0 | Rn | Rd |
12885 * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
12886 */
9f82e0ff 12887static void disas_simd_indexed(DisasContext *s, uint32_t insn)
384b26fb 12888{
f5e51e7f
PM
12889 /* This encoding has two kinds of instruction:
12890 * normal, where we perform elt x idxelt => elt for each
12891 * element in the vector
12892 * long, where we perform elt x idxelt and generate a result of
12893 * double the width of the input element
12894 * The long ops have a 'part' specifier (ie come in INSN, INSN2 pairs).
12895 */
9f82e0ff 12896 bool is_scalar = extract32(insn, 28, 1);
f5e51e7f
PM
12897 bool is_q = extract32(insn, 30, 1);
12898 bool u = extract32(insn, 29, 1);
12899 int size = extract32(insn, 22, 2);
12900 int l = extract32(insn, 21, 1);
12901 int m = extract32(insn, 20, 1);
12902 /* Note that the Rm field here is only 4 bits, not 5 as it usually is */
12903 int rm = extract32(insn, 16, 4);
12904 int opcode = extract32(insn, 12, 4);
12905 int h = extract32(insn, 11, 1);
12906 int rn = extract32(insn, 5, 5);
12907 int rd = extract32(insn, 0, 5);
12908 bool is_long = false;
d17b7cdc 12909 int is_fp = 0;
5d265064 12910 bool is_fp16 = false;
f5e51e7f
PM
12911 int index;
12912 TCGv_ptr fpst;
12913
5f81b1de
RH
12914 switch (16 * u + opcode) {
12915 case 0x08: /* MUL */
12916 case 0x10: /* MLA */
12917 case 0x14: /* MLS */
12918 if (is_scalar) {
f5e51e7f
PM
12919 unallocated_encoding(s);
12920 return;
12921 }
12922 break;
5f81b1de
RH
12923 case 0x02: /* SMLAL, SMLAL2 */
12924 case 0x12: /* UMLAL, UMLAL2 */
12925 case 0x06: /* SMLSL, SMLSL2 */
12926 case 0x16: /* UMLSL, UMLSL2 */
12927 case 0x0a: /* SMULL, SMULL2 */
12928 case 0x1a: /* UMULL, UMULL2 */
9f82e0ff
PM
12929 if (is_scalar) {
12930 unallocated_encoding(s);
12931 return;
12932 }
f5e51e7f
PM
12933 is_long = true;
12934 break;
5f81b1de
RH
12935 case 0x03: /* SQDMLAL, SQDMLAL2 */
12936 case 0x07: /* SQDMLSL, SQDMLSL2 */
12937 case 0x0b: /* SQDMULL, SQDMULL2 */
f5e51e7f 12938 is_long = true;
f5e51e7f 12939 break;
5f81b1de
RH
12940 case 0x0c: /* SQDMULH */
12941 case 0x0d: /* SQRDMULH */
9f82e0ff 12942 break;
5f81b1de
RH
12943 case 0x01: /* FMLA */
12944 case 0x05: /* FMLS */
12945 case 0x09: /* FMUL */
12946 case 0x19: /* FMULX */
d17b7cdc 12947 is_fp = 1;
f5e51e7f 12948 break;
d345df7a
RH
12949 case 0x1d: /* SQRDMLAH */
12950 case 0x1f: /* SQRDMLSH */
962fcbf2 12951 if (!dc_isar_feature(aa64_rdm, s)) {
d345df7a
RH
12952 unallocated_encoding(s);
12953 return;
12954 }
12955 break;
26c470a7
RH
12956 case 0x0e: /* SDOT */
12957 case 0x1e: /* UDOT */
4977986c 12958 if (is_scalar || size != MO_32 || !dc_isar_feature(aa64_dp, s)) {
26c470a7
RH
12959 unallocated_encoding(s);
12960 return;
12961 }
12962 break;
d17b7cdc
RH
12963 case 0x11: /* FCMLA #0 */
12964 case 0x13: /* FCMLA #90 */
12965 case 0x15: /* FCMLA #180 */
12966 case 0x17: /* FCMLA #270 */
4dfabb6d 12967 if (is_scalar || !dc_isar_feature(aa64_fcma, s)) {
d17b7cdc
RH
12968 unallocated_encoding(s);
12969 return;
12970 }
12971 is_fp = 2;
12972 break;
0caa5af8
RH
12973 case 0x00: /* FMLAL */
12974 case 0x04: /* FMLSL */
12975 case 0x18: /* FMLAL2 */
12976 case 0x1c: /* FMLSL2 */
12977 if (is_scalar || size != MO_32 || !dc_isar_feature(aa64_fhm, s)) {
12978 unallocated_encoding(s);
12979 return;
12980 }
12981 size = MO_16;
12982 /* is_fp, but we pass cpu_env not fp_status. */
12983 break;
f5e51e7f
PM
12984 default:
12985 unallocated_encoding(s);
12986 return;
12987 }
12988
d17b7cdc
RH
12989 switch (is_fp) {
12990 case 1: /* normal fp */
14776ab5 12991 /* convert insn encoded size to MemOp size */
5d265064 12992 switch (size) {
449f264b 12993 case 0: /* half-precision */
5d265064 12994 size = MO_16;
d17b7cdc 12995 is_fp16 = true;
449f264b
RH
12996 break;
12997 case MO_32: /* single precision */
12998 case MO_64: /* double precision */
12999 break;
13000 default:
5d265064
AB
13001 unallocated_encoding(s);
13002 return;
f5e51e7f 13003 }
d17b7cdc
RH
13004 break;
13005
13006 case 2: /* complex fp */
13007 /* Each indexable element is a complex pair. */
eaefb97a 13008 size += 1;
d17b7cdc
RH
13009 switch (size) {
13010 case MO_32:
13011 if (h && !is_q) {
13012 unallocated_encoding(s);
13013 return;
13014 }
13015 is_fp16 = true;
13016 break;
13017 case MO_64:
13018 break;
13019 default:
13020 unallocated_encoding(s);
13021 return;
13022 }
13023 break;
13024
13025 default: /* integer */
f5e51e7f 13026 switch (size) {
449f264b
RH
13027 case MO_8:
13028 case MO_64:
f5e51e7f
PM
13029 unallocated_encoding(s);
13030 return;
13031 }
d17b7cdc
RH
13032 break;
13033 }
5763190f 13034 if (is_fp16 && !dc_isar_feature(aa64_fp16, s)) {
d17b7cdc
RH
13035 unallocated_encoding(s);
13036 return;
f5e51e7f
PM
13037 }
13038
14776ab5 13039 /* Given MemOp size, adjust register and indexing. */
449f264b
RH
13040 switch (size) {
13041 case MO_16:
13042 index = h << 2 | l << 1 | m;
13043 break;
13044 case MO_32:
13045 index = h << 1 | l;
13046 rm |= m << 4;
13047 break;
13048 case MO_64:
13049 if (l || !is_q) {
13050 unallocated_encoding(s);
13051 return;
13052 }
13053 index = h;
13054 rm |= m << 4;
13055 break;
13056 default:
13057 g_assert_not_reached();
13058 }
13059
8c6afa6a
PM
13060 if (!fp_access_check(s)) {
13061 return;
13062 }
13063
f5e51e7f 13064 if (is_fp) {
5d265064 13065 fpst = get_fpstatus_ptr(is_fp16);
f5e51e7f 13066 } else {
f764718d 13067 fpst = NULL;
f5e51e7f
PM
13068 }
13069
d17b7cdc 13070 switch (16 * u + opcode) {
26c470a7
RH
13071 case 0x0e: /* SDOT */
13072 case 0x1e: /* UDOT */
13073 gen_gvec_op3_ool(s, is_q, rd, rn, rm, index,
13074 u ? gen_helper_gvec_udot_idx_b
13075 : gen_helper_gvec_sdot_idx_b);
13076 return;
d17b7cdc
RH
13077 case 0x11: /* FCMLA #0 */
13078 case 0x13: /* FCMLA #90 */
13079 case 0x15: /* FCMLA #180 */
13080 case 0x17: /* FCMLA #270 */
2cc99919
RH
13081 {
13082 int rot = extract32(insn, 13, 2);
13083 int data = (index << 2) | rot;
13084 tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
13085 vec_full_reg_offset(s, rn),
13086 vec_full_reg_offset(s, rm), fpst,
13087 is_q ? 16 : 8, vec_full_reg_size(s), data,
13088 size == MO_64
13089 ? gen_helper_gvec_fcmlas_idx
13090 : gen_helper_gvec_fcmlah_idx);
13091 tcg_temp_free_ptr(fpst);
13092 }
d17b7cdc 13093 return;
0caa5af8
RH
13094
13095 case 0x00: /* FMLAL */
13096 case 0x04: /* FMLSL */
13097 case 0x18: /* FMLAL2 */
13098 case 0x1c: /* FMLSL2 */
13099 {
13100 int is_s = extract32(opcode, 2, 1);
13101 int is_2 = u;
13102 int data = (index << 2) | (is_2 << 1) | is_s;
13103 tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
13104 vec_full_reg_offset(s, rn),
13105 vec_full_reg_offset(s, rm), cpu_env,
13106 is_q ? 16 : 8, vec_full_reg_size(s),
13107 data, gen_helper_gvec_fmlal_idx_a64);
13108 }
13109 return;
d17b7cdc
RH
13110 }
13111
f5e51e7f
PM
13112 if (size == 3) {
13113 TCGv_i64 tcg_idx = tcg_temp_new_i64();
13114 int pass;
13115
13116 assert(is_fp && is_q && !is_long);
13117
13118 read_vec_element(s, tcg_idx, rm, index, MO_64);
13119
9f82e0ff 13120 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
f5e51e7f
PM
13121 TCGv_i64 tcg_op = tcg_temp_new_i64();
13122 TCGv_i64 tcg_res = tcg_temp_new_i64();
13123
13124 read_vec_element(s, tcg_op, rn, pass, MO_64);
13125
5f81b1de
RH
13126 switch (16 * u + opcode) {
13127 case 0x05: /* FMLS */
f5e51e7f
PM
13128 /* As usual for ARM, separate negation for fused multiply-add */
13129 gen_helper_vfp_negd(tcg_op, tcg_op);
13130 /* fall through */
5f81b1de 13131 case 0x01: /* FMLA */
f5e51e7f
PM
13132 read_vec_element(s, tcg_res, rd, pass, MO_64);
13133 gen_helper_vfp_muladdd(tcg_res, tcg_op, tcg_idx, tcg_res, fpst);
13134 break;
5f81b1de
RH
13135 case 0x09: /* FMUL */
13136 gen_helper_vfp_muld(tcg_res, tcg_op, tcg_idx, fpst);
13137 break;
13138 case 0x19: /* FMULX */
13139 gen_helper_vfp_mulxd(tcg_res, tcg_op, tcg_idx, fpst);
f5e51e7f
PM
13140 break;
13141 default:
13142 g_assert_not_reached();
13143 }
13144
13145 write_vec_element(s, tcg_res, rd, pass, MO_64);
13146 tcg_temp_free_i64(tcg_op);
13147 tcg_temp_free_i64(tcg_res);
13148 }
13149
13150 tcg_temp_free_i64(tcg_idx);
4ff55bcb 13151 clear_vec_high(s, !is_scalar, rd);
f5e51e7f 13152 } else if (!is_long) {
9f82e0ff
PM
13153 /* 32 bit floating point, or 16 or 32 bit integer.
13154 * For the 16 bit scalar case we use the usual Neon helpers and
13155 * rely on the fact that 0 op 0 == 0 with no side effects.
13156 */
f5e51e7f 13157 TCGv_i32 tcg_idx = tcg_temp_new_i32();
9f82e0ff
PM
13158 int pass, maxpasses;
13159
13160 if (is_scalar) {
13161 maxpasses = 1;
13162 } else {
13163 maxpasses = is_q ? 4 : 2;
13164 }
f5e51e7f
PM
13165
13166 read_vec_element_i32(s, tcg_idx, rm, index, size);
13167
9f82e0ff 13168 if (size == 1 && !is_scalar) {
f5e51e7f
PM
13169 /* The simplest way to handle the 16x16 indexed ops is to duplicate
13170 * the index into both halves of the 32 bit tcg_idx and then use
13171 * the usual Neon helpers.
13172 */
13173 tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
13174 }
13175
9f82e0ff 13176 for (pass = 0; pass < maxpasses; pass++) {
f5e51e7f
PM
13177 TCGv_i32 tcg_op = tcg_temp_new_i32();
13178 TCGv_i32 tcg_res = tcg_temp_new_i32();
13179
9f82e0ff 13180 read_vec_element_i32(s, tcg_op, rn, pass, is_scalar ? size : MO_32);
f5e51e7f 13181
5f81b1de
RH
13182 switch (16 * u + opcode) {
13183 case 0x08: /* MUL */
13184 case 0x10: /* MLA */
13185 case 0x14: /* MLS */
f5e51e7f
PM
13186 {
13187 static NeonGenTwoOpFn * const fns[2][2] = {
13188 { gen_helper_neon_add_u16, gen_helper_neon_sub_u16 },
13189 { tcg_gen_add_i32, tcg_gen_sub_i32 },
13190 };
13191 NeonGenTwoOpFn *genfn;
13192 bool is_sub = opcode == 0x4;
13193
13194 if (size == 1) {
13195 gen_helper_neon_mul_u16(tcg_res, tcg_op, tcg_idx);
13196 } else {
13197 tcg_gen_mul_i32(tcg_res, tcg_op, tcg_idx);
13198 }
13199 if (opcode == 0x8) {
13200 break;
13201 }
13202 read_vec_element_i32(s, tcg_op, rd, pass, MO_32);
13203 genfn = fns[size - 1][is_sub];
13204 genfn(tcg_res, tcg_op, tcg_res);
13205 break;
13206 }
5f81b1de
RH
13207 case 0x05: /* FMLS */
13208 case 0x01: /* FMLA */
5d265064
AB
13209 read_vec_element_i32(s, tcg_res, rd, pass,
13210 is_scalar ? size : MO_32);
13211 switch (size) {
13212 case 1:
13213 if (opcode == 0x5) {
13214 /* As usual for ARM, separate negation for fused
13215 * multiply-add */
13216 tcg_gen_xori_i32(tcg_op, tcg_op, 0x80008000);
13217 }
6089030c
AB
13218 if (is_scalar) {
13219 gen_helper_advsimd_muladdh(tcg_res, tcg_op, tcg_idx,
13220 tcg_res, fpst);
13221 } else {
13222 gen_helper_advsimd_muladd2h(tcg_res, tcg_op, tcg_idx,
13223 tcg_res, fpst);
13224 }
5d265064
AB
13225 break;
13226 case 2:
13227 if (opcode == 0x5) {
13228 /* As usual for ARM, separate negation for
13229 * fused multiply-add */
13230 tcg_gen_xori_i32(tcg_op, tcg_op, 0x80000000);
13231 }
13232 gen_helper_vfp_muladds(tcg_res, tcg_op, tcg_idx,
13233 tcg_res, fpst);
13234 break;
13235 default:
13236 g_assert_not_reached();
13237 }
f5e51e7f 13238 break;
5f81b1de 13239 case 0x09: /* FMUL */
5d265064
AB
13240 switch (size) {
13241 case 1:
5f81b1de
RH
13242 if (is_scalar) {
13243 gen_helper_advsimd_mulh(tcg_res, tcg_op,
13244 tcg_idx, fpst);
5d265064 13245 } else {
5f81b1de
RH
13246 gen_helper_advsimd_mul2h(tcg_res, tcg_op,
13247 tcg_idx, fpst);
5d265064
AB
13248 }
13249 break;
13250 case 2:
5f81b1de
RH
13251 gen_helper_vfp_muls(tcg_res, tcg_op, tcg_idx, fpst);
13252 break;
13253 default:
13254 g_assert_not_reached();
13255 }
13256 break;
13257 case 0x19: /* FMULX */
13258 switch (size) {
13259 case 1:
13260 if (is_scalar) {
13261 gen_helper_advsimd_mulxh(tcg_res, tcg_op,
13262 tcg_idx, fpst);
5d265064 13263 } else {
5f81b1de
RH
13264 gen_helper_advsimd_mulx2h(tcg_res, tcg_op,
13265 tcg_idx, fpst);
5d265064
AB
13266 }
13267 break;
5f81b1de
RH
13268 case 2:
13269 gen_helper_vfp_mulxs(tcg_res, tcg_op, tcg_idx, fpst);
13270 break;
5d265064
AB
13271 default:
13272 g_assert_not_reached();
f5e51e7f
PM
13273 }
13274 break;
5f81b1de 13275 case 0x0c: /* SQDMULH */
f5e51e7f
PM
13276 if (size == 1) {
13277 gen_helper_neon_qdmulh_s16(tcg_res, cpu_env,
13278 tcg_op, tcg_idx);
13279 } else {
13280 gen_helper_neon_qdmulh_s32(tcg_res, cpu_env,
13281 tcg_op, tcg_idx);
13282 }
13283 break;
5f81b1de 13284 case 0x0d: /* SQRDMULH */
f5e51e7f
PM
13285 if (size == 1) {
13286 gen_helper_neon_qrdmulh_s16(tcg_res, cpu_env,
13287 tcg_op, tcg_idx);
13288 } else {
13289 gen_helper_neon_qrdmulh_s32(tcg_res, cpu_env,
13290 tcg_op, tcg_idx);
13291 }
13292 break;
d345df7a
RH
13293 case 0x1d: /* SQRDMLAH */
13294 read_vec_element_i32(s, tcg_res, rd, pass,
13295 is_scalar ? size : MO_32);
13296 if (size == 1) {
13297 gen_helper_neon_qrdmlah_s16(tcg_res, cpu_env,
13298 tcg_op, tcg_idx, tcg_res);
13299 } else {
13300 gen_helper_neon_qrdmlah_s32(tcg_res, cpu_env,
13301 tcg_op, tcg_idx, tcg_res);
13302 }
13303 break;
13304 case 0x1f: /* SQRDMLSH */
13305 read_vec_element_i32(s, tcg_res, rd, pass,
13306 is_scalar ? size : MO_32);
13307 if (size == 1) {
13308 gen_helper_neon_qrdmlsh_s16(tcg_res, cpu_env,
13309 tcg_op, tcg_idx, tcg_res);
13310 } else {
13311 gen_helper_neon_qrdmlsh_s32(tcg_res, cpu_env,
13312 tcg_op, tcg_idx, tcg_res);
13313 }
13314 break;
f5e51e7f
PM
13315 default:
13316 g_assert_not_reached();
13317 }
13318
9f82e0ff
PM
13319 if (is_scalar) {
13320 write_fp_sreg(s, rd, tcg_res);
13321 } else {
13322 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
13323 }
13324
f5e51e7f
PM
13325 tcg_temp_free_i32(tcg_op);
13326 tcg_temp_free_i32(tcg_res);
13327 }
13328
13329 tcg_temp_free_i32(tcg_idx);
4ff55bcb 13330 clear_vec_high(s, is_q, rd);
f5e51e7f
PM
13331 } else {
13332 /* long ops: 16x16->32 or 32x32->64 */
c44ad1fd
PM
13333 TCGv_i64 tcg_res[2];
13334 int pass;
13335 bool satop = extract32(opcode, 0, 1);
14776ab5 13336 MemOp memop = MO_32;
c44ad1fd
PM
13337
13338 if (satop || !u) {
13339 memop |= MO_SIGN;
13340 }
13341
13342 if (size == 2) {
13343 TCGv_i64 tcg_idx = tcg_temp_new_i64();
13344
13345 read_vec_element(s, tcg_idx, rm, index, memop);
13346
9f82e0ff 13347 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
c44ad1fd
PM
13348 TCGv_i64 tcg_op = tcg_temp_new_i64();
13349 TCGv_i64 tcg_passres;
9f82e0ff 13350 int passelt;
c44ad1fd 13351
9f82e0ff
PM
13352 if (is_scalar) {
13353 passelt = 0;
13354 } else {
13355 passelt = pass + (is_q * 2);
13356 }
13357
13358 read_vec_element(s, tcg_op, rn, passelt, memop);
c44ad1fd
PM
13359
13360 tcg_res[pass] = tcg_temp_new_i64();
13361
13362 if (opcode == 0xa || opcode == 0xb) {
13363 /* Non-accumulating ops */
13364 tcg_passres = tcg_res[pass];
13365 } else {
13366 tcg_passres = tcg_temp_new_i64();
13367 }
13368
13369 tcg_gen_mul_i64(tcg_passres, tcg_op, tcg_idx);
13370 tcg_temp_free_i64(tcg_op);
13371
13372 if (satop) {
13373 /* saturating, doubling */
13374 gen_helper_neon_addl_saturate_s64(tcg_passres, cpu_env,
13375 tcg_passres, tcg_passres);
13376 }
13377
13378 if (opcode == 0xa || opcode == 0xb) {
13379 continue;
13380 }
13381
13382 /* Accumulating op: handle accumulate step */
13383 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
13384
13385 switch (opcode) {
13386 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
13387 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
13388 break;
13389 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
13390 tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
13391 break;
13392 case 0x7: /* SQDMLSL, SQDMLSL2 */
13393 tcg_gen_neg_i64(tcg_passres, tcg_passres);
13394 /* fall through */
13395 case 0x3: /* SQDMLAL, SQDMLAL2 */
13396 gen_helper_neon_addl_saturate_s64(tcg_res[pass], cpu_env,
13397 tcg_res[pass],
13398 tcg_passres);
13399 break;
13400 default:
13401 g_assert_not_reached();
13402 }
13403 tcg_temp_free_i64(tcg_passres);
13404 }
13405 tcg_temp_free_i64(tcg_idx);
9f82e0ff 13406
4ff55bcb 13407 clear_vec_high(s, !is_scalar, rd);
c44ad1fd
PM
13408 } else {
13409 TCGv_i32 tcg_idx = tcg_temp_new_i32();
13410
13411 assert(size == 1);
13412 read_vec_element_i32(s, tcg_idx, rm, index, size);
13413
9f82e0ff
PM
13414 if (!is_scalar) {
13415 /* The simplest way to handle the 16x16 indexed ops is to
13416 * duplicate the index into both halves of the 32 bit tcg_idx
13417 * and then use the usual Neon helpers.
13418 */
13419 tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
13420 }
c44ad1fd 13421
9f82e0ff 13422 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
c44ad1fd
PM
13423 TCGv_i32 tcg_op = tcg_temp_new_i32();
13424 TCGv_i64 tcg_passres;
13425
9f82e0ff
PM
13426 if (is_scalar) {
13427 read_vec_element_i32(s, tcg_op, rn, pass, size);
13428 } else {
13429 read_vec_element_i32(s, tcg_op, rn,
13430 pass + (is_q * 2), MO_32);
13431 }
13432
c44ad1fd
PM
13433 tcg_res[pass] = tcg_temp_new_i64();
13434
13435 if (opcode == 0xa || opcode == 0xb) {
13436 /* Non-accumulating ops */
13437 tcg_passres = tcg_res[pass];
13438 } else {
13439 tcg_passres = tcg_temp_new_i64();
13440 }
13441
13442 if (memop & MO_SIGN) {
13443 gen_helper_neon_mull_s16(tcg_passres, tcg_op, tcg_idx);
13444 } else {
13445 gen_helper_neon_mull_u16(tcg_passres, tcg_op, tcg_idx);
13446 }
13447 if (satop) {
13448 gen_helper_neon_addl_saturate_s32(tcg_passres, cpu_env,
13449 tcg_passres, tcg_passres);
13450 }
13451 tcg_temp_free_i32(tcg_op);
13452
13453 if (opcode == 0xa || opcode == 0xb) {
13454 continue;
13455 }
13456
13457 /* Accumulating op: handle accumulate step */
13458 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
13459
13460 switch (opcode) {
13461 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
13462 gen_helper_neon_addl_u32(tcg_res[pass], tcg_res[pass],
13463 tcg_passres);
13464 break;
13465 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
13466 gen_helper_neon_subl_u32(tcg_res[pass], tcg_res[pass],
13467 tcg_passres);
13468 break;
13469 case 0x7: /* SQDMLSL, SQDMLSL2 */
13470 gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
13471 /* fall through */
13472 case 0x3: /* SQDMLAL, SQDMLAL2 */
13473 gen_helper_neon_addl_saturate_s32(tcg_res[pass], cpu_env,
13474 tcg_res[pass],
13475 tcg_passres);
13476 break;
13477 default:
13478 g_assert_not_reached();
13479 }
13480 tcg_temp_free_i64(tcg_passres);
13481 }
13482 tcg_temp_free_i32(tcg_idx);
9f82e0ff
PM
13483
13484 if (is_scalar) {
13485 tcg_gen_ext32u_i64(tcg_res[0], tcg_res[0]);
13486 }
13487 }
13488
13489 if (is_scalar) {
13490 tcg_res[1] = tcg_const_i64(0);
c44ad1fd
PM
13491 }
13492
13493 for (pass = 0; pass < 2; pass++) {
13494 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
13495 tcg_temp_free_i64(tcg_res[pass]);
13496 }
f5e51e7f
PM
13497 }
13498
f764718d 13499 if (fpst) {
f5e51e7f
PM
13500 tcg_temp_free_ptr(fpst);
13501 }
384b26fb
AB
13502}
13503
4ce31af4 13504/* Crypto AES
384b26fb
AB
13505 * 31 24 23 22 21 17 16 12 11 10 9 5 4 0
13506 * +-----------------+------+-----------+--------+-----+------+------+
13507 * | 0 1 0 0 1 1 1 0 | size | 1 0 1 0 0 | opcode | 1 0 | Rn | Rd |
13508 * +-----------------+------+-----------+--------+-----+------+------+
13509 */
13510static void disas_crypto_aes(DisasContext *s, uint32_t insn)
13511{
5acc765c
PM
13512 int size = extract32(insn, 22, 2);
13513 int opcode = extract32(insn, 12, 5);
13514 int rn = extract32(insn, 5, 5);
13515 int rd = extract32(insn, 0, 5);
13516 int decrypt;
1a66ac61
RH
13517 TCGv_ptr tcg_rd_ptr, tcg_rn_ptr;
13518 TCGv_i32 tcg_decrypt;
13519 CryptoThreeOpIntFn *genfn;
5acc765c 13520
962fcbf2 13521 if (!dc_isar_feature(aa64_aes, s) || size != 0) {
5acc765c
PM
13522 unallocated_encoding(s);
13523 return;
13524 }
13525
13526 switch (opcode) {
13527 case 0x4: /* AESE */
13528 decrypt = 0;
13529 genfn = gen_helper_crypto_aese;
13530 break;
13531 case 0x6: /* AESMC */
13532 decrypt = 0;
13533 genfn = gen_helper_crypto_aesmc;
13534 break;
13535 case 0x5: /* AESD */
13536 decrypt = 1;
13537 genfn = gen_helper_crypto_aese;
13538 break;
13539 case 0x7: /* AESIMC */
13540 decrypt = 1;
13541 genfn = gen_helper_crypto_aesmc;
13542 break;
13543 default:
13544 unallocated_encoding(s);
13545 return;
13546 }
13547
a4f5c5b7
NR
13548 if (!fp_access_check(s)) {
13549 return;
13550 }
13551
1a66ac61
RH
13552 tcg_rd_ptr = vec_full_reg_ptr(s, rd);
13553 tcg_rn_ptr = vec_full_reg_ptr(s, rn);
5acc765c
PM
13554 tcg_decrypt = tcg_const_i32(decrypt);
13555
1a66ac61 13556 genfn(tcg_rd_ptr, tcg_rn_ptr, tcg_decrypt);
5acc765c 13557
1a66ac61
RH
13558 tcg_temp_free_ptr(tcg_rd_ptr);
13559 tcg_temp_free_ptr(tcg_rn_ptr);
5acc765c 13560 tcg_temp_free_i32(tcg_decrypt);
384b26fb
AB
13561}
13562
4ce31af4 13563/* Crypto three-reg SHA
384b26fb
AB
13564 * 31 24 23 22 21 20 16 15 14 12 11 10 9 5 4 0
13565 * +-----------------+------+---+------+---+--------+-----+------+------+
13566 * | 0 1 0 1 1 1 1 0 | size | 0 | Rm | 0 | opcode | 0 0 | Rn | Rd |
13567 * +-----------------+------+---+------+---+--------+-----+------+------+
13568 */
13569static void disas_crypto_three_reg_sha(DisasContext *s, uint32_t insn)
13570{
be56f04e
PM
13571 int size = extract32(insn, 22, 2);
13572 int opcode = extract32(insn, 12, 3);
13573 int rm = extract32(insn, 16, 5);
13574 int rn = extract32(insn, 5, 5);
13575 int rd = extract32(insn, 0, 5);
1a66ac61
RH
13576 CryptoThreeOpFn *genfn;
13577 TCGv_ptr tcg_rd_ptr, tcg_rn_ptr, tcg_rm_ptr;
962fcbf2 13578 bool feature;
be56f04e
PM
13579
13580 if (size != 0) {
13581 unallocated_encoding(s);
13582 return;
13583 }
13584
13585 switch (opcode) {
13586 case 0: /* SHA1C */
13587 case 1: /* SHA1P */
13588 case 2: /* SHA1M */
13589 case 3: /* SHA1SU0 */
13590 genfn = NULL;
962fcbf2 13591 feature = dc_isar_feature(aa64_sha1, s);
be56f04e
PM
13592 break;
13593 case 4: /* SHA256H */
13594 genfn = gen_helper_crypto_sha256h;
962fcbf2 13595 feature = dc_isar_feature(aa64_sha256, s);
be56f04e
PM
13596 break;
13597 case 5: /* SHA256H2 */
13598 genfn = gen_helper_crypto_sha256h2;
962fcbf2 13599 feature = dc_isar_feature(aa64_sha256, s);
be56f04e
PM
13600 break;
13601 case 6: /* SHA256SU1 */
13602 genfn = gen_helper_crypto_sha256su1;
962fcbf2 13603 feature = dc_isar_feature(aa64_sha256, s);
be56f04e
PM
13604 break;
13605 default:
13606 unallocated_encoding(s);
13607 return;
13608 }
13609
962fcbf2 13610 if (!feature) {
be56f04e
PM
13611 unallocated_encoding(s);
13612 return;
13613 }
13614
a4f5c5b7
NR
13615 if (!fp_access_check(s)) {
13616 return;
13617 }
13618
1a66ac61
RH
13619 tcg_rd_ptr = vec_full_reg_ptr(s, rd);
13620 tcg_rn_ptr = vec_full_reg_ptr(s, rn);
13621 tcg_rm_ptr = vec_full_reg_ptr(s, rm);
be56f04e
PM
13622
13623 if (genfn) {
1a66ac61 13624 genfn(tcg_rd_ptr, tcg_rn_ptr, tcg_rm_ptr);
be56f04e
PM
13625 } else {
13626 TCGv_i32 tcg_opcode = tcg_const_i32(opcode);
13627
1a66ac61
RH
13628 gen_helper_crypto_sha1_3reg(tcg_rd_ptr, tcg_rn_ptr,
13629 tcg_rm_ptr, tcg_opcode);
be56f04e
PM
13630 tcg_temp_free_i32(tcg_opcode);
13631 }
13632
1a66ac61
RH
13633 tcg_temp_free_ptr(tcg_rd_ptr);
13634 tcg_temp_free_ptr(tcg_rn_ptr);
13635 tcg_temp_free_ptr(tcg_rm_ptr);
384b26fb
AB
13636}
13637
4ce31af4 13638/* Crypto two-reg SHA
384b26fb
AB
13639 * 31 24 23 22 21 17 16 12 11 10 9 5 4 0
13640 * +-----------------+------+-----------+--------+-----+------+------+
13641 * | 0 1 0 1 1 1 1 0 | size | 1 0 1 0 0 | opcode | 1 0 | Rn | Rd |
13642 * +-----------------+------+-----------+--------+-----+------+------+
13643 */
13644static void disas_crypto_two_reg_sha(DisasContext *s, uint32_t insn)
13645{
f6fe04d5
PM
13646 int size = extract32(insn, 22, 2);
13647 int opcode = extract32(insn, 12, 5);
13648 int rn = extract32(insn, 5, 5);
13649 int rd = extract32(insn, 0, 5);
1a66ac61 13650 CryptoTwoOpFn *genfn;
962fcbf2 13651 bool feature;
1a66ac61 13652 TCGv_ptr tcg_rd_ptr, tcg_rn_ptr;
f6fe04d5
PM
13653
13654 if (size != 0) {
13655 unallocated_encoding(s);
13656 return;
13657 }
13658
13659 switch (opcode) {
13660 case 0: /* SHA1H */
962fcbf2 13661 feature = dc_isar_feature(aa64_sha1, s);
f6fe04d5
PM
13662 genfn = gen_helper_crypto_sha1h;
13663 break;
13664 case 1: /* SHA1SU1 */
962fcbf2 13665 feature = dc_isar_feature(aa64_sha1, s);
f6fe04d5
PM
13666 genfn = gen_helper_crypto_sha1su1;
13667 break;
13668 case 2: /* SHA256SU0 */
962fcbf2 13669 feature = dc_isar_feature(aa64_sha256, s);
f6fe04d5
PM
13670 genfn = gen_helper_crypto_sha256su0;
13671 break;
13672 default:
13673 unallocated_encoding(s);
13674 return;
13675 }
13676
962fcbf2 13677 if (!feature) {
f6fe04d5
PM
13678 unallocated_encoding(s);
13679 return;
13680 }
13681
a4f5c5b7
NR
13682 if (!fp_access_check(s)) {
13683 return;
13684 }
13685
1a66ac61
RH
13686 tcg_rd_ptr = vec_full_reg_ptr(s, rd);
13687 tcg_rn_ptr = vec_full_reg_ptr(s, rn);
f6fe04d5 13688
1a66ac61 13689 genfn(tcg_rd_ptr, tcg_rn_ptr);
f6fe04d5 13690
1a66ac61
RH
13691 tcg_temp_free_ptr(tcg_rd_ptr);
13692 tcg_temp_free_ptr(tcg_rn_ptr);
384b26fb
AB
13693}
13694
90b827d1
AB
13695/* Crypto three-reg SHA512
13696 * 31 21 20 16 15 14 13 12 11 10 9 5 4 0
13697 * +-----------------------+------+---+---+-----+--------+------+------+
13698 * | 1 1 0 0 1 1 1 0 0 1 1 | Rm | 1 | O | 0 0 | opcode | Rn | Rd |
13699 * +-----------------------+------+---+---+-----+--------+------+------+
13700 */
13701static void disas_crypto_three_reg_sha512(DisasContext *s, uint32_t insn)
13702{
13703 int opcode = extract32(insn, 10, 2);
13704 int o = extract32(insn, 14, 1);
13705 int rm = extract32(insn, 16, 5);
13706 int rn = extract32(insn, 5, 5);
13707 int rd = extract32(insn, 0, 5);
962fcbf2 13708 bool feature;
90b827d1
AB
13709 CryptoThreeOpFn *genfn;
13710
13711 if (o == 0) {
13712 switch (opcode) {
13713 case 0: /* SHA512H */
962fcbf2 13714 feature = dc_isar_feature(aa64_sha512, s);
90b827d1
AB
13715 genfn = gen_helper_crypto_sha512h;
13716 break;
13717 case 1: /* SHA512H2 */
962fcbf2 13718 feature = dc_isar_feature(aa64_sha512, s);
90b827d1
AB
13719 genfn = gen_helper_crypto_sha512h2;
13720 break;
13721 case 2: /* SHA512SU1 */
962fcbf2 13722 feature = dc_isar_feature(aa64_sha512, s);
90b827d1
AB
13723 genfn = gen_helper_crypto_sha512su1;
13724 break;
cd270ade 13725 case 3: /* RAX1 */
962fcbf2 13726 feature = dc_isar_feature(aa64_sha3, s);
cd270ade
AB
13727 genfn = NULL;
13728 break;
c7a5e791
PN
13729 default:
13730 g_assert_not_reached();
90b827d1
AB
13731 }
13732 } else {
80d6f4c6
AB
13733 switch (opcode) {
13734 case 0: /* SM3PARTW1 */
962fcbf2 13735 feature = dc_isar_feature(aa64_sm3, s);
80d6f4c6
AB
13736 genfn = gen_helper_crypto_sm3partw1;
13737 break;
13738 case 1: /* SM3PARTW2 */
962fcbf2 13739 feature = dc_isar_feature(aa64_sm3, s);
80d6f4c6
AB
13740 genfn = gen_helper_crypto_sm3partw2;
13741 break;
b6577bcd 13742 case 2: /* SM4EKEY */
962fcbf2 13743 feature = dc_isar_feature(aa64_sm4, s);
b6577bcd
AB
13744 genfn = gen_helper_crypto_sm4ekey;
13745 break;
80d6f4c6
AB
13746 default:
13747 unallocated_encoding(s);
13748 return;
13749 }
90b827d1
AB
13750 }
13751
962fcbf2 13752 if (!feature) {
90b827d1
AB
13753 unallocated_encoding(s);
13754 return;
13755 }
13756
13757 if (!fp_access_check(s)) {
13758 return;
13759 }
13760
13761 if (genfn) {
13762 TCGv_ptr tcg_rd_ptr, tcg_rn_ptr, tcg_rm_ptr;
13763
13764 tcg_rd_ptr = vec_full_reg_ptr(s, rd);
13765 tcg_rn_ptr = vec_full_reg_ptr(s, rn);
13766 tcg_rm_ptr = vec_full_reg_ptr(s, rm);
13767
13768 genfn(tcg_rd_ptr, tcg_rn_ptr, tcg_rm_ptr);
13769
13770 tcg_temp_free_ptr(tcg_rd_ptr);
13771 tcg_temp_free_ptr(tcg_rn_ptr);
13772 tcg_temp_free_ptr(tcg_rm_ptr);
13773 } else {
cd270ade
AB
13774 TCGv_i64 tcg_op1, tcg_op2, tcg_res[2];
13775 int pass;
13776
13777 tcg_op1 = tcg_temp_new_i64();
13778 tcg_op2 = tcg_temp_new_i64();
13779 tcg_res[0] = tcg_temp_new_i64();
13780 tcg_res[1] = tcg_temp_new_i64();
13781
13782 for (pass = 0; pass < 2; pass++) {
13783 read_vec_element(s, tcg_op1, rn, pass, MO_64);
13784 read_vec_element(s, tcg_op2, rm, pass, MO_64);
13785
13786 tcg_gen_rotli_i64(tcg_res[pass], tcg_op2, 1);
13787 tcg_gen_xor_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
13788 }
13789 write_vec_element(s, tcg_res[0], rd, 0, MO_64);
13790 write_vec_element(s, tcg_res[1], rd, 1, MO_64);
13791
13792 tcg_temp_free_i64(tcg_op1);
13793 tcg_temp_free_i64(tcg_op2);
13794 tcg_temp_free_i64(tcg_res[0]);
13795 tcg_temp_free_i64(tcg_res[1]);
90b827d1
AB
13796 }
13797}
13798
13799/* Crypto two-reg SHA512
13800 * 31 12 11 10 9 5 4 0
13801 * +-----------------------------------------+--------+------+------+
13802 * | 1 1 0 0 1 1 1 0 1 1 0 0 0 0 0 0 1 0 0 0 | opcode | Rn | Rd |
13803 * +-----------------------------------------+--------+------+------+
13804 */
13805static void disas_crypto_two_reg_sha512(DisasContext *s, uint32_t insn)
13806{
13807 int opcode = extract32(insn, 10, 2);
13808 int rn = extract32(insn, 5, 5);
13809 int rd = extract32(insn, 0, 5);
13810 TCGv_ptr tcg_rd_ptr, tcg_rn_ptr;
962fcbf2 13811 bool feature;
90b827d1
AB
13812 CryptoTwoOpFn *genfn;
13813
13814 switch (opcode) {
13815 case 0: /* SHA512SU0 */
962fcbf2 13816 feature = dc_isar_feature(aa64_sha512, s);
90b827d1
AB
13817 genfn = gen_helper_crypto_sha512su0;
13818 break;
b6577bcd 13819 case 1: /* SM4E */
962fcbf2 13820 feature = dc_isar_feature(aa64_sm4, s);
b6577bcd
AB
13821 genfn = gen_helper_crypto_sm4e;
13822 break;
90b827d1
AB
13823 default:
13824 unallocated_encoding(s);
13825 return;
13826 }
13827
962fcbf2 13828 if (!feature) {
90b827d1
AB
13829 unallocated_encoding(s);
13830 return;
13831 }
13832
13833 if (!fp_access_check(s)) {
13834 return;
13835 }
13836
13837 tcg_rd_ptr = vec_full_reg_ptr(s, rd);
13838 tcg_rn_ptr = vec_full_reg_ptr(s, rn);
13839
13840 genfn(tcg_rd_ptr, tcg_rn_ptr);
13841
13842 tcg_temp_free_ptr(tcg_rd_ptr);
13843 tcg_temp_free_ptr(tcg_rn_ptr);
13844}
13845
cd270ade
AB
13846/* Crypto four-register
13847 * 31 23 22 21 20 16 15 14 10 9 5 4 0
13848 * +-------------------+-----+------+---+------+------+------+
13849 * | 1 1 0 0 1 1 1 0 0 | Op0 | Rm | 0 | Ra | Rn | Rd |
13850 * +-------------------+-----+------+---+------+------+------+
13851 */
13852static void disas_crypto_four_reg(DisasContext *s, uint32_t insn)
13853{
13854 int op0 = extract32(insn, 21, 2);
13855 int rm = extract32(insn, 16, 5);
13856 int ra = extract32(insn, 10, 5);
13857 int rn = extract32(insn, 5, 5);
13858 int rd = extract32(insn, 0, 5);
962fcbf2 13859 bool feature;
cd270ade
AB
13860
13861 switch (op0) {
13862 case 0: /* EOR3 */
13863 case 1: /* BCAX */
962fcbf2 13864 feature = dc_isar_feature(aa64_sha3, s);
cd270ade 13865 break;
80d6f4c6 13866 case 2: /* SM3SS1 */
962fcbf2 13867 feature = dc_isar_feature(aa64_sm3, s);
80d6f4c6 13868 break;
cd270ade
AB
13869 default:
13870 unallocated_encoding(s);
13871 return;
13872 }
13873
962fcbf2 13874 if (!feature) {
cd270ade
AB
13875 unallocated_encoding(s);
13876 return;
13877 }
13878
13879 if (!fp_access_check(s)) {
13880 return;
13881 }
13882
13883 if (op0 < 2) {
13884 TCGv_i64 tcg_op1, tcg_op2, tcg_op3, tcg_res[2];
13885 int pass;
13886
13887 tcg_op1 = tcg_temp_new_i64();
13888 tcg_op2 = tcg_temp_new_i64();
13889 tcg_op3 = tcg_temp_new_i64();
13890 tcg_res[0] = tcg_temp_new_i64();
13891 tcg_res[1] = tcg_temp_new_i64();
13892
13893 for (pass = 0; pass < 2; pass++) {
13894 read_vec_element(s, tcg_op1, rn, pass, MO_64);
13895 read_vec_element(s, tcg_op2, rm, pass, MO_64);
13896 read_vec_element(s, tcg_op3, ra, pass, MO_64);
13897
13898 if (op0 == 0) {
13899 /* EOR3 */
13900 tcg_gen_xor_i64(tcg_res[pass], tcg_op2, tcg_op3);
13901 } else {
13902 /* BCAX */
13903 tcg_gen_andc_i64(tcg_res[pass], tcg_op2, tcg_op3);
13904 }
13905 tcg_gen_xor_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
13906 }
13907 write_vec_element(s, tcg_res[0], rd, 0, MO_64);
13908 write_vec_element(s, tcg_res[1], rd, 1, MO_64);
13909
13910 tcg_temp_free_i64(tcg_op1);
13911 tcg_temp_free_i64(tcg_op2);
13912 tcg_temp_free_i64(tcg_op3);
13913 tcg_temp_free_i64(tcg_res[0]);
13914 tcg_temp_free_i64(tcg_res[1]);
13915 } else {
80d6f4c6
AB
13916 TCGv_i32 tcg_op1, tcg_op2, tcg_op3, tcg_res, tcg_zero;
13917
13918 tcg_op1 = tcg_temp_new_i32();
13919 tcg_op2 = tcg_temp_new_i32();
13920 tcg_op3 = tcg_temp_new_i32();
13921 tcg_res = tcg_temp_new_i32();
13922 tcg_zero = tcg_const_i32(0);
13923
13924 read_vec_element_i32(s, tcg_op1, rn, 3, MO_32);
13925 read_vec_element_i32(s, tcg_op2, rm, 3, MO_32);
13926 read_vec_element_i32(s, tcg_op3, ra, 3, MO_32);
13927
13928 tcg_gen_rotri_i32(tcg_res, tcg_op1, 20);
13929 tcg_gen_add_i32(tcg_res, tcg_res, tcg_op2);
13930 tcg_gen_add_i32(tcg_res, tcg_res, tcg_op3);
13931 tcg_gen_rotri_i32(tcg_res, tcg_res, 25);
13932
13933 write_vec_element_i32(s, tcg_zero, rd, 0, MO_32);
13934 write_vec_element_i32(s, tcg_zero, rd, 1, MO_32);
13935 write_vec_element_i32(s, tcg_zero, rd, 2, MO_32);
13936 write_vec_element_i32(s, tcg_res, rd, 3, MO_32);
13937
13938 tcg_temp_free_i32(tcg_op1);
13939 tcg_temp_free_i32(tcg_op2);
13940 tcg_temp_free_i32(tcg_op3);
13941 tcg_temp_free_i32(tcg_res);
13942 tcg_temp_free_i32(tcg_zero);
cd270ade
AB
13943 }
13944}
13945
13946/* Crypto XAR
13947 * 31 21 20 16 15 10 9 5 4 0
13948 * +-----------------------+------+--------+------+------+
13949 * | 1 1 0 0 1 1 1 0 1 0 0 | Rm | imm6 | Rn | Rd |
13950 * +-----------------------+------+--------+------+------+
13951 */
13952static void disas_crypto_xar(DisasContext *s, uint32_t insn)
13953{
13954 int rm = extract32(insn, 16, 5);
13955 int imm6 = extract32(insn, 10, 6);
13956 int rn = extract32(insn, 5, 5);
13957 int rd = extract32(insn, 0, 5);
13958 TCGv_i64 tcg_op1, tcg_op2, tcg_res[2];
13959 int pass;
13960
962fcbf2 13961 if (!dc_isar_feature(aa64_sha3, s)) {
cd270ade
AB
13962 unallocated_encoding(s);
13963 return;
13964 }
13965
13966 if (!fp_access_check(s)) {
13967 return;
13968 }
13969
13970 tcg_op1 = tcg_temp_new_i64();
13971 tcg_op2 = tcg_temp_new_i64();
13972 tcg_res[0] = tcg_temp_new_i64();
13973 tcg_res[1] = tcg_temp_new_i64();
13974
13975 for (pass = 0; pass < 2; pass++) {
13976 read_vec_element(s, tcg_op1, rn, pass, MO_64);
13977 read_vec_element(s, tcg_op2, rm, pass, MO_64);
13978
13979 tcg_gen_xor_i64(tcg_res[pass], tcg_op1, tcg_op2);
13980 tcg_gen_rotri_i64(tcg_res[pass], tcg_res[pass], imm6);
13981 }
13982 write_vec_element(s, tcg_res[0], rd, 0, MO_64);
13983 write_vec_element(s, tcg_res[1], rd, 1, MO_64);
13984
13985 tcg_temp_free_i64(tcg_op1);
13986 tcg_temp_free_i64(tcg_op2);
13987 tcg_temp_free_i64(tcg_res[0]);
13988 tcg_temp_free_i64(tcg_res[1]);
13989}
13990
80d6f4c6
AB
13991/* Crypto three-reg imm2
13992 * 31 21 20 16 15 14 13 12 11 10 9 5 4 0
13993 * +-----------------------+------+-----+------+--------+------+------+
13994 * | 1 1 0 0 1 1 1 0 0 1 0 | Rm | 1 0 | imm2 | opcode | Rn | Rd |
13995 * +-----------------------+------+-----+------+--------+------+------+
13996 */
13997static void disas_crypto_three_reg_imm2(DisasContext *s, uint32_t insn)
13998{
13999 int opcode = extract32(insn, 10, 2);
14000 int imm2 = extract32(insn, 12, 2);
14001 int rm = extract32(insn, 16, 5);
14002 int rn = extract32(insn, 5, 5);
14003 int rd = extract32(insn, 0, 5);
14004 TCGv_ptr tcg_rd_ptr, tcg_rn_ptr, tcg_rm_ptr;
14005 TCGv_i32 tcg_imm2, tcg_opcode;
14006
962fcbf2 14007 if (!dc_isar_feature(aa64_sm3, s)) {
80d6f4c6
AB
14008 unallocated_encoding(s);
14009 return;
14010 }
14011
14012 if (!fp_access_check(s)) {
14013 return;
14014 }
14015
14016 tcg_rd_ptr = vec_full_reg_ptr(s, rd);
14017 tcg_rn_ptr = vec_full_reg_ptr(s, rn);
14018 tcg_rm_ptr = vec_full_reg_ptr(s, rm);
14019 tcg_imm2 = tcg_const_i32(imm2);
14020 tcg_opcode = tcg_const_i32(opcode);
14021
14022 gen_helper_crypto_sm3tt(tcg_rd_ptr, tcg_rn_ptr, tcg_rm_ptr, tcg_imm2,
14023 tcg_opcode);
14024
14025 tcg_temp_free_ptr(tcg_rd_ptr);
14026 tcg_temp_free_ptr(tcg_rn_ptr);
14027 tcg_temp_free_ptr(tcg_rm_ptr);
14028 tcg_temp_free_i32(tcg_imm2);
14029 tcg_temp_free_i32(tcg_opcode);
14030}
14031
384b26fb
AB
14032/* C3.6 Data processing - SIMD, inc Crypto
14033 *
14034 * As the decode gets a little complex we are using a table based
14035 * approach for this part of the decode.
14036 */
14037static const AArch64DecodeTable data_proc_simd[] = {
14038 /* pattern , mask , fn */
14039 { 0x0e200400, 0x9f200400, disas_simd_three_reg_same },
e7186d82 14040 { 0x0e008400, 0x9f208400, disas_simd_three_reg_same_extra },
384b26fb
AB
14041 { 0x0e200000, 0x9f200c00, disas_simd_three_reg_diff },
14042 { 0x0e200800, 0x9f3e0c00, disas_simd_two_reg_misc },
14043 { 0x0e300800, 0x9f3e0c00, disas_simd_across_lanes },
14044 { 0x0e000400, 0x9fe08400, disas_simd_copy },
9f82e0ff 14045 { 0x0f000000, 0x9f000400, disas_simd_indexed }, /* vector indexed */
384b26fb
AB
14046 /* simd_mod_imm decode is a subset of simd_shift_imm, so must precede it */
14047 { 0x0f000400, 0x9ff80400, disas_simd_mod_imm },
14048 { 0x0f000400, 0x9f800400, disas_simd_shift_imm },
14049 { 0x0e000000, 0xbf208c00, disas_simd_tb },
14050 { 0x0e000800, 0xbf208c00, disas_simd_zip_trn },
14051 { 0x2e000000, 0xbf208400, disas_simd_ext },
14052 { 0x5e200400, 0xdf200400, disas_simd_scalar_three_reg_same },
d9061ec3 14053 { 0x5e008400, 0xdf208400, disas_simd_scalar_three_reg_same_extra },
384b26fb
AB
14054 { 0x5e200000, 0xdf200c00, disas_simd_scalar_three_reg_diff },
14055 { 0x5e200800, 0xdf3e0c00, disas_simd_scalar_two_reg_misc },
14056 { 0x5e300800, 0xdf3e0c00, disas_simd_scalar_pairwise },
14057 { 0x5e000400, 0xdfe08400, disas_simd_scalar_copy },
9f82e0ff 14058 { 0x5f000000, 0xdf000400, disas_simd_indexed }, /* scalar indexed */
384b26fb
AB
14059 { 0x5f000400, 0xdf800400, disas_simd_scalar_shift_imm },
14060 { 0x4e280800, 0xff3e0c00, disas_crypto_aes },
14061 { 0x5e000000, 0xff208c00, disas_crypto_three_reg_sha },
14062 { 0x5e280800, 0xff3e0c00, disas_crypto_two_reg_sha },
90b827d1
AB
14063 { 0xce608000, 0xffe0b000, disas_crypto_three_reg_sha512 },
14064 { 0xcec08000, 0xfffff000, disas_crypto_two_reg_sha512 },
cd270ade
AB
14065 { 0xce000000, 0xff808000, disas_crypto_four_reg },
14066 { 0xce800000, 0xffe00000, disas_crypto_xar },
80d6f4c6 14067 { 0xce408000, 0xffe0c000, disas_crypto_three_reg_imm2 },
376e8d6c 14068 { 0x0e400400, 0x9f60c400, disas_simd_three_reg_same_fp16 },
5d432be6 14069 { 0x0e780800, 0x8f7e0c00, disas_simd_two_reg_misc_fp16 },
7c93b774 14070 { 0x5e400400, 0xdf60c400, disas_simd_scalar_three_reg_same_fp16 },
384b26fb
AB
14071 { 0x00000000, 0x00000000, NULL }
14072};
14073
faa0ba46
PM
14074static void disas_data_proc_simd(DisasContext *s, uint32_t insn)
14075{
14076 /* Note that this is called with all non-FP cases from
14077 * table C3-6 so it must UNDEF for entries not specifically
14078 * allocated to instructions in that table.
14079 */
384b26fb
AB
14080 AArch64DecodeFn *fn = lookup_disas_fn(&data_proc_simd[0], insn);
14081 if (fn) {
14082 fn(s, insn);
14083 } else {
14084 unallocated_encoding(s);
14085 }
faa0ba46
PM
14086}
14087
ad7ee8a2
CF
14088/* C3.6 Data processing - SIMD and floating point */
14089static void disas_data_proc_simd_fp(DisasContext *s, uint32_t insn)
14090{
faa0ba46
PM
14091 if (extract32(insn, 28, 1) == 1 && extract32(insn, 30, 1) == 0) {
14092 disas_data_proc_fp(s, insn);
14093 } else {
14094 /* SIMD, including crypto */
14095 disas_data_proc_simd(s, insn);
14096 }
ad7ee8a2
CF
14097}
14098
51bf0d7a
RH
14099/**
14100 * is_guarded_page:
14101 * @env: The cpu environment
14102 * @s: The DisasContext
14103 *
14104 * Return true if the page is guarded.
14105 */
14106static bool is_guarded_page(CPUARMState *env, DisasContext *s)
14107{
14108#ifdef CONFIG_USER_ONLY
14109 return false; /* FIXME */
14110#else
14111 uint64_t addr = s->base.pc_first;
14112 int mmu_idx = arm_to_core_mmu_idx(s->mmu_idx);
14113 unsigned int index = tlb_index(env, mmu_idx, addr);
14114 CPUTLBEntry *entry = tlb_entry(env, mmu_idx, addr);
14115
14116 /*
14117 * We test this immediately after reading an insn, which means
14118 * that any normal page must be in the TLB. The only exception
14119 * would be for executing from flash or device memory, which
14120 * does not retain the TLB entry.
14121 *
14122 * FIXME: Assume false for those, for now. We could use
14123 * arm_cpu_get_phys_page_attrs_debug to re-read the page
14124 * table entry even for that case.
14125 */
14126 return (tlb_hit(entry->addr_code, addr) &&
a40ec84e 14127 env_tlb(env)->d[mmu_idx].iotlb[index].attrs.target_tlb_bit0);
51bf0d7a
RH
14128#endif
14129}
14130
14131/**
14132 * btype_destination_ok:
14133 * @insn: The instruction at the branch destination
14134 * @bt: SCTLR_ELx.BT
14135 * @btype: PSTATE.BTYPE, and is non-zero
14136 *
14137 * On a guarded page, there are a limited number of insns
14138 * that may be present at the branch target:
14139 * - branch target identifiers,
14140 * - paciasp, pacibsp,
14141 * - BRK insn
14142 * - HLT insn
14143 * Anything else causes a Branch Target Exception.
14144 *
14145 * Return true if the branch is compatible, false to raise BTITRAP.
14146 */
14147static bool btype_destination_ok(uint32_t insn, bool bt, int btype)
14148{
14149 if ((insn & 0xfffff01fu) == 0xd503201fu) {
14150 /* HINT space */
14151 switch (extract32(insn, 5, 7)) {
14152 case 0b011001: /* PACIASP */
14153 case 0b011011: /* PACIBSP */
14154 /*
14155 * If SCTLR_ELx.BT, then PACI*SP are not compatible
14156 * with btype == 3. Otherwise all btype are ok.
14157 */
14158 return !bt || btype != 3;
14159 case 0b100000: /* BTI */
14160 /* Not compatible with any btype. */
14161 return false;
14162 case 0b100010: /* BTI c */
14163 /* Not compatible with btype == 3 */
14164 return btype != 3;
14165 case 0b100100: /* BTI j */
14166 /* Not compatible with btype == 2 */
14167 return btype != 2;
14168 case 0b100110: /* BTI jc */
14169 /* Compatible with any btype. */
14170 return true;
14171 }
14172 } else {
14173 switch (insn & 0xffe0001fu) {
14174 case 0xd4200000u: /* BRK */
14175 case 0xd4400000u: /* HLT */
14176 /* Give priority to the breakpoint exception. */
14177 return true;
14178 }
14179 }
14180 return false;
14181}
14182
ad7ee8a2 14183/* C3.1 A64 instruction index by encoding */
40f860cd 14184static void disas_a64_insn(CPUARMState *env, DisasContext *s)
14ade10f
AG
14185{
14186 uint32_t insn;
14187
a0415916
RH
14188 s->pc_curr = s->base.pc_next;
14189 insn = arm_ldl_code(env, s->base.pc_next, s->sctlr_b);
14ade10f 14190 s->insn = insn;
a0415916 14191 s->base.pc_next += 4;
14ade10f 14192
90e49638
PM
14193 s->fp_access_checked = false;
14194
51bf0d7a
RH
14195 if (dc_isar_feature(aa64_bti, s)) {
14196 if (s->base.num_insns == 1) {
14197 /*
14198 * At the first insn of the TB, compute s->guarded_page.
14199 * We delayed computing this until successfully reading
14200 * the first insn of the TB, above. This (mostly) ensures
14201 * that the softmmu tlb entry has been populated, and the
14202 * page table GP bit is available.
14203 *
14204 * Note that we need to compute this even if btype == 0,
14205 * because this value is used for BR instructions later
14206 * where ENV is not available.
14207 */
14208 s->guarded_page = is_guarded_page(env, s);
14209
14210 /* First insn can have btype set to non-zero. */
14211 tcg_debug_assert(s->btype >= 0);
14212
14213 /*
14214 * Note that the Branch Target Exception has fairly high
14215 * priority -- below debugging exceptions but above most
14216 * everything else. This allows us to handle this now
14217 * instead of waiting until the insn is otherwise decoded.
14218 */
14219 if (s->btype != 0
14220 && s->guarded_page
14221 && !btype_destination_ok(insn, s->bt, s->btype)) {
a767fac8
RH
14222 gen_exception_insn(s, s->pc_curr, EXCP_UDEF,
14223 syn_btitrap(s->btype),
51bf0d7a
RH
14224 default_exception_el(s));
14225 return;
14226 }
14227 } else {
14228 /* Not the first insn: btype must be 0. */
14229 tcg_debug_assert(s->btype == 0);
14230 }
14231 }
14232
ad7ee8a2 14233 switch (extract32(insn, 25, 4)) {
38388f7e 14234 case 0x0: case 0x1: case 0x3: /* UNALLOCATED */
14ade10f
AG
14235 unallocated_encoding(s);
14236 break;
38388f7e 14237 case 0x2:
cd208a1c 14238 if (!dc_isar_feature(aa64_sve, s) || !disas_sve(s, insn)) {
38388f7e
RH
14239 unallocated_encoding(s);
14240 }
14241 break;
ad7ee8a2
CF
14242 case 0x8: case 0x9: /* Data processing - immediate */
14243 disas_data_proc_imm(s, insn);
14244 break;
14245 case 0xa: case 0xb: /* Branch, exception generation and system insns */
14246 disas_b_exc_sys(s, insn);
14247 break;
14248 case 0x4:
14249 case 0x6:
14250 case 0xc:
14251 case 0xe: /* Loads and stores */
14252 disas_ldst(s, insn);
14253 break;
14254 case 0x5:
14255 case 0xd: /* Data processing - register */
14256 disas_data_proc_reg(s, insn);
14257 break;
14258 case 0x7:
14259 case 0xf: /* Data processing - SIMD and floating point */
14260 disas_data_proc_simd_fp(s, insn);
14261 break;
14262 default:
14263 assert(FALSE); /* all 15 cases should be handled above */
14264 break;
14ade10f 14265 }
11e169de
AG
14266
14267 /* if we allocated any temporaries, free them here */
14268 free_tmp_a64(s);
51bf0d7a
RH
14269
14270 /*
14271 * After execution of most insns, btype is reset to 0.
14272 * Note that we set btype == -1 when the insn sets btype.
14273 */
14274 if (s->btype > 0 && s->base.is_jmp != DISAS_NORETURN) {
14275 reset_btype(s);
14276 }
40f860cd 14277}
14ade10f 14278
b542683d
EC
14279static void aarch64_tr_init_disas_context(DisasContextBase *dcbase,
14280 CPUState *cpu)
40f860cd 14281{
dcba3a8d 14282 DisasContext *dc = container_of(dcbase, DisasContext, base);
5c039906 14283 CPUARMState *env = cpu->env_ptr;
2fc0cc0e 14284 ARMCPU *arm_cpu = env_archcpu(env);
aad821ac
RH
14285 uint32_t tb_flags = dc->base.tb->flags;
14286 int bound, core_mmu_idx;
40f860cd 14287
962fcbf2 14288 dc->isar = &arm_cpu->isar;
40f860cd
PM
14289 dc->condjmp = 0;
14290
14291 dc->aarch64 = 1;
cef9ee70
SS
14292 /* If we are coming from secure EL0 in a system with a 32-bit EL3, then
14293 * there is no secure EL1, so we route exceptions to EL3.
14294 */
14295 dc->secure_routed_to_el3 = arm_feature(env, ARM_FEATURE_EL3) &&
14296 !arm_el_is_aa64(env, 3);
40f860cd 14297 dc->thumb = 0;
f9fd40eb 14298 dc->sctlr_b = 0;
aad821ac 14299 dc->be_data = FIELD_EX32(tb_flags, TBFLAG_ANY, BE_DATA) ? MO_BE : MO_LE;
40f860cd
PM
14300 dc->condexec_mask = 0;
14301 dc->condexec_cond = 0;
aad821ac 14302 core_mmu_idx = FIELD_EX32(tb_flags, TBFLAG_ANY, MMUIDX);
20dc67c9 14303 dc->mmu_idx = core_to_aa64_mmu_idx(core_mmu_idx);
476a4692 14304 dc->tbii = FIELD_EX32(tb_flags, TBFLAG_A64, TBII);
4a9ee99d 14305 dc->tbid = FIELD_EX32(tb_flags, TBFLAG_A64, TBID);
c1e37810 14306 dc->current_el = arm_mmu_idx_to_el(dc->mmu_idx);
40f860cd 14307#if !defined(CONFIG_USER_ONLY)
c1e37810 14308 dc->user = (dc->current_el == 0);
40f860cd 14309#endif
aad821ac
RH
14310 dc->fp_excp_el = FIELD_EX32(tb_flags, TBFLAG_ANY, FPEXC_EL);
14311 dc->sve_excp_el = FIELD_EX32(tb_flags, TBFLAG_A64, SVEEXC_EL);
14312 dc->sve_len = (FIELD_EX32(tb_flags, TBFLAG_A64, ZCR_LEN) + 1) * 16;
0816ef1b 14313 dc->pauth_active = FIELD_EX32(tb_flags, TBFLAG_A64, PAUTH_ACTIVE);
08f1434a
RH
14314 dc->bt = FIELD_EX32(tb_flags, TBFLAG_A64, BT);
14315 dc->btype = FIELD_EX32(tb_flags, TBFLAG_A64, BTYPE);
cc28fc30 14316 dc->unpriv = FIELD_EX32(tb_flags, TBFLAG_A64, UNPRIV);
40f860cd
PM
14317 dc->vec_len = 0;
14318 dc->vec_stride = 0;
5c039906 14319 dc->cp_regs = arm_cpu->cp_regs;
a984e42c 14320 dc->features = env->features;
40f860cd 14321
7ea47fe7
PM
14322 /* Single step state. The code-generation logic here is:
14323 * SS_ACTIVE == 0:
14324 * generate code with no special handling for single-stepping (except
14325 * that anything that can make us go to SS_ACTIVE == 1 must end the TB;
14326 * this happens anyway because those changes are all system register or
14327 * PSTATE writes).
14328 * SS_ACTIVE == 1, PSTATE.SS == 1: (active-not-pending)
14329 * emit code for one insn
14330 * emit code to clear PSTATE.SS
14331 * emit code to generate software step exception for completed step
14332 * end TB (as usual for having generated an exception)
14333 * SS_ACTIVE == 1, PSTATE.SS == 0: (active-pending)
14334 * emit code to generate a software step exception
14335 * end the TB
14336 */
aad821ac
RH
14337 dc->ss_active = FIELD_EX32(tb_flags, TBFLAG_ANY, SS_ACTIVE);
14338 dc->pstate_ss = FIELD_EX32(tb_flags, TBFLAG_ANY, PSTATE_SS);
7ea47fe7 14339 dc->is_ldex = false;
8bd587c1 14340 dc->debug_target_el = FIELD_EX32(tb_flags, TBFLAG_ANY, DEBUG_TARGET_EL);
7ea47fe7 14341
dcc3a212
RH
14342 /* Bound the number of insns to execute to those left on the page. */
14343 bound = -(dc->base.pc_first | TARGET_PAGE_MASK) / 4;
14344
14345 /* If architectural single step active, limit to 1. */
14346 if (dc->ss_active) {
14347 bound = 1;
14348 }
b542683d 14349 dc->base.max_insns = MIN(dc->base.max_insns, bound);
24299c89 14350
11e169de 14351 init_tmp_a64_array(dc);
5c039906
LV
14352}
14353
23169224
LV
14354static void aarch64_tr_tb_start(DisasContextBase *db, CPUState *cpu)
14355{
23169224
LV
14356}
14357
a68956ad
LV
14358static void aarch64_tr_insn_start(DisasContextBase *dcbase, CPUState *cpu)
14359{
14360 DisasContext *dc = container_of(dcbase, DisasContext, base);
14361
a0415916 14362 tcg_gen_insn_start(dc->base.pc_next, 0, 0);
15fa08f8 14363 dc->insn_start = tcg_last_op();
a68956ad
LV
14364}
14365
0cb56b37
LV
14366static bool aarch64_tr_breakpoint_check(DisasContextBase *dcbase, CPUState *cpu,
14367 const CPUBreakpoint *bp)
14368{
14369 DisasContext *dc = container_of(dcbase, DisasContext, base);
14370
14371 if (bp->flags & BP_CPU) {
a0415916 14372 gen_a64_set_pc_im(dc->base.pc_next);
0cb56b37
LV
14373 gen_helper_check_breakpoints(cpu_env);
14374 /* End the TB early; it likely won't be executed */
14375 dc->base.is_jmp = DISAS_TOO_MANY;
14376 } else {
aee828e7 14377 gen_exception_internal_insn(dc, dc->base.pc_next, EXCP_DEBUG);
0cb56b37
LV
14378 /* The address covered by the breakpoint must be
14379 included in [tb->pc, tb->pc + tb->size) in order
14380 to for it to be properly cleared -- thus we
14381 increment the PC here so that the logic setting
14382 tb->size below does the right thing. */
a0415916 14383 dc->base.pc_next += 4;
0cb56b37
LV
14384 dc->base.is_jmp = DISAS_NORETURN;
14385 }
14386
14387 return true;
14388}
14389
24299c89
LV
14390static void aarch64_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)
14391{
14392 DisasContext *dc = container_of(dcbase, DisasContext, base);
14393 CPUARMState *env = cpu->env_ptr;
14394
14395 if (dc->ss_active && !dc->pstate_ss) {
14396 /* Singlestep state is Active-pending.
14397 * If we're in this state at the start of a TB then either
14398 * a) we just took an exception to an EL which is being debugged
14399 * and this is the first insn in the exception handler
14400 * b) debug exceptions were masked and we just unmasked them
14401 * without changing EL (eg by clearing PSTATE.D)
14402 * In either case we're going to take a swstep exception in the
14403 * "did not step an insn" case, and so the syndrome ISV and EX
14404 * bits should be zero.
14405 */
14406 assert(dc->base.num_insns == 1);
c1d5f50f 14407 gen_swstep_exception(dc, 0, 0);
24299c89
LV
14408 dc->base.is_jmp = DISAS_NORETURN;
14409 } else {
14410 disas_a64_insn(env, dc);
14411 }
14412
23169224 14413 translator_loop_temp_check(&dc->base);
24299c89
LV
14414}
14415
be407964
LV
14416static void aarch64_tr_tb_stop(DisasContextBase *dcbase, CPUState *cpu)
14417{
14418 DisasContext *dc = container_of(dcbase, DisasContext, base);
14419
14420 if (unlikely(dc->base.singlestep_enabled || dc->ss_active)) {
14421 /* Note that this means single stepping WFI doesn't halt the CPU.
14422 * For conditional branch insns this is harmless unreachable code as
14423 * gen_goto_tb() has already handled emitting the debug exception
14424 * (and thus a tb-jump is not possible when singlestepping).
14425 */
14426 switch (dc->base.is_jmp) {
14427 default:
a0415916 14428 gen_a64_set_pc_im(dc->base.pc_next);
be407964 14429 /* fall through */
dddbba99 14430 case DISAS_EXIT:
be407964
LV
14431 case DISAS_JUMP:
14432 if (dc->base.singlestep_enabled) {
14433 gen_exception_internal(EXCP_DEBUG);
14434 } else {
14435 gen_step_complete_exception(dc);
14436 }
14437 break;
14438 case DISAS_NORETURN:
14439 break;
14440 }
14441 } else {
14442 switch (dc->base.is_jmp) {
14443 case DISAS_NEXT:
14444 case DISAS_TOO_MANY:
a0415916 14445 gen_goto_tb(dc, 1, dc->base.pc_next);
be407964
LV
14446 break;
14447 default:
14448 case DISAS_UPDATE:
a0415916 14449 gen_a64_set_pc_im(dc->base.pc_next);
be407964 14450 /* fall through */
be407964 14451 case DISAS_EXIT:
07ea28b4 14452 tcg_gen_exit_tb(NULL, 0);
be407964 14453 break;
a75a52d6
VK
14454 case DISAS_JUMP:
14455 tcg_gen_lookup_and_goto_ptr();
14456 break;
be407964
LV
14457 case DISAS_NORETURN:
14458 case DISAS_SWI:
14459 break;
14460 case DISAS_WFE:
a0415916 14461 gen_a64_set_pc_im(dc->base.pc_next);
be407964
LV
14462 gen_helper_wfe(cpu_env);
14463 break;
14464 case DISAS_YIELD:
a0415916 14465 gen_a64_set_pc_im(dc->base.pc_next);
be407964
LV
14466 gen_helper_yield(cpu_env);
14467 break;
14468 case DISAS_WFI:
58803318 14469 {
be407964
LV
14470 /* This is a special case because we don't want to just halt the CPU
14471 * if trying to debug across a WFI.
14472 */
58803318
SS
14473 TCGv_i32 tmp = tcg_const_i32(4);
14474
a0415916 14475 gen_a64_set_pc_im(dc->base.pc_next);
58803318
SS
14476 gen_helper_wfi(cpu_env, tmp);
14477 tcg_temp_free_i32(tmp);
be407964
LV
14478 /* The helper doesn't necessarily throw an exception, but we
14479 * must go back to the main loop to check for interrupts anyway.
14480 */
07ea28b4 14481 tcg_gen_exit_tb(NULL, 0);
be407964
LV
14482 break;
14483 }
58803318 14484 }
be407964
LV
14485 }
14486}
14487
58350fa4
LV
14488static void aarch64_tr_disas_log(const DisasContextBase *dcbase,
14489 CPUState *cpu)
14490{
14491 DisasContext *dc = container_of(dcbase, DisasContext, base);
14492
14493 qemu_log("IN: %s\n", lookup_symbol(dc->base.pc_first));
1d48474d 14494 log_target_disas(cpu, dc->base.pc_first, dc->base.tb->size);
58350fa4
LV
14495}
14496
23169224
LV
14497const TranslatorOps aarch64_translator_ops = {
14498 .init_disas_context = aarch64_tr_init_disas_context,
14499 .tb_start = aarch64_tr_tb_start,
14500 .insn_start = aarch64_tr_insn_start,
14501 .breakpoint_check = aarch64_tr_breakpoint_check,
14502 .translate_insn = aarch64_tr_translate_insn,
14503 .tb_stop = aarch64_tr_tb_stop,
14504 .disas_log = aarch64_tr_disas_log,
14505};