Merge remote-tracking branch 'remotes/stefanha/tags/block-pull-request' into staging
[qemu/kevin.git] / target-arm / translate-a64.c
blob9175e48797f668dbe8dd7bb7c7d34b89012b2bc5
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 "translate.h"
29 #include "qemu/host-utils.h"
31 #include "exec/gen-icount.h"
33 #include "helper.h"
34 #define GEN_HELPER 1
35 #include "helper.h"
37 static TCGv_i64 cpu_X[32];
38 static TCGv_i64 cpu_pc;
39 static TCGv_i32 cpu_NF, cpu_ZF, cpu_CF, cpu_VF;
41 /* Load/store exclusive handling */
42 static TCGv_i64 cpu_exclusive_addr;
43 static TCGv_i64 cpu_exclusive_val;
44 static TCGv_i64 cpu_exclusive_high;
45 #ifdef CONFIG_USER_ONLY
46 static TCGv_i64 cpu_exclusive_test;
47 static TCGv_i32 cpu_exclusive_info;
48 #endif
50 static const char *regnames[] = {
51 "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7",
52 "x8", "x9", "x10", "x11", "x12", "x13", "x14", "x15",
53 "x16", "x17", "x18", "x19", "x20", "x21", "x22", "x23",
54 "x24", "x25", "x26", "x27", "x28", "x29", "lr", "sp"
57 enum a64_shift_type {
58 A64_SHIFT_TYPE_LSL = 0,
59 A64_SHIFT_TYPE_LSR = 1,
60 A64_SHIFT_TYPE_ASR = 2,
61 A64_SHIFT_TYPE_ROR = 3
64 /* Table based decoder typedefs - used when the relevant bits for decode
65 * are too awkwardly scattered across the instruction (eg SIMD).
67 typedef void AArch64DecodeFn(DisasContext *s, uint32_t insn);
69 typedef struct AArch64DecodeTable {
70 uint32_t pattern;
71 uint32_t mask;
72 AArch64DecodeFn *disas_fn;
73 } AArch64DecodeTable;
75 /* Function prototype for gen_ functions for calling Neon helpers */
76 typedef void NeonGenOneOpEnvFn(TCGv_i32, TCGv_ptr, TCGv_i32);
77 typedef void NeonGenTwoOpFn(TCGv_i32, TCGv_i32, TCGv_i32);
78 typedef void NeonGenTwoOpEnvFn(TCGv_i32, TCGv_ptr, TCGv_i32, TCGv_i32);
79 typedef void NeonGenTwo64OpFn(TCGv_i64, TCGv_i64, TCGv_i64);
80 typedef void NeonGenTwo64OpEnvFn(TCGv_i64, TCGv_ptr, TCGv_i64, TCGv_i64);
81 typedef void NeonGenNarrowFn(TCGv_i32, TCGv_i64);
82 typedef void NeonGenNarrowEnvFn(TCGv_i32, TCGv_ptr, TCGv_i64);
83 typedef void NeonGenWidenFn(TCGv_i64, TCGv_i32);
84 typedef void NeonGenTwoSingleOPFn(TCGv_i32, TCGv_i32, TCGv_i32, TCGv_ptr);
85 typedef void NeonGenTwoDoubleOPFn(TCGv_i64, TCGv_i64, TCGv_i64, TCGv_ptr);
86 typedef void NeonGenOneOpFn(TCGv_i64, TCGv_i64);
88 /* initialize TCG globals. */
89 void a64_translate_init(void)
91 int i;
93 cpu_pc = tcg_global_mem_new_i64(TCG_AREG0,
94 offsetof(CPUARMState, pc),
95 "pc");
96 for (i = 0; i < 32; i++) {
97 cpu_X[i] = tcg_global_mem_new_i64(TCG_AREG0,
98 offsetof(CPUARMState, xregs[i]),
99 regnames[i]);
102 cpu_NF = tcg_global_mem_new_i32(TCG_AREG0, offsetof(CPUARMState, NF), "NF");
103 cpu_ZF = tcg_global_mem_new_i32(TCG_AREG0, offsetof(CPUARMState, ZF), "ZF");
104 cpu_CF = tcg_global_mem_new_i32(TCG_AREG0, offsetof(CPUARMState, CF), "CF");
105 cpu_VF = tcg_global_mem_new_i32(TCG_AREG0, offsetof(CPUARMState, VF), "VF");
107 cpu_exclusive_addr = tcg_global_mem_new_i64(TCG_AREG0,
108 offsetof(CPUARMState, exclusive_addr), "exclusive_addr");
109 cpu_exclusive_val = tcg_global_mem_new_i64(TCG_AREG0,
110 offsetof(CPUARMState, exclusive_val), "exclusive_val");
111 cpu_exclusive_high = tcg_global_mem_new_i64(TCG_AREG0,
112 offsetof(CPUARMState, exclusive_high), "exclusive_high");
113 #ifdef CONFIG_USER_ONLY
114 cpu_exclusive_test = tcg_global_mem_new_i64(TCG_AREG0,
115 offsetof(CPUARMState, exclusive_test), "exclusive_test");
116 cpu_exclusive_info = tcg_global_mem_new_i32(TCG_AREG0,
117 offsetof(CPUARMState, exclusive_info), "exclusive_info");
118 #endif
121 void aarch64_cpu_dump_state(CPUState *cs, FILE *f,
122 fprintf_function cpu_fprintf, int flags)
124 ARMCPU *cpu = ARM_CPU(cs);
125 CPUARMState *env = &cpu->env;
126 uint32_t psr = pstate_read(env);
127 int i;
129 cpu_fprintf(f, "PC=%016"PRIx64" SP=%016"PRIx64"\n",
130 env->pc, env->xregs[31]);
131 for (i = 0; i < 31; i++) {
132 cpu_fprintf(f, "X%02d=%016"PRIx64, i, env->xregs[i]);
133 if ((i % 4) == 3) {
134 cpu_fprintf(f, "\n");
135 } else {
136 cpu_fprintf(f, " ");
139 cpu_fprintf(f, "PSTATE=%08x (flags %c%c%c%c)\n",
140 psr,
141 psr & PSTATE_N ? 'N' : '-',
142 psr & PSTATE_Z ? 'Z' : '-',
143 psr & PSTATE_C ? 'C' : '-',
144 psr & PSTATE_V ? 'V' : '-');
145 cpu_fprintf(f, "\n");
147 if (flags & CPU_DUMP_FPU) {
148 int numvfpregs = 32;
149 for (i = 0; i < numvfpregs; i += 2) {
150 uint64_t vlo = float64_val(env->vfp.regs[i * 2]);
151 uint64_t vhi = float64_val(env->vfp.regs[(i * 2) + 1]);
152 cpu_fprintf(f, "q%02d=%016" PRIx64 ":%016" PRIx64 " ",
153 i, vhi, vlo);
154 vlo = float64_val(env->vfp.regs[(i + 1) * 2]);
155 vhi = float64_val(env->vfp.regs[((i + 1) * 2) + 1]);
156 cpu_fprintf(f, "q%02d=%016" PRIx64 ":%016" PRIx64 "\n",
157 i + 1, vhi, vlo);
159 cpu_fprintf(f, "FPCR: %08x FPSR: %08x\n",
160 vfp_get_fpcr(env), vfp_get_fpsr(env));
164 static int get_mem_index(DisasContext *s)
166 #ifdef CONFIG_USER_ONLY
167 return 1;
168 #else
169 return s->user;
170 #endif
173 void gen_a64_set_pc_im(uint64_t val)
175 tcg_gen_movi_i64(cpu_pc, val);
178 static void gen_exception(int excp)
180 TCGv_i32 tmp = tcg_temp_new_i32();
181 tcg_gen_movi_i32(tmp, excp);
182 gen_helper_exception(cpu_env, tmp);
183 tcg_temp_free_i32(tmp);
186 static void gen_exception_insn(DisasContext *s, int offset, int excp)
188 gen_a64_set_pc_im(s->pc - offset);
189 gen_exception(excp);
190 s->is_jmp = DISAS_EXC;
193 static inline bool use_goto_tb(DisasContext *s, int n, uint64_t dest)
195 /* No direct tb linking with singlestep or deterministic io */
196 if (s->singlestep_enabled || (s->tb->cflags & CF_LAST_IO)) {
197 return false;
200 /* Only link tbs from inside the same guest page */
201 if ((s->tb->pc & TARGET_PAGE_MASK) != (dest & TARGET_PAGE_MASK)) {
202 return false;
205 return true;
208 static inline void gen_goto_tb(DisasContext *s, int n, uint64_t dest)
210 TranslationBlock *tb;
212 tb = s->tb;
213 if (use_goto_tb(s, n, dest)) {
214 tcg_gen_goto_tb(n);
215 gen_a64_set_pc_im(dest);
216 tcg_gen_exit_tb((intptr_t)tb + n);
217 s->is_jmp = DISAS_TB_JUMP;
218 } else {
219 gen_a64_set_pc_im(dest);
220 if (s->singlestep_enabled) {
221 gen_exception(EXCP_DEBUG);
223 tcg_gen_exit_tb(0);
224 s->is_jmp = DISAS_JUMP;
228 static void unallocated_encoding(DisasContext *s)
230 gen_exception_insn(s, 4, EXCP_UDEF);
233 #define unsupported_encoding(s, insn) \
234 do { \
235 qemu_log_mask(LOG_UNIMP, \
236 "%s:%d: unsupported instruction encoding 0x%08x " \
237 "at pc=%016" PRIx64 "\n", \
238 __FILE__, __LINE__, insn, s->pc - 4); \
239 unallocated_encoding(s); \
240 } while (0);
242 static void init_tmp_a64_array(DisasContext *s)
244 #ifdef CONFIG_DEBUG_TCG
245 int i;
246 for (i = 0; i < ARRAY_SIZE(s->tmp_a64); i++) {
247 TCGV_UNUSED_I64(s->tmp_a64[i]);
249 #endif
250 s->tmp_a64_count = 0;
253 static void free_tmp_a64(DisasContext *s)
255 int i;
256 for (i = 0; i < s->tmp_a64_count; i++) {
257 tcg_temp_free_i64(s->tmp_a64[i]);
259 init_tmp_a64_array(s);
262 static TCGv_i64 new_tmp_a64(DisasContext *s)
264 assert(s->tmp_a64_count < TMP_A64_MAX);
265 return s->tmp_a64[s->tmp_a64_count++] = tcg_temp_new_i64();
268 static TCGv_i64 new_tmp_a64_zero(DisasContext *s)
270 TCGv_i64 t = new_tmp_a64(s);
271 tcg_gen_movi_i64(t, 0);
272 return t;
276 * Register access functions
278 * These functions are used for directly accessing a register in where
279 * changes to the final register value are likely to be made. If you
280 * need to use a register for temporary calculation (e.g. index type
281 * operations) use the read_* form.
283 * B1.2.1 Register mappings
285 * In instruction register encoding 31 can refer to ZR (zero register) or
286 * the SP (stack pointer) depending on context. In QEMU's case we map SP
287 * to cpu_X[31] and ZR accesses to a temporary which can be discarded.
288 * This is the point of the _sp forms.
290 static TCGv_i64 cpu_reg(DisasContext *s, int reg)
292 if (reg == 31) {
293 return new_tmp_a64_zero(s);
294 } else {
295 return cpu_X[reg];
299 /* register access for when 31 == SP */
300 static TCGv_i64 cpu_reg_sp(DisasContext *s, int reg)
302 return cpu_X[reg];
305 /* read a cpu register in 32bit/64bit mode. Returns a TCGv_i64
306 * representing the register contents. This TCGv is an auto-freed
307 * temporary so it need not be explicitly freed, and may be modified.
309 static TCGv_i64 read_cpu_reg(DisasContext *s, int reg, int sf)
311 TCGv_i64 v = new_tmp_a64(s);
312 if (reg != 31) {
313 if (sf) {
314 tcg_gen_mov_i64(v, cpu_X[reg]);
315 } else {
316 tcg_gen_ext32u_i64(v, cpu_X[reg]);
318 } else {
319 tcg_gen_movi_i64(v, 0);
321 return v;
324 static TCGv_i64 read_cpu_reg_sp(DisasContext *s, int reg, int sf)
326 TCGv_i64 v = new_tmp_a64(s);
327 if (sf) {
328 tcg_gen_mov_i64(v, cpu_X[reg]);
329 } else {
330 tcg_gen_ext32u_i64(v, cpu_X[reg]);
332 return v;
335 /* Return the offset into CPUARMState of an element of specified
336 * size, 'element' places in from the least significant end of
337 * the FP/vector register Qn.
339 static inline int vec_reg_offset(int regno, int element, TCGMemOp size)
341 int offs = offsetof(CPUARMState, vfp.regs[regno * 2]);
342 #ifdef HOST_WORDS_BIGENDIAN
343 /* This is complicated slightly because vfp.regs[2n] is
344 * still the low half and vfp.regs[2n+1] the high half
345 * of the 128 bit vector, even on big endian systems.
346 * Calculate the offset assuming a fully bigendian 128 bits,
347 * then XOR to account for the order of the two 64 bit halves.
349 offs += (16 - ((element + 1) * (1 << size)));
350 offs ^= 8;
351 #else
352 offs += element * (1 << size);
353 #endif
354 return offs;
357 /* Return the offset into CPUARMState of a slice (from
358 * the least significant end) of FP register Qn (ie
359 * Dn, Sn, Hn or Bn).
360 * (Note that this is not the same mapping as for A32; see cpu.h)
362 static inline int fp_reg_offset(int regno, TCGMemOp size)
364 int offs = offsetof(CPUARMState, vfp.regs[regno * 2]);
365 #ifdef HOST_WORDS_BIGENDIAN
366 offs += (8 - (1 << size));
367 #endif
368 return offs;
371 /* Offset of the high half of the 128 bit vector Qn */
372 static inline int fp_reg_hi_offset(int regno)
374 return offsetof(CPUARMState, vfp.regs[regno * 2 + 1]);
377 /* Convenience accessors for reading and writing single and double
378 * FP registers. Writing clears the upper parts of the associated
379 * 128 bit vector register, as required by the architecture.
380 * Note that unlike the GP register accessors, the values returned
381 * by the read functions must be manually freed.
383 static TCGv_i64 read_fp_dreg(DisasContext *s, int reg)
385 TCGv_i64 v = tcg_temp_new_i64();
387 tcg_gen_ld_i64(v, cpu_env, fp_reg_offset(reg, MO_64));
388 return v;
391 static TCGv_i32 read_fp_sreg(DisasContext *s, int reg)
393 TCGv_i32 v = tcg_temp_new_i32();
395 tcg_gen_ld_i32(v, cpu_env, fp_reg_offset(reg, MO_32));
396 return v;
399 static void write_fp_dreg(DisasContext *s, int reg, TCGv_i64 v)
401 TCGv_i64 tcg_zero = tcg_const_i64(0);
403 tcg_gen_st_i64(v, cpu_env, fp_reg_offset(reg, MO_64));
404 tcg_gen_st_i64(tcg_zero, cpu_env, fp_reg_hi_offset(reg));
405 tcg_temp_free_i64(tcg_zero);
408 static void write_fp_sreg(DisasContext *s, int reg, TCGv_i32 v)
410 TCGv_i64 tmp = tcg_temp_new_i64();
412 tcg_gen_extu_i32_i64(tmp, v);
413 write_fp_dreg(s, reg, tmp);
414 tcg_temp_free_i64(tmp);
417 static TCGv_ptr get_fpstatus_ptr(void)
419 TCGv_ptr statusptr = tcg_temp_new_ptr();
420 int offset;
422 /* In A64 all instructions (both FP and Neon) use the FPCR;
423 * there is no equivalent of the A32 Neon "standard FPSCR value"
424 * and all operations use vfp.fp_status.
426 offset = offsetof(CPUARMState, vfp.fp_status);
427 tcg_gen_addi_ptr(statusptr, cpu_env, offset);
428 return statusptr;
431 /* Set ZF and NF based on a 64 bit result. This is alas fiddlier
432 * than the 32 bit equivalent.
434 static inline void gen_set_NZ64(TCGv_i64 result)
436 TCGv_i64 flag = tcg_temp_new_i64();
438 tcg_gen_setcondi_i64(TCG_COND_NE, flag, result, 0);
439 tcg_gen_trunc_i64_i32(cpu_ZF, flag);
440 tcg_gen_shri_i64(flag, result, 32);
441 tcg_gen_trunc_i64_i32(cpu_NF, flag);
442 tcg_temp_free_i64(flag);
445 /* Set NZCV as for a logical operation: NZ as per result, CV cleared. */
446 static inline void gen_logic_CC(int sf, TCGv_i64 result)
448 if (sf) {
449 gen_set_NZ64(result);
450 } else {
451 tcg_gen_trunc_i64_i32(cpu_ZF, result);
452 tcg_gen_trunc_i64_i32(cpu_NF, result);
454 tcg_gen_movi_i32(cpu_CF, 0);
455 tcg_gen_movi_i32(cpu_VF, 0);
458 /* dest = T0 + T1; compute C, N, V and Z flags */
459 static void gen_add_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
461 if (sf) {
462 TCGv_i64 result, flag, tmp;
463 result = tcg_temp_new_i64();
464 flag = tcg_temp_new_i64();
465 tmp = tcg_temp_new_i64();
467 tcg_gen_movi_i64(tmp, 0);
468 tcg_gen_add2_i64(result, flag, t0, tmp, t1, tmp);
470 tcg_gen_trunc_i64_i32(cpu_CF, flag);
472 gen_set_NZ64(result);
474 tcg_gen_xor_i64(flag, result, t0);
475 tcg_gen_xor_i64(tmp, t0, t1);
476 tcg_gen_andc_i64(flag, flag, tmp);
477 tcg_temp_free_i64(tmp);
478 tcg_gen_shri_i64(flag, flag, 32);
479 tcg_gen_trunc_i64_i32(cpu_VF, flag);
481 tcg_gen_mov_i64(dest, result);
482 tcg_temp_free_i64(result);
483 tcg_temp_free_i64(flag);
484 } else {
485 /* 32 bit arithmetic */
486 TCGv_i32 t0_32 = tcg_temp_new_i32();
487 TCGv_i32 t1_32 = tcg_temp_new_i32();
488 TCGv_i32 tmp = tcg_temp_new_i32();
490 tcg_gen_movi_i32(tmp, 0);
491 tcg_gen_trunc_i64_i32(t0_32, t0);
492 tcg_gen_trunc_i64_i32(t1_32, t1);
493 tcg_gen_add2_i32(cpu_NF, cpu_CF, t0_32, tmp, t1_32, tmp);
494 tcg_gen_mov_i32(cpu_ZF, cpu_NF);
495 tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
496 tcg_gen_xor_i32(tmp, t0_32, t1_32);
497 tcg_gen_andc_i32(cpu_VF, cpu_VF, tmp);
498 tcg_gen_extu_i32_i64(dest, cpu_NF);
500 tcg_temp_free_i32(tmp);
501 tcg_temp_free_i32(t0_32);
502 tcg_temp_free_i32(t1_32);
506 /* dest = T0 - T1; compute C, N, V and Z flags */
507 static void gen_sub_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
509 if (sf) {
510 /* 64 bit arithmetic */
511 TCGv_i64 result, flag, tmp;
513 result = tcg_temp_new_i64();
514 flag = tcg_temp_new_i64();
515 tcg_gen_sub_i64(result, t0, t1);
517 gen_set_NZ64(result);
519 tcg_gen_setcond_i64(TCG_COND_GEU, flag, t0, t1);
520 tcg_gen_trunc_i64_i32(cpu_CF, flag);
522 tcg_gen_xor_i64(flag, result, t0);
523 tmp = tcg_temp_new_i64();
524 tcg_gen_xor_i64(tmp, t0, t1);
525 tcg_gen_and_i64(flag, flag, tmp);
526 tcg_temp_free_i64(tmp);
527 tcg_gen_shri_i64(flag, flag, 32);
528 tcg_gen_trunc_i64_i32(cpu_VF, flag);
529 tcg_gen_mov_i64(dest, result);
530 tcg_temp_free_i64(flag);
531 tcg_temp_free_i64(result);
532 } else {
533 /* 32 bit arithmetic */
534 TCGv_i32 t0_32 = tcg_temp_new_i32();
535 TCGv_i32 t1_32 = tcg_temp_new_i32();
536 TCGv_i32 tmp;
538 tcg_gen_trunc_i64_i32(t0_32, t0);
539 tcg_gen_trunc_i64_i32(t1_32, t1);
540 tcg_gen_sub_i32(cpu_NF, t0_32, t1_32);
541 tcg_gen_mov_i32(cpu_ZF, cpu_NF);
542 tcg_gen_setcond_i32(TCG_COND_GEU, cpu_CF, t0_32, t1_32);
543 tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
544 tmp = tcg_temp_new_i32();
545 tcg_gen_xor_i32(tmp, t0_32, t1_32);
546 tcg_temp_free_i32(t0_32);
547 tcg_temp_free_i32(t1_32);
548 tcg_gen_and_i32(cpu_VF, cpu_VF, tmp);
549 tcg_temp_free_i32(tmp);
550 tcg_gen_extu_i32_i64(dest, cpu_NF);
554 /* dest = T0 + T1 + CF; do not compute flags. */
555 static void gen_adc(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
557 TCGv_i64 flag = tcg_temp_new_i64();
558 tcg_gen_extu_i32_i64(flag, cpu_CF);
559 tcg_gen_add_i64(dest, t0, t1);
560 tcg_gen_add_i64(dest, dest, flag);
561 tcg_temp_free_i64(flag);
563 if (!sf) {
564 tcg_gen_ext32u_i64(dest, dest);
568 /* dest = T0 + T1 + CF; compute C, N, V and Z flags. */
569 static void gen_adc_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
571 if (sf) {
572 TCGv_i64 result, cf_64, vf_64, tmp;
573 result = tcg_temp_new_i64();
574 cf_64 = tcg_temp_new_i64();
575 vf_64 = tcg_temp_new_i64();
576 tmp = tcg_const_i64(0);
578 tcg_gen_extu_i32_i64(cf_64, cpu_CF);
579 tcg_gen_add2_i64(result, cf_64, t0, tmp, cf_64, tmp);
580 tcg_gen_add2_i64(result, cf_64, result, cf_64, t1, tmp);
581 tcg_gen_trunc_i64_i32(cpu_CF, cf_64);
582 gen_set_NZ64(result);
584 tcg_gen_xor_i64(vf_64, result, t0);
585 tcg_gen_xor_i64(tmp, t0, t1);
586 tcg_gen_andc_i64(vf_64, vf_64, tmp);
587 tcg_gen_shri_i64(vf_64, vf_64, 32);
588 tcg_gen_trunc_i64_i32(cpu_VF, vf_64);
590 tcg_gen_mov_i64(dest, result);
592 tcg_temp_free_i64(tmp);
593 tcg_temp_free_i64(vf_64);
594 tcg_temp_free_i64(cf_64);
595 tcg_temp_free_i64(result);
596 } else {
597 TCGv_i32 t0_32, t1_32, tmp;
598 t0_32 = tcg_temp_new_i32();
599 t1_32 = tcg_temp_new_i32();
600 tmp = tcg_const_i32(0);
602 tcg_gen_trunc_i64_i32(t0_32, t0);
603 tcg_gen_trunc_i64_i32(t1_32, t1);
604 tcg_gen_add2_i32(cpu_NF, cpu_CF, t0_32, tmp, cpu_CF, tmp);
605 tcg_gen_add2_i32(cpu_NF, cpu_CF, cpu_NF, cpu_CF, t1_32, tmp);
607 tcg_gen_mov_i32(cpu_ZF, cpu_NF);
608 tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
609 tcg_gen_xor_i32(tmp, t0_32, t1_32);
610 tcg_gen_andc_i32(cpu_VF, cpu_VF, tmp);
611 tcg_gen_extu_i32_i64(dest, cpu_NF);
613 tcg_temp_free_i32(tmp);
614 tcg_temp_free_i32(t1_32);
615 tcg_temp_free_i32(t0_32);
620 * Load/Store generators
624 * Store from GPR register to memory.
626 static void do_gpr_st_memidx(DisasContext *s, TCGv_i64 source,
627 TCGv_i64 tcg_addr, int size, int memidx)
629 g_assert(size <= 3);
630 tcg_gen_qemu_st_i64(source, tcg_addr, memidx, MO_TE + size);
633 static void do_gpr_st(DisasContext *s, TCGv_i64 source,
634 TCGv_i64 tcg_addr, int size)
636 do_gpr_st_memidx(s, source, tcg_addr, size, get_mem_index(s));
640 * Load from memory to GPR register
642 static void do_gpr_ld_memidx(DisasContext *s, TCGv_i64 dest, TCGv_i64 tcg_addr,
643 int size, bool is_signed, bool extend, int memidx)
645 TCGMemOp memop = MO_TE + size;
647 g_assert(size <= 3);
649 if (is_signed) {
650 memop += MO_SIGN;
653 tcg_gen_qemu_ld_i64(dest, tcg_addr, memidx, memop);
655 if (extend && is_signed) {
656 g_assert(size < 3);
657 tcg_gen_ext32u_i64(dest, dest);
661 static void do_gpr_ld(DisasContext *s, TCGv_i64 dest, TCGv_i64 tcg_addr,
662 int size, bool is_signed, bool extend)
664 do_gpr_ld_memidx(s, dest, tcg_addr, size, is_signed, extend,
665 get_mem_index(s));
669 * Store from FP register to memory
671 static void do_fp_st(DisasContext *s, int srcidx, TCGv_i64 tcg_addr, int size)
673 /* This writes the bottom N bits of a 128 bit wide vector to memory */
674 TCGv_i64 tmp = tcg_temp_new_i64();
675 tcg_gen_ld_i64(tmp, cpu_env, fp_reg_offset(srcidx, MO_64));
676 if (size < 4) {
677 tcg_gen_qemu_st_i64(tmp, tcg_addr, get_mem_index(s), MO_TE + size);
678 } else {
679 TCGv_i64 tcg_hiaddr = tcg_temp_new_i64();
680 tcg_gen_qemu_st_i64(tmp, tcg_addr, get_mem_index(s), MO_TEQ);
681 tcg_gen_qemu_st64(tmp, tcg_addr, get_mem_index(s));
682 tcg_gen_ld_i64(tmp, cpu_env, fp_reg_hi_offset(srcidx));
683 tcg_gen_addi_i64(tcg_hiaddr, tcg_addr, 8);
684 tcg_gen_qemu_st_i64(tmp, tcg_hiaddr, get_mem_index(s), MO_TEQ);
685 tcg_temp_free_i64(tcg_hiaddr);
688 tcg_temp_free_i64(tmp);
692 * Load from memory to FP register
694 static void do_fp_ld(DisasContext *s, int destidx, TCGv_i64 tcg_addr, int size)
696 /* This always zero-extends and writes to a full 128 bit wide vector */
697 TCGv_i64 tmplo = tcg_temp_new_i64();
698 TCGv_i64 tmphi;
700 if (size < 4) {
701 TCGMemOp memop = MO_TE + size;
702 tmphi = tcg_const_i64(0);
703 tcg_gen_qemu_ld_i64(tmplo, tcg_addr, get_mem_index(s), memop);
704 } else {
705 TCGv_i64 tcg_hiaddr;
706 tmphi = tcg_temp_new_i64();
707 tcg_hiaddr = tcg_temp_new_i64();
709 tcg_gen_qemu_ld_i64(tmplo, tcg_addr, get_mem_index(s), MO_TEQ);
710 tcg_gen_addi_i64(tcg_hiaddr, tcg_addr, 8);
711 tcg_gen_qemu_ld_i64(tmphi, tcg_hiaddr, get_mem_index(s), MO_TEQ);
712 tcg_temp_free_i64(tcg_hiaddr);
715 tcg_gen_st_i64(tmplo, cpu_env, fp_reg_offset(destidx, MO_64));
716 tcg_gen_st_i64(tmphi, cpu_env, fp_reg_hi_offset(destidx));
718 tcg_temp_free_i64(tmplo);
719 tcg_temp_free_i64(tmphi);
723 * Vector load/store helpers.
725 * The principal difference between this and a FP load is that we don't
726 * zero extend as we are filling a partial chunk of the vector register.
727 * These functions don't support 128 bit loads/stores, which would be
728 * normal load/store operations.
730 * The _i32 versions are useful when operating on 32 bit quantities
731 * (eg for floating point single or using Neon helper functions).
734 /* Get value of an element within a vector register */
735 static void read_vec_element(DisasContext *s, TCGv_i64 tcg_dest, int srcidx,
736 int element, TCGMemOp memop)
738 int vect_off = vec_reg_offset(srcidx, element, memop & MO_SIZE);
739 switch (memop) {
740 case MO_8:
741 tcg_gen_ld8u_i64(tcg_dest, cpu_env, vect_off);
742 break;
743 case MO_16:
744 tcg_gen_ld16u_i64(tcg_dest, cpu_env, vect_off);
745 break;
746 case MO_32:
747 tcg_gen_ld32u_i64(tcg_dest, cpu_env, vect_off);
748 break;
749 case MO_8|MO_SIGN:
750 tcg_gen_ld8s_i64(tcg_dest, cpu_env, vect_off);
751 break;
752 case MO_16|MO_SIGN:
753 tcg_gen_ld16s_i64(tcg_dest, cpu_env, vect_off);
754 break;
755 case MO_32|MO_SIGN:
756 tcg_gen_ld32s_i64(tcg_dest, cpu_env, vect_off);
757 break;
758 case MO_64:
759 case MO_64|MO_SIGN:
760 tcg_gen_ld_i64(tcg_dest, cpu_env, vect_off);
761 break;
762 default:
763 g_assert_not_reached();
767 static void read_vec_element_i32(DisasContext *s, TCGv_i32 tcg_dest, int srcidx,
768 int element, TCGMemOp memop)
770 int vect_off = vec_reg_offset(srcidx, element, memop & MO_SIZE);
771 switch (memop) {
772 case MO_8:
773 tcg_gen_ld8u_i32(tcg_dest, cpu_env, vect_off);
774 break;
775 case MO_16:
776 tcg_gen_ld16u_i32(tcg_dest, cpu_env, vect_off);
777 break;
778 case MO_8|MO_SIGN:
779 tcg_gen_ld8s_i32(tcg_dest, cpu_env, vect_off);
780 break;
781 case MO_16|MO_SIGN:
782 tcg_gen_ld16s_i32(tcg_dest, cpu_env, vect_off);
783 break;
784 case MO_32:
785 case MO_32|MO_SIGN:
786 tcg_gen_ld_i32(tcg_dest, cpu_env, vect_off);
787 break;
788 default:
789 g_assert_not_reached();
793 /* Set value of an element within a vector register */
794 static void write_vec_element(DisasContext *s, TCGv_i64 tcg_src, int destidx,
795 int element, TCGMemOp memop)
797 int vect_off = vec_reg_offset(destidx, element, memop & MO_SIZE);
798 switch (memop) {
799 case MO_8:
800 tcg_gen_st8_i64(tcg_src, cpu_env, vect_off);
801 break;
802 case MO_16:
803 tcg_gen_st16_i64(tcg_src, cpu_env, vect_off);
804 break;
805 case MO_32:
806 tcg_gen_st32_i64(tcg_src, cpu_env, vect_off);
807 break;
808 case MO_64:
809 tcg_gen_st_i64(tcg_src, cpu_env, vect_off);
810 break;
811 default:
812 g_assert_not_reached();
816 static void write_vec_element_i32(DisasContext *s, TCGv_i32 tcg_src,
817 int destidx, int element, TCGMemOp memop)
819 int vect_off = vec_reg_offset(destidx, element, memop & MO_SIZE);
820 switch (memop) {
821 case MO_8:
822 tcg_gen_st8_i32(tcg_src, cpu_env, vect_off);
823 break;
824 case MO_16:
825 tcg_gen_st16_i32(tcg_src, cpu_env, vect_off);
826 break;
827 case MO_32:
828 tcg_gen_st_i32(tcg_src, cpu_env, vect_off);
829 break;
830 default:
831 g_assert_not_reached();
835 /* Clear the high 64 bits of a 128 bit vector (in general non-quad
836 * vector ops all need to do this).
838 static void clear_vec_high(DisasContext *s, int rd)
840 TCGv_i64 tcg_zero = tcg_const_i64(0);
842 write_vec_element(s, tcg_zero, rd, 1, MO_64);
843 tcg_temp_free_i64(tcg_zero);
846 /* Store from vector register to memory */
847 static void do_vec_st(DisasContext *s, int srcidx, int element,
848 TCGv_i64 tcg_addr, int size)
850 TCGMemOp memop = MO_TE + size;
851 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
853 read_vec_element(s, tcg_tmp, srcidx, element, size);
854 tcg_gen_qemu_st_i64(tcg_tmp, tcg_addr, get_mem_index(s), memop);
856 tcg_temp_free_i64(tcg_tmp);
859 /* Load from memory to vector register */
860 static void do_vec_ld(DisasContext *s, int destidx, int element,
861 TCGv_i64 tcg_addr, int size)
863 TCGMemOp memop = MO_TE + size;
864 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
866 tcg_gen_qemu_ld_i64(tcg_tmp, tcg_addr, get_mem_index(s), memop);
867 write_vec_element(s, tcg_tmp, destidx, element, size);
869 tcg_temp_free_i64(tcg_tmp);
873 * This utility function is for doing register extension with an
874 * optional shift. You will likely want to pass a temporary for the
875 * destination register. See DecodeRegExtend() in the ARM ARM.
877 static void ext_and_shift_reg(TCGv_i64 tcg_out, TCGv_i64 tcg_in,
878 int option, unsigned int shift)
880 int extsize = extract32(option, 0, 2);
881 bool is_signed = extract32(option, 2, 1);
883 if (is_signed) {
884 switch (extsize) {
885 case 0:
886 tcg_gen_ext8s_i64(tcg_out, tcg_in);
887 break;
888 case 1:
889 tcg_gen_ext16s_i64(tcg_out, tcg_in);
890 break;
891 case 2:
892 tcg_gen_ext32s_i64(tcg_out, tcg_in);
893 break;
894 case 3:
895 tcg_gen_mov_i64(tcg_out, tcg_in);
896 break;
898 } else {
899 switch (extsize) {
900 case 0:
901 tcg_gen_ext8u_i64(tcg_out, tcg_in);
902 break;
903 case 1:
904 tcg_gen_ext16u_i64(tcg_out, tcg_in);
905 break;
906 case 2:
907 tcg_gen_ext32u_i64(tcg_out, tcg_in);
908 break;
909 case 3:
910 tcg_gen_mov_i64(tcg_out, tcg_in);
911 break;
915 if (shift) {
916 tcg_gen_shli_i64(tcg_out, tcg_out, shift);
920 static inline void gen_check_sp_alignment(DisasContext *s)
922 /* The AArch64 architecture mandates that (if enabled via PSTATE
923 * or SCTLR bits) there is a check that SP is 16-aligned on every
924 * SP-relative load or store (with an exception generated if it is not).
925 * In line with general QEMU practice regarding misaligned accesses,
926 * we omit these checks for the sake of guest program performance.
927 * This function is provided as a hook so we can more easily add these
928 * checks in future (possibly as a "favour catching guest program bugs
929 * over speed" user selectable option).
934 * This provides a simple table based table lookup decoder. It is
935 * intended to be used when the relevant bits for decode are too
936 * awkwardly placed and switch/if based logic would be confusing and
937 * deeply nested. Since it's a linear search through the table, tables
938 * should be kept small.
940 * It returns the first handler where insn & mask == pattern, or
941 * NULL if there is no match.
942 * The table is terminated by an empty mask (i.e. 0)
944 static inline AArch64DecodeFn *lookup_disas_fn(const AArch64DecodeTable *table,
945 uint32_t insn)
947 const AArch64DecodeTable *tptr = table;
949 while (tptr->mask) {
950 if ((insn & tptr->mask) == tptr->pattern) {
951 return tptr->disas_fn;
953 tptr++;
955 return NULL;
959 * the instruction disassembly implemented here matches
960 * the instruction encoding classifications in chapter 3 (C3)
961 * of the ARM Architecture Reference Manual (DDI0487A_a)
964 /* C3.2.7 Unconditional branch (immediate)
965 * 31 30 26 25 0
966 * +----+-----------+-------------------------------------+
967 * | op | 0 0 1 0 1 | imm26 |
968 * +----+-----------+-------------------------------------+
970 static void disas_uncond_b_imm(DisasContext *s, uint32_t insn)
972 uint64_t addr = s->pc + sextract32(insn, 0, 26) * 4 - 4;
974 if (insn & (1 << 31)) {
975 /* C5.6.26 BL Branch with link */
976 tcg_gen_movi_i64(cpu_reg(s, 30), s->pc);
979 /* C5.6.20 B Branch / C5.6.26 BL Branch with link */
980 gen_goto_tb(s, 0, addr);
983 /* C3.2.1 Compare & branch (immediate)
984 * 31 30 25 24 23 5 4 0
985 * +----+-------------+----+---------------------+--------+
986 * | sf | 0 1 1 0 1 0 | op | imm19 | Rt |
987 * +----+-------------+----+---------------------+--------+
989 static void disas_comp_b_imm(DisasContext *s, uint32_t insn)
991 unsigned int sf, op, rt;
992 uint64_t addr;
993 int label_match;
994 TCGv_i64 tcg_cmp;
996 sf = extract32(insn, 31, 1);
997 op = extract32(insn, 24, 1); /* 0: CBZ; 1: CBNZ */
998 rt = extract32(insn, 0, 5);
999 addr = s->pc + sextract32(insn, 5, 19) * 4 - 4;
1001 tcg_cmp = read_cpu_reg(s, rt, sf);
1002 label_match = gen_new_label();
1004 tcg_gen_brcondi_i64(op ? TCG_COND_NE : TCG_COND_EQ,
1005 tcg_cmp, 0, label_match);
1007 gen_goto_tb(s, 0, s->pc);
1008 gen_set_label(label_match);
1009 gen_goto_tb(s, 1, addr);
1012 /* C3.2.5 Test & branch (immediate)
1013 * 31 30 25 24 23 19 18 5 4 0
1014 * +----+-------------+----+-------+-------------+------+
1015 * | b5 | 0 1 1 0 1 1 | op | b40 | imm14 | Rt |
1016 * +----+-------------+----+-------+-------------+------+
1018 static void disas_test_b_imm(DisasContext *s, uint32_t insn)
1020 unsigned int bit_pos, op, rt;
1021 uint64_t addr;
1022 int label_match;
1023 TCGv_i64 tcg_cmp;
1025 bit_pos = (extract32(insn, 31, 1) << 5) | extract32(insn, 19, 5);
1026 op = extract32(insn, 24, 1); /* 0: TBZ; 1: TBNZ */
1027 addr = s->pc + sextract32(insn, 5, 14) * 4 - 4;
1028 rt = extract32(insn, 0, 5);
1030 tcg_cmp = tcg_temp_new_i64();
1031 tcg_gen_andi_i64(tcg_cmp, cpu_reg(s, rt), (1ULL << bit_pos));
1032 label_match = gen_new_label();
1033 tcg_gen_brcondi_i64(op ? TCG_COND_NE : TCG_COND_EQ,
1034 tcg_cmp, 0, label_match);
1035 tcg_temp_free_i64(tcg_cmp);
1036 gen_goto_tb(s, 0, s->pc);
1037 gen_set_label(label_match);
1038 gen_goto_tb(s, 1, addr);
1041 /* C3.2.2 / C5.6.19 Conditional branch (immediate)
1042 * 31 25 24 23 5 4 3 0
1043 * +---------------+----+---------------------+----+------+
1044 * | 0 1 0 1 0 1 0 | o1 | imm19 | o0 | cond |
1045 * +---------------+----+---------------------+----+------+
1047 static void disas_cond_b_imm(DisasContext *s, uint32_t insn)
1049 unsigned int cond;
1050 uint64_t addr;
1052 if ((insn & (1 << 4)) || (insn & (1 << 24))) {
1053 unallocated_encoding(s);
1054 return;
1056 addr = s->pc + sextract32(insn, 5, 19) * 4 - 4;
1057 cond = extract32(insn, 0, 4);
1059 if (cond < 0x0e) {
1060 /* genuinely conditional branches */
1061 int label_match = gen_new_label();
1062 arm_gen_test_cc(cond, label_match);
1063 gen_goto_tb(s, 0, s->pc);
1064 gen_set_label(label_match);
1065 gen_goto_tb(s, 1, addr);
1066 } else {
1067 /* 0xe and 0xf are both "always" conditions */
1068 gen_goto_tb(s, 0, addr);
1072 /* C5.6.68 HINT */
1073 static void handle_hint(DisasContext *s, uint32_t insn,
1074 unsigned int op1, unsigned int op2, unsigned int crm)
1076 unsigned int selector = crm << 3 | op2;
1078 if (op1 != 3) {
1079 unallocated_encoding(s);
1080 return;
1083 switch (selector) {
1084 case 0: /* NOP */
1085 return;
1086 case 3: /* WFI */
1087 s->is_jmp = DISAS_WFI;
1088 return;
1089 case 1: /* YIELD */
1090 case 2: /* WFE */
1091 case 4: /* SEV */
1092 case 5: /* SEVL */
1093 /* we treat all as NOP at least for now */
1094 return;
1095 default:
1096 /* default specified as NOP equivalent */
1097 return;
1101 static void gen_clrex(DisasContext *s, uint32_t insn)
1103 tcg_gen_movi_i64(cpu_exclusive_addr, -1);
1106 /* CLREX, DSB, DMB, ISB */
1107 static void handle_sync(DisasContext *s, uint32_t insn,
1108 unsigned int op1, unsigned int op2, unsigned int crm)
1110 if (op1 != 3) {
1111 unallocated_encoding(s);
1112 return;
1115 switch (op2) {
1116 case 2: /* CLREX */
1117 gen_clrex(s, insn);
1118 return;
1119 case 4: /* DSB */
1120 case 5: /* DMB */
1121 case 6: /* ISB */
1122 /* We don't emulate caches so barriers are no-ops */
1123 return;
1124 default:
1125 unallocated_encoding(s);
1126 return;
1130 /* C5.6.130 MSR (immediate) - move immediate to processor state field */
1131 static void handle_msr_i(DisasContext *s, uint32_t insn,
1132 unsigned int op1, unsigned int op2, unsigned int crm)
1134 int op = op1 << 3 | op2;
1135 switch (op) {
1136 case 0x05: /* SPSel */
1137 if (s->current_pl == 0) {
1138 unallocated_encoding(s);
1139 return;
1141 /* fall through */
1142 case 0x1e: /* DAIFSet */
1143 case 0x1f: /* DAIFClear */
1145 TCGv_i32 tcg_imm = tcg_const_i32(crm);
1146 TCGv_i32 tcg_op = tcg_const_i32(op);
1147 gen_a64_set_pc_im(s->pc - 4);
1148 gen_helper_msr_i_pstate(cpu_env, tcg_op, tcg_imm);
1149 tcg_temp_free_i32(tcg_imm);
1150 tcg_temp_free_i32(tcg_op);
1151 s->is_jmp = DISAS_UPDATE;
1152 break;
1154 default:
1155 unallocated_encoding(s);
1156 return;
1160 static void gen_get_nzcv(TCGv_i64 tcg_rt)
1162 TCGv_i32 tmp = tcg_temp_new_i32();
1163 TCGv_i32 nzcv = tcg_temp_new_i32();
1165 /* build bit 31, N */
1166 tcg_gen_andi_i32(nzcv, cpu_NF, (1 << 31));
1167 /* build bit 30, Z */
1168 tcg_gen_setcondi_i32(TCG_COND_EQ, tmp, cpu_ZF, 0);
1169 tcg_gen_deposit_i32(nzcv, nzcv, tmp, 30, 1);
1170 /* build bit 29, C */
1171 tcg_gen_deposit_i32(nzcv, nzcv, cpu_CF, 29, 1);
1172 /* build bit 28, V */
1173 tcg_gen_shri_i32(tmp, cpu_VF, 31);
1174 tcg_gen_deposit_i32(nzcv, nzcv, tmp, 28, 1);
1175 /* generate result */
1176 tcg_gen_extu_i32_i64(tcg_rt, nzcv);
1178 tcg_temp_free_i32(nzcv);
1179 tcg_temp_free_i32(tmp);
1182 static void gen_set_nzcv(TCGv_i64 tcg_rt)
1185 TCGv_i32 nzcv = tcg_temp_new_i32();
1187 /* take NZCV from R[t] */
1188 tcg_gen_trunc_i64_i32(nzcv, tcg_rt);
1190 /* bit 31, N */
1191 tcg_gen_andi_i32(cpu_NF, nzcv, (1 << 31));
1192 /* bit 30, Z */
1193 tcg_gen_andi_i32(cpu_ZF, nzcv, (1 << 30));
1194 tcg_gen_setcondi_i32(TCG_COND_EQ, cpu_ZF, cpu_ZF, 0);
1195 /* bit 29, C */
1196 tcg_gen_andi_i32(cpu_CF, nzcv, (1 << 29));
1197 tcg_gen_shri_i32(cpu_CF, cpu_CF, 29);
1198 /* bit 28, V */
1199 tcg_gen_andi_i32(cpu_VF, nzcv, (1 << 28));
1200 tcg_gen_shli_i32(cpu_VF, cpu_VF, 3);
1201 tcg_temp_free_i32(nzcv);
1204 /* C5.6.129 MRS - move from system register
1205 * C5.6.131 MSR (register) - move to system register
1206 * C5.6.204 SYS
1207 * C5.6.205 SYSL
1208 * These are all essentially the same insn in 'read' and 'write'
1209 * versions, with varying op0 fields.
1211 static void handle_sys(DisasContext *s, uint32_t insn, bool isread,
1212 unsigned int op0, unsigned int op1, unsigned int op2,
1213 unsigned int crn, unsigned int crm, unsigned int rt)
1215 const ARMCPRegInfo *ri;
1216 TCGv_i64 tcg_rt;
1218 ri = get_arm_cp_reginfo(s->cp_regs,
1219 ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP,
1220 crn, crm, op0, op1, op2));
1222 if (!ri) {
1223 /* Unknown register; this might be a guest error or a QEMU
1224 * unimplemented feature.
1226 qemu_log_mask(LOG_UNIMP, "%s access to unsupported AArch64 "
1227 "system register op0:%d op1:%d crn:%d crm:%d op2:%d\n",
1228 isread ? "read" : "write", op0, op1, crn, crm, op2);
1229 unallocated_encoding(s);
1230 return;
1233 /* Check access permissions */
1234 if (!cp_access_ok(s->current_pl, ri, isread)) {
1235 unallocated_encoding(s);
1236 return;
1239 if (ri->accessfn) {
1240 /* Emit code to perform further access permissions checks at
1241 * runtime; this may result in an exception.
1243 TCGv_ptr tmpptr;
1244 gen_a64_set_pc_im(s->pc - 4);
1245 tmpptr = tcg_const_ptr(ri);
1246 gen_helper_access_check_cp_reg(cpu_env, tmpptr);
1247 tcg_temp_free_ptr(tmpptr);
1250 /* Handle special cases first */
1251 switch (ri->type & ~(ARM_CP_FLAG_MASK & ~ARM_CP_SPECIAL)) {
1252 case ARM_CP_NOP:
1253 return;
1254 case ARM_CP_NZCV:
1255 tcg_rt = cpu_reg(s, rt);
1256 if (isread) {
1257 gen_get_nzcv(tcg_rt);
1258 } else {
1259 gen_set_nzcv(tcg_rt);
1261 return;
1262 case ARM_CP_CURRENTEL:
1263 /* Reads as current EL value from pstate, which is
1264 * guaranteed to be constant by the tb flags.
1266 tcg_rt = cpu_reg(s, rt);
1267 tcg_gen_movi_i64(tcg_rt, s->current_pl << 2);
1268 return;
1269 default:
1270 break;
1273 if (use_icount && (ri->type & ARM_CP_IO)) {
1274 gen_io_start();
1277 tcg_rt = cpu_reg(s, rt);
1279 if (isread) {
1280 if (ri->type & ARM_CP_CONST) {
1281 tcg_gen_movi_i64(tcg_rt, ri->resetvalue);
1282 } else if (ri->readfn) {
1283 TCGv_ptr tmpptr;
1284 tmpptr = tcg_const_ptr(ri);
1285 gen_helper_get_cp_reg64(tcg_rt, cpu_env, tmpptr);
1286 tcg_temp_free_ptr(tmpptr);
1287 } else {
1288 tcg_gen_ld_i64(tcg_rt, cpu_env, ri->fieldoffset);
1290 } else {
1291 if (ri->type & ARM_CP_CONST) {
1292 /* If not forbidden by access permissions, treat as WI */
1293 return;
1294 } else if (ri->writefn) {
1295 TCGv_ptr tmpptr;
1296 tmpptr = tcg_const_ptr(ri);
1297 gen_helper_set_cp_reg64(cpu_env, tmpptr, tcg_rt);
1298 tcg_temp_free_ptr(tmpptr);
1299 } else {
1300 tcg_gen_st_i64(tcg_rt, cpu_env, ri->fieldoffset);
1304 if (use_icount && (ri->type & ARM_CP_IO)) {
1305 /* I/O operations must end the TB here (whether read or write) */
1306 gen_io_end();
1307 s->is_jmp = DISAS_UPDATE;
1308 } else if (!isread && !(ri->type & ARM_CP_SUPPRESS_TB_END)) {
1309 /* We default to ending the TB on a coprocessor register write,
1310 * but allow this to be suppressed by the register definition
1311 * (usually only necessary to work around guest bugs).
1313 s->is_jmp = DISAS_UPDATE;
1317 /* C3.2.4 System
1318 * 31 22 21 20 19 18 16 15 12 11 8 7 5 4 0
1319 * +---------------------+---+-----+-----+-------+-------+-----+------+
1320 * | 1 1 0 1 0 1 0 1 0 0 | L | op0 | op1 | CRn | CRm | op2 | Rt |
1321 * +---------------------+---+-----+-----+-------+-------+-----+------+
1323 static void disas_system(DisasContext *s, uint32_t insn)
1325 unsigned int l, op0, op1, crn, crm, op2, rt;
1326 l = extract32(insn, 21, 1);
1327 op0 = extract32(insn, 19, 2);
1328 op1 = extract32(insn, 16, 3);
1329 crn = extract32(insn, 12, 4);
1330 crm = extract32(insn, 8, 4);
1331 op2 = extract32(insn, 5, 3);
1332 rt = extract32(insn, 0, 5);
1334 if (op0 == 0) {
1335 if (l || rt != 31) {
1336 unallocated_encoding(s);
1337 return;
1339 switch (crn) {
1340 case 2: /* C5.6.68 HINT */
1341 handle_hint(s, insn, op1, op2, crm);
1342 break;
1343 case 3: /* CLREX, DSB, DMB, ISB */
1344 handle_sync(s, insn, op1, op2, crm);
1345 break;
1346 case 4: /* C5.6.130 MSR (immediate) */
1347 handle_msr_i(s, insn, op1, op2, crm);
1348 break;
1349 default:
1350 unallocated_encoding(s);
1351 break;
1353 return;
1355 handle_sys(s, insn, l, op0, op1, op2, crn, crm, rt);
1358 /* C3.2.3 Exception generation
1360 * 31 24 23 21 20 5 4 2 1 0
1361 * +-----------------+-----+------------------------+-----+----+
1362 * | 1 1 0 1 0 1 0 0 | opc | imm16 | op2 | LL |
1363 * +-----------------------+------------------------+----------+
1365 static void disas_exc(DisasContext *s, uint32_t insn)
1367 int opc = extract32(insn, 21, 3);
1368 int op2_ll = extract32(insn, 0, 5);
1370 switch (opc) {
1371 case 0:
1372 /* SVC, HVC, SMC; since we don't support the Virtualization
1373 * or TrustZone extensions these all UNDEF except SVC.
1375 if (op2_ll != 1) {
1376 unallocated_encoding(s);
1377 break;
1379 gen_exception_insn(s, 0, EXCP_SWI);
1380 break;
1381 case 1:
1382 if (op2_ll != 0) {
1383 unallocated_encoding(s);
1384 break;
1386 /* BRK */
1387 gen_exception_insn(s, 0, EXCP_BKPT);
1388 break;
1389 case 2:
1390 if (op2_ll != 0) {
1391 unallocated_encoding(s);
1392 break;
1394 /* HLT */
1395 unsupported_encoding(s, insn);
1396 break;
1397 case 5:
1398 if (op2_ll < 1 || op2_ll > 3) {
1399 unallocated_encoding(s);
1400 break;
1402 /* DCPS1, DCPS2, DCPS3 */
1403 unsupported_encoding(s, insn);
1404 break;
1405 default:
1406 unallocated_encoding(s);
1407 break;
1411 /* C3.2.7 Unconditional branch (register)
1412 * 31 25 24 21 20 16 15 10 9 5 4 0
1413 * +---------------+-------+-------+-------+------+-------+
1414 * | 1 1 0 1 0 1 1 | opc | op2 | op3 | Rn | op4 |
1415 * +---------------+-------+-------+-------+------+-------+
1417 static void disas_uncond_b_reg(DisasContext *s, uint32_t insn)
1419 unsigned int opc, op2, op3, rn, op4;
1421 opc = extract32(insn, 21, 4);
1422 op2 = extract32(insn, 16, 5);
1423 op3 = extract32(insn, 10, 6);
1424 rn = extract32(insn, 5, 5);
1425 op4 = extract32(insn, 0, 5);
1427 if (op4 != 0x0 || op3 != 0x0 || op2 != 0x1f) {
1428 unallocated_encoding(s);
1429 return;
1432 switch (opc) {
1433 case 0: /* BR */
1434 case 2: /* RET */
1435 break;
1436 case 1: /* BLR */
1437 tcg_gen_movi_i64(cpu_reg(s, 30), s->pc);
1438 break;
1439 case 4: /* ERET */
1440 case 5: /* DRPS */
1441 if (rn != 0x1f) {
1442 unallocated_encoding(s);
1443 } else {
1444 unsupported_encoding(s, insn);
1446 return;
1447 default:
1448 unallocated_encoding(s);
1449 return;
1452 tcg_gen_mov_i64(cpu_pc, cpu_reg(s, rn));
1453 s->is_jmp = DISAS_JUMP;
1456 /* C3.2 Branches, exception generating and system instructions */
1457 static void disas_b_exc_sys(DisasContext *s, uint32_t insn)
1459 switch (extract32(insn, 25, 7)) {
1460 case 0x0a: case 0x0b:
1461 case 0x4a: case 0x4b: /* Unconditional branch (immediate) */
1462 disas_uncond_b_imm(s, insn);
1463 break;
1464 case 0x1a: case 0x5a: /* Compare & branch (immediate) */
1465 disas_comp_b_imm(s, insn);
1466 break;
1467 case 0x1b: case 0x5b: /* Test & branch (immediate) */
1468 disas_test_b_imm(s, insn);
1469 break;
1470 case 0x2a: /* Conditional branch (immediate) */
1471 disas_cond_b_imm(s, insn);
1472 break;
1473 case 0x6a: /* Exception generation / System */
1474 if (insn & (1 << 24)) {
1475 disas_system(s, insn);
1476 } else {
1477 disas_exc(s, insn);
1479 break;
1480 case 0x6b: /* Unconditional branch (register) */
1481 disas_uncond_b_reg(s, insn);
1482 break;
1483 default:
1484 unallocated_encoding(s);
1485 break;
1490 * Load/Store exclusive instructions are implemented by remembering
1491 * the value/address loaded, and seeing if these are the same
1492 * when the store is performed. This is not actually the architecturally
1493 * mandated semantics, but it works for typical guest code sequences
1494 * and avoids having to monitor regular stores.
1496 * In system emulation mode only one CPU will be running at once, so
1497 * this sequence is effectively atomic. In user emulation mode we
1498 * throw an exception and handle the atomic operation elsewhere.
1500 static void gen_load_exclusive(DisasContext *s, int rt, int rt2,
1501 TCGv_i64 addr, int size, bool is_pair)
1503 TCGv_i64 tmp = tcg_temp_new_i64();
1504 TCGMemOp memop = MO_TE + size;
1506 g_assert(size <= 3);
1507 tcg_gen_qemu_ld_i64(tmp, addr, get_mem_index(s), memop);
1509 if (is_pair) {
1510 TCGv_i64 addr2 = tcg_temp_new_i64();
1511 TCGv_i64 hitmp = tcg_temp_new_i64();
1513 g_assert(size >= 2);
1514 tcg_gen_addi_i64(addr2, addr, 1 << size);
1515 tcg_gen_qemu_ld_i64(hitmp, addr2, get_mem_index(s), memop);
1516 tcg_temp_free_i64(addr2);
1517 tcg_gen_mov_i64(cpu_exclusive_high, hitmp);
1518 tcg_gen_mov_i64(cpu_reg(s, rt2), hitmp);
1519 tcg_temp_free_i64(hitmp);
1522 tcg_gen_mov_i64(cpu_exclusive_val, tmp);
1523 tcg_gen_mov_i64(cpu_reg(s, rt), tmp);
1525 tcg_temp_free_i64(tmp);
1526 tcg_gen_mov_i64(cpu_exclusive_addr, addr);
1529 #ifdef CONFIG_USER_ONLY
1530 static void gen_store_exclusive(DisasContext *s, int rd, int rt, int rt2,
1531 TCGv_i64 addr, int size, int is_pair)
1533 tcg_gen_mov_i64(cpu_exclusive_test, addr);
1534 tcg_gen_movi_i32(cpu_exclusive_info,
1535 size | is_pair << 2 | (rd << 4) | (rt << 9) | (rt2 << 14));
1536 gen_exception_insn(s, 4, EXCP_STREX);
1538 #else
1539 static void gen_store_exclusive(DisasContext *s, int rd, int rt, int rt2,
1540 TCGv_i64 inaddr, int size, int is_pair)
1542 /* if (env->exclusive_addr == addr && env->exclusive_val == [addr]
1543 * && (!is_pair || env->exclusive_high == [addr + datasize])) {
1544 * [addr] = {Rt};
1545 * if (is_pair) {
1546 * [addr + datasize] = {Rt2};
1548 * {Rd} = 0;
1549 * } else {
1550 * {Rd} = 1;
1552 * env->exclusive_addr = -1;
1554 int fail_label = gen_new_label();
1555 int done_label = gen_new_label();
1556 TCGv_i64 addr = tcg_temp_local_new_i64();
1557 TCGv_i64 tmp;
1559 /* Copy input into a local temp so it is not trashed when the
1560 * basic block ends at the branch insn.
1562 tcg_gen_mov_i64(addr, inaddr);
1563 tcg_gen_brcond_i64(TCG_COND_NE, addr, cpu_exclusive_addr, fail_label);
1565 tmp = tcg_temp_new_i64();
1566 tcg_gen_qemu_ld_i64(tmp, addr, get_mem_index(s), MO_TE + size);
1567 tcg_gen_brcond_i64(TCG_COND_NE, tmp, cpu_exclusive_val, fail_label);
1568 tcg_temp_free_i64(tmp);
1570 if (is_pair) {
1571 TCGv_i64 addrhi = tcg_temp_new_i64();
1572 TCGv_i64 tmphi = tcg_temp_new_i64();
1574 tcg_gen_addi_i64(addrhi, addr, 1 << size);
1575 tcg_gen_qemu_ld_i64(tmphi, addrhi, get_mem_index(s), MO_TE + size);
1576 tcg_gen_brcond_i64(TCG_COND_NE, tmphi, cpu_exclusive_high, fail_label);
1578 tcg_temp_free_i64(tmphi);
1579 tcg_temp_free_i64(addrhi);
1582 /* We seem to still have the exclusive monitor, so do the store */
1583 tcg_gen_qemu_st_i64(cpu_reg(s, rt), addr, get_mem_index(s), MO_TE + size);
1584 if (is_pair) {
1585 TCGv_i64 addrhi = tcg_temp_new_i64();
1587 tcg_gen_addi_i64(addrhi, addr, 1 << size);
1588 tcg_gen_qemu_st_i64(cpu_reg(s, rt2), addrhi,
1589 get_mem_index(s), MO_TE + size);
1590 tcg_temp_free_i64(addrhi);
1593 tcg_temp_free_i64(addr);
1595 tcg_gen_movi_i64(cpu_reg(s, rd), 0);
1596 tcg_gen_br(done_label);
1597 gen_set_label(fail_label);
1598 tcg_gen_movi_i64(cpu_reg(s, rd), 1);
1599 gen_set_label(done_label);
1600 tcg_gen_movi_i64(cpu_exclusive_addr, -1);
1603 #endif
1605 /* C3.3.6 Load/store exclusive
1607 * 31 30 29 24 23 22 21 20 16 15 14 10 9 5 4 0
1608 * +-----+-------------+----+---+----+------+----+-------+------+------+
1609 * | sz | 0 0 1 0 0 0 | o2 | L | o1 | Rs | o0 | Rt2 | Rn | Rt |
1610 * +-----+-------------+----+---+----+------+----+-------+------+------+
1612 * sz: 00 -> 8 bit, 01 -> 16 bit, 10 -> 32 bit, 11 -> 64 bit
1613 * L: 0 -> store, 1 -> load
1614 * o2: 0 -> exclusive, 1 -> not
1615 * o1: 0 -> single register, 1 -> register pair
1616 * o0: 1 -> load-acquire/store-release, 0 -> not
1618 * o0 == 0 AND o2 == 1 is un-allocated
1619 * o1 == 1 is un-allocated except for 32 and 64 bit sizes
1621 static void disas_ldst_excl(DisasContext *s, uint32_t insn)
1623 int rt = extract32(insn, 0, 5);
1624 int rn = extract32(insn, 5, 5);
1625 int rt2 = extract32(insn, 10, 5);
1626 int is_lasr = extract32(insn, 15, 1);
1627 int rs = extract32(insn, 16, 5);
1628 int is_pair = extract32(insn, 21, 1);
1629 int is_store = !extract32(insn, 22, 1);
1630 int is_excl = !extract32(insn, 23, 1);
1631 int size = extract32(insn, 30, 2);
1632 TCGv_i64 tcg_addr;
1634 if ((!is_excl && !is_lasr) ||
1635 (is_pair && size < 2)) {
1636 unallocated_encoding(s);
1637 return;
1640 if (rn == 31) {
1641 gen_check_sp_alignment(s);
1643 tcg_addr = read_cpu_reg_sp(s, rn, 1);
1645 /* Note that since TCG is single threaded load-acquire/store-release
1646 * semantics require no extra if (is_lasr) { ... } handling.
1649 if (is_excl) {
1650 if (!is_store) {
1651 gen_load_exclusive(s, rt, rt2, tcg_addr, size, is_pair);
1652 } else {
1653 gen_store_exclusive(s, rs, rt, rt2, tcg_addr, size, is_pair);
1655 } else {
1656 TCGv_i64 tcg_rt = cpu_reg(s, rt);
1657 if (is_store) {
1658 do_gpr_st(s, tcg_rt, tcg_addr, size);
1659 } else {
1660 do_gpr_ld(s, tcg_rt, tcg_addr, size, false, false);
1662 if (is_pair) {
1663 TCGv_i64 tcg_rt2 = cpu_reg(s, rt);
1664 tcg_gen_addi_i64(tcg_addr, tcg_addr, 1 << size);
1665 if (is_store) {
1666 do_gpr_st(s, tcg_rt2, tcg_addr, size);
1667 } else {
1668 do_gpr_ld(s, tcg_rt2, tcg_addr, size, false, false);
1675 * C3.3.5 Load register (literal)
1677 * 31 30 29 27 26 25 24 23 5 4 0
1678 * +-----+-------+---+-----+-------------------+-------+
1679 * | opc | 0 1 1 | V | 0 0 | imm19 | Rt |
1680 * +-----+-------+---+-----+-------------------+-------+
1682 * V: 1 -> vector (simd/fp)
1683 * opc (non-vector): 00 -> 32 bit, 01 -> 64 bit,
1684 * 10-> 32 bit signed, 11 -> prefetch
1685 * opc (vector): 00 -> 32 bit, 01 -> 64 bit, 10 -> 128 bit (11 unallocated)
1687 static void disas_ld_lit(DisasContext *s, uint32_t insn)
1689 int rt = extract32(insn, 0, 5);
1690 int64_t imm = sextract32(insn, 5, 19) << 2;
1691 bool is_vector = extract32(insn, 26, 1);
1692 int opc = extract32(insn, 30, 2);
1693 bool is_signed = false;
1694 int size = 2;
1695 TCGv_i64 tcg_rt, tcg_addr;
1697 if (is_vector) {
1698 if (opc == 3) {
1699 unallocated_encoding(s);
1700 return;
1702 size = 2 + opc;
1703 } else {
1704 if (opc == 3) {
1705 /* PRFM (literal) : prefetch */
1706 return;
1708 size = 2 + extract32(opc, 0, 1);
1709 is_signed = extract32(opc, 1, 1);
1712 tcg_rt = cpu_reg(s, rt);
1714 tcg_addr = tcg_const_i64((s->pc - 4) + imm);
1715 if (is_vector) {
1716 do_fp_ld(s, rt, tcg_addr, size);
1717 } else {
1718 do_gpr_ld(s, tcg_rt, tcg_addr, size, is_signed, false);
1720 tcg_temp_free_i64(tcg_addr);
1724 * C5.6.80 LDNP (Load Pair - non-temporal hint)
1725 * C5.6.81 LDP (Load Pair - non vector)
1726 * C5.6.82 LDPSW (Load Pair Signed Word - non vector)
1727 * C5.6.176 STNP (Store Pair - non-temporal hint)
1728 * C5.6.177 STP (Store Pair - non vector)
1729 * C6.3.165 LDNP (Load Pair of SIMD&FP - non-temporal hint)
1730 * C6.3.165 LDP (Load Pair of SIMD&FP)
1731 * C6.3.284 STNP (Store Pair of SIMD&FP - non-temporal hint)
1732 * C6.3.284 STP (Store Pair of SIMD&FP)
1734 * 31 30 29 27 26 25 24 23 22 21 15 14 10 9 5 4 0
1735 * +-----+-------+---+---+-------+---+-----------------------------+
1736 * | opc | 1 0 1 | V | 0 | index | L | imm7 | Rt2 | Rn | Rt |
1737 * +-----+-------+---+---+-------+---+-------+-------+------+------+
1739 * opc: LDP/STP/LDNP/STNP 00 -> 32 bit, 10 -> 64 bit
1740 * LDPSW 01
1741 * LDP/STP/LDNP/STNP (SIMD) 00 -> 32 bit, 01 -> 64 bit, 10 -> 128 bit
1742 * V: 0 -> GPR, 1 -> Vector
1743 * idx: 00 -> signed offset with non-temporal hint, 01 -> post-index,
1744 * 10 -> signed offset, 11 -> pre-index
1745 * L: 0 -> Store 1 -> Load
1747 * Rt, Rt2 = GPR or SIMD registers to be stored
1748 * Rn = general purpose register containing address
1749 * imm7 = signed offset (multiple of 4 or 8 depending on size)
1751 static void disas_ldst_pair(DisasContext *s, uint32_t insn)
1753 int rt = extract32(insn, 0, 5);
1754 int rn = extract32(insn, 5, 5);
1755 int rt2 = extract32(insn, 10, 5);
1756 int64_t offset = sextract32(insn, 15, 7);
1757 int index = extract32(insn, 23, 2);
1758 bool is_vector = extract32(insn, 26, 1);
1759 bool is_load = extract32(insn, 22, 1);
1760 int opc = extract32(insn, 30, 2);
1762 bool is_signed = false;
1763 bool postindex = false;
1764 bool wback = false;
1766 TCGv_i64 tcg_addr; /* calculated address */
1767 int size;
1769 if (opc == 3) {
1770 unallocated_encoding(s);
1771 return;
1774 if (is_vector) {
1775 size = 2 + opc;
1776 } else {
1777 size = 2 + extract32(opc, 1, 1);
1778 is_signed = extract32(opc, 0, 1);
1779 if (!is_load && is_signed) {
1780 unallocated_encoding(s);
1781 return;
1785 switch (index) {
1786 case 1: /* post-index */
1787 postindex = true;
1788 wback = true;
1789 break;
1790 case 0:
1791 /* signed offset with "non-temporal" hint. Since we don't emulate
1792 * caches we don't care about hints to the cache system about
1793 * data access patterns, and handle this identically to plain
1794 * signed offset.
1796 if (is_signed) {
1797 /* There is no non-temporal-hint version of LDPSW */
1798 unallocated_encoding(s);
1799 return;
1801 postindex = false;
1802 break;
1803 case 2: /* signed offset, rn not updated */
1804 postindex = false;
1805 break;
1806 case 3: /* pre-index */
1807 postindex = false;
1808 wback = true;
1809 break;
1812 offset <<= size;
1814 if (rn == 31) {
1815 gen_check_sp_alignment(s);
1818 tcg_addr = read_cpu_reg_sp(s, rn, 1);
1820 if (!postindex) {
1821 tcg_gen_addi_i64(tcg_addr, tcg_addr, offset);
1824 if (is_vector) {
1825 if (is_load) {
1826 do_fp_ld(s, rt, tcg_addr, size);
1827 } else {
1828 do_fp_st(s, rt, tcg_addr, size);
1830 } else {
1831 TCGv_i64 tcg_rt = cpu_reg(s, rt);
1832 if (is_load) {
1833 do_gpr_ld(s, tcg_rt, tcg_addr, size, is_signed, false);
1834 } else {
1835 do_gpr_st(s, tcg_rt, tcg_addr, size);
1838 tcg_gen_addi_i64(tcg_addr, tcg_addr, 1 << size);
1839 if (is_vector) {
1840 if (is_load) {
1841 do_fp_ld(s, rt2, tcg_addr, size);
1842 } else {
1843 do_fp_st(s, rt2, tcg_addr, size);
1845 } else {
1846 TCGv_i64 tcg_rt2 = cpu_reg(s, rt2);
1847 if (is_load) {
1848 do_gpr_ld(s, tcg_rt2, tcg_addr, size, is_signed, false);
1849 } else {
1850 do_gpr_st(s, tcg_rt2, tcg_addr, size);
1854 if (wback) {
1855 if (postindex) {
1856 tcg_gen_addi_i64(tcg_addr, tcg_addr, offset - (1 << size));
1857 } else {
1858 tcg_gen_subi_i64(tcg_addr, tcg_addr, 1 << size);
1860 tcg_gen_mov_i64(cpu_reg_sp(s, rn), tcg_addr);
1865 * C3.3.8 Load/store (immediate post-indexed)
1866 * C3.3.9 Load/store (immediate pre-indexed)
1867 * C3.3.12 Load/store (unscaled immediate)
1869 * 31 30 29 27 26 25 24 23 22 21 20 12 11 10 9 5 4 0
1870 * +----+-------+---+-----+-----+---+--------+-----+------+------+
1871 * |size| 1 1 1 | V | 0 0 | opc | 0 | imm9 | idx | Rn | Rt |
1872 * +----+-------+---+-----+-----+---+--------+-----+------+------+
1874 * idx = 01 -> post-indexed, 11 pre-indexed, 00 unscaled imm. (no writeback)
1875 10 -> unprivileged
1876 * V = 0 -> non-vector
1877 * size: 00 -> 8 bit, 01 -> 16 bit, 10 -> 32 bit, 11 -> 64bit
1878 * opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
1880 static void disas_ldst_reg_imm9(DisasContext *s, uint32_t insn)
1882 int rt = extract32(insn, 0, 5);
1883 int rn = extract32(insn, 5, 5);
1884 int imm9 = sextract32(insn, 12, 9);
1885 int opc = extract32(insn, 22, 2);
1886 int size = extract32(insn, 30, 2);
1887 int idx = extract32(insn, 10, 2);
1888 bool is_signed = false;
1889 bool is_store = false;
1890 bool is_extended = false;
1891 bool is_unpriv = (idx == 2);
1892 bool is_vector = extract32(insn, 26, 1);
1893 bool post_index;
1894 bool writeback;
1896 TCGv_i64 tcg_addr;
1898 if (is_vector) {
1899 size |= (opc & 2) << 1;
1900 if (size > 4 || is_unpriv) {
1901 unallocated_encoding(s);
1902 return;
1904 is_store = ((opc & 1) == 0);
1905 } else {
1906 if (size == 3 && opc == 2) {
1907 /* PRFM - prefetch */
1908 if (is_unpriv) {
1909 unallocated_encoding(s);
1910 return;
1912 return;
1914 if (opc == 3 && size > 1) {
1915 unallocated_encoding(s);
1916 return;
1918 is_store = (opc == 0);
1919 is_signed = opc & (1<<1);
1920 is_extended = (size < 3) && (opc & 1);
1923 switch (idx) {
1924 case 0:
1925 case 2:
1926 post_index = false;
1927 writeback = false;
1928 break;
1929 case 1:
1930 post_index = true;
1931 writeback = true;
1932 break;
1933 case 3:
1934 post_index = false;
1935 writeback = true;
1936 break;
1939 if (rn == 31) {
1940 gen_check_sp_alignment(s);
1942 tcg_addr = read_cpu_reg_sp(s, rn, 1);
1944 if (!post_index) {
1945 tcg_gen_addi_i64(tcg_addr, tcg_addr, imm9);
1948 if (is_vector) {
1949 if (is_store) {
1950 do_fp_st(s, rt, tcg_addr, size);
1951 } else {
1952 do_fp_ld(s, rt, tcg_addr, size);
1954 } else {
1955 TCGv_i64 tcg_rt = cpu_reg(s, rt);
1956 int memidx = is_unpriv ? 1 : get_mem_index(s);
1958 if (is_store) {
1959 do_gpr_st_memidx(s, tcg_rt, tcg_addr, size, memidx);
1960 } else {
1961 do_gpr_ld_memidx(s, tcg_rt, tcg_addr, size,
1962 is_signed, is_extended, memidx);
1966 if (writeback) {
1967 TCGv_i64 tcg_rn = cpu_reg_sp(s, rn);
1968 if (post_index) {
1969 tcg_gen_addi_i64(tcg_addr, tcg_addr, imm9);
1971 tcg_gen_mov_i64(tcg_rn, tcg_addr);
1976 * C3.3.10 Load/store (register offset)
1978 * 31 30 29 27 26 25 24 23 22 21 20 16 15 13 12 11 10 9 5 4 0
1979 * +----+-------+---+-----+-----+---+------+-----+--+-----+----+----+
1980 * |size| 1 1 1 | V | 0 0 | opc | 1 | Rm | opt | S| 1 0 | Rn | Rt |
1981 * +----+-------+---+-----+-----+---+------+-----+--+-----+----+----+
1983 * For non-vector:
1984 * size: 00-> byte, 01 -> 16 bit, 10 -> 32bit, 11 -> 64bit
1985 * opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
1986 * For vector:
1987 * size is opc<1>:size<1:0> so 100 -> 128 bit; 110 and 111 unallocated
1988 * opc<0>: 0 -> store, 1 -> load
1989 * V: 1 -> vector/simd
1990 * opt: extend encoding (see DecodeRegExtend)
1991 * S: if S=1 then scale (essentially index by sizeof(size))
1992 * Rt: register to transfer into/out of
1993 * Rn: address register or SP for base
1994 * Rm: offset register or ZR for offset
1996 static void disas_ldst_reg_roffset(DisasContext *s, uint32_t insn)
1998 int rt = extract32(insn, 0, 5);
1999 int rn = extract32(insn, 5, 5);
2000 int shift = extract32(insn, 12, 1);
2001 int rm = extract32(insn, 16, 5);
2002 int opc = extract32(insn, 22, 2);
2003 int opt = extract32(insn, 13, 3);
2004 int size = extract32(insn, 30, 2);
2005 bool is_signed = false;
2006 bool is_store = false;
2007 bool is_extended = false;
2008 bool is_vector = extract32(insn, 26, 1);
2010 TCGv_i64 tcg_rm;
2011 TCGv_i64 tcg_addr;
2013 if (extract32(opt, 1, 1) == 0) {
2014 unallocated_encoding(s);
2015 return;
2018 if (is_vector) {
2019 size |= (opc & 2) << 1;
2020 if (size > 4) {
2021 unallocated_encoding(s);
2022 return;
2024 is_store = !extract32(opc, 0, 1);
2025 } else {
2026 if (size == 3 && opc == 2) {
2027 /* PRFM - prefetch */
2028 return;
2030 if (opc == 3 && size > 1) {
2031 unallocated_encoding(s);
2032 return;
2034 is_store = (opc == 0);
2035 is_signed = extract32(opc, 1, 1);
2036 is_extended = (size < 3) && extract32(opc, 0, 1);
2039 if (rn == 31) {
2040 gen_check_sp_alignment(s);
2042 tcg_addr = read_cpu_reg_sp(s, rn, 1);
2044 tcg_rm = read_cpu_reg(s, rm, 1);
2045 ext_and_shift_reg(tcg_rm, tcg_rm, opt, shift ? size : 0);
2047 tcg_gen_add_i64(tcg_addr, tcg_addr, tcg_rm);
2049 if (is_vector) {
2050 if (is_store) {
2051 do_fp_st(s, rt, tcg_addr, size);
2052 } else {
2053 do_fp_ld(s, rt, tcg_addr, size);
2055 } else {
2056 TCGv_i64 tcg_rt = cpu_reg(s, rt);
2057 if (is_store) {
2058 do_gpr_st(s, tcg_rt, tcg_addr, size);
2059 } else {
2060 do_gpr_ld(s, tcg_rt, tcg_addr, size, is_signed, is_extended);
2066 * C3.3.13 Load/store (unsigned immediate)
2068 * 31 30 29 27 26 25 24 23 22 21 10 9 5
2069 * +----+-------+---+-----+-----+------------+-------+------+
2070 * |size| 1 1 1 | V | 0 1 | opc | imm12 | Rn | Rt |
2071 * +----+-------+---+-----+-----+------------+-------+------+
2073 * For non-vector:
2074 * size: 00-> byte, 01 -> 16 bit, 10 -> 32bit, 11 -> 64bit
2075 * opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
2076 * For vector:
2077 * size is opc<1>:size<1:0> so 100 -> 128 bit; 110 and 111 unallocated
2078 * opc<0>: 0 -> store, 1 -> load
2079 * Rn: base address register (inc SP)
2080 * Rt: target register
2082 static void disas_ldst_reg_unsigned_imm(DisasContext *s, uint32_t insn)
2084 int rt = extract32(insn, 0, 5);
2085 int rn = extract32(insn, 5, 5);
2086 unsigned int imm12 = extract32(insn, 10, 12);
2087 bool is_vector = extract32(insn, 26, 1);
2088 int size = extract32(insn, 30, 2);
2089 int opc = extract32(insn, 22, 2);
2090 unsigned int offset;
2092 TCGv_i64 tcg_addr;
2094 bool is_store;
2095 bool is_signed = false;
2096 bool is_extended = false;
2098 if (is_vector) {
2099 size |= (opc & 2) << 1;
2100 if (size > 4) {
2101 unallocated_encoding(s);
2102 return;
2104 is_store = !extract32(opc, 0, 1);
2105 } else {
2106 if (size == 3 && opc == 2) {
2107 /* PRFM - prefetch */
2108 return;
2110 if (opc == 3 && size > 1) {
2111 unallocated_encoding(s);
2112 return;
2114 is_store = (opc == 0);
2115 is_signed = extract32(opc, 1, 1);
2116 is_extended = (size < 3) && extract32(opc, 0, 1);
2119 if (rn == 31) {
2120 gen_check_sp_alignment(s);
2122 tcg_addr = read_cpu_reg_sp(s, rn, 1);
2123 offset = imm12 << size;
2124 tcg_gen_addi_i64(tcg_addr, tcg_addr, offset);
2126 if (is_vector) {
2127 if (is_store) {
2128 do_fp_st(s, rt, tcg_addr, size);
2129 } else {
2130 do_fp_ld(s, rt, tcg_addr, size);
2132 } else {
2133 TCGv_i64 tcg_rt = cpu_reg(s, rt);
2134 if (is_store) {
2135 do_gpr_st(s, tcg_rt, tcg_addr, size);
2136 } else {
2137 do_gpr_ld(s, tcg_rt, tcg_addr, size, is_signed, is_extended);
2142 /* Load/store register (all forms) */
2143 static void disas_ldst_reg(DisasContext *s, uint32_t insn)
2145 switch (extract32(insn, 24, 2)) {
2146 case 0:
2147 if (extract32(insn, 21, 1) == 1 && extract32(insn, 10, 2) == 2) {
2148 disas_ldst_reg_roffset(s, insn);
2149 } else {
2150 /* Load/store register (unscaled immediate)
2151 * Load/store immediate pre/post-indexed
2152 * Load/store register unprivileged
2154 disas_ldst_reg_imm9(s, insn);
2156 break;
2157 case 1:
2158 disas_ldst_reg_unsigned_imm(s, insn);
2159 break;
2160 default:
2161 unallocated_encoding(s);
2162 break;
2166 /* C3.3.1 AdvSIMD load/store multiple structures
2168 * 31 30 29 23 22 21 16 15 12 11 10 9 5 4 0
2169 * +---+---+---------------+---+-------------+--------+------+------+------+
2170 * | 0 | Q | 0 0 1 1 0 0 0 | L | 0 0 0 0 0 0 | opcode | size | Rn | Rt |
2171 * +---+---+---------------+---+-------------+--------+------+------+------+
2173 * C3.3.2 AdvSIMD load/store multiple structures (post-indexed)
2175 * 31 30 29 23 22 21 20 16 15 12 11 10 9 5 4 0
2176 * +---+---+---------------+---+---+---------+--------+------+------+------+
2177 * | 0 | Q | 0 0 1 1 0 0 1 | L | 0 | Rm | opcode | size | Rn | Rt |
2178 * +---+---+---------------+---+---+---------+--------+------+------+------+
2180 * Rt: first (or only) SIMD&FP register to be transferred
2181 * Rn: base address or SP
2182 * Rm (post-index only): post-index register (when !31) or size dependent #imm
2184 static void disas_ldst_multiple_struct(DisasContext *s, uint32_t insn)
2186 int rt = extract32(insn, 0, 5);
2187 int rn = extract32(insn, 5, 5);
2188 int size = extract32(insn, 10, 2);
2189 int opcode = extract32(insn, 12, 4);
2190 bool is_store = !extract32(insn, 22, 1);
2191 bool is_postidx = extract32(insn, 23, 1);
2192 bool is_q = extract32(insn, 30, 1);
2193 TCGv_i64 tcg_addr, tcg_rn;
2195 int ebytes = 1 << size;
2196 int elements = (is_q ? 128 : 64) / (8 << size);
2197 int rpt; /* num iterations */
2198 int selem; /* structure elements */
2199 int r;
2201 if (extract32(insn, 31, 1) || extract32(insn, 21, 1)) {
2202 unallocated_encoding(s);
2203 return;
2206 /* From the shared decode logic */
2207 switch (opcode) {
2208 case 0x0:
2209 rpt = 1;
2210 selem = 4;
2211 break;
2212 case 0x2:
2213 rpt = 4;
2214 selem = 1;
2215 break;
2216 case 0x4:
2217 rpt = 1;
2218 selem = 3;
2219 break;
2220 case 0x6:
2221 rpt = 3;
2222 selem = 1;
2223 break;
2224 case 0x7:
2225 rpt = 1;
2226 selem = 1;
2227 break;
2228 case 0x8:
2229 rpt = 1;
2230 selem = 2;
2231 break;
2232 case 0xa:
2233 rpt = 2;
2234 selem = 1;
2235 break;
2236 default:
2237 unallocated_encoding(s);
2238 return;
2241 if (size == 3 && !is_q && selem != 1) {
2242 /* reserved */
2243 unallocated_encoding(s);
2244 return;
2247 if (rn == 31) {
2248 gen_check_sp_alignment(s);
2251 tcg_rn = cpu_reg_sp(s, rn);
2252 tcg_addr = tcg_temp_new_i64();
2253 tcg_gen_mov_i64(tcg_addr, tcg_rn);
2255 for (r = 0; r < rpt; r++) {
2256 int e;
2257 for (e = 0; e < elements; e++) {
2258 int tt = (rt + r) % 32;
2259 int xs;
2260 for (xs = 0; xs < selem; xs++) {
2261 if (is_store) {
2262 do_vec_st(s, tt, e, tcg_addr, size);
2263 } else {
2264 do_vec_ld(s, tt, e, tcg_addr, size);
2266 /* For non-quad operations, setting a slice of the low
2267 * 64 bits of the register clears the high 64 bits (in
2268 * the ARM ARM pseudocode this is implicit in the fact
2269 * that 'rval' is a 64 bit wide variable). We optimize
2270 * by noticing that we only need to do this the first
2271 * time we touch a register.
2273 if (!is_q && e == 0 && (r == 0 || xs == selem - 1)) {
2274 clear_vec_high(s, tt);
2277 tcg_gen_addi_i64(tcg_addr, tcg_addr, ebytes);
2278 tt = (tt + 1) % 32;
2283 if (is_postidx) {
2284 int rm = extract32(insn, 16, 5);
2285 if (rm == 31) {
2286 tcg_gen_mov_i64(tcg_rn, tcg_addr);
2287 } else {
2288 tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, rm));
2291 tcg_temp_free_i64(tcg_addr);
2294 /* C3.3.3 AdvSIMD load/store single structure
2296 * 31 30 29 23 22 21 20 16 15 13 12 11 10 9 5 4 0
2297 * +---+---+---------------+-----+-----------+-----+---+------+------+------+
2298 * | 0 | Q | 0 0 1 1 0 1 0 | L R | 0 0 0 0 0 | opc | S | size | Rn | Rt |
2299 * +---+---+---------------+-----+-----------+-----+---+------+------+------+
2301 * C3.3.4 AdvSIMD load/store single structure (post-indexed)
2303 * 31 30 29 23 22 21 20 16 15 13 12 11 10 9 5 4 0
2304 * +---+---+---------------+-----+-----------+-----+---+------+------+------+
2305 * | 0 | Q | 0 0 1 1 0 1 1 | L R | Rm | opc | S | size | Rn | Rt |
2306 * +---+---+---------------+-----+-----------+-----+---+------+------+------+
2308 * Rt: first (or only) SIMD&FP register to be transferred
2309 * Rn: base address or SP
2310 * Rm (post-index only): post-index register (when !31) or size dependent #imm
2311 * index = encoded in Q:S:size dependent on size
2313 * lane_size = encoded in R, opc
2314 * transfer width = encoded in opc, S, size
2316 static void disas_ldst_single_struct(DisasContext *s, uint32_t insn)
2318 int rt = extract32(insn, 0, 5);
2319 int rn = extract32(insn, 5, 5);
2320 int size = extract32(insn, 10, 2);
2321 int S = extract32(insn, 12, 1);
2322 int opc = extract32(insn, 13, 3);
2323 int R = extract32(insn, 21, 1);
2324 int is_load = extract32(insn, 22, 1);
2325 int is_postidx = extract32(insn, 23, 1);
2326 int is_q = extract32(insn, 30, 1);
2328 int scale = extract32(opc, 1, 2);
2329 int selem = (extract32(opc, 0, 1) << 1 | R) + 1;
2330 bool replicate = false;
2331 int index = is_q << 3 | S << 2 | size;
2332 int ebytes, xs;
2333 TCGv_i64 tcg_addr, tcg_rn;
2335 switch (scale) {
2336 case 3:
2337 if (!is_load || S) {
2338 unallocated_encoding(s);
2339 return;
2341 scale = size;
2342 replicate = true;
2343 break;
2344 case 0:
2345 break;
2346 case 1:
2347 if (extract32(size, 0, 1)) {
2348 unallocated_encoding(s);
2349 return;
2351 index >>= 1;
2352 break;
2353 case 2:
2354 if (extract32(size, 1, 1)) {
2355 unallocated_encoding(s);
2356 return;
2358 if (!extract32(size, 0, 1)) {
2359 index >>= 2;
2360 } else {
2361 if (S) {
2362 unallocated_encoding(s);
2363 return;
2365 index >>= 3;
2366 scale = 3;
2368 break;
2369 default:
2370 g_assert_not_reached();
2373 ebytes = 1 << scale;
2375 if (rn == 31) {
2376 gen_check_sp_alignment(s);
2379 tcg_rn = cpu_reg_sp(s, rn);
2380 tcg_addr = tcg_temp_new_i64();
2381 tcg_gen_mov_i64(tcg_addr, tcg_rn);
2383 for (xs = 0; xs < selem; xs++) {
2384 if (replicate) {
2385 /* Load and replicate to all elements */
2386 uint64_t mulconst;
2387 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
2389 tcg_gen_qemu_ld_i64(tcg_tmp, tcg_addr,
2390 get_mem_index(s), MO_TE + scale);
2391 switch (scale) {
2392 case 0:
2393 mulconst = 0x0101010101010101ULL;
2394 break;
2395 case 1:
2396 mulconst = 0x0001000100010001ULL;
2397 break;
2398 case 2:
2399 mulconst = 0x0000000100000001ULL;
2400 break;
2401 case 3:
2402 mulconst = 0;
2403 break;
2404 default:
2405 g_assert_not_reached();
2407 if (mulconst) {
2408 tcg_gen_muli_i64(tcg_tmp, tcg_tmp, mulconst);
2410 write_vec_element(s, tcg_tmp, rt, 0, MO_64);
2411 if (is_q) {
2412 write_vec_element(s, tcg_tmp, rt, 1, MO_64);
2413 } else {
2414 clear_vec_high(s, rt);
2416 tcg_temp_free_i64(tcg_tmp);
2417 } else {
2418 /* Load/store one element per register */
2419 if (is_load) {
2420 do_vec_ld(s, rt, index, tcg_addr, MO_TE + scale);
2421 } else {
2422 do_vec_st(s, rt, index, tcg_addr, MO_TE + scale);
2425 tcg_gen_addi_i64(tcg_addr, tcg_addr, ebytes);
2426 rt = (rt + 1) % 32;
2429 if (is_postidx) {
2430 int rm = extract32(insn, 16, 5);
2431 if (rm == 31) {
2432 tcg_gen_mov_i64(tcg_rn, tcg_addr);
2433 } else {
2434 tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, rm));
2437 tcg_temp_free_i64(tcg_addr);
2440 /* C3.3 Loads and stores */
2441 static void disas_ldst(DisasContext *s, uint32_t insn)
2443 switch (extract32(insn, 24, 6)) {
2444 case 0x08: /* Load/store exclusive */
2445 disas_ldst_excl(s, insn);
2446 break;
2447 case 0x18: case 0x1c: /* Load register (literal) */
2448 disas_ld_lit(s, insn);
2449 break;
2450 case 0x28: case 0x29:
2451 case 0x2c: case 0x2d: /* Load/store pair (all forms) */
2452 disas_ldst_pair(s, insn);
2453 break;
2454 case 0x38: case 0x39:
2455 case 0x3c: case 0x3d: /* Load/store register (all forms) */
2456 disas_ldst_reg(s, insn);
2457 break;
2458 case 0x0c: /* AdvSIMD load/store multiple structures */
2459 disas_ldst_multiple_struct(s, insn);
2460 break;
2461 case 0x0d: /* AdvSIMD load/store single structure */
2462 disas_ldst_single_struct(s, insn);
2463 break;
2464 default:
2465 unallocated_encoding(s);
2466 break;
2470 /* C3.4.6 PC-rel. addressing
2471 * 31 30 29 28 24 23 5 4 0
2472 * +----+-------+-----------+-------------------+------+
2473 * | op | immlo | 1 0 0 0 0 | immhi | Rd |
2474 * +----+-------+-----------+-------------------+------+
2476 static void disas_pc_rel_adr(DisasContext *s, uint32_t insn)
2478 unsigned int page, rd;
2479 uint64_t base;
2480 int64_t offset;
2482 page = extract32(insn, 31, 1);
2483 /* SignExtend(immhi:immlo) -> offset */
2484 offset = ((int64_t)sextract32(insn, 5, 19) << 2) | extract32(insn, 29, 2);
2485 rd = extract32(insn, 0, 5);
2486 base = s->pc - 4;
2488 if (page) {
2489 /* ADRP (page based) */
2490 base &= ~0xfff;
2491 offset <<= 12;
2494 tcg_gen_movi_i64(cpu_reg(s, rd), base + offset);
2498 * C3.4.1 Add/subtract (immediate)
2500 * 31 30 29 28 24 23 22 21 10 9 5 4 0
2501 * +--+--+--+-----------+-----+-------------+-----+-----+
2502 * |sf|op| S| 1 0 0 0 1 |shift| imm12 | Rn | Rd |
2503 * +--+--+--+-----------+-----+-------------+-----+-----+
2505 * sf: 0 -> 32bit, 1 -> 64bit
2506 * op: 0 -> add , 1 -> sub
2507 * S: 1 -> set flags
2508 * shift: 00 -> LSL imm by 0, 01 -> LSL imm by 12
2510 static void disas_add_sub_imm(DisasContext *s, uint32_t insn)
2512 int rd = extract32(insn, 0, 5);
2513 int rn = extract32(insn, 5, 5);
2514 uint64_t imm = extract32(insn, 10, 12);
2515 int shift = extract32(insn, 22, 2);
2516 bool setflags = extract32(insn, 29, 1);
2517 bool sub_op = extract32(insn, 30, 1);
2518 bool is_64bit = extract32(insn, 31, 1);
2520 TCGv_i64 tcg_rn = cpu_reg_sp(s, rn);
2521 TCGv_i64 tcg_rd = setflags ? cpu_reg(s, rd) : cpu_reg_sp(s, rd);
2522 TCGv_i64 tcg_result;
2524 switch (shift) {
2525 case 0x0:
2526 break;
2527 case 0x1:
2528 imm <<= 12;
2529 break;
2530 default:
2531 unallocated_encoding(s);
2532 return;
2535 tcg_result = tcg_temp_new_i64();
2536 if (!setflags) {
2537 if (sub_op) {
2538 tcg_gen_subi_i64(tcg_result, tcg_rn, imm);
2539 } else {
2540 tcg_gen_addi_i64(tcg_result, tcg_rn, imm);
2542 } else {
2543 TCGv_i64 tcg_imm = tcg_const_i64(imm);
2544 if (sub_op) {
2545 gen_sub_CC(is_64bit, tcg_result, tcg_rn, tcg_imm);
2546 } else {
2547 gen_add_CC(is_64bit, tcg_result, tcg_rn, tcg_imm);
2549 tcg_temp_free_i64(tcg_imm);
2552 if (is_64bit) {
2553 tcg_gen_mov_i64(tcg_rd, tcg_result);
2554 } else {
2555 tcg_gen_ext32u_i64(tcg_rd, tcg_result);
2558 tcg_temp_free_i64(tcg_result);
2561 /* The input should be a value in the bottom e bits (with higher
2562 * bits zero); returns that value replicated into every element
2563 * of size e in a 64 bit integer.
2565 static uint64_t bitfield_replicate(uint64_t mask, unsigned int e)
2567 assert(e != 0);
2568 while (e < 64) {
2569 mask |= mask << e;
2570 e *= 2;
2572 return mask;
2575 /* Return a value with the bottom len bits set (where 0 < len <= 64) */
2576 static inline uint64_t bitmask64(unsigned int length)
2578 assert(length > 0 && length <= 64);
2579 return ~0ULL >> (64 - length);
2582 /* Simplified variant of pseudocode DecodeBitMasks() for the case where we
2583 * only require the wmask. Returns false if the imms/immr/immn are a reserved
2584 * value (ie should cause a guest UNDEF exception), and true if they are
2585 * valid, in which case the decoded bit pattern is written to result.
2587 static bool logic_imm_decode_wmask(uint64_t *result, unsigned int immn,
2588 unsigned int imms, unsigned int immr)
2590 uint64_t mask;
2591 unsigned e, levels, s, r;
2592 int len;
2594 assert(immn < 2 && imms < 64 && immr < 64);
2596 /* The bit patterns we create here are 64 bit patterns which
2597 * are vectors of identical elements of size e = 2, 4, 8, 16, 32 or
2598 * 64 bits each. Each element contains the same value: a run
2599 * of between 1 and e-1 non-zero bits, rotated within the
2600 * element by between 0 and e-1 bits.
2602 * The element size and run length are encoded into immn (1 bit)
2603 * and imms (6 bits) as follows:
2604 * 64 bit elements: immn = 1, imms = <length of run - 1>
2605 * 32 bit elements: immn = 0, imms = 0 : <length of run - 1>
2606 * 16 bit elements: immn = 0, imms = 10 : <length of run - 1>
2607 * 8 bit elements: immn = 0, imms = 110 : <length of run - 1>
2608 * 4 bit elements: immn = 0, imms = 1110 : <length of run - 1>
2609 * 2 bit elements: immn = 0, imms = 11110 : <length of run - 1>
2610 * Notice that immn = 0, imms = 11111x is the only combination
2611 * not covered by one of the above options; this is reserved.
2612 * Further, <length of run - 1> all-ones is a reserved pattern.
2614 * In all cases the rotation is by immr % e (and immr is 6 bits).
2617 /* First determine the element size */
2618 len = 31 - clz32((immn << 6) | (~imms & 0x3f));
2619 if (len < 1) {
2620 /* This is the immn == 0, imms == 0x11111x case */
2621 return false;
2623 e = 1 << len;
2625 levels = e - 1;
2626 s = imms & levels;
2627 r = immr & levels;
2629 if (s == levels) {
2630 /* <length of run - 1> mustn't be all-ones. */
2631 return false;
2634 /* Create the value of one element: s+1 set bits rotated
2635 * by r within the element (which is e bits wide)...
2637 mask = bitmask64(s + 1);
2638 mask = (mask >> r) | (mask << (e - r));
2639 /* ...then replicate the element over the whole 64 bit value */
2640 mask = bitfield_replicate(mask, e);
2641 *result = mask;
2642 return true;
2645 /* C3.4.4 Logical (immediate)
2646 * 31 30 29 28 23 22 21 16 15 10 9 5 4 0
2647 * +----+-----+-------------+---+------+------+------+------+
2648 * | sf | opc | 1 0 0 1 0 0 | N | immr | imms | Rn | Rd |
2649 * +----+-----+-------------+---+------+------+------+------+
2651 static void disas_logic_imm(DisasContext *s, uint32_t insn)
2653 unsigned int sf, opc, is_n, immr, imms, rn, rd;
2654 TCGv_i64 tcg_rd, tcg_rn;
2655 uint64_t wmask;
2656 bool is_and = false;
2658 sf = extract32(insn, 31, 1);
2659 opc = extract32(insn, 29, 2);
2660 is_n = extract32(insn, 22, 1);
2661 immr = extract32(insn, 16, 6);
2662 imms = extract32(insn, 10, 6);
2663 rn = extract32(insn, 5, 5);
2664 rd = extract32(insn, 0, 5);
2666 if (!sf && is_n) {
2667 unallocated_encoding(s);
2668 return;
2671 if (opc == 0x3) { /* ANDS */
2672 tcg_rd = cpu_reg(s, rd);
2673 } else {
2674 tcg_rd = cpu_reg_sp(s, rd);
2676 tcg_rn = cpu_reg(s, rn);
2678 if (!logic_imm_decode_wmask(&wmask, is_n, imms, immr)) {
2679 /* some immediate field values are reserved */
2680 unallocated_encoding(s);
2681 return;
2684 if (!sf) {
2685 wmask &= 0xffffffff;
2688 switch (opc) {
2689 case 0x3: /* ANDS */
2690 case 0x0: /* AND */
2691 tcg_gen_andi_i64(tcg_rd, tcg_rn, wmask);
2692 is_and = true;
2693 break;
2694 case 0x1: /* ORR */
2695 tcg_gen_ori_i64(tcg_rd, tcg_rn, wmask);
2696 break;
2697 case 0x2: /* EOR */
2698 tcg_gen_xori_i64(tcg_rd, tcg_rn, wmask);
2699 break;
2700 default:
2701 assert(FALSE); /* must handle all above */
2702 break;
2705 if (!sf && !is_and) {
2706 /* zero extend final result; we know we can skip this for AND
2707 * since the immediate had the high 32 bits clear.
2709 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
2712 if (opc == 3) { /* ANDS */
2713 gen_logic_CC(sf, tcg_rd);
2718 * C3.4.5 Move wide (immediate)
2720 * 31 30 29 28 23 22 21 20 5 4 0
2721 * +--+-----+-------------+-----+----------------+------+
2722 * |sf| opc | 1 0 0 1 0 1 | hw | imm16 | Rd |
2723 * +--+-----+-------------+-----+----------------+------+
2725 * sf: 0 -> 32 bit, 1 -> 64 bit
2726 * opc: 00 -> N, 10 -> Z, 11 -> K
2727 * hw: shift/16 (0,16, and sf only 32, 48)
2729 static void disas_movw_imm(DisasContext *s, uint32_t insn)
2731 int rd = extract32(insn, 0, 5);
2732 uint64_t imm = extract32(insn, 5, 16);
2733 int sf = extract32(insn, 31, 1);
2734 int opc = extract32(insn, 29, 2);
2735 int pos = extract32(insn, 21, 2) << 4;
2736 TCGv_i64 tcg_rd = cpu_reg(s, rd);
2737 TCGv_i64 tcg_imm;
2739 if (!sf && (pos >= 32)) {
2740 unallocated_encoding(s);
2741 return;
2744 switch (opc) {
2745 case 0: /* MOVN */
2746 case 2: /* MOVZ */
2747 imm <<= pos;
2748 if (opc == 0) {
2749 imm = ~imm;
2751 if (!sf) {
2752 imm &= 0xffffffffu;
2754 tcg_gen_movi_i64(tcg_rd, imm);
2755 break;
2756 case 3: /* MOVK */
2757 tcg_imm = tcg_const_i64(imm);
2758 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_imm, pos, 16);
2759 tcg_temp_free_i64(tcg_imm);
2760 if (!sf) {
2761 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
2763 break;
2764 default:
2765 unallocated_encoding(s);
2766 break;
2770 /* C3.4.2 Bitfield
2771 * 31 30 29 28 23 22 21 16 15 10 9 5 4 0
2772 * +----+-----+-------------+---+------+------+------+------+
2773 * | sf | opc | 1 0 0 1 1 0 | N | immr | imms | Rn | Rd |
2774 * +----+-----+-------------+---+------+------+------+------+
2776 static void disas_bitfield(DisasContext *s, uint32_t insn)
2778 unsigned int sf, n, opc, ri, si, rn, rd, bitsize, pos, len;
2779 TCGv_i64 tcg_rd, tcg_tmp;
2781 sf = extract32(insn, 31, 1);
2782 opc = extract32(insn, 29, 2);
2783 n = extract32(insn, 22, 1);
2784 ri = extract32(insn, 16, 6);
2785 si = extract32(insn, 10, 6);
2786 rn = extract32(insn, 5, 5);
2787 rd = extract32(insn, 0, 5);
2788 bitsize = sf ? 64 : 32;
2790 if (sf != n || ri >= bitsize || si >= bitsize || opc > 2) {
2791 unallocated_encoding(s);
2792 return;
2795 tcg_rd = cpu_reg(s, rd);
2796 tcg_tmp = read_cpu_reg(s, rn, sf);
2798 /* OPTME: probably worth recognizing common cases of ext{8,16,32}{u,s} */
2800 if (opc != 1) { /* SBFM or UBFM */
2801 tcg_gen_movi_i64(tcg_rd, 0);
2804 /* do the bit move operation */
2805 if (si >= ri) {
2806 /* Wd<s-r:0> = Wn<s:r> */
2807 tcg_gen_shri_i64(tcg_tmp, tcg_tmp, ri);
2808 pos = 0;
2809 len = (si - ri) + 1;
2810 } else {
2811 /* Wd<32+s-r,32-r> = Wn<s:0> */
2812 pos = bitsize - ri;
2813 len = si + 1;
2816 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, pos, len);
2818 if (opc == 0) { /* SBFM - sign extend the destination field */
2819 tcg_gen_shli_i64(tcg_rd, tcg_rd, 64 - (pos + len));
2820 tcg_gen_sari_i64(tcg_rd, tcg_rd, 64 - (pos + len));
2823 if (!sf) { /* zero extend final result */
2824 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
2828 /* C3.4.3 Extract
2829 * 31 30 29 28 23 22 21 20 16 15 10 9 5 4 0
2830 * +----+------+-------------+---+----+------+--------+------+------+
2831 * | sf | op21 | 1 0 0 1 1 1 | N | o0 | Rm | imms | Rn | Rd |
2832 * +----+------+-------------+---+----+------+--------+------+------+
2834 static void disas_extract(DisasContext *s, uint32_t insn)
2836 unsigned int sf, n, rm, imm, rn, rd, bitsize, op21, op0;
2838 sf = extract32(insn, 31, 1);
2839 n = extract32(insn, 22, 1);
2840 rm = extract32(insn, 16, 5);
2841 imm = extract32(insn, 10, 6);
2842 rn = extract32(insn, 5, 5);
2843 rd = extract32(insn, 0, 5);
2844 op21 = extract32(insn, 29, 2);
2845 op0 = extract32(insn, 21, 1);
2846 bitsize = sf ? 64 : 32;
2848 if (sf != n || op21 || op0 || imm >= bitsize) {
2849 unallocated_encoding(s);
2850 } else {
2851 TCGv_i64 tcg_rd, tcg_rm, tcg_rn;
2853 tcg_rd = cpu_reg(s, rd);
2855 if (imm) {
2856 /* OPTME: we can special case rm==rn as a rotate */
2857 tcg_rm = read_cpu_reg(s, rm, sf);
2858 tcg_rn = read_cpu_reg(s, rn, sf);
2859 tcg_gen_shri_i64(tcg_rm, tcg_rm, imm);
2860 tcg_gen_shli_i64(tcg_rn, tcg_rn, bitsize - imm);
2861 tcg_gen_or_i64(tcg_rd, tcg_rm, tcg_rn);
2862 if (!sf) {
2863 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
2865 } else {
2866 /* tcg shl_i32/shl_i64 is undefined for 32/64 bit shifts,
2867 * so an extract from bit 0 is a special case.
2869 if (sf) {
2870 tcg_gen_mov_i64(tcg_rd, cpu_reg(s, rm));
2871 } else {
2872 tcg_gen_ext32u_i64(tcg_rd, cpu_reg(s, rm));
2879 /* C3.4 Data processing - immediate */
2880 static void disas_data_proc_imm(DisasContext *s, uint32_t insn)
2882 switch (extract32(insn, 23, 6)) {
2883 case 0x20: case 0x21: /* PC-rel. addressing */
2884 disas_pc_rel_adr(s, insn);
2885 break;
2886 case 0x22: case 0x23: /* Add/subtract (immediate) */
2887 disas_add_sub_imm(s, insn);
2888 break;
2889 case 0x24: /* Logical (immediate) */
2890 disas_logic_imm(s, insn);
2891 break;
2892 case 0x25: /* Move wide (immediate) */
2893 disas_movw_imm(s, insn);
2894 break;
2895 case 0x26: /* Bitfield */
2896 disas_bitfield(s, insn);
2897 break;
2898 case 0x27: /* Extract */
2899 disas_extract(s, insn);
2900 break;
2901 default:
2902 unallocated_encoding(s);
2903 break;
2907 /* Shift a TCGv src by TCGv shift_amount, put result in dst.
2908 * Note that it is the caller's responsibility to ensure that the
2909 * shift amount is in range (ie 0..31 or 0..63) and provide the ARM
2910 * mandated semantics for out of range shifts.
2912 static void shift_reg(TCGv_i64 dst, TCGv_i64 src, int sf,
2913 enum a64_shift_type shift_type, TCGv_i64 shift_amount)
2915 switch (shift_type) {
2916 case A64_SHIFT_TYPE_LSL:
2917 tcg_gen_shl_i64(dst, src, shift_amount);
2918 break;
2919 case A64_SHIFT_TYPE_LSR:
2920 tcg_gen_shr_i64(dst, src, shift_amount);
2921 break;
2922 case A64_SHIFT_TYPE_ASR:
2923 if (!sf) {
2924 tcg_gen_ext32s_i64(dst, src);
2926 tcg_gen_sar_i64(dst, sf ? src : dst, shift_amount);
2927 break;
2928 case A64_SHIFT_TYPE_ROR:
2929 if (sf) {
2930 tcg_gen_rotr_i64(dst, src, shift_amount);
2931 } else {
2932 TCGv_i32 t0, t1;
2933 t0 = tcg_temp_new_i32();
2934 t1 = tcg_temp_new_i32();
2935 tcg_gen_trunc_i64_i32(t0, src);
2936 tcg_gen_trunc_i64_i32(t1, shift_amount);
2937 tcg_gen_rotr_i32(t0, t0, t1);
2938 tcg_gen_extu_i32_i64(dst, t0);
2939 tcg_temp_free_i32(t0);
2940 tcg_temp_free_i32(t1);
2942 break;
2943 default:
2944 assert(FALSE); /* all shift types should be handled */
2945 break;
2948 if (!sf) { /* zero extend final result */
2949 tcg_gen_ext32u_i64(dst, dst);
2953 /* Shift a TCGv src by immediate, put result in dst.
2954 * The shift amount must be in range (this should always be true as the
2955 * relevant instructions will UNDEF on bad shift immediates).
2957 static void shift_reg_imm(TCGv_i64 dst, TCGv_i64 src, int sf,
2958 enum a64_shift_type shift_type, unsigned int shift_i)
2960 assert(shift_i < (sf ? 64 : 32));
2962 if (shift_i == 0) {
2963 tcg_gen_mov_i64(dst, src);
2964 } else {
2965 TCGv_i64 shift_const;
2967 shift_const = tcg_const_i64(shift_i);
2968 shift_reg(dst, src, sf, shift_type, shift_const);
2969 tcg_temp_free_i64(shift_const);
2973 /* C3.5.10 Logical (shifted register)
2974 * 31 30 29 28 24 23 22 21 20 16 15 10 9 5 4 0
2975 * +----+-----+-----------+-------+---+------+--------+------+------+
2976 * | sf | opc | 0 1 0 1 0 | shift | N | Rm | imm6 | Rn | Rd |
2977 * +----+-----+-----------+-------+---+------+--------+------+------+
2979 static void disas_logic_reg(DisasContext *s, uint32_t insn)
2981 TCGv_i64 tcg_rd, tcg_rn, tcg_rm;
2982 unsigned int sf, opc, shift_type, invert, rm, shift_amount, rn, rd;
2984 sf = extract32(insn, 31, 1);
2985 opc = extract32(insn, 29, 2);
2986 shift_type = extract32(insn, 22, 2);
2987 invert = extract32(insn, 21, 1);
2988 rm = extract32(insn, 16, 5);
2989 shift_amount = extract32(insn, 10, 6);
2990 rn = extract32(insn, 5, 5);
2991 rd = extract32(insn, 0, 5);
2993 if (!sf && (shift_amount & (1 << 5))) {
2994 unallocated_encoding(s);
2995 return;
2998 tcg_rd = cpu_reg(s, rd);
3000 if (opc == 1 && shift_amount == 0 && shift_type == 0 && rn == 31) {
3001 /* Unshifted ORR and ORN with WZR/XZR is the standard encoding for
3002 * register-register MOV and MVN, so it is worth special casing.
3004 tcg_rm = cpu_reg(s, rm);
3005 if (invert) {
3006 tcg_gen_not_i64(tcg_rd, tcg_rm);
3007 if (!sf) {
3008 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3010 } else {
3011 if (sf) {
3012 tcg_gen_mov_i64(tcg_rd, tcg_rm);
3013 } else {
3014 tcg_gen_ext32u_i64(tcg_rd, tcg_rm);
3017 return;
3020 tcg_rm = read_cpu_reg(s, rm, sf);
3022 if (shift_amount) {
3023 shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, shift_amount);
3026 tcg_rn = cpu_reg(s, rn);
3028 switch (opc | (invert << 2)) {
3029 case 0: /* AND */
3030 case 3: /* ANDS */
3031 tcg_gen_and_i64(tcg_rd, tcg_rn, tcg_rm);
3032 break;
3033 case 1: /* ORR */
3034 tcg_gen_or_i64(tcg_rd, tcg_rn, tcg_rm);
3035 break;
3036 case 2: /* EOR */
3037 tcg_gen_xor_i64(tcg_rd, tcg_rn, tcg_rm);
3038 break;
3039 case 4: /* BIC */
3040 case 7: /* BICS */
3041 tcg_gen_andc_i64(tcg_rd, tcg_rn, tcg_rm);
3042 break;
3043 case 5: /* ORN */
3044 tcg_gen_orc_i64(tcg_rd, tcg_rn, tcg_rm);
3045 break;
3046 case 6: /* EON */
3047 tcg_gen_eqv_i64(tcg_rd, tcg_rn, tcg_rm);
3048 break;
3049 default:
3050 assert(FALSE);
3051 break;
3054 if (!sf) {
3055 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3058 if (opc == 3) {
3059 gen_logic_CC(sf, tcg_rd);
3064 * C3.5.1 Add/subtract (extended register)
3066 * 31|30|29|28 24|23 22|21|20 16|15 13|12 10|9 5|4 0|
3067 * +--+--+--+-----------+-----+--+-------+------+------+----+----+
3068 * |sf|op| S| 0 1 0 1 1 | opt | 1| Rm |option| imm3 | Rn | Rd |
3069 * +--+--+--+-----------+-----+--+-------+------+------+----+----+
3071 * sf: 0 -> 32bit, 1 -> 64bit
3072 * op: 0 -> add , 1 -> sub
3073 * S: 1 -> set flags
3074 * opt: 00
3075 * option: extension type (see DecodeRegExtend)
3076 * imm3: optional shift to Rm
3078 * Rd = Rn + LSL(extend(Rm), amount)
3080 static void disas_add_sub_ext_reg(DisasContext *s, uint32_t insn)
3082 int rd = extract32(insn, 0, 5);
3083 int rn = extract32(insn, 5, 5);
3084 int imm3 = extract32(insn, 10, 3);
3085 int option = extract32(insn, 13, 3);
3086 int rm = extract32(insn, 16, 5);
3087 bool setflags = extract32(insn, 29, 1);
3088 bool sub_op = extract32(insn, 30, 1);
3089 bool sf = extract32(insn, 31, 1);
3091 TCGv_i64 tcg_rm, tcg_rn; /* temps */
3092 TCGv_i64 tcg_rd;
3093 TCGv_i64 tcg_result;
3095 if (imm3 > 4) {
3096 unallocated_encoding(s);
3097 return;
3100 /* non-flag setting ops may use SP */
3101 if (!setflags) {
3102 tcg_rd = cpu_reg_sp(s, rd);
3103 } else {
3104 tcg_rd = cpu_reg(s, rd);
3106 tcg_rn = read_cpu_reg_sp(s, rn, sf);
3108 tcg_rm = read_cpu_reg(s, rm, sf);
3109 ext_and_shift_reg(tcg_rm, tcg_rm, option, imm3);
3111 tcg_result = tcg_temp_new_i64();
3113 if (!setflags) {
3114 if (sub_op) {
3115 tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
3116 } else {
3117 tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
3119 } else {
3120 if (sub_op) {
3121 gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
3122 } else {
3123 gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
3127 if (sf) {
3128 tcg_gen_mov_i64(tcg_rd, tcg_result);
3129 } else {
3130 tcg_gen_ext32u_i64(tcg_rd, tcg_result);
3133 tcg_temp_free_i64(tcg_result);
3137 * C3.5.2 Add/subtract (shifted register)
3139 * 31 30 29 28 24 23 22 21 20 16 15 10 9 5 4 0
3140 * +--+--+--+-----------+-----+--+-------+---------+------+------+
3141 * |sf|op| S| 0 1 0 1 1 |shift| 0| Rm | imm6 | Rn | Rd |
3142 * +--+--+--+-----------+-----+--+-------+---------+------+------+
3144 * sf: 0 -> 32bit, 1 -> 64bit
3145 * op: 0 -> add , 1 -> sub
3146 * S: 1 -> set flags
3147 * shift: 00 -> LSL, 01 -> LSR, 10 -> ASR, 11 -> RESERVED
3148 * imm6: Shift amount to apply to Rm before the add/sub
3150 static void disas_add_sub_reg(DisasContext *s, uint32_t insn)
3152 int rd = extract32(insn, 0, 5);
3153 int rn = extract32(insn, 5, 5);
3154 int imm6 = extract32(insn, 10, 6);
3155 int rm = extract32(insn, 16, 5);
3156 int shift_type = extract32(insn, 22, 2);
3157 bool setflags = extract32(insn, 29, 1);
3158 bool sub_op = extract32(insn, 30, 1);
3159 bool sf = extract32(insn, 31, 1);
3161 TCGv_i64 tcg_rd = cpu_reg(s, rd);
3162 TCGv_i64 tcg_rn, tcg_rm;
3163 TCGv_i64 tcg_result;
3165 if ((shift_type == 3) || (!sf && (imm6 > 31))) {
3166 unallocated_encoding(s);
3167 return;
3170 tcg_rn = read_cpu_reg(s, rn, sf);
3171 tcg_rm = read_cpu_reg(s, rm, sf);
3173 shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, imm6);
3175 tcg_result = tcg_temp_new_i64();
3177 if (!setflags) {
3178 if (sub_op) {
3179 tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
3180 } else {
3181 tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
3183 } else {
3184 if (sub_op) {
3185 gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
3186 } else {
3187 gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
3191 if (sf) {
3192 tcg_gen_mov_i64(tcg_rd, tcg_result);
3193 } else {
3194 tcg_gen_ext32u_i64(tcg_rd, tcg_result);
3197 tcg_temp_free_i64(tcg_result);
3200 /* C3.5.9 Data-processing (3 source)
3202 31 30 29 28 24 23 21 20 16 15 14 10 9 5 4 0
3203 +--+------+-----------+------+------+----+------+------+------+
3204 |sf| op54 | 1 1 0 1 1 | op31 | Rm | o0 | Ra | Rn | Rd |
3205 +--+------+-----------+------+------+----+------+------+------+
3208 static void disas_data_proc_3src(DisasContext *s, uint32_t insn)
3210 int rd = extract32(insn, 0, 5);
3211 int rn = extract32(insn, 5, 5);
3212 int ra = extract32(insn, 10, 5);
3213 int rm = extract32(insn, 16, 5);
3214 int op_id = (extract32(insn, 29, 3) << 4) |
3215 (extract32(insn, 21, 3) << 1) |
3216 extract32(insn, 15, 1);
3217 bool sf = extract32(insn, 31, 1);
3218 bool is_sub = extract32(op_id, 0, 1);
3219 bool is_high = extract32(op_id, 2, 1);
3220 bool is_signed = false;
3221 TCGv_i64 tcg_op1;
3222 TCGv_i64 tcg_op2;
3223 TCGv_i64 tcg_tmp;
3225 /* Note that op_id is sf:op54:op31:o0 so it includes the 32/64 size flag */
3226 switch (op_id) {
3227 case 0x42: /* SMADDL */
3228 case 0x43: /* SMSUBL */
3229 case 0x44: /* SMULH */
3230 is_signed = true;
3231 break;
3232 case 0x0: /* MADD (32bit) */
3233 case 0x1: /* MSUB (32bit) */
3234 case 0x40: /* MADD (64bit) */
3235 case 0x41: /* MSUB (64bit) */
3236 case 0x4a: /* UMADDL */
3237 case 0x4b: /* UMSUBL */
3238 case 0x4c: /* UMULH */
3239 break;
3240 default:
3241 unallocated_encoding(s);
3242 return;
3245 if (is_high) {
3246 TCGv_i64 low_bits = tcg_temp_new_i64(); /* low bits discarded */
3247 TCGv_i64 tcg_rd = cpu_reg(s, rd);
3248 TCGv_i64 tcg_rn = cpu_reg(s, rn);
3249 TCGv_i64 tcg_rm = cpu_reg(s, rm);
3251 if (is_signed) {
3252 tcg_gen_muls2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
3253 } else {
3254 tcg_gen_mulu2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
3257 tcg_temp_free_i64(low_bits);
3258 return;
3261 tcg_op1 = tcg_temp_new_i64();
3262 tcg_op2 = tcg_temp_new_i64();
3263 tcg_tmp = tcg_temp_new_i64();
3265 if (op_id < 0x42) {
3266 tcg_gen_mov_i64(tcg_op1, cpu_reg(s, rn));
3267 tcg_gen_mov_i64(tcg_op2, cpu_reg(s, rm));
3268 } else {
3269 if (is_signed) {
3270 tcg_gen_ext32s_i64(tcg_op1, cpu_reg(s, rn));
3271 tcg_gen_ext32s_i64(tcg_op2, cpu_reg(s, rm));
3272 } else {
3273 tcg_gen_ext32u_i64(tcg_op1, cpu_reg(s, rn));
3274 tcg_gen_ext32u_i64(tcg_op2, cpu_reg(s, rm));
3278 if (ra == 31 && !is_sub) {
3279 /* Special-case MADD with rA == XZR; it is the standard MUL alias */
3280 tcg_gen_mul_i64(cpu_reg(s, rd), tcg_op1, tcg_op2);
3281 } else {
3282 tcg_gen_mul_i64(tcg_tmp, tcg_op1, tcg_op2);
3283 if (is_sub) {
3284 tcg_gen_sub_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
3285 } else {
3286 tcg_gen_add_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
3290 if (!sf) {
3291 tcg_gen_ext32u_i64(cpu_reg(s, rd), cpu_reg(s, rd));
3294 tcg_temp_free_i64(tcg_op1);
3295 tcg_temp_free_i64(tcg_op2);
3296 tcg_temp_free_i64(tcg_tmp);
3299 /* C3.5.3 - Add/subtract (with carry)
3300 * 31 30 29 28 27 26 25 24 23 22 21 20 16 15 10 9 5 4 0
3301 * +--+--+--+------------------------+------+---------+------+-----+
3302 * |sf|op| S| 1 1 0 1 0 0 0 0 | rm | opcode2 | Rn | Rd |
3303 * +--+--+--+------------------------+------+---------+------+-----+
3304 * [000000]
3307 static void disas_adc_sbc(DisasContext *s, uint32_t insn)
3309 unsigned int sf, op, setflags, rm, rn, rd;
3310 TCGv_i64 tcg_y, tcg_rn, tcg_rd;
3312 if (extract32(insn, 10, 6) != 0) {
3313 unallocated_encoding(s);
3314 return;
3317 sf = extract32(insn, 31, 1);
3318 op = extract32(insn, 30, 1);
3319 setflags = extract32(insn, 29, 1);
3320 rm = extract32(insn, 16, 5);
3321 rn = extract32(insn, 5, 5);
3322 rd = extract32(insn, 0, 5);
3324 tcg_rd = cpu_reg(s, rd);
3325 tcg_rn = cpu_reg(s, rn);
3327 if (op) {
3328 tcg_y = new_tmp_a64(s);
3329 tcg_gen_not_i64(tcg_y, cpu_reg(s, rm));
3330 } else {
3331 tcg_y = cpu_reg(s, rm);
3334 if (setflags) {
3335 gen_adc_CC(sf, tcg_rd, tcg_rn, tcg_y);
3336 } else {
3337 gen_adc(sf, tcg_rd, tcg_rn, tcg_y);
3341 /* C3.5.4 - C3.5.5 Conditional compare (immediate / register)
3342 * 31 30 29 28 27 26 25 24 23 22 21 20 16 15 12 11 10 9 5 4 3 0
3343 * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
3344 * |sf|op| S| 1 1 0 1 0 0 1 0 |imm5/rm | cond |i/r |o2| Rn |o3|nzcv |
3345 * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
3346 * [1] y [0] [0]
3348 static void disas_cc(DisasContext *s, uint32_t insn)
3350 unsigned int sf, op, y, cond, rn, nzcv, is_imm;
3351 int label_continue = -1;
3352 TCGv_i64 tcg_tmp, tcg_y, tcg_rn;
3354 if (!extract32(insn, 29, 1)) {
3355 unallocated_encoding(s);
3356 return;
3358 if (insn & (1 << 10 | 1 << 4)) {
3359 unallocated_encoding(s);
3360 return;
3362 sf = extract32(insn, 31, 1);
3363 op = extract32(insn, 30, 1);
3364 is_imm = extract32(insn, 11, 1);
3365 y = extract32(insn, 16, 5); /* y = rm (reg) or imm5 (imm) */
3366 cond = extract32(insn, 12, 4);
3367 rn = extract32(insn, 5, 5);
3368 nzcv = extract32(insn, 0, 4);
3370 if (cond < 0x0e) { /* not always */
3371 int label_match = gen_new_label();
3372 label_continue = gen_new_label();
3373 arm_gen_test_cc(cond, label_match);
3374 /* nomatch: */
3375 tcg_tmp = tcg_temp_new_i64();
3376 tcg_gen_movi_i64(tcg_tmp, nzcv << 28);
3377 gen_set_nzcv(tcg_tmp);
3378 tcg_temp_free_i64(tcg_tmp);
3379 tcg_gen_br(label_continue);
3380 gen_set_label(label_match);
3382 /* match, or condition is always */
3383 if (is_imm) {
3384 tcg_y = new_tmp_a64(s);
3385 tcg_gen_movi_i64(tcg_y, y);
3386 } else {
3387 tcg_y = cpu_reg(s, y);
3389 tcg_rn = cpu_reg(s, rn);
3391 tcg_tmp = tcg_temp_new_i64();
3392 if (op) {
3393 gen_sub_CC(sf, tcg_tmp, tcg_rn, tcg_y);
3394 } else {
3395 gen_add_CC(sf, tcg_tmp, tcg_rn, tcg_y);
3397 tcg_temp_free_i64(tcg_tmp);
3399 if (cond < 0x0e) { /* continue */
3400 gen_set_label(label_continue);
3404 /* C3.5.6 Conditional select
3405 * 31 30 29 28 21 20 16 15 12 11 10 9 5 4 0
3406 * +----+----+---+-----------------+------+------+-----+------+------+
3407 * | sf | op | S | 1 1 0 1 0 1 0 0 | Rm | cond | op2 | Rn | Rd |
3408 * +----+----+---+-----------------+------+------+-----+------+------+
3410 static void disas_cond_select(DisasContext *s, uint32_t insn)
3412 unsigned int sf, else_inv, rm, cond, else_inc, rn, rd;
3413 TCGv_i64 tcg_rd, tcg_src;
3415 if (extract32(insn, 29, 1) || extract32(insn, 11, 1)) {
3416 /* S == 1 or op2<1> == 1 */
3417 unallocated_encoding(s);
3418 return;
3420 sf = extract32(insn, 31, 1);
3421 else_inv = extract32(insn, 30, 1);
3422 rm = extract32(insn, 16, 5);
3423 cond = extract32(insn, 12, 4);
3424 else_inc = extract32(insn, 10, 1);
3425 rn = extract32(insn, 5, 5);
3426 rd = extract32(insn, 0, 5);
3428 if (rd == 31) {
3429 /* silly no-op write; until we use movcond we must special-case
3430 * this to avoid a dead temporary across basic blocks.
3432 return;
3435 tcg_rd = cpu_reg(s, rd);
3437 if (cond >= 0x0e) { /* condition "always" */
3438 tcg_src = read_cpu_reg(s, rn, sf);
3439 tcg_gen_mov_i64(tcg_rd, tcg_src);
3440 } else {
3441 /* OPTME: we could use movcond here, at the cost of duplicating
3442 * a lot of the arm_gen_test_cc() logic.
3444 int label_match = gen_new_label();
3445 int label_continue = gen_new_label();
3447 arm_gen_test_cc(cond, label_match);
3448 /* nomatch: */
3449 tcg_src = cpu_reg(s, rm);
3451 if (else_inv && else_inc) {
3452 tcg_gen_neg_i64(tcg_rd, tcg_src);
3453 } else if (else_inv) {
3454 tcg_gen_not_i64(tcg_rd, tcg_src);
3455 } else if (else_inc) {
3456 tcg_gen_addi_i64(tcg_rd, tcg_src, 1);
3457 } else {
3458 tcg_gen_mov_i64(tcg_rd, tcg_src);
3460 if (!sf) {
3461 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3463 tcg_gen_br(label_continue);
3464 /* match: */
3465 gen_set_label(label_match);
3466 tcg_src = read_cpu_reg(s, rn, sf);
3467 tcg_gen_mov_i64(tcg_rd, tcg_src);
3468 /* continue: */
3469 gen_set_label(label_continue);
3473 static void handle_clz(DisasContext *s, unsigned int sf,
3474 unsigned int rn, unsigned int rd)
3476 TCGv_i64 tcg_rd, tcg_rn;
3477 tcg_rd = cpu_reg(s, rd);
3478 tcg_rn = cpu_reg(s, rn);
3480 if (sf) {
3481 gen_helper_clz64(tcg_rd, tcg_rn);
3482 } else {
3483 TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
3484 tcg_gen_trunc_i64_i32(tcg_tmp32, tcg_rn);
3485 gen_helper_clz(tcg_tmp32, tcg_tmp32);
3486 tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
3487 tcg_temp_free_i32(tcg_tmp32);
3491 static void handle_cls(DisasContext *s, unsigned int sf,
3492 unsigned int rn, unsigned int rd)
3494 TCGv_i64 tcg_rd, tcg_rn;
3495 tcg_rd = cpu_reg(s, rd);
3496 tcg_rn = cpu_reg(s, rn);
3498 if (sf) {
3499 gen_helper_cls64(tcg_rd, tcg_rn);
3500 } else {
3501 TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
3502 tcg_gen_trunc_i64_i32(tcg_tmp32, tcg_rn);
3503 gen_helper_cls32(tcg_tmp32, tcg_tmp32);
3504 tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
3505 tcg_temp_free_i32(tcg_tmp32);
3509 static void handle_rbit(DisasContext *s, unsigned int sf,
3510 unsigned int rn, unsigned int rd)
3512 TCGv_i64 tcg_rd, tcg_rn;
3513 tcg_rd = cpu_reg(s, rd);
3514 tcg_rn = cpu_reg(s, rn);
3516 if (sf) {
3517 gen_helper_rbit64(tcg_rd, tcg_rn);
3518 } else {
3519 TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
3520 tcg_gen_trunc_i64_i32(tcg_tmp32, tcg_rn);
3521 gen_helper_rbit(tcg_tmp32, tcg_tmp32);
3522 tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
3523 tcg_temp_free_i32(tcg_tmp32);
3527 /* C5.6.149 REV with sf==1, opcode==3 ("REV64") */
3528 static void handle_rev64(DisasContext *s, unsigned int sf,
3529 unsigned int rn, unsigned int rd)
3531 if (!sf) {
3532 unallocated_encoding(s);
3533 return;
3535 tcg_gen_bswap64_i64(cpu_reg(s, rd), cpu_reg(s, rn));
3538 /* C5.6.149 REV with sf==0, opcode==2
3539 * C5.6.151 REV32 (sf==1, opcode==2)
3541 static void handle_rev32(DisasContext *s, unsigned int sf,
3542 unsigned int rn, unsigned int rd)
3544 TCGv_i64 tcg_rd = cpu_reg(s, rd);
3546 if (sf) {
3547 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
3548 TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
3550 /* bswap32_i64 requires zero high word */
3551 tcg_gen_ext32u_i64(tcg_tmp, tcg_rn);
3552 tcg_gen_bswap32_i64(tcg_rd, tcg_tmp);
3553 tcg_gen_shri_i64(tcg_tmp, tcg_rn, 32);
3554 tcg_gen_bswap32_i64(tcg_tmp, tcg_tmp);
3555 tcg_gen_concat32_i64(tcg_rd, tcg_rd, tcg_tmp);
3557 tcg_temp_free_i64(tcg_tmp);
3558 } else {
3559 tcg_gen_ext32u_i64(tcg_rd, cpu_reg(s, rn));
3560 tcg_gen_bswap32_i64(tcg_rd, tcg_rd);
3564 /* C5.6.150 REV16 (opcode==1) */
3565 static void handle_rev16(DisasContext *s, unsigned int sf,
3566 unsigned int rn, unsigned int rd)
3568 TCGv_i64 tcg_rd = cpu_reg(s, rd);
3569 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
3570 TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
3572 tcg_gen_andi_i64(tcg_tmp, tcg_rn, 0xffff);
3573 tcg_gen_bswap16_i64(tcg_rd, tcg_tmp);
3575 tcg_gen_shri_i64(tcg_tmp, tcg_rn, 16);
3576 tcg_gen_andi_i64(tcg_tmp, tcg_tmp, 0xffff);
3577 tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp);
3578 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, 16, 16);
3580 if (sf) {
3581 tcg_gen_shri_i64(tcg_tmp, tcg_rn, 32);
3582 tcg_gen_andi_i64(tcg_tmp, tcg_tmp, 0xffff);
3583 tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp);
3584 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, 32, 16);
3586 tcg_gen_shri_i64(tcg_tmp, tcg_rn, 48);
3587 tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp);
3588 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, 48, 16);
3591 tcg_temp_free_i64(tcg_tmp);
3594 /* C3.5.7 Data-processing (1 source)
3595 * 31 30 29 28 21 20 16 15 10 9 5 4 0
3596 * +----+---+---+-----------------+---------+--------+------+------+
3597 * | sf | 1 | S | 1 1 0 1 0 1 1 0 | opcode2 | opcode | Rn | Rd |
3598 * +----+---+---+-----------------+---------+--------+------+------+
3600 static void disas_data_proc_1src(DisasContext *s, uint32_t insn)
3602 unsigned int sf, opcode, rn, rd;
3604 if (extract32(insn, 29, 1) || extract32(insn, 16, 5)) {
3605 unallocated_encoding(s);
3606 return;
3609 sf = extract32(insn, 31, 1);
3610 opcode = extract32(insn, 10, 6);
3611 rn = extract32(insn, 5, 5);
3612 rd = extract32(insn, 0, 5);
3614 switch (opcode) {
3615 case 0: /* RBIT */
3616 handle_rbit(s, sf, rn, rd);
3617 break;
3618 case 1: /* REV16 */
3619 handle_rev16(s, sf, rn, rd);
3620 break;
3621 case 2: /* REV32 */
3622 handle_rev32(s, sf, rn, rd);
3623 break;
3624 case 3: /* REV64 */
3625 handle_rev64(s, sf, rn, rd);
3626 break;
3627 case 4: /* CLZ */
3628 handle_clz(s, sf, rn, rd);
3629 break;
3630 case 5: /* CLS */
3631 handle_cls(s, sf, rn, rd);
3632 break;
3636 static void handle_div(DisasContext *s, bool is_signed, unsigned int sf,
3637 unsigned int rm, unsigned int rn, unsigned int rd)
3639 TCGv_i64 tcg_n, tcg_m, tcg_rd;
3640 tcg_rd = cpu_reg(s, rd);
3642 if (!sf && is_signed) {
3643 tcg_n = new_tmp_a64(s);
3644 tcg_m = new_tmp_a64(s);
3645 tcg_gen_ext32s_i64(tcg_n, cpu_reg(s, rn));
3646 tcg_gen_ext32s_i64(tcg_m, cpu_reg(s, rm));
3647 } else {
3648 tcg_n = read_cpu_reg(s, rn, sf);
3649 tcg_m = read_cpu_reg(s, rm, sf);
3652 if (is_signed) {
3653 gen_helper_sdiv64(tcg_rd, tcg_n, tcg_m);
3654 } else {
3655 gen_helper_udiv64(tcg_rd, tcg_n, tcg_m);
3658 if (!sf) { /* zero extend final result */
3659 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3663 /* C5.6.115 LSLV, C5.6.118 LSRV, C5.6.17 ASRV, C5.6.154 RORV */
3664 static void handle_shift_reg(DisasContext *s,
3665 enum a64_shift_type shift_type, unsigned int sf,
3666 unsigned int rm, unsigned int rn, unsigned int rd)
3668 TCGv_i64 tcg_shift = tcg_temp_new_i64();
3669 TCGv_i64 tcg_rd = cpu_reg(s, rd);
3670 TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
3672 tcg_gen_andi_i64(tcg_shift, cpu_reg(s, rm), sf ? 63 : 31);
3673 shift_reg(tcg_rd, tcg_rn, sf, shift_type, tcg_shift);
3674 tcg_temp_free_i64(tcg_shift);
3677 /* C3.5.8 Data-processing (2 source)
3678 * 31 30 29 28 21 20 16 15 10 9 5 4 0
3679 * +----+---+---+-----------------+------+--------+------+------+
3680 * | sf | 0 | S | 1 1 0 1 0 1 1 0 | Rm | opcode | Rn | Rd |
3681 * +----+---+---+-----------------+------+--------+------+------+
3683 static void disas_data_proc_2src(DisasContext *s, uint32_t insn)
3685 unsigned int sf, rm, opcode, rn, rd;
3686 sf = extract32(insn, 31, 1);
3687 rm = extract32(insn, 16, 5);
3688 opcode = extract32(insn, 10, 6);
3689 rn = extract32(insn, 5, 5);
3690 rd = extract32(insn, 0, 5);
3692 if (extract32(insn, 29, 1)) {
3693 unallocated_encoding(s);
3694 return;
3697 switch (opcode) {
3698 case 2: /* UDIV */
3699 handle_div(s, false, sf, rm, rn, rd);
3700 break;
3701 case 3: /* SDIV */
3702 handle_div(s, true, sf, rm, rn, rd);
3703 break;
3704 case 8: /* LSLV */
3705 handle_shift_reg(s, A64_SHIFT_TYPE_LSL, sf, rm, rn, rd);
3706 break;
3707 case 9: /* LSRV */
3708 handle_shift_reg(s, A64_SHIFT_TYPE_LSR, sf, rm, rn, rd);
3709 break;
3710 case 10: /* ASRV */
3711 handle_shift_reg(s, A64_SHIFT_TYPE_ASR, sf, rm, rn, rd);
3712 break;
3713 case 11: /* RORV */
3714 handle_shift_reg(s, A64_SHIFT_TYPE_ROR, sf, rm, rn, rd);
3715 break;
3716 case 16:
3717 case 17:
3718 case 18:
3719 case 19:
3720 case 20:
3721 case 21:
3722 case 22:
3723 case 23: /* CRC32 */
3724 unsupported_encoding(s, insn);
3725 break;
3726 default:
3727 unallocated_encoding(s);
3728 break;
3732 /* C3.5 Data processing - register */
3733 static void disas_data_proc_reg(DisasContext *s, uint32_t insn)
3735 switch (extract32(insn, 24, 5)) {
3736 case 0x0a: /* Logical (shifted register) */
3737 disas_logic_reg(s, insn);
3738 break;
3739 case 0x0b: /* Add/subtract */
3740 if (insn & (1 << 21)) { /* (extended register) */
3741 disas_add_sub_ext_reg(s, insn);
3742 } else {
3743 disas_add_sub_reg(s, insn);
3745 break;
3746 case 0x1b: /* Data-processing (3 source) */
3747 disas_data_proc_3src(s, insn);
3748 break;
3749 case 0x1a:
3750 switch (extract32(insn, 21, 3)) {
3751 case 0x0: /* Add/subtract (with carry) */
3752 disas_adc_sbc(s, insn);
3753 break;
3754 case 0x2: /* Conditional compare */
3755 disas_cc(s, insn); /* both imm and reg forms */
3756 break;
3757 case 0x4: /* Conditional select */
3758 disas_cond_select(s, insn);
3759 break;
3760 case 0x6: /* Data-processing */
3761 if (insn & (1 << 30)) { /* (1 source) */
3762 disas_data_proc_1src(s, insn);
3763 } else { /* (2 source) */
3764 disas_data_proc_2src(s, insn);
3766 break;
3767 default:
3768 unallocated_encoding(s);
3769 break;
3771 break;
3772 default:
3773 unallocated_encoding(s);
3774 break;
3778 static void handle_fp_compare(DisasContext *s, bool is_double,
3779 unsigned int rn, unsigned int rm,
3780 bool cmp_with_zero, bool signal_all_nans)
3782 TCGv_i64 tcg_flags = tcg_temp_new_i64();
3783 TCGv_ptr fpst = get_fpstatus_ptr();
3785 if (is_double) {
3786 TCGv_i64 tcg_vn, tcg_vm;
3788 tcg_vn = read_fp_dreg(s, rn);
3789 if (cmp_with_zero) {
3790 tcg_vm = tcg_const_i64(0);
3791 } else {
3792 tcg_vm = read_fp_dreg(s, rm);
3794 if (signal_all_nans) {
3795 gen_helper_vfp_cmped_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
3796 } else {
3797 gen_helper_vfp_cmpd_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
3799 tcg_temp_free_i64(tcg_vn);
3800 tcg_temp_free_i64(tcg_vm);
3801 } else {
3802 TCGv_i32 tcg_vn, tcg_vm;
3804 tcg_vn = read_fp_sreg(s, rn);
3805 if (cmp_with_zero) {
3806 tcg_vm = tcg_const_i32(0);
3807 } else {
3808 tcg_vm = read_fp_sreg(s, rm);
3810 if (signal_all_nans) {
3811 gen_helper_vfp_cmpes_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
3812 } else {
3813 gen_helper_vfp_cmps_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
3815 tcg_temp_free_i32(tcg_vn);
3816 tcg_temp_free_i32(tcg_vm);
3819 tcg_temp_free_ptr(fpst);
3821 gen_set_nzcv(tcg_flags);
3823 tcg_temp_free_i64(tcg_flags);
3826 /* C3.6.22 Floating point compare
3827 * 31 30 29 28 24 23 22 21 20 16 15 14 13 10 9 5 4 0
3828 * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
3829 * | M | 0 | S | 1 1 1 1 0 | type | 1 | Rm | op | 1 0 0 0 | Rn | op2 |
3830 * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
3832 static void disas_fp_compare(DisasContext *s, uint32_t insn)
3834 unsigned int mos, type, rm, op, rn, opc, op2r;
3836 mos = extract32(insn, 29, 3);
3837 type = extract32(insn, 22, 2); /* 0 = single, 1 = double */
3838 rm = extract32(insn, 16, 5);
3839 op = extract32(insn, 14, 2);
3840 rn = extract32(insn, 5, 5);
3841 opc = extract32(insn, 3, 2);
3842 op2r = extract32(insn, 0, 3);
3844 if (mos || op || op2r || type > 1) {
3845 unallocated_encoding(s);
3846 return;
3849 handle_fp_compare(s, type, rn, rm, opc & 1, opc & 2);
3852 /* C3.6.23 Floating point conditional compare
3853 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 3 0
3854 * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
3855 * | M | 0 | S | 1 1 1 1 0 | type | 1 | Rm | cond | 0 1 | Rn | op | nzcv |
3856 * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
3858 static void disas_fp_ccomp(DisasContext *s, uint32_t insn)
3860 unsigned int mos, type, rm, cond, rn, op, nzcv;
3861 TCGv_i64 tcg_flags;
3862 int label_continue = -1;
3864 mos = extract32(insn, 29, 3);
3865 type = extract32(insn, 22, 2); /* 0 = single, 1 = double */
3866 rm = extract32(insn, 16, 5);
3867 cond = extract32(insn, 12, 4);
3868 rn = extract32(insn, 5, 5);
3869 op = extract32(insn, 4, 1);
3870 nzcv = extract32(insn, 0, 4);
3872 if (mos || type > 1) {
3873 unallocated_encoding(s);
3874 return;
3877 if (cond < 0x0e) { /* not always */
3878 int label_match = gen_new_label();
3879 label_continue = gen_new_label();
3880 arm_gen_test_cc(cond, label_match);
3881 /* nomatch: */
3882 tcg_flags = tcg_const_i64(nzcv << 28);
3883 gen_set_nzcv(tcg_flags);
3884 tcg_temp_free_i64(tcg_flags);
3885 tcg_gen_br(label_continue);
3886 gen_set_label(label_match);
3889 handle_fp_compare(s, type, rn, rm, false, op);
3891 if (cond < 0x0e) {
3892 gen_set_label(label_continue);
3896 /* copy src FP register to dst FP register; type specifies single or double */
3897 static void gen_mov_fp2fp(DisasContext *s, int type, int dst, int src)
3899 if (type) {
3900 TCGv_i64 v = read_fp_dreg(s, src);
3901 write_fp_dreg(s, dst, v);
3902 tcg_temp_free_i64(v);
3903 } else {
3904 TCGv_i32 v = read_fp_sreg(s, src);
3905 write_fp_sreg(s, dst, v);
3906 tcg_temp_free_i32(v);
3910 /* C3.6.24 Floating point conditional select
3911 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 0
3912 * +---+---+---+-----------+------+---+------+------+-----+------+------+
3913 * | M | 0 | S | 1 1 1 1 0 | type | 1 | Rm | cond | 1 1 | Rn | Rd |
3914 * +---+---+---+-----------+------+---+------+------+-----+------+------+
3916 static void disas_fp_csel(DisasContext *s, uint32_t insn)
3918 unsigned int mos, type, rm, cond, rn, rd;
3919 int label_continue = -1;
3921 mos = extract32(insn, 29, 3);
3922 type = extract32(insn, 22, 2); /* 0 = single, 1 = double */
3923 rm = extract32(insn, 16, 5);
3924 cond = extract32(insn, 12, 4);
3925 rn = extract32(insn, 5, 5);
3926 rd = extract32(insn, 0, 5);
3928 if (mos || type > 1) {
3929 unallocated_encoding(s);
3930 return;
3933 if (cond < 0x0e) { /* not always */
3934 int label_match = gen_new_label();
3935 label_continue = gen_new_label();
3936 arm_gen_test_cc(cond, label_match);
3937 /* nomatch: */
3938 gen_mov_fp2fp(s, type, rd, rm);
3939 tcg_gen_br(label_continue);
3940 gen_set_label(label_match);
3943 gen_mov_fp2fp(s, type, rd, rn);
3945 if (cond < 0x0e) { /* continue */
3946 gen_set_label(label_continue);
3950 /* C3.6.25 Floating-point data-processing (1 source) - single precision */
3951 static void handle_fp_1src_single(DisasContext *s, int opcode, int rd, int rn)
3953 TCGv_ptr fpst;
3954 TCGv_i32 tcg_op;
3955 TCGv_i32 tcg_res;
3957 fpst = get_fpstatus_ptr();
3958 tcg_op = read_fp_sreg(s, rn);
3959 tcg_res = tcg_temp_new_i32();
3961 switch (opcode) {
3962 case 0x0: /* FMOV */
3963 tcg_gen_mov_i32(tcg_res, tcg_op);
3964 break;
3965 case 0x1: /* FABS */
3966 gen_helper_vfp_abss(tcg_res, tcg_op);
3967 break;
3968 case 0x2: /* FNEG */
3969 gen_helper_vfp_negs(tcg_res, tcg_op);
3970 break;
3971 case 0x3: /* FSQRT */
3972 gen_helper_vfp_sqrts(tcg_res, tcg_op, cpu_env);
3973 break;
3974 case 0x8: /* FRINTN */
3975 case 0x9: /* FRINTP */
3976 case 0xa: /* FRINTM */
3977 case 0xb: /* FRINTZ */
3978 case 0xc: /* FRINTA */
3980 TCGv_i32 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(opcode & 7));
3982 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
3983 gen_helper_rints(tcg_res, tcg_op, fpst);
3985 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
3986 tcg_temp_free_i32(tcg_rmode);
3987 break;
3989 case 0xe: /* FRINTX */
3990 gen_helper_rints_exact(tcg_res, tcg_op, fpst);
3991 break;
3992 case 0xf: /* FRINTI */
3993 gen_helper_rints(tcg_res, tcg_op, fpst);
3994 break;
3995 default:
3996 abort();
3999 write_fp_sreg(s, rd, tcg_res);
4001 tcg_temp_free_ptr(fpst);
4002 tcg_temp_free_i32(tcg_op);
4003 tcg_temp_free_i32(tcg_res);
4006 /* C3.6.25 Floating-point data-processing (1 source) - double precision */
4007 static void handle_fp_1src_double(DisasContext *s, int opcode, int rd, int rn)
4009 TCGv_ptr fpst;
4010 TCGv_i64 tcg_op;
4011 TCGv_i64 tcg_res;
4013 fpst = get_fpstatus_ptr();
4014 tcg_op = read_fp_dreg(s, rn);
4015 tcg_res = tcg_temp_new_i64();
4017 switch (opcode) {
4018 case 0x0: /* FMOV */
4019 tcg_gen_mov_i64(tcg_res, tcg_op);
4020 break;
4021 case 0x1: /* FABS */
4022 gen_helper_vfp_absd(tcg_res, tcg_op);
4023 break;
4024 case 0x2: /* FNEG */
4025 gen_helper_vfp_negd(tcg_res, tcg_op);
4026 break;
4027 case 0x3: /* FSQRT */
4028 gen_helper_vfp_sqrtd(tcg_res, tcg_op, cpu_env);
4029 break;
4030 case 0x8: /* FRINTN */
4031 case 0x9: /* FRINTP */
4032 case 0xa: /* FRINTM */
4033 case 0xb: /* FRINTZ */
4034 case 0xc: /* FRINTA */
4036 TCGv_i32 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(opcode & 7));
4038 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
4039 gen_helper_rintd(tcg_res, tcg_op, fpst);
4041 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
4042 tcg_temp_free_i32(tcg_rmode);
4043 break;
4045 case 0xe: /* FRINTX */
4046 gen_helper_rintd_exact(tcg_res, tcg_op, fpst);
4047 break;
4048 case 0xf: /* FRINTI */
4049 gen_helper_rintd(tcg_res, tcg_op, fpst);
4050 break;
4051 default:
4052 abort();
4055 write_fp_dreg(s, rd, tcg_res);
4057 tcg_temp_free_ptr(fpst);
4058 tcg_temp_free_i64(tcg_op);
4059 tcg_temp_free_i64(tcg_res);
4062 static void handle_fp_fcvt(DisasContext *s, int opcode,
4063 int rd, int rn, int dtype, int ntype)
4065 switch (ntype) {
4066 case 0x0:
4068 TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
4069 if (dtype == 1) {
4070 /* Single to double */
4071 TCGv_i64 tcg_rd = tcg_temp_new_i64();
4072 gen_helper_vfp_fcvtds(tcg_rd, tcg_rn, cpu_env);
4073 write_fp_dreg(s, rd, tcg_rd);
4074 tcg_temp_free_i64(tcg_rd);
4075 } else {
4076 /* Single to half */
4077 TCGv_i32 tcg_rd = tcg_temp_new_i32();
4078 gen_helper_vfp_fcvt_f32_to_f16(tcg_rd, tcg_rn, cpu_env);
4079 /* write_fp_sreg is OK here because top half of tcg_rd is zero */
4080 write_fp_sreg(s, rd, tcg_rd);
4081 tcg_temp_free_i32(tcg_rd);
4083 tcg_temp_free_i32(tcg_rn);
4084 break;
4086 case 0x1:
4088 TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
4089 TCGv_i32 tcg_rd = tcg_temp_new_i32();
4090 if (dtype == 0) {
4091 /* Double to single */
4092 gen_helper_vfp_fcvtsd(tcg_rd, tcg_rn, cpu_env);
4093 } else {
4094 /* Double to half */
4095 gen_helper_vfp_fcvt_f64_to_f16(tcg_rd, tcg_rn, cpu_env);
4096 /* write_fp_sreg is OK here because top half of tcg_rd is zero */
4098 write_fp_sreg(s, rd, tcg_rd);
4099 tcg_temp_free_i32(tcg_rd);
4100 tcg_temp_free_i64(tcg_rn);
4101 break;
4103 case 0x3:
4105 TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
4106 tcg_gen_ext16u_i32(tcg_rn, tcg_rn);
4107 if (dtype == 0) {
4108 /* Half to single */
4109 TCGv_i32 tcg_rd = tcg_temp_new_i32();
4110 gen_helper_vfp_fcvt_f16_to_f32(tcg_rd, tcg_rn, cpu_env);
4111 write_fp_sreg(s, rd, tcg_rd);
4112 tcg_temp_free_i32(tcg_rd);
4113 } else {
4114 /* Half to double */
4115 TCGv_i64 tcg_rd = tcg_temp_new_i64();
4116 gen_helper_vfp_fcvt_f16_to_f64(tcg_rd, tcg_rn, cpu_env);
4117 write_fp_dreg(s, rd, tcg_rd);
4118 tcg_temp_free_i64(tcg_rd);
4120 tcg_temp_free_i32(tcg_rn);
4121 break;
4123 default:
4124 abort();
4128 /* C3.6.25 Floating point data-processing (1 source)
4129 * 31 30 29 28 24 23 22 21 20 15 14 10 9 5 4 0
4130 * +---+---+---+-----------+------+---+--------+-----------+------+------+
4131 * | M | 0 | S | 1 1 1 1 0 | type | 1 | opcode | 1 0 0 0 0 | Rn | Rd |
4132 * +---+---+---+-----------+------+---+--------+-----------+------+------+
4134 static void disas_fp_1src(DisasContext *s, uint32_t insn)
4136 int type = extract32(insn, 22, 2);
4137 int opcode = extract32(insn, 15, 6);
4138 int rn = extract32(insn, 5, 5);
4139 int rd = extract32(insn, 0, 5);
4141 switch (opcode) {
4142 case 0x4: case 0x5: case 0x7:
4144 /* FCVT between half, single and double precision */
4145 int dtype = extract32(opcode, 0, 2);
4146 if (type == 2 || dtype == type) {
4147 unallocated_encoding(s);
4148 return;
4150 handle_fp_fcvt(s, opcode, rd, rn, dtype, type);
4151 break;
4153 case 0x0 ... 0x3:
4154 case 0x8 ... 0xc:
4155 case 0xe ... 0xf:
4156 /* 32-to-32 and 64-to-64 ops */
4157 switch (type) {
4158 case 0:
4159 handle_fp_1src_single(s, opcode, rd, rn);
4160 break;
4161 case 1:
4162 handle_fp_1src_double(s, opcode, rd, rn);
4163 break;
4164 default:
4165 unallocated_encoding(s);
4167 break;
4168 default:
4169 unallocated_encoding(s);
4170 break;
4174 /* C3.6.26 Floating-point data-processing (2 source) - single precision */
4175 static void handle_fp_2src_single(DisasContext *s, int opcode,
4176 int rd, int rn, int rm)
4178 TCGv_i32 tcg_op1;
4179 TCGv_i32 tcg_op2;
4180 TCGv_i32 tcg_res;
4181 TCGv_ptr fpst;
4183 tcg_res = tcg_temp_new_i32();
4184 fpst = get_fpstatus_ptr();
4185 tcg_op1 = read_fp_sreg(s, rn);
4186 tcg_op2 = read_fp_sreg(s, rm);
4188 switch (opcode) {
4189 case 0x0: /* FMUL */
4190 gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
4191 break;
4192 case 0x1: /* FDIV */
4193 gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
4194 break;
4195 case 0x2: /* FADD */
4196 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
4197 break;
4198 case 0x3: /* FSUB */
4199 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
4200 break;
4201 case 0x4: /* FMAX */
4202 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
4203 break;
4204 case 0x5: /* FMIN */
4205 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
4206 break;
4207 case 0x6: /* FMAXNM */
4208 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
4209 break;
4210 case 0x7: /* FMINNM */
4211 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
4212 break;
4213 case 0x8: /* FNMUL */
4214 gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
4215 gen_helper_vfp_negs(tcg_res, tcg_res);
4216 break;
4219 write_fp_sreg(s, rd, tcg_res);
4221 tcg_temp_free_ptr(fpst);
4222 tcg_temp_free_i32(tcg_op1);
4223 tcg_temp_free_i32(tcg_op2);
4224 tcg_temp_free_i32(tcg_res);
4227 /* C3.6.26 Floating-point data-processing (2 source) - double precision */
4228 static void handle_fp_2src_double(DisasContext *s, int opcode,
4229 int rd, int rn, int rm)
4231 TCGv_i64 tcg_op1;
4232 TCGv_i64 tcg_op2;
4233 TCGv_i64 tcg_res;
4234 TCGv_ptr fpst;
4236 tcg_res = tcg_temp_new_i64();
4237 fpst = get_fpstatus_ptr();
4238 tcg_op1 = read_fp_dreg(s, rn);
4239 tcg_op2 = read_fp_dreg(s, rm);
4241 switch (opcode) {
4242 case 0x0: /* FMUL */
4243 gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
4244 break;
4245 case 0x1: /* FDIV */
4246 gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
4247 break;
4248 case 0x2: /* FADD */
4249 gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
4250 break;
4251 case 0x3: /* FSUB */
4252 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
4253 break;
4254 case 0x4: /* FMAX */
4255 gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
4256 break;
4257 case 0x5: /* FMIN */
4258 gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
4259 break;
4260 case 0x6: /* FMAXNM */
4261 gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
4262 break;
4263 case 0x7: /* FMINNM */
4264 gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
4265 break;
4266 case 0x8: /* FNMUL */
4267 gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
4268 gen_helper_vfp_negd(tcg_res, tcg_res);
4269 break;
4272 write_fp_dreg(s, rd, tcg_res);
4274 tcg_temp_free_ptr(fpst);
4275 tcg_temp_free_i64(tcg_op1);
4276 tcg_temp_free_i64(tcg_op2);
4277 tcg_temp_free_i64(tcg_res);
4280 /* C3.6.26 Floating point data-processing (2 source)
4281 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 0
4282 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
4283 * | M | 0 | S | 1 1 1 1 0 | type | 1 | Rm | opcode | 1 0 | Rn | Rd |
4284 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
4286 static void disas_fp_2src(DisasContext *s, uint32_t insn)
4288 int type = extract32(insn, 22, 2);
4289 int rd = extract32(insn, 0, 5);
4290 int rn = extract32(insn, 5, 5);
4291 int rm = extract32(insn, 16, 5);
4292 int opcode = extract32(insn, 12, 4);
4294 if (opcode > 8) {
4295 unallocated_encoding(s);
4296 return;
4299 switch (type) {
4300 case 0:
4301 handle_fp_2src_single(s, opcode, rd, rn, rm);
4302 break;
4303 case 1:
4304 handle_fp_2src_double(s, opcode, rd, rn, rm);
4305 break;
4306 default:
4307 unallocated_encoding(s);
4311 /* C3.6.27 Floating-point data-processing (3 source) - single precision */
4312 static void handle_fp_3src_single(DisasContext *s, bool o0, bool o1,
4313 int rd, int rn, int rm, int ra)
4315 TCGv_i32 tcg_op1, tcg_op2, tcg_op3;
4316 TCGv_i32 tcg_res = tcg_temp_new_i32();
4317 TCGv_ptr fpst = get_fpstatus_ptr();
4319 tcg_op1 = read_fp_sreg(s, rn);
4320 tcg_op2 = read_fp_sreg(s, rm);
4321 tcg_op3 = read_fp_sreg(s, ra);
4323 /* These are fused multiply-add, and must be done as one
4324 * floating point operation with no rounding between the
4325 * multiplication and addition steps.
4326 * NB that doing the negations here as separate steps is
4327 * correct : an input NaN should come out with its sign bit
4328 * flipped if it is a negated-input.
4330 if (o1 == true) {
4331 gen_helper_vfp_negs(tcg_op3, tcg_op3);
4334 if (o0 != o1) {
4335 gen_helper_vfp_negs(tcg_op1, tcg_op1);
4338 gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
4340 write_fp_sreg(s, rd, tcg_res);
4342 tcg_temp_free_ptr(fpst);
4343 tcg_temp_free_i32(tcg_op1);
4344 tcg_temp_free_i32(tcg_op2);
4345 tcg_temp_free_i32(tcg_op3);
4346 tcg_temp_free_i32(tcg_res);
4349 /* C3.6.27 Floating-point data-processing (3 source) - double precision */
4350 static void handle_fp_3src_double(DisasContext *s, bool o0, bool o1,
4351 int rd, int rn, int rm, int ra)
4353 TCGv_i64 tcg_op1, tcg_op2, tcg_op3;
4354 TCGv_i64 tcg_res = tcg_temp_new_i64();
4355 TCGv_ptr fpst = get_fpstatus_ptr();
4357 tcg_op1 = read_fp_dreg(s, rn);
4358 tcg_op2 = read_fp_dreg(s, rm);
4359 tcg_op3 = read_fp_dreg(s, ra);
4361 /* These are fused multiply-add, and must be done as one
4362 * floating point operation with no rounding between the
4363 * multiplication and addition steps.
4364 * NB that doing the negations here as separate steps is
4365 * correct : an input NaN should come out with its sign bit
4366 * flipped if it is a negated-input.
4368 if (o1 == true) {
4369 gen_helper_vfp_negd(tcg_op3, tcg_op3);
4372 if (o0 != o1) {
4373 gen_helper_vfp_negd(tcg_op1, tcg_op1);
4376 gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
4378 write_fp_dreg(s, rd, tcg_res);
4380 tcg_temp_free_ptr(fpst);
4381 tcg_temp_free_i64(tcg_op1);
4382 tcg_temp_free_i64(tcg_op2);
4383 tcg_temp_free_i64(tcg_op3);
4384 tcg_temp_free_i64(tcg_res);
4387 /* C3.6.27 Floating point data-processing (3 source)
4388 * 31 30 29 28 24 23 22 21 20 16 15 14 10 9 5 4 0
4389 * +---+---+---+-----------+------+----+------+----+------+------+------+
4390 * | M | 0 | S | 1 1 1 1 1 | type | o1 | Rm | o0 | Ra | Rn | Rd |
4391 * +---+---+---+-----------+------+----+------+----+------+------+------+
4393 static void disas_fp_3src(DisasContext *s, uint32_t insn)
4395 int type = extract32(insn, 22, 2);
4396 int rd = extract32(insn, 0, 5);
4397 int rn = extract32(insn, 5, 5);
4398 int ra = extract32(insn, 10, 5);
4399 int rm = extract32(insn, 16, 5);
4400 bool o0 = extract32(insn, 15, 1);
4401 bool o1 = extract32(insn, 21, 1);
4403 switch (type) {
4404 case 0:
4405 handle_fp_3src_single(s, o0, o1, rd, rn, rm, ra);
4406 break;
4407 case 1:
4408 handle_fp_3src_double(s, o0, o1, rd, rn, rm, ra);
4409 break;
4410 default:
4411 unallocated_encoding(s);
4415 /* C3.6.28 Floating point immediate
4416 * 31 30 29 28 24 23 22 21 20 13 12 10 9 5 4 0
4417 * +---+---+---+-----------+------+---+------------+-------+------+------+
4418 * | M | 0 | S | 1 1 1 1 0 | type | 1 | imm8 | 1 0 0 | imm5 | Rd |
4419 * +---+---+---+-----------+------+---+------------+-------+------+------+
4421 static void disas_fp_imm(DisasContext *s, uint32_t insn)
4423 int rd = extract32(insn, 0, 5);
4424 int imm8 = extract32(insn, 13, 8);
4425 int is_double = extract32(insn, 22, 2);
4426 uint64_t imm;
4427 TCGv_i64 tcg_res;
4429 if (is_double > 1) {
4430 unallocated_encoding(s);
4431 return;
4434 /* The imm8 encodes the sign bit, enough bits to represent
4435 * an exponent in the range 01....1xx to 10....0xx,
4436 * and the most significant 4 bits of the mantissa; see
4437 * VFPExpandImm() in the v8 ARM ARM.
4439 if (is_double) {
4440 imm = (extract32(imm8, 7, 1) ? 0x8000 : 0) |
4441 (extract32(imm8, 6, 1) ? 0x3fc0 : 0x4000) |
4442 extract32(imm8, 0, 6);
4443 imm <<= 48;
4444 } else {
4445 imm = (extract32(imm8, 7, 1) ? 0x8000 : 0) |
4446 (extract32(imm8, 6, 1) ? 0x3e00 : 0x4000) |
4447 (extract32(imm8, 0, 6) << 3);
4448 imm <<= 16;
4451 tcg_res = tcg_const_i64(imm);
4452 write_fp_dreg(s, rd, tcg_res);
4453 tcg_temp_free_i64(tcg_res);
4456 /* Handle floating point <=> fixed point conversions. Note that we can
4457 * also deal with fp <=> integer conversions as a special case (scale == 64)
4458 * OPTME: consider handling that special case specially or at least skipping
4459 * the call to scalbn in the helpers for zero shifts.
4461 static void handle_fpfpcvt(DisasContext *s, int rd, int rn, int opcode,
4462 bool itof, int rmode, int scale, int sf, int type)
4464 bool is_signed = !(opcode & 1);
4465 bool is_double = type;
4466 TCGv_ptr tcg_fpstatus;
4467 TCGv_i32 tcg_shift;
4469 tcg_fpstatus = get_fpstatus_ptr();
4471 tcg_shift = tcg_const_i32(64 - scale);
4473 if (itof) {
4474 TCGv_i64 tcg_int = cpu_reg(s, rn);
4475 if (!sf) {
4476 TCGv_i64 tcg_extend = new_tmp_a64(s);
4478 if (is_signed) {
4479 tcg_gen_ext32s_i64(tcg_extend, tcg_int);
4480 } else {
4481 tcg_gen_ext32u_i64(tcg_extend, tcg_int);
4484 tcg_int = tcg_extend;
4487 if (is_double) {
4488 TCGv_i64 tcg_double = tcg_temp_new_i64();
4489 if (is_signed) {
4490 gen_helper_vfp_sqtod(tcg_double, tcg_int,
4491 tcg_shift, tcg_fpstatus);
4492 } else {
4493 gen_helper_vfp_uqtod(tcg_double, tcg_int,
4494 tcg_shift, tcg_fpstatus);
4496 write_fp_dreg(s, rd, tcg_double);
4497 tcg_temp_free_i64(tcg_double);
4498 } else {
4499 TCGv_i32 tcg_single = tcg_temp_new_i32();
4500 if (is_signed) {
4501 gen_helper_vfp_sqtos(tcg_single, tcg_int,
4502 tcg_shift, tcg_fpstatus);
4503 } else {
4504 gen_helper_vfp_uqtos(tcg_single, tcg_int,
4505 tcg_shift, tcg_fpstatus);
4507 write_fp_sreg(s, rd, tcg_single);
4508 tcg_temp_free_i32(tcg_single);
4510 } else {
4511 TCGv_i64 tcg_int = cpu_reg(s, rd);
4512 TCGv_i32 tcg_rmode;
4514 if (extract32(opcode, 2, 1)) {
4515 /* There are too many rounding modes to all fit into rmode,
4516 * so FCVTA[US] is a special case.
4518 rmode = FPROUNDING_TIEAWAY;
4521 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rmode));
4523 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
4525 if (is_double) {
4526 TCGv_i64 tcg_double = read_fp_dreg(s, rn);
4527 if (is_signed) {
4528 if (!sf) {
4529 gen_helper_vfp_tosld(tcg_int, tcg_double,
4530 tcg_shift, tcg_fpstatus);
4531 } else {
4532 gen_helper_vfp_tosqd(tcg_int, tcg_double,
4533 tcg_shift, tcg_fpstatus);
4535 } else {
4536 if (!sf) {
4537 gen_helper_vfp_tould(tcg_int, tcg_double,
4538 tcg_shift, tcg_fpstatus);
4539 } else {
4540 gen_helper_vfp_touqd(tcg_int, tcg_double,
4541 tcg_shift, tcg_fpstatus);
4544 tcg_temp_free_i64(tcg_double);
4545 } else {
4546 TCGv_i32 tcg_single = read_fp_sreg(s, rn);
4547 if (sf) {
4548 if (is_signed) {
4549 gen_helper_vfp_tosqs(tcg_int, tcg_single,
4550 tcg_shift, tcg_fpstatus);
4551 } else {
4552 gen_helper_vfp_touqs(tcg_int, tcg_single,
4553 tcg_shift, tcg_fpstatus);
4555 } else {
4556 TCGv_i32 tcg_dest = tcg_temp_new_i32();
4557 if (is_signed) {
4558 gen_helper_vfp_tosls(tcg_dest, tcg_single,
4559 tcg_shift, tcg_fpstatus);
4560 } else {
4561 gen_helper_vfp_touls(tcg_dest, tcg_single,
4562 tcg_shift, tcg_fpstatus);
4564 tcg_gen_extu_i32_i64(tcg_int, tcg_dest);
4565 tcg_temp_free_i32(tcg_dest);
4567 tcg_temp_free_i32(tcg_single);
4570 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
4571 tcg_temp_free_i32(tcg_rmode);
4573 if (!sf) {
4574 tcg_gen_ext32u_i64(tcg_int, tcg_int);
4578 tcg_temp_free_ptr(tcg_fpstatus);
4579 tcg_temp_free_i32(tcg_shift);
4582 /* C3.6.29 Floating point <-> fixed point conversions
4583 * 31 30 29 28 24 23 22 21 20 19 18 16 15 10 9 5 4 0
4584 * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
4585 * | sf | 0 | S | 1 1 1 1 0 | type | 0 | rmode | opcode | scale | Rn | Rd |
4586 * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
4588 static void disas_fp_fixed_conv(DisasContext *s, uint32_t insn)
4590 int rd = extract32(insn, 0, 5);
4591 int rn = extract32(insn, 5, 5);
4592 int scale = extract32(insn, 10, 6);
4593 int opcode = extract32(insn, 16, 3);
4594 int rmode = extract32(insn, 19, 2);
4595 int type = extract32(insn, 22, 2);
4596 bool sbit = extract32(insn, 29, 1);
4597 bool sf = extract32(insn, 31, 1);
4598 bool itof;
4600 if (sbit || (type > 1)
4601 || (!sf && scale < 32)) {
4602 unallocated_encoding(s);
4603 return;
4606 switch ((rmode << 3) | opcode) {
4607 case 0x2: /* SCVTF */
4608 case 0x3: /* UCVTF */
4609 itof = true;
4610 break;
4611 case 0x18: /* FCVTZS */
4612 case 0x19: /* FCVTZU */
4613 itof = false;
4614 break;
4615 default:
4616 unallocated_encoding(s);
4617 return;
4620 handle_fpfpcvt(s, rd, rn, opcode, itof, FPROUNDING_ZERO, scale, sf, type);
4623 static void handle_fmov(DisasContext *s, int rd, int rn, int type, bool itof)
4625 /* FMOV: gpr to or from float, double, or top half of quad fp reg,
4626 * without conversion.
4629 if (itof) {
4630 TCGv_i64 tcg_rn = cpu_reg(s, rn);
4632 switch (type) {
4633 case 0:
4635 /* 32 bit */
4636 TCGv_i64 tmp = tcg_temp_new_i64();
4637 tcg_gen_ext32u_i64(tmp, tcg_rn);
4638 tcg_gen_st_i64(tmp, cpu_env, fp_reg_offset(rd, MO_64));
4639 tcg_gen_movi_i64(tmp, 0);
4640 tcg_gen_st_i64(tmp, cpu_env, fp_reg_hi_offset(rd));
4641 tcg_temp_free_i64(tmp);
4642 break;
4644 case 1:
4646 /* 64 bit */
4647 TCGv_i64 tmp = tcg_const_i64(0);
4648 tcg_gen_st_i64(tcg_rn, cpu_env, fp_reg_offset(rd, MO_64));
4649 tcg_gen_st_i64(tmp, cpu_env, fp_reg_hi_offset(rd));
4650 tcg_temp_free_i64(tmp);
4651 break;
4653 case 2:
4654 /* 64 bit to top half. */
4655 tcg_gen_st_i64(tcg_rn, cpu_env, fp_reg_hi_offset(rd));
4656 break;
4658 } else {
4659 TCGv_i64 tcg_rd = cpu_reg(s, rd);
4661 switch (type) {
4662 case 0:
4663 /* 32 bit */
4664 tcg_gen_ld32u_i64(tcg_rd, cpu_env, fp_reg_offset(rn, MO_32));
4665 break;
4666 case 1:
4667 /* 64 bit */
4668 tcg_gen_ld_i64(tcg_rd, cpu_env, fp_reg_offset(rn, MO_64));
4669 break;
4670 case 2:
4671 /* 64 bits from top half */
4672 tcg_gen_ld_i64(tcg_rd, cpu_env, fp_reg_hi_offset(rn));
4673 break;
4678 /* C3.6.30 Floating point <-> integer conversions
4679 * 31 30 29 28 24 23 22 21 20 19 18 16 15 10 9 5 4 0
4680 * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
4681 * | sf | 0 | S | 1 1 1 1 0 | type | 1 | rmode | opc | 0 0 0 0 0 0 | Rn | Rd |
4682 * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
4684 static void disas_fp_int_conv(DisasContext *s, uint32_t insn)
4686 int rd = extract32(insn, 0, 5);
4687 int rn = extract32(insn, 5, 5);
4688 int opcode = extract32(insn, 16, 3);
4689 int rmode = extract32(insn, 19, 2);
4690 int type = extract32(insn, 22, 2);
4691 bool sbit = extract32(insn, 29, 1);
4692 bool sf = extract32(insn, 31, 1);
4694 if (sbit) {
4695 unallocated_encoding(s);
4696 return;
4699 if (opcode > 5) {
4700 /* FMOV */
4701 bool itof = opcode & 1;
4703 if (rmode >= 2) {
4704 unallocated_encoding(s);
4705 return;
4708 switch (sf << 3 | type << 1 | rmode) {
4709 case 0x0: /* 32 bit */
4710 case 0xa: /* 64 bit */
4711 case 0xd: /* 64 bit to top half of quad */
4712 break;
4713 default:
4714 /* all other sf/type/rmode combinations are invalid */
4715 unallocated_encoding(s);
4716 break;
4719 handle_fmov(s, rd, rn, type, itof);
4720 } else {
4721 /* actual FP conversions */
4722 bool itof = extract32(opcode, 1, 1);
4724 if (type > 1 || (rmode != 0 && opcode > 1)) {
4725 unallocated_encoding(s);
4726 return;
4729 handle_fpfpcvt(s, rd, rn, opcode, itof, rmode, 64, sf, type);
4733 /* FP-specific subcases of table C3-6 (SIMD and FP data processing)
4734 * 31 30 29 28 25 24 0
4735 * +---+---+---+---------+-----------------------------+
4736 * | | 0 | | 1 1 1 1 | |
4737 * +---+---+---+---------+-----------------------------+
4739 static void disas_data_proc_fp(DisasContext *s, uint32_t insn)
4741 if (extract32(insn, 24, 1)) {
4742 /* Floating point data-processing (3 source) */
4743 disas_fp_3src(s, insn);
4744 } else if (extract32(insn, 21, 1) == 0) {
4745 /* Floating point to fixed point conversions */
4746 disas_fp_fixed_conv(s, insn);
4747 } else {
4748 switch (extract32(insn, 10, 2)) {
4749 case 1:
4750 /* Floating point conditional compare */
4751 disas_fp_ccomp(s, insn);
4752 break;
4753 case 2:
4754 /* Floating point data-processing (2 source) */
4755 disas_fp_2src(s, insn);
4756 break;
4757 case 3:
4758 /* Floating point conditional select */
4759 disas_fp_csel(s, insn);
4760 break;
4761 case 0:
4762 switch (ctz32(extract32(insn, 12, 4))) {
4763 case 0: /* [15:12] == xxx1 */
4764 /* Floating point immediate */
4765 disas_fp_imm(s, insn);
4766 break;
4767 case 1: /* [15:12] == xx10 */
4768 /* Floating point compare */
4769 disas_fp_compare(s, insn);
4770 break;
4771 case 2: /* [15:12] == x100 */
4772 /* Floating point data-processing (1 source) */
4773 disas_fp_1src(s, insn);
4774 break;
4775 case 3: /* [15:12] == 1000 */
4776 unallocated_encoding(s);
4777 break;
4778 default: /* [15:12] == 0000 */
4779 /* Floating point <-> integer conversions */
4780 disas_fp_int_conv(s, insn);
4781 break;
4783 break;
4788 static void do_ext64(DisasContext *s, TCGv_i64 tcg_left, TCGv_i64 tcg_right,
4789 int pos)
4791 /* Extract 64 bits from the middle of two concatenated 64 bit
4792 * vector register slices left:right. The extracted bits start
4793 * at 'pos' bits into the right (least significant) side.
4794 * We return the result in tcg_right, and guarantee not to
4795 * trash tcg_left.
4797 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
4798 assert(pos > 0 && pos < 64);
4800 tcg_gen_shri_i64(tcg_right, tcg_right, pos);
4801 tcg_gen_shli_i64(tcg_tmp, tcg_left, 64 - pos);
4802 tcg_gen_or_i64(tcg_right, tcg_right, tcg_tmp);
4804 tcg_temp_free_i64(tcg_tmp);
4807 /* C3.6.1 EXT
4808 * 31 30 29 24 23 22 21 20 16 15 14 11 10 9 5 4 0
4809 * +---+---+-------------+-----+---+------+---+------+---+------+------+
4810 * | 0 | Q | 1 0 1 1 1 0 | op2 | 0 | Rm | 0 | imm4 | 0 | Rn | Rd |
4811 * +---+---+-------------+-----+---+------+---+------+---+------+------+
4813 static void disas_simd_ext(DisasContext *s, uint32_t insn)
4815 int is_q = extract32(insn, 30, 1);
4816 int op2 = extract32(insn, 22, 2);
4817 int imm4 = extract32(insn, 11, 4);
4818 int rm = extract32(insn, 16, 5);
4819 int rn = extract32(insn, 5, 5);
4820 int rd = extract32(insn, 0, 5);
4821 int pos = imm4 << 3;
4822 TCGv_i64 tcg_resl, tcg_resh;
4824 if (op2 != 0 || (!is_q && extract32(imm4, 3, 1))) {
4825 unallocated_encoding(s);
4826 return;
4829 tcg_resh = tcg_temp_new_i64();
4830 tcg_resl = tcg_temp_new_i64();
4832 /* Vd gets bits starting at pos bits into Vm:Vn. This is
4833 * either extracting 128 bits from a 128:128 concatenation, or
4834 * extracting 64 bits from a 64:64 concatenation.
4836 if (!is_q) {
4837 read_vec_element(s, tcg_resl, rn, 0, MO_64);
4838 if (pos != 0) {
4839 read_vec_element(s, tcg_resh, rm, 0, MO_64);
4840 do_ext64(s, tcg_resh, tcg_resl, pos);
4842 tcg_gen_movi_i64(tcg_resh, 0);
4843 } else {
4844 TCGv_i64 tcg_hh;
4845 typedef struct {
4846 int reg;
4847 int elt;
4848 } EltPosns;
4849 EltPosns eltposns[] = { {rn, 0}, {rn, 1}, {rm, 0}, {rm, 1} };
4850 EltPosns *elt = eltposns;
4852 if (pos >= 64) {
4853 elt++;
4854 pos -= 64;
4857 read_vec_element(s, tcg_resl, elt->reg, elt->elt, MO_64);
4858 elt++;
4859 read_vec_element(s, tcg_resh, elt->reg, elt->elt, MO_64);
4860 elt++;
4861 if (pos != 0) {
4862 do_ext64(s, tcg_resh, tcg_resl, pos);
4863 tcg_hh = tcg_temp_new_i64();
4864 read_vec_element(s, tcg_hh, elt->reg, elt->elt, MO_64);
4865 do_ext64(s, tcg_hh, tcg_resh, pos);
4866 tcg_temp_free_i64(tcg_hh);
4870 write_vec_element(s, tcg_resl, rd, 0, MO_64);
4871 tcg_temp_free_i64(tcg_resl);
4872 write_vec_element(s, tcg_resh, rd, 1, MO_64);
4873 tcg_temp_free_i64(tcg_resh);
4876 /* C3.6.2 TBL/TBX
4877 * 31 30 29 24 23 22 21 20 16 15 14 13 12 11 10 9 5 4 0
4878 * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
4879 * | 0 | Q | 0 0 1 1 1 0 | op2 | 0 | Rm | 0 | len | op | 0 0 | Rn | Rd |
4880 * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
4882 static void disas_simd_tb(DisasContext *s, uint32_t insn)
4884 int op2 = extract32(insn, 22, 2);
4885 int is_q = extract32(insn, 30, 1);
4886 int rm = extract32(insn, 16, 5);
4887 int rn = extract32(insn, 5, 5);
4888 int rd = extract32(insn, 0, 5);
4889 int is_tblx = extract32(insn, 12, 1);
4890 int len = extract32(insn, 13, 2);
4891 TCGv_i64 tcg_resl, tcg_resh, tcg_idx;
4892 TCGv_i32 tcg_regno, tcg_numregs;
4894 if (op2 != 0) {
4895 unallocated_encoding(s);
4896 return;
4899 /* This does a table lookup: for every byte element in the input
4900 * we index into a table formed from up to four vector registers,
4901 * and then the output is the result of the lookups. Our helper
4902 * function does the lookup operation for a single 64 bit part of
4903 * the input.
4905 tcg_resl = tcg_temp_new_i64();
4906 tcg_resh = tcg_temp_new_i64();
4908 if (is_tblx) {
4909 read_vec_element(s, tcg_resl, rd, 0, MO_64);
4910 } else {
4911 tcg_gen_movi_i64(tcg_resl, 0);
4913 if (is_tblx && is_q) {
4914 read_vec_element(s, tcg_resh, rd, 1, MO_64);
4915 } else {
4916 tcg_gen_movi_i64(tcg_resh, 0);
4919 tcg_idx = tcg_temp_new_i64();
4920 tcg_regno = tcg_const_i32(rn);
4921 tcg_numregs = tcg_const_i32(len + 1);
4922 read_vec_element(s, tcg_idx, rm, 0, MO_64);
4923 gen_helper_simd_tbl(tcg_resl, cpu_env, tcg_resl, tcg_idx,
4924 tcg_regno, tcg_numregs);
4925 if (is_q) {
4926 read_vec_element(s, tcg_idx, rm, 1, MO_64);
4927 gen_helper_simd_tbl(tcg_resh, cpu_env, tcg_resh, tcg_idx,
4928 tcg_regno, tcg_numregs);
4930 tcg_temp_free_i64(tcg_idx);
4931 tcg_temp_free_i32(tcg_regno);
4932 tcg_temp_free_i32(tcg_numregs);
4934 write_vec_element(s, tcg_resl, rd, 0, MO_64);
4935 tcg_temp_free_i64(tcg_resl);
4936 write_vec_element(s, tcg_resh, rd, 1, MO_64);
4937 tcg_temp_free_i64(tcg_resh);
4940 /* C3.6.3 ZIP/UZP/TRN
4941 * 31 30 29 24 23 22 21 20 16 15 14 12 11 10 9 5 4 0
4942 * +---+---+-------------+------+---+------+---+------------------+------+
4943 * | 0 | Q | 0 0 1 1 1 0 | size | 0 | Rm | 0 | opc | 1 0 | Rn | Rd |
4944 * +---+---+-------------+------+---+------+---+------------------+------+
4946 static void disas_simd_zip_trn(DisasContext *s, uint32_t insn)
4948 int rd = extract32(insn, 0, 5);
4949 int rn = extract32(insn, 5, 5);
4950 int rm = extract32(insn, 16, 5);
4951 int size = extract32(insn, 22, 2);
4952 /* opc field bits [1:0] indicate ZIP/UZP/TRN;
4953 * bit 2 indicates 1 vs 2 variant of the insn.
4955 int opcode = extract32(insn, 12, 2);
4956 bool part = extract32(insn, 14, 1);
4957 bool is_q = extract32(insn, 30, 1);
4958 int esize = 8 << size;
4959 int i, ofs;
4960 int datasize = is_q ? 128 : 64;
4961 int elements = datasize / esize;
4962 TCGv_i64 tcg_res, tcg_resl, tcg_resh;
4964 if (opcode == 0 || (size == 3 && !is_q)) {
4965 unallocated_encoding(s);
4966 return;
4969 tcg_resl = tcg_const_i64(0);
4970 tcg_resh = tcg_const_i64(0);
4971 tcg_res = tcg_temp_new_i64();
4973 for (i = 0; i < elements; i++) {
4974 switch (opcode) {
4975 case 1: /* UZP1/2 */
4977 int midpoint = elements / 2;
4978 if (i < midpoint) {
4979 read_vec_element(s, tcg_res, rn, 2 * i + part, size);
4980 } else {
4981 read_vec_element(s, tcg_res, rm,
4982 2 * (i - midpoint) + part, size);
4984 break;
4986 case 2: /* TRN1/2 */
4987 if (i & 1) {
4988 read_vec_element(s, tcg_res, rm, (i & ~1) + part, size);
4989 } else {
4990 read_vec_element(s, tcg_res, rn, (i & ~1) + part, size);
4992 break;
4993 case 3: /* ZIP1/2 */
4995 int base = part * elements / 2;
4996 if (i & 1) {
4997 read_vec_element(s, tcg_res, rm, base + (i >> 1), size);
4998 } else {
4999 read_vec_element(s, tcg_res, rn, base + (i >> 1), size);
5001 break;
5003 default:
5004 g_assert_not_reached();
5007 ofs = i * esize;
5008 if (ofs < 64) {
5009 tcg_gen_shli_i64(tcg_res, tcg_res, ofs);
5010 tcg_gen_or_i64(tcg_resl, tcg_resl, tcg_res);
5011 } else {
5012 tcg_gen_shli_i64(tcg_res, tcg_res, ofs - 64);
5013 tcg_gen_or_i64(tcg_resh, tcg_resh, tcg_res);
5017 tcg_temp_free_i64(tcg_res);
5019 write_vec_element(s, tcg_resl, rd, 0, MO_64);
5020 tcg_temp_free_i64(tcg_resl);
5021 write_vec_element(s, tcg_resh, rd, 1, MO_64);
5022 tcg_temp_free_i64(tcg_resh);
5025 static void do_minmaxop(DisasContext *s, TCGv_i32 tcg_elt1, TCGv_i32 tcg_elt2,
5026 int opc, bool is_min, TCGv_ptr fpst)
5028 /* Helper function for disas_simd_across_lanes: do a single precision
5029 * min/max operation on the specified two inputs,
5030 * and return the result in tcg_elt1.
5032 if (opc == 0xc) {
5033 if (is_min) {
5034 gen_helper_vfp_minnums(tcg_elt1, tcg_elt1, tcg_elt2, fpst);
5035 } else {
5036 gen_helper_vfp_maxnums(tcg_elt1, tcg_elt1, tcg_elt2, fpst);
5038 } else {
5039 assert(opc == 0xf);
5040 if (is_min) {
5041 gen_helper_vfp_mins(tcg_elt1, tcg_elt1, tcg_elt2, fpst);
5042 } else {
5043 gen_helper_vfp_maxs(tcg_elt1, tcg_elt1, tcg_elt2, fpst);
5048 /* C3.6.4 AdvSIMD across lanes
5049 * 31 30 29 28 24 23 22 21 17 16 12 11 10 9 5 4 0
5050 * +---+---+---+-----------+------+-----------+--------+-----+------+------+
5051 * | 0 | Q | U | 0 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 | Rn | Rd |
5052 * +---+---+---+-----------+------+-----------+--------+-----+------+------+
5054 static void disas_simd_across_lanes(DisasContext *s, uint32_t insn)
5056 int rd = extract32(insn, 0, 5);
5057 int rn = extract32(insn, 5, 5);
5058 int size = extract32(insn, 22, 2);
5059 int opcode = extract32(insn, 12, 5);
5060 bool is_q = extract32(insn, 30, 1);
5061 bool is_u = extract32(insn, 29, 1);
5062 bool is_fp = false;
5063 bool is_min = false;
5064 int esize;
5065 int elements;
5066 int i;
5067 TCGv_i64 tcg_res, tcg_elt;
5069 switch (opcode) {
5070 case 0x1b: /* ADDV */
5071 if (is_u) {
5072 unallocated_encoding(s);
5073 return;
5075 /* fall through */
5076 case 0x3: /* SADDLV, UADDLV */
5077 case 0xa: /* SMAXV, UMAXV */
5078 case 0x1a: /* SMINV, UMINV */
5079 if (size == 3 || (size == 2 && !is_q)) {
5080 unallocated_encoding(s);
5081 return;
5083 break;
5084 case 0xc: /* FMAXNMV, FMINNMV */
5085 case 0xf: /* FMAXV, FMINV */
5086 if (!is_u || !is_q || extract32(size, 0, 1)) {
5087 unallocated_encoding(s);
5088 return;
5090 /* Bit 1 of size field encodes min vs max, and actual size is always
5091 * 32 bits: adjust the size variable so following code can rely on it
5093 is_min = extract32(size, 1, 1);
5094 is_fp = true;
5095 size = 2;
5096 break;
5097 default:
5098 unallocated_encoding(s);
5099 return;
5102 esize = 8 << size;
5103 elements = (is_q ? 128 : 64) / esize;
5105 tcg_res = tcg_temp_new_i64();
5106 tcg_elt = tcg_temp_new_i64();
5108 /* These instructions operate across all lanes of a vector
5109 * to produce a single result. We can guarantee that a 64
5110 * bit intermediate is sufficient:
5111 * + for [US]ADDLV the maximum element size is 32 bits, and
5112 * the result type is 64 bits
5113 * + for FMAX*V, FMIN*V, ADDV the intermediate type is the
5114 * same as the element size, which is 32 bits at most
5115 * For the integer operations we can choose to work at 64
5116 * or 32 bits and truncate at the end; for simplicity
5117 * we use 64 bits always. The floating point
5118 * ops do require 32 bit intermediates, though.
5120 if (!is_fp) {
5121 read_vec_element(s, tcg_res, rn, 0, size | (is_u ? 0 : MO_SIGN));
5123 for (i = 1; i < elements; i++) {
5124 read_vec_element(s, tcg_elt, rn, i, size | (is_u ? 0 : MO_SIGN));
5126 switch (opcode) {
5127 case 0x03: /* SADDLV / UADDLV */
5128 case 0x1b: /* ADDV */
5129 tcg_gen_add_i64(tcg_res, tcg_res, tcg_elt);
5130 break;
5131 case 0x0a: /* SMAXV / UMAXV */
5132 tcg_gen_movcond_i64(is_u ? TCG_COND_GEU : TCG_COND_GE,
5133 tcg_res,
5134 tcg_res, tcg_elt, tcg_res, tcg_elt);
5135 break;
5136 case 0x1a: /* SMINV / UMINV */
5137 tcg_gen_movcond_i64(is_u ? TCG_COND_LEU : TCG_COND_LE,
5138 tcg_res,
5139 tcg_res, tcg_elt, tcg_res, tcg_elt);
5140 break;
5141 break;
5142 default:
5143 g_assert_not_reached();
5147 } else {
5148 /* Floating point ops which work on 32 bit (single) intermediates.
5149 * Note that correct NaN propagation requires that we do these
5150 * operations in exactly the order specified by the pseudocode.
5152 TCGv_i32 tcg_elt1 = tcg_temp_new_i32();
5153 TCGv_i32 tcg_elt2 = tcg_temp_new_i32();
5154 TCGv_i32 tcg_elt3 = tcg_temp_new_i32();
5155 TCGv_ptr fpst = get_fpstatus_ptr();
5157 assert(esize == 32);
5158 assert(elements == 4);
5160 read_vec_element(s, tcg_elt, rn, 0, MO_32);
5161 tcg_gen_trunc_i64_i32(tcg_elt1, tcg_elt);
5162 read_vec_element(s, tcg_elt, rn, 1, MO_32);
5163 tcg_gen_trunc_i64_i32(tcg_elt2, tcg_elt);
5165 do_minmaxop(s, tcg_elt1, tcg_elt2, opcode, is_min, fpst);
5167 read_vec_element(s, tcg_elt, rn, 2, MO_32);
5168 tcg_gen_trunc_i64_i32(tcg_elt2, tcg_elt);
5169 read_vec_element(s, tcg_elt, rn, 3, MO_32);
5170 tcg_gen_trunc_i64_i32(tcg_elt3, tcg_elt);
5172 do_minmaxop(s, tcg_elt2, tcg_elt3, opcode, is_min, fpst);
5174 do_minmaxop(s, tcg_elt1, tcg_elt2, opcode, is_min, fpst);
5176 tcg_gen_extu_i32_i64(tcg_res, tcg_elt1);
5177 tcg_temp_free_i32(tcg_elt1);
5178 tcg_temp_free_i32(tcg_elt2);
5179 tcg_temp_free_i32(tcg_elt3);
5180 tcg_temp_free_ptr(fpst);
5183 tcg_temp_free_i64(tcg_elt);
5185 /* Now truncate the result to the width required for the final output */
5186 if (opcode == 0x03) {
5187 /* SADDLV, UADDLV: result is 2*esize */
5188 size++;
5191 switch (size) {
5192 case 0:
5193 tcg_gen_ext8u_i64(tcg_res, tcg_res);
5194 break;
5195 case 1:
5196 tcg_gen_ext16u_i64(tcg_res, tcg_res);
5197 break;
5198 case 2:
5199 tcg_gen_ext32u_i64(tcg_res, tcg_res);
5200 break;
5201 case 3:
5202 break;
5203 default:
5204 g_assert_not_reached();
5207 write_fp_dreg(s, rd, tcg_res);
5208 tcg_temp_free_i64(tcg_res);
5211 /* C6.3.31 DUP (Element, Vector)
5213 * 31 30 29 21 20 16 15 10 9 5 4 0
5214 * +---+---+-------------------+--------+-------------+------+------+
5215 * | 0 | Q | 0 0 1 1 1 0 0 0 0 | imm5 | 0 0 0 0 0 1 | Rn | Rd |
5216 * +---+---+-------------------+--------+-------------+------+------+
5218 * size: encoded in imm5 (see ARM ARM LowestSetBit())
5220 static void handle_simd_dupe(DisasContext *s, int is_q, int rd, int rn,
5221 int imm5)
5223 int size = ctz32(imm5);
5224 int esize = 8 << size;
5225 int elements = (is_q ? 128 : 64) / esize;
5226 int index, i;
5227 TCGv_i64 tmp;
5229 if (size > 3 || (size == 3 && !is_q)) {
5230 unallocated_encoding(s);
5231 return;
5234 index = imm5 >> (size + 1);
5236 tmp = tcg_temp_new_i64();
5237 read_vec_element(s, tmp, rn, index, size);
5239 for (i = 0; i < elements; i++) {
5240 write_vec_element(s, tmp, rd, i, size);
5243 if (!is_q) {
5244 clear_vec_high(s, rd);
5247 tcg_temp_free_i64(tmp);
5250 /* C6.3.31 DUP (element, scalar)
5251 * 31 21 20 16 15 10 9 5 4 0
5252 * +-----------------------+--------+-------------+------+------+
5253 * | 0 1 0 1 1 1 1 0 0 0 0 | imm5 | 0 0 0 0 0 1 | Rn | Rd |
5254 * +-----------------------+--------+-------------+------+------+
5256 static void handle_simd_dupes(DisasContext *s, int rd, int rn,
5257 int imm5)
5259 int size = ctz32(imm5);
5260 int index;
5261 TCGv_i64 tmp;
5263 if (size > 3) {
5264 unallocated_encoding(s);
5265 return;
5268 index = imm5 >> (size + 1);
5270 /* This instruction just extracts the specified element and
5271 * zero-extends it into the bottom of the destination register.
5273 tmp = tcg_temp_new_i64();
5274 read_vec_element(s, tmp, rn, index, size);
5275 write_fp_dreg(s, rd, tmp);
5276 tcg_temp_free_i64(tmp);
5279 /* C6.3.32 DUP (General)
5281 * 31 30 29 21 20 16 15 10 9 5 4 0
5282 * +---+---+-------------------+--------+-------------+------+------+
5283 * | 0 | Q | 0 0 1 1 1 0 0 0 0 | imm5 | 0 0 0 0 1 1 | Rn | Rd |
5284 * +---+---+-------------------+--------+-------------+------+------+
5286 * size: encoded in imm5 (see ARM ARM LowestSetBit())
5288 static void handle_simd_dupg(DisasContext *s, int is_q, int rd, int rn,
5289 int imm5)
5291 int size = ctz32(imm5);
5292 int esize = 8 << size;
5293 int elements = (is_q ? 128 : 64)/esize;
5294 int i = 0;
5296 if (size > 3 || ((size == 3) && !is_q)) {
5297 unallocated_encoding(s);
5298 return;
5300 for (i = 0; i < elements; i++) {
5301 write_vec_element(s, cpu_reg(s, rn), rd, i, size);
5303 if (!is_q) {
5304 clear_vec_high(s, rd);
5308 /* C6.3.150 INS (Element)
5310 * 31 21 20 16 15 14 11 10 9 5 4 0
5311 * +-----------------------+--------+------------+---+------+------+
5312 * | 0 1 1 0 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 | Rn | Rd |
5313 * +-----------------------+--------+------------+---+------+------+
5315 * size: encoded in imm5 (see ARM ARM LowestSetBit())
5316 * index: encoded in imm5<4:size+1>
5318 static void handle_simd_inse(DisasContext *s, int rd, int rn,
5319 int imm4, int imm5)
5321 int size = ctz32(imm5);
5322 int src_index, dst_index;
5323 TCGv_i64 tmp;
5325 if (size > 3) {
5326 unallocated_encoding(s);
5327 return;
5329 dst_index = extract32(imm5, 1+size, 5);
5330 src_index = extract32(imm4, size, 4);
5332 tmp = tcg_temp_new_i64();
5334 read_vec_element(s, tmp, rn, src_index, size);
5335 write_vec_element(s, tmp, rd, dst_index, size);
5337 tcg_temp_free_i64(tmp);
5341 /* C6.3.151 INS (General)
5343 * 31 21 20 16 15 10 9 5 4 0
5344 * +-----------------------+--------+-------------+------+------+
5345 * | 0 1 0 0 1 1 1 0 0 0 0 | imm5 | 0 0 0 1 1 1 | Rn | Rd |
5346 * +-----------------------+--------+-------------+------+------+
5348 * size: encoded in imm5 (see ARM ARM LowestSetBit())
5349 * index: encoded in imm5<4:size+1>
5351 static void handle_simd_insg(DisasContext *s, int rd, int rn, int imm5)
5353 int size = ctz32(imm5);
5354 int idx;
5356 if (size > 3) {
5357 unallocated_encoding(s);
5358 return;
5361 idx = extract32(imm5, 1 + size, 4 - size);
5362 write_vec_element(s, cpu_reg(s, rn), rd, idx, size);
5366 * C6.3.321 UMOV (General)
5367 * C6.3.237 SMOV (General)
5369 * 31 30 29 21 20 16 15 12 10 9 5 4 0
5370 * +---+---+-------------------+--------+-------------+------+------+
5371 * | 0 | Q | 0 0 1 1 1 0 0 0 0 | imm5 | 0 0 1 U 1 1 | Rn | Rd |
5372 * +---+---+-------------------+--------+-------------+------+------+
5374 * U: unsigned when set
5375 * size: encoded in imm5 (see ARM ARM LowestSetBit())
5377 static void handle_simd_umov_smov(DisasContext *s, int is_q, int is_signed,
5378 int rn, int rd, int imm5)
5380 int size = ctz32(imm5);
5381 int element;
5382 TCGv_i64 tcg_rd;
5384 /* Check for UnallocatedEncodings */
5385 if (is_signed) {
5386 if (size > 2 || (size == 2 && !is_q)) {
5387 unallocated_encoding(s);
5388 return;
5390 } else {
5391 if (size > 3
5392 || (size < 3 && is_q)
5393 || (size == 3 && !is_q)) {
5394 unallocated_encoding(s);
5395 return;
5398 element = extract32(imm5, 1+size, 4);
5400 tcg_rd = cpu_reg(s, rd);
5401 read_vec_element(s, tcg_rd, rn, element, size | (is_signed ? MO_SIGN : 0));
5402 if (is_signed && !is_q) {
5403 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
5407 /* C3.6.5 AdvSIMD copy
5408 * 31 30 29 28 21 20 16 15 14 11 10 9 5 4 0
5409 * +---+---+----+-----------------+------+---+------+---+------+------+
5410 * | 0 | Q | op | 0 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 | Rn | Rd |
5411 * +---+---+----+-----------------+------+---+------+---+------+------+
5413 static void disas_simd_copy(DisasContext *s, uint32_t insn)
5415 int rd = extract32(insn, 0, 5);
5416 int rn = extract32(insn, 5, 5);
5417 int imm4 = extract32(insn, 11, 4);
5418 int op = extract32(insn, 29, 1);
5419 int is_q = extract32(insn, 30, 1);
5420 int imm5 = extract32(insn, 16, 5);
5422 if (op) {
5423 if (is_q) {
5424 /* INS (element) */
5425 handle_simd_inse(s, rd, rn, imm4, imm5);
5426 } else {
5427 unallocated_encoding(s);
5429 } else {
5430 switch (imm4) {
5431 case 0:
5432 /* DUP (element - vector) */
5433 handle_simd_dupe(s, is_q, rd, rn, imm5);
5434 break;
5435 case 1:
5436 /* DUP (general) */
5437 handle_simd_dupg(s, is_q, rd, rn, imm5);
5438 break;
5439 case 3:
5440 if (is_q) {
5441 /* INS (general) */
5442 handle_simd_insg(s, rd, rn, imm5);
5443 } else {
5444 unallocated_encoding(s);
5446 break;
5447 case 5:
5448 case 7:
5449 /* UMOV/SMOV (is_q indicates 32/64; imm4 indicates signedness) */
5450 handle_simd_umov_smov(s, is_q, (imm4 == 5), rn, rd, imm5);
5451 break;
5452 default:
5453 unallocated_encoding(s);
5454 break;
5459 /* C3.6.6 AdvSIMD modified immediate
5460 * 31 30 29 28 19 18 16 15 12 11 10 9 5 4 0
5461 * +---+---+----+---------------------+-----+-------+----+---+-------+------+
5462 * | 0 | Q | op | 0 1 1 1 1 0 0 0 0 0 | abc | cmode | o2 | 1 | defgh | Rd |
5463 * +---+---+----+---------------------+-----+-------+----+---+-------+------+
5465 * There are a number of operations that can be carried out here:
5466 * MOVI - move (shifted) imm into register
5467 * MVNI - move inverted (shifted) imm into register
5468 * ORR - bitwise OR of (shifted) imm with register
5469 * BIC - bitwise clear of (shifted) imm with register
5471 static void disas_simd_mod_imm(DisasContext *s, uint32_t insn)
5473 int rd = extract32(insn, 0, 5);
5474 int cmode = extract32(insn, 12, 4);
5475 int cmode_3_1 = extract32(cmode, 1, 3);
5476 int cmode_0 = extract32(cmode, 0, 1);
5477 int o2 = extract32(insn, 11, 1);
5478 uint64_t abcdefgh = extract32(insn, 5, 5) | (extract32(insn, 16, 3) << 5);
5479 bool is_neg = extract32(insn, 29, 1);
5480 bool is_q = extract32(insn, 30, 1);
5481 uint64_t imm = 0;
5482 TCGv_i64 tcg_rd, tcg_imm;
5483 int i;
5485 if (o2 != 0 || ((cmode == 0xf) && is_neg && !is_q)) {
5486 unallocated_encoding(s);
5487 return;
5490 /* See AdvSIMDExpandImm() in ARM ARM */
5491 switch (cmode_3_1) {
5492 case 0: /* Replicate(Zeros(24):imm8, 2) */
5493 case 1: /* Replicate(Zeros(16):imm8:Zeros(8), 2) */
5494 case 2: /* Replicate(Zeros(8):imm8:Zeros(16), 2) */
5495 case 3: /* Replicate(imm8:Zeros(24), 2) */
5497 int shift = cmode_3_1 * 8;
5498 imm = bitfield_replicate(abcdefgh << shift, 32);
5499 break;
5501 case 4: /* Replicate(Zeros(8):imm8, 4) */
5502 case 5: /* Replicate(imm8:Zeros(8), 4) */
5504 int shift = (cmode_3_1 & 0x1) * 8;
5505 imm = bitfield_replicate(abcdefgh << shift, 16);
5506 break;
5508 case 6:
5509 if (cmode_0) {
5510 /* Replicate(Zeros(8):imm8:Ones(16), 2) */
5511 imm = (abcdefgh << 16) | 0xffff;
5512 } else {
5513 /* Replicate(Zeros(16):imm8:Ones(8), 2) */
5514 imm = (abcdefgh << 8) | 0xff;
5516 imm = bitfield_replicate(imm, 32);
5517 break;
5518 case 7:
5519 if (!cmode_0 && !is_neg) {
5520 imm = bitfield_replicate(abcdefgh, 8);
5521 } else if (!cmode_0 && is_neg) {
5522 int i;
5523 imm = 0;
5524 for (i = 0; i < 8; i++) {
5525 if ((abcdefgh) & (1 << i)) {
5526 imm |= 0xffULL << (i * 8);
5529 } else if (cmode_0) {
5530 if (is_neg) {
5531 imm = (abcdefgh & 0x3f) << 48;
5532 if (abcdefgh & 0x80) {
5533 imm |= 0x8000000000000000ULL;
5535 if (abcdefgh & 0x40) {
5536 imm |= 0x3fc0000000000000ULL;
5537 } else {
5538 imm |= 0x4000000000000000ULL;
5540 } else {
5541 imm = (abcdefgh & 0x3f) << 19;
5542 if (abcdefgh & 0x80) {
5543 imm |= 0x80000000;
5545 if (abcdefgh & 0x40) {
5546 imm |= 0x3e000000;
5547 } else {
5548 imm |= 0x40000000;
5550 imm |= (imm << 32);
5553 break;
5556 if (cmode_3_1 != 7 && is_neg) {
5557 imm = ~imm;
5560 tcg_imm = tcg_const_i64(imm);
5561 tcg_rd = new_tmp_a64(s);
5563 for (i = 0; i < 2; i++) {
5564 int foffs = i ? fp_reg_hi_offset(rd) : fp_reg_offset(rd, MO_64);
5566 if (i == 1 && !is_q) {
5567 /* non-quad ops clear high half of vector */
5568 tcg_gen_movi_i64(tcg_rd, 0);
5569 } else if ((cmode & 0x9) == 0x1 || (cmode & 0xd) == 0x9) {
5570 tcg_gen_ld_i64(tcg_rd, cpu_env, foffs);
5571 if (is_neg) {
5572 /* AND (BIC) */
5573 tcg_gen_and_i64(tcg_rd, tcg_rd, tcg_imm);
5574 } else {
5575 /* ORR */
5576 tcg_gen_or_i64(tcg_rd, tcg_rd, tcg_imm);
5578 } else {
5579 /* MOVI */
5580 tcg_gen_mov_i64(tcg_rd, tcg_imm);
5582 tcg_gen_st_i64(tcg_rd, cpu_env, foffs);
5585 tcg_temp_free_i64(tcg_imm);
5588 /* C3.6.7 AdvSIMD scalar copy
5589 * 31 30 29 28 21 20 16 15 14 11 10 9 5 4 0
5590 * +-----+----+-----------------+------+---+------+---+------+------+
5591 * | 0 1 | op | 1 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 | Rn | Rd |
5592 * +-----+----+-----------------+------+---+------+---+------+------+
5594 static void disas_simd_scalar_copy(DisasContext *s, uint32_t insn)
5596 int rd = extract32(insn, 0, 5);
5597 int rn = extract32(insn, 5, 5);
5598 int imm4 = extract32(insn, 11, 4);
5599 int imm5 = extract32(insn, 16, 5);
5600 int op = extract32(insn, 29, 1);
5602 if (op != 0 || imm4 != 0) {
5603 unallocated_encoding(s);
5604 return;
5607 /* DUP (element, scalar) */
5608 handle_simd_dupes(s, rd, rn, imm5);
5611 /* C3.6.8 AdvSIMD scalar pairwise
5612 * 31 30 29 28 24 23 22 21 17 16 12 11 10 9 5 4 0
5613 * +-----+---+-----------+------+-----------+--------+-----+------+------+
5614 * | 0 1 | U | 1 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 | Rn | Rd |
5615 * +-----+---+-----------+------+-----------+--------+-----+------+------+
5617 static void disas_simd_scalar_pairwise(DisasContext *s, uint32_t insn)
5619 int u = extract32(insn, 29, 1);
5620 int size = extract32(insn, 22, 2);
5621 int opcode = extract32(insn, 12, 5);
5622 int rn = extract32(insn, 5, 5);
5623 int rd = extract32(insn, 0, 5);
5624 TCGv_ptr fpst;
5626 /* For some ops (the FP ones), size[1] is part of the encoding.
5627 * For ADDP strictly it is not but size[1] is always 1 for valid
5628 * encodings.
5630 opcode |= (extract32(size, 1, 1) << 5);
5632 switch (opcode) {
5633 case 0x3b: /* ADDP */
5634 if (u || size != 3) {
5635 unallocated_encoding(s);
5636 return;
5638 TCGV_UNUSED_PTR(fpst);
5639 break;
5640 case 0xc: /* FMAXNMP */
5641 case 0xd: /* FADDP */
5642 case 0xf: /* FMAXP */
5643 case 0x2c: /* FMINNMP */
5644 case 0x2f: /* FMINP */
5645 /* FP op, size[0] is 32 or 64 bit */
5646 if (!u) {
5647 unallocated_encoding(s);
5648 return;
5650 size = extract32(size, 0, 1) ? 3 : 2;
5651 fpst = get_fpstatus_ptr();
5652 break;
5653 default:
5654 unallocated_encoding(s);
5655 return;
5658 if (size == 3) {
5659 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
5660 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
5661 TCGv_i64 tcg_res = tcg_temp_new_i64();
5663 read_vec_element(s, tcg_op1, rn, 0, MO_64);
5664 read_vec_element(s, tcg_op2, rn, 1, MO_64);
5666 switch (opcode) {
5667 case 0x3b: /* ADDP */
5668 tcg_gen_add_i64(tcg_res, tcg_op1, tcg_op2);
5669 break;
5670 case 0xc: /* FMAXNMP */
5671 gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
5672 break;
5673 case 0xd: /* FADDP */
5674 gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
5675 break;
5676 case 0xf: /* FMAXP */
5677 gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
5678 break;
5679 case 0x2c: /* FMINNMP */
5680 gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
5681 break;
5682 case 0x2f: /* FMINP */
5683 gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
5684 break;
5685 default:
5686 g_assert_not_reached();
5689 write_fp_dreg(s, rd, tcg_res);
5691 tcg_temp_free_i64(tcg_op1);
5692 tcg_temp_free_i64(tcg_op2);
5693 tcg_temp_free_i64(tcg_res);
5694 } else {
5695 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
5696 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
5697 TCGv_i32 tcg_res = tcg_temp_new_i32();
5699 read_vec_element_i32(s, tcg_op1, rn, 0, MO_32);
5700 read_vec_element_i32(s, tcg_op2, rn, 1, MO_32);
5702 switch (opcode) {
5703 case 0xc: /* FMAXNMP */
5704 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
5705 break;
5706 case 0xd: /* FADDP */
5707 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
5708 break;
5709 case 0xf: /* FMAXP */
5710 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
5711 break;
5712 case 0x2c: /* FMINNMP */
5713 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
5714 break;
5715 case 0x2f: /* FMINP */
5716 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
5717 break;
5718 default:
5719 g_assert_not_reached();
5722 write_fp_sreg(s, rd, tcg_res);
5724 tcg_temp_free_i32(tcg_op1);
5725 tcg_temp_free_i32(tcg_op2);
5726 tcg_temp_free_i32(tcg_res);
5729 if (!TCGV_IS_UNUSED_PTR(fpst)) {
5730 tcg_temp_free_ptr(fpst);
5735 * Common SSHR[RA]/USHR[RA] - Shift right (optional rounding/accumulate)
5737 * This code is handles the common shifting code and is used by both
5738 * the vector and scalar code.
5740 static void handle_shri_with_rndacc(TCGv_i64 tcg_res, TCGv_i64 tcg_src,
5741 TCGv_i64 tcg_rnd, bool accumulate,
5742 bool is_u, int size, int shift)
5744 bool extended_result = false;
5745 bool round = !TCGV_IS_UNUSED_I64(tcg_rnd);
5746 int ext_lshift = 0;
5747 TCGv_i64 tcg_src_hi;
5749 if (round && size == 3) {
5750 extended_result = true;
5751 ext_lshift = 64 - shift;
5752 tcg_src_hi = tcg_temp_new_i64();
5753 } else if (shift == 64) {
5754 if (!accumulate && is_u) {
5755 /* result is zero */
5756 tcg_gen_movi_i64(tcg_res, 0);
5757 return;
5761 /* Deal with the rounding step */
5762 if (round) {
5763 if (extended_result) {
5764 TCGv_i64 tcg_zero = tcg_const_i64(0);
5765 if (!is_u) {
5766 /* take care of sign extending tcg_res */
5767 tcg_gen_sari_i64(tcg_src_hi, tcg_src, 63);
5768 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
5769 tcg_src, tcg_src_hi,
5770 tcg_rnd, tcg_zero);
5771 } else {
5772 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
5773 tcg_src, tcg_zero,
5774 tcg_rnd, tcg_zero);
5776 tcg_temp_free_i64(tcg_zero);
5777 } else {
5778 tcg_gen_add_i64(tcg_src, tcg_src, tcg_rnd);
5782 /* Now do the shift right */
5783 if (round && extended_result) {
5784 /* extended case, >64 bit precision required */
5785 if (ext_lshift == 0) {
5786 /* special case, only high bits matter */
5787 tcg_gen_mov_i64(tcg_src, tcg_src_hi);
5788 } else {
5789 tcg_gen_shri_i64(tcg_src, tcg_src, shift);
5790 tcg_gen_shli_i64(tcg_src_hi, tcg_src_hi, ext_lshift);
5791 tcg_gen_or_i64(tcg_src, tcg_src, tcg_src_hi);
5793 } else {
5794 if (is_u) {
5795 if (shift == 64) {
5796 /* essentially shifting in 64 zeros */
5797 tcg_gen_movi_i64(tcg_src, 0);
5798 } else {
5799 tcg_gen_shri_i64(tcg_src, tcg_src, shift);
5801 } else {
5802 if (shift == 64) {
5803 /* effectively extending the sign-bit */
5804 tcg_gen_sari_i64(tcg_src, tcg_src, 63);
5805 } else {
5806 tcg_gen_sari_i64(tcg_src, tcg_src, shift);
5811 if (accumulate) {
5812 tcg_gen_add_i64(tcg_res, tcg_res, tcg_src);
5813 } else {
5814 tcg_gen_mov_i64(tcg_res, tcg_src);
5817 if (extended_result) {
5818 tcg_temp_free_i64(tcg_src_hi);
5822 /* Common SHL/SLI - Shift left with an optional insert */
5823 static void handle_shli_with_ins(TCGv_i64 tcg_res, TCGv_i64 tcg_src,
5824 bool insert, int shift)
5826 if (insert) { /* SLI */
5827 tcg_gen_deposit_i64(tcg_res, tcg_res, tcg_src, shift, 64 - shift);
5828 } else { /* SHL */
5829 tcg_gen_shli_i64(tcg_res, tcg_src, shift);
5833 /* SRI: shift right with insert */
5834 static void handle_shri_with_ins(TCGv_i64 tcg_res, TCGv_i64 tcg_src,
5835 int size, int shift)
5837 int esize = 8 << size;
5839 /* shift count same as element size is valid but does nothing;
5840 * special case to avoid potential shift by 64.
5842 if (shift != esize) {
5843 tcg_gen_shri_i64(tcg_src, tcg_src, shift);
5844 tcg_gen_deposit_i64(tcg_res, tcg_res, tcg_src, 0, esize - shift);
5848 /* SSHR[RA]/USHR[RA] - Scalar shift right (optional rounding/accumulate) */
5849 static void handle_scalar_simd_shri(DisasContext *s,
5850 bool is_u, int immh, int immb,
5851 int opcode, int rn, int rd)
5853 const int size = 3;
5854 int immhb = immh << 3 | immb;
5855 int shift = 2 * (8 << size) - immhb;
5856 bool accumulate = false;
5857 bool round = false;
5858 bool insert = false;
5859 TCGv_i64 tcg_rn;
5860 TCGv_i64 tcg_rd;
5861 TCGv_i64 tcg_round;
5863 if (!extract32(immh, 3, 1)) {
5864 unallocated_encoding(s);
5865 return;
5868 switch (opcode) {
5869 case 0x02: /* SSRA / USRA (accumulate) */
5870 accumulate = true;
5871 break;
5872 case 0x04: /* SRSHR / URSHR (rounding) */
5873 round = true;
5874 break;
5875 case 0x06: /* SRSRA / URSRA (accum + rounding) */
5876 accumulate = round = true;
5877 break;
5878 case 0x08: /* SRI */
5879 insert = true;
5880 break;
5883 if (round) {
5884 uint64_t round_const = 1ULL << (shift - 1);
5885 tcg_round = tcg_const_i64(round_const);
5886 } else {
5887 TCGV_UNUSED_I64(tcg_round);
5890 tcg_rn = read_fp_dreg(s, rn);
5891 tcg_rd = (accumulate || insert) ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
5893 if (insert) {
5894 handle_shri_with_ins(tcg_rd, tcg_rn, size, shift);
5895 } else {
5896 handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
5897 accumulate, is_u, size, shift);
5900 write_fp_dreg(s, rd, tcg_rd);
5902 tcg_temp_free_i64(tcg_rn);
5903 tcg_temp_free_i64(tcg_rd);
5904 if (round) {
5905 tcg_temp_free_i64(tcg_round);
5909 /* SHL/SLI - Scalar shift left */
5910 static void handle_scalar_simd_shli(DisasContext *s, bool insert,
5911 int immh, int immb, int opcode,
5912 int rn, int rd)
5914 int size = 32 - clz32(immh) - 1;
5915 int immhb = immh << 3 | immb;
5916 int shift = immhb - (8 << size);
5917 TCGv_i64 tcg_rn = new_tmp_a64(s);
5918 TCGv_i64 tcg_rd = new_tmp_a64(s);
5920 if (!extract32(immh, 3, 1)) {
5921 unallocated_encoding(s);
5922 return;
5925 tcg_rn = read_fp_dreg(s, rn);
5926 tcg_rd = insert ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
5928 handle_shli_with_ins(tcg_rd, tcg_rn, insert, shift);
5930 write_fp_dreg(s, rd, tcg_rd);
5932 tcg_temp_free_i64(tcg_rn);
5933 tcg_temp_free_i64(tcg_rd);
5936 /* SQSHRN/SQSHRUN - Saturating (signed/unsigned) shift right with
5937 * (signed/unsigned) narrowing */
5938 static void handle_vec_simd_sqshrn(DisasContext *s, bool is_scalar, bool is_q,
5939 bool is_u_shift, bool is_u_narrow,
5940 int immh, int immb, int opcode,
5941 int rn, int rd)
5943 int immhb = immh << 3 | immb;
5944 int size = 32 - clz32(immh) - 1;
5945 int esize = 8 << size;
5946 int shift = (2 * esize) - immhb;
5947 int elements = is_scalar ? 1 : (64 / esize);
5948 bool round = extract32(opcode, 0, 1);
5949 TCGMemOp ldop = (size + 1) | (is_u_shift ? 0 : MO_SIGN);
5950 TCGv_i64 tcg_rn, tcg_rd, tcg_round;
5951 TCGv_i32 tcg_rd_narrowed;
5952 TCGv_i64 tcg_final;
5954 static NeonGenNarrowEnvFn * const signed_narrow_fns[4][2] = {
5955 { gen_helper_neon_narrow_sat_s8,
5956 gen_helper_neon_unarrow_sat8 },
5957 { gen_helper_neon_narrow_sat_s16,
5958 gen_helper_neon_unarrow_sat16 },
5959 { gen_helper_neon_narrow_sat_s32,
5960 gen_helper_neon_unarrow_sat32 },
5961 { NULL, NULL },
5963 static NeonGenNarrowEnvFn * const unsigned_narrow_fns[4] = {
5964 gen_helper_neon_narrow_sat_u8,
5965 gen_helper_neon_narrow_sat_u16,
5966 gen_helper_neon_narrow_sat_u32,
5967 NULL
5969 NeonGenNarrowEnvFn *narrowfn;
5971 int i;
5973 assert(size < 4);
5975 if (extract32(immh, 3, 1)) {
5976 unallocated_encoding(s);
5977 return;
5980 if (is_u_shift) {
5981 narrowfn = unsigned_narrow_fns[size];
5982 } else {
5983 narrowfn = signed_narrow_fns[size][is_u_narrow ? 1 : 0];
5986 tcg_rn = tcg_temp_new_i64();
5987 tcg_rd = tcg_temp_new_i64();
5988 tcg_rd_narrowed = tcg_temp_new_i32();
5989 tcg_final = tcg_const_i64(0);
5991 if (round) {
5992 uint64_t round_const = 1ULL << (shift - 1);
5993 tcg_round = tcg_const_i64(round_const);
5994 } else {
5995 TCGV_UNUSED_I64(tcg_round);
5998 for (i = 0; i < elements; i++) {
5999 read_vec_element(s, tcg_rn, rn, i, ldop);
6000 handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
6001 false, is_u_shift, size+1, shift);
6002 narrowfn(tcg_rd_narrowed, cpu_env, tcg_rd);
6003 tcg_gen_extu_i32_i64(tcg_rd, tcg_rd_narrowed);
6004 tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
6007 if (!is_q) {
6008 clear_vec_high(s, rd);
6009 write_vec_element(s, tcg_final, rd, 0, MO_64);
6010 } else {
6011 write_vec_element(s, tcg_final, rd, 1, MO_64);
6014 if (round) {
6015 tcg_temp_free_i64(tcg_round);
6017 tcg_temp_free_i64(tcg_rn);
6018 tcg_temp_free_i64(tcg_rd);
6019 tcg_temp_free_i32(tcg_rd_narrowed);
6020 tcg_temp_free_i64(tcg_final);
6021 return;
6024 /* SQSHLU, UQSHL, SQSHL: saturating left shifts */
6025 static void handle_simd_qshl(DisasContext *s, bool scalar, bool is_q,
6026 bool src_unsigned, bool dst_unsigned,
6027 int immh, int immb, int rn, int rd)
6029 int immhb = immh << 3 | immb;
6030 int size = 32 - clz32(immh) - 1;
6031 int shift = immhb - (8 << size);
6032 int pass;
6034 assert(immh != 0);
6035 assert(!(scalar && is_q));
6037 if (!scalar) {
6038 if (!is_q && extract32(immh, 3, 1)) {
6039 unallocated_encoding(s);
6040 return;
6043 /* Since we use the variable-shift helpers we must
6044 * replicate the shift count into each element of
6045 * the tcg_shift value.
6047 switch (size) {
6048 case 0:
6049 shift |= shift << 8;
6050 /* fall through */
6051 case 1:
6052 shift |= shift << 16;
6053 break;
6054 case 2:
6055 case 3:
6056 break;
6057 default:
6058 g_assert_not_reached();
6062 if (size == 3) {
6063 TCGv_i64 tcg_shift = tcg_const_i64(shift);
6064 static NeonGenTwo64OpEnvFn * const fns[2][2] = {
6065 { gen_helper_neon_qshl_s64, gen_helper_neon_qshlu_s64 },
6066 { NULL, gen_helper_neon_qshl_u64 },
6068 NeonGenTwo64OpEnvFn *genfn = fns[src_unsigned][dst_unsigned];
6069 int maxpass = is_q ? 2 : 1;
6071 for (pass = 0; pass < maxpass; pass++) {
6072 TCGv_i64 tcg_op = tcg_temp_new_i64();
6074 read_vec_element(s, tcg_op, rn, pass, MO_64);
6075 genfn(tcg_op, cpu_env, tcg_op, tcg_shift);
6076 write_vec_element(s, tcg_op, rd, pass, MO_64);
6078 tcg_temp_free_i64(tcg_op);
6080 tcg_temp_free_i64(tcg_shift);
6082 if (!is_q) {
6083 clear_vec_high(s, rd);
6085 } else {
6086 TCGv_i32 tcg_shift = tcg_const_i32(shift);
6087 static NeonGenTwoOpEnvFn * const fns[2][2][3] = {
6089 { gen_helper_neon_qshl_s8,
6090 gen_helper_neon_qshl_s16,
6091 gen_helper_neon_qshl_s32 },
6092 { gen_helper_neon_qshlu_s8,
6093 gen_helper_neon_qshlu_s16,
6094 gen_helper_neon_qshlu_s32 }
6095 }, {
6096 { NULL, NULL, NULL },
6097 { gen_helper_neon_qshl_u8,
6098 gen_helper_neon_qshl_u16,
6099 gen_helper_neon_qshl_u32 }
6102 NeonGenTwoOpEnvFn *genfn = fns[src_unsigned][dst_unsigned][size];
6103 TCGMemOp memop = scalar ? size : MO_32;
6104 int maxpass = scalar ? 1 : is_q ? 4 : 2;
6106 for (pass = 0; pass < maxpass; pass++) {
6107 TCGv_i32 tcg_op = tcg_temp_new_i32();
6109 read_vec_element_i32(s, tcg_op, rn, pass, memop);
6110 genfn(tcg_op, cpu_env, tcg_op, tcg_shift);
6111 if (scalar) {
6112 switch (size) {
6113 case 0:
6114 tcg_gen_ext8u_i32(tcg_op, tcg_op);
6115 break;
6116 case 1:
6117 tcg_gen_ext16u_i32(tcg_op, tcg_op);
6118 break;
6119 case 2:
6120 break;
6121 default:
6122 g_assert_not_reached();
6124 write_fp_sreg(s, rd, tcg_op);
6125 } else {
6126 write_vec_element_i32(s, tcg_op, rd, pass, MO_32);
6129 tcg_temp_free_i32(tcg_op);
6131 tcg_temp_free_i32(tcg_shift);
6133 if (!is_q && !scalar) {
6134 clear_vec_high(s, rd);
6139 /* Common vector code for handling integer to FP conversion */
6140 static void handle_simd_intfp_conv(DisasContext *s, int rd, int rn,
6141 int elements, int is_signed,
6142 int fracbits, int size)
6144 bool is_double = size == 3 ? true : false;
6145 TCGv_ptr tcg_fpst = get_fpstatus_ptr();
6146 TCGv_i32 tcg_shift = tcg_const_i32(fracbits);
6147 TCGv_i64 tcg_int = tcg_temp_new_i64();
6148 TCGMemOp mop = size | (is_signed ? MO_SIGN : 0);
6149 int pass;
6151 for (pass = 0; pass < elements; pass++) {
6152 read_vec_element(s, tcg_int, rn, pass, mop);
6154 if (is_double) {
6155 TCGv_i64 tcg_double = tcg_temp_new_i64();
6156 if (is_signed) {
6157 gen_helper_vfp_sqtod(tcg_double, tcg_int,
6158 tcg_shift, tcg_fpst);
6159 } else {
6160 gen_helper_vfp_uqtod(tcg_double, tcg_int,
6161 tcg_shift, tcg_fpst);
6163 if (elements == 1) {
6164 write_fp_dreg(s, rd, tcg_double);
6165 } else {
6166 write_vec_element(s, tcg_double, rd, pass, MO_64);
6168 tcg_temp_free_i64(tcg_double);
6169 } else {
6170 TCGv_i32 tcg_single = tcg_temp_new_i32();
6171 if (is_signed) {
6172 gen_helper_vfp_sqtos(tcg_single, tcg_int,
6173 tcg_shift, tcg_fpst);
6174 } else {
6175 gen_helper_vfp_uqtos(tcg_single, tcg_int,
6176 tcg_shift, tcg_fpst);
6178 if (elements == 1) {
6179 write_fp_sreg(s, rd, tcg_single);
6180 } else {
6181 write_vec_element_i32(s, tcg_single, rd, pass, MO_32);
6183 tcg_temp_free_i32(tcg_single);
6187 if (!is_double && elements == 2) {
6188 clear_vec_high(s, rd);
6191 tcg_temp_free_i64(tcg_int);
6192 tcg_temp_free_ptr(tcg_fpst);
6193 tcg_temp_free_i32(tcg_shift);
6196 /* UCVTF/SCVTF - Integer to FP conversion */
6197 static void handle_simd_shift_intfp_conv(DisasContext *s, bool is_scalar,
6198 bool is_q, bool is_u,
6199 int immh, int immb, int opcode,
6200 int rn, int rd)
6202 bool is_double = extract32(immh, 3, 1);
6203 int size = is_double ? MO_64 : MO_32;
6204 int elements;
6205 int immhb = immh << 3 | immb;
6206 int fracbits = (is_double ? 128 : 64) - immhb;
6208 if (!extract32(immh, 2, 2)) {
6209 unallocated_encoding(s);
6210 return;
6213 if (is_scalar) {
6214 elements = 1;
6215 } else {
6216 elements = is_double ? 2 : is_q ? 4 : 2;
6217 if (is_double && !is_q) {
6218 unallocated_encoding(s);
6219 return;
6222 /* immh == 0 would be a failure of the decode logic */
6223 g_assert(immh);
6225 handle_simd_intfp_conv(s, rd, rn, elements, !is_u, fracbits, size);
6228 /* FCVTZS, FVCVTZU - FP to fixedpoint conversion */
6229 static void handle_simd_shift_fpint_conv(DisasContext *s, bool is_scalar,
6230 bool is_q, bool is_u,
6231 int immh, int immb, int rn, int rd)
6233 bool is_double = extract32(immh, 3, 1);
6234 int immhb = immh << 3 | immb;
6235 int fracbits = (is_double ? 128 : 64) - immhb;
6236 int pass;
6237 TCGv_ptr tcg_fpstatus;
6238 TCGv_i32 tcg_rmode, tcg_shift;
6240 if (!extract32(immh, 2, 2)) {
6241 unallocated_encoding(s);
6242 return;
6245 if (!is_scalar && !is_q && is_double) {
6246 unallocated_encoding(s);
6247 return;
6250 assert(!(is_scalar && is_q));
6252 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(FPROUNDING_ZERO));
6253 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
6254 tcg_fpstatus = get_fpstatus_ptr();
6255 tcg_shift = tcg_const_i32(fracbits);
6257 if (is_double) {
6258 int maxpass = is_scalar ? 1 : is_q ? 2 : 1;
6260 for (pass = 0; pass < maxpass; pass++) {
6261 TCGv_i64 tcg_op = tcg_temp_new_i64();
6263 read_vec_element(s, tcg_op, rn, pass, MO_64);
6264 if (is_u) {
6265 gen_helper_vfp_touqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
6266 } else {
6267 gen_helper_vfp_tosqd(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
6269 write_vec_element(s, tcg_op, rd, pass, MO_64);
6270 tcg_temp_free_i64(tcg_op);
6272 if (!is_q) {
6273 clear_vec_high(s, rd);
6275 } else {
6276 int maxpass = is_scalar ? 1 : is_q ? 4 : 2;
6277 for (pass = 0; pass < maxpass; pass++) {
6278 TCGv_i32 tcg_op = tcg_temp_new_i32();
6280 read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
6281 if (is_u) {
6282 gen_helper_vfp_touls(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
6283 } else {
6284 gen_helper_vfp_tosls(tcg_op, tcg_op, tcg_shift, tcg_fpstatus);
6286 if (is_scalar) {
6287 write_fp_sreg(s, rd, tcg_op);
6288 } else {
6289 write_vec_element_i32(s, tcg_op, rd, pass, MO_32);
6291 tcg_temp_free_i32(tcg_op);
6293 if (!is_q && !is_scalar) {
6294 clear_vec_high(s, rd);
6298 tcg_temp_free_ptr(tcg_fpstatus);
6299 tcg_temp_free_i32(tcg_shift);
6300 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
6301 tcg_temp_free_i32(tcg_rmode);
6304 /* C3.6.9 AdvSIMD scalar shift by immediate
6305 * 31 30 29 28 23 22 19 18 16 15 11 10 9 5 4 0
6306 * +-----+---+-------------+------+------+--------+---+------+------+
6307 * | 0 1 | U | 1 1 1 1 1 0 | immh | immb | opcode | 1 | Rn | Rd |
6308 * +-----+---+-------------+------+------+--------+---+------+------+
6310 * This is the scalar version so it works on a fixed sized registers
6312 static void disas_simd_scalar_shift_imm(DisasContext *s, uint32_t insn)
6314 int rd = extract32(insn, 0, 5);
6315 int rn = extract32(insn, 5, 5);
6316 int opcode = extract32(insn, 11, 5);
6317 int immb = extract32(insn, 16, 3);
6318 int immh = extract32(insn, 19, 4);
6319 bool is_u = extract32(insn, 29, 1);
6321 if (immh == 0) {
6322 unallocated_encoding(s);
6323 return;
6326 switch (opcode) {
6327 case 0x08: /* SRI */
6328 if (!is_u) {
6329 unallocated_encoding(s);
6330 return;
6332 /* fall through */
6333 case 0x00: /* SSHR / USHR */
6334 case 0x02: /* SSRA / USRA */
6335 case 0x04: /* SRSHR / URSHR */
6336 case 0x06: /* SRSRA / URSRA */
6337 handle_scalar_simd_shri(s, is_u, immh, immb, opcode, rn, rd);
6338 break;
6339 case 0x0a: /* SHL / SLI */
6340 handle_scalar_simd_shli(s, is_u, immh, immb, opcode, rn, rd);
6341 break;
6342 case 0x1c: /* SCVTF, UCVTF */
6343 handle_simd_shift_intfp_conv(s, true, false, is_u, immh, immb,
6344 opcode, rn, rd);
6345 break;
6346 case 0x10: /* SQSHRUN, SQSHRUN2 */
6347 case 0x11: /* SQRSHRUN, SQRSHRUN2 */
6348 if (!is_u) {
6349 unallocated_encoding(s);
6350 return;
6352 handle_vec_simd_sqshrn(s, true, false, false, true,
6353 immh, immb, opcode, rn, rd);
6354 break;
6355 case 0x12: /* SQSHRN, SQSHRN2, UQSHRN */
6356 case 0x13: /* SQRSHRN, SQRSHRN2, UQRSHRN, UQRSHRN2 */
6357 handle_vec_simd_sqshrn(s, true, false, is_u, is_u,
6358 immh, immb, opcode, rn, rd);
6359 break;
6360 case 0xc: /* SQSHLU */
6361 if (!is_u) {
6362 unallocated_encoding(s);
6363 return;
6365 handle_simd_qshl(s, true, false, false, true, immh, immb, rn, rd);
6366 break;
6367 case 0xe: /* SQSHL, UQSHL */
6368 handle_simd_qshl(s, true, false, is_u, is_u, immh, immb, rn, rd);
6369 break;
6370 case 0x1f: /* FCVTZS, FCVTZU */
6371 handle_simd_shift_fpint_conv(s, true, false, is_u, immh, immb, rn, rd);
6372 break;
6373 default:
6374 unallocated_encoding(s);
6375 break;
6379 /* C3.6.10 AdvSIMD scalar three different
6380 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 0
6381 * +-----+---+-----------+------+---+------+--------+-----+------+------+
6382 * | 0 1 | U | 1 1 1 1 0 | size | 1 | Rm | opcode | 0 0 | Rn | Rd |
6383 * +-----+---+-----------+------+---+------+--------+-----+------+------+
6385 static void disas_simd_scalar_three_reg_diff(DisasContext *s, uint32_t insn)
6387 bool is_u = extract32(insn, 29, 1);
6388 int size = extract32(insn, 22, 2);
6389 int opcode = extract32(insn, 12, 4);
6390 int rm = extract32(insn, 16, 5);
6391 int rn = extract32(insn, 5, 5);
6392 int rd = extract32(insn, 0, 5);
6394 if (is_u) {
6395 unallocated_encoding(s);
6396 return;
6399 switch (opcode) {
6400 case 0x9: /* SQDMLAL, SQDMLAL2 */
6401 case 0xb: /* SQDMLSL, SQDMLSL2 */
6402 case 0xd: /* SQDMULL, SQDMULL2 */
6403 if (size == 0 || size == 3) {
6404 unallocated_encoding(s);
6405 return;
6407 break;
6408 default:
6409 unallocated_encoding(s);
6410 return;
6413 if (size == 2) {
6414 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
6415 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
6416 TCGv_i64 tcg_res = tcg_temp_new_i64();
6418 read_vec_element(s, tcg_op1, rn, 0, MO_32 | MO_SIGN);
6419 read_vec_element(s, tcg_op2, rm, 0, MO_32 | MO_SIGN);
6421 tcg_gen_mul_i64(tcg_res, tcg_op1, tcg_op2);
6422 gen_helper_neon_addl_saturate_s64(tcg_res, cpu_env, tcg_res, tcg_res);
6424 switch (opcode) {
6425 case 0xd: /* SQDMULL, SQDMULL2 */
6426 break;
6427 case 0xb: /* SQDMLSL, SQDMLSL2 */
6428 tcg_gen_neg_i64(tcg_res, tcg_res);
6429 /* fall through */
6430 case 0x9: /* SQDMLAL, SQDMLAL2 */
6431 read_vec_element(s, tcg_op1, rd, 0, MO_64);
6432 gen_helper_neon_addl_saturate_s64(tcg_res, cpu_env,
6433 tcg_res, tcg_op1);
6434 break;
6435 default:
6436 g_assert_not_reached();
6439 write_fp_dreg(s, rd, tcg_res);
6441 tcg_temp_free_i64(tcg_op1);
6442 tcg_temp_free_i64(tcg_op2);
6443 tcg_temp_free_i64(tcg_res);
6444 } else {
6445 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
6446 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
6447 TCGv_i64 tcg_res = tcg_temp_new_i64();
6449 read_vec_element_i32(s, tcg_op1, rn, 0, MO_16);
6450 read_vec_element_i32(s, tcg_op2, rm, 0, MO_16);
6452 gen_helper_neon_mull_s16(tcg_res, tcg_op1, tcg_op2);
6453 gen_helper_neon_addl_saturate_s32(tcg_res, cpu_env, tcg_res, tcg_res);
6455 switch (opcode) {
6456 case 0xd: /* SQDMULL, SQDMULL2 */
6457 break;
6458 case 0xb: /* SQDMLSL, SQDMLSL2 */
6459 gen_helper_neon_negl_u32(tcg_res, tcg_res);
6460 /* fall through */
6461 case 0x9: /* SQDMLAL, SQDMLAL2 */
6463 TCGv_i64 tcg_op3 = tcg_temp_new_i64();
6464 read_vec_element(s, tcg_op3, rd, 0, MO_32);
6465 gen_helper_neon_addl_saturate_s32(tcg_res, cpu_env,
6466 tcg_res, tcg_op3);
6467 tcg_temp_free_i64(tcg_op3);
6468 break;
6470 default:
6471 g_assert_not_reached();
6474 tcg_gen_ext32u_i64(tcg_res, tcg_res);
6475 write_fp_dreg(s, rd, tcg_res);
6477 tcg_temp_free_i32(tcg_op1);
6478 tcg_temp_free_i32(tcg_op2);
6479 tcg_temp_free_i64(tcg_res);
6483 static void handle_3same_64(DisasContext *s, int opcode, bool u,
6484 TCGv_i64 tcg_rd, TCGv_i64 tcg_rn, TCGv_i64 tcg_rm)
6486 /* Handle 64x64->64 opcodes which are shared between the scalar
6487 * and vector 3-same groups. We cover every opcode where size == 3
6488 * is valid in either the three-reg-same (integer, not pairwise)
6489 * or scalar-three-reg-same groups. (Some opcodes are not yet
6490 * implemented.)
6492 TCGCond cond;
6494 switch (opcode) {
6495 case 0x1: /* SQADD */
6496 if (u) {
6497 gen_helper_neon_qadd_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
6498 } else {
6499 gen_helper_neon_qadd_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
6501 break;
6502 case 0x5: /* SQSUB */
6503 if (u) {
6504 gen_helper_neon_qsub_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
6505 } else {
6506 gen_helper_neon_qsub_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
6508 break;
6509 case 0x6: /* CMGT, CMHI */
6510 /* 64 bit integer comparison, result = test ? (2^64 - 1) : 0.
6511 * We implement this using setcond (test) and then negating.
6513 cond = u ? TCG_COND_GTU : TCG_COND_GT;
6514 do_cmop:
6515 tcg_gen_setcond_i64(cond, tcg_rd, tcg_rn, tcg_rm);
6516 tcg_gen_neg_i64(tcg_rd, tcg_rd);
6517 break;
6518 case 0x7: /* CMGE, CMHS */
6519 cond = u ? TCG_COND_GEU : TCG_COND_GE;
6520 goto do_cmop;
6521 case 0x11: /* CMTST, CMEQ */
6522 if (u) {
6523 cond = TCG_COND_EQ;
6524 goto do_cmop;
6526 /* CMTST : test is "if (X & Y != 0)". */
6527 tcg_gen_and_i64(tcg_rd, tcg_rn, tcg_rm);
6528 tcg_gen_setcondi_i64(TCG_COND_NE, tcg_rd, tcg_rd, 0);
6529 tcg_gen_neg_i64(tcg_rd, tcg_rd);
6530 break;
6531 case 0x8: /* SSHL, USHL */
6532 if (u) {
6533 gen_helper_neon_shl_u64(tcg_rd, tcg_rn, tcg_rm);
6534 } else {
6535 gen_helper_neon_shl_s64(tcg_rd, tcg_rn, tcg_rm);
6537 break;
6538 case 0x9: /* SQSHL, UQSHL */
6539 if (u) {
6540 gen_helper_neon_qshl_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
6541 } else {
6542 gen_helper_neon_qshl_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
6544 break;
6545 case 0xa: /* SRSHL, URSHL */
6546 if (u) {
6547 gen_helper_neon_rshl_u64(tcg_rd, tcg_rn, tcg_rm);
6548 } else {
6549 gen_helper_neon_rshl_s64(tcg_rd, tcg_rn, tcg_rm);
6551 break;
6552 case 0xb: /* SQRSHL, UQRSHL */
6553 if (u) {
6554 gen_helper_neon_qrshl_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
6555 } else {
6556 gen_helper_neon_qrshl_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
6558 break;
6559 case 0x10: /* ADD, SUB */
6560 if (u) {
6561 tcg_gen_sub_i64(tcg_rd, tcg_rn, tcg_rm);
6562 } else {
6563 tcg_gen_add_i64(tcg_rd, tcg_rn, tcg_rm);
6565 break;
6566 default:
6567 g_assert_not_reached();
6571 /* Handle the 3-same-operands float operations; shared by the scalar
6572 * and vector encodings. The caller must filter out any encodings
6573 * not allocated for the encoding it is dealing with.
6575 static void handle_3same_float(DisasContext *s, int size, int elements,
6576 int fpopcode, int rd, int rn, int rm)
6578 int pass;
6579 TCGv_ptr fpst = get_fpstatus_ptr();
6581 for (pass = 0; pass < elements; pass++) {
6582 if (size) {
6583 /* Double */
6584 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
6585 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
6586 TCGv_i64 tcg_res = tcg_temp_new_i64();
6588 read_vec_element(s, tcg_op1, rn, pass, MO_64);
6589 read_vec_element(s, tcg_op2, rm, pass, MO_64);
6591 switch (fpopcode) {
6592 case 0x39: /* FMLS */
6593 /* As usual for ARM, separate negation for fused multiply-add */
6594 gen_helper_vfp_negd(tcg_op1, tcg_op1);
6595 /* fall through */
6596 case 0x19: /* FMLA */
6597 read_vec_element(s, tcg_res, rd, pass, MO_64);
6598 gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2,
6599 tcg_res, fpst);
6600 break;
6601 case 0x18: /* FMAXNM */
6602 gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
6603 break;
6604 case 0x1a: /* FADD */
6605 gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
6606 break;
6607 case 0x1b: /* FMULX */
6608 gen_helper_vfp_mulxd(tcg_res, tcg_op1, tcg_op2, fpst);
6609 break;
6610 case 0x1c: /* FCMEQ */
6611 gen_helper_neon_ceq_f64(tcg_res, tcg_op1, tcg_op2, fpst);
6612 break;
6613 case 0x1e: /* FMAX */
6614 gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
6615 break;
6616 case 0x1f: /* FRECPS */
6617 gen_helper_recpsf_f64(tcg_res, tcg_op1, tcg_op2, fpst);
6618 break;
6619 case 0x38: /* FMINNM */
6620 gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
6621 break;
6622 case 0x3a: /* FSUB */
6623 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
6624 break;
6625 case 0x3e: /* FMIN */
6626 gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
6627 break;
6628 case 0x3f: /* FRSQRTS */
6629 gen_helper_rsqrtsf_f64(tcg_res, tcg_op1, tcg_op2, fpst);
6630 break;
6631 case 0x5b: /* FMUL */
6632 gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
6633 break;
6634 case 0x5c: /* FCMGE */
6635 gen_helper_neon_cge_f64(tcg_res, tcg_op1, tcg_op2, fpst);
6636 break;
6637 case 0x5d: /* FACGE */
6638 gen_helper_neon_acge_f64(tcg_res, tcg_op1, tcg_op2, fpst);
6639 break;
6640 case 0x5f: /* FDIV */
6641 gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
6642 break;
6643 case 0x7a: /* FABD */
6644 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
6645 gen_helper_vfp_absd(tcg_res, tcg_res);
6646 break;
6647 case 0x7c: /* FCMGT */
6648 gen_helper_neon_cgt_f64(tcg_res, tcg_op1, tcg_op2, fpst);
6649 break;
6650 case 0x7d: /* FACGT */
6651 gen_helper_neon_acgt_f64(tcg_res, tcg_op1, tcg_op2, fpst);
6652 break;
6653 default:
6654 g_assert_not_reached();
6657 write_vec_element(s, tcg_res, rd, pass, MO_64);
6659 tcg_temp_free_i64(tcg_res);
6660 tcg_temp_free_i64(tcg_op1);
6661 tcg_temp_free_i64(tcg_op2);
6662 } else {
6663 /* Single */
6664 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
6665 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
6666 TCGv_i32 tcg_res = tcg_temp_new_i32();
6668 read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
6669 read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
6671 switch (fpopcode) {
6672 case 0x39: /* FMLS */
6673 /* As usual for ARM, separate negation for fused multiply-add */
6674 gen_helper_vfp_negs(tcg_op1, tcg_op1);
6675 /* fall through */
6676 case 0x19: /* FMLA */
6677 read_vec_element_i32(s, tcg_res, rd, pass, MO_32);
6678 gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2,
6679 tcg_res, fpst);
6680 break;
6681 case 0x1a: /* FADD */
6682 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
6683 break;
6684 case 0x1b: /* FMULX */
6685 gen_helper_vfp_mulxs(tcg_res, tcg_op1, tcg_op2, fpst);
6686 break;
6687 case 0x1c: /* FCMEQ */
6688 gen_helper_neon_ceq_f32(tcg_res, tcg_op1, tcg_op2, fpst);
6689 break;
6690 case 0x1e: /* FMAX */
6691 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
6692 break;
6693 case 0x1f: /* FRECPS */
6694 gen_helper_recpsf_f32(tcg_res, tcg_op1, tcg_op2, fpst);
6695 break;
6696 case 0x18: /* FMAXNM */
6697 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
6698 break;
6699 case 0x38: /* FMINNM */
6700 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
6701 break;
6702 case 0x3a: /* FSUB */
6703 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
6704 break;
6705 case 0x3e: /* FMIN */
6706 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
6707 break;
6708 case 0x3f: /* FRSQRTS */
6709 gen_helper_rsqrtsf_f32(tcg_res, tcg_op1, tcg_op2, fpst);
6710 break;
6711 case 0x5b: /* FMUL */
6712 gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
6713 break;
6714 case 0x5c: /* FCMGE */
6715 gen_helper_neon_cge_f32(tcg_res, tcg_op1, tcg_op2, fpst);
6716 break;
6717 case 0x5d: /* FACGE */
6718 gen_helper_neon_acge_f32(tcg_res, tcg_op1, tcg_op2, fpst);
6719 break;
6720 case 0x5f: /* FDIV */
6721 gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
6722 break;
6723 case 0x7a: /* FABD */
6724 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
6725 gen_helper_vfp_abss(tcg_res, tcg_res);
6726 break;
6727 case 0x7c: /* FCMGT */
6728 gen_helper_neon_cgt_f32(tcg_res, tcg_op1, tcg_op2, fpst);
6729 break;
6730 case 0x7d: /* FACGT */
6731 gen_helper_neon_acgt_f32(tcg_res, tcg_op1, tcg_op2, fpst);
6732 break;
6733 default:
6734 g_assert_not_reached();
6737 if (elements == 1) {
6738 /* scalar single so clear high part */
6739 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
6741 tcg_gen_extu_i32_i64(tcg_tmp, tcg_res);
6742 write_vec_element(s, tcg_tmp, rd, pass, MO_64);
6743 tcg_temp_free_i64(tcg_tmp);
6744 } else {
6745 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
6748 tcg_temp_free_i32(tcg_res);
6749 tcg_temp_free_i32(tcg_op1);
6750 tcg_temp_free_i32(tcg_op2);
6754 tcg_temp_free_ptr(fpst);
6756 if ((elements << size) < 4) {
6757 /* scalar, or non-quad vector op */
6758 clear_vec_high(s, rd);
6762 /* C3.6.11 AdvSIMD scalar three same
6763 * 31 30 29 28 24 23 22 21 20 16 15 11 10 9 5 4 0
6764 * +-----+---+-----------+------+---+------+--------+---+------+------+
6765 * | 0 1 | U | 1 1 1 1 0 | size | 1 | Rm | opcode | 1 | Rn | Rd |
6766 * +-----+---+-----------+------+---+------+--------+---+------+------+
6768 static void disas_simd_scalar_three_reg_same(DisasContext *s, uint32_t insn)
6770 int rd = extract32(insn, 0, 5);
6771 int rn = extract32(insn, 5, 5);
6772 int opcode = extract32(insn, 11, 5);
6773 int rm = extract32(insn, 16, 5);
6774 int size = extract32(insn, 22, 2);
6775 bool u = extract32(insn, 29, 1);
6776 TCGv_i64 tcg_rd;
6778 if (opcode >= 0x18) {
6779 /* Floating point: U, size[1] and opcode indicate operation */
6780 int fpopcode = opcode | (extract32(size, 1, 1) << 5) | (u << 6);
6781 switch (fpopcode) {
6782 case 0x1b: /* FMULX */
6783 case 0x1f: /* FRECPS */
6784 case 0x3f: /* FRSQRTS */
6785 case 0x5d: /* FACGE */
6786 case 0x7d: /* FACGT */
6787 case 0x1c: /* FCMEQ */
6788 case 0x5c: /* FCMGE */
6789 case 0x7c: /* FCMGT */
6790 case 0x7a: /* FABD */
6791 break;
6792 default:
6793 unallocated_encoding(s);
6794 return;
6797 handle_3same_float(s, extract32(size, 0, 1), 1, fpopcode, rd, rn, rm);
6798 return;
6801 switch (opcode) {
6802 case 0x1: /* SQADD, UQADD */
6803 case 0x5: /* SQSUB, UQSUB */
6804 case 0x9: /* SQSHL, UQSHL */
6805 case 0xb: /* SQRSHL, UQRSHL */
6806 break;
6807 case 0x8: /* SSHL, USHL */
6808 case 0xa: /* SRSHL, URSHL */
6809 case 0x6: /* CMGT, CMHI */
6810 case 0x7: /* CMGE, CMHS */
6811 case 0x11: /* CMTST, CMEQ */
6812 case 0x10: /* ADD, SUB (vector) */
6813 if (size != 3) {
6814 unallocated_encoding(s);
6815 return;
6817 break;
6818 case 0x16: /* SQDMULH, SQRDMULH (vector) */
6819 if (size != 1 && size != 2) {
6820 unallocated_encoding(s);
6821 return;
6823 break;
6824 default:
6825 unallocated_encoding(s);
6826 return;
6829 tcg_rd = tcg_temp_new_i64();
6831 if (size == 3) {
6832 TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
6833 TCGv_i64 tcg_rm = read_fp_dreg(s, rm);
6835 handle_3same_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rm);
6836 tcg_temp_free_i64(tcg_rn);
6837 tcg_temp_free_i64(tcg_rm);
6838 } else {
6839 /* Do a single operation on the lowest element in the vector.
6840 * We use the standard Neon helpers and rely on 0 OP 0 == 0 with
6841 * no side effects for all these operations.
6842 * OPTME: special-purpose helpers would avoid doing some
6843 * unnecessary work in the helper for the 8 and 16 bit cases.
6845 NeonGenTwoOpEnvFn *genenvfn;
6846 TCGv_i32 tcg_rn = tcg_temp_new_i32();
6847 TCGv_i32 tcg_rm = tcg_temp_new_i32();
6848 TCGv_i32 tcg_rd32 = tcg_temp_new_i32();
6850 read_vec_element_i32(s, tcg_rn, rn, 0, size);
6851 read_vec_element_i32(s, tcg_rm, rm, 0, size);
6853 switch (opcode) {
6854 case 0x1: /* SQADD, UQADD */
6856 static NeonGenTwoOpEnvFn * const fns[3][2] = {
6857 { gen_helper_neon_qadd_s8, gen_helper_neon_qadd_u8 },
6858 { gen_helper_neon_qadd_s16, gen_helper_neon_qadd_u16 },
6859 { gen_helper_neon_qadd_s32, gen_helper_neon_qadd_u32 },
6861 genenvfn = fns[size][u];
6862 break;
6864 case 0x5: /* SQSUB, UQSUB */
6866 static NeonGenTwoOpEnvFn * const fns[3][2] = {
6867 { gen_helper_neon_qsub_s8, gen_helper_neon_qsub_u8 },
6868 { gen_helper_neon_qsub_s16, gen_helper_neon_qsub_u16 },
6869 { gen_helper_neon_qsub_s32, gen_helper_neon_qsub_u32 },
6871 genenvfn = fns[size][u];
6872 break;
6874 case 0x9: /* SQSHL, UQSHL */
6876 static NeonGenTwoOpEnvFn * const fns[3][2] = {
6877 { gen_helper_neon_qshl_s8, gen_helper_neon_qshl_u8 },
6878 { gen_helper_neon_qshl_s16, gen_helper_neon_qshl_u16 },
6879 { gen_helper_neon_qshl_s32, gen_helper_neon_qshl_u32 },
6881 genenvfn = fns[size][u];
6882 break;
6884 case 0xb: /* SQRSHL, UQRSHL */
6886 static NeonGenTwoOpEnvFn * const fns[3][2] = {
6887 { gen_helper_neon_qrshl_s8, gen_helper_neon_qrshl_u8 },
6888 { gen_helper_neon_qrshl_s16, gen_helper_neon_qrshl_u16 },
6889 { gen_helper_neon_qrshl_s32, gen_helper_neon_qrshl_u32 },
6891 genenvfn = fns[size][u];
6892 break;
6894 case 0x16: /* SQDMULH, SQRDMULH */
6896 static NeonGenTwoOpEnvFn * const fns[2][2] = {
6897 { gen_helper_neon_qdmulh_s16, gen_helper_neon_qrdmulh_s16 },
6898 { gen_helper_neon_qdmulh_s32, gen_helper_neon_qrdmulh_s32 },
6900 assert(size == 1 || size == 2);
6901 genenvfn = fns[size - 1][u];
6902 break;
6904 default:
6905 g_assert_not_reached();
6908 genenvfn(tcg_rd32, cpu_env, tcg_rn, tcg_rm);
6909 tcg_gen_extu_i32_i64(tcg_rd, tcg_rd32);
6910 tcg_temp_free_i32(tcg_rd32);
6911 tcg_temp_free_i32(tcg_rn);
6912 tcg_temp_free_i32(tcg_rm);
6915 write_fp_dreg(s, rd, tcg_rd);
6917 tcg_temp_free_i64(tcg_rd);
6920 static void handle_2misc_64(DisasContext *s, int opcode, bool u,
6921 TCGv_i64 tcg_rd, TCGv_i64 tcg_rn,
6922 TCGv_i32 tcg_rmode, TCGv_ptr tcg_fpstatus)
6924 /* Handle 64->64 opcodes which are shared between the scalar and
6925 * vector 2-reg-misc groups. We cover every integer opcode where size == 3
6926 * is valid in either group and also the double-precision fp ops.
6927 * The caller only need provide tcg_rmode and tcg_fpstatus if the op
6928 * requires them.
6930 TCGCond cond;
6932 switch (opcode) {
6933 case 0x4: /* CLS, CLZ */
6934 if (u) {
6935 gen_helper_clz64(tcg_rd, tcg_rn);
6936 } else {
6937 gen_helper_cls64(tcg_rd, tcg_rn);
6939 break;
6940 case 0x5: /* NOT */
6941 /* This opcode is shared with CNT and RBIT but we have earlier
6942 * enforced that size == 3 if and only if this is the NOT insn.
6944 tcg_gen_not_i64(tcg_rd, tcg_rn);
6945 break;
6946 case 0x7: /* SQABS, SQNEG */
6947 if (u) {
6948 gen_helper_neon_qneg_s64(tcg_rd, cpu_env, tcg_rn);
6949 } else {
6950 gen_helper_neon_qabs_s64(tcg_rd, cpu_env, tcg_rn);
6952 break;
6953 case 0xa: /* CMLT */
6954 /* 64 bit integer comparison against zero, result is
6955 * test ? (2^64 - 1) : 0. We implement via setcond(!test) and
6956 * subtracting 1.
6958 cond = TCG_COND_LT;
6959 do_cmop:
6960 tcg_gen_setcondi_i64(cond, tcg_rd, tcg_rn, 0);
6961 tcg_gen_neg_i64(tcg_rd, tcg_rd);
6962 break;
6963 case 0x8: /* CMGT, CMGE */
6964 cond = u ? TCG_COND_GE : TCG_COND_GT;
6965 goto do_cmop;
6966 case 0x9: /* CMEQ, CMLE */
6967 cond = u ? TCG_COND_LE : TCG_COND_EQ;
6968 goto do_cmop;
6969 case 0xb: /* ABS, NEG */
6970 if (u) {
6971 tcg_gen_neg_i64(tcg_rd, tcg_rn);
6972 } else {
6973 TCGv_i64 tcg_zero = tcg_const_i64(0);
6974 tcg_gen_neg_i64(tcg_rd, tcg_rn);
6975 tcg_gen_movcond_i64(TCG_COND_GT, tcg_rd, tcg_rn, tcg_zero,
6976 tcg_rn, tcg_rd);
6977 tcg_temp_free_i64(tcg_zero);
6979 break;
6980 case 0x2f: /* FABS */
6981 gen_helper_vfp_absd(tcg_rd, tcg_rn);
6982 break;
6983 case 0x6f: /* FNEG */
6984 gen_helper_vfp_negd(tcg_rd, tcg_rn);
6985 break;
6986 case 0x7f: /* FSQRT */
6987 gen_helper_vfp_sqrtd(tcg_rd, tcg_rn, cpu_env);
6988 break;
6989 case 0x1a: /* FCVTNS */
6990 case 0x1b: /* FCVTMS */
6991 case 0x1c: /* FCVTAS */
6992 case 0x3a: /* FCVTPS */
6993 case 0x3b: /* FCVTZS */
6995 TCGv_i32 tcg_shift = tcg_const_i32(0);
6996 gen_helper_vfp_tosqd(tcg_rd, tcg_rn, tcg_shift, tcg_fpstatus);
6997 tcg_temp_free_i32(tcg_shift);
6998 break;
7000 case 0x5a: /* FCVTNU */
7001 case 0x5b: /* FCVTMU */
7002 case 0x5c: /* FCVTAU */
7003 case 0x7a: /* FCVTPU */
7004 case 0x7b: /* FCVTZU */
7006 TCGv_i32 tcg_shift = tcg_const_i32(0);
7007 gen_helper_vfp_touqd(tcg_rd, tcg_rn, tcg_shift, tcg_fpstatus);
7008 tcg_temp_free_i32(tcg_shift);
7009 break;
7011 case 0x18: /* FRINTN */
7012 case 0x19: /* FRINTM */
7013 case 0x38: /* FRINTP */
7014 case 0x39: /* FRINTZ */
7015 case 0x58: /* FRINTA */
7016 case 0x79: /* FRINTI */
7017 gen_helper_rintd(tcg_rd, tcg_rn, tcg_fpstatus);
7018 break;
7019 case 0x59: /* FRINTX */
7020 gen_helper_rintd_exact(tcg_rd, tcg_rn, tcg_fpstatus);
7021 break;
7022 default:
7023 g_assert_not_reached();
7027 static void handle_2misc_fcmp_zero(DisasContext *s, int opcode,
7028 bool is_scalar, bool is_u, bool is_q,
7029 int size, int rn, int rd)
7031 bool is_double = (size == 3);
7032 TCGv_ptr fpst = get_fpstatus_ptr();
7034 if (is_double) {
7035 TCGv_i64 tcg_op = tcg_temp_new_i64();
7036 TCGv_i64 tcg_zero = tcg_const_i64(0);
7037 TCGv_i64 tcg_res = tcg_temp_new_i64();
7038 NeonGenTwoDoubleOPFn *genfn;
7039 bool swap = false;
7040 int pass;
7042 switch (opcode) {
7043 case 0x2e: /* FCMLT (zero) */
7044 swap = true;
7045 /* fallthrough */
7046 case 0x2c: /* FCMGT (zero) */
7047 genfn = gen_helper_neon_cgt_f64;
7048 break;
7049 case 0x2d: /* FCMEQ (zero) */
7050 genfn = gen_helper_neon_ceq_f64;
7051 break;
7052 case 0x6d: /* FCMLE (zero) */
7053 swap = true;
7054 /* fall through */
7055 case 0x6c: /* FCMGE (zero) */
7056 genfn = gen_helper_neon_cge_f64;
7057 break;
7058 default:
7059 g_assert_not_reached();
7062 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
7063 read_vec_element(s, tcg_op, rn, pass, MO_64);
7064 if (swap) {
7065 genfn(tcg_res, tcg_zero, tcg_op, fpst);
7066 } else {
7067 genfn(tcg_res, tcg_op, tcg_zero, fpst);
7069 write_vec_element(s, tcg_res, rd, pass, MO_64);
7071 if (is_scalar) {
7072 clear_vec_high(s, rd);
7075 tcg_temp_free_i64(tcg_res);
7076 tcg_temp_free_i64(tcg_zero);
7077 tcg_temp_free_i64(tcg_op);
7078 } else {
7079 TCGv_i32 tcg_op = tcg_temp_new_i32();
7080 TCGv_i32 tcg_zero = tcg_const_i32(0);
7081 TCGv_i32 tcg_res = tcg_temp_new_i32();
7082 NeonGenTwoSingleOPFn *genfn;
7083 bool swap = false;
7084 int pass, maxpasses;
7086 switch (opcode) {
7087 case 0x2e: /* FCMLT (zero) */
7088 swap = true;
7089 /* fall through */
7090 case 0x2c: /* FCMGT (zero) */
7091 genfn = gen_helper_neon_cgt_f32;
7092 break;
7093 case 0x2d: /* FCMEQ (zero) */
7094 genfn = gen_helper_neon_ceq_f32;
7095 break;
7096 case 0x6d: /* FCMLE (zero) */
7097 swap = true;
7098 /* fall through */
7099 case 0x6c: /* FCMGE (zero) */
7100 genfn = gen_helper_neon_cge_f32;
7101 break;
7102 default:
7103 g_assert_not_reached();
7106 if (is_scalar) {
7107 maxpasses = 1;
7108 } else {
7109 maxpasses = is_q ? 4 : 2;
7112 for (pass = 0; pass < maxpasses; pass++) {
7113 read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
7114 if (swap) {
7115 genfn(tcg_res, tcg_zero, tcg_op, fpst);
7116 } else {
7117 genfn(tcg_res, tcg_op, tcg_zero, fpst);
7119 if (is_scalar) {
7120 write_fp_sreg(s, rd, tcg_res);
7121 } else {
7122 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
7125 tcg_temp_free_i32(tcg_res);
7126 tcg_temp_free_i32(tcg_zero);
7127 tcg_temp_free_i32(tcg_op);
7128 if (!is_q && !is_scalar) {
7129 clear_vec_high(s, rd);
7133 tcg_temp_free_ptr(fpst);
7136 static void handle_2misc_reciprocal(DisasContext *s, int opcode,
7137 bool is_scalar, bool is_u, bool is_q,
7138 int size, int rn, int rd)
7140 bool is_double = (size == 3);
7141 TCGv_ptr fpst = get_fpstatus_ptr();
7143 if (is_double) {
7144 TCGv_i64 tcg_op = tcg_temp_new_i64();
7145 TCGv_i64 tcg_res = tcg_temp_new_i64();
7146 int pass;
7148 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
7149 read_vec_element(s, tcg_op, rn, pass, MO_64);
7150 switch (opcode) {
7151 case 0x3d: /* FRECPE */
7152 gen_helper_recpe_f64(tcg_res, tcg_op, fpst);
7153 break;
7154 case 0x3f: /* FRECPX */
7155 gen_helper_frecpx_f64(tcg_res, tcg_op, fpst);
7156 break;
7157 case 0x7d: /* FRSQRTE */
7158 gen_helper_rsqrte_f64(tcg_res, tcg_op, fpst);
7159 break;
7160 default:
7161 g_assert_not_reached();
7163 write_vec_element(s, tcg_res, rd, pass, MO_64);
7165 if (is_scalar) {
7166 clear_vec_high(s, rd);
7169 tcg_temp_free_i64(tcg_res);
7170 tcg_temp_free_i64(tcg_op);
7171 } else {
7172 TCGv_i32 tcg_op = tcg_temp_new_i32();
7173 TCGv_i32 tcg_res = tcg_temp_new_i32();
7174 int pass, maxpasses;
7176 if (is_scalar) {
7177 maxpasses = 1;
7178 } else {
7179 maxpasses = is_q ? 4 : 2;
7182 for (pass = 0; pass < maxpasses; pass++) {
7183 read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
7185 switch (opcode) {
7186 case 0x3c: /* URECPE */
7187 gen_helper_recpe_u32(tcg_res, tcg_op, fpst);
7188 break;
7189 case 0x3d: /* FRECPE */
7190 gen_helper_recpe_f32(tcg_res, tcg_op, fpst);
7191 break;
7192 case 0x3f: /* FRECPX */
7193 gen_helper_frecpx_f32(tcg_res, tcg_op, fpst);
7194 break;
7195 case 0x7d: /* FRSQRTE */
7196 gen_helper_rsqrte_f32(tcg_res, tcg_op, fpst);
7197 break;
7198 default:
7199 g_assert_not_reached();
7202 if (is_scalar) {
7203 write_fp_sreg(s, rd, tcg_res);
7204 } else {
7205 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
7208 tcg_temp_free_i32(tcg_res);
7209 tcg_temp_free_i32(tcg_op);
7210 if (!is_q && !is_scalar) {
7211 clear_vec_high(s, rd);
7214 tcg_temp_free_ptr(fpst);
7217 static void handle_2misc_narrow(DisasContext *s, bool scalar,
7218 int opcode, bool u, bool is_q,
7219 int size, int rn, int rd)
7221 /* Handle 2-reg-misc ops which are narrowing (so each 2*size element
7222 * in the source becomes a size element in the destination).
7224 int pass;
7225 TCGv_i32 tcg_res[2];
7226 int destelt = is_q ? 2 : 0;
7227 int passes = scalar ? 1 : 2;
7229 if (scalar) {
7230 tcg_res[1] = tcg_const_i32(0);
7233 for (pass = 0; pass < passes; pass++) {
7234 TCGv_i64 tcg_op = tcg_temp_new_i64();
7235 NeonGenNarrowFn *genfn = NULL;
7236 NeonGenNarrowEnvFn *genenvfn = NULL;
7238 if (scalar) {
7239 read_vec_element(s, tcg_op, rn, pass, size + 1);
7240 } else {
7241 read_vec_element(s, tcg_op, rn, pass, MO_64);
7243 tcg_res[pass] = tcg_temp_new_i32();
7245 switch (opcode) {
7246 case 0x12: /* XTN, SQXTUN */
7248 static NeonGenNarrowFn * const xtnfns[3] = {
7249 gen_helper_neon_narrow_u8,
7250 gen_helper_neon_narrow_u16,
7251 tcg_gen_trunc_i64_i32,
7253 static NeonGenNarrowEnvFn * const sqxtunfns[3] = {
7254 gen_helper_neon_unarrow_sat8,
7255 gen_helper_neon_unarrow_sat16,
7256 gen_helper_neon_unarrow_sat32,
7258 if (u) {
7259 genenvfn = sqxtunfns[size];
7260 } else {
7261 genfn = xtnfns[size];
7263 break;
7265 case 0x14: /* SQXTN, UQXTN */
7267 static NeonGenNarrowEnvFn * const fns[3][2] = {
7268 { gen_helper_neon_narrow_sat_s8,
7269 gen_helper_neon_narrow_sat_u8 },
7270 { gen_helper_neon_narrow_sat_s16,
7271 gen_helper_neon_narrow_sat_u16 },
7272 { gen_helper_neon_narrow_sat_s32,
7273 gen_helper_neon_narrow_sat_u32 },
7275 genenvfn = fns[size][u];
7276 break;
7278 case 0x16: /* FCVTN, FCVTN2 */
7279 /* 32 bit to 16 bit or 64 bit to 32 bit float conversion */
7280 if (size == 2) {
7281 gen_helper_vfp_fcvtsd(tcg_res[pass], tcg_op, cpu_env);
7282 } else {
7283 TCGv_i32 tcg_lo = tcg_temp_new_i32();
7284 TCGv_i32 tcg_hi = tcg_temp_new_i32();
7285 tcg_gen_trunc_i64_i32(tcg_lo, tcg_op);
7286 gen_helper_vfp_fcvt_f32_to_f16(tcg_lo, tcg_lo, cpu_env);
7287 tcg_gen_shri_i64(tcg_op, tcg_op, 32);
7288 tcg_gen_trunc_i64_i32(tcg_hi, tcg_op);
7289 gen_helper_vfp_fcvt_f32_to_f16(tcg_hi, tcg_hi, cpu_env);
7290 tcg_gen_deposit_i32(tcg_res[pass], tcg_lo, tcg_hi, 16, 16);
7291 tcg_temp_free_i32(tcg_lo);
7292 tcg_temp_free_i32(tcg_hi);
7294 break;
7295 case 0x56: /* FCVTXN, FCVTXN2 */
7296 /* 64 bit to 32 bit float conversion
7297 * with von Neumann rounding (round to odd)
7299 assert(size == 2);
7300 gen_helper_fcvtx_f64_to_f32(tcg_res[pass], tcg_op, cpu_env);
7301 break;
7302 default:
7303 g_assert_not_reached();
7306 if (genfn) {
7307 genfn(tcg_res[pass], tcg_op);
7308 } else if (genenvfn) {
7309 genenvfn(tcg_res[pass], cpu_env, tcg_op);
7312 tcg_temp_free_i64(tcg_op);
7315 for (pass = 0; pass < 2; pass++) {
7316 write_vec_element_i32(s, tcg_res[pass], rd, destelt + pass, MO_32);
7317 tcg_temp_free_i32(tcg_res[pass]);
7319 if (!is_q) {
7320 clear_vec_high(s, rd);
7324 /* Remaining saturating accumulating ops */
7325 static void handle_2misc_satacc(DisasContext *s, bool is_scalar, bool is_u,
7326 bool is_q, int size, int rn, int rd)
7328 bool is_double = (size == 3);
7330 if (is_double) {
7331 TCGv_i64 tcg_rn = tcg_temp_new_i64();
7332 TCGv_i64 tcg_rd = tcg_temp_new_i64();
7333 int pass;
7335 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
7336 read_vec_element(s, tcg_rn, rn, pass, MO_64);
7337 read_vec_element(s, tcg_rd, rd, pass, MO_64);
7339 if (is_u) { /* USQADD */
7340 gen_helper_neon_uqadd_s64(tcg_rd, cpu_env, tcg_rn, tcg_rd);
7341 } else { /* SUQADD */
7342 gen_helper_neon_sqadd_u64(tcg_rd, cpu_env, tcg_rn, tcg_rd);
7344 write_vec_element(s, tcg_rd, rd, pass, MO_64);
7346 if (is_scalar) {
7347 clear_vec_high(s, rd);
7350 tcg_temp_free_i64(tcg_rd);
7351 tcg_temp_free_i64(tcg_rn);
7352 } else {
7353 TCGv_i32 tcg_rn = tcg_temp_new_i32();
7354 TCGv_i32 tcg_rd = tcg_temp_new_i32();
7355 int pass, maxpasses;
7357 if (is_scalar) {
7358 maxpasses = 1;
7359 } else {
7360 maxpasses = is_q ? 4 : 2;
7363 for (pass = 0; pass < maxpasses; pass++) {
7364 if (is_scalar) {
7365 read_vec_element_i32(s, tcg_rn, rn, pass, size);
7366 read_vec_element_i32(s, tcg_rd, rd, pass, size);
7367 } else {
7368 read_vec_element_i32(s, tcg_rn, rn, pass, MO_32);
7369 read_vec_element_i32(s, tcg_rd, rd, pass, MO_32);
7372 if (is_u) { /* USQADD */
7373 switch (size) {
7374 case 0:
7375 gen_helper_neon_uqadd_s8(tcg_rd, cpu_env, tcg_rn, tcg_rd);
7376 break;
7377 case 1:
7378 gen_helper_neon_uqadd_s16(tcg_rd, cpu_env, tcg_rn, tcg_rd);
7379 break;
7380 case 2:
7381 gen_helper_neon_uqadd_s32(tcg_rd, cpu_env, tcg_rn, tcg_rd);
7382 break;
7383 default:
7384 g_assert_not_reached();
7386 } else { /* SUQADD */
7387 switch (size) {
7388 case 0:
7389 gen_helper_neon_sqadd_u8(tcg_rd, cpu_env, tcg_rn, tcg_rd);
7390 break;
7391 case 1:
7392 gen_helper_neon_sqadd_u16(tcg_rd, cpu_env, tcg_rn, tcg_rd);
7393 break;
7394 case 2:
7395 gen_helper_neon_sqadd_u32(tcg_rd, cpu_env, tcg_rn, tcg_rd);
7396 break;
7397 default:
7398 g_assert_not_reached();
7402 if (is_scalar) {
7403 TCGv_i64 tcg_zero = tcg_const_i64(0);
7404 write_vec_element(s, tcg_zero, rd, 0, MO_64);
7405 tcg_temp_free_i64(tcg_zero);
7407 write_vec_element_i32(s, tcg_rd, rd, pass, MO_32);
7410 if (!is_q) {
7411 clear_vec_high(s, rd);
7414 tcg_temp_free_i32(tcg_rd);
7415 tcg_temp_free_i32(tcg_rn);
7419 /* C3.6.12 AdvSIMD scalar two reg misc
7420 * 31 30 29 28 24 23 22 21 17 16 12 11 10 9 5 4 0
7421 * +-----+---+-----------+------+-----------+--------+-----+------+------+
7422 * | 0 1 | U | 1 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 | Rn | Rd |
7423 * +-----+---+-----------+------+-----------+--------+-----+------+------+
7425 static void disas_simd_scalar_two_reg_misc(DisasContext *s, uint32_t insn)
7427 int rd = extract32(insn, 0, 5);
7428 int rn = extract32(insn, 5, 5);
7429 int opcode = extract32(insn, 12, 5);
7430 int size = extract32(insn, 22, 2);
7431 bool u = extract32(insn, 29, 1);
7432 bool is_fcvt = false;
7433 int rmode;
7434 TCGv_i32 tcg_rmode;
7435 TCGv_ptr tcg_fpstatus;
7437 switch (opcode) {
7438 case 0x3: /* USQADD / SUQADD*/
7439 handle_2misc_satacc(s, true, u, false, size, rn, rd);
7440 return;
7441 case 0x7: /* SQABS / SQNEG */
7442 break;
7443 case 0xa: /* CMLT */
7444 if (u) {
7445 unallocated_encoding(s);
7446 return;
7448 /* fall through */
7449 case 0x8: /* CMGT, CMGE */
7450 case 0x9: /* CMEQ, CMLE */
7451 case 0xb: /* ABS, NEG */
7452 if (size != 3) {
7453 unallocated_encoding(s);
7454 return;
7456 break;
7457 case 0x12: /* SQXTUN */
7458 if (u) {
7459 unallocated_encoding(s);
7460 return;
7462 /* fall through */
7463 case 0x14: /* SQXTN, UQXTN */
7464 if (size == 3) {
7465 unallocated_encoding(s);
7466 return;
7468 handle_2misc_narrow(s, true, opcode, u, false, size, rn, rd);
7469 return;
7470 case 0xc ... 0xf:
7471 case 0x16 ... 0x1d:
7472 case 0x1f:
7473 /* Floating point: U, size[1] and opcode indicate operation;
7474 * size[0] indicates single or double precision.
7476 opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
7477 size = extract32(size, 0, 1) ? 3 : 2;
7478 switch (opcode) {
7479 case 0x2c: /* FCMGT (zero) */
7480 case 0x2d: /* FCMEQ (zero) */
7481 case 0x2e: /* FCMLT (zero) */
7482 case 0x6c: /* FCMGE (zero) */
7483 case 0x6d: /* FCMLE (zero) */
7484 handle_2misc_fcmp_zero(s, opcode, true, u, true, size, rn, rd);
7485 return;
7486 case 0x1d: /* SCVTF */
7487 case 0x5d: /* UCVTF */
7489 bool is_signed = (opcode == 0x1d);
7490 handle_simd_intfp_conv(s, rd, rn, 1, is_signed, 0, size);
7491 return;
7493 case 0x3d: /* FRECPE */
7494 case 0x3f: /* FRECPX */
7495 case 0x7d: /* FRSQRTE */
7496 handle_2misc_reciprocal(s, opcode, true, u, true, size, rn, rd);
7497 return;
7498 case 0x1a: /* FCVTNS */
7499 case 0x1b: /* FCVTMS */
7500 case 0x3a: /* FCVTPS */
7501 case 0x3b: /* FCVTZS */
7502 case 0x5a: /* FCVTNU */
7503 case 0x5b: /* FCVTMU */
7504 case 0x7a: /* FCVTPU */
7505 case 0x7b: /* FCVTZU */
7506 is_fcvt = true;
7507 rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
7508 break;
7509 case 0x1c: /* FCVTAS */
7510 case 0x5c: /* FCVTAU */
7511 /* TIEAWAY doesn't fit in the usual rounding mode encoding */
7512 is_fcvt = true;
7513 rmode = FPROUNDING_TIEAWAY;
7514 break;
7515 case 0x56: /* FCVTXN, FCVTXN2 */
7516 if (size == 2) {
7517 unallocated_encoding(s);
7518 return;
7520 handle_2misc_narrow(s, true, opcode, u, false, size - 1, rn, rd);
7521 return;
7522 default:
7523 unallocated_encoding(s);
7524 return;
7526 break;
7527 default:
7528 unallocated_encoding(s);
7529 return;
7532 if (is_fcvt) {
7533 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rmode));
7534 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
7535 tcg_fpstatus = get_fpstatus_ptr();
7536 } else {
7537 TCGV_UNUSED_I32(tcg_rmode);
7538 TCGV_UNUSED_PTR(tcg_fpstatus);
7541 if (size == 3) {
7542 TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
7543 TCGv_i64 tcg_rd = tcg_temp_new_i64();
7545 handle_2misc_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rmode, tcg_fpstatus);
7546 write_fp_dreg(s, rd, tcg_rd);
7547 tcg_temp_free_i64(tcg_rd);
7548 tcg_temp_free_i64(tcg_rn);
7549 } else {
7550 TCGv_i32 tcg_rn = tcg_temp_new_i32();
7551 TCGv_i32 tcg_rd = tcg_temp_new_i32();
7553 read_vec_element_i32(s, tcg_rn, rn, 0, size);
7555 switch (opcode) {
7556 case 0x7: /* SQABS, SQNEG */
7558 NeonGenOneOpEnvFn *genfn;
7559 static NeonGenOneOpEnvFn * const fns[3][2] = {
7560 { gen_helper_neon_qabs_s8, gen_helper_neon_qneg_s8 },
7561 { gen_helper_neon_qabs_s16, gen_helper_neon_qneg_s16 },
7562 { gen_helper_neon_qabs_s32, gen_helper_neon_qneg_s32 },
7564 genfn = fns[size][u];
7565 genfn(tcg_rd, cpu_env, tcg_rn);
7566 break;
7568 case 0x1a: /* FCVTNS */
7569 case 0x1b: /* FCVTMS */
7570 case 0x1c: /* FCVTAS */
7571 case 0x3a: /* FCVTPS */
7572 case 0x3b: /* FCVTZS */
7574 TCGv_i32 tcg_shift = tcg_const_i32(0);
7575 gen_helper_vfp_tosls(tcg_rd, tcg_rn, tcg_shift, tcg_fpstatus);
7576 tcg_temp_free_i32(tcg_shift);
7577 break;
7579 case 0x5a: /* FCVTNU */
7580 case 0x5b: /* FCVTMU */
7581 case 0x5c: /* FCVTAU */
7582 case 0x7a: /* FCVTPU */
7583 case 0x7b: /* FCVTZU */
7585 TCGv_i32 tcg_shift = tcg_const_i32(0);
7586 gen_helper_vfp_touls(tcg_rd, tcg_rn, tcg_shift, tcg_fpstatus);
7587 tcg_temp_free_i32(tcg_shift);
7588 break;
7590 default:
7591 g_assert_not_reached();
7594 write_fp_sreg(s, rd, tcg_rd);
7595 tcg_temp_free_i32(tcg_rd);
7596 tcg_temp_free_i32(tcg_rn);
7599 if (is_fcvt) {
7600 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
7601 tcg_temp_free_i32(tcg_rmode);
7602 tcg_temp_free_ptr(tcg_fpstatus);
7606 /* SSHR[RA]/USHR[RA] - Vector shift right (optional rounding/accumulate) */
7607 static void handle_vec_simd_shri(DisasContext *s, bool is_q, bool is_u,
7608 int immh, int immb, int opcode, int rn, int rd)
7610 int size = 32 - clz32(immh) - 1;
7611 int immhb = immh << 3 | immb;
7612 int shift = 2 * (8 << size) - immhb;
7613 bool accumulate = false;
7614 bool round = false;
7615 bool insert = false;
7616 int dsize = is_q ? 128 : 64;
7617 int esize = 8 << size;
7618 int elements = dsize/esize;
7619 TCGMemOp memop = size | (is_u ? 0 : MO_SIGN);
7620 TCGv_i64 tcg_rn = new_tmp_a64(s);
7621 TCGv_i64 tcg_rd = new_tmp_a64(s);
7622 TCGv_i64 tcg_round;
7623 int i;
7625 if (extract32(immh, 3, 1) && !is_q) {
7626 unallocated_encoding(s);
7627 return;
7630 if (size > 3 && !is_q) {
7631 unallocated_encoding(s);
7632 return;
7635 switch (opcode) {
7636 case 0x02: /* SSRA / USRA (accumulate) */
7637 accumulate = true;
7638 break;
7639 case 0x04: /* SRSHR / URSHR (rounding) */
7640 round = true;
7641 break;
7642 case 0x06: /* SRSRA / URSRA (accum + rounding) */
7643 accumulate = round = true;
7644 break;
7645 case 0x08: /* SRI */
7646 insert = true;
7647 break;
7650 if (round) {
7651 uint64_t round_const = 1ULL << (shift - 1);
7652 tcg_round = tcg_const_i64(round_const);
7653 } else {
7654 TCGV_UNUSED_I64(tcg_round);
7657 for (i = 0; i < elements; i++) {
7658 read_vec_element(s, tcg_rn, rn, i, memop);
7659 if (accumulate || insert) {
7660 read_vec_element(s, tcg_rd, rd, i, memop);
7663 if (insert) {
7664 handle_shri_with_ins(tcg_rd, tcg_rn, size, shift);
7665 } else {
7666 handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
7667 accumulate, is_u, size, shift);
7670 write_vec_element(s, tcg_rd, rd, i, size);
7673 if (!is_q) {
7674 clear_vec_high(s, rd);
7677 if (round) {
7678 tcg_temp_free_i64(tcg_round);
7682 /* SHL/SLI - Vector shift left */
7683 static void handle_vec_simd_shli(DisasContext *s, bool is_q, bool insert,
7684 int immh, int immb, int opcode, int rn, int rd)
7686 int size = 32 - clz32(immh) - 1;
7687 int immhb = immh << 3 | immb;
7688 int shift = immhb - (8 << size);
7689 int dsize = is_q ? 128 : 64;
7690 int esize = 8 << size;
7691 int elements = dsize/esize;
7692 TCGv_i64 tcg_rn = new_tmp_a64(s);
7693 TCGv_i64 tcg_rd = new_tmp_a64(s);
7694 int i;
7696 if (extract32(immh, 3, 1) && !is_q) {
7697 unallocated_encoding(s);
7698 return;
7701 if (size > 3 && !is_q) {
7702 unallocated_encoding(s);
7703 return;
7706 for (i = 0; i < elements; i++) {
7707 read_vec_element(s, tcg_rn, rn, i, size);
7708 if (insert) {
7709 read_vec_element(s, tcg_rd, rd, i, size);
7712 handle_shli_with_ins(tcg_rd, tcg_rn, insert, shift);
7714 write_vec_element(s, tcg_rd, rd, i, size);
7717 if (!is_q) {
7718 clear_vec_high(s, rd);
7722 /* USHLL/SHLL - Vector shift left with widening */
7723 static void handle_vec_simd_wshli(DisasContext *s, bool is_q, bool is_u,
7724 int immh, int immb, int opcode, int rn, int rd)
7726 int size = 32 - clz32(immh) - 1;
7727 int immhb = immh << 3 | immb;
7728 int shift = immhb - (8 << size);
7729 int dsize = 64;
7730 int esize = 8 << size;
7731 int elements = dsize/esize;
7732 TCGv_i64 tcg_rn = new_tmp_a64(s);
7733 TCGv_i64 tcg_rd = new_tmp_a64(s);
7734 int i;
7736 if (size >= 3) {
7737 unallocated_encoding(s);
7738 return;
7741 /* For the LL variants the store is larger than the load,
7742 * so if rd == rn we would overwrite parts of our input.
7743 * So load everything right now and use shifts in the main loop.
7745 read_vec_element(s, tcg_rn, rn, is_q ? 1 : 0, MO_64);
7747 for (i = 0; i < elements; i++) {
7748 tcg_gen_shri_i64(tcg_rd, tcg_rn, i * esize);
7749 ext_and_shift_reg(tcg_rd, tcg_rd, size | (!is_u << 2), 0);
7750 tcg_gen_shli_i64(tcg_rd, tcg_rd, shift);
7751 write_vec_element(s, tcg_rd, rd, i, size + 1);
7755 /* SHRN/RSHRN - Shift right with narrowing (and potential rounding) */
7756 static void handle_vec_simd_shrn(DisasContext *s, bool is_q,
7757 int immh, int immb, int opcode, int rn, int rd)
7759 int immhb = immh << 3 | immb;
7760 int size = 32 - clz32(immh) - 1;
7761 int dsize = 64;
7762 int esize = 8 << size;
7763 int elements = dsize/esize;
7764 int shift = (2 * esize) - immhb;
7765 bool round = extract32(opcode, 0, 1);
7766 TCGv_i64 tcg_rn, tcg_rd, tcg_final;
7767 TCGv_i64 tcg_round;
7768 int i;
7770 if (extract32(immh, 3, 1)) {
7771 unallocated_encoding(s);
7772 return;
7775 tcg_rn = tcg_temp_new_i64();
7776 tcg_rd = tcg_temp_new_i64();
7777 tcg_final = tcg_temp_new_i64();
7778 read_vec_element(s, tcg_final, rd, is_q ? 1 : 0, MO_64);
7780 if (round) {
7781 uint64_t round_const = 1ULL << (shift - 1);
7782 tcg_round = tcg_const_i64(round_const);
7783 } else {
7784 TCGV_UNUSED_I64(tcg_round);
7787 for (i = 0; i < elements; i++) {
7788 read_vec_element(s, tcg_rn, rn, i, size+1);
7789 handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
7790 false, true, size+1, shift);
7792 tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
7795 if (!is_q) {
7796 clear_vec_high(s, rd);
7797 write_vec_element(s, tcg_final, rd, 0, MO_64);
7798 } else {
7799 write_vec_element(s, tcg_final, rd, 1, MO_64);
7802 if (round) {
7803 tcg_temp_free_i64(tcg_round);
7805 tcg_temp_free_i64(tcg_rn);
7806 tcg_temp_free_i64(tcg_rd);
7807 tcg_temp_free_i64(tcg_final);
7808 return;
7812 /* C3.6.14 AdvSIMD shift by immediate
7813 * 31 30 29 28 23 22 19 18 16 15 11 10 9 5 4 0
7814 * +---+---+---+-------------+------+------+--------+---+------+------+
7815 * | 0 | Q | U | 0 1 1 1 1 0 | immh | immb | opcode | 1 | Rn | Rd |
7816 * +---+---+---+-------------+------+------+--------+---+------+------+
7818 static void disas_simd_shift_imm(DisasContext *s, uint32_t insn)
7820 int rd = extract32(insn, 0, 5);
7821 int rn = extract32(insn, 5, 5);
7822 int opcode = extract32(insn, 11, 5);
7823 int immb = extract32(insn, 16, 3);
7824 int immh = extract32(insn, 19, 4);
7825 bool is_u = extract32(insn, 29, 1);
7826 bool is_q = extract32(insn, 30, 1);
7828 switch (opcode) {
7829 case 0x08: /* SRI */
7830 if (!is_u) {
7831 unallocated_encoding(s);
7832 return;
7834 /* fall through */
7835 case 0x00: /* SSHR / USHR */
7836 case 0x02: /* SSRA / USRA (accumulate) */
7837 case 0x04: /* SRSHR / URSHR (rounding) */
7838 case 0x06: /* SRSRA / URSRA (accum + rounding) */
7839 handle_vec_simd_shri(s, is_q, is_u, immh, immb, opcode, rn, rd);
7840 break;
7841 case 0x0a: /* SHL / SLI */
7842 handle_vec_simd_shli(s, is_q, is_u, immh, immb, opcode, rn, rd);
7843 break;
7844 case 0x10: /* SHRN */
7845 case 0x11: /* RSHRN / SQRSHRUN */
7846 if (is_u) {
7847 handle_vec_simd_sqshrn(s, false, is_q, false, true, immh, immb,
7848 opcode, rn, rd);
7849 } else {
7850 handle_vec_simd_shrn(s, is_q, immh, immb, opcode, rn, rd);
7852 break;
7853 case 0x12: /* SQSHRN / UQSHRN */
7854 case 0x13: /* SQRSHRN / UQRSHRN */
7855 handle_vec_simd_sqshrn(s, false, is_q, is_u, is_u, immh, immb,
7856 opcode, rn, rd);
7857 break;
7858 case 0x14: /* SSHLL / USHLL */
7859 handle_vec_simd_wshli(s, is_q, is_u, immh, immb, opcode, rn, rd);
7860 break;
7861 case 0x1c: /* SCVTF / UCVTF */
7862 handle_simd_shift_intfp_conv(s, false, is_q, is_u, immh, immb,
7863 opcode, rn, rd);
7864 break;
7865 case 0xc: /* SQSHLU */
7866 if (!is_u) {
7867 unallocated_encoding(s);
7868 return;
7870 handle_simd_qshl(s, false, is_q, false, true, immh, immb, rn, rd);
7871 break;
7872 case 0xe: /* SQSHL, UQSHL */
7873 handle_simd_qshl(s, false, is_q, is_u, is_u, immh, immb, rn, rd);
7874 break;
7875 case 0x1f: /* FCVTZS/ FCVTZU */
7876 handle_simd_shift_fpint_conv(s, false, is_q, is_u, immh, immb, rn, rd);
7877 return;
7878 default:
7879 unallocated_encoding(s);
7880 return;
7884 /* Generate code to do a "long" addition or subtraction, ie one done in
7885 * TCGv_i64 on vector lanes twice the width specified by size.
7887 static void gen_neon_addl(int size, bool is_sub, TCGv_i64 tcg_res,
7888 TCGv_i64 tcg_op1, TCGv_i64 tcg_op2)
7890 static NeonGenTwo64OpFn * const fns[3][2] = {
7891 { gen_helper_neon_addl_u16, gen_helper_neon_subl_u16 },
7892 { gen_helper_neon_addl_u32, gen_helper_neon_subl_u32 },
7893 { tcg_gen_add_i64, tcg_gen_sub_i64 },
7895 NeonGenTwo64OpFn *genfn;
7896 assert(size < 3);
7898 genfn = fns[size][is_sub];
7899 genfn(tcg_res, tcg_op1, tcg_op2);
7902 static void handle_3rd_widening(DisasContext *s, int is_q, int is_u, int size,
7903 int opcode, int rd, int rn, int rm)
7905 /* 3-reg-different widening insns: 64 x 64 -> 128 */
7906 TCGv_i64 tcg_res[2];
7907 int pass, accop;
7909 tcg_res[0] = tcg_temp_new_i64();
7910 tcg_res[1] = tcg_temp_new_i64();
7912 /* Does this op do an adding accumulate, a subtracting accumulate,
7913 * or no accumulate at all?
7915 switch (opcode) {
7916 case 5:
7917 case 8:
7918 case 9:
7919 accop = 1;
7920 break;
7921 case 10:
7922 case 11:
7923 accop = -1;
7924 break;
7925 default:
7926 accop = 0;
7927 break;
7930 if (accop != 0) {
7931 read_vec_element(s, tcg_res[0], rd, 0, MO_64);
7932 read_vec_element(s, tcg_res[1], rd, 1, MO_64);
7935 /* size == 2 means two 32x32->64 operations; this is worth special
7936 * casing because we can generally handle it inline.
7938 if (size == 2) {
7939 for (pass = 0; pass < 2; pass++) {
7940 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
7941 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
7942 TCGv_i64 tcg_passres;
7943 TCGMemOp memop = MO_32 | (is_u ? 0 : MO_SIGN);
7945 int elt = pass + is_q * 2;
7947 read_vec_element(s, tcg_op1, rn, elt, memop);
7948 read_vec_element(s, tcg_op2, rm, elt, memop);
7950 if (accop == 0) {
7951 tcg_passres = tcg_res[pass];
7952 } else {
7953 tcg_passres = tcg_temp_new_i64();
7956 switch (opcode) {
7957 case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
7958 tcg_gen_add_i64(tcg_passres, tcg_op1, tcg_op2);
7959 break;
7960 case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
7961 tcg_gen_sub_i64(tcg_passres, tcg_op1, tcg_op2);
7962 break;
7963 case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
7964 case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
7966 TCGv_i64 tcg_tmp1 = tcg_temp_new_i64();
7967 TCGv_i64 tcg_tmp2 = tcg_temp_new_i64();
7969 tcg_gen_sub_i64(tcg_tmp1, tcg_op1, tcg_op2);
7970 tcg_gen_sub_i64(tcg_tmp2, tcg_op2, tcg_op1);
7971 tcg_gen_movcond_i64(is_u ? TCG_COND_GEU : TCG_COND_GE,
7972 tcg_passres,
7973 tcg_op1, tcg_op2, tcg_tmp1, tcg_tmp2);
7974 tcg_temp_free_i64(tcg_tmp1);
7975 tcg_temp_free_i64(tcg_tmp2);
7976 break;
7978 case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
7979 case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
7980 case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
7981 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
7982 break;
7983 case 9: /* SQDMLAL, SQDMLAL2 */
7984 case 11: /* SQDMLSL, SQDMLSL2 */
7985 case 13: /* SQDMULL, SQDMULL2 */
7986 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
7987 gen_helper_neon_addl_saturate_s64(tcg_passres, cpu_env,
7988 tcg_passres, tcg_passres);
7989 break;
7990 default:
7991 g_assert_not_reached();
7994 if (opcode == 9 || opcode == 11) {
7995 /* saturating accumulate ops */
7996 if (accop < 0) {
7997 tcg_gen_neg_i64(tcg_passres, tcg_passres);
7999 gen_helper_neon_addl_saturate_s64(tcg_res[pass], cpu_env,
8000 tcg_res[pass], tcg_passres);
8001 } else if (accop > 0) {
8002 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
8003 } else if (accop < 0) {
8004 tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
8007 if (accop != 0) {
8008 tcg_temp_free_i64(tcg_passres);
8011 tcg_temp_free_i64(tcg_op1);
8012 tcg_temp_free_i64(tcg_op2);
8014 } else {
8015 /* size 0 or 1, generally helper functions */
8016 for (pass = 0; pass < 2; pass++) {
8017 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
8018 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
8019 TCGv_i64 tcg_passres;
8020 int elt = pass + is_q * 2;
8022 read_vec_element_i32(s, tcg_op1, rn, elt, MO_32);
8023 read_vec_element_i32(s, tcg_op2, rm, elt, MO_32);
8025 if (accop == 0) {
8026 tcg_passres = tcg_res[pass];
8027 } else {
8028 tcg_passres = tcg_temp_new_i64();
8031 switch (opcode) {
8032 case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
8033 case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
8035 TCGv_i64 tcg_op2_64 = tcg_temp_new_i64();
8036 static NeonGenWidenFn * const widenfns[2][2] = {
8037 { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
8038 { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
8040 NeonGenWidenFn *widenfn = widenfns[size][is_u];
8042 widenfn(tcg_op2_64, tcg_op2);
8043 widenfn(tcg_passres, tcg_op1);
8044 gen_neon_addl(size, (opcode == 2), tcg_passres,
8045 tcg_passres, tcg_op2_64);
8046 tcg_temp_free_i64(tcg_op2_64);
8047 break;
8049 case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
8050 case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
8051 if (size == 0) {
8052 if (is_u) {
8053 gen_helper_neon_abdl_u16(tcg_passres, tcg_op1, tcg_op2);
8054 } else {
8055 gen_helper_neon_abdl_s16(tcg_passres, tcg_op1, tcg_op2);
8057 } else {
8058 if (is_u) {
8059 gen_helper_neon_abdl_u32(tcg_passres, tcg_op1, tcg_op2);
8060 } else {
8061 gen_helper_neon_abdl_s32(tcg_passres, tcg_op1, tcg_op2);
8064 break;
8065 case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
8066 case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
8067 case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
8068 if (size == 0) {
8069 if (is_u) {
8070 gen_helper_neon_mull_u8(tcg_passres, tcg_op1, tcg_op2);
8071 } else {
8072 gen_helper_neon_mull_s8(tcg_passres, tcg_op1, tcg_op2);
8074 } else {
8075 if (is_u) {
8076 gen_helper_neon_mull_u16(tcg_passres, tcg_op1, tcg_op2);
8077 } else {
8078 gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
8081 break;
8082 case 9: /* SQDMLAL, SQDMLAL2 */
8083 case 11: /* SQDMLSL, SQDMLSL2 */
8084 case 13: /* SQDMULL, SQDMULL2 */
8085 assert(size == 1);
8086 gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
8087 gen_helper_neon_addl_saturate_s32(tcg_passres, cpu_env,
8088 tcg_passres, tcg_passres);
8089 break;
8090 case 14: /* PMULL */
8091 assert(size == 0);
8092 gen_helper_neon_mull_p8(tcg_passres, tcg_op1, tcg_op2);
8093 break;
8094 default:
8095 g_assert_not_reached();
8097 tcg_temp_free_i32(tcg_op1);
8098 tcg_temp_free_i32(tcg_op2);
8100 if (accop != 0) {
8101 if (opcode == 9 || opcode == 11) {
8102 /* saturating accumulate ops */
8103 if (accop < 0) {
8104 gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
8106 gen_helper_neon_addl_saturate_s32(tcg_res[pass], cpu_env,
8107 tcg_res[pass],
8108 tcg_passres);
8109 } else {
8110 gen_neon_addl(size, (accop < 0), tcg_res[pass],
8111 tcg_res[pass], tcg_passres);
8113 tcg_temp_free_i64(tcg_passres);
8118 write_vec_element(s, tcg_res[0], rd, 0, MO_64);
8119 write_vec_element(s, tcg_res[1], rd, 1, MO_64);
8120 tcg_temp_free_i64(tcg_res[0]);
8121 tcg_temp_free_i64(tcg_res[1]);
8124 static void handle_3rd_wide(DisasContext *s, int is_q, int is_u, int size,
8125 int opcode, int rd, int rn, int rm)
8127 TCGv_i64 tcg_res[2];
8128 int part = is_q ? 2 : 0;
8129 int pass;
8131 for (pass = 0; pass < 2; pass++) {
8132 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8133 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
8134 TCGv_i64 tcg_op2_wide = tcg_temp_new_i64();
8135 static NeonGenWidenFn * const widenfns[3][2] = {
8136 { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
8137 { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
8138 { tcg_gen_ext_i32_i64, tcg_gen_extu_i32_i64 },
8140 NeonGenWidenFn *widenfn = widenfns[size][is_u];
8142 read_vec_element(s, tcg_op1, rn, pass, MO_64);
8143 read_vec_element_i32(s, tcg_op2, rm, part + pass, MO_32);
8144 widenfn(tcg_op2_wide, tcg_op2);
8145 tcg_temp_free_i32(tcg_op2);
8146 tcg_res[pass] = tcg_temp_new_i64();
8147 gen_neon_addl(size, (opcode == 3),
8148 tcg_res[pass], tcg_op1, tcg_op2_wide);
8149 tcg_temp_free_i64(tcg_op1);
8150 tcg_temp_free_i64(tcg_op2_wide);
8153 for (pass = 0; pass < 2; pass++) {
8154 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
8155 tcg_temp_free_i64(tcg_res[pass]);
8159 static void do_narrow_high_u32(TCGv_i32 res, TCGv_i64 in)
8161 tcg_gen_shri_i64(in, in, 32);
8162 tcg_gen_trunc_i64_i32(res, in);
8165 static void do_narrow_round_high_u32(TCGv_i32 res, TCGv_i64 in)
8167 tcg_gen_addi_i64(in, in, 1U << 31);
8168 do_narrow_high_u32(res, in);
8171 static void handle_3rd_narrowing(DisasContext *s, int is_q, int is_u, int size,
8172 int opcode, int rd, int rn, int rm)
8174 TCGv_i32 tcg_res[2];
8175 int part = is_q ? 2 : 0;
8176 int pass;
8178 for (pass = 0; pass < 2; pass++) {
8179 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8180 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
8181 TCGv_i64 tcg_wideres = tcg_temp_new_i64();
8182 static NeonGenNarrowFn * const narrowfns[3][2] = {
8183 { gen_helper_neon_narrow_high_u8,
8184 gen_helper_neon_narrow_round_high_u8 },
8185 { gen_helper_neon_narrow_high_u16,
8186 gen_helper_neon_narrow_round_high_u16 },
8187 { do_narrow_high_u32, do_narrow_round_high_u32 },
8189 NeonGenNarrowFn *gennarrow = narrowfns[size][is_u];
8191 read_vec_element(s, tcg_op1, rn, pass, MO_64);
8192 read_vec_element(s, tcg_op2, rm, pass, MO_64);
8194 gen_neon_addl(size, (opcode == 6), tcg_wideres, tcg_op1, tcg_op2);
8196 tcg_temp_free_i64(tcg_op1);
8197 tcg_temp_free_i64(tcg_op2);
8199 tcg_res[pass] = tcg_temp_new_i32();
8200 gennarrow(tcg_res[pass], tcg_wideres);
8201 tcg_temp_free_i64(tcg_wideres);
8204 for (pass = 0; pass < 2; pass++) {
8205 write_vec_element_i32(s, tcg_res[pass], rd, pass + part, MO_32);
8206 tcg_temp_free_i32(tcg_res[pass]);
8208 if (!is_q) {
8209 clear_vec_high(s, rd);
8213 static void handle_pmull_64(DisasContext *s, int is_q, int rd, int rn, int rm)
8215 /* PMULL of 64 x 64 -> 128 is an odd special case because it
8216 * is the only three-reg-diff instruction which produces a
8217 * 128-bit wide result from a single operation. However since
8218 * it's possible to calculate the two halves more or less
8219 * separately we just use two helper calls.
8221 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8222 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
8223 TCGv_i64 tcg_res = tcg_temp_new_i64();
8225 read_vec_element(s, tcg_op1, rn, is_q, MO_64);
8226 read_vec_element(s, tcg_op2, rm, is_q, MO_64);
8227 gen_helper_neon_pmull_64_lo(tcg_res, tcg_op1, tcg_op2);
8228 write_vec_element(s, tcg_res, rd, 0, MO_64);
8229 gen_helper_neon_pmull_64_hi(tcg_res, tcg_op1, tcg_op2);
8230 write_vec_element(s, tcg_res, rd, 1, MO_64);
8232 tcg_temp_free_i64(tcg_op1);
8233 tcg_temp_free_i64(tcg_op2);
8234 tcg_temp_free_i64(tcg_res);
8237 /* C3.6.15 AdvSIMD three different
8238 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 0
8239 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
8240 * | 0 | Q | U | 0 1 1 1 0 | size | 1 | Rm | opcode | 0 0 | Rn | Rd |
8241 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
8243 static void disas_simd_three_reg_diff(DisasContext *s, uint32_t insn)
8245 /* Instructions in this group fall into three basic classes
8246 * (in each case with the operation working on each element in
8247 * the input vectors):
8248 * (1) widening 64 x 64 -> 128 (with possibly Vd as an extra
8249 * 128 bit input)
8250 * (2) wide 64 x 128 -> 128
8251 * (3) narrowing 128 x 128 -> 64
8252 * Here we do initial decode, catch unallocated cases and
8253 * dispatch to separate functions for each class.
8255 int is_q = extract32(insn, 30, 1);
8256 int is_u = extract32(insn, 29, 1);
8257 int size = extract32(insn, 22, 2);
8258 int opcode = extract32(insn, 12, 4);
8259 int rm = extract32(insn, 16, 5);
8260 int rn = extract32(insn, 5, 5);
8261 int rd = extract32(insn, 0, 5);
8263 switch (opcode) {
8264 case 1: /* SADDW, SADDW2, UADDW, UADDW2 */
8265 case 3: /* SSUBW, SSUBW2, USUBW, USUBW2 */
8266 /* 64 x 128 -> 128 */
8267 if (size == 3) {
8268 unallocated_encoding(s);
8269 return;
8271 handle_3rd_wide(s, is_q, is_u, size, opcode, rd, rn, rm);
8272 break;
8273 case 4: /* ADDHN, ADDHN2, RADDHN, RADDHN2 */
8274 case 6: /* SUBHN, SUBHN2, RSUBHN, RSUBHN2 */
8275 /* 128 x 128 -> 64 */
8276 if (size == 3) {
8277 unallocated_encoding(s);
8278 return;
8280 handle_3rd_narrowing(s, is_q, is_u, size, opcode, rd, rn, rm);
8281 break;
8282 case 14: /* PMULL, PMULL2 */
8283 if (is_u || size == 1 || size == 2) {
8284 unallocated_encoding(s);
8285 return;
8287 if (size == 3) {
8288 if (!arm_dc_feature(s, ARM_FEATURE_V8_AES)) {
8289 unallocated_encoding(s);
8290 return;
8292 handle_pmull_64(s, is_q, rd, rn, rm);
8293 return;
8295 goto is_widening;
8296 case 9: /* SQDMLAL, SQDMLAL2 */
8297 case 11: /* SQDMLSL, SQDMLSL2 */
8298 case 13: /* SQDMULL, SQDMULL2 */
8299 if (is_u || size == 0) {
8300 unallocated_encoding(s);
8301 return;
8303 /* fall through */
8304 case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
8305 case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
8306 case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
8307 case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
8308 case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
8309 case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
8310 case 12: /* SMULL, SMULL2, UMULL, UMULL2 */
8311 /* 64 x 64 -> 128 */
8312 if (size == 3) {
8313 unallocated_encoding(s);
8314 return;
8316 is_widening:
8317 handle_3rd_widening(s, is_q, is_u, size, opcode, rd, rn, rm);
8318 break;
8319 default:
8320 /* opcode 15 not allocated */
8321 unallocated_encoding(s);
8322 break;
8326 /* Logic op (opcode == 3) subgroup of C3.6.16. */
8327 static void disas_simd_3same_logic(DisasContext *s, uint32_t insn)
8329 int rd = extract32(insn, 0, 5);
8330 int rn = extract32(insn, 5, 5);
8331 int rm = extract32(insn, 16, 5);
8332 int size = extract32(insn, 22, 2);
8333 bool is_u = extract32(insn, 29, 1);
8334 bool is_q = extract32(insn, 30, 1);
8335 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8336 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
8337 TCGv_i64 tcg_res[2];
8338 int pass;
8340 tcg_res[0] = tcg_temp_new_i64();
8341 tcg_res[1] = tcg_temp_new_i64();
8343 for (pass = 0; pass < (is_q ? 2 : 1); pass++) {
8344 read_vec_element(s, tcg_op1, rn, pass, MO_64);
8345 read_vec_element(s, tcg_op2, rm, pass, MO_64);
8347 if (!is_u) {
8348 switch (size) {
8349 case 0: /* AND */
8350 tcg_gen_and_i64(tcg_res[pass], tcg_op1, tcg_op2);
8351 break;
8352 case 1: /* BIC */
8353 tcg_gen_andc_i64(tcg_res[pass], tcg_op1, tcg_op2);
8354 break;
8355 case 2: /* ORR */
8356 tcg_gen_or_i64(tcg_res[pass], tcg_op1, tcg_op2);
8357 break;
8358 case 3: /* ORN */
8359 tcg_gen_orc_i64(tcg_res[pass], tcg_op1, tcg_op2);
8360 break;
8362 } else {
8363 if (size != 0) {
8364 /* B* ops need res loaded to operate on */
8365 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
8368 switch (size) {
8369 case 0: /* EOR */
8370 tcg_gen_xor_i64(tcg_res[pass], tcg_op1, tcg_op2);
8371 break;
8372 case 1: /* BSL bitwise select */
8373 tcg_gen_xor_i64(tcg_op1, tcg_op1, tcg_op2);
8374 tcg_gen_and_i64(tcg_op1, tcg_op1, tcg_res[pass]);
8375 tcg_gen_xor_i64(tcg_res[pass], tcg_op2, tcg_op1);
8376 break;
8377 case 2: /* BIT, bitwise insert if true */
8378 tcg_gen_xor_i64(tcg_op1, tcg_op1, tcg_res[pass]);
8379 tcg_gen_and_i64(tcg_op1, tcg_op1, tcg_op2);
8380 tcg_gen_xor_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
8381 break;
8382 case 3: /* BIF, bitwise insert if false */
8383 tcg_gen_xor_i64(tcg_op1, tcg_op1, tcg_res[pass]);
8384 tcg_gen_andc_i64(tcg_op1, tcg_op1, tcg_op2);
8385 tcg_gen_xor_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
8386 break;
8391 write_vec_element(s, tcg_res[0], rd, 0, MO_64);
8392 if (!is_q) {
8393 tcg_gen_movi_i64(tcg_res[1], 0);
8395 write_vec_element(s, tcg_res[1], rd, 1, MO_64);
8397 tcg_temp_free_i64(tcg_op1);
8398 tcg_temp_free_i64(tcg_op2);
8399 tcg_temp_free_i64(tcg_res[0]);
8400 tcg_temp_free_i64(tcg_res[1]);
8403 /* Helper functions for 32 bit comparisons */
8404 static void gen_max_s32(TCGv_i32 res, TCGv_i32 op1, TCGv_i32 op2)
8406 tcg_gen_movcond_i32(TCG_COND_GE, res, op1, op2, op1, op2);
8409 static void gen_max_u32(TCGv_i32 res, TCGv_i32 op1, TCGv_i32 op2)
8411 tcg_gen_movcond_i32(TCG_COND_GEU, res, op1, op2, op1, op2);
8414 static void gen_min_s32(TCGv_i32 res, TCGv_i32 op1, TCGv_i32 op2)
8416 tcg_gen_movcond_i32(TCG_COND_LE, res, op1, op2, op1, op2);
8419 static void gen_min_u32(TCGv_i32 res, TCGv_i32 op1, TCGv_i32 op2)
8421 tcg_gen_movcond_i32(TCG_COND_LEU, res, op1, op2, op1, op2);
8424 /* Pairwise op subgroup of C3.6.16.
8426 * This is called directly or via the handle_3same_float for float pairwise
8427 * operations where the opcode and size are calculated differently.
8429 static void handle_simd_3same_pair(DisasContext *s, int is_q, int u, int opcode,
8430 int size, int rn, int rm, int rd)
8432 TCGv_ptr fpst;
8433 int pass;
8435 /* Floating point operations need fpst */
8436 if (opcode >= 0x58) {
8437 fpst = get_fpstatus_ptr();
8438 } else {
8439 TCGV_UNUSED_PTR(fpst);
8442 /* These operations work on the concatenated rm:rn, with each pair of
8443 * adjacent elements being operated on to produce an element in the result.
8445 if (size == 3) {
8446 TCGv_i64 tcg_res[2];
8448 for (pass = 0; pass < 2; pass++) {
8449 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8450 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
8451 int passreg = (pass == 0) ? rn : rm;
8453 read_vec_element(s, tcg_op1, passreg, 0, MO_64);
8454 read_vec_element(s, tcg_op2, passreg, 1, MO_64);
8455 tcg_res[pass] = tcg_temp_new_i64();
8457 switch (opcode) {
8458 case 0x17: /* ADDP */
8459 tcg_gen_add_i64(tcg_res[pass], tcg_op1, tcg_op2);
8460 break;
8461 case 0x58: /* FMAXNMP */
8462 gen_helper_vfp_maxnumd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
8463 break;
8464 case 0x5a: /* FADDP */
8465 gen_helper_vfp_addd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
8466 break;
8467 case 0x5e: /* FMAXP */
8468 gen_helper_vfp_maxd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
8469 break;
8470 case 0x78: /* FMINNMP */
8471 gen_helper_vfp_minnumd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
8472 break;
8473 case 0x7e: /* FMINP */
8474 gen_helper_vfp_mind(tcg_res[pass], tcg_op1, tcg_op2, fpst);
8475 break;
8476 default:
8477 g_assert_not_reached();
8480 tcg_temp_free_i64(tcg_op1);
8481 tcg_temp_free_i64(tcg_op2);
8484 for (pass = 0; pass < 2; pass++) {
8485 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
8486 tcg_temp_free_i64(tcg_res[pass]);
8488 } else {
8489 int maxpass = is_q ? 4 : 2;
8490 TCGv_i32 tcg_res[4];
8492 for (pass = 0; pass < maxpass; pass++) {
8493 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
8494 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
8495 NeonGenTwoOpFn *genfn = NULL;
8496 int passreg = pass < (maxpass / 2) ? rn : rm;
8497 int passelt = (is_q && (pass & 1)) ? 2 : 0;
8499 read_vec_element_i32(s, tcg_op1, passreg, passelt, MO_32);
8500 read_vec_element_i32(s, tcg_op2, passreg, passelt + 1, MO_32);
8501 tcg_res[pass] = tcg_temp_new_i32();
8503 switch (opcode) {
8504 case 0x17: /* ADDP */
8506 static NeonGenTwoOpFn * const fns[3] = {
8507 gen_helper_neon_padd_u8,
8508 gen_helper_neon_padd_u16,
8509 tcg_gen_add_i32,
8511 genfn = fns[size];
8512 break;
8514 case 0x14: /* SMAXP, UMAXP */
8516 static NeonGenTwoOpFn * const fns[3][2] = {
8517 { gen_helper_neon_pmax_s8, gen_helper_neon_pmax_u8 },
8518 { gen_helper_neon_pmax_s16, gen_helper_neon_pmax_u16 },
8519 { gen_max_s32, gen_max_u32 },
8521 genfn = fns[size][u];
8522 break;
8524 case 0x15: /* SMINP, UMINP */
8526 static NeonGenTwoOpFn * const fns[3][2] = {
8527 { gen_helper_neon_pmin_s8, gen_helper_neon_pmin_u8 },
8528 { gen_helper_neon_pmin_s16, gen_helper_neon_pmin_u16 },
8529 { gen_min_s32, gen_min_u32 },
8531 genfn = fns[size][u];
8532 break;
8534 /* The FP operations are all on single floats (32 bit) */
8535 case 0x58: /* FMAXNMP */
8536 gen_helper_vfp_maxnums(tcg_res[pass], tcg_op1, tcg_op2, fpst);
8537 break;
8538 case 0x5a: /* FADDP */
8539 gen_helper_vfp_adds(tcg_res[pass], tcg_op1, tcg_op2, fpst);
8540 break;
8541 case 0x5e: /* FMAXP */
8542 gen_helper_vfp_maxs(tcg_res[pass], tcg_op1, tcg_op2, fpst);
8543 break;
8544 case 0x78: /* FMINNMP */
8545 gen_helper_vfp_minnums(tcg_res[pass], tcg_op1, tcg_op2, fpst);
8546 break;
8547 case 0x7e: /* FMINP */
8548 gen_helper_vfp_mins(tcg_res[pass], tcg_op1, tcg_op2, fpst);
8549 break;
8550 default:
8551 g_assert_not_reached();
8554 /* FP ops called directly, otherwise call now */
8555 if (genfn) {
8556 genfn(tcg_res[pass], tcg_op1, tcg_op2);
8559 tcg_temp_free_i32(tcg_op1);
8560 tcg_temp_free_i32(tcg_op2);
8563 for (pass = 0; pass < maxpass; pass++) {
8564 write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_32);
8565 tcg_temp_free_i32(tcg_res[pass]);
8567 if (!is_q) {
8568 clear_vec_high(s, rd);
8572 if (!TCGV_IS_UNUSED_PTR(fpst)) {
8573 tcg_temp_free_ptr(fpst);
8577 /* Floating point op subgroup of C3.6.16. */
8578 static void disas_simd_3same_float(DisasContext *s, uint32_t insn)
8580 /* For floating point ops, the U, size[1] and opcode bits
8581 * together indicate the operation. size[0] indicates single
8582 * or double.
8584 int fpopcode = extract32(insn, 11, 5)
8585 | (extract32(insn, 23, 1) << 5)
8586 | (extract32(insn, 29, 1) << 6);
8587 int is_q = extract32(insn, 30, 1);
8588 int size = extract32(insn, 22, 1);
8589 int rm = extract32(insn, 16, 5);
8590 int rn = extract32(insn, 5, 5);
8591 int rd = extract32(insn, 0, 5);
8593 int datasize = is_q ? 128 : 64;
8594 int esize = 32 << size;
8595 int elements = datasize / esize;
8597 if (size == 1 && !is_q) {
8598 unallocated_encoding(s);
8599 return;
8602 switch (fpopcode) {
8603 case 0x58: /* FMAXNMP */
8604 case 0x5a: /* FADDP */
8605 case 0x5e: /* FMAXP */
8606 case 0x78: /* FMINNMP */
8607 case 0x7e: /* FMINP */
8608 if (size && !is_q) {
8609 unallocated_encoding(s);
8610 return;
8612 handle_simd_3same_pair(s, is_q, 0, fpopcode, size ? MO_64 : MO_32,
8613 rn, rm, rd);
8614 return;
8615 case 0x1b: /* FMULX */
8616 case 0x1f: /* FRECPS */
8617 case 0x3f: /* FRSQRTS */
8618 case 0x5d: /* FACGE */
8619 case 0x7d: /* FACGT */
8620 case 0x19: /* FMLA */
8621 case 0x39: /* FMLS */
8622 case 0x18: /* FMAXNM */
8623 case 0x1a: /* FADD */
8624 case 0x1c: /* FCMEQ */
8625 case 0x1e: /* FMAX */
8626 case 0x38: /* FMINNM */
8627 case 0x3a: /* FSUB */
8628 case 0x3e: /* FMIN */
8629 case 0x5b: /* FMUL */
8630 case 0x5c: /* FCMGE */
8631 case 0x5f: /* FDIV */
8632 case 0x7a: /* FABD */
8633 case 0x7c: /* FCMGT */
8634 handle_3same_float(s, size, elements, fpopcode, rd, rn, rm);
8635 return;
8636 default:
8637 unallocated_encoding(s);
8638 return;
8642 /* Integer op subgroup of C3.6.16. */
8643 static void disas_simd_3same_int(DisasContext *s, uint32_t insn)
8645 int is_q = extract32(insn, 30, 1);
8646 int u = extract32(insn, 29, 1);
8647 int size = extract32(insn, 22, 2);
8648 int opcode = extract32(insn, 11, 5);
8649 int rm = extract32(insn, 16, 5);
8650 int rn = extract32(insn, 5, 5);
8651 int rd = extract32(insn, 0, 5);
8652 int pass;
8654 switch (opcode) {
8655 case 0x13: /* MUL, PMUL */
8656 if (u && size != 0) {
8657 unallocated_encoding(s);
8658 return;
8660 /* fall through */
8661 case 0x0: /* SHADD, UHADD */
8662 case 0x2: /* SRHADD, URHADD */
8663 case 0x4: /* SHSUB, UHSUB */
8664 case 0xc: /* SMAX, UMAX */
8665 case 0xd: /* SMIN, UMIN */
8666 case 0xe: /* SABD, UABD */
8667 case 0xf: /* SABA, UABA */
8668 case 0x12: /* MLA, MLS */
8669 if (size == 3) {
8670 unallocated_encoding(s);
8671 return;
8673 break;
8674 case 0x16: /* SQDMULH, SQRDMULH */
8675 if (size == 0 || size == 3) {
8676 unallocated_encoding(s);
8677 return;
8679 break;
8680 default:
8681 if (size == 3 && !is_q) {
8682 unallocated_encoding(s);
8683 return;
8685 break;
8688 if (size == 3) {
8689 for (pass = 0; pass < (is_q ? 2 : 1); pass++) {
8690 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8691 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
8692 TCGv_i64 tcg_res = tcg_temp_new_i64();
8694 read_vec_element(s, tcg_op1, rn, pass, MO_64);
8695 read_vec_element(s, tcg_op2, rm, pass, MO_64);
8697 handle_3same_64(s, opcode, u, tcg_res, tcg_op1, tcg_op2);
8699 write_vec_element(s, tcg_res, rd, pass, MO_64);
8701 tcg_temp_free_i64(tcg_res);
8702 tcg_temp_free_i64(tcg_op1);
8703 tcg_temp_free_i64(tcg_op2);
8705 } else {
8706 for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
8707 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
8708 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
8709 TCGv_i32 tcg_res = tcg_temp_new_i32();
8710 NeonGenTwoOpFn *genfn = NULL;
8711 NeonGenTwoOpEnvFn *genenvfn = NULL;
8713 read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
8714 read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
8716 switch (opcode) {
8717 case 0x0: /* SHADD, UHADD */
8719 static NeonGenTwoOpFn * const fns[3][2] = {
8720 { gen_helper_neon_hadd_s8, gen_helper_neon_hadd_u8 },
8721 { gen_helper_neon_hadd_s16, gen_helper_neon_hadd_u16 },
8722 { gen_helper_neon_hadd_s32, gen_helper_neon_hadd_u32 },
8724 genfn = fns[size][u];
8725 break;
8727 case 0x1: /* SQADD, UQADD */
8729 static NeonGenTwoOpEnvFn * const fns[3][2] = {
8730 { gen_helper_neon_qadd_s8, gen_helper_neon_qadd_u8 },
8731 { gen_helper_neon_qadd_s16, gen_helper_neon_qadd_u16 },
8732 { gen_helper_neon_qadd_s32, gen_helper_neon_qadd_u32 },
8734 genenvfn = fns[size][u];
8735 break;
8737 case 0x2: /* SRHADD, URHADD */
8739 static NeonGenTwoOpFn * const fns[3][2] = {
8740 { gen_helper_neon_rhadd_s8, gen_helper_neon_rhadd_u8 },
8741 { gen_helper_neon_rhadd_s16, gen_helper_neon_rhadd_u16 },
8742 { gen_helper_neon_rhadd_s32, gen_helper_neon_rhadd_u32 },
8744 genfn = fns[size][u];
8745 break;
8747 case 0x4: /* SHSUB, UHSUB */
8749 static NeonGenTwoOpFn * const fns[3][2] = {
8750 { gen_helper_neon_hsub_s8, gen_helper_neon_hsub_u8 },
8751 { gen_helper_neon_hsub_s16, gen_helper_neon_hsub_u16 },
8752 { gen_helper_neon_hsub_s32, gen_helper_neon_hsub_u32 },
8754 genfn = fns[size][u];
8755 break;
8757 case 0x5: /* SQSUB, UQSUB */
8759 static NeonGenTwoOpEnvFn * const fns[3][2] = {
8760 { gen_helper_neon_qsub_s8, gen_helper_neon_qsub_u8 },
8761 { gen_helper_neon_qsub_s16, gen_helper_neon_qsub_u16 },
8762 { gen_helper_neon_qsub_s32, gen_helper_neon_qsub_u32 },
8764 genenvfn = fns[size][u];
8765 break;
8767 case 0x6: /* CMGT, CMHI */
8769 static NeonGenTwoOpFn * const fns[3][2] = {
8770 { gen_helper_neon_cgt_s8, gen_helper_neon_cgt_u8 },
8771 { gen_helper_neon_cgt_s16, gen_helper_neon_cgt_u16 },
8772 { gen_helper_neon_cgt_s32, gen_helper_neon_cgt_u32 },
8774 genfn = fns[size][u];
8775 break;
8777 case 0x7: /* CMGE, CMHS */
8779 static NeonGenTwoOpFn * const fns[3][2] = {
8780 { gen_helper_neon_cge_s8, gen_helper_neon_cge_u8 },
8781 { gen_helper_neon_cge_s16, gen_helper_neon_cge_u16 },
8782 { gen_helper_neon_cge_s32, gen_helper_neon_cge_u32 },
8784 genfn = fns[size][u];
8785 break;
8787 case 0x8: /* SSHL, USHL */
8789 static NeonGenTwoOpFn * const fns[3][2] = {
8790 { gen_helper_neon_shl_s8, gen_helper_neon_shl_u8 },
8791 { gen_helper_neon_shl_s16, gen_helper_neon_shl_u16 },
8792 { gen_helper_neon_shl_s32, gen_helper_neon_shl_u32 },
8794 genfn = fns[size][u];
8795 break;
8797 case 0x9: /* SQSHL, UQSHL */
8799 static NeonGenTwoOpEnvFn * const fns[3][2] = {
8800 { gen_helper_neon_qshl_s8, gen_helper_neon_qshl_u8 },
8801 { gen_helper_neon_qshl_s16, gen_helper_neon_qshl_u16 },
8802 { gen_helper_neon_qshl_s32, gen_helper_neon_qshl_u32 },
8804 genenvfn = fns[size][u];
8805 break;
8807 case 0xa: /* SRSHL, URSHL */
8809 static NeonGenTwoOpFn * const fns[3][2] = {
8810 { gen_helper_neon_rshl_s8, gen_helper_neon_rshl_u8 },
8811 { gen_helper_neon_rshl_s16, gen_helper_neon_rshl_u16 },
8812 { gen_helper_neon_rshl_s32, gen_helper_neon_rshl_u32 },
8814 genfn = fns[size][u];
8815 break;
8817 case 0xb: /* SQRSHL, UQRSHL */
8819 static NeonGenTwoOpEnvFn * const fns[3][2] = {
8820 { gen_helper_neon_qrshl_s8, gen_helper_neon_qrshl_u8 },
8821 { gen_helper_neon_qrshl_s16, gen_helper_neon_qrshl_u16 },
8822 { gen_helper_neon_qrshl_s32, gen_helper_neon_qrshl_u32 },
8824 genenvfn = fns[size][u];
8825 break;
8827 case 0xc: /* SMAX, UMAX */
8829 static NeonGenTwoOpFn * const fns[3][2] = {
8830 { gen_helper_neon_max_s8, gen_helper_neon_max_u8 },
8831 { gen_helper_neon_max_s16, gen_helper_neon_max_u16 },
8832 { gen_max_s32, gen_max_u32 },
8834 genfn = fns[size][u];
8835 break;
8838 case 0xd: /* SMIN, UMIN */
8840 static NeonGenTwoOpFn * const fns[3][2] = {
8841 { gen_helper_neon_min_s8, gen_helper_neon_min_u8 },
8842 { gen_helper_neon_min_s16, gen_helper_neon_min_u16 },
8843 { gen_min_s32, gen_min_u32 },
8845 genfn = fns[size][u];
8846 break;
8848 case 0xe: /* SABD, UABD */
8849 case 0xf: /* SABA, UABA */
8851 static NeonGenTwoOpFn * const fns[3][2] = {
8852 { gen_helper_neon_abd_s8, gen_helper_neon_abd_u8 },
8853 { gen_helper_neon_abd_s16, gen_helper_neon_abd_u16 },
8854 { gen_helper_neon_abd_s32, gen_helper_neon_abd_u32 },
8856 genfn = fns[size][u];
8857 break;
8859 case 0x10: /* ADD, SUB */
8861 static NeonGenTwoOpFn * const fns[3][2] = {
8862 { gen_helper_neon_add_u8, gen_helper_neon_sub_u8 },
8863 { gen_helper_neon_add_u16, gen_helper_neon_sub_u16 },
8864 { tcg_gen_add_i32, tcg_gen_sub_i32 },
8866 genfn = fns[size][u];
8867 break;
8869 case 0x11: /* CMTST, CMEQ */
8871 static NeonGenTwoOpFn * const fns[3][2] = {
8872 { gen_helper_neon_tst_u8, gen_helper_neon_ceq_u8 },
8873 { gen_helper_neon_tst_u16, gen_helper_neon_ceq_u16 },
8874 { gen_helper_neon_tst_u32, gen_helper_neon_ceq_u32 },
8876 genfn = fns[size][u];
8877 break;
8879 case 0x13: /* MUL, PMUL */
8880 if (u) {
8881 /* PMUL */
8882 assert(size == 0);
8883 genfn = gen_helper_neon_mul_p8;
8884 break;
8886 /* fall through : MUL */
8887 case 0x12: /* MLA, MLS */
8889 static NeonGenTwoOpFn * const fns[3] = {
8890 gen_helper_neon_mul_u8,
8891 gen_helper_neon_mul_u16,
8892 tcg_gen_mul_i32,
8894 genfn = fns[size];
8895 break;
8897 case 0x16: /* SQDMULH, SQRDMULH */
8899 static NeonGenTwoOpEnvFn * const fns[2][2] = {
8900 { gen_helper_neon_qdmulh_s16, gen_helper_neon_qrdmulh_s16 },
8901 { gen_helper_neon_qdmulh_s32, gen_helper_neon_qrdmulh_s32 },
8903 assert(size == 1 || size == 2);
8904 genenvfn = fns[size - 1][u];
8905 break;
8907 default:
8908 g_assert_not_reached();
8911 if (genenvfn) {
8912 genenvfn(tcg_res, cpu_env, tcg_op1, tcg_op2);
8913 } else {
8914 genfn(tcg_res, tcg_op1, tcg_op2);
8917 if (opcode == 0xf || opcode == 0x12) {
8918 /* SABA, UABA, MLA, MLS: accumulating ops */
8919 static NeonGenTwoOpFn * const fns[3][2] = {
8920 { gen_helper_neon_add_u8, gen_helper_neon_sub_u8 },
8921 { gen_helper_neon_add_u16, gen_helper_neon_sub_u16 },
8922 { tcg_gen_add_i32, tcg_gen_sub_i32 },
8924 bool is_sub = (opcode == 0x12 && u); /* MLS */
8926 genfn = fns[size][is_sub];
8927 read_vec_element_i32(s, tcg_op1, rd, pass, MO_32);
8928 genfn(tcg_res, tcg_op1, tcg_res);
8931 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
8933 tcg_temp_free_i32(tcg_res);
8934 tcg_temp_free_i32(tcg_op1);
8935 tcg_temp_free_i32(tcg_op2);
8939 if (!is_q) {
8940 clear_vec_high(s, rd);
8944 /* C3.6.16 AdvSIMD three same
8945 * 31 30 29 28 24 23 22 21 20 16 15 11 10 9 5 4 0
8946 * +---+---+---+-----------+------+---+------+--------+---+------+------+
8947 * | 0 | Q | U | 0 1 1 1 0 | size | 1 | Rm | opcode | 1 | Rn | Rd |
8948 * +---+---+---+-----------+------+---+------+--------+---+------+------+
8950 static void disas_simd_three_reg_same(DisasContext *s, uint32_t insn)
8952 int opcode = extract32(insn, 11, 5);
8954 switch (opcode) {
8955 case 0x3: /* logic ops */
8956 disas_simd_3same_logic(s, insn);
8957 break;
8958 case 0x17: /* ADDP */
8959 case 0x14: /* SMAXP, UMAXP */
8960 case 0x15: /* SMINP, UMINP */
8962 /* Pairwise operations */
8963 int is_q = extract32(insn, 30, 1);
8964 int u = extract32(insn, 29, 1);
8965 int size = extract32(insn, 22, 2);
8966 int rm = extract32(insn, 16, 5);
8967 int rn = extract32(insn, 5, 5);
8968 int rd = extract32(insn, 0, 5);
8969 if (opcode == 0x17) {
8970 if (u || (size == 3 && !is_q)) {
8971 unallocated_encoding(s);
8972 return;
8974 } else {
8975 if (size == 3) {
8976 unallocated_encoding(s);
8977 return;
8980 handle_simd_3same_pair(s, is_q, u, opcode, size, rn, rm, rd);
8981 break;
8983 case 0x18 ... 0x31:
8984 /* floating point ops, sz[1] and U are part of opcode */
8985 disas_simd_3same_float(s, insn);
8986 break;
8987 default:
8988 disas_simd_3same_int(s, insn);
8989 break;
8993 static void handle_2misc_widening(DisasContext *s, int opcode, bool is_q,
8994 int size, int rn, int rd)
8996 /* Handle 2-reg-misc ops which are widening (so each size element
8997 * in the source becomes a 2*size element in the destination.
8998 * The only instruction like this is FCVTL.
9000 int pass;
9002 if (size == 3) {
9003 /* 32 -> 64 bit fp conversion */
9004 TCGv_i64 tcg_res[2];
9005 int srcelt = is_q ? 2 : 0;
9007 for (pass = 0; pass < 2; pass++) {
9008 TCGv_i32 tcg_op = tcg_temp_new_i32();
9009 tcg_res[pass] = tcg_temp_new_i64();
9011 read_vec_element_i32(s, tcg_op, rn, srcelt + pass, MO_32);
9012 gen_helper_vfp_fcvtds(tcg_res[pass], tcg_op, cpu_env);
9013 tcg_temp_free_i32(tcg_op);
9015 for (pass = 0; pass < 2; pass++) {
9016 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
9017 tcg_temp_free_i64(tcg_res[pass]);
9019 } else {
9020 /* 16 -> 32 bit fp conversion */
9021 int srcelt = is_q ? 4 : 0;
9022 TCGv_i32 tcg_res[4];
9024 for (pass = 0; pass < 4; pass++) {
9025 tcg_res[pass] = tcg_temp_new_i32();
9027 read_vec_element_i32(s, tcg_res[pass], rn, srcelt + pass, MO_16);
9028 gen_helper_vfp_fcvt_f16_to_f32(tcg_res[pass], tcg_res[pass],
9029 cpu_env);
9031 for (pass = 0; pass < 4; pass++) {
9032 write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_32);
9033 tcg_temp_free_i32(tcg_res[pass]);
9038 static void handle_rev(DisasContext *s, int opcode, bool u,
9039 bool is_q, int size, int rn, int rd)
9041 int op = (opcode << 1) | u;
9042 int opsz = op + size;
9043 int grp_size = 3 - opsz;
9044 int dsize = is_q ? 128 : 64;
9045 int i;
9047 if (opsz >= 3) {
9048 unallocated_encoding(s);
9049 return;
9052 if (size == 0) {
9053 /* Special case bytes, use bswap op on each group of elements */
9054 int groups = dsize / (8 << grp_size);
9056 for (i = 0; i < groups; i++) {
9057 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
9059 read_vec_element(s, tcg_tmp, rn, i, grp_size);
9060 switch (grp_size) {
9061 case MO_16:
9062 tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp);
9063 break;
9064 case MO_32:
9065 tcg_gen_bswap32_i64(tcg_tmp, tcg_tmp);
9066 break;
9067 case MO_64:
9068 tcg_gen_bswap64_i64(tcg_tmp, tcg_tmp);
9069 break;
9070 default:
9071 g_assert_not_reached();
9073 write_vec_element(s, tcg_tmp, rd, i, grp_size);
9074 tcg_temp_free_i64(tcg_tmp);
9076 if (!is_q) {
9077 clear_vec_high(s, rd);
9079 } else {
9080 int revmask = (1 << grp_size) - 1;
9081 int esize = 8 << size;
9082 int elements = dsize / esize;
9083 TCGv_i64 tcg_rn = tcg_temp_new_i64();
9084 TCGv_i64 tcg_rd = tcg_const_i64(0);
9085 TCGv_i64 tcg_rd_hi = tcg_const_i64(0);
9087 for (i = 0; i < elements; i++) {
9088 int e_rev = (i & 0xf) ^ revmask;
9089 int off = e_rev * esize;
9090 read_vec_element(s, tcg_rn, rn, i, size);
9091 if (off >= 64) {
9092 tcg_gen_deposit_i64(tcg_rd_hi, tcg_rd_hi,
9093 tcg_rn, off - 64, esize);
9094 } else {
9095 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, off, esize);
9098 write_vec_element(s, tcg_rd, rd, 0, MO_64);
9099 write_vec_element(s, tcg_rd_hi, rd, 1, MO_64);
9101 tcg_temp_free_i64(tcg_rd_hi);
9102 tcg_temp_free_i64(tcg_rd);
9103 tcg_temp_free_i64(tcg_rn);
9107 static void handle_2misc_pairwise(DisasContext *s, int opcode, bool u,
9108 bool is_q, int size, int rn, int rd)
9110 /* Implement the pairwise operations from 2-misc:
9111 * SADDLP, UADDLP, SADALP, UADALP.
9112 * These all add pairs of elements in the input to produce a
9113 * double-width result element in the output (possibly accumulating).
9115 bool accum = (opcode == 0x6);
9116 int maxpass = is_q ? 2 : 1;
9117 int pass;
9118 TCGv_i64 tcg_res[2];
9120 if (size == 2) {
9121 /* 32 + 32 -> 64 op */
9122 TCGMemOp memop = size + (u ? 0 : MO_SIGN);
9124 for (pass = 0; pass < maxpass; pass++) {
9125 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
9126 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
9128 tcg_res[pass] = tcg_temp_new_i64();
9130 read_vec_element(s, tcg_op1, rn, pass * 2, memop);
9131 read_vec_element(s, tcg_op2, rn, pass * 2 + 1, memop);
9132 tcg_gen_add_i64(tcg_res[pass], tcg_op1, tcg_op2);
9133 if (accum) {
9134 read_vec_element(s, tcg_op1, rd, pass, MO_64);
9135 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
9138 tcg_temp_free_i64(tcg_op1);
9139 tcg_temp_free_i64(tcg_op2);
9141 } else {
9142 for (pass = 0; pass < maxpass; pass++) {
9143 TCGv_i64 tcg_op = tcg_temp_new_i64();
9144 NeonGenOneOpFn *genfn;
9145 static NeonGenOneOpFn * const fns[2][2] = {
9146 { gen_helper_neon_addlp_s8, gen_helper_neon_addlp_u8 },
9147 { gen_helper_neon_addlp_s16, gen_helper_neon_addlp_u16 },
9150 genfn = fns[size][u];
9152 tcg_res[pass] = tcg_temp_new_i64();
9154 read_vec_element(s, tcg_op, rn, pass, MO_64);
9155 genfn(tcg_res[pass], tcg_op);
9157 if (accum) {
9158 read_vec_element(s, tcg_op, rd, pass, MO_64);
9159 if (size == 0) {
9160 gen_helper_neon_addl_u16(tcg_res[pass],
9161 tcg_res[pass], tcg_op);
9162 } else {
9163 gen_helper_neon_addl_u32(tcg_res[pass],
9164 tcg_res[pass], tcg_op);
9167 tcg_temp_free_i64(tcg_op);
9170 if (!is_q) {
9171 tcg_res[1] = tcg_const_i64(0);
9173 for (pass = 0; pass < 2; pass++) {
9174 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
9175 tcg_temp_free_i64(tcg_res[pass]);
9179 static void handle_shll(DisasContext *s, bool is_q, int size, int rn, int rd)
9181 /* Implement SHLL and SHLL2 */
9182 int pass;
9183 int part = is_q ? 2 : 0;
9184 TCGv_i64 tcg_res[2];
9186 for (pass = 0; pass < 2; pass++) {
9187 static NeonGenWidenFn * const widenfns[3] = {
9188 gen_helper_neon_widen_u8,
9189 gen_helper_neon_widen_u16,
9190 tcg_gen_extu_i32_i64,
9192 NeonGenWidenFn *widenfn = widenfns[size];
9193 TCGv_i32 tcg_op = tcg_temp_new_i32();
9195 read_vec_element_i32(s, tcg_op, rn, part + pass, MO_32);
9196 tcg_res[pass] = tcg_temp_new_i64();
9197 widenfn(tcg_res[pass], tcg_op);
9198 tcg_gen_shli_i64(tcg_res[pass], tcg_res[pass], 8 << size);
9200 tcg_temp_free_i32(tcg_op);
9203 for (pass = 0; pass < 2; pass++) {
9204 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
9205 tcg_temp_free_i64(tcg_res[pass]);
9209 /* C3.6.17 AdvSIMD two reg misc
9210 * 31 30 29 28 24 23 22 21 17 16 12 11 10 9 5 4 0
9211 * +---+---+---+-----------+------+-----------+--------+-----+------+------+
9212 * | 0 | Q | U | 0 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 | Rn | Rd |
9213 * +---+---+---+-----------+------+-----------+--------+-----+------+------+
9215 static void disas_simd_two_reg_misc(DisasContext *s, uint32_t insn)
9217 int size = extract32(insn, 22, 2);
9218 int opcode = extract32(insn, 12, 5);
9219 bool u = extract32(insn, 29, 1);
9220 bool is_q = extract32(insn, 30, 1);
9221 int rn = extract32(insn, 5, 5);
9222 int rd = extract32(insn, 0, 5);
9223 bool need_fpstatus = false;
9224 bool need_rmode = false;
9225 int rmode = -1;
9226 TCGv_i32 tcg_rmode;
9227 TCGv_ptr tcg_fpstatus;
9229 switch (opcode) {
9230 case 0x0: /* REV64, REV32 */
9231 case 0x1: /* REV16 */
9232 handle_rev(s, opcode, u, is_q, size, rn, rd);
9233 return;
9234 case 0x5: /* CNT, NOT, RBIT */
9235 if (u && size == 0) {
9236 /* NOT: adjust size so we can use the 64-bits-at-a-time loop. */
9237 size = 3;
9238 break;
9239 } else if (u && size == 1) {
9240 /* RBIT */
9241 break;
9242 } else if (!u && size == 0) {
9243 /* CNT */
9244 break;
9246 unallocated_encoding(s);
9247 return;
9248 case 0x12: /* XTN, XTN2, SQXTUN, SQXTUN2 */
9249 case 0x14: /* SQXTN, SQXTN2, UQXTN, UQXTN2 */
9250 if (size == 3) {
9251 unallocated_encoding(s);
9252 return;
9254 handle_2misc_narrow(s, false, opcode, u, is_q, size, rn, rd);
9255 return;
9256 case 0x4: /* CLS, CLZ */
9257 if (size == 3) {
9258 unallocated_encoding(s);
9259 return;
9261 break;
9262 case 0x2: /* SADDLP, UADDLP */
9263 case 0x6: /* SADALP, UADALP */
9264 if (size == 3) {
9265 unallocated_encoding(s);
9266 return;
9268 handle_2misc_pairwise(s, opcode, u, is_q, size, rn, rd);
9269 return;
9270 case 0x13: /* SHLL, SHLL2 */
9271 if (u == 0 || size == 3) {
9272 unallocated_encoding(s);
9273 return;
9275 handle_shll(s, is_q, size, rn, rd);
9276 return;
9277 case 0xa: /* CMLT */
9278 if (u == 1) {
9279 unallocated_encoding(s);
9280 return;
9282 /* fall through */
9283 case 0x8: /* CMGT, CMGE */
9284 case 0x9: /* CMEQ, CMLE */
9285 case 0xb: /* ABS, NEG */
9286 if (size == 3 && !is_q) {
9287 unallocated_encoding(s);
9288 return;
9290 break;
9291 case 0x3: /* SUQADD, USQADD */
9292 if (size == 3 && !is_q) {
9293 unallocated_encoding(s);
9294 return;
9296 handle_2misc_satacc(s, false, u, is_q, size, rn, rd);
9297 return;
9298 case 0x7: /* SQABS, SQNEG */
9299 if (size == 3 && !is_q) {
9300 unallocated_encoding(s);
9301 return;
9303 break;
9304 case 0xc ... 0xf:
9305 case 0x16 ... 0x1d:
9306 case 0x1f:
9308 /* Floating point: U, size[1] and opcode indicate operation;
9309 * size[0] indicates single or double precision.
9311 int is_double = extract32(size, 0, 1);
9312 opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
9313 size = is_double ? 3 : 2;
9314 switch (opcode) {
9315 case 0x2f: /* FABS */
9316 case 0x6f: /* FNEG */
9317 if (size == 3 && !is_q) {
9318 unallocated_encoding(s);
9319 return;
9321 break;
9322 case 0x1d: /* SCVTF */
9323 case 0x5d: /* UCVTF */
9325 bool is_signed = (opcode == 0x1d) ? true : false;
9326 int elements = is_double ? 2 : is_q ? 4 : 2;
9327 if (is_double && !is_q) {
9328 unallocated_encoding(s);
9329 return;
9331 handle_simd_intfp_conv(s, rd, rn, elements, is_signed, 0, size);
9332 return;
9334 case 0x2c: /* FCMGT (zero) */
9335 case 0x2d: /* FCMEQ (zero) */
9336 case 0x2e: /* FCMLT (zero) */
9337 case 0x6c: /* FCMGE (zero) */
9338 case 0x6d: /* FCMLE (zero) */
9339 if (size == 3 && !is_q) {
9340 unallocated_encoding(s);
9341 return;
9343 handle_2misc_fcmp_zero(s, opcode, false, u, is_q, size, rn, rd);
9344 return;
9345 case 0x7f: /* FSQRT */
9346 if (size == 3 && !is_q) {
9347 unallocated_encoding(s);
9348 return;
9350 break;
9351 case 0x1a: /* FCVTNS */
9352 case 0x1b: /* FCVTMS */
9353 case 0x3a: /* FCVTPS */
9354 case 0x3b: /* FCVTZS */
9355 case 0x5a: /* FCVTNU */
9356 case 0x5b: /* FCVTMU */
9357 case 0x7a: /* FCVTPU */
9358 case 0x7b: /* FCVTZU */
9359 need_fpstatus = true;
9360 need_rmode = true;
9361 rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
9362 if (size == 3 && !is_q) {
9363 unallocated_encoding(s);
9364 return;
9366 break;
9367 case 0x5c: /* FCVTAU */
9368 case 0x1c: /* FCVTAS */
9369 need_fpstatus = true;
9370 need_rmode = true;
9371 rmode = FPROUNDING_TIEAWAY;
9372 if (size == 3 && !is_q) {
9373 unallocated_encoding(s);
9374 return;
9376 break;
9377 case 0x3c: /* URECPE */
9378 if (size == 3) {
9379 unallocated_encoding(s);
9380 return;
9382 /* fall through */
9383 case 0x3d: /* FRECPE */
9384 case 0x7d: /* FRSQRTE */
9385 if (size == 3 && !is_q) {
9386 unallocated_encoding(s);
9387 return;
9389 handle_2misc_reciprocal(s, opcode, false, u, is_q, size, rn, rd);
9390 return;
9391 case 0x56: /* FCVTXN, FCVTXN2 */
9392 if (size == 2) {
9393 unallocated_encoding(s);
9394 return;
9396 /* fall through */
9397 case 0x16: /* FCVTN, FCVTN2 */
9398 /* handle_2misc_narrow does a 2*size -> size operation, but these
9399 * instructions encode the source size rather than dest size.
9401 handle_2misc_narrow(s, false, opcode, 0, is_q, size - 1, rn, rd);
9402 return;
9403 case 0x17: /* FCVTL, FCVTL2 */
9404 handle_2misc_widening(s, opcode, is_q, size, rn, rd);
9405 return;
9406 case 0x18: /* FRINTN */
9407 case 0x19: /* FRINTM */
9408 case 0x38: /* FRINTP */
9409 case 0x39: /* FRINTZ */
9410 need_rmode = true;
9411 rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
9412 /* fall through */
9413 case 0x59: /* FRINTX */
9414 case 0x79: /* FRINTI */
9415 need_fpstatus = true;
9416 if (size == 3 && !is_q) {
9417 unallocated_encoding(s);
9418 return;
9420 break;
9421 case 0x58: /* FRINTA */
9422 need_rmode = true;
9423 rmode = FPROUNDING_TIEAWAY;
9424 need_fpstatus = true;
9425 if (size == 3 && !is_q) {
9426 unallocated_encoding(s);
9427 return;
9429 break;
9430 case 0x7c: /* URSQRTE */
9431 if (size == 3) {
9432 unallocated_encoding(s);
9433 return;
9435 need_fpstatus = true;
9436 break;
9437 default:
9438 unallocated_encoding(s);
9439 return;
9441 break;
9443 default:
9444 unallocated_encoding(s);
9445 return;
9448 if (need_fpstatus) {
9449 tcg_fpstatus = get_fpstatus_ptr();
9450 } else {
9451 TCGV_UNUSED_PTR(tcg_fpstatus);
9453 if (need_rmode) {
9454 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rmode));
9455 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
9456 } else {
9457 TCGV_UNUSED_I32(tcg_rmode);
9460 if (size == 3) {
9461 /* All 64-bit element operations can be shared with scalar 2misc */
9462 int pass;
9464 for (pass = 0; pass < (is_q ? 2 : 1); pass++) {
9465 TCGv_i64 tcg_op = tcg_temp_new_i64();
9466 TCGv_i64 tcg_res = tcg_temp_new_i64();
9468 read_vec_element(s, tcg_op, rn, pass, MO_64);
9470 handle_2misc_64(s, opcode, u, tcg_res, tcg_op,
9471 tcg_rmode, tcg_fpstatus);
9473 write_vec_element(s, tcg_res, rd, pass, MO_64);
9475 tcg_temp_free_i64(tcg_res);
9476 tcg_temp_free_i64(tcg_op);
9478 } else {
9479 int pass;
9481 for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
9482 TCGv_i32 tcg_op = tcg_temp_new_i32();
9483 TCGv_i32 tcg_res = tcg_temp_new_i32();
9484 TCGCond cond;
9486 read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
9488 if (size == 2) {
9489 /* Special cases for 32 bit elements */
9490 switch (opcode) {
9491 case 0xa: /* CMLT */
9492 /* 32 bit integer comparison against zero, result is
9493 * test ? (2^32 - 1) : 0. We implement via setcond(test)
9494 * and inverting.
9496 cond = TCG_COND_LT;
9497 do_cmop:
9498 tcg_gen_setcondi_i32(cond, tcg_res, tcg_op, 0);
9499 tcg_gen_neg_i32(tcg_res, tcg_res);
9500 break;
9501 case 0x8: /* CMGT, CMGE */
9502 cond = u ? TCG_COND_GE : TCG_COND_GT;
9503 goto do_cmop;
9504 case 0x9: /* CMEQ, CMLE */
9505 cond = u ? TCG_COND_LE : TCG_COND_EQ;
9506 goto do_cmop;
9507 case 0x4: /* CLS */
9508 if (u) {
9509 gen_helper_clz32(tcg_res, tcg_op);
9510 } else {
9511 gen_helper_cls32(tcg_res, tcg_op);
9513 break;
9514 case 0x7: /* SQABS, SQNEG */
9515 if (u) {
9516 gen_helper_neon_qneg_s32(tcg_res, cpu_env, tcg_op);
9517 } else {
9518 gen_helper_neon_qabs_s32(tcg_res, cpu_env, tcg_op);
9520 break;
9521 case 0xb: /* ABS, NEG */
9522 if (u) {
9523 tcg_gen_neg_i32(tcg_res, tcg_op);
9524 } else {
9525 TCGv_i32 tcg_zero = tcg_const_i32(0);
9526 tcg_gen_neg_i32(tcg_res, tcg_op);
9527 tcg_gen_movcond_i32(TCG_COND_GT, tcg_res, tcg_op,
9528 tcg_zero, tcg_op, tcg_res);
9529 tcg_temp_free_i32(tcg_zero);
9531 break;
9532 case 0x2f: /* FABS */
9533 gen_helper_vfp_abss(tcg_res, tcg_op);
9534 break;
9535 case 0x6f: /* FNEG */
9536 gen_helper_vfp_negs(tcg_res, tcg_op);
9537 break;
9538 case 0x7f: /* FSQRT */
9539 gen_helper_vfp_sqrts(tcg_res, tcg_op, cpu_env);
9540 break;
9541 case 0x1a: /* FCVTNS */
9542 case 0x1b: /* FCVTMS */
9543 case 0x1c: /* FCVTAS */
9544 case 0x3a: /* FCVTPS */
9545 case 0x3b: /* FCVTZS */
9547 TCGv_i32 tcg_shift = tcg_const_i32(0);
9548 gen_helper_vfp_tosls(tcg_res, tcg_op,
9549 tcg_shift, tcg_fpstatus);
9550 tcg_temp_free_i32(tcg_shift);
9551 break;
9553 case 0x5a: /* FCVTNU */
9554 case 0x5b: /* FCVTMU */
9555 case 0x5c: /* FCVTAU */
9556 case 0x7a: /* FCVTPU */
9557 case 0x7b: /* FCVTZU */
9559 TCGv_i32 tcg_shift = tcg_const_i32(0);
9560 gen_helper_vfp_touls(tcg_res, tcg_op,
9561 tcg_shift, tcg_fpstatus);
9562 tcg_temp_free_i32(tcg_shift);
9563 break;
9565 case 0x18: /* FRINTN */
9566 case 0x19: /* FRINTM */
9567 case 0x38: /* FRINTP */
9568 case 0x39: /* FRINTZ */
9569 case 0x58: /* FRINTA */
9570 case 0x79: /* FRINTI */
9571 gen_helper_rints(tcg_res, tcg_op, tcg_fpstatus);
9572 break;
9573 case 0x59: /* FRINTX */
9574 gen_helper_rints_exact(tcg_res, tcg_op, tcg_fpstatus);
9575 break;
9576 case 0x7c: /* URSQRTE */
9577 gen_helper_rsqrte_u32(tcg_res, tcg_op, tcg_fpstatus);
9578 break;
9579 default:
9580 g_assert_not_reached();
9582 } else {
9583 /* Use helpers for 8 and 16 bit elements */
9584 switch (opcode) {
9585 case 0x5: /* CNT, RBIT */
9586 /* For these two insns size is part of the opcode specifier
9587 * (handled earlier); they always operate on byte elements.
9589 if (u) {
9590 gen_helper_neon_rbit_u8(tcg_res, tcg_op);
9591 } else {
9592 gen_helper_neon_cnt_u8(tcg_res, tcg_op);
9594 break;
9595 case 0x7: /* SQABS, SQNEG */
9597 NeonGenOneOpEnvFn *genfn;
9598 static NeonGenOneOpEnvFn * const fns[2][2] = {
9599 { gen_helper_neon_qabs_s8, gen_helper_neon_qneg_s8 },
9600 { gen_helper_neon_qabs_s16, gen_helper_neon_qneg_s16 },
9602 genfn = fns[size][u];
9603 genfn(tcg_res, cpu_env, tcg_op);
9604 break;
9606 case 0x8: /* CMGT, CMGE */
9607 case 0x9: /* CMEQ, CMLE */
9608 case 0xa: /* CMLT */
9610 static NeonGenTwoOpFn * const fns[3][2] = {
9611 { gen_helper_neon_cgt_s8, gen_helper_neon_cgt_s16 },
9612 { gen_helper_neon_cge_s8, gen_helper_neon_cge_s16 },
9613 { gen_helper_neon_ceq_u8, gen_helper_neon_ceq_u16 },
9615 NeonGenTwoOpFn *genfn;
9616 int comp;
9617 bool reverse;
9618 TCGv_i32 tcg_zero = tcg_const_i32(0);
9620 /* comp = index into [CMGT, CMGE, CMEQ, CMLE, CMLT] */
9621 comp = (opcode - 0x8) * 2 + u;
9622 /* ...but LE, LT are implemented as reverse GE, GT */
9623 reverse = (comp > 2);
9624 if (reverse) {
9625 comp = 4 - comp;
9627 genfn = fns[comp][size];
9628 if (reverse) {
9629 genfn(tcg_res, tcg_zero, tcg_op);
9630 } else {
9631 genfn(tcg_res, tcg_op, tcg_zero);
9633 tcg_temp_free_i32(tcg_zero);
9634 break;
9636 case 0xb: /* ABS, NEG */
9637 if (u) {
9638 TCGv_i32 tcg_zero = tcg_const_i32(0);
9639 if (size) {
9640 gen_helper_neon_sub_u16(tcg_res, tcg_zero, tcg_op);
9641 } else {
9642 gen_helper_neon_sub_u8(tcg_res, tcg_zero, tcg_op);
9644 tcg_temp_free_i32(tcg_zero);
9645 } else {
9646 if (size) {
9647 gen_helper_neon_abs_s16(tcg_res, tcg_op);
9648 } else {
9649 gen_helper_neon_abs_s8(tcg_res, tcg_op);
9652 break;
9653 case 0x4: /* CLS, CLZ */
9654 if (u) {
9655 if (size == 0) {
9656 gen_helper_neon_clz_u8(tcg_res, tcg_op);
9657 } else {
9658 gen_helper_neon_clz_u16(tcg_res, tcg_op);
9660 } else {
9661 if (size == 0) {
9662 gen_helper_neon_cls_s8(tcg_res, tcg_op);
9663 } else {
9664 gen_helper_neon_cls_s16(tcg_res, tcg_op);
9667 break;
9668 default:
9669 g_assert_not_reached();
9673 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
9675 tcg_temp_free_i32(tcg_res);
9676 tcg_temp_free_i32(tcg_op);
9679 if (!is_q) {
9680 clear_vec_high(s, rd);
9683 if (need_rmode) {
9684 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
9685 tcg_temp_free_i32(tcg_rmode);
9687 if (need_fpstatus) {
9688 tcg_temp_free_ptr(tcg_fpstatus);
9692 /* C3.6.13 AdvSIMD scalar x indexed element
9693 * 31 30 29 28 24 23 22 21 20 19 16 15 12 11 10 9 5 4 0
9694 * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
9695 * | 0 1 | U | 1 1 1 1 1 | size | L | M | Rm | opc | H | 0 | Rn | Rd |
9696 * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
9697 * C3.6.18 AdvSIMD vector x indexed element
9698 * 31 30 29 28 24 23 22 21 20 19 16 15 12 11 10 9 5 4 0
9699 * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
9700 * | 0 | Q | U | 0 1 1 1 1 | size | L | M | Rm | opc | H | 0 | Rn | Rd |
9701 * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
9703 static void disas_simd_indexed(DisasContext *s, uint32_t insn)
9705 /* This encoding has two kinds of instruction:
9706 * normal, where we perform elt x idxelt => elt for each
9707 * element in the vector
9708 * long, where we perform elt x idxelt and generate a result of
9709 * double the width of the input element
9710 * The long ops have a 'part' specifier (ie come in INSN, INSN2 pairs).
9712 bool is_scalar = extract32(insn, 28, 1);
9713 bool is_q = extract32(insn, 30, 1);
9714 bool u = extract32(insn, 29, 1);
9715 int size = extract32(insn, 22, 2);
9716 int l = extract32(insn, 21, 1);
9717 int m = extract32(insn, 20, 1);
9718 /* Note that the Rm field here is only 4 bits, not 5 as it usually is */
9719 int rm = extract32(insn, 16, 4);
9720 int opcode = extract32(insn, 12, 4);
9721 int h = extract32(insn, 11, 1);
9722 int rn = extract32(insn, 5, 5);
9723 int rd = extract32(insn, 0, 5);
9724 bool is_long = false;
9725 bool is_fp = false;
9726 int index;
9727 TCGv_ptr fpst;
9729 switch (opcode) {
9730 case 0x0: /* MLA */
9731 case 0x4: /* MLS */
9732 if (!u || is_scalar) {
9733 unallocated_encoding(s);
9734 return;
9736 break;
9737 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
9738 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
9739 case 0xa: /* SMULL, SMULL2, UMULL, UMULL2 */
9740 if (is_scalar) {
9741 unallocated_encoding(s);
9742 return;
9744 is_long = true;
9745 break;
9746 case 0x3: /* SQDMLAL, SQDMLAL2 */
9747 case 0x7: /* SQDMLSL, SQDMLSL2 */
9748 case 0xb: /* SQDMULL, SQDMULL2 */
9749 is_long = true;
9750 /* fall through */
9751 case 0xc: /* SQDMULH */
9752 case 0xd: /* SQRDMULH */
9753 if (u) {
9754 unallocated_encoding(s);
9755 return;
9757 break;
9758 case 0x8: /* MUL */
9759 if (u || is_scalar) {
9760 unallocated_encoding(s);
9761 return;
9763 break;
9764 case 0x1: /* FMLA */
9765 case 0x5: /* FMLS */
9766 if (u) {
9767 unallocated_encoding(s);
9768 return;
9770 /* fall through */
9771 case 0x9: /* FMUL, FMULX */
9772 if (!extract32(size, 1, 1)) {
9773 unallocated_encoding(s);
9774 return;
9776 is_fp = true;
9777 break;
9778 default:
9779 unallocated_encoding(s);
9780 return;
9783 if (is_fp) {
9784 /* low bit of size indicates single/double */
9785 size = extract32(size, 0, 1) ? 3 : 2;
9786 if (size == 2) {
9787 index = h << 1 | l;
9788 } else {
9789 if (l || !is_q) {
9790 unallocated_encoding(s);
9791 return;
9793 index = h;
9795 rm |= (m << 4);
9796 } else {
9797 switch (size) {
9798 case 1:
9799 index = h << 2 | l << 1 | m;
9800 break;
9801 case 2:
9802 index = h << 1 | l;
9803 rm |= (m << 4);
9804 break;
9805 default:
9806 unallocated_encoding(s);
9807 return;
9811 if (is_fp) {
9812 fpst = get_fpstatus_ptr();
9813 } else {
9814 TCGV_UNUSED_PTR(fpst);
9817 if (size == 3) {
9818 TCGv_i64 tcg_idx = tcg_temp_new_i64();
9819 int pass;
9821 assert(is_fp && is_q && !is_long);
9823 read_vec_element(s, tcg_idx, rm, index, MO_64);
9825 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
9826 TCGv_i64 tcg_op = tcg_temp_new_i64();
9827 TCGv_i64 tcg_res = tcg_temp_new_i64();
9829 read_vec_element(s, tcg_op, rn, pass, MO_64);
9831 switch (opcode) {
9832 case 0x5: /* FMLS */
9833 /* As usual for ARM, separate negation for fused multiply-add */
9834 gen_helper_vfp_negd(tcg_op, tcg_op);
9835 /* fall through */
9836 case 0x1: /* FMLA */
9837 read_vec_element(s, tcg_res, rd, pass, MO_64);
9838 gen_helper_vfp_muladdd(tcg_res, tcg_op, tcg_idx, tcg_res, fpst);
9839 break;
9840 case 0x9: /* FMUL, FMULX */
9841 if (u) {
9842 gen_helper_vfp_mulxd(tcg_res, tcg_op, tcg_idx, fpst);
9843 } else {
9844 gen_helper_vfp_muld(tcg_res, tcg_op, tcg_idx, fpst);
9846 break;
9847 default:
9848 g_assert_not_reached();
9851 write_vec_element(s, tcg_res, rd, pass, MO_64);
9852 tcg_temp_free_i64(tcg_op);
9853 tcg_temp_free_i64(tcg_res);
9856 if (is_scalar) {
9857 clear_vec_high(s, rd);
9860 tcg_temp_free_i64(tcg_idx);
9861 } else if (!is_long) {
9862 /* 32 bit floating point, or 16 or 32 bit integer.
9863 * For the 16 bit scalar case we use the usual Neon helpers and
9864 * rely on the fact that 0 op 0 == 0 with no side effects.
9866 TCGv_i32 tcg_idx = tcg_temp_new_i32();
9867 int pass, maxpasses;
9869 if (is_scalar) {
9870 maxpasses = 1;
9871 } else {
9872 maxpasses = is_q ? 4 : 2;
9875 read_vec_element_i32(s, tcg_idx, rm, index, size);
9877 if (size == 1 && !is_scalar) {
9878 /* The simplest way to handle the 16x16 indexed ops is to duplicate
9879 * the index into both halves of the 32 bit tcg_idx and then use
9880 * the usual Neon helpers.
9882 tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
9885 for (pass = 0; pass < maxpasses; pass++) {
9886 TCGv_i32 tcg_op = tcg_temp_new_i32();
9887 TCGv_i32 tcg_res = tcg_temp_new_i32();
9889 read_vec_element_i32(s, tcg_op, rn, pass, is_scalar ? size : MO_32);
9891 switch (opcode) {
9892 case 0x0: /* MLA */
9893 case 0x4: /* MLS */
9894 case 0x8: /* MUL */
9896 static NeonGenTwoOpFn * const fns[2][2] = {
9897 { gen_helper_neon_add_u16, gen_helper_neon_sub_u16 },
9898 { tcg_gen_add_i32, tcg_gen_sub_i32 },
9900 NeonGenTwoOpFn *genfn;
9901 bool is_sub = opcode == 0x4;
9903 if (size == 1) {
9904 gen_helper_neon_mul_u16(tcg_res, tcg_op, tcg_idx);
9905 } else {
9906 tcg_gen_mul_i32(tcg_res, tcg_op, tcg_idx);
9908 if (opcode == 0x8) {
9909 break;
9911 read_vec_element_i32(s, tcg_op, rd, pass, MO_32);
9912 genfn = fns[size - 1][is_sub];
9913 genfn(tcg_res, tcg_op, tcg_res);
9914 break;
9916 case 0x5: /* FMLS */
9917 /* As usual for ARM, separate negation for fused multiply-add */
9918 gen_helper_vfp_negs(tcg_op, tcg_op);
9919 /* fall through */
9920 case 0x1: /* FMLA */
9921 read_vec_element_i32(s, tcg_res, rd, pass, MO_32);
9922 gen_helper_vfp_muladds(tcg_res, tcg_op, tcg_idx, tcg_res, fpst);
9923 break;
9924 case 0x9: /* FMUL, FMULX */
9925 if (u) {
9926 gen_helper_vfp_mulxs(tcg_res, tcg_op, tcg_idx, fpst);
9927 } else {
9928 gen_helper_vfp_muls(tcg_res, tcg_op, tcg_idx, fpst);
9930 break;
9931 case 0xc: /* SQDMULH */
9932 if (size == 1) {
9933 gen_helper_neon_qdmulh_s16(tcg_res, cpu_env,
9934 tcg_op, tcg_idx);
9935 } else {
9936 gen_helper_neon_qdmulh_s32(tcg_res, cpu_env,
9937 tcg_op, tcg_idx);
9939 break;
9940 case 0xd: /* SQRDMULH */
9941 if (size == 1) {
9942 gen_helper_neon_qrdmulh_s16(tcg_res, cpu_env,
9943 tcg_op, tcg_idx);
9944 } else {
9945 gen_helper_neon_qrdmulh_s32(tcg_res, cpu_env,
9946 tcg_op, tcg_idx);
9948 break;
9949 default:
9950 g_assert_not_reached();
9953 if (is_scalar) {
9954 write_fp_sreg(s, rd, tcg_res);
9955 } else {
9956 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
9959 tcg_temp_free_i32(tcg_op);
9960 tcg_temp_free_i32(tcg_res);
9963 tcg_temp_free_i32(tcg_idx);
9965 if (!is_q) {
9966 clear_vec_high(s, rd);
9968 } else {
9969 /* long ops: 16x16->32 or 32x32->64 */
9970 TCGv_i64 tcg_res[2];
9971 int pass;
9972 bool satop = extract32(opcode, 0, 1);
9973 TCGMemOp memop = MO_32;
9975 if (satop || !u) {
9976 memop |= MO_SIGN;
9979 if (size == 2) {
9980 TCGv_i64 tcg_idx = tcg_temp_new_i64();
9982 read_vec_element(s, tcg_idx, rm, index, memop);
9984 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
9985 TCGv_i64 tcg_op = tcg_temp_new_i64();
9986 TCGv_i64 tcg_passres;
9987 int passelt;
9989 if (is_scalar) {
9990 passelt = 0;
9991 } else {
9992 passelt = pass + (is_q * 2);
9995 read_vec_element(s, tcg_op, rn, passelt, memop);
9997 tcg_res[pass] = tcg_temp_new_i64();
9999 if (opcode == 0xa || opcode == 0xb) {
10000 /* Non-accumulating ops */
10001 tcg_passres = tcg_res[pass];
10002 } else {
10003 tcg_passres = tcg_temp_new_i64();
10006 tcg_gen_mul_i64(tcg_passres, tcg_op, tcg_idx);
10007 tcg_temp_free_i64(tcg_op);
10009 if (satop) {
10010 /* saturating, doubling */
10011 gen_helper_neon_addl_saturate_s64(tcg_passres, cpu_env,
10012 tcg_passres, tcg_passres);
10015 if (opcode == 0xa || opcode == 0xb) {
10016 continue;
10019 /* Accumulating op: handle accumulate step */
10020 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
10022 switch (opcode) {
10023 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10024 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
10025 break;
10026 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10027 tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
10028 break;
10029 case 0x7: /* SQDMLSL, SQDMLSL2 */
10030 tcg_gen_neg_i64(tcg_passres, tcg_passres);
10031 /* fall through */
10032 case 0x3: /* SQDMLAL, SQDMLAL2 */
10033 gen_helper_neon_addl_saturate_s64(tcg_res[pass], cpu_env,
10034 tcg_res[pass],
10035 tcg_passres);
10036 break;
10037 default:
10038 g_assert_not_reached();
10040 tcg_temp_free_i64(tcg_passres);
10042 tcg_temp_free_i64(tcg_idx);
10044 if (is_scalar) {
10045 clear_vec_high(s, rd);
10047 } else {
10048 TCGv_i32 tcg_idx = tcg_temp_new_i32();
10050 assert(size == 1);
10051 read_vec_element_i32(s, tcg_idx, rm, index, size);
10053 if (!is_scalar) {
10054 /* The simplest way to handle the 16x16 indexed ops is to
10055 * duplicate the index into both halves of the 32 bit tcg_idx
10056 * and then use the usual Neon helpers.
10058 tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
10061 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
10062 TCGv_i32 tcg_op = tcg_temp_new_i32();
10063 TCGv_i64 tcg_passres;
10065 if (is_scalar) {
10066 read_vec_element_i32(s, tcg_op, rn, pass, size);
10067 } else {
10068 read_vec_element_i32(s, tcg_op, rn,
10069 pass + (is_q * 2), MO_32);
10072 tcg_res[pass] = tcg_temp_new_i64();
10074 if (opcode == 0xa || opcode == 0xb) {
10075 /* Non-accumulating ops */
10076 tcg_passres = tcg_res[pass];
10077 } else {
10078 tcg_passres = tcg_temp_new_i64();
10081 if (memop & MO_SIGN) {
10082 gen_helper_neon_mull_s16(tcg_passres, tcg_op, tcg_idx);
10083 } else {
10084 gen_helper_neon_mull_u16(tcg_passres, tcg_op, tcg_idx);
10086 if (satop) {
10087 gen_helper_neon_addl_saturate_s32(tcg_passres, cpu_env,
10088 tcg_passres, tcg_passres);
10090 tcg_temp_free_i32(tcg_op);
10092 if (opcode == 0xa || opcode == 0xb) {
10093 continue;
10096 /* Accumulating op: handle accumulate step */
10097 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
10099 switch (opcode) {
10100 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
10101 gen_helper_neon_addl_u32(tcg_res[pass], tcg_res[pass],
10102 tcg_passres);
10103 break;
10104 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
10105 gen_helper_neon_subl_u32(tcg_res[pass], tcg_res[pass],
10106 tcg_passres);
10107 break;
10108 case 0x7: /* SQDMLSL, SQDMLSL2 */
10109 gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
10110 /* fall through */
10111 case 0x3: /* SQDMLAL, SQDMLAL2 */
10112 gen_helper_neon_addl_saturate_s32(tcg_res[pass], cpu_env,
10113 tcg_res[pass],
10114 tcg_passres);
10115 break;
10116 default:
10117 g_assert_not_reached();
10119 tcg_temp_free_i64(tcg_passres);
10121 tcg_temp_free_i32(tcg_idx);
10123 if (is_scalar) {
10124 tcg_gen_ext32u_i64(tcg_res[0], tcg_res[0]);
10128 if (is_scalar) {
10129 tcg_res[1] = tcg_const_i64(0);
10132 for (pass = 0; pass < 2; pass++) {
10133 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
10134 tcg_temp_free_i64(tcg_res[pass]);
10138 if (!TCGV_IS_UNUSED_PTR(fpst)) {
10139 tcg_temp_free_ptr(fpst);
10143 /* C3.6.19 Crypto AES
10144 * 31 24 23 22 21 17 16 12 11 10 9 5 4 0
10145 * +-----------------+------+-----------+--------+-----+------+------+
10146 * | 0 1 0 0 1 1 1 0 | size | 1 0 1 0 0 | opcode | 1 0 | Rn | Rd |
10147 * +-----------------+------+-----------+--------+-----+------+------+
10149 static void disas_crypto_aes(DisasContext *s, uint32_t insn)
10151 unsupported_encoding(s, insn);
10154 /* C3.6.20 Crypto three-reg SHA
10155 * 31 24 23 22 21 20 16 15 14 12 11 10 9 5 4 0
10156 * +-----------------+------+---+------+---+--------+-----+------+------+
10157 * | 0 1 0 1 1 1 1 0 | size | 0 | Rm | 0 | opcode | 0 0 | Rn | Rd |
10158 * +-----------------+------+---+------+---+--------+-----+------+------+
10160 static void disas_crypto_three_reg_sha(DisasContext *s, uint32_t insn)
10162 unsupported_encoding(s, insn);
10165 /* C3.6.21 Crypto two-reg SHA
10166 * 31 24 23 22 21 17 16 12 11 10 9 5 4 0
10167 * +-----------------+------+-----------+--------+-----+------+------+
10168 * | 0 1 0 1 1 1 1 0 | size | 1 0 1 0 0 | opcode | 1 0 | Rn | Rd |
10169 * +-----------------+------+-----------+--------+-----+------+------+
10171 static void disas_crypto_two_reg_sha(DisasContext *s, uint32_t insn)
10173 unsupported_encoding(s, insn);
10176 /* C3.6 Data processing - SIMD, inc Crypto
10178 * As the decode gets a little complex we are using a table based
10179 * approach for this part of the decode.
10181 static const AArch64DecodeTable data_proc_simd[] = {
10182 /* pattern , mask , fn */
10183 { 0x0e200400, 0x9f200400, disas_simd_three_reg_same },
10184 { 0x0e200000, 0x9f200c00, disas_simd_three_reg_diff },
10185 { 0x0e200800, 0x9f3e0c00, disas_simd_two_reg_misc },
10186 { 0x0e300800, 0x9f3e0c00, disas_simd_across_lanes },
10187 { 0x0e000400, 0x9fe08400, disas_simd_copy },
10188 { 0x0f000000, 0x9f000400, disas_simd_indexed }, /* vector indexed */
10189 /* simd_mod_imm decode is a subset of simd_shift_imm, so must precede it */
10190 { 0x0f000400, 0x9ff80400, disas_simd_mod_imm },
10191 { 0x0f000400, 0x9f800400, disas_simd_shift_imm },
10192 { 0x0e000000, 0xbf208c00, disas_simd_tb },
10193 { 0x0e000800, 0xbf208c00, disas_simd_zip_trn },
10194 { 0x2e000000, 0xbf208400, disas_simd_ext },
10195 { 0x5e200400, 0xdf200400, disas_simd_scalar_three_reg_same },
10196 { 0x5e200000, 0xdf200c00, disas_simd_scalar_three_reg_diff },
10197 { 0x5e200800, 0xdf3e0c00, disas_simd_scalar_two_reg_misc },
10198 { 0x5e300800, 0xdf3e0c00, disas_simd_scalar_pairwise },
10199 { 0x5e000400, 0xdfe08400, disas_simd_scalar_copy },
10200 { 0x5f000000, 0xdf000400, disas_simd_indexed }, /* scalar indexed */
10201 { 0x5f000400, 0xdf800400, disas_simd_scalar_shift_imm },
10202 { 0x4e280800, 0xff3e0c00, disas_crypto_aes },
10203 { 0x5e000000, 0xff208c00, disas_crypto_three_reg_sha },
10204 { 0x5e280800, 0xff3e0c00, disas_crypto_two_reg_sha },
10205 { 0x00000000, 0x00000000, NULL }
10208 static void disas_data_proc_simd(DisasContext *s, uint32_t insn)
10210 /* Note that this is called with all non-FP cases from
10211 * table C3-6 so it must UNDEF for entries not specifically
10212 * allocated to instructions in that table.
10214 AArch64DecodeFn *fn = lookup_disas_fn(&data_proc_simd[0], insn);
10215 if (fn) {
10216 fn(s, insn);
10217 } else {
10218 unallocated_encoding(s);
10222 /* C3.6 Data processing - SIMD and floating point */
10223 static void disas_data_proc_simd_fp(DisasContext *s, uint32_t insn)
10225 if (extract32(insn, 28, 1) == 1 && extract32(insn, 30, 1) == 0) {
10226 disas_data_proc_fp(s, insn);
10227 } else {
10228 /* SIMD, including crypto */
10229 disas_data_proc_simd(s, insn);
10233 /* C3.1 A64 instruction index by encoding */
10234 static void disas_a64_insn(CPUARMState *env, DisasContext *s)
10236 uint32_t insn;
10238 insn = arm_ldl_code(env, s->pc, s->bswap_code);
10239 s->insn = insn;
10240 s->pc += 4;
10242 switch (extract32(insn, 25, 4)) {
10243 case 0x0: case 0x1: case 0x2: case 0x3: /* UNALLOCATED */
10244 unallocated_encoding(s);
10245 break;
10246 case 0x8: case 0x9: /* Data processing - immediate */
10247 disas_data_proc_imm(s, insn);
10248 break;
10249 case 0xa: case 0xb: /* Branch, exception generation and system insns */
10250 disas_b_exc_sys(s, insn);
10251 break;
10252 case 0x4:
10253 case 0x6:
10254 case 0xc:
10255 case 0xe: /* Loads and stores */
10256 disas_ldst(s, insn);
10257 break;
10258 case 0x5:
10259 case 0xd: /* Data processing - register */
10260 disas_data_proc_reg(s, insn);
10261 break;
10262 case 0x7:
10263 case 0xf: /* Data processing - SIMD and floating point */
10264 disas_data_proc_simd_fp(s, insn);
10265 break;
10266 default:
10267 assert(FALSE); /* all 15 cases should be handled above */
10268 break;
10271 /* if we allocated any temporaries, free them here */
10272 free_tmp_a64(s);
10275 void gen_intermediate_code_internal_a64(ARMCPU *cpu,
10276 TranslationBlock *tb,
10277 bool search_pc)
10279 CPUState *cs = CPU(cpu);
10280 CPUARMState *env = &cpu->env;
10281 DisasContext dc1, *dc = &dc1;
10282 CPUBreakpoint *bp;
10283 uint16_t *gen_opc_end;
10284 int j, lj;
10285 target_ulong pc_start;
10286 target_ulong next_page_start;
10287 int num_insns;
10288 int max_insns;
10290 pc_start = tb->pc;
10292 dc->tb = tb;
10294 gen_opc_end = tcg_ctx.gen_opc_buf + OPC_MAX_SIZE;
10296 dc->is_jmp = DISAS_NEXT;
10297 dc->pc = pc_start;
10298 dc->singlestep_enabled = cs->singlestep_enabled;
10299 dc->condjmp = 0;
10301 dc->aarch64 = 1;
10302 dc->thumb = 0;
10303 dc->bswap_code = 0;
10304 dc->condexec_mask = 0;
10305 dc->condexec_cond = 0;
10306 #if !defined(CONFIG_USER_ONLY)
10307 dc->user = (ARM_TBFLAG_AA64_EL(tb->flags) == 0);
10308 #endif
10309 dc->vfp_enabled = 0;
10310 dc->vec_len = 0;
10311 dc->vec_stride = 0;
10312 dc->cp_regs = cpu->cp_regs;
10313 dc->current_pl = arm_current_pl(env);
10314 dc->features = env->features;
10316 init_tmp_a64_array(dc);
10318 next_page_start = (pc_start & TARGET_PAGE_MASK) + TARGET_PAGE_SIZE;
10319 lj = -1;
10320 num_insns = 0;
10321 max_insns = tb->cflags & CF_COUNT_MASK;
10322 if (max_insns == 0) {
10323 max_insns = CF_COUNT_MASK;
10326 gen_tb_start();
10328 tcg_clear_temp_count();
10330 do {
10331 if (unlikely(!QTAILQ_EMPTY(&cs->breakpoints))) {
10332 QTAILQ_FOREACH(bp, &cs->breakpoints, entry) {
10333 if (bp->pc == dc->pc) {
10334 gen_exception_insn(dc, 0, EXCP_DEBUG);
10335 /* Advance PC so that clearing the breakpoint will
10336 invalidate this TB. */
10337 dc->pc += 2;
10338 goto done_generating;
10343 if (search_pc) {
10344 j = tcg_ctx.gen_opc_ptr - tcg_ctx.gen_opc_buf;
10345 if (lj < j) {
10346 lj++;
10347 while (lj < j) {
10348 tcg_ctx.gen_opc_instr_start[lj++] = 0;
10351 tcg_ctx.gen_opc_pc[lj] = dc->pc;
10352 tcg_ctx.gen_opc_instr_start[lj] = 1;
10353 tcg_ctx.gen_opc_icount[lj] = num_insns;
10356 if (num_insns + 1 == max_insns && (tb->cflags & CF_LAST_IO)) {
10357 gen_io_start();
10360 if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP | CPU_LOG_TB_OP_OPT))) {
10361 tcg_gen_debug_insn_start(dc->pc);
10364 disas_a64_insn(env, dc);
10366 if (tcg_check_temp_count()) {
10367 fprintf(stderr, "TCG temporary leak before "TARGET_FMT_lx"\n",
10368 dc->pc);
10371 /* Translation stops when a conditional branch is encountered.
10372 * Otherwise the subsequent code could get translated several times.
10373 * Also stop translation when a page boundary is reached. This
10374 * ensures prefetch aborts occur at the right place.
10376 num_insns++;
10377 } while (!dc->is_jmp && tcg_ctx.gen_opc_ptr < gen_opc_end &&
10378 !cs->singlestep_enabled &&
10379 !singlestep &&
10380 dc->pc < next_page_start &&
10381 num_insns < max_insns);
10383 if (tb->cflags & CF_LAST_IO) {
10384 gen_io_end();
10387 if (unlikely(cs->singlestep_enabled) && dc->is_jmp != DISAS_EXC) {
10388 /* Note that this means single stepping WFI doesn't halt the CPU.
10389 * For conditional branch insns this is harmless unreachable code as
10390 * gen_goto_tb() has already handled emitting the debug exception
10391 * (and thus a tb-jump is not possible when singlestepping).
10393 assert(dc->is_jmp != DISAS_TB_JUMP);
10394 if (dc->is_jmp != DISAS_JUMP) {
10395 gen_a64_set_pc_im(dc->pc);
10397 gen_exception(EXCP_DEBUG);
10398 } else {
10399 switch (dc->is_jmp) {
10400 case DISAS_NEXT:
10401 gen_goto_tb(dc, 1, dc->pc);
10402 break;
10403 default:
10404 case DISAS_UPDATE:
10405 gen_a64_set_pc_im(dc->pc);
10406 /* fall through */
10407 case DISAS_JUMP:
10408 /* indicate that the hash table must be used to find the next TB */
10409 tcg_gen_exit_tb(0);
10410 break;
10411 case DISAS_TB_JUMP:
10412 case DISAS_EXC:
10413 case DISAS_SWI:
10414 break;
10415 case DISAS_WFI:
10416 /* This is a special case because we don't want to just halt the CPU
10417 * if trying to debug across a WFI.
10419 gen_a64_set_pc_im(dc->pc);
10420 gen_helper_wfi(cpu_env);
10421 break;
10425 done_generating:
10426 gen_tb_end(tb, num_insns);
10427 *tcg_ctx.gen_opc_ptr = INDEX_op_end;
10429 #ifdef DEBUG_DISAS
10430 if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)) {
10431 qemu_log("----------------\n");
10432 qemu_log("IN: %s\n", lookup_symbol(pc_start));
10433 log_target_disas(env, pc_start, dc->pc - pc_start,
10434 4 | (dc->bswap_code << 1));
10435 qemu_log("\n");
10437 #endif
10438 if (search_pc) {
10439 j = tcg_ctx.gen_opc_ptr - tcg_ctx.gen_opc_buf;
10440 lj++;
10441 while (lj <= j) {
10442 tcg_ctx.gen_opc_instr_start[lj++] = 0;
10444 } else {
10445 tb->size = dc->pc - pc_start;
10446 tb->icount = num_insns;