target-arm: A64: Avoid signed shifts in disas_ldst_pair()
[qemu.git] / target-arm / translate-a64.c
blob9f54501a9ec744e4466506b688ea0b761a6370c2
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 <stdarg.h>
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <inttypes.h>
25 #include "cpu.h"
26 #include "tcg-op.h"
27 #include "qemu/log.h"
28 #include "arm_ldst.h"
29 #include "translate.h"
30 #include "internals.h"
31 #include "qemu/host-utils.h"
33 #include "exec/gen-icount.h"
35 #include "exec/helper-proto.h"
36 #include "exec/helper-gen.h"
38 #include "trace-tcg.h"
40 static TCGv_i64 cpu_X[32];
41 static TCGv_i64 cpu_pc;
42 static TCGv_i32 cpu_NF, cpu_ZF, cpu_CF, cpu_VF;
44 /* Load/store exclusive handling */
45 static TCGv_i64 cpu_exclusive_addr;
46 static TCGv_i64 cpu_exclusive_val;
47 static TCGv_i64 cpu_exclusive_high;
48 #ifdef CONFIG_USER_ONLY
49 static TCGv_i64 cpu_exclusive_test;
50 static TCGv_i32 cpu_exclusive_info;
51 #endif
53 static const char *regnames[] = {
54 "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7",
55 "x8", "x9", "x10", "x11", "x12", "x13", "x14", "x15",
56 "x16", "x17", "x18", "x19", "x20", "x21", "x22", "x23",
57 "x24", "x25", "x26", "x27", "x28", "x29", "lr", "sp"
60 enum a64_shift_type {
61 A64_SHIFT_TYPE_LSL = 0,
62 A64_SHIFT_TYPE_LSR = 1,
63 A64_SHIFT_TYPE_ASR = 2,
64 A64_SHIFT_TYPE_ROR = 3
67 /* Table based decoder typedefs - used when the relevant bits for decode
68 * are too awkwardly scattered across the instruction (eg SIMD).
70 typedef void AArch64DecodeFn(DisasContext *s, uint32_t insn);
72 typedef struct AArch64DecodeTable {
73 uint32_t pattern;
74 uint32_t mask;
75 AArch64DecodeFn *disas_fn;
76 } AArch64DecodeTable;
78 /* Function prototype for gen_ functions for calling Neon helpers */
79 typedef void NeonGenOneOpEnvFn(TCGv_i32, TCGv_ptr, TCGv_i32);
80 typedef void NeonGenTwoOpFn(TCGv_i32, TCGv_i32, TCGv_i32);
81 typedef void NeonGenTwoOpEnvFn(TCGv_i32, TCGv_ptr, TCGv_i32, TCGv_i32);
82 typedef void NeonGenTwo64OpFn(TCGv_i64, TCGv_i64, TCGv_i64);
83 typedef void NeonGenTwo64OpEnvFn(TCGv_i64, TCGv_ptr, TCGv_i64, TCGv_i64);
84 typedef void NeonGenNarrowFn(TCGv_i32, TCGv_i64);
85 typedef void NeonGenNarrowEnvFn(TCGv_i32, TCGv_ptr, TCGv_i64);
86 typedef void NeonGenWidenFn(TCGv_i64, TCGv_i32);
87 typedef void NeonGenTwoSingleOPFn(TCGv_i32, TCGv_i32, TCGv_i32, TCGv_ptr);
88 typedef void NeonGenTwoDoubleOPFn(TCGv_i64, TCGv_i64, TCGv_i64, TCGv_ptr);
89 typedef void NeonGenOneOpFn(TCGv_i64, TCGv_i64);
90 typedef void CryptoTwoOpEnvFn(TCGv_ptr, TCGv_i32, TCGv_i32);
91 typedef void CryptoThreeOpEnvFn(TCGv_ptr, TCGv_i32, TCGv_i32, TCGv_i32);
93 /* initialize TCG globals. */
94 void a64_translate_init(void)
96 int i;
98 cpu_pc = tcg_global_mem_new_i64(TCG_AREG0,
99 offsetof(CPUARMState, pc),
100 "pc");
101 for (i = 0; i < 32; i++) {
102 cpu_X[i] = tcg_global_mem_new_i64(TCG_AREG0,
103 offsetof(CPUARMState, xregs[i]),
104 regnames[i]);
107 cpu_NF = tcg_global_mem_new_i32(TCG_AREG0, offsetof(CPUARMState, NF), "NF");
108 cpu_ZF = tcg_global_mem_new_i32(TCG_AREG0, offsetof(CPUARMState, ZF), "ZF");
109 cpu_CF = tcg_global_mem_new_i32(TCG_AREG0, offsetof(CPUARMState, CF), "CF");
110 cpu_VF = tcg_global_mem_new_i32(TCG_AREG0, offsetof(CPUARMState, VF), "VF");
112 cpu_exclusive_addr = tcg_global_mem_new_i64(TCG_AREG0,
113 offsetof(CPUARMState, exclusive_addr), "exclusive_addr");
114 cpu_exclusive_val = tcg_global_mem_new_i64(TCG_AREG0,
115 offsetof(CPUARMState, exclusive_val), "exclusive_val");
116 cpu_exclusive_high = tcg_global_mem_new_i64(TCG_AREG0,
117 offsetof(CPUARMState, exclusive_high), "exclusive_high");
118 #ifdef CONFIG_USER_ONLY
119 cpu_exclusive_test = tcg_global_mem_new_i64(TCG_AREG0,
120 offsetof(CPUARMState, exclusive_test), "exclusive_test");
121 cpu_exclusive_info = tcg_global_mem_new_i32(TCG_AREG0,
122 offsetof(CPUARMState, exclusive_info), "exclusive_info");
123 #endif
126 static inline ARMMMUIdx get_a64_user_mem_index(DisasContext *s)
128 /* Return the mmu_idx to use for A64 "unprivileged load/store" insns:
129 * if EL1, access as if EL0; otherwise access at current EL
131 switch (s->mmu_idx) {
132 case ARMMMUIdx_S12NSE1:
133 return ARMMMUIdx_S12NSE0;
134 case ARMMMUIdx_S1SE1:
135 return ARMMMUIdx_S1SE0;
136 case ARMMMUIdx_S2NS:
137 g_assert_not_reached();
138 default:
139 return s->mmu_idx;
143 void aarch64_cpu_dump_state(CPUState *cs, FILE *f,
144 fprintf_function cpu_fprintf, int flags)
146 ARMCPU *cpu = ARM_CPU(cs);
147 CPUARMState *env = &cpu->env;
148 uint32_t psr = pstate_read(env);
149 int i;
151 cpu_fprintf(f, "PC=%016"PRIx64" SP=%016"PRIx64"\n",
152 env->pc, env->xregs[31]);
153 for (i = 0; i < 31; i++) {
154 cpu_fprintf(f, "X%02d=%016"PRIx64, i, env->xregs[i]);
155 if ((i % 4) == 3) {
156 cpu_fprintf(f, "\n");
157 } else {
158 cpu_fprintf(f, " ");
161 cpu_fprintf(f, "PSTATE=%08x (flags %c%c%c%c)\n",
162 psr,
163 psr & PSTATE_N ? 'N' : '-',
164 psr & PSTATE_Z ? 'Z' : '-',
165 psr & PSTATE_C ? 'C' : '-',
166 psr & PSTATE_V ? 'V' : '-');
167 cpu_fprintf(f, "\n");
169 if (flags & CPU_DUMP_FPU) {
170 int numvfpregs = 32;
171 for (i = 0; i < numvfpregs; i += 2) {
172 uint64_t vlo = float64_val(env->vfp.regs[i * 2]);
173 uint64_t vhi = float64_val(env->vfp.regs[(i * 2) + 1]);
174 cpu_fprintf(f, "q%02d=%016" PRIx64 ":%016" PRIx64 " ",
175 i, vhi, vlo);
176 vlo = float64_val(env->vfp.regs[(i + 1) * 2]);
177 vhi = float64_val(env->vfp.regs[((i + 1) * 2) + 1]);
178 cpu_fprintf(f, "q%02d=%016" PRIx64 ":%016" PRIx64 "\n",
179 i + 1, vhi, vlo);
181 cpu_fprintf(f, "FPCR: %08x FPSR: %08x\n",
182 vfp_get_fpcr(env), vfp_get_fpsr(env));
186 void gen_a64_set_pc_im(uint64_t val)
188 tcg_gen_movi_i64(cpu_pc, val);
191 static void gen_exception_internal(int excp)
193 TCGv_i32 tcg_excp = tcg_const_i32(excp);
195 assert(excp_is_internal(excp));
196 gen_helper_exception_internal(cpu_env, tcg_excp);
197 tcg_temp_free_i32(tcg_excp);
200 static void gen_exception(int excp, uint32_t syndrome)
202 TCGv_i32 tcg_excp = tcg_const_i32(excp);
203 TCGv_i32 tcg_syn = tcg_const_i32(syndrome);
205 gen_helper_exception_with_syndrome(cpu_env, tcg_excp, tcg_syn);
206 tcg_temp_free_i32(tcg_syn);
207 tcg_temp_free_i32(tcg_excp);
210 static void gen_exception_internal_insn(DisasContext *s, int offset, int excp)
212 gen_a64_set_pc_im(s->pc - offset);
213 gen_exception_internal(excp);
214 s->is_jmp = DISAS_EXC;
217 static void gen_exception_insn(DisasContext *s, int offset, int excp,
218 uint32_t syndrome)
220 gen_a64_set_pc_im(s->pc - offset);
221 gen_exception(excp, syndrome);
222 s->is_jmp = DISAS_EXC;
225 static void gen_ss_advance(DisasContext *s)
227 /* If the singlestep state is Active-not-pending, advance to
228 * Active-pending.
230 if (s->ss_active) {
231 s->pstate_ss = 0;
232 gen_helper_clear_pstate_ss(cpu_env);
236 static void gen_step_complete_exception(DisasContext *s)
238 /* We just completed step of an insn. Move from Active-not-pending
239 * to Active-pending, and then also take the swstep exception.
240 * This corresponds to making the (IMPDEF) choice to prioritize
241 * swstep exceptions over asynchronous exceptions taken to an exception
242 * level where debug is disabled. This choice has the advantage that
243 * we do not need to maintain internal state corresponding to the
244 * ISV/EX syndrome bits between completion of the step and generation
245 * of the exception, and our syndrome information is always correct.
247 gen_ss_advance(s);
248 gen_exception(EXCP_UDEF, syn_swstep(s->ss_same_el, 1, s->is_ldex));
249 s->is_jmp = DISAS_EXC;
252 static inline bool use_goto_tb(DisasContext *s, int n, uint64_t dest)
254 /* No direct tb linking with singlestep (either QEMU's or the ARM
255 * debug architecture kind) or deterministic io
257 if (s->singlestep_enabled || s->ss_active || (s->tb->cflags & CF_LAST_IO)) {
258 return false;
261 /* Only link tbs from inside the same guest page */
262 if ((s->tb->pc & TARGET_PAGE_MASK) != (dest & TARGET_PAGE_MASK)) {
263 return false;
266 return true;
269 static inline void gen_goto_tb(DisasContext *s, int n, uint64_t dest)
271 TranslationBlock *tb;
273 tb = s->tb;
274 if (use_goto_tb(s, n, dest)) {
275 tcg_gen_goto_tb(n);
276 gen_a64_set_pc_im(dest);
277 tcg_gen_exit_tb((intptr_t)tb + n);
278 s->is_jmp = DISAS_TB_JUMP;
279 } else {
280 gen_a64_set_pc_im(dest);
281 if (s->ss_active) {
282 gen_step_complete_exception(s);
283 } else if (s->singlestep_enabled) {
284 gen_exception_internal(EXCP_DEBUG);
285 } else {
286 tcg_gen_exit_tb(0);
287 s->is_jmp = DISAS_TB_JUMP;
292 static void unallocated_encoding(DisasContext *s)
294 /* Unallocated and reserved encodings are uncategorized */
295 gen_exception_insn(s, 4, EXCP_UDEF, syn_uncategorized());
298 #define unsupported_encoding(s, insn) \
299 do { \
300 qemu_log_mask(LOG_UNIMP, \
301 "%s:%d: unsupported instruction encoding 0x%08x " \
302 "at pc=%016" PRIx64 "\n", \
303 __FILE__, __LINE__, insn, s->pc - 4); \
304 unallocated_encoding(s); \
305 } while (0);
307 static void init_tmp_a64_array(DisasContext *s)
309 #ifdef CONFIG_DEBUG_TCG
310 int i;
311 for (i = 0; i < ARRAY_SIZE(s->tmp_a64); i++) {
312 TCGV_UNUSED_I64(s->tmp_a64[i]);
314 #endif
315 s->tmp_a64_count = 0;
318 static void free_tmp_a64(DisasContext *s)
320 int i;
321 for (i = 0; i < s->tmp_a64_count; i++) {
322 tcg_temp_free_i64(s->tmp_a64[i]);
324 init_tmp_a64_array(s);
327 static TCGv_i64 new_tmp_a64(DisasContext *s)
329 assert(s->tmp_a64_count < TMP_A64_MAX);
330 return s->tmp_a64[s->tmp_a64_count++] = tcg_temp_new_i64();
333 static TCGv_i64 new_tmp_a64_zero(DisasContext *s)
335 TCGv_i64 t = new_tmp_a64(s);
336 tcg_gen_movi_i64(t, 0);
337 return t;
341 * Register access functions
343 * These functions are used for directly accessing a register in where
344 * changes to the final register value are likely to be made. If you
345 * need to use a register for temporary calculation (e.g. index type
346 * operations) use the read_* form.
348 * B1.2.1 Register mappings
350 * In instruction register encoding 31 can refer to ZR (zero register) or
351 * the SP (stack pointer) depending on context. In QEMU's case we map SP
352 * to cpu_X[31] and ZR accesses to a temporary which can be discarded.
353 * This is the point of the _sp forms.
355 static TCGv_i64 cpu_reg(DisasContext *s, int reg)
357 if (reg == 31) {
358 return new_tmp_a64_zero(s);
359 } else {
360 return cpu_X[reg];
364 /* register access for when 31 == SP */
365 static TCGv_i64 cpu_reg_sp(DisasContext *s, int reg)
367 return cpu_X[reg];
370 /* read a cpu register in 32bit/64bit mode. Returns a TCGv_i64
371 * representing the register contents. This TCGv is an auto-freed
372 * temporary so it need not be explicitly freed, and may be modified.
374 static TCGv_i64 read_cpu_reg(DisasContext *s, int reg, int sf)
376 TCGv_i64 v = new_tmp_a64(s);
377 if (reg != 31) {
378 if (sf) {
379 tcg_gen_mov_i64(v, cpu_X[reg]);
380 } else {
381 tcg_gen_ext32u_i64(v, cpu_X[reg]);
383 } else {
384 tcg_gen_movi_i64(v, 0);
386 return v;
389 static TCGv_i64 read_cpu_reg_sp(DisasContext *s, int reg, int sf)
391 TCGv_i64 v = new_tmp_a64(s);
392 if (sf) {
393 tcg_gen_mov_i64(v, cpu_X[reg]);
394 } else {
395 tcg_gen_ext32u_i64(v, cpu_X[reg]);
397 return v;
400 /* We should have at some point before trying to access an FP register
401 * done the necessary access check, so assert that
402 * (a) we did the check and
403 * (b) we didn't then just plough ahead anyway if it failed.
404 * Print the instruction pattern in the abort message so we can figure
405 * out what we need to fix if a user encounters this problem in the wild.
407 static inline void assert_fp_access_checked(DisasContext *s)
409 #ifdef CONFIG_DEBUG_TCG
410 if (unlikely(!s->fp_access_checked || !s->cpacr_fpen)) {
411 fprintf(stderr, "target-arm: FP access check missing for "
412 "instruction 0x%08x\n", s->insn);
413 abort();
415 #endif
418 /* Return the offset into CPUARMState of an element of specified
419 * size, 'element' places in from the least significant end of
420 * the FP/vector register Qn.
422 static inline int vec_reg_offset(DisasContext *s, int regno,
423 int element, TCGMemOp size)
425 int offs = offsetof(CPUARMState, vfp.regs[regno * 2]);
426 #ifdef HOST_WORDS_BIGENDIAN
427 /* This is complicated slightly because vfp.regs[2n] is
428 * still the low half and vfp.regs[2n+1] the high half
429 * of the 128 bit vector, even on big endian systems.
430 * Calculate the offset assuming a fully bigendian 128 bits,
431 * then XOR to account for the order of the two 64 bit halves.
433 offs += (16 - ((element + 1) * (1 << size)));
434 offs ^= 8;
435 #else
436 offs += element * (1 << size);
437 #endif
438 assert_fp_access_checked(s);
439 return offs;
442 /* Return the offset into CPUARMState of a slice (from
443 * the least significant end) of FP register Qn (ie
444 * Dn, Sn, Hn or Bn).
445 * (Note that this is not the same mapping as for A32; see cpu.h)
447 static inline int fp_reg_offset(DisasContext *s, int regno, TCGMemOp size)
449 int offs = offsetof(CPUARMState, vfp.regs[regno * 2]);
450 #ifdef HOST_WORDS_BIGENDIAN
451 offs += (8 - (1 << size));
452 #endif
453 assert_fp_access_checked(s);
454 return offs;
457 /* Offset of the high half of the 128 bit vector Qn */
458 static inline int fp_reg_hi_offset(DisasContext *s, int regno)
460 assert_fp_access_checked(s);
461 return offsetof(CPUARMState, vfp.regs[regno * 2 + 1]);
464 /* Convenience accessors for reading and writing single and double
465 * FP registers. Writing clears the upper parts of the associated
466 * 128 bit vector register, as required by the architecture.
467 * Note that unlike the GP register accessors, the values returned
468 * by the read functions must be manually freed.
470 static TCGv_i64 read_fp_dreg(DisasContext *s, int reg)
472 TCGv_i64 v = tcg_temp_new_i64();
474 tcg_gen_ld_i64(v, cpu_env, fp_reg_offset(s, reg, MO_64));
475 return v;
478 static TCGv_i32 read_fp_sreg(DisasContext *s, int reg)
480 TCGv_i32 v = tcg_temp_new_i32();
482 tcg_gen_ld_i32(v, cpu_env, fp_reg_offset(s, reg, MO_32));
483 return v;
486 static void write_fp_dreg(DisasContext *s, int reg, TCGv_i64 v)
488 TCGv_i64 tcg_zero = tcg_const_i64(0);
490 tcg_gen_st_i64(v, cpu_env, fp_reg_offset(s, reg, MO_64));
491 tcg_gen_st_i64(tcg_zero, cpu_env, fp_reg_hi_offset(s, reg));
492 tcg_temp_free_i64(tcg_zero);
495 static void write_fp_sreg(DisasContext *s, int reg, TCGv_i32 v)
497 TCGv_i64 tmp = tcg_temp_new_i64();
499 tcg_gen_extu_i32_i64(tmp, v);
500 write_fp_dreg(s, reg, tmp);
501 tcg_temp_free_i64(tmp);
504 static TCGv_ptr get_fpstatus_ptr(void)
506 TCGv_ptr statusptr = tcg_temp_new_ptr();
507 int offset;
509 /* In A64 all instructions (both FP and Neon) use the FPCR;
510 * there is no equivalent of the A32 Neon "standard FPSCR value"
511 * and all operations use vfp.fp_status.
513 offset = offsetof(CPUARMState, vfp.fp_status);
514 tcg_gen_addi_ptr(statusptr, cpu_env, offset);
515 return statusptr;
518 /* Set ZF and NF based on a 64 bit result. This is alas fiddlier
519 * than the 32 bit equivalent.
521 static inline void gen_set_NZ64(TCGv_i64 result)
523 TCGv_i64 flag = tcg_temp_new_i64();
525 tcg_gen_setcondi_i64(TCG_COND_NE, flag, result, 0);
526 tcg_gen_trunc_i64_i32(cpu_ZF, flag);
527 tcg_gen_shri_i64(flag, result, 32);
528 tcg_gen_trunc_i64_i32(cpu_NF, flag);
529 tcg_temp_free_i64(flag);
532 /* Set NZCV as for a logical operation: NZ as per result, CV cleared. */
533 static inline void gen_logic_CC(int sf, TCGv_i64 result)
535 if (sf) {
536 gen_set_NZ64(result);
537 } else {
538 tcg_gen_trunc_i64_i32(cpu_ZF, result);
539 tcg_gen_trunc_i64_i32(cpu_NF, result);
541 tcg_gen_movi_i32(cpu_CF, 0);
542 tcg_gen_movi_i32(cpu_VF, 0);
545 /* dest = T0 + T1; compute C, N, V and Z flags */
546 static void gen_add_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
548 if (sf) {
549 TCGv_i64 result, flag, tmp;
550 result = tcg_temp_new_i64();
551 flag = tcg_temp_new_i64();
552 tmp = tcg_temp_new_i64();
554 tcg_gen_movi_i64(tmp, 0);
555 tcg_gen_add2_i64(result, flag, t0, tmp, t1, tmp);
557 tcg_gen_trunc_i64_i32(cpu_CF, flag);
559 gen_set_NZ64(result);
561 tcg_gen_xor_i64(flag, result, t0);
562 tcg_gen_xor_i64(tmp, t0, t1);
563 tcg_gen_andc_i64(flag, flag, tmp);
564 tcg_temp_free_i64(tmp);
565 tcg_gen_shri_i64(flag, flag, 32);
566 tcg_gen_trunc_i64_i32(cpu_VF, flag);
568 tcg_gen_mov_i64(dest, result);
569 tcg_temp_free_i64(result);
570 tcg_temp_free_i64(flag);
571 } else {
572 /* 32 bit arithmetic */
573 TCGv_i32 t0_32 = tcg_temp_new_i32();
574 TCGv_i32 t1_32 = tcg_temp_new_i32();
575 TCGv_i32 tmp = tcg_temp_new_i32();
577 tcg_gen_movi_i32(tmp, 0);
578 tcg_gen_trunc_i64_i32(t0_32, t0);
579 tcg_gen_trunc_i64_i32(t1_32, t1);
580 tcg_gen_add2_i32(cpu_NF, cpu_CF, t0_32, tmp, t1_32, tmp);
581 tcg_gen_mov_i32(cpu_ZF, cpu_NF);
582 tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
583 tcg_gen_xor_i32(tmp, t0_32, t1_32);
584 tcg_gen_andc_i32(cpu_VF, cpu_VF, tmp);
585 tcg_gen_extu_i32_i64(dest, cpu_NF);
587 tcg_temp_free_i32(tmp);
588 tcg_temp_free_i32(t0_32);
589 tcg_temp_free_i32(t1_32);
593 /* dest = T0 - T1; compute C, N, V and Z flags */
594 static void gen_sub_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
596 if (sf) {
597 /* 64 bit arithmetic */
598 TCGv_i64 result, flag, tmp;
600 result = tcg_temp_new_i64();
601 flag = tcg_temp_new_i64();
602 tcg_gen_sub_i64(result, t0, t1);
604 gen_set_NZ64(result);
606 tcg_gen_setcond_i64(TCG_COND_GEU, flag, t0, t1);
607 tcg_gen_trunc_i64_i32(cpu_CF, flag);
609 tcg_gen_xor_i64(flag, result, t0);
610 tmp = tcg_temp_new_i64();
611 tcg_gen_xor_i64(tmp, t0, t1);
612 tcg_gen_and_i64(flag, flag, tmp);
613 tcg_temp_free_i64(tmp);
614 tcg_gen_shri_i64(flag, flag, 32);
615 tcg_gen_trunc_i64_i32(cpu_VF, flag);
616 tcg_gen_mov_i64(dest, result);
617 tcg_temp_free_i64(flag);
618 tcg_temp_free_i64(result);
619 } else {
620 /* 32 bit arithmetic */
621 TCGv_i32 t0_32 = tcg_temp_new_i32();
622 TCGv_i32 t1_32 = tcg_temp_new_i32();
623 TCGv_i32 tmp;
625 tcg_gen_trunc_i64_i32(t0_32, t0);
626 tcg_gen_trunc_i64_i32(t1_32, t1);
627 tcg_gen_sub_i32(cpu_NF, t0_32, t1_32);
628 tcg_gen_mov_i32(cpu_ZF, cpu_NF);
629 tcg_gen_setcond_i32(TCG_COND_GEU, cpu_CF, t0_32, t1_32);
630 tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
631 tmp = tcg_temp_new_i32();
632 tcg_gen_xor_i32(tmp, t0_32, t1_32);
633 tcg_temp_free_i32(t0_32);
634 tcg_temp_free_i32(t1_32);
635 tcg_gen_and_i32(cpu_VF, cpu_VF, tmp);
636 tcg_temp_free_i32(tmp);
637 tcg_gen_extu_i32_i64(dest, cpu_NF);
641 /* dest = T0 + T1 + CF; do not compute flags. */
642 static void gen_adc(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
644 TCGv_i64 flag = tcg_temp_new_i64();
645 tcg_gen_extu_i32_i64(flag, cpu_CF);
646 tcg_gen_add_i64(dest, t0, t1);
647 tcg_gen_add_i64(dest, dest, flag);
648 tcg_temp_free_i64(flag);
650 if (!sf) {
651 tcg_gen_ext32u_i64(dest, dest);
655 /* dest = T0 + T1 + CF; compute C, N, V and Z flags. */
656 static void gen_adc_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
658 if (sf) {
659 TCGv_i64 result, cf_64, vf_64, tmp;
660 result = tcg_temp_new_i64();
661 cf_64 = tcg_temp_new_i64();
662 vf_64 = tcg_temp_new_i64();
663 tmp = tcg_const_i64(0);
665 tcg_gen_extu_i32_i64(cf_64, cpu_CF);
666 tcg_gen_add2_i64(result, cf_64, t0, tmp, cf_64, tmp);
667 tcg_gen_add2_i64(result, cf_64, result, cf_64, t1, tmp);
668 tcg_gen_trunc_i64_i32(cpu_CF, cf_64);
669 gen_set_NZ64(result);
671 tcg_gen_xor_i64(vf_64, result, t0);
672 tcg_gen_xor_i64(tmp, t0, t1);
673 tcg_gen_andc_i64(vf_64, vf_64, tmp);
674 tcg_gen_shri_i64(vf_64, vf_64, 32);
675 tcg_gen_trunc_i64_i32(cpu_VF, vf_64);
677 tcg_gen_mov_i64(dest, result);
679 tcg_temp_free_i64(tmp);
680 tcg_temp_free_i64(vf_64);
681 tcg_temp_free_i64(cf_64);
682 tcg_temp_free_i64(result);
683 } else {
684 TCGv_i32 t0_32, t1_32, tmp;
685 t0_32 = tcg_temp_new_i32();
686 t1_32 = tcg_temp_new_i32();
687 tmp = tcg_const_i32(0);
689 tcg_gen_trunc_i64_i32(t0_32, t0);
690 tcg_gen_trunc_i64_i32(t1_32, t1);
691 tcg_gen_add2_i32(cpu_NF, cpu_CF, t0_32, tmp, cpu_CF, tmp);
692 tcg_gen_add2_i32(cpu_NF, cpu_CF, cpu_NF, cpu_CF, t1_32, tmp);
694 tcg_gen_mov_i32(cpu_ZF, cpu_NF);
695 tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
696 tcg_gen_xor_i32(tmp, t0_32, t1_32);
697 tcg_gen_andc_i32(cpu_VF, cpu_VF, tmp);
698 tcg_gen_extu_i32_i64(dest, cpu_NF);
700 tcg_temp_free_i32(tmp);
701 tcg_temp_free_i32(t1_32);
702 tcg_temp_free_i32(t0_32);
707 * Load/Store generators
711 * Store from GPR register to memory.
713 static void do_gpr_st_memidx(DisasContext *s, TCGv_i64 source,
714 TCGv_i64 tcg_addr, int size, int memidx)
716 g_assert(size <= 3);
717 tcg_gen_qemu_st_i64(source, tcg_addr, memidx, MO_TE + size);
720 static void do_gpr_st(DisasContext *s, TCGv_i64 source,
721 TCGv_i64 tcg_addr, int size)
723 do_gpr_st_memidx(s, source, tcg_addr, size, get_mem_index(s));
727 * Load from memory to GPR register
729 static void do_gpr_ld_memidx(DisasContext *s, TCGv_i64 dest, TCGv_i64 tcg_addr,
730 int size, bool is_signed, bool extend, int memidx)
732 TCGMemOp memop = MO_TE + size;
734 g_assert(size <= 3);
736 if (is_signed) {
737 memop += MO_SIGN;
740 tcg_gen_qemu_ld_i64(dest, tcg_addr, memidx, memop);
742 if (extend && is_signed) {
743 g_assert(size < 3);
744 tcg_gen_ext32u_i64(dest, dest);
748 static void do_gpr_ld(DisasContext *s, TCGv_i64 dest, TCGv_i64 tcg_addr,
749 int size, bool is_signed, bool extend)
751 do_gpr_ld_memidx(s, dest, tcg_addr, size, is_signed, extend,
752 get_mem_index(s));
756 * Store from FP register to memory
758 static void do_fp_st(DisasContext *s, int srcidx, TCGv_i64 tcg_addr, int size)
760 /* This writes the bottom N bits of a 128 bit wide vector to memory */
761 TCGv_i64 tmp = tcg_temp_new_i64();
762 tcg_gen_ld_i64(tmp, cpu_env, fp_reg_offset(s, srcidx, MO_64));
763 if (size < 4) {
764 tcg_gen_qemu_st_i64(tmp, tcg_addr, get_mem_index(s), MO_TE + size);
765 } else {
766 TCGv_i64 tcg_hiaddr = tcg_temp_new_i64();
767 tcg_gen_qemu_st_i64(tmp, tcg_addr, get_mem_index(s), MO_TEQ);
768 tcg_gen_ld_i64(tmp, cpu_env, fp_reg_hi_offset(s, srcidx));
769 tcg_gen_addi_i64(tcg_hiaddr, tcg_addr, 8);
770 tcg_gen_qemu_st_i64(tmp, tcg_hiaddr, get_mem_index(s), MO_TEQ);
771 tcg_temp_free_i64(tcg_hiaddr);
774 tcg_temp_free_i64(tmp);
778 * Load from memory to FP register
780 static void do_fp_ld(DisasContext *s, int destidx, TCGv_i64 tcg_addr, int size)
782 /* This always zero-extends and writes to a full 128 bit wide vector */
783 TCGv_i64 tmplo = tcg_temp_new_i64();
784 TCGv_i64 tmphi;
786 if (size < 4) {
787 TCGMemOp memop = MO_TE + size;
788 tmphi = tcg_const_i64(0);
789 tcg_gen_qemu_ld_i64(tmplo, tcg_addr, get_mem_index(s), memop);
790 } else {
791 TCGv_i64 tcg_hiaddr;
792 tmphi = tcg_temp_new_i64();
793 tcg_hiaddr = tcg_temp_new_i64();
795 tcg_gen_qemu_ld_i64(tmplo, tcg_addr, get_mem_index(s), MO_TEQ);
796 tcg_gen_addi_i64(tcg_hiaddr, tcg_addr, 8);
797 tcg_gen_qemu_ld_i64(tmphi, tcg_hiaddr, get_mem_index(s), MO_TEQ);
798 tcg_temp_free_i64(tcg_hiaddr);
801 tcg_gen_st_i64(tmplo, cpu_env, fp_reg_offset(s, destidx, MO_64));
802 tcg_gen_st_i64(tmphi, cpu_env, fp_reg_hi_offset(s, destidx));
804 tcg_temp_free_i64(tmplo);
805 tcg_temp_free_i64(tmphi);
809 * Vector load/store helpers.
811 * The principal difference between this and a FP load is that we don't
812 * zero extend as we are filling a partial chunk of the vector register.
813 * These functions don't support 128 bit loads/stores, which would be
814 * normal load/store operations.
816 * The _i32 versions are useful when operating on 32 bit quantities
817 * (eg for floating point single or using Neon helper functions).
820 /* Get value of an element within a vector register */
821 static void read_vec_element(DisasContext *s, TCGv_i64 tcg_dest, int srcidx,
822 int element, TCGMemOp memop)
824 int vect_off = vec_reg_offset(s, srcidx, element, memop & MO_SIZE);
825 switch (memop) {
826 case MO_8:
827 tcg_gen_ld8u_i64(tcg_dest, cpu_env, vect_off);
828 break;
829 case MO_16:
830 tcg_gen_ld16u_i64(tcg_dest, cpu_env, vect_off);
831 break;
832 case MO_32:
833 tcg_gen_ld32u_i64(tcg_dest, cpu_env, vect_off);
834 break;
835 case MO_8|MO_SIGN:
836 tcg_gen_ld8s_i64(tcg_dest, cpu_env, vect_off);
837 break;
838 case MO_16|MO_SIGN:
839 tcg_gen_ld16s_i64(tcg_dest, cpu_env, vect_off);
840 break;
841 case MO_32|MO_SIGN:
842 tcg_gen_ld32s_i64(tcg_dest, cpu_env, vect_off);
843 break;
844 case MO_64:
845 case MO_64|MO_SIGN:
846 tcg_gen_ld_i64(tcg_dest, cpu_env, vect_off);
847 break;
848 default:
849 g_assert_not_reached();
853 static void read_vec_element_i32(DisasContext *s, TCGv_i32 tcg_dest, int srcidx,
854 int element, TCGMemOp memop)
856 int vect_off = vec_reg_offset(s, srcidx, element, memop & MO_SIZE);
857 switch (memop) {
858 case MO_8:
859 tcg_gen_ld8u_i32(tcg_dest, cpu_env, vect_off);
860 break;
861 case MO_16:
862 tcg_gen_ld16u_i32(tcg_dest, cpu_env, vect_off);
863 break;
864 case MO_8|MO_SIGN:
865 tcg_gen_ld8s_i32(tcg_dest, cpu_env, vect_off);
866 break;
867 case MO_16|MO_SIGN:
868 tcg_gen_ld16s_i32(tcg_dest, cpu_env, vect_off);
869 break;
870 case MO_32:
871 case MO_32|MO_SIGN:
872 tcg_gen_ld_i32(tcg_dest, cpu_env, vect_off);
873 break;
874 default:
875 g_assert_not_reached();
879 /* Set value of an element within a vector register */
880 static void write_vec_element(DisasContext *s, TCGv_i64 tcg_src, int destidx,
881 int element, TCGMemOp memop)
883 int vect_off = vec_reg_offset(s, destidx, element, memop & MO_SIZE);
884 switch (memop) {
885 case MO_8:
886 tcg_gen_st8_i64(tcg_src, cpu_env, vect_off);
887 break;
888 case MO_16:
889 tcg_gen_st16_i64(tcg_src, cpu_env, vect_off);
890 break;
891 case MO_32:
892 tcg_gen_st32_i64(tcg_src, cpu_env, vect_off);
893 break;
894 case MO_64:
895 tcg_gen_st_i64(tcg_src, cpu_env, vect_off);
896 break;
897 default:
898 g_assert_not_reached();
902 static void write_vec_element_i32(DisasContext *s, TCGv_i32 tcg_src,
903 int destidx, int element, TCGMemOp memop)
905 int vect_off = vec_reg_offset(s, destidx, element, memop & MO_SIZE);
906 switch (memop) {
907 case MO_8:
908 tcg_gen_st8_i32(tcg_src, cpu_env, vect_off);
909 break;
910 case MO_16:
911 tcg_gen_st16_i32(tcg_src, cpu_env, vect_off);
912 break;
913 case MO_32:
914 tcg_gen_st_i32(tcg_src, cpu_env, vect_off);
915 break;
916 default:
917 g_assert_not_reached();
921 /* Clear the high 64 bits of a 128 bit vector (in general non-quad
922 * vector ops all need to do this).
924 static void clear_vec_high(DisasContext *s, int rd)
926 TCGv_i64 tcg_zero = tcg_const_i64(0);
928 write_vec_element(s, tcg_zero, rd, 1, MO_64);
929 tcg_temp_free_i64(tcg_zero);
932 /* Store from vector register to memory */
933 static void do_vec_st(DisasContext *s, int srcidx, int element,
934 TCGv_i64 tcg_addr, int size)
936 TCGMemOp memop = MO_TE + size;
937 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
939 read_vec_element(s, tcg_tmp, srcidx, element, size);
940 tcg_gen_qemu_st_i64(tcg_tmp, tcg_addr, get_mem_index(s), memop);
942 tcg_temp_free_i64(tcg_tmp);
945 /* Load from memory to vector register */
946 static void do_vec_ld(DisasContext *s, int destidx, int element,
947 TCGv_i64 tcg_addr, int size)
949 TCGMemOp memop = MO_TE + size;
950 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
952 tcg_gen_qemu_ld_i64(tcg_tmp, tcg_addr, get_mem_index(s), memop);
953 write_vec_element(s, tcg_tmp, destidx, element, size);
955 tcg_temp_free_i64(tcg_tmp);
958 /* Check that FP/Neon access is enabled. If it is, return
959 * true. If not, emit code to generate an appropriate exception,
960 * and return false; the caller should not emit any code for
961 * the instruction. Note that this check must happen after all
962 * unallocated-encoding checks (otherwise the syndrome information
963 * for the resulting exception will be incorrect).
965 static inline bool fp_access_check(DisasContext *s)
967 assert(!s->fp_access_checked);
968 s->fp_access_checked = true;
970 if (s->cpacr_fpen) {
971 return true;
974 gen_exception_insn(s, 4, EXCP_UDEF, syn_fp_access_trap(1, 0xe, false));
975 return false;
979 * This utility function is for doing register extension with an
980 * optional shift. You will likely want to pass a temporary for the
981 * destination register. See DecodeRegExtend() in the ARM ARM.
983 static void ext_and_shift_reg(TCGv_i64 tcg_out, TCGv_i64 tcg_in,
984 int option, unsigned int shift)
986 int extsize = extract32(option, 0, 2);
987 bool is_signed = extract32(option, 2, 1);
989 if (is_signed) {
990 switch (extsize) {
991 case 0:
992 tcg_gen_ext8s_i64(tcg_out, tcg_in);
993 break;
994 case 1:
995 tcg_gen_ext16s_i64(tcg_out, tcg_in);
996 break;
997 case 2:
998 tcg_gen_ext32s_i64(tcg_out, tcg_in);
999 break;
1000 case 3:
1001 tcg_gen_mov_i64(tcg_out, tcg_in);
1002 break;
1004 } else {
1005 switch (extsize) {
1006 case 0:
1007 tcg_gen_ext8u_i64(tcg_out, tcg_in);
1008 break;
1009 case 1:
1010 tcg_gen_ext16u_i64(tcg_out, tcg_in);
1011 break;
1012 case 2:
1013 tcg_gen_ext32u_i64(tcg_out, tcg_in);
1014 break;
1015 case 3:
1016 tcg_gen_mov_i64(tcg_out, tcg_in);
1017 break;
1021 if (shift) {
1022 tcg_gen_shli_i64(tcg_out, tcg_out, shift);
1026 static inline void gen_check_sp_alignment(DisasContext *s)
1028 /* The AArch64 architecture mandates that (if enabled via PSTATE
1029 * or SCTLR bits) there is a check that SP is 16-aligned on every
1030 * SP-relative load or store (with an exception generated if it is not).
1031 * In line with general QEMU practice regarding misaligned accesses,
1032 * we omit these checks for the sake of guest program performance.
1033 * This function is provided as a hook so we can more easily add these
1034 * checks in future (possibly as a "favour catching guest program bugs
1035 * over speed" user selectable option).
1040 * This provides a simple table based table lookup decoder. It is
1041 * intended to be used when the relevant bits for decode are too
1042 * awkwardly placed and switch/if based logic would be confusing and
1043 * deeply nested. Since it's a linear search through the table, tables
1044 * should be kept small.
1046 * It returns the first handler where insn & mask == pattern, or
1047 * NULL if there is no match.
1048 * The table is terminated by an empty mask (i.e. 0)
1050 static inline AArch64DecodeFn *lookup_disas_fn(const AArch64DecodeTable *table,
1051 uint32_t insn)
1053 const AArch64DecodeTable *tptr = table;
1055 while (tptr->mask) {
1056 if ((insn & tptr->mask) == tptr->pattern) {
1057 return tptr->disas_fn;
1059 tptr++;
1061 return NULL;
1065 * the instruction disassembly implemented here matches
1066 * the instruction encoding classifications in chapter 3 (C3)
1067 * of the ARM Architecture Reference Manual (DDI0487A_a)
1070 /* C3.2.7 Unconditional branch (immediate)
1071 * 31 30 26 25 0
1072 * +----+-----------+-------------------------------------+
1073 * | op | 0 0 1 0 1 | imm26 |
1074 * +----+-----------+-------------------------------------+
1076 static void disas_uncond_b_imm(DisasContext *s, uint32_t insn)
1078 uint64_t addr = s->pc + sextract32(insn, 0, 26) * 4 - 4;
1080 if (insn & (1U << 31)) {
1081 /* C5.6.26 BL Branch with link */
1082 tcg_gen_movi_i64(cpu_reg(s, 30), s->pc);
1085 /* C5.6.20 B Branch / C5.6.26 BL Branch with link */
1086 gen_goto_tb(s, 0, addr);
1089 /* C3.2.1 Compare & branch (immediate)
1090 * 31 30 25 24 23 5 4 0
1091 * +----+-------------+----+---------------------+--------+
1092 * | sf | 0 1 1 0 1 0 | op | imm19 | Rt |
1093 * +----+-------------+----+---------------------+--------+
1095 static void disas_comp_b_imm(DisasContext *s, uint32_t insn)
1097 unsigned int sf, op, rt;
1098 uint64_t addr;
1099 int label_match;
1100 TCGv_i64 tcg_cmp;
1102 sf = extract32(insn, 31, 1);
1103 op = extract32(insn, 24, 1); /* 0: CBZ; 1: CBNZ */
1104 rt = extract32(insn, 0, 5);
1105 addr = s->pc + sextract32(insn, 5, 19) * 4 - 4;
1107 tcg_cmp = read_cpu_reg(s, rt, sf);
1108 label_match = gen_new_label();
1110 tcg_gen_brcondi_i64(op ? TCG_COND_NE : TCG_COND_EQ,
1111 tcg_cmp, 0, label_match);
1113 gen_goto_tb(s, 0, s->pc);
1114 gen_set_label(label_match);
1115 gen_goto_tb(s, 1, addr);
1118 /* C3.2.5 Test & branch (immediate)
1119 * 31 30 25 24 23 19 18 5 4 0
1120 * +----+-------------+----+-------+-------------+------+
1121 * | b5 | 0 1 1 0 1 1 | op | b40 | imm14 | Rt |
1122 * +----+-------------+----+-------+-------------+------+
1124 static void disas_test_b_imm(DisasContext *s, uint32_t insn)
1126 unsigned int bit_pos, op, rt;
1127 uint64_t addr;
1128 int label_match;
1129 TCGv_i64 tcg_cmp;
1131 bit_pos = (extract32(insn, 31, 1) << 5) | extract32(insn, 19, 5);
1132 op = extract32(insn, 24, 1); /* 0: TBZ; 1: TBNZ */
1133 addr = s->pc + sextract32(insn, 5, 14) * 4 - 4;
1134 rt = extract32(insn, 0, 5);
1136 tcg_cmp = tcg_temp_new_i64();
1137 tcg_gen_andi_i64(tcg_cmp, cpu_reg(s, rt), (1ULL << bit_pos));
1138 label_match = gen_new_label();
1139 tcg_gen_brcondi_i64(op ? TCG_COND_NE : TCG_COND_EQ,
1140 tcg_cmp, 0, label_match);
1141 tcg_temp_free_i64(tcg_cmp);
1142 gen_goto_tb(s, 0, s->pc);
1143 gen_set_label(label_match);
1144 gen_goto_tb(s, 1, addr);
1147 /* C3.2.2 / C5.6.19 Conditional branch (immediate)
1148 * 31 25 24 23 5 4 3 0
1149 * +---------------+----+---------------------+----+------+
1150 * | 0 1 0 1 0 1 0 | o1 | imm19 | o0 | cond |
1151 * +---------------+----+---------------------+----+------+
1153 static void disas_cond_b_imm(DisasContext *s, uint32_t insn)
1155 unsigned int cond;
1156 uint64_t addr;
1158 if ((insn & (1 << 4)) || (insn & (1 << 24))) {
1159 unallocated_encoding(s);
1160 return;
1162 addr = s->pc + sextract32(insn, 5, 19) * 4 - 4;
1163 cond = extract32(insn, 0, 4);
1165 if (cond < 0x0e) {
1166 /* genuinely conditional branches */
1167 int label_match = gen_new_label();
1168 arm_gen_test_cc(cond, label_match);
1169 gen_goto_tb(s, 0, s->pc);
1170 gen_set_label(label_match);
1171 gen_goto_tb(s, 1, addr);
1172 } else {
1173 /* 0xe and 0xf are both "always" conditions */
1174 gen_goto_tb(s, 0, addr);
1178 /* C5.6.68 HINT */
1179 static void handle_hint(DisasContext *s, uint32_t insn,
1180 unsigned int op1, unsigned int op2, unsigned int crm)
1182 unsigned int selector = crm << 3 | op2;
1184 if (op1 != 3) {
1185 unallocated_encoding(s);
1186 return;
1189 switch (selector) {
1190 case 0: /* NOP */
1191 return;
1192 case 3: /* WFI */
1193 s->is_jmp = DISAS_WFI;
1194 return;
1195 case 1: /* YIELD */
1196 case 2: /* WFE */
1197 s->is_jmp = DISAS_WFE;
1198 return;
1199 case 4: /* SEV */
1200 case 5: /* SEVL */
1201 /* we treat all as NOP at least for now */
1202 return;
1203 default:
1204 /* default specified as NOP equivalent */
1205 return;
1209 static void gen_clrex(DisasContext *s, uint32_t insn)
1211 tcg_gen_movi_i64(cpu_exclusive_addr, -1);
1214 /* CLREX, DSB, DMB, ISB */
1215 static void handle_sync(DisasContext *s, uint32_t insn,
1216 unsigned int op1, unsigned int op2, unsigned int crm)
1218 if (op1 != 3) {
1219 unallocated_encoding(s);
1220 return;
1223 switch (op2) {
1224 case 2: /* CLREX */
1225 gen_clrex(s, insn);
1226 return;
1227 case 4: /* DSB */
1228 case 5: /* DMB */
1229 case 6: /* ISB */
1230 /* We don't emulate caches so barriers are no-ops */
1231 return;
1232 default:
1233 unallocated_encoding(s);
1234 return;
1238 /* C5.6.130 MSR (immediate) - move immediate to processor state field */
1239 static void handle_msr_i(DisasContext *s, uint32_t insn,
1240 unsigned int op1, unsigned int op2, unsigned int crm)
1242 int op = op1 << 3 | op2;
1243 switch (op) {
1244 case 0x05: /* SPSel */
1245 if (s->current_el == 0) {
1246 unallocated_encoding(s);
1247 return;
1249 /* fall through */
1250 case 0x1e: /* DAIFSet */
1251 case 0x1f: /* DAIFClear */
1253 TCGv_i32 tcg_imm = tcg_const_i32(crm);
1254 TCGv_i32 tcg_op = tcg_const_i32(op);
1255 gen_a64_set_pc_im(s->pc - 4);
1256 gen_helper_msr_i_pstate(cpu_env, tcg_op, tcg_imm);
1257 tcg_temp_free_i32(tcg_imm);
1258 tcg_temp_free_i32(tcg_op);
1259 s->is_jmp = DISAS_UPDATE;
1260 break;
1262 default:
1263 unallocated_encoding(s);
1264 return;
1268 static void gen_get_nzcv(TCGv_i64 tcg_rt)
1270 TCGv_i32 tmp = tcg_temp_new_i32();
1271 TCGv_i32 nzcv = tcg_temp_new_i32();
1273 /* build bit 31, N */
1274 tcg_gen_andi_i32(nzcv, cpu_NF, (1U << 31));
1275 /* build bit 30, Z */
1276 tcg_gen_setcondi_i32(TCG_COND_EQ, tmp, cpu_ZF, 0);
1277 tcg_gen_deposit_i32(nzcv, nzcv, tmp, 30, 1);
1278 /* build bit 29, C */
1279 tcg_gen_deposit_i32(nzcv, nzcv, cpu_CF, 29, 1);
1280 /* build bit 28, V */
1281 tcg_gen_shri_i32(tmp, cpu_VF, 31);
1282 tcg_gen_deposit_i32(nzcv, nzcv, tmp, 28, 1);
1283 /* generate result */
1284 tcg_gen_extu_i32_i64(tcg_rt, nzcv);
1286 tcg_temp_free_i32(nzcv);
1287 tcg_temp_free_i32(tmp);
1290 static void gen_set_nzcv(TCGv_i64 tcg_rt)
1293 TCGv_i32 nzcv = tcg_temp_new_i32();
1295 /* take NZCV from R[t] */
1296 tcg_gen_trunc_i64_i32(nzcv, tcg_rt);
1298 /* bit 31, N */
1299 tcg_gen_andi_i32(cpu_NF, nzcv, (1U << 31));
1300 /* bit 30, Z */
1301 tcg_gen_andi_i32(cpu_ZF, nzcv, (1 << 30));
1302 tcg_gen_setcondi_i32(TCG_COND_EQ, cpu_ZF, cpu_ZF, 0);
1303 /* bit 29, C */
1304 tcg_gen_andi_i32(cpu_CF, nzcv, (1 << 29));
1305 tcg_gen_shri_i32(cpu_CF, cpu_CF, 29);
1306 /* bit 28, V */
1307 tcg_gen_andi_i32(cpu_VF, nzcv, (1 << 28));
1308 tcg_gen_shli_i32(cpu_VF, cpu_VF, 3);
1309 tcg_temp_free_i32(nzcv);
1312 /* C5.6.129 MRS - move from system register
1313 * C5.6.131 MSR (register) - move to system register
1314 * C5.6.204 SYS
1315 * C5.6.205 SYSL
1316 * These are all essentially the same insn in 'read' and 'write'
1317 * versions, with varying op0 fields.
1319 static void handle_sys(DisasContext *s, uint32_t insn, bool isread,
1320 unsigned int op0, unsigned int op1, unsigned int op2,
1321 unsigned int crn, unsigned int crm, unsigned int rt)
1323 const ARMCPRegInfo *ri;
1324 TCGv_i64 tcg_rt;
1326 ri = get_arm_cp_reginfo(s->cp_regs,
1327 ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP,
1328 crn, crm, op0, op1, op2));
1330 if (!ri) {
1331 /* Unknown register; this might be a guest error or a QEMU
1332 * unimplemented feature.
1334 qemu_log_mask(LOG_UNIMP, "%s access to unsupported AArch64 "
1335 "system register op0:%d op1:%d crn:%d crm:%d op2:%d\n",
1336 isread ? "read" : "write", op0, op1, crn, crm, op2);
1337 unallocated_encoding(s);
1338 return;
1341 /* Check access permissions */
1342 if (!cp_access_ok(s->current_el, ri, isread)) {
1343 unallocated_encoding(s);
1344 return;
1347 if (ri->accessfn) {
1348 /* Emit code to perform further access permissions checks at
1349 * runtime; this may result in an exception.
1351 TCGv_ptr tmpptr;
1352 TCGv_i32 tcg_syn;
1353 uint32_t syndrome;
1355 gen_a64_set_pc_im(s->pc - 4);
1356 tmpptr = tcg_const_ptr(ri);
1357 syndrome = syn_aa64_sysregtrap(op0, op1, op2, crn, crm, rt, isread);
1358 tcg_syn = tcg_const_i32(syndrome);
1359 gen_helper_access_check_cp_reg(cpu_env, tmpptr, tcg_syn);
1360 tcg_temp_free_ptr(tmpptr);
1361 tcg_temp_free_i32(tcg_syn);
1364 /* Handle special cases first */
1365 switch (ri->type & ~(ARM_CP_FLAG_MASK & ~ARM_CP_SPECIAL)) {
1366 case ARM_CP_NOP:
1367 return;
1368 case ARM_CP_NZCV:
1369 tcg_rt = cpu_reg(s, rt);
1370 if (isread) {
1371 gen_get_nzcv(tcg_rt);
1372 } else {
1373 gen_set_nzcv(tcg_rt);
1375 return;
1376 case ARM_CP_CURRENTEL:
1377 /* Reads as current EL value from pstate, which is
1378 * guaranteed to be constant by the tb flags.
1380 tcg_rt = cpu_reg(s, rt);
1381 tcg_gen_movi_i64(tcg_rt, s->current_el << 2);
1382 return;
1383 case ARM_CP_DC_ZVA:
1384 /* Writes clear the aligned block of memory which rt points into. */
1385 tcg_rt = cpu_reg(s, rt);
1386 gen_helper_dc_zva(cpu_env, tcg_rt);
1387 return;
1388 default:
1389 break;
1392 if ((s->tb->cflags & CF_USE_ICOUNT) && (ri->type & ARM_CP_IO)) {
1393 gen_io_start();
1396 tcg_rt = cpu_reg(s, rt);
1398 if (isread) {
1399 if (ri->type & ARM_CP_CONST) {
1400 tcg_gen_movi_i64(tcg_rt, ri->resetvalue);
1401 } else if (ri->readfn) {
1402 TCGv_ptr tmpptr;
1403 tmpptr = tcg_const_ptr(ri);
1404 gen_helper_get_cp_reg64(tcg_rt, cpu_env, tmpptr);
1405 tcg_temp_free_ptr(tmpptr);
1406 } else {
1407 tcg_gen_ld_i64(tcg_rt, cpu_env, ri->fieldoffset);
1409 } else {
1410 if (ri->type & ARM_CP_CONST) {
1411 /* If not forbidden by access permissions, treat as WI */
1412 return;
1413 } else if (ri->writefn) {
1414 TCGv_ptr tmpptr;
1415 tmpptr = tcg_const_ptr(ri);
1416 gen_helper_set_cp_reg64(cpu_env, tmpptr, tcg_rt);
1417 tcg_temp_free_ptr(tmpptr);
1418 } else {
1419 tcg_gen_st_i64(tcg_rt, cpu_env, ri->fieldoffset);
1423 if ((s->tb->cflags & CF_USE_ICOUNT) && (ri->type & ARM_CP_IO)) {
1424 /* I/O operations must end the TB here (whether read or write) */
1425 gen_io_end();
1426 s->is_jmp = DISAS_UPDATE;
1427 } else if (!isread && !(ri->type & ARM_CP_SUPPRESS_TB_END)) {
1428 /* We default to ending the TB on a coprocessor register write,
1429 * but allow this to be suppressed by the register definition
1430 * (usually only necessary to work around guest bugs).
1432 s->is_jmp = DISAS_UPDATE;
1436 /* C3.2.4 System
1437 * 31 22 21 20 19 18 16 15 12 11 8 7 5 4 0
1438 * +---------------------+---+-----+-----+-------+-------+-----+------+
1439 * | 1 1 0 1 0 1 0 1 0 0 | L | op0 | op1 | CRn | CRm | op2 | Rt |
1440 * +---------------------+---+-----+-----+-------+-------+-----+------+
1442 static void disas_system(DisasContext *s, uint32_t insn)
1444 unsigned int l, op0, op1, crn, crm, op2, rt;
1445 l = extract32(insn, 21, 1);
1446 op0 = extract32(insn, 19, 2);
1447 op1 = extract32(insn, 16, 3);
1448 crn = extract32(insn, 12, 4);
1449 crm = extract32(insn, 8, 4);
1450 op2 = extract32(insn, 5, 3);
1451 rt = extract32(insn, 0, 5);
1453 if (op0 == 0) {
1454 if (l || rt != 31) {
1455 unallocated_encoding(s);
1456 return;
1458 switch (crn) {
1459 case 2: /* C5.6.68 HINT */
1460 handle_hint(s, insn, op1, op2, crm);
1461 break;
1462 case 3: /* CLREX, DSB, DMB, ISB */
1463 handle_sync(s, insn, op1, op2, crm);
1464 break;
1465 case 4: /* C5.6.130 MSR (immediate) */
1466 handle_msr_i(s, insn, op1, op2, crm);
1467 break;
1468 default:
1469 unallocated_encoding(s);
1470 break;
1472 return;
1474 handle_sys(s, insn, l, op0, op1, op2, crn, crm, rt);
1477 /* C3.2.3 Exception generation
1479 * 31 24 23 21 20 5 4 2 1 0
1480 * +-----------------+-----+------------------------+-----+----+
1481 * | 1 1 0 1 0 1 0 0 | opc | imm16 | op2 | LL |
1482 * +-----------------------+------------------------+----------+
1484 static void disas_exc(DisasContext *s, uint32_t insn)
1486 int opc = extract32(insn, 21, 3);
1487 int op2_ll = extract32(insn, 0, 5);
1488 int imm16 = extract32(insn, 5, 16);
1489 TCGv_i32 tmp;
1491 switch (opc) {
1492 case 0:
1493 /* For SVC, HVC and SMC we advance the single-step state
1494 * machine before taking the exception. This is architecturally
1495 * mandated, to ensure that single-stepping a system call
1496 * instruction works properly.
1498 switch (op2_ll) {
1499 case 1:
1500 gen_ss_advance(s);
1501 gen_exception_insn(s, 0, EXCP_SWI, syn_aa64_svc(imm16));
1502 break;
1503 case 2:
1504 if (s->current_el == 0) {
1505 unallocated_encoding(s);
1506 break;
1508 /* The pre HVC helper handles cases when HVC gets trapped
1509 * as an undefined insn by runtime configuration.
1511 gen_a64_set_pc_im(s->pc - 4);
1512 gen_helper_pre_hvc(cpu_env);
1513 gen_ss_advance(s);
1514 gen_exception_insn(s, 0, EXCP_HVC, syn_aa64_hvc(imm16));
1515 break;
1516 case 3:
1517 if (s->current_el == 0) {
1518 unallocated_encoding(s);
1519 break;
1521 gen_a64_set_pc_im(s->pc - 4);
1522 tmp = tcg_const_i32(syn_aa64_smc(imm16));
1523 gen_helper_pre_smc(cpu_env, tmp);
1524 tcg_temp_free_i32(tmp);
1525 gen_ss_advance(s);
1526 gen_exception_insn(s, 0, EXCP_SMC, syn_aa64_smc(imm16));
1527 break;
1528 default:
1529 unallocated_encoding(s);
1530 break;
1532 break;
1533 case 1:
1534 if (op2_ll != 0) {
1535 unallocated_encoding(s);
1536 break;
1538 /* BRK */
1539 gen_exception_insn(s, 4, EXCP_BKPT, syn_aa64_bkpt(imm16));
1540 break;
1541 case 2:
1542 if (op2_ll != 0) {
1543 unallocated_encoding(s);
1544 break;
1546 /* HLT */
1547 unsupported_encoding(s, insn);
1548 break;
1549 case 5:
1550 if (op2_ll < 1 || op2_ll > 3) {
1551 unallocated_encoding(s);
1552 break;
1554 /* DCPS1, DCPS2, DCPS3 */
1555 unsupported_encoding(s, insn);
1556 break;
1557 default:
1558 unallocated_encoding(s);
1559 break;
1563 /* C3.2.7 Unconditional branch (register)
1564 * 31 25 24 21 20 16 15 10 9 5 4 0
1565 * +---------------+-------+-------+-------+------+-------+
1566 * | 1 1 0 1 0 1 1 | opc | op2 | op3 | Rn | op4 |
1567 * +---------------+-------+-------+-------+------+-------+
1569 static void disas_uncond_b_reg(DisasContext *s, uint32_t insn)
1571 unsigned int opc, op2, op3, rn, op4;
1573 opc = extract32(insn, 21, 4);
1574 op2 = extract32(insn, 16, 5);
1575 op3 = extract32(insn, 10, 6);
1576 rn = extract32(insn, 5, 5);
1577 op4 = extract32(insn, 0, 5);
1579 if (op4 != 0x0 || op3 != 0x0 || op2 != 0x1f) {
1580 unallocated_encoding(s);
1581 return;
1584 switch (opc) {
1585 case 0: /* BR */
1586 case 2: /* RET */
1587 tcg_gen_mov_i64(cpu_pc, cpu_reg(s, rn));
1588 break;
1589 case 1: /* BLR */
1590 tcg_gen_mov_i64(cpu_pc, cpu_reg(s, rn));
1591 tcg_gen_movi_i64(cpu_reg(s, 30), s->pc);
1592 break;
1593 case 4: /* ERET */
1594 if (s->current_el == 0) {
1595 unallocated_encoding(s);
1596 return;
1598 gen_helper_exception_return(cpu_env);
1599 s->is_jmp = DISAS_JUMP;
1600 return;
1601 case 5: /* DRPS */
1602 if (rn != 0x1f) {
1603 unallocated_encoding(s);
1604 } else {
1605 unsupported_encoding(s, insn);
1607 return;
1608 default:
1609 unallocated_encoding(s);
1610 return;
1613 s->is_jmp = DISAS_JUMP;
1616 /* C3.2 Branches, exception generating and system instructions */
1617 static void disas_b_exc_sys(DisasContext *s, uint32_t insn)
1619 switch (extract32(insn, 25, 7)) {
1620 case 0x0a: case 0x0b:
1621 case 0x4a: case 0x4b: /* Unconditional branch (immediate) */
1622 disas_uncond_b_imm(s, insn);
1623 break;
1624 case 0x1a: case 0x5a: /* Compare & branch (immediate) */
1625 disas_comp_b_imm(s, insn);
1626 break;
1627 case 0x1b: case 0x5b: /* Test & branch (immediate) */
1628 disas_test_b_imm(s, insn);
1629 break;
1630 case 0x2a: /* Conditional branch (immediate) */
1631 disas_cond_b_imm(s, insn);
1632 break;
1633 case 0x6a: /* Exception generation / System */
1634 if (insn & (1 << 24)) {
1635 disas_system(s, insn);
1636 } else {
1637 disas_exc(s, insn);
1639 break;
1640 case 0x6b: /* Unconditional branch (register) */
1641 disas_uncond_b_reg(s, insn);
1642 break;
1643 default:
1644 unallocated_encoding(s);
1645 break;
1650 * Load/Store exclusive instructions are implemented by remembering
1651 * the value/address loaded, and seeing if these are the same
1652 * when the store is performed. This is not actually the architecturally
1653 * mandated semantics, but it works for typical guest code sequences
1654 * and avoids having to monitor regular stores.
1656 * In system emulation mode only one CPU will be running at once, so
1657 * this sequence is effectively atomic. In user emulation mode we
1658 * throw an exception and handle the atomic operation elsewhere.
1660 static void gen_load_exclusive(DisasContext *s, int rt, int rt2,
1661 TCGv_i64 addr, int size, bool is_pair)
1663 TCGv_i64 tmp = tcg_temp_new_i64();
1664 TCGMemOp memop = MO_TE + size;
1666 g_assert(size <= 3);
1667 tcg_gen_qemu_ld_i64(tmp, addr, get_mem_index(s), memop);
1669 if (is_pair) {
1670 TCGv_i64 addr2 = tcg_temp_new_i64();
1671 TCGv_i64 hitmp = tcg_temp_new_i64();
1673 g_assert(size >= 2);
1674 tcg_gen_addi_i64(addr2, addr, 1 << size);
1675 tcg_gen_qemu_ld_i64(hitmp, addr2, get_mem_index(s), memop);
1676 tcg_temp_free_i64(addr2);
1677 tcg_gen_mov_i64(cpu_exclusive_high, hitmp);
1678 tcg_gen_mov_i64(cpu_reg(s, rt2), hitmp);
1679 tcg_temp_free_i64(hitmp);
1682 tcg_gen_mov_i64(cpu_exclusive_val, tmp);
1683 tcg_gen_mov_i64(cpu_reg(s, rt), tmp);
1685 tcg_temp_free_i64(tmp);
1686 tcg_gen_mov_i64(cpu_exclusive_addr, addr);
1689 #ifdef CONFIG_USER_ONLY
1690 static void gen_store_exclusive(DisasContext *s, int rd, int rt, int rt2,
1691 TCGv_i64 addr, int size, int is_pair)
1693 tcg_gen_mov_i64(cpu_exclusive_test, addr);
1694 tcg_gen_movi_i32(cpu_exclusive_info,
1695 size | is_pair << 2 | (rd << 4) | (rt << 9) | (rt2 << 14));
1696 gen_exception_internal_insn(s, 4, EXCP_STREX);
1698 #else
1699 static void gen_store_exclusive(DisasContext *s, int rd, int rt, int rt2,
1700 TCGv_i64 inaddr, int size, int is_pair)
1702 /* if (env->exclusive_addr == addr && env->exclusive_val == [addr]
1703 * && (!is_pair || env->exclusive_high == [addr + datasize])) {
1704 * [addr] = {Rt};
1705 * if (is_pair) {
1706 * [addr + datasize] = {Rt2};
1708 * {Rd} = 0;
1709 * } else {
1710 * {Rd} = 1;
1712 * env->exclusive_addr = -1;
1714 int fail_label = gen_new_label();
1715 int done_label = gen_new_label();
1716 TCGv_i64 addr = tcg_temp_local_new_i64();
1717 TCGv_i64 tmp;
1719 /* Copy input into a local temp so it is not trashed when the
1720 * basic block ends at the branch insn.
1722 tcg_gen_mov_i64(addr, inaddr);
1723 tcg_gen_brcond_i64(TCG_COND_NE, addr, cpu_exclusive_addr, fail_label);
1725 tmp = tcg_temp_new_i64();
1726 tcg_gen_qemu_ld_i64(tmp, addr, get_mem_index(s), MO_TE + size);
1727 tcg_gen_brcond_i64(TCG_COND_NE, tmp, cpu_exclusive_val, fail_label);
1728 tcg_temp_free_i64(tmp);
1730 if (is_pair) {
1731 TCGv_i64 addrhi = tcg_temp_new_i64();
1732 TCGv_i64 tmphi = tcg_temp_new_i64();
1734 tcg_gen_addi_i64(addrhi, addr, 1 << size);
1735 tcg_gen_qemu_ld_i64(tmphi, addrhi, get_mem_index(s), MO_TE + size);
1736 tcg_gen_brcond_i64(TCG_COND_NE, tmphi, cpu_exclusive_high, fail_label);
1738 tcg_temp_free_i64(tmphi);
1739 tcg_temp_free_i64(addrhi);
1742 /* We seem to still have the exclusive monitor, so do the store */
1743 tcg_gen_qemu_st_i64(cpu_reg(s, rt), addr, get_mem_index(s), MO_TE + size);
1744 if (is_pair) {
1745 TCGv_i64 addrhi = tcg_temp_new_i64();
1747 tcg_gen_addi_i64(addrhi, addr, 1 << size);
1748 tcg_gen_qemu_st_i64(cpu_reg(s, rt2), addrhi,
1749 get_mem_index(s), MO_TE + size);
1750 tcg_temp_free_i64(addrhi);
1753 tcg_temp_free_i64(addr);
1755 tcg_gen_movi_i64(cpu_reg(s, rd), 0);
1756 tcg_gen_br(done_label);
1757 gen_set_label(fail_label);
1758 tcg_gen_movi_i64(cpu_reg(s, rd), 1);
1759 gen_set_label(done_label);
1760 tcg_gen_movi_i64(cpu_exclusive_addr, -1);
1763 #endif
1765 /* C3.3.6 Load/store exclusive
1767 * 31 30 29 24 23 22 21 20 16 15 14 10 9 5 4 0
1768 * +-----+-------------+----+---+----+------+----+-------+------+------+
1769 * | sz | 0 0 1 0 0 0 | o2 | L | o1 | Rs | o0 | Rt2 | Rn | Rt |
1770 * +-----+-------------+----+---+----+------+----+-------+------+------+
1772 * sz: 00 -> 8 bit, 01 -> 16 bit, 10 -> 32 bit, 11 -> 64 bit
1773 * L: 0 -> store, 1 -> load
1774 * o2: 0 -> exclusive, 1 -> not
1775 * o1: 0 -> single register, 1 -> register pair
1776 * o0: 1 -> load-acquire/store-release, 0 -> not
1778 * o0 == 0 AND o2 == 1 is un-allocated
1779 * o1 == 1 is un-allocated except for 32 and 64 bit sizes
1781 static void disas_ldst_excl(DisasContext *s, uint32_t insn)
1783 int rt = extract32(insn, 0, 5);
1784 int rn = extract32(insn, 5, 5);
1785 int rt2 = extract32(insn, 10, 5);
1786 int is_lasr = extract32(insn, 15, 1);
1787 int rs = extract32(insn, 16, 5);
1788 int is_pair = extract32(insn, 21, 1);
1789 int is_store = !extract32(insn, 22, 1);
1790 int is_excl = !extract32(insn, 23, 1);
1791 int size = extract32(insn, 30, 2);
1792 TCGv_i64 tcg_addr;
1794 if ((!is_excl && !is_lasr) ||
1795 (is_pair && size < 2)) {
1796 unallocated_encoding(s);
1797 return;
1800 if (rn == 31) {
1801 gen_check_sp_alignment(s);
1803 tcg_addr = read_cpu_reg_sp(s, rn, 1);
1805 /* Note that since TCG is single threaded load-acquire/store-release
1806 * semantics require no extra if (is_lasr) { ... } handling.
1809 if (is_excl) {
1810 if (!is_store) {
1811 s->is_ldex = true;
1812 gen_load_exclusive(s, rt, rt2, tcg_addr, size, is_pair);
1813 } else {
1814 gen_store_exclusive(s, rs, rt, rt2, tcg_addr, size, is_pair);
1816 } else {
1817 TCGv_i64 tcg_rt = cpu_reg(s, rt);
1818 if (is_store) {
1819 do_gpr_st(s, tcg_rt, tcg_addr, size);
1820 } else {
1821 do_gpr_ld(s, tcg_rt, tcg_addr, size, false, false);
1823 if (is_pair) {
1824 TCGv_i64 tcg_rt2 = cpu_reg(s, rt);
1825 tcg_gen_addi_i64(tcg_addr, tcg_addr, 1 << size);
1826 if (is_store) {
1827 do_gpr_st(s, tcg_rt2, tcg_addr, size);
1828 } else {
1829 do_gpr_ld(s, tcg_rt2, tcg_addr, size, false, false);
1836 * C3.3.5 Load register (literal)
1838 * 31 30 29 27 26 25 24 23 5 4 0
1839 * +-----+-------+---+-----+-------------------+-------+
1840 * | opc | 0 1 1 | V | 0 0 | imm19 | Rt |
1841 * +-----+-------+---+-----+-------------------+-------+
1843 * V: 1 -> vector (simd/fp)
1844 * opc (non-vector): 00 -> 32 bit, 01 -> 64 bit,
1845 * 10-> 32 bit signed, 11 -> prefetch
1846 * opc (vector): 00 -> 32 bit, 01 -> 64 bit, 10 -> 128 bit (11 unallocated)
1848 static void disas_ld_lit(DisasContext *s, uint32_t insn)
1850 int rt = extract32(insn, 0, 5);
1851 int64_t imm = sextract32(insn, 5, 19) << 2;
1852 bool is_vector = extract32(insn, 26, 1);
1853 int opc = extract32(insn, 30, 2);
1854 bool is_signed = false;
1855 int size = 2;
1856 TCGv_i64 tcg_rt, tcg_addr;
1858 if (is_vector) {
1859 if (opc == 3) {
1860 unallocated_encoding(s);
1861 return;
1863 size = 2 + opc;
1864 if (!fp_access_check(s)) {
1865 return;
1867 } else {
1868 if (opc == 3) {
1869 /* PRFM (literal) : prefetch */
1870 return;
1872 size = 2 + extract32(opc, 0, 1);
1873 is_signed = extract32(opc, 1, 1);
1876 tcg_rt = cpu_reg(s, rt);
1878 tcg_addr = tcg_const_i64((s->pc - 4) + imm);
1879 if (is_vector) {
1880 do_fp_ld(s, rt, tcg_addr, size);
1881 } else {
1882 do_gpr_ld(s, tcg_rt, tcg_addr, size, is_signed, false);
1884 tcg_temp_free_i64(tcg_addr);
1888 * C5.6.80 LDNP (Load Pair - non-temporal hint)
1889 * C5.6.81 LDP (Load Pair - non vector)
1890 * C5.6.82 LDPSW (Load Pair Signed Word - non vector)
1891 * C5.6.176 STNP (Store Pair - non-temporal hint)
1892 * C5.6.177 STP (Store Pair - non vector)
1893 * C6.3.165 LDNP (Load Pair of SIMD&FP - non-temporal hint)
1894 * C6.3.165 LDP (Load Pair of SIMD&FP)
1895 * C6.3.284 STNP (Store Pair of SIMD&FP - non-temporal hint)
1896 * C6.3.284 STP (Store Pair of SIMD&FP)
1898 * 31 30 29 27 26 25 24 23 22 21 15 14 10 9 5 4 0
1899 * +-----+-------+---+---+-------+---+-----------------------------+
1900 * | opc | 1 0 1 | V | 0 | index | L | imm7 | Rt2 | Rn | Rt |
1901 * +-----+-------+---+---+-------+---+-------+-------+------+------+
1903 * opc: LDP/STP/LDNP/STNP 00 -> 32 bit, 10 -> 64 bit
1904 * LDPSW 01
1905 * LDP/STP/LDNP/STNP (SIMD) 00 -> 32 bit, 01 -> 64 bit, 10 -> 128 bit
1906 * V: 0 -> GPR, 1 -> Vector
1907 * idx: 00 -> signed offset with non-temporal hint, 01 -> post-index,
1908 * 10 -> signed offset, 11 -> pre-index
1909 * L: 0 -> Store 1 -> Load
1911 * Rt, Rt2 = GPR or SIMD registers to be stored
1912 * Rn = general purpose register containing address
1913 * imm7 = signed offset (multiple of 4 or 8 depending on size)
1915 static void disas_ldst_pair(DisasContext *s, uint32_t insn)
1917 int rt = extract32(insn, 0, 5);
1918 int rn = extract32(insn, 5, 5);
1919 int rt2 = extract32(insn, 10, 5);
1920 uint64_t offset = sextract64(insn, 15, 7);
1921 int index = extract32(insn, 23, 2);
1922 bool is_vector = extract32(insn, 26, 1);
1923 bool is_load = extract32(insn, 22, 1);
1924 int opc = extract32(insn, 30, 2);
1926 bool is_signed = false;
1927 bool postindex = false;
1928 bool wback = false;
1930 TCGv_i64 tcg_addr; /* calculated address */
1931 int size;
1933 if (opc == 3) {
1934 unallocated_encoding(s);
1935 return;
1938 if (is_vector) {
1939 size = 2 + opc;
1940 } else {
1941 size = 2 + extract32(opc, 1, 1);
1942 is_signed = extract32(opc, 0, 1);
1943 if (!is_load && is_signed) {
1944 unallocated_encoding(s);
1945 return;
1949 switch (index) {
1950 case 1: /* post-index */
1951 postindex = true;
1952 wback = true;
1953 break;
1954 case 0:
1955 /* signed offset with "non-temporal" hint. Since we don't emulate
1956 * caches we don't care about hints to the cache system about
1957 * data access patterns, and handle this identically to plain
1958 * signed offset.
1960 if (is_signed) {
1961 /* There is no non-temporal-hint version of LDPSW */
1962 unallocated_encoding(s);
1963 return;
1965 postindex = false;
1966 break;
1967 case 2: /* signed offset, rn not updated */
1968 postindex = false;
1969 break;
1970 case 3: /* pre-index */
1971 postindex = false;
1972 wback = true;
1973 break;
1976 if (is_vector && !fp_access_check(s)) {
1977 return;
1980 offset <<= size;
1982 if (rn == 31) {
1983 gen_check_sp_alignment(s);
1986 tcg_addr = read_cpu_reg_sp(s, rn, 1);
1988 if (!postindex) {
1989 tcg_gen_addi_i64(tcg_addr, tcg_addr, offset);
1992 if (is_vector) {
1993 if (is_load) {
1994 do_fp_ld(s, rt, tcg_addr, size);
1995 } else {
1996 do_fp_st(s, rt, tcg_addr, size);
1998 } else {
1999 TCGv_i64 tcg_rt = cpu_reg(s, rt);
2000 if (is_load) {
2001 do_gpr_ld(s, tcg_rt, tcg_addr, size, is_signed, false);
2002 } else {
2003 do_gpr_st(s, tcg_rt, tcg_addr, size);
2006 tcg_gen_addi_i64(tcg_addr, tcg_addr, 1 << size);
2007 if (is_vector) {
2008 if (is_load) {
2009 do_fp_ld(s, rt2, tcg_addr, size);
2010 } else {
2011 do_fp_st(s, rt2, tcg_addr, size);
2013 } else {
2014 TCGv_i64 tcg_rt2 = cpu_reg(s, rt2);
2015 if (is_load) {
2016 do_gpr_ld(s, tcg_rt2, tcg_addr, size, is_signed, false);
2017 } else {
2018 do_gpr_st(s, tcg_rt2, tcg_addr, size);
2022 if (wback) {
2023 if (postindex) {
2024 tcg_gen_addi_i64(tcg_addr, tcg_addr, offset - (1 << size));
2025 } else {
2026 tcg_gen_subi_i64(tcg_addr, tcg_addr, 1 << size);
2028 tcg_gen_mov_i64(cpu_reg_sp(s, rn), tcg_addr);
2033 * C3.3.8 Load/store (immediate post-indexed)
2034 * C3.3.9 Load/store (immediate pre-indexed)
2035 * C3.3.12 Load/store (unscaled immediate)
2037 * 31 30 29 27 26 25 24 23 22 21 20 12 11 10 9 5 4 0
2038 * +----+-------+---+-----+-----+---+--------+-----+------+------+
2039 * |size| 1 1 1 | V | 0 0 | opc | 0 | imm9 | idx | Rn | Rt |
2040 * +----+-------+---+-----+-----+---+--------+-----+------+------+
2042 * idx = 01 -> post-indexed, 11 pre-indexed, 00 unscaled imm. (no writeback)
2043 10 -> unprivileged
2044 * V = 0 -> non-vector
2045 * size: 00 -> 8 bit, 01 -> 16 bit, 10 -> 32 bit, 11 -> 64bit
2046 * opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
2048 static void disas_ldst_reg_imm9(DisasContext *s, uint32_t insn)
2050 int rt = extract32(insn, 0, 5);
2051 int rn = extract32(insn, 5, 5);
2052 int imm9 = sextract32(insn, 12, 9);
2053 int opc = extract32(insn, 22, 2);
2054 int size = extract32(insn, 30, 2);
2055 int idx = extract32(insn, 10, 2);
2056 bool is_signed = false;
2057 bool is_store = false;
2058 bool is_extended = false;
2059 bool is_unpriv = (idx == 2);
2060 bool is_vector = extract32(insn, 26, 1);
2061 bool post_index;
2062 bool writeback;
2064 TCGv_i64 tcg_addr;
2066 if (is_vector) {
2067 size |= (opc & 2) << 1;
2068 if (size > 4 || is_unpriv) {
2069 unallocated_encoding(s);
2070 return;
2072 is_store = ((opc & 1) == 0);
2073 if (!fp_access_check(s)) {
2074 return;
2076 } else {
2077 if (size == 3 && opc == 2) {
2078 /* PRFM - prefetch */
2079 if (is_unpriv) {
2080 unallocated_encoding(s);
2081 return;
2083 return;
2085 if (opc == 3 && size > 1) {
2086 unallocated_encoding(s);
2087 return;
2089 is_store = (opc == 0);
2090 is_signed = opc & (1<<1);
2091 is_extended = (size < 3) && (opc & 1);
2094 switch (idx) {
2095 case 0:
2096 case 2:
2097 post_index = false;
2098 writeback = false;
2099 break;
2100 case 1:
2101 post_index = true;
2102 writeback = true;
2103 break;
2104 case 3:
2105 post_index = false;
2106 writeback = true;
2107 break;
2110 if (rn == 31) {
2111 gen_check_sp_alignment(s);
2113 tcg_addr = read_cpu_reg_sp(s, rn, 1);
2115 if (!post_index) {
2116 tcg_gen_addi_i64(tcg_addr, tcg_addr, imm9);
2119 if (is_vector) {
2120 if (is_store) {
2121 do_fp_st(s, rt, tcg_addr, size);
2122 } else {
2123 do_fp_ld(s, rt, tcg_addr, size);
2125 } else {
2126 TCGv_i64 tcg_rt = cpu_reg(s, rt);
2127 int memidx = is_unpriv ? get_a64_user_mem_index(s) : get_mem_index(s);
2129 if (is_store) {
2130 do_gpr_st_memidx(s, tcg_rt, tcg_addr, size, memidx);
2131 } else {
2132 do_gpr_ld_memidx(s, tcg_rt, tcg_addr, size,
2133 is_signed, is_extended, memidx);
2137 if (writeback) {
2138 TCGv_i64 tcg_rn = cpu_reg_sp(s, rn);
2139 if (post_index) {
2140 tcg_gen_addi_i64(tcg_addr, tcg_addr, imm9);
2142 tcg_gen_mov_i64(tcg_rn, tcg_addr);
2147 * C3.3.10 Load/store (register offset)
2149 * 31 30 29 27 26 25 24 23 22 21 20 16 15 13 12 11 10 9 5 4 0
2150 * +----+-------+---+-----+-----+---+------+-----+--+-----+----+----+
2151 * |size| 1 1 1 | V | 0 0 | opc | 1 | Rm | opt | S| 1 0 | Rn | Rt |
2152 * +----+-------+---+-----+-----+---+------+-----+--+-----+----+----+
2154 * For non-vector:
2155 * size: 00-> byte, 01 -> 16 bit, 10 -> 32bit, 11 -> 64bit
2156 * opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
2157 * For vector:
2158 * size is opc<1>:size<1:0> so 100 -> 128 bit; 110 and 111 unallocated
2159 * opc<0>: 0 -> store, 1 -> load
2160 * V: 1 -> vector/simd
2161 * opt: extend encoding (see DecodeRegExtend)
2162 * S: if S=1 then scale (essentially index by sizeof(size))
2163 * Rt: register to transfer into/out of
2164 * Rn: address register or SP for base
2165 * Rm: offset register or ZR for offset
2167 static void disas_ldst_reg_roffset(DisasContext *s, uint32_t insn)
2169 int rt = extract32(insn, 0, 5);
2170 int rn = extract32(insn, 5, 5);
2171 int shift = extract32(insn, 12, 1);
2172 int rm = extract32(insn, 16, 5);
2173 int opc = extract32(insn, 22, 2);
2174 int opt = extract32(insn, 13, 3);
2175 int size = extract32(insn, 30, 2);
2176 bool is_signed = false;
2177 bool is_store = false;
2178 bool is_extended = false;
2179 bool is_vector = extract32(insn, 26, 1);
2181 TCGv_i64 tcg_rm;
2182 TCGv_i64 tcg_addr;
2184 if (extract32(opt, 1, 1) == 0) {
2185 unallocated_encoding(s);
2186 return;
2189 if (is_vector) {
2190 size |= (opc & 2) << 1;
2191 if (size > 4) {
2192 unallocated_encoding(s);
2193 return;
2195 is_store = !extract32(opc, 0, 1);
2196 if (!fp_access_check(s)) {
2197 return;
2199 } else {
2200 if (size == 3 && opc == 2) {
2201 /* PRFM - prefetch */
2202 return;
2204 if (opc == 3 && size > 1) {
2205 unallocated_encoding(s);
2206 return;
2208 is_store = (opc == 0);
2209 is_signed = extract32(opc, 1, 1);
2210 is_extended = (size < 3) && extract32(opc, 0, 1);
2213 if (rn == 31) {
2214 gen_check_sp_alignment(s);
2216 tcg_addr = read_cpu_reg_sp(s, rn, 1);
2218 tcg_rm = read_cpu_reg(s, rm, 1);
2219 ext_and_shift_reg(tcg_rm, tcg_rm, opt, shift ? size : 0);
2221 tcg_gen_add_i64(tcg_addr, tcg_addr, tcg_rm);
2223 if (is_vector) {
2224 if (is_store) {
2225 do_fp_st(s, rt, tcg_addr, size);
2226 } else {
2227 do_fp_ld(s, rt, tcg_addr, size);
2229 } else {
2230 TCGv_i64 tcg_rt = cpu_reg(s, rt);
2231 if (is_store) {
2232 do_gpr_st(s, tcg_rt, tcg_addr, size);
2233 } else {
2234 do_gpr_ld(s, tcg_rt, tcg_addr, size, is_signed, is_extended);
2240 * C3.3.13 Load/store (unsigned immediate)
2242 * 31 30 29 27 26 25 24 23 22 21 10 9 5
2243 * +----+-------+---+-----+-----+------------+-------+------+
2244 * |size| 1 1 1 | V | 0 1 | opc | imm12 | Rn | Rt |
2245 * +----+-------+---+-----+-----+------------+-------+------+
2247 * For non-vector:
2248 * size: 00-> byte, 01 -> 16 bit, 10 -> 32bit, 11 -> 64bit
2249 * opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
2250 * For vector:
2251 * size is opc<1>:size<1:0> so 100 -> 128 bit; 110 and 111 unallocated
2252 * opc<0>: 0 -> store, 1 -> load
2253 * Rn: base address register (inc SP)
2254 * Rt: target register
2256 static void disas_ldst_reg_unsigned_imm(DisasContext *s, uint32_t insn)
2258 int rt = extract32(insn, 0, 5);
2259 int rn = extract32(insn, 5, 5);
2260 unsigned int imm12 = extract32(insn, 10, 12);
2261 bool is_vector = extract32(insn, 26, 1);
2262 int size = extract32(insn, 30, 2);
2263 int opc = extract32(insn, 22, 2);
2264 unsigned int offset;
2266 TCGv_i64 tcg_addr;
2268 bool is_store;
2269 bool is_signed = false;
2270 bool is_extended = false;
2272 if (is_vector) {
2273 size |= (opc & 2) << 1;
2274 if (size > 4) {
2275 unallocated_encoding(s);
2276 return;
2278 is_store = !extract32(opc, 0, 1);
2279 if (!fp_access_check(s)) {
2280 return;
2282 } else {
2283 if (size == 3 && opc == 2) {
2284 /* PRFM - prefetch */
2285 return;
2287 if (opc == 3 && size > 1) {
2288 unallocated_encoding(s);
2289 return;
2291 is_store = (opc == 0);
2292 is_signed = extract32(opc, 1, 1);
2293 is_extended = (size < 3) && extract32(opc, 0, 1);
2296 if (rn == 31) {
2297 gen_check_sp_alignment(s);
2299 tcg_addr = read_cpu_reg_sp(s, rn, 1);
2300 offset = imm12 << size;
2301 tcg_gen_addi_i64(tcg_addr, tcg_addr, offset);
2303 if (is_vector) {
2304 if (is_store) {
2305 do_fp_st(s, rt, tcg_addr, size);
2306 } else {
2307 do_fp_ld(s, rt, tcg_addr, size);
2309 } else {
2310 TCGv_i64 tcg_rt = cpu_reg(s, rt);
2311 if (is_store) {
2312 do_gpr_st(s, tcg_rt, tcg_addr, size);
2313 } else {
2314 do_gpr_ld(s, tcg_rt, tcg_addr, size, is_signed, is_extended);
2319 /* Load/store register (all forms) */
2320 static void disas_ldst_reg(DisasContext *s, uint32_t insn)
2322 switch (extract32(insn, 24, 2)) {
2323 case 0:
2324 if (extract32(insn, 21, 1) == 1 && extract32(insn, 10, 2) == 2) {
2325 disas_ldst_reg_roffset(s, insn);
2326 } else {
2327 /* Load/store register (unscaled immediate)
2328 * Load/store immediate pre/post-indexed
2329 * Load/store register unprivileged
2331 disas_ldst_reg_imm9(s, insn);
2333 break;
2334 case 1:
2335 disas_ldst_reg_unsigned_imm(s, insn);
2336 break;
2337 default:
2338 unallocated_encoding(s);
2339 break;
2343 /* C3.3.1 AdvSIMD load/store multiple structures
2345 * 31 30 29 23 22 21 16 15 12 11 10 9 5 4 0
2346 * +---+---+---------------+---+-------------+--------+------+------+------+
2347 * | 0 | Q | 0 0 1 1 0 0 0 | L | 0 0 0 0 0 0 | opcode | size | Rn | Rt |
2348 * +---+---+---------------+---+-------------+--------+------+------+------+
2350 * C3.3.2 AdvSIMD load/store multiple structures (post-indexed)
2352 * 31 30 29 23 22 21 20 16 15 12 11 10 9 5 4 0
2353 * +---+---+---------------+---+---+---------+--------+------+------+------+
2354 * | 0 | Q | 0 0 1 1 0 0 1 | L | 0 | Rm | opcode | size | Rn | Rt |
2355 * +---+---+---------------+---+---+---------+--------+------+------+------+
2357 * Rt: first (or only) SIMD&FP register to be transferred
2358 * Rn: base address or SP
2359 * Rm (post-index only): post-index register (when !31) or size dependent #imm
2361 static void disas_ldst_multiple_struct(DisasContext *s, uint32_t insn)
2363 int rt = extract32(insn, 0, 5);
2364 int rn = extract32(insn, 5, 5);
2365 int size = extract32(insn, 10, 2);
2366 int opcode = extract32(insn, 12, 4);
2367 bool is_store = !extract32(insn, 22, 1);
2368 bool is_postidx = extract32(insn, 23, 1);
2369 bool is_q = extract32(insn, 30, 1);
2370 TCGv_i64 tcg_addr, tcg_rn;
2372 int ebytes = 1 << size;
2373 int elements = (is_q ? 128 : 64) / (8 << size);
2374 int rpt; /* num iterations */
2375 int selem; /* structure elements */
2376 int r;
2378 if (extract32(insn, 31, 1) || extract32(insn, 21, 1)) {
2379 unallocated_encoding(s);
2380 return;
2383 /* From the shared decode logic */
2384 switch (opcode) {
2385 case 0x0:
2386 rpt = 1;
2387 selem = 4;
2388 break;
2389 case 0x2:
2390 rpt = 4;
2391 selem = 1;
2392 break;
2393 case 0x4:
2394 rpt = 1;
2395 selem = 3;
2396 break;
2397 case 0x6:
2398 rpt = 3;
2399 selem = 1;
2400 break;
2401 case 0x7:
2402 rpt = 1;
2403 selem = 1;
2404 break;
2405 case 0x8:
2406 rpt = 1;
2407 selem = 2;
2408 break;
2409 case 0xa:
2410 rpt = 2;
2411 selem = 1;
2412 break;
2413 default:
2414 unallocated_encoding(s);
2415 return;
2418 if (size == 3 && !is_q && selem != 1) {
2419 /* reserved */
2420 unallocated_encoding(s);
2421 return;
2424 if (!fp_access_check(s)) {
2425 return;
2428 if (rn == 31) {
2429 gen_check_sp_alignment(s);
2432 tcg_rn = cpu_reg_sp(s, rn);
2433 tcg_addr = tcg_temp_new_i64();
2434 tcg_gen_mov_i64(tcg_addr, tcg_rn);
2436 for (r = 0; r < rpt; r++) {
2437 int e;
2438 for (e = 0; e < elements; e++) {
2439 int tt = (rt + r) % 32;
2440 int xs;
2441 for (xs = 0; xs < selem; xs++) {
2442 if (is_store) {
2443 do_vec_st(s, tt, e, tcg_addr, size);
2444 } else {
2445 do_vec_ld(s, tt, e, tcg_addr, size);
2447 /* For non-quad operations, setting a slice of the low
2448 * 64 bits of the register clears the high 64 bits (in
2449 * the ARM ARM pseudocode this is implicit in the fact
2450 * that 'rval' is a 64 bit wide variable). We optimize
2451 * by noticing that we only need to do this the first
2452 * time we touch a register.
2454 if (!is_q && e == 0 && (r == 0 || xs == selem - 1)) {
2455 clear_vec_high(s, tt);
2458 tcg_gen_addi_i64(tcg_addr, tcg_addr, ebytes);
2459 tt = (tt + 1) % 32;
2464 if (is_postidx) {
2465 int rm = extract32(insn, 16, 5);
2466 if (rm == 31) {
2467 tcg_gen_mov_i64(tcg_rn, tcg_addr);
2468 } else {
2469 tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, rm));
2472 tcg_temp_free_i64(tcg_addr);
2475 /* C3.3.3 AdvSIMD load/store single structure
2477 * 31 30 29 23 22 21 20 16 15 13 12 11 10 9 5 4 0
2478 * +---+---+---------------+-----+-----------+-----+---+------+------+------+
2479 * | 0 | Q | 0 0 1 1 0 1 0 | L R | 0 0 0 0 0 | opc | S | size | Rn | Rt |
2480 * +---+---+---------------+-----+-----------+-----+---+------+------+------+
2482 * C3.3.4 AdvSIMD load/store single structure (post-indexed)
2484 * 31 30 29 23 22 21 20 16 15 13 12 11 10 9 5 4 0
2485 * +---+---+---------------+-----+-----------+-----+---+------+------+------+
2486 * | 0 | Q | 0 0 1 1 0 1 1 | L R | Rm | opc | S | size | Rn | Rt |
2487 * +---+---+---------------+-----+-----------+-----+---+------+------+------+
2489 * Rt: first (or only) SIMD&FP register to be transferred
2490 * Rn: base address or SP
2491 * Rm (post-index only): post-index register (when !31) or size dependent #imm
2492 * index = encoded in Q:S:size dependent on size
2494 * lane_size = encoded in R, opc
2495 * transfer width = encoded in opc, S, size
2497 static void disas_ldst_single_struct(DisasContext *s, uint32_t insn)
2499 int rt = extract32(insn, 0, 5);
2500 int rn = extract32(insn, 5, 5);
2501 int size = extract32(insn, 10, 2);
2502 int S = extract32(insn, 12, 1);
2503 int opc = extract32(insn, 13, 3);
2504 int R = extract32(insn, 21, 1);
2505 int is_load = extract32(insn, 22, 1);
2506 int is_postidx = extract32(insn, 23, 1);
2507 int is_q = extract32(insn, 30, 1);
2509 int scale = extract32(opc, 1, 2);
2510 int selem = (extract32(opc, 0, 1) << 1 | R) + 1;
2511 bool replicate = false;
2512 int index = is_q << 3 | S << 2 | size;
2513 int ebytes, xs;
2514 TCGv_i64 tcg_addr, tcg_rn;
2516 switch (scale) {
2517 case 3:
2518 if (!is_load || S) {
2519 unallocated_encoding(s);
2520 return;
2522 scale = size;
2523 replicate = true;
2524 break;
2525 case 0:
2526 break;
2527 case 1:
2528 if (extract32(size, 0, 1)) {
2529 unallocated_encoding(s);
2530 return;
2532 index >>= 1;
2533 break;
2534 case 2:
2535 if (extract32(size, 1, 1)) {
2536 unallocated_encoding(s);
2537 return;
2539 if (!extract32(size, 0, 1)) {
2540 index >>= 2;
2541 } else {
2542 if (S) {
2543 unallocated_encoding(s);
2544 return;
2546 index >>= 3;
2547 scale = 3;
2549 break;
2550 default:
2551 g_assert_not_reached();
2554 if (!fp_access_check(s)) {
2555 return;
2558 ebytes = 1 << scale;
2560 if (rn == 31) {
2561 gen_check_sp_alignment(s);
2564 tcg_rn = cpu_reg_sp(s, rn);
2565 tcg_addr = tcg_temp_new_i64();
2566 tcg_gen_mov_i64(tcg_addr, tcg_rn);
2568 for (xs = 0; xs < selem; xs++) {
2569 if (replicate) {
2570 /* Load and replicate to all elements */
2571 uint64_t mulconst;
2572 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
2574 tcg_gen_qemu_ld_i64(tcg_tmp, tcg_addr,
2575 get_mem_index(s), MO_TE + scale);
2576 switch (scale) {
2577 case 0:
2578 mulconst = 0x0101010101010101ULL;
2579 break;
2580 case 1:
2581 mulconst = 0x0001000100010001ULL;
2582 break;
2583 case 2:
2584 mulconst = 0x0000000100000001ULL;
2585 break;
2586 case 3:
2587 mulconst = 0;
2588 break;
2589 default:
2590 g_assert_not_reached();
2592 if (mulconst) {
2593 tcg_gen_muli_i64(tcg_tmp, tcg_tmp, mulconst);
2595 write_vec_element(s, tcg_tmp, rt, 0, MO_64);
2596 if (is_q) {
2597 write_vec_element(s, tcg_tmp, rt, 1, MO_64);
2598 } else {
2599 clear_vec_high(s, rt);
2601 tcg_temp_free_i64(tcg_tmp);
2602 } else {
2603 /* Load/store one element per register */
2604 if (is_load) {
2605 do_vec_ld(s, rt, index, tcg_addr, MO_TE + scale);
2606 } else {
2607 do_vec_st(s, rt, index, tcg_addr, MO_TE + scale);
2610 tcg_gen_addi_i64(tcg_addr, tcg_addr, ebytes);
2611 rt = (rt + 1) % 32;
2614 if (is_postidx) {
2615 int rm = extract32(insn, 16, 5);
2616 if (rm == 31) {
2617 tcg_gen_mov_i64(tcg_rn, tcg_addr);
2618 } else {
2619 tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, rm));
2622 tcg_temp_free_i64(tcg_addr);
2625 /* C3.3 Loads and stores */
2626 static void disas_ldst(DisasContext *s, uint32_t insn)
2628 switch (extract32(insn, 24, 6)) {
2629 case 0x08: /* Load/store exclusive */
2630 disas_ldst_excl(s, insn);
2631 break;
2632 case 0x18: case 0x1c: /* Load register (literal) */
2633 disas_ld_lit(s, insn);
2634 break;
2635 case 0x28: case 0x29:
2636 case 0x2c: case 0x2d: /* Load/store pair (all forms) */
2637 disas_ldst_pair(s, insn);
2638 break;
2639 case 0x38: case 0x39:
2640 case 0x3c: case 0x3d: /* Load/store register (all forms) */
2641 disas_ldst_reg(s, insn);
2642 break;
2643 case 0x0c: /* AdvSIMD load/store multiple structures */
2644 disas_ldst_multiple_struct(s, insn);
2645 break;
2646 case 0x0d: /* AdvSIMD load/store single structure */
2647 disas_ldst_single_struct(s, insn);
2648 break;
2649 default:
2650 unallocated_encoding(s);
2651 break;
2655 /* C3.4.6 PC-rel. addressing
2656 * 31 30 29 28 24 23 5 4 0
2657 * +----+-------+-----------+-------------------+------+
2658 * | op | immlo | 1 0 0 0 0 | immhi | Rd |
2659 * +----+-------+-----------+-------------------+------+
2661 static void disas_pc_rel_adr(DisasContext *s, uint32_t insn)
2663 unsigned int page, rd;
2664 uint64_t base;
2665 uint64_t offset;
2667 page = extract32(insn, 31, 1);
2668 /* SignExtend(immhi:immlo) -> offset */
2669 offset = sextract64(insn, 5, 19);
2670 offset = offset << 2 | extract32(insn, 29, 2);
2671 rd = extract32(insn, 0, 5);
2672 base = s->pc - 4;
2674 if (page) {
2675 /* ADRP (page based) */
2676 base &= ~0xfff;
2677 offset <<= 12;
2680 tcg_gen_movi_i64(cpu_reg(s, rd), base + offset);
2684 * C3.4.1 Add/subtract (immediate)
2686 * 31 30 29 28 24 23 22 21 10 9 5 4 0
2687 * +--+--+--+-----------+-----+-------------+-----+-----+
2688 * |sf|op| S| 1 0 0 0 1 |shift| imm12 | Rn | Rd |
2689 * +--+--+--+-----------+-----+-------------+-----+-----+
2691 * sf: 0 -> 32bit, 1 -> 64bit
2692 * op: 0 -> add , 1 -> sub
2693 * S: 1 -> set flags
2694 * shift: 00 -> LSL imm by 0, 01 -> LSL imm by 12
2696 static void disas_add_sub_imm(DisasContext *s, uint32_t insn)
2698 int rd = extract32(insn, 0, 5);
2699 int rn = extract32(insn, 5, 5);
2700 uint64_t imm = extract32(insn, 10, 12);
2701 int shift = extract32(insn, 22, 2);
2702 bool setflags = extract32(insn, 29, 1);
2703 bool sub_op = extract32(insn, 30, 1);
2704 bool is_64bit = extract32(insn, 31, 1);
2706 TCGv_i64 tcg_rn = cpu_reg_sp(s, rn);
2707 TCGv_i64 tcg_rd = setflags ? cpu_reg(s, rd) : cpu_reg_sp(s, rd);
2708 TCGv_i64 tcg_result;
2710 switch (shift) {
2711 case 0x0:
2712 break;
2713 case 0x1:
2714 imm <<= 12;
2715 break;
2716 default:
2717 unallocated_encoding(s);
2718 return;
2721 tcg_result = tcg_temp_new_i64();
2722 if (!setflags) {
2723 if (sub_op) {
2724 tcg_gen_subi_i64(tcg_result, tcg_rn, imm);
2725 } else {
2726 tcg_gen_addi_i64(tcg_result, tcg_rn, imm);
2728 } else {
2729 TCGv_i64 tcg_imm = tcg_const_i64(imm);
2730 if (sub_op) {
2731 gen_sub_CC(is_64bit, tcg_result, tcg_rn, tcg_imm);
2732 } else {
2733 gen_add_CC(is_64bit, tcg_result, tcg_rn, tcg_imm);
2735 tcg_temp_free_i64(tcg_imm);
2738 if (is_64bit) {
2739 tcg_gen_mov_i64(tcg_rd, tcg_result);
2740 } else {
2741 tcg_gen_ext32u_i64(tcg_rd, tcg_result);
2744 tcg_temp_free_i64(tcg_result);
2747 /* The input should be a value in the bottom e bits (with higher
2748 * bits zero); returns that value replicated into every element
2749 * of size e in a 64 bit integer.
2751 static uint64_t bitfield_replicate(uint64_t mask, unsigned int e)
2753 assert(e != 0);
2754 while (e < 64) {
2755 mask |= mask << e;
2756 e *= 2;
2758 return mask;
2761 /* Return a value with the bottom len bits set (where 0 < len <= 64) */
2762 static inline uint64_t bitmask64(unsigned int length)
2764 assert(length > 0 && length <= 64);
2765 return ~0ULL >> (64 - length);
2768 /* Simplified variant of pseudocode DecodeBitMasks() for the case where we
2769 * only require the wmask. Returns false if the imms/immr/immn are a reserved
2770 * value (ie should cause a guest UNDEF exception), and true if they are
2771 * valid, in which case the decoded bit pattern is written to result.
2773 static bool logic_imm_decode_wmask(uint64_t *result, unsigned int immn,
2774 unsigned int imms, unsigned int immr)
2776 uint64_t mask;
2777 unsigned e, levels, s, r;
2778 int len;
2780 assert(immn < 2 && imms < 64 && immr < 64);
2782 /* The bit patterns we create here are 64 bit patterns which
2783 * are vectors of identical elements of size e = 2, 4, 8, 16, 32 or
2784 * 64 bits each. Each element contains the same value: a run
2785 * of between 1 and e-1 non-zero bits, rotated within the
2786 * element by between 0 and e-1 bits.
2788 * The element size and run length are encoded into immn (1 bit)
2789 * and imms (6 bits) as follows:
2790 * 64 bit elements: immn = 1, imms = <length of run - 1>
2791 * 32 bit elements: immn = 0, imms = 0 : <length of run - 1>
2792 * 16 bit elements: immn = 0, imms = 10 : <length of run - 1>
2793 * 8 bit elements: immn = 0, imms = 110 : <length of run - 1>
2794 * 4 bit elements: immn = 0, imms = 1110 : <length of run - 1>
2795 * 2 bit elements: immn = 0, imms = 11110 : <length of run - 1>
2796 * Notice that immn = 0, imms = 11111x is the only combination
2797 * not covered by one of the above options; this is reserved.
2798 * Further, <length of run - 1> all-ones is a reserved pattern.
2800 * In all cases the rotation is by immr % e (and immr is 6 bits).
2803 /* First determine the element size */
2804 len = 31 - clz32((immn << 6) | (~imms & 0x3f));
2805 if (len < 1) {
2806 /* This is the immn == 0, imms == 0x11111x case */
2807 return false;
2809 e = 1 << len;
2811 levels = e - 1;
2812 s = imms & levels;
2813 r = immr & levels;
2815 if (s == levels) {
2816 /* <length of run - 1> mustn't be all-ones. */
2817 return false;
2820 /* Create the value of one element: s+1 set bits rotated
2821 * by r within the element (which is e bits wide)...
2823 mask = bitmask64(s + 1);
2824 if (r) {
2825 mask = (mask >> r) | (mask << (e - r));
2826 mask &= bitmask64(e);
2828 /* ...then replicate the element over the whole 64 bit value */
2829 mask = bitfield_replicate(mask, e);
2830 *result = mask;
2831 return true;
2834 /* C3.4.4 Logical (immediate)
2835 * 31 30 29 28 23 22 21 16 15 10 9 5 4 0
2836 * +----+-----+-------------+---+------+------+------+------+
2837 * | sf | opc | 1 0 0 1 0 0 | N | immr | imms | Rn | Rd |
2838 * +----+-----+-------------+---+------+------+------+------+
2840 static void disas_logic_imm(DisasContext *s, uint32_t insn)
2842 unsigned int sf, opc, is_n, immr, imms, rn, rd;
2843 TCGv_i64 tcg_rd, tcg_rn;
2844 uint64_t wmask;
2845 bool is_and = false;
2847 sf = extract32(insn, 31, 1);
2848 opc = extract32(insn, 29, 2);
2849 is_n = extract32(insn, 22, 1);
2850 immr = extract32(insn, 16, 6);
2851 imms = extract32(insn, 10, 6);
2852 rn = extract32(insn, 5, 5);
2853 rd = extract32(insn, 0, 5);
2855 if (!sf && is_n) {
2856 unallocated_encoding(s);
2857 return;
2860 if (opc == 0x3) { /* ANDS */
2861 tcg_rd = cpu_reg(s, rd);
2862 } else {
2863 tcg_rd = cpu_reg_sp(s, rd);
2865 tcg_rn = cpu_reg(s, rn);
2867 if (!logic_imm_decode_wmask(&wmask, is_n, imms, immr)) {
2868 /* some immediate field values are reserved */
2869 unallocated_encoding(s);
2870 return;
2873 if (!sf) {
2874 wmask &= 0xffffffff;
2877 switch (opc) {
2878 case 0x3: /* ANDS */
2879 case 0x0: /* AND */
2880 tcg_gen_andi_i64(tcg_rd, tcg_rn, wmask);
2881 is_and = true;
2882 break;
2883 case 0x1: /* ORR */
2884 tcg_gen_ori_i64(tcg_rd, tcg_rn, wmask);
2885 break;
2886 case 0x2: /* EOR */
2887 tcg_gen_xori_i64(tcg_rd, tcg_rn, wmask);
2888 break;
2889 default:
2890 assert(FALSE); /* must handle all above */
2891 break;
2894 if (!sf && !is_and) {
2895 /* zero extend final result; we know we can skip this for AND
2896 * since the immediate had the high 32 bits clear.
2898 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
2901 if (opc == 3) { /* ANDS */
2902 gen_logic_CC(sf, tcg_rd);
2907 * C3.4.5 Move wide (immediate)
2909 * 31 30 29 28 23 22 21 20 5 4 0
2910 * +--+-----+-------------+-----+----------------+------+
2911 * |sf| opc | 1 0 0 1 0 1 | hw | imm16 | Rd |
2912 * +--+-----+-------------+-----+----------------+------+
2914 * sf: 0 -> 32 bit, 1 -> 64 bit
2915 * opc: 00 -> N, 10 -> Z, 11 -> K
2916 * hw: shift/16 (0,16, and sf only 32, 48)
2918 static void disas_movw_imm(DisasContext *s, uint32_t insn)
2920 int rd = extract32(insn, 0, 5);
2921 uint64_t imm = extract32(insn, 5, 16);
2922 int sf = extract32(insn, 31, 1);
2923 int opc = extract32(insn, 29, 2);
2924 int pos = extract32(insn, 21, 2) << 4;
2925 TCGv_i64 tcg_rd = cpu_reg(s, rd);
2926 TCGv_i64 tcg_imm;
2928 if (!sf && (pos >= 32)) {
2929 unallocated_encoding(s);
2930 return;
2933 switch (opc) {
2934 case 0: /* MOVN */
2935 case 2: /* MOVZ */
2936 imm <<= pos;
2937 if (opc == 0) {
2938 imm = ~imm;
2940 if (!sf) {
2941 imm &= 0xffffffffu;
2943 tcg_gen_movi_i64(tcg_rd, imm);
2944 break;
2945 case 3: /* MOVK */
2946 tcg_imm = tcg_const_i64(imm);
2947 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_imm, pos, 16);
2948 tcg_temp_free_i64(tcg_imm);
2949 if (!sf) {
2950 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
2952 break;
2953 default:
2954 unallocated_encoding(s);
2955 break;
2959 /* C3.4.2 Bitfield
2960 * 31 30 29 28 23 22 21 16 15 10 9 5 4 0
2961 * +----+-----+-------------+---+------+------+------+------+
2962 * | sf | opc | 1 0 0 1 1 0 | N | immr | imms | Rn | Rd |
2963 * +----+-----+-------------+---+------+------+------+------+
2965 static void disas_bitfield(DisasContext *s, uint32_t insn)
2967 unsigned int sf, n, opc, ri, si, rn, rd, bitsize, pos, len;
2968 TCGv_i64 tcg_rd, tcg_tmp;
2970 sf = extract32(insn, 31, 1);
2971 opc = extract32(insn, 29, 2);
2972 n = extract32(insn, 22, 1);
2973 ri = extract32(insn, 16, 6);
2974 si = extract32(insn, 10, 6);
2975 rn = extract32(insn, 5, 5);
2976 rd = extract32(insn, 0, 5);
2977 bitsize = sf ? 64 : 32;
2979 if (sf != n || ri >= bitsize || si >= bitsize || opc > 2) {
2980 unallocated_encoding(s);
2981 return;
2984 tcg_rd = cpu_reg(s, rd);
2985 tcg_tmp = read_cpu_reg(s, rn, sf);
2987 /* OPTME: probably worth recognizing common cases of ext{8,16,32}{u,s} */
2989 if (opc != 1) { /* SBFM or UBFM */
2990 tcg_gen_movi_i64(tcg_rd, 0);
2993 /* do the bit move operation */
2994 if (si >= ri) {
2995 /* Wd<s-r:0> = Wn<s:r> */
2996 tcg_gen_shri_i64(tcg_tmp, tcg_tmp, ri);
2997 pos = 0;
2998 len = (si - ri) + 1;
2999 } else {
3000 /* Wd<32+s-r,32-r> = Wn<s:0> */
3001 pos = bitsize - ri;
3002 len = si + 1;
3005 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, pos, len);
3007 if (opc == 0) { /* SBFM - sign extend the destination field */
3008 tcg_gen_shli_i64(tcg_rd, tcg_rd, 64 - (pos + len));
3009 tcg_gen_sari_i64(tcg_rd, tcg_rd, 64 - (pos + len));
3012 if (!sf) { /* zero extend final result */
3013 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3017 /* C3.4.3 Extract
3018 * 31 30 29 28 23 22 21 20 16 15 10 9 5 4 0
3019 * +----+------+-------------+---+----+------+--------+------+------+
3020 * | sf | op21 | 1 0 0 1 1 1 | N | o0 | Rm | imms | Rn | Rd |
3021 * +----+------+-------------+---+----+------+--------+------+------+
3023 static void disas_extract(DisasContext *s, uint32_t insn)
3025 unsigned int sf, n, rm, imm, rn, rd, bitsize, op21, op0;
3027 sf = extract32(insn, 31, 1);
3028 n = extract32(insn, 22, 1);
3029 rm = extract32(insn, 16, 5);
3030 imm = extract32(insn, 10, 6);
3031 rn = extract32(insn, 5, 5);
3032 rd = extract32(insn, 0, 5);
3033 op21 = extract32(insn, 29, 2);
3034 op0 = extract32(insn, 21, 1);
3035 bitsize = sf ? 64 : 32;
3037 if (sf != n || op21 || op0 || imm >= bitsize) {
3038 unallocated_encoding(s);
3039 } else {
3040 TCGv_i64 tcg_rd, tcg_rm, tcg_rn;
3042 tcg_rd = cpu_reg(s, rd);
3044 if (imm) {
3045 /* OPTME: we can special case rm==rn as a rotate */
3046 tcg_rm = read_cpu_reg(s, rm, sf);
3047 tcg_rn = read_cpu_reg(s, rn, sf);
3048 tcg_gen_shri_i64(tcg_rm, tcg_rm, imm);
3049 tcg_gen_shli_i64(tcg_rn, tcg_rn, bitsize - imm);
3050 tcg_gen_or_i64(tcg_rd, tcg_rm, tcg_rn);
3051 if (!sf) {
3052 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3054 } else {
3055 /* tcg shl_i32/shl_i64 is undefined for 32/64 bit shifts,
3056 * so an extract from bit 0 is a special case.
3058 if (sf) {
3059 tcg_gen_mov_i64(tcg_rd, cpu_reg(s, rm));
3060 } else {
3061 tcg_gen_ext32u_i64(tcg_rd, cpu_reg(s, rm));
3068 /* C3.4 Data processing - immediate */
3069 static void disas_data_proc_imm(DisasContext *s, uint32_t insn)
3071 switch (extract32(insn, 23, 6)) {
3072 case 0x20: case 0x21: /* PC-rel. addressing */
3073 disas_pc_rel_adr(s, insn);
3074 break;
3075 case 0x22: case 0x23: /* Add/subtract (immediate) */
3076 disas_add_sub_imm(s, insn);
3077 break;
3078 case 0x24: /* Logical (immediate) */
3079 disas_logic_imm(s, insn);
3080 break;
3081 case 0x25: /* Move wide (immediate) */
3082 disas_movw_imm(s, insn);
3083 break;
3084 case 0x26: /* Bitfield */
3085 disas_bitfield(s, insn);
3086 break;
3087 case 0x27: /* Extract */
3088 disas_extract(s, insn);
3089 break;
3090 default:
3091 unallocated_encoding(s);
3092 break;
3096 /* Shift a TCGv src by TCGv shift_amount, put result in dst.
3097 * Note that it is the caller's responsibility to ensure that the
3098 * shift amount is in range (ie 0..31 or 0..63) and provide the ARM
3099 * mandated semantics for out of range shifts.
3101 static void shift_reg(TCGv_i64 dst, TCGv_i64 src, int sf,
3102 enum a64_shift_type shift_type, TCGv_i64 shift_amount)
3104 switch (shift_type) {
3105 case A64_SHIFT_TYPE_LSL:
3106 tcg_gen_shl_i64(dst, src, shift_amount);
3107 break;
3108 case A64_SHIFT_TYPE_LSR:
3109 tcg_gen_shr_i64(dst, src, shift_amount);
3110 break;
3111 case A64_SHIFT_TYPE_ASR:
3112 if (!sf) {
3113 tcg_gen_ext32s_i64(dst, src);
3115 tcg_gen_sar_i64(dst, sf ? src : dst, shift_amount);
3116 break;
3117 case A64_SHIFT_TYPE_ROR:
3118 if (sf) {
3119 tcg_gen_rotr_i64(dst, src, shift_amount);
3120 } else {
3121 TCGv_i32 t0, t1;
3122 t0 = tcg_temp_new_i32();
3123 t1 = tcg_temp_new_i32();
3124 tcg_gen_trunc_i64_i32(t0, src);
3125 tcg_gen_trunc_i64_i32(t1, shift_amount);
3126 tcg_gen_rotr_i32(t0, t0, t1);
3127 tcg_gen_extu_i32_i64(dst, t0);
3128 tcg_temp_free_i32(t0);
3129 tcg_temp_free_i32(t1);
3131 break;
3132 default:
3133 assert(FALSE); /* all shift types should be handled */
3134 break;
3137 if (!sf) { /* zero extend final result */
3138 tcg_gen_ext32u_i64(dst, dst);
3142 /* Shift a TCGv src by immediate, put result in dst.
3143 * The shift amount must be in range (this should always be true as the
3144 * relevant instructions will UNDEF on bad shift immediates).
3146 static void shift_reg_imm(TCGv_i64 dst, TCGv_i64 src, int sf,
3147 enum a64_shift_type shift_type, unsigned int shift_i)
3149 assert(shift_i < (sf ? 64 : 32));
3151 if (shift_i == 0) {
3152 tcg_gen_mov_i64(dst, src);
3153 } else {
3154 TCGv_i64 shift_const;
3156 shift_const = tcg_const_i64(shift_i);
3157 shift_reg(dst, src, sf, shift_type, shift_const);
3158 tcg_temp_free_i64(shift_const);
3162 /* C3.5.10 Logical (shifted register)
3163 * 31 30 29 28 24 23 22 21 20 16 15 10 9 5 4 0
3164 * +----+-----+-----------+-------+---+------+--------+------+------+
3165 * | sf | opc | 0 1 0 1 0 | shift | N | Rm | imm6 | Rn | Rd |
3166 * +----+-----+-----------+-------+---+------+--------+------+------+
3168 static void disas_logic_reg(DisasContext *s, uint32_t insn)
3170 TCGv_i64 tcg_rd, tcg_rn, tcg_rm;
3171 unsigned int sf, opc, shift_type, invert, rm, shift_amount, rn, rd;
3173 sf = extract32(insn, 31, 1);
3174 opc = extract32(insn, 29, 2);
3175 shift_type = extract32(insn, 22, 2);
3176 invert = extract32(insn, 21, 1);
3177 rm = extract32(insn, 16, 5);
3178 shift_amount = extract32(insn, 10, 6);
3179 rn = extract32(insn, 5, 5);
3180 rd = extract32(insn, 0, 5);
3182 if (!sf && (shift_amount & (1 << 5))) {
3183 unallocated_encoding(s);
3184 return;
3187 tcg_rd = cpu_reg(s, rd);
3189 if (opc == 1 && shift_amount == 0 && shift_type == 0 && rn == 31) {
3190 /* Unshifted ORR and ORN with WZR/XZR is the standard encoding for
3191 * register-register MOV and MVN, so it is worth special casing.
3193 tcg_rm = cpu_reg(s, rm);
3194 if (invert) {
3195 tcg_gen_not_i64(tcg_rd, tcg_rm);
3196 if (!sf) {
3197 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3199 } else {
3200 if (sf) {
3201 tcg_gen_mov_i64(tcg_rd, tcg_rm);
3202 } else {
3203 tcg_gen_ext32u_i64(tcg_rd, tcg_rm);
3206 return;
3209 tcg_rm = read_cpu_reg(s, rm, sf);
3211 if (shift_amount) {
3212 shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, shift_amount);
3215 tcg_rn = cpu_reg(s, rn);
3217 switch (opc | (invert << 2)) {
3218 case 0: /* AND */
3219 case 3: /* ANDS */
3220 tcg_gen_and_i64(tcg_rd, tcg_rn, tcg_rm);
3221 break;
3222 case 1: /* ORR */
3223 tcg_gen_or_i64(tcg_rd, tcg_rn, tcg_rm);
3224 break;
3225 case 2: /* EOR */
3226 tcg_gen_xor_i64(tcg_rd, tcg_rn, tcg_rm);
3227 break;
3228 case 4: /* BIC */
3229 case 7: /* BICS */
3230 tcg_gen_andc_i64(tcg_rd, tcg_rn, tcg_rm);
3231 break;
3232 case 5: /* ORN */
3233 tcg_gen_orc_i64(tcg_rd, tcg_rn, tcg_rm);
3234 break;
3235 case 6: /* EON */
3236 tcg_gen_eqv_i64(tcg_rd, tcg_rn, tcg_rm);
3237 break;
3238 default:
3239 assert(FALSE);
3240 break;
3243 if (!sf) {
3244 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3247 if (opc == 3) {
3248 gen_logic_CC(sf, tcg_rd);
3253 * C3.5.1 Add/subtract (extended register)
3255 * 31|30|29|28 24|23 22|21|20 16|15 13|12 10|9 5|4 0|
3256 * +--+--+--+-----------+-----+--+-------+------+------+----+----+
3257 * |sf|op| S| 0 1 0 1 1 | opt | 1| Rm |option| imm3 | Rn | Rd |
3258 * +--+--+--+-----------+-----+--+-------+------+------+----+----+
3260 * sf: 0 -> 32bit, 1 -> 64bit
3261 * op: 0 -> add , 1 -> sub
3262 * S: 1 -> set flags
3263 * opt: 00
3264 * option: extension type (see DecodeRegExtend)
3265 * imm3: optional shift to Rm
3267 * Rd = Rn + LSL(extend(Rm), amount)
3269 static void disas_add_sub_ext_reg(DisasContext *s, uint32_t insn)
3271 int rd = extract32(insn, 0, 5);
3272 int rn = extract32(insn, 5, 5);
3273 int imm3 = extract32(insn, 10, 3);
3274 int option = extract32(insn, 13, 3);
3275 int rm = extract32(insn, 16, 5);
3276 bool setflags = extract32(insn, 29, 1);
3277 bool sub_op = extract32(insn, 30, 1);
3278 bool sf = extract32(insn, 31, 1);
3280 TCGv_i64 tcg_rm, tcg_rn; /* temps */
3281 TCGv_i64 tcg_rd;
3282 TCGv_i64 tcg_result;
3284 if (imm3 > 4) {
3285 unallocated_encoding(s);
3286 return;
3289 /* non-flag setting ops may use SP */
3290 if (!setflags) {
3291 tcg_rd = cpu_reg_sp(s, rd);
3292 } else {
3293 tcg_rd = cpu_reg(s, rd);
3295 tcg_rn = read_cpu_reg_sp(s, rn, sf);
3297 tcg_rm = read_cpu_reg(s, rm, sf);
3298 ext_and_shift_reg(tcg_rm, tcg_rm, option, imm3);
3300 tcg_result = tcg_temp_new_i64();
3302 if (!setflags) {
3303 if (sub_op) {
3304 tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
3305 } else {
3306 tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
3308 } else {
3309 if (sub_op) {
3310 gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
3311 } else {
3312 gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
3316 if (sf) {
3317 tcg_gen_mov_i64(tcg_rd, tcg_result);
3318 } else {
3319 tcg_gen_ext32u_i64(tcg_rd, tcg_result);
3322 tcg_temp_free_i64(tcg_result);
3326 * C3.5.2 Add/subtract (shifted register)
3328 * 31 30 29 28 24 23 22 21 20 16 15 10 9 5 4 0
3329 * +--+--+--+-----------+-----+--+-------+---------+------+------+
3330 * |sf|op| S| 0 1 0 1 1 |shift| 0| Rm | imm6 | Rn | Rd |
3331 * +--+--+--+-----------+-----+--+-------+---------+------+------+
3333 * sf: 0 -> 32bit, 1 -> 64bit
3334 * op: 0 -> add , 1 -> sub
3335 * S: 1 -> set flags
3336 * shift: 00 -> LSL, 01 -> LSR, 10 -> ASR, 11 -> RESERVED
3337 * imm6: Shift amount to apply to Rm before the add/sub
3339 static void disas_add_sub_reg(DisasContext *s, uint32_t insn)
3341 int rd = extract32(insn, 0, 5);
3342 int rn = extract32(insn, 5, 5);
3343 int imm6 = extract32(insn, 10, 6);
3344 int rm = extract32(insn, 16, 5);
3345 int shift_type = extract32(insn, 22, 2);
3346 bool setflags = extract32(insn, 29, 1);
3347 bool sub_op = extract32(insn, 30, 1);
3348 bool sf = extract32(insn, 31, 1);
3350 TCGv_i64 tcg_rd = cpu_reg(s, rd);
3351 TCGv_i64 tcg_rn, tcg_rm;
3352 TCGv_i64 tcg_result;
3354 if ((shift_type == 3) || (!sf && (imm6 > 31))) {
3355 unallocated_encoding(s);
3356 return;
3359 tcg_rn = read_cpu_reg(s, rn, sf);
3360 tcg_rm = read_cpu_reg(s, rm, sf);
3362 shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, imm6);
3364 tcg_result = tcg_temp_new_i64();
3366 if (!setflags) {
3367 if (sub_op) {
3368 tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
3369 } else {
3370 tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
3372 } else {
3373 if (sub_op) {
3374 gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
3375 } else {
3376 gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
3380 if (sf) {
3381 tcg_gen_mov_i64(tcg_rd, tcg_result);
3382 } else {
3383 tcg_gen_ext32u_i64(tcg_rd, tcg_result);
3386 tcg_temp_free_i64(tcg_result);
3389 /* C3.5.9 Data-processing (3 source)
3391 31 30 29 28 24 23 21 20 16 15 14 10 9 5 4 0
3392 +--+------+-----------+------+------+----+------+------+------+
3393 |sf| op54 | 1 1 0 1 1 | op31 | Rm | o0 | Ra | Rn | Rd |
3394 +--+------+-----------+------+------+----+------+------+------+
3397 static void disas_data_proc_3src(DisasContext *s, uint32_t insn)
3399 int rd = extract32(insn, 0, 5);
3400 int rn = extract32(insn, 5, 5);
3401 int ra = extract32(insn, 10, 5);
3402 int rm = extract32(insn, 16, 5);
3403 int op_id = (extract32(insn, 29, 3) << 4) |
3404 (extract32(insn, 21, 3) << 1) |
3405 extract32(insn, 15, 1);
3406 bool sf = extract32(insn, 31, 1);
3407 bool is_sub = extract32(op_id, 0, 1);
3408 bool is_high = extract32(op_id, 2, 1);
3409 bool is_signed = false;
3410 TCGv_i64 tcg_op1;
3411 TCGv_i64 tcg_op2;
3412 TCGv_i64 tcg_tmp;
3414 /* Note that op_id is sf:op54:op31:o0 so it includes the 32/64 size flag */
3415 switch (op_id) {
3416 case 0x42: /* SMADDL */
3417 case 0x43: /* SMSUBL */
3418 case 0x44: /* SMULH */
3419 is_signed = true;
3420 break;
3421 case 0x0: /* MADD (32bit) */
3422 case 0x1: /* MSUB (32bit) */
3423 case 0x40: /* MADD (64bit) */
3424 case 0x41: /* MSUB (64bit) */
3425 case 0x4a: /* UMADDL */
3426 case 0x4b: /* UMSUBL */
3427 case 0x4c: /* UMULH */
3428 break;
3429 default:
3430 unallocated_encoding(s);
3431 return;
3434 if (is_high) {
3435 TCGv_i64 low_bits = tcg_temp_new_i64(); /* low bits discarded */
3436 TCGv_i64 tcg_rd = cpu_reg(s, rd);
3437 TCGv_i64 tcg_rn = cpu_reg(s, rn);
3438 TCGv_i64 tcg_rm = cpu_reg(s, rm);
3440 if (is_signed) {
3441 tcg_gen_muls2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
3442 } else {
3443 tcg_gen_mulu2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
3446 tcg_temp_free_i64(low_bits);
3447 return;
3450 tcg_op1 = tcg_temp_new_i64();
3451 tcg_op2 = tcg_temp_new_i64();
3452 tcg_tmp = tcg_temp_new_i64();
3454 if (op_id < 0x42) {
3455 tcg_gen_mov_i64(tcg_op1, cpu_reg(s, rn));
3456 tcg_gen_mov_i64(tcg_op2, cpu_reg(s, rm));
3457 } else {
3458 if (is_signed) {
3459 tcg_gen_ext32s_i64(tcg_op1, cpu_reg(s, rn));
3460 tcg_gen_ext32s_i64(tcg_op2, cpu_reg(s, rm));
3461 } else {
3462 tcg_gen_ext32u_i64(tcg_op1, cpu_reg(s, rn));
3463 tcg_gen_ext32u_i64(tcg_op2, cpu_reg(s, rm));
3467 if (ra == 31 && !is_sub) {
3468 /* Special-case MADD with rA == XZR; it is the standard MUL alias */
3469 tcg_gen_mul_i64(cpu_reg(s, rd), tcg_op1, tcg_op2);
3470 } else {
3471 tcg_gen_mul_i64(tcg_tmp, tcg_op1, tcg_op2);
3472 if (is_sub) {
3473 tcg_gen_sub_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
3474 } else {
3475 tcg_gen_add_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
3479 if (!sf) {
3480 tcg_gen_ext32u_i64(cpu_reg(s, rd), cpu_reg(s, rd));
3483 tcg_temp_free_i64(tcg_op1);
3484 tcg_temp_free_i64(tcg_op2);
3485 tcg_temp_free_i64(tcg_tmp);
3488 /* C3.5.3 - Add/subtract (with carry)
3489 * 31 30 29 28 27 26 25 24 23 22 21 20 16 15 10 9 5 4 0
3490 * +--+--+--+------------------------+------+---------+------+-----+
3491 * |sf|op| S| 1 1 0 1 0 0 0 0 | rm | opcode2 | Rn | Rd |
3492 * +--+--+--+------------------------+------+---------+------+-----+
3493 * [000000]
3496 static void disas_adc_sbc(DisasContext *s, uint32_t insn)
3498 unsigned int sf, op, setflags, rm, rn, rd;
3499 TCGv_i64 tcg_y, tcg_rn, tcg_rd;
3501 if (extract32(insn, 10, 6) != 0) {
3502 unallocated_encoding(s);
3503 return;
3506 sf = extract32(insn, 31, 1);
3507 op = extract32(insn, 30, 1);
3508 setflags = extract32(insn, 29, 1);
3509 rm = extract32(insn, 16, 5);
3510 rn = extract32(insn, 5, 5);
3511 rd = extract32(insn, 0, 5);
3513 tcg_rd = cpu_reg(s, rd);
3514 tcg_rn = cpu_reg(s, rn);
3516 if (op) {
3517 tcg_y = new_tmp_a64(s);
3518 tcg_gen_not_i64(tcg_y, cpu_reg(s, rm));
3519 } else {
3520 tcg_y = cpu_reg(s, rm);
3523 if (setflags) {
3524 gen_adc_CC(sf, tcg_rd, tcg_rn, tcg_y);
3525 } else {
3526 gen_adc(sf, tcg_rd, tcg_rn, tcg_y);
3530 /* C3.5.4 - C3.5.5 Conditional compare (immediate / register)
3531 * 31 30 29 28 27 26 25 24 23 22 21 20 16 15 12 11 10 9 5 4 3 0
3532 * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
3533 * |sf|op| S| 1 1 0 1 0 0 1 0 |imm5/rm | cond |i/r |o2| Rn |o3|nzcv |
3534 * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
3535 * [1] y [0] [0]
3537 static void disas_cc(DisasContext *s, uint32_t insn)
3539 unsigned int sf, op, y, cond, rn, nzcv, is_imm;
3540 int label_continue = -1;
3541 TCGv_i64 tcg_tmp, tcg_y, tcg_rn;
3543 if (!extract32(insn, 29, 1)) {
3544 unallocated_encoding(s);
3545 return;
3547 if (insn & (1 << 10 | 1 << 4)) {
3548 unallocated_encoding(s);
3549 return;
3551 sf = extract32(insn, 31, 1);
3552 op = extract32(insn, 30, 1);
3553 is_imm = extract32(insn, 11, 1);
3554 y = extract32(insn, 16, 5); /* y = rm (reg) or imm5 (imm) */
3555 cond = extract32(insn, 12, 4);
3556 rn = extract32(insn, 5, 5);
3557 nzcv = extract32(insn, 0, 4);
3559 if (cond < 0x0e) { /* not always */
3560 int label_match = gen_new_label();
3561 label_continue = gen_new_label();
3562 arm_gen_test_cc(cond, label_match);
3563 /* nomatch: */
3564 tcg_tmp = tcg_temp_new_i64();
3565 tcg_gen_movi_i64(tcg_tmp, nzcv << 28);
3566 gen_set_nzcv(tcg_tmp);
3567 tcg_temp_free_i64(tcg_tmp);
3568 tcg_gen_br(label_continue);
3569 gen_set_label(label_match);
3571 /* match, or condition is always */
3572 if (is_imm) {
3573 tcg_y = new_tmp_a64(s);
3574 tcg_gen_movi_i64(tcg_y, y);
3575 } else {
3576 tcg_y = cpu_reg(s, y);
3578 tcg_rn = cpu_reg(s, rn);
3580 tcg_tmp = tcg_temp_new_i64();
3581 if (op) {
3582 gen_sub_CC(sf, tcg_tmp, tcg_rn, tcg_y);
3583 } else {
3584 gen_add_CC(sf, tcg_tmp, tcg_rn, tcg_y);
3586 tcg_temp_free_i64(tcg_tmp);
3588 if (cond < 0x0e) { /* continue */
3589 gen_set_label(label_continue);
3593 /* C3.5.6 Conditional select
3594 * 31 30 29 28 21 20 16 15 12 11 10 9 5 4 0
3595 * +----+----+---+-----------------+------+------+-----+------+------+
3596 * | sf | op | S | 1 1 0 1 0 1 0 0 | Rm | cond | op2 | Rn | Rd |
3597 * +----+----+---+-----------------+------+------+-----+------+------+
3599 static void disas_cond_select(DisasContext *s, uint32_t insn)
3601 unsigned int sf, else_inv, rm, cond, else_inc, rn, rd;
3602 TCGv_i64 tcg_rd, tcg_src;
3604 if (extract32(insn, 29, 1) || extract32(insn, 11, 1)) {
3605 /* S == 1 or op2<1> == 1 */
3606 unallocated_encoding(s);
3607 return;
3609 sf = extract32(insn, 31, 1);
3610 else_inv = extract32(insn, 30, 1);
3611 rm = extract32(insn, 16, 5);
3612 cond = extract32(insn, 12, 4);
3613 else_inc = extract32(insn, 10, 1);
3614 rn = extract32(insn, 5, 5);
3615 rd = extract32(insn, 0, 5);
3617 if (rd == 31) {
3618 /* silly no-op write; until we use movcond we must special-case
3619 * this to avoid a dead temporary across basic blocks.
3621 return;
3624 tcg_rd = cpu_reg(s, rd);
3626 if (cond >= 0x0e) { /* condition "always" */
3627 tcg_src = read_cpu_reg(s, rn, sf);
3628 tcg_gen_mov_i64(tcg_rd, tcg_src);
3629 } else {
3630 /* OPTME: we could use movcond here, at the cost of duplicating
3631 * a lot of the arm_gen_test_cc() logic.
3633 int label_match = gen_new_label();
3634 int label_continue = gen_new_label();
3636 arm_gen_test_cc(cond, label_match);
3637 /* nomatch: */
3638 tcg_src = cpu_reg(s, rm);
3640 if (else_inv && else_inc) {
3641 tcg_gen_neg_i64(tcg_rd, tcg_src);
3642 } else if (else_inv) {
3643 tcg_gen_not_i64(tcg_rd, tcg_src);
3644 } else if (else_inc) {
3645 tcg_gen_addi_i64(tcg_rd, tcg_src, 1);
3646 } else {
3647 tcg_gen_mov_i64(tcg_rd, tcg_src);
3649 if (!sf) {
3650 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3652 tcg_gen_br(label_continue);
3653 /* match: */
3654 gen_set_label(label_match);
3655 tcg_src = read_cpu_reg(s, rn, sf);
3656 tcg_gen_mov_i64(tcg_rd, tcg_src);
3657 /* continue: */
3658 gen_set_label(label_continue);
3662 static void handle_clz(DisasContext *s, unsigned int sf,
3663 unsigned int rn, unsigned int rd)
3665 TCGv_i64 tcg_rd, tcg_rn;
3666 tcg_rd = cpu_reg(s, rd);
3667 tcg_rn = cpu_reg(s, rn);
3669 if (sf) {
3670 gen_helper_clz64(tcg_rd, tcg_rn);
3671 } else {
3672 TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
3673 tcg_gen_trunc_i64_i32(tcg_tmp32, tcg_rn);
3674 gen_helper_clz(tcg_tmp32, tcg_tmp32);
3675 tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
3676 tcg_temp_free_i32(tcg_tmp32);
3680 static void handle_cls(DisasContext *s, unsigned int sf,
3681 unsigned int rn, unsigned int rd)
3683 TCGv_i64 tcg_rd, tcg_rn;
3684 tcg_rd = cpu_reg(s, rd);
3685 tcg_rn = cpu_reg(s, rn);
3687 if (sf) {
3688 gen_helper_cls64(tcg_rd, tcg_rn);
3689 } else {
3690 TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
3691 tcg_gen_trunc_i64_i32(tcg_tmp32, tcg_rn);
3692 gen_helper_cls32(tcg_tmp32, tcg_tmp32);
3693 tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
3694 tcg_temp_free_i32(tcg_tmp32);
3698 static void handle_rbit(DisasContext *s, unsigned int sf,
3699 unsigned int rn, unsigned int rd)
3701 TCGv_i64 tcg_rd, tcg_rn;
3702 tcg_rd = cpu_reg(s, rd);
3703 tcg_rn = cpu_reg(s, rn);
3705 if (sf) {
3706 gen_helper_rbit64(tcg_rd, tcg_rn);
3707 } else {
3708 TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
3709 tcg_gen_trunc_i64_i32(tcg_tmp32, tcg_rn);
3710 gen_helper_rbit(tcg_tmp32, tcg_tmp32);
3711 tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
3712 tcg_temp_free_i32(tcg_tmp32);
3716 /* C5.6.149 REV with sf==1, opcode==3 ("REV64") */
3717 static void handle_rev64(DisasContext *s, unsigned int sf,
3718 unsigned int rn, unsigned int rd)
3720 if (!sf) {
3721 unallocated_encoding(s);
3722 return;
3724 tcg_gen_bswap64_i64(cpu_reg(s, rd), cpu_reg(s, rn));
3727 /* C5.6.149 REV with sf==0, opcode==2
3728 * C5.6.151 REV32 (sf==1, opcode==2)
3730 static void handle_rev32(DisasContext *s, unsigned int sf,
3731 unsigned int rn, unsigned int rd)
3733 TCGv_i64 tcg_rd = cpu_reg(s, rd);
3735 if (sf) {
3736 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
3737 TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
3739 /* bswap32_i64 requires zero high word */
3740 tcg_gen_ext32u_i64(tcg_tmp, tcg_rn);
3741 tcg_gen_bswap32_i64(tcg_rd, tcg_tmp);
3742 tcg_gen_shri_i64(tcg_tmp, tcg_rn, 32);
3743 tcg_gen_bswap32_i64(tcg_tmp, tcg_tmp);
3744 tcg_gen_concat32_i64(tcg_rd, tcg_rd, tcg_tmp);
3746 tcg_temp_free_i64(tcg_tmp);
3747 } else {
3748 tcg_gen_ext32u_i64(tcg_rd, cpu_reg(s, rn));
3749 tcg_gen_bswap32_i64(tcg_rd, tcg_rd);
3753 /* C5.6.150 REV16 (opcode==1) */
3754 static void handle_rev16(DisasContext *s, unsigned int sf,
3755 unsigned int rn, unsigned int rd)
3757 TCGv_i64 tcg_rd = cpu_reg(s, rd);
3758 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
3759 TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
3761 tcg_gen_andi_i64(tcg_tmp, tcg_rn, 0xffff);
3762 tcg_gen_bswap16_i64(tcg_rd, tcg_tmp);
3764 tcg_gen_shri_i64(tcg_tmp, tcg_rn, 16);
3765 tcg_gen_andi_i64(tcg_tmp, tcg_tmp, 0xffff);
3766 tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp);
3767 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, 16, 16);
3769 if (sf) {
3770 tcg_gen_shri_i64(tcg_tmp, tcg_rn, 32);
3771 tcg_gen_andi_i64(tcg_tmp, tcg_tmp, 0xffff);
3772 tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp);
3773 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, 32, 16);
3775 tcg_gen_shri_i64(tcg_tmp, tcg_rn, 48);
3776 tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp);
3777 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, 48, 16);
3780 tcg_temp_free_i64(tcg_tmp);
3783 /* C3.5.7 Data-processing (1 source)
3784 * 31 30 29 28 21 20 16 15 10 9 5 4 0
3785 * +----+---+---+-----------------+---------+--------+------+------+
3786 * | sf | 1 | S | 1 1 0 1 0 1 1 0 | opcode2 | opcode | Rn | Rd |
3787 * +----+---+---+-----------------+---------+--------+------+------+
3789 static void disas_data_proc_1src(DisasContext *s, uint32_t insn)
3791 unsigned int sf, opcode, rn, rd;
3793 if (extract32(insn, 29, 1) || extract32(insn, 16, 5)) {
3794 unallocated_encoding(s);
3795 return;
3798 sf = extract32(insn, 31, 1);
3799 opcode = extract32(insn, 10, 6);
3800 rn = extract32(insn, 5, 5);
3801 rd = extract32(insn, 0, 5);
3803 switch (opcode) {
3804 case 0: /* RBIT */
3805 handle_rbit(s, sf, rn, rd);
3806 break;
3807 case 1: /* REV16 */
3808 handle_rev16(s, sf, rn, rd);
3809 break;
3810 case 2: /* REV32 */
3811 handle_rev32(s, sf, rn, rd);
3812 break;
3813 case 3: /* REV64 */
3814 handle_rev64(s, sf, rn, rd);
3815 break;
3816 case 4: /* CLZ */
3817 handle_clz(s, sf, rn, rd);
3818 break;
3819 case 5: /* CLS */
3820 handle_cls(s, sf, rn, rd);
3821 break;
3825 static void handle_div(DisasContext *s, bool is_signed, unsigned int sf,
3826 unsigned int rm, unsigned int rn, unsigned int rd)
3828 TCGv_i64 tcg_n, tcg_m, tcg_rd;
3829 tcg_rd = cpu_reg(s, rd);
3831 if (!sf && is_signed) {
3832 tcg_n = new_tmp_a64(s);
3833 tcg_m = new_tmp_a64(s);
3834 tcg_gen_ext32s_i64(tcg_n, cpu_reg(s, rn));
3835 tcg_gen_ext32s_i64(tcg_m, cpu_reg(s, rm));
3836 } else {
3837 tcg_n = read_cpu_reg(s, rn, sf);
3838 tcg_m = read_cpu_reg(s, rm, sf);
3841 if (is_signed) {
3842 gen_helper_sdiv64(tcg_rd, tcg_n, tcg_m);
3843 } else {
3844 gen_helper_udiv64(tcg_rd, tcg_n, tcg_m);
3847 if (!sf) { /* zero extend final result */
3848 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3852 /* C5.6.115 LSLV, C5.6.118 LSRV, C5.6.17 ASRV, C5.6.154 RORV */
3853 static void handle_shift_reg(DisasContext *s,
3854 enum a64_shift_type shift_type, unsigned int sf,
3855 unsigned int rm, unsigned int rn, unsigned int rd)
3857 TCGv_i64 tcg_shift = tcg_temp_new_i64();
3858 TCGv_i64 tcg_rd = cpu_reg(s, rd);
3859 TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
3861 tcg_gen_andi_i64(tcg_shift, cpu_reg(s, rm), sf ? 63 : 31);
3862 shift_reg(tcg_rd, tcg_rn, sf, shift_type, tcg_shift);
3863 tcg_temp_free_i64(tcg_shift);
3866 /* CRC32[BHWX], CRC32C[BHWX] */
3867 static void handle_crc32(DisasContext *s,
3868 unsigned int sf, unsigned int sz, bool crc32c,
3869 unsigned int rm, unsigned int rn, unsigned int rd)
3871 TCGv_i64 tcg_acc, tcg_val;
3872 TCGv_i32 tcg_bytes;
3874 if (!arm_dc_feature(s, ARM_FEATURE_CRC)
3875 || (sf == 1 && sz != 3)
3876 || (sf == 0 && sz == 3)) {
3877 unallocated_encoding(s);
3878 return;
3881 if (sz == 3) {
3882 tcg_val = cpu_reg(s, rm);
3883 } else {
3884 uint64_t mask;
3885 switch (sz) {
3886 case 0:
3887 mask = 0xFF;
3888 break;
3889 case 1:
3890 mask = 0xFFFF;
3891 break;
3892 case 2:
3893 mask = 0xFFFFFFFF;
3894 break;
3895 default:
3896 g_assert_not_reached();
3898 tcg_val = new_tmp_a64(s);
3899 tcg_gen_andi_i64(tcg_val, cpu_reg(s, rm), mask);
3902 tcg_acc = cpu_reg(s, rn);
3903 tcg_bytes = tcg_const_i32(1 << sz);
3905 if (crc32c) {
3906 gen_helper_crc32c_64(cpu_reg(s, rd), tcg_acc, tcg_val, tcg_bytes);
3907 } else {
3908 gen_helper_crc32_64(cpu_reg(s, rd), tcg_acc, tcg_val, tcg_bytes);
3911 tcg_temp_free_i32(tcg_bytes);
3914 /* C3.5.8 Data-processing (2 source)
3915 * 31 30 29 28 21 20 16 15 10 9 5 4 0
3916 * +----+---+---+-----------------+------+--------+------+------+
3917 * | sf | 0 | S | 1 1 0 1 0 1 1 0 | Rm | opcode | Rn | Rd |
3918 * +----+---+---+-----------------+------+--------+------+------+
3920 static void disas_data_proc_2src(DisasContext *s, uint32_t insn)
3922 unsigned int sf, rm, opcode, rn, rd;
3923 sf = extract32(insn, 31, 1);
3924 rm = extract32(insn, 16, 5);
3925 opcode = extract32(insn, 10, 6);
3926 rn = extract32(insn, 5, 5);
3927 rd = extract32(insn, 0, 5);
3929 if (extract32(insn, 29, 1)) {
3930 unallocated_encoding(s);
3931 return;
3934 switch (opcode) {
3935 case 2: /* UDIV */
3936 handle_div(s, false, sf, rm, rn, rd);
3937 break;
3938 case 3: /* SDIV */
3939 handle_div(s, true, sf, rm, rn, rd);
3940 break;
3941 case 8: /* LSLV */
3942 handle_shift_reg(s, A64_SHIFT_TYPE_LSL, sf, rm, rn, rd);
3943 break;
3944 case 9: /* LSRV */
3945 handle_shift_reg(s, A64_SHIFT_TYPE_LSR, sf, rm, rn, rd);
3946 break;
3947 case 10: /* ASRV */
3948 handle_shift_reg(s, A64_SHIFT_TYPE_ASR, sf, rm, rn, rd);
3949 break;
3950 case 11: /* RORV */
3951 handle_shift_reg(s, A64_SHIFT_TYPE_ROR, sf, rm, rn, rd);
3952 break;
3953 case 16:
3954 case 17:
3955 case 18:
3956 case 19:
3957 case 20:
3958 case 21:
3959 case 22:
3960 case 23: /* CRC32 */
3962 int sz = extract32(opcode, 0, 2);
3963 bool crc32c = extract32(opcode, 2, 1);
3964 handle_crc32(s, sf, sz, crc32c, rm, rn, rd);
3965 break;
3967 default:
3968 unallocated_encoding(s);
3969 break;
3973 /* C3.5 Data processing - register */
3974 static void disas_data_proc_reg(DisasContext *s, uint32_t insn)
3976 switch (extract32(insn, 24, 5)) {
3977 case 0x0a: /* Logical (shifted register) */
3978 disas_logic_reg(s, insn);
3979 break;
3980 case 0x0b: /* Add/subtract */
3981 if (insn & (1 << 21)) { /* (extended register) */
3982 disas_add_sub_ext_reg(s, insn);
3983 } else {
3984 disas_add_sub_reg(s, insn);
3986 break;
3987 case 0x1b: /* Data-processing (3 source) */
3988 disas_data_proc_3src(s, insn);
3989 break;
3990 case 0x1a:
3991 switch (extract32(insn, 21, 3)) {
3992 case 0x0: /* Add/subtract (with carry) */
3993 disas_adc_sbc(s, insn);
3994 break;
3995 case 0x2: /* Conditional compare */
3996 disas_cc(s, insn); /* both imm and reg forms */
3997 break;
3998 case 0x4: /* Conditional select */
3999 disas_cond_select(s, insn);
4000 break;
4001 case 0x6: /* Data-processing */
4002 if (insn & (1 << 30)) { /* (1 source) */
4003 disas_data_proc_1src(s, insn);
4004 } else { /* (2 source) */
4005 disas_data_proc_2src(s, insn);
4007 break;
4008 default:
4009 unallocated_encoding(s);
4010 break;
4012 break;
4013 default:
4014 unallocated_encoding(s);
4015 break;
4019 static void handle_fp_compare(DisasContext *s, bool is_double,
4020 unsigned int rn, unsigned int rm,
4021 bool cmp_with_zero, bool signal_all_nans)
4023 TCGv_i64 tcg_flags = tcg_temp_new_i64();
4024 TCGv_ptr fpst = get_fpstatus_ptr();
4026 if (is_double) {
4027 TCGv_i64 tcg_vn, tcg_vm;
4029 tcg_vn = read_fp_dreg(s, rn);
4030 if (cmp_with_zero) {
4031 tcg_vm = tcg_const_i64(0);
4032 } else {
4033 tcg_vm = read_fp_dreg(s, rm);
4035 if (signal_all_nans) {
4036 gen_helper_vfp_cmped_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
4037 } else {
4038 gen_helper_vfp_cmpd_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
4040 tcg_temp_free_i64(tcg_vn);
4041 tcg_temp_free_i64(tcg_vm);
4042 } else {
4043 TCGv_i32 tcg_vn, tcg_vm;
4045 tcg_vn = read_fp_sreg(s, rn);
4046 if (cmp_with_zero) {
4047 tcg_vm = tcg_const_i32(0);
4048 } else {
4049 tcg_vm = read_fp_sreg(s, rm);
4051 if (signal_all_nans) {
4052 gen_helper_vfp_cmpes_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
4053 } else {
4054 gen_helper_vfp_cmps_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
4056 tcg_temp_free_i32(tcg_vn);
4057 tcg_temp_free_i32(tcg_vm);
4060 tcg_temp_free_ptr(fpst);
4062 gen_set_nzcv(tcg_flags);
4064 tcg_temp_free_i64(tcg_flags);
4067 /* C3.6.22 Floating point compare
4068 * 31 30 29 28 24 23 22 21 20 16 15 14 13 10 9 5 4 0
4069 * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
4070 * | M | 0 | S | 1 1 1 1 0 | type | 1 | Rm | op | 1 0 0 0 | Rn | op2 |
4071 * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
4073 static void disas_fp_compare(DisasContext *s, uint32_t insn)
4075 unsigned int mos, type, rm, op, rn, opc, op2r;
4077 mos = extract32(insn, 29, 3);
4078 type = extract32(insn, 22, 2); /* 0 = single, 1 = double */
4079 rm = extract32(insn, 16, 5);
4080 op = extract32(insn, 14, 2);
4081 rn = extract32(insn, 5, 5);
4082 opc = extract32(insn, 3, 2);
4083 op2r = extract32(insn, 0, 3);
4085 if (mos || op || op2r || type > 1) {
4086 unallocated_encoding(s);
4087 return;
4090 if (!fp_access_check(s)) {
4091 return;
4094 handle_fp_compare(s, type, rn, rm, opc & 1, opc & 2);
4097 /* C3.6.23 Floating point conditional compare
4098 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 3 0
4099 * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
4100 * | M | 0 | S | 1 1 1 1 0 | type | 1 | Rm | cond | 0 1 | Rn | op | nzcv |
4101 * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
4103 static void disas_fp_ccomp(DisasContext *s, uint32_t insn)
4105 unsigned int mos, type, rm, cond, rn, op, nzcv;
4106 TCGv_i64 tcg_flags;
4107 int label_continue = -1;
4109 mos = extract32(insn, 29, 3);
4110 type = extract32(insn, 22, 2); /* 0 = single, 1 = double */
4111 rm = extract32(insn, 16, 5);
4112 cond = extract32(insn, 12, 4);
4113 rn = extract32(insn, 5, 5);
4114 op = extract32(insn, 4, 1);
4115 nzcv = extract32(insn, 0, 4);
4117 if (mos || type > 1) {
4118 unallocated_encoding(s);
4119 return;
4122 if (!fp_access_check(s)) {
4123 return;
4126 if (cond < 0x0e) { /* not always */
4127 int label_match = gen_new_label();
4128 label_continue = gen_new_label();
4129 arm_gen_test_cc(cond, label_match);
4130 /* nomatch: */
4131 tcg_flags = tcg_const_i64(nzcv << 28);
4132 gen_set_nzcv(tcg_flags);
4133 tcg_temp_free_i64(tcg_flags);
4134 tcg_gen_br(label_continue);
4135 gen_set_label(label_match);
4138 handle_fp_compare(s, type, rn, rm, false, op);
4140 if (cond < 0x0e) {
4141 gen_set_label(label_continue);
4145 /* copy src FP register to dst FP register; type specifies single or double */
4146 static void gen_mov_fp2fp(DisasContext *s, int type, int dst, int src)
4148 if (type) {
4149 TCGv_i64 v = read_fp_dreg(s, src);
4150 write_fp_dreg(s, dst, v);
4151 tcg_temp_free_i64(v);
4152 } else {
4153 TCGv_i32 v = read_fp_sreg(s, src);
4154 write_fp_sreg(s, dst, v);
4155 tcg_temp_free_i32(v);
4159 /* C3.6.24 Floating point conditional select
4160 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 0
4161 * +---+---+---+-----------+------+---+------+------+-----+------+------+
4162 * | M | 0 | S | 1 1 1 1 0 | type | 1 | Rm | cond | 1 1 | Rn | Rd |
4163 * +---+---+---+-----------+------+---+------+------+-----+------+------+
4165 static void disas_fp_csel(DisasContext *s, uint32_t insn)
4167 unsigned int mos, type, rm, cond, rn, rd;
4168 int label_continue = -1;
4170 mos = extract32(insn, 29, 3);
4171 type = extract32(insn, 22, 2); /* 0 = single, 1 = double */
4172 rm = extract32(insn, 16, 5);
4173 cond = extract32(insn, 12, 4);
4174 rn = extract32(insn, 5, 5);
4175 rd = extract32(insn, 0, 5);
4177 if (mos || type > 1) {
4178 unallocated_encoding(s);
4179 return;
4182 if (!fp_access_check(s)) {
4183 return;
4186 if (cond < 0x0e) { /* not always */
4187 int label_match = gen_new_label();
4188 label_continue = gen_new_label();
4189 arm_gen_test_cc(cond, label_match);
4190 /* nomatch: */
4191 gen_mov_fp2fp(s, type, rd, rm);
4192 tcg_gen_br(label_continue);
4193 gen_set_label(label_match);
4196 gen_mov_fp2fp(s, type, rd, rn);
4198 if (cond < 0x0e) { /* continue */
4199 gen_set_label(label_continue);
4203 /* C3.6.25 Floating-point data-processing (1 source) - single precision */
4204 static void handle_fp_1src_single(DisasContext *s, int opcode, int rd, int rn)
4206 TCGv_ptr fpst;
4207 TCGv_i32 tcg_op;
4208 TCGv_i32 tcg_res;
4210 fpst = get_fpstatus_ptr();
4211 tcg_op = read_fp_sreg(s, rn);
4212 tcg_res = tcg_temp_new_i32();
4214 switch (opcode) {
4215 case 0x0: /* FMOV */
4216 tcg_gen_mov_i32(tcg_res, tcg_op);
4217 break;
4218 case 0x1: /* FABS */
4219 gen_helper_vfp_abss(tcg_res, tcg_op);
4220 break;
4221 case 0x2: /* FNEG */
4222 gen_helper_vfp_negs(tcg_res, tcg_op);
4223 break;
4224 case 0x3: /* FSQRT */
4225 gen_helper_vfp_sqrts(tcg_res, tcg_op, cpu_env);
4226 break;
4227 case 0x8: /* FRINTN */
4228 case 0x9: /* FRINTP */
4229 case 0xa: /* FRINTM */
4230 case 0xb: /* FRINTZ */
4231 case 0xc: /* FRINTA */
4233 TCGv_i32 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(opcode & 7));
4235 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
4236 gen_helper_rints(tcg_res, tcg_op, fpst);
4238 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
4239 tcg_temp_free_i32(tcg_rmode);
4240 break;
4242 case 0xe: /* FRINTX */
4243 gen_helper_rints_exact(tcg_res, tcg_op, fpst);
4244 break;
4245 case 0xf: /* FRINTI */
4246 gen_helper_rints(tcg_res, tcg_op, fpst);
4247 break;
4248 default:
4249 abort();
4252 write_fp_sreg(s, rd, tcg_res);
4254 tcg_temp_free_ptr(fpst);
4255 tcg_temp_free_i32(tcg_op);
4256 tcg_temp_free_i32(tcg_res);
4259 /* C3.6.25 Floating-point data-processing (1 source) - double precision */
4260 static void handle_fp_1src_double(DisasContext *s, int opcode, int rd, int rn)
4262 TCGv_ptr fpst;
4263 TCGv_i64 tcg_op;
4264 TCGv_i64 tcg_res;
4266 fpst = get_fpstatus_ptr();
4267 tcg_op = read_fp_dreg(s, rn);
4268 tcg_res = tcg_temp_new_i64();
4270 switch (opcode) {
4271 case 0x0: /* FMOV */
4272 tcg_gen_mov_i64(tcg_res, tcg_op);
4273 break;
4274 case 0x1: /* FABS */
4275 gen_helper_vfp_absd(tcg_res, tcg_op);
4276 break;
4277 case 0x2: /* FNEG */
4278 gen_helper_vfp_negd(tcg_res, tcg_op);
4279 break;
4280 case 0x3: /* FSQRT */
4281 gen_helper_vfp_sqrtd(tcg_res, tcg_op, cpu_env);
4282 break;
4283 case 0x8: /* FRINTN */
4284 case 0x9: /* FRINTP */
4285 case 0xa: /* FRINTM */
4286 case 0xb: /* FRINTZ */
4287 case 0xc: /* FRINTA */
4289 TCGv_i32 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(opcode & 7));
4291 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
4292 gen_helper_rintd(tcg_res, tcg_op, fpst);
4294 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
4295 tcg_temp_free_i32(tcg_rmode);
4296 break;
4298 case 0xe: /* FRINTX */
4299 gen_helper_rintd_exact(tcg_res, tcg_op, fpst);
4300 break;
4301 case 0xf: /* FRINTI */
4302 gen_helper_rintd(tcg_res, tcg_op, fpst);
4303 break;
4304 default:
4305 abort();
4308 write_fp_dreg(s, rd, tcg_res);
4310 tcg_temp_free_ptr(fpst);
4311 tcg_temp_free_i64(tcg_op);
4312 tcg_temp_free_i64(tcg_res);
4315 static void handle_fp_fcvt(DisasContext *s, int opcode,
4316 int rd, int rn, int dtype, int ntype)
4318 switch (ntype) {
4319 case 0x0:
4321 TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
4322 if (dtype == 1) {
4323 /* Single to double */
4324 TCGv_i64 tcg_rd = tcg_temp_new_i64();
4325 gen_helper_vfp_fcvtds(tcg_rd, tcg_rn, cpu_env);
4326 write_fp_dreg(s, rd, tcg_rd);
4327 tcg_temp_free_i64(tcg_rd);
4328 } else {
4329 /* Single to half */
4330 TCGv_i32 tcg_rd = tcg_temp_new_i32();
4331 gen_helper_vfp_fcvt_f32_to_f16(tcg_rd, tcg_rn, cpu_env);
4332 /* write_fp_sreg is OK here because top half of tcg_rd is zero */
4333 write_fp_sreg(s, rd, tcg_rd);
4334 tcg_temp_free_i32(tcg_rd);
4336 tcg_temp_free_i32(tcg_rn);
4337 break;
4339 case 0x1:
4341 TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
4342 TCGv_i32 tcg_rd = tcg_temp_new_i32();
4343 if (dtype == 0) {
4344 /* Double to single */
4345 gen_helper_vfp_fcvtsd(tcg_rd, tcg_rn, cpu_env);
4346 } else {
4347 /* Double to half */
4348 gen_helper_vfp_fcvt_f64_to_f16(tcg_rd, tcg_rn, cpu_env);
4349 /* write_fp_sreg is OK here because top half of tcg_rd is zero */
4351 write_fp_sreg(s, rd, tcg_rd);
4352 tcg_temp_free_i32(tcg_rd);
4353 tcg_temp_free_i64(tcg_rn);
4354 break;
4356 case 0x3:
4358 TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
4359 tcg_gen_ext16u_i32(tcg_rn, tcg_rn);
4360 if (dtype == 0) {
4361 /* Half to single */
4362 TCGv_i32 tcg_rd = tcg_temp_new_i32();
4363 gen_helper_vfp_fcvt_f16_to_f32(tcg_rd, tcg_rn, cpu_env);
4364 write_fp_sreg(s, rd, tcg_rd);
4365 tcg_temp_free_i32(tcg_rd);
4366 } else {
4367 /* Half to double */
4368 TCGv_i64 tcg_rd = tcg_temp_new_i64();
4369 gen_helper_vfp_fcvt_f16_to_f64(tcg_rd, tcg_rn, cpu_env);
4370 write_fp_dreg(s, rd, tcg_rd);
4371 tcg_temp_free_i64(tcg_rd);
4373 tcg_temp_free_i32(tcg_rn);
4374 break;
4376 default:
4377 abort();
4381 /* C3.6.25 Floating point data-processing (1 source)
4382 * 31 30 29 28 24 23 22 21 20 15 14 10 9 5 4 0
4383 * +---+---+---+-----------+------+---+--------+-----------+------+------+
4384 * | M | 0 | S | 1 1 1 1 0 | type | 1 | opcode | 1 0 0 0 0 | Rn | Rd |
4385 * +---+---+---+-----------+------+---+--------+-----------+------+------+
4387 static void disas_fp_1src(DisasContext *s, uint32_t insn)
4389 int type = extract32(insn, 22, 2);
4390 int opcode = extract32(insn, 15, 6);
4391 int rn = extract32(insn, 5, 5);
4392 int rd = extract32(insn, 0, 5);
4394 switch (opcode) {
4395 case 0x4: case 0x5: case 0x7:
4397 /* FCVT between half, single and double precision */
4398 int dtype = extract32(opcode, 0, 2);
4399 if (type == 2 || dtype == type) {
4400 unallocated_encoding(s);
4401 return;
4403 if (!fp_access_check(s)) {
4404 return;
4407 handle_fp_fcvt(s, opcode, rd, rn, dtype, type);
4408 break;
4410 case 0x0 ... 0x3:
4411 case 0x8 ... 0xc:
4412 case 0xe ... 0xf:
4413 /* 32-to-32 and 64-to-64 ops */
4414 switch (type) {
4415 case 0:
4416 if (!fp_access_check(s)) {
4417 return;
4420 handle_fp_1src_single(s, opcode, rd, rn);
4421 break;
4422 case 1:
4423 if (!fp_access_check(s)) {
4424 return;
4427 handle_fp_1src_double(s, opcode, rd, rn);
4428 break;
4429 default:
4430 unallocated_encoding(s);
4432 break;
4433 default:
4434 unallocated_encoding(s);
4435 break;
4439 /* C3.6.26 Floating-point data-processing (2 source) - single precision */
4440 static void handle_fp_2src_single(DisasContext *s, int opcode,
4441 int rd, int rn, int rm)
4443 TCGv_i32 tcg_op1;
4444 TCGv_i32 tcg_op2;
4445 TCGv_i32 tcg_res;
4446 TCGv_ptr fpst;
4448 tcg_res = tcg_temp_new_i32();
4449 fpst = get_fpstatus_ptr();
4450 tcg_op1 = read_fp_sreg(s, rn);
4451 tcg_op2 = read_fp_sreg(s, rm);
4453 switch (opcode) {
4454 case 0x0: /* FMUL */
4455 gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
4456 break;
4457 case 0x1: /* FDIV */
4458 gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
4459 break;
4460 case 0x2: /* FADD */
4461 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
4462 break;
4463 case 0x3: /* FSUB */
4464 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
4465 break;
4466 case 0x4: /* FMAX */
4467 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
4468 break;
4469 case 0x5: /* FMIN */
4470 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
4471 break;
4472 case 0x6: /* FMAXNM */
4473 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
4474 break;
4475 case 0x7: /* FMINNM */
4476 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
4477 break;
4478 case 0x8: /* FNMUL */
4479 gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
4480 gen_helper_vfp_negs(tcg_res, tcg_res);
4481 break;
4484 write_fp_sreg(s, rd, tcg_res);
4486 tcg_temp_free_ptr(fpst);
4487 tcg_temp_free_i32(tcg_op1);
4488 tcg_temp_free_i32(tcg_op2);
4489 tcg_temp_free_i32(tcg_res);
4492 /* C3.6.26 Floating-point data-processing (2 source) - double precision */
4493 static void handle_fp_2src_double(DisasContext *s, int opcode,
4494 int rd, int rn, int rm)
4496 TCGv_i64 tcg_op1;
4497 TCGv_i64 tcg_op2;
4498 TCGv_i64 tcg_res;
4499 TCGv_ptr fpst;
4501 tcg_res = tcg_temp_new_i64();
4502 fpst = get_fpstatus_ptr();
4503 tcg_op1 = read_fp_dreg(s, rn);
4504 tcg_op2 = read_fp_dreg(s, rm);
4506 switch (opcode) {
4507 case 0x0: /* FMUL */
4508 gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
4509 break;
4510 case 0x1: /* FDIV */
4511 gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
4512 break;
4513 case 0x2: /* FADD */
4514 gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
4515 break;
4516 case 0x3: /* FSUB */
4517 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
4518 break;
4519 case 0x4: /* FMAX */
4520 gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
4521 break;
4522 case 0x5: /* FMIN */
4523 gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
4524 break;
4525 case 0x6: /* FMAXNM */
4526 gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
4527 break;
4528 case 0x7: /* FMINNM */
4529 gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
4530 break;
4531 case 0x8: /* FNMUL */
4532 gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
4533 gen_helper_vfp_negd(tcg_res, tcg_res);
4534 break;
4537 write_fp_dreg(s, rd, tcg_res);
4539 tcg_temp_free_ptr(fpst);
4540 tcg_temp_free_i64(tcg_op1);
4541 tcg_temp_free_i64(tcg_op2);
4542 tcg_temp_free_i64(tcg_res);
4545 /* C3.6.26 Floating point data-processing (2 source)
4546 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 0
4547 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
4548 * | M | 0 | S | 1 1 1 1 0 | type | 1 | Rm | opcode | 1 0 | Rn | Rd |
4549 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
4551 static void disas_fp_2src(DisasContext *s, uint32_t insn)
4553 int type = extract32(insn, 22, 2);
4554 int rd = extract32(insn, 0, 5);
4555 int rn = extract32(insn, 5, 5);
4556 int rm = extract32(insn, 16, 5);
4557 int opcode = extract32(insn, 12, 4);
4559 if (opcode > 8) {
4560 unallocated_encoding(s);
4561 return;
4564 switch (type) {
4565 case 0:
4566 if (!fp_access_check(s)) {
4567 return;
4569 handle_fp_2src_single(s, opcode, rd, rn, rm);
4570 break;
4571 case 1:
4572 if (!fp_access_check(s)) {
4573 return;
4575 handle_fp_2src_double(s, opcode, rd, rn, rm);
4576 break;
4577 default:
4578 unallocated_encoding(s);
4582 /* C3.6.27 Floating-point data-processing (3 source) - single precision */
4583 static void handle_fp_3src_single(DisasContext *s, bool o0, bool o1,
4584 int rd, int rn, int rm, int ra)
4586 TCGv_i32 tcg_op1, tcg_op2, tcg_op3;
4587 TCGv_i32 tcg_res = tcg_temp_new_i32();
4588 TCGv_ptr fpst = get_fpstatus_ptr();
4590 tcg_op1 = read_fp_sreg(s, rn);
4591 tcg_op2 = read_fp_sreg(s, rm);
4592 tcg_op3 = read_fp_sreg(s, ra);
4594 /* These are fused multiply-add, and must be done as one
4595 * floating point operation with no rounding between the
4596 * multiplication and addition steps.
4597 * NB that doing the negations here as separate steps is
4598 * correct : an input NaN should come out with its sign bit
4599 * flipped if it is a negated-input.
4601 if (o1 == true) {
4602 gen_helper_vfp_negs(tcg_op3, tcg_op3);
4605 if (o0 != o1) {
4606 gen_helper_vfp_negs(tcg_op1, tcg_op1);
4609 gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
4611 write_fp_sreg(s, rd, tcg_res);
4613 tcg_temp_free_ptr(fpst);
4614 tcg_temp_free_i32(tcg_op1);
4615 tcg_temp_free_i32(tcg_op2);
4616 tcg_temp_free_i32(tcg_op3);
4617 tcg_temp_free_i32(tcg_res);
4620 /* C3.6.27 Floating-point data-processing (3 source) - double precision */
4621 static void handle_fp_3src_double(DisasContext *s, bool o0, bool o1,
4622 int rd, int rn, int rm, int ra)
4624 TCGv_i64 tcg_op1, tcg_op2, tcg_op3;
4625 TCGv_i64 tcg_res = tcg_temp_new_i64();
4626 TCGv_ptr fpst = get_fpstatus_ptr();
4628 tcg_op1 = read_fp_dreg(s, rn);
4629 tcg_op2 = read_fp_dreg(s, rm);
4630 tcg_op3 = read_fp_dreg(s, ra);
4632 /* These are fused multiply-add, and must be done as one
4633 * floating point operation with no rounding between the
4634 * multiplication and addition steps.
4635 * NB that doing the negations here as separate steps is
4636 * correct : an input NaN should come out with its sign bit
4637 * flipped if it is a negated-input.
4639 if (o1 == true) {
4640 gen_helper_vfp_negd(tcg_op3, tcg_op3);
4643 if (o0 != o1) {
4644 gen_helper_vfp_negd(tcg_op1, tcg_op1);
4647 gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
4649 write_fp_dreg(s, rd, tcg_res);
4651 tcg_temp_free_ptr(fpst);
4652 tcg_temp_free_i64(tcg_op1);
4653 tcg_temp_free_i64(tcg_op2);
4654 tcg_temp_free_i64(tcg_op3);
4655 tcg_temp_free_i64(tcg_res);
4658 /* C3.6.27 Floating point data-processing (3 source)
4659 * 31 30 29 28 24 23 22 21 20 16 15 14 10 9 5 4 0
4660 * +---+---+---+-----------+------+----+------+----+------+------+------+
4661 * | M | 0 | S | 1 1 1 1 1 | type | o1 | Rm | o0 | Ra | Rn | Rd |
4662 * +---+---+---+-----------+------+----+------+----+------+------+------+
4664 static void disas_fp_3src(DisasContext *s, uint32_t insn)
4666 int type = extract32(insn, 22, 2);
4667 int rd = extract32(insn, 0, 5);
4668 int rn = extract32(insn, 5, 5);
4669 int ra = extract32(insn, 10, 5);
4670 int rm = extract32(insn, 16, 5);
4671 bool o0 = extract32(insn, 15, 1);
4672 bool o1 = extract32(insn, 21, 1);
4674 switch (type) {
4675 case 0:
4676 if (!fp_access_check(s)) {
4677 return;
4679 handle_fp_3src_single(s, o0, o1, rd, rn, rm, ra);
4680 break;
4681 case 1:
4682 if (!fp_access_check(s)) {
4683 return;
4685 handle_fp_3src_double(s, o0, o1, rd, rn, rm, ra);
4686 break;
4687 default:
4688 unallocated_encoding(s);
4692 /* C3.6.28 Floating point immediate
4693 * 31 30 29 28 24 23 22 21 20 13 12 10 9 5 4 0
4694 * +---+---+---+-----------+------+---+------------+-------+------+------+
4695 * | M | 0 | S | 1 1 1 1 0 | type | 1 | imm8 | 1 0 0 | imm5 | Rd |
4696 * +---+---+---+-----------+------+---+------------+-------+------+------+
4698 static void disas_fp_imm(DisasContext *s, uint32_t insn)
4700 int rd = extract32(insn, 0, 5);
4701 int imm8 = extract32(insn, 13, 8);
4702 int is_double = extract32(insn, 22, 2);
4703 uint64_t imm;
4704 TCGv_i64 tcg_res;
4706 if (is_double > 1) {
4707 unallocated_encoding(s);
4708 return;
4711 if (!fp_access_check(s)) {
4712 return;
4715 /* The imm8 encodes the sign bit, enough bits to represent
4716 * an exponent in the range 01....1xx to 10....0xx,
4717 * and the most significant 4 bits of the mantissa; see
4718 * VFPExpandImm() in the v8 ARM ARM.
4720 if (is_double) {
4721 imm = (extract32(imm8, 7, 1) ? 0x8000 : 0) |
4722 (extract32(imm8, 6, 1) ? 0x3fc0 : 0x4000) |
4723 extract32(imm8, 0, 6);
4724 imm <<= 48;
4725 } else {
4726 imm = (extract32(imm8, 7, 1) ? 0x8000 : 0) |
4727 (extract32(imm8, 6, 1) ? 0x3e00 : 0x4000) |
4728 (extract32(imm8, 0, 6) << 3);
4729 imm <<= 16;
4732 tcg_res = tcg_const_i64(imm);
4733 write_fp_dreg(s, rd, tcg_res);
4734 tcg_temp_free_i64(tcg_res);
4737 /* Handle floating point <=> fixed point conversions. Note that we can
4738 * also deal with fp <=> integer conversions as a special case (scale == 64)
4739 * OPTME: consider handling that special case specially or at least skipping
4740 * the call to scalbn in the helpers for zero shifts.
4742 static void handle_fpfpcvt(DisasContext *s, int rd, int rn, int opcode,
4743 bool itof, int rmode, int scale, int sf, int type)
4745 bool is_signed = !(opcode & 1);
4746 bool is_double = type;
4747 TCGv_ptr tcg_fpstatus;
4748 TCGv_i32 tcg_shift;
4750 tcg_fpstatus = get_fpstatus_ptr();
4752 tcg_shift = tcg_const_i32(64 - scale);
4754 if (itof) {
4755 TCGv_i64 tcg_int = cpu_reg(s, rn);
4756 if (!sf) {
4757 TCGv_i64 tcg_extend = new_tmp_a64(s);
4759 if (is_signed) {
4760 tcg_gen_ext32s_i64(tcg_extend, tcg_int);
4761 } else {
4762 tcg_gen_ext32u_i64(tcg_extend, tcg_int);
4765 tcg_int = tcg_extend;
4768 if (is_double) {
4769 TCGv_i64 tcg_double = tcg_temp_new_i64();
4770 if (is_signed) {
4771 gen_helper_vfp_sqtod(tcg_double, tcg_int,
4772 tcg_shift, tcg_fpstatus);
4773 } else {
4774 gen_helper_vfp_uqtod(tcg_double, tcg_int,
4775 tcg_shift, tcg_fpstatus);
4777 write_fp_dreg(s, rd, tcg_double);
4778 tcg_temp_free_i64(tcg_double);
4779 } else {
4780 TCGv_i32 tcg_single = tcg_temp_new_i32();
4781 if (is_signed) {
4782 gen_helper_vfp_sqtos(tcg_single, tcg_int,
4783 tcg_shift, tcg_fpstatus);
4784 } else {
4785 gen_helper_vfp_uqtos(tcg_single, tcg_int,
4786 tcg_shift, tcg_fpstatus);
4788 write_fp_sreg(s, rd, tcg_single);
4789 tcg_temp_free_i32(tcg_single);
4791 } else {
4792 TCGv_i64 tcg_int = cpu_reg(s, rd);
4793 TCGv_i32 tcg_rmode;
4795 if (extract32(opcode, 2, 1)) {
4796 /* There are too many rounding modes to all fit into rmode,
4797 * so FCVTA[US] is a special case.
4799 rmode = FPROUNDING_TIEAWAY;
4802 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rmode));
4804 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
4806 if (is_double) {
4807 TCGv_i64 tcg_double = read_fp_dreg(s, rn);
4808 if (is_signed) {
4809 if (!sf) {
4810 gen_helper_vfp_tosld(tcg_int, tcg_double,
4811 tcg_shift, tcg_fpstatus);
4812 } else {
4813 gen_helper_vfp_tosqd(tcg_int, tcg_double,
4814 tcg_shift, tcg_fpstatus);
4816 } else {
4817 if (!sf) {
4818 gen_helper_vfp_tould(tcg_int, tcg_double,
4819 tcg_shift, tcg_fpstatus);
4820 } else {
4821 gen_helper_vfp_touqd(tcg_int, tcg_double,
4822 tcg_shift, tcg_fpstatus);
4825 tcg_temp_free_i64(tcg_double);
4826 } else {
4827 TCGv_i32 tcg_single = read_fp_sreg(s, rn);
4828 if (sf) {
4829 if (is_signed) {
4830 gen_helper_vfp_tosqs(tcg_int, tcg_single,
4831 tcg_shift, tcg_fpstatus);
4832 } else {
4833 gen_helper_vfp_touqs(tcg_int, tcg_single,
4834 tcg_shift, tcg_fpstatus);
4836 } else {
4837 TCGv_i32 tcg_dest = tcg_temp_new_i32();
4838 if (is_signed) {
4839 gen_helper_vfp_tosls(tcg_dest, tcg_single,
4840 tcg_shift, tcg_fpstatus);
4841 } else {
4842 gen_helper_vfp_touls(tcg_dest, tcg_single,
4843 tcg_shift, tcg_fpstatus);
4845 tcg_gen_extu_i32_i64(tcg_int, tcg_dest);
4846 tcg_temp_free_i32(tcg_dest);
4848 tcg_temp_free_i32(tcg_single);
4851 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
4852 tcg_temp_free_i32(tcg_rmode);
4854 if (!sf) {
4855 tcg_gen_ext32u_i64(tcg_int, tcg_int);
4859 tcg_temp_free_ptr(tcg_fpstatus);
4860 tcg_temp_free_i32(tcg_shift);
4863 /* C3.6.29 Floating point <-> fixed point conversions
4864 * 31 30 29 28 24 23 22 21 20 19 18 16 15 10 9 5 4 0
4865 * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
4866 * | sf | 0 | S | 1 1 1 1 0 | type | 0 | rmode | opcode | scale | Rn | Rd |
4867 * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
4869 static void disas_fp_fixed_conv(DisasContext *s, uint32_t insn)
4871 int rd = extract32(insn, 0, 5);
4872 int rn = extract32(insn, 5, 5);
4873 int scale = extract32(insn, 10, 6);
4874 int opcode = extract32(insn, 16, 3);
4875 int rmode = extract32(insn, 19, 2);
4876 int type = extract32(insn, 22, 2);
4877 bool sbit = extract32(insn, 29, 1);
4878 bool sf = extract32(insn, 31, 1);
4879 bool itof;
4881 if (sbit || (type > 1)
4882 || (!sf && scale < 32)) {
4883 unallocated_encoding(s);
4884 return;
4887 switch ((rmode << 3) | opcode) {
4888 case 0x2: /* SCVTF */
4889 case 0x3: /* UCVTF */
4890 itof = true;
4891 break;
4892 case 0x18: /* FCVTZS */
4893 case 0x19: /* FCVTZU */
4894 itof = false;
4895 break;
4896 default:
4897 unallocated_encoding(s);
4898 return;
4901 if (!fp_access_check(s)) {
4902 return;
4905 handle_fpfpcvt(s, rd, rn, opcode, itof, FPROUNDING_ZERO, scale, sf, type);
4908 static void handle_fmov(DisasContext *s, int rd, int rn, int type, bool itof)
4910 /* FMOV: gpr to or from float, double, or top half of quad fp reg,
4911 * without conversion.
4914 if (itof) {
4915 TCGv_i64 tcg_rn = cpu_reg(s, rn);
4917 switch (type) {
4918 case 0:
4920 /* 32 bit */
4921 TCGv_i64 tmp = tcg_temp_new_i64();
4922 tcg_gen_ext32u_i64(tmp, tcg_rn);
4923 tcg_gen_st_i64(tmp, cpu_env, fp_reg_offset(s, rd, MO_64));
4924 tcg_gen_movi_i64(tmp, 0);
4925 tcg_gen_st_i64(tmp, cpu_env, fp_reg_hi_offset(s, rd));
4926 tcg_temp_free_i64(tmp);
4927 break;
4929 case 1:
4931 /* 64 bit */
4932 TCGv_i64 tmp = tcg_const_i64(0);
4933 tcg_gen_st_i64(tcg_rn, cpu_env, fp_reg_offset(s, rd, MO_64));
4934 tcg_gen_st_i64(tmp, cpu_env, fp_reg_hi_offset(s, rd));
4935 tcg_temp_free_i64(tmp);
4936 break;
4938 case 2:
4939 /* 64 bit to top half. */
4940 tcg_gen_st_i64(tcg_rn, cpu_env, fp_reg_hi_offset(s, rd));
4941 break;
4943 } else {
4944 TCGv_i64 tcg_rd = cpu_reg(s, rd);
4946 switch (type) {
4947 case 0:
4948 /* 32 bit */
4949 tcg_gen_ld32u_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_32));
4950 break;
4951 case 1:
4952 /* 64 bit */
4953 tcg_gen_ld_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_64));
4954 break;
4955 case 2:
4956 /* 64 bits from top half */
4957 tcg_gen_ld_i64(tcg_rd, cpu_env, fp_reg_hi_offset(s, rn));
4958 break;
4963 /* C3.6.30 Floating point <-> integer conversions
4964 * 31 30 29 28 24 23 22 21 20 19 18 16 15 10 9 5 4 0
4965 * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
4966 * | sf | 0 | S | 1 1 1 1 0 | type | 1 | rmode | opc | 0 0 0 0 0 0 | Rn | Rd |
4967 * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
4969 static void disas_fp_int_conv(DisasContext *s, uint32_t insn)
4971 int rd = extract32(insn, 0, 5);
4972 int rn = extract32(insn, 5, 5);
4973 int opcode = extract32(insn, 16, 3);
4974 int rmode = extract32(insn, 19, 2);
4975 int type = extract32(insn, 22, 2);
4976 bool sbit = extract32(insn, 29, 1);
4977 bool sf = extract32(insn, 31, 1);
4979 if (sbit) {
4980 unallocated_encoding(s);
4981 return;
4984 if (opcode > 5) {
4985 /* FMOV */
4986 bool itof = opcode & 1;
4988 if (rmode >= 2) {
4989 unallocated_encoding(s);
4990 return;
4993 switch (sf << 3 | type << 1 | rmode) {
4994 case 0x0: /* 32 bit */
4995 case 0xa: /* 64 bit */
4996 case 0xd: /* 64 bit to top half of quad */
4997 break;
4998 default:
4999 /* all other sf/type/rmode combinations are invalid */
5000 unallocated_encoding(s);
5001 break;
5004 if (!fp_access_check(s)) {
5005 return;
5007 handle_fmov(s, rd, rn, type, itof);
5008 } else {
5009 /* actual FP conversions */
5010 bool itof = extract32(opcode, 1, 1);
5012 if (type > 1 || (rmode != 0 && opcode > 1)) {
5013 unallocated_encoding(s);
5014 return;
5017 if (!fp_access_check(s)) {
5018 return;
5020 handle_fpfpcvt(s, rd, rn, opcode, itof, rmode, 64, sf, type);
5024 /* FP-specific subcases of table C3-6 (SIMD and FP data processing)
5025 * 31 30 29 28 25 24 0
5026 * +---+---+---+---------+-----------------------------+
5027 * | | 0 | | 1 1 1 1 | |
5028 * +---+---+---+---------+-----------------------------+
5030 static void disas_data_proc_fp(DisasContext *s, uint32_t insn)
5032 if (extract32(insn, 24, 1)) {
5033 /* Floating point data-processing (3 source) */
5034 disas_fp_3src(s, insn);
5035 } else if (extract32(insn, 21, 1) == 0) {
5036 /* Floating point to fixed point conversions */
5037 disas_fp_fixed_conv(s, insn);
5038 } else {
5039 switch (extract32(insn, 10, 2)) {
5040 case 1:
5041 /* Floating point conditional compare */
5042 disas_fp_ccomp(s, insn);
5043 break;
5044 case 2:
5045 /* Floating point data-processing (2 source) */
5046 disas_fp_2src(s, insn);
5047 break;
5048 case 3:
5049 /* Floating point conditional select */
5050 disas_fp_csel(s, insn);
5051 break;
5052 case 0:
5053 switch (ctz32(extract32(insn, 12, 4))) {
5054 case 0: /* [15:12] == xxx1 */
5055 /* Floating point immediate */
5056 disas_fp_imm(s, insn);
5057 break;
5058 case 1: /* [15:12] == xx10 */
5059 /* Floating point compare */
5060 disas_fp_compare(s, insn);
5061 break;
5062 case 2: /* [15:12] == x100 */
5063 /* Floating point data-processing (1 source) */
5064 disas_fp_1src(s, insn);
5065 break;
5066 case 3: /* [15:12] == 1000 */
5067 unallocated_encoding(s);
5068 break;
5069 default: /* [15:12] == 0000 */
5070 /* Floating point <-> integer conversions */
5071 disas_fp_int_conv(s, insn);
5072 break;
5074 break;
5079 static void do_ext64(DisasContext *s, TCGv_i64 tcg_left, TCGv_i64 tcg_right,
5080 int pos)
5082 /* Extract 64 bits from the middle of two concatenated 64 bit
5083 * vector register slices left:right. The extracted bits start
5084 * at 'pos' bits into the right (least significant) side.
5085 * We return the result in tcg_right, and guarantee not to
5086 * trash tcg_left.
5088 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
5089 assert(pos > 0 && pos < 64);
5091 tcg_gen_shri_i64(tcg_right, tcg_right, pos);
5092 tcg_gen_shli_i64(tcg_tmp, tcg_left, 64 - pos);
5093 tcg_gen_or_i64(tcg_right, tcg_right, tcg_tmp);
5095 tcg_temp_free_i64(tcg_tmp);
5098 /* C3.6.1 EXT
5099 * 31 30 29 24 23 22 21 20 16 15 14 11 10 9 5 4 0
5100 * +---+---+-------------+-----+---+------+---+------+---+------+------+
5101 * | 0 | Q | 1 0 1 1 1 0 | op2 | 0 | Rm | 0 | imm4 | 0 | Rn | Rd |
5102 * +---+---+-------------+-----+---+------+---+------+---+------+------+
5104 static void disas_simd_ext(DisasContext *s, uint32_t insn)
5106 int is_q = extract32(insn, 30, 1);
5107 int op2 = extract32(insn, 22, 2);
5108 int imm4 = extract32(insn, 11, 4);
5109 int rm = extract32(insn, 16, 5);
5110 int rn = extract32(insn, 5, 5);
5111 int rd = extract32(insn, 0, 5);
5112 int pos = imm4 << 3;
5113 TCGv_i64 tcg_resl, tcg_resh;
5115 if (op2 != 0 || (!is_q && extract32(imm4, 3, 1))) {
5116 unallocated_encoding(s);
5117 return;
5120 if (!fp_access_check(s)) {
5121 return;
5124 tcg_resh = tcg_temp_new_i64();
5125 tcg_resl = tcg_temp_new_i64();
5127 /* Vd gets bits starting at pos bits into Vm:Vn. This is
5128 * either extracting 128 bits from a 128:128 concatenation, or
5129 * extracting 64 bits from a 64:64 concatenation.
5131 if (!is_q) {
5132 read_vec_element(s, tcg_resl, rn, 0, MO_64);
5133 if (pos != 0) {
5134 read_vec_element(s, tcg_resh, rm, 0, MO_64);
5135 do_ext64(s, tcg_resh, tcg_resl, pos);
5137 tcg_gen_movi_i64(tcg_resh, 0);
5138 } else {
5139 TCGv_i64 tcg_hh;
5140 typedef struct {
5141 int reg;
5142 int elt;
5143 } EltPosns;
5144 EltPosns eltposns[] = { {rn, 0}, {rn, 1}, {rm, 0}, {rm, 1} };
5145 EltPosns *elt = eltposns;
5147 if (pos >= 64) {
5148 elt++;
5149 pos -= 64;
5152 read_vec_element(s, tcg_resl, elt->reg, elt->elt, MO_64);
5153 elt++;
5154 read_vec_element(s, tcg_resh, elt->reg, elt->elt, MO_64);
5155 elt++;
5156 if (pos != 0) {
5157 do_ext64(s, tcg_resh, tcg_resl, pos);
5158 tcg_hh = tcg_temp_new_i64();
5159 read_vec_element(s, tcg_hh, elt->reg, elt->elt, MO_64);
5160 do_ext64(s, tcg_hh, tcg_resh, pos);
5161 tcg_temp_free_i64(tcg_hh);
5165 write_vec_element(s, tcg_resl, rd, 0, MO_64);
5166 tcg_temp_free_i64(tcg_resl);
5167 write_vec_element(s, tcg_resh, rd, 1, MO_64);
5168 tcg_temp_free_i64(tcg_resh);
5171 /* C3.6.2 TBL/TBX
5172 * 31 30 29 24 23 22 21 20 16 15 14 13 12 11 10 9 5 4 0
5173 * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
5174 * | 0 | Q | 0 0 1 1 1 0 | op2 | 0 | Rm | 0 | len | op | 0 0 | Rn | Rd |
5175 * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
5177 static void disas_simd_tb(DisasContext *s, uint32_t insn)
5179 int op2 = extract32(insn, 22, 2);
5180 int is_q = extract32(insn, 30, 1);
5181 int rm = extract32(insn, 16, 5);
5182 int rn = extract32(insn, 5, 5);
5183 int rd = extract32(insn, 0, 5);
5184 int is_tblx = extract32(insn, 12, 1);
5185 int len = extract32(insn, 13, 2);
5186 TCGv_i64 tcg_resl, tcg_resh, tcg_idx;
5187 TCGv_i32 tcg_regno, tcg_numregs;
5189 if (op2 != 0) {
5190 unallocated_encoding(s);
5191 return;
5194 if (!fp_access_check(s)) {
5195 return;
5198 /* This does a table lookup: for every byte element in the input
5199 * we index into a table formed from up to four vector registers,
5200 * and then the output is the result of the lookups. Our helper
5201 * function does the lookup operation for a single 64 bit part of
5202 * the input.
5204 tcg_resl = tcg_temp_new_i64();
5205 tcg_resh = tcg_temp_new_i64();
5207 if (is_tblx) {
5208 read_vec_element(s, tcg_resl, rd, 0, MO_64);
5209 } else {
5210 tcg_gen_movi_i64(tcg_resl, 0);
5212 if (is_tblx && is_q) {
5213 read_vec_element(s, tcg_resh, rd, 1, MO_64);
5214 } else {
5215 tcg_gen_movi_i64(tcg_resh, 0);
5218 tcg_idx = tcg_temp_new_i64();
5219 tcg_regno = tcg_const_i32(rn);
5220 tcg_numregs = tcg_const_i32(len + 1);
5221 read_vec_element(s, tcg_idx, rm, 0, MO_64);
5222 gen_helper_simd_tbl(tcg_resl, cpu_env, tcg_resl, tcg_idx,
5223 tcg_regno, tcg_numregs);
5224 if (is_q) {
5225 read_vec_element(s, tcg_idx, rm, 1, MO_64);
5226 gen_helper_simd_tbl(tcg_resh, cpu_env, tcg_resh, tcg_idx,
5227 tcg_regno, tcg_numregs);
5229 tcg_temp_free_i64(tcg_idx);
5230 tcg_temp_free_i32(tcg_regno);
5231 tcg_temp_free_i32(tcg_numregs);
5233 write_vec_element(s, tcg_resl, rd, 0, MO_64);
5234 tcg_temp_free_i64(tcg_resl);
5235 write_vec_element(s, tcg_resh, rd, 1, MO_64);
5236 tcg_temp_free_i64(tcg_resh);
5239 /* C3.6.3 ZIP/UZP/TRN
5240 * 31 30 29 24 23 22 21 20 16 15 14 12 11 10 9 5 4 0
5241 * +---+---+-------------+------+---+------+---+------------------+------+
5242 * | 0 | Q | 0 0 1 1 1 0 | size | 0 | Rm | 0 | opc | 1 0 | Rn | Rd |
5243 * +---+---+-------------+------+---+------+---+------------------+------+
5245 static void disas_simd_zip_trn(DisasContext *s, uint32_t insn)
5247 int rd = extract32(insn, 0, 5);
5248 int rn = extract32(insn, 5, 5);
5249 int rm = extract32(insn, 16, 5);
5250 int size = extract32(insn, 22, 2);
5251 /* opc field bits [1:0] indicate ZIP/UZP/TRN;
5252 * bit 2 indicates 1 vs 2 variant of the insn.
5254 int opcode = extract32(insn, 12, 2);
5255 bool part = extract32(insn, 14, 1);
5256 bool is_q = extract32(insn, 30, 1);
5257 int esize = 8 << size;
5258 int i, ofs;
5259 int datasize = is_q ? 128 : 64;
5260 int elements = datasize / esize;
5261 TCGv_i64 tcg_res, tcg_resl, tcg_resh;
5263 if (opcode == 0 || (size == 3 && !is_q)) {
5264 unallocated_encoding(s);
5265 return;
5268 if (!fp_access_check(s)) {
5269 return;
5272 tcg_resl = tcg_const_i64(0);
5273 tcg_resh = tcg_const_i64(0);
5274 tcg_res = tcg_temp_new_i64();
5276 for (i = 0; i < elements; i++) {
5277 switch (opcode) {
5278 case 1: /* UZP1/2 */
5280 int midpoint = elements / 2;
5281 if (i < midpoint) {
5282 read_vec_element(s, tcg_res, rn, 2 * i + part, size);
5283 } else {
5284 read_vec_element(s, tcg_res, rm,
5285 2 * (i - midpoint) + part, size);
5287 break;
5289 case 2: /* TRN1/2 */
5290 if (i & 1) {
5291 read_vec_element(s, tcg_res, rm, (i & ~1) + part, size);
5292 } else {
5293 read_vec_element(s, tcg_res, rn, (i & ~1) + part, size);
5295 break;
5296 case 3: /* ZIP1/2 */
5298 int base = part * elements / 2;
5299 if (i & 1) {
5300 read_vec_element(s, tcg_res, rm, base + (i >> 1), size);
5301 } else {
5302 read_vec_element(s, tcg_res, rn, base + (i >> 1), size);
5304 break;
5306 default:
5307 g_assert_not_reached();
5310 ofs = i * esize;
5311 if (ofs < 64) {
5312 tcg_gen_shli_i64(tcg_res, tcg_res, ofs);
5313 tcg_gen_or_i64(tcg_resl, tcg_resl, tcg_res);
5314 } else {
5315 tcg_gen_shli_i64(tcg_res, tcg_res, ofs - 64);
5316 tcg_gen_or_i64(tcg_resh, tcg_resh, tcg_res);
5320 tcg_temp_free_i64(tcg_res);
5322 write_vec_element(s, tcg_resl, rd, 0, MO_64);
5323 tcg_temp_free_i64(tcg_resl);
5324 write_vec_element(s, tcg_resh, rd, 1, MO_64);
5325 tcg_temp_free_i64(tcg_resh);
5328 static void do_minmaxop(DisasContext *s, TCGv_i32 tcg_elt1, TCGv_i32 tcg_elt2,
5329 int opc, bool is_min, TCGv_ptr fpst)
5331 /* Helper function for disas_simd_across_lanes: do a single precision
5332 * min/max operation on the specified two inputs,
5333 * and return the result in tcg_elt1.
5335 if (opc == 0xc) {
5336 if (is_min) {
5337 gen_helper_vfp_minnums(tcg_elt1, tcg_elt1, tcg_elt2, fpst);
5338 } else {
5339 gen_helper_vfp_maxnums(tcg_elt1, tcg_elt1, tcg_elt2, fpst);
5341 } else {
5342 assert(opc == 0xf);
5343 if (is_min) {
5344 gen_helper_vfp_mins(tcg_elt1, tcg_elt1, tcg_elt2, fpst);
5345 } else {
5346 gen_helper_vfp_maxs(tcg_elt1, tcg_elt1, tcg_elt2, fpst);
5351 /* C3.6.4 AdvSIMD across lanes
5352 * 31 30 29 28 24 23 22 21 17 16 12 11 10 9 5 4 0
5353 * +---+---+---+-----------+------+-----------+--------+-----+------+------+
5354 * | 0 | Q | U | 0 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 | Rn | Rd |
5355 * +---+---+---+-----------+------+-----------+--------+-----+------+------+
5357 static void disas_simd_across_lanes(DisasContext *s, uint32_t insn)
5359 int rd = extract32(insn, 0, 5);
5360 int rn = extract32(insn, 5, 5);
5361 int size = extract32(insn, 22, 2);
5362 int opcode = extract32(insn, 12, 5);
5363 bool is_q = extract32(insn, 30, 1);
5364 bool is_u = extract32(insn, 29, 1);
5365 bool is_fp = false;
5366 bool is_min = false;
5367 int esize;
5368 int elements;
5369 int i;
5370 TCGv_i64 tcg_res, tcg_elt;
5372 switch (opcode) {
5373 case 0x1b: /* ADDV */
5374 if (is_u) {
5375 unallocated_encoding(s);
5376 return;
5378 /* fall through */
5379 case 0x3: /* SADDLV, UADDLV */
5380 case 0xa: /* SMAXV, UMAXV */
5381 case 0x1a: /* SMINV, UMINV */
5382 if (size == 3 || (size == 2 && !is_q)) {
5383 unallocated_encoding(s);
5384 return;
5386 break;
5387 case 0xc: /* FMAXNMV, FMINNMV */
5388 case 0xf: /* FMAXV, FMINV */
5389 if (!is_u || !is_q || extract32(size, 0, 1)) {
5390 unallocated_encoding(s);
5391 return;
5393 /* Bit 1 of size field encodes min vs max, and actual size is always
5394 * 32 bits: adjust the size variable so following code can rely on it
5396 is_min = extract32(size, 1, 1);
5397 is_fp = true;
5398 size = 2;
5399 break;
5400 default:
5401 unallocated_encoding(s);
5402 return;
5405 if (!fp_access_check(s)) {
5406 return;
5409 esize = 8 << size;
5410 elements = (is_q ? 128 : 64) / esize;
5412 tcg_res = tcg_temp_new_i64();
5413 tcg_elt = tcg_temp_new_i64();
5415 /* These instructions operate across all lanes of a vector
5416 * to produce a single result. We can guarantee that a 64
5417 * bit intermediate is sufficient:
5418 * + for [US]ADDLV the maximum element size is 32 bits, and
5419 * the result type is 64 bits
5420 * + for FMAX*V, FMIN*V, ADDV the intermediate type is the
5421 * same as the element size, which is 32 bits at most
5422 * For the integer operations we can choose to work at 64
5423 * or 32 bits and truncate at the end; for simplicity
5424 * we use 64 bits always. The floating point
5425 * ops do require 32 bit intermediates, though.
5427 if (!is_fp) {
5428 read_vec_element(s, tcg_res, rn, 0, size | (is_u ? 0 : MO_SIGN));
5430 for (i = 1; i < elements; i++) {
5431 read_vec_element(s, tcg_elt, rn, i, size | (is_u ? 0 : MO_SIGN));
5433 switch (opcode) {
5434 case 0x03: /* SADDLV / UADDLV */
5435 case 0x1b: /* ADDV */
5436 tcg_gen_add_i64(tcg_res, tcg_res, tcg_elt);
5437 break;
5438 case 0x0a: /* SMAXV / UMAXV */
5439 tcg_gen_movcond_i64(is_u ? TCG_COND_GEU : TCG_COND_GE,
5440 tcg_res,
5441 tcg_res, tcg_elt, tcg_res, tcg_elt);
5442 break;
5443 case 0x1a: /* SMINV / UMINV */
5444 tcg_gen_movcond_i64(is_u ? TCG_COND_LEU : TCG_COND_LE,
5445 tcg_res,
5446 tcg_res, tcg_elt, tcg_res, tcg_elt);
5447 break;
5448 break;
5449 default:
5450 g_assert_not_reached();
5454 } else {
5455 /* Floating point ops which work on 32 bit (single) intermediates.
5456 * Note that correct NaN propagation requires that we do these
5457 * operations in exactly the order specified by the pseudocode.
5459 TCGv_i32 tcg_elt1 = tcg_temp_new_i32();
5460 TCGv_i32 tcg_elt2 = tcg_temp_new_i32();
5461 TCGv_i32 tcg_elt3 = tcg_temp_new_i32();
5462 TCGv_ptr fpst = get_fpstatus_ptr();
5464 assert(esize == 32);
5465 assert(elements == 4);
5467 read_vec_element(s, tcg_elt, rn, 0, MO_32);
5468 tcg_gen_trunc_i64_i32(tcg_elt1, tcg_elt);
5469 read_vec_element(s, tcg_elt, rn, 1, MO_32);
5470 tcg_gen_trunc_i64_i32(tcg_elt2, tcg_elt);
5472 do_minmaxop(s, tcg_elt1, tcg_elt2, opcode, is_min, fpst);
5474 read_vec_element(s, tcg_elt, rn, 2, MO_32);
5475 tcg_gen_trunc_i64_i32(tcg_elt2, tcg_elt);
5476 read_vec_element(s, tcg_elt, rn, 3, MO_32);
5477 tcg_gen_trunc_i64_i32(tcg_elt3, tcg_elt);
5479 do_minmaxop(s, tcg_elt2, tcg_elt3, opcode, is_min, fpst);
5481 do_minmaxop(s, tcg_elt1, tcg_elt2, opcode, is_min, fpst);
5483 tcg_gen_extu_i32_i64(tcg_res, tcg_elt1);
5484 tcg_temp_free_i32(tcg_elt1);
5485 tcg_temp_free_i32(tcg_elt2);
5486 tcg_temp_free_i32(tcg_elt3);
5487 tcg_temp_free_ptr(fpst);
5490 tcg_temp_free_i64(tcg_elt);
5492 /* Now truncate the result to the width required for the final output */
5493 if (opcode == 0x03) {
5494 /* SADDLV, UADDLV: result is 2*esize */
5495 size++;
5498 switch (size) {
5499 case 0:
5500 tcg_gen_ext8u_i64(tcg_res, tcg_res);
5501 break;
5502 case 1:
5503 tcg_gen_ext16u_i64(tcg_res, tcg_res);
5504 break;
5505 case 2:
5506 tcg_gen_ext32u_i64(tcg_res, tcg_res);
5507 break;
5508 case 3:
5509 break;
5510 default:
5511 g_assert_not_reached();
5514 write_fp_dreg(s, rd, tcg_res);
5515 tcg_temp_free_i64(tcg_res);
5518 /* C6.3.31 DUP (Element, Vector)
5520 * 31 30 29 21 20 16 15 10 9 5 4 0
5521 * +---+---+-------------------+--------+-------------+------+------+
5522 * | 0 | Q | 0 0 1 1 1 0 0 0 0 | imm5 | 0 0 0 0 0 1 | Rn | Rd |
5523 * +---+---+-------------------+--------+-------------+------+------+
5525 * size: encoded in imm5 (see ARM ARM LowestSetBit())
5527 static void handle_simd_dupe(DisasContext *s, int is_q, int rd, int rn,
5528 int imm5)
5530 int size = ctz32(imm5);
5531 int esize = 8 << size;
5532 int elements = (is_q ? 128 : 64) / esize;
5533 int index, i;
5534 TCGv_i64 tmp;
5536 if (size > 3 || (size == 3 && !is_q)) {
5537 unallocated_encoding(s);
5538 return;
5541 if (!fp_access_check(s)) {
5542 return;
5545 index = imm5 >> (size + 1);
5547 tmp = tcg_temp_new_i64();
5548 read_vec_element(s, tmp, rn, index, size);
5550 for (i = 0; i < elements; i++) {
5551 write_vec_element(s, tmp, rd, i, size);
5554 if (!is_q) {
5555 clear_vec_high(s, rd);
5558 tcg_temp_free_i64(tmp);
5561 /* C6.3.31 DUP (element, scalar)
5562 * 31 21 20 16 15 10 9 5 4 0
5563 * +-----------------------+--------+-------------+------+------+
5564 * | 0 1 0 1 1 1 1 0 0 0 0 | imm5 | 0 0 0 0 0 1 | Rn | Rd |
5565 * +-----------------------+--------+-------------+------+------+
5567 static void handle_simd_dupes(DisasContext *s, int rd, int rn,
5568 int imm5)
5570 int size = ctz32(imm5);
5571 int index;
5572 TCGv_i64 tmp;
5574 if (size > 3) {
5575 unallocated_encoding(s);
5576 return;
5579 if (!fp_access_check(s)) {
5580 return;
5583 index = imm5 >> (size + 1);
5585 /* This instruction just extracts the specified element and
5586 * zero-extends it into the bottom of the destination register.
5588 tmp = tcg_temp_new_i64();
5589 read_vec_element(s, tmp, rn, index, size);
5590 write_fp_dreg(s, rd, tmp);
5591 tcg_temp_free_i64(tmp);
5594 /* C6.3.32 DUP (General)
5596 * 31 30 29 21 20 16 15 10 9 5 4 0
5597 * +---+---+-------------------+--------+-------------+------+------+
5598 * | 0 | Q | 0 0 1 1 1 0 0 0 0 | imm5 | 0 0 0 0 1 1 | Rn | Rd |
5599 * +---+---+-------------------+--------+-------------+------+------+
5601 * size: encoded in imm5 (see ARM ARM LowestSetBit())
5603 static void handle_simd_dupg(DisasContext *s, int is_q, int rd, int rn,
5604 int imm5)
5606 int size = ctz32(imm5);
5607 int esize = 8 << size;
5608 int elements = (is_q ? 128 : 64)/esize;
5609 int i = 0;
5611 if (size > 3 || ((size == 3) && !is_q)) {
5612 unallocated_encoding(s);
5613 return;
5616 if (!fp_access_check(s)) {
5617 return;
5620 for (i = 0; i < elements; i++) {
5621 write_vec_element(s, cpu_reg(s, rn), rd, i, size);
5623 if (!is_q) {
5624 clear_vec_high(s, rd);
5628 /* C6.3.150 INS (Element)
5630 * 31 21 20 16 15 14 11 10 9 5 4 0
5631 * +-----------------------+--------+------------+---+------+------+
5632 * | 0 1 1 0 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 | Rn | Rd |
5633 * +-----------------------+--------+------------+---+------+------+
5635 * size: encoded in imm5 (see ARM ARM LowestSetBit())
5636 * index: encoded in imm5<4:size+1>
5638 static void handle_simd_inse(DisasContext *s, int rd, int rn,
5639 int imm4, int imm5)
5641 int size = ctz32(imm5);
5642 int src_index, dst_index;
5643 TCGv_i64 tmp;
5645 if (size > 3) {
5646 unallocated_encoding(s);
5647 return;
5650 if (!fp_access_check(s)) {
5651 return;
5654 dst_index = extract32(imm5, 1+size, 5);
5655 src_index = extract32(imm4, size, 4);
5657 tmp = tcg_temp_new_i64();
5659 read_vec_element(s, tmp, rn, src_index, size);
5660 write_vec_element(s, tmp, rd, dst_index, size);
5662 tcg_temp_free_i64(tmp);
5666 /* C6.3.151 INS (General)
5668 * 31 21 20 16 15 10 9 5 4 0
5669 * +-----------------------+--------+-------------+------+------+
5670 * | 0 1 0 0 1 1 1 0 0 0 0 | imm5 | 0 0 0 1 1 1 | Rn | Rd |
5671 * +-----------------------+--------+-------------+------+------+
5673 * size: encoded in imm5 (see ARM ARM LowestSetBit())
5674 * index: encoded in imm5<4:size+1>
5676 static void handle_simd_insg(DisasContext *s, int rd, int rn, int imm5)
5678 int size = ctz32(imm5);
5679 int idx;
5681 if (size > 3) {
5682 unallocated_encoding(s);
5683 return;
5686 if (!fp_access_check(s)) {
5687 return;
5690 idx = extract32(imm5, 1 + size, 4 - size);
5691 write_vec_element(s, cpu_reg(s, rn), rd, idx, size);
5695 * C6.3.321 UMOV (General)
5696 * C6.3.237 SMOV (General)
5698 * 31 30 29 21 20 16 15 12 10 9 5 4 0
5699 * +---+---+-------------------+--------+-------------+------+------+
5700 * | 0 | Q | 0 0 1 1 1 0 0 0 0 | imm5 | 0 0 1 U 1 1 | Rn | Rd |
5701 * +---+---+-------------------+--------+-------------+------+------+
5703 * U: unsigned when set
5704 * size: encoded in imm5 (see ARM ARM LowestSetBit())
5706 static void handle_simd_umov_smov(DisasContext *s, int is_q, int is_signed,
5707 int rn, int rd, int imm5)
5709 int size = ctz32(imm5);
5710 int element;
5711 TCGv_i64 tcg_rd;
5713 /* Check for UnallocatedEncodings */
5714 if (is_signed) {
5715 if (size > 2 || (size == 2 && !is_q)) {
5716 unallocated_encoding(s);
5717 return;
5719 } else {
5720 if (size > 3
5721 || (size < 3 && is_q)
5722 || (size == 3 && !is_q)) {
5723 unallocated_encoding(s);
5724 return;
5728 if (!fp_access_check(s)) {
5729 return;
5732 element = extract32(imm5, 1+size, 4);
5734 tcg_rd = cpu_reg(s, rd);
5735 read_vec_element(s, tcg_rd, rn, element, size | (is_signed ? MO_SIGN : 0));
5736 if (is_signed && !is_q) {
5737 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
5741 /* C3.6.5 AdvSIMD copy
5742 * 31 30 29 28 21 20 16 15 14 11 10 9 5 4 0
5743 * +---+---+----+-----------------+------+---+------+---+------+------+
5744 * | 0 | Q | op | 0 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 | Rn | Rd |
5745 * +---+---+----+-----------------+------+---+------+---+------+------+
5747 static void disas_simd_copy(DisasContext *s, uint32_t insn)
5749 int rd = extract32(insn, 0, 5);
5750 int rn = extract32(insn, 5, 5);
5751 int imm4 = extract32(insn, 11, 4);
5752 int op = extract32(insn, 29, 1);
5753 int is_q = extract32(insn, 30, 1);
5754 int imm5 = extract32(insn, 16, 5);
5756 if (op) {
5757 if (is_q) {
5758 /* INS (element) */
5759 handle_simd_inse(s, rd, rn, imm4, imm5);
5760 } else {
5761 unallocated_encoding(s);
5763 } else {
5764 switch (imm4) {
5765 case 0:
5766 /* DUP (element - vector) */
5767 handle_simd_dupe(s, is_q, rd, rn, imm5);
5768 break;
5769 case 1:
5770 /* DUP (general) */
5771 handle_simd_dupg(s, is_q, rd, rn, imm5);
5772 break;
5773 case 3:
5774 if (is_q) {
5775 /* INS (general) */
5776 handle_simd_insg(s, rd, rn, imm5);
5777 } else {
5778 unallocated_encoding(s);
5780 break;
5781 case 5:
5782 case 7:
5783 /* UMOV/SMOV (is_q indicates 32/64; imm4 indicates signedness) */
5784 handle_simd_umov_smov(s, is_q, (imm4 == 5), rn, rd, imm5);
5785 break;
5786 default:
5787 unallocated_encoding(s);
5788 break;
5793 /* C3.6.6 AdvSIMD modified immediate
5794 * 31 30 29 28 19 18 16 15 12 11 10 9 5 4 0
5795 * +---+---+----+---------------------+-----+-------+----+---+-------+------+
5796 * | 0 | Q | op | 0 1 1 1 1 0 0 0 0 0 | abc | cmode | o2 | 1 | defgh | Rd |
5797 * +---+---+----+---------------------+-----+-------+----+---+-------+------+
5799 * There are a number of operations that can be carried out here:
5800 * MOVI - move (shifted) imm into register
5801 * MVNI - move inverted (shifted) imm into register
5802 * ORR - bitwise OR of (shifted) imm with register
5803 * BIC - bitwise clear of (shifted) imm with register
5805 static void disas_simd_mod_imm(DisasContext *s, uint32_t insn)
5807 int rd = extract32(insn, 0, 5);
5808 int cmode = extract32(insn, 12, 4);
5809 int cmode_3_1 = extract32(cmode, 1, 3);
5810 int cmode_0 = extract32(cmode, 0, 1);
5811 int o2 = extract32(insn, 11, 1);
5812 uint64_t abcdefgh = extract32(insn, 5, 5) | (extract32(insn, 16, 3) << 5);
5813 bool is_neg = extract32(insn, 29, 1);
5814 bool is_q = extract32(insn, 30, 1);
5815 uint64_t imm = 0;
5816 TCGv_i64 tcg_rd, tcg_imm;
5817 int i;
5819 if (o2 != 0 || ((cmode == 0xf) && is_neg && !is_q)) {
5820 unallocated_encoding(s);
5821 return;
5824 if (!fp_access_check(s)) {
5825 return;
5828 /* See AdvSIMDExpandImm() in ARM ARM */
5829 switch (cmode_3_1) {
5830 case 0: /* Replicate(Zeros(24):imm8, 2) */
5831 case 1: /* Replicate(Zeros(16):imm8:Zeros(8), 2) */
5832 case 2: /* Replicate(Zeros(8):imm8:Zeros(16), 2) */
5833 case 3: /* Replicate(imm8:Zeros(24), 2) */
5835 int shift = cmode_3_1 * 8;
5836 imm = bitfield_replicate(abcdefgh << shift, 32);
5837 break;
5839 case 4: /* Replicate(Zeros(8):imm8, 4) */
5840 case 5: /* Replicate(imm8:Zeros(8), 4) */
5842 int shift = (cmode_3_1 & 0x1) * 8;
5843 imm = bitfield_replicate(abcdefgh << shift, 16);
5844 break;
5846 case 6:
5847 if (cmode_0) {
5848 /* Replicate(Zeros(8):imm8:Ones(16), 2) */
5849 imm = (abcdefgh << 16) | 0xffff;
5850 } else {
5851 /* Replicate(Zeros(16):imm8:Ones(8), 2) */
5852 imm = (abcdefgh << 8) | 0xff;
5854 imm = bitfield_replicate(imm, 32);
5855 break;
5856 case 7:
5857 if (!cmode_0 && !is_neg) {
5858 imm = bitfield_replicate(abcdefgh, 8);
5859 } else if (!cmode_0 && is_neg) {
5860 int i;
5861 imm = 0;
5862 for (i = 0; i < 8; i++) {
5863 if ((abcdefgh) & (1 << i)) {
5864 imm |= 0xffULL << (i * 8);
5867 } else if (cmode_0) {
5868 if (is_neg) {
5869 imm = (abcdefgh & 0x3f) << 48;
5870 if (abcdefgh & 0x80) {
5871 imm |= 0x8000000000000000ULL;
5873 if (abcdefgh & 0x40) {
5874 imm |= 0x3fc0000000000000ULL;
5875 } else {
5876 imm |= 0x4000000000000000ULL;
5878 } else {
5879 imm = (abcdefgh & 0x3f) << 19;
5880 if (abcdefgh & 0x80) {
5881 imm |= 0x80000000;
5883 if (abcdefgh & 0x40) {
5884 imm |= 0x3e000000;
5885 } else {
5886 imm |= 0x40000000;
5888 imm |= (imm << 32);
5891 break;
5894 if (cmode_3_1 != 7 && is_neg) {
5895 imm = ~imm;
5898 tcg_imm = tcg_const_i64(imm);
5899 tcg_rd = new_tmp_a64(s);
5901 for (i = 0; i < 2; i++) {
5902 int foffs = i ? fp_reg_hi_offset(s, rd) : fp_reg_offset(s, rd, MO_64);
5904 if (i == 1 && !is_q) {
5905 /* non-quad ops clear high half of vector */
5906 tcg_gen_movi_i64(tcg_rd, 0);
5907 } else if ((cmode & 0x9) == 0x1 || (cmode & 0xd) == 0x9) {
5908 tcg_gen_ld_i64(tcg_rd, cpu_env, foffs);
5909 if (is_neg) {
5910 /* AND (BIC) */
5911 tcg_gen_and_i64(tcg_rd, tcg_rd, tcg_imm);
5912 } else {
5913 /* ORR */
5914 tcg_gen_or_i64(tcg_rd, tcg_rd, tcg_imm);
5916 } else {
5917 /* MOVI */
5918 tcg_gen_mov_i64(tcg_rd, tcg_imm);
5920 tcg_gen_st_i64(tcg_rd, cpu_env, foffs);
5923 tcg_temp_free_i64(tcg_imm);
5926 /* C3.6.7 AdvSIMD scalar copy
5927 * 31 30 29 28 21 20 16 15 14 11 10 9 5 4 0
5928 * +-----+----+-----------------+------+---+------+---+------+------+
5929 * | 0 1 | op | 1 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 | Rn | Rd |
5930 * +-----+----+-----------------+------+---+------+---+------+------+
5932 static void disas_simd_scalar_copy(DisasContext *s, uint32_t insn)
5934 int rd = extract32(insn, 0, 5);
5935 int rn = extract32(insn, 5, 5);
5936 int imm4 = extract32(insn, 11, 4);
5937 int imm5 = extract32(insn, 16, 5);
5938 int op = extract32(insn, 29, 1);
5940 if (op != 0 || imm4 != 0) {
5941 unallocated_encoding(s);
5942 return;
5945 /* DUP (element, scalar) */
5946 handle_simd_dupes(s, rd, rn, imm5);
5949 /* C3.6.8 AdvSIMD scalar pairwise
5950 * 31 30 29 28 24 23 22 21 17 16 12 11 10 9 5 4 0
5951 * +-----+---+-----------+------+-----------+--------+-----+------+------+
5952 * | 0 1 | U | 1 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 | Rn | Rd |
5953 * +-----+---+-----------+------+-----------+--------+-----+------+------+
5955 static void disas_simd_scalar_pairwise(DisasContext *s, uint32_t insn)
5957 int u = extract32(insn, 29, 1);
5958 int size = extract32(insn, 22, 2);
5959 int opcode = extract32(insn, 12, 5);
5960 int rn = extract32(insn, 5, 5);
5961 int rd = extract32(insn, 0, 5);
5962 TCGv_ptr fpst;
5964 /* For some ops (the FP ones), size[1] is part of the encoding.
5965 * For ADDP strictly it is not but size[1] is always 1 for valid
5966 * encodings.
5968 opcode |= (extract32(size, 1, 1) << 5);
5970 switch (opcode) {
5971 case 0x3b: /* ADDP */
5972 if (u || size != 3) {
5973 unallocated_encoding(s);
5974 return;
5976 if (!fp_access_check(s)) {
5977 return;
5980 TCGV_UNUSED_PTR(fpst);
5981 break;
5982 case 0xc: /* FMAXNMP */
5983 case 0xd: /* FADDP */
5984 case 0xf: /* FMAXP */
5985 case 0x2c: /* FMINNMP */
5986 case 0x2f: /* FMINP */
5987 /* FP op, size[0] is 32 or 64 bit */
5988 if (!u) {
5989 unallocated_encoding(s);
5990 return;
5992 if (!fp_access_check(s)) {
5993 return;
5996 size = extract32(size, 0, 1) ? 3 : 2;
5997 fpst = get_fpstatus_ptr();
5998 break;
5999 default:
6000 unallocated_encoding(s);
6001 return;
6004 if (size == 3) {
6005 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
6006 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
6007 TCGv_i64 tcg_res = tcg_temp_new_i64();
6009 read_vec_element(s, tcg_op1, rn, 0, MO_64);
6010 read_vec_element(s, tcg_op2, rn, 1, MO_64);
6012 switch (opcode) {
6013 case 0x3b: /* ADDP */
6014 tcg_gen_add_i64(tcg_res, tcg_op1, tcg_op2);
6015 break;
6016 case 0xc: /* FMAXNMP */
6017 gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
6018 break;
6019 case 0xd: /* FADDP */
6020 gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
6021 break;
6022 case 0xf: /* FMAXP */
6023 gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
6024 break;
6025 case 0x2c: /* FMINNMP */
6026 gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
6027 break;
6028 case 0x2f: /* FMINP */
6029 gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
6030 break;
6031 default:
6032 g_assert_not_reached();
6035 write_fp_dreg(s, rd, tcg_res);
6037 tcg_temp_free_i64(tcg_op1);
6038 tcg_temp_free_i64(tcg_op2);
6039 tcg_temp_free_i64(tcg_res);
6040 } else {
6041 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
6042 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
6043 TCGv_i32 tcg_res = tcg_temp_new_i32();
6045 read_vec_element_i32(s, tcg_op1, rn, 0, MO_32);
6046 read_vec_element_i32(s, tcg_op2, rn, 1, MO_32);
6048 switch (opcode) {
6049 case 0xc: /* FMAXNMP */
6050 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
6051 break;
6052 case 0xd: /* FADDP */
6053 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
6054 break;
6055 case 0xf: /* FMAXP */
6056 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
6057 break;
6058 case 0x2c: /* FMINNMP */
6059 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
6060 break;
6061 case 0x2f: /* FMINP */
6062 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
6063 break;
6064 default:
6065 g_assert_not_reached();
6068 write_fp_sreg(s, rd, tcg_res);
6070 tcg_temp_free_i32(tcg_op1);
6071 tcg_temp_free_i32(tcg_op2);
6072 tcg_temp_free_i32(tcg_res);
6075 if (!TCGV_IS_UNUSED_PTR(fpst)) {
6076 tcg_temp_free_ptr(fpst);
6081 * Common SSHR[RA]/USHR[RA] - Shift right (optional rounding/accumulate)
6083 * This code is handles the common shifting code and is used by both
6084 * the vector and scalar code.
6086 static void handle_shri_with_rndacc(TCGv_i64 tcg_res, TCGv_i64 tcg_src,
6087 TCGv_i64 tcg_rnd, bool accumulate,
6088 bool is_u, int size, int shift)
6090 bool extended_result = false;
6091 bool round = !TCGV_IS_UNUSED_I64(tcg_rnd);
6092 int ext_lshift = 0;
6093 TCGv_i64 tcg_src_hi;
6095 if (round && size == 3) {
6096 extended_result = true;
6097 ext_lshift = 64 - shift;
6098 tcg_src_hi = tcg_temp_new_i64();
6099 } else if (shift == 64) {
6100 if (!accumulate && is_u) {
6101 /* result is zero */
6102 tcg_gen_movi_i64(tcg_res, 0);
6103 return;
6107 /* Deal with the rounding step */
6108 if (round) {
6109 if (extended_result) {
6110 TCGv_i64 tcg_zero = tcg_const_i64(0);
6111 if (!is_u) {
6112 /* take care of sign extending tcg_res */
6113 tcg_gen_sari_i64(tcg_src_hi, tcg_src, 63);
6114 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
6115 tcg_src, tcg_src_hi,
6116 tcg_rnd, tcg_zero);
6117 } else {
6118 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
6119 tcg_src, tcg_zero,
6120 tcg_rnd, tcg_zero);
6122 tcg_temp_free_i64(tcg_zero);
6123 } else {
6124 tcg_gen_add_i64(tcg_src, tcg_src, tcg_rnd);
6128 /* Now do the shift right */
6129 if (round && extended_result) {
6130 /* extended case, >64 bit precision required */
6131 if (ext_lshift == 0) {
6132 /* special case, only high bits matter */
6133 tcg_gen_mov_i64(tcg_src, tcg_src_hi);
6134 } else {
6135 tcg_gen_shri_i64(tcg_src, tcg_src, shift);
6136 tcg_gen_shli_i64(tcg_src_hi, tcg_src_hi, ext_lshift);
6137 tcg_gen_or_i64(tcg_src, tcg_src, tcg_src_hi);
6139 } else {
6140 if (is_u) {
6141 if (shift == 64) {
6142 /* essentially shifting in 64 zeros */
6143 tcg_gen_movi_i64(tcg_src, 0);
6144 } else {
6145 tcg_gen_shri_i64(tcg_src, tcg_src, shift);
6147 } else {
6148 if (shift == 64) {
6149 /* effectively extending the sign-bit */
6150 tcg_gen_sari_i64(tcg_src, tcg_src, 63);
6151 } else {
6152 tcg_gen_sari_i64(tcg_src, tcg_src, shift);
6157 if (accumulate) {
6158 tcg_gen_add_i64(tcg_res, tcg_res, tcg_src);
6159 } else {
6160 tcg_gen_mov_i64(tcg_res, tcg_src);
6163 if (extended_result) {
6164 tcg_temp_free_i64(tcg_src_hi);
6168 /* Common SHL/SLI - Shift left with an optional insert */
6169 static void handle_shli_with_ins(TCGv_i64 tcg_res, TCGv_i64 tcg_src,
6170 bool insert, int shift)
6172 if (insert) { /* SLI */
6173 tcg_gen_deposit_i64(tcg_res, tcg_res, tcg_src, shift, 64 - shift);
6174 } else { /* SHL */
6175 tcg_gen_shli_i64(tcg_res, tcg_src, shift);
6179 /* SRI: shift right with insert */
6180 static void handle_shri_with_ins(TCGv_i64 tcg_res, TCGv_i64 tcg_src,
6181 int size, int shift)
6183 int esize = 8 << size;
6185 /* shift count same as element size is valid but does nothing;
6186 * special case to avoid potential shift by 64.
6188 if (shift != esize) {
6189 tcg_gen_shri_i64(tcg_src, tcg_src, shift);
6190 tcg_gen_deposit_i64(tcg_res, tcg_res, tcg_src, 0, esize - shift);
6194 /* SSHR[RA]/USHR[RA] - Scalar shift right (optional rounding/accumulate) */
6195 static void handle_scalar_simd_shri(DisasContext *s,
6196 bool is_u, int immh, int immb,
6197 int opcode, int rn, int rd)
6199 const int size = 3;
6200 int immhb = immh << 3 | immb;
6201 int shift = 2 * (8 << size) - immhb;
6202 bool accumulate = false;
6203 bool round = false;
6204 bool insert = false;
6205 TCGv_i64 tcg_rn;
6206 TCGv_i64 tcg_rd;
6207 TCGv_i64 tcg_round;
6209 if (!extract32(immh, 3, 1)) {
6210 unallocated_encoding(s);
6211 return;
6214 if (!fp_access_check(s)) {
6215 return;
6218 switch (opcode) {
6219 case 0x02: /* SSRA / USRA (accumulate) */
6220 accumulate = true;
6221 break;
6222 case 0x04: /* SRSHR / URSHR (rounding) */
6223 round = true;
6224 break;
6225 case 0x06: /* SRSRA / URSRA (accum + rounding) */
6226 accumulate = round = true;
6227 break;
6228 case 0x08: /* SRI */
6229 insert = true;
6230 break;
6233 if (round) {
6234 uint64_t round_const = 1ULL << (shift - 1);
6235 tcg_round = tcg_const_i64(round_const);
6236 } else {
6237 TCGV_UNUSED_I64(tcg_round);
6240 tcg_rn = read_fp_dreg(s, rn);
6241 tcg_rd = (accumulate || insert) ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
6243 if (insert) {
6244 handle_shri_with_ins(tcg_rd, tcg_rn, size, shift);
6245 } else {
6246 handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
6247 accumulate, is_u, size, shift);
6250 write_fp_dreg(s, rd, tcg_rd);
6252 tcg_temp_free_i64(tcg_rn);
6253 tcg_temp_free_i64(tcg_rd);
6254 if (round) {
6255 tcg_temp_free_i64(tcg_round);
6259 /* SHL/SLI - Scalar shift left */
6260 static void handle_scalar_simd_shli(DisasContext *s, bool insert,
6261 int immh, int immb, int opcode,
6262 int rn, int rd)
6264 int size = 32 - clz32(immh) - 1;
6265 int immhb = immh << 3 | immb;
6266 int shift = immhb - (8 << size);
6267 TCGv_i64 tcg_rn = new_tmp_a64(s);
6268 TCGv_i64 tcg_rd = new_tmp_a64(s);
6270 if (!extract32(immh, 3, 1)) {
6271 unallocated_encoding(s);
6272 return;
6275 if (!fp_access_check(s)) {
6276 return;
6279 tcg_rn = read_fp_dreg(s, rn);
6280 tcg_rd = insert ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
6282 handle_shli_with_ins(tcg_rd, tcg_rn, insert, shift);
6284 write_fp_dreg(s, rd, tcg_rd);
6286 tcg_temp_free_i64(tcg_rn);
6287 tcg_temp_free_i64(tcg_rd);
6290 /* SQSHRN/SQSHRUN - Saturating (signed/unsigned) shift right with
6291 * (signed/unsigned) narrowing */
6292 static void handle_vec_simd_sqshrn(DisasContext *s, bool is_scalar, bool is_q,
6293 bool is_u_shift, bool is_u_narrow,
6294 int immh, int immb, int opcode,
6295 int rn, int rd)
6297 int immhb = immh << 3 | immb;
6298 int size = 32 - clz32(immh) - 1;
6299 int esize = 8 << size;
6300 int shift = (2 * esize) - immhb;
6301 int elements = is_scalar ? 1 : (64 / esize);
6302 bool round = extract32(opcode, 0, 1);
6303 TCGMemOp ldop = (size + 1) | (is_u_shift ? 0 : MO_SIGN);
6304 TCGv_i64 tcg_rn, tcg_rd, tcg_round;
6305 TCGv_i32 tcg_rd_narrowed;
6306 TCGv_i64 tcg_final;
6308 static NeonGenNarrowEnvFn * const signed_narrow_fns[4][2] = {
6309 { gen_helper_neon_narrow_sat_s8,
6310 gen_helper_neon_unarrow_sat8 },
6311 { gen_helper_neon_narrow_sat_s16,
6312 gen_helper_neon_unarrow_sat16 },
6313 { gen_helper_neon_narrow_sat_s32,
6314 gen_helper_neon_unarrow_sat32 },
6315 { NULL, NULL },
6317 static NeonGenNarrowEnvFn * const unsigned_narrow_fns[4] = {
6318 gen_helper_neon_narrow_sat_u8,
6319 gen_helper_neon_narrow_sat_u16,
6320 gen_helper_neon_narrow_sat_u32,
6321 NULL
6323 NeonGenNarrowEnvFn *narrowfn;
6325 int i;
6327 assert(size < 4);
6329 if (extract32(immh, 3, 1)) {
6330 unallocated_encoding(s);
6331 return;
6334 if (!fp_access_check(s)) {
6335 return;
6338 if (is_u_shift) {
6339 narrowfn = unsigned_narrow_fns[size];
6340 } else {
6341 narrowfn = signed_narrow_fns[size][is_u_narrow ? 1 : 0];
6344 tcg_rn = tcg_temp_new_i64();
6345 tcg_rd = tcg_temp_new_i64();
6346 tcg_rd_narrowed = tcg_temp_new_i32();
6347 tcg_final = tcg_const_i64(0);
6349 if (round) {
6350 uint64_t round_const = 1ULL << (shift - 1);
6351 tcg_round = tcg_const_i64(round_const);
6352 } else {
6353 TCGV_UNUSED_I64(tcg_round);
6356 for (i = 0; i < elements; i++) {
6357 read_vec_element(s, tcg_rn, rn, i, ldop);
6358 handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
6359 false, is_u_shift, size+1, shift);
6360 narrowfn(tcg_rd_narrowed, cpu_env, tcg_rd);
6361 tcg_gen_extu_i32_i64(tcg_rd, tcg_rd_narrowed);
6362 tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
6365 if (!is_q) {
6366 clear_vec_high(s, rd);
6367 write_vec_element(s, tcg_final, rd, 0, MO_64);
6368 } else {
6369 write_vec_element(s, tcg_final, rd, 1, MO_64);
6372 if (round) {
6373 tcg_temp_free_i64(tcg_round);
6375 tcg_temp_free_i64(tcg_rn);
6376 tcg_temp_free_i64(tcg_rd);
6377 tcg_temp_free_i32(tcg_rd_narrowed);
6378 tcg_temp_free_i64(tcg_final);
6379 return;
6382 /* SQSHLU, UQSHL, SQSHL: saturating left shifts */
6383 static void handle_simd_qshl(DisasContext *s, bool scalar, bool is_q,
6384 bool src_unsigned, bool dst_unsigned,
6385 int immh, int immb, int rn, int rd)
6387 int immhb = immh << 3 | immb;
6388 int size = 32 - clz32(immh) - 1;
6389 int shift = immhb - (8 << size);
6390 int pass;
6392 assert(immh != 0);
6393 assert(!(scalar && is_q));
6395 if (!scalar) {
6396 if (!is_q && extract32(immh, 3, 1)) {
6397 unallocated_encoding(s);
6398 return;
6401 /* Since we use the variable-shift helpers we must
6402 * replicate the shift count into each element of
6403 * the tcg_shift value.
6405 switch (size) {
6406 case 0:
6407 shift |= shift << 8;
6408 /* fall through */
6409 case 1:
6410 shift |= shift << 16;
6411 break;
6412 case 2:
6413 case 3:
6414 break;
6415 default:
6416 g_assert_not_reached();
6420 if (!fp_access_check(s)) {
6421 return;
6424 if (size == 3) {
6425 TCGv_i64 tcg_shift = tcg_const_i64(shift);
6426 static NeonGenTwo64OpEnvFn * const fns[2][2] = {
6427 { gen_helper_neon_qshl_s64, gen_helper_neon_qshlu_s64 },
6428 { NULL, gen_helper_neon_qshl_u64 },
6430 NeonGenTwo64OpEnvFn *genfn = fns[src_unsigned][dst_unsigned];
6431 int maxpass = is_q ? 2 : 1;
6433 for (pass = 0; pass < maxpass; pass++) {
6434 TCGv_i64 tcg_op = tcg_temp_new_i64();
6436 read_vec_element(s, tcg_op, rn, pass, MO_64);
6437 genfn(tcg_op, cpu_env, tcg_op, tcg_shift);
6438 write_vec_element(s, tcg_op, rd, pass, MO_64);
6440 tcg_temp_free_i64(tcg_op);
6442 tcg_temp_free_i64(tcg_shift);
6444 if (!is_q) {
6445 clear_vec_high(s, rd);
6447 } else {
6448 TCGv_i32 tcg_shift = tcg_const_i32(shift);
6449 static NeonGenTwoOpEnvFn * const fns[2][2][3] = {
6451 { gen_helper_neon_qshl_s8,
6452 gen_helper_neon_qshl_s16,
6453 gen_helper_neon_qshl_s32 },
6454 { gen_helper_neon_qshlu_s8,
6455 gen_helper_neon_qshlu_s16,
6456 gen_helper_neon_qshlu_s32 }
6457 }, {
6458 { NULL, NULL, NULL },
6459 { gen_helper_neon_qshl_u8,
6460 gen_helper_neon_qshl_u16,
6461 gen_helper_neon_qshl_u32 }
6464 NeonGenTwoOpEnvFn *genfn = fns[src_unsigned][dst_unsigned][size];
6465 TCGMemOp memop = scalar ? size : MO_32;
6466 int maxpass = scalar ? 1 : is_q ? 4 : 2;
6468 for (pass = 0; pass < maxpass; pass++) {
6469 TCGv_i32 tcg_op = tcg_temp_new_i32();
6471 read_vec_element_i32(s, tcg_op, rn, pass, memop);
6472 genfn(tcg_op, cpu_env, tcg_op, tcg_shift);
6473 if (scalar) {
6474 switch (size) {
6475 case 0:
6476 tcg_gen_ext8u_i32(tcg_op, tcg_op);
6477 break;
6478 case 1:
6479 tcg_gen_ext16u_i32(tcg_op, tcg_op);
6480 break;
6481 case 2:
6482 break;
6483 default:
6484 g_assert_not_reached();
6486 write_fp_sreg(s, rd, tcg_op);
6487 } else {
6488 write_vec_element_i32(s, tcg_op, rd, pass, MO_32);
6491 tcg_temp_free_i32(tcg_op);
6493 tcg_temp_free_i32(tcg_shift);
6495 if (!is_q && !scalar) {
6496 clear_vec_high(s, rd);
6501 /* Common vector code for handling integer to FP conversion */
6502 static void handle_simd_intfp_conv(DisasContext *s, int rd, int rn,
6503 int elements, int is_signed,
6504 int fracbits, int size)
6506 bool is_double = size == 3 ? true : false;
6507 TCGv_ptr tcg_fpst = get_fpstatus_ptr();
6508 TCGv_i32 tcg_shift = tcg_const_i32(fracbits);
6509 TCGv_i64 tcg_int = tcg_temp_new_i64();
6510 TCGMemOp mop = size | (is_signed ? MO_SIGN : 0);
6511 int pass;
6513 for (pass = 0; pass < elements; pass++) {
6514 read_vec_element(s, tcg_int, rn, pass, mop);
6516 if (is_double) {
6517 TCGv_i64 tcg_double = tcg_temp_new_i64();
6518 if (is_signed) {
6519 gen_helper_vfp_sqtod(tcg_double, tcg_int,
6520 tcg_shift, tcg_fpst);
6521 } else {
6522 gen_helper_vfp_uqtod(tcg_double, tcg_int,
6523 tcg_shift, tcg_fpst);
6525 if (elements == 1) {
6526 write_fp_dreg(s, rd, tcg_double);
6527 } else {
6528 write_vec_element(s, tcg_double, rd, pass, MO_64);
6530 tcg_temp_free_i64(tcg_double);
6531 } else {
6532 TCGv_i32 tcg_single = tcg_temp_new_i32();
6533 if (is_signed) {
6534 gen_helper_vfp_sqtos(tcg_single, tcg_int,
6535 tcg_shift, tcg_fpst);
6536 } else {
6537 gen_helper_vfp_uqtos(tcg_single, tcg_int,
6538 tcg_shift, tcg_fpst);
6540 if (elements == 1) {
6541 write_fp_sreg(s, rd, tcg_single);
6542 } else {
6543 write_vec_element_i32(s, tcg_single, rd, pass, MO_32);
6545 tcg_temp_free_i32(tcg_single);
6549 if (!is_double && elements == 2) {
6550 clear_vec_high(s, rd);
6553 tcg_temp_free_i64(tcg_int);
6554 tcg_temp_free_ptr(tcg_fpst);
6555 tcg_temp_free_i32(tcg_shift);
6558 /* UCVTF/SCVTF - Integer to FP conversion */
6559 static void handle_simd_shift_intfp_conv(DisasContext *s, bool is_scalar,
6560 bool is_q, bool is_u,
6561 int immh, int immb, int opcode,
6562 int rn, int rd)
6564 bool is_double = extract32(immh, 3, 1);
6565 int size = is_double ? MO_64 : MO_32;
6566 int elements;
6567 int immhb = immh << 3 | immb;
6568 int fracbits = (is_double ? 128 : 64) - immhb;
6570 if (!extract32(immh, 2, 2)) {
6571 unallocated_encoding(s);
6572 return;
6575 if (is_scalar) {
6576 elements = 1;
6577 } else {
6578 elements = is_double ? 2 : is_q ? 4 : 2;
6579 if (is_double && !is_q) {
6580 unallocated_encoding(s);
6581 return;
6585 if (!fp_access_check(s)) {
6586 return;
6589 /* immh == 0 would be a failure of the decode logic */
6590 g_assert(immh);
6592 handle_simd_intfp_conv(s, rd, rn, elements, !is_u, fracbits, size);
6595 /* FCVTZS, FVCVTZU - FP to fixedpoint conversion */
6596 static void handle_simd_shift_fpint_conv(DisasContext *s, bool is_scalar,
6597 bool is_q, bool is_u,
6598 int immh, int immb, int rn, int rd)
6600 bool is_double = extract32(immh, 3, 1);
6601 int immhb = immh << 3 | immb;
6602 int fracbits = (is_double ? 128 : 64) - immhb;
6603 int pass;
6604 TCGv_ptr tcg_fpstatus;
6605 TCGv_i32 tcg_rmode, tcg_shift;
6607 if (!extract32(immh, 2, 2)) {
6608 unallocated_encoding(s);
6609 return;
6612 if (!is_scalar && !is_q && is_double) {
6613 unallocated_encoding(s);
6614 return;
6617 if (!fp_access_check(s)) {
6618 return;
6621 assert(!(is_scalar && is_q));
6623 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(FPROUNDING_ZERO));
6624 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
6625 tcg_fpstatus = get_fpstatus_ptr();
6626 tcg_shift = tcg_const_i32(fracbits);
6628 if (is_double) {
6629 int maxpass = is_scalar ? 1 : 2;
6631 for (pass = 0; pass < maxpass; pass++) {
6632 TCGv_i64 tcg_op = tcg_temp_new_i64();
6634 read_vec_element(s, tcg_op, rn, pass, MO_64);
6635 if (is_u) {
6636 gen_helper_vfp_touqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
6637 } else {
6638 gen_helper_vfp_tosqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
6640 write_vec_element(s, tcg_op, rd, pass, MO_64);
6641 tcg_temp_free_i64(tcg_op);
6643 if (!is_q) {
6644 clear_vec_high(s, rd);
6646 } else {
6647 int maxpass = is_scalar ? 1 : is_q ? 4 : 2;
6648 for (pass = 0; pass < maxpass; pass++) {
6649 TCGv_i32 tcg_op = tcg_temp_new_i32();
6651 read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
6652 if (is_u) {
6653 gen_helper_vfp_touls(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
6654 } else {
6655 gen_helper_vfp_tosls(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
6657 if (is_scalar) {
6658 write_fp_sreg(s, rd, tcg_op);
6659 } else {
6660 write_vec_element_i32(s, tcg_op, rd, pass, MO_32);
6662 tcg_temp_free_i32(tcg_op);
6664 if (!is_q && !is_scalar) {
6665 clear_vec_high(s, rd);
6669 tcg_temp_free_ptr(tcg_fpstatus);
6670 tcg_temp_free_i32(tcg_shift);
6671 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
6672 tcg_temp_free_i32(tcg_rmode);
6675 /* C3.6.9 AdvSIMD scalar shift by immediate
6676 * 31 30 29 28 23 22 19 18 16 15 11 10 9 5 4 0
6677 * +-----+---+-------------+------+------+--------+---+------+------+
6678 * | 0 1 | U | 1 1 1 1 1 0 | immh | immb | opcode | 1 | Rn | Rd |
6679 * +-----+---+-------------+------+------+--------+---+------+------+
6681 * This is the scalar version so it works on a fixed sized registers
6683 static void disas_simd_scalar_shift_imm(DisasContext *s, uint32_t insn)
6685 int rd = extract32(insn, 0, 5);
6686 int rn = extract32(insn, 5, 5);
6687 int opcode = extract32(insn, 11, 5);
6688 int immb = extract32(insn, 16, 3);
6689 int immh = extract32(insn, 19, 4);
6690 bool is_u = extract32(insn, 29, 1);
6692 if (immh == 0) {
6693 unallocated_encoding(s);
6694 return;
6697 switch (opcode) {
6698 case 0x08: /* SRI */
6699 if (!is_u) {
6700 unallocated_encoding(s);
6701 return;
6703 /* fall through */
6704 case 0x00: /* SSHR / USHR */
6705 case 0x02: /* SSRA / USRA */
6706 case 0x04: /* SRSHR / URSHR */
6707 case 0x06: /* SRSRA / URSRA */
6708 handle_scalar_simd_shri(s, is_u, immh, immb, opcode, rn, rd);
6709 break;
6710 case 0x0a: /* SHL / SLI */
6711 handle_scalar_simd_shli(s, is_u, immh, immb, opcode, rn, rd);
6712 break;
6713 case 0x1c: /* SCVTF, UCVTF */
6714 handle_simd_shift_intfp_conv(s, true, false, is_u, immh, immb,
6715 opcode, rn, rd);
6716 break;
6717 case 0x10: /* SQSHRUN, SQSHRUN2 */
6718 case 0x11: /* SQRSHRUN, SQRSHRUN2 */
6719 if (!is_u) {
6720 unallocated_encoding(s);
6721 return;
6723 handle_vec_simd_sqshrn(s, true, false, false, true,
6724 immh, immb, opcode, rn, rd);
6725 break;
6726 case 0x12: /* SQSHRN, SQSHRN2, UQSHRN */
6727 case 0x13: /* SQRSHRN, SQRSHRN2, UQRSHRN, UQRSHRN2 */
6728 handle_vec_simd_sqshrn(s, true, false, is_u, is_u,
6729 immh, immb, opcode, rn, rd);
6730 break;
6731 case 0xc: /* SQSHLU */
6732 if (!is_u) {
6733 unallocated_encoding(s);
6734 return;
6736 handle_simd_qshl(s, true, false, false, true, immh, immb, rn, rd);
6737 break;
6738 case 0xe: /* SQSHL, UQSHL */
6739 handle_simd_qshl(s, true, false, is_u, is_u, immh, immb, rn, rd);
6740 break;
6741 case 0x1f: /* FCVTZS, FCVTZU */
6742 handle_simd_shift_fpint_conv(s, true, false, is_u, immh, immb, rn, rd);
6743 break;
6744 default:
6745 unallocated_encoding(s);
6746 break;
6750 /* C3.6.10 AdvSIMD scalar three different
6751 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 0
6752 * +-----+---+-----------+------+---+------+--------+-----+------+------+
6753 * | 0 1 | U | 1 1 1 1 0 | size | 1 | Rm | opcode | 0 0 | Rn | Rd |
6754 * +-----+---+-----------+------+---+------+--------+-----+------+------+
6756 static void disas_simd_scalar_three_reg_diff(DisasContext *s, uint32_t insn)
6758 bool is_u = extract32(insn, 29, 1);
6759 int size = extract32(insn, 22, 2);
6760 int opcode = extract32(insn, 12, 4);
6761 int rm = extract32(insn, 16, 5);
6762 int rn = extract32(insn, 5, 5);
6763 int rd = extract32(insn, 0, 5);
6765 if (is_u) {
6766 unallocated_encoding(s);
6767 return;
6770 switch (opcode) {
6771 case 0x9: /* SQDMLAL, SQDMLAL2 */
6772 case 0xb: /* SQDMLSL, SQDMLSL2 */
6773 case 0xd: /* SQDMULL, SQDMULL2 */
6774 if (size == 0 || size == 3) {
6775 unallocated_encoding(s);
6776 return;
6778 break;
6779 default:
6780 unallocated_encoding(s);
6781 return;
6784 if (!fp_access_check(s)) {
6785 return;
6788 if (size == 2) {
6789 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
6790 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
6791 TCGv_i64 tcg_res = tcg_temp_new_i64();
6793 read_vec_element(s, tcg_op1, rn, 0, MO_32 | MO_SIGN);
6794 read_vec_element(s, tcg_op2, rm, 0, MO_32 | MO_SIGN);
6796 tcg_gen_mul_i64(tcg_res, tcg_op1, tcg_op2);
6797 gen_helper_neon_addl_saturate_s64(tcg_res, cpu_env, tcg_res, tcg_res);
6799 switch (opcode) {
6800 case 0xd: /* SQDMULL, SQDMULL2 */
6801 break;
6802 case 0xb: /* SQDMLSL, SQDMLSL2 */
6803 tcg_gen_neg_i64(tcg_res, tcg_res);
6804 /* fall through */
6805 case 0x9: /* SQDMLAL, SQDMLAL2 */
6806 read_vec_element(s, tcg_op1, rd, 0, MO_64);
6807 gen_helper_neon_addl_saturate_s64(tcg_res, cpu_env,
6808 tcg_res, tcg_op1);
6809 break;
6810 default:
6811 g_assert_not_reached();
6814 write_fp_dreg(s, rd, tcg_res);
6816 tcg_temp_free_i64(tcg_op1);
6817 tcg_temp_free_i64(tcg_op2);
6818 tcg_temp_free_i64(tcg_res);
6819 } else {
6820 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
6821 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
6822 TCGv_i64 tcg_res = tcg_temp_new_i64();
6824 read_vec_element_i32(s, tcg_op1, rn, 0, MO_16);
6825 read_vec_element_i32(s, tcg_op2, rm, 0, MO_16);
6827 gen_helper_neon_mull_s16(tcg_res, tcg_op1, tcg_op2);
6828 gen_helper_neon_addl_saturate_s32(tcg_res, cpu_env, tcg_res, tcg_res);
6830 switch (opcode) {
6831 case 0xd: /* SQDMULL, SQDMULL2 */
6832 break;
6833 case 0xb: /* SQDMLSL, SQDMLSL2 */
6834 gen_helper_neon_negl_u32(tcg_res, tcg_res);
6835 /* fall through */
6836 case 0x9: /* SQDMLAL, SQDMLAL2 */
6838 TCGv_i64 tcg_op3 = tcg_temp_new_i64();
6839 read_vec_element(s, tcg_op3, rd, 0, MO_32);
6840 gen_helper_neon_addl_saturate_s32(tcg_res, cpu_env,
6841 tcg_res, tcg_op3);
6842 tcg_temp_free_i64(tcg_op3);
6843 break;
6845 default:
6846 g_assert_not_reached();
6849 tcg_gen_ext32u_i64(tcg_res, tcg_res);
6850 write_fp_dreg(s, rd, tcg_res);
6852 tcg_temp_free_i32(tcg_op1);
6853 tcg_temp_free_i32(tcg_op2);
6854 tcg_temp_free_i64(tcg_res);
6858 static void handle_3same_64(DisasContext *s, int opcode, bool u,
6859 TCGv_i64 tcg_rd, TCGv_i64 tcg_rn, TCGv_i64 tcg_rm)
6861 /* Handle 64x64->64 opcodes which are shared between the scalar
6862 * and vector 3-same groups. We cover every opcode where size == 3
6863 * is valid in either the three-reg-same (integer, not pairwise)
6864 * or scalar-three-reg-same groups. (Some opcodes are not yet
6865 * implemented.)
6867 TCGCond cond;
6869 switch (opcode) {
6870 case 0x1: /* SQADD */
6871 if (u) {
6872 gen_helper_neon_qadd_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
6873 } else {
6874 gen_helper_neon_qadd_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
6876 break;
6877 case 0x5: /* SQSUB */
6878 if (u) {
6879 gen_helper_neon_qsub_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
6880 } else {
6881 gen_helper_neon_qsub_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
6883 break;
6884 case 0x6: /* CMGT, CMHI */
6885 /* 64 bit integer comparison, result = test ? (2^64 - 1) : 0.
6886 * We implement this using setcond (test) and then negating.
6888 cond = u ? TCG_COND_GTU : TCG_COND_GT;
6889 do_cmop:
6890 tcg_gen_setcond_i64(cond, tcg_rd, tcg_rn, tcg_rm);
6891 tcg_gen_neg_i64(tcg_rd, tcg_rd);
6892 break;
6893 case 0x7: /* CMGE, CMHS */
6894 cond = u ? TCG_COND_GEU : TCG_COND_GE;
6895 goto do_cmop;
6896 case 0x11: /* CMTST, CMEQ */
6897 if (u) {
6898 cond = TCG_COND_EQ;
6899 goto do_cmop;
6901 /* CMTST : test is "if (X & Y != 0)". */
6902 tcg_gen_and_i64(tcg_rd, tcg_rn, tcg_rm);
6903 tcg_gen_setcondi_i64(TCG_COND_NE, tcg_rd, tcg_rd, 0);
6904 tcg_gen_neg_i64(tcg_rd, tcg_rd);
6905 break;
6906 case 0x8: /* SSHL, USHL */
6907 if (u) {
6908 gen_helper_neon_shl_u64(tcg_rd, tcg_rn, tcg_rm);
6909 } else {
6910 gen_helper_neon_shl_s64(tcg_rd, tcg_rn, tcg_rm);
6912 break;
6913 case 0x9: /* SQSHL, UQSHL */
6914 if (u) {
6915 gen_helper_neon_qshl_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
6916 } else {
6917 gen_helper_neon_qshl_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
6919 break;
6920 case 0xa: /* SRSHL, URSHL */
6921 if (u) {
6922 gen_helper_neon_rshl_u64(tcg_rd, tcg_rn, tcg_rm);
6923 } else {
6924 gen_helper_neon_rshl_s64(tcg_rd, tcg_rn, tcg_rm);
6926 break;
6927 case 0xb: /* SQRSHL, UQRSHL */
6928 if (u) {
6929 gen_helper_neon_qrshl_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
6930 } else {
6931 gen_helper_neon_qrshl_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
6933 break;
6934 case 0x10: /* ADD, SUB */
6935 if (u) {
6936 tcg_gen_sub_i64(tcg_rd, tcg_rn, tcg_rm);
6937 } else {
6938 tcg_gen_add_i64(tcg_rd, tcg_rn, tcg_rm);
6940 break;
6941 default:
6942 g_assert_not_reached();
6946 /* Handle the 3-same-operands float operations; shared by the scalar
6947 * and vector encodings. The caller must filter out any encodings
6948 * not allocated for the encoding it is dealing with.
6950 static void handle_3same_float(DisasContext *s, int size, int elements,
6951 int fpopcode, int rd, int rn, int rm)
6953 int pass;
6954 TCGv_ptr fpst = get_fpstatus_ptr();
6956 for (pass = 0; pass < elements; pass++) {
6957 if (size) {
6958 /* Double */
6959 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
6960 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
6961 TCGv_i64 tcg_res = tcg_temp_new_i64();
6963 read_vec_element(s, tcg_op1, rn, pass, MO_64);
6964 read_vec_element(s, tcg_op2, rm, pass, MO_64);
6966 switch (fpopcode) {
6967 case 0x39: /* FMLS */
6968 /* As usual for ARM, separate negation for fused multiply-add */
6969 gen_helper_vfp_negd(tcg_op1, tcg_op1);
6970 /* fall through */
6971 case 0x19: /* FMLA */
6972 read_vec_element(s, tcg_res, rd, pass, MO_64);
6973 gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2,
6974 tcg_res, fpst);
6975 break;
6976 case 0x18: /* FMAXNM */
6977 gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
6978 break;
6979 case 0x1a: /* FADD */
6980 gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
6981 break;
6982 case 0x1b: /* FMULX */
6983 gen_helper_vfp_mulxd(tcg_res, tcg_op1, tcg_op2, fpst);
6984 break;
6985 case 0x1c: /* FCMEQ */
6986 gen_helper_neon_ceq_f64(tcg_res, tcg_op1, tcg_op2, fpst);
6987 break;
6988 case 0x1e: /* FMAX */
6989 gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
6990 break;
6991 case 0x1f: /* FRECPS */
6992 gen_helper_recpsf_f64(tcg_res, tcg_op1, tcg_op2, fpst);
6993 break;
6994 case 0x38: /* FMINNM */
6995 gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
6996 break;
6997 case 0x3a: /* FSUB */
6998 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
6999 break;
7000 case 0x3e: /* FMIN */
7001 gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
7002 break;
7003 case 0x3f: /* FRSQRTS */
7004 gen_helper_rsqrtsf_f64(tcg_res, tcg_op1, tcg_op2, fpst);
7005 break;
7006 case 0x5b: /* FMUL */
7007 gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
7008 break;
7009 case 0x5c: /* FCMGE */
7010 gen_helper_neon_cge_f64(tcg_res, tcg_op1, tcg_op2, fpst);
7011 break;
7012 case 0x5d: /* FACGE */
7013 gen_helper_neon_acge_f64(tcg_res, tcg_op1, tcg_op2, fpst);
7014 break;
7015 case 0x5f: /* FDIV */
7016 gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
7017 break;
7018 case 0x7a: /* FABD */
7019 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
7020 gen_helper_vfp_absd(tcg_res, tcg_res);
7021 break;
7022 case 0x7c: /* FCMGT */
7023 gen_helper_neon_cgt_f64(tcg_res, tcg_op1, tcg_op2, fpst);
7024 break;
7025 case 0x7d: /* FACGT */
7026 gen_helper_neon_acgt_f64(tcg_res, tcg_op1, tcg_op2, fpst);
7027 break;
7028 default:
7029 g_assert_not_reached();
7032 write_vec_element(s, tcg_res, rd, pass, MO_64);
7034 tcg_temp_free_i64(tcg_res);
7035 tcg_temp_free_i64(tcg_op1);
7036 tcg_temp_free_i64(tcg_op2);
7037 } else {
7038 /* Single */
7039 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
7040 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
7041 TCGv_i32 tcg_res = tcg_temp_new_i32();
7043 read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
7044 read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
7046 switch (fpopcode) {
7047 case 0x39: /* FMLS */
7048 /* As usual for ARM, separate negation for fused multiply-add */
7049 gen_helper_vfp_negs(tcg_op1, tcg_op1);
7050 /* fall through */
7051 case 0x19: /* FMLA */
7052 read_vec_element_i32(s, tcg_res, rd, pass, MO_32);
7053 gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2,
7054 tcg_res, fpst);
7055 break;
7056 case 0x1a: /* FADD */
7057 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
7058 break;
7059 case 0x1b: /* FMULX */
7060 gen_helper_vfp_mulxs(tcg_res, tcg_op1, tcg_op2, fpst);
7061 break;
7062 case 0x1c: /* FCMEQ */
7063 gen_helper_neon_ceq_f32(tcg_res, tcg_op1, tcg_op2, fpst);
7064 break;
7065 case 0x1e: /* FMAX */
7066 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
7067 break;
7068 case 0x1f: /* FRECPS */
7069 gen_helper_recpsf_f32(tcg_res, tcg_op1, tcg_op2, fpst);
7070 break;
7071 case 0x18: /* FMAXNM */
7072 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
7073 break;
7074 case 0x38: /* FMINNM */
7075 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
7076 break;
7077 case 0x3a: /* FSUB */
7078 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
7079 break;
7080 case 0x3e: /* FMIN */
7081 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
7082 break;
7083 case 0x3f: /* FRSQRTS */
7084 gen_helper_rsqrtsf_f32(tcg_res, tcg_op1, tcg_op2, fpst);
7085 break;
7086 case 0x5b: /* FMUL */
7087 gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
7088 break;
7089 case 0x5c: /* FCMGE */
7090 gen_helper_neon_cge_f32(tcg_res, tcg_op1, tcg_op2, fpst);
7091 break;
7092 case 0x5d: /* FACGE */
7093 gen_helper_neon_acge_f32(tcg_res, tcg_op1, tcg_op2, fpst);
7094 break;
7095 case 0x5f: /* FDIV */
7096 gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
7097 break;
7098 case 0x7a: /* FABD */
7099 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
7100 gen_helper_vfp_abss(tcg_res, tcg_res);
7101 break;
7102 case 0x7c: /* FCMGT */
7103 gen_helper_neon_cgt_f32(tcg_res, tcg_op1, tcg_op2, fpst);
7104 break;
7105 case 0x7d: /* FACGT */
7106 gen_helper_neon_acgt_f32(tcg_res, tcg_op1, tcg_op2, fpst);
7107 break;
7108 default:
7109 g_assert_not_reached();
7112 if (elements == 1) {
7113 /* scalar single so clear high part */
7114 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
7116 tcg_gen_extu_i32_i64(tcg_tmp, tcg_res);
7117 write_vec_element(s, tcg_tmp, rd, pass, MO_64);
7118 tcg_temp_free_i64(tcg_tmp);
7119 } else {
7120 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
7123 tcg_temp_free_i32(tcg_res);
7124 tcg_temp_free_i32(tcg_op1);
7125 tcg_temp_free_i32(tcg_op2);
7129 tcg_temp_free_ptr(fpst);
7131 if ((elements << size) < 4) {
7132 /* scalar, or non-quad vector op */
7133 clear_vec_high(s, rd);
7137 /* C3.6.11 AdvSIMD scalar three same
7138 * 31 30 29 28 24 23 22 21 20 16 15 11 10 9 5 4 0
7139 * +-----+---+-----------+------+---+------+--------+---+------+------+
7140 * | 0 1 | U | 1 1 1 1 0 | size | 1 | Rm | opcode | 1 | Rn | Rd |
7141 * +-----+---+-----------+------+---+------+--------+---+------+------+
7143 static void disas_simd_scalar_three_reg_same(DisasContext *s, uint32_t insn)
7145 int rd = extract32(insn, 0, 5);
7146 int rn = extract32(insn, 5, 5);
7147 int opcode = extract32(insn, 11, 5);
7148 int rm = extract32(insn, 16, 5);
7149 int size = extract32(insn, 22, 2);
7150 bool u = extract32(insn, 29, 1);
7151 TCGv_i64 tcg_rd;
7153 if (opcode >= 0x18) {
7154 /* Floating point: U, size[1] and opcode indicate operation */
7155 int fpopcode = opcode | (extract32(size, 1, 1) << 5) | (u << 6);
7156 switch (fpopcode) {
7157 case 0x1b: /* FMULX */
7158 case 0x1f: /* FRECPS */
7159 case 0x3f: /* FRSQRTS */
7160 case 0x5d: /* FACGE */
7161 case 0x7d: /* FACGT */
7162 case 0x1c: /* FCMEQ */
7163 case 0x5c: /* FCMGE */
7164 case 0x7c: /* FCMGT */
7165 case 0x7a: /* FABD */
7166 break;
7167 default:
7168 unallocated_encoding(s);
7169 return;
7172 if (!fp_access_check(s)) {
7173 return;
7176 handle_3same_float(s, extract32(size, 0, 1), 1, fpopcode, rd, rn, rm);
7177 return;
7180 switch (opcode) {
7181 case 0x1: /* SQADD, UQADD */
7182 case 0x5: /* SQSUB, UQSUB */
7183 case 0x9: /* SQSHL, UQSHL */
7184 case 0xb: /* SQRSHL, UQRSHL */
7185 break;
7186 case 0x8: /* SSHL, USHL */
7187 case 0xa: /* SRSHL, URSHL */
7188 case 0x6: /* CMGT, CMHI */
7189 case 0x7: /* CMGE, CMHS */
7190 case 0x11: /* CMTST, CMEQ */
7191 case 0x10: /* ADD, SUB (vector) */
7192 if (size != 3) {
7193 unallocated_encoding(s);
7194 return;
7196 break;
7197 case 0x16: /* SQDMULH, SQRDMULH (vector) */
7198 if (size != 1 && size != 2) {
7199 unallocated_encoding(s);
7200 return;
7202 break;
7203 default:
7204 unallocated_encoding(s);
7205 return;
7208 if (!fp_access_check(s)) {
7209 return;
7212 tcg_rd = tcg_temp_new_i64();
7214 if (size == 3) {
7215 TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
7216 TCGv_i64 tcg_rm = read_fp_dreg(s, rm);
7218 handle_3same_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rm);
7219 tcg_temp_free_i64(tcg_rn);
7220 tcg_temp_free_i64(tcg_rm);
7221 } else {
7222 /* Do a single operation on the lowest element in the vector.
7223 * We use the standard Neon helpers and rely on 0 OP 0 == 0 with
7224 * no side effects for all these operations.
7225 * OPTME: special-purpose helpers would avoid doing some
7226 * unnecessary work in the helper for the 8 and 16 bit cases.
7228 NeonGenTwoOpEnvFn *genenvfn;
7229 TCGv_i32 tcg_rn = tcg_temp_new_i32();
7230 TCGv_i32 tcg_rm = tcg_temp_new_i32();
7231 TCGv_i32 tcg_rd32 = tcg_temp_new_i32();
7233 read_vec_element_i32(s, tcg_rn, rn, 0, size);
7234 read_vec_element_i32(s, tcg_rm, rm, 0, size);
7236 switch (opcode) {
7237 case 0x1: /* SQADD, UQADD */
7239 static NeonGenTwoOpEnvFn * const fns[3][2] = {
7240 { gen_helper_neon_qadd_s8, gen_helper_neon_qadd_u8 },
7241 { gen_helper_neon_qadd_s16, gen_helper_neon_qadd_u16 },
7242 { gen_helper_neon_qadd_s32, gen_helper_neon_qadd_u32 },
7244 genenvfn = fns[size][u];
7245 break;
7247 case 0x5: /* SQSUB, UQSUB */
7249 static NeonGenTwoOpEnvFn * const fns[3][2] = {
7250 { gen_helper_neon_qsub_s8, gen_helper_neon_qsub_u8 },
7251 { gen_helper_neon_qsub_s16, gen_helper_neon_qsub_u16 },
7252 { gen_helper_neon_qsub_s32, gen_helper_neon_qsub_u32 },
7254 genenvfn = fns[size][u];
7255 break;
7257 case 0x9: /* SQSHL, UQSHL */
7259 static NeonGenTwoOpEnvFn * const fns[3][2] = {
7260 { gen_helper_neon_qshl_s8, gen_helper_neon_qshl_u8 },
7261 { gen_helper_neon_qshl_s16, gen_helper_neon_qshl_u16 },
7262 { gen_helper_neon_qshl_s32, gen_helper_neon_qshl_u32 },
7264 genenvfn = fns[size][u];
7265 break;
7267 case 0xb: /* SQRSHL, UQRSHL */
7269 static NeonGenTwoOpEnvFn * const fns[3][2] = {
7270 { gen_helper_neon_qrshl_s8, gen_helper_neon_qrshl_u8 },
7271 { gen_helper_neon_qrshl_s16, gen_helper_neon_qrshl_u16 },
7272 { gen_helper_neon_qrshl_s32, gen_helper_neon_qrshl_u32 },
7274 genenvfn = fns[size][u];
7275 break;
7277 case 0x16: /* SQDMULH, SQRDMULH */
7279 static NeonGenTwoOpEnvFn * const fns[2][2] = {
7280 { gen_helper_neon_qdmulh_s16, gen_helper_neon_qrdmulh_s16 },
7281 { gen_helper_neon_qdmulh_s32, gen_helper_neon_qrdmulh_s32 },
7283 assert(size == 1 || size == 2);
7284 genenvfn = fns[size - 1][u];
7285 break;
7287 default:
7288 g_assert_not_reached();
7291 genenvfn(tcg_rd32, cpu_env, tcg_rn, tcg_rm);
7292 tcg_gen_extu_i32_i64(tcg_rd, tcg_rd32);
7293 tcg_temp_free_i32(tcg_rd32);
7294 tcg_temp_free_i32(tcg_rn);
7295 tcg_temp_free_i32(tcg_rm);
7298 write_fp_dreg(s, rd, tcg_rd);
7300 tcg_temp_free_i64(tcg_rd);
7303 static void handle_2misc_64(DisasContext *s, int opcode, bool u,
7304 TCGv_i64 tcg_rd, TCGv_i64 tcg_rn,
7305 TCGv_i32 tcg_rmode, TCGv_ptr tcg_fpstatus)
7307 /* Handle 64->64 opcodes which are shared between the scalar and
7308 * vector 2-reg-misc groups. We cover every integer opcode where size == 3
7309 * is valid in either group and also the double-precision fp ops.
7310 * The caller only need provide tcg_rmode and tcg_fpstatus if the op
7311 * requires them.
7313 TCGCond cond;
7315 switch (opcode) {
7316 case 0x4: /* CLS, CLZ */
7317 if (u) {
7318 gen_helper_clz64(tcg_rd, tcg_rn);
7319 } else {
7320 gen_helper_cls64(tcg_rd, tcg_rn);
7322 break;
7323 case 0x5: /* NOT */
7324 /* This opcode is shared with CNT and RBIT but we have earlier
7325 * enforced that size == 3 if and only if this is the NOT insn.
7327 tcg_gen_not_i64(tcg_rd, tcg_rn);
7328 break;
7329 case 0x7: /* SQABS, SQNEG */
7330 if (u) {
7331 gen_helper_neon_qneg_s64(tcg_rd, cpu_env, tcg_rn);
7332 } else {
7333 gen_helper_neon_qabs_s64(tcg_rd, cpu_env, tcg_rn);
7335 break;
7336 case 0xa: /* CMLT */
7337 /* 64 bit integer comparison against zero, result is
7338 * test ? (2^64 - 1) : 0. We implement via setcond(!test) and
7339 * subtracting 1.
7341 cond = TCG_COND_LT;
7342 do_cmop:
7343 tcg_gen_setcondi_i64(cond, tcg_rd, tcg_rn, 0);
7344 tcg_gen_neg_i64(tcg_rd, tcg_rd);
7345 break;
7346 case 0x8: /* CMGT, CMGE */
7347 cond = u ? TCG_COND_GE : TCG_COND_GT;
7348 goto do_cmop;
7349 case 0x9: /* CMEQ, CMLE */
7350 cond = u ? TCG_COND_LE : TCG_COND_EQ;
7351 goto do_cmop;
7352 case 0xb: /* ABS, NEG */
7353 if (u) {
7354 tcg_gen_neg_i64(tcg_rd, tcg_rn);
7355 } else {
7356 TCGv_i64 tcg_zero = tcg_const_i64(0);
7357 tcg_gen_neg_i64(tcg_rd, tcg_rn);
7358 tcg_gen_movcond_i64(TCG_COND_GT, tcg_rd, tcg_rn, tcg_zero,
7359 tcg_rn, tcg_rd);
7360 tcg_temp_free_i64(tcg_zero);
7362 break;
7363 case 0x2f: /* FABS */
7364 gen_helper_vfp_absd(tcg_rd, tcg_rn);
7365 break;
7366 case 0x6f: /* FNEG */
7367 gen_helper_vfp_negd(tcg_rd, tcg_rn);
7368 break;
7369 case 0x7f: /* FSQRT */
7370 gen_helper_vfp_sqrtd(tcg_rd, tcg_rn, cpu_env);
7371 break;
7372 case 0x1a: /* FCVTNS */
7373 case 0x1b: /* FCVTMS */
7374 case 0x1c: /* FCVTAS */
7375 case 0x3a: /* FCVTPS */
7376 case 0x3b: /* FCVTZS */
7378 TCGv_i32 tcg_shift = tcg_const_i32(0);
7379 gen_helper_vfp_tosqd(tcg_rd, tcg_rn, tcg_shift, tcg_fpstatus);
7380 tcg_temp_free_i32(tcg_shift);
7381 break;
7383 case 0x5a: /* FCVTNU */
7384 case 0x5b: /* FCVTMU */
7385 case 0x5c: /* FCVTAU */
7386 case 0x7a: /* FCVTPU */
7387 case 0x7b: /* FCVTZU */
7389 TCGv_i32 tcg_shift = tcg_const_i32(0);
7390 gen_helper_vfp_touqd(tcg_rd, tcg_rn, tcg_shift, tcg_fpstatus);
7391 tcg_temp_free_i32(tcg_shift);
7392 break;
7394 case 0x18: /* FRINTN */
7395 case 0x19: /* FRINTM */
7396 case 0x38: /* FRINTP */
7397 case 0x39: /* FRINTZ */
7398 case 0x58: /* FRINTA */
7399 case 0x79: /* FRINTI */
7400 gen_helper_rintd(tcg_rd, tcg_rn, tcg_fpstatus);
7401 break;
7402 case 0x59: /* FRINTX */
7403 gen_helper_rintd_exact(tcg_rd, tcg_rn, tcg_fpstatus);
7404 break;
7405 default:
7406 g_assert_not_reached();
7410 static void handle_2misc_fcmp_zero(DisasContext *s, int opcode,
7411 bool is_scalar, bool is_u, bool is_q,
7412 int size, int rn, int rd)
7414 bool is_double = (size == 3);
7415 TCGv_ptr fpst;
7417 if (!fp_access_check(s)) {
7418 return;
7421 fpst = get_fpstatus_ptr();
7423 if (is_double) {
7424 TCGv_i64 tcg_op = tcg_temp_new_i64();
7425 TCGv_i64 tcg_zero = tcg_const_i64(0);
7426 TCGv_i64 tcg_res = tcg_temp_new_i64();
7427 NeonGenTwoDoubleOPFn *genfn;
7428 bool swap = false;
7429 int pass;
7431 switch (opcode) {
7432 case 0x2e: /* FCMLT (zero) */
7433 swap = true;
7434 /* fallthrough */
7435 case 0x2c: /* FCMGT (zero) */
7436 genfn = gen_helper_neon_cgt_f64;
7437 break;
7438 case 0x2d: /* FCMEQ (zero) */
7439 genfn = gen_helper_neon_ceq_f64;
7440 break;
7441 case 0x6d: /* FCMLE (zero) */
7442 swap = true;
7443 /* fall through */
7444 case 0x6c: /* FCMGE (zero) */
7445 genfn = gen_helper_neon_cge_f64;
7446 break;
7447 default:
7448 g_assert_not_reached();
7451 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
7452 read_vec_element(s, tcg_op, rn, pass, MO_64);
7453 if (swap) {
7454 genfn(tcg_res, tcg_zero, tcg_op, fpst);
7455 } else {
7456 genfn(tcg_res, tcg_op, tcg_zero, fpst);
7458 write_vec_element(s, tcg_res, rd, pass, MO_64);
7460 if (is_scalar) {
7461 clear_vec_high(s, rd);
7464 tcg_temp_free_i64(tcg_res);
7465 tcg_temp_free_i64(tcg_zero);
7466 tcg_temp_free_i64(tcg_op);
7467 } else {
7468 TCGv_i32 tcg_op = tcg_temp_new_i32();
7469 TCGv_i32 tcg_zero = tcg_const_i32(0);
7470 TCGv_i32 tcg_res = tcg_temp_new_i32();
7471 NeonGenTwoSingleOPFn *genfn;
7472 bool swap = false;
7473 int pass, maxpasses;
7475 switch (opcode) {
7476 case 0x2e: /* FCMLT (zero) */
7477 swap = true;
7478 /* fall through */
7479 case 0x2c: /* FCMGT (zero) */
7480 genfn = gen_helper_neon_cgt_f32;
7481 break;
7482 case 0x2d: /* FCMEQ (zero) */
7483 genfn = gen_helper_neon_ceq_f32;
7484 break;
7485 case 0x6d: /* FCMLE (zero) */
7486 swap = true;
7487 /* fall through */
7488 case 0x6c: /* FCMGE (zero) */
7489 genfn = gen_helper_neon_cge_f32;
7490 break;
7491 default:
7492 g_assert_not_reached();
7495 if (is_scalar) {
7496 maxpasses = 1;
7497 } else {
7498 maxpasses = is_q ? 4 : 2;
7501 for (pass = 0; pass < maxpasses; pass++) {
7502 read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
7503 if (swap) {
7504 genfn(tcg_res, tcg_zero, tcg_op, fpst);
7505 } else {
7506 genfn(tcg_res, tcg_op, tcg_zero, fpst);
7508 if (is_scalar) {
7509 write_fp_sreg(s, rd, tcg_res);
7510 } else {
7511 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
7514 tcg_temp_free_i32(tcg_res);
7515 tcg_temp_free_i32(tcg_zero);
7516 tcg_temp_free_i32(tcg_op);
7517 if (!is_q && !is_scalar) {
7518 clear_vec_high(s, rd);
7522 tcg_temp_free_ptr(fpst);
7525 static void handle_2misc_reciprocal(DisasContext *s, int opcode,
7526 bool is_scalar, bool is_u, bool is_q,
7527 int size, int rn, int rd)
7529 bool is_double = (size == 3);
7530 TCGv_ptr fpst = get_fpstatus_ptr();
7532 if (is_double) {
7533 TCGv_i64 tcg_op = tcg_temp_new_i64();
7534 TCGv_i64 tcg_res = tcg_temp_new_i64();
7535 int pass;
7537 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
7538 read_vec_element(s, tcg_op, rn, pass, MO_64);
7539 switch (opcode) {
7540 case 0x3d: /* FRECPE */
7541 gen_helper_recpe_f64(tcg_res, tcg_op, fpst);
7542 break;
7543 case 0x3f: /* FRECPX */
7544 gen_helper_frecpx_f64(tcg_res, tcg_op, fpst);
7545 break;
7546 case 0x7d: /* FRSQRTE */
7547 gen_helper_rsqrte_f64(tcg_res, tcg_op, fpst);
7548 break;
7549 default:
7550 g_assert_not_reached();
7552 write_vec_element(s, tcg_res, rd, pass, MO_64);
7554 if (is_scalar) {
7555 clear_vec_high(s, rd);
7558 tcg_temp_free_i64(tcg_res);
7559 tcg_temp_free_i64(tcg_op);
7560 } else {
7561 TCGv_i32 tcg_op = tcg_temp_new_i32();
7562 TCGv_i32 tcg_res = tcg_temp_new_i32();
7563 int pass, maxpasses;
7565 if (is_scalar) {
7566 maxpasses = 1;
7567 } else {
7568 maxpasses = is_q ? 4 : 2;
7571 for (pass = 0; pass < maxpasses; pass++) {
7572 read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
7574 switch (opcode) {
7575 case 0x3c: /* URECPE */
7576 gen_helper_recpe_u32(tcg_res, tcg_op, fpst);
7577 break;
7578 case 0x3d: /* FRECPE */
7579 gen_helper_recpe_f32(tcg_res, tcg_op, fpst);
7580 break;
7581 case 0x3f: /* FRECPX */
7582 gen_helper_frecpx_f32(tcg_res, tcg_op, fpst);
7583 break;
7584 case 0x7d: /* FRSQRTE */
7585 gen_helper_rsqrte_f32(tcg_res, tcg_op, fpst);
7586 break;
7587 default:
7588 g_assert_not_reached();
7591 if (is_scalar) {
7592 write_fp_sreg(s, rd, tcg_res);
7593 } else {
7594 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
7597 tcg_temp_free_i32(tcg_res);
7598 tcg_temp_free_i32(tcg_op);
7599 if (!is_q && !is_scalar) {
7600 clear_vec_high(s, rd);
7603 tcg_temp_free_ptr(fpst);
7606 static void handle_2misc_narrow(DisasContext *s, bool scalar,
7607 int opcode, bool u, bool is_q,
7608 int size, int rn, int rd)
7610 /* Handle 2-reg-misc ops which are narrowing (so each 2*size element
7611 * in the source becomes a size element in the destination).
7613 int pass;
7614 TCGv_i32 tcg_res[2];
7615 int destelt = is_q ? 2 : 0;
7616 int passes = scalar ? 1 : 2;
7618 if (scalar) {
7619 tcg_res[1] = tcg_const_i32(0);
7622 for (pass = 0; pass < passes; pass++) {
7623 TCGv_i64 tcg_op = tcg_temp_new_i64();
7624 NeonGenNarrowFn *genfn = NULL;
7625 NeonGenNarrowEnvFn *genenvfn = NULL;
7627 if (scalar) {
7628 read_vec_element(s, tcg_op, rn, pass, size + 1);
7629 } else {
7630 read_vec_element(s, tcg_op, rn, pass, MO_64);
7632 tcg_res[pass] = tcg_temp_new_i32();
7634 switch (opcode) {
7635 case 0x12: /* XTN, SQXTUN */
7637 static NeonGenNarrowFn * const xtnfns[3] = {
7638 gen_helper_neon_narrow_u8,
7639 gen_helper_neon_narrow_u16,
7640 tcg_gen_trunc_i64_i32,
7642 static NeonGenNarrowEnvFn * const sqxtunfns[3] = {
7643 gen_helper_neon_unarrow_sat8,
7644 gen_helper_neon_unarrow_sat16,
7645 gen_helper_neon_unarrow_sat32,
7647 if (u) {
7648 genenvfn = sqxtunfns[size];
7649 } else {
7650 genfn = xtnfns[size];
7652 break;
7654 case 0x14: /* SQXTN, UQXTN */
7656 static NeonGenNarrowEnvFn * const fns[3][2] = {
7657 { gen_helper_neon_narrow_sat_s8,
7658 gen_helper_neon_narrow_sat_u8 },
7659 { gen_helper_neon_narrow_sat_s16,
7660 gen_helper_neon_narrow_sat_u16 },
7661 { gen_helper_neon_narrow_sat_s32,
7662 gen_helper_neon_narrow_sat_u32 },
7664 genenvfn = fns[size][u];
7665 break;
7667 case 0x16: /* FCVTN, FCVTN2 */
7668 /* 32 bit to 16 bit or 64 bit to 32 bit float conversion */
7669 if (size == 2) {
7670 gen_helper_vfp_fcvtsd(tcg_res[pass], tcg_op, cpu_env);
7671 } else {
7672 TCGv_i32 tcg_lo = tcg_temp_new_i32();
7673 TCGv_i32 tcg_hi = tcg_temp_new_i32();
7674 tcg_gen_trunc_i64_i32(tcg_lo, tcg_op);
7675 gen_helper_vfp_fcvt_f32_to_f16(tcg_lo, tcg_lo, cpu_env);
7676 tcg_gen_shri_i64(tcg_op, tcg_op, 32);
7677 tcg_gen_trunc_i64_i32(tcg_hi, tcg_op);
7678 gen_helper_vfp_fcvt_f32_to_f16(tcg_hi, tcg_hi, cpu_env);
7679 tcg_gen_deposit_i32(tcg_res[pass], tcg_lo, tcg_hi, 16, 16);
7680 tcg_temp_free_i32(tcg_lo);
7681 tcg_temp_free_i32(tcg_hi);
7683 break;
7684 case 0x56: /* FCVTXN, FCVTXN2 */
7685 /* 64 bit to 32 bit float conversion
7686 * with von Neumann rounding (round to odd)
7688 assert(size == 2);
7689 gen_helper_fcvtx_f64_to_f32(tcg_res[pass], tcg_op, cpu_env);
7690 break;
7691 default:
7692 g_assert_not_reached();
7695 if (genfn) {
7696 genfn(tcg_res[pass], tcg_op);
7697 } else if (genenvfn) {
7698 genenvfn(tcg_res[pass], cpu_env, tcg_op);
7701 tcg_temp_free_i64(tcg_op);
7704 for (pass = 0; pass < 2; pass++) {
7705 write_vec_element_i32(s, tcg_res[pass], rd, destelt + pass, MO_32);
7706 tcg_temp_free_i32(tcg_res[pass]);
7708 if (!is_q) {
7709 clear_vec_high(s, rd);
7713 /* Remaining saturating accumulating ops */
7714 static void handle_2misc_satacc(DisasContext *s, bool is_scalar, bool is_u,
7715 bool is_q, int size, int rn, int rd)
7717 bool is_double = (size == 3);
7719 if (is_double) {
7720 TCGv_i64 tcg_rn = tcg_temp_new_i64();
7721 TCGv_i64 tcg_rd = tcg_temp_new_i64();
7722 int pass;
7724 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
7725 read_vec_element(s, tcg_rn, rn, pass, MO_64);
7726 read_vec_element(s, tcg_rd, rd, pass, MO_64);
7728 if (is_u) { /* USQADD */
7729 gen_helper_neon_uqadd_s64(tcg_rd, cpu_env, tcg_rn, tcg_rd);
7730 } else { /* SUQADD */
7731 gen_helper_neon_sqadd_u64(tcg_rd, cpu_env, tcg_rn, tcg_rd);
7733 write_vec_element(s, tcg_rd, rd, pass, MO_64);
7735 if (is_scalar) {
7736 clear_vec_high(s, rd);
7739 tcg_temp_free_i64(tcg_rd);
7740 tcg_temp_free_i64(tcg_rn);
7741 } else {
7742 TCGv_i32 tcg_rn = tcg_temp_new_i32();
7743 TCGv_i32 tcg_rd = tcg_temp_new_i32();
7744 int pass, maxpasses;
7746 if (is_scalar) {
7747 maxpasses = 1;
7748 } else {
7749 maxpasses = is_q ? 4 : 2;
7752 for (pass = 0; pass < maxpasses; pass++) {
7753 if (is_scalar) {
7754 read_vec_element_i32(s, tcg_rn, rn, pass, size);
7755 read_vec_element_i32(s, tcg_rd, rd, pass, size);
7756 } else {
7757 read_vec_element_i32(s, tcg_rn, rn, pass, MO_32);
7758 read_vec_element_i32(s, tcg_rd, rd, pass, MO_32);
7761 if (is_u) { /* USQADD */
7762 switch (size) {
7763 case 0:
7764 gen_helper_neon_uqadd_s8(tcg_rd, cpu_env, tcg_rn, tcg_rd);
7765 break;
7766 case 1:
7767 gen_helper_neon_uqadd_s16(tcg_rd, cpu_env, tcg_rn, tcg_rd);
7768 break;
7769 case 2:
7770 gen_helper_neon_uqadd_s32(tcg_rd, cpu_env, tcg_rn, tcg_rd);
7771 break;
7772 default:
7773 g_assert_not_reached();
7775 } else { /* SUQADD */
7776 switch (size) {
7777 case 0:
7778 gen_helper_neon_sqadd_u8(tcg_rd, cpu_env, tcg_rn, tcg_rd);
7779 break;
7780 case 1:
7781 gen_helper_neon_sqadd_u16(tcg_rd, cpu_env, tcg_rn, tcg_rd);
7782 break;
7783 case 2:
7784 gen_helper_neon_sqadd_u32(tcg_rd, cpu_env, tcg_rn, tcg_rd);
7785 break;
7786 default:
7787 g_assert_not_reached();
7791 if (is_scalar) {
7792 TCGv_i64 tcg_zero = tcg_const_i64(0);
7793 write_vec_element(s, tcg_zero, rd, 0, MO_64);
7794 tcg_temp_free_i64(tcg_zero);
7796 write_vec_element_i32(s, tcg_rd, rd, pass, MO_32);
7799 if (!is_q) {
7800 clear_vec_high(s, rd);
7803 tcg_temp_free_i32(tcg_rd);
7804 tcg_temp_free_i32(tcg_rn);
7808 /* C3.6.12 AdvSIMD scalar two reg misc
7809 * 31 30 29 28 24 23 22 21 17 16 12 11 10 9 5 4 0
7810 * +-----+---+-----------+------+-----------+--------+-----+------+------+
7811 * | 0 1 | U | 1 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 | Rn | Rd |
7812 * +-----+---+-----------+------+-----------+--------+-----+------+------+
7814 static void disas_simd_scalar_two_reg_misc(DisasContext *s, uint32_t insn)
7816 int rd = extract32(insn, 0, 5);
7817 int rn = extract32(insn, 5, 5);
7818 int opcode = extract32(insn, 12, 5);
7819 int size = extract32(insn, 22, 2);
7820 bool u = extract32(insn, 29, 1);
7821 bool is_fcvt = false;
7822 int rmode;
7823 TCGv_i32 tcg_rmode;
7824 TCGv_ptr tcg_fpstatus;
7826 switch (opcode) {
7827 case 0x3: /* USQADD / SUQADD*/
7828 if (!fp_access_check(s)) {
7829 return;
7831 handle_2misc_satacc(s, true, u, false, size, rn, rd);
7832 return;
7833 case 0x7: /* SQABS / SQNEG */
7834 break;
7835 case 0xa: /* CMLT */
7836 if (u) {
7837 unallocated_encoding(s);
7838 return;
7840 /* fall through */
7841 case 0x8: /* CMGT, CMGE */
7842 case 0x9: /* CMEQ, CMLE */
7843 case 0xb: /* ABS, NEG */
7844 if (size != 3) {
7845 unallocated_encoding(s);
7846 return;
7848 break;
7849 case 0x12: /* SQXTUN */
7850 if (!u) {
7851 unallocated_encoding(s);
7852 return;
7854 /* fall through */
7855 case 0x14: /* SQXTN, UQXTN */
7856 if (size == 3) {
7857 unallocated_encoding(s);
7858 return;
7860 if (!fp_access_check(s)) {
7861 return;
7863 handle_2misc_narrow(s, true, opcode, u, false, size, rn, rd);
7864 return;
7865 case 0xc ... 0xf:
7866 case 0x16 ... 0x1d:
7867 case 0x1f:
7868 /* Floating point: U, size[1] and opcode indicate operation;
7869 * size[0] indicates single or double precision.
7871 opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
7872 size = extract32(size, 0, 1) ? 3 : 2;
7873 switch (opcode) {
7874 case 0x2c: /* FCMGT (zero) */
7875 case 0x2d: /* FCMEQ (zero) */
7876 case 0x2e: /* FCMLT (zero) */
7877 case 0x6c: /* FCMGE (zero) */
7878 case 0x6d: /* FCMLE (zero) */
7879 handle_2misc_fcmp_zero(s, opcode, true, u, true, size, rn, rd);
7880 return;
7881 case 0x1d: /* SCVTF */
7882 case 0x5d: /* UCVTF */
7884 bool is_signed = (opcode == 0x1d);
7885 if (!fp_access_check(s)) {
7886 return;
7888 handle_simd_intfp_conv(s, rd, rn, 1, is_signed, 0, size);
7889 return;
7891 case 0x3d: /* FRECPE */
7892 case 0x3f: /* FRECPX */
7893 case 0x7d: /* FRSQRTE */
7894 if (!fp_access_check(s)) {
7895 return;
7897 handle_2misc_reciprocal(s, opcode, true, u, true, size, rn, rd);
7898 return;
7899 case 0x1a: /* FCVTNS */
7900 case 0x1b: /* FCVTMS */
7901 case 0x3a: /* FCVTPS */
7902 case 0x3b: /* FCVTZS */
7903 case 0x5a: /* FCVTNU */
7904 case 0x5b: /* FCVTMU */
7905 case 0x7a: /* FCVTPU */
7906 case 0x7b: /* FCVTZU */
7907 is_fcvt = true;
7908 rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
7909 break;
7910 case 0x1c: /* FCVTAS */
7911 case 0x5c: /* FCVTAU */
7912 /* TIEAWAY doesn't fit in the usual rounding mode encoding */
7913 is_fcvt = true;
7914 rmode = FPROUNDING_TIEAWAY;
7915 break;
7916 case 0x56: /* FCVTXN, FCVTXN2 */
7917 if (size == 2) {
7918 unallocated_encoding(s);
7919 return;
7921 if (!fp_access_check(s)) {
7922 return;
7924 handle_2misc_narrow(s, true, opcode, u, false, size - 1, rn, rd);
7925 return;
7926 default:
7927 unallocated_encoding(s);
7928 return;
7930 break;
7931 default:
7932 unallocated_encoding(s);
7933 return;
7936 if (!fp_access_check(s)) {
7937 return;
7940 if (is_fcvt) {
7941 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rmode));
7942 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
7943 tcg_fpstatus = get_fpstatus_ptr();
7944 } else {
7945 TCGV_UNUSED_I32(tcg_rmode);
7946 TCGV_UNUSED_PTR(tcg_fpstatus);
7949 if (size == 3) {
7950 TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
7951 TCGv_i64 tcg_rd = tcg_temp_new_i64();
7953 handle_2misc_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rmode, tcg_fpstatus);
7954 write_fp_dreg(s, rd, tcg_rd);
7955 tcg_temp_free_i64(tcg_rd);
7956 tcg_temp_free_i64(tcg_rn);
7957 } else {
7958 TCGv_i32 tcg_rn = tcg_temp_new_i32();
7959 TCGv_i32 tcg_rd = tcg_temp_new_i32();
7961 read_vec_element_i32(s, tcg_rn, rn, 0, size);
7963 switch (opcode) {
7964 case 0x7: /* SQABS, SQNEG */
7966 NeonGenOneOpEnvFn *genfn;
7967 static NeonGenOneOpEnvFn * const fns[3][2] = {
7968 { gen_helper_neon_qabs_s8, gen_helper_neon_qneg_s8 },
7969 { gen_helper_neon_qabs_s16, gen_helper_neon_qneg_s16 },
7970 { gen_helper_neon_qabs_s32, gen_helper_neon_qneg_s32 },
7972 genfn = fns[size][u];
7973 genfn(tcg_rd, cpu_env, tcg_rn);
7974 break;
7976 case 0x1a: /* FCVTNS */
7977 case 0x1b: /* FCVTMS */
7978 case 0x1c: /* FCVTAS */
7979 case 0x3a: /* FCVTPS */
7980 case 0x3b: /* FCVTZS */
7982 TCGv_i32 tcg_shift = tcg_const_i32(0);
7983 gen_helper_vfp_tosls(tcg_rd, tcg_rn, tcg_shift, tcg_fpstatus);
7984 tcg_temp_free_i32(tcg_shift);
7985 break;
7987 case 0x5a: /* FCVTNU */
7988 case 0x5b: /* FCVTMU */
7989 case 0x5c: /* FCVTAU */
7990 case 0x7a: /* FCVTPU */
7991 case 0x7b: /* FCVTZU */
7993 TCGv_i32 tcg_shift = tcg_const_i32(0);
7994 gen_helper_vfp_touls(tcg_rd, tcg_rn, tcg_shift, tcg_fpstatus);
7995 tcg_temp_free_i32(tcg_shift);
7996 break;
7998 default:
7999 g_assert_not_reached();
8002 write_fp_sreg(s, rd, tcg_rd);
8003 tcg_temp_free_i32(tcg_rd);
8004 tcg_temp_free_i32(tcg_rn);
8007 if (is_fcvt) {
8008 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
8009 tcg_temp_free_i32(tcg_rmode);
8010 tcg_temp_free_ptr(tcg_fpstatus);
8014 /* SSHR[RA]/USHR[RA] - Vector shift right (optional rounding/accumulate) */
8015 static void handle_vec_simd_shri(DisasContext *s, bool is_q, bool is_u,
8016 int immh, int immb, int opcode, int rn, int rd)
8018 int size = 32 - clz32(immh) - 1;
8019 int immhb = immh << 3 | immb;
8020 int shift = 2 * (8 << size) - immhb;
8021 bool accumulate = false;
8022 bool round = false;
8023 bool insert = false;
8024 int dsize = is_q ? 128 : 64;
8025 int esize = 8 << size;
8026 int elements = dsize/esize;
8027 TCGMemOp memop = size | (is_u ? 0 : MO_SIGN);
8028 TCGv_i64 tcg_rn = new_tmp_a64(s);
8029 TCGv_i64 tcg_rd = new_tmp_a64(s);
8030 TCGv_i64 tcg_round;
8031 int i;
8033 if (extract32(immh, 3, 1) && !is_q) {
8034 unallocated_encoding(s);
8035 return;
8038 if (size > 3 && !is_q) {
8039 unallocated_encoding(s);
8040 return;
8043 if (!fp_access_check(s)) {
8044 return;
8047 switch (opcode) {
8048 case 0x02: /* SSRA / USRA (accumulate) */
8049 accumulate = true;
8050 break;
8051 case 0x04: /* SRSHR / URSHR (rounding) */
8052 round = true;
8053 break;
8054 case 0x06: /* SRSRA / URSRA (accum + rounding) */
8055 accumulate = round = true;
8056 break;
8057 case 0x08: /* SRI */
8058 insert = true;
8059 break;
8062 if (round) {
8063 uint64_t round_const = 1ULL << (shift - 1);
8064 tcg_round = tcg_const_i64(round_const);
8065 } else {
8066 TCGV_UNUSED_I64(tcg_round);
8069 for (i = 0; i < elements; i++) {
8070 read_vec_element(s, tcg_rn, rn, i, memop);
8071 if (accumulate || insert) {
8072 read_vec_element(s, tcg_rd, rd, i, memop);
8075 if (insert) {
8076 handle_shri_with_ins(tcg_rd, tcg_rn, size, shift);
8077 } else {
8078 handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
8079 accumulate, is_u, size, shift);
8082 write_vec_element(s, tcg_rd, rd, i, size);
8085 if (!is_q) {
8086 clear_vec_high(s, rd);
8089 if (round) {
8090 tcg_temp_free_i64(tcg_round);
8094 /* SHL/SLI - Vector shift left */
8095 static void handle_vec_simd_shli(DisasContext *s, bool is_q, bool insert,
8096 int immh, int immb, int opcode, int rn, int rd)
8098 int size = 32 - clz32(immh) - 1;
8099 int immhb = immh << 3 | immb;
8100 int shift = immhb - (8 << size);
8101 int dsize = is_q ? 128 : 64;
8102 int esize = 8 << size;
8103 int elements = dsize/esize;
8104 TCGv_i64 tcg_rn = new_tmp_a64(s);
8105 TCGv_i64 tcg_rd = new_tmp_a64(s);
8106 int i;
8108 if (extract32(immh, 3, 1) && !is_q) {
8109 unallocated_encoding(s);
8110 return;
8113 if (size > 3 && !is_q) {
8114 unallocated_encoding(s);
8115 return;
8118 if (!fp_access_check(s)) {
8119 return;
8122 for (i = 0; i < elements; i++) {
8123 read_vec_element(s, tcg_rn, rn, i, size);
8124 if (insert) {
8125 read_vec_element(s, tcg_rd, rd, i, size);
8128 handle_shli_with_ins(tcg_rd, tcg_rn, insert, shift);
8130 write_vec_element(s, tcg_rd, rd, i, size);
8133 if (!is_q) {
8134 clear_vec_high(s, rd);
8138 /* USHLL/SHLL - Vector shift left with widening */
8139 static void handle_vec_simd_wshli(DisasContext *s, bool is_q, bool is_u,
8140 int immh, int immb, int opcode, int rn, int rd)
8142 int size = 32 - clz32(immh) - 1;
8143 int immhb = immh << 3 | immb;
8144 int shift = immhb - (8 << size);
8145 int dsize = 64;
8146 int esize = 8 << size;
8147 int elements = dsize/esize;
8148 TCGv_i64 tcg_rn = new_tmp_a64(s);
8149 TCGv_i64 tcg_rd = new_tmp_a64(s);
8150 int i;
8152 if (size >= 3) {
8153 unallocated_encoding(s);
8154 return;
8157 if (!fp_access_check(s)) {
8158 return;
8161 /* For the LL variants the store is larger than the load,
8162 * so if rd == rn we would overwrite parts of our input.
8163 * So load everything right now and use shifts in the main loop.
8165 read_vec_element(s, tcg_rn, rn, is_q ? 1 : 0, MO_64);
8167 for (i = 0; i < elements; i++) {
8168 tcg_gen_shri_i64(tcg_rd, tcg_rn, i * esize);
8169 ext_and_shift_reg(tcg_rd, tcg_rd, size | (!is_u << 2), 0);
8170 tcg_gen_shli_i64(tcg_rd, tcg_rd, shift);
8171 write_vec_element(s, tcg_rd, rd, i, size + 1);
8175 /* SHRN/RSHRN - Shift right with narrowing (and potential rounding) */
8176 static void handle_vec_simd_shrn(DisasContext *s, bool is_q,
8177 int immh, int immb, int opcode, int rn, int rd)
8179 int immhb = immh << 3 | immb;
8180 int size = 32 - clz32(immh) - 1;
8181 int dsize = 64;
8182 int esize = 8 << size;
8183 int elements = dsize/esize;
8184 int shift = (2 * esize) - immhb;
8185 bool round = extract32(opcode, 0, 1);
8186 TCGv_i64 tcg_rn, tcg_rd, tcg_final;
8187 TCGv_i64 tcg_round;
8188 int i;
8190 if (extract32(immh, 3, 1)) {
8191 unallocated_encoding(s);
8192 return;
8195 if (!fp_access_check(s)) {
8196 return;
8199 tcg_rn = tcg_temp_new_i64();
8200 tcg_rd = tcg_temp_new_i64();
8201 tcg_final = tcg_temp_new_i64();
8202 read_vec_element(s, tcg_final, rd, is_q ? 1 : 0, MO_64);
8204 if (round) {
8205 uint64_t round_const = 1ULL << (shift - 1);
8206 tcg_round = tcg_const_i64(round_const);
8207 } else {
8208 TCGV_UNUSED_I64(tcg_round);
8211 for (i = 0; i < elements; i++) {
8212 read_vec_element(s, tcg_rn, rn, i, size+1);
8213 handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
8214 false, true, size+1, shift);
8216 tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
8219 if (!is_q) {
8220 clear_vec_high(s, rd);
8221 write_vec_element(s, tcg_final, rd, 0, MO_64);
8222 } else {
8223 write_vec_element(s, tcg_final, rd, 1, MO_64);
8226 if (round) {
8227 tcg_temp_free_i64(tcg_round);
8229 tcg_temp_free_i64(tcg_rn);
8230 tcg_temp_free_i64(tcg_rd);
8231 tcg_temp_free_i64(tcg_final);
8232 return;
8236 /* C3.6.14 AdvSIMD shift by immediate
8237 * 31 30 29 28 23 22 19 18 16 15 11 10 9 5 4 0
8238 * +---+---+---+-------------+------+------+--------+---+------+------+
8239 * | 0 | Q | U | 0 1 1 1 1 0 | immh | immb | opcode | 1 | Rn | Rd |
8240 * +---+---+---+-------------+------+------+--------+---+------+------+
8242 static void disas_simd_shift_imm(DisasContext *s, uint32_t insn)
8244 int rd = extract32(insn, 0, 5);
8245 int rn = extract32(insn, 5, 5);
8246 int opcode = extract32(insn, 11, 5);
8247 int immb = extract32(insn, 16, 3);
8248 int immh = extract32(insn, 19, 4);
8249 bool is_u = extract32(insn, 29, 1);
8250 bool is_q = extract32(insn, 30, 1);
8252 switch (opcode) {
8253 case 0x08: /* SRI */
8254 if (!is_u) {
8255 unallocated_encoding(s);
8256 return;
8258 /* fall through */
8259 case 0x00: /* SSHR / USHR */
8260 case 0x02: /* SSRA / USRA (accumulate) */
8261 case 0x04: /* SRSHR / URSHR (rounding) */
8262 case 0x06: /* SRSRA / URSRA (accum + rounding) */
8263 handle_vec_simd_shri(s, is_q, is_u, immh, immb, opcode, rn, rd);
8264 break;
8265 case 0x0a: /* SHL / SLI */
8266 handle_vec_simd_shli(s, is_q, is_u, immh, immb, opcode, rn, rd);
8267 break;
8268 case 0x10: /* SHRN */
8269 case 0x11: /* RSHRN / SQRSHRUN */
8270 if (is_u) {
8271 handle_vec_simd_sqshrn(s, false, is_q, false, true, immh, immb,
8272 opcode, rn, rd);
8273 } else {
8274 handle_vec_simd_shrn(s, is_q, immh, immb, opcode, rn, rd);
8276 break;
8277 case 0x12: /* SQSHRN / UQSHRN */
8278 case 0x13: /* SQRSHRN / UQRSHRN */
8279 handle_vec_simd_sqshrn(s, false, is_q, is_u, is_u, immh, immb,
8280 opcode, rn, rd);
8281 break;
8282 case 0x14: /* SSHLL / USHLL */
8283 handle_vec_simd_wshli(s, is_q, is_u, immh, immb, opcode, rn, rd);
8284 break;
8285 case 0x1c: /* SCVTF / UCVTF */
8286 handle_simd_shift_intfp_conv(s, false, is_q, is_u, immh, immb,
8287 opcode, rn, rd);
8288 break;
8289 case 0xc: /* SQSHLU */
8290 if (!is_u) {
8291 unallocated_encoding(s);
8292 return;
8294 handle_simd_qshl(s, false, is_q, false, true, immh, immb, rn, rd);
8295 break;
8296 case 0xe: /* SQSHL, UQSHL */
8297 handle_simd_qshl(s, false, is_q, is_u, is_u, immh, immb, rn, rd);
8298 break;
8299 case 0x1f: /* FCVTZS/ FCVTZU */
8300 handle_simd_shift_fpint_conv(s, false, is_q, is_u, immh, immb, rn, rd);
8301 return;
8302 default:
8303 unallocated_encoding(s);
8304 return;
8308 /* Generate code to do a "long" addition or subtraction, ie one done in
8309 * TCGv_i64 on vector lanes twice the width specified by size.
8311 static void gen_neon_addl(int size, bool is_sub, TCGv_i64 tcg_res,
8312 TCGv_i64 tcg_op1, TCGv_i64 tcg_op2)
8314 static NeonGenTwo64OpFn * const fns[3][2] = {
8315 { gen_helper_neon_addl_u16, gen_helper_neon_subl_u16 },
8316 { gen_helper_neon_addl_u32, gen_helper_neon_subl_u32 },
8317 { tcg_gen_add_i64, tcg_gen_sub_i64 },
8319 NeonGenTwo64OpFn *genfn;
8320 assert(size < 3);
8322 genfn = fns[size][is_sub];
8323 genfn(tcg_res, tcg_op1, tcg_op2);
8326 static void handle_3rd_widening(DisasContext *s, int is_q, int is_u, int size,
8327 int opcode, int rd, int rn, int rm)
8329 /* 3-reg-different widening insns: 64 x 64 -> 128 */
8330 TCGv_i64 tcg_res[2];
8331 int pass, accop;
8333 tcg_res[0] = tcg_temp_new_i64();
8334 tcg_res[1] = tcg_temp_new_i64();
8336 /* Does this op do an adding accumulate, a subtracting accumulate,
8337 * or no accumulate at all?
8339 switch (opcode) {
8340 case 5:
8341 case 8:
8342 case 9:
8343 accop = 1;
8344 break;
8345 case 10:
8346 case 11:
8347 accop = -1;
8348 break;
8349 default:
8350 accop = 0;
8351 break;
8354 if (accop != 0) {
8355 read_vec_element(s, tcg_res[0], rd, 0, MO_64);
8356 read_vec_element(s, tcg_res[1], rd, 1, MO_64);
8359 /* size == 2 means two 32x32->64 operations; this is worth special
8360 * casing because we can generally handle it inline.
8362 if (size == 2) {
8363 for (pass = 0; pass < 2; pass++) {
8364 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8365 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
8366 TCGv_i64 tcg_passres;
8367 TCGMemOp memop = MO_32 | (is_u ? 0 : MO_SIGN);
8369 int elt = pass + is_q * 2;
8371 read_vec_element(s, tcg_op1, rn, elt, memop);
8372 read_vec_element(s, tcg_op2, rm, elt, memop);
8374 if (accop == 0) {
8375 tcg_passres = tcg_res[pass];
8376 } else {
8377 tcg_passres = tcg_temp_new_i64();
8380 switch (opcode) {
8381 case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
8382 tcg_gen_add_i64(tcg_passres, tcg_op1, tcg_op2);
8383 break;
8384 case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
8385 tcg_gen_sub_i64(tcg_passres, tcg_op1, tcg_op2);
8386 break;
8387 case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
8388 case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
8390 TCGv_i64 tcg_tmp1 = tcg_temp_new_i64();
8391 TCGv_i64 tcg_tmp2 = tcg_temp_new_i64();
8393 tcg_gen_sub_i64(tcg_tmp1, tcg_op1, tcg_op2);
8394 tcg_gen_sub_i64(tcg_tmp2, tcg_op2, tcg_op1);
8395 tcg_gen_movcond_i64(is_u ? TCG_COND_GEU : TCG_COND_GE,
8396 tcg_passres,
8397 tcg_op1, tcg_op2, tcg_tmp1, tcg_tmp2);
8398 tcg_temp_free_i64(tcg_tmp1);
8399 tcg_temp_free_i64(tcg_tmp2);
8400 break;
8402 case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
8403 case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
8404 case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
8405 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
8406 break;
8407 case 9: /* SQDMLAL, SQDMLAL2 */
8408 case 11: /* SQDMLSL, SQDMLSL2 */
8409 case 13: /* SQDMULL, SQDMULL2 */
8410 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
8411 gen_helper_neon_addl_saturate_s64(tcg_passres, cpu_env,
8412 tcg_passres, tcg_passres);
8413 break;
8414 default:
8415 g_assert_not_reached();
8418 if (opcode == 9 || opcode == 11) {
8419 /* saturating accumulate ops */
8420 if (accop < 0) {
8421 tcg_gen_neg_i64(tcg_passres, tcg_passres);
8423 gen_helper_neon_addl_saturate_s64(tcg_res[pass], cpu_env,
8424 tcg_res[pass], tcg_passres);
8425 } else if (accop > 0) {
8426 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
8427 } else if (accop < 0) {
8428 tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
8431 if (accop != 0) {
8432 tcg_temp_free_i64(tcg_passres);
8435 tcg_temp_free_i64(tcg_op1);
8436 tcg_temp_free_i64(tcg_op2);
8438 } else {
8439 /* size 0 or 1, generally helper functions */
8440 for (pass = 0; pass < 2; pass++) {
8441 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
8442 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
8443 TCGv_i64 tcg_passres;
8444 int elt = pass + is_q * 2;
8446 read_vec_element_i32(s, tcg_op1, rn, elt, MO_32);
8447 read_vec_element_i32(s, tcg_op2, rm, elt, MO_32);
8449 if (accop == 0) {
8450 tcg_passres = tcg_res[pass];
8451 } else {
8452 tcg_passres = tcg_temp_new_i64();
8455 switch (opcode) {
8456 case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
8457 case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
8459 TCGv_i64 tcg_op2_64 = tcg_temp_new_i64();
8460 static NeonGenWidenFn * const widenfns[2][2] = {
8461 { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
8462 { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
8464 NeonGenWidenFn *widenfn = widenfns[size][is_u];
8466 widenfn(tcg_op2_64, tcg_op2);
8467 widenfn(tcg_passres, tcg_op1);
8468 gen_neon_addl(size, (opcode == 2), tcg_passres,
8469 tcg_passres, tcg_op2_64);
8470 tcg_temp_free_i64(tcg_op2_64);
8471 break;
8473 case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
8474 case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
8475 if (size == 0) {
8476 if (is_u) {
8477 gen_helper_neon_abdl_u16(tcg_passres, tcg_op1, tcg_op2);
8478 } else {
8479 gen_helper_neon_abdl_s16(tcg_passres, tcg_op1, tcg_op2);
8481 } else {
8482 if (is_u) {
8483 gen_helper_neon_abdl_u32(tcg_passres, tcg_op1, tcg_op2);
8484 } else {
8485 gen_helper_neon_abdl_s32(tcg_passres, tcg_op1, tcg_op2);
8488 break;
8489 case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
8490 case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
8491 case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
8492 if (size == 0) {
8493 if (is_u) {
8494 gen_helper_neon_mull_u8(tcg_passres, tcg_op1, tcg_op2);
8495 } else {
8496 gen_helper_neon_mull_s8(tcg_passres, tcg_op1, tcg_op2);
8498 } else {
8499 if (is_u) {
8500 gen_helper_neon_mull_u16(tcg_passres, tcg_op1, tcg_op2);
8501 } else {
8502 gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
8505 break;
8506 case 9: /* SQDMLAL, SQDMLAL2 */
8507 case 11: /* SQDMLSL, SQDMLSL2 */
8508 case 13: /* SQDMULL, SQDMULL2 */
8509 assert(size == 1);
8510 gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
8511 gen_helper_neon_addl_saturate_s32(tcg_passres, cpu_env,
8512 tcg_passres, tcg_passres);
8513 break;
8514 case 14: /* PMULL */
8515 assert(size == 0);
8516 gen_helper_neon_mull_p8(tcg_passres, tcg_op1, tcg_op2);
8517 break;
8518 default:
8519 g_assert_not_reached();
8521 tcg_temp_free_i32(tcg_op1);
8522 tcg_temp_free_i32(tcg_op2);
8524 if (accop != 0) {
8525 if (opcode == 9 || opcode == 11) {
8526 /* saturating accumulate ops */
8527 if (accop < 0) {
8528 gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
8530 gen_helper_neon_addl_saturate_s32(tcg_res[pass], cpu_env,
8531 tcg_res[pass],
8532 tcg_passres);
8533 } else {
8534 gen_neon_addl(size, (accop < 0), tcg_res[pass],
8535 tcg_res[pass], tcg_passres);
8537 tcg_temp_free_i64(tcg_passres);
8542 write_vec_element(s, tcg_res[0], rd, 0, MO_64);
8543 write_vec_element(s, tcg_res[1], rd, 1, MO_64);
8544 tcg_temp_free_i64(tcg_res[0]);
8545 tcg_temp_free_i64(tcg_res[1]);
8548 static void handle_3rd_wide(DisasContext *s, int is_q, int is_u, int size,
8549 int opcode, int rd, int rn, int rm)
8551 TCGv_i64 tcg_res[2];
8552 int part = is_q ? 2 : 0;
8553 int pass;
8555 for (pass = 0; pass < 2; pass++) {
8556 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8557 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
8558 TCGv_i64 tcg_op2_wide = tcg_temp_new_i64();
8559 static NeonGenWidenFn * const widenfns[3][2] = {
8560 { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
8561 { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
8562 { tcg_gen_ext_i32_i64, tcg_gen_extu_i32_i64 },
8564 NeonGenWidenFn *widenfn = widenfns[size][is_u];
8566 read_vec_element(s, tcg_op1, rn, pass, MO_64);
8567 read_vec_element_i32(s, tcg_op2, rm, part + pass, MO_32);
8568 widenfn(tcg_op2_wide, tcg_op2);
8569 tcg_temp_free_i32(tcg_op2);
8570 tcg_res[pass] = tcg_temp_new_i64();
8571 gen_neon_addl(size, (opcode == 3),
8572 tcg_res[pass], tcg_op1, tcg_op2_wide);
8573 tcg_temp_free_i64(tcg_op1);
8574 tcg_temp_free_i64(tcg_op2_wide);
8577 for (pass = 0; pass < 2; pass++) {
8578 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
8579 tcg_temp_free_i64(tcg_res[pass]);
8583 static void do_narrow_high_u32(TCGv_i32 res, TCGv_i64 in)
8585 tcg_gen_shri_i64(in, in, 32);
8586 tcg_gen_trunc_i64_i32(res, in);
8589 static void do_narrow_round_high_u32(TCGv_i32 res, TCGv_i64 in)
8591 tcg_gen_addi_i64(in, in, 1U << 31);
8592 do_narrow_high_u32(res, in);
8595 static void handle_3rd_narrowing(DisasContext *s, int is_q, int is_u, int size,
8596 int opcode, int rd, int rn, int rm)
8598 TCGv_i32 tcg_res[2];
8599 int part = is_q ? 2 : 0;
8600 int pass;
8602 for (pass = 0; pass < 2; pass++) {
8603 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8604 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
8605 TCGv_i64 tcg_wideres = tcg_temp_new_i64();
8606 static NeonGenNarrowFn * const narrowfns[3][2] = {
8607 { gen_helper_neon_narrow_high_u8,
8608 gen_helper_neon_narrow_round_high_u8 },
8609 { gen_helper_neon_narrow_high_u16,
8610 gen_helper_neon_narrow_round_high_u16 },
8611 { do_narrow_high_u32, do_narrow_round_high_u32 },
8613 NeonGenNarrowFn *gennarrow = narrowfns[size][is_u];
8615 read_vec_element(s, tcg_op1, rn, pass, MO_64);
8616 read_vec_element(s, tcg_op2, rm, pass, MO_64);
8618 gen_neon_addl(size, (opcode == 6), tcg_wideres, tcg_op1, tcg_op2);
8620 tcg_temp_free_i64(tcg_op1);
8621 tcg_temp_free_i64(tcg_op2);
8623 tcg_res[pass] = tcg_temp_new_i32();
8624 gennarrow(tcg_res[pass], tcg_wideres);
8625 tcg_temp_free_i64(tcg_wideres);
8628 for (pass = 0; pass < 2; pass++) {
8629 write_vec_element_i32(s, tcg_res[pass], rd, pass + part, MO_32);
8630 tcg_temp_free_i32(tcg_res[pass]);
8632 if (!is_q) {
8633 clear_vec_high(s, rd);
8637 static void handle_pmull_64(DisasContext *s, int is_q, int rd, int rn, int rm)
8639 /* PMULL of 64 x 64 -> 128 is an odd special case because it
8640 * is the only three-reg-diff instruction which produces a
8641 * 128-bit wide result from a single operation. However since
8642 * it's possible to calculate the two halves more or less
8643 * separately we just use two helper calls.
8645 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8646 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
8647 TCGv_i64 tcg_res = tcg_temp_new_i64();
8649 read_vec_element(s, tcg_op1, rn, is_q, MO_64);
8650 read_vec_element(s, tcg_op2, rm, is_q, MO_64);
8651 gen_helper_neon_pmull_64_lo(tcg_res, tcg_op1, tcg_op2);
8652 write_vec_element(s, tcg_res, rd, 0, MO_64);
8653 gen_helper_neon_pmull_64_hi(tcg_res, tcg_op1, tcg_op2);
8654 write_vec_element(s, tcg_res, rd, 1, MO_64);
8656 tcg_temp_free_i64(tcg_op1);
8657 tcg_temp_free_i64(tcg_op2);
8658 tcg_temp_free_i64(tcg_res);
8661 /* C3.6.15 AdvSIMD three different
8662 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 0
8663 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
8664 * | 0 | Q | U | 0 1 1 1 0 | size | 1 | Rm | opcode | 0 0 | Rn | Rd |
8665 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
8667 static void disas_simd_three_reg_diff(DisasContext *s, uint32_t insn)
8669 /* Instructions in this group fall into three basic classes
8670 * (in each case with the operation working on each element in
8671 * the input vectors):
8672 * (1) widening 64 x 64 -> 128 (with possibly Vd as an extra
8673 * 128 bit input)
8674 * (2) wide 64 x 128 -> 128
8675 * (3) narrowing 128 x 128 -> 64
8676 * Here we do initial decode, catch unallocated cases and
8677 * dispatch to separate functions for each class.
8679 int is_q = extract32(insn, 30, 1);
8680 int is_u = extract32(insn, 29, 1);
8681 int size = extract32(insn, 22, 2);
8682 int opcode = extract32(insn, 12, 4);
8683 int rm = extract32(insn, 16, 5);
8684 int rn = extract32(insn, 5, 5);
8685 int rd = extract32(insn, 0, 5);
8687 switch (opcode) {
8688 case 1: /* SADDW, SADDW2, UADDW, UADDW2 */
8689 case 3: /* SSUBW, SSUBW2, USUBW, USUBW2 */
8690 /* 64 x 128 -> 128 */
8691 if (size == 3) {
8692 unallocated_encoding(s);
8693 return;
8695 if (!fp_access_check(s)) {
8696 return;
8698 handle_3rd_wide(s, is_q, is_u, size, opcode, rd, rn, rm);
8699 break;
8700 case 4: /* ADDHN, ADDHN2, RADDHN, RADDHN2 */
8701 case 6: /* SUBHN, SUBHN2, RSUBHN, RSUBHN2 */
8702 /* 128 x 128 -> 64 */
8703 if (size == 3) {
8704 unallocated_encoding(s);
8705 return;
8707 if (!fp_access_check(s)) {
8708 return;
8710 handle_3rd_narrowing(s, is_q, is_u, size, opcode, rd, rn, rm);
8711 break;
8712 case 14: /* PMULL, PMULL2 */
8713 if (is_u || size == 1 || size == 2) {
8714 unallocated_encoding(s);
8715 return;
8717 if (size == 3) {
8718 if (!arm_dc_feature(s, ARM_FEATURE_V8_PMULL)) {
8719 unallocated_encoding(s);
8720 return;
8722 if (!fp_access_check(s)) {
8723 return;
8725 handle_pmull_64(s, is_q, rd, rn, rm);
8726 return;
8728 goto is_widening;
8729 case 9: /* SQDMLAL, SQDMLAL2 */
8730 case 11: /* SQDMLSL, SQDMLSL2 */
8731 case 13: /* SQDMULL, SQDMULL2 */
8732 if (is_u || size == 0) {
8733 unallocated_encoding(s);
8734 return;
8736 /* fall through */
8737 case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
8738 case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
8739 case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
8740 case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
8741 case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
8742 case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
8743 case 12: /* SMULL, SMULL2, UMULL, UMULL2 */
8744 /* 64 x 64 -> 128 */
8745 if (size == 3) {
8746 unallocated_encoding(s);
8747 return;
8749 is_widening:
8750 if (!fp_access_check(s)) {
8751 return;
8754 handle_3rd_widening(s, is_q, is_u, size, opcode, rd, rn, rm);
8755 break;
8756 default:
8757 /* opcode 15 not allocated */
8758 unallocated_encoding(s);
8759 break;
8763 /* Logic op (opcode == 3) subgroup of C3.6.16. */
8764 static void disas_simd_3same_logic(DisasContext *s, uint32_t insn)
8766 int rd = extract32(insn, 0, 5);
8767 int rn = extract32(insn, 5, 5);
8768 int rm = extract32(insn, 16, 5);
8769 int size = extract32(insn, 22, 2);
8770 bool is_u = extract32(insn, 29, 1);
8771 bool is_q = extract32(insn, 30, 1);
8772 TCGv_i64 tcg_op1, tcg_op2, tcg_res[2];
8773 int pass;
8775 if (!fp_access_check(s)) {
8776 return;
8779 tcg_op1 = tcg_temp_new_i64();
8780 tcg_op2 = tcg_temp_new_i64();
8781 tcg_res[0] = tcg_temp_new_i64();
8782 tcg_res[1] = tcg_temp_new_i64();
8784 for (pass = 0; pass < (is_q ? 2 : 1); pass++) {
8785 read_vec_element(s, tcg_op1, rn, pass, MO_64);
8786 read_vec_element(s, tcg_op2, rm, pass, MO_64);
8788 if (!is_u) {
8789 switch (size) {
8790 case 0: /* AND */
8791 tcg_gen_and_i64(tcg_res[pass], tcg_op1, tcg_op2);
8792 break;
8793 case 1: /* BIC */
8794 tcg_gen_andc_i64(tcg_res[pass], tcg_op1, tcg_op2);
8795 break;
8796 case 2: /* ORR */
8797 tcg_gen_or_i64(tcg_res[pass], tcg_op1, tcg_op2);
8798 break;
8799 case 3: /* ORN */
8800 tcg_gen_orc_i64(tcg_res[pass], tcg_op1, tcg_op2);
8801 break;
8803 } else {
8804 if (size != 0) {
8805 /* B* ops need res loaded to operate on */
8806 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
8809 switch (size) {
8810 case 0: /* EOR */
8811 tcg_gen_xor_i64(tcg_res[pass], tcg_op1, tcg_op2);
8812 break;
8813 case 1: /* BSL bitwise select */
8814 tcg_gen_xor_i64(tcg_op1, tcg_op1, tcg_op2);
8815 tcg_gen_and_i64(tcg_op1, tcg_op1, tcg_res[pass]);
8816 tcg_gen_xor_i64(tcg_res[pass], tcg_op2, tcg_op1);
8817 break;
8818 case 2: /* BIT, bitwise insert if true */
8819 tcg_gen_xor_i64(tcg_op1, tcg_op1, tcg_res[pass]);
8820 tcg_gen_and_i64(tcg_op1, tcg_op1, tcg_op2);
8821 tcg_gen_xor_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
8822 break;
8823 case 3: /* BIF, bitwise insert if false */
8824 tcg_gen_xor_i64(tcg_op1, tcg_op1, tcg_res[pass]);
8825 tcg_gen_andc_i64(tcg_op1, tcg_op1, tcg_op2);
8826 tcg_gen_xor_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
8827 break;
8832 write_vec_element(s, tcg_res[0], rd, 0, MO_64);
8833 if (!is_q) {
8834 tcg_gen_movi_i64(tcg_res[1], 0);
8836 write_vec_element(s, tcg_res[1], rd, 1, MO_64);
8838 tcg_temp_free_i64(tcg_op1);
8839 tcg_temp_free_i64(tcg_op2);
8840 tcg_temp_free_i64(tcg_res[0]);
8841 tcg_temp_free_i64(tcg_res[1]);
8844 /* Helper functions for 32 bit comparisons */
8845 static void gen_max_s32(TCGv_i32 res, TCGv_i32 op1, TCGv_i32 op2)
8847 tcg_gen_movcond_i32(TCG_COND_GE, res, op1, op2, op1, op2);
8850 static void gen_max_u32(TCGv_i32 res, TCGv_i32 op1, TCGv_i32 op2)
8852 tcg_gen_movcond_i32(TCG_COND_GEU, res, op1, op2, op1, op2);
8855 static void gen_min_s32(TCGv_i32 res, TCGv_i32 op1, TCGv_i32 op2)
8857 tcg_gen_movcond_i32(TCG_COND_LE, res, op1, op2, op1, op2);
8860 static void gen_min_u32(TCGv_i32 res, TCGv_i32 op1, TCGv_i32 op2)
8862 tcg_gen_movcond_i32(TCG_COND_LEU, res, op1, op2, op1, op2);
8865 /* Pairwise op subgroup of C3.6.16.
8867 * This is called directly or via the handle_3same_float for float pairwise
8868 * operations where the opcode and size are calculated differently.
8870 static void handle_simd_3same_pair(DisasContext *s, int is_q, int u, int opcode,
8871 int size, int rn, int rm, int rd)
8873 TCGv_ptr fpst;
8874 int pass;
8876 /* Floating point operations need fpst */
8877 if (opcode >= 0x58) {
8878 fpst = get_fpstatus_ptr();
8879 } else {
8880 TCGV_UNUSED_PTR(fpst);
8883 if (!fp_access_check(s)) {
8884 return;
8887 /* These operations work on the concatenated rm:rn, with each pair of
8888 * adjacent elements being operated on to produce an element in the result.
8890 if (size == 3) {
8891 TCGv_i64 tcg_res[2];
8893 for (pass = 0; pass < 2; pass++) {
8894 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8895 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
8896 int passreg = (pass == 0) ? rn : rm;
8898 read_vec_element(s, tcg_op1, passreg, 0, MO_64);
8899 read_vec_element(s, tcg_op2, passreg, 1, MO_64);
8900 tcg_res[pass] = tcg_temp_new_i64();
8902 switch (opcode) {
8903 case 0x17: /* ADDP */
8904 tcg_gen_add_i64(tcg_res[pass], tcg_op1, tcg_op2);
8905 break;
8906 case 0x58: /* FMAXNMP */
8907 gen_helper_vfp_maxnumd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
8908 break;
8909 case 0x5a: /* FADDP */
8910 gen_helper_vfp_addd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
8911 break;
8912 case 0x5e: /* FMAXP */
8913 gen_helper_vfp_maxd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
8914 break;
8915 case 0x78: /* FMINNMP */
8916 gen_helper_vfp_minnumd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
8917 break;
8918 case 0x7e: /* FMINP */
8919 gen_helper_vfp_mind(tcg_res[pass], tcg_op1, tcg_op2, fpst);
8920 break;
8921 default:
8922 g_assert_not_reached();
8925 tcg_temp_free_i64(tcg_op1);
8926 tcg_temp_free_i64(tcg_op2);
8929 for (pass = 0; pass < 2; pass++) {
8930 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
8931 tcg_temp_free_i64(tcg_res[pass]);
8933 } else {
8934 int maxpass = is_q ? 4 : 2;
8935 TCGv_i32 tcg_res[4];
8937 for (pass = 0; pass < maxpass; pass++) {
8938 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
8939 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
8940 NeonGenTwoOpFn *genfn = NULL;
8941 int passreg = pass < (maxpass / 2) ? rn : rm;
8942 int passelt = (is_q && (pass & 1)) ? 2 : 0;
8944 read_vec_element_i32(s, tcg_op1, passreg, passelt, MO_32);
8945 read_vec_element_i32(s, tcg_op2, passreg, passelt + 1, MO_32);
8946 tcg_res[pass] = tcg_temp_new_i32();
8948 switch (opcode) {
8949 case 0x17: /* ADDP */
8951 static NeonGenTwoOpFn * const fns[3] = {
8952 gen_helper_neon_padd_u8,
8953 gen_helper_neon_padd_u16,
8954 tcg_gen_add_i32,
8956 genfn = fns[size];
8957 break;
8959 case 0x14: /* SMAXP, UMAXP */
8961 static NeonGenTwoOpFn * const fns[3][2] = {
8962 { gen_helper_neon_pmax_s8, gen_helper_neon_pmax_u8 },
8963 { gen_helper_neon_pmax_s16, gen_helper_neon_pmax_u16 },
8964 { gen_max_s32, gen_max_u32 },
8966 genfn = fns[size][u];
8967 break;
8969 case 0x15: /* SMINP, UMINP */
8971 static NeonGenTwoOpFn * const fns[3][2] = {
8972 { gen_helper_neon_pmin_s8, gen_helper_neon_pmin_u8 },
8973 { gen_helper_neon_pmin_s16, gen_helper_neon_pmin_u16 },
8974 { gen_min_s32, gen_min_u32 },
8976 genfn = fns[size][u];
8977 break;
8979 /* The FP operations are all on single floats (32 bit) */
8980 case 0x58: /* FMAXNMP */
8981 gen_helper_vfp_maxnums(tcg_res[pass], tcg_op1, tcg_op2, fpst);
8982 break;
8983 case 0x5a: /* FADDP */
8984 gen_helper_vfp_adds(tcg_res[pass], tcg_op1, tcg_op2, fpst);
8985 break;
8986 case 0x5e: /* FMAXP */
8987 gen_helper_vfp_maxs(tcg_res[pass], tcg_op1, tcg_op2, fpst);
8988 break;
8989 case 0x78: /* FMINNMP */
8990 gen_helper_vfp_minnums(tcg_res[pass], tcg_op1, tcg_op2, fpst);
8991 break;
8992 case 0x7e: /* FMINP */
8993 gen_helper_vfp_mins(tcg_res[pass], tcg_op1, tcg_op2, fpst);
8994 break;
8995 default:
8996 g_assert_not_reached();
8999 /* FP ops called directly, otherwise call now */
9000 if (genfn) {
9001 genfn(tcg_res[pass], tcg_op1, tcg_op2);
9004 tcg_temp_free_i32(tcg_op1);
9005 tcg_temp_free_i32(tcg_op2);
9008 for (pass = 0; pass < maxpass; pass++) {
9009 write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_32);
9010 tcg_temp_free_i32(tcg_res[pass]);
9012 if (!is_q) {
9013 clear_vec_high(s, rd);
9017 if (!TCGV_IS_UNUSED_PTR(fpst)) {
9018 tcg_temp_free_ptr(fpst);
9022 /* Floating point op subgroup of C3.6.16. */
9023 static void disas_simd_3same_float(DisasContext *s, uint32_t insn)
9025 /* For floating point ops, the U, size[1] and opcode bits
9026 * together indicate the operation. size[0] indicates single
9027 * or double.
9029 int fpopcode = extract32(insn, 11, 5)
9030 | (extract32(insn, 23, 1) << 5)
9031 | (extract32(insn, 29, 1) << 6);
9032 int is_q = extract32(insn, 30, 1);
9033 int size = extract32(insn, 22, 1);
9034 int rm = extract32(insn, 16, 5);
9035 int rn = extract32(insn, 5, 5);
9036 int rd = extract32(insn, 0, 5);
9038 int datasize = is_q ? 128 : 64;
9039 int esize = 32 << size;
9040 int elements = datasize / esize;
9042 if (size == 1 && !is_q) {
9043 unallocated_encoding(s);
9044 return;
9047 switch (fpopcode) {
9048 case 0x58: /* FMAXNMP */
9049 case 0x5a: /* FADDP */
9050 case 0x5e: /* FMAXP */
9051 case 0x78: /* FMINNMP */
9052 case 0x7e: /* FMINP */
9053 if (size && !is_q) {
9054 unallocated_encoding(s);
9055 return;
9057 handle_simd_3same_pair(s, is_q, 0, fpopcode, size ? MO_64 : MO_32,
9058 rn, rm, rd);
9059 return;
9060 case 0x1b: /* FMULX */
9061 case 0x1f: /* FRECPS */
9062 case 0x3f: /* FRSQRTS */
9063 case 0x5d: /* FACGE */
9064 case 0x7d: /* FACGT */
9065 case 0x19: /* FMLA */
9066 case 0x39: /* FMLS */
9067 case 0x18: /* FMAXNM */
9068 case 0x1a: /* FADD */
9069 case 0x1c: /* FCMEQ */
9070 case 0x1e: /* FMAX */
9071 case 0x38: /* FMINNM */
9072 case 0x3a: /* FSUB */
9073 case 0x3e: /* FMIN */
9074 case 0x5b: /* FMUL */
9075 case 0x5c: /* FCMGE */
9076 case 0x5f: /* FDIV */
9077 case 0x7a: /* FABD */
9078 case 0x7c: /* FCMGT */
9079 if (!fp_access_check(s)) {
9080 return;
9083 handle_3same_float(s, size, elements, fpopcode, rd, rn, rm);
9084 return;
9085 default:
9086 unallocated_encoding(s);
9087 return;
9091 /* Integer op subgroup of C3.6.16. */
9092 static void disas_simd_3same_int(DisasContext *s, uint32_t insn)
9094 int is_q = extract32(insn, 30, 1);
9095 int u = extract32(insn, 29, 1);
9096 int size = extract32(insn, 22, 2);
9097 int opcode = extract32(insn, 11, 5);
9098 int rm = extract32(insn, 16, 5);
9099 int rn = extract32(insn, 5, 5);
9100 int rd = extract32(insn, 0, 5);
9101 int pass;
9103 switch (opcode) {
9104 case 0x13: /* MUL, PMUL */
9105 if (u && size != 0) {
9106 unallocated_encoding(s);
9107 return;
9109 /* fall through */
9110 case 0x0: /* SHADD, UHADD */
9111 case 0x2: /* SRHADD, URHADD */
9112 case 0x4: /* SHSUB, UHSUB */
9113 case 0xc: /* SMAX, UMAX */
9114 case 0xd: /* SMIN, UMIN */
9115 case 0xe: /* SABD, UABD */
9116 case 0xf: /* SABA, UABA */
9117 case 0x12: /* MLA, MLS */
9118 if (size == 3) {
9119 unallocated_encoding(s);
9120 return;
9122 break;
9123 case 0x16: /* SQDMULH, SQRDMULH */
9124 if (size == 0 || size == 3) {
9125 unallocated_encoding(s);
9126 return;
9128 break;
9129 default:
9130 if (size == 3 && !is_q) {
9131 unallocated_encoding(s);
9132 return;
9134 break;
9137 if (!fp_access_check(s)) {
9138 return;
9141 if (size == 3) {
9142 assert(is_q);
9143 for (pass = 0; pass < 2; pass++) {
9144 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
9145 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
9146 TCGv_i64 tcg_res = tcg_temp_new_i64();
9148 read_vec_element(s, tcg_op1, rn, pass, MO_64);
9149 read_vec_element(s, tcg_op2, rm, pass, MO_64);
9151 handle_3same_64(s, opcode, u, tcg_res, tcg_op1, tcg_op2);
9153 write_vec_element(s, tcg_res, rd, pass, MO_64);
9155 tcg_temp_free_i64(tcg_res);
9156 tcg_temp_free_i64(tcg_op1);
9157 tcg_temp_free_i64(tcg_op2);
9159 } else {
9160 for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
9161 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
9162 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
9163 TCGv_i32 tcg_res = tcg_temp_new_i32();
9164 NeonGenTwoOpFn *genfn = NULL;
9165 NeonGenTwoOpEnvFn *genenvfn = NULL;
9167 read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
9168 read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
9170 switch (opcode) {
9171 case 0x0: /* SHADD, UHADD */
9173 static NeonGenTwoOpFn * const fns[3][2] = {
9174 { gen_helper_neon_hadd_s8, gen_helper_neon_hadd_u8 },
9175 { gen_helper_neon_hadd_s16, gen_helper_neon_hadd_u16 },
9176 { gen_helper_neon_hadd_s32, gen_helper_neon_hadd_u32 },
9178 genfn = fns[size][u];
9179 break;
9181 case 0x1: /* SQADD, UQADD */
9183 static NeonGenTwoOpEnvFn * const fns[3][2] = {
9184 { gen_helper_neon_qadd_s8, gen_helper_neon_qadd_u8 },
9185 { gen_helper_neon_qadd_s16, gen_helper_neon_qadd_u16 },
9186 { gen_helper_neon_qadd_s32, gen_helper_neon_qadd_u32 },
9188 genenvfn = fns[size][u];
9189 break;
9191 case 0x2: /* SRHADD, URHADD */
9193 static NeonGenTwoOpFn * const fns[3][2] = {
9194 { gen_helper_neon_rhadd_s8, gen_helper_neon_rhadd_u8 },
9195 { gen_helper_neon_rhadd_s16, gen_helper_neon_rhadd_u16 },
9196 { gen_helper_neon_rhadd_s32, gen_helper_neon_rhadd_u32 },
9198 genfn = fns[size][u];
9199 break;
9201 case 0x4: /* SHSUB, UHSUB */
9203 static NeonGenTwoOpFn * const fns[3][2] = {
9204 { gen_helper_neon_hsub_s8, gen_helper_neon_hsub_u8 },
9205 { gen_helper_neon_hsub_s16, gen_helper_neon_hsub_u16 },
9206 { gen_helper_neon_hsub_s32, gen_helper_neon_hsub_u32 },
9208 genfn = fns[size][u];
9209 break;
9211 case 0x5: /* SQSUB, UQSUB */
9213 static NeonGenTwoOpEnvFn * const fns[3][2] = {
9214 { gen_helper_neon_qsub_s8, gen_helper_neon_qsub_u8 },
9215 { gen_helper_neon_qsub_s16, gen_helper_neon_qsub_u16 },
9216 { gen_helper_neon_qsub_s32, gen_helper_neon_qsub_u32 },
9218 genenvfn = fns[size][u];
9219 break;
9221 case 0x6: /* CMGT, CMHI */
9223 static NeonGenTwoOpFn * const fns[3][2] = {
9224 { gen_helper_neon_cgt_s8, gen_helper_neon_cgt_u8 },
9225 { gen_helper_neon_cgt_s16, gen_helper_neon_cgt_u16 },
9226 { gen_helper_neon_cgt_s32, gen_helper_neon_cgt_u32 },
9228 genfn = fns[size][u];
9229 break;
9231 case 0x7: /* CMGE, CMHS */
9233 static NeonGenTwoOpFn * const fns[3][2] = {
9234 { gen_helper_neon_cge_s8, gen_helper_neon_cge_u8 },
9235 { gen_helper_neon_cge_s16, gen_helper_neon_cge_u16 },
9236 { gen_helper_neon_cge_s32, gen_helper_neon_cge_u32 },
9238 genfn = fns[size][u];
9239 break;
9241 case 0x8: /* SSHL, USHL */
9243 static NeonGenTwoOpFn * const fns[3][2] = {
9244 { gen_helper_neon_shl_s8, gen_helper_neon_shl_u8 },
9245 { gen_helper_neon_shl_s16, gen_helper_neon_shl_u16 },
9246 { gen_helper_neon_shl_s32, gen_helper_neon_shl_u32 },
9248 genfn = fns[size][u];
9249 break;
9251 case 0x9: /* SQSHL, UQSHL */
9253 static NeonGenTwoOpEnvFn * const fns[3][2] = {
9254 { gen_helper_neon_qshl_s8, gen_helper_neon_qshl_u8 },
9255 { gen_helper_neon_qshl_s16, gen_helper_neon_qshl_u16 },
9256 { gen_helper_neon_qshl_s32, gen_helper_neon_qshl_u32 },
9258 genenvfn = fns[size][u];
9259 break;
9261 case 0xa: /* SRSHL, URSHL */
9263 static NeonGenTwoOpFn * const fns[3][2] = {
9264 { gen_helper_neon_rshl_s8, gen_helper_neon_rshl_u8 },
9265 { gen_helper_neon_rshl_s16, gen_helper_neon_rshl_u16 },
9266 { gen_helper_neon_rshl_s32, gen_helper_neon_rshl_u32 },
9268 genfn = fns[size][u];
9269 break;
9271 case 0xb: /* SQRSHL, UQRSHL */
9273 static NeonGenTwoOpEnvFn * const fns[3][2] = {
9274 { gen_helper_neon_qrshl_s8, gen_helper_neon_qrshl_u8 },
9275 { gen_helper_neon_qrshl_s16, gen_helper_neon_qrshl_u16 },
9276 { gen_helper_neon_qrshl_s32, gen_helper_neon_qrshl_u32 },
9278 genenvfn = fns[size][u];
9279 break;
9281 case 0xc: /* SMAX, UMAX */
9283 static NeonGenTwoOpFn * const fns[3][2] = {
9284 { gen_helper_neon_max_s8, gen_helper_neon_max_u8 },
9285 { gen_helper_neon_max_s16, gen_helper_neon_max_u16 },
9286 { gen_max_s32, gen_max_u32 },
9288 genfn = fns[size][u];
9289 break;
9292 case 0xd: /* SMIN, UMIN */
9294 static NeonGenTwoOpFn * const fns[3][2] = {
9295 { gen_helper_neon_min_s8, gen_helper_neon_min_u8 },
9296 { gen_helper_neon_min_s16, gen_helper_neon_min_u16 },
9297 { gen_min_s32, gen_min_u32 },
9299 genfn = fns[size][u];
9300 break;
9302 case 0xe: /* SABD, UABD */
9303 case 0xf: /* SABA, UABA */
9305 static NeonGenTwoOpFn * const fns[3][2] = {
9306 { gen_helper_neon_abd_s8, gen_helper_neon_abd_u8 },
9307 { gen_helper_neon_abd_s16, gen_helper_neon_abd_u16 },
9308 { gen_helper_neon_abd_s32, gen_helper_neon_abd_u32 },
9310 genfn = fns[size][u];
9311 break;
9313 case 0x10: /* ADD, SUB */
9315 static NeonGenTwoOpFn * const fns[3][2] = {
9316 { gen_helper_neon_add_u8, gen_helper_neon_sub_u8 },
9317 { gen_helper_neon_add_u16, gen_helper_neon_sub_u16 },
9318 { tcg_gen_add_i32, tcg_gen_sub_i32 },
9320 genfn = fns[size][u];
9321 break;
9323 case 0x11: /* CMTST, CMEQ */
9325 static NeonGenTwoOpFn * const fns[3][2] = {
9326 { gen_helper_neon_tst_u8, gen_helper_neon_ceq_u8 },
9327 { gen_helper_neon_tst_u16, gen_helper_neon_ceq_u16 },
9328 { gen_helper_neon_tst_u32, gen_helper_neon_ceq_u32 },
9330 genfn = fns[size][u];
9331 break;
9333 case 0x13: /* MUL, PMUL */
9334 if (u) {
9335 /* PMUL */
9336 assert(size == 0);
9337 genfn = gen_helper_neon_mul_p8;
9338 break;
9340 /* fall through : MUL */
9341 case 0x12: /* MLA, MLS */
9343 static NeonGenTwoOpFn * const fns[3] = {
9344 gen_helper_neon_mul_u8,
9345 gen_helper_neon_mul_u16,
9346 tcg_gen_mul_i32,
9348 genfn = fns[size];
9349 break;
9351 case 0x16: /* SQDMULH, SQRDMULH */
9353 static NeonGenTwoOpEnvFn * const fns[2][2] = {
9354 { gen_helper_neon_qdmulh_s16, gen_helper_neon_qrdmulh_s16 },
9355 { gen_helper_neon_qdmulh_s32, gen_helper_neon_qrdmulh_s32 },
9357 assert(size == 1 || size == 2);
9358 genenvfn = fns[size - 1][u];
9359 break;
9361 default:
9362 g_assert_not_reached();
9365 if (genenvfn) {
9366 genenvfn(tcg_res, cpu_env, tcg_op1, tcg_op2);
9367 } else {
9368 genfn(tcg_res, tcg_op1, tcg_op2);
9371 if (opcode == 0xf || opcode == 0x12) {
9372 /* SABA, UABA, MLA, MLS: accumulating ops */
9373 static NeonGenTwoOpFn * const fns[3][2] = {
9374 { gen_helper_neon_add_u8, gen_helper_neon_sub_u8 },
9375 { gen_helper_neon_add_u16, gen_helper_neon_sub_u16 },
9376 { tcg_gen_add_i32, tcg_gen_sub_i32 },
9378 bool is_sub = (opcode == 0x12 && u); /* MLS */
9380 genfn = fns[size][is_sub];
9381 read_vec_element_i32(s, tcg_op1, rd, pass, MO_32);
9382 genfn(tcg_res, tcg_op1, tcg_res);
9385 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
9387 tcg_temp_free_i32(tcg_res);
9388 tcg_temp_free_i32(tcg_op1);
9389 tcg_temp_free_i32(tcg_op2);
9393 if (!is_q) {
9394 clear_vec_high(s, rd);
9398 /* C3.6.16 AdvSIMD three same
9399 * 31 30 29 28 24 23 22 21 20 16 15 11 10 9 5 4 0
9400 * +---+---+---+-----------+------+---+------+--------+---+------+------+
9401 * | 0 | Q | U | 0 1 1 1 0 | size | 1 | Rm | opcode | 1 | Rn | Rd |
9402 * +---+---+---+-----------+------+---+------+--------+---+------+------+
9404 static void disas_simd_three_reg_same(DisasContext *s, uint32_t insn)
9406 int opcode = extract32(insn, 11, 5);
9408 switch (opcode) {
9409 case 0x3: /* logic ops */
9410 disas_simd_3same_logic(s, insn);
9411 break;
9412 case 0x17: /* ADDP */
9413 case 0x14: /* SMAXP, UMAXP */
9414 case 0x15: /* SMINP, UMINP */
9416 /* Pairwise operations */
9417 int is_q = extract32(insn, 30, 1);
9418 int u = extract32(insn, 29, 1);
9419 int size = extract32(insn, 22, 2);
9420 int rm = extract32(insn, 16, 5);
9421 int rn = extract32(insn, 5, 5);
9422 int rd = extract32(insn, 0, 5);
9423 if (opcode == 0x17) {
9424 if (u || (size == 3 && !is_q)) {
9425 unallocated_encoding(s);
9426 return;
9428 } else {
9429 if (size == 3) {
9430 unallocated_encoding(s);
9431 return;
9434 handle_simd_3same_pair(s, is_q, u, opcode, size, rn, rm, rd);
9435 break;
9437 case 0x18 ... 0x31:
9438 /* floating point ops, sz[1] and U are part of opcode */
9439 disas_simd_3same_float(s, insn);
9440 break;
9441 default:
9442 disas_simd_3same_int(s, insn);
9443 break;
9447 static void handle_2misc_widening(DisasContext *s, int opcode, bool is_q,
9448 int size, int rn, int rd)
9450 /* Handle 2-reg-misc ops which are widening (so each size element
9451 * in the source becomes a 2*size element in the destination.
9452 * The only instruction like this is FCVTL.
9454 int pass;
9456 if (size == 3) {
9457 /* 32 -> 64 bit fp conversion */
9458 TCGv_i64 tcg_res[2];
9459 int srcelt = is_q ? 2 : 0;
9461 for (pass = 0; pass < 2; pass++) {
9462 TCGv_i32 tcg_op = tcg_temp_new_i32();
9463 tcg_res[pass] = tcg_temp_new_i64();
9465 read_vec_element_i32(s, tcg_op, rn, srcelt + pass, MO_32);
9466 gen_helper_vfp_fcvtds(tcg_res[pass], tcg_op, cpu_env);
9467 tcg_temp_free_i32(tcg_op);
9469 for (pass = 0; pass < 2; pass++) {
9470 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
9471 tcg_temp_free_i64(tcg_res[pass]);
9473 } else {
9474 /* 16 -> 32 bit fp conversion */
9475 int srcelt = is_q ? 4 : 0;
9476 TCGv_i32 tcg_res[4];
9478 for (pass = 0; pass < 4; pass++) {
9479 tcg_res[pass] = tcg_temp_new_i32();
9481 read_vec_element_i32(s, tcg_res[pass], rn, srcelt + pass, MO_16);
9482 gen_helper_vfp_fcvt_f16_to_f32(tcg_res[pass], tcg_res[pass],
9483 cpu_env);
9485 for (pass = 0; pass < 4; pass++) {
9486 write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_32);
9487 tcg_temp_free_i32(tcg_res[pass]);
9492 static void handle_rev(DisasContext *s, int opcode, bool u,
9493 bool is_q, int size, int rn, int rd)
9495 int op = (opcode << 1) | u;
9496 int opsz = op + size;
9497 int grp_size = 3 - opsz;
9498 int dsize = is_q ? 128 : 64;
9499 int i;
9501 if (opsz >= 3) {
9502 unallocated_encoding(s);
9503 return;
9506 if (!fp_access_check(s)) {
9507 return;
9510 if (size == 0) {
9511 /* Special case bytes, use bswap op on each group of elements */
9512 int groups = dsize / (8 << grp_size);
9514 for (i = 0; i < groups; i++) {
9515 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
9517 read_vec_element(s, tcg_tmp, rn, i, grp_size);
9518 switch (grp_size) {
9519 case MO_16:
9520 tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp);
9521 break;
9522 case MO_32:
9523 tcg_gen_bswap32_i64(tcg_tmp, tcg_tmp);
9524 break;
9525 case MO_64:
9526 tcg_gen_bswap64_i64(tcg_tmp, tcg_tmp);
9527 break;
9528 default:
9529 g_assert_not_reached();
9531 write_vec_element(s, tcg_tmp, rd, i, grp_size);
9532 tcg_temp_free_i64(tcg_tmp);
9534 if (!is_q) {
9535 clear_vec_high(s, rd);
9537 } else {
9538 int revmask = (1 << grp_size) - 1;
9539 int esize = 8 << size;
9540 int elements = dsize / esize;
9541 TCGv_i64 tcg_rn = tcg_temp_new_i64();
9542 TCGv_i64 tcg_rd = tcg_const_i64(0);
9543 TCGv_i64 tcg_rd_hi = tcg_const_i64(0);
9545 for (i = 0; i < elements; i++) {
9546 int e_rev = (i & 0xf) ^ revmask;
9547 int off = e_rev * esize;
9548 read_vec_element(s, tcg_rn, rn, i, size);
9549 if (off >= 64) {
9550 tcg_gen_deposit_i64(tcg_rd_hi, tcg_rd_hi,
9551 tcg_rn, off - 64, esize);
9552 } else {
9553 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, off, esize);
9556 write_vec_element(s, tcg_rd, rd, 0, MO_64);
9557 write_vec_element(s, tcg_rd_hi, rd, 1, MO_64);
9559 tcg_temp_free_i64(tcg_rd_hi);
9560 tcg_temp_free_i64(tcg_rd);
9561 tcg_temp_free_i64(tcg_rn);
9565 static void handle_2misc_pairwise(DisasContext *s, int opcode, bool u,
9566 bool is_q, int size, int rn, int rd)
9568 /* Implement the pairwise operations from 2-misc:
9569 * SADDLP, UADDLP, SADALP, UADALP.
9570 * These all add pairs of elements in the input to produce a
9571 * double-width result element in the output (possibly accumulating).
9573 bool accum = (opcode == 0x6);
9574 int maxpass = is_q ? 2 : 1;
9575 int pass;
9576 TCGv_i64 tcg_res[2];
9578 if (size == 2) {
9579 /* 32 + 32 -> 64 op */
9580 TCGMemOp memop = size + (u ? 0 : MO_SIGN);
9582 for (pass = 0; pass < maxpass; pass++) {
9583 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
9584 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
9586 tcg_res[pass] = tcg_temp_new_i64();
9588 read_vec_element(s, tcg_op1, rn, pass * 2, memop);
9589 read_vec_element(s, tcg_op2, rn, pass * 2 + 1, memop);
9590 tcg_gen_add_i64(tcg_res[pass], tcg_op1, tcg_op2);
9591 if (accum) {
9592 read_vec_element(s, tcg_op1, rd, pass, MO_64);
9593 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
9596 tcg_temp_free_i64(tcg_op1);
9597 tcg_temp_free_i64(tcg_op2);
9599 } else {
9600 for (pass = 0; pass < maxpass; pass++) {
9601 TCGv_i64 tcg_op = tcg_temp_new_i64();
9602 NeonGenOneOpFn *genfn;
9603 static NeonGenOneOpFn * const fns[2][2] = {
9604 { gen_helper_neon_addlp_s8, gen_helper_neon_addlp_u8 },
9605 { gen_helper_neon_addlp_s16, gen_helper_neon_addlp_u16 },
9608 genfn = fns[size][u];
9610 tcg_res[pass] = tcg_temp_new_i64();
9612 read_vec_element(s, tcg_op, rn, pass, MO_64);
9613 genfn(tcg_res[pass], tcg_op);
9615 if (accum) {
9616 read_vec_element(s, tcg_op, rd, pass, MO_64);
9617 if (size == 0) {
9618 gen_helper_neon_addl_u16(tcg_res[pass],
9619 tcg_res[pass], tcg_op);
9620 } else {
9621 gen_helper_neon_addl_u32(tcg_res[pass],
9622 tcg_res[pass], tcg_op);
9625 tcg_temp_free_i64(tcg_op);
9628 if (!is_q) {
9629 tcg_res[1] = tcg_const_i64(0);
9631 for (pass = 0; pass < 2; pass++) {
9632 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
9633 tcg_temp_free_i64(tcg_res[pass]);
9637 static void handle_shll(DisasContext *s, bool is_q, int size, int rn, int rd)
9639 /* Implement SHLL and SHLL2 */
9640 int pass;
9641 int part = is_q ? 2 : 0;
9642 TCGv_i64 tcg_res[2];
9644 for (pass = 0; pass < 2; pass++) {
9645 static NeonGenWidenFn * const widenfns[3] = {
9646 gen_helper_neon_widen_u8,
9647 gen_helper_neon_widen_u16,
9648 tcg_gen_extu_i32_i64,
9650 NeonGenWidenFn *widenfn = widenfns[size];
9651 TCGv_i32 tcg_op = tcg_temp_new_i32();
9653 read_vec_element_i32(s, tcg_op, rn, part + pass, MO_32);
9654 tcg_res[pass] = tcg_temp_new_i64();
9655 widenfn(tcg_res[pass], tcg_op);
9656 tcg_gen_shli_i64(tcg_res[pass], tcg_res[pass], 8 << size);
9658 tcg_temp_free_i32(tcg_op);
9661 for (pass = 0; pass < 2; pass++) {
9662 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
9663 tcg_temp_free_i64(tcg_res[pass]);
9667 /* C3.6.17 AdvSIMD two reg misc
9668 * 31 30 29 28 24 23 22 21 17 16 12 11 10 9 5 4 0
9669 * +---+---+---+-----------+------+-----------+--------+-----+------+------+
9670 * | 0 | Q | U | 0 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 | Rn | Rd |
9671 * +---+---+---+-----------+------+-----------+--------+-----+------+------+
9673 static void disas_simd_two_reg_misc(DisasContext *s, uint32_t insn)
9675 int size = extract32(insn, 22, 2);
9676 int opcode = extract32(insn, 12, 5);
9677 bool u = extract32(insn, 29, 1);
9678 bool is_q = extract32(insn, 30, 1);
9679 int rn = extract32(insn, 5, 5);
9680 int rd = extract32(insn, 0, 5);
9681 bool need_fpstatus = false;
9682 bool need_rmode = false;
9683 int rmode = -1;
9684 TCGv_i32 tcg_rmode;
9685 TCGv_ptr tcg_fpstatus;
9687 switch (opcode) {
9688 case 0x0: /* REV64, REV32 */
9689 case 0x1: /* REV16 */
9690 handle_rev(s, opcode, u, is_q, size, rn, rd);
9691 return;
9692 case 0x5: /* CNT, NOT, RBIT */
9693 if (u && size == 0) {
9694 /* NOT: adjust size so we can use the 64-bits-at-a-time loop. */
9695 size = 3;
9696 break;
9697 } else if (u && size == 1) {
9698 /* RBIT */
9699 break;
9700 } else if (!u && size == 0) {
9701 /* CNT */
9702 break;
9704 unallocated_encoding(s);
9705 return;
9706 case 0x12: /* XTN, XTN2, SQXTUN, SQXTUN2 */
9707 case 0x14: /* SQXTN, SQXTN2, UQXTN, UQXTN2 */
9708 if (size == 3) {
9709 unallocated_encoding(s);
9710 return;
9712 if (!fp_access_check(s)) {
9713 return;
9716 handle_2misc_narrow(s, false, opcode, u, is_q, size, rn, rd);
9717 return;
9718 case 0x4: /* CLS, CLZ */
9719 if (size == 3) {
9720 unallocated_encoding(s);
9721 return;
9723 break;
9724 case 0x2: /* SADDLP, UADDLP */
9725 case 0x6: /* SADALP, UADALP */
9726 if (size == 3) {
9727 unallocated_encoding(s);
9728 return;
9730 if (!fp_access_check(s)) {
9731 return;
9733 handle_2misc_pairwise(s, opcode, u, is_q, size, rn, rd);
9734 return;
9735 case 0x13: /* SHLL, SHLL2 */
9736 if (u == 0 || size == 3) {
9737 unallocated_encoding(s);
9738 return;
9740 if (!fp_access_check(s)) {
9741 return;
9743 handle_shll(s, is_q, size, rn, rd);
9744 return;
9745 case 0xa: /* CMLT */
9746 if (u == 1) {
9747 unallocated_encoding(s);
9748 return;
9750 /* fall through */
9751 case 0x8: /* CMGT, CMGE */
9752 case 0x9: /* CMEQ, CMLE */
9753 case 0xb: /* ABS, NEG */
9754 if (size == 3 && !is_q) {
9755 unallocated_encoding(s);
9756 return;
9758 break;
9759 case 0x3: /* SUQADD, USQADD */
9760 if (size == 3 && !is_q) {
9761 unallocated_encoding(s);
9762 return;
9764 if (!fp_access_check(s)) {
9765 return;
9767 handle_2misc_satacc(s, false, u, is_q, size, rn, rd);
9768 return;
9769 case 0x7: /* SQABS, SQNEG */
9770 if (size == 3 && !is_q) {
9771 unallocated_encoding(s);
9772 return;
9774 break;
9775 case 0xc ... 0xf:
9776 case 0x16 ... 0x1d:
9777 case 0x1f:
9779 /* Floating point: U, size[1] and opcode indicate operation;
9780 * size[0] indicates single or double precision.
9782 int is_double = extract32(size, 0, 1);
9783 opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
9784 size = is_double ? 3 : 2;
9785 switch (opcode) {
9786 case 0x2f: /* FABS */
9787 case 0x6f: /* FNEG */
9788 if (size == 3 && !is_q) {
9789 unallocated_encoding(s);
9790 return;
9792 break;
9793 case 0x1d: /* SCVTF */
9794 case 0x5d: /* UCVTF */
9796 bool is_signed = (opcode == 0x1d) ? true : false;
9797 int elements = is_double ? 2 : is_q ? 4 : 2;
9798 if (is_double && !is_q) {
9799 unallocated_encoding(s);
9800 return;
9802 if (!fp_access_check(s)) {
9803 return;
9805 handle_simd_intfp_conv(s, rd, rn, elements, is_signed, 0, size);
9806 return;
9808 case 0x2c: /* FCMGT (zero) */
9809 case 0x2d: /* FCMEQ (zero) */
9810 case 0x2e: /* FCMLT (zero) */
9811 case 0x6c: /* FCMGE (zero) */
9812 case 0x6d: /* FCMLE (zero) */
9813 if (size == 3 && !is_q) {
9814 unallocated_encoding(s);
9815 return;
9817 handle_2misc_fcmp_zero(s, opcode, false, u, is_q, size, rn, rd);
9818 return;
9819 case 0x7f: /* FSQRT */
9820 if (size == 3 && !is_q) {
9821 unallocated_encoding(s);
9822 return;
9824 break;
9825 case 0x1a: /* FCVTNS */
9826 case 0x1b: /* FCVTMS */
9827 case 0x3a: /* FCVTPS */
9828 case 0x3b: /* FCVTZS */
9829 case 0x5a: /* FCVTNU */
9830 case 0x5b: /* FCVTMU */
9831 case 0x7a: /* FCVTPU */
9832 case 0x7b: /* FCVTZU */
9833 need_fpstatus = true;
9834 need_rmode = true;
9835 rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
9836 if (size == 3 && !is_q) {
9837 unallocated_encoding(s);
9838 return;
9840 break;
9841 case 0x5c: /* FCVTAU */
9842 case 0x1c: /* FCVTAS */
9843 need_fpstatus = true;
9844 need_rmode = true;
9845 rmode = FPROUNDING_TIEAWAY;
9846 if (size == 3 && !is_q) {
9847 unallocated_encoding(s);
9848 return;
9850 break;
9851 case 0x3c: /* URECPE */
9852 if (size == 3) {
9853 unallocated_encoding(s);
9854 return;
9856 /* fall through */
9857 case 0x3d: /* FRECPE */
9858 case 0x7d: /* FRSQRTE */
9859 if (size == 3 && !is_q) {
9860 unallocated_encoding(s);
9861 return;
9863 if (!fp_access_check(s)) {
9864 return;
9866 handle_2misc_reciprocal(s, opcode, false, u, is_q, size, rn, rd);
9867 return;
9868 case 0x56: /* FCVTXN, FCVTXN2 */
9869 if (size == 2) {
9870 unallocated_encoding(s);
9871 return;
9873 /* fall through */
9874 case 0x16: /* FCVTN, FCVTN2 */
9875 /* handle_2misc_narrow does a 2*size -> size operation, but these
9876 * instructions encode the source size rather than dest size.
9878 if (!fp_access_check(s)) {
9879 return;
9881 handle_2misc_narrow(s, false, opcode, 0, is_q, size - 1, rn, rd);
9882 return;
9883 case 0x17: /* FCVTL, FCVTL2 */
9884 if (!fp_access_check(s)) {
9885 return;
9887 handle_2misc_widening(s, opcode, is_q, size, rn, rd);
9888 return;
9889 case 0x18: /* FRINTN */
9890 case 0x19: /* FRINTM */
9891 case 0x38: /* FRINTP */
9892 case 0x39: /* FRINTZ */
9893 need_rmode = true;
9894 rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
9895 /* fall through */
9896 case 0x59: /* FRINTX */
9897 case 0x79: /* FRINTI */
9898 need_fpstatus = true;
9899 if (size == 3 && !is_q) {
9900 unallocated_encoding(s);
9901 return;
9903 break;
9904 case 0x58: /* FRINTA */
9905 need_rmode = true;
9906 rmode = FPROUNDING_TIEAWAY;
9907 need_fpstatus = true;
9908 if (size == 3 && !is_q) {
9909 unallocated_encoding(s);
9910 return;
9912 break;
9913 case 0x7c: /* URSQRTE */
9914 if (size == 3) {
9915 unallocated_encoding(s);
9916 return;
9918 need_fpstatus = true;
9919 break;
9920 default:
9921 unallocated_encoding(s);
9922 return;
9924 break;
9926 default:
9927 unallocated_encoding(s);
9928 return;
9931 if (!fp_access_check(s)) {
9932 return;
9935 if (need_fpstatus) {
9936 tcg_fpstatus = get_fpstatus_ptr();
9937 } else {
9938 TCGV_UNUSED_PTR(tcg_fpstatus);
9940 if (need_rmode) {
9941 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rmode));
9942 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
9943 } else {
9944 TCGV_UNUSED_I32(tcg_rmode);
9947 if (size == 3) {
9948 /* All 64-bit element operations can be shared with scalar 2misc */
9949 int pass;
9951 for (pass = 0; pass < (is_q ? 2 : 1); pass++) {
9952 TCGv_i64 tcg_op = tcg_temp_new_i64();
9953 TCGv_i64 tcg_res = tcg_temp_new_i64();
9955 read_vec_element(s, tcg_op, rn, pass, MO_64);
9957 handle_2misc_64(s, opcode, u, tcg_res, tcg_op,
9958 tcg_rmode, tcg_fpstatus);
9960 write_vec_element(s, tcg_res, rd, pass, MO_64);
9962 tcg_temp_free_i64(tcg_res);
9963 tcg_temp_free_i64(tcg_op);
9965 } else {
9966 int pass;
9968 for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
9969 TCGv_i32 tcg_op = tcg_temp_new_i32();
9970 TCGv_i32 tcg_res = tcg_temp_new_i32();
9971 TCGCond cond;
9973 read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
9975 if (size == 2) {
9976 /* Special cases for 32 bit elements */
9977 switch (opcode) {
9978 case 0xa: /* CMLT */
9979 /* 32 bit integer comparison against zero, result is
9980 * test ? (2^32 - 1) : 0. We implement via setcond(test)
9981 * and inverting.
9983 cond = TCG_COND_LT;
9984 do_cmop:
9985 tcg_gen_setcondi_i32(cond, tcg_res, tcg_op, 0);
9986 tcg_gen_neg_i32(tcg_res, tcg_res);
9987 break;
9988 case 0x8: /* CMGT, CMGE */
9989 cond = u ? TCG_COND_GE : TCG_COND_GT;
9990 goto do_cmop;
9991 case 0x9: /* CMEQ, CMLE */
9992 cond = u ? TCG_COND_LE : TCG_COND_EQ;
9993 goto do_cmop;
9994 case 0x4: /* CLS */
9995 if (u) {
9996 gen_helper_clz32(tcg_res, tcg_op);
9997 } else {
9998 gen_helper_cls32(tcg_res, tcg_op);
10000 break;
10001 case 0x7: /* SQABS, SQNEG */
10002 if (u) {
10003 gen_helper_neon_qneg_s32(tcg_res, cpu_env, tcg_op);
10004 } else {
10005 gen_helper_neon_qabs_s32(tcg_res, cpu_env, tcg_op);
10007 break;
10008 case 0xb: /* ABS, NEG */
10009 if (u) {
10010 tcg_gen_neg_i32(tcg_res, tcg_op);
10011 } else {
10012 TCGv_i32 tcg_zero = tcg_const_i32(0);
10013 tcg_gen_neg_i32(tcg_res, tcg_op);
10014 tcg_gen_movcond_i32(TCG_COND_GT, tcg_res, tcg_op,
10015 tcg_zero, tcg_op, tcg_res);
10016 tcg_temp_free_i32(tcg_zero);
10018 break;
10019 case 0x2f: /* FABS */
10020 gen_helper_vfp_abss(tcg_res, tcg_op);
10021 break;
10022 case 0x6f: /* FNEG */
10023 gen_helper_vfp_negs(tcg_res, tcg_op);
10024 break;
10025 case 0x7f: /* FSQRT */
10026 gen_helper_vfp_sqrts(tcg_res, tcg_op, cpu_env);
10027 break;
10028 case 0x1a: /* FCVTNS */
10029 case 0x1b: /* FCVTMS */
10030 case 0x1c: /* FCVTAS */
10031 case 0x3a: /* FCVTPS */
10032 case 0x3b: /* FCVTZS */
10034 TCGv_i32 tcg_shift = tcg_const_i32(0);
10035 gen_helper_vfp_tosls(tcg_res, tcg_op,
10036 tcg_shift, tcg_fpstatus);
10037 tcg_temp_free_i32(tcg_shift);
10038 break;
10040 case 0x5a: /* FCVTNU */
10041 case 0x5b: /* FCVTMU */
10042 case 0x5c: /* FCVTAU */
10043 case 0x7a: /* FCVTPU */
10044 case 0x7b: /* FCVTZU */
10046 TCGv_i32 tcg_shift = tcg_const_i32(0);
10047 gen_helper_vfp_touls(tcg_res, tcg_op,
10048 tcg_shift, tcg_fpstatus);
10049 tcg_temp_free_i32(tcg_shift);
10050 break;
10052 case 0x18: /* FRINTN */
10053 case 0x19: /* FRINTM */
10054 case 0x38: /* FRINTP */
10055 case 0x39: /* FRINTZ */
10056 case 0x58: /* FRINTA */
10057 case 0x79: /* FRINTI */
10058 gen_helper_rints(tcg_res, tcg_op, tcg_fpstatus);
10059 break;
10060 case 0x59: /* FRINTX */
10061 gen_helper_rints_exact(tcg_res, tcg_op, tcg_fpstatus);
10062 break;
10063 case 0x7c: /* URSQRTE */
10064 gen_helper_rsqrte_u32(tcg_res, tcg_op, tcg_fpstatus);
10065 break;
10066 default:
10067 g_assert_not_reached();
10069 } else {
10070 /* Use helpers for 8 and 16 bit elements */
10071 switch (opcode) {
10072 case 0x5: /* CNT, RBIT */
10073 /* For these two insns size is part of the opcode specifier
10074 * (handled earlier); they always operate on byte elements.
10076 if (u) {
10077 gen_helper_neon_rbit_u8(tcg_res, tcg_op);
10078 } else {
10079 gen_helper_neon_cnt_u8(tcg_res, tcg_op);
10081 break;
10082 case 0x7: /* SQABS, SQNEG */
10084 NeonGenOneOpEnvFn *genfn;
10085 static NeonGenOneOpEnvFn * const fns[2][2] = {
10086 { gen_helper_neon_qabs_s8, gen_helper_neon_qneg_s8 },
10087 { gen_helper_neon_qabs_s16, gen_helper_neon_qneg_s16 },
10089 genfn = fns[size][u];
10090 genfn(tcg_res, cpu_env, tcg_op);
10091 break;
10093 case 0x8: /* CMGT, CMGE */
10094 case 0x9: /* CMEQ, CMLE */
10095 case 0xa: /* CMLT */
10097 static NeonGenTwoOpFn * const fns[3][2] = {
10098 { gen_helper_neon_cgt_s8, gen_helper_neon_cgt_s16 },
10099 { gen_helper_neon_cge_s8, gen_helper_neon_cge_s16 },
10100 { gen_helper_neon_ceq_u8, gen_helper_neon_ceq_u16 },
10102 NeonGenTwoOpFn *genfn;
10103 int comp;
10104 bool reverse;
10105 TCGv_i32 tcg_zero = tcg_const_i32(0);
10107 /* comp = index into [CMGT, CMGE, CMEQ, CMLE, CMLT] */
10108 comp = (opcode - 0x8) * 2 + u;
10109 /* ...but LE, LT are implemented as reverse GE, GT */
10110 reverse = (comp > 2);
10111 if (reverse) {
10112 comp = 4 - comp;
10114 genfn = fns[comp][size];
10115 if (reverse) {
10116 genfn(tcg_res, tcg_zero, tcg_op);
10117 } else {
10118 genfn(tcg_res, tcg_op, tcg_zero);
10120 tcg_temp_free_i32(tcg_zero);
10121 break;
10123 case 0xb: /* ABS, NEG */
10124 if (u) {
10125 TCGv_i32 tcg_zero = tcg_const_i32(0);
10126 if (size) {
10127 gen_helper_neon_sub_u16(tcg_res, tcg_zero, tcg_op);
10128 } else {
10129 gen_helper_neon_sub_u8(tcg_res, tcg_zero, tcg_op);
10131 tcg_temp_free_i32(tcg_zero);
10132 } else {
10133 if (size) {
10134 gen_helper_neon_abs_s16(tcg_res, tcg_op);
10135 } else {
10136 gen_helper_neon_abs_s8(tcg_res, tcg_op);
10139 break;
10140 case 0x4: /* CLS, CLZ */
10141 if (u) {
10142 if (size == 0) {
10143 gen_helper_neon_clz_u8(tcg_res, tcg_op);
10144 } else {
10145 gen_helper_neon_clz_u16(tcg_res, tcg_op);
10147 } else {
10148 if (size == 0) {
10149 gen_helper_neon_cls_s8(tcg_res, tcg_op);
10150 } else {
10151 gen_helper_neon_cls_s16(tcg_res, tcg_op);
10154 break;
10155 default:
10156 g_assert_not_reached();
10160 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
10162 tcg_temp_free_i32(tcg_res);
10163 tcg_temp_free_i32(tcg_op);
10166 if (!is_q) {
10167 clear_vec_high(s, rd);
10170 if (need_rmode) {
10171 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
10172 tcg_temp_free_i32(tcg_rmode);
10174 if (need_fpstatus) {
10175 tcg_temp_free_ptr(tcg_fpstatus);
10179 /* C3.6.13 AdvSIMD scalar x indexed element
10180 * 31 30 29 28 24 23 22 21 20 19 16 15 12 11 10 9 5 4 0
10181 * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
10182 * | 0 1 | U | 1 1 1 1 1 | size | L | M | Rm | opc | H | 0 | Rn | Rd |
10183 * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
10184 * C3.6.18 AdvSIMD vector x indexed element
10185 * 31 30 29 28 24 23 22 21 20 19 16 15 12 11 10 9 5 4 0
10186 * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
10187 * | 0 | Q | U | 0 1 1 1 1 | size | L | M | Rm | opc | H | 0 | Rn | Rd |
10188 * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
10190 static void disas_simd_indexed(DisasContext *s, uint32_t insn)
10192 /* This encoding has two kinds of instruction:
10193 * normal, where we perform elt x idxelt => elt for each
10194 * element in the vector
10195 * long, where we perform elt x idxelt and generate a result of
10196 * double the width of the input element
10197 * The long ops have a 'part' specifier (ie come in INSN, INSN2 pairs).
10199 bool is_scalar = extract32(insn, 28, 1);
10200 bool is_q = extract32(insn, 30, 1);
10201 bool u = extract32(insn, 29, 1);
10202 int size = extract32(insn, 22, 2);
10203 int l = extract32(insn, 21, 1);
10204 int m = extract32(insn, 20, 1);
10205 /* Note that the Rm field here is only 4 bits, not 5 as it usually is */
10206 int rm = extract32(insn, 16, 4);
10207 int opcode = extract32(insn, 12, 4);
10208 int h = extract32(insn, 11, 1);
10209 int rn = extract32(insn, 5, 5);
10210 int rd = extract32(insn, 0, 5);
10211 bool is_long = false;
10212 bool is_fp = false;
10213 int index;
10214 TCGv_ptr fpst;
10216 switch (opcode) {
10217 case 0x0: /* MLA */
10218 case 0x4: /* MLS */
10219 if (!u || is_scalar) {
10220 unallocated_encoding(s);
10221 return;
10223 break;
10224 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10225 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10226 case 0xa: /* SMULL, SMULL2, UMULL, UMULL2 */
10227 if (is_scalar) {
10228 unallocated_encoding(s);
10229 return;
10231 is_long = true;
10232 break;
10233 case 0x3: /* SQDMLAL, SQDMLAL2 */
10234 case 0x7: /* SQDMLSL, SQDMLSL2 */
10235 case 0xb: /* SQDMULL, SQDMULL2 */
10236 is_long = true;
10237 /* fall through */
10238 case 0xc: /* SQDMULH */
10239 case 0xd: /* SQRDMULH */
10240 if (u) {
10241 unallocated_encoding(s);
10242 return;
10244 break;
10245 case 0x8: /* MUL */
10246 if (u || is_scalar) {
10247 unallocated_encoding(s);
10248 return;
10250 break;
10251 case 0x1: /* FMLA */
10252 case 0x5: /* FMLS */
10253 if (u) {
10254 unallocated_encoding(s);
10255 return;
10257 /* fall through */
10258 case 0x9: /* FMUL, FMULX */
10259 if (!extract32(size, 1, 1)) {
10260 unallocated_encoding(s);
10261 return;
10263 is_fp = true;
10264 break;
10265 default:
10266 unallocated_encoding(s);
10267 return;
10270 if (is_fp) {
10271 /* low bit of size indicates single/double */
10272 size = extract32(size, 0, 1) ? 3 : 2;
10273 if (size == 2) {
10274 index = h << 1 | l;
10275 } else {
10276 if (l || !is_q) {
10277 unallocated_encoding(s);
10278 return;
10280 index = h;
10282 rm |= (m << 4);
10283 } else {
10284 switch (size) {
10285 case 1:
10286 index = h << 2 | l << 1 | m;
10287 break;
10288 case 2:
10289 index = h << 1 | l;
10290 rm |= (m << 4);
10291 break;
10292 default:
10293 unallocated_encoding(s);
10294 return;
10298 if (!fp_access_check(s)) {
10299 return;
10302 if (is_fp) {
10303 fpst = get_fpstatus_ptr();
10304 } else {
10305 TCGV_UNUSED_PTR(fpst);
10308 if (size == 3) {
10309 TCGv_i64 tcg_idx = tcg_temp_new_i64();
10310 int pass;
10312 assert(is_fp && is_q && !is_long);
10314 read_vec_element(s, tcg_idx, rm, index, MO_64);
10316 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
10317 TCGv_i64 tcg_op = tcg_temp_new_i64();
10318 TCGv_i64 tcg_res = tcg_temp_new_i64();
10320 read_vec_element(s, tcg_op, rn, pass, MO_64);
10322 switch (opcode) {
10323 case 0x5: /* FMLS */
10324 /* As usual for ARM, separate negation for fused multiply-add */
10325 gen_helper_vfp_negd(tcg_op, tcg_op);
10326 /* fall through */
10327 case 0x1: /* FMLA */
10328 read_vec_element(s, tcg_res, rd, pass, MO_64);
10329 gen_helper_vfp_muladdd(tcg_res, tcg_op, tcg_idx, tcg_res, fpst);
10330 break;
10331 case 0x9: /* FMUL, FMULX */
10332 if (u) {
10333 gen_helper_vfp_mulxd(tcg_res, tcg_op, tcg_idx, fpst);
10334 } else {
10335 gen_helper_vfp_muld(tcg_res, tcg_op, tcg_idx, fpst);
10337 break;
10338 default:
10339 g_assert_not_reached();
10342 write_vec_element(s, tcg_res, rd, pass, MO_64);
10343 tcg_temp_free_i64(tcg_op);
10344 tcg_temp_free_i64(tcg_res);
10347 if (is_scalar) {
10348 clear_vec_high(s, rd);
10351 tcg_temp_free_i64(tcg_idx);
10352 } else if (!is_long) {
10353 /* 32 bit floating point, or 16 or 32 bit integer.
10354 * For the 16 bit scalar case we use the usual Neon helpers and
10355 * rely on the fact that 0 op 0 == 0 with no side effects.
10357 TCGv_i32 tcg_idx = tcg_temp_new_i32();
10358 int pass, maxpasses;
10360 if (is_scalar) {
10361 maxpasses = 1;
10362 } else {
10363 maxpasses = is_q ? 4 : 2;
10366 read_vec_element_i32(s, tcg_idx, rm, index, size);
10368 if (size == 1 && !is_scalar) {
10369 /* The simplest way to handle the 16x16 indexed ops is to duplicate
10370 * the index into both halves of the 32 bit tcg_idx and then use
10371 * the usual Neon helpers.
10373 tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
10376 for (pass = 0; pass < maxpasses; pass++) {
10377 TCGv_i32 tcg_op = tcg_temp_new_i32();
10378 TCGv_i32 tcg_res = tcg_temp_new_i32();
10380 read_vec_element_i32(s, tcg_op, rn, pass, is_scalar ? size : MO_32);
10382 switch (opcode) {
10383 case 0x0: /* MLA */
10384 case 0x4: /* MLS */
10385 case 0x8: /* MUL */
10387 static NeonGenTwoOpFn * const fns[2][2] = {
10388 { gen_helper_neon_add_u16, gen_helper_neon_sub_u16 },
10389 { tcg_gen_add_i32, tcg_gen_sub_i32 },
10391 NeonGenTwoOpFn *genfn;
10392 bool is_sub = opcode == 0x4;
10394 if (size == 1) {
10395 gen_helper_neon_mul_u16(tcg_res, tcg_op, tcg_idx);
10396 } else {
10397 tcg_gen_mul_i32(tcg_res, tcg_op, tcg_idx);
10399 if (opcode == 0x8) {
10400 break;
10402 read_vec_element_i32(s, tcg_op, rd, pass, MO_32);
10403 genfn = fns[size - 1][is_sub];
10404 genfn(tcg_res, tcg_op, tcg_res);
10405 break;
10407 case 0x5: /* FMLS */
10408 /* As usual for ARM, separate negation for fused multiply-add */
10409 gen_helper_vfp_negs(tcg_op, tcg_op);
10410 /* fall through */
10411 case 0x1: /* FMLA */
10412 read_vec_element_i32(s, tcg_res, rd, pass, MO_32);
10413 gen_helper_vfp_muladds(tcg_res, tcg_op, tcg_idx, tcg_res, fpst);
10414 break;
10415 case 0x9: /* FMUL, FMULX */
10416 if (u) {
10417 gen_helper_vfp_mulxs(tcg_res, tcg_op, tcg_idx, fpst);
10418 } else {
10419 gen_helper_vfp_muls(tcg_res, tcg_op, tcg_idx, fpst);
10421 break;
10422 case 0xc: /* SQDMULH */
10423 if (size == 1) {
10424 gen_helper_neon_qdmulh_s16(tcg_res, cpu_env,
10425 tcg_op, tcg_idx);
10426 } else {
10427 gen_helper_neon_qdmulh_s32(tcg_res, cpu_env,
10428 tcg_op, tcg_idx);
10430 break;
10431 case 0xd: /* SQRDMULH */
10432 if (size == 1) {
10433 gen_helper_neon_qrdmulh_s16(tcg_res, cpu_env,
10434 tcg_op, tcg_idx);
10435 } else {
10436 gen_helper_neon_qrdmulh_s32(tcg_res, cpu_env,
10437 tcg_op, tcg_idx);
10439 break;
10440 default:
10441 g_assert_not_reached();
10444 if (is_scalar) {
10445 write_fp_sreg(s, rd, tcg_res);
10446 } else {
10447 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
10450 tcg_temp_free_i32(tcg_op);
10451 tcg_temp_free_i32(tcg_res);
10454 tcg_temp_free_i32(tcg_idx);
10456 if (!is_q) {
10457 clear_vec_high(s, rd);
10459 } else {
10460 /* long ops: 16x16->32 or 32x32->64 */
10461 TCGv_i64 tcg_res[2];
10462 int pass;
10463 bool satop = extract32(opcode, 0, 1);
10464 TCGMemOp memop = MO_32;
10466 if (satop || !u) {
10467 memop |= MO_SIGN;
10470 if (size == 2) {
10471 TCGv_i64 tcg_idx = tcg_temp_new_i64();
10473 read_vec_element(s, tcg_idx, rm, index, memop);
10475 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
10476 TCGv_i64 tcg_op = tcg_temp_new_i64();
10477 TCGv_i64 tcg_passres;
10478 int passelt;
10480 if (is_scalar) {
10481 passelt = 0;
10482 } else {
10483 passelt = pass + (is_q * 2);
10486 read_vec_element(s, tcg_op, rn, passelt, memop);
10488 tcg_res[pass] = tcg_temp_new_i64();
10490 if (opcode == 0xa || opcode == 0xb) {
10491 /* Non-accumulating ops */
10492 tcg_passres = tcg_res[pass];
10493 } else {
10494 tcg_passres = tcg_temp_new_i64();
10497 tcg_gen_mul_i64(tcg_passres, tcg_op, tcg_idx);
10498 tcg_temp_free_i64(tcg_op);
10500 if (satop) {
10501 /* saturating, doubling */
10502 gen_helper_neon_addl_saturate_s64(tcg_passres, cpu_env,
10503 tcg_passres, tcg_passres);
10506 if (opcode == 0xa || opcode == 0xb) {
10507 continue;
10510 /* Accumulating op: handle accumulate step */
10511 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
10513 switch (opcode) {
10514 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10515 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
10516 break;
10517 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10518 tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
10519 break;
10520 case 0x7: /* SQDMLSL, SQDMLSL2 */
10521 tcg_gen_neg_i64(tcg_passres, tcg_passres);
10522 /* fall through */
10523 case 0x3: /* SQDMLAL, SQDMLAL2 */
10524 gen_helper_neon_addl_saturate_s64(tcg_res[pass], cpu_env,
10525 tcg_res[pass],
10526 tcg_passres);
10527 break;
10528 default:
10529 g_assert_not_reached();
10531 tcg_temp_free_i64(tcg_passres);
10533 tcg_temp_free_i64(tcg_idx);
10535 if (is_scalar) {
10536 clear_vec_high(s, rd);
10538 } else {
10539 TCGv_i32 tcg_idx = tcg_temp_new_i32();
10541 assert(size == 1);
10542 read_vec_element_i32(s, tcg_idx, rm, index, size);
10544 if (!is_scalar) {
10545 /* The simplest way to handle the 16x16 indexed ops is to
10546 * duplicate the index into both halves of the 32 bit tcg_idx
10547 * and then use the usual Neon helpers.
10549 tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
10552 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
10553 TCGv_i32 tcg_op = tcg_temp_new_i32();
10554 TCGv_i64 tcg_passres;
10556 if (is_scalar) {
10557 read_vec_element_i32(s, tcg_op, rn, pass, size);
10558 } else {
10559 read_vec_element_i32(s, tcg_op, rn,
10560 pass + (is_q * 2), MO_32);
10563 tcg_res[pass] = tcg_temp_new_i64();
10565 if (opcode == 0xa || opcode == 0xb) {
10566 /* Non-accumulating ops */
10567 tcg_passres = tcg_res[pass];
10568 } else {
10569 tcg_passres = tcg_temp_new_i64();
10572 if (memop & MO_SIGN) {
10573 gen_helper_neon_mull_s16(tcg_passres, tcg_op, tcg_idx);
10574 } else {
10575 gen_helper_neon_mull_u16(tcg_passres, tcg_op, tcg_idx);
10577 if (satop) {
10578 gen_helper_neon_addl_saturate_s32(tcg_passres, cpu_env,
10579 tcg_passres, tcg_passres);
10581 tcg_temp_free_i32(tcg_op);
10583 if (opcode == 0xa || opcode == 0xb) {
10584 continue;
10587 /* Accumulating op: handle accumulate step */
10588 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
10590 switch (opcode) {
10591 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10592 gen_helper_neon_addl_u32(tcg_res[pass], tcg_res[pass],
10593 tcg_passres);
10594 break;
10595 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10596 gen_helper_neon_subl_u32(tcg_res[pass], tcg_res[pass],
10597 tcg_passres);
10598 break;
10599 case 0x7: /* SQDMLSL, SQDMLSL2 */
10600 gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
10601 /* fall through */
10602 case 0x3: /* SQDMLAL, SQDMLAL2 */
10603 gen_helper_neon_addl_saturate_s32(tcg_res[pass], cpu_env,
10604 tcg_res[pass],
10605 tcg_passres);
10606 break;
10607 default:
10608 g_assert_not_reached();
10610 tcg_temp_free_i64(tcg_passres);
10612 tcg_temp_free_i32(tcg_idx);
10614 if (is_scalar) {
10615 tcg_gen_ext32u_i64(tcg_res[0], tcg_res[0]);
10619 if (is_scalar) {
10620 tcg_res[1] = tcg_const_i64(0);
10623 for (pass = 0; pass < 2; pass++) {
10624 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
10625 tcg_temp_free_i64(tcg_res[pass]);
10629 if (!TCGV_IS_UNUSED_PTR(fpst)) {
10630 tcg_temp_free_ptr(fpst);
10634 /* C3.6.19 Crypto AES
10635 * 31 24 23 22 21 17 16 12 11 10 9 5 4 0
10636 * +-----------------+------+-----------+--------+-----+------+------+
10637 * | 0 1 0 0 1 1 1 0 | size | 1 0 1 0 0 | opcode | 1 0 | Rn | Rd |
10638 * +-----------------+------+-----------+--------+-----+------+------+
10640 static void disas_crypto_aes(DisasContext *s, uint32_t insn)
10642 int size = extract32(insn, 22, 2);
10643 int opcode = extract32(insn, 12, 5);
10644 int rn = extract32(insn, 5, 5);
10645 int rd = extract32(insn, 0, 5);
10646 int decrypt;
10647 TCGv_i32 tcg_rd_regno, tcg_rn_regno, tcg_decrypt;
10648 CryptoThreeOpEnvFn *genfn;
10650 if (!arm_dc_feature(s, ARM_FEATURE_V8_AES)
10651 || size != 0) {
10652 unallocated_encoding(s);
10653 return;
10656 switch (opcode) {
10657 case 0x4: /* AESE */
10658 decrypt = 0;
10659 genfn = gen_helper_crypto_aese;
10660 break;
10661 case 0x6: /* AESMC */
10662 decrypt = 0;
10663 genfn = gen_helper_crypto_aesmc;
10664 break;
10665 case 0x5: /* AESD */
10666 decrypt = 1;
10667 genfn = gen_helper_crypto_aese;
10668 break;
10669 case 0x7: /* AESIMC */
10670 decrypt = 1;
10671 genfn = gen_helper_crypto_aesmc;
10672 break;
10673 default:
10674 unallocated_encoding(s);
10675 return;
10678 /* Note that we convert the Vx register indexes into the
10679 * index within the vfp.regs[] array, so we can share the
10680 * helper with the AArch32 instructions.
10682 tcg_rd_regno = tcg_const_i32(rd << 1);
10683 tcg_rn_regno = tcg_const_i32(rn << 1);
10684 tcg_decrypt = tcg_const_i32(decrypt);
10686 genfn(cpu_env, tcg_rd_regno, tcg_rn_regno, tcg_decrypt);
10688 tcg_temp_free_i32(tcg_rd_regno);
10689 tcg_temp_free_i32(tcg_rn_regno);
10690 tcg_temp_free_i32(tcg_decrypt);
10693 /* C3.6.20 Crypto three-reg SHA
10694 * 31 24 23 22 21 20 16 15 14 12 11 10 9 5 4 0
10695 * +-----------------+------+---+------+---+--------+-----+------+------+
10696 * | 0 1 0 1 1 1 1 0 | size | 0 | Rm | 0 | opcode | 0 0 | Rn | Rd |
10697 * +-----------------+------+---+------+---+--------+-----+------+------+
10699 static void disas_crypto_three_reg_sha(DisasContext *s, uint32_t insn)
10701 int size = extract32(insn, 22, 2);
10702 int opcode = extract32(insn, 12, 3);
10703 int rm = extract32(insn, 16, 5);
10704 int rn = extract32(insn, 5, 5);
10705 int rd = extract32(insn, 0, 5);
10706 CryptoThreeOpEnvFn *genfn;
10707 TCGv_i32 tcg_rd_regno, tcg_rn_regno, tcg_rm_regno;
10708 int feature = ARM_FEATURE_V8_SHA256;
10710 if (size != 0) {
10711 unallocated_encoding(s);
10712 return;
10715 switch (opcode) {
10716 case 0: /* SHA1C */
10717 case 1: /* SHA1P */
10718 case 2: /* SHA1M */
10719 case 3: /* SHA1SU0 */
10720 genfn = NULL;
10721 feature = ARM_FEATURE_V8_SHA1;
10722 break;
10723 case 4: /* SHA256H */
10724 genfn = gen_helper_crypto_sha256h;
10725 break;
10726 case 5: /* SHA256H2 */
10727 genfn = gen_helper_crypto_sha256h2;
10728 break;
10729 case 6: /* SHA256SU1 */
10730 genfn = gen_helper_crypto_sha256su1;
10731 break;
10732 default:
10733 unallocated_encoding(s);
10734 return;
10737 if (!arm_dc_feature(s, feature)) {
10738 unallocated_encoding(s);
10739 return;
10742 tcg_rd_regno = tcg_const_i32(rd << 1);
10743 tcg_rn_regno = tcg_const_i32(rn << 1);
10744 tcg_rm_regno = tcg_const_i32(rm << 1);
10746 if (genfn) {
10747 genfn(cpu_env, tcg_rd_regno, tcg_rn_regno, tcg_rm_regno);
10748 } else {
10749 TCGv_i32 tcg_opcode = tcg_const_i32(opcode);
10751 gen_helper_crypto_sha1_3reg(cpu_env, tcg_rd_regno,
10752 tcg_rn_regno, tcg_rm_regno, tcg_opcode);
10753 tcg_temp_free_i32(tcg_opcode);
10756 tcg_temp_free_i32(tcg_rd_regno);
10757 tcg_temp_free_i32(tcg_rn_regno);
10758 tcg_temp_free_i32(tcg_rm_regno);
10761 /* C3.6.21 Crypto two-reg SHA
10762 * 31 24 23 22 21 17 16 12 11 10 9 5 4 0
10763 * +-----------------+------+-----------+--------+-----+------+------+
10764 * | 0 1 0 1 1 1 1 0 | size | 1 0 1 0 0 | opcode | 1 0 | Rn | Rd |
10765 * +-----------------+------+-----------+--------+-----+------+------+
10767 static void disas_crypto_two_reg_sha(DisasContext *s, uint32_t insn)
10769 int size = extract32(insn, 22, 2);
10770 int opcode = extract32(insn, 12, 5);
10771 int rn = extract32(insn, 5, 5);
10772 int rd = extract32(insn, 0, 5);
10773 CryptoTwoOpEnvFn *genfn;
10774 int feature;
10775 TCGv_i32 tcg_rd_regno, tcg_rn_regno;
10777 if (size != 0) {
10778 unallocated_encoding(s);
10779 return;
10782 switch (opcode) {
10783 case 0: /* SHA1H */
10784 feature = ARM_FEATURE_V8_SHA1;
10785 genfn = gen_helper_crypto_sha1h;
10786 break;
10787 case 1: /* SHA1SU1 */
10788 feature = ARM_FEATURE_V8_SHA1;
10789 genfn = gen_helper_crypto_sha1su1;
10790 break;
10791 case 2: /* SHA256SU0 */
10792 feature = ARM_FEATURE_V8_SHA256;
10793 genfn = gen_helper_crypto_sha256su0;
10794 break;
10795 default:
10796 unallocated_encoding(s);
10797 return;
10800 if (!arm_dc_feature(s, feature)) {
10801 unallocated_encoding(s);
10802 return;
10805 tcg_rd_regno = tcg_const_i32(rd << 1);
10806 tcg_rn_regno = tcg_const_i32(rn << 1);
10808 genfn(cpu_env, tcg_rd_regno, tcg_rn_regno);
10810 tcg_temp_free_i32(tcg_rd_regno);
10811 tcg_temp_free_i32(tcg_rn_regno);
10814 /* C3.6 Data processing - SIMD, inc Crypto
10816 * As the decode gets a little complex we are using a table based
10817 * approach for this part of the decode.
10819 static const AArch64DecodeTable data_proc_simd[] = {
10820 /* pattern , mask , fn */
10821 { 0x0e200400, 0x9f200400, disas_simd_three_reg_same },
10822 { 0x0e200000, 0x9f200c00, disas_simd_three_reg_diff },
10823 { 0x0e200800, 0x9f3e0c00, disas_simd_two_reg_misc },
10824 { 0x0e300800, 0x9f3e0c00, disas_simd_across_lanes },
10825 { 0x0e000400, 0x9fe08400, disas_simd_copy },
10826 { 0x0f000000, 0x9f000400, disas_simd_indexed }, /* vector indexed */
10827 /* simd_mod_imm decode is a subset of simd_shift_imm, so must precede it */
10828 { 0x0f000400, 0x9ff80400, disas_simd_mod_imm },
10829 { 0x0f000400, 0x9f800400, disas_simd_shift_imm },
10830 { 0x0e000000, 0xbf208c00, disas_simd_tb },
10831 { 0x0e000800, 0xbf208c00, disas_simd_zip_trn },
10832 { 0x2e000000, 0xbf208400, disas_simd_ext },
10833 { 0x5e200400, 0xdf200400, disas_simd_scalar_three_reg_same },
10834 { 0x5e200000, 0xdf200c00, disas_simd_scalar_three_reg_diff },
10835 { 0x5e200800, 0xdf3e0c00, disas_simd_scalar_two_reg_misc },
10836 { 0x5e300800, 0xdf3e0c00, disas_simd_scalar_pairwise },
10837 { 0x5e000400, 0xdfe08400, disas_simd_scalar_copy },
10838 { 0x5f000000, 0xdf000400, disas_simd_indexed }, /* scalar indexed */
10839 { 0x5f000400, 0xdf800400, disas_simd_scalar_shift_imm },
10840 { 0x4e280800, 0xff3e0c00, disas_crypto_aes },
10841 { 0x5e000000, 0xff208c00, disas_crypto_three_reg_sha },
10842 { 0x5e280800, 0xff3e0c00, disas_crypto_two_reg_sha },
10843 { 0x00000000, 0x00000000, NULL }
10846 static void disas_data_proc_simd(DisasContext *s, uint32_t insn)
10848 /* Note that this is called with all non-FP cases from
10849 * table C3-6 so it must UNDEF for entries not specifically
10850 * allocated to instructions in that table.
10852 AArch64DecodeFn *fn = lookup_disas_fn(&data_proc_simd[0], insn);
10853 if (fn) {
10854 fn(s, insn);
10855 } else {
10856 unallocated_encoding(s);
10860 /* C3.6 Data processing - SIMD and floating point */
10861 static void disas_data_proc_simd_fp(DisasContext *s, uint32_t insn)
10863 if (extract32(insn, 28, 1) == 1 && extract32(insn, 30, 1) == 0) {
10864 disas_data_proc_fp(s, insn);
10865 } else {
10866 /* SIMD, including crypto */
10867 disas_data_proc_simd(s, insn);
10871 /* C3.1 A64 instruction index by encoding */
10872 static void disas_a64_insn(CPUARMState *env, DisasContext *s)
10874 uint32_t insn;
10876 insn = arm_ldl_code(env, s->pc, s->bswap_code);
10877 s->insn = insn;
10878 s->pc += 4;
10880 s->fp_access_checked = false;
10882 switch (extract32(insn, 25, 4)) {
10883 case 0x0: case 0x1: case 0x2: case 0x3: /* UNALLOCATED */
10884 unallocated_encoding(s);
10885 break;
10886 case 0x8: case 0x9: /* Data processing - immediate */
10887 disas_data_proc_imm(s, insn);
10888 break;
10889 case 0xa: case 0xb: /* Branch, exception generation and system insns */
10890 disas_b_exc_sys(s, insn);
10891 break;
10892 case 0x4:
10893 case 0x6:
10894 case 0xc:
10895 case 0xe: /* Loads and stores */
10896 disas_ldst(s, insn);
10897 break;
10898 case 0x5:
10899 case 0xd: /* Data processing - register */
10900 disas_data_proc_reg(s, insn);
10901 break;
10902 case 0x7:
10903 case 0xf: /* Data processing - SIMD and floating point */
10904 disas_data_proc_simd_fp(s, insn);
10905 break;
10906 default:
10907 assert(FALSE); /* all 15 cases should be handled above */
10908 break;
10911 /* if we allocated any temporaries, free them here */
10912 free_tmp_a64(s);
10915 void gen_intermediate_code_internal_a64(ARMCPU *cpu,
10916 TranslationBlock *tb,
10917 bool search_pc)
10919 CPUState *cs = CPU(cpu);
10920 CPUARMState *env = &cpu->env;
10921 DisasContext dc1, *dc = &dc1;
10922 CPUBreakpoint *bp;
10923 uint16_t *gen_opc_end;
10924 int j, lj;
10925 target_ulong pc_start;
10926 target_ulong next_page_start;
10927 int num_insns;
10928 int max_insns;
10930 pc_start = tb->pc;
10932 dc->tb = tb;
10934 gen_opc_end = tcg_ctx.gen_opc_buf + OPC_MAX_SIZE;
10936 dc->is_jmp = DISAS_NEXT;
10937 dc->pc = pc_start;
10938 dc->singlestep_enabled = cs->singlestep_enabled;
10939 dc->condjmp = 0;
10941 dc->aarch64 = 1;
10942 dc->thumb = 0;
10943 dc->bswap_code = 0;
10944 dc->condexec_mask = 0;
10945 dc->condexec_cond = 0;
10946 dc->mmu_idx = ARM_TBFLAG_MMUIDX(tb->flags);
10947 dc->current_el = arm_mmu_idx_to_el(dc->mmu_idx);
10948 #if !defined(CONFIG_USER_ONLY)
10949 dc->user = (dc->current_el == 0);
10950 #endif
10951 dc->cpacr_fpen = ARM_TBFLAG_AA64_FPEN(tb->flags);
10952 dc->vec_len = 0;
10953 dc->vec_stride = 0;
10954 dc->cp_regs = cpu->cp_regs;
10955 dc->features = env->features;
10957 /* Single step state. The code-generation logic here is:
10958 * SS_ACTIVE == 0:
10959 * generate code with no special handling for single-stepping (except
10960 * that anything that can make us go to SS_ACTIVE == 1 must end the TB;
10961 * this happens anyway because those changes are all system register or
10962 * PSTATE writes).
10963 * SS_ACTIVE == 1, PSTATE.SS == 1: (active-not-pending)
10964 * emit code for one insn
10965 * emit code to clear PSTATE.SS
10966 * emit code to generate software step exception for completed step
10967 * end TB (as usual for having generated an exception)
10968 * SS_ACTIVE == 1, PSTATE.SS == 0: (active-pending)
10969 * emit code to generate a software step exception
10970 * end the TB
10972 dc->ss_active = ARM_TBFLAG_AA64_SS_ACTIVE(tb->flags);
10973 dc->pstate_ss = ARM_TBFLAG_AA64_PSTATE_SS(tb->flags);
10974 dc->is_ldex = false;
10975 dc->ss_same_el = (arm_debug_target_el(env) == dc->current_el);
10977 init_tmp_a64_array(dc);
10979 next_page_start = (pc_start & TARGET_PAGE_MASK) + TARGET_PAGE_SIZE;
10980 lj = -1;
10981 num_insns = 0;
10982 max_insns = tb->cflags & CF_COUNT_MASK;
10983 if (max_insns == 0) {
10984 max_insns = CF_COUNT_MASK;
10987 gen_tb_start(tb);
10989 tcg_clear_temp_count();
10991 do {
10992 if (unlikely(!QTAILQ_EMPTY(&cs->breakpoints))) {
10993 QTAILQ_FOREACH(bp, &cs->breakpoints, entry) {
10994 if (bp->pc == dc->pc) {
10995 gen_exception_internal_insn(dc, 0, EXCP_DEBUG);
10996 /* Advance PC so that clearing the breakpoint will
10997 invalidate this TB. */
10998 dc->pc += 2;
10999 goto done_generating;
11004 if (search_pc) {
11005 j = tcg_ctx.gen_opc_ptr - tcg_ctx.gen_opc_buf;
11006 if (lj < j) {
11007 lj++;
11008 while (lj < j) {
11009 tcg_ctx.gen_opc_instr_start[lj++] = 0;
11012 tcg_ctx.gen_opc_pc[lj] = dc->pc;
11013 tcg_ctx.gen_opc_instr_start[lj] = 1;
11014 tcg_ctx.gen_opc_icount[lj] = num_insns;
11017 if (num_insns + 1 == max_insns && (tb->cflags & CF_LAST_IO)) {
11018 gen_io_start();
11021 if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP | CPU_LOG_TB_OP_OPT))) {
11022 tcg_gen_debug_insn_start(dc->pc);
11025 if (dc->ss_active && !dc->pstate_ss) {
11026 /* Singlestep state is Active-pending.
11027 * If we're in this state at the start of a TB then either
11028 * a) we just took an exception to an EL which is being debugged
11029 * and this is the first insn in the exception handler
11030 * b) debug exceptions were masked and we just unmasked them
11031 * without changing EL (eg by clearing PSTATE.D)
11032 * In either case we're going to take a swstep exception in the
11033 * "did not step an insn" case, and so the syndrome ISV and EX
11034 * bits should be zero.
11036 assert(num_insns == 0);
11037 gen_exception(EXCP_UDEF, syn_swstep(dc->ss_same_el, 0, 0));
11038 dc->is_jmp = DISAS_EXC;
11039 break;
11042 disas_a64_insn(env, dc);
11044 if (tcg_check_temp_count()) {
11045 fprintf(stderr, "TCG temporary leak before "TARGET_FMT_lx"\n",
11046 dc->pc);
11049 /* Translation stops when a conditional branch is encountered.
11050 * Otherwise the subsequent code could get translated several times.
11051 * Also stop translation when a page boundary is reached. This
11052 * ensures prefetch aborts occur at the right place.
11054 num_insns++;
11055 } while (!dc->is_jmp && tcg_ctx.gen_opc_ptr < gen_opc_end &&
11056 !cs->singlestep_enabled &&
11057 !singlestep &&
11058 !dc->ss_active &&
11059 dc->pc < next_page_start &&
11060 num_insns < max_insns);
11062 if (tb->cflags & CF_LAST_IO) {
11063 gen_io_end();
11066 if (unlikely(cs->singlestep_enabled || dc->ss_active)
11067 && dc->is_jmp != DISAS_EXC) {
11068 /* Note that this means single stepping WFI doesn't halt the CPU.
11069 * For conditional branch insns this is harmless unreachable code as
11070 * gen_goto_tb() has already handled emitting the debug exception
11071 * (and thus a tb-jump is not possible when singlestepping).
11073 assert(dc->is_jmp != DISAS_TB_JUMP);
11074 if (dc->is_jmp != DISAS_JUMP) {
11075 gen_a64_set_pc_im(dc->pc);
11077 if (cs->singlestep_enabled) {
11078 gen_exception_internal(EXCP_DEBUG);
11079 } else {
11080 gen_step_complete_exception(dc);
11082 } else {
11083 switch (dc->is_jmp) {
11084 case DISAS_NEXT:
11085 gen_goto_tb(dc, 1, dc->pc);
11086 break;
11087 default:
11088 case DISAS_UPDATE:
11089 gen_a64_set_pc_im(dc->pc);
11090 /* fall through */
11091 case DISAS_JUMP:
11092 /* indicate that the hash table must be used to find the next TB */
11093 tcg_gen_exit_tb(0);
11094 break;
11095 case DISAS_TB_JUMP:
11096 case DISAS_EXC:
11097 case DISAS_SWI:
11098 break;
11099 case DISAS_WFE:
11100 gen_a64_set_pc_im(dc->pc);
11101 gen_helper_wfe(cpu_env);
11102 break;
11103 case DISAS_WFI:
11104 /* This is a special case because we don't want to just halt the CPU
11105 * if trying to debug across a WFI.
11107 gen_a64_set_pc_im(dc->pc);
11108 gen_helper_wfi(cpu_env);
11109 break;
11113 done_generating:
11114 gen_tb_end(tb, num_insns);
11115 *tcg_ctx.gen_opc_ptr = INDEX_op_end;
11117 #ifdef DEBUG_DISAS
11118 if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)) {
11119 qemu_log("----------------\n");
11120 qemu_log("IN: %s\n", lookup_symbol(pc_start));
11121 log_target_disas(env, pc_start, dc->pc - pc_start,
11122 4 | (dc->bswap_code << 1));
11123 qemu_log("\n");
11125 #endif
11126 if (search_pc) {
11127 j = tcg_ctx.gen_opc_ptr - tcg_ctx.gen_opc_buf;
11128 lj++;
11129 while (lj <= j) {
11130 tcg_ctx.gen_opc_instr_start[lj++] = 0;
11132 } else {
11133 tb->size = dc->pc - pc_start;
11134 tb->icount = num_insns;