megasas: Implement DCMD_CLUSTER_RESET_LD
[qemu/qmp-unstable.git] / target-arm / translate-a64.c
blob3a3c48acf4ffcbabec56d35397694f79d3aaa10e
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 void aarch64_cpu_dump_state(CPUState *cs, FILE *f,
127 fprintf_function cpu_fprintf, int flags)
129 ARMCPU *cpu = ARM_CPU(cs);
130 CPUARMState *env = &cpu->env;
131 uint32_t psr = pstate_read(env);
132 int i;
134 cpu_fprintf(f, "PC=%016"PRIx64" SP=%016"PRIx64"\n",
135 env->pc, env->xregs[31]);
136 for (i = 0; i < 31; i++) {
137 cpu_fprintf(f, "X%02d=%016"PRIx64, i, env->xregs[i]);
138 if ((i % 4) == 3) {
139 cpu_fprintf(f, "\n");
140 } else {
141 cpu_fprintf(f, " ");
144 cpu_fprintf(f, "PSTATE=%08x (flags %c%c%c%c)\n",
145 psr,
146 psr & PSTATE_N ? 'N' : '-',
147 psr & PSTATE_Z ? 'Z' : '-',
148 psr & PSTATE_C ? 'C' : '-',
149 psr & PSTATE_V ? 'V' : '-');
150 cpu_fprintf(f, "\n");
152 if (flags & CPU_DUMP_FPU) {
153 int numvfpregs = 32;
154 for (i = 0; i < numvfpregs; i += 2) {
155 uint64_t vlo = float64_val(env->vfp.regs[i * 2]);
156 uint64_t vhi = float64_val(env->vfp.regs[(i * 2) + 1]);
157 cpu_fprintf(f, "q%02d=%016" PRIx64 ":%016" PRIx64 " ",
158 i, vhi, vlo);
159 vlo = float64_val(env->vfp.regs[(i + 1) * 2]);
160 vhi = float64_val(env->vfp.regs[((i + 1) * 2) + 1]);
161 cpu_fprintf(f, "q%02d=%016" PRIx64 ":%016" PRIx64 "\n",
162 i + 1, vhi, vlo);
164 cpu_fprintf(f, "FPCR: %08x FPSR: %08x\n",
165 vfp_get_fpcr(env), vfp_get_fpsr(env));
169 void gen_a64_set_pc_im(uint64_t val)
171 tcg_gen_movi_i64(cpu_pc, val);
174 static void gen_exception_internal(int excp)
176 TCGv_i32 tcg_excp = tcg_const_i32(excp);
178 assert(excp_is_internal(excp));
179 gen_helper_exception_internal(cpu_env, tcg_excp);
180 tcg_temp_free_i32(tcg_excp);
183 static void gen_exception(int excp, uint32_t syndrome)
185 TCGv_i32 tcg_excp = tcg_const_i32(excp);
186 TCGv_i32 tcg_syn = tcg_const_i32(syndrome);
188 gen_helper_exception_with_syndrome(cpu_env, tcg_excp, tcg_syn);
189 tcg_temp_free_i32(tcg_syn);
190 tcg_temp_free_i32(tcg_excp);
193 static void gen_exception_internal_insn(DisasContext *s, int offset, int excp)
195 gen_a64_set_pc_im(s->pc - offset);
196 gen_exception_internal(excp);
197 s->is_jmp = DISAS_EXC;
200 static void gen_exception_insn(DisasContext *s, int offset, int excp,
201 uint32_t syndrome)
203 gen_a64_set_pc_im(s->pc - offset);
204 gen_exception(excp, syndrome);
205 s->is_jmp = DISAS_EXC;
208 static void gen_ss_advance(DisasContext *s)
210 /* If the singlestep state is Active-not-pending, advance to
211 * Active-pending.
213 if (s->ss_active) {
214 s->pstate_ss = 0;
215 gen_helper_clear_pstate_ss(cpu_env);
219 static void gen_step_complete_exception(DisasContext *s)
221 /* We just completed step of an insn. Move from Active-not-pending
222 * to Active-pending, and then also take the swstep exception.
223 * This corresponds to making the (IMPDEF) choice to prioritize
224 * swstep exceptions over asynchronous exceptions taken to an exception
225 * level where debug is disabled. This choice has the advantage that
226 * we do not need to maintain internal state corresponding to the
227 * ISV/EX syndrome bits between completion of the step and generation
228 * of the exception, and our syndrome information is always correct.
230 gen_ss_advance(s);
231 gen_exception(EXCP_UDEF, syn_swstep(s->ss_same_el, 1, s->is_ldex));
232 s->is_jmp = DISAS_EXC;
235 static inline bool use_goto_tb(DisasContext *s, int n, uint64_t dest)
237 /* No direct tb linking with singlestep (either QEMU's or the ARM
238 * debug architecture kind) or deterministic io
240 if (s->singlestep_enabled || s->ss_active || (s->tb->cflags & CF_LAST_IO)) {
241 return false;
244 /* Only link tbs from inside the same guest page */
245 if ((s->tb->pc & TARGET_PAGE_MASK) != (dest & TARGET_PAGE_MASK)) {
246 return false;
249 return true;
252 static inline void gen_goto_tb(DisasContext *s, int n, uint64_t dest)
254 TranslationBlock *tb;
256 tb = s->tb;
257 if (use_goto_tb(s, n, dest)) {
258 tcg_gen_goto_tb(n);
259 gen_a64_set_pc_im(dest);
260 tcg_gen_exit_tb((intptr_t)tb + n);
261 s->is_jmp = DISAS_TB_JUMP;
262 } else {
263 gen_a64_set_pc_im(dest);
264 if (s->ss_active) {
265 gen_step_complete_exception(s);
266 } else if (s->singlestep_enabled) {
267 gen_exception_internal(EXCP_DEBUG);
268 } else {
269 tcg_gen_exit_tb(0);
270 s->is_jmp = DISAS_TB_JUMP;
275 static void unallocated_encoding(DisasContext *s)
277 /* Unallocated and reserved encodings are uncategorized */
278 gen_exception_insn(s, 4, EXCP_UDEF, syn_uncategorized());
281 #define unsupported_encoding(s, insn) \
282 do { \
283 qemu_log_mask(LOG_UNIMP, \
284 "%s:%d: unsupported instruction encoding 0x%08x " \
285 "at pc=%016" PRIx64 "\n", \
286 __FILE__, __LINE__, insn, s->pc - 4); \
287 unallocated_encoding(s); \
288 } while (0);
290 static void init_tmp_a64_array(DisasContext *s)
292 #ifdef CONFIG_DEBUG_TCG
293 int i;
294 for (i = 0; i < ARRAY_SIZE(s->tmp_a64); i++) {
295 TCGV_UNUSED_I64(s->tmp_a64[i]);
297 #endif
298 s->tmp_a64_count = 0;
301 static void free_tmp_a64(DisasContext *s)
303 int i;
304 for (i = 0; i < s->tmp_a64_count; i++) {
305 tcg_temp_free_i64(s->tmp_a64[i]);
307 init_tmp_a64_array(s);
310 static TCGv_i64 new_tmp_a64(DisasContext *s)
312 assert(s->tmp_a64_count < TMP_A64_MAX);
313 return s->tmp_a64[s->tmp_a64_count++] = tcg_temp_new_i64();
316 static TCGv_i64 new_tmp_a64_zero(DisasContext *s)
318 TCGv_i64 t = new_tmp_a64(s);
319 tcg_gen_movi_i64(t, 0);
320 return t;
324 * Register access functions
326 * These functions are used for directly accessing a register in where
327 * changes to the final register value are likely to be made. If you
328 * need to use a register for temporary calculation (e.g. index type
329 * operations) use the read_* form.
331 * B1.2.1 Register mappings
333 * In instruction register encoding 31 can refer to ZR (zero register) or
334 * the SP (stack pointer) depending on context. In QEMU's case we map SP
335 * to cpu_X[31] and ZR accesses to a temporary which can be discarded.
336 * This is the point of the _sp forms.
338 static TCGv_i64 cpu_reg(DisasContext *s, int reg)
340 if (reg == 31) {
341 return new_tmp_a64_zero(s);
342 } else {
343 return cpu_X[reg];
347 /* register access for when 31 == SP */
348 static TCGv_i64 cpu_reg_sp(DisasContext *s, int reg)
350 return cpu_X[reg];
353 /* read a cpu register in 32bit/64bit mode. Returns a TCGv_i64
354 * representing the register contents. This TCGv is an auto-freed
355 * temporary so it need not be explicitly freed, and may be modified.
357 static TCGv_i64 read_cpu_reg(DisasContext *s, int reg, int sf)
359 TCGv_i64 v = new_tmp_a64(s);
360 if (reg != 31) {
361 if (sf) {
362 tcg_gen_mov_i64(v, cpu_X[reg]);
363 } else {
364 tcg_gen_ext32u_i64(v, cpu_X[reg]);
366 } else {
367 tcg_gen_movi_i64(v, 0);
369 return v;
372 static TCGv_i64 read_cpu_reg_sp(DisasContext *s, int reg, int sf)
374 TCGv_i64 v = new_tmp_a64(s);
375 if (sf) {
376 tcg_gen_mov_i64(v, cpu_X[reg]);
377 } else {
378 tcg_gen_ext32u_i64(v, cpu_X[reg]);
380 return v;
383 /* We should have at some point before trying to access an FP register
384 * done the necessary access check, so assert that
385 * (a) we did the check and
386 * (b) we didn't then just plough ahead anyway if it failed.
387 * Print the instruction pattern in the abort message so we can figure
388 * out what we need to fix if a user encounters this problem in the wild.
390 static inline void assert_fp_access_checked(DisasContext *s)
392 #ifdef CONFIG_DEBUG_TCG
393 if (unlikely(!s->fp_access_checked || !s->cpacr_fpen)) {
394 fprintf(stderr, "target-arm: FP access check missing for "
395 "instruction 0x%08x\n", s->insn);
396 abort();
398 #endif
401 /* Return the offset into CPUARMState of an element of specified
402 * size, 'element' places in from the least significant end of
403 * the FP/vector register Qn.
405 static inline int vec_reg_offset(DisasContext *s, int regno,
406 int element, TCGMemOp size)
408 int offs = offsetof(CPUARMState, vfp.regs[regno * 2]);
409 #ifdef HOST_WORDS_BIGENDIAN
410 /* This is complicated slightly because vfp.regs[2n] is
411 * still the low half and vfp.regs[2n+1] the high half
412 * of the 128 bit vector, even on big endian systems.
413 * Calculate the offset assuming a fully bigendian 128 bits,
414 * then XOR to account for the order of the two 64 bit halves.
416 offs += (16 - ((element + 1) * (1 << size)));
417 offs ^= 8;
418 #else
419 offs += element * (1 << size);
420 #endif
421 assert_fp_access_checked(s);
422 return offs;
425 /* Return the offset into CPUARMState of a slice (from
426 * the least significant end) of FP register Qn (ie
427 * Dn, Sn, Hn or Bn).
428 * (Note that this is not the same mapping as for A32; see cpu.h)
430 static inline int fp_reg_offset(DisasContext *s, int regno, TCGMemOp size)
432 int offs = offsetof(CPUARMState, vfp.regs[regno * 2]);
433 #ifdef HOST_WORDS_BIGENDIAN
434 offs += (8 - (1 << size));
435 #endif
436 assert_fp_access_checked(s);
437 return offs;
440 /* Offset of the high half of the 128 bit vector Qn */
441 static inline int fp_reg_hi_offset(DisasContext *s, int regno)
443 assert_fp_access_checked(s);
444 return offsetof(CPUARMState, vfp.regs[regno * 2 + 1]);
447 /* Convenience accessors for reading and writing single and double
448 * FP registers. Writing clears the upper parts of the associated
449 * 128 bit vector register, as required by the architecture.
450 * Note that unlike the GP register accessors, the values returned
451 * by the read functions must be manually freed.
453 static TCGv_i64 read_fp_dreg(DisasContext *s, int reg)
455 TCGv_i64 v = tcg_temp_new_i64();
457 tcg_gen_ld_i64(v, cpu_env, fp_reg_offset(s, reg, MO_64));
458 return v;
461 static TCGv_i32 read_fp_sreg(DisasContext *s, int reg)
463 TCGv_i32 v = tcg_temp_new_i32();
465 tcg_gen_ld_i32(v, cpu_env, fp_reg_offset(s, reg, MO_32));
466 return v;
469 static void write_fp_dreg(DisasContext *s, int reg, TCGv_i64 v)
471 TCGv_i64 tcg_zero = tcg_const_i64(0);
473 tcg_gen_st_i64(v, cpu_env, fp_reg_offset(s, reg, MO_64));
474 tcg_gen_st_i64(tcg_zero, cpu_env, fp_reg_hi_offset(s, reg));
475 tcg_temp_free_i64(tcg_zero);
478 static void write_fp_sreg(DisasContext *s, int reg, TCGv_i32 v)
480 TCGv_i64 tmp = tcg_temp_new_i64();
482 tcg_gen_extu_i32_i64(tmp, v);
483 write_fp_dreg(s, reg, tmp);
484 tcg_temp_free_i64(tmp);
487 static TCGv_ptr get_fpstatus_ptr(void)
489 TCGv_ptr statusptr = tcg_temp_new_ptr();
490 int offset;
492 /* In A64 all instructions (both FP and Neon) use the FPCR;
493 * there is no equivalent of the A32 Neon "standard FPSCR value"
494 * and all operations use vfp.fp_status.
496 offset = offsetof(CPUARMState, vfp.fp_status);
497 tcg_gen_addi_ptr(statusptr, cpu_env, offset);
498 return statusptr;
501 /* Set ZF and NF based on a 64 bit result. This is alas fiddlier
502 * than the 32 bit equivalent.
504 static inline void gen_set_NZ64(TCGv_i64 result)
506 TCGv_i64 flag = tcg_temp_new_i64();
508 tcg_gen_setcondi_i64(TCG_COND_NE, flag, result, 0);
509 tcg_gen_trunc_i64_i32(cpu_ZF, flag);
510 tcg_gen_shri_i64(flag, result, 32);
511 tcg_gen_trunc_i64_i32(cpu_NF, flag);
512 tcg_temp_free_i64(flag);
515 /* Set NZCV as for a logical operation: NZ as per result, CV cleared. */
516 static inline void gen_logic_CC(int sf, TCGv_i64 result)
518 if (sf) {
519 gen_set_NZ64(result);
520 } else {
521 tcg_gen_trunc_i64_i32(cpu_ZF, result);
522 tcg_gen_trunc_i64_i32(cpu_NF, result);
524 tcg_gen_movi_i32(cpu_CF, 0);
525 tcg_gen_movi_i32(cpu_VF, 0);
528 /* dest = T0 + T1; compute C, N, V and Z flags */
529 static void gen_add_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
531 if (sf) {
532 TCGv_i64 result, flag, tmp;
533 result = tcg_temp_new_i64();
534 flag = tcg_temp_new_i64();
535 tmp = tcg_temp_new_i64();
537 tcg_gen_movi_i64(tmp, 0);
538 tcg_gen_add2_i64(result, flag, t0, tmp, t1, tmp);
540 tcg_gen_trunc_i64_i32(cpu_CF, flag);
542 gen_set_NZ64(result);
544 tcg_gen_xor_i64(flag, result, t0);
545 tcg_gen_xor_i64(tmp, t0, t1);
546 tcg_gen_andc_i64(flag, flag, tmp);
547 tcg_temp_free_i64(tmp);
548 tcg_gen_shri_i64(flag, flag, 32);
549 tcg_gen_trunc_i64_i32(cpu_VF, flag);
551 tcg_gen_mov_i64(dest, result);
552 tcg_temp_free_i64(result);
553 tcg_temp_free_i64(flag);
554 } else {
555 /* 32 bit arithmetic */
556 TCGv_i32 t0_32 = tcg_temp_new_i32();
557 TCGv_i32 t1_32 = tcg_temp_new_i32();
558 TCGv_i32 tmp = tcg_temp_new_i32();
560 tcg_gen_movi_i32(tmp, 0);
561 tcg_gen_trunc_i64_i32(t0_32, t0);
562 tcg_gen_trunc_i64_i32(t1_32, t1);
563 tcg_gen_add2_i32(cpu_NF, cpu_CF, t0_32, tmp, t1_32, tmp);
564 tcg_gen_mov_i32(cpu_ZF, cpu_NF);
565 tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
566 tcg_gen_xor_i32(tmp, t0_32, t1_32);
567 tcg_gen_andc_i32(cpu_VF, cpu_VF, tmp);
568 tcg_gen_extu_i32_i64(dest, cpu_NF);
570 tcg_temp_free_i32(tmp);
571 tcg_temp_free_i32(t0_32);
572 tcg_temp_free_i32(t1_32);
576 /* dest = T0 - T1; compute C, N, V and Z flags */
577 static void gen_sub_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
579 if (sf) {
580 /* 64 bit arithmetic */
581 TCGv_i64 result, flag, tmp;
583 result = tcg_temp_new_i64();
584 flag = tcg_temp_new_i64();
585 tcg_gen_sub_i64(result, t0, t1);
587 gen_set_NZ64(result);
589 tcg_gen_setcond_i64(TCG_COND_GEU, flag, t0, t1);
590 tcg_gen_trunc_i64_i32(cpu_CF, flag);
592 tcg_gen_xor_i64(flag, result, t0);
593 tmp = tcg_temp_new_i64();
594 tcg_gen_xor_i64(tmp, t0, t1);
595 tcg_gen_and_i64(flag, flag, tmp);
596 tcg_temp_free_i64(tmp);
597 tcg_gen_shri_i64(flag, flag, 32);
598 tcg_gen_trunc_i64_i32(cpu_VF, flag);
599 tcg_gen_mov_i64(dest, result);
600 tcg_temp_free_i64(flag);
601 tcg_temp_free_i64(result);
602 } else {
603 /* 32 bit arithmetic */
604 TCGv_i32 t0_32 = tcg_temp_new_i32();
605 TCGv_i32 t1_32 = tcg_temp_new_i32();
606 TCGv_i32 tmp;
608 tcg_gen_trunc_i64_i32(t0_32, t0);
609 tcg_gen_trunc_i64_i32(t1_32, t1);
610 tcg_gen_sub_i32(cpu_NF, t0_32, t1_32);
611 tcg_gen_mov_i32(cpu_ZF, cpu_NF);
612 tcg_gen_setcond_i32(TCG_COND_GEU, cpu_CF, t0_32, t1_32);
613 tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
614 tmp = tcg_temp_new_i32();
615 tcg_gen_xor_i32(tmp, t0_32, t1_32);
616 tcg_temp_free_i32(t0_32);
617 tcg_temp_free_i32(t1_32);
618 tcg_gen_and_i32(cpu_VF, cpu_VF, tmp);
619 tcg_temp_free_i32(tmp);
620 tcg_gen_extu_i32_i64(dest, cpu_NF);
624 /* dest = T0 + T1 + CF; do not compute flags. */
625 static void gen_adc(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
627 TCGv_i64 flag = tcg_temp_new_i64();
628 tcg_gen_extu_i32_i64(flag, cpu_CF);
629 tcg_gen_add_i64(dest, t0, t1);
630 tcg_gen_add_i64(dest, dest, flag);
631 tcg_temp_free_i64(flag);
633 if (!sf) {
634 tcg_gen_ext32u_i64(dest, dest);
638 /* dest = T0 + T1 + CF; compute C, N, V and Z flags. */
639 static void gen_adc_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
641 if (sf) {
642 TCGv_i64 result, cf_64, vf_64, tmp;
643 result = tcg_temp_new_i64();
644 cf_64 = tcg_temp_new_i64();
645 vf_64 = tcg_temp_new_i64();
646 tmp = tcg_const_i64(0);
648 tcg_gen_extu_i32_i64(cf_64, cpu_CF);
649 tcg_gen_add2_i64(result, cf_64, t0, tmp, cf_64, tmp);
650 tcg_gen_add2_i64(result, cf_64, result, cf_64, t1, tmp);
651 tcg_gen_trunc_i64_i32(cpu_CF, cf_64);
652 gen_set_NZ64(result);
654 tcg_gen_xor_i64(vf_64, result, t0);
655 tcg_gen_xor_i64(tmp, t0, t1);
656 tcg_gen_andc_i64(vf_64, vf_64, tmp);
657 tcg_gen_shri_i64(vf_64, vf_64, 32);
658 tcg_gen_trunc_i64_i32(cpu_VF, vf_64);
660 tcg_gen_mov_i64(dest, result);
662 tcg_temp_free_i64(tmp);
663 tcg_temp_free_i64(vf_64);
664 tcg_temp_free_i64(cf_64);
665 tcg_temp_free_i64(result);
666 } else {
667 TCGv_i32 t0_32, t1_32, tmp;
668 t0_32 = tcg_temp_new_i32();
669 t1_32 = tcg_temp_new_i32();
670 tmp = tcg_const_i32(0);
672 tcg_gen_trunc_i64_i32(t0_32, t0);
673 tcg_gen_trunc_i64_i32(t1_32, t1);
674 tcg_gen_add2_i32(cpu_NF, cpu_CF, t0_32, tmp, cpu_CF, tmp);
675 tcg_gen_add2_i32(cpu_NF, cpu_CF, cpu_NF, cpu_CF, t1_32, tmp);
677 tcg_gen_mov_i32(cpu_ZF, cpu_NF);
678 tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
679 tcg_gen_xor_i32(tmp, t0_32, t1_32);
680 tcg_gen_andc_i32(cpu_VF, cpu_VF, tmp);
681 tcg_gen_extu_i32_i64(dest, cpu_NF);
683 tcg_temp_free_i32(tmp);
684 tcg_temp_free_i32(t1_32);
685 tcg_temp_free_i32(t0_32);
690 * Load/Store generators
694 * Store from GPR register to memory.
696 static void do_gpr_st_memidx(DisasContext *s, TCGv_i64 source,
697 TCGv_i64 tcg_addr, int size, int memidx)
699 g_assert(size <= 3);
700 tcg_gen_qemu_st_i64(source, tcg_addr, memidx, MO_TE + size);
703 static void do_gpr_st(DisasContext *s, TCGv_i64 source,
704 TCGv_i64 tcg_addr, int size)
706 do_gpr_st_memidx(s, source, tcg_addr, size, get_mem_index(s));
710 * Load from memory to GPR register
712 static void do_gpr_ld_memidx(DisasContext *s, TCGv_i64 dest, TCGv_i64 tcg_addr,
713 int size, bool is_signed, bool extend, int memidx)
715 TCGMemOp memop = MO_TE + size;
717 g_assert(size <= 3);
719 if (is_signed) {
720 memop += MO_SIGN;
723 tcg_gen_qemu_ld_i64(dest, tcg_addr, memidx, memop);
725 if (extend && is_signed) {
726 g_assert(size < 3);
727 tcg_gen_ext32u_i64(dest, dest);
731 static void do_gpr_ld(DisasContext *s, TCGv_i64 dest, TCGv_i64 tcg_addr,
732 int size, bool is_signed, bool extend)
734 do_gpr_ld_memidx(s, dest, tcg_addr, size, is_signed, extend,
735 get_mem_index(s));
739 * Store from FP register to memory
741 static void do_fp_st(DisasContext *s, int srcidx, TCGv_i64 tcg_addr, int size)
743 /* This writes the bottom N bits of a 128 bit wide vector to memory */
744 TCGv_i64 tmp = tcg_temp_new_i64();
745 tcg_gen_ld_i64(tmp, cpu_env, fp_reg_offset(s, srcidx, MO_64));
746 if (size < 4) {
747 tcg_gen_qemu_st_i64(tmp, tcg_addr, get_mem_index(s), MO_TE + size);
748 } else {
749 TCGv_i64 tcg_hiaddr = tcg_temp_new_i64();
750 tcg_gen_qemu_st_i64(tmp, tcg_addr, get_mem_index(s), MO_TEQ);
751 tcg_gen_qemu_st64(tmp, tcg_addr, get_mem_index(s));
752 tcg_gen_ld_i64(tmp, cpu_env, fp_reg_hi_offset(s, srcidx));
753 tcg_gen_addi_i64(tcg_hiaddr, tcg_addr, 8);
754 tcg_gen_qemu_st_i64(tmp, tcg_hiaddr, get_mem_index(s), MO_TEQ);
755 tcg_temp_free_i64(tcg_hiaddr);
758 tcg_temp_free_i64(tmp);
762 * Load from memory to FP register
764 static void do_fp_ld(DisasContext *s, int destidx, TCGv_i64 tcg_addr, int size)
766 /* This always zero-extends and writes to a full 128 bit wide vector */
767 TCGv_i64 tmplo = tcg_temp_new_i64();
768 TCGv_i64 tmphi;
770 if (size < 4) {
771 TCGMemOp memop = MO_TE + size;
772 tmphi = tcg_const_i64(0);
773 tcg_gen_qemu_ld_i64(tmplo, tcg_addr, get_mem_index(s), memop);
774 } else {
775 TCGv_i64 tcg_hiaddr;
776 tmphi = tcg_temp_new_i64();
777 tcg_hiaddr = tcg_temp_new_i64();
779 tcg_gen_qemu_ld_i64(tmplo, tcg_addr, get_mem_index(s), MO_TEQ);
780 tcg_gen_addi_i64(tcg_hiaddr, tcg_addr, 8);
781 tcg_gen_qemu_ld_i64(tmphi, tcg_hiaddr, get_mem_index(s), MO_TEQ);
782 tcg_temp_free_i64(tcg_hiaddr);
785 tcg_gen_st_i64(tmplo, cpu_env, fp_reg_offset(s, destidx, MO_64));
786 tcg_gen_st_i64(tmphi, cpu_env, fp_reg_hi_offset(s, destidx));
788 tcg_temp_free_i64(tmplo);
789 tcg_temp_free_i64(tmphi);
793 * Vector load/store helpers.
795 * The principal difference between this and a FP load is that we don't
796 * zero extend as we are filling a partial chunk of the vector register.
797 * These functions don't support 128 bit loads/stores, which would be
798 * normal load/store operations.
800 * The _i32 versions are useful when operating on 32 bit quantities
801 * (eg for floating point single or using Neon helper functions).
804 /* Get value of an element within a vector register */
805 static void read_vec_element(DisasContext *s, TCGv_i64 tcg_dest, int srcidx,
806 int element, TCGMemOp memop)
808 int vect_off = vec_reg_offset(s, srcidx, element, memop & MO_SIZE);
809 switch (memop) {
810 case MO_8:
811 tcg_gen_ld8u_i64(tcg_dest, cpu_env, vect_off);
812 break;
813 case MO_16:
814 tcg_gen_ld16u_i64(tcg_dest, cpu_env, vect_off);
815 break;
816 case MO_32:
817 tcg_gen_ld32u_i64(tcg_dest, cpu_env, vect_off);
818 break;
819 case MO_8|MO_SIGN:
820 tcg_gen_ld8s_i64(tcg_dest, cpu_env, vect_off);
821 break;
822 case MO_16|MO_SIGN:
823 tcg_gen_ld16s_i64(tcg_dest, cpu_env, vect_off);
824 break;
825 case MO_32|MO_SIGN:
826 tcg_gen_ld32s_i64(tcg_dest, cpu_env, vect_off);
827 break;
828 case MO_64:
829 case MO_64|MO_SIGN:
830 tcg_gen_ld_i64(tcg_dest, cpu_env, vect_off);
831 break;
832 default:
833 g_assert_not_reached();
837 static void read_vec_element_i32(DisasContext *s, TCGv_i32 tcg_dest, int srcidx,
838 int element, TCGMemOp memop)
840 int vect_off = vec_reg_offset(s, srcidx, element, memop & MO_SIZE);
841 switch (memop) {
842 case MO_8:
843 tcg_gen_ld8u_i32(tcg_dest, cpu_env, vect_off);
844 break;
845 case MO_16:
846 tcg_gen_ld16u_i32(tcg_dest, cpu_env, vect_off);
847 break;
848 case MO_8|MO_SIGN:
849 tcg_gen_ld8s_i32(tcg_dest, cpu_env, vect_off);
850 break;
851 case MO_16|MO_SIGN:
852 tcg_gen_ld16s_i32(tcg_dest, cpu_env, vect_off);
853 break;
854 case MO_32:
855 case MO_32|MO_SIGN:
856 tcg_gen_ld_i32(tcg_dest, cpu_env, vect_off);
857 break;
858 default:
859 g_assert_not_reached();
863 /* Set value of an element within a vector register */
864 static void write_vec_element(DisasContext *s, TCGv_i64 tcg_src, int destidx,
865 int element, TCGMemOp memop)
867 int vect_off = vec_reg_offset(s, destidx, element, memop & MO_SIZE);
868 switch (memop) {
869 case MO_8:
870 tcg_gen_st8_i64(tcg_src, cpu_env, vect_off);
871 break;
872 case MO_16:
873 tcg_gen_st16_i64(tcg_src, cpu_env, vect_off);
874 break;
875 case MO_32:
876 tcg_gen_st32_i64(tcg_src, cpu_env, vect_off);
877 break;
878 case MO_64:
879 tcg_gen_st_i64(tcg_src, cpu_env, vect_off);
880 break;
881 default:
882 g_assert_not_reached();
886 static void write_vec_element_i32(DisasContext *s, TCGv_i32 tcg_src,
887 int destidx, int element, TCGMemOp memop)
889 int vect_off = vec_reg_offset(s, destidx, element, memop & MO_SIZE);
890 switch (memop) {
891 case MO_8:
892 tcg_gen_st8_i32(tcg_src, cpu_env, vect_off);
893 break;
894 case MO_16:
895 tcg_gen_st16_i32(tcg_src, cpu_env, vect_off);
896 break;
897 case MO_32:
898 tcg_gen_st_i32(tcg_src, cpu_env, vect_off);
899 break;
900 default:
901 g_assert_not_reached();
905 /* Clear the high 64 bits of a 128 bit vector (in general non-quad
906 * vector ops all need to do this).
908 static void clear_vec_high(DisasContext *s, int rd)
910 TCGv_i64 tcg_zero = tcg_const_i64(0);
912 write_vec_element(s, tcg_zero, rd, 1, MO_64);
913 tcg_temp_free_i64(tcg_zero);
916 /* Store from vector register to memory */
917 static void do_vec_st(DisasContext *s, int srcidx, int element,
918 TCGv_i64 tcg_addr, int size)
920 TCGMemOp memop = MO_TE + size;
921 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
923 read_vec_element(s, tcg_tmp, srcidx, element, size);
924 tcg_gen_qemu_st_i64(tcg_tmp, tcg_addr, get_mem_index(s), memop);
926 tcg_temp_free_i64(tcg_tmp);
929 /* Load from memory to vector register */
930 static void do_vec_ld(DisasContext *s, int destidx, int element,
931 TCGv_i64 tcg_addr, int size)
933 TCGMemOp memop = MO_TE + size;
934 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
936 tcg_gen_qemu_ld_i64(tcg_tmp, tcg_addr, get_mem_index(s), memop);
937 write_vec_element(s, tcg_tmp, destidx, element, size);
939 tcg_temp_free_i64(tcg_tmp);
942 /* Check that FP/Neon access is enabled. If it is, return
943 * true. If not, emit code to generate an appropriate exception,
944 * and return false; the caller should not emit any code for
945 * the instruction. Note that this check must happen after all
946 * unallocated-encoding checks (otherwise the syndrome information
947 * for the resulting exception will be incorrect).
949 static inline bool fp_access_check(DisasContext *s)
951 assert(!s->fp_access_checked);
952 s->fp_access_checked = true;
954 if (s->cpacr_fpen) {
955 return true;
958 gen_exception_insn(s, 4, EXCP_UDEF, syn_fp_access_trap(1, 0xe, false));
959 return false;
963 * This utility function is for doing register extension with an
964 * optional shift. You will likely want to pass a temporary for the
965 * destination register. See DecodeRegExtend() in the ARM ARM.
967 static void ext_and_shift_reg(TCGv_i64 tcg_out, TCGv_i64 tcg_in,
968 int option, unsigned int shift)
970 int extsize = extract32(option, 0, 2);
971 bool is_signed = extract32(option, 2, 1);
973 if (is_signed) {
974 switch (extsize) {
975 case 0:
976 tcg_gen_ext8s_i64(tcg_out, tcg_in);
977 break;
978 case 1:
979 tcg_gen_ext16s_i64(tcg_out, tcg_in);
980 break;
981 case 2:
982 tcg_gen_ext32s_i64(tcg_out, tcg_in);
983 break;
984 case 3:
985 tcg_gen_mov_i64(tcg_out, tcg_in);
986 break;
988 } else {
989 switch (extsize) {
990 case 0:
991 tcg_gen_ext8u_i64(tcg_out, tcg_in);
992 break;
993 case 1:
994 tcg_gen_ext16u_i64(tcg_out, tcg_in);
995 break;
996 case 2:
997 tcg_gen_ext32u_i64(tcg_out, tcg_in);
998 break;
999 case 3:
1000 tcg_gen_mov_i64(tcg_out, tcg_in);
1001 break;
1005 if (shift) {
1006 tcg_gen_shli_i64(tcg_out, tcg_out, shift);
1010 static inline void gen_check_sp_alignment(DisasContext *s)
1012 /* The AArch64 architecture mandates that (if enabled via PSTATE
1013 * or SCTLR bits) there is a check that SP is 16-aligned on every
1014 * SP-relative load or store (with an exception generated if it is not).
1015 * In line with general QEMU practice regarding misaligned accesses,
1016 * we omit these checks for the sake of guest program performance.
1017 * This function is provided as a hook so we can more easily add these
1018 * checks in future (possibly as a "favour catching guest program bugs
1019 * over speed" user selectable option).
1024 * This provides a simple table based table lookup decoder. It is
1025 * intended to be used when the relevant bits for decode are too
1026 * awkwardly placed and switch/if based logic would be confusing and
1027 * deeply nested. Since it's a linear search through the table, tables
1028 * should be kept small.
1030 * It returns the first handler where insn & mask == pattern, or
1031 * NULL if there is no match.
1032 * The table is terminated by an empty mask (i.e. 0)
1034 static inline AArch64DecodeFn *lookup_disas_fn(const AArch64DecodeTable *table,
1035 uint32_t insn)
1037 const AArch64DecodeTable *tptr = table;
1039 while (tptr->mask) {
1040 if ((insn & tptr->mask) == tptr->pattern) {
1041 return tptr->disas_fn;
1043 tptr++;
1045 return NULL;
1049 * the instruction disassembly implemented here matches
1050 * the instruction encoding classifications in chapter 3 (C3)
1051 * of the ARM Architecture Reference Manual (DDI0487A_a)
1054 /* C3.2.7 Unconditional branch (immediate)
1055 * 31 30 26 25 0
1056 * +----+-----------+-------------------------------------+
1057 * | op | 0 0 1 0 1 | imm26 |
1058 * +----+-----------+-------------------------------------+
1060 static void disas_uncond_b_imm(DisasContext *s, uint32_t insn)
1062 uint64_t addr = s->pc + sextract32(insn, 0, 26) * 4 - 4;
1064 if (insn & (1 << 31)) {
1065 /* C5.6.26 BL Branch with link */
1066 tcg_gen_movi_i64(cpu_reg(s, 30), s->pc);
1069 /* C5.6.20 B Branch / C5.6.26 BL Branch with link */
1070 gen_goto_tb(s, 0, addr);
1073 /* C3.2.1 Compare & branch (immediate)
1074 * 31 30 25 24 23 5 4 0
1075 * +----+-------------+----+---------------------+--------+
1076 * | sf | 0 1 1 0 1 0 | op | imm19 | Rt |
1077 * +----+-------------+----+---------------------+--------+
1079 static void disas_comp_b_imm(DisasContext *s, uint32_t insn)
1081 unsigned int sf, op, rt;
1082 uint64_t addr;
1083 int label_match;
1084 TCGv_i64 tcg_cmp;
1086 sf = extract32(insn, 31, 1);
1087 op = extract32(insn, 24, 1); /* 0: CBZ; 1: CBNZ */
1088 rt = extract32(insn, 0, 5);
1089 addr = s->pc + sextract32(insn, 5, 19) * 4 - 4;
1091 tcg_cmp = read_cpu_reg(s, rt, sf);
1092 label_match = gen_new_label();
1094 tcg_gen_brcondi_i64(op ? TCG_COND_NE : TCG_COND_EQ,
1095 tcg_cmp, 0, label_match);
1097 gen_goto_tb(s, 0, s->pc);
1098 gen_set_label(label_match);
1099 gen_goto_tb(s, 1, addr);
1102 /* C3.2.5 Test & branch (immediate)
1103 * 31 30 25 24 23 19 18 5 4 0
1104 * +----+-------------+----+-------+-------------+------+
1105 * | b5 | 0 1 1 0 1 1 | op | b40 | imm14 | Rt |
1106 * +----+-------------+----+-------+-------------+------+
1108 static void disas_test_b_imm(DisasContext *s, uint32_t insn)
1110 unsigned int bit_pos, op, rt;
1111 uint64_t addr;
1112 int label_match;
1113 TCGv_i64 tcg_cmp;
1115 bit_pos = (extract32(insn, 31, 1) << 5) | extract32(insn, 19, 5);
1116 op = extract32(insn, 24, 1); /* 0: TBZ; 1: TBNZ */
1117 addr = s->pc + sextract32(insn, 5, 14) * 4 - 4;
1118 rt = extract32(insn, 0, 5);
1120 tcg_cmp = tcg_temp_new_i64();
1121 tcg_gen_andi_i64(tcg_cmp, cpu_reg(s, rt), (1ULL << bit_pos));
1122 label_match = gen_new_label();
1123 tcg_gen_brcondi_i64(op ? TCG_COND_NE : TCG_COND_EQ,
1124 tcg_cmp, 0, label_match);
1125 tcg_temp_free_i64(tcg_cmp);
1126 gen_goto_tb(s, 0, s->pc);
1127 gen_set_label(label_match);
1128 gen_goto_tb(s, 1, addr);
1131 /* C3.2.2 / C5.6.19 Conditional branch (immediate)
1132 * 31 25 24 23 5 4 3 0
1133 * +---------------+----+---------------------+----+------+
1134 * | 0 1 0 1 0 1 0 | o1 | imm19 | o0 | cond |
1135 * +---------------+----+---------------------+----+------+
1137 static void disas_cond_b_imm(DisasContext *s, uint32_t insn)
1139 unsigned int cond;
1140 uint64_t addr;
1142 if ((insn & (1 << 4)) || (insn & (1 << 24))) {
1143 unallocated_encoding(s);
1144 return;
1146 addr = s->pc + sextract32(insn, 5, 19) * 4 - 4;
1147 cond = extract32(insn, 0, 4);
1149 if (cond < 0x0e) {
1150 /* genuinely conditional branches */
1151 int label_match = gen_new_label();
1152 arm_gen_test_cc(cond, label_match);
1153 gen_goto_tb(s, 0, s->pc);
1154 gen_set_label(label_match);
1155 gen_goto_tb(s, 1, addr);
1156 } else {
1157 /* 0xe and 0xf are both "always" conditions */
1158 gen_goto_tb(s, 0, addr);
1162 /* C5.6.68 HINT */
1163 static void handle_hint(DisasContext *s, uint32_t insn,
1164 unsigned int op1, unsigned int op2, unsigned int crm)
1166 unsigned int selector = crm << 3 | op2;
1168 if (op1 != 3) {
1169 unallocated_encoding(s);
1170 return;
1173 switch (selector) {
1174 case 0: /* NOP */
1175 return;
1176 case 3: /* WFI */
1177 s->is_jmp = DISAS_WFI;
1178 return;
1179 case 1: /* YIELD */
1180 case 2: /* WFE */
1181 s->is_jmp = DISAS_WFE;
1182 return;
1183 case 4: /* SEV */
1184 case 5: /* SEVL */
1185 /* we treat all as NOP at least for now */
1186 return;
1187 default:
1188 /* default specified as NOP equivalent */
1189 return;
1193 static void gen_clrex(DisasContext *s, uint32_t insn)
1195 tcg_gen_movi_i64(cpu_exclusive_addr, -1);
1198 /* CLREX, DSB, DMB, ISB */
1199 static void handle_sync(DisasContext *s, uint32_t insn,
1200 unsigned int op1, unsigned int op2, unsigned int crm)
1202 if (op1 != 3) {
1203 unallocated_encoding(s);
1204 return;
1207 switch (op2) {
1208 case 2: /* CLREX */
1209 gen_clrex(s, insn);
1210 return;
1211 case 4: /* DSB */
1212 case 5: /* DMB */
1213 case 6: /* ISB */
1214 /* We don't emulate caches so barriers are no-ops */
1215 return;
1216 default:
1217 unallocated_encoding(s);
1218 return;
1222 /* C5.6.130 MSR (immediate) - move immediate to processor state field */
1223 static void handle_msr_i(DisasContext *s, uint32_t insn,
1224 unsigned int op1, unsigned int op2, unsigned int crm)
1226 int op = op1 << 3 | op2;
1227 switch (op) {
1228 case 0x05: /* SPSel */
1229 if (s->current_el == 0) {
1230 unallocated_encoding(s);
1231 return;
1233 /* fall through */
1234 case 0x1e: /* DAIFSet */
1235 case 0x1f: /* DAIFClear */
1237 TCGv_i32 tcg_imm = tcg_const_i32(crm);
1238 TCGv_i32 tcg_op = tcg_const_i32(op);
1239 gen_a64_set_pc_im(s->pc - 4);
1240 gen_helper_msr_i_pstate(cpu_env, tcg_op, tcg_imm);
1241 tcg_temp_free_i32(tcg_imm);
1242 tcg_temp_free_i32(tcg_op);
1243 s->is_jmp = DISAS_UPDATE;
1244 break;
1246 default:
1247 unallocated_encoding(s);
1248 return;
1252 static void gen_get_nzcv(TCGv_i64 tcg_rt)
1254 TCGv_i32 tmp = tcg_temp_new_i32();
1255 TCGv_i32 nzcv = tcg_temp_new_i32();
1257 /* build bit 31, N */
1258 tcg_gen_andi_i32(nzcv, cpu_NF, (1 << 31));
1259 /* build bit 30, Z */
1260 tcg_gen_setcondi_i32(TCG_COND_EQ, tmp, cpu_ZF, 0);
1261 tcg_gen_deposit_i32(nzcv, nzcv, tmp, 30, 1);
1262 /* build bit 29, C */
1263 tcg_gen_deposit_i32(nzcv, nzcv, cpu_CF, 29, 1);
1264 /* build bit 28, V */
1265 tcg_gen_shri_i32(tmp, cpu_VF, 31);
1266 tcg_gen_deposit_i32(nzcv, nzcv, tmp, 28, 1);
1267 /* generate result */
1268 tcg_gen_extu_i32_i64(tcg_rt, nzcv);
1270 tcg_temp_free_i32(nzcv);
1271 tcg_temp_free_i32(tmp);
1274 static void gen_set_nzcv(TCGv_i64 tcg_rt)
1277 TCGv_i32 nzcv = tcg_temp_new_i32();
1279 /* take NZCV from R[t] */
1280 tcg_gen_trunc_i64_i32(nzcv, tcg_rt);
1282 /* bit 31, N */
1283 tcg_gen_andi_i32(cpu_NF, nzcv, (1 << 31));
1284 /* bit 30, Z */
1285 tcg_gen_andi_i32(cpu_ZF, nzcv, (1 << 30));
1286 tcg_gen_setcondi_i32(TCG_COND_EQ, cpu_ZF, cpu_ZF, 0);
1287 /* bit 29, C */
1288 tcg_gen_andi_i32(cpu_CF, nzcv, (1 << 29));
1289 tcg_gen_shri_i32(cpu_CF, cpu_CF, 29);
1290 /* bit 28, V */
1291 tcg_gen_andi_i32(cpu_VF, nzcv, (1 << 28));
1292 tcg_gen_shli_i32(cpu_VF, cpu_VF, 3);
1293 tcg_temp_free_i32(nzcv);
1296 /* C5.6.129 MRS - move from system register
1297 * C5.6.131 MSR (register) - move to system register
1298 * C5.6.204 SYS
1299 * C5.6.205 SYSL
1300 * These are all essentially the same insn in 'read' and 'write'
1301 * versions, with varying op0 fields.
1303 static void handle_sys(DisasContext *s, uint32_t insn, bool isread,
1304 unsigned int op0, unsigned int op1, unsigned int op2,
1305 unsigned int crn, unsigned int crm, unsigned int rt)
1307 const ARMCPRegInfo *ri;
1308 TCGv_i64 tcg_rt;
1310 ri = get_arm_cp_reginfo(s->cp_regs,
1311 ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP,
1312 crn, crm, op0, op1, op2));
1314 if (!ri) {
1315 /* Unknown register; this might be a guest error or a QEMU
1316 * unimplemented feature.
1318 qemu_log_mask(LOG_UNIMP, "%s access to unsupported AArch64 "
1319 "system register op0:%d op1:%d crn:%d crm:%d op2:%d\n",
1320 isread ? "read" : "write", op0, op1, crn, crm, op2);
1321 unallocated_encoding(s);
1322 return;
1325 /* Check access permissions */
1326 if (!cp_access_ok(s->current_el, ri, isread)) {
1327 unallocated_encoding(s);
1328 return;
1331 if (ri->accessfn) {
1332 /* Emit code to perform further access permissions checks at
1333 * runtime; this may result in an exception.
1335 TCGv_ptr tmpptr;
1336 TCGv_i32 tcg_syn;
1337 uint32_t syndrome;
1339 gen_a64_set_pc_im(s->pc - 4);
1340 tmpptr = tcg_const_ptr(ri);
1341 syndrome = syn_aa64_sysregtrap(op0, op1, op2, crn, crm, rt, isread);
1342 tcg_syn = tcg_const_i32(syndrome);
1343 gen_helper_access_check_cp_reg(cpu_env, tmpptr, tcg_syn);
1344 tcg_temp_free_ptr(tmpptr);
1345 tcg_temp_free_i32(tcg_syn);
1348 /* Handle special cases first */
1349 switch (ri->type & ~(ARM_CP_FLAG_MASK & ~ARM_CP_SPECIAL)) {
1350 case ARM_CP_NOP:
1351 return;
1352 case ARM_CP_NZCV:
1353 tcg_rt = cpu_reg(s, rt);
1354 if (isread) {
1355 gen_get_nzcv(tcg_rt);
1356 } else {
1357 gen_set_nzcv(tcg_rt);
1359 return;
1360 case ARM_CP_CURRENTEL:
1361 /* Reads as current EL value from pstate, which is
1362 * guaranteed to be constant by the tb flags.
1364 tcg_rt = cpu_reg(s, rt);
1365 tcg_gen_movi_i64(tcg_rt, s->current_el << 2);
1366 return;
1367 case ARM_CP_DC_ZVA:
1368 /* Writes clear the aligned block of memory which rt points into. */
1369 tcg_rt = cpu_reg(s, rt);
1370 gen_helper_dc_zva(cpu_env, tcg_rt);
1371 return;
1372 default:
1373 break;
1376 if (use_icount && (ri->type & ARM_CP_IO)) {
1377 gen_io_start();
1380 tcg_rt = cpu_reg(s, rt);
1382 if (isread) {
1383 if (ri->type & ARM_CP_CONST) {
1384 tcg_gen_movi_i64(tcg_rt, ri->resetvalue);
1385 } else if (ri->readfn) {
1386 TCGv_ptr tmpptr;
1387 tmpptr = tcg_const_ptr(ri);
1388 gen_helper_get_cp_reg64(tcg_rt, cpu_env, tmpptr);
1389 tcg_temp_free_ptr(tmpptr);
1390 } else {
1391 tcg_gen_ld_i64(tcg_rt, cpu_env, ri->fieldoffset);
1393 } else {
1394 if (ri->type & ARM_CP_CONST) {
1395 /* If not forbidden by access permissions, treat as WI */
1396 return;
1397 } else if (ri->writefn) {
1398 TCGv_ptr tmpptr;
1399 tmpptr = tcg_const_ptr(ri);
1400 gen_helper_set_cp_reg64(cpu_env, tmpptr, tcg_rt);
1401 tcg_temp_free_ptr(tmpptr);
1402 } else {
1403 tcg_gen_st_i64(tcg_rt, cpu_env, ri->fieldoffset);
1407 if (use_icount && (ri->type & ARM_CP_IO)) {
1408 /* I/O operations must end the TB here (whether read or write) */
1409 gen_io_end();
1410 s->is_jmp = DISAS_UPDATE;
1411 } else if (!isread && !(ri->type & ARM_CP_SUPPRESS_TB_END)) {
1412 /* We default to ending the TB on a coprocessor register write,
1413 * but allow this to be suppressed by the register definition
1414 * (usually only necessary to work around guest bugs).
1416 s->is_jmp = DISAS_UPDATE;
1420 /* C3.2.4 System
1421 * 31 22 21 20 19 18 16 15 12 11 8 7 5 4 0
1422 * +---------------------+---+-----+-----+-------+-------+-----+------+
1423 * | 1 1 0 1 0 1 0 1 0 0 | L | op0 | op1 | CRn | CRm | op2 | Rt |
1424 * +---------------------+---+-----+-----+-------+-------+-----+------+
1426 static void disas_system(DisasContext *s, uint32_t insn)
1428 unsigned int l, op0, op1, crn, crm, op2, rt;
1429 l = extract32(insn, 21, 1);
1430 op0 = extract32(insn, 19, 2);
1431 op1 = extract32(insn, 16, 3);
1432 crn = extract32(insn, 12, 4);
1433 crm = extract32(insn, 8, 4);
1434 op2 = extract32(insn, 5, 3);
1435 rt = extract32(insn, 0, 5);
1437 if (op0 == 0) {
1438 if (l || rt != 31) {
1439 unallocated_encoding(s);
1440 return;
1442 switch (crn) {
1443 case 2: /* C5.6.68 HINT */
1444 handle_hint(s, insn, op1, op2, crm);
1445 break;
1446 case 3: /* CLREX, DSB, DMB, ISB */
1447 handle_sync(s, insn, op1, op2, crm);
1448 break;
1449 case 4: /* C5.6.130 MSR (immediate) */
1450 handle_msr_i(s, insn, op1, op2, crm);
1451 break;
1452 default:
1453 unallocated_encoding(s);
1454 break;
1456 return;
1458 handle_sys(s, insn, l, op0, op1, op2, crn, crm, rt);
1461 /* C3.2.3 Exception generation
1463 * 31 24 23 21 20 5 4 2 1 0
1464 * +-----------------+-----+------------------------+-----+----+
1465 * | 1 1 0 1 0 1 0 0 | opc | imm16 | op2 | LL |
1466 * +-----------------------+------------------------+----------+
1468 static void disas_exc(DisasContext *s, uint32_t insn)
1470 int opc = extract32(insn, 21, 3);
1471 int op2_ll = extract32(insn, 0, 5);
1472 int imm16 = extract32(insn, 5, 16);
1473 TCGv_i32 tmp;
1475 switch (opc) {
1476 case 0:
1477 /* For SVC, HVC and SMC we advance the single-step state
1478 * machine before taking the exception. This is architecturally
1479 * mandated, to ensure that single-stepping a system call
1480 * instruction works properly.
1482 switch (op2_ll) {
1483 case 1:
1484 gen_ss_advance(s);
1485 gen_exception_insn(s, 0, EXCP_SWI, syn_aa64_svc(imm16));
1486 break;
1487 case 2:
1488 if (s->current_el == 0) {
1489 unallocated_encoding(s);
1490 break;
1492 /* The pre HVC helper handles cases when HVC gets trapped
1493 * as an undefined insn by runtime configuration.
1495 gen_a64_set_pc_im(s->pc - 4);
1496 gen_helper_pre_hvc(cpu_env);
1497 gen_ss_advance(s);
1498 gen_exception_insn(s, 0, EXCP_HVC, syn_aa64_hvc(imm16));
1499 break;
1500 case 3:
1501 if (s->current_el == 0) {
1502 unallocated_encoding(s);
1503 break;
1505 gen_a64_set_pc_im(s->pc - 4);
1506 tmp = tcg_const_i32(syn_aa64_smc(imm16));
1507 gen_helper_pre_smc(cpu_env, tmp);
1508 tcg_temp_free_i32(tmp);
1509 gen_ss_advance(s);
1510 gen_exception_insn(s, 0, EXCP_SMC, syn_aa64_smc(imm16));
1511 break;
1512 default:
1513 unallocated_encoding(s);
1514 break;
1516 break;
1517 case 1:
1518 if (op2_ll != 0) {
1519 unallocated_encoding(s);
1520 break;
1522 /* BRK */
1523 gen_exception_insn(s, 4, EXCP_BKPT, syn_aa64_bkpt(imm16));
1524 break;
1525 case 2:
1526 if (op2_ll != 0) {
1527 unallocated_encoding(s);
1528 break;
1530 /* HLT */
1531 unsupported_encoding(s, insn);
1532 break;
1533 case 5:
1534 if (op2_ll < 1 || op2_ll > 3) {
1535 unallocated_encoding(s);
1536 break;
1538 /* DCPS1, DCPS2, DCPS3 */
1539 unsupported_encoding(s, insn);
1540 break;
1541 default:
1542 unallocated_encoding(s);
1543 break;
1547 /* C3.2.7 Unconditional branch (register)
1548 * 31 25 24 21 20 16 15 10 9 5 4 0
1549 * +---------------+-------+-------+-------+------+-------+
1550 * | 1 1 0 1 0 1 1 | opc | op2 | op3 | Rn | op4 |
1551 * +---------------+-------+-------+-------+------+-------+
1553 static void disas_uncond_b_reg(DisasContext *s, uint32_t insn)
1555 unsigned int opc, op2, op3, rn, op4;
1557 opc = extract32(insn, 21, 4);
1558 op2 = extract32(insn, 16, 5);
1559 op3 = extract32(insn, 10, 6);
1560 rn = extract32(insn, 5, 5);
1561 op4 = extract32(insn, 0, 5);
1563 if (op4 != 0x0 || op3 != 0x0 || op2 != 0x1f) {
1564 unallocated_encoding(s);
1565 return;
1568 switch (opc) {
1569 case 0: /* BR */
1570 case 2: /* RET */
1571 tcg_gen_mov_i64(cpu_pc, cpu_reg(s, rn));
1572 break;
1573 case 1: /* BLR */
1574 tcg_gen_mov_i64(cpu_pc, cpu_reg(s, rn));
1575 tcg_gen_movi_i64(cpu_reg(s, 30), s->pc);
1576 break;
1577 case 4: /* ERET */
1578 if (s->current_el == 0) {
1579 unallocated_encoding(s);
1580 return;
1582 gen_helper_exception_return(cpu_env);
1583 s->is_jmp = DISAS_JUMP;
1584 return;
1585 case 5: /* DRPS */
1586 if (rn != 0x1f) {
1587 unallocated_encoding(s);
1588 } else {
1589 unsupported_encoding(s, insn);
1591 return;
1592 default:
1593 unallocated_encoding(s);
1594 return;
1597 s->is_jmp = DISAS_JUMP;
1600 /* C3.2 Branches, exception generating and system instructions */
1601 static void disas_b_exc_sys(DisasContext *s, uint32_t insn)
1603 switch (extract32(insn, 25, 7)) {
1604 case 0x0a: case 0x0b:
1605 case 0x4a: case 0x4b: /* Unconditional branch (immediate) */
1606 disas_uncond_b_imm(s, insn);
1607 break;
1608 case 0x1a: case 0x5a: /* Compare & branch (immediate) */
1609 disas_comp_b_imm(s, insn);
1610 break;
1611 case 0x1b: case 0x5b: /* Test & branch (immediate) */
1612 disas_test_b_imm(s, insn);
1613 break;
1614 case 0x2a: /* Conditional branch (immediate) */
1615 disas_cond_b_imm(s, insn);
1616 break;
1617 case 0x6a: /* Exception generation / System */
1618 if (insn & (1 << 24)) {
1619 disas_system(s, insn);
1620 } else {
1621 disas_exc(s, insn);
1623 break;
1624 case 0x6b: /* Unconditional branch (register) */
1625 disas_uncond_b_reg(s, insn);
1626 break;
1627 default:
1628 unallocated_encoding(s);
1629 break;
1634 * Load/Store exclusive instructions are implemented by remembering
1635 * the value/address loaded, and seeing if these are the same
1636 * when the store is performed. This is not actually the architecturally
1637 * mandated semantics, but it works for typical guest code sequences
1638 * and avoids having to monitor regular stores.
1640 * In system emulation mode only one CPU will be running at once, so
1641 * this sequence is effectively atomic. In user emulation mode we
1642 * throw an exception and handle the atomic operation elsewhere.
1644 static void gen_load_exclusive(DisasContext *s, int rt, int rt2,
1645 TCGv_i64 addr, int size, bool is_pair)
1647 TCGv_i64 tmp = tcg_temp_new_i64();
1648 TCGMemOp memop = MO_TE + size;
1650 g_assert(size <= 3);
1651 tcg_gen_qemu_ld_i64(tmp, addr, get_mem_index(s), memop);
1653 if (is_pair) {
1654 TCGv_i64 addr2 = tcg_temp_new_i64();
1655 TCGv_i64 hitmp = tcg_temp_new_i64();
1657 g_assert(size >= 2);
1658 tcg_gen_addi_i64(addr2, addr, 1 << size);
1659 tcg_gen_qemu_ld_i64(hitmp, addr2, get_mem_index(s), memop);
1660 tcg_temp_free_i64(addr2);
1661 tcg_gen_mov_i64(cpu_exclusive_high, hitmp);
1662 tcg_gen_mov_i64(cpu_reg(s, rt2), hitmp);
1663 tcg_temp_free_i64(hitmp);
1666 tcg_gen_mov_i64(cpu_exclusive_val, tmp);
1667 tcg_gen_mov_i64(cpu_reg(s, rt), tmp);
1669 tcg_temp_free_i64(tmp);
1670 tcg_gen_mov_i64(cpu_exclusive_addr, addr);
1673 #ifdef CONFIG_USER_ONLY
1674 static void gen_store_exclusive(DisasContext *s, int rd, int rt, int rt2,
1675 TCGv_i64 addr, int size, int is_pair)
1677 tcg_gen_mov_i64(cpu_exclusive_test, addr);
1678 tcg_gen_movi_i32(cpu_exclusive_info,
1679 size | is_pair << 2 | (rd << 4) | (rt << 9) | (rt2 << 14));
1680 gen_exception_internal_insn(s, 4, EXCP_STREX);
1682 #else
1683 static void gen_store_exclusive(DisasContext *s, int rd, int rt, int rt2,
1684 TCGv_i64 inaddr, int size, int is_pair)
1686 /* if (env->exclusive_addr == addr && env->exclusive_val == [addr]
1687 * && (!is_pair || env->exclusive_high == [addr + datasize])) {
1688 * [addr] = {Rt};
1689 * if (is_pair) {
1690 * [addr + datasize] = {Rt2};
1692 * {Rd} = 0;
1693 * } else {
1694 * {Rd} = 1;
1696 * env->exclusive_addr = -1;
1698 int fail_label = gen_new_label();
1699 int done_label = gen_new_label();
1700 TCGv_i64 addr = tcg_temp_local_new_i64();
1701 TCGv_i64 tmp;
1703 /* Copy input into a local temp so it is not trashed when the
1704 * basic block ends at the branch insn.
1706 tcg_gen_mov_i64(addr, inaddr);
1707 tcg_gen_brcond_i64(TCG_COND_NE, addr, cpu_exclusive_addr, fail_label);
1709 tmp = tcg_temp_new_i64();
1710 tcg_gen_qemu_ld_i64(tmp, addr, get_mem_index(s), MO_TE + size);
1711 tcg_gen_brcond_i64(TCG_COND_NE, tmp, cpu_exclusive_val, fail_label);
1712 tcg_temp_free_i64(tmp);
1714 if (is_pair) {
1715 TCGv_i64 addrhi = tcg_temp_new_i64();
1716 TCGv_i64 tmphi = tcg_temp_new_i64();
1718 tcg_gen_addi_i64(addrhi, addr, 1 << size);
1719 tcg_gen_qemu_ld_i64(tmphi, addrhi, get_mem_index(s), MO_TE + size);
1720 tcg_gen_brcond_i64(TCG_COND_NE, tmphi, cpu_exclusive_high, fail_label);
1722 tcg_temp_free_i64(tmphi);
1723 tcg_temp_free_i64(addrhi);
1726 /* We seem to still have the exclusive monitor, so do the store */
1727 tcg_gen_qemu_st_i64(cpu_reg(s, rt), addr, get_mem_index(s), MO_TE + size);
1728 if (is_pair) {
1729 TCGv_i64 addrhi = tcg_temp_new_i64();
1731 tcg_gen_addi_i64(addrhi, addr, 1 << size);
1732 tcg_gen_qemu_st_i64(cpu_reg(s, rt2), addrhi,
1733 get_mem_index(s), MO_TE + size);
1734 tcg_temp_free_i64(addrhi);
1737 tcg_temp_free_i64(addr);
1739 tcg_gen_movi_i64(cpu_reg(s, rd), 0);
1740 tcg_gen_br(done_label);
1741 gen_set_label(fail_label);
1742 tcg_gen_movi_i64(cpu_reg(s, rd), 1);
1743 gen_set_label(done_label);
1744 tcg_gen_movi_i64(cpu_exclusive_addr, -1);
1747 #endif
1749 /* C3.3.6 Load/store exclusive
1751 * 31 30 29 24 23 22 21 20 16 15 14 10 9 5 4 0
1752 * +-----+-------------+----+---+----+------+----+-------+------+------+
1753 * | sz | 0 0 1 0 0 0 | o2 | L | o1 | Rs | o0 | Rt2 | Rn | Rt |
1754 * +-----+-------------+----+---+----+------+----+-------+------+------+
1756 * sz: 00 -> 8 bit, 01 -> 16 bit, 10 -> 32 bit, 11 -> 64 bit
1757 * L: 0 -> store, 1 -> load
1758 * o2: 0 -> exclusive, 1 -> not
1759 * o1: 0 -> single register, 1 -> register pair
1760 * o0: 1 -> load-acquire/store-release, 0 -> not
1762 * o0 == 0 AND o2 == 1 is un-allocated
1763 * o1 == 1 is un-allocated except for 32 and 64 bit sizes
1765 static void disas_ldst_excl(DisasContext *s, uint32_t insn)
1767 int rt = extract32(insn, 0, 5);
1768 int rn = extract32(insn, 5, 5);
1769 int rt2 = extract32(insn, 10, 5);
1770 int is_lasr = extract32(insn, 15, 1);
1771 int rs = extract32(insn, 16, 5);
1772 int is_pair = extract32(insn, 21, 1);
1773 int is_store = !extract32(insn, 22, 1);
1774 int is_excl = !extract32(insn, 23, 1);
1775 int size = extract32(insn, 30, 2);
1776 TCGv_i64 tcg_addr;
1778 if ((!is_excl && !is_lasr) ||
1779 (is_pair && size < 2)) {
1780 unallocated_encoding(s);
1781 return;
1784 if (rn == 31) {
1785 gen_check_sp_alignment(s);
1787 tcg_addr = read_cpu_reg_sp(s, rn, 1);
1789 /* Note that since TCG is single threaded load-acquire/store-release
1790 * semantics require no extra if (is_lasr) { ... } handling.
1793 if (is_excl) {
1794 if (!is_store) {
1795 s->is_ldex = true;
1796 gen_load_exclusive(s, rt, rt2, tcg_addr, size, is_pair);
1797 } else {
1798 gen_store_exclusive(s, rs, rt, rt2, tcg_addr, size, is_pair);
1800 } else {
1801 TCGv_i64 tcg_rt = cpu_reg(s, rt);
1802 if (is_store) {
1803 do_gpr_st(s, tcg_rt, tcg_addr, size);
1804 } else {
1805 do_gpr_ld(s, tcg_rt, tcg_addr, size, false, false);
1807 if (is_pair) {
1808 TCGv_i64 tcg_rt2 = cpu_reg(s, rt);
1809 tcg_gen_addi_i64(tcg_addr, tcg_addr, 1 << size);
1810 if (is_store) {
1811 do_gpr_st(s, tcg_rt2, tcg_addr, size);
1812 } else {
1813 do_gpr_ld(s, tcg_rt2, tcg_addr, size, false, false);
1820 * C3.3.5 Load register (literal)
1822 * 31 30 29 27 26 25 24 23 5 4 0
1823 * +-----+-------+---+-----+-------------------+-------+
1824 * | opc | 0 1 1 | V | 0 0 | imm19 | Rt |
1825 * +-----+-------+---+-----+-------------------+-------+
1827 * V: 1 -> vector (simd/fp)
1828 * opc (non-vector): 00 -> 32 bit, 01 -> 64 bit,
1829 * 10-> 32 bit signed, 11 -> prefetch
1830 * opc (vector): 00 -> 32 bit, 01 -> 64 bit, 10 -> 128 bit (11 unallocated)
1832 static void disas_ld_lit(DisasContext *s, uint32_t insn)
1834 int rt = extract32(insn, 0, 5);
1835 int64_t imm = sextract32(insn, 5, 19) << 2;
1836 bool is_vector = extract32(insn, 26, 1);
1837 int opc = extract32(insn, 30, 2);
1838 bool is_signed = false;
1839 int size = 2;
1840 TCGv_i64 tcg_rt, tcg_addr;
1842 if (is_vector) {
1843 if (opc == 3) {
1844 unallocated_encoding(s);
1845 return;
1847 size = 2 + opc;
1848 if (!fp_access_check(s)) {
1849 return;
1851 } else {
1852 if (opc == 3) {
1853 /* PRFM (literal) : prefetch */
1854 return;
1856 size = 2 + extract32(opc, 0, 1);
1857 is_signed = extract32(opc, 1, 1);
1860 tcg_rt = cpu_reg(s, rt);
1862 tcg_addr = tcg_const_i64((s->pc - 4) + imm);
1863 if (is_vector) {
1864 do_fp_ld(s, rt, tcg_addr, size);
1865 } else {
1866 do_gpr_ld(s, tcg_rt, tcg_addr, size, is_signed, false);
1868 tcg_temp_free_i64(tcg_addr);
1872 * C5.6.80 LDNP (Load Pair - non-temporal hint)
1873 * C5.6.81 LDP (Load Pair - non vector)
1874 * C5.6.82 LDPSW (Load Pair Signed Word - non vector)
1875 * C5.6.176 STNP (Store Pair - non-temporal hint)
1876 * C5.6.177 STP (Store Pair - non vector)
1877 * C6.3.165 LDNP (Load Pair of SIMD&FP - non-temporal hint)
1878 * C6.3.165 LDP (Load Pair of SIMD&FP)
1879 * C6.3.284 STNP (Store Pair of SIMD&FP - non-temporal hint)
1880 * C6.3.284 STP (Store Pair of SIMD&FP)
1882 * 31 30 29 27 26 25 24 23 22 21 15 14 10 9 5 4 0
1883 * +-----+-------+---+---+-------+---+-----------------------------+
1884 * | opc | 1 0 1 | V | 0 | index | L | imm7 | Rt2 | Rn | Rt |
1885 * +-----+-------+---+---+-------+---+-------+-------+------+------+
1887 * opc: LDP/STP/LDNP/STNP 00 -> 32 bit, 10 -> 64 bit
1888 * LDPSW 01
1889 * LDP/STP/LDNP/STNP (SIMD) 00 -> 32 bit, 01 -> 64 bit, 10 -> 128 bit
1890 * V: 0 -> GPR, 1 -> Vector
1891 * idx: 00 -> signed offset with non-temporal hint, 01 -> post-index,
1892 * 10 -> signed offset, 11 -> pre-index
1893 * L: 0 -> Store 1 -> Load
1895 * Rt, Rt2 = GPR or SIMD registers to be stored
1896 * Rn = general purpose register containing address
1897 * imm7 = signed offset (multiple of 4 or 8 depending on size)
1899 static void disas_ldst_pair(DisasContext *s, uint32_t insn)
1901 int rt = extract32(insn, 0, 5);
1902 int rn = extract32(insn, 5, 5);
1903 int rt2 = extract32(insn, 10, 5);
1904 int64_t offset = sextract32(insn, 15, 7);
1905 int index = extract32(insn, 23, 2);
1906 bool is_vector = extract32(insn, 26, 1);
1907 bool is_load = extract32(insn, 22, 1);
1908 int opc = extract32(insn, 30, 2);
1910 bool is_signed = false;
1911 bool postindex = false;
1912 bool wback = false;
1914 TCGv_i64 tcg_addr; /* calculated address */
1915 int size;
1917 if (opc == 3) {
1918 unallocated_encoding(s);
1919 return;
1922 if (is_vector) {
1923 size = 2 + opc;
1924 } else {
1925 size = 2 + extract32(opc, 1, 1);
1926 is_signed = extract32(opc, 0, 1);
1927 if (!is_load && is_signed) {
1928 unallocated_encoding(s);
1929 return;
1933 switch (index) {
1934 case 1: /* post-index */
1935 postindex = true;
1936 wback = true;
1937 break;
1938 case 0:
1939 /* signed offset with "non-temporal" hint. Since we don't emulate
1940 * caches we don't care about hints to the cache system about
1941 * data access patterns, and handle this identically to plain
1942 * signed offset.
1944 if (is_signed) {
1945 /* There is no non-temporal-hint version of LDPSW */
1946 unallocated_encoding(s);
1947 return;
1949 postindex = false;
1950 break;
1951 case 2: /* signed offset, rn not updated */
1952 postindex = false;
1953 break;
1954 case 3: /* pre-index */
1955 postindex = false;
1956 wback = true;
1957 break;
1960 if (is_vector && !fp_access_check(s)) {
1961 return;
1964 offset <<= size;
1966 if (rn == 31) {
1967 gen_check_sp_alignment(s);
1970 tcg_addr = read_cpu_reg_sp(s, rn, 1);
1972 if (!postindex) {
1973 tcg_gen_addi_i64(tcg_addr, tcg_addr, offset);
1976 if (is_vector) {
1977 if (is_load) {
1978 do_fp_ld(s, rt, tcg_addr, size);
1979 } else {
1980 do_fp_st(s, rt, tcg_addr, size);
1982 } else {
1983 TCGv_i64 tcg_rt = cpu_reg(s, rt);
1984 if (is_load) {
1985 do_gpr_ld(s, tcg_rt, tcg_addr, size, is_signed, false);
1986 } else {
1987 do_gpr_st(s, tcg_rt, tcg_addr, size);
1990 tcg_gen_addi_i64(tcg_addr, tcg_addr, 1 << size);
1991 if (is_vector) {
1992 if (is_load) {
1993 do_fp_ld(s, rt2, tcg_addr, size);
1994 } else {
1995 do_fp_st(s, rt2, tcg_addr, size);
1997 } else {
1998 TCGv_i64 tcg_rt2 = cpu_reg(s, rt2);
1999 if (is_load) {
2000 do_gpr_ld(s, tcg_rt2, tcg_addr, size, is_signed, false);
2001 } else {
2002 do_gpr_st(s, tcg_rt2, tcg_addr, size);
2006 if (wback) {
2007 if (postindex) {
2008 tcg_gen_addi_i64(tcg_addr, tcg_addr, offset - (1 << size));
2009 } else {
2010 tcg_gen_subi_i64(tcg_addr, tcg_addr, 1 << size);
2012 tcg_gen_mov_i64(cpu_reg_sp(s, rn), tcg_addr);
2017 * C3.3.8 Load/store (immediate post-indexed)
2018 * C3.3.9 Load/store (immediate pre-indexed)
2019 * C3.3.12 Load/store (unscaled immediate)
2021 * 31 30 29 27 26 25 24 23 22 21 20 12 11 10 9 5 4 0
2022 * +----+-------+---+-----+-----+---+--------+-----+------+------+
2023 * |size| 1 1 1 | V | 0 0 | opc | 0 | imm9 | idx | Rn | Rt |
2024 * +----+-------+---+-----+-----+---+--------+-----+------+------+
2026 * idx = 01 -> post-indexed, 11 pre-indexed, 00 unscaled imm. (no writeback)
2027 10 -> unprivileged
2028 * V = 0 -> non-vector
2029 * size: 00 -> 8 bit, 01 -> 16 bit, 10 -> 32 bit, 11 -> 64bit
2030 * opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
2032 static void disas_ldst_reg_imm9(DisasContext *s, uint32_t insn)
2034 int rt = extract32(insn, 0, 5);
2035 int rn = extract32(insn, 5, 5);
2036 int imm9 = sextract32(insn, 12, 9);
2037 int opc = extract32(insn, 22, 2);
2038 int size = extract32(insn, 30, 2);
2039 int idx = extract32(insn, 10, 2);
2040 bool is_signed = false;
2041 bool is_store = false;
2042 bool is_extended = false;
2043 bool is_unpriv = (idx == 2);
2044 bool is_vector = extract32(insn, 26, 1);
2045 bool post_index;
2046 bool writeback;
2048 TCGv_i64 tcg_addr;
2050 if (is_vector) {
2051 size |= (opc & 2) << 1;
2052 if (size > 4 || is_unpriv) {
2053 unallocated_encoding(s);
2054 return;
2056 is_store = ((opc & 1) == 0);
2057 if (!fp_access_check(s)) {
2058 return;
2060 } else {
2061 if (size == 3 && opc == 2) {
2062 /* PRFM - prefetch */
2063 if (is_unpriv) {
2064 unallocated_encoding(s);
2065 return;
2067 return;
2069 if (opc == 3 && size > 1) {
2070 unallocated_encoding(s);
2071 return;
2073 is_store = (opc == 0);
2074 is_signed = opc & (1<<1);
2075 is_extended = (size < 3) && (opc & 1);
2078 switch (idx) {
2079 case 0:
2080 case 2:
2081 post_index = false;
2082 writeback = false;
2083 break;
2084 case 1:
2085 post_index = true;
2086 writeback = true;
2087 break;
2088 case 3:
2089 post_index = false;
2090 writeback = true;
2091 break;
2094 if (rn == 31) {
2095 gen_check_sp_alignment(s);
2097 tcg_addr = read_cpu_reg_sp(s, rn, 1);
2099 if (!post_index) {
2100 tcg_gen_addi_i64(tcg_addr, tcg_addr, imm9);
2103 if (is_vector) {
2104 if (is_store) {
2105 do_fp_st(s, rt, tcg_addr, size);
2106 } else {
2107 do_fp_ld(s, rt, tcg_addr, size);
2109 } else {
2110 TCGv_i64 tcg_rt = cpu_reg(s, rt);
2111 int memidx = is_unpriv ? 1 : get_mem_index(s);
2113 if (is_store) {
2114 do_gpr_st_memidx(s, tcg_rt, tcg_addr, size, memidx);
2115 } else {
2116 do_gpr_ld_memidx(s, tcg_rt, tcg_addr, size,
2117 is_signed, is_extended, memidx);
2121 if (writeback) {
2122 TCGv_i64 tcg_rn = cpu_reg_sp(s, rn);
2123 if (post_index) {
2124 tcg_gen_addi_i64(tcg_addr, tcg_addr, imm9);
2126 tcg_gen_mov_i64(tcg_rn, tcg_addr);
2131 * C3.3.10 Load/store (register offset)
2133 * 31 30 29 27 26 25 24 23 22 21 20 16 15 13 12 11 10 9 5 4 0
2134 * +----+-------+---+-----+-----+---+------+-----+--+-----+----+----+
2135 * |size| 1 1 1 | V | 0 0 | opc | 1 | Rm | opt | S| 1 0 | Rn | Rt |
2136 * +----+-------+---+-----+-----+---+------+-----+--+-----+----+----+
2138 * For non-vector:
2139 * size: 00-> byte, 01 -> 16 bit, 10 -> 32bit, 11 -> 64bit
2140 * opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
2141 * For vector:
2142 * size is opc<1>:size<1:0> so 100 -> 128 bit; 110 and 111 unallocated
2143 * opc<0>: 0 -> store, 1 -> load
2144 * V: 1 -> vector/simd
2145 * opt: extend encoding (see DecodeRegExtend)
2146 * S: if S=1 then scale (essentially index by sizeof(size))
2147 * Rt: register to transfer into/out of
2148 * Rn: address register or SP for base
2149 * Rm: offset register or ZR for offset
2151 static void disas_ldst_reg_roffset(DisasContext *s, uint32_t insn)
2153 int rt = extract32(insn, 0, 5);
2154 int rn = extract32(insn, 5, 5);
2155 int shift = extract32(insn, 12, 1);
2156 int rm = extract32(insn, 16, 5);
2157 int opc = extract32(insn, 22, 2);
2158 int opt = extract32(insn, 13, 3);
2159 int size = extract32(insn, 30, 2);
2160 bool is_signed = false;
2161 bool is_store = false;
2162 bool is_extended = false;
2163 bool is_vector = extract32(insn, 26, 1);
2165 TCGv_i64 tcg_rm;
2166 TCGv_i64 tcg_addr;
2168 if (extract32(opt, 1, 1) == 0) {
2169 unallocated_encoding(s);
2170 return;
2173 if (is_vector) {
2174 size |= (opc & 2) << 1;
2175 if (size > 4) {
2176 unallocated_encoding(s);
2177 return;
2179 is_store = !extract32(opc, 0, 1);
2180 if (!fp_access_check(s)) {
2181 return;
2183 } else {
2184 if (size == 3 && opc == 2) {
2185 /* PRFM - prefetch */
2186 return;
2188 if (opc == 3 && size > 1) {
2189 unallocated_encoding(s);
2190 return;
2192 is_store = (opc == 0);
2193 is_signed = extract32(opc, 1, 1);
2194 is_extended = (size < 3) && extract32(opc, 0, 1);
2197 if (rn == 31) {
2198 gen_check_sp_alignment(s);
2200 tcg_addr = read_cpu_reg_sp(s, rn, 1);
2202 tcg_rm = read_cpu_reg(s, rm, 1);
2203 ext_and_shift_reg(tcg_rm, tcg_rm, opt, shift ? size : 0);
2205 tcg_gen_add_i64(tcg_addr, tcg_addr, tcg_rm);
2207 if (is_vector) {
2208 if (is_store) {
2209 do_fp_st(s, rt, tcg_addr, size);
2210 } else {
2211 do_fp_ld(s, rt, tcg_addr, size);
2213 } else {
2214 TCGv_i64 tcg_rt = cpu_reg(s, rt);
2215 if (is_store) {
2216 do_gpr_st(s, tcg_rt, tcg_addr, size);
2217 } else {
2218 do_gpr_ld(s, tcg_rt, tcg_addr, size, is_signed, is_extended);
2224 * C3.3.13 Load/store (unsigned immediate)
2226 * 31 30 29 27 26 25 24 23 22 21 10 9 5
2227 * +----+-------+---+-----+-----+------------+-------+------+
2228 * |size| 1 1 1 | V | 0 1 | opc | imm12 | Rn | Rt |
2229 * +----+-------+---+-----+-----+------------+-------+------+
2231 * For non-vector:
2232 * size: 00-> byte, 01 -> 16 bit, 10 -> 32bit, 11 -> 64bit
2233 * opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
2234 * For vector:
2235 * size is opc<1>:size<1:0> so 100 -> 128 bit; 110 and 111 unallocated
2236 * opc<0>: 0 -> store, 1 -> load
2237 * Rn: base address register (inc SP)
2238 * Rt: target register
2240 static void disas_ldst_reg_unsigned_imm(DisasContext *s, uint32_t insn)
2242 int rt = extract32(insn, 0, 5);
2243 int rn = extract32(insn, 5, 5);
2244 unsigned int imm12 = extract32(insn, 10, 12);
2245 bool is_vector = extract32(insn, 26, 1);
2246 int size = extract32(insn, 30, 2);
2247 int opc = extract32(insn, 22, 2);
2248 unsigned int offset;
2250 TCGv_i64 tcg_addr;
2252 bool is_store;
2253 bool is_signed = false;
2254 bool is_extended = false;
2256 if (is_vector) {
2257 size |= (opc & 2) << 1;
2258 if (size > 4) {
2259 unallocated_encoding(s);
2260 return;
2262 is_store = !extract32(opc, 0, 1);
2263 if (!fp_access_check(s)) {
2264 return;
2266 } else {
2267 if (size == 3 && opc == 2) {
2268 /* PRFM - prefetch */
2269 return;
2271 if (opc == 3 && size > 1) {
2272 unallocated_encoding(s);
2273 return;
2275 is_store = (opc == 0);
2276 is_signed = extract32(opc, 1, 1);
2277 is_extended = (size < 3) && extract32(opc, 0, 1);
2280 if (rn == 31) {
2281 gen_check_sp_alignment(s);
2283 tcg_addr = read_cpu_reg_sp(s, rn, 1);
2284 offset = imm12 << size;
2285 tcg_gen_addi_i64(tcg_addr, tcg_addr, offset);
2287 if (is_vector) {
2288 if (is_store) {
2289 do_fp_st(s, rt, tcg_addr, size);
2290 } else {
2291 do_fp_ld(s, rt, tcg_addr, size);
2293 } else {
2294 TCGv_i64 tcg_rt = cpu_reg(s, rt);
2295 if (is_store) {
2296 do_gpr_st(s, tcg_rt, tcg_addr, size);
2297 } else {
2298 do_gpr_ld(s, tcg_rt, tcg_addr, size, is_signed, is_extended);
2303 /* Load/store register (all forms) */
2304 static void disas_ldst_reg(DisasContext *s, uint32_t insn)
2306 switch (extract32(insn, 24, 2)) {
2307 case 0:
2308 if (extract32(insn, 21, 1) == 1 && extract32(insn, 10, 2) == 2) {
2309 disas_ldst_reg_roffset(s, insn);
2310 } else {
2311 /* Load/store register (unscaled immediate)
2312 * Load/store immediate pre/post-indexed
2313 * Load/store register unprivileged
2315 disas_ldst_reg_imm9(s, insn);
2317 break;
2318 case 1:
2319 disas_ldst_reg_unsigned_imm(s, insn);
2320 break;
2321 default:
2322 unallocated_encoding(s);
2323 break;
2327 /* C3.3.1 AdvSIMD load/store multiple structures
2329 * 31 30 29 23 22 21 16 15 12 11 10 9 5 4 0
2330 * +---+---+---------------+---+-------------+--------+------+------+------+
2331 * | 0 | Q | 0 0 1 1 0 0 0 | L | 0 0 0 0 0 0 | opcode | size | Rn | Rt |
2332 * +---+---+---------------+---+-------------+--------+------+------+------+
2334 * C3.3.2 AdvSIMD load/store multiple structures (post-indexed)
2336 * 31 30 29 23 22 21 20 16 15 12 11 10 9 5 4 0
2337 * +---+---+---------------+---+---+---------+--------+------+------+------+
2338 * | 0 | Q | 0 0 1 1 0 0 1 | L | 0 | Rm | opcode | size | Rn | Rt |
2339 * +---+---+---------------+---+---+---------+--------+------+------+------+
2341 * Rt: first (or only) SIMD&FP register to be transferred
2342 * Rn: base address or SP
2343 * Rm (post-index only): post-index register (when !31) or size dependent #imm
2345 static void disas_ldst_multiple_struct(DisasContext *s, uint32_t insn)
2347 int rt = extract32(insn, 0, 5);
2348 int rn = extract32(insn, 5, 5);
2349 int size = extract32(insn, 10, 2);
2350 int opcode = extract32(insn, 12, 4);
2351 bool is_store = !extract32(insn, 22, 1);
2352 bool is_postidx = extract32(insn, 23, 1);
2353 bool is_q = extract32(insn, 30, 1);
2354 TCGv_i64 tcg_addr, tcg_rn;
2356 int ebytes = 1 << size;
2357 int elements = (is_q ? 128 : 64) / (8 << size);
2358 int rpt; /* num iterations */
2359 int selem; /* structure elements */
2360 int r;
2362 if (extract32(insn, 31, 1) || extract32(insn, 21, 1)) {
2363 unallocated_encoding(s);
2364 return;
2367 /* From the shared decode logic */
2368 switch (opcode) {
2369 case 0x0:
2370 rpt = 1;
2371 selem = 4;
2372 break;
2373 case 0x2:
2374 rpt = 4;
2375 selem = 1;
2376 break;
2377 case 0x4:
2378 rpt = 1;
2379 selem = 3;
2380 break;
2381 case 0x6:
2382 rpt = 3;
2383 selem = 1;
2384 break;
2385 case 0x7:
2386 rpt = 1;
2387 selem = 1;
2388 break;
2389 case 0x8:
2390 rpt = 1;
2391 selem = 2;
2392 break;
2393 case 0xa:
2394 rpt = 2;
2395 selem = 1;
2396 break;
2397 default:
2398 unallocated_encoding(s);
2399 return;
2402 if (size == 3 && !is_q && selem != 1) {
2403 /* reserved */
2404 unallocated_encoding(s);
2405 return;
2408 if (!fp_access_check(s)) {
2409 return;
2412 if (rn == 31) {
2413 gen_check_sp_alignment(s);
2416 tcg_rn = cpu_reg_sp(s, rn);
2417 tcg_addr = tcg_temp_new_i64();
2418 tcg_gen_mov_i64(tcg_addr, tcg_rn);
2420 for (r = 0; r < rpt; r++) {
2421 int e;
2422 for (e = 0; e < elements; e++) {
2423 int tt = (rt + r) % 32;
2424 int xs;
2425 for (xs = 0; xs < selem; xs++) {
2426 if (is_store) {
2427 do_vec_st(s, tt, e, tcg_addr, size);
2428 } else {
2429 do_vec_ld(s, tt, e, tcg_addr, size);
2431 /* For non-quad operations, setting a slice of the low
2432 * 64 bits of the register clears the high 64 bits (in
2433 * the ARM ARM pseudocode this is implicit in the fact
2434 * that 'rval' is a 64 bit wide variable). We optimize
2435 * by noticing that we only need to do this the first
2436 * time we touch a register.
2438 if (!is_q && e == 0 && (r == 0 || xs == selem - 1)) {
2439 clear_vec_high(s, tt);
2442 tcg_gen_addi_i64(tcg_addr, tcg_addr, ebytes);
2443 tt = (tt + 1) % 32;
2448 if (is_postidx) {
2449 int rm = extract32(insn, 16, 5);
2450 if (rm == 31) {
2451 tcg_gen_mov_i64(tcg_rn, tcg_addr);
2452 } else {
2453 tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, rm));
2456 tcg_temp_free_i64(tcg_addr);
2459 /* C3.3.3 AdvSIMD load/store single structure
2461 * 31 30 29 23 22 21 20 16 15 13 12 11 10 9 5 4 0
2462 * +---+---+---------------+-----+-----------+-----+---+------+------+------+
2463 * | 0 | Q | 0 0 1 1 0 1 0 | L R | 0 0 0 0 0 | opc | S | size | Rn | Rt |
2464 * +---+---+---------------+-----+-----------+-----+---+------+------+------+
2466 * C3.3.4 AdvSIMD load/store single structure (post-indexed)
2468 * 31 30 29 23 22 21 20 16 15 13 12 11 10 9 5 4 0
2469 * +---+---+---------------+-----+-----------+-----+---+------+------+------+
2470 * | 0 | Q | 0 0 1 1 0 1 1 | L R | Rm | opc | S | size | Rn | Rt |
2471 * +---+---+---------------+-----+-----------+-----+---+------+------+------+
2473 * Rt: first (or only) SIMD&FP register to be transferred
2474 * Rn: base address or SP
2475 * Rm (post-index only): post-index register (when !31) or size dependent #imm
2476 * index = encoded in Q:S:size dependent on size
2478 * lane_size = encoded in R, opc
2479 * transfer width = encoded in opc, S, size
2481 static void disas_ldst_single_struct(DisasContext *s, uint32_t insn)
2483 int rt = extract32(insn, 0, 5);
2484 int rn = extract32(insn, 5, 5);
2485 int size = extract32(insn, 10, 2);
2486 int S = extract32(insn, 12, 1);
2487 int opc = extract32(insn, 13, 3);
2488 int R = extract32(insn, 21, 1);
2489 int is_load = extract32(insn, 22, 1);
2490 int is_postidx = extract32(insn, 23, 1);
2491 int is_q = extract32(insn, 30, 1);
2493 int scale = extract32(opc, 1, 2);
2494 int selem = (extract32(opc, 0, 1) << 1 | R) + 1;
2495 bool replicate = false;
2496 int index = is_q << 3 | S << 2 | size;
2497 int ebytes, xs;
2498 TCGv_i64 tcg_addr, tcg_rn;
2500 switch (scale) {
2501 case 3:
2502 if (!is_load || S) {
2503 unallocated_encoding(s);
2504 return;
2506 scale = size;
2507 replicate = true;
2508 break;
2509 case 0:
2510 break;
2511 case 1:
2512 if (extract32(size, 0, 1)) {
2513 unallocated_encoding(s);
2514 return;
2516 index >>= 1;
2517 break;
2518 case 2:
2519 if (extract32(size, 1, 1)) {
2520 unallocated_encoding(s);
2521 return;
2523 if (!extract32(size, 0, 1)) {
2524 index >>= 2;
2525 } else {
2526 if (S) {
2527 unallocated_encoding(s);
2528 return;
2530 index >>= 3;
2531 scale = 3;
2533 break;
2534 default:
2535 g_assert_not_reached();
2538 if (!fp_access_check(s)) {
2539 return;
2542 ebytes = 1 << scale;
2544 if (rn == 31) {
2545 gen_check_sp_alignment(s);
2548 tcg_rn = cpu_reg_sp(s, rn);
2549 tcg_addr = tcg_temp_new_i64();
2550 tcg_gen_mov_i64(tcg_addr, tcg_rn);
2552 for (xs = 0; xs < selem; xs++) {
2553 if (replicate) {
2554 /* Load and replicate to all elements */
2555 uint64_t mulconst;
2556 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
2558 tcg_gen_qemu_ld_i64(tcg_tmp, tcg_addr,
2559 get_mem_index(s), MO_TE + scale);
2560 switch (scale) {
2561 case 0:
2562 mulconst = 0x0101010101010101ULL;
2563 break;
2564 case 1:
2565 mulconst = 0x0001000100010001ULL;
2566 break;
2567 case 2:
2568 mulconst = 0x0000000100000001ULL;
2569 break;
2570 case 3:
2571 mulconst = 0;
2572 break;
2573 default:
2574 g_assert_not_reached();
2576 if (mulconst) {
2577 tcg_gen_muli_i64(tcg_tmp, tcg_tmp, mulconst);
2579 write_vec_element(s, tcg_tmp, rt, 0, MO_64);
2580 if (is_q) {
2581 write_vec_element(s, tcg_tmp, rt, 1, MO_64);
2582 } else {
2583 clear_vec_high(s, rt);
2585 tcg_temp_free_i64(tcg_tmp);
2586 } else {
2587 /* Load/store one element per register */
2588 if (is_load) {
2589 do_vec_ld(s, rt, index, tcg_addr, MO_TE + scale);
2590 } else {
2591 do_vec_st(s, rt, index, tcg_addr, MO_TE + scale);
2594 tcg_gen_addi_i64(tcg_addr, tcg_addr, ebytes);
2595 rt = (rt + 1) % 32;
2598 if (is_postidx) {
2599 int rm = extract32(insn, 16, 5);
2600 if (rm == 31) {
2601 tcg_gen_mov_i64(tcg_rn, tcg_addr);
2602 } else {
2603 tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, rm));
2606 tcg_temp_free_i64(tcg_addr);
2609 /* C3.3 Loads and stores */
2610 static void disas_ldst(DisasContext *s, uint32_t insn)
2612 switch (extract32(insn, 24, 6)) {
2613 case 0x08: /* Load/store exclusive */
2614 disas_ldst_excl(s, insn);
2615 break;
2616 case 0x18: case 0x1c: /* Load register (literal) */
2617 disas_ld_lit(s, insn);
2618 break;
2619 case 0x28: case 0x29:
2620 case 0x2c: case 0x2d: /* Load/store pair (all forms) */
2621 disas_ldst_pair(s, insn);
2622 break;
2623 case 0x38: case 0x39:
2624 case 0x3c: case 0x3d: /* Load/store register (all forms) */
2625 disas_ldst_reg(s, insn);
2626 break;
2627 case 0x0c: /* AdvSIMD load/store multiple structures */
2628 disas_ldst_multiple_struct(s, insn);
2629 break;
2630 case 0x0d: /* AdvSIMD load/store single structure */
2631 disas_ldst_single_struct(s, insn);
2632 break;
2633 default:
2634 unallocated_encoding(s);
2635 break;
2639 /* C3.4.6 PC-rel. addressing
2640 * 31 30 29 28 24 23 5 4 0
2641 * +----+-------+-----------+-------------------+------+
2642 * | op | immlo | 1 0 0 0 0 | immhi | Rd |
2643 * +----+-------+-----------+-------------------+------+
2645 static void disas_pc_rel_adr(DisasContext *s, uint32_t insn)
2647 unsigned int page, rd;
2648 uint64_t base;
2649 int64_t offset;
2651 page = extract32(insn, 31, 1);
2652 /* SignExtend(immhi:immlo) -> offset */
2653 offset = ((int64_t)sextract32(insn, 5, 19) << 2) | extract32(insn, 29, 2);
2654 rd = extract32(insn, 0, 5);
2655 base = s->pc - 4;
2657 if (page) {
2658 /* ADRP (page based) */
2659 base &= ~0xfff;
2660 offset <<= 12;
2663 tcg_gen_movi_i64(cpu_reg(s, rd), base + offset);
2667 * C3.4.1 Add/subtract (immediate)
2669 * 31 30 29 28 24 23 22 21 10 9 5 4 0
2670 * +--+--+--+-----------+-----+-------------+-----+-----+
2671 * |sf|op| S| 1 0 0 0 1 |shift| imm12 | Rn | Rd |
2672 * +--+--+--+-----------+-----+-------------+-----+-----+
2674 * sf: 0 -> 32bit, 1 -> 64bit
2675 * op: 0 -> add , 1 -> sub
2676 * S: 1 -> set flags
2677 * shift: 00 -> LSL imm by 0, 01 -> LSL imm by 12
2679 static void disas_add_sub_imm(DisasContext *s, uint32_t insn)
2681 int rd = extract32(insn, 0, 5);
2682 int rn = extract32(insn, 5, 5);
2683 uint64_t imm = extract32(insn, 10, 12);
2684 int shift = extract32(insn, 22, 2);
2685 bool setflags = extract32(insn, 29, 1);
2686 bool sub_op = extract32(insn, 30, 1);
2687 bool is_64bit = extract32(insn, 31, 1);
2689 TCGv_i64 tcg_rn = cpu_reg_sp(s, rn);
2690 TCGv_i64 tcg_rd = setflags ? cpu_reg(s, rd) : cpu_reg_sp(s, rd);
2691 TCGv_i64 tcg_result;
2693 switch (shift) {
2694 case 0x0:
2695 break;
2696 case 0x1:
2697 imm <<= 12;
2698 break;
2699 default:
2700 unallocated_encoding(s);
2701 return;
2704 tcg_result = tcg_temp_new_i64();
2705 if (!setflags) {
2706 if (sub_op) {
2707 tcg_gen_subi_i64(tcg_result, tcg_rn, imm);
2708 } else {
2709 tcg_gen_addi_i64(tcg_result, tcg_rn, imm);
2711 } else {
2712 TCGv_i64 tcg_imm = tcg_const_i64(imm);
2713 if (sub_op) {
2714 gen_sub_CC(is_64bit, tcg_result, tcg_rn, tcg_imm);
2715 } else {
2716 gen_add_CC(is_64bit, tcg_result, tcg_rn, tcg_imm);
2718 tcg_temp_free_i64(tcg_imm);
2721 if (is_64bit) {
2722 tcg_gen_mov_i64(tcg_rd, tcg_result);
2723 } else {
2724 tcg_gen_ext32u_i64(tcg_rd, tcg_result);
2727 tcg_temp_free_i64(tcg_result);
2730 /* The input should be a value in the bottom e bits (with higher
2731 * bits zero); returns that value replicated into every element
2732 * of size e in a 64 bit integer.
2734 static uint64_t bitfield_replicate(uint64_t mask, unsigned int e)
2736 assert(e != 0);
2737 while (e < 64) {
2738 mask |= mask << e;
2739 e *= 2;
2741 return mask;
2744 /* Return a value with the bottom len bits set (where 0 < len <= 64) */
2745 static inline uint64_t bitmask64(unsigned int length)
2747 assert(length > 0 && length <= 64);
2748 return ~0ULL >> (64 - length);
2751 /* Simplified variant of pseudocode DecodeBitMasks() for the case where we
2752 * only require the wmask. Returns false if the imms/immr/immn are a reserved
2753 * value (ie should cause a guest UNDEF exception), and true if they are
2754 * valid, in which case the decoded bit pattern is written to result.
2756 static bool logic_imm_decode_wmask(uint64_t *result, unsigned int immn,
2757 unsigned int imms, unsigned int immr)
2759 uint64_t mask;
2760 unsigned e, levels, s, r;
2761 int len;
2763 assert(immn < 2 && imms < 64 && immr < 64);
2765 /* The bit patterns we create here are 64 bit patterns which
2766 * are vectors of identical elements of size e = 2, 4, 8, 16, 32 or
2767 * 64 bits each. Each element contains the same value: a run
2768 * of between 1 and e-1 non-zero bits, rotated within the
2769 * element by between 0 and e-1 bits.
2771 * The element size and run length are encoded into immn (1 bit)
2772 * and imms (6 bits) as follows:
2773 * 64 bit elements: immn = 1, imms = <length of run - 1>
2774 * 32 bit elements: immn = 0, imms = 0 : <length of run - 1>
2775 * 16 bit elements: immn = 0, imms = 10 : <length of run - 1>
2776 * 8 bit elements: immn = 0, imms = 110 : <length of run - 1>
2777 * 4 bit elements: immn = 0, imms = 1110 : <length of run - 1>
2778 * 2 bit elements: immn = 0, imms = 11110 : <length of run - 1>
2779 * Notice that immn = 0, imms = 11111x is the only combination
2780 * not covered by one of the above options; this is reserved.
2781 * Further, <length of run - 1> all-ones is a reserved pattern.
2783 * In all cases the rotation is by immr % e (and immr is 6 bits).
2786 /* First determine the element size */
2787 len = 31 - clz32((immn << 6) | (~imms & 0x3f));
2788 if (len < 1) {
2789 /* This is the immn == 0, imms == 0x11111x case */
2790 return false;
2792 e = 1 << len;
2794 levels = e - 1;
2795 s = imms & levels;
2796 r = immr & levels;
2798 if (s == levels) {
2799 /* <length of run - 1> mustn't be all-ones. */
2800 return false;
2803 /* Create the value of one element: s+1 set bits rotated
2804 * by r within the element (which is e bits wide)...
2806 mask = bitmask64(s + 1);
2807 mask = (mask >> r) | (mask << (e - r));
2808 /* ...then replicate the element over the whole 64 bit value */
2809 mask = bitfield_replicate(mask, e);
2810 *result = mask;
2811 return true;
2814 /* C3.4.4 Logical (immediate)
2815 * 31 30 29 28 23 22 21 16 15 10 9 5 4 0
2816 * +----+-----+-------------+---+------+------+------+------+
2817 * | sf | opc | 1 0 0 1 0 0 | N | immr | imms | Rn | Rd |
2818 * +----+-----+-------------+---+------+------+------+------+
2820 static void disas_logic_imm(DisasContext *s, uint32_t insn)
2822 unsigned int sf, opc, is_n, immr, imms, rn, rd;
2823 TCGv_i64 tcg_rd, tcg_rn;
2824 uint64_t wmask;
2825 bool is_and = false;
2827 sf = extract32(insn, 31, 1);
2828 opc = extract32(insn, 29, 2);
2829 is_n = extract32(insn, 22, 1);
2830 immr = extract32(insn, 16, 6);
2831 imms = extract32(insn, 10, 6);
2832 rn = extract32(insn, 5, 5);
2833 rd = extract32(insn, 0, 5);
2835 if (!sf && is_n) {
2836 unallocated_encoding(s);
2837 return;
2840 if (opc == 0x3) { /* ANDS */
2841 tcg_rd = cpu_reg(s, rd);
2842 } else {
2843 tcg_rd = cpu_reg_sp(s, rd);
2845 tcg_rn = cpu_reg(s, rn);
2847 if (!logic_imm_decode_wmask(&wmask, is_n, imms, immr)) {
2848 /* some immediate field values are reserved */
2849 unallocated_encoding(s);
2850 return;
2853 if (!sf) {
2854 wmask &= 0xffffffff;
2857 switch (opc) {
2858 case 0x3: /* ANDS */
2859 case 0x0: /* AND */
2860 tcg_gen_andi_i64(tcg_rd, tcg_rn, wmask);
2861 is_and = true;
2862 break;
2863 case 0x1: /* ORR */
2864 tcg_gen_ori_i64(tcg_rd, tcg_rn, wmask);
2865 break;
2866 case 0x2: /* EOR */
2867 tcg_gen_xori_i64(tcg_rd, tcg_rn, wmask);
2868 break;
2869 default:
2870 assert(FALSE); /* must handle all above */
2871 break;
2874 if (!sf && !is_and) {
2875 /* zero extend final result; we know we can skip this for AND
2876 * since the immediate had the high 32 bits clear.
2878 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
2881 if (opc == 3) { /* ANDS */
2882 gen_logic_CC(sf, tcg_rd);
2887 * C3.4.5 Move wide (immediate)
2889 * 31 30 29 28 23 22 21 20 5 4 0
2890 * +--+-----+-------------+-----+----------------+------+
2891 * |sf| opc | 1 0 0 1 0 1 | hw | imm16 | Rd |
2892 * +--+-----+-------------+-----+----------------+------+
2894 * sf: 0 -> 32 bit, 1 -> 64 bit
2895 * opc: 00 -> N, 10 -> Z, 11 -> K
2896 * hw: shift/16 (0,16, and sf only 32, 48)
2898 static void disas_movw_imm(DisasContext *s, uint32_t insn)
2900 int rd = extract32(insn, 0, 5);
2901 uint64_t imm = extract32(insn, 5, 16);
2902 int sf = extract32(insn, 31, 1);
2903 int opc = extract32(insn, 29, 2);
2904 int pos = extract32(insn, 21, 2) << 4;
2905 TCGv_i64 tcg_rd = cpu_reg(s, rd);
2906 TCGv_i64 tcg_imm;
2908 if (!sf && (pos >= 32)) {
2909 unallocated_encoding(s);
2910 return;
2913 switch (opc) {
2914 case 0: /* MOVN */
2915 case 2: /* MOVZ */
2916 imm <<= pos;
2917 if (opc == 0) {
2918 imm = ~imm;
2920 if (!sf) {
2921 imm &= 0xffffffffu;
2923 tcg_gen_movi_i64(tcg_rd, imm);
2924 break;
2925 case 3: /* MOVK */
2926 tcg_imm = tcg_const_i64(imm);
2927 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_imm, pos, 16);
2928 tcg_temp_free_i64(tcg_imm);
2929 if (!sf) {
2930 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
2932 break;
2933 default:
2934 unallocated_encoding(s);
2935 break;
2939 /* C3.4.2 Bitfield
2940 * 31 30 29 28 23 22 21 16 15 10 9 5 4 0
2941 * +----+-----+-------------+---+------+------+------+------+
2942 * | sf | opc | 1 0 0 1 1 0 | N | immr | imms | Rn | Rd |
2943 * +----+-----+-------------+---+------+------+------+------+
2945 static void disas_bitfield(DisasContext *s, uint32_t insn)
2947 unsigned int sf, n, opc, ri, si, rn, rd, bitsize, pos, len;
2948 TCGv_i64 tcg_rd, tcg_tmp;
2950 sf = extract32(insn, 31, 1);
2951 opc = extract32(insn, 29, 2);
2952 n = extract32(insn, 22, 1);
2953 ri = extract32(insn, 16, 6);
2954 si = extract32(insn, 10, 6);
2955 rn = extract32(insn, 5, 5);
2956 rd = extract32(insn, 0, 5);
2957 bitsize = sf ? 64 : 32;
2959 if (sf != n || ri >= bitsize || si >= bitsize || opc > 2) {
2960 unallocated_encoding(s);
2961 return;
2964 tcg_rd = cpu_reg(s, rd);
2965 tcg_tmp = read_cpu_reg(s, rn, sf);
2967 /* OPTME: probably worth recognizing common cases of ext{8,16,32}{u,s} */
2969 if (opc != 1) { /* SBFM or UBFM */
2970 tcg_gen_movi_i64(tcg_rd, 0);
2973 /* do the bit move operation */
2974 if (si >= ri) {
2975 /* Wd<s-r:0> = Wn<s:r> */
2976 tcg_gen_shri_i64(tcg_tmp, tcg_tmp, ri);
2977 pos = 0;
2978 len = (si - ri) + 1;
2979 } else {
2980 /* Wd<32+s-r,32-r> = Wn<s:0> */
2981 pos = bitsize - ri;
2982 len = si + 1;
2985 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, pos, len);
2987 if (opc == 0) { /* SBFM - sign extend the destination field */
2988 tcg_gen_shli_i64(tcg_rd, tcg_rd, 64 - (pos + len));
2989 tcg_gen_sari_i64(tcg_rd, tcg_rd, 64 - (pos + len));
2992 if (!sf) { /* zero extend final result */
2993 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
2997 /* C3.4.3 Extract
2998 * 31 30 29 28 23 22 21 20 16 15 10 9 5 4 0
2999 * +----+------+-------------+---+----+------+--------+------+------+
3000 * | sf | op21 | 1 0 0 1 1 1 | N | o0 | Rm | imms | Rn | Rd |
3001 * +----+------+-------------+---+----+------+--------+------+------+
3003 static void disas_extract(DisasContext *s, uint32_t insn)
3005 unsigned int sf, n, rm, imm, rn, rd, bitsize, op21, op0;
3007 sf = extract32(insn, 31, 1);
3008 n = extract32(insn, 22, 1);
3009 rm = extract32(insn, 16, 5);
3010 imm = extract32(insn, 10, 6);
3011 rn = extract32(insn, 5, 5);
3012 rd = extract32(insn, 0, 5);
3013 op21 = extract32(insn, 29, 2);
3014 op0 = extract32(insn, 21, 1);
3015 bitsize = sf ? 64 : 32;
3017 if (sf != n || op21 || op0 || imm >= bitsize) {
3018 unallocated_encoding(s);
3019 } else {
3020 TCGv_i64 tcg_rd, tcg_rm, tcg_rn;
3022 tcg_rd = cpu_reg(s, rd);
3024 if (imm) {
3025 /* OPTME: we can special case rm==rn as a rotate */
3026 tcg_rm = read_cpu_reg(s, rm, sf);
3027 tcg_rn = read_cpu_reg(s, rn, sf);
3028 tcg_gen_shri_i64(tcg_rm, tcg_rm, imm);
3029 tcg_gen_shli_i64(tcg_rn, tcg_rn, bitsize - imm);
3030 tcg_gen_or_i64(tcg_rd, tcg_rm, tcg_rn);
3031 if (!sf) {
3032 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3034 } else {
3035 /* tcg shl_i32/shl_i64 is undefined for 32/64 bit shifts,
3036 * so an extract from bit 0 is a special case.
3038 if (sf) {
3039 tcg_gen_mov_i64(tcg_rd, cpu_reg(s, rm));
3040 } else {
3041 tcg_gen_ext32u_i64(tcg_rd, cpu_reg(s, rm));
3048 /* C3.4 Data processing - immediate */
3049 static void disas_data_proc_imm(DisasContext *s, uint32_t insn)
3051 switch (extract32(insn, 23, 6)) {
3052 case 0x20: case 0x21: /* PC-rel. addressing */
3053 disas_pc_rel_adr(s, insn);
3054 break;
3055 case 0x22: case 0x23: /* Add/subtract (immediate) */
3056 disas_add_sub_imm(s, insn);
3057 break;
3058 case 0x24: /* Logical (immediate) */
3059 disas_logic_imm(s, insn);
3060 break;
3061 case 0x25: /* Move wide (immediate) */
3062 disas_movw_imm(s, insn);
3063 break;
3064 case 0x26: /* Bitfield */
3065 disas_bitfield(s, insn);
3066 break;
3067 case 0x27: /* Extract */
3068 disas_extract(s, insn);
3069 break;
3070 default:
3071 unallocated_encoding(s);
3072 break;
3076 /* Shift a TCGv src by TCGv shift_amount, put result in dst.
3077 * Note that it is the caller's responsibility to ensure that the
3078 * shift amount is in range (ie 0..31 or 0..63) and provide the ARM
3079 * mandated semantics for out of range shifts.
3081 static void shift_reg(TCGv_i64 dst, TCGv_i64 src, int sf,
3082 enum a64_shift_type shift_type, TCGv_i64 shift_amount)
3084 switch (shift_type) {
3085 case A64_SHIFT_TYPE_LSL:
3086 tcg_gen_shl_i64(dst, src, shift_amount);
3087 break;
3088 case A64_SHIFT_TYPE_LSR:
3089 tcg_gen_shr_i64(dst, src, shift_amount);
3090 break;
3091 case A64_SHIFT_TYPE_ASR:
3092 if (!sf) {
3093 tcg_gen_ext32s_i64(dst, src);
3095 tcg_gen_sar_i64(dst, sf ? src : dst, shift_amount);
3096 break;
3097 case A64_SHIFT_TYPE_ROR:
3098 if (sf) {
3099 tcg_gen_rotr_i64(dst, src, shift_amount);
3100 } else {
3101 TCGv_i32 t0, t1;
3102 t0 = tcg_temp_new_i32();
3103 t1 = tcg_temp_new_i32();
3104 tcg_gen_trunc_i64_i32(t0, src);
3105 tcg_gen_trunc_i64_i32(t1, shift_amount);
3106 tcg_gen_rotr_i32(t0, t0, t1);
3107 tcg_gen_extu_i32_i64(dst, t0);
3108 tcg_temp_free_i32(t0);
3109 tcg_temp_free_i32(t1);
3111 break;
3112 default:
3113 assert(FALSE); /* all shift types should be handled */
3114 break;
3117 if (!sf) { /* zero extend final result */
3118 tcg_gen_ext32u_i64(dst, dst);
3122 /* Shift a TCGv src by immediate, put result in dst.
3123 * The shift amount must be in range (this should always be true as the
3124 * relevant instructions will UNDEF on bad shift immediates).
3126 static void shift_reg_imm(TCGv_i64 dst, TCGv_i64 src, int sf,
3127 enum a64_shift_type shift_type, unsigned int shift_i)
3129 assert(shift_i < (sf ? 64 : 32));
3131 if (shift_i == 0) {
3132 tcg_gen_mov_i64(dst, src);
3133 } else {
3134 TCGv_i64 shift_const;
3136 shift_const = tcg_const_i64(shift_i);
3137 shift_reg(dst, src, sf, shift_type, shift_const);
3138 tcg_temp_free_i64(shift_const);
3142 /* C3.5.10 Logical (shifted register)
3143 * 31 30 29 28 24 23 22 21 20 16 15 10 9 5 4 0
3144 * +----+-----+-----------+-------+---+------+--------+------+------+
3145 * | sf | opc | 0 1 0 1 0 | shift | N | Rm | imm6 | Rn | Rd |
3146 * +----+-----+-----------+-------+---+------+--------+------+------+
3148 static void disas_logic_reg(DisasContext *s, uint32_t insn)
3150 TCGv_i64 tcg_rd, tcg_rn, tcg_rm;
3151 unsigned int sf, opc, shift_type, invert, rm, shift_amount, rn, rd;
3153 sf = extract32(insn, 31, 1);
3154 opc = extract32(insn, 29, 2);
3155 shift_type = extract32(insn, 22, 2);
3156 invert = extract32(insn, 21, 1);
3157 rm = extract32(insn, 16, 5);
3158 shift_amount = extract32(insn, 10, 6);
3159 rn = extract32(insn, 5, 5);
3160 rd = extract32(insn, 0, 5);
3162 if (!sf && (shift_amount & (1 << 5))) {
3163 unallocated_encoding(s);
3164 return;
3167 tcg_rd = cpu_reg(s, rd);
3169 if (opc == 1 && shift_amount == 0 && shift_type == 0 && rn == 31) {
3170 /* Unshifted ORR and ORN with WZR/XZR is the standard encoding for
3171 * register-register MOV and MVN, so it is worth special casing.
3173 tcg_rm = cpu_reg(s, rm);
3174 if (invert) {
3175 tcg_gen_not_i64(tcg_rd, tcg_rm);
3176 if (!sf) {
3177 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3179 } else {
3180 if (sf) {
3181 tcg_gen_mov_i64(tcg_rd, tcg_rm);
3182 } else {
3183 tcg_gen_ext32u_i64(tcg_rd, tcg_rm);
3186 return;
3189 tcg_rm = read_cpu_reg(s, rm, sf);
3191 if (shift_amount) {
3192 shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, shift_amount);
3195 tcg_rn = cpu_reg(s, rn);
3197 switch (opc | (invert << 2)) {
3198 case 0: /* AND */
3199 case 3: /* ANDS */
3200 tcg_gen_and_i64(tcg_rd, tcg_rn, tcg_rm);
3201 break;
3202 case 1: /* ORR */
3203 tcg_gen_or_i64(tcg_rd, tcg_rn, tcg_rm);
3204 break;
3205 case 2: /* EOR */
3206 tcg_gen_xor_i64(tcg_rd, tcg_rn, tcg_rm);
3207 break;
3208 case 4: /* BIC */
3209 case 7: /* BICS */
3210 tcg_gen_andc_i64(tcg_rd, tcg_rn, tcg_rm);
3211 break;
3212 case 5: /* ORN */
3213 tcg_gen_orc_i64(tcg_rd, tcg_rn, tcg_rm);
3214 break;
3215 case 6: /* EON */
3216 tcg_gen_eqv_i64(tcg_rd, tcg_rn, tcg_rm);
3217 break;
3218 default:
3219 assert(FALSE);
3220 break;
3223 if (!sf) {
3224 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3227 if (opc == 3) {
3228 gen_logic_CC(sf, tcg_rd);
3233 * C3.5.1 Add/subtract (extended register)
3235 * 31|30|29|28 24|23 22|21|20 16|15 13|12 10|9 5|4 0|
3236 * +--+--+--+-----------+-----+--+-------+------+------+----+----+
3237 * |sf|op| S| 0 1 0 1 1 | opt | 1| Rm |option| imm3 | Rn | Rd |
3238 * +--+--+--+-----------+-----+--+-------+------+------+----+----+
3240 * sf: 0 -> 32bit, 1 -> 64bit
3241 * op: 0 -> add , 1 -> sub
3242 * S: 1 -> set flags
3243 * opt: 00
3244 * option: extension type (see DecodeRegExtend)
3245 * imm3: optional shift to Rm
3247 * Rd = Rn + LSL(extend(Rm), amount)
3249 static void disas_add_sub_ext_reg(DisasContext *s, uint32_t insn)
3251 int rd = extract32(insn, 0, 5);
3252 int rn = extract32(insn, 5, 5);
3253 int imm3 = extract32(insn, 10, 3);
3254 int option = extract32(insn, 13, 3);
3255 int rm = extract32(insn, 16, 5);
3256 bool setflags = extract32(insn, 29, 1);
3257 bool sub_op = extract32(insn, 30, 1);
3258 bool sf = extract32(insn, 31, 1);
3260 TCGv_i64 tcg_rm, tcg_rn; /* temps */
3261 TCGv_i64 tcg_rd;
3262 TCGv_i64 tcg_result;
3264 if (imm3 > 4) {
3265 unallocated_encoding(s);
3266 return;
3269 /* non-flag setting ops may use SP */
3270 if (!setflags) {
3271 tcg_rd = cpu_reg_sp(s, rd);
3272 } else {
3273 tcg_rd = cpu_reg(s, rd);
3275 tcg_rn = read_cpu_reg_sp(s, rn, sf);
3277 tcg_rm = read_cpu_reg(s, rm, sf);
3278 ext_and_shift_reg(tcg_rm, tcg_rm, option, imm3);
3280 tcg_result = tcg_temp_new_i64();
3282 if (!setflags) {
3283 if (sub_op) {
3284 tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
3285 } else {
3286 tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
3288 } else {
3289 if (sub_op) {
3290 gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
3291 } else {
3292 gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
3296 if (sf) {
3297 tcg_gen_mov_i64(tcg_rd, tcg_result);
3298 } else {
3299 tcg_gen_ext32u_i64(tcg_rd, tcg_result);
3302 tcg_temp_free_i64(tcg_result);
3306 * C3.5.2 Add/subtract (shifted register)
3308 * 31 30 29 28 24 23 22 21 20 16 15 10 9 5 4 0
3309 * +--+--+--+-----------+-----+--+-------+---------+------+------+
3310 * |sf|op| S| 0 1 0 1 1 |shift| 0| Rm | imm6 | Rn | Rd |
3311 * +--+--+--+-----------+-----+--+-------+---------+------+------+
3313 * sf: 0 -> 32bit, 1 -> 64bit
3314 * op: 0 -> add , 1 -> sub
3315 * S: 1 -> set flags
3316 * shift: 00 -> LSL, 01 -> LSR, 10 -> ASR, 11 -> RESERVED
3317 * imm6: Shift amount to apply to Rm before the add/sub
3319 static void disas_add_sub_reg(DisasContext *s, uint32_t insn)
3321 int rd = extract32(insn, 0, 5);
3322 int rn = extract32(insn, 5, 5);
3323 int imm6 = extract32(insn, 10, 6);
3324 int rm = extract32(insn, 16, 5);
3325 int shift_type = extract32(insn, 22, 2);
3326 bool setflags = extract32(insn, 29, 1);
3327 bool sub_op = extract32(insn, 30, 1);
3328 bool sf = extract32(insn, 31, 1);
3330 TCGv_i64 tcg_rd = cpu_reg(s, rd);
3331 TCGv_i64 tcg_rn, tcg_rm;
3332 TCGv_i64 tcg_result;
3334 if ((shift_type == 3) || (!sf && (imm6 > 31))) {
3335 unallocated_encoding(s);
3336 return;
3339 tcg_rn = read_cpu_reg(s, rn, sf);
3340 tcg_rm = read_cpu_reg(s, rm, sf);
3342 shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, imm6);
3344 tcg_result = tcg_temp_new_i64();
3346 if (!setflags) {
3347 if (sub_op) {
3348 tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
3349 } else {
3350 tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
3352 } else {
3353 if (sub_op) {
3354 gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
3355 } else {
3356 gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
3360 if (sf) {
3361 tcg_gen_mov_i64(tcg_rd, tcg_result);
3362 } else {
3363 tcg_gen_ext32u_i64(tcg_rd, tcg_result);
3366 tcg_temp_free_i64(tcg_result);
3369 /* C3.5.9 Data-processing (3 source)
3371 31 30 29 28 24 23 21 20 16 15 14 10 9 5 4 0
3372 +--+------+-----------+------+------+----+------+------+------+
3373 |sf| op54 | 1 1 0 1 1 | op31 | Rm | o0 | Ra | Rn | Rd |
3374 +--+------+-----------+------+------+----+------+------+------+
3377 static void disas_data_proc_3src(DisasContext *s, uint32_t insn)
3379 int rd = extract32(insn, 0, 5);
3380 int rn = extract32(insn, 5, 5);
3381 int ra = extract32(insn, 10, 5);
3382 int rm = extract32(insn, 16, 5);
3383 int op_id = (extract32(insn, 29, 3) << 4) |
3384 (extract32(insn, 21, 3) << 1) |
3385 extract32(insn, 15, 1);
3386 bool sf = extract32(insn, 31, 1);
3387 bool is_sub = extract32(op_id, 0, 1);
3388 bool is_high = extract32(op_id, 2, 1);
3389 bool is_signed = false;
3390 TCGv_i64 tcg_op1;
3391 TCGv_i64 tcg_op2;
3392 TCGv_i64 tcg_tmp;
3394 /* Note that op_id is sf:op54:op31:o0 so it includes the 32/64 size flag */
3395 switch (op_id) {
3396 case 0x42: /* SMADDL */
3397 case 0x43: /* SMSUBL */
3398 case 0x44: /* SMULH */
3399 is_signed = true;
3400 break;
3401 case 0x0: /* MADD (32bit) */
3402 case 0x1: /* MSUB (32bit) */
3403 case 0x40: /* MADD (64bit) */
3404 case 0x41: /* MSUB (64bit) */
3405 case 0x4a: /* UMADDL */
3406 case 0x4b: /* UMSUBL */
3407 case 0x4c: /* UMULH */
3408 break;
3409 default:
3410 unallocated_encoding(s);
3411 return;
3414 if (is_high) {
3415 TCGv_i64 low_bits = tcg_temp_new_i64(); /* low bits discarded */
3416 TCGv_i64 tcg_rd = cpu_reg(s, rd);
3417 TCGv_i64 tcg_rn = cpu_reg(s, rn);
3418 TCGv_i64 tcg_rm = cpu_reg(s, rm);
3420 if (is_signed) {
3421 tcg_gen_muls2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
3422 } else {
3423 tcg_gen_mulu2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
3426 tcg_temp_free_i64(low_bits);
3427 return;
3430 tcg_op1 = tcg_temp_new_i64();
3431 tcg_op2 = tcg_temp_new_i64();
3432 tcg_tmp = tcg_temp_new_i64();
3434 if (op_id < 0x42) {
3435 tcg_gen_mov_i64(tcg_op1, cpu_reg(s, rn));
3436 tcg_gen_mov_i64(tcg_op2, cpu_reg(s, rm));
3437 } else {
3438 if (is_signed) {
3439 tcg_gen_ext32s_i64(tcg_op1, cpu_reg(s, rn));
3440 tcg_gen_ext32s_i64(tcg_op2, cpu_reg(s, rm));
3441 } else {
3442 tcg_gen_ext32u_i64(tcg_op1, cpu_reg(s, rn));
3443 tcg_gen_ext32u_i64(tcg_op2, cpu_reg(s, rm));
3447 if (ra == 31 && !is_sub) {
3448 /* Special-case MADD with rA == XZR; it is the standard MUL alias */
3449 tcg_gen_mul_i64(cpu_reg(s, rd), tcg_op1, tcg_op2);
3450 } else {
3451 tcg_gen_mul_i64(tcg_tmp, tcg_op1, tcg_op2);
3452 if (is_sub) {
3453 tcg_gen_sub_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
3454 } else {
3455 tcg_gen_add_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
3459 if (!sf) {
3460 tcg_gen_ext32u_i64(cpu_reg(s, rd), cpu_reg(s, rd));
3463 tcg_temp_free_i64(tcg_op1);
3464 tcg_temp_free_i64(tcg_op2);
3465 tcg_temp_free_i64(tcg_tmp);
3468 /* C3.5.3 - Add/subtract (with carry)
3469 * 31 30 29 28 27 26 25 24 23 22 21 20 16 15 10 9 5 4 0
3470 * +--+--+--+------------------------+------+---------+------+-----+
3471 * |sf|op| S| 1 1 0 1 0 0 0 0 | rm | opcode2 | Rn | Rd |
3472 * +--+--+--+------------------------+------+---------+------+-----+
3473 * [000000]
3476 static void disas_adc_sbc(DisasContext *s, uint32_t insn)
3478 unsigned int sf, op, setflags, rm, rn, rd;
3479 TCGv_i64 tcg_y, tcg_rn, tcg_rd;
3481 if (extract32(insn, 10, 6) != 0) {
3482 unallocated_encoding(s);
3483 return;
3486 sf = extract32(insn, 31, 1);
3487 op = extract32(insn, 30, 1);
3488 setflags = extract32(insn, 29, 1);
3489 rm = extract32(insn, 16, 5);
3490 rn = extract32(insn, 5, 5);
3491 rd = extract32(insn, 0, 5);
3493 tcg_rd = cpu_reg(s, rd);
3494 tcg_rn = cpu_reg(s, rn);
3496 if (op) {
3497 tcg_y = new_tmp_a64(s);
3498 tcg_gen_not_i64(tcg_y, cpu_reg(s, rm));
3499 } else {
3500 tcg_y = cpu_reg(s, rm);
3503 if (setflags) {
3504 gen_adc_CC(sf, tcg_rd, tcg_rn, tcg_y);
3505 } else {
3506 gen_adc(sf, tcg_rd, tcg_rn, tcg_y);
3510 /* C3.5.4 - C3.5.5 Conditional compare (immediate / register)
3511 * 31 30 29 28 27 26 25 24 23 22 21 20 16 15 12 11 10 9 5 4 3 0
3512 * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
3513 * |sf|op| S| 1 1 0 1 0 0 1 0 |imm5/rm | cond |i/r |o2| Rn |o3|nzcv |
3514 * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
3515 * [1] y [0] [0]
3517 static void disas_cc(DisasContext *s, uint32_t insn)
3519 unsigned int sf, op, y, cond, rn, nzcv, is_imm;
3520 int label_continue = -1;
3521 TCGv_i64 tcg_tmp, tcg_y, tcg_rn;
3523 if (!extract32(insn, 29, 1)) {
3524 unallocated_encoding(s);
3525 return;
3527 if (insn & (1 << 10 | 1 << 4)) {
3528 unallocated_encoding(s);
3529 return;
3531 sf = extract32(insn, 31, 1);
3532 op = extract32(insn, 30, 1);
3533 is_imm = extract32(insn, 11, 1);
3534 y = extract32(insn, 16, 5); /* y = rm (reg) or imm5 (imm) */
3535 cond = extract32(insn, 12, 4);
3536 rn = extract32(insn, 5, 5);
3537 nzcv = extract32(insn, 0, 4);
3539 if (cond < 0x0e) { /* not always */
3540 int label_match = gen_new_label();
3541 label_continue = gen_new_label();
3542 arm_gen_test_cc(cond, label_match);
3543 /* nomatch: */
3544 tcg_tmp = tcg_temp_new_i64();
3545 tcg_gen_movi_i64(tcg_tmp, nzcv << 28);
3546 gen_set_nzcv(tcg_tmp);
3547 tcg_temp_free_i64(tcg_tmp);
3548 tcg_gen_br(label_continue);
3549 gen_set_label(label_match);
3551 /* match, or condition is always */
3552 if (is_imm) {
3553 tcg_y = new_tmp_a64(s);
3554 tcg_gen_movi_i64(tcg_y, y);
3555 } else {
3556 tcg_y = cpu_reg(s, y);
3558 tcg_rn = cpu_reg(s, rn);
3560 tcg_tmp = tcg_temp_new_i64();
3561 if (op) {
3562 gen_sub_CC(sf, tcg_tmp, tcg_rn, tcg_y);
3563 } else {
3564 gen_add_CC(sf, tcg_tmp, tcg_rn, tcg_y);
3566 tcg_temp_free_i64(tcg_tmp);
3568 if (cond < 0x0e) { /* continue */
3569 gen_set_label(label_continue);
3573 /* C3.5.6 Conditional select
3574 * 31 30 29 28 21 20 16 15 12 11 10 9 5 4 0
3575 * +----+----+---+-----------------+------+------+-----+------+------+
3576 * | sf | op | S | 1 1 0 1 0 1 0 0 | Rm | cond | op2 | Rn | Rd |
3577 * +----+----+---+-----------------+------+------+-----+------+------+
3579 static void disas_cond_select(DisasContext *s, uint32_t insn)
3581 unsigned int sf, else_inv, rm, cond, else_inc, rn, rd;
3582 TCGv_i64 tcg_rd, tcg_src;
3584 if (extract32(insn, 29, 1) || extract32(insn, 11, 1)) {
3585 /* S == 1 or op2<1> == 1 */
3586 unallocated_encoding(s);
3587 return;
3589 sf = extract32(insn, 31, 1);
3590 else_inv = extract32(insn, 30, 1);
3591 rm = extract32(insn, 16, 5);
3592 cond = extract32(insn, 12, 4);
3593 else_inc = extract32(insn, 10, 1);
3594 rn = extract32(insn, 5, 5);
3595 rd = extract32(insn, 0, 5);
3597 if (rd == 31) {
3598 /* silly no-op write; until we use movcond we must special-case
3599 * this to avoid a dead temporary across basic blocks.
3601 return;
3604 tcg_rd = cpu_reg(s, rd);
3606 if (cond >= 0x0e) { /* condition "always" */
3607 tcg_src = read_cpu_reg(s, rn, sf);
3608 tcg_gen_mov_i64(tcg_rd, tcg_src);
3609 } else {
3610 /* OPTME: we could use movcond here, at the cost of duplicating
3611 * a lot of the arm_gen_test_cc() logic.
3613 int label_match = gen_new_label();
3614 int label_continue = gen_new_label();
3616 arm_gen_test_cc(cond, label_match);
3617 /* nomatch: */
3618 tcg_src = cpu_reg(s, rm);
3620 if (else_inv && else_inc) {
3621 tcg_gen_neg_i64(tcg_rd, tcg_src);
3622 } else if (else_inv) {
3623 tcg_gen_not_i64(tcg_rd, tcg_src);
3624 } else if (else_inc) {
3625 tcg_gen_addi_i64(tcg_rd, tcg_src, 1);
3626 } else {
3627 tcg_gen_mov_i64(tcg_rd, tcg_src);
3629 if (!sf) {
3630 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3632 tcg_gen_br(label_continue);
3633 /* match: */
3634 gen_set_label(label_match);
3635 tcg_src = read_cpu_reg(s, rn, sf);
3636 tcg_gen_mov_i64(tcg_rd, tcg_src);
3637 /* continue: */
3638 gen_set_label(label_continue);
3642 static void handle_clz(DisasContext *s, unsigned int sf,
3643 unsigned int rn, unsigned int rd)
3645 TCGv_i64 tcg_rd, tcg_rn;
3646 tcg_rd = cpu_reg(s, rd);
3647 tcg_rn = cpu_reg(s, rn);
3649 if (sf) {
3650 gen_helper_clz64(tcg_rd, tcg_rn);
3651 } else {
3652 TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
3653 tcg_gen_trunc_i64_i32(tcg_tmp32, tcg_rn);
3654 gen_helper_clz(tcg_tmp32, tcg_tmp32);
3655 tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
3656 tcg_temp_free_i32(tcg_tmp32);
3660 static void handle_cls(DisasContext *s, unsigned int sf,
3661 unsigned int rn, unsigned int rd)
3663 TCGv_i64 tcg_rd, tcg_rn;
3664 tcg_rd = cpu_reg(s, rd);
3665 tcg_rn = cpu_reg(s, rn);
3667 if (sf) {
3668 gen_helper_cls64(tcg_rd, tcg_rn);
3669 } else {
3670 TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
3671 tcg_gen_trunc_i64_i32(tcg_tmp32, tcg_rn);
3672 gen_helper_cls32(tcg_tmp32, tcg_tmp32);
3673 tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
3674 tcg_temp_free_i32(tcg_tmp32);
3678 static void handle_rbit(DisasContext *s, unsigned int sf,
3679 unsigned int rn, unsigned int rd)
3681 TCGv_i64 tcg_rd, tcg_rn;
3682 tcg_rd = cpu_reg(s, rd);
3683 tcg_rn = cpu_reg(s, rn);
3685 if (sf) {
3686 gen_helper_rbit64(tcg_rd, tcg_rn);
3687 } else {
3688 TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
3689 tcg_gen_trunc_i64_i32(tcg_tmp32, tcg_rn);
3690 gen_helper_rbit(tcg_tmp32, tcg_tmp32);
3691 tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
3692 tcg_temp_free_i32(tcg_tmp32);
3696 /* C5.6.149 REV with sf==1, opcode==3 ("REV64") */
3697 static void handle_rev64(DisasContext *s, unsigned int sf,
3698 unsigned int rn, unsigned int rd)
3700 if (!sf) {
3701 unallocated_encoding(s);
3702 return;
3704 tcg_gen_bswap64_i64(cpu_reg(s, rd), cpu_reg(s, rn));
3707 /* C5.6.149 REV with sf==0, opcode==2
3708 * C5.6.151 REV32 (sf==1, opcode==2)
3710 static void handle_rev32(DisasContext *s, unsigned int sf,
3711 unsigned int rn, unsigned int rd)
3713 TCGv_i64 tcg_rd = cpu_reg(s, rd);
3715 if (sf) {
3716 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
3717 TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
3719 /* bswap32_i64 requires zero high word */
3720 tcg_gen_ext32u_i64(tcg_tmp, tcg_rn);
3721 tcg_gen_bswap32_i64(tcg_rd, tcg_tmp);
3722 tcg_gen_shri_i64(tcg_tmp, tcg_rn, 32);
3723 tcg_gen_bswap32_i64(tcg_tmp, tcg_tmp);
3724 tcg_gen_concat32_i64(tcg_rd, tcg_rd, tcg_tmp);
3726 tcg_temp_free_i64(tcg_tmp);
3727 } else {
3728 tcg_gen_ext32u_i64(tcg_rd, cpu_reg(s, rn));
3729 tcg_gen_bswap32_i64(tcg_rd, tcg_rd);
3733 /* C5.6.150 REV16 (opcode==1) */
3734 static void handle_rev16(DisasContext *s, unsigned int sf,
3735 unsigned int rn, unsigned int rd)
3737 TCGv_i64 tcg_rd = cpu_reg(s, rd);
3738 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
3739 TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
3741 tcg_gen_andi_i64(tcg_tmp, tcg_rn, 0xffff);
3742 tcg_gen_bswap16_i64(tcg_rd, tcg_tmp);
3744 tcg_gen_shri_i64(tcg_tmp, tcg_rn, 16);
3745 tcg_gen_andi_i64(tcg_tmp, tcg_tmp, 0xffff);
3746 tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp);
3747 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, 16, 16);
3749 if (sf) {
3750 tcg_gen_shri_i64(tcg_tmp, tcg_rn, 32);
3751 tcg_gen_andi_i64(tcg_tmp, tcg_tmp, 0xffff);
3752 tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp);
3753 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, 32, 16);
3755 tcg_gen_shri_i64(tcg_tmp, tcg_rn, 48);
3756 tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp);
3757 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, 48, 16);
3760 tcg_temp_free_i64(tcg_tmp);
3763 /* C3.5.7 Data-processing (1 source)
3764 * 31 30 29 28 21 20 16 15 10 9 5 4 0
3765 * +----+---+---+-----------------+---------+--------+------+------+
3766 * | sf | 1 | S | 1 1 0 1 0 1 1 0 | opcode2 | opcode | Rn | Rd |
3767 * +----+---+---+-----------------+---------+--------+------+------+
3769 static void disas_data_proc_1src(DisasContext *s, uint32_t insn)
3771 unsigned int sf, opcode, rn, rd;
3773 if (extract32(insn, 29, 1) || extract32(insn, 16, 5)) {
3774 unallocated_encoding(s);
3775 return;
3778 sf = extract32(insn, 31, 1);
3779 opcode = extract32(insn, 10, 6);
3780 rn = extract32(insn, 5, 5);
3781 rd = extract32(insn, 0, 5);
3783 switch (opcode) {
3784 case 0: /* RBIT */
3785 handle_rbit(s, sf, rn, rd);
3786 break;
3787 case 1: /* REV16 */
3788 handle_rev16(s, sf, rn, rd);
3789 break;
3790 case 2: /* REV32 */
3791 handle_rev32(s, sf, rn, rd);
3792 break;
3793 case 3: /* REV64 */
3794 handle_rev64(s, sf, rn, rd);
3795 break;
3796 case 4: /* CLZ */
3797 handle_clz(s, sf, rn, rd);
3798 break;
3799 case 5: /* CLS */
3800 handle_cls(s, sf, rn, rd);
3801 break;
3805 static void handle_div(DisasContext *s, bool is_signed, unsigned int sf,
3806 unsigned int rm, unsigned int rn, unsigned int rd)
3808 TCGv_i64 tcg_n, tcg_m, tcg_rd;
3809 tcg_rd = cpu_reg(s, rd);
3811 if (!sf && is_signed) {
3812 tcg_n = new_tmp_a64(s);
3813 tcg_m = new_tmp_a64(s);
3814 tcg_gen_ext32s_i64(tcg_n, cpu_reg(s, rn));
3815 tcg_gen_ext32s_i64(tcg_m, cpu_reg(s, rm));
3816 } else {
3817 tcg_n = read_cpu_reg(s, rn, sf);
3818 tcg_m = read_cpu_reg(s, rm, sf);
3821 if (is_signed) {
3822 gen_helper_sdiv64(tcg_rd, tcg_n, tcg_m);
3823 } else {
3824 gen_helper_udiv64(tcg_rd, tcg_n, tcg_m);
3827 if (!sf) { /* zero extend final result */
3828 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3832 /* C5.6.115 LSLV, C5.6.118 LSRV, C5.6.17 ASRV, C5.6.154 RORV */
3833 static void handle_shift_reg(DisasContext *s,
3834 enum a64_shift_type shift_type, unsigned int sf,
3835 unsigned int rm, unsigned int rn, unsigned int rd)
3837 TCGv_i64 tcg_shift = tcg_temp_new_i64();
3838 TCGv_i64 tcg_rd = cpu_reg(s, rd);
3839 TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
3841 tcg_gen_andi_i64(tcg_shift, cpu_reg(s, rm), sf ? 63 : 31);
3842 shift_reg(tcg_rd, tcg_rn, sf, shift_type, tcg_shift);
3843 tcg_temp_free_i64(tcg_shift);
3846 /* CRC32[BHWX], CRC32C[BHWX] */
3847 static void handle_crc32(DisasContext *s,
3848 unsigned int sf, unsigned int sz, bool crc32c,
3849 unsigned int rm, unsigned int rn, unsigned int rd)
3851 TCGv_i64 tcg_acc, tcg_val;
3852 TCGv_i32 tcg_bytes;
3854 if (!arm_dc_feature(s, ARM_FEATURE_CRC)
3855 || (sf == 1 && sz != 3)
3856 || (sf == 0 && sz == 3)) {
3857 unallocated_encoding(s);
3858 return;
3861 if (sz == 3) {
3862 tcg_val = cpu_reg(s, rm);
3863 } else {
3864 uint64_t mask;
3865 switch (sz) {
3866 case 0:
3867 mask = 0xFF;
3868 break;
3869 case 1:
3870 mask = 0xFFFF;
3871 break;
3872 case 2:
3873 mask = 0xFFFFFFFF;
3874 break;
3875 default:
3876 g_assert_not_reached();
3878 tcg_val = new_tmp_a64(s);
3879 tcg_gen_andi_i64(tcg_val, cpu_reg(s, rm), mask);
3882 tcg_acc = cpu_reg(s, rn);
3883 tcg_bytes = tcg_const_i32(1 << sz);
3885 if (crc32c) {
3886 gen_helper_crc32c_64(cpu_reg(s, rd), tcg_acc, tcg_val, tcg_bytes);
3887 } else {
3888 gen_helper_crc32_64(cpu_reg(s, rd), tcg_acc, tcg_val, tcg_bytes);
3891 tcg_temp_free_i32(tcg_bytes);
3894 /* C3.5.8 Data-processing (2 source)
3895 * 31 30 29 28 21 20 16 15 10 9 5 4 0
3896 * +----+---+---+-----------------+------+--------+------+------+
3897 * | sf | 0 | S | 1 1 0 1 0 1 1 0 | Rm | opcode | Rn | Rd |
3898 * +----+---+---+-----------------+------+--------+------+------+
3900 static void disas_data_proc_2src(DisasContext *s, uint32_t insn)
3902 unsigned int sf, rm, opcode, rn, rd;
3903 sf = extract32(insn, 31, 1);
3904 rm = extract32(insn, 16, 5);
3905 opcode = extract32(insn, 10, 6);
3906 rn = extract32(insn, 5, 5);
3907 rd = extract32(insn, 0, 5);
3909 if (extract32(insn, 29, 1)) {
3910 unallocated_encoding(s);
3911 return;
3914 switch (opcode) {
3915 case 2: /* UDIV */
3916 handle_div(s, false, sf, rm, rn, rd);
3917 break;
3918 case 3: /* SDIV */
3919 handle_div(s, true, sf, rm, rn, rd);
3920 break;
3921 case 8: /* LSLV */
3922 handle_shift_reg(s, A64_SHIFT_TYPE_LSL, sf, rm, rn, rd);
3923 break;
3924 case 9: /* LSRV */
3925 handle_shift_reg(s, A64_SHIFT_TYPE_LSR, sf, rm, rn, rd);
3926 break;
3927 case 10: /* ASRV */
3928 handle_shift_reg(s, A64_SHIFT_TYPE_ASR, sf, rm, rn, rd);
3929 break;
3930 case 11: /* RORV */
3931 handle_shift_reg(s, A64_SHIFT_TYPE_ROR, sf, rm, rn, rd);
3932 break;
3933 case 16:
3934 case 17:
3935 case 18:
3936 case 19:
3937 case 20:
3938 case 21:
3939 case 22:
3940 case 23: /* CRC32 */
3942 int sz = extract32(opcode, 0, 2);
3943 bool crc32c = extract32(opcode, 2, 1);
3944 handle_crc32(s, sf, sz, crc32c, rm, rn, rd);
3945 break;
3947 default:
3948 unallocated_encoding(s);
3949 break;
3953 /* C3.5 Data processing - register */
3954 static void disas_data_proc_reg(DisasContext *s, uint32_t insn)
3956 switch (extract32(insn, 24, 5)) {
3957 case 0x0a: /* Logical (shifted register) */
3958 disas_logic_reg(s, insn);
3959 break;
3960 case 0x0b: /* Add/subtract */
3961 if (insn & (1 << 21)) { /* (extended register) */
3962 disas_add_sub_ext_reg(s, insn);
3963 } else {
3964 disas_add_sub_reg(s, insn);
3966 break;
3967 case 0x1b: /* Data-processing (3 source) */
3968 disas_data_proc_3src(s, insn);
3969 break;
3970 case 0x1a:
3971 switch (extract32(insn, 21, 3)) {
3972 case 0x0: /* Add/subtract (with carry) */
3973 disas_adc_sbc(s, insn);
3974 break;
3975 case 0x2: /* Conditional compare */
3976 disas_cc(s, insn); /* both imm and reg forms */
3977 break;
3978 case 0x4: /* Conditional select */
3979 disas_cond_select(s, insn);
3980 break;
3981 case 0x6: /* Data-processing */
3982 if (insn & (1 << 30)) { /* (1 source) */
3983 disas_data_proc_1src(s, insn);
3984 } else { /* (2 source) */
3985 disas_data_proc_2src(s, insn);
3987 break;
3988 default:
3989 unallocated_encoding(s);
3990 break;
3992 break;
3993 default:
3994 unallocated_encoding(s);
3995 break;
3999 static void handle_fp_compare(DisasContext *s, bool is_double,
4000 unsigned int rn, unsigned int rm,
4001 bool cmp_with_zero, bool signal_all_nans)
4003 TCGv_i64 tcg_flags = tcg_temp_new_i64();
4004 TCGv_ptr fpst = get_fpstatus_ptr();
4006 if (is_double) {
4007 TCGv_i64 tcg_vn, tcg_vm;
4009 tcg_vn = read_fp_dreg(s, rn);
4010 if (cmp_with_zero) {
4011 tcg_vm = tcg_const_i64(0);
4012 } else {
4013 tcg_vm = read_fp_dreg(s, rm);
4015 if (signal_all_nans) {
4016 gen_helper_vfp_cmped_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
4017 } else {
4018 gen_helper_vfp_cmpd_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
4020 tcg_temp_free_i64(tcg_vn);
4021 tcg_temp_free_i64(tcg_vm);
4022 } else {
4023 TCGv_i32 tcg_vn, tcg_vm;
4025 tcg_vn = read_fp_sreg(s, rn);
4026 if (cmp_with_zero) {
4027 tcg_vm = tcg_const_i32(0);
4028 } else {
4029 tcg_vm = read_fp_sreg(s, rm);
4031 if (signal_all_nans) {
4032 gen_helper_vfp_cmpes_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
4033 } else {
4034 gen_helper_vfp_cmps_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
4036 tcg_temp_free_i32(tcg_vn);
4037 tcg_temp_free_i32(tcg_vm);
4040 tcg_temp_free_ptr(fpst);
4042 gen_set_nzcv(tcg_flags);
4044 tcg_temp_free_i64(tcg_flags);
4047 /* C3.6.22 Floating point compare
4048 * 31 30 29 28 24 23 22 21 20 16 15 14 13 10 9 5 4 0
4049 * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
4050 * | M | 0 | S | 1 1 1 1 0 | type | 1 | Rm | op | 1 0 0 0 | Rn | op2 |
4051 * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
4053 static void disas_fp_compare(DisasContext *s, uint32_t insn)
4055 unsigned int mos, type, rm, op, rn, opc, op2r;
4057 mos = extract32(insn, 29, 3);
4058 type = extract32(insn, 22, 2); /* 0 = single, 1 = double */
4059 rm = extract32(insn, 16, 5);
4060 op = extract32(insn, 14, 2);
4061 rn = extract32(insn, 5, 5);
4062 opc = extract32(insn, 3, 2);
4063 op2r = extract32(insn, 0, 3);
4065 if (mos || op || op2r || type > 1) {
4066 unallocated_encoding(s);
4067 return;
4070 if (!fp_access_check(s)) {
4071 return;
4074 handle_fp_compare(s, type, rn, rm, opc & 1, opc & 2);
4077 /* C3.6.23 Floating point conditional compare
4078 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 3 0
4079 * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
4080 * | M | 0 | S | 1 1 1 1 0 | type | 1 | Rm | cond | 0 1 | Rn | op | nzcv |
4081 * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
4083 static void disas_fp_ccomp(DisasContext *s, uint32_t insn)
4085 unsigned int mos, type, rm, cond, rn, op, nzcv;
4086 TCGv_i64 tcg_flags;
4087 int label_continue = -1;
4089 mos = extract32(insn, 29, 3);
4090 type = extract32(insn, 22, 2); /* 0 = single, 1 = double */
4091 rm = extract32(insn, 16, 5);
4092 cond = extract32(insn, 12, 4);
4093 rn = extract32(insn, 5, 5);
4094 op = extract32(insn, 4, 1);
4095 nzcv = extract32(insn, 0, 4);
4097 if (mos || type > 1) {
4098 unallocated_encoding(s);
4099 return;
4102 if (!fp_access_check(s)) {
4103 return;
4106 if (cond < 0x0e) { /* not always */
4107 int label_match = gen_new_label();
4108 label_continue = gen_new_label();
4109 arm_gen_test_cc(cond, label_match);
4110 /* nomatch: */
4111 tcg_flags = tcg_const_i64(nzcv << 28);
4112 gen_set_nzcv(tcg_flags);
4113 tcg_temp_free_i64(tcg_flags);
4114 tcg_gen_br(label_continue);
4115 gen_set_label(label_match);
4118 handle_fp_compare(s, type, rn, rm, false, op);
4120 if (cond < 0x0e) {
4121 gen_set_label(label_continue);
4125 /* copy src FP register to dst FP register; type specifies single or double */
4126 static void gen_mov_fp2fp(DisasContext *s, int type, int dst, int src)
4128 if (type) {
4129 TCGv_i64 v = read_fp_dreg(s, src);
4130 write_fp_dreg(s, dst, v);
4131 tcg_temp_free_i64(v);
4132 } else {
4133 TCGv_i32 v = read_fp_sreg(s, src);
4134 write_fp_sreg(s, dst, v);
4135 tcg_temp_free_i32(v);
4139 /* C3.6.24 Floating point conditional select
4140 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 0
4141 * +---+---+---+-----------+------+---+------+------+-----+------+------+
4142 * | M | 0 | S | 1 1 1 1 0 | type | 1 | Rm | cond | 1 1 | Rn | Rd |
4143 * +---+---+---+-----------+------+---+------+------+-----+------+------+
4145 static void disas_fp_csel(DisasContext *s, uint32_t insn)
4147 unsigned int mos, type, rm, cond, rn, rd;
4148 int label_continue = -1;
4150 mos = extract32(insn, 29, 3);
4151 type = extract32(insn, 22, 2); /* 0 = single, 1 = double */
4152 rm = extract32(insn, 16, 5);
4153 cond = extract32(insn, 12, 4);
4154 rn = extract32(insn, 5, 5);
4155 rd = extract32(insn, 0, 5);
4157 if (mos || type > 1) {
4158 unallocated_encoding(s);
4159 return;
4162 if (!fp_access_check(s)) {
4163 return;
4166 if (cond < 0x0e) { /* not always */
4167 int label_match = gen_new_label();
4168 label_continue = gen_new_label();
4169 arm_gen_test_cc(cond, label_match);
4170 /* nomatch: */
4171 gen_mov_fp2fp(s, type, rd, rm);
4172 tcg_gen_br(label_continue);
4173 gen_set_label(label_match);
4176 gen_mov_fp2fp(s, type, rd, rn);
4178 if (cond < 0x0e) { /* continue */
4179 gen_set_label(label_continue);
4183 /* C3.6.25 Floating-point data-processing (1 source) - single precision */
4184 static void handle_fp_1src_single(DisasContext *s, int opcode, int rd, int rn)
4186 TCGv_ptr fpst;
4187 TCGv_i32 tcg_op;
4188 TCGv_i32 tcg_res;
4190 fpst = get_fpstatus_ptr();
4191 tcg_op = read_fp_sreg(s, rn);
4192 tcg_res = tcg_temp_new_i32();
4194 switch (opcode) {
4195 case 0x0: /* FMOV */
4196 tcg_gen_mov_i32(tcg_res, tcg_op);
4197 break;
4198 case 0x1: /* FABS */
4199 gen_helper_vfp_abss(tcg_res, tcg_op);
4200 break;
4201 case 0x2: /* FNEG */
4202 gen_helper_vfp_negs(tcg_res, tcg_op);
4203 break;
4204 case 0x3: /* FSQRT */
4205 gen_helper_vfp_sqrts(tcg_res, tcg_op, cpu_env);
4206 break;
4207 case 0x8: /* FRINTN */
4208 case 0x9: /* FRINTP */
4209 case 0xa: /* FRINTM */
4210 case 0xb: /* FRINTZ */
4211 case 0xc: /* FRINTA */
4213 TCGv_i32 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(opcode & 7));
4215 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
4216 gen_helper_rints(tcg_res, tcg_op, fpst);
4218 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
4219 tcg_temp_free_i32(tcg_rmode);
4220 break;
4222 case 0xe: /* FRINTX */
4223 gen_helper_rints_exact(tcg_res, tcg_op, fpst);
4224 break;
4225 case 0xf: /* FRINTI */
4226 gen_helper_rints(tcg_res, tcg_op, fpst);
4227 break;
4228 default:
4229 abort();
4232 write_fp_sreg(s, rd, tcg_res);
4234 tcg_temp_free_ptr(fpst);
4235 tcg_temp_free_i32(tcg_op);
4236 tcg_temp_free_i32(tcg_res);
4239 /* C3.6.25 Floating-point data-processing (1 source) - double precision */
4240 static void handle_fp_1src_double(DisasContext *s, int opcode, int rd, int rn)
4242 TCGv_ptr fpst;
4243 TCGv_i64 tcg_op;
4244 TCGv_i64 tcg_res;
4246 fpst = get_fpstatus_ptr();
4247 tcg_op = read_fp_dreg(s, rn);
4248 tcg_res = tcg_temp_new_i64();
4250 switch (opcode) {
4251 case 0x0: /* FMOV */
4252 tcg_gen_mov_i64(tcg_res, tcg_op);
4253 break;
4254 case 0x1: /* FABS */
4255 gen_helper_vfp_absd(tcg_res, tcg_op);
4256 break;
4257 case 0x2: /* FNEG */
4258 gen_helper_vfp_negd(tcg_res, tcg_op);
4259 break;
4260 case 0x3: /* FSQRT */
4261 gen_helper_vfp_sqrtd(tcg_res, tcg_op, cpu_env);
4262 break;
4263 case 0x8: /* FRINTN */
4264 case 0x9: /* FRINTP */
4265 case 0xa: /* FRINTM */
4266 case 0xb: /* FRINTZ */
4267 case 0xc: /* FRINTA */
4269 TCGv_i32 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(opcode & 7));
4271 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
4272 gen_helper_rintd(tcg_res, tcg_op, fpst);
4274 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
4275 tcg_temp_free_i32(tcg_rmode);
4276 break;
4278 case 0xe: /* FRINTX */
4279 gen_helper_rintd_exact(tcg_res, tcg_op, fpst);
4280 break;
4281 case 0xf: /* FRINTI */
4282 gen_helper_rintd(tcg_res, tcg_op, fpst);
4283 break;
4284 default:
4285 abort();
4288 write_fp_dreg(s, rd, tcg_res);
4290 tcg_temp_free_ptr(fpst);
4291 tcg_temp_free_i64(tcg_op);
4292 tcg_temp_free_i64(tcg_res);
4295 static void handle_fp_fcvt(DisasContext *s, int opcode,
4296 int rd, int rn, int dtype, int ntype)
4298 switch (ntype) {
4299 case 0x0:
4301 TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
4302 if (dtype == 1) {
4303 /* Single to double */
4304 TCGv_i64 tcg_rd = tcg_temp_new_i64();
4305 gen_helper_vfp_fcvtds(tcg_rd, tcg_rn, cpu_env);
4306 write_fp_dreg(s, rd, tcg_rd);
4307 tcg_temp_free_i64(tcg_rd);
4308 } else {
4309 /* Single to half */
4310 TCGv_i32 tcg_rd = tcg_temp_new_i32();
4311 gen_helper_vfp_fcvt_f32_to_f16(tcg_rd, tcg_rn, cpu_env);
4312 /* write_fp_sreg is OK here because top half of tcg_rd is zero */
4313 write_fp_sreg(s, rd, tcg_rd);
4314 tcg_temp_free_i32(tcg_rd);
4316 tcg_temp_free_i32(tcg_rn);
4317 break;
4319 case 0x1:
4321 TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
4322 TCGv_i32 tcg_rd = tcg_temp_new_i32();
4323 if (dtype == 0) {
4324 /* Double to single */
4325 gen_helper_vfp_fcvtsd(tcg_rd, tcg_rn, cpu_env);
4326 } else {
4327 /* Double to half */
4328 gen_helper_vfp_fcvt_f64_to_f16(tcg_rd, tcg_rn, cpu_env);
4329 /* write_fp_sreg is OK here because top half of tcg_rd is zero */
4331 write_fp_sreg(s, rd, tcg_rd);
4332 tcg_temp_free_i32(tcg_rd);
4333 tcg_temp_free_i64(tcg_rn);
4334 break;
4336 case 0x3:
4338 TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
4339 tcg_gen_ext16u_i32(tcg_rn, tcg_rn);
4340 if (dtype == 0) {
4341 /* Half to single */
4342 TCGv_i32 tcg_rd = tcg_temp_new_i32();
4343 gen_helper_vfp_fcvt_f16_to_f32(tcg_rd, tcg_rn, cpu_env);
4344 write_fp_sreg(s, rd, tcg_rd);
4345 tcg_temp_free_i32(tcg_rd);
4346 } else {
4347 /* Half to double */
4348 TCGv_i64 tcg_rd = tcg_temp_new_i64();
4349 gen_helper_vfp_fcvt_f16_to_f64(tcg_rd, tcg_rn, cpu_env);
4350 write_fp_dreg(s, rd, tcg_rd);
4351 tcg_temp_free_i64(tcg_rd);
4353 tcg_temp_free_i32(tcg_rn);
4354 break;
4356 default:
4357 abort();
4361 /* C3.6.25 Floating point data-processing (1 source)
4362 * 31 30 29 28 24 23 22 21 20 15 14 10 9 5 4 0
4363 * +---+---+---+-----------+------+---+--------+-----------+------+------+
4364 * | M | 0 | S | 1 1 1 1 0 | type | 1 | opcode | 1 0 0 0 0 | Rn | Rd |
4365 * +---+---+---+-----------+------+---+--------+-----------+------+------+
4367 static void disas_fp_1src(DisasContext *s, uint32_t insn)
4369 int type = extract32(insn, 22, 2);
4370 int opcode = extract32(insn, 15, 6);
4371 int rn = extract32(insn, 5, 5);
4372 int rd = extract32(insn, 0, 5);
4374 switch (opcode) {
4375 case 0x4: case 0x5: case 0x7:
4377 /* FCVT between half, single and double precision */
4378 int dtype = extract32(opcode, 0, 2);
4379 if (type == 2 || dtype == type) {
4380 unallocated_encoding(s);
4381 return;
4383 if (!fp_access_check(s)) {
4384 return;
4387 handle_fp_fcvt(s, opcode, rd, rn, dtype, type);
4388 break;
4390 case 0x0 ... 0x3:
4391 case 0x8 ... 0xc:
4392 case 0xe ... 0xf:
4393 /* 32-to-32 and 64-to-64 ops */
4394 switch (type) {
4395 case 0:
4396 if (!fp_access_check(s)) {
4397 return;
4400 handle_fp_1src_single(s, opcode, rd, rn);
4401 break;
4402 case 1:
4403 if (!fp_access_check(s)) {
4404 return;
4407 handle_fp_1src_double(s, opcode, rd, rn);
4408 break;
4409 default:
4410 unallocated_encoding(s);
4412 break;
4413 default:
4414 unallocated_encoding(s);
4415 break;
4419 /* C3.6.26 Floating-point data-processing (2 source) - single precision */
4420 static void handle_fp_2src_single(DisasContext *s, int opcode,
4421 int rd, int rn, int rm)
4423 TCGv_i32 tcg_op1;
4424 TCGv_i32 tcg_op2;
4425 TCGv_i32 tcg_res;
4426 TCGv_ptr fpst;
4428 tcg_res = tcg_temp_new_i32();
4429 fpst = get_fpstatus_ptr();
4430 tcg_op1 = read_fp_sreg(s, rn);
4431 tcg_op2 = read_fp_sreg(s, rm);
4433 switch (opcode) {
4434 case 0x0: /* FMUL */
4435 gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
4436 break;
4437 case 0x1: /* FDIV */
4438 gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
4439 break;
4440 case 0x2: /* FADD */
4441 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
4442 break;
4443 case 0x3: /* FSUB */
4444 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
4445 break;
4446 case 0x4: /* FMAX */
4447 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
4448 break;
4449 case 0x5: /* FMIN */
4450 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
4451 break;
4452 case 0x6: /* FMAXNM */
4453 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
4454 break;
4455 case 0x7: /* FMINNM */
4456 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
4457 break;
4458 case 0x8: /* FNMUL */
4459 gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
4460 gen_helper_vfp_negs(tcg_res, tcg_res);
4461 break;
4464 write_fp_sreg(s, rd, tcg_res);
4466 tcg_temp_free_ptr(fpst);
4467 tcg_temp_free_i32(tcg_op1);
4468 tcg_temp_free_i32(tcg_op2);
4469 tcg_temp_free_i32(tcg_res);
4472 /* C3.6.26 Floating-point data-processing (2 source) - double precision */
4473 static void handle_fp_2src_double(DisasContext *s, int opcode,
4474 int rd, int rn, int rm)
4476 TCGv_i64 tcg_op1;
4477 TCGv_i64 tcg_op2;
4478 TCGv_i64 tcg_res;
4479 TCGv_ptr fpst;
4481 tcg_res = tcg_temp_new_i64();
4482 fpst = get_fpstatus_ptr();
4483 tcg_op1 = read_fp_dreg(s, rn);
4484 tcg_op2 = read_fp_dreg(s, rm);
4486 switch (opcode) {
4487 case 0x0: /* FMUL */
4488 gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
4489 break;
4490 case 0x1: /* FDIV */
4491 gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
4492 break;
4493 case 0x2: /* FADD */
4494 gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
4495 break;
4496 case 0x3: /* FSUB */
4497 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
4498 break;
4499 case 0x4: /* FMAX */
4500 gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
4501 break;
4502 case 0x5: /* FMIN */
4503 gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
4504 break;
4505 case 0x6: /* FMAXNM */
4506 gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
4507 break;
4508 case 0x7: /* FMINNM */
4509 gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
4510 break;
4511 case 0x8: /* FNMUL */
4512 gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
4513 gen_helper_vfp_negd(tcg_res, tcg_res);
4514 break;
4517 write_fp_dreg(s, rd, tcg_res);
4519 tcg_temp_free_ptr(fpst);
4520 tcg_temp_free_i64(tcg_op1);
4521 tcg_temp_free_i64(tcg_op2);
4522 tcg_temp_free_i64(tcg_res);
4525 /* C3.6.26 Floating point data-processing (2 source)
4526 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 0
4527 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
4528 * | M | 0 | S | 1 1 1 1 0 | type | 1 | Rm | opcode | 1 0 | Rn | Rd |
4529 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
4531 static void disas_fp_2src(DisasContext *s, uint32_t insn)
4533 int type = extract32(insn, 22, 2);
4534 int rd = extract32(insn, 0, 5);
4535 int rn = extract32(insn, 5, 5);
4536 int rm = extract32(insn, 16, 5);
4537 int opcode = extract32(insn, 12, 4);
4539 if (opcode > 8) {
4540 unallocated_encoding(s);
4541 return;
4544 switch (type) {
4545 case 0:
4546 if (!fp_access_check(s)) {
4547 return;
4549 handle_fp_2src_single(s, opcode, rd, rn, rm);
4550 break;
4551 case 1:
4552 if (!fp_access_check(s)) {
4553 return;
4555 handle_fp_2src_double(s, opcode, rd, rn, rm);
4556 break;
4557 default:
4558 unallocated_encoding(s);
4562 /* C3.6.27 Floating-point data-processing (3 source) - single precision */
4563 static void handle_fp_3src_single(DisasContext *s, bool o0, bool o1,
4564 int rd, int rn, int rm, int ra)
4566 TCGv_i32 tcg_op1, tcg_op2, tcg_op3;
4567 TCGv_i32 tcg_res = tcg_temp_new_i32();
4568 TCGv_ptr fpst = get_fpstatus_ptr();
4570 tcg_op1 = read_fp_sreg(s, rn);
4571 tcg_op2 = read_fp_sreg(s, rm);
4572 tcg_op3 = read_fp_sreg(s, ra);
4574 /* These are fused multiply-add, and must be done as one
4575 * floating point operation with no rounding between the
4576 * multiplication and addition steps.
4577 * NB that doing the negations here as separate steps is
4578 * correct : an input NaN should come out with its sign bit
4579 * flipped if it is a negated-input.
4581 if (o1 == true) {
4582 gen_helper_vfp_negs(tcg_op3, tcg_op3);
4585 if (o0 != o1) {
4586 gen_helper_vfp_negs(tcg_op1, tcg_op1);
4589 gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
4591 write_fp_sreg(s, rd, tcg_res);
4593 tcg_temp_free_ptr(fpst);
4594 tcg_temp_free_i32(tcg_op1);
4595 tcg_temp_free_i32(tcg_op2);
4596 tcg_temp_free_i32(tcg_op3);
4597 tcg_temp_free_i32(tcg_res);
4600 /* C3.6.27 Floating-point data-processing (3 source) - double precision */
4601 static void handle_fp_3src_double(DisasContext *s, bool o0, bool o1,
4602 int rd, int rn, int rm, int ra)
4604 TCGv_i64 tcg_op1, tcg_op2, tcg_op3;
4605 TCGv_i64 tcg_res = tcg_temp_new_i64();
4606 TCGv_ptr fpst = get_fpstatus_ptr();
4608 tcg_op1 = read_fp_dreg(s, rn);
4609 tcg_op2 = read_fp_dreg(s, rm);
4610 tcg_op3 = read_fp_dreg(s, ra);
4612 /* These are fused multiply-add, and must be done as one
4613 * floating point operation with no rounding between the
4614 * multiplication and addition steps.
4615 * NB that doing the negations here as separate steps is
4616 * correct : an input NaN should come out with its sign bit
4617 * flipped if it is a negated-input.
4619 if (o1 == true) {
4620 gen_helper_vfp_negd(tcg_op3, tcg_op3);
4623 if (o0 != o1) {
4624 gen_helper_vfp_negd(tcg_op1, tcg_op1);
4627 gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
4629 write_fp_dreg(s, rd, tcg_res);
4631 tcg_temp_free_ptr(fpst);
4632 tcg_temp_free_i64(tcg_op1);
4633 tcg_temp_free_i64(tcg_op2);
4634 tcg_temp_free_i64(tcg_op3);
4635 tcg_temp_free_i64(tcg_res);
4638 /* C3.6.27 Floating point data-processing (3 source)
4639 * 31 30 29 28 24 23 22 21 20 16 15 14 10 9 5 4 0
4640 * +---+---+---+-----------+------+----+------+----+------+------+------+
4641 * | M | 0 | S | 1 1 1 1 1 | type | o1 | Rm | o0 | Ra | Rn | Rd |
4642 * +---+---+---+-----------+------+----+------+----+------+------+------+
4644 static void disas_fp_3src(DisasContext *s, uint32_t insn)
4646 int type = extract32(insn, 22, 2);
4647 int rd = extract32(insn, 0, 5);
4648 int rn = extract32(insn, 5, 5);
4649 int ra = extract32(insn, 10, 5);
4650 int rm = extract32(insn, 16, 5);
4651 bool o0 = extract32(insn, 15, 1);
4652 bool o1 = extract32(insn, 21, 1);
4654 switch (type) {
4655 case 0:
4656 if (!fp_access_check(s)) {
4657 return;
4659 handle_fp_3src_single(s, o0, o1, rd, rn, rm, ra);
4660 break;
4661 case 1:
4662 if (!fp_access_check(s)) {
4663 return;
4665 handle_fp_3src_double(s, o0, o1, rd, rn, rm, ra);
4666 break;
4667 default:
4668 unallocated_encoding(s);
4672 /* C3.6.28 Floating point immediate
4673 * 31 30 29 28 24 23 22 21 20 13 12 10 9 5 4 0
4674 * +---+---+---+-----------+------+---+------------+-------+------+------+
4675 * | M | 0 | S | 1 1 1 1 0 | type | 1 | imm8 | 1 0 0 | imm5 | Rd |
4676 * +---+---+---+-----------+------+---+------------+-------+------+------+
4678 static void disas_fp_imm(DisasContext *s, uint32_t insn)
4680 int rd = extract32(insn, 0, 5);
4681 int imm8 = extract32(insn, 13, 8);
4682 int is_double = extract32(insn, 22, 2);
4683 uint64_t imm;
4684 TCGv_i64 tcg_res;
4686 if (is_double > 1) {
4687 unallocated_encoding(s);
4688 return;
4691 if (!fp_access_check(s)) {
4692 return;
4695 /* The imm8 encodes the sign bit, enough bits to represent
4696 * an exponent in the range 01....1xx to 10....0xx,
4697 * and the most significant 4 bits of the mantissa; see
4698 * VFPExpandImm() in the v8 ARM ARM.
4700 if (is_double) {
4701 imm = (extract32(imm8, 7, 1) ? 0x8000 : 0) |
4702 (extract32(imm8, 6, 1) ? 0x3fc0 : 0x4000) |
4703 extract32(imm8, 0, 6);
4704 imm <<= 48;
4705 } else {
4706 imm = (extract32(imm8, 7, 1) ? 0x8000 : 0) |
4707 (extract32(imm8, 6, 1) ? 0x3e00 : 0x4000) |
4708 (extract32(imm8, 0, 6) << 3);
4709 imm <<= 16;
4712 tcg_res = tcg_const_i64(imm);
4713 write_fp_dreg(s, rd, tcg_res);
4714 tcg_temp_free_i64(tcg_res);
4717 /* Handle floating point <=> fixed point conversions. Note that we can
4718 * also deal with fp <=> integer conversions as a special case (scale == 64)
4719 * OPTME: consider handling that special case specially or at least skipping
4720 * the call to scalbn in the helpers for zero shifts.
4722 static void handle_fpfpcvt(DisasContext *s, int rd, int rn, int opcode,
4723 bool itof, int rmode, int scale, int sf, int type)
4725 bool is_signed = !(opcode & 1);
4726 bool is_double = type;
4727 TCGv_ptr tcg_fpstatus;
4728 TCGv_i32 tcg_shift;
4730 tcg_fpstatus = get_fpstatus_ptr();
4732 tcg_shift = tcg_const_i32(64 - scale);
4734 if (itof) {
4735 TCGv_i64 tcg_int = cpu_reg(s, rn);
4736 if (!sf) {
4737 TCGv_i64 tcg_extend = new_tmp_a64(s);
4739 if (is_signed) {
4740 tcg_gen_ext32s_i64(tcg_extend, tcg_int);
4741 } else {
4742 tcg_gen_ext32u_i64(tcg_extend, tcg_int);
4745 tcg_int = tcg_extend;
4748 if (is_double) {
4749 TCGv_i64 tcg_double = tcg_temp_new_i64();
4750 if (is_signed) {
4751 gen_helper_vfp_sqtod(tcg_double, tcg_int,
4752 tcg_shift, tcg_fpstatus);
4753 } else {
4754 gen_helper_vfp_uqtod(tcg_double, tcg_int,
4755 tcg_shift, tcg_fpstatus);
4757 write_fp_dreg(s, rd, tcg_double);
4758 tcg_temp_free_i64(tcg_double);
4759 } else {
4760 TCGv_i32 tcg_single = tcg_temp_new_i32();
4761 if (is_signed) {
4762 gen_helper_vfp_sqtos(tcg_single, tcg_int,
4763 tcg_shift, tcg_fpstatus);
4764 } else {
4765 gen_helper_vfp_uqtos(tcg_single, tcg_int,
4766 tcg_shift, tcg_fpstatus);
4768 write_fp_sreg(s, rd, tcg_single);
4769 tcg_temp_free_i32(tcg_single);
4771 } else {
4772 TCGv_i64 tcg_int = cpu_reg(s, rd);
4773 TCGv_i32 tcg_rmode;
4775 if (extract32(opcode, 2, 1)) {
4776 /* There are too many rounding modes to all fit into rmode,
4777 * so FCVTA[US] is a special case.
4779 rmode = FPROUNDING_TIEAWAY;
4782 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rmode));
4784 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
4786 if (is_double) {
4787 TCGv_i64 tcg_double = read_fp_dreg(s, rn);
4788 if (is_signed) {
4789 if (!sf) {
4790 gen_helper_vfp_tosld(tcg_int, tcg_double,
4791 tcg_shift, tcg_fpstatus);
4792 } else {
4793 gen_helper_vfp_tosqd(tcg_int, tcg_double,
4794 tcg_shift, tcg_fpstatus);
4796 } else {
4797 if (!sf) {
4798 gen_helper_vfp_tould(tcg_int, tcg_double,
4799 tcg_shift, tcg_fpstatus);
4800 } else {
4801 gen_helper_vfp_touqd(tcg_int, tcg_double,
4802 tcg_shift, tcg_fpstatus);
4805 tcg_temp_free_i64(tcg_double);
4806 } else {
4807 TCGv_i32 tcg_single = read_fp_sreg(s, rn);
4808 if (sf) {
4809 if (is_signed) {
4810 gen_helper_vfp_tosqs(tcg_int, tcg_single,
4811 tcg_shift, tcg_fpstatus);
4812 } else {
4813 gen_helper_vfp_touqs(tcg_int, tcg_single,
4814 tcg_shift, tcg_fpstatus);
4816 } else {
4817 TCGv_i32 tcg_dest = tcg_temp_new_i32();
4818 if (is_signed) {
4819 gen_helper_vfp_tosls(tcg_dest, tcg_single,
4820 tcg_shift, tcg_fpstatus);
4821 } else {
4822 gen_helper_vfp_touls(tcg_dest, tcg_single,
4823 tcg_shift, tcg_fpstatus);
4825 tcg_gen_extu_i32_i64(tcg_int, tcg_dest);
4826 tcg_temp_free_i32(tcg_dest);
4828 tcg_temp_free_i32(tcg_single);
4831 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
4832 tcg_temp_free_i32(tcg_rmode);
4834 if (!sf) {
4835 tcg_gen_ext32u_i64(tcg_int, tcg_int);
4839 tcg_temp_free_ptr(tcg_fpstatus);
4840 tcg_temp_free_i32(tcg_shift);
4843 /* C3.6.29 Floating point <-> fixed point conversions
4844 * 31 30 29 28 24 23 22 21 20 19 18 16 15 10 9 5 4 0
4845 * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
4846 * | sf | 0 | S | 1 1 1 1 0 | type | 0 | rmode | opcode | scale | Rn | Rd |
4847 * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
4849 static void disas_fp_fixed_conv(DisasContext *s, uint32_t insn)
4851 int rd = extract32(insn, 0, 5);
4852 int rn = extract32(insn, 5, 5);
4853 int scale = extract32(insn, 10, 6);
4854 int opcode = extract32(insn, 16, 3);
4855 int rmode = extract32(insn, 19, 2);
4856 int type = extract32(insn, 22, 2);
4857 bool sbit = extract32(insn, 29, 1);
4858 bool sf = extract32(insn, 31, 1);
4859 bool itof;
4861 if (sbit || (type > 1)
4862 || (!sf && scale < 32)) {
4863 unallocated_encoding(s);
4864 return;
4867 switch ((rmode << 3) | opcode) {
4868 case 0x2: /* SCVTF */
4869 case 0x3: /* UCVTF */
4870 itof = true;
4871 break;
4872 case 0x18: /* FCVTZS */
4873 case 0x19: /* FCVTZU */
4874 itof = false;
4875 break;
4876 default:
4877 unallocated_encoding(s);
4878 return;
4881 if (!fp_access_check(s)) {
4882 return;
4885 handle_fpfpcvt(s, rd, rn, opcode, itof, FPROUNDING_ZERO, scale, sf, type);
4888 static void handle_fmov(DisasContext *s, int rd, int rn, int type, bool itof)
4890 /* FMOV: gpr to or from float, double, or top half of quad fp reg,
4891 * without conversion.
4894 if (itof) {
4895 TCGv_i64 tcg_rn = cpu_reg(s, rn);
4897 switch (type) {
4898 case 0:
4900 /* 32 bit */
4901 TCGv_i64 tmp = tcg_temp_new_i64();
4902 tcg_gen_ext32u_i64(tmp, tcg_rn);
4903 tcg_gen_st_i64(tmp, cpu_env, fp_reg_offset(s, rd, MO_64));
4904 tcg_gen_movi_i64(tmp, 0);
4905 tcg_gen_st_i64(tmp, cpu_env, fp_reg_hi_offset(s, rd));
4906 tcg_temp_free_i64(tmp);
4907 break;
4909 case 1:
4911 /* 64 bit */
4912 TCGv_i64 tmp = tcg_const_i64(0);
4913 tcg_gen_st_i64(tcg_rn, cpu_env, fp_reg_offset(s, rd, MO_64));
4914 tcg_gen_st_i64(tmp, cpu_env, fp_reg_hi_offset(s, rd));
4915 tcg_temp_free_i64(tmp);
4916 break;
4918 case 2:
4919 /* 64 bit to top half. */
4920 tcg_gen_st_i64(tcg_rn, cpu_env, fp_reg_hi_offset(s, rd));
4921 break;
4923 } else {
4924 TCGv_i64 tcg_rd = cpu_reg(s, rd);
4926 switch (type) {
4927 case 0:
4928 /* 32 bit */
4929 tcg_gen_ld32u_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_32));
4930 break;
4931 case 1:
4932 /* 64 bit */
4933 tcg_gen_ld_i64(tcg_rd, cpu_env, fp_reg_offset(s, rn, MO_64));
4934 break;
4935 case 2:
4936 /* 64 bits from top half */
4937 tcg_gen_ld_i64(tcg_rd, cpu_env, fp_reg_hi_offset(s, rn));
4938 break;
4943 /* C3.6.30 Floating point <-> integer conversions
4944 * 31 30 29 28 24 23 22 21 20 19 18 16 15 10 9 5 4 0
4945 * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
4946 * | sf | 0 | S | 1 1 1 1 0 | type | 1 | rmode | opc | 0 0 0 0 0 0 | Rn | Rd |
4947 * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
4949 static void disas_fp_int_conv(DisasContext *s, uint32_t insn)
4951 int rd = extract32(insn, 0, 5);
4952 int rn = extract32(insn, 5, 5);
4953 int opcode = extract32(insn, 16, 3);
4954 int rmode = extract32(insn, 19, 2);
4955 int type = extract32(insn, 22, 2);
4956 bool sbit = extract32(insn, 29, 1);
4957 bool sf = extract32(insn, 31, 1);
4959 if (sbit) {
4960 unallocated_encoding(s);
4961 return;
4964 if (opcode > 5) {
4965 /* FMOV */
4966 bool itof = opcode & 1;
4968 if (rmode >= 2) {
4969 unallocated_encoding(s);
4970 return;
4973 switch (sf << 3 | type << 1 | rmode) {
4974 case 0x0: /* 32 bit */
4975 case 0xa: /* 64 bit */
4976 case 0xd: /* 64 bit to top half of quad */
4977 break;
4978 default:
4979 /* all other sf/type/rmode combinations are invalid */
4980 unallocated_encoding(s);
4981 break;
4984 if (!fp_access_check(s)) {
4985 return;
4987 handle_fmov(s, rd, rn, type, itof);
4988 } else {
4989 /* actual FP conversions */
4990 bool itof = extract32(opcode, 1, 1);
4992 if (type > 1 || (rmode != 0 && opcode > 1)) {
4993 unallocated_encoding(s);
4994 return;
4997 if (!fp_access_check(s)) {
4998 return;
5000 handle_fpfpcvt(s, rd, rn, opcode, itof, rmode, 64, sf, type);
5004 /* FP-specific subcases of table C3-6 (SIMD and FP data processing)
5005 * 31 30 29 28 25 24 0
5006 * +---+---+---+---------+-----------------------------+
5007 * | | 0 | | 1 1 1 1 | |
5008 * +---+---+---+---------+-----------------------------+
5010 static void disas_data_proc_fp(DisasContext *s, uint32_t insn)
5012 if (extract32(insn, 24, 1)) {
5013 /* Floating point data-processing (3 source) */
5014 disas_fp_3src(s, insn);
5015 } else if (extract32(insn, 21, 1) == 0) {
5016 /* Floating point to fixed point conversions */
5017 disas_fp_fixed_conv(s, insn);
5018 } else {
5019 switch (extract32(insn, 10, 2)) {
5020 case 1:
5021 /* Floating point conditional compare */
5022 disas_fp_ccomp(s, insn);
5023 break;
5024 case 2:
5025 /* Floating point data-processing (2 source) */
5026 disas_fp_2src(s, insn);
5027 break;
5028 case 3:
5029 /* Floating point conditional select */
5030 disas_fp_csel(s, insn);
5031 break;
5032 case 0:
5033 switch (ctz32(extract32(insn, 12, 4))) {
5034 case 0: /* [15:12] == xxx1 */
5035 /* Floating point immediate */
5036 disas_fp_imm(s, insn);
5037 break;
5038 case 1: /* [15:12] == xx10 */
5039 /* Floating point compare */
5040 disas_fp_compare(s, insn);
5041 break;
5042 case 2: /* [15:12] == x100 */
5043 /* Floating point data-processing (1 source) */
5044 disas_fp_1src(s, insn);
5045 break;
5046 case 3: /* [15:12] == 1000 */
5047 unallocated_encoding(s);
5048 break;
5049 default: /* [15:12] == 0000 */
5050 /* Floating point <-> integer conversions */
5051 disas_fp_int_conv(s, insn);
5052 break;
5054 break;
5059 static void do_ext64(DisasContext *s, TCGv_i64 tcg_left, TCGv_i64 tcg_right,
5060 int pos)
5062 /* Extract 64 bits from the middle of two concatenated 64 bit
5063 * vector register slices left:right. The extracted bits start
5064 * at 'pos' bits into the right (least significant) side.
5065 * We return the result in tcg_right, and guarantee not to
5066 * trash tcg_left.
5068 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
5069 assert(pos > 0 && pos < 64);
5071 tcg_gen_shri_i64(tcg_right, tcg_right, pos);
5072 tcg_gen_shli_i64(tcg_tmp, tcg_left, 64 - pos);
5073 tcg_gen_or_i64(tcg_right, tcg_right, tcg_tmp);
5075 tcg_temp_free_i64(tcg_tmp);
5078 /* C3.6.1 EXT
5079 * 31 30 29 24 23 22 21 20 16 15 14 11 10 9 5 4 0
5080 * +---+---+-------------+-----+---+------+---+------+---+------+------+
5081 * | 0 | Q | 1 0 1 1 1 0 | op2 | 0 | Rm | 0 | imm4 | 0 | Rn | Rd |
5082 * +---+---+-------------+-----+---+------+---+------+---+------+------+
5084 static void disas_simd_ext(DisasContext *s, uint32_t insn)
5086 int is_q = extract32(insn, 30, 1);
5087 int op2 = extract32(insn, 22, 2);
5088 int imm4 = extract32(insn, 11, 4);
5089 int rm = extract32(insn, 16, 5);
5090 int rn = extract32(insn, 5, 5);
5091 int rd = extract32(insn, 0, 5);
5092 int pos = imm4 << 3;
5093 TCGv_i64 tcg_resl, tcg_resh;
5095 if (op2 != 0 || (!is_q && extract32(imm4, 3, 1))) {
5096 unallocated_encoding(s);
5097 return;
5100 if (!fp_access_check(s)) {
5101 return;
5104 tcg_resh = tcg_temp_new_i64();
5105 tcg_resl = tcg_temp_new_i64();
5107 /* Vd gets bits starting at pos bits into Vm:Vn. This is
5108 * either extracting 128 bits from a 128:128 concatenation, or
5109 * extracting 64 bits from a 64:64 concatenation.
5111 if (!is_q) {
5112 read_vec_element(s, tcg_resl, rn, 0, MO_64);
5113 if (pos != 0) {
5114 read_vec_element(s, tcg_resh, rm, 0, MO_64);
5115 do_ext64(s, tcg_resh, tcg_resl, pos);
5117 tcg_gen_movi_i64(tcg_resh, 0);
5118 } else {
5119 TCGv_i64 tcg_hh;
5120 typedef struct {
5121 int reg;
5122 int elt;
5123 } EltPosns;
5124 EltPosns eltposns[] = { {rn, 0}, {rn, 1}, {rm, 0}, {rm, 1} };
5125 EltPosns *elt = eltposns;
5127 if (pos >= 64) {
5128 elt++;
5129 pos -= 64;
5132 read_vec_element(s, tcg_resl, elt->reg, elt->elt, MO_64);
5133 elt++;
5134 read_vec_element(s, tcg_resh, elt->reg, elt->elt, MO_64);
5135 elt++;
5136 if (pos != 0) {
5137 do_ext64(s, tcg_resh, tcg_resl, pos);
5138 tcg_hh = tcg_temp_new_i64();
5139 read_vec_element(s, tcg_hh, elt->reg, elt->elt, MO_64);
5140 do_ext64(s, tcg_hh, tcg_resh, pos);
5141 tcg_temp_free_i64(tcg_hh);
5145 write_vec_element(s, tcg_resl, rd, 0, MO_64);
5146 tcg_temp_free_i64(tcg_resl);
5147 write_vec_element(s, tcg_resh, rd, 1, MO_64);
5148 tcg_temp_free_i64(tcg_resh);
5151 /* C3.6.2 TBL/TBX
5152 * 31 30 29 24 23 22 21 20 16 15 14 13 12 11 10 9 5 4 0
5153 * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
5154 * | 0 | Q | 0 0 1 1 1 0 | op2 | 0 | Rm | 0 | len | op | 0 0 | Rn | Rd |
5155 * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
5157 static void disas_simd_tb(DisasContext *s, uint32_t insn)
5159 int op2 = extract32(insn, 22, 2);
5160 int is_q = extract32(insn, 30, 1);
5161 int rm = extract32(insn, 16, 5);
5162 int rn = extract32(insn, 5, 5);
5163 int rd = extract32(insn, 0, 5);
5164 int is_tblx = extract32(insn, 12, 1);
5165 int len = extract32(insn, 13, 2);
5166 TCGv_i64 tcg_resl, tcg_resh, tcg_idx;
5167 TCGv_i32 tcg_regno, tcg_numregs;
5169 if (op2 != 0) {
5170 unallocated_encoding(s);
5171 return;
5174 if (!fp_access_check(s)) {
5175 return;
5178 /* This does a table lookup: for every byte element in the input
5179 * we index into a table formed from up to four vector registers,
5180 * and then the output is the result of the lookups. Our helper
5181 * function does the lookup operation for a single 64 bit part of
5182 * the input.
5184 tcg_resl = tcg_temp_new_i64();
5185 tcg_resh = tcg_temp_new_i64();
5187 if (is_tblx) {
5188 read_vec_element(s, tcg_resl, rd, 0, MO_64);
5189 } else {
5190 tcg_gen_movi_i64(tcg_resl, 0);
5192 if (is_tblx && is_q) {
5193 read_vec_element(s, tcg_resh, rd, 1, MO_64);
5194 } else {
5195 tcg_gen_movi_i64(tcg_resh, 0);
5198 tcg_idx = tcg_temp_new_i64();
5199 tcg_regno = tcg_const_i32(rn);
5200 tcg_numregs = tcg_const_i32(len + 1);
5201 read_vec_element(s, tcg_idx, rm, 0, MO_64);
5202 gen_helper_simd_tbl(tcg_resl, cpu_env, tcg_resl, tcg_idx,
5203 tcg_regno, tcg_numregs);
5204 if (is_q) {
5205 read_vec_element(s, tcg_idx, rm, 1, MO_64);
5206 gen_helper_simd_tbl(tcg_resh, cpu_env, tcg_resh, tcg_idx,
5207 tcg_regno, tcg_numregs);
5209 tcg_temp_free_i64(tcg_idx);
5210 tcg_temp_free_i32(tcg_regno);
5211 tcg_temp_free_i32(tcg_numregs);
5213 write_vec_element(s, tcg_resl, rd, 0, MO_64);
5214 tcg_temp_free_i64(tcg_resl);
5215 write_vec_element(s, tcg_resh, rd, 1, MO_64);
5216 tcg_temp_free_i64(tcg_resh);
5219 /* C3.6.3 ZIP/UZP/TRN
5220 * 31 30 29 24 23 22 21 20 16 15 14 12 11 10 9 5 4 0
5221 * +---+---+-------------+------+---+------+---+------------------+------+
5222 * | 0 | Q | 0 0 1 1 1 0 | size | 0 | Rm | 0 | opc | 1 0 | Rn | Rd |
5223 * +---+---+-------------+------+---+------+---+------------------+------+
5225 static void disas_simd_zip_trn(DisasContext *s, uint32_t insn)
5227 int rd = extract32(insn, 0, 5);
5228 int rn = extract32(insn, 5, 5);
5229 int rm = extract32(insn, 16, 5);
5230 int size = extract32(insn, 22, 2);
5231 /* opc field bits [1:0] indicate ZIP/UZP/TRN;
5232 * bit 2 indicates 1 vs 2 variant of the insn.
5234 int opcode = extract32(insn, 12, 2);
5235 bool part = extract32(insn, 14, 1);
5236 bool is_q = extract32(insn, 30, 1);
5237 int esize = 8 << size;
5238 int i, ofs;
5239 int datasize = is_q ? 128 : 64;
5240 int elements = datasize / esize;
5241 TCGv_i64 tcg_res, tcg_resl, tcg_resh;
5243 if (opcode == 0 || (size == 3 && !is_q)) {
5244 unallocated_encoding(s);
5245 return;
5248 if (!fp_access_check(s)) {
5249 return;
5252 tcg_resl = tcg_const_i64(0);
5253 tcg_resh = tcg_const_i64(0);
5254 tcg_res = tcg_temp_new_i64();
5256 for (i = 0; i < elements; i++) {
5257 switch (opcode) {
5258 case 1: /* UZP1/2 */
5260 int midpoint = elements / 2;
5261 if (i < midpoint) {
5262 read_vec_element(s, tcg_res, rn, 2 * i + part, size);
5263 } else {
5264 read_vec_element(s, tcg_res, rm,
5265 2 * (i - midpoint) + part, size);
5267 break;
5269 case 2: /* TRN1/2 */
5270 if (i & 1) {
5271 read_vec_element(s, tcg_res, rm, (i & ~1) + part, size);
5272 } else {
5273 read_vec_element(s, tcg_res, rn, (i & ~1) + part, size);
5275 break;
5276 case 3: /* ZIP1/2 */
5278 int base = part * elements / 2;
5279 if (i & 1) {
5280 read_vec_element(s, tcg_res, rm, base + (i >> 1), size);
5281 } else {
5282 read_vec_element(s, tcg_res, rn, base + (i >> 1), size);
5284 break;
5286 default:
5287 g_assert_not_reached();
5290 ofs = i * esize;
5291 if (ofs < 64) {
5292 tcg_gen_shli_i64(tcg_res, tcg_res, ofs);
5293 tcg_gen_or_i64(tcg_resl, tcg_resl, tcg_res);
5294 } else {
5295 tcg_gen_shli_i64(tcg_res, tcg_res, ofs - 64);
5296 tcg_gen_or_i64(tcg_resh, tcg_resh, tcg_res);
5300 tcg_temp_free_i64(tcg_res);
5302 write_vec_element(s, tcg_resl, rd, 0, MO_64);
5303 tcg_temp_free_i64(tcg_resl);
5304 write_vec_element(s, tcg_resh, rd, 1, MO_64);
5305 tcg_temp_free_i64(tcg_resh);
5308 static void do_minmaxop(DisasContext *s, TCGv_i32 tcg_elt1, TCGv_i32 tcg_elt2,
5309 int opc, bool is_min, TCGv_ptr fpst)
5311 /* Helper function for disas_simd_across_lanes: do a single precision
5312 * min/max operation on the specified two inputs,
5313 * and return the result in tcg_elt1.
5315 if (opc == 0xc) {
5316 if (is_min) {
5317 gen_helper_vfp_minnums(tcg_elt1, tcg_elt1, tcg_elt2, fpst);
5318 } else {
5319 gen_helper_vfp_maxnums(tcg_elt1, tcg_elt1, tcg_elt2, fpst);
5321 } else {
5322 assert(opc == 0xf);
5323 if (is_min) {
5324 gen_helper_vfp_mins(tcg_elt1, tcg_elt1, tcg_elt2, fpst);
5325 } else {
5326 gen_helper_vfp_maxs(tcg_elt1, tcg_elt1, tcg_elt2, fpst);
5331 /* C3.6.4 AdvSIMD across lanes
5332 * 31 30 29 28 24 23 22 21 17 16 12 11 10 9 5 4 0
5333 * +---+---+---+-----------+------+-----------+--------+-----+------+------+
5334 * | 0 | Q | U | 0 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 | Rn | Rd |
5335 * +---+---+---+-----------+------+-----------+--------+-----+------+------+
5337 static void disas_simd_across_lanes(DisasContext *s, uint32_t insn)
5339 int rd = extract32(insn, 0, 5);
5340 int rn = extract32(insn, 5, 5);
5341 int size = extract32(insn, 22, 2);
5342 int opcode = extract32(insn, 12, 5);
5343 bool is_q = extract32(insn, 30, 1);
5344 bool is_u = extract32(insn, 29, 1);
5345 bool is_fp = false;
5346 bool is_min = false;
5347 int esize;
5348 int elements;
5349 int i;
5350 TCGv_i64 tcg_res, tcg_elt;
5352 switch (opcode) {
5353 case 0x1b: /* ADDV */
5354 if (is_u) {
5355 unallocated_encoding(s);
5356 return;
5358 /* fall through */
5359 case 0x3: /* SADDLV, UADDLV */
5360 case 0xa: /* SMAXV, UMAXV */
5361 case 0x1a: /* SMINV, UMINV */
5362 if (size == 3 || (size == 2 && !is_q)) {
5363 unallocated_encoding(s);
5364 return;
5366 break;
5367 case 0xc: /* FMAXNMV, FMINNMV */
5368 case 0xf: /* FMAXV, FMINV */
5369 if (!is_u || !is_q || extract32(size, 0, 1)) {
5370 unallocated_encoding(s);
5371 return;
5373 /* Bit 1 of size field encodes min vs max, and actual size is always
5374 * 32 bits: adjust the size variable so following code can rely on it
5376 is_min = extract32(size, 1, 1);
5377 is_fp = true;
5378 size = 2;
5379 break;
5380 default:
5381 unallocated_encoding(s);
5382 return;
5385 if (!fp_access_check(s)) {
5386 return;
5389 esize = 8 << size;
5390 elements = (is_q ? 128 : 64) / esize;
5392 tcg_res = tcg_temp_new_i64();
5393 tcg_elt = tcg_temp_new_i64();
5395 /* These instructions operate across all lanes of a vector
5396 * to produce a single result. We can guarantee that a 64
5397 * bit intermediate is sufficient:
5398 * + for [US]ADDLV the maximum element size is 32 bits, and
5399 * the result type is 64 bits
5400 * + for FMAX*V, FMIN*V, ADDV the intermediate type is the
5401 * same as the element size, which is 32 bits at most
5402 * For the integer operations we can choose to work at 64
5403 * or 32 bits and truncate at the end; for simplicity
5404 * we use 64 bits always. The floating point
5405 * ops do require 32 bit intermediates, though.
5407 if (!is_fp) {
5408 read_vec_element(s, tcg_res, rn, 0, size | (is_u ? 0 : MO_SIGN));
5410 for (i = 1; i < elements; i++) {
5411 read_vec_element(s, tcg_elt, rn, i, size | (is_u ? 0 : MO_SIGN));
5413 switch (opcode) {
5414 case 0x03: /* SADDLV / UADDLV */
5415 case 0x1b: /* ADDV */
5416 tcg_gen_add_i64(tcg_res, tcg_res, tcg_elt);
5417 break;
5418 case 0x0a: /* SMAXV / UMAXV */
5419 tcg_gen_movcond_i64(is_u ? TCG_COND_GEU : TCG_COND_GE,
5420 tcg_res,
5421 tcg_res, tcg_elt, tcg_res, tcg_elt);
5422 break;
5423 case 0x1a: /* SMINV / UMINV */
5424 tcg_gen_movcond_i64(is_u ? TCG_COND_LEU : TCG_COND_LE,
5425 tcg_res,
5426 tcg_res, tcg_elt, tcg_res, tcg_elt);
5427 break;
5428 break;
5429 default:
5430 g_assert_not_reached();
5434 } else {
5435 /* Floating point ops which work on 32 bit (single) intermediates.
5436 * Note that correct NaN propagation requires that we do these
5437 * operations in exactly the order specified by the pseudocode.
5439 TCGv_i32 tcg_elt1 = tcg_temp_new_i32();
5440 TCGv_i32 tcg_elt2 = tcg_temp_new_i32();
5441 TCGv_i32 tcg_elt3 = tcg_temp_new_i32();
5442 TCGv_ptr fpst = get_fpstatus_ptr();
5444 assert(esize == 32);
5445 assert(elements == 4);
5447 read_vec_element(s, tcg_elt, rn, 0, MO_32);
5448 tcg_gen_trunc_i64_i32(tcg_elt1, tcg_elt);
5449 read_vec_element(s, tcg_elt, rn, 1, MO_32);
5450 tcg_gen_trunc_i64_i32(tcg_elt2, tcg_elt);
5452 do_minmaxop(s, tcg_elt1, tcg_elt2, opcode, is_min, fpst);
5454 read_vec_element(s, tcg_elt, rn, 2, MO_32);
5455 tcg_gen_trunc_i64_i32(tcg_elt2, tcg_elt);
5456 read_vec_element(s, tcg_elt, rn, 3, MO_32);
5457 tcg_gen_trunc_i64_i32(tcg_elt3, tcg_elt);
5459 do_minmaxop(s, tcg_elt2, tcg_elt3, opcode, is_min, fpst);
5461 do_minmaxop(s, tcg_elt1, tcg_elt2, opcode, is_min, fpst);
5463 tcg_gen_extu_i32_i64(tcg_res, tcg_elt1);
5464 tcg_temp_free_i32(tcg_elt1);
5465 tcg_temp_free_i32(tcg_elt2);
5466 tcg_temp_free_i32(tcg_elt3);
5467 tcg_temp_free_ptr(fpst);
5470 tcg_temp_free_i64(tcg_elt);
5472 /* Now truncate the result to the width required for the final output */
5473 if (opcode == 0x03) {
5474 /* SADDLV, UADDLV: result is 2*esize */
5475 size++;
5478 switch (size) {
5479 case 0:
5480 tcg_gen_ext8u_i64(tcg_res, tcg_res);
5481 break;
5482 case 1:
5483 tcg_gen_ext16u_i64(tcg_res, tcg_res);
5484 break;
5485 case 2:
5486 tcg_gen_ext32u_i64(tcg_res, tcg_res);
5487 break;
5488 case 3:
5489 break;
5490 default:
5491 g_assert_not_reached();
5494 write_fp_dreg(s, rd, tcg_res);
5495 tcg_temp_free_i64(tcg_res);
5498 /* C6.3.31 DUP (Element, Vector)
5500 * 31 30 29 21 20 16 15 10 9 5 4 0
5501 * +---+---+-------------------+--------+-------------+------+------+
5502 * | 0 | Q | 0 0 1 1 1 0 0 0 0 | imm5 | 0 0 0 0 0 1 | Rn | Rd |
5503 * +---+---+-------------------+--------+-------------+------+------+
5505 * size: encoded in imm5 (see ARM ARM LowestSetBit())
5507 static void handle_simd_dupe(DisasContext *s, int is_q, int rd, int rn,
5508 int imm5)
5510 int size = ctz32(imm5);
5511 int esize = 8 << size;
5512 int elements = (is_q ? 128 : 64) / esize;
5513 int index, i;
5514 TCGv_i64 tmp;
5516 if (size > 3 || (size == 3 && !is_q)) {
5517 unallocated_encoding(s);
5518 return;
5521 if (!fp_access_check(s)) {
5522 return;
5525 index = imm5 >> (size + 1);
5527 tmp = tcg_temp_new_i64();
5528 read_vec_element(s, tmp, rn, index, size);
5530 for (i = 0; i < elements; i++) {
5531 write_vec_element(s, tmp, rd, i, size);
5534 if (!is_q) {
5535 clear_vec_high(s, rd);
5538 tcg_temp_free_i64(tmp);
5541 /* C6.3.31 DUP (element, scalar)
5542 * 31 21 20 16 15 10 9 5 4 0
5543 * +-----------------------+--------+-------------+------+------+
5544 * | 0 1 0 1 1 1 1 0 0 0 0 | imm5 | 0 0 0 0 0 1 | Rn | Rd |
5545 * +-----------------------+--------+-------------+------+------+
5547 static void handle_simd_dupes(DisasContext *s, int rd, int rn,
5548 int imm5)
5550 int size = ctz32(imm5);
5551 int index;
5552 TCGv_i64 tmp;
5554 if (size > 3) {
5555 unallocated_encoding(s);
5556 return;
5559 if (!fp_access_check(s)) {
5560 return;
5563 index = imm5 >> (size + 1);
5565 /* This instruction just extracts the specified element and
5566 * zero-extends it into the bottom of the destination register.
5568 tmp = tcg_temp_new_i64();
5569 read_vec_element(s, tmp, rn, index, size);
5570 write_fp_dreg(s, rd, tmp);
5571 tcg_temp_free_i64(tmp);
5574 /* C6.3.32 DUP (General)
5576 * 31 30 29 21 20 16 15 10 9 5 4 0
5577 * +---+---+-------------------+--------+-------------+------+------+
5578 * | 0 | Q | 0 0 1 1 1 0 0 0 0 | imm5 | 0 0 0 0 1 1 | Rn | Rd |
5579 * +---+---+-------------------+--------+-------------+------+------+
5581 * size: encoded in imm5 (see ARM ARM LowestSetBit())
5583 static void handle_simd_dupg(DisasContext *s, int is_q, int rd, int rn,
5584 int imm5)
5586 int size = ctz32(imm5);
5587 int esize = 8 << size;
5588 int elements = (is_q ? 128 : 64)/esize;
5589 int i = 0;
5591 if (size > 3 || ((size == 3) && !is_q)) {
5592 unallocated_encoding(s);
5593 return;
5596 if (!fp_access_check(s)) {
5597 return;
5600 for (i = 0; i < elements; i++) {
5601 write_vec_element(s, cpu_reg(s, rn), rd, i, size);
5603 if (!is_q) {
5604 clear_vec_high(s, rd);
5608 /* C6.3.150 INS (Element)
5610 * 31 21 20 16 15 14 11 10 9 5 4 0
5611 * +-----------------------+--------+------------+---+------+------+
5612 * | 0 1 1 0 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 | Rn | Rd |
5613 * +-----------------------+--------+------------+---+------+------+
5615 * size: encoded in imm5 (see ARM ARM LowestSetBit())
5616 * index: encoded in imm5<4:size+1>
5618 static void handle_simd_inse(DisasContext *s, int rd, int rn,
5619 int imm4, int imm5)
5621 int size = ctz32(imm5);
5622 int src_index, dst_index;
5623 TCGv_i64 tmp;
5625 if (size > 3) {
5626 unallocated_encoding(s);
5627 return;
5630 if (!fp_access_check(s)) {
5631 return;
5634 dst_index = extract32(imm5, 1+size, 5);
5635 src_index = extract32(imm4, size, 4);
5637 tmp = tcg_temp_new_i64();
5639 read_vec_element(s, tmp, rn, src_index, size);
5640 write_vec_element(s, tmp, rd, dst_index, size);
5642 tcg_temp_free_i64(tmp);
5646 /* C6.3.151 INS (General)
5648 * 31 21 20 16 15 10 9 5 4 0
5649 * +-----------------------+--------+-------------+------+------+
5650 * | 0 1 0 0 1 1 1 0 0 0 0 | imm5 | 0 0 0 1 1 1 | Rn | Rd |
5651 * +-----------------------+--------+-------------+------+------+
5653 * size: encoded in imm5 (see ARM ARM LowestSetBit())
5654 * index: encoded in imm5<4:size+1>
5656 static void handle_simd_insg(DisasContext *s, int rd, int rn, int imm5)
5658 int size = ctz32(imm5);
5659 int idx;
5661 if (size > 3) {
5662 unallocated_encoding(s);
5663 return;
5666 if (!fp_access_check(s)) {
5667 return;
5670 idx = extract32(imm5, 1 + size, 4 - size);
5671 write_vec_element(s, cpu_reg(s, rn), rd, idx, size);
5675 * C6.3.321 UMOV (General)
5676 * C6.3.237 SMOV (General)
5678 * 31 30 29 21 20 16 15 12 10 9 5 4 0
5679 * +---+---+-------------------+--------+-------------+------+------+
5680 * | 0 | Q | 0 0 1 1 1 0 0 0 0 | imm5 | 0 0 1 U 1 1 | Rn | Rd |
5681 * +---+---+-------------------+--------+-------------+------+------+
5683 * U: unsigned when set
5684 * size: encoded in imm5 (see ARM ARM LowestSetBit())
5686 static void handle_simd_umov_smov(DisasContext *s, int is_q, int is_signed,
5687 int rn, int rd, int imm5)
5689 int size = ctz32(imm5);
5690 int element;
5691 TCGv_i64 tcg_rd;
5693 /* Check for UnallocatedEncodings */
5694 if (is_signed) {
5695 if (size > 2 || (size == 2 && !is_q)) {
5696 unallocated_encoding(s);
5697 return;
5699 } else {
5700 if (size > 3
5701 || (size < 3 && is_q)
5702 || (size == 3 && !is_q)) {
5703 unallocated_encoding(s);
5704 return;
5708 if (!fp_access_check(s)) {
5709 return;
5712 element = extract32(imm5, 1+size, 4);
5714 tcg_rd = cpu_reg(s, rd);
5715 read_vec_element(s, tcg_rd, rn, element, size | (is_signed ? MO_SIGN : 0));
5716 if (is_signed && !is_q) {
5717 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
5721 /* C3.6.5 AdvSIMD copy
5722 * 31 30 29 28 21 20 16 15 14 11 10 9 5 4 0
5723 * +---+---+----+-----------------+------+---+------+---+------+------+
5724 * | 0 | Q | op | 0 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 | Rn | Rd |
5725 * +---+---+----+-----------------+------+---+------+---+------+------+
5727 static void disas_simd_copy(DisasContext *s, uint32_t insn)
5729 int rd = extract32(insn, 0, 5);
5730 int rn = extract32(insn, 5, 5);
5731 int imm4 = extract32(insn, 11, 4);
5732 int op = extract32(insn, 29, 1);
5733 int is_q = extract32(insn, 30, 1);
5734 int imm5 = extract32(insn, 16, 5);
5736 if (op) {
5737 if (is_q) {
5738 /* INS (element) */
5739 handle_simd_inse(s, rd, rn, imm4, imm5);
5740 } else {
5741 unallocated_encoding(s);
5743 } else {
5744 switch (imm4) {
5745 case 0:
5746 /* DUP (element - vector) */
5747 handle_simd_dupe(s, is_q, rd, rn, imm5);
5748 break;
5749 case 1:
5750 /* DUP (general) */
5751 handle_simd_dupg(s, is_q, rd, rn, imm5);
5752 break;
5753 case 3:
5754 if (is_q) {
5755 /* INS (general) */
5756 handle_simd_insg(s, rd, rn, imm5);
5757 } else {
5758 unallocated_encoding(s);
5760 break;
5761 case 5:
5762 case 7:
5763 /* UMOV/SMOV (is_q indicates 32/64; imm4 indicates signedness) */
5764 handle_simd_umov_smov(s, is_q, (imm4 == 5), rn, rd, imm5);
5765 break;
5766 default:
5767 unallocated_encoding(s);
5768 break;
5773 /* C3.6.6 AdvSIMD modified immediate
5774 * 31 30 29 28 19 18 16 15 12 11 10 9 5 4 0
5775 * +---+---+----+---------------------+-----+-------+----+---+-------+------+
5776 * | 0 | Q | op | 0 1 1 1 1 0 0 0 0 0 | abc | cmode | o2 | 1 | defgh | Rd |
5777 * +---+---+----+---------------------+-----+-------+----+---+-------+------+
5779 * There are a number of operations that can be carried out here:
5780 * MOVI - move (shifted) imm into register
5781 * MVNI - move inverted (shifted) imm into register
5782 * ORR - bitwise OR of (shifted) imm with register
5783 * BIC - bitwise clear of (shifted) imm with register
5785 static void disas_simd_mod_imm(DisasContext *s, uint32_t insn)
5787 int rd = extract32(insn, 0, 5);
5788 int cmode = extract32(insn, 12, 4);
5789 int cmode_3_1 = extract32(cmode, 1, 3);
5790 int cmode_0 = extract32(cmode, 0, 1);
5791 int o2 = extract32(insn, 11, 1);
5792 uint64_t abcdefgh = extract32(insn, 5, 5) | (extract32(insn, 16, 3) << 5);
5793 bool is_neg = extract32(insn, 29, 1);
5794 bool is_q = extract32(insn, 30, 1);
5795 uint64_t imm = 0;
5796 TCGv_i64 tcg_rd, tcg_imm;
5797 int i;
5799 if (o2 != 0 || ((cmode == 0xf) && is_neg && !is_q)) {
5800 unallocated_encoding(s);
5801 return;
5804 if (!fp_access_check(s)) {
5805 return;
5808 /* See AdvSIMDExpandImm() in ARM ARM */
5809 switch (cmode_3_1) {
5810 case 0: /* Replicate(Zeros(24):imm8, 2) */
5811 case 1: /* Replicate(Zeros(16):imm8:Zeros(8), 2) */
5812 case 2: /* Replicate(Zeros(8):imm8:Zeros(16), 2) */
5813 case 3: /* Replicate(imm8:Zeros(24), 2) */
5815 int shift = cmode_3_1 * 8;
5816 imm = bitfield_replicate(abcdefgh << shift, 32);
5817 break;
5819 case 4: /* Replicate(Zeros(8):imm8, 4) */
5820 case 5: /* Replicate(imm8:Zeros(8), 4) */
5822 int shift = (cmode_3_1 & 0x1) * 8;
5823 imm = bitfield_replicate(abcdefgh << shift, 16);
5824 break;
5826 case 6:
5827 if (cmode_0) {
5828 /* Replicate(Zeros(8):imm8:Ones(16), 2) */
5829 imm = (abcdefgh << 16) | 0xffff;
5830 } else {
5831 /* Replicate(Zeros(16):imm8:Ones(8), 2) */
5832 imm = (abcdefgh << 8) | 0xff;
5834 imm = bitfield_replicate(imm, 32);
5835 break;
5836 case 7:
5837 if (!cmode_0 && !is_neg) {
5838 imm = bitfield_replicate(abcdefgh, 8);
5839 } else if (!cmode_0 && is_neg) {
5840 int i;
5841 imm = 0;
5842 for (i = 0; i < 8; i++) {
5843 if ((abcdefgh) & (1 << i)) {
5844 imm |= 0xffULL << (i * 8);
5847 } else if (cmode_0) {
5848 if (is_neg) {
5849 imm = (abcdefgh & 0x3f) << 48;
5850 if (abcdefgh & 0x80) {
5851 imm |= 0x8000000000000000ULL;
5853 if (abcdefgh & 0x40) {
5854 imm |= 0x3fc0000000000000ULL;
5855 } else {
5856 imm |= 0x4000000000000000ULL;
5858 } else {
5859 imm = (abcdefgh & 0x3f) << 19;
5860 if (abcdefgh & 0x80) {
5861 imm |= 0x80000000;
5863 if (abcdefgh & 0x40) {
5864 imm |= 0x3e000000;
5865 } else {
5866 imm |= 0x40000000;
5868 imm |= (imm << 32);
5871 break;
5874 if (cmode_3_1 != 7 && is_neg) {
5875 imm = ~imm;
5878 tcg_imm = tcg_const_i64(imm);
5879 tcg_rd = new_tmp_a64(s);
5881 for (i = 0; i < 2; i++) {
5882 int foffs = i ? fp_reg_hi_offset(s, rd) : fp_reg_offset(s, rd, MO_64);
5884 if (i == 1 && !is_q) {
5885 /* non-quad ops clear high half of vector */
5886 tcg_gen_movi_i64(tcg_rd, 0);
5887 } else if ((cmode & 0x9) == 0x1 || (cmode & 0xd) == 0x9) {
5888 tcg_gen_ld_i64(tcg_rd, cpu_env, foffs);
5889 if (is_neg) {
5890 /* AND (BIC) */
5891 tcg_gen_and_i64(tcg_rd, tcg_rd, tcg_imm);
5892 } else {
5893 /* ORR */
5894 tcg_gen_or_i64(tcg_rd, tcg_rd, tcg_imm);
5896 } else {
5897 /* MOVI */
5898 tcg_gen_mov_i64(tcg_rd, tcg_imm);
5900 tcg_gen_st_i64(tcg_rd, cpu_env, foffs);
5903 tcg_temp_free_i64(tcg_imm);
5906 /* C3.6.7 AdvSIMD scalar copy
5907 * 31 30 29 28 21 20 16 15 14 11 10 9 5 4 0
5908 * +-----+----+-----------------+------+---+------+---+------+------+
5909 * | 0 1 | op | 1 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 | Rn | Rd |
5910 * +-----+----+-----------------+------+---+------+---+------+------+
5912 static void disas_simd_scalar_copy(DisasContext *s, uint32_t insn)
5914 int rd = extract32(insn, 0, 5);
5915 int rn = extract32(insn, 5, 5);
5916 int imm4 = extract32(insn, 11, 4);
5917 int imm5 = extract32(insn, 16, 5);
5918 int op = extract32(insn, 29, 1);
5920 if (op != 0 || imm4 != 0) {
5921 unallocated_encoding(s);
5922 return;
5925 /* DUP (element, scalar) */
5926 handle_simd_dupes(s, rd, rn, imm5);
5929 /* C3.6.8 AdvSIMD scalar pairwise
5930 * 31 30 29 28 24 23 22 21 17 16 12 11 10 9 5 4 0
5931 * +-----+---+-----------+------+-----------+--------+-----+------+------+
5932 * | 0 1 | U | 1 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 | Rn | Rd |
5933 * +-----+---+-----------+------+-----------+--------+-----+------+------+
5935 static void disas_simd_scalar_pairwise(DisasContext *s, uint32_t insn)
5937 int u = extract32(insn, 29, 1);
5938 int size = extract32(insn, 22, 2);
5939 int opcode = extract32(insn, 12, 5);
5940 int rn = extract32(insn, 5, 5);
5941 int rd = extract32(insn, 0, 5);
5942 TCGv_ptr fpst;
5944 /* For some ops (the FP ones), size[1] is part of the encoding.
5945 * For ADDP strictly it is not but size[1] is always 1 for valid
5946 * encodings.
5948 opcode |= (extract32(size, 1, 1) << 5);
5950 switch (opcode) {
5951 case 0x3b: /* ADDP */
5952 if (u || size != 3) {
5953 unallocated_encoding(s);
5954 return;
5956 if (!fp_access_check(s)) {
5957 return;
5960 TCGV_UNUSED_PTR(fpst);
5961 break;
5962 case 0xc: /* FMAXNMP */
5963 case 0xd: /* FADDP */
5964 case 0xf: /* FMAXP */
5965 case 0x2c: /* FMINNMP */
5966 case 0x2f: /* FMINP */
5967 /* FP op, size[0] is 32 or 64 bit */
5968 if (!u) {
5969 unallocated_encoding(s);
5970 return;
5972 if (!fp_access_check(s)) {
5973 return;
5976 size = extract32(size, 0, 1) ? 3 : 2;
5977 fpst = get_fpstatus_ptr();
5978 break;
5979 default:
5980 unallocated_encoding(s);
5981 return;
5984 if (size == 3) {
5985 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
5986 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
5987 TCGv_i64 tcg_res = tcg_temp_new_i64();
5989 read_vec_element(s, tcg_op1, rn, 0, MO_64);
5990 read_vec_element(s, tcg_op2, rn, 1, MO_64);
5992 switch (opcode) {
5993 case 0x3b: /* ADDP */
5994 tcg_gen_add_i64(tcg_res, tcg_op1, tcg_op2);
5995 break;
5996 case 0xc: /* FMAXNMP */
5997 gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
5998 break;
5999 case 0xd: /* FADDP */
6000 gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
6001 break;
6002 case 0xf: /* FMAXP */
6003 gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
6004 break;
6005 case 0x2c: /* FMINNMP */
6006 gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
6007 break;
6008 case 0x2f: /* FMINP */
6009 gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
6010 break;
6011 default:
6012 g_assert_not_reached();
6015 write_fp_dreg(s, rd, tcg_res);
6017 tcg_temp_free_i64(tcg_op1);
6018 tcg_temp_free_i64(tcg_op2);
6019 tcg_temp_free_i64(tcg_res);
6020 } else {
6021 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
6022 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
6023 TCGv_i32 tcg_res = tcg_temp_new_i32();
6025 read_vec_element_i32(s, tcg_op1, rn, 0, MO_32);
6026 read_vec_element_i32(s, tcg_op2, rn, 1, MO_32);
6028 switch (opcode) {
6029 case 0xc: /* FMAXNMP */
6030 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
6031 break;
6032 case 0xd: /* FADDP */
6033 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
6034 break;
6035 case 0xf: /* FMAXP */
6036 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
6037 break;
6038 case 0x2c: /* FMINNMP */
6039 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
6040 break;
6041 case 0x2f: /* FMINP */
6042 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
6043 break;
6044 default:
6045 g_assert_not_reached();
6048 write_fp_sreg(s, rd, tcg_res);
6050 tcg_temp_free_i32(tcg_op1);
6051 tcg_temp_free_i32(tcg_op2);
6052 tcg_temp_free_i32(tcg_res);
6055 if (!TCGV_IS_UNUSED_PTR(fpst)) {
6056 tcg_temp_free_ptr(fpst);
6061 * Common SSHR[RA]/USHR[RA] - Shift right (optional rounding/accumulate)
6063 * This code is handles the common shifting code and is used by both
6064 * the vector and scalar code.
6066 static void handle_shri_with_rndacc(TCGv_i64 tcg_res, TCGv_i64 tcg_src,
6067 TCGv_i64 tcg_rnd, bool accumulate,
6068 bool is_u, int size, int shift)
6070 bool extended_result = false;
6071 bool round = !TCGV_IS_UNUSED_I64(tcg_rnd);
6072 int ext_lshift = 0;
6073 TCGv_i64 tcg_src_hi;
6075 if (round && size == 3) {
6076 extended_result = true;
6077 ext_lshift = 64 - shift;
6078 tcg_src_hi = tcg_temp_new_i64();
6079 } else if (shift == 64) {
6080 if (!accumulate && is_u) {
6081 /* result is zero */
6082 tcg_gen_movi_i64(tcg_res, 0);
6083 return;
6087 /* Deal with the rounding step */
6088 if (round) {
6089 if (extended_result) {
6090 TCGv_i64 tcg_zero = tcg_const_i64(0);
6091 if (!is_u) {
6092 /* take care of sign extending tcg_res */
6093 tcg_gen_sari_i64(tcg_src_hi, tcg_src, 63);
6094 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
6095 tcg_src, tcg_src_hi,
6096 tcg_rnd, tcg_zero);
6097 } else {
6098 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
6099 tcg_src, tcg_zero,
6100 tcg_rnd, tcg_zero);
6102 tcg_temp_free_i64(tcg_zero);
6103 } else {
6104 tcg_gen_add_i64(tcg_src, tcg_src, tcg_rnd);
6108 /* Now do the shift right */
6109 if (round && extended_result) {
6110 /* extended case, >64 bit precision required */
6111 if (ext_lshift == 0) {
6112 /* special case, only high bits matter */
6113 tcg_gen_mov_i64(tcg_src, tcg_src_hi);
6114 } else {
6115 tcg_gen_shri_i64(tcg_src, tcg_src, shift);
6116 tcg_gen_shli_i64(tcg_src_hi, tcg_src_hi, ext_lshift);
6117 tcg_gen_or_i64(tcg_src, tcg_src, tcg_src_hi);
6119 } else {
6120 if (is_u) {
6121 if (shift == 64) {
6122 /* essentially shifting in 64 zeros */
6123 tcg_gen_movi_i64(tcg_src, 0);
6124 } else {
6125 tcg_gen_shri_i64(tcg_src, tcg_src, shift);
6127 } else {
6128 if (shift == 64) {
6129 /* effectively extending the sign-bit */
6130 tcg_gen_sari_i64(tcg_src, tcg_src, 63);
6131 } else {
6132 tcg_gen_sari_i64(tcg_src, tcg_src, shift);
6137 if (accumulate) {
6138 tcg_gen_add_i64(tcg_res, tcg_res, tcg_src);
6139 } else {
6140 tcg_gen_mov_i64(tcg_res, tcg_src);
6143 if (extended_result) {
6144 tcg_temp_free_i64(tcg_src_hi);
6148 /* Common SHL/SLI - Shift left with an optional insert */
6149 static void handle_shli_with_ins(TCGv_i64 tcg_res, TCGv_i64 tcg_src,
6150 bool insert, int shift)
6152 if (insert) { /* SLI */
6153 tcg_gen_deposit_i64(tcg_res, tcg_res, tcg_src, shift, 64 - shift);
6154 } else { /* SHL */
6155 tcg_gen_shli_i64(tcg_res, tcg_src, shift);
6159 /* SRI: shift right with insert */
6160 static void handle_shri_with_ins(TCGv_i64 tcg_res, TCGv_i64 tcg_src,
6161 int size, int shift)
6163 int esize = 8 << size;
6165 /* shift count same as element size is valid but does nothing;
6166 * special case to avoid potential shift by 64.
6168 if (shift != esize) {
6169 tcg_gen_shri_i64(tcg_src, tcg_src, shift);
6170 tcg_gen_deposit_i64(tcg_res, tcg_res, tcg_src, 0, esize - shift);
6174 /* SSHR[RA]/USHR[RA] - Scalar shift right (optional rounding/accumulate) */
6175 static void handle_scalar_simd_shri(DisasContext *s,
6176 bool is_u, int immh, int immb,
6177 int opcode, int rn, int rd)
6179 const int size = 3;
6180 int immhb = immh << 3 | immb;
6181 int shift = 2 * (8 << size) - immhb;
6182 bool accumulate = false;
6183 bool round = false;
6184 bool insert = false;
6185 TCGv_i64 tcg_rn;
6186 TCGv_i64 tcg_rd;
6187 TCGv_i64 tcg_round;
6189 if (!extract32(immh, 3, 1)) {
6190 unallocated_encoding(s);
6191 return;
6194 if (!fp_access_check(s)) {
6195 return;
6198 switch (opcode) {
6199 case 0x02: /* SSRA / USRA (accumulate) */
6200 accumulate = true;
6201 break;
6202 case 0x04: /* SRSHR / URSHR (rounding) */
6203 round = true;
6204 break;
6205 case 0x06: /* SRSRA / URSRA (accum + rounding) */
6206 accumulate = round = true;
6207 break;
6208 case 0x08: /* SRI */
6209 insert = true;
6210 break;
6213 if (round) {
6214 uint64_t round_const = 1ULL << (shift - 1);
6215 tcg_round = tcg_const_i64(round_const);
6216 } else {
6217 TCGV_UNUSED_I64(tcg_round);
6220 tcg_rn = read_fp_dreg(s, rn);
6221 tcg_rd = (accumulate || insert) ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
6223 if (insert) {
6224 handle_shri_with_ins(tcg_rd, tcg_rn, size, shift);
6225 } else {
6226 handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
6227 accumulate, is_u, size, shift);
6230 write_fp_dreg(s, rd, tcg_rd);
6232 tcg_temp_free_i64(tcg_rn);
6233 tcg_temp_free_i64(tcg_rd);
6234 if (round) {
6235 tcg_temp_free_i64(tcg_round);
6239 /* SHL/SLI - Scalar shift left */
6240 static void handle_scalar_simd_shli(DisasContext *s, bool insert,
6241 int immh, int immb, int opcode,
6242 int rn, int rd)
6244 int size = 32 - clz32(immh) - 1;
6245 int immhb = immh << 3 | immb;
6246 int shift = immhb - (8 << size);
6247 TCGv_i64 tcg_rn = new_tmp_a64(s);
6248 TCGv_i64 tcg_rd = new_tmp_a64(s);
6250 if (!extract32(immh, 3, 1)) {
6251 unallocated_encoding(s);
6252 return;
6255 if (!fp_access_check(s)) {
6256 return;
6259 tcg_rn = read_fp_dreg(s, rn);
6260 tcg_rd = insert ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
6262 handle_shli_with_ins(tcg_rd, tcg_rn, insert, shift);
6264 write_fp_dreg(s, rd, tcg_rd);
6266 tcg_temp_free_i64(tcg_rn);
6267 tcg_temp_free_i64(tcg_rd);
6270 /* SQSHRN/SQSHRUN - Saturating (signed/unsigned) shift right with
6271 * (signed/unsigned) narrowing */
6272 static void handle_vec_simd_sqshrn(DisasContext *s, bool is_scalar, bool is_q,
6273 bool is_u_shift, bool is_u_narrow,
6274 int immh, int immb, int opcode,
6275 int rn, int rd)
6277 int immhb = immh << 3 | immb;
6278 int size = 32 - clz32(immh) - 1;
6279 int esize = 8 << size;
6280 int shift = (2 * esize) - immhb;
6281 int elements = is_scalar ? 1 : (64 / esize);
6282 bool round = extract32(opcode, 0, 1);
6283 TCGMemOp ldop = (size + 1) | (is_u_shift ? 0 : MO_SIGN);
6284 TCGv_i64 tcg_rn, tcg_rd, tcg_round;
6285 TCGv_i32 tcg_rd_narrowed;
6286 TCGv_i64 tcg_final;
6288 static NeonGenNarrowEnvFn * const signed_narrow_fns[4][2] = {
6289 { gen_helper_neon_narrow_sat_s8,
6290 gen_helper_neon_unarrow_sat8 },
6291 { gen_helper_neon_narrow_sat_s16,
6292 gen_helper_neon_unarrow_sat16 },
6293 { gen_helper_neon_narrow_sat_s32,
6294 gen_helper_neon_unarrow_sat32 },
6295 { NULL, NULL },
6297 static NeonGenNarrowEnvFn * const unsigned_narrow_fns[4] = {
6298 gen_helper_neon_narrow_sat_u8,
6299 gen_helper_neon_narrow_sat_u16,
6300 gen_helper_neon_narrow_sat_u32,
6301 NULL
6303 NeonGenNarrowEnvFn *narrowfn;
6305 int i;
6307 assert(size < 4);
6309 if (extract32(immh, 3, 1)) {
6310 unallocated_encoding(s);
6311 return;
6314 if (!fp_access_check(s)) {
6315 return;
6318 if (is_u_shift) {
6319 narrowfn = unsigned_narrow_fns[size];
6320 } else {
6321 narrowfn = signed_narrow_fns[size][is_u_narrow ? 1 : 0];
6324 tcg_rn = tcg_temp_new_i64();
6325 tcg_rd = tcg_temp_new_i64();
6326 tcg_rd_narrowed = tcg_temp_new_i32();
6327 tcg_final = tcg_const_i64(0);
6329 if (round) {
6330 uint64_t round_const = 1ULL << (shift - 1);
6331 tcg_round = tcg_const_i64(round_const);
6332 } else {
6333 TCGV_UNUSED_I64(tcg_round);
6336 for (i = 0; i < elements; i++) {
6337 read_vec_element(s, tcg_rn, rn, i, ldop);
6338 handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
6339 false, is_u_shift, size+1, shift);
6340 narrowfn(tcg_rd_narrowed, cpu_env, tcg_rd);
6341 tcg_gen_extu_i32_i64(tcg_rd, tcg_rd_narrowed);
6342 tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
6345 if (!is_q) {
6346 clear_vec_high(s, rd);
6347 write_vec_element(s, tcg_final, rd, 0, MO_64);
6348 } else {
6349 write_vec_element(s, tcg_final, rd, 1, MO_64);
6352 if (round) {
6353 tcg_temp_free_i64(tcg_round);
6355 tcg_temp_free_i64(tcg_rn);
6356 tcg_temp_free_i64(tcg_rd);
6357 tcg_temp_free_i32(tcg_rd_narrowed);
6358 tcg_temp_free_i64(tcg_final);
6359 return;
6362 /* SQSHLU, UQSHL, SQSHL: saturating left shifts */
6363 static void handle_simd_qshl(DisasContext *s, bool scalar, bool is_q,
6364 bool src_unsigned, bool dst_unsigned,
6365 int immh, int immb, int rn, int rd)
6367 int immhb = immh << 3 | immb;
6368 int size = 32 - clz32(immh) - 1;
6369 int shift = immhb - (8 << size);
6370 int pass;
6372 assert(immh != 0);
6373 assert(!(scalar && is_q));
6375 if (!scalar) {
6376 if (!is_q && extract32(immh, 3, 1)) {
6377 unallocated_encoding(s);
6378 return;
6381 /* Since we use the variable-shift helpers we must
6382 * replicate the shift count into each element of
6383 * the tcg_shift value.
6385 switch (size) {
6386 case 0:
6387 shift |= shift << 8;
6388 /* fall through */
6389 case 1:
6390 shift |= shift << 16;
6391 break;
6392 case 2:
6393 case 3:
6394 break;
6395 default:
6396 g_assert_not_reached();
6400 if (!fp_access_check(s)) {
6401 return;
6404 if (size == 3) {
6405 TCGv_i64 tcg_shift = tcg_const_i64(shift);
6406 static NeonGenTwo64OpEnvFn * const fns[2][2] = {
6407 { gen_helper_neon_qshl_s64, gen_helper_neon_qshlu_s64 },
6408 { NULL, gen_helper_neon_qshl_u64 },
6410 NeonGenTwo64OpEnvFn *genfn = fns[src_unsigned][dst_unsigned];
6411 int maxpass = is_q ? 2 : 1;
6413 for (pass = 0; pass < maxpass; pass++) {
6414 TCGv_i64 tcg_op = tcg_temp_new_i64();
6416 read_vec_element(s, tcg_op, rn, pass, MO_64);
6417 genfn(tcg_op, cpu_env, tcg_op, tcg_shift);
6418 write_vec_element(s, tcg_op, rd, pass, MO_64);
6420 tcg_temp_free_i64(tcg_op);
6422 tcg_temp_free_i64(tcg_shift);
6424 if (!is_q) {
6425 clear_vec_high(s, rd);
6427 } else {
6428 TCGv_i32 tcg_shift = tcg_const_i32(shift);
6429 static NeonGenTwoOpEnvFn * const fns[2][2][3] = {
6431 { gen_helper_neon_qshl_s8,
6432 gen_helper_neon_qshl_s16,
6433 gen_helper_neon_qshl_s32 },
6434 { gen_helper_neon_qshlu_s8,
6435 gen_helper_neon_qshlu_s16,
6436 gen_helper_neon_qshlu_s32 }
6437 }, {
6438 { NULL, NULL, NULL },
6439 { gen_helper_neon_qshl_u8,
6440 gen_helper_neon_qshl_u16,
6441 gen_helper_neon_qshl_u32 }
6444 NeonGenTwoOpEnvFn *genfn = fns[src_unsigned][dst_unsigned][size];
6445 TCGMemOp memop = scalar ? size : MO_32;
6446 int maxpass = scalar ? 1 : is_q ? 4 : 2;
6448 for (pass = 0; pass < maxpass; pass++) {
6449 TCGv_i32 tcg_op = tcg_temp_new_i32();
6451 read_vec_element_i32(s, tcg_op, rn, pass, memop);
6452 genfn(tcg_op, cpu_env, tcg_op, tcg_shift);
6453 if (scalar) {
6454 switch (size) {
6455 case 0:
6456 tcg_gen_ext8u_i32(tcg_op, tcg_op);
6457 break;
6458 case 1:
6459 tcg_gen_ext16u_i32(tcg_op, tcg_op);
6460 break;
6461 case 2:
6462 break;
6463 default:
6464 g_assert_not_reached();
6466 write_fp_sreg(s, rd, tcg_op);
6467 } else {
6468 write_vec_element_i32(s, tcg_op, rd, pass, MO_32);
6471 tcg_temp_free_i32(tcg_op);
6473 tcg_temp_free_i32(tcg_shift);
6475 if (!is_q && !scalar) {
6476 clear_vec_high(s, rd);
6481 /* Common vector code for handling integer to FP conversion */
6482 static void handle_simd_intfp_conv(DisasContext *s, int rd, int rn,
6483 int elements, int is_signed,
6484 int fracbits, int size)
6486 bool is_double = size == 3 ? true : false;
6487 TCGv_ptr tcg_fpst = get_fpstatus_ptr();
6488 TCGv_i32 tcg_shift = tcg_const_i32(fracbits);
6489 TCGv_i64 tcg_int = tcg_temp_new_i64();
6490 TCGMemOp mop = size | (is_signed ? MO_SIGN : 0);
6491 int pass;
6493 for (pass = 0; pass < elements; pass++) {
6494 read_vec_element(s, tcg_int, rn, pass, mop);
6496 if (is_double) {
6497 TCGv_i64 tcg_double = tcg_temp_new_i64();
6498 if (is_signed) {
6499 gen_helper_vfp_sqtod(tcg_double, tcg_int,
6500 tcg_shift, tcg_fpst);
6501 } else {
6502 gen_helper_vfp_uqtod(tcg_double, tcg_int,
6503 tcg_shift, tcg_fpst);
6505 if (elements == 1) {
6506 write_fp_dreg(s, rd, tcg_double);
6507 } else {
6508 write_vec_element(s, tcg_double, rd, pass, MO_64);
6510 tcg_temp_free_i64(tcg_double);
6511 } else {
6512 TCGv_i32 tcg_single = tcg_temp_new_i32();
6513 if (is_signed) {
6514 gen_helper_vfp_sqtos(tcg_single, tcg_int,
6515 tcg_shift, tcg_fpst);
6516 } else {
6517 gen_helper_vfp_uqtos(tcg_single, tcg_int,
6518 tcg_shift, tcg_fpst);
6520 if (elements == 1) {
6521 write_fp_sreg(s, rd, tcg_single);
6522 } else {
6523 write_vec_element_i32(s, tcg_single, rd, pass, MO_32);
6525 tcg_temp_free_i32(tcg_single);
6529 if (!is_double && elements == 2) {
6530 clear_vec_high(s, rd);
6533 tcg_temp_free_i64(tcg_int);
6534 tcg_temp_free_ptr(tcg_fpst);
6535 tcg_temp_free_i32(tcg_shift);
6538 /* UCVTF/SCVTF - Integer to FP conversion */
6539 static void handle_simd_shift_intfp_conv(DisasContext *s, bool is_scalar,
6540 bool is_q, bool is_u,
6541 int immh, int immb, int opcode,
6542 int rn, int rd)
6544 bool is_double = extract32(immh, 3, 1);
6545 int size = is_double ? MO_64 : MO_32;
6546 int elements;
6547 int immhb = immh << 3 | immb;
6548 int fracbits = (is_double ? 128 : 64) - immhb;
6550 if (!extract32(immh, 2, 2)) {
6551 unallocated_encoding(s);
6552 return;
6555 if (is_scalar) {
6556 elements = 1;
6557 } else {
6558 elements = is_double ? 2 : is_q ? 4 : 2;
6559 if (is_double && !is_q) {
6560 unallocated_encoding(s);
6561 return;
6565 if (!fp_access_check(s)) {
6566 return;
6569 /* immh == 0 would be a failure of the decode logic */
6570 g_assert(immh);
6572 handle_simd_intfp_conv(s, rd, rn, elements, !is_u, fracbits, size);
6575 /* FCVTZS, FVCVTZU - FP to fixedpoint conversion */
6576 static void handle_simd_shift_fpint_conv(DisasContext *s, bool is_scalar,
6577 bool is_q, bool is_u,
6578 int immh, int immb, int rn, int rd)
6580 bool is_double = extract32(immh, 3, 1);
6581 int immhb = immh << 3 | immb;
6582 int fracbits = (is_double ? 128 : 64) - immhb;
6583 int pass;
6584 TCGv_ptr tcg_fpstatus;
6585 TCGv_i32 tcg_rmode, tcg_shift;
6587 if (!extract32(immh, 2, 2)) {
6588 unallocated_encoding(s);
6589 return;
6592 if (!is_scalar && !is_q && is_double) {
6593 unallocated_encoding(s);
6594 return;
6597 if (!fp_access_check(s)) {
6598 return;
6601 assert(!(is_scalar && is_q));
6603 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(FPROUNDING_ZERO));
6604 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
6605 tcg_fpstatus = get_fpstatus_ptr();
6606 tcg_shift = tcg_const_i32(fracbits);
6608 if (is_double) {
6609 int maxpass = is_scalar ? 1 : 2;
6611 for (pass = 0; pass < maxpass; pass++) {
6612 TCGv_i64 tcg_op = tcg_temp_new_i64();
6614 read_vec_element(s, tcg_op, rn, pass, MO_64);
6615 if (is_u) {
6616 gen_helper_vfp_touqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
6617 } else {
6618 gen_helper_vfp_tosqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
6620 write_vec_element(s, tcg_op, rd, pass, MO_64);
6621 tcg_temp_free_i64(tcg_op);
6623 if (!is_q) {
6624 clear_vec_high(s, rd);
6626 } else {
6627 int maxpass = is_scalar ? 1 : is_q ? 4 : 2;
6628 for (pass = 0; pass < maxpass; pass++) {
6629 TCGv_i32 tcg_op = tcg_temp_new_i32();
6631 read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
6632 if (is_u) {
6633 gen_helper_vfp_touls(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
6634 } else {
6635 gen_helper_vfp_tosls(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
6637 if (is_scalar) {
6638 write_fp_sreg(s, rd, tcg_op);
6639 } else {
6640 write_vec_element_i32(s, tcg_op, rd, pass, MO_32);
6642 tcg_temp_free_i32(tcg_op);
6644 if (!is_q && !is_scalar) {
6645 clear_vec_high(s, rd);
6649 tcg_temp_free_ptr(tcg_fpstatus);
6650 tcg_temp_free_i32(tcg_shift);
6651 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
6652 tcg_temp_free_i32(tcg_rmode);
6655 /* C3.6.9 AdvSIMD scalar shift by immediate
6656 * 31 30 29 28 23 22 19 18 16 15 11 10 9 5 4 0
6657 * +-----+---+-------------+------+------+--------+---+------+------+
6658 * | 0 1 | U | 1 1 1 1 1 0 | immh | immb | opcode | 1 | Rn | Rd |
6659 * +-----+---+-------------+------+------+--------+---+------+------+
6661 * This is the scalar version so it works on a fixed sized registers
6663 static void disas_simd_scalar_shift_imm(DisasContext *s, uint32_t insn)
6665 int rd = extract32(insn, 0, 5);
6666 int rn = extract32(insn, 5, 5);
6667 int opcode = extract32(insn, 11, 5);
6668 int immb = extract32(insn, 16, 3);
6669 int immh = extract32(insn, 19, 4);
6670 bool is_u = extract32(insn, 29, 1);
6672 if (immh == 0) {
6673 unallocated_encoding(s);
6674 return;
6677 switch (opcode) {
6678 case 0x08: /* SRI */
6679 if (!is_u) {
6680 unallocated_encoding(s);
6681 return;
6683 /* fall through */
6684 case 0x00: /* SSHR / USHR */
6685 case 0x02: /* SSRA / USRA */
6686 case 0x04: /* SRSHR / URSHR */
6687 case 0x06: /* SRSRA / URSRA */
6688 handle_scalar_simd_shri(s, is_u, immh, immb, opcode, rn, rd);
6689 break;
6690 case 0x0a: /* SHL / SLI */
6691 handle_scalar_simd_shli(s, is_u, immh, immb, opcode, rn, rd);
6692 break;
6693 case 0x1c: /* SCVTF, UCVTF */
6694 handle_simd_shift_intfp_conv(s, true, false, is_u, immh, immb,
6695 opcode, rn, rd);
6696 break;
6697 case 0x10: /* SQSHRUN, SQSHRUN2 */
6698 case 0x11: /* SQRSHRUN, SQRSHRUN2 */
6699 if (!is_u) {
6700 unallocated_encoding(s);
6701 return;
6703 handle_vec_simd_sqshrn(s, true, false, false, true,
6704 immh, immb, opcode, rn, rd);
6705 break;
6706 case 0x12: /* SQSHRN, SQSHRN2, UQSHRN */
6707 case 0x13: /* SQRSHRN, SQRSHRN2, UQRSHRN, UQRSHRN2 */
6708 handle_vec_simd_sqshrn(s, true, false, is_u, is_u,
6709 immh, immb, opcode, rn, rd);
6710 break;
6711 case 0xc: /* SQSHLU */
6712 if (!is_u) {
6713 unallocated_encoding(s);
6714 return;
6716 handle_simd_qshl(s, true, false, false, true, immh, immb, rn, rd);
6717 break;
6718 case 0xe: /* SQSHL, UQSHL */
6719 handle_simd_qshl(s, true, false, is_u, is_u, immh, immb, rn, rd);
6720 break;
6721 case 0x1f: /* FCVTZS, FCVTZU */
6722 handle_simd_shift_fpint_conv(s, true, false, is_u, immh, immb, rn, rd);
6723 break;
6724 default:
6725 unallocated_encoding(s);
6726 break;
6730 /* C3.6.10 AdvSIMD scalar three different
6731 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 0
6732 * +-----+---+-----------+------+---+------+--------+-----+------+------+
6733 * | 0 1 | U | 1 1 1 1 0 | size | 1 | Rm | opcode | 0 0 | Rn | Rd |
6734 * +-----+---+-----------+------+---+------+--------+-----+------+------+
6736 static void disas_simd_scalar_three_reg_diff(DisasContext *s, uint32_t insn)
6738 bool is_u = extract32(insn, 29, 1);
6739 int size = extract32(insn, 22, 2);
6740 int opcode = extract32(insn, 12, 4);
6741 int rm = extract32(insn, 16, 5);
6742 int rn = extract32(insn, 5, 5);
6743 int rd = extract32(insn, 0, 5);
6745 if (is_u) {
6746 unallocated_encoding(s);
6747 return;
6750 switch (opcode) {
6751 case 0x9: /* SQDMLAL, SQDMLAL2 */
6752 case 0xb: /* SQDMLSL, SQDMLSL2 */
6753 case 0xd: /* SQDMULL, SQDMULL2 */
6754 if (size == 0 || size == 3) {
6755 unallocated_encoding(s);
6756 return;
6758 break;
6759 default:
6760 unallocated_encoding(s);
6761 return;
6764 if (!fp_access_check(s)) {
6765 return;
6768 if (size == 2) {
6769 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
6770 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
6771 TCGv_i64 tcg_res = tcg_temp_new_i64();
6773 read_vec_element(s, tcg_op1, rn, 0, MO_32 | MO_SIGN);
6774 read_vec_element(s, tcg_op2, rm, 0, MO_32 | MO_SIGN);
6776 tcg_gen_mul_i64(tcg_res, tcg_op1, tcg_op2);
6777 gen_helper_neon_addl_saturate_s64(tcg_res, cpu_env, tcg_res, tcg_res);
6779 switch (opcode) {
6780 case 0xd: /* SQDMULL, SQDMULL2 */
6781 break;
6782 case 0xb: /* SQDMLSL, SQDMLSL2 */
6783 tcg_gen_neg_i64(tcg_res, tcg_res);
6784 /* fall through */
6785 case 0x9: /* SQDMLAL, SQDMLAL2 */
6786 read_vec_element(s, tcg_op1, rd, 0, MO_64);
6787 gen_helper_neon_addl_saturate_s64(tcg_res, cpu_env,
6788 tcg_res, tcg_op1);
6789 break;
6790 default:
6791 g_assert_not_reached();
6794 write_fp_dreg(s, rd, tcg_res);
6796 tcg_temp_free_i64(tcg_op1);
6797 tcg_temp_free_i64(tcg_op2);
6798 tcg_temp_free_i64(tcg_res);
6799 } else {
6800 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
6801 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
6802 TCGv_i64 tcg_res = tcg_temp_new_i64();
6804 read_vec_element_i32(s, tcg_op1, rn, 0, MO_16);
6805 read_vec_element_i32(s, tcg_op2, rm, 0, MO_16);
6807 gen_helper_neon_mull_s16(tcg_res, tcg_op1, tcg_op2);
6808 gen_helper_neon_addl_saturate_s32(tcg_res, cpu_env, tcg_res, tcg_res);
6810 switch (opcode) {
6811 case 0xd: /* SQDMULL, SQDMULL2 */
6812 break;
6813 case 0xb: /* SQDMLSL, SQDMLSL2 */
6814 gen_helper_neon_negl_u32(tcg_res, tcg_res);
6815 /* fall through */
6816 case 0x9: /* SQDMLAL, SQDMLAL2 */
6818 TCGv_i64 tcg_op3 = tcg_temp_new_i64();
6819 read_vec_element(s, tcg_op3, rd, 0, MO_32);
6820 gen_helper_neon_addl_saturate_s32(tcg_res, cpu_env,
6821 tcg_res, tcg_op3);
6822 tcg_temp_free_i64(tcg_op3);
6823 break;
6825 default:
6826 g_assert_not_reached();
6829 tcg_gen_ext32u_i64(tcg_res, tcg_res);
6830 write_fp_dreg(s, rd, tcg_res);
6832 tcg_temp_free_i32(tcg_op1);
6833 tcg_temp_free_i32(tcg_op2);
6834 tcg_temp_free_i64(tcg_res);
6838 static void handle_3same_64(DisasContext *s, int opcode, bool u,
6839 TCGv_i64 tcg_rd, TCGv_i64 tcg_rn, TCGv_i64 tcg_rm)
6841 /* Handle 64x64->64 opcodes which are shared between the scalar
6842 * and vector 3-same groups. We cover every opcode where size == 3
6843 * is valid in either the three-reg-same (integer, not pairwise)
6844 * or scalar-three-reg-same groups. (Some opcodes are not yet
6845 * implemented.)
6847 TCGCond cond;
6849 switch (opcode) {
6850 case 0x1: /* SQADD */
6851 if (u) {
6852 gen_helper_neon_qadd_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
6853 } else {
6854 gen_helper_neon_qadd_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
6856 break;
6857 case 0x5: /* SQSUB */
6858 if (u) {
6859 gen_helper_neon_qsub_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
6860 } else {
6861 gen_helper_neon_qsub_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
6863 break;
6864 case 0x6: /* CMGT, CMHI */
6865 /* 64 bit integer comparison, result = test ? (2^64 - 1) : 0.
6866 * We implement this using setcond (test) and then negating.
6868 cond = u ? TCG_COND_GTU : TCG_COND_GT;
6869 do_cmop:
6870 tcg_gen_setcond_i64(cond, tcg_rd, tcg_rn, tcg_rm);
6871 tcg_gen_neg_i64(tcg_rd, tcg_rd);
6872 break;
6873 case 0x7: /* CMGE, CMHS */
6874 cond = u ? TCG_COND_GEU : TCG_COND_GE;
6875 goto do_cmop;
6876 case 0x11: /* CMTST, CMEQ */
6877 if (u) {
6878 cond = TCG_COND_EQ;
6879 goto do_cmop;
6881 /* CMTST : test is "if (X & Y != 0)". */
6882 tcg_gen_and_i64(tcg_rd, tcg_rn, tcg_rm);
6883 tcg_gen_setcondi_i64(TCG_COND_NE, tcg_rd, tcg_rd, 0);
6884 tcg_gen_neg_i64(tcg_rd, tcg_rd);
6885 break;
6886 case 0x8: /* SSHL, USHL */
6887 if (u) {
6888 gen_helper_neon_shl_u64(tcg_rd, tcg_rn, tcg_rm);
6889 } else {
6890 gen_helper_neon_shl_s64(tcg_rd, tcg_rn, tcg_rm);
6892 break;
6893 case 0x9: /* SQSHL, UQSHL */
6894 if (u) {
6895 gen_helper_neon_qshl_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
6896 } else {
6897 gen_helper_neon_qshl_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
6899 break;
6900 case 0xa: /* SRSHL, URSHL */
6901 if (u) {
6902 gen_helper_neon_rshl_u64(tcg_rd, tcg_rn, tcg_rm);
6903 } else {
6904 gen_helper_neon_rshl_s64(tcg_rd, tcg_rn, tcg_rm);
6906 break;
6907 case 0xb: /* SQRSHL, UQRSHL */
6908 if (u) {
6909 gen_helper_neon_qrshl_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
6910 } else {
6911 gen_helper_neon_qrshl_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
6913 break;
6914 case 0x10: /* ADD, SUB */
6915 if (u) {
6916 tcg_gen_sub_i64(tcg_rd, tcg_rn, tcg_rm);
6917 } else {
6918 tcg_gen_add_i64(tcg_rd, tcg_rn, tcg_rm);
6920 break;
6921 default:
6922 g_assert_not_reached();
6926 /* Handle the 3-same-operands float operations; shared by the scalar
6927 * and vector encodings. The caller must filter out any encodings
6928 * not allocated for the encoding it is dealing with.
6930 static void handle_3same_float(DisasContext *s, int size, int elements,
6931 int fpopcode, int rd, int rn, int rm)
6933 int pass;
6934 TCGv_ptr fpst = get_fpstatus_ptr();
6936 for (pass = 0; pass < elements; pass++) {
6937 if (size) {
6938 /* Double */
6939 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
6940 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
6941 TCGv_i64 tcg_res = tcg_temp_new_i64();
6943 read_vec_element(s, tcg_op1, rn, pass, MO_64);
6944 read_vec_element(s, tcg_op2, rm, pass, MO_64);
6946 switch (fpopcode) {
6947 case 0x39: /* FMLS */
6948 /* As usual for ARM, separate negation for fused multiply-add */
6949 gen_helper_vfp_negd(tcg_op1, tcg_op1);
6950 /* fall through */
6951 case 0x19: /* FMLA */
6952 read_vec_element(s, tcg_res, rd, pass, MO_64);
6953 gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2,
6954 tcg_res, fpst);
6955 break;
6956 case 0x18: /* FMAXNM */
6957 gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
6958 break;
6959 case 0x1a: /* FADD */
6960 gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
6961 break;
6962 case 0x1b: /* FMULX */
6963 gen_helper_vfp_mulxd(tcg_res, tcg_op1, tcg_op2, fpst);
6964 break;
6965 case 0x1c: /* FCMEQ */
6966 gen_helper_neon_ceq_f64(tcg_res, tcg_op1, tcg_op2, fpst);
6967 break;
6968 case 0x1e: /* FMAX */
6969 gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
6970 break;
6971 case 0x1f: /* FRECPS */
6972 gen_helper_recpsf_f64(tcg_res, tcg_op1, tcg_op2, fpst);
6973 break;
6974 case 0x38: /* FMINNM */
6975 gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
6976 break;
6977 case 0x3a: /* FSUB */
6978 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
6979 break;
6980 case 0x3e: /* FMIN */
6981 gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
6982 break;
6983 case 0x3f: /* FRSQRTS */
6984 gen_helper_rsqrtsf_f64(tcg_res, tcg_op1, tcg_op2, fpst);
6985 break;
6986 case 0x5b: /* FMUL */
6987 gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
6988 break;
6989 case 0x5c: /* FCMGE */
6990 gen_helper_neon_cge_f64(tcg_res, tcg_op1, tcg_op2, fpst);
6991 break;
6992 case 0x5d: /* FACGE */
6993 gen_helper_neon_acge_f64(tcg_res, tcg_op1, tcg_op2, fpst);
6994 break;
6995 case 0x5f: /* FDIV */
6996 gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
6997 break;
6998 case 0x7a: /* FABD */
6999 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
7000 gen_helper_vfp_absd(tcg_res, tcg_res);
7001 break;
7002 case 0x7c: /* FCMGT */
7003 gen_helper_neon_cgt_f64(tcg_res, tcg_op1, tcg_op2, fpst);
7004 break;
7005 case 0x7d: /* FACGT */
7006 gen_helper_neon_acgt_f64(tcg_res, tcg_op1, tcg_op2, fpst);
7007 break;
7008 default:
7009 g_assert_not_reached();
7012 write_vec_element(s, tcg_res, rd, pass, MO_64);
7014 tcg_temp_free_i64(tcg_res);
7015 tcg_temp_free_i64(tcg_op1);
7016 tcg_temp_free_i64(tcg_op2);
7017 } else {
7018 /* Single */
7019 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
7020 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
7021 TCGv_i32 tcg_res = tcg_temp_new_i32();
7023 read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
7024 read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
7026 switch (fpopcode) {
7027 case 0x39: /* FMLS */
7028 /* As usual for ARM, separate negation for fused multiply-add */
7029 gen_helper_vfp_negs(tcg_op1, tcg_op1);
7030 /* fall through */
7031 case 0x19: /* FMLA */
7032 read_vec_element_i32(s, tcg_res, rd, pass, MO_32);
7033 gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2,
7034 tcg_res, fpst);
7035 break;
7036 case 0x1a: /* FADD */
7037 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
7038 break;
7039 case 0x1b: /* FMULX */
7040 gen_helper_vfp_mulxs(tcg_res, tcg_op1, tcg_op2, fpst);
7041 break;
7042 case 0x1c: /* FCMEQ */
7043 gen_helper_neon_ceq_f32(tcg_res, tcg_op1, tcg_op2, fpst);
7044 break;
7045 case 0x1e: /* FMAX */
7046 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
7047 break;
7048 case 0x1f: /* FRECPS */
7049 gen_helper_recpsf_f32(tcg_res, tcg_op1, tcg_op2, fpst);
7050 break;
7051 case 0x18: /* FMAXNM */
7052 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
7053 break;
7054 case 0x38: /* FMINNM */
7055 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
7056 break;
7057 case 0x3a: /* FSUB */
7058 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
7059 break;
7060 case 0x3e: /* FMIN */
7061 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
7062 break;
7063 case 0x3f: /* FRSQRTS */
7064 gen_helper_rsqrtsf_f32(tcg_res, tcg_op1, tcg_op2, fpst);
7065 break;
7066 case 0x5b: /* FMUL */
7067 gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
7068 break;
7069 case 0x5c: /* FCMGE */
7070 gen_helper_neon_cge_f32(tcg_res, tcg_op1, tcg_op2, fpst);
7071 break;
7072 case 0x5d: /* FACGE */
7073 gen_helper_neon_acge_f32(tcg_res, tcg_op1, tcg_op2, fpst);
7074 break;
7075 case 0x5f: /* FDIV */
7076 gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
7077 break;
7078 case 0x7a: /* FABD */
7079 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
7080 gen_helper_vfp_abss(tcg_res, tcg_res);
7081 break;
7082 case 0x7c: /* FCMGT */
7083 gen_helper_neon_cgt_f32(tcg_res, tcg_op1, tcg_op2, fpst);
7084 break;
7085 case 0x7d: /* FACGT */
7086 gen_helper_neon_acgt_f32(tcg_res, tcg_op1, tcg_op2, fpst);
7087 break;
7088 default:
7089 g_assert_not_reached();
7092 if (elements == 1) {
7093 /* scalar single so clear high part */
7094 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
7096 tcg_gen_extu_i32_i64(tcg_tmp, tcg_res);
7097 write_vec_element(s, tcg_tmp, rd, pass, MO_64);
7098 tcg_temp_free_i64(tcg_tmp);
7099 } else {
7100 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
7103 tcg_temp_free_i32(tcg_res);
7104 tcg_temp_free_i32(tcg_op1);
7105 tcg_temp_free_i32(tcg_op2);
7109 tcg_temp_free_ptr(fpst);
7111 if ((elements << size) < 4) {
7112 /* scalar, or non-quad vector op */
7113 clear_vec_high(s, rd);
7117 /* C3.6.11 AdvSIMD scalar three same
7118 * 31 30 29 28 24 23 22 21 20 16 15 11 10 9 5 4 0
7119 * +-----+---+-----------+------+---+------+--------+---+------+------+
7120 * | 0 1 | U | 1 1 1 1 0 | size | 1 | Rm | opcode | 1 | Rn | Rd |
7121 * +-----+---+-----------+------+---+------+--------+---+------+------+
7123 static void disas_simd_scalar_three_reg_same(DisasContext *s, uint32_t insn)
7125 int rd = extract32(insn, 0, 5);
7126 int rn = extract32(insn, 5, 5);
7127 int opcode = extract32(insn, 11, 5);
7128 int rm = extract32(insn, 16, 5);
7129 int size = extract32(insn, 22, 2);
7130 bool u = extract32(insn, 29, 1);
7131 TCGv_i64 tcg_rd;
7133 if (opcode >= 0x18) {
7134 /* Floating point: U, size[1] and opcode indicate operation */
7135 int fpopcode = opcode | (extract32(size, 1, 1) << 5) | (u << 6);
7136 switch (fpopcode) {
7137 case 0x1b: /* FMULX */
7138 case 0x1f: /* FRECPS */
7139 case 0x3f: /* FRSQRTS */
7140 case 0x5d: /* FACGE */
7141 case 0x7d: /* FACGT */
7142 case 0x1c: /* FCMEQ */
7143 case 0x5c: /* FCMGE */
7144 case 0x7c: /* FCMGT */
7145 case 0x7a: /* FABD */
7146 break;
7147 default:
7148 unallocated_encoding(s);
7149 return;
7152 if (!fp_access_check(s)) {
7153 return;
7156 handle_3same_float(s, extract32(size, 0, 1), 1, fpopcode, rd, rn, rm);
7157 return;
7160 switch (opcode) {
7161 case 0x1: /* SQADD, UQADD */
7162 case 0x5: /* SQSUB, UQSUB */
7163 case 0x9: /* SQSHL, UQSHL */
7164 case 0xb: /* SQRSHL, UQRSHL */
7165 break;
7166 case 0x8: /* SSHL, USHL */
7167 case 0xa: /* SRSHL, URSHL */
7168 case 0x6: /* CMGT, CMHI */
7169 case 0x7: /* CMGE, CMHS */
7170 case 0x11: /* CMTST, CMEQ */
7171 case 0x10: /* ADD, SUB (vector) */
7172 if (size != 3) {
7173 unallocated_encoding(s);
7174 return;
7176 break;
7177 case 0x16: /* SQDMULH, SQRDMULH (vector) */
7178 if (size != 1 && size != 2) {
7179 unallocated_encoding(s);
7180 return;
7182 break;
7183 default:
7184 unallocated_encoding(s);
7185 return;
7188 if (!fp_access_check(s)) {
7189 return;
7192 tcg_rd = tcg_temp_new_i64();
7194 if (size == 3) {
7195 TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
7196 TCGv_i64 tcg_rm = read_fp_dreg(s, rm);
7198 handle_3same_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rm);
7199 tcg_temp_free_i64(tcg_rn);
7200 tcg_temp_free_i64(tcg_rm);
7201 } else {
7202 /* Do a single operation on the lowest element in the vector.
7203 * We use the standard Neon helpers and rely on 0 OP 0 == 0 with
7204 * no side effects for all these operations.
7205 * OPTME: special-purpose helpers would avoid doing some
7206 * unnecessary work in the helper for the 8 and 16 bit cases.
7208 NeonGenTwoOpEnvFn *genenvfn;
7209 TCGv_i32 tcg_rn = tcg_temp_new_i32();
7210 TCGv_i32 tcg_rm = tcg_temp_new_i32();
7211 TCGv_i32 tcg_rd32 = tcg_temp_new_i32();
7213 read_vec_element_i32(s, tcg_rn, rn, 0, size);
7214 read_vec_element_i32(s, tcg_rm, rm, 0, size);
7216 switch (opcode) {
7217 case 0x1: /* SQADD, UQADD */
7219 static NeonGenTwoOpEnvFn * const fns[3][2] = {
7220 { gen_helper_neon_qadd_s8, gen_helper_neon_qadd_u8 },
7221 { gen_helper_neon_qadd_s16, gen_helper_neon_qadd_u16 },
7222 { gen_helper_neon_qadd_s32, gen_helper_neon_qadd_u32 },
7224 genenvfn = fns[size][u];
7225 break;
7227 case 0x5: /* SQSUB, UQSUB */
7229 static NeonGenTwoOpEnvFn * const fns[3][2] = {
7230 { gen_helper_neon_qsub_s8, gen_helper_neon_qsub_u8 },
7231 { gen_helper_neon_qsub_s16, gen_helper_neon_qsub_u16 },
7232 { gen_helper_neon_qsub_s32, gen_helper_neon_qsub_u32 },
7234 genenvfn = fns[size][u];
7235 break;
7237 case 0x9: /* SQSHL, UQSHL */
7239 static NeonGenTwoOpEnvFn * const fns[3][2] = {
7240 { gen_helper_neon_qshl_s8, gen_helper_neon_qshl_u8 },
7241 { gen_helper_neon_qshl_s16, gen_helper_neon_qshl_u16 },
7242 { gen_helper_neon_qshl_s32, gen_helper_neon_qshl_u32 },
7244 genenvfn = fns[size][u];
7245 break;
7247 case 0xb: /* SQRSHL, UQRSHL */
7249 static NeonGenTwoOpEnvFn * const fns[3][2] = {
7250 { gen_helper_neon_qrshl_s8, gen_helper_neon_qrshl_u8 },
7251 { gen_helper_neon_qrshl_s16, gen_helper_neon_qrshl_u16 },
7252 { gen_helper_neon_qrshl_s32, gen_helper_neon_qrshl_u32 },
7254 genenvfn = fns[size][u];
7255 break;
7257 case 0x16: /* SQDMULH, SQRDMULH */
7259 static NeonGenTwoOpEnvFn * const fns[2][2] = {
7260 { gen_helper_neon_qdmulh_s16, gen_helper_neon_qrdmulh_s16 },
7261 { gen_helper_neon_qdmulh_s32, gen_helper_neon_qrdmulh_s32 },
7263 assert(size == 1 || size == 2);
7264 genenvfn = fns[size - 1][u];
7265 break;
7267 default:
7268 g_assert_not_reached();
7271 genenvfn(tcg_rd32, cpu_env, tcg_rn, tcg_rm);
7272 tcg_gen_extu_i32_i64(tcg_rd, tcg_rd32);
7273 tcg_temp_free_i32(tcg_rd32);
7274 tcg_temp_free_i32(tcg_rn);
7275 tcg_temp_free_i32(tcg_rm);
7278 write_fp_dreg(s, rd, tcg_rd);
7280 tcg_temp_free_i64(tcg_rd);
7283 static void handle_2misc_64(DisasContext *s, int opcode, bool u,
7284 TCGv_i64 tcg_rd, TCGv_i64 tcg_rn,
7285 TCGv_i32 tcg_rmode, TCGv_ptr tcg_fpstatus)
7287 /* Handle 64->64 opcodes which are shared between the scalar and
7288 * vector 2-reg-misc groups. We cover every integer opcode where size == 3
7289 * is valid in either group and also the double-precision fp ops.
7290 * The caller only need provide tcg_rmode and tcg_fpstatus if the op
7291 * requires them.
7293 TCGCond cond;
7295 switch (opcode) {
7296 case 0x4: /* CLS, CLZ */
7297 if (u) {
7298 gen_helper_clz64(tcg_rd, tcg_rn);
7299 } else {
7300 gen_helper_cls64(tcg_rd, tcg_rn);
7302 break;
7303 case 0x5: /* NOT */
7304 /* This opcode is shared with CNT and RBIT but we have earlier
7305 * enforced that size == 3 if and only if this is the NOT insn.
7307 tcg_gen_not_i64(tcg_rd, tcg_rn);
7308 break;
7309 case 0x7: /* SQABS, SQNEG */
7310 if (u) {
7311 gen_helper_neon_qneg_s64(tcg_rd, cpu_env, tcg_rn);
7312 } else {
7313 gen_helper_neon_qabs_s64(tcg_rd, cpu_env, tcg_rn);
7315 break;
7316 case 0xa: /* CMLT */
7317 /* 64 bit integer comparison against zero, result is
7318 * test ? (2^64 - 1) : 0. We implement via setcond(!test) and
7319 * subtracting 1.
7321 cond = TCG_COND_LT;
7322 do_cmop:
7323 tcg_gen_setcondi_i64(cond, tcg_rd, tcg_rn, 0);
7324 tcg_gen_neg_i64(tcg_rd, tcg_rd);
7325 break;
7326 case 0x8: /* CMGT, CMGE */
7327 cond = u ? TCG_COND_GE : TCG_COND_GT;
7328 goto do_cmop;
7329 case 0x9: /* CMEQ, CMLE */
7330 cond = u ? TCG_COND_LE : TCG_COND_EQ;
7331 goto do_cmop;
7332 case 0xb: /* ABS, NEG */
7333 if (u) {
7334 tcg_gen_neg_i64(tcg_rd, tcg_rn);
7335 } else {
7336 TCGv_i64 tcg_zero = tcg_const_i64(0);
7337 tcg_gen_neg_i64(tcg_rd, tcg_rn);
7338 tcg_gen_movcond_i64(TCG_COND_GT, tcg_rd, tcg_rn, tcg_zero,
7339 tcg_rn, tcg_rd);
7340 tcg_temp_free_i64(tcg_zero);
7342 break;
7343 case 0x2f: /* FABS */
7344 gen_helper_vfp_absd(tcg_rd, tcg_rn);
7345 break;
7346 case 0x6f: /* FNEG */
7347 gen_helper_vfp_negd(tcg_rd, tcg_rn);
7348 break;
7349 case 0x7f: /* FSQRT */
7350 gen_helper_vfp_sqrtd(tcg_rd, tcg_rn, cpu_env);
7351 break;
7352 case 0x1a: /* FCVTNS */
7353 case 0x1b: /* FCVTMS */
7354 case 0x1c: /* FCVTAS */
7355 case 0x3a: /* FCVTPS */
7356 case 0x3b: /* FCVTZS */
7358 TCGv_i32 tcg_shift = tcg_const_i32(0);
7359 gen_helper_vfp_tosqd(tcg_rd, tcg_rn, tcg_shift, tcg_fpstatus);
7360 tcg_temp_free_i32(tcg_shift);
7361 break;
7363 case 0x5a: /* FCVTNU */
7364 case 0x5b: /* FCVTMU */
7365 case 0x5c: /* FCVTAU */
7366 case 0x7a: /* FCVTPU */
7367 case 0x7b: /* FCVTZU */
7369 TCGv_i32 tcg_shift = tcg_const_i32(0);
7370 gen_helper_vfp_touqd(tcg_rd, tcg_rn, tcg_shift, tcg_fpstatus);
7371 tcg_temp_free_i32(tcg_shift);
7372 break;
7374 case 0x18: /* FRINTN */
7375 case 0x19: /* FRINTM */
7376 case 0x38: /* FRINTP */
7377 case 0x39: /* FRINTZ */
7378 case 0x58: /* FRINTA */
7379 case 0x79: /* FRINTI */
7380 gen_helper_rintd(tcg_rd, tcg_rn, tcg_fpstatus);
7381 break;
7382 case 0x59: /* FRINTX */
7383 gen_helper_rintd_exact(tcg_rd, tcg_rn, tcg_fpstatus);
7384 break;
7385 default:
7386 g_assert_not_reached();
7390 static void handle_2misc_fcmp_zero(DisasContext *s, int opcode,
7391 bool is_scalar, bool is_u, bool is_q,
7392 int size, int rn, int rd)
7394 bool is_double = (size == 3);
7395 TCGv_ptr fpst;
7397 if (!fp_access_check(s)) {
7398 return;
7401 fpst = get_fpstatus_ptr();
7403 if (is_double) {
7404 TCGv_i64 tcg_op = tcg_temp_new_i64();
7405 TCGv_i64 tcg_zero = tcg_const_i64(0);
7406 TCGv_i64 tcg_res = tcg_temp_new_i64();
7407 NeonGenTwoDoubleOPFn *genfn;
7408 bool swap = false;
7409 int pass;
7411 switch (opcode) {
7412 case 0x2e: /* FCMLT (zero) */
7413 swap = true;
7414 /* fallthrough */
7415 case 0x2c: /* FCMGT (zero) */
7416 genfn = gen_helper_neon_cgt_f64;
7417 break;
7418 case 0x2d: /* FCMEQ (zero) */
7419 genfn = gen_helper_neon_ceq_f64;
7420 break;
7421 case 0x6d: /* FCMLE (zero) */
7422 swap = true;
7423 /* fall through */
7424 case 0x6c: /* FCMGE (zero) */
7425 genfn = gen_helper_neon_cge_f64;
7426 break;
7427 default:
7428 g_assert_not_reached();
7431 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
7432 read_vec_element(s, tcg_op, rn, pass, MO_64);
7433 if (swap) {
7434 genfn(tcg_res, tcg_zero, tcg_op, fpst);
7435 } else {
7436 genfn(tcg_res, tcg_op, tcg_zero, fpst);
7438 write_vec_element(s, tcg_res, rd, pass, MO_64);
7440 if (is_scalar) {
7441 clear_vec_high(s, rd);
7444 tcg_temp_free_i64(tcg_res);
7445 tcg_temp_free_i64(tcg_zero);
7446 tcg_temp_free_i64(tcg_op);
7447 } else {
7448 TCGv_i32 tcg_op = tcg_temp_new_i32();
7449 TCGv_i32 tcg_zero = tcg_const_i32(0);
7450 TCGv_i32 tcg_res = tcg_temp_new_i32();
7451 NeonGenTwoSingleOPFn *genfn;
7452 bool swap = false;
7453 int pass, maxpasses;
7455 switch (opcode) {
7456 case 0x2e: /* FCMLT (zero) */
7457 swap = true;
7458 /* fall through */
7459 case 0x2c: /* FCMGT (zero) */
7460 genfn = gen_helper_neon_cgt_f32;
7461 break;
7462 case 0x2d: /* FCMEQ (zero) */
7463 genfn = gen_helper_neon_ceq_f32;
7464 break;
7465 case 0x6d: /* FCMLE (zero) */
7466 swap = true;
7467 /* fall through */
7468 case 0x6c: /* FCMGE (zero) */
7469 genfn = gen_helper_neon_cge_f32;
7470 break;
7471 default:
7472 g_assert_not_reached();
7475 if (is_scalar) {
7476 maxpasses = 1;
7477 } else {
7478 maxpasses = is_q ? 4 : 2;
7481 for (pass = 0; pass < maxpasses; pass++) {
7482 read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
7483 if (swap) {
7484 genfn(tcg_res, tcg_zero, tcg_op, fpst);
7485 } else {
7486 genfn(tcg_res, tcg_op, tcg_zero, fpst);
7488 if (is_scalar) {
7489 write_fp_sreg(s, rd, tcg_res);
7490 } else {
7491 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
7494 tcg_temp_free_i32(tcg_res);
7495 tcg_temp_free_i32(tcg_zero);
7496 tcg_temp_free_i32(tcg_op);
7497 if (!is_q && !is_scalar) {
7498 clear_vec_high(s, rd);
7502 tcg_temp_free_ptr(fpst);
7505 static void handle_2misc_reciprocal(DisasContext *s, int opcode,
7506 bool is_scalar, bool is_u, bool is_q,
7507 int size, int rn, int rd)
7509 bool is_double = (size == 3);
7510 TCGv_ptr fpst = get_fpstatus_ptr();
7512 if (is_double) {
7513 TCGv_i64 tcg_op = tcg_temp_new_i64();
7514 TCGv_i64 tcg_res = tcg_temp_new_i64();
7515 int pass;
7517 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
7518 read_vec_element(s, tcg_op, rn, pass, MO_64);
7519 switch (opcode) {
7520 case 0x3d: /* FRECPE */
7521 gen_helper_recpe_f64(tcg_res, tcg_op, fpst);
7522 break;
7523 case 0x3f: /* FRECPX */
7524 gen_helper_frecpx_f64(tcg_res, tcg_op, fpst);
7525 break;
7526 case 0x7d: /* FRSQRTE */
7527 gen_helper_rsqrte_f64(tcg_res, tcg_op, fpst);
7528 break;
7529 default:
7530 g_assert_not_reached();
7532 write_vec_element(s, tcg_res, rd, pass, MO_64);
7534 if (is_scalar) {
7535 clear_vec_high(s, rd);
7538 tcg_temp_free_i64(tcg_res);
7539 tcg_temp_free_i64(tcg_op);
7540 } else {
7541 TCGv_i32 tcg_op = tcg_temp_new_i32();
7542 TCGv_i32 tcg_res = tcg_temp_new_i32();
7543 int pass, maxpasses;
7545 if (is_scalar) {
7546 maxpasses = 1;
7547 } else {
7548 maxpasses = is_q ? 4 : 2;
7551 for (pass = 0; pass < maxpasses; pass++) {
7552 read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
7554 switch (opcode) {
7555 case 0x3c: /* URECPE */
7556 gen_helper_recpe_u32(tcg_res, tcg_op, fpst);
7557 break;
7558 case 0x3d: /* FRECPE */
7559 gen_helper_recpe_f32(tcg_res, tcg_op, fpst);
7560 break;
7561 case 0x3f: /* FRECPX */
7562 gen_helper_frecpx_f32(tcg_res, tcg_op, fpst);
7563 break;
7564 case 0x7d: /* FRSQRTE */
7565 gen_helper_rsqrte_f32(tcg_res, tcg_op, fpst);
7566 break;
7567 default:
7568 g_assert_not_reached();
7571 if (is_scalar) {
7572 write_fp_sreg(s, rd, tcg_res);
7573 } else {
7574 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
7577 tcg_temp_free_i32(tcg_res);
7578 tcg_temp_free_i32(tcg_op);
7579 if (!is_q && !is_scalar) {
7580 clear_vec_high(s, rd);
7583 tcg_temp_free_ptr(fpst);
7586 static void handle_2misc_narrow(DisasContext *s, bool scalar,
7587 int opcode, bool u, bool is_q,
7588 int size, int rn, int rd)
7590 /* Handle 2-reg-misc ops which are narrowing (so each 2*size element
7591 * in the source becomes a size element in the destination).
7593 int pass;
7594 TCGv_i32 tcg_res[2];
7595 int destelt = is_q ? 2 : 0;
7596 int passes = scalar ? 1 : 2;
7598 if (scalar) {
7599 tcg_res[1] = tcg_const_i32(0);
7602 for (pass = 0; pass < passes; pass++) {
7603 TCGv_i64 tcg_op = tcg_temp_new_i64();
7604 NeonGenNarrowFn *genfn = NULL;
7605 NeonGenNarrowEnvFn *genenvfn = NULL;
7607 if (scalar) {
7608 read_vec_element(s, tcg_op, rn, pass, size + 1);
7609 } else {
7610 read_vec_element(s, tcg_op, rn, pass, MO_64);
7612 tcg_res[pass] = tcg_temp_new_i32();
7614 switch (opcode) {
7615 case 0x12: /* XTN, SQXTUN */
7617 static NeonGenNarrowFn * const xtnfns[3] = {
7618 gen_helper_neon_narrow_u8,
7619 gen_helper_neon_narrow_u16,
7620 tcg_gen_trunc_i64_i32,
7622 static NeonGenNarrowEnvFn * const sqxtunfns[3] = {
7623 gen_helper_neon_unarrow_sat8,
7624 gen_helper_neon_unarrow_sat16,
7625 gen_helper_neon_unarrow_sat32,
7627 if (u) {
7628 genenvfn = sqxtunfns[size];
7629 } else {
7630 genfn = xtnfns[size];
7632 break;
7634 case 0x14: /* SQXTN, UQXTN */
7636 static NeonGenNarrowEnvFn * const fns[3][2] = {
7637 { gen_helper_neon_narrow_sat_s8,
7638 gen_helper_neon_narrow_sat_u8 },
7639 { gen_helper_neon_narrow_sat_s16,
7640 gen_helper_neon_narrow_sat_u16 },
7641 { gen_helper_neon_narrow_sat_s32,
7642 gen_helper_neon_narrow_sat_u32 },
7644 genenvfn = fns[size][u];
7645 break;
7647 case 0x16: /* FCVTN, FCVTN2 */
7648 /* 32 bit to 16 bit or 64 bit to 32 bit float conversion */
7649 if (size == 2) {
7650 gen_helper_vfp_fcvtsd(tcg_res[pass], tcg_op, cpu_env);
7651 } else {
7652 TCGv_i32 tcg_lo = tcg_temp_new_i32();
7653 TCGv_i32 tcg_hi = tcg_temp_new_i32();
7654 tcg_gen_trunc_i64_i32(tcg_lo, tcg_op);
7655 gen_helper_vfp_fcvt_f32_to_f16(tcg_lo, tcg_lo, cpu_env);
7656 tcg_gen_shri_i64(tcg_op, tcg_op, 32);
7657 tcg_gen_trunc_i64_i32(tcg_hi, tcg_op);
7658 gen_helper_vfp_fcvt_f32_to_f16(tcg_hi, tcg_hi, cpu_env);
7659 tcg_gen_deposit_i32(tcg_res[pass], tcg_lo, tcg_hi, 16, 16);
7660 tcg_temp_free_i32(tcg_lo);
7661 tcg_temp_free_i32(tcg_hi);
7663 break;
7664 case 0x56: /* FCVTXN, FCVTXN2 */
7665 /* 64 bit to 32 bit float conversion
7666 * with von Neumann rounding (round to odd)
7668 assert(size == 2);
7669 gen_helper_fcvtx_f64_to_f32(tcg_res[pass], tcg_op, cpu_env);
7670 break;
7671 default:
7672 g_assert_not_reached();
7675 if (genfn) {
7676 genfn(tcg_res[pass], tcg_op);
7677 } else if (genenvfn) {
7678 genenvfn(tcg_res[pass], cpu_env, tcg_op);
7681 tcg_temp_free_i64(tcg_op);
7684 for (pass = 0; pass < 2; pass++) {
7685 write_vec_element_i32(s, tcg_res[pass], rd, destelt + pass, MO_32);
7686 tcg_temp_free_i32(tcg_res[pass]);
7688 if (!is_q) {
7689 clear_vec_high(s, rd);
7693 /* Remaining saturating accumulating ops */
7694 static void handle_2misc_satacc(DisasContext *s, bool is_scalar, bool is_u,
7695 bool is_q, int size, int rn, int rd)
7697 bool is_double = (size == 3);
7699 if (is_double) {
7700 TCGv_i64 tcg_rn = tcg_temp_new_i64();
7701 TCGv_i64 tcg_rd = tcg_temp_new_i64();
7702 int pass;
7704 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
7705 read_vec_element(s, tcg_rn, rn, pass, MO_64);
7706 read_vec_element(s, tcg_rd, rd, pass, MO_64);
7708 if (is_u) { /* USQADD */
7709 gen_helper_neon_uqadd_s64(tcg_rd, cpu_env, tcg_rn, tcg_rd);
7710 } else { /* SUQADD */
7711 gen_helper_neon_sqadd_u64(tcg_rd, cpu_env, tcg_rn, tcg_rd);
7713 write_vec_element(s, tcg_rd, rd, pass, MO_64);
7715 if (is_scalar) {
7716 clear_vec_high(s, rd);
7719 tcg_temp_free_i64(tcg_rd);
7720 tcg_temp_free_i64(tcg_rn);
7721 } else {
7722 TCGv_i32 tcg_rn = tcg_temp_new_i32();
7723 TCGv_i32 tcg_rd = tcg_temp_new_i32();
7724 int pass, maxpasses;
7726 if (is_scalar) {
7727 maxpasses = 1;
7728 } else {
7729 maxpasses = is_q ? 4 : 2;
7732 for (pass = 0; pass < maxpasses; pass++) {
7733 if (is_scalar) {
7734 read_vec_element_i32(s, tcg_rn, rn, pass, size);
7735 read_vec_element_i32(s, tcg_rd, rd, pass, size);
7736 } else {
7737 read_vec_element_i32(s, tcg_rn, rn, pass, MO_32);
7738 read_vec_element_i32(s, tcg_rd, rd, pass, MO_32);
7741 if (is_u) { /* USQADD */
7742 switch (size) {
7743 case 0:
7744 gen_helper_neon_uqadd_s8(tcg_rd, cpu_env, tcg_rn, tcg_rd);
7745 break;
7746 case 1:
7747 gen_helper_neon_uqadd_s16(tcg_rd, cpu_env, tcg_rn, tcg_rd);
7748 break;
7749 case 2:
7750 gen_helper_neon_uqadd_s32(tcg_rd, cpu_env, tcg_rn, tcg_rd);
7751 break;
7752 default:
7753 g_assert_not_reached();
7755 } else { /* SUQADD */
7756 switch (size) {
7757 case 0:
7758 gen_helper_neon_sqadd_u8(tcg_rd, cpu_env, tcg_rn, tcg_rd);
7759 break;
7760 case 1:
7761 gen_helper_neon_sqadd_u16(tcg_rd, cpu_env, tcg_rn, tcg_rd);
7762 break;
7763 case 2:
7764 gen_helper_neon_sqadd_u32(tcg_rd, cpu_env, tcg_rn, tcg_rd);
7765 break;
7766 default:
7767 g_assert_not_reached();
7771 if (is_scalar) {
7772 TCGv_i64 tcg_zero = tcg_const_i64(0);
7773 write_vec_element(s, tcg_zero, rd, 0, MO_64);
7774 tcg_temp_free_i64(tcg_zero);
7776 write_vec_element_i32(s, tcg_rd, rd, pass, MO_32);
7779 if (!is_q) {
7780 clear_vec_high(s, rd);
7783 tcg_temp_free_i32(tcg_rd);
7784 tcg_temp_free_i32(tcg_rn);
7788 /* C3.6.12 AdvSIMD scalar two reg misc
7789 * 31 30 29 28 24 23 22 21 17 16 12 11 10 9 5 4 0
7790 * +-----+---+-----------+------+-----------+--------+-----+------+------+
7791 * | 0 1 | U | 1 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 | Rn | Rd |
7792 * +-----+---+-----------+------+-----------+--------+-----+------+------+
7794 static void disas_simd_scalar_two_reg_misc(DisasContext *s, uint32_t insn)
7796 int rd = extract32(insn, 0, 5);
7797 int rn = extract32(insn, 5, 5);
7798 int opcode = extract32(insn, 12, 5);
7799 int size = extract32(insn, 22, 2);
7800 bool u = extract32(insn, 29, 1);
7801 bool is_fcvt = false;
7802 int rmode;
7803 TCGv_i32 tcg_rmode;
7804 TCGv_ptr tcg_fpstatus;
7806 switch (opcode) {
7807 case 0x3: /* USQADD / SUQADD*/
7808 if (!fp_access_check(s)) {
7809 return;
7811 handle_2misc_satacc(s, true, u, false, size, rn, rd);
7812 return;
7813 case 0x7: /* SQABS / SQNEG */
7814 break;
7815 case 0xa: /* CMLT */
7816 if (u) {
7817 unallocated_encoding(s);
7818 return;
7820 /* fall through */
7821 case 0x8: /* CMGT, CMGE */
7822 case 0x9: /* CMEQ, CMLE */
7823 case 0xb: /* ABS, NEG */
7824 if (size != 3) {
7825 unallocated_encoding(s);
7826 return;
7828 break;
7829 case 0x12: /* SQXTUN */
7830 if (!u) {
7831 unallocated_encoding(s);
7832 return;
7834 /* fall through */
7835 case 0x14: /* SQXTN, UQXTN */
7836 if (size == 3) {
7837 unallocated_encoding(s);
7838 return;
7840 if (!fp_access_check(s)) {
7841 return;
7843 handle_2misc_narrow(s, true, opcode, u, false, size, rn, rd);
7844 return;
7845 case 0xc ... 0xf:
7846 case 0x16 ... 0x1d:
7847 case 0x1f:
7848 /* Floating point: U, size[1] and opcode indicate operation;
7849 * size[0] indicates single or double precision.
7851 opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
7852 size = extract32(size, 0, 1) ? 3 : 2;
7853 switch (opcode) {
7854 case 0x2c: /* FCMGT (zero) */
7855 case 0x2d: /* FCMEQ (zero) */
7856 case 0x2e: /* FCMLT (zero) */
7857 case 0x6c: /* FCMGE (zero) */
7858 case 0x6d: /* FCMLE (zero) */
7859 handle_2misc_fcmp_zero(s, opcode, true, u, true, size, rn, rd);
7860 return;
7861 case 0x1d: /* SCVTF */
7862 case 0x5d: /* UCVTF */
7864 bool is_signed = (opcode == 0x1d);
7865 if (!fp_access_check(s)) {
7866 return;
7868 handle_simd_intfp_conv(s, rd, rn, 1, is_signed, 0, size);
7869 return;
7871 case 0x3d: /* FRECPE */
7872 case 0x3f: /* FRECPX */
7873 case 0x7d: /* FRSQRTE */
7874 if (!fp_access_check(s)) {
7875 return;
7877 handle_2misc_reciprocal(s, opcode, true, u, true, size, rn, rd);
7878 return;
7879 case 0x1a: /* FCVTNS */
7880 case 0x1b: /* FCVTMS */
7881 case 0x3a: /* FCVTPS */
7882 case 0x3b: /* FCVTZS */
7883 case 0x5a: /* FCVTNU */
7884 case 0x5b: /* FCVTMU */
7885 case 0x7a: /* FCVTPU */
7886 case 0x7b: /* FCVTZU */
7887 is_fcvt = true;
7888 rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
7889 break;
7890 case 0x1c: /* FCVTAS */
7891 case 0x5c: /* FCVTAU */
7892 /* TIEAWAY doesn't fit in the usual rounding mode encoding */
7893 is_fcvt = true;
7894 rmode = FPROUNDING_TIEAWAY;
7895 break;
7896 case 0x56: /* FCVTXN, FCVTXN2 */
7897 if (size == 2) {
7898 unallocated_encoding(s);
7899 return;
7901 if (!fp_access_check(s)) {
7902 return;
7904 handle_2misc_narrow(s, true, opcode, u, false, size - 1, rn, rd);
7905 return;
7906 default:
7907 unallocated_encoding(s);
7908 return;
7910 break;
7911 default:
7912 unallocated_encoding(s);
7913 return;
7916 if (!fp_access_check(s)) {
7917 return;
7920 if (is_fcvt) {
7921 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rmode));
7922 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
7923 tcg_fpstatus = get_fpstatus_ptr();
7924 } else {
7925 TCGV_UNUSED_I32(tcg_rmode);
7926 TCGV_UNUSED_PTR(tcg_fpstatus);
7929 if (size == 3) {
7930 TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
7931 TCGv_i64 tcg_rd = tcg_temp_new_i64();
7933 handle_2misc_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rmode, tcg_fpstatus);
7934 write_fp_dreg(s, rd, tcg_rd);
7935 tcg_temp_free_i64(tcg_rd);
7936 tcg_temp_free_i64(tcg_rn);
7937 } else {
7938 TCGv_i32 tcg_rn = tcg_temp_new_i32();
7939 TCGv_i32 tcg_rd = tcg_temp_new_i32();
7941 read_vec_element_i32(s, tcg_rn, rn, 0, size);
7943 switch (opcode) {
7944 case 0x7: /* SQABS, SQNEG */
7946 NeonGenOneOpEnvFn *genfn;
7947 static NeonGenOneOpEnvFn * const fns[3][2] = {
7948 { gen_helper_neon_qabs_s8, gen_helper_neon_qneg_s8 },
7949 { gen_helper_neon_qabs_s16, gen_helper_neon_qneg_s16 },
7950 { gen_helper_neon_qabs_s32, gen_helper_neon_qneg_s32 },
7952 genfn = fns[size][u];
7953 genfn(tcg_rd, cpu_env, tcg_rn);
7954 break;
7956 case 0x1a: /* FCVTNS */
7957 case 0x1b: /* FCVTMS */
7958 case 0x1c: /* FCVTAS */
7959 case 0x3a: /* FCVTPS */
7960 case 0x3b: /* FCVTZS */
7962 TCGv_i32 tcg_shift = tcg_const_i32(0);
7963 gen_helper_vfp_tosls(tcg_rd, tcg_rn, tcg_shift, tcg_fpstatus);
7964 tcg_temp_free_i32(tcg_shift);
7965 break;
7967 case 0x5a: /* FCVTNU */
7968 case 0x5b: /* FCVTMU */
7969 case 0x5c: /* FCVTAU */
7970 case 0x7a: /* FCVTPU */
7971 case 0x7b: /* FCVTZU */
7973 TCGv_i32 tcg_shift = tcg_const_i32(0);
7974 gen_helper_vfp_touls(tcg_rd, tcg_rn, tcg_shift, tcg_fpstatus);
7975 tcg_temp_free_i32(tcg_shift);
7976 break;
7978 default:
7979 g_assert_not_reached();
7982 write_fp_sreg(s, rd, tcg_rd);
7983 tcg_temp_free_i32(tcg_rd);
7984 tcg_temp_free_i32(tcg_rn);
7987 if (is_fcvt) {
7988 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
7989 tcg_temp_free_i32(tcg_rmode);
7990 tcg_temp_free_ptr(tcg_fpstatus);
7994 /* SSHR[RA]/USHR[RA] - Vector shift right (optional rounding/accumulate) */
7995 static void handle_vec_simd_shri(DisasContext *s, bool is_q, bool is_u,
7996 int immh, int immb, int opcode, int rn, int rd)
7998 int size = 32 - clz32(immh) - 1;
7999 int immhb = immh << 3 | immb;
8000 int shift = 2 * (8 << size) - immhb;
8001 bool accumulate = false;
8002 bool round = false;
8003 bool insert = false;
8004 int dsize = is_q ? 128 : 64;
8005 int esize = 8 << size;
8006 int elements = dsize/esize;
8007 TCGMemOp memop = size | (is_u ? 0 : MO_SIGN);
8008 TCGv_i64 tcg_rn = new_tmp_a64(s);
8009 TCGv_i64 tcg_rd = new_tmp_a64(s);
8010 TCGv_i64 tcg_round;
8011 int i;
8013 if (extract32(immh, 3, 1) && !is_q) {
8014 unallocated_encoding(s);
8015 return;
8018 if (size > 3 && !is_q) {
8019 unallocated_encoding(s);
8020 return;
8023 if (!fp_access_check(s)) {
8024 return;
8027 switch (opcode) {
8028 case 0x02: /* SSRA / USRA (accumulate) */
8029 accumulate = true;
8030 break;
8031 case 0x04: /* SRSHR / URSHR (rounding) */
8032 round = true;
8033 break;
8034 case 0x06: /* SRSRA / URSRA (accum + rounding) */
8035 accumulate = round = true;
8036 break;
8037 case 0x08: /* SRI */
8038 insert = true;
8039 break;
8042 if (round) {
8043 uint64_t round_const = 1ULL << (shift - 1);
8044 tcg_round = tcg_const_i64(round_const);
8045 } else {
8046 TCGV_UNUSED_I64(tcg_round);
8049 for (i = 0; i < elements; i++) {
8050 read_vec_element(s, tcg_rn, rn, i, memop);
8051 if (accumulate || insert) {
8052 read_vec_element(s, tcg_rd, rd, i, memop);
8055 if (insert) {
8056 handle_shri_with_ins(tcg_rd, tcg_rn, size, shift);
8057 } else {
8058 handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
8059 accumulate, is_u, size, shift);
8062 write_vec_element(s, tcg_rd, rd, i, size);
8065 if (!is_q) {
8066 clear_vec_high(s, rd);
8069 if (round) {
8070 tcg_temp_free_i64(tcg_round);
8074 /* SHL/SLI - Vector shift left */
8075 static void handle_vec_simd_shli(DisasContext *s, bool is_q, bool insert,
8076 int immh, int immb, int opcode, int rn, int rd)
8078 int size = 32 - clz32(immh) - 1;
8079 int immhb = immh << 3 | immb;
8080 int shift = immhb - (8 << size);
8081 int dsize = is_q ? 128 : 64;
8082 int esize = 8 << size;
8083 int elements = dsize/esize;
8084 TCGv_i64 tcg_rn = new_tmp_a64(s);
8085 TCGv_i64 tcg_rd = new_tmp_a64(s);
8086 int i;
8088 if (extract32(immh, 3, 1) && !is_q) {
8089 unallocated_encoding(s);
8090 return;
8093 if (size > 3 && !is_q) {
8094 unallocated_encoding(s);
8095 return;
8098 if (!fp_access_check(s)) {
8099 return;
8102 for (i = 0; i < elements; i++) {
8103 read_vec_element(s, tcg_rn, rn, i, size);
8104 if (insert) {
8105 read_vec_element(s, tcg_rd, rd, i, size);
8108 handle_shli_with_ins(tcg_rd, tcg_rn, insert, shift);
8110 write_vec_element(s, tcg_rd, rd, i, size);
8113 if (!is_q) {
8114 clear_vec_high(s, rd);
8118 /* USHLL/SHLL - Vector shift left with widening */
8119 static void handle_vec_simd_wshli(DisasContext *s, bool is_q, bool is_u,
8120 int immh, int immb, int opcode, int rn, int rd)
8122 int size = 32 - clz32(immh) - 1;
8123 int immhb = immh << 3 | immb;
8124 int shift = immhb - (8 << size);
8125 int dsize = 64;
8126 int esize = 8 << size;
8127 int elements = dsize/esize;
8128 TCGv_i64 tcg_rn = new_tmp_a64(s);
8129 TCGv_i64 tcg_rd = new_tmp_a64(s);
8130 int i;
8132 if (size >= 3) {
8133 unallocated_encoding(s);
8134 return;
8137 if (!fp_access_check(s)) {
8138 return;
8141 /* For the LL variants the store is larger than the load,
8142 * so if rd == rn we would overwrite parts of our input.
8143 * So load everything right now and use shifts in the main loop.
8145 read_vec_element(s, tcg_rn, rn, is_q ? 1 : 0, MO_64);
8147 for (i = 0; i < elements; i++) {
8148 tcg_gen_shri_i64(tcg_rd, tcg_rn, i * esize);
8149 ext_and_shift_reg(tcg_rd, tcg_rd, size | (!is_u << 2), 0);
8150 tcg_gen_shli_i64(tcg_rd, tcg_rd, shift);
8151 write_vec_element(s, tcg_rd, rd, i, size + 1);
8155 /* SHRN/RSHRN - Shift right with narrowing (and potential rounding) */
8156 static void handle_vec_simd_shrn(DisasContext *s, bool is_q,
8157 int immh, int immb, int opcode, int rn, int rd)
8159 int immhb = immh << 3 | immb;
8160 int size = 32 - clz32(immh) - 1;
8161 int dsize = 64;
8162 int esize = 8 << size;
8163 int elements = dsize/esize;
8164 int shift = (2 * esize) - immhb;
8165 bool round = extract32(opcode, 0, 1);
8166 TCGv_i64 tcg_rn, tcg_rd, tcg_final;
8167 TCGv_i64 tcg_round;
8168 int i;
8170 if (extract32(immh, 3, 1)) {
8171 unallocated_encoding(s);
8172 return;
8175 if (!fp_access_check(s)) {
8176 return;
8179 tcg_rn = tcg_temp_new_i64();
8180 tcg_rd = tcg_temp_new_i64();
8181 tcg_final = tcg_temp_new_i64();
8182 read_vec_element(s, tcg_final, rd, is_q ? 1 : 0, MO_64);
8184 if (round) {
8185 uint64_t round_const = 1ULL << (shift - 1);
8186 tcg_round = tcg_const_i64(round_const);
8187 } else {
8188 TCGV_UNUSED_I64(tcg_round);
8191 for (i = 0; i < elements; i++) {
8192 read_vec_element(s, tcg_rn, rn, i, size+1);
8193 handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
8194 false, true, size+1, shift);
8196 tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
8199 if (!is_q) {
8200 clear_vec_high(s, rd);
8201 write_vec_element(s, tcg_final, rd, 0, MO_64);
8202 } else {
8203 write_vec_element(s, tcg_final, rd, 1, MO_64);
8206 if (round) {
8207 tcg_temp_free_i64(tcg_round);
8209 tcg_temp_free_i64(tcg_rn);
8210 tcg_temp_free_i64(tcg_rd);
8211 tcg_temp_free_i64(tcg_final);
8212 return;
8216 /* C3.6.14 AdvSIMD shift by immediate
8217 * 31 30 29 28 23 22 19 18 16 15 11 10 9 5 4 0
8218 * +---+---+---+-------------+------+------+--------+---+------+------+
8219 * | 0 | Q | U | 0 1 1 1 1 0 | immh | immb | opcode | 1 | Rn | Rd |
8220 * +---+---+---+-------------+------+------+--------+---+------+------+
8222 static void disas_simd_shift_imm(DisasContext *s, uint32_t insn)
8224 int rd = extract32(insn, 0, 5);
8225 int rn = extract32(insn, 5, 5);
8226 int opcode = extract32(insn, 11, 5);
8227 int immb = extract32(insn, 16, 3);
8228 int immh = extract32(insn, 19, 4);
8229 bool is_u = extract32(insn, 29, 1);
8230 bool is_q = extract32(insn, 30, 1);
8232 switch (opcode) {
8233 case 0x08: /* SRI */
8234 if (!is_u) {
8235 unallocated_encoding(s);
8236 return;
8238 /* fall through */
8239 case 0x00: /* SSHR / USHR */
8240 case 0x02: /* SSRA / USRA (accumulate) */
8241 case 0x04: /* SRSHR / URSHR (rounding) */
8242 case 0x06: /* SRSRA / URSRA (accum + rounding) */
8243 handle_vec_simd_shri(s, is_q, is_u, immh, immb, opcode, rn, rd);
8244 break;
8245 case 0x0a: /* SHL / SLI */
8246 handle_vec_simd_shli(s, is_q, is_u, immh, immb, opcode, rn, rd);
8247 break;
8248 case 0x10: /* SHRN */
8249 case 0x11: /* RSHRN / SQRSHRUN */
8250 if (is_u) {
8251 handle_vec_simd_sqshrn(s, false, is_q, false, true, immh, immb,
8252 opcode, rn, rd);
8253 } else {
8254 handle_vec_simd_shrn(s, is_q, immh, immb, opcode, rn, rd);
8256 break;
8257 case 0x12: /* SQSHRN / UQSHRN */
8258 case 0x13: /* SQRSHRN / UQRSHRN */
8259 handle_vec_simd_sqshrn(s, false, is_q, is_u, is_u, immh, immb,
8260 opcode, rn, rd);
8261 break;
8262 case 0x14: /* SSHLL / USHLL */
8263 handle_vec_simd_wshli(s, is_q, is_u, immh, immb, opcode, rn, rd);
8264 break;
8265 case 0x1c: /* SCVTF / UCVTF */
8266 handle_simd_shift_intfp_conv(s, false, is_q, is_u, immh, immb,
8267 opcode, rn, rd);
8268 break;
8269 case 0xc: /* SQSHLU */
8270 if (!is_u) {
8271 unallocated_encoding(s);
8272 return;
8274 handle_simd_qshl(s, false, is_q, false, true, immh, immb, rn, rd);
8275 break;
8276 case 0xe: /* SQSHL, UQSHL */
8277 handle_simd_qshl(s, false, is_q, is_u, is_u, immh, immb, rn, rd);
8278 break;
8279 case 0x1f: /* FCVTZS/ FCVTZU */
8280 handle_simd_shift_fpint_conv(s, false, is_q, is_u, immh, immb, rn, rd);
8281 return;
8282 default:
8283 unallocated_encoding(s);
8284 return;
8288 /* Generate code to do a "long" addition or subtraction, ie one done in
8289 * TCGv_i64 on vector lanes twice the width specified by size.
8291 static void gen_neon_addl(int size, bool is_sub, TCGv_i64 tcg_res,
8292 TCGv_i64 tcg_op1, TCGv_i64 tcg_op2)
8294 static NeonGenTwo64OpFn * const fns[3][2] = {
8295 { gen_helper_neon_addl_u16, gen_helper_neon_subl_u16 },
8296 { gen_helper_neon_addl_u32, gen_helper_neon_subl_u32 },
8297 { tcg_gen_add_i64, tcg_gen_sub_i64 },
8299 NeonGenTwo64OpFn *genfn;
8300 assert(size < 3);
8302 genfn = fns[size][is_sub];
8303 genfn(tcg_res, tcg_op1, tcg_op2);
8306 static void handle_3rd_widening(DisasContext *s, int is_q, int is_u, int size,
8307 int opcode, int rd, int rn, int rm)
8309 /* 3-reg-different widening insns: 64 x 64 -> 128 */
8310 TCGv_i64 tcg_res[2];
8311 int pass, accop;
8313 tcg_res[0] = tcg_temp_new_i64();
8314 tcg_res[1] = tcg_temp_new_i64();
8316 /* Does this op do an adding accumulate, a subtracting accumulate,
8317 * or no accumulate at all?
8319 switch (opcode) {
8320 case 5:
8321 case 8:
8322 case 9:
8323 accop = 1;
8324 break;
8325 case 10:
8326 case 11:
8327 accop = -1;
8328 break;
8329 default:
8330 accop = 0;
8331 break;
8334 if (accop != 0) {
8335 read_vec_element(s, tcg_res[0], rd, 0, MO_64);
8336 read_vec_element(s, tcg_res[1], rd, 1, MO_64);
8339 /* size == 2 means two 32x32->64 operations; this is worth special
8340 * casing because we can generally handle it inline.
8342 if (size == 2) {
8343 for (pass = 0; pass < 2; pass++) {
8344 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8345 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
8346 TCGv_i64 tcg_passres;
8347 TCGMemOp memop = MO_32 | (is_u ? 0 : MO_SIGN);
8349 int elt = pass + is_q * 2;
8351 read_vec_element(s, tcg_op1, rn, elt, memop);
8352 read_vec_element(s, tcg_op2, rm, elt, memop);
8354 if (accop == 0) {
8355 tcg_passres = tcg_res[pass];
8356 } else {
8357 tcg_passres = tcg_temp_new_i64();
8360 switch (opcode) {
8361 case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
8362 tcg_gen_add_i64(tcg_passres, tcg_op1, tcg_op2);
8363 break;
8364 case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
8365 tcg_gen_sub_i64(tcg_passres, tcg_op1, tcg_op2);
8366 break;
8367 case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
8368 case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
8370 TCGv_i64 tcg_tmp1 = tcg_temp_new_i64();
8371 TCGv_i64 tcg_tmp2 = tcg_temp_new_i64();
8373 tcg_gen_sub_i64(tcg_tmp1, tcg_op1, tcg_op2);
8374 tcg_gen_sub_i64(tcg_tmp2, tcg_op2, tcg_op1);
8375 tcg_gen_movcond_i64(is_u ? TCG_COND_GEU : TCG_COND_GE,
8376 tcg_passres,
8377 tcg_op1, tcg_op2, tcg_tmp1, tcg_tmp2);
8378 tcg_temp_free_i64(tcg_tmp1);
8379 tcg_temp_free_i64(tcg_tmp2);
8380 break;
8382 case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
8383 case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
8384 case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
8385 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
8386 break;
8387 case 9: /* SQDMLAL, SQDMLAL2 */
8388 case 11: /* SQDMLSL, SQDMLSL2 */
8389 case 13: /* SQDMULL, SQDMULL2 */
8390 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
8391 gen_helper_neon_addl_saturate_s64(tcg_passres, cpu_env,
8392 tcg_passres, tcg_passres);
8393 break;
8394 default:
8395 g_assert_not_reached();
8398 if (opcode == 9 || opcode == 11) {
8399 /* saturating accumulate ops */
8400 if (accop < 0) {
8401 tcg_gen_neg_i64(tcg_passres, tcg_passres);
8403 gen_helper_neon_addl_saturate_s64(tcg_res[pass], cpu_env,
8404 tcg_res[pass], tcg_passres);
8405 } else if (accop > 0) {
8406 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
8407 } else if (accop < 0) {
8408 tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
8411 if (accop != 0) {
8412 tcg_temp_free_i64(tcg_passres);
8415 tcg_temp_free_i64(tcg_op1);
8416 tcg_temp_free_i64(tcg_op2);
8418 } else {
8419 /* size 0 or 1, generally helper functions */
8420 for (pass = 0; pass < 2; pass++) {
8421 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
8422 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
8423 TCGv_i64 tcg_passres;
8424 int elt = pass + is_q * 2;
8426 read_vec_element_i32(s, tcg_op1, rn, elt, MO_32);
8427 read_vec_element_i32(s, tcg_op2, rm, elt, MO_32);
8429 if (accop == 0) {
8430 tcg_passres = tcg_res[pass];
8431 } else {
8432 tcg_passres = tcg_temp_new_i64();
8435 switch (opcode) {
8436 case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
8437 case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
8439 TCGv_i64 tcg_op2_64 = tcg_temp_new_i64();
8440 static NeonGenWidenFn * const widenfns[2][2] = {
8441 { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
8442 { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
8444 NeonGenWidenFn *widenfn = widenfns[size][is_u];
8446 widenfn(tcg_op2_64, tcg_op2);
8447 widenfn(tcg_passres, tcg_op1);
8448 gen_neon_addl(size, (opcode == 2), tcg_passres,
8449 tcg_passres, tcg_op2_64);
8450 tcg_temp_free_i64(tcg_op2_64);
8451 break;
8453 case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
8454 case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
8455 if (size == 0) {
8456 if (is_u) {
8457 gen_helper_neon_abdl_u16(tcg_passres, tcg_op1, tcg_op2);
8458 } else {
8459 gen_helper_neon_abdl_s16(tcg_passres, tcg_op1, tcg_op2);
8461 } else {
8462 if (is_u) {
8463 gen_helper_neon_abdl_u32(tcg_passres, tcg_op1, tcg_op2);
8464 } else {
8465 gen_helper_neon_abdl_s32(tcg_passres, tcg_op1, tcg_op2);
8468 break;
8469 case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
8470 case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
8471 case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
8472 if (size == 0) {
8473 if (is_u) {
8474 gen_helper_neon_mull_u8(tcg_passres, tcg_op1, tcg_op2);
8475 } else {
8476 gen_helper_neon_mull_s8(tcg_passres, tcg_op1, tcg_op2);
8478 } else {
8479 if (is_u) {
8480 gen_helper_neon_mull_u16(tcg_passres, tcg_op1, tcg_op2);
8481 } else {
8482 gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
8485 break;
8486 case 9: /* SQDMLAL, SQDMLAL2 */
8487 case 11: /* SQDMLSL, SQDMLSL2 */
8488 case 13: /* SQDMULL, SQDMULL2 */
8489 assert(size == 1);
8490 gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
8491 gen_helper_neon_addl_saturate_s32(tcg_passres, cpu_env,
8492 tcg_passres, tcg_passres);
8493 break;
8494 case 14: /* PMULL */
8495 assert(size == 0);
8496 gen_helper_neon_mull_p8(tcg_passres, tcg_op1, tcg_op2);
8497 break;
8498 default:
8499 g_assert_not_reached();
8501 tcg_temp_free_i32(tcg_op1);
8502 tcg_temp_free_i32(tcg_op2);
8504 if (accop != 0) {
8505 if (opcode == 9 || opcode == 11) {
8506 /* saturating accumulate ops */
8507 if (accop < 0) {
8508 gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
8510 gen_helper_neon_addl_saturate_s32(tcg_res[pass], cpu_env,
8511 tcg_res[pass],
8512 tcg_passres);
8513 } else {
8514 gen_neon_addl(size, (accop < 0), tcg_res[pass],
8515 tcg_res[pass], tcg_passres);
8517 tcg_temp_free_i64(tcg_passres);
8522 write_vec_element(s, tcg_res[0], rd, 0, MO_64);
8523 write_vec_element(s, tcg_res[1], rd, 1, MO_64);
8524 tcg_temp_free_i64(tcg_res[0]);
8525 tcg_temp_free_i64(tcg_res[1]);
8528 static void handle_3rd_wide(DisasContext *s, int is_q, int is_u, int size,
8529 int opcode, int rd, int rn, int rm)
8531 TCGv_i64 tcg_res[2];
8532 int part = is_q ? 2 : 0;
8533 int pass;
8535 for (pass = 0; pass < 2; pass++) {
8536 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8537 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
8538 TCGv_i64 tcg_op2_wide = tcg_temp_new_i64();
8539 static NeonGenWidenFn * const widenfns[3][2] = {
8540 { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
8541 { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
8542 { tcg_gen_ext_i32_i64, tcg_gen_extu_i32_i64 },
8544 NeonGenWidenFn *widenfn = widenfns[size][is_u];
8546 read_vec_element(s, tcg_op1, rn, pass, MO_64);
8547 read_vec_element_i32(s, tcg_op2, rm, part + pass, MO_32);
8548 widenfn(tcg_op2_wide, tcg_op2);
8549 tcg_temp_free_i32(tcg_op2);
8550 tcg_res[pass] = tcg_temp_new_i64();
8551 gen_neon_addl(size, (opcode == 3),
8552 tcg_res[pass], tcg_op1, tcg_op2_wide);
8553 tcg_temp_free_i64(tcg_op1);
8554 tcg_temp_free_i64(tcg_op2_wide);
8557 for (pass = 0; pass < 2; pass++) {
8558 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
8559 tcg_temp_free_i64(tcg_res[pass]);
8563 static void do_narrow_high_u32(TCGv_i32 res, TCGv_i64 in)
8565 tcg_gen_shri_i64(in, in, 32);
8566 tcg_gen_trunc_i64_i32(res, in);
8569 static void do_narrow_round_high_u32(TCGv_i32 res, TCGv_i64 in)
8571 tcg_gen_addi_i64(in, in, 1U << 31);
8572 do_narrow_high_u32(res, in);
8575 static void handle_3rd_narrowing(DisasContext *s, int is_q, int is_u, int size,
8576 int opcode, int rd, int rn, int rm)
8578 TCGv_i32 tcg_res[2];
8579 int part = is_q ? 2 : 0;
8580 int pass;
8582 for (pass = 0; pass < 2; pass++) {
8583 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8584 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
8585 TCGv_i64 tcg_wideres = tcg_temp_new_i64();
8586 static NeonGenNarrowFn * const narrowfns[3][2] = {
8587 { gen_helper_neon_narrow_high_u8,
8588 gen_helper_neon_narrow_round_high_u8 },
8589 { gen_helper_neon_narrow_high_u16,
8590 gen_helper_neon_narrow_round_high_u16 },
8591 { do_narrow_high_u32, do_narrow_round_high_u32 },
8593 NeonGenNarrowFn *gennarrow = narrowfns[size][is_u];
8595 read_vec_element(s, tcg_op1, rn, pass, MO_64);
8596 read_vec_element(s, tcg_op2, rm, pass, MO_64);
8598 gen_neon_addl(size, (opcode == 6), tcg_wideres, tcg_op1, tcg_op2);
8600 tcg_temp_free_i64(tcg_op1);
8601 tcg_temp_free_i64(tcg_op2);
8603 tcg_res[pass] = tcg_temp_new_i32();
8604 gennarrow(tcg_res[pass], tcg_wideres);
8605 tcg_temp_free_i64(tcg_wideres);
8608 for (pass = 0; pass < 2; pass++) {
8609 write_vec_element_i32(s, tcg_res[pass], rd, pass + part, MO_32);
8610 tcg_temp_free_i32(tcg_res[pass]);
8612 if (!is_q) {
8613 clear_vec_high(s, rd);
8617 static void handle_pmull_64(DisasContext *s, int is_q, int rd, int rn, int rm)
8619 /* PMULL of 64 x 64 -> 128 is an odd special case because it
8620 * is the only three-reg-diff instruction which produces a
8621 * 128-bit wide result from a single operation. However since
8622 * it's possible to calculate the two halves more or less
8623 * separately we just use two helper calls.
8625 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8626 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
8627 TCGv_i64 tcg_res = tcg_temp_new_i64();
8629 read_vec_element(s, tcg_op1, rn, is_q, MO_64);
8630 read_vec_element(s, tcg_op2, rm, is_q, MO_64);
8631 gen_helper_neon_pmull_64_lo(tcg_res, tcg_op1, tcg_op2);
8632 write_vec_element(s, tcg_res, rd, 0, MO_64);
8633 gen_helper_neon_pmull_64_hi(tcg_res, tcg_op1, tcg_op2);
8634 write_vec_element(s, tcg_res, rd, 1, MO_64);
8636 tcg_temp_free_i64(tcg_op1);
8637 tcg_temp_free_i64(tcg_op2);
8638 tcg_temp_free_i64(tcg_res);
8641 /* C3.6.15 AdvSIMD three different
8642 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 0
8643 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
8644 * | 0 | Q | U | 0 1 1 1 0 | size | 1 | Rm | opcode | 0 0 | Rn | Rd |
8645 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
8647 static void disas_simd_three_reg_diff(DisasContext *s, uint32_t insn)
8649 /* Instructions in this group fall into three basic classes
8650 * (in each case with the operation working on each element in
8651 * the input vectors):
8652 * (1) widening 64 x 64 -> 128 (with possibly Vd as an extra
8653 * 128 bit input)
8654 * (2) wide 64 x 128 -> 128
8655 * (3) narrowing 128 x 128 -> 64
8656 * Here we do initial decode, catch unallocated cases and
8657 * dispatch to separate functions for each class.
8659 int is_q = extract32(insn, 30, 1);
8660 int is_u = extract32(insn, 29, 1);
8661 int size = extract32(insn, 22, 2);
8662 int opcode = extract32(insn, 12, 4);
8663 int rm = extract32(insn, 16, 5);
8664 int rn = extract32(insn, 5, 5);
8665 int rd = extract32(insn, 0, 5);
8667 switch (opcode) {
8668 case 1: /* SADDW, SADDW2, UADDW, UADDW2 */
8669 case 3: /* SSUBW, SSUBW2, USUBW, USUBW2 */
8670 /* 64 x 128 -> 128 */
8671 if (size == 3) {
8672 unallocated_encoding(s);
8673 return;
8675 if (!fp_access_check(s)) {
8676 return;
8678 handle_3rd_wide(s, is_q, is_u, size, opcode, rd, rn, rm);
8679 break;
8680 case 4: /* ADDHN, ADDHN2, RADDHN, RADDHN2 */
8681 case 6: /* SUBHN, SUBHN2, RSUBHN, RSUBHN2 */
8682 /* 128 x 128 -> 64 */
8683 if (size == 3) {
8684 unallocated_encoding(s);
8685 return;
8687 if (!fp_access_check(s)) {
8688 return;
8690 handle_3rd_narrowing(s, is_q, is_u, size, opcode, rd, rn, rm);
8691 break;
8692 case 14: /* PMULL, PMULL2 */
8693 if (is_u || size == 1 || size == 2) {
8694 unallocated_encoding(s);
8695 return;
8697 if (size == 3) {
8698 if (!arm_dc_feature(s, ARM_FEATURE_V8_PMULL)) {
8699 unallocated_encoding(s);
8700 return;
8702 if (!fp_access_check(s)) {
8703 return;
8705 handle_pmull_64(s, is_q, rd, rn, rm);
8706 return;
8708 goto is_widening;
8709 case 9: /* SQDMLAL, SQDMLAL2 */
8710 case 11: /* SQDMLSL, SQDMLSL2 */
8711 case 13: /* SQDMULL, SQDMULL2 */
8712 if (is_u || size == 0) {
8713 unallocated_encoding(s);
8714 return;
8716 /* fall through */
8717 case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
8718 case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
8719 case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
8720 case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
8721 case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
8722 case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
8723 case 12: /* SMULL, SMULL2, UMULL, UMULL2 */
8724 /* 64 x 64 -> 128 */
8725 if (size == 3) {
8726 unallocated_encoding(s);
8727 return;
8729 is_widening:
8730 if (!fp_access_check(s)) {
8731 return;
8734 handle_3rd_widening(s, is_q, is_u, size, opcode, rd, rn, rm);
8735 break;
8736 default:
8737 /* opcode 15 not allocated */
8738 unallocated_encoding(s);
8739 break;
8743 /* Logic op (opcode == 3) subgroup of C3.6.16. */
8744 static void disas_simd_3same_logic(DisasContext *s, uint32_t insn)
8746 int rd = extract32(insn, 0, 5);
8747 int rn = extract32(insn, 5, 5);
8748 int rm = extract32(insn, 16, 5);
8749 int size = extract32(insn, 22, 2);
8750 bool is_u = extract32(insn, 29, 1);
8751 bool is_q = extract32(insn, 30, 1);
8752 TCGv_i64 tcg_op1, tcg_op2, tcg_res[2];
8753 int pass;
8755 if (!fp_access_check(s)) {
8756 return;
8759 tcg_op1 = tcg_temp_new_i64();
8760 tcg_op2 = tcg_temp_new_i64();
8761 tcg_res[0] = tcg_temp_new_i64();
8762 tcg_res[1] = tcg_temp_new_i64();
8764 for (pass = 0; pass < (is_q ? 2 : 1); pass++) {
8765 read_vec_element(s, tcg_op1, rn, pass, MO_64);
8766 read_vec_element(s, tcg_op2, rm, pass, MO_64);
8768 if (!is_u) {
8769 switch (size) {
8770 case 0: /* AND */
8771 tcg_gen_and_i64(tcg_res[pass], tcg_op1, tcg_op2);
8772 break;
8773 case 1: /* BIC */
8774 tcg_gen_andc_i64(tcg_res[pass], tcg_op1, tcg_op2);
8775 break;
8776 case 2: /* ORR */
8777 tcg_gen_or_i64(tcg_res[pass], tcg_op1, tcg_op2);
8778 break;
8779 case 3: /* ORN */
8780 tcg_gen_orc_i64(tcg_res[pass], tcg_op1, tcg_op2);
8781 break;
8783 } else {
8784 if (size != 0) {
8785 /* B* ops need res loaded to operate on */
8786 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
8789 switch (size) {
8790 case 0: /* EOR */
8791 tcg_gen_xor_i64(tcg_res[pass], tcg_op1, tcg_op2);
8792 break;
8793 case 1: /* BSL bitwise select */
8794 tcg_gen_xor_i64(tcg_op1, tcg_op1, tcg_op2);
8795 tcg_gen_and_i64(tcg_op1, tcg_op1, tcg_res[pass]);
8796 tcg_gen_xor_i64(tcg_res[pass], tcg_op2, tcg_op1);
8797 break;
8798 case 2: /* BIT, bitwise insert if true */
8799 tcg_gen_xor_i64(tcg_op1, tcg_op1, tcg_res[pass]);
8800 tcg_gen_and_i64(tcg_op1, tcg_op1, tcg_op2);
8801 tcg_gen_xor_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
8802 break;
8803 case 3: /* BIF, bitwise insert if false */
8804 tcg_gen_xor_i64(tcg_op1, tcg_op1, tcg_res[pass]);
8805 tcg_gen_andc_i64(tcg_op1, tcg_op1, tcg_op2);
8806 tcg_gen_xor_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
8807 break;
8812 write_vec_element(s, tcg_res[0], rd, 0, MO_64);
8813 if (!is_q) {
8814 tcg_gen_movi_i64(tcg_res[1], 0);
8816 write_vec_element(s, tcg_res[1], rd, 1, MO_64);
8818 tcg_temp_free_i64(tcg_op1);
8819 tcg_temp_free_i64(tcg_op2);
8820 tcg_temp_free_i64(tcg_res[0]);
8821 tcg_temp_free_i64(tcg_res[1]);
8824 /* Helper functions for 32 bit comparisons */
8825 static void gen_max_s32(TCGv_i32 res, TCGv_i32 op1, TCGv_i32 op2)
8827 tcg_gen_movcond_i32(TCG_COND_GE, res, op1, op2, op1, op2);
8830 static void gen_max_u32(TCGv_i32 res, TCGv_i32 op1, TCGv_i32 op2)
8832 tcg_gen_movcond_i32(TCG_COND_GEU, res, op1, op2, op1, op2);
8835 static void gen_min_s32(TCGv_i32 res, TCGv_i32 op1, TCGv_i32 op2)
8837 tcg_gen_movcond_i32(TCG_COND_LE, res, op1, op2, op1, op2);
8840 static void gen_min_u32(TCGv_i32 res, TCGv_i32 op1, TCGv_i32 op2)
8842 tcg_gen_movcond_i32(TCG_COND_LEU, res, op1, op2, op1, op2);
8845 /* Pairwise op subgroup of C3.6.16.
8847 * This is called directly or via the handle_3same_float for float pairwise
8848 * operations where the opcode and size are calculated differently.
8850 static void handle_simd_3same_pair(DisasContext *s, int is_q, int u, int opcode,
8851 int size, int rn, int rm, int rd)
8853 TCGv_ptr fpst;
8854 int pass;
8856 /* Floating point operations need fpst */
8857 if (opcode >= 0x58) {
8858 fpst = get_fpstatus_ptr();
8859 } else {
8860 TCGV_UNUSED_PTR(fpst);
8863 if (!fp_access_check(s)) {
8864 return;
8867 /* These operations work on the concatenated rm:rn, with each pair of
8868 * adjacent elements being operated on to produce an element in the result.
8870 if (size == 3) {
8871 TCGv_i64 tcg_res[2];
8873 for (pass = 0; pass < 2; pass++) {
8874 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8875 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
8876 int passreg = (pass == 0) ? rn : rm;
8878 read_vec_element(s, tcg_op1, passreg, 0, MO_64);
8879 read_vec_element(s, tcg_op2, passreg, 1, MO_64);
8880 tcg_res[pass] = tcg_temp_new_i64();
8882 switch (opcode) {
8883 case 0x17: /* ADDP */
8884 tcg_gen_add_i64(tcg_res[pass], tcg_op1, tcg_op2);
8885 break;
8886 case 0x58: /* FMAXNMP */
8887 gen_helper_vfp_maxnumd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
8888 break;
8889 case 0x5a: /* FADDP */
8890 gen_helper_vfp_addd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
8891 break;
8892 case 0x5e: /* FMAXP */
8893 gen_helper_vfp_maxd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
8894 break;
8895 case 0x78: /* FMINNMP */
8896 gen_helper_vfp_minnumd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
8897 break;
8898 case 0x7e: /* FMINP */
8899 gen_helper_vfp_mind(tcg_res[pass], tcg_op1, tcg_op2, fpst);
8900 break;
8901 default:
8902 g_assert_not_reached();
8905 tcg_temp_free_i64(tcg_op1);
8906 tcg_temp_free_i64(tcg_op2);
8909 for (pass = 0; pass < 2; pass++) {
8910 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
8911 tcg_temp_free_i64(tcg_res[pass]);
8913 } else {
8914 int maxpass = is_q ? 4 : 2;
8915 TCGv_i32 tcg_res[4];
8917 for (pass = 0; pass < maxpass; pass++) {
8918 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
8919 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
8920 NeonGenTwoOpFn *genfn = NULL;
8921 int passreg = pass < (maxpass / 2) ? rn : rm;
8922 int passelt = (is_q && (pass & 1)) ? 2 : 0;
8924 read_vec_element_i32(s, tcg_op1, passreg, passelt, MO_32);
8925 read_vec_element_i32(s, tcg_op2, passreg, passelt + 1, MO_32);
8926 tcg_res[pass] = tcg_temp_new_i32();
8928 switch (opcode) {
8929 case 0x17: /* ADDP */
8931 static NeonGenTwoOpFn * const fns[3] = {
8932 gen_helper_neon_padd_u8,
8933 gen_helper_neon_padd_u16,
8934 tcg_gen_add_i32,
8936 genfn = fns[size];
8937 break;
8939 case 0x14: /* SMAXP, UMAXP */
8941 static NeonGenTwoOpFn * const fns[3][2] = {
8942 { gen_helper_neon_pmax_s8, gen_helper_neon_pmax_u8 },
8943 { gen_helper_neon_pmax_s16, gen_helper_neon_pmax_u16 },
8944 { gen_max_s32, gen_max_u32 },
8946 genfn = fns[size][u];
8947 break;
8949 case 0x15: /* SMINP, UMINP */
8951 static NeonGenTwoOpFn * const fns[3][2] = {
8952 { gen_helper_neon_pmin_s8, gen_helper_neon_pmin_u8 },
8953 { gen_helper_neon_pmin_s16, gen_helper_neon_pmin_u16 },
8954 { gen_min_s32, gen_min_u32 },
8956 genfn = fns[size][u];
8957 break;
8959 /* The FP operations are all on single floats (32 bit) */
8960 case 0x58: /* FMAXNMP */
8961 gen_helper_vfp_maxnums(tcg_res[pass], tcg_op1, tcg_op2, fpst);
8962 break;
8963 case 0x5a: /* FADDP */
8964 gen_helper_vfp_adds(tcg_res[pass], tcg_op1, tcg_op2, fpst);
8965 break;
8966 case 0x5e: /* FMAXP */
8967 gen_helper_vfp_maxs(tcg_res[pass], tcg_op1, tcg_op2, fpst);
8968 break;
8969 case 0x78: /* FMINNMP */
8970 gen_helper_vfp_minnums(tcg_res[pass], tcg_op1, tcg_op2, fpst);
8971 break;
8972 case 0x7e: /* FMINP */
8973 gen_helper_vfp_mins(tcg_res[pass], tcg_op1, tcg_op2, fpst);
8974 break;
8975 default:
8976 g_assert_not_reached();
8979 /* FP ops called directly, otherwise call now */
8980 if (genfn) {
8981 genfn(tcg_res[pass], tcg_op1, tcg_op2);
8984 tcg_temp_free_i32(tcg_op1);
8985 tcg_temp_free_i32(tcg_op2);
8988 for (pass = 0; pass < maxpass; pass++) {
8989 write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_32);
8990 tcg_temp_free_i32(tcg_res[pass]);
8992 if (!is_q) {
8993 clear_vec_high(s, rd);
8997 if (!TCGV_IS_UNUSED_PTR(fpst)) {
8998 tcg_temp_free_ptr(fpst);
9002 /* Floating point op subgroup of C3.6.16. */
9003 static void disas_simd_3same_float(DisasContext *s, uint32_t insn)
9005 /* For floating point ops, the U, size[1] and opcode bits
9006 * together indicate the operation. size[0] indicates single
9007 * or double.
9009 int fpopcode = extract32(insn, 11, 5)
9010 | (extract32(insn, 23, 1) << 5)
9011 | (extract32(insn, 29, 1) << 6);
9012 int is_q = extract32(insn, 30, 1);
9013 int size = extract32(insn, 22, 1);
9014 int rm = extract32(insn, 16, 5);
9015 int rn = extract32(insn, 5, 5);
9016 int rd = extract32(insn, 0, 5);
9018 int datasize = is_q ? 128 : 64;
9019 int esize = 32 << size;
9020 int elements = datasize / esize;
9022 if (size == 1 && !is_q) {
9023 unallocated_encoding(s);
9024 return;
9027 switch (fpopcode) {
9028 case 0x58: /* FMAXNMP */
9029 case 0x5a: /* FADDP */
9030 case 0x5e: /* FMAXP */
9031 case 0x78: /* FMINNMP */
9032 case 0x7e: /* FMINP */
9033 if (size && !is_q) {
9034 unallocated_encoding(s);
9035 return;
9037 handle_simd_3same_pair(s, is_q, 0, fpopcode, size ? MO_64 : MO_32,
9038 rn, rm, rd);
9039 return;
9040 case 0x1b: /* FMULX */
9041 case 0x1f: /* FRECPS */
9042 case 0x3f: /* FRSQRTS */
9043 case 0x5d: /* FACGE */
9044 case 0x7d: /* FACGT */
9045 case 0x19: /* FMLA */
9046 case 0x39: /* FMLS */
9047 case 0x18: /* FMAXNM */
9048 case 0x1a: /* FADD */
9049 case 0x1c: /* FCMEQ */
9050 case 0x1e: /* FMAX */
9051 case 0x38: /* FMINNM */
9052 case 0x3a: /* FSUB */
9053 case 0x3e: /* FMIN */
9054 case 0x5b: /* FMUL */
9055 case 0x5c: /* FCMGE */
9056 case 0x5f: /* FDIV */
9057 case 0x7a: /* FABD */
9058 case 0x7c: /* FCMGT */
9059 if (!fp_access_check(s)) {
9060 return;
9063 handle_3same_float(s, size, elements, fpopcode, rd, rn, rm);
9064 return;
9065 default:
9066 unallocated_encoding(s);
9067 return;
9071 /* Integer op subgroup of C3.6.16. */
9072 static void disas_simd_3same_int(DisasContext *s, uint32_t insn)
9074 int is_q = extract32(insn, 30, 1);
9075 int u = extract32(insn, 29, 1);
9076 int size = extract32(insn, 22, 2);
9077 int opcode = extract32(insn, 11, 5);
9078 int rm = extract32(insn, 16, 5);
9079 int rn = extract32(insn, 5, 5);
9080 int rd = extract32(insn, 0, 5);
9081 int pass;
9083 switch (opcode) {
9084 case 0x13: /* MUL, PMUL */
9085 if (u && size != 0) {
9086 unallocated_encoding(s);
9087 return;
9089 /* fall through */
9090 case 0x0: /* SHADD, UHADD */
9091 case 0x2: /* SRHADD, URHADD */
9092 case 0x4: /* SHSUB, UHSUB */
9093 case 0xc: /* SMAX, UMAX */
9094 case 0xd: /* SMIN, UMIN */
9095 case 0xe: /* SABD, UABD */
9096 case 0xf: /* SABA, UABA */
9097 case 0x12: /* MLA, MLS */
9098 if (size == 3) {
9099 unallocated_encoding(s);
9100 return;
9102 break;
9103 case 0x16: /* SQDMULH, SQRDMULH */
9104 if (size == 0 || size == 3) {
9105 unallocated_encoding(s);
9106 return;
9108 break;
9109 default:
9110 if (size == 3 && !is_q) {
9111 unallocated_encoding(s);
9112 return;
9114 break;
9117 if (!fp_access_check(s)) {
9118 return;
9121 if (size == 3) {
9122 assert(is_q);
9123 for (pass = 0; pass < 2; pass++) {
9124 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
9125 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
9126 TCGv_i64 tcg_res = tcg_temp_new_i64();
9128 read_vec_element(s, tcg_op1, rn, pass, MO_64);
9129 read_vec_element(s, tcg_op2, rm, pass, MO_64);
9131 handle_3same_64(s, opcode, u, tcg_res, tcg_op1, tcg_op2);
9133 write_vec_element(s, tcg_res, rd, pass, MO_64);
9135 tcg_temp_free_i64(tcg_res);
9136 tcg_temp_free_i64(tcg_op1);
9137 tcg_temp_free_i64(tcg_op2);
9139 } else {
9140 for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
9141 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
9142 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
9143 TCGv_i32 tcg_res = tcg_temp_new_i32();
9144 NeonGenTwoOpFn *genfn = NULL;
9145 NeonGenTwoOpEnvFn *genenvfn = NULL;
9147 read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
9148 read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
9150 switch (opcode) {
9151 case 0x0: /* SHADD, UHADD */
9153 static NeonGenTwoOpFn * const fns[3][2] = {
9154 { gen_helper_neon_hadd_s8, gen_helper_neon_hadd_u8 },
9155 { gen_helper_neon_hadd_s16, gen_helper_neon_hadd_u16 },
9156 { gen_helper_neon_hadd_s32, gen_helper_neon_hadd_u32 },
9158 genfn = fns[size][u];
9159 break;
9161 case 0x1: /* SQADD, UQADD */
9163 static NeonGenTwoOpEnvFn * const fns[3][2] = {
9164 { gen_helper_neon_qadd_s8, gen_helper_neon_qadd_u8 },
9165 { gen_helper_neon_qadd_s16, gen_helper_neon_qadd_u16 },
9166 { gen_helper_neon_qadd_s32, gen_helper_neon_qadd_u32 },
9168 genenvfn = fns[size][u];
9169 break;
9171 case 0x2: /* SRHADD, URHADD */
9173 static NeonGenTwoOpFn * const fns[3][2] = {
9174 { gen_helper_neon_rhadd_s8, gen_helper_neon_rhadd_u8 },
9175 { gen_helper_neon_rhadd_s16, gen_helper_neon_rhadd_u16 },
9176 { gen_helper_neon_rhadd_s32, gen_helper_neon_rhadd_u32 },
9178 genfn = fns[size][u];
9179 break;
9181 case 0x4: /* SHSUB, UHSUB */
9183 static NeonGenTwoOpFn * const fns[3][2] = {
9184 { gen_helper_neon_hsub_s8, gen_helper_neon_hsub_u8 },
9185 { gen_helper_neon_hsub_s16, gen_helper_neon_hsub_u16 },
9186 { gen_helper_neon_hsub_s32, gen_helper_neon_hsub_u32 },
9188 genfn = fns[size][u];
9189 break;
9191 case 0x5: /* SQSUB, UQSUB */
9193 static NeonGenTwoOpEnvFn * const fns[3][2] = {
9194 { gen_helper_neon_qsub_s8, gen_helper_neon_qsub_u8 },
9195 { gen_helper_neon_qsub_s16, gen_helper_neon_qsub_u16 },
9196 { gen_helper_neon_qsub_s32, gen_helper_neon_qsub_u32 },
9198 genenvfn = fns[size][u];
9199 break;
9201 case 0x6: /* CMGT, CMHI */
9203 static NeonGenTwoOpFn * const fns[3][2] = {
9204 { gen_helper_neon_cgt_s8, gen_helper_neon_cgt_u8 },
9205 { gen_helper_neon_cgt_s16, gen_helper_neon_cgt_u16 },
9206 { gen_helper_neon_cgt_s32, gen_helper_neon_cgt_u32 },
9208 genfn = fns[size][u];
9209 break;
9211 case 0x7: /* CMGE, CMHS */
9213 static NeonGenTwoOpFn * const fns[3][2] = {
9214 { gen_helper_neon_cge_s8, gen_helper_neon_cge_u8 },
9215 { gen_helper_neon_cge_s16, gen_helper_neon_cge_u16 },
9216 { gen_helper_neon_cge_s32, gen_helper_neon_cge_u32 },
9218 genfn = fns[size][u];
9219 break;
9221 case 0x8: /* SSHL, USHL */
9223 static NeonGenTwoOpFn * const fns[3][2] = {
9224 { gen_helper_neon_shl_s8, gen_helper_neon_shl_u8 },
9225 { gen_helper_neon_shl_s16, gen_helper_neon_shl_u16 },
9226 { gen_helper_neon_shl_s32, gen_helper_neon_shl_u32 },
9228 genfn = fns[size][u];
9229 break;
9231 case 0x9: /* SQSHL, UQSHL */
9233 static NeonGenTwoOpEnvFn * const fns[3][2] = {
9234 { gen_helper_neon_qshl_s8, gen_helper_neon_qshl_u8 },
9235 { gen_helper_neon_qshl_s16, gen_helper_neon_qshl_u16 },
9236 { gen_helper_neon_qshl_s32, gen_helper_neon_qshl_u32 },
9238 genenvfn = fns[size][u];
9239 break;
9241 case 0xa: /* SRSHL, URSHL */
9243 static NeonGenTwoOpFn * const fns[3][2] = {
9244 { gen_helper_neon_rshl_s8, gen_helper_neon_rshl_u8 },
9245 { gen_helper_neon_rshl_s16, gen_helper_neon_rshl_u16 },
9246 { gen_helper_neon_rshl_s32, gen_helper_neon_rshl_u32 },
9248 genfn = fns[size][u];
9249 break;
9251 case 0xb: /* SQRSHL, UQRSHL */
9253 static NeonGenTwoOpEnvFn * const fns[3][2] = {
9254 { gen_helper_neon_qrshl_s8, gen_helper_neon_qrshl_u8 },
9255 { gen_helper_neon_qrshl_s16, gen_helper_neon_qrshl_u16 },
9256 { gen_helper_neon_qrshl_s32, gen_helper_neon_qrshl_u32 },
9258 genenvfn = fns[size][u];
9259 break;
9261 case 0xc: /* SMAX, UMAX */
9263 static NeonGenTwoOpFn * const fns[3][2] = {
9264 { gen_helper_neon_max_s8, gen_helper_neon_max_u8 },
9265 { gen_helper_neon_max_s16, gen_helper_neon_max_u16 },
9266 { gen_max_s32, gen_max_u32 },
9268 genfn = fns[size][u];
9269 break;
9272 case 0xd: /* SMIN, UMIN */
9274 static NeonGenTwoOpFn * const fns[3][2] = {
9275 { gen_helper_neon_min_s8, gen_helper_neon_min_u8 },
9276 { gen_helper_neon_min_s16, gen_helper_neon_min_u16 },
9277 { gen_min_s32, gen_min_u32 },
9279 genfn = fns[size][u];
9280 break;
9282 case 0xe: /* SABD, UABD */
9283 case 0xf: /* SABA, UABA */
9285 static NeonGenTwoOpFn * const fns[3][2] = {
9286 { gen_helper_neon_abd_s8, gen_helper_neon_abd_u8 },
9287 { gen_helper_neon_abd_s16, gen_helper_neon_abd_u16 },
9288 { gen_helper_neon_abd_s32, gen_helper_neon_abd_u32 },
9290 genfn = fns[size][u];
9291 break;
9293 case 0x10: /* ADD, SUB */
9295 static NeonGenTwoOpFn * const fns[3][2] = {
9296 { gen_helper_neon_add_u8, gen_helper_neon_sub_u8 },
9297 { gen_helper_neon_add_u16, gen_helper_neon_sub_u16 },
9298 { tcg_gen_add_i32, tcg_gen_sub_i32 },
9300 genfn = fns[size][u];
9301 break;
9303 case 0x11: /* CMTST, CMEQ */
9305 static NeonGenTwoOpFn * const fns[3][2] = {
9306 { gen_helper_neon_tst_u8, gen_helper_neon_ceq_u8 },
9307 { gen_helper_neon_tst_u16, gen_helper_neon_ceq_u16 },
9308 { gen_helper_neon_tst_u32, gen_helper_neon_ceq_u32 },
9310 genfn = fns[size][u];
9311 break;
9313 case 0x13: /* MUL, PMUL */
9314 if (u) {
9315 /* PMUL */
9316 assert(size == 0);
9317 genfn = gen_helper_neon_mul_p8;
9318 break;
9320 /* fall through : MUL */
9321 case 0x12: /* MLA, MLS */
9323 static NeonGenTwoOpFn * const fns[3] = {
9324 gen_helper_neon_mul_u8,
9325 gen_helper_neon_mul_u16,
9326 tcg_gen_mul_i32,
9328 genfn = fns[size];
9329 break;
9331 case 0x16: /* SQDMULH, SQRDMULH */
9333 static NeonGenTwoOpEnvFn * const fns[2][2] = {
9334 { gen_helper_neon_qdmulh_s16, gen_helper_neon_qrdmulh_s16 },
9335 { gen_helper_neon_qdmulh_s32, gen_helper_neon_qrdmulh_s32 },
9337 assert(size == 1 || size == 2);
9338 genenvfn = fns[size - 1][u];
9339 break;
9341 default:
9342 g_assert_not_reached();
9345 if (genenvfn) {
9346 genenvfn(tcg_res, cpu_env, tcg_op1, tcg_op2);
9347 } else {
9348 genfn(tcg_res, tcg_op1, tcg_op2);
9351 if (opcode == 0xf || opcode == 0x12) {
9352 /* SABA, UABA, MLA, MLS: accumulating ops */
9353 static NeonGenTwoOpFn * const fns[3][2] = {
9354 { gen_helper_neon_add_u8, gen_helper_neon_sub_u8 },
9355 { gen_helper_neon_add_u16, gen_helper_neon_sub_u16 },
9356 { tcg_gen_add_i32, tcg_gen_sub_i32 },
9358 bool is_sub = (opcode == 0x12 && u); /* MLS */
9360 genfn = fns[size][is_sub];
9361 read_vec_element_i32(s, tcg_op1, rd, pass, MO_32);
9362 genfn(tcg_res, tcg_op1, tcg_res);
9365 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
9367 tcg_temp_free_i32(tcg_res);
9368 tcg_temp_free_i32(tcg_op1);
9369 tcg_temp_free_i32(tcg_op2);
9373 if (!is_q) {
9374 clear_vec_high(s, rd);
9378 /* C3.6.16 AdvSIMD three same
9379 * 31 30 29 28 24 23 22 21 20 16 15 11 10 9 5 4 0
9380 * +---+---+---+-----------+------+---+------+--------+---+------+------+
9381 * | 0 | Q | U | 0 1 1 1 0 | size | 1 | Rm | opcode | 1 | Rn | Rd |
9382 * +---+---+---+-----------+------+---+------+--------+---+------+------+
9384 static void disas_simd_three_reg_same(DisasContext *s, uint32_t insn)
9386 int opcode = extract32(insn, 11, 5);
9388 switch (opcode) {
9389 case 0x3: /* logic ops */
9390 disas_simd_3same_logic(s, insn);
9391 break;
9392 case 0x17: /* ADDP */
9393 case 0x14: /* SMAXP, UMAXP */
9394 case 0x15: /* SMINP, UMINP */
9396 /* Pairwise operations */
9397 int is_q = extract32(insn, 30, 1);
9398 int u = extract32(insn, 29, 1);
9399 int size = extract32(insn, 22, 2);
9400 int rm = extract32(insn, 16, 5);
9401 int rn = extract32(insn, 5, 5);
9402 int rd = extract32(insn, 0, 5);
9403 if (opcode == 0x17) {
9404 if (u || (size == 3 && !is_q)) {
9405 unallocated_encoding(s);
9406 return;
9408 } else {
9409 if (size == 3) {
9410 unallocated_encoding(s);
9411 return;
9414 handle_simd_3same_pair(s, is_q, u, opcode, size, rn, rm, rd);
9415 break;
9417 case 0x18 ... 0x31:
9418 /* floating point ops, sz[1] and U are part of opcode */
9419 disas_simd_3same_float(s, insn);
9420 break;
9421 default:
9422 disas_simd_3same_int(s, insn);
9423 break;
9427 static void handle_2misc_widening(DisasContext *s, int opcode, bool is_q,
9428 int size, int rn, int rd)
9430 /* Handle 2-reg-misc ops which are widening (so each size element
9431 * in the source becomes a 2*size element in the destination.
9432 * The only instruction like this is FCVTL.
9434 int pass;
9436 if (size == 3) {
9437 /* 32 -> 64 bit fp conversion */
9438 TCGv_i64 tcg_res[2];
9439 int srcelt = is_q ? 2 : 0;
9441 for (pass = 0; pass < 2; pass++) {
9442 TCGv_i32 tcg_op = tcg_temp_new_i32();
9443 tcg_res[pass] = tcg_temp_new_i64();
9445 read_vec_element_i32(s, tcg_op, rn, srcelt + pass, MO_32);
9446 gen_helper_vfp_fcvtds(tcg_res[pass], tcg_op, cpu_env);
9447 tcg_temp_free_i32(tcg_op);
9449 for (pass = 0; pass < 2; pass++) {
9450 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
9451 tcg_temp_free_i64(tcg_res[pass]);
9453 } else {
9454 /* 16 -> 32 bit fp conversion */
9455 int srcelt = is_q ? 4 : 0;
9456 TCGv_i32 tcg_res[4];
9458 for (pass = 0; pass < 4; pass++) {
9459 tcg_res[pass] = tcg_temp_new_i32();
9461 read_vec_element_i32(s, tcg_res[pass], rn, srcelt + pass, MO_16);
9462 gen_helper_vfp_fcvt_f16_to_f32(tcg_res[pass], tcg_res[pass],
9463 cpu_env);
9465 for (pass = 0; pass < 4; pass++) {
9466 write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_32);
9467 tcg_temp_free_i32(tcg_res[pass]);
9472 static void handle_rev(DisasContext *s, int opcode, bool u,
9473 bool is_q, int size, int rn, int rd)
9475 int op = (opcode << 1) | u;
9476 int opsz = op + size;
9477 int grp_size = 3 - opsz;
9478 int dsize = is_q ? 128 : 64;
9479 int i;
9481 if (opsz >= 3) {
9482 unallocated_encoding(s);
9483 return;
9486 if (!fp_access_check(s)) {
9487 return;
9490 if (size == 0) {
9491 /* Special case bytes, use bswap op on each group of elements */
9492 int groups = dsize / (8 << grp_size);
9494 for (i = 0; i < groups; i++) {
9495 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
9497 read_vec_element(s, tcg_tmp, rn, i, grp_size);
9498 switch (grp_size) {
9499 case MO_16:
9500 tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp);
9501 break;
9502 case MO_32:
9503 tcg_gen_bswap32_i64(tcg_tmp, tcg_tmp);
9504 break;
9505 case MO_64:
9506 tcg_gen_bswap64_i64(tcg_tmp, tcg_tmp);
9507 break;
9508 default:
9509 g_assert_not_reached();
9511 write_vec_element(s, tcg_tmp, rd, i, grp_size);
9512 tcg_temp_free_i64(tcg_tmp);
9514 if (!is_q) {
9515 clear_vec_high(s, rd);
9517 } else {
9518 int revmask = (1 << grp_size) - 1;
9519 int esize = 8 << size;
9520 int elements = dsize / esize;
9521 TCGv_i64 tcg_rn = tcg_temp_new_i64();
9522 TCGv_i64 tcg_rd = tcg_const_i64(0);
9523 TCGv_i64 tcg_rd_hi = tcg_const_i64(0);
9525 for (i = 0; i < elements; i++) {
9526 int e_rev = (i & 0xf) ^ revmask;
9527 int off = e_rev * esize;
9528 read_vec_element(s, tcg_rn, rn, i, size);
9529 if (off >= 64) {
9530 tcg_gen_deposit_i64(tcg_rd_hi, tcg_rd_hi,
9531 tcg_rn, off - 64, esize);
9532 } else {
9533 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, off, esize);
9536 write_vec_element(s, tcg_rd, rd, 0, MO_64);
9537 write_vec_element(s, tcg_rd_hi, rd, 1, MO_64);
9539 tcg_temp_free_i64(tcg_rd_hi);
9540 tcg_temp_free_i64(tcg_rd);
9541 tcg_temp_free_i64(tcg_rn);
9545 static void handle_2misc_pairwise(DisasContext *s, int opcode, bool u,
9546 bool is_q, int size, int rn, int rd)
9548 /* Implement the pairwise operations from 2-misc:
9549 * SADDLP, UADDLP, SADALP, UADALP.
9550 * These all add pairs of elements in the input to produce a
9551 * double-width result element in the output (possibly accumulating).
9553 bool accum = (opcode == 0x6);
9554 int maxpass = is_q ? 2 : 1;
9555 int pass;
9556 TCGv_i64 tcg_res[2];
9558 if (size == 2) {
9559 /* 32 + 32 -> 64 op */
9560 TCGMemOp memop = size + (u ? 0 : MO_SIGN);
9562 for (pass = 0; pass < maxpass; pass++) {
9563 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
9564 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
9566 tcg_res[pass] = tcg_temp_new_i64();
9568 read_vec_element(s, tcg_op1, rn, pass * 2, memop);
9569 read_vec_element(s, tcg_op2, rn, pass * 2 + 1, memop);
9570 tcg_gen_add_i64(tcg_res[pass], tcg_op1, tcg_op2);
9571 if (accum) {
9572 read_vec_element(s, tcg_op1, rd, pass, MO_64);
9573 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
9576 tcg_temp_free_i64(tcg_op1);
9577 tcg_temp_free_i64(tcg_op2);
9579 } else {
9580 for (pass = 0; pass < maxpass; pass++) {
9581 TCGv_i64 tcg_op = tcg_temp_new_i64();
9582 NeonGenOneOpFn *genfn;
9583 static NeonGenOneOpFn * const fns[2][2] = {
9584 { gen_helper_neon_addlp_s8, gen_helper_neon_addlp_u8 },
9585 { gen_helper_neon_addlp_s16, gen_helper_neon_addlp_u16 },
9588 genfn = fns[size][u];
9590 tcg_res[pass] = tcg_temp_new_i64();
9592 read_vec_element(s, tcg_op, rn, pass, MO_64);
9593 genfn(tcg_res[pass], tcg_op);
9595 if (accum) {
9596 read_vec_element(s, tcg_op, rd, pass, MO_64);
9597 if (size == 0) {
9598 gen_helper_neon_addl_u16(tcg_res[pass],
9599 tcg_res[pass], tcg_op);
9600 } else {
9601 gen_helper_neon_addl_u32(tcg_res[pass],
9602 tcg_res[pass], tcg_op);
9605 tcg_temp_free_i64(tcg_op);
9608 if (!is_q) {
9609 tcg_res[1] = tcg_const_i64(0);
9611 for (pass = 0; pass < 2; pass++) {
9612 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
9613 tcg_temp_free_i64(tcg_res[pass]);
9617 static void handle_shll(DisasContext *s, bool is_q, int size, int rn, int rd)
9619 /* Implement SHLL and SHLL2 */
9620 int pass;
9621 int part = is_q ? 2 : 0;
9622 TCGv_i64 tcg_res[2];
9624 for (pass = 0; pass < 2; pass++) {
9625 static NeonGenWidenFn * const widenfns[3] = {
9626 gen_helper_neon_widen_u8,
9627 gen_helper_neon_widen_u16,
9628 tcg_gen_extu_i32_i64,
9630 NeonGenWidenFn *widenfn = widenfns[size];
9631 TCGv_i32 tcg_op = tcg_temp_new_i32();
9633 read_vec_element_i32(s, tcg_op, rn, part + pass, MO_32);
9634 tcg_res[pass] = tcg_temp_new_i64();
9635 widenfn(tcg_res[pass], tcg_op);
9636 tcg_gen_shli_i64(tcg_res[pass], tcg_res[pass], 8 << size);
9638 tcg_temp_free_i32(tcg_op);
9641 for (pass = 0; pass < 2; pass++) {
9642 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
9643 tcg_temp_free_i64(tcg_res[pass]);
9647 /* C3.6.17 AdvSIMD two reg misc
9648 * 31 30 29 28 24 23 22 21 17 16 12 11 10 9 5 4 0
9649 * +---+---+---+-----------+------+-----------+--------+-----+------+------+
9650 * | 0 | Q | U | 0 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 | Rn | Rd |
9651 * +---+---+---+-----------+------+-----------+--------+-----+------+------+
9653 static void disas_simd_two_reg_misc(DisasContext *s, uint32_t insn)
9655 int size = extract32(insn, 22, 2);
9656 int opcode = extract32(insn, 12, 5);
9657 bool u = extract32(insn, 29, 1);
9658 bool is_q = extract32(insn, 30, 1);
9659 int rn = extract32(insn, 5, 5);
9660 int rd = extract32(insn, 0, 5);
9661 bool need_fpstatus = false;
9662 bool need_rmode = false;
9663 int rmode = -1;
9664 TCGv_i32 tcg_rmode;
9665 TCGv_ptr tcg_fpstatus;
9667 switch (opcode) {
9668 case 0x0: /* REV64, REV32 */
9669 case 0x1: /* REV16 */
9670 handle_rev(s, opcode, u, is_q, size, rn, rd);
9671 return;
9672 case 0x5: /* CNT, NOT, RBIT */
9673 if (u && size == 0) {
9674 /* NOT: adjust size so we can use the 64-bits-at-a-time loop. */
9675 size = 3;
9676 break;
9677 } else if (u && size == 1) {
9678 /* RBIT */
9679 break;
9680 } else if (!u && size == 0) {
9681 /* CNT */
9682 break;
9684 unallocated_encoding(s);
9685 return;
9686 case 0x12: /* XTN, XTN2, SQXTUN, SQXTUN2 */
9687 case 0x14: /* SQXTN, SQXTN2, UQXTN, UQXTN2 */
9688 if (size == 3) {
9689 unallocated_encoding(s);
9690 return;
9692 if (!fp_access_check(s)) {
9693 return;
9696 handle_2misc_narrow(s, false, opcode, u, is_q, size, rn, rd);
9697 return;
9698 case 0x4: /* CLS, CLZ */
9699 if (size == 3) {
9700 unallocated_encoding(s);
9701 return;
9703 break;
9704 case 0x2: /* SADDLP, UADDLP */
9705 case 0x6: /* SADALP, UADALP */
9706 if (size == 3) {
9707 unallocated_encoding(s);
9708 return;
9710 if (!fp_access_check(s)) {
9711 return;
9713 handle_2misc_pairwise(s, opcode, u, is_q, size, rn, rd);
9714 return;
9715 case 0x13: /* SHLL, SHLL2 */
9716 if (u == 0 || size == 3) {
9717 unallocated_encoding(s);
9718 return;
9720 if (!fp_access_check(s)) {
9721 return;
9723 handle_shll(s, is_q, size, rn, rd);
9724 return;
9725 case 0xa: /* CMLT */
9726 if (u == 1) {
9727 unallocated_encoding(s);
9728 return;
9730 /* fall through */
9731 case 0x8: /* CMGT, CMGE */
9732 case 0x9: /* CMEQ, CMLE */
9733 case 0xb: /* ABS, NEG */
9734 if (size == 3 && !is_q) {
9735 unallocated_encoding(s);
9736 return;
9738 break;
9739 case 0x3: /* SUQADD, USQADD */
9740 if (size == 3 && !is_q) {
9741 unallocated_encoding(s);
9742 return;
9744 if (!fp_access_check(s)) {
9745 return;
9747 handle_2misc_satacc(s, false, u, is_q, size, rn, rd);
9748 return;
9749 case 0x7: /* SQABS, SQNEG */
9750 if (size == 3 && !is_q) {
9751 unallocated_encoding(s);
9752 return;
9754 break;
9755 case 0xc ... 0xf:
9756 case 0x16 ... 0x1d:
9757 case 0x1f:
9759 /* Floating point: U, size[1] and opcode indicate operation;
9760 * size[0] indicates single or double precision.
9762 int is_double = extract32(size, 0, 1);
9763 opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
9764 size = is_double ? 3 : 2;
9765 switch (opcode) {
9766 case 0x2f: /* FABS */
9767 case 0x6f: /* FNEG */
9768 if (size == 3 && !is_q) {
9769 unallocated_encoding(s);
9770 return;
9772 break;
9773 case 0x1d: /* SCVTF */
9774 case 0x5d: /* UCVTF */
9776 bool is_signed = (opcode == 0x1d) ? true : false;
9777 int elements = is_double ? 2 : is_q ? 4 : 2;
9778 if (is_double && !is_q) {
9779 unallocated_encoding(s);
9780 return;
9782 if (!fp_access_check(s)) {
9783 return;
9785 handle_simd_intfp_conv(s, rd, rn, elements, is_signed, 0, size);
9786 return;
9788 case 0x2c: /* FCMGT (zero) */
9789 case 0x2d: /* FCMEQ (zero) */
9790 case 0x2e: /* FCMLT (zero) */
9791 case 0x6c: /* FCMGE (zero) */
9792 case 0x6d: /* FCMLE (zero) */
9793 if (size == 3 && !is_q) {
9794 unallocated_encoding(s);
9795 return;
9797 handle_2misc_fcmp_zero(s, opcode, false, u, is_q, size, rn, rd);
9798 return;
9799 case 0x7f: /* FSQRT */
9800 if (size == 3 && !is_q) {
9801 unallocated_encoding(s);
9802 return;
9804 break;
9805 case 0x1a: /* FCVTNS */
9806 case 0x1b: /* FCVTMS */
9807 case 0x3a: /* FCVTPS */
9808 case 0x3b: /* FCVTZS */
9809 case 0x5a: /* FCVTNU */
9810 case 0x5b: /* FCVTMU */
9811 case 0x7a: /* FCVTPU */
9812 case 0x7b: /* FCVTZU */
9813 need_fpstatus = true;
9814 need_rmode = true;
9815 rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
9816 if (size == 3 && !is_q) {
9817 unallocated_encoding(s);
9818 return;
9820 break;
9821 case 0x5c: /* FCVTAU */
9822 case 0x1c: /* FCVTAS */
9823 need_fpstatus = true;
9824 need_rmode = true;
9825 rmode = FPROUNDING_TIEAWAY;
9826 if (size == 3 && !is_q) {
9827 unallocated_encoding(s);
9828 return;
9830 break;
9831 case 0x3c: /* URECPE */
9832 if (size == 3) {
9833 unallocated_encoding(s);
9834 return;
9836 /* fall through */
9837 case 0x3d: /* FRECPE */
9838 case 0x7d: /* FRSQRTE */
9839 if (size == 3 && !is_q) {
9840 unallocated_encoding(s);
9841 return;
9843 if (!fp_access_check(s)) {
9844 return;
9846 handle_2misc_reciprocal(s, opcode, false, u, is_q, size, rn, rd);
9847 return;
9848 case 0x56: /* FCVTXN, FCVTXN2 */
9849 if (size == 2) {
9850 unallocated_encoding(s);
9851 return;
9853 /* fall through */
9854 case 0x16: /* FCVTN, FCVTN2 */
9855 /* handle_2misc_narrow does a 2*size -> size operation, but these
9856 * instructions encode the source size rather than dest size.
9858 if (!fp_access_check(s)) {
9859 return;
9861 handle_2misc_narrow(s, false, opcode, 0, is_q, size - 1, rn, rd);
9862 return;
9863 case 0x17: /* FCVTL, FCVTL2 */
9864 if (!fp_access_check(s)) {
9865 return;
9867 handle_2misc_widening(s, opcode, is_q, size, rn, rd);
9868 return;
9869 case 0x18: /* FRINTN */
9870 case 0x19: /* FRINTM */
9871 case 0x38: /* FRINTP */
9872 case 0x39: /* FRINTZ */
9873 need_rmode = true;
9874 rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
9875 /* fall through */
9876 case 0x59: /* FRINTX */
9877 case 0x79: /* FRINTI */
9878 need_fpstatus = true;
9879 if (size == 3 && !is_q) {
9880 unallocated_encoding(s);
9881 return;
9883 break;
9884 case 0x58: /* FRINTA */
9885 need_rmode = true;
9886 rmode = FPROUNDING_TIEAWAY;
9887 need_fpstatus = true;
9888 if (size == 3 && !is_q) {
9889 unallocated_encoding(s);
9890 return;
9892 break;
9893 case 0x7c: /* URSQRTE */
9894 if (size == 3) {
9895 unallocated_encoding(s);
9896 return;
9898 need_fpstatus = true;
9899 break;
9900 default:
9901 unallocated_encoding(s);
9902 return;
9904 break;
9906 default:
9907 unallocated_encoding(s);
9908 return;
9911 if (!fp_access_check(s)) {
9912 return;
9915 if (need_fpstatus) {
9916 tcg_fpstatus = get_fpstatus_ptr();
9917 } else {
9918 TCGV_UNUSED_PTR(tcg_fpstatus);
9920 if (need_rmode) {
9921 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rmode));
9922 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
9923 } else {
9924 TCGV_UNUSED_I32(tcg_rmode);
9927 if (size == 3) {
9928 /* All 64-bit element operations can be shared with scalar 2misc */
9929 int pass;
9931 for (pass = 0; pass < (is_q ? 2 : 1); pass++) {
9932 TCGv_i64 tcg_op = tcg_temp_new_i64();
9933 TCGv_i64 tcg_res = tcg_temp_new_i64();
9935 read_vec_element(s, tcg_op, rn, pass, MO_64);
9937 handle_2misc_64(s, opcode, u, tcg_res, tcg_op,
9938 tcg_rmode, tcg_fpstatus);
9940 write_vec_element(s, tcg_res, rd, pass, MO_64);
9942 tcg_temp_free_i64(tcg_res);
9943 tcg_temp_free_i64(tcg_op);
9945 } else {
9946 int pass;
9948 for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
9949 TCGv_i32 tcg_op = tcg_temp_new_i32();
9950 TCGv_i32 tcg_res = tcg_temp_new_i32();
9951 TCGCond cond;
9953 read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
9955 if (size == 2) {
9956 /* Special cases for 32 bit elements */
9957 switch (opcode) {
9958 case 0xa: /* CMLT */
9959 /* 32 bit integer comparison against zero, result is
9960 * test ? (2^32 - 1) : 0. We implement via setcond(test)
9961 * and inverting.
9963 cond = TCG_COND_LT;
9964 do_cmop:
9965 tcg_gen_setcondi_i32(cond, tcg_res, tcg_op, 0);
9966 tcg_gen_neg_i32(tcg_res, tcg_res);
9967 break;
9968 case 0x8: /* CMGT, CMGE */
9969 cond = u ? TCG_COND_GE : TCG_COND_GT;
9970 goto do_cmop;
9971 case 0x9: /* CMEQ, CMLE */
9972 cond = u ? TCG_COND_LE : TCG_COND_EQ;
9973 goto do_cmop;
9974 case 0x4: /* CLS */
9975 if (u) {
9976 gen_helper_clz32(tcg_res, tcg_op);
9977 } else {
9978 gen_helper_cls32(tcg_res, tcg_op);
9980 break;
9981 case 0x7: /* SQABS, SQNEG */
9982 if (u) {
9983 gen_helper_neon_qneg_s32(tcg_res, cpu_env, tcg_op);
9984 } else {
9985 gen_helper_neon_qabs_s32(tcg_res, cpu_env, tcg_op);
9987 break;
9988 case 0xb: /* ABS, NEG */
9989 if (u) {
9990 tcg_gen_neg_i32(tcg_res, tcg_op);
9991 } else {
9992 TCGv_i32 tcg_zero = tcg_const_i32(0);
9993 tcg_gen_neg_i32(tcg_res, tcg_op);
9994 tcg_gen_movcond_i32(TCG_COND_GT, tcg_res, tcg_op,
9995 tcg_zero, tcg_op, tcg_res);
9996 tcg_temp_free_i32(tcg_zero);
9998 break;
9999 case 0x2f: /* FABS */
10000 gen_helper_vfp_abss(tcg_res, tcg_op);
10001 break;
10002 case 0x6f: /* FNEG */
10003 gen_helper_vfp_negs(tcg_res, tcg_op);
10004 break;
10005 case 0x7f: /* FSQRT */
10006 gen_helper_vfp_sqrts(tcg_res, tcg_op, cpu_env);
10007 break;
10008 case 0x1a: /* FCVTNS */
10009 case 0x1b: /* FCVTMS */
10010 case 0x1c: /* FCVTAS */
10011 case 0x3a: /* FCVTPS */
10012 case 0x3b: /* FCVTZS */
10014 TCGv_i32 tcg_shift = tcg_const_i32(0);
10015 gen_helper_vfp_tosls(tcg_res, tcg_op,
10016 tcg_shift, tcg_fpstatus);
10017 tcg_temp_free_i32(tcg_shift);
10018 break;
10020 case 0x5a: /* FCVTNU */
10021 case 0x5b: /* FCVTMU */
10022 case 0x5c: /* FCVTAU */
10023 case 0x7a: /* FCVTPU */
10024 case 0x7b: /* FCVTZU */
10026 TCGv_i32 tcg_shift = tcg_const_i32(0);
10027 gen_helper_vfp_touls(tcg_res, tcg_op,
10028 tcg_shift, tcg_fpstatus);
10029 tcg_temp_free_i32(tcg_shift);
10030 break;
10032 case 0x18: /* FRINTN */
10033 case 0x19: /* FRINTM */
10034 case 0x38: /* FRINTP */
10035 case 0x39: /* FRINTZ */
10036 case 0x58: /* FRINTA */
10037 case 0x79: /* FRINTI */
10038 gen_helper_rints(tcg_res, tcg_op, tcg_fpstatus);
10039 break;
10040 case 0x59: /* FRINTX */
10041 gen_helper_rints_exact(tcg_res, tcg_op, tcg_fpstatus);
10042 break;
10043 case 0x7c: /* URSQRTE */
10044 gen_helper_rsqrte_u32(tcg_res, tcg_op, tcg_fpstatus);
10045 break;
10046 default:
10047 g_assert_not_reached();
10049 } else {
10050 /* Use helpers for 8 and 16 bit elements */
10051 switch (opcode) {
10052 case 0x5: /* CNT, RBIT */
10053 /* For these two insns size is part of the opcode specifier
10054 * (handled earlier); they always operate on byte elements.
10056 if (u) {
10057 gen_helper_neon_rbit_u8(tcg_res, tcg_op);
10058 } else {
10059 gen_helper_neon_cnt_u8(tcg_res, tcg_op);
10061 break;
10062 case 0x7: /* SQABS, SQNEG */
10064 NeonGenOneOpEnvFn *genfn;
10065 static NeonGenOneOpEnvFn * const fns[2][2] = {
10066 { gen_helper_neon_qabs_s8, gen_helper_neon_qneg_s8 },
10067 { gen_helper_neon_qabs_s16, gen_helper_neon_qneg_s16 },
10069 genfn = fns[size][u];
10070 genfn(tcg_res, cpu_env, tcg_op);
10071 break;
10073 case 0x8: /* CMGT, CMGE */
10074 case 0x9: /* CMEQ, CMLE */
10075 case 0xa: /* CMLT */
10077 static NeonGenTwoOpFn * const fns[3][2] = {
10078 { gen_helper_neon_cgt_s8, gen_helper_neon_cgt_s16 },
10079 { gen_helper_neon_cge_s8, gen_helper_neon_cge_s16 },
10080 { gen_helper_neon_ceq_u8, gen_helper_neon_ceq_u16 },
10082 NeonGenTwoOpFn *genfn;
10083 int comp;
10084 bool reverse;
10085 TCGv_i32 tcg_zero = tcg_const_i32(0);
10087 /* comp = index into [CMGT, CMGE, CMEQ, CMLE, CMLT] */
10088 comp = (opcode - 0x8) * 2 + u;
10089 /* ...but LE, LT are implemented as reverse GE, GT */
10090 reverse = (comp > 2);
10091 if (reverse) {
10092 comp = 4 - comp;
10094 genfn = fns[comp][size];
10095 if (reverse) {
10096 genfn(tcg_res, tcg_zero, tcg_op);
10097 } else {
10098 genfn(tcg_res, tcg_op, tcg_zero);
10100 tcg_temp_free_i32(tcg_zero);
10101 break;
10103 case 0xb: /* ABS, NEG */
10104 if (u) {
10105 TCGv_i32 tcg_zero = tcg_const_i32(0);
10106 if (size) {
10107 gen_helper_neon_sub_u16(tcg_res, tcg_zero, tcg_op);
10108 } else {
10109 gen_helper_neon_sub_u8(tcg_res, tcg_zero, tcg_op);
10111 tcg_temp_free_i32(tcg_zero);
10112 } else {
10113 if (size) {
10114 gen_helper_neon_abs_s16(tcg_res, tcg_op);
10115 } else {
10116 gen_helper_neon_abs_s8(tcg_res, tcg_op);
10119 break;
10120 case 0x4: /* CLS, CLZ */
10121 if (u) {
10122 if (size == 0) {
10123 gen_helper_neon_clz_u8(tcg_res, tcg_op);
10124 } else {
10125 gen_helper_neon_clz_u16(tcg_res, tcg_op);
10127 } else {
10128 if (size == 0) {
10129 gen_helper_neon_cls_s8(tcg_res, tcg_op);
10130 } else {
10131 gen_helper_neon_cls_s16(tcg_res, tcg_op);
10134 break;
10135 default:
10136 g_assert_not_reached();
10140 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
10142 tcg_temp_free_i32(tcg_res);
10143 tcg_temp_free_i32(tcg_op);
10146 if (!is_q) {
10147 clear_vec_high(s, rd);
10150 if (need_rmode) {
10151 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
10152 tcg_temp_free_i32(tcg_rmode);
10154 if (need_fpstatus) {
10155 tcg_temp_free_ptr(tcg_fpstatus);
10159 /* C3.6.13 AdvSIMD scalar x indexed element
10160 * 31 30 29 28 24 23 22 21 20 19 16 15 12 11 10 9 5 4 0
10161 * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
10162 * | 0 1 | U | 1 1 1 1 1 | size | L | M | Rm | opc | H | 0 | Rn | Rd |
10163 * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
10164 * C3.6.18 AdvSIMD vector x indexed element
10165 * 31 30 29 28 24 23 22 21 20 19 16 15 12 11 10 9 5 4 0
10166 * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
10167 * | 0 | Q | U | 0 1 1 1 1 | size | L | M | Rm | opc | H | 0 | Rn | Rd |
10168 * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
10170 static void disas_simd_indexed(DisasContext *s, uint32_t insn)
10172 /* This encoding has two kinds of instruction:
10173 * normal, where we perform elt x idxelt => elt for each
10174 * element in the vector
10175 * long, where we perform elt x idxelt and generate a result of
10176 * double the width of the input element
10177 * The long ops have a 'part' specifier (ie come in INSN, INSN2 pairs).
10179 bool is_scalar = extract32(insn, 28, 1);
10180 bool is_q = extract32(insn, 30, 1);
10181 bool u = extract32(insn, 29, 1);
10182 int size = extract32(insn, 22, 2);
10183 int l = extract32(insn, 21, 1);
10184 int m = extract32(insn, 20, 1);
10185 /* Note that the Rm field here is only 4 bits, not 5 as it usually is */
10186 int rm = extract32(insn, 16, 4);
10187 int opcode = extract32(insn, 12, 4);
10188 int h = extract32(insn, 11, 1);
10189 int rn = extract32(insn, 5, 5);
10190 int rd = extract32(insn, 0, 5);
10191 bool is_long = false;
10192 bool is_fp = false;
10193 int index;
10194 TCGv_ptr fpst;
10196 switch (opcode) {
10197 case 0x0: /* MLA */
10198 case 0x4: /* MLS */
10199 if (!u || is_scalar) {
10200 unallocated_encoding(s);
10201 return;
10203 break;
10204 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10205 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10206 case 0xa: /* SMULL, SMULL2, UMULL, UMULL2 */
10207 if (is_scalar) {
10208 unallocated_encoding(s);
10209 return;
10211 is_long = true;
10212 break;
10213 case 0x3: /* SQDMLAL, SQDMLAL2 */
10214 case 0x7: /* SQDMLSL, SQDMLSL2 */
10215 case 0xb: /* SQDMULL, SQDMULL2 */
10216 is_long = true;
10217 /* fall through */
10218 case 0xc: /* SQDMULH */
10219 case 0xd: /* SQRDMULH */
10220 if (u) {
10221 unallocated_encoding(s);
10222 return;
10224 break;
10225 case 0x8: /* MUL */
10226 if (u || is_scalar) {
10227 unallocated_encoding(s);
10228 return;
10230 break;
10231 case 0x1: /* FMLA */
10232 case 0x5: /* FMLS */
10233 if (u) {
10234 unallocated_encoding(s);
10235 return;
10237 /* fall through */
10238 case 0x9: /* FMUL, FMULX */
10239 if (!extract32(size, 1, 1)) {
10240 unallocated_encoding(s);
10241 return;
10243 is_fp = true;
10244 break;
10245 default:
10246 unallocated_encoding(s);
10247 return;
10250 if (is_fp) {
10251 /* low bit of size indicates single/double */
10252 size = extract32(size, 0, 1) ? 3 : 2;
10253 if (size == 2) {
10254 index = h << 1 | l;
10255 } else {
10256 if (l || !is_q) {
10257 unallocated_encoding(s);
10258 return;
10260 index = h;
10262 rm |= (m << 4);
10263 } else {
10264 switch (size) {
10265 case 1:
10266 index = h << 2 | l << 1 | m;
10267 break;
10268 case 2:
10269 index = h << 1 | l;
10270 rm |= (m << 4);
10271 break;
10272 default:
10273 unallocated_encoding(s);
10274 return;
10278 if (!fp_access_check(s)) {
10279 return;
10282 if (is_fp) {
10283 fpst = get_fpstatus_ptr();
10284 } else {
10285 TCGV_UNUSED_PTR(fpst);
10288 if (size == 3) {
10289 TCGv_i64 tcg_idx = tcg_temp_new_i64();
10290 int pass;
10292 assert(is_fp && is_q && !is_long);
10294 read_vec_element(s, tcg_idx, rm, index, MO_64);
10296 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
10297 TCGv_i64 tcg_op = tcg_temp_new_i64();
10298 TCGv_i64 tcg_res = tcg_temp_new_i64();
10300 read_vec_element(s, tcg_op, rn, pass, MO_64);
10302 switch (opcode) {
10303 case 0x5: /* FMLS */
10304 /* As usual for ARM, separate negation for fused multiply-add */
10305 gen_helper_vfp_negd(tcg_op, tcg_op);
10306 /* fall through */
10307 case 0x1: /* FMLA */
10308 read_vec_element(s, tcg_res, rd, pass, MO_64);
10309 gen_helper_vfp_muladdd(tcg_res, tcg_op, tcg_idx, tcg_res, fpst);
10310 break;
10311 case 0x9: /* FMUL, FMULX */
10312 if (u) {
10313 gen_helper_vfp_mulxd(tcg_res, tcg_op, tcg_idx, fpst);
10314 } else {
10315 gen_helper_vfp_muld(tcg_res, tcg_op, tcg_idx, fpst);
10317 break;
10318 default:
10319 g_assert_not_reached();
10322 write_vec_element(s, tcg_res, rd, pass, MO_64);
10323 tcg_temp_free_i64(tcg_op);
10324 tcg_temp_free_i64(tcg_res);
10327 if (is_scalar) {
10328 clear_vec_high(s, rd);
10331 tcg_temp_free_i64(tcg_idx);
10332 } else if (!is_long) {
10333 /* 32 bit floating point, or 16 or 32 bit integer.
10334 * For the 16 bit scalar case we use the usual Neon helpers and
10335 * rely on the fact that 0 op 0 == 0 with no side effects.
10337 TCGv_i32 tcg_idx = tcg_temp_new_i32();
10338 int pass, maxpasses;
10340 if (is_scalar) {
10341 maxpasses = 1;
10342 } else {
10343 maxpasses = is_q ? 4 : 2;
10346 read_vec_element_i32(s, tcg_idx, rm, index, size);
10348 if (size == 1 && !is_scalar) {
10349 /* The simplest way to handle the 16x16 indexed ops is to duplicate
10350 * the index into both halves of the 32 bit tcg_idx and then use
10351 * the usual Neon helpers.
10353 tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
10356 for (pass = 0; pass < maxpasses; pass++) {
10357 TCGv_i32 tcg_op = tcg_temp_new_i32();
10358 TCGv_i32 tcg_res = tcg_temp_new_i32();
10360 read_vec_element_i32(s, tcg_op, rn, pass, is_scalar ? size : MO_32);
10362 switch (opcode) {
10363 case 0x0: /* MLA */
10364 case 0x4: /* MLS */
10365 case 0x8: /* MUL */
10367 static NeonGenTwoOpFn * const fns[2][2] = {
10368 { gen_helper_neon_add_u16, gen_helper_neon_sub_u16 },
10369 { tcg_gen_add_i32, tcg_gen_sub_i32 },
10371 NeonGenTwoOpFn *genfn;
10372 bool is_sub = opcode == 0x4;
10374 if (size == 1) {
10375 gen_helper_neon_mul_u16(tcg_res, tcg_op, tcg_idx);
10376 } else {
10377 tcg_gen_mul_i32(tcg_res, tcg_op, tcg_idx);
10379 if (opcode == 0x8) {
10380 break;
10382 read_vec_element_i32(s, tcg_op, rd, pass, MO_32);
10383 genfn = fns[size - 1][is_sub];
10384 genfn(tcg_res, tcg_op, tcg_res);
10385 break;
10387 case 0x5: /* FMLS */
10388 /* As usual for ARM, separate negation for fused multiply-add */
10389 gen_helper_vfp_negs(tcg_op, tcg_op);
10390 /* fall through */
10391 case 0x1: /* FMLA */
10392 read_vec_element_i32(s, tcg_res, rd, pass, MO_32);
10393 gen_helper_vfp_muladds(tcg_res, tcg_op, tcg_idx, tcg_res, fpst);
10394 break;
10395 case 0x9: /* FMUL, FMULX */
10396 if (u) {
10397 gen_helper_vfp_mulxs(tcg_res, tcg_op, tcg_idx, fpst);
10398 } else {
10399 gen_helper_vfp_muls(tcg_res, tcg_op, tcg_idx, fpst);
10401 break;
10402 case 0xc: /* SQDMULH */
10403 if (size == 1) {
10404 gen_helper_neon_qdmulh_s16(tcg_res, cpu_env,
10405 tcg_op, tcg_idx);
10406 } else {
10407 gen_helper_neon_qdmulh_s32(tcg_res, cpu_env,
10408 tcg_op, tcg_idx);
10410 break;
10411 case 0xd: /* SQRDMULH */
10412 if (size == 1) {
10413 gen_helper_neon_qrdmulh_s16(tcg_res, cpu_env,
10414 tcg_op, tcg_idx);
10415 } else {
10416 gen_helper_neon_qrdmulh_s32(tcg_res, cpu_env,
10417 tcg_op, tcg_idx);
10419 break;
10420 default:
10421 g_assert_not_reached();
10424 if (is_scalar) {
10425 write_fp_sreg(s, rd, tcg_res);
10426 } else {
10427 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
10430 tcg_temp_free_i32(tcg_op);
10431 tcg_temp_free_i32(tcg_res);
10434 tcg_temp_free_i32(tcg_idx);
10436 if (!is_q) {
10437 clear_vec_high(s, rd);
10439 } else {
10440 /* long ops: 16x16->32 or 32x32->64 */
10441 TCGv_i64 tcg_res[2];
10442 int pass;
10443 bool satop = extract32(opcode, 0, 1);
10444 TCGMemOp memop = MO_32;
10446 if (satop || !u) {
10447 memop |= MO_SIGN;
10450 if (size == 2) {
10451 TCGv_i64 tcg_idx = tcg_temp_new_i64();
10453 read_vec_element(s, tcg_idx, rm, index, memop);
10455 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
10456 TCGv_i64 tcg_op = tcg_temp_new_i64();
10457 TCGv_i64 tcg_passres;
10458 int passelt;
10460 if (is_scalar) {
10461 passelt = 0;
10462 } else {
10463 passelt = pass + (is_q * 2);
10466 read_vec_element(s, tcg_op, rn, passelt, memop);
10468 tcg_res[pass] = tcg_temp_new_i64();
10470 if (opcode == 0xa || opcode == 0xb) {
10471 /* Non-accumulating ops */
10472 tcg_passres = tcg_res[pass];
10473 } else {
10474 tcg_passres = tcg_temp_new_i64();
10477 tcg_gen_mul_i64(tcg_passres, tcg_op, tcg_idx);
10478 tcg_temp_free_i64(tcg_op);
10480 if (satop) {
10481 /* saturating, doubling */
10482 gen_helper_neon_addl_saturate_s64(tcg_passres, cpu_env,
10483 tcg_passres, tcg_passres);
10486 if (opcode == 0xa || opcode == 0xb) {
10487 continue;
10490 /* Accumulating op: handle accumulate step */
10491 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
10493 switch (opcode) {
10494 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10495 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
10496 break;
10497 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10498 tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
10499 break;
10500 case 0x7: /* SQDMLSL, SQDMLSL2 */
10501 tcg_gen_neg_i64(tcg_passres, tcg_passres);
10502 /* fall through */
10503 case 0x3: /* SQDMLAL, SQDMLAL2 */
10504 gen_helper_neon_addl_saturate_s64(tcg_res[pass], cpu_env,
10505 tcg_res[pass],
10506 tcg_passres);
10507 break;
10508 default:
10509 g_assert_not_reached();
10511 tcg_temp_free_i64(tcg_passres);
10513 tcg_temp_free_i64(tcg_idx);
10515 if (is_scalar) {
10516 clear_vec_high(s, rd);
10518 } else {
10519 TCGv_i32 tcg_idx = tcg_temp_new_i32();
10521 assert(size == 1);
10522 read_vec_element_i32(s, tcg_idx, rm, index, size);
10524 if (!is_scalar) {
10525 /* The simplest way to handle the 16x16 indexed ops is to
10526 * duplicate the index into both halves of the 32 bit tcg_idx
10527 * and then use the usual Neon helpers.
10529 tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
10532 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
10533 TCGv_i32 tcg_op = tcg_temp_new_i32();
10534 TCGv_i64 tcg_passres;
10536 if (is_scalar) {
10537 read_vec_element_i32(s, tcg_op, rn, pass, size);
10538 } else {
10539 read_vec_element_i32(s, tcg_op, rn,
10540 pass + (is_q * 2), MO_32);
10543 tcg_res[pass] = tcg_temp_new_i64();
10545 if (opcode == 0xa || opcode == 0xb) {
10546 /* Non-accumulating ops */
10547 tcg_passres = tcg_res[pass];
10548 } else {
10549 tcg_passres = tcg_temp_new_i64();
10552 if (memop & MO_SIGN) {
10553 gen_helper_neon_mull_s16(tcg_passres, tcg_op, tcg_idx);
10554 } else {
10555 gen_helper_neon_mull_u16(tcg_passres, tcg_op, tcg_idx);
10557 if (satop) {
10558 gen_helper_neon_addl_saturate_s32(tcg_passres, cpu_env,
10559 tcg_passres, tcg_passres);
10561 tcg_temp_free_i32(tcg_op);
10563 if (opcode == 0xa || opcode == 0xb) {
10564 continue;
10567 /* Accumulating op: handle accumulate step */
10568 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
10570 switch (opcode) {
10571 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10572 gen_helper_neon_addl_u32(tcg_res[pass], tcg_res[pass],
10573 tcg_passres);
10574 break;
10575 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10576 gen_helper_neon_subl_u32(tcg_res[pass], tcg_res[pass],
10577 tcg_passres);
10578 break;
10579 case 0x7: /* SQDMLSL, SQDMLSL2 */
10580 gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
10581 /* fall through */
10582 case 0x3: /* SQDMLAL, SQDMLAL2 */
10583 gen_helper_neon_addl_saturate_s32(tcg_res[pass], cpu_env,
10584 tcg_res[pass],
10585 tcg_passres);
10586 break;
10587 default:
10588 g_assert_not_reached();
10590 tcg_temp_free_i64(tcg_passres);
10592 tcg_temp_free_i32(tcg_idx);
10594 if (is_scalar) {
10595 tcg_gen_ext32u_i64(tcg_res[0], tcg_res[0]);
10599 if (is_scalar) {
10600 tcg_res[1] = tcg_const_i64(0);
10603 for (pass = 0; pass < 2; pass++) {
10604 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
10605 tcg_temp_free_i64(tcg_res[pass]);
10609 if (!TCGV_IS_UNUSED_PTR(fpst)) {
10610 tcg_temp_free_ptr(fpst);
10614 /* C3.6.19 Crypto AES
10615 * 31 24 23 22 21 17 16 12 11 10 9 5 4 0
10616 * +-----------------+------+-----------+--------+-----+------+------+
10617 * | 0 1 0 0 1 1 1 0 | size | 1 0 1 0 0 | opcode | 1 0 | Rn | Rd |
10618 * +-----------------+------+-----------+--------+-----+------+------+
10620 static void disas_crypto_aes(DisasContext *s, uint32_t insn)
10622 int size = extract32(insn, 22, 2);
10623 int opcode = extract32(insn, 12, 5);
10624 int rn = extract32(insn, 5, 5);
10625 int rd = extract32(insn, 0, 5);
10626 int decrypt;
10627 TCGv_i32 tcg_rd_regno, tcg_rn_regno, tcg_decrypt;
10628 CryptoThreeOpEnvFn *genfn;
10630 if (!arm_dc_feature(s, ARM_FEATURE_V8_AES)
10631 || size != 0) {
10632 unallocated_encoding(s);
10633 return;
10636 switch (opcode) {
10637 case 0x4: /* AESE */
10638 decrypt = 0;
10639 genfn = gen_helper_crypto_aese;
10640 break;
10641 case 0x6: /* AESMC */
10642 decrypt = 0;
10643 genfn = gen_helper_crypto_aesmc;
10644 break;
10645 case 0x5: /* AESD */
10646 decrypt = 1;
10647 genfn = gen_helper_crypto_aese;
10648 break;
10649 case 0x7: /* AESIMC */
10650 decrypt = 1;
10651 genfn = gen_helper_crypto_aesmc;
10652 break;
10653 default:
10654 unallocated_encoding(s);
10655 return;
10658 /* Note that we convert the Vx register indexes into the
10659 * index within the vfp.regs[] array, so we can share the
10660 * helper with the AArch32 instructions.
10662 tcg_rd_regno = tcg_const_i32(rd << 1);
10663 tcg_rn_regno = tcg_const_i32(rn << 1);
10664 tcg_decrypt = tcg_const_i32(decrypt);
10666 genfn(cpu_env, tcg_rd_regno, tcg_rn_regno, tcg_decrypt);
10668 tcg_temp_free_i32(tcg_rd_regno);
10669 tcg_temp_free_i32(tcg_rn_regno);
10670 tcg_temp_free_i32(tcg_decrypt);
10673 /* C3.6.20 Crypto three-reg SHA
10674 * 31 24 23 22 21 20 16 15 14 12 11 10 9 5 4 0
10675 * +-----------------+------+---+------+---+--------+-----+------+------+
10676 * | 0 1 0 1 1 1 1 0 | size | 0 | Rm | 0 | opcode | 0 0 | Rn | Rd |
10677 * +-----------------+------+---+------+---+--------+-----+------+------+
10679 static void disas_crypto_three_reg_sha(DisasContext *s, uint32_t insn)
10681 int size = extract32(insn, 22, 2);
10682 int opcode = extract32(insn, 12, 3);
10683 int rm = extract32(insn, 16, 5);
10684 int rn = extract32(insn, 5, 5);
10685 int rd = extract32(insn, 0, 5);
10686 CryptoThreeOpEnvFn *genfn;
10687 TCGv_i32 tcg_rd_regno, tcg_rn_regno, tcg_rm_regno;
10688 int feature = ARM_FEATURE_V8_SHA256;
10690 if (size != 0) {
10691 unallocated_encoding(s);
10692 return;
10695 switch (opcode) {
10696 case 0: /* SHA1C */
10697 case 1: /* SHA1P */
10698 case 2: /* SHA1M */
10699 case 3: /* SHA1SU0 */
10700 genfn = NULL;
10701 feature = ARM_FEATURE_V8_SHA1;
10702 break;
10703 case 4: /* SHA256H */
10704 genfn = gen_helper_crypto_sha256h;
10705 break;
10706 case 5: /* SHA256H2 */
10707 genfn = gen_helper_crypto_sha256h2;
10708 break;
10709 case 6: /* SHA256SU1 */
10710 genfn = gen_helper_crypto_sha256su1;
10711 break;
10712 default:
10713 unallocated_encoding(s);
10714 return;
10717 if (!arm_dc_feature(s, feature)) {
10718 unallocated_encoding(s);
10719 return;
10722 tcg_rd_regno = tcg_const_i32(rd << 1);
10723 tcg_rn_regno = tcg_const_i32(rn << 1);
10724 tcg_rm_regno = tcg_const_i32(rm << 1);
10726 if (genfn) {
10727 genfn(cpu_env, tcg_rd_regno, tcg_rn_regno, tcg_rm_regno);
10728 } else {
10729 TCGv_i32 tcg_opcode = tcg_const_i32(opcode);
10731 gen_helper_crypto_sha1_3reg(cpu_env, tcg_rd_regno,
10732 tcg_rn_regno, tcg_rm_regno, tcg_opcode);
10733 tcg_temp_free_i32(tcg_opcode);
10736 tcg_temp_free_i32(tcg_rd_regno);
10737 tcg_temp_free_i32(tcg_rn_regno);
10738 tcg_temp_free_i32(tcg_rm_regno);
10741 /* C3.6.21 Crypto two-reg SHA
10742 * 31 24 23 22 21 17 16 12 11 10 9 5 4 0
10743 * +-----------------+------+-----------+--------+-----+------+------+
10744 * | 0 1 0 1 1 1 1 0 | size | 1 0 1 0 0 | opcode | 1 0 | Rn | Rd |
10745 * +-----------------+------+-----------+--------+-----+------+------+
10747 static void disas_crypto_two_reg_sha(DisasContext *s, uint32_t insn)
10749 int size = extract32(insn, 22, 2);
10750 int opcode = extract32(insn, 12, 5);
10751 int rn = extract32(insn, 5, 5);
10752 int rd = extract32(insn, 0, 5);
10753 CryptoTwoOpEnvFn *genfn;
10754 int feature;
10755 TCGv_i32 tcg_rd_regno, tcg_rn_regno;
10757 if (size != 0) {
10758 unallocated_encoding(s);
10759 return;
10762 switch (opcode) {
10763 case 0: /* SHA1H */
10764 feature = ARM_FEATURE_V8_SHA1;
10765 genfn = gen_helper_crypto_sha1h;
10766 break;
10767 case 1: /* SHA1SU1 */
10768 feature = ARM_FEATURE_V8_SHA1;
10769 genfn = gen_helper_crypto_sha1su1;
10770 break;
10771 case 2: /* SHA256SU0 */
10772 feature = ARM_FEATURE_V8_SHA256;
10773 genfn = gen_helper_crypto_sha256su0;
10774 break;
10775 default:
10776 unallocated_encoding(s);
10777 return;
10780 if (!arm_dc_feature(s, feature)) {
10781 unallocated_encoding(s);
10782 return;
10785 tcg_rd_regno = tcg_const_i32(rd << 1);
10786 tcg_rn_regno = tcg_const_i32(rn << 1);
10788 genfn(cpu_env, tcg_rd_regno, tcg_rn_regno);
10790 tcg_temp_free_i32(tcg_rd_regno);
10791 tcg_temp_free_i32(tcg_rn_regno);
10794 /* C3.6 Data processing - SIMD, inc Crypto
10796 * As the decode gets a little complex we are using a table based
10797 * approach for this part of the decode.
10799 static const AArch64DecodeTable data_proc_simd[] = {
10800 /* pattern , mask , fn */
10801 { 0x0e200400, 0x9f200400, disas_simd_three_reg_same },
10802 { 0x0e200000, 0x9f200c00, disas_simd_three_reg_diff },
10803 { 0x0e200800, 0x9f3e0c00, disas_simd_two_reg_misc },
10804 { 0x0e300800, 0x9f3e0c00, disas_simd_across_lanes },
10805 { 0x0e000400, 0x9fe08400, disas_simd_copy },
10806 { 0x0f000000, 0x9f000400, disas_simd_indexed }, /* vector indexed */
10807 /* simd_mod_imm decode is a subset of simd_shift_imm, so must precede it */
10808 { 0x0f000400, 0x9ff80400, disas_simd_mod_imm },
10809 { 0x0f000400, 0x9f800400, disas_simd_shift_imm },
10810 { 0x0e000000, 0xbf208c00, disas_simd_tb },
10811 { 0x0e000800, 0xbf208c00, disas_simd_zip_trn },
10812 { 0x2e000000, 0xbf208400, disas_simd_ext },
10813 { 0x5e200400, 0xdf200400, disas_simd_scalar_three_reg_same },
10814 { 0x5e200000, 0xdf200c00, disas_simd_scalar_three_reg_diff },
10815 { 0x5e200800, 0xdf3e0c00, disas_simd_scalar_two_reg_misc },
10816 { 0x5e300800, 0xdf3e0c00, disas_simd_scalar_pairwise },
10817 { 0x5e000400, 0xdfe08400, disas_simd_scalar_copy },
10818 { 0x5f000000, 0xdf000400, disas_simd_indexed }, /* scalar indexed */
10819 { 0x5f000400, 0xdf800400, disas_simd_scalar_shift_imm },
10820 { 0x4e280800, 0xff3e0c00, disas_crypto_aes },
10821 { 0x5e000000, 0xff208c00, disas_crypto_three_reg_sha },
10822 { 0x5e280800, 0xff3e0c00, disas_crypto_two_reg_sha },
10823 { 0x00000000, 0x00000000, NULL }
10826 static void disas_data_proc_simd(DisasContext *s, uint32_t insn)
10828 /* Note that this is called with all non-FP cases from
10829 * table C3-6 so it must UNDEF for entries not specifically
10830 * allocated to instructions in that table.
10832 AArch64DecodeFn *fn = lookup_disas_fn(&data_proc_simd[0], insn);
10833 if (fn) {
10834 fn(s, insn);
10835 } else {
10836 unallocated_encoding(s);
10840 /* C3.6 Data processing - SIMD and floating point */
10841 static void disas_data_proc_simd_fp(DisasContext *s, uint32_t insn)
10843 if (extract32(insn, 28, 1) == 1 && extract32(insn, 30, 1) == 0) {
10844 disas_data_proc_fp(s, insn);
10845 } else {
10846 /* SIMD, including crypto */
10847 disas_data_proc_simd(s, insn);
10851 /* C3.1 A64 instruction index by encoding */
10852 static void disas_a64_insn(CPUARMState *env, DisasContext *s)
10854 uint32_t insn;
10856 insn = arm_ldl_code(env, s->pc, s->bswap_code);
10857 s->insn = insn;
10858 s->pc += 4;
10860 s->fp_access_checked = false;
10862 switch (extract32(insn, 25, 4)) {
10863 case 0x0: case 0x1: case 0x2: case 0x3: /* UNALLOCATED */
10864 unallocated_encoding(s);
10865 break;
10866 case 0x8: case 0x9: /* Data processing - immediate */
10867 disas_data_proc_imm(s, insn);
10868 break;
10869 case 0xa: case 0xb: /* Branch, exception generation and system insns */
10870 disas_b_exc_sys(s, insn);
10871 break;
10872 case 0x4:
10873 case 0x6:
10874 case 0xc:
10875 case 0xe: /* Loads and stores */
10876 disas_ldst(s, insn);
10877 break;
10878 case 0x5:
10879 case 0xd: /* Data processing - register */
10880 disas_data_proc_reg(s, insn);
10881 break;
10882 case 0x7:
10883 case 0xf: /* Data processing - SIMD and floating point */
10884 disas_data_proc_simd_fp(s, insn);
10885 break;
10886 default:
10887 assert(FALSE); /* all 15 cases should be handled above */
10888 break;
10891 /* if we allocated any temporaries, free them here */
10892 free_tmp_a64(s);
10895 void gen_intermediate_code_internal_a64(ARMCPU *cpu,
10896 TranslationBlock *tb,
10897 bool search_pc)
10899 CPUState *cs = CPU(cpu);
10900 CPUARMState *env = &cpu->env;
10901 DisasContext dc1, *dc = &dc1;
10902 CPUBreakpoint *bp;
10903 uint16_t *gen_opc_end;
10904 int j, lj;
10905 target_ulong pc_start;
10906 target_ulong next_page_start;
10907 int num_insns;
10908 int max_insns;
10910 pc_start = tb->pc;
10912 dc->tb = tb;
10914 gen_opc_end = tcg_ctx.gen_opc_buf + OPC_MAX_SIZE;
10916 dc->is_jmp = DISAS_NEXT;
10917 dc->pc = pc_start;
10918 dc->singlestep_enabled = cs->singlestep_enabled;
10919 dc->condjmp = 0;
10921 dc->aarch64 = 1;
10922 dc->thumb = 0;
10923 dc->bswap_code = 0;
10924 dc->condexec_mask = 0;
10925 dc->condexec_cond = 0;
10926 #if !defined(CONFIG_USER_ONLY)
10927 dc->user = (ARM_TBFLAG_AA64_EL(tb->flags) == 0);
10928 #endif
10929 dc->cpacr_fpen = ARM_TBFLAG_AA64_FPEN(tb->flags);
10930 dc->vec_len = 0;
10931 dc->vec_stride = 0;
10932 dc->cp_regs = cpu->cp_regs;
10933 dc->current_el = arm_current_el(env);
10934 dc->features = env->features;
10936 /* Single step state. The code-generation logic here is:
10937 * SS_ACTIVE == 0:
10938 * generate code with no special handling for single-stepping (except
10939 * that anything that can make us go to SS_ACTIVE == 1 must end the TB;
10940 * this happens anyway because those changes are all system register or
10941 * PSTATE writes).
10942 * SS_ACTIVE == 1, PSTATE.SS == 1: (active-not-pending)
10943 * emit code for one insn
10944 * emit code to clear PSTATE.SS
10945 * emit code to generate software step exception for completed step
10946 * end TB (as usual for having generated an exception)
10947 * SS_ACTIVE == 1, PSTATE.SS == 0: (active-pending)
10948 * emit code to generate a software step exception
10949 * end the TB
10951 dc->ss_active = ARM_TBFLAG_AA64_SS_ACTIVE(tb->flags);
10952 dc->pstate_ss = ARM_TBFLAG_AA64_PSTATE_SS(tb->flags);
10953 dc->is_ldex = false;
10954 dc->ss_same_el = (arm_debug_target_el(env) == dc->current_el);
10956 init_tmp_a64_array(dc);
10958 next_page_start = (pc_start & TARGET_PAGE_MASK) + TARGET_PAGE_SIZE;
10959 lj = -1;
10960 num_insns = 0;
10961 max_insns = tb->cflags & CF_COUNT_MASK;
10962 if (max_insns == 0) {
10963 max_insns = CF_COUNT_MASK;
10966 gen_tb_start();
10968 tcg_clear_temp_count();
10970 do {
10971 if (unlikely(!QTAILQ_EMPTY(&cs->breakpoints))) {
10972 QTAILQ_FOREACH(bp, &cs->breakpoints, entry) {
10973 if (bp->pc == dc->pc) {
10974 gen_exception_internal_insn(dc, 0, EXCP_DEBUG);
10975 /* Advance PC so that clearing the breakpoint will
10976 invalidate this TB. */
10977 dc->pc += 2;
10978 goto done_generating;
10983 if (search_pc) {
10984 j = tcg_ctx.gen_opc_ptr - tcg_ctx.gen_opc_buf;
10985 if (lj < j) {
10986 lj++;
10987 while (lj < j) {
10988 tcg_ctx.gen_opc_instr_start[lj++] = 0;
10991 tcg_ctx.gen_opc_pc[lj] = dc->pc;
10992 tcg_ctx.gen_opc_instr_start[lj] = 1;
10993 tcg_ctx.gen_opc_icount[lj] = num_insns;
10996 if (num_insns + 1 == max_insns && (tb->cflags & CF_LAST_IO)) {
10997 gen_io_start();
11000 if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP | CPU_LOG_TB_OP_OPT))) {
11001 tcg_gen_debug_insn_start(dc->pc);
11004 if (dc->ss_active && !dc->pstate_ss) {
11005 /* Singlestep state is Active-pending.
11006 * If we're in this state at the start of a TB then either
11007 * a) we just took an exception to an EL which is being debugged
11008 * and this is the first insn in the exception handler
11009 * b) debug exceptions were masked and we just unmasked them
11010 * without changing EL (eg by clearing PSTATE.D)
11011 * In either case we're going to take a swstep exception in the
11012 * "did not step an insn" case, and so the syndrome ISV and EX
11013 * bits should be zero.
11015 assert(num_insns == 0);
11016 gen_exception(EXCP_UDEF, syn_swstep(dc->ss_same_el, 0, 0));
11017 dc->is_jmp = DISAS_EXC;
11018 break;
11021 disas_a64_insn(env, dc);
11023 if (tcg_check_temp_count()) {
11024 fprintf(stderr, "TCG temporary leak before "TARGET_FMT_lx"\n",
11025 dc->pc);
11028 /* Translation stops when a conditional branch is encountered.
11029 * Otherwise the subsequent code could get translated several times.
11030 * Also stop translation when a page boundary is reached. This
11031 * ensures prefetch aborts occur at the right place.
11033 num_insns++;
11034 } while (!dc->is_jmp && tcg_ctx.gen_opc_ptr < gen_opc_end &&
11035 !cs->singlestep_enabled &&
11036 !singlestep &&
11037 !dc->ss_active &&
11038 dc->pc < next_page_start &&
11039 num_insns < max_insns);
11041 if (tb->cflags & CF_LAST_IO) {
11042 gen_io_end();
11045 if (unlikely(cs->singlestep_enabled || dc->ss_active)
11046 && dc->is_jmp != DISAS_EXC) {
11047 /* Note that this means single stepping WFI doesn't halt the CPU.
11048 * For conditional branch insns this is harmless unreachable code as
11049 * gen_goto_tb() has already handled emitting the debug exception
11050 * (and thus a tb-jump is not possible when singlestepping).
11052 assert(dc->is_jmp != DISAS_TB_JUMP);
11053 if (dc->is_jmp != DISAS_JUMP) {
11054 gen_a64_set_pc_im(dc->pc);
11056 if (cs->singlestep_enabled) {
11057 gen_exception_internal(EXCP_DEBUG);
11058 } else {
11059 gen_step_complete_exception(dc);
11061 } else {
11062 switch (dc->is_jmp) {
11063 case DISAS_NEXT:
11064 gen_goto_tb(dc, 1, dc->pc);
11065 break;
11066 default:
11067 case DISAS_UPDATE:
11068 gen_a64_set_pc_im(dc->pc);
11069 /* fall through */
11070 case DISAS_JUMP:
11071 /* indicate that the hash table must be used to find the next TB */
11072 tcg_gen_exit_tb(0);
11073 break;
11074 case DISAS_TB_JUMP:
11075 case DISAS_EXC:
11076 case DISAS_SWI:
11077 break;
11078 case DISAS_WFE:
11079 gen_a64_set_pc_im(dc->pc);
11080 gen_helper_wfe(cpu_env);
11081 break;
11082 case DISAS_WFI:
11083 /* This is a special case because we don't want to just halt the CPU
11084 * if trying to debug across a WFI.
11086 gen_a64_set_pc_im(dc->pc);
11087 gen_helper_wfi(cpu_env);
11088 break;
11092 done_generating:
11093 gen_tb_end(tb, num_insns);
11094 *tcg_ctx.gen_opc_ptr = INDEX_op_end;
11096 #ifdef DEBUG_DISAS
11097 if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)) {
11098 qemu_log("----------------\n");
11099 qemu_log("IN: %s\n", lookup_symbol(pc_start));
11100 log_target_disas(env, pc_start, dc->pc - pc_start,
11101 4 | (dc->bswap_code << 1));
11102 qemu_log("\n");
11104 #endif
11105 if (search_pc) {
11106 j = tcg_ctx.gen_opc_ptr - tcg_ctx.gen_opc_buf;
11107 lj++;
11108 while (lj <= j) {
11109 tcg_ctx.gen_opc_instr_start[lj++] = 0;
11111 } else {
11112 tb->size = dc->pc - pc_start;
11113 tb->icount = num_insns;