blob: 797e36463e4c2c5216b6e43469fcad1925717380 [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ée94ad8702014-01-23 14:37:07 +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 Maydell0d19b492014-01-23 13:13:41 +000075/* Function prototype for gen_ functions for calling Neon helpers */
76typedef void NeonGenTwoOpFn(TCGv_i32, TCGv_i32, TCGv_i32);
Peter Maydell299088d2014-01-25 14:37:12 +000077typedef void NeonGenTwoOpEnvFn(TCGv_i32, TCGv_ptr, TCGv_i32, TCGv_i32);
Peter Maydellf10708d2014-01-26 14:06:50 +000078typedef void NeonGenNarrowFn(TCGv_i32, TCGv_i64);
79typedef void NeonGenNarrowEnvFn(TCGv_i32, TCGv_ptr, TCGv_i64);
Peter Maydell0d19b492014-01-23 13:13:41 +000080
Alexander Graf14ade102013-09-03 20:12:10 +010081/* initialize TCG globals. */
82void a64_translate_init(void)
83{
84 int i;
85
86 cpu_pc = tcg_global_mem_new_i64(TCG_AREG0,
87 offsetof(CPUARMState, pc),
88 "pc");
89 for (i = 0; i < 32; i++) {
90 cpu_X[i] = tcg_global_mem_new_i64(TCG_AREG0,
91 offsetof(CPUARMState, xregs[i]),
92 regnames[i]);
93 }
94
Alexander Graf832ffa12013-12-17 19:42:34 +000095 cpu_NF = tcg_global_mem_new_i32(TCG_AREG0, offsetof(CPUARMState, NF), "NF");
96 cpu_ZF = tcg_global_mem_new_i32(TCG_AREG0, offsetof(CPUARMState, ZF), "ZF");
97 cpu_CF = tcg_global_mem_new_i32(TCG_AREG0, offsetof(CPUARMState, CF), "CF");
98 cpu_VF = tcg_global_mem_new_i32(TCG_AREG0, offsetof(CPUARMState, VF), "VF");
Michael Matzfa2ef212014-01-04 22:15:47 +000099
100 cpu_exclusive_addr = tcg_global_mem_new_i64(TCG_AREG0,
101 offsetof(CPUARMState, exclusive_addr), "exclusive_addr");
102 cpu_exclusive_val = tcg_global_mem_new_i64(TCG_AREG0,
103 offsetof(CPUARMState, exclusive_val), "exclusive_val");
104 cpu_exclusive_high = tcg_global_mem_new_i64(TCG_AREG0,
105 offsetof(CPUARMState, exclusive_high), "exclusive_high");
106#ifdef CONFIG_USER_ONLY
107 cpu_exclusive_test = tcg_global_mem_new_i64(TCG_AREG0,
108 offsetof(CPUARMState, exclusive_test), "exclusive_test");
109 cpu_exclusive_info = tcg_global_mem_new_i32(TCG_AREG0,
110 offsetof(CPUARMState, exclusive_info), "exclusive_info");
111#endif
Alexander Graf14ade102013-09-03 20:12:10 +0100112}
113
114void aarch64_cpu_dump_state(CPUState *cs, FILE *f,
115 fprintf_function cpu_fprintf, int flags)
116{
117 ARMCPU *cpu = ARM_CPU(cs);
118 CPUARMState *env = &cpu->env;
Peter Maydelld3563122013-12-17 19:42:30 +0000119 uint32_t psr = pstate_read(env);
Alexander Graf14ade102013-09-03 20:12:10 +0100120 int i;
121
122 cpu_fprintf(f, "PC=%016"PRIx64" SP=%016"PRIx64"\n",
123 env->pc, env->xregs[31]);
124 for (i = 0; i < 31; i++) {
125 cpu_fprintf(f, "X%02d=%016"PRIx64, i, env->xregs[i]);
126 if ((i % 4) == 3) {
127 cpu_fprintf(f, "\n");
128 } else {
129 cpu_fprintf(f, " ");
130 }
131 }
Peter Maydelld3563122013-12-17 19:42:30 +0000132 cpu_fprintf(f, "PSTATE=%08x (flags %c%c%c%c)\n",
133 psr,
134 psr & PSTATE_N ? 'N' : '-',
135 psr & PSTATE_Z ? 'Z' : '-',
136 psr & PSTATE_C ? 'C' : '-',
137 psr & PSTATE_V ? 'V' : '-');
Alexander Graf14ade102013-09-03 20:12:10 +0100138 cpu_fprintf(f, "\n");
Alexander Graff6d8a312014-01-04 22:15:49 +0000139
140 if (flags & CPU_DUMP_FPU) {
141 int numvfpregs = 32;
142 for (i = 0; i < numvfpregs; i += 2) {
143 uint64_t vlo = float64_val(env->vfp.regs[i * 2]);
144 uint64_t vhi = float64_val(env->vfp.regs[(i * 2) + 1]);
145 cpu_fprintf(f, "q%02d=%016" PRIx64 ":%016" PRIx64 " ",
146 i, vhi, vlo);
147 vlo = float64_val(env->vfp.regs[(i + 1) * 2]);
148 vhi = float64_val(env->vfp.regs[((i + 1) * 2) + 1]);
149 cpu_fprintf(f, "q%02d=%016" PRIx64 ":%016" PRIx64 "\n",
150 i + 1, vhi, vlo);
151 }
152 cpu_fprintf(f, "FPCR: %08x FPSR: %08x\n",
153 vfp_get_fpcr(env), vfp_get_fpsr(env));
154 }
Alexander Graf14ade102013-09-03 20:12:10 +0100155}
156
Peter Maydell4a08d472013-12-22 22:32:27 +0000157static int get_mem_index(DisasContext *s)
158{
159#ifdef CONFIG_USER_ONLY
160 return 1;
161#else
162 return s->user;
163#endif
164}
165
Alexander Graf14ade102013-09-03 20:12:10 +0100166void gen_a64_set_pc_im(uint64_t val)
167{
168 tcg_gen_movi_i64(cpu_pc, val);
169}
170
171static void gen_exception(int excp)
172{
173 TCGv_i32 tmp = tcg_temp_new_i32();
174 tcg_gen_movi_i32(tmp, excp);
175 gen_helper_exception(cpu_env, tmp);
176 tcg_temp_free_i32(tmp);
177}
178
179static void gen_exception_insn(DisasContext *s, int offset, int excp)
180{
181 gen_a64_set_pc_im(s->pc - offset);
182 gen_exception(excp);
Peter Maydell40f860c2013-12-17 19:42:31 +0000183 s->is_jmp = DISAS_EXC;
184}
185
186static inline bool use_goto_tb(DisasContext *s, int n, uint64_t dest)
187{
188 /* No direct tb linking with singlestep or deterministic io */
189 if (s->singlestep_enabled || (s->tb->cflags & CF_LAST_IO)) {
190 return false;
191 }
192
193 /* Only link tbs from inside the same guest page */
194 if ((s->tb->pc & TARGET_PAGE_MASK) != (dest & TARGET_PAGE_MASK)) {
195 return false;
196 }
197
198 return true;
199}
200
201static inline void gen_goto_tb(DisasContext *s, int n, uint64_t dest)
202{
203 TranslationBlock *tb;
204
205 tb = s->tb;
206 if (use_goto_tb(s, n, dest)) {
207 tcg_gen_goto_tb(n);
208 gen_a64_set_pc_im(dest);
209 tcg_gen_exit_tb((tcg_target_long)tb + n);
210 s->is_jmp = DISAS_TB_JUMP;
211 } else {
212 gen_a64_set_pc_im(dest);
213 if (s->singlestep_enabled) {
214 gen_exception(EXCP_DEBUG);
215 }
216 tcg_gen_exit_tb(0);
217 s->is_jmp = DISAS_JUMP;
218 }
Alexander Graf14ade102013-09-03 20:12:10 +0100219}
220
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +0000221static void unallocated_encoding(DisasContext *s)
Alexander Graf14ade102013-09-03 20:12:10 +0100222{
Alexander Graf14ade102013-09-03 20:12:10 +0100223 gen_exception_insn(s, 4, EXCP_UDEF);
224}
225
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +0000226#define unsupported_encoding(s, insn) \
227 do { \
228 qemu_log_mask(LOG_UNIMP, \
229 "%s:%d: unsupported instruction encoding 0x%08x " \
230 "at pc=%016" PRIx64 "\n", \
231 __FILE__, __LINE__, insn, s->pc - 4); \
232 unallocated_encoding(s); \
233 } while (0);
Alexander Graf14ade102013-09-03 20:12:10 +0100234
Alexander Graf11e169d2013-12-17 19:42:32 +0000235static void init_tmp_a64_array(DisasContext *s)
236{
237#ifdef CONFIG_DEBUG_TCG
238 int i;
239 for (i = 0; i < ARRAY_SIZE(s->tmp_a64); i++) {
240 TCGV_UNUSED_I64(s->tmp_a64[i]);
241 }
242#endif
243 s->tmp_a64_count = 0;
244}
245
246static void free_tmp_a64(DisasContext *s)
247{
248 int i;
249 for (i = 0; i < s->tmp_a64_count; i++) {
250 tcg_temp_free_i64(s->tmp_a64[i]);
251 }
252 init_tmp_a64_array(s);
253}
254
255static TCGv_i64 new_tmp_a64(DisasContext *s)
256{
257 assert(s->tmp_a64_count < TMP_A64_MAX);
258 return s->tmp_a64[s->tmp_a64_count++] = tcg_temp_new_i64();
259}
260
261static TCGv_i64 new_tmp_a64_zero(DisasContext *s)
262{
263 TCGv_i64 t = new_tmp_a64(s);
264 tcg_gen_movi_i64(t, 0);
265 return t;
266}
267
Alexander Graf71b46082013-12-17 19:42:36 +0000268/*
269 * Register access functions
270 *
271 * These functions are used for directly accessing a register in where
272 * changes to the final register value are likely to be made. If you
273 * need to use a register for temporary calculation (e.g. index type
274 * operations) use the read_* form.
275 *
276 * B1.2.1 Register mappings
277 *
278 * In instruction register encoding 31 can refer to ZR (zero register) or
279 * the SP (stack pointer) depending on context. In QEMU's case we map SP
280 * to cpu_X[31] and ZR accesses to a temporary which can be discarded.
281 * This is the point of the _sp forms.
282 */
Alexander Graf11e169d2013-12-17 19:42:32 +0000283static TCGv_i64 cpu_reg(DisasContext *s, int reg)
284{
285 if (reg == 31) {
286 return new_tmp_a64_zero(s);
287 } else {
288 return cpu_X[reg];
289 }
290}
291
Alexander Graf71b46082013-12-17 19:42:36 +0000292/* register access for when 31 == SP */
293static TCGv_i64 cpu_reg_sp(DisasContext *s, int reg)
294{
295 return cpu_X[reg];
296}
297
Alexander Graf60e53382013-12-17 19:42:33 +0000298/* read a cpu register in 32bit/64bit mode. Returns a TCGv_i64
299 * representing the register contents. This TCGv is an auto-freed
300 * temporary so it need not be explicitly freed, and may be modified.
301 */
302static TCGv_i64 read_cpu_reg(DisasContext *s, int reg, int sf)
303{
304 TCGv_i64 v = new_tmp_a64(s);
305 if (reg != 31) {
306 if (sf) {
307 tcg_gen_mov_i64(v, cpu_X[reg]);
308 } else {
309 tcg_gen_ext32u_i64(v, cpu_X[reg]);
310 }
311 } else {
312 tcg_gen_movi_i64(v, 0);
313 }
314 return v;
315}
316
Peter Maydell4a08d472013-12-22 22:32:27 +0000317static TCGv_i64 read_cpu_reg_sp(DisasContext *s, int reg, int sf)
318{
319 TCGv_i64 v = new_tmp_a64(s);
320 if (sf) {
321 tcg_gen_mov_i64(v, cpu_X[reg]);
322 } else {
323 tcg_gen_ext32u_i64(v, cpu_X[reg]);
324 }
325 return v;
326}
327
Alex Bennée539ed652014-01-23 14:37:07 +0000328/* Return the offset into CPUARMState of an element of specified
329 * size, 'element' places in from the least significant end of
330 * the FP/vector register Qn.
331 */
332static inline int vec_reg_offset(int regno, int element, TCGMemOp size)
333{
334 int offs = offsetof(CPUARMState, vfp.regs[regno * 2]);
335#ifdef HOST_WORDS_BIGENDIAN
336 /* This is complicated slightly because vfp.regs[2n] is
337 * still the low half and vfp.regs[2n+1] the high half
338 * of the 128 bit vector, even on big endian systems.
339 * Calculate the offset assuming a fully bigendian 128 bits,
340 * then XOR to account for the order of the two 64 bit halves.
341 */
342 offs += (16 - ((element + 1) * (1 << size)));
343 offs ^= 8;
344#else
345 offs += element * (1 << size);
346#endif
347 return offs;
348}
349
Peter Maydelle2f90562014-01-04 22:15:49 +0000350/* Return the offset into CPUARMState of a slice (from
351 * the least significant end) of FP register Qn (ie
352 * Dn, Sn, Hn or Bn).
353 * (Note that this is not the same mapping as for A32; see cpu.h)
354 */
355static inline int fp_reg_offset(int regno, TCGMemOp size)
356{
357 int offs = offsetof(CPUARMState, vfp.regs[regno * 2]);
358#ifdef HOST_WORDS_BIGENDIAN
359 offs += (8 - (1 << size));
360#endif
361 return offs;
362}
363
364/* Offset of the high half of the 128 bit vector Qn */
365static inline int fp_reg_hi_offset(int regno)
366{
367 return offsetof(CPUARMState, vfp.regs[regno * 2 + 1]);
368}
369
Alexander Grafec73d2e2014-01-04 22:15:50 +0000370/* Convenience accessors for reading and writing single and double
371 * FP registers. Writing clears the upper parts of the associated
372 * 128 bit vector register, as required by the architecture.
373 * Note that unlike the GP register accessors, the values returned
374 * by the read functions must be manually freed.
375 */
376static TCGv_i64 read_fp_dreg(DisasContext *s, int reg)
377{
378 TCGv_i64 v = tcg_temp_new_i64();
379
380 tcg_gen_ld_i64(v, cpu_env, fp_reg_offset(reg, MO_64));
381 return v;
382}
383
384static TCGv_i32 read_fp_sreg(DisasContext *s, int reg)
385{
386 TCGv_i32 v = tcg_temp_new_i32();
387
388 tcg_gen_ld_i32(v, cpu_env, fp_reg_offset(reg, MO_32));
389 return v;
390}
391
392static void write_fp_dreg(DisasContext *s, int reg, TCGv_i64 v)
393{
394 TCGv_i64 tcg_zero = tcg_const_i64(0);
395
396 tcg_gen_st_i64(v, cpu_env, fp_reg_offset(reg, MO_64));
397 tcg_gen_st_i64(tcg_zero, cpu_env, fp_reg_hi_offset(reg));
398 tcg_temp_free_i64(tcg_zero);
399}
400
401static void write_fp_sreg(DisasContext *s, int reg, TCGv_i32 v)
402{
403 TCGv_i64 tmp = tcg_temp_new_i64();
404
405 tcg_gen_extu_i32_i64(tmp, v);
406 write_fp_dreg(s, reg, tmp);
407 tcg_temp_free_i64(tmp);
408}
409
410static TCGv_ptr get_fpstatus_ptr(void)
411{
412 TCGv_ptr statusptr = tcg_temp_new_ptr();
413 int offset;
414
415 /* In A64 all instructions (both FP and Neon) use the FPCR;
416 * there is no equivalent of the A32 Neon "standard FPSCR value"
417 * and all operations use vfp.fp_status.
418 */
419 offset = offsetof(CPUARMState, vfp.fp_status);
420 tcg_gen_addi_ptr(statusptr, cpu_env, offset);
421 return statusptr;
422}
423
Alexander Graf832ffa12013-12-17 19:42:34 +0000424/* Set ZF and NF based on a 64 bit result. This is alas fiddlier
425 * than the 32 bit equivalent.
426 */
427static inline void gen_set_NZ64(TCGv_i64 result)
428{
429 TCGv_i64 flag = tcg_temp_new_i64();
430
431 tcg_gen_setcondi_i64(TCG_COND_NE, flag, result, 0);
432 tcg_gen_trunc_i64_i32(cpu_ZF, flag);
433 tcg_gen_shri_i64(flag, result, 32);
434 tcg_gen_trunc_i64_i32(cpu_NF, flag);
435 tcg_temp_free_i64(flag);
436}
437
438/* Set NZCV as for a logical operation: NZ as per result, CV cleared. */
439static inline void gen_logic_CC(int sf, TCGv_i64 result)
440{
441 if (sf) {
442 gen_set_NZ64(result);
443 } else {
444 tcg_gen_trunc_i64_i32(cpu_ZF, result);
445 tcg_gen_trunc_i64_i32(cpu_NF, result);
446 }
447 tcg_gen_movi_i32(cpu_CF, 0);
448 tcg_gen_movi_i32(cpu_VF, 0);
449}
450
Alex Bennéeb0ff21b2013-12-23 23:27:29 +0000451/* dest = T0 + T1; compute C, N, V and Z flags */
452static void gen_add_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
453{
454 if (sf) {
455 TCGv_i64 result, flag, tmp;
456 result = tcg_temp_new_i64();
457 flag = tcg_temp_new_i64();
458 tmp = tcg_temp_new_i64();
459
460 tcg_gen_movi_i64(tmp, 0);
461 tcg_gen_add2_i64(result, flag, t0, tmp, t1, tmp);
462
463 tcg_gen_trunc_i64_i32(cpu_CF, flag);
464
465 gen_set_NZ64(result);
466
467 tcg_gen_xor_i64(flag, result, t0);
468 tcg_gen_xor_i64(tmp, t0, t1);
469 tcg_gen_andc_i64(flag, flag, tmp);
470 tcg_temp_free_i64(tmp);
471 tcg_gen_shri_i64(flag, flag, 32);
472 tcg_gen_trunc_i64_i32(cpu_VF, flag);
473
474 tcg_gen_mov_i64(dest, result);
475 tcg_temp_free_i64(result);
476 tcg_temp_free_i64(flag);
477 } else {
478 /* 32 bit arithmetic */
479 TCGv_i32 t0_32 = tcg_temp_new_i32();
480 TCGv_i32 t1_32 = tcg_temp_new_i32();
481 TCGv_i32 tmp = tcg_temp_new_i32();
482
483 tcg_gen_movi_i32(tmp, 0);
484 tcg_gen_trunc_i64_i32(t0_32, t0);
485 tcg_gen_trunc_i64_i32(t1_32, t1);
486 tcg_gen_add2_i32(cpu_NF, cpu_CF, t0_32, tmp, t1_32, tmp);
487 tcg_gen_mov_i32(cpu_ZF, cpu_NF);
488 tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
489 tcg_gen_xor_i32(tmp, t0_32, t1_32);
490 tcg_gen_andc_i32(cpu_VF, cpu_VF, tmp);
491 tcg_gen_extu_i32_i64(dest, cpu_NF);
492
493 tcg_temp_free_i32(tmp);
494 tcg_temp_free_i32(t0_32);
495 tcg_temp_free_i32(t1_32);
496 }
497}
498
499/* dest = T0 - T1; compute C, N, V and Z flags */
500static void gen_sub_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
501{
502 if (sf) {
503 /* 64 bit arithmetic */
504 TCGv_i64 result, flag, tmp;
505
506 result = tcg_temp_new_i64();
507 flag = tcg_temp_new_i64();
508 tcg_gen_sub_i64(result, t0, t1);
509
510 gen_set_NZ64(result);
511
512 tcg_gen_setcond_i64(TCG_COND_GEU, flag, t0, t1);
513 tcg_gen_trunc_i64_i32(cpu_CF, flag);
514
515 tcg_gen_xor_i64(flag, result, t0);
516 tmp = tcg_temp_new_i64();
517 tcg_gen_xor_i64(tmp, t0, t1);
518 tcg_gen_and_i64(flag, flag, tmp);
519 tcg_temp_free_i64(tmp);
520 tcg_gen_shri_i64(flag, flag, 32);
521 tcg_gen_trunc_i64_i32(cpu_VF, flag);
522 tcg_gen_mov_i64(dest, result);
523 tcg_temp_free_i64(flag);
524 tcg_temp_free_i64(result);
525 } else {
526 /* 32 bit arithmetic */
527 TCGv_i32 t0_32 = tcg_temp_new_i32();
528 TCGv_i32 t1_32 = tcg_temp_new_i32();
529 TCGv_i32 tmp;
530
531 tcg_gen_trunc_i64_i32(t0_32, t0);
532 tcg_gen_trunc_i64_i32(t1_32, t1);
533 tcg_gen_sub_i32(cpu_NF, t0_32, t1_32);
534 tcg_gen_mov_i32(cpu_ZF, cpu_NF);
535 tcg_gen_setcond_i32(TCG_COND_GEU, cpu_CF, t0_32, t1_32);
536 tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
537 tmp = tcg_temp_new_i32();
538 tcg_gen_xor_i32(tmp, t0_32, t1_32);
539 tcg_temp_free_i32(t0_32);
540 tcg_temp_free_i32(t1_32);
541 tcg_gen_and_i32(cpu_VF, cpu_VF, tmp);
542 tcg_temp_free_i32(tmp);
543 tcg_gen_extu_i32_i64(dest, cpu_NF);
544 }
545}
546
Claudio Fontana643dbb02014-01-04 22:15:46 +0000547/* dest = T0 + T1 + CF; do not compute flags. */
548static void gen_adc(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
549{
550 TCGv_i64 flag = tcg_temp_new_i64();
551 tcg_gen_extu_i32_i64(flag, cpu_CF);
552 tcg_gen_add_i64(dest, t0, t1);
553 tcg_gen_add_i64(dest, dest, flag);
554 tcg_temp_free_i64(flag);
555
556 if (!sf) {
557 tcg_gen_ext32u_i64(dest, dest);
558 }
559}
560
561/* dest = T0 + T1 + CF; compute C, N, V and Z flags. */
562static void gen_adc_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
563{
564 if (sf) {
565 TCGv_i64 result, cf_64, vf_64, tmp;
566 result = tcg_temp_new_i64();
567 cf_64 = tcg_temp_new_i64();
568 vf_64 = tcg_temp_new_i64();
569 tmp = tcg_const_i64(0);
570
571 tcg_gen_extu_i32_i64(cf_64, cpu_CF);
572 tcg_gen_add2_i64(result, cf_64, t0, tmp, cf_64, tmp);
573 tcg_gen_add2_i64(result, cf_64, result, cf_64, t1, tmp);
574 tcg_gen_trunc_i64_i32(cpu_CF, cf_64);
575 gen_set_NZ64(result);
576
577 tcg_gen_xor_i64(vf_64, result, t0);
578 tcg_gen_xor_i64(tmp, t0, t1);
579 tcg_gen_andc_i64(vf_64, vf_64, tmp);
580 tcg_gen_shri_i64(vf_64, vf_64, 32);
581 tcg_gen_trunc_i64_i32(cpu_VF, vf_64);
582
583 tcg_gen_mov_i64(dest, result);
584
585 tcg_temp_free_i64(tmp);
586 tcg_temp_free_i64(vf_64);
587 tcg_temp_free_i64(cf_64);
588 tcg_temp_free_i64(result);
589 } else {
590 TCGv_i32 t0_32, t1_32, tmp;
591 t0_32 = tcg_temp_new_i32();
592 t1_32 = tcg_temp_new_i32();
593 tmp = tcg_const_i32(0);
594
595 tcg_gen_trunc_i64_i32(t0_32, t0);
596 tcg_gen_trunc_i64_i32(t1_32, t1);
597 tcg_gen_add2_i32(cpu_NF, cpu_CF, t0_32, tmp, cpu_CF, tmp);
598 tcg_gen_add2_i32(cpu_NF, cpu_CF, cpu_NF, cpu_CF, t1_32, tmp);
599
600 tcg_gen_mov_i32(cpu_ZF, cpu_NF);
601 tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
602 tcg_gen_xor_i32(tmp, t0_32, t1_32);
603 tcg_gen_andc_i32(cpu_VF, cpu_VF, tmp);
604 tcg_gen_extu_i32_i64(dest, cpu_NF);
605
606 tcg_temp_free_i32(tmp);
607 tcg_temp_free_i32(t1_32);
608 tcg_temp_free_i32(t0_32);
609 }
610}
611
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +0000612/*
Peter Maydell4a08d472013-12-22 22:32:27 +0000613 * Load/Store generators
614 */
615
616/*
617 * Store from GPR register to memory
618 */
619static void do_gpr_st(DisasContext *s, TCGv_i64 source,
620 TCGv_i64 tcg_addr, int size)
621{
622 g_assert(size <= 3);
623 tcg_gen_qemu_st_i64(source, tcg_addr, get_mem_index(s), MO_TE + size);
624}
625
626/*
627 * Load from memory to GPR register
628 */
629static void do_gpr_ld(DisasContext *s, TCGv_i64 dest, TCGv_i64 tcg_addr,
630 int size, bool is_signed, bool extend)
631{
632 TCGMemOp memop = MO_TE + size;
633
634 g_assert(size <= 3);
635
636 if (is_signed) {
637 memop += MO_SIGN;
638 }
639
640 tcg_gen_qemu_ld_i64(dest, tcg_addr, get_mem_index(s), memop);
641
642 if (extend && is_signed) {
643 g_assert(size < 3);
644 tcg_gen_ext32u_i64(dest, dest);
645 }
646}
647
648/*
649 * Store from FP register to memory
650 */
651static void do_fp_st(DisasContext *s, int srcidx, TCGv_i64 tcg_addr, int size)
652{
653 /* This writes the bottom N bits of a 128 bit wide vector to memory */
Peter Maydell4a08d472013-12-22 22:32:27 +0000654 TCGv_i64 tmp = tcg_temp_new_i64();
Peter Maydelle2f90562014-01-04 22:15:49 +0000655 tcg_gen_ld_i64(tmp, cpu_env, fp_reg_offset(srcidx, MO_64));
Peter Maydell4a08d472013-12-22 22:32:27 +0000656 if (size < 4) {
Peter Maydell4a08d472013-12-22 22:32:27 +0000657 tcg_gen_qemu_st_i64(tmp, tcg_addr, get_mem_index(s), MO_TE + size);
658 } else {
659 TCGv_i64 tcg_hiaddr = tcg_temp_new_i64();
Peter Maydell4a08d472013-12-22 22:32:27 +0000660 tcg_gen_qemu_st_i64(tmp, tcg_addr, get_mem_index(s), MO_TEQ);
661 tcg_gen_qemu_st64(tmp, tcg_addr, get_mem_index(s));
Peter Maydelle2f90562014-01-04 22:15:49 +0000662 tcg_gen_ld_i64(tmp, cpu_env, fp_reg_hi_offset(srcidx));
Peter Maydell4a08d472013-12-22 22:32:27 +0000663 tcg_gen_addi_i64(tcg_hiaddr, tcg_addr, 8);
664 tcg_gen_qemu_st_i64(tmp, tcg_hiaddr, get_mem_index(s), MO_TEQ);
665 tcg_temp_free_i64(tcg_hiaddr);
666 }
667
668 tcg_temp_free_i64(tmp);
669}
670
671/*
672 * Load from memory to FP register
673 */
674static void do_fp_ld(DisasContext *s, int destidx, TCGv_i64 tcg_addr, int size)
675{
676 /* This always zero-extends and writes to a full 128 bit wide vector */
Peter Maydell4a08d472013-12-22 22:32:27 +0000677 TCGv_i64 tmplo = tcg_temp_new_i64();
678 TCGv_i64 tmphi;
679
680 if (size < 4) {
681 TCGMemOp memop = MO_TE + size;
682 tmphi = tcg_const_i64(0);
683 tcg_gen_qemu_ld_i64(tmplo, tcg_addr, get_mem_index(s), memop);
684 } else {
685 TCGv_i64 tcg_hiaddr;
686 tmphi = tcg_temp_new_i64();
687 tcg_hiaddr = tcg_temp_new_i64();
688
689 tcg_gen_qemu_ld_i64(tmplo, tcg_addr, get_mem_index(s), MO_TEQ);
690 tcg_gen_addi_i64(tcg_hiaddr, tcg_addr, 8);
691 tcg_gen_qemu_ld_i64(tmphi, tcg_hiaddr, get_mem_index(s), MO_TEQ);
692 tcg_temp_free_i64(tcg_hiaddr);
693 }
694
Peter Maydelle2f90562014-01-04 22:15:49 +0000695 tcg_gen_st_i64(tmplo, cpu_env, fp_reg_offset(destidx, MO_64));
696 tcg_gen_st_i64(tmphi, cpu_env, fp_reg_hi_offset(destidx));
Peter Maydell4a08d472013-12-22 22:32:27 +0000697
698 tcg_temp_free_i64(tmplo);
699 tcg_temp_free_i64(tmphi);
700}
701
Alex Bennée229b7a02013-12-23 23:27:29 +0000702/*
Alex Bennée539ed652014-01-23 14:37:07 +0000703 * Vector load/store helpers.
704 *
705 * The principal difference between this and a FP load is that we don't
706 * zero extend as we are filling a partial chunk of the vector register.
707 * These functions don't support 128 bit loads/stores, which would be
708 * normal load/store operations.
Peter Maydellac224302014-01-14 12:44:38 +0000709 *
710 * The _i32 versions are useful when operating on 32 bit quantities
711 * (eg for floating point single or using Neon helper functions).
Alex Bennée539ed652014-01-23 14:37:07 +0000712 */
713
714/* Get value of an element within a vector register */
715static void read_vec_element(DisasContext *s, TCGv_i64 tcg_dest, int srcidx,
716 int element, TCGMemOp memop)
717{
718 int vect_off = vec_reg_offset(srcidx, element, memop & MO_SIZE);
719 switch (memop) {
720 case MO_8:
721 tcg_gen_ld8u_i64(tcg_dest, cpu_env, vect_off);
722 break;
723 case MO_16:
724 tcg_gen_ld16u_i64(tcg_dest, cpu_env, vect_off);
725 break;
726 case MO_32:
727 tcg_gen_ld32u_i64(tcg_dest, cpu_env, vect_off);
728 break;
729 case MO_8|MO_SIGN:
730 tcg_gen_ld8s_i64(tcg_dest, cpu_env, vect_off);
731 break;
732 case MO_16|MO_SIGN:
733 tcg_gen_ld16s_i64(tcg_dest, cpu_env, vect_off);
734 break;
735 case MO_32|MO_SIGN:
736 tcg_gen_ld32s_i64(tcg_dest, cpu_env, vect_off);
737 break;
738 case MO_64:
739 case MO_64|MO_SIGN:
740 tcg_gen_ld_i64(tcg_dest, cpu_env, vect_off);
741 break;
742 default:
743 g_assert_not_reached();
744 }
745}
746
Peter Maydellac224302014-01-14 12:44:38 +0000747static void read_vec_element_i32(DisasContext *s, TCGv_i32 tcg_dest, int srcidx,
748 int element, TCGMemOp memop)
749{
750 int vect_off = vec_reg_offset(srcidx, element, memop & MO_SIZE);
751 switch (memop) {
752 case MO_8:
753 tcg_gen_ld8u_i32(tcg_dest, cpu_env, vect_off);
754 break;
755 case MO_16:
756 tcg_gen_ld16u_i32(tcg_dest, cpu_env, vect_off);
757 break;
758 case MO_8|MO_SIGN:
759 tcg_gen_ld8s_i32(tcg_dest, cpu_env, vect_off);
760 break;
761 case MO_16|MO_SIGN:
762 tcg_gen_ld16s_i32(tcg_dest, cpu_env, vect_off);
763 break;
764 case MO_32:
765 case MO_32|MO_SIGN:
766 tcg_gen_ld_i32(tcg_dest, cpu_env, vect_off);
767 break;
768 default:
769 g_assert_not_reached();
770 }
771}
772
Alex Bennée539ed652014-01-23 14:37:07 +0000773/* Set value of an element within a vector register */
774static void write_vec_element(DisasContext *s, TCGv_i64 tcg_src, int destidx,
775 int element, TCGMemOp memop)
776{
777 int vect_off = vec_reg_offset(destidx, element, memop & MO_SIZE);
778 switch (memop) {
779 case MO_8:
780 tcg_gen_st8_i64(tcg_src, cpu_env, vect_off);
781 break;
782 case MO_16:
783 tcg_gen_st16_i64(tcg_src, cpu_env, vect_off);
784 break;
785 case MO_32:
786 tcg_gen_st32_i64(tcg_src, cpu_env, vect_off);
787 break;
788 case MO_64:
789 tcg_gen_st_i64(tcg_src, cpu_env, vect_off);
790 break;
791 default:
792 g_assert_not_reached();
793 }
794}
795
Peter Maydell0d19b492014-01-23 13:13:41 +0000796static void write_vec_element_i32(DisasContext *s, TCGv_i32 tcg_src,
797 int destidx, int element, TCGMemOp memop)
798{
799 int vect_off = vec_reg_offset(destidx, element, memop & MO_SIZE);
800 switch (memop) {
801 case MO_8:
802 tcg_gen_st8_i32(tcg_src, cpu_env, vect_off);
803 break;
804 case MO_16:
805 tcg_gen_st16_i32(tcg_src, cpu_env, vect_off);
806 break;
807 case MO_32:
808 tcg_gen_st_i32(tcg_src, cpu_env, vect_off);
809 break;
810 default:
811 g_assert_not_reached();
812 }
813}
814
Alex Bennée539ed652014-01-23 14:37:07 +0000815/* Clear the high 64 bits of a 128 bit vector (in general non-quad
816 * vector ops all need to do this).
817 */
818static void clear_vec_high(DisasContext *s, int rd)
819{
820 TCGv_i64 tcg_zero = tcg_const_i64(0);
821
822 write_vec_element(s, tcg_zero, rd, 1, MO_64);
823 tcg_temp_free_i64(tcg_zero);
824}
825
826/* Store from vector register to memory */
827static void do_vec_st(DisasContext *s, int srcidx, int element,
828 TCGv_i64 tcg_addr, int size)
829{
830 TCGMemOp memop = MO_TE + size;
831 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
832
833 read_vec_element(s, tcg_tmp, srcidx, element, size);
834 tcg_gen_qemu_st_i64(tcg_tmp, tcg_addr, get_mem_index(s), memop);
835
836 tcg_temp_free_i64(tcg_tmp);
837}
838
839/* Load from memory to vector register */
840static void do_vec_ld(DisasContext *s, int destidx, int element,
841 TCGv_i64 tcg_addr, int size)
842{
843 TCGMemOp memop = MO_TE + size;
844 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
845
846 tcg_gen_qemu_ld_i64(tcg_tmp, tcg_addr, get_mem_index(s), memop);
847 write_vec_element(s, tcg_tmp, destidx, element, size);
848
849 tcg_temp_free_i64(tcg_tmp);
850}
851
852/*
Alex Bennée229b7a02013-12-23 23:27:29 +0000853 * This utility function is for doing register extension with an
854 * optional shift. You will likely want to pass a temporary for the
855 * destination register. See DecodeRegExtend() in the ARM ARM.
856 */
857static void ext_and_shift_reg(TCGv_i64 tcg_out, TCGv_i64 tcg_in,
858 int option, unsigned int shift)
859{
860 int extsize = extract32(option, 0, 2);
861 bool is_signed = extract32(option, 2, 1);
862
863 if (is_signed) {
864 switch (extsize) {
865 case 0:
866 tcg_gen_ext8s_i64(tcg_out, tcg_in);
867 break;
868 case 1:
869 tcg_gen_ext16s_i64(tcg_out, tcg_in);
870 break;
871 case 2:
872 tcg_gen_ext32s_i64(tcg_out, tcg_in);
873 break;
874 case 3:
875 tcg_gen_mov_i64(tcg_out, tcg_in);
876 break;
877 }
878 } else {
879 switch (extsize) {
880 case 0:
881 tcg_gen_ext8u_i64(tcg_out, tcg_in);
882 break;
883 case 1:
884 tcg_gen_ext16u_i64(tcg_out, tcg_in);
885 break;
886 case 2:
887 tcg_gen_ext32u_i64(tcg_out, tcg_in);
888 break;
889 case 3:
890 tcg_gen_mov_i64(tcg_out, tcg_in);
891 break;
892 }
893 }
894
895 if (shift) {
896 tcg_gen_shli_i64(tcg_out, tcg_out, shift);
897 }
898}
899
Peter Maydell4a08d472013-12-22 22:32:27 +0000900static inline void gen_check_sp_alignment(DisasContext *s)
901{
902 /* The AArch64 architecture mandates that (if enabled via PSTATE
903 * or SCTLR bits) there is a check that SP is 16-aligned on every
904 * SP-relative load or store (with an exception generated if it is not).
905 * In line with general QEMU practice regarding misaligned accesses,
906 * we omit these checks for the sake of guest program performance.
907 * This function is provided as a hook so we can more easily add these
908 * checks in future (possibly as a "favour catching guest program bugs
909 * over speed" user selectable option).
910 */
911}
912
913/*
Alex Bennée94ad8702014-01-23 14:37:07 +0000914 * This provides a simple table based table lookup decoder. It is
915 * intended to be used when the relevant bits for decode are too
916 * awkwardly placed and switch/if based logic would be confusing and
917 * deeply nested. Since it's a linear search through the table, tables
918 * should be kept small.
919 *
920 * It returns the first handler where insn & mask == pattern, or
921 * NULL if there is no match.
922 * The table is terminated by an empty mask (i.e. 0)
923 */
924static inline AArch64DecodeFn *lookup_disas_fn(const AArch64DecodeTable *table,
925 uint32_t insn)
926{
927 const AArch64DecodeTable *tptr = table;
928
929 while (tptr->mask) {
930 if ((insn & tptr->mask) == tptr->pattern) {
931 return tptr->disas_fn;
932 }
933 tptr++;
934 }
935 return NULL;
936}
937
938/*
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +0000939 * the instruction disassembly implemented here matches
940 * the instruction encoding classifications in chapter 3 (C3)
941 * of the ARM Architecture Reference Manual (DDI0487A_a)
942 */
943
Alexander Graf11e169d2013-12-17 19:42:32 +0000944/* C3.2.7 Unconditional branch (immediate)
945 * 31 30 26 25 0
946 * +----+-----------+-------------------------------------+
947 * | op | 0 0 1 0 1 | imm26 |
948 * +----+-----------+-------------------------------------+
949 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +0000950static void disas_uncond_b_imm(DisasContext *s, uint32_t insn)
951{
Alexander Graf11e169d2013-12-17 19:42:32 +0000952 uint64_t addr = s->pc + sextract32(insn, 0, 26) * 4 - 4;
953
954 if (insn & (1 << 31)) {
955 /* C5.6.26 BL Branch with link */
956 tcg_gen_movi_i64(cpu_reg(s, 30), s->pc);
957 }
958
959 /* C5.6.20 B Branch / C5.6.26 BL Branch with link */
960 gen_goto_tb(s, 0, addr);
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +0000961}
962
Alexander Graf60e53382013-12-17 19:42:33 +0000963/* C3.2.1 Compare & branch (immediate)
964 * 31 30 25 24 23 5 4 0
965 * +----+-------------+----+---------------------+--------+
966 * | sf | 0 1 1 0 1 0 | op | imm19 | Rt |
967 * +----+-------------+----+---------------------+--------+
968 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +0000969static void disas_comp_b_imm(DisasContext *s, uint32_t insn)
970{
Alexander Graf60e53382013-12-17 19:42:33 +0000971 unsigned int sf, op, rt;
972 uint64_t addr;
973 int label_match;
974 TCGv_i64 tcg_cmp;
975
976 sf = extract32(insn, 31, 1);
977 op = extract32(insn, 24, 1); /* 0: CBZ; 1: CBNZ */
978 rt = extract32(insn, 0, 5);
979 addr = s->pc + sextract32(insn, 5, 19) * 4 - 4;
980
981 tcg_cmp = read_cpu_reg(s, rt, sf);
982 label_match = gen_new_label();
983
984 tcg_gen_brcondi_i64(op ? TCG_COND_NE : TCG_COND_EQ,
985 tcg_cmp, 0, label_match);
986
987 gen_goto_tb(s, 0, s->pc);
988 gen_set_label(label_match);
989 gen_goto_tb(s, 1, addr);
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +0000990}
991
Alexander Grafdb0f7952013-12-17 19:42:33 +0000992/* C3.2.5 Test & branch (immediate)
993 * 31 30 25 24 23 19 18 5 4 0
994 * +----+-------------+----+-------+-------------+------+
995 * | b5 | 0 1 1 0 1 1 | op | b40 | imm14 | Rt |
996 * +----+-------------+----+-------+-------------+------+
997 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +0000998static void disas_test_b_imm(DisasContext *s, uint32_t insn)
999{
Alexander Grafdb0f7952013-12-17 19:42:33 +00001000 unsigned int bit_pos, op, rt;
1001 uint64_t addr;
1002 int label_match;
1003 TCGv_i64 tcg_cmp;
1004
1005 bit_pos = (extract32(insn, 31, 1) << 5) | extract32(insn, 19, 5);
1006 op = extract32(insn, 24, 1); /* 0: TBZ; 1: TBNZ */
1007 addr = s->pc + sextract32(insn, 5, 14) * 4 - 4;
1008 rt = extract32(insn, 0, 5);
1009
1010 tcg_cmp = tcg_temp_new_i64();
1011 tcg_gen_andi_i64(tcg_cmp, cpu_reg(s, rt), (1ULL << bit_pos));
1012 label_match = gen_new_label();
1013 tcg_gen_brcondi_i64(op ? TCG_COND_NE : TCG_COND_EQ,
1014 tcg_cmp, 0, label_match);
1015 tcg_temp_free_i64(tcg_cmp);
1016 gen_goto_tb(s, 0, s->pc);
1017 gen_set_label(label_match);
1018 gen_goto_tb(s, 1, addr);
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00001019}
1020
Alexander Graf39fb7302013-12-17 19:42:33 +00001021/* C3.2.2 / C5.6.19 Conditional branch (immediate)
1022 * 31 25 24 23 5 4 3 0
1023 * +---------------+----+---------------------+----+------+
1024 * | 0 1 0 1 0 1 0 | o1 | imm19 | o0 | cond |
1025 * +---------------+----+---------------------+----+------+
1026 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00001027static void disas_cond_b_imm(DisasContext *s, uint32_t insn)
1028{
Alexander Graf39fb7302013-12-17 19:42:33 +00001029 unsigned int cond;
1030 uint64_t addr;
1031
1032 if ((insn & (1 << 4)) || (insn & (1 << 24))) {
1033 unallocated_encoding(s);
1034 return;
1035 }
1036 addr = s->pc + sextract32(insn, 5, 19) * 4 - 4;
1037 cond = extract32(insn, 0, 4);
1038
1039 if (cond < 0x0e) {
1040 /* genuinely conditional branches */
1041 int label_match = gen_new_label();
1042 arm_gen_test_cc(cond, label_match);
1043 gen_goto_tb(s, 0, s->pc);
1044 gen_set_label(label_match);
1045 gen_goto_tb(s, 1, addr);
1046 } else {
1047 /* 0xe and 0xf are both "always" conditions */
1048 gen_goto_tb(s, 0, addr);
1049 }
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00001050}
1051
Claudio Fontana87462e02013-12-17 19:42:32 +00001052/* C5.6.68 HINT */
1053static void handle_hint(DisasContext *s, uint32_t insn,
1054 unsigned int op1, unsigned int op2, unsigned int crm)
1055{
1056 unsigned int selector = crm << 3 | op2;
1057
1058 if (op1 != 3) {
1059 unallocated_encoding(s);
1060 return;
1061 }
1062
1063 switch (selector) {
1064 case 0: /* NOP */
1065 return;
1066 case 1: /* YIELD */
1067 case 2: /* WFE */
1068 case 3: /* WFI */
1069 case 4: /* SEV */
1070 case 5: /* SEVL */
1071 /* we treat all as NOP at least for now */
1072 return;
1073 default:
1074 /* default specified as NOP equivalent */
1075 return;
1076 }
1077}
1078
Michael Matzfa2ef212014-01-04 22:15:47 +00001079static void gen_clrex(DisasContext *s, uint32_t insn)
1080{
1081 tcg_gen_movi_i64(cpu_exclusive_addr, -1);
1082}
1083
Claudio Fontana87462e02013-12-17 19:42:32 +00001084/* CLREX, DSB, DMB, ISB */
1085static void handle_sync(DisasContext *s, uint32_t insn,
1086 unsigned int op1, unsigned int op2, unsigned int crm)
1087{
1088 if (op1 != 3) {
1089 unallocated_encoding(s);
1090 return;
1091 }
1092
1093 switch (op2) {
1094 case 2: /* CLREX */
Michael Matzfa2ef212014-01-04 22:15:47 +00001095 gen_clrex(s, insn);
Claudio Fontana87462e02013-12-17 19:42:32 +00001096 return;
1097 case 4: /* DSB */
1098 case 5: /* DMB */
1099 case 6: /* ISB */
1100 /* We don't emulate caches so barriers are no-ops */
1101 return;
1102 default:
1103 unallocated_encoding(s);
1104 return;
1105 }
1106}
1107
1108/* C5.6.130 MSR (immediate) - move immediate to processor state field */
1109static void handle_msr_i(DisasContext *s, uint32_t insn,
1110 unsigned int op1, unsigned int op2, unsigned int crm)
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00001111{
1112 unsupported_encoding(s, insn);
1113}
1114
Peter Maydellb0d2b7d2014-01-04 22:15:45 +00001115static void gen_get_nzcv(TCGv_i64 tcg_rt)
1116{
1117 TCGv_i32 tmp = tcg_temp_new_i32();
1118 TCGv_i32 nzcv = tcg_temp_new_i32();
1119
1120 /* build bit 31, N */
1121 tcg_gen_andi_i32(nzcv, cpu_NF, (1 << 31));
1122 /* build bit 30, Z */
1123 tcg_gen_setcondi_i32(TCG_COND_EQ, tmp, cpu_ZF, 0);
1124 tcg_gen_deposit_i32(nzcv, nzcv, tmp, 30, 1);
1125 /* build bit 29, C */
1126 tcg_gen_deposit_i32(nzcv, nzcv, cpu_CF, 29, 1);
1127 /* build bit 28, V */
1128 tcg_gen_shri_i32(tmp, cpu_VF, 31);
1129 tcg_gen_deposit_i32(nzcv, nzcv, tmp, 28, 1);
1130 /* generate result */
1131 tcg_gen_extu_i32_i64(tcg_rt, nzcv);
1132
1133 tcg_temp_free_i32(nzcv);
1134 tcg_temp_free_i32(tmp);
1135}
1136
1137static void gen_set_nzcv(TCGv_i64 tcg_rt)
1138
1139{
1140 TCGv_i32 nzcv = tcg_temp_new_i32();
1141
1142 /* take NZCV from R[t] */
1143 tcg_gen_trunc_i64_i32(nzcv, tcg_rt);
1144
1145 /* bit 31, N */
1146 tcg_gen_andi_i32(cpu_NF, nzcv, (1 << 31));
1147 /* bit 30, Z */
1148 tcg_gen_andi_i32(cpu_ZF, nzcv, (1 << 30));
1149 tcg_gen_setcondi_i32(TCG_COND_EQ, cpu_ZF, cpu_ZF, 0);
1150 /* bit 29, C */
1151 tcg_gen_andi_i32(cpu_CF, nzcv, (1 << 29));
1152 tcg_gen_shri_i32(cpu_CF, cpu_CF, 29);
1153 /* bit 28, V */
1154 tcg_gen_andi_i32(cpu_VF, nzcv, (1 << 28));
1155 tcg_gen_shli_i32(cpu_VF, cpu_VF, 3);
1156 tcg_temp_free_i32(nzcv);
1157}
1158
Peter Maydellfea50522014-01-04 22:15:45 +00001159/* C5.6.129 MRS - move from system register
1160 * C5.6.131 MSR (register) - move to system register
1161 * C5.6.204 SYS
1162 * C5.6.205 SYSL
1163 * These are all essentially the same insn in 'read' and 'write'
1164 * versions, with varying op0 fields.
1165 */
1166static void handle_sys(DisasContext *s, uint32_t insn, bool isread,
1167 unsigned int op0, unsigned int op1, unsigned int op2,
Claudio Fontana87462e02013-12-17 19:42:32 +00001168 unsigned int crn, unsigned int crm, unsigned int rt)
1169{
Peter Maydellfea50522014-01-04 22:15:45 +00001170 const ARMCPRegInfo *ri;
1171 TCGv_i64 tcg_rt;
Claudio Fontana87462e02013-12-17 19:42:32 +00001172
Peter Maydellfea50522014-01-04 22:15:45 +00001173 ri = get_arm_cp_reginfo(s->cp_regs,
1174 ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP,
1175 crn, crm, op0, op1, op2));
Claudio Fontana87462e02013-12-17 19:42:32 +00001176
Peter Maydellfea50522014-01-04 22:15:45 +00001177 if (!ri) {
1178 /* Unknown register */
1179 unallocated_encoding(s);
1180 return;
1181 }
1182
1183 /* Check access permissions */
1184 if (!cp_access_ok(s->current_pl, ri, isread)) {
1185 unallocated_encoding(s);
1186 return;
1187 }
1188
1189 /* Handle special cases first */
1190 switch (ri->type & ~(ARM_CP_FLAG_MASK & ~ARM_CP_SPECIAL)) {
1191 case ARM_CP_NOP:
1192 return;
Peter Maydellb0d2b7d2014-01-04 22:15:45 +00001193 case ARM_CP_NZCV:
1194 tcg_rt = cpu_reg(s, rt);
1195 if (isread) {
1196 gen_get_nzcv(tcg_rt);
1197 } else {
1198 gen_set_nzcv(tcg_rt);
1199 }
1200 return;
Peter Maydellfea50522014-01-04 22:15:45 +00001201 default:
1202 break;
1203 }
1204
1205 if (use_icount && (ri->type & ARM_CP_IO)) {
1206 gen_io_start();
1207 }
1208
1209 tcg_rt = cpu_reg(s, rt);
1210
1211 if (isread) {
1212 if (ri->type & ARM_CP_CONST) {
1213 tcg_gen_movi_i64(tcg_rt, ri->resetvalue);
1214 } else if (ri->readfn) {
1215 TCGv_ptr tmpptr;
1216 gen_a64_set_pc_im(s->pc - 4);
1217 tmpptr = tcg_const_ptr(ri);
1218 gen_helper_get_cp_reg64(tcg_rt, cpu_env, tmpptr);
1219 tcg_temp_free_ptr(tmpptr);
1220 } else {
1221 tcg_gen_ld_i64(tcg_rt, cpu_env, ri->fieldoffset);
1222 }
1223 } else {
1224 if (ri->type & ARM_CP_CONST) {
1225 /* If not forbidden by access permissions, treat as WI */
1226 return;
1227 } else if (ri->writefn) {
1228 TCGv_ptr tmpptr;
1229 gen_a64_set_pc_im(s->pc - 4);
1230 tmpptr = tcg_const_ptr(ri);
1231 gen_helper_set_cp_reg64(cpu_env, tmpptr, tcg_rt);
1232 tcg_temp_free_ptr(tmpptr);
1233 } else {
1234 tcg_gen_st_i64(tcg_rt, cpu_env, ri->fieldoffset);
1235 }
1236 }
1237
1238 if (use_icount && (ri->type & ARM_CP_IO)) {
1239 /* I/O operations must end the TB here (whether read or write) */
1240 gen_io_end();
1241 s->is_jmp = DISAS_UPDATE;
1242 } else if (!isread && !(ri->type & ARM_CP_SUPPRESS_TB_END)) {
1243 /* We default to ending the TB on a coprocessor register write,
1244 * but allow this to be suppressed by the register definition
1245 * (usually only necessary to work around guest bugs).
1246 */
1247 s->is_jmp = DISAS_UPDATE;
1248 }
Claudio Fontana87462e02013-12-17 19:42:32 +00001249}
1250
1251/* C3.2.4 System
1252 * 31 22 21 20 19 18 16 15 12 11 8 7 5 4 0
1253 * +---------------------+---+-----+-----+-------+-------+-----+------+
1254 * | 1 1 0 1 0 1 0 1 0 0 | L | op0 | op1 | CRn | CRm | op2 | Rt |
1255 * +---------------------+---+-----+-----+-------+-------+-----+------+
1256 */
1257static void disas_system(DisasContext *s, uint32_t insn)
1258{
1259 unsigned int l, op0, op1, crn, crm, op2, rt;
1260 l = extract32(insn, 21, 1);
1261 op0 = extract32(insn, 19, 2);
1262 op1 = extract32(insn, 16, 3);
1263 crn = extract32(insn, 12, 4);
1264 crm = extract32(insn, 8, 4);
1265 op2 = extract32(insn, 5, 3);
1266 rt = extract32(insn, 0, 5);
1267
1268 if (op0 == 0) {
1269 if (l || rt != 31) {
1270 unallocated_encoding(s);
1271 return;
1272 }
1273 switch (crn) {
1274 case 2: /* C5.6.68 HINT */
1275 handle_hint(s, insn, op1, op2, crm);
1276 break;
1277 case 3: /* CLREX, DSB, DMB, ISB */
1278 handle_sync(s, insn, op1, op2, crm);
1279 break;
1280 case 4: /* C5.6.130 MSR (immediate) */
1281 handle_msr_i(s, insn, op1, op2, crm);
1282 break;
1283 default:
1284 unallocated_encoding(s);
1285 break;
1286 }
1287 return;
1288 }
Peter Maydellfea50522014-01-04 22:15:45 +00001289 handle_sys(s, insn, l, op0, op1, op2, crn, crm, rt);
Claudio Fontana87462e02013-12-17 19:42:32 +00001290}
1291
Alexander Graf9618e802013-12-23 23:27:30 +00001292/* C3.2.3 Exception generation
1293 *
1294 * 31 24 23 21 20 5 4 2 1 0
1295 * +-----------------+-----+------------------------+-----+----+
1296 * | 1 1 0 1 0 1 0 0 | opc | imm16 | op2 | LL |
1297 * +-----------------------+------------------------+----------+
1298 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00001299static void disas_exc(DisasContext *s, uint32_t insn)
1300{
Alexander Graf9618e802013-12-23 23:27:30 +00001301 int opc = extract32(insn, 21, 3);
1302 int op2_ll = extract32(insn, 0, 5);
1303
1304 switch (opc) {
1305 case 0:
1306 /* SVC, HVC, SMC; since we don't support the Virtualization
1307 * or TrustZone extensions these all UNDEF except SVC.
1308 */
1309 if (op2_ll != 1) {
1310 unallocated_encoding(s);
1311 break;
1312 }
1313 gen_exception_insn(s, 0, EXCP_SWI);
1314 break;
1315 case 1:
1316 if (op2_ll != 0) {
1317 unallocated_encoding(s);
1318 break;
1319 }
1320 /* BRK */
1321 gen_exception_insn(s, 0, EXCP_BKPT);
1322 break;
1323 case 2:
1324 if (op2_ll != 0) {
1325 unallocated_encoding(s);
1326 break;
1327 }
1328 /* HLT */
1329 unsupported_encoding(s, insn);
1330 break;
1331 case 5:
1332 if (op2_ll < 1 || op2_ll > 3) {
1333 unallocated_encoding(s);
1334 break;
1335 }
1336 /* DCPS1, DCPS2, DCPS3 */
1337 unsupported_encoding(s, insn);
1338 break;
1339 default:
1340 unallocated_encoding(s);
1341 break;
1342 }
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00001343}
1344
Alexander Grafb001c8c2013-12-17 19:42:33 +00001345/* C3.2.7 Unconditional branch (register)
1346 * 31 25 24 21 20 16 15 10 9 5 4 0
1347 * +---------------+-------+-------+-------+------+-------+
1348 * | 1 1 0 1 0 1 1 | opc | op2 | op3 | Rn | op4 |
1349 * +---------------+-------+-------+-------+------+-------+
1350 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00001351static void disas_uncond_b_reg(DisasContext *s, uint32_t insn)
1352{
Alexander Grafb001c8c2013-12-17 19:42:33 +00001353 unsigned int opc, op2, op3, rn, op4;
1354
1355 opc = extract32(insn, 21, 4);
1356 op2 = extract32(insn, 16, 5);
1357 op3 = extract32(insn, 10, 6);
1358 rn = extract32(insn, 5, 5);
1359 op4 = extract32(insn, 0, 5);
1360
1361 if (op4 != 0x0 || op3 != 0x0 || op2 != 0x1f) {
1362 unallocated_encoding(s);
1363 return;
1364 }
1365
1366 switch (opc) {
1367 case 0: /* BR */
1368 case 2: /* RET */
1369 break;
1370 case 1: /* BLR */
1371 tcg_gen_movi_i64(cpu_reg(s, 30), s->pc);
1372 break;
1373 case 4: /* ERET */
1374 case 5: /* DRPS */
1375 if (rn != 0x1f) {
1376 unallocated_encoding(s);
1377 } else {
1378 unsupported_encoding(s, insn);
1379 }
1380 return;
1381 default:
1382 unallocated_encoding(s);
1383 return;
1384 }
1385
1386 tcg_gen_mov_i64(cpu_pc, cpu_reg(s, rn));
1387 s->is_jmp = DISAS_JUMP;
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00001388}
1389
1390/* C3.2 Branches, exception generating and system instructions */
1391static void disas_b_exc_sys(DisasContext *s, uint32_t insn)
1392{
1393 switch (extract32(insn, 25, 7)) {
1394 case 0x0a: case 0x0b:
1395 case 0x4a: case 0x4b: /* Unconditional branch (immediate) */
1396 disas_uncond_b_imm(s, insn);
1397 break;
1398 case 0x1a: case 0x5a: /* Compare & branch (immediate) */
1399 disas_comp_b_imm(s, insn);
1400 break;
1401 case 0x1b: case 0x5b: /* Test & branch (immediate) */
1402 disas_test_b_imm(s, insn);
1403 break;
1404 case 0x2a: /* Conditional branch (immediate) */
1405 disas_cond_b_imm(s, insn);
1406 break;
1407 case 0x6a: /* Exception generation / System */
1408 if (insn & (1 << 24)) {
1409 disas_system(s, insn);
1410 } else {
1411 disas_exc(s, insn);
1412 }
1413 break;
1414 case 0x6b: /* Unconditional branch (register) */
1415 disas_uncond_b_reg(s, insn);
1416 break;
1417 default:
1418 unallocated_encoding(s);
1419 break;
1420 }
1421}
1422
Michael Matzfa2ef212014-01-04 22:15:47 +00001423/*
1424 * Load/Store exclusive instructions are implemented by remembering
1425 * the value/address loaded, and seeing if these are the same
1426 * when the store is performed. This is not actually the architecturally
1427 * mandated semantics, but it works for typical guest code sequences
1428 * and avoids having to monitor regular stores.
1429 *
1430 * In system emulation mode only one CPU will be running at once, so
1431 * this sequence is effectively atomic. In user emulation mode we
1432 * throw an exception and handle the atomic operation elsewhere.
1433 */
1434static void gen_load_exclusive(DisasContext *s, int rt, int rt2,
1435 TCGv_i64 addr, int size, bool is_pair)
1436{
1437 TCGv_i64 tmp = tcg_temp_new_i64();
1438 TCGMemOp memop = MO_TE + size;
1439
1440 g_assert(size <= 3);
1441 tcg_gen_qemu_ld_i64(tmp, addr, get_mem_index(s), memop);
1442
1443 if (is_pair) {
1444 TCGv_i64 addr2 = tcg_temp_new_i64();
1445 TCGv_i64 hitmp = tcg_temp_new_i64();
1446
1447 g_assert(size >= 2);
1448 tcg_gen_addi_i64(addr2, addr, 1 << size);
1449 tcg_gen_qemu_ld_i64(hitmp, addr2, get_mem_index(s), memop);
1450 tcg_temp_free_i64(addr2);
1451 tcg_gen_mov_i64(cpu_exclusive_high, hitmp);
1452 tcg_gen_mov_i64(cpu_reg(s, rt2), hitmp);
1453 tcg_temp_free_i64(hitmp);
1454 }
1455
1456 tcg_gen_mov_i64(cpu_exclusive_val, tmp);
1457 tcg_gen_mov_i64(cpu_reg(s, rt), tmp);
1458
1459 tcg_temp_free_i64(tmp);
1460 tcg_gen_mov_i64(cpu_exclusive_addr, addr);
1461}
1462
1463#ifdef CONFIG_USER_ONLY
1464static void gen_store_exclusive(DisasContext *s, int rd, int rt, int rt2,
1465 TCGv_i64 addr, int size, int is_pair)
1466{
1467 tcg_gen_mov_i64(cpu_exclusive_test, addr);
1468 tcg_gen_movi_i32(cpu_exclusive_info,
1469 size | is_pair << 2 | (rd << 4) | (rt << 9) | (rt2 << 14));
1470 gen_exception_insn(s, 4, EXCP_STREX);
1471}
1472#else
1473static void gen_store_exclusive(DisasContext *s, int rd, int rt, int rt2,
1474 TCGv_i64 addr, int size, int is_pair)
1475{
1476 qemu_log_mask(LOG_UNIMP,
1477 "%s:%d: system mode store_exclusive unsupported "
1478 "at pc=%016" PRIx64 "\n",
1479 __FILE__, __LINE__, s->pc - 4);
1480}
1481#endif
1482
1483/* C3.3.6 Load/store exclusive
1484 *
1485 * 31 30 29 24 23 22 21 20 16 15 14 10 9 5 4 0
1486 * +-----+-------------+----+---+----+------+----+-------+------+------+
1487 * | sz | 0 0 1 0 0 0 | o2 | L | o1 | Rs | o0 | Rt2 | Rn | Rt |
1488 * +-----+-------------+----+---+----+------+----+-------+------+------+
1489 *
1490 * sz: 00 -> 8 bit, 01 -> 16 bit, 10 -> 32 bit, 11 -> 64 bit
1491 * L: 0 -> store, 1 -> load
1492 * o2: 0 -> exclusive, 1 -> not
1493 * o1: 0 -> single register, 1 -> register pair
1494 * o0: 1 -> load-acquire/store-release, 0 -> not
1495 *
1496 * o0 == 0 AND o2 == 1 is un-allocated
1497 * o1 == 1 is un-allocated except for 32 and 64 bit sizes
1498 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00001499static void disas_ldst_excl(DisasContext *s, uint32_t insn)
1500{
Michael Matzfa2ef212014-01-04 22:15:47 +00001501 int rt = extract32(insn, 0, 5);
1502 int rn = extract32(insn, 5, 5);
1503 int rt2 = extract32(insn, 10, 5);
1504 int is_lasr = extract32(insn, 15, 1);
1505 int rs = extract32(insn, 16, 5);
1506 int is_pair = extract32(insn, 21, 1);
1507 int is_store = !extract32(insn, 22, 1);
1508 int is_excl = !extract32(insn, 23, 1);
1509 int size = extract32(insn, 30, 2);
1510 TCGv_i64 tcg_addr;
1511
1512 if ((!is_excl && !is_lasr) ||
1513 (is_pair && size < 2)) {
1514 unallocated_encoding(s);
1515 return;
1516 }
1517
1518 if (rn == 31) {
1519 gen_check_sp_alignment(s);
1520 }
1521 tcg_addr = read_cpu_reg_sp(s, rn, 1);
1522
1523 /* Note that since TCG is single threaded load-acquire/store-release
1524 * semantics require no extra if (is_lasr) { ... } handling.
1525 */
1526
1527 if (is_excl) {
1528 if (!is_store) {
1529 gen_load_exclusive(s, rt, rt2, tcg_addr, size, is_pair);
1530 } else {
1531 gen_store_exclusive(s, rs, rt, rt2, tcg_addr, size, is_pair);
1532 }
1533 } else {
1534 TCGv_i64 tcg_rt = cpu_reg(s, rt);
1535 if (is_store) {
1536 do_gpr_st(s, tcg_rt, tcg_addr, size);
1537 } else {
1538 do_gpr_ld(s, tcg_rt, tcg_addr, size, false, false);
1539 }
1540 if (is_pair) {
1541 TCGv_i64 tcg_rt2 = cpu_reg(s, rt);
1542 tcg_gen_addi_i64(tcg_addr, tcg_addr, 1 << size);
1543 if (is_store) {
1544 do_gpr_st(s, tcg_rt2, tcg_addr, size);
1545 } else {
1546 do_gpr_ld(s, tcg_rt2, tcg_addr, size, false, false);
1547 }
1548 }
1549 }
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00001550}
1551
Alexander Graf32b64e82014-01-04 22:15:46 +00001552/*
1553 * C3.3.5 Load register (literal)
1554 *
1555 * 31 30 29 27 26 25 24 23 5 4 0
1556 * +-----+-------+---+-----+-------------------+-------+
1557 * | opc | 0 1 1 | V | 0 0 | imm19 | Rt |
1558 * +-----+-------+---+-----+-------------------+-------+
1559 *
1560 * V: 1 -> vector (simd/fp)
1561 * opc (non-vector): 00 -> 32 bit, 01 -> 64 bit,
1562 * 10-> 32 bit signed, 11 -> prefetch
1563 * opc (vector): 00 -> 32 bit, 01 -> 64 bit, 10 -> 128 bit (11 unallocated)
1564 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00001565static void disas_ld_lit(DisasContext *s, uint32_t insn)
1566{
Alexander Graf32b64e82014-01-04 22:15:46 +00001567 int rt = extract32(insn, 0, 5);
1568 int64_t imm = sextract32(insn, 5, 19) << 2;
1569 bool is_vector = extract32(insn, 26, 1);
1570 int opc = extract32(insn, 30, 2);
1571 bool is_signed = false;
1572 int size = 2;
1573 TCGv_i64 tcg_rt, tcg_addr;
1574
1575 if (is_vector) {
1576 if (opc == 3) {
1577 unallocated_encoding(s);
1578 return;
1579 }
1580 size = 2 + opc;
1581 } else {
1582 if (opc == 3) {
1583 /* PRFM (literal) : prefetch */
1584 return;
1585 }
1586 size = 2 + extract32(opc, 0, 1);
1587 is_signed = extract32(opc, 1, 1);
1588 }
1589
1590 tcg_rt = cpu_reg(s, rt);
1591
1592 tcg_addr = tcg_const_i64((s->pc - 4) + imm);
1593 if (is_vector) {
1594 do_fp_ld(s, rt, tcg_addr, size);
1595 } else {
1596 do_gpr_ld(s, tcg_rt, tcg_addr, size, is_signed, false);
1597 }
1598 tcg_temp_free_i64(tcg_addr);
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00001599}
1600
Peter Maydell4a08d472013-12-22 22:32:27 +00001601/*
1602 * C5.6.80 LDNP (Load Pair - non-temporal hint)
1603 * C5.6.81 LDP (Load Pair - non vector)
1604 * C5.6.82 LDPSW (Load Pair Signed Word - non vector)
1605 * C5.6.176 STNP (Store Pair - non-temporal hint)
1606 * C5.6.177 STP (Store Pair - non vector)
1607 * C6.3.165 LDNP (Load Pair of SIMD&FP - non-temporal hint)
1608 * C6.3.165 LDP (Load Pair of SIMD&FP)
1609 * C6.3.284 STNP (Store Pair of SIMD&FP - non-temporal hint)
1610 * C6.3.284 STP (Store Pair of SIMD&FP)
1611 *
1612 * 31 30 29 27 26 25 24 23 22 21 15 14 10 9 5 4 0
1613 * +-----+-------+---+---+-------+---+-----------------------------+
1614 * | opc | 1 0 1 | V | 0 | index | L | imm7 | Rt2 | Rn | Rt |
1615 * +-----+-------+---+---+-------+---+-------+-------+------+------+
1616 *
1617 * opc: LDP/STP/LDNP/STNP 00 -> 32 bit, 10 -> 64 bit
1618 * LDPSW 01
1619 * LDP/STP/LDNP/STNP (SIMD) 00 -> 32 bit, 01 -> 64 bit, 10 -> 128 bit
1620 * V: 0 -> GPR, 1 -> Vector
1621 * idx: 00 -> signed offset with non-temporal hint, 01 -> post-index,
1622 * 10 -> signed offset, 11 -> pre-index
1623 * L: 0 -> Store 1 -> Load
1624 *
1625 * Rt, Rt2 = GPR or SIMD registers to be stored
1626 * Rn = general purpose register containing address
1627 * imm7 = signed offset (multiple of 4 or 8 depending on size)
1628 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00001629static void disas_ldst_pair(DisasContext *s, uint32_t insn)
1630{
Peter Maydell4a08d472013-12-22 22:32:27 +00001631 int rt = extract32(insn, 0, 5);
1632 int rn = extract32(insn, 5, 5);
1633 int rt2 = extract32(insn, 10, 5);
1634 int64_t offset = sextract32(insn, 15, 7);
1635 int index = extract32(insn, 23, 2);
1636 bool is_vector = extract32(insn, 26, 1);
1637 bool is_load = extract32(insn, 22, 1);
1638 int opc = extract32(insn, 30, 2);
1639
1640 bool is_signed = false;
1641 bool postindex = false;
1642 bool wback = false;
1643
1644 TCGv_i64 tcg_addr; /* calculated address */
1645 int size;
1646
1647 if (opc == 3) {
1648 unallocated_encoding(s);
1649 return;
1650 }
1651
1652 if (is_vector) {
1653 size = 2 + opc;
1654 } else {
1655 size = 2 + extract32(opc, 1, 1);
1656 is_signed = extract32(opc, 0, 1);
1657 if (!is_load && is_signed) {
1658 unallocated_encoding(s);
1659 return;
1660 }
1661 }
1662
1663 switch (index) {
1664 case 1: /* post-index */
1665 postindex = true;
1666 wback = true;
1667 break;
1668 case 0:
1669 /* signed offset with "non-temporal" hint. Since we don't emulate
1670 * caches we don't care about hints to the cache system about
1671 * data access patterns, and handle this identically to plain
1672 * signed offset.
1673 */
1674 if (is_signed) {
1675 /* There is no non-temporal-hint version of LDPSW */
1676 unallocated_encoding(s);
1677 return;
1678 }
1679 postindex = false;
1680 break;
1681 case 2: /* signed offset, rn not updated */
1682 postindex = false;
1683 break;
1684 case 3: /* pre-index */
1685 postindex = false;
1686 wback = true;
1687 break;
1688 }
1689
1690 offset <<= size;
1691
1692 if (rn == 31) {
1693 gen_check_sp_alignment(s);
1694 }
1695
1696 tcg_addr = read_cpu_reg_sp(s, rn, 1);
1697
1698 if (!postindex) {
1699 tcg_gen_addi_i64(tcg_addr, tcg_addr, offset);
1700 }
1701
1702 if (is_vector) {
1703 if (is_load) {
1704 do_fp_ld(s, rt, tcg_addr, size);
1705 } else {
1706 do_fp_st(s, rt, tcg_addr, size);
1707 }
1708 } else {
1709 TCGv_i64 tcg_rt = cpu_reg(s, rt);
1710 if (is_load) {
1711 do_gpr_ld(s, tcg_rt, tcg_addr, size, is_signed, false);
1712 } else {
1713 do_gpr_st(s, tcg_rt, tcg_addr, size);
1714 }
1715 }
1716 tcg_gen_addi_i64(tcg_addr, tcg_addr, 1 << size);
1717 if (is_vector) {
1718 if (is_load) {
1719 do_fp_ld(s, rt2, tcg_addr, size);
1720 } else {
1721 do_fp_st(s, rt2, tcg_addr, size);
1722 }
1723 } else {
1724 TCGv_i64 tcg_rt2 = cpu_reg(s, rt2);
1725 if (is_load) {
1726 do_gpr_ld(s, tcg_rt2, tcg_addr, size, is_signed, false);
1727 } else {
1728 do_gpr_st(s, tcg_rt2, tcg_addr, size);
1729 }
1730 }
1731
1732 if (wback) {
1733 if (postindex) {
1734 tcg_gen_addi_i64(tcg_addr, tcg_addr, offset - (1 << size));
1735 } else {
1736 tcg_gen_subi_i64(tcg_addr, tcg_addr, 1 << size);
1737 }
1738 tcg_gen_mov_i64(cpu_reg_sp(s, rn), tcg_addr);
1739 }
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00001740}
1741
Alex Bennéed5612f12013-12-23 23:27:28 +00001742/*
Alex Bennéea5e94a92013-12-23 23:27:29 +00001743 * C3.3.8 Load/store (immediate post-indexed)
1744 * C3.3.9 Load/store (immediate pre-indexed)
1745 * C3.3.12 Load/store (unscaled immediate)
1746 *
1747 * 31 30 29 27 26 25 24 23 22 21 20 12 11 10 9 5 4 0
1748 * +----+-------+---+-----+-----+---+--------+-----+------+------+
1749 * |size| 1 1 1 | V | 0 0 | opc | 0 | imm9 | idx | Rn | Rt |
1750 * +----+-------+---+-----+-----+---+--------+-----+------+------+
1751 *
1752 * idx = 01 -> post-indexed, 11 pre-indexed, 00 unscaled imm. (no writeback)
1753 * V = 0 -> non-vector
1754 * size: 00 -> 8 bit, 01 -> 16 bit, 10 -> 32 bit, 11 -> 64bit
1755 * opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
1756 */
1757static void disas_ldst_reg_imm9(DisasContext *s, uint32_t insn)
1758{
1759 int rt = extract32(insn, 0, 5);
1760 int rn = extract32(insn, 5, 5);
1761 int imm9 = sextract32(insn, 12, 9);
1762 int opc = extract32(insn, 22, 2);
1763 int size = extract32(insn, 30, 2);
1764 int idx = extract32(insn, 10, 2);
1765 bool is_signed = false;
1766 bool is_store = false;
1767 bool is_extended = false;
1768 bool is_vector = extract32(insn, 26, 1);
1769 bool post_index;
1770 bool writeback;
1771
1772 TCGv_i64 tcg_addr;
1773
1774 if (is_vector) {
1775 size |= (opc & 2) << 1;
1776 if (size > 4) {
1777 unallocated_encoding(s);
1778 return;
1779 }
1780 is_store = ((opc & 1) == 0);
1781 } else {
1782 if (size == 3 && opc == 2) {
1783 /* PRFM - prefetch */
1784 return;
1785 }
1786 if (opc == 3 && size > 1) {
1787 unallocated_encoding(s);
1788 return;
1789 }
1790 is_store = (opc == 0);
1791 is_signed = opc & (1<<1);
1792 is_extended = (size < 3) && (opc & 1);
1793 }
1794
1795 switch (idx) {
1796 case 0:
1797 post_index = false;
1798 writeback = false;
1799 break;
1800 case 1:
1801 post_index = true;
1802 writeback = true;
1803 break;
1804 case 3:
1805 post_index = false;
1806 writeback = true;
1807 break;
1808 case 2:
1809 g_assert(false);
1810 break;
1811 }
1812
1813 if (rn == 31) {
1814 gen_check_sp_alignment(s);
1815 }
1816 tcg_addr = read_cpu_reg_sp(s, rn, 1);
1817
1818 if (!post_index) {
1819 tcg_gen_addi_i64(tcg_addr, tcg_addr, imm9);
1820 }
1821
1822 if (is_vector) {
1823 if (is_store) {
1824 do_fp_st(s, rt, tcg_addr, size);
1825 } else {
1826 do_fp_ld(s, rt, tcg_addr, size);
1827 }
1828 } else {
1829 TCGv_i64 tcg_rt = cpu_reg(s, rt);
1830 if (is_store) {
1831 do_gpr_st(s, tcg_rt, tcg_addr, size);
1832 } else {
1833 do_gpr_ld(s, tcg_rt, tcg_addr, size, is_signed, is_extended);
1834 }
1835 }
1836
1837 if (writeback) {
1838 TCGv_i64 tcg_rn = cpu_reg_sp(s, rn);
1839 if (post_index) {
1840 tcg_gen_addi_i64(tcg_addr, tcg_addr, imm9);
1841 }
1842 tcg_gen_mov_i64(tcg_rn, tcg_addr);
1843 }
1844}
1845
1846/*
Alex Bennée229b7a02013-12-23 23:27:29 +00001847 * C3.3.10 Load/store (register offset)
1848 *
1849 * 31 30 29 27 26 25 24 23 22 21 20 16 15 13 12 11 10 9 5 4 0
1850 * +----+-------+---+-----+-----+---+------+-----+--+-----+----+----+
1851 * |size| 1 1 1 | V | 0 0 | opc | 1 | Rm | opt | S| 1 0 | Rn | Rt |
1852 * +----+-------+---+-----+-----+---+------+-----+--+-----+----+----+
1853 *
1854 * For non-vector:
1855 * size: 00-> byte, 01 -> 16 bit, 10 -> 32bit, 11 -> 64bit
1856 * opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
1857 * For vector:
1858 * size is opc<1>:size<1:0> so 100 -> 128 bit; 110 and 111 unallocated
1859 * opc<0>: 0 -> store, 1 -> load
1860 * V: 1 -> vector/simd
1861 * opt: extend encoding (see DecodeRegExtend)
1862 * S: if S=1 then scale (essentially index by sizeof(size))
1863 * Rt: register to transfer into/out of
1864 * Rn: address register or SP for base
1865 * Rm: offset register or ZR for offset
1866 */
1867static void disas_ldst_reg_roffset(DisasContext *s, uint32_t insn)
1868{
1869 int rt = extract32(insn, 0, 5);
1870 int rn = extract32(insn, 5, 5);
1871 int shift = extract32(insn, 12, 1);
1872 int rm = extract32(insn, 16, 5);
1873 int opc = extract32(insn, 22, 2);
1874 int opt = extract32(insn, 13, 3);
1875 int size = extract32(insn, 30, 2);
1876 bool is_signed = false;
1877 bool is_store = false;
1878 bool is_extended = false;
1879 bool is_vector = extract32(insn, 26, 1);
1880
1881 TCGv_i64 tcg_rm;
1882 TCGv_i64 tcg_addr;
1883
1884 if (extract32(opt, 1, 1) == 0) {
1885 unallocated_encoding(s);
1886 return;
1887 }
1888
1889 if (is_vector) {
1890 size |= (opc & 2) << 1;
1891 if (size > 4) {
1892 unallocated_encoding(s);
1893 return;
1894 }
1895 is_store = !extract32(opc, 0, 1);
1896 } else {
1897 if (size == 3 && opc == 2) {
1898 /* PRFM - prefetch */
1899 return;
1900 }
1901 if (opc == 3 && size > 1) {
1902 unallocated_encoding(s);
1903 return;
1904 }
1905 is_store = (opc == 0);
1906 is_signed = extract32(opc, 1, 1);
1907 is_extended = (size < 3) && extract32(opc, 0, 1);
1908 }
1909
1910 if (rn == 31) {
1911 gen_check_sp_alignment(s);
1912 }
1913 tcg_addr = read_cpu_reg_sp(s, rn, 1);
1914
1915 tcg_rm = read_cpu_reg(s, rm, 1);
1916 ext_and_shift_reg(tcg_rm, tcg_rm, opt, shift ? size : 0);
1917
1918 tcg_gen_add_i64(tcg_addr, tcg_addr, tcg_rm);
1919
1920 if (is_vector) {
1921 if (is_store) {
1922 do_fp_st(s, rt, tcg_addr, size);
1923 } else {
1924 do_fp_ld(s, rt, tcg_addr, size);
1925 }
1926 } else {
1927 TCGv_i64 tcg_rt = cpu_reg(s, rt);
1928 if (is_store) {
1929 do_gpr_st(s, tcg_rt, tcg_addr, size);
1930 } else {
1931 do_gpr_ld(s, tcg_rt, tcg_addr, size, is_signed, is_extended);
1932 }
1933 }
1934}
1935
1936/*
Alex Bennéed5612f12013-12-23 23:27:28 +00001937 * C3.3.13 Load/store (unsigned immediate)
1938 *
1939 * 31 30 29 27 26 25 24 23 22 21 10 9 5
1940 * +----+-------+---+-----+-----+------------+-------+------+
1941 * |size| 1 1 1 | V | 0 1 | opc | imm12 | Rn | Rt |
1942 * +----+-------+---+-----+-----+------------+-------+------+
1943 *
1944 * For non-vector:
1945 * size: 00-> byte, 01 -> 16 bit, 10 -> 32bit, 11 -> 64bit
1946 * opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
1947 * For vector:
1948 * size is opc<1>:size<1:0> so 100 -> 128 bit; 110 and 111 unallocated
1949 * opc<0>: 0 -> store, 1 -> load
1950 * Rn: base address register (inc SP)
1951 * Rt: target register
1952 */
1953static void disas_ldst_reg_unsigned_imm(DisasContext *s, uint32_t insn)
1954{
1955 int rt = extract32(insn, 0, 5);
1956 int rn = extract32(insn, 5, 5);
1957 unsigned int imm12 = extract32(insn, 10, 12);
1958 bool is_vector = extract32(insn, 26, 1);
1959 int size = extract32(insn, 30, 2);
1960 int opc = extract32(insn, 22, 2);
1961 unsigned int offset;
1962
1963 TCGv_i64 tcg_addr;
1964
1965 bool is_store;
1966 bool is_signed = false;
1967 bool is_extended = false;
1968
1969 if (is_vector) {
1970 size |= (opc & 2) << 1;
1971 if (size > 4) {
1972 unallocated_encoding(s);
1973 return;
1974 }
1975 is_store = !extract32(opc, 0, 1);
1976 } else {
1977 if (size == 3 && opc == 2) {
1978 /* PRFM - prefetch */
1979 return;
1980 }
1981 if (opc == 3 && size > 1) {
1982 unallocated_encoding(s);
1983 return;
1984 }
1985 is_store = (opc == 0);
1986 is_signed = extract32(opc, 1, 1);
1987 is_extended = (size < 3) && extract32(opc, 0, 1);
1988 }
1989
1990 if (rn == 31) {
1991 gen_check_sp_alignment(s);
1992 }
1993 tcg_addr = read_cpu_reg_sp(s, rn, 1);
1994 offset = imm12 << size;
1995 tcg_gen_addi_i64(tcg_addr, tcg_addr, offset);
1996
1997 if (is_vector) {
1998 if (is_store) {
1999 do_fp_st(s, rt, tcg_addr, size);
2000 } else {
2001 do_fp_ld(s, rt, tcg_addr, size);
2002 }
2003 } else {
2004 TCGv_i64 tcg_rt = cpu_reg(s, rt);
2005 if (is_store) {
2006 do_gpr_st(s, tcg_rt, tcg_addr, size);
2007 } else {
2008 do_gpr_ld(s, tcg_rt, tcg_addr, size, is_signed, is_extended);
2009 }
2010 }
2011}
2012
Alex Bennéea5e94a92013-12-23 23:27:29 +00002013/* Load/store register (immediate forms) */
2014static void disas_ldst_reg_imm(DisasContext *s, uint32_t insn)
2015{
2016 switch (extract32(insn, 10, 2)) {
2017 case 0: case 1: case 3:
2018 /* Load/store register (unscaled immediate) */
2019 /* Load/store immediate pre/post-indexed */
2020 disas_ldst_reg_imm9(s, insn);
2021 break;
2022 case 2:
2023 /* Load/store register unprivileged */
2024 unsupported_encoding(s, insn);
2025 break;
2026 default:
2027 unallocated_encoding(s);
2028 break;
2029 }
2030}
2031
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00002032/* Load/store register (all forms) */
2033static void disas_ldst_reg(DisasContext *s, uint32_t insn)
2034{
Alex Bennéed5612f12013-12-23 23:27:28 +00002035 switch (extract32(insn, 24, 2)) {
2036 case 0:
Alex Bennée229b7a02013-12-23 23:27:29 +00002037 if (extract32(insn, 21, 1) == 1 && extract32(insn, 10, 2) == 2) {
2038 disas_ldst_reg_roffset(s, insn);
2039 } else {
Alex Bennéea5e94a92013-12-23 23:27:29 +00002040 disas_ldst_reg_imm(s, insn);
Alex Bennée229b7a02013-12-23 23:27:29 +00002041 }
Alex Bennéed5612f12013-12-23 23:27:28 +00002042 break;
2043 case 1:
2044 disas_ldst_reg_unsigned_imm(s, insn);
2045 break;
2046 default:
2047 unallocated_encoding(s);
2048 break;
2049 }
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00002050}
2051
Alex Bennée539ed652014-01-23 14:37:07 +00002052/* C3.3.1 AdvSIMD load/store multiple structures
2053 *
2054 * 31 30 29 23 22 21 16 15 12 11 10 9 5 4 0
2055 * +---+---+---------------+---+-------------+--------+------+------+------+
2056 * | 0 | Q | 0 0 1 1 0 0 0 | L | 0 0 0 0 0 0 | opcode | size | Rn | Rt |
2057 * +---+---+---------------+---+-------------+--------+------+------+------+
2058 *
2059 * C3.3.2 AdvSIMD load/store multiple structures (post-indexed)
2060 *
2061 * 31 30 29 23 22 21 20 16 15 12 11 10 9 5 4 0
2062 * +---+---+---------------+---+---+---------+--------+------+------+------+
2063 * | 0 | Q | 0 0 1 1 0 0 1 | L | 0 | Rm | opcode | size | Rn | Rt |
2064 * +---+---+---------------+---+---+---------+--------+------+------+------+
2065 *
2066 * Rt: first (or only) SIMD&FP register to be transferred
2067 * Rn: base address or SP
2068 * Rm (post-index only): post-index register (when !31) or size dependent #imm
2069 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00002070static void disas_ldst_multiple_struct(DisasContext *s, uint32_t insn)
2071{
Alex Bennée539ed652014-01-23 14:37:07 +00002072 int rt = extract32(insn, 0, 5);
2073 int rn = extract32(insn, 5, 5);
2074 int size = extract32(insn, 10, 2);
2075 int opcode = extract32(insn, 12, 4);
2076 bool is_store = !extract32(insn, 22, 1);
2077 bool is_postidx = extract32(insn, 23, 1);
2078 bool is_q = extract32(insn, 30, 1);
2079 TCGv_i64 tcg_addr, tcg_rn;
2080
2081 int ebytes = 1 << size;
2082 int elements = (is_q ? 128 : 64) / (8 << size);
2083 int rpt; /* num iterations */
2084 int selem; /* structure elements */
2085 int r;
2086
2087 if (extract32(insn, 31, 1) || extract32(insn, 21, 1)) {
2088 unallocated_encoding(s);
2089 return;
2090 }
2091
2092 /* From the shared decode logic */
2093 switch (opcode) {
2094 case 0x0:
2095 rpt = 1;
2096 selem = 4;
2097 break;
2098 case 0x2:
2099 rpt = 4;
2100 selem = 1;
2101 break;
2102 case 0x4:
2103 rpt = 1;
2104 selem = 3;
2105 break;
2106 case 0x6:
2107 rpt = 3;
2108 selem = 1;
2109 break;
2110 case 0x7:
2111 rpt = 1;
2112 selem = 1;
2113 break;
2114 case 0x8:
2115 rpt = 1;
2116 selem = 2;
2117 break;
2118 case 0xa:
2119 rpt = 2;
2120 selem = 1;
2121 break;
2122 default:
2123 unallocated_encoding(s);
2124 return;
2125 }
2126
2127 if (size == 3 && !is_q && selem != 1) {
2128 /* reserved */
2129 unallocated_encoding(s);
2130 return;
2131 }
2132
2133 if (rn == 31) {
2134 gen_check_sp_alignment(s);
2135 }
2136
2137 tcg_rn = cpu_reg_sp(s, rn);
2138 tcg_addr = tcg_temp_new_i64();
2139 tcg_gen_mov_i64(tcg_addr, tcg_rn);
2140
2141 for (r = 0; r < rpt; r++) {
2142 int e;
2143 for (e = 0; e < elements; e++) {
2144 int tt = (rt + r) % 32;
2145 int xs;
2146 for (xs = 0; xs < selem; xs++) {
2147 if (is_store) {
2148 do_vec_st(s, tt, e, tcg_addr, size);
2149 } else {
2150 do_vec_ld(s, tt, e, tcg_addr, size);
2151
2152 /* For non-quad operations, setting a slice of the low
2153 * 64 bits of the register clears the high 64 bits (in
2154 * the ARM ARM pseudocode this is implicit in the fact
2155 * that 'rval' is a 64 bit wide variable). We optimize
2156 * by noticing that we only need to do this the first
2157 * time we touch a register.
2158 */
2159 if (!is_q && e == 0 && (r == 0 || xs == selem - 1)) {
2160 clear_vec_high(s, tt);
2161 }
2162 }
2163 tcg_gen_addi_i64(tcg_addr, tcg_addr, ebytes);
2164 tt = (tt + 1) % 32;
2165 }
2166 }
2167 }
2168
2169 if (is_postidx) {
2170 int rm = extract32(insn, 16, 5);
2171 if (rm == 31) {
2172 tcg_gen_mov_i64(tcg_rn, tcg_addr);
2173 } else {
2174 tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, rm));
2175 }
2176 }
2177 tcg_temp_free_i64(tcg_addr);
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00002178}
2179
Peter Maydell561f5582014-01-23 14:37:07 +00002180/* C3.3.3 AdvSIMD load/store single structure
2181 *
2182 * 31 30 29 23 22 21 20 16 15 13 12 11 10 9 5 4 0
2183 * +---+---+---------------+-----+-----------+-----+---+------+------+------+
2184 * | 0 | Q | 0 0 1 1 0 1 0 | L R | 0 0 0 0 0 | opc | S | size | Rn | Rt |
2185 * +---+---+---------------+-----+-----------+-----+---+------+------+------+
2186 *
2187 * C3.3.4 AdvSIMD load/store single structure (post-indexed)
2188 *
2189 * 31 30 29 23 22 21 20 16 15 13 12 11 10 9 5 4 0
2190 * +---+---+---------------+-----+-----------+-----+---+------+------+------+
2191 * | 0 | Q | 0 0 1 1 0 1 1 | L R | Rm | opc | S | size | Rn | Rt |
2192 * +---+---+---------------+-----+-----------+-----+---+------+------+------+
2193 *
2194 * Rt: first (or only) SIMD&FP register to be transferred
2195 * Rn: base address or SP
2196 * Rm (post-index only): post-index register (when !31) or size dependent #imm
2197 * index = encoded in Q:S:size dependent on size
2198 *
2199 * lane_size = encoded in R, opc
2200 * transfer width = encoded in opc, S, size
2201 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00002202static void disas_ldst_single_struct(DisasContext *s, uint32_t insn)
2203{
Peter Maydell561f5582014-01-23 14:37:07 +00002204 int rt = extract32(insn, 0, 5);
2205 int rn = extract32(insn, 5, 5);
2206 int size = extract32(insn, 10, 2);
2207 int S = extract32(insn, 12, 1);
2208 int opc = extract32(insn, 13, 3);
2209 int R = extract32(insn, 21, 1);
2210 int is_load = extract32(insn, 22, 1);
2211 int is_postidx = extract32(insn, 23, 1);
2212 int is_q = extract32(insn, 30, 1);
2213
2214 int scale = extract32(opc, 1, 2);
2215 int selem = (extract32(opc, 0, 1) << 1 | R) + 1;
2216 bool replicate = false;
2217 int index = is_q << 3 | S << 2 | size;
2218 int ebytes, xs;
2219 TCGv_i64 tcg_addr, tcg_rn;
2220
2221 switch (scale) {
2222 case 3:
2223 if (!is_load || S) {
2224 unallocated_encoding(s);
2225 return;
2226 }
2227 scale = size;
2228 replicate = true;
2229 break;
2230 case 0:
2231 break;
2232 case 1:
2233 if (extract32(size, 0, 1)) {
2234 unallocated_encoding(s);
2235 return;
2236 }
2237 index >>= 1;
2238 break;
2239 case 2:
2240 if (extract32(size, 1, 1)) {
2241 unallocated_encoding(s);
2242 return;
2243 }
2244 if (!extract32(size, 0, 1)) {
2245 index >>= 2;
2246 } else {
2247 if (S) {
2248 unallocated_encoding(s);
2249 return;
2250 }
2251 index >>= 3;
2252 scale = 3;
2253 }
2254 break;
2255 default:
2256 g_assert_not_reached();
2257 }
2258
2259 ebytes = 1 << scale;
2260
2261 if (rn == 31) {
2262 gen_check_sp_alignment(s);
2263 }
2264
2265 tcg_rn = cpu_reg_sp(s, rn);
2266 tcg_addr = tcg_temp_new_i64();
2267 tcg_gen_mov_i64(tcg_addr, tcg_rn);
2268
2269 for (xs = 0; xs < selem; xs++) {
2270 if (replicate) {
2271 /* Load and replicate to all elements */
2272 uint64_t mulconst;
2273 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
2274
2275 tcg_gen_qemu_ld_i64(tcg_tmp, tcg_addr,
2276 get_mem_index(s), MO_TE + scale);
2277 switch (scale) {
2278 case 0:
2279 mulconst = 0x0101010101010101ULL;
2280 break;
2281 case 1:
2282 mulconst = 0x0001000100010001ULL;
2283 break;
2284 case 2:
2285 mulconst = 0x0000000100000001ULL;
2286 break;
2287 case 3:
2288 mulconst = 0;
2289 break;
2290 default:
2291 g_assert_not_reached();
2292 }
2293 if (mulconst) {
2294 tcg_gen_muli_i64(tcg_tmp, tcg_tmp, mulconst);
2295 }
2296 write_vec_element(s, tcg_tmp, rt, 0, MO_64);
2297 if (is_q) {
2298 write_vec_element(s, tcg_tmp, rt, 1, MO_64);
2299 } else {
2300 clear_vec_high(s, rt);
2301 }
2302 tcg_temp_free_i64(tcg_tmp);
2303 } else {
2304 /* Load/store one element per register */
2305 if (is_load) {
2306 do_vec_ld(s, rt, index, tcg_addr, MO_TE + scale);
2307 } else {
2308 do_vec_st(s, rt, index, tcg_addr, MO_TE + scale);
2309 }
2310 }
2311 tcg_gen_addi_i64(tcg_addr, tcg_addr, ebytes);
2312 rt = (rt + 1) % 32;
2313 }
2314
2315 if (is_postidx) {
2316 int rm = extract32(insn, 16, 5);
2317 if (rm == 31) {
2318 tcg_gen_mov_i64(tcg_rn, tcg_addr);
2319 } else {
2320 tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, rm));
2321 }
2322 }
2323 tcg_temp_free_i64(tcg_addr);
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00002324}
2325
2326/* C3.3 Loads and stores */
2327static void disas_ldst(DisasContext *s, uint32_t insn)
2328{
2329 switch (extract32(insn, 24, 6)) {
2330 case 0x08: /* Load/store exclusive */
2331 disas_ldst_excl(s, insn);
2332 break;
2333 case 0x18: case 0x1c: /* Load register (literal) */
2334 disas_ld_lit(s, insn);
2335 break;
2336 case 0x28: case 0x29:
2337 case 0x2c: case 0x2d: /* Load/store pair (all forms) */
2338 disas_ldst_pair(s, insn);
2339 break;
2340 case 0x38: case 0x39:
2341 case 0x3c: case 0x3d: /* Load/store register (all forms) */
2342 disas_ldst_reg(s, insn);
2343 break;
2344 case 0x0c: /* AdvSIMD load/store multiple structures */
2345 disas_ldst_multiple_struct(s, insn);
2346 break;
2347 case 0x0d: /* AdvSIMD load/store single structure */
2348 disas_ldst_single_struct(s, insn);
2349 break;
2350 default:
2351 unallocated_encoding(s);
2352 break;
2353 }
2354}
2355
Alexander Graf15bfe8b2013-12-17 19:42:34 +00002356/* C3.4.6 PC-rel. addressing
2357 * 31 30 29 28 24 23 5 4 0
2358 * +----+-------+-----------+-------------------+------+
2359 * | op | immlo | 1 0 0 0 0 | immhi | Rd |
2360 * +----+-------+-----------+-------------------+------+
2361 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00002362static void disas_pc_rel_adr(DisasContext *s, uint32_t insn)
2363{
Alexander Graf15bfe8b2013-12-17 19:42:34 +00002364 unsigned int page, rd;
2365 uint64_t base;
2366 int64_t offset;
2367
2368 page = extract32(insn, 31, 1);
2369 /* SignExtend(immhi:immlo) -> offset */
2370 offset = ((int64_t)sextract32(insn, 5, 19) << 2) | extract32(insn, 29, 2);
2371 rd = extract32(insn, 0, 5);
2372 base = s->pc - 4;
2373
2374 if (page) {
2375 /* ADRP (page based) */
2376 base &= ~0xfff;
2377 offset <<= 12;
2378 }
2379
2380 tcg_gen_movi_i64(cpu_reg(s, rd), base + offset);
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00002381}
2382
Alex Bennéeb0ff21b2013-12-23 23:27:29 +00002383/*
2384 * C3.4.1 Add/subtract (immediate)
2385 *
2386 * 31 30 29 28 24 23 22 21 10 9 5 4 0
2387 * +--+--+--+-----------+-----+-------------+-----+-----+
2388 * |sf|op| S| 1 0 0 0 1 |shift| imm12 | Rn | Rd |
2389 * +--+--+--+-----------+-----+-------------+-----+-----+
2390 *
2391 * sf: 0 -> 32bit, 1 -> 64bit
2392 * op: 0 -> add , 1 -> sub
2393 * S: 1 -> set flags
2394 * shift: 00 -> LSL imm by 0, 01 -> LSL imm by 12
2395 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00002396static void disas_add_sub_imm(DisasContext *s, uint32_t insn)
2397{
Alex Bennéeb0ff21b2013-12-23 23:27:29 +00002398 int rd = extract32(insn, 0, 5);
2399 int rn = extract32(insn, 5, 5);
2400 uint64_t imm = extract32(insn, 10, 12);
2401 int shift = extract32(insn, 22, 2);
2402 bool setflags = extract32(insn, 29, 1);
2403 bool sub_op = extract32(insn, 30, 1);
2404 bool is_64bit = extract32(insn, 31, 1);
2405
2406 TCGv_i64 tcg_rn = cpu_reg_sp(s, rn);
2407 TCGv_i64 tcg_rd = setflags ? cpu_reg(s, rd) : cpu_reg_sp(s, rd);
2408 TCGv_i64 tcg_result;
2409
2410 switch (shift) {
2411 case 0x0:
2412 break;
2413 case 0x1:
2414 imm <<= 12;
2415 break;
2416 default:
2417 unallocated_encoding(s);
2418 return;
2419 }
2420
2421 tcg_result = tcg_temp_new_i64();
2422 if (!setflags) {
2423 if (sub_op) {
2424 tcg_gen_subi_i64(tcg_result, tcg_rn, imm);
2425 } else {
2426 tcg_gen_addi_i64(tcg_result, tcg_rn, imm);
2427 }
2428 } else {
2429 TCGv_i64 tcg_imm = tcg_const_i64(imm);
2430 if (sub_op) {
2431 gen_sub_CC(is_64bit, tcg_result, tcg_rn, tcg_imm);
2432 } else {
2433 gen_add_CC(is_64bit, tcg_result, tcg_rn, tcg_imm);
2434 }
2435 tcg_temp_free_i64(tcg_imm);
2436 }
2437
2438 if (is_64bit) {
2439 tcg_gen_mov_i64(tcg_rd, tcg_result);
2440 } else {
2441 tcg_gen_ext32u_i64(tcg_rd, tcg_result);
2442 }
2443
2444 tcg_temp_free_i64(tcg_result);
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00002445}
2446
Alexander Graf71b46082013-12-17 19:42:36 +00002447/* The input should be a value in the bottom e bits (with higher
2448 * bits zero); returns that value replicated into every element
2449 * of size e in a 64 bit integer.
2450 */
2451static uint64_t bitfield_replicate(uint64_t mask, unsigned int e)
2452{
2453 assert(e != 0);
2454 while (e < 64) {
2455 mask |= mask << e;
2456 e *= 2;
2457 }
2458 return mask;
2459}
2460
2461/* Return a value with the bottom len bits set (where 0 < len <= 64) */
2462static inline uint64_t bitmask64(unsigned int length)
2463{
2464 assert(length > 0 && length <= 64);
2465 return ~0ULL >> (64 - length);
2466}
2467
2468/* Simplified variant of pseudocode DecodeBitMasks() for the case where we
2469 * only require the wmask. Returns false if the imms/immr/immn are a reserved
2470 * value (ie should cause a guest UNDEF exception), and true if they are
2471 * valid, in which case the decoded bit pattern is written to result.
2472 */
2473static bool logic_imm_decode_wmask(uint64_t *result, unsigned int immn,
2474 unsigned int imms, unsigned int immr)
2475{
2476 uint64_t mask;
2477 unsigned e, levels, s, r;
2478 int len;
2479
2480 assert(immn < 2 && imms < 64 && immr < 64);
2481
2482 /* The bit patterns we create here are 64 bit patterns which
2483 * are vectors of identical elements of size e = 2, 4, 8, 16, 32 or
2484 * 64 bits each. Each element contains the same value: a run
2485 * of between 1 and e-1 non-zero bits, rotated within the
2486 * element by between 0 and e-1 bits.
2487 *
2488 * The element size and run length are encoded into immn (1 bit)
2489 * and imms (6 bits) as follows:
2490 * 64 bit elements: immn = 1, imms = <length of run - 1>
2491 * 32 bit elements: immn = 0, imms = 0 : <length of run - 1>
2492 * 16 bit elements: immn = 0, imms = 10 : <length of run - 1>
2493 * 8 bit elements: immn = 0, imms = 110 : <length of run - 1>
2494 * 4 bit elements: immn = 0, imms = 1110 : <length of run - 1>
2495 * 2 bit elements: immn = 0, imms = 11110 : <length of run - 1>
2496 * Notice that immn = 0, imms = 11111x is the only combination
2497 * not covered by one of the above options; this is reserved.
2498 * Further, <length of run - 1> all-ones is a reserved pattern.
2499 *
2500 * In all cases the rotation is by immr % e (and immr is 6 bits).
2501 */
2502
2503 /* First determine the element size */
2504 len = 31 - clz32((immn << 6) | (~imms & 0x3f));
2505 if (len < 1) {
2506 /* This is the immn == 0, imms == 0x11111x case */
2507 return false;
2508 }
2509 e = 1 << len;
2510
2511 levels = e - 1;
2512 s = imms & levels;
2513 r = immr & levels;
2514
2515 if (s == levels) {
2516 /* <length of run - 1> mustn't be all-ones. */
2517 return false;
2518 }
2519
2520 /* Create the value of one element: s+1 set bits rotated
2521 * by r within the element (which is e bits wide)...
2522 */
2523 mask = bitmask64(s + 1);
2524 mask = (mask >> r) | (mask << (e - r));
2525 /* ...then replicate the element over the whole 64 bit value */
2526 mask = bitfield_replicate(mask, e);
2527 *result = mask;
2528 return true;
2529}
2530
2531/* C3.4.4 Logical (immediate)
2532 * 31 30 29 28 23 22 21 16 15 10 9 5 4 0
2533 * +----+-----+-------------+---+------+------+------+------+
2534 * | sf | opc | 1 0 0 1 0 0 | N | immr | imms | Rn | Rd |
2535 * +----+-----+-------------+---+------+------+------+------+
2536 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00002537static void disas_logic_imm(DisasContext *s, uint32_t insn)
2538{
Alexander Graf71b46082013-12-17 19:42:36 +00002539 unsigned int sf, opc, is_n, immr, imms, rn, rd;
2540 TCGv_i64 tcg_rd, tcg_rn;
2541 uint64_t wmask;
2542 bool is_and = false;
2543
2544 sf = extract32(insn, 31, 1);
2545 opc = extract32(insn, 29, 2);
2546 is_n = extract32(insn, 22, 1);
2547 immr = extract32(insn, 16, 6);
2548 imms = extract32(insn, 10, 6);
2549 rn = extract32(insn, 5, 5);
2550 rd = extract32(insn, 0, 5);
2551
2552 if (!sf && is_n) {
2553 unallocated_encoding(s);
2554 return;
2555 }
2556
2557 if (opc == 0x3) { /* ANDS */
2558 tcg_rd = cpu_reg(s, rd);
2559 } else {
2560 tcg_rd = cpu_reg_sp(s, rd);
2561 }
2562 tcg_rn = cpu_reg(s, rn);
2563
2564 if (!logic_imm_decode_wmask(&wmask, is_n, imms, immr)) {
2565 /* some immediate field values are reserved */
2566 unallocated_encoding(s);
2567 return;
2568 }
2569
2570 if (!sf) {
2571 wmask &= 0xffffffff;
2572 }
2573
2574 switch (opc) {
2575 case 0x3: /* ANDS */
2576 case 0x0: /* AND */
2577 tcg_gen_andi_i64(tcg_rd, tcg_rn, wmask);
2578 is_and = true;
2579 break;
2580 case 0x1: /* ORR */
2581 tcg_gen_ori_i64(tcg_rd, tcg_rn, wmask);
2582 break;
2583 case 0x2: /* EOR */
2584 tcg_gen_xori_i64(tcg_rd, tcg_rn, wmask);
2585 break;
2586 default:
2587 assert(FALSE); /* must handle all above */
2588 break;
2589 }
2590
2591 if (!sf && !is_and) {
2592 /* zero extend final result; we know we can skip this for AND
2593 * since the immediate had the high 32 bits clear.
2594 */
2595 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
2596 }
2597
2598 if (opc == 3) { /* ANDS */
2599 gen_logic_CC(sf, tcg_rd);
2600 }
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00002601}
2602
Alex Bennéeed6ec672013-12-23 23:27:29 +00002603/*
2604 * C3.4.5 Move wide (immediate)
2605 *
2606 * 31 30 29 28 23 22 21 20 5 4 0
2607 * +--+-----+-------------+-----+----------------+------+
2608 * |sf| opc | 1 0 0 1 0 1 | hw | imm16 | Rd |
2609 * +--+-----+-------------+-----+----------------+------+
2610 *
2611 * sf: 0 -> 32 bit, 1 -> 64 bit
2612 * opc: 00 -> N, 10 -> Z, 11 -> K
2613 * hw: shift/16 (0,16, and sf only 32, 48)
2614 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00002615static void disas_movw_imm(DisasContext *s, uint32_t insn)
2616{
Alex Bennéeed6ec672013-12-23 23:27:29 +00002617 int rd = extract32(insn, 0, 5);
2618 uint64_t imm = extract32(insn, 5, 16);
2619 int sf = extract32(insn, 31, 1);
2620 int opc = extract32(insn, 29, 2);
2621 int pos = extract32(insn, 21, 2) << 4;
2622 TCGv_i64 tcg_rd = cpu_reg(s, rd);
2623 TCGv_i64 tcg_imm;
2624
2625 if (!sf && (pos >= 32)) {
2626 unallocated_encoding(s);
2627 return;
2628 }
2629
2630 switch (opc) {
2631 case 0: /* MOVN */
2632 case 2: /* MOVZ */
2633 imm <<= pos;
2634 if (opc == 0) {
2635 imm = ~imm;
2636 }
2637 if (!sf) {
2638 imm &= 0xffffffffu;
2639 }
2640 tcg_gen_movi_i64(tcg_rd, imm);
2641 break;
2642 case 3: /* MOVK */
2643 tcg_imm = tcg_const_i64(imm);
2644 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_imm, pos, 16);
2645 tcg_temp_free_i64(tcg_imm);
2646 if (!sf) {
2647 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
2648 }
2649 break;
2650 default:
2651 unallocated_encoding(s);
2652 break;
2653 }
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00002654}
2655
Claudio Fontana88077742013-12-17 19:42:35 +00002656/* C3.4.2 Bitfield
2657 * 31 30 29 28 23 22 21 16 15 10 9 5 4 0
2658 * +----+-----+-------------+---+------+------+------+------+
2659 * | sf | opc | 1 0 0 1 1 0 | N | immr | imms | Rn | Rd |
2660 * +----+-----+-------------+---+------+------+------+------+
2661 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00002662static void disas_bitfield(DisasContext *s, uint32_t insn)
2663{
Claudio Fontana88077742013-12-17 19:42:35 +00002664 unsigned int sf, n, opc, ri, si, rn, rd, bitsize, pos, len;
2665 TCGv_i64 tcg_rd, tcg_tmp;
2666
2667 sf = extract32(insn, 31, 1);
2668 opc = extract32(insn, 29, 2);
2669 n = extract32(insn, 22, 1);
2670 ri = extract32(insn, 16, 6);
2671 si = extract32(insn, 10, 6);
2672 rn = extract32(insn, 5, 5);
2673 rd = extract32(insn, 0, 5);
2674 bitsize = sf ? 64 : 32;
2675
2676 if (sf != n || ri >= bitsize || si >= bitsize || opc > 2) {
2677 unallocated_encoding(s);
2678 return;
2679 }
2680
2681 tcg_rd = cpu_reg(s, rd);
2682 tcg_tmp = read_cpu_reg(s, rn, sf);
2683
2684 /* OPTME: probably worth recognizing common cases of ext{8,16,32}{u,s} */
2685
2686 if (opc != 1) { /* SBFM or UBFM */
2687 tcg_gen_movi_i64(tcg_rd, 0);
2688 }
2689
2690 /* do the bit move operation */
2691 if (si >= ri) {
2692 /* Wd<s-r:0> = Wn<s:r> */
2693 tcg_gen_shri_i64(tcg_tmp, tcg_tmp, ri);
2694 pos = 0;
2695 len = (si - ri) + 1;
2696 } else {
2697 /* Wd<32+s-r,32-r> = Wn<s:0> */
2698 pos = bitsize - ri;
2699 len = si + 1;
2700 }
2701
2702 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, pos, len);
2703
2704 if (opc == 0) { /* SBFM - sign extend the destination field */
2705 tcg_gen_shli_i64(tcg_rd, tcg_rd, 64 - (pos + len));
2706 tcg_gen_sari_i64(tcg_rd, tcg_rd, 64 - (pos + len));
2707 }
2708
2709 if (!sf) { /* zero extend final result */
2710 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
2711 }
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00002712}
2713
Alexander Grafe801de92013-12-17 19:42:34 +00002714/* C3.4.3 Extract
2715 * 31 30 29 28 23 22 21 20 16 15 10 9 5 4 0
2716 * +----+------+-------------+---+----+------+--------+------+------+
2717 * | sf | op21 | 1 0 0 1 1 1 | N | o0 | Rm | imms | Rn | Rd |
2718 * +----+------+-------------+---+----+------+--------+------+------+
2719 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00002720static void disas_extract(DisasContext *s, uint32_t insn)
2721{
Alexander Grafe801de92013-12-17 19:42:34 +00002722 unsigned int sf, n, rm, imm, rn, rd, bitsize, op21, op0;
2723
2724 sf = extract32(insn, 31, 1);
2725 n = extract32(insn, 22, 1);
2726 rm = extract32(insn, 16, 5);
2727 imm = extract32(insn, 10, 6);
2728 rn = extract32(insn, 5, 5);
2729 rd = extract32(insn, 0, 5);
2730 op21 = extract32(insn, 29, 2);
2731 op0 = extract32(insn, 21, 1);
2732 bitsize = sf ? 64 : 32;
2733
2734 if (sf != n || op21 || op0 || imm >= bitsize) {
2735 unallocated_encoding(s);
2736 } else {
2737 TCGv_i64 tcg_rd, tcg_rm, tcg_rn;
2738
2739 tcg_rd = cpu_reg(s, rd);
2740
2741 if (imm) {
2742 /* OPTME: we can special case rm==rn as a rotate */
2743 tcg_rm = read_cpu_reg(s, rm, sf);
2744 tcg_rn = read_cpu_reg(s, rn, sf);
2745 tcg_gen_shri_i64(tcg_rm, tcg_rm, imm);
2746 tcg_gen_shli_i64(tcg_rn, tcg_rn, bitsize - imm);
2747 tcg_gen_or_i64(tcg_rd, tcg_rm, tcg_rn);
2748 if (!sf) {
2749 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
2750 }
2751 } else {
2752 /* tcg shl_i32/shl_i64 is undefined for 32/64 bit shifts,
2753 * so an extract from bit 0 is a special case.
2754 */
2755 if (sf) {
2756 tcg_gen_mov_i64(tcg_rd, cpu_reg(s, rm));
2757 } else {
2758 tcg_gen_ext32u_i64(tcg_rd, cpu_reg(s, rm));
2759 }
2760 }
2761
2762 }
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00002763}
2764
2765/* C3.4 Data processing - immediate */
2766static void disas_data_proc_imm(DisasContext *s, uint32_t insn)
2767{
2768 switch (extract32(insn, 23, 6)) {
2769 case 0x20: case 0x21: /* PC-rel. addressing */
2770 disas_pc_rel_adr(s, insn);
2771 break;
2772 case 0x22: case 0x23: /* Add/subtract (immediate) */
2773 disas_add_sub_imm(s, insn);
2774 break;
2775 case 0x24: /* Logical (immediate) */
2776 disas_logic_imm(s, insn);
2777 break;
2778 case 0x25: /* Move wide (immediate) */
2779 disas_movw_imm(s, insn);
2780 break;
2781 case 0x26: /* Bitfield */
2782 disas_bitfield(s, insn);
2783 break;
2784 case 0x27: /* Extract */
2785 disas_extract(s, insn);
2786 break;
2787 default:
2788 unallocated_encoding(s);
2789 break;
2790 }
2791}
2792
Alexander Graf832ffa12013-12-17 19:42:34 +00002793/* Shift a TCGv src by TCGv shift_amount, put result in dst.
2794 * Note that it is the caller's responsibility to ensure that the
2795 * shift amount is in range (ie 0..31 or 0..63) and provide the ARM
2796 * mandated semantics for out of range shifts.
2797 */
2798static void shift_reg(TCGv_i64 dst, TCGv_i64 src, int sf,
2799 enum a64_shift_type shift_type, TCGv_i64 shift_amount)
2800{
2801 switch (shift_type) {
2802 case A64_SHIFT_TYPE_LSL:
2803 tcg_gen_shl_i64(dst, src, shift_amount);
2804 break;
2805 case A64_SHIFT_TYPE_LSR:
2806 tcg_gen_shr_i64(dst, src, shift_amount);
2807 break;
2808 case A64_SHIFT_TYPE_ASR:
2809 if (!sf) {
2810 tcg_gen_ext32s_i64(dst, src);
2811 }
2812 tcg_gen_sar_i64(dst, sf ? src : dst, shift_amount);
2813 break;
2814 case A64_SHIFT_TYPE_ROR:
2815 if (sf) {
2816 tcg_gen_rotr_i64(dst, src, shift_amount);
2817 } else {
2818 TCGv_i32 t0, t1;
2819 t0 = tcg_temp_new_i32();
2820 t1 = tcg_temp_new_i32();
2821 tcg_gen_trunc_i64_i32(t0, src);
2822 tcg_gen_trunc_i64_i32(t1, shift_amount);
2823 tcg_gen_rotr_i32(t0, t0, t1);
2824 tcg_gen_extu_i32_i64(dst, t0);
2825 tcg_temp_free_i32(t0);
2826 tcg_temp_free_i32(t1);
2827 }
2828 break;
2829 default:
2830 assert(FALSE); /* all shift types should be handled */
2831 break;
2832 }
2833
2834 if (!sf) { /* zero extend final result */
2835 tcg_gen_ext32u_i64(dst, dst);
2836 }
2837}
2838
2839/* Shift a TCGv src by immediate, put result in dst.
2840 * The shift amount must be in range (this should always be true as the
2841 * relevant instructions will UNDEF on bad shift immediates).
2842 */
2843static void shift_reg_imm(TCGv_i64 dst, TCGv_i64 src, int sf,
2844 enum a64_shift_type shift_type, unsigned int shift_i)
2845{
2846 assert(shift_i < (sf ? 64 : 32));
2847
2848 if (shift_i == 0) {
2849 tcg_gen_mov_i64(dst, src);
2850 } else {
2851 TCGv_i64 shift_const;
2852
2853 shift_const = tcg_const_i64(shift_i);
2854 shift_reg(dst, src, sf, shift_type, shift_const);
2855 tcg_temp_free_i64(shift_const);
2856 }
2857}
2858
2859/* C3.5.10 Logical (shifted register)
2860 * 31 30 29 28 24 23 22 21 20 16 15 10 9 5 4 0
2861 * +----+-----+-----------+-------+---+------+--------+------+------+
2862 * | sf | opc | 0 1 0 1 0 | shift | N | Rm | imm6 | Rn | Rd |
2863 * +----+-----+-----------+-------+---+------+--------+------+------+
2864 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00002865static void disas_logic_reg(DisasContext *s, uint32_t insn)
2866{
Alexander Graf832ffa12013-12-17 19:42:34 +00002867 TCGv_i64 tcg_rd, tcg_rn, tcg_rm;
2868 unsigned int sf, opc, shift_type, invert, rm, shift_amount, rn, rd;
2869
2870 sf = extract32(insn, 31, 1);
2871 opc = extract32(insn, 29, 2);
2872 shift_type = extract32(insn, 22, 2);
2873 invert = extract32(insn, 21, 1);
2874 rm = extract32(insn, 16, 5);
2875 shift_amount = extract32(insn, 10, 6);
2876 rn = extract32(insn, 5, 5);
2877 rd = extract32(insn, 0, 5);
2878
2879 if (!sf && (shift_amount & (1 << 5))) {
2880 unallocated_encoding(s);
2881 return;
2882 }
2883
2884 tcg_rd = cpu_reg(s, rd);
2885
2886 if (opc == 1 && shift_amount == 0 && shift_type == 0 && rn == 31) {
2887 /* Unshifted ORR and ORN with WZR/XZR is the standard encoding for
2888 * register-register MOV and MVN, so it is worth special casing.
2889 */
2890 tcg_rm = cpu_reg(s, rm);
2891 if (invert) {
2892 tcg_gen_not_i64(tcg_rd, tcg_rm);
2893 if (!sf) {
2894 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
2895 }
2896 } else {
2897 if (sf) {
2898 tcg_gen_mov_i64(tcg_rd, tcg_rm);
2899 } else {
2900 tcg_gen_ext32u_i64(tcg_rd, tcg_rm);
2901 }
2902 }
2903 return;
2904 }
2905
2906 tcg_rm = read_cpu_reg(s, rm, sf);
2907
2908 if (shift_amount) {
2909 shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, shift_amount);
2910 }
2911
2912 tcg_rn = cpu_reg(s, rn);
2913
2914 switch (opc | (invert << 2)) {
2915 case 0: /* AND */
2916 case 3: /* ANDS */
2917 tcg_gen_and_i64(tcg_rd, tcg_rn, tcg_rm);
2918 break;
2919 case 1: /* ORR */
2920 tcg_gen_or_i64(tcg_rd, tcg_rn, tcg_rm);
2921 break;
2922 case 2: /* EOR */
2923 tcg_gen_xor_i64(tcg_rd, tcg_rn, tcg_rm);
2924 break;
2925 case 4: /* BIC */
2926 case 7: /* BICS */
2927 tcg_gen_andc_i64(tcg_rd, tcg_rn, tcg_rm);
2928 break;
2929 case 5: /* ORN */
2930 tcg_gen_orc_i64(tcg_rd, tcg_rn, tcg_rm);
2931 break;
2932 case 6: /* EON */
2933 tcg_gen_eqv_i64(tcg_rd, tcg_rn, tcg_rm);
2934 break;
2935 default:
2936 assert(FALSE);
2937 break;
2938 }
2939
2940 if (!sf) {
2941 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
2942 }
2943
2944 if (opc == 3) {
2945 gen_logic_CC(sf, tcg_rd);
2946 }
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00002947}
2948
Alex Bennéeb0ff21b2013-12-23 23:27:29 +00002949/*
2950 * C3.5.1 Add/subtract (extended register)
2951 *
2952 * 31|30|29|28 24|23 22|21|20 16|15 13|12 10|9 5|4 0|
2953 * +--+--+--+-----------+-----+--+-------+------+------+----+----+
2954 * |sf|op| S| 0 1 0 1 1 | opt | 1| Rm |option| imm3 | Rn | Rd |
2955 * +--+--+--+-----------+-----+--+-------+------+------+----+----+
2956 *
2957 * sf: 0 -> 32bit, 1 -> 64bit
2958 * op: 0 -> add , 1 -> sub
2959 * S: 1 -> set flags
2960 * opt: 00
2961 * option: extension type (see DecodeRegExtend)
2962 * imm3: optional shift to Rm
2963 *
2964 * Rd = Rn + LSL(extend(Rm), amount)
2965 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00002966static void disas_add_sub_ext_reg(DisasContext *s, uint32_t insn)
2967{
Alex Bennéeb0ff21b2013-12-23 23:27:29 +00002968 int rd = extract32(insn, 0, 5);
2969 int rn = extract32(insn, 5, 5);
2970 int imm3 = extract32(insn, 10, 3);
2971 int option = extract32(insn, 13, 3);
2972 int rm = extract32(insn, 16, 5);
2973 bool setflags = extract32(insn, 29, 1);
2974 bool sub_op = extract32(insn, 30, 1);
2975 bool sf = extract32(insn, 31, 1);
2976
2977 TCGv_i64 tcg_rm, tcg_rn; /* temps */
2978 TCGv_i64 tcg_rd;
2979 TCGv_i64 tcg_result;
2980
2981 if (imm3 > 4) {
2982 unallocated_encoding(s);
2983 return;
2984 }
2985
2986 /* non-flag setting ops may use SP */
2987 if (!setflags) {
2988 tcg_rn = read_cpu_reg_sp(s, rn, sf);
2989 tcg_rd = cpu_reg_sp(s, rd);
2990 } else {
2991 tcg_rn = read_cpu_reg(s, rn, sf);
2992 tcg_rd = cpu_reg(s, rd);
2993 }
2994
2995 tcg_rm = read_cpu_reg(s, rm, sf);
2996 ext_and_shift_reg(tcg_rm, tcg_rm, option, imm3);
2997
2998 tcg_result = tcg_temp_new_i64();
2999
3000 if (!setflags) {
3001 if (sub_op) {
3002 tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
3003 } else {
3004 tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
3005 }
3006 } else {
3007 if (sub_op) {
3008 gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
3009 } else {
3010 gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
3011 }
3012 }
3013
3014 if (sf) {
3015 tcg_gen_mov_i64(tcg_rd, tcg_result);
3016 } else {
3017 tcg_gen_ext32u_i64(tcg_rd, tcg_result);
3018 }
3019
3020 tcg_temp_free_i64(tcg_result);
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00003021}
3022
Alex Bennéeb0ff21b2013-12-23 23:27:29 +00003023/*
3024 * C3.5.2 Add/subtract (shifted register)
3025 *
3026 * 31 30 29 28 24 23 22 21 20 16 15 10 9 5 4 0
3027 * +--+--+--+-----------+-----+--+-------+---------+------+------+
3028 * |sf|op| S| 0 1 0 1 1 |shift| 0| Rm | imm6 | Rn | Rd |
3029 * +--+--+--+-----------+-----+--+-------+---------+------+------+
3030 *
3031 * sf: 0 -> 32bit, 1 -> 64bit
3032 * op: 0 -> add , 1 -> sub
3033 * S: 1 -> set flags
3034 * shift: 00 -> LSL, 01 -> LSR, 10 -> ASR, 11 -> RESERVED
3035 * imm6: Shift amount to apply to Rm before the add/sub
3036 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00003037static void disas_add_sub_reg(DisasContext *s, uint32_t insn)
3038{
Alex Bennéeb0ff21b2013-12-23 23:27:29 +00003039 int rd = extract32(insn, 0, 5);
3040 int rn = extract32(insn, 5, 5);
3041 int imm6 = extract32(insn, 10, 6);
3042 int rm = extract32(insn, 16, 5);
3043 int shift_type = extract32(insn, 22, 2);
3044 bool setflags = extract32(insn, 29, 1);
3045 bool sub_op = extract32(insn, 30, 1);
3046 bool sf = extract32(insn, 31, 1);
3047
3048 TCGv_i64 tcg_rd = cpu_reg(s, rd);
3049 TCGv_i64 tcg_rn, tcg_rm;
3050 TCGv_i64 tcg_result;
3051
3052 if ((shift_type == 3) || (!sf && (imm6 > 31))) {
3053 unallocated_encoding(s);
3054 return;
3055 }
3056
3057 tcg_rn = read_cpu_reg(s, rn, sf);
3058 tcg_rm = read_cpu_reg(s, rm, sf);
3059
3060 shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, imm6);
3061
3062 tcg_result = tcg_temp_new_i64();
3063
3064 if (!setflags) {
3065 if (sub_op) {
3066 tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
3067 } else {
3068 tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
3069 }
3070 } else {
3071 if (sub_op) {
3072 gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
3073 } else {
3074 gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
3075 }
3076 }
3077
3078 if (sf) {
3079 tcg_gen_mov_i64(tcg_rd, tcg_result);
3080 } else {
3081 tcg_gen_ext32u_i64(tcg_rd, tcg_result);
3082 }
3083
3084 tcg_temp_free_i64(tcg_result);
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00003085}
3086
Alexander Graf52c8b9a2013-12-23 23:27:30 +00003087/* C3.5.9 Data-processing (3 source)
3088
3089 31 30 29 28 24 23 21 20 16 15 14 10 9 5 4 0
3090 +--+------+-----------+------+------+----+------+------+------+
3091 |sf| op54 | 1 1 0 1 1 | op31 | Rm | o0 | Ra | Rn | Rd |
3092 +--+------+-----------+------+------+----+------+------+------+
3093
3094 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00003095static void disas_data_proc_3src(DisasContext *s, uint32_t insn)
3096{
Alexander Graf52c8b9a2013-12-23 23:27:30 +00003097 int rd = extract32(insn, 0, 5);
3098 int rn = extract32(insn, 5, 5);
3099 int ra = extract32(insn, 10, 5);
3100 int rm = extract32(insn, 16, 5);
3101 int op_id = (extract32(insn, 29, 3) << 4) |
3102 (extract32(insn, 21, 3) << 1) |
3103 extract32(insn, 15, 1);
3104 bool sf = extract32(insn, 31, 1);
3105 bool is_sub = extract32(op_id, 0, 1);
3106 bool is_high = extract32(op_id, 2, 1);
3107 bool is_signed = false;
3108 TCGv_i64 tcg_op1;
3109 TCGv_i64 tcg_op2;
3110 TCGv_i64 tcg_tmp;
3111
3112 /* Note that op_id is sf:op54:op31:o0 so it includes the 32/64 size flag */
3113 switch (op_id) {
3114 case 0x42: /* SMADDL */
3115 case 0x43: /* SMSUBL */
3116 case 0x44: /* SMULH */
3117 is_signed = true;
3118 break;
3119 case 0x0: /* MADD (32bit) */
3120 case 0x1: /* MSUB (32bit) */
3121 case 0x40: /* MADD (64bit) */
3122 case 0x41: /* MSUB (64bit) */
3123 case 0x4a: /* UMADDL */
3124 case 0x4b: /* UMSUBL */
3125 case 0x4c: /* UMULH */
3126 break;
3127 default:
3128 unallocated_encoding(s);
3129 return;
3130 }
3131
3132 if (is_high) {
3133 TCGv_i64 low_bits = tcg_temp_new_i64(); /* low bits discarded */
3134 TCGv_i64 tcg_rd = cpu_reg(s, rd);
3135 TCGv_i64 tcg_rn = cpu_reg(s, rn);
3136 TCGv_i64 tcg_rm = cpu_reg(s, rm);
3137
3138 if (is_signed) {
3139 tcg_gen_muls2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
3140 } else {
3141 tcg_gen_mulu2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
3142 }
3143
3144 tcg_temp_free_i64(low_bits);
3145 return;
3146 }
3147
3148 tcg_op1 = tcg_temp_new_i64();
3149 tcg_op2 = tcg_temp_new_i64();
3150 tcg_tmp = tcg_temp_new_i64();
3151
3152 if (op_id < 0x42) {
3153 tcg_gen_mov_i64(tcg_op1, cpu_reg(s, rn));
3154 tcg_gen_mov_i64(tcg_op2, cpu_reg(s, rm));
3155 } else {
3156 if (is_signed) {
3157 tcg_gen_ext32s_i64(tcg_op1, cpu_reg(s, rn));
3158 tcg_gen_ext32s_i64(tcg_op2, cpu_reg(s, rm));
3159 } else {
3160 tcg_gen_ext32u_i64(tcg_op1, cpu_reg(s, rn));
3161 tcg_gen_ext32u_i64(tcg_op2, cpu_reg(s, rm));
3162 }
3163 }
3164
3165 if (ra == 31 && !is_sub) {
3166 /* Special-case MADD with rA == XZR; it is the standard MUL alias */
3167 tcg_gen_mul_i64(cpu_reg(s, rd), tcg_op1, tcg_op2);
3168 } else {
3169 tcg_gen_mul_i64(tcg_tmp, tcg_op1, tcg_op2);
3170 if (is_sub) {
3171 tcg_gen_sub_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
3172 } else {
3173 tcg_gen_add_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
3174 }
3175 }
3176
3177 if (!sf) {
3178 tcg_gen_ext32u_i64(cpu_reg(s, rd), cpu_reg(s, rd));
3179 }
3180
3181 tcg_temp_free_i64(tcg_op1);
3182 tcg_temp_free_i64(tcg_op2);
3183 tcg_temp_free_i64(tcg_tmp);
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00003184}
3185
Claudio Fontana643dbb02014-01-04 22:15:46 +00003186/* C3.5.3 - Add/subtract (with carry)
3187 * 31 30 29 28 27 26 25 24 23 22 21 20 16 15 10 9 5 4 0
3188 * +--+--+--+------------------------+------+---------+------+-----+
3189 * |sf|op| S| 1 1 0 1 0 0 0 0 | rm | opcode2 | Rn | Rd |
3190 * +--+--+--+------------------------+------+---------+------+-----+
3191 * [000000]
3192 */
3193
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00003194static void disas_adc_sbc(DisasContext *s, uint32_t insn)
3195{
Claudio Fontana643dbb02014-01-04 22:15:46 +00003196 unsigned int sf, op, setflags, rm, rn, rd;
3197 TCGv_i64 tcg_y, tcg_rn, tcg_rd;
3198
3199 if (extract32(insn, 10, 6) != 0) {
3200 unallocated_encoding(s);
3201 return;
3202 }
3203
3204 sf = extract32(insn, 31, 1);
3205 op = extract32(insn, 30, 1);
3206 setflags = extract32(insn, 29, 1);
3207 rm = extract32(insn, 16, 5);
3208 rn = extract32(insn, 5, 5);
3209 rd = extract32(insn, 0, 5);
3210
3211 tcg_rd = cpu_reg(s, rd);
3212 tcg_rn = cpu_reg(s, rn);
3213
3214 if (op) {
3215 tcg_y = new_tmp_a64(s);
3216 tcg_gen_not_i64(tcg_y, cpu_reg(s, rm));
3217 } else {
3218 tcg_y = cpu_reg(s, rm);
3219 }
3220
3221 if (setflags) {
3222 gen_adc_CC(sf, tcg_rd, tcg_rn, tcg_y);
3223 } else {
3224 gen_adc(sf, tcg_rd, tcg_rn, tcg_y);
3225 }
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00003226}
3227
Claudio Fontana750813c2014-01-04 22:15:46 +00003228/* C3.5.4 - C3.5.5 Conditional compare (immediate / register)
3229 * 31 30 29 28 27 26 25 24 23 22 21 20 16 15 12 11 10 9 5 4 3 0
3230 * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
3231 * |sf|op| S| 1 1 0 1 0 0 1 0 |imm5/rm | cond |i/r |o2| Rn |o3|nzcv |
3232 * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
3233 * [1] y [0] [0]
3234 */
3235static void disas_cc(DisasContext *s, uint32_t insn)
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00003236{
Claudio Fontana750813c2014-01-04 22:15:46 +00003237 unsigned int sf, op, y, cond, rn, nzcv, is_imm;
3238 int label_continue = -1;
3239 TCGv_i64 tcg_tmp, tcg_y, tcg_rn;
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00003240
Claudio Fontana750813c2014-01-04 22:15:46 +00003241 if (!extract32(insn, 29, 1)) {
3242 unallocated_encoding(s);
3243 return;
3244 }
3245 if (insn & (1 << 10 | 1 << 4)) {
3246 unallocated_encoding(s);
3247 return;
3248 }
3249 sf = extract32(insn, 31, 1);
3250 op = extract32(insn, 30, 1);
3251 is_imm = extract32(insn, 11, 1);
3252 y = extract32(insn, 16, 5); /* y = rm (reg) or imm5 (imm) */
3253 cond = extract32(insn, 12, 4);
3254 rn = extract32(insn, 5, 5);
3255 nzcv = extract32(insn, 0, 4);
3256
3257 if (cond < 0x0e) { /* not always */
3258 int label_match = gen_new_label();
3259 label_continue = gen_new_label();
3260 arm_gen_test_cc(cond, label_match);
3261 /* nomatch: */
3262 tcg_tmp = tcg_temp_new_i64();
3263 tcg_gen_movi_i64(tcg_tmp, nzcv << 28);
3264 gen_set_nzcv(tcg_tmp);
3265 tcg_temp_free_i64(tcg_tmp);
3266 tcg_gen_br(label_continue);
3267 gen_set_label(label_match);
3268 }
3269 /* match, or condition is always */
3270 if (is_imm) {
3271 tcg_y = new_tmp_a64(s);
3272 tcg_gen_movi_i64(tcg_y, y);
3273 } else {
3274 tcg_y = cpu_reg(s, y);
3275 }
3276 tcg_rn = cpu_reg(s, rn);
3277
3278 tcg_tmp = tcg_temp_new_i64();
3279 if (op) {
3280 gen_sub_CC(sf, tcg_tmp, tcg_rn, tcg_y);
3281 } else {
3282 gen_add_CC(sf, tcg_tmp, tcg_rn, tcg_y);
3283 }
3284 tcg_temp_free_i64(tcg_tmp);
3285
3286 if (cond < 0x0e) { /* continue */
3287 gen_set_label(label_continue);
3288 }
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00003289}
3290
Claudio Fontanae952d8c2013-12-17 19:42:33 +00003291/* C3.5.6 Conditional select
3292 * 31 30 29 28 21 20 16 15 12 11 10 9 5 4 0
3293 * +----+----+---+-----------------+------+------+-----+------+------+
3294 * | sf | op | S | 1 1 0 1 0 1 0 0 | Rm | cond | op2 | Rn | Rd |
3295 * +----+----+---+-----------------+------+------+-----+------+------+
3296 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00003297static void disas_cond_select(DisasContext *s, uint32_t insn)
3298{
Claudio Fontanae952d8c2013-12-17 19:42:33 +00003299 unsigned int sf, else_inv, rm, cond, else_inc, rn, rd;
3300 TCGv_i64 tcg_rd, tcg_src;
3301
3302 if (extract32(insn, 29, 1) || extract32(insn, 11, 1)) {
3303 /* S == 1 or op2<1> == 1 */
3304 unallocated_encoding(s);
3305 return;
3306 }
3307 sf = extract32(insn, 31, 1);
3308 else_inv = extract32(insn, 30, 1);
3309 rm = extract32(insn, 16, 5);
3310 cond = extract32(insn, 12, 4);
3311 else_inc = extract32(insn, 10, 1);
3312 rn = extract32(insn, 5, 5);
3313 rd = extract32(insn, 0, 5);
3314
3315 if (rd == 31) {
3316 /* silly no-op write; until we use movcond we must special-case
3317 * this to avoid a dead temporary across basic blocks.
3318 */
3319 return;
3320 }
3321
3322 tcg_rd = cpu_reg(s, rd);
3323
3324 if (cond >= 0x0e) { /* condition "always" */
3325 tcg_src = read_cpu_reg(s, rn, sf);
3326 tcg_gen_mov_i64(tcg_rd, tcg_src);
3327 } else {
3328 /* OPTME: we could use movcond here, at the cost of duplicating
3329 * a lot of the arm_gen_test_cc() logic.
3330 */
3331 int label_match = gen_new_label();
3332 int label_continue = gen_new_label();
3333
3334 arm_gen_test_cc(cond, label_match);
3335 /* nomatch: */
3336 tcg_src = cpu_reg(s, rm);
3337
3338 if (else_inv && else_inc) {
3339 tcg_gen_neg_i64(tcg_rd, tcg_src);
3340 } else if (else_inv) {
3341 tcg_gen_not_i64(tcg_rd, tcg_src);
3342 } else if (else_inc) {
3343 tcg_gen_addi_i64(tcg_rd, tcg_src, 1);
3344 } else {
3345 tcg_gen_mov_i64(tcg_rd, tcg_src);
3346 }
3347 if (!sf) {
3348 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3349 }
3350 tcg_gen_br(label_continue);
3351 /* match: */
3352 gen_set_label(label_match);
3353 tcg_src = read_cpu_reg(s, rn, sf);
3354 tcg_gen_mov_i64(tcg_rd, tcg_src);
3355 /* continue: */
3356 gen_set_label(label_continue);
3357 }
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00003358}
3359
Claudio Fontana680ead22013-12-17 19:42:35 +00003360static void handle_clz(DisasContext *s, unsigned int sf,
3361 unsigned int rn, unsigned int rd)
3362{
3363 TCGv_i64 tcg_rd, tcg_rn;
3364 tcg_rd = cpu_reg(s, rd);
3365 tcg_rn = cpu_reg(s, rn);
3366
3367 if (sf) {
3368 gen_helper_clz64(tcg_rd, tcg_rn);
3369 } else {
3370 TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
3371 tcg_gen_trunc_i64_i32(tcg_tmp32, tcg_rn);
3372 gen_helper_clz(tcg_tmp32, tcg_tmp32);
3373 tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
3374 tcg_temp_free_i32(tcg_tmp32);
3375 }
3376}
3377
Claudio Fontanae80c5022013-12-17 19:42:35 +00003378static void handle_cls(DisasContext *s, unsigned int sf,
3379 unsigned int rn, unsigned int rd)
3380{
3381 TCGv_i64 tcg_rd, tcg_rn;
3382 tcg_rd = cpu_reg(s, rd);
3383 tcg_rn = cpu_reg(s, rn);
3384
3385 if (sf) {
3386 gen_helper_cls64(tcg_rd, tcg_rn);
3387 } else {
3388 TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
3389 tcg_gen_trunc_i64_i32(tcg_tmp32, tcg_rn);
3390 gen_helper_cls32(tcg_tmp32, tcg_tmp32);
3391 tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
3392 tcg_temp_free_i32(tcg_tmp32);
3393 }
3394}
3395
Alexander Graf82e14b02013-12-17 19:42:35 +00003396static void handle_rbit(DisasContext *s, unsigned int sf,
3397 unsigned int rn, unsigned int rd)
3398{
3399 TCGv_i64 tcg_rd, tcg_rn;
3400 tcg_rd = cpu_reg(s, rd);
3401 tcg_rn = cpu_reg(s, rn);
3402
3403 if (sf) {
3404 gen_helper_rbit64(tcg_rd, tcg_rn);
3405 } else {
3406 TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
3407 tcg_gen_trunc_i64_i32(tcg_tmp32, tcg_rn);
3408 gen_helper_rbit(tcg_tmp32, tcg_tmp32);
3409 tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
3410 tcg_temp_free_i32(tcg_tmp32);
3411 }
3412}
3413
Claudio Fontana45323202013-12-17 19:42:35 +00003414/* C5.6.149 REV with sf==1, opcode==3 ("REV64") */
3415static void handle_rev64(DisasContext *s, unsigned int sf,
3416 unsigned int rn, unsigned int rd)
3417{
3418 if (!sf) {
3419 unallocated_encoding(s);
3420 return;
3421 }
3422 tcg_gen_bswap64_i64(cpu_reg(s, rd), cpu_reg(s, rn));
3423}
3424
3425/* C5.6.149 REV with sf==0, opcode==2
3426 * C5.6.151 REV32 (sf==1, opcode==2)
3427 */
3428static void handle_rev32(DisasContext *s, unsigned int sf,
3429 unsigned int rn, unsigned int rd)
3430{
3431 TCGv_i64 tcg_rd = cpu_reg(s, rd);
3432
3433 if (sf) {
3434 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
3435 TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
3436
3437 /* bswap32_i64 requires zero high word */
3438 tcg_gen_ext32u_i64(tcg_tmp, tcg_rn);
3439 tcg_gen_bswap32_i64(tcg_rd, tcg_tmp);
3440 tcg_gen_shri_i64(tcg_tmp, tcg_rn, 32);
3441 tcg_gen_bswap32_i64(tcg_tmp, tcg_tmp);
3442 tcg_gen_concat32_i64(tcg_rd, tcg_rd, tcg_tmp);
3443
3444 tcg_temp_free_i64(tcg_tmp);
3445 } else {
3446 tcg_gen_ext32u_i64(tcg_rd, cpu_reg(s, rn));
3447 tcg_gen_bswap32_i64(tcg_rd, tcg_rd);
3448 }
3449}
3450
3451/* C5.6.150 REV16 (opcode==1) */
3452static void handle_rev16(DisasContext *s, unsigned int sf,
3453 unsigned int rn, unsigned int rd)
3454{
3455 TCGv_i64 tcg_rd = cpu_reg(s, rd);
3456 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
3457 TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
3458
3459 tcg_gen_andi_i64(tcg_tmp, tcg_rn, 0xffff);
3460 tcg_gen_bswap16_i64(tcg_rd, tcg_tmp);
3461
3462 tcg_gen_shri_i64(tcg_tmp, tcg_rn, 16);
3463 tcg_gen_andi_i64(tcg_tmp, tcg_tmp, 0xffff);
3464 tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp);
3465 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, 16, 16);
3466
3467 if (sf) {
3468 tcg_gen_shri_i64(tcg_tmp, tcg_rn, 32);
3469 tcg_gen_andi_i64(tcg_tmp, tcg_tmp, 0xffff);
3470 tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp);
3471 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, 32, 16);
3472
3473 tcg_gen_shri_i64(tcg_tmp, tcg_rn, 48);
3474 tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp);
3475 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, 48, 16);
3476 }
3477
3478 tcg_temp_free_i64(tcg_tmp);
3479}
3480
Claudio Fontana680ead22013-12-17 19:42:35 +00003481/* C3.5.7 Data-processing (1 source)
3482 * 31 30 29 28 21 20 16 15 10 9 5 4 0
3483 * +----+---+---+-----------------+---------+--------+------+------+
3484 * | sf | 1 | S | 1 1 0 1 0 1 1 0 | opcode2 | opcode | Rn | Rd |
3485 * +----+---+---+-----------------+---------+--------+------+------+
3486 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00003487static void disas_data_proc_1src(DisasContext *s, uint32_t insn)
3488{
Claudio Fontana680ead22013-12-17 19:42:35 +00003489 unsigned int sf, opcode, rn, rd;
3490
3491 if (extract32(insn, 29, 1) || extract32(insn, 16, 5)) {
3492 unallocated_encoding(s);
3493 return;
3494 }
3495
3496 sf = extract32(insn, 31, 1);
3497 opcode = extract32(insn, 10, 6);
3498 rn = extract32(insn, 5, 5);
3499 rd = extract32(insn, 0, 5);
3500
3501 switch (opcode) {
3502 case 0: /* RBIT */
Alexander Graf82e14b02013-12-17 19:42:35 +00003503 handle_rbit(s, sf, rn, rd);
3504 break;
Claudio Fontana680ead22013-12-17 19:42:35 +00003505 case 1: /* REV16 */
Claudio Fontana45323202013-12-17 19:42:35 +00003506 handle_rev16(s, sf, rn, rd);
3507 break;
Claudio Fontana680ead22013-12-17 19:42:35 +00003508 case 2: /* REV32 */
Claudio Fontana45323202013-12-17 19:42:35 +00003509 handle_rev32(s, sf, rn, rd);
3510 break;
Claudio Fontana680ead22013-12-17 19:42:35 +00003511 case 3: /* REV64 */
Claudio Fontana45323202013-12-17 19:42:35 +00003512 handle_rev64(s, sf, rn, rd);
Claudio Fontana680ead22013-12-17 19:42:35 +00003513 break;
3514 case 4: /* CLZ */
3515 handle_clz(s, sf, rn, rd);
3516 break;
3517 case 5: /* CLS */
Claudio Fontanae80c5022013-12-17 19:42:35 +00003518 handle_cls(s, sf, rn, rd);
Claudio Fontana680ead22013-12-17 19:42:35 +00003519 break;
3520 }
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00003521}
3522
Alexander Graf8220e912013-12-17 19:42:34 +00003523static void handle_div(DisasContext *s, bool is_signed, unsigned int sf,
3524 unsigned int rm, unsigned int rn, unsigned int rd)
3525{
3526 TCGv_i64 tcg_n, tcg_m, tcg_rd;
3527 tcg_rd = cpu_reg(s, rd);
3528
3529 if (!sf && is_signed) {
3530 tcg_n = new_tmp_a64(s);
3531 tcg_m = new_tmp_a64(s);
3532 tcg_gen_ext32s_i64(tcg_n, cpu_reg(s, rn));
3533 tcg_gen_ext32s_i64(tcg_m, cpu_reg(s, rm));
3534 } else {
3535 tcg_n = read_cpu_reg(s, rn, sf);
3536 tcg_m = read_cpu_reg(s, rm, sf);
3537 }
3538
3539 if (is_signed) {
3540 gen_helper_sdiv64(tcg_rd, tcg_n, tcg_m);
3541 } else {
3542 gen_helper_udiv64(tcg_rd, tcg_n, tcg_m);
3543 }
3544
3545 if (!sf) { /* zero extend final result */
3546 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3547 }
3548}
3549
Alexander Graf6c1adc92013-12-17 19:42:34 +00003550/* C5.6.115 LSLV, C5.6.118 LSRV, C5.6.17 ASRV, C5.6.154 RORV */
3551static void handle_shift_reg(DisasContext *s,
3552 enum a64_shift_type shift_type, unsigned int sf,
3553 unsigned int rm, unsigned int rn, unsigned int rd)
3554{
3555 TCGv_i64 tcg_shift = tcg_temp_new_i64();
3556 TCGv_i64 tcg_rd = cpu_reg(s, rd);
3557 TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
3558
3559 tcg_gen_andi_i64(tcg_shift, cpu_reg(s, rm), sf ? 63 : 31);
3560 shift_reg(tcg_rd, tcg_rn, sf, shift_type, tcg_shift);
3561 tcg_temp_free_i64(tcg_shift);
3562}
3563
Alexander Graf8220e912013-12-17 19:42:34 +00003564/* C3.5.8 Data-processing (2 source)
3565 * 31 30 29 28 21 20 16 15 10 9 5 4 0
3566 * +----+---+---+-----------------+------+--------+------+------+
3567 * | sf | 0 | S | 1 1 0 1 0 1 1 0 | Rm | opcode | Rn | Rd |
3568 * +----+---+---+-----------------+------+--------+------+------+
3569 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00003570static void disas_data_proc_2src(DisasContext *s, uint32_t insn)
3571{
Alexander Graf8220e912013-12-17 19:42:34 +00003572 unsigned int sf, rm, opcode, rn, rd;
3573 sf = extract32(insn, 31, 1);
3574 rm = extract32(insn, 16, 5);
3575 opcode = extract32(insn, 10, 6);
3576 rn = extract32(insn, 5, 5);
3577 rd = extract32(insn, 0, 5);
3578
3579 if (extract32(insn, 29, 1)) {
3580 unallocated_encoding(s);
3581 return;
3582 }
3583
3584 switch (opcode) {
3585 case 2: /* UDIV */
3586 handle_div(s, false, sf, rm, rn, rd);
3587 break;
3588 case 3: /* SDIV */
3589 handle_div(s, true, sf, rm, rn, rd);
3590 break;
3591 case 8: /* LSLV */
Alexander Graf6c1adc92013-12-17 19:42:34 +00003592 handle_shift_reg(s, A64_SHIFT_TYPE_LSL, sf, rm, rn, rd);
3593 break;
Alexander Graf8220e912013-12-17 19:42:34 +00003594 case 9: /* LSRV */
Alexander Graf6c1adc92013-12-17 19:42:34 +00003595 handle_shift_reg(s, A64_SHIFT_TYPE_LSR, sf, rm, rn, rd);
3596 break;
Alexander Graf8220e912013-12-17 19:42:34 +00003597 case 10: /* ASRV */
Alexander Graf6c1adc92013-12-17 19:42:34 +00003598 handle_shift_reg(s, A64_SHIFT_TYPE_ASR, sf, rm, rn, rd);
3599 break;
Alexander Graf8220e912013-12-17 19:42:34 +00003600 case 11: /* RORV */
Alexander Graf6c1adc92013-12-17 19:42:34 +00003601 handle_shift_reg(s, A64_SHIFT_TYPE_ROR, sf, rm, rn, rd);
3602 break;
Alexander Graf8220e912013-12-17 19:42:34 +00003603 case 16:
3604 case 17:
3605 case 18:
3606 case 19:
3607 case 20:
3608 case 21:
3609 case 22:
3610 case 23: /* CRC32 */
3611 unsupported_encoding(s, insn);
3612 break;
3613 default:
3614 unallocated_encoding(s);
3615 break;
3616 }
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00003617}
3618
3619/* C3.5 Data processing - register */
3620static void disas_data_proc_reg(DisasContext *s, uint32_t insn)
3621{
3622 switch (extract32(insn, 24, 5)) {
3623 case 0x0a: /* Logical (shifted register) */
3624 disas_logic_reg(s, insn);
3625 break;
3626 case 0x0b: /* Add/subtract */
3627 if (insn & (1 << 21)) { /* (extended register) */
3628 disas_add_sub_ext_reg(s, insn);
3629 } else {
3630 disas_add_sub_reg(s, insn);
3631 }
3632 break;
3633 case 0x1b: /* Data-processing (3 source) */
3634 disas_data_proc_3src(s, insn);
3635 break;
3636 case 0x1a:
3637 switch (extract32(insn, 21, 3)) {
3638 case 0x0: /* Add/subtract (with carry) */
3639 disas_adc_sbc(s, insn);
3640 break;
3641 case 0x2: /* Conditional compare */
Claudio Fontana750813c2014-01-04 22:15:46 +00003642 disas_cc(s, insn); /* both imm and reg forms */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00003643 break;
3644 case 0x4: /* Conditional select */
3645 disas_cond_select(s, insn);
3646 break;
3647 case 0x6: /* Data-processing */
3648 if (insn & (1 << 30)) { /* (1 source) */
3649 disas_data_proc_1src(s, insn);
3650 } else { /* (2 source) */
3651 disas_data_proc_2src(s, insn);
3652 }
3653 break;
3654 default:
3655 unallocated_encoding(s);
3656 break;
3657 }
3658 break;
3659 default:
3660 unallocated_encoding(s);
3661 break;
3662 }
3663}
3664
Alexander Graf52a1f6a2014-01-07 17:19:14 +00003665/* Convert ARM rounding mode to softfloat */
3666static inline int arm_rmode_to_sf(int rmode)
3667{
3668 switch (rmode) {
3669 case FPROUNDING_TIEAWAY:
3670 rmode = float_round_ties_away;
3671 break;
3672 case FPROUNDING_ODD:
3673 /* FIXME: add support for TIEAWAY and ODD */
3674 qemu_log_mask(LOG_UNIMP, "arm: unimplemented rounding mode: %d\n",
3675 rmode);
3676 case FPROUNDING_TIEEVEN:
3677 default:
3678 rmode = float_round_nearest_even;
3679 break;
3680 case FPROUNDING_POSINF:
3681 rmode = float_round_up;
3682 break;
3683 case FPROUNDING_NEGINF:
3684 rmode = float_round_down;
3685 break;
3686 case FPROUNDING_ZERO:
3687 rmode = float_round_to_zero;
3688 break;
3689 }
3690 return rmode;
3691}
3692
Claudio Fontanada7dafe2014-01-04 22:15:50 +00003693static void handle_fp_compare(DisasContext *s, bool is_double,
3694 unsigned int rn, unsigned int rm,
3695 bool cmp_with_zero, bool signal_all_nans)
3696{
3697 TCGv_i64 tcg_flags = tcg_temp_new_i64();
3698 TCGv_ptr fpst = get_fpstatus_ptr();
3699
3700 if (is_double) {
3701 TCGv_i64 tcg_vn, tcg_vm;
3702
3703 tcg_vn = read_fp_dreg(s, rn);
3704 if (cmp_with_zero) {
3705 tcg_vm = tcg_const_i64(0);
3706 } else {
3707 tcg_vm = read_fp_dreg(s, rm);
3708 }
3709 if (signal_all_nans) {
3710 gen_helper_vfp_cmped_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
3711 } else {
3712 gen_helper_vfp_cmpd_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
3713 }
3714 tcg_temp_free_i64(tcg_vn);
3715 tcg_temp_free_i64(tcg_vm);
3716 } else {
3717 TCGv_i32 tcg_vn, tcg_vm;
3718
3719 tcg_vn = read_fp_sreg(s, rn);
3720 if (cmp_with_zero) {
3721 tcg_vm = tcg_const_i32(0);
3722 } else {
3723 tcg_vm = read_fp_sreg(s, rm);
3724 }
3725 if (signal_all_nans) {
3726 gen_helper_vfp_cmpes_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
3727 } else {
3728 gen_helper_vfp_cmps_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
3729 }
3730 tcg_temp_free_i32(tcg_vn);
3731 tcg_temp_free_i32(tcg_vm);
3732 }
3733
3734 tcg_temp_free_ptr(fpst);
3735
3736 gen_set_nzcv(tcg_flags);
3737
3738 tcg_temp_free_i64(tcg_flags);
3739}
3740
Peter Maydellfaa0ba42013-12-23 23:27:30 +00003741/* C3.6.22 Floating point compare
3742 * 31 30 29 28 24 23 22 21 20 16 15 14 13 10 9 5 4 0
3743 * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
3744 * | M | 0 | S | 1 1 1 1 0 | type | 1 | Rm | op | 1 0 0 0 | Rn | op2 |
3745 * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
3746 */
3747static void disas_fp_compare(DisasContext *s, uint32_t insn)
3748{
Claudio Fontanada7dafe2014-01-04 22:15:50 +00003749 unsigned int mos, type, rm, op, rn, opc, op2r;
3750
3751 mos = extract32(insn, 29, 3);
3752 type = extract32(insn, 22, 2); /* 0 = single, 1 = double */
3753 rm = extract32(insn, 16, 5);
3754 op = extract32(insn, 14, 2);
3755 rn = extract32(insn, 5, 5);
3756 opc = extract32(insn, 3, 2);
3757 op2r = extract32(insn, 0, 3);
3758
3759 if (mos || op || op2r || type > 1) {
3760 unallocated_encoding(s);
3761 return;
3762 }
3763
3764 handle_fp_compare(s, type, rn, rm, opc & 1, opc & 2);
Peter Maydellfaa0ba42013-12-23 23:27:30 +00003765}
3766
3767/* C3.6.23 Floating point conditional compare
3768 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 3 0
3769 * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
3770 * | M | 0 | S | 1 1 1 1 0 | type | 1 | Rm | cond | 0 1 | Rn | op | nzcv |
3771 * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
3772 */
3773static void disas_fp_ccomp(DisasContext *s, uint32_t insn)
3774{
Claudio Fontana513f1d72014-01-04 22:15:51 +00003775 unsigned int mos, type, rm, cond, rn, op, nzcv;
3776 TCGv_i64 tcg_flags;
3777 int label_continue = -1;
3778
3779 mos = extract32(insn, 29, 3);
3780 type = extract32(insn, 22, 2); /* 0 = single, 1 = double */
3781 rm = extract32(insn, 16, 5);
3782 cond = extract32(insn, 12, 4);
3783 rn = extract32(insn, 5, 5);
3784 op = extract32(insn, 4, 1);
3785 nzcv = extract32(insn, 0, 4);
3786
3787 if (mos || type > 1) {
3788 unallocated_encoding(s);
3789 return;
3790 }
3791
3792 if (cond < 0x0e) { /* not always */
3793 int label_match = gen_new_label();
3794 label_continue = gen_new_label();
3795 arm_gen_test_cc(cond, label_match);
3796 /* nomatch: */
3797 tcg_flags = tcg_const_i64(nzcv << 28);
3798 gen_set_nzcv(tcg_flags);
3799 tcg_temp_free_i64(tcg_flags);
3800 tcg_gen_br(label_continue);
3801 gen_set_label(label_match);
3802 }
3803
3804 handle_fp_compare(s, type, rn, rm, false, op);
3805
3806 if (cond < 0x0e) {
3807 gen_set_label(label_continue);
3808 }
Peter Maydellfaa0ba42013-12-23 23:27:30 +00003809}
3810
Claudio Fontana5640ff62014-01-04 22:15:51 +00003811/* copy src FP register to dst FP register; type specifies single or double */
3812static void gen_mov_fp2fp(DisasContext *s, int type, int dst, int src)
3813{
3814 if (type) {
3815 TCGv_i64 v = read_fp_dreg(s, src);
3816 write_fp_dreg(s, dst, v);
3817 tcg_temp_free_i64(v);
3818 } else {
3819 TCGv_i32 v = read_fp_sreg(s, src);
3820 write_fp_sreg(s, dst, v);
3821 tcg_temp_free_i32(v);
3822 }
3823}
3824
Peter Maydellfaa0ba42013-12-23 23:27:30 +00003825/* C3.6.24 Floating point conditional select
3826 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 0
3827 * +---+---+---+-----------+------+---+------+------+-----+------+------+
3828 * | M | 0 | S | 1 1 1 1 0 | type | 1 | Rm | cond | 1 1 | Rn | Rd |
3829 * +---+---+---+-----------+------+---+------+------+-----+------+------+
3830 */
3831static void disas_fp_csel(DisasContext *s, uint32_t insn)
3832{
Claudio Fontana5640ff62014-01-04 22:15:51 +00003833 unsigned int mos, type, rm, cond, rn, rd;
3834 int label_continue = -1;
3835
3836 mos = extract32(insn, 29, 3);
3837 type = extract32(insn, 22, 2); /* 0 = single, 1 = double */
3838 rm = extract32(insn, 16, 5);
3839 cond = extract32(insn, 12, 4);
3840 rn = extract32(insn, 5, 5);
3841 rd = extract32(insn, 0, 5);
3842
3843 if (mos || type > 1) {
3844 unallocated_encoding(s);
3845 return;
3846 }
3847
3848 if (cond < 0x0e) { /* not always */
3849 int label_match = gen_new_label();
3850 label_continue = gen_new_label();
3851 arm_gen_test_cc(cond, label_match);
3852 /* nomatch: */
3853 gen_mov_fp2fp(s, type, rd, rm);
3854 tcg_gen_br(label_continue);
3855 gen_set_label(label_match);
3856 }
3857
3858 gen_mov_fp2fp(s, type, rd, rn);
3859
3860 if (cond < 0x0e) { /* continue */
3861 gen_set_label(label_continue);
3862 }
Peter Maydellfaa0ba42013-12-23 23:27:30 +00003863}
3864
Peter Maydelld9b08482014-01-07 17:19:14 +00003865/* C3.6.25 Floating-point data-processing (1 source) - single precision */
3866static void handle_fp_1src_single(DisasContext *s, int opcode, int rd, int rn)
3867{
3868 TCGv_ptr fpst;
3869 TCGv_i32 tcg_op;
3870 TCGv_i32 tcg_res;
3871
3872 fpst = get_fpstatus_ptr();
3873 tcg_op = read_fp_sreg(s, rn);
3874 tcg_res = tcg_temp_new_i32();
3875
3876 switch (opcode) {
3877 case 0x0: /* FMOV */
3878 tcg_gen_mov_i32(tcg_res, tcg_op);
3879 break;
3880 case 0x1: /* FABS */
3881 gen_helper_vfp_abss(tcg_res, tcg_op);
3882 break;
3883 case 0x2: /* FNEG */
3884 gen_helper_vfp_negs(tcg_res, tcg_op);
3885 break;
3886 case 0x3: /* FSQRT */
3887 gen_helper_vfp_sqrts(tcg_res, tcg_op, cpu_env);
3888 break;
3889 case 0x8: /* FRINTN */
3890 case 0x9: /* FRINTP */
3891 case 0xa: /* FRINTM */
3892 case 0xb: /* FRINTZ */
3893 case 0xc: /* FRINTA */
3894 {
3895 TCGv_i32 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(opcode & 7));
3896
3897 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
3898 gen_helper_rints(tcg_res, tcg_op, fpst);
3899
3900 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
3901 tcg_temp_free_i32(tcg_rmode);
3902 break;
3903 }
3904 case 0xe: /* FRINTX */
3905 gen_helper_rints_exact(tcg_res, tcg_op, fpst);
3906 break;
3907 case 0xf: /* FRINTI */
3908 gen_helper_rints(tcg_res, tcg_op, fpst);
3909 break;
3910 default:
3911 abort();
3912 }
3913
3914 write_fp_sreg(s, rd, tcg_res);
3915
3916 tcg_temp_free_ptr(fpst);
3917 tcg_temp_free_i32(tcg_op);
3918 tcg_temp_free_i32(tcg_res);
3919}
3920
3921/* C3.6.25 Floating-point data-processing (1 source) - double precision */
3922static void handle_fp_1src_double(DisasContext *s, int opcode, int rd, int rn)
3923{
3924 TCGv_ptr fpst;
3925 TCGv_i64 tcg_op;
3926 TCGv_i64 tcg_res;
3927
3928 fpst = get_fpstatus_ptr();
3929 tcg_op = read_fp_dreg(s, rn);
3930 tcg_res = tcg_temp_new_i64();
3931
3932 switch (opcode) {
3933 case 0x0: /* FMOV */
3934 tcg_gen_mov_i64(tcg_res, tcg_op);
3935 break;
3936 case 0x1: /* FABS */
3937 gen_helper_vfp_absd(tcg_res, tcg_op);
3938 break;
3939 case 0x2: /* FNEG */
3940 gen_helper_vfp_negd(tcg_res, tcg_op);
3941 break;
3942 case 0x3: /* FSQRT */
3943 gen_helper_vfp_sqrtd(tcg_res, tcg_op, cpu_env);
3944 break;
3945 case 0x8: /* FRINTN */
3946 case 0x9: /* FRINTP */
3947 case 0xa: /* FRINTM */
3948 case 0xb: /* FRINTZ */
3949 case 0xc: /* FRINTA */
3950 {
3951 TCGv_i32 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(opcode & 7));
3952
3953 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
3954 gen_helper_rintd(tcg_res, tcg_op, fpst);
3955
3956 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
3957 tcg_temp_free_i32(tcg_rmode);
3958 break;
3959 }
3960 case 0xe: /* FRINTX */
3961 gen_helper_rintd_exact(tcg_res, tcg_op, fpst);
3962 break;
3963 case 0xf: /* FRINTI */
3964 gen_helper_rintd(tcg_res, tcg_op, fpst);
3965 break;
3966 default:
3967 abort();
3968 }
3969
3970 write_fp_dreg(s, rd, tcg_res);
3971
3972 tcg_temp_free_ptr(fpst);
3973 tcg_temp_free_i64(tcg_op);
3974 tcg_temp_free_i64(tcg_res);
3975}
3976
Peter Maydell8900aad2014-01-07 17:19:15 +00003977static void handle_fp_fcvt(DisasContext *s, int opcode,
3978 int rd, int rn, int dtype, int ntype)
3979{
3980 switch (ntype) {
3981 case 0x0:
3982 {
3983 TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
3984 if (dtype == 1) {
3985 /* Single to double */
3986 TCGv_i64 tcg_rd = tcg_temp_new_i64();
3987 gen_helper_vfp_fcvtds(tcg_rd, tcg_rn, cpu_env);
3988 write_fp_dreg(s, rd, tcg_rd);
3989 tcg_temp_free_i64(tcg_rd);
3990 } else {
3991 /* Single to half */
3992 TCGv_i32 tcg_rd = tcg_temp_new_i32();
3993 gen_helper_vfp_fcvt_f32_to_f16(tcg_rd, tcg_rn, cpu_env);
3994 /* write_fp_sreg is OK here because top half of tcg_rd is zero */
3995 write_fp_sreg(s, rd, tcg_rd);
3996 tcg_temp_free_i32(tcg_rd);
3997 }
3998 tcg_temp_free_i32(tcg_rn);
3999 break;
4000 }
4001 case 0x1:
4002 {
4003 TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
4004 TCGv_i32 tcg_rd = tcg_temp_new_i32();
4005 if (dtype == 0) {
4006 /* Double to single */
4007 gen_helper_vfp_fcvtsd(tcg_rd, tcg_rn, cpu_env);
4008 } else {
4009 /* Double to half */
4010 gen_helper_vfp_fcvt_f64_to_f16(tcg_rd, tcg_rn, cpu_env);
4011 /* write_fp_sreg is OK here because top half of tcg_rd is zero */
4012 }
4013 write_fp_sreg(s, rd, tcg_rd);
4014 tcg_temp_free_i32(tcg_rd);
4015 tcg_temp_free_i64(tcg_rn);
4016 break;
4017 }
4018 case 0x3:
4019 {
4020 TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
4021 tcg_gen_ext16u_i32(tcg_rn, tcg_rn);
4022 if (dtype == 0) {
4023 /* Half to single */
4024 TCGv_i32 tcg_rd = tcg_temp_new_i32();
4025 gen_helper_vfp_fcvt_f16_to_f32(tcg_rd, tcg_rn, cpu_env);
4026 write_fp_sreg(s, rd, tcg_rd);
4027 tcg_temp_free_i32(tcg_rd);
4028 } else {
4029 /* Half to double */
4030 TCGv_i64 tcg_rd = tcg_temp_new_i64();
4031 gen_helper_vfp_fcvt_f16_to_f64(tcg_rd, tcg_rn, cpu_env);
4032 write_fp_dreg(s, rd, tcg_rd);
4033 tcg_temp_free_i64(tcg_rd);
4034 }
4035 tcg_temp_free_i32(tcg_rn);
4036 break;
4037 }
4038 default:
4039 abort();
4040 }
4041}
4042
Peter Maydellfaa0ba42013-12-23 23:27:30 +00004043/* C3.6.25 Floating point data-processing (1 source)
4044 * 31 30 29 28 24 23 22 21 20 15 14 10 9 5 4 0
4045 * +---+---+---+-----------+------+---+--------+-----------+------+------+
4046 * | M | 0 | S | 1 1 1 1 0 | type | 1 | opcode | 1 0 0 0 0 | Rn | Rd |
4047 * +---+---+---+-----------+------+---+--------+-----------+------+------+
4048 */
4049static void disas_fp_1src(DisasContext *s, uint32_t insn)
4050{
Peter Maydelld9b08482014-01-07 17:19:14 +00004051 int type = extract32(insn, 22, 2);
4052 int opcode = extract32(insn, 15, 6);
4053 int rn = extract32(insn, 5, 5);
4054 int rd = extract32(insn, 0, 5);
4055
4056 switch (opcode) {
4057 case 0x4: case 0x5: case 0x7:
Peter Maydell8900aad2014-01-07 17:19:15 +00004058 {
Peter Maydelld9b08482014-01-07 17:19:14 +00004059 /* FCVT between half, single and double precision */
Peter Maydell8900aad2014-01-07 17:19:15 +00004060 int dtype = extract32(opcode, 0, 2);
4061 if (type == 2 || dtype == type) {
4062 unallocated_encoding(s);
4063 return;
4064 }
4065 handle_fp_fcvt(s, opcode, rd, rn, dtype, type);
Peter Maydelld9b08482014-01-07 17:19:14 +00004066 break;
Peter Maydell8900aad2014-01-07 17:19:15 +00004067 }
Peter Maydelld9b08482014-01-07 17:19:14 +00004068 case 0x0 ... 0x3:
4069 case 0x8 ... 0xc:
4070 case 0xe ... 0xf:
4071 /* 32-to-32 and 64-to-64 ops */
4072 switch (type) {
4073 case 0:
4074 handle_fp_1src_single(s, opcode, rd, rn);
4075 break;
4076 case 1:
4077 handle_fp_1src_double(s, opcode, rd, rn);
4078 break;
4079 default:
4080 unallocated_encoding(s);
4081 }
4082 break;
4083 default:
4084 unallocated_encoding(s);
4085 break;
4086 }
Peter Maydellfaa0ba42013-12-23 23:27:30 +00004087}
4088
Alexander Grafec73d2e2014-01-04 22:15:50 +00004089/* C3.6.26 Floating-point data-processing (2 source) - single precision */
4090static void handle_fp_2src_single(DisasContext *s, int opcode,
4091 int rd, int rn, int rm)
4092{
4093 TCGv_i32 tcg_op1;
4094 TCGv_i32 tcg_op2;
4095 TCGv_i32 tcg_res;
4096 TCGv_ptr fpst;
4097
4098 tcg_res = tcg_temp_new_i32();
4099 fpst = get_fpstatus_ptr();
4100 tcg_op1 = read_fp_sreg(s, rn);
4101 tcg_op2 = read_fp_sreg(s, rm);
4102
4103 switch (opcode) {
4104 case 0x0: /* FMUL */
4105 gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
4106 break;
4107 case 0x1: /* FDIV */
4108 gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
4109 break;
4110 case 0x2: /* FADD */
4111 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
4112 break;
4113 case 0x3: /* FSUB */
4114 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
4115 break;
4116 case 0x4: /* FMAX */
4117 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
4118 break;
4119 case 0x5: /* FMIN */
4120 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
4121 break;
4122 case 0x6: /* FMAXNM */
4123 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
4124 break;
4125 case 0x7: /* FMINNM */
4126 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
4127 break;
4128 case 0x8: /* FNMUL */
4129 gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
4130 gen_helper_vfp_negs(tcg_res, tcg_res);
4131 break;
4132 }
4133
4134 write_fp_sreg(s, rd, tcg_res);
4135
4136 tcg_temp_free_ptr(fpst);
4137 tcg_temp_free_i32(tcg_op1);
4138 tcg_temp_free_i32(tcg_op2);
4139 tcg_temp_free_i32(tcg_res);
4140}
4141
4142/* C3.6.26 Floating-point data-processing (2 source) - double precision */
4143static void handle_fp_2src_double(DisasContext *s, int opcode,
4144 int rd, int rn, int rm)
4145{
4146 TCGv_i64 tcg_op1;
4147 TCGv_i64 tcg_op2;
4148 TCGv_i64 tcg_res;
4149 TCGv_ptr fpst;
4150
4151 tcg_res = tcg_temp_new_i64();
4152 fpst = get_fpstatus_ptr();
4153 tcg_op1 = read_fp_dreg(s, rn);
4154 tcg_op2 = read_fp_dreg(s, rm);
4155
4156 switch (opcode) {
4157 case 0x0: /* FMUL */
4158 gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
4159 break;
4160 case 0x1: /* FDIV */
4161 gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
4162 break;
4163 case 0x2: /* FADD */
4164 gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
4165 break;
4166 case 0x3: /* FSUB */
4167 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
4168 break;
4169 case 0x4: /* FMAX */
4170 gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
4171 break;
4172 case 0x5: /* FMIN */
4173 gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
4174 break;
4175 case 0x6: /* FMAXNM */
4176 gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
4177 break;
4178 case 0x7: /* FMINNM */
4179 gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
4180 break;
4181 case 0x8: /* FNMUL */
4182 gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
4183 gen_helper_vfp_negd(tcg_res, tcg_res);
4184 break;
4185 }
4186
4187 write_fp_dreg(s, rd, tcg_res);
4188
4189 tcg_temp_free_ptr(fpst);
4190 tcg_temp_free_i64(tcg_op1);
4191 tcg_temp_free_i64(tcg_op2);
4192 tcg_temp_free_i64(tcg_res);
4193}
4194
Peter Maydellfaa0ba42013-12-23 23:27:30 +00004195/* C3.6.26 Floating point data-processing (2 source)
4196 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 0
4197 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
4198 * | M | 0 | S | 1 1 1 1 0 | type | 1 | Rm | opcode | 1 0 | Rn | Rd |
4199 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
4200 */
4201static void disas_fp_2src(DisasContext *s, uint32_t insn)
4202{
Alexander Grafec73d2e2014-01-04 22:15:50 +00004203 int type = extract32(insn, 22, 2);
4204 int rd = extract32(insn, 0, 5);
4205 int rn = extract32(insn, 5, 5);
4206 int rm = extract32(insn, 16, 5);
4207 int opcode = extract32(insn, 12, 4);
4208
4209 if (opcode > 8) {
4210 unallocated_encoding(s);
4211 return;
4212 }
4213
4214 switch (type) {
4215 case 0:
4216 handle_fp_2src_single(s, opcode, rd, rn, rm);
4217 break;
4218 case 1:
4219 handle_fp_2src_double(s, opcode, rd, rn, rm);
4220 break;
4221 default:
4222 unallocated_encoding(s);
4223 }
Peter Maydellfaa0ba42013-12-23 23:27:30 +00004224}
4225
Alexander Graf6a306672014-01-04 22:15:50 +00004226/* C3.6.27 Floating-point data-processing (3 source) - single precision */
4227static void handle_fp_3src_single(DisasContext *s, bool o0, bool o1,
4228 int rd, int rn, int rm, int ra)
4229{
4230 TCGv_i32 tcg_op1, tcg_op2, tcg_op3;
4231 TCGv_i32 tcg_res = tcg_temp_new_i32();
4232 TCGv_ptr fpst = get_fpstatus_ptr();
4233
4234 tcg_op1 = read_fp_sreg(s, rn);
4235 tcg_op2 = read_fp_sreg(s, rm);
4236 tcg_op3 = read_fp_sreg(s, ra);
4237
4238 /* These are fused multiply-add, and must be done as one
4239 * floating point operation with no rounding between the
4240 * multiplication and addition steps.
4241 * NB that doing the negations here as separate steps is
4242 * correct : an input NaN should come out with its sign bit
4243 * flipped if it is a negated-input.
4244 */
4245 if (o1 == true) {
4246 gen_helper_vfp_negs(tcg_op3, tcg_op3);
4247 }
4248
4249 if (o0 != o1) {
4250 gen_helper_vfp_negs(tcg_op1, tcg_op1);
4251 }
4252
4253 gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
4254
4255 write_fp_sreg(s, rd, tcg_res);
4256
4257 tcg_temp_free_ptr(fpst);
4258 tcg_temp_free_i32(tcg_op1);
4259 tcg_temp_free_i32(tcg_op2);
4260 tcg_temp_free_i32(tcg_op3);
4261 tcg_temp_free_i32(tcg_res);
4262}
4263
4264/* C3.6.27 Floating-point data-processing (3 source) - double precision */
4265static void handle_fp_3src_double(DisasContext *s, bool o0, bool o1,
4266 int rd, int rn, int rm, int ra)
4267{
4268 TCGv_i64 tcg_op1, tcg_op2, tcg_op3;
4269 TCGv_i64 tcg_res = tcg_temp_new_i64();
4270 TCGv_ptr fpst = get_fpstatus_ptr();
4271
4272 tcg_op1 = read_fp_dreg(s, rn);
4273 tcg_op2 = read_fp_dreg(s, rm);
4274 tcg_op3 = read_fp_dreg(s, ra);
4275
4276 /* These are fused multiply-add, and must be done as one
4277 * floating point operation with no rounding between the
4278 * multiplication and addition steps.
4279 * NB that doing the negations here as separate steps is
4280 * correct : an input NaN should come out with its sign bit
4281 * flipped if it is a negated-input.
4282 */
4283 if (o1 == true) {
4284 gen_helper_vfp_negd(tcg_op3, tcg_op3);
4285 }
4286
4287 if (o0 != o1) {
4288 gen_helper_vfp_negd(tcg_op1, tcg_op1);
4289 }
4290
4291 gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
4292
4293 write_fp_dreg(s, rd, tcg_res);
4294
4295 tcg_temp_free_ptr(fpst);
4296 tcg_temp_free_i64(tcg_op1);
4297 tcg_temp_free_i64(tcg_op2);
4298 tcg_temp_free_i64(tcg_op3);
4299 tcg_temp_free_i64(tcg_res);
4300}
4301
Peter Maydellfaa0ba42013-12-23 23:27:30 +00004302/* C3.6.27 Floating point data-processing (3 source)
4303 * 31 30 29 28 24 23 22 21 20 16 15 14 10 9 5 4 0
4304 * +---+---+---+-----------+------+----+------+----+------+------+------+
4305 * | M | 0 | S | 1 1 1 1 1 | type | o1 | Rm | o0 | Ra | Rn | Rd |
4306 * +---+---+---+-----------+------+----+------+----+------+------+------+
4307 */
4308static void disas_fp_3src(DisasContext *s, uint32_t insn)
4309{
Alexander Graf6a306672014-01-04 22:15:50 +00004310 int type = extract32(insn, 22, 2);
4311 int rd = extract32(insn, 0, 5);
4312 int rn = extract32(insn, 5, 5);
4313 int ra = extract32(insn, 10, 5);
4314 int rm = extract32(insn, 16, 5);
4315 bool o0 = extract32(insn, 15, 1);
4316 bool o1 = extract32(insn, 21, 1);
4317
4318 switch (type) {
4319 case 0:
4320 handle_fp_3src_single(s, o0, o1, rd, rn, rm, ra);
4321 break;
4322 case 1:
4323 handle_fp_3src_double(s, o0, o1, rd, rn, rm, ra);
4324 break;
4325 default:
4326 unallocated_encoding(s);
4327 }
Peter Maydellfaa0ba42013-12-23 23:27:30 +00004328}
4329
4330/* C3.6.28 Floating point immediate
4331 * 31 30 29 28 24 23 22 21 20 13 12 10 9 5 4 0
4332 * +---+---+---+-----------+------+---+------------+-------+------+------+
4333 * | M | 0 | S | 1 1 1 1 0 | type | 1 | imm8 | 1 0 0 | imm5 | Rd |
4334 * +---+---+---+-----------+------+---+------------+-------+------+------+
4335 */
4336static void disas_fp_imm(DisasContext *s, uint32_t insn)
4337{
Alexander Graf6163f862014-01-04 22:15:50 +00004338 int rd = extract32(insn, 0, 5);
4339 int imm8 = extract32(insn, 13, 8);
4340 int is_double = extract32(insn, 22, 2);
4341 uint64_t imm;
4342 TCGv_i64 tcg_res;
4343
4344 if (is_double > 1) {
4345 unallocated_encoding(s);
4346 return;
4347 }
4348
4349 /* The imm8 encodes the sign bit, enough bits to represent
4350 * an exponent in the range 01....1xx to 10....0xx,
4351 * and the most significant 4 bits of the mantissa; see
4352 * VFPExpandImm() in the v8 ARM ARM.
4353 */
4354 if (is_double) {
4355 imm = (extract32(imm8, 7, 1) ? 0x8000 : 0) |
4356 (extract32(imm8, 6, 1) ? 0x3fc0 : 0x4000) |
4357 extract32(imm8, 0, 6);
4358 imm <<= 48;
4359 } else {
4360 imm = (extract32(imm8, 7, 1) ? 0x8000 : 0) |
4361 (extract32(imm8, 6, 1) ? 0x3e00 : 0x4000) |
4362 (extract32(imm8, 0, 6) << 3);
4363 imm <<= 16;
4364 }
4365
4366 tcg_res = tcg_const_i64(imm);
4367 write_fp_dreg(s, rd, tcg_res);
4368 tcg_temp_free_i64(tcg_res);
Peter Maydellfaa0ba42013-12-23 23:27:30 +00004369}
4370
Alexander Graf52a1f6a2014-01-07 17:19:14 +00004371/* Handle floating point <=> fixed point conversions. Note that we can
4372 * also deal with fp <=> integer conversions as a special case (scale == 64)
4373 * OPTME: consider handling that special case specially or at least skipping
4374 * the call to scalbn in the helpers for zero shifts.
4375 */
4376static void handle_fpfpcvt(DisasContext *s, int rd, int rn, int opcode,
4377 bool itof, int rmode, int scale, int sf, int type)
4378{
4379 bool is_signed = !(opcode & 1);
4380 bool is_double = type;
4381 TCGv_ptr tcg_fpstatus;
4382 TCGv_i32 tcg_shift;
4383
4384 tcg_fpstatus = get_fpstatus_ptr();
4385
4386 tcg_shift = tcg_const_i32(64 - scale);
4387
4388 if (itof) {
4389 TCGv_i64 tcg_int = cpu_reg(s, rn);
4390 if (!sf) {
4391 TCGv_i64 tcg_extend = new_tmp_a64(s);
4392
4393 if (is_signed) {
4394 tcg_gen_ext32s_i64(tcg_extend, tcg_int);
4395 } else {
4396 tcg_gen_ext32u_i64(tcg_extend, tcg_int);
4397 }
4398
4399 tcg_int = tcg_extend;
4400 }
4401
4402 if (is_double) {
4403 TCGv_i64 tcg_double = tcg_temp_new_i64();
4404 if (is_signed) {
4405 gen_helper_vfp_sqtod(tcg_double, tcg_int,
4406 tcg_shift, tcg_fpstatus);
4407 } else {
4408 gen_helper_vfp_uqtod(tcg_double, tcg_int,
4409 tcg_shift, tcg_fpstatus);
4410 }
4411 write_fp_dreg(s, rd, tcg_double);
4412 tcg_temp_free_i64(tcg_double);
4413 } else {
4414 TCGv_i32 tcg_single = tcg_temp_new_i32();
4415 if (is_signed) {
4416 gen_helper_vfp_sqtos(tcg_single, tcg_int,
4417 tcg_shift, tcg_fpstatus);
4418 } else {
4419 gen_helper_vfp_uqtos(tcg_single, tcg_int,
4420 tcg_shift, tcg_fpstatus);
4421 }
4422 write_fp_sreg(s, rd, tcg_single);
4423 tcg_temp_free_i32(tcg_single);
4424 }
4425 } else {
4426 TCGv_i64 tcg_int = cpu_reg(s, rd);
4427 TCGv_i32 tcg_rmode;
4428
4429 if (extract32(opcode, 2, 1)) {
4430 /* There are too many rounding modes to all fit into rmode,
4431 * so FCVTA[US] is a special case.
4432 */
4433 rmode = FPROUNDING_TIEAWAY;
4434 }
4435
4436 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rmode));
4437
4438 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
4439
4440 if (is_double) {
4441 TCGv_i64 tcg_double = read_fp_dreg(s, rn);
4442 if (is_signed) {
4443 if (!sf) {
4444 gen_helper_vfp_tosld(tcg_int, tcg_double,
4445 tcg_shift, tcg_fpstatus);
4446 } else {
4447 gen_helper_vfp_tosqd(tcg_int, tcg_double,
4448 tcg_shift, tcg_fpstatus);
4449 }
4450 } else {
4451 if (!sf) {
4452 gen_helper_vfp_tould(tcg_int, tcg_double,
4453 tcg_shift, tcg_fpstatus);
4454 } else {
4455 gen_helper_vfp_touqd(tcg_int, tcg_double,
4456 tcg_shift, tcg_fpstatus);
4457 }
4458 }
4459 tcg_temp_free_i64(tcg_double);
4460 } else {
4461 TCGv_i32 tcg_single = read_fp_sreg(s, rn);
4462 if (sf) {
4463 if (is_signed) {
4464 gen_helper_vfp_tosqs(tcg_int, tcg_single,
4465 tcg_shift, tcg_fpstatus);
4466 } else {
4467 gen_helper_vfp_touqs(tcg_int, tcg_single,
4468 tcg_shift, tcg_fpstatus);
4469 }
4470 } else {
4471 TCGv_i32 tcg_dest = tcg_temp_new_i32();
4472 if (is_signed) {
4473 gen_helper_vfp_tosls(tcg_dest, tcg_single,
4474 tcg_shift, tcg_fpstatus);
4475 } else {
4476 gen_helper_vfp_touls(tcg_dest, tcg_single,
4477 tcg_shift, tcg_fpstatus);
4478 }
4479 tcg_gen_extu_i32_i64(tcg_int, tcg_dest);
4480 tcg_temp_free_i32(tcg_dest);
4481 }
4482 tcg_temp_free_i32(tcg_single);
4483 }
4484
4485 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
4486 tcg_temp_free_i32(tcg_rmode);
4487
4488 if (!sf) {
4489 tcg_gen_ext32u_i64(tcg_int, tcg_int);
4490 }
4491 }
4492
4493 tcg_temp_free_ptr(tcg_fpstatus);
4494 tcg_temp_free_i32(tcg_shift);
4495}
4496
Peter Maydellfaa0ba42013-12-23 23:27:30 +00004497/* C3.6.29 Floating point <-> fixed point conversions
4498 * 31 30 29 28 24 23 22 21 20 19 18 16 15 10 9 5 4 0
4499 * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
4500 * | sf | 0 | S | 1 1 1 1 0 | type | 0 | rmode | opcode | scale | Rn | Rd |
4501 * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
4502 */
4503static void disas_fp_fixed_conv(DisasContext *s, uint32_t insn)
4504{
Alexander Graf52a1f6a2014-01-07 17:19:14 +00004505 int rd = extract32(insn, 0, 5);
4506 int rn = extract32(insn, 5, 5);
4507 int scale = extract32(insn, 10, 6);
4508 int opcode = extract32(insn, 16, 3);
4509 int rmode = extract32(insn, 19, 2);
4510 int type = extract32(insn, 22, 2);
4511 bool sbit = extract32(insn, 29, 1);
4512 bool sf = extract32(insn, 31, 1);
4513 bool itof;
4514
4515 if (sbit || (type > 1)
4516 || (!sf && scale < 32)) {
4517 unallocated_encoding(s);
4518 return;
4519 }
4520
4521 switch ((rmode << 3) | opcode) {
4522 case 0x2: /* SCVTF */
4523 case 0x3: /* UCVTF */
4524 itof = true;
4525 break;
4526 case 0x18: /* FCVTZS */
4527 case 0x19: /* FCVTZU */
4528 itof = false;
4529 break;
4530 default:
4531 unallocated_encoding(s);
4532 return;
4533 }
4534
4535 handle_fpfpcvt(s, rd, rn, opcode, itof, FPROUNDING_ZERO, scale, sf, type);
Peter Maydellfaa0ba42013-12-23 23:27:30 +00004536}
4537
Peter Maydellce5458e2013-12-23 23:27:31 +00004538static void handle_fmov(DisasContext *s, int rd, int rn, int type, bool itof)
4539{
4540 /* FMOV: gpr to or from float, double, or top half of quad fp reg,
4541 * without conversion.
4542 */
4543
4544 if (itof) {
Peter Maydellce5458e2013-12-23 23:27:31 +00004545 TCGv_i64 tcg_rn = cpu_reg(s, rn);
4546
4547 switch (type) {
4548 case 0:
4549 {
4550 /* 32 bit */
4551 TCGv_i64 tmp = tcg_temp_new_i64();
4552 tcg_gen_ext32u_i64(tmp, tcg_rn);
Peter Maydelle2f90562014-01-04 22:15:49 +00004553 tcg_gen_st_i64(tmp, cpu_env, fp_reg_offset(rd, MO_64));
Peter Maydellce5458e2013-12-23 23:27:31 +00004554 tcg_gen_movi_i64(tmp, 0);
Peter Maydelle2f90562014-01-04 22:15:49 +00004555 tcg_gen_st_i64(tmp, cpu_env, fp_reg_hi_offset(rd));
Peter Maydellce5458e2013-12-23 23:27:31 +00004556 tcg_temp_free_i64(tmp);
4557 break;
4558 }
4559 case 1:
4560 {
4561 /* 64 bit */
4562 TCGv_i64 tmp = tcg_const_i64(0);
Peter Maydelle2f90562014-01-04 22:15:49 +00004563 tcg_gen_st_i64(tcg_rn, cpu_env, fp_reg_offset(rd, MO_64));
4564 tcg_gen_st_i64(tmp, cpu_env, fp_reg_hi_offset(rd));
Peter Maydellce5458e2013-12-23 23:27:31 +00004565 tcg_temp_free_i64(tmp);
4566 break;
4567 }
4568 case 2:
4569 /* 64 bit to top half. */
Peter Maydelle2f90562014-01-04 22:15:49 +00004570 tcg_gen_st_i64(tcg_rn, cpu_env, fp_reg_hi_offset(rd));
Peter Maydellce5458e2013-12-23 23:27:31 +00004571 break;
4572 }
4573 } else {
Peter Maydellce5458e2013-12-23 23:27:31 +00004574 TCGv_i64 tcg_rd = cpu_reg(s, rd);
4575
4576 switch (type) {
4577 case 0:
4578 /* 32 bit */
Peter Maydelle2f90562014-01-04 22:15:49 +00004579 tcg_gen_ld32u_i64(tcg_rd, cpu_env, fp_reg_offset(rn, MO_32));
4580 break;
4581 case 1:
4582 /* 64 bit */
4583 tcg_gen_ld_i64(tcg_rd, cpu_env, fp_reg_offset(rn, MO_64));
Peter Maydellce5458e2013-12-23 23:27:31 +00004584 break;
4585 case 2:
4586 /* 64 bits from top half */
Peter Maydelle2f90562014-01-04 22:15:49 +00004587 tcg_gen_ld_i64(tcg_rd, cpu_env, fp_reg_hi_offset(rn));
Peter Maydellce5458e2013-12-23 23:27:31 +00004588 break;
4589 }
4590 }
4591}
4592
Peter Maydellfaa0ba42013-12-23 23:27:30 +00004593/* C3.6.30 Floating point <-> integer conversions
4594 * 31 30 29 28 24 23 22 21 20 19 18 16 15 10 9 5 4 0
4595 * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
Will Newtonc436d402014-01-07 17:19:14 +00004596 * | 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 +00004597 * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
4598 */
4599static void disas_fp_int_conv(DisasContext *s, uint32_t insn)
4600{
Peter Maydellce5458e2013-12-23 23:27:31 +00004601 int rd = extract32(insn, 0, 5);
4602 int rn = extract32(insn, 5, 5);
4603 int opcode = extract32(insn, 16, 3);
4604 int rmode = extract32(insn, 19, 2);
4605 int type = extract32(insn, 22, 2);
4606 bool sbit = extract32(insn, 29, 1);
4607 bool sf = extract32(insn, 31, 1);
4608
Will Newtonc436d402014-01-07 17:19:14 +00004609 if (sbit) {
4610 unallocated_encoding(s);
4611 return;
4612 }
4613
4614 if (opcode > 5) {
Peter Maydellce5458e2013-12-23 23:27:31 +00004615 /* FMOV */
4616 bool itof = opcode & 1;
4617
Will Newtonc436d402014-01-07 17:19:14 +00004618 if (rmode >= 2) {
4619 unallocated_encoding(s);
4620 return;
4621 }
4622
Peter Maydellce5458e2013-12-23 23:27:31 +00004623 switch (sf << 3 | type << 1 | rmode) {
4624 case 0x0: /* 32 bit */
4625 case 0xa: /* 64 bit */
4626 case 0xd: /* 64 bit to top half of quad */
4627 break;
4628 default:
4629 /* all other sf/type/rmode combinations are invalid */
4630 unallocated_encoding(s);
4631 break;
4632 }
4633
4634 handle_fmov(s, rd, rn, type, itof);
4635 } else {
4636 /* actual FP conversions */
Will Newtonc436d402014-01-07 17:19:14 +00004637 bool itof = extract32(opcode, 1, 1);
4638
4639 if (type > 1 || (rmode != 0 && opcode > 1)) {
4640 unallocated_encoding(s);
4641 return;
4642 }
4643
4644 handle_fpfpcvt(s, rd, rn, opcode, itof, rmode, 64, sf, type);
Peter Maydellce5458e2013-12-23 23:27:31 +00004645 }
Peter Maydellfaa0ba42013-12-23 23:27:30 +00004646}
4647
4648/* FP-specific subcases of table C3-6 (SIMD and FP data processing)
4649 * 31 30 29 28 25 24 0
4650 * +---+---+---+---------+-----------------------------+
4651 * | | 0 | | 1 1 1 1 | |
4652 * +---+---+---+---------+-----------------------------+
4653 */
4654static void disas_data_proc_fp(DisasContext *s, uint32_t insn)
4655{
4656 if (extract32(insn, 24, 1)) {
4657 /* Floating point data-processing (3 source) */
4658 disas_fp_3src(s, insn);
4659 } else if (extract32(insn, 21, 1) == 0) {
4660 /* Floating point to fixed point conversions */
4661 disas_fp_fixed_conv(s, insn);
4662 } else {
4663 switch (extract32(insn, 10, 2)) {
4664 case 1:
4665 /* Floating point conditional compare */
4666 disas_fp_ccomp(s, insn);
4667 break;
4668 case 2:
4669 /* Floating point data-processing (2 source) */
4670 disas_fp_2src(s, insn);
4671 break;
4672 case 3:
4673 /* Floating point conditional select */
4674 disas_fp_csel(s, insn);
4675 break;
4676 case 0:
4677 switch (ctz32(extract32(insn, 12, 4))) {
4678 case 0: /* [15:12] == xxx1 */
4679 /* Floating point immediate */
4680 disas_fp_imm(s, insn);
4681 break;
4682 case 1: /* [15:12] == xx10 */
4683 /* Floating point compare */
4684 disas_fp_compare(s, insn);
4685 break;
4686 case 2: /* [15:12] == x100 */
4687 /* Floating point data-processing (1 source) */
4688 disas_fp_1src(s, insn);
4689 break;
4690 case 3: /* [15:12] == 1000 */
4691 unallocated_encoding(s);
4692 break;
4693 default: /* [15:12] == 0000 */
4694 /* Floating point <-> integer conversions */
4695 disas_fp_int_conv(s, insn);
4696 break;
4697 }
4698 break;
4699 }
4700 }
4701}
4702
Peter Maydellc4ee3382014-01-23 14:37:08 +00004703static void do_ext64(DisasContext *s, TCGv_i64 tcg_left, TCGv_i64 tcg_right,
4704 int pos)
4705{
4706 /* Extract 64 bits from the middle of two concatenated 64 bit
4707 * vector register slices left:right. The extracted bits start
4708 * at 'pos' bits into the right (least significant) side.
4709 * We return the result in tcg_right, and guarantee not to
4710 * trash tcg_left.
4711 */
4712 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
4713 assert(pos > 0 && pos < 64);
4714
4715 tcg_gen_shri_i64(tcg_right, tcg_right, pos);
4716 tcg_gen_shli_i64(tcg_tmp, tcg_left, 64 - pos);
4717 tcg_gen_or_i64(tcg_right, tcg_right, tcg_tmp);
4718
4719 tcg_temp_free_i64(tcg_tmp);
4720}
4721
Alex Bennée94ad8702014-01-23 14:37:07 +00004722/* C3.6.1 EXT
4723 * 31 30 29 24 23 22 21 20 16 15 14 11 10 9 5 4 0
4724 * +---+---+-------------+-----+---+------+---+------+---+------+------+
4725 * | 0 | Q | 1 0 1 1 1 0 | op2 | 0 | Rm | 0 | imm4 | 0 | Rn | Rd |
4726 * +---+---+-------------+-----+---+------+---+------+---+------+------+
4727 */
4728static void disas_simd_ext(DisasContext *s, uint32_t insn)
4729{
Peter Maydellc4ee3382014-01-23 14:37:08 +00004730 int is_q = extract32(insn, 30, 1);
4731 int op2 = extract32(insn, 22, 2);
4732 int imm4 = extract32(insn, 11, 4);
4733 int rm = extract32(insn, 16, 5);
4734 int rn = extract32(insn, 5, 5);
4735 int rd = extract32(insn, 0, 5);
4736 int pos = imm4 << 3;
4737 TCGv_i64 tcg_resl, tcg_resh;
4738
4739 if (op2 != 0 || (!is_q && extract32(imm4, 3, 1))) {
4740 unallocated_encoding(s);
4741 return;
4742 }
4743
4744 tcg_resh = tcg_temp_new_i64();
4745 tcg_resl = tcg_temp_new_i64();
4746
4747 /* Vd gets bits starting at pos bits into Vm:Vn. This is
4748 * either extracting 128 bits from a 128:128 concatenation, or
4749 * extracting 64 bits from a 64:64 concatenation.
4750 */
4751 if (!is_q) {
4752 read_vec_element(s, tcg_resl, rn, 0, MO_64);
4753 if (pos != 0) {
4754 read_vec_element(s, tcg_resh, rm, 0, MO_64);
4755 do_ext64(s, tcg_resh, tcg_resl, pos);
4756 }
4757 tcg_gen_movi_i64(tcg_resh, 0);
4758 } else {
4759 TCGv_i64 tcg_hh;
4760 typedef struct {
4761 int reg;
4762 int elt;
4763 } EltPosns;
4764 EltPosns eltposns[] = { {rn, 0}, {rn, 1}, {rm, 0}, {rm, 1} };
4765 EltPosns *elt = eltposns;
4766
4767 if (pos >= 64) {
4768 elt++;
4769 pos -= 64;
4770 }
4771
4772 read_vec_element(s, tcg_resl, elt->reg, elt->elt, MO_64);
4773 elt++;
4774 read_vec_element(s, tcg_resh, elt->reg, elt->elt, MO_64);
4775 elt++;
4776 if (pos != 0) {
4777 do_ext64(s, tcg_resh, tcg_resl, pos);
4778 tcg_hh = tcg_temp_new_i64();
4779 read_vec_element(s, tcg_hh, elt->reg, elt->elt, MO_64);
4780 do_ext64(s, tcg_hh, tcg_resh, pos);
4781 tcg_temp_free_i64(tcg_hh);
4782 }
4783 }
4784
4785 write_vec_element(s, tcg_resl, rd, 0, MO_64);
4786 tcg_temp_free_i64(tcg_resl);
4787 write_vec_element(s, tcg_resh, rd, 1, MO_64);
4788 tcg_temp_free_i64(tcg_resh);
Alex Bennée94ad8702014-01-23 14:37:07 +00004789}
4790
4791/* C3.6.2 TBL/TBX
4792 * 31 30 29 24 23 22 21 20 16 15 14 13 12 11 10 9 5 4 0
4793 * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
4794 * | 0 | Q | 0 0 1 1 1 0 | op2 | 0 | Rm | 0 | len | op | 0 0 | Rn | Rd |
4795 * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
4796 */
4797static void disas_simd_tb(DisasContext *s, uint32_t insn)
4798{
Michael Matzdc3250e2014-01-23 14:37:08 +00004799 int op2 = extract32(insn, 22, 2);
4800 int is_q = extract32(insn, 30, 1);
4801 int rm = extract32(insn, 16, 5);
4802 int rn = extract32(insn, 5, 5);
4803 int rd = extract32(insn, 0, 5);
4804 int is_tblx = extract32(insn, 12, 1);
4805 int len = extract32(insn, 13, 2);
4806 TCGv_i64 tcg_resl, tcg_resh, tcg_idx;
4807 TCGv_i32 tcg_regno, tcg_numregs;
4808
4809 if (op2 != 0) {
4810 unallocated_encoding(s);
4811 return;
4812 }
4813
4814 /* This does a table lookup: for every byte element in the input
4815 * we index into a table formed from up to four vector registers,
4816 * and then the output is the result of the lookups. Our helper
4817 * function does the lookup operation for a single 64 bit part of
4818 * the input.
4819 */
4820 tcg_resl = tcg_temp_new_i64();
4821 tcg_resh = tcg_temp_new_i64();
4822
4823 if (is_tblx) {
4824 read_vec_element(s, tcg_resl, rd, 0, MO_64);
4825 } else {
4826 tcg_gen_movi_i64(tcg_resl, 0);
4827 }
4828 if (is_tblx && is_q) {
4829 read_vec_element(s, tcg_resh, rd, 1, MO_64);
4830 } else {
4831 tcg_gen_movi_i64(tcg_resh, 0);
4832 }
4833
4834 tcg_idx = tcg_temp_new_i64();
4835 tcg_regno = tcg_const_i32(rn);
4836 tcg_numregs = tcg_const_i32(len + 1);
4837 read_vec_element(s, tcg_idx, rm, 0, MO_64);
4838 gen_helper_simd_tbl(tcg_resl, cpu_env, tcg_resl, tcg_idx,
4839 tcg_regno, tcg_numregs);
4840 if (is_q) {
4841 read_vec_element(s, tcg_idx, rm, 1, MO_64);
4842 gen_helper_simd_tbl(tcg_resh, cpu_env, tcg_resh, tcg_idx,
4843 tcg_regno, tcg_numregs);
4844 }
4845 tcg_temp_free_i64(tcg_idx);
4846 tcg_temp_free_i32(tcg_regno);
4847 tcg_temp_free_i32(tcg_numregs);
4848
4849 write_vec_element(s, tcg_resl, rd, 0, MO_64);
4850 tcg_temp_free_i64(tcg_resl);
4851 write_vec_element(s, tcg_resh, rd, 1, MO_64);
4852 tcg_temp_free_i64(tcg_resh);
Alex Bennée94ad8702014-01-23 14:37:07 +00004853}
4854
4855/* C3.6.3 ZIP/UZP/TRN
4856 * 31 30 29 24 23 22 21 20 16 15 14 12 11 10 9 5 4 0
4857 * +---+---+-------------+------+---+------+---+------------------+------+
4858 * | 0 | Q | 0 0 1 1 1 0 | size | 0 | Rm | 0 | opc | 1 0 | Rn | Rd |
4859 * +---+---+-------------+------+---+------+---+------------------+------+
4860 */
4861static void disas_simd_zip_trn(DisasContext *s, uint32_t insn)
4862{
Michael Matz19c3a9d2014-01-23 14:37:08 +00004863 int rd = extract32(insn, 0, 5);
4864 int rn = extract32(insn, 5, 5);
4865 int rm = extract32(insn, 16, 5);
4866 int size = extract32(insn, 22, 2);
4867 /* opc field bits [1:0] indicate ZIP/UZP/TRN;
4868 * bit 2 indicates 1 vs 2 variant of the insn.
4869 */
4870 int opcode = extract32(insn, 12, 2);
4871 bool part = extract32(insn, 14, 1);
4872 bool is_q = extract32(insn, 30, 1);
4873 int esize = 8 << size;
4874 int i, ofs;
4875 int datasize = is_q ? 128 : 64;
4876 int elements = datasize / esize;
4877 TCGv_i64 tcg_res, tcg_resl, tcg_resh;
4878
4879 if (opcode == 0 || (size == 3 && !is_q)) {
4880 unallocated_encoding(s);
4881 return;
4882 }
4883
4884 tcg_resl = tcg_const_i64(0);
4885 tcg_resh = tcg_const_i64(0);
4886 tcg_res = tcg_temp_new_i64();
4887
4888 for (i = 0; i < elements; i++) {
4889 switch (opcode) {
4890 case 1: /* UZP1/2 */
4891 {
4892 int midpoint = elements / 2;
4893 if (i < midpoint) {
4894 read_vec_element(s, tcg_res, rn, 2 * i + part, size);
4895 } else {
4896 read_vec_element(s, tcg_res, rm,
4897 2 * (i - midpoint) + part, size);
4898 }
4899 break;
4900 }
4901 case 2: /* TRN1/2 */
4902 if (i & 1) {
4903 read_vec_element(s, tcg_res, rm, (i & ~1) + part, size);
4904 } else {
4905 read_vec_element(s, tcg_res, rn, (i & ~1) + part, size);
4906 }
4907 break;
4908 case 3: /* ZIP1/2 */
4909 {
4910 int base = part * elements / 2;
4911 if (i & 1) {
4912 read_vec_element(s, tcg_res, rm, base + (i >> 1), size);
4913 } else {
4914 read_vec_element(s, tcg_res, rn, base + (i >> 1), size);
4915 }
4916 break;
4917 }
4918 default:
4919 g_assert_not_reached();
4920 }
4921
4922 ofs = i * esize;
4923 if (ofs < 64) {
4924 tcg_gen_shli_i64(tcg_res, tcg_res, ofs);
4925 tcg_gen_or_i64(tcg_resl, tcg_resl, tcg_res);
4926 } else {
4927 tcg_gen_shli_i64(tcg_res, tcg_res, ofs - 64);
4928 tcg_gen_or_i64(tcg_resh, tcg_resh, tcg_res);
4929 }
4930 }
4931
4932 tcg_temp_free_i64(tcg_res);
4933
4934 write_vec_element(s, tcg_resl, rd, 0, MO_64);
4935 tcg_temp_free_i64(tcg_resl);
4936 write_vec_element(s, tcg_resh, rd, 1, MO_64);
4937 tcg_temp_free_i64(tcg_resh);
Alex Bennée94ad8702014-01-23 14:37:07 +00004938}
4939
Michael Matz20287612014-01-23 14:37:08 +00004940static void do_minmaxop(DisasContext *s, TCGv_i32 tcg_elt1, TCGv_i32 tcg_elt2,
4941 int opc, bool is_min, TCGv_ptr fpst)
4942{
4943 /* Helper function for disas_simd_across_lanes: do a single precision
4944 * min/max operation on the specified two inputs,
4945 * and return the result in tcg_elt1.
4946 */
4947 if (opc == 0xc) {
4948 if (is_min) {
4949 gen_helper_vfp_minnums(tcg_elt1, tcg_elt1, tcg_elt2, fpst);
4950 } else {
4951 gen_helper_vfp_maxnums(tcg_elt1, tcg_elt1, tcg_elt2, fpst);
4952 }
4953 } else {
4954 assert(opc == 0xf);
4955 if (is_min) {
4956 gen_helper_vfp_mins(tcg_elt1, tcg_elt1, tcg_elt2, fpst);
4957 } else {
4958 gen_helper_vfp_maxs(tcg_elt1, tcg_elt1, tcg_elt2, fpst);
4959 }
4960 }
4961}
4962
Alex Bennée94ad8702014-01-23 14:37:07 +00004963/* C3.6.4 AdvSIMD across lanes
4964 * 31 30 29 28 24 23 22 21 17 16 12 11 10 9 5 4 0
4965 * +---+---+---+-----------+------+-----------+--------+-----+------+------+
4966 * | 0 | Q | U | 0 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 | Rn | Rd |
4967 * +---+---+---+-----------+------+-----------+--------+-----+------+------+
4968 */
4969static void disas_simd_across_lanes(DisasContext *s, uint32_t insn)
4970{
Michael Matz20287612014-01-23 14:37:08 +00004971 int rd = extract32(insn, 0, 5);
4972 int rn = extract32(insn, 5, 5);
4973 int size = extract32(insn, 22, 2);
4974 int opcode = extract32(insn, 12, 5);
4975 bool is_q = extract32(insn, 30, 1);
4976 bool is_u = extract32(insn, 29, 1);
4977 bool is_fp = false;
4978 bool is_min = false;
4979 int esize;
4980 int elements;
4981 int i;
4982 TCGv_i64 tcg_res, tcg_elt;
4983
4984 switch (opcode) {
4985 case 0x1b: /* ADDV */
4986 if (is_u) {
4987 unallocated_encoding(s);
4988 return;
4989 }
4990 /* fall through */
4991 case 0x3: /* SADDLV, UADDLV */
4992 case 0xa: /* SMAXV, UMAXV */
4993 case 0x1a: /* SMINV, UMINV */
4994 if (size == 3 || (size == 2 && !is_q)) {
4995 unallocated_encoding(s);
4996 return;
4997 }
4998 break;
4999 case 0xc: /* FMAXNMV, FMINNMV */
5000 case 0xf: /* FMAXV, FMINV */
5001 if (!is_u || !is_q || extract32(size, 0, 1)) {
5002 unallocated_encoding(s);
5003 return;
5004 }
5005 /* Bit 1 of size field encodes min vs max, and actual size is always
5006 * 32 bits: adjust the size variable so following code can rely on it
5007 */
5008 is_min = extract32(size, 1, 1);
5009 is_fp = true;
5010 size = 2;
5011 break;
5012 default:
5013 unallocated_encoding(s);
5014 return;
5015 }
5016
5017 esize = 8 << size;
5018 elements = (is_q ? 128 : 64) / esize;
5019
5020 tcg_res = tcg_temp_new_i64();
5021 tcg_elt = tcg_temp_new_i64();
5022
5023 /* These instructions operate across all lanes of a vector
5024 * to produce a single result. We can guarantee that a 64
5025 * bit intermediate is sufficient:
5026 * + for [US]ADDLV the maximum element size is 32 bits, and
5027 * the result type is 64 bits
5028 * + for FMAX*V, FMIN*V, ADDV the intermediate type is the
5029 * same as the element size, which is 32 bits at most
5030 * For the integer operations we can choose to work at 64
5031 * or 32 bits and truncate at the end; for simplicity
5032 * we use 64 bits always. The floating point
5033 * ops do require 32 bit intermediates, though.
5034 */
5035 if (!is_fp) {
5036 read_vec_element(s, tcg_res, rn, 0, size | (is_u ? 0 : MO_SIGN));
5037
5038 for (i = 1; i < elements; i++) {
5039 read_vec_element(s, tcg_elt, rn, i, size | (is_u ? 0 : MO_SIGN));
5040
5041 switch (opcode) {
5042 case 0x03: /* SADDLV / UADDLV */
5043 case 0x1b: /* ADDV */
5044 tcg_gen_add_i64(tcg_res, tcg_res, tcg_elt);
5045 break;
5046 case 0x0a: /* SMAXV / UMAXV */
5047 tcg_gen_movcond_i64(is_u ? TCG_COND_GEU : TCG_COND_GE,
5048 tcg_res,
5049 tcg_res, tcg_elt, tcg_res, tcg_elt);
5050 break;
5051 case 0x1a: /* SMINV / UMINV */
5052 tcg_gen_movcond_i64(is_u ? TCG_COND_LEU : TCG_COND_LE,
5053 tcg_res,
5054 tcg_res, tcg_elt, tcg_res, tcg_elt);
5055 break;
5056 break;
5057 default:
5058 g_assert_not_reached();
5059 }
5060
5061 }
5062 } else {
5063 /* Floating point ops which work on 32 bit (single) intermediates.
5064 * Note that correct NaN propagation requires that we do these
5065 * operations in exactly the order specified by the pseudocode.
5066 */
5067 TCGv_i32 tcg_elt1 = tcg_temp_new_i32();
5068 TCGv_i32 tcg_elt2 = tcg_temp_new_i32();
5069 TCGv_i32 tcg_elt3 = tcg_temp_new_i32();
5070 TCGv_ptr fpst = get_fpstatus_ptr();
5071
5072 assert(esize == 32);
5073 assert(elements == 4);
5074
5075 read_vec_element(s, tcg_elt, rn, 0, MO_32);
5076 tcg_gen_trunc_i64_i32(tcg_elt1, tcg_elt);
5077 read_vec_element(s, tcg_elt, rn, 1, MO_32);
5078 tcg_gen_trunc_i64_i32(tcg_elt2, tcg_elt);
5079
5080 do_minmaxop(s, tcg_elt1, tcg_elt2, opcode, is_min, fpst);
5081
5082 read_vec_element(s, tcg_elt, rn, 2, MO_32);
5083 tcg_gen_trunc_i64_i32(tcg_elt2, tcg_elt);
5084 read_vec_element(s, tcg_elt, rn, 3, MO_32);
5085 tcg_gen_trunc_i64_i32(tcg_elt3, tcg_elt);
5086
5087 do_minmaxop(s, tcg_elt2, tcg_elt3, opcode, is_min, fpst);
5088
5089 do_minmaxop(s, tcg_elt1, tcg_elt2, opcode, is_min, fpst);
5090
5091 tcg_gen_extu_i32_i64(tcg_res, tcg_elt1);
5092 tcg_temp_free_i32(tcg_elt1);
5093 tcg_temp_free_i32(tcg_elt2);
5094 tcg_temp_free_i32(tcg_elt3);
5095 tcg_temp_free_ptr(fpst);
5096 }
5097
5098 tcg_temp_free_i64(tcg_elt);
5099
5100 /* Now truncate the result to the width required for the final output */
5101 if (opcode == 0x03) {
5102 /* SADDLV, UADDLV: result is 2*esize */
5103 size++;
5104 }
5105
5106 switch (size) {
5107 case 0:
5108 tcg_gen_ext8u_i64(tcg_res, tcg_res);
5109 break;
5110 case 1:
5111 tcg_gen_ext16u_i64(tcg_res, tcg_res);
5112 break;
5113 case 2:
5114 tcg_gen_ext32u_i64(tcg_res, tcg_res);
5115 break;
5116 case 3:
5117 break;
5118 default:
5119 g_assert_not_reached();
5120 }
5121
5122 write_fp_dreg(s, rd, tcg_res);
5123 tcg_temp_free_i64(tcg_res);
Alex Bennée94ad8702014-01-23 14:37:07 +00005124}
5125
Alex Bennée5725d0d2014-01-23 14:37:08 +00005126/* C6.3.31 DUP (Element, Vector)
5127 *
5128 * 31 30 29 21 20 16 15 10 9 5 4 0
5129 * +---+---+-------------------+--------+-------------+------+------+
5130 * | 0 | Q | 0 0 1 1 1 0 0 0 0 | imm5 | 0 0 0 0 0 1 | Rn | Rd |
5131 * +---+---+-------------------+--------+-------------+------+------+
5132 *
5133 * size: encoded in imm5 (see ARM ARM LowestSetBit())
5134 */
5135static void handle_simd_dupe(DisasContext *s, int is_q, int rd, int rn,
5136 int imm5)
5137{
5138 int size = ctz32(imm5);
5139 int esize = 8 << size;
5140 int elements = (is_q ? 128 : 64) / esize;
5141 int index, i;
5142 TCGv_i64 tmp;
5143
5144 if (size > 3 || (size == 3 && !is_q)) {
5145 unallocated_encoding(s);
5146 return;
5147 }
5148
5149 index = imm5 >> (size + 1);
5150
5151 tmp = tcg_temp_new_i64();
5152 read_vec_element(s, tmp, rn, index, size);
5153
5154 for (i = 0; i < elements; i++) {
5155 write_vec_element(s, tmp, rd, i, size);
5156 }
5157
5158 if (!is_q) {
5159 clear_vec_high(s, rd);
5160 }
5161
5162 tcg_temp_free_i64(tmp);
5163}
5164
Peter Maydellf9469f92014-01-23 14:37:09 +00005165/* C6.3.31 DUP (element, scalar)
5166 * 31 21 20 16 15 10 9 5 4 0
5167 * +-----------------------+--------+-------------+------+------+
5168 * | 0 1 0 1 1 1 1 0 0 0 0 | imm5 | 0 0 0 0 0 1 | Rn | Rd |
5169 * +-----------------------+--------+-------------+------+------+
5170 */
5171static void handle_simd_dupes(DisasContext *s, int rd, int rn,
5172 int imm5)
5173{
5174 int size = ctz32(imm5);
5175 int index;
5176 TCGv_i64 tmp;
5177
5178 if (size > 3) {
5179 unallocated_encoding(s);
5180 return;
5181 }
5182
5183 index = imm5 >> (size + 1);
5184
5185 /* This instruction just extracts the specified element and
5186 * zero-extends it into the bottom of the destination register.
5187 */
5188 tmp = tcg_temp_new_i64();
5189 read_vec_element(s, tmp, rn, index, size);
5190 write_fp_dreg(s, rd, tmp);
5191 tcg_temp_free_i64(tmp);
5192}
5193
Alex Bennée5725d0d2014-01-23 14:37:08 +00005194/* C6.3.32 DUP (General)
5195 *
5196 * 31 30 29 21 20 16 15 10 9 5 4 0
5197 * +---+---+-------------------+--------+-------------+------+------+
5198 * | 0 | Q | 0 0 1 1 1 0 0 0 0 | imm5 | 0 0 0 0 1 1 | Rn | Rd |
5199 * +---+---+-------------------+--------+-------------+------+------+
5200 *
5201 * size: encoded in imm5 (see ARM ARM LowestSetBit())
5202 */
5203static void handle_simd_dupg(DisasContext *s, int is_q, int rd, int rn,
5204 int imm5)
5205{
5206 int size = ctz32(imm5);
5207 int esize = 8 << size;
5208 int elements = (is_q ? 128 : 64)/esize;
5209 int i = 0;
5210
5211 if (size > 3 || ((size == 3) && !is_q)) {
5212 unallocated_encoding(s);
5213 return;
5214 }
5215 for (i = 0; i < elements; i++) {
5216 write_vec_element(s, cpu_reg(s, rn), rd, i, size);
5217 }
5218 if (!is_q) {
5219 clear_vec_high(s, rd);
5220 }
5221}
5222
5223/* C6.3.150 INS (Element)
5224 *
5225 * 31 21 20 16 15 14 11 10 9 5 4 0
5226 * +-----------------------+--------+------------+---+------+------+
5227 * | 0 1 1 0 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 | Rn | Rd |
5228 * +-----------------------+--------+------------+---+------+------+
5229 *
5230 * size: encoded in imm5 (see ARM ARM LowestSetBit())
5231 * index: encoded in imm5<4:size+1>
5232 */
5233static void handle_simd_inse(DisasContext *s, int rd, int rn,
5234 int imm4, int imm5)
5235{
5236 int size = ctz32(imm5);
5237 int src_index, dst_index;
5238 TCGv_i64 tmp;
5239
5240 if (size > 3) {
5241 unallocated_encoding(s);
5242 return;
5243 }
5244 dst_index = extract32(imm5, 1+size, 5);
5245 src_index = extract32(imm4, size, 4);
5246
5247 tmp = tcg_temp_new_i64();
5248
5249 read_vec_element(s, tmp, rn, src_index, size);
5250 write_vec_element(s, tmp, rd, dst_index, size);
5251
5252 tcg_temp_free_i64(tmp);
5253}
5254
5255
5256/* C6.3.151 INS (General)
5257 *
5258 * 31 21 20 16 15 10 9 5 4 0
5259 * +-----------------------+--------+-------------+------+------+
5260 * | 0 1 0 0 1 1 1 0 0 0 0 | imm5 | 0 0 0 1 1 1 | Rn | Rd |
5261 * +-----------------------+--------+-------------+------+------+
5262 *
5263 * size: encoded in imm5 (see ARM ARM LowestSetBit())
5264 * index: encoded in imm5<4:size+1>
5265 */
5266static void handle_simd_insg(DisasContext *s, int rd, int rn, int imm5)
5267{
5268 int size = ctz32(imm5);
5269 int idx;
5270
5271 if (size > 3) {
5272 unallocated_encoding(s);
5273 return;
5274 }
5275
5276 idx = extract32(imm5, 1 + size, 4 - size);
5277 write_vec_element(s, cpu_reg(s, rn), rd, idx, size);
5278}
5279
5280/*
5281 * C6.3.321 UMOV (General)
5282 * C6.3.237 SMOV (General)
5283 *
5284 * 31 30 29 21 20 16 15 12 10 9 5 4 0
5285 * +---+---+-------------------+--------+-------------+------+------+
5286 * | 0 | Q | 0 0 1 1 1 0 0 0 0 | imm5 | 0 0 1 U 1 1 | Rn | Rd |
5287 * +---+---+-------------------+--------+-------------+------+------+
5288 *
5289 * U: unsigned when set
5290 * size: encoded in imm5 (see ARM ARM LowestSetBit())
5291 */
5292static void handle_simd_umov_smov(DisasContext *s, int is_q, int is_signed,
5293 int rn, int rd, int imm5)
5294{
5295 int size = ctz32(imm5);
5296 int element;
5297 TCGv_i64 tcg_rd;
5298
5299 /* Check for UnallocatedEncodings */
5300 if (is_signed) {
5301 if (size > 2 || (size == 2 && !is_q)) {
5302 unallocated_encoding(s);
5303 return;
5304 }
5305 } else {
5306 if (size > 3
5307 || (size < 3 && is_q)
5308 || (size == 3 && !is_q)) {
5309 unallocated_encoding(s);
5310 return;
5311 }
5312 }
5313 element = extract32(imm5, 1+size, 4);
5314
5315 tcg_rd = cpu_reg(s, rd);
5316 read_vec_element(s, tcg_rd, rn, element, size | (is_signed ? MO_SIGN : 0));
5317 if (is_signed && !is_q) {
5318 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
5319 }
5320}
5321
Alex Bennée94ad8702014-01-23 14:37:07 +00005322/* C3.6.5 AdvSIMD copy
5323 * 31 30 29 28 21 20 16 15 14 11 10 9 5 4 0
5324 * +---+---+----+-----------------+------+---+------+---+------+------+
5325 * | 0 | Q | op | 0 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 | Rn | Rd |
5326 * +---+---+----+-----------------+------+---+------+---+------+------+
5327 */
5328static void disas_simd_copy(DisasContext *s, uint32_t insn)
5329{
Alex Bennée5725d0d2014-01-23 14:37:08 +00005330 int rd = extract32(insn, 0, 5);
5331 int rn = extract32(insn, 5, 5);
5332 int imm4 = extract32(insn, 11, 4);
5333 int op = extract32(insn, 29, 1);
5334 int is_q = extract32(insn, 30, 1);
5335 int imm5 = extract32(insn, 16, 5);
5336
5337 if (op) {
5338 if (is_q) {
5339 /* INS (element) */
5340 handle_simd_inse(s, rd, rn, imm4, imm5);
5341 } else {
5342 unallocated_encoding(s);
5343 }
5344 } else {
5345 switch (imm4) {
5346 case 0:
5347 /* DUP (element - vector) */
5348 handle_simd_dupe(s, is_q, rd, rn, imm5);
5349 break;
5350 case 1:
5351 /* DUP (general) */
5352 handle_simd_dupg(s, is_q, rd, rn, imm5);
5353 break;
5354 case 3:
5355 if (is_q) {
5356 /* INS (general) */
5357 handle_simd_insg(s, rd, rn, imm5);
5358 } else {
5359 unallocated_encoding(s);
5360 }
5361 break;
5362 case 5:
5363 case 7:
5364 /* UMOV/SMOV (is_q indicates 32/64; imm4 indicates signedness) */
5365 handle_simd_umov_smov(s, is_q, (imm4 == 5), rn, rd, imm5);
5366 break;
5367 default:
5368 unallocated_encoding(s);
5369 break;
5370 }
5371 }
Alex Bennée94ad8702014-01-23 14:37:07 +00005372}
5373
5374/* C3.6.6 AdvSIMD modified immediate
5375 * 31 30 29 28 19 18 16 15 12 11 10 9 5 4 0
5376 * +---+---+----+---------------------+-----+-------+----+---+-------+------+
5377 * | 0 | Q | op | 0 1 1 1 1 0 0 0 0 0 | abc | cmode | o2 | 1 | defgh | Rd |
5378 * +---+---+----+---------------------+-----+-------+----+---+-------+------+
Alex Bennéeee5dfda2014-01-23 14:37:09 +00005379 *
5380 * There are a number of operations that can be carried out here:
5381 * MOVI - move (shifted) imm into register
5382 * MVNI - move inverted (shifted) imm into register
5383 * ORR - bitwise OR of (shifted) imm with register
5384 * BIC - bitwise clear of (shifted) imm with register
Alex Bennée94ad8702014-01-23 14:37:07 +00005385 */
5386static void disas_simd_mod_imm(DisasContext *s, uint32_t insn)
5387{
Alex Bennéeee5dfda2014-01-23 14:37:09 +00005388 int rd = extract32(insn, 0, 5);
5389 int cmode = extract32(insn, 12, 4);
5390 int cmode_3_1 = extract32(cmode, 1, 3);
5391 int cmode_0 = extract32(cmode, 0, 1);
5392 int o2 = extract32(insn, 11, 1);
5393 uint64_t abcdefgh = extract32(insn, 5, 5) | (extract32(insn, 16, 3) << 5);
5394 bool is_neg = extract32(insn, 29, 1);
5395 bool is_q = extract32(insn, 30, 1);
5396 uint64_t imm = 0;
5397 TCGv_i64 tcg_rd, tcg_imm;
5398 int i;
5399
5400 if (o2 != 0 || ((cmode == 0xf) && is_neg && !is_q)) {
5401 unallocated_encoding(s);
5402 return;
5403 }
5404
5405 /* See AdvSIMDExpandImm() in ARM ARM */
5406 switch (cmode_3_1) {
5407 case 0: /* Replicate(Zeros(24):imm8, 2) */
5408 case 1: /* Replicate(Zeros(16):imm8:Zeros(8), 2) */
5409 case 2: /* Replicate(Zeros(8):imm8:Zeros(16), 2) */
5410 case 3: /* Replicate(imm8:Zeros(24), 2) */
5411 {
5412 int shift = cmode_3_1 * 8;
5413 imm = bitfield_replicate(abcdefgh << shift, 32);
5414 break;
5415 }
5416 case 4: /* Replicate(Zeros(8):imm8, 4) */
5417 case 5: /* Replicate(imm8:Zeros(8), 4) */
5418 {
5419 int shift = (cmode_3_1 & 0x1) * 8;
5420 imm = bitfield_replicate(abcdefgh << shift, 16);
5421 break;
5422 }
5423 case 6:
5424 if (cmode_0) {
5425 /* Replicate(Zeros(8):imm8:Ones(16), 2) */
5426 imm = (abcdefgh << 16) | 0xffff;
5427 } else {
5428 /* Replicate(Zeros(16):imm8:Ones(8), 2) */
5429 imm = (abcdefgh << 8) | 0xff;
5430 }
5431 imm = bitfield_replicate(imm, 32);
5432 break;
5433 case 7:
5434 if (!cmode_0 && !is_neg) {
5435 imm = bitfield_replicate(abcdefgh, 8);
5436 } else if (!cmode_0 && is_neg) {
5437 int i;
5438 imm = 0;
5439 for (i = 0; i < 8; i++) {
5440 if ((abcdefgh) & (1 << i)) {
5441 imm |= 0xffULL << (i * 8);
5442 }
5443 }
5444 } else if (cmode_0) {
5445 if (is_neg) {
5446 imm = (abcdefgh & 0x3f) << 48;
5447 if (abcdefgh & 0x80) {
5448 imm |= 0x8000000000000000ULL;
5449 }
5450 if (abcdefgh & 0x40) {
5451 imm |= 0x3fc0000000000000ULL;
5452 } else {
5453 imm |= 0x4000000000000000ULL;
5454 }
5455 } else {
5456 imm = (abcdefgh & 0x3f) << 19;
5457 if (abcdefgh & 0x80) {
5458 imm |= 0x80000000;
5459 }
5460 if (abcdefgh & 0x40) {
5461 imm |= 0x3e000000;
5462 } else {
5463 imm |= 0x40000000;
5464 }
5465 imm |= (imm << 32);
5466 }
5467 }
5468 break;
5469 }
5470
5471 if (cmode_3_1 != 7 && is_neg) {
5472 imm = ~imm;
5473 }
5474
5475 tcg_imm = tcg_const_i64(imm);
5476 tcg_rd = new_tmp_a64(s);
5477
5478 for (i = 0; i < 2; i++) {
5479 int foffs = i ? fp_reg_hi_offset(rd) : fp_reg_offset(rd, MO_64);
5480
5481 if (i == 1 && !is_q) {
5482 /* non-quad ops clear high half of vector */
5483 tcg_gen_movi_i64(tcg_rd, 0);
5484 } else if ((cmode & 0x9) == 0x1 || (cmode & 0xd) == 0x9) {
5485 tcg_gen_ld_i64(tcg_rd, cpu_env, foffs);
5486 if (is_neg) {
5487 /* AND (BIC) */
5488 tcg_gen_and_i64(tcg_rd, tcg_rd, tcg_imm);
5489 } else {
5490 /* ORR */
5491 tcg_gen_or_i64(tcg_rd, tcg_rd, tcg_imm);
5492 }
5493 } else {
5494 /* MOVI */
5495 tcg_gen_mov_i64(tcg_rd, tcg_imm);
5496 }
5497 tcg_gen_st_i64(tcg_rd, cpu_env, foffs);
5498 }
5499
5500 tcg_temp_free_i64(tcg_imm);
Alex Bennée94ad8702014-01-23 14:37:07 +00005501}
5502
5503/* C3.6.7 AdvSIMD scalar copy
5504 * 31 30 29 28 21 20 16 15 14 11 10 9 5 4 0
5505 * +-----+----+-----------------+------+---+------+---+------+------+
5506 * | 0 1 | op | 1 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 | Rn | Rd |
5507 * +-----+----+-----------------+------+---+------+---+------+------+
5508 */
5509static void disas_simd_scalar_copy(DisasContext *s, uint32_t insn)
5510{
Peter Maydellf9469f92014-01-23 14:37:09 +00005511 int rd = extract32(insn, 0, 5);
5512 int rn = extract32(insn, 5, 5);
5513 int imm4 = extract32(insn, 11, 4);
5514 int imm5 = extract32(insn, 16, 5);
5515 int op = extract32(insn, 29, 1);
5516
5517 if (op != 0 || imm4 != 0) {
5518 unallocated_encoding(s);
5519 return;
5520 }
5521
5522 /* DUP (element, scalar) */
5523 handle_simd_dupes(s, rd, rn, imm5);
Alex Bennée94ad8702014-01-23 14:37:07 +00005524}
5525
5526/* C3.6.8 AdvSIMD scalar pairwise
5527 * 31 30 29 28 24 23 22 21 17 16 12 11 10 9 5 4 0
5528 * +-----+---+-----------+------+-----------+--------+-----+------+------+
5529 * | 0 1 | U | 1 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 | Rn | Rd |
5530 * +-----+---+-----------+------+-----------+--------+-----+------+------+
5531 */
5532static void disas_simd_scalar_pairwise(DisasContext *s, uint32_t insn)
5533{
Peter Maydellcec2bec2014-01-25 21:14:52 +00005534 int u = extract32(insn, 29, 1);
5535 int size = extract32(insn, 22, 2);
5536 int opcode = extract32(insn, 12, 5);
5537 int rn = extract32(insn, 5, 5);
5538 int rd = extract32(insn, 0, 5);
5539 TCGv_ptr fpst;
5540
5541 /* For some ops (the FP ones), size[1] is part of the encoding.
5542 * For ADDP strictly it is not but size[1] is always 1 for valid
5543 * encodings.
5544 */
5545 opcode |= (extract32(size, 1, 1) << 5);
5546
5547 switch (opcode) {
5548 case 0x3b: /* ADDP */
5549 if (u || size != 3) {
5550 unallocated_encoding(s);
5551 return;
5552 }
5553 TCGV_UNUSED_PTR(fpst);
5554 break;
5555 case 0xc: /* FMAXNMP */
5556 case 0xd: /* FADDP */
5557 case 0xf: /* FMAXP */
5558 case 0x2c: /* FMINNMP */
5559 case 0x2f: /* FMINP */
5560 /* FP op, size[0] is 32 or 64 bit */
5561 if (!u) {
5562 unallocated_encoding(s);
5563 return;
5564 }
5565 size = extract32(size, 0, 1) ? 3 : 2;
5566 fpst = get_fpstatus_ptr();
5567 break;
5568 default:
5569 unallocated_encoding(s);
5570 return;
5571 }
5572
5573 if (size == 3) {
5574 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
5575 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
5576 TCGv_i64 tcg_res = tcg_temp_new_i64();
5577
5578 read_vec_element(s, tcg_op1, rn, 0, MO_64);
5579 read_vec_element(s, tcg_op2, rn, 1, MO_64);
5580
5581 switch (opcode) {
5582 case 0x3b: /* ADDP */
5583 tcg_gen_add_i64(tcg_res, tcg_op1, tcg_op2);
5584 break;
5585 case 0xc: /* FMAXNMP */
5586 gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
5587 break;
5588 case 0xd: /* FADDP */
5589 gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
5590 break;
5591 case 0xf: /* FMAXP */
5592 gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
5593 break;
5594 case 0x2c: /* FMINNMP */
5595 gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
5596 break;
5597 case 0x2f: /* FMINP */
5598 gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
5599 break;
5600 default:
5601 g_assert_not_reached();
5602 }
5603
5604 write_fp_dreg(s, rd, tcg_res);
5605
5606 tcg_temp_free_i64(tcg_op1);
5607 tcg_temp_free_i64(tcg_op2);
5608 tcg_temp_free_i64(tcg_res);
5609 } else {
5610 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
5611 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
5612 TCGv_i32 tcg_res = tcg_temp_new_i32();
5613
5614 read_vec_element_i32(s, tcg_op1, rn, 0, MO_32);
5615 read_vec_element_i32(s, tcg_op2, rn, 1, MO_32);
5616
5617 switch (opcode) {
5618 case 0xc: /* FMAXNMP */
5619 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
5620 break;
5621 case 0xd: /* FADDP */
5622 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
5623 break;
5624 case 0xf: /* FMAXP */
5625 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
5626 break;
5627 case 0x2c: /* FMINNMP */
5628 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
5629 break;
5630 case 0x2f: /* FMINP */
5631 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
5632 break;
5633 default:
5634 g_assert_not_reached();
5635 }
5636
5637 write_fp_sreg(s, rd, tcg_res);
5638
5639 tcg_temp_free_i32(tcg_op1);
5640 tcg_temp_free_i32(tcg_op2);
5641 tcg_temp_free_i32(tcg_res);
5642 }
5643
5644 if (!TCGV_IS_UNUSED_PTR(fpst)) {
5645 tcg_temp_free_ptr(fpst);
5646 }
Alex Bennée94ad8702014-01-23 14:37:07 +00005647}
5648
Alex Bennée4db2b1e2014-01-09 18:13:58 +00005649/*
5650 * Common SSHR[RA]/USHR[RA] - Shift right (optional rounding/accumulate)
5651 *
5652 * This code is handles the common shifting code and is used by both
5653 * the vector and scalar code.
5654 */
5655static void handle_shri_with_rndacc(TCGv_i64 tcg_res, TCGv_i64 tcg_src,
5656 TCGv_i64 tcg_rnd, bool accumulate,
5657 bool is_u, int size, int shift)
5658{
5659 bool extended_result = false;
5660 bool round = !TCGV_IS_UNUSED_I64(tcg_rnd);
5661 int ext_lshift = 0;
5662 TCGv_i64 tcg_src_hi;
5663
5664 if (round && size == 3) {
5665 extended_result = true;
5666 ext_lshift = 64 - shift;
5667 tcg_src_hi = tcg_temp_new_i64();
5668 } else if (shift == 64) {
5669 if (!accumulate && is_u) {
5670 /* result is zero */
5671 tcg_gen_movi_i64(tcg_res, 0);
5672 return;
5673 }
5674 }
5675
5676 /* Deal with the rounding step */
5677 if (round) {
5678 if (extended_result) {
5679 TCGv_i64 tcg_zero = tcg_const_i64(0);
5680 if (!is_u) {
5681 /* take care of sign extending tcg_res */
5682 tcg_gen_sari_i64(tcg_src_hi, tcg_src, 63);
5683 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
5684 tcg_src, tcg_src_hi,
5685 tcg_rnd, tcg_zero);
5686 } else {
5687 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
5688 tcg_src, tcg_zero,
5689 tcg_rnd, tcg_zero);
5690 }
5691 tcg_temp_free_i64(tcg_zero);
5692 } else {
5693 tcg_gen_add_i64(tcg_src, tcg_src, tcg_rnd);
5694 }
5695 }
5696
5697 /* Now do the shift right */
5698 if (round && extended_result) {
5699 /* extended case, >64 bit precision required */
5700 if (ext_lshift == 0) {
5701 /* special case, only high bits matter */
5702 tcg_gen_mov_i64(tcg_src, tcg_src_hi);
5703 } else {
5704 tcg_gen_shri_i64(tcg_src, tcg_src, shift);
5705 tcg_gen_shli_i64(tcg_src_hi, tcg_src_hi, ext_lshift);
5706 tcg_gen_or_i64(tcg_src, tcg_src, tcg_src_hi);
5707 }
5708 } else {
5709 if (is_u) {
5710 if (shift == 64) {
5711 /* essentially shifting in 64 zeros */
5712 tcg_gen_movi_i64(tcg_src, 0);
5713 } else {
5714 tcg_gen_shri_i64(tcg_src, tcg_src, shift);
5715 }
5716 } else {
5717 if (shift == 64) {
5718 /* effectively extending the sign-bit */
5719 tcg_gen_sari_i64(tcg_src, tcg_src, 63);
5720 } else {
5721 tcg_gen_sari_i64(tcg_src, tcg_src, shift);
5722 }
5723 }
5724 }
5725
5726 if (accumulate) {
5727 tcg_gen_add_i64(tcg_res, tcg_res, tcg_src);
5728 } else {
5729 tcg_gen_mov_i64(tcg_res, tcg_src);
5730 }
5731
5732 if (extended_result) {
5733 tcg_temp_free_i64(tcg_src_hi);
5734 }
5735}
5736
5737/* Common SHL/SLI - Shift left with an optional insert */
5738static void handle_shli_with_ins(TCGv_i64 tcg_res, TCGv_i64 tcg_src,
5739 bool insert, int shift)
5740{
5741 if (insert) { /* SLI */
5742 tcg_gen_deposit_i64(tcg_res, tcg_res, tcg_src, shift, 64 - shift);
5743 } else { /* SHL */
5744 tcg_gen_shli_i64(tcg_res, tcg_src, shift);
5745 }
5746}
5747
5748/* SSHR[RA]/USHR[RA] - Scalar shift right (optional rounding/accumulate) */
5749static void handle_scalar_simd_shri(DisasContext *s,
5750 bool is_u, int immh, int immb,
5751 int opcode, int rn, int rd)
5752{
5753 const int size = 3;
5754 int immhb = immh << 3 | immb;
5755 int shift = 2 * (8 << size) - immhb;
5756 bool accumulate = false;
5757 bool round = false;
5758 TCGv_i64 tcg_rn;
5759 TCGv_i64 tcg_rd;
5760 TCGv_i64 tcg_round;
5761
5762 if (!extract32(immh, 3, 1)) {
5763 unallocated_encoding(s);
5764 return;
5765 }
5766
5767 switch (opcode) {
5768 case 0x02: /* SSRA / USRA (accumulate) */
5769 accumulate = true;
5770 break;
5771 case 0x04: /* SRSHR / URSHR (rounding) */
5772 round = true;
5773 break;
5774 case 0x06: /* SRSRA / URSRA (accum + rounding) */
5775 accumulate = round = true;
5776 break;
5777 }
5778
5779 if (round) {
5780 uint64_t round_const = 1ULL << (shift - 1);
5781 tcg_round = tcg_const_i64(round_const);
5782 } else {
5783 TCGV_UNUSED_I64(tcg_round);
5784 }
5785
5786 tcg_rn = read_fp_dreg(s, rn);
5787 tcg_rd = accumulate ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
5788
5789 handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
5790 accumulate, is_u, size, shift);
5791
5792 write_fp_dreg(s, rd, tcg_rd);
5793
5794 tcg_temp_free_i64(tcg_rn);
5795 tcg_temp_free_i64(tcg_rd);
5796 if (round) {
5797 tcg_temp_free_i64(tcg_round);
5798 }
5799}
5800
5801/* SHL/SLI - Scalar shift left */
5802static void handle_scalar_simd_shli(DisasContext *s, bool insert,
5803 int immh, int immb, int opcode,
5804 int rn, int rd)
5805{
5806 int size = 32 - clz32(immh) - 1;
5807 int immhb = immh << 3 | immb;
5808 int shift = immhb - (8 << size);
5809 TCGv_i64 tcg_rn = new_tmp_a64(s);
5810 TCGv_i64 tcg_rd = new_tmp_a64(s);
5811
5812 if (!extract32(immh, 3, 1)) {
5813 unallocated_encoding(s);
5814 return;
5815 }
5816
5817 tcg_rn = read_fp_dreg(s, rn);
5818 tcg_rd = insert ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
5819
5820 handle_shli_with_ins(tcg_rd, tcg_rn, insert, shift);
5821
5822 write_fp_dreg(s, rd, tcg_rd);
5823
5824 tcg_temp_free_i64(tcg_rn);
5825 tcg_temp_free_i64(tcg_rd);
5826}
5827
Alex Bennée94ad8702014-01-23 14:37:07 +00005828/* C3.6.9 AdvSIMD scalar shift by immediate
5829 * 31 30 29 28 23 22 19 18 16 15 11 10 9 5 4 0
5830 * +-----+---+-------------+------+------+--------+---+------+------+
5831 * | 0 1 | U | 1 1 1 1 1 0 | immh | immb | opcode | 1 | Rn | Rd |
5832 * +-----+---+-------------+------+------+--------+---+------+------+
Alex Bennée4db2b1e2014-01-09 18:13:58 +00005833 *
5834 * This is the scalar version so it works on a fixed sized registers
Alex Bennée94ad8702014-01-23 14:37:07 +00005835 */
5836static void disas_simd_scalar_shift_imm(DisasContext *s, uint32_t insn)
5837{
Alex Bennée4db2b1e2014-01-09 18:13:58 +00005838 int rd = extract32(insn, 0, 5);
5839 int rn = extract32(insn, 5, 5);
5840 int opcode = extract32(insn, 11, 5);
5841 int immb = extract32(insn, 16, 3);
5842 int immh = extract32(insn, 19, 4);
5843 bool is_u = extract32(insn, 29, 1);
5844
5845 switch (opcode) {
5846 case 0x00: /* SSHR / USHR */
5847 case 0x02: /* SSRA / USRA */
5848 case 0x04: /* SRSHR / URSHR */
5849 case 0x06: /* SRSRA / URSRA */
5850 handle_scalar_simd_shri(s, is_u, immh, immb, opcode, rn, rd);
5851 break;
5852 case 0x0a: /* SHL / SLI */
5853 handle_scalar_simd_shli(s, is_u, immh, immb, opcode, rn, rd);
5854 break;
5855 default:
5856 unsupported_encoding(s, insn);
5857 break;
5858 }
Alex Bennée94ad8702014-01-23 14:37:07 +00005859}
5860
5861/* C3.6.10 AdvSIMD scalar three different
5862 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 0
5863 * +-----+---+-----------+------+---+------+--------+-----+------+------+
5864 * | 0 1 | U | 1 1 1 1 0 | size | 1 | Rm | opcode | 0 0 | Rn | Rd |
5865 * +-----+---+-----------+------+---+------+--------+-----+------+------+
5866 */
5867static void disas_simd_scalar_three_reg_diff(DisasContext *s, uint32_t insn)
5868{
5869 unsupported_encoding(s, insn);
5870}
5871
Peter Maydell453ff1f2014-01-07 12:13:47 +00005872static void handle_3same_64(DisasContext *s, int opcode, bool u,
5873 TCGv_i64 tcg_rd, TCGv_i64 tcg_rn, TCGv_i64 tcg_rm)
5874{
5875 /* Handle 64x64->64 opcodes which are shared between the scalar
5876 * and vector 3-same groups. We cover every opcode where size == 3
5877 * is valid in either the three-reg-same (integer, not pairwise)
5878 * or scalar-three-reg-same groups. (Some opcodes are not yet
5879 * implemented.)
5880 */
5881 TCGCond cond;
5882
5883 switch (opcode) {
Peter Maydell299088d2014-01-25 14:37:12 +00005884 case 0x1: /* SQADD */
5885 if (u) {
5886 gen_helper_neon_qadd_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
5887 } else {
5888 gen_helper_neon_qadd_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
5889 }
5890 break;
5891 case 0x5: /* SQSUB */
5892 if (u) {
5893 gen_helper_neon_qsub_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
5894 } else {
5895 gen_helper_neon_qsub_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
5896 }
5897 break;
Peter Maydell453ff1f2014-01-07 12:13:47 +00005898 case 0x6: /* CMGT, CMHI */
5899 /* 64 bit integer comparison, result = test ? (2^64 - 1) : 0.
5900 * We implement this using setcond (test) and then negating.
5901 */
5902 cond = u ? TCG_COND_GTU : TCG_COND_GT;
5903 do_cmop:
5904 tcg_gen_setcond_i64(cond, tcg_rd, tcg_rn, tcg_rm);
5905 tcg_gen_neg_i64(tcg_rd, tcg_rd);
5906 break;
5907 case 0x7: /* CMGE, CMHS */
5908 cond = u ? TCG_COND_GEU : TCG_COND_GE;
5909 goto do_cmop;
5910 case 0x11: /* CMTST, CMEQ */
5911 if (u) {
5912 cond = TCG_COND_EQ;
5913 goto do_cmop;
5914 }
5915 /* CMTST : test is "if (X & Y != 0)". */
5916 tcg_gen_and_i64(tcg_rd, tcg_rn, tcg_rm);
5917 tcg_gen_setcondi_i64(TCG_COND_NE, tcg_rd, tcg_rd, 0);
5918 tcg_gen_neg_i64(tcg_rd, tcg_rd);
5919 break;
Peter Maydell299088d2014-01-25 14:37:12 +00005920 case 0x8: /* SSHL, USHL */
5921 if (u) {
5922 gen_helper_neon_shl_u64(tcg_rd, tcg_rn, tcg_rm);
5923 } else {
5924 gen_helper_neon_shl_s64(tcg_rd, tcg_rn, tcg_rm);
5925 }
5926 break;
5927 case 0x9: /* SQSHL, UQSHL */
5928 if (u) {
5929 gen_helper_neon_qshl_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
5930 } else {
5931 gen_helper_neon_qshl_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
5932 }
5933 break;
5934 case 0xa: /* SRSHL, URSHL */
5935 if (u) {
5936 gen_helper_neon_rshl_u64(tcg_rd, tcg_rn, tcg_rm);
5937 } else {
5938 gen_helper_neon_rshl_s64(tcg_rd, tcg_rn, tcg_rm);
5939 }
5940 break;
5941 case 0xb: /* SQRSHL, UQRSHL */
5942 if (u) {
5943 gen_helper_neon_qrshl_u64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
5944 } else {
5945 gen_helper_neon_qrshl_s64(tcg_rd, cpu_env, tcg_rn, tcg_rm);
5946 }
5947 break;
Peter Maydell453ff1f2014-01-07 12:13:47 +00005948 case 0x10: /* ADD, SUB */
5949 if (u) {
5950 tcg_gen_sub_i64(tcg_rd, tcg_rn, tcg_rm);
5951 } else {
5952 tcg_gen_add_i64(tcg_rd, tcg_rn, tcg_rm);
5953 }
5954 break;
Peter Maydell453ff1f2014-01-07 12:13:47 +00005955 default:
5956 g_assert_not_reached();
5957 }
5958}
5959
Peter Maydell55bab512014-01-17 11:35:41 +00005960/* Handle the 3-same-operands float operations; shared by the scalar
5961 * and vector encodings. The caller must filter out any encodings
5962 * not allocated for the encoding it is dealing with.
5963 */
5964static void handle_3same_float(DisasContext *s, int size, int elements,
5965 int fpopcode, int rd, int rn, int rm)
5966{
5967 int pass;
5968 TCGv_ptr fpst = get_fpstatus_ptr();
5969
5970 for (pass = 0; pass < elements; pass++) {
5971 if (size) {
5972 /* Double */
5973 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
5974 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
5975 TCGv_i64 tcg_res = tcg_temp_new_i64();
5976
5977 read_vec_element(s, tcg_op1, rn, pass, MO_64);
5978 read_vec_element(s, tcg_op2, rm, pass, MO_64);
5979
5980 switch (fpopcode) {
5981 case 0x18: /* FMAXNM */
5982 gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
5983 break;
5984 case 0x1a: /* FADD */
5985 gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
5986 break;
5987 case 0x1e: /* FMAX */
5988 gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
5989 break;
5990 case 0x38: /* FMINNM */
5991 gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
5992 break;
5993 case 0x3a: /* FSUB */
5994 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
5995 break;
5996 case 0x3e: /* FMIN */
5997 gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
5998 break;
5999 case 0x5b: /* FMUL */
6000 gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
6001 break;
6002 case 0x5f: /* FDIV */
6003 gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
6004 break;
6005 case 0x7a: /* FABD */
6006 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
6007 gen_helper_vfp_absd(tcg_res, tcg_res);
6008 break;
6009 default:
6010 g_assert_not_reached();
6011 }
6012
6013 write_vec_element(s, tcg_res, rd, pass, MO_64);
6014
6015 tcg_temp_free_i64(tcg_res);
6016 tcg_temp_free_i64(tcg_op1);
6017 tcg_temp_free_i64(tcg_op2);
6018 } else {
6019 /* Single */
6020 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
6021 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
6022 TCGv_i32 tcg_res = tcg_temp_new_i32();
6023
6024 read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
6025 read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
6026
6027 switch (fpopcode) {
6028 case 0x1a: /* FADD */
6029 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
6030 break;
6031 case 0x1e: /* FMAX */
6032 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
6033 break;
6034 case 0x18: /* FMAXNM */
6035 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
6036 break;
6037 case 0x38: /* FMINNM */
6038 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
6039 break;
6040 case 0x3a: /* FSUB */
6041 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
6042 break;
6043 case 0x3e: /* FMIN */
6044 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
6045 break;
6046 case 0x5b: /* FMUL */
6047 gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
6048 break;
6049 case 0x5f: /* FDIV */
6050 gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
6051 break;
6052 case 0x7a: /* FABD */
6053 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
6054 gen_helper_vfp_abss(tcg_res, tcg_res);
6055 break;
6056 default:
6057 g_assert_not_reached();
6058 }
6059
6060 if (elements == 1) {
6061 /* scalar single so clear high part */
6062 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
6063
6064 tcg_gen_extu_i32_i64(tcg_tmp, tcg_res);
6065 write_vec_element(s, tcg_tmp, rd, pass, MO_64);
6066 tcg_temp_free_i64(tcg_tmp);
6067 } else {
6068 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
6069 }
6070
6071 tcg_temp_free_i32(tcg_res);
6072 tcg_temp_free_i32(tcg_op1);
6073 tcg_temp_free_i32(tcg_op2);
6074 }
6075 }
6076
6077 tcg_temp_free_ptr(fpst);
6078
6079 if ((elements << size) < 4) {
6080 /* scalar, or non-quad vector op */
6081 clear_vec_high(s, rd);
6082 }
6083}
6084
Alex Bennée94ad8702014-01-23 14:37:07 +00006085/* C3.6.11 AdvSIMD scalar three same
6086 * 31 30 29 28 24 23 22 21 20 16 15 11 10 9 5 4 0
6087 * +-----+---+-----------+------+---+------+--------+---+------+------+
6088 * | 0 1 | U | 1 1 1 1 0 | size | 1 | Rm | opcode | 1 | Rn | Rd |
6089 * +-----+---+-----------+------+---+------+--------+---+------+------+
6090 */
6091static void disas_simd_scalar_three_reg_same(DisasContext *s, uint32_t insn)
6092{
Peter Maydell453ff1f2014-01-07 12:13:47 +00006093 int rd = extract32(insn, 0, 5);
6094 int rn = extract32(insn, 5, 5);
6095 int opcode = extract32(insn, 11, 5);
6096 int rm = extract32(insn, 16, 5);
6097 int size = extract32(insn, 22, 2);
6098 bool u = extract32(insn, 29, 1);
Peter Maydell453ff1f2014-01-07 12:13:47 +00006099 TCGv_i64 tcg_rd;
6100
6101 if (opcode >= 0x18) {
6102 /* Floating point: U, size[1] and opcode indicate operation */
6103 int fpopcode = opcode | (extract32(size, 1, 1) << 5) | (u << 6);
6104 switch (fpopcode) {
6105 case 0x1b: /* FMULX */
6106 case 0x1c: /* FCMEQ */
6107 case 0x1f: /* FRECPS */
6108 case 0x3f: /* FRSQRTS */
6109 case 0x5c: /* FCMGE */
6110 case 0x5d: /* FACGE */
Peter Maydell453ff1f2014-01-07 12:13:47 +00006111 case 0x7c: /* FCMGT */
6112 case 0x7d: /* FACGT */
6113 unsupported_encoding(s, insn);
6114 return;
Peter Maydell55bab512014-01-17 11:35:41 +00006115 case 0x7a: /* FABD */
6116 break;
Peter Maydell453ff1f2014-01-07 12:13:47 +00006117 default:
6118 unallocated_encoding(s);
6119 return;
6120 }
Peter Maydell55bab512014-01-17 11:35:41 +00006121
6122 handle_3same_float(s, extract32(size, 0, 1), 1, fpopcode, rd, rn, rm);
6123 return;
Peter Maydell453ff1f2014-01-07 12:13:47 +00006124 }
6125
6126 switch (opcode) {
6127 case 0x1: /* SQADD, UQADD */
6128 case 0x5: /* SQSUB, UQSUB */
Peter Maydelle2e193f2014-01-25 22:07:40 +00006129 case 0x9: /* SQSHL, UQSHL */
6130 case 0xb: /* SQRSHL, UQRSHL */
6131 break;
Peter Maydell299088d2014-01-25 14:37:12 +00006132 case 0x8: /* SSHL, USHL */
6133 case 0xa: /* SRSHL, URSHL */
Peter Maydell453ff1f2014-01-07 12:13:47 +00006134 case 0x6: /* CMGT, CMHI */
6135 case 0x7: /* CMGE, CMHS */
6136 case 0x11: /* CMTST, CMEQ */
6137 case 0x10: /* ADD, SUB (vector) */
6138 if (size != 3) {
6139 unallocated_encoding(s);
6140 return;
6141 }
6142 break;
Peter Maydell453ff1f2014-01-07 12:13:47 +00006143 case 0x16: /* SQDMULH, SQRDMULH (vector) */
6144 if (size != 1 && size != 2) {
6145 unallocated_encoding(s);
6146 return;
6147 }
Peter Maydelle2e193f2014-01-25 22:07:40 +00006148 break;
Peter Maydell453ff1f2014-01-07 12:13:47 +00006149 default:
6150 unallocated_encoding(s);
6151 return;
6152 }
6153
Peter Maydell453ff1f2014-01-07 12:13:47 +00006154 tcg_rd = tcg_temp_new_i64();
6155
Peter Maydelle2e193f2014-01-25 22:07:40 +00006156 if (size == 3) {
6157 TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
6158 TCGv_i64 tcg_rm = read_fp_dreg(s, rm);
6159
6160 handle_3same_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rm);
6161 tcg_temp_free_i64(tcg_rn);
6162 tcg_temp_free_i64(tcg_rm);
6163 } else {
6164 /* Do a single operation on the lowest element in the vector.
6165 * We use the standard Neon helpers and rely on 0 OP 0 == 0 with
6166 * no side effects for all these operations.
6167 * OPTME: special-purpose helpers would avoid doing some
6168 * unnecessary work in the helper for the 8 and 16 bit cases.
6169 */
6170 NeonGenTwoOpEnvFn *genenvfn;
6171 TCGv_i32 tcg_rn = tcg_temp_new_i32();
6172 TCGv_i32 tcg_rm = tcg_temp_new_i32();
6173 TCGv_i32 tcg_rd32 = tcg_temp_new_i32();
6174
6175 read_vec_element_i32(s, tcg_rn, rn, 0, size);
6176 read_vec_element_i32(s, tcg_rm, rm, 0, size);
6177
6178 switch (opcode) {
6179 case 0x1: /* SQADD, UQADD */
6180 {
6181 static NeonGenTwoOpEnvFn * const fns[3][2] = {
6182 { gen_helper_neon_qadd_s8, gen_helper_neon_qadd_u8 },
6183 { gen_helper_neon_qadd_s16, gen_helper_neon_qadd_u16 },
6184 { gen_helper_neon_qadd_s32, gen_helper_neon_qadd_u32 },
6185 };
6186 genenvfn = fns[size][u];
6187 break;
6188 }
6189 case 0x5: /* SQSUB, UQSUB */
6190 {
6191 static NeonGenTwoOpEnvFn * const fns[3][2] = {
6192 { gen_helper_neon_qsub_s8, gen_helper_neon_qsub_u8 },
6193 { gen_helper_neon_qsub_s16, gen_helper_neon_qsub_u16 },
6194 { gen_helper_neon_qsub_s32, gen_helper_neon_qsub_u32 },
6195 };
6196 genenvfn = fns[size][u];
6197 break;
6198 }
6199 case 0x9: /* SQSHL, UQSHL */
6200 {
6201 static NeonGenTwoOpEnvFn * const fns[3][2] = {
6202 { gen_helper_neon_qshl_s8, gen_helper_neon_qshl_u8 },
6203 { gen_helper_neon_qshl_s16, gen_helper_neon_qshl_u16 },
6204 { gen_helper_neon_qshl_s32, gen_helper_neon_qshl_u32 },
6205 };
6206 genenvfn = fns[size][u];
6207 break;
6208 }
6209 case 0xb: /* SQRSHL, UQRSHL */
6210 {
6211 static NeonGenTwoOpEnvFn * const fns[3][2] = {
6212 { gen_helper_neon_qrshl_s8, gen_helper_neon_qrshl_u8 },
6213 { gen_helper_neon_qrshl_s16, gen_helper_neon_qrshl_u16 },
6214 { gen_helper_neon_qrshl_s32, gen_helper_neon_qrshl_u32 },
6215 };
6216 genenvfn = fns[size][u];
6217 break;
6218 }
6219 case 0x16: /* SQDMULH, SQRDMULH */
6220 {
6221 static NeonGenTwoOpEnvFn * const fns[2][2] = {
6222 { gen_helper_neon_qdmulh_s16, gen_helper_neon_qrdmulh_s16 },
6223 { gen_helper_neon_qdmulh_s32, gen_helper_neon_qrdmulh_s32 },
6224 };
6225 assert(size == 1 || size == 2);
6226 genenvfn = fns[size - 1][u];
6227 break;
6228 }
6229 default:
6230 g_assert_not_reached();
6231 }
6232
6233 genenvfn(tcg_rd32, cpu_env, tcg_rn, tcg_rm);
6234 tcg_gen_extu_i32_i64(tcg_rd, tcg_rd32);
6235 tcg_temp_free_i32(tcg_rd32);
6236 tcg_temp_free_i32(tcg_rn);
6237 tcg_temp_free_i32(tcg_rm);
6238 }
Peter Maydell453ff1f2014-01-07 12:13:47 +00006239
6240 write_fp_dreg(s, rd, tcg_rd);
6241
Peter Maydell453ff1f2014-01-07 12:13:47 +00006242 tcg_temp_free_i64(tcg_rd);
Alex Bennée94ad8702014-01-23 14:37:07 +00006243}
6244
Peter Maydell506f5772014-01-14 18:48:32 +00006245static void handle_2misc_64(DisasContext *s, int opcode, bool u,
6246 TCGv_i64 tcg_rd, TCGv_i64 tcg_rn)
6247{
6248 /* Handle 64->64 opcodes which are shared between the scalar and
6249 * vector 2-reg-misc groups. We cover every integer opcode where size == 3
Peter Maydell825c6e92014-01-26 18:40:44 +00006250 * is valid in either group and also the double-precision fp ops.
Peter Maydell506f5772014-01-14 18:48:32 +00006251 */
6252 TCGCond cond;
6253
6254 switch (opcode) {
Peter Maydell6aa24912014-01-26 00:53:25 +00006255 case 0x5: /* NOT */
6256 /* This opcode is shared with CNT and RBIT but we have earlier
6257 * enforced that size == 3 if and only if this is the NOT insn.
6258 */
6259 tcg_gen_not_i64(tcg_rd, tcg_rn);
6260 break;
Peter Maydell506f5772014-01-14 18:48:32 +00006261 case 0xa: /* CMLT */
6262 /* 64 bit integer comparison against zero, result is
6263 * test ? (2^64 - 1) : 0. We implement via setcond(!test) and
6264 * subtracting 1.
6265 */
6266 cond = TCG_COND_LT;
6267 do_cmop:
6268 tcg_gen_setcondi_i64(tcg_invert_cond(cond), tcg_rd, tcg_rn, 0);
6269 tcg_gen_subi_i64(tcg_rd, tcg_rd, 1);
6270 break;
6271 case 0x8: /* CMGT, CMGE */
6272 cond = u ? TCG_COND_GE : TCG_COND_GT;
6273 goto do_cmop;
6274 case 0x9: /* CMEQ, CMLE */
6275 cond = u ? TCG_COND_LE : TCG_COND_EQ;
6276 goto do_cmop;
6277 case 0xb: /* ABS, NEG */
6278 if (u) {
6279 tcg_gen_neg_i64(tcg_rd, tcg_rn);
6280 } else {
6281 TCGv_i64 tcg_zero = tcg_const_i64(0);
6282 tcg_gen_neg_i64(tcg_rd, tcg_rn);
6283 tcg_gen_movcond_i64(TCG_COND_GT, tcg_rd, tcg_rn, tcg_zero,
6284 tcg_rn, tcg_rd);
6285 tcg_temp_free_i64(tcg_zero);
6286 }
6287 break;
Peter Maydell825c6e92014-01-26 18:40:44 +00006288 case 0x2f: /* FABS */
6289 gen_helper_vfp_absd(tcg_rd, tcg_rn);
6290 break;
6291 case 0x6f: /* FNEG */
6292 gen_helper_vfp_negd(tcg_rd, tcg_rn);
6293 break;
Peter Maydell506f5772014-01-14 18:48:32 +00006294 default:
6295 g_assert_not_reached();
6296 }
6297}
6298
Alex Bennée94ad8702014-01-23 14:37:07 +00006299/* C3.6.12 AdvSIMD scalar two reg misc
6300 * 31 30 29 28 24 23 22 21 17 16 12 11 10 9 5 4 0
6301 * +-----+---+-----------+------+-----------+--------+-----+------+------+
6302 * | 0 1 | U | 1 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 | Rn | Rd |
6303 * +-----+---+-----------+------+-----------+--------+-----+------+------+
6304 */
6305static void disas_simd_scalar_two_reg_misc(DisasContext *s, uint32_t insn)
6306{
Peter Maydell506f5772014-01-14 18:48:32 +00006307 int rd = extract32(insn, 0, 5);
6308 int rn = extract32(insn, 5, 5);
6309 int opcode = extract32(insn, 12, 5);
6310 int size = extract32(insn, 22, 2);
6311 bool u = extract32(insn, 29, 1);
6312
6313 switch (opcode) {
6314 case 0xa: /* CMLT */
6315 if (u) {
6316 unallocated_encoding(s);
6317 return;
6318 }
6319 /* fall through */
6320 case 0x8: /* CMGT, CMGE */
6321 case 0x9: /* CMEQ, CMLE */
6322 case 0xb: /* ABS, NEG */
6323 if (size != 3) {
6324 unallocated_encoding(s);
6325 return;
6326 }
6327 break;
6328 default:
6329 /* Other categories of encoding in this class:
6330 * + floating point (single and double)
6331 * + SUQADD/USQADD/SQABS/SQNEG : size 8, 16, 32 or 64
6332 * + SQXTN/SQXTN2/SQXTUN/SQXTUN2/UQXTN/UQXTN2:
6333 * narrowing saturate ops: size 64/32/16 -> 32/16/8
6334 */
6335 unsupported_encoding(s, insn);
6336 return;
6337 }
6338
6339 if (size == 3) {
6340 TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
6341 TCGv_i64 tcg_rd = tcg_temp_new_i64();
6342
6343 handle_2misc_64(s, opcode, u, tcg_rd, tcg_rn);
6344 write_fp_dreg(s, rd, tcg_rd);
6345 tcg_temp_free_i64(tcg_rd);
6346 tcg_temp_free_i64(tcg_rn);
6347 } else {
6348 /* the 'size might not be 64' ops aren't implemented yet */
6349 g_assert_not_reached();
6350 }
Alex Bennée94ad8702014-01-23 14:37:07 +00006351}
6352
6353/* C3.6.13 AdvSIMD scalar x indexed element
6354 * 31 30 29 28 24 23 22 21 20 19 16 15 12 11 10 9 5 4 0
6355 * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
6356 * | 0 1 | U | 1 1 1 1 1 | size | L | M | Rm | opc | H | 0 | Rn | Rd |
6357 * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
6358 */
6359static void disas_simd_scalar_indexed(DisasContext *s, uint32_t insn)
6360{
6361 unsupported_encoding(s, insn);
6362}
6363
Alex Bennée4db2b1e2014-01-09 18:13:58 +00006364/* SSHR[RA]/USHR[RA] - Vector shift right (optional rounding/accumulate) */
6365static void handle_vec_simd_shri(DisasContext *s, bool is_q, bool is_u,
6366 int immh, int immb, int opcode, int rn, int rd)
6367{
6368 int size = 32 - clz32(immh) - 1;
6369 int immhb = immh << 3 | immb;
6370 int shift = 2 * (8 << size) - immhb;
6371 bool accumulate = false;
6372 bool round = false;
6373 int dsize = is_q ? 128 : 64;
6374 int esize = 8 << size;
6375 int elements = dsize/esize;
6376 TCGMemOp memop = size | (is_u ? 0 : MO_SIGN);
6377 TCGv_i64 tcg_rn = new_tmp_a64(s);
6378 TCGv_i64 tcg_rd = new_tmp_a64(s);
6379 TCGv_i64 tcg_round;
6380 int i;
6381
6382 if (extract32(immh, 3, 1) && !is_q) {
6383 unallocated_encoding(s);
6384 return;
6385 }
6386
6387 if (size > 3 && !is_q) {
6388 unallocated_encoding(s);
6389 return;
6390 }
6391
6392 switch (opcode) {
6393 case 0x02: /* SSRA / USRA (accumulate) */
6394 accumulate = true;
6395 break;
6396 case 0x04: /* SRSHR / URSHR (rounding) */
6397 round = true;
6398 break;
6399 case 0x06: /* SRSRA / URSRA (accum + rounding) */
6400 accumulate = round = true;
6401 break;
6402 }
6403
6404 if (round) {
6405 uint64_t round_const = 1ULL << (shift - 1);
6406 tcg_round = tcg_const_i64(round_const);
6407 } else {
6408 TCGV_UNUSED_I64(tcg_round);
6409 }
6410
6411 for (i = 0; i < elements; i++) {
6412 read_vec_element(s, tcg_rn, rn, i, memop);
6413 if (accumulate) {
6414 read_vec_element(s, tcg_rd, rd, i, memop);
6415 }
6416
6417 handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
6418 accumulate, is_u, size, shift);
6419
6420 write_vec_element(s, tcg_rd, rd, i, size);
6421 }
6422
6423 if (!is_q) {
6424 clear_vec_high(s, rd);
6425 }
6426
6427 if (round) {
6428 tcg_temp_free_i64(tcg_round);
6429 }
6430}
6431
6432/* SHL/SLI - Vector shift left */
6433static void handle_vec_simd_shli(DisasContext *s, bool is_q, bool insert,
6434 int immh, int immb, int opcode, int rn, int rd)
6435{
6436 int size = 32 - clz32(immh) - 1;
6437 int immhb = immh << 3 | immb;
6438 int shift = immhb - (8 << size);
6439 int dsize = is_q ? 128 : 64;
6440 int esize = 8 << size;
6441 int elements = dsize/esize;
6442 TCGv_i64 tcg_rn = new_tmp_a64(s);
6443 TCGv_i64 tcg_rd = new_tmp_a64(s);
6444 int i;
6445
6446 if (extract32(immh, 3, 1) && !is_q) {
6447 unallocated_encoding(s);
6448 return;
6449 }
6450
6451 if (size > 3 && !is_q) {
6452 unallocated_encoding(s);
6453 return;
6454 }
6455
6456 for (i = 0; i < elements; i++) {
6457 read_vec_element(s, tcg_rn, rn, i, size);
6458 if (insert) {
6459 read_vec_element(s, tcg_rd, rd, i, size);
6460 }
6461
6462 handle_shli_with_ins(tcg_rd, tcg_rn, insert, shift);
6463
6464 write_vec_element(s, tcg_rd, rd, i, size);
6465 }
6466
6467 if (!is_q) {
6468 clear_vec_high(s, rd);
6469 }
6470}
6471
6472/* USHLL/SHLL - Vector shift left with widening */
6473static void handle_vec_simd_wshli(DisasContext *s, bool is_q, bool is_u,
6474 int immh, int immb, int opcode, int rn, int rd)
6475{
6476 int size = 32 - clz32(immh) - 1;
6477 int immhb = immh << 3 | immb;
6478 int shift = immhb - (8 << size);
6479 int dsize = 64;
6480 int esize = 8 << size;
6481 int elements = dsize/esize;
6482 TCGv_i64 tcg_rn = new_tmp_a64(s);
6483 TCGv_i64 tcg_rd = new_tmp_a64(s);
6484 int i;
6485
6486 if (size >= 3) {
6487 unallocated_encoding(s);
6488 return;
6489 }
6490
6491 /* For the LL variants the store is larger than the load,
6492 * so if rd == rn we would overwrite parts of our input.
6493 * So load everything right now and use shifts in the main loop.
6494 */
6495 read_vec_element(s, tcg_rn, rn, is_q ? 1 : 0, MO_64);
6496
6497 for (i = 0; i < elements; i++) {
6498 tcg_gen_shri_i64(tcg_rd, tcg_rn, i * esize);
6499 ext_and_shift_reg(tcg_rd, tcg_rd, size | (!is_u << 2), 0);
6500 tcg_gen_shli_i64(tcg_rd, tcg_rd, shift);
6501 write_vec_element(s, tcg_rd, rd, i, size + 1);
6502 }
6503}
6504
6505
Alex Bennée94ad8702014-01-23 14:37:07 +00006506/* C3.6.14 AdvSIMD shift by immediate
6507 * 31 30 29 28 23 22 19 18 16 15 11 10 9 5 4 0
6508 * +---+---+---+-------------+------+------+--------+---+------+------+
6509 * | 0 | Q | U | 0 1 1 1 1 0 | immh | immb | opcode | 1 | Rn | Rd |
6510 * +---+---+---+-------------+------+------+--------+---+------+------+
6511 */
6512static void disas_simd_shift_imm(DisasContext *s, uint32_t insn)
6513{
Alex Bennée4db2b1e2014-01-09 18:13:58 +00006514 int rd = extract32(insn, 0, 5);
6515 int rn = extract32(insn, 5, 5);
6516 int opcode = extract32(insn, 11, 5);
6517 int immb = extract32(insn, 16, 3);
6518 int immh = extract32(insn, 19, 4);
6519 bool is_u = extract32(insn, 29, 1);
6520 bool is_q = extract32(insn, 30, 1);
6521
6522 switch (opcode) {
6523 case 0x00: /* SSHR / USHR */
6524 case 0x02: /* SSRA / USRA (accumulate) */
6525 case 0x04: /* SRSHR / URSHR (rounding) */
6526 case 0x06: /* SRSRA / URSRA (accum + rounding) */
6527 handle_vec_simd_shri(s, is_q, is_u, immh, immb, opcode, rn, rd);
6528 break;
6529 case 0x0a: /* SHL / SLI */
6530 handle_vec_simd_shli(s, is_q, is_u, immh, immb, opcode, rn, rd);
6531 break;
6532 case 0x14: /* SSHLL / USHLL */
6533 handle_vec_simd_wshli(s, is_q, is_u, immh, immb, opcode, rn, rd);
6534 break;
6535 default:
6536 /* We don't currently implement any of the Narrow or saturating shifts;
6537 * nor do we implement the fixed-point conversions in this
6538 * encoding group (SCVTF, FCVTZS, UCVTF, FCVTZU).
6539 */
6540 unsupported_encoding(s, insn);
6541 return;
6542 }
Alex Bennée94ad8702014-01-23 14:37:07 +00006543}
6544
Peter Maydellac224302014-01-14 12:44:38 +00006545static void handle_3rd_widening(DisasContext *s, int is_q, int is_u, int size,
6546 int opcode, int rd, int rn, int rm)
6547{
6548 /* 3-reg-different widening insns: 64 x 64 -> 128 */
6549 TCGv_i64 tcg_res[2];
6550 int pass, accop;
6551
6552 tcg_res[0] = tcg_temp_new_i64();
6553 tcg_res[1] = tcg_temp_new_i64();
6554
6555 /* Does this op do an adding accumulate, a subtracting accumulate,
6556 * or no accumulate at all?
6557 */
6558 switch (opcode) {
6559 case 5:
6560 case 8:
6561 case 9:
6562 accop = 1;
6563 break;
6564 case 10:
6565 case 11:
6566 accop = -1;
6567 break;
6568 default:
6569 accop = 0;
6570 break;
6571 }
6572
6573 if (accop != 0) {
6574 read_vec_element(s, tcg_res[0], rd, 0, MO_64);
6575 read_vec_element(s, tcg_res[1], rd, 1, MO_64);
6576 }
6577
6578 /* size == 2 means two 32x32->64 operations; this is worth special
6579 * casing because we can generally handle it inline.
6580 */
6581 if (size == 2) {
6582 for (pass = 0; pass < 2; pass++) {
6583 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
6584 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
6585 TCGv_i64 tcg_passres;
6586 TCGMemOp memop = MO_32 | (is_u ? 0 : MO_SIGN);
6587
6588 int elt = pass + is_q * 2;
6589
6590 read_vec_element(s, tcg_op1, rn, elt, memop);
6591 read_vec_element(s, tcg_op2, rm, elt, memop);
6592
6593 if (accop == 0) {
6594 tcg_passres = tcg_res[pass];
6595 } else {
6596 tcg_passres = tcg_temp_new_i64();
6597 }
6598
6599 switch (opcode) {
Peter Maydellca8ab582014-01-14 12:44:38 +00006600 case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
6601 case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
6602 {
6603 TCGv_i64 tcg_tmp1 = tcg_temp_new_i64();
6604 TCGv_i64 tcg_tmp2 = tcg_temp_new_i64();
6605
6606 tcg_gen_sub_i64(tcg_tmp1, tcg_op1, tcg_op2);
6607 tcg_gen_sub_i64(tcg_tmp2, tcg_op2, tcg_op1);
6608 tcg_gen_movcond_i64(is_u ? TCG_COND_GEU : TCG_COND_GE,
6609 tcg_passres,
6610 tcg_op1, tcg_op2, tcg_tmp1, tcg_tmp2);
6611 tcg_temp_free_i64(tcg_tmp1);
6612 tcg_temp_free_i64(tcg_tmp2);
6613 break;
6614 }
Peter Maydellac224302014-01-14 12:44:38 +00006615 case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
6616 case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
6617 case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
6618 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
6619 break;
6620 default:
6621 g_assert_not_reached();
6622 }
6623
6624 if (accop > 0) {
6625 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
6626 tcg_temp_free_i64(tcg_passres);
6627 } else if (accop < 0) {
6628 tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
6629 tcg_temp_free_i64(tcg_passres);
6630 }
6631
6632 tcg_temp_free_i64(tcg_op1);
6633 tcg_temp_free_i64(tcg_op2);
6634 }
6635 } else {
6636 /* size 0 or 1, generally helper functions */
6637 for (pass = 0; pass < 2; pass++) {
6638 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
6639 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
6640 TCGv_i64 tcg_passres;
6641 int elt = pass + is_q * 2;
6642
6643 read_vec_element_i32(s, tcg_op1, rn, elt, MO_32);
6644 read_vec_element_i32(s, tcg_op2, rm, elt, MO_32);
6645
6646 if (accop == 0) {
6647 tcg_passres = tcg_res[pass];
6648 } else {
6649 tcg_passres = tcg_temp_new_i64();
6650 }
6651
6652 switch (opcode) {
Peter Maydellca8ab582014-01-14 12:44:38 +00006653 case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
6654 case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
6655 if (size == 0) {
6656 if (is_u) {
6657 gen_helper_neon_abdl_u16(tcg_passres, tcg_op1, tcg_op2);
6658 } else {
6659 gen_helper_neon_abdl_s16(tcg_passres, tcg_op1, tcg_op2);
6660 }
6661 } else {
6662 if (is_u) {
6663 gen_helper_neon_abdl_u32(tcg_passres, tcg_op1, tcg_op2);
6664 } else {
6665 gen_helper_neon_abdl_s32(tcg_passres, tcg_op1, tcg_op2);
6666 }
6667 }
6668 break;
Peter Maydellac224302014-01-14 12:44:38 +00006669 case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
6670 case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
6671 case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
6672 if (size == 0) {
6673 if (is_u) {
6674 gen_helper_neon_mull_u8(tcg_passres, tcg_op1, tcg_op2);
6675 } else {
6676 gen_helper_neon_mull_s8(tcg_passres, tcg_op1, tcg_op2);
6677 }
6678 } else {
6679 if (is_u) {
6680 gen_helper_neon_mull_u16(tcg_passres, tcg_op1, tcg_op2);
6681 } else {
6682 gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
6683 }
6684 }
6685 break;
6686 default:
6687 g_assert_not_reached();
6688 }
6689 tcg_temp_free_i32(tcg_op1);
6690 tcg_temp_free_i32(tcg_op2);
6691
6692 if (accop > 0) {
6693 if (size == 0) {
6694 gen_helper_neon_addl_u16(tcg_res[pass], tcg_res[pass],
6695 tcg_passres);
6696 } else {
6697 gen_helper_neon_addl_u32(tcg_res[pass], tcg_res[pass],
6698 tcg_passres);
6699 }
6700 tcg_temp_free_i64(tcg_passres);
6701 } else if (accop < 0) {
6702 if (size == 0) {
6703 gen_helper_neon_subl_u16(tcg_res[pass], tcg_res[pass],
6704 tcg_passres);
6705 } else {
6706 gen_helper_neon_subl_u32(tcg_res[pass], tcg_res[pass],
6707 tcg_passres);
6708 }
6709 tcg_temp_free_i64(tcg_passres);
6710 }
6711 }
6712 }
6713
6714 write_vec_element(s, tcg_res[0], rd, 0, MO_64);
6715 write_vec_element(s, tcg_res[1], rd, 1, MO_64);
6716 tcg_temp_free_i64(tcg_res[0]);
6717 tcg_temp_free_i64(tcg_res[1]);
6718}
6719
Alex Bennée94ad8702014-01-23 14:37:07 +00006720/* C3.6.15 AdvSIMD three different
6721 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 0
6722 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
6723 * | 0 | Q | U | 0 1 1 1 0 | size | 1 | Rm | opcode | 0 0 | Rn | Rd |
6724 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
6725 */
6726static void disas_simd_three_reg_diff(DisasContext *s, uint32_t insn)
6727{
Peter Maydellac224302014-01-14 12:44:38 +00006728 /* Instructions in this group fall into three basic classes
6729 * (in each case with the operation working on each element in
6730 * the input vectors):
6731 * (1) widening 64 x 64 -> 128 (with possibly Vd as an extra
6732 * 128 bit input)
6733 * (2) wide 64 x 128 -> 128
6734 * (3) narrowing 128 x 128 -> 64
6735 * Here we do initial decode, catch unallocated cases and
6736 * dispatch to separate functions for each class.
6737 */
6738 int is_q = extract32(insn, 30, 1);
6739 int is_u = extract32(insn, 29, 1);
6740 int size = extract32(insn, 22, 2);
6741 int opcode = extract32(insn, 12, 4);
6742 int rm = extract32(insn, 16, 5);
6743 int rn = extract32(insn, 5, 5);
6744 int rd = extract32(insn, 0, 5);
6745
6746 switch (opcode) {
6747 case 1: /* SADDW, SADDW2, UADDW, UADDW2 */
6748 case 3: /* SSUBW, SSUBW2, USUBW, USUBW2 */
6749 /* 64 x 128 -> 128 */
6750 unsupported_encoding(s, insn);
6751 break;
6752 case 4: /* ADDHN, ADDHN2, RADDHN, RADDHN2 */
6753 case 6: /* SUBHN, SUBHN2, RSUBHN, RSUBHN2 */
6754 /* 128 x 128 -> 64 */
6755 unsupported_encoding(s, insn);
6756 break;
6757 case 9:
6758 case 11:
6759 case 13:
6760 case 14:
6761 if (is_u) {
6762 unallocated_encoding(s);
6763 return;
6764 }
6765 /* fall through */
6766 case 0:
6767 case 2:
Peter Maydellac224302014-01-14 12:44:38 +00006768 unsupported_encoding(s, insn);
6769 break;
Peter Maydellca8ab582014-01-14 12:44:38 +00006770 case 5:
6771 case 7:
Peter Maydellac224302014-01-14 12:44:38 +00006772 case 8:
6773 case 10:
6774 case 12:
6775 /* 64 x 64 -> 128 */
6776 if (size == 3) {
6777 unallocated_encoding(s);
6778 return;
6779 }
6780 handle_3rd_widening(s, is_q, is_u, size, opcode, rd, rn, rm);
6781 break;
6782 default:
6783 /* opcode 15 not allocated */
6784 unallocated_encoding(s);
6785 break;
6786 }
Alex Bennée94ad8702014-01-23 14:37:07 +00006787}
6788
Peter Maydellaab820f2014-01-16 13:00:47 +00006789/* Logic op (opcode == 3) subgroup of C3.6.16. */
6790static void disas_simd_3same_logic(DisasContext *s, uint32_t insn)
6791{
Peter Maydell75415722014-01-23 13:19:00 +00006792 int rd = extract32(insn, 0, 5);
6793 int rn = extract32(insn, 5, 5);
6794 int rm = extract32(insn, 16, 5);
6795 int size = extract32(insn, 22, 2);
6796 bool is_u = extract32(insn, 29, 1);
6797 bool is_q = extract32(insn, 30, 1);
6798 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
6799 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
6800 TCGv_i64 tcg_res[2];
6801 int pass;
6802
6803 tcg_res[0] = tcg_temp_new_i64();
6804 tcg_res[1] = tcg_temp_new_i64();
6805
6806 for (pass = 0; pass < (is_q ? 2 : 1); pass++) {
6807 read_vec_element(s, tcg_op1, rn, pass, MO_64);
6808 read_vec_element(s, tcg_op2, rm, pass, MO_64);
6809
6810 if (!is_u) {
6811 switch (size) {
6812 case 0: /* AND */
6813 tcg_gen_and_i64(tcg_res[pass], tcg_op1, tcg_op2);
6814 break;
6815 case 1: /* BIC */
6816 tcg_gen_andc_i64(tcg_res[pass], tcg_op1, tcg_op2);
6817 break;
6818 case 2: /* ORR */
6819 tcg_gen_or_i64(tcg_res[pass], tcg_op1, tcg_op2);
6820 break;
6821 case 3: /* ORN */
6822 tcg_gen_orc_i64(tcg_res[pass], tcg_op1, tcg_op2);
6823 break;
6824 }
6825 } else {
6826 if (size != 0) {
6827 /* B* ops need res loaded to operate on */
6828 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
6829 }
6830
6831 switch (size) {
6832 case 0: /* EOR */
6833 tcg_gen_xor_i64(tcg_res[pass], tcg_op1, tcg_op2);
6834 break;
6835 case 1: /* BSL bitwise select */
6836 tcg_gen_xor_i64(tcg_op1, tcg_op1, tcg_op2);
6837 tcg_gen_and_i64(tcg_op1, tcg_op1, tcg_res[pass]);
6838 tcg_gen_xor_i64(tcg_res[pass], tcg_op2, tcg_op1);
6839 break;
6840 case 2: /* BIT, bitwise insert if true */
6841 tcg_gen_xor_i64(tcg_op1, tcg_op1, tcg_res[pass]);
6842 tcg_gen_and_i64(tcg_op1, tcg_op1, tcg_op2);
6843 tcg_gen_xor_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
6844 break;
6845 case 3: /* BIF, bitwise insert if false */
6846 tcg_gen_xor_i64(tcg_op1, tcg_op1, tcg_res[pass]);
6847 tcg_gen_andc_i64(tcg_op1, tcg_op1, tcg_op2);
6848 tcg_gen_xor_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
6849 break;
6850 }
6851 }
6852 }
6853
6854 write_vec_element(s, tcg_res[0], rd, 0, MO_64);
6855 if (!is_q) {
6856 tcg_gen_movi_i64(tcg_res[1], 0);
6857 }
6858 write_vec_element(s, tcg_res[1], rd, 1, MO_64);
6859
6860 tcg_temp_free_i64(tcg_op1);
6861 tcg_temp_free_i64(tcg_op2);
6862 tcg_temp_free_i64(tcg_res[0]);
6863 tcg_temp_free_i64(tcg_res[1]);
Peter Maydellaab820f2014-01-16 13:00:47 +00006864}
6865
Peter Maydell971675f2014-01-25 19:28:01 +00006866/* Helper functions for pairwise 32 bit comparisons */
6867static void gen_pmax_s32(TCGv_i32 res, TCGv_i32 op1, TCGv_i32 op2)
6868{
6869 tcg_gen_movcond_i32(TCG_COND_GE, res, op1, op2, op1, op2);
6870}
6871
6872static void gen_pmax_u32(TCGv_i32 res, TCGv_i32 op1, TCGv_i32 op2)
6873{
6874 tcg_gen_movcond_i32(TCG_COND_GEU, res, op1, op2, op1, op2);
6875}
6876
6877static void gen_pmin_s32(TCGv_i32 res, TCGv_i32 op1, TCGv_i32 op2)
6878{
6879 tcg_gen_movcond_i32(TCG_COND_LE, res, op1, op2, op1, op2);
6880}
6881
6882static void gen_pmin_u32(TCGv_i32 res, TCGv_i32 op1, TCGv_i32 op2)
6883{
6884 tcg_gen_movcond_i32(TCG_COND_LEU, res, op1, op2, op1, op2);
6885}
6886
Peter Maydellaab820f2014-01-16 13:00:47 +00006887/* Pairwise op subgroup of C3.6.16. */
6888static void disas_simd_3same_pair(DisasContext *s, uint32_t insn)
6889{
Peter Maydell971675f2014-01-25 19:28:01 +00006890 int is_q = extract32(insn, 30, 1);
6891 int u = extract32(insn, 29, 1);
6892 int size = extract32(insn, 22, 2);
6893 int opcode = extract32(insn, 11, 5);
6894 int rm = extract32(insn, 16, 5);
6895 int rn = extract32(insn, 5, 5);
6896 int rd = extract32(insn, 0, 5);
6897 int pass;
6898
6899 if (size == 3 && !is_q) {
6900 unallocated_encoding(s);
6901 return;
6902 }
6903
6904 switch (opcode) {
6905 case 0x14: /* SMAXP, UMAXP */
6906 case 0x15: /* SMINP, UMINP */
6907 if (size == 3) {
6908 unallocated_encoding(s);
6909 return;
6910 }
6911 break;
6912 case 0x17:
6913 if (u) {
6914 unallocated_encoding(s);
6915 return;
6916 }
6917 break;
6918 default:
6919 g_assert_not_reached();
6920 }
6921
6922 /* These operations work on the concatenated rm:rn, with each pair of
6923 * adjacent elements being operated on to produce an element in the result.
6924 */
6925 if (size == 3) {
6926 TCGv_i64 tcg_res[2];
6927
6928 for (pass = 0; pass < 2; pass++) {
6929 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
6930 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
6931 int passreg = (pass == 0) ? rn : rm;
6932
6933 read_vec_element(s, tcg_op1, passreg, 0, MO_64);
6934 read_vec_element(s, tcg_op2, passreg, 1, MO_64);
6935 tcg_res[pass] = tcg_temp_new_i64();
6936
6937 /* The only 64 bit pairwise integer op is ADDP */
6938 assert(opcode == 0x17);
6939 tcg_gen_add_i64(tcg_res[pass], tcg_op1, tcg_op2);
6940
6941 tcg_temp_free_i64(tcg_op1);
6942 tcg_temp_free_i64(tcg_op2);
6943 }
6944
6945 for (pass = 0; pass < 2; pass++) {
6946 write_vec_element(s, tcg_res[pass], rd, pass, MO_64);
6947 tcg_temp_free_i64(tcg_res[pass]);
6948 }
6949 } else {
6950 int maxpass = is_q ? 4 : 2;
6951 TCGv_i32 tcg_res[4];
6952
6953 for (pass = 0; pass < maxpass; pass++) {
6954 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
6955 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
6956 NeonGenTwoOpFn *genfn;
6957 int passreg = pass < (maxpass / 2) ? rn : rm;
6958 int passelt = (is_q && (pass & 1)) ? 2 : 0;
6959
6960 read_vec_element_i32(s, tcg_op1, passreg, passelt, MO_32);
6961 read_vec_element_i32(s, tcg_op2, passreg, passelt + 1, MO_32);
6962 tcg_res[pass] = tcg_temp_new_i32();
6963
6964 switch (opcode) {
6965 case 0x17: /* ADDP */
6966 {
6967 static NeonGenTwoOpFn * const fns[3] = {
6968 gen_helper_neon_padd_u8,
6969 gen_helper_neon_padd_u16,
6970 tcg_gen_add_i32,
6971 };
6972 genfn = fns[size];
6973 break;
6974 }
6975 case 0x14: /* SMAXP, UMAXP */
6976 {
6977 static NeonGenTwoOpFn * const fns[3][2] = {
6978 { gen_helper_neon_pmax_s8, gen_helper_neon_pmax_u8 },
6979 { gen_helper_neon_pmax_s16, gen_helper_neon_pmax_u16 },
6980 { gen_pmax_s32, gen_pmax_u32 },
6981 };
6982 genfn = fns[size][u];
6983 break;
6984 }
6985 case 0x15: /* SMINP, UMINP */
6986 {
6987 static NeonGenTwoOpFn * const fns[3][2] = {
6988 { gen_helper_neon_pmin_s8, gen_helper_neon_pmin_u8 },
6989 { gen_helper_neon_pmin_s16, gen_helper_neon_pmin_u16 },
6990 { gen_pmin_s32, gen_pmin_u32 },
6991 };
6992 genfn = fns[size][u];
6993 break;
6994 }
6995 default:
6996 g_assert_not_reached();
6997 }
6998
6999 genfn(tcg_res[pass], tcg_op1, tcg_op2);
7000
7001 tcg_temp_free_i32(tcg_op1);
7002 tcg_temp_free_i32(tcg_op2);
7003 }
7004
7005 for (pass = 0; pass < maxpass; pass++) {
7006 write_vec_element_i32(s, tcg_res[pass], rd, pass, MO_32);
7007 tcg_temp_free_i32(tcg_res[pass]);
7008 }
7009 if (!is_q) {
7010 clear_vec_high(s, rd);
7011 }
7012 }
Peter Maydellaab820f2014-01-16 13:00:47 +00007013}
7014
7015/* Floating point op subgroup of C3.6.16. */
7016static void disas_simd_3same_float(DisasContext *s, uint32_t insn)
7017{
Peter Maydell55bab512014-01-17 11:35:41 +00007018 /* For floating point ops, the U, size[1] and opcode bits
7019 * together indicate the operation. size[0] indicates single
7020 * or double.
7021 */
7022 int fpopcode = extract32(insn, 11, 5)
7023 | (extract32(insn, 23, 1) << 5)
7024 | (extract32(insn, 29, 1) << 6);
7025 int is_q = extract32(insn, 30, 1);
7026 int size = extract32(insn, 22, 1);
7027 int rm = extract32(insn, 16, 5);
7028 int rn = extract32(insn, 5, 5);
7029 int rd = extract32(insn, 0, 5);
7030
7031 int datasize = is_q ? 128 : 64;
7032 int esize = 32 << size;
7033 int elements = datasize / esize;
7034
7035 if (size == 1 && !is_q) {
7036 unallocated_encoding(s);
7037 return;
7038 }
7039
7040 switch (fpopcode) {
7041 case 0x58: /* FMAXNMP */
7042 case 0x5a: /* FADDP */
7043 case 0x5e: /* FMAXP */
7044 case 0x78: /* FMINNMP */
7045 case 0x7e: /* FMINP */
7046 /* pairwise ops */
7047 unsupported_encoding(s, insn);
7048 return;
7049 case 0x1b: /* FMULX */
7050 case 0x1c: /* FCMEQ */
7051 case 0x1f: /* FRECPS */
7052 case 0x3f: /* FRSQRTS */
7053 case 0x5c: /* FCMGE */
7054 case 0x5d: /* FACGE */
7055 case 0x7c: /* FCMGT */
7056 case 0x7d: /* FACGT */
7057 case 0x19: /* FMLA */
7058 case 0x39: /* FMLS */
7059 unsupported_encoding(s, insn);
7060 return;
7061 case 0x18: /* FMAXNM */
7062 case 0x1a: /* FADD */
7063 case 0x1e: /* FMAX */
7064 case 0x38: /* FMINNM */
7065 case 0x3a: /* FSUB */
7066 case 0x3e: /* FMIN */
7067 case 0x5b: /* FMUL */
7068 case 0x5f: /* FDIV */
7069 case 0x7a: /* FABD */
7070 handle_3same_float(s, size, elements, fpopcode, rd, rn, rm);
7071 return;
7072 default:
7073 unallocated_encoding(s);
7074 return;
7075 }
Peter Maydellaab820f2014-01-16 13:00:47 +00007076}
7077
7078/* Integer op subgroup of C3.6.16. */
7079static void disas_simd_3same_int(DisasContext *s, uint32_t insn)
7080{
Peter Maydell0d19b492014-01-23 13:13:41 +00007081 int is_q = extract32(insn, 30, 1);
7082 int u = extract32(insn, 29, 1);
7083 int size = extract32(insn, 22, 2);
7084 int opcode = extract32(insn, 11, 5);
7085 int rm = extract32(insn, 16, 5);
7086 int rn = extract32(insn, 5, 5);
7087 int rd = extract32(insn, 0, 5);
7088 int pass;
7089
7090 switch (opcode) {
7091 case 0x13: /* MUL, PMUL */
7092 if (u && size != 0) {
7093 unallocated_encoding(s);
7094 return;
7095 }
7096 /* fall through */
7097 case 0x0: /* SHADD, UHADD */
7098 case 0x2: /* SRHADD, URHADD */
7099 case 0x4: /* SHSUB, UHSUB */
7100 case 0xc: /* SMAX, UMAX */
7101 case 0xd: /* SMIN, UMIN */
7102 case 0xe: /* SABD, UABD */
7103 case 0xf: /* SABA, UABA */
7104 case 0x12: /* MLA, MLS */
7105 if (size == 3) {
7106 unallocated_encoding(s);
7107 return;
7108 }
Peter Maydellf6d7c6a2014-01-25 18:39:06 +00007109 break;
Peter Maydell0d19b492014-01-23 13:13:41 +00007110 case 0x16: /* SQDMULH, SQRDMULH */
7111 if (size == 0 || size == 3) {
7112 unallocated_encoding(s);
7113 return;
7114 }
Peter Maydellf6d7c6a2014-01-25 18:39:06 +00007115 break;
Peter Maydell0d19b492014-01-23 13:13:41 +00007116 default:
7117 if (size == 3 && !is_q) {
7118 unallocated_encoding(s);
7119 return;
7120 }
7121 break;
7122 }
7123
7124 if (size == 3) {
7125 for (pass = 0; pass < (is_q ? 2 : 1); pass++) {
7126 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
7127 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
7128 TCGv_i64 tcg_res = tcg_temp_new_i64();
7129
7130 read_vec_element(s, tcg_op1, rn, pass, MO_64);
7131 read_vec_element(s, tcg_op2, rm, pass, MO_64);
7132
7133 handle_3same_64(s, opcode, u, tcg_res, tcg_op1, tcg_op2);
7134
7135 write_vec_element(s, tcg_res, rd, pass, MO_64);
7136
7137 tcg_temp_free_i64(tcg_res);
7138 tcg_temp_free_i64(tcg_op1);
7139 tcg_temp_free_i64(tcg_op2);
7140 }
7141 } else {
7142 for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
7143 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
7144 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
7145 TCGv_i32 tcg_res = tcg_temp_new_i32();
Peter Maydell299088d2014-01-25 14:37:12 +00007146 NeonGenTwoOpFn *genfn = NULL;
7147 NeonGenTwoOpEnvFn *genenvfn = NULL;
Peter Maydell0d19b492014-01-23 13:13:41 +00007148
7149 read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
7150 read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
7151
7152 switch (opcode) {
Peter Maydellf6d7c6a2014-01-25 18:39:06 +00007153 case 0x0: /* SHADD, UHADD */
7154 {
7155 static NeonGenTwoOpFn * const fns[3][2] = {
7156 { gen_helper_neon_hadd_s8, gen_helper_neon_hadd_u8 },
7157 { gen_helper_neon_hadd_s16, gen_helper_neon_hadd_u16 },
7158 { gen_helper_neon_hadd_s32, gen_helper_neon_hadd_u32 },
7159 };
7160 genfn = fns[size][u];
7161 break;
7162 }
Peter Maydell299088d2014-01-25 14:37:12 +00007163 case 0x1: /* SQADD, UQADD */
7164 {
7165 static NeonGenTwoOpEnvFn * const fns[3][2] = {
7166 { gen_helper_neon_qadd_s8, gen_helper_neon_qadd_u8 },
7167 { gen_helper_neon_qadd_s16, gen_helper_neon_qadd_u16 },
7168 { gen_helper_neon_qadd_s32, gen_helper_neon_qadd_u32 },
7169 };
7170 genenvfn = fns[size][u];
7171 break;
7172 }
Peter Maydellf6d7c6a2014-01-25 18:39:06 +00007173 case 0x2: /* SRHADD, URHADD */
7174 {
7175 static NeonGenTwoOpFn * const fns[3][2] = {
7176 { gen_helper_neon_rhadd_s8, gen_helper_neon_rhadd_u8 },
7177 { gen_helper_neon_rhadd_s16, gen_helper_neon_rhadd_u16 },
7178 { gen_helper_neon_rhadd_s32, gen_helper_neon_rhadd_u32 },
7179 };
7180 genfn = fns[size][u];
7181 break;
7182 }
7183 case 0x4: /* SHSUB, UHSUB */
7184 {
7185 static NeonGenTwoOpFn * const fns[3][2] = {
7186 { gen_helper_neon_hsub_s8, gen_helper_neon_hsub_u8 },
7187 { gen_helper_neon_hsub_s16, gen_helper_neon_hsub_u16 },
7188 { gen_helper_neon_hsub_s32, gen_helper_neon_hsub_u32 },
7189 };
7190 genfn = fns[size][u];
7191 break;
7192 }
Peter Maydell299088d2014-01-25 14:37:12 +00007193 case 0x5: /* SQSUB, UQSUB */
7194 {
7195 static NeonGenTwoOpEnvFn * const fns[3][2] = {
7196 { gen_helper_neon_qsub_s8, gen_helper_neon_qsub_u8 },
7197 { gen_helper_neon_qsub_s16, gen_helper_neon_qsub_u16 },
7198 { gen_helper_neon_qsub_s32, gen_helper_neon_qsub_u32 },
7199 };
7200 genenvfn = fns[size][u];
7201 break;
7202 }
Peter Maydell0d19b492014-01-23 13:13:41 +00007203 case 0x6: /* CMGT, CMHI */
7204 {
7205 static NeonGenTwoOpFn * const fns[3][2] = {
7206 { gen_helper_neon_cgt_s8, gen_helper_neon_cgt_u8 },
7207 { gen_helper_neon_cgt_s16, gen_helper_neon_cgt_u16 },
7208 { gen_helper_neon_cgt_s32, gen_helper_neon_cgt_u32 },
7209 };
7210 genfn = fns[size][u];
7211 break;
7212 }
7213 case 0x7: /* CMGE, CMHS */
7214 {
7215 static NeonGenTwoOpFn * const fns[3][2] = {
7216 { gen_helper_neon_cge_s8, gen_helper_neon_cge_u8 },
7217 { gen_helper_neon_cge_s16, gen_helper_neon_cge_u16 },
7218 { gen_helper_neon_cge_s32, gen_helper_neon_cge_u32 },
7219 };
7220 genfn = fns[size][u];
7221 break;
7222 }
Peter Maydell299088d2014-01-25 14:37:12 +00007223 case 0x8: /* SSHL, USHL */
7224 {
7225 static NeonGenTwoOpFn * const fns[3][2] = {
Peter Maydellf6d7c6a2014-01-25 18:39:06 +00007226 { gen_helper_neon_shl_s8, gen_helper_neon_shl_u8 },
7227 { gen_helper_neon_shl_s16, gen_helper_neon_shl_u16 },
7228 { gen_helper_neon_shl_s32, gen_helper_neon_shl_u32 },
Peter Maydell299088d2014-01-25 14:37:12 +00007229 };
7230 genfn = fns[size][u];
7231 break;
7232 }
7233 case 0x9: /* SQSHL, UQSHL */
7234 {
7235 static NeonGenTwoOpEnvFn * const fns[3][2] = {
Peter Maydellf6d7c6a2014-01-25 18:39:06 +00007236 { gen_helper_neon_qshl_s8, gen_helper_neon_qshl_u8 },
7237 { gen_helper_neon_qshl_s16, gen_helper_neon_qshl_u16 },
7238 { gen_helper_neon_qshl_s32, gen_helper_neon_qshl_u32 },
Peter Maydell299088d2014-01-25 14:37:12 +00007239 };
7240 genenvfn = fns[size][u];
7241 break;
7242 }
7243 case 0xa: /* SRSHL, URSHL */
7244 {
7245 static NeonGenTwoOpFn * const fns[3][2] = {
Peter Maydellf6d7c6a2014-01-25 18:39:06 +00007246 { gen_helper_neon_rshl_s8, gen_helper_neon_rshl_u8 },
7247 { gen_helper_neon_rshl_s16, gen_helper_neon_rshl_u16 },
7248 { gen_helper_neon_rshl_s32, gen_helper_neon_rshl_u32 },
Peter Maydell299088d2014-01-25 14:37:12 +00007249 };
7250 genfn = fns[size][u];
7251 break;
7252 }
7253 case 0xb: /* SQRSHL, UQRSHL */
7254 {
7255 static NeonGenTwoOpEnvFn * const fns[3][2] = {
Peter Maydellf6d7c6a2014-01-25 18:39:06 +00007256 { gen_helper_neon_qrshl_s8, gen_helper_neon_qrshl_u8 },
7257 { gen_helper_neon_qrshl_s16, gen_helper_neon_qrshl_u16 },
7258 { gen_helper_neon_qrshl_s32, gen_helper_neon_qrshl_u32 },
Peter Maydell299088d2014-01-25 14:37:12 +00007259 };
7260 genenvfn = fns[size][u];
7261 break;
7262 }
Peter Maydellf6d7c6a2014-01-25 18:39:06 +00007263 case 0xc: /* SMAX, UMAX */
7264 {
7265 static NeonGenTwoOpFn * const fns[3][2] = {
7266 { gen_helper_neon_max_s8, gen_helper_neon_max_u8 },
7267 { gen_helper_neon_max_s16, gen_helper_neon_max_u16 },
7268 { gen_helper_neon_max_s32, gen_helper_neon_max_u32 },
7269 };
7270 genfn = fns[size][u];
7271 break;
7272 }
7273
7274 case 0xd: /* SMIN, UMIN */
7275 {
7276 static NeonGenTwoOpFn * const fns[3][2] = {
7277 { gen_helper_neon_min_s8, gen_helper_neon_min_u8 },
7278 { gen_helper_neon_min_s16, gen_helper_neon_min_u16 },
7279 { gen_helper_neon_min_s32, gen_helper_neon_min_u32 },
7280 };
7281 genfn = fns[size][u];
7282 break;
7283 }
7284 case 0xe: /* SABD, UABD */
7285 case 0xf: /* SABA, UABA */
7286 {
7287 static NeonGenTwoOpFn * const fns[3][2] = {
7288 { gen_helper_neon_abd_s8, gen_helper_neon_abd_u8 },
7289 { gen_helper_neon_abd_s16, gen_helper_neon_abd_u16 },
7290 { gen_helper_neon_abd_s32, gen_helper_neon_abd_u32 },
7291 };
7292 genfn = fns[size][u];
7293 break;
7294 }
Peter Maydell0d19b492014-01-23 13:13:41 +00007295 case 0x10: /* ADD, SUB */
7296 {
7297 static NeonGenTwoOpFn * const fns[3][2] = {
7298 { gen_helper_neon_add_u8, gen_helper_neon_sub_u8 },
7299 { gen_helper_neon_add_u16, gen_helper_neon_sub_u16 },
7300 { tcg_gen_add_i32, tcg_gen_sub_i32 },
7301 };
7302 genfn = fns[size][u];
7303 break;
7304 }
7305 case 0x11: /* CMTST, CMEQ */
7306 {
7307 static NeonGenTwoOpFn * const fns[3][2] = {
7308 { gen_helper_neon_tst_u8, gen_helper_neon_ceq_u8 },
7309 { gen_helper_neon_tst_u16, gen_helper_neon_ceq_u16 },
7310 { gen_helper_neon_tst_u32, gen_helper_neon_ceq_u32 },
7311 };
7312 genfn = fns[size][u];
7313 break;
7314 }
Peter Maydellf6d7c6a2014-01-25 18:39:06 +00007315 case 0x13: /* MUL, PMUL */
7316 if (u) {
7317 /* PMUL */
7318 assert(size == 0);
7319 genfn = gen_helper_neon_mul_p8;
7320 break;
7321 }
7322 /* fall through : MUL */
7323 case 0x12: /* MLA, MLS */
7324 {
7325 static NeonGenTwoOpFn * const fns[3] = {
7326 gen_helper_neon_mul_u8,
7327 gen_helper_neon_mul_u16,
7328 tcg_gen_mul_i32,
7329 };
7330 genfn = fns[size];
7331 break;
7332 }
7333 case 0x16: /* SQDMULH, SQRDMULH */
7334 {
7335 static NeonGenTwoOpEnvFn * const fns[2][2] = {
7336 { gen_helper_neon_qdmulh_s16, gen_helper_neon_qrdmulh_s16 },
7337 { gen_helper_neon_qdmulh_s32, gen_helper_neon_qrdmulh_s32 },
7338 };
7339 assert(size == 1 || size == 2);
7340 genenvfn = fns[size - 1][u];
7341 break;
7342 }
Peter Maydell0d19b492014-01-23 13:13:41 +00007343 default:
7344 g_assert_not_reached();
7345 }
7346
Peter Maydell299088d2014-01-25 14:37:12 +00007347 if (genenvfn) {
7348 genenvfn(tcg_res, cpu_env, tcg_op1, tcg_op2);
7349 } else {
7350 genfn(tcg_res, tcg_op1, tcg_op2);
7351 }
Peter Maydell0d19b492014-01-23 13:13:41 +00007352
Peter Maydellf6d7c6a2014-01-25 18:39:06 +00007353 if (opcode == 0xf || opcode == 0x12) {
7354 /* SABA, UABA, MLA, MLS: accumulating ops */
7355 static NeonGenTwoOpFn * const fns[3][2] = {
7356 { gen_helper_neon_add_u8, gen_helper_neon_sub_u8 },
7357 { gen_helper_neon_add_u16, gen_helper_neon_sub_u16 },
7358 { tcg_gen_add_i32, tcg_gen_sub_i32 },
7359 };
7360 bool is_sub = (opcode == 0x12 && u); /* MLS */
7361
7362 genfn = fns[size][is_sub];
7363 read_vec_element_i32(s, tcg_op1, rd, pass, MO_32);
7364 genfn(tcg_res, tcg_res, tcg_op1);
7365 }
7366
Peter Maydell0d19b492014-01-23 13:13:41 +00007367 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
7368
7369 tcg_temp_free_i32(tcg_res);
7370 tcg_temp_free_i32(tcg_op1);
7371 tcg_temp_free_i32(tcg_op2);
7372 }
7373 }
7374
7375 if (!is_q) {
7376 clear_vec_high(s, rd);
7377 }
Peter Maydellaab820f2014-01-16 13:00:47 +00007378}
7379
Alex Bennée94ad8702014-01-23 14:37:07 +00007380/* C3.6.16 AdvSIMD three same
7381 * 31 30 29 28 24 23 22 21 20 16 15 11 10 9 5 4 0
7382 * +---+---+---+-----------+------+---+------+--------+---+------+------+
7383 * | 0 | Q | U | 0 1 1 1 0 | size | 1 | Rm | opcode | 1 | Rn | Rd |
7384 * +---+---+---+-----------+------+---+------+--------+---+------+------+
7385 */
7386static void disas_simd_three_reg_same(DisasContext *s, uint32_t insn)
7387{
Peter Maydellaab820f2014-01-16 13:00:47 +00007388 int opcode = extract32(insn, 11, 5);
7389
7390 switch (opcode) {
7391 case 0x3: /* logic ops */
7392 disas_simd_3same_logic(s, insn);
7393 break;
7394 case 0x17: /* ADDP */
7395 case 0x14: /* SMAXP, UMAXP */
7396 case 0x15: /* SMINP, UMINP */
7397 /* Pairwise operations */
7398 disas_simd_3same_pair(s, insn);
7399 break;
7400 case 0x18 ... 0x31:
7401 /* floating point ops, sz[1] and U are part of opcode */
7402 disas_simd_3same_float(s, insn);
7403 break;
7404 default:
7405 disas_simd_3same_int(s, insn);
7406 break;
7407 }
Alex Bennée94ad8702014-01-23 14:37:07 +00007408}
7409
Peter Maydellf10708d2014-01-26 14:06:50 +00007410static void handle_2misc_narrow(DisasContext *s, int opcode, bool u, bool is_q,
7411 int size, int rn, int rd)
7412{
7413 /* Handle 2-reg-misc ops which are narrowing (so each 2*size element
7414 * in the source becomes a size element in the destination).
7415 */
7416 int pass;
7417 TCGv_i32 tcg_res[2];
7418 int destelt = is_q ? 2 : 0;
7419
7420 for (pass = 0; pass < 2; pass++) {
7421 TCGv_i64 tcg_op = tcg_temp_new_i64();
7422 NeonGenNarrowFn *genfn = NULL;
7423 NeonGenNarrowEnvFn *genenvfn = NULL;
7424
7425 read_vec_element(s, tcg_op, rn, pass, MO_64);
7426 tcg_res[pass] = tcg_temp_new_i32();
7427
7428 switch (opcode) {
7429 case 0x12: /* XTN, SQXTUN */
7430 {
7431 static NeonGenNarrowFn * const xtnfns[3] = {
7432 gen_helper_neon_narrow_u8,
7433 gen_helper_neon_narrow_u16,
7434 tcg_gen_trunc_i64_i32,
7435 };
7436 static NeonGenNarrowEnvFn * const sqxtunfns[3] = {
7437 gen_helper_neon_unarrow_sat8,
7438 gen_helper_neon_unarrow_sat16,
7439 gen_helper_neon_unarrow_sat32,
7440 };
7441 if (u) {
7442 genenvfn = sqxtunfns[size];
7443 } else {
7444 genfn = xtnfns[size];
7445 }
7446 break;
7447 }
7448 case 0x14: /* SQXTN, UQXTN */
7449 {
7450 static NeonGenNarrowEnvFn * const fns[3][2] = {
7451 { gen_helper_neon_narrow_sat_s8,
7452 gen_helper_neon_narrow_sat_u8 },
7453 { gen_helper_neon_narrow_sat_s16,
7454 gen_helper_neon_narrow_sat_u16 },
7455 { gen_helper_neon_narrow_sat_s32,
7456 gen_helper_neon_narrow_sat_u32 },
7457 };
7458 genenvfn = fns[size][u];
7459 break;
7460 }
7461 default:
7462 g_assert_not_reached();
7463 }
7464
7465 if (genfn) {
7466 genfn(tcg_res[pass], tcg_op);
7467 } else {
7468 genenvfn(tcg_res[pass], cpu_env, tcg_op);
7469 }
7470
7471 tcg_temp_free_i64(tcg_op);
7472 }
7473
7474 for (pass = 0; pass < 2; pass++) {
7475 write_vec_element_i32(s, tcg_res[pass], rd, destelt + pass, MO_32);
7476 tcg_temp_free_i32(tcg_res[pass]);
7477 }
7478 if (!is_q) {
7479 clear_vec_high(s, rd);
7480 }
7481}
7482
Alex Bennée41f25192014-01-26 00:33:46 +00007483static void handle_rev(DisasContext *s, int opcode, bool u,
7484 bool is_q, int size, int rn, int rd)
7485{
7486 int op = (opcode & 1) << 1 | u;
7487 int opsz = op + size;
7488 int ibits = 3 - opsz;
7489 int revmask = (1 << ibits) - 1;
7490 int dsize = is_q ? 128 : 64;
7491 int esize = 8 << size;
7492 int elements = dsize / esize;
7493 int i;
7494 TCGv_i64 tcg_rd_hi, tcg_rd, tcg_rn;
7495
7496 if (opsz >= 3) {
7497 unallocated_encoding(s);
7498 return;
7499 }
7500
7501 tcg_rn = tcg_temp_new_i64();
7502 tcg_rd = tcg_const_i64(0);
7503 tcg_rd_hi = tcg_const_i64(0);
7504
7505 for (i = 0; i < elements; i++) {
7506 int e_rev = (i & 0xf) ^ revmask;
7507 int off = e_rev * esize;
7508 read_vec_element(s, tcg_rn, rn, i, size);
7509 if (off >= 64) {
7510 tcg_gen_deposit_i64(tcg_rd_hi, tcg_rd_hi, tcg_rn, off - 64, esize);
7511 } else {
7512 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_rn, off, esize);
7513 }
7514 }
7515 write_vec_element(s, tcg_rd, rd, 0, MO_64);
7516 write_vec_element(s, tcg_rd_hi, rd, 1, MO_64);
7517
7518 tcg_temp_free_i64(tcg_rd_hi);
7519 tcg_temp_free_i64(tcg_rd);
7520 tcg_temp_free_i64(tcg_rn);
7521}
7522
Alex Bennée94ad8702014-01-23 14:37:07 +00007523/* C3.6.17 AdvSIMD two reg misc
7524 * 31 30 29 28 24 23 22 21 17 16 12 11 10 9 5 4 0
7525 * +---+---+---+-----------+------+-----------+--------+-----+------+------+
7526 * | 0 | Q | U | 0 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 | Rn | Rd |
7527 * +---+---+---+-----------+------+-----------+--------+-----+------+------+
7528 */
7529static void disas_simd_two_reg_misc(DisasContext *s, uint32_t insn)
7530{
Peter Maydell3ae07092014-01-25 23:55:44 +00007531 int size = extract32(insn, 22, 2);
7532 int opcode = extract32(insn, 12, 5);
7533 bool u = extract32(insn, 29, 1);
7534 bool is_q = extract32(insn, 30, 1);
Peter Maydellfdb8bdf2014-01-26 01:05:54 +00007535 int rn = extract32(insn, 5, 5);
7536 int rd = extract32(insn, 0, 5);
Peter Maydell3ae07092014-01-25 23:55:44 +00007537
7538 switch (opcode) {
7539 case 0x0: /* REV64, REV32 */
7540 case 0x1: /* REV16 */
Alex Bennée41f25192014-01-26 00:33:46 +00007541 handle_rev(s, opcode, u, is_q, size, rn, rd);
Peter Maydell3ae07092014-01-25 23:55:44 +00007542 return;
Peter Maydell6aa24912014-01-26 00:53:25 +00007543 case 0x5: /* CNT, NOT, RBIT */
7544 if (u && size == 0) {
7545 /* NOT: adjust size so we can use the 64-bits-at-a-time loop. */
7546 size = 3;
7547 break;
7548 } else if (u && size == 1) {
7549 /* RBIT */
7550 break;
7551 } else if (!u && size == 0) {
7552 /* CNT */
7553 break;
Peter Maydell3ae07092014-01-25 23:55:44 +00007554 }
Peter Maydell6aa24912014-01-26 00:53:25 +00007555 unallocated_encoding(s);
Peter Maydell3ae07092014-01-25 23:55:44 +00007556 return;
Peter Maydellf10708d2014-01-26 14:06:50 +00007557 case 0x12: /* XTN, XTN2, SQXTUN, SQXTUN2 */
7558 case 0x14: /* SQXTN, SQXTN2, UQXTN, UQXTN2 */
7559 if (size == 3) {
7560 unallocated_encoding(s);
7561 return;
7562 }
7563 handle_2misc_narrow(s, opcode, u, is_q, size, rn, rd);
7564 return;
Peter Maydell3ae07092014-01-25 23:55:44 +00007565 case 0x2: /* SADDLP, UADDLP */
7566 case 0x4: /* CLS, CLZ */
7567 case 0x6: /* SADALP, UADALP */
Peter Maydell3ae07092014-01-25 23:55:44 +00007568 if (size == 3) {
7569 unallocated_encoding(s);
7570 return;
7571 }
7572 unsupported_encoding(s, insn);
7573 return;
7574 case 0x13: /* SHLL, SHLL2 */
7575 if (u == 0 || size == 3) {
7576 unallocated_encoding(s);
7577 return;
7578 }
7579 unsupported_encoding(s, insn);
7580 return;
7581 case 0xa: /* CMLT */
7582 if (u == 1) {
7583 unallocated_encoding(s);
7584 return;
7585 }
7586 /* fall through */
Peter Maydell3ae07092014-01-25 23:55:44 +00007587 case 0x8: /* CMGT, CMGE */
7588 case 0x9: /* CMEQ, CMLE */
7589 case 0xb: /* ABS, NEG */
7590 if (size == 3 && !is_q) {
7591 unallocated_encoding(s);
7592 return;
7593 }
Peter Maydellfdb8bdf2014-01-26 01:05:54 +00007594 break;
7595 case 0x3: /* SUQADD, USQADD */
7596 case 0x7: /* SQABS, SQNEG */
7597 if (size == 3 && !is_q) {
7598 unallocated_encoding(s);
7599 return;
7600 }
Peter Maydell3ae07092014-01-25 23:55:44 +00007601 unsupported_encoding(s, insn);
7602 return;
7603 case 0xc ... 0xf:
7604 case 0x16 ... 0x1d:
7605 case 0x1f:
7606 {
7607 /* Floating point: U, size[1] and opcode indicate operation;
7608 * size[0] indicates single or double precision.
7609 */
7610 opcode |= (extract32(size, 1, 1) << 5) | (u << 6);
7611 size = extract32(size, 0, 1) ? 3 : 2;
7612 switch (opcode) {
Peter Maydell825c6e92014-01-26 18:40:44 +00007613 case 0x2f: /* FABS */
7614 case 0x6f: /* FNEG */
7615 if (size == 3 && !is_q) {
7616 unallocated_encoding(s);
7617 return;
7618 }
7619 break;
Peter Maydell3ae07092014-01-25 23:55:44 +00007620 case 0x16: /* FCVTN, FCVTN2 */
7621 case 0x17: /* FCVTL, FCVTL2 */
7622 case 0x18: /* FRINTN */
7623 case 0x19: /* FRINTM */
7624 case 0x1a: /* FCVTNS */
7625 case 0x1b: /* FCVTMS */
7626 case 0x1c: /* FCVTAS */
7627 case 0x1d: /* SCVTF */
7628 case 0x2c: /* FCMGT (zero) */
7629 case 0x2d: /* FCMEQ (zero) */
7630 case 0x2e: /* FCMLT (zero) */
Peter Maydell3ae07092014-01-25 23:55:44 +00007631 case 0x38: /* FRINTP */
7632 case 0x39: /* FRINTZ */
7633 case 0x3a: /* FCVTPS */
7634 case 0x3b: /* FCVTZS */
7635 case 0x3c: /* URECPE */
7636 case 0x3d: /* FRECPE */
7637 case 0x56: /* FCVTXN, FCVTXN2 */
7638 case 0x58: /* FRINTA */
7639 case 0x59: /* FRINTX */
7640 case 0x5a: /* FCVTNU */
7641 case 0x5b: /* FCVTMU */
7642 case 0x5c: /* FCVTAU */
7643 case 0x5d: /* UCVTF */
7644 case 0x6c: /* FCMGE (zero) */
7645 case 0x6d: /* FCMLE (zero) */
Peter Maydell3ae07092014-01-25 23:55:44 +00007646 case 0x79: /* FRINTI */
7647 case 0x7a: /* FCVTPU */
7648 case 0x7b: /* FCVTZU */
7649 case 0x7c: /* URSQRTE */
7650 case 0x7d: /* FRSQRTE */
7651 case 0x7f: /* FSQRT */
7652 unsupported_encoding(s, insn);
7653 return;
7654 default:
7655 unallocated_encoding(s);
7656 return;
7657 }
7658 break;
7659 }
7660 default:
7661 unallocated_encoding(s);
7662 return;
7663 }
Peter Maydellfdb8bdf2014-01-26 01:05:54 +00007664
7665 if (size == 3) {
7666 /* All 64-bit element operations can be shared with scalar 2misc */
7667 int pass;
7668
7669 for (pass = 0; pass < (is_q ? 2 : 1); pass++) {
7670 TCGv_i64 tcg_op = tcg_temp_new_i64();
7671 TCGv_i64 tcg_res = tcg_temp_new_i64();
7672
7673 read_vec_element(s, tcg_op, rn, pass, MO_64);
7674
7675 handle_2misc_64(s, opcode, u, tcg_res, tcg_op);
7676
7677 write_vec_element(s, tcg_res, rd, pass, MO_64);
7678
7679 tcg_temp_free_i64(tcg_res);
7680 tcg_temp_free_i64(tcg_op);
7681 }
7682 } else {
7683 int pass;
7684
7685 for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
7686 TCGv_i32 tcg_op = tcg_temp_new_i32();
7687 TCGv_i32 tcg_res = tcg_temp_new_i32();
7688 TCGCond cond;
7689
7690 read_vec_element_i32(s, tcg_op, rn, pass, MO_32);
7691
7692 if (size == 2) {
7693 /* Special cases for 32 bit elements */
7694 switch (opcode) {
7695 case 0xa: /* CMLT */
7696 /* 32 bit integer comparison against zero, result is
7697 * test ? (2^32 - 1) : 0. We implement via setcond(test)
7698 * and inverting.
7699 */
7700 cond = TCG_COND_LT;
7701 do_cmop:
7702 tcg_gen_setcondi_i32(cond, tcg_res, tcg_op, 0);
7703 tcg_gen_neg_i32(tcg_res, tcg_res);
7704 break;
7705 case 0x8: /* CMGT, CMGE */
7706 cond = u ? TCG_COND_GE : TCG_COND_GT;
7707 goto do_cmop;
7708 case 0x9: /* CMEQ, CMLE */
7709 cond = u ? TCG_COND_LE : TCG_COND_EQ;
7710 goto do_cmop;
7711 case 0xb: /* ABS, NEG */
7712 if (u) {
7713 tcg_gen_neg_i32(tcg_res, tcg_op);
7714 } else {
7715 TCGv_i32 tcg_zero = tcg_const_i32(0);
7716 tcg_gen_neg_i32(tcg_res, tcg_op);
7717 tcg_gen_movcond_i32(TCG_COND_GT, tcg_res, tcg_op,
7718 tcg_zero, tcg_op, tcg_res);
7719 tcg_temp_free_i32(tcg_zero);
7720 }
7721 break;
Peter Maydell825c6e92014-01-26 18:40:44 +00007722 case 0x2f: /* FABS */
7723 gen_helper_vfp_abss(tcg_res, tcg_op);
7724 break;
7725 case 0x6f: /* FNEG */
7726 gen_helper_vfp_negs(tcg_res, tcg_op);
7727 break;
Peter Maydellfdb8bdf2014-01-26 01:05:54 +00007728 default:
7729 g_assert_not_reached();
7730 }
7731 } else {
7732 /* Use helpers for 8 and 16 bit elements */
7733 switch (opcode) {
Peter Maydell6aa24912014-01-26 00:53:25 +00007734 case 0x5: /* CNT, RBIT */
7735 /* For these two insns size is part of the opcode specifier
7736 * (handled earlier); they always operate on byte elements.
7737 */
7738 if (u) {
7739 gen_helper_neon_rbit_u8(tcg_res, tcg_op);
7740 } else {
7741 gen_helper_neon_cnt_u8(tcg_res, tcg_op);
7742 }
7743 break;
Peter Maydellfdb8bdf2014-01-26 01:05:54 +00007744 case 0x8: /* CMGT, CMGE */
7745 case 0x9: /* CMEQ, CMLE */
7746 case 0xa: /* CMLT */
7747 {
7748 static NeonGenTwoOpFn * const fns[3][2] = {
7749 { gen_helper_neon_cgt_s8, gen_helper_neon_cgt_s16 },
7750 { gen_helper_neon_cge_s8, gen_helper_neon_cge_s16 },
7751 { gen_helper_neon_ceq_u8, gen_helper_neon_ceq_u16 },
7752 };
7753 NeonGenTwoOpFn *genfn;
7754 int comp;
7755 bool reverse;
7756 TCGv_i32 tcg_zero = tcg_const_i32(0);
7757
7758 /* comp = index into [CMGT, CMGE, CMEQ, CMLE, CMLT] */
7759 comp = (opcode - 0x8) * 2 + u;
7760 /* ...but LE, LT are implemented as reverse GE, GT */
7761 reverse = (comp > 2);
7762 if (reverse) {
7763 comp = 4 - comp;
7764 }
7765 genfn = fns[comp][size];
7766 if (reverse) {
7767 genfn(tcg_res, tcg_zero, tcg_op);
7768 } else {
7769 genfn(tcg_res, tcg_op, tcg_zero);
7770 }
7771 tcg_temp_free_i32(tcg_zero);
7772 break;
7773 }
7774 case 0xb: /* ABS, NEG */
7775 if (u) {
7776 TCGv_i32 tcg_zero = tcg_const_i32(0);
7777 if (size) {
7778 gen_helper_neon_sub_u16(tcg_res, tcg_zero, tcg_op);
7779 } else {
7780 gen_helper_neon_sub_u8(tcg_res, tcg_zero, tcg_op);
7781 }
7782 tcg_temp_free_i32(tcg_zero);
7783 } else {
7784 if (size) {
7785 gen_helper_neon_abs_s16(tcg_res, tcg_op);
7786 } else {
7787 gen_helper_neon_abs_s8(tcg_res, tcg_op);
7788 }
7789 }
7790 break;
7791 default:
7792 g_assert_not_reached();
7793 }
7794 }
7795
7796 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
7797
7798 tcg_temp_free_i32(tcg_res);
7799 tcg_temp_free_i32(tcg_op);
7800 }
7801 }
7802 if (!is_q) {
7803 clear_vec_high(s, rd);
7804 }
Alex Bennée94ad8702014-01-23 14:37:07 +00007805}
7806
7807/* C3.6.18 AdvSIMD vector x indexed element
7808 * 31 30 29 28 24 23 22 21 20 19 16 15 12 11 10 9 5 4 0
7809 * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
7810 * | 0 | Q | U | 0 1 1 1 1 | size | L | M | Rm | opc | H | 0 | Rn | Rd |
7811 * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
7812 */
7813static void disas_simd_indexed_vector(DisasContext *s, uint32_t insn)
7814{
7815 unsupported_encoding(s, insn);
7816}
7817
7818/* C3.6.19 Crypto AES
7819 * 31 24 23 22 21 17 16 12 11 10 9 5 4 0
7820 * +-----------------+------+-----------+--------+-----+------+------+
7821 * | 0 1 0 0 1 1 1 0 | size | 1 0 1 0 0 | opcode | 1 0 | Rn | Rd |
7822 * +-----------------+------+-----------+--------+-----+------+------+
7823 */
7824static void disas_crypto_aes(DisasContext *s, uint32_t insn)
7825{
7826 unsupported_encoding(s, insn);
7827}
7828
7829/* C3.6.20 Crypto three-reg SHA
7830 * 31 24 23 22 21 20 16 15 14 12 11 10 9 5 4 0
7831 * +-----------------+------+---+------+---+--------+-----+------+------+
7832 * | 0 1 0 1 1 1 1 0 | size | 0 | Rm | 0 | opcode | 0 0 | Rn | Rd |
7833 * +-----------------+------+---+------+---+--------+-----+------+------+
7834 */
7835static void disas_crypto_three_reg_sha(DisasContext *s, uint32_t insn)
7836{
7837 unsupported_encoding(s, insn);
7838}
7839
7840/* C3.6.21 Crypto two-reg SHA
7841 * 31 24 23 22 21 17 16 12 11 10 9 5 4 0
7842 * +-----------------+------+-----------+--------+-----+------+------+
7843 * | 0 1 0 1 1 1 1 0 | size | 1 0 1 0 0 | opcode | 1 0 | Rn | Rd |
7844 * +-----------------+------+-----------+--------+-----+------+------+
7845 */
7846static void disas_crypto_two_reg_sha(DisasContext *s, uint32_t insn)
7847{
7848 unsupported_encoding(s, insn);
7849}
7850
7851/* C3.6 Data processing - SIMD, inc Crypto
7852 *
7853 * As the decode gets a little complex we are using a table based
7854 * approach for this part of the decode.
7855 */
7856static const AArch64DecodeTable data_proc_simd[] = {
7857 /* pattern , mask , fn */
7858 { 0x0e200400, 0x9f200400, disas_simd_three_reg_same },
7859 { 0x0e200000, 0x9f200c00, disas_simd_three_reg_diff },
7860 { 0x0e200800, 0x9f3e0c00, disas_simd_two_reg_misc },
7861 { 0x0e300800, 0x9f3e0c00, disas_simd_across_lanes },
7862 { 0x0e000400, 0x9fe08400, disas_simd_copy },
7863 { 0x0f000000, 0x9f000400, disas_simd_indexed_vector },
7864 /* simd_mod_imm decode is a subset of simd_shift_imm, so must precede it */
7865 { 0x0f000400, 0x9ff80400, disas_simd_mod_imm },
7866 { 0x0f000400, 0x9f800400, disas_simd_shift_imm },
7867 { 0x0e000000, 0xbf208c00, disas_simd_tb },
7868 { 0x0e000800, 0xbf208c00, disas_simd_zip_trn },
7869 { 0x2e000000, 0xbf208400, disas_simd_ext },
7870 { 0x5e200400, 0xdf200400, disas_simd_scalar_three_reg_same },
7871 { 0x5e200000, 0xdf200c00, disas_simd_scalar_three_reg_diff },
7872 { 0x5e200800, 0xdf3e0c00, disas_simd_scalar_two_reg_misc },
7873 { 0x5e300800, 0xdf3e0c00, disas_simd_scalar_pairwise },
7874 { 0x5e000400, 0xdfe08400, disas_simd_scalar_copy },
7875 { 0x5f000000, 0xdf000400, disas_simd_scalar_indexed },
7876 { 0x5f000400, 0xdf800400, disas_simd_scalar_shift_imm },
7877 { 0x4e280800, 0xff3e0c00, disas_crypto_aes },
7878 { 0x5e000000, 0xff208c00, disas_crypto_three_reg_sha },
7879 { 0x5e280800, 0xff3e0c00, disas_crypto_two_reg_sha },
7880 { 0x00000000, 0x00000000, NULL }
7881};
7882
Peter Maydellfaa0ba42013-12-23 23:27:30 +00007883static void disas_data_proc_simd(DisasContext *s, uint32_t insn)
7884{
7885 /* Note that this is called with all non-FP cases from
7886 * table C3-6 so it must UNDEF for entries not specifically
7887 * allocated to instructions in that table.
7888 */
Alex Bennée94ad8702014-01-23 14:37:07 +00007889 AArch64DecodeFn *fn = lookup_disas_fn(&data_proc_simd[0], insn);
7890 if (fn) {
7891 fn(s, insn);
7892 } else {
7893 unallocated_encoding(s);
7894 }
Peter Maydellfaa0ba42013-12-23 23:27:30 +00007895}
7896
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00007897/* C3.6 Data processing - SIMD and floating point */
7898static void disas_data_proc_simd_fp(DisasContext *s, uint32_t insn)
7899{
Peter Maydellfaa0ba42013-12-23 23:27:30 +00007900 if (extract32(insn, 28, 1) == 1 && extract32(insn, 30, 1) == 0) {
7901 disas_data_proc_fp(s, insn);
7902 } else {
7903 /* SIMD, including crypto */
7904 disas_data_proc_simd(s, insn);
7905 }
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00007906}
7907
7908/* C3.1 A64 instruction index by encoding */
Peter Maydell40f860c2013-12-17 19:42:31 +00007909static void disas_a64_insn(CPUARMState *env, DisasContext *s)
Alexander Graf14ade102013-09-03 20:12:10 +01007910{
7911 uint32_t insn;
7912
7913 insn = arm_ldl_code(env, s->pc, s->bswap_code);
7914 s->insn = insn;
7915 s->pc += 4;
7916
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00007917 switch (extract32(insn, 25, 4)) {
7918 case 0x0: case 0x1: case 0x2: case 0x3: /* UNALLOCATED */
Alexander Graf14ade102013-09-03 20:12:10 +01007919 unallocated_encoding(s);
7920 break;
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00007921 case 0x8: case 0x9: /* Data processing - immediate */
7922 disas_data_proc_imm(s, insn);
7923 break;
7924 case 0xa: case 0xb: /* Branch, exception generation and system insns */
7925 disas_b_exc_sys(s, insn);
7926 break;
7927 case 0x4:
7928 case 0x6:
7929 case 0xc:
7930 case 0xe: /* Loads and stores */
7931 disas_ldst(s, insn);
7932 break;
7933 case 0x5:
7934 case 0xd: /* Data processing - register */
7935 disas_data_proc_reg(s, insn);
7936 break;
7937 case 0x7:
7938 case 0xf: /* Data processing - SIMD and floating point */
7939 disas_data_proc_simd_fp(s, insn);
7940 break;
7941 default:
7942 assert(FALSE); /* all 15 cases should be handled above */
7943 break;
Alexander Graf14ade102013-09-03 20:12:10 +01007944 }
Alexander Graf11e169d2013-12-17 19:42:32 +00007945
7946 /* if we allocated any temporaries, free them here */
7947 free_tmp_a64(s);
Peter Maydell40f860c2013-12-17 19:42:31 +00007948}
Alexander Graf14ade102013-09-03 20:12:10 +01007949
Peter Maydell40f860c2013-12-17 19:42:31 +00007950void gen_intermediate_code_internal_a64(ARMCPU *cpu,
7951 TranslationBlock *tb,
7952 bool search_pc)
7953{
7954 CPUState *cs = CPU(cpu);
7955 CPUARMState *env = &cpu->env;
7956 DisasContext dc1, *dc = &dc1;
7957 CPUBreakpoint *bp;
7958 uint16_t *gen_opc_end;
7959 int j, lj;
7960 target_ulong pc_start;
7961 target_ulong next_page_start;
7962 int num_insns;
7963 int max_insns;
7964
7965 pc_start = tb->pc;
7966
7967 dc->tb = tb;
7968
7969 gen_opc_end = tcg_ctx.gen_opc_buf + OPC_MAX_SIZE;
7970
7971 dc->is_jmp = DISAS_NEXT;
7972 dc->pc = pc_start;
7973 dc->singlestep_enabled = cs->singlestep_enabled;
7974 dc->condjmp = 0;
7975
7976 dc->aarch64 = 1;
7977 dc->thumb = 0;
7978 dc->bswap_code = 0;
7979 dc->condexec_mask = 0;
7980 dc->condexec_cond = 0;
7981#if !defined(CONFIG_USER_ONLY)
7982 dc->user = 0;
7983#endif
7984 dc->vfp_enabled = 0;
7985 dc->vec_len = 0;
7986 dc->vec_stride = 0;
Peter Maydell60322b32014-01-04 22:15:44 +00007987 dc->cp_regs = cpu->cp_regs;
7988 dc->current_pl = arm_current_pl(env);
Peter Maydell40f860c2013-12-17 19:42:31 +00007989
Alexander Graf11e169d2013-12-17 19:42:32 +00007990 init_tmp_a64_array(dc);
7991
Peter Maydell40f860c2013-12-17 19:42:31 +00007992 next_page_start = (pc_start & TARGET_PAGE_MASK) + TARGET_PAGE_SIZE;
7993 lj = -1;
7994 num_insns = 0;
7995 max_insns = tb->cflags & CF_COUNT_MASK;
7996 if (max_insns == 0) {
7997 max_insns = CF_COUNT_MASK;
7998 }
7999
8000 gen_tb_start();
8001
8002 tcg_clear_temp_count();
8003
8004 do {
8005 if (unlikely(!QTAILQ_EMPTY(&env->breakpoints))) {
8006 QTAILQ_FOREACH(bp, &env->breakpoints, entry) {
8007 if (bp->pc == dc->pc) {
8008 gen_exception_insn(dc, 0, EXCP_DEBUG);
8009 /* Advance PC so that clearing the breakpoint will
8010 invalidate this TB. */
8011 dc->pc += 2;
8012 goto done_generating;
8013 }
8014 }
8015 }
8016
8017 if (search_pc) {
8018 j = tcg_ctx.gen_opc_ptr - tcg_ctx.gen_opc_buf;
8019 if (lj < j) {
8020 lj++;
8021 while (lj < j) {
8022 tcg_ctx.gen_opc_instr_start[lj++] = 0;
8023 }
8024 }
8025 tcg_ctx.gen_opc_pc[lj] = dc->pc;
8026 tcg_ctx.gen_opc_instr_start[lj] = 1;
8027 tcg_ctx.gen_opc_icount[lj] = num_insns;
8028 }
8029
8030 if (num_insns + 1 == max_insns && (tb->cflags & CF_LAST_IO)) {
8031 gen_io_start();
8032 }
8033
8034 if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP | CPU_LOG_TB_OP_OPT))) {
8035 tcg_gen_debug_insn_start(dc->pc);
8036 }
8037
8038 disas_a64_insn(env, dc);
8039
8040 if (tcg_check_temp_count()) {
8041 fprintf(stderr, "TCG temporary leak before "TARGET_FMT_lx"\n",
8042 dc->pc);
8043 }
8044
8045 /* Translation stops when a conditional branch is encountered.
8046 * Otherwise the subsequent code could get translated several times.
8047 * Also stop translation when a page boundary is reached. This
8048 * ensures prefetch aborts occur at the right place.
8049 */
8050 num_insns++;
8051 } while (!dc->is_jmp && tcg_ctx.gen_opc_ptr < gen_opc_end &&
8052 !cs->singlestep_enabled &&
8053 !singlestep &&
8054 dc->pc < next_page_start &&
8055 num_insns < max_insns);
8056
8057 if (tb->cflags & CF_LAST_IO) {
8058 gen_io_end();
8059 }
8060
8061 if (unlikely(cs->singlestep_enabled) && dc->is_jmp != DISAS_EXC) {
8062 /* Note that this means single stepping WFI doesn't halt the CPU.
8063 * For conditional branch insns this is harmless unreachable code as
8064 * gen_goto_tb() has already handled emitting the debug exception
8065 * (and thus a tb-jump is not possible when singlestepping).
8066 */
8067 assert(dc->is_jmp != DISAS_TB_JUMP);
8068 if (dc->is_jmp != DISAS_JUMP) {
8069 gen_a64_set_pc_im(dc->pc);
8070 }
8071 gen_exception(EXCP_DEBUG);
8072 } else {
8073 switch (dc->is_jmp) {
8074 case DISAS_NEXT:
8075 gen_goto_tb(dc, 1, dc->pc);
8076 break;
8077 default:
Peter Maydell40f860c2013-12-17 19:42:31 +00008078 case DISAS_UPDATE:
Peter Maydellfea50522014-01-04 22:15:45 +00008079 gen_a64_set_pc_im(dc->pc);
8080 /* fall through */
8081 case DISAS_JUMP:
Peter Maydell40f860c2013-12-17 19:42:31 +00008082 /* indicate that the hash table must be used to find the next TB */
8083 tcg_gen_exit_tb(0);
8084 break;
8085 case DISAS_TB_JUMP:
8086 case DISAS_EXC:
8087 case DISAS_SWI:
8088 break;
8089 case DISAS_WFI:
8090 /* This is a special case because we don't want to just halt the CPU
8091 * if trying to debug across a WFI.
8092 */
8093 gen_helper_wfi(cpu_env);
8094 break;
8095 }
8096 }
8097
8098done_generating:
8099 gen_tb_end(tb, num_insns);
8100 *tcg_ctx.gen_opc_ptr = INDEX_op_end;
8101
8102#ifdef DEBUG_DISAS
8103 if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)) {
8104 qemu_log("----------------\n");
8105 qemu_log("IN: %s\n", lookup_symbol(pc_start));
8106 log_target_disas(env, pc_start, dc->pc - pc_start,
8107 dc->thumb | (dc->bswap_code << 1));
8108 qemu_log("\n");
8109 }
8110#endif
8111 if (search_pc) {
8112 j = tcg_ctx.gen_opc_ptr - tcg_ctx.gen_opc_buf;
8113 lj++;
8114 while (lj <= j) {
8115 tcg_ctx.gen_opc_instr_start[lj++] = 0;
8116 }
8117 } else {
8118 tb->size = dc->pc - pc_start;
8119 tb->icount = num_insns;
Alexander Graf14ade102013-09-03 20:12:10 +01008120 }
8121}