Merge remote-tracking branch 'remotes/kraxel/tags/pull-input-20160928-1' into staging
[qemu/kevin.git] / target-arm / translate-a64.c
blobddf52f5e7967173cf1bd915afbba4faf51722577
1 /*
2 * AArch64 translation
4 * Copyright (c) 2013 Alexander Graf <agraf@suse.de>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 #include "qemu/osdep.h"
21 #include "cpu.h"
22 #include "exec/exec-all.h"
23 #include "tcg-op.h"
24 #include "qemu/log.h"
25 #include "arm_ldst.h"
26 #include "translate.h"
27 #include "internals.h"
28 #include "qemu/host-utils.h"
30 #include "exec/semihost.h"
31 #include "exec/gen-icount.h"
33 #include "exec/helper-proto.h"
34 #include "exec/helper-gen.h"
35 #include "exec/log.h"
37 #include "trace-tcg.h"
39 static TCGv_i64 cpu_X[32];
40 static TCGv_i64 cpu_pc;
42 /* Load/store exclusive handling */
43 static TCGv_i64 cpu_exclusive_high;
45 static const char *regnames[] = {
46 "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7",
47 "x8", "x9", "x10", "x11", "x12", "x13", "x14", "x15",
48 "x16", "x17", "x18", "x19", "x20", "x21", "x22", "x23",
49 "x24", "x25", "x26", "x27", "x28", "x29", "lr", "sp"
52 enum a64_shift_type {
53 A64_SHIFT_TYPE_LSL = 0,
54 A64_SHIFT_TYPE_LSR = 1,
55 A64_SHIFT_TYPE_ASR = 2,
56 A64_SHIFT_TYPE_ROR = 3
59 /* Table based decoder typedefs - used when the relevant bits for decode
60 * are too awkwardly scattered across the instruction (eg SIMD).
62 typedef void AArch64DecodeFn(DisasContext *s, uint32_t insn);
64 typedef struct AArch64DecodeTable {
65 uint32_t pattern;
66 uint32_t mask;
67 AArch64DecodeFn *disas_fn;
68 } AArch64DecodeTable;
70 /* Function prototype for gen_ functions for calling Neon helpers */
71 typedef void NeonGenOneOpEnvFn(TCGv_i32, TCGv_ptr, TCGv_i32);
72 typedef void NeonGenTwoOpFn(TCGv_i32, TCGv_i32, TCGv_i32);
73 typedef void NeonGenTwoOpEnvFn(TCGv_i32, TCGv_ptr, TCGv_i32, TCGv_i32);
74 typedef void NeonGenTwo64OpFn(TCGv_i64, TCGv_i64, TCGv_i64);
75 typedef void NeonGenTwo64OpEnvFn(TCGv_i64, TCGv_ptr, TCGv_i64, TCGv_i64);
76 typedef void NeonGenNarrowFn(TCGv_i32, TCGv_i64);
77 typedef void NeonGenNarrowEnvFn(TCGv_i32, TCGv_ptr, TCGv_i64);
78 typedef void NeonGenWidenFn(TCGv_i64, TCGv_i32);
79 typedef void NeonGenTwoSingleOPFn(TCGv_i32, TCGv_i32, TCGv_i32, TCGv_ptr);
80 typedef void NeonGenTwoDoubleOPFn(TCGv_i64, TCGv_i64, TCGv_i64, TCGv_ptr);
81 typedef void NeonGenOneOpFn(TCGv_i64, TCGv_i64);
82 typedef void CryptoTwoOpEnvFn(TCGv_ptr, TCGv_i32, TCGv_i32);
83 typedef void CryptoThreeOpEnvFn(TCGv_ptr, TCGv_i32, TCGv_i32, TCGv_i32);
85 /* initialize TCG globals. */
86 void a64_translate_init(void)
88 int i;
90 cpu_pc = tcg_global_mem_new_i64(cpu_env,
91 offsetof(CPUARMState, pc),
92 "pc");
93 for (i = 0; i < 32; i++) {
94 cpu_X[i] = tcg_global_mem_new_i64(cpu_env,
95 offsetof(CPUARMState, xregs[i]),
96 regnames[i]);
99 cpu_exclusive_high = tcg_global_mem_new_i64(cpu_env,
100 offsetof(CPUARMState, exclusive_high), "exclusive_high");
103 static inline ARMMMUIdx get_a64_user_mem_index(DisasContext *s)
105 /* Return the mmu_idx to use for A64 "unprivileged load/store" insns:
106 * if EL1, access as if EL0; otherwise access at current EL
108 switch (s->mmu_idx) {
109 case ARMMMUIdx_S12NSE1:
110 return ARMMMUIdx_S12NSE0;
111 case ARMMMUIdx_S1SE1:
112 return ARMMMUIdx_S1SE0;
113 case ARMMMUIdx_S2NS:
114 g_assert_not_reached();
115 default:
116 return s->mmu_idx;
120 void aarch64_cpu_dump_state(CPUState *cs, FILE *f,
121 fprintf_function cpu_fprintf, int flags)
123 ARMCPU *cpu = ARM_CPU(cs);
124 CPUARMState *env = &cpu->env;
125 uint32_t psr = pstate_read(env);
126 int i;
127 int el = arm_current_el(env);
128 const char *ns_status;
130 cpu_fprintf(f, "PC=%016"PRIx64" SP=%016"PRIx64"\n",
131 env->pc, env->xregs[31]);
132 for (i = 0; i < 31; i++) {
133 cpu_fprintf(f, "X%02d=%016"PRIx64, i, env->xregs[i]);
134 if ((i % 4) == 3) {
135 cpu_fprintf(f, "\n");
136 } else {
137 cpu_fprintf(f, " ");
141 if (arm_feature(env, ARM_FEATURE_EL3) && el != 3) {
142 ns_status = env->cp15.scr_el3 & SCR_NS ? "NS " : "S ";
143 } else {
144 ns_status = "";
147 cpu_fprintf(f, "\nPSTATE=%08x %c%c%c%c %sEL%d%c\n",
148 psr,
149 psr & PSTATE_N ? 'N' : '-',
150 psr & PSTATE_Z ? 'Z' : '-',
151 psr & PSTATE_C ? 'C' : '-',
152 psr & PSTATE_V ? 'V' : '-',
153 ns_status,
155 psr & PSTATE_SP ? 'h' : 't');
157 if (flags & CPU_DUMP_FPU) {
158 int numvfpregs = 32;
159 for (i = 0; i < numvfpregs; i += 2) {
160 uint64_t vlo = float64_val(env->vfp.regs[i * 2]);
161 uint64_t vhi = float64_val(env->vfp.regs[(i * 2) + 1]);
162 cpu_fprintf(f, "q%02d=%016" PRIx64 ":%016" PRIx64 " ",
163 i, vhi, vlo);
164 vlo = float64_val(env->vfp.regs[(i + 1) * 2]);
165 vhi = float64_val(env->vfp.regs[((i + 1) * 2) + 1]);
166 cpu_fprintf(f, "q%02d=%016" PRIx64 ":%016" PRIx64 "\n",
167 i + 1, vhi, vlo);
169 cpu_fprintf(f, "FPCR: %08x FPSR: %08x\n",
170 vfp_get_fpcr(env), vfp_get_fpsr(env));
174 void gen_a64_set_pc_im(uint64_t val)
176 tcg_gen_movi_i64(cpu_pc, val);
179 typedef struct DisasCompare64 {
180 TCGCond cond;
181 TCGv_i64 value;
182 } DisasCompare64;
184 static void a64_test_cc(DisasCompare64 *c64, int cc)
186 DisasCompare c32;
188 arm_test_cc(&c32, cc);
190 /* Sign-extend the 32-bit value so that the GE/LT comparisons work
191 * properly. The NE/EQ comparisons are also fine with this choice. */
192 c64->cond = c32.cond;
193 c64->value = tcg_temp_new_i64();
194 tcg_gen_ext_i32_i64(c64->value, c32.value);
196 arm_free_cc(&c32);
199 static void a64_free_cc(DisasCompare64 *c64)
201 tcg_temp_free_i64(c64->value);
204 static void gen_exception_internal(int excp)
206 TCGv_i32 tcg_excp = tcg_const_i32(excp);
208 assert(excp_is_internal(excp));
209 gen_helper_exception_internal(cpu_env, tcg_excp);
210 tcg_temp_free_i32(tcg_excp);
213 static void gen_exception(int excp, uint32_t syndrome, uint32_t target_el)
215 TCGv_i32 tcg_excp = tcg_const_i32(excp);
216 TCGv_i32 tcg_syn = tcg_const_i32(syndrome);
217 TCGv_i32 tcg_el = tcg_const_i32(target_el);
219 gen_helper_exception_with_syndrome(cpu_env, tcg_excp,
220 tcg_syn, tcg_el);
221 tcg_temp_free_i32(tcg_el);
222 tcg_temp_free_i32(tcg_syn);
223 tcg_temp_free_i32(tcg_excp);
226 static void gen_exception_internal_insn(DisasContext *s, int offset, int excp)
228 gen_a64_set_pc_im(s->pc - offset);
229 gen_exception_internal(excp);
230 s->is_jmp = DISAS_EXC;
233 static void gen_exception_insn(DisasContext *s, int offset, int excp,
234 uint32_t syndrome, uint32_t target_el)
236 gen_a64_set_pc_im(s->pc - offset);
237 gen_exception(excp, syndrome, target_el);
238 s->is_jmp = DISAS_EXC;
241 static void gen_ss_advance(DisasContext *s)
243 /* If the singlestep state is Active-not-pending, advance to
244 * Active-pending.
246 if (s->ss_active) {
247 s->pstate_ss = 0;
248 gen_helper_clear_pstate_ss(cpu_env);
252 static void gen_step_complete_exception(DisasContext *s)
254 /* We just completed step of an insn. Move from Active-not-pending
255 * to Active-pending, and then also take the swstep exception.
256 * This corresponds to making the (IMPDEF) choice to prioritize
257 * swstep exceptions over asynchronous exceptions taken to an exception
258 * level where debug is disabled. This choice has the advantage that
259 * we do not need to maintain internal state corresponding to the
260 * ISV/EX syndrome bits between completion of the step and generation
261 * of the exception, and our syndrome information is always correct.
263 gen_ss_advance(s);
264 gen_exception(EXCP_UDEF, syn_swstep(s->ss_same_el, 1, s->is_ldex),
265 default_exception_el(s));
266 s->is_jmp = DISAS_EXC;
269 static inline bool use_goto_tb(DisasContext *s, int n, uint64_t dest)
271 /* No direct tb linking with singlestep (either QEMU's or the ARM
272 * debug architecture kind) or deterministic io
274 if (s->singlestep_enabled || s->ss_active || (s->tb->cflags & CF_LAST_IO)) {
275 return false;
278 #ifndef CONFIG_USER_ONLY
279 /* Only link tbs from inside the same guest page */
280 if ((s->tb->pc & TARGET_PAGE_MASK) != (dest & TARGET_PAGE_MASK)) {
281 return false;
283 #endif
285 return true;
288 static inline void gen_goto_tb(DisasContext *s, int n, uint64_t dest)
290 TranslationBlock *tb;
292 tb = s->tb;
293 if (use_goto_tb(s, n, dest)) {
294 tcg_gen_goto_tb(n);
295 gen_a64_set_pc_im(dest);
296 tcg_gen_exit_tb((intptr_t)tb + n);
297 s->is_jmp = DISAS_TB_JUMP;
298 } else {
299 gen_a64_set_pc_im(dest);
300 if (s->ss_active) {
301 gen_step_complete_exception(s);
302 } else if (s->singlestep_enabled) {
303 gen_exception_internal(EXCP_DEBUG);
304 } else {
305 tcg_gen_exit_tb(0);
306 s->is_jmp = DISAS_TB_JUMP;
311 static void disas_set_insn_syndrome(DisasContext *s, uint32_t syn)
313 /* We don't need to save all of the syndrome so we mask and shift
314 * out uneeded bits to help the sleb128 encoder do a better job.
316 syn &= ARM_INSN_START_WORD2_MASK;
317 syn >>= ARM_INSN_START_WORD2_SHIFT;
319 /* We check and clear insn_start_idx to catch multiple updates. */
320 assert(s->insn_start_idx != 0);
321 tcg_set_insn_param(s->insn_start_idx, 2, syn);
322 s->insn_start_idx = 0;
325 static void unallocated_encoding(DisasContext *s)
327 /* Unallocated and reserved encodings are uncategorized */
328 gen_exception_insn(s, 4, EXCP_UDEF, syn_uncategorized(),
329 default_exception_el(s));
332 #define unsupported_encoding(s, insn) \
333 do { \
334 qemu_log_mask(LOG_UNIMP, \
335 "%s:%d: unsupported instruction encoding 0x%08x " \
336 "at pc=%016" PRIx64 "\n", \
337 __FILE__, __LINE__, insn, s->pc - 4); \
338 unallocated_encoding(s); \
339 } while (0);
341 static void init_tmp_a64_array(DisasContext *s)
343 #ifdef CONFIG_DEBUG_TCG
344 int i;
345 for (i = 0; i < ARRAY_SIZE(s->tmp_a64); i++) {
346 TCGV_UNUSED_I64(s->tmp_a64[i]);
348 #endif
349 s->tmp_a64_count = 0;
352 static void free_tmp_a64(DisasContext *s)
354 int i;
355 for (i = 0; i < s->tmp_a64_count; i++) {
356 tcg_temp_free_i64(s->tmp_a64[i]);
358 init_tmp_a64_array(s);
361 static TCGv_i64 new_tmp_a64(DisasContext *s)
363 assert(s->tmp_a64_count < TMP_A64_MAX);
364 return s->tmp_a64[s->tmp_a64_count++] = tcg_temp_new_i64();
367 static TCGv_i64 new_tmp_a64_zero(DisasContext *s)
369 TCGv_i64 t = new_tmp_a64(s);
370 tcg_gen_movi_i64(t, 0);
371 return t;
375 * Register access functions
377 * These functions are used for directly accessing a register in where
378 * changes to the final register value are likely to be made. If you
379 * need to use a register for temporary calculation (e.g. index type
380 * operations) use the read_* form.
382 * B1.2.1 Register mappings
384 * In instruction register encoding 31 can refer to ZR (zero register) or
385 * the SP (stack pointer) depending on context. In QEMU's case we map SP
386 * to cpu_X[31] and ZR accesses to a temporary which can be discarded.
387 * This is the point of the _sp forms.
389 static TCGv_i64 cpu_reg(DisasContext *s, int reg)
391 if (reg == 31) {
392 return new_tmp_a64_zero(s);
393 } else {
394 return cpu_X[reg];
398 /* register access for when 31 == SP */
399 static TCGv_i64 cpu_reg_sp(DisasContext *s, int reg)
401 return cpu_X[reg];
404 /* read a cpu register in 32bit/64bit mode. Returns a TCGv_i64
405 * representing the register contents. This TCGv is an auto-freed
406 * temporary so it need not be explicitly freed, and may be modified.
408 static TCGv_i64 read_cpu_reg(DisasContext *s, int reg, int sf)
410 TCGv_i64 v = new_tmp_a64(s);
411 if (reg != 31) {
412 if (sf) {
413 tcg_gen_mov_i64(v, cpu_X[reg]);
414 } else {
415 tcg_gen_ext32u_i64(v, cpu_X[reg]);
417 } else {
418 tcg_gen_movi_i64(v, 0);
420 return v;
423 static TCGv_i64 read_cpu_reg_sp(DisasContext *s, int reg, int sf)
425 TCGv_i64 v = new_tmp_a64(s);
426 if (sf) {
427 tcg_gen_mov_i64(v, cpu_X[reg]);
428 } else {
429 tcg_gen_ext32u_i64(v, cpu_X[reg]);
431 return v;
434 /* We should have at some point before trying to access an FP register
435 * done the necessary access check, so assert that
436 * (a) we did the check and
437 * (b) we didn't then just plough ahead anyway if it failed.
438 * Print the instruction pattern in the abort message so we can figure
439 * out what we need to fix if a user encounters this problem in the wild.
441 static inline void assert_fp_access_checked(DisasContext *s)
443 #ifdef CONFIG_DEBUG_TCG
444 if (unlikely(!s->fp_access_checked || s->fp_excp_el)) {
445 fprintf(stderr, "target-arm: FP access check missing for "
446 "instruction 0x%08x\n", s->insn);
447 abort();
449 #endif
452 /* Return the offset into CPUARMState of an element of specified
453 * size, 'element' places in from the least significant end of
454 * the FP/vector register Qn.
456 static inline int vec_reg_offset(DisasContext *s, int regno,
457 int element, TCGMemOp size)
459 int offs = offsetof(CPUARMState, vfp.regs[regno * 2]);
460 #ifdef HOST_WORDS_BIGENDIAN
461 /* This is complicated slightly because vfp.regs[2n] is
462 * still the low half and vfp.regs[2n+1] the high half
463 * of the 128 bit vector, even on big endian systems.
464 * Calculate the offset assuming a fully bigendian 128 bits,
465 * then XOR to account for the order of the two 64 bit halves.
467 offs += (16 - ((element + 1) * (1 << size)));
468 offs ^= 8;
469 #else
470 offs += element * (1 << size);
471 #endif
472 assert_fp_access_checked(s);
473 return offs;
476 /* Return the offset into CPUARMState of a slice (from
477 * the least significant end) of FP register Qn (ie
478 * Dn, Sn, Hn or Bn).
479 * (Note that this is not the same mapping as for A32; see cpu.h)
481 static inline int fp_reg_offset(DisasContext *s, int regno, TCGMemOp size)
483 int offs = offsetof(CPUARMState, vfp.regs[regno * 2]);
484 #ifdef HOST_WORDS_BIGENDIAN
485 offs += (8 - (1 << size));
486 #endif
487 assert_fp_access_checked(s);
488 return offs;
491 /* Offset of the high half of the 128 bit vector Qn */
492 static inline int fp_reg_hi_offset(DisasContext *s, int regno)
494 assert_fp_access_checked(s);
495 return offsetof(CPUARMState, vfp.regs[regno * 2 + 1]);
498 /* Convenience accessors for reading and writing single and double
499 * FP registers. Writing clears the upper parts of the associated
500 * 128 bit vector register, as required by the architecture.
501 * Note that unlike the GP register accessors, the values returned
502 * by the read functions must be manually freed.
504 static TCGv_i64 read_fp_dreg(DisasContext *s, int reg)
506 TCGv_i64 v = tcg_temp_new_i64();
508 tcg_gen_ld_i64(v, cpu_env, fp_reg_offset(s, reg, MO_64));
509 return v;
512 static TCGv_i32 read_fp_sreg(DisasContext *s, int reg)
514 TCGv_i32 v = tcg_temp_new_i32();
516 tcg_gen_ld_i32(v, cpu_env, fp_reg_offset(s, reg, MO_32));
517 return v;
520 static void write_fp_dreg(DisasContext *s, int reg, TCGv_i64 v)
522 TCGv_i64 tcg_zero = tcg_const_i64(0);
524 tcg_gen_st_i64(v, cpu_env, fp_reg_offset(s, reg, MO_64));
525 tcg_gen_st_i64(tcg_zero, cpu_env, fp_reg_hi_offset(s, reg));
526 tcg_temp_free_i64(tcg_zero);
529 static void write_fp_sreg(DisasContext *s, int reg, TCGv_i32 v)
531 TCGv_i64 tmp = tcg_temp_new_i64();
533 tcg_gen_extu_i32_i64(tmp, v);
534 write_fp_dreg(s, reg, tmp);
535 tcg_temp_free_i64(tmp);
538 static TCGv_ptr get_fpstatus_ptr(void)
540 TCGv_ptr statusptr = tcg_temp_new_ptr();
541 int offset;
543 /* In A64 all instructions (both FP and Neon) use the FPCR;
544 * there is no equivalent of the A32 Neon "standard FPSCR value"
545 * and all operations use vfp.fp_status.
547 offset = offsetof(CPUARMState, vfp.fp_status);
548 tcg_gen_addi_ptr(statusptr, cpu_env, offset);
549 return statusptr;
552 /* Set ZF and NF based on a 64 bit result. This is alas fiddlier
553 * than the 32 bit equivalent.
555 static inline void gen_set_NZ64(TCGv_i64 result)
557 tcg_gen_extr_i64_i32(cpu_ZF, cpu_NF, result);
558 tcg_gen_or_i32(cpu_ZF, cpu_ZF, cpu_NF);
561 /* Set NZCV as for a logical operation: NZ as per result, CV cleared. */
562 static inline void gen_logic_CC(int sf, TCGv_i64 result)
564 if (sf) {
565 gen_set_NZ64(result);
566 } else {
567 tcg_gen_extrl_i64_i32(cpu_ZF, result);
568 tcg_gen_mov_i32(cpu_NF, cpu_ZF);
570 tcg_gen_movi_i32(cpu_CF, 0);
571 tcg_gen_movi_i32(cpu_VF, 0);
574 /* dest = T0 + T1; compute C, N, V and Z flags */
575 static void gen_add_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
577 if (sf) {
578 TCGv_i64 result, flag, tmp;
579 result = tcg_temp_new_i64();
580 flag = tcg_temp_new_i64();
581 tmp = tcg_temp_new_i64();
583 tcg_gen_movi_i64(tmp, 0);
584 tcg_gen_add2_i64(result, flag, t0, tmp, t1, tmp);
586 tcg_gen_extrl_i64_i32(cpu_CF, flag);
588 gen_set_NZ64(result);
590 tcg_gen_xor_i64(flag, result, t0);
591 tcg_gen_xor_i64(tmp, t0, t1);
592 tcg_gen_andc_i64(flag, flag, tmp);
593 tcg_temp_free_i64(tmp);
594 tcg_gen_extrh_i64_i32(cpu_VF, flag);
596 tcg_gen_mov_i64(dest, result);
597 tcg_temp_free_i64(result);
598 tcg_temp_free_i64(flag);
599 } else {
600 /* 32 bit arithmetic */
601 TCGv_i32 t0_32 = tcg_temp_new_i32();
602 TCGv_i32 t1_32 = tcg_temp_new_i32();
603 TCGv_i32 tmp = tcg_temp_new_i32();
605 tcg_gen_movi_i32(tmp, 0);
606 tcg_gen_extrl_i64_i32(t0_32, t0);
607 tcg_gen_extrl_i64_i32(t1_32, t1);
608 tcg_gen_add2_i32(cpu_NF, cpu_CF, t0_32, tmp, t1_32, tmp);
609 tcg_gen_mov_i32(cpu_ZF, cpu_NF);
610 tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
611 tcg_gen_xor_i32(tmp, t0_32, t1_32);
612 tcg_gen_andc_i32(cpu_VF, cpu_VF, tmp);
613 tcg_gen_extu_i32_i64(dest, cpu_NF);
615 tcg_temp_free_i32(tmp);
616 tcg_temp_free_i32(t0_32);
617 tcg_temp_free_i32(t1_32);
621 /* dest = T0 - T1; compute C, N, V and Z flags */
622 static void gen_sub_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
624 if (sf) {
625 /* 64 bit arithmetic */
626 TCGv_i64 result, flag, tmp;
628 result = tcg_temp_new_i64();
629 flag = tcg_temp_new_i64();
630 tcg_gen_sub_i64(result, t0, t1);
632 gen_set_NZ64(result);
634 tcg_gen_setcond_i64(TCG_COND_GEU, flag, t0, t1);
635 tcg_gen_extrl_i64_i32(cpu_CF, flag);
637 tcg_gen_xor_i64(flag, result, t0);
638 tmp = tcg_temp_new_i64();
639 tcg_gen_xor_i64(tmp, t0, t1);
640 tcg_gen_and_i64(flag, flag, tmp);
641 tcg_temp_free_i64(tmp);
642 tcg_gen_extrh_i64_i32(cpu_VF, flag);
643 tcg_gen_mov_i64(dest, result);
644 tcg_temp_free_i64(flag);
645 tcg_temp_free_i64(result);
646 } else {
647 /* 32 bit arithmetic */
648 TCGv_i32 t0_32 = tcg_temp_new_i32();
649 TCGv_i32 t1_32 = tcg_temp_new_i32();
650 TCGv_i32 tmp;
652 tcg_gen_extrl_i64_i32(t0_32, t0);
653 tcg_gen_extrl_i64_i32(t1_32, t1);
654 tcg_gen_sub_i32(cpu_NF, t0_32, t1_32);
655 tcg_gen_mov_i32(cpu_ZF, cpu_NF);
656 tcg_gen_setcond_i32(TCG_COND_GEU, cpu_CF, t0_32, t1_32);
657 tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
658 tmp = tcg_temp_new_i32();
659 tcg_gen_xor_i32(tmp, t0_32, t1_32);
660 tcg_temp_free_i32(t0_32);
661 tcg_temp_free_i32(t1_32);
662 tcg_gen_and_i32(cpu_VF, cpu_VF, tmp);
663 tcg_temp_free_i32(tmp);
664 tcg_gen_extu_i32_i64(dest, cpu_NF);
668 /* dest = T0 + T1 + CF; do not compute flags. */
669 static void gen_adc(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
671 TCGv_i64 flag = tcg_temp_new_i64();
672 tcg_gen_extu_i32_i64(flag, cpu_CF);
673 tcg_gen_add_i64(dest, t0, t1);
674 tcg_gen_add_i64(dest, dest, flag);
675 tcg_temp_free_i64(flag);
677 if (!sf) {
678 tcg_gen_ext32u_i64(dest, dest);
682 /* dest = T0 + T1 + CF; compute C, N, V and Z flags. */
683 static void gen_adc_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
685 if (sf) {
686 TCGv_i64 result, cf_64, vf_64, tmp;
687 result = tcg_temp_new_i64();
688 cf_64 = tcg_temp_new_i64();
689 vf_64 = tcg_temp_new_i64();
690 tmp = tcg_const_i64(0);
692 tcg_gen_extu_i32_i64(cf_64, cpu_CF);
693 tcg_gen_add2_i64(result, cf_64, t0, tmp, cf_64, tmp);
694 tcg_gen_add2_i64(result, cf_64, result, cf_64, t1, tmp);
695 tcg_gen_extrl_i64_i32(cpu_CF, cf_64);
696 gen_set_NZ64(result);
698 tcg_gen_xor_i64(vf_64, result, t0);
699 tcg_gen_xor_i64(tmp, t0, t1);
700 tcg_gen_andc_i64(vf_64, vf_64, tmp);
701 tcg_gen_extrh_i64_i32(cpu_VF, vf_64);
703 tcg_gen_mov_i64(dest, result);
705 tcg_temp_free_i64(tmp);
706 tcg_temp_free_i64(vf_64);
707 tcg_temp_free_i64(cf_64);
708 tcg_temp_free_i64(result);
709 } else {
710 TCGv_i32 t0_32, t1_32, tmp;
711 t0_32 = tcg_temp_new_i32();
712 t1_32 = tcg_temp_new_i32();
713 tmp = tcg_const_i32(0);
715 tcg_gen_extrl_i64_i32(t0_32, t0);
716 tcg_gen_extrl_i64_i32(t1_32, t1);
717 tcg_gen_add2_i32(cpu_NF, cpu_CF, t0_32, tmp, cpu_CF, tmp);
718 tcg_gen_add2_i32(cpu_NF, cpu_CF, cpu_NF, cpu_CF, t1_32, tmp);
720 tcg_gen_mov_i32(cpu_ZF, cpu_NF);
721 tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
722 tcg_gen_xor_i32(tmp, t0_32, t1_32);
723 tcg_gen_andc_i32(cpu_VF, cpu_VF, tmp);
724 tcg_gen_extu_i32_i64(dest, cpu_NF);
726 tcg_temp_free_i32(tmp);
727 tcg_temp_free_i32(t1_32);
728 tcg_temp_free_i32(t0_32);
733 * Load/Store generators
737 * Store from GPR register to memory.
739 static void do_gpr_st_memidx(DisasContext *s, TCGv_i64 source,
740 TCGv_i64 tcg_addr, int size, int memidx,
741 bool iss_valid,
742 unsigned int iss_srt,
743 bool iss_sf, bool iss_ar)
745 g_assert(size <= 3);
746 tcg_gen_qemu_st_i64(source, tcg_addr, memidx, s->be_data + size);
748 if (iss_valid) {
749 uint32_t syn;
751 syn = syn_data_abort_with_iss(0,
752 size,
753 false,
754 iss_srt,
755 iss_sf,
756 iss_ar,
757 0, 0, 0, 0, 0, false);
758 disas_set_insn_syndrome(s, syn);
762 static void do_gpr_st(DisasContext *s, TCGv_i64 source,
763 TCGv_i64 tcg_addr, int size,
764 bool iss_valid,
765 unsigned int iss_srt,
766 bool iss_sf, bool iss_ar)
768 do_gpr_st_memidx(s, source, tcg_addr, size, get_mem_index(s),
769 iss_valid, iss_srt, iss_sf, iss_ar);
773 * Load from memory to GPR register
775 static void do_gpr_ld_memidx(DisasContext *s,
776 TCGv_i64 dest, TCGv_i64 tcg_addr,
777 int size, bool is_signed,
778 bool extend, int memidx,
779 bool iss_valid, unsigned int iss_srt,
780 bool iss_sf, bool iss_ar)
782 TCGMemOp memop = s->be_data + size;
784 g_assert(size <= 3);
786 if (is_signed) {
787 memop += MO_SIGN;
790 tcg_gen_qemu_ld_i64(dest, tcg_addr, memidx, memop);
792 if (extend && is_signed) {
793 g_assert(size < 3);
794 tcg_gen_ext32u_i64(dest, dest);
797 if (iss_valid) {
798 uint32_t syn;
800 syn = syn_data_abort_with_iss(0,
801 size,
802 is_signed,
803 iss_srt,
804 iss_sf,
805 iss_ar,
806 0, 0, 0, 0, 0, false);
807 disas_set_insn_syndrome(s, syn);
811 static void do_gpr_ld(DisasContext *s,
812 TCGv_i64 dest, TCGv_i64 tcg_addr,
813 int size, bool is_signed, bool extend,
814 bool iss_valid, unsigned int iss_srt,
815 bool iss_sf, bool iss_ar)
817 do_gpr_ld_memidx(s, dest, tcg_addr, size, is_signed, extend,
818 get_mem_index(s),
819 iss_valid, iss_srt, iss_sf, iss_ar);
823 * Store from FP register to memory
825 static void do_fp_st(DisasContext *s, int srcidx, TCGv_i64 tcg_addr, int size)
827 /* This writes the bottom N bits of a 128 bit wide vector to memory */
828 TCGv_i64 tmp = tcg_temp_new_i64();
829 tcg_gen_ld_i64(tmp, cpu_env, fp_reg_offset(s, srcidx, MO_64));
830 if (size < 4) {
831 tcg_gen_qemu_st_i64(tmp, tcg_addr, get_mem_index(s),
832 s->be_data + size);
833 } else {
834 bool be = s->be_data == MO_BE;
835 TCGv_i64 tcg_hiaddr = tcg_temp_new_i64();
837 tcg_gen_addi_i64(tcg_hiaddr, tcg_addr, 8);
838 tcg_gen_qemu_st_i64(tmp, be ? tcg_hiaddr : tcg_addr, get_mem_index(s),
839 s->be_data | MO_Q);
840 tcg_gen_ld_i64(tmp, cpu_env, fp_reg_hi_offset(s, srcidx));
841 tcg_gen_qemu_st_i64(tmp, be ? tcg_addr : tcg_hiaddr, get_mem_index(s),
842 s->be_data | MO_Q);
843 tcg_temp_free_i64(tcg_hiaddr);
846 tcg_temp_free_i64(tmp);
850 * Load from memory to FP register
852 static void do_fp_ld(DisasContext *s, int destidx, TCGv_i64 tcg_addr, int size)
854 /* This always zero-extends and writes to a full 128 bit wide vector */
855 TCGv_i64 tmplo = tcg_temp_new_i64();
856 TCGv_i64 tmphi;
858 if (size < 4) {
859 TCGMemOp memop = s->be_data + size;
860 tmphi = tcg_const_i64(0);
861 tcg_gen_qemu_ld_i64(tmplo, tcg_addr, get_mem_index(s), memop);
862 } else {
863 bool be = s->be_data == MO_BE;
864 TCGv_i64 tcg_hiaddr;
866 tmphi = tcg_temp_new_i64();
867 tcg_hiaddr = tcg_temp_new_i64();
869 tcg_gen_addi_i64(tcg_hiaddr, tcg_addr, 8);
870 tcg_gen_qemu_ld_i64(tmplo, be ? tcg_hiaddr : tcg_addr, get_mem_index(s),
871 s->be_data | MO_Q);
872 tcg_gen_qemu_ld_i64(tmphi, be ? tcg_addr : tcg_hiaddr, get_mem_index(s),
873 s->be_data | MO_Q);
874 tcg_temp_free_i64(tcg_hiaddr);
877 tcg_gen_st_i64(tmplo, cpu_env, fp_reg_offset(s, destidx, MO_64));
878 tcg_gen_st_i64(tmphi, cpu_env, fp_reg_hi_offset(s, destidx));
880 tcg_temp_free_i64(tmplo);
881 tcg_temp_free_i64(tmphi);
885 * Vector load/store helpers.
887 * The principal difference between this and a FP load is that we don't
888 * zero extend as we are filling a partial chunk of the vector register.
889 * These functions don't support 128 bit loads/stores, which would be
890 * normal load/store operations.
892 * The _i32 versions are useful when operating on 32 bit quantities
893 * (eg for floating point single or using Neon helper functions).
896 /* Get value of an element within a vector register */
897 static void read_vec_element(DisasContext *s, TCGv_i64 tcg_dest, int srcidx,
898 int element, TCGMemOp memop)
900 int vect_off = vec_reg_offset(s, srcidx, element, memop & MO_SIZE);
901 switch (memop) {
902 case MO_8:
903 tcg_gen_ld8u_i64(tcg_dest, cpu_env, vect_off);
904 break;
905 case MO_16:
906 tcg_gen_ld16u_i64(tcg_dest, cpu_env, vect_off);
907 break;
908 case MO_32:
909 tcg_gen_ld32u_i64(tcg_dest, cpu_env, vect_off);
910 break;
911 case MO_8|MO_SIGN:
912 tcg_gen_ld8s_i64(tcg_dest, cpu_env, vect_off);
913 break;
914 case MO_16|MO_SIGN:
915 tcg_gen_ld16s_i64(tcg_dest, cpu_env, vect_off);
916 break;
917 case MO_32|MO_SIGN:
918 tcg_gen_ld32s_i64(tcg_dest, cpu_env, vect_off);
919 break;
920 case MO_64:
921 case MO_64|MO_SIGN:
922 tcg_gen_ld_i64(tcg_dest, cpu_env, vect_off);
923 break;
924 default:
925 g_assert_not_reached();
929 static void read_vec_element_i32(DisasContext *s, TCGv_i32 tcg_dest, int srcidx,
930 int element, TCGMemOp memop)
932 int vect_off = vec_reg_offset(s, srcidx, element, memop & MO_SIZE);
933 switch (memop) {
934 case MO_8:
935 tcg_gen_ld8u_i32(tcg_dest, cpu_env, vect_off);
936 break;
937 case MO_16:
938 tcg_gen_ld16u_i32(tcg_dest, cpu_env, vect_off);
939 break;
940 case MO_8|MO_SIGN:
941 tcg_gen_ld8s_i32(tcg_dest, cpu_env, vect_off);
942 break;
943 case MO_16|MO_SIGN:
944 tcg_gen_ld16s_i32(tcg_dest, cpu_env, vect_off);
945 break;
946 case MO_32:
947 case MO_32|MO_SIGN:
948 tcg_gen_ld_i32(tcg_dest, cpu_env, vect_off);
949 break;
950 default:
951 g_assert_not_reached();
955 /* Set value of an element within a vector register */
956 static void write_vec_element(DisasContext *s, TCGv_i64 tcg_src, int destidx,
957 int element, TCGMemOp memop)
959 int vect_off = vec_reg_offset(s, destidx, element, memop & MO_SIZE);
960 switch (memop) {
961 case MO_8:
962 tcg_gen_st8_i64(tcg_src, cpu_env, vect_off);
963 break;
964 case MO_16:
965 tcg_gen_st16_i64(tcg_src, cpu_env, vect_off);
966 break;
967 case MO_32:
968 tcg_gen_st32_i64(tcg_src, cpu_env, vect_off);
969 break;
970 case MO_64:
971 tcg_gen_st_i64(tcg_src, cpu_env, vect_off);
972 break;
973 default:
974 g_assert_not_reached();
978 static void write_vec_element_i32(DisasContext *s, TCGv_i32 tcg_src,
979 int destidx, int element, TCGMemOp memop)
981 int vect_off = vec_reg_offset(s, destidx, element, memop & MO_SIZE);
982 switch (memop) {
983 case MO_8:
984 tcg_gen_st8_i32(tcg_src, cpu_env, vect_off);
985 break;
986 case MO_16:
987 tcg_gen_st16_i32(tcg_src, cpu_env, vect_off);
988 break;
989 case MO_32:
990 tcg_gen_st_i32(tcg_src, cpu_env, vect_off);
991 break;
992 default:
993 g_assert_not_reached();
997 /* Clear the high 64 bits of a 128 bit vector (in general non-quad
998 * vector ops all need to do this).
1000 static void clear_vec_high(DisasContext *s, int rd)
1002 TCGv_i64 tcg_zero = tcg_const_i64(0);
1004 write_vec_element(s, tcg_zero, rd, 1, MO_64);
1005 tcg_temp_free_i64(tcg_zero);
1008 /* Store from vector register to memory */
1009 static void do_vec_st(DisasContext *s, int srcidx, int element,
1010 TCGv_i64 tcg_addr, int size)
1012 TCGMemOp memop = s->be_data + size;
1013 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
1015 read_vec_element(s, tcg_tmp, srcidx, element, size);
1016 tcg_gen_qemu_st_i64(tcg_tmp, tcg_addr, get_mem_index(s), memop);
1018 tcg_temp_free_i64(tcg_tmp);
1021 /* Load from memory to vector register */
1022 static void do_vec_ld(DisasContext *s, int destidx, int element,
1023 TCGv_i64 tcg_addr, int size)
1025 TCGMemOp memop = s->be_data + size;
1026 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
1028 tcg_gen_qemu_ld_i64(tcg_tmp, tcg_addr, get_mem_index(s), memop);
1029 write_vec_element(s, tcg_tmp, destidx, element, size);
1031 tcg_temp_free_i64(tcg_tmp);
1034 /* Check that FP/Neon access is enabled. If it is, return
1035 * true. If not, emit code to generate an appropriate exception,
1036 * and return false; the caller should not emit any code for
1037 * the instruction. Note that this check must happen after all
1038 * unallocated-encoding checks (otherwise the syndrome information
1039 * for the resulting exception will be incorrect).
1041 static inline bool fp_access_check(DisasContext *s)
1043 assert(!s->fp_access_checked);
1044 s->fp_access_checked = true;
1046 if (!s->fp_excp_el) {
1047 return true;
1050 gen_exception_insn(s, 4, EXCP_UDEF, syn_fp_access_trap(1, 0xe, false),
1051 s->fp_excp_el);
1052 return false;
1056 * This utility function is for doing register extension with an
1057 * optional shift. You will likely want to pass a temporary for the
1058 * destination register. See DecodeRegExtend() in the ARM ARM.
1060 static void ext_and_shift_reg(TCGv_i64 tcg_out, TCGv_i64 tcg_in,
1061 int option, unsigned int shift)
1063 int extsize = extract32(option, 0, 2);
1064 bool is_signed = extract32(option, 2, 1);
1066 if (is_signed) {
1067 switch (extsize) {
1068 case 0:
1069 tcg_gen_ext8s_i64(tcg_out, tcg_in);
1070 break;
1071 case 1:
1072 tcg_gen_ext16s_i64(tcg_out, tcg_in);
1073 break;
1074 case 2:
1075 tcg_gen_ext32s_i64(tcg_out, tcg_in);
1076 break;
1077 case 3:
1078 tcg_gen_mov_i64(tcg_out, tcg_in);
1079 break;
1081 } else {
1082 switch (extsize) {
1083 case 0:
1084 tcg_gen_ext8u_i64(tcg_out, tcg_in);
1085 break;
1086 case 1:
1087 tcg_gen_ext16u_i64(tcg_out, tcg_in);
1088 break;
1089 case 2:
1090 tcg_gen_ext32u_i64(tcg_out, tcg_in);
1091 break;
1092 case 3:
1093 tcg_gen_mov_i64(tcg_out, tcg_in);
1094 break;
1098 if (shift) {
1099 tcg_gen_shli_i64(tcg_out, tcg_out, shift);
1103 static inline void gen_check_sp_alignment(DisasContext *s)
1105 /* The AArch64 architecture mandates that (if enabled via PSTATE
1106 * or SCTLR bits) there is a check that SP is 16-aligned on every
1107 * SP-relative load or store (with an exception generated if it is not).
1108 * In line with general QEMU practice regarding misaligned accesses,
1109 * we omit these checks for the sake of guest program performance.
1110 * This function is provided as a hook so we can more easily add these
1111 * checks in future (possibly as a "favour catching guest program bugs
1112 * over speed" user selectable option).
1117 * This provides a simple table based table lookup decoder. It is
1118 * intended to be used when the relevant bits for decode are too
1119 * awkwardly placed and switch/if based logic would be confusing and
1120 * deeply nested. Since it's a linear search through the table, tables
1121 * should be kept small.
1123 * It returns the first handler where insn & mask == pattern, or
1124 * NULL if there is no match.
1125 * The table is terminated by an empty mask (i.e. 0)
1127 static inline AArch64DecodeFn *lookup_disas_fn(const AArch64DecodeTable *table,
1128 uint32_t insn)
1130 const AArch64DecodeTable *tptr = table;
1132 while (tptr->mask) {
1133 if ((insn & tptr->mask) == tptr->pattern) {
1134 return tptr->disas_fn;
1136 tptr++;
1138 return NULL;
1142 * the instruction disassembly implemented here matches
1143 * the instruction encoding classifications in chapter 3 (C3)
1144 * of the ARM Architecture Reference Manual (DDI0487A_a)
1147 /* C3.2.7 Unconditional branch (immediate)
1148 * 31 30 26 25 0
1149 * +----+-----------+-------------------------------------+
1150 * | op | 0 0 1 0 1 | imm26 |
1151 * +----+-----------+-------------------------------------+
1153 static void disas_uncond_b_imm(DisasContext *s, uint32_t insn)
1155 uint64_t addr = s->pc + sextract32(insn, 0, 26) * 4 - 4;
1157 if (insn & (1U << 31)) {
1158 /* C5.6.26 BL Branch with link */
1159 tcg_gen_movi_i64(cpu_reg(s, 30), s->pc);
1162 /* C5.6.20 B Branch / C5.6.26 BL Branch with link */
1163 gen_goto_tb(s, 0, addr);
1166 /* C3.2.1 Compare & branch (immediate)
1167 * 31 30 25 24 23 5 4 0
1168 * +----+-------------+----+---------------------+--------+
1169 * | sf | 0 1 1 0 1 0 | op | imm19 | Rt |
1170 * +----+-------------+----+---------------------+--------+
1172 static void disas_comp_b_imm(DisasContext *s, uint32_t insn)
1174 unsigned int sf, op, rt;
1175 uint64_t addr;
1176 TCGLabel *label_match;
1177 TCGv_i64 tcg_cmp;
1179 sf = extract32(insn, 31, 1);
1180 op = extract32(insn, 24, 1); /* 0: CBZ; 1: CBNZ */
1181 rt = extract32(insn, 0, 5);
1182 addr = s->pc + sextract32(insn, 5, 19) * 4 - 4;
1184 tcg_cmp = read_cpu_reg(s, rt, sf);
1185 label_match = gen_new_label();
1187 tcg_gen_brcondi_i64(op ? TCG_COND_NE : TCG_COND_EQ,
1188 tcg_cmp, 0, label_match);
1190 gen_goto_tb(s, 0, s->pc);
1191 gen_set_label(label_match);
1192 gen_goto_tb(s, 1, addr);
1195 /* C3.2.5 Test & branch (immediate)
1196 * 31 30 25 24 23 19 18 5 4 0
1197 * +----+-------------+----+-------+-------------+------+
1198 * | b5 | 0 1 1 0 1 1 | op | b40 | imm14 | Rt |
1199 * +----+-------------+----+-------+-------------+------+
1201 static void disas_test_b_imm(DisasContext *s, uint32_t insn)
1203 unsigned int bit_pos, op, rt;
1204 uint64_t addr;
1205 TCGLabel *label_match;
1206 TCGv_i64 tcg_cmp;
1208 bit_pos = (extract32(insn, 31, 1) << 5) | extract32(insn, 19, 5);
1209 op = extract32(insn, 24, 1); /* 0: TBZ; 1: TBNZ */
1210 addr = s->pc + sextract32(insn, 5, 14) * 4 - 4;
1211 rt = extract32(insn, 0, 5);
1213 tcg_cmp = tcg_temp_new_i64();
1214 tcg_gen_andi_i64(tcg_cmp, cpu_reg(s, rt), (1ULL << bit_pos));
1215 label_match = gen_new_label();
1216 tcg_gen_brcondi_i64(op ? TCG_COND_NE : TCG_COND_EQ,
1217 tcg_cmp, 0, label_match);
1218 tcg_temp_free_i64(tcg_cmp);
1219 gen_goto_tb(s, 0, s->pc);
1220 gen_set_label(label_match);
1221 gen_goto_tb(s, 1, addr);
1224 /* C3.2.2 / C5.6.19 Conditional branch (immediate)
1225 * 31 25 24 23 5 4 3 0
1226 * +---------------+----+---------------------+----+------+
1227 * | 0 1 0 1 0 1 0 | o1 | imm19 | o0 | cond |
1228 * +---------------+----+---------------------+----+------+
1230 static void disas_cond_b_imm(DisasContext *s, uint32_t insn)
1232 unsigned int cond;
1233 uint64_t addr;
1235 if ((insn & (1 << 4)) || (insn & (1 << 24))) {
1236 unallocated_encoding(s);
1237 return;
1239 addr = s->pc + sextract32(insn, 5, 19) * 4 - 4;
1240 cond = extract32(insn, 0, 4);
1242 if (cond < 0x0e) {
1243 /* genuinely conditional branches */
1244 TCGLabel *label_match = gen_new_label();
1245 arm_gen_test_cc(cond, label_match);
1246 gen_goto_tb(s, 0, s->pc);
1247 gen_set_label(label_match);
1248 gen_goto_tb(s, 1, addr);
1249 } else {
1250 /* 0xe and 0xf are both "always" conditions */
1251 gen_goto_tb(s, 0, addr);
1255 /* C5.6.68 HINT */
1256 static void handle_hint(DisasContext *s, uint32_t insn,
1257 unsigned int op1, unsigned int op2, unsigned int crm)
1259 unsigned int selector = crm << 3 | op2;
1261 if (op1 != 3) {
1262 unallocated_encoding(s);
1263 return;
1266 switch (selector) {
1267 case 0: /* NOP */
1268 return;
1269 case 3: /* WFI */
1270 s->is_jmp = DISAS_WFI;
1271 return;
1272 case 1: /* YIELD */
1273 s->is_jmp = DISAS_YIELD;
1274 return;
1275 case 2: /* WFE */
1276 s->is_jmp = DISAS_WFE;
1277 return;
1278 case 4: /* SEV */
1279 case 5: /* SEVL */
1280 /* we treat all as NOP at least for now */
1281 return;
1282 default:
1283 /* default specified as NOP equivalent */
1284 return;
1288 static void gen_clrex(DisasContext *s, uint32_t insn)
1290 tcg_gen_movi_i64(cpu_exclusive_addr, -1);
1293 /* CLREX, DSB, DMB, ISB */
1294 static void handle_sync(DisasContext *s, uint32_t insn,
1295 unsigned int op1, unsigned int op2, unsigned int crm)
1297 TCGBar bar;
1299 if (op1 != 3) {
1300 unallocated_encoding(s);
1301 return;
1304 switch (op2) {
1305 case 2: /* CLREX */
1306 gen_clrex(s, insn);
1307 return;
1308 case 4: /* DSB */
1309 case 5: /* DMB */
1310 switch (crm & 3) {
1311 case 1: /* MBReqTypes_Reads */
1312 bar = TCG_BAR_SC | TCG_MO_LD_LD | TCG_MO_LD_ST;
1313 break;
1314 case 2: /* MBReqTypes_Writes */
1315 bar = TCG_BAR_SC | TCG_MO_ST_ST;
1316 break;
1317 default: /* MBReqTypes_All */
1318 bar = TCG_BAR_SC | TCG_MO_ALL;
1319 break;
1321 tcg_gen_mb(bar);
1322 return;
1323 case 6: /* ISB */
1324 /* We need to break the TB after this insn to execute
1325 * a self-modified code correctly and also to take
1326 * any pending interrupts immediately.
1328 s->is_jmp = DISAS_UPDATE;
1329 return;
1330 default:
1331 unallocated_encoding(s);
1332 return;
1336 /* C5.6.130 MSR (immediate) - move immediate to processor state field */
1337 static void handle_msr_i(DisasContext *s, uint32_t insn,
1338 unsigned int op1, unsigned int op2, unsigned int crm)
1340 int op = op1 << 3 | op2;
1341 switch (op) {
1342 case 0x05: /* SPSel */
1343 if (s->current_el == 0) {
1344 unallocated_encoding(s);
1345 return;
1347 /* fall through */
1348 case 0x1e: /* DAIFSet */
1349 case 0x1f: /* DAIFClear */
1351 TCGv_i32 tcg_imm = tcg_const_i32(crm);
1352 TCGv_i32 tcg_op = tcg_const_i32(op);
1353 gen_a64_set_pc_im(s->pc - 4);
1354 gen_helper_msr_i_pstate(cpu_env, tcg_op, tcg_imm);
1355 tcg_temp_free_i32(tcg_imm);
1356 tcg_temp_free_i32(tcg_op);
1357 s->is_jmp = DISAS_UPDATE;
1358 break;
1360 default:
1361 unallocated_encoding(s);
1362 return;
1366 static void gen_get_nzcv(TCGv_i64 tcg_rt)
1368 TCGv_i32 tmp = tcg_temp_new_i32();
1369 TCGv_i32 nzcv = tcg_temp_new_i32();
1371 /* build bit 31, N */
1372 tcg_gen_andi_i32(nzcv, cpu_NF, (1U << 31));
1373 /* build bit 30, Z */
1374 tcg_gen_setcondi_i32(TCG_COND_EQ, tmp, cpu_ZF, 0);
1375 tcg_gen_deposit_i32(nzcv, nzcv, tmp, 30, 1);
1376 /* build bit 29, C */
1377 tcg_gen_deposit_i32(nzcv, nzcv, cpu_CF, 29, 1);
1378 /* build bit 28, V */
1379 tcg_gen_shri_i32(tmp, cpu_VF, 31);
1380 tcg_gen_deposit_i32(nzcv, nzcv, tmp, 28, 1);
1381 /* generate result */
1382 tcg_gen_extu_i32_i64(tcg_rt, nzcv);
1384 tcg_temp_free_i32(nzcv);
1385 tcg_temp_free_i32(tmp);
1388 static void gen_set_nzcv(TCGv_i64 tcg_rt)
1391 TCGv_i32 nzcv = tcg_temp_new_i32();
1393 /* take NZCV from R[t] */
1394 tcg_gen_extrl_i64_i32(nzcv, tcg_rt);
1396 /* bit 31, N */
1397 tcg_gen_andi_i32(cpu_NF, nzcv, (1U << 31));
1398 /* bit 30, Z */
1399 tcg_gen_andi_i32(cpu_ZF, nzcv, (1 << 30));
1400 tcg_gen_setcondi_i32(TCG_COND_EQ, cpu_ZF, cpu_ZF, 0);
1401 /* bit 29, C */
1402 tcg_gen_andi_i32(cpu_CF, nzcv, (1 << 29));
1403 tcg_gen_shri_i32(cpu_CF, cpu_CF, 29);
1404 /* bit 28, V */
1405 tcg_gen_andi_i32(cpu_VF, nzcv, (1 << 28));
1406 tcg_gen_shli_i32(cpu_VF, cpu_VF, 3);
1407 tcg_temp_free_i32(nzcv);
1410 /* C5.6.129 MRS - move from system register
1411 * C5.6.131 MSR (register) - move to system register
1412 * C5.6.204 SYS
1413 * C5.6.205 SYSL
1414 * These are all essentially the same insn in 'read' and 'write'
1415 * versions, with varying op0 fields.
1417 static void handle_sys(DisasContext *s, uint32_t insn, bool isread,
1418 unsigned int op0, unsigned int op1, unsigned int op2,
1419 unsigned int crn, unsigned int crm, unsigned int rt)
1421 const ARMCPRegInfo *ri;
1422 TCGv_i64 tcg_rt;
1424 ri = get_arm_cp_reginfo(s->cp_regs,
1425 ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP,
1426 crn, crm, op0, op1, op2));
1428 if (!ri) {
1429 /* Unknown register; this might be a guest error or a QEMU
1430 * unimplemented feature.
1432 qemu_log_mask(LOG_UNIMP, "%s access to unsupported AArch64 "
1433 "system register op0:%d op1:%d crn:%d crm:%d op2:%d\n",
1434 isread ? "read" : "write", op0, op1, crn, crm, op2);
1435 unallocated_encoding(s);
1436 return;
1439 /* Check access permissions */
1440 if (!cp_access_ok(s->current_el, ri, isread)) {
1441 unallocated_encoding(s);
1442 return;
1445 if (ri->accessfn) {
1446 /* Emit code to perform further access permissions checks at
1447 * runtime; this may result in an exception.
1449 TCGv_ptr tmpptr;
1450 TCGv_i32 tcg_syn, tcg_isread;
1451 uint32_t syndrome;
1453 gen_a64_set_pc_im(s->pc - 4);
1454 tmpptr = tcg_const_ptr(ri);
1455 syndrome = syn_aa64_sysregtrap(op0, op1, op2, crn, crm, rt, isread);
1456 tcg_syn = tcg_const_i32(syndrome);
1457 tcg_isread = tcg_const_i32(isread);
1458 gen_helper_access_check_cp_reg(cpu_env, tmpptr, tcg_syn, tcg_isread);
1459 tcg_temp_free_ptr(tmpptr);
1460 tcg_temp_free_i32(tcg_syn);
1461 tcg_temp_free_i32(tcg_isread);
1464 /* Handle special cases first */
1465 switch (ri->type & ~(ARM_CP_FLAG_MASK & ~ARM_CP_SPECIAL)) {
1466 case ARM_CP_NOP:
1467 return;
1468 case ARM_CP_NZCV:
1469 tcg_rt = cpu_reg(s, rt);
1470 if (isread) {
1471 gen_get_nzcv(tcg_rt);
1472 } else {
1473 gen_set_nzcv(tcg_rt);
1475 return;
1476 case ARM_CP_CURRENTEL:
1477 /* Reads as current EL value from pstate, which is
1478 * guaranteed to be constant by the tb flags.
1480 tcg_rt = cpu_reg(s, rt);
1481 tcg_gen_movi_i64(tcg_rt, s->current_el << 2);
1482 return;
1483 case ARM_CP_DC_ZVA:
1484 /* Writes clear the aligned block of memory which rt points into. */
1485 tcg_rt = cpu_reg(s, rt);
1486 gen_helper_dc_zva(cpu_env, tcg_rt);
1487 return;
1488 default:
1489 break;
1492 if ((s->tb->cflags & CF_USE_ICOUNT) && (ri->type & ARM_CP_IO)) {
1493 gen_io_start();
1496 tcg_rt = cpu_reg(s, rt);
1498 if (isread) {
1499 if (ri->type & ARM_CP_CONST) {
1500 tcg_gen_movi_i64(tcg_rt, ri->resetvalue);
1501 } else if (ri->readfn) {
1502 TCGv_ptr tmpptr;
1503 tmpptr = tcg_const_ptr(ri);
1504 gen_helper_get_cp_reg64(tcg_rt, cpu_env, tmpptr);
1505 tcg_temp_free_ptr(tmpptr);
1506 } else {
1507 tcg_gen_ld_i64(tcg_rt, cpu_env, ri->fieldoffset);
1509 } else {
1510 if (ri->type & ARM_CP_CONST) {
1511 /* If not forbidden by access permissions, treat as WI */
1512 return;
1513 } else if (ri->writefn) {
1514 TCGv_ptr tmpptr;
1515 tmpptr = tcg_const_ptr(ri);
1516 gen_helper_set_cp_reg64(cpu_env, tmpptr, tcg_rt);
1517 tcg_temp_free_ptr(tmpptr);
1518 } else {
1519 tcg_gen_st_i64(tcg_rt, cpu_env, ri->fieldoffset);
1523 if ((s->tb->cflags & CF_USE_ICOUNT) && (ri->type & ARM_CP_IO)) {
1524 /* I/O operations must end the TB here (whether read or write) */
1525 gen_io_end();
1526 s->is_jmp = DISAS_UPDATE;
1527 } else if (!isread && !(ri->type & ARM_CP_SUPPRESS_TB_END)) {
1528 /* We default to ending the TB on a coprocessor register write,
1529 * but allow this to be suppressed by the register definition
1530 * (usually only necessary to work around guest bugs).
1532 s->is_jmp = DISAS_UPDATE;
1536 /* C3.2.4 System
1537 * 31 22 21 20 19 18 16 15 12 11 8 7 5 4 0
1538 * +---------------------+---+-----+-----+-------+-------+-----+------+
1539 * | 1 1 0 1 0 1 0 1 0 0 | L | op0 | op1 | CRn | CRm | op2 | Rt |
1540 * +---------------------+---+-----+-----+-------+-------+-----+------+
1542 static void disas_system(DisasContext *s, uint32_t insn)
1544 unsigned int l, op0, op1, crn, crm, op2, rt;
1545 l = extract32(insn, 21, 1);
1546 op0 = extract32(insn, 19, 2);
1547 op1 = extract32(insn, 16, 3);
1548 crn = extract32(insn, 12, 4);
1549 crm = extract32(insn, 8, 4);
1550 op2 = extract32(insn, 5, 3);
1551 rt = extract32(insn, 0, 5);
1553 if (op0 == 0) {
1554 if (l || rt != 31) {
1555 unallocated_encoding(s);
1556 return;
1558 switch (crn) {
1559 case 2: /* C5.6.68 HINT */
1560 handle_hint(s, insn, op1, op2, crm);
1561 break;
1562 case 3: /* CLREX, DSB, DMB, ISB */
1563 handle_sync(s, insn, op1, op2, crm);
1564 break;
1565 case 4: /* C5.6.130 MSR (immediate) */
1566 handle_msr_i(s, insn, op1, op2, crm);
1567 break;
1568 default:
1569 unallocated_encoding(s);
1570 break;
1572 return;
1574 handle_sys(s, insn, l, op0, op1, op2, crn, crm, rt);
1577 /* C3.2.3 Exception generation
1579 * 31 24 23 21 20 5 4 2 1 0
1580 * +-----------------+-----+------------------------+-----+----+
1581 * | 1 1 0 1 0 1 0 0 | opc | imm16 | op2 | LL |
1582 * +-----------------------+------------------------+----------+
1584 static void disas_exc(DisasContext *s, uint32_t insn)
1586 int opc = extract32(insn, 21, 3);
1587 int op2_ll = extract32(insn, 0, 5);
1588 int imm16 = extract32(insn, 5, 16);
1589 TCGv_i32 tmp;
1591 switch (opc) {
1592 case 0:
1593 /* For SVC, HVC and SMC we advance the single-step state
1594 * machine before taking the exception. This is architecturally
1595 * mandated, to ensure that single-stepping a system call
1596 * instruction works properly.
1598 switch (op2_ll) {
1599 case 1:
1600 gen_ss_advance(s);
1601 gen_exception_insn(s, 0, EXCP_SWI, syn_aa64_svc(imm16),
1602 default_exception_el(s));
1603 break;
1604 case 2:
1605 if (s->current_el == 0) {
1606 unallocated_encoding(s);
1607 break;
1609 /* The pre HVC helper handles cases when HVC gets trapped
1610 * as an undefined insn by runtime configuration.
1612 gen_a64_set_pc_im(s->pc - 4);
1613 gen_helper_pre_hvc(cpu_env);
1614 gen_ss_advance(s);
1615 gen_exception_insn(s, 0, EXCP_HVC, syn_aa64_hvc(imm16), 2);
1616 break;
1617 case 3:
1618 if (s->current_el == 0) {
1619 unallocated_encoding(s);
1620 break;
1622 gen_a64_set_pc_im(s->pc - 4);
1623 tmp = tcg_const_i32(syn_aa64_smc(imm16));
1624 gen_helper_pre_smc(cpu_env, tmp);
1625 tcg_temp_free_i32(tmp);
1626 gen_ss_advance(s);
1627 gen_exception_insn(s, 0, EXCP_SMC, syn_aa64_smc(imm16), 3);
1628 break;
1629 default:
1630 unallocated_encoding(s);
1631 break;
1633 break;
1634 case 1:
1635 if (op2_ll != 0) {
1636 unallocated_encoding(s);
1637 break;
1639 /* BRK */
1640 gen_exception_insn(s, 4, EXCP_BKPT, syn_aa64_bkpt(imm16),
1641 default_exception_el(s));
1642 break;
1643 case 2:
1644 if (op2_ll != 0) {
1645 unallocated_encoding(s);
1646 break;
1648 /* HLT. This has two purposes.
1649 * Architecturally, it is an external halting debug instruction.
1650 * Since QEMU doesn't implement external debug, we treat this as
1651 * it is required for halting debug disabled: it will UNDEF.
1652 * Secondly, "HLT 0xf000" is the A64 semihosting syscall instruction.
1654 if (semihosting_enabled() && imm16 == 0xf000) {
1655 #ifndef CONFIG_USER_ONLY
1656 /* In system mode, don't allow userspace access to semihosting,
1657 * to provide some semblance of security (and for consistency
1658 * with our 32-bit semihosting).
1660 if (s->current_el == 0) {
1661 unsupported_encoding(s, insn);
1662 break;
1664 #endif
1665 gen_exception_internal_insn(s, 0, EXCP_SEMIHOST);
1666 } else {
1667 unsupported_encoding(s, insn);
1669 break;
1670 case 5:
1671 if (op2_ll < 1 || op2_ll > 3) {
1672 unallocated_encoding(s);
1673 break;
1675 /* DCPS1, DCPS2, DCPS3 */
1676 unsupported_encoding(s, insn);
1677 break;
1678 default:
1679 unallocated_encoding(s);
1680 break;
1684 /* C3.2.7 Unconditional branch (register)
1685 * 31 25 24 21 20 16 15 10 9 5 4 0
1686 * +---------------+-------+-------+-------+------+-------+
1687 * | 1 1 0 1 0 1 1 | opc | op2 | op3 | Rn | op4 |
1688 * +---------------+-------+-------+-------+------+-------+
1690 static void disas_uncond_b_reg(DisasContext *s, uint32_t insn)
1692 unsigned int opc, op2, op3, rn, op4;
1694 opc = extract32(insn, 21, 4);
1695 op2 = extract32(insn, 16, 5);
1696 op3 = extract32(insn, 10, 6);
1697 rn = extract32(insn, 5, 5);
1698 op4 = extract32(insn, 0, 5);
1700 if (op4 != 0x0 || op3 != 0x0 || op2 != 0x1f) {
1701 unallocated_encoding(s);
1702 return;
1705 switch (opc) {
1706 case 0: /* BR */
1707 case 2: /* RET */
1708 tcg_gen_mov_i64(cpu_pc, cpu_reg(s, rn));
1709 break;
1710 case 1: /* BLR */
1711 tcg_gen_mov_i64(cpu_pc, cpu_reg(s, rn));
1712 tcg_gen_movi_i64(cpu_reg(s, 30), s->pc);
1713 break;
1714 case 4: /* ERET */
1715 if (s->current_el == 0) {
1716 unallocated_encoding(s);
1717 return;
1719 gen_helper_exception_return(cpu_env);
1720 s->is_jmp = DISAS_JUMP;
1721 return;
1722 case 5: /* DRPS */
1723 if (rn != 0x1f) {
1724 unallocated_encoding(s);
1725 } else {
1726 unsupported_encoding(s, insn);
1728 return;
1729 default:
1730 unallocated_encoding(s);
1731 return;
1734 s->is_jmp = DISAS_JUMP;
1737 /* C3.2 Branches, exception generating and system instructions */
1738 static void disas_b_exc_sys(DisasContext *s, uint32_t insn)
1740 switch (extract32(insn, 25, 7)) {
1741 case 0x0a: case 0x0b:
1742 case 0x4a: case 0x4b: /* Unconditional branch (immediate) */
1743 disas_uncond_b_imm(s, insn);
1744 break;
1745 case 0x1a: case 0x5a: /* Compare & branch (immediate) */
1746 disas_comp_b_imm(s, insn);
1747 break;
1748 case 0x1b: case 0x5b: /* Test & branch (immediate) */
1749 disas_test_b_imm(s, insn);
1750 break;
1751 case 0x2a: /* Conditional branch (immediate) */
1752 disas_cond_b_imm(s, insn);
1753 break;
1754 case 0x6a: /* Exception generation / System */
1755 if (insn & (1 << 24)) {
1756 disas_system(s, insn);
1757 } else {
1758 disas_exc(s, insn);
1760 break;
1761 case 0x6b: /* Unconditional branch (register) */
1762 disas_uncond_b_reg(s, insn);
1763 break;
1764 default:
1765 unallocated_encoding(s);
1766 break;
1771 * Load/Store exclusive instructions are implemented by remembering
1772 * the value/address loaded, and seeing if these are the same
1773 * when the store is performed. This is not actually the architecturally
1774 * mandated semantics, but it works for typical guest code sequences
1775 * and avoids having to monitor regular stores.
1777 * In system emulation mode only one CPU will be running at once, so
1778 * this sequence is effectively atomic. In user emulation mode we
1779 * throw an exception and handle the atomic operation elsewhere.
1781 static void gen_load_exclusive(DisasContext *s, int rt, int rt2,
1782 TCGv_i64 addr, int size, bool is_pair)
1784 TCGv_i64 tmp = tcg_temp_new_i64();
1785 TCGMemOp memop = s->be_data + size;
1787 g_assert(size <= 3);
1788 tcg_gen_qemu_ld_i64(tmp, addr, get_mem_index(s), memop);
1790 if (is_pair) {
1791 TCGv_i64 addr2 = tcg_temp_new_i64();
1792 TCGv_i64 hitmp = tcg_temp_new_i64();
1794 g_assert(size >= 2);
1795 tcg_gen_addi_i64(addr2, addr, 1 << size);
1796 tcg_gen_qemu_ld_i64(hitmp, addr2, get_mem_index(s), memop);
1797 tcg_temp_free_i64(addr2);
1798 tcg_gen_mov_i64(cpu_exclusive_high, hitmp);
1799 tcg_gen_mov_i64(cpu_reg(s, rt2), hitmp);
1800 tcg_temp_free_i64(hitmp);
1803 tcg_gen_mov_i64(cpu_exclusive_val, tmp);
1804 tcg_gen_mov_i64(cpu_reg(s, rt), tmp);
1806 tcg_temp_free_i64(tmp);
1807 tcg_gen_mov_i64(cpu_exclusive_addr, addr);
1810 #ifdef CONFIG_USER_ONLY
1811 static void gen_store_exclusive(DisasContext *s, int rd, int rt, int rt2,
1812 TCGv_i64 addr, int size, int is_pair)
1814 tcg_gen_mov_i64(cpu_exclusive_test, addr);
1815 tcg_gen_movi_i32(cpu_exclusive_info,
1816 size | is_pair << 2 | (rd << 4) | (rt << 9) | (rt2 << 14));
1817 gen_exception_internal_insn(s, 4, EXCP_STREX);
1819 #else
1820 static void gen_store_exclusive(DisasContext *s, int rd, int rt, int rt2,
1821 TCGv_i64 inaddr, int size, int is_pair)
1823 /* if (env->exclusive_addr == addr && env->exclusive_val == [addr]
1824 * && (!is_pair || env->exclusive_high == [addr + datasize])) {
1825 * [addr] = {Rt};
1826 * if (is_pair) {
1827 * [addr + datasize] = {Rt2};
1829 * {Rd} = 0;
1830 * } else {
1831 * {Rd} = 1;
1833 * env->exclusive_addr = -1;
1835 TCGLabel *fail_label = gen_new_label();
1836 TCGLabel *done_label = gen_new_label();
1837 TCGv_i64 addr = tcg_temp_local_new_i64();
1838 TCGv_i64 tmp;
1840 /* Copy input into a local temp so it is not trashed when the
1841 * basic block ends at the branch insn.
1843 tcg_gen_mov_i64(addr, inaddr);
1844 tcg_gen_brcond_i64(TCG_COND_NE, addr, cpu_exclusive_addr, fail_label);
1846 tmp = tcg_temp_new_i64();
1847 tcg_gen_qemu_ld_i64(tmp, addr, get_mem_index(s), s->be_data + size);
1848 tcg_gen_brcond_i64(TCG_COND_NE, tmp, cpu_exclusive_val, fail_label);
1849 tcg_temp_free_i64(tmp);
1851 if (is_pair) {
1852 TCGv_i64 addrhi = tcg_temp_new_i64();
1853 TCGv_i64 tmphi = tcg_temp_new_i64();
1855 tcg_gen_addi_i64(addrhi, addr, 1 << size);
1856 tcg_gen_qemu_ld_i64(tmphi, addrhi, get_mem_index(s),
1857 s->be_data + size);
1858 tcg_gen_brcond_i64(TCG_COND_NE, tmphi, cpu_exclusive_high, fail_label);
1860 tcg_temp_free_i64(tmphi);
1861 tcg_temp_free_i64(addrhi);
1864 /* We seem to still have the exclusive monitor, so do the store */
1865 tcg_gen_qemu_st_i64(cpu_reg(s, rt), addr, get_mem_index(s),
1866 s->be_data + size);
1867 if (is_pair) {
1868 TCGv_i64 addrhi = tcg_temp_new_i64();
1870 tcg_gen_addi_i64(addrhi, addr, 1 << size);
1871 tcg_gen_qemu_st_i64(cpu_reg(s, rt2), addrhi,
1872 get_mem_index(s), s->be_data + size);
1873 tcg_temp_free_i64(addrhi);
1876 tcg_temp_free_i64(addr);
1878 tcg_gen_movi_i64(cpu_reg(s, rd), 0);
1879 tcg_gen_br(done_label);
1880 gen_set_label(fail_label);
1881 tcg_gen_movi_i64(cpu_reg(s, rd), 1);
1882 gen_set_label(done_label);
1883 tcg_gen_movi_i64(cpu_exclusive_addr, -1);
1886 #endif
1888 /* Update the Sixty-Four bit (SF) registersize. This logic is derived
1889 * from the ARMv8 specs for LDR (Shared decode for all encodings).
1891 static bool disas_ldst_compute_iss_sf(int size, bool is_signed, int opc)
1893 int opc0 = extract32(opc, 0, 1);
1894 int regsize;
1896 if (is_signed) {
1897 regsize = opc0 ? 32 : 64;
1898 } else {
1899 regsize = size == 3 ? 64 : 32;
1901 return regsize == 64;
1904 /* C3.3.6 Load/store exclusive
1906 * 31 30 29 24 23 22 21 20 16 15 14 10 9 5 4 0
1907 * +-----+-------------+----+---+----+------+----+-------+------+------+
1908 * | sz | 0 0 1 0 0 0 | o2 | L | o1 | Rs | o0 | Rt2 | Rn | Rt |
1909 * +-----+-------------+----+---+----+------+----+-------+------+------+
1911 * sz: 00 -> 8 bit, 01 -> 16 bit, 10 -> 32 bit, 11 -> 64 bit
1912 * L: 0 -> store, 1 -> load
1913 * o2: 0 -> exclusive, 1 -> not
1914 * o1: 0 -> single register, 1 -> register pair
1915 * o0: 1 -> load-acquire/store-release, 0 -> not
1917 static void disas_ldst_excl(DisasContext *s, uint32_t insn)
1919 int rt = extract32(insn, 0, 5);
1920 int rn = extract32(insn, 5, 5);
1921 int rt2 = extract32(insn, 10, 5);
1922 int is_lasr = extract32(insn, 15, 1);
1923 int rs = extract32(insn, 16, 5);
1924 int is_pair = extract32(insn, 21, 1);
1925 int is_store = !extract32(insn, 22, 1);
1926 int is_excl = !extract32(insn, 23, 1);
1927 int size = extract32(insn, 30, 2);
1928 TCGv_i64 tcg_addr;
1930 if ((!is_excl && !is_pair && !is_lasr) ||
1931 (!is_excl && is_pair) ||
1932 (is_pair && size < 2)) {
1933 unallocated_encoding(s);
1934 return;
1937 if (rn == 31) {
1938 gen_check_sp_alignment(s);
1940 tcg_addr = read_cpu_reg_sp(s, rn, 1);
1942 /* Note that since TCG is single threaded load-acquire/store-release
1943 * semantics require no extra if (is_lasr) { ... } handling.
1946 if (is_excl) {
1947 if (!is_store) {
1948 s->is_ldex = true;
1949 gen_load_exclusive(s, rt, rt2, tcg_addr, size, is_pair);
1950 if (is_lasr) {
1951 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
1953 } else {
1954 if (is_lasr) {
1955 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
1957 gen_store_exclusive(s, rs, rt, rt2, tcg_addr, size, is_pair);
1959 } else {
1960 TCGv_i64 tcg_rt = cpu_reg(s, rt);
1961 bool iss_sf = disas_ldst_compute_iss_sf(size, false, 0);
1963 /* Generate ISS for non-exclusive accesses including LASR. */
1964 if (is_store) {
1965 if (is_lasr) {
1966 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_STRL);
1968 do_gpr_st(s, tcg_rt, tcg_addr, size,
1969 true, rt, iss_sf, is_lasr);
1970 } else {
1971 do_gpr_ld(s, tcg_rt, tcg_addr, size, false, false,
1972 true, rt, iss_sf, is_lasr);
1973 if (is_lasr) {
1974 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_LDAQ);
1981 * C3.3.5 Load register (literal)
1983 * 31 30 29 27 26 25 24 23 5 4 0
1984 * +-----+-------+---+-----+-------------------+-------+
1985 * | opc | 0 1 1 | V | 0 0 | imm19 | Rt |
1986 * +-----+-------+---+-----+-------------------+-------+
1988 * V: 1 -> vector (simd/fp)
1989 * opc (non-vector): 00 -> 32 bit, 01 -> 64 bit,
1990 * 10-> 32 bit signed, 11 -> prefetch
1991 * opc (vector): 00 -> 32 bit, 01 -> 64 bit, 10 -> 128 bit (11 unallocated)
1993 static void disas_ld_lit(DisasContext *s, uint32_t insn)
1995 int rt = extract32(insn, 0, 5);
1996 int64_t imm = sextract32(insn, 5, 19) << 2;
1997 bool is_vector = extract32(insn, 26, 1);
1998 int opc = extract32(insn, 30, 2);
1999 bool is_signed = false;
2000 int size = 2;
2001 TCGv_i64 tcg_rt, tcg_addr;
2003 if (is_vector) {
2004 if (opc == 3) {
2005 unallocated_encoding(s);
2006 return;
2008 size = 2 + opc;
2009 if (!fp_access_check(s)) {
2010 return;
2012 } else {
2013 if (opc == 3) {
2014 /* PRFM (literal) : prefetch */
2015 return;
2017 size = 2 + extract32(opc, 0, 1);
2018 is_signed = extract32(opc, 1, 1);
2021 tcg_rt = cpu_reg(s, rt);
2023 tcg_addr = tcg_const_i64((s->pc - 4) + imm);
2024 if (is_vector) {
2025 do_fp_ld(s, rt, tcg_addr, size);
2026 } else {
2027 /* Only unsigned 32bit loads target 32bit registers. */
2028 bool iss_sf = opc == 0 ? 32 : 64;
2030 do_gpr_ld(s, tcg_rt, tcg_addr, size, is_signed, false,
2031 true, rt, iss_sf, false);
2033 tcg_temp_free_i64(tcg_addr);
2037 * C5.6.80 LDNP (Load Pair - non-temporal hint)
2038 * C5.6.81 LDP (Load Pair - non vector)
2039 * C5.6.82 LDPSW (Load Pair Signed Word - non vector)
2040 * C5.6.176 STNP (Store Pair - non-temporal hint)
2041 * C5.6.177 STP (Store Pair - non vector)
2042 * C6.3.165 LDNP (Load Pair of SIMD&FP - non-temporal hint)
2043 * C6.3.165 LDP (Load Pair of SIMD&FP)
2044 * C6.3.284 STNP (Store Pair of SIMD&FP - non-temporal hint)
2045 * C6.3.284 STP (Store Pair of SIMD&FP)
2047 * 31 30 29 27 26 25 24 23 22 21 15 14 10 9 5 4 0
2048 * +-----+-------+---+---+-------+---+-----------------------------+
2049 * | opc | 1 0 1 | V | 0 | index | L | imm7 | Rt2 | Rn | Rt |
2050 * +-----+-------+---+---+-------+---+-------+-------+------+------+
2052 * opc: LDP/STP/LDNP/STNP 00 -> 32 bit, 10 -> 64 bit
2053 * LDPSW 01
2054 * LDP/STP/LDNP/STNP (SIMD) 00 -> 32 bit, 01 -> 64 bit, 10 -> 128 bit
2055 * V: 0 -> GPR, 1 -> Vector
2056 * idx: 00 -> signed offset with non-temporal hint, 01 -> post-index,
2057 * 10 -> signed offset, 11 -> pre-index
2058 * L: 0 -> Store 1 -> Load
2060 * Rt, Rt2 = GPR or SIMD registers to be stored
2061 * Rn = general purpose register containing address
2062 * imm7 = signed offset (multiple of 4 or 8 depending on size)
2064 static void disas_ldst_pair(DisasContext *s, uint32_t insn)
2066 int rt = extract32(insn, 0, 5);
2067 int rn = extract32(insn, 5, 5);
2068 int rt2 = extract32(insn, 10, 5);
2069 uint64_t offset = sextract64(insn, 15, 7);
2070 int index = extract32(insn, 23, 2);
2071 bool is_vector = extract32(insn, 26, 1);
2072 bool is_load = extract32(insn, 22, 1);
2073 int opc = extract32(insn, 30, 2);
2075 bool is_signed = false;
2076 bool postindex = false;
2077 bool wback = false;
2079 TCGv_i64 tcg_addr; /* calculated address */
2080 int size;
2082 if (opc == 3) {
2083 unallocated_encoding(s);
2084 return;
2087 if (is_vector) {
2088 size = 2 + opc;
2089 } else {
2090 size = 2 + extract32(opc, 1, 1);
2091 is_signed = extract32(opc, 0, 1);
2092 if (!is_load && is_signed) {
2093 unallocated_encoding(s);
2094 return;
2098 switch (index) {
2099 case 1: /* post-index */
2100 postindex = true;
2101 wback = true;
2102 break;
2103 case 0:
2104 /* signed offset with "non-temporal" hint. Since we don't emulate
2105 * caches we don't care about hints to the cache system about
2106 * data access patterns, and handle this identically to plain
2107 * signed offset.
2109 if (is_signed) {
2110 /* There is no non-temporal-hint version of LDPSW */
2111 unallocated_encoding(s);
2112 return;
2114 postindex = false;
2115 break;
2116 case 2: /* signed offset, rn not updated */
2117 postindex = false;
2118 break;
2119 case 3: /* pre-index */
2120 postindex = false;
2121 wback = true;
2122 break;
2125 if (is_vector && !fp_access_check(s)) {
2126 return;
2129 offset <<= size;
2131 if (rn == 31) {
2132 gen_check_sp_alignment(s);
2135 tcg_addr = read_cpu_reg_sp(s, rn, 1);
2137 if (!postindex) {
2138 tcg_gen_addi_i64(tcg_addr, tcg_addr, offset);
2141 if (is_vector) {
2142 if (is_load) {
2143 do_fp_ld(s, rt, tcg_addr, size);
2144 } else {
2145 do_fp_st(s, rt, tcg_addr, size);
2147 } else {
2148 TCGv_i64 tcg_rt = cpu_reg(s, rt);
2149 if (is_load) {
2150 do_gpr_ld(s, tcg_rt, tcg_addr, size, is_signed, false,
2151 false, 0, false, false);
2152 } else {
2153 do_gpr_st(s, tcg_rt, tcg_addr, size,
2154 false, 0, false, false);
2157 tcg_gen_addi_i64(tcg_addr, tcg_addr, 1 << size);
2158 if (is_vector) {
2159 if (is_load) {
2160 do_fp_ld(s, rt2, tcg_addr, size);
2161 } else {
2162 do_fp_st(s, rt2, tcg_addr, size);
2164 } else {
2165 TCGv_i64 tcg_rt2 = cpu_reg(s, rt2);
2166 if (is_load) {
2167 do_gpr_ld(s, tcg_rt2, tcg_addr, size, is_signed, false,
2168 false, 0, false, false);
2169 } else {
2170 do_gpr_st(s, tcg_rt2, tcg_addr, size,
2171 false, 0, false, false);
2175 if (wback) {
2176 if (postindex) {
2177 tcg_gen_addi_i64(tcg_addr, tcg_addr, offset - (1 << size));
2178 } else {
2179 tcg_gen_subi_i64(tcg_addr, tcg_addr, 1 << size);
2181 tcg_gen_mov_i64(cpu_reg_sp(s, rn), tcg_addr);
2186 * C3.3.8 Load/store (immediate post-indexed)
2187 * C3.3.9 Load/store (immediate pre-indexed)
2188 * C3.3.12 Load/store (unscaled immediate)
2190 * 31 30 29 27 26 25 24 23 22 21 20 12 11 10 9 5 4 0
2191 * +----+-------+---+-----+-----+---+--------+-----+------+------+
2192 * |size| 1 1 1 | V | 0 0 | opc | 0 | imm9 | idx | Rn | Rt |
2193 * +----+-------+---+-----+-----+---+--------+-----+------+------+
2195 * idx = 01 -> post-indexed, 11 pre-indexed, 00 unscaled imm. (no writeback)
2196 10 -> unprivileged
2197 * V = 0 -> non-vector
2198 * size: 00 -> 8 bit, 01 -> 16 bit, 10 -> 32 bit, 11 -> 64bit
2199 * opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
2201 static void disas_ldst_reg_imm9(DisasContext *s, uint32_t insn,
2202 int opc,
2203 int size,
2204 int rt,
2205 bool is_vector)
2207 int rn = extract32(insn, 5, 5);
2208 int imm9 = sextract32(insn, 12, 9);
2209 int idx = extract32(insn, 10, 2);
2210 bool is_signed = false;
2211 bool is_store = false;
2212 bool is_extended = false;
2213 bool is_unpriv = (idx == 2);
2214 bool iss_valid = !is_vector;
2215 bool post_index;
2216 bool writeback;
2218 TCGv_i64 tcg_addr;
2220 if (is_vector) {
2221 size |= (opc & 2) << 1;
2222 if (size > 4 || is_unpriv) {
2223 unallocated_encoding(s);
2224 return;
2226 is_store = ((opc & 1) == 0);
2227 if (!fp_access_check(s)) {
2228 return;
2230 } else {
2231 if (size == 3 && opc == 2) {
2232 /* PRFM - prefetch */
2233 if (is_unpriv) {
2234 unallocated_encoding(s);
2235 return;
2237 return;
2239 if (opc == 3 && size > 1) {
2240 unallocated_encoding(s);
2241 return;
2243 is_store = (opc == 0);
2244 is_signed = extract32(opc, 1, 1);
2245 is_extended = (size < 3) && extract32(opc, 0, 1);
2248 switch (idx) {
2249 case 0:
2250 case 2:
2251 post_index = false;
2252 writeback = false;
2253 break;
2254 case 1:
2255 post_index = true;
2256 writeback = true;
2257 break;
2258 case 3:
2259 post_index = false;
2260 writeback = true;
2261 break;
2264 if (rn == 31) {
2265 gen_check_sp_alignment(s);
2267 tcg_addr = read_cpu_reg_sp(s, rn, 1);
2269 if (!post_index) {
2270 tcg_gen_addi_i64(tcg_addr, tcg_addr, imm9);
2273 if (is_vector) {
2274 if (is_store) {
2275 do_fp_st(s, rt, tcg_addr, size);
2276 } else {
2277 do_fp_ld(s, rt, tcg_addr, size);
2279 } else {
2280 TCGv_i64 tcg_rt = cpu_reg(s, rt);
2281 int memidx = is_unpriv ? get_a64_user_mem_index(s) : get_mem_index(s);
2282 bool iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
2284 if (is_store) {
2285 do_gpr_st_memidx(s, tcg_rt, tcg_addr, size, memidx,
2286 iss_valid, rt, iss_sf, false);
2287 } else {
2288 do_gpr_ld_memidx(s, tcg_rt, tcg_addr, size,
2289 is_signed, is_extended, memidx,
2290 iss_valid, rt, iss_sf, false);
2294 if (writeback) {
2295 TCGv_i64 tcg_rn = cpu_reg_sp(s, rn);
2296 if (post_index) {
2297 tcg_gen_addi_i64(tcg_addr, tcg_addr, imm9);
2299 tcg_gen_mov_i64(tcg_rn, tcg_addr);
2304 * C3.3.10 Load/store (register offset)
2306 * 31 30 29 27 26 25 24 23 22 21 20 16 15 13 12 11 10 9 5 4 0
2307 * +----+-------+---+-----+-----+---+------+-----+--+-----+----+----+
2308 * |size| 1 1 1 | V | 0 0 | opc | 1 | Rm | opt | S| 1 0 | Rn | Rt |
2309 * +----+-------+---+-----+-----+---+------+-----+--+-----+----+----+
2311 * For non-vector:
2312 * size: 00-> byte, 01 -> 16 bit, 10 -> 32bit, 11 -> 64bit
2313 * opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
2314 * For vector:
2315 * size is opc<1>:size<1:0> so 100 -> 128 bit; 110 and 111 unallocated
2316 * opc<0>: 0 -> store, 1 -> load
2317 * V: 1 -> vector/simd
2318 * opt: extend encoding (see DecodeRegExtend)
2319 * S: if S=1 then scale (essentially index by sizeof(size))
2320 * Rt: register to transfer into/out of
2321 * Rn: address register or SP for base
2322 * Rm: offset register or ZR for offset
2324 static void disas_ldst_reg_roffset(DisasContext *s, uint32_t insn,
2325 int opc,
2326 int size,
2327 int rt,
2328 bool is_vector)
2330 int rn = extract32(insn, 5, 5);
2331 int shift = extract32(insn, 12, 1);
2332 int rm = extract32(insn, 16, 5);
2333 int opt = extract32(insn, 13, 3);
2334 bool is_signed = false;
2335 bool is_store = false;
2336 bool is_extended = false;
2338 TCGv_i64 tcg_rm;
2339 TCGv_i64 tcg_addr;
2341 if (extract32(opt, 1, 1) == 0) {
2342 unallocated_encoding(s);
2343 return;
2346 if (is_vector) {
2347 size |= (opc & 2) << 1;
2348 if (size > 4) {
2349 unallocated_encoding(s);
2350 return;
2352 is_store = !extract32(opc, 0, 1);
2353 if (!fp_access_check(s)) {
2354 return;
2356 } else {
2357 if (size == 3 && opc == 2) {
2358 /* PRFM - prefetch */
2359 return;
2361 if (opc == 3 && size > 1) {
2362 unallocated_encoding(s);
2363 return;
2365 is_store = (opc == 0);
2366 is_signed = extract32(opc, 1, 1);
2367 is_extended = (size < 3) && extract32(opc, 0, 1);
2370 if (rn == 31) {
2371 gen_check_sp_alignment(s);
2373 tcg_addr = read_cpu_reg_sp(s, rn, 1);
2375 tcg_rm = read_cpu_reg(s, rm, 1);
2376 ext_and_shift_reg(tcg_rm, tcg_rm, opt, shift ? size : 0);
2378 tcg_gen_add_i64(tcg_addr, tcg_addr, tcg_rm);
2380 if (is_vector) {
2381 if (is_store) {
2382 do_fp_st(s, rt, tcg_addr, size);
2383 } else {
2384 do_fp_ld(s, rt, tcg_addr, size);
2386 } else {
2387 TCGv_i64 tcg_rt = cpu_reg(s, rt);
2388 bool iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
2389 if (is_store) {
2390 do_gpr_st(s, tcg_rt, tcg_addr, size,
2391 true, rt, iss_sf, false);
2392 } else {
2393 do_gpr_ld(s, tcg_rt, tcg_addr, size,
2394 is_signed, is_extended,
2395 true, rt, iss_sf, false);
2401 * C3.3.13 Load/store (unsigned immediate)
2403 * 31 30 29 27 26 25 24 23 22 21 10 9 5
2404 * +----+-------+---+-----+-----+------------+-------+------+
2405 * |size| 1 1 1 | V | 0 1 | opc | imm12 | Rn | Rt |
2406 * +----+-------+---+-----+-----+------------+-------+------+
2408 * For non-vector:
2409 * size: 00-> byte, 01 -> 16 bit, 10 -> 32bit, 11 -> 64bit
2410 * opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
2411 * For vector:
2412 * size is opc<1>:size<1:0> so 100 -> 128 bit; 110 and 111 unallocated
2413 * opc<0>: 0 -> store, 1 -> load
2414 * Rn: base address register (inc SP)
2415 * Rt: target register
2417 static void disas_ldst_reg_unsigned_imm(DisasContext *s, uint32_t insn,
2418 int opc,
2419 int size,
2420 int rt,
2421 bool is_vector)
2423 int rn = extract32(insn, 5, 5);
2424 unsigned int imm12 = extract32(insn, 10, 12);
2425 unsigned int offset;
2427 TCGv_i64 tcg_addr;
2429 bool is_store;
2430 bool is_signed = false;
2431 bool is_extended = false;
2433 if (is_vector) {
2434 size |= (opc & 2) << 1;
2435 if (size > 4) {
2436 unallocated_encoding(s);
2437 return;
2439 is_store = !extract32(opc, 0, 1);
2440 if (!fp_access_check(s)) {
2441 return;
2443 } else {
2444 if (size == 3 && opc == 2) {
2445 /* PRFM - prefetch */
2446 return;
2448 if (opc == 3 && size > 1) {
2449 unallocated_encoding(s);
2450 return;
2452 is_store = (opc == 0);
2453 is_signed = extract32(opc, 1, 1);
2454 is_extended = (size < 3) && extract32(opc, 0, 1);
2457 if (rn == 31) {
2458 gen_check_sp_alignment(s);
2460 tcg_addr = read_cpu_reg_sp(s, rn, 1);
2461 offset = imm12 << size;
2462 tcg_gen_addi_i64(tcg_addr, tcg_addr, offset);
2464 if (is_vector) {
2465 if (is_store) {
2466 do_fp_st(s, rt, tcg_addr, size);
2467 } else {
2468 do_fp_ld(s, rt, tcg_addr, size);
2470 } else {
2471 TCGv_i64 tcg_rt = cpu_reg(s, rt);
2472 bool iss_sf = disas_ldst_compute_iss_sf(size, is_signed, opc);
2473 if (is_store) {
2474 do_gpr_st(s, tcg_rt, tcg_addr, size,
2475 true, rt, iss_sf, false);
2476 } else {
2477 do_gpr_ld(s, tcg_rt, tcg_addr, size, is_signed, is_extended,
2478 true, rt, iss_sf, false);
2483 /* Load/store register (all forms) */
2484 static void disas_ldst_reg(DisasContext *s, uint32_t insn)
2486 int rt = extract32(insn, 0, 5);
2487 int opc = extract32(insn, 22, 2);
2488 bool is_vector = extract32(insn, 26, 1);
2489 int size = extract32(insn, 30, 2);
2491 switch (extract32(insn, 24, 2)) {
2492 case 0:
2493 if (extract32(insn, 21, 1) == 1 && extract32(insn, 10, 2) == 2) {
2494 disas_ldst_reg_roffset(s, insn, opc, size, rt, is_vector);
2495 } else {
2496 /* Load/store register (unscaled immediate)
2497 * Load/store immediate pre/post-indexed
2498 * Load/store register unprivileged
2500 disas_ldst_reg_imm9(s, insn, opc, size, rt, is_vector);
2502 break;
2503 case 1:
2504 disas_ldst_reg_unsigned_imm(s, insn, opc, size, rt, is_vector);
2505 break;
2506 default:
2507 unallocated_encoding(s);
2508 break;
2512 /* C3.3.1 AdvSIMD load/store multiple structures
2514 * 31 30 29 23 22 21 16 15 12 11 10 9 5 4 0
2515 * +---+---+---------------+---+-------------+--------+------+------+------+
2516 * | 0 | Q | 0 0 1 1 0 0 0 | L | 0 0 0 0 0 0 | opcode | size | Rn | Rt |
2517 * +---+---+---------------+---+-------------+--------+------+------+------+
2519 * C3.3.2 AdvSIMD load/store multiple structures (post-indexed)
2521 * 31 30 29 23 22 21 20 16 15 12 11 10 9 5 4 0
2522 * +---+---+---------------+---+---+---------+--------+------+------+------+
2523 * | 0 | Q | 0 0 1 1 0 0 1 | L | 0 | Rm | opcode | size | Rn | Rt |
2524 * +---+---+---------------+---+---+---------+--------+------+------+------+
2526 * Rt: first (or only) SIMD&FP register to be transferred
2527 * Rn: base address or SP
2528 * Rm (post-index only): post-index register (when !31) or size dependent #imm
2530 static void disas_ldst_multiple_struct(DisasContext *s, uint32_t insn)
2532 int rt = extract32(insn, 0, 5);
2533 int rn = extract32(insn, 5, 5);
2534 int size = extract32(insn, 10, 2);
2535 int opcode = extract32(insn, 12, 4);
2536 bool is_store = !extract32(insn, 22, 1);
2537 bool is_postidx = extract32(insn, 23, 1);
2538 bool is_q = extract32(insn, 30, 1);
2539 TCGv_i64 tcg_addr, tcg_rn;
2541 int ebytes = 1 << size;
2542 int elements = (is_q ? 128 : 64) / (8 << size);
2543 int rpt; /* num iterations */
2544 int selem; /* structure elements */
2545 int r;
2547 if (extract32(insn, 31, 1) || extract32(insn, 21, 1)) {
2548 unallocated_encoding(s);
2549 return;
2552 /* From the shared decode logic */
2553 switch (opcode) {
2554 case 0x0:
2555 rpt = 1;
2556 selem = 4;
2557 break;
2558 case 0x2:
2559 rpt = 4;
2560 selem = 1;
2561 break;
2562 case 0x4:
2563 rpt = 1;
2564 selem = 3;
2565 break;
2566 case 0x6:
2567 rpt = 3;
2568 selem = 1;
2569 break;
2570 case 0x7:
2571 rpt = 1;
2572 selem = 1;
2573 break;
2574 case 0x8:
2575 rpt = 1;
2576 selem = 2;
2577 break;
2578 case 0xa:
2579 rpt = 2;
2580 selem = 1;
2581 break;
2582 default:
2583 unallocated_encoding(s);
2584 return;
2587 if (size == 3 && !is_q && selem != 1) {
2588 /* reserved */
2589 unallocated_encoding(s);
2590 return;
2593 if (!fp_access_check(s)) {
2594 return;
2597 if (rn == 31) {
2598 gen_check_sp_alignment(s);
2601 tcg_rn = cpu_reg_sp(s, rn);
2602 tcg_addr = tcg_temp_new_i64();
2603 tcg_gen_mov_i64(tcg_addr, tcg_rn);
2605 for (r = 0; r < rpt; r++) {
2606 int e;
2607 for (e = 0; e < elements; e++) {
2608 int tt = (rt + r) % 32;
2609 int xs;
2610 for (xs = 0; xs < selem; xs++) {
2611 if (is_store) {
2612 do_vec_st(s, tt, e, tcg_addr, size);
2613 } else {
2614 do_vec_ld(s, tt, e, tcg_addr, size);
2616 /* For non-quad operations, setting a slice of the low
2617 * 64 bits of the register clears the high 64 bits (in
2618 * the ARM ARM pseudocode this is implicit in the fact
2619 * that 'rval' is a 64 bit wide variable). We optimize
2620 * by noticing that we only need to do this the first
2621 * time we touch a register.
2623 if (!is_q && e == 0 && (r == 0 || xs == selem - 1)) {
2624 clear_vec_high(s, tt);
2627 tcg_gen_addi_i64(tcg_addr, tcg_addr, ebytes);
2628 tt = (tt + 1) % 32;
2633 if (is_postidx) {
2634 int rm = extract32(insn, 16, 5);
2635 if (rm == 31) {
2636 tcg_gen_mov_i64(tcg_rn, tcg_addr);
2637 } else {
2638 tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, rm));
2641 tcg_temp_free_i64(tcg_addr);
2644 /* C3.3.3 AdvSIMD load/store single structure
2646 * 31 30 29 23 22 21 20 16 15 13 12 11 10 9 5 4 0
2647 * +---+---+---------------+-----+-----------+-----+---+------+------+------+
2648 * | 0 | Q | 0 0 1 1 0 1 0 | L R | 0 0 0 0 0 | opc | S | size | Rn | Rt |
2649 * +---+---+---------------+-----+-----------+-----+---+------+------+------+
2651 * C3.3.4 AdvSIMD load/store single structure (post-indexed)
2653 * 31 30 29 23 22 21 20 16 15 13 12 11 10 9 5 4 0
2654 * +---+---+---------------+-----+-----------+-----+---+------+------+------+
2655 * | 0 | Q | 0 0 1 1 0 1 1 | L R | Rm | opc | S | size | Rn | Rt |
2656 * +---+---+---------------+-----+-----------+-----+---+------+------+------+
2658 * Rt: first (or only) SIMD&FP register to be transferred
2659 * Rn: base address or SP
2660 * Rm (post-index only): post-index register (when !31) or size dependent #imm
2661 * index = encoded in Q:S:size dependent on size
2663 * lane_size = encoded in R, opc
2664 * transfer width = encoded in opc, S, size
2666 static void disas_ldst_single_struct(DisasContext *s, uint32_t insn)
2668 int rt = extract32(insn, 0, 5);
2669 int rn = extract32(insn, 5, 5);
2670 int size = extract32(insn, 10, 2);
2671 int S = extract32(insn, 12, 1);
2672 int opc = extract32(insn, 13, 3);
2673 int R = extract32(insn, 21, 1);
2674 int is_load = extract32(insn, 22, 1);
2675 int is_postidx = extract32(insn, 23, 1);
2676 int is_q = extract32(insn, 30, 1);
2678 int scale = extract32(opc, 1, 2);
2679 int selem = (extract32(opc, 0, 1) << 1 | R) + 1;
2680 bool replicate = false;
2681 int index = is_q << 3 | S << 2 | size;
2682 int ebytes, xs;
2683 TCGv_i64 tcg_addr, tcg_rn;
2685 switch (scale) {
2686 case 3:
2687 if (!is_load || S) {
2688 unallocated_encoding(s);
2689 return;
2691 scale = size;
2692 replicate = true;
2693 break;
2694 case 0:
2695 break;
2696 case 1:
2697 if (extract32(size, 0, 1)) {
2698 unallocated_encoding(s);
2699 return;
2701 index >>= 1;
2702 break;
2703 case 2:
2704 if (extract32(size, 1, 1)) {
2705 unallocated_encoding(s);
2706 return;
2708 if (!extract32(size, 0, 1)) {
2709 index >>= 2;
2710 } else {
2711 if (S) {
2712 unallocated_encoding(s);
2713 return;
2715 index >>= 3;
2716 scale = 3;
2718 break;
2719 default:
2720 g_assert_not_reached();
2723 if (!fp_access_check(s)) {
2724 return;
2727 ebytes = 1 << scale;
2729 if (rn == 31) {
2730 gen_check_sp_alignment(s);
2733 tcg_rn = cpu_reg_sp(s, rn);
2734 tcg_addr = tcg_temp_new_i64();
2735 tcg_gen_mov_i64(tcg_addr, tcg_rn);
2737 for (xs = 0; xs < selem; xs++) {
2738 if (replicate) {
2739 /* Load and replicate to all elements */
2740 uint64_t mulconst;
2741 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
2743 tcg_gen_qemu_ld_i64(tcg_tmp, tcg_addr,
2744 get_mem_index(s), s->be_data + scale);
2745 switch (scale) {
2746 case 0:
2747 mulconst = 0x0101010101010101ULL;
2748 break;
2749 case 1:
2750 mulconst = 0x0001000100010001ULL;
2751 break;
2752 case 2:
2753 mulconst = 0x0000000100000001ULL;
2754 break;
2755 case 3:
2756 mulconst = 0;
2757 break;
2758 default:
2759 g_assert_not_reached();
2761 if (mulconst) {
2762 tcg_gen_muli_i64(tcg_tmp, tcg_tmp, mulconst);
2764 write_vec_element(s, tcg_tmp, rt, 0, MO_64);
2765 if (is_q) {
2766 write_vec_element(s, tcg_tmp, rt, 1, MO_64);
2767 } else {
2768 clear_vec_high(s, rt);
2770 tcg_temp_free_i64(tcg_tmp);
2771 } else {
2772 /* Load/store one element per register */
2773 if (is_load) {
2774 do_vec_ld(s, rt, index, tcg_addr, s->be_data + scale);
2775 } else {
2776 do_vec_st(s, rt, index, tcg_addr, s->be_data + scale);
2779 tcg_gen_addi_i64(tcg_addr, tcg_addr, ebytes);
2780 rt = (rt + 1) % 32;
2783 if (is_postidx) {
2784 int rm = extract32(insn, 16, 5);
2785 if (rm == 31) {
2786 tcg_gen_mov_i64(tcg_rn, tcg_addr);
2787 } else {
2788 tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, rm));
2791 tcg_temp_free_i64(tcg_addr);
2794 /* C3.3 Loads and stores */
2795 static void disas_ldst(DisasContext *s, uint32_t insn)
2797 switch (extract32(insn, 24, 6)) {
2798 case 0x08: /* Load/store exclusive */
2799 disas_ldst_excl(s, insn);
2800 break;
2801 case 0x18: case 0x1c: /* Load register (literal) */
2802 disas_ld_lit(s, insn);
2803 break;
2804 case 0x28: case 0x29:
2805 case 0x2c: case 0x2d: /* Load/store pair (all forms) */
2806 disas_ldst_pair(s, insn);
2807 break;
2808 case 0x38: case 0x39:
2809 case 0x3c: case 0x3d: /* Load/store register (all forms) */
2810 disas_ldst_reg(s, insn);
2811 break;
2812 case 0x0c: /* AdvSIMD load/store multiple structures */
2813 disas_ldst_multiple_struct(s, insn);
2814 break;
2815 case 0x0d: /* AdvSIMD load/store single structure */
2816 disas_ldst_single_struct(s, insn);
2817 break;
2818 default:
2819 unallocated_encoding(s);
2820 break;
2824 /* C3.4.6 PC-rel. addressing
2825 * 31 30 29 28 24 23 5 4 0
2826 * +----+-------+-----------+-------------------+------+
2827 * | op | immlo | 1 0 0 0 0 | immhi | Rd |
2828 * +----+-------+-----------+-------------------+------+
2830 static void disas_pc_rel_adr(DisasContext *s, uint32_t insn)
2832 unsigned int page, rd;
2833 uint64_t base;
2834 uint64_t offset;
2836 page = extract32(insn, 31, 1);
2837 /* SignExtend(immhi:immlo) -> offset */
2838 offset = sextract64(insn, 5, 19);
2839 offset = offset << 2 | extract32(insn, 29, 2);
2840 rd = extract32(insn, 0, 5);
2841 base = s->pc - 4;
2843 if (page) {
2844 /* ADRP (page based) */
2845 base &= ~0xfff;
2846 offset <<= 12;
2849 tcg_gen_movi_i64(cpu_reg(s, rd), base + offset);
2853 * C3.4.1 Add/subtract (immediate)
2855 * 31 30 29 28 24 23 22 21 10 9 5 4 0
2856 * +--+--+--+-----------+-----+-------------+-----+-----+
2857 * |sf|op| S| 1 0 0 0 1 |shift| imm12 | Rn | Rd |
2858 * +--+--+--+-----------+-----+-------------+-----+-----+
2860 * sf: 0 -> 32bit, 1 -> 64bit
2861 * op: 0 -> add , 1 -> sub
2862 * S: 1 -> set flags
2863 * shift: 00 -> LSL imm by 0, 01 -> LSL imm by 12
2865 static void disas_add_sub_imm(DisasContext *s, uint32_t insn)
2867 int rd = extract32(insn, 0, 5);
2868 int rn = extract32(insn, 5, 5);
2869 uint64_t imm = extract32(insn, 10, 12);
2870 int shift = extract32(insn, 22, 2);
2871 bool setflags = extract32(insn, 29, 1);
2872 bool sub_op = extract32(insn, 30, 1);
2873 bool is_64bit = extract32(insn, 31, 1);
2875 TCGv_i64 tcg_rn = cpu_reg_sp(s, rn);
2876 TCGv_i64 tcg_rd = setflags ? cpu_reg(s, rd) : cpu_reg_sp(s, rd);
2877 TCGv_i64 tcg_result;
2879 switch (shift) {
2880 case 0x0:
2881 break;
2882 case 0x1:
2883 imm <<= 12;
2884 break;
2885 default:
2886 unallocated_encoding(s);
2887 return;
2890 tcg_result = tcg_temp_new_i64();
2891 if (!setflags) {
2892 if (sub_op) {
2893 tcg_gen_subi_i64(tcg_result, tcg_rn, imm);
2894 } else {
2895 tcg_gen_addi_i64(tcg_result, tcg_rn, imm);
2897 } else {
2898 TCGv_i64 tcg_imm = tcg_const_i64(imm);
2899 if (sub_op) {
2900 gen_sub_CC(is_64bit, tcg_result, tcg_rn, tcg_imm);
2901 } else {
2902 gen_add_CC(is_64bit, tcg_result, tcg_rn, tcg_imm);
2904 tcg_temp_free_i64(tcg_imm);
2907 if (is_64bit) {
2908 tcg_gen_mov_i64(tcg_rd, tcg_result);
2909 } else {
2910 tcg_gen_ext32u_i64(tcg_rd, tcg_result);
2913 tcg_temp_free_i64(tcg_result);
2916 /* The input should be a value in the bottom e bits (with higher
2917 * bits zero); returns that value replicated into every element
2918 * of size e in a 64 bit integer.
2920 static uint64_t bitfield_replicate(uint64_t mask, unsigned int e)
2922 assert(e != 0);
2923 while (e < 64) {
2924 mask |= mask << e;
2925 e *= 2;
2927 return mask;
2930 /* Return a value with the bottom len bits set (where 0 < len <= 64) */
2931 static inline uint64_t bitmask64(unsigned int length)
2933 assert(length > 0 && length <= 64);
2934 return ~0ULL >> (64 - length);
2937 /* Simplified variant of pseudocode DecodeBitMasks() for the case where we
2938 * only require the wmask. Returns false if the imms/immr/immn are a reserved
2939 * value (ie should cause a guest UNDEF exception), and true if they are
2940 * valid, in which case the decoded bit pattern is written to result.
2942 static bool logic_imm_decode_wmask(uint64_t *result, unsigned int immn,
2943 unsigned int imms, unsigned int immr)
2945 uint64_t mask;
2946 unsigned e, levels, s, r;
2947 int len;
2949 assert(immn < 2 && imms < 64 && immr < 64);
2951 /* The bit patterns we create here are 64 bit patterns which
2952 * are vectors of identical elements of size e = 2, 4, 8, 16, 32 or
2953 * 64 bits each. Each element contains the same value: a run
2954 * of between 1 and e-1 non-zero bits, rotated within the
2955 * element by between 0 and e-1 bits.
2957 * The element size and run length are encoded into immn (1 bit)
2958 * and imms (6 bits) as follows:
2959 * 64 bit elements: immn = 1, imms = <length of run - 1>
2960 * 32 bit elements: immn = 0, imms = 0 : <length of run - 1>
2961 * 16 bit elements: immn = 0, imms = 10 : <length of run - 1>
2962 * 8 bit elements: immn = 0, imms = 110 : <length of run - 1>
2963 * 4 bit elements: immn = 0, imms = 1110 : <length of run - 1>
2964 * 2 bit elements: immn = 0, imms = 11110 : <length of run - 1>
2965 * Notice that immn = 0, imms = 11111x is the only combination
2966 * not covered by one of the above options; this is reserved.
2967 * Further, <length of run - 1> all-ones is a reserved pattern.
2969 * In all cases the rotation is by immr % e (and immr is 6 bits).
2972 /* First determine the element size */
2973 len = 31 - clz32((immn << 6) | (~imms & 0x3f));
2974 if (len < 1) {
2975 /* This is the immn == 0, imms == 0x11111x case */
2976 return false;
2978 e = 1 << len;
2980 levels = e - 1;
2981 s = imms & levels;
2982 r = immr & levels;
2984 if (s == levels) {
2985 /* <length of run - 1> mustn't be all-ones. */
2986 return false;
2989 /* Create the value of one element: s+1 set bits rotated
2990 * by r within the element (which is e bits wide)...
2992 mask = bitmask64(s + 1);
2993 if (r) {
2994 mask = (mask >> r) | (mask << (e - r));
2995 mask &= bitmask64(e);
2997 /* ...then replicate the element over the whole 64 bit value */
2998 mask = bitfield_replicate(mask, e);
2999 *result = mask;
3000 return true;
3003 /* C3.4.4 Logical (immediate)
3004 * 31 30 29 28 23 22 21 16 15 10 9 5 4 0
3005 * +----+-----+-------------+---+------+------+------+------+
3006 * | sf | opc | 1 0 0 1 0 0 | N | immr | imms | Rn | Rd |
3007 * +----+-----+-------------+---+------+------+------+------+
3009 static void disas_logic_imm(DisasContext *s, uint32_t insn)
3011 unsigned int sf, opc, is_n, immr, imms, rn, rd;
3012 TCGv_i64 tcg_rd, tcg_rn;
3013 uint64_t wmask;
3014 bool is_and = false;
3016 sf = extract32(insn, 31, 1);
3017 opc = extract32(insn, 29, 2);
3018 is_n = extract32(insn, 22, 1);
3019 immr = extract32(insn, 16, 6);
3020 imms = extract32(insn, 10, 6);
3021 rn = extract32(insn, 5, 5);
3022 rd = extract32(insn, 0, 5);
3024 if (!sf && is_n) {
3025 unallocated_encoding(s);
3026 return;
3029 if (opc == 0x3) { /* ANDS */
3030 tcg_rd = cpu_reg(s, rd);
3031 } else {
3032 tcg_rd = cpu_reg_sp(s, rd);
3034 tcg_rn = cpu_reg(s, rn);
3036 if (!logic_imm_decode_wmask(&wmask, is_n, imms, immr)) {
3037 /* some immediate field values are reserved */
3038 unallocated_encoding(s);
3039 return;
3042 if (!sf) {
3043 wmask &= 0xffffffff;
3046 switch (opc) {
3047 case 0x3: /* ANDS */
3048 case 0x0: /* AND */
3049 tcg_gen_andi_i64(tcg_rd, tcg_rn, wmask);
3050 is_and = true;
3051 break;
3052 case 0x1: /* ORR */
3053 tcg_gen_ori_i64(tcg_rd, tcg_rn, wmask);
3054 break;
3055 case 0x2: /* EOR */
3056 tcg_gen_xori_i64(tcg_rd, tcg_rn, wmask);
3057 break;
3058 default:
3059 assert(FALSE); /* must handle all above */
3060 break;
3063 if (!sf && !is_and) {
3064 /* zero extend final result; we know we can skip this for AND
3065 * since the immediate had the high 32 bits clear.
3067 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3070 if (opc == 3) { /* ANDS */
3071 gen_logic_CC(sf, tcg_rd);
3076 * C3.4.5 Move wide (immediate)
3078 * 31 30 29 28 23 22 21 20 5 4 0
3079 * +--+-----+-------------+-----+----------------+------+
3080 * |sf| opc | 1 0 0 1 0 1 | hw | imm16 | Rd |
3081 * +--+-----+-------------+-----+----------------+------+
3083 * sf: 0 -> 32 bit, 1 -> 64 bit
3084 * opc: 00 -> N, 10 -> Z, 11 -> K
3085 * hw: shift/16 (0,16, and sf only 32, 48)
3087 static void disas_movw_imm(DisasContext *s, uint32_t insn)
3089 int rd = extract32(insn, 0, 5);
3090 uint64_t imm = extract32(insn, 5, 16);
3091 int sf = extract32(insn, 31, 1);
3092 int opc = extract32(insn, 29, 2);
3093 int pos = extract32(insn, 21, 2) << 4;
3094 TCGv_i64 tcg_rd = cpu_reg(s, rd);
3095 TCGv_i64 tcg_imm;
3097 if (!sf && (pos >= 32)) {
3098 unallocated_encoding(s);
3099 return;
3102 switch (opc) {
3103 case 0: /* MOVN */
3104 case 2: /* MOVZ */
3105 imm <<= pos;
3106 if (opc == 0) {
3107 imm = ~imm;
3109 if (!sf) {
3110 imm &= 0xffffffffu;
3112 tcg_gen_movi_i64(tcg_rd, imm);
3113 break;
3114 case 3: /* MOVK */
3115 tcg_imm = tcg_const_i64(imm);
3116 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_imm, pos, 16);
3117 tcg_temp_free_i64(tcg_imm);
3118 if (!sf) {
3119 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3121 break;
3122 default:
3123 unallocated_encoding(s);
3124 break;
3128 /* C3.4.2 Bitfield
3129 * 31 30 29 28 23 22 21 16 15 10 9 5 4 0
3130 * +----+-----+-------------+---+------+------+------+------+
3131 * | sf | opc | 1 0 0 1 1 0 | N | immr | imms | Rn | Rd |
3132 * +----+-----+-------------+---+------+------+------+------+
3134 static void disas_bitfield(DisasContext *s, uint32_t insn)
3136 unsigned int sf, n, opc, ri, si, rn, rd, bitsize, pos, len;
3137 TCGv_i64 tcg_rd, tcg_tmp;
3139 sf = extract32(insn, 31, 1);
3140 opc = extract32(insn, 29, 2);
3141 n = extract32(insn, 22, 1);
3142 ri = extract32(insn, 16, 6);
3143 si = extract32(insn, 10, 6);
3144 rn = extract32(insn, 5, 5);
3145 rd = extract32(insn, 0, 5);
3146 bitsize = sf ? 64 : 32;
3148 if (sf != n || ri >= bitsize || si >= bitsize || opc > 2) {
3149 unallocated_encoding(s);
3150 return;
3153 tcg_rd = cpu_reg(s, rd);
3155 /* Suppress the zero-extend for !sf. Since RI and SI are constrained
3156 to be smaller than bitsize, we'll never reference data outside the
3157 low 32-bits anyway. */
3158 tcg_tmp = read_cpu_reg(s, rn, 1);
3160 /* Recognize the common aliases. */
3161 if (opc == 0) { /* SBFM */
3162 if (ri == 0) {
3163 if (si == 7) { /* SXTB */
3164 tcg_gen_ext8s_i64(tcg_rd, tcg_tmp);
3165 goto done;
3166 } else if (si == 15) { /* SXTH */
3167 tcg_gen_ext16s_i64(tcg_rd, tcg_tmp);
3168 goto done;
3169 } else if (si == 31) { /* SXTW */
3170 tcg_gen_ext32s_i64(tcg_rd, tcg_tmp);
3171 goto done;
3174 if (si == 63 || (si == 31 && ri <= si)) { /* ASR */
3175 if (si == 31) {
3176 tcg_gen_ext32s_i64(tcg_tmp, tcg_tmp);
3178 tcg_gen_sari_i64(tcg_rd, tcg_tmp, ri);
3179 goto done;
3181 } else if (opc == 2) { /* UBFM */
3182 if (ri == 0) { /* UXTB, UXTH, plus non-canonical AND */
3183 tcg_gen_andi_i64(tcg_rd, tcg_tmp, bitmask64(si + 1));
3184 return;
3186 if (si == 63 || (si == 31 && ri <= si)) { /* LSR */
3187 if (si == 31) {
3188 tcg_gen_ext32u_i64(tcg_tmp, tcg_tmp);
3190 tcg_gen_shri_i64(tcg_rd, tcg_tmp, ri);
3191 return;
3193 if (si + 1 == ri && si != bitsize - 1) { /* LSL */
3194 int shift = bitsize - 1 - si;
3195 tcg_gen_shli_i64(tcg_rd, tcg_tmp, shift);
3196 goto done;
3200 if (opc != 1) { /* SBFM or UBFM */
3201 tcg_gen_movi_i64(tcg_rd, 0);
3204 /* do the bit move operation */
3205 if (si >= ri) {
3206 /* Wd<s-r:0> = Wn<s:r> */
3207 tcg_gen_shri_i64(tcg_tmp, tcg_tmp, ri);
3208 pos = 0;
3209 len = (si - ri) + 1;
3210 } else {
3211 /* Wd<32+s-r,32-r> = Wn<s:0> */
3212 pos = bitsize - ri;
3213 len = si + 1;
3216 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, pos, len);
3218 if (opc == 0) { /* SBFM - sign extend the destination field */
3219 tcg_gen_shli_i64(tcg_rd, tcg_rd, 64 - (pos + len));
3220 tcg_gen_sari_i64(tcg_rd, tcg_rd, 64 - (pos + len));
3223 done:
3224 if (!sf) { /* zero extend final result */
3225 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3229 /* C3.4.3 Extract
3230 * 31 30 29 28 23 22 21 20 16 15 10 9 5 4 0
3231 * +----+------+-------------+---+----+------+--------+------+------+
3232 * | sf | op21 | 1 0 0 1 1 1 | N | o0 | Rm | imms | Rn | Rd |
3233 * +----+------+-------------+---+----+------+--------+------+------+
3235 static void disas_extract(DisasContext *s, uint32_t insn)
3237 unsigned int sf, n, rm, imm, rn, rd, bitsize, op21, op0;
3239 sf = extract32(insn, 31, 1);
3240 n = extract32(insn, 22, 1);
3241 rm = extract32(insn, 16, 5);
3242 imm = extract32(insn, 10, 6);
3243 rn = extract32(insn, 5, 5);
3244 rd = extract32(insn, 0, 5);
3245 op21 = extract32(insn, 29, 2);
3246 op0 = extract32(insn, 21, 1);
3247 bitsize = sf ? 64 : 32;
3249 if (sf != n || op21 || op0 || imm >= bitsize) {
3250 unallocated_encoding(s);
3251 } else {
3252 TCGv_i64 tcg_rd, tcg_rm, tcg_rn;
3254 tcg_rd = cpu_reg(s, rd);
3256 if (unlikely(imm == 0)) {
3257 /* tcg shl_i32/shl_i64 is undefined for 32/64 bit shifts,
3258 * so an extract from bit 0 is a special case.
3260 if (sf) {
3261 tcg_gen_mov_i64(tcg_rd, cpu_reg(s, rm));
3262 } else {
3263 tcg_gen_ext32u_i64(tcg_rd, cpu_reg(s, rm));
3265 } else if (rm == rn) { /* ROR */
3266 tcg_rm = cpu_reg(s, rm);
3267 if (sf) {
3268 tcg_gen_rotri_i64(tcg_rd, tcg_rm, imm);
3269 } else {
3270 TCGv_i32 tmp = tcg_temp_new_i32();
3271 tcg_gen_extrl_i64_i32(tmp, tcg_rm);
3272 tcg_gen_rotri_i32(tmp, tmp, imm);
3273 tcg_gen_extu_i32_i64(tcg_rd, tmp);
3274 tcg_temp_free_i32(tmp);
3276 } else {
3277 tcg_rm = read_cpu_reg(s, rm, sf);
3278 tcg_rn = read_cpu_reg(s, rn, sf);
3279 tcg_gen_shri_i64(tcg_rm, tcg_rm, imm);
3280 tcg_gen_shli_i64(tcg_rn, tcg_rn, bitsize - imm);
3281 tcg_gen_or_i64(tcg_rd, tcg_rm, tcg_rn);
3282 if (!sf) {
3283 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3289 /* C3.4 Data processing - immediate */
3290 static void disas_data_proc_imm(DisasContext *s, uint32_t insn)
3292 switch (extract32(insn, 23, 6)) {
3293 case 0x20: case 0x21: /* PC-rel. addressing */
3294 disas_pc_rel_adr(s, insn);
3295 break;
3296 case 0x22: case 0x23: /* Add/subtract (immediate) */
3297 disas_add_sub_imm(s, insn);
3298 break;
3299 case 0x24: /* Logical (immediate) */
3300 disas_logic_imm(s, insn);
3301 break;
3302 case 0x25: /* Move wide (immediate) */
3303 disas_movw_imm(s, insn);
3304 break;
3305 case 0x26: /* Bitfield */
3306 disas_bitfield(s, insn);
3307 break;
3308 case 0x27: /* Extract */
3309 disas_extract(s, insn);
3310 break;
3311 default:
3312 unallocated_encoding(s);
3313 break;
3317 /* Shift a TCGv src by TCGv shift_amount, put result in dst.
3318 * Note that it is the caller's responsibility to ensure that the
3319 * shift amount is in range (ie 0..31 or 0..63) and provide the ARM
3320 * mandated semantics for out of range shifts.
3322 static void shift_reg(TCGv_i64 dst, TCGv_i64 src, int sf,
3323 enum a64_shift_type shift_type, TCGv_i64 shift_amount)
3325 switch (shift_type) {
3326 case A64_SHIFT_TYPE_LSL:
3327 tcg_gen_shl_i64(dst, src, shift_amount);
3328 break;
3329 case A64_SHIFT_TYPE_LSR:
3330 tcg_gen_shr_i64(dst, src, shift_amount);
3331 break;
3332 case A64_SHIFT_TYPE_ASR:
3333 if (!sf) {
3334 tcg_gen_ext32s_i64(dst, src);
3336 tcg_gen_sar_i64(dst, sf ? src : dst, shift_amount);
3337 break;
3338 case A64_SHIFT_TYPE_ROR:
3339 if (sf) {
3340 tcg_gen_rotr_i64(dst, src, shift_amount);
3341 } else {
3342 TCGv_i32 t0, t1;
3343 t0 = tcg_temp_new_i32();
3344 t1 = tcg_temp_new_i32();
3345 tcg_gen_extrl_i64_i32(t0, src);
3346 tcg_gen_extrl_i64_i32(t1, shift_amount);
3347 tcg_gen_rotr_i32(t0, t0, t1);
3348 tcg_gen_extu_i32_i64(dst, t0);
3349 tcg_temp_free_i32(t0);
3350 tcg_temp_free_i32(t1);
3352 break;
3353 default:
3354 assert(FALSE); /* all shift types should be handled */
3355 break;
3358 if (!sf) { /* zero extend final result */
3359 tcg_gen_ext32u_i64(dst, dst);
3363 /* Shift a TCGv src by immediate, put result in dst.
3364 * The shift amount must be in range (this should always be true as the
3365 * relevant instructions will UNDEF on bad shift immediates).
3367 static void shift_reg_imm(TCGv_i64 dst, TCGv_i64 src, int sf,
3368 enum a64_shift_type shift_type, unsigned int shift_i)
3370 assert(shift_i < (sf ? 64 : 32));
3372 if (shift_i == 0) {
3373 tcg_gen_mov_i64(dst, src);
3374 } else {
3375 TCGv_i64 shift_const;
3377 shift_const = tcg_const_i64(shift_i);
3378 shift_reg(dst, src, sf, shift_type, shift_const);
3379 tcg_temp_free_i64(shift_const);
3383 /* C3.5.10 Logical (shifted register)
3384 * 31 30 29 28 24 23 22 21 20 16 15 10 9 5 4 0
3385 * +----+-----+-----------+-------+---+------+--------+------+------+
3386 * | sf | opc | 0 1 0 1 0 | shift | N | Rm | imm6 | Rn | Rd |
3387 * +----+-----+-----------+-------+---+------+--------+------+------+
3389 static void disas_logic_reg(DisasContext *s, uint32_t insn)
3391 TCGv_i64 tcg_rd, tcg_rn, tcg_rm;
3392 unsigned int sf, opc, shift_type, invert, rm, shift_amount, rn, rd;
3394 sf = extract32(insn, 31, 1);
3395 opc = extract32(insn, 29, 2);
3396 shift_type = extract32(insn, 22, 2);
3397 invert = extract32(insn, 21, 1);
3398 rm = extract32(insn, 16, 5);
3399 shift_amount = extract32(insn, 10, 6);
3400 rn = extract32(insn, 5, 5);
3401 rd = extract32(insn, 0, 5);
3403 if (!sf && (shift_amount & (1 << 5))) {
3404 unallocated_encoding(s);
3405 return;
3408 tcg_rd = cpu_reg(s, rd);
3410 if (opc == 1 && shift_amount == 0 && shift_type == 0 && rn == 31) {
3411 /* Unshifted ORR and ORN with WZR/XZR is the standard encoding for
3412 * register-register MOV and MVN, so it is worth special casing.
3414 tcg_rm = cpu_reg(s, rm);
3415 if (invert) {
3416 tcg_gen_not_i64(tcg_rd, tcg_rm);
3417 if (!sf) {
3418 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3420 } else {
3421 if (sf) {
3422 tcg_gen_mov_i64(tcg_rd, tcg_rm);
3423 } else {
3424 tcg_gen_ext32u_i64(tcg_rd, tcg_rm);
3427 return;
3430 tcg_rm = read_cpu_reg(s, rm, sf);
3432 if (shift_amount) {
3433 shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, shift_amount);
3436 tcg_rn = cpu_reg(s, rn);
3438 switch (opc | (invert << 2)) {
3439 case 0: /* AND */
3440 case 3: /* ANDS */
3441 tcg_gen_and_i64(tcg_rd, tcg_rn, tcg_rm);
3442 break;
3443 case 1: /* ORR */
3444 tcg_gen_or_i64(tcg_rd, tcg_rn, tcg_rm);
3445 break;
3446 case 2: /* EOR */
3447 tcg_gen_xor_i64(tcg_rd, tcg_rn, tcg_rm);
3448 break;
3449 case 4: /* BIC */
3450 case 7: /* BICS */
3451 tcg_gen_andc_i64(tcg_rd, tcg_rn, tcg_rm);
3452 break;
3453 case 5: /* ORN */
3454 tcg_gen_orc_i64(tcg_rd, tcg_rn, tcg_rm);
3455 break;
3456 case 6: /* EON */
3457 tcg_gen_eqv_i64(tcg_rd, tcg_rn, tcg_rm);
3458 break;
3459 default:
3460 assert(FALSE);
3461 break;
3464 if (!sf) {
3465 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3468 if (opc == 3) {
3469 gen_logic_CC(sf, tcg_rd);
3474 * C3.5.1 Add/subtract (extended register)
3476 * 31|30|29|28 24|23 22|21|20 16|15 13|12 10|9 5|4 0|
3477 * +--+--+--+-----------+-----+--+-------+------+------+----+----+
3478 * |sf|op| S| 0 1 0 1 1 | opt | 1| Rm |option| imm3 | Rn | Rd |
3479 * +--+--+--+-----------+-----+--+-------+------+------+----+----+
3481 * sf: 0 -> 32bit, 1 -> 64bit
3482 * op: 0 -> add , 1 -> sub
3483 * S: 1 -> set flags
3484 * opt: 00
3485 * option: extension type (see DecodeRegExtend)
3486 * imm3: optional shift to Rm
3488 * Rd = Rn + LSL(extend(Rm), amount)
3490 static void disas_add_sub_ext_reg(DisasContext *s, uint32_t insn)
3492 int rd = extract32(insn, 0, 5);
3493 int rn = extract32(insn, 5, 5);
3494 int imm3 = extract32(insn, 10, 3);
3495 int option = extract32(insn, 13, 3);
3496 int rm = extract32(insn, 16, 5);
3497 bool setflags = extract32(insn, 29, 1);
3498 bool sub_op = extract32(insn, 30, 1);
3499 bool sf = extract32(insn, 31, 1);
3501 TCGv_i64 tcg_rm, tcg_rn; /* temps */
3502 TCGv_i64 tcg_rd;
3503 TCGv_i64 tcg_result;
3505 if (imm3 > 4) {
3506 unallocated_encoding(s);
3507 return;
3510 /* non-flag setting ops may use SP */
3511 if (!setflags) {
3512 tcg_rd = cpu_reg_sp(s, rd);
3513 } else {
3514 tcg_rd = cpu_reg(s, rd);
3516 tcg_rn = read_cpu_reg_sp(s, rn, sf);
3518 tcg_rm = read_cpu_reg(s, rm, sf);
3519 ext_and_shift_reg(tcg_rm, tcg_rm, option, imm3);
3521 tcg_result = tcg_temp_new_i64();
3523 if (!setflags) {
3524 if (sub_op) {
3525 tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
3526 } else {
3527 tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
3529 } else {
3530 if (sub_op) {
3531 gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
3532 } else {
3533 gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
3537 if (sf) {
3538 tcg_gen_mov_i64(tcg_rd, tcg_result);
3539 } else {
3540 tcg_gen_ext32u_i64(tcg_rd, tcg_result);
3543 tcg_temp_free_i64(tcg_result);
3547 * C3.5.2 Add/subtract (shifted register)
3549 * 31 30 29 28 24 23 22 21 20 16 15 10 9 5 4 0
3550 * +--+--+--+-----------+-----+--+-------+---------+------+------+
3551 * |sf|op| S| 0 1 0 1 1 |shift| 0| Rm | imm6 | Rn | Rd |
3552 * +--+--+--+-----------+-----+--+-------+---------+------+------+
3554 * sf: 0 -> 32bit, 1 -> 64bit
3555 * op: 0 -> add , 1 -> sub
3556 * S: 1 -> set flags
3557 * shift: 00 -> LSL, 01 -> LSR, 10 -> ASR, 11 -> RESERVED
3558 * imm6: Shift amount to apply to Rm before the add/sub
3560 static void disas_add_sub_reg(DisasContext *s, uint32_t insn)
3562 int rd = extract32(insn, 0, 5);
3563 int rn = extract32(insn, 5, 5);
3564 int imm6 = extract32(insn, 10, 6);
3565 int rm = extract32(insn, 16, 5);
3566 int shift_type = extract32(insn, 22, 2);
3567 bool setflags = extract32(insn, 29, 1);
3568 bool sub_op = extract32(insn, 30, 1);
3569 bool sf = extract32(insn, 31, 1);
3571 TCGv_i64 tcg_rd = cpu_reg(s, rd);
3572 TCGv_i64 tcg_rn, tcg_rm;
3573 TCGv_i64 tcg_result;
3575 if ((shift_type == 3) || (!sf && (imm6 > 31))) {
3576 unallocated_encoding(s);
3577 return;
3580 tcg_rn = read_cpu_reg(s, rn, sf);
3581 tcg_rm = read_cpu_reg(s, rm, sf);
3583 shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, imm6);
3585 tcg_result = tcg_temp_new_i64();
3587 if (!setflags) {
3588 if (sub_op) {
3589 tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
3590 } else {
3591 tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
3593 } else {
3594 if (sub_op) {
3595 gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
3596 } else {
3597 gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
3601 if (sf) {
3602 tcg_gen_mov_i64(tcg_rd, tcg_result);
3603 } else {
3604 tcg_gen_ext32u_i64(tcg_rd, tcg_result);
3607 tcg_temp_free_i64(tcg_result);
3610 /* C3.5.9 Data-processing (3 source)
3612 31 30 29 28 24 23 21 20 16 15 14 10 9 5 4 0
3613 +--+------+-----------+------+------+----+------+------+------+
3614 |sf| op54 | 1 1 0 1 1 | op31 | Rm | o0 | Ra | Rn | Rd |
3615 +--+------+-----------+------+------+----+------+------+------+
3618 static void disas_data_proc_3src(DisasContext *s, uint32_t insn)
3620 int rd = extract32(insn, 0, 5);
3621 int rn = extract32(insn, 5, 5);
3622 int ra = extract32(insn, 10, 5);
3623 int rm = extract32(insn, 16, 5);
3624 int op_id = (extract32(insn, 29, 3) << 4) |
3625 (extract32(insn, 21, 3) << 1) |
3626 extract32(insn, 15, 1);
3627 bool sf = extract32(insn, 31, 1);
3628 bool is_sub = extract32(op_id, 0, 1);
3629 bool is_high = extract32(op_id, 2, 1);
3630 bool is_signed = false;
3631 TCGv_i64 tcg_op1;
3632 TCGv_i64 tcg_op2;
3633 TCGv_i64 tcg_tmp;
3635 /* Note that op_id is sf:op54:op31:o0 so it includes the 32/64 size flag */
3636 switch (op_id) {
3637 case 0x42: /* SMADDL */
3638 case 0x43: /* SMSUBL */
3639 case 0x44: /* SMULH */
3640 is_signed = true;
3641 break;
3642 case 0x0: /* MADD (32bit) */
3643 case 0x1: /* MSUB (32bit) */
3644 case 0x40: /* MADD (64bit) */
3645 case 0x41: /* MSUB (64bit) */
3646 case 0x4a: /* UMADDL */
3647 case 0x4b: /* UMSUBL */
3648 case 0x4c: /* UMULH */
3649 break;
3650 default:
3651 unallocated_encoding(s);
3652 return;
3655 if (is_high) {
3656 TCGv_i64 low_bits = tcg_temp_new_i64(); /* low bits discarded */
3657 TCGv_i64 tcg_rd = cpu_reg(s, rd);
3658 TCGv_i64 tcg_rn = cpu_reg(s, rn);
3659 TCGv_i64 tcg_rm = cpu_reg(s, rm);
3661 if (is_signed) {
3662 tcg_gen_muls2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
3663 } else {
3664 tcg_gen_mulu2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
3667 tcg_temp_free_i64(low_bits);
3668 return;
3671 tcg_op1 = tcg_temp_new_i64();
3672 tcg_op2 = tcg_temp_new_i64();
3673 tcg_tmp = tcg_temp_new_i64();
3675 if (op_id < 0x42) {
3676 tcg_gen_mov_i64(tcg_op1, cpu_reg(s, rn));
3677 tcg_gen_mov_i64(tcg_op2, cpu_reg(s, rm));
3678 } else {
3679 if (is_signed) {
3680 tcg_gen_ext32s_i64(tcg_op1, cpu_reg(s, rn));
3681 tcg_gen_ext32s_i64(tcg_op2, cpu_reg(s, rm));
3682 } else {
3683 tcg_gen_ext32u_i64(tcg_op1, cpu_reg(s, rn));
3684 tcg_gen_ext32u_i64(tcg_op2, cpu_reg(s, rm));
3688 if (ra == 31 && !is_sub) {
3689 /* Special-case MADD with rA == XZR; it is the standard MUL alias */
3690 tcg_gen_mul_i64(cpu_reg(s, rd), tcg_op1, tcg_op2);
3691 } else {
3692 tcg_gen_mul_i64(tcg_tmp, tcg_op1, tcg_op2);
3693 if (is_sub) {
3694 tcg_gen_sub_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
3695 } else {
3696 tcg_gen_add_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
3700 if (!sf) {
3701 tcg_gen_ext32u_i64(cpu_reg(s, rd), cpu_reg(s, rd));
3704 tcg_temp_free_i64(tcg_op1);
3705 tcg_temp_free_i64(tcg_op2);
3706 tcg_temp_free_i64(tcg_tmp);
3709 /* C3.5.3 - Add/subtract (with carry)
3710 * 31 30 29 28 27 26 25 24 23 22 21 20 16 15 10 9 5 4 0
3711 * +--+--+--+------------------------+------+---------+------+-----+
3712 * |sf|op| S| 1 1 0 1 0 0 0 0 | rm | opcode2 | Rn | Rd |
3713 * +--+--+--+------------------------+------+---------+------+-----+
3714 * [000000]
3717 static void disas_adc_sbc(DisasContext *s, uint32_t insn)
3719 unsigned int sf, op, setflags, rm, rn, rd;
3720 TCGv_i64 tcg_y, tcg_rn, tcg_rd;
3722 if (extract32(insn, 10, 6) != 0) {
3723 unallocated_encoding(s);
3724 return;
3727 sf = extract32(insn, 31, 1);
3728 op = extract32(insn, 30, 1);
3729 setflags = extract32(insn, 29, 1);
3730 rm = extract32(insn, 16, 5);
3731 rn = extract32(insn, 5, 5);
3732 rd = extract32(insn, 0, 5);
3734 tcg_rd = cpu_reg(s, rd);
3735 tcg_rn = cpu_reg(s, rn);
3737 if (op) {
3738 tcg_y = new_tmp_a64(s);
3739 tcg_gen_not_i64(tcg_y, cpu_reg(s, rm));
3740 } else {
3741 tcg_y = cpu_reg(s, rm);
3744 if (setflags) {
3745 gen_adc_CC(sf, tcg_rd, tcg_rn, tcg_y);
3746 } else {
3747 gen_adc(sf, tcg_rd, tcg_rn, tcg_y);
3751 /* C3.5.4 - C3.5.5 Conditional compare (immediate / register)
3752 * 31 30 29 28 27 26 25 24 23 22 21 20 16 15 12 11 10 9 5 4 3 0
3753 * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
3754 * |sf|op| S| 1 1 0 1 0 0 1 0 |imm5/rm | cond |i/r |o2| Rn |o3|nzcv |
3755 * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
3756 * [1] y [0] [0]
3758 static void disas_cc(DisasContext *s, uint32_t insn)
3760 unsigned int sf, op, y, cond, rn, nzcv, is_imm;
3761 TCGv_i32 tcg_t0, tcg_t1, tcg_t2;
3762 TCGv_i64 tcg_tmp, tcg_y, tcg_rn;
3763 DisasCompare c;
3765 if (!extract32(insn, 29, 1)) {
3766 unallocated_encoding(s);
3767 return;
3769 if (insn & (1 << 10 | 1 << 4)) {
3770 unallocated_encoding(s);
3771 return;
3773 sf = extract32(insn, 31, 1);
3774 op = extract32(insn, 30, 1);
3775 is_imm = extract32(insn, 11, 1);
3776 y = extract32(insn, 16, 5); /* y = rm (reg) or imm5 (imm) */
3777 cond = extract32(insn, 12, 4);
3778 rn = extract32(insn, 5, 5);
3779 nzcv = extract32(insn, 0, 4);
3781 /* Set T0 = !COND. */
3782 tcg_t0 = tcg_temp_new_i32();
3783 arm_test_cc(&c, cond);
3784 tcg_gen_setcondi_i32(tcg_invert_cond(c.cond), tcg_t0, c.value, 0);
3785 arm_free_cc(&c);
3787 /* Load the arguments for the new comparison. */
3788 if (is_imm) {
3789 tcg_y = new_tmp_a64(s);
3790 tcg_gen_movi_i64(tcg_y, y);
3791 } else {
3792 tcg_y = cpu_reg(s, y);
3794 tcg_rn = cpu_reg(s, rn);
3796 /* Set the flags for the new comparison. */
3797 tcg_tmp = tcg_temp_new_i64();
3798 if (op) {
3799 gen_sub_CC(sf, tcg_tmp, tcg_rn, tcg_y);
3800 } else {
3801 gen_add_CC(sf, tcg_tmp, tcg_rn, tcg_y);
3803 tcg_temp_free_i64(tcg_tmp);
3805 /* If COND was false, force the flags to #nzcv. Compute two masks
3806 * to help with this: T1 = (COND ? 0 : -1), T2 = (COND ? -1 : 0).
3807 * For tcg hosts that support ANDC, we can make do with just T1.
3808 * In either case, allow the tcg optimizer to delete any unused mask.
3810 tcg_t1 = tcg_temp_new_i32();
3811 tcg_t2 = tcg_temp_new_i32();
3812 tcg_gen_neg_i32(tcg_t1, tcg_t0);
3813 tcg_gen_subi_i32(tcg_t2, tcg_t0, 1);
3815 if (nzcv & 8) { /* N */
3816 tcg_gen_or_i32(cpu_NF, cpu_NF, tcg_t1);
3817 } else {
3818 if (TCG_TARGET_HAS_andc_i32) {
3819 tcg_gen_andc_i32(cpu_NF, cpu_NF, tcg_t1);
3820 } else {
3821 tcg_gen_and_i32(cpu_NF, cpu_NF, tcg_t2);
3824 if (nzcv & 4) { /* Z */
3825 if (TCG_TARGET_HAS_andc_i32) {
3826 tcg_gen_andc_i32(cpu_ZF, cpu_ZF, tcg_t1);
3827 } else {
3828 tcg_gen_and_i32(cpu_ZF, cpu_ZF, tcg_t2);
3830 } else {
3831 tcg_gen_or_i32(cpu_ZF, cpu_ZF, tcg_t0);
3833 if (nzcv & 2) { /* C */
3834 tcg_gen_or_i32(cpu_CF, cpu_CF, tcg_t0);
3835 } else {
3836 if (TCG_TARGET_HAS_andc_i32) {
3837 tcg_gen_andc_i32(cpu_CF, cpu_CF, tcg_t1);
3838 } else {
3839 tcg_gen_and_i32(cpu_CF, cpu_CF, tcg_t2);
3842 if (nzcv & 1) { /* V */
3843 tcg_gen_or_i32(cpu_VF, cpu_VF, tcg_t1);
3844 } else {
3845 if (TCG_TARGET_HAS_andc_i32) {
3846 tcg_gen_andc_i32(cpu_VF, cpu_VF, tcg_t1);
3847 } else {
3848 tcg_gen_and_i32(cpu_VF, cpu_VF, tcg_t2);
3851 tcg_temp_free_i32(tcg_t0);
3852 tcg_temp_free_i32(tcg_t1);
3853 tcg_temp_free_i32(tcg_t2);
3856 /* C3.5.6 Conditional select
3857 * 31 30 29 28 21 20 16 15 12 11 10 9 5 4 0
3858 * +----+----+---+-----------------+------+------+-----+------+------+
3859 * | sf | op | S | 1 1 0 1 0 1 0 0 | Rm | cond | op2 | Rn | Rd |
3860 * +----+----+---+-----------------+------+------+-----+------+------+
3862 static void disas_cond_select(DisasContext *s, uint32_t insn)
3864 unsigned int sf, else_inv, rm, cond, else_inc, rn, rd;
3865 TCGv_i64 tcg_rd, zero;
3866 DisasCompare64 c;
3868 if (extract32(insn, 29, 1) || extract32(insn, 11, 1)) {
3869 /* S == 1 or op2<1> == 1 */
3870 unallocated_encoding(s);
3871 return;
3873 sf = extract32(insn, 31, 1);
3874 else_inv = extract32(insn, 30, 1);
3875 rm = extract32(insn, 16, 5);
3876 cond = extract32(insn, 12, 4);
3877 else_inc = extract32(insn, 10, 1);
3878 rn = extract32(insn, 5, 5);
3879 rd = extract32(insn, 0, 5);
3881 tcg_rd = cpu_reg(s, rd);
3883 a64_test_cc(&c, cond);
3884 zero = tcg_const_i64(0);
3886 if (rn == 31 && rm == 31 && (else_inc ^ else_inv)) {
3887 /* CSET & CSETM. */
3888 tcg_gen_setcond_i64(tcg_invert_cond(c.cond), tcg_rd, c.value, zero);
3889 if (else_inv) {
3890 tcg_gen_neg_i64(tcg_rd, tcg_rd);
3892 } else {
3893 TCGv_i64 t_true = cpu_reg(s, rn);
3894 TCGv_i64 t_false = read_cpu_reg(s, rm, 1);
3895 if (else_inv && else_inc) {
3896 tcg_gen_neg_i64(t_false, t_false);
3897 } else if (else_inv) {
3898 tcg_gen_not_i64(t_false, t_false);
3899 } else if (else_inc) {
3900 tcg_gen_addi_i64(t_false, t_false, 1);
3902 tcg_gen_movcond_i64(c.cond, tcg_rd, c.value, zero, t_true, t_false);
3905 tcg_temp_free_i64(zero);
3906 a64_free_cc(&c);
3908 if (!sf) {
3909 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3913 static void handle_clz(DisasContext *s, unsigned int sf,
3914 unsigned int rn, unsigned int rd)
3916 TCGv_i64 tcg_rd, tcg_rn;
3917 tcg_rd = cpu_reg(s, rd);
3918 tcg_rn = cpu_reg(s, rn);
3920 if (sf) {
3921 gen_helper_clz64(tcg_rd, tcg_rn);
3922 } else {
3923 TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
3924 tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
3925 gen_helper_clz(tcg_tmp32, tcg_tmp32);
3926 tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
3927 tcg_temp_free_i32(tcg_tmp32);
3931 static void handle_cls(DisasContext *s, unsigned int sf,
3932 unsigned int rn, unsigned int rd)
3934 TCGv_i64 tcg_rd, tcg_rn;
3935 tcg_rd = cpu_reg(s, rd);
3936 tcg_rn = cpu_reg(s, rn);
3938 if (sf) {
3939 gen_helper_cls64(tcg_rd, tcg_rn);
3940 } else {
3941 TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
3942 tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
3943 gen_helper_cls32(tcg_tmp32, tcg_tmp32);
3944 tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
3945 tcg_temp_free_i32(tcg_tmp32);
3949 static void handle_rbit(DisasContext *s, unsigned int sf,
3950 unsigned int rn, unsigned int rd)
3952 TCGv_i64 tcg_rd, tcg_rn;
3953 tcg_rd = cpu_reg(s, rd);
3954 tcg_rn = cpu_reg(s, rn);
3956 if (sf) {
3957 gen_helper_rbit64(tcg_rd, tcg_rn);
3958 } else {
3959 TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
3960 tcg_gen_extrl_i64_i32(tcg_tmp32, tcg_rn);
3961 gen_helper_rbit(tcg_tmp32, tcg_tmp32);
3962 tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
3963 tcg_temp_free_i32(tcg_tmp32);
3967 /* C5.6.149 REV with sf==1, opcode==3 ("REV64") */
3968 static void handle_rev64(DisasContext *s, unsigned int sf,
3969 unsigned int rn, unsigned int rd)
3971 if (!sf) {
3972 unallocated_encoding(s);
3973 return;
3975 tcg_gen_bswap64_i64(cpu_reg(s, rd), cpu_reg(s, rn));
3978 /* C5.6.149 REV with sf==0, opcode==2
3979 * C5.6.151 REV32 (sf==1, opcode==2)
3981 static void handle_rev32(DisasContext *s, unsigned int sf,
3982 unsigned int rn, unsigned int rd)
3984 TCGv_i64 tcg_rd = cpu_reg(s, rd);
3986 if (sf) {
3987 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
3988 TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
3990 /* bswap32_i64 requires zero high word */
3991 tcg_gen_ext32u_i64(tcg_tmp, tcg_rn);
3992 tcg_gen_bswap32_i64(tcg_rd, tcg_tmp);
3993 tcg_gen_shri_i64(tcg_tmp, tcg_rn, 32);
3994 tcg_gen_bswap32_i64(tcg_tmp, tcg_tmp);
3995 tcg_gen_concat32_i64(tcg_rd, tcg_rd, tcg_tmp);
3997 tcg_temp_free_i64(tcg_tmp);
3998 } else {
3999 tcg_gen_ext32u_i64(tcg_rd, cpu_reg(s, rn));
4000 tcg_gen_bswap32_i64(tcg_rd, tcg_rd);
4004 /* C5.6.150 REV16 (opcode==1) */
4005 static void handle_rev16(DisasContext *s, unsigned int sf,
4006 unsigned int rn, unsigned int rd)
4008 TCGv_i64 tcg_rd = cpu_reg(s, rd);
4009 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
4010 TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
4012 tcg_gen_andi_i64(tcg_tmp, tcg_rn, 0xffff);
4013 tcg_gen_bswap16_i64(tcg_rd, tcg_tmp);
4015 tcg_gen_shri_i64(tcg_tmp, tcg_rn, 16);
4016 tcg_gen_andi_i64(tcg_tmp, tcg_tmp, 0xffff);
4017 tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp);
4018 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, 16, 16);
4020 if (sf) {
4021 tcg_gen_shri_i64(tcg_tmp, tcg_rn, 32);
4022 tcg_gen_andi_i64(tcg_tmp, tcg_tmp, 0xffff);
4023 tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp);
4024 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, 32, 16);
4026 tcg_gen_shri_i64(tcg_tmp, tcg_rn, 48);
4027 tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp);
4028 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, 48, 16);
4031 tcg_temp_free_i64(tcg_tmp);
4034 /* C3.5.7 Data-processing (1 source)
4035 * 31 30 29 28 21 20 16 15 10 9 5 4 0
4036 * +----+---+---+-----------------+---------+--------+------+------+
4037 * | sf | 1 | S | 1 1 0 1 0 1 1 0 | opcode2 | opcode | Rn | Rd |
4038 * +----+---+---+-----------------+---------+--------+------+------+
4040 static void disas_data_proc_1src(DisasContext *s, uint32_t insn)
4042 unsigned int sf, opcode, rn, rd;
4044 if (extract32(insn, 29, 1) || extract32(insn, 16, 5)) {
4045 unallocated_encoding(s);
4046 return;
4049 sf = extract32(insn, 31, 1);
4050 opcode = extract32(insn, 10, 6);
4051 rn = extract32(insn, 5, 5);
4052 rd = extract32(insn, 0, 5);
4054 switch (opcode) {
4055 case 0: /* RBIT */
4056 handle_rbit(s, sf, rn, rd);
4057 break;
4058 case 1: /* REV16 */
4059 handle_rev16(s, sf, rn, rd);
4060 break;
4061 case 2: /* REV32 */
4062 handle_rev32(s, sf, rn, rd);
4063 break;
4064 case 3: /* REV64 */
4065 handle_rev64(s, sf, rn, rd);
4066 break;
4067 case 4: /* CLZ */
4068 handle_clz(s, sf, rn, rd);
4069 break;
4070 case 5: /* CLS */
4071 handle_cls(s, sf, rn, rd);
4072 break;
4076 static void handle_div(DisasContext *s, bool is_signed, unsigned int sf,
4077 unsigned int rm, unsigned int rn, unsigned int rd)
4079 TCGv_i64 tcg_n, tcg_m, tcg_rd;
4080 tcg_rd = cpu_reg(s, rd);
4082 if (!sf && is_signed) {
4083 tcg_n = new_tmp_a64(s);
4084 tcg_m = new_tmp_a64(s);
4085 tcg_gen_ext32s_i64(tcg_n, cpu_reg(s, rn));
4086 tcg_gen_ext32s_i64(tcg_m, cpu_reg(s, rm));
4087 } else {
4088 tcg_n = read_cpu_reg(s, rn, sf);
4089 tcg_m = read_cpu_reg(s, rm, sf);
4092 if (is_signed) {
4093 gen_helper_sdiv64(tcg_rd, tcg_n, tcg_m);
4094 } else {
4095 gen_helper_udiv64(tcg_rd, tcg_n, tcg_m);
4098 if (!sf) { /* zero extend final result */
4099 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
4103 /* C5.6.115 LSLV, C5.6.118 LSRV, C5.6.17 ASRV, C5.6.154 RORV */
4104 static void handle_shift_reg(DisasContext *s,
4105 enum a64_shift_type shift_type, unsigned int sf,
4106 unsigned int rm, unsigned int rn, unsigned int rd)
4108 TCGv_i64 tcg_shift = tcg_temp_new_i64();
4109 TCGv_i64 tcg_rd = cpu_reg(s, rd);
4110 TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
4112 tcg_gen_andi_i64(tcg_shift, cpu_reg(s, rm), sf ? 63 : 31);
4113 shift_reg(tcg_rd, tcg_rn, sf, shift_type, tcg_shift);
4114 tcg_temp_free_i64(tcg_shift);
4117 /* CRC32[BHWX], CRC32C[BHWX] */
4118 static void handle_crc32(DisasContext *s,
4119 unsigned int sf, unsigned int sz, bool crc32c,
4120 unsigned int rm, unsigned int rn, unsigned int rd)
4122 TCGv_i64 tcg_acc, tcg_val;
4123 TCGv_i32 tcg_bytes;
4125 if (!arm_dc_feature(s, ARM_FEATURE_CRC)
4126 || (sf == 1 && sz != 3)
4127 || (sf == 0 && sz == 3)) {
4128 unallocated_encoding(s);
4129 return;
4132 if (sz == 3) {
4133 tcg_val = cpu_reg(s, rm);
4134 } else {
4135 uint64_t mask;
4136 switch (sz) {
4137 case 0:
4138 mask = 0xFF;
4139 break;
4140 case 1:
4141 mask = 0xFFFF;
4142 break;
4143 case 2:
4144 mask = 0xFFFFFFFF;
4145 break;
4146 default:
4147 g_assert_not_reached();
4149 tcg_val = new_tmp_a64(s);
4150 tcg_gen_andi_i64(tcg_val, cpu_reg(s, rm), mask);
4153 tcg_acc = cpu_reg(s, rn);
4154 tcg_bytes = tcg_const_i32(1 << sz);
4156 if (crc32c) {
4157 gen_helper_crc32c_64(cpu_reg(s, rd), tcg_acc, tcg_val, tcg_bytes);
4158 } else {
4159 gen_helper_crc32_64(cpu_reg(s, rd), tcg_acc, tcg_val, tcg_bytes);
4162 tcg_temp_free_i32(tcg_bytes);
4165 /* C3.5.8 Data-processing (2 source)
4166 * 31 30 29 28 21 20 16 15 10 9 5 4 0
4167 * +----+---+---+-----------------+------+--------+------+------+
4168 * | sf | 0 | S | 1 1 0 1 0 1 1 0 | Rm | opcode | Rn | Rd |
4169 * +----+---+---+-----------------+------+--------+------+------+
4171 static void disas_data_proc_2src(DisasContext *s, uint32_t insn)
4173 unsigned int sf, rm, opcode, rn, rd;
4174 sf = extract32(insn, 31, 1);
4175 rm = extract32(insn, 16, 5);
4176 opcode = extract32(insn, 10, 6);
4177 rn = extract32(insn, 5, 5);
4178 rd = extract32(insn, 0, 5);
4180 if (extract32(insn, 29, 1)) {
4181 unallocated_encoding(s);
4182 return;
4185 switch (opcode) {
4186 case 2: /* UDIV */
4187 handle_div(s, false, sf, rm, rn, rd);
4188 break;
4189 case 3: /* SDIV */
4190 handle_div(s, true, sf, rm, rn, rd);
4191 break;
4192 case 8: /* LSLV */
4193 handle_shift_reg(s, A64_SHIFT_TYPE_LSL, sf, rm, rn, rd);
4194 break;
4195 case 9: /* LSRV */
4196 handle_shift_reg(s, A64_SHIFT_TYPE_LSR, sf, rm, rn, rd);
4197 break;
4198 case 10: /* ASRV */
4199 handle_shift_reg(s, A64_SHIFT_TYPE_ASR, sf, rm, rn, rd);
4200 break;
4201 case 11: /* RORV */
4202 handle_shift_reg(s, A64_SHIFT_TYPE_ROR, sf, rm, rn, rd);
4203 break;
4204 case 16:
4205 case 17:
4206 case 18:
4207 case 19:
4208 case 20:
4209 case 21:
4210 case 22:
4211 case 23: /* CRC32 */
4213 int sz = extract32(opcode, 0, 2);
4214 bool crc32c = extract32(opcode, 2, 1);
4215 handle_crc32(s, sf, sz, crc32c, rm, rn, rd);
4216 break;
4218 default:
4219 unallocated_encoding(s);
4220 break;
4224 /* C3.5 Data processing - register */
4225 static void disas_data_proc_reg(DisasContext *s, uint32_t insn)
4227 switch (extract32(insn, 24, 5)) {
4228 case 0x0a: /* Logical (shifted register) */
4229 disas_logic_reg(s, insn);
4230 break;
4231 case 0x0b: /* Add/subtract */
4232 if (insn & (1 << 21)) { /* (extended register) */
4233 disas_add_sub_ext_reg(s, insn);
4234 } else {
4235 disas_add_sub_reg(s, insn);
4237 break;
4238 case 0x1b: /* Data-processing (3 source) */
4239 disas_data_proc_3src(s, insn);
4240 break;
4241 case 0x1a:
4242 switch (extract32(insn, 21, 3)) {
4243 case 0x0: /* Add/subtract (with carry) */
4244 disas_adc_sbc(s, insn);
4245 break;
4246 case 0x2: /* Conditional compare */
4247 disas_cc(s, insn); /* both imm and reg forms */
4248 break;
4249 case 0x4: /* Conditional select */
4250 disas_cond_select(s, insn);
4251 break;
4252 case 0x6: /* Data-processing */
4253 if (insn & (1 << 30)) { /* (1 source) */
4254 disas_data_proc_1src(s, insn);
4255 } else { /* (2 source) */
4256 disas_data_proc_2src(s, insn);
4258 break;
4259 default:
4260 unallocated_encoding(s);
4261 break;
4263 break;
4264 default:
4265 unallocated_encoding(s);
4266 break;
4270 static void handle_fp_compare(DisasContext *s, bool is_double,
4271 unsigned int rn, unsigned int rm,
4272 bool cmp_with_zero, bool signal_all_nans)
4274 TCGv_i64 tcg_flags = tcg_temp_new_i64();
4275 TCGv_ptr fpst = get_fpstatus_ptr();
4277 if (is_double) {
4278 TCGv_i64 tcg_vn, tcg_vm;
4280 tcg_vn = read_fp_dreg(s, rn);
4281 if (cmp_with_zero) {
4282 tcg_vm = tcg_const_i64(0);
4283 } else {
4284 tcg_vm = read_fp_dreg(s, rm);
4286 if (signal_all_nans) {
4287 gen_helper_vfp_cmped_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
4288 } else {
4289 gen_helper_vfp_cmpd_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
4291 tcg_temp_free_i64(tcg_vn);
4292 tcg_temp_free_i64(tcg_vm);
4293 } else {
4294 TCGv_i32 tcg_vn, tcg_vm;
4296 tcg_vn = read_fp_sreg(s, rn);
4297 if (cmp_with_zero) {
4298 tcg_vm = tcg_const_i32(0);
4299 } else {
4300 tcg_vm = read_fp_sreg(s, rm);
4302 if (signal_all_nans) {
4303 gen_helper_vfp_cmpes_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
4304 } else {
4305 gen_helper_vfp_cmps_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
4307 tcg_temp_free_i32(tcg_vn);
4308 tcg_temp_free_i32(tcg_vm);
4311 tcg_temp_free_ptr(fpst);
4313 gen_set_nzcv(tcg_flags);
4315 tcg_temp_free_i64(tcg_flags);
4318 /* C3.6.22 Floating point compare
4319 * 31 30 29 28 24 23 22 21 20 16 15 14 13 10 9 5 4 0
4320 * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
4321 * | M | 0 | S | 1 1 1 1 0 | type | 1 | Rm | op | 1 0 0 0 | Rn | op2 |
4322 * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
4324 static void disas_fp_compare(DisasContext *s, uint32_t insn)
4326 unsigned int mos, type, rm, op, rn, opc, op2r;
4328 mos = extract32(insn, 29, 3);
4329 type = extract32(insn, 22, 2); /* 0 = single, 1 = double */
4330 rm = extract32(insn, 16, 5);
4331 op = extract32(insn, 14, 2);
4332 rn = extract32(insn, 5, 5);
4333 opc = extract32(insn, 3, 2);
4334 op2r = extract32(insn, 0, 3);
4336 if (mos || op || op2r || type > 1) {
4337 unallocated_encoding(s);
4338 return;
4341 if (!fp_access_check(s)) {
4342 return;
4345 handle_fp_compare(s, type, rn, rm, opc & 1, opc & 2);
4348 /* C3.6.23 Floating point conditional compare
4349 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 3 0
4350 * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
4351 * | M | 0 | S | 1 1 1 1 0 | type | 1 | Rm | cond | 0 1 | Rn | op | nzcv |
4352 * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
4354 static void disas_fp_ccomp(DisasContext *s, uint32_t insn)
4356 unsigned int mos, type, rm, cond, rn, op, nzcv;
4357 TCGv_i64 tcg_flags;
4358 TCGLabel *label_continue = NULL;
4360 mos = extract32(insn, 29, 3);
4361 type = extract32(insn, 22, 2); /* 0 = single, 1 = double */
4362 rm = extract32(insn, 16, 5);
4363 cond = extract32(insn, 12, 4);
4364 rn = extract32(insn, 5, 5);
4365 op = extract32(insn, 4, 1);
4366 nzcv = extract32(insn, 0, 4);
4368 if (mos || type > 1) {
4369 unallocated_encoding(s);
4370 return;
4373 if (!fp_access_check(s)) {
4374 return;
4377 if (cond < 0x0e) { /* not always */
4378 TCGLabel *label_match = gen_new_label();
4379 label_continue = gen_new_label();
4380 arm_gen_test_cc(cond, label_match);
4381 /* nomatch: */
4382 tcg_flags = tcg_const_i64(nzcv << 28);
4383 gen_set_nzcv(tcg_flags);
4384 tcg_temp_free_i64(tcg_flags);
4385 tcg_gen_br(label_continue);
4386 gen_set_label(label_match);
4389 handle_fp_compare(s, type, rn, rm, false, op);
4391 if (cond < 0x0e) {
4392 gen_set_label(label_continue);
4396 /* C3.6.24 Floating point conditional select
4397 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 0
4398 * +---+---+---+-----------+------+---+------+------+-----+------+------+
4399 * | M | 0 | S | 1 1 1 1 0 | type | 1 | Rm | cond | 1 1 | Rn | Rd |
4400 * +---+---+---+-----------+------+---+------+------+-----+------+------+
4402 static void disas_fp_csel(DisasContext *s, uint32_t insn)
4404 unsigned int mos, type, rm, cond, rn, rd;
4405 TCGv_i64 t_true, t_false, t_zero;
4406 DisasCompare64 c;
4408 mos = extract32(insn, 29, 3);
4409 type = extract32(insn, 22, 2); /* 0 = single, 1 = double */
4410 rm = extract32(insn, 16, 5);
4411 cond = extract32(insn, 12, 4);
4412 rn = extract32(insn, 5, 5);
4413 rd = extract32(insn, 0, 5);
4415 if (mos || type > 1) {
4416 unallocated_encoding(s);
4417 return;
4420 if (!fp_access_check(s)) {
4421 return;
4424 /* Zero extend sreg inputs to 64 bits now. */
4425 t_true = tcg_temp_new_i64();
4426 t_false = tcg_temp_new_i64();
4427 read_vec_element(s, t_true, rn, 0, type ? MO_64 : MO_32);
4428 read_vec_element(s, t_false, rm, 0, type ? MO_64 : MO_32);
4430 a64_test_cc(&c, cond);
4431 t_zero = tcg_const_i64(0);
4432 tcg_gen_movcond_i64(c.cond, t_true, c.value, t_zero, t_true, t_false);
4433 tcg_temp_free_i64(t_zero);
4434 tcg_temp_free_i64(t_false);
4435 a64_free_cc(&c);
4437 /* Note that sregs write back zeros to the high bits,
4438 and we've already done the zero-extension. */
4439 write_fp_dreg(s, rd, t_true);
4440 tcg_temp_free_i64(t_true);
4443 /* C3.6.25 Floating-point data-processing (1 source) - single precision */
4444 static void handle_fp_1src_single(DisasContext *s, int opcode, int rd, int rn)
4446 TCGv_ptr fpst;
4447 TCGv_i32 tcg_op;
4448 TCGv_i32 tcg_res;
4450 fpst = get_fpstatus_ptr();
4451 tcg_op = read_fp_sreg(s, rn);
4452 tcg_res = tcg_temp_new_i32();
4454 switch (opcode) {
4455 case 0x0: /* FMOV */
4456 tcg_gen_mov_i32(tcg_res, tcg_op);
4457 break;
4458 case 0x1: /* FABS */
4459 gen_helper_vfp_abss(tcg_res, tcg_op);
4460 break;
4461 case 0x2: /* FNEG */
4462 gen_helper_vfp_negs(tcg_res, tcg_op);
4463 break;
4464 case 0x3: /* FSQRT */
4465 gen_helper_vfp_sqrts(tcg_res, tcg_op, cpu_env);
4466 break;
4467 case 0x8: /* FRINTN */
4468 case 0x9: /* FRINTP */
4469 case 0xa: /* FRINTM */
4470 case 0xb: /* FRINTZ */
4471 case 0xc: /* FRINTA */
4473 TCGv_i32 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(opcode & 7));
4475 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
4476 gen_helper_rints(tcg_res, tcg_op, fpst);
4478 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
4479 tcg_temp_free_i32(tcg_rmode);
4480 break;
4482 case 0xe: /* FRINTX */
4483 gen_helper_rints_exact(tcg_res, tcg_op, fpst);
4484 break;
4485 case 0xf: /* FRINTI */
4486 gen_helper_rints(tcg_res, tcg_op, fpst);
4487 break;
4488 default:
4489 abort();
4492 write_fp_sreg(s, rd, tcg_res);
4494 tcg_temp_free_ptr(fpst);
4495 tcg_temp_free_i32(tcg_op);
4496 tcg_temp_free_i32(tcg_res);
4499 /* C3.6.25 Floating-point data-processing (1 source) - double precision */
4500 static void handle_fp_1src_double(DisasContext *s, int opcode, int rd, int rn)
4502 TCGv_ptr fpst;
4503 TCGv_i64 tcg_op;
4504 TCGv_i64 tcg_res;
4506 fpst = get_fpstatus_ptr();
4507 tcg_op = read_fp_dreg(s, rn);
4508 tcg_res = tcg_temp_new_i64();
4510 switch (opcode) {
4511 case 0x0: /* FMOV */
4512 tcg_gen_mov_i64(tcg_res, tcg_op);
4513 break;
4514 case 0x1: /* FABS */
4515 gen_helper_vfp_absd(tcg_res, tcg_op);
4516 break;
4517 case 0x2: /* FNEG */
4518 gen_helper_vfp_negd(tcg_res, tcg_op);
4519 break;
4520 case 0x3: /* FSQRT */
4521 gen_helper_vfp_sqrtd(tcg_res, tcg_op, cpu_env);
4522 break;
4523 case 0x8: /* FRINTN */
4524 case 0x9: /* FRINTP */
4525 case 0xa: /* FRINTM */
4526 case 0xb: /* FRINTZ */
4527 case 0xc: /* FRINTA */
4529 TCGv_i32 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(opcode & 7));
4531 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
4532 gen_helper_rintd(tcg_res, tcg_op, fpst);
4534 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
4535 tcg_temp_free_i32(tcg_rmode);
4536 break;
4538 case 0xe: /* FRINTX */
4539 gen_helper_rintd_exact(tcg_res, tcg_op, fpst);
4540 break;
4541 case 0xf: /* FRINTI */
4542 gen_helper_rintd(tcg_res, tcg_op, fpst);
4543 break;
4544 default:
4545 abort();
4548 write_fp_dreg(s, rd, tcg_res);
4550 tcg_temp_free_ptr(fpst);
4551 tcg_temp_free_i64(tcg_op);
4552 tcg_temp_free_i64(tcg_res);
4555 static void handle_fp_fcvt(DisasContext *s, int opcode,
4556 int rd, int rn, int dtype, int ntype)
4558 switch (ntype) {
4559 case 0x0:
4561 TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
4562 if (dtype == 1) {
4563 /* Single to double */
4564 TCGv_i64 tcg_rd = tcg_temp_new_i64();
4565 gen_helper_vfp_fcvtds(tcg_rd, tcg_rn, cpu_env);
4566 write_fp_dreg(s, rd, tcg_rd);
4567 tcg_temp_free_i64(tcg_rd);
4568 } else {
4569 /* Single to half */
4570 TCGv_i32 tcg_rd = tcg_temp_new_i32();
4571 gen_helper_vfp_fcvt_f32_to_f16(tcg_rd, tcg_rn, cpu_env);
4572 /* write_fp_sreg is OK here because top half of tcg_rd is zero */
4573 write_fp_sreg(s, rd, tcg_rd);
4574 tcg_temp_free_i32(tcg_rd);
4576 tcg_temp_free_i32(tcg_rn);
4577 break;
4579 case 0x1:
4581 TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
4582 TCGv_i32 tcg_rd = tcg_temp_new_i32();
4583 if (dtype == 0) {
4584 /* Double to single */
4585 gen_helper_vfp_fcvtsd(tcg_rd, tcg_rn, cpu_env);
4586 } else {
4587 /* Double to half */
4588 gen_helper_vfp_fcvt_f64_to_f16(tcg_rd, tcg_rn, cpu_env);
4589 /* write_fp_sreg is OK here because top half of tcg_rd is zero */
4591 write_fp_sreg(s, rd, tcg_rd);
4592 tcg_temp_free_i32(tcg_rd);
4593 tcg_temp_free_i64(tcg_rn);
4594 break;
4596 case 0x3:
4598 TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
4599 tcg_gen_ext16u_i32(tcg_rn, tcg_rn);
4600 if (dtype == 0) {
4601 /* Half to single */
4602 TCGv_i32 tcg_rd = tcg_temp_new_i32();
4603 gen_helper_vfp_fcvt_f16_to_f32(tcg_rd, tcg_rn, cpu_env);
4604 write_fp_sreg(s, rd, tcg_rd);
4605 tcg_temp_free_i32(tcg_rd);
4606 } else {
4607 /* Half to double */
4608 TCGv_i64 tcg_rd = tcg_temp_new_i64();
4609 gen_helper_vfp_fcvt_f16_to_f64(tcg_rd, tcg_rn, cpu_env);
4610 write_fp_dreg(s, rd, tcg_rd);
4611 tcg_temp_free_i64(tcg_rd);
4613 tcg_temp_free_i32(tcg_rn);
4614 break;
4616 default:
4617 abort();
4621 /* C3.6.25 Floating point data-processing (1 source)
4622 * 31 30 29 28 24 23 22 21 20 15 14 10 9 5 4 0
4623 * +---+---+---+-----------+------+---+--------+-----------+------+------+
4624 * | M | 0 | S | 1 1 1 1 0 | type | 1 | opcode | 1 0 0 0 0 | Rn | Rd |
4625 * +---+---+---+-----------+------+---+--------+-----------+------+------+
4627 static void disas_fp_1src(DisasContext *s, uint32_t insn)
4629 int type = extract32(insn, 22, 2);
4630 int opcode = extract32(insn, 15, 6);
4631 int rn = extract32(insn, 5, 5);
4632 int rd = extract32(insn, 0, 5);
4634 switch (opcode) {
4635 case 0x4: case 0x5: case 0x7:
4637 /* FCVT between half, single and double precision */
4638 int dtype = extract32(opcode, 0, 2);
4639 if (type == 2 || dtype == type) {
4640 unallocated_encoding(s);
4641 return;
4643 if (!fp_access_check(s)) {
4644 return;
4647 handle_fp_fcvt(s, opcode, rd, rn, dtype, type);
4648 break;
4650 case 0x0 ... 0x3:
4651 case 0x8 ... 0xc:
4652 case 0xe ... 0xf:
4653 /* 32-to-32 and 64-to-64 ops */
4654 switch (type) {
4655 case 0:
4656 if (!fp_access_check(s)) {
4657 return;
4660 handle_fp_1src_single(s, opcode, rd, rn);
4661 break;
4662 case 1:
4663 if (!fp_access_check(s)) {
4664 return;
4667 handle_fp_1src_double(s, opcode, rd, rn);
4668 break;
4669 default:
4670 unallocated_encoding(s);
4672 break;
4673 default:
4674 unallocated_encoding(s);
4675 break;
4679 /* C3.6.26 Floating-point data-processing (2 source) - single precision */
4680 static void handle_fp_2src_single(DisasContext *s, int opcode,
4681 int rd, int rn, int rm)
4683 TCGv_i32 tcg_op1;
4684 TCGv_i32 tcg_op2;
4685 TCGv_i32 tcg_res;
4686 TCGv_ptr fpst;
4688 tcg_res = tcg_temp_new_i32();
4689 fpst = get_fpstatus_ptr();
4690 tcg_op1 = read_fp_sreg(s, rn);
4691 tcg_op2 = read_fp_sreg(s, rm);
4693 switch (opcode) {
4694 case 0x0: /* FMUL */
4695 gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
4696 break;
4697 case 0x1: /* FDIV */
4698 gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
4699 break;
4700 case 0x2: /* FADD */
4701 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
4702 break;
4703 case 0x3: /* FSUB */
4704 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
4705 break;
4706 case 0x4: /* FMAX */
4707 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
4708 break;
4709 case 0x5: /* FMIN */
4710 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
4711 break;
4712 case 0x6: /* FMAXNM */
4713 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
4714 break;
4715 case 0x7: /* FMINNM */
4716 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
4717 break;
4718 case 0x8: /* FNMUL */
4719 gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
4720 gen_helper_vfp_negs(tcg_res, tcg_res);
4721 break;
4724 write_fp_sreg(s, rd, tcg_res);
4726 tcg_temp_free_ptr(fpst);
4727 tcg_temp_free_i32(tcg_op1);
4728 tcg_temp_free_i32(tcg_op2);
4729 tcg_temp_free_i32(tcg_res);
4732 /* C3.6.26 Floating-point data-processing (2 source) - double precision */
4733 static void handle_fp_2src_double(DisasContext *s, int opcode,
4734 int rd, int rn, int rm)
4736 TCGv_i64 tcg_op1;
4737 TCGv_i64 tcg_op2;
4738 TCGv_i64 tcg_res;
4739 TCGv_ptr fpst;
4741 tcg_res = tcg_temp_new_i64();
4742 fpst = get_fpstatus_ptr();
4743 tcg_op1 = read_fp_dreg(s, rn);
4744 tcg_op2 = read_fp_dreg(s, rm);
4746 switch (opcode) {
4747 case 0x0: /* FMUL */
4748 gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
4749 break;
4750 case 0x1: /* FDIV */
4751 gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
4752 break;
4753 case 0x2: /* FADD */
4754 gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
4755 break;
4756 case 0x3: /* FSUB */
4757 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
4758 break;
4759 case 0x4: /* FMAX */
4760 gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
4761 break;
4762 case 0x5: /* FMIN */
4763 gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
4764 break;
4765 case 0x6: /* FMAXNM */
4766 gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
4767 break;
4768 case 0x7: /* FMINNM */
4769 gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
4770 break;
4771 case 0x8: /* FNMUL */
4772 gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
4773 gen_helper_vfp_negd(tcg_res, tcg_res);
4774 break;
4777 write_fp_dreg(s, rd, tcg_res);
4779 tcg_temp_free_ptr(fpst);
4780 tcg_temp_free_i64(tcg_op1);
4781 tcg_temp_free_i64(tcg_op2);
4782 tcg_temp_free_i64(tcg_res);
4785 /* C3.6.26 Floating point data-processing (2 source)
4786 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 0
4787 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
4788 * | M | 0 | S | 1 1 1 1 0 | type | 1 | Rm | opcode | 1 0 | Rn | Rd |
4789 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
4791 static void disas_fp_2src(DisasContext *s, uint32_t insn)
4793 int type = extract32(insn, 22, 2);
4794 int rd = extract32(insn, 0, 5);
4795 int rn = extract32(insn, 5, 5);
4796 int rm = extract32(insn, 16, 5);
4797 int opcode = extract32(insn, 12, 4);
4799 if (opcode > 8) {
4800 unallocated_encoding(s);
4801 return;
4804 switch (type) {
4805 case 0:
4806 if (!fp_access_check(s)) {
4807 return;
4809 handle_fp_2src_single(s, opcode, rd, rn, rm);
4810 break;
4811 case 1:
4812 if (!fp_access_check(s)) {
4813 return;
4815 handle_fp_2src_double(s, opcode, rd, rn, rm);
4816 break;
4817 default:
4818 unallocated_encoding(s);
4822 /* C3.6.27 Floating-point data-processing (3 source) - single precision */
4823 static void handle_fp_3src_single(DisasContext *s, bool o0, bool o1,
4824 int rd, int rn, int rm, int ra)
4826 TCGv_i32 tcg_op1, tcg_op2, tcg_op3;
4827 TCGv_i32 tcg_res = tcg_temp_new_i32();
4828 TCGv_ptr fpst = get_fpstatus_ptr();
4830 tcg_op1 = read_fp_sreg(s, rn);
4831 tcg_op2 = read_fp_sreg(s, rm);
4832 tcg_op3 = read_fp_sreg(s, ra);
4834 /* These are fused multiply-add, and must be done as one
4835 * floating point operation with no rounding between the
4836 * multiplication and addition steps.
4837 * NB that doing the negations here as separate steps is
4838 * correct : an input NaN should come out with its sign bit
4839 * flipped if it is a negated-input.
4841 if (o1 == true) {
4842 gen_helper_vfp_negs(tcg_op3, tcg_op3);
4845 if (o0 != o1) {
4846 gen_helper_vfp_negs(tcg_op1, tcg_op1);
4849 gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
4851 write_fp_sreg(s, rd, tcg_res);
4853 tcg_temp_free_ptr(fpst);
4854 tcg_temp_free_i32(tcg_op1);
4855 tcg_temp_free_i32(tcg_op2);
4856 tcg_temp_free_i32(tcg_op3);
4857 tcg_temp_free_i32(tcg_res);
4860 /* C3.6.27 Floating-point data-processing (3 source) - double precision */
4861 static void handle_fp_3src_double(DisasContext *s, bool o0, bool o1,
4862 int rd, int rn, int rm, int ra)
4864 TCGv_i64 tcg_op1, tcg_op2, tcg_op3;
4865 TCGv_i64 tcg_res = tcg_temp_new_i64();
4866 TCGv_ptr fpst = get_fpstatus_ptr();
4868 tcg_op1 = read_fp_dreg(s, rn);
4869 tcg_op2 = read_fp_dreg(s, rm);
4870 tcg_op3 = read_fp_dreg(s, ra);
4872 /* These are fused multiply-add, and must be done as one
4873 * floating point operation with no rounding between the
4874 * multiplication and addition steps.
4875 * NB that doing the negations here as separate steps is
4876 * correct : an input NaN should come out with its sign bit
4877 * flipped if it is a negated-input.
4879 if (o1 == true) {
4880 gen_helper_vfp_negd(tcg_op3, tcg_op3);
4883 if (o0 != o1) {
4884 gen_helper_vfp_negd(tcg_op1, tcg_op1);
4887 gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
4889 write_fp_dreg(s, rd, tcg_res);
4891 tcg_temp_free_ptr(fpst);
4892 tcg_temp_free_i64(tcg_op1);
4893 tcg_temp_free_i64(tcg_op2);
4894 tcg_temp_free_i64(tcg_op3);
4895 tcg_temp_free_i64(tcg_res);
4898 /* C3.6.27 Floating point data-processing (3 source)
4899 * 31 30 29 28 24 23 22 21 20 16 15 14 10 9 5 4 0
4900 * +---+---+---+-----------+------+----+------+----+------+------+------+
4901 * | M | 0 | S | 1 1 1 1 1 | type | o1 | Rm | o0 | Ra | Rn | Rd |
4902 * +---+---+---+-----------+------+----+------+----+------+------+------+
4904 static void disas_fp_3src(DisasContext *s, uint32_t insn)
4906 int type = extract32(insn, 22, 2);
4907 int rd = extract32(insn, 0, 5);
4908 int rn = extract32(insn, 5, 5);
4909 int ra = extract32(insn, 10, 5);
4910 int rm = extract32(insn, 16, 5);
4911 bool o0 = extract32(insn, 15, 1);
4912 bool o1 = extract32(insn, 21, 1);
4914 switch (type) {
4915 case 0:
4916 if (!fp_access_check(s)) {
4917 return;
4919 handle_fp_3src_single(s, o0, o1, rd, rn, rm, ra);
4920 break;
4921 case 1:
4922 if (!fp_access_check(s)) {
4923 return;
4925 handle_fp_3src_double(s, o0, o1, rd, rn, rm, ra);
4926 break;
4927 default:
4928 unallocated_encoding(s);
4932 /* C3.6.28 Floating point immediate
4933 * 31 30 29 28 24 23 22 21 20 13 12 10 9 5 4 0
4934 * +---+---+---+-----------+------+---+------------+-------+------+------+
4935 * | M | 0 | S | 1 1 1 1 0 | type | 1 | imm8 | 1 0 0 | imm5 | Rd |
4936 * +---+---+---+-----------+------+---+------------+-------+------+------+
4938 static void disas_fp_imm(DisasContext *s, uint32_t insn)
4940 int rd = extract32(insn, 0, 5);
4941 int imm8 = extract32(insn, 13, 8);
4942 int is_double = extract32(insn, 22, 2);
4943 uint64_t imm;
4944 TCGv_i64 tcg_res;
4946 if (is_double > 1) {
4947 unallocated_encoding(s);
4948 return;
4951 if (!fp_access_check(s)) {
4952 return;
4955 /* The imm8 encodes the sign bit, enough bits to represent
4956 * an exponent in the range 01....1xx to 10....0xx,
4957 * and the most significant 4 bits of the mantissa; see
4958 * VFPExpandImm() in the v8 ARM ARM.
4960 if (is_double) {
4961 imm = (extract32(imm8, 7, 1) ? 0x8000 : 0) |
4962 (extract32(imm8, 6, 1) ? 0x3fc0 : 0x4000) |
4963 extract32(imm8, 0, 6);
4964 imm <<= 48;
4965 } else {
4966 imm = (extract32(imm8, 7, 1) ? 0x8000 : 0) |
4967 (extract32(imm8, 6, 1) ? 0x3e00 : 0x4000) |
4968 (extract32(imm8, 0, 6) << 3);
4969 imm <<= 16;
4972 tcg_res = tcg_const_i64(imm);
4973 write_fp_dreg(s, rd, tcg_res);
4974 tcg_temp_free_i64(tcg_res);
4977 /* Handle floating point <=> fixed point conversions. Note that we can
4978 * also deal with fp <=> integer conversions as a special case (scale == 64)
4979 * OPTME: consider handling that special case specially or at least skipping
4980 * the call to scalbn in the helpers for zero shifts.
4982 static void handle_fpfpcvt(DisasContext *s, int rd, int rn, int opcode,
4983 bool itof, int rmode, int scale, int sf, int type)
4985 bool is_signed = !(opcode & 1);
4986 bool is_double = type;
4987 TCGv_ptr tcg_fpstatus;
4988 TCGv_i32 tcg_shift;
4990 tcg_fpstatus = get_fpstatus_ptr();
4992 tcg_shift = tcg_const_i32(64 - scale);
4994 if (itof) {
4995 TCGv_i64 tcg_int = cpu_reg(s, rn);
4996 if (!sf) {
4997 TCGv_i64 tcg_extend = new_tmp_a64(s);
4999 if (is_signed) {
5000 tcg_gen_ext32s_i64(tcg_extend, tcg_int);
5001 } else {
5002 tcg_gen_ext32u_i64(tcg_extend, tcg_int);
5005 tcg_int = tcg_extend;
5008 if (is_double) {
5009 TCGv_i64 tcg_double = tcg_temp_new_i64();
5010 if (is_signed) {
5011 gen_helper_vfp_sqtod(tcg_double, tcg_int,
5012 tcg_shift, tcg_fpstatus);
5013 } else {
5014 gen_helper_vfp_uqtod(tcg_double, tcg_int,
5015 tcg_shift, tcg_fpstatus);
5017 write_fp_dreg(s, rd, tcg_double);
5018 tcg_temp_free_i64(tcg_double);
5019 } else {
5020 TCGv_i32 tcg_single = tcg_temp_new_i32();
5021 if (is_signed) {
5022 gen_helper_vfp_sqtos(tcg_single, tcg_int,
5023 tcg_shift, tcg_fpstatus);
5024 } else {
5025 gen_helper_vfp_uqtos(tcg_single, tcg_int,
5026 tcg_shift, tcg_fpstatus);
5028 write_fp_sreg(s, rd, tcg_single);
5029 tcg_temp_free_i32(tcg_single);
5031 } else {
5032 TCGv_i64 tcg_int = cpu_reg(s, rd);
5033 TCGv_i32 tcg_rmode;
5035 if (extract32(opcode, 2, 1)) {
5036 /* There are too many rounding modes to all fit into rmode,
5037 * so FCVTA[US] is a special case.
5039 rmode = FPROUNDING_TIEAWAY;
5042 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rmode));
5044 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
5046 if (is_double) {
5047 TCGv_i64 tcg_double = read_fp_dreg(s, rn);
5048 if (is_signed) {
5049 if (!sf) {
5050 gen_helper_vfp_tosld(tcg_int, tcg_double,
5051 tcg_shift, tcg_fpstatus);
5052 } else {
5053 gen_helper_vfp_tosqd(tcg_int, tcg_double,
5054 tcg_shift, tcg_fpstatus);
5056 } else {
5057 if (!sf) {
5058 gen_helper_vfp_tould(tcg_int, tcg_double,
5059 tcg_shift, tcg_fpstatus);
5060 } else {
5061 gen_helper_vfp_touqd(tcg_int, tcg_double,
5062 tcg_shift, tcg_fpstatus);
5065 tcg_temp_free_i64(tcg_double);
5066 } else {
5067 TCGv_i32 tcg_single = read_fp_sreg(s, rn);
5068 if (sf) {
5069 if (is_signed) {
5070 gen_helper_vfp_tosqs(tcg_int, tcg_single,
5071 tcg_shift, tcg_fpstatus);
5072 } else {
5073 gen_helper_vfp_touqs(tcg_int, tcg_single,
5074 tcg_shift, tcg_fpstatus);
5076 } else {
5077 TCGv_i32 tcg_dest = tcg_temp_new_i32();
5078 if (is_signed) {
5079 gen_helper_vfp_tosls(tcg_dest, tcg_single,
5080 tcg_shift, tcg_fpstatus);
5081 } else {
5082 gen_helper_vfp_touls(tcg_dest, tcg_single,
5083 tcg_shift, tcg_fpstatus);
5085 tcg_gen_extu_i32_i64(tcg_int, tcg_dest);
5086 tcg_temp_free_i32(tcg_dest);
5088 tcg_temp_free_i32(tcg_single);
5091 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
5092 tcg_temp_free_i32(tcg_rmode);
5094 if (!sf) {
5095 tcg_gen_ext32u_i64(tcg_int, tcg_int);
5099 tcg_temp_free_ptr(tcg_fpstatus);
5100 tcg_temp_free_i32(tcg_shift);
5103 /* C3.6.29 Floating point <-> fixed point conversions
5104 * 31 30 29 28 24 23 22 21 20 19 18 16 15 10 9 5 4 0
5105 * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
5106 * | sf | 0 | S | 1 1 1 1 0 | type | 0 | rmode | opcode | scale | Rn | Rd |
5107 * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
5109 static void disas_fp_fixed_conv(DisasContext *s, uint32_t insn)
5111 int rd = extract32(insn, 0, 5);
5112 int rn = extract32(insn, 5, 5);
5113 int scale = extract32(insn, 10, 6);
5114 int opcode = extract32(insn, 16, 3);
5115 int rmode = extract32(insn, 19, 2);
5116 int type = extract32(insn, 22, 2);
5117 bool sbit = extract32(insn, 29, 1);
5118 bool sf = extract32(insn, 31, 1);
5119 bool itof;
5121 if (sbit || (type > 1)
5122 || (!sf && scale < 32)) {
5123 unallocated_encoding(s);
5124 return;
5127 switch ((rmode << 3) | opcode) {
5128 case 0x2: /* SCVTF */
5129 case 0x3: /* UCVTF */
5130 itof = true;
5131 break;
5132 case 0x18: /* FCVTZS */
5133 case 0x19: /* FCVTZU */
5134 itof = false;
5135 break;
5136 default:
5137 unallocated_encoding(s);
5138 return;
5141 if (!fp_access_check(s)) {
5142 return;
5145 handle_fpfpcvt(s, rd, rn, opcode, itof, FPROUNDING_ZERO, scale, sf, type);
5148 static void handle_fmov(DisasContext *s, int rd, int rn, int type, bool itof)
5150 /* FMOV: gpr to or from float, double, or top half of quad fp reg,
5151 * without conversion.
5154 if (itof) {
5155 TCGv_i64 tcg_rn = cpu_reg(s, rn);
5157 switch (type) {
5158 case 0:
5160 /* 32 bit */
5161 TCGv_i64 tmp = tcg_temp_new_i64();
5162 tcg_gen_ext32u_i64(tmp, tcg_rn);
5163 tcg_gen_st_i64(tmp, cpu_env, fp_reg_offset(s, rd, MO_64));
5164 tcg_gen_movi_i64(tmp, 0);
5165 tcg_gen_st_i64(tmp, cpu_env, fp_reg_hi_offset(s, rd));
5166 tcg_temp_free_i64(tmp);
5167 break;
5169 case 1:
5171 /* 64 bit */
5172 TCGv_i64 tmp = tcg_const_i64(0);
5173 tcg_gen_st_i64(tcg_rn, cpu_env, fp_reg_offset(s, rd, MO_64));
5174 tcg_gen_st_i64(tmp, cpu_env, fp_reg_hi_offset(s, rd));
5175 tcg_temp_free_i64(tmp);
5176 break;
5178 case 2:
5179 /* 64 bit to top half. */
5180 tcg_gen_st_i64(tcg_rn, cpu_env, fp_reg_hi_offset(s, rd));
5181 break;
5183 } else {
5184 TCGv_i64 tcg_rd = cpu_reg(s, rd);
5186 switch (type) {
5187 case 0:
5188 /* 32 bit */
5189 tcg_gen_ld32u_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_32));
5190 break;
5191 case 1:
5192 /* 64 bit */
5193 tcg_gen_ld_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_64));
5194 break;
5195 case 2:
5196 /* 64 bits from top half */
5197 tcg_gen_ld_i64(tcg_rd, cpu_env, fp_reg_hi_offset(s, rn));
5198 break;
5203 /* C3.6.30 Floating point <-> integer conversions
5204 * 31 30 29 28 24 23 22 21 20 19 18 16 15 10 9 5 4 0
5205 * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
5206 * | sf | 0 | S | 1 1 1 1 0 | type | 1 | rmode | opc | 0 0 0 0 0 0 | Rn | Rd |
5207 * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
5209 static void disas_fp_int_conv(DisasContext *s, uint32_t insn)
5211 int rd = extract32(insn, 0, 5);
5212 int rn = extract32(insn, 5, 5);
5213 int opcode = extract32(insn, 16, 3);
5214 int rmode = extract32(insn, 19, 2);
5215 int type = extract32(insn, 22, 2);
5216 bool sbit = extract32(insn, 29, 1);
5217 bool sf = extract32(insn, 31, 1);
5219 if (sbit) {
5220 unallocated_encoding(s);
5221 return;
5224 if (opcode > 5) {
5225 /* FMOV */
5226 bool itof = opcode & 1;
5228 if (rmode >= 2) {
5229 unallocated_encoding(s);
5230 return;
5233 switch (sf << 3 | type << 1 | rmode) {
5234 case 0x0: /* 32 bit */
5235 case 0xa: /* 64 bit */
5236 case 0xd: /* 64 bit to top half of quad */
5237 break;
5238 default:
5239 /* all other sf/type/rmode combinations are invalid */
5240 unallocated_encoding(s);
5241 break;
5244 if (!fp_access_check(s)) {
5245 return;
5247 handle_fmov(s, rd, rn, type, itof);
5248 } else {
5249 /* actual FP conversions */
5250 bool itof = extract32(opcode, 1, 1);
5252 if (type > 1 || (rmode != 0 && opcode > 1)) {
5253 unallocated_encoding(s);
5254 return;
5257 if (!fp_access_check(s)) {
5258 return;
5260 handle_fpfpcvt(s, rd, rn, opcode, itof, rmode, 64, sf, type);
5264 /* FP-specific subcases of table C3-6 (SIMD and FP data processing)
5265 * 31 30 29 28 25 24 0
5266 * +---+---+---+---------+-----------------------------+
5267 * | | 0 | | 1 1 1 1 | |
5268 * +---+---+---+---------+-----------------------------+
5270 static void disas_data_proc_fp(DisasContext *s, uint32_t insn)
5272 if (extract32(insn, 24, 1)) {
5273 /* Floating point data-processing (3 source) */
5274 disas_fp_3src(s, insn);
5275 } else if (extract32(insn, 21, 1) == 0) {
5276 /* Floating point to fixed point conversions */
5277 disas_fp_fixed_conv(s, insn);
5278 } else {
5279 switch (extract32(insn, 10, 2)) {
5280 case 1:
5281 /* Floating point conditional compare */
5282 disas_fp_ccomp(s, insn);
5283 break;
5284 case 2:
5285 /* Floating point data-processing (2 source) */
5286 disas_fp_2src(s, insn);
5287 break;
5288 case 3:
5289 /* Floating point conditional select */
5290 disas_fp_csel(s, insn);
5291 break;
5292 case 0:
5293 switch (ctz32(extract32(insn, 12, 4))) {
5294 case 0: /* [15:12] == xxx1 */
5295 /* Floating point immediate */
5296 disas_fp_imm(s, insn);
5297 break;
5298 case 1: /* [15:12] == xx10 */
5299 /* Floating point compare */
5300 disas_fp_compare(s, insn);
5301 break;
5302 case 2: /* [15:12] == x100 */
5303 /* Floating point data-processing (1 source) */
5304 disas_fp_1src(s, insn);
5305 break;
5306 case 3: /* [15:12] == 1000 */
5307 unallocated_encoding(s);
5308 break;
5309 default: /* [15:12] == 0000 */
5310 /* Floating point <-> integer conversions */
5311 disas_fp_int_conv(s, insn);
5312 break;
5314 break;
5319 static void do_ext64(DisasContext *s, TCGv_i64 tcg_left, TCGv_i64 tcg_right,
5320 int pos)
5322 /* Extract 64 bits from the middle of two concatenated 64 bit
5323 * vector register slices left:right. The extracted bits start
5324 * at 'pos' bits into the right (least significant) side.
5325 * We return the result in tcg_right, and guarantee not to
5326 * trash tcg_left.
5328 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
5329 assert(pos > 0 && pos < 64);
5331 tcg_gen_shri_i64(tcg_right, tcg_right, pos);
5332 tcg_gen_shli_i64(tcg_tmp, tcg_left, 64 - pos);
5333 tcg_gen_or_i64(tcg_right, tcg_right, tcg_tmp);
5335 tcg_temp_free_i64(tcg_tmp);
5338 /* C3.6.1 EXT
5339 * 31 30 29 24 23 22 21 20 16 15 14 11 10 9 5 4 0
5340 * +---+---+-------------+-----+---+------+---+------+---+------+------+
5341 * | 0 | Q | 1 0 1 1 1 0 | op2 | 0 | Rm | 0 | imm4 | 0 | Rn | Rd |
5342 * +---+---+-------------+-----+---+------+---+------+---+------+------+
5344 static void disas_simd_ext(DisasContext *s, uint32_t insn)
5346 int is_q = extract32(insn, 30, 1);
5347 int op2 = extract32(insn, 22, 2);
5348 int imm4 = extract32(insn, 11, 4);
5349 int rm = extract32(insn, 16, 5);
5350 int rn = extract32(insn, 5, 5);
5351 int rd = extract32(insn, 0, 5);
5352 int pos = imm4 << 3;
5353 TCGv_i64 tcg_resl, tcg_resh;
5355 if (op2 != 0 || (!is_q && extract32(imm4, 3, 1))) {
5356 unallocated_encoding(s);
5357 return;
5360 if (!fp_access_check(s)) {
5361 return;
5364 tcg_resh = tcg_temp_new_i64();
5365 tcg_resl = tcg_temp_new_i64();
5367 /* Vd gets bits starting at pos bits into Vm:Vn. This is
5368 * either extracting 128 bits from a 128:128 concatenation, or
5369 * extracting 64 bits from a 64:64 concatenation.
5371 if (!is_q) {
5372 read_vec_element(s, tcg_resl, rn, 0, MO_64);
5373 if (pos != 0) {
5374 read_vec_element(s, tcg_resh, rm, 0, MO_64);
5375 do_ext64(s, tcg_resh, tcg_resl, pos);
5377 tcg_gen_movi_i64(tcg_resh, 0);
5378 } else {
5379 TCGv_i64 tcg_hh;
5380 typedef struct {
5381 int reg;
5382 int elt;
5383 } EltPosns;
5384 EltPosns eltposns[] = { {rn, 0}, {rn, 1}, {rm, 0}, {rm, 1} };
5385 EltPosns *elt = eltposns;
5387 if (pos >= 64) {
5388 elt++;
5389 pos -= 64;
5392 read_vec_element(s, tcg_resl, elt->reg, elt->elt, MO_64);
5393 elt++;
5394 read_vec_element(s, tcg_resh, elt->reg, elt->elt, MO_64);
5395 elt++;
5396 if (pos != 0) {
5397 do_ext64(s, tcg_resh, tcg_resl, pos);
5398 tcg_hh = tcg_temp_new_i64();
5399 read_vec_element(s, tcg_hh, elt->reg, elt->elt, MO_64);
5400 do_ext64(s, tcg_hh, tcg_resh, pos);
5401 tcg_temp_free_i64(tcg_hh);
5405 write_vec_element(s, tcg_resl, rd, 0, MO_64);
5406 tcg_temp_free_i64(tcg_resl);
5407 write_vec_element(s, tcg_resh, rd, 1, MO_64);
5408 tcg_temp_free_i64(tcg_resh);
5411 /* C3.6.2 TBL/TBX
5412 * 31 30 29 24 23 22 21 20 16 15 14 13 12 11 10 9 5 4 0
5413 * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
5414 * | 0 | Q | 0 0 1 1 1 0 | op2 | 0 | Rm | 0 | len | op | 0 0 | Rn | Rd |
5415 * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
5417 static void disas_simd_tb(DisasContext *s, uint32_t insn)
5419 int op2 = extract32(insn, 22, 2);
5420 int is_q = extract32(insn, 30, 1);
5421 int rm = extract32(insn, 16, 5);
5422 int rn = extract32(insn, 5, 5);
5423 int rd = extract32(insn, 0, 5);
5424 int is_tblx = extract32(insn, 12, 1);
5425 int len = extract32(insn, 13, 2);
5426 TCGv_i64 tcg_resl, tcg_resh, tcg_idx;
5427 TCGv_i32 tcg_regno, tcg_numregs;
5429 if (op2 != 0) {
5430 unallocated_encoding(s);
5431 return;
5434 if (!fp_access_check(s)) {
5435 return;
5438 /* This does a table lookup: for every byte element in the input
5439 * we index into a table formed from up to four vector registers,
5440 * and then the output is the result of the lookups. Our helper
5441 * function does the lookup operation for a single 64 bit part of
5442 * the input.
5444 tcg_resl = tcg_temp_new_i64();
5445 tcg_resh = tcg_temp_new_i64();
5447 if (is_tblx) {
5448 read_vec_element(s, tcg_resl, rd, 0, MO_64);
5449 } else {
5450 tcg_gen_movi_i64(tcg_resl, 0);
5452 if (is_tblx && is_q) {
5453 read_vec_element(s, tcg_resh, rd, 1, MO_64);
5454 } else {
5455 tcg_gen_movi_i64(tcg_resh, 0);
5458 tcg_idx = tcg_temp_new_i64();
5459 tcg_regno = tcg_const_i32(rn);
5460 tcg_numregs = tcg_const_i32(len + 1);
5461 read_vec_element(s, tcg_idx, rm, 0, MO_64);
5462 gen_helper_simd_tbl(tcg_resl, cpu_env, tcg_resl, tcg_idx,
5463 tcg_regno, tcg_numregs);
5464 if (is_q) {
5465 read_vec_element(s, tcg_idx, rm, 1, MO_64);
5466 gen_helper_simd_tbl(tcg_resh, cpu_env, tcg_resh, tcg_idx,
5467 tcg_regno, tcg_numregs);
5469 tcg_temp_free_i64(tcg_idx);
5470 tcg_temp_free_i32(tcg_regno);
5471 tcg_temp_free_i32(tcg_numregs);
5473 write_vec_element(s, tcg_resl, rd, 0, MO_64);
5474 tcg_temp_free_i64(tcg_resl);
5475 write_vec_element(s, tcg_resh, rd, 1, MO_64);
5476 tcg_temp_free_i64(tcg_resh);
5479 /* C3.6.3 ZIP/UZP/TRN
5480 * 31 30 29 24 23 22 21 20 16 15 14 12 11 10 9 5 4 0
5481 * +---+---+-------------+------+---+------+---+------------------+------+
5482 * | 0 | Q | 0 0 1 1 1 0 | size | 0 | Rm | 0 | opc | 1 0 | Rn | Rd |
5483 * +---+---+-------------+------+---+------+---+------------------+------+
5485 static void disas_simd_zip_trn(DisasContext *s, uint32_t insn)
5487 int rd = extract32(insn, 0, 5);
5488 int rn = extract32(insn, 5, 5);
5489 int rm = extract32(insn, 16, 5);
5490 int size = extract32(insn, 22, 2);
5491 /* opc field bits [1:0] indicate ZIP/UZP/TRN;
5492 * bit 2 indicates 1 vs 2 variant of the insn.
5494 int opcode = extract32(insn, 12, 2);
5495 bool part = extract32(insn, 14, 1);
5496 bool is_q = extract32(insn, 30, 1);
5497 int esize = 8 << size;
5498 int i, ofs;
5499 int datasize = is_q ? 128 : 64;
5500 int elements = datasize / esize;
5501 TCGv_i64 tcg_res, tcg_resl, tcg_resh;
5503 if (opcode == 0 || (size == 3 && !is_q)) {
5504 unallocated_encoding(s);
5505 return;
5508 if (!fp_access_check(s)) {
5509 return;
5512 tcg_resl = tcg_const_i64(0);
5513 tcg_resh = tcg_const_i64(0);
5514 tcg_res = tcg_temp_new_i64();
5516 for (i = 0; i < elements; i++) {
5517 switch (opcode) {
5518 case 1: /* UZP1/2 */
5520 int midpoint = elements / 2;
5521 if (i < midpoint) {
5522 read_vec_element(s, tcg_res, rn, 2 * i + part, size);
5523 } else {
5524 read_vec_element(s, tcg_res, rm,
5525 2 * (i - midpoint) + part, size);
5527 break;
5529 case 2: /* TRN1/2 */
5530 if (i & 1) {
5531 read_vec_element(s, tcg_res, rm, (i & ~1) + part, size);
5532 } else {
5533 read_vec_element(s, tcg_res, rn, (i & ~1) + part, size);
5535 break;
5536 case 3: /* ZIP1/2 */
5538 int base = part * elements / 2;
5539 if (i & 1) {
5540 read_vec_element(s, tcg_res, rm, base + (i >> 1), size);
5541 } else {
5542 read_vec_element(s, tcg_res, rn, base + (i >> 1), size);
5544 break;
5546 default:
5547 g_assert_not_reached();
5550 ofs = i * esize;
5551 if (ofs < 64) {
5552 tcg_gen_shli_i64(tcg_res, tcg_res, ofs);
5553 tcg_gen_or_i64(tcg_resl, tcg_resl, tcg_res);
5554 } else {
5555 tcg_gen_shli_i64(tcg_res, tcg_res, ofs - 64);
5556 tcg_gen_or_i64(tcg_resh, tcg_resh, tcg_res);
5560 tcg_temp_free_i64(tcg_res);
5562 write_vec_element(s, tcg_resl, rd, 0, MO_64);
5563 tcg_temp_free_i64(tcg_resl);
5564 write_vec_element(s, tcg_resh, rd, 1, MO_64);
5565 tcg_temp_free_i64(tcg_resh);
5568 static void do_minmaxop(DisasContext *s, TCGv_i32 tcg_elt1, TCGv_i32 tcg_elt2,
5569 int opc, bool is_min, TCGv_ptr fpst)
5571 /* Helper function for disas_simd_across_lanes: do a single precision
5572 * min/max operation on the specified two inputs,
5573 * and return the result in tcg_elt1.
5575 if (opc == 0xc) {
5576 if (is_min) {
5577 gen_helper_vfp_minnums(tcg_elt1, tcg_elt1, tcg_elt2, fpst);
5578 } else {
5579 gen_helper_vfp_maxnums(tcg_elt1, tcg_elt1, tcg_elt2, fpst);
5581 } else {
5582 assert(opc == 0xf);
5583 if (is_min) {
5584 gen_helper_vfp_mins(tcg_elt1, tcg_elt1, tcg_elt2, fpst);
5585 } else {
5586 gen_helper_vfp_maxs(tcg_elt1, tcg_elt1, tcg_elt2, fpst);
5591 /* C3.6.4 AdvSIMD across lanes
5592 * 31 30 29 28 24 23 22 21 17 16 12 11 10 9 5 4 0
5593 * +---+---+---+-----------+------+-----------+--------+-----+------+------+
5594 * | 0 | Q | U | 0 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 | Rn | Rd |
5595 * +---+---+---+-----------+------+-----------+--------+-----+------+------+
5597 static void disas_simd_across_lanes(DisasContext *s, uint32_t insn)
5599 int rd = extract32(insn, 0, 5);
5600 int rn = extract32(insn, 5, 5);
5601 int size = extract32(insn, 22, 2);
5602 int opcode = extract32(insn, 12, 5);
5603 bool is_q = extract32(insn, 30, 1);
5604 bool is_u = extract32(insn, 29, 1);
5605 bool is_fp = false;
5606 bool is_min = false;
5607 int esize;
5608 int elements;
5609 int i;
5610 TCGv_i64 tcg_res, tcg_elt;
5612 switch (opcode) {
5613 case 0x1b: /* ADDV */
5614 if (is_u) {
5615 unallocated_encoding(s);
5616 return;
5618 /* fall through */
5619 case 0x3: /* SADDLV, UADDLV */
5620 case 0xa: /* SMAXV, UMAXV */
5621 case 0x1a: /* SMINV, UMINV */
5622 if (size == 3 || (size == 2 && !is_q)) {
5623 unallocated_encoding(s);
5624 return;
5626 break;
5627 case 0xc: /* FMAXNMV, FMINNMV */
5628 case 0xf: /* FMAXV, FMINV */
5629 if (!is_u || !is_q || extract32(size, 0, 1)) {
5630 unallocated_encoding(s);
5631 return;
5633 /* Bit 1 of size field encodes min vs max, and actual size is always
5634 * 32 bits: adjust the size variable so following code can rely on it
5636 is_min = extract32(size, 1, 1);
5637 is_fp = true;
5638 size = 2;
5639 break;
5640 default:
5641 unallocated_encoding(s);
5642 return;
5645 if (!fp_access_check(s)) {
5646 return;
5649 esize = 8 << size;
5650 elements = (is_q ? 128 : 64) / esize;
5652 tcg_res = tcg_temp_new_i64();
5653 tcg_elt = tcg_temp_new_i64();
5655 /* These instructions operate across all lanes of a vector
5656 * to produce a single result. We can guarantee that a 64
5657 * bit intermediate is sufficient:
5658 * + for [US]ADDLV the maximum element size is 32 bits, and
5659 * the result type is 64 bits
5660 * + for FMAX*V, FMIN*V, ADDV the intermediate type is the
5661 * same as the element size, which is 32 bits at most
5662 * For the integer operations we can choose to work at 64
5663 * or 32 bits and truncate at the end; for simplicity
5664 * we use 64 bits always. The floating point
5665 * ops do require 32 bit intermediates, though.
5667 if (!is_fp) {
5668 read_vec_element(s, tcg_res, rn, 0, size | (is_u ? 0 : MO_SIGN));
5670 for (i = 1; i < elements; i++) {
5671 read_vec_element(s, tcg_elt, rn, i, size | (is_u ? 0 : MO_SIGN));
5673 switch (opcode) {
5674 case 0x03: /* SADDLV / UADDLV */
5675 case 0x1b: /* ADDV */
5676 tcg_gen_add_i64(tcg_res, tcg_res, tcg_elt);
5677 break;
5678 case 0x0a: /* SMAXV / UMAXV */
5679 tcg_gen_movcond_i64(is_u ? TCG_COND_GEU : TCG_COND_GE,
5680 tcg_res,
5681 tcg_res, tcg_elt, tcg_res, tcg_elt);
5682 break;
5683 case 0x1a: /* SMINV / UMINV */
5684 tcg_gen_movcond_i64(is_u ? TCG_COND_LEU : TCG_COND_LE,
5685 tcg_res,
5686 tcg_res, tcg_elt, tcg_res, tcg_elt);
5687 break;
5688 break;
5689 default:
5690 g_assert_not_reached();
5694 } else {
5695 /* Floating point ops which work on 32 bit (single) intermediates.
5696 * Note that correct NaN propagation requires that we do these
5697 * operations in exactly the order specified by the pseudocode.
5699 TCGv_i32 tcg_elt1 = tcg_temp_new_i32();
5700 TCGv_i32 tcg_elt2 = tcg_temp_new_i32();
5701 TCGv_i32 tcg_elt3 = tcg_temp_new_i32();
5702 TCGv_ptr fpst = get_fpstatus_ptr();
5704 assert(esize == 32);
5705 assert(elements == 4);
5707 read_vec_element(s, tcg_elt, rn, 0, MO_32);
5708 tcg_gen_extrl_i64_i32(tcg_elt1, tcg_elt);
5709 read_vec_element(s, tcg_elt, rn, 1, MO_32);
5710 tcg_gen_extrl_i64_i32(tcg_elt2, tcg_elt);
5712 do_minmaxop(s, tcg_elt1, tcg_elt2, opcode, is_min, fpst);
5714 read_vec_element(s, tcg_elt, rn, 2, MO_32);
5715 tcg_gen_extrl_i64_i32(tcg_elt2, tcg_elt);
5716 read_vec_element(s, tcg_elt, rn, 3, MO_32);
5717 tcg_gen_extrl_i64_i32(tcg_elt3, tcg_elt);
5719 do_minmaxop(s, tcg_elt2, tcg_elt3, opcode, is_min, fpst);
5721 do_minmaxop(s, tcg_elt1, tcg_elt2, opcode, is_min, fpst);
5723 tcg_gen_extu_i32_i64(tcg_res, tcg_elt1);
5724 tcg_temp_free_i32(tcg_elt1);
5725 tcg_temp_free_i32(tcg_elt2);
5726 tcg_temp_free_i32(tcg_elt3);
5727 tcg_temp_free_ptr(fpst);
5730 tcg_temp_free_i64(tcg_elt);
5732 /* Now truncate the result to the width required for the final output */
5733 if (opcode == 0x03) {
5734 /* SADDLV, UADDLV: result is 2*esize */
5735 size++;
5738 switch (size) {
5739 case 0:
5740 tcg_gen_ext8u_i64(tcg_res, tcg_res);
5741 break;
5742 case 1:
5743 tcg_gen_ext16u_i64(tcg_res, tcg_res);
5744 break;
5745 case 2:
5746 tcg_gen_ext32u_i64(tcg_res, tcg_res);
5747 break;
5748 case 3:
5749 break;
5750 default:
5751 g_assert_not_reached();
5754 write_fp_dreg(s, rd, tcg_res);
5755 tcg_temp_free_i64(tcg_res);
5758 /* C6.3.31 DUP (Element, Vector)
5760 * 31 30 29 21 20 16 15 10 9 5 4 0
5761 * +---+---+-------------------+--------+-------------+------+------+
5762 * | 0 | Q | 0 0 1 1 1 0 0 0 0 | imm5 | 0 0 0 0 0 1 | Rn | Rd |
5763 * +---+---+-------------------+--------+-------------+------+------+
5765 * size: encoded in imm5 (see ARM ARM LowestSetBit())
5767 static void handle_simd_dupe(DisasContext *s, int is_q, int rd, int rn,
5768 int imm5)
5770 int size = ctz32(imm5);
5771 int esize = 8 << size;
5772 int elements = (is_q ? 128 : 64) / esize;
5773 int index, i;
5774 TCGv_i64 tmp;
5776 if (size > 3 || (size == 3 && !is_q)) {
5777 unallocated_encoding(s);
5778 return;
5781 if (!fp_access_check(s)) {
5782 return;
5785 index = imm5 >> (size + 1);
5787 tmp = tcg_temp_new_i64();
5788 read_vec_element(s, tmp, rn, index, size);
5790 for (i = 0; i < elements; i++) {
5791 write_vec_element(s, tmp, rd, i, size);
5794 if (!is_q) {
5795 clear_vec_high(s, rd);
5798 tcg_temp_free_i64(tmp);
5801 /* C6.3.31 DUP (element, scalar)
5802 * 31 21 20 16 15 10 9 5 4 0
5803 * +-----------------------+--------+-------------+------+------+
5804 * | 0 1 0 1 1 1 1 0 0 0 0 | imm5 | 0 0 0 0 0 1 | Rn | Rd |
5805 * +-----------------------+--------+-------------+------+------+
5807 static void handle_simd_dupes(DisasContext *s, int rd, int rn,
5808 int imm5)
5810 int size = ctz32(imm5);
5811 int index;
5812 TCGv_i64 tmp;
5814 if (size > 3) {
5815 unallocated_encoding(s);
5816 return;
5819 if (!fp_access_check(s)) {
5820 return;
5823 index = imm5 >> (size + 1);
5825 /* This instruction just extracts the specified element and
5826 * zero-extends it into the bottom of the destination register.
5828 tmp = tcg_temp_new_i64();
5829 read_vec_element(s, tmp, rn, index, size);
5830 write_fp_dreg(s, rd, tmp);
5831 tcg_temp_free_i64(tmp);
5834 /* C6.3.32 DUP (General)
5836 * 31 30 29 21 20 16 15 10 9 5 4 0
5837 * +---+---+-------------------+--------+-------------+------+------+
5838 * | 0 | Q | 0 0 1 1 1 0 0 0 0 | imm5 | 0 0 0 0 1 1 | Rn | Rd |
5839 * +---+---+-------------------+--------+-------------+------+------+
5841 * size: encoded in imm5 (see ARM ARM LowestSetBit())
5843 static void handle_simd_dupg(DisasContext *s, int is_q, int rd, int rn,
5844 int imm5)
5846 int size = ctz32(imm5);
5847 int esize = 8 << size;
5848 int elements = (is_q ? 128 : 64)/esize;
5849 int i = 0;
5851 if (size > 3 || ((size == 3) && !is_q)) {
5852 unallocated_encoding(s);
5853 return;
5856 if (!fp_access_check(s)) {
5857 return;
5860 for (i = 0; i < elements; i++) {
5861 write_vec_element(s, cpu_reg(s, rn), rd, i, size);
5863 if (!is_q) {
5864 clear_vec_high(s, rd);
5868 /* C6.3.150 INS (Element)
5870 * 31 21 20 16 15 14 11 10 9 5 4 0
5871 * +-----------------------+--------+------------+---+------+------+
5872 * | 0 1 1 0 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 | Rn | Rd |
5873 * +-----------------------+--------+------------+---+------+------+
5875 * size: encoded in imm5 (see ARM ARM LowestSetBit())
5876 * index: encoded in imm5<4:size+1>
5878 static void handle_simd_inse(DisasContext *s, int rd, int rn,
5879 int imm4, int imm5)
5881 int size = ctz32(imm5);
5882 int src_index, dst_index;
5883 TCGv_i64 tmp;
5885 if (size > 3) {
5886 unallocated_encoding(s);
5887 return;
5890 if (!fp_access_check(s)) {
5891 return;
5894 dst_index = extract32(imm5, 1+size, 5);
5895 src_index = extract32(imm4, size, 4);
5897 tmp = tcg_temp_new_i64();
5899 read_vec_element(s, tmp, rn, src_index, size);
5900 write_vec_element(s, tmp, rd, dst_index, size);
5902 tcg_temp_free_i64(tmp);
5906 /* C6.3.151 INS (General)
5908 * 31 21 20 16 15 10 9 5 4 0
5909 * +-----------------------+--------+-------------+------+------+
5910 * | 0 1 0 0 1 1 1 0 0 0 0 | imm5 | 0 0 0 1 1 1 | Rn | Rd |
5911 * +-----------------------+--------+-------------+------+------+
5913 * size: encoded in imm5 (see ARM ARM LowestSetBit())
5914 * index: encoded in imm5<4:size+1>
5916 static void handle_simd_insg(DisasContext *s, int rd, int rn, int imm5)
5918 int size = ctz32(imm5);
5919 int idx;
5921 if (size > 3) {
5922 unallocated_encoding(s);
5923 return;
5926 if (!fp_access_check(s)) {
5927 return;
5930 idx = extract32(imm5, 1 + size, 4 - size);
5931 write_vec_element(s, cpu_reg(s, rn), rd, idx, size);
5935 * C6.3.321 UMOV (General)
5936 * C6.3.237 SMOV (General)
5938 * 31 30 29 21 20 16 15 12 10 9 5 4 0
5939 * +---+---+-------------------+--------+-------------+------+------+
5940 * | 0 | Q | 0 0 1 1 1 0 0 0 0 | imm5 | 0 0 1 U 1 1 | Rn | Rd |
5941 * +---+---+-------------------+--------+-------------+------+------+
5943 * U: unsigned when set
5944 * size: encoded in imm5 (see ARM ARM LowestSetBit())
5946 static void handle_simd_umov_smov(DisasContext *s, int is_q, int is_signed,
5947 int rn, int rd, int imm5)
5949 int size = ctz32(imm5);
5950 int element;
5951 TCGv_i64 tcg_rd;
5953 /* Check for UnallocatedEncodings */
5954 if (is_signed) {
5955 if (size > 2 || (size == 2 && !is_q)) {
5956 unallocated_encoding(s);
5957 return;
5959 } else {
5960 if (size > 3
5961 || (size < 3 && is_q)
5962 || (size == 3 && !is_q)) {
5963 unallocated_encoding(s);
5964 return;
5968 if (!fp_access_check(s)) {
5969 return;
5972 element = extract32(imm5, 1+size, 4);
5974 tcg_rd = cpu_reg(s, rd);
5975 read_vec_element(s, tcg_rd, rn, element, size | (is_signed ? MO_SIGN : 0));
5976 if (is_signed && !is_q) {
5977 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
5981 /* C3.6.5 AdvSIMD copy
5982 * 31 30 29 28 21 20 16 15 14 11 10 9 5 4 0
5983 * +---+---+----+-----------------+------+---+------+---+------+------+
5984 * | 0 | Q | op | 0 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 | Rn | Rd |
5985 * +---+---+----+-----------------+------+---+------+---+------+------+
5987 static void disas_simd_copy(DisasContext *s, uint32_t insn)
5989 int rd = extract32(insn, 0, 5);
5990 int rn = extract32(insn, 5, 5);
5991 int imm4 = extract32(insn, 11, 4);
5992 int op = extract32(insn, 29, 1);
5993 int is_q = extract32(insn, 30, 1);
5994 int imm5 = extract32(insn, 16, 5);
5996 if (op) {
5997 if (is_q) {
5998 /* INS (element) */
5999 handle_simd_inse(s, rd, rn, imm4, imm5);
6000 } else {
6001 unallocated_encoding(s);
6003 } else {
6004 switch (imm4) {
6005 case 0:
6006 /* DUP (element - vector) */
6007 handle_simd_dupe(s, is_q, rd, rn, imm5);
6008 break;
6009 case 1:
6010 /* DUP (general) */
6011 handle_simd_dupg(s, is_q, rd, rn, imm5);
6012 break;
6013 case 3:
6014 if (is_q) {
6015 /* INS (general) */
6016 handle_simd_insg(s, rd, rn, imm5);
6017 } else {
6018 unallocated_encoding(s);
6020 break;
6021 case 5:
6022 case 7:
6023 /* UMOV/SMOV (is_q indicates 32/64; imm4 indicates signedness) */
6024 handle_simd_umov_smov(s, is_q, (imm4 == 5), rn, rd, imm5);
6025 break;
6026 default:
6027 unallocated_encoding(s);
6028 break;
6033 /* C3.6.6 AdvSIMD modified immediate
6034 * 31 30 29 28 19 18 16 15 12 11 10 9 5 4 0
6035 * +---+---+----+---------------------+-----+-------+----+---+-------+------+
6036 * | 0 | Q | op | 0 1 1 1 1 0 0 0 0 0 | abc | cmode | o2 | 1 | defgh | Rd |
6037 * +---+---+----+---------------------+-----+-------+----+---+-------+------+
6039 * There are a number of operations that can be carried out here:
6040 * MOVI - move (shifted) imm into register
6041 * MVNI - move inverted (shifted) imm into register
6042 * ORR - bitwise OR of (shifted) imm with register
6043 * BIC - bitwise clear of (shifted) imm with register
6045 static void disas_simd_mod_imm(DisasContext *s, uint32_t insn)
6047 int rd = extract32(insn, 0, 5);
6048 int cmode = extract32(insn, 12, 4);
6049 int cmode_3_1 = extract32(cmode, 1, 3);
6050 int cmode_0 = extract32(cmode, 0, 1);
6051 int o2 = extract32(insn, 11, 1);
6052 uint64_t abcdefgh = extract32(insn, 5, 5) | (extract32(insn, 16, 3) << 5);
6053 bool is_neg = extract32(insn, 29, 1);
6054 bool is_q = extract32(insn, 30, 1);
6055 uint64_t imm = 0;
6056 TCGv_i64 tcg_rd, tcg_imm;
6057 int i;
6059 if (o2 != 0 || ((cmode == 0xf) && is_neg && !is_q)) {
6060 unallocated_encoding(s);
6061 return;
6064 if (!fp_access_check(s)) {
6065 return;
6068 /* See AdvSIMDExpandImm() in ARM ARM */
6069 switch (cmode_3_1) {
6070 case 0: /* Replicate(Zeros(24):imm8, 2) */
6071 case 1: /* Replicate(Zeros(16):imm8:Zeros(8), 2) */
6072 case 2: /* Replicate(Zeros(8):imm8:Zeros(16), 2) */
6073 case 3: /* Replicate(imm8:Zeros(24), 2) */
6075 int shift = cmode_3_1 * 8;
6076 imm = bitfield_replicate(abcdefgh << shift, 32);
6077 break;
6079 case 4: /* Replicate(Zeros(8):imm8, 4) */
6080 case 5: /* Replicate(imm8:Zeros(8), 4) */
6082 int shift = (cmode_3_1 & 0x1) * 8;
6083 imm = bitfield_replicate(abcdefgh << shift, 16);
6084 break;
6086 case 6:
6087 if (cmode_0) {
6088 /* Replicate(Zeros(8):imm8:Ones(16), 2) */
6089 imm = (abcdefgh << 16) | 0xffff;
6090 } else {
6091 /* Replicate(Zeros(16):imm8:Ones(8), 2) */
6092 imm = (abcdefgh << 8) | 0xff;
6094 imm = bitfield_replicate(imm, 32);
6095 break;
6096 case 7:
6097 if (!cmode_0 && !is_neg) {
6098 imm = bitfield_replicate(abcdefgh, 8);
6099 } else if (!cmode_0 && is_neg) {
6100 int i;
6101 imm = 0;
6102 for (i = 0; i < 8; i++) {
6103 if ((abcdefgh) & (1 << i)) {
6104 imm |= 0xffULL << (i * 8);
6107 } else if (cmode_0) {
6108 if (is_neg) {
6109 imm = (abcdefgh & 0x3f) << 48;
6110 if (abcdefgh & 0x80) {
6111 imm |= 0x8000000000000000ULL;
6113 if (abcdefgh & 0x40) {
6114 imm |= 0x3fc0000000000000ULL;
6115 } else {
6116 imm |= 0x4000000000000000ULL;
6118 } else {
6119 imm = (abcdefgh & 0x3f) << 19;
6120 if (abcdefgh & 0x80) {
6121 imm |= 0x80000000;
6123 if (abcdefgh & 0x40) {
6124 imm |= 0x3e000000;
6125 } else {
6126 imm |= 0x40000000;
6128 imm |= (imm << 32);
6131 break;
6134 if (cmode_3_1 != 7 && is_neg) {
6135 imm = ~imm;
6138 tcg_imm = tcg_const_i64(imm);
6139 tcg_rd = new_tmp_a64(s);
6141 for (i = 0; i < 2; i++) {
6142 int foffs = i ? fp_reg_hi_offset(s, rd) : fp_reg_offset(s, rd, MO_64);
6144 if (i == 1 && !is_q) {
6145 /* non-quad ops clear high half of vector */
6146 tcg_gen_movi_i64(tcg_rd, 0);
6147 } else if ((cmode & 0x9) == 0x1 || (cmode & 0xd) == 0x9) {
6148 tcg_gen_ld_i64(tcg_rd, cpu_env, foffs);
6149 if (is_neg) {
6150 /* AND (BIC) */
6151 tcg_gen_and_i64(tcg_rd, tcg_rd, tcg_imm);
6152 } else {
6153 /* ORR */
6154 tcg_gen_or_i64(tcg_rd, tcg_rd, tcg_imm);
6156 } else {
6157 /* MOVI */
6158 tcg_gen_mov_i64(tcg_rd, tcg_imm);
6160 tcg_gen_st_i64(tcg_rd, cpu_env, foffs);
6163 tcg_temp_free_i64(tcg_imm);
6166 /* C3.6.7 AdvSIMD scalar copy
6167 * 31 30 29 28 21 20 16 15 14 11 10 9 5 4 0
6168 * +-----+----+-----------------+------+---+------+---+------+------+
6169 * | 0 1 | op | 1 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 | Rn | Rd |
6170 * +-----+----+-----------------+------+---+------+---+------+------+
6172 static void disas_simd_scalar_copy(DisasContext *s, uint32_t insn)
6174 int rd = extract32(insn, 0, 5);
6175 int rn = extract32(insn, 5, 5);
6176 int imm4 = extract32(insn, 11, 4);
6177 int imm5 = extract32(insn, 16, 5);
6178 int op = extract32(insn, 29, 1);
6180 if (op != 0 || imm4 != 0) {
6181 unallocated_encoding(s);
6182 return;
6185 /* DUP (element, scalar) */
6186 handle_simd_dupes(s, rd, rn, imm5);
6189 /* C3.6.8 AdvSIMD scalar pairwise
6190 * 31 30 29 28 24 23 22 21 17 16 12 11 10 9 5 4 0
6191 * +-----+---+-----------+------+-----------+--------+-----+------+------+
6192 * | 0 1 | U | 1 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 | Rn | Rd |
6193 * +-----+---+-----------+------+-----------+--------+-----+------+------+
6195 static void disas_simd_scalar_pairwise(DisasContext *s, uint32_t insn)
6197 int u = extract32(insn, 29, 1);
6198 int size = extract32(insn, 22, 2);
6199 int opcode = extract32(insn, 12, 5);
6200 int rn = extract32(insn, 5, 5);
6201 int rd = extract32(insn, 0, 5);
6202 TCGv_ptr fpst;
6204 /* For some ops (the FP ones), size[1] is part of the encoding.
6205 * For ADDP strictly it is not but size[1] is always 1 for valid
6206 * encodings.
6208 opcode |= (extract32(size, 1, 1) << 5);
6210 switch (opcode) {
6211 case 0x3b: /* ADDP */
6212 if (u || size != 3) {
6213 unallocated_encoding(s);
6214 return;
6216 if (!fp_access_check(s)) {
6217 return;
6220 TCGV_UNUSED_PTR(fpst);
6221 break;
6222 case 0xc: /* FMAXNMP */
6223 case 0xd: /* FADDP */
6224 case 0xf: /* FMAXP */
6225 case 0x2c: /* FMINNMP */
6226 case 0x2f: /* FMINP */
6227 /* FP op, size[0] is 32 or 64 bit */
6228 if (!u) {
6229 unallocated_encoding(s);
6230 return;
6232 if (!fp_access_check(s)) {
6233 return;
6236 size = extract32(size, 0, 1) ? 3 : 2;
6237 fpst = get_fpstatus_ptr();
6238 break;
6239 default:
6240 unallocated_encoding(s);
6241 return;
6244 if (size == 3) {
6245 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
6246 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
6247 TCGv_i64 tcg_res = tcg_temp_new_i64();
6249 read_vec_element(s, tcg_op1, rn, 0, MO_64);
6250 read_vec_element(s, tcg_op2, rn, 1, MO_64);
6252 switch (opcode) {
6253 case 0x3b: /* ADDP */
6254 tcg_gen_add_i64(tcg_res, tcg_op1, tcg_op2);
6255 break;
6256 case 0xc: /* FMAXNMP */
6257 gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
6258 break;
6259 case 0xd: /* FADDP */
6260 gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
6261 break;
6262 case 0xf: /* FMAXP */
6263 gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
6264 break;
6265 case 0x2c: /* FMINNMP */
6266 gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
6267 break;
6268 case 0x2f: /* FMINP */
6269 gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
6270 break;
6271 default:
6272 g_assert_not_reached();
6275 write_fp_dreg(s, rd, tcg_res);
6277 tcg_temp_free_i64(tcg_op1);
6278 tcg_temp_free_i64(tcg_op2);
6279 tcg_temp_free_i64(tcg_res);
6280 } else {
6281 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
6282 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
6283 TCGv_i32 tcg_res = tcg_temp_new_i32();
6285 read_vec_element_i32(s, tcg_op1, rn, 0, MO_32);
6286 read_vec_element_i32(s, tcg_op2, rn, 1, MO_32);
6288 switch (opcode) {
6289 case 0xc: /* FMAXNMP */
6290 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
6291 break;
6292 case 0xd: /* FADDP */
6293 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
6294 break;
6295 case 0xf: /* FMAXP */
6296 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
6297 break;
6298 case 0x2c: /* FMINNMP */
6299 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
6300 break;
6301 case 0x2f: /* FMINP */
6302 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
6303 break;
6304 default:
6305 g_assert_not_reached();
6308 write_fp_sreg(s, rd, tcg_res);
6310 tcg_temp_free_i32(tcg_op1);
6311 tcg_temp_free_i32(tcg_op2);
6312 tcg_temp_free_i32(tcg_res);
6315 if (!TCGV_IS_UNUSED_PTR(fpst)) {
6316 tcg_temp_free_ptr(fpst);
6321 * Common SSHR[RA]/USHR[RA] - Shift right (optional rounding/accumulate)
6323 * This code is handles the common shifting code and is used by both
6324 * the vector and scalar code.
6326 static void handle_shri_with_rndacc(TCGv_i64 tcg_res, TCGv_i64 tcg_src,
6327 TCGv_i64 tcg_rnd, bool accumulate,
6328 bool is_u, int size, int shift)
6330 bool extended_result = false;
6331 bool round = !TCGV_IS_UNUSED_I64(tcg_rnd);
6332 int ext_lshift = 0;
6333 TCGv_i64 tcg_src_hi;
6335 if (round && size == 3) {
6336 extended_result = true;
6337 ext_lshift = 64 - shift;
6338 tcg_src_hi = tcg_temp_new_i64();
6339 } else if (shift == 64) {
6340 if (!accumulate && is_u) {
6341 /* result is zero */
6342 tcg_gen_movi_i64(tcg_res, 0);
6343 return;
6347 /* Deal with the rounding step */
6348 if (round) {
6349 if (extended_result) {
6350 TCGv_i64 tcg_zero = tcg_const_i64(0);
6351 if (!is_u) {
6352 /* take care of sign extending tcg_res */
6353 tcg_gen_sari_i64(tcg_src_hi, tcg_src, 63);
6354 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
6355 tcg_src, tcg_src_hi,
6356 tcg_rnd, tcg_zero);
6357 } else {
6358 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
6359 tcg_src, tcg_zero,
6360 tcg_rnd, tcg_zero);
6362 tcg_temp_free_i64(tcg_zero);
6363 } else {
6364 tcg_gen_add_i64(tcg_src, tcg_src, tcg_rnd);
6368 /* Now do the shift right */
6369 if (round && extended_result) {
6370 /* extended case, >64 bit precision required */
6371 if (ext_lshift == 0) {
6372 /* special case, only high bits matter */
6373 tcg_gen_mov_i64(tcg_src, tcg_src_hi);
6374 } else {
6375 tcg_gen_shri_i64(tcg_src, tcg_src, shift);
6376 tcg_gen_shli_i64(tcg_src_hi, tcg_src_hi, ext_lshift);
6377 tcg_gen_or_i64(tcg_src, tcg_src, tcg_src_hi);
6379 } else {
6380 if (is_u) {
6381 if (shift == 64) {
6382 /* essentially shifting in 64 zeros */
6383 tcg_gen_movi_i64(tcg_src, 0);
6384 } else {
6385 tcg_gen_shri_i64(tcg_src, tcg_src, shift);
6387 } else {
6388 if (shift == 64) {
6389 /* effectively extending the sign-bit */
6390 tcg_gen_sari_i64(tcg_src, tcg_src, 63);
6391 } else {
6392 tcg_gen_sari_i64(tcg_src, tcg_src, shift);
6397 if (accumulate) {
6398 tcg_gen_add_i64(tcg_res, tcg_res, tcg_src);
6399 } else {
6400 tcg_gen_mov_i64(tcg_res, tcg_src);
6403 if (extended_result) {
6404 tcg_temp_free_i64(tcg_src_hi);
6408 /* Common SHL/SLI - Shift left with an optional insert */
6409 static void handle_shli_with_ins(TCGv_i64 tcg_res, TCGv_i64 tcg_src,
6410 bool insert, int shift)
6412 if (insert) { /* SLI */
6413 tcg_gen_deposit_i64(tcg_res, tcg_res, tcg_src, shift, 64 - shift);
6414 } else { /* SHL */
6415 tcg_gen_shli_i64(tcg_res, tcg_src, shift);
6419 /* SRI: shift right with insert */
6420 static void handle_shri_with_ins(TCGv_i64 tcg_res, TCGv_i64 tcg_src,
6421 int size, int shift)
6423 int esize = 8 << size;
6425 /* shift count same as element size is valid but does nothing;
6426 * special case to avoid potential shift by 64.
6428 if (shift != esize) {
6429 tcg_gen_shri_i64(tcg_src, tcg_src, shift);
6430 tcg_gen_deposit_i64(tcg_res, tcg_res, tcg_src, 0, esize - shift);
6434 /* SSHR[RA]/USHR[RA] - Scalar shift right (optional rounding/accumulate) */
6435 static void handle_scalar_simd_shri(DisasContext *s,
6436 bool is_u, int immh, int immb,
6437 int opcode, int rn, int rd)
6439 const int size = 3;
6440 int immhb = immh << 3 | immb;
6441 int shift = 2 * (8 << size) - immhb;
6442 bool accumulate = false;
6443 bool round = false;
6444 bool insert = false;
6445 TCGv_i64 tcg_rn;
6446 TCGv_i64 tcg_rd;
6447 TCGv_i64 tcg_round;
6449 if (!extract32(immh, 3, 1)) {
6450 unallocated_encoding(s);
6451 return;
6454 if (!fp_access_check(s)) {
6455 return;
6458 switch (opcode) {
6459 case 0x02: /* SSRA / USRA (accumulate) */
6460 accumulate = true;
6461 break;
6462 case 0x04: /* SRSHR / URSHR (rounding) */
6463 round = true;
6464 break;
6465 case 0x06: /* SRSRA / URSRA (accum + rounding) */
6466 accumulate = round = true;
6467 break;
6468 case 0x08: /* SRI */
6469 insert = true;
6470 break;
6473 if (round) {
6474 uint64_t round_const = 1ULL << (shift - 1);
6475 tcg_round = tcg_const_i64(round_const);
6476 } else {
6477 TCGV_UNUSED_I64(tcg_round);
6480 tcg_rn = read_fp_dreg(s, rn);
6481 tcg_rd = (accumulate || insert) ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
6483 if (insert) {
6484 handle_shri_with_ins(tcg_rd, tcg_rn, size, shift);
6485 } else {
6486 handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
6487 accumulate, is_u, size, shift);
6490 write_fp_dreg(s, rd, tcg_rd);
6492 tcg_temp_free_i64(tcg_rn);
6493 tcg_temp_free_i64(tcg_rd);
6494 if (round) {
6495 tcg_temp_free_i64(tcg_round);
6499 /* SHL/SLI - Scalar shift left */
6500 static void handle_scalar_simd_shli(DisasContext *s, bool insert,
6501 int immh, int immb, int opcode,
6502 int rn, int rd)
6504 int size = 32 - clz32(immh) - 1;
6505 int immhb = immh << 3 | immb;
6506 int shift = immhb - (8 << size);
6507 TCGv_i64 tcg_rn = new_tmp_a64(s);
6508 TCGv_i64 tcg_rd = new_tmp_a64(s);
6510 if (!extract32(immh, 3, 1)) {
6511 unallocated_encoding(s);
6512 return;
6515 if (!fp_access_check(s)) {
6516 return;
6519 tcg_rn = read_fp_dreg(s, rn);
6520 tcg_rd = insert ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
6522 handle_shli_with_ins(tcg_rd, tcg_rn, insert, shift);
6524 write_fp_dreg(s, rd, tcg_rd);
6526 tcg_temp_free_i64(tcg_rn);
6527 tcg_temp_free_i64(tcg_rd);
6530 /* SQSHRN/SQSHRUN - Saturating (signed/unsigned) shift right with
6531 * (signed/unsigned) narrowing */
6532 static void handle_vec_simd_sqshrn(DisasContext *s, bool is_scalar, bool is_q,
6533 bool is_u_shift, bool is_u_narrow,
6534 int immh, int immb, int opcode,
6535 int rn, int rd)
6537 int immhb = immh << 3 | immb;
6538 int size = 32 - clz32(immh) - 1;
6539 int esize = 8 << size;
6540 int shift = (2 * esize) - immhb;
6541 int elements = is_scalar ? 1 : (64 / esize);
6542 bool round = extract32(opcode, 0, 1);
6543 TCGMemOp ldop = (size + 1) | (is_u_shift ? 0 : MO_SIGN);
6544 TCGv_i64 tcg_rn, tcg_rd, tcg_round;
6545 TCGv_i32 tcg_rd_narrowed;
6546 TCGv_i64 tcg_final;
6548 static NeonGenNarrowEnvFn * const signed_narrow_fns[4][2] = {
6549 { gen_helper_neon_narrow_sat_s8,
6550 gen_helper_neon_unarrow_sat8 },
6551 { gen_helper_neon_narrow_sat_s16,
6552 gen_helper_neon_unarrow_sat16 },
6553 { gen_helper_neon_narrow_sat_s32,
6554 gen_helper_neon_unarrow_sat32 },
6555 { NULL, NULL },
6557 static NeonGenNarrowEnvFn * const unsigned_narrow_fns[4] = {
6558 gen_helper_neon_narrow_sat_u8,
6559 gen_helper_neon_narrow_sat_u16,
6560 gen_helper_neon_narrow_sat_u32,
6561 NULL
6563 NeonGenNarrowEnvFn *narrowfn;
6565 int i;
6567 assert(size < 4);
6569 if (extract32(immh, 3, 1)) {
6570 unallocated_encoding(s);
6571 return;
6574 if (!fp_access_check(s)) {
6575 return;
6578 if (is_u_shift) {
6579 narrowfn = unsigned_narrow_fns[size];
6580 } else {
6581 narrowfn = signed_narrow_fns[size][is_u_narrow ? 1 : 0];
6584 tcg_rn = tcg_temp_new_i64();
6585 tcg_rd = tcg_temp_new_i64();
6586 tcg_rd_narrowed = tcg_temp_new_i32();
6587 tcg_final = tcg_const_i64(0);
6589 if (round) {
6590 uint64_t round_const = 1ULL << (shift - 1);
6591 tcg_round = tcg_const_i64(round_const);
6592 } else {
6593 TCGV_UNUSED_I64(tcg_round);
6596 for (i = 0; i < elements; i++) {
6597 read_vec_element(s, tcg_rn, rn, i, ldop);
6598 handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
6599 false, is_u_shift, size+1, shift);
6600 narrowfn(tcg_rd_narrowed, cpu_env, tcg_rd);
6601 tcg_gen_extu_i32_i64(tcg_rd, tcg_rd_narrowed);
6602 tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
6605 if (!is_q) {
6606 clear_vec_high(s, rd);
6607 write_vec_element(s, tcg_final, rd, 0, MO_64);
6608 } else {
6609 write_vec_element(s, tcg_final, rd, 1, MO_64);
6612 if (round) {
6613 tcg_temp_free_i64(tcg_round);
6615 tcg_temp_free_i64(tcg_rn);
6616 tcg_temp_free_i64(tcg_rd);
6617 tcg_temp_free_i32(tcg_rd_narrowed);
6618 tcg_temp_free_i64(tcg_final);
6619 return;
6622 /* SQSHLU, UQSHL, SQSHL: saturating left shifts */
6623 static void handle_simd_qshl(DisasContext *s, bool scalar, bool is_q,
6624 bool src_unsigned, bool dst_unsigned,
6625 int immh, int immb, int rn, int rd)
6627 int immhb = immh << 3 | immb;
6628 int size = 32 - clz32(immh) - 1;
6629 int shift = immhb - (8 << size);
6630 int pass;
6632 assert(immh != 0);
6633 assert(!(scalar && is_q));
6635 if (!scalar) {
6636 if (!is_q && extract32(immh, 3, 1)) {
6637 unallocated_encoding(s);
6638 return;
6641 /* Since we use the variable-shift helpers we must
6642 * replicate the shift count into each element of
6643 * the tcg_shift value.
6645 switch (size) {
6646 case 0:
6647 shift |= shift << 8;
6648 /* fall through */
6649 case 1:
6650 shift |= shift << 16;
6651 break;
6652 case 2:
6653 case 3:
6654 break;
6655 default:
6656 g_assert_not_reached();
6660 if (!fp_access_check(s)) {
6661 return;
6664 if (size == 3) {
6665 TCGv_i64 tcg_shift = tcg_const_i64(shift);
6666 static NeonGenTwo64OpEnvFn * const fns[2][2] = {
6667 { gen_helper_neon_qshl_s64, gen_helper_neon_qshlu_s64 },
6668 { NULL, gen_helper_neon_qshl_u64 },
6670 NeonGenTwo64OpEnvFn *genfn = fns[src_unsigned][dst_unsigned];
6671 int maxpass = is_q ? 2 : 1;
6673 for (pass = 0; pass < maxpass; pass++) {
6674 TCGv_i64 tcg_op = tcg_temp_new_i64();
6676 read_vec_element(s, tcg_op, rn, pass, MO_64);
6677 genfn(tcg_op, cpu_env, tcg_op, tcg_shift);
6678 write_vec_element(s, tcg_op, rd, pass, MO_64);
6680 tcg_temp_free_i64(tcg_op);
6682 tcg_temp_free_i64(tcg_shift);
6684 if (!is_q) {
6685 clear_vec_high(s, rd);
6687 } else {
6688 TCGv_i32 tcg_shift = tcg_const_i32(shift);
6689 static NeonGenTwoOpEnvFn * const fns[2][2][3] = {
6691 { gen_helper_neon_qshl_s8,
6692 gen_helper_neon_qshl_s16,
6693 gen_helper_neon_qshl_s32 },
6694 { gen_helper_neon_qshlu_s8,
6695 gen_helper_neon_qshlu_s16,
6696 gen_helper_neon_qshlu_s32 }
6697 }, {
6698 { NULL, NULL, NULL },
6699 { gen_helper_neon_qshl_u8,
6700 gen_helper_neon_qshl_u16,
6701 gen_helper_neon_qshl_u32 }
6704 NeonGenTwoOpEnvFn *genfn = fns[src_unsigned][dst_unsigned][size];
6705 TCGMemOp memop = scalar ? size : MO_32;
6706 int maxpass = scalar ? 1 : is_q ? 4 : 2;
6708 for (pass = 0; pass < maxpass; pass++) {
6709 TCGv_i32 tcg_op = tcg_temp_new_i32();
6711 read_vec_element_i32(s, tcg_op, rn, pass, memop);
6712 genfn(tcg_op, cpu_env, tcg_op, tcg_shift);
6713 if (scalar) {
6714 switch (size) {
6715 case 0:
6716 tcg_gen_ext8u_i32(tcg_op, tcg_op);
6717 break;
6718 case 1:
6719 tcg_gen_ext16u_i32(tcg_op, tcg_op);
6720 break;
6721 case 2:
6722 break;
6723 default:
6724 g_assert_not_reached();
6726 write_fp_sreg(s, rd, tcg_op);
6727 } else {
6728 write_vec_element_i32(s, tcg_op, rd, pass, MO_32);
6731 tcg_temp_free_i32(tcg_op);
6733 tcg_temp_free_i32(tcg_shift);
6735 if (!is_q && !scalar) {
6736 clear_vec_high(s, rd);
6741 /* Common vector code for handling integer to FP conversion */
6742 static void handle_simd_intfp_conv(DisasContext *s, int rd, int rn,
6743 int elements, int is_signed,
6744 int fracbits, int size)
6746 bool is_double = size == 3 ? true : false;
6747 TCGv_ptr tcg_fpst = get_fpstatus_ptr();
6748 TCGv_i32 tcg_shift = tcg_const_i32(fracbits);
6749 TCGv_i64 tcg_int = tcg_temp_new_i64();
6750 TCGMemOp mop = size | (is_signed ? MO_SIGN : 0);
6751 int pass;
6753 for (pass = 0; pass < elements; pass++) {
6754 read_vec_element(s, tcg_int, rn, pass, mop);
6756 if (is_double) {
6757 TCGv_i64 tcg_double = tcg_temp_new_i64();
6758 if (is_signed) {
6759 gen_helper_vfp_sqtod(tcg_double, tcg_int,
6760 tcg_shift, tcg_fpst);
6761 } else {
6762 gen_helper_vfp_uqtod(tcg_double, tcg_int,
6763 tcg_shift, tcg_fpst);
6765 if (elements == 1) {
6766 write_fp_dreg(s, rd, tcg_double);
6767 } else {
6768 write_vec_element(s, tcg_double, rd, pass, MO_64);
6770 tcg_temp_free_i64(tcg_double);
6771 } else {
6772 TCGv_i32 tcg_single = tcg_temp_new_i32();
6773 if (is_signed) {
6774 gen_helper_vfp_sqtos(tcg_single, tcg_int,
6775 tcg_shift, tcg_fpst);
6776 } else {
6777 gen_helper_vfp_uqtos(tcg_single, tcg_int,
6778 tcg_shift, tcg_fpst);
6780 if (elements == 1) {
6781 write_fp_sreg(s, rd, tcg_single);
6782 } else {
6783 write_vec_element_i32(s, tcg_single, rd, pass, MO_32);
6785 tcg_temp_free_i32(tcg_single);
6789 if (!is_double && elements == 2) {
6790 clear_vec_high(s, rd);
6793 tcg_temp_free_i64(tcg_int);
6794 tcg_temp_free_ptr(tcg_fpst);
6795 tcg_temp_free_i32(tcg_shift);
6798 /* UCVTF/SCVTF - Integer to FP conversion */
6799 static void handle_simd_shift_intfp_conv(DisasContext *s, bool is_scalar,
6800 bool is_q, bool is_u,
6801 int immh, int immb, int opcode,
6802 int rn, int rd)
6804 bool is_double = extract32(immh, 3, 1);
6805 int size = is_double ? MO_64 : MO_32;
6806 int elements;
6807 int immhb = immh << 3 | immb;
6808 int fracbits = (is_double ? 128 : 64) - immhb;
6810 if (!extract32(immh, 2, 2)) {
6811 unallocated_encoding(s);
6812 return;
6815 if (is_scalar) {
6816 elements = 1;
6817 } else {
6818 elements = is_double ? 2 : is_q ? 4 : 2;
6819 if (is_double && !is_q) {
6820 unallocated_encoding(s);
6821 return;
6825 if (!fp_access_check(s)) {
6826 return;
6829 /* immh == 0 would be a failure of the decode logic */
6830 g_assert(immh);
6832 handle_simd_intfp_conv(s, rd, rn, elements, !is_u, fracbits, size);
6835 /* FCVTZS, FVCVTZU - FP to fixedpoint conversion */
6836 static void handle_simd_shift_fpint_conv(DisasContext *s, bool is_scalar,
6837 bool is_q, bool is_u,
6838 int immh, int immb, int rn, int rd)
6840 bool is_double = extract32(immh, 3, 1);
6841 int immhb = immh << 3 | immb;
6842 int fracbits = (is_double ? 128 : 64) - immhb;
6843 int pass;
6844 TCGv_ptr tcg_fpstatus;
6845 TCGv_i32 tcg_rmode, tcg_shift;
6847 if (!extract32(immh, 2, 2)) {
6848 unallocated_encoding(s);
6849 return;
6852 if (!is_scalar && !is_q && is_double) {
6853 unallocated_encoding(s);
6854 return;
6857 if (!fp_access_check(s)) {
6858 return;
6861 assert(!(is_scalar && is_q));
6863 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(FPROUNDING_ZERO));
6864 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
6865 tcg_fpstatus = get_fpstatus_ptr();
6866 tcg_shift = tcg_const_i32(fracbits);
6868 if (is_double) {
6869 int maxpass = is_scalar ? 1 : 2;
6871 for (pass = 0; pass < maxpass; pass++) {
6872 TCGv_i64 tcg_op = tcg_temp_new_i64();
6874 read_vec_element(s, tcg_op, rn, pass, MO_64);
6875 if (is_u) {
6876 gen_helper_vfp_touqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
6877 } else {
6878 gen_helper_vfp_tosqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
6880 write_vec_element(s, tcg_op, rd, pass, MO_64);
6881 tcg_temp_free_i64(tcg_op);
6883 if (!is_q) {
6884 clear_vec_high(s, rd);
6886 } else {
6887 int maxpass = is_scalar ? 1 : is_q ? 4 : 2;
6888 for (pass = 0; pass < maxpass; pass++) {
6889 TCGv_i32 tcg_op = tcg_temp_new_i32();
6891 read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
6892 if (is_u) {
6893 gen_helper_vfp_touls(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
6894 } else {
6895 gen_helper_vfp_tosls(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
6897 if (is_scalar) {
6898 write_fp_sreg(s, rd, tcg_op);
6899 } else {
6900 write_vec_element_i32(s, tcg_op, rd, pass, MO_32);
6902 tcg_temp_free_i32(tcg_op);
6904 if (!is_q && !is_scalar) {
6905 clear_vec_high(s, rd);
6909 tcg_temp_free_ptr(tcg_fpstatus);
6910 tcg_temp_free_i32(tcg_shift);
6911 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
6912 tcg_temp_free_i32(tcg_rmode);
6915 /* C3.6.9 AdvSIMD scalar shift by immediate
6916 * 31 30 29 28 23 22 19 18 16 15 11 10 9 5 4 0
6917 * +-----+---+-------------+------+------+--------+---+------+------+
6918 * | 0 1 | U | 1 1 1 1 1 0 | immh | immb | opcode | 1 | Rn | Rd |
6919 * +-----+---+-------------+------+------+--------+---+------+------+
6921 * This is the scalar version so it works on a fixed sized registers
6923 static void disas_simd_scalar_shift_imm(DisasContext *s, uint32_t insn)
6925 int rd = extract32(insn, 0, 5);
6926 int rn = extract32(insn, 5, 5);
6927 int opcode = extract32(insn, 11, 5);
6928 int immb = extract32(insn, 16, 3);
6929 int immh = extract32(insn, 19, 4);
6930 bool is_u = extract32(insn, 29, 1);
6932 if (immh == 0) {
6933 unallocated_encoding(s);
6934 return;
6937 switch (opcode) {
6938 case 0x08: /* SRI */
6939 if (!is_u) {
6940 unallocated_encoding(s);
6941 return;
6943 /* fall through */
6944 case 0x00: /* SSHR / USHR */
6945 case 0x02: /* SSRA / USRA */
6946 case 0x04: /* SRSHR / URSHR */
6947 case 0x06: /* SRSRA / URSRA */
6948 handle_scalar_simd_shri(s, is_u, immh, immb, opcode, rn, rd);
6949 break;
6950 case 0x0a: /* SHL / SLI */
6951 handle_scalar_simd_shli(s, is_u, immh, immb, opcode, rn, rd);
6952 break;
6953 case 0x1c: /* SCVTF, UCVTF */
6954 handle_simd_shift_intfp_conv(s, true, false, is_u, immh, immb,
6955 opcode, rn, rd);
6956 break;
6957 case 0x10: /* SQSHRUN, SQSHRUN2 */
6958 case 0x11: /* SQRSHRUN, SQRSHRUN2 */
6959 if (!is_u) {
6960 unallocated_encoding(s);
6961 return;
6963 handle_vec_simd_sqshrn(s, true, false, false, true,
6964 immh, immb, opcode, rn, rd);
6965 break;
6966 case 0x12: /* SQSHRN, SQSHRN2, UQSHRN */
6967 case 0x13: /* SQRSHRN, SQRSHRN2, UQRSHRN, UQRSHRN2 */
6968 handle_vec_simd_sqshrn(s, true, false, is_u, is_u,
6969 immh, immb, opcode, rn, rd);
6970 break;
6971 case 0xc: /* SQSHLU */
6972 if (!is_u) {
6973 unallocated_encoding(s);
6974 return;
6976 handle_simd_qshl(s, true, false, false, true, immh, immb, rn, rd);
6977 break;
6978 case 0xe: /* SQSHL, UQSHL */
6979 handle_simd_qshl(s, true, false, is_u, is_u, immh, immb, rn, rd);
6980 break;
6981 case 0x1f: /* FCVTZS, FCVTZU */
6982 handle_simd_shift_fpint_conv(s, true, false, is_u, immh, immb, rn, rd);
6983 break;
6984 default:
6985 unallocated_encoding(s);
6986 break;
6990 /* C3.6.10 AdvSIMD scalar three different
6991 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 0
6992 * +-----+---+-----------+------+---+------+--------+-----+------+------+
6993 * | 0 1 | U | 1 1 1 1 0 | size | 1 | Rm | opcode | 0 0 | Rn | Rd |
6994 * +-----+---+-----------+------+---+------+--------+-----+------+------+
6996 static void disas_simd_scalar_three_reg_diff(DisasContext *s, uint32_t insn)
6998 bool is_u = extract32(insn, 29, 1);
6999 int size = extract32(insn, 22, 2);
7000 int opcode = extract32(insn, 12, 4);
7001 int rm = extract32(insn, 16, 5);
7002 int rn = extract32(insn, 5, 5);
7003 int rd = extract32(insn, 0, 5);
7005 if (is_u) {
7006 unallocated_encoding(s);
7007 return;
7010 switch (opcode) {
7011 case 0x9: /* SQDMLAL, SQDMLAL2 */
7012 case 0xb: /* SQDMLSL, SQDMLSL2 */
7013 case 0xd: /* SQDMULL, SQDMULL2 */
7014 if (size == 0 || size == 3) {
7015 unallocated_encoding(s);
7016 return;
7018 break;
7019 default:
7020 unallocated_encoding(s);
7021 return;
7024 if (!fp_access_check(s)) {
7025 return;
7028 if (size == 2) {
7029 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
7030 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
7031 TCGv_i64 tcg_res = tcg_temp_new_i64();
7033 read_vec_element(s, tcg_op1, rn, 0, MO_32 | MO_SIGN);
7034 read_vec_element(s, tcg_op2, rm, 0, MO_32 | MO_SIGN);
7036 tcg_gen_mul_i64(tcg_res, tcg_op1, tcg_op2);
7037 gen_helper_neon_addl_saturate_s64(tcg_res, cpu_env, tcg_res, tcg_res);
7039 switch (opcode) {
7040 case 0xd: /* SQDMULL, SQDMULL2 */
7041 break;
7042 case 0xb: /* SQDMLSL, SQDMLSL2 */
7043 tcg_gen_neg_i64(tcg_res, tcg_res);
7044 /* fall through */
7045 case 0x9: /* SQDMLAL, SQDMLAL2 */
7046 read_vec_element(s, tcg_op1, rd, 0, MO_64);
7047 gen_helper_neon_addl_saturate_s64(tcg_res, cpu_env,
7048 tcg_res, tcg_op1);
7049 break;
7050 default:
7051 g_assert_not_reached();
7054 write_fp_dreg(s, rd, tcg_res);
7056 tcg_temp_free_i64(tcg_op1);
7057 tcg_temp_free_i64(tcg_op2);
7058 tcg_temp_free_i64(tcg_res);
7059 } else {
7060 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
7061 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
7062 TCGv_i64 tcg_res = tcg_temp_new_i64();
7064 read_vec_element_i32(s, tcg_op1, rn, 0, MO_16);
7065 read_vec_element_i32(s, tcg_op2, rm, 0, MO_16);
7067 gen_helper_neon_mull_s16(tcg_res, tcg_op1, tcg_op2);
7068 gen_helper_neon_addl_saturate_s32(tcg_res, cpu_env, tcg_res, tcg_res);
7070 switch (opcode) {
7071 case 0xd: /* SQDMULL, SQDMULL2 */
7072 break;
7073 case 0xb: /* SQDMLSL, SQDMLSL2 */
7074 gen_helper_neon_negl_u32(tcg_res, tcg_res);
7075 /* fall through */
7076 case 0x9: /* SQDMLAL, SQDMLAL2 */
7078 TCGv_i64 tcg_op3 = tcg_temp_new_i64();
7079 read_vec_element(s, tcg_op3, rd, 0, MO_32);
7080 gen_helper_neon_addl_saturate_s32(tcg_res, cpu_env,
7081 tcg_res, tcg_op3);
7082 tcg_temp_free_i64(tcg_op3);
7083 break;
7085 default:
7086 g_assert_not_reached();
7089 tcg_gen_ext32u_i64(tcg_res, tcg_res);
7090 write_fp_dreg(s, rd, tcg_res);
7092 tcg_temp_free_i32(tcg_op1);
7093 tcg_temp_free_i32(tcg_op2);
7094 tcg_temp_free_i64(tcg_res);
7098 static void handle_3same_64(DisasContext *s, int opcode, bool u,
7099 TCGv_i64 tcg_rd, TCGv_i64 tcg_rn, TCGv_i64 tcg_rm)
7101 /* Handle 64x64->64 opcodes which are shared between the scalar
7102 * and vector 3-same groups. We cover every opcode where size == 3
7103 * is valid in either the three-reg-same (integer, not pairwise)
7104 * or scalar-three-reg-same groups. (Some opcodes are not yet
7105 * implemented.)
7107 TCGCond cond;
7109 switch (opcode) {
7110 case 0x1: /* SQADD */
7111 if (u) {
7112 gen_helper_neon_qadd_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
7113 } else {
7114 gen_helper_neon_qadd_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
7116 break;
7117 case 0x5: /* SQSUB */
7118 if (u) {
7119 gen_helper_neon_qsub_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
7120 } else {
7121 gen_helper_neon_qsub_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
7123 break;
7124 case 0x6: /* CMGT, CMHI */
7125 /* 64 bit integer comparison, result = test ? (2^64 - 1) : 0.
7126 * We implement this using setcond (test) and then negating.
7128 cond = u ? TCG_COND_GTU : TCG_COND_GT;
7129 do_cmop:
7130 tcg_gen_setcond_i64(cond, tcg_rd, tcg_rn, tcg_rm);
7131 tcg_gen_neg_i64(tcg_rd, tcg_rd);
7132 break;
7133 case 0x7: /* CMGE, CMHS */
7134 cond = u ? TCG_COND_GEU : TCG_COND_GE;
7135 goto do_cmop;
7136 case 0x11: /* CMTST, CMEQ */
7137 if (u) {
7138 cond = TCG_COND_EQ;
7139 goto do_cmop;
7141 /* CMTST : test is "if (X & Y != 0)". */
7142 tcg_gen_and_i64(tcg_rd, tcg_rn, tcg_rm);
7143 tcg_gen_setcondi_i64(TCG_COND_NE, tcg_rd, tcg_rd, 0);
7144 tcg_gen_neg_i64(tcg_rd, tcg_rd);
7145 break;
7146 case 0x8: /* SSHL, USHL */
7147 if (u) {
7148 gen_helper_neon_shl_u64(tcg_rd, tcg_rn, tcg_rm);
7149 } else {
7150 gen_helper_neon_shl_s64(tcg_rd, tcg_rn, tcg_rm);
7152 break;
7153 case 0x9: /* SQSHL, UQSHL */
7154 if (u) {
7155 gen_helper_neon_qshl_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
7156 } else {
7157 gen_helper_neon_qshl_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
7159 break;
7160 case 0xa: /* SRSHL, URSHL */
7161 if (u) {
7162 gen_helper_neon_rshl_u64(tcg_rd, tcg_rn, tcg_rm);
7163 } else {
7164 gen_helper_neon_rshl_s64(tcg_rd, tcg_rn, tcg_rm);
7166 break;
7167 case 0xb: /* SQRSHL, UQRSHL */
7168 if (u) {
7169 gen_helper_neon_qrshl_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
7170 } else {
7171 gen_helper_neon_qrshl_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
7173 break;
7174 case 0x10: /* ADD, SUB */
7175 if (u) {
7176 tcg_gen_sub_i64(tcg_rd, tcg_rn, tcg_rm);
7177 } else {
7178 tcg_gen_add_i64(tcg_rd, tcg_rn, tcg_rm);
7180 break;
7181 default:
7182 g_assert_not_reached();
7186 /* Handle the 3-same-operands float operations; shared by the scalar
7187 * and vector encodings. The caller must filter out any encodings
7188 * not allocated for the encoding it is dealing with.
7190 static void handle_3same_float(DisasContext *s, int size, int elements,
7191 int fpopcode, int rd, int rn, int rm)
7193 int pass;
7194 TCGv_ptr fpst = get_fpstatus_ptr();
7196 for (pass = 0; pass < elements; pass++) {
7197 if (size) {
7198 /* Double */
7199 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
7200 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
7201 TCGv_i64 tcg_res = tcg_temp_new_i64();
7203 read_vec_element(s, tcg_op1, rn, pass, MO_64);
7204 read_vec_element(s, tcg_op2, rm, pass, MO_64);
7206 switch (fpopcode) {
7207 case 0x39: /* FMLS */
7208 /* As usual for ARM, separate negation for fused multiply-add */
7209 gen_helper_vfp_negd(tcg_op1, tcg_op1);
7210 /* fall through */
7211 case 0x19: /* FMLA */
7212 read_vec_element(s, tcg_res, rd, pass, MO_64);
7213 gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2,
7214 tcg_res, fpst);
7215 break;
7216 case 0x18: /* FMAXNM */
7217 gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
7218 break;
7219 case 0x1a: /* FADD */
7220 gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
7221 break;
7222 case 0x1b: /* FMULX */
7223 gen_helper_vfp_mulxd(tcg_res, tcg_op1, tcg_op2, fpst);
7224 break;
7225 case 0x1c: /* FCMEQ */
7226 gen_helper_neon_ceq_f64(tcg_res, tcg_op1, tcg_op2, fpst);
7227 break;
7228 case 0x1e: /* FMAX */
7229 gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
7230 break;
7231 case 0x1f: /* FRECPS */
7232 gen_helper_recpsf_f64(tcg_res, tcg_op1, tcg_op2, fpst);
7233 break;
7234 case 0x38: /* FMINNM */
7235 gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
7236 break;
7237 case 0x3a: /* FSUB */
7238 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
7239 break;
7240 case 0x3e: /* FMIN */
7241 gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
7242 break;
7243 case 0x3f: /* FRSQRTS */
7244 gen_helper_rsqrtsf_f64(tcg_res, tcg_op1, tcg_op2, fpst);
7245 break;
7246 case 0x5b: /* FMUL */
7247 gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
7248 break;
7249 case 0x5c: /* FCMGE */
7250 gen_helper_neon_cge_f64(tcg_res, tcg_op1, tcg_op2, fpst);
7251 break;
7252 case 0x5d: /* FACGE */
7253 gen_helper_neon_acge_f64(tcg_res, tcg_op1, tcg_op2, fpst);
7254 break;
7255 case 0x5f: /* FDIV */
7256 gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
7257 break;
7258 case 0x7a: /* FABD */
7259 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
7260 gen_helper_vfp_absd(tcg_res, tcg_res);
7261 break;
7262 case 0x7c: /* FCMGT */
7263 gen_helper_neon_cgt_f64(tcg_res, tcg_op1, tcg_op2, fpst);
7264 break;
7265 case 0x7d: /* FACGT */
7266 gen_helper_neon_acgt_f64(tcg_res, tcg_op1, tcg_op2, fpst);
7267 break;
7268 default:
7269 g_assert_not_reached();
7272 write_vec_element(s, tcg_res, rd, pass, MO_64);
7274 tcg_temp_free_i64(tcg_res);
7275 tcg_temp_free_i64(tcg_op1);
7276 tcg_temp_free_i64(tcg_op2);
7277 } else {
7278 /* Single */
7279 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
7280 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
7281 TCGv_i32 tcg_res = tcg_temp_new_i32();
7283 read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
7284 read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
7286 switch (fpopcode) {
7287 case 0x39: /* FMLS */
7288 /* As usual for ARM, separate negation for fused multiply-add */
7289 gen_helper_vfp_negs(tcg_op1, tcg_op1);
7290 /* fall through */
7291 case 0x19: /* FMLA */
7292 read_vec_element_i32(s, tcg_res, rd, pass, MO_32);
7293 gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2,
7294 tcg_res, fpst);
7295 break;
7296 case 0x1a: /* FADD */
7297 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
7298 break;
7299 case 0x1b: /* FMULX */
7300 gen_helper_vfp_mulxs(tcg_res, tcg_op1, tcg_op2, fpst);
7301 break;
7302 case 0x1c: /* FCMEQ */
7303 gen_helper_neon_ceq_f32(tcg_res, tcg_op1, tcg_op2, fpst);
7304 break;
7305 case 0x1e: /* FMAX */
7306 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
7307 break;
7308 case 0x1f: /* FRECPS */
7309 gen_helper_recpsf_f32(tcg_res, tcg_op1, tcg_op2, fpst);
7310 break;
7311 case 0x18: /* FMAXNM */
7312 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
7313 break;
7314 case 0x38: /* FMINNM */
7315 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
7316 break;
7317 case 0x3a: /* FSUB */
7318 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
7319 break;
7320 case 0x3e: /* FMIN */
7321 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
7322 break;
7323 case 0x3f: /* FRSQRTS */
7324 gen_helper_rsqrtsf_f32(tcg_res, tcg_op1, tcg_op2, fpst);
7325 break;
7326 case 0x5b: /* FMUL */
7327 gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
7328 break;
7329 case 0x5c: /* FCMGE */
7330 gen_helper_neon_cge_f32(tcg_res, tcg_op1, tcg_op2, fpst);
7331 break;
7332 case 0x5d: /* FACGE */
7333 gen_helper_neon_acge_f32(tcg_res, tcg_op1, tcg_op2, fpst);
7334 break;
7335 case 0x5f: /* FDIV */
7336 gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
7337 break;
7338 case 0x7a: /* FABD */
7339 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
7340 gen_helper_vfp_abss(tcg_res, tcg_res);
7341 break;
7342 case 0x7c: /* FCMGT */
7343 gen_helper_neon_cgt_f32(tcg_res, tcg_op1, tcg_op2, fpst);
7344 break;
7345 case 0x7d: /* FACGT */
7346 gen_helper_neon_acgt_f32(tcg_res, tcg_op1, tcg_op2, fpst);
7347 break;
7348 default:
7349 g_assert_not_reached();
7352 if (elements == 1) {
7353 /* scalar single so clear high part */
7354 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
7356 tcg_gen_extu_i32_i64(tcg_tmp, tcg_res);
7357 write_vec_element(s, tcg_tmp, rd, pass, MO_64);
7358 tcg_temp_free_i64(tcg_tmp);
7359 } else {
7360 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
7363 tcg_temp_free_i32(tcg_res);
7364 tcg_temp_free_i32(tcg_op1);
7365 tcg_temp_free_i32(tcg_op2);
7369 tcg_temp_free_ptr(fpst);
7371 if ((elements << size) < 4) {
7372 /* scalar, or non-quad vector op */
7373 clear_vec_high(s, rd);
7377 /* C3.6.11 AdvSIMD scalar three same
7378 * 31 30 29 28 24 23 22 21 20 16 15 11 10 9 5 4 0
7379 * +-----+---+-----------+------+---+------+--------+---+------+------+
7380 * | 0 1 | U | 1 1 1 1 0 | size | 1 | Rm | opcode | 1 | Rn | Rd |
7381 * +-----+---+-----------+------+---+------+--------+---+------+------+
7383 static void disas_simd_scalar_three_reg_same(DisasContext *s, uint32_t insn)
7385 int rd = extract32(insn, 0, 5);
7386 int rn = extract32(insn, 5, 5);
7387 int opcode = extract32(insn, 11, 5);
7388 int rm = extract32(insn, 16, 5);
7389 int size = extract32(insn, 22, 2);
7390 bool u = extract32(insn, 29, 1);
7391 TCGv_i64 tcg_rd;
7393 if (opcode >= 0x18) {
7394 /* Floating point: U, size[1] and opcode indicate operation */
7395 int fpopcode = opcode | (extract32(size, 1, 1) << 5) | (u << 6);
7396 switch (fpopcode) {
7397 case 0x1b: /* FMULX */
7398 case 0x1f: /* FRECPS */
7399 case 0x3f: /* FRSQRTS */
7400 case 0x5d: /* FACGE */
7401 case 0x7d: /* FACGT */
7402 case 0x1c: /* FCMEQ */
7403 case 0x5c: /* FCMGE */
7404 case 0x7c: /* FCMGT */
7405 case 0x7a: /* FABD */
7406 break;
7407 default:
7408 unallocated_encoding(s);
7409 return;
7412 if (!fp_access_check(s)) {
7413 return;
7416 handle_3same_float(s, extract32(size, 0, 1), 1, fpopcode, rd, rn, rm);
7417 return;
7420 switch (opcode) {
7421 case 0x1: /* SQADD, UQADD */
7422 case 0x5: /* SQSUB, UQSUB */
7423 case 0x9: /* SQSHL, UQSHL */
7424 case 0xb: /* SQRSHL, UQRSHL */
7425 break;
7426 case 0x8: /* SSHL, USHL */
7427 case 0xa: /* SRSHL, URSHL */
7428 case 0x6: /* CMGT, CMHI */
7429 case 0x7: /* CMGE, CMHS */
7430 case 0x11: /* CMTST, CMEQ */
7431 case 0x10: /* ADD, SUB (vector) */
7432 if (size != 3) {
7433 unallocated_encoding(s);
7434 return;
7436 break;
7437 case 0x16: /* SQDMULH, SQRDMULH (vector) */
7438 if (size != 1 && size != 2) {
7439 unallocated_encoding(s);
7440 return;
7442 break;
7443 default:
7444 unallocated_encoding(s);
7445 return;
7448 if (!fp_access_check(s)) {
7449 return;
7452 tcg_rd = tcg_temp_new_i64();
7454 if (size == 3) {
7455 TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
7456 TCGv_i64 tcg_rm = read_fp_dreg(s, rm);
7458 handle_3same_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rm);
7459 tcg_temp_free_i64(tcg_rn);
7460 tcg_temp_free_i64(tcg_rm);
7461 } else {
7462 /* Do a single operation on the lowest element in the vector.
7463 * We use the standard Neon helpers and rely on 0 OP 0 == 0 with
7464 * no side effects for all these operations.
7465 * OPTME: special-purpose helpers would avoid doing some
7466 * unnecessary work in the helper for the 8 and 16 bit cases.
7468 NeonGenTwoOpEnvFn *genenvfn;
7469 TCGv_i32 tcg_rn = tcg_temp_new_i32();
7470 TCGv_i32 tcg_rm = tcg_temp_new_i32();
7471 TCGv_i32 tcg_rd32 = tcg_temp_new_i32();
7473 read_vec_element_i32(s, tcg_rn, rn, 0, size);
7474 read_vec_element_i32(s, tcg_rm, rm, 0, size);
7476 switch (opcode) {
7477 case 0x1: /* SQADD, UQADD */
7479 static NeonGenTwoOpEnvFn * const fns[3][2] = {
7480 { gen_helper_neon_qadd_s8, gen_helper_neon_qadd_u8 },
7481 { gen_helper_neon_qadd_s16, gen_helper_neon_qadd_u16 },
7482 { gen_helper_neon_qadd_s32, gen_helper_neon_qadd_u32 },
7484 genenvfn = fns[size][u];
7485 break;
7487 case 0x5: /* SQSUB, UQSUB */
7489 static NeonGenTwoOpEnvFn * const fns[3][2] = {
7490 { gen_helper_neon_qsub_s8, gen_helper_neon_qsub_u8 },
7491 { gen_helper_neon_qsub_s16, gen_helper_neon_qsub_u16 },
7492 { gen_helper_neon_qsub_s32, gen_helper_neon_qsub_u32 },
7494 genenvfn = fns[size][u];
7495 break;
7497 case 0x9: /* SQSHL, UQSHL */
7499 static NeonGenTwoOpEnvFn * const fns[3][2] = {
7500 { gen_helper_neon_qshl_s8, gen_helper_neon_qshl_u8 },
7501 { gen_helper_neon_qshl_s16, gen_helper_neon_qshl_u16 },
7502 { gen_helper_neon_qshl_s32, gen_helper_neon_qshl_u32 },
7504 genenvfn = fns[size][u];
7505 break;
7507 case 0xb: /* SQRSHL, UQRSHL */
7509 static NeonGenTwoOpEnvFn * const fns[3][2] = {
7510 { gen_helper_neon_qrshl_s8, gen_helper_neon_qrshl_u8 },
7511 { gen_helper_neon_qrshl_s16, gen_helper_neon_qrshl_u16 },
7512 { gen_helper_neon_qrshl_s32, gen_helper_neon_qrshl_u32 },
7514 genenvfn = fns[size][u];
7515 break;
7517 case 0x16: /* SQDMULH, SQRDMULH */
7519 static NeonGenTwoOpEnvFn * const fns[2][2] = {
7520 { gen_helper_neon_qdmulh_s16, gen_helper_neon_qrdmulh_s16 },
7521 { gen_helper_neon_qdmulh_s32, gen_helper_neon_qrdmulh_s32 },
7523 assert(size == 1 || size == 2);
7524 genenvfn = fns[size - 1][u];
7525 break;
7527 default:
7528 g_assert_not_reached();
7531 genenvfn(tcg_rd32, cpu_env, tcg_rn, tcg_rm);
7532 tcg_gen_extu_i32_i64(tcg_rd, tcg_rd32);
7533 tcg_temp_free_i32(tcg_rd32);
7534 tcg_temp_free_i32(tcg_rn);
7535 tcg_temp_free_i32(tcg_rm);
7538 write_fp_dreg(s, rd, tcg_rd);
7540 tcg_temp_free_i64(tcg_rd);
7543 static void handle_2misc_64(DisasContext *s, int opcode, bool u,
7544 TCGv_i64 tcg_rd, TCGv_i64 tcg_rn,
7545 TCGv_i32 tcg_rmode, TCGv_ptr tcg_fpstatus)
7547 /* Handle 64->64 opcodes which are shared between the scalar and
7548 * vector 2-reg-misc groups. We cover every integer opcode where size == 3
7549 * is valid in either group and also the double-precision fp ops.
7550 * The caller only need provide tcg_rmode and tcg_fpstatus if the op
7551 * requires them.
7553 TCGCond cond;
7555 switch (opcode) {
7556 case 0x4: /* CLS, CLZ */
7557 if (u) {
7558 gen_helper_clz64(tcg_rd, tcg_rn);
7559 } else {
7560 gen_helper_cls64(tcg_rd, tcg_rn);
7562 break;
7563 case 0x5: /* NOT */
7564 /* This opcode is shared with CNT and RBIT but we have earlier
7565 * enforced that size == 3 if and only if this is the NOT insn.
7567 tcg_gen_not_i64(tcg_rd, tcg_rn);
7568 break;
7569 case 0x7: /* SQABS, SQNEG */
7570 if (u) {
7571 gen_helper_neon_qneg_s64(tcg_rd, cpu_env, tcg_rn);
7572 } else {
7573 gen_helper_neon_qabs_s64(tcg_rd, cpu_env, tcg_rn);
7575 break;
7576 case 0xa: /* CMLT */
7577 /* 64 bit integer comparison against zero, result is
7578 * test ? (2^64 - 1) : 0. We implement via setcond(!test) and
7579 * subtracting 1.
7581 cond = TCG_COND_LT;
7582 do_cmop:
7583 tcg_gen_setcondi_i64(cond, tcg_rd, tcg_rn, 0);
7584 tcg_gen_neg_i64(tcg_rd, tcg_rd);
7585 break;
7586 case 0x8: /* CMGT, CMGE */
7587 cond = u ? TCG_COND_GE : TCG_COND_GT;
7588 goto do_cmop;
7589 case 0x9: /* CMEQ, CMLE */
7590 cond = u ? TCG_COND_LE : TCG_COND_EQ;
7591 goto do_cmop;
7592 case 0xb: /* ABS, NEG */
7593 if (u) {
7594 tcg_gen_neg_i64(tcg_rd, tcg_rn);
7595 } else {
7596 TCGv_i64 tcg_zero = tcg_const_i64(0);
7597 tcg_gen_neg_i64(tcg_rd, tcg_rn);
7598 tcg_gen_movcond_i64(TCG_COND_GT, tcg_rd, tcg_rn, tcg_zero,
7599 tcg_rn, tcg_rd);
7600 tcg_temp_free_i64(tcg_zero);
7602 break;
7603 case 0x2f: /* FABS */
7604 gen_helper_vfp_absd(tcg_rd, tcg_rn);
7605 break;
7606 case 0x6f: /* FNEG */
7607 gen_helper_vfp_negd(tcg_rd, tcg_rn);
7608 break;
7609 case 0x7f: /* FSQRT */
7610 gen_helper_vfp_sqrtd(tcg_rd, tcg_rn, cpu_env);
7611 break;
7612 case 0x1a: /* FCVTNS */
7613 case 0x1b: /* FCVTMS */
7614 case 0x1c: /* FCVTAS */
7615 case 0x3a: /* FCVTPS */
7616 case 0x3b: /* FCVTZS */
7618 TCGv_i32 tcg_shift = tcg_const_i32(0);
7619 gen_helper_vfp_tosqd(tcg_rd, tcg_rn, tcg_shift, tcg_fpstatus);
7620 tcg_temp_free_i32(tcg_shift);
7621 break;
7623 case 0x5a: /* FCVTNU */
7624 case 0x5b: /* FCVTMU */
7625 case 0x5c: /* FCVTAU */
7626 case 0x7a: /* FCVTPU */
7627 case 0x7b: /* FCVTZU */
7629 TCGv_i32 tcg_shift = tcg_const_i32(0);
7630 gen_helper_vfp_touqd(tcg_rd, tcg_rn, tcg_shift, tcg_fpstatus);
7631 tcg_temp_free_i32(tcg_shift);
7632 break;
7634 case 0x18: /* FRINTN */
7635 case 0x19: /* FRINTM */
7636 case 0x38: /* FRINTP */
7637 case 0x39: /* FRINTZ */
7638 case 0x58: /* FRINTA */
7639 case 0x79: /* FRINTI */
7640 gen_helper_rintd(tcg_rd, tcg_rn, tcg_fpstatus);
7641 break;
7642 case 0x59: /* FRINTX */
7643 gen_helper_rintd_exact(tcg_rd, tcg_rn, tcg_fpstatus);
7644 break;
7645 default:
7646 g_assert_not_reached();
7650 static void handle_2misc_fcmp_zero(DisasContext *s, int opcode,
7651 bool is_scalar, bool is_u, bool is_q,
7652 int size, int rn, int rd)
7654 bool is_double = (size == 3);
7655 TCGv_ptr fpst;
7657 if (!fp_access_check(s)) {
7658 return;
7661 fpst = get_fpstatus_ptr();
7663 if (is_double) {
7664 TCGv_i64 tcg_op = tcg_temp_new_i64();
7665 TCGv_i64 tcg_zero = tcg_const_i64(0);
7666 TCGv_i64 tcg_res = tcg_temp_new_i64();
7667 NeonGenTwoDoubleOPFn *genfn;
7668 bool swap = false;
7669 int pass;
7671 switch (opcode) {
7672 case 0x2e: /* FCMLT (zero) */
7673 swap = true;
7674 /* fallthrough */
7675 case 0x2c: /* FCMGT (zero) */
7676 genfn = gen_helper_neon_cgt_f64;
7677 break;
7678 case 0x2d: /* FCMEQ (zero) */
7679 genfn = gen_helper_neon_ceq_f64;
7680 break;
7681 case 0x6d: /* FCMLE (zero) */
7682 swap = true;
7683 /* fall through */
7684 case 0x6c: /* FCMGE (zero) */
7685 genfn = gen_helper_neon_cge_f64;
7686 break;
7687 default:
7688 g_assert_not_reached();
7691 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
7692 read_vec_element(s, tcg_op, rn, pass, MO_64);
7693 if (swap) {
7694 genfn(tcg_res, tcg_zero, tcg_op, fpst);
7695 } else {
7696 genfn(tcg_res, tcg_op, tcg_zero, fpst);
7698 write_vec_element(s, tcg_res, rd, pass, MO_64);
7700 if (is_scalar) {
7701 clear_vec_high(s, rd);
7704 tcg_temp_free_i64(tcg_res);
7705 tcg_temp_free_i64(tcg_zero);
7706 tcg_temp_free_i64(tcg_op);
7707 } else {
7708 TCGv_i32 tcg_op = tcg_temp_new_i32();
7709 TCGv_i32 tcg_zero = tcg_const_i32(0);
7710 TCGv_i32 tcg_res = tcg_temp_new_i32();
7711 NeonGenTwoSingleOPFn *genfn;
7712 bool swap = false;
7713 int pass, maxpasses;
7715 switch (opcode) {
7716 case 0x2e: /* FCMLT (zero) */
7717 swap = true;
7718 /* fall through */
7719 case 0x2c: /* FCMGT (zero) */
7720 genfn = gen_helper_neon_cgt_f32;
7721 break;
7722 case 0x2d: /* FCMEQ (zero) */
7723 genfn = gen_helper_neon_ceq_f32;
7724 break;
7725 case 0x6d: /* FCMLE (zero) */
7726 swap = true;
7727 /* fall through */
7728 case 0x6c: /* FCMGE (zero) */
7729 genfn = gen_helper_neon_cge_f32;
7730 break;
7731 default:
7732 g_assert_not_reached();
7735 if (is_scalar) {
7736 maxpasses = 1;
7737 } else {
7738 maxpasses = is_q ? 4 : 2;
7741 for (pass = 0; pass < maxpasses; pass++) {
7742 read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
7743 if (swap) {
7744 genfn(tcg_res, tcg_zero, tcg_op, fpst);
7745 } else {
7746 genfn(tcg_res, tcg_op, tcg_zero, fpst);
7748 if (is_scalar) {
7749 write_fp_sreg(s, rd, tcg_res);
7750 } else {
7751 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
7754 tcg_temp_free_i32(tcg_res);
7755 tcg_temp_free_i32(tcg_zero);
7756 tcg_temp_free_i32(tcg_op);
7757 if (!is_q && !is_scalar) {
7758 clear_vec_high(s, rd);
7762 tcg_temp_free_ptr(fpst);
7765 static void handle_2misc_reciprocal(DisasContext *s, int opcode,
7766 bool is_scalar, bool is_u, bool is_q,
7767 int size, int rn, int rd)
7769 bool is_double = (size == 3);
7770 TCGv_ptr fpst = get_fpstatus_ptr();
7772 if (is_double) {
7773 TCGv_i64 tcg_op = tcg_temp_new_i64();
7774 TCGv_i64 tcg_res = tcg_temp_new_i64();
7775 int pass;
7777 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
7778 read_vec_element(s, tcg_op, rn, pass, MO_64);
7779 switch (opcode) {
7780 case 0x3d: /* FRECPE */
7781 gen_helper_recpe_f64(tcg_res, tcg_op, fpst);
7782 break;
7783 case 0x3f: /* FRECPX */
7784 gen_helper_frecpx_f64(tcg_res, tcg_op, fpst);
7785 break;
7786 case 0x7d: /* FRSQRTE */
7787 gen_helper_rsqrte_f64(tcg_res, tcg_op, fpst);
7788 break;
7789 default:
7790 g_assert_not_reached();
7792 write_vec_element(s, tcg_res, rd, pass, MO_64);
7794 if (is_scalar) {
7795 clear_vec_high(s, rd);
7798 tcg_temp_free_i64(tcg_res);
7799 tcg_temp_free_i64(tcg_op);
7800 } else {
7801 TCGv_i32 tcg_op = tcg_temp_new_i32();
7802 TCGv_i32 tcg_res = tcg_temp_new_i32();
7803 int pass, maxpasses;
7805 if (is_scalar) {
7806 maxpasses = 1;
7807 } else {
7808 maxpasses = is_q ? 4 : 2;
7811 for (pass = 0; pass < maxpasses; pass++) {
7812 read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
7814 switch (opcode) {
7815 case 0x3c: /* URECPE */
7816 gen_helper_recpe_u32(tcg_res, tcg_op, fpst);
7817 break;
7818 case 0x3d: /* FRECPE */
7819 gen_helper_recpe_f32(tcg_res, tcg_op, fpst);
7820 break;
7821 case 0x3f: /* FRECPX */
7822 gen_helper_frecpx_f32(tcg_res, tcg_op, fpst);
7823 break;
7824 case 0x7d: /* FRSQRTE */
7825 gen_helper_rsqrte_f32(tcg_res, tcg_op, fpst);
7826 break;
7827 default:
7828 g_assert_not_reached();
7831 if (is_scalar) {
7832 write_fp_sreg(s, rd, tcg_res);
7833 } else {
7834 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
7837 tcg_temp_free_i32(tcg_res);
7838 tcg_temp_free_i32(tcg_op);
7839 if (!is_q && !is_scalar) {
7840 clear_vec_high(s, rd);
7843 tcg_temp_free_ptr(fpst);
7846 static void handle_2misc_narrow(DisasContext *s, bool scalar,
7847 int opcode, bool u, bool is_q,
7848 int size, int rn, int rd)
7850 /* Handle 2-reg-misc ops which are narrowing (so each 2*size element
7851 * in the source becomes a size element in the destination).
7853 int pass;
7854 TCGv_i32 tcg_res[2];
7855 int destelt = is_q ? 2 : 0;
7856 int passes = scalar ? 1 : 2;
7858 if (scalar) {
7859 tcg_res[1] = tcg_const_i32(0);
7862 for (pass = 0; pass < passes; pass++) {
7863 TCGv_i64 tcg_op = tcg_temp_new_i64();
7864 NeonGenNarrowFn *genfn = NULL;
7865 NeonGenNarrowEnvFn *genenvfn = NULL;
7867 if (scalar) {
7868 read_vec_element(s, tcg_op, rn, pass, size + 1);
7869 } else {
7870 read_vec_element(s, tcg_op, rn, pass, MO_64);
7872 tcg_res[pass] = tcg_temp_new_i32();
7874 switch (opcode) {
7875 case 0x12: /* XTN, SQXTUN */
7877 static NeonGenNarrowFn * const xtnfns[3] = {
7878 gen_helper_neon_narrow_u8,
7879 gen_helper_neon_narrow_u16,
7880 tcg_gen_extrl_i64_i32,
7882 static NeonGenNarrowEnvFn * const sqxtunfns[3] = {
7883 gen_helper_neon_unarrow_sat8,
7884 gen_helper_neon_unarrow_sat16,
7885 gen_helper_neon_unarrow_sat32,
7887 if (u) {
7888 genenvfn = sqxtunfns[size];
7889 } else {
7890 genfn = xtnfns[size];
7892 break;
7894 case 0x14: /* SQXTN, UQXTN */
7896 static NeonGenNarrowEnvFn * const fns[3][2] = {
7897 { gen_helper_neon_narrow_sat_s8,
7898 gen_helper_neon_narrow_sat_u8 },
7899 { gen_helper_neon_narrow_sat_s16,
7900 gen_helper_neon_narrow_sat_u16 },
7901 { gen_helper_neon_narrow_sat_s32,
7902 gen_helper_neon_narrow_sat_u32 },
7904 genenvfn = fns[size][u];
7905 break;
7907 case 0x16: /* FCVTN, FCVTN2 */
7908 /* 32 bit to 16 bit or 64 bit to 32 bit float conversion */
7909 if (size == 2) {
7910 gen_helper_vfp_fcvtsd(tcg_res[pass], tcg_op, cpu_env);
7911 } else {
7912 TCGv_i32 tcg_lo = tcg_temp_new_i32();
7913 TCGv_i32 tcg_hi = tcg_temp_new_i32();
7914 tcg_gen_extr_i64_i32(tcg_lo, tcg_hi, tcg_op);
7915 gen_helper_vfp_fcvt_f32_to_f16(tcg_lo, tcg_lo, cpu_env);
7916 gen_helper_vfp_fcvt_f32_to_f16(tcg_hi, tcg_hi, cpu_env);
7917 tcg_gen_deposit_i32(tcg_res[pass], tcg_lo, tcg_hi, 16, 16);
7918 tcg_temp_free_i32(tcg_lo);
7919 tcg_temp_free_i32(tcg_hi);
7921 break;
7922 case 0x56: /* FCVTXN, FCVTXN2 */
7923 /* 64 bit to 32 bit float conversion
7924 * with von Neumann rounding (round to odd)
7926 assert(size == 2);
7927 gen_helper_fcvtx_f64_to_f32(tcg_res[pass], tcg_op, cpu_env);
7928 break;
7929 default:
7930 g_assert_not_reached();
7933 if (genfn) {
7934 genfn(tcg_res[pass], tcg_op);
7935 } else if (genenvfn) {
7936 genenvfn(tcg_res[pass], cpu_env, tcg_op);
7939 tcg_temp_free_i64(tcg_op);
7942 for (pass = 0; pass < 2; pass++) {
7943 write_vec_element_i32(s, tcg_res[pass], rd, destelt + pass, MO_32);
7944 tcg_temp_free_i32(tcg_res[pass]);
7946 if (!is_q) {
7947 clear_vec_high(s, rd);
7951 /* Remaining saturating accumulating ops */
7952 static void handle_2misc_satacc(DisasContext *s, bool is_scalar, bool is_u,
7953 bool is_q, int size, int rn, int rd)
7955 bool is_double = (size == 3);
7957 if (is_double) {
7958 TCGv_i64 tcg_rn = tcg_temp_new_i64();
7959 TCGv_i64 tcg_rd = tcg_temp_new_i64();
7960 int pass;
7962 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
7963 read_vec_element(s, tcg_rn, rn, pass, MO_64);
7964 read_vec_element(s, tcg_rd, rd, pass, MO_64);
7966 if (is_u) { /* USQADD */
7967 gen_helper_neon_uqadd_s64(tcg_rd, cpu_env, tcg_rn, tcg_rd);
7968 } else { /* SUQADD */
7969 gen_helper_neon_sqadd_u64(tcg_rd, cpu_env, tcg_rn, tcg_rd);
7971 write_vec_element(s, tcg_rd, rd, pass, MO_64);
7973 if (is_scalar) {
7974 clear_vec_high(s, rd);
7977 tcg_temp_free_i64(tcg_rd);
7978 tcg_temp_free_i64(tcg_rn);
7979 } else {
7980 TCGv_i32 tcg_rn = tcg_temp_new_i32();
7981 TCGv_i32 tcg_rd = tcg_temp_new_i32();
7982 int pass, maxpasses;
7984 if (is_scalar) {
7985 maxpasses = 1;
7986 } else {
7987 maxpasses = is_q ? 4 : 2;
7990 for (pass = 0; pass < maxpasses; pass++) {
7991 if (is_scalar) {
7992 read_vec_element_i32(s, tcg_rn, rn, pass, size);
7993 read_vec_element_i32(s, tcg_rd, rd, pass, size);
7994 } else {
7995 read_vec_element_i32(s, tcg_rn, rn, pass, MO_32);
7996 read_vec_element_i32(s, tcg_rd, rd, pass, MO_32);
7999 if (is_u) { /* USQADD */
8000 switch (size) {
8001 case 0:
8002 gen_helper_neon_uqadd_s8(tcg_rd, cpu_env, tcg_rn, tcg_rd);
8003 break;
8004 case 1:
8005 gen_helper_neon_uqadd_s16(tcg_rd, cpu_env, tcg_rn, tcg_rd);
8006 break;
8007 case 2:
8008 gen_helper_neon_uqadd_s32(tcg_rd, cpu_env, tcg_rn, tcg_rd);
8009 break;
8010 default:
8011 g_assert_not_reached();
8013 } else { /* SUQADD */
8014 switch (size) {
8015 case 0:
8016 gen_helper_neon_sqadd_u8(tcg_rd, cpu_env, tcg_rn, tcg_rd);
8017 break;
8018 case 1:
8019 gen_helper_neon_sqadd_u16(tcg_rd, cpu_env, tcg_rn, tcg_rd);
8020 break;
8021 case 2:
8022 gen_helper_neon_sqadd_u32(tcg_rd, cpu_env, tcg_rn, tcg_rd);
8023 break;
8024 default:
8025 g_assert_not_reached();
8029 if (is_scalar) {
8030 TCGv_i64 tcg_zero = tcg_const_i64(0);
8031 write_vec_element(s, tcg_zero, rd, 0, MO_64);
8032 tcg_temp_free_i64(tcg_zero);
8034 write_vec_element_i32(s, tcg_rd, rd, pass, MO_32);
8037 if (!is_q) {
8038 clear_vec_high(s, rd);
8041 tcg_temp_free_i32(tcg_rd);
8042 tcg_temp_free_i32(tcg_rn);
8046 /* C3.6.12 AdvSIMD scalar two reg misc
8047 * 31 30 29 28 24 23 22 21 17 16 12 11 10 9 5 4 0
8048 * +-----+---+-----------+------+-----------+--------+-----+------+------+
8049 * | 0 1 | U | 1 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 | Rn | Rd |
8050 * +-----+---+-----------+------+-----------+--------+-----+------+------+
8052 static void disas_simd_scalar_two_reg_misc(DisasContext *s, uint32_t insn)
8054 int rd = extract32(insn, 0, 5);
8055 int rn = extract32(insn, 5, 5);
8056 int opcode = extract32(insn, 12, 5);
8057 int size = extract32(insn, 22, 2);
8058 bool u = extract32(insn, 29, 1);
8059 bool is_fcvt = false;
8060 int rmode;
8061 TCGv_i32 tcg_rmode;
8062 TCGv_ptr tcg_fpstatus;
8064 switch (opcode) {
8065 case 0x3: /* USQADD / SUQADD*/
8066 if (!fp_access_check(s)) {
8067 return;
8069 handle_2misc_satacc(s, true, u, false, size, rn, rd);
8070 return;
8071 case 0x7: /* SQABS / SQNEG */
8072 break;
8073 case 0xa: /* CMLT */
8074 if (u) {
8075 unallocated_encoding(s);
8076 return;
8078 /* fall through */
8079 case 0x8: /* CMGT, CMGE */
8080 case 0x9: /* CMEQ, CMLE */
8081 case 0xb: /* ABS, NEG */
8082 if (size != 3) {
8083 unallocated_encoding(s);
8084 return;
8086 break;
8087 case 0x12: /* SQXTUN */
8088 if (!u) {
8089 unallocated_encoding(s);
8090 return;
8092 /* fall through */
8093 case 0x14: /* SQXTN, UQXTN */
8094 if (size == 3) {
8095 unallocated_encoding(s);
8096 return;
8098 if (!fp_access_check(s)) {
8099 return;
8101 handle_2misc_narrow(s, true, opcode, u, false, size, rn, rd);
8102 return;
8103 case 0xc ... 0xf:
8104 case 0x16 ... 0x1d:
8105 case 0x1f:
8106 /* Floating point: U, size[1] and opcode indicate operation;
8107 * size[0] indicates single or double precision.
8109 opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
8110 size = extract32(size, 0, 1) ? 3 : 2;
8111 switch (opcode) {
8112 case 0x2c: /* FCMGT (zero) */
8113 case 0x2d: /* FCMEQ (zero) */
8114 case 0x2e: /* FCMLT (zero) */
8115 case 0x6c: /* FCMGE (zero) */
8116 case 0x6d: /* FCMLE (zero) */
8117 handle_2misc_fcmp_zero(s, opcode, true, u, true, size, rn, rd);
8118 return;
8119 case 0x1d: /* SCVTF */
8120 case 0x5d: /* UCVTF */
8122 bool is_signed = (opcode == 0x1d);
8123 if (!fp_access_check(s)) {
8124 return;
8126 handle_simd_intfp_conv(s, rd, rn, 1, is_signed, 0, size);
8127 return;
8129 case 0x3d: /* FRECPE */
8130 case 0x3f: /* FRECPX */
8131 case 0x7d: /* FRSQRTE */
8132 if (!fp_access_check(s)) {
8133 return;
8135 handle_2misc_reciprocal(s, opcode, true, u, true, size, rn, rd);
8136 return;
8137 case 0x1a: /* FCVTNS */
8138 case 0x1b: /* FCVTMS */
8139 case 0x3a: /* FCVTPS */
8140 case 0x3b: /* FCVTZS */
8141 case 0x5a: /* FCVTNU */
8142 case 0x5b: /* FCVTMU */
8143 case 0x7a: /* FCVTPU */
8144 case 0x7b: /* FCVTZU */
8145 is_fcvt = true;
8146 rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
8147 break;
8148 case 0x1c: /* FCVTAS */
8149 case 0x5c: /* FCVTAU */
8150 /* TIEAWAY doesn't fit in the usual rounding mode encoding */
8151 is_fcvt = true;
8152 rmode = FPROUNDING_TIEAWAY;
8153 break;
8154 case 0x56: /* FCVTXN, FCVTXN2 */
8155 if (size == 2) {
8156 unallocated_encoding(s);
8157 return;
8159 if (!fp_access_check(s)) {
8160 return;
8162 handle_2misc_narrow(s, true, opcode, u, false, size - 1, rn, rd);
8163 return;
8164 default:
8165 unallocated_encoding(s);
8166 return;
8168 break;
8169 default:
8170 unallocated_encoding(s);
8171 return;
8174 if (!fp_access_check(s)) {
8175 return;
8178 if (is_fcvt) {
8179 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rmode));
8180 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
8181 tcg_fpstatus = get_fpstatus_ptr();
8182 } else {
8183 TCGV_UNUSED_I32(tcg_rmode);
8184 TCGV_UNUSED_PTR(tcg_fpstatus);
8187 if (size == 3) {
8188 TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
8189 TCGv_i64 tcg_rd = tcg_temp_new_i64();
8191 handle_2misc_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rmode, tcg_fpstatus);
8192 write_fp_dreg(s, rd, tcg_rd);
8193 tcg_temp_free_i64(tcg_rd);
8194 tcg_temp_free_i64(tcg_rn);
8195 } else {
8196 TCGv_i32 tcg_rn = tcg_temp_new_i32();
8197 TCGv_i32 tcg_rd = tcg_temp_new_i32();
8199 read_vec_element_i32(s, tcg_rn, rn, 0, size);
8201 switch (opcode) {
8202 case 0x7: /* SQABS, SQNEG */
8204 NeonGenOneOpEnvFn *genfn;
8205 static NeonGenOneOpEnvFn * const fns[3][2] = {
8206 { gen_helper_neon_qabs_s8, gen_helper_neon_qneg_s8 },
8207 { gen_helper_neon_qabs_s16, gen_helper_neon_qneg_s16 },
8208 { gen_helper_neon_qabs_s32, gen_helper_neon_qneg_s32 },
8210 genfn = fns[size][u];
8211 genfn(tcg_rd, cpu_env, tcg_rn);
8212 break;
8214 case 0x1a: /* FCVTNS */
8215 case 0x1b: /* FCVTMS */
8216 case 0x1c: /* FCVTAS */
8217 case 0x3a: /* FCVTPS */
8218 case 0x3b: /* FCVTZS */
8220 TCGv_i32 tcg_shift = tcg_const_i32(0);
8221 gen_helper_vfp_tosls(tcg_rd, tcg_rn, tcg_shift, tcg_fpstatus);
8222 tcg_temp_free_i32(tcg_shift);
8223 break;
8225 case 0x5a: /* FCVTNU */
8226 case 0x5b: /* FCVTMU */
8227 case 0x5c: /* FCVTAU */
8228 case 0x7a: /* FCVTPU */
8229 case 0x7b: /* FCVTZU */
8231 TCGv_i32 tcg_shift = tcg_const_i32(0);
8232 gen_helper_vfp_touls(tcg_rd, tcg_rn, tcg_shift, tcg_fpstatus);
8233 tcg_temp_free_i32(tcg_shift);
8234 break;
8236 default:
8237 g_assert_not_reached();
8240 write_fp_sreg(s, rd, tcg_rd);
8241 tcg_temp_free_i32(tcg_rd);
8242 tcg_temp_free_i32(tcg_rn);
8245 if (is_fcvt) {
8246 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
8247 tcg_temp_free_i32(tcg_rmode);
8248 tcg_temp_free_ptr(tcg_fpstatus);
8252 /* SSHR[RA]/USHR[RA] - Vector shift right (optional rounding/accumulate) */
8253 static void handle_vec_simd_shri(DisasContext *s, bool is_q, bool is_u,
8254 int immh, int immb, int opcode, int rn, int rd)
8256 int size = 32 - clz32(immh) - 1;
8257 int immhb = immh << 3 | immb;
8258 int shift = 2 * (8 << size) - immhb;
8259 bool accumulate = false;
8260 bool round = false;
8261 bool insert = false;
8262 int dsize = is_q ? 128 : 64;
8263 int esize = 8 << size;
8264 int elements = dsize/esize;
8265 TCGMemOp memop = size | (is_u ? 0 : MO_SIGN);
8266 TCGv_i64 tcg_rn = new_tmp_a64(s);
8267 TCGv_i64 tcg_rd = new_tmp_a64(s);
8268 TCGv_i64 tcg_round;
8269 int i;
8271 if (extract32(immh, 3, 1) && !is_q) {
8272 unallocated_encoding(s);
8273 return;
8276 if (size > 3 && !is_q) {
8277 unallocated_encoding(s);
8278 return;
8281 if (!fp_access_check(s)) {
8282 return;
8285 switch (opcode) {
8286 case 0x02: /* SSRA / USRA (accumulate) */
8287 accumulate = true;
8288 break;
8289 case 0x04: /* SRSHR / URSHR (rounding) */
8290 round = true;
8291 break;
8292 case 0x06: /* SRSRA / URSRA (accum + rounding) */
8293 accumulate = round = true;
8294 break;
8295 case 0x08: /* SRI */
8296 insert = true;
8297 break;
8300 if (round) {
8301 uint64_t round_const = 1ULL << (shift - 1);
8302 tcg_round = tcg_const_i64(round_const);
8303 } else {
8304 TCGV_UNUSED_I64(tcg_round);
8307 for (i = 0; i < elements; i++) {
8308 read_vec_element(s, tcg_rn, rn, i, memop);
8309 if (accumulate || insert) {
8310 read_vec_element(s, tcg_rd, rd, i, memop);
8313 if (insert) {
8314 handle_shri_with_ins(tcg_rd, tcg_rn, size, shift);
8315 } else {
8316 handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
8317 accumulate, is_u, size, shift);
8320 write_vec_element(s, tcg_rd, rd, i, size);
8323 if (!is_q) {
8324 clear_vec_high(s, rd);
8327 if (round) {
8328 tcg_temp_free_i64(tcg_round);
8332 /* SHL/SLI - Vector shift left */
8333 static void handle_vec_simd_shli(DisasContext *s, bool is_q, bool insert,
8334 int immh, int immb, int opcode, int rn, int rd)
8336 int size = 32 - clz32(immh) - 1;
8337 int immhb = immh << 3 | immb;
8338 int shift = immhb - (8 << size);
8339 int dsize = is_q ? 128 : 64;
8340 int esize = 8 << size;
8341 int elements = dsize/esize;
8342 TCGv_i64 tcg_rn = new_tmp_a64(s);
8343 TCGv_i64 tcg_rd = new_tmp_a64(s);
8344 int i;
8346 if (extract32(immh, 3, 1) && !is_q) {
8347 unallocated_encoding(s);
8348 return;
8351 if (size > 3 && !is_q) {
8352 unallocated_encoding(s);
8353 return;
8356 if (!fp_access_check(s)) {
8357 return;
8360 for (i = 0; i < elements; i++) {
8361 read_vec_element(s, tcg_rn, rn, i, size);
8362 if (insert) {
8363 read_vec_element(s, tcg_rd, rd, i, size);
8366 handle_shli_with_ins(tcg_rd, tcg_rn, insert, shift);
8368 write_vec_element(s, tcg_rd, rd, i, size);
8371 if (!is_q) {
8372 clear_vec_high(s, rd);
8376 /* USHLL/SHLL - Vector shift left with widening */
8377 static void handle_vec_simd_wshli(DisasContext *s, bool is_q, bool is_u,
8378 int immh, int immb, int opcode, int rn, int rd)
8380 int size = 32 - clz32(immh) - 1;
8381 int immhb = immh << 3 | immb;
8382 int shift = immhb - (8 << size);
8383 int dsize = 64;
8384 int esize = 8 << size;
8385 int elements = dsize/esize;
8386 TCGv_i64 tcg_rn = new_tmp_a64(s);
8387 TCGv_i64 tcg_rd = new_tmp_a64(s);
8388 int i;
8390 if (size >= 3) {
8391 unallocated_encoding(s);
8392 return;
8395 if (!fp_access_check(s)) {
8396 return;
8399 /* For the LL variants the store is larger than the load,
8400 * so if rd == rn we would overwrite parts of our input.
8401 * So load everything right now and use shifts in the main loop.
8403 read_vec_element(s, tcg_rn, rn, is_q ? 1 : 0, MO_64);
8405 for (i = 0; i < elements; i++) {
8406 tcg_gen_shri_i64(tcg_rd, tcg_rn, i * esize);
8407 ext_and_shift_reg(tcg_rd, tcg_rd, size | (!is_u << 2), 0);
8408 tcg_gen_shli_i64(tcg_rd, tcg_rd, shift);
8409 write_vec_element(s, tcg_rd, rd, i, size + 1);
8413 /* SHRN/RSHRN - Shift right with narrowing (and potential rounding) */
8414 static void handle_vec_simd_shrn(DisasContext *s, bool is_q,
8415 int immh, int immb, int opcode, int rn, int rd)
8417 int immhb = immh << 3 | immb;
8418 int size = 32 - clz32(immh) - 1;
8419 int dsize = 64;
8420 int esize = 8 << size;
8421 int elements = dsize/esize;
8422 int shift = (2 * esize) - immhb;
8423 bool round = extract32(opcode, 0, 1);
8424 TCGv_i64 tcg_rn, tcg_rd, tcg_final;
8425 TCGv_i64 tcg_round;
8426 int i;
8428 if (extract32(immh, 3, 1)) {
8429 unallocated_encoding(s);
8430 return;
8433 if (!fp_access_check(s)) {
8434 return;
8437 tcg_rn = tcg_temp_new_i64();
8438 tcg_rd = tcg_temp_new_i64();
8439 tcg_final = tcg_temp_new_i64();
8440 read_vec_element(s, tcg_final, rd, is_q ? 1 : 0, MO_64);
8442 if (round) {
8443 uint64_t round_const = 1ULL << (shift - 1);
8444 tcg_round = tcg_const_i64(round_const);
8445 } else {
8446 TCGV_UNUSED_I64(tcg_round);
8449 for (i = 0; i < elements; i++) {
8450 read_vec_element(s, tcg_rn, rn, i, size+1);
8451 handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
8452 false, true, size+1, shift);
8454 tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
8457 if (!is_q) {
8458 clear_vec_high(s, rd);
8459 write_vec_element(s, tcg_final, rd, 0, MO_64);
8460 } else {
8461 write_vec_element(s, tcg_final, rd, 1, MO_64);
8464 if (round) {
8465 tcg_temp_free_i64(tcg_round);
8467 tcg_temp_free_i64(tcg_rn);
8468 tcg_temp_free_i64(tcg_rd);
8469 tcg_temp_free_i64(tcg_final);
8470 return;
8474 /* C3.6.14 AdvSIMD shift by immediate
8475 * 31 30 29 28 23 22 19 18 16 15 11 10 9 5 4 0
8476 * +---+---+---+-------------+------+------+--------+---+------+------+
8477 * | 0 | Q | U | 0 1 1 1 1 0 | immh | immb | opcode | 1 | Rn | Rd |
8478 * +---+---+---+-------------+------+------+--------+---+------+------+
8480 static void disas_simd_shift_imm(DisasContext *s, uint32_t insn)
8482 int rd = extract32(insn, 0, 5);
8483 int rn = extract32(insn, 5, 5);
8484 int opcode = extract32(insn, 11, 5);
8485 int immb = extract32(insn, 16, 3);
8486 int immh = extract32(insn, 19, 4);
8487 bool is_u = extract32(insn, 29, 1);
8488 bool is_q = extract32(insn, 30, 1);
8490 switch (opcode) {
8491 case 0x08: /* SRI */
8492 if (!is_u) {
8493 unallocated_encoding(s);
8494 return;
8496 /* fall through */
8497 case 0x00: /* SSHR / USHR */
8498 case 0x02: /* SSRA / USRA (accumulate) */
8499 case 0x04: /* SRSHR / URSHR (rounding) */
8500 case 0x06: /* SRSRA / URSRA (accum + rounding) */
8501 handle_vec_simd_shri(s, is_q, is_u, immh, immb, opcode, rn, rd);
8502 break;
8503 case 0x0a: /* SHL / SLI */
8504 handle_vec_simd_shli(s, is_q, is_u, immh, immb, opcode, rn, rd);
8505 break;
8506 case 0x10: /* SHRN */
8507 case 0x11: /* RSHRN / SQRSHRUN */
8508 if (is_u) {
8509 handle_vec_simd_sqshrn(s, false, is_q, false, true, immh, immb,
8510 opcode, rn, rd);
8511 } else {
8512 handle_vec_simd_shrn(s, is_q, immh, immb, opcode, rn, rd);
8514 break;
8515 case 0x12: /* SQSHRN / UQSHRN */
8516 case 0x13: /* SQRSHRN / UQRSHRN */
8517 handle_vec_simd_sqshrn(s, false, is_q, is_u, is_u, immh, immb,
8518 opcode, rn, rd);
8519 break;
8520 case 0x14: /* SSHLL / USHLL */
8521 handle_vec_simd_wshli(s, is_q, is_u, immh, immb, opcode, rn, rd);
8522 break;
8523 case 0x1c: /* SCVTF / UCVTF */
8524 handle_simd_shift_intfp_conv(s, false, is_q, is_u, immh, immb,
8525 opcode, rn, rd);
8526 break;
8527 case 0xc: /* SQSHLU */
8528 if (!is_u) {
8529 unallocated_encoding(s);
8530 return;
8532 handle_simd_qshl(s, false, is_q, false, true, immh, immb, rn, rd);
8533 break;
8534 case 0xe: /* SQSHL, UQSHL */
8535 handle_simd_qshl(s, false, is_q, is_u, is_u, immh, immb, rn, rd);
8536 break;
8537 case 0x1f: /* FCVTZS/ FCVTZU */
8538 handle_simd_shift_fpint_conv(s, false, is_q, is_u, immh, immb, rn, rd);
8539 return;
8540 default:
8541 unallocated_encoding(s);
8542 return;
8546 /* Generate code to do a "long" addition or subtraction, ie one done in
8547 * TCGv_i64 on vector lanes twice the width specified by size.
8549 static void gen_neon_addl(int size, bool is_sub, TCGv_i64 tcg_res,
8550 TCGv_i64 tcg_op1, TCGv_i64 tcg_op2)
8552 static NeonGenTwo64OpFn * const fns[3][2] = {
8553 { gen_helper_neon_addl_u16, gen_helper_neon_subl_u16 },
8554 { gen_helper_neon_addl_u32, gen_helper_neon_subl_u32 },
8555 { tcg_gen_add_i64, tcg_gen_sub_i64 },
8557 NeonGenTwo64OpFn *genfn;
8558 assert(size < 3);
8560 genfn = fns[size][is_sub];
8561 genfn(tcg_res, tcg_op1, tcg_op2);
8564 static void handle_3rd_widening(DisasContext *s, int is_q, int is_u, int size,
8565 int opcode, int rd, int rn, int rm)
8567 /* 3-reg-different widening insns: 64 x 64 -> 128 */
8568 TCGv_i64 tcg_res[2];
8569 int pass, accop;
8571 tcg_res[0] = tcg_temp_new_i64();
8572 tcg_res[1] = tcg_temp_new_i64();
8574 /* Does this op do an adding accumulate, a subtracting accumulate,
8575 * or no accumulate at all?
8577 switch (opcode) {
8578 case 5:
8579 case 8:
8580 case 9:
8581 accop = 1;
8582 break;
8583 case 10:
8584 case 11:
8585 accop = -1;
8586 break;
8587 default:
8588 accop = 0;
8589 break;
8592 if (accop != 0) {
8593 read_vec_element(s, tcg_res[0], rd, 0, MO_64);
8594 read_vec_element(s, tcg_res[1], rd, 1, MO_64);
8597 /* size == 2 means two 32x32->64 operations; this is worth special
8598 * casing because we can generally handle it inline.
8600 if (size == 2) {
8601 for (pass = 0; pass < 2; pass++) {
8602 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8603 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
8604 TCGv_i64 tcg_passres;
8605 TCGMemOp memop = MO_32 | (is_u ? 0 : MO_SIGN);
8607 int elt = pass + is_q * 2;
8609 read_vec_element(s, tcg_op1, rn, elt, memop);
8610 read_vec_element(s, tcg_op2, rm, elt, memop);
8612 if (accop == 0) {
8613 tcg_passres = tcg_res[pass];
8614 } else {
8615 tcg_passres = tcg_temp_new_i64();
8618 switch (opcode) {
8619 case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
8620 tcg_gen_add_i64(tcg_passres, tcg_op1, tcg_op2);
8621 break;
8622 case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
8623 tcg_gen_sub_i64(tcg_passres, tcg_op1, tcg_op2);
8624 break;
8625 case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
8626 case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
8628 TCGv_i64 tcg_tmp1 = tcg_temp_new_i64();
8629 TCGv_i64 tcg_tmp2 = tcg_temp_new_i64();
8631 tcg_gen_sub_i64(tcg_tmp1, tcg_op1, tcg_op2);
8632 tcg_gen_sub_i64(tcg_tmp2, tcg_op2, tcg_op1);
8633 tcg_gen_movcond_i64(is_u ? TCG_COND_GEU : TCG_COND_GE,
8634 tcg_passres,
8635 tcg_op1, tcg_op2, tcg_tmp1, tcg_tmp2);
8636 tcg_temp_free_i64(tcg_tmp1);
8637 tcg_temp_free_i64(tcg_tmp2);
8638 break;
8640 case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
8641 case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
8642 case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
8643 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
8644 break;
8645 case 9: /* SQDMLAL, SQDMLAL2 */
8646 case 11: /* SQDMLSL, SQDMLSL2 */
8647 case 13: /* SQDMULL, SQDMULL2 */
8648 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
8649 gen_helper_neon_addl_saturate_s64(tcg_passres, cpu_env,
8650 tcg_passres, tcg_passres);
8651 break;
8652 default:
8653 g_assert_not_reached();
8656 if (opcode == 9 || opcode == 11) {
8657 /* saturating accumulate ops */
8658 if (accop < 0) {
8659 tcg_gen_neg_i64(tcg_passres, tcg_passres);
8661 gen_helper_neon_addl_saturate_s64(tcg_res[pass], cpu_env,
8662 tcg_res[pass], tcg_passres);
8663 } else if (accop > 0) {
8664 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
8665 } else if (accop < 0) {
8666 tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
8669 if (accop != 0) {
8670 tcg_temp_free_i64(tcg_passres);
8673 tcg_temp_free_i64(tcg_op1);
8674 tcg_temp_free_i64(tcg_op2);
8676 } else {
8677 /* size 0 or 1, generally helper functions */
8678 for (pass = 0; pass < 2; pass++) {
8679 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
8680 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
8681 TCGv_i64 tcg_passres;
8682 int elt = pass + is_q * 2;
8684 read_vec_element_i32(s, tcg_op1, rn, elt, MO_32);
8685 read_vec_element_i32(s, tcg_op2, rm, elt, MO_32);
8687 if (accop == 0) {
8688 tcg_passres = tcg_res[pass];
8689 } else {
8690 tcg_passres = tcg_temp_new_i64();
8693 switch (opcode) {
8694 case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
8695 case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
8697 TCGv_i64 tcg_op2_64 = tcg_temp_new_i64();
8698 static NeonGenWidenFn * const widenfns[2][2] = {
8699 { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
8700 { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
8702 NeonGenWidenFn *widenfn = widenfns[size][is_u];
8704 widenfn(tcg_op2_64, tcg_op2);
8705 widenfn(tcg_passres, tcg_op1);
8706 gen_neon_addl(size, (opcode == 2), tcg_passres,
8707 tcg_passres, tcg_op2_64);
8708 tcg_temp_free_i64(tcg_op2_64);
8709 break;
8711 case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
8712 case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
8713 if (size == 0) {
8714 if (is_u) {
8715 gen_helper_neon_abdl_u16(tcg_passres, tcg_op1, tcg_op2);
8716 } else {
8717 gen_helper_neon_abdl_s16(tcg_passres, tcg_op1, tcg_op2);
8719 } else {
8720 if (is_u) {
8721 gen_helper_neon_abdl_u32(tcg_passres, tcg_op1, tcg_op2);
8722 } else {
8723 gen_helper_neon_abdl_s32(tcg_passres, tcg_op1, tcg_op2);
8726 break;
8727 case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
8728 case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
8729 case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
8730 if (size == 0) {
8731 if (is_u) {
8732 gen_helper_neon_mull_u8(tcg_passres, tcg_op1, tcg_op2);
8733 } else {
8734 gen_helper_neon_mull_s8(tcg_passres, tcg_op1, tcg_op2);
8736 } else {
8737 if (is_u) {
8738 gen_helper_neon_mull_u16(tcg_passres, tcg_op1, tcg_op2);
8739 } else {
8740 gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
8743 break;
8744 case 9: /* SQDMLAL, SQDMLAL2 */
8745 case 11: /* SQDMLSL, SQDMLSL2 */
8746 case 13: /* SQDMULL, SQDMULL2 */
8747 assert(size == 1);
8748 gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
8749 gen_helper_neon_addl_saturate_s32(tcg_passres, cpu_env,
8750 tcg_passres, tcg_passres);
8751 break;
8752 case 14: /* PMULL */
8753 assert(size == 0);
8754 gen_helper_neon_mull_p8(tcg_passres, tcg_op1, tcg_op2);
8755 break;
8756 default:
8757 g_assert_not_reached();
8759 tcg_temp_free_i32(tcg_op1);
8760 tcg_temp_free_i32(tcg_op2);
8762 if (accop != 0) {
8763 if (opcode == 9 || opcode == 11) {
8764 /* saturating accumulate ops */
8765 if (accop < 0) {
8766 gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
8768 gen_helper_neon_addl_saturate_s32(tcg_res[pass], cpu_env,
8769 tcg_res[pass],
8770 tcg_passres);
8771 } else {
8772 gen_neon_addl(size, (accop < 0), tcg_res[pass],
8773 tcg_res[pass], tcg_passres);
8775 tcg_temp_free_i64(tcg_passres);
8780 write_vec_element(s, tcg_res[0], rd, 0, MO_64);
8781 write_vec_element(s, tcg_res[1], rd, 1, MO_64);
8782 tcg_temp_free_i64(tcg_res[0]);
8783 tcg_temp_free_i64(tcg_res[1]);
8786 static void handle_3rd_wide(DisasContext *s, int is_q, int is_u, int size,
8787 int opcode, int rd, int rn, int rm)
8789 TCGv_i64 tcg_res[2];
8790 int part = is_q ? 2 : 0;
8791 int pass;
8793 for (pass = 0; pass < 2; pass++) {
8794 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8795 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
8796 TCGv_i64 tcg_op2_wide = tcg_temp_new_i64();
8797 static NeonGenWidenFn * const widenfns[3][2] = {
8798 { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
8799 { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
8800 { tcg_gen_ext_i32_i64, tcg_gen_extu_i32_i64 },
8802 NeonGenWidenFn *widenfn = widenfns[size][is_u];
8804 read_vec_element(s, tcg_op1, rn, pass, MO_64);
8805 read_vec_element_i32(s, tcg_op2, rm, part + pass, MO_32);
8806 widenfn(tcg_op2_wide, tcg_op2);
8807 tcg_temp_free_i32(tcg_op2);
8808 tcg_res[pass] = tcg_temp_new_i64();
8809 gen_neon_addl(size, (opcode == 3),
8810 tcg_res[pass], tcg_op1, tcg_op2_wide);
8811 tcg_temp_free_i64(tcg_op1);
8812 tcg_temp_free_i64(tcg_op2_wide);
8815 for (pass = 0; pass < 2; pass++) {
8816 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
8817 tcg_temp_free_i64(tcg_res[pass]);
8821 static void do_narrow_round_high_u32(TCGv_i32 res, TCGv_i64 in)
8823 tcg_gen_addi_i64(in, in, 1U << 31);
8824 tcg_gen_extrh_i64_i32(res, in);
8827 static void handle_3rd_narrowing(DisasContext *s, int is_q, int is_u, int size,
8828 int opcode, int rd, int rn, int rm)
8830 TCGv_i32 tcg_res[2];
8831 int part = is_q ? 2 : 0;
8832 int pass;
8834 for (pass = 0; pass < 2; pass++) {
8835 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8836 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
8837 TCGv_i64 tcg_wideres = tcg_temp_new_i64();
8838 static NeonGenNarrowFn * const narrowfns[3][2] = {
8839 { gen_helper_neon_narrow_high_u8,
8840 gen_helper_neon_narrow_round_high_u8 },
8841 { gen_helper_neon_narrow_high_u16,
8842 gen_helper_neon_narrow_round_high_u16 },
8843 { tcg_gen_extrh_i64_i32, do_narrow_round_high_u32 },
8845 NeonGenNarrowFn *gennarrow = narrowfns[size][is_u];
8847 read_vec_element(s, tcg_op1, rn, pass, MO_64);
8848 read_vec_element(s, tcg_op2, rm, pass, MO_64);
8850 gen_neon_addl(size, (opcode == 6), tcg_wideres, tcg_op1, tcg_op2);
8852 tcg_temp_free_i64(tcg_op1);
8853 tcg_temp_free_i64(tcg_op2);
8855 tcg_res[pass] = tcg_temp_new_i32();
8856 gennarrow(tcg_res[pass], tcg_wideres);
8857 tcg_temp_free_i64(tcg_wideres);
8860 for (pass = 0; pass < 2; pass++) {
8861 write_vec_element_i32(s, tcg_res[pass], rd, pass + part, MO_32);
8862 tcg_temp_free_i32(tcg_res[pass]);
8864 if (!is_q) {
8865 clear_vec_high(s, rd);
8869 static void handle_pmull_64(DisasContext *s, int is_q, int rd, int rn, int rm)
8871 /* PMULL of 64 x 64 -> 128 is an odd special case because it
8872 * is the only three-reg-diff instruction which produces a
8873 * 128-bit wide result from a single operation. However since
8874 * it's possible to calculate the two halves more or less
8875 * separately we just use two helper calls.
8877 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8878 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
8879 TCGv_i64 tcg_res = tcg_temp_new_i64();
8881 read_vec_element(s, tcg_op1, rn, is_q, MO_64);
8882 read_vec_element(s, tcg_op2, rm, is_q, MO_64);
8883 gen_helper_neon_pmull_64_lo(tcg_res, tcg_op1, tcg_op2);
8884 write_vec_element(s, tcg_res, rd, 0, MO_64);
8885 gen_helper_neon_pmull_64_hi(tcg_res, tcg_op1, tcg_op2);
8886 write_vec_element(s, tcg_res, rd, 1, MO_64);
8888 tcg_temp_free_i64(tcg_op1);
8889 tcg_temp_free_i64(tcg_op2);
8890 tcg_temp_free_i64(tcg_res);
8893 /* C3.6.15 AdvSIMD three different
8894 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 0
8895 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
8896 * | 0 | Q | U | 0 1 1 1 0 | size | 1 | Rm | opcode | 0 0 | Rn | Rd |
8897 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
8899 static void disas_simd_three_reg_diff(DisasContext *s, uint32_t insn)
8901 /* Instructions in this group fall into three basic classes
8902 * (in each case with the operation working on each element in
8903 * the input vectors):
8904 * (1) widening 64 x 64 -> 128 (with possibly Vd as an extra
8905 * 128 bit input)
8906 * (2) wide 64 x 128 -> 128
8907 * (3) narrowing 128 x 128 -> 64
8908 * Here we do initial decode, catch unallocated cases and
8909 * dispatch to separate functions for each class.
8911 int is_q = extract32(insn, 30, 1);
8912 int is_u = extract32(insn, 29, 1);
8913 int size = extract32(insn, 22, 2);
8914 int opcode = extract32(insn, 12, 4);
8915 int rm = extract32(insn, 16, 5);
8916 int rn = extract32(insn, 5, 5);
8917 int rd = extract32(insn, 0, 5);
8919 switch (opcode) {
8920 case 1: /* SADDW, SADDW2, UADDW, UADDW2 */
8921 case 3: /* SSUBW, SSUBW2, USUBW, USUBW2 */
8922 /* 64 x 128 -> 128 */
8923 if (size == 3) {
8924 unallocated_encoding(s);
8925 return;
8927 if (!fp_access_check(s)) {
8928 return;
8930 handle_3rd_wide(s, is_q, is_u, size, opcode, rd, rn, rm);
8931 break;
8932 case 4: /* ADDHN, ADDHN2, RADDHN, RADDHN2 */
8933 case 6: /* SUBHN, SUBHN2, RSUBHN, RSUBHN2 */
8934 /* 128 x 128 -> 64 */
8935 if (size == 3) {
8936 unallocated_encoding(s);
8937 return;
8939 if (!fp_access_check(s)) {
8940 return;
8942 handle_3rd_narrowing(s, is_q, is_u, size, opcode, rd, rn, rm);
8943 break;
8944 case 14: /* PMULL, PMULL2 */
8945 if (is_u || size == 1 || size == 2) {
8946 unallocated_encoding(s);
8947 return;
8949 if (size == 3) {
8950 if (!arm_dc_feature(s, ARM_FEATURE_V8_PMULL)) {
8951 unallocated_encoding(s);
8952 return;
8954 if (!fp_access_check(s)) {
8955 return;
8957 handle_pmull_64(s, is_q, rd, rn, rm);
8958 return;
8960 goto is_widening;
8961 case 9: /* SQDMLAL, SQDMLAL2 */
8962 case 11: /* SQDMLSL, SQDMLSL2 */
8963 case 13: /* SQDMULL, SQDMULL2 */
8964 if (is_u || size == 0) {
8965 unallocated_encoding(s);
8966 return;
8968 /* fall through */
8969 case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
8970 case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
8971 case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
8972 case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
8973 case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
8974 case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
8975 case 12: /* SMULL, SMULL2, UMULL, UMULL2 */
8976 /* 64 x 64 -> 128 */
8977 if (size == 3) {
8978 unallocated_encoding(s);
8979 return;
8981 is_widening:
8982 if (!fp_access_check(s)) {
8983 return;
8986 handle_3rd_widening(s, is_q, is_u, size, opcode, rd, rn, rm);
8987 break;
8988 default:
8989 /* opcode 15 not allocated */
8990 unallocated_encoding(s);
8991 break;
8995 /* Logic op (opcode == 3) subgroup of C3.6.16. */
8996 static void disas_simd_3same_logic(DisasContext *s, uint32_t insn)
8998 int rd = extract32(insn, 0, 5);
8999 int rn = extract32(insn, 5, 5);
9000 int rm = extract32(insn, 16, 5);
9001 int size = extract32(insn, 22, 2);
9002 bool is_u = extract32(insn, 29, 1);
9003 bool is_q = extract32(insn, 30, 1);
9004 TCGv_i64 tcg_op1, tcg_op2, tcg_res[2];
9005 int pass;
9007 if (!fp_access_check(s)) {
9008 return;
9011 tcg_op1 = tcg_temp_new_i64();
9012 tcg_op2 = tcg_temp_new_i64();
9013 tcg_res[0] = tcg_temp_new_i64();
9014 tcg_res[1] = tcg_temp_new_i64();
9016 for (pass = 0; pass < (is_q ? 2 : 1); pass++) {
9017 read_vec_element(s, tcg_op1, rn, pass, MO_64);
9018 read_vec_element(s, tcg_op2, rm, pass, MO_64);
9020 if (!is_u) {
9021 switch (size) {
9022 case 0: /* AND */
9023 tcg_gen_and_i64(tcg_res[pass], tcg_op1, tcg_op2);
9024 break;
9025 case 1: /* BIC */
9026 tcg_gen_andc_i64(tcg_res[pass], tcg_op1, tcg_op2);
9027 break;
9028 case 2: /* ORR */
9029 tcg_gen_or_i64(tcg_res[pass], tcg_op1, tcg_op2);
9030 break;
9031 case 3: /* ORN */
9032 tcg_gen_orc_i64(tcg_res[pass], tcg_op1, tcg_op2);
9033 break;
9035 } else {
9036 if (size != 0) {
9037 /* B* ops need res loaded to operate on */
9038 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
9041 switch (size) {
9042 case 0: /* EOR */
9043 tcg_gen_xor_i64(tcg_res[pass], tcg_op1, tcg_op2);
9044 break;
9045 case 1: /* BSL bitwise select */
9046 tcg_gen_xor_i64(tcg_op1, tcg_op1, tcg_op2);
9047 tcg_gen_and_i64(tcg_op1, tcg_op1, tcg_res[pass]);
9048 tcg_gen_xor_i64(tcg_res[pass], tcg_op2, tcg_op1);
9049 break;
9050 case 2: /* BIT, bitwise insert if true */
9051 tcg_gen_xor_i64(tcg_op1, tcg_op1, tcg_res[pass]);
9052 tcg_gen_and_i64(tcg_op1, tcg_op1, tcg_op2);
9053 tcg_gen_xor_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
9054 break;
9055 case 3: /* BIF, bitwise insert if false */
9056 tcg_gen_xor_i64(tcg_op1, tcg_op1, tcg_res[pass]);
9057 tcg_gen_andc_i64(tcg_op1, tcg_op1, tcg_op2);
9058 tcg_gen_xor_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
9059 break;
9064 write_vec_element(s, tcg_res[0], rd, 0, MO_64);
9065 if (!is_q) {
9066 tcg_gen_movi_i64(tcg_res[1], 0);
9068 write_vec_element(s, tcg_res[1], rd, 1, MO_64);
9070 tcg_temp_free_i64(tcg_op1);
9071 tcg_temp_free_i64(tcg_op2);
9072 tcg_temp_free_i64(tcg_res[0]);
9073 tcg_temp_free_i64(tcg_res[1]);
9076 /* Helper functions for 32 bit comparisons */
9077 static void gen_max_s32(TCGv_i32 res, TCGv_i32 op1, TCGv_i32 op2)
9079 tcg_gen_movcond_i32(TCG_COND_GE, res, op1, op2, op1, op2);
9082 static void gen_max_u32(TCGv_i32 res, TCGv_i32 op1, TCGv_i32 op2)
9084 tcg_gen_movcond_i32(TCG_COND_GEU, res, op1, op2, op1, op2);
9087 static void gen_min_s32(TCGv_i32 res, TCGv_i32 op1, TCGv_i32 op2)
9089 tcg_gen_movcond_i32(TCG_COND_LE, res, op1, op2, op1, op2);
9092 static void gen_min_u32(TCGv_i32 res, TCGv_i32 op1, TCGv_i32 op2)
9094 tcg_gen_movcond_i32(TCG_COND_LEU, res, op1, op2, op1, op2);
9097 /* Pairwise op subgroup of C3.6.16.
9099 * This is called directly or via the handle_3same_float for float pairwise
9100 * operations where the opcode and size are calculated differently.
9102 static void handle_simd_3same_pair(DisasContext *s, int is_q, int u, int opcode,
9103 int size, int rn, int rm, int rd)
9105 TCGv_ptr fpst;
9106 int pass;
9108 /* Floating point operations need fpst */
9109 if (opcode >= 0x58) {
9110 fpst = get_fpstatus_ptr();
9111 } else {
9112 TCGV_UNUSED_PTR(fpst);
9115 if (!fp_access_check(s)) {
9116 return;
9119 /* These operations work on the concatenated rm:rn, with each pair of
9120 * adjacent elements being operated on to produce an element in the result.
9122 if (size == 3) {
9123 TCGv_i64 tcg_res[2];
9125 for (pass = 0; pass < 2; pass++) {
9126 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
9127 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
9128 int passreg = (pass == 0) ? rn : rm;
9130 read_vec_element(s, tcg_op1, passreg, 0, MO_64);
9131 read_vec_element(s, tcg_op2, passreg, 1, MO_64);
9132 tcg_res[pass] = tcg_temp_new_i64();
9134 switch (opcode) {
9135 case 0x17: /* ADDP */
9136 tcg_gen_add_i64(tcg_res[pass], tcg_op1, tcg_op2);
9137 break;
9138 case 0x58: /* FMAXNMP */
9139 gen_helper_vfp_maxnumd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9140 break;
9141 case 0x5a: /* FADDP */
9142 gen_helper_vfp_addd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9143 break;
9144 case 0x5e: /* FMAXP */
9145 gen_helper_vfp_maxd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9146 break;
9147 case 0x78: /* FMINNMP */
9148 gen_helper_vfp_minnumd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9149 break;
9150 case 0x7e: /* FMINP */
9151 gen_helper_vfp_mind(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9152 break;
9153 default:
9154 g_assert_not_reached();
9157 tcg_temp_free_i64(tcg_op1);
9158 tcg_temp_free_i64(tcg_op2);
9161 for (pass = 0; pass < 2; pass++) {
9162 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
9163 tcg_temp_free_i64(tcg_res[pass]);
9165 } else {
9166 int maxpass = is_q ? 4 : 2;
9167 TCGv_i32 tcg_res[4];
9169 for (pass = 0; pass < maxpass; pass++) {
9170 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
9171 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
9172 NeonGenTwoOpFn *genfn = NULL;
9173 int passreg = pass < (maxpass / 2) ? rn : rm;
9174 int passelt = (is_q && (pass & 1)) ? 2 : 0;
9176 read_vec_element_i32(s, tcg_op1, passreg, passelt, MO_32);
9177 read_vec_element_i32(s, tcg_op2, passreg, passelt + 1, MO_32);
9178 tcg_res[pass] = tcg_temp_new_i32();
9180 switch (opcode) {
9181 case 0x17: /* ADDP */
9183 static NeonGenTwoOpFn * const fns[3] = {
9184 gen_helper_neon_padd_u8,
9185 gen_helper_neon_padd_u16,
9186 tcg_gen_add_i32,
9188 genfn = fns[size];
9189 break;
9191 case 0x14: /* SMAXP, UMAXP */
9193 static NeonGenTwoOpFn * const fns[3][2] = {
9194 { gen_helper_neon_pmax_s8, gen_helper_neon_pmax_u8 },
9195 { gen_helper_neon_pmax_s16, gen_helper_neon_pmax_u16 },
9196 { gen_max_s32, gen_max_u32 },
9198 genfn = fns[size][u];
9199 break;
9201 case 0x15: /* SMINP, UMINP */
9203 static NeonGenTwoOpFn * const fns[3][2] = {
9204 { gen_helper_neon_pmin_s8, gen_helper_neon_pmin_u8 },
9205 { gen_helper_neon_pmin_s16, gen_helper_neon_pmin_u16 },
9206 { gen_min_s32, gen_min_u32 },
9208 genfn = fns[size][u];
9209 break;
9211 /* The FP operations are all on single floats (32 bit) */
9212 case 0x58: /* FMAXNMP */
9213 gen_helper_vfp_maxnums(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9214 break;
9215 case 0x5a: /* FADDP */
9216 gen_helper_vfp_adds(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9217 break;
9218 case 0x5e: /* FMAXP */
9219 gen_helper_vfp_maxs(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9220 break;
9221 case 0x78: /* FMINNMP */
9222 gen_helper_vfp_minnums(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9223 break;
9224 case 0x7e: /* FMINP */
9225 gen_helper_vfp_mins(tcg_res[pass], tcg_op1, tcg_op2, fpst);
9226 break;
9227 default:
9228 g_assert_not_reached();
9231 /* FP ops called directly, otherwise call now */
9232 if (genfn) {
9233 genfn(tcg_res[pass], tcg_op1, tcg_op2);
9236 tcg_temp_free_i32(tcg_op1);
9237 tcg_temp_free_i32(tcg_op2);
9240 for (pass = 0; pass < maxpass; pass++) {
9241 write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_32);
9242 tcg_temp_free_i32(tcg_res[pass]);
9244 if (!is_q) {
9245 clear_vec_high(s, rd);
9249 if (!TCGV_IS_UNUSED_PTR(fpst)) {
9250 tcg_temp_free_ptr(fpst);
9254 /* Floating point op subgroup of C3.6.16. */
9255 static void disas_simd_3same_float(DisasContext *s, uint32_t insn)
9257 /* For floating point ops, the U, size[1] and opcode bits
9258 * together indicate the operation. size[0] indicates single
9259 * or double.
9261 int fpopcode = extract32(insn, 11, 5)
9262 | (extract32(insn, 23, 1) << 5)
9263 | (extract32(insn, 29, 1) << 6);
9264 int is_q = extract32(insn, 30, 1);
9265 int size = extract32(insn, 22, 1);
9266 int rm = extract32(insn, 16, 5);
9267 int rn = extract32(insn, 5, 5);
9268 int rd = extract32(insn, 0, 5);
9270 int datasize = is_q ? 128 : 64;
9271 int esize = 32 << size;
9272 int elements = datasize / esize;
9274 if (size == 1 && !is_q) {
9275 unallocated_encoding(s);
9276 return;
9279 switch (fpopcode) {
9280 case 0x58: /* FMAXNMP */
9281 case 0x5a: /* FADDP */
9282 case 0x5e: /* FMAXP */
9283 case 0x78: /* FMINNMP */
9284 case 0x7e: /* FMINP */
9285 if (size && !is_q) {
9286 unallocated_encoding(s);
9287 return;
9289 handle_simd_3same_pair(s, is_q, 0, fpopcode, size ? MO_64 : MO_32,
9290 rn, rm, rd);
9291 return;
9292 case 0x1b: /* FMULX */
9293 case 0x1f: /* FRECPS */
9294 case 0x3f: /* FRSQRTS */
9295 case 0x5d: /* FACGE */
9296 case 0x7d: /* FACGT */
9297 case 0x19: /* FMLA */
9298 case 0x39: /* FMLS */
9299 case 0x18: /* FMAXNM */
9300 case 0x1a: /* FADD */
9301 case 0x1c: /* FCMEQ */
9302 case 0x1e: /* FMAX */
9303 case 0x38: /* FMINNM */
9304 case 0x3a: /* FSUB */
9305 case 0x3e: /* FMIN */
9306 case 0x5b: /* FMUL */
9307 case 0x5c: /* FCMGE */
9308 case 0x5f: /* FDIV */
9309 case 0x7a: /* FABD */
9310 case 0x7c: /* FCMGT */
9311 if (!fp_access_check(s)) {
9312 return;
9315 handle_3same_float(s, size, elements, fpopcode, rd, rn, rm);
9316 return;
9317 default:
9318 unallocated_encoding(s);
9319 return;
9323 /* Integer op subgroup of C3.6.16. */
9324 static void disas_simd_3same_int(DisasContext *s, uint32_t insn)
9326 int is_q = extract32(insn, 30, 1);
9327 int u = extract32(insn, 29, 1);
9328 int size = extract32(insn, 22, 2);
9329 int opcode = extract32(insn, 11, 5);
9330 int rm = extract32(insn, 16, 5);
9331 int rn = extract32(insn, 5, 5);
9332 int rd = extract32(insn, 0, 5);
9333 int pass;
9335 switch (opcode) {
9336 case 0x13: /* MUL, PMUL */
9337 if (u && size != 0) {
9338 unallocated_encoding(s);
9339 return;
9341 /* fall through */
9342 case 0x0: /* SHADD, UHADD */
9343 case 0x2: /* SRHADD, URHADD */
9344 case 0x4: /* SHSUB, UHSUB */
9345 case 0xc: /* SMAX, UMAX */
9346 case 0xd: /* SMIN, UMIN */
9347 case 0xe: /* SABD, UABD */
9348 case 0xf: /* SABA, UABA */
9349 case 0x12: /* MLA, MLS */
9350 if (size == 3) {
9351 unallocated_encoding(s);
9352 return;
9354 break;
9355 case 0x16: /* SQDMULH, SQRDMULH */
9356 if (size == 0 || size == 3) {
9357 unallocated_encoding(s);
9358 return;
9360 break;
9361 default:
9362 if (size == 3 && !is_q) {
9363 unallocated_encoding(s);
9364 return;
9366 break;
9369 if (!fp_access_check(s)) {
9370 return;
9373 if (size == 3) {
9374 assert(is_q);
9375 for (pass = 0; pass < 2; pass++) {
9376 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
9377 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
9378 TCGv_i64 tcg_res = tcg_temp_new_i64();
9380 read_vec_element(s, tcg_op1, rn, pass, MO_64);
9381 read_vec_element(s, tcg_op2, rm, pass, MO_64);
9383 handle_3same_64(s, opcode, u, tcg_res, tcg_op1, tcg_op2);
9385 write_vec_element(s, tcg_res, rd, pass, MO_64);
9387 tcg_temp_free_i64(tcg_res);
9388 tcg_temp_free_i64(tcg_op1);
9389 tcg_temp_free_i64(tcg_op2);
9391 } else {
9392 for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
9393 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
9394 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
9395 TCGv_i32 tcg_res = tcg_temp_new_i32();
9396 NeonGenTwoOpFn *genfn = NULL;
9397 NeonGenTwoOpEnvFn *genenvfn = NULL;
9399 read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
9400 read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
9402 switch (opcode) {
9403 case 0x0: /* SHADD, UHADD */
9405 static NeonGenTwoOpFn * const fns[3][2] = {
9406 { gen_helper_neon_hadd_s8, gen_helper_neon_hadd_u8 },
9407 { gen_helper_neon_hadd_s16, gen_helper_neon_hadd_u16 },
9408 { gen_helper_neon_hadd_s32, gen_helper_neon_hadd_u32 },
9410 genfn = fns[size][u];
9411 break;
9413 case 0x1: /* SQADD, UQADD */
9415 static NeonGenTwoOpEnvFn * const fns[3][2] = {
9416 { gen_helper_neon_qadd_s8, gen_helper_neon_qadd_u8 },
9417 { gen_helper_neon_qadd_s16, gen_helper_neon_qadd_u16 },
9418 { gen_helper_neon_qadd_s32, gen_helper_neon_qadd_u32 },
9420 genenvfn = fns[size][u];
9421 break;
9423 case 0x2: /* SRHADD, URHADD */
9425 static NeonGenTwoOpFn * const fns[3][2] = {
9426 { gen_helper_neon_rhadd_s8, gen_helper_neon_rhadd_u8 },
9427 { gen_helper_neon_rhadd_s16, gen_helper_neon_rhadd_u16 },
9428 { gen_helper_neon_rhadd_s32, gen_helper_neon_rhadd_u32 },
9430 genfn = fns[size][u];
9431 break;
9433 case 0x4: /* SHSUB, UHSUB */
9435 static NeonGenTwoOpFn * const fns[3][2] = {
9436 { gen_helper_neon_hsub_s8, gen_helper_neon_hsub_u8 },
9437 { gen_helper_neon_hsub_s16, gen_helper_neon_hsub_u16 },
9438 { gen_helper_neon_hsub_s32, gen_helper_neon_hsub_u32 },
9440 genfn = fns[size][u];
9441 break;
9443 case 0x5: /* SQSUB, UQSUB */
9445 static NeonGenTwoOpEnvFn * const fns[3][2] = {
9446 { gen_helper_neon_qsub_s8, gen_helper_neon_qsub_u8 },
9447 { gen_helper_neon_qsub_s16, gen_helper_neon_qsub_u16 },
9448 { gen_helper_neon_qsub_s32, gen_helper_neon_qsub_u32 },
9450 genenvfn = fns[size][u];
9451 break;
9453 case 0x6: /* CMGT, CMHI */
9455 static NeonGenTwoOpFn * const fns[3][2] = {
9456 { gen_helper_neon_cgt_s8, gen_helper_neon_cgt_u8 },
9457 { gen_helper_neon_cgt_s16, gen_helper_neon_cgt_u16 },
9458 { gen_helper_neon_cgt_s32, gen_helper_neon_cgt_u32 },
9460 genfn = fns[size][u];
9461 break;
9463 case 0x7: /* CMGE, CMHS */
9465 static NeonGenTwoOpFn * const fns[3][2] = {
9466 { gen_helper_neon_cge_s8, gen_helper_neon_cge_u8 },
9467 { gen_helper_neon_cge_s16, gen_helper_neon_cge_u16 },
9468 { gen_helper_neon_cge_s32, gen_helper_neon_cge_u32 },
9470 genfn = fns[size][u];
9471 break;
9473 case 0x8: /* SSHL, USHL */
9475 static NeonGenTwoOpFn * const fns[3][2] = {
9476 { gen_helper_neon_shl_s8, gen_helper_neon_shl_u8 },
9477 { gen_helper_neon_shl_s16, gen_helper_neon_shl_u16 },
9478 { gen_helper_neon_shl_s32, gen_helper_neon_shl_u32 },
9480 genfn = fns[size][u];
9481 break;
9483 case 0x9: /* SQSHL, UQSHL */
9485 static NeonGenTwoOpEnvFn * const fns[3][2] = {
9486 { gen_helper_neon_qshl_s8, gen_helper_neon_qshl_u8 },
9487 { gen_helper_neon_qshl_s16, gen_helper_neon_qshl_u16 },
9488 { gen_helper_neon_qshl_s32, gen_helper_neon_qshl_u32 },
9490 genenvfn = fns[size][u];
9491 break;
9493 case 0xa: /* SRSHL, URSHL */
9495 static NeonGenTwoOpFn * const fns[3][2] = {
9496 { gen_helper_neon_rshl_s8, gen_helper_neon_rshl_u8 },
9497 { gen_helper_neon_rshl_s16, gen_helper_neon_rshl_u16 },
9498 { gen_helper_neon_rshl_s32, gen_helper_neon_rshl_u32 },
9500 genfn = fns[size][u];
9501 break;
9503 case 0xb: /* SQRSHL, UQRSHL */
9505 static NeonGenTwoOpEnvFn * const fns[3][2] = {
9506 { gen_helper_neon_qrshl_s8, gen_helper_neon_qrshl_u8 },
9507 { gen_helper_neon_qrshl_s16, gen_helper_neon_qrshl_u16 },
9508 { gen_helper_neon_qrshl_s32, gen_helper_neon_qrshl_u32 },
9510 genenvfn = fns[size][u];
9511 break;
9513 case 0xc: /* SMAX, UMAX */
9515 static NeonGenTwoOpFn * const fns[3][2] = {
9516 { gen_helper_neon_max_s8, gen_helper_neon_max_u8 },
9517 { gen_helper_neon_max_s16, gen_helper_neon_max_u16 },
9518 { gen_max_s32, gen_max_u32 },
9520 genfn = fns[size][u];
9521 break;
9524 case 0xd: /* SMIN, UMIN */
9526 static NeonGenTwoOpFn * const fns[3][2] = {
9527 { gen_helper_neon_min_s8, gen_helper_neon_min_u8 },
9528 { gen_helper_neon_min_s16, gen_helper_neon_min_u16 },
9529 { gen_min_s32, gen_min_u32 },
9531 genfn = fns[size][u];
9532 break;
9534 case 0xe: /* SABD, UABD */
9535 case 0xf: /* SABA, UABA */
9537 static NeonGenTwoOpFn * const fns[3][2] = {
9538 { gen_helper_neon_abd_s8, gen_helper_neon_abd_u8 },
9539 { gen_helper_neon_abd_s16, gen_helper_neon_abd_u16 },
9540 { gen_helper_neon_abd_s32, gen_helper_neon_abd_u32 },
9542 genfn = fns[size][u];
9543 break;
9545 case 0x10: /* ADD, SUB */
9547 static NeonGenTwoOpFn * const fns[3][2] = {
9548 { gen_helper_neon_add_u8, gen_helper_neon_sub_u8 },
9549 { gen_helper_neon_add_u16, gen_helper_neon_sub_u16 },
9550 { tcg_gen_add_i32, tcg_gen_sub_i32 },
9552 genfn = fns[size][u];
9553 break;
9555 case 0x11: /* CMTST, CMEQ */
9557 static NeonGenTwoOpFn * const fns[3][2] = {
9558 { gen_helper_neon_tst_u8, gen_helper_neon_ceq_u8 },
9559 { gen_helper_neon_tst_u16, gen_helper_neon_ceq_u16 },
9560 { gen_helper_neon_tst_u32, gen_helper_neon_ceq_u32 },
9562 genfn = fns[size][u];
9563 break;
9565 case 0x13: /* MUL, PMUL */
9566 if (u) {
9567 /* PMUL */
9568 assert(size == 0);
9569 genfn = gen_helper_neon_mul_p8;
9570 break;
9572 /* fall through : MUL */
9573 case 0x12: /* MLA, MLS */
9575 static NeonGenTwoOpFn * const fns[3] = {
9576 gen_helper_neon_mul_u8,
9577 gen_helper_neon_mul_u16,
9578 tcg_gen_mul_i32,
9580 genfn = fns[size];
9581 break;
9583 case 0x16: /* SQDMULH, SQRDMULH */
9585 static NeonGenTwoOpEnvFn * const fns[2][2] = {
9586 { gen_helper_neon_qdmulh_s16, gen_helper_neon_qrdmulh_s16 },
9587 { gen_helper_neon_qdmulh_s32, gen_helper_neon_qrdmulh_s32 },
9589 assert(size == 1 || size == 2);
9590 genenvfn = fns[size - 1][u];
9591 break;
9593 default:
9594 g_assert_not_reached();
9597 if (genenvfn) {
9598 genenvfn(tcg_res, cpu_env, tcg_op1, tcg_op2);
9599 } else {
9600 genfn(tcg_res, tcg_op1, tcg_op2);
9603 if (opcode == 0xf || opcode == 0x12) {
9604 /* SABA, UABA, MLA, MLS: accumulating ops */
9605 static NeonGenTwoOpFn * const fns[3][2] = {
9606 { gen_helper_neon_add_u8, gen_helper_neon_sub_u8 },
9607 { gen_helper_neon_add_u16, gen_helper_neon_sub_u16 },
9608 { tcg_gen_add_i32, tcg_gen_sub_i32 },
9610 bool is_sub = (opcode == 0x12 && u); /* MLS */
9612 genfn = fns[size][is_sub];
9613 read_vec_element_i32(s, tcg_op1, rd, pass, MO_32);
9614 genfn(tcg_res, tcg_op1, tcg_res);
9617 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
9619 tcg_temp_free_i32(tcg_res);
9620 tcg_temp_free_i32(tcg_op1);
9621 tcg_temp_free_i32(tcg_op2);
9625 if (!is_q) {
9626 clear_vec_high(s, rd);
9630 /* C3.6.16 AdvSIMD three same
9631 * 31 30 29 28 24 23 22 21 20 16 15 11 10 9 5 4 0
9632 * +---+---+---+-----------+------+---+------+--------+---+------+------+
9633 * | 0 | Q | U | 0 1 1 1 0 | size | 1 | Rm | opcode | 1 | Rn | Rd |
9634 * +---+---+---+-----------+------+---+------+--------+---+------+------+
9636 static void disas_simd_three_reg_same(DisasContext *s, uint32_t insn)
9638 int opcode = extract32(insn, 11, 5);
9640 switch (opcode) {
9641 case 0x3: /* logic ops */
9642 disas_simd_3same_logic(s, insn);
9643 break;
9644 case 0x17: /* ADDP */
9645 case 0x14: /* SMAXP, UMAXP */
9646 case 0x15: /* SMINP, UMINP */
9648 /* Pairwise operations */
9649 int is_q = extract32(insn, 30, 1);
9650 int u = extract32(insn, 29, 1);
9651 int size = extract32(insn, 22, 2);
9652 int rm = extract32(insn, 16, 5);
9653 int rn = extract32(insn, 5, 5);
9654 int rd = extract32(insn, 0, 5);
9655 if (opcode == 0x17) {
9656 if (u || (size == 3 && !is_q)) {
9657 unallocated_encoding(s);
9658 return;
9660 } else {
9661 if (size == 3) {
9662 unallocated_encoding(s);
9663 return;
9666 handle_simd_3same_pair(s, is_q, u, opcode, size, rn, rm, rd);
9667 break;
9669 case 0x18 ... 0x31:
9670 /* floating point ops, sz[1] and U are part of opcode */
9671 disas_simd_3same_float(s, insn);
9672 break;
9673 default:
9674 disas_simd_3same_int(s, insn);
9675 break;
9679 static void handle_2misc_widening(DisasContext *s, int opcode, bool is_q,
9680 int size, int rn, int rd)
9682 /* Handle 2-reg-misc ops which are widening (so each size element
9683 * in the source becomes a 2*size element in the destination.
9684 * The only instruction like this is FCVTL.
9686 int pass;
9688 if (size == 3) {
9689 /* 32 -> 64 bit fp conversion */
9690 TCGv_i64 tcg_res[2];
9691 int srcelt = is_q ? 2 : 0;
9693 for (pass = 0; pass < 2; pass++) {
9694 TCGv_i32 tcg_op = tcg_temp_new_i32();
9695 tcg_res[pass] = tcg_temp_new_i64();
9697 read_vec_element_i32(s, tcg_op, rn, srcelt + pass, MO_32);
9698 gen_helper_vfp_fcvtds(tcg_res[pass], tcg_op, cpu_env);
9699 tcg_temp_free_i32(tcg_op);
9701 for (pass = 0; pass < 2; pass++) {
9702 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
9703 tcg_temp_free_i64(tcg_res[pass]);
9705 } else {
9706 /* 16 -> 32 bit fp conversion */
9707 int srcelt = is_q ? 4 : 0;
9708 TCGv_i32 tcg_res[4];
9710 for (pass = 0; pass < 4; pass++) {
9711 tcg_res[pass] = tcg_temp_new_i32();
9713 read_vec_element_i32(s, tcg_res[pass], rn, srcelt + pass, MO_16);
9714 gen_helper_vfp_fcvt_f16_to_f32(tcg_res[pass], tcg_res[pass],
9715 cpu_env);
9717 for (pass = 0; pass < 4; pass++) {
9718 write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_32);
9719 tcg_temp_free_i32(tcg_res[pass]);
9724 static void handle_rev(DisasContext *s, int opcode, bool u,
9725 bool is_q, int size, int rn, int rd)
9727 int op = (opcode << 1) | u;
9728 int opsz = op + size;
9729 int grp_size = 3 - opsz;
9730 int dsize = is_q ? 128 : 64;
9731 int i;
9733 if (opsz >= 3) {
9734 unallocated_encoding(s);
9735 return;
9738 if (!fp_access_check(s)) {
9739 return;
9742 if (size == 0) {
9743 /* Special case bytes, use bswap op on each group of elements */
9744 int groups = dsize / (8 << grp_size);
9746 for (i = 0; i < groups; i++) {
9747 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
9749 read_vec_element(s, tcg_tmp, rn, i, grp_size);
9750 switch (grp_size) {
9751 case MO_16:
9752 tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp);
9753 break;
9754 case MO_32:
9755 tcg_gen_bswap32_i64(tcg_tmp, tcg_tmp);
9756 break;
9757 case MO_64:
9758 tcg_gen_bswap64_i64(tcg_tmp, tcg_tmp);
9759 break;
9760 default:
9761 g_assert_not_reached();
9763 write_vec_element(s, tcg_tmp, rd, i, grp_size);
9764 tcg_temp_free_i64(tcg_tmp);
9766 if (!is_q) {
9767 clear_vec_high(s, rd);
9769 } else {
9770 int revmask = (1 << grp_size) - 1;
9771 int esize = 8 << size;
9772 int elements = dsize / esize;
9773 TCGv_i64 tcg_rn = tcg_temp_new_i64();
9774 TCGv_i64 tcg_rd = tcg_const_i64(0);
9775 TCGv_i64 tcg_rd_hi = tcg_const_i64(0);
9777 for (i = 0; i < elements; i++) {
9778 int e_rev = (i & 0xf) ^ revmask;
9779 int off = e_rev * esize;
9780 read_vec_element(s, tcg_rn, rn, i, size);
9781 if (off >= 64) {
9782 tcg_gen_deposit_i64(tcg_rd_hi, tcg_rd_hi,
9783 tcg_rn, off - 64, esize);
9784 } else {
9785 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, off, esize);
9788 write_vec_element(s, tcg_rd, rd, 0, MO_64);
9789 write_vec_element(s, tcg_rd_hi, rd, 1, MO_64);
9791 tcg_temp_free_i64(tcg_rd_hi);
9792 tcg_temp_free_i64(tcg_rd);
9793 tcg_temp_free_i64(tcg_rn);
9797 static void handle_2misc_pairwise(DisasContext *s, int opcode, bool u,
9798 bool is_q, int size, int rn, int rd)
9800 /* Implement the pairwise operations from 2-misc:
9801 * SADDLP, UADDLP, SADALP, UADALP.
9802 * These all add pairs of elements in the input to produce a
9803 * double-width result element in the output (possibly accumulating).
9805 bool accum = (opcode == 0x6);
9806 int maxpass = is_q ? 2 : 1;
9807 int pass;
9808 TCGv_i64 tcg_res[2];
9810 if (size == 2) {
9811 /* 32 + 32 -> 64 op */
9812 TCGMemOp memop = size + (u ? 0 : MO_SIGN);
9814 for (pass = 0; pass < maxpass; pass++) {
9815 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
9816 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
9818 tcg_res[pass] = tcg_temp_new_i64();
9820 read_vec_element(s, tcg_op1, rn, pass * 2, memop);
9821 read_vec_element(s, tcg_op2, rn, pass * 2 + 1, memop);
9822 tcg_gen_add_i64(tcg_res[pass], tcg_op1, tcg_op2);
9823 if (accum) {
9824 read_vec_element(s, tcg_op1, rd, pass, MO_64);
9825 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
9828 tcg_temp_free_i64(tcg_op1);
9829 tcg_temp_free_i64(tcg_op2);
9831 } else {
9832 for (pass = 0; pass < maxpass; pass++) {
9833 TCGv_i64 tcg_op = tcg_temp_new_i64();
9834 NeonGenOneOpFn *genfn;
9835 static NeonGenOneOpFn * const fns[2][2] = {
9836 { gen_helper_neon_addlp_s8, gen_helper_neon_addlp_u8 },
9837 { gen_helper_neon_addlp_s16, gen_helper_neon_addlp_u16 },
9840 genfn = fns[size][u];
9842 tcg_res[pass] = tcg_temp_new_i64();
9844 read_vec_element(s, tcg_op, rn, pass, MO_64);
9845 genfn(tcg_res[pass], tcg_op);
9847 if (accum) {
9848 read_vec_element(s, tcg_op, rd, pass, MO_64);
9849 if (size == 0) {
9850 gen_helper_neon_addl_u16(tcg_res[pass],
9851 tcg_res[pass], tcg_op);
9852 } else {
9853 gen_helper_neon_addl_u32(tcg_res[pass],
9854 tcg_res[pass], tcg_op);
9857 tcg_temp_free_i64(tcg_op);
9860 if (!is_q) {
9861 tcg_res[1] = tcg_const_i64(0);
9863 for (pass = 0; pass < 2; pass++) {
9864 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
9865 tcg_temp_free_i64(tcg_res[pass]);
9869 static void handle_shll(DisasContext *s, bool is_q, int size, int rn, int rd)
9871 /* Implement SHLL and SHLL2 */
9872 int pass;
9873 int part = is_q ? 2 : 0;
9874 TCGv_i64 tcg_res[2];
9876 for (pass = 0; pass < 2; pass++) {
9877 static NeonGenWidenFn * const widenfns[3] = {
9878 gen_helper_neon_widen_u8,
9879 gen_helper_neon_widen_u16,
9880 tcg_gen_extu_i32_i64,
9882 NeonGenWidenFn *widenfn = widenfns[size];
9883 TCGv_i32 tcg_op = tcg_temp_new_i32();
9885 read_vec_element_i32(s, tcg_op, rn, part + pass, MO_32);
9886 tcg_res[pass] = tcg_temp_new_i64();
9887 widenfn(tcg_res[pass], tcg_op);
9888 tcg_gen_shli_i64(tcg_res[pass], tcg_res[pass], 8 << size);
9890 tcg_temp_free_i32(tcg_op);
9893 for (pass = 0; pass < 2; pass++) {
9894 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
9895 tcg_temp_free_i64(tcg_res[pass]);
9899 /* C3.6.17 AdvSIMD two reg misc
9900 * 31 30 29 28 24 23 22 21 17 16 12 11 10 9 5 4 0
9901 * +---+---+---+-----------+------+-----------+--------+-----+------+------+
9902 * | 0 | Q | U | 0 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 | Rn | Rd |
9903 * +---+---+---+-----------+------+-----------+--------+-----+------+------+
9905 static void disas_simd_two_reg_misc(DisasContext *s, uint32_t insn)
9907 int size = extract32(insn, 22, 2);
9908 int opcode = extract32(insn, 12, 5);
9909 bool u = extract32(insn, 29, 1);
9910 bool is_q = extract32(insn, 30, 1);
9911 int rn = extract32(insn, 5, 5);
9912 int rd = extract32(insn, 0, 5);
9913 bool need_fpstatus = false;
9914 bool need_rmode = false;
9915 int rmode = -1;
9916 TCGv_i32 tcg_rmode;
9917 TCGv_ptr tcg_fpstatus;
9919 switch (opcode) {
9920 case 0x0: /* REV64, REV32 */
9921 case 0x1: /* REV16 */
9922 handle_rev(s, opcode, u, is_q, size, rn, rd);
9923 return;
9924 case 0x5: /* CNT, NOT, RBIT */
9925 if (u && size == 0) {
9926 /* NOT: adjust size so we can use the 64-bits-at-a-time loop. */
9927 size = 3;
9928 break;
9929 } else if (u && size == 1) {
9930 /* RBIT */
9931 break;
9932 } else if (!u && size == 0) {
9933 /* CNT */
9934 break;
9936 unallocated_encoding(s);
9937 return;
9938 case 0x12: /* XTN, XTN2, SQXTUN, SQXTUN2 */
9939 case 0x14: /* SQXTN, SQXTN2, UQXTN, UQXTN2 */
9940 if (size == 3) {
9941 unallocated_encoding(s);
9942 return;
9944 if (!fp_access_check(s)) {
9945 return;
9948 handle_2misc_narrow(s, false, opcode, u, is_q, size, rn, rd);
9949 return;
9950 case 0x4: /* CLS, CLZ */
9951 if (size == 3) {
9952 unallocated_encoding(s);
9953 return;
9955 break;
9956 case 0x2: /* SADDLP, UADDLP */
9957 case 0x6: /* SADALP, UADALP */
9958 if (size == 3) {
9959 unallocated_encoding(s);
9960 return;
9962 if (!fp_access_check(s)) {
9963 return;
9965 handle_2misc_pairwise(s, opcode, u, is_q, size, rn, rd);
9966 return;
9967 case 0x13: /* SHLL, SHLL2 */
9968 if (u == 0 || size == 3) {
9969 unallocated_encoding(s);
9970 return;
9972 if (!fp_access_check(s)) {
9973 return;
9975 handle_shll(s, is_q, size, rn, rd);
9976 return;
9977 case 0xa: /* CMLT */
9978 if (u == 1) {
9979 unallocated_encoding(s);
9980 return;
9982 /* fall through */
9983 case 0x8: /* CMGT, CMGE */
9984 case 0x9: /* CMEQ, CMLE */
9985 case 0xb: /* ABS, NEG */
9986 if (size == 3 && !is_q) {
9987 unallocated_encoding(s);
9988 return;
9990 break;
9991 case 0x3: /* SUQADD, USQADD */
9992 if (size == 3 && !is_q) {
9993 unallocated_encoding(s);
9994 return;
9996 if (!fp_access_check(s)) {
9997 return;
9999 handle_2misc_satacc(s, false, u, is_q, size, rn, rd);
10000 return;
10001 case 0x7: /* SQABS, SQNEG */
10002 if (size == 3 && !is_q) {
10003 unallocated_encoding(s);
10004 return;
10006 break;
10007 case 0xc ... 0xf:
10008 case 0x16 ... 0x1d:
10009 case 0x1f:
10011 /* Floating point: U, size[1] and opcode indicate operation;
10012 * size[0] indicates single or double precision.
10014 int is_double = extract32(size, 0, 1);
10015 opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
10016 size = is_double ? 3 : 2;
10017 switch (opcode) {
10018 case 0x2f: /* FABS */
10019 case 0x6f: /* FNEG */
10020 if (size == 3 && !is_q) {
10021 unallocated_encoding(s);
10022 return;
10024 break;
10025 case 0x1d: /* SCVTF */
10026 case 0x5d: /* UCVTF */
10028 bool is_signed = (opcode == 0x1d) ? true : false;
10029 int elements = is_double ? 2 : is_q ? 4 : 2;
10030 if (is_double && !is_q) {
10031 unallocated_encoding(s);
10032 return;
10034 if (!fp_access_check(s)) {
10035 return;
10037 handle_simd_intfp_conv(s, rd, rn, elements, is_signed, 0, size);
10038 return;
10040 case 0x2c: /* FCMGT (zero) */
10041 case 0x2d: /* FCMEQ (zero) */
10042 case 0x2e: /* FCMLT (zero) */
10043 case 0x6c: /* FCMGE (zero) */
10044 case 0x6d: /* FCMLE (zero) */
10045 if (size == 3 && !is_q) {
10046 unallocated_encoding(s);
10047 return;
10049 handle_2misc_fcmp_zero(s, opcode, false, u, is_q, size, rn, rd);
10050 return;
10051 case 0x7f: /* FSQRT */
10052 if (size == 3 && !is_q) {
10053 unallocated_encoding(s);
10054 return;
10056 break;
10057 case 0x1a: /* FCVTNS */
10058 case 0x1b: /* FCVTMS */
10059 case 0x3a: /* FCVTPS */
10060 case 0x3b: /* FCVTZS */
10061 case 0x5a: /* FCVTNU */
10062 case 0x5b: /* FCVTMU */
10063 case 0x7a: /* FCVTPU */
10064 case 0x7b: /* FCVTZU */
10065 need_fpstatus = true;
10066 need_rmode = true;
10067 rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
10068 if (size == 3 && !is_q) {
10069 unallocated_encoding(s);
10070 return;
10072 break;
10073 case 0x5c: /* FCVTAU */
10074 case 0x1c: /* FCVTAS */
10075 need_fpstatus = true;
10076 need_rmode = true;
10077 rmode = FPROUNDING_TIEAWAY;
10078 if (size == 3 && !is_q) {
10079 unallocated_encoding(s);
10080 return;
10082 break;
10083 case 0x3c: /* URECPE */
10084 if (size == 3) {
10085 unallocated_encoding(s);
10086 return;
10088 /* fall through */
10089 case 0x3d: /* FRECPE */
10090 case 0x7d: /* FRSQRTE */
10091 if (size == 3 && !is_q) {
10092 unallocated_encoding(s);
10093 return;
10095 if (!fp_access_check(s)) {
10096 return;
10098 handle_2misc_reciprocal(s, opcode, false, u, is_q, size, rn, rd);
10099 return;
10100 case 0x56: /* FCVTXN, FCVTXN2 */
10101 if (size == 2) {
10102 unallocated_encoding(s);
10103 return;
10105 /* fall through */
10106 case 0x16: /* FCVTN, FCVTN2 */
10107 /* handle_2misc_narrow does a 2*size -> size operation, but these
10108 * instructions encode the source size rather than dest size.
10110 if (!fp_access_check(s)) {
10111 return;
10113 handle_2misc_narrow(s, false, opcode, 0, is_q, size - 1, rn, rd);
10114 return;
10115 case 0x17: /* FCVTL, FCVTL2 */
10116 if (!fp_access_check(s)) {
10117 return;
10119 handle_2misc_widening(s, opcode, is_q, size, rn, rd);
10120 return;
10121 case 0x18: /* FRINTN */
10122 case 0x19: /* FRINTM */
10123 case 0x38: /* FRINTP */
10124 case 0x39: /* FRINTZ */
10125 need_rmode = true;
10126 rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
10127 /* fall through */
10128 case 0x59: /* FRINTX */
10129 case 0x79: /* FRINTI */
10130 need_fpstatus = true;
10131 if (size == 3 && !is_q) {
10132 unallocated_encoding(s);
10133 return;
10135 break;
10136 case 0x58: /* FRINTA */
10137 need_rmode = true;
10138 rmode = FPROUNDING_TIEAWAY;
10139 need_fpstatus = true;
10140 if (size == 3 && !is_q) {
10141 unallocated_encoding(s);
10142 return;
10144 break;
10145 case 0x7c: /* URSQRTE */
10146 if (size == 3) {
10147 unallocated_encoding(s);
10148 return;
10150 need_fpstatus = true;
10151 break;
10152 default:
10153 unallocated_encoding(s);
10154 return;
10156 break;
10158 default:
10159 unallocated_encoding(s);
10160 return;
10163 if (!fp_access_check(s)) {
10164 return;
10167 if (need_fpstatus) {
10168 tcg_fpstatus = get_fpstatus_ptr();
10169 } else {
10170 TCGV_UNUSED_PTR(tcg_fpstatus);
10172 if (need_rmode) {
10173 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rmode));
10174 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
10175 } else {
10176 TCGV_UNUSED_I32(tcg_rmode);
10179 if (size == 3) {
10180 /* All 64-bit element operations can be shared with scalar 2misc */
10181 int pass;
10183 for (pass = 0; pass < (is_q ? 2 : 1); pass++) {
10184 TCGv_i64 tcg_op = tcg_temp_new_i64();
10185 TCGv_i64 tcg_res = tcg_temp_new_i64();
10187 read_vec_element(s, tcg_op, rn, pass, MO_64);
10189 handle_2misc_64(s, opcode, u, tcg_res, tcg_op,
10190 tcg_rmode, tcg_fpstatus);
10192 write_vec_element(s, tcg_res, rd, pass, MO_64);
10194 tcg_temp_free_i64(tcg_res);
10195 tcg_temp_free_i64(tcg_op);
10197 } else {
10198 int pass;
10200 for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
10201 TCGv_i32 tcg_op = tcg_temp_new_i32();
10202 TCGv_i32 tcg_res = tcg_temp_new_i32();
10203 TCGCond cond;
10205 read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
10207 if (size == 2) {
10208 /* Special cases for 32 bit elements */
10209 switch (opcode) {
10210 case 0xa: /* CMLT */
10211 /* 32 bit integer comparison against zero, result is
10212 * test ? (2^32 - 1) : 0. We implement via setcond(test)
10213 * and inverting.
10215 cond = TCG_COND_LT;
10216 do_cmop:
10217 tcg_gen_setcondi_i32(cond, tcg_res, tcg_op, 0);
10218 tcg_gen_neg_i32(tcg_res, tcg_res);
10219 break;
10220 case 0x8: /* CMGT, CMGE */
10221 cond = u ? TCG_COND_GE : TCG_COND_GT;
10222 goto do_cmop;
10223 case 0x9: /* CMEQ, CMLE */
10224 cond = u ? TCG_COND_LE : TCG_COND_EQ;
10225 goto do_cmop;
10226 case 0x4: /* CLS */
10227 if (u) {
10228 gen_helper_clz32(tcg_res, tcg_op);
10229 } else {
10230 gen_helper_cls32(tcg_res, tcg_op);
10232 break;
10233 case 0x7: /* SQABS, SQNEG */
10234 if (u) {
10235 gen_helper_neon_qneg_s32(tcg_res, cpu_env, tcg_op);
10236 } else {
10237 gen_helper_neon_qabs_s32(tcg_res, cpu_env, tcg_op);
10239 break;
10240 case 0xb: /* ABS, NEG */
10241 if (u) {
10242 tcg_gen_neg_i32(tcg_res, tcg_op);
10243 } else {
10244 TCGv_i32 tcg_zero = tcg_const_i32(0);
10245 tcg_gen_neg_i32(tcg_res, tcg_op);
10246 tcg_gen_movcond_i32(TCG_COND_GT, tcg_res, tcg_op,
10247 tcg_zero, tcg_op, tcg_res);
10248 tcg_temp_free_i32(tcg_zero);
10250 break;
10251 case 0x2f: /* FABS */
10252 gen_helper_vfp_abss(tcg_res, tcg_op);
10253 break;
10254 case 0x6f: /* FNEG */
10255 gen_helper_vfp_negs(tcg_res, tcg_op);
10256 break;
10257 case 0x7f: /* FSQRT */
10258 gen_helper_vfp_sqrts(tcg_res, tcg_op, cpu_env);
10259 break;
10260 case 0x1a: /* FCVTNS */
10261 case 0x1b: /* FCVTMS */
10262 case 0x1c: /* FCVTAS */
10263 case 0x3a: /* FCVTPS */
10264 case 0x3b: /* FCVTZS */
10266 TCGv_i32 tcg_shift = tcg_const_i32(0);
10267 gen_helper_vfp_tosls(tcg_res, tcg_op,
10268 tcg_shift, tcg_fpstatus);
10269 tcg_temp_free_i32(tcg_shift);
10270 break;
10272 case 0x5a: /* FCVTNU */
10273 case 0x5b: /* FCVTMU */
10274 case 0x5c: /* FCVTAU */
10275 case 0x7a: /* FCVTPU */
10276 case 0x7b: /* FCVTZU */
10278 TCGv_i32 tcg_shift = tcg_const_i32(0);
10279 gen_helper_vfp_touls(tcg_res, tcg_op,
10280 tcg_shift, tcg_fpstatus);
10281 tcg_temp_free_i32(tcg_shift);
10282 break;
10284 case 0x18: /* FRINTN */
10285 case 0x19: /* FRINTM */
10286 case 0x38: /* FRINTP */
10287 case 0x39: /* FRINTZ */
10288 case 0x58: /* FRINTA */
10289 case 0x79: /* FRINTI */
10290 gen_helper_rints(tcg_res, tcg_op, tcg_fpstatus);
10291 break;
10292 case 0x59: /* FRINTX */
10293 gen_helper_rints_exact(tcg_res, tcg_op, tcg_fpstatus);
10294 break;
10295 case 0x7c: /* URSQRTE */
10296 gen_helper_rsqrte_u32(tcg_res, tcg_op, tcg_fpstatus);
10297 break;
10298 default:
10299 g_assert_not_reached();
10301 } else {
10302 /* Use helpers for 8 and 16 bit elements */
10303 switch (opcode) {
10304 case 0x5: /* CNT, RBIT */
10305 /* For these two insns size is part of the opcode specifier
10306 * (handled earlier); they always operate on byte elements.
10308 if (u) {
10309 gen_helper_neon_rbit_u8(tcg_res, tcg_op);
10310 } else {
10311 gen_helper_neon_cnt_u8(tcg_res, tcg_op);
10313 break;
10314 case 0x7: /* SQABS, SQNEG */
10316 NeonGenOneOpEnvFn *genfn;
10317 static NeonGenOneOpEnvFn * const fns[2][2] = {
10318 { gen_helper_neon_qabs_s8, gen_helper_neon_qneg_s8 },
10319 { gen_helper_neon_qabs_s16, gen_helper_neon_qneg_s16 },
10321 genfn = fns[size][u];
10322 genfn(tcg_res, cpu_env, tcg_op);
10323 break;
10325 case 0x8: /* CMGT, CMGE */
10326 case 0x9: /* CMEQ, CMLE */
10327 case 0xa: /* CMLT */
10329 static NeonGenTwoOpFn * const fns[3][2] = {
10330 { gen_helper_neon_cgt_s8, gen_helper_neon_cgt_s16 },
10331 { gen_helper_neon_cge_s8, gen_helper_neon_cge_s16 },
10332 { gen_helper_neon_ceq_u8, gen_helper_neon_ceq_u16 },
10334 NeonGenTwoOpFn *genfn;
10335 int comp;
10336 bool reverse;
10337 TCGv_i32 tcg_zero = tcg_const_i32(0);
10339 /* comp = index into [CMGT, CMGE, CMEQ, CMLE, CMLT] */
10340 comp = (opcode - 0x8) * 2 + u;
10341 /* ...but LE, LT are implemented as reverse GE, GT */
10342 reverse = (comp > 2);
10343 if (reverse) {
10344 comp = 4 - comp;
10346 genfn = fns[comp][size];
10347 if (reverse) {
10348 genfn(tcg_res, tcg_zero, tcg_op);
10349 } else {
10350 genfn(tcg_res, tcg_op, tcg_zero);
10352 tcg_temp_free_i32(tcg_zero);
10353 break;
10355 case 0xb: /* ABS, NEG */
10356 if (u) {
10357 TCGv_i32 tcg_zero = tcg_const_i32(0);
10358 if (size) {
10359 gen_helper_neon_sub_u16(tcg_res, tcg_zero, tcg_op);
10360 } else {
10361 gen_helper_neon_sub_u8(tcg_res, tcg_zero, tcg_op);
10363 tcg_temp_free_i32(tcg_zero);
10364 } else {
10365 if (size) {
10366 gen_helper_neon_abs_s16(tcg_res, tcg_op);
10367 } else {
10368 gen_helper_neon_abs_s8(tcg_res, tcg_op);
10371 break;
10372 case 0x4: /* CLS, CLZ */
10373 if (u) {
10374 if (size == 0) {
10375 gen_helper_neon_clz_u8(tcg_res, tcg_op);
10376 } else {
10377 gen_helper_neon_clz_u16(tcg_res, tcg_op);
10379 } else {
10380 if (size == 0) {
10381 gen_helper_neon_cls_s8(tcg_res, tcg_op);
10382 } else {
10383 gen_helper_neon_cls_s16(tcg_res, tcg_op);
10386 break;
10387 default:
10388 g_assert_not_reached();
10392 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
10394 tcg_temp_free_i32(tcg_res);
10395 tcg_temp_free_i32(tcg_op);
10398 if (!is_q) {
10399 clear_vec_high(s, rd);
10402 if (need_rmode) {
10403 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
10404 tcg_temp_free_i32(tcg_rmode);
10406 if (need_fpstatus) {
10407 tcg_temp_free_ptr(tcg_fpstatus);
10411 /* C3.6.13 AdvSIMD scalar x indexed element
10412 * 31 30 29 28 24 23 22 21 20 19 16 15 12 11 10 9 5 4 0
10413 * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
10414 * | 0 1 | U | 1 1 1 1 1 | size | L | M | Rm | opc | H | 0 | Rn | Rd |
10415 * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
10416 * C3.6.18 AdvSIMD vector x indexed element
10417 * 31 30 29 28 24 23 22 21 20 19 16 15 12 11 10 9 5 4 0
10418 * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
10419 * | 0 | Q | U | 0 1 1 1 1 | size | L | M | Rm | opc | H | 0 | Rn | Rd |
10420 * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
10422 static void disas_simd_indexed(DisasContext *s, uint32_t insn)
10424 /* This encoding has two kinds of instruction:
10425 * normal, where we perform elt x idxelt => elt for each
10426 * element in the vector
10427 * long, where we perform elt x idxelt and generate a result of
10428 * double the width of the input element
10429 * The long ops have a 'part' specifier (ie come in INSN, INSN2 pairs).
10431 bool is_scalar = extract32(insn, 28, 1);
10432 bool is_q = extract32(insn, 30, 1);
10433 bool u = extract32(insn, 29, 1);
10434 int size = extract32(insn, 22, 2);
10435 int l = extract32(insn, 21, 1);
10436 int m = extract32(insn, 20, 1);
10437 /* Note that the Rm field here is only 4 bits, not 5 as it usually is */
10438 int rm = extract32(insn, 16, 4);
10439 int opcode = extract32(insn, 12, 4);
10440 int h = extract32(insn, 11, 1);
10441 int rn = extract32(insn, 5, 5);
10442 int rd = extract32(insn, 0, 5);
10443 bool is_long = false;
10444 bool is_fp = false;
10445 int index;
10446 TCGv_ptr fpst;
10448 switch (opcode) {
10449 case 0x0: /* MLA */
10450 case 0x4: /* MLS */
10451 if (!u || is_scalar) {
10452 unallocated_encoding(s);
10453 return;
10455 break;
10456 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10457 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10458 case 0xa: /* SMULL, SMULL2, UMULL, UMULL2 */
10459 if (is_scalar) {
10460 unallocated_encoding(s);
10461 return;
10463 is_long = true;
10464 break;
10465 case 0x3: /* SQDMLAL, SQDMLAL2 */
10466 case 0x7: /* SQDMLSL, SQDMLSL2 */
10467 case 0xb: /* SQDMULL, SQDMULL2 */
10468 is_long = true;
10469 /* fall through */
10470 case 0xc: /* SQDMULH */
10471 case 0xd: /* SQRDMULH */
10472 if (u) {
10473 unallocated_encoding(s);
10474 return;
10476 break;
10477 case 0x8: /* MUL */
10478 if (u || is_scalar) {
10479 unallocated_encoding(s);
10480 return;
10482 break;
10483 case 0x1: /* FMLA */
10484 case 0x5: /* FMLS */
10485 if (u) {
10486 unallocated_encoding(s);
10487 return;
10489 /* fall through */
10490 case 0x9: /* FMUL, FMULX */
10491 if (!extract32(size, 1, 1)) {
10492 unallocated_encoding(s);
10493 return;
10495 is_fp = true;
10496 break;
10497 default:
10498 unallocated_encoding(s);
10499 return;
10502 if (is_fp) {
10503 /* low bit of size indicates single/double */
10504 size = extract32(size, 0, 1) ? 3 : 2;
10505 if (size == 2) {
10506 index = h << 1 | l;
10507 } else {
10508 if (l || !is_q) {
10509 unallocated_encoding(s);
10510 return;
10512 index = h;
10514 rm |= (m << 4);
10515 } else {
10516 switch (size) {
10517 case 1:
10518 index = h << 2 | l << 1 | m;
10519 break;
10520 case 2:
10521 index = h << 1 | l;
10522 rm |= (m << 4);
10523 break;
10524 default:
10525 unallocated_encoding(s);
10526 return;
10530 if (!fp_access_check(s)) {
10531 return;
10534 if (is_fp) {
10535 fpst = get_fpstatus_ptr();
10536 } else {
10537 TCGV_UNUSED_PTR(fpst);
10540 if (size == 3) {
10541 TCGv_i64 tcg_idx = tcg_temp_new_i64();
10542 int pass;
10544 assert(is_fp && is_q && !is_long);
10546 read_vec_element(s, tcg_idx, rm, index, MO_64);
10548 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
10549 TCGv_i64 tcg_op = tcg_temp_new_i64();
10550 TCGv_i64 tcg_res = tcg_temp_new_i64();
10552 read_vec_element(s, tcg_op, rn, pass, MO_64);
10554 switch (opcode) {
10555 case 0x5: /* FMLS */
10556 /* As usual for ARM, separate negation for fused multiply-add */
10557 gen_helper_vfp_negd(tcg_op, tcg_op);
10558 /* fall through */
10559 case 0x1: /* FMLA */
10560 read_vec_element(s, tcg_res, rd, pass, MO_64);
10561 gen_helper_vfp_muladdd(tcg_res, tcg_op, tcg_idx, tcg_res, fpst);
10562 break;
10563 case 0x9: /* FMUL, FMULX */
10564 if (u) {
10565 gen_helper_vfp_mulxd(tcg_res, tcg_op, tcg_idx, fpst);
10566 } else {
10567 gen_helper_vfp_muld(tcg_res, tcg_op, tcg_idx, fpst);
10569 break;
10570 default:
10571 g_assert_not_reached();
10574 write_vec_element(s, tcg_res, rd, pass, MO_64);
10575 tcg_temp_free_i64(tcg_op);
10576 tcg_temp_free_i64(tcg_res);
10579 if (is_scalar) {
10580 clear_vec_high(s, rd);
10583 tcg_temp_free_i64(tcg_idx);
10584 } else if (!is_long) {
10585 /* 32 bit floating point, or 16 or 32 bit integer.
10586 * For the 16 bit scalar case we use the usual Neon helpers and
10587 * rely on the fact that 0 op 0 == 0 with no side effects.
10589 TCGv_i32 tcg_idx = tcg_temp_new_i32();
10590 int pass, maxpasses;
10592 if (is_scalar) {
10593 maxpasses = 1;
10594 } else {
10595 maxpasses = is_q ? 4 : 2;
10598 read_vec_element_i32(s, tcg_idx, rm, index, size);
10600 if (size == 1 && !is_scalar) {
10601 /* The simplest way to handle the 16x16 indexed ops is to duplicate
10602 * the index into both halves of the 32 bit tcg_idx and then use
10603 * the usual Neon helpers.
10605 tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
10608 for (pass = 0; pass < maxpasses; pass++) {
10609 TCGv_i32 tcg_op = tcg_temp_new_i32();
10610 TCGv_i32 tcg_res = tcg_temp_new_i32();
10612 read_vec_element_i32(s, tcg_op, rn, pass, is_scalar ? size : MO_32);
10614 switch (opcode) {
10615 case 0x0: /* MLA */
10616 case 0x4: /* MLS */
10617 case 0x8: /* MUL */
10619 static NeonGenTwoOpFn * const fns[2][2] = {
10620 { gen_helper_neon_add_u16, gen_helper_neon_sub_u16 },
10621 { tcg_gen_add_i32, tcg_gen_sub_i32 },
10623 NeonGenTwoOpFn *genfn;
10624 bool is_sub = opcode == 0x4;
10626 if (size == 1) {
10627 gen_helper_neon_mul_u16(tcg_res, tcg_op, tcg_idx);
10628 } else {
10629 tcg_gen_mul_i32(tcg_res, tcg_op, tcg_idx);
10631 if (opcode == 0x8) {
10632 break;
10634 read_vec_element_i32(s, tcg_op, rd, pass, MO_32);
10635 genfn = fns[size - 1][is_sub];
10636 genfn(tcg_res, tcg_op, tcg_res);
10637 break;
10639 case 0x5: /* FMLS */
10640 /* As usual for ARM, separate negation for fused multiply-add */
10641 gen_helper_vfp_negs(tcg_op, tcg_op);
10642 /* fall through */
10643 case 0x1: /* FMLA */
10644 read_vec_element_i32(s, tcg_res, rd, pass, MO_32);
10645 gen_helper_vfp_muladds(tcg_res, tcg_op, tcg_idx, tcg_res, fpst);
10646 break;
10647 case 0x9: /* FMUL, FMULX */
10648 if (u) {
10649 gen_helper_vfp_mulxs(tcg_res, tcg_op, tcg_idx, fpst);
10650 } else {
10651 gen_helper_vfp_muls(tcg_res, tcg_op, tcg_idx, fpst);
10653 break;
10654 case 0xc: /* SQDMULH */
10655 if (size == 1) {
10656 gen_helper_neon_qdmulh_s16(tcg_res, cpu_env,
10657 tcg_op, tcg_idx);
10658 } else {
10659 gen_helper_neon_qdmulh_s32(tcg_res, cpu_env,
10660 tcg_op, tcg_idx);
10662 break;
10663 case 0xd: /* SQRDMULH */
10664 if (size == 1) {
10665 gen_helper_neon_qrdmulh_s16(tcg_res, cpu_env,
10666 tcg_op, tcg_idx);
10667 } else {
10668 gen_helper_neon_qrdmulh_s32(tcg_res, cpu_env,
10669 tcg_op, tcg_idx);
10671 break;
10672 default:
10673 g_assert_not_reached();
10676 if (is_scalar) {
10677 write_fp_sreg(s, rd, tcg_res);
10678 } else {
10679 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
10682 tcg_temp_free_i32(tcg_op);
10683 tcg_temp_free_i32(tcg_res);
10686 tcg_temp_free_i32(tcg_idx);
10688 if (!is_q) {
10689 clear_vec_high(s, rd);
10691 } else {
10692 /* long ops: 16x16->32 or 32x32->64 */
10693 TCGv_i64 tcg_res[2];
10694 int pass;
10695 bool satop = extract32(opcode, 0, 1);
10696 TCGMemOp memop = MO_32;
10698 if (satop || !u) {
10699 memop |= MO_SIGN;
10702 if (size == 2) {
10703 TCGv_i64 tcg_idx = tcg_temp_new_i64();
10705 read_vec_element(s, tcg_idx, rm, index, memop);
10707 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
10708 TCGv_i64 tcg_op = tcg_temp_new_i64();
10709 TCGv_i64 tcg_passres;
10710 int passelt;
10712 if (is_scalar) {
10713 passelt = 0;
10714 } else {
10715 passelt = pass + (is_q * 2);
10718 read_vec_element(s, tcg_op, rn, passelt, memop);
10720 tcg_res[pass] = tcg_temp_new_i64();
10722 if (opcode == 0xa || opcode == 0xb) {
10723 /* Non-accumulating ops */
10724 tcg_passres = tcg_res[pass];
10725 } else {
10726 tcg_passres = tcg_temp_new_i64();
10729 tcg_gen_mul_i64(tcg_passres, tcg_op, tcg_idx);
10730 tcg_temp_free_i64(tcg_op);
10732 if (satop) {
10733 /* saturating, doubling */
10734 gen_helper_neon_addl_saturate_s64(tcg_passres, cpu_env,
10735 tcg_passres, tcg_passres);
10738 if (opcode == 0xa || opcode == 0xb) {
10739 continue;
10742 /* Accumulating op: handle accumulate step */
10743 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
10745 switch (opcode) {
10746 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10747 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
10748 break;
10749 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10750 tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
10751 break;
10752 case 0x7: /* SQDMLSL, SQDMLSL2 */
10753 tcg_gen_neg_i64(tcg_passres, tcg_passres);
10754 /* fall through */
10755 case 0x3: /* SQDMLAL, SQDMLAL2 */
10756 gen_helper_neon_addl_saturate_s64(tcg_res[pass], cpu_env,
10757 tcg_res[pass],
10758 tcg_passres);
10759 break;
10760 default:
10761 g_assert_not_reached();
10763 tcg_temp_free_i64(tcg_passres);
10765 tcg_temp_free_i64(tcg_idx);
10767 if (is_scalar) {
10768 clear_vec_high(s, rd);
10770 } else {
10771 TCGv_i32 tcg_idx = tcg_temp_new_i32();
10773 assert(size == 1);
10774 read_vec_element_i32(s, tcg_idx, rm, index, size);
10776 if (!is_scalar) {
10777 /* The simplest way to handle the 16x16 indexed ops is to
10778 * duplicate the index into both halves of the 32 bit tcg_idx
10779 * and then use the usual Neon helpers.
10781 tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
10784 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
10785 TCGv_i32 tcg_op = tcg_temp_new_i32();
10786 TCGv_i64 tcg_passres;
10788 if (is_scalar) {
10789 read_vec_element_i32(s, tcg_op, rn, pass, size);
10790 } else {
10791 read_vec_element_i32(s, tcg_op, rn,
10792 pass + (is_q * 2), MO_32);
10795 tcg_res[pass] = tcg_temp_new_i64();
10797 if (opcode == 0xa || opcode == 0xb) {
10798 /* Non-accumulating ops */
10799 tcg_passres = tcg_res[pass];
10800 } else {
10801 tcg_passres = tcg_temp_new_i64();
10804 if (memop & MO_SIGN) {
10805 gen_helper_neon_mull_s16(tcg_passres, tcg_op, tcg_idx);
10806 } else {
10807 gen_helper_neon_mull_u16(tcg_passres, tcg_op, tcg_idx);
10809 if (satop) {
10810 gen_helper_neon_addl_saturate_s32(tcg_passres, cpu_env,
10811 tcg_passres, tcg_passres);
10813 tcg_temp_free_i32(tcg_op);
10815 if (opcode == 0xa || opcode == 0xb) {
10816 continue;
10819 /* Accumulating op: handle accumulate step */
10820 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
10822 switch (opcode) {
10823 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10824 gen_helper_neon_addl_u32(tcg_res[pass], tcg_res[pass],
10825 tcg_passres);
10826 break;
10827 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10828 gen_helper_neon_subl_u32(tcg_res[pass], tcg_res[pass],
10829 tcg_passres);
10830 break;
10831 case 0x7: /* SQDMLSL, SQDMLSL2 */
10832 gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
10833 /* fall through */
10834 case 0x3: /* SQDMLAL, SQDMLAL2 */
10835 gen_helper_neon_addl_saturate_s32(tcg_res[pass], cpu_env,
10836 tcg_res[pass],
10837 tcg_passres);
10838 break;
10839 default:
10840 g_assert_not_reached();
10842 tcg_temp_free_i64(tcg_passres);
10844 tcg_temp_free_i32(tcg_idx);
10846 if (is_scalar) {
10847 tcg_gen_ext32u_i64(tcg_res[0], tcg_res[0]);
10851 if (is_scalar) {
10852 tcg_res[1] = tcg_const_i64(0);
10855 for (pass = 0; pass < 2; pass++) {
10856 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
10857 tcg_temp_free_i64(tcg_res[pass]);
10861 if (!TCGV_IS_UNUSED_PTR(fpst)) {
10862 tcg_temp_free_ptr(fpst);
10866 /* C3.6.19 Crypto AES
10867 * 31 24 23 22 21 17 16 12 11 10 9 5 4 0
10868 * +-----------------+------+-----------+--------+-----+------+------+
10869 * | 0 1 0 0 1 1 1 0 | size | 1 0 1 0 0 | opcode | 1 0 | Rn | Rd |
10870 * +-----------------+------+-----------+--------+-----+------+------+
10872 static void disas_crypto_aes(DisasContext *s, uint32_t insn)
10874 int size = extract32(insn, 22, 2);
10875 int opcode = extract32(insn, 12, 5);
10876 int rn = extract32(insn, 5, 5);
10877 int rd = extract32(insn, 0, 5);
10878 int decrypt;
10879 TCGv_i32 tcg_rd_regno, tcg_rn_regno, tcg_decrypt;
10880 CryptoThreeOpEnvFn *genfn;
10882 if (!arm_dc_feature(s, ARM_FEATURE_V8_AES)
10883 || size != 0) {
10884 unallocated_encoding(s);
10885 return;
10888 switch (opcode) {
10889 case 0x4: /* AESE */
10890 decrypt = 0;
10891 genfn = gen_helper_crypto_aese;
10892 break;
10893 case 0x6: /* AESMC */
10894 decrypt = 0;
10895 genfn = gen_helper_crypto_aesmc;
10896 break;
10897 case 0x5: /* AESD */
10898 decrypt = 1;
10899 genfn = gen_helper_crypto_aese;
10900 break;
10901 case 0x7: /* AESIMC */
10902 decrypt = 1;
10903 genfn = gen_helper_crypto_aesmc;
10904 break;
10905 default:
10906 unallocated_encoding(s);
10907 return;
10910 /* Note that we convert the Vx register indexes into the
10911 * index within the vfp.regs[] array, so we can share the
10912 * helper with the AArch32 instructions.
10914 tcg_rd_regno = tcg_const_i32(rd << 1);
10915 tcg_rn_regno = tcg_const_i32(rn << 1);
10916 tcg_decrypt = tcg_const_i32(decrypt);
10918 genfn(cpu_env, tcg_rd_regno, tcg_rn_regno, tcg_decrypt);
10920 tcg_temp_free_i32(tcg_rd_regno);
10921 tcg_temp_free_i32(tcg_rn_regno);
10922 tcg_temp_free_i32(tcg_decrypt);
10925 /* C3.6.20 Crypto three-reg SHA
10926 * 31 24 23 22 21 20 16 15 14 12 11 10 9 5 4 0
10927 * +-----------------+------+---+------+---+--------+-----+------+------+
10928 * | 0 1 0 1 1 1 1 0 | size | 0 | Rm | 0 | opcode | 0 0 | Rn | Rd |
10929 * +-----------------+------+---+------+---+--------+-----+------+------+
10931 static void disas_crypto_three_reg_sha(DisasContext *s, uint32_t insn)
10933 int size = extract32(insn, 22, 2);
10934 int opcode = extract32(insn, 12, 3);
10935 int rm = extract32(insn, 16, 5);
10936 int rn = extract32(insn, 5, 5);
10937 int rd = extract32(insn, 0, 5);
10938 CryptoThreeOpEnvFn *genfn;
10939 TCGv_i32 tcg_rd_regno, tcg_rn_regno, tcg_rm_regno;
10940 int feature = ARM_FEATURE_V8_SHA256;
10942 if (size != 0) {
10943 unallocated_encoding(s);
10944 return;
10947 switch (opcode) {
10948 case 0: /* SHA1C */
10949 case 1: /* SHA1P */
10950 case 2: /* SHA1M */
10951 case 3: /* SHA1SU0 */
10952 genfn = NULL;
10953 feature = ARM_FEATURE_V8_SHA1;
10954 break;
10955 case 4: /* SHA256H */
10956 genfn = gen_helper_crypto_sha256h;
10957 break;
10958 case 5: /* SHA256H2 */
10959 genfn = gen_helper_crypto_sha256h2;
10960 break;
10961 case 6: /* SHA256SU1 */
10962 genfn = gen_helper_crypto_sha256su1;
10963 break;
10964 default:
10965 unallocated_encoding(s);
10966 return;
10969 if (!arm_dc_feature(s, feature)) {
10970 unallocated_encoding(s);
10971 return;
10974 tcg_rd_regno = tcg_const_i32(rd << 1);
10975 tcg_rn_regno = tcg_const_i32(rn << 1);
10976 tcg_rm_regno = tcg_const_i32(rm << 1);
10978 if (genfn) {
10979 genfn(cpu_env, tcg_rd_regno, tcg_rn_regno, tcg_rm_regno);
10980 } else {
10981 TCGv_i32 tcg_opcode = tcg_const_i32(opcode);
10983 gen_helper_crypto_sha1_3reg(cpu_env, tcg_rd_regno,
10984 tcg_rn_regno, tcg_rm_regno, tcg_opcode);
10985 tcg_temp_free_i32(tcg_opcode);
10988 tcg_temp_free_i32(tcg_rd_regno);
10989 tcg_temp_free_i32(tcg_rn_regno);
10990 tcg_temp_free_i32(tcg_rm_regno);
10993 /* C3.6.21 Crypto two-reg SHA
10994 * 31 24 23 22 21 17 16 12 11 10 9 5 4 0
10995 * +-----------------+------+-----------+--------+-----+------+------+
10996 * | 0 1 0 1 1 1 1 0 | size | 1 0 1 0 0 | opcode | 1 0 | Rn | Rd |
10997 * +-----------------+------+-----------+--------+-----+------+------+
10999 static void disas_crypto_two_reg_sha(DisasContext *s, uint32_t insn)
11001 int size = extract32(insn, 22, 2);
11002 int opcode = extract32(insn, 12, 5);
11003 int rn = extract32(insn, 5, 5);
11004 int rd = extract32(insn, 0, 5);
11005 CryptoTwoOpEnvFn *genfn;
11006 int feature;
11007 TCGv_i32 tcg_rd_regno, tcg_rn_regno;
11009 if (size != 0) {
11010 unallocated_encoding(s);
11011 return;
11014 switch (opcode) {
11015 case 0: /* SHA1H */
11016 feature = ARM_FEATURE_V8_SHA1;
11017 genfn = gen_helper_crypto_sha1h;
11018 break;
11019 case 1: /* SHA1SU1 */
11020 feature = ARM_FEATURE_V8_SHA1;
11021 genfn = gen_helper_crypto_sha1su1;
11022 break;
11023 case 2: /* SHA256SU0 */
11024 feature = ARM_FEATURE_V8_SHA256;
11025 genfn = gen_helper_crypto_sha256su0;
11026 break;
11027 default:
11028 unallocated_encoding(s);
11029 return;
11032 if (!arm_dc_feature(s, feature)) {
11033 unallocated_encoding(s);
11034 return;
11037 tcg_rd_regno = tcg_const_i32(rd << 1);
11038 tcg_rn_regno = tcg_const_i32(rn << 1);
11040 genfn(cpu_env, tcg_rd_regno, tcg_rn_regno);
11042 tcg_temp_free_i32(tcg_rd_regno);
11043 tcg_temp_free_i32(tcg_rn_regno);
11046 /* C3.6 Data processing - SIMD, inc Crypto
11048 * As the decode gets a little complex we are using a table based
11049 * approach for this part of the decode.
11051 static const AArch64DecodeTable data_proc_simd[] = {
11052 /* pattern , mask , fn */
11053 { 0x0e200400, 0x9f200400, disas_simd_three_reg_same },
11054 { 0x0e200000, 0x9f200c00, disas_simd_three_reg_diff },
11055 { 0x0e200800, 0x9f3e0c00, disas_simd_two_reg_misc },
11056 { 0x0e300800, 0x9f3e0c00, disas_simd_across_lanes },
11057 { 0x0e000400, 0x9fe08400, disas_simd_copy },
11058 { 0x0f000000, 0x9f000400, disas_simd_indexed }, /* vector indexed */
11059 /* simd_mod_imm decode is a subset of simd_shift_imm, so must precede it */
11060 { 0x0f000400, 0x9ff80400, disas_simd_mod_imm },
11061 { 0x0f000400, 0x9f800400, disas_simd_shift_imm },
11062 { 0x0e000000, 0xbf208c00, disas_simd_tb },
11063 { 0x0e000800, 0xbf208c00, disas_simd_zip_trn },
11064 { 0x2e000000, 0xbf208400, disas_simd_ext },
11065 { 0x5e200400, 0xdf200400, disas_simd_scalar_three_reg_same },
11066 { 0x5e200000, 0xdf200c00, disas_simd_scalar_three_reg_diff },
11067 { 0x5e200800, 0xdf3e0c00, disas_simd_scalar_two_reg_misc },
11068 { 0x5e300800, 0xdf3e0c00, disas_simd_scalar_pairwise },
11069 { 0x5e000400, 0xdfe08400, disas_simd_scalar_copy },
11070 { 0x5f000000, 0xdf000400, disas_simd_indexed }, /* scalar indexed */
11071 { 0x5f000400, 0xdf800400, disas_simd_scalar_shift_imm },
11072 { 0x4e280800, 0xff3e0c00, disas_crypto_aes },
11073 { 0x5e000000, 0xff208c00, disas_crypto_three_reg_sha },
11074 { 0x5e280800, 0xff3e0c00, disas_crypto_two_reg_sha },
11075 { 0x00000000, 0x00000000, NULL }
11078 static void disas_data_proc_simd(DisasContext *s, uint32_t insn)
11080 /* Note that this is called with all non-FP cases from
11081 * table C3-6 so it must UNDEF for entries not specifically
11082 * allocated to instructions in that table.
11084 AArch64DecodeFn *fn = lookup_disas_fn(&data_proc_simd[0], insn);
11085 if (fn) {
11086 fn(s, insn);
11087 } else {
11088 unallocated_encoding(s);
11092 /* C3.6 Data processing - SIMD and floating point */
11093 static void disas_data_proc_simd_fp(DisasContext *s, uint32_t insn)
11095 if (extract32(insn, 28, 1) == 1 && extract32(insn, 30, 1) == 0) {
11096 disas_data_proc_fp(s, insn);
11097 } else {
11098 /* SIMD, including crypto */
11099 disas_data_proc_simd(s, insn);
11103 /* C3.1 A64 instruction index by encoding */
11104 static void disas_a64_insn(CPUARMState *env, DisasContext *s)
11106 uint32_t insn;
11108 insn = arm_ldl_code(env, s->pc, s->sctlr_b);
11109 s->insn = insn;
11110 s->pc += 4;
11112 s->fp_access_checked = false;
11114 switch (extract32(insn, 25, 4)) {
11115 case 0x0: case 0x1: case 0x2: case 0x3: /* UNALLOCATED */
11116 unallocated_encoding(s);
11117 break;
11118 case 0x8: case 0x9: /* Data processing - immediate */
11119 disas_data_proc_imm(s, insn);
11120 break;
11121 case 0xa: case 0xb: /* Branch, exception generation and system insns */
11122 disas_b_exc_sys(s, insn);
11123 break;
11124 case 0x4:
11125 case 0x6:
11126 case 0xc:
11127 case 0xe: /* Loads and stores */
11128 disas_ldst(s, insn);
11129 break;
11130 case 0x5:
11131 case 0xd: /* Data processing - register */
11132 disas_data_proc_reg(s, insn);
11133 break;
11134 case 0x7:
11135 case 0xf: /* Data processing - SIMD and floating point */
11136 disas_data_proc_simd_fp(s, insn);
11137 break;
11138 default:
11139 assert(FALSE); /* all 15 cases should be handled above */
11140 break;
11143 /* if we allocated any temporaries, free them here */
11144 free_tmp_a64(s);
11147 void gen_intermediate_code_a64(ARMCPU *cpu, TranslationBlock *tb)
11149 CPUState *cs = CPU(cpu);
11150 CPUARMState *env = &cpu->env;
11151 DisasContext dc1, *dc = &dc1;
11152 target_ulong pc_start;
11153 target_ulong next_page_start;
11154 int num_insns;
11155 int max_insns;
11157 pc_start = tb->pc;
11159 dc->tb = tb;
11161 dc->is_jmp = DISAS_NEXT;
11162 dc->pc = pc_start;
11163 dc->singlestep_enabled = cs->singlestep_enabled;
11164 dc->condjmp = 0;
11166 dc->aarch64 = 1;
11167 /* If we are coming from secure EL0 in a system with a 32-bit EL3, then
11168 * there is no secure EL1, so we route exceptions to EL3.
11170 dc->secure_routed_to_el3 = arm_feature(env, ARM_FEATURE_EL3) &&
11171 !arm_el_is_aa64(env, 3);
11172 dc->thumb = 0;
11173 dc->sctlr_b = 0;
11174 dc->be_data = ARM_TBFLAG_BE_DATA(tb->flags) ? MO_BE : MO_LE;
11175 dc->condexec_mask = 0;
11176 dc->condexec_cond = 0;
11177 dc->mmu_idx = ARM_TBFLAG_MMUIDX(tb->flags);
11178 dc->current_el = arm_mmu_idx_to_el(dc->mmu_idx);
11179 #if !defined(CONFIG_USER_ONLY)
11180 dc->user = (dc->current_el == 0);
11181 #endif
11182 dc->fp_excp_el = ARM_TBFLAG_FPEXC_EL(tb->flags);
11183 dc->vec_len = 0;
11184 dc->vec_stride = 0;
11185 dc->cp_regs = cpu->cp_regs;
11186 dc->features = env->features;
11188 /* Single step state. The code-generation logic here is:
11189 * SS_ACTIVE == 0:
11190 * generate code with no special handling for single-stepping (except
11191 * that anything that can make us go to SS_ACTIVE == 1 must end the TB;
11192 * this happens anyway because those changes are all system register or
11193 * PSTATE writes).
11194 * SS_ACTIVE == 1, PSTATE.SS == 1: (active-not-pending)
11195 * emit code for one insn
11196 * emit code to clear PSTATE.SS
11197 * emit code to generate software step exception for completed step
11198 * end TB (as usual for having generated an exception)
11199 * SS_ACTIVE == 1, PSTATE.SS == 0: (active-pending)
11200 * emit code to generate a software step exception
11201 * end the TB
11203 dc->ss_active = ARM_TBFLAG_SS_ACTIVE(tb->flags);
11204 dc->pstate_ss = ARM_TBFLAG_PSTATE_SS(tb->flags);
11205 dc->is_ldex = false;
11206 dc->ss_same_el = (arm_debug_target_el(env) == dc->current_el);
11208 init_tmp_a64_array(dc);
11210 next_page_start = (pc_start & TARGET_PAGE_MASK) + TARGET_PAGE_SIZE;
11211 num_insns = 0;
11212 max_insns = tb->cflags & CF_COUNT_MASK;
11213 if (max_insns == 0) {
11214 max_insns = CF_COUNT_MASK;
11216 if (max_insns > TCG_MAX_INSNS) {
11217 max_insns = TCG_MAX_INSNS;
11220 gen_tb_start(tb);
11222 tcg_clear_temp_count();
11224 do {
11225 dc->insn_start_idx = tcg_op_buf_count();
11226 tcg_gen_insn_start(dc->pc, 0, 0);
11227 num_insns++;
11229 if (unlikely(!QTAILQ_EMPTY(&cs->breakpoints))) {
11230 CPUBreakpoint *bp;
11231 QTAILQ_FOREACH(bp, &cs->breakpoints, entry) {
11232 if (bp->pc == dc->pc) {
11233 if (bp->flags & BP_CPU) {
11234 gen_a64_set_pc_im(dc->pc);
11235 gen_helper_check_breakpoints(cpu_env);
11236 /* End the TB early; it likely won't be executed */
11237 dc->is_jmp = DISAS_UPDATE;
11238 } else {
11239 gen_exception_internal_insn(dc, 0, EXCP_DEBUG);
11240 /* The address covered by the breakpoint must be
11241 included in [tb->pc, tb->pc + tb->size) in order
11242 to for it to be properly cleared -- thus we
11243 increment the PC here so that the logic setting
11244 tb->size below does the right thing. */
11245 dc->pc += 4;
11246 goto done_generating;
11248 break;
11253 if (num_insns == max_insns && (tb->cflags & CF_LAST_IO)) {
11254 gen_io_start();
11257 if (dc->ss_active && !dc->pstate_ss) {
11258 /* Singlestep state is Active-pending.
11259 * If we're in this state at the start of a TB then either
11260 * a) we just took an exception to an EL which is being debugged
11261 * and this is the first insn in the exception handler
11262 * b) debug exceptions were masked and we just unmasked them
11263 * without changing EL (eg by clearing PSTATE.D)
11264 * In either case we're going to take a swstep exception in the
11265 * "did not step an insn" case, and so the syndrome ISV and EX
11266 * bits should be zero.
11268 assert(num_insns == 1);
11269 gen_exception(EXCP_UDEF, syn_swstep(dc->ss_same_el, 0, 0),
11270 default_exception_el(dc));
11271 dc->is_jmp = DISAS_EXC;
11272 break;
11275 disas_a64_insn(env, dc);
11277 if (tcg_check_temp_count()) {
11278 fprintf(stderr, "TCG temporary leak before "TARGET_FMT_lx"\n",
11279 dc->pc);
11282 /* Translation stops when a conditional branch is encountered.
11283 * Otherwise the subsequent code could get translated several times.
11284 * Also stop translation when a page boundary is reached. This
11285 * ensures prefetch aborts occur at the right place.
11287 } while (!dc->is_jmp && !tcg_op_buf_full() &&
11288 !cs->singlestep_enabled &&
11289 !singlestep &&
11290 !dc->ss_active &&
11291 dc->pc < next_page_start &&
11292 num_insns < max_insns);
11294 if (tb->cflags & CF_LAST_IO) {
11295 gen_io_end();
11298 if (unlikely(cs->singlestep_enabled || dc->ss_active)
11299 && dc->is_jmp != DISAS_EXC) {
11300 /* Note that this means single stepping WFI doesn't halt the CPU.
11301 * For conditional branch insns this is harmless unreachable code as
11302 * gen_goto_tb() has already handled emitting the debug exception
11303 * (and thus a tb-jump is not possible when singlestepping).
11305 assert(dc->is_jmp != DISAS_TB_JUMP);
11306 if (dc->is_jmp != DISAS_JUMP) {
11307 gen_a64_set_pc_im(dc->pc);
11309 if (cs->singlestep_enabled) {
11310 gen_exception_internal(EXCP_DEBUG);
11311 } else {
11312 gen_step_complete_exception(dc);
11314 } else {
11315 switch (dc->is_jmp) {
11316 case DISAS_NEXT:
11317 gen_goto_tb(dc, 1, dc->pc);
11318 break;
11319 default:
11320 case DISAS_UPDATE:
11321 gen_a64_set_pc_im(dc->pc);
11322 /* fall through */
11323 case DISAS_JUMP:
11324 /* indicate that the hash table must be used to find the next TB */
11325 tcg_gen_exit_tb(0);
11326 break;
11327 case DISAS_TB_JUMP:
11328 case DISAS_EXC:
11329 case DISAS_SWI:
11330 break;
11331 case DISAS_WFE:
11332 gen_a64_set_pc_im(dc->pc);
11333 gen_helper_wfe(cpu_env);
11334 break;
11335 case DISAS_YIELD:
11336 gen_a64_set_pc_im(dc->pc);
11337 gen_helper_yield(cpu_env);
11338 break;
11339 case DISAS_WFI:
11340 /* This is a special case because we don't want to just halt the CPU
11341 * if trying to debug across a WFI.
11343 gen_a64_set_pc_im(dc->pc);
11344 gen_helper_wfi(cpu_env);
11345 /* The helper doesn't necessarily throw an exception, but we
11346 * must go back to the main loop to check for interrupts anyway.
11348 tcg_gen_exit_tb(0);
11349 break;
11353 done_generating:
11354 gen_tb_end(tb, num_insns);
11356 #ifdef DEBUG_DISAS
11357 if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM) &&
11358 qemu_log_in_addr_range(pc_start)) {
11359 qemu_log("----------------\n");
11360 qemu_log("IN: %s\n", lookup_symbol(pc_start));
11361 log_target_disas(cs, pc_start, dc->pc - pc_start,
11362 4 | (bswap_code(dc->sctlr_b) ? 2 : 0));
11363 qemu_log("\n");
11365 #endif
11366 tb->size = dc->pc - pc_start;
11367 tb->icount = num_insns;