blob: 45f89961f820c4b197b694116081918fe9913121 [file] [log] [blame]
Alexander Graf14ade102013-09-03 20:12:10 +01001/*
2 * AArch64 translation
3 *
4 * Copyright (c) 2013 Alexander Graf <agraf@suse.de>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 */
19#include <stdarg.h>
20#include <stdlib.h>
21#include <stdio.h>
22#include <string.h>
23#include <inttypes.h>
24
25#include "cpu.h"
26#include "tcg-op.h"
27#include "qemu/log.h"
28#include "translate.h"
29#include "qemu/host-utils.h"
30
Peter Maydell40f860c2013-12-17 19:42:31 +000031#include "exec/gen-icount.h"
32
Alexander Graf14ade102013-09-03 20:12:10 +010033#include "helper.h"
34#define GEN_HELPER 1
35#include "helper.h"
36
37static TCGv_i64 cpu_X[32];
38static TCGv_i64 cpu_pc;
Alexander Graf832ffa12013-12-17 19:42:34 +000039static TCGv_i32 cpu_NF, cpu_ZF, cpu_CF, cpu_VF;
Alexander Graf14ade102013-09-03 20:12:10 +010040
Michael Matzfa2ef212014-01-04 22:15:47 +000041/* Load/store exclusive handling */
42static TCGv_i64 cpu_exclusive_addr;
43static TCGv_i64 cpu_exclusive_val;
44static TCGv_i64 cpu_exclusive_high;
45#ifdef CONFIG_USER_ONLY
46static TCGv_i64 cpu_exclusive_test;
47static TCGv_i32 cpu_exclusive_info;
48#endif
49
Alexander Graf14ade102013-09-03 20:12:10 +010050static 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"
55};
56
Alexander Graf832ffa12013-12-17 19:42:34 +000057enum 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
62};
63
Alex Bennée384b26f2014-01-31 14:47:30 +000064/* Table based decoder typedefs - used when the relevant bits for decode
65 * are too awkwardly scattered across the instruction (eg SIMD).
66 */
67typedef void AArch64DecodeFn(DisasContext *s, uint32_t insn);
68
69typedef struct AArch64DecodeTable {
70 uint32_t pattern;
71 uint32_t mask;
72 AArch64DecodeFn *disas_fn;
73} AArch64DecodeTable;
74
Peter Maydell1f8a73a2014-01-31 14:47:37 +000075/* Function prototype for gen_ functions for calling Neon helpers */
76typedef void NeonGenTwoOpFn(TCGv_i32, TCGv_i32, TCGv_i32);
Peter Maydell6d9571f2014-02-08 14:46:55 +000077typedef void NeonGenTwoOpEnvFn(TCGv_i32, TCGv_ptr, TCGv_i32, TCGv_i32);
Peter Maydell70d7f982014-02-20 10:35:55 +000078typedef void NeonGenTwo64OpFn(TCGv_i64, TCGv_i64, TCGv_i64);
Peter Maydelld980fd52014-02-03 23:31:52 +000079typedef void NeonGenNarrowFn(TCGv_i32, TCGv_i64);
80typedef void NeonGenNarrowEnvFn(TCGv_i32, TCGv_ptr, TCGv_i64);
Peter Maydell70d7f982014-02-20 10:35:55 +000081typedef void NeonGenWidenFn(TCGv_i64, TCGv_i32);
Alex Bennée8908f4d2014-02-20 10:35:49 +000082typedef void NeonGenTwoSingleOPFn(TCGv_i32, TCGv_i32, TCGv_i32, TCGv_ptr);
83typedef void NeonGenTwoDoubleOPFn(TCGv_i64, TCGv_i64, TCGv_i64, TCGv_ptr);
Peter Maydell74ef1972014-03-07 12:06:13 +000084typedef void NeonGenOneOpFn(TCGv_i64, TCGv_i64);
Peter Maydell1f8a73a2014-01-31 14:47:37 +000085
Alexander Graf14ade102013-09-03 20:12:10 +010086/* initialize TCG globals. */
87void a64_translate_init(void)
88{
89 int i;
90
91 cpu_pc = tcg_global_mem_new_i64(TCG_AREG0,
92 offsetof(CPUARMState, pc),
93 "pc");
94 for (i = 0; i < 32; i++) {
95 cpu_X[i] = tcg_global_mem_new_i64(TCG_AREG0,
96 offsetof(CPUARMState, xregs[i]),
97 regnames[i]);
98 }
99
Alexander Graf832ffa12013-12-17 19:42:34 +0000100 cpu_NF = tcg_global_mem_new_i32(TCG_AREG0, offsetof(CPUARMState, NF), "NF");
101 cpu_ZF = tcg_global_mem_new_i32(TCG_AREG0, offsetof(CPUARMState, ZF), "ZF");
102 cpu_CF = tcg_global_mem_new_i32(TCG_AREG0, offsetof(CPUARMState, CF), "CF");
103 cpu_VF = tcg_global_mem_new_i32(TCG_AREG0, offsetof(CPUARMState, VF), "VF");
Michael Matzfa2ef212014-01-04 22:15:47 +0000104
105 cpu_exclusive_addr = tcg_global_mem_new_i64(TCG_AREG0,
106 offsetof(CPUARMState, exclusive_addr), "exclusive_addr");
107 cpu_exclusive_val = tcg_global_mem_new_i64(TCG_AREG0,
108 offsetof(CPUARMState, exclusive_val), "exclusive_val");
109 cpu_exclusive_high = tcg_global_mem_new_i64(TCG_AREG0,
110 offsetof(CPUARMState, exclusive_high), "exclusive_high");
111#ifdef CONFIG_USER_ONLY
112 cpu_exclusive_test = tcg_global_mem_new_i64(TCG_AREG0,
113 offsetof(CPUARMState, exclusive_test), "exclusive_test");
114 cpu_exclusive_info = tcg_global_mem_new_i32(TCG_AREG0,
115 offsetof(CPUARMState, exclusive_info), "exclusive_info");
116#endif
Alexander Graf14ade102013-09-03 20:12:10 +0100117}
118
119void aarch64_cpu_dump_state(CPUState *cs, FILE *f,
120 fprintf_function cpu_fprintf, int flags)
121{
122 ARMCPU *cpu = ARM_CPU(cs);
123 CPUARMState *env = &cpu->env;
Peter Maydelld3563122013-12-17 19:42:30 +0000124 uint32_t psr = pstate_read(env);
Alexander Graf14ade102013-09-03 20:12:10 +0100125 int i;
126
127 cpu_fprintf(f, "PC=%016"PRIx64" SP=%016"PRIx64"\n",
128 env->pc, env->xregs[31]);
129 for (i = 0; i < 31; i++) {
130 cpu_fprintf(f, "X%02d=%016"PRIx64, i, env->xregs[i]);
131 if ((i % 4) == 3) {
132 cpu_fprintf(f, "\n");
133 } else {
134 cpu_fprintf(f, " ");
135 }
136 }
Peter Maydelld3563122013-12-17 19:42:30 +0000137 cpu_fprintf(f, "PSTATE=%08x (flags %c%c%c%c)\n",
138 psr,
139 psr & PSTATE_N ? 'N' : '-',
140 psr & PSTATE_Z ? 'Z' : '-',
141 psr & PSTATE_C ? 'C' : '-',
142 psr & PSTATE_V ? 'V' : '-');
Alexander Graf14ade102013-09-03 20:12:10 +0100143 cpu_fprintf(f, "\n");
Alexander Graff6d8a312014-01-04 22:15:49 +0000144
145 if (flags & CPU_DUMP_FPU) {
146 int numvfpregs = 32;
147 for (i = 0; i < numvfpregs; i += 2) {
148 uint64_t vlo = float64_val(env->vfp.regs[i * 2]);
149 uint64_t vhi = float64_val(env->vfp.regs[(i * 2) + 1]);
150 cpu_fprintf(f, "q%02d=%016" PRIx64 ":%016" PRIx64 " ",
151 i, vhi, vlo);
152 vlo = float64_val(env->vfp.regs[(i + 1) * 2]);
153 vhi = float64_val(env->vfp.regs[((i + 1) * 2) + 1]);
154 cpu_fprintf(f, "q%02d=%016" PRIx64 ":%016" PRIx64 "\n",
155 i + 1, vhi, vlo);
156 }
157 cpu_fprintf(f, "FPCR: %08x FPSR: %08x\n",
158 vfp_get_fpcr(env), vfp_get_fpsr(env));
159 }
Alexander Graf14ade102013-09-03 20:12:10 +0100160}
161
Peter Maydell4a08d472013-12-22 22:32:27 +0000162static int get_mem_index(DisasContext *s)
163{
164#ifdef CONFIG_USER_ONLY
165 return 1;
166#else
167 return s->user;
168#endif
169}
170
Alexander Graf14ade102013-09-03 20:12:10 +0100171void gen_a64_set_pc_im(uint64_t val)
172{
173 tcg_gen_movi_i64(cpu_pc, val);
174}
175
176static void gen_exception(int excp)
177{
178 TCGv_i32 tmp = tcg_temp_new_i32();
179 tcg_gen_movi_i32(tmp, excp);
180 gen_helper_exception(cpu_env, tmp);
181 tcg_temp_free_i32(tmp);
182}
183
184static void gen_exception_insn(DisasContext *s, int offset, int excp)
185{
186 gen_a64_set_pc_im(s->pc - offset);
187 gen_exception(excp);
Peter Maydell40f860c2013-12-17 19:42:31 +0000188 s->is_jmp = DISAS_EXC;
189}
190
191static inline bool use_goto_tb(DisasContext *s, int n, uint64_t dest)
192{
193 /* No direct tb linking with singlestep or deterministic io */
194 if (s->singlestep_enabled || (s->tb->cflags & CF_LAST_IO)) {
195 return false;
196 }
197
198 /* Only link tbs from inside the same guest page */
199 if ((s->tb->pc & TARGET_PAGE_MASK) != (dest & TARGET_PAGE_MASK)) {
200 return false;
201 }
202
203 return true;
204}
205
206static inline void gen_goto_tb(DisasContext *s, int n, uint64_t dest)
207{
208 TranslationBlock *tb;
209
210 tb = s->tb;
211 if (use_goto_tb(s, n, dest)) {
212 tcg_gen_goto_tb(n);
213 gen_a64_set_pc_im(dest);
214 tcg_gen_exit_tb((tcg_target_long)tb + n);
215 s->is_jmp = DISAS_TB_JUMP;
216 } else {
217 gen_a64_set_pc_im(dest);
218 if (s->singlestep_enabled) {
219 gen_exception(EXCP_DEBUG);
220 }
221 tcg_gen_exit_tb(0);
222 s->is_jmp = DISAS_JUMP;
223 }
Alexander Graf14ade102013-09-03 20:12:10 +0100224}
225
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +0000226static void unallocated_encoding(DisasContext *s)
Alexander Graf14ade102013-09-03 20:12:10 +0100227{
Alexander Graf14ade102013-09-03 20:12:10 +0100228 gen_exception_insn(s, 4, EXCP_UDEF);
229}
230
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +0000231#define unsupported_encoding(s, insn) \
232 do { \
233 qemu_log_mask(LOG_UNIMP, \
234 "%s:%d: unsupported instruction encoding 0x%08x " \
235 "at pc=%016" PRIx64 "\n", \
236 __FILE__, __LINE__, insn, s->pc - 4); \
237 unallocated_encoding(s); \
238 } while (0);
Alexander Graf14ade102013-09-03 20:12:10 +0100239
Alexander Graf11e169d2013-12-17 19:42:32 +0000240static void init_tmp_a64_array(DisasContext *s)
241{
242#ifdef CONFIG_DEBUG_TCG
243 int i;
244 for (i = 0; i < ARRAY_SIZE(s->tmp_a64); i++) {
245 TCGV_UNUSED_I64(s->tmp_a64[i]);
246 }
247#endif
248 s->tmp_a64_count = 0;
249}
250
251static void free_tmp_a64(DisasContext *s)
252{
253 int i;
254 for (i = 0; i < s->tmp_a64_count; i++) {
255 tcg_temp_free_i64(s->tmp_a64[i]);
256 }
257 init_tmp_a64_array(s);
258}
259
260static TCGv_i64 new_tmp_a64(DisasContext *s)
261{
262 assert(s->tmp_a64_count < TMP_A64_MAX);
263 return s->tmp_a64[s->tmp_a64_count++] = tcg_temp_new_i64();
264}
265
266static TCGv_i64 new_tmp_a64_zero(DisasContext *s)
267{
268 TCGv_i64 t = new_tmp_a64(s);
269 tcg_gen_movi_i64(t, 0);
270 return t;
271}
272
Alexander Graf71b46082013-12-17 19:42:36 +0000273/*
274 * Register access functions
275 *
276 * These functions are used for directly accessing a register in where
277 * changes to the final register value are likely to be made. If you
278 * need to use a register for temporary calculation (e.g. index type
279 * operations) use the read_* form.
280 *
281 * B1.2.1 Register mappings
282 *
283 * In instruction register encoding 31 can refer to ZR (zero register) or
284 * the SP (stack pointer) depending on context. In QEMU's case we map SP
285 * to cpu_X[31] and ZR accesses to a temporary which can be discarded.
286 * This is the point of the _sp forms.
287 */
Alexander Graf11e169d2013-12-17 19:42:32 +0000288static TCGv_i64 cpu_reg(DisasContext *s, int reg)
289{
290 if (reg == 31) {
291 return new_tmp_a64_zero(s);
292 } else {
293 return cpu_X[reg];
294 }
295}
296
Alexander Graf71b46082013-12-17 19:42:36 +0000297/* register access for when 31 == SP */
298static TCGv_i64 cpu_reg_sp(DisasContext *s, int reg)
299{
300 return cpu_X[reg];
301}
302
Alexander Graf60e53382013-12-17 19:42:33 +0000303/* read a cpu register in 32bit/64bit mode. Returns a TCGv_i64
304 * representing the register contents. This TCGv is an auto-freed
305 * temporary so it need not be explicitly freed, and may be modified.
306 */
307static TCGv_i64 read_cpu_reg(DisasContext *s, int reg, int sf)
308{
309 TCGv_i64 v = new_tmp_a64(s);
310 if (reg != 31) {
311 if (sf) {
312 tcg_gen_mov_i64(v, cpu_X[reg]);
313 } else {
314 tcg_gen_ext32u_i64(v, cpu_X[reg]);
315 }
316 } else {
317 tcg_gen_movi_i64(v, 0);
318 }
319 return v;
320}
321
Peter Maydell4a08d472013-12-22 22:32:27 +0000322static TCGv_i64 read_cpu_reg_sp(DisasContext *s, int reg, int sf)
323{
324 TCGv_i64 v = new_tmp_a64(s);
325 if (sf) {
326 tcg_gen_mov_i64(v, cpu_X[reg]);
327 } else {
328 tcg_gen_ext32u_i64(v, cpu_X[reg]);
329 }
330 return v;
331}
332
Alex Bennée72430bf2014-01-31 14:47:30 +0000333/* Return the offset into CPUARMState of an element of specified
334 * size, 'element' places in from the least significant end of
335 * the FP/vector register Qn.
336 */
337static inline int vec_reg_offset(int regno, int element, TCGMemOp size)
338{
339 int offs = offsetof(CPUARMState, vfp.regs[regno * 2]);
340#ifdef HOST_WORDS_BIGENDIAN
341 /* This is complicated slightly because vfp.regs[2n] is
342 * still the low half and vfp.regs[2n+1] the high half
343 * of the 128 bit vector, even on big endian systems.
344 * Calculate the offset assuming a fully bigendian 128 bits,
345 * then XOR to account for the order of the two 64 bit halves.
346 */
347 offs += (16 - ((element + 1) * (1 << size)));
348 offs ^= 8;
349#else
350 offs += element * (1 << size);
351#endif
352 return offs;
353}
354
Peter Maydelle2f90562014-01-04 22:15:49 +0000355/* Return the offset into CPUARMState of a slice (from
356 * the least significant end) of FP register Qn (ie
357 * Dn, Sn, Hn or Bn).
358 * (Note that this is not the same mapping as for A32; see cpu.h)
359 */
360static inline int fp_reg_offset(int regno, TCGMemOp size)
361{
362 int offs = offsetof(CPUARMState, vfp.regs[regno * 2]);
363#ifdef HOST_WORDS_BIGENDIAN
364 offs += (8 - (1 << size));
365#endif
366 return offs;
367}
368
369/* Offset of the high half of the 128 bit vector Qn */
370static inline int fp_reg_hi_offset(int regno)
371{
372 return offsetof(CPUARMState, vfp.regs[regno * 2 + 1]);
373}
374
Alexander Grafec73d2e2014-01-04 22:15:50 +0000375/* Convenience accessors for reading and writing single and double
376 * FP registers. Writing clears the upper parts of the associated
377 * 128 bit vector register, as required by the architecture.
378 * Note that unlike the GP register accessors, the values returned
379 * by the read functions must be manually freed.
380 */
381static TCGv_i64 read_fp_dreg(DisasContext *s, int reg)
382{
383 TCGv_i64 v = tcg_temp_new_i64();
384
385 tcg_gen_ld_i64(v, cpu_env, fp_reg_offset(reg, MO_64));
386 return v;
387}
388
389static TCGv_i32 read_fp_sreg(DisasContext *s, int reg)
390{
391 TCGv_i32 v = tcg_temp_new_i32();
392
393 tcg_gen_ld_i32(v, cpu_env, fp_reg_offset(reg, MO_32));
394 return v;
395}
396
397static void write_fp_dreg(DisasContext *s, int reg, TCGv_i64 v)
398{
399 TCGv_i64 tcg_zero = tcg_const_i64(0);
400
401 tcg_gen_st_i64(v, cpu_env, fp_reg_offset(reg, MO_64));
402 tcg_gen_st_i64(tcg_zero, cpu_env, fp_reg_hi_offset(reg));
403 tcg_temp_free_i64(tcg_zero);
404}
405
406static void write_fp_sreg(DisasContext *s, int reg, TCGv_i32 v)
407{
408 TCGv_i64 tmp = tcg_temp_new_i64();
409
410 tcg_gen_extu_i32_i64(tmp, v);
411 write_fp_dreg(s, reg, tmp);
412 tcg_temp_free_i64(tmp);
413}
414
415static TCGv_ptr get_fpstatus_ptr(void)
416{
417 TCGv_ptr statusptr = tcg_temp_new_ptr();
418 int offset;
419
420 /* In A64 all instructions (both FP and Neon) use the FPCR;
421 * there is no equivalent of the A32 Neon "standard FPSCR value"
422 * and all operations use vfp.fp_status.
423 */
424 offset = offsetof(CPUARMState, vfp.fp_status);
425 tcg_gen_addi_ptr(statusptr, cpu_env, offset);
426 return statusptr;
427}
428
Alexander Graf832ffa12013-12-17 19:42:34 +0000429/* Set ZF and NF based on a 64 bit result. This is alas fiddlier
430 * than the 32 bit equivalent.
431 */
432static inline void gen_set_NZ64(TCGv_i64 result)
433{
434 TCGv_i64 flag = tcg_temp_new_i64();
435
436 tcg_gen_setcondi_i64(TCG_COND_NE, flag, result, 0);
437 tcg_gen_trunc_i64_i32(cpu_ZF, flag);
438 tcg_gen_shri_i64(flag, result, 32);
439 tcg_gen_trunc_i64_i32(cpu_NF, flag);
440 tcg_temp_free_i64(flag);
441}
442
443/* Set NZCV as for a logical operation: NZ as per result, CV cleared. */
444static inline void gen_logic_CC(int sf, TCGv_i64 result)
445{
446 if (sf) {
447 gen_set_NZ64(result);
448 } else {
449 tcg_gen_trunc_i64_i32(cpu_ZF, result);
450 tcg_gen_trunc_i64_i32(cpu_NF, result);
451 }
452 tcg_gen_movi_i32(cpu_CF, 0);
453 tcg_gen_movi_i32(cpu_VF, 0);
454}
455
Alex Bennéeb0ff21b2013-12-23 23:27:29 +0000456/* dest = T0 + T1; compute C, N, V and Z flags */
457static void gen_add_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
458{
459 if (sf) {
460 TCGv_i64 result, flag, tmp;
461 result = tcg_temp_new_i64();
462 flag = tcg_temp_new_i64();
463 tmp = tcg_temp_new_i64();
464
465 tcg_gen_movi_i64(tmp, 0);
466 tcg_gen_add2_i64(result, flag, t0, tmp, t1, tmp);
467
468 tcg_gen_trunc_i64_i32(cpu_CF, flag);
469
470 gen_set_NZ64(result);
471
472 tcg_gen_xor_i64(flag, result, t0);
473 tcg_gen_xor_i64(tmp, t0, t1);
474 tcg_gen_andc_i64(flag, flag, tmp);
475 tcg_temp_free_i64(tmp);
476 tcg_gen_shri_i64(flag, flag, 32);
477 tcg_gen_trunc_i64_i32(cpu_VF, flag);
478
479 tcg_gen_mov_i64(dest, result);
480 tcg_temp_free_i64(result);
481 tcg_temp_free_i64(flag);
482 } else {
483 /* 32 bit arithmetic */
484 TCGv_i32 t0_32 = tcg_temp_new_i32();
485 TCGv_i32 t1_32 = tcg_temp_new_i32();
486 TCGv_i32 tmp = tcg_temp_new_i32();
487
488 tcg_gen_movi_i32(tmp, 0);
489 tcg_gen_trunc_i64_i32(t0_32, t0);
490 tcg_gen_trunc_i64_i32(t1_32, t1);
491 tcg_gen_add2_i32(cpu_NF, cpu_CF, t0_32, tmp, t1_32, tmp);
492 tcg_gen_mov_i32(cpu_ZF, cpu_NF);
493 tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
494 tcg_gen_xor_i32(tmp, t0_32, t1_32);
495 tcg_gen_andc_i32(cpu_VF, cpu_VF, tmp);
496 tcg_gen_extu_i32_i64(dest, cpu_NF);
497
498 tcg_temp_free_i32(tmp);
499 tcg_temp_free_i32(t0_32);
500 tcg_temp_free_i32(t1_32);
501 }
502}
503
504/* dest = T0 - T1; compute C, N, V and Z flags */
505static void gen_sub_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
506{
507 if (sf) {
508 /* 64 bit arithmetic */
509 TCGv_i64 result, flag, tmp;
510
511 result = tcg_temp_new_i64();
512 flag = tcg_temp_new_i64();
513 tcg_gen_sub_i64(result, t0, t1);
514
515 gen_set_NZ64(result);
516
517 tcg_gen_setcond_i64(TCG_COND_GEU, flag, t0, t1);
518 tcg_gen_trunc_i64_i32(cpu_CF, flag);
519
520 tcg_gen_xor_i64(flag, result, t0);
521 tmp = tcg_temp_new_i64();
522 tcg_gen_xor_i64(tmp, t0, t1);
523 tcg_gen_and_i64(flag, flag, tmp);
524 tcg_temp_free_i64(tmp);
525 tcg_gen_shri_i64(flag, flag, 32);
526 tcg_gen_trunc_i64_i32(cpu_VF, flag);
527 tcg_gen_mov_i64(dest, result);
528 tcg_temp_free_i64(flag);
529 tcg_temp_free_i64(result);
530 } else {
531 /* 32 bit arithmetic */
532 TCGv_i32 t0_32 = tcg_temp_new_i32();
533 TCGv_i32 t1_32 = tcg_temp_new_i32();
534 TCGv_i32 tmp;
535
536 tcg_gen_trunc_i64_i32(t0_32, t0);
537 tcg_gen_trunc_i64_i32(t1_32, t1);
538 tcg_gen_sub_i32(cpu_NF, t0_32, t1_32);
539 tcg_gen_mov_i32(cpu_ZF, cpu_NF);
540 tcg_gen_setcond_i32(TCG_COND_GEU, cpu_CF, t0_32, t1_32);
541 tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
542 tmp = tcg_temp_new_i32();
543 tcg_gen_xor_i32(tmp, t0_32, t1_32);
544 tcg_temp_free_i32(t0_32);
545 tcg_temp_free_i32(t1_32);
546 tcg_gen_and_i32(cpu_VF, cpu_VF, tmp);
547 tcg_temp_free_i32(tmp);
548 tcg_gen_extu_i32_i64(dest, cpu_NF);
549 }
550}
551
Claudio Fontana643dbb02014-01-04 22:15:46 +0000552/* dest = T0 + T1 + CF; do not compute flags. */
553static void gen_adc(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
554{
555 TCGv_i64 flag = tcg_temp_new_i64();
556 tcg_gen_extu_i32_i64(flag, cpu_CF);
557 tcg_gen_add_i64(dest, t0, t1);
558 tcg_gen_add_i64(dest, dest, flag);
559 tcg_temp_free_i64(flag);
560
561 if (!sf) {
562 tcg_gen_ext32u_i64(dest, dest);
563 }
564}
565
566/* dest = T0 + T1 + CF; compute C, N, V and Z flags. */
567static void gen_adc_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
568{
569 if (sf) {
570 TCGv_i64 result, cf_64, vf_64, tmp;
571 result = tcg_temp_new_i64();
572 cf_64 = tcg_temp_new_i64();
573 vf_64 = tcg_temp_new_i64();
574 tmp = tcg_const_i64(0);
575
576 tcg_gen_extu_i32_i64(cf_64, cpu_CF);
577 tcg_gen_add2_i64(result, cf_64, t0, tmp, cf_64, tmp);
578 tcg_gen_add2_i64(result, cf_64, result, cf_64, t1, tmp);
579 tcg_gen_trunc_i64_i32(cpu_CF, cf_64);
580 gen_set_NZ64(result);
581
582 tcg_gen_xor_i64(vf_64, result, t0);
583 tcg_gen_xor_i64(tmp, t0, t1);
584 tcg_gen_andc_i64(vf_64, vf_64, tmp);
585 tcg_gen_shri_i64(vf_64, vf_64, 32);
586 tcg_gen_trunc_i64_i32(cpu_VF, vf_64);
587
588 tcg_gen_mov_i64(dest, result);
589
590 tcg_temp_free_i64(tmp);
591 tcg_temp_free_i64(vf_64);
592 tcg_temp_free_i64(cf_64);
593 tcg_temp_free_i64(result);
594 } else {
595 TCGv_i32 t0_32, t1_32, tmp;
596 t0_32 = tcg_temp_new_i32();
597 t1_32 = tcg_temp_new_i32();
598 tmp = tcg_const_i32(0);
599
600 tcg_gen_trunc_i64_i32(t0_32, t0);
601 tcg_gen_trunc_i64_i32(t1_32, t1);
602 tcg_gen_add2_i32(cpu_NF, cpu_CF, t0_32, tmp, cpu_CF, tmp);
603 tcg_gen_add2_i32(cpu_NF, cpu_CF, cpu_NF, cpu_CF, t1_32, tmp);
604
605 tcg_gen_mov_i32(cpu_ZF, cpu_NF);
606 tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
607 tcg_gen_xor_i32(tmp, t0_32, t1_32);
608 tcg_gen_andc_i32(cpu_VF, cpu_VF, tmp);
609 tcg_gen_extu_i32_i64(dest, cpu_NF);
610
611 tcg_temp_free_i32(tmp);
612 tcg_temp_free_i32(t1_32);
613 tcg_temp_free_i32(t0_32);
614 }
615}
616
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +0000617/*
Peter Maydell4a08d472013-12-22 22:32:27 +0000618 * Load/Store generators
619 */
620
621/*
Peter Maydell60510ae2014-02-20 10:35:56 +0000622 * Store from GPR register to memory.
Peter Maydell4a08d472013-12-22 22:32:27 +0000623 */
Peter Maydell60510ae2014-02-20 10:35:56 +0000624static void do_gpr_st_memidx(DisasContext *s, TCGv_i64 source,
625 TCGv_i64 tcg_addr, int size, int memidx)
626{
627 g_assert(size <= 3);
628 tcg_gen_qemu_st_i64(source, tcg_addr, memidx, MO_TE + size);
629}
630
Peter Maydell4a08d472013-12-22 22:32:27 +0000631static void do_gpr_st(DisasContext *s, TCGv_i64 source,
632 TCGv_i64 tcg_addr, int size)
633{
Peter Maydell60510ae2014-02-20 10:35:56 +0000634 do_gpr_st_memidx(s, source, tcg_addr, size, get_mem_index(s));
Peter Maydell4a08d472013-12-22 22:32:27 +0000635}
636
637/*
638 * Load from memory to GPR register
639 */
Peter Maydell60510ae2014-02-20 10:35:56 +0000640static void do_gpr_ld_memidx(DisasContext *s, TCGv_i64 dest, TCGv_i64 tcg_addr,
641 int size, bool is_signed, bool extend, int memidx)
Peter Maydell4a08d472013-12-22 22:32:27 +0000642{
643 TCGMemOp memop = MO_TE + size;
644
645 g_assert(size <= 3);
646
647 if (is_signed) {
648 memop += MO_SIGN;
649 }
650
Peter Maydell60510ae2014-02-20 10:35:56 +0000651 tcg_gen_qemu_ld_i64(dest, tcg_addr, memidx, memop);
Peter Maydell4a08d472013-12-22 22:32:27 +0000652
653 if (extend && is_signed) {
654 g_assert(size < 3);
655 tcg_gen_ext32u_i64(dest, dest);
656 }
657}
658
Peter Maydell60510ae2014-02-20 10:35:56 +0000659static void do_gpr_ld(DisasContext *s, TCGv_i64 dest, TCGv_i64 tcg_addr,
660 int size, bool is_signed, bool extend)
661{
662 do_gpr_ld_memidx(s, dest, tcg_addr, size, is_signed, extend,
663 get_mem_index(s));
664}
665
Peter Maydell4a08d472013-12-22 22:32:27 +0000666/*
667 * Store from FP register to memory
668 */
669static void do_fp_st(DisasContext *s, int srcidx, TCGv_i64 tcg_addr, int size)
670{
671 /* This writes the bottom N bits of a 128 bit wide vector to memory */
Peter Maydell4a08d472013-12-22 22:32:27 +0000672 TCGv_i64 tmp = tcg_temp_new_i64();
Peter Maydelle2f90562014-01-04 22:15:49 +0000673 tcg_gen_ld_i64(tmp, cpu_env, fp_reg_offset(srcidx, MO_64));
Peter Maydell4a08d472013-12-22 22:32:27 +0000674 if (size < 4) {
Peter Maydell4a08d472013-12-22 22:32:27 +0000675 tcg_gen_qemu_st_i64(tmp, tcg_addr, get_mem_index(s), MO_TE + size);
676 } else {
677 TCGv_i64 tcg_hiaddr = tcg_temp_new_i64();
Peter Maydell4a08d472013-12-22 22:32:27 +0000678 tcg_gen_qemu_st_i64(tmp, tcg_addr, get_mem_index(s), MO_TEQ);
679 tcg_gen_qemu_st64(tmp, tcg_addr, get_mem_index(s));
Peter Maydelle2f90562014-01-04 22:15:49 +0000680 tcg_gen_ld_i64(tmp, cpu_env, fp_reg_hi_offset(srcidx));
Peter Maydell4a08d472013-12-22 22:32:27 +0000681 tcg_gen_addi_i64(tcg_hiaddr, tcg_addr, 8);
682 tcg_gen_qemu_st_i64(tmp, tcg_hiaddr, get_mem_index(s), MO_TEQ);
683 tcg_temp_free_i64(tcg_hiaddr);
684 }
685
686 tcg_temp_free_i64(tmp);
687}
688
689/*
690 * Load from memory to FP register
691 */
692static void do_fp_ld(DisasContext *s, int destidx, TCGv_i64 tcg_addr, int size)
693{
694 /* This always zero-extends and writes to a full 128 bit wide vector */
Peter Maydell4a08d472013-12-22 22:32:27 +0000695 TCGv_i64 tmplo = tcg_temp_new_i64();
696 TCGv_i64 tmphi;
697
698 if (size < 4) {
699 TCGMemOp memop = MO_TE + size;
700 tmphi = tcg_const_i64(0);
701 tcg_gen_qemu_ld_i64(tmplo, tcg_addr, get_mem_index(s), memop);
702 } else {
703 TCGv_i64 tcg_hiaddr;
704 tmphi = tcg_temp_new_i64();
705 tcg_hiaddr = tcg_temp_new_i64();
706
707 tcg_gen_qemu_ld_i64(tmplo, tcg_addr, get_mem_index(s), MO_TEQ);
708 tcg_gen_addi_i64(tcg_hiaddr, tcg_addr, 8);
709 tcg_gen_qemu_ld_i64(tmphi, tcg_hiaddr, get_mem_index(s), MO_TEQ);
710 tcg_temp_free_i64(tcg_hiaddr);
711 }
712
Peter Maydelle2f90562014-01-04 22:15:49 +0000713 tcg_gen_st_i64(tmplo, cpu_env, fp_reg_offset(destidx, MO_64));
714 tcg_gen_st_i64(tmphi, cpu_env, fp_reg_hi_offset(destidx));
Peter Maydell4a08d472013-12-22 22:32:27 +0000715
716 tcg_temp_free_i64(tmplo);
717 tcg_temp_free_i64(tmphi);
718}
719
Alex Bennée229b7a02013-12-23 23:27:29 +0000720/*
Alex Bennée72430bf2014-01-31 14:47:30 +0000721 * Vector load/store helpers.
722 *
723 * The principal difference between this and a FP load is that we don't
724 * zero extend as we are filling a partial chunk of the vector register.
725 * These functions don't support 128 bit loads/stores, which would be
726 * normal load/store operations.
Peter Maydella08582f2014-01-31 14:47:36 +0000727 *
728 * The _i32 versions are useful when operating on 32 bit quantities
729 * (eg for floating point single or using Neon helper functions).
Alex Bennée72430bf2014-01-31 14:47:30 +0000730 */
731
732/* Get value of an element within a vector register */
733static void read_vec_element(DisasContext *s, TCGv_i64 tcg_dest, int srcidx,
734 int element, TCGMemOp memop)
735{
736 int vect_off = vec_reg_offset(srcidx, element, memop & MO_SIZE);
737 switch (memop) {
738 case MO_8:
739 tcg_gen_ld8u_i64(tcg_dest, cpu_env, vect_off);
740 break;
741 case MO_16:
742 tcg_gen_ld16u_i64(tcg_dest, cpu_env, vect_off);
743 break;
744 case MO_32:
745 tcg_gen_ld32u_i64(tcg_dest, cpu_env, vect_off);
746 break;
747 case MO_8|MO_SIGN:
748 tcg_gen_ld8s_i64(tcg_dest, cpu_env, vect_off);
749 break;
750 case MO_16|MO_SIGN:
751 tcg_gen_ld16s_i64(tcg_dest, cpu_env, vect_off);
752 break;
753 case MO_32|MO_SIGN:
754 tcg_gen_ld32s_i64(tcg_dest, cpu_env, vect_off);
755 break;
756 case MO_64:
757 case MO_64|MO_SIGN:
758 tcg_gen_ld_i64(tcg_dest, cpu_env, vect_off);
759 break;
760 default:
761 g_assert_not_reached();
762 }
763}
764
Peter Maydella08582f2014-01-31 14:47:36 +0000765static void read_vec_element_i32(DisasContext *s, TCGv_i32 tcg_dest, int srcidx,
766 int element, TCGMemOp memop)
767{
768 int vect_off = vec_reg_offset(srcidx, element, memop & MO_SIZE);
769 switch (memop) {
770 case MO_8:
771 tcg_gen_ld8u_i32(tcg_dest, cpu_env, vect_off);
772 break;
773 case MO_16:
774 tcg_gen_ld16u_i32(tcg_dest, cpu_env, vect_off);
775 break;
776 case MO_8|MO_SIGN:
777 tcg_gen_ld8s_i32(tcg_dest, cpu_env, vect_off);
778 break;
779 case MO_16|MO_SIGN:
780 tcg_gen_ld16s_i32(tcg_dest, cpu_env, vect_off);
781 break;
782 case MO_32:
783 case MO_32|MO_SIGN:
784 tcg_gen_ld_i32(tcg_dest, cpu_env, vect_off);
785 break;
786 default:
787 g_assert_not_reached();
788 }
789}
790
Alex Bennée72430bf2014-01-31 14:47:30 +0000791/* Set value of an element within a vector register */
792static void write_vec_element(DisasContext *s, TCGv_i64 tcg_src, int destidx,
793 int element, TCGMemOp memop)
794{
795 int vect_off = vec_reg_offset(destidx, element, memop & MO_SIZE);
796 switch (memop) {
797 case MO_8:
798 tcg_gen_st8_i64(tcg_src, cpu_env, vect_off);
799 break;
800 case MO_16:
801 tcg_gen_st16_i64(tcg_src, cpu_env, vect_off);
802 break;
803 case MO_32:
804 tcg_gen_st32_i64(tcg_src, cpu_env, vect_off);
805 break;
806 case MO_64:
807 tcg_gen_st_i64(tcg_src, cpu_env, vect_off);
808 break;
809 default:
810 g_assert_not_reached();
811 }
812}
813
Peter Maydell1f8a73a2014-01-31 14:47:37 +0000814static void write_vec_element_i32(DisasContext *s, TCGv_i32 tcg_src,
815 int destidx, int element, TCGMemOp memop)
816{
817 int vect_off = vec_reg_offset(destidx, element, memop & MO_SIZE);
818 switch (memop) {
819 case MO_8:
820 tcg_gen_st8_i32(tcg_src, cpu_env, vect_off);
821 break;
822 case MO_16:
823 tcg_gen_st16_i32(tcg_src, cpu_env, vect_off);
824 break;
825 case MO_32:
826 tcg_gen_st_i32(tcg_src, cpu_env, vect_off);
827 break;
828 default:
829 g_assert_not_reached();
830 }
831}
832
Alex Bennée72430bf2014-01-31 14:47:30 +0000833/* Clear the high 64 bits of a 128 bit vector (in general non-quad
834 * vector ops all need to do this).
835 */
836static void clear_vec_high(DisasContext *s, int rd)
837{
838 TCGv_i64 tcg_zero = tcg_const_i64(0);
839
840 write_vec_element(s, tcg_zero, rd, 1, MO_64);
841 tcg_temp_free_i64(tcg_zero);
842}
843
844/* Store from vector register to memory */
845static void do_vec_st(DisasContext *s, int srcidx, int element,
846 TCGv_i64 tcg_addr, int size)
847{
848 TCGMemOp memop = MO_TE + size;
849 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
850
851 read_vec_element(s, tcg_tmp, srcidx, element, size);
852 tcg_gen_qemu_st_i64(tcg_tmp, tcg_addr, get_mem_index(s), memop);
853
854 tcg_temp_free_i64(tcg_tmp);
855}
856
857/* Load from memory to vector register */
858static void do_vec_ld(DisasContext *s, int destidx, int element,
859 TCGv_i64 tcg_addr, int size)
860{
861 TCGMemOp memop = MO_TE + size;
862 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
863
864 tcg_gen_qemu_ld_i64(tcg_tmp, tcg_addr, get_mem_index(s), memop);
865 write_vec_element(s, tcg_tmp, destidx, element, size);
866
867 tcg_temp_free_i64(tcg_tmp);
868}
869
870/*
Alex Bennée229b7a02013-12-23 23:27:29 +0000871 * This utility function is for doing register extension with an
872 * optional shift. You will likely want to pass a temporary for the
873 * destination register. See DecodeRegExtend() in the ARM ARM.
874 */
875static void ext_and_shift_reg(TCGv_i64 tcg_out, TCGv_i64 tcg_in,
876 int option, unsigned int shift)
877{
878 int extsize = extract32(option, 0, 2);
879 bool is_signed = extract32(option, 2, 1);
880
881 if (is_signed) {
882 switch (extsize) {
883 case 0:
884 tcg_gen_ext8s_i64(tcg_out, tcg_in);
885 break;
886 case 1:
887 tcg_gen_ext16s_i64(tcg_out, tcg_in);
888 break;
889 case 2:
890 tcg_gen_ext32s_i64(tcg_out, tcg_in);
891 break;
892 case 3:
893 tcg_gen_mov_i64(tcg_out, tcg_in);
894 break;
895 }
896 } else {
897 switch (extsize) {
898 case 0:
899 tcg_gen_ext8u_i64(tcg_out, tcg_in);
900 break;
901 case 1:
902 tcg_gen_ext16u_i64(tcg_out, tcg_in);
903 break;
904 case 2:
905 tcg_gen_ext32u_i64(tcg_out, tcg_in);
906 break;
907 case 3:
908 tcg_gen_mov_i64(tcg_out, tcg_in);
909 break;
910 }
911 }
912
913 if (shift) {
914 tcg_gen_shli_i64(tcg_out, tcg_out, shift);
915 }
916}
917
Peter Maydell4a08d472013-12-22 22:32:27 +0000918static inline void gen_check_sp_alignment(DisasContext *s)
919{
920 /* The AArch64 architecture mandates that (if enabled via PSTATE
921 * or SCTLR bits) there is a check that SP is 16-aligned on every
922 * SP-relative load or store (with an exception generated if it is not).
923 * In line with general QEMU practice regarding misaligned accesses,
924 * we omit these checks for the sake of guest program performance.
925 * This function is provided as a hook so we can more easily add these
926 * checks in future (possibly as a "favour catching guest program bugs
927 * over speed" user selectable option).
928 */
929}
930
931/*
Alex Bennée384b26f2014-01-31 14:47:30 +0000932 * This provides a simple table based table lookup decoder. It is
933 * intended to be used when the relevant bits for decode are too
934 * awkwardly placed and switch/if based logic would be confusing and
935 * deeply nested. Since it's a linear search through the table, tables
936 * should be kept small.
937 *
938 * It returns the first handler where insn & mask == pattern, or
939 * NULL if there is no match.
940 * The table is terminated by an empty mask (i.e. 0)
941 */
942static inline AArch64DecodeFn *lookup_disas_fn(const AArch64DecodeTable *table,
943 uint32_t insn)
944{
945 const AArch64DecodeTable *tptr = table;
946
947 while (tptr->mask) {
948 if ((insn & tptr->mask) == tptr->pattern) {
949 return tptr->disas_fn;
950 }
951 tptr++;
952 }
953 return NULL;
954}
955
956/*
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +0000957 * the instruction disassembly implemented here matches
958 * the instruction encoding classifications in chapter 3 (C3)
959 * of the ARM Architecture Reference Manual (DDI0487A_a)
960 */
961
Alexander Graf11e169d2013-12-17 19:42:32 +0000962/* C3.2.7 Unconditional branch (immediate)
963 * 31 30 26 25 0
964 * +----+-----------+-------------------------------------+
965 * | op | 0 0 1 0 1 | imm26 |
966 * +----+-----------+-------------------------------------+
967 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +0000968static void disas_uncond_b_imm(DisasContext *s, uint32_t insn)
969{
Alexander Graf11e169d2013-12-17 19:42:32 +0000970 uint64_t addr = s->pc + sextract32(insn, 0, 26) * 4 - 4;
971
972 if (insn & (1 << 31)) {
973 /* C5.6.26 BL Branch with link */
974 tcg_gen_movi_i64(cpu_reg(s, 30), s->pc);
975 }
976
977 /* C5.6.20 B Branch / C5.6.26 BL Branch with link */
978 gen_goto_tb(s, 0, addr);
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +0000979}
980
Alexander Graf60e53382013-12-17 19:42:33 +0000981/* C3.2.1 Compare & branch (immediate)
982 * 31 30 25 24 23 5 4 0
983 * +----+-------------+----+---------------------+--------+
984 * | sf | 0 1 1 0 1 0 | op | imm19 | Rt |
985 * +----+-------------+----+---------------------+--------+
986 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +0000987static void disas_comp_b_imm(DisasContext *s, uint32_t insn)
988{
Alexander Graf60e53382013-12-17 19:42:33 +0000989 unsigned int sf, op, rt;
990 uint64_t addr;
991 int label_match;
992 TCGv_i64 tcg_cmp;
993
994 sf = extract32(insn, 31, 1);
995 op = extract32(insn, 24, 1); /* 0: CBZ; 1: CBNZ */
996 rt = extract32(insn, 0, 5);
997 addr = s->pc + sextract32(insn, 5, 19) * 4 - 4;
998
999 tcg_cmp = read_cpu_reg(s, rt, sf);
1000 label_match = gen_new_label();
1001
1002 tcg_gen_brcondi_i64(op ? TCG_COND_NE : TCG_COND_EQ,
1003 tcg_cmp, 0, label_match);
1004
1005 gen_goto_tb(s, 0, s->pc);
1006 gen_set_label(label_match);
1007 gen_goto_tb(s, 1, addr);
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00001008}
1009
Alexander Grafdb0f7952013-12-17 19:42:33 +00001010/* C3.2.5 Test & branch (immediate)
1011 * 31 30 25 24 23 19 18 5 4 0
1012 * +----+-------------+----+-------+-------------+------+
1013 * | b5 | 0 1 1 0 1 1 | op | b40 | imm14 | Rt |
1014 * +----+-------------+----+-------+-------------+------+
1015 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00001016static void disas_test_b_imm(DisasContext *s, uint32_t insn)
1017{
Alexander Grafdb0f7952013-12-17 19:42:33 +00001018 unsigned int bit_pos, op, rt;
1019 uint64_t addr;
1020 int label_match;
1021 TCGv_i64 tcg_cmp;
1022
1023 bit_pos = (extract32(insn, 31, 1) << 5) | extract32(insn, 19, 5);
1024 op = extract32(insn, 24, 1); /* 0: TBZ; 1: TBNZ */
1025 addr = s->pc + sextract32(insn, 5, 14) * 4 - 4;
1026 rt = extract32(insn, 0, 5);
1027
1028 tcg_cmp = tcg_temp_new_i64();
1029 tcg_gen_andi_i64(tcg_cmp, cpu_reg(s, rt), (1ULL << bit_pos));
1030 label_match = gen_new_label();
1031 tcg_gen_brcondi_i64(op ? TCG_COND_NE : TCG_COND_EQ,
1032 tcg_cmp, 0, label_match);
1033 tcg_temp_free_i64(tcg_cmp);
1034 gen_goto_tb(s, 0, s->pc);
1035 gen_set_label(label_match);
1036 gen_goto_tb(s, 1, addr);
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00001037}
1038
Alexander Graf39fb7302013-12-17 19:42:33 +00001039/* C3.2.2 / C5.6.19 Conditional branch (immediate)
1040 * 31 25 24 23 5 4 3 0
1041 * +---------------+----+---------------------+----+------+
1042 * | 0 1 0 1 0 1 0 | o1 | imm19 | o0 | cond |
1043 * +---------------+----+---------------------+----+------+
1044 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00001045static void disas_cond_b_imm(DisasContext *s, uint32_t insn)
1046{
Alexander Graf39fb7302013-12-17 19:42:33 +00001047 unsigned int cond;
1048 uint64_t addr;
1049
1050 if ((insn & (1 << 4)) || (insn & (1 << 24))) {
1051 unallocated_encoding(s);
1052 return;
1053 }
1054 addr = s->pc + sextract32(insn, 5, 19) * 4 - 4;
1055 cond = extract32(insn, 0, 4);
1056
1057 if (cond < 0x0e) {
1058 /* genuinely conditional branches */
1059 int label_match = gen_new_label();
1060 arm_gen_test_cc(cond, label_match);
1061 gen_goto_tb(s, 0, s->pc);
1062 gen_set_label(label_match);
1063 gen_goto_tb(s, 1, addr);
1064 } else {
1065 /* 0xe and 0xf are both "always" conditions */
1066 gen_goto_tb(s, 0, addr);
1067 }
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00001068}
1069
Claudio Fontana87462e02013-12-17 19:42:32 +00001070/* C5.6.68 HINT */
1071static void handle_hint(DisasContext *s, uint32_t insn,
1072 unsigned int op1, unsigned int op2, unsigned int crm)
1073{
1074 unsigned int selector = crm << 3 | op2;
1075
1076 if (op1 != 3) {
1077 unallocated_encoding(s);
1078 return;
1079 }
1080
1081 switch (selector) {
1082 case 0: /* NOP */
1083 return;
Peter Maydell1ed69e82014-02-26 17:20:06 +00001084 case 3: /* WFI */
1085 s->is_jmp = DISAS_WFI;
1086 return;
Claudio Fontana87462e02013-12-17 19:42:32 +00001087 case 1: /* YIELD */
1088 case 2: /* WFE */
Claudio Fontana87462e02013-12-17 19:42:32 +00001089 case 4: /* SEV */
1090 case 5: /* SEVL */
1091 /* we treat all as NOP at least for now */
1092 return;
1093 default:
1094 /* default specified as NOP equivalent */
1095 return;
1096 }
1097}
1098
Michael Matzfa2ef212014-01-04 22:15:47 +00001099static void gen_clrex(DisasContext *s, uint32_t insn)
1100{
1101 tcg_gen_movi_i64(cpu_exclusive_addr, -1);
1102}
1103
Claudio Fontana87462e02013-12-17 19:42:32 +00001104/* CLREX, DSB, DMB, ISB */
1105static void handle_sync(DisasContext *s, uint32_t insn,
1106 unsigned int op1, unsigned int op2, unsigned int crm)
1107{
1108 if (op1 != 3) {
1109 unallocated_encoding(s);
1110 return;
1111 }
1112
1113 switch (op2) {
1114 case 2: /* CLREX */
Michael Matzfa2ef212014-01-04 22:15:47 +00001115 gen_clrex(s, insn);
Claudio Fontana87462e02013-12-17 19:42:32 +00001116 return;
1117 case 4: /* DSB */
1118 case 5: /* DMB */
1119 case 6: /* ISB */
1120 /* We don't emulate caches so barriers are no-ops */
1121 return;
1122 default:
1123 unallocated_encoding(s);
1124 return;
1125 }
1126}
1127
1128/* C5.6.130 MSR (immediate) - move immediate to processor state field */
1129static void handle_msr_i(DisasContext *s, uint32_t insn,
1130 unsigned int op1, unsigned int op2, unsigned int crm)
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00001131{
Peter Maydell9cfa0b42014-02-26 17:20:06 +00001132 int op = op1 << 3 | op2;
1133 switch (op) {
1134 case 0x05: /* SPSel */
1135 if (s->current_pl == 0) {
1136 unallocated_encoding(s);
1137 return;
1138 }
1139 /* fall through */
1140 case 0x1e: /* DAIFSet */
1141 case 0x1f: /* DAIFClear */
1142 {
1143 TCGv_i32 tcg_imm = tcg_const_i32(crm);
1144 TCGv_i32 tcg_op = tcg_const_i32(op);
1145 gen_a64_set_pc_im(s->pc - 4);
1146 gen_helper_msr_i_pstate(cpu_env, tcg_op, tcg_imm);
1147 tcg_temp_free_i32(tcg_imm);
1148 tcg_temp_free_i32(tcg_op);
1149 s->is_jmp = DISAS_UPDATE;
1150 break;
1151 }
1152 default:
1153 unallocated_encoding(s);
1154 return;
1155 }
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00001156}
1157
Peter Maydellb0d2b7d2014-01-04 22:15:45 +00001158static void gen_get_nzcv(TCGv_i64 tcg_rt)
1159{
1160 TCGv_i32 tmp = tcg_temp_new_i32();
1161 TCGv_i32 nzcv = tcg_temp_new_i32();
1162
1163 /* build bit 31, N */
1164 tcg_gen_andi_i32(nzcv, cpu_NF, (1 << 31));
1165 /* build bit 30, Z */
1166 tcg_gen_setcondi_i32(TCG_COND_EQ, tmp, cpu_ZF, 0);
1167 tcg_gen_deposit_i32(nzcv, nzcv, tmp, 30, 1);
1168 /* build bit 29, C */
1169 tcg_gen_deposit_i32(nzcv, nzcv, cpu_CF, 29, 1);
1170 /* build bit 28, V */
1171 tcg_gen_shri_i32(tmp, cpu_VF, 31);
1172 tcg_gen_deposit_i32(nzcv, nzcv, tmp, 28, 1);
1173 /* generate result */
1174 tcg_gen_extu_i32_i64(tcg_rt, nzcv);
1175
1176 tcg_temp_free_i32(nzcv);
1177 tcg_temp_free_i32(tmp);
1178}
1179
1180static void gen_set_nzcv(TCGv_i64 tcg_rt)
1181
1182{
1183 TCGv_i32 nzcv = tcg_temp_new_i32();
1184
1185 /* take NZCV from R[t] */
1186 tcg_gen_trunc_i64_i32(nzcv, tcg_rt);
1187
1188 /* bit 31, N */
1189 tcg_gen_andi_i32(cpu_NF, nzcv, (1 << 31));
1190 /* bit 30, Z */
1191 tcg_gen_andi_i32(cpu_ZF, nzcv, (1 << 30));
1192 tcg_gen_setcondi_i32(TCG_COND_EQ, cpu_ZF, cpu_ZF, 0);
1193 /* bit 29, C */
1194 tcg_gen_andi_i32(cpu_CF, nzcv, (1 << 29));
1195 tcg_gen_shri_i32(cpu_CF, cpu_CF, 29);
1196 /* bit 28, V */
1197 tcg_gen_andi_i32(cpu_VF, nzcv, (1 << 28));
1198 tcg_gen_shli_i32(cpu_VF, cpu_VF, 3);
1199 tcg_temp_free_i32(nzcv);
1200}
1201
Peter Maydellfea50522014-01-04 22:15:45 +00001202/* C5.6.129 MRS - move from system register
1203 * C5.6.131 MSR (register) - move to system register
1204 * C5.6.204 SYS
1205 * C5.6.205 SYSL
1206 * These are all essentially the same insn in 'read' and 'write'
1207 * versions, with varying op0 fields.
1208 */
1209static void handle_sys(DisasContext *s, uint32_t insn, bool isread,
1210 unsigned int op0, unsigned int op1, unsigned int op2,
Claudio Fontana87462e02013-12-17 19:42:32 +00001211 unsigned int crn, unsigned int crm, unsigned int rt)
1212{
Peter Maydellfea50522014-01-04 22:15:45 +00001213 const ARMCPRegInfo *ri;
1214 TCGv_i64 tcg_rt;
Claudio Fontana87462e02013-12-17 19:42:32 +00001215
Peter Maydellfea50522014-01-04 22:15:45 +00001216 ri = get_arm_cp_reginfo(s->cp_regs,
1217 ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP,
1218 crn, crm, op0, op1, op2));
Claudio Fontana87462e02013-12-17 19:42:32 +00001219
Peter Maydellfea50522014-01-04 22:15:45 +00001220 if (!ri) {
Peter Maydell626187d2014-02-20 10:35:52 +00001221 /* Unknown register; this might be a guest error or a QEMU
1222 * unimplemented feature.
1223 */
1224 qemu_log_mask(LOG_UNIMP, "%s access to unsupported AArch64 "
1225 "system register op0:%d op1:%d crn:%d crm:%d op2:%d\n",
1226 isread ? "read" : "write", op0, op1, crn, crm, op2);
Peter Maydellfea50522014-01-04 22:15:45 +00001227 unallocated_encoding(s);
1228 return;
1229 }
1230
1231 /* Check access permissions */
1232 if (!cp_access_ok(s->current_pl, ri, isread)) {
1233 unallocated_encoding(s);
1234 return;
1235 }
1236
Peter Maydellf59df3f2014-02-20 10:35:52 +00001237 if (ri->accessfn) {
1238 /* Emit code to perform further access permissions checks at
1239 * runtime; this may result in an exception.
1240 */
1241 TCGv_ptr tmpptr;
1242 gen_a64_set_pc_im(s->pc - 4);
1243 tmpptr = tcg_const_ptr(ri);
1244 gen_helper_access_check_cp_reg(cpu_env, tmpptr);
1245 tcg_temp_free_ptr(tmpptr);
1246 }
1247
Peter Maydellfea50522014-01-04 22:15:45 +00001248 /* Handle special cases first */
1249 switch (ri->type & ~(ARM_CP_FLAG_MASK & ~ARM_CP_SPECIAL)) {
1250 case ARM_CP_NOP:
1251 return;
Peter Maydellb0d2b7d2014-01-04 22:15:45 +00001252 case ARM_CP_NZCV:
1253 tcg_rt = cpu_reg(s, rt);
1254 if (isread) {
1255 gen_get_nzcv(tcg_rt);
1256 } else {
1257 gen_set_nzcv(tcg_rt);
1258 }
1259 return;
Peter Maydell0eef9d92014-02-26 17:20:02 +00001260 case ARM_CP_CURRENTEL:
1261 /* Reads as current EL value from pstate, which is
1262 * guaranteed to be constant by the tb flags.
1263 */
1264 tcg_rt = cpu_reg(s, rt);
1265 tcg_gen_movi_i64(tcg_rt, s->current_pl << 2);
1266 return;
Peter Maydellfea50522014-01-04 22:15:45 +00001267 default:
1268 break;
1269 }
1270
1271 if (use_icount && (ri->type & ARM_CP_IO)) {
1272 gen_io_start();
1273 }
1274
1275 tcg_rt = cpu_reg(s, rt);
1276
1277 if (isread) {
1278 if (ri->type & ARM_CP_CONST) {
1279 tcg_gen_movi_i64(tcg_rt, ri->resetvalue);
1280 } else if (ri->readfn) {
1281 TCGv_ptr tmpptr;
Peter Maydellfea50522014-01-04 22:15:45 +00001282 tmpptr = tcg_const_ptr(ri);
1283 gen_helper_get_cp_reg64(tcg_rt, cpu_env, tmpptr);
1284 tcg_temp_free_ptr(tmpptr);
1285 } else {
1286 tcg_gen_ld_i64(tcg_rt, cpu_env, ri->fieldoffset);
1287 }
1288 } else {
1289 if (ri->type & ARM_CP_CONST) {
1290 /* If not forbidden by access permissions, treat as WI */
1291 return;
1292 } else if (ri->writefn) {
1293 TCGv_ptr tmpptr;
Peter Maydellfea50522014-01-04 22:15:45 +00001294 tmpptr = tcg_const_ptr(ri);
1295 gen_helper_set_cp_reg64(cpu_env, tmpptr, tcg_rt);
1296 tcg_temp_free_ptr(tmpptr);
1297 } else {
1298 tcg_gen_st_i64(tcg_rt, cpu_env, ri->fieldoffset);
1299 }
1300 }
1301
1302 if (use_icount && (ri->type & ARM_CP_IO)) {
1303 /* I/O operations must end the TB here (whether read or write) */
1304 gen_io_end();
1305 s->is_jmp = DISAS_UPDATE;
1306 } else if (!isread && !(ri->type & ARM_CP_SUPPRESS_TB_END)) {
1307 /* We default to ending the TB on a coprocessor register write,
1308 * but allow this to be suppressed by the register definition
1309 * (usually only necessary to work around guest bugs).
1310 */
1311 s->is_jmp = DISAS_UPDATE;
1312 }
Claudio Fontana87462e02013-12-17 19:42:32 +00001313}
1314
1315/* C3.2.4 System
1316 * 31 22 21 20 19 18 16 15 12 11 8 7 5 4 0
1317 * +---------------------+---+-----+-----+-------+-------+-----+------+
1318 * | 1 1 0 1 0 1 0 1 0 0 | L | op0 | op1 | CRn | CRm | op2 | Rt |
1319 * +---------------------+---+-----+-----+-------+-------+-----+------+
1320 */
1321static void disas_system(DisasContext *s, uint32_t insn)
1322{
1323 unsigned int l, op0, op1, crn, crm, op2, rt;
1324 l = extract32(insn, 21, 1);
1325 op0 = extract32(insn, 19, 2);
1326 op1 = extract32(insn, 16, 3);
1327 crn = extract32(insn, 12, 4);
1328 crm = extract32(insn, 8, 4);
1329 op2 = extract32(insn, 5, 3);
1330 rt = extract32(insn, 0, 5);
1331
1332 if (op0 == 0) {
1333 if (l || rt != 31) {
1334 unallocated_encoding(s);
1335 return;
1336 }
1337 switch (crn) {
1338 case 2: /* C5.6.68 HINT */
1339 handle_hint(s, insn, op1, op2, crm);
1340 break;
1341 case 3: /* CLREX, DSB, DMB, ISB */
1342 handle_sync(s, insn, op1, op2, crm);
1343 break;
1344 case 4: /* C5.6.130 MSR (immediate) */
1345 handle_msr_i(s, insn, op1, op2, crm);
1346 break;
1347 default:
1348 unallocated_encoding(s);
1349 break;
1350 }
1351 return;
1352 }
Peter Maydellfea50522014-01-04 22:15:45 +00001353 handle_sys(s, insn, l, op0, op1, op2, crn, crm, rt);
Claudio Fontana87462e02013-12-17 19:42:32 +00001354}
1355
Alexander Graf9618e802013-12-23 23:27:30 +00001356/* C3.2.3 Exception generation
1357 *
1358 * 31 24 23 21 20 5 4 2 1 0
1359 * +-----------------+-----+------------------------+-----+----+
1360 * | 1 1 0 1 0 1 0 0 | opc | imm16 | op2 | LL |
1361 * +-----------------------+------------------------+----------+
1362 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00001363static void disas_exc(DisasContext *s, uint32_t insn)
1364{
Alexander Graf9618e802013-12-23 23:27:30 +00001365 int opc = extract32(insn, 21, 3);
1366 int op2_ll = extract32(insn, 0, 5);
1367
1368 switch (opc) {
1369 case 0:
1370 /* SVC, HVC, SMC; since we don't support the Virtualization
1371 * or TrustZone extensions these all UNDEF except SVC.
1372 */
1373 if (op2_ll != 1) {
1374 unallocated_encoding(s);
1375 break;
1376 }
1377 gen_exception_insn(s, 0, EXCP_SWI);
1378 break;
1379 case 1:
1380 if (op2_ll != 0) {
1381 unallocated_encoding(s);
1382 break;
1383 }
1384 /* BRK */
1385 gen_exception_insn(s, 0, EXCP_BKPT);
1386 break;
1387 case 2:
1388 if (op2_ll != 0) {
1389 unallocated_encoding(s);
1390 break;
1391 }
1392 /* HLT */
1393 unsupported_encoding(s, insn);
1394 break;
1395 case 5:
1396 if (op2_ll < 1 || op2_ll > 3) {
1397 unallocated_encoding(s);
1398 break;
1399 }
1400 /* DCPS1, DCPS2, DCPS3 */
1401 unsupported_encoding(s, insn);
1402 break;
1403 default:
1404 unallocated_encoding(s);
1405 break;
1406 }
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00001407}
1408
Alexander Grafb001c8c2013-12-17 19:42:33 +00001409/* C3.2.7 Unconditional branch (register)
1410 * 31 25 24 21 20 16 15 10 9 5 4 0
1411 * +---------------+-------+-------+-------+------+-------+
1412 * | 1 1 0 1 0 1 1 | opc | op2 | op3 | Rn | op4 |
1413 * +---------------+-------+-------+-------+------+-------+
1414 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00001415static void disas_uncond_b_reg(DisasContext *s, uint32_t insn)
1416{
Alexander Grafb001c8c2013-12-17 19:42:33 +00001417 unsigned int opc, op2, op3, rn, op4;
1418
1419 opc = extract32(insn, 21, 4);
1420 op2 = extract32(insn, 16, 5);
1421 op3 = extract32(insn, 10, 6);
1422 rn = extract32(insn, 5, 5);
1423 op4 = extract32(insn, 0, 5);
1424
1425 if (op4 != 0x0 || op3 != 0x0 || op2 != 0x1f) {
1426 unallocated_encoding(s);
1427 return;
1428 }
1429
1430 switch (opc) {
1431 case 0: /* BR */
1432 case 2: /* RET */
1433 break;
1434 case 1: /* BLR */
1435 tcg_gen_movi_i64(cpu_reg(s, 30), s->pc);
1436 break;
1437 case 4: /* ERET */
1438 case 5: /* DRPS */
1439 if (rn != 0x1f) {
1440 unallocated_encoding(s);
1441 } else {
1442 unsupported_encoding(s, insn);
1443 }
1444 return;
1445 default:
1446 unallocated_encoding(s);
1447 return;
1448 }
1449
1450 tcg_gen_mov_i64(cpu_pc, cpu_reg(s, rn));
1451 s->is_jmp = DISAS_JUMP;
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00001452}
1453
1454/* C3.2 Branches, exception generating and system instructions */
1455static void disas_b_exc_sys(DisasContext *s, uint32_t insn)
1456{
1457 switch (extract32(insn, 25, 7)) {
1458 case 0x0a: case 0x0b:
1459 case 0x4a: case 0x4b: /* Unconditional branch (immediate) */
1460 disas_uncond_b_imm(s, insn);
1461 break;
1462 case 0x1a: case 0x5a: /* Compare & branch (immediate) */
1463 disas_comp_b_imm(s, insn);
1464 break;
1465 case 0x1b: case 0x5b: /* Test & branch (immediate) */
1466 disas_test_b_imm(s, insn);
1467 break;
1468 case 0x2a: /* Conditional branch (immediate) */
1469 disas_cond_b_imm(s, insn);
1470 break;
1471 case 0x6a: /* Exception generation / System */
1472 if (insn & (1 << 24)) {
1473 disas_system(s, insn);
1474 } else {
1475 disas_exc(s, insn);
1476 }
1477 break;
1478 case 0x6b: /* Unconditional branch (register) */
1479 disas_uncond_b_reg(s, insn);
1480 break;
1481 default:
1482 unallocated_encoding(s);
1483 break;
1484 }
1485}
1486
Michael Matzfa2ef212014-01-04 22:15:47 +00001487/*
1488 * Load/Store exclusive instructions are implemented by remembering
1489 * the value/address loaded, and seeing if these are the same
1490 * when the store is performed. This is not actually the architecturally
1491 * mandated semantics, but it works for typical guest code sequences
1492 * and avoids having to monitor regular stores.
1493 *
1494 * In system emulation mode only one CPU will be running at once, so
1495 * this sequence is effectively atomic. In user emulation mode we
1496 * throw an exception and handle the atomic operation elsewhere.
1497 */
1498static void gen_load_exclusive(DisasContext *s, int rt, int rt2,
1499 TCGv_i64 addr, int size, bool is_pair)
1500{
1501 TCGv_i64 tmp = tcg_temp_new_i64();
1502 TCGMemOp memop = MO_TE + size;
1503
1504 g_assert(size <= 3);
1505 tcg_gen_qemu_ld_i64(tmp, addr, get_mem_index(s), memop);
1506
1507 if (is_pair) {
1508 TCGv_i64 addr2 = tcg_temp_new_i64();
1509 TCGv_i64 hitmp = tcg_temp_new_i64();
1510
1511 g_assert(size >= 2);
1512 tcg_gen_addi_i64(addr2, addr, 1 << size);
1513 tcg_gen_qemu_ld_i64(hitmp, addr2, get_mem_index(s), memop);
1514 tcg_temp_free_i64(addr2);
1515 tcg_gen_mov_i64(cpu_exclusive_high, hitmp);
1516 tcg_gen_mov_i64(cpu_reg(s, rt2), hitmp);
1517 tcg_temp_free_i64(hitmp);
1518 }
1519
1520 tcg_gen_mov_i64(cpu_exclusive_val, tmp);
1521 tcg_gen_mov_i64(cpu_reg(s, rt), tmp);
1522
1523 tcg_temp_free_i64(tmp);
1524 tcg_gen_mov_i64(cpu_exclusive_addr, addr);
1525}
1526
1527#ifdef CONFIG_USER_ONLY
1528static void gen_store_exclusive(DisasContext *s, int rd, int rt, int rt2,
1529 TCGv_i64 addr, int size, int is_pair)
1530{
1531 tcg_gen_mov_i64(cpu_exclusive_test, addr);
1532 tcg_gen_movi_i32(cpu_exclusive_info,
1533 size | is_pair << 2 | (rd << 4) | (rt << 9) | (rt2 << 14));
1534 gen_exception_insn(s, 4, EXCP_STREX);
1535}
1536#else
1537static void gen_store_exclusive(DisasContext *s, int rd, int rt, int rt2,
Peter Maydelld324b362014-02-20 10:35:55 +00001538 TCGv_i64 inaddr, int size, int is_pair)
Michael Matzfa2ef212014-01-04 22:15:47 +00001539{
Peter Maydelld324b362014-02-20 10:35:55 +00001540 /* if (env->exclusive_addr == addr && env->exclusive_val == [addr]
1541 * && (!is_pair || env->exclusive_high == [addr + datasize])) {
1542 * [addr] = {Rt};
1543 * if (is_pair) {
1544 * [addr + datasize] = {Rt2};
1545 * }
1546 * {Rd} = 0;
1547 * } else {
1548 * {Rd} = 1;
1549 * }
1550 * env->exclusive_addr = -1;
1551 */
1552 int fail_label = gen_new_label();
1553 int done_label = gen_new_label();
1554 TCGv_i64 addr = tcg_temp_local_new_i64();
1555 TCGv_i64 tmp;
1556
1557 /* Copy input into a local temp so it is not trashed when the
1558 * basic block ends at the branch insn.
1559 */
1560 tcg_gen_mov_i64(addr, inaddr);
1561 tcg_gen_brcond_i64(TCG_COND_NE, addr, cpu_exclusive_addr, fail_label);
1562
1563 tmp = tcg_temp_new_i64();
1564 tcg_gen_qemu_ld_i64(tmp, addr, get_mem_index(s), MO_TE + size);
1565 tcg_gen_brcond_i64(TCG_COND_NE, tmp, cpu_exclusive_val, fail_label);
1566 tcg_temp_free_i64(tmp);
1567
1568 if (is_pair) {
1569 TCGv_i64 addrhi = tcg_temp_new_i64();
1570 TCGv_i64 tmphi = tcg_temp_new_i64();
1571
1572 tcg_gen_addi_i64(addrhi, addr, 1 << size);
1573 tcg_gen_qemu_ld_i64(tmphi, addrhi, get_mem_index(s), MO_TE + size);
1574 tcg_gen_brcond_i64(TCG_COND_NE, tmphi, cpu_exclusive_high, fail_label);
1575
1576 tcg_temp_free_i64(tmphi);
1577 tcg_temp_free_i64(addrhi);
1578 }
1579
1580 /* We seem to still have the exclusive monitor, so do the store */
1581 tcg_gen_qemu_st_i64(cpu_reg(s, rt), addr, get_mem_index(s), MO_TE + size);
1582 if (is_pair) {
1583 TCGv_i64 addrhi = tcg_temp_new_i64();
1584
1585 tcg_gen_addi_i64(addrhi, addr, 1 << size);
1586 tcg_gen_qemu_st_i64(cpu_reg(s, rt2), addrhi,
1587 get_mem_index(s), MO_TE + size);
1588 tcg_temp_free_i64(addrhi);
1589 }
1590
1591 tcg_temp_free_i64(addr);
1592
1593 tcg_gen_movi_i64(cpu_reg(s, rd), 0);
1594 tcg_gen_br(done_label);
1595 gen_set_label(fail_label);
1596 tcg_gen_movi_i64(cpu_reg(s, rd), 1);
1597 gen_set_label(done_label);
1598 tcg_gen_movi_i64(cpu_exclusive_addr, -1);
1599
Michael Matzfa2ef212014-01-04 22:15:47 +00001600}
1601#endif
1602
1603/* C3.3.6 Load/store exclusive
1604 *
1605 * 31 30 29 24 23 22 21 20 16 15 14 10 9 5 4 0
1606 * +-----+-------------+----+---+----+------+----+-------+------+------+
1607 * | sz | 0 0 1 0 0 0 | o2 | L | o1 | Rs | o0 | Rt2 | Rn | Rt |
1608 * +-----+-------------+----+---+----+------+----+-------+------+------+
1609 *
1610 * sz: 00 -> 8 bit, 01 -> 16 bit, 10 -> 32 bit, 11 -> 64 bit
1611 * L: 0 -> store, 1 -> load
1612 * o2: 0 -> exclusive, 1 -> not
1613 * o1: 0 -> single register, 1 -> register pair
1614 * o0: 1 -> load-acquire/store-release, 0 -> not
1615 *
1616 * o0 == 0 AND o2 == 1 is un-allocated
1617 * o1 == 1 is un-allocated except for 32 and 64 bit sizes
1618 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00001619static void disas_ldst_excl(DisasContext *s, uint32_t insn)
1620{
Michael Matzfa2ef212014-01-04 22:15:47 +00001621 int rt = extract32(insn, 0, 5);
1622 int rn = extract32(insn, 5, 5);
1623 int rt2 = extract32(insn, 10, 5);
1624 int is_lasr = extract32(insn, 15, 1);
1625 int rs = extract32(insn, 16, 5);
1626 int is_pair = extract32(insn, 21, 1);
1627 int is_store = !extract32(insn, 22, 1);
1628 int is_excl = !extract32(insn, 23, 1);
1629 int size = extract32(insn, 30, 2);
1630 TCGv_i64 tcg_addr;
1631
1632 if ((!is_excl && !is_lasr) ||
1633 (is_pair && size < 2)) {
1634 unallocated_encoding(s);
1635 return;
1636 }
1637
1638 if (rn == 31) {
1639 gen_check_sp_alignment(s);
1640 }
1641 tcg_addr = read_cpu_reg_sp(s, rn, 1);
1642
1643 /* Note that since TCG is single threaded load-acquire/store-release
1644 * semantics require no extra if (is_lasr) { ... } handling.
1645 */
1646
1647 if (is_excl) {
1648 if (!is_store) {
1649 gen_load_exclusive(s, rt, rt2, tcg_addr, size, is_pair);
1650 } else {
1651 gen_store_exclusive(s, rs, rt, rt2, tcg_addr, size, is_pair);
1652 }
1653 } else {
1654 TCGv_i64 tcg_rt = cpu_reg(s, rt);
1655 if (is_store) {
1656 do_gpr_st(s, tcg_rt, tcg_addr, size);
1657 } else {
1658 do_gpr_ld(s, tcg_rt, tcg_addr, size, false, false);
1659 }
1660 if (is_pair) {
1661 TCGv_i64 tcg_rt2 = cpu_reg(s, rt);
1662 tcg_gen_addi_i64(tcg_addr, tcg_addr, 1 << size);
1663 if (is_store) {
1664 do_gpr_st(s, tcg_rt2, tcg_addr, size);
1665 } else {
1666 do_gpr_ld(s, tcg_rt2, tcg_addr, size, false, false);
1667 }
1668 }
1669 }
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00001670}
1671
Alexander Graf32b64e82014-01-04 22:15:46 +00001672/*
1673 * C3.3.5 Load register (literal)
1674 *
1675 * 31 30 29 27 26 25 24 23 5 4 0
1676 * +-----+-------+---+-----+-------------------+-------+
1677 * | opc | 0 1 1 | V | 0 0 | imm19 | Rt |
1678 * +-----+-------+---+-----+-------------------+-------+
1679 *
1680 * V: 1 -> vector (simd/fp)
1681 * opc (non-vector): 00 -> 32 bit, 01 -> 64 bit,
1682 * 10-> 32 bit signed, 11 -> prefetch
1683 * opc (vector): 00 -> 32 bit, 01 -> 64 bit, 10 -> 128 bit (11 unallocated)
1684 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00001685static void disas_ld_lit(DisasContext *s, uint32_t insn)
1686{
Alexander Graf32b64e82014-01-04 22:15:46 +00001687 int rt = extract32(insn, 0, 5);
1688 int64_t imm = sextract32(insn, 5, 19) << 2;
1689 bool is_vector = extract32(insn, 26, 1);
1690 int opc = extract32(insn, 30, 2);
1691 bool is_signed = false;
1692 int size = 2;
1693 TCGv_i64 tcg_rt, tcg_addr;
1694
1695 if (is_vector) {
1696 if (opc == 3) {
1697 unallocated_encoding(s);
1698 return;
1699 }
1700 size = 2 + opc;
1701 } else {
1702 if (opc == 3) {
1703 /* PRFM (literal) : prefetch */
1704 return;
1705 }
1706 size = 2 + extract32(opc, 0, 1);
1707 is_signed = extract32(opc, 1, 1);
1708 }
1709
1710 tcg_rt = cpu_reg(s, rt);
1711
1712 tcg_addr = tcg_const_i64((s->pc - 4) + imm);
1713 if (is_vector) {
1714 do_fp_ld(s, rt, tcg_addr, size);
1715 } else {
1716 do_gpr_ld(s, tcg_rt, tcg_addr, size, is_signed, false);
1717 }
1718 tcg_temp_free_i64(tcg_addr);
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00001719}
1720
Peter Maydell4a08d472013-12-22 22:32:27 +00001721/*
1722 * C5.6.80 LDNP (Load Pair - non-temporal hint)
1723 * C5.6.81 LDP (Load Pair - non vector)
1724 * C5.6.82 LDPSW (Load Pair Signed Word - non vector)
1725 * C5.6.176 STNP (Store Pair - non-temporal hint)
1726 * C5.6.177 STP (Store Pair - non vector)
1727 * C6.3.165 LDNP (Load Pair of SIMD&FP - non-temporal hint)
1728 * C6.3.165 LDP (Load Pair of SIMD&FP)
1729 * C6.3.284 STNP (Store Pair of SIMD&FP - non-temporal hint)
1730 * C6.3.284 STP (Store Pair of SIMD&FP)
1731 *
1732 * 31 30 29 27 26 25 24 23 22 21 15 14 10 9 5 4 0
1733 * +-----+-------+---+---+-------+---+-----------------------------+
1734 * | opc | 1 0 1 | V | 0 | index | L | imm7 | Rt2 | Rn | Rt |
1735 * +-----+-------+---+---+-------+---+-------+-------+------+------+
1736 *
1737 * opc: LDP/STP/LDNP/STNP 00 -> 32 bit, 10 -> 64 bit
1738 * LDPSW 01
1739 * LDP/STP/LDNP/STNP (SIMD) 00 -> 32 bit, 01 -> 64 bit, 10 -> 128 bit
1740 * V: 0 -> GPR, 1 -> Vector
1741 * idx: 00 -> signed offset with non-temporal hint, 01 -> post-index,
1742 * 10 -> signed offset, 11 -> pre-index
1743 * L: 0 -> Store 1 -> Load
1744 *
1745 * Rt, Rt2 = GPR or SIMD registers to be stored
1746 * Rn = general purpose register containing address
1747 * imm7 = signed offset (multiple of 4 or 8 depending on size)
1748 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00001749static void disas_ldst_pair(DisasContext *s, uint32_t insn)
1750{
Peter Maydell4a08d472013-12-22 22:32:27 +00001751 int rt = extract32(insn, 0, 5);
1752 int rn = extract32(insn, 5, 5);
1753 int rt2 = extract32(insn, 10, 5);
1754 int64_t offset = sextract32(insn, 15, 7);
1755 int index = extract32(insn, 23, 2);
1756 bool is_vector = extract32(insn, 26, 1);
1757 bool is_load = extract32(insn, 22, 1);
1758 int opc = extract32(insn, 30, 2);
1759
1760 bool is_signed = false;
1761 bool postindex = false;
1762 bool wback = false;
1763
1764 TCGv_i64 tcg_addr; /* calculated address */
1765 int size;
1766
1767 if (opc == 3) {
1768 unallocated_encoding(s);
1769 return;
1770 }
1771
1772 if (is_vector) {
1773 size = 2 + opc;
1774 } else {
1775 size = 2 + extract32(opc, 1, 1);
1776 is_signed = extract32(opc, 0, 1);
1777 if (!is_load && is_signed) {
1778 unallocated_encoding(s);
1779 return;
1780 }
1781 }
1782
1783 switch (index) {
1784 case 1: /* post-index */
1785 postindex = true;
1786 wback = true;
1787 break;
1788 case 0:
1789 /* signed offset with "non-temporal" hint. Since we don't emulate
1790 * caches we don't care about hints to the cache system about
1791 * data access patterns, and handle this identically to plain
1792 * signed offset.
1793 */
1794 if (is_signed) {
1795 /* There is no non-temporal-hint version of LDPSW */
1796 unallocated_encoding(s);
1797 return;
1798 }
1799 postindex = false;
1800 break;
1801 case 2: /* signed offset, rn not updated */
1802 postindex = false;
1803 break;
1804 case 3: /* pre-index */
1805 postindex = false;
1806 wback = true;
1807 break;
1808 }
1809
1810 offset <<= size;
1811
1812 if (rn == 31) {
1813 gen_check_sp_alignment(s);
1814 }
1815
1816 tcg_addr = read_cpu_reg_sp(s, rn, 1);
1817
1818 if (!postindex) {
1819 tcg_gen_addi_i64(tcg_addr, tcg_addr, offset);
1820 }
1821
1822 if (is_vector) {
1823 if (is_load) {
1824 do_fp_ld(s, rt, tcg_addr, size);
1825 } else {
1826 do_fp_st(s, rt, tcg_addr, size);
1827 }
1828 } else {
1829 TCGv_i64 tcg_rt = cpu_reg(s, rt);
1830 if (is_load) {
1831 do_gpr_ld(s, tcg_rt, tcg_addr, size, is_signed, false);
1832 } else {
1833 do_gpr_st(s, tcg_rt, tcg_addr, size);
1834 }
1835 }
1836 tcg_gen_addi_i64(tcg_addr, tcg_addr, 1 << size);
1837 if (is_vector) {
1838 if (is_load) {
1839 do_fp_ld(s, rt2, tcg_addr, size);
1840 } else {
1841 do_fp_st(s, rt2, tcg_addr, size);
1842 }
1843 } else {
1844 TCGv_i64 tcg_rt2 = cpu_reg(s, rt2);
1845 if (is_load) {
1846 do_gpr_ld(s, tcg_rt2, tcg_addr, size, is_signed, false);
1847 } else {
1848 do_gpr_st(s, tcg_rt2, tcg_addr, size);
1849 }
1850 }
1851
1852 if (wback) {
1853 if (postindex) {
1854 tcg_gen_addi_i64(tcg_addr, tcg_addr, offset - (1 << size));
1855 } else {
1856 tcg_gen_subi_i64(tcg_addr, tcg_addr, 1 << size);
1857 }
1858 tcg_gen_mov_i64(cpu_reg_sp(s, rn), tcg_addr);
1859 }
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00001860}
1861
Alex Bennéed5612f12013-12-23 23:27:28 +00001862/*
Alex Bennéea5e94a92013-12-23 23:27:29 +00001863 * C3.3.8 Load/store (immediate post-indexed)
1864 * C3.3.9 Load/store (immediate pre-indexed)
1865 * C3.3.12 Load/store (unscaled immediate)
1866 *
1867 * 31 30 29 27 26 25 24 23 22 21 20 12 11 10 9 5 4 0
1868 * +----+-------+---+-----+-----+---+--------+-----+------+------+
1869 * |size| 1 1 1 | V | 0 0 | opc | 0 | imm9 | idx | Rn | Rt |
1870 * +----+-------+---+-----+-----+---+--------+-----+------+------+
1871 *
1872 * idx = 01 -> post-indexed, 11 pre-indexed, 00 unscaled imm. (no writeback)
Peter Maydell60510ae2014-02-20 10:35:56 +00001873 10 -> unprivileged
Alex Bennéea5e94a92013-12-23 23:27:29 +00001874 * V = 0 -> non-vector
1875 * size: 00 -> 8 bit, 01 -> 16 bit, 10 -> 32 bit, 11 -> 64bit
1876 * opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
1877 */
1878static void disas_ldst_reg_imm9(DisasContext *s, uint32_t insn)
1879{
1880 int rt = extract32(insn, 0, 5);
1881 int rn = extract32(insn, 5, 5);
1882 int imm9 = sextract32(insn, 12, 9);
1883 int opc = extract32(insn, 22, 2);
1884 int size = extract32(insn, 30, 2);
1885 int idx = extract32(insn, 10, 2);
1886 bool is_signed = false;
1887 bool is_store = false;
1888 bool is_extended = false;
Peter Maydell60510ae2014-02-20 10:35:56 +00001889 bool is_unpriv = (idx == 2);
Alex Bennéea5e94a92013-12-23 23:27:29 +00001890 bool is_vector = extract32(insn, 26, 1);
1891 bool post_index;
1892 bool writeback;
1893
1894 TCGv_i64 tcg_addr;
1895
1896 if (is_vector) {
1897 size |= (opc & 2) << 1;
Peter Maydell60510ae2014-02-20 10:35:56 +00001898 if (size > 4 || is_unpriv) {
Alex Bennéea5e94a92013-12-23 23:27:29 +00001899 unallocated_encoding(s);
1900 return;
1901 }
1902 is_store = ((opc & 1) == 0);
1903 } else {
1904 if (size == 3 && opc == 2) {
1905 /* PRFM - prefetch */
Peter Maydell60510ae2014-02-20 10:35:56 +00001906 if (is_unpriv) {
1907 unallocated_encoding(s);
1908 return;
1909 }
Alex Bennéea5e94a92013-12-23 23:27:29 +00001910 return;
1911 }
1912 if (opc == 3 && size > 1) {
1913 unallocated_encoding(s);
1914 return;
1915 }
1916 is_store = (opc == 0);
1917 is_signed = opc & (1<<1);
1918 is_extended = (size < 3) && (opc & 1);
1919 }
1920
1921 switch (idx) {
1922 case 0:
Peter Maydell60510ae2014-02-20 10:35:56 +00001923 case 2:
Alex Bennéea5e94a92013-12-23 23:27:29 +00001924 post_index = false;
1925 writeback = false;
1926 break;
1927 case 1:
1928 post_index = true;
1929 writeback = true;
1930 break;
1931 case 3:
1932 post_index = false;
1933 writeback = true;
1934 break;
Alex Bennéea5e94a92013-12-23 23:27:29 +00001935 }
1936
1937 if (rn == 31) {
1938 gen_check_sp_alignment(s);
1939 }
1940 tcg_addr = read_cpu_reg_sp(s, rn, 1);
1941
1942 if (!post_index) {
1943 tcg_gen_addi_i64(tcg_addr, tcg_addr, imm9);
1944 }
1945
1946 if (is_vector) {
1947 if (is_store) {
1948 do_fp_st(s, rt, tcg_addr, size);
1949 } else {
1950 do_fp_ld(s, rt, tcg_addr, size);
1951 }
1952 } else {
1953 TCGv_i64 tcg_rt = cpu_reg(s, rt);
Peter Maydell60510ae2014-02-20 10:35:56 +00001954 int memidx = is_unpriv ? 1 : get_mem_index(s);
1955
Alex Bennéea5e94a92013-12-23 23:27:29 +00001956 if (is_store) {
Peter Maydell60510ae2014-02-20 10:35:56 +00001957 do_gpr_st_memidx(s, tcg_rt, tcg_addr, size, memidx);
Alex Bennéea5e94a92013-12-23 23:27:29 +00001958 } else {
Peter Maydell60510ae2014-02-20 10:35:56 +00001959 do_gpr_ld_memidx(s, tcg_rt, tcg_addr, size,
1960 is_signed, is_extended, memidx);
Alex Bennéea5e94a92013-12-23 23:27:29 +00001961 }
1962 }
1963
1964 if (writeback) {
1965 TCGv_i64 tcg_rn = cpu_reg_sp(s, rn);
1966 if (post_index) {
1967 tcg_gen_addi_i64(tcg_addr, tcg_addr, imm9);
1968 }
1969 tcg_gen_mov_i64(tcg_rn, tcg_addr);
1970 }
1971}
1972
1973/*
Alex Bennée229b7a02013-12-23 23:27:29 +00001974 * C3.3.10 Load/store (register offset)
1975 *
1976 * 31 30 29 27 26 25 24 23 22 21 20 16 15 13 12 11 10 9 5 4 0
1977 * +----+-------+---+-----+-----+---+------+-----+--+-----+----+----+
1978 * |size| 1 1 1 | V | 0 0 | opc | 1 | Rm | opt | S| 1 0 | Rn | Rt |
1979 * +----+-------+---+-----+-----+---+------+-----+--+-----+----+----+
1980 *
1981 * For non-vector:
1982 * size: 00-> byte, 01 -> 16 bit, 10 -> 32bit, 11 -> 64bit
1983 * opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
1984 * For vector:
1985 * size is opc<1>:size<1:0> so 100 -> 128 bit; 110 and 111 unallocated
1986 * opc<0>: 0 -> store, 1 -> load
1987 * V: 1 -> vector/simd
1988 * opt: extend encoding (see DecodeRegExtend)
1989 * S: if S=1 then scale (essentially index by sizeof(size))
1990 * Rt: register to transfer into/out of
1991 * Rn: address register or SP for base
1992 * Rm: offset register or ZR for offset
1993 */
1994static void disas_ldst_reg_roffset(DisasContext *s, uint32_t insn)
1995{
1996 int rt = extract32(insn, 0, 5);
1997 int rn = extract32(insn, 5, 5);
1998 int shift = extract32(insn, 12, 1);
1999 int rm = extract32(insn, 16, 5);
2000 int opc = extract32(insn, 22, 2);
2001 int opt = extract32(insn, 13, 3);
2002 int size = extract32(insn, 30, 2);
2003 bool is_signed = false;
2004 bool is_store = false;
2005 bool is_extended = false;
2006 bool is_vector = extract32(insn, 26, 1);
2007
2008 TCGv_i64 tcg_rm;
2009 TCGv_i64 tcg_addr;
2010
2011 if (extract32(opt, 1, 1) == 0) {
2012 unallocated_encoding(s);
2013 return;
2014 }
2015
2016 if (is_vector) {
2017 size |= (opc & 2) << 1;
2018 if (size > 4) {
2019 unallocated_encoding(s);
2020 return;
2021 }
2022 is_store = !extract32(opc, 0, 1);
2023 } else {
2024 if (size == 3 && opc == 2) {
2025 /* PRFM - prefetch */
2026 return;
2027 }
2028 if (opc == 3 && size > 1) {
2029 unallocated_encoding(s);
2030 return;
2031 }
2032 is_store = (opc == 0);
2033 is_signed = extract32(opc, 1, 1);
2034 is_extended = (size < 3) && extract32(opc, 0, 1);
2035 }
2036
2037 if (rn == 31) {
2038 gen_check_sp_alignment(s);
2039 }
2040 tcg_addr = read_cpu_reg_sp(s, rn, 1);
2041
2042 tcg_rm = read_cpu_reg(s, rm, 1);
2043 ext_and_shift_reg(tcg_rm, tcg_rm, opt, shift ? size : 0);
2044
2045 tcg_gen_add_i64(tcg_addr, tcg_addr, tcg_rm);
2046
2047 if (is_vector) {
2048 if (is_store) {
2049 do_fp_st(s, rt, tcg_addr, size);
2050 } else {
2051 do_fp_ld(s, rt, tcg_addr, size);
2052 }
2053 } else {
2054 TCGv_i64 tcg_rt = cpu_reg(s, rt);
2055 if (is_store) {
2056 do_gpr_st(s, tcg_rt, tcg_addr, size);
2057 } else {
2058 do_gpr_ld(s, tcg_rt, tcg_addr, size, is_signed, is_extended);
2059 }
2060 }
2061}
2062
2063/*
Alex Bennéed5612f12013-12-23 23:27:28 +00002064 * C3.3.13 Load/store (unsigned immediate)
2065 *
2066 * 31 30 29 27 26 25 24 23 22 21 10 9 5
2067 * +----+-------+---+-----+-----+------------+-------+------+
2068 * |size| 1 1 1 | V | 0 1 | opc | imm12 | Rn | Rt |
2069 * +----+-------+---+-----+-----+------------+-------+------+
2070 *
2071 * For non-vector:
2072 * size: 00-> byte, 01 -> 16 bit, 10 -> 32bit, 11 -> 64bit
2073 * opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
2074 * For vector:
2075 * size is opc<1>:size<1:0> so 100 -> 128 bit; 110 and 111 unallocated
2076 * opc<0>: 0 -> store, 1 -> load
2077 * Rn: base address register (inc SP)
2078 * Rt: target register
2079 */
2080static void disas_ldst_reg_unsigned_imm(DisasContext *s, uint32_t insn)
2081{
2082 int rt = extract32(insn, 0, 5);
2083 int rn = extract32(insn, 5, 5);
2084 unsigned int imm12 = extract32(insn, 10, 12);
2085 bool is_vector = extract32(insn, 26, 1);
2086 int size = extract32(insn, 30, 2);
2087 int opc = extract32(insn, 22, 2);
2088 unsigned int offset;
2089
2090 TCGv_i64 tcg_addr;
2091
2092 bool is_store;
2093 bool is_signed = false;
2094 bool is_extended = false;
2095
2096 if (is_vector) {
2097 size |= (opc & 2) << 1;
2098 if (size > 4) {
2099 unallocated_encoding(s);
2100 return;
2101 }
2102 is_store = !extract32(opc, 0, 1);
2103 } else {
2104 if (size == 3 && opc == 2) {
2105 /* PRFM - prefetch */
2106 return;
2107 }
2108 if (opc == 3 && size > 1) {
2109 unallocated_encoding(s);
2110 return;
2111 }
2112 is_store = (opc == 0);
2113 is_signed = extract32(opc, 1, 1);
2114 is_extended = (size < 3) && extract32(opc, 0, 1);
2115 }
2116
2117 if (rn == 31) {
2118 gen_check_sp_alignment(s);
2119 }
2120 tcg_addr = read_cpu_reg_sp(s, rn, 1);
2121 offset = imm12 << size;
2122 tcg_gen_addi_i64(tcg_addr, tcg_addr, offset);
2123
2124 if (is_vector) {
2125 if (is_store) {
2126 do_fp_st(s, rt, tcg_addr, size);
2127 } else {
2128 do_fp_ld(s, rt, tcg_addr, size);
2129 }
2130 } else {
2131 TCGv_i64 tcg_rt = cpu_reg(s, rt);
2132 if (is_store) {
2133 do_gpr_st(s, tcg_rt, tcg_addr, size);
2134 } else {
2135 do_gpr_ld(s, tcg_rt, tcg_addr, size, is_signed, is_extended);
2136 }
2137 }
2138}
2139
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00002140/* Load/store register (all forms) */
2141static void disas_ldst_reg(DisasContext *s, uint32_t insn)
2142{
Alex Bennéed5612f12013-12-23 23:27:28 +00002143 switch (extract32(insn, 24, 2)) {
2144 case 0:
Alex Bennée229b7a02013-12-23 23:27:29 +00002145 if (extract32(insn, 21, 1) == 1 && extract32(insn, 10, 2) == 2) {
2146 disas_ldst_reg_roffset(s, insn);
2147 } else {
Peter Maydell60510ae2014-02-20 10:35:56 +00002148 /* Load/store register (unscaled immediate)
2149 * Load/store immediate pre/post-indexed
2150 * Load/store register unprivileged
2151 */
2152 disas_ldst_reg_imm9(s, insn);
Alex Bennée229b7a02013-12-23 23:27:29 +00002153 }
Alex Bennéed5612f12013-12-23 23:27:28 +00002154 break;
2155 case 1:
2156 disas_ldst_reg_unsigned_imm(s, insn);
2157 break;
2158 default:
2159 unallocated_encoding(s);
2160 break;
2161 }
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00002162}
2163
Alex Bennée72430bf2014-01-31 14:47:30 +00002164/* C3.3.1 AdvSIMD load/store multiple structures
2165 *
2166 * 31 30 29 23 22 21 16 15 12 11 10 9 5 4 0
2167 * +---+---+---------------+---+-------------+--------+------+------+------+
2168 * | 0 | Q | 0 0 1 1 0 0 0 | L | 0 0 0 0 0 0 | opcode | size | Rn | Rt |
2169 * +---+---+---------------+---+-------------+--------+------+------+------+
2170 *
2171 * C3.3.2 AdvSIMD load/store multiple structures (post-indexed)
2172 *
2173 * 31 30 29 23 22 21 20 16 15 12 11 10 9 5 4 0
2174 * +---+---+---------------+---+---+---------+--------+------+------+------+
2175 * | 0 | Q | 0 0 1 1 0 0 1 | L | 0 | Rm | opcode | size | Rn | Rt |
2176 * +---+---+---------------+---+---+---------+--------+------+------+------+
2177 *
2178 * Rt: first (or only) SIMD&FP register to be transferred
2179 * Rn: base address or SP
2180 * Rm (post-index only): post-index register (when !31) or size dependent #imm
2181 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00002182static void disas_ldst_multiple_struct(DisasContext *s, uint32_t insn)
2183{
Alex Bennée72430bf2014-01-31 14:47:30 +00002184 int rt = extract32(insn, 0, 5);
2185 int rn = extract32(insn, 5, 5);
2186 int size = extract32(insn, 10, 2);
2187 int opcode = extract32(insn, 12, 4);
2188 bool is_store = !extract32(insn, 22, 1);
2189 bool is_postidx = extract32(insn, 23, 1);
2190 bool is_q = extract32(insn, 30, 1);
2191 TCGv_i64 tcg_addr, tcg_rn;
2192
2193 int ebytes = 1 << size;
2194 int elements = (is_q ? 128 : 64) / (8 << size);
2195 int rpt; /* num iterations */
2196 int selem; /* structure elements */
2197 int r;
2198
2199 if (extract32(insn, 31, 1) || extract32(insn, 21, 1)) {
2200 unallocated_encoding(s);
2201 return;
2202 }
2203
2204 /* From the shared decode logic */
2205 switch (opcode) {
2206 case 0x0:
2207 rpt = 1;
2208 selem = 4;
2209 break;
2210 case 0x2:
2211 rpt = 4;
2212 selem = 1;
2213 break;
2214 case 0x4:
2215 rpt = 1;
2216 selem = 3;
2217 break;
2218 case 0x6:
2219 rpt = 3;
2220 selem = 1;
2221 break;
2222 case 0x7:
2223 rpt = 1;
2224 selem = 1;
2225 break;
2226 case 0x8:
2227 rpt = 1;
2228 selem = 2;
2229 break;
2230 case 0xa:
2231 rpt = 2;
2232 selem = 1;
2233 break;
2234 default:
2235 unallocated_encoding(s);
2236 return;
2237 }
2238
2239 if (size == 3 && !is_q && selem != 1) {
2240 /* reserved */
2241 unallocated_encoding(s);
2242 return;
2243 }
2244
2245 if (rn == 31) {
2246 gen_check_sp_alignment(s);
2247 }
2248
2249 tcg_rn = cpu_reg_sp(s, rn);
2250 tcg_addr = tcg_temp_new_i64();
2251 tcg_gen_mov_i64(tcg_addr, tcg_rn);
2252
2253 for (r = 0; r < rpt; r++) {
2254 int e;
2255 for (e = 0; e < elements; e++) {
2256 int tt = (rt + r) % 32;
2257 int xs;
2258 for (xs = 0; xs < selem; xs++) {
2259 if (is_store) {
2260 do_vec_st(s, tt, e, tcg_addr, size);
2261 } else {
2262 do_vec_ld(s, tt, e, tcg_addr, size);
2263
2264 /* For non-quad operations, setting a slice of the low
2265 * 64 bits of the register clears the high 64 bits (in
2266 * the ARM ARM pseudocode this is implicit in the fact
2267 * that 'rval' is a 64 bit wide variable). We optimize
2268 * by noticing that we only need to do this the first
2269 * time we touch a register.
2270 */
2271 if (!is_q && e == 0 && (r == 0 || xs == selem - 1)) {
2272 clear_vec_high(s, tt);
2273 }
2274 }
2275 tcg_gen_addi_i64(tcg_addr, tcg_addr, ebytes);
2276 tt = (tt + 1) % 32;
2277 }
2278 }
2279 }
2280
2281 if (is_postidx) {
2282 int rm = extract32(insn, 16, 5);
2283 if (rm == 31) {
2284 tcg_gen_mov_i64(tcg_rn, tcg_addr);
2285 } else {
2286 tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, rm));
2287 }
2288 }
2289 tcg_temp_free_i64(tcg_addr);
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00002290}
2291
Peter Maydelldf54e472014-01-31 14:47:30 +00002292/* C3.3.3 AdvSIMD load/store single structure
2293 *
2294 * 31 30 29 23 22 21 20 16 15 13 12 11 10 9 5 4 0
2295 * +---+---+---------------+-----+-----------+-----+---+------+------+------+
2296 * | 0 | Q | 0 0 1 1 0 1 0 | L R | 0 0 0 0 0 | opc | S | size | Rn | Rt |
2297 * +---+---+---------------+-----+-----------+-----+---+------+------+------+
2298 *
2299 * C3.3.4 AdvSIMD load/store single structure (post-indexed)
2300 *
2301 * 31 30 29 23 22 21 20 16 15 13 12 11 10 9 5 4 0
2302 * +---+---+---------------+-----+-----------+-----+---+------+------+------+
2303 * | 0 | Q | 0 0 1 1 0 1 1 | L R | Rm | opc | S | size | Rn | Rt |
2304 * +---+---+---------------+-----+-----------+-----+---+------+------+------+
2305 *
2306 * Rt: first (or only) SIMD&FP register to be transferred
2307 * Rn: base address or SP
2308 * Rm (post-index only): post-index register (when !31) or size dependent #imm
2309 * index = encoded in Q:S:size dependent on size
2310 *
2311 * lane_size = encoded in R, opc
2312 * transfer width = encoded in opc, S, size
2313 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00002314static void disas_ldst_single_struct(DisasContext *s, uint32_t insn)
2315{
Peter Maydelldf54e472014-01-31 14:47:30 +00002316 int rt = extract32(insn, 0, 5);
2317 int rn = extract32(insn, 5, 5);
2318 int size = extract32(insn, 10, 2);
2319 int S = extract32(insn, 12, 1);
2320 int opc = extract32(insn, 13, 3);
2321 int R = extract32(insn, 21, 1);
2322 int is_load = extract32(insn, 22, 1);
2323 int is_postidx = extract32(insn, 23, 1);
2324 int is_q = extract32(insn, 30, 1);
2325
2326 int scale = extract32(opc, 1, 2);
2327 int selem = (extract32(opc, 0, 1) << 1 | R) + 1;
2328 bool replicate = false;
2329 int index = is_q << 3 | S << 2 | size;
2330 int ebytes, xs;
2331 TCGv_i64 tcg_addr, tcg_rn;
2332
2333 switch (scale) {
2334 case 3:
2335 if (!is_load || S) {
2336 unallocated_encoding(s);
2337 return;
2338 }
2339 scale = size;
2340 replicate = true;
2341 break;
2342 case 0:
2343 break;
2344 case 1:
2345 if (extract32(size, 0, 1)) {
2346 unallocated_encoding(s);
2347 return;
2348 }
2349 index >>= 1;
2350 break;
2351 case 2:
2352 if (extract32(size, 1, 1)) {
2353 unallocated_encoding(s);
2354 return;
2355 }
2356 if (!extract32(size, 0, 1)) {
2357 index >>= 2;
2358 } else {
2359 if (S) {
2360 unallocated_encoding(s);
2361 return;
2362 }
2363 index >>= 3;
2364 scale = 3;
2365 }
2366 break;
2367 default:
2368 g_assert_not_reached();
2369 }
2370
2371 ebytes = 1 << scale;
2372
2373 if (rn == 31) {
2374 gen_check_sp_alignment(s);
2375 }
2376
2377 tcg_rn = cpu_reg_sp(s, rn);
2378 tcg_addr = tcg_temp_new_i64();
2379 tcg_gen_mov_i64(tcg_addr, tcg_rn);
2380
2381 for (xs = 0; xs < selem; xs++) {
2382 if (replicate) {
2383 /* Load and replicate to all elements */
2384 uint64_t mulconst;
2385 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
2386
2387 tcg_gen_qemu_ld_i64(tcg_tmp, tcg_addr,
2388 get_mem_index(s), MO_TE + scale);
2389 switch (scale) {
2390 case 0:
2391 mulconst = 0x0101010101010101ULL;
2392 break;
2393 case 1:
2394 mulconst = 0x0001000100010001ULL;
2395 break;
2396 case 2:
2397 mulconst = 0x0000000100000001ULL;
2398 break;
2399 case 3:
2400 mulconst = 0;
2401 break;
2402 default:
2403 g_assert_not_reached();
2404 }
2405 if (mulconst) {
2406 tcg_gen_muli_i64(tcg_tmp, tcg_tmp, mulconst);
2407 }
2408 write_vec_element(s, tcg_tmp, rt, 0, MO_64);
2409 if (is_q) {
2410 write_vec_element(s, tcg_tmp, rt, 1, MO_64);
2411 } else {
2412 clear_vec_high(s, rt);
2413 }
2414 tcg_temp_free_i64(tcg_tmp);
2415 } else {
2416 /* Load/store one element per register */
2417 if (is_load) {
2418 do_vec_ld(s, rt, index, tcg_addr, MO_TE + scale);
2419 } else {
2420 do_vec_st(s, rt, index, tcg_addr, MO_TE + scale);
2421 }
2422 }
2423 tcg_gen_addi_i64(tcg_addr, tcg_addr, ebytes);
2424 rt = (rt + 1) % 32;
2425 }
2426
2427 if (is_postidx) {
2428 int rm = extract32(insn, 16, 5);
2429 if (rm == 31) {
2430 tcg_gen_mov_i64(tcg_rn, tcg_addr);
2431 } else {
2432 tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, rm));
2433 }
2434 }
2435 tcg_temp_free_i64(tcg_addr);
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00002436}
2437
2438/* C3.3 Loads and stores */
2439static void disas_ldst(DisasContext *s, uint32_t insn)
2440{
2441 switch (extract32(insn, 24, 6)) {
2442 case 0x08: /* Load/store exclusive */
2443 disas_ldst_excl(s, insn);
2444 break;
2445 case 0x18: case 0x1c: /* Load register (literal) */
2446 disas_ld_lit(s, insn);
2447 break;
2448 case 0x28: case 0x29:
2449 case 0x2c: case 0x2d: /* Load/store pair (all forms) */
2450 disas_ldst_pair(s, insn);
2451 break;
2452 case 0x38: case 0x39:
2453 case 0x3c: case 0x3d: /* Load/store register (all forms) */
2454 disas_ldst_reg(s, insn);
2455 break;
2456 case 0x0c: /* AdvSIMD load/store multiple structures */
2457 disas_ldst_multiple_struct(s, insn);
2458 break;
2459 case 0x0d: /* AdvSIMD load/store single structure */
2460 disas_ldst_single_struct(s, insn);
2461 break;
2462 default:
2463 unallocated_encoding(s);
2464 break;
2465 }
2466}
2467
Alexander Graf15bfe8b2013-12-17 19:42:34 +00002468/* C3.4.6 PC-rel. addressing
2469 * 31 30 29 28 24 23 5 4 0
2470 * +----+-------+-----------+-------------------+------+
2471 * | op | immlo | 1 0 0 0 0 | immhi | Rd |
2472 * +----+-------+-----------+-------------------+------+
2473 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00002474static void disas_pc_rel_adr(DisasContext *s, uint32_t insn)
2475{
Alexander Graf15bfe8b2013-12-17 19:42:34 +00002476 unsigned int page, rd;
2477 uint64_t base;
2478 int64_t offset;
2479
2480 page = extract32(insn, 31, 1);
2481 /* SignExtend(immhi:immlo) -> offset */
2482 offset = ((int64_t)sextract32(insn, 5, 19) << 2) | extract32(insn, 29, 2);
2483 rd = extract32(insn, 0, 5);
2484 base = s->pc - 4;
2485
2486 if (page) {
2487 /* ADRP (page based) */
2488 base &= ~0xfff;
2489 offset <<= 12;
2490 }
2491
2492 tcg_gen_movi_i64(cpu_reg(s, rd), base + offset);
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00002493}
2494
Alex Bennéeb0ff21b2013-12-23 23:27:29 +00002495/*
2496 * C3.4.1 Add/subtract (immediate)
2497 *
2498 * 31 30 29 28 24 23 22 21 10 9 5 4 0
2499 * +--+--+--+-----------+-----+-------------+-----+-----+
2500 * |sf|op| S| 1 0 0 0 1 |shift| imm12 | Rn | Rd |
2501 * +--+--+--+-----------+-----+-------------+-----+-----+
2502 *
2503 * sf: 0 -> 32bit, 1 -> 64bit
2504 * op: 0 -> add , 1 -> sub
2505 * S: 1 -> set flags
2506 * shift: 00 -> LSL imm by 0, 01 -> LSL imm by 12
2507 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00002508static void disas_add_sub_imm(DisasContext *s, uint32_t insn)
2509{
Alex Bennéeb0ff21b2013-12-23 23:27:29 +00002510 int rd = extract32(insn, 0, 5);
2511 int rn = extract32(insn, 5, 5);
2512 uint64_t imm = extract32(insn, 10, 12);
2513 int shift = extract32(insn, 22, 2);
2514 bool setflags = extract32(insn, 29, 1);
2515 bool sub_op = extract32(insn, 30, 1);
2516 bool is_64bit = extract32(insn, 31, 1);
2517
2518 TCGv_i64 tcg_rn = cpu_reg_sp(s, rn);
2519 TCGv_i64 tcg_rd = setflags ? cpu_reg(s, rd) : cpu_reg_sp(s, rd);
2520 TCGv_i64 tcg_result;
2521
2522 switch (shift) {
2523 case 0x0:
2524 break;
2525 case 0x1:
2526 imm <<= 12;
2527 break;
2528 default:
2529 unallocated_encoding(s);
2530 return;
2531 }
2532
2533 tcg_result = tcg_temp_new_i64();
2534 if (!setflags) {
2535 if (sub_op) {
2536 tcg_gen_subi_i64(tcg_result, tcg_rn, imm);
2537 } else {
2538 tcg_gen_addi_i64(tcg_result, tcg_rn, imm);
2539 }
2540 } else {
2541 TCGv_i64 tcg_imm = tcg_const_i64(imm);
2542 if (sub_op) {
2543 gen_sub_CC(is_64bit, tcg_result, tcg_rn, tcg_imm);
2544 } else {
2545 gen_add_CC(is_64bit, tcg_result, tcg_rn, tcg_imm);
2546 }
2547 tcg_temp_free_i64(tcg_imm);
2548 }
2549
2550 if (is_64bit) {
2551 tcg_gen_mov_i64(tcg_rd, tcg_result);
2552 } else {
2553 tcg_gen_ext32u_i64(tcg_rd, tcg_result);
2554 }
2555
2556 tcg_temp_free_i64(tcg_result);
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00002557}
2558
Alexander Graf71b46082013-12-17 19:42:36 +00002559/* The input should be a value in the bottom e bits (with higher
2560 * bits zero); returns that value replicated into every element
2561 * of size e in a 64 bit integer.
2562 */
2563static uint64_t bitfield_replicate(uint64_t mask, unsigned int e)
2564{
2565 assert(e != 0);
2566 while (e < 64) {
2567 mask |= mask << e;
2568 e *= 2;
2569 }
2570 return mask;
2571}
2572
2573/* Return a value with the bottom len bits set (where 0 < len <= 64) */
2574static inline uint64_t bitmask64(unsigned int length)
2575{
2576 assert(length > 0 && length <= 64);
2577 return ~0ULL >> (64 - length);
2578}
2579
2580/* Simplified variant of pseudocode DecodeBitMasks() for the case where we
2581 * only require the wmask. Returns false if the imms/immr/immn are a reserved
2582 * value (ie should cause a guest UNDEF exception), and true if they are
2583 * valid, in which case the decoded bit pattern is written to result.
2584 */
2585static bool logic_imm_decode_wmask(uint64_t *result, unsigned int immn,
2586 unsigned int imms, unsigned int immr)
2587{
2588 uint64_t mask;
2589 unsigned e, levels, s, r;
2590 int len;
2591
2592 assert(immn < 2 && imms < 64 && immr < 64);
2593
2594 /* The bit patterns we create here are 64 bit patterns which
2595 * are vectors of identical elements of size e = 2, 4, 8, 16, 32 or
2596 * 64 bits each. Each element contains the same value: a run
2597 * of between 1 and e-1 non-zero bits, rotated within the
2598 * element by between 0 and e-1 bits.
2599 *
2600 * The element size and run length are encoded into immn (1 bit)
2601 * and imms (6 bits) as follows:
2602 * 64 bit elements: immn = 1, imms = <length of run - 1>
2603 * 32 bit elements: immn = 0, imms = 0 : <length of run - 1>
2604 * 16 bit elements: immn = 0, imms = 10 : <length of run - 1>
2605 * 8 bit elements: immn = 0, imms = 110 : <length of run - 1>
2606 * 4 bit elements: immn = 0, imms = 1110 : <length of run - 1>
2607 * 2 bit elements: immn = 0, imms = 11110 : <length of run - 1>
2608 * Notice that immn = 0, imms = 11111x is the only combination
2609 * not covered by one of the above options; this is reserved.
2610 * Further, <length of run - 1> all-ones is a reserved pattern.
2611 *
2612 * In all cases the rotation is by immr % e (and immr is 6 bits).
2613 */
2614
2615 /* First determine the element size */
2616 len = 31 - clz32((immn << 6) | (~imms & 0x3f));
2617 if (len < 1) {
2618 /* This is the immn == 0, imms == 0x11111x case */
2619 return false;
2620 }
2621 e = 1 << len;
2622
2623 levels = e - 1;
2624 s = imms & levels;
2625 r = immr & levels;
2626
2627 if (s == levels) {
2628 /* <length of run - 1> mustn't be all-ones. */
2629 return false;
2630 }
2631
2632 /* Create the value of one element: s+1 set bits rotated
2633 * by r within the element (which is e bits wide)...
2634 */
2635 mask = bitmask64(s + 1);
2636 mask = (mask >> r) | (mask << (e - r));
2637 /* ...then replicate the element over the whole 64 bit value */
2638 mask = bitfield_replicate(mask, e);
2639 *result = mask;
2640 return true;
2641}
2642
2643/* C3.4.4 Logical (immediate)
2644 * 31 30 29 28 23 22 21 16 15 10 9 5 4 0
2645 * +----+-----+-------------+---+------+------+------+------+
2646 * | sf | opc | 1 0 0 1 0 0 | N | immr | imms | Rn | Rd |
2647 * +----+-----+-------------+---+------+------+------+------+
2648 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00002649static void disas_logic_imm(DisasContext *s, uint32_t insn)
2650{
Alexander Graf71b46082013-12-17 19:42:36 +00002651 unsigned int sf, opc, is_n, immr, imms, rn, rd;
2652 TCGv_i64 tcg_rd, tcg_rn;
2653 uint64_t wmask;
2654 bool is_and = false;
2655
2656 sf = extract32(insn, 31, 1);
2657 opc = extract32(insn, 29, 2);
2658 is_n = extract32(insn, 22, 1);
2659 immr = extract32(insn, 16, 6);
2660 imms = extract32(insn, 10, 6);
2661 rn = extract32(insn, 5, 5);
2662 rd = extract32(insn, 0, 5);
2663
2664 if (!sf && is_n) {
2665 unallocated_encoding(s);
2666 return;
2667 }
2668
2669 if (opc == 0x3) { /* ANDS */
2670 tcg_rd = cpu_reg(s, rd);
2671 } else {
2672 tcg_rd = cpu_reg_sp(s, rd);
2673 }
2674 tcg_rn = cpu_reg(s, rn);
2675
2676 if (!logic_imm_decode_wmask(&wmask, is_n, imms, immr)) {
2677 /* some immediate field values are reserved */
2678 unallocated_encoding(s);
2679 return;
2680 }
2681
2682 if (!sf) {
2683 wmask &= 0xffffffff;
2684 }
2685
2686 switch (opc) {
2687 case 0x3: /* ANDS */
2688 case 0x0: /* AND */
2689 tcg_gen_andi_i64(tcg_rd, tcg_rn, wmask);
2690 is_and = true;
2691 break;
2692 case 0x1: /* ORR */
2693 tcg_gen_ori_i64(tcg_rd, tcg_rn, wmask);
2694 break;
2695 case 0x2: /* EOR */
2696 tcg_gen_xori_i64(tcg_rd, tcg_rn, wmask);
2697 break;
2698 default:
2699 assert(FALSE); /* must handle all above */
2700 break;
2701 }
2702
2703 if (!sf && !is_and) {
2704 /* zero extend final result; we know we can skip this for AND
2705 * since the immediate had the high 32 bits clear.
2706 */
2707 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
2708 }
2709
2710 if (opc == 3) { /* ANDS */
2711 gen_logic_CC(sf, tcg_rd);
2712 }
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00002713}
2714
Alex Bennéeed6ec672013-12-23 23:27:29 +00002715/*
2716 * C3.4.5 Move wide (immediate)
2717 *
2718 * 31 30 29 28 23 22 21 20 5 4 0
2719 * +--+-----+-------------+-----+----------------+------+
2720 * |sf| opc | 1 0 0 1 0 1 | hw | imm16 | Rd |
2721 * +--+-----+-------------+-----+----------------+------+
2722 *
2723 * sf: 0 -> 32 bit, 1 -> 64 bit
2724 * opc: 00 -> N, 10 -> Z, 11 -> K
2725 * hw: shift/16 (0,16, and sf only 32, 48)
2726 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00002727static void disas_movw_imm(DisasContext *s, uint32_t insn)
2728{
Alex Bennéeed6ec672013-12-23 23:27:29 +00002729 int rd = extract32(insn, 0, 5);
2730 uint64_t imm = extract32(insn, 5, 16);
2731 int sf = extract32(insn, 31, 1);
2732 int opc = extract32(insn, 29, 2);
2733 int pos = extract32(insn, 21, 2) << 4;
2734 TCGv_i64 tcg_rd = cpu_reg(s, rd);
2735 TCGv_i64 tcg_imm;
2736
2737 if (!sf && (pos >= 32)) {
2738 unallocated_encoding(s);
2739 return;
2740 }
2741
2742 switch (opc) {
2743 case 0: /* MOVN */
2744 case 2: /* MOVZ */
2745 imm <<= pos;
2746 if (opc == 0) {
2747 imm = ~imm;
2748 }
2749 if (!sf) {
2750 imm &= 0xffffffffu;
2751 }
2752 tcg_gen_movi_i64(tcg_rd, imm);
2753 break;
2754 case 3: /* MOVK */
2755 tcg_imm = tcg_const_i64(imm);
2756 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_imm, pos, 16);
2757 tcg_temp_free_i64(tcg_imm);
2758 if (!sf) {
2759 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
2760 }
2761 break;
2762 default:
2763 unallocated_encoding(s);
2764 break;
2765 }
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00002766}
2767
Claudio Fontana88077742013-12-17 19:42:35 +00002768/* C3.4.2 Bitfield
2769 * 31 30 29 28 23 22 21 16 15 10 9 5 4 0
2770 * +----+-----+-------------+---+------+------+------+------+
2771 * | sf | opc | 1 0 0 1 1 0 | N | immr | imms | Rn | Rd |
2772 * +----+-----+-------------+---+------+------+------+------+
2773 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00002774static void disas_bitfield(DisasContext *s, uint32_t insn)
2775{
Claudio Fontana88077742013-12-17 19:42:35 +00002776 unsigned int sf, n, opc, ri, si, rn, rd, bitsize, pos, len;
2777 TCGv_i64 tcg_rd, tcg_tmp;
2778
2779 sf = extract32(insn, 31, 1);
2780 opc = extract32(insn, 29, 2);
2781 n = extract32(insn, 22, 1);
2782 ri = extract32(insn, 16, 6);
2783 si = extract32(insn, 10, 6);
2784 rn = extract32(insn, 5, 5);
2785 rd = extract32(insn, 0, 5);
2786 bitsize = sf ? 64 : 32;
2787
2788 if (sf != n || ri >= bitsize || si >= bitsize || opc > 2) {
2789 unallocated_encoding(s);
2790 return;
2791 }
2792
2793 tcg_rd = cpu_reg(s, rd);
2794 tcg_tmp = read_cpu_reg(s, rn, sf);
2795
2796 /* OPTME: probably worth recognizing common cases of ext{8,16,32}{u,s} */
2797
2798 if (opc != 1) { /* SBFM or UBFM */
2799 tcg_gen_movi_i64(tcg_rd, 0);
2800 }
2801
2802 /* do the bit move operation */
2803 if (si >= ri) {
2804 /* Wd<s-r:0> = Wn<s:r> */
2805 tcg_gen_shri_i64(tcg_tmp, tcg_tmp, ri);
2806 pos = 0;
2807 len = (si - ri) + 1;
2808 } else {
2809 /* Wd<32+s-r,32-r> = Wn<s:0> */
2810 pos = bitsize - ri;
2811 len = si + 1;
2812 }
2813
2814 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, pos, len);
2815
2816 if (opc == 0) { /* SBFM - sign extend the destination field */
2817 tcg_gen_shli_i64(tcg_rd, tcg_rd, 64 - (pos + len));
2818 tcg_gen_sari_i64(tcg_rd, tcg_rd, 64 - (pos + len));
2819 }
2820
2821 if (!sf) { /* zero extend final result */
2822 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
2823 }
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00002824}
2825
Alexander Grafe801de92013-12-17 19:42:34 +00002826/* C3.4.3 Extract
2827 * 31 30 29 28 23 22 21 20 16 15 10 9 5 4 0
2828 * +----+------+-------------+---+----+------+--------+------+------+
2829 * | sf | op21 | 1 0 0 1 1 1 | N | o0 | Rm | imms | Rn | Rd |
2830 * +----+------+-------------+---+----+------+--------+------+------+
2831 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00002832static void disas_extract(DisasContext *s, uint32_t insn)
2833{
Alexander Grafe801de92013-12-17 19:42:34 +00002834 unsigned int sf, n, rm, imm, rn, rd, bitsize, op21, op0;
2835
2836 sf = extract32(insn, 31, 1);
2837 n = extract32(insn, 22, 1);
2838 rm = extract32(insn, 16, 5);
2839 imm = extract32(insn, 10, 6);
2840 rn = extract32(insn, 5, 5);
2841 rd = extract32(insn, 0, 5);
2842 op21 = extract32(insn, 29, 2);
2843 op0 = extract32(insn, 21, 1);
2844 bitsize = sf ? 64 : 32;
2845
2846 if (sf != n || op21 || op0 || imm >= bitsize) {
2847 unallocated_encoding(s);
2848 } else {
2849 TCGv_i64 tcg_rd, tcg_rm, tcg_rn;
2850
2851 tcg_rd = cpu_reg(s, rd);
2852
2853 if (imm) {
2854 /* OPTME: we can special case rm==rn as a rotate */
2855 tcg_rm = read_cpu_reg(s, rm, sf);
2856 tcg_rn = read_cpu_reg(s, rn, sf);
2857 tcg_gen_shri_i64(tcg_rm, tcg_rm, imm);
2858 tcg_gen_shli_i64(tcg_rn, tcg_rn, bitsize - imm);
2859 tcg_gen_or_i64(tcg_rd, tcg_rm, tcg_rn);
2860 if (!sf) {
2861 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
2862 }
2863 } else {
2864 /* tcg shl_i32/shl_i64 is undefined for 32/64 bit shifts,
2865 * so an extract from bit 0 is a special case.
2866 */
2867 if (sf) {
2868 tcg_gen_mov_i64(tcg_rd, cpu_reg(s, rm));
2869 } else {
2870 tcg_gen_ext32u_i64(tcg_rd, cpu_reg(s, rm));
2871 }
2872 }
2873
2874 }
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00002875}
2876
2877/* C3.4 Data processing - immediate */
2878static void disas_data_proc_imm(DisasContext *s, uint32_t insn)
2879{
2880 switch (extract32(insn, 23, 6)) {
2881 case 0x20: case 0x21: /* PC-rel. addressing */
2882 disas_pc_rel_adr(s, insn);
2883 break;
2884 case 0x22: case 0x23: /* Add/subtract (immediate) */
2885 disas_add_sub_imm(s, insn);
2886 break;
2887 case 0x24: /* Logical (immediate) */
2888 disas_logic_imm(s, insn);
2889 break;
2890 case 0x25: /* Move wide (immediate) */
2891 disas_movw_imm(s, insn);
2892 break;
2893 case 0x26: /* Bitfield */
2894 disas_bitfield(s, insn);
2895 break;
2896 case 0x27: /* Extract */
2897 disas_extract(s, insn);
2898 break;
2899 default:
2900 unallocated_encoding(s);
2901 break;
2902 }
2903}
2904
Alexander Graf832ffa12013-12-17 19:42:34 +00002905/* Shift a TCGv src by TCGv shift_amount, put result in dst.
2906 * Note that it is the caller's responsibility to ensure that the
2907 * shift amount is in range (ie 0..31 or 0..63) and provide the ARM
2908 * mandated semantics for out of range shifts.
2909 */
2910static void shift_reg(TCGv_i64 dst, TCGv_i64 src, int sf,
2911 enum a64_shift_type shift_type, TCGv_i64 shift_amount)
2912{
2913 switch (shift_type) {
2914 case A64_SHIFT_TYPE_LSL:
2915 tcg_gen_shl_i64(dst, src, shift_amount);
2916 break;
2917 case A64_SHIFT_TYPE_LSR:
2918 tcg_gen_shr_i64(dst, src, shift_amount);
2919 break;
2920 case A64_SHIFT_TYPE_ASR:
2921 if (!sf) {
2922 tcg_gen_ext32s_i64(dst, src);
2923 }
2924 tcg_gen_sar_i64(dst, sf ? src : dst, shift_amount);
2925 break;
2926 case A64_SHIFT_TYPE_ROR:
2927 if (sf) {
2928 tcg_gen_rotr_i64(dst, src, shift_amount);
2929 } else {
2930 TCGv_i32 t0, t1;
2931 t0 = tcg_temp_new_i32();
2932 t1 = tcg_temp_new_i32();
2933 tcg_gen_trunc_i64_i32(t0, src);
2934 tcg_gen_trunc_i64_i32(t1, shift_amount);
2935 tcg_gen_rotr_i32(t0, t0, t1);
2936 tcg_gen_extu_i32_i64(dst, t0);
2937 tcg_temp_free_i32(t0);
2938 tcg_temp_free_i32(t1);
2939 }
2940 break;
2941 default:
2942 assert(FALSE); /* all shift types should be handled */
2943 break;
2944 }
2945
2946 if (!sf) { /* zero extend final result */
2947 tcg_gen_ext32u_i64(dst, dst);
2948 }
2949}
2950
2951/* Shift a TCGv src by immediate, put result in dst.
2952 * The shift amount must be in range (this should always be true as the
2953 * relevant instructions will UNDEF on bad shift immediates).
2954 */
2955static void shift_reg_imm(TCGv_i64 dst, TCGv_i64 src, int sf,
2956 enum a64_shift_type shift_type, unsigned int shift_i)
2957{
2958 assert(shift_i < (sf ? 64 : 32));
2959
2960 if (shift_i == 0) {
2961 tcg_gen_mov_i64(dst, src);
2962 } else {
2963 TCGv_i64 shift_const;
2964
2965 shift_const = tcg_const_i64(shift_i);
2966 shift_reg(dst, src, sf, shift_type, shift_const);
2967 tcg_temp_free_i64(shift_const);
2968 }
2969}
2970
2971/* C3.5.10 Logical (shifted register)
2972 * 31 30 29 28 24 23 22 21 20 16 15 10 9 5 4 0
2973 * +----+-----+-----------+-------+---+------+--------+------+------+
2974 * | sf | opc | 0 1 0 1 0 | shift | N | Rm | imm6 | Rn | Rd |
2975 * +----+-----+-----------+-------+---+------+--------+------+------+
2976 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00002977static void disas_logic_reg(DisasContext *s, uint32_t insn)
2978{
Alexander Graf832ffa12013-12-17 19:42:34 +00002979 TCGv_i64 tcg_rd, tcg_rn, tcg_rm;
2980 unsigned int sf, opc, shift_type, invert, rm, shift_amount, rn, rd;
2981
2982 sf = extract32(insn, 31, 1);
2983 opc = extract32(insn, 29, 2);
2984 shift_type = extract32(insn, 22, 2);
2985 invert = extract32(insn, 21, 1);
2986 rm = extract32(insn, 16, 5);
2987 shift_amount = extract32(insn, 10, 6);
2988 rn = extract32(insn, 5, 5);
2989 rd = extract32(insn, 0, 5);
2990
2991 if (!sf && (shift_amount & (1 << 5))) {
2992 unallocated_encoding(s);
2993 return;
2994 }
2995
2996 tcg_rd = cpu_reg(s, rd);
2997
2998 if (opc == 1 && shift_amount == 0 && shift_type == 0 && rn == 31) {
2999 /* Unshifted ORR and ORN with WZR/XZR is the standard encoding for
3000 * register-register MOV and MVN, so it is worth special casing.
3001 */
3002 tcg_rm = cpu_reg(s, rm);
3003 if (invert) {
3004 tcg_gen_not_i64(tcg_rd, tcg_rm);
3005 if (!sf) {
3006 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3007 }
3008 } else {
3009 if (sf) {
3010 tcg_gen_mov_i64(tcg_rd, tcg_rm);
3011 } else {
3012 tcg_gen_ext32u_i64(tcg_rd, tcg_rm);
3013 }
3014 }
3015 return;
3016 }
3017
3018 tcg_rm = read_cpu_reg(s, rm, sf);
3019
3020 if (shift_amount) {
3021 shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, shift_amount);
3022 }
3023
3024 tcg_rn = cpu_reg(s, rn);
3025
3026 switch (opc | (invert << 2)) {
3027 case 0: /* AND */
3028 case 3: /* ANDS */
3029 tcg_gen_and_i64(tcg_rd, tcg_rn, tcg_rm);
3030 break;
3031 case 1: /* ORR */
3032 tcg_gen_or_i64(tcg_rd, tcg_rn, tcg_rm);
3033 break;
3034 case 2: /* EOR */
3035 tcg_gen_xor_i64(tcg_rd, tcg_rn, tcg_rm);
3036 break;
3037 case 4: /* BIC */
3038 case 7: /* BICS */
3039 tcg_gen_andc_i64(tcg_rd, tcg_rn, tcg_rm);
3040 break;
3041 case 5: /* ORN */
3042 tcg_gen_orc_i64(tcg_rd, tcg_rn, tcg_rm);
3043 break;
3044 case 6: /* EON */
3045 tcg_gen_eqv_i64(tcg_rd, tcg_rn, tcg_rm);
3046 break;
3047 default:
3048 assert(FALSE);
3049 break;
3050 }
3051
3052 if (!sf) {
3053 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3054 }
3055
3056 if (opc == 3) {
3057 gen_logic_CC(sf, tcg_rd);
3058 }
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00003059}
3060
Alex Bennéeb0ff21b2013-12-23 23:27:29 +00003061/*
3062 * C3.5.1 Add/subtract (extended register)
3063 *
3064 * 31|30|29|28 24|23 22|21|20 16|15 13|12 10|9 5|4 0|
3065 * +--+--+--+-----------+-----+--+-------+------+------+----+----+
3066 * |sf|op| S| 0 1 0 1 1 | opt | 1| Rm |option| imm3 | Rn | Rd |
3067 * +--+--+--+-----------+-----+--+-------+------+------+----+----+
3068 *
3069 * sf: 0 -> 32bit, 1 -> 64bit
3070 * op: 0 -> add , 1 -> sub
3071 * S: 1 -> set flags
3072 * opt: 00
3073 * option: extension type (see DecodeRegExtend)
3074 * imm3: optional shift to Rm
3075 *
3076 * Rd = Rn + LSL(extend(Rm), amount)
3077 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00003078static void disas_add_sub_ext_reg(DisasContext *s, uint32_t insn)
3079{
Alex Bennéeb0ff21b2013-12-23 23:27:29 +00003080 int rd = extract32(insn, 0, 5);
3081 int rn = extract32(insn, 5, 5);
3082 int imm3 = extract32(insn, 10, 3);
3083 int option = extract32(insn, 13, 3);
3084 int rm = extract32(insn, 16, 5);
3085 bool setflags = extract32(insn, 29, 1);
3086 bool sub_op = extract32(insn, 30, 1);
3087 bool sf = extract32(insn, 31, 1);
3088
3089 TCGv_i64 tcg_rm, tcg_rn; /* temps */
3090 TCGv_i64 tcg_rd;
3091 TCGv_i64 tcg_result;
3092
3093 if (imm3 > 4) {
3094 unallocated_encoding(s);
3095 return;
3096 }
3097
3098 /* non-flag setting ops may use SP */
3099 if (!setflags) {
Alex Bennéeb0ff21b2013-12-23 23:27:29 +00003100 tcg_rd = cpu_reg_sp(s, rd);
3101 } else {
Alex Bennéeb0ff21b2013-12-23 23:27:29 +00003102 tcg_rd = cpu_reg(s, rd);
3103 }
Alex Bennéee201ef72014-02-20 12:46:44 +00003104 tcg_rn = read_cpu_reg_sp(s, rn, sf);
Alex Bennéeb0ff21b2013-12-23 23:27:29 +00003105
3106 tcg_rm = read_cpu_reg(s, rm, sf);
3107 ext_and_shift_reg(tcg_rm, tcg_rm, option, imm3);
3108
3109 tcg_result = tcg_temp_new_i64();
3110
3111 if (!setflags) {
3112 if (sub_op) {
3113 tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
3114 } else {
3115 tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
3116 }
3117 } else {
3118 if (sub_op) {
3119 gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
3120 } else {
3121 gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
3122 }
3123 }
3124
3125 if (sf) {
3126 tcg_gen_mov_i64(tcg_rd, tcg_result);
3127 } else {
3128 tcg_gen_ext32u_i64(tcg_rd, tcg_result);
3129 }
3130
3131 tcg_temp_free_i64(tcg_result);
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00003132}
3133
Alex Bennéeb0ff21b2013-12-23 23:27:29 +00003134/*
3135 * C3.5.2 Add/subtract (shifted register)
3136 *
3137 * 31 30 29 28 24 23 22 21 20 16 15 10 9 5 4 0
3138 * +--+--+--+-----------+-----+--+-------+---------+------+------+
3139 * |sf|op| S| 0 1 0 1 1 |shift| 0| Rm | imm6 | Rn | Rd |
3140 * +--+--+--+-----------+-----+--+-------+---------+------+------+
3141 *
3142 * sf: 0 -> 32bit, 1 -> 64bit
3143 * op: 0 -> add , 1 -> sub
3144 * S: 1 -> set flags
3145 * shift: 00 -> LSL, 01 -> LSR, 10 -> ASR, 11 -> RESERVED
3146 * imm6: Shift amount to apply to Rm before the add/sub
3147 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00003148static void disas_add_sub_reg(DisasContext *s, uint32_t insn)
3149{
Alex Bennéeb0ff21b2013-12-23 23:27:29 +00003150 int rd = extract32(insn, 0, 5);
3151 int rn = extract32(insn, 5, 5);
3152 int imm6 = extract32(insn, 10, 6);
3153 int rm = extract32(insn, 16, 5);
3154 int shift_type = extract32(insn, 22, 2);
3155 bool setflags = extract32(insn, 29, 1);
3156 bool sub_op = extract32(insn, 30, 1);
3157 bool sf = extract32(insn, 31, 1);
3158
3159 TCGv_i64 tcg_rd = cpu_reg(s, rd);
3160 TCGv_i64 tcg_rn, tcg_rm;
3161 TCGv_i64 tcg_result;
3162
3163 if ((shift_type == 3) || (!sf && (imm6 > 31))) {
3164 unallocated_encoding(s);
3165 return;
3166 }
3167
3168 tcg_rn = read_cpu_reg(s, rn, sf);
3169 tcg_rm = read_cpu_reg(s, rm, sf);
3170
3171 shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, imm6);
3172
3173 tcg_result = tcg_temp_new_i64();
3174
3175 if (!setflags) {
3176 if (sub_op) {
3177 tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
3178 } else {
3179 tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
3180 }
3181 } else {
3182 if (sub_op) {
3183 gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
3184 } else {
3185 gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
3186 }
3187 }
3188
3189 if (sf) {
3190 tcg_gen_mov_i64(tcg_rd, tcg_result);
3191 } else {
3192 tcg_gen_ext32u_i64(tcg_rd, tcg_result);
3193 }
3194
3195 tcg_temp_free_i64(tcg_result);
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00003196}
3197
Alexander Graf52c8b9a2013-12-23 23:27:30 +00003198/* C3.5.9 Data-processing (3 source)
3199
3200 31 30 29 28 24 23 21 20 16 15 14 10 9 5 4 0
3201 +--+------+-----------+------+------+----+------+------+------+
3202 |sf| op54 | 1 1 0 1 1 | op31 | Rm | o0 | Ra | Rn | Rd |
3203 +--+------+-----------+------+------+----+------+------+------+
3204
3205 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00003206static void disas_data_proc_3src(DisasContext *s, uint32_t insn)
3207{
Alexander Graf52c8b9a2013-12-23 23:27:30 +00003208 int rd = extract32(insn, 0, 5);
3209 int rn = extract32(insn, 5, 5);
3210 int ra = extract32(insn, 10, 5);
3211 int rm = extract32(insn, 16, 5);
3212 int op_id = (extract32(insn, 29, 3) << 4) |
3213 (extract32(insn, 21, 3) << 1) |
3214 extract32(insn, 15, 1);
3215 bool sf = extract32(insn, 31, 1);
3216 bool is_sub = extract32(op_id, 0, 1);
3217 bool is_high = extract32(op_id, 2, 1);
3218 bool is_signed = false;
3219 TCGv_i64 tcg_op1;
3220 TCGv_i64 tcg_op2;
3221 TCGv_i64 tcg_tmp;
3222
3223 /* Note that op_id is sf:op54:op31:o0 so it includes the 32/64 size flag */
3224 switch (op_id) {
3225 case 0x42: /* SMADDL */
3226 case 0x43: /* SMSUBL */
3227 case 0x44: /* SMULH */
3228 is_signed = true;
3229 break;
3230 case 0x0: /* MADD (32bit) */
3231 case 0x1: /* MSUB (32bit) */
3232 case 0x40: /* MADD (64bit) */
3233 case 0x41: /* MSUB (64bit) */
3234 case 0x4a: /* UMADDL */
3235 case 0x4b: /* UMSUBL */
3236 case 0x4c: /* UMULH */
3237 break;
3238 default:
3239 unallocated_encoding(s);
3240 return;
3241 }
3242
3243 if (is_high) {
3244 TCGv_i64 low_bits = tcg_temp_new_i64(); /* low bits discarded */
3245 TCGv_i64 tcg_rd = cpu_reg(s, rd);
3246 TCGv_i64 tcg_rn = cpu_reg(s, rn);
3247 TCGv_i64 tcg_rm = cpu_reg(s, rm);
3248
3249 if (is_signed) {
3250 tcg_gen_muls2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
3251 } else {
3252 tcg_gen_mulu2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
3253 }
3254
3255 tcg_temp_free_i64(low_bits);
3256 return;
3257 }
3258
3259 tcg_op1 = tcg_temp_new_i64();
3260 tcg_op2 = tcg_temp_new_i64();
3261 tcg_tmp = tcg_temp_new_i64();
3262
3263 if (op_id < 0x42) {
3264 tcg_gen_mov_i64(tcg_op1, cpu_reg(s, rn));
3265 tcg_gen_mov_i64(tcg_op2, cpu_reg(s, rm));
3266 } else {
3267 if (is_signed) {
3268 tcg_gen_ext32s_i64(tcg_op1, cpu_reg(s, rn));
3269 tcg_gen_ext32s_i64(tcg_op2, cpu_reg(s, rm));
3270 } else {
3271 tcg_gen_ext32u_i64(tcg_op1, cpu_reg(s, rn));
3272 tcg_gen_ext32u_i64(tcg_op2, cpu_reg(s, rm));
3273 }
3274 }
3275
3276 if (ra == 31 && !is_sub) {
3277 /* Special-case MADD with rA == XZR; it is the standard MUL alias */
3278 tcg_gen_mul_i64(cpu_reg(s, rd), tcg_op1, tcg_op2);
3279 } else {
3280 tcg_gen_mul_i64(tcg_tmp, tcg_op1, tcg_op2);
3281 if (is_sub) {
3282 tcg_gen_sub_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
3283 } else {
3284 tcg_gen_add_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
3285 }
3286 }
3287
3288 if (!sf) {
3289 tcg_gen_ext32u_i64(cpu_reg(s, rd), cpu_reg(s, rd));
3290 }
3291
3292 tcg_temp_free_i64(tcg_op1);
3293 tcg_temp_free_i64(tcg_op2);
3294 tcg_temp_free_i64(tcg_tmp);
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00003295}
3296
Claudio Fontana643dbb02014-01-04 22:15:46 +00003297/* C3.5.3 - Add/subtract (with carry)
3298 * 31 30 29 28 27 26 25 24 23 22 21 20 16 15 10 9 5 4 0
3299 * +--+--+--+------------------------+------+---------+------+-----+
3300 * |sf|op| S| 1 1 0 1 0 0 0 0 | rm | opcode2 | Rn | Rd |
3301 * +--+--+--+------------------------+------+---------+------+-----+
3302 * [000000]
3303 */
3304
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00003305static void disas_adc_sbc(DisasContext *s, uint32_t insn)
3306{
Claudio Fontana643dbb02014-01-04 22:15:46 +00003307 unsigned int sf, op, setflags, rm, rn, rd;
3308 TCGv_i64 tcg_y, tcg_rn, tcg_rd;
3309
3310 if (extract32(insn, 10, 6) != 0) {
3311 unallocated_encoding(s);
3312 return;
3313 }
3314
3315 sf = extract32(insn, 31, 1);
3316 op = extract32(insn, 30, 1);
3317 setflags = extract32(insn, 29, 1);
3318 rm = extract32(insn, 16, 5);
3319 rn = extract32(insn, 5, 5);
3320 rd = extract32(insn, 0, 5);
3321
3322 tcg_rd = cpu_reg(s, rd);
3323 tcg_rn = cpu_reg(s, rn);
3324
3325 if (op) {
3326 tcg_y = new_tmp_a64(s);
3327 tcg_gen_not_i64(tcg_y, cpu_reg(s, rm));
3328 } else {
3329 tcg_y = cpu_reg(s, rm);
3330 }
3331
3332 if (setflags) {
3333 gen_adc_CC(sf, tcg_rd, tcg_rn, tcg_y);
3334 } else {
3335 gen_adc(sf, tcg_rd, tcg_rn, tcg_y);
3336 }
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00003337}
3338
Claudio Fontana750813c2014-01-04 22:15:46 +00003339/* C3.5.4 - C3.5.5 Conditional compare (immediate / register)
3340 * 31 30 29 28 27 26 25 24 23 22 21 20 16 15 12 11 10 9 5 4 3 0
3341 * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
3342 * |sf|op| S| 1 1 0 1 0 0 1 0 |imm5/rm | cond |i/r |o2| Rn |o3|nzcv |
3343 * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
3344 * [1] y [0] [0]
3345 */
3346static void disas_cc(DisasContext *s, uint32_t insn)
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00003347{
Claudio Fontana750813c2014-01-04 22:15:46 +00003348 unsigned int sf, op, y, cond, rn, nzcv, is_imm;
3349 int label_continue = -1;
3350 TCGv_i64 tcg_tmp, tcg_y, tcg_rn;
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00003351
Claudio Fontana750813c2014-01-04 22:15:46 +00003352 if (!extract32(insn, 29, 1)) {
3353 unallocated_encoding(s);
3354 return;
3355 }
3356 if (insn & (1 << 10 | 1 << 4)) {
3357 unallocated_encoding(s);
3358 return;
3359 }
3360 sf = extract32(insn, 31, 1);
3361 op = extract32(insn, 30, 1);
3362 is_imm = extract32(insn, 11, 1);
3363 y = extract32(insn, 16, 5); /* y = rm (reg) or imm5 (imm) */
3364 cond = extract32(insn, 12, 4);
3365 rn = extract32(insn, 5, 5);
3366 nzcv = extract32(insn, 0, 4);
3367
3368 if (cond < 0x0e) { /* not always */
3369 int label_match = gen_new_label();
3370 label_continue = gen_new_label();
3371 arm_gen_test_cc(cond, label_match);
3372 /* nomatch: */
3373 tcg_tmp = tcg_temp_new_i64();
3374 tcg_gen_movi_i64(tcg_tmp, nzcv << 28);
3375 gen_set_nzcv(tcg_tmp);
3376 tcg_temp_free_i64(tcg_tmp);
3377 tcg_gen_br(label_continue);
3378 gen_set_label(label_match);
3379 }
3380 /* match, or condition is always */
3381 if (is_imm) {
3382 tcg_y = new_tmp_a64(s);
3383 tcg_gen_movi_i64(tcg_y, y);
3384 } else {
3385 tcg_y = cpu_reg(s, y);
3386 }
3387 tcg_rn = cpu_reg(s, rn);
3388
3389 tcg_tmp = tcg_temp_new_i64();
3390 if (op) {
3391 gen_sub_CC(sf, tcg_tmp, tcg_rn, tcg_y);
3392 } else {
3393 gen_add_CC(sf, tcg_tmp, tcg_rn, tcg_y);
3394 }
3395 tcg_temp_free_i64(tcg_tmp);
3396
3397 if (cond < 0x0e) { /* continue */
3398 gen_set_label(label_continue);
3399 }
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00003400}
3401
Claudio Fontanae952d8c2013-12-17 19:42:33 +00003402/* C3.5.6 Conditional select
3403 * 31 30 29 28 21 20 16 15 12 11 10 9 5 4 0
3404 * +----+----+---+-----------------+------+------+-----+------+------+
3405 * | sf | op | S | 1 1 0 1 0 1 0 0 | Rm | cond | op2 | Rn | Rd |
3406 * +----+----+---+-----------------+------+------+-----+------+------+
3407 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00003408static void disas_cond_select(DisasContext *s, uint32_t insn)
3409{
Claudio Fontanae952d8c2013-12-17 19:42:33 +00003410 unsigned int sf, else_inv, rm, cond, else_inc, rn, rd;
3411 TCGv_i64 tcg_rd, tcg_src;
3412
3413 if (extract32(insn, 29, 1) || extract32(insn, 11, 1)) {
3414 /* S == 1 or op2<1> == 1 */
3415 unallocated_encoding(s);
3416 return;
3417 }
3418 sf = extract32(insn, 31, 1);
3419 else_inv = extract32(insn, 30, 1);
3420 rm = extract32(insn, 16, 5);
3421 cond = extract32(insn, 12, 4);
3422 else_inc = extract32(insn, 10, 1);
3423 rn = extract32(insn, 5, 5);
3424 rd = extract32(insn, 0, 5);
3425
3426 if (rd == 31) {
3427 /* silly no-op write; until we use movcond we must special-case
3428 * this to avoid a dead temporary across basic blocks.
3429 */
3430 return;
3431 }
3432
3433 tcg_rd = cpu_reg(s, rd);
3434
3435 if (cond >= 0x0e) { /* condition "always" */
3436 tcg_src = read_cpu_reg(s, rn, sf);
3437 tcg_gen_mov_i64(tcg_rd, tcg_src);
3438 } else {
3439 /* OPTME: we could use movcond here, at the cost of duplicating
3440 * a lot of the arm_gen_test_cc() logic.
3441 */
3442 int label_match = gen_new_label();
3443 int label_continue = gen_new_label();
3444
3445 arm_gen_test_cc(cond, label_match);
3446 /* nomatch: */
3447 tcg_src = cpu_reg(s, rm);
3448
3449 if (else_inv && else_inc) {
3450 tcg_gen_neg_i64(tcg_rd, tcg_src);
3451 } else if (else_inv) {
3452 tcg_gen_not_i64(tcg_rd, tcg_src);
3453 } else if (else_inc) {
3454 tcg_gen_addi_i64(tcg_rd, tcg_src, 1);
3455 } else {
3456 tcg_gen_mov_i64(tcg_rd, tcg_src);
3457 }
3458 if (!sf) {
3459 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3460 }
3461 tcg_gen_br(label_continue);
3462 /* match: */
3463 gen_set_label(label_match);
3464 tcg_src = read_cpu_reg(s, rn, sf);
3465 tcg_gen_mov_i64(tcg_rd, tcg_src);
3466 /* continue: */
3467 gen_set_label(label_continue);
3468 }
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00003469}
3470
Claudio Fontana680ead22013-12-17 19:42:35 +00003471static void handle_clz(DisasContext *s, unsigned int sf,
3472 unsigned int rn, unsigned int rd)
3473{
3474 TCGv_i64 tcg_rd, tcg_rn;
3475 tcg_rd = cpu_reg(s, rd);
3476 tcg_rn = cpu_reg(s, rn);
3477
3478 if (sf) {
3479 gen_helper_clz64(tcg_rd, tcg_rn);
3480 } else {
3481 TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
3482 tcg_gen_trunc_i64_i32(tcg_tmp32, tcg_rn);
3483 gen_helper_clz(tcg_tmp32, tcg_tmp32);
3484 tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
3485 tcg_temp_free_i32(tcg_tmp32);
3486 }
3487}
3488
Claudio Fontanae80c5022013-12-17 19:42:35 +00003489static void handle_cls(DisasContext *s, unsigned int sf,
3490 unsigned int rn, unsigned int rd)
3491{
3492 TCGv_i64 tcg_rd, tcg_rn;
3493 tcg_rd = cpu_reg(s, rd);
3494 tcg_rn = cpu_reg(s, rn);
3495
3496 if (sf) {
3497 gen_helper_cls64(tcg_rd, tcg_rn);
3498 } else {
3499 TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
3500 tcg_gen_trunc_i64_i32(tcg_tmp32, tcg_rn);
3501 gen_helper_cls32(tcg_tmp32, tcg_tmp32);
3502 tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
3503 tcg_temp_free_i32(tcg_tmp32);
3504 }
3505}
3506
Alexander Graf82e14b02013-12-17 19:42:35 +00003507static void handle_rbit(DisasContext *s, unsigned int sf,
3508 unsigned int rn, unsigned int rd)
3509{
3510 TCGv_i64 tcg_rd, tcg_rn;
3511 tcg_rd = cpu_reg(s, rd);
3512 tcg_rn = cpu_reg(s, rn);
3513
3514 if (sf) {
3515 gen_helper_rbit64(tcg_rd, tcg_rn);
3516 } else {
3517 TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
3518 tcg_gen_trunc_i64_i32(tcg_tmp32, tcg_rn);
3519 gen_helper_rbit(tcg_tmp32, tcg_tmp32);
3520 tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
3521 tcg_temp_free_i32(tcg_tmp32);
3522 }
3523}
3524
Claudio Fontana45323202013-12-17 19:42:35 +00003525/* C5.6.149 REV with sf==1, opcode==3 ("REV64") */
3526static void handle_rev64(DisasContext *s, unsigned int sf,
3527 unsigned int rn, unsigned int rd)
3528{
3529 if (!sf) {
3530 unallocated_encoding(s);
3531 return;
3532 }
3533 tcg_gen_bswap64_i64(cpu_reg(s, rd), cpu_reg(s, rn));
3534}
3535
3536/* C5.6.149 REV with sf==0, opcode==2
3537 * C5.6.151 REV32 (sf==1, opcode==2)
3538 */
3539static void handle_rev32(DisasContext *s, unsigned int sf,
3540 unsigned int rn, unsigned int rd)
3541{
3542 TCGv_i64 tcg_rd = cpu_reg(s, rd);
3543
3544 if (sf) {
3545 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
3546 TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
3547
3548 /* bswap32_i64 requires zero high word */
3549 tcg_gen_ext32u_i64(tcg_tmp, tcg_rn);
3550 tcg_gen_bswap32_i64(tcg_rd, tcg_tmp);
3551 tcg_gen_shri_i64(tcg_tmp, tcg_rn, 32);
3552 tcg_gen_bswap32_i64(tcg_tmp, tcg_tmp);
3553 tcg_gen_concat32_i64(tcg_rd, tcg_rd, tcg_tmp);
3554
3555 tcg_temp_free_i64(tcg_tmp);
3556 } else {
3557 tcg_gen_ext32u_i64(tcg_rd, cpu_reg(s, rn));
3558 tcg_gen_bswap32_i64(tcg_rd, tcg_rd);
3559 }
3560}
3561
3562/* C5.6.150 REV16 (opcode==1) */
3563static void handle_rev16(DisasContext *s, unsigned int sf,
3564 unsigned int rn, unsigned int rd)
3565{
3566 TCGv_i64 tcg_rd = cpu_reg(s, rd);
3567 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
3568 TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
3569
3570 tcg_gen_andi_i64(tcg_tmp, tcg_rn, 0xffff);
3571 tcg_gen_bswap16_i64(tcg_rd, tcg_tmp);
3572
3573 tcg_gen_shri_i64(tcg_tmp, tcg_rn, 16);
3574 tcg_gen_andi_i64(tcg_tmp, tcg_tmp, 0xffff);
3575 tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp);
3576 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, 16, 16);
3577
3578 if (sf) {
3579 tcg_gen_shri_i64(tcg_tmp, tcg_rn, 32);
3580 tcg_gen_andi_i64(tcg_tmp, tcg_tmp, 0xffff);
3581 tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp);
3582 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, 32, 16);
3583
3584 tcg_gen_shri_i64(tcg_tmp, tcg_rn, 48);
3585 tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp);
3586 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, 48, 16);
3587 }
3588
3589 tcg_temp_free_i64(tcg_tmp);
3590}
3591
Claudio Fontana680ead22013-12-17 19:42:35 +00003592/* C3.5.7 Data-processing (1 source)
3593 * 31 30 29 28 21 20 16 15 10 9 5 4 0
3594 * +----+---+---+-----------------+---------+--------+------+------+
3595 * | sf | 1 | S | 1 1 0 1 0 1 1 0 | opcode2 | opcode | Rn | Rd |
3596 * +----+---+---+-----------------+---------+--------+------+------+
3597 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00003598static void disas_data_proc_1src(DisasContext *s, uint32_t insn)
3599{
Claudio Fontana680ead22013-12-17 19:42:35 +00003600 unsigned int sf, opcode, rn, rd;
3601
3602 if (extract32(insn, 29, 1) || extract32(insn, 16, 5)) {
3603 unallocated_encoding(s);
3604 return;
3605 }
3606
3607 sf = extract32(insn, 31, 1);
3608 opcode = extract32(insn, 10, 6);
3609 rn = extract32(insn, 5, 5);
3610 rd = extract32(insn, 0, 5);
3611
3612 switch (opcode) {
3613 case 0: /* RBIT */
Alexander Graf82e14b02013-12-17 19:42:35 +00003614 handle_rbit(s, sf, rn, rd);
3615 break;
Claudio Fontana680ead22013-12-17 19:42:35 +00003616 case 1: /* REV16 */
Claudio Fontana45323202013-12-17 19:42:35 +00003617 handle_rev16(s, sf, rn, rd);
3618 break;
Claudio Fontana680ead22013-12-17 19:42:35 +00003619 case 2: /* REV32 */
Claudio Fontana45323202013-12-17 19:42:35 +00003620 handle_rev32(s, sf, rn, rd);
3621 break;
Claudio Fontana680ead22013-12-17 19:42:35 +00003622 case 3: /* REV64 */
Claudio Fontana45323202013-12-17 19:42:35 +00003623 handle_rev64(s, sf, rn, rd);
Claudio Fontana680ead22013-12-17 19:42:35 +00003624 break;
3625 case 4: /* CLZ */
3626 handle_clz(s, sf, rn, rd);
3627 break;
3628 case 5: /* CLS */
Claudio Fontanae80c5022013-12-17 19:42:35 +00003629 handle_cls(s, sf, rn, rd);
Claudio Fontana680ead22013-12-17 19:42:35 +00003630 break;
3631 }
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00003632}
3633
Alexander Graf8220e912013-12-17 19:42:34 +00003634static void handle_div(DisasContext *s, bool is_signed, unsigned int sf,
3635 unsigned int rm, unsigned int rn, unsigned int rd)
3636{
3637 TCGv_i64 tcg_n, tcg_m, tcg_rd;
3638 tcg_rd = cpu_reg(s, rd);
3639
3640 if (!sf && is_signed) {
3641 tcg_n = new_tmp_a64(s);
3642 tcg_m = new_tmp_a64(s);
3643 tcg_gen_ext32s_i64(tcg_n, cpu_reg(s, rn));
3644 tcg_gen_ext32s_i64(tcg_m, cpu_reg(s, rm));
3645 } else {
3646 tcg_n = read_cpu_reg(s, rn, sf);
3647 tcg_m = read_cpu_reg(s, rm, sf);
3648 }
3649
3650 if (is_signed) {
3651 gen_helper_sdiv64(tcg_rd, tcg_n, tcg_m);
3652 } else {
3653 gen_helper_udiv64(tcg_rd, tcg_n, tcg_m);
3654 }
3655
3656 if (!sf) { /* zero extend final result */
3657 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3658 }
3659}
3660
Alexander Graf6c1adc92013-12-17 19:42:34 +00003661/* C5.6.115 LSLV, C5.6.118 LSRV, C5.6.17 ASRV, C5.6.154 RORV */
3662static void handle_shift_reg(DisasContext *s,
3663 enum a64_shift_type shift_type, unsigned int sf,
3664 unsigned int rm, unsigned int rn, unsigned int rd)
3665{
3666 TCGv_i64 tcg_shift = tcg_temp_new_i64();
3667 TCGv_i64 tcg_rd = cpu_reg(s, rd);
3668 TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
3669
3670 tcg_gen_andi_i64(tcg_shift, cpu_reg(s, rm), sf ? 63 : 31);
3671 shift_reg(tcg_rd, tcg_rn, sf, shift_type, tcg_shift);
3672 tcg_temp_free_i64(tcg_shift);
3673}
3674
Alexander Graf8220e912013-12-17 19:42:34 +00003675/* C3.5.8 Data-processing (2 source)
3676 * 31 30 29 28 21 20 16 15 10 9 5 4 0
3677 * +----+---+---+-----------------+------+--------+------+------+
3678 * | sf | 0 | S | 1 1 0 1 0 1 1 0 | Rm | opcode | Rn | Rd |
3679 * +----+---+---+-----------------+------+--------+------+------+
3680 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00003681static void disas_data_proc_2src(DisasContext *s, uint32_t insn)
3682{
Alexander Graf8220e912013-12-17 19:42:34 +00003683 unsigned int sf, rm, opcode, rn, rd;
3684 sf = extract32(insn, 31, 1);
3685 rm = extract32(insn, 16, 5);
3686 opcode = extract32(insn, 10, 6);
3687 rn = extract32(insn, 5, 5);
3688 rd = extract32(insn, 0, 5);
3689
3690 if (extract32(insn, 29, 1)) {
3691 unallocated_encoding(s);
3692 return;
3693 }
3694
3695 switch (opcode) {
3696 case 2: /* UDIV */
3697 handle_div(s, false, sf, rm, rn, rd);
3698 break;
3699 case 3: /* SDIV */
3700 handle_div(s, true, sf, rm, rn, rd);
3701 break;
3702 case 8: /* LSLV */
Alexander Graf6c1adc92013-12-17 19:42:34 +00003703 handle_shift_reg(s, A64_SHIFT_TYPE_LSL, sf, rm, rn, rd);
3704 break;
Alexander Graf8220e912013-12-17 19:42:34 +00003705 case 9: /* LSRV */
Alexander Graf6c1adc92013-12-17 19:42:34 +00003706 handle_shift_reg(s, A64_SHIFT_TYPE_LSR, sf, rm, rn, rd);
3707 break;
Alexander Graf8220e912013-12-17 19:42:34 +00003708 case 10: /* ASRV */
Alexander Graf6c1adc92013-12-17 19:42:34 +00003709 handle_shift_reg(s, A64_SHIFT_TYPE_ASR, sf, rm, rn, rd);
3710 break;
Alexander Graf8220e912013-12-17 19:42:34 +00003711 case 11: /* RORV */
Alexander Graf6c1adc92013-12-17 19:42:34 +00003712 handle_shift_reg(s, A64_SHIFT_TYPE_ROR, sf, rm, rn, rd);
3713 break;
Alexander Graf8220e912013-12-17 19:42:34 +00003714 case 16:
3715 case 17:
3716 case 18:
3717 case 19:
3718 case 20:
3719 case 21:
3720 case 22:
3721 case 23: /* CRC32 */
3722 unsupported_encoding(s, insn);
3723 break;
3724 default:
3725 unallocated_encoding(s);
3726 break;
3727 }
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00003728}
3729
3730/* C3.5 Data processing - register */
3731static void disas_data_proc_reg(DisasContext *s, uint32_t insn)
3732{
3733 switch (extract32(insn, 24, 5)) {
3734 case 0x0a: /* Logical (shifted register) */
3735 disas_logic_reg(s, insn);
3736 break;
3737 case 0x0b: /* Add/subtract */
3738 if (insn & (1 << 21)) { /* (extended register) */
3739 disas_add_sub_ext_reg(s, insn);
3740 } else {
3741 disas_add_sub_reg(s, insn);
3742 }
3743 break;
3744 case 0x1b: /* Data-processing (3 source) */
3745 disas_data_proc_3src(s, insn);
3746 break;
3747 case 0x1a:
3748 switch (extract32(insn, 21, 3)) {
3749 case 0x0: /* Add/subtract (with carry) */
3750 disas_adc_sbc(s, insn);
3751 break;
3752 case 0x2: /* Conditional compare */
Claudio Fontana750813c2014-01-04 22:15:46 +00003753 disas_cc(s, insn); /* both imm and reg forms */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00003754 break;
3755 case 0x4: /* Conditional select */
3756 disas_cond_select(s, insn);
3757 break;
3758 case 0x6: /* Data-processing */
3759 if (insn & (1 << 30)) { /* (1 source) */
3760 disas_data_proc_1src(s, insn);
3761 } else { /* (2 source) */
3762 disas_data_proc_2src(s, insn);
3763 }
3764 break;
3765 default:
3766 unallocated_encoding(s);
3767 break;
3768 }
3769 break;
3770 default:
3771 unallocated_encoding(s);
3772 break;
3773 }
3774}
3775
Claudio Fontanada7dafe2014-01-04 22:15:50 +00003776static void handle_fp_compare(DisasContext *s, bool is_double,
3777 unsigned int rn, unsigned int rm,
3778 bool cmp_with_zero, bool signal_all_nans)
3779{
3780 TCGv_i64 tcg_flags = tcg_temp_new_i64();
3781 TCGv_ptr fpst = get_fpstatus_ptr();
3782
3783 if (is_double) {
3784 TCGv_i64 tcg_vn, tcg_vm;
3785
3786 tcg_vn = read_fp_dreg(s, rn);
3787 if (cmp_with_zero) {
3788 tcg_vm = tcg_const_i64(0);
3789 } else {
3790 tcg_vm = read_fp_dreg(s, rm);
3791 }
3792 if (signal_all_nans) {
3793 gen_helper_vfp_cmped_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
3794 } else {
3795 gen_helper_vfp_cmpd_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
3796 }
3797 tcg_temp_free_i64(tcg_vn);
3798 tcg_temp_free_i64(tcg_vm);
3799 } else {
3800 TCGv_i32 tcg_vn, tcg_vm;
3801
3802 tcg_vn = read_fp_sreg(s, rn);
3803 if (cmp_with_zero) {
3804 tcg_vm = tcg_const_i32(0);
3805 } else {
3806 tcg_vm = read_fp_sreg(s, rm);
3807 }
3808 if (signal_all_nans) {
3809 gen_helper_vfp_cmpes_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
3810 } else {
3811 gen_helper_vfp_cmps_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
3812 }
3813 tcg_temp_free_i32(tcg_vn);
3814 tcg_temp_free_i32(tcg_vm);
3815 }
3816
3817 tcg_temp_free_ptr(fpst);
3818
3819 gen_set_nzcv(tcg_flags);
3820
3821 tcg_temp_free_i64(tcg_flags);
3822}
3823
Peter Maydellfaa0ba42013-12-23 23:27:30 +00003824/* C3.6.22 Floating point compare
3825 * 31 30 29 28 24 23 22 21 20 16 15 14 13 10 9 5 4 0
3826 * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
3827 * | M | 0 | S | 1 1 1 1 0 | type | 1 | Rm | op | 1 0 0 0 | Rn | op2 |
3828 * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
3829 */
3830static void disas_fp_compare(DisasContext *s, uint32_t insn)
3831{
Claudio Fontanada7dafe2014-01-04 22:15:50 +00003832 unsigned int mos, type, rm, op, rn, opc, op2r;
3833
3834 mos = extract32(insn, 29, 3);
3835 type = extract32(insn, 22, 2); /* 0 = single, 1 = double */
3836 rm = extract32(insn, 16, 5);
3837 op = extract32(insn, 14, 2);
3838 rn = extract32(insn, 5, 5);
3839 opc = extract32(insn, 3, 2);
3840 op2r = extract32(insn, 0, 3);
3841
3842 if (mos || op || op2r || type > 1) {
3843 unallocated_encoding(s);
3844 return;
3845 }
3846
3847 handle_fp_compare(s, type, rn, rm, opc & 1, opc & 2);
Peter Maydellfaa0ba42013-12-23 23:27:30 +00003848}
3849
3850/* C3.6.23 Floating point conditional compare
3851 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 3 0
3852 * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
3853 * | M | 0 | S | 1 1 1 1 0 | type | 1 | Rm | cond | 0 1 | Rn | op | nzcv |
3854 * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
3855 */
3856static void disas_fp_ccomp(DisasContext *s, uint32_t insn)
3857{
Claudio Fontana513f1d72014-01-04 22:15:51 +00003858 unsigned int mos, type, rm, cond, rn, op, nzcv;
3859 TCGv_i64 tcg_flags;
3860 int label_continue = -1;
3861
3862 mos = extract32(insn, 29, 3);
3863 type = extract32(insn, 22, 2); /* 0 = single, 1 = double */
3864 rm = extract32(insn, 16, 5);
3865 cond = extract32(insn, 12, 4);
3866 rn = extract32(insn, 5, 5);
3867 op = extract32(insn, 4, 1);
3868 nzcv = extract32(insn, 0, 4);
3869
3870 if (mos || type > 1) {
3871 unallocated_encoding(s);
3872 return;
3873 }
3874
3875 if (cond < 0x0e) { /* not always */
3876 int label_match = gen_new_label();
3877 label_continue = gen_new_label();
3878 arm_gen_test_cc(cond, label_match);
3879 /* nomatch: */
3880 tcg_flags = tcg_const_i64(nzcv << 28);
3881 gen_set_nzcv(tcg_flags);
3882 tcg_temp_free_i64(tcg_flags);
3883 tcg_gen_br(label_continue);
3884 gen_set_label(label_match);
3885 }
3886
3887 handle_fp_compare(s, type, rn, rm, false, op);
3888
3889 if (cond < 0x0e) {
3890 gen_set_label(label_continue);
3891 }
Peter Maydellfaa0ba42013-12-23 23:27:30 +00003892}
3893
Claudio Fontana5640ff62014-01-04 22:15:51 +00003894/* copy src FP register to dst FP register; type specifies single or double */
3895static void gen_mov_fp2fp(DisasContext *s, int type, int dst, int src)
3896{
3897 if (type) {
3898 TCGv_i64 v = read_fp_dreg(s, src);
3899 write_fp_dreg(s, dst, v);
3900 tcg_temp_free_i64(v);
3901 } else {
3902 TCGv_i32 v = read_fp_sreg(s, src);
3903 write_fp_sreg(s, dst, v);
3904 tcg_temp_free_i32(v);
3905 }
3906}
3907
Peter Maydellfaa0ba42013-12-23 23:27:30 +00003908/* C3.6.24 Floating point conditional select
3909 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 0
3910 * +---+---+---+-----------+------+---+------+------+-----+------+------+
3911 * | M | 0 | S | 1 1 1 1 0 | type | 1 | Rm | cond | 1 1 | Rn | Rd |
3912 * +---+---+---+-----------+------+---+------+------+-----+------+------+
3913 */
3914static void disas_fp_csel(DisasContext *s, uint32_t insn)
3915{
Claudio Fontana5640ff62014-01-04 22:15:51 +00003916 unsigned int mos, type, rm, cond, rn, rd;
3917 int label_continue = -1;
3918
3919 mos = extract32(insn, 29, 3);
3920 type = extract32(insn, 22, 2); /* 0 = single, 1 = double */
3921 rm = extract32(insn, 16, 5);
3922 cond = extract32(insn, 12, 4);
3923 rn = extract32(insn, 5, 5);
3924 rd = extract32(insn, 0, 5);
3925
3926 if (mos || type > 1) {
3927 unallocated_encoding(s);
3928 return;
3929 }
3930
3931 if (cond < 0x0e) { /* not always */
3932 int label_match = gen_new_label();
3933 label_continue = gen_new_label();
3934 arm_gen_test_cc(cond, label_match);
3935 /* nomatch: */
3936 gen_mov_fp2fp(s, type, rd, rm);
3937 tcg_gen_br(label_continue);
3938 gen_set_label(label_match);
3939 }
3940
3941 gen_mov_fp2fp(s, type, rd, rn);
3942
3943 if (cond < 0x0e) { /* continue */
3944 gen_set_label(label_continue);
3945 }
Peter Maydellfaa0ba42013-12-23 23:27:30 +00003946}
3947
Peter Maydelld9b08482014-01-07 17:19:14 +00003948/* C3.6.25 Floating-point data-processing (1 source) - single precision */
3949static void handle_fp_1src_single(DisasContext *s, int opcode, int rd, int rn)
3950{
3951 TCGv_ptr fpst;
3952 TCGv_i32 tcg_op;
3953 TCGv_i32 tcg_res;
3954
3955 fpst = get_fpstatus_ptr();
3956 tcg_op = read_fp_sreg(s, rn);
3957 tcg_res = tcg_temp_new_i32();
3958
3959 switch (opcode) {
3960 case 0x0: /* FMOV */
3961 tcg_gen_mov_i32(tcg_res, tcg_op);
3962 break;
3963 case 0x1: /* FABS */
3964 gen_helper_vfp_abss(tcg_res, tcg_op);
3965 break;
3966 case 0x2: /* FNEG */
3967 gen_helper_vfp_negs(tcg_res, tcg_op);
3968 break;
3969 case 0x3: /* FSQRT */
3970 gen_helper_vfp_sqrts(tcg_res, tcg_op, cpu_env);
3971 break;
3972 case 0x8: /* FRINTN */
3973 case 0x9: /* FRINTP */
3974 case 0xa: /* FRINTM */
3975 case 0xb: /* FRINTZ */
3976 case 0xc: /* FRINTA */
3977 {
3978 TCGv_i32 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(opcode & 7));
3979
3980 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
3981 gen_helper_rints(tcg_res, tcg_op, fpst);
3982
3983 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
3984 tcg_temp_free_i32(tcg_rmode);
3985 break;
3986 }
3987 case 0xe: /* FRINTX */
3988 gen_helper_rints_exact(tcg_res, tcg_op, fpst);
3989 break;
3990 case 0xf: /* FRINTI */
3991 gen_helper_rints(tcg_res, tcg_op, fpst);
3992 break;
3993 default:
3994 abort();
3995 }
3996
3997 write_fp_sreg(s, rd, tcg_res);
3998
3999 tcg_temp_free_ptr(fpst);
4000 tcg_temp_free_i32(tcg_op);
4001 tcg_temp_free_i32(tcg_res);
4002}
4003
4004/* C3.6.25 Floating-point data-processing (1 source) - double precision */
4005static void handle_fp_1src_double(DisasContext *s, int opcode, int rd, int rn)
4006{
4007 TCGv_ptr fpst;
4008 TCGv_i64 tcg_op;
4009 TCGv_i64 tcg_res;
4010
4011 fpst = get_fpstatus_ptr();
4012 tcg_op = read_fp_dreg(s, rn);
4013 tcg_res = tcg_temp_new_i64();
4014
4015 switch (opcode) {
4016 case 0x0: /* FMOV */
4017 tcg_gen_mov_i64(tcg_res, tcg_op);
4018 break;
4019 case 0x1: /* FABS */
4020 gen_helper_vfp_absd(tcg_res, tcg_op);
4021 break;
4022 case 0x2: /* FNEG */
4023 gen_helper_vfp_negd(tcg_res, tcg_op);
4024 break;
4025 case 0x3: /* FSQRT */
4026 gen_helper_vfp_sqrtd(tcg_res, tcg_op, cpu_env);
4027 break;
4028 case 0x8: /* FRINTN */
4029 case 0x9: /* FRINTP */
4030 case 0xa: /* FRINTM */
4031 case 0xb: /* FRINTZ */
4032 case 0xc: /* FRINTA */
4033 {
4034 TCGv_i32 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(opcode & 7));
4035
4036 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
4037 gen_helper_rintd(tcg_res, tcg_op, fpst);
4038
4039 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
4040 tcg_temp_free_i32(tcg_rmode);
4041 break;
4042 }
4043 case 0xe: /* FRINTX */
4044 gen_helper_rintd_exact(tcg_res, tcg_op, fpst);
4045 break;
4046 case 0xf: /* FRINTI */
4047 gen_helper_rintd(tcg_res, tcg_op, fpst);
4048 break;
4049 default:
4050 abort();
4051 }
4052
4053 write_fp_dreg(s, rd, tcg_res);
4054
4055 tcg_temp_free_ptr(fpst);
4056 tcg_temp_free_i64(tcg_op);
4057 tcg_temp_free_i64(tcg_res);
4058}
4059
Peter Maydell8900aad2014-01-07 17:19:15 +00004060static void handle_fp_fcvt(DisasContext *s, int opcode,
4061 int rd, int rn, int dtype, int ntype)
4062{
4063 switch (ntype) {
4064 case 0x0:
4065 {
4066 TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
4067 if (dtype == 1) {
4068 /* Single to double */
4069 TCGv_i64 tcg_rd = tcg_temp_new_i64();
4070 gen_helper_vfp_fcvtds(tcg_rd, tcg_rn, cpu_env);
4071 write_fp_dreg(s, rd, tcg_rd);
4072 tcg_temp_free_i64(tcg_rd);
4073 } else {
4074 /* Single to half */
4075 TCGv_i32 tcg_rd = tcg_temp_new_i32();
4076 gen_helper_vfp_fcvt_f32_to_f16(tcg_rd, tcg_rn, cpu_env);
4077 /* write_fp_sreg is OK here because top half of tcg_rd is zero */
4078 write_fp_sreg(s, rd, tcg_rd);
4079 tcg_temp_free_i32(tcg_rd);
4080 }
4081 tcg_temp_free_i32(tcg_rn);
4082 break;
4083 }
4084 case 0x1:
4085 {
4086 TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
4087 TCGv_i32 tcg_rd = tcg_temp_new_i32();
4088 if (dtype == 0) {
4089 /* Double to single */
4090 gen_helper_vfp_fcvtsd(tcg_rd, tcg_rn, cpu_env);
4091 } else {
4092 /* Double to half */
4093 gen_helper_vfp_fcvt_f64_to_f16(tcg_rd, tcg_rn, cpu_env);
4094 /* write_fp_sreg is OK here because top half of tcg_rd is zero */
4095 }
4096 write_fp_sreg(s, rd, tcg_rd);
4097 tcg_temp_free_i32(tcg_rd);
4098 tcg_temp_free_i64(tcg_rn);
4099 break;
4100 }
4101 case 0x3:
4102 {
4103 TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
4104 tcg_gen_ext16u_i32(tcg_rn, tcg_rn);
4105 if (dtype == 0) {
4106 /* Half to single */
4107 TCGv_i32 tcg_rd = tcg_temp_new_i32();
4108 gen_helper_vfp_fcvt_f16_to_f32(tcg_rd, tcg_rn, cpu_env);
4109 write_fp_sreg(s, rd, tcg_rd);
4110 tcg_temp_free_i32(tcg_rd);
4111 } else {
4112 /* Half to double */
4113 TCGv_i64 tcg_rd = tcg_temp_new_i64();
4114 gen_helper_vfp_fcvt_f16_to_f64(tcg_rd, tcg_rn, cpu_env);
4115 write_fp_dreg(s, rd, tcg_rd);
4116 tcg_temp_free_i64(tcg_rd);
4117 }
4118 tcg_temp_free_i32(tcg_rn);
4119 break;
4120 }
4121 default:
4122 abort();
4123 }
4124}
4125
Peter Maydellfaa0ba42013-12-23 23:27:30 +00004126/* C3.6.25 Floating point data-processing (1 source)
4127 * 31 30 29 28 24 23 22 21 20 15 14 10 9 5 4 0
4128 * +---+---+---+-----------+------+---+--------+-----------+------+------+
4129 * | M | 0 | S | 1 1 1 1 0 | type | 1 | opcode | 1 0 0 0 0 | Rn | Rd |
4130 * +---+---+---+-----------+------+---+--------+-----------+------+------+
4131 */
4132static void disas_fp_1src(DisasContext *s, uint32_t insn)
4133{
Peter Maydelld9b08482014-01-07 17:19:14 +00004134 int type = extract32(insn, 22, 2);
4135 int opcode = extract32(insn, 15, 6);
4136 int rn = extract32(insn, 5, 5);
4137 int rd = extract32(insn, 0, 5);
4138
4139 switch (opcode) {
4140 case 0x4: case 0x5: case 0x7:
Peter Maydell8900aad2014-01-07 17:19:15 +00004141 {
Peter Maydelld9b08482014-01-07 17:19:14 +00004142 /* FCVT between half, single and double precision */
Peter Maydell8900aad2014-01-07 17:19:15 +00004143 int dtype = extract32(opcode, 0, 2);
4144 if (type == 2 || dtype == type) {
4145 unallocated_encoding(s);
4146 return;
4147 }
4148 handle_fp_fcvt(s, opcode, rd, rn, dtype, type);
Peter Maydelld9b08482014-01-07 17:19:14 +00004149 break;
Peter Maydell8900aad2014-01-07 17:19:15 +00004150 }
Peter Maydelld9b08482014-01-07 17:19:14 +00004151 case 0x0 ... 0x3:
4152 case 0x8 ... 0xc:
4153 case 0xe ... 0xf:
4154 /* 32-to-32 and 64-to-64 ops */
4155 switch (type) {
4156 case 0:
4157 handle_fp_1src_single(s, opcode, rd, rn);
4158 break;
4159 case 1:
4160 handle_fp_1src_double(s, opcode, rd, rn);
4161 break;
4162 default:
4163 unallocated_encoding(s);
4164 }
4165 break;
4166 default:
4167 unallocated_encoding(s);
4168 break;
4169 }
Peter Maydellfaa0ba42013-12-23 23:27:30 +00004170}
4171
Alexander Grafec73d2e2014-01-04 22:15:50 +00004172/* C3.6.26 Floating-point data-processing (2 source) - single precision */
4173static void handle_fp_2src_single(DisasContext *s, int opcode,
4174 int rd, int rn, int rm)
4175{
4176 TCGv_i32 tcg_op1;
4177 TCGv_i32 tcg_op2;
4178 TCGv_i32 tcg_res;
4179 TCGv_ptr fpst;
4180
4181 tcg_res = tcg_temp_new_i32();
4182 fpst = get_fpstatus_ptr();
4183 tcg_op1 = read_fp_sreg(s, rn);
4184 tcg_op2 = read_fp_sreg(s, rm);
4185
4186 switch (opcode) {
4187 case 0x0: /* FMUL */
4188 gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
4189 break;
4190 case 0x1: /* FDIV */
4191 gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
4192 break;
4193 case 0x2: /* FADD */
4194 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
4195 break;
4196 case 0x3: /* FSUB */
4197 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
4198 break;
4199 case 0x4: /* FMAX */
4200 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
4201 break;
4202 case 0x5: /* FMIN */
4203 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
4204 break;
4205 case 0x6: /* FMAXNM */
4206 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
4207 break;
4208 case 0x7: /* FMINNM */
4209 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
4210 break;
4211 case 0x8: /* FNMUL */
4212 gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
4213 gen_helper_vfp_negs(tcg_res, tcg_res);
4214 break;
4215 }
4216
4217 write_fp_sreg(s, rd, tcg_res);
4218
4219 tcg_temp_free_ptr(fpst);
4220 tcg_temp_free_i32(tcg_op1);
4221 tcg_temp_free_i32(tcg_op2);
4222 tcg_temp_free_i32(tcg_res);
4223}
4224
4225/* C3.6.26 Floating-point data-processing (2 source) - double precision */
4226static void handle_fp_2src_double(DisasContext *s, int opcode,
4227 int rd, int rn, int rm)
4228{
4229 TCGv_i64 tcg_op1;
4230 TCGv_i64 tcg_op2;
4231 TCGv_i64 tcg_res;
4232 TCGv_ptr fpst;
4233
4234 tcg_res = tcg_temp_new_i64();
4235 fpst = get_fpstatus_ptr();
4236 tcg_op1 = read_fp_dreg(s, rn);
4237 tcg_op2 = read_fp_dreg(s, rm);
4238
4239 switch (opcode) {
4240 case 0x0: /* FMUL */
4241 gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
4242 break;
4243 case 0x1: /* FDIV */
4244 gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
4245 break;
4246 case 0x2: /* FADD */
4247 gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
4248 break;
4249 case 0x3: /* FSUB */
4250 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
4251 break;
4252 case 0x4: /* FMAX */
4253 gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
4254 break;
4255 case 0x5: /* FMIN */
4256 gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
4257 break;
4258 case 0x6: /* FMAXNM */
4259 gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
4260 break;
4261 case 0x7: /* FMINNM */
4262 gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
4263 break;
4264 case 0x8: /* FNMUL */
4265 gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
4266 gen_helper_vfp_negd(tcg_res, tcg_res);
4267 break;
4268 }
4269
4270 write_fp_dreg(s, rd, tcg_res);
4271
4272 tcg_temp_free_ptr(fpst);
4273 tcg_temp_free_i64(tcg_op1);
4274 tcg_temp_free_i64(tcg_op2);
4275 tcg_temp_free_i64(tcg_res);
4276}
4277
Peter Maydellfaa0ba42013-12-23 23:27:30 +00004278/* C3.6.26 Floating point data-processing (2 source)
4279 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 0
4280 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
4281 * | M | 0 | S | 1 1 1 1 0 | type | 1 | Rm | opcode | 1 0 | Rn | Rd |
4282 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
4283 */
4284static void disas_fp_2src(DisasContext *s, uint32_t insn)
4285{
Alexander Grafec73d2e2014-01-04 22:15:50 +00004286 int type = extract32(insn, 22, 2);
4287 int rd = extract32(insn, 0, 5);
4288 int rn = extract32(insn, 5, 5);
4289 int rm = extract32(insn, 16, 5);
4290 int opcode = extract32(insn, 12, 4);
4291
4292 if (opcode > 8) {
4293 unallocated_encoding(s);
4294 return;
4295 }
4296
4297 switch (type) {
4298 case 0:
4299 handle_fp_2src_single(s, opcode, rd, rn, rm);
4300 break;
4301 case 1:
4302 handle_fp_2src_double(s, opcode, rd, rn, rm);
4303 break;
4304 default:
4305 unallocated_encoding(s);
4306 }
Peter Maydellfaa0ba42013-12-23 23:27:30 +00004307}
4308
Alexander Graf6a306672014-01-04 22:15:50 +00004309/* C3.6.27 Floating-point data-processing (3 source) - single precision */
4310static void handle_fp_3src_single(DisasContext *s, bool o0, bool o1,
4311 int rd, int rn, int rm, int ra)
4312{
4313 TCGv_i32 tcg_op1, tcg_op2, tcg_op3;
4314 TCGv_i32 tcg_res = tcg_temp_new_i32();
4315 TCGv_ptr fpst = get_fpstatus_ptr();
4316
4317 tcg_op1 = read_fp_sreg(s, rn);
4318 tcg_op2 = read_fp_sreg(s, rm);
4319 tcg_op3 = read_fp_sreg(s, ra);
4320
4321 /* These are fused multiply-add, and must be done as one
4322 * floating point operation with no rounding between the
4323 * multiplication and addition steps.
4324 * NB that doing the negations here as separate steps is
4325 * correct : an input NaN should come out with its sign bit
4326 * flipped if it is a negated-input.
4327 */
4328 if (o1 == true) {
4329 gen_helper_vfp_negs(tcg_op3, tcg_op3);
4330 }
4331
4332 if (o0 != o1) {
4333 gen_helper_vfp_negs(tcg_op1, tcg_op1);
4334 }
4335
4336 gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
4337
4338 write_fp_sreg(s, rd, tcg_res);
4339
4340 tcg_temp_free_ptr(fpst);
4341 tcg_temp_free_i32(tcg_op1);
4342 tcg_temp_free_i32(tcg_op2);
4343 tcg_temp_free_i32(tcg_op3);
4344 tcg_temp_free_i32(tcg_res);
4345}
4346
4347/* C3.6.27 Floating-point data-processing (3 source) - double precision */
4348static void handle_fp_3src_double(DisasContext *s, bool o0, bool o1,
4349 int rd, int rn, int rm, int ra)
4350{
4351 TCGv_i64 tcg_op1, tcg_op2, tcg_op3;
4352 TCGv_i64 tcg_res = tcg_temp_new_i64();
4353 TCGv_ptr fpst = get_fpstatus_ptr();
4354
4355 tcg_op1 = read_fp_dreg(s, rn);
4356 tcg_op2 = read_fp_dreg(s, rm);
4357 tcg_op3 = read_fp_dreg(s, ra);
4358
4359 /* These are fused multiply-add, and must be done as one
4360 * floating point operation with no rounding between the
4361 * multiplication and addition steps.
4362 * NB that doing the negations here as separate steps is
4363 * correct : an input NaN should come out with its sign bit
4364 * flipped if it is a negated-input.
4365 */
4366 if (o1 == true) {
4367 gen_helper_vfp_negd(tcg_op3, tcg_op3);
4368 }
4369
4370 if (o0 != o1) {
4371 gen_helper_vfp_negd(tcg_op1, tcg_op1);
4372 }
4373
4374 gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
4375
4376 write_fp_dreg(s, rd, tcg_res);
4377
4378 tcg_temp_free_ptr(fpst);
4379 tcg_temp_free_i64(tcg_op1);
4380 tcg_temp_free_i64(tcg_op2);
4381 tcg_temp_free_i64(tcg_op3);
4382 tcg_temp_free_i64(tcg_res);
4383}
4384
Peter Maydellfaa0ba42013-12-23 23:27:30 +00004385/* C3.6.27 Floating point data-processing (3 source)
4386 * 31 30 29 28 24 23 22 21 20 16 15 14 10 9 5 4 0
4387 * +---+---+---+-----------+------+----+------+----+------+------+------+
4388 * | M | 0 | S | 1 1 1 1 1 | type | o1 | Rm | o0 | Ra | Rn | Rd |
4389 * +---+---+---+-----------+------+----+------+----+------+------+------+
4390 */
4391static void disas_fp_3src(DisasContext *s, uint32_t insn)
4392{
Alexander Graf6a306672014-01-04 22:15:50 +00004393 int type = extract32(insn, 22, 2);
4394 int rd = extract32(insn, 0, 5);
4395 int rn = extract32(insn, 5, 5);
4396 int ra = extract32(insn, 10, 5);
4397 int rm = extract32(insn, 16, 5);
4398 bool o0 = extract32(insn, 15, 1);
4399 bool o1 = extract32(insn, 21, 1);
4400
4401 switch (type) {
4402 case 0:
4403 handle_fp_3src_single(s, o0, o1, rd, rn, rm, ra);
4404 break;
4405 case 1:
4406 handle_fp_3src_double(s, o0, o1, rd, rn, rm, ra);
4407 break;
4408 default:
4409 unallocated_encoding(s);
4410 }
Peter Maydellfaa0ba42013-12-23 23:27:30 +00004411}
4412
4413/* C3.6.28 Floating point immediate
4414 * 31 30 29 28 24 23 22 21 20 13 12 10 9 5 4 0
4415 * +---+---+---+-----------+------+---+------------+-------+------+------+
4416 * | M | 0 | S | 1 1 1 1 0 | type | 1 | imm8 | 1 0 0 | imm5 | Rd |
4417 * +---+---+---+-----------+------+---+------------+-------+------+------+
4418 */
4419static void disas_fp_imm(DisasContext *s, uint32_t insn)
4420{
Alexander Graf6163f862014-01-04 22:15:50 +00004421 int rd = extract32(insn, 0, 5);
4422 int imm8 = extract32(insn, 13, 8);
4423 int is_double = extract32(insn, 22, 2);
4424 uint64_t imm;
4425 TCGv_i64 tcg_res;
4426
4427 if (is_double > 1) {
4428 unallocated_encoding(s);
4429 return;
4430 }
4431
4432 /* The imm8 encodes the sign bit, enough bits to represent
4433 * an exponent in the range 01....1xx to 10....0xx,
4434 * and the most significant 4 bits of the mantissa; see
4435 * VFPExpandImm() in the v8 ARM ARM.
4436 */
4437 if (is_double) {
4438 imm = (extract32(imm8, 7, 1) ? 0x8000 : 0) |
4439 (extract32(imm8, 6, 1) ? 0x3fc0 : 0x4000) |
4440 extract32(imm8, 0, 6);
4441 imm <<= 48;
4442 } else {
4443 imm = (extract32(imm8, 7, 1) ? 0x8000 : 0) |
4444 (extract32(imm8, 6, 1) ? 0x3e00 : 0x4000) |
4445 (extract32(imm8, 0, 6) << 3);
4446 imm <<= 16;
4447 }
4448
4449 tcg_res = tcg_const_i64(imm);
4450 write_fp_dreg(s, rd, tcg_res);
4451 tcg_temp_free_i64(tcg_res);
Peter Maydellfaa0ba42013-12-23 23:27:30 +00004452}
4453
Alexander Graf52a1f6a2014-01-07 17:19:14 +00004454/* Handle floating point <=> fixed point conversions. Note that we can
4455 * also deal with fp <=> integer conversions as a special case (scale == 64)
4456 * OPTME: consider handling that special case specially or at least skipping
4457 * the call to scalbn in the helpers for zero shifts.
4458 */
4459static void handle_fpfpcvt(DisasContext *s, int rd, int rn, int opcode,
4460 bool itof, int rmode, int scale, int sf, int type)
4461{
4462 bool is_signed = !(opcode & 1);
4463 bool is_double = type;
4464 TCGv_ptr tcg_fpstatus;
4465 TCGv_i32 tcg_shift;
4466
4467 tcg_fpstatus = get_fpstatus_ptr();
4468
4469 tcg_shift = tcg_const_i32(64 - scale);
4470
4471 if (itof) {
4472 TCGv_i64 tcg_int = cpu_reg(s, rn);
4473 if (!sf) {
4474 TCGv_i64 tcg_extend = new_tmp_a64(s);
4475
4476 if (is_signed) {
4477 tcg_gen_ext32s_i64(tcg_extend, tcg_int);
4478 } else {
4479 tcg_gen_ext32u_i64(tcg_extend, tcg_int);
4480 }
4481
4482 tcg_int = tcg_extend;
4483 }
4484
4485 if (is_double) {
4486 TCGv_i64 tcg_double = tcg_temp_new_i64();
4487 if (is_signed) {
4488 gen_helper_vfp_sqtod(tcg_double, tcg_int,
4489 tcg_shift, tcg_fpstatus);
4490 } else {
4491 gen_helper_vfp_uqtod(tcg_double, tcg_int,
4492 tcg_shift, tcg_fpstatus);
4493 }
4494 write_fp_dreg(s, rd, tcg_double);
4495 tcg_temp_free_i64(tcg_double);
4496 } else {
4497 TCGv_i32 tcg_single = tcg_temp_new_i32();
4498 if (is_signed) {
4499 gen_helper_vfp_sqtos(tcg_single, tcg_int,
4500 tcg_shift, tcg_fpstatus);
4501 } else {
4502 gen_helper_vfp_uqtos(tcg_single, tcg_int,
4503 tcg_shift, tcg_fpstatus);
4504 }
4505 write_fp_sreg(s, rd, tcg_single);
4506 tcg_temp_free_i32(tcg_single);
4507 }
4508 } else {
4509 TCGv_i64 tcg_int = cpu_reg(s, rd);
4510 TCGv_i32 tcg_rmode;
4511
4512 if (extract32(opcode, 2, 1)) {
4513 /* There are too many rounding modes to all fit into rmode,
4514 * so FCVTA[US] is a special case.
4515 */
4516 rmode = FPROUNDING_TIEAWAY;
4517 }
4518
4519 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rmode));
4520
4521 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
4522
4523 if (is_double) {
4524 TCGv_i64 tcg_double = read_fp_dreg(s, rn);
4525 if (is_signed) {
4526 if (!sf) {
4527 gen_helper_vfp_tosld(tcg_int, tcg_double,
4528 tcg_shift, tcg_fpstatus);
4529 } else {
4530 gen_helper_vfp_tosqd(tcg_int, tcg_double,
4531 tcg_shift, tcg_fpstatus);
4532 }
4533 } else {
4534 if (!sf) {
4535 gen_helper_vfp_tould(tcg_int, tcg_double,
4536 tcg_shift, tcg_fpstatus);
4537 } else {
4538 gen_helper_vfp_touqd(tcg_int, tcg_double,
4539 tcg_shift, tcg_fpstatus);
4540 }
4541 }
4542 tcg_temp_free_i64(tcg_double);
4543 } else {
4544 TCGv_i32 tcg_single = read_fp_sreg(s, rn);
4545 if (sf) {
4546 if (is_signed) {
4547 gen_helper_vfp_tosqs(tcg_int, tcg_single,
4548 tcg_shift, tcg_fpstatus);
4549 } else {
4550 gen_helper_vfp_touqs(tcg_int, tcg_single,
4551 tcg_shift, tcg_fpstatus);
4552 }
4553 } else {
4554 TCGv_i32 tcg_dest = tcg_temp_new_i32();
4555 if (is_signed) {
4556 gen_helper_vfp_tosls(tcg_dest, tcg_single,
4557 tcg_shift, tcg_fpstatus);
4558 } else {
4559 gen_helper_vfp_touls(tcg_dest, tcg_single,
4560 tcg_shift, tcg_fpstatus);
4561 }
4562 tcg_gen_extu_i32_i64(tcg_int, tcg_dest);
4563 tcg_temp_free_i32(tcg_dest);
4564 }
4565 tcg_temp_free_i32(tcg_single);
4566 }
4567
4568 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
4569 tcg_temp_free_i32(tcg_rmode);
4570
4571 if (!sf) {
4572 tcg_gen_ext32u_i64(tcg_int, tcg_int);
4573 }
4574 }
4575
4576 tcg_temp_free_ptr(tcg_fpstatus);
4577 tcg_temp_free_i32(tcg_shift);
4578}
4579
Peter Maydellfaa0ba42013-12-23 23:27:30 +00004580/* C3.6.29 Floating point <-> fixed point conversions
4581 * 31 30 29 28 24 23 22 21 20 19 18 16 15 10 9 5 4 0
4582 * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
4583 * | sf | 0 | S | 1 1 1 1 0 | type | 0 | rmode | opcode | scale | Rn | Rd |
4584 * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
4585 */
4586static void disas_fp_fixed_conv(DisasContext *s, uint32_t insn)
4587{
Alexander Graf52a1f6a2014-01-07 17:19:14 +00004588 int rd = extract32(insn, 0, 5);
4589 int rn = extract32(insn, 5, 5);
4590 int scale = extract32(insn, 10, 6);
4591 int opcode = extract32(insn, 16, 3);
4592 int rmode = extract32(insn, 19, 2);
4593 int type = extract32(insn, 22, 2);
4594 bool sbit = extract32(insn, 29, 1);
4595 bool sf = extract32(insn, 31, 1);
4596 bool itof;
4597
4598 if (sbit || (type > 1)
4599 || (!sf && scale < 32)) {
4600 unallocated_encoding(s);
4601 return;
4602 }
4603
4604 switch ((rmode << 3) | opcode) {
4605 case 0x2: /* SCVTF */
4606 case 0x3: /* UCVTF */
4607 itof = true;
4608 break;
4609 case 0x18: /* FCVTZS */
4610 case 0x19: /* FCVTZU */
4611 itof = false;
4612 break;
4613 default:
4614 unallocated_encoding(s);
4615 return;
4616 }
4617
4618 handle_fpfpcvt(s, rd, rn, opcode, itof, FPROUNDING_ZERO, scale, sf, type);
Peter Maydellfaa0ba42013-12-23 23:27:30 +00004619}
4620
Peter Maydellce5458e2013-12-23 23:27:31 +00004621static void handle_fmov(DisasContext *s, int rd, int rn, int type, bool itof)
4622{
4623 /* FMOV: gpr to or from float, double, or top half of quad fp reg,
4624 * without conversion.
4625 */
4626
4627 if (itof) {
Peter Maydellce5458e2013-12-23 23:27:31 +00004628 TCGv_i64 tcg_rn = cpu_reg(s, rn);
4629
4630 switch (type) {
4631 case 0:
4632 {
4633 /* 32 bit */
4634 TCGv_i64 tmp = tcg_temp_new_i64();
4635 tcg_gen_ext32u_i64(tmp, tcg_rn);
Peter Maydelle2f90562014-01-04 22:15:49 +00004636 tcg_gen_st_i64(tmp, cpu_env, fp_reg_offset(rd, MO_64));
Peter Maydellce5458e2013-12-23 23:27:31 +00004637 tcg_gen_movi_i64(tmp, 0);
Peter Maydelle2f90562014-01-04 22:15:49 +00004638 tcg_gen_st_i64(tmp, cpu_env, fp_reg_hi_offset(rd));
Peter Maydellce5458e2013-12-23 23:27:31 +00004639 tcg_temp_free_i64(tmp);
4640 break;
4641 }
4642 case 1:
4643 {
4644 /* 64 bit */
4645 TCGv_i64 tmp = tcg_const_i64(0);
Peter Maydelle2f90562014-01-04 22:15:49 +00004646 tcg_gen_st_i64(tcg_rn, cpu_env, fp_reg_offset(rd, MO_64));
4647 tcg_gen_st_i64(tmp, cpu_env, fp_reg_hi_offset(rd));
Peter Maydellce5458e2013-12-23 23:27:31 +00004648 tcg_temp_free_i64(tmp);
4649 break;
4650 }
4651 case 2:
4652 /* 64 bit to top half. */
Peter Maydelle2f90562014-01-04 22:15:49 +00004653 tcg_gen_st_i64(tcg_rn, cpu_env, fp_reg_hi_offset(rd));
Peter Maydellce5458e2013-12-23 23:27:31 +00004654 break;
4655 }
4656 } else {
Peter Maydellce5458e2013-12-23 23:27:31 +00004657 TCGv_i64 tcg_rd = cpu_reg(s, rd);
4658
4659 switch (type) {
4660 case 0:
4661 /* 32 bit */
Peter Maydelle2f90562014-01-04 22:15:49 +00004662 tcg_gen_ld32u_i64(tcg_rd, cpu_env, fp_reg_offset(rn, MO_32));
4663 break;
4664 case 1:
4665 /* 64 bit */
4666 tcg_gen_ld_i64(tcg_rd, cpu_env, fp_reg_offset(rn, MO_64));
Peter Maydellce5458e2013-12-23 23:27:31 +00004667 break;
4668 case 2:
4669 /* 64 bits from top half */
Peter Maydelle2f90562014-01-04 22:15:49 +00004670 tcg_gen_ld_i64(tcg_rd, cpu_env, fp_reg_hi_offset(rn));
Peter Maydellce5458e2013-12-23 23:27:31 +00004671 break;
4672 }
4673 }
4674}
4675
Peter Maydellfaa0ba42013-12-23 23:27:30 +00004676/* C3.6.30 Floating point <-> integer conversions
4677 * 31 30 29 28 24 23 22 21 20 19 18 16 15 10 9 5 4 0
4678 * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
Will Newtonc436d402014-01-07 17:19:14 +00004679 * | sf | 0 | S | 1 1 1 1 0 | type | 1 | rmode | opc | 0 0 0 0 0 0 | Rn | Rd |
Peter Maydellfaa0ba42013-12-23 23:27:30 +00004680 * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
4681 */
4682static void disas_fp_int_conv(DisasContext *s, uint32_t insn)
4683{
Peter Maydellce5458e2013-12-23 23:27:31 +00004684 int rd = extract32(insn, 0, 5);
4685 int rn = extract32(insn, 5, 5);
4686 int opcode = extract32(insn, 16, 3);
4687 int rmode = extract32(insn, 19, 2);
4688 int type = extract32(insn, 22, 2);
4689 bool sbit = extract32(insn, 29, 1);
4690 bool sf = extract32(insn, 31, 1);
4691
Will Newtonc436d402014-01-07 17:19:14 +00004692 if (sbit) {
4693 unallocated_encoding(s);
4694 return;
4695 }
4696
4697 if (opcode > 5) {
Peter Maydellce5458e2013-12-23 23:27:31 +00004698 /* FMOV */
4699 bool itof = opcode & 1;
4700
Will Newtonc436d402014-01-07 17:19:14 +00004701 if (rmode >= 2) {
4702 unallocated_encoding(s);
4703 return;
4704 }
4705
Peter Maydellce5458e2013-12-23 23:27:31 +00004706 switch (sf << 3 | type << 1 | rmode) {
4707 case 0x0: /* 32 bit */
4708 case 0xa: /* 64 bit */
4709 case 0xd: /* 64 bit to top half of quad */
4710 break;
4711 default:
4712 /* all other sf/type/rmode combinations are invalid */
4713 unallocated_encoding(s);
4714 break;
4715 }
4716
4717 handle_fmov(s, rd, rn, type, itof);
4718 } else {
4719 /* actual FP conversions */
Will Newtonc436d402014-01-07 17:19:14 +00004720 bool itof = extract32(opcode, 1, 1);
4721
4722 if (type > 1 || (rmode != 0 && opcode > 1)) {
4723 unallocated_encoding(s);
4724 return;
4725 }
4726
4727 handle_fpfpcvt(s, rd, rn, opcode, itof, rmode, 64, sf, type);
Peter Maydellce5458e2013-12-23 23:27:31 +00004728 }
Peter Maydellfaa0ba42013-12-23 23:27:30 +00004729}
4730
4731/* FP-specific subcases of table C3-6 (SIMD and FP data processing)
4732 * 31 30 29 28 25 24 0
4733 * +---+---+---+---------+-----------------------------+
4734 * | | 0 | | 1 1 1 1 | |
4735 * +---+---+---+---------+-----------------------------+
4736 */
4737static void disas_data_proc_fp(DisasContext *s, uint32_t insn)
4738{
4739 if (extract32(insn, 24, 1)) {
4740 /* Floating point data-processing (3 source) */
4741 disas_fp_3src(s, insn);
4742 } else if (extract32(insn, 21, 1) == 0) {
4743 /* Floating point to fixed point conversions */
4744 disas_fp_fixed_conv(s, insn);
4745 } else {
4746 switch (extract32(insn, 10, 2)) {
4747 case 1:
4748 /* Floating point conditional compare */
4749 disas_fp_ccomp(s, insn);
4750 break;
4751 case 2:
4752 /* Floating point data-processing (2 source) */
4753 disas_fp_2src(s, insn);
4754 break;
4755 case 3:
4756 /* Floating point conditional select */
4757 disas_fp_csel(s, insn);
4758 break;
4759 case 0:
4760 switch (ctz32(extract32(insn, 12, 4))) {
4761 case 0: /* [15:12] == xxx1 */
4762 /* Floating point immediate */
4763 disas_fp_imm(s, insn);
4764 break;
4765 case 1: /* [15:12] == xx10 */
4766 /* Floating point compare */
4767 disas_fp_compare(s, insn);
4768 break;
4769 case 2: /* [15:12] == x100 */
4770 /* Floating point data-processing (1 source) */
4771 disas_fp_1src(s, insn);
4772 break;
4773 case 3: /* [15:12] == 1000 */
4774 unallocated_encoding(s);
4775 break;
4776 default: /* [15:12] == 0000 */
4777 /* Floating point <-> integer conversions */
4778 disas_fp_int_conv(s, insn);
4779 break;
4780 }
4781 break;
4782 }
4783 }
4784}
4785
Peter Maydell5c737472014-01-31 14:47:30 +00004786static void do_ext64(DisasContext *s, TCGv_i64 tcg_left, TCGv_i64 tcg_right,
4787 int pos)
4788{
4789 /* Extract 64 bits from the middle of two concatenated 64 bit
4790 * vector register slices left:right. The extracted bits start
4791 * at 'pos' bits into the right (least significant) side.
4792 * We return the result in tcg_right, and guarantee not to
4793 * trash tcg_left.
4794 */
4795 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
4796 assert(pos > 0 && pos < 64);
4797
4798 tcg_gen_shri_i64(tcg_right, tcg_right, pos);
4799 tcg_gen_shli_i64(tcg_tmp, tcg_left, 64 - pos);
4800 tcg_gen_or_i64(tcg_right, tcg_right, tcg_tmp);
4801
4802 tcg_temp_free_i64(tcg_tmp);
4803}
4804
Alex Bennée384b26f2014-01-31 14:47:30 +00004805/* C3.6.1 EXT
4806 * 31 30 29 24 23 22 21 20 16 15 14 11 10 9 5 4 0
4807 * +---+---+-------------+-----+---+------+---+------+---+------+------+
4808 * | 0 | Q | 1 0 1 1 1 0 | op2 | 0 | Rm | 0 | imm4 | 0 | Rn | Rd |
4809 * +---+---+-------------+-----+---+------+---+------+---+------+------+
4810 */
4811static void disas_simd_ext(DisasContext *s, uint32_t insn)
4812{
Peter Maydell5c737472014-01-31 14:47:30 +00004813 int is_q = extract32(insn, 30, 1);
4814 int op2 = extract32(insn, 22, 2);
4815 int imm4 = extract32(insn, 11, 4);
4816 int rm = extract32(insn, 16, 5);
4817 int rn = extract32(insn, 5, 5);
4818 int rd = extract32(insn, 0, 5);
4819 int pos = imm4 << 3;
4820 TCGv_i64 tcg_resl, tcg_resh;
4821
4822 if (op2 != 0 || (!is_q && extract32(imm4, 3, 1))) {
4823 unallocated_encoding(s);
4824 return;
4825 }
4826
4827 tcg_resh = tcg_temp_new_i64();
4828 tcg_resl = tcg_temp_new_i64();
4829
4830 /* Vd gets bits starting at pos bits into Vm:Vn. This is
4831 * either extracting 128 bits from a 128:128 concatenation, or
4832 * extracting 64 bits from a 64:64 concatenation.
4833 */
4834 if (!is_q) {
4835 read_vec_element(s, tcg_resl, rn, 0, MO_64);
4836 if (pos != 0) {
4837 read_vec_element(s, tcg_resh, rm, 0, MO_64);
4838 do_ext64(s, tcg_resh, tcg_resl, pos);
4839 }
4840 tcg_gen_movi_i64(tcg_resh, 0);
4841 } else {
4842 TCGv_i64 tcg_hh;
4843 typedef struct {
4844 int reg;
4845 int elt;
4846 } EltPosns;
4847 EltPosns eltposns[] = { {rn, 0}, {rn, 1}, {rm, 0}, {rm, 1} };
4848 EltPosns *elt = eltposns;
4849
4850 if (pos >= 64) {
4851 elt++;
4852 pos -= 64;
4853 }
4854
4855 read_vec_element(s, tcg_resl, elt->reg, elt->elt, MO_64);
4856 elt++;
4857 read_vec_element(s, tcg_resh, elt->reg, elt->elt, MO_64);
4858 elt++;
4859 if (pos != 0) {
4860 do_ext64(s, tcg_resh, tcg_resl, pos);
4861 tcg_hh = tcg_temp_new_i64();
4862 read_vec_element(s, tcg_hh, elt->reg, elt->elt, MO_64);
4863 do_ext64(s, tcg_hh, tcg_resh, pos);
4864 tcg_temp_free_i64(tcg_hh);
4865 }
4866 }
4867
4868 write_vec_element(s, tcg_resl, rd, 0, MO_64);
4869 tcg_temp_free_i64(tcg_resl);
4870 write_vec_element(s, tcg_resh, rd, 1, MO_64);
4871 tcg_temp_free_i64(tcg_resh);
Alex Bennée384b26f2014-01-31 14:47:30 +00004872}
4873
4874/* C3.6.2 TBL/TBX
4875 * 31 30 29 24 23 22 21 20 16 15 14 13 12 11 10 9 5 4 0
4876 * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
4877 * | 0 | Q | 0 0 1 1 1 0 | op2 | 0 | Rm | 0 | len | op | 0 0 | Rn | Rd |
4878 * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
4879 */
4880static void disas_simd_tb(DisasContext *s, uint32_t insn)
4881{
Michael Matz7c510482014-01-31 14:47:31 +00004882 int op2 = extract32(insn, 22, 2);
4883 int is_q = extract32(insn, 30, 1);
4884 int rm = extract32(insn, 16, 5);
4885 int rn = extract32(insn, 5, 5);
4886 int rd = extract32(insn, 0, 5);
4887 int is_tblx = extract32(insn, 12, 1);
4888 int len = extract32(insn, 13, 2);
4889 TCGv_i64 tcg_resl, tcg_resh, tcg_idx;
4890 TCGv_i32 tcg_regno, tcg_numregs;
4891
4892 if (op2 != 0) {
4893 unallocated_encoding(s);
4894 return;
4895 }
4896
4897 /* This does a table lookup: for every byte element in the input
4898 * we index into a table formed from up to four vector registers,
4899 * and then the output is the result of the lookups. Our helper
4900 * function does the lookup operation for a single 64 bit part of
4901 * the input.
4902 */
4903 tcg_resl = tcg_temp_new_i64();
4904 tcg_resh = tcg_temp_new_i64();
4905
4906 if (is_tblx) {
4907 read_vec_element(s, tcg_resl, rd, 0, MO_64);
4908 } else {
4909 tcg_gen_movi_i64(tcg_resl, 0);
4910 }
4911 if (is_tblx && is_q) {
4912 read_vec_element(s, tcg_resh, rd, 1, MO_64);
4913 } else {
4914 tcg_gen_movi_i64(tcg_resh, 0);
4915 }
4916
4917 tcg_idx = tcg_temp_new_i64();
4918 tcg_regno = tcg_const_i32(rn);
4919 tcg_numregs = tcg_const_i32(len + 1);
4920 read_vec_element(s, tcg_idx, rm, 0, MO_64);
4921 gen_helper_simd_tbl(tcg_resl, cpu_env, tcg_resl, tcg_idx,
4922 tcg_regno, tcg_numregs);
4923 if (is_q) {
4924 read_vec_element(s, tcg_idx, rm, 1, MO_64);
4925 gen_helper_simd_tbl(tcg_resh, cpu_env, tcg_resh, tcg_idx,
4926 tcg_regno, tcg_numregs);
4927 }
4928 tcg_temp_free_i64(tcg_idx);
4929 tcg_temp_free_i32(tcg_regno);
4930 tcg_temp_free_i32(tcg_numregs);
4931
4932 write_vec_element(s, tcg_resl, rd, 0, MO_64);
4933 tcg_temp_free_i64(tcg_resl);
4934 write_vec_element(s, tcg_resh, rd, 1, MO_64);
4935 tcg_temp_free_i64(tcg_resh);
Alex Bennée384b26f2014-01-31 14:47:30 +00004936}
4937
4938/* C3.6.3 ZIP/UZP/TRN
4939 * 31 30 29 24 23 22 21 20 16 15 14 12 11 10 9 5 4 0
4940 * +---+---+-------------+------+---+------+---+------------------+------+
4941 * | 0 | Q | 0 0 1 1 1 0 | size | 0 | Rm | 0 | opc | 1 0 | Rn | Rd |
4942 * +---+---+-------------+------+---+------+---+------------------+------+
4943 */
4944static void disas_simd_zip_trn(DisasContext *s, uint32_t insn)
4945{
Michael Matz5fa54692014-01-31 14:47:31 +00004946 int rd = extract32(insn, 0, 5);
4947 int rn = extract32(insn, 5, 5);
4948 int rm = extract32(insn, 16, 5);
4949 int size = extract32(insn, 22, 2);
4950 /* opc field bits [1:0] indicate ZIP/UZP/TRN;
4951 * bit 2 indicates 1 vs 2 variant of the insn.
4952 */
4953 int opcode = extract32(insn, 12, 2);
4954 bool part = extract32(insn, 14, 1);
4955 bool is_q = extract32(insn, 30, 1);
4956 int esize = 8 << size;
4957 int i, ofs;
4958 int datasize = is_q ? 128 : 64;
4959 int elements = datasize / esize;
4960 TCGv_i64 tcg_res, tcg_resl, tcg_resh;
4961
4962 if (opcode == 0 || (size == 3 && !is_q)) {
4963 unallocated_encoding(s);
4964 return;
4965 }
4966
4967 tcg_resl = tcg_const_i64(0);
4968 tcg_resh = tcg_const_i64(0);
4969 tcg_res = tcg_temp_new_i64();
4970
4971 for (i = 0; i < elements; i++) {
4972 switch (opcode) {
4973 case 1: /* UZP1/2 */
4974 {
4975 int midpoint = elements / 2;
4976 if (i < midpoint) {
4977 read_vec_element(s, tcg_res, rn, 2 * i + part, size);
4978 } else {
4979 read_vec_element(s, tcg_res, rm,
4980 2 * (i - midpoint) + part, size);
4981 }
4982 break;
4983 }
4984 case 2: /* TRN1/2 */
4985 if (i & 1) {
4986 read_vec_element(s, tcg_res, rm, (i & ~1) + part, size);
4987 } else {
4988 read_vec_element(s, tcg_res, rn, (i & ~1) + part, size);
4989 }
4990 break;
4991 case 3: /* ZIP1/2 */
4992 {
4993 int base = part * elements / 2;
4994 if (i & 1) {
4995 read_vec_element(s, tcg_res, rm, base + (i >> 1), size);
4996 } else {
4997 read_vec_element(s, tcg_res, rn, base + (i >> 1), size);
4998 }
4999 break;
5000 }
5001 default:
5002 g_assert_not_reached();
5003 }
5004
5005 ofs = i * esize;
5006 if (ofs < 64) {
5007 tcg_gen_shli_i64(tcg_res, tcg_res, ofs);
5008 tcg_gen_or_i64(tcg_resl, tcg_resl, tcg_res);
5009 } else {
5010 tcg_gen_shli_i64(tcg_res, tcg_res, ofs - 64);
5011 tcg_gen_or_i64(tcg_resh, tcg_resh, tcg_res);
5012 }
5013 }
5014
5015 tcg_temp_free_i64(tcg_res);
5016
5017 write_vec_element(s, tcg_resl, rd, 0, MO_64);
5018 tcg_temp_free_i64(tcg_resl);
5019 write_vec_element(s, tcg_resh, rd, 1, MO_64);
5020 tcg_temp_free_i64(tcg_resh);
Alex Bennée384b26f2014-01-31 14:47:30 +00005021}
5022
Michael Matz4a0ff1c2014-01-31 14:47:31 +00005023static void do_minmaxop(DisasContext *s, TCGv_i32 tcg_elt1, TCGv_i32 tcg_elt2,
5024 int opc, bool is_min, TCGv_ptr fpst)
5025{
5026 /* Helper function for disas_simd_across_lanes: do a single precision
5027 * min/max operation on the specified two inputs,
5028 * and return the result in tcg_elt1.
5029 */
5030 if (opc == 0xc) {
5031 if (is_min) {
5032 gen_helper_vfp_minnums(tcg_elt1, tcg_elt1, tcg_elt2, fpst);
5033 } else {
5034 gen_helper_vfp_maxnums(tcg_elt1, tcg_elt1, tcg_elt2, fpst);
5035 }
5036 } else {
5037 assert(opc == 0xf);
5038 if (is_min) {
5039 gen_helper_vfp_mins(tcg_elt1, tcg_elt1, tcg_elt2, fpst);
5040 } else {
5041 gen_helper_vfp_maxs(tcg_elt1, tcg_elt1, tcg_elt2, fpst);
5042 }
5043 }
5044}
5045
Alex Bennée384b26f2014-01-31 14:47:30 +00005046/* C3.6.4 AdvSIMD across lanes
5047 * 31 30 29 28 24 23 22 21 17 16 12 11 10 9 5 4 0
5048 * +---+---+---+-----------+------+-----------+--------+-----+------+------+
5049 * | 0 | Q | U | 0 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 | Rn | Rd |
5050 * +---+---+---+-----------+------+-----------+--------+-----+------+------+
5051 */
5052static void disas_simd_across_lanes(DisasContext *s, uint32_t insn)
5053{
Michael Matz4a0ff1c2014-01-31 14:47:31 +00005054 int rd = extract32(insn, 0, 5);
5055 int rn = extract32(insn, 5, 5);
5056 int size = extract32(insn, 22, 2);
5057 int opcode = extract32(insn, 12, 5);
5058 bool is_q = extract32(insn, 30, 1);
5059 bool is_u = extract32(insn, 29, 1);
5060 bool is_fp = false;
5061 bool is_min = false;
5062 int esize;
5063 int elements;
5064 int i;
5065 TCGv_i64 tcg_res, tcg_elt;
5066
5067 switch (opcode) {
5068 case 0x1b: /* ADDV */
5069 if (is_u) {
5070 unallocated_encoding(s);
5071 return;
5072 }
5073 /* fall through */
5074 case 0x3: /* SADDLV, UADDLV */
5075 case 0xa: /* SMAXV, UMAXV */
5076 case 0x1a: /* SMINV, UMINV */
5077 if (size == 3 || (size == 2 && !is_q)) {
5078 unallocated_encoding(s);
5079 return;
5080 }
5081 break;
5082 case 0xc: /* FMAXNMV, FMINNMV */
5083 case 0xf: /* FMAXV, FMINV */
5084 if (!is_u || !is_q || extract32(size, 0, 1)) {
5085 unallocated_encoding(s);
5086 return;
5087 }
5088 /* Bit 1 of size field encodes min vs max, and actual size is always
5089 * 32 bits: adjust the size variable so following code can rely on it
5090 */
5091 is_min = extract32(size, 1, 1);
5092 is_fp = true;
5093 size = 2;
5094 break;
5095 default:
5096 unallocated_encoding(s);
5097 return;
5098 }
5099
5100 esize = 8 << size;
5101 elements = (is_q ? 128 : 64) / esize;
5102
5103 tcg_res = tcg_temp_new_i64();
5104 tcg_elt = tcg_temp_new_i64();
5105
5106 /* These instructions operate across all lanes of a vector
5107 * to produce a single result. We can guarantee that a 64
5108 * bit intermediate is sufficient:
5109 * + for [US]ADDLV the maximum element size is 32 bits, and
5110 * the result type is 64 bits
5111 * + for FMAX*V, FMIN*V, ADDV the intermediate type is the
5112 * same as the element size, which is 32 bits at most
5113 * For the integer operations we can choose to work at 64
5114 * or 32 bits and truncate at the end; for simplicity
5115 * we use 64 bits always. The floating point
5116 * ops do require 32 bit intermediates, though.
5117 */
5118 if (!is_fp) {
5119 read_vec_element(s, tcg_res, rn, 0, size | (is_u ? 0 : MO_SIGN));
5120
5121 for (i = 1; i < elements; i++) {
5122 read_vec_element(s, tcg_elt, rn, i, size | (is_u ? 0 : MO_SIGN));
5123
5124 switch (opcode) {
5125 case 0x03: /* SADDLV / UADDLV */
5126 case 0x1b: /* ADDV */
5127 tcg_gen_add_i64(tcg_res, tcg_res, tcg_elt);
5128 break;
5129 case 0x0a: /* SMAXV / UMAXV */
5130 tcg_gen_movcond_i64(is_u ? TCG_COND_GEU : TCG_COND_GE,
5131 tcg_res,
5132 tcg_res, tcg_elt, tcg_res, tcg_elt);
5133 break;
5134 case 0x1a: /* SMINV / UMINV */
5135 tcg_gen_movcond_i64(is_u ? TCG_COND_LEU : TCG_COND_LE,
5136 tcg_res,
5137 tcg_res, tcg_elt, tcg_res, tcg_elt);
5138 break;
5139 break;
5140 default:
5141 g_assert_not_reached();
5142 }
5143
5144 }
5145 } else {
5146 /* Floating point ops which work on 32 bit (single) intermediates.
5147 * Note that correct NaN propagation requires that we do these
5148 * operations in exactly the order specified by the pseudocode.
5149 */
5150 TCGv_i32 tcg_elt1 = tcg_temp_new_i32();
5151 TCGv_i32 tcg_elt2 = tcg_temp_new_i32();
5152 TCGv_i32 tcg_elt3 = tcg_temp_new_i32();
5153 TCGv_ptr fpst = get_fpstatus_ptr();
5154
5155 assert(esize == 32);
5156 assert(elements == 4);
5157
5158 read_vec_element(s, tcg_elt, rn, 0, MO_32);
5159 tcg_gen_trunc_i64_i32(tcg_elt1, tcg_elt);
5160 read_vec_element(s, tcg_elt, rn, 1, MO_32);
5161 tcg_gen_trunc_i64_i32(tcg_elt2, tcg_elt);
5162
5163 do_minmaxop(s, tcg_elt1, tcg_elt2, opcode, is_min, fpst);
5164
5165 read_vec_element(s, tcg_elt, rn, 2, MO_32);
5166 tcg_gen_trunc_i64_i32(tcg_elt2, tcg_elt);
5167 read_vec_element(s, tcg_elt, rn, 3, MO_32);
5168 tcg_gen_trunc_i64_i32(tcg_elt3, tcg_elt);
5169
5170 do_minmaxop(s, tcg_elt2, tcg_elt3, opcode, is_min, fpst);
5171
5172 do_minmaxop(s, tcg_elt1, tcg_elt2, opcode, is_min, fpst);
5173
5174 tcg_gen_extu_i32_i64(tcg_res, tcg_elt1);
5175 tcg_temp_free_i32(tcg_elt1);
5176 tcg_temp_free_i32(tcg_elt2);
5177 tcg_temp_free_i32(tcg_elt3);
5178 tcg_temp_free_ptr(fpst);
5179 }
5180
5181 tcg_temp_free_i64(tcg_elt);
5182
5183 /* Now truncate the result to the width required for the final output */
5184 if (opcode == 0x03) {
5185 /* SADDLV, UADDLV: result is 2*esize */
5186 size++;
5187 }
5188
5189 switch (size) {
5190 case 0:
5191 tcg_gen_ext8u_i64(tcg_res, tcg_res);
5192 break;
5193 case 1:
5194 tcg_gen_ext16u_i64(tcg_res, tcg_res);
5195 break;
5196 case 2:
5197 tcg_gen_ext32u_i64(tcg_res, tcg_res);
5198 break;
5199 case 3:
5200 break;
5201 default:
5202 g_assert_not_reached();
5203 }
5204
5205 write_fp_dreg(s, rd, tcg_res);
5206 tcg_temp_free_i64(tcg_res);
Alex Bennée384b26f2014-01-31 14:47:30 +00005207}
5208
Alex Bennée67bb9382014-01-31 14:47:31 +00005209/* C6.3.31 DUP (Element, Vector)
5210 *
5211 * 31 30 29 21 20 16 15 10 9 5 4 0
5212 * +---+---+-------------------+--------+-------------+------+------+
5213 * | 0 | Q | 0 0 1 1 1 0 0 0 0 | imm5 | 0 0 0 0 0 1 | Rn | Rd |
5214 * +---+---+-------------------+--------+-------------+------+------+
5215 *
5216 * size: encoded in imm5 (see ARM ARM LowestSetBit())
5217 */
5218static void handle_simd_dupe(DisasContext *s, int is_q, int rd, int rn,
5219 int imm5)
5220{
5221 int size = ctz32(imm5);
5222 int esize = 8 << size;
5223 int elements = (is_q ? 128 : 64) / esize;
5224 int index, i;
5225 TCGv_i64 tmp;
5226
5227 if (size > 3 || (size == 3 && !is_q)) {
5228 unallocated_encoding(s);
5229 return;
5230 }
5231
5232 index = imm5 >> (size + 1);
5233
5234 tmp = tcg_temp_new_i64();
5235 read_vec_element(s, tmp, rn, index, size);
5236
5237 for (i = 0; i < elements; i++) {
5238 write_vec_element(s, tmp, rd, i, size);
5239 }
5240
5241 if (!is_q) {
5242 clear_vec_high(s, rd);
5243 }
5244
5245 tcg_temp_free_i64(tmp);
5246}
5247
Peter Maydell360a6f22014-01-31 14:47:32 +00005248/* C6.3.31 DUP (element, scalar)
5249 * 31 21 20 16 15 10 9 5 4 0
5250 * +-----------------------+--------+-------------+------+------+
5251 * | 0 1 0 1 1 1 1 0 0 0 0 | imm5 | 0 0 0 0 0 1 | Rn | Rd |
5252 * +-----------------------+--------+-------------+------+------+
5253 */
5254static void handle_simd_dupes(DisasContext *s, int rd, int rn,
5255 int imm5)
5256{
5257 int size = ctz32(imm5);
5258 int index;
5259 TCGv_i64 tmp;
5260
5261 if (size > 3) {
5262 unallocated_encoding(s);
5263 return;
5264 }
5265
5266 index = imm5 >> (size + 1);
5267
5268 /* This instruction just extracts the specified element and
5269 * zero-extends it into the bottom of the destination register.
5270 */
5271 tmp = tcg_temp_new_i64();
5272 read_vec_element(s, tmp, rn, index, size);
5273 write_fp_dreg(s, rd, tmp);
5274 tcg_temp_free_i64(tmp);
5275}
5276
Alex Bennée67bb9382014-01-31 14:47:31 +00005277/* C6.3.32 DUP (General)
5278 *
5279 * 31 30 29 21 20 16 15 10 9 5 4 0
5280 * +---+---+-------------------+--------+-------------+------+------+
5281 * | 0 | Q | 0 0 1 1 1 0 0 0 0 | imm5 | 0 0 0 0 1 1 | Rn | Rd |
5282 * +---+---+-------------------+--------+-------------+------+------+
5283 *
5284 * size: encoded in imm5 (see ARM ARM LowestSetBit())
5285 */
5286static void handle_simd_dupg(DisasContext *s, int is_q, int rd, int rn,
5287 int imm5)
5288{
5289 int size = ctz32(imm5);
5290 int esize = 8 << size;
5291 int elements = (is_q ? 128 : 64)/esize;
5292 int i = 0;
5293
5294 if (size > 3 || ((size == 3) && !is_q)) {
5295 unallocated_encoding(s);
5296 return;
5297 }
5298 for (i = 0; i < elements; i++) {
5299 write_vec_element(s, cpu_reg(s, rn), rd, i, size);
5300 }
5301 if (!is_q) {
5302 clear_vec_high(s, rd);
5303 }
5304}
5305
5306/* C6.3.150 INS (Element)
5307 *
5308 * 31 21 20 16 15 14 11 10 9 5 4 0
5309 * +-----------------------+--------+------------+---+------+------+
5310 * | 0 1 1 0 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 | Rn | Rd |
5311 * +-----------------------+--------+------------+---+------+------+
5312 *
5313 * size: encoded in imm5 (see ARM ARM LowestSetBit())
5314 * index: encoded in imm5<4:size+1>
5315 */
5316static void handle_simd_inse(DisasContext *s, int rd, int rn,
5317 int imm4, int imm5)
5318{
5319 int size = ctz32(imm5);
5320 int src_index, dst_index;
5321 TCGv_i64 tmp;
5322
5323 if (size > 3) {
5324 unallocated_encoding(s);
5325 return;
5326 }
5327 dst_index = extract32(imm5, 1+size, 5);
5328 src_index = extract32(imm4, size, 4);
5329
5330 tmp = tcg_temp_new_i64();
5331
5332 read_vec_element(s, tmp, rn, src_index, size);
5333 write_vec_element(s, tmp, rd, dst_index, size);
5334
5335 tcg_temp_free_i64(tmp);
5336}
5337
5338
5339/* C6.3.151 INS (General)
5340 *
5341 * 31 21 20 16 15 10 9 5 4 0
5342 * +-----------------------+--------+-------------+------+------+
5343 * | 0 1 0 0 1 1 1 0 0 0 0 | imm5 | 0 0 0 1 1 1 | Rn | Rd |
5344 * +-----------------------+--------+-------------+------+------+
5345 *
5346 * size: encoded in imm5 (see ARM ARM LowestSetBit())
5347 * index: encoded in imm5<4:size+1>
5348 */
5349static void handle_simd_insg(DisasContext *s, int rd, int rn, int imm5)
5350{
5351 int size = ctz32(imm5);
5352 int idx;
5353
5354 if (size > 3) {
5355 unallocated_encoding(s);
5356 return;
5357 }
5358
5359 idx = extract32(imm5, 1 + size, 4 - size);
5360 write_vec_element(s, cpu_reg(s, rn), rd, idx, size);
5361}
5362
5363/*
5364 * C6.3.321 UMOV (General)
5365 * C6.3.237 SMOV (General)
5366 *
5367 * 31 30 29 21 20 16 15 12 10 9 5 4 0
5368 * +---+---+-------------------+--------+-------------+------+------+
5369 * | 0 | Q | 0 0 1 1 1 0 0 0 0 | imm5 | 0 0 1 U 1 1 | Rn | Rd |
5370 * +---+---+-------------------+--------+-------------+------+------+
5371 *
5372 * U: unsigned when set
5373 * size: encoded in imm5 (see ARM ARM LowestSetBit())
5374 */
5375static void handle_simd_umov_smov(DisasContext *s, int is_q, int is_signed,
5376 int rn, int rd, int imm5)
5377{
5378 int size = ctz32(imm5);
5379 int element;
5380 TCGv_i64 tcg_rd;
5381
5382 /* Check for UnallocatedEncodings */
5383 if (is_signed) {
5384 if (size > 2 || (size == 2 && !is_q)) {
5385 unallocated_encoding(s);
5386 return;
5387 }
5388 } else {
5389 if (size > 3
5390 || (size < 3 && is_q)
5391 || (size == 3 && !is_q)) {
5392 unallocated_encoding(s);
5393 return;
5394 }
5395 }
5396 element = extract32(imm5, 1+size, 4);
5397
5398 tcg_rd = cpu_reg(s, rd);
5399 read_vec_element(s, tcg_rd, rn, element, size | (is_signed ? MO_SIGN : 0));
5400 if (is_signed && !is_q) {
5401 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
5402 }
5403}
5404
Alex Bennée384b26f2014-01-31 14:47:30 +00005405/* C3.6.5 AdvSIMD copy
5406 * 31 30 29 28 21 20 16 15 14 11 10 9 5 4 0
5407 * +---+---+----+-----------------+------+---+------+---+------+------+
5408 * | 0 | Q | op | 0 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 | Rn | Rd |
5409 * +---+---+----+-----------------+------+---+------+---+------+------+
5410 */
5411static void disas_simd_copy(DisasContext *s, uint32_t insn)
5412{
Alex Bennée67bb9382014-01-31 14:47:31 +00005413 int rd = extract32(insn, 0, 5);
5414 int rn = extract32(insn, 5, 5);
5415 int imm4 = extract32(insn, 11, 4);
5416 int op = extract32(insn, 29, 1);
5417 int is_q = extract32(insn, 30, 1);
5418 int imm5 = extract32(insn, 16, 5);
5419
5420 if (op) {
5421 if (is_q) {
5422 /* INS (element) */
5423 handle_simd_inse(s, rd, rn, imm4, imm5);
5424 } else {
5425 unallocated_encoding(s);
5426 }
5427 } else {
5428 switch (imm4) {
5429 case 0:
5430 /* DUP (element - vector) */
5431 handle_simd_dupe(s, is_q, rd, rn, imm5);
5432 break;
5433 case 1:
5434 /* DUP (general) */
5435 handle_simd_dupg(s, is_q, rd, rn, imm5);
5436 break;
5437 case 3:
5438 if (is_q) {
5439 /* INS (general) */
5440 handle_simd_insg(s, rd, rn, imm5);
5441 } else {
5442 unallocated_encoding(s);
5443 }
5444 break;
5445 case 5:
5446 case 7:
5447 /* UMOV/SMOV (is_q indicates 32/64; imm4 indicates signedness) */
5448 handle_simd_umov_smov(s, is_q, (imm4 == 5), rn, rd, imm5);
5449 break;
5450 default:
5451 unallocated_encoding(s);
5452 break;
5453 }
5454 }
Alex Bennée384b26f2014-01-31 14:47:30 +00005455}
5456
5457/* C3.6.6 AdvSIMD modified immediate
5458 * 31 30 29 28 19 18 16 15 12 11 10 9 5 4 0
5459 * +---+---+----+---------------------+-----+-------+----+---+-------+------+
5460 * | 0 | Q | op | 0 1 1 1 1 0 0 0 0 0 | abc | cmode | o2 | 1 | defgh | Rd |
5461 * +---+---+----+---------------------+-----+-------+----+---+-------+------+
Alex Bennéef3f8c4f2014-01-31 14:47:32 +00005462 *
5463 * There are a number of operations that can be carried out here:
5464 * MOVI - move (shifted) imm into register
5465 * MVNI - move inverted (shifted) imm into register
5466 * ORR - bitwise OR of (shifted) imm with register
5467 * BIC - bitwise clear of (shifted) imm with register
Alex Bennée384b26f2014-01-31 14:47:30 +00005468 */
5469static void disas_simd_mod_imm(DisasContext *s, uint32_t insn)
5470{
Alex Bennéef3f8c4f2014-01-31 14:47:32 +00005471 int rd = extract32(insn, 0, 5);
5472 int cmode = extract32(insn, 12, 4);
5473 int cmode_3_1 = extract32(cmode, 1, 3);
5474 int cmode_0 = extract32(cmode, 0, 1);
5475 int o2 = extract32(insn, 11, 1);
5476 uint64_t abcdefgh = extract32(insn, 5, 5) | (extract32(insn, 16, 3) << 5);
5477 bool is_neg = extract32(insn, 29, 1);
5478 bool is_q = extract32(insn, 30, 1);
5479 uint64_t imm = 0;
5480 TCGv_i64 tcg_rd, tcg_imm;
5481 int i;
5482
5483 if (o2 != 0 || ((cmode == 0xf) && is_neg && !is_q)) {
5484 unallocated_encoding(s);
5485 return;
5486 }
5487
5488 /* See AdvSIMDExpandImm() in ARM ARM */
5489 switch (cmode_3_1) {
5490 case 0: /* Replicate(Zeros(24):imm8, 2) */
5491 case 1: /* Replicate(Zeros(16):imm8:Zeros(8), 2) */
5492 case 2: /* Replicate(Zeros(8):imm8:Zeros(16), 2) */
5493 case 3: /* Replicate(imm8:Zeros(24), 2) */
5494 {
5495 int shift = cmode_3_1 * 8;
5496 imm = bitfield_replicate(abcdefgh << shift, 32);
5497 break;
5498 }
5499 case 4: /* Replicate(Zeros(8):imm8, 4) */
5500 case 5: /* Replicate(imm8:Zeros(8), 4) */
5501 {
5502 int shift = (cmode_3_1 & 0x1) * 8;
5503 imm = bitfield_replicate(abcdefgh << shift, 16);
5504 break;
5505 }
5506 case 6:
5507 if (cmode_0) {
5508 /* Replicate(Zeros(8):imm8:Ones(16), 2) */
5509 imm = (abcdefgh << 16) | 0xffff;
5510 } else {
5511 /* Replicate(Zeros(16):imm8:Ones(8), 2) */
5512 imm = (abcdefgh << 8) | 0xff;
5513 }
5514 imm = bitfield_replicate(imm, 32);
5515 break;
5516 case 7:
5517 if (!cmode_0 && !is_neg) {
5518 imm = bitfield_replicate(abcdefgh, 8);
5519 } else if (!cmode_0 && is_neg) {
5520 int i;
5521 imm = 0;
5522 for (i = 0; i < 8; i++) {
5523 if ((abcdefgh) & (1 << i)) {
5524 imm |= 0xffULL << (i * 8);
5525 }
5526 }
5527 } else if (cmode_0) {
5528 if (is_neg) {
5529 imm = (abcdefgh & 0x3f) << 48;
5530 if (abcdefgh & 0x80) {
5531 imm |= 0x8000000000000000ULL;
5532 }
5533 if (abcdefgh & 0x40) {
5534 imm |= 0x3fc0000000000000ULL;
5535 } else {
5536 imm |= 0x4000000000000000ULL;
5537 }
5538 } else {
5539 imm = (abcdefgh & 0x3f) << 19;
5540 if (abcdefgh & 0x80) {
5541 imm |= 0x80000000;
5542 }
5543 if (abcdefgh & 0x40) {
5544 imm |= 0x3e000000;
5545 } else {
5546 imm |= 0x40000000;
5547 }
5548 imm |= (imm << 32);
5549 }
5550 }
5551 break;
5552 }
5553
5554 if (cmode_3_1 != 7 && is_neg) {
5555 imm = ~imm;
5556 }
5557
5558 tcg_imm = tcg_const_i64(imm);
5559 tcg_rd = new_tmp_a64(s);
5560
5561 for (i = 0; i < 2; i++) {
5562 int foffs = i ? fp_reg_hi_offset(rd) : fp_reg_offset(rd, MO_64);
5563
5564 if (i == 1 && !is_q) {
5565 /* non-quad ops clear high half of vector */
5566 tcg_gen_movi_i64(tcg_rd, 0);
5567 } else if ((cmode & 0x9) == 0x1 || (cmode & 0xd) == 0x9) {
5568 tcg_gen_ld_i64(tcg_rd, cpu_env, foffs);
5569 if (is_neg) {
5570 /* AND (BIC) */
5571 tcg_gen_and_i64(tcg_rd, tcg_rd, tcg_imm);
5572 } else {
5573 /* ORR */
5574 tcg_gen_or_i64(tcg_rd, tcg_rd, tcg_imm);
5575 }
5576 } else {
5577 /* MOVI */
5578 tcg_gen_mov_i64(tcg_rd, tcg_imm);
5579 }
5580 tcg_gen_st_i64(tcg_rd, cpu_env, foffs);
5581 }
5582
5583 tcg_temp_free_i64(tcg_imm);
Alex Bennée384b26f2014-01-31 14:47:30 +00005584}
5585
5586/* C3.6.7 AdvSIMD scalar copy
5587 * 31 30 29 28 21 20 16 15 14 11 10 9 5 4 0
5588 * +-----+----+-----------------+------+---+------+---+------+------+
5589 * | 0 1 | op | 1 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 | Rn | Rd |
5590 * +-----+----+-----------------+------+---+------+---+------+------+
5591 */
5592static void disas_simd_scalar_copy(DisasContext *s, uint32_t insn)
5593{
Peter Maydell360a6f22014-01-31 14:47:32 +00005594 int rd = extract32(insn, 0, 5);
5595 int rn = extract32(insn, 5, 5);
5596 int imm4 = extract32(insn, 11, 4);
5597 int imm5 = extract32(insn, 16, 5);
5598 int op = extract32(insn, 29, 1);
5599
5600 if (op != 0 || imm4 != 0) {
5601 unallocated_encoding(s);
5602 return;
5603 }
5604
5605 /* DUP (element, scalar) */
5606 handle_simd_dupes(s, rd, rn, imm5);
Alex Bennée384b26f2014-01-31 14:47:30 +00005607}
5608
5609/* C3.6.8 AdvSIMD scalar pairwise
5610 * 31 30 29 28 24 23 22 21 17 16 12 11 10 9 5 4 0
5611 * +-----+---+-----------+------+-----------+--------+-----+------+------+
5612 * | 0 1 | U | 1 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 | Rn | Rd |
5613 * +-----+---+-----------+------+-----------+--------+-----+------+------+
5614 */
5615static void disas_simd_scalar_pairwise(DisasContext *s, uint32_t insn)
5616{
Peter Maydell3720a7e2014-02-08 14:46:56 +00005617 int u = extract32(insn, 29, 1);
5618 int size = extract32(insn, 22, 2);
5619 int opcode = extract32(insn, 12, 5);
5620 int rn = extract32(insn, 5, 5);
5621 int rd = extract32(insn, 0, 5);
5622 TCGv_ptr fpst;
5623
5624 /* For some ops (the FP ones), size[1] is part of the encoding.
5625 * For ADDP strictly it is not but size[1] is always 1 for valid
5626 * encodings.
5627 */
5628 opcode |= (extract32(size, 1, 1) << 5);
5629
5630 switch (opcode) {
5631 case 0x3b: /* ADDP */
5632 if (u || size != 3) {
5633 unallocated_encoding(s);
5634 return;
5635 }
5636 TCGV_UNUSED_PTR(fpst);
5637 break;
5638 case 0xc: /* FMAXNMP */
5639 case 0xd: /* FADDP */
5640 case 0xf: /* FMAXP */
5641 case 0x2c: /* FMINNMP */
5642 case 0x2f: /* FMINP */
5643 /* FP op, size[0] is 32 or 64 bit */
5644 if (!u) {
5645 unallocated_encoding(s);
5646 return;
5647 }
5648 size = extract32(size, 0, 1) ? 3 : 2;
5649 fpst = get_fpstatus_ptr();
5650 break;
5651 default:
5652 unallocated_encoding(s);
5653 return;
5654 }
5655
5656 if (size == 3) {
5657 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
5658 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
5659 TCGv_i64 tcg_res = tcg_temp_new_i64();
5660
5661 read_vec_element(s, tcg_op1, rn, 0, MO_64);
5662 read_vec_element(s, tcg_op2, rn, 1, MO_64);
5663
5664 switch (opcode) {
5665 case 0x3b: /* ADDP */
5666 tcg_gen_add_i64(tcg_res, tcg_op1, tcg_op2);
5667 break;
5668 case 0xc: /* FMAXNMP */
5669 gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
5670 break;
5671 case 0xd: /* FADDP */
5672 gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
5673 break;
5674 case 0xf: /* FMAXP */
5675 gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
5676 break;
5677 case 0x2c: /* FMINNMP */
5678 gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
5679 break;
5680 case 0x2f: /* FMINP */
5681 gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
5682 break;
5683 default:
5684 g_assert_not_reached();
5685 }
5686
5687 write_fp_dreg(s, rd, tcg_res);
5688
5689 tcg_temp_free_i64(tcg_op1);
5690 tcg_temp_free_i64(tcg_op2);
5691 tcg_temp_free_i64(tcg_res);
5692 } else {
5693 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
5694 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
5695 TCGv_i32 tcg_res = tcg_temp_new_i32();
5696
5697 read_vec_element_i32(s, tcg_op1, rn, 0, MO_32);
5698 read_vec_element_i32(s, tcg_op2, rn, 1, MO_32);
5699
5700 switch (opcode) {
5701 case 0xc: /* FMAXNMP */
5702 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
5703 break;
5704 case 0xd: /* FADDP */
5705 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
5706 break;
5707 case 0xf: /* FMAXP */
5708 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
5709 break;
5710 case 0x2c: /* FMINNMP */
5711 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
5712 break;
5713 case 0x2f: /* FMINP */
5714 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
5715 break;
5716 default:
5717 g_assert_not_reached();
5718 }
5719
5720 write_fp_sreg(s, rd, tcg_res);
5721
5722 tcg_temp_free_i32(tcg_op1);
5723 tcg_temp_free_i32(tcg_op2);
5724 tcg_temp_free_i32(tcg_res);
5725 }
5726
5727 if (!TCGV_IS_UNUSED_PTR(fpst)) {
5728 tcg_temp_free_ptr(fpst);
5729 }
Alex Bennée384b26f2014-01-31 14:47:30 +00005730}
5731
Alex Bennée4d1cef82014-01-31 14:47:37 +00005732/*
5733 * Common SSHR[RA]/USHR[RA] - Shift right (optional rounding/accumulate)
5734 *
5735 * This code is handles the common shifting code and is used by both
5736 * the vector and scalar code.
5737 */
5738static void handle_shri_with_rndacc(TCGv_i64 tcg_res, TCGv_i64 tcg_src,
5739 TCGv_i64 tcg_rnd, bool accumulate,
5740 bool is_u, int size, int shift)
5741{
5742 bool extended_result = false;
5743 bool round = !TCGV_IS_UNUSED_I64(tcg_rnd);
5744 int ext_lshift = 0;
5745 TCGv_i64 tcg_src_hi;
5746
5747 if (round && size == 3) {
5748 extended_result = true;
5749 ext_lshift = 64 - shift;
5750 tcg_src_hi = tcg_temp_new_i64();
5751 } else if (shift == 64) {
5752 if (!accumulate && is_u) {
5753 /* result is zero */
5754 tcg_gen_movi_i64(tcg_res, 0);
5755 return;
5756 }
5757 }
5758
5759 /* Deal with the rounding step */
5760 if (round) {
5761 if (extended_result) {
5762 TCGv_i64 tcg_zero = tcg_const_i64(0);
5763 if (!is_u) {
5764 /* take care of sign extending tcg_res */
5765 tcg_gen_sari_i64(tcg_src_hi, tcg_src, 63);
5766 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
5767 tcg_src, tcg_src_hi,
5768 tcg_rnd, tcg_zero);
5769 } else {
5770 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
5771 tcg_src, tcg_zero,
5772 tcg_rnd, tcg_zero);
5773 }
5774 tcg_temp_free_i64(tcg_zero);
5775 } else {
5776 tcg_gen_add_i64(tcg_src, tcg_src, tcg_rnd);
5777 }
5778 }
5779
5780 /* Now do the shift right */
5781 if (round && extended_result) {
5782 /* extended case, >64 bit precision required */
5783 if (ext_lshift == 0) {
5784 /* special case, only high bits matter */
5785 tcg_gen_mov_i64(tcg_src, tcg_src_hi);
5786 } else {
5787 tcg_gen_shri_i64(tcg_src, tcg_src, shift);
5788 tcg_gen_shli_i64(tcg_src_hi, tcg_src_hi, ext_lshift);
5789 tcg_gen_or_i64(tcg_src, tcg_src, tcg_src_hi);
5790 }
5791 } else {
5792 if (is_u) {
5793 if (shift == 64) {
5794 /* essentially shifting in 64 zeros */
5795 tcg_gen_movi_i64(tcg_src, 0);
5796 } else {
5797 tcg_gen_shri_i64(tcg_src, tcg_src, shift);
5798 }
5799 } else {
5800 if (shift == 64) {
5801 /* effectively extending the sign-bit */
5802 tcg_gen_sari_i64(tcg_src, tcg_src, 63);
5803 } else {
5804 tcg_gen_sari_i64(tcg_src, tcg_src, shift);
5805 }
5806 }
5807 }
5808
5809 if (accumulate) {
5810 tcg_gen_add_i64(tcg_res, tcg_res, tcg_src);
5811 } else {
5812 tcg_gen_mov_i64(tcg_res, tcg_src);
5813 }
5814
5815 if (extended_result) {
5816 tcg_temp_free_i64(tcg_src_hi);
5817 }
5818}
5819
5820/* Common SHL/SLI - Shift left with an optional insert */
5821static void handle_shli_with_ins(TCGv_i64 tcg_res, TCGv_i64 tcg_src,
5822 bool insert, int shift)
5823{
5824 if (insert) { /* SLI */
5825 tcg_gen_deposit_i64(tcg_res, tcg_res, tcg_src, shift, 64 - shift);
5826 } else { /* SHL */
5827 tcg_gen_shli_i64(tcg_res, tcg_src, shift);
5828 }
5829}
5830
5831/* SSHR[RA]/USHR[RA] - Scalar shift right (optional rounding/accumulate) */
5832static void handle_scalar_simd_shri(DisasContext *s,
5833 bool is_u, int immh, int immb,
5834 int opcode, int rn, int rd)
5835{
5836 const int size = 3;
5837 int immhb = immh << 3 | immb;
5838 int shift = 2 * (8 << size) - immhb;
5839 bool accumulate = false;
5840 bool round = false;
5841 TCGv_i64 tcg_rn;
5842 TCGv_i64 tcg_rd;
5843 TCGv_i64 tcg_round;
5844
5845 if (!extract32(immh, 3, 1)) {
5846 unallocated_encoding(s);
5847 return;
5848 }
5849
5850 switch (opcode) {
5851 case 0x02: /* SSRA / USRA (accumulate) */
5852 accumulate = true;
5853 break;
5854 case 0x04: /* SRSHR / URSHR (rounding) */
5855 round = true;
5856 break;
5857 case 0x06: /* SRSRA / URSRA (accum + rounding) */
5858 accumulate = round = true;
5859 break;
5860 }
5861
5862 if (round) {
5863 uint64_t round_const = 1ULL << (shift - 1);
5864 tcg_round = tcg_const_i64(round_const);
5865 } else {
5866 TCGV_UNUSED_I64(tcg_round);
5867 }
5868
5869 tcg_rn = read_fp_dreg(s, rn);
5870 tcg_rd = accumulate ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
5871
5872 handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
5873 accumulate, is_u, size, shift);
5874
5875 write_fp_dreg(s, rd, tcg_rd);
5876
5877 tcg_temp_free_i64(tcg_rn);
5878 tcg_temp_free_i64(tcg_rd);
5879 if (round) {
5880 tcg_temp_free_i64(tcg_round);
5881 }
5882}
5883
5884/* SHL/SLI - Scalar shift left */
5885static void handle_scalar_simd_shli(DisasContext *s, bool insert,
5886 int immh, int immb, int opcode,
5887 int rn, int rd)
5888{
5889 int size = 32 - clz32(immh) - 1;
5890 int immhb = immh << 3 | immb;
5891 int shift = immhb - (8 << size);
5892 TCGv_i64 tcg_rn = new_tmp_a64(s);
5893 TCGv_i64 tcg_rd = new_tmp_a64(s);
5894
5895 if (!extract32(immh, 3, 1)) {
5896 unallocated_encoding(s);
5897 return;
5898 }
5899
5900 tcg_rn = read_fp_dreg(s, rn);
5901 tcg_rd = insert ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
5902
5903 handle_shli_with_ins(tcg_rd, tcg_rn, insert, shift);
5904
5905 write_fp_dreg(s, rd, tcg_rd);
5906
5907 tcg_temp_free_i64(tcg_rn);
5908 tcg_temp_free_i64(tcg_rd);
5909}
5910
Peter Maydellc3882982014-03-08 21:56:55 +00005911/* SQSHRN/SQSHRUN - Saturating (signed/unsigned) shift right with
5912 * (signed/unsigned) narrowing */
5913static void handle_vec_simd_sqshrn(DisasContext *s, bool is_scalar, bool is_q,
5914 bool is_u_shift, bool is_u_narrow,
5915 int immh, int immb, int opcode,
5916 int rn, int rd)
5917{
5918 int immhb = immh << 3 | immb;
5919 int size = 32 - clz32(immh) - 1;
5920 int esize = 8 << size;
5921 int shift = (2 * esize) - immhb;
5922 int elements = is_scalar ? 1 : (64 / esize);
5923 bool round = extract32(opcode, 0, 1);
5924 TCGMemOp ldop = (size + 1) | (is_u_shift ? 0 : MO_SIGN);
5925 TCGv_i64 tcg_rn, tcg_rd, tcg_round;
5926 TCGv_i32 tcg_rd_narrowed;
5927 TCGv_i64 tcg_final;
5928
5929 static NeonGenNarrowEnvFn * const signed_narrow_fns[4][2] = {
5930 { gen_helper_neon_narrow_sat_s8,
5931 gen_helper_neon_unarrow_sat8 },
5932 { gen_helper_neon_narrow_sat_s16,
5933 gen_helper_neon_unarrow_sat16 },
5934 { gen_helper_neon_narrow_sat_s32,
5935 gen_helper_neon_unarrow_sat32 },
5936 { NULL, NULL },
5937 };
5938 static NeonGenNarrowEnvFn * const unsigned_narrow_fns[4] = {
5939 gen_helper_neon_narrow_sat_u8,
5940 gen_helper_neon_narrow_sat_u16,
5941 gen_helper_neon_narrow_sat_u32,
5942 NULL
5943 };
5944 NeonGenNarrowEnvFn *narrowfn;
5945
5946 int i;
5947
5948 assert(size < 4);
5949
5950 if (extract32(immh, 3, 1)) {
5951 unallocated_encoding(s);
5952 return;
5953 }
5954
5955 if (is_u_shift) {
5956 narrowfn = unsigned_narrow_fns[size];
5957 } else {
5958 narrowfn = signed_narrow_fns[size][is_u_narrow ? 1 : 0];
5959 }
5960
5961 tcg_rn = tcg_temp_new_i64();
5962 tcg_rd = tcg_temp_new_i64();
5963 tcg_rd_narrowed = tcg_temp_new_i32();
5964 tcg_final = tcg_const_i64(0);
5965
5966 if (round) {
5967 uint64_t round_const = 1ULL << (shift - 1);
5968 tcg_round = tcg_const_i64(round_const);
5969 } else {
5970 TCGV_UNUSED_I64(tcg_round);
5971 }
5972
5973 for (i = 0; i < elements; i++) {
5974 read_vec_element(s, tcg_rn, rn, i, ldop);
5975 handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
5976 false, is_u_shift, size+1, shift);
5977 narrowfn(tcg_rd_narrowed, cpu_env, tcg_rd);
5978 tcg_gen_extu_i32_i64(tcg_rd, tcg_rd_narrowed);
5979 tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
5980 }
5981
5982 if (!is_q) {
5983 clear_vec_high(s, rd);
5984 write_vec_element(s, tcg_final, rd, 0, MO_64);
5985 } else {
5986 write_vec_element(s, tcg_final, rd, 1, MO_64);
5987 }
5988
5989 if (round) {
5990 tcg_temp_free_i64(tcg_round);
5991 }
5992 tcg_temp_free_i64(tcg_rn);
5993 tcg_temp_free_i64(tcg_rd);
5994 tcg_temp_free_i32(tcg_rd_narrowed);
5995 tcg_temp_free_i64(tcg_final);
5996 return;
5997}
5998
Peter Maydell745849b2014-03-08 20:40:20 +00005999/* Common vector code for handling integer to FP conversion */
6000static void handle_simd_intfp_conv(DisasContext *s, int rd, int rn,
6001 int elements, int is_signed,
6002 int fracbits, int size)
6003{
6004 bool is_double = size == 3 ? true : false;
6005 TCGv_ptr tcg_fpst = get_fpstatus_ptr();
6006 TCGv_i32 tcg_shift = tcg_const_i32(fracbits);
6007 TCGv_i64 tcg_int = tcg_temp_new_i64();
6008 TCGMemOp mop = size | (is_signed ? MO_SIGN : 0);
6009 int pass;
6010
6011 for (pass = 0; pass < elements; pass++) {
6012 read_vec_element(s, tcg_int, rn, pass, mop);
6013
6014 if (is_double) {
6015 TCGv_i64 tcg_double = tcg_temp_new_i64();
6016 if (is_signed) {
6017 gen_helper_vfp_sqtod(tcg_double, tcg_int,
6018 tcg_shift, tcg_fpst);
6019 } else {
6020 gen_helper_vfp_uqtod(tcg_double, tcg_int,
6021 tcg_shift, tcg_fpst);
6022 }
6023 if (elements == 1) {
6024 write_fp_dreg(s, rd, tcg_double);
6025 } else {
6026 write_vec_element(s, tcg_double, rd, pass, MO_64);
6027 }
6028 tcg_temp_free_i64(tcg_double);
6029 } else {
6030 TCGv_i32 tcg_single = tcg_temp_new_i32();
6031 if (is_signed) {
6032 gen_helper_vfp_sqtos(tcg_single, tcg_int,
6033 tcg_shift, tcg_fpst);
6034 } else {
6035 gen_helper_vfp_uqtos(tcg_single, tcg_int,
6036 tcg_shift, tcg_fpst);
6037 }
6038 if (elements == 1) {
6039 write_fp_sreg(s, rd, tcg_single);
6040 } else {
6041 write_vec_element_i32(s, tcg_single, rd, pass, MO_32);
6042 }
6043 tcg_temp_free_i32(tcg_single);
6044 }
6045 }
6046
6047 if (!is_double && elements == 2) {
6048 clear_vec_high(s, rd);
6049 }
6050
6051 tcg_temp_free_i64(tcg_int);
6052 tcg_temp_free_ptr(tcg_fpst);
6053 tcg_temp_free_i32(tcg_shift);
6054}
6055
6056/* UCVTF/SCVTF - Integer to FP conversion */
6057static void handle_simd_shift_intfp_conv(DisasContext *s, bool is_scalar,
6058 bool is_q, bool is_u,
6059 int immh, int immb, int opcode,
6060 int rn, int rd)
6061{
6062 bool is_double = extract32(immh, 3, 1);
6063 int size = is_double ? MO_64 : MO_32;
6064 int elements;
6065 int immhb = immh << 3 | immb;
6066 int fracbits = (is_double ? 128 : 64) - immhb;
6067
6068 if (!extract32(immh, 2, 2)) {
6069 unallocated_encoding(s);
6070 return;
6071 }
6072
6073 if (is_scalar) {
6074 elements = 1;
6075 } else {
6076 elements = is_double ? 2 : is_q ? 4 : 2;
6077 if (is_double && !is_q) {
6078 unallocated_encoding(s);
6079 return;
6080 }
6081 }
6082 /* immh == 0 would be a failure of the decode logic */
6083 g_assert(immh);
6084
6085 handle_simd_intfp_conv(s, rd, rn, elements, !is_u, fracbits, size);
6086}
6087
Alex Bennée384b26f2014-01-31 14:47:30 +00006088/* C3.6.9 AdvSIMD scalar shift by immediate
6089 * 31 30 29 28 23 22 19 18 16 15 11 10 9 5 4 0
6090 * +-----+---+-------------+------+------+--------+---+------+------+
6091 * | 0 1 | U | 1 1 1 1 1 0 | immh | immb | opcode | 1 | Rn | Rd |
6092 * +-----+---+-------------+------+------+--------+---+------+------+
Alex Bennée4d1cef82014-01-31 14:47:37 +00006093 *
6094 * This is the scalar version so it works on a fixed sized registers
Alex Bennée384b26f2014-01-31 14:47:30 +00006095 */
6096static void disas_simd_scalar_shift_imm(DisasContext *s, uint32_t insn)
6097{
Alex Bennée4d1cef82014-01-31 14:47:37 +00006098 int rd = extract32(insn, 0, 5);
6099 int rn = extract32(insn, 5, 5);
6100 int opcode = extract32(insn, 11, 5);
6101 int immb = extract32(insn, 16, 3);
6102 int immh = extract32(insn, 19, 4);
6103 bool is_u = extract32(insn, 29, 1);
6104
Peter Maydellc3882982014-03-08 21:56:55 +00006105 if (immh == 0) {
6106 unallocated_encoding(s);
6107 return;
6108 }
6109
Alex Bennée4d1cef82014-01-31 14:47:37 +00006110 switch (opcode) {
6111 case 0x00: /* SSHR / USHR */
6112 case 0x02: /* SSRA / USRA */
6113 case 0x04: /* SRSHR / URSHR */
6114 case 0x06: /* SRSRA / URSRA */
6115 handle_scalar_simd_shri(s, is_u, immh, immb, opcode, rn, rd);
6116 break;
6117 case 0x0a: /* SHL / SLI */
6118 handle_scalar_simd_shli(s, is_u, immh, immb, opcode, rn, rd);
6119 break;
Peter Maydell745849b2014-03-08 20:40:20 +00006120 case 0x1c: /* SCVTF, UCVTF */
6121 handle_simd_shift_intfp_conv(s, true, false, is_u, immh, immb,
6122 opcode, rn, rd);
6123 break;
Peter Maydellc3882982014-03-08 21:56:55 +00006124 case 0x10: /* SQSHRUN, SQSHRUN2 */
6125 case 0x11: /* SQRSHRUN, SQRSHRUN2 */
6126 if (!is_u) {
6127 unallocated_encoding(s);
6128 return;
6129 }
6130 handle_vec_simd_sqshrn(s, true, false, false, true,
6131 immh, immb, opcode, rn, rd);
6132 break;
6133 case 0x12: /* SQSHRN, SQSHRN2, UQSHRN */
6134 case 0x13: /* SQRSHRN, SQRSHRN2, UQRSHRN, UQRSHRN2 */
6135 handle_vec_simd_sqshrn(s, true, false, is_u, is_u,
6136 immh, immb, opcode, rn, rd);
6137 break;
Alex Bennée4d1cef82014-01-31 14:47:37 +00006138 default:
6139 unsupported_encoding(s, insn);
6140 break;
6141 }
Alex Bennée384b26f2014-01-31 14:47:30 +00006142}
6143
6144/* C3.6.10 AdvSIMD scalar three different
6145 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 0
6146 * +-----+---+-----------+------+---+------+--------+-----+------+------+
6147 * | 0 1 | U | 1 1 1 1 0 | size | 1 | Rm | opcode | 0 0 | Rn | Rd |
6148 * +-----+---+-----------+------+---+------+--------+-----+------+------+
6149 */
6150static void disas_simd_scalar_three_reg_diff(DisasContext *s, uint32_t insn)
6151{
Peter Maydellb033cd32014-02-20 10:35:49 +00006152 bool is_u = extract32(insn, 29, 1);
6153 int size = extract32(insn, 22, 2);
6154 int opcode = extract32(insn, 12, 4);
6155 int rm = extract32(insn, 16, 5);
6156 int rn = extract32(insn, 5, 5);
6157 int rd = extract32(insn, 0, 5);
6158
6159 if (is_u) {
6160 unallocated_encoding(s);
6161 return;
6162 }
6163
6164 switch (opcode) {
6165 case 0x9: /* SQDMLAL, SQDMLAL2 */
6166 case 0xb: /* SQDMLSL, SQDMLSL2 */
6167 case 0xd: /* SQDMULL, SQDMULL2 */
6168 if (size == 0 || size == 3) {
6169 unallocated_encoding(s);
6170 return;
6171 }
6172 break;
6173 default:
6174 unallocated_encoding(s);
6175 return;
6176 }
6177
6178 if (size == 2) {
6179 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
6180 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
6181 TCGv_i64 tcg_res = tcg_temp_new_i64();
6182
6183 read_vec_element(s, tcg_op1, rn, 0, MO_32 | MO_SIGN);
6184 read_vec_element(s, tcg_op2, rm, 0, MO_32 | MO_SIGN);
6185
6186 tcg_gen_mul_i64(tcg_res, tcg_op1, tcg_op2);
6187 gen_helper_neon_addl_saturate_s64(tcg_res, cpu_env, tcg_res, tcg_res);
6188
6189 switch (opcode) {
6190 case 0xd: /* SQDMULL, SQDMULL2 */
6191 break;
6192 case 0xb: /* SQDMLSL, SQDMLSL2 */
6193 tcg_gen_neg_i64(tcg_res, tcg_res);
6194 /* fall through */
6195 case 0x9: /* SQDMLAL, SQDMLAL2 */
6196 read_vec_element(s, tcg_op1, rd, 0, MO_64);
6197 gen_helper_neon_addl_saturate_s64(tcg_res, cpu_env,
6198 tcg_res, tcg_op1);
6199 break;
6200 default:
6201 g_assert_not_reached();
6202 }
6203
6204 write_fp_dreg(s, rd, tcg_res);
6205
6206 tcg_temp_free_i64(tcg_op1);
6207 tcg_temp_free_i64(tcg_op2);
6208 tcg_temp_free_i64(tcg_res);
6209 } else {
6210 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
6211 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
6212 TCGv_i64 tcg_res = tcg_temp_new_i64();
6213
6214 read_vec_element_i32(s, tcg_op1, rn, 0, MO_16);
6215 read_vec_element_i32(s, tcg_op2, rm, 0, MO_16);
6216
6217 gen_helper_neon_mull_s16(tcg_res, tcg_op1, tcg_op2);
6218 gen_helper_neon_addl_saturate_s32(tcg_res, cpu_env, tcg_res, tcg_res);
6219
6220 switch (opcode) {
6221 case 0xd: /* SQDMULL, SQDMULL2 */
6222 break;
6223 case 0xb: /* SQDMLSL, SQDMLSL2 */
6224 gen_helper_neon_negl_u32(tcg_res, tcg_res);
6225 /* fall through */
6226 case 0x9: /* SQDMLAL, SQDMLAL2 */
6227 {
6228 TCGv_i64 tcg_op3 = tcg_temp_new_i64();
6229 read_vec_element(s, tcg_op3, rd, 0, MO_32);
6230 gen_helper_neon_addl_saturate_s32(tcg_res, cpu_env,
6231 tcg_res, tcg_op3);
6232 tcg_temp_free_i64(tcg_op3);
6233 break;
6234 }
6235 default:
6236 g_assert_not_reached();
6237 }
6238
6239 tcg_gen_ext32u_i64(tcg_res, tcg_res);
6240 write_fp_dreg(s, rd, tcg_res);
6241
6242 tcg_temp_free_i32(tcg_op1);
6243 tcg_temp_free_i32(tcg_op2);
6244 tcg_temp_free_i64(tcg_res);
6245 }
Alex Bennée384b26f2014-01-31 14:47:30 +00006246}
6247
Peter Maydellb305dba2014-01-31 14:47:36 +00006248static void handle_3same_64(DisasContext *s, int opcode, bool u,
6249 TCGv_i64 tcg_rd, TCGv_i64 tcg_rn, TCGv_i64 tcg_rm)
6250{
6251 /* Handle 64x64->64 opcodes which are shared between the scalar
6252 * and vector 3-same groups. We cover every opcode where size == 3
6253 * is valid in either the three-reg-same (integer, not pairwise)
6254 * or scalar-three-reg-same groups. (Some opcodes are not yet
6255 * implemented.)
6256 */
6257 TCGCond cond;
6258
6259 switch (opcode) {
Peter Maydell6d9571f2014-02-08 14:46:55 +00006260 case 0x1: /* SQADD */
6261 if (u) {
6262 gen_helper_neon_qadd_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
6263 } else {
6264 gen_helper_neon_qadd_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
6265 }
6266 break;
6267 case 0x5: /* SQSUB */
6268 if (u) {
6269 gen_helper_neon_qsub_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
6270 } else {
6271 gen_helper_neon_qsub_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
6272 }
6273 break;
Peter Maydellb305dba2014-01-31 14:47:36 +00006274 case 0x6: /* CMGT, CMHI */
6275 /* 64 bit integer comparison, result = test ? (2^64 - 1) : 0.
6276 * We implement this using setcond (test) and then negating.
6277 */
6278 cond = u ? TCG_COND_GTU : TCG_COND_GT;
6279 do_cmop:
6280 tcg_gen_setcond_i64(cond, tcg_rd, tcg_rn, tcg_rm);
6281 tcg_gen_neg_i64(tcg_rd, tcg_rd);
6282 break;
6283 case 0x7: /* CMGE, CMHS */
6284 cond = u ? TCG_COND_GEU : TCG_COND_GE;
6285 goto do_cmop;
6286 case 0x11: /* CMTST, CMEQ */
6287 if (u) {
6288 cond = TCG_COND_EQ;
6289 goto do_cmop;
6290 }
6291 /* CMTST : test is "if (X & Y != 0)". */
6292 tcg_gen_and_i64(tcg_rd, tcg_rn, tcg_rm);
6293 tcg_gen_setcondi_i64(TCG_COND_NE, tcg_rd, tcg_rd, 0);
6294 tcg_gen_neg_i64(tcg_rd, tcg_rd);
6295 break;
Peter Maydell6d9571f2014-02-08 14:46:55 +00006296 case 0x8: /* SSHL, USHL */
6297 if (u) {
6298 gen_helper_neon_shl_u64(tcg_rd, tcg_rn, tcg_rm);
6299 } else {
6300 gen_helper_neon_shl_s64(tcg_rd, tcg_rn, tcg_rm);
6301 }
6302 break;
6303 case 0x9: /* SQSHL, UQSHL */
6304 if (u) {
6305 gen_helper_neon_qshl_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
6306 } else {
6307 gen_helper_neon_qshl_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
6308 }
6309 break;
6310 case 0xa: /* SRSHL, URSHL */
6311 if (u) {
6312 gen_helper_neon_rshl_u64(tcg_rd, tcg_rn, tcg_rm);
6313 } else {
6314 gen_helper_neon_rshl_s64(tcg_rd, tcg_rn, tcg_rm);
6315 }
6316 break;
6317 case 0xb: /* SQRSHL, UQRSHL */
6318 if (u) {
6319 gen_helper_neon_qrshl_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
6320 } else {
6321 gen_helper_neon_qrshl_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
6322 }
6323 break;
Peter Maydellb305dba2014-01-31 14:47:36 +00006324 case 0x10: /* ADD, SUB */
6325 if (u) {
6326 tcg_gen_sub_i64(tcg_rd, tcg_rn, tcg_rm);
6327 } else {
6328 tcg_gen_add_i64(tcg_rd, tcg_rn, tcg_rm);
6329 }
6330 break;
Peter Maydellb305dba2014-01-31 14:47:36 +00006331 default:
6332 g_assert_not_reached();
6333 }
6334}
6335
Peter Maydell845ea092014-01-31 14:47:37 +00006336/* Handle the 3-same-operands float operations; shared by the scalar
6337 * and vector encodings. The caller must filter out any encodings
6338 * not allocated for the encoding it is dealing with.
6339 */
6340static void handle_3same_float(DisasContext *s, int size, int elements,
6341 int fpopcode, int rd, int rn, int rm)
6342{
6343 int pass;
6344 TCGv_ptr fpst = get_fpstatus_ptr();
6345
6346 for (pass = 0; pass < elements; pass++) {
6347 if (size) {
6348 /* Double */
6349 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
6350 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
6351 TCGv_i64 tcg_res = tcg_temp_new_i64();
6352
6353 read_vec_element(s, tcg_op1, rn, pass, MO_64);
6354 read_vec_element(s, tcg_op2, rm, pass, MO_64);
6355
6356 switch (fpopcode) {
Peter Maydell057d5f62014-02-20 10:35:50 +00006357 case 0x39: /* FMLS */
6358 /* As usual for ARM, separate negation for fused multiply-add */
6359 gen_helper_vfp_negd(tcg_op1, tcg_op1);
6360 /* fall through */
6361 case 0x19: /* FMLA */
6362 read_vec_element(s, tcg_res, rd, pass, MO_64);
6363 gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2,
6364 tcg_res, fpst);
6365 break;
Peter Maydell845ea092014-01-31 14:47:37 +00006366 case 0x18: /* FMAXNM */
6367 gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
6368 break;
6369 case 0x1a: /* FADD */
6370 gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
6371 break;
Peter Maydell057d5f62014-02-20 10:35:50 +00006372 case 0x1b: /* FMULX */
6373 gen_helper_vfp_mulxd(tcg_res, tcg_op1, tcg_op2, fpst);
6374 break;
Alex Bennée8908f4d2014-02-20 10:35:49 +00006375 case 0x1c: /* FCMEQ */
6376 gen_helper_neon_ceq_f64(tcg_res, tcg_op1, tcg_op2, fpst);
6377 break;
Peter Maydell845ea092014-01-31 14:47:37 +00006378 case 0x1e: /* FMAX */
6379 gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
6380 break;
Peter Maydell057d5f62014-02-20 10:35:50 +00006381 case 0x1f: /* FRECPS */
6382 gen_helper_recpsf_f64(tcg_res, tcg_op1, tcg_op2, fpst);
6383 break;
Peter Maydell845ea092014-01-31 14:47:37 +00006384 case 0x38: /* FMINNM */
6385 gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
6386 break;
6387 case 0x3a: /* FSUB */
6388 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
6389 break;
6390 case 0x3e: /* FMIN */
6391 gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
6392 break;
Peter Maydell057d5f62014-02-20 10:35:50 +00006393 case 0x3f: /* FRSQRTS */
6394 gen_helper_rsqrtsf_f64(tcg_res, tcg_op1, tcg_op2, fpst);
6395 break;
Peter Maydell845ea092014-01-31 14:47:37 +00006396 case 0x5b: /* FMUL */
6397 gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
6398 break;
Alex Bennée8908f4d2014-02-20 10:35:49 +00006399 case 0x5c: /* FCMGE */
6400 gen_helper_neon_cge_f64(tcg_res, tcg_op1, tcg_op2, fpst);
6401 break;
Peter Maydell057d5f62014-02-20 10:35:50 +00006402 case 0x5d: /* FACGE */
6403 gen_helper_neon_acge_f64(tcg_res, tcg_op1, tcg_op2, fpst);
6404 break;
Peter Maydell845ea092014-01-31 14:47:37 +00006405 case 0x5f: /* FDIV */
6406 gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
6407 break;
6408 case 0x7a: /* FABD */
6409 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
6410 gen_helper_vfp_absd(tcg_res, tcg_res);
6411 break;
Alex Bennée8908f4d2014-02-20 10:35:49 +00006412 case 0x7c: /* FCMGT */
6413 gen_helper_neon_cgt_f64(tcg_res, tcg_op1, tcg_op2, fpst);
6414 break;
Peter Maydell057d5f62014-02-20 10:35:50 +00006415 case 0x7d: /* FACGT */
6416 gen_helper_neon_acgt_f64(tcg_res, tcg_op1, tcg_op2, fpst);
6417 break;
Peter Maydell845ea092014-01-31 14:47:37 +00006418 default:
6419 g_assert_not_reached();
6420 }
6421
6422 write_vec_element(s, tcg_res, rd, pass, MO_64);
6423
6424 tcg_temp_free_i64(tcg_res);
6425 tcg_temp_free_i64(tcg_op1);
6426 tcg_temp_free_i64(tcg_op2);
6427 } else {
6428 /* Single */
6429 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
6430 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
6431 TCGv_i32 tcg_res = tcg_temp_new_i32();
6432
6433 read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
6434 read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
6435
6436 switch (fpopcode) {
Peter Maydell057d5f62014-02-20 10:35:50 +00006437 case 0x39: /* FMLS */
6438 /* As usual for ARM, separate negation for fused multiply-add */
6439 gen_helper_vfp_negs(tcg_op1, tcg_op1);
6440 /* fall through */
6441 case 0x19: /* FMLA */
6442 read_vec_element_i32(s, tcg_res, rd, pass, MO_32);
6443 gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2,
6444 tcg_res, fpst);
6445 break;
Peter Maydell845ea092014-01-31 14:47:37 +00006446 case 0x1a: /* FADD */
6447 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
6448 break;
Peter Maydell057d5f62014-02-20 10:35:50 +00006449 case 0x1b: /* FMULX */
6450 gen_helper_vfp_mulxs(tcg_res, tcg_op1, tcg_op2, fpst);
6451 break;
Alex Bennée8908f4d2014-02-20 10:35:49 +00006452 case 0x1c: /* FCMEQ */
6453 gen_helper_neon_ceq_f32(tcg_res, tcg_op1, tcg_op2, fpst);
6454 break;
Peter Maydell845ea092014-01-31 14:47:37 +00006455 case 0x1e: /* FMAX */
6456 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
6457 break;
Peter Maydell057d5f62014-02-20 10:35:50 +00006458 case 0x1f: /* FRECPS */
6459 gen_helper_recpsf_f32(tcg_res, tcg_op1, tcg_op2, fpst);
6460 break;
Peter Maydell845ea092014-01-31 14:47:37 +00006461 case 0x18: /* FMAXNM */
6462 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
6463 break;
6464 case 0x38: /* FMINNM */
6465 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
6466 break;
6467 case 0x3a: /* FSUB */
6468 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
6469 break;
6470 case 0x3e: /* FMIN */
6471 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
6472 break;
Peter Maydell057d5f62014-02-20 10:35:50 +00006473 case 0x3f: /* FRSQRTS */
6474 gen_helper_rsqrtsf_f32(tcg_res, tcg_op1, tcg_op2, fpst);
6475 break;
Peter Maydell845ea092014-01-31 14:47:37 +00006476 case 0x5b: /* FMUL */
6477 gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
6478 break;
Alex Bennée8908f4d2014-02-20 10:35:49 +00006479 case 0x5c: /* FCMGE */
6480 gen_helper_neon_cge_f32(tcg_res, tcg_op1, tcg_op2, fpst);
6481 break;
Peter Maydell057d5f62014-02-20 10:35:50 +00006482 case 0x5d: /* FACGE */
6483 gen_helper_neon_acge_f32(tcg_res, tcg_op1, tcg_op2, fpst);
6484 break;
Peter Maydell845ea092014-01-31 14:47:37 +00006485 case 0x5f: /* FDIV */
6486 gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
6487 break;
6488 case 0x7a: /* FABD */
6489 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
6490 gen_helper_vfp_abss(tcg_res, tcg_res);
6491 break;
Alex Bennée8908f4d2014-02-20 10:35:49 +00006492 case 0x7c: /* FCMGT */
6493 gen_helper_neon_cgt_f32(tcg_res, tcg_op1, tcg_op2, fpst);
6494 break;
Peter Maydell057d5f62014-02-20 10:35:50 +00006495 case 0x7d: /* FACGT */
6496 gen_helper_neon_acgt_f32(tcg_res, tcg_op1, tcg_op2, fpst);
6497 break;
Peter Maydell845ea092014-01-31 14:47:37 +00006498 default:
6499 g_assert_not_reached();
6500 }
6501
6502 if (elements == 1) {
6503 /* scalar single so clear high part */
6504 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
6505
6506 tcg_gen_extu_i32_i64(tcg_tmp, tcg_res);
6507 write_vec_element(s, tcg_tmp, rd, pass, MO_64);
6508 tcg_temp_free_i64(tcg_tmp);
6509 } else {
6510 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
6511 }
6512
6513 tcg_temp_free_i32(tcg_res);
6514 tcg_temp_free_i32(tcg_op1);
6515 tcg_temp_free_i32(tcg_op2);
6516 }
6517 }
6518
6519 tcg_temp_free_ptr(fpst);
6520
6521 if ((elements << size) < 4) {
6522 /* scalar, or non-quad vector op */
6523 clear_vec_high(s, rd);
6524 }
6525}
6526
Alex Bennée384b26f2014-01-31 14:47:30 +00006527/* C3.6.11 AdvSIMD scalar three same
6528 * 31 30 29 28 24 23 22 21 20 16 15 11 10 9 5 4 0
6529 * +-----+---+-----------+------+---+------+--------+---+------+------+
6530 * | 0 1 | U | 1 1 1 1 0 | size | 1 | Rm | opcode | 1 | Rn | Rd |
6531 * +-----+---+-----------+------+---+------+--------+---+------+------+
6532 */
6533static void disas_simd_scalar_three_reg_same(DisasContext *s, uint32_t insn)
6534{
Peter Maydellb305dba2014-01-31 14:47:36 +00006535 int rd = extract32(insn, 0, 5);
6536 int rn = extract32(insn, 5, 5);
6537 int opcode = extract32(insn, 11, 5);
6538 int rm = extract32(insn, 16, 5);
6539 int size = extract32(insn, 22, 2);
6540 bool u = extract32(insn, 29, 1);
Peter Maydellb305dba2014-01-31 14:47:36 +00006541 TCGv_i64 tcg_rd;
6542
6543 if (opcode >= 0x18) {
6544 /* Floating point: U, size[1] and opcode indicate operation */
6545 int fpopcode = opcode | (extract32(size, 1, 1) << 5) | (u << 6);
6546 switch (fpopcode) {
6547 case 0x1b: /* FMULX */
Peter Maydellb305dba2014-01-31 14:47:36 +00006548 case 0x1f: /* FRECPS */
6549 case 0x3f: /* FRSQRTS */
Peter Maydellb305dba2014-01-31 14:47:36 +00006550 case 0x5d: /* FACGE */
Peter Maydellb305dba2014-01-31 14:47:36 +00006551 case 0x7d: /* FACGT */
Alex Bennée8908f4d2014-02-20 10:35:49 +00006552 case 0x1c: /* FCMEQ */
6553 case 0x5c: /* FCMGE */
6554 case 0x7c: /* FCMGT */
Peter Maydell845ea092014-01-31 14:47:37 +00006555 case 0x7a: /* FABD */
6556 break;
Peter Maydellb305dba2014-01-31 14:47:36 +00006557 default:
6558 unallocated_encoding(s);
6559 return;
6560 }
Peter Maydell845ea092014-01-31 14:47:37 +00006561
6562 handle_3same_float(s, extract32(size, 0, 1), 1, fpopcode, rd, rn, rm);
6563 return;
Peter Maydellb305dba2014-01-31 14:47:36 +00006564 }
6565
6566 switch (opcode) {
6567 case 0x1: /* SQADD, UQADD */
6568 case 0x5: /* SQSUB, UQSUB */
Peter Maydellc0b2b5f2014-02-08 14:46:56 +00006569 case 0x9: /* SQSHL, UQSHL */
6570 case 0xb: /* SQRSHL, UQRSHL */
6571 break;
Peter Maydell6d9571f2014-02-08 14:46:55 +00006572 case 0x8: /* SSHL, USHL */
6573 case 0xa: /* SRSHL, URSHL */
Peter Maydellb305dba2014-01-31 14:47:36 +00006574 case 0x6: /* CMGT, CMHI */
6575 case 0x7: /* CMGE, CMHS */
6576 case 0x11: /* CMTST, CMEQ */
6577 case 0x10: /* ADD, SUB (vector) */
6578 if (size != 3) {
6579 unallocated_encoding(s);
6580 return;
6581 }
6582 break;
Peter Maydellb305dba2014-01-31 14:47:36 +00006583 case 0x16: /* SQDMULH, SQRDMULH (vector) */
6584 if (size != 1 && size != 2) {
6585 unallocated_encoding(s);
6586 return;
6587 }
Peter Maydellc0b2b5f2014-02-08 14:46:56 +00006588 break;
Peter Maydellb305dba2014-01-31 14:47:36 +00006589 default:
6590 unallocated_encoding(s);
6591 return;
6592 }
6593
Peter Maydellb305dba2014-01-31 14:47:36 +00006594 tcg_rd = tcg_temp_new_i64();
6595
Peter Maydellc0b2b5f2014-02-08 14:46:56 +00006596 if (size == 3) {
6597 TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
6598 TCGv_i64 tcg_rm = read_fp_dreg(s, rm);
6599
6600 handle_3same_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rm);
6601 tcg_temp_free_i64(tcg_rn);
6602 tcg_temp_free_i64(tcg_rm);
6603 } else {
6604 /* Do a single operation on the lowest element in the vector.
6605 * We use the standard Neon helpers and rely on 0 OP 0 == 0 with
6606 * no side effects for all these operations.
6607 * OPTME: special-purpose helpers would avoid doing some
6608 * unnecessary work in the helper for the 8 and 16 bit cases.
6609 */
6610 NeonGenTwoOpEnvFn *genenvfn;
6611 TCGv_i32 tcg_rn = tcg_temp_new_i32();
6612 TCGv_i32 tcg_rm = tcg_temp_new_i32();
6613 TCGv_i32 tcg_rd32 = tcg_temp_new_i32();
6614
6615 read_vec_element_i32(s, tcg_rn, rn, 0, size);
6616 read_vec_element_i32(s, tcg_rm, rm, 0, size);
6617
6618 switch (opcode) {
6619 case 0x1: /* SQADD, UQADD */
6620 {
6621 static NeonGenTwoOpEnvFn * const fns[3][2] = {
6622 { gen_helper_neon_qadd_s8, gen_helper_neon_qadd_u8 },
6623 { gen_helper_neon_qadd_s16, gen_helper_neon_qadd_u16 },
6624 { gen_helper_neon_qadd_s32, gen_helper_neon_qadd_u32 },
6625 };
6626 genenvfn = fns[size][u];
6627 break;
6628 }
6629 case 0x5: /* SQSUB, UQSUB */
6630 {
6631 static NeonGenTwoOpEnvFn * const fns[3][2] = {
6632 { gen_helper_neon_qsub_s8, gen_helper_neon_qsub_u8 },
6633 { gen_helper_neon_qsub_s16, gen_helper_neon_qsub_u16 },
6634 { gen_helper_neon_qsub_s32, gen_helper_neon_qsub_u32 },
6635 };
6636 genenvfn = fns[size][u];
6637 break;
6638 }
6639 case 0x9: /* SQSHL, UQSHL */
6640 {
6641 static NeonGenTwoOpEnvFn * const fns[3][2] = {
6642 { gen_helper_neon_qshl_s8, gen_helper_neon_qshl_u8 },
6643 { gen_helper_neon_qshl_s16, gen_helper_neon_qshl_u16 },
6644 { gen_helper_neon_qshl_s32, gen_helper_neon_qshl_u32 },
6645 };
6646 genenvfn = fns[size][u];
6647 break;
6648 }
6649 case 0xb: /* SQRSHL, UQRSHL */
6650 {
6651 static NeonGenTwoOpEnvFn * const fns[3][2] = {
6652 { gen_helper_neon_qrshl_s8, gen_helper_neon_qrshl_u8 },
6653 { gen_helper_neon_qrshl_s16, gen_helper_neon_qrshl_u16 },
6654 { gen_helper_neon_qrshl_s32, gen_helper_neon_qrshl_u32 },
6655 };
6656 genenvfn = fns[size][u];
6657 break;
6658 }
6659 case 0x16: /* SQDMULH, SQRDMULH */
6660 {
6661 static NeonGenTwoOpEnvFn * const fns[2][2] = {
6662 { gen_helper_neon_qdmulh_s16, gen_helper_neon_qrdmulh_s16 },
6663 { gen_helper_neon_qdmulh_s32, gen_helper_neon_qrdmulh_s32 },
6664 };
6665 assert(size == 1 || size == 2);
6666 genenvfn = fns[size - 1][u];
6667 break;
6668 }
6669 default:
6670 g_assert_not_reached();
6671 }
6672
6673 genenvfn(tcg_rd32, cpu_env, tcg_rn, tcg_rm);
6674 tcg_gen_extu_i32_i64(tcg_rd, tcg_rd32);
6675 tcg_temp_free_i32(tcg_rd32);
6676 tcg_temp_free_i32(tcg_rn);
6677 tcg_temp_free_i32(tcg_rm);
6678 }
Peter Maydellb305dba2014-01-31 14:47:36 +00006679
6680 write_fp_dreg(s, rd, tcg_rd);
6681
Peter Maydellb305dba2014-01-31 14:47:36 +00006682 tcg_temp_free_i64(tcg_rd);
Alex Bennée384b26f2014-01-31 14:47:30 +00006683}
6684
Peter Maydelleffa8e02014-02-08 14:46:56 +00006685static void handle_2misc_64(DisasContext *s, int opcode, bool u,
Peter Maydell416ac202014-03-07 16:49:14 +00006686 TCGv_i64 tcg_rd, TCGv_i64 tcg_rn,
6687 TCGv_i32 tcg_rmode, TCGv_ptr tcg_fpstatus)
Peter Maydelleffa8e02014-02-08 14:46:56 +00006688{
6689 /* Handle 64->64 opcodes which are shared between the scalar and
6690 * vector 2-reg-misc groups. We cover every integer opcode where size == 3
Peter Maydellf93d0132014-02-03 23:31:52 +00006691 * is valid in either group and also the double-precision fp ops.
Peter Maydell416ac202014-03-07 16:49:14 +00006692 * The caller only need provide tcg_rmode and tcg_fpstatus if the op
6693 * requires them.
Peter Maydelleffa8e02014-02-08 14:46:56 +00006694 */
6695 TCGCond cond;
6696
6697 switch (opcode) {
Alex Bennée7f8d0b72014-02-25 14:50:33 +00006698 case 0x4: /* CLS, CLZ */
6699 if (u) {
6700 gen_helper_clz64(tcg_rd, tcg_rn);
6701 } else {
6702 gen_helper_cls64(tcg_rd, tcg_rn);
6703 }
6704 break;
Peter Maydell86cbc412014-02-03 23:31:51 +00006705 case 0x5: /* NOT */
6706 /* This opcode is shared with CNT and RBIT but we have earlier
6707 * enforced that size == 3 if and only if this is the NOT insn.
6708 */
6709 tcg_gen_not_i64(tcg_rd, tcg_rn);
6710 break;
Peter Maydelleffa8e02014-02-08 14:46:56 +00006711 case 0xa: /* CMLT */
6712 /* 64 bit integer comparison against zero, result is
6713 * test ? (2^64 - 1) : 0. We implement via setcond(!test) and
6714 * subtracting 1.
6715 */
6716 cond = TCG_COND_LT;
6717 do_cmop:
6718 tcg_gen_setcondi_i64(cond, tcg_rd, tcg_rn, 0);
6719 tcg_gen_neg_i64(tcg_rd, tcg_rd);
6720 break;
6721 case 0x8: /* CMGT, CMGE */
6722 cond = u ? TCG_COND_GE : TCG_COND_GT;
6723 goto do_cmop;
6724 case 0x9: /* CMEQ, CMLE */
6725 cond = u ? TCG_COND_LE : TCG_COND_EQ;
6726 goto do_cmop;
6727 case 0xb: /* ABS, NEG */
6728 if (u) {
6729 tcg_gen_neg_i64(tcg_rd, tcg_rn);
6730 } else {
6731 TCGv_i64 tcg_zero = tcg_const_i64(0);
6732 tcg_gen_neg_i64(tcg_rd, tcg_rn);
6733 tcg_gen_movcond_i64(TCG_COND_GT, tcg_rd, tcg_rn, tcg_zero,
6734 tcg_rn, tcg_rd);
6735 tcg_temp_free_i64(tcg_zero);
6736 }
6737 break;
Peter Maydellf93d0132014-02-03 23:31:52 +00006738 case 0x2f: /* FABS */
6739 gen_helper_vfp_absd(tcg_rd, tcg_rn);
6740 break;
6741 case 0x6f: /* FNEG */
6742 gen_helper_vfp_negd(tcg_rd, tcg_rn);
6743 break;
Alex Bennéeb399d6d2014-02-25 12:21:05 +00006744 case 0x7f: /* FSQRT */
6745 gen_helper_vfp_sqrtd(tcg_rd, tcg_rn, cpu_env);
6746 break;
Peter Maydell416ac202014-03-07 16:49:14 +00006747 case 0x1a: /* FCVTNS */
6748 case 0x1b: /* FCVTMS */
6749 case 0x1c: /* FCVTAS */
6750 case 0x3a: /* FCVTPS */
6751 case 0x3b: /* FCVTZS */
6752 {
6753 TCGv_i32 tcg_shift = tcg_const_i32(0);
6754 gen_helper_vfp_tosqd(tcg_rd, tcg_rn, tcg_shift, tcg_fpstatus);
6755 tcg_temp_free_i32(tcg_shift);
6756 break;
6757 }
6758 case 0x5a: /* FCVTNU */
6759 case 0x5b: /* FCVTMU */
6760 case 0x5c: /* FCVTAU */
6761 case 0x7a: /* FCVTPU */
6762 case 0x7b: /* FCVTZU */
6763 {
6764 TCGv_i32 tcg_shift = tcg_const_i32(0);
6765 gen_helper_vfp_touqd(tcg_rd, tcg_rn, tcg_shift, tcg_fpstatus);
6766 tcg_temp_free_i32(tcg_shift);
6767 break;
6768 }
Peter Maydelleffa8e02014-02-08 14:46:56 +00006769 default:
6770 g_assert_not_reached();
6771 }
6772}
6773
Alex Bennée8908f4d2014-02-20 10:35:49 +00006774static void handle_2misc_fcmp_zero(DisasContext *s, int opcode,
6775 bool is_scalar, bool is_u, bool is_q,
6776 int size, int rn, int rd)
6777{
6778 bool is_double = (size == 3);
6779 TCGv_ptr fpst = get_fpstatus_ptr();
6780
6781 if (is_double) {
6782 TCGv_i64 tcg_op = tcg_temp_new_i64();
6783 TCGv_i64 tcg_zero = tcg_const_i64(0);
6784 TCGv_i64 tcg_res = tcg_temp_new_i64();
6785 NeonGenTwoDoubleOPFn *genfn;
6786 bool swap = false;
6787 int pass;
6788
6789 switch (opcode) {
6790 case 0x2e: /* FCMLT (zero) */
6791 swap = true;
6792 /* fallthrough */
6793 case 0x2c: /* FCMGT (zero) */
6794 genfn = gen_helper_neon_cgt_f64;
6795 break;
6796 case 0x2d: /* FCMEQ (zero) */
6797 genfn = gen_helper_neon_ceq_f64;
6798 break;
6799 case 0x6d: /* FCMLE (zero) */
6800 swap = true;
6801 /* fall through */
6802 case 0x6c: /* FCMGE (zero) */
6803 genfn = gen_helper_neon_cge_f64;
6804 break;
6805 default:
6806 g_assert_not_reached();
6807 }
6808
6809 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
6810 read_vec_element(s, tcg_op, rn, pass, MO_64);
6811 if (swap) {
6812 genfn(tcg_res, tcg_zero, tcg_op, fpst);
6813 } else {
6814 genfn(tcg_res, tcg_op, tcg_zero, fpst);
6815 }
6816 write_vec_element(s, tcg_res, rd, pass, MO_64);
6817 }
6818 if (is_scalar) {
6819 clear_vec_high(s, rd);
6820 }
6821
6822 tcg_temp_free_i64(tcg_res);
6823 tcg_temp_free_i64(tcg_zero);
6824 tcg_temp_free_i64(tcg_op);
6825 } else {
6826 TCGv_i32 tcg_op = tcg_temp_new_i32();
6827 TCGv_i32 tcg_zero = tcg_const_i32(0);
6828 TCGv_i32 tcg_res = tcg_temp_new_i32();
6829 NeonGenTwoSingleOPFn *genfn;
6830 bool swap = false;
6831 int pass, maxpasses;
6832
6833 switch (opcode) {
6834 case 0x2e: /* FCMLT (zero) */
6835 swap = true;
6836 /* fall through */
6837 case 0x2c: /* FCMGT (zero) */
6838 genfn = gen_helper_neon_cgt_f32;
6839 break;
6840 case 0x2d: /* FCMEQ (zero) */
6841 genfn = gen_helper_neon_ceq_f32;
6842 break;
6843 case 0x6d: /* FCMLE (zero) */
6844 swap = true;
6845 /* fall through */
6846 case 0x6c: /* FCMGE (zero) */
6847 genfn = gen_helper_neon_cge_f32;
6848 break;
6849 default:
6850 g_assert_not_reached();
6851 }
6852
6853 if (is_scalar) {
6854 maxpasses = 1;
6855 } else {
6856 maxpasses = is_q ? 4 : 2;
6857 }
6858
6859 for (pass = 0; pass < maxpasses; pass++) {
6860 read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
6861 if (swap) {
6862 genfn(tcg_res, tcg_zero, tcg_op, fpst);
6863 } else {
6864 genfn(tcg_res, tcg_op, tcg_zero, fpst);
6865 }
6866 if (is_scalar) {
6867 write_fp_sreg(s, rd, tcg_res);
6868 } else {
6869 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
6870 }
6871 }
6872 tcg_temp_free_i32(tcg_res);
6873 tcg_temp_free_i32(tcg_zero);
6874 tcg_temp_free_i32(tcg_op);
6875 if (!is_q && !is_scalar) {
6876 clear_vec_high(s, rd);
6877 }
6878 }
6879
6880 tcg_temp_free_ptr(fpst);
6881}
6882
Alex Bennée384b26f2014-01-31 14:47:30 +00006883/* C3.6.12 AdvSIMD scalar two reg misc
6884 * 31 30 29 28 24 23 22 21 17 16 12 11 10 9 5 4 0
6885 * +-----+---+-----------+------+-----------+--------+-----+------+------+
6886 * | 0 1 | U | 1 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 | Rn | Rd |
6887 * +-----+---+-----------+------+-----------+--------+-----+------+------+
6888 */
6889static void disas_simd_scalar_two_reg_misc(DisasContext *s, uint32_t insn)
6890{
Peter Maydelleffa8e02014-02-08 14:46:56 +00006891 int rd = extract32(insn, 0, 5);
6892 int rn = extract32(insn, 5, 5);
6893 int opcode = extract32(insn, 12, 5);
6894 int size = extract32(insn, 22, 2);
6895 bool u = extract32(insn, 29, 1);
Peter Maydell416ac202014-03-07 16:49:14 +00006896 bool is_fcvt = false;
6897 int rmode;
6898 TCGv_i32 tcg_rmode;
6899 TCGv_ptr tcg_fpstatus;
Peter Maydelleffa8e02014-02-08 14:46:56 +00006900
6901 switch (opcode) {
6902 case 0xa: /* CMLT */
6903 if (u) {
6904 unallocated_encoding(s);
6905 return;
6906 }
6907 /* fall through */
6908 case 0x8: /* CMGT, CMGE */
6909 case 0x9: /* CMEQ, CMLE */
6910 case 0xb: /* ABS, NEG */
6911 if (size != 3) {
6912 unallocated_encoding(s);
6913 return;
6914 }
6915 break;
Alex Bennée8908f4d2014-02-20 10:35:49 +00006916 case 0xc ... 0xf:
6917 case 0x16 ... 0x1d:
6918 case 0x1f:
6919 /* Floating point: U, size[1] and opcode indicate operation;
6920 * size[0] indicates single or double precision.
6921 */
6922 opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
6923 size = extract32(size, 0, 1) ? 3 : 2;
6924 switch (opcode) {
6925 case 0x2c: /* FCMGT (zero) */
6926 case 0x2d: /* FCMEQ (zero) */
6927 case 0x2e: /* FCMLT (zero) */
6928 case 0x6c: /* FCMGE (zero) */
6929 case 0x6d: /* FCMLE (zero) */
6930 handle_2misc_fcmp_zero(s, opcode, true, u, true, size, rn, rd);
6931 return;
Peter Maydell745849b2014-03-08 20:40:20 +00006932 case 0x1d: /* SCVTF */
6933 case 0x5d: /* UCVTF */
6934 {
6935 bool is_signed = (opcode == 0x1d);
6936 handle_simd_intfp_conv(s, rd, rn, 1, is_signed, 0, size);
6937 return;
6938 }
Alex Bennée8908f4d2014-02-20 10:35:49 +00006939 case 0x1a: /* FCVTNS */
6940 case 0x1b: /* FCVTMS */
Alex Bennée8908f4d2014-02-20 10:35:49 +00006941 case 0x3a: /* FCVTPS */
6942 case 0x3b: /* FCVTZS */
Peter Maydell416ac202014-03-07 16:49:14 +00006943 case 0x5a: /* FCVTNU */
6944 case 0x5b: /* FCVTMU */
6945 case 0x7a: /* FCVTPU */
6946 case 0x7b: /* FCVTZU */
6947 is_fcvt = true;
6948 rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
6949 break;
6950 case 0x1c: /* FCVTAS */
6951 case 0x5c: /* FCVTAU */
6952 /* TIEAWAY doesn't fit in the usual rounding mode encoding */
6953 is_fcvt = true;
6954 rmode = FPROUNDING_TIEAWAY;
6955 break;
Alex Bennée8908f4d2014-02-20 10:35:49 +00006956 case 0x3d: /* FRECPE */
6957 case 0x3f: /* FRECPX */
6958 case 0x56: /* FCVTXN, FCVTXN2 */
Alex Bennée8908f4d2014-02-20 10:35:49 +00006959 case 0x7d: /* FRSQRTE */
6960 unsupported_encoding(s, insn);
6961 return;
6962 default:
6963 unallocated_encoding(s);
6964 return;
6965 }
6966 break;
Peter Maydelleffa8e02014-02-08 14:46:56 +00006967 default:
6968 /* Other categories of encoding in this class:
Peter Maydelleffa8e02014-02-08 14:46:56 +00006969 * + SUQADD/USQADD/SQABS/SQNEG : size 8, 16, 32 or 64
6970 * + SQXTN/SQXTN2/SQXTUN/SQXTUN2/UQXTN/UQXTN2:
6971 * narrowing saturate ops: size 64/32/16 -> 32/16/8
6972 */
6973 unsupported_encoding(s, insn);
6974 return;
6975 }
6976
Peter Maydell416ac202014-03-07 16:49:14 +00006977 if (is_fcvt) {
6978 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rmode));
6979 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
6980 tcg_fpstatus = get_fpstatus_ptr();
6981 } else {
6982 TCGV_UNUSED_I32(tcg_rmode);
6983 TCGV_UNUSED_PTR(tcg_fpstatus);
6984 }
6985
Peter Maydelleffa8e02014-02-08 14:46:56 +00006986 if (size == 3) {
6987 TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
6988 TCGv_i64 tcg_rd = tcg_temp_new_i64();
6989
Peter Maydell416ac202014-03-07 16:49:14 +00006990 handle_2misc_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rmode, tcg_fpstatus);
Peter Maydelleffa8e02014-02-08 14:46:56 +00006991 write_fp_dreg(s, rd, tcg_rd);
6992 tcg_temp_free_i64(tcg_rd);
6993 tcg_temp_free_i64(tcg_rn);
Peter Maydell416ac202014-03-07 16:49:14 +00006994 } else if (size == 2) {
6995 TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
6996 TCGv_i32 tcg_rd = tcg_temp_new_i32();
6997
6998 switch (opcode) {
6999 case 0x1a: /* FCVTNS */
7000 case 0x1b: /* FCVTMS */
7001 case 0x1c: /* FCVTAS */
7002 case 0x3a: /* FCVTPS */
7003 case 0x3b: /* FCVTZS */
7004 {
7005 TCGv_i32 tcg_shift = tcg_const_i32(0);
7006 gen_helper_vfp_tosls(tcg_rd, tcg_rn, tcg_shift, tcg_fpstatus);
7007 tcg_temp_free_i32(tcg_shift);
7008 break;
7009 }
7010 case 0x5a: /* FCVTNU */
7011 case 0x5b: /* FCVTMU */
7012 case 0x5c: /* FCVTAU */
7013 case 0x7a: /* FCVTPU */
7014 case 0x7b: /* FCVTZU */
7015 {
7016 TCGv_i32 tcg_shift = tcg_const_i32(0);
7017 gen_helper_vfp_touls(tcg_rd, tcg_rn, tcg_shift, tcg_fpstatus);
7018 tcg_temp_free_i32(tcg_shift);
7019 break;
7020 }
7021 default:
7022 g_assert_not_reached();
7023 }
7024
7025 write_fp_sreg(s, rd, tcg_rd);
7026 tcg_temp_free_i32(tcg_rd);
7027 tcg_temp_free_i32(tcg_rn);
Peter Maydelleffa8e02014-02-08 14:46:56 +00007028 } else {
Peter Maydelleffa8e02014-02-08 14:46:56 +00007029 g_assert_not_reached();
7030 }
Peter Maydell416ac202014-03-07 16:49:14 +00007031
7032 if (is_fcvt) {
7033 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
7034 tcg_temp_free_i32(tcg_rmode);
7035 tcg_temp_free_ptr(tcg_fpstatus);
7036 }
Alex Bennée384b26f2014-01-31 14:47:30 +00007037}
7038
Alex Bennée4d1cef82014-01-31 14:47:37 +00007039/* SSHR[RA]/USHR[RA] - Vector shift right (optional rounding/accumulate) */
7040static void handle_vec_simd_shri(DisasContext *s, bool is_q, bool is_u,
7041 int immh, int immb, int opcode, int rn, int rd)
7042{
7043 int size = 32 - clz32(immh) - 1;
7044 int immhb = immh << 3 | immb;
7045 int shift = 2 * (8 << size) - immhb;
7046 bool accumulate = false;
7047 bool round = false;
7048 int dsize = is_q ? 128 : 64;
7049 int esize = 8 << size;
7050 int elements = dsize/esize;
7051 TCGMemOp memop = size | (is_u ? 0 : MO_SIGN);
7052 TCGv_i64 tcg_rn = new_tmp_a64(s);
7053 TCGv_i64 tcg_rd = new_tmp_a64(s);
7054 TCGv_i64 tcg_round;
7055 int i;
7056
7057 if (extract32(immh, 3, 1) && !is_q) {
7058 unallocated_encoding(s);
7059 return;
7060 }
7061
7062 if (size > 3 && !is_q) {
7063 unallocated_encoding(s);
7064 return;
7065 }
7066
7067 switch (opcode) {
7068 case 0x02: /* SSRA / USRA (accumulate) */
7069 accumulate = true;
7070 break;
7071 case 0x04: /* SRSHR / URSHR (rounding) */
7072 round = true;
7073 break;
7074 case 0x06: /* SRSRA / URSRA (accum + rounding) */
7075 accumulate = round = true;
7076 break;
7077 }
7078
7079 if (round) {
7080 uint64_t round_const = 1ULL << (shift - 1);
7081 tcg_round = tcg_const_i64(round_const);
7082 } else {
7083 TCGV_UNUSED_I64(tcg_round);
7084 }
7085
7086 for (i = 0; i < elements; i++) {
7087 read_vec_element(s, tcg_rn, rn, i, memop);
7088 if (accumulate) {
7089 read_vec_element(s, tcg_rd, rd, i, memop);
7090 }
7091
7092 handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
7093 accumulate, is_u, size, shift);
7094
7095 write_vec_element(s, tcg_rd, rd, i, size);
7096 }
7097
7098 if (!is_q) {
7099 clear_vec_high(s, rd);
7100 }
7101
7102 if (round) {
7103 tcg_temp_free_i64(tcg_round);
7104 }
7105}
7106
7107/* SHL/SLI - Vector shift left */
7108static void handle_vec_simd_shli(DisasContext *s, bool is_q, bool insert,
7109 int immh, int immb, int opcode, int rn, int rd)
7110{
7111 int size = 32 - clz32(immh) - 1;
7112 int immhb = immh << 3 | immb;
7113 int shift = immhb - (8 << size);
7114 int dsize = is_q ? 128 : 64;
7115 int esize = 8 << size;
7116 int elements = dsize/esize;
7117 TCGv_i64 tcg_rn = new_tmp_a64(s);
7118 TCGv_i64 tcg_rd = new_tmp_a64(s);
7119 int i;
7120
7121 if (extract32(immh, 3, 1) && !is_q) {
7122 unallocated_encoding(s);
7123 return;
7124 }
7125
7126 if (size > 3 && !is_q) {
7127 unallocated_encoding(s);
7128 return;
7129 }
7130
7131 for (i = 0; i < elements; i++) {
7132 read_vec_element(s, tcg_rn, rn, i, size);
7133 if (insert) {
7134 read_vec_element(s, tcg_rd, rd, i, size);
7135 }
7136
7137 handle_shli_with_ins(tcg_rd, tcg_rn, insert, shift);
7138
7139 write_vec_element(s, tcg_rd, rd, i, size);
7140 }
7141
7142 if (!is_q) {
7143 clear_vec_high(s, rd);
7144 }
7145}
7146
7147/* USHLL/SHLL - Vector shift left with widening */
7148static void handle_vec_simd_wshli(DisasContext *s, bool is_q, bool is_u,
7149 int immh, int immb, int opcode, int rn, int rd)
7150{
7151 int size = 32 - clz32(immh) - 1;
7152 int immhb = immh << 3 | immb;
7153 int shift = immhb - (8 << size);
7154 int dsize = 64;
7155 int esize = 8 << size;
7156 int elements = dsize/esize;
7157 TCGv_i64 tcg_rn = new_tmp_a64(s);
7158 TCGv_i64 tcg_rd = new_tmp_a64(s);
7159 int i;
7160
7161 if (size >= 3) {
7162 unallocated_encoding(s);
7163 return;
7164 }
7165
7166 /* For the LL variants the store is larger than the load,
7167 * so if rd == rn we would overwrite parts of our input.
7168 * So load everything right now and use shifts in the main loop.
7169 */
7170 read_vec_element(s, tcg_rn, rn, is_q ? 1 : 0, MO_64);
7171
7172 for (i = 0; i < elements; i++) {
7173 tcg_gen_shri_i64(tcg_rd, tcg_rn, i * esize);
7174 ext_and_shift_reg(tcg_rd, tcg_rd, size | (!is_u << 2), 0);
7175 tcg_gen_shli_i64(tcg_rd, tcg_rd, shift);
7176 write_vec_element(s, tcg_rd, rd, i, size + 1);
7177 }
7178}
7179
Peter Maydellc3882982014-03-08 21:56:55 +00007180/* SHRN/RSHRN - Shift right with narrowing (and potential rounding) */
7181static void handle_vec_simd_shrn(DisasContext *s, bool is_q,
7182 int immh, int immb, int opcode, int rn, int rd)
7183{
7184 int immhb = immh << 3 | immb;
7185 int size = 32 - clz32(immh) - 1;
7186 int dsize = 64;
7187 int esize = 8 << size;
7188 int elements = dsize/esize;
7189 int shift = (2 * esize) - immhb;
7190 bool round = extract32(opcode, 0, 1);
7191 TCGv_i64 tcg_rn, tcg_rd, tcg_final;
7192 TCGv_i64 tcg_round;
7193 int i;
7194
7195 if (extract32(immh, 3, 1)) {
7196 unallocated_encoding(s);
7197 return;
7198 }
7199
7200 tcg_rn = tcg_temp_new_i64();
7201 tcg_rd = tcg_temp_new_i64();
7202 tcg_final = tcg_temp_new_i64();
7203 read_vec_element(s, tcg_final, rd, is_q ? 1 : 0, MO_64);
7204
7205 if (round) {
7206 uint64_t round_const = 1ULL << (shift - 1);
7207 tcg_round = tcg_const_i64(round_const);
7208 } else {
7209 TCGV_UNUSED_I64(tcg_round);
7210 }
7211
7212 for (i = 0; i < elements; i++) {
7213 read_vec_element(s, tcg_rn, rn, i, size+1);
7214 handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
7215 false, true, size+1, shift);
7216
7217 tcg_gen_deposit_i64(tcg_final, tcg_final, tcg_rd, esize * i, esize);
7218 }
7219
7220 if (!is_q) {
7221 clear_vec_high(s, rd);
7222 write_vec_element(s, tcg_final, rd, 0, MO_64);
7223 } else {
7224 write_vec_element(s, tcg_final, rd, 1, MO_64);
7225 }
7226
7227 if (round) {
7228 tcg_temp_free_i64(tcg_round);
7229 }
7230 tcg_temp_free_i64(tcg_rn);
7231 tcg_temp_free_i64(tcg_rd);
7232 tcg_temp_free_i64(tcg_final);
7233 return;
7234}
7235
7236
Alex Bennée384b26f2014-01-31 14:47:30 +00007237/* C3.6.14 AdvSIMD shift by immediate
7238 * 31 30 29 28 23 22 19 18 16 15 11 10 9 5 4 0
7239 * +---+---+---+-------------+------+------+--------+---+------+------+
7240 * | 0 | Q | U | 0 1 1 1 1 0 | immh | immb | opcode | 1 | Rn | Rd |
7241 * +---+---+---+-------------+------+------+--------+---+------+------+
7242 */
7243static void disas_simd_shift_imm(DisasContext *s, uint32_t insn)
7244{
Alex Bennée4d1cef82014-01-31 14:47:37 +00007245 int rd = extract32(insn, 0, 5);
7246 int rn = extract32(insn, 5, 5);
7247 int opcode = extract32(insn, 11, 5);
7248 int immb = extract32(insn, 16, 3);
7249 int immh = extract32(insn, 19, 4);
7250 bool is_u = extract32(insn, 29, 1);
7251 bool is_q = extract32(insn, 30, 1);
7252
7253 switch (opcode) {
7254 case 0x00: /* SSHR / USHR */
7255 case 0x02: /* SSRA / USRA (accumulate) */
7256 case 0x04: /* SRSHR / URSHR (rounding) */
7257 case 0x06: /* SRSRA / URSRA (accum + rounding) */
7258 handle_vec_simd_shri(s, is_q, is_u, immh, immb, opcode, rn, rd);
7259 break;
7260 case 0x0a: /* SHL / SLI */
7261 handle_vec_simd_shli(s, is_q, is_u, immh, immb, opcode, rn, rd);
7262 break;
Peter Maydellc3882982014-03-08 21:56:55 +00007263 case 0x10: /* SHRN */
7264 case 0x11: /* RSHRN / SQRSHRUN */
7265 if (is_u) {
7266 handle_vec_simd_sqshrn(s, false, is_q, false, true, immh, immb,
7267 opcode, rn, rd);
7268 } else {
7269 handle_vec_simd_shrn(s, is_q, immh, immb, opcode, rn, rd);
7270 }
7271 break;
7272 case 0x12: /* SQSHRN / UQSHRN */
7273 case 0x13: /* SQRSHRN / UQRSHRN */
7274 handle_vec_simd_sqshrn(s, false, is_q, is_u, is_u, immh, immb,
7275 opcode, rn, rd);
7276 break;
Alex Bennée4d1cef82014-01-31 14:47:37 +00007277 case 0x14: /* SSHLL / USHLL */
7278 handle_vec_simd_wshli(s, is_q, is_u, immh, immb, opcode, rn, rd);
7279 break;
Peter Maydell745849b2014-03-08 20:40:20 +00007280 case 0x1c: /* SCVTF / UCVTF */
7281 handle_simd_shift_intfp_conv(s, false, is_q, is_u, immh, immb,
7282 opcode, rn, rd);
7283 break;
7284 case 0x1f: /* FCVTZS/ FCVTZU */
7285 unsupported_encoding(s, insn);
7286 return;
Alex Bennée4d1cef82014-01-31 14:47:37 +00007287 default:
Alex Bennée4d1cef82014-01-31 14:47:37 +00007288 unsupported_encoding(s, insn);
7289 return;
7290 }
Alex Bennée384b26f2014-01-31 14:47:30 +00007291}
7292
Peter Maydell70d7f982014-02-20 10:35:55 +00007293/* Generate code to do a "long" addition or subtraction, ie one done in
7294 * TCGv_i64 on vector lanes twice the width specified by size.
7295 */
7296static void gen_neon_addl(int size, bool is_sub, TCGv_i64 tcg_res,
7297 TCGv_i64 tcg_op1, TCGv_i64 tcg_op2)
7298{
7299 static NeonGenTwo64OpFn * const fns[3][2] = {
7300 { gen_helper_neon_addl_u16, gen_helper_neon_subl_u16 },
7301 { gen_helper_neon_addl_u32, gen_helper_neon_subl_u32 },
7302 { tcg_gen_add_i64, tcg_gen_sub_i64 },
7303 };
7304 NeonGenTwo64OpFn *genfn;
7305 assert(size < 3);
7306
7307 genfn = fns[size][is_sub];
7308 genfn(tcg_res, tcg_op1, tcg_op2);
7309}
7310
Peter Maydella08582f2014-01-31 14:47:36 +00007311static void handle_3rd_widening(DisasContext *s, int is_q, int is_u, int size,
7312 int opcode, int rd, int rn, int rm)
7313{
7314 /* 3-reg-different widening insns: 64 x 64 -> 128 */
7315 TCGv_i64 tcg_res[2];
7316 int pass, accop;
7317
7318 tcg_res[0] = tcg_temp_new_i64();
7319 tcg_res[1] = tcg_temp_new_i64();
7320
7321 /* Does this op do an adding accumulate, a subtracting accumulate,
7322 * or no accumulate at all?
7323 */
7324 switch (opcode) {
7325 case 5:
7326 case 8:
7327 case 9:
7328 accop = 1;
7329 break;
7330 case 10:
7331 case 11:
7332 accop = -1;
7333 break;
7334 default:
7335 accop = 0;
7336 break;
7337 }
7338
7339 if (accop != 0) {
7340 read_vec_element(s, tcg_res[0], rd, 0, MO_64);
7341 read_vec_element(s, tcg_res[1], rd, 1, MO_64);
7342 }
7343
7344 /* size == 2 means two 32x32->64 operations; this is worth special
7345 * casing because we can generally handle it inline.
7346 */
7347 if (size == 2) {
7348 for (pass = 0; pass < 2; pass++) {
7349 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
7350 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
7351 TCGv_i64 tcg_passres;
7352 TCGMemOp memop = MO_32 | (is_u ? 0 : MO_SIGN);
7353
7354 int elt = pass + is_q * 2;
7355
7356 read_vec_element(s, tcg_op1, rn, elt, memop);
7357 read_vec_element(s, tcg_op2, rm, elt, memop);
7358
7359 if (accop == 0) {
7360 tcg_passres = tcg_res[pass];
7361 } else {
7362 tcg_passres = tcg_temp_new_i64();
7363 }
7364
7365 switch (opcode) {
Peter Maydell70d7f982014-02-20 10:35:55 +00007366 case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
7367 tcg_gen_add_i64(tcg_passres, tcg_op1, tcg_op2);
7368 break;
7369 case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
7370 tcg_gen_sub_i64(tcg_passres, tcg_op1, tcg_op2);
7371 break;
Peter Maydell0ae39322014-01-31 14:47:36 +00007372 case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
7373 case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
7374 {
7375 TCGv_i64 tcg_tmp1 = tcg_temp_new_i64();
7376 TCGv_i64 tcg_tmp2 = tcg_temp_new_i64();
7377
7378 tcg_gen_sub_i64(tcg_tmp1, tcg_op1, tcg_op2);
7379 tcg_gen_sub_i64(tcg_tmp2, tcg_op2, tcg_op1);
7380 tcg_gen_movcond_i64(is_u ? TCG_COND_GEU : TCG_COND_GE,
7381 tcg_passres,
7382 tcg_op1, tcg_op2, tcg_tmp1, tcg_tmp2);
7383 tcg_temp_free_i64(tcg_tmp1);
7384 tcg_temp_free_i64(tcg_tmp2);
7385 break;
7386 }
Peter Maydella08582f2014-01-31 14:47:36 +00007387 case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
7388 case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
7389 case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
7390 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
7391 break;
Peter Maydell70d7f982014-02-20 10:35:55 +00007392 case 9: /* SQDMLAL, SQDMLAL2 */
7393 case 11: /* SQDMLSL, SQDMLSL2 */
7394 case 13: /* SQDMULL, SQDMULL2 */
7395 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
7396 gen_helper_neon_addl_saturate_s64(tcg_passres, cpu_env,
7397 tcg_passres, tcg_passres);
7398 break;
Peter Maydella08582f2014-01-31 14:47:36 +00007399 default:
7400 g_assert_not_reached();
7401 }
7402
Peter Maydell70d7f982014-02-20 10:35:55 +00007403 if (opcode == 9 || opcode == 11) {
7404 /* saturating accumulate ops */
7405 if (accop < 0) {
7406 tcg_gen_neg_i64(tcg_passres, tcg_passres);
7407 }
7408 gen_helper_neon_addl_saturate_s64(tcg_res[pass], cpu_env,
7409 tcg_res[pass], tcg_passres);
7410 } else if (accop > 0) {
Peter Maydella08582f2014-01-31 14:47:36 +00007411 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
Peter Maydella08582f2014-01-31 14:47:36 +00007412 } else if (accop < 0) {
7413 tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
Peter Maydell70d7f982014-02-20 10:35:55 +00007414 }
7415
7416 if (accop != 0) {
Peter Maydella08582f2014-01-31 14:47:36 +00007417 tcg_temp_free_i64(tcg_passres);
7418 }
7419
7420 tcg_temp_free_i64(tcg_op1);
7421 tcg_temp_free_i64(tcg_op2);
7422 }
7423 } else {
7424 /* size 0 or 1, generally helper functions */
7425 for (pass = 0; pass < 2; pass++) {
7426 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
7427 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
7428 TCGv_i64 tcg_passres;
7429 int elt = pass + is_q * 2;
7430
7431 read_vec_element_i32(s, tcg_op1, rn, elt, MO_32);
7432 read_vec_element_i32(s, tcg_op2, rm, elt, MO_32);
7433
7434 if (accop == 0) {
7435 tcg_passres = tcg_res[pass];
7436 } else {
7437 tcg_passres = tcg_temp_new_i64();
7438 }
7439
7440 switch (opcode) {
Peter Maydell70d7f982014-02-20 10:35:55 +00007441 case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
7442 case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
7443 {
7444 TCGv_i64 tcg_op2_64 = tcg_temp_new_i64();
7445 static NeonGenWidenFn * const widenfns[2][2] = {
7446 { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
7447 { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
7448 };
7449 NeonGenWidenFn *widenfn = widenfns[size][is_u];
7450
7451 widenfn(tcg_op2_64, tcg_op2);
7452 widenfn(tcg_passres, tcg_op1);
7453 gen_neon_addl(size, (opcode == 2), tcg_passres,
7454 tcg_passres, tcg_op2_64);
7455 tcg_temp_free_i64(tcg_op2_64);
7456 break;
7457 }
Peter Maydell0ae39322014-01-31 14:47:36 +00007458 case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
7459 case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
7460 if (size == 0) {
7461 if (is_u) {
7462 gen_helper_neon_abdl_u16(tcg_passres, tcg_op1, tcg_op2);
7463 } else {
7464 gen_helper_neon_abdl_s16(tcg_passres, tcg_op1, tcg_op2);
7465 }
7466 } else {
7467 if (is_u) {
7468 gen_helper_neon_abdl_u32(tcg_passres, tcg_op1, tcg_op2);
7469 } else {
7470 gen_helper_neon_abdl_s32(tcg_passres, tcg_op1, tcg_op2);
7471 }
7472 }
7473 break;
Peter Maydella08582f2014-01-31 14:47:36 +00007474 case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
7475 case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
7476 case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
7477 if (size == 0) {
7478 if (is_u) {
7479 gen_helper_neon_mull_u8(tcg_passres, tcg_op1, tcg_op2);
7480 } else {
7481 gen_helper_neon_mull_s8(tcg_passres, tcg_op1, tcg_op2);
7482 }
7483 } else {
7484 if (is_u) {
7485 gen_helper_neon_mull_u16(tcg_passres, tcg_op1, tcg_op2);
7486 } else {
7487 gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
7488 }
7489 }
7490 break;
Peter Maydell70d7f982014-02-20 10:35:55 +00007491 case 9: /* SQDMLAL, SQDMLAL2 */
7492 case 11: /* SQDMLSL, SQDMLSL2 */
7493 case 13: /* SQDMULL, SQDMULL2 */
7494 assert(size == 1);
7495 gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
7496 gen_helper_neon_addl_saturate_s32(tcg_passres, cpu_env,
7497 tcg_passres, tcg_passres);
7498 break;
Peter Maydell6c13ec22014-02-27 12:03:17 +00007499 case 14: /* PMULL */
7500 assert(size == 0);
7501 gen_helper_neon_mull_p8(tcg_passres, tcg_op1, tcg_op2);
7502 break;
Peter Maydella08582f2014-01-31 14:47:36 +00007503 default:
7504 g_assert_not_reached();
7505 }
7506 tcg_temp_free_i32(tcg_op1);
7507 tcg_temp_free_i32(tcg_op2);
7508
Peter Maydell70d7f982014-02-20 10:35:55 +00007509 if (accop != 0) {
7510 if (opcode == 9 || opcode == 11) {
7511 /* saturating accumulate ops */
7512 if (accop < 0) {
7513 gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
7514 }
7515 gen_helper_neon_addl_saturate_s32(tcg_res[pass], cpu_env,
7516 tcg_res[pass],
7517 tcg_passres);
Peter Maydella08582f2014-01-31 14:47:36 +00007518 } else {
Peter Maydell70d7f982014-02-20 10:35:55 +00007519 gen_neon_addl(size, (accop < 0), tcg_res[pass],
7520 tcg_res[pass], tcg_passres);
Peter Maydella08582f2014-01-31 14:47:36 +00007521 }
7522 tcg_temp_free_i64(tcg_passres);
7523 }
7524 }
7525 }
7526
7527 write_vec_element(s, tcg_res[0], rd, 0, MO_64);
7528 write_vec_element(s, tcg_res[1], rd, 1, MO_64);
7529 tcg_temp_free_i64(tcg_res[0]);
7530 tcg_temp_free_i64(tcg_res[1]);
7531}
7532
Peter Maydelldfc15c72014-02-20 10:35:56 +00007533static void handle_3rd_wide(DisasContext *s, int is_q, int is_u, int size,
7534 int opcode, int rd, int rn, int rm)
7535{
7536 TCGv_i64 tcg_res[2];
7537 int part = is_q ? 2 : 0;
7538 int pass;
7539
7540 for (pass = 0; pass < 2; pass++) {
7541 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
7542 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
7543 TCGv_i64 tcg_op2_wide = tcg_temp_new_i64();
7544 static NeonGenWidenFn * const widenfns[3][2] = {
7545 { gen_helper_neon_widen_s8, gen_helper_neon_widen_u8 },
7546 { gen_helper_neon_widen_s16, gen_helper_neon_widen_u16 },
7547 { tcg_gen_ext_i32_i64, tcg_gen_extu_i32_i64 },
7548 };
7549 NeonGenWidenFn *widenfn = widenfns[size][is_u];
7550
7551 read_vec_element(s, tcg_op1, rn, pass, MO_64);
7552 read_vec_element_i32(s, tcg_op2, rm, part + pass, MO_32);
7553 widenfn(tcg_op2_wide, tcg_op2);
7554 tcg_temp_free_i32(tcg_op2);
7555 tcg_res[pass] = tcg_temp_new_i64();
7556 gen_neon_addl(size, (opcode == 3),
7557 tcg_res[pass], tcg_op1, tcg_op2_wide);
7558 tcg_temp_free_i64(tcg_op1);
7559 tcg_temp_free_i64(tcg_op2_wide);
7560 }
7561
7562 for (pass = 0; pass < 2; pass++) {
7563 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
7564 tcg_temp_free_i64(tcg_res[pass]);
7565 }
7566}
7567
Peter Maydelle4b998d2014-02-20 10:35:56 +00007568static void do_narrow_high_u32(TCGv_i32 res, TCGv_i64 in)
7569{
7570 tcg_gen_shri_i64(in, in, 32);
7571 tcg_gen_trunc_i64_i32(res, in);
7572}
7573
7574static void do_narrow_round_high_u32(TCGv_i32 res, TCGv_i64 in)
7575{
7576 tcg_gen_addi_i64(in, in, 1U << 31);
7577 do_narrow_high_u32(res, in);
7578}
7579
7580static void handle_3rd_narrowing(DisasContext *s, int is_q, int is_u, int size,
7581 int opcode, int rd, int rn, int rm)
7582{
7583 TCGv_i32 tcg_res[2];
7584 int part = is_q ? 2 : 0;
7585 int pass;
7586
7587 for (pass = 0; pass < 2; pass++) {
7588 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
7589 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
7590 TCGv_i64 tcg_wideres = tcg_temp_new_i64();
7591 static NeonGenNarrowFn * const narrowfns[3][2] = {
7592 { gen_helper_neon_narrow_high_u8,
7593 gen_helper_neon_narrow_round_high_u8 },
7594 { gen_helper_neon_narrow_high_u16,
7595 gen_helper_neon_narrow_round_high_u16 },
7596 { do_narrow_high_u32, do_narrow_round_high_u32 },
7597 };
7598 NeonGenNarrowFn *gennarrow = narrowfns[size][is_u];
7599
7600 read_vec_element(s, tcg_op1, rn, pass, MO_64);
7601 read_vec_element(s, tcg_op2, rm, pass, MO_64);
7602
7603 gen_neon_addl(size, (opcode == 6), tcg_wideres, tcg_op1, tcg_op2);
7604
7605 tcg_temp_free_i64(tcg_op1);
7606 tcg_temp_free_i64(tcg_op2);
7607
7608 tcg_res[pass] = tcg_temp_new_i32();
7609 gennarrow(tcg_res[pass], tcg_wideres);
7610 tcg_temp_free_i64(tcg_wideres);
7611 }
7612
7613 for (pass = 0; pass < 2; pass++) {
7614 write_vec_element_i32(s, tcg_res[pass], rd, pass + part, MO_32);
7615 tcg_temp_free_i32(tcg_res[pass]);
7616 }
7617 if (!is_q) {
7618 clear_vec_high(s, rd);
7619 }
7620}
7621
Peter Maydell6c13ec22014-02-27 12:03:17 +00007622static void handle_pmull_64(DisasContext *s, int is_q, int rd, int rn, int rm)
7623{
7624 /* PMULL of 64 x 64 -> 128 is an odd special case because it
7625 * is the only three-reg-diff instruction which produces a
7626 * 128-bit wide result from a single operation. However since
7627 * it's possible to calculate the two halves more or less
7628 * separately we just use two helper calls.
7629 */
7630 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
7631 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
7632 TCGv_i64 tcg_res = tcg_temp_new_i64();
7633
7634 read_vec_element(s, tcg_op1, rn, is_q, MO_64);
7635 read_vec_element(s, tcg_op2, rm, is_q, MO_64);
7636 gen_helper_neon_pmull_64_lo(tcg_res, tcg_op1, tcg_op2);
7637 write_vec_element(s, tcg_res, rd, 0, MO_64);
7638 gen_helper_neon_pmull_64_hi(tcg_res, tcg_op1, tcg_op2);
7639 write_vec_element(s, tcg_res, rd, 1, MO_64);
7640
7641 tcg_temp_free_i64(tcg_op1);
7642 tcg_temp_free_i64(tcg_op2);
7643 tcg_temp_free_i64(tcg_res);
7644}
7645
Alex Bennée384b26f2014-01-31 14:47:30 +00007646/* C3.6.15 AdvSIMD three different
7647 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 0
7648 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
7649 * | 0 | Q | U | 0 1 1 1 0 | size | 1 | Rm | opcode | 0 0 | Rn | Rd |
7650 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
7651 */
7652static void disas_simd_three_reg_diff(DisasContext *s, uint32_t insn)
7653{
Peter Maydella08582f2014-01-31 14:47:36 +00007654 /* Instructions in this group fall into three basic classes
7655 * (in each case with the operation working on each element in
7656 * the input vectors):
7657 * (1) widening 64 x 64 -> 128 (with possibly Vd as an extra
7658 * 128 bit input)
7659 * (2) wide 64 x 128 -> 128
7660 * (3) narrowing 128 x 128 -> 64
7661 * Here we do initial decode, catch unallocated cases and
7662 * dispatch to separate functions for each class.
7663 */
7664 int is_q = extract32(insn, 30, 1);
7665 int is_u = extract32(insn, 29, 1);
7666 int size = extract32(insn, 22, 2);
7667 int opcode = extract32(insn, 12, 4);
7668 int rm = extract32(insn, 16, 5);
7669 int rn = extract32(insn, 5, 5);
7670 int rd = extract32(insn, 0, 5);
7671
7672 switch (opcode) {
7673 case 1: /* SADDW, SADDW2, UADDW, UADDW2 */
7674 case 3: /* SSUBW, SSUBW2, USUBW, USUBW2 */
7675 /* 64 x 128 -> 128 */
Peter Maydelldfc15c72014-02-20 10:35:56 +00007676 if (size == 3) {
7677 unallocated_encoding(s);
7678 return;
7679 }
7680 handle_3rd_wide(s, is_q, is_u, size, opcode, rd, rn, rm);
Peter Maydella08582f2014-01-31 14:47:36 +00007681 break;
7682 case 4: /* ADDHN, ADDHN2, RADDHN, RADDHN2 */
7683 case 6: /* SUBHN, SUBHN2, RSUBHN, RSUBHN2 */
7684 /* 128 x 128 -> 64 */
Peter Maydelle4b998d2014-02-20 10:35:56 +00007685 if (size == 3) {
7686 unallocated_encoding(s);
7687 return;
7688 }
7689 handle_3rd_narrowing(s, is_q, is_u, size, opcode, rd, rn, rm);
Peter Maydella08582f2014-01-31 14:47:36 +00007690 break;
Peter Maydell70d7f982014-02-20 10:35:55 +00007691 case 14: /* PMULL, PMULL2 */
7692 if (is_u || size == 1 || size == 2) {
7693 unallocated_encoding(s);
7694 return;
7695 }
Peter Maydell6c13ec22014-02-27 12:03:17 +00007696 if (size == 3) {
7697 if (!arm_dc_feature(s, ARM_FEATURE_V8_AES)) {
7698 unallocated_encoding(s);
7699 return;
7700 }
7701 handle_pmull_64(s, is_q, rd, rn, rm);
7702 return;
7703 }
7704 goto is_widening;
Peter Maydell13caf1f2014-02-20 10:35:55 +00007705 case 9: /* SQDMLAL, SQDMLAL2 */
7706 case 11: /* SQDMLSL, SQDMLSL2 */
7707 case 13: /* SQDMULL, SQDMULL2 */
Peter Maydell70d7f982014-02-20 10:35:55 +00007708 if (is_u || size == 0) {
Peter Maydella08582f2014-01-31 14:47:36 +00007709 unallocated_encoding(s);
7710 return;
7711 }
7712 /* fall through */
Peter Maydell13caf1f2014-02-20 10:35:55 +00007713 case 0: /* SADDL, SADDL2, UADDL, UADDL2 */
7714 case 2: /* SSUBL, SSUBL2, USUBL, USUBL2 */
Peter Maydell13caf1f2014-02-20 10:35:55 +00007715 case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
7716 case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
7717 case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
7718 case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
7719 case 12: /* SMULL, SMULL2, UMULL, UMULL2 */
Peter Maydella08582f2014-01-31 14:47:36 +00007720 /* 64 x 64 -> 128 */
7721 if (size == 3) {
7722 unallocated_encoding(s);
7723 return;
7724 }
Peter Maydell6c13ec22014-02-27 12:03:17 +00007725 is_widening:
Peter Maydella08582f2014-01-31 14:47:36 +00007726 handle_3rd_widening(s, is_q, is_u, size, opcode, rd, rn, rm);
7727 break;
7728 default:
7729 /* opcode 15 not allocated */
7730 unallocated_encoding(s);
7731 break;
7732 }
Alex Bennée384b26f2014-01-31 14:47:30 +00007733}
7734
Peter Maydelle1cea112014-01-31 14:47:37 +00007735/* Logic op (opcode == 3) subgroup of C3.6.16. */
7736static void disas_simd_3same_logic(DisasContext *s, uint32_t insn)
7737{
Peter Maydell956d2722014-01-31 14:47:37 +00007738 int rd = extract32(insn, 0, 5);
7739 int rn = extract32(insn, 5, 5);
7740 int rm = extract32(insn, 16, 5);
7741 int size = extract32(insn, 22, 2);
7742 bool is_u = extract32(insn, 29, 1);
7743 bool is_q = extract32(insn, 30, 1);
7744 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
7745 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
7746 TCGv_i64 tcg_res[2];
7747 int pass;
7748
7749 tcg_res[0] = tcg_temp_new_i64();
7750 tcg_res[1] = tcg_temp_new_i64();
7751
7752 for (pass = 0; pass < (is_q ? 2 : 1); pass++) {
7753 read_vec_element(s, tcg_op1, rn, pass, MO_64);
7754 read_vec_element(s, tcg_op2, rm, pass, MO_64);
7755
7756 if (!is_u) {
7757 switch (size) {
7758 case 0: /* AND */
7759 tcg_gen_and_i64(tcg_res[pass], tcg_op1, tcg_op2);
7760 break;
7761 case 1: /* BIC */
7762 tcg_gen_andc_i64(tcg_res[pass], tcg_op1, tcg_op2);
7763 break;
7764 case 2: /* ORR */
7765 tcg_gen_or_i64(tcg_res[pass], tcg_op1, tcg_op2);
7766 break;
7767 case 3: /* ORN */
7768 tcg_gen_orc_i64(tcg_res[pass], tcg_op1, tcg_op2);
7769 break;
7770 }
7771 } else {
7772 if (size != 0) {
7773 /* B* ops need res loaded to operate on */
7774 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
7775 }
7776
7777 switch (size) {
7778 case 0: /* EOR */
7779 tcg_gen_xor_i64(tcg_res[pass], tcg_op1, tcg_op2);
7780 break;
7781 case 1: /* BSL bitwise select */
7782 tcg_gen_xor_i64(tcg_op1, tcg_op1, tcg_op2);
7783 tcg_gen_and_i64(tcg_op1, tcg_op1, tcg_res[pass]);
7784 tcg_gen_xor_i64(tcg_res[pass], tcg_op2, tcg_op1);
7785 break;
7786 case 2: /* BIT, bitwise insert if true */
7787 tcg_gen_xor_i64(tcg_op1, tcg_op1, tcg_res[pass]);
7788 tcg_gen_and_i64(tcg_op1, tcg_op1, tcg_op2);
7789 tcg_gen_xor_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
7790 break;
7791 case 3: /* BIF, bitwise insert if false */
7792 tcg_gen_xor_i64(tcg_op1, tcg_op1, tcg_res[pass]);
7793 tcg_gen_andc_i64(tcg_op1, tcg_op1, tcg_op2);
7794 tcg_gen_xor_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
7795 break;
7796 }
7797 }
7798 }
7799
7800 write_vec_element(s, tcg_res[0], rd, 0, MO_64);
7801 if (!is_q) {
7802 tcg_gen_movi_i64(tcg_res[1], 0);
7803 }
7804 write_vec_element(s, tcg_res[1], rd, 1, MO_64);
7805
7806 tcg_temp_free_i64(tcg_op1);
7807 tcg_temp_free_i64(tcg_op2);
7808 tcg_temp_free_i64(tcg_res[0]);
7809 tcg_temp_free_i64(tcg_res[1]);
Peter Maydelle1cea112014-01-31 14:47:37 +00007810}
7811
Peter Maydell8b12a0c2014-02-08 14:46:55 +00007812/* Helper functions for 32 bit comparisons */
7813static void gen_max_s32(TCGv_i32 res, TCGv_i32 op1, TCGv_i32 op2)
7814{
7815 tcg_gen_movcond_i32(TCG_COND_GE, res, op1, op2, op1, op2);
7816}
7817
7818static void gen_max_u32(TCGv_i32 res, TCGv_i32 op1, TCGv_i32 op2)
7819{
7820 tcg_gen_movcond_i32(TCG_COND_GEU, res, op1, op2, op1, op2);
7821}
7822
7823static void gen_min_s32(TCGv_i32 res, TCGv_i32 op1, TCGv_i32 op2)
7824{
7825 tcg_gen_movcond_i32(TCG_COND_LE, res, op1, op2, op1, op2);
7826}
7827
7828static void gen_min_u32(TCGv_i32 res, TCGv_i32 op1, TCGv_i32 op2)
7829{
7830 tcg_gen_movcond_i32(TCG_COND_LEU, res, op1, op2, op1, op2);
7831}
7832
Alex Bennéebc242f92014-02-20 10:35:50 +00007833/* Pairwise op subgroup of C3.6.16.
7834 *
7835 * This is called directly or via the handle_3same_float for float pairwise
7836 * operations where the opcode and size are calculated differently.
7837 */
7838static void handle_simd_3same_pair(DisasContext *s, int is_q, int u, int opcode,
7839 int size, int rn, int rm, int rd)
Peter Maydelle1cea112014-01-31 14:47:37 +00007840{
Alex Bennéebc242f92014-02-20 10:35:50 +00007841 TCGv_ptr fpst;
Peter Maydell0173a002014-02-08 14:46:55 +00007842 int pass;
7843
Alex Bennéebc242f92014-02-20 10:35:50 +00007844 /* Floating point operations need fpst */
7845 if (opcode >= 0x58) {
7846 fpst = get_fpstatus_ptr();
7847 } else {
7848 TCGV_UNUSED_PTR(fpst);
Peter Maydell0173a002014-02-08 14:46:55 +00007849 }
7850
7851 /* These operations work on the concatenated rm:rn, with each pair of
7852 * adjacent elements being operated on to produce an element in the result.
7853 */
7854 if (size == 3) {
7855 TCGv_i64 tcg_res[2];
7856
7857 for (pass = 0; pass < 2; pass++) {
7858 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
7859 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
7860 int passreg = (pass == 0) ? rn : rm;
7861
7862 read_vec_element(s, tcg_op1, passreg, 0, MO_64);
7863 read_vec_element(s, tcg_op2, passreg, 1, MO_64);
7864 tcg_res[pass] = tcg_temp_new_i64();
7865
Alex Bennéebc242f92014-02-20 10:35:50 +00007866 switch (opcode) {
7867 case 0x17: /* ADDP */
7868 tcg_gen_add_i64(tcg_res[pass], tcg_op1, tcg_op2);
7869 break;
7870 case 0x58: /* FMAXNMP */
7871 gen_helper_vfp_maxnumd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
7872 break;
7873 case 0x5a: /* FADDP */
7874 gen_helper_vfp_addd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
7875 break;
7876 case 0x5e: /* FMAXP */
7877 gen_helper_vfp_maxd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
7878 break;
7879 case 0x78: /* FMINNMP */
7880 gen_helper_vfp_minnumd(tcg_res[pass], tcg_op1, tcg_op2, fpst);
7881 break;
7882 case 0x7e: /* FMINP */
7883 gen_helper_vfp_mind(tcg_res[pass], tcg_op1, tcg_op2, fpst);
7884 break;
7885 default:
7886 g_assert_not_reached();
7887 }
Peter Maydell0173a002014-02-08 14:46:55 +00007888
7889 tcg_temp_free_i64(tcg_op1);
7890 tcg_temp_free_i64(tcg_op2);
7891 }
7892
7893 for (pass = 0; pass < 2; pass++) {
7894 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
7895 tcg_temp_free_i64(tcg_res[pass]);
7896 }
7897 } else {
7898 int maxpass = is_q ? 4 : 2;
7899 TCGv_i32 tcg_res[4];
7900
7901 for (pass = 0; pass < maxpass; pass++) {
7902 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
7903 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
Alex Bennéebc242f92014-02-20 10:35:50 +00007904 NeonGenTwoOpFn *genfn = NULL;
Peter Maydell0173a002014-02-08 14:46:55 +00007905 int passreg = pass < (maxpass / 2) ? rn : rm;
7906 int passelt = (is_q && (pass & 1)) ? 2 : 0;
7907
7908 read_vec_element_i32(s, tcg_op1, passreg, passelt, MO_32);
7909 read_vec_element_i32(s, tcg_op2, passreg, passelt + 1, MO_32);
7910 tcg_res[pass] = tcg_temp_new_i32();
7911
7912 switch (opcode) {
7913 case 0x17: /* ADDP */
7914 {
7915 static NeonGenTwoOpFn * const fns[3] = {
7916 gen_helper_neon_padd_u8,
7917 gen_helper_neon_padd_u16,
7918 tcg_gen_add_i32,
7919 };
7920 genfn = fns[size];
7921 break;
7922 }
7923 case 0x14: /* SMAXP, UMAXP */
7924 {
7925 static NeonGenTwoOpFn * const fns[3][2] = {
7926 { gen_helper_neon_pmax_s8, gen_helper_neon_pmax_u8 },
7927 { gen_helper_neon_pmax_s16, gen_helper_neon_pmax_u16 },
7928 { gen_max_s32, gen_max_u32 },
7929 };
7930 genfn = fns[size][u];
7931 break;
7932 }
7933 case 0x15: /* SMINP, UMINP */
7934 {
7935 static NeonGenTwoOpFn * const fns[3][2] = {
7936 { gen_helper_neon_pmin_s8, gen_helper_neon_pmin_u8 },
7937 { gen_helper_neon_pmin_s16, gen_helper_neon_pmin_u16 },
7938 { gen_min_s32, gen_min_u32 },
7939 };
7940 genfn = fns[size][u];
7941 break;
7942 }
Alex Bennéebc242f92014-02-20 10:35:50 +00007943 /* The FP operations are all on single floats (32 bit) */
7944 case 0x58: /* FMAXNMP */
7945 gen_helper_vfp_maxnums(tcg_res[pass], tcg_op1, tcg_op2, fpst);
7946 break;
7947 case 0x5a: /* FADDP */
7948 gen_helper_vfp_adds(tcg_res[pass], tcg_op1, tcg_op2, fpst);
7949 break;
7950 case 0x5e: /* FMAXP */
7951 gen_helper_vfp_maxs(tcg_res[pass], tcg_op1, tcg_op2, fpst);
7952 break;
7953 case 0x78: /* FMINNMP */
7954 gen_helper_vfp_minnums(tcg_res[pass], tcg_op1, tcg_op2, fpst);
7955 break;
7956 case 0x7e: /* FMINP */
7957 gen_helper_vfp_mins(tcg_res[pass], tcg_op1, tcg_op2, fpst);
7958 break;
Peter Maydell0173a002014-02-08 14:46:55 +00007959 default:
7960 g_assert_not_reached();
7961 }
7962
Alex Bennéebc242f92014-02-20 10:35:50 +00007963 /* FP ops called directly, otherwise call now */
7964 if (genfn) {
7965 genfn(tcg_res[pass], tcg_op1, tcg_op2);
7966 }
Peter Maydell0173a002014-02-08 14:46:55 +00007967
7968 tcg_temp_free_i32(tcg_op1);
7969 tcg_temp_free_i32(tcg_op2);
7970 }
7971
7972 for (pass = 0; pass < maxpass; pass++) {
7973 write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_32);
7974 tcg_temp_free_i32(tcg_res[pass]);
7975 }
7976 if (!is_q) {
7977 clear_vec_high(s, rd);
7978 }
7979 }
Alex Bennéebc242f92014-02-20 10:35:50 +00007980
7981 if (!TCGV_IS_UNUSED_PTR(fpst)) {
7982 tcg_temp_free_ptr(fpst);
7983 }
Peter Maydelle1cea112014-01-31 14:47:37 +00007984}
7985
7986/* Floating point op subgroup of C3.6.16. */
7987static void disas_simd_3same_float(DisasContext *s, uint32_t insn)
7988{
Peter Maydell845ea092014-01-31 14:47:37 +00007989 /* For floating point ops, the U, size[1] and opcode bits
7990 * together indicate the operation. size[0] indicates single
7991 * or double.
7992 */
7993 int fpopcode = extract32(insn, 11, 5)
7994 | (extract32(insn, 23, 1) << 5)
7995 | (extract32(insn, 29, 1) << 6);
7996 int is_q = extract32(insn, 30, 1);
7997 int size = extract32(insn, 22, 1);
7998 int rm = extract32(insn, 16, 5);
7999 int rn = extract32(insn, 5, 5);
8000 int rd = extract32(insn, 0, 5);
8001
8002 int datasize = is_q ? 128 : 64;
8003 int esize = 32 << size;
8004 int elements = datasize / esize;
8005
8006 if (size == 1 && !is_q) {
8007 unallocated_encoding(s);
8008 return;
8009 }
8010
8011 switch (fpopcode) {
8012 case 0x58: /* FMAXNMP */
8013 case 0x5a: /* FADDP */
8014 case 0x5e: /* FMAXP */
8015 case 0x78: /* FMINNMP */
8016 case 0x7e: /* FMINP */
Alex Bennéebc242f92014-02-20 10:35:50 +00008017 if (size && !is_q) {
8018 unallocated_encoding(s);
8019 return;
8020 }
8021 handle_simd_3same_pair(s, is_q, 0, fpopcode, size ? MO_64 : MO_32,
8022 rn, rm, rd);
Peter Maydell845ea092014-01-31 14:47:37 +00008023 return;
8024 case 0x1b: /* FMULX */
Peter Maydell845ea092014-01-31 14:47:37 +00008025 case 0x1f: /* FRECPS */
8026 case 0x3f: /* FRSQRTS */
Peter Maydell845ea092014-01-31 14:47:37 +00008027 case 0x5d: /* FACGE */
Peter Maydell845ea092014-01-31 14:47:37 +00008028 case 0x7d: /* FACGT */
8029 case 0x19: /* FMLA */
8030 case 0x39: /* FMLS */
Peter Maydell845ea092014-01-31 14:47:37 +00008031 case 0x18: /* FMAXNM */
8032 case 0x1a: /* FADD */
Alex Bennée8908f4d2014-02-20 10:35:49 +00008033 case 0x1c: /* FCMEQ */
Peter Maydell845ea092014-01-31 14:47:37 +00008034 case 0x1e: /* FMAX */
8035 case 0x38: /* FMINNM */
8036 case 0x3a: /* FSUB */
8037 case 0x3e: /* FMIN */
8038 case 0x5b: /* FMUL */
Alex Bennée8908f4d2014-02-20 10:35:49 +00008039 case 0x5c: /* FCMGE */
Peter Maydell845ea092014-01-31 14:47:37 +00008040 case 0x5f: /* FDIV */
8041 case 0x7a: /* FABD */
Alex Bennée8908f4d2014-02-20 10:35:49 +00008042 case 0x7c: /* FCMGT */
Peter Maydell845ea092014-01-31 14:47:37 +00008043 handle_3same_float(s, size, elements, fpopcode, rd, rn, rm);
8044 return;
8045 default:
8046 unallocated_encoding(s);
8047 return;
8048 }
Peter Maydelle1cea112014-01-31 14:47:37 +00008049}
8050
8051/* Integer op subgroup of C3.6.16. */
8052static void disas_simd_3same_int(DisasContext *s, uint32_t insn)
8053{
Peter Maydell1f8a73a2014-01-31 14:47:37 +00008054 int is_q = extract32(insn, 30, 1);
8055 int u = extract32(insn, 29, 1);
8056 int size = extract32(insn, 22, 2);
8057 int opcode = extract32(insn, 11, 5);
8058 int rm = extract32(insn, 16, 5);
8059 int rn = extract32(insn, 5, 5);
8060 int rd = extract32(insn, 0, 5);
8061 int pass;
8062
8063 switch (opcode) {
8064 case 0x13: /* MUL, PMUL */
8065 if (u && size != 0) {
8066 unallocated_encoding(s);
8067 return;
8068 }
8069 /* fall through */
8070 case 0x0: /* SHADD, UHADD */
8071 case 0x2: /* SRHADD, URHADD */
8072 case 0x4: /* SHSUB, UHSUB */
8073 case 0xc: /* SMAX, UMAX */
8074 case 0xd: /* SMIN, UMIN */
8075 case 0xe: /* SABD, UABD */
8076 case 0xf: /* SABA, UABA */
8077 case 0x12: /* MLA, MLS */
8078 if (size == 3) {
8079 unallocated_encoding(s);
8080 return;
8081 }
Peter Maydell8b12a0c2014-02-08 14:46:55 +00008082 break;
Peter Maydell1f8a73a2014-01-31 14:47:37 +00008083 case 0x16: /* SQDMULH, SQRDMULH */
8084 if (size == 0 || size == 3) {
8085 unallocated_encoding(s);
8086 return;
8087 }
Peter Maydell8b12a0c2014-02-08 14:46:55 +00008088 break;
Peter Maydell1f8a73a2014-01-31 14:47:37 +00008089 default:
8090 if (size == 3 && !is_q) {
8091 unallocated_encoding(s);
8092 return;
8093 }
8094 break;
8095 }
8096
8097 if (size == 3) {
8098 for (pass = 0; pass < (is_q ? 2 : 1); pass++) {
8099 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8100 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
8101 TCGv_i64 tcg_res = tcg_temp_new_i64();
8102
8103 read_vec_element(s, tcg_op1, rn, pass, MO_64);
8104 read_vec_element(s, tcg_op2, rm, pass, MO_64);
8105
8106 handle_3same_64(s, opcode, u, tcg_res, tcg_op1, tcg_op2);
8107
8108 write_vec_element(s, tcg_res, rd, pass, MO_64);
8109
8110 tcg_temp_free_i64(tcg_res);
8111 tcg_temp_free_i64(tcg_op1);
8112 tcg_temp_free_i64(tcg_op2);
8113 }
8114 } else {
8115 for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
8116 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
8117 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
8118 TCGv_i32 tcg_res = tcg_temp_new_i32();
Peter Maydell6d9571f2014-02-08 14:46:55 +00008119 NeonGenTwoOpFn *genfn = NULL;
8120 NeonGenTwoOpEnvFn *genenvfn = NULL;
Peter Maydell1f8a73a2014-01-31 14:47:37 +00008121
8122 read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
8123 read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
8124
8125 switch (opcode) {
Peter Maydell8b12a0c2014-02-08 14:46:55 +00008126 case 0x0: /* SHADD, UHADD */
8127 {
8128 static NeonGenTwoOpFn * const fns[3][2] = {
8129 { gen_helper_neon_hadd_s8, gen_helper_neon_hadd_u8 },
8130 { gen_helper_neon_hadd_s16, gen_helper_neon_hadd_u16 },
8131 { gen_helper_neon_hadd_s32, gen_helper_neon_hadd_u32 },
8132 };
8133 genfn = fns[size][u];
8134 break;
8135 }
Peter Maydell6d9571f2014-02-08 14:46:55 +00008136 case 0x1: /* SQADD, UQADD */
8137 {
8138 static NeonGenTwoOpEnvFn * const fns[3][2] = {
8139 { gen_helper_neon_qadd_s8, gen_helper_neon_qadd_u8 },
8140 { gen_helper_neon_qadd_s16, gen_helper_neon_qadd_u16 },
8141 { gen_helper_neon_qadd_s32, gen_helper_neon_qadd_u32 },
8142 };
8143 genenvfn = fns[size][u];
8144 break;
8145 }
Peter Maydell8b12a0c2014-02-08 14:46:55 +00008146 case 0x2: /* SRHADD, URHADD */
8147 {
8148 static NeonGenTwoOpFn * const fns[3][2] = {
8149 { gen_helper_neon_rhadd_s8, gen_helper_neon_rhadd_u8 },
8150 { gen_helper_neon_rhadd_s16, gen_helper_neon_rhadd_u16 },
8151 { gen_helper_neon_rhadd_s32, gen_helper_neon_rhadd_u32 },
8152 };
8153 genfn = fns[size][u];
8154 break;
8155 }
8156 case 0x4: /* SHSUB, UHSUB */
8157 {
8158 static NeonGenTwoOpFn * const fns[3][2] = {
8159 { gen_helper_neon_hsub_s8, gen_helper_neon_hsub_u8 },
8160 { gen_helper_neon_hsub_s16, gen_helper_neon_hsub_u16 },
8161 { gen_helper_neon_hsub_s32, gen_helper_neon_hsub_u32 },
8162 };
8163 genfn = fns[size][u];
8164 break;
8165 }
Peter Maydell6d9571f2014-02-08 14:46:55 +00008166 case 0x5: /* SQSUB, UQSUB */
8167 {
8168 static NeonGenTwoOpEnvFn * const fns[3][2] = {
8169 { gen_helper_neon_qsub_s8, gen_helper_neon_qsub_u8 },
8170 { gen_helper_neon_qsub_s16, gen_helper_neon_qsub_u16 },
8171 { gen_helper_neon_qsub_s32, gen_helper_neon_qsub_u32 },
8172 };
8173 genenvfn = fns[size][u];
8174 break;
8175 }
Peter Maydell1f8a73a2014-01-31 14:47:37 +00008176 case 0x6: /* CMGT, CMHI */
8177 {
8178 static NeonGenTwoOpFn * const fns[3][2] = {
8179 { gen_helper_neon_cgt_s8, gen_helper_neon_cgt_u8 },
8180 { gen_helper_neon_cgt_s16, gen_helper_neon_cgt_u16 },
8181 { gen_helper_neon_cgt_s32, gen_helper_neon_cgt_u32 },
8182 };
8183 genfn = fns[size][u];
8184 break;
8185 }
8186 case 0x7: /* CMGE, CMHS */
8187 {
8188 static NeonGenTwoOpFn * const fns[3][2] = {
8189 { gen_helper_neon_cge_s8, gen_helper_neon_cge_u8 },
8190 { gen_helper_neon_cge_s16, gen_helper_neon_cge_u16 },
8191 { gen_helper_neon_cge_s32, gen_helper_neon_cge_u32 },
8192 };
8193 genfn = fns[size][u];
8194 break;
8195 }
Peter Maydell6d9571f2014-02-08 14:46:55 +00008196 case 0x8: /* SSHL, USHL */
8197 {
8198 static NeonGenTwoOpFn * const fns[3][2] = {
8199 { gen_helper_neon_shl_s8, gen_helper_neon_shl_u8 },
8200 { gen_helper_neon_shl_s16, gen_helper_neon_shl_u16 },
8201 { gen_helper_neon_shl_s32, gen_helper_neon_shl_u32 },
8202 };
8203 genfn = fns[size][u];
8204 break;
8205 }
8206 case 0x9: /* SQSHL, UQSHL */
8207 {
8208 static NeonGenTwoOpEnvFn * const fns[3][2] = {
8209 { gen_helper_neon_qshl_s8, gen_helper_neon_qshl_u8 },
8210 { gen_helper_neon_qshl_s16, gen_helper_neon_qshl_u16 },
8211 { gen_helper_neon_qshl_s32, gen_helper_neon_qshl_u32 },
8212 };
8213 genenvfn = fns[size][u];
8214 break;
8215 }
8216 case 0xa: /* SRSHL, URSHL */
8217 {
8218 static NeonGenTwoOpFn * const fns[3][2] = {
8219 { gen_helper_neon_rshl_s8, gen_helper_neon_rshl_u8 },
8220 { gen_helper_neon_rshl_s16, gen_helper_neon_rshl_u16 },
8221 { gen_helper_neon_rshl_s32, gen_helper_neon_rshl_u32 },
8222 };
8223 genfn = fns[size][u];
8224 break;
8225 }
8226 case 0xb: /* SQRSHL, UQRSHL */
8227 {
8228 static NeonGenTwoOpEnvFn * const fns[3][2] = {
8229 { gen_helper_neon_qrshl_s8, gen_helper_neon_qrshl_u8 },
8230 { gen_helper_neon_qrshl_s16, gen_helper_neon_qrshl_u16 },
8231 { gen_helper_neon_qrshl_s32, gen_helper_neon_qrshl_u32 },
8232 };
8233 genenvfn = fns[size][u];
8234 break;
8235 }
Peter Maydell8b12a0c2014-02-08 14:46:55 +00008236 case 0xc: /* SMAX, UMAX */
8237 {
8238 static NeonGenTwoOpFn * const fns[3][2] = {
8239 { gen_helper_neon_max_s8, gen_helper_neon_max_u8 },
8240 { gen_helper_neon_max_s16, gen_helper_neon_max_u16 },
8241 { gen_max_s32, gen_max_u32 },
8242 };
8243 genfn = fns[size][u];
8244 break;
8245 }
8246
8247 case 0xd: /* SMIN, UMIN */
8248 {
8249 static NeonGenTwoOpFn * const fns[3][2] = {
8250 { gen_helper_neon_min_s8, gen_helper_neon_min_u8 },
8251 { gen_helper_neon_min_s16, gen_helper_neon_min_u16 },
8252 { gen_min_s32, gen_min_u32 },
8253 };
8254 genfn = fns[size][u];
8255 break;
8256 }
8257 case 0xe: /* SABD, UABD */
8258 case 0xf: /* SABA, UABA */
8259 {
8260 static NeonGenTwoOpFn * const fns[3][2] = {
8261 { gen_helper_neon_abd_s8, gen_helper_neon_abd_u8 },
8262 { gen_helper_neon_abd_s16, gen_helper_neon_abd_u16 },
8263 { gen_helper_neon_abd_s32, gen_helper_neon_abd_u32 },
8264 };
8265 genfn = fns[size][u];
8266 break;
8267 }
Peter Maydell1f8a73a2014-01-31 14:47:37 +00008268 case 0x10: /* ADD, SUB */
8269 {
8270 static NeonGenTwoOpFn * const fns[3][2] = {
8271 { gen_helper_neon_add_u8, gen_helper_neon_sub_u8 },
8272 { gen_helper_neon_add_u16, gen_helper_neon_sub_u16 },
8273 { tcg_gen_add_i32, tcg_gen_sub_i32 },
8274 };
8275 genfn = fns[size][u];
8276 break;
8277 }
8278 case 0x11: /* CMTST, CMEQ */
8279 {
8280 static NeonGenTwoOpFn * const fns[3][2] = {
8281 { gen_helper_neon_tst_u8, gen_helper_neon_ceq_u8 },
8282 { gen_helper_neon_tst_u16, gen_helper_neon_ceq_u16 },
8283 { gen_helper_neon_tst_u32, gen_helper_neon_ceq_u32 },
8284 };
8285 genfn = fns[size][u];
8286 break;
8287 }
Peter Maydell8b12a0c2014-02-08 14:46:55 +00008288 case 0x13: /* MUL, PMUL */
8289 if (u) {
8290 /* PMUL */
8291 assert(size == 0);
8292 genfn = gen_helper_neon_mul_p8;
8293 break;
8294 }
8295 /* fall through : MUL */
8296 case 0x12: /* MLA, MLS */
8297 {
8298 static NeonGenTwoOpFn * const fns[3] = {
8299 gen_helper_neon_mul_u8,
8300 gen_helper_neon_mul_u16,
8301 tcg_gen_mul_i32,
8302 };
8303 genfn = fns[size];
8304 break;
8305 }
8306 case 0x16: /* SQDMULH, SQRDMULH */
8307 {
8308 static NeonGenTwoOpEnvFn * const fns[2][2] = {
8309 { gen_helper_neon_qdmulh_s16, gen_helper_neon_qrdmulh_s16 },
8310 { gen_helper_neon_qdmulh_s32, gen_helper_neon_qrdmulh_s32 },
8311 };
8312 assert(size == 1 || size == 2);
8313 genenvfn = fns[size - 1][u];
8314 break;
8315 }
Peter Maydell1f8a73a2014-01-31 14:47:37 +00008316 default:
8317 g_assert_not_reached();
8318 }
8319
Peter Maydell6d9571f2014-02-08 14:46:55 +00008320 if (genenvfn) {
8321 genenvfn(tcg_res, cpu_env, tcg_op1, tcg_op2);
8322 } else {
8323 genfn(tcg_res, tcg_op1, tcg_op2);
8324 }
Peter Maydell1f8a73a2014-01-31 14:47:37 +00008325
Peter Maydell8b12a0c2014-02-08 14:46:55 +00008326 if (opcode == 0xf || opcode == 0x12) {
8327 /* SABA, UABA, MLA, MLS: accumulating ops */
8328 static NeonGenTwoOpFn * const fns[3][2] = {
8329 { gen_helper_neon_add_u8, gen_helper_neon_sub_u8 },
8330 { gen_helper_neon_add_u16, gen_helper_neon_sub_u16 },
8331 { tcg_gen_add_i32, tcg_gen_sub_i32 },
8332 };
8333 bool is_sub = (opcode == 0x12 && u); /* MLS */
8334
8335 genfn = fns[size][is_sub];
8336 read_vec_element_i32(s, tcg_op1, rd, pass, MO_32);
8337 genfn(tcg_res, tcg_res, tcg_op1);
8338 }
8339
Peter Maydell1f8a73a2014-01-31 14:47:37 +00008340 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
8341
8342 tcg_temp_free_i32(tcg_res);
8343 tcg_temp_free_i32(tcg_op1);
8344 tcg_temp_free_i32(tcg_op2);
8345 }
8346 }
8347
8348 if (!is_q) {
8349 clear_vec_high(s, rd);
8350 }
Peter Maydelle1cea112014-01-31 14:47:37 +00008351}
8352
Alex Bennée384b26f2014-01-31 14:47:30 +00008353/* C3.6.16 AdvSIMD three same
8354 * 31 30 29 28 24 23 22 21 20 16 15 11 10 9 5 4 0
8355 * +---+---+---+-----------+------+---+------+--------+---+------+------+
8356 * | 0 | Q | U | 0 1 1 1 0 | size | 1 | Rm | opcode | 1 | Rn | Rd |
8357 * +---+---+---+-----------+------+---+------+--------+---+------+------+
8358 */
8359static void disas_simd_three_reg_same(DisasContext *s, uint32_t insn)
8360{
Peter Maydelle1cea112014-01-31 14:47:37 +00008361 int opcode = extract32(insn, 11, 5);
8362
8363 switch (opcode) {
8364 case 0x3: /* logic ops */
8365 disas_simd_3same_logic(s, insn);
8366 break;
8367 case 0x17: /* ADDP */
8368 case 0x14: /* SMAXP, UMAXP */
8369 case 0x15: /* SMINP, UMINP */
Alex Bennéebc242f92014-02-20 10:35:50 +00008370 {
Peter Maydelle1cea112014-01-31 14:47:37 +00008371 /* Pairwise operations */
Alex Bennéebc242f92014-02-20 10:35:50 +00008372 int is_q = extract32(insn, 30, 1);
8373 int u = extract32(insn, 29, 1);
8374 int size = extract32(insn, 22, 2);
8375 int rm = extract32(insn, 16, 5);
8376 int rn = extract32(insn, 5, 5);
8377 int rd = extract32(insn, 0, 5);
8378 if (opcode == 0x17) {
8379 if (u || (size == 3 && !is_q)) {
8380 unallocated_encoding(s);
8381 return;
8382 }
8383 } else {
8384 if (size == 3) {
8385 unallocated_encoding(s);
8386 return;
8387 }
8388 }
8389 handle_simd_3same_pair(s, is_q, u, opcode, size, rn, rm, rd);
Peter Maydelle1cea112014-01-31 14:47:37 +00008390 break;
Alex Bennéebc242f92014-02-20 10:35:50 +00008391 }
Peter Maydelle1cea112014-01-31 14:47:37 +00008392 case 0x18 ... 0x31:
8393 /* floating point ops, sz[1] and U are part of opcode */
8394 disas_simd_3same_float(s, insn);
8395 break;
8396 default:
8397 disas_simd_3same_int(s, insn);
8398 break;
8399 }
Alex Bennée384b26f2014-01-31 14:47:30 +00008400}
8401
Peter Maydelld980fd52014-02-03 23:31:52 +00008402static void handle_2misc_narrow(DisasContext *s, int opcode, bool u, bool is_q,
8403 int size, int rn, int rd)
8404{
8405 /* Handle 2-reg-misc ops which are narrowing (so each 2*size element
8406 * in the source becomes a size element in the destination).
8407 */
8408 int pass;
8409 TCGv_i32 tcg_res[2];
8410 int destelt = is_q ? 2 : 0;
8411
8412 for (pass = 0; pass < 2; pass++) {
8413 TCGv_i64 tcg_op = tcg_temp_new_i64();
8414 NeonGenNarrowFn *genfn = NULL;
8415 NeonGenNarrowEnvFn *genenvfn = NULL;
8416
8417 read_vec_element(s, tcg_op, rn, pass, MO_64);
8418 tcg_res[pass] = tcg_temp_new_i32();
8419
8420 switch (opcode) {
8421 case 0x12: /* XTN, SQXTUN */
8422 {
8423 static NeonGenNarrowFn * const xtnfns[3] = {
8424 gen_helper_neon_narrow_u8,
8425 gen_helper_neon_narrow_u16,
8426 tcg_gen_trunc_i64_i32,
8427 };
8428 static NeonGenNarrowEnvFn * const sqxtunfns[3] = {
8429 gen_helper_neon_unarrow_sat8,
8430 gen_helper_neon_unarrow_sat16,
8431 gen_helper_neon_unarrow_sat32,
8432 };
8433 if (u) {
8434 genenvfn = sqxtunfns[size];
8435 } else {
8436 genfn = xtnfns[size];
8437 }
8438 break;
8439 }
8440 case 0x14: /* SQXTN, UQXTN */
8441 {
8442 static NeonGenNarrowEnvFn * const fns[3][2] = {
8443 { gen_helper_neon_narrow_sat_s8,
8444 gen_helper_neon_narrow_sat_u8 },
8445 { gen_helper_neon_narrow_sat_s16,
8446 gen_helper_neon_narrow_sat_u16 },
8447 { gen_helper_neon_narrow_sat_s32,
8448 gen_helper_neon_narrow_sat_u32 },
8449 };
8450 genenvfn = fns[size][u];
8451 break;
8452 }
Peter Maydelle49605c2014-03-08 16:13:27 +00008453 case 0x16: /* FCVTN, FCVTN2 */
8454 /* 32 bit to 16 bit or 64 bit to 32 bit float conversion */
8455 if (size == 3) {
8456 gen_helper_vfp_fcvtsd(tcg_res[pass], tcg_op, cpu_env);
8457 } else {
8458 TCGv_i32 tcg_lo = tcg_temp_new_i32();
8459 TCGv_i32 tcg_hi = tcg_temp_new_i32();
8460 tcg_gen_trunc_i64_i32(tcg_lo, tcg_op);
8461 gen_helper_vfp_fcvt_f32_to_f16(tcg_lo, tcg_lo, cpu_env);
8462 tcg_gen_shri_i64(tcg_op, tcg_op, 32);
8463 tcg_gen_trunc_i64_i32(tcg_hi, tcg_op);
8464 gen_helper_vfp_fcvt_f32_to_f16(tcg_hi, tcg_hi, cpu_env);
8465 tcg_gen_deposit_i32(tcg_res[pass], tcg_lo, tcg_hi, 16, 16);
8466 tcg_temp_free_i32(tcg_lo);
8467 tcg_temp_free_i32(tcg_hi);
8468 }
8469 break;
Peter Maydelld980fd52014-02-03 23:31:52 +00008470 default:
8471 g_assert_not_reached();
8472 }
8473
8474 if (genfn) {
8475 genfn(tcg_res[pass], tcg_op);
Peter Maydelle49605c2014-03-08 16:13:27 +00008476 } else if (genenvfn) {
Peter Maydelld980fd52014-02-03 23:31:52 +00008477 genenvfn(tcg_res[pass], cpu_env, tcg_op);
8478 }
8479
8480 tcg_temp_free_i64(tcg_op);
8481 }
8482
8483 for (pass = 0; pass < 2; pass++) {
8484 write_vec_element_i32(s, tcg_res[pass], rd, destelt + pass, MO_32);
8485 tcg_temp_free_i32(tcg_res[pass]);
8486 }
8487 if (!is_q) {
8488 clear_vec_high(s, rd);
8489 }
8490}
8491
Peter Maydell8073eb32014-03-08 17:45:15 +00008492static void handle_2misc_widening(DisasContext *s, int opcode, bool is_q,
8493 int size, int rn, int rd)
8494{
8495 /* Handle 2-reg-misc ops which are widening (so each size element
8496 * in the source becomes a 2*size element in the destination.
8497 * The only instruction like this is FCVTL.
8498 */
8499 int pass;
8500
8501 if (size == 3) {
8502 /* 32 -> 64 bit fp conversion */
8503 TCGv_i64 tcg_res[2];
8504 int srcelt = is_q ? 2 : 0;
8505
8506 for (pass = 0; pass < 2; pass++) {
8507 TCGv_i32 tcg_op = tcg_temp_new_i32();
8508 tcg_res[pass] = tcg_temp_new_i64();
8509
8510 read_vec_element_i32(s, tcg_op, rn, srcelt + pass, MO_32);
8511 gen_helper_vfp_fcvtds(tcg_res[pass], tcg_op, cpu_env);
8512 tcg_temp_free_i32(tcg_op);
8513 }
8514 for (pass = 0; pass < 2; pass++) {
8515 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
8516 tcg_temp_free_i64(tcg_res[pass]);
8517 }
8518 } else {
8519 /* 16 -> 32 bit fp conversion */
8520 int srcelt = is_q ? 4 : 0;
8521 TCGv_i32 tcg_res[4];
8522
8523 for (pass = 0; pass < 4; pass++) {
8524 tcg_res[pass] = tcg_temp_new_i32();
8525
8526 read_vec_element_i32(s, tcg_res[pass], rn, srcelt + pass, MO_16);
8527 gen_helper_vfp_fcvt_f16_to_f32(tcg_res[pass], tcg_res[pass],
8528 cpu_env);
8529 }
8530 for (pass = 0; pass < 4; pass++) {
8531 write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_32);
8532 tcg_temp_free_i32(tcg_res[pass]);
8533 }
8534 }
8535}
8536
Alex Bennée39d82112014-02-03 23:31:52 +00008537static void handle_rev(DisasContext *s, int opcode, bool u,
8538 bool is_q, int size, int rn, int rd)
8539{
8540 int op = (opcode << 1) | u;
8541 int opsz = op + size;
8542 int grp_size = 3 - opsz;
8543 int dsize = is_q ? 128 : 64;
8544 int i;
8545
8546 if (opsz >= 3) {
8547 unallocated_encoding(s);
8548 return;
8549 }
8550
8551 if (size == 0) {
8552 /* Special case bytes, use bswap op on each group of elements */
8553 int groups = dsize / (8 << grp_size);
8554
8555 for (i = 0; i < groups; i++) {
8556 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
8557
8558 read_vec_element(s, tcg_tmp, rn, i, grp_size);
8559 switch (grp_size) {
8560 case MO_16:
8561 tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp);
8562 break;
8563 case MO_32:
8564 tcg_gen_bswap32_i64(tcg_tmp, tcg_tmp);
8565 break;
8566 case MO_64:
8567 tcg_gen_bswap64_i64(tcg_tmp, tcg_tmp);
8568 break;
8569 default:
8570 g_assert_not_reached();
8571 }
8572 write_vec_element(s, tcg_tmp, rd, i, grp_size);
8573 tcg_temp_free_i64(tcg_tmp);
8574 }
8575 if (!is_q) {
8576 clear_vec_high(s, rd);
8577 }
8578 } else {
8579 int revmask = (1 << grp_size) - 1;
8580 int esize = 8 << size;
8581 int elements = dsize / esize;
8582 TCGv_i64 tcg_rn = tcg_temp_new_i64();
8583 TCGv_i64 tcg_rd = tcg_const_i64(0);
8584 TCGv_i64 tcg_rd_hi = tcg_const_i64(0);
8585
8586 for (i = 0; i < elements; i++) {
8587 int e_rev = (i & 0xf) ^ revmask;
8588 int off = e_rev * esize;
8589 read_vec_element(s, tcg_rn, rn, i, size);
8590 if (off >= 64) {
8591 tcg_gen_deposit_i64(tcg_rd_hi, tcg_rd_hi,
8592 tcg_rn, off - 64, esize);
8593 } else {
8594 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, off, esize);
8595 }
8596 }
8597 write_vec_element(s, tcg_rd, rd, 0, MO_64);
8598 write_vec_element(s, tcg_rd_hi, rd, 1, MO_64);
8599
8600 tcg_temp_free_i64(tcg_rd_hi);
8601 tcg_temp_free_i64(tcg_rd);
8602 tcg_temp_free_i64(tcg_rn);
8603 }
8604}
8605
Peter Maydell74ef1972014-03-07 12:06:13 +00008606static void handle_2misc_pairwise(DisasContext *s, int opcode, bool u,
8607 bool is_q, int size, int rn, int rd)
8608{
8609 /* Implement the pairwise operations from 2-misc:
8610 * SADDLP, UADDLP, SADALP, UADALP.
8611 * These all add pairs of elements in the input to produce a
8612 * double-width result element in the output (possibly accumulating).
8613 */
8614 bool accum = (opcode == 0x6);
8615 int maxpass = is_q ? 2 : 1;
8616 int pass;
8617 TCGv_i64 tcg_res[2];
8618
8619 if (size == 2) {
8620 /* 32 + 32 -> 64 op */
8621 TCGMemOp memop = size + (u ? 0 : MO_SIGN);
8622
8623 for (pass = 0; pass < maxpass; pass++) {
8624 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
8625 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
8626
8627 tcg_res[pass] = tcg_temp_new_i64();
8628
8629 read_vec_element(s, tcg_op1, rn, pass * 2, memop);
8630 read_vec_element(s, tcg_op2, rn, pass * 2 + 1, memop);
8631 tcg_gen_add_i64(tcg_res[pass], tcg_op1, tcg_op2);
8632 if (accum) {
8633 read_vec_element(s, tcg_op1, rd, pass, MO_64);
8634 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
8635 }
8636
8637 tcg_temp_free_i64(tcg_op1);
8638 tcg_temp_free_i64(tcg_op2);
8639 }
8640 } else {
8641 for (pass = 0; pass < maxpass; pass++) {
8642 TCGv_i64 tcg_op = tcg_temp_new_i64();
8643 NeonGenOneOpFn *genfn;
8644 static NeonGenOneOpFn * const fns[2][2] = {
8645 { gen_helper_neon_addlp_s8, gen_helper_neon_addlp_u8 },
8646 { gen_helper_neon_addlp_s16, gen_helper_neon_addlp_u16 },
8647 };
8648
8649 genfn = fns[size][u];
8650
8651 tcg_res[pass] = tcg_temp_new_i64();
8652
8653 read_vec_element(s, tcg_op, rn, pass, MO_64);
8654 genfn(tcg_res[pass], tcg_op);
8655
8656 if (accum) {
8657 read_vec_element(s, tcg_op, rd, pass, MO_64);
8658 if (size == 0) {
8659 gen_helper_neon_addl_u16(tcg_res[pass],
8660 tcg_res[pass], tcg_op);
8661 } else {
8662 gen_helper_neon_addl_u32(tcg_res[pass],
8663 tcg_res[pass], tcg_op);
8664 }
8665 }
8666 tcg_temp_free_i64(tcg_op);
8667 }
8668 }
8669 if (!is_q) {
8670 tcg_res[1] = tcg_const_i64(0);
8671 }
8672 for (pass = 0; pass < 2; pass++) {
8673 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
8674 tcg_temp_free_i64(tcg_res[pass]);
8675 }
8676}
8677
Peter Maydell1566d3d2014-03-07 15:22:24 +00008678static void handle_shll(DisasContext *s, bool is_q, int size, int rn, int rd)
8679{
8680 /* Implement SHLL and SHLL2 */
8681 int pass;
8682 int part = is_q ? 2 : 0;
8683 TCGv_i64 tcg_res[2];
8684
8685 for (pass = 0; pass < 2; pass++) {
8686 static NeonGenWidenFn * const widenfns[3] = {
8687 gen_helper_neon_widen_u8,
8688 gen_helper_neon_widen_u16,
8689 tcg_gen_extu_i32_i64,
8690 };
8691 NeonGenWidenFn *widenfn = widenfns[size];
8692 TCGv_i32 tcg_op = tcg_temp_new_i32();
8693
8694 read_vec_element_i32(s, tcg_op, rn, part + pass, MO_32);
8695 tcg_res[pass] = tcg_temp_new_i64();
8696 widenfn(tcg_res[pass], tcg_op);
8697 tcg_gen_shli_i64(tcg_res[pass], tcg_res[pass], 8 << size);
8698
8699 tcg_temp_free_i32(tcg_op);
8700 }
8701
8702 for (pass = 0; pass < 2; pass++) {
8703 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
8704 tcg_temp_free_i64(tcg_res[pass]);
8705 }
8706}
8707
Alex Bennée384b26f2014-01-31 14:47:30 +00008708/* C3.6.17 AdvSIMD two reg misc
8709 * 31 30 29 28 24 23 22 21 17 16 12 11 10 9 5 4 0
8710 * +---+---+---+-----------+------+-----------+--------+-----+------+------+
8711 * | 0 | Q | U | 0 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 | Rn | Rd |
8712 * +---+---+---+-----------+------+-----------+--------+-----+------+------+
8713 */
8714static void disas_simd_two_reg_misc(DisasContext *s, uint32_t insn)
8715{
Peter Maydell45aecc62014-02-08 14:46:56 +00008716 int size = extract32(insn, 22, 2);
8717 int opcode = extract32(insn, 12, 5);
8718 bool u = extract32(insn, 29, 1);
8719 bool is_q = extract32(insn, 30, 1);
Peter Maydell94b6c912014-02-03 23:31:51 +00008720 int rn = extract32(insn, 5, 5);
8721 int rd = extract32(insn, 0, 5);
Peter Maydell416ac202014-03-07 16:49:14 +00008722 bool need_fpstatus = false;
8723 bool need_rmode = false;
8724 int rmode;
8725 TCGv_i32 tcg_rmode;
8726 TCGv_ptr tcg_fpstatus;
Peter Maydell45aecc62014-02-08 14:46:56 +00008727
8728 switch (opcode) {
8729 case 0x0: /* REV64, REV32 */
8730 case 0x1: /* REV16 */
Alex Bennée39d82112014-02-03 23:31:52 +00008731 handle_rev(s, opcode, u, is_q, size, rn, rd);
Peter Maydell45aecc62014-02-08 14:46:56 +00008732 return;
Peter Maydell86cbc412014-02-03 23:31:51 +00008733 case 0x5: /* CNT, NOT, RBIT */
8734 if (u && size == 0) {
8735 /* NOT: adjust size so we can use the 64-bits-at-a-time loop. */
8736 size = 3;
8737 break;
8738 } else if (u && size == 1) {
8739 /* RBIT */
8740 break;
8741 } else if (!u && size == 0) {
8742 /* CNT */
8743 break;
Peter Maydell45aecc62014-02-08 14:46:56 +00008744 }
Peter Maydell86cbc412014-02-03 23:31:51 +00008745 unallocated_encoding(s);
Peter Maydell45aecc62014-02-08 14:46:56 +00008746 return;
Peter Maydelld980fd52014-02-03 23:31:52 +00008747 case 0x12: /* XTN, XTN2, SQXTUN, SQXTUN2 */
8748 case 0x14: /* SQXTN, SQXTN2, UQXTN, UQXTN2 */
8749 if (size == 3) {
8750 unallocated_encoding(s);
8751 return;
8752 }
8753 handle_2misc_narrow(s, opcode, u, is_q, size, rn, rd);
8754 return;
Peter Maydell45aecc62014-02-08 14:46:56 +00008755 case 0x4: /* CLS, CLZ */
Alex Bennée7f8d0b72014-02-25 14:50:33 +00008756 if (size == 3) {
8757 unallocated_encoding(s);
8758 return;
8759 }
8760 break;
8761 case 0x2: /* SADDLP, UADDLP */
Peter Maydell45aecc62014-02-08 14:46:56 +00008762 case 0x6: /* SADALP, UADALP */
Peter Maydell45aecc62014-02-08 14:46:56 +00008763 if (size == 3) {
8764 unallocated_encoding(s);
8765 return;
8766 }
Peter Maydell74ef1972014-03-07 12:06:13 +00008767 handle_2misc_pairwise(s, opcode, u, is_q, size, rn, rd);
Peter Maydell45aecc62014-02-08 14:46:56 +00008768 return;
8769 case 0x13: /* SHLL, SHLL2 */
8770 if (u == 0 || size == 3) {
8771 unallocated_encoding(s);
8772 return;
8773 }
Peter Maydell1566d3d2014-03-07 15:22:24 +00008774 handle_shll(s, is_q, size, rn, rd);
Peter Maydell45aecc62014-02-08 14:46:56 +00008775 return;
8776 case 0xa: /* CMLT */
8777 if (u == 1) {
8778 unallocated_encoding(s);
8779 return;
8780 }
8781 /* fall through */
Peter Maydell45aecc62014-02-08 14:46:56 +00008782 case 0x8: /* CMGT, CMGE */
8783 case 0x9: /* CMEQ, CMLE */
8784 case 0xb: /* ABS, NEG */
8785 if (size == 3 && !is_q) {
8786 unallocated_encoding(s);
8787 return;
8788 }
Peter Maydell94b6c912014-02-03 23:31:51 +00008789 break;
8790 case 0x3: /* SUQADD, USQADD */
8791 case 0x7: /* SQABS, SQNEG */
8792 if (size == 3 && !is_q) {
8793 unallocated_encoding(s);
8794 return;
8795 }
Peter Maydell45aecc62014-02-08 14:46:56 +00008796 unsupported_encoding(s, insn);
8797 return;
8798 case 0xc ... 0xf:
8799 case 0x16 ... 0x1d:
8800 case 0x1f:
8801 {
8802 /* Floating point: U, size[1] and opcode indicate operation;
8803 * size[0] indicates single or double precision.
8804 */
Peter Maydell745849b2014-03-08 20:40:20 +00008805 int is_double = extract32(size, 0, 1);
Peter Maydell45aecc62014-02-08 14:46:56 +00008806 opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
Peter Maydell745849b2014-03-08 20:40:20 +00008807 size = is_double ? 3 : 2;
Peter Maydell45aecc62014-02-08 14:46:56 +00008808 switch (opcode) {
Peter Maydellf93d0132014-02-03 23:31:52 +00008809 case 0x2f: /* FABS */
8810 case 0x6f: /* FNEG */
8811 if (size == 3 && !is_q) {
8812 unallocated_encoding(s);
8813 return;
8814 }
8815 break;
Peter Maydell745849b2014-03-08 20:40:20 +00008816 case 0x1d: /* SCVTF */
8817 case 0x5d: /* UCVTF */
8818 {
8819 bool is_signed = (opcode == 0x1d) ? true : false;
8820 int elements = is_double ? 2 : is_q ? 4 : 2;
8821 if (is_double && !is_q) {
8822 unallocated_encoding(s);
8823 return;
8824 }
8825 handle_simd_intfp_conv(s, rd, rn, elements, is_signed, 0, size);
8826 return;
8827 }
Alex Bennée8908f4d2014-02-20 10:35:49 +00008828 case 0x2c: /* FCMGT (zero) */
8829 case 0x2d: /* FCMEQ (zero) */
8830 case 0x2e: /* FCMLT (zero) */
8831 case 0x6c: /* FCMGE (zero) */
8832 case 0x6d: /* FCMLE (zero) */
8833 if (size == 3 && !is_q) {
8834 unallocated_encoding(s);
8835 return;
8836 }
8837 handle_2misc_fcmp_zero(s, opcode, false, u, is_q, size, rn, rd);
8838 return;
Alex Bennéeb399d6d2014-02-25 12:21:05 +00008839 case 0x7f: /* FSQRT */
8840 if (size == 3 && !is_q) {
8841 unallocated_encoding(s);
8842 return;
8843 }
8844 break;
Peter Maydell416ac202014-03-07 16:49:14 +00008845 case 0x1a: /* FCVTNS */
8846 case 0x1b: /* FCVTMS */
8847 case 0x3a: /* FCVTPS */
8848 case 0x3b: /* FCVTZS */
8849 case 0x5a: /* FCVTNU */
8850 case 0x5b: /* FCVTMU */
8851 case 0x7a: /* FCVTPU */
8852 case 0x7b: /* FCVTZU */
8853 need_fpstatus = true;
8854 need_rmode = true;
8855 rmode = extract32(opcode, 5, 1) | (extract32(opcode, 0, 1) << 1);
8856 if (size == 3 && !is_q) {
8857 unallocated_encoding(s);
8858 return;
8859 }
8860 break;
8861 case 0x5c: /* FCVTAU */
8862 case 0x1c: /* FCVTAS */
8863 need_fpstatus = true;
8864 need_rmode = true;
8865 rmode = FPROUNDING_TIEAWAY;
8866 if (size == 3 && !is_q) {
8867 unallocated_encoding(s);
8868 return;
8869 }
8870 break;
Peter Maydell45aecc62014-02-08 14:46:56 +00008871 case 0x16: /* FCVTN, FCVTN2 */
Peter Maydelle49605c2014-03-08 16:13:27 +00008872 handle_2misc_narrow(s, opcode, 0, is_q, size, rn, rd);
8873 return;
Peter Maydell45aecc62014-02-08 14:46:56 +00008874 case 0x17: /* FCVTL, FCVTL2 */
Peter Maydell8073eb32014-03-08 17:45:15 +00008875 handle_2misc_widening(s, opcode, is_q, size, rn, rd);
8876 return;
Peter Maydell45aecc62014-02-08 14:46:56 +00008877 case 0x18: /* FRINTN */
8878 case 0x19: /* FRINTM */
Peter Maydell45aecc62014-02-08 14:46:56 +00008879 case 0x38: /* FRINTP */
8880 case 0x39: /* FRINTZ */
Peter Maydell45aecc62014-02-08 14:46:56 +00008881 case 0x3c: /* URECPE */
8882 case 0x3d: /* FRECPE */
8883 case 0x56: /* FCVTXN, FCVTXN2 */
8884 case 0x58: /* FRINTA */
8885 case 0x59: /* FRINTX */
Peter Maydell45aecc62014-02-08 14:46:56 +00008886 case 0x79: /* FRINTI */
Peter Maydell45aecc62014-02-08 14:46:56 +00008887 case 0x7c: /* URSQRTE */
8888 case 0x7d: /* FRSQRTE */
Peter Maydell45aecc62014-02-08 14:46:56 +00008889 unsupported_encoding(s, insn);
8890 return;
8891 default:
8892 unallocated_encoding(s);
8893 return;
8894 }
8895 break;
8896 }
8897 default:
8898 unallocated_encoding(s);
8899 return;
8900 }
Peter Maydell94b6c912014-02-03 23:31:51 +00008901
Peter Maydell416ac202014-03-07 16:49:14 +00008902 if (need_fpstatus) {
8903 tcg_fpstatus = get_fpstatus_ptr();
8904 } else {
8905 TCGV_UNUSED_PTR(tcg_fpstatus);
8906 }
8907 if (need_rmode) {
8908 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rmode));
8909 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
8910 } else {
8911 TCGV_UNUSED_I32(tcg_rmode);
8912 }
8913
Peter Maydell94b6c912014-02-03 23:31:51 +00008914 if (size == 3) {
8915 /* All 64-bit element operations can be shared with scalar 2misc */
8916 int pass;
8917
8918 for (pass = 0; pass < (is_q ? 2 : 1); pass++) {
8919 TCGv_i64 tcg_op = tcg_temp_new_i64();
8920 TCGv_i64 tcg_res = tcg_temp_new_i64();
8921
8922 read_vec_element(s, tcg_op, rn, pass, MO_64);
8923
Peter Maydell416ac202014-03-07 16:49:14 +00008924 handle_2misc_64(s, opcode, u, tcg_res, tcg_op,
8925 tcg_rmode, tcg_fpstatus);
Peter Maydell94b6c912014-02-03 23:31:51 +00008926
8927 write_vec_element(s, tcg_res, rd, pass, MO_64);
8928
8929 tcg_temp_free_i64(tcg_res);
8930 tcg_temp_free_i64(tcg_op);
8931 }
8932 } else {
8933 int pass;
8934
8935 for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
8936 TCGv_i32 tcg_op = tcg_temp_new_i32();
8937 TCGv_i32 tcg_res = tcg_temp_new_i32();
8938 TCGCond cond;
8939
8940 read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
8941
8942 if (size == 2) {
8943 /* Special cases for 32 bit elements */
8944 switch (opcode) {
8945 case 0xa: /* CMLT */
8946 /* 32 bit integer comparison against zero, result is
8947 * test ? (2^32 - 1) : 0. We implement via setcond(test)
8948 * and inverting.
8949 */
8950 cond = TCG_COND_LT;
8951 do_cmop:
8952 tcg_gen_setcondi_i32(cond, tcg_res, tcg_op, 0);
8953 tcg_gen_neg_i32(tcg_res, tcg_res);
8954 break;
8955 case 0x8: /* CMGT, CMGE */
8956 cond = u ? TCG_COND_GE : TCG_COND_GT;
8957 goto do_cmop;
8958 case 0x9: /* CMEQ, CMLE */
8959 cond = u ? TCG_COND_LE : TCG_COND_EQ;
8960 goto do_cmop;
Alex Bennée7f8d0b72014-02-25 14:50:33 +00008961 case 0x4: /* CLS */
8962 if (u) {
8963 gen_helper_clz32(tcg_res, tcg_op);
8964 } else {
8965 gen_helper_cls32(tcg_res, tcg_op);
8966 }
8967 break;
Peter Maydell94b6c912014-02-03 23:31:51 +00008968 case 0xb: /* ABS, NEG */
8969 if (u) {
8970 tcg_gen_neg_i32(tcg_res, tcg_op);
8971 } else {
8972 TCGv_i32 tcg_zero = tcg_const_i32(0);
8973 tcg_gen_neg_i32(tcg_res, tcg_op);
8974 tcg_gen_movcond_i32(TCG_COND_GT, tcg_res, tcg_op,
8975 tcg_zero, tcg_op, tcg_res);
8976 tcg_temp_free_i32(tcg_zero);
8977 }
8978 break;
Peter Maydellf93d0132014-02-03 23:31:52 +00008979 case 0x2f: /* FABS */
8980 gen_helper_vfp_abss(tcg_res, tcg_op);
8981 break;
8982 case 0x6f: /* FNEG */
8983 gen_helper_vfp_negs(tcg_res, tcg_op);
8984 break;
Alex Bennéeb399d6d2014-02-25 12:21:05 +00008985 case 0x7f: /* FSQRT */
8986 gen_helper_vfp_sqrts(tcg_res, tcg_op, cpu_env);
8987 break;
Peter Maydell416ac202014-03-07 16:49:14 +00008988 case 0x1a: /* FCVTNS */
8989 case 0x1b: /* FCVTMS */
8990 case 0x1c: /* FCVTAS */
8991 case 0x3a: /* FCVTPS */
8992 case 0x3b: /* FCVTZS */
8993 {
8994 TCGv_i32 tcg_shift = tcg_const_i32(0);
8995 gen_helper_vfp_tosls(tcg_res, tcg_op,
8996 tcg_shift, tcg_fpstatus);
8997 tcg_temp_free_i32(tcg_shift);
8998 break;
8999 }
9000 case 0x5a: /* FCVTNU */
9001 case 0x5b: /* FCVTMU */
9002 case 0x5c: /* FCVTAU */
9003 case 0x7a: /* FCVTPU */
9004 case 0x7b: /* FCVTZU */
9005 {
9006 TCGv_i32 tcg_shift = tcg_const_i32(0);
9007 gen_helper_vfp_touls(tcg_res, tcg_op,
9008 tcg_shift, tcg_fpstatus);
9009 tcg_temp_free_i32(tcg_shift);
9010 break;
9011 }
Peter Maydell94b6c912014-02-03 23:31:51 +00009012 default:
9013 g_assert_not_reached();
9014 }
9015 } else {
9016 /* Use helpers for 8 and 16 bit elements */
9017 switch (opcode) {
Peter Maydell86cbc412014-02-03 23:31:51 +00009018 case 0x5: /* CNT, RBIT */
9019 /* For these two insns size is part of the opcode specifier
9020 * (handled earlier); they always operate on byte elements.
9021 */
9022 if (u) {
9023 gen_helper_neon_rbit_u8(tcg_res, tcg_op);
9024 } else {
9025 gen_helper_neon_cnt_u8(tcg_res, tcg_op);
9026 }
9027 break;
Peter Maydell94b6c912014-02-03 23:31:51 +00009028 case 0x8: /* CMGT, CMGE */
9029 case 0x9: /* CMEQ, CMLE */
9030 case 0xa: /* CMLT */
9031 {
9032 static NeonGenTwoOpFn * const fns[3][2] = {
9033 { gen_helper_neon_cgt_s8, gen_helper_neon_cgt_s16 },
9034 { gen_helper_neon_cge_s8, gen_helper_neon_cge_s16 },
9035 { gen_helper_neon_ceq_u8, gen_helper_neon_ceq_u16 },
9036 };
9037 NeonGenTwoOpFn *genfn;
9038 int comp;
9039 bool reverse;
9040 TCGv_i32 tcg_zero = tcg_const_i32(0);
9041
9042 /* comp = index into [CMGT, CMGE, CMEQ, CMLE, CMLT] */
9043 comp = (opcode - 0x8) * 2 + u;
9044 /* ...but LE, LT are implemented as reverse GE, GT */
9045 reverse = (comp > 2);
9046 if (reverse) {
9047 comp = 4 - comp;
9048 }
9049 genfn = fns[comp][size];
9050 if (reverse) {
9051 genfn(tcg_res, tcg_zero, tcg_op);
9052 } else {
9053 genfn(tcg_res, tcg_op, tcg_zero);
9054 }
9055 tcg_temp_free_i32(tcg_zero);
9056 break;
9057 }
9058 case 0xb: /* ABS, NEG */
9059 if (u) {
9060 TCGv_i32 tcg_zero = tcg_const_i32(0);
9061 if (size) {
9062 gen_helper_neon_sub_u16(tcg_res, tcg_zero, tcg_op);
9063 } else {
9064 gen_helper_neon_sub_u8(tcg_res, tcg_zero, tcg_op);
9065 }
9066 tcg_temp_free_i32(tcg_zero);
9067 } else {
9068 if (size) {
9069 gen_helper_neon_abs_s16(tcg_res, tcg_op);
9070 } else {
9071 gen_helper_neon_abs_s8(tcg_res, tcg_op);
9072 }
9073 }
9074 break;
Alex Bennée7f8d0b72014-02-25 14:50:33 +00009075 case 0x4: /* CLS, CLZ */
9076 if (u) {
9077 if (size == 0) {
9078 gen_helper_neon_clz_u8(tcg_res, tcg_op);
9079 } else {
9080 gen_helper_neon_clz_u16(tcg_res, tcg_op);
9081 }
9082 } else {
9083 if (size == 0) {
9084 gen_helper_neon_cls_s8(tcg_res, tcg_op);
9085 } else {
9086 gen_helper_neon_cls_s16(tcg_res, tcg_op);
9087 }
9088 }
9089 break;
Peter Maydell94b6c912014-02-03 23:31:51 +00009090 default:
9091 g_assert_not_reached();
9092 }
9093 }
9094
9095 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
9096
9097 tcg_temp_free_i32(tcg_res);
9098 tcg_temp_free_i32(tcg_op);
9099 }
9100 }
9101 if (!is_q) {
9102 clear_vec_high(s, rd);
9103 }
Peter Maydell416ac202014-03-07 16:49:14 +00009104
9105 if (need_rmode) {
9106 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
9107 tcg_temp_free_i32(tcg_rmode);
9108 }
9109 if (need_fpstatus) {
9110 tcg_temp_free_ptr(tcg_fpstatus);
9111 }
Alex Bennée384b26f2014-01-31 14:47:30 +00009112}
9113
Peter Maydell9f82e0f2014-02-20 10:35:49 +00009114/* C3.6.13 AdvSIMD scalar x indexed element
9115 * 31 30 29 28 24 23 22 21 20 19 16 15 12 11 10 9 5 4 0
9116 * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
9117 * | 0 1 | U | 1 1 1 1 1 | size | L | M | Rm | opc | H | 0 | Rn | Rd |
9118 * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
9119 * C3.6.18 AdvSIMD vector x indexed element
Alex Bennée384b26f2014-01-31 14:47:30 +00009120 * 31 30 29 28 24 23 22 21 20 19 16 15 12 11 10 9 5 4 0
9121 * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
9122 * | 0 | Q | U | 0 1 1 1 1 | size | L | M | Rm | opc | H | 0 | Rn | Rd |
9123 * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
9124 */
Peter Maydell9f82e0f2014-02-20 10:35:49 +00009125static void disas_simd_indexed(DisasContext *s, uint32_t insn)
Alex Bennée384b26f2014-01-31 14:47:30 +00009126{
Peter Maydellf5e51e72014-02-20 10:35:48 +00009127 /* This encoding has two kinds of instruction:
9128 * normal, where we perform elt x idxelt => elt for each
9129 * element in the vector
9130 * long, where we perform elt x idxelt and generate a result of
9131 * double the width of the input element
9132 * The long ops have a 'part' specifier (ie come in INSN, INSN2 pairs).
9133 */
Peter Maydell9f82e0f2014-02-20 10:35:49 +00009134 bool is_scalar = extract32(insn, 28, 1);
Peter Maydellf5e51e72014-02-20 10:35:48 +00009135 bool is_q = extract32(insn, 30, 1);
9136 bool u = extract32(insn, 29, 1);
9137 int size = extract32(insn, 22, 2);
9138 int l = extract32(insn, 21, 1);
9139 int m = extract32(insn, 20, 1);
9140 /* Note that the Rm field here is only 4 bits, not 5 as it usually is */
9141 int rm = extract32(insn, 16, 4);
9142 int opcode = extract32(insn, 12, 4);
9143 int h = extract32(insn, 11, 1);
9144 int rn = extract32(insn, 5, 5);
9145 int rd = extract32(insn, 0, 5);
9146 bool is_long = false;
9147 bool is_fp = false;
9148 int index;
9149 TCGv_ptr fpst;
9150
9151 switch (opcode) {
9152 case 0x0: /* MLA */
9153 case 0x4: /* MLS */
Peter Maydell9f82e0f2014-02-20 10:35:49 +00009154 if (!u || is_scalar) {
Peter Maydellf5e51e72014-02-20 10:35:48 +00009155 unallocated_encoding(s);
9156 return;
9157 }
9158 break;
9159 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
9160 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
9161 case 0xa: /* SMULL, SMULL2, UMULL, UMULL2 */
Peter Maydell9f82e0f2014-02-20 10:35:49 +00009162 if (is_scalar) {
9163 unallocated_encoding(s);
9164 return;
9165 }
Peter Maydellf5e51e72014-02-20 10:35:48 +00009166 is_long = true;
9167 break;
9168 case 0x3: /* SQDMLAL, SQDMLAL2 */
9169 case 0x7: /* SQDMLSL, SQDMLSL2 */
9170 case 0xb: /* SQDMULL, SQDMULL2 */
9171 is_long = true;
9172 /* fall through */
9173 case 0xc: /* SQDMULH */
9174 case 0xd: /* SQRDMULH */
Peter Maydellf5e51e72014-02-20 10:35:48 +00009175 if (u) {
9176 unallocated_encoding(s);
9177 return;
9178 }
9179 break;
Peter Maydell9f82e0f2014-02-20 10:35:49 +00009180 case 0x8: /* MUL */
9181 if (u || is_scalar) {
9182 unallocated_encoding(s);
9183 return;
9184 }
9185 break;
Peter Maydellf5e51e72014-02-20 10:35:48 +00009186 case 0x1: /* FMLA */
9187 case 0x5: /* FMLS */
9188 if (u) {
9189 unallocated_encoding(s);
9190 return;
9191 }
9192 /* fall through */
9193 case 0x9: /* FMUL, FMULX */
9194 if (!extract32(size, 1, 1)) {
9195 unallocated_encoding(s);
9196 return;
9197 }
9198 is_fp = true;
9199 break;
9200 default:
9201 unallocated_encoding(s);
9202 return;
9203 }
9204
9205 if (is_fp) {
9206 /* low bit of size indicates single/double */
9207 size = extract32(size, 0, 1) ? 3 : 2;
9208 if (size == 2) {
9209 index = h << 1 | l;
9210 } else {
9211 if (l || !is_q) {
9212 unallocated_encoding(s);
9213 return;
9214 }
9215 index = h;
9216 }
9217 rm |= (m << 4);
9218 } else {
9219 switch (size) {
9220 case 1:
9221 index = h << 2 | l << 1 | m;
9222 break;
9223 case 2:
9224 index = h << 1 | l;
9225 rm |= (m << 4);
9226 break;
9227 default:
9228 unallocated_encoding(s);
9229 return;
9230 }
9231 }
9232
Peter Maydellf5e51e72014-02-20 10:35:48 +00009233 if (is_fp) {
9234 fpst = get_fpstatus_ptr();
9235 } else {
9236 TCGV_UNUSED_PTR(fpst);
9237 }
9238
9239 if (size == 3) {
9240 TCGv_i64 tcg_idx = tcg_temp_new_i64();
9241 int pass;
9242
9243 assert(is_fp && is_q && !is_long);
9244
9245 read_vec_element(s, tcg_idx, rm, index, MO_64);
9246
Peter Maydell9f82e0f2014-02-20 10:35:49 +00009247 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
Peter Maydellf5e51e72014-02-20 10:35:48 +00009248 TCGv_i64 tcg_op = tcg_temp_new_i64();
9249 TCGv_i64 tcg_res = tcg_temp_new_i64();
9250
9251 read_vec_element(s, tcg_op, rn, pass, MO_64);
9252
9253 switch (opcode) {
9254 case 0x5: /* FMLS */
9255 /* As usual for ARM, separate negation for fused multiply-add */
9256 gen_helper_vfp_negd(tcg_op, tcg_op);
9257 /* fall through */
9258 case 0x1: /* FMLA */
9259 read_vec_element(s, tcg_res, rd, pass, MO_64);
9260 gen_helper_vfp_muladdd(tcg_res, tcg_op, tcg_idx, tcg_res, fpst);
9261 break;
9262 case 0x9: /* FMUL, FMULX */
9263 if (u) {
9264 gen_helper_vfp_mulxd(tcg_res, tcg_op, tcg_idx, fpst);
9265 } else {
9266 gen_helper_vfp_muld(tcg_res, tcg_op, tcg_idx, fpst);
9267 }
9268 break;
9269 default:
9270 g_assert_not_reached();
9271 }
9272
9273 write_vec_element(s, tcg_res, rd, pass, MO_64);
9274 tcg_temp_free_i64(tcg_op);
9275 tcg_temp_free_i64(tcg_res);
9276 }
9277
Peter Maydell9f82e0f2014-02-20 10:35:49 +00009278 if (is_scalar) {
9279 clear_vec_high(s, rd);
9280 }
9281
Peter Maydellf5e51e72014-02-20 10:35:48 +00009282 tcg_temp_free_i64(tcg_idx);
9283 } else if (!is_long) {
Peter Maydell9f82e0f2014-02-20 10:35:49 +00009284 /* 32 bit floating point, or 16 or 32 bit integer.
9285 * For the 16 bit scalar case we use the usual Neon helpers and
9286 * rely on the fact that 0 op 0 == 0 with no side effects.
9287 */
Peter Maydellf5e51e72014-02-20 10:35:48 +00009288 TCGv_i32 tcg_idx = tcg_temp_new_i32();
Peter Maydell9f82e0f2014-02-20 10:35:49 +00009289 int pass, maxpasses;
9290
9291 if (is_scalar) {
9292 maxpasses = 1;
9293 } else {
9294 maxpasses = is_q ? 4 : 2;
9295 }
Peter Maydellf5e51e72014-02-20 10:35:48 +00009296
9297 read_vec_element_i32(s, tcg_idx, rm, index, size);
9298
Peter Maydell9f82e0f2014-02-20 10:35:49 +00009299 if (size == 1 && !is_scalar) {
Peter Maydellf5e51e72014-02-20 10:35:48 +00009300 /* The simplest way to handle the 16x16 indexed ops is to duplicate
9301 * the index into both halves of the 32 bit tcg_idx and then use
9302 * the usual Neon helpers.
9303 */
9304 tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
9305 }
9306
Peter Maydell9f82e0f2014-02-20 10:35:49 +00009307 for (pass = 0; pass < maxpasses; pass++) {
Peter Maydellf5e51e72014-02-20 10:35:48 +00009308 TCGv_i32 tcg_op = tcg_temp_new_i32();
9309 TCGv_i32 tcg_res = tcg_temp_new_i32();
9310
Peter Maydell9f82e0f2014-02-20 10:35:49 +00009311 read_vec_element_i32(s, tcg_op, rn, pass, is_scalar ? size : MO_32);
Peter Maydellf5e51e72014-02-20 10:35:48 +00009312
9313 switch (opcode) {
9314 case 0x0: /* MLA */
9315 case 0x4: /* MLS */
9316 case 0x8: /* MUL */
9317 {
9318 static NeonGenTwoOpFn * const fns[2][2] = {
9319 { gen_helper_neon_add_u16, gen_helper_neon_sub_u16 },
9320 { tcg_gen_add_i32, tcg_gen_sub_i32 },
9321 };
9322 NeonGenTwoOpFn *genfn;
9323 bool is_sub = opcode == 0x4;
9324
9325 if (size == 1) {
9326 gen_helper_neon_mul_u16(tcg_res, tcg_op, tcg_idx);
9327 } else {
9328 tcg_gen_mul_i32(tcg_res, tcg_op, tcg_idx);
9329 }
9330 if (opcode == 0x8) {
9331 break;
9332 }
9333 read_vec_element_i32(s, tcg_op, rd, pass, MO_32);
9334 genfn = fns[size - 1][is_sub];
9335 genfn(tcg_res, tcg_op, tcg_res);
9336 break;
9337 }
9338 case 0x5: /* FMLS */
9339 /* As usual for ARM, separate negation for fused multiply-add */
9340 gen_helper_vfp_negs(tcg_op, tcg_op);
9341 /* fall through */
9342 case 0x1: /* FMLA */
9343 read_vec_element_i32(s, tcg_res, rd, pass, MO_32);
9344 gen_helper_vfp_muladds(tcg_res, tcg_op, tcg_idx, tcg_res, fpst);
9345 break;
9346 case 0x9: /* FMUL, FMULX */
9347 if (u) {
9348 gen_helper_vfp_mulxs(tcg_res, tcg_op, tcg_idx, fpst);
9349 } else {
9350 gen_helper_vfp_muls(tcg_res, tcg_op, tcg_idx, fpst);
9351 }
9352 break;
9353 case 0xc: /* SQDMULH */
9354 if (size == 1) {
9355 gen_helper_neon_qdmulh_s16(tcg_res, cpu_env,
9356 tcg_op, tcg_idx);
9357 } else {
9358 gen_helper_neon_qdmulh_s32(tcg_res, cpu_env,
9359 tcg_op, tcg_idx);
9360 }
9361 break;
9362 case 0xd: /* SQRDMULH */
9363 if (size == 1) {
9364 gen_helper_neon_qrdmulh_s16(tcg_res, cpu_env,
9365 tcg_op, tcg_idx);
9366 } else {
9367 gen_helper_neon_qrdmulh_s32(tcg_res, cpu_env,
9368 tcg_op, tcg_idx);
9369 }
9370 break;
9371 default:
9372 g_assert_not_reached();
9373 }
9374
Peter Maydell9f82e0f2014-02-20 10:35:49 +00009375 if (is_scalar) {
9376 write_fp_sreg(s, rd, tcg_res);
9377 } else {
9378 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
9379 }
9380
Peter Maydellf5e51e72014-02-20 10:35:48 +00009381 tcg_temp_free_i32(tcg_op);
9382 tcg_temp_free_i32(tcg_res);
9383 }
9384
9385 tcg_temp_free_i32(tcg_idx);
9386
9387 if (!is_q) {
9388 clear_vec_high(s, rd);
9389 }
9390 } else {
9391 /* long ops: 16x16->32 or 32x32->64 */
Peter Maydellc44ad1f2014-02-20 10:35:49 +00009392 TCGv_i64 tcg_res[2];
9393 int pass;
9394 bool satop = extract32(opcode, 0, 1);
9395 TCGMemOp memop = MO_32;
9396
9397 if (satop || !u) {
9398 memop |= MO_SIGN;
9399 }
9400
9401 if (size == 2) {
9402 TCGv_i64 tcg_idx = tcg_temp_new_i64();
9403
9404 read_vec_element(s, tcg_idx, rm, index, memop);
9405
Peter Maydell9f82e0f2014-02-20 10:35:49 +00009406 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
Peter Maydellc44ad1f2014-02-20 10:35:49 +00009407 TCGv_i64 tcg_op = tcg_temp_new_i64();
9408 TCGv_i64 tcg_passres;
Peter Maydell9f82e0f2014-02-20 10:35:49 +00009409 int passelt;
Peter Maydellc44ad1f2014-02-20 10:35:49 +00009410
Peter Maydell9f82e0f2014-02-20 10:35:49 +00009411 if (is_scalar) {
9412 passelt = 0;
9413 } else {
9414 passelt = pass + (is_q * 2);
9415 }
9416
9417 read_vec_element(s, tcg_op, rn, passelt, memop);
Peter Maydellc44ad1f2014-02-20 10:35:49 +00009418
9419 tcg_res[pass] = tcg_temp_new_i64();
9420
9421 if (opcode == 0xa || opcode == 0xb) {
9422 /* Non-accumulating ops */
9423 tcg_passres = tcg_res[pass];
9424 } else {
9425 tcg_passres = tcg_temp_new_i64();
9426 }
9427
9428 tcg_gen_mul_i64(tcg_passres, tcg_op, tcg_idx);
9429 tcg_temp_free_i64(tcg_op);
9430
9431 if (satop) {
9432 /* saturating, doubling */
9433 gen_helper_neon_addl_saturate_s64(tcg_passres, cpu_env,
9434 tcg_passres, tcg_passres);
9435 }
9436
9437 if (opcode == 0xa || opcode == 0xb) {
9438 continue;
9439 }
9440
9441 /* Accumulating op: handle accumulate step */
9442 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
9443
9444 switch (opcode) {
9445 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
9446 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
9447 break;
9448 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
9449 tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
9450 break;
9451 case 0x7: /* SQDMLSL, SQDMLSL2 */
9452 tcg_gen_neg_i64(tcg_passres, tcg_passres);
9453 /* fall through */
9454 case 0x3: /* SQDMLAL, SQDMLAL2 */
9455 gen_helper_neon_addl_saturate_s64(tcg_res[pass], cpu_env,
9456 tcg_res[pass],
9457 tcg_passres);
9458 break;
9459 default:
9460 g_assert_not_reached();
9461 }
9462 tcg_temp_free_i64(tcg_passres);
9463 }
9464 tcg_temp_free_i64(tcg_idx);
Peter Maydell9f82e0f2014-02-20 10:35:49 +00009465
9466 if (is_scalar) {
9467 clear_vec_high(s, rd);
9468 }
Peter Maydellc44ad1f2014-02-20 10:35:49 +00009469 } else {
9470 TCGv_i32 tcg_idx = tcg_temp_new_i32();
9471
9472 assert(size == 1);
9473 read_vec_element_i32(s, tcg_idx, rm, index, size);
9474
Peter Maydell9f82e0f2014-02-20 10:35:49 +00009475 if (!is_scalar) {
9476 /* The simplest way to handle the 16x16 indexed ops is to
9477 * duplicate the index into both halves of the 32 bit tcg_idx
9478 * and then use the usual Neon helpers.
9479 */
9480 tcg_gen_deposit_i32(tcg_idx, tcg_idx, tcg_idx, 16, 16);
9481 }
Peter Maydellc44ad1f2014-02-20 10:35:49 +00009482
Peter Maydell9f82e0f2014-02-20 10:35:49 +00009483 for (pass = 0; pass < (is_scalar ? 1 : 2); pass++) {
Peter Maydellc44ad1f2014-02-20 10:35:49 +00009484 TCGv_i32 tcg_op = tcg_temp_new_i32();
9485 TCGv_i64 tcg_passres;
9486
Peter Maydell9f82e0f2014-02-20 10:35:49 +00009487 if (is_scalar) {
9488 read_vec_element_i32(s, tcg_op, rn, pass, size);
9489 } else {
9490 read_vec_element_i32(s, tcg_op, rn,
9491 pass + (is_q * 2), MO_32);
9492 }
9493
Peter Maydellc44ad1f2014-02-20 10:35:49 +00009494 tcg_res[pass] = tcg_temp_new_i64();
9495
9496 if (opcode == 0xa || opcode == 0xb) {
9497 /* Non-accumulating ops */
9498 tcg_passres = tcg_res[pass];
9499 } else {
9500 tcg_passres = tcg_temp_new_i64();
9501 }
9502
9503 if (memop & MO_SIGN) {
9504 gen_helper_neon_mull_s16(tcg_passres, tcg_op, tcg_idx);
9505 } else {
9506 gen_helper_neon_mull_u16(tcg_passres, tcg_op, tcg_idx);
9507 }
9508 if (satop) {
9509 gen_helper_neon_addl_saturate_s32(tcg_passres, cpu_env,
9510 tcg_passres, tcg_passres);
9511 }
9512 tcg_temp_free_i32(tcg_op);
9513
9514 if (opcode == 0xa || opcode == 0xb) {
9515 continue;
9516 }
9517
9518 /* Accumulating op: handle accumulate step */
9519 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
9520
9521 switch (opcode) {
9522 case 0x2: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
9523 gen_helper_neon_addl_u32(tcg_res[pass], tcg_res[pass],
9524 tcg_passres);
9525 break;
9526 case 0x6: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
9527 gen_helper_neon_subl_u32(tcg_res[pass], tcg_res[pass],
9528 tcg_passres);
9529 break;
9530 case 0x7: /* SQDMLSL, SQDMLSL2 */
9531 gen_helper_neon_negl_u32(tcg_passres, tcg_passres);
9532 /* fall through */
9533 case 0x3: /* SQDMLAL, SQDMLAL2 */
9534 gen_helper_neon_addl_saturate_s32(tcg_res[pass], cpu_env,
9535 tcg_res[pass],
9536 tcg_passres);
9537 break;
9538 default:
9539 g_assert_not_reached();
9540 }
9541 tcg_temp_free_i64(tcg_passres);
9542 }
9543 tcg_temp_free_i32(tcg_idx);
Peter Maydell9f82e0f2014-02-20 10:35:49 +00009544
9545 if (is_scalar) {
9546 tcg_gen_ext32u_i64(tcg_res[0], tcg_res[0]);
9547 }
9548 }
9549
9550 if (is_scalar) {
9551 tcg_res[1] = tcg_const_i64(0);
Peter Maydellc44ad1f2014-02-20 10:35:49 +00009552 }
9553
9554 for (pass = 0; pass < 2; pass++) {
9555 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
9556 tcg_temp_free_i64(tcg_res[pass]);
9557 }
Peter Maydellf5e51e72014-02-20 10:35:48 +00009558 }
9559
9560 if (!TCGV_IS_UNUSED_PTR(fpst)) {
9561 tcg_temp_free_ptr(fpst);
9562 }
Alex Bennée384b26f2014-01-31 14:47:30 +00009563}
9564
9565/* C3.6.19 Crypto AES
9566 * 31 24 23 22 21 17 16 12 11 10 9 5 4 0
9567 * +-----------------+------+-----------+--------+-----+------+------+
9568 * | 0 1 0 0 1 1 1 0 | size | 1 0 1 0 0 | opcode | 1 0 | Rn | Rd |
9569 * +-----------------+------+-----------+--------+-----+------+------+
9570 */
9571static void disas_crypto_aes(DisasContext *s, uint32_t insn)
9572{
9573 unsupported_encoding(s, insn);
9574}
9575
9576/* C3.6.20 Crypto three-reg SHA
9577 * 31 24 23 22 21 20 16 15 14 12 11 10 9 5 4 0
9578 * +-----------------+------+---+------+---+--------+-----+------+------+
9579 * | 0 1 0 1 1 1 1 0 | size | 0 | Rm | 0 | opcode | 0 0 | Rn | Rd |
9580 * +-----------------+------+---+------+---+--------+-----+------+------+
9581 */
9582static void disas_crypto_three_reg_sha(DisasContext *s, uint32_t insn)
9583{
9584 unsupported_encoding(s, insn);
9585}
9586
9587/* C3.6.21 Crypto two-reg SHA
9588 * 31 24 23 22 21 17 16 12 11 10 9 5 4 0
9589 * +-----------------+------+-----------+--------+-----+------+------+
9590 * | 0 1 0 1 1 1 1 0 | size | 1 0 1 0 0 | opcode | 1 0 | Rn | Rd |
9591 * +-----------------+------+-----------+--------+-----+------+------+
9592 */
9593static void disas_crypto_two_reg_sha(DisasContext *s, uint32_t insn)
9594{
9595 unsupported_encoding(s, insn);
9596}
9597
9598/* C3.6 Data processing - SIMD, inc Crypto
9599 *
9600 * As the decode gets a little complex we are using a table based
9601 * approach for this part of the decode.
9602 */
9603static const AArch64DecodeTable data_proc_simd[] = {
9604 /* pattern , mask , fn */
9605 { 0x0e200400, 0x9f200400, disas_simd_three_reg_same },
9606 { 0x0e200000, 0x9f200c00, disas_simd_three_reg_diff },
9607 { 0x0e200800, 0x9f3e0c00, disas_simd_two_reg_misc },
9608 { 0x0e300800, 0x9f3e0c00, disas_simd_across_lanes },
9609 { 0x0e000400, 0x9fe08400, disas_simd_copy },
Peter Maydell9f82e0f2014-02-20 10:35:49 +00009610 { 0x0f000000, 0x9f000400, disas_simd_indexed }, /* vector indexed */
Alex Bennée384b26f2014-01-31 14:47:30 +00009611 /* simd_mod_imm decode is a subset of simd_shift_imm, so must precede it */
9612 { 0x0f000400, 0x9ff80400, disas_simd_mod_imm },
9613 { 0x0f000400, 0x9f800400, disas_simd_shift_imm },
9614 { 0x0e000000, 0xbf208c00, disas_simd_tb },
9615 { 0x0e000800, 0xbf208c00, disas_simd_zip_trn },
9616 { 0x2e000000, 0xbf208400, disas_simd_ext },
9617 { 0x5e200400, 0xdf200400, disas_simd_scalar_three_reg_same },
9618 { 0x5e200000, 0xdf200c00, disas_simd_scalar_three_reg_diff },
9619 { 0x5e200800, 0xdf3e0c00, disas_simd_scalar_two_reg_misc },
9620 { 0x5e300800, 0xdf3e0c00, disas_simd_scalar_pairwise },
9621 { 0x5e000400, 0xdfe08400, disas_simd_scalar_copy },
Peter Maydell9f82e0f2014-02-20 10:35:49 +00009622 { 0x5f000000, 0xdf000400, disas_simd_indexed }, /* scalar indexed */
Alex Bennée384b26f2014-01-31 14:47:30 +00009623 { 0x5f000400, 0xdf800400, disas_simd_scalar_shift_imm },
9624 { 0x4e280800, 0xff3e0c00, disas_crypto_aes },
9625 { 0x5e000000, 0xff208c00, disas_crypto_three_reg_sha },
9626 { 0x5e280800, 0xff3e0c00, disas_crypto_two_reg_sha },
9627 { 0x00000000, 0x00000000, NULL }
9628};
9629
Peter Maydellfaa0ba42013-12-23 23:27:30 +00009630static void disas_data_proc_simd(DisasContext *s, uint32_t insn)
9631{
9632 /* Note that this is called with all non-FP cases from
9633 * table C3-6 so it must UNDEF for entries not specifically
9634 * allocated to instructions in that table.
9635 */
Alex Bennée384b26f2014-01-31 14:47:30 +00009636 AArch64DecodeFn *fn = lookup_disas_fn(&data_proc_simd[0], insn);
9637 if (fn) {
9638 fn(s, insn);
9639 } else {
9640 unallocated_encoding(s);
9641 }
Peter Maydellfaa0ba42013-12-23 23:27:30 +00009642}
9643
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00009644/* C3.6 Data processing - SIMD and floating point */
9645static void disas_data_proc_simd_fp(DisasContext *s, uint32_t insn)
9646{
Peter Maydellfaa0ba42013-12-23 23:27:30 +00009647 if (extract32(insn, 28, 1) == 1 && extract32(insn, 30, 1) == 0) {
9648 disas_data_proc_fp(s, insn);
9649 } else {
9650 /* SIMD, including crypto */
9651 disas_data_proc_simd(s, insn);
9652 }
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00009653}
9654
9655/* C3.1 A64 instruction index by encoding */
Peter Maydell40f860c2013-12-17 19:42:31 +00009656static void disas_a64_insn(CPUARMState *env, DisasContext *s)
Alexander Graf14ade102013-09-03 20:12:10 +01009657{
9658 uint32_t insn;
9659
9660 insn = arm_ldl_code(env, s->pc, s->bswap_code);
9661 s->insn = insn;
9662 s->pc += 4;
9663
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00009664 switch (extract32(insn, 25, 4)) {
9665 case 0x0: case 0x1: case 0x2: case 0x3: /* UNALLOCATED */
Alexander Graf14ade102013-09-03 20:12:10 +01009666 unallocated_encoding(s);
9667 break;
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00009668 case 0x8: case 0x9: /* Data processing - immediate */
9669 disas_data_proc_imm(s, insn);
9670 break;
9671 case 0xa: case 0xb: /* Branch, exception generation and system insns */
9672 disas_b_exc_sys(s, insn);
9673 break;
9674 case 0x4:
9675 case 0x6:
9676 case 0xc:
9677 case 0xe: /* Loads and stores */
9678 disas_ldst(s, insn);
9679 break;
9680 case 0x5:
9681 case 0xd: /* Data processing - register */
9682 disas_data_proc_reg(s, insn);
9683 break;
9684 case 0x7:
9685 case 0xf: /* Data processing - SIMD and floating point */
9686 disas_data_proc_simd_fp(s, insn);
9687 break;
9688 default:
9689 assert(FALSE); /* all 15 cases should be handled above */
9690 break;
Alexander Graf14ade102013-09-03 20:12:10 +01009691 }
Alexander Graf11e169d2013-12-17 19:42:32 +00009692
9693 /* if we allocated any temporaries, free them here */
9694 free_tmp_a64(s);
Peter Maydell40f860c2013-12-17 19:42:31 +00009695}
Alexander Graf14ade102013-09-03 20:12:10 +01009696
Peter Maydell40f860c2013-12-17 19:42:31 +00009697void gen_intermediate_code_internal_a64(ARMCPU *cpu,
9698 TranslationBlock *tb,
9699 bool search_pc)
9700{
9701 CPUState *cs = CPU(cpu);
9702 CPUARMState *env = &cpu->env;
9703 DisasContext dc1, *dc = &dc1;
9704 CPUBreakpoint *bp;
9705 uint16_t *gen_opc_end;
9706 int j, lj;
9707 target_ulong pc_start;
9708 target_ulong next_page_start;
9709 int num_insns;
9710 int max_insns;
9711
9712 pc_start = tb->pc;
9713
9714 dc->tb = tb;
9715
9716 gen_opc_end = tcg_ctx.gen_opc_buf + OPC_MAX_SIZE;
9717
9718 dc->is_jmp = DISAS_NEXT;
9719 dc->pc = pc_start;
9720 dc->singlestep_enabled = cs->singlestep_enabled;
9721 dc->condjmp = 0;
9722
9723 dc->aarch64 = 1;
9724 dc->thumb = 0;
9725 dc->bswap_code = 0;
9726 dc->condexec_mask = 0;
9727 dc->condexec_cond = 0;
9728#if !defined(CONFIG_USER_ONLY)
Peter Maydelld9ea7d22014-02-26 17:20:05 +00009729 dc->user = (ARM_TBFLAG_AA64_EL(tb->flags) == 0);
Peter Maydell40f860c2013-12-17 19:42:31 +00009730#endif
9731 dc->vfp_enabled = 0;
9732 dc->vec_len = 0;
9733 dc->vec_stride = 0;
Peter Maydell60322b32014-01-04 22:15:44 +00009734 dc->cp_regs = cpu->cp_regs;
9735 dc->current_pl = arm_current_pl(env);
Peter Maydell6c13ec22014-02-27 12:03:17 +00009736 dc->features = env->features;
Peter Maydell40f860c2013-12-17 19:42:31 +00009737
Alexander Graf11e169d2013-12-17 19:42:32 +00009738 init_tmp_a64_array(dc);
9739
Peter Maydell40f860c2013-12-17 19:42:31 +00009740 next_page_start = (pc_start & TARGET_PAGE_MASK) + TARGET_PAGE_SIZE;
9741 lj = -1;
9742 num_insns = 0;
9743 max_insns = tb->cflags & CF_COUNT_MASK;
9744 if (max_insns == 0) {
9745 max_insns = CF_COUNT_MASK;
9746 }
9747
9748 gen_tb_start();
9749
9750 tcg_clear_temp_count();
9751
9752 do {
9753 if (unlikely(!QTAILQ_EMPTY(&env->breakpoints))) {
9754 QTAILQ_FOREACH(bp, &env->breakpoints, entry) {
9755 if (bp->pc == dc->pc) {
9756 gen_exception_insn(dc, 0, EXCP_DEBUG);
9757 /* Advance PC so that clearing the breakpoint will
9758 invalidate this TB. */
9759 dc->pc += 2;
9760 goto done_generating;
9761 }
9762 }
9763 }
9764
9765 if (search_pc) {
9766 j = tcg_ctx.gen_opc_ptr - tcg_ctx.gen_opc_buf;
9767 if (lj < j) {
9768 lj++;
9769 while (lj < j) {
9770 tcg_ctx.gen_opc_instr_start[lj++] = 0;
9771 }
9772 }
9773 tcg_ctx.gen_opc_pc[lj] = dc->pc;
9774 tcg_ctx.gen_opc_instr_start[lj] = 1;
9775 tcg_ctx.gen_opc_icount[lj] = num_insns;
9776 }
9777
9778 if (num_insns + 1 == max_insns && (tb->cflags & CF_LAST_IO)) {
9779 gen_io_start();
9780 }
9781
9782 if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP | CPU_LOG_TB_OP_OPT))) {
9783 tcg_gen_debug_insn_start(dc->pc);
9784 }
9785
9786 disas_a64_insn(env, dc);
9787
9788 if (tcg_check_temp_count()) {
9789 fprintf(stderr, "TCG temporary leak before "TARGET_FMT_lx"\n",
9790 dc->pc);
9791 }
9792
9793 /* Translation stops when a conditional branch is encountered.
9794 * Otherwise the subsequent code could get translated several times.
9795 * Also stop translation when a page boundary is reached. This
9796 * ensures prefetch aborts occur at the right place.
9797 */
9798 num_insns++;
9799 } while (!dc->is_jmp && tcg_ctx.gen_opc_ptr < gen_opc_end &&
9800 !cs->singlestep_enabled &&
9801 !singlestep &&
9802 dc->pc < next_page_start &&
9803 num_insns < max_insns);
9804
9805 if (tb->cflags & CF_LAST_IO) {
9806 gen_io_end();
9807 }
9808
9809 if (unlikely(cs->singlestep_enabled) && dc->is_jmp != DISAS_EXC) {
9810 /* Note that this means single stepping WFI doesn't halt the CPU.
9811 * For conditional branch insns this is harmless unreachable code as
9812 * gen_goto_tb() has already handled emitting the debug exception
9813 * (and thus a tb-jump is not possible when singlestepping).
9814 */
9815 assert(dc->is_jmp != DISAS_TB_JUMP);
9816 if (dc->is_jmp != DISAS_JUMP) {
9817 gen_a64_set_pc_im(dc->pc);
9818 }
9819 gen_exception(EXCP_DEBUG);
9820 } else {
9821 switch (dc->is_jmp) {
9822 case DISAS_NEXT:
9823 gen_goto_tb(dc, 1, dc->pc);
9824 break;
9825 default:
Peter Maydell40f860c2013-12-17 19:42:31 +00009826 case DISAS_UPDATE:
Peter Maydellfea50522014-01-04 22:15:45 +00009827 gen_a64_set_pc_im(dc->pc);
9828 /* fall through */
9829 case DISAS_JUMP:
Peter Maydell40f860c2013-12-17 19:42:31 +00009830 /* indicate that the hash table must be used to find the next TB */
9831 tcg_gen_exit_tb(0);
9832 break;
9833 case DISAS_TB_JUMP:
9834 case DISAS_EXC:
9835 case DISAS_SWI:
9836 break;
9837 case DISAS_WFI:
9838 /* This is a special case because we don't want to just halt the CPU
9839 * if trying to debug across a WFI.
9840 */
Peter Maydell1ed69e82014-02-26 17:20:06 +00009841 gen_a64_set_pc_im(dc->pc);
Peter Maydell40f860c2013-12-17 19:42:31 +00009842 gen_helper_wfi(cpu_env);
9843 break;
9844 }
9845 }
9846
9847done_generating:
9848 gen_tb_end(tb, num_insns);
9849 *tcg_ctx.gen_opc_ptr = INDEX_op_end;
9850
9851#ifdef DEBUG_DISAS
9852 if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)) {
9853 qemu_log("----------------\n");
9854 qemu_log("IN: %s\n", lookup_symbol(pc_start));
9855 log_target_disas(env, pc_start, dc->pc - pc_start,
Claudio Fontana999b53e2014-02-05 17:27:28 +00009856 4 | (dc->bswap_code << 1));
Peter Maydell40f860c2013-12-17 19:42:31 +00009857 qemu_log("\n");
9858 }
9859#endif
9860 if (search_pc) {
9861 j = tcg_ctx.gen_opc_ptr - tcg_ctx.gen_opc_buf;
9862 lj++;
9863 while (lj <= j) {
9864 tcg_ctx.gen_opc_instr_start[lj++] = 0;
9865 }
9866 } else {
9867 tb->size = dc->pc - pc_start;
9868 tb->icount = num_insns;
Alexander Graf14ade102013-09-03 20:12:10 +01009869 }
9870}