blob: 6cdb8fc672dea2dd4321eab9f23c2ddc4c042b97 [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);
77
Alexander Graf14ade102013-09-03 20:12:10 +010078/* initialize TCG globals. */
79void a64_translate_init(void)
80{
81 int i;
82
83 cpu_pc = tcg_global_mem_new_i64(TCG_AREG0,
84 offsetof(CPUARMState, pc),
85 "pc");
86 for (i = 0; i < 32; i++) {
87 cpu_X[i] = tcg_global_mem_new_i64(TCG_AREG0,
88 offsetof(CPUARMState, xregs[i]),
89 regnames[i]);
90 }
91
Alexander Graf832ffa12013-12-17 19:42:34 +000092 cpu_NF = tcg_global_mem_new_i32(TCG_AREG0, offsetof(CPUARMState, NF), "NF");
93 cpu_ZF = tcg_global_mem_new_i32(TCG_AREG0, offsetof(CPUARMState, ZF), "ZF");
94 cpu_CF = tcg_global_mem_new_i32(TCG_AREG0, offsetof(CPUARMState, CF), "CF");
95 cpu_VF = tcg_global_mem_new_i32(TCG_AREG0, offsetof(CPUARMState, VF), "VF");
Michael Matzfa2ef212014-01-04 22:15:47 +000096
97 cpu_exclusive_addr = tcg_global_mem_new_i64(TCG_AREG0,
98 offsetof(CPUARMState, exclusive_addr), "exclusive_addr");
99 cpu_exclusive_val = tcg_global_mem_new_i64(TCG_AREG0,
100 offsetof(CPUARMState, exclusive_val), "exclusive_val");
101 cpu_exclusive_high = tcg_global_mem_new_i64(TCG_AREG0,
102 offsetof(CPUARMState, exclusive_high), "exclusive_high");
103#ifdef CONFIG_USER_ONLY
104 cpu_exclusive_test = tcg_global_mem_new_i64(TCG_AREG0,
105 offsetof(CPUARMState, exclusive_test), "exclusive_test");
106 cpu_exclusive_info = tcg_global_mem_new_i32(TCG_AREG0,
107 offsetof(CPUARMState, exclusive_info), "exclusive_info");
108#endif
Alexander Graf14ade102013-09-03 20:12:10 +0100109}
110
111void aarch64_cpu_dump_state(CPUState *cs, FILE *f,
112 fprintf_function cpu_fprintf, int flags)
113{
114 ARMCPU *cpu = ARM_CPU(cs);
115 CPUARMState *env = &cpu->env;
Peter Maydelld3563122013-12-17 19:42:30 +0000116 uint32_t psr = pstate_read(env);
Alexander Graf14ade102013-09-03 20:12:10 +0100117 int i;
118
119 cpu_fprintf(f, "PC=%016"PRIx64" SP=%016"PRIx64"\n",
120 env->pc, env->xregs[31]);
121 for (i = 0; i < 31; i++) {
122 cpu_fprintf(f, "X%02d=%016"PRIx64, i, env->xregs[i]);
123 if ((i % 4) == 3) {
124 cpu_fprintf(f, "\n");
125 } else {
126 cpu_fprintf(f, " ");
127 }
128 }
Peter Maydelld3563122013-12-17 19:42:30 +0000129 cpu_fprintf(f, "PSTATE=%08x (flags %c%c%c%c)\n",
130 psr,
131 psr & PSTATE_N ? 'N' : '-',
132 psr & PSTATE_Z ? 'Z' : '-',
133 psr & PSTATE_C ? 'C' : '-',
134 psr & PSTATE_V ? 'V' : '-');
Alexander Graf14ade102013-09-03 20:12:10 +0100135 cpu_fprintf(f, "\n");
Alexander Graff6d8a312014-01-04 22:15:49 +0000136
137 if (flags & CPU_DUMP_FPU) {
138 int numvfpregs = 32;
139 for (i = 0; i < numvfpregs; i += 2) {
140 uint64_t vlo = float64_val(env->vfp.regs[i * 2]);
141 uint64_t vhi = float64_val(env->vfp.regs[(i * 2) + 1]);
142 cpu_fprintf(f, "q%02d=%016" PRIx64 ":%016" PRIx64 " ",
143 i, vhi, vlo);
144 vlo = float64_val(env->vfp.regs[(i + 1) * 2]);
145 vhi = float64_val(env->vfp.regs[((i + 1) * 2) + 1]);
146 cpu_fprintf(f, "q%02d=%016" PRIx64 ":%016" PRIx64 "\n",
147 i + 1, vhi, vlo);
148 }
149 cpu_fprintf(f, "FPCR: %08x FPSR: %08x\n",
150 vfp_get_fpcr(env), vfp_get_fpsr(env));
151 }
Alexander Graf14ade102013-09-03 20:12:10 +0100152}
153
Peter Maydell4a08d472013-12-22 22:32:27 +0000154static int get_mem_index(DisasContext *s)
155{
156#ifdef CONFIG_USER_ONLY
157 return 1;
158#else
159 return s->user;
160#endif
161}
162
Alexander Graf14ade102013-09-03 20:12:10 +0100163void gen_a64_set_pc_im(uint64_t val)
164{
165 tcg_gen_movi_i64(cpu_pc, val);
166}
167
168static void gen_exception(int excp)
169{
170 TCGv_i32 tmp = tcg_temp_new_i32();
171 tcg_gen_movi_i32(tmp, excp);
172 gen_helper_exception(cpu_env, tmp);
173 tcg_temp_free_i32(tmp);
174}
175
176static void gen_exception_insn(DisasContext *s, int offset, int excp)
177{
178 gen_a64_set_pc_im(s->pc - offset);
179 gen_exception(excp);
Peter Maydell40f860c2013-12-17 19:42:31 +0000180 s->is_jmp = DISAS_EXC;
181}
182
183static inline bool use_goto_tb(DisasContext *s, int n, uint64_t dest)
184{
185 /* No direct tb linking with singlestep or deterministic io */
186 if (s->singlestep_enabled || (s->tb->cflags & CF_LAST_IO)) {
187 return false;
188 }
189
190 /* Only link tbs from inside the same guest page */
191 if ((s->tb->pc & TARGET_PAGE_MASK) != (dest & TARGET_PAGE_MASK)) {
192 return false;
193 }
194
195 return true;
196}
197
198static inline void gen_goto_tb(DisasContext *s, int n, uint64_t dest)
199{
200 TranslationBlock *tb;
201
202 tb = s->tb;
203 if (use_goto_tb(s, n, dest)) {
204 tcg_gen_goto_tb(n);
205 gen_a64_set_pc_im(dest);
206 tcg_gen_exit_tb((tcg_target_long)tb + n);
207 s->is_jmp = DISAS_TB_JUMP;
208 } else {
209 gen_a64_set_pc_im(dest);
210 if (s->singlestep_enabled) {
211 gen_exception(EXCP_DEBUG);
212 }
213 tcg_gen_exit_tb(0);
214 s->is_jmp = DISAS_JUMP;
215 }
Alexander Graf14ade102013-09-03 20:12:10 +0100216}
217
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +0000218static void unallocated_encoding(DisasContext *s)
Alexander Graf14ade102013-09-03 20:12:10 +0100219{
Alexander Graf14ade102013-09-03 20:12:10 +0100220 gen_exception_insn(s, 4, EXCP_UDEF);
221}
222
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +0000223#define unsupported_encoding(s, insn) \
224 do { \
225 qemu_log_mask(LOG_UNIMP, \
226 "%s:%d: unsupported instruction encoding 0x%08x " \
227 "at pc=%016" PRIx64 "\n", \
228 __FILE__, __LINE__, insn, s->pc - 4); \
229 unallocated_encoding(s); \
230 } while (0);
Alexander Graf14ade102013-09-03 20:12:10 +0100231
Alexander Graf11e169d2013-12-17 19:42:32 +0000232static void init_tmp_a64_array(DisasContext *s)
233{
234#ifdef CONFIG_DEBUG_TCG
235 int i;
236 for (i = 0; i < ARRAY_SIZE(s->tmp_a64); i++) {
237 TCGV_UNUSED_I64(s->tmp_a64[i]);
238 }
239#endif
240 s->tmp_a64_count = 0;
241}
242
243static void free_tmp_a64(DisasContext *s)
244{
245 int i;
246 for (i = 0; i < s->tmp_a64_count; i++) {
247 tcg_temp_free_i64(s->tmp_a64[i]);
248 }
249 init_tmp_a64_array(s);
250}
251
252static TCGv_i64 new_tmp_a64(DisasContext *s)
253{
254 assert(s->tmp_a64_count < TMP_A64_MAX);
255 return s->tmp_a64[s->tmp_a64_count++] = tcg_temp_new_i64();
256}
257
258static TCGv_i64 new_tmp_a64_zero(DisasContext *s)
259{
260 TCGv_i64 t = new_tmp_a64(s);
261 tcg_gen_movi_i64(t, 0);
262 return t;
263}
264
Alexander Graf71b46082013-12-17 19:42:36 +0000265/*
266 * Register access functions
267 *
268 * These functions are used for directly accessing a register in where
269 * changes to the final register value are likely to be made. If you
270 * need to use a register for temporary calculation (e.g. index type
271 * operations) use the read_* form.
272 *
273 * B1.2.1 Register mappings
274 *
275 * In instruction register encoding 31 can refer to ZR (zero register) or
276 * the SP (stack pointer) depending on context. In QEMU's case we map SP
277 * to cpu_X[31] and ZR accesses to a temporary which can be discarded.
278 * This is the point of the _sp forms.
279 */
Alexander Graf11e169d2013-12-17 19:42:32 +0000280static TCGv_i64 cpu_reg(DisasContext *s, int reg)
281{
282 if (reg == 31) {
283 return new_tmp_a64_zero(s);
284 } else {
285 return cpu_X[reg];
286 }
287}
288
Alexander Graf71b46082013-12-17 19:42:36 +0000289/* register access for when 31 == SP */
290static TCGv_i64 cpu_reg_sp(DisasContext *s, int reg)
291{
292 return cpu_X[reg];
293}
294
Alexander Graf60e53382013-12-17 19:42:33 +0000295/* read a cpu register in 32bit/64bit mode. Returns a TCGv_i64
296 * representing the register contents. This TCGv is an auto-freed
297 * temporary so it need not be explicitly freed, and may be modified.
298 */
299static TCGv_i64 read_cpu_reg(DisasContext *s, int reg, int sf)
300{
301 TCGv_i64 v = new_tmp_a64(s);
302 if (reg != 31) {
303 if (sf) {
304 tcg_gen_mov_i64(v, cpu_X[reg]);
305 } else {
306 tcg_gen_ext32u_i64(v, cpu_X[reg]);
307 }
308 } else {
309 tcg_gen_movi_i64(v, 0);
310 }
311 return v;
312}
313
Peter Maydell4a08d472013-12-22 22:32:27 +0000314static TCGv_i64 read_cpu_reg_sp(DisasContext *s, int reg, int sf)
315{
316 TCGv_i64 v = new_tmp_a64(s);
317 if (sf) {
318 tcg_gen_mov_i64(v, cpu_X[reg]);
319 } else {
320 tcg_gen_ext32u_i64(v, cpu_X[reg]);
321 }
322 return v;
323}
324
Alex Bennée539ed652014-01-23 14:37:07 +0000325/* Return the offset into CPUARMState of an element of specified
326 * size, 'element' places in from the least significant end of
327 * the FP/vector register Qn.
328 */
329static inline int vec_reg_offset(int regno, int element, TCGMemOp size)
330{
331 int offs = offsetof(CPUARMState, vfp.regs[regno * 2]);
332#ifdef HOST_WORDS_BIGENDIAN
333 /* This is complicated slightly because vfp.regs[2n] is
334 * still the low half and vfp.regs[2n+1] the high half
335 * of the 128 bit vector, even on big endian systems.
336 * Calculate the offset assuming a fully bigendian 128 bits,
337 * then XOR to account for the order of the two 64 bit halves.
338 */
339 offs += (16 - ((element + 1) * (1 << size)));
340 offs ^= 8;
341#else
342 offs += element * (1 << size);
343#endif
344 return offs;
345}
346
Peter Maydelle2f90562014-01-04 22:15:49 +0000347/* Return the offset into CPUARMState of a slice (from
348 * the least significant end) of FP register Qn (ie
349 * Dn, Sn, Hn or Bn).
350 * (Note that this is not the same mapping as for A32; see cpu.h)
351 */
352static inline int fp_reg_offset(int regno, TCGMemOp size)
353{
354 int offs = offsetof(CPUARMState, vfp.regs[regno * 2]);
355#ifdef HOST_WORDS_BIGENDIAN
356 offs += (8 - (1 << size));
357#endif
358 return offs;
359}
360
361/* Offset of the high half of the 128 bit vector Qn */
362static inline int fp_reg_hi_offset(int regno)
363{
364 return offsetof(CPUARMState, vfp.regs[regno * 2 + 1]);
365}
366
Alexander Grafec73d2e2014-01-04 22:15:50 +0000367/* Convenience accessors for reading and writing single and double
368 * FP registers. Writing clears the upper parts of the associated
369 * 128 bit vector register, as required by the architecture.
370 * Note that unlike the GP register accessors, the values returned
371 * by the read functions must be manually freed.
372 */
373static TCGv_i64 read_fp_dreg(DisasContext *s, int reg)
374{
375 TCGv_i64 v = tcg_temp_new_i64();
376
377 tcg_gen_ld_i64(v, cpu_env, fp_reg_offset(reg, MO_64));
378 return v;
379}
380
381static TCGv_i32 read_fp_sreg(DisasContext *s, int reg)
382{
383 TCGv_i32 v = tcg_temp_new_i32();
384
385 tcg_gen_ld_i32(v, cpu_env, fp_reg_offset(reg, MO_32));
386 return v;
387}
388
389static void write_fp_dreg(DisasContext *s, int reg, TCGv_i64 v)
390{
391 TCGv_i64 tcg_zero = tcg_const_i64(0);
392
393 tcg_gen_st_i64(v, cpu_env, fp_reg_offset(reg, MO_64));
394 tcg_gen_st_i64(tcg_zero, cpu_env, fp_reg_hi_offset(reg));
395 tcg_temp_free_i64(tcg_zero);
396}
397
398static void write_fp_sreg(DisasContext *s, int reg, TCGv_i32 v)
399{
400 TCGv_i64 tmp = tcg_temp_new_i64();
401
402 tcg_gen_extu_i32_i64(tmp, v);
403 write_fp_dreg(s, reg, tmp);
404 tcg_temp_free_i64(tmp);
405}
406
407static TCGv_ptr get_fpstatus_ptr(void)
408{
409 TCGv_ptr statusptr = tcg_temp_new_ptr();
410 int offset;
411
412 /* In A64 all instructions (both FP and Neon) use the FPCR;
413 * there is no equivalent of the A32 Neon "standard FPSCR value"
414 * and all operations use vfp.fp_status.
415 */
416 offset = offsetof(CPUARMState, vfp.fp_status);
417 tcg_gen_addi_ptr(statusptr, cpu_env, offset);
418 return statusptr;
419}
420
Alexander Graf832ffa12013-12-17 19:42:34 +0000421/* Set ZF and NF based on a 64 bit result. This is alas fiddlier
422 * than the 32 bit equivalent.
423 */
424static inline void gen_set_NZ64(TCGv_i64 result)
425{
426 TCGv_i64 flag = tcg_temp_new_i64();
427
428 tcg_gen_setcondi_i64(TCG_COND_NE, flag, result, 0);
429 tcg_gen_trunc_i64_i32(cpu_ZF, flag);
430 tcg_gen_shri_i64(flag, result, 32);
431 tcg_gen_trunc_i64_i32(cpu_NF, flag);
432 tcg_temp_free_i64(flag);
433}
434
435/* Set NZCV as for a logical operation: NZ as per result, CV cleared. */
436static inline void gen_logic_CC(int sf, TCGv_i64 result)
437{
438 if (sf) {
439 gen_set_NZ64(result);
440 } else {
441 tcg_gen_trunc_i64_i32(cpu_ZF, result);
442 tcg_gen_trunc_i64_i32(cpu_NF, result);
443 }
444 tcg_gen_movi_i32(cpu_CF, 0);
445 tcg_gen_movi_i32(cpu_VF, 0);
446}
447
Alex Bennéeb0ff21b2013-12-23 23:27:29 +0000448/* dest = T0 + T1; compute C, N, V and Z flags */
449static void gen_add_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
450{
451 if (sf) {
452 TCGv_i64 result, flag, tmp;
453 result = tcg_temp_new_i64();
454 flag = tcg_temp_new_i64();
455 tmp = tcg_temp_new_i64();
456
457 tcg_gen_movi_i64(tmp, 0);
458 tcg_gen_add2_i64(result, flag, t0, tmp, t1, tmp);
459
460 tcg_gen_trunc_i64_i32(cpu_CF, flag);
461
462 gen_set_NZ64(result);
463
464 tcg_gen_xor_i64(flag, result, t0);
465 tcg_gen_xor_i64(tmp, t0, t1);
466 tcg_gen_andc_i64(flag, flag, tmp);
467 tcg_temp_free_i64(tmp);
468 tcg_gen_shri_i64(flag, flag, 32);
469 tcg_gen_trunc_i64_i32(cpu_VF, flag);
470
471 tcg_gen_mov_i64(dest, result);
472 tcg_temp_free_i64(result);
473 tcg_temp_free_i64(flag);
474 } else {
475 /* 32 bit arithmetic */
476 TCGv_i32 t0_32 = tcg_temp_new_i32();
477 TCGv_i32 t1_32 = tcg_temp_new_i32();
478 TCGv_i32 tmp = tcg_temp_new_i32();
479
480 tcg_gen_movi_i32(tmp, 0);
481 tcg_gen_trunc_i64_i32(t0_32, t0);
482 tcg_gen_trunc_i64_i32(t1_32, t1);
483 tcg_gen_add2_i32(cpu_NF, cpu_CF, t0_32, tmp, t1_32, tmp);
484 tcg_gen_mov_i32(cpu_ZF, cpu_NF);
485 tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
486 tcg_gen_xor_i32(tmp, t0_32, t1_32);
487 tcg_gen_andc_i32(cpu_VF, cpu_VF, tmp);
488 tcg_gen_extu_i32_i64(dest, cpu_NF);
489
490 tcg_temp_free_i32(tmp);
491 tcg_temp_free_i32(t0_32);
492 tcg_temp_free_i32(t1_32);
493 }
494}
495
496/* dest = T0 - T1; compute C, N, V and Z flags */
497static void gen_sub_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
498{
499 if (sf) {
500 /* 64 bit arithmetic */
501 TCGv_i64 result, flag, tmp;
502
503 result = tcg_temp_new_i64();
504 flag = tcg_temp_new_i64();
505 tcg_gen_sub_i64(result, t0, t1);
506
507 gen_set_NZ64(result);
508
509 tcg_gen_setcond_i64(TCG_COND_GEU, flag, t0, t1);
510 tcg_gen_trunc_i64_i32(cpu_CF, flag);
511
512 tcg_gen_xor_i64(flag, result, t0);
513 tmp = tcg_temp_new_i64();
514 tcg_gen_xor_i64(tmp, t0, t1);
515 tcg_gen_and_i64(flag, flag, tmp);
516 tcg_temp_free_i64(tmp);
517 tcg_gen_shri_i64(flag, flag, 32);
518 tcg_gen_trunc_i64_i32(cpu_VF, flag);
519 tcg_gen_mov_i64(dest, result);
520 tcg_temp_free_i64(flag);
521 tcg_temp_free_i64(result);
522 } else {
523 /* 32 bit arithmetic */
524 TCGv_i32 t0_32 = tcg_temp_new_i32();
525 TCGv_i32 t1_32 = tcg_temp_new_i32();
526 TCGv_i32 tmp;
527
528 tcg_gen_trunc_i64_i32(t0_32, t0);
529 tcg_gen_trunc_i64_i32(t1_32, t1);
530 tcg_gen_sub_i32(cpu_NF, t0_32, t1_32);
531 tcg_gen_mov_i32(cpu_ZF, cpu_NF);
532 tcg_gen_setcond_i32(TCG_COND_GEU, cpu_CF, t0_32, t1_32);
533 tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
534 tmp = tcg_temp_new_i32();
535 tcg_gen_xor_i32(tmp, t0_32, t1_32);
536 tcg_temp_free_i32(t0_32);
537 tcg_temp_free_i32(t1_32);
538 tcg_gen_and_i32(cpu_VF, cpu_VF, tmp);
539 tcg_temp_free_i32(tmp);
540 tcg_gen_extu_i32_i64(dest, cpu_NF);
541 }
542}
543
Claudio Fontana643dbb02014-01-04 22:15:46 +0000544/* dest = T0 + T1 + CF; do not compute flags. */
545static void gen_adc(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
546{
547 TCGv_i64 flag = tcg_temp_new_i64();
548 tcg_gen_extu_i32_i64(flag, cpu_CF);
549 tcg_gen_add_i64(dest, t0, t1);
550 tcg_gen_add_i64(dest, dest, flag);
551 tcg_temp_free_i64(flag);
552
553 if (!sf) {
554 tcg_gen_ext32u_i64(dest, dest);
555 }
556}
557
558/* dest = T0 + T1 + CF; compute C, N, V and Z flags. */
559static void gen_adc_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
560{
561 if (sf) {
562 TCGv_i64 result, cf_64, vf_64, tmp;
563 result = tcg_temp_new_i64();
564 cf_64 = tcg_temp_new_i64();
565 vf_64 = tcg_temp_new_i64();
566 tmp = tcg_const_i64(0);
567
568 tcg_gen_extu_i32_i64(cf_64, cpu_CF);
569 tcg_gen_add2_i64(result, cf_64, t0, tmp, cf_64, tmp);
570 tcg_gen_add2_i64(result, cf_64, result, cf_64, t1, tmp);
571 tcg_gen_trunc_i64_i32(cpu_CF, cf_64);
572 gen_set_NZ64(result);
573
574 tcg_gen_xor_i64(vf_64, result, t0);
575 tcg_gen_xor_i64(tmp, t0, t1);
576 tcg_gen_andc_i64(vf_64, vf_64, tmp);
577 tcg_gen_shri_i64(vf_64, vf_64, 32);
578 tcg_gen_trunc_i64_i32(cpu_VF, vf_64);
579
580 tcg_gen_mov_i64(dest, result);
581
582 tcg_temp_free_i64(tmp);
583 tcg_temp_free_i64(vf_64);
584 tcg_temp_free_i64(cf_64);
585 tcg_temp_free_i64(result);
586 } else {
587 TCGv_i32 t0_32, t1_32, tmp;
588 t0_32 = tcg_temp_new_i32();
589 t1_32 = tcg_temp_new_i32();
590 tmp = tcg_const_i32(0);
591
592 tcg_gen_trunc_i64_i32(t0_32, t0);
593 tcg_gen_trunc_i64_i32(t1_32, t1);
594 tcg_gen_add2_i32(cpu_NF, cpu_CF, t0_32, tmp, cpu_CF, tmp);
595 tcg_gen_add2_i32(cpu_NF, cpu_CF, cpu_NF, cpu_CF, t1_32, tmp);
596
597 tcg_gen_mov_i32(cpu_ZF, cpu_NF);
598 tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
599 tcg_gen_xor_i32(tmp, t0_32, t1_32);
600 tcg_gen_andc_i32(cpu_VF, cpu_VF, tmp);
601 tcg_gen_extu_i32_i64(dest, cpu_NF);
602
603 tcg_temp_free_i32(tmp);
604 tcg_temp_free_i32(t1_32);
605 tcg_temp_free_i32(t0_32);
606 }
607}
608
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +0000609/*
Peter Maydell4a08d472013-12-22 22:32:27 +0000610 * Load/Store generators
611 */
612
613/*
614 * Store from GPR register to memory
615 */
616static void do_gpr_st(DisasContext *s, TCGv_i64 source,
617 TCGv_i64 tcg_addr, int size)
618{
619 g_assert(size <= 3);
620 tcg_gen_qemu_st_i64(source, tcg_addr, get_mem_index(s), MO_TE + size);
621}
622
623/*
624 * Load from memory to GPR register
625 */
626static void do_gpr_ld(DisasContext *s, TCGv_i64 dest, TCGv_i64 tcg_addr,
627 int size, bool is_signed, bool extend)
628{
629 TCGMemOp memop = MO_TE + size;
630
631 g_assert(size <= 3);
632
633 if (is_signed) {
634 memop += MO_SIGN;
635 }
636
637 tcg_gen_qemu_ld_i64(dest, tcg_addr, get_mem_index(s), memop);
638
639 if (extend && is_signed) {
640 g_assert(size < 3);
641 tcg_gen_ext32u_i64(dest, dest);
642 }
643}
644
645/*
646 * Store from FP register to memory
647 */
648static void do_fp_st(DisasContext *s, int srcidx, TCGv_i64 tcg_addr, int size)
649{
650 /* This writes the bottom N bits of a 128 bit wide vector to memory */
Peter Maydell4a08d472013-12-22 22:32:27 +0000651 TCGv_i64 tmp = tcg_temp_new_i64();
Peter Maydelle2f90562014-01-04 22:15:49 +0000652 tcg_gen_ld_i64(tmp, cpu_env, fp_reg_offset(srcidx, MO_64));
Peter Maydell4a08d472013-12-22 22:32:27 +0000653 if (size < 4) {
Peter Maydell4a08d472013-12-22 22:32:27 +0000654 tcg_gen_qemu_st_i64(tmp, tcg_addr, get_mem_index(s), MO_TE + size);
655 } else {
656 TCGv_i64 tcg_hiaddr = tcg_temp_new_i64();
Peter Maydell4a08d472013-12-22 22:32:27 +0000657 tcg_gen_qemu_st_i64(tmp, tcg_addr, get_mem_index(s), MO_TEQ);
658 tcg_gen_qemu_st64(tmp, tcg_addr, get_mem_index(s));
Peter Maydelle2f90562014-01-04 22:15:49 +0000659 tcg_gen_ld_i64(tmp, cpu_env, fp_reg_hi_offset(srcidx));
Peter Maydell4a08d472013-12-22 22:32:27 +0000660 tcg_gen_addi_i64(tcg_hiaddr, tcg_addr, 8);
661 tcg_gen_qemu_st_i64(tmp, tcg_hiaddr, get_mem_index(s), MO_TEQ);
662 tcg_temp_free_i64(tcg_hiaddr);
663 }
664
665 tcg_temp_free_i64(tmp);
666}
667
668/*
669 * Load from memory to FP register
670 */
671static void do_fp_ld(DisasContext *s, int destidx, TCGv_i64 tcg_addr, int size)
672{
673 /* This always zero-extends and writes to a full 128 bit wide vector */
Peter Maydell4a08d472013-12-22 22:32:27 +0000674 TCGv_i64 tmplo = tcg_temp_new_i64();
675 TCGv_i64 tmphi;
676
677 if (size < 4) {
678 TCGMemOp memop = MO_TE + size;
679 tmphi = tcg_const_i64(0);
680 tcg_gen_qemu_ld_i64(tmplo, tcg_addr, get_mem_index(s), memop);
681 } else {
682 TCGv_i64 tcg_hiaddr;
683 tmphi = tcg_temp_new_i64();
684 tcg_hiaddr = tcg_temp_new_i64();
685
686 tcg_gen_qemu_ld_i64(tmplo, tcg_addr, get_mem_index(s), MO_TEQ);
687 tcg_gen_addi_i64(tcg_hiaddr, tcg_addr, 8);
688 tcg_gen_qemu_ld_i64(tmphi, tcg_hiaddr, get_mem_index(s), MO_TEQ);
689 tcg_temp_free_i64(tcg_hiaddr);
690 }
691
Peter Maydelle2f90562014-01-04 22:15:49 +0000692 tcg_gen_st_i64(tmplo, cpu_env, fp_reg_offset(destidx, MO_64));
693 tcg_gen_st_i64(tmphi, cpu_env, fp_reg_hi_offset(destidx));
Peter Maydell4a08d472013-12-22 22:32:27 +0000694
695 tcg_temp_free_i64(tmplo);
696 tcg_temp_free_i64(tmphi);
697}
698
Alex Bennée229b7a02013-12-23 23:27:29 +0000699/*
Alex Bennée539ed652014-01-23 14:37:07 +0000700 * Vector load/store helpers.
701 *
702 * The principal difference between this and a FP load is that we don't
703 * zero extend as we are filling a partial chunk of the vector register.
704 * These functions don't support 128 bit loads/stores, which would be
705 * normal load/store operations.
Peter Maydellac224302014-01-14 12:44:38 +0000706 *
707 * The _i32 versions are useful when operating on 32 bit quantities
708 * (eg for floating point single or using Neon helper functions).
Alex Bennée539ed652014-01-23 14:37:07 +0000709 */
710
711/* Get value of an element within a vector register */
712static void read_vec_element(DisasContext *s, TCGv_i64 tcg_dest, int srcidx,
713 int element, TCGMemOp memop)
714{
715 int vect_off = vec_reg_offset(srcidx, element, memop & MO_SIZE);
716 switch (memop) {
717 case MO_8:
718 tcg_gen_ld8u_i64(tcg_dest, cpu_env, vect_off);
719 break;
720 case MO_16:
721 tcg_gen_ld16u_i64(tcg_dest, cpu_env, vect_off);
722 break;
723 case MO_32:
724 tcg_gen_ld32u_i64(tcg_dest, cpu_env, vect_off);
725 break;
726 case MO_8|MO_SIGN:
727 tcg_gen_ld8s_i64(tcg_dest, cpu_env, vect_off);
728 break;
729 case MO_16|MO_SIGN:
730 tcg_gen_ld16s_i64(tcg_dest, cpu_env, vect_off);
731 break;
732 case MO_32|MO_SIGN:
733 tcg_gen_ld32s_i64(tcg_dest, cpu_env, vect_off);
734 break;
735 case MO_64:
736 case MO_64|MO_SIGN:
737 tcg_gen_ld_i64(tcg_dest, cpu_env, vect_off);
738 break;
739 default:
740 g_assert_not_reached();
741 }
742}
743
Peter Maydellac224302014-01-14 12:44:38 +0000744static void read_vec_element_i32(DisasContext *s, TCGv_i32 tcg_dest, int srcidx,
745 int element, TCGMemOp memop)
746{
747 int vect_off = vec_reg_offset(srcidx, element, memop & MO_SIZE);
748 switch (memop) {
749 case MO_8:
750 tcg_gen_ld8u_i32(tcg_dest, cpu_env, vect_off);
751 break;
752 case MO_16:
753 tcg_gen_ld16u_i32(tcg_dest, cpu_env, vect_off);
754 break;
755 case MO_8|MO_SIGN:
756 tcg_gen_ld8s_i32(tcg_dest, cpu_env, vect_off);
757 break;
758 case MO_16|MO_SIGN:
759 tcg_gen_ld16s_i32(tcg_dest, cpu_env, vect_off);
760 break;
761 case MO_32:
762 case MO_32|MO_SIGN:
763 tcg_gen_ld_i32(tcg_dest, cpu_env, vect_off);
764 break;
765 default:
766 g_assert_not_reached();
767 }
768}
769
Alex Bennée539ed652014-01-23 14:37:07 +0000770/* Set value of an element within a vector register */
771static void write_vec_element(DisasContext *s, TCGv_i64 tcg_src, int destidx,
772 int element, TCGMemOp memop)
773{
774 int vect_off = vec_reg_offset(destidx, element, memop & MO_SIZE);
775 switch (memop) {
776 case MO_8:
777 tcg_gen_st8_i64(tcg_src, cpu_env, vect_off);
778 break;
779 case MO_16:
780 tcg_gen_st16_i64(tcg_src, cpu_env, vect_off);
781 break;
782 case MO_32:
783 tcg_gen_st32_i64(tcg_src, cpu_env, vect_off);
784 break;
785 case MO_64:
786 tcg_gen_st_i64(tcg_src, cpu_env, vect_off);
787 break;
788 default:
789 g_assert_not_reached();
790 }
791}
792
Peter Maydell0d19b492014-01-23 13:13:41 +0000793static void write_vec_element_i32(DisasContext *s, TCGv_i32 tcg_src,
794 int destidx, int element, TCGMemOp memop)
795{
796 int vect_off = vec_reg_offset(destidx, element, memop & MO_SIZE);
797 switch (memop) {
798 case MO_8:
799 tcg_gen_st8_i32(tcg_src, cpu_env, vect_off);
800 break;
801 case MO_16:
802 tcg_gen_st16_i32(tcg_src, cpu_env, vect_off);
803 break;
804 case MO_32:
805 tcg_gen_st_i32(tcg_src, cpu_env, vect_off);
806 break;
807 default:
808 g_assert_not_reached();
809 }
810}
811
Alex Bennée539ed652014-01-23 14:37:07 +0000812/* Clear the high 64 bits of a 128 bit vector (in general non-quad
813 * vector ops all need to do this).
814 */
815static void clear_vec_high(DisasContext *s, int rd)
816{
817 TCGv_i64 tcg_zero = tcg_const_i64(0);
818
819 write_vec_element(s, tcg_zero, rd, 1, MO_64);
820 tcg_temp_free_i64(tcg_zero);
821}
822
823/* Store from vector register to memory */
824static void do_vec_st(DisasContext *s, int srcidx, int element,
825 TCGv_i64 tcg_addr, int size)
826{
827 TCGMemOp memop = MO_TE + size;
828 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
829
830 read_vec_element(s, tcg_tmp, srcidx, element, size);
831 tcg_gen_qemu_st_i64(tcg_tmp, tcg_addr, get_mem_index(s), memop);
832
833 tcg_temp_free_i64(tcg_tmp);
834}
835
836/* Load from memory to vector register */
837static void do_vec_ld(DisasContext *s, int destidx, int element,
838 TCGv_i64 tcg_addr, int size)
839{
840 TCGMemOp memop = MO_TE + size;
841 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
842
843 tcg_gen_qemu_ld_i64(tcg_tmp, tcg_addr, get_mem_index(s), memop);
844 write_vec_element(s, tcg_tmp, destidx, element, size);
845
846 tcg_temp_free_i64(tcg_tmp);
847}
848
849/*
Alex Bennée229b7a02013-12-23 23:27:29 +0000850 * This utility function is for doing register extension with an
851 * optional shift. You will likely want to pass a temporary for the
852 * destination register. See DecodeRegExtend() in the ARM ARM.
853 */
854static void ext_and_shift_reg(TCGv_i64 tcg_out, TCGv_i64 tcg_in,
855 int option, unsigned int shift)
856{
857 int extsize = extract32(option, 0, 2);
858 bool is_signed = extract32(option, 2, 1);
859
860 if (is_signed) {
861 switch (extsize) {
862 case 0:
863 tcg_gen_ext8s_i64(tcg_out, tcg_in);
864 break;
865 case 1:
866 tcg_gen_ext16s_i64(tcg_out, tcg_in);
867 break;
868 case 2:
869 tcg_gen_ext32s_i64(tcg_out, tcg_in);
870 break;
871 case 3:
872 tcg_gen_mov_i64(tcg_out, tcg_in);
873 break;
874 }
875 } else {
876 switch (extsize) {
877 case 0:
878 tcg_gen_ext8u_i64(tcg_out, tcg_in);
879 break;
880 case 1:
881 tcg_gen_ext16u_i64(tcg_out, tcg_in);
882 break;
883 case 2:
884 tcg_gen_ext32u_i64(tcg_out, tcg_in);
885 break;
886 case 3:
887 tcg_gen_mov_i64(tcg_out, tcg_in);
888 break;
889 }
890 }
891
892 if (shift) {
893 tcg_gen_shli_i64(tcg_out, tcg_out, shift);
894 }
895}
896
Peter Maydell4a08d472013-12-22 22:32:27 +0000897static inline void gen_check_sp_alignment(DisasContext *s)
898{
899 /* The AArch64 architecture mandates that (if enabled via PSTATE
900 * or SCTLR bits) there is a check that SP is 16-aligned on every
901 * SP-relative load or store (with an exception generated if it is not).
902 * In line with general QEMU practice regarding misaligned accesses,
903 * we omit these checks for the sake of guest program performance.
904 * This function is provided as a hook so we can more easily add these
905 * checks in future (possibly as a "favour catching guest program bugs
906 * over speed" user selectable option).
907 */
908}
909
910/*
Alex Bennée94ad8702014-01-23 14:37:07 +0000911 * This provides a simple table based table lookup decoder. It is
912 * intended to be used when the relevant bits for decode are too
913 * awkwardly placed and switch/if based logic would be confusing and
914 * deeply nested. Since it's a linear search through the table, tables
915 * should be kept small.
916 *
917 * It returns the first handler where insn & mask == pattern, or
918 * NULL if there is no match.
919 * The table is terminated by an empty mask (i.e. 0)
920 */
921static inline AArch64DecodeFn *lookup_disas_fn(const AArch64DecodeTable *table,
922 uint32_t insn)
923{
924 const AArch64DecodeTable *tptr = table;
925
926 while (tptr->mask) {
927 if ((insn & tptr->mask) == tptr->pattern) {
928 return tptr->disas_fn;
929 }
930 tptr++;
931 }
932 return NULL;
933}
934
935/*
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +0000936 * the instruction disassembly implemented here matches
937 * the instruction encoding classifications in chapter 3 (C3)
938 * of the ARM Architecture Reference Manual (DDI0487A_a)
939 */
940
Alexander Graf11e169d2013-12-17 19:42:32 +0000941/* C3.2.7 Unconditional branch (immediate)
942 * 31 30 26 25 0
943 * +----+-----------+-------------------------------------+
944 * | op | 0 0 1 0 1 | imm26 |
945 * +----+-----------+-------------------------------------+
946 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +0000947static void disas_uncond_b_imm(DisasContext *s, uint32_t insn)
948{
Alexander Graf11e169d2013-12-17 19:42:32 +0000949 uint64_t addr = s->pc + sextract32(insn, 0, 26) * 4 - 4;
950
951 if (insn & (1 << 31)) {
952 /* C5.6.26 BL Branch with link */
953 tcg_gen_movi_i64(cpu_reg(s, 30), s->pc);
954 }
955
956 /* C5.6.20 B Branch / C5.6.26 BL Branch with link */
957 gen_goto_tb(s, 0, addr);
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +0000958}
959
Alexander Graf60e53382013-12-17 19:42:33 +0000960/* C3.2.1 Compare & branch (immediate)
961 * 31 30 25 24 23 5 4 0
962 * +----+-------------+----+---------------------+--------+
963 * | sf | 0 1 1 0 1 0 | op | imm19 | Rt |
964 * +----+-------------+----+---------------------+--------+
965 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +0000966static void disas_comp_b_imm(DisasContext *s, uint32_t insn)
967{
Alexander Graf60e53382013-12-17 19:42:33 +0000968 unsigned int sf, op, rt;
969 uint64_t addr;
970 int label_match;
971 TCGv_i64 tcg_cmp;
972
973 sf = extract32(insn, 31, 1);
974 op = extract32(insn, 24, 1); /* 0: CBZ; 1: CBNZ */
975 rt = extract32(insn, 0, 5);
976 addr = s->pc + sextract32(insn, 5, 19) * 4 - 4;
977
978 tcg_cmp = read_cpu_reg(s, rt, sf);
979 label_match = gen_new_label();
980
981 tcg_gen_brcondi_i64(op ? TCG_COND_NE : TCG_COND_EQ,
982 tcg_cmp, 0, label_match);
983
984 gen_goto_tb(s, 0, s->pc);
985 gen_set_label(label_match);
986 gen_goto_tb(s, 1, addr);
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +0000987}
988
Alexander Grafdb0f7952013-12-17 19:42:33 +0000989/* C3.2.5 Test & branch (immediate)
990 * 31 30 25 24 23 19 18 5 4 0
991 * +----+-------------+----+-------+-------------+------+
992 * | b5 | 0 1 1 0 1 1 | op | b40 | imm14 | Rt |
993 * +----+-------------+----+-------+-------------+------+
994 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +0000995static void disas_test_b_imm(DisasContext *s, uint32_t insn)
996{
Alexander Grafdb0f7952013-12-17 19:42:33 +0000997 unsigned int bit_pos, op, rt;
998 uint64_t addr;
999 int label_match;
1000 TCGv_i64 tcg_cmp;
1001
1002 bit_pos = (extract32(insn, 31, 1) << 5) | extract32(insn, 19, 5);
1003 op = extract32(insn, 24, 1); /* 0: TBZ; 1: TBNZ */
1004 addr = s->pc + sextract32(insn, 5, 14) * 4 - 4;
1005 rt = extract32(insn, 0, 5);
1006
1007 tcg_cmp = tcg_temp_new_i64();
1008 tcg_gen_andi_i64(tcg_cmp, cpu_reg(s, rt), (1ULL << bit_pos));
1009 label_match = gen_new_label();
1010 tcg_gen_brcondi_i64(op ? TCG_COND_NE : TCG_COND_EQ,
1011 tcg_cmp, 0, label_match);
1012 tcg_temp_free_i64(tcg_cmp);
1013 gen_goto_tb(s, 0, s->pc);
1014 gen_set_label(label_match);
1015 gen_goto_tb(s, 1, addr);
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00001016}
1017
Alexander Graf39fb7302013-12-17 19:42:33 +00001018/* C3.2.2 / C5.6.19 Conditional branch (immediate)
1019 * 31 25 24 23 5 4 3 0
1020 * +---------------+----+---------------------+----+------+
1021 * | 0 1 0 1 0 1 0 | o1 | imm19 | o0 | cond |
1022 * +---------------+----+---------------------+----+------+
1023 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00001024static void disas_cond_b_imm(DisasContext *s, uint32_t insn)
1025{
Alexander Graf39fb7302013-12-17 19:42:33 +00001026 unsigned int cond;
1027 uint64_t addr;
1028
1029 if ((insn & (1 << 4)) || (insn & (1 << 24))) {
1030 unallocated_encoding(s);
1031 return;
1032 }
1033 addr = s->pc + sextract32(insn, 5, 19) * 4 - 4;
1034 cond = extract32(insn, 0, 4);
1035
1036 if (cond < 0x0e) {
1037 /* genuinely conditional branches */
1038 int label_match = gen_new_label();
1039 arm_gen_test_cc(cond, label_match);
1040 gen_goto_tb(s, 0, s->pc);
1041 gen_set_label(label_match);
1042 gen_goto_tb(s, 1, addr);
1043 } else {
1044 /* 0xe and 0xf are both "always" conditions */
1045 gen_goto_tb(s, 0, addr);
1046 }
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00001047}
1048
Claudio Fontana87462e02013-12-17 19:42:32 +00001049/* C5.6.68 HINT */
1050static void handle_hint(DisasContext *s, uint32_t insn,
1051 unsigned int op1, unsigned int op2, unsigned int crm)
1052{
1053 unsigned int selector = crm << 3 | op2;
1054
1055 if (op1 != 3) {
1056 unallocated_encoding(s);
1057 return;
1058 }
1059
1060 switch (selector) {
1061 case 0: /* NOP */
1062 return;
1063 case 1: /* YIELD */
1064 case 2: /* WFE */
1065 case 3: /* WFI */
1066 case 4: /* SEV */
1067 case 5: /* SEVL */
1068 /* we treat all as NOP at least for now */
1069 return;
1070 default:
1071 /* default specified as NOP equivalent */
1072 return;
1073 }
1074}
1075
Michael Matzfa2ef212014-01-04 22:15:47 +00001076static void gen_clrex(DisasContext *s, uint32_t insn)
1077{
1078 tcg_gen_movi_i64(cpu_exclusive_addr, -1);
1079}
1080
Claudio Fontana87462e02013-12-17 19:42:32 +00001081/* CLREX, DSB, DMB, ISB */
1082static void handle_sync(DisasContext *s, uint32_t insn,
1083 unsigned int op1, unsigned int op2, unsigned int crm)
1084{
1085 if (op1 != 3) {
1086 unallocated_encoding(s);
1087 return;
1088 }
1089
1090 switch (op2) {
1091 case 2: /* CLREX */
Michael Matzfa2ef212014-01-04 22:15:47 +00001092 gen_clrex(s, insn);
Claudio Fontana87462e02013-12-17 19:42:32 +00001093 return;
1094 case 4: /* DSB */
1095 case 5: /* DMB */
1096 case 6: /* ISB */
1097 /* We don't emulate caches so barriers are no-ops */
1098 return;
1099 default:
1100 unallocated_encoding(s);
1101 return;
1102 }
1103}
1104
1105/* C5.6.130 MSR (immediate) - move immediate to processor state field */
1106static void handle_msr_i(DisasContext *s, uint32_t insn,
1107 unsigned int op1, unsigned int op2, unsigned int crm)
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00001108{
1109 unsupported_encoding(s, insn);
1110}
1111
Peter Maydellb0d2b7d2014-01-04 22:15:45 +00001112static void gen_get_nzcv(TCGv_i64 tcg_rt)
1113{
1114 TCGv_i32 tmp = tcg_temp_new_i32();
1115 TCGv_i32 nzcv = tcg_temp_new_i32();
1116
1117 /* build bit 31, N */
1118 tcg_gen_andi_i32(nzcv, cpu_NF, (1 << 31));
1119 /* build bit 30, Z */
1120 tcg_gen_setcondi_i32(TCG_COND_EQ, tmp, cpu_ZF, 0);
1121 tcg_gen_deposit_i32(nzcv, nzcv, tmp, 30, 1);
1122 /* build bit 29, C */
1123 tcg_gen_deposit_i32(nzcv, nzcv, cpu_CF, 29, 1);
1124 /* build bit 28, V */
1125 tcg_gen_shri_i32(tmp, cpu_VF, 31);
1126 tcg_gen_deposit_i32(nzcv, nzcv, tmp, 28, 1);
1127 /* generate result */
1128 tcg_gen_extu_i32_i64(tcg_rt, nzcv);
1129
1130 tcg_temp_free_i32(nzcv);
1131 tcg_temp_free_i32(tmp);
1132}
1133
1134static void gen_set_nzcv(TCGv_i64 tcg_rt)
1135
1136{
1137 TCGv_i32 nzcv = tcg_temp_new_i32();
1138
1139 /* take NZCV from R[t] */
1140 tcg_gen_trunc_i64_i32(nzcv, tcg_rt);
1141
1142 /* bit 31, N */
1143 tcg_gen_andi_i32(cpu_NF, nzcv, (1 << 31));
1144 /* bit 30, Z */
1145 tcg_gen_andi_i32(cpu_ZF, nzcv, (1 << 30));
1146 tcg_gen_setcondi_i32(TCG_COND_EQ, cpu_ZF, cpu_ZF, 0);
1147 /* bit 29, C */
1148 tcg_gen_andi_i32(cpu_CF, nzcv, (1 << 29));
1149 tcg_gen_shri_i32(cpu_CF, cpu_CF, 29);
1150 /* bit 28, V */
1151 tcg_gen_andi_i32(cpu_VF, nzcv, (1 << 28));
1152 tcg_gen_shli_i32(cpu_VF, cpu_VF, 3);
1153 tcg_temp_free_i32(nzcv);
1154}
1155
Peter Maydellfea50522014-01-04 22:15:45 +00001156/* C5.6.129 MRS - move from system register
1157 * C5.6.131 MSR (register) - move to system register
1158 * C5.6.204 SYS
1159 * C5.6.205 SYSL
1160 * These are all essentially the same insn in 'read' and 'write'
1161 * versions, with varying op0 fields.
1162 */
1163static void handle_sys(DisasContext *s, uint32_t insn, bool isread,
1164 unsigned int op0, unsigned int op1, unsigned int op2,
Claudio Fontana87462e02013-12-17 19:42:32 +00001165 unsigned int crn, unsigned int crm, unsigned int rt)
1166{
Peter Maydellfea50522014-01-04 22:15:45 +00001167 const ARMCPRegInfo *ri;
1168 TCGv_i64 tcg_rt;
Claudio Fontana87462e02013-12-17 19:42:32 +00001169
Peter Maydellfea50522014-01-04 22:15:45 +00001170 ri = get_arm_cp_reginfo(s->cp_regs,
1171 ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP,
1172 crn, crm, op0, op1, op2));
Claudio Fontana87462e02013-12-17 19:42:32 +00001173
Peter Maydellfea50522014-01-04 22:15:45 +00001174 if (!ri) {
1175 /* Unknown register */
1176 unallocated_encoding(s);
1177 return;
1178 }
1179
1180 /* Check access permissions */
1181 if (!cp_access_ok(s->current_pl, ri, isread)) {
1182 unallocated_encoding(s);
1183 return;
1184 }
1185
1186 /* Handle special cases first */
1187 switch (ri->type & ~(ARM_CP_FLAG_MASK & ~ARM_CP_SPECIAL)) {
1188 case ARM_CP_NOP:
1189 return;
Peter Maydellb0d2b7d2014-01-04 22:15:45 +00001190 case ARM_CP_NZCV:
1191 tcg_rt = cpu_reg(s, rt);
1192 if (isread) {
1193 gen_get_nzcv(tcg_rt);
1194 } else {
1195 gen_set_nzcv(tcg_rt);
1196 }
1197 return;
Peter Maydellfea50522014-01-04 22:15:45 +00001198 default:
1199 break;
1200 }
1201
1202 if (use_icount && (ri->type & ARM_CP_IO)) {
1203 gen_io_start();
1204 }
1205
1206 tcg_rt = cpu_reg(s, rt);
1207
1208 if (isread) {
1209 if (ri->type & ARM_CP_CONST) {
1210 tcg_gen_movi_i64(tcg_rt, ri->resetvalue);
1211 } else if (ri->readfn) {
1212 TCGv_ptr tmpptr;
1213 gen_a64_set_pc_im(s->pc - 4);
1214 tmpptr = tcg_const_ptr(ri);
1215 gen_helper_get_cp_reg64(tcg_rt, cpu_env, tmpptr);
1216 tcg_temp_free_ptr(tmpptr);
1217 } else {
1218 tcg_gen_ld_i64(tcg_rt, cpu_env, ri->fieldoffset);
1219 }
1220 } else {
1221 if (ri->type & ARM_CP_CONST) {
1222 /* If not forbidden by access permissions, treat as WI */
1223 return;
1224 } else if (ri->writefn) {
1225 TCGv_ptr tmpptr;
1226 gen_a64_set_pc_im(s->pc - 4);
1227 tmpptr = tcg_const_ptr(ri);
1228 gen_helper_set_cp_reg64(cpu_env, tmpptr, tcg_rt);
1229 tcg_temp_free_ptr(tmpptr);
1230 } else {
1231 tcg_gen_st_i64(tcg_rt, cpu_env, ri->fieldoffset);
1232 }
1233 }
1234
1235 if (use_icount && (ri->type & ARM_CP_IO)) {
1236 /* I/O operations must end the TB here (whether read or write) */
1237 gen_io_end();
1238 s->is_jmp = DISAS_UPDATE;
1239 } else if (!isread && !(ri->type & ARM_CP_SUPPRESS_TB_END)) {
1240 /* We default to ending the TB on a coprocessor register write,
1241 * but allow this to be suppressed by the register definition
1242 * (usually only necessary to work around guest bugs).
1243 */
1244 s->is_jmp = DISAS_UPDATE;
1245 }
Claudio Fontana87462e02013-12-17 19:42:32 +00001246}
1247
1248/* C3.2.4 System
1249 * 31 22 21 20 19 18 16 15 12 11 8 7 5 4 0
1250 * +---------------------+---+-----+-----+-------+-------+-----+------+
1251 * | 1 1 0 1 0 1 0 1 0 0 | L | op0 | op1 | CRn | CRm | op2 | Rt |
1252 * +---------------------+---+-----+-----+-------+-------+-----+------+
1253 */
1254static void disas_system(DisasContext *s, uint32_t insn)
1255{
1256 unsigned int l, op0, op1, crn, crm, op2, rt;
1257 l = extract32(insn, 21, 1);
1258 op0 = extract32(insn, 19, 2);
1259 op1 = extract32(insn, 16, 3);
1260 crn = extract32(insn, 12, 4);
1261 crm = extract32(insn, 8, 4);
1262 op2 = extract32(insn, 5, 3);
1263 rt = extract32(insn, 0, 5);
1264
1265 if (op0 == 0) {
1266 if (l || rt != 31) {
1267 unallocated_encoding(s);
1268 return;
1269 }
1270 switch (crn) {
1271 case 2: /* C5.6.68 HINT */
1272 handle_hint(s, insn, op1, op2, crm);
1273 break;
1274 case 3: /* CLREX, DSB, DMB, ISB */
1275 handle_sync(s, insn, op1, op2, crm);
1276 break;
1277 case 4: /* C5.6.130 MSR (immediate) */
1278 handle_msr_i(s, insn, op1, op2, crm);
1279 break;
1280 default:
1281 unallocated_encoding(s);
1282 break;
1283 }
1284 return;
1285 }
Peter Maydellfea50522014-01-04 22:15:45 +00001286 handle_sys(s, insn, l, op0, op1, op2, crn, crm, rt);
Claudio Fontana87462e02013-12-17 19:42:32 +00001287}
1288
Alexander Graf9618e802013-12-23 23:27:30 +00001289/* C3.2.3 Exception generation
1290 *
1291 * 31 24 23 21 20 5 4 2 1 0
1292 * +-----------------+-----+------------------------+-----+----+
1293 * | 1 1 0 1 0 1 0 0 | opc | imm16 | op2 | LL |
1294 * +-----------------------+------------------------+----------+
1295 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00001296static void disas_exc(DisasContext *s, uint32_t insn)
1297{
Alexander Graf9618e802013-12-23 23:27:30 +00001298 int opc = extract32(insn, 21, 3);
1299 int op2_ll = extract32(insn, 0, 5);
1300
1301 switch (opc) {
1302 case 0:
1303 /* SVC, HVC, SMC; since we don't support the Virtualization
1304 * or TrustZone extensions these all UNDEF except SVC.
1305 */
1306 if (op2_ll != 1) {
1307 unallocated_encoding(s);
1308 break;
1309 }
1310 gen_exception_insn(s, 0, EXCP_SWI);
1311 break;
1312 case 1:
1313 if (op2_ll != 0) {
1314 unallocated_encoding(s);
1315 break;
1316 }
1317 /* BRK */
1318 gen_exception_insn(s, 0, EXCP_BKPT);
1319 break;
1320 case 2:
1321 if (op2_ll != 0) {
1322 unallocated_encoding(s);
1323 break;
1324 }
1325 /* HLT */
1326 unsupported_encoding(s, insn);
1327 break;
1328 case 5:
1329 if (op2_ll < 1 || op2_ll > 3) {
1330 unallocated_encoding(s);
1331 break;
1332 }
1333 /* DCPS1, DCPS2, DCPS3 */
1334 unsupported_encoding(s, insn);
1335 break;
1336 default:
1337 unallocated_encoding(s);
1338 break;
1339 }
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00001340}
1341
Alexander Grafb001c8c2013-12-17 19:42:33 +00001342/* C3.2.7 Unconditional branch (register)
1343 * 31 25 24 21 20 16 15 10 9 5 4 0
1344 * +---------------+-------+-------+-------+------+-------+
1345 * | 1 1 0 1 0 1 1 | opc | op2 | op3 | Rn | op4 |
1346 * +---------------+-------+-------+-------+------+-------+
1347 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00001348static void disas_uncond_b_reg(DisasContext *s, uint32_t insn)
1349{
Alexander Grafb001c8c2013-12-17 19:42:33 +00001350 unsigned int opc, op2, op3, rn, op4;
1351
1352 opc = extract32(insn, 21, 4);
1353 op2 = extract32(insn, 16, 5);
1354 op3 = extract32(insn, 10, 6);
1355 rn = extract32(insn, 5, 5);
1356 op4 = extract32(insn, 0, 5);
1357
1358 if (op4 != 0x0 || op3 != 0x0 || op2 != 0x1f) {
1359 unallocated_encoding(s);
1360 return;
1361 }
1362
1363 switch (opc) {
1364 case 0: /* BR */
1365 case 2: /* RET */
1366 break;
1367 case 1: /* BLR */
1368 tcg_gen_movi_i64(cpu_reg(s, 30), s->pc);
1369 break;
1370 case 4: /* ERET */
1371 case 5: /* DRPS */
1372 if (rn != 0x1f) {
1373 unallocated_encoding(s);
1374 } else {
1375 unsupported_encoding(s, insn);
1376 }
1377 return;
1378 default:
1379 unallocated_encoding(s);
1380 return;
1381 }
1382
1383 tcg_gen_mov_i64(cpu_pc, cpu_reg(s, rn));
1384 s->is_jmp = DISAS_JUMP;
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00001385}
1386
1387/* C3.2 Branches, exception generating and system instructions */
1388static void disas_b_exc_sys(DisasContext *s, uint32_t insn)
1389{
1390 switch (extract32(insn, 25, 7)) {
1391 case 0x0a: case 0x0b:
1392 case 0x4a: case 0x4b: /* Unconditional branch (immediate) */
1393 disas_uncond_b_imm(s, insn);
1394 break;
1395 case 0x1a: case 0x5a: /* Compare & branch (immediate) */
1396 disas_comp_b_imm(s, insn);
1397 break;
1398 case 0x1b: case 0x5b: /* Test & branch (immediate) */
1399 disas_test_b_imm(s, insn);
1400 break;
1401 case 0x2a: /* Conditional branch (immediate) */
1402 disas_cond_b_imm(s, insn);
1403 break;
1404 case 0x6a: /* Exception generation / System */
1405 if (insn & (1 << 24)) {
1406 disas_system(s, insn);
1407 } else {
1408 disas_exc(s, insn);
1409 }
1410 break;
1411 case 0x6b: /* Unconditional branch (register) */
1412 disas_uncond_b_reg(s, insn);
1413 break;
1414 default:
1415 unallocated_encoding(s);
1416 break;
1417 }
1418}
1419
Michael Matzfa2ef212014-01-04 22:15:47 +00001420/*
1421 * Load/Store exclusive instructions are implemented by remembering
1422 * the value/address loaded, and seeing if these are the same
1423 * when the store is performed. This is not actually the architecturally
1424 * mandated semantics, but it works for typical guest code sequences
1425 * and avoids having to monitor regular stores.
1426 *
1427 * In system emulation mode only one CPU will be running at once, so
1428 * this sequence is effectively atomic. In user emulation mode we
1429 * throw an exception and handle the atomic operation elsewhere.
1430 */
1431static void gen_load_exclusive(DisasContext *s, int rt, int rt2,
1432 TCGv_i64 addr, int size, bool is_pair)
1433{
1434 TCGv_i64 tmp = tcg_temp_new_i64();
1435 TCGMemOp memop = MO_TE + size;
1436
1437 g_assert(size <= 3);
1438 tcg_gen_qemu_ld_i64(tmp, addr, get_mem_index(s), memop);
1439
1440 if (is_pair) {
1441 TCGv_i64 addr2 = tcg_temp_new_i64();
1442 TCGv_i64 hitmp = tcg_temp_new_i64();
1443
1444 g_assert(size >= 2);
1445 tcg_gen_addi_i64(addr2, addr, 1 << size);
1446 tcg_gen_qemu_ld_i64(hitmp, addr2, get_mem_index(s), memop);
1447 tcg_temp_free_i64(addr2);
1448 tcg_gen_mov_i64(cpu_exclusive_high, hitmp);
1449 tcg_gen_mov_i64(cpu_reg(s, rt2), hitmp);
1450 tcg_temp_free_i64(hitmp);
1451 }
1452
1453 tcg_gen_mov_i64(cpu_exclusive_val, tmp);
1454 tcg_gen_mov_i64(cpu_reg(s, rt), tmp);
1455
1456 tcg_temp_free_i64(tmp);
1457 tcg_gen_mov_i64(cpu_exclusive_addr, addr);
1458}
1459
1460#ifdef CONFIG_USER_ONLY
1461static void gen_store_exclusive(DisasContext *s, int rd, int rt, int rt2,
1462 TCGv_i64 addr, int size, int is_pair)
1463{
1464 tcg_gen_mov_i64(cpu_exclusive_test, addr);
1465 tcg_gen_movi_i32(cpu_exclusive_info,
1466 size | is_pair << 2 | (rd << 4) | (rt << 9) | (rt2 << 14));
1467 gen_exception_insn(s, 4, EXCP_STREX);
1468}
1469#else
1470static void gen_store_exclusive(DisasContext *s, int rd, int rt, int rt2,
1471 TCGv_i64 addr, int size, int is_pair)
1472{
1473 qemu_log_mask(LOG_UNIMP,
1474 "%s:%d: system mode store_exclusive unsupported "
1475 "at pc=%016" PRIx64 "\n",
1476 __FILE__, __LINE__, s->pc - 4);
1477}
1478#endif
1479
1480/* C3.3.6 Load/store exclusive
1481 *
1482 * 31 30 29 24 23 22 21 20 16 15 14 10 9 5 4 0
1483 * +-----+-------------+----+---+----+------+----+-------+------+------+
1484 * | sz | 0 0 1 0 0 0 | o2 | L | o1 | Rs | o0 | Rt2 | Rn | Rt |
1485 * +-----+-------------+----+---+----+------+----+-------+------+------+
1486 *
1487 * sz: 00 -> 8 bit, 01 -> 16 bit, 10 -> 32 bit, 11 -> 64 bit
1488 * L: 0 -> store, 1 -> load
1489 * o2: 0 -> exclusive, 1 -> not
1490 * o1: 0 -> single register, 1 -> register pair
1491 * o0: 1 -> load-acquire/store-release, 0 -> not
1492 *
1493 * o0 == 0 AND o2 == 1 is un-allocated
1494 * o1 == 1 is un-allocated except for 32 and 64 bit sizes
1495 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00001496static void disas_ldst_excl(DisasContext *s, uint32_t insn)
1497{
Michael Matzfa2ef212014-01-04 22:15:47 +00001498 int rt = extract32(insn, 0, 5);
1499 int rn = extract32(insn, 5, 5);
1500 int rt2 = extract32(insn, 10, 5);
1501 int is_lasr = extract32(insn, 15, 1);
1502 int rs = extract32(insn, 16, 5);
1503 int is_pair = extract32(insn, 21, 1);
1504 int is_store = !extract32(insn, 22, 1);
1505 int is_excl = !extract32(insn, 23, 1);
1506 int size = extract32(insn, 30, 2);
1507 TCGv_i64 tcg_addr;
1508
1509 if ((!is_excl && !is_lasr) ||
1510 (is_pair && size < 2)) {
1511 unallocated_encoding(s);
1512 return;
1513 }
1514
1515 if (rn == 31) {
1516 gen_check_sp_alignment(s);
1517 }
1518 tcg_addr = read_cpu_reg_sp(s, rn, 1);
1519
1520 /* Note that since TCG is single threaded load-acquire/store-release
1521 * semantics require no extra if (is_lasr) { ... } handling.
1522 */
1523
1524 if (is_excl) {
1525 if (!is_store) {
1526 gen_load_exclusive(s, rt, rt2, tcg_addr, size, is_pair);
1527 } else {
1528 gen_store_exclusive(s, rs, rt, rt2, tcg_addr, size, is_pair);
1529 }
1530 } else {
1531 TCGv_i64 tcg_rt = cpu_reg(s, rt);
1532 if (is_store) {
1533 do_gpr_st(s, tcg_rt, tcg_addr, size);
1534 } else {
1535 do_gpr_ld(s, tcg_rt, tcg_addr, size, false, false);
1536 }
1537 if (is_pair) {
1538 TCGv_i64 tcg_rt2 = cpu_reg(s, rt);
1539 tcg_gen_addi_i64(tcg_addr, tcg_addr, 1 << size);
1540 if (is_store) {
1541 do_gpr_st(s, tcg_rt2, tcg_addr, size);
1542 } else {
1543 do_gpr_ld(s, tcg_rt2, tcg_addr, size, false, false);
1544 }
1545 }
1546 }
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00001547}
1548
Alexander Graf32b64e82014-01-04 22:15:46 +00001549/*
1550 * C3.3.5 Load register (literal)
1551 *
1552 * 31 30 29 27 26 25 24 23 5 4 0
1553 * +-----+-------+---+-----+-------------------+-------+
1554 * | opc | 0 1 1 | V | 0 0 | imm19 | Rt |
1555 * +-----+-------+---+-----+-------------------+-------+
1556 *
1557 * V: 1 -> vector (simd/fp)
1558 * opc (non-vector): 00 -> 32 bit, 01 -> 64 bit,
1559 * 10-> 32 bit signed, 11 -> prefetch
1560 * opc (vector): 00 -> 32 bit, 01 -> 64 bit, 10 -> 128 bit (11 unallocated)
1561 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00001562static void disas_ld_lit(DisasContext *s, uint32_t insn)
1563{
Alexander Graf32b64e82014-01-04 22:15:46 +00001564 int rt = extract32(insn, 0, 5);
1565 int64_t imm = sextract32(insn, 5, 19) << 2;
1566 bool is_vector = extract32(insn, 26, 1);
1567 int opc = extract32(insn, 30, 2);
1568 bool is_signed = false;
1569 int size = 2;
1570 TCGv_i64 tcg_rt, tcg_addr;
1571
1572 if (is_vector) {
1573 if (opc == 3) {
1574 unallocated_encoding(s);
1575 return;
1576 }
1577 size = 2 + opc;
1578 } else {
1579 if (opc == 3) {
1580 /* PRFM (literal) : prefetch */
1581 return;
1582 }
1583 size = 2 + extract32(opc, 0, 1);
1584 is_signed = extract32(opc, 1, 1);
1585 }
1586
1587 tcg_rt = cpu_reg(s, rt);
1588
1589 tcg_addr = tcg_const_i64((s->pc - 4) + imm);
1590 if (is_vector) {
1591 do_fp_ld(s, rt, tcg_addr, size);
1592 } else {
1593 do_gpr_ld(s, tcg_rt, tcg_addr, size, is_signed, false);
1594 }
1595 tcg_temp_free_i64(tcg_addr);
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00001596}
1597
Peter Maydell4a08d472013-12-22 22:32:27 +00001598/*
1599 * C5.6.80 LDNP (Load Pair - non-temporal hint)
1600 * C5.6.81 LDP (Load Pair - non vector)
1601 * C5.6.82 LDPSW (Load Pair Signed Word - non vector)
1602 * C5.6.176 STNP (Store Pair - non-temporal hint)
1603 * C5.6.177 STP (Store Pair - non vector)
1604 * C6.3.165 LDNP (Load Pair of SIMD&FP - non-temporal hint)
1605 * C6.3.165 LDP (Load Pair of SIMD&FP)
1606 * C6.3.284 STNP (Store Pair of SIMD&FP - non-temporal hint)
1607 * C6.3.284 STP (Store Pair of SIMD&FP)
1608 *
1609 * 31 30 29 27 26 25 24 23 22 21 15 14 10 9 5 4 0
1610 * +-----+-------+---+---+-------+---+-----------------------------+
1611 * | opc | 1 0 1 | V | 0 | index | L | imm7 | Rt2 | Rn | Rt |
1612 * +-----+-------+---+---+-------+---+-------+-------+------+------+
1613 *
1614 * opc: LDP/STP/LDNP/STNP 00 -> 32 bit, 10 -> 64 bit
1615 * LDPSW 01
1616 * LDP/STP/LDNP/STNP (SIMD) 00 -> 32 bit, 01 -> 64 bit, 10 -> 128 bit
1617 * V: 0 -> GPR, 1 -> Vector
1618 * idx: 00 -> signed offset with non-temporal hint, 01 -> post-index,
1619 * 10 -> signed offset, 11 -> pre-index
1620 * L: 0 -> Store 1 -> Load
1621 *
1622 * Rt, Rt2 = GPR or SIMD registers to be stored
1623 * Rn = general purpose register containing address
1624 * imm7 = signed offset (multiple of 4 or 8 depending on size)
1625 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00001626static void disas_ldst_pair(DisasContext *s, uint32_t insn)
1627{
Peter Maydell4a08d472013-12-22 22:32:27 +00001628 int rt = extract32(insn, 0, 5);
1629 int rn = extract32(insn, 5, 5);
1630 int rt2 = extract32(insn, 10, 5);
1631 int64_t offset = sextract32(insn, 15, 7);
1632 int index = extract32(insn, 23, 2);
1633 bool is_vector = extract32(insn, 26, 1);
1634 bool is_load = extract32(insn, 22, 1);
1635 int opc = extract32(insn, 30, 2);
1636
1637 bool is_signed = false;
1638 bool postindex = false;
1639 bool wback = false;
1640
1641 TCGv_i64 tcg_addr; /* calculated address */
1642 int size;
1643
1644 if (opc == 3) {
1645 unallocated_encoding(s);
1646 return;
1647 }
1648
1649 if (is_vector) {
1650 size = 2 + opc;
1651 } else {
1652 size = 2 + extract32(opc, 1, 1);
1653 is_signed = extract32(opc, 0, 1);
1654 if (!is_load && is_signed) {
1655 unallocated_encoding(s);
1656 return;
1657 }
1658 }
1659
1660 switch (index) {
1661 case 1: /* post-index */
1662 postindex = true;
1663 wback = true;
1664 break;
1665 case 0:
1666 /* signed offset with "non-temporal" hint. Since we don't emulate
1667 * caches we don't care about hints to the cache system about
1668 * data access patterns, and handle this identically to plain
1669 * signed offset.
1670 */
1671 if (is_signed) {
1672 /* There is no non-temporal-hint version of LDPSW */
1673 unallocated_encoding(s);
1674 return;
1675 }
1676 postindex = false;
1677 break;
1678 case 2: /* signed offset, rn not updated */
1679 postindex = false;
1680 break;
1681 case 3: /* pre-index */
1682 postindex = false;
1683 wback = true;
1684 break;
1685 }
1686
1687 offset <<= size;
1688
1689 if (rn == 31) {
1690 gen_check_sp_alignment(s);
1691 }
1692
1693 tcg_addr = read_cpu_reg_sp(s, rn, 1);
1694
1695 if (!postindex) {
1696 tcg_gen_addi_i64(tcg_addr, tcg_addr, offset);
1697 }
1698
1699 if (is_vector) {
1700 if (is_load) {
1701 do_fp_ld(s, rt, tcg_addr, size);
1702 } else {
1703 do_fp_st(s, rt, tcg_addr, size);
1704 }
1705 } else {
1706 TCGv_i64 tcg_rt = cpu_reg(s, rt);
1707 if (is_load) {
1708 do_gpr_ld(s, tcg_rt, tcg_addr, size, is_signed, false);
1709 } else {
1710 do_gpr_st(s, tcg_rt, tcg_addr, size);
1711 }
1712 }
1713 tcg_gen_addi_i64(tcg_addr, tcg_addr, 1 << size);
1714 if (is_vector) {
1715 if (is_load) {
1716 do_fp_ld(s, rt2, tcg_addr, size);
1717 } else {
1718 do_fp_st(s, rt2, tcg_addr, size);
1719 }
1720 } else {
1721 TCGv_i64 tcg_rt2 = cpu_reg(s, rt2);
1722 if (is_load) {
1723 do_gpr_ld(s, tcg_rt2, tcg_addr, size, is_signed, false);
1724 } else {
1725 do_gpr_st(s, tcg_rt2, tcg_addr, size);
1726 }
1727 }
1728
1729 if (wback) {
1730 if (postindex) {
1731 tcg_gen_addi_i64(tcg_addr, tcg_addr, offset - (1 << size));
1732 } else {
1733 tcg_gen_subi_i64(tcg_addr, tcg_addr, 1 << size);
1734 }
1735 tcg_gen_mov_i64(cpu_reg_sp(s, rn), tcg_addr);
1736 }
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00001737}
1738
Alex Bennéed5612f12013-12-23 23:27:28 +00001739/*
Alex Bennéea5e94a92013-12-23 23:27:29 +00001740 * C3.3.8 Load/store (immediate post-indexed)
1741 * C3.3.9 Load/store (immediate pre-indexed)
1742 * C3.3.12 Load/store (unscaled immediate)
1743 *
1744 * 31 30 29 27 26 25 24 23 22 21 20 12 11 10 9 5 4 0
1745 * +----+-------+---+-----+-----+---+--------+-----+------+------+
1746 * |size| 1 1 1 | V | 0 0 | opc | 0 | imm9 | idx | Rn | Rt |
1747 * +----+-------+---+-----+-----+---+--------+-----+------+------+
1748 *
1749 * idx = 01 -> post-indexed, 11 pre-indexed, 00 unscaled imm. (no writeback)
1750 * V = 0 -> non-vector
1751 * size: 00 -> 8 bit, 01 -> 16 bit, 10 -> 32 bit, 11 -> 64bit
1752 * opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
1753 */
1754static void disas_ldst_reg_imm9(DisasContext *s, uint32_t insn)
1755{
1756 int rt = extract32(insn, 0, 5);
1757 int rn = extract32(insn, 5, 5);
1758 int imm9 = sextract32(insn, 12, 9);
1759 int opc = extract32(insn, 22, 2);
1760 int size = extract32(insn, 30, 2);
1761 int idx = extract32(insn, 10, 2);
1762 bool is_signed = false;
1763 bool is_store = false;
1764 bool is_extended = false;
1765 bool is_vector = extract32(insn, 26, 1);
1766 bool post_index;
1767 bool writeback;
1768
1769 TCGv_i64 tcg_addr;
1770
1771 if (is_vector) {
1772 size |= (opc & 2) << 1;
1773 if (size > 4) {
1774 unallocated_encoding(s);
1775 return;
1776 }
1777 is_store = ((opc & 1) == 0);
1778 } else {
1779 if (size == 3 && opc == 2) {
1780 /* PRFM - prefetch */
1781 return;
1782 }
1783 if (opc == 3 && size > 1) {
1784 unallocated_encoding(s);
1785 return;
1786 }
1787 is_store = (opc == 0);
1788 is_signed = opc & (1<<1);
1789 is_extended = (size < 3) && (opc & 1);
1790 }
1791
1792 switch (idx) {
1793 case 0:
1794 post_index = false;
1795 writeback = false;
1796 break;
1797 case 1:
1798 post_index = true;
1799 writeback = true;
1800 break;
1801 case 3:
1802 post_index = false;
1803 writeback = true;
1804 break;
1805 case 2:
1806 g_assert(false);
1807 break;
1808 }
1809
1810 if (rn == 31) {
1811 gen_check_sp_alignment(s);
1812 }
1813 tcg_addr = read_cpu_reg_sp(s, rn, 1);
1814
1815 if (!post_index) {
1816 tcg_gen_addi_i64(tcg_addr, tcg_addr, imm9);
1817 }
1818
1819 if (is_vector) {
1820 if (is_store) {
1821 do_fp_st(s, rt, tcg_addr, size);
1822 } else {
1823 do_fp_ld(s, rt, tcg_addr, size);
1824 }
1825 } else {
1826 TCGv_i64 tcg_rt = cpu_reg(s, rt);
1827 if (is_store) {
1828 do_gpr_st(s, tcg_rt, tcg_addr, size);
1829 } else {
1830 do_gpr_ld(s, tcg_rt, tcg_addr, size, is_signed, is_extended);
1831 }
1832 }
1833
1834 if (writeback) {
1835 TCGv_i64 tcg_rn = cpu_reg_sp(s, rn);
1836 if (post_index) {
1837 tcg_gen_addi_i64(tcg_addr, tcg_addr, imm9);
1838 }
1839 tcg_gen_mov_i64(tcg_rn, tcg_addr);
1840 }
1841}
1842
1843/*
Alex Bennée229b7a02013-12-23 23:27:29 +00001844 * C3.3.10 Load/store (register offset)
1845 *
1846 * 31 30 29 27 26 25 24 23 22 21 20 16 15 13 12 11 10 9 5 4 0
1847 * +----+-------+---+-----+-----+---+------+-----+--+-----+----+----+
1848 * |size| 1 1 1 | V | 0 0 | opc | 1 | Rm | opt | S| 1 0 | Rn | Rt |
1849 * +----+-------+---+-----+-----+---+------+-----+--+-----+----+----+
1850 *
1851 * For non-vector:
1852 * size: 00-> byte, 01 -> 16 bit, 10 -> 32bit, 11 -> 64bit
1853 * opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
1854 * For vector:
1855 * size is opc<1>:size<1:0> so 100 -> 128 bit; 110 and 111 unallocated
1856 * opc<0>: 0 -> store, 1 -> load
1857 * V: 1 -> vector/simd
1858 * opt: extend encoding (see DecodeRegExtend)
1859 * S: if S=1 then scale (essentially index by sizeof(size))
1860 * Rt: register to transfer into/out of
1861 * Rn: address register or SP for base
1862 * Rm: offset register or ZR for offset
1863 */
1864static void disas_ldst_reg_roffset(DisasContext *s, uint32_t insn)
1865{
1866 int rt = extract32(insn, 0, 5);
1867 int rn = extract32(insn, 5, 5);
1868 int shift = extract32(insn, 12, 1);
1869 int rm = extract32(insn, 16, 5);
1870 int opc = extract32(insn, 22, 2);
1871 int opt = extract32(insn, 13, 3);
1872 int size = extract32(insn, 30, 2);
1873 bool is_signed = false;
1874 bool is_store = false;
1875 bool is_extended = false;
1876 bool is_vector = extract32(insn, 26, 1);
1877
1878 TCGv_i64 tcg_rm;
1879 TCGv_i64 tcg_addr;
1880
1881 if (extract32(opt, 1, 1) == 0) {
1882 unallocated_encoding(s);
1883 return;
1884 }
1885
1886 if (is_vector) {
1887 size |= (opc & 2) << 1;
1888 if (size > 4) {
1889 unallocated_encoding(s);
1890 return;
1891 }
1892 is_store = !extract32(opc, 0, 1);
1893 } else {
1894 if (size == 3 && opc == 2) {
1895 /* PRFM - prefetch */
1896 return;
1897 }
1898 if (opc == 3 && size > 1) {
1899 unallocated_encoding(s);
1900 return;
1901 }
1902 is_store = (opc == 0);
1903 is_signed = extract32(opc, 1, 1);
1904 is_extended = (size < 3) && extract32(opc, 0, 1);
1905 }
1906
1907 if (rn == 31) {
1908 gen_check_sp_alignment(s);
1909 }
1910 tcg_addr = read_cpu_reg_sp(s, rn, 1);
1911
1912 tcg_rm = read_cpu_reg(s, rm, 1);
1913 ext_and_shift_reg(tcg_rm, tcg_rm, opt, shift ? size : 0);
1914
1915 tcg_gen_add_i64(tcg_addr, tcg_addr, tcg_rm);
1916
1917 if (is_vector) {
1918 if (is_store) {
1919 do_fp_st(s, rt, tcg_addr, size);
1920 } else {
1921 do_fp_ld(s, rt, tcg_addr, size);
1922 }
1923 } else {
1924 TCGv_i64 tcg_rt = cpu_reg(s, rt);
1925 if (is_store) {
1926 do_gpr_st(s, tcg_rt, tcg_addr, size);
1927 } else {
1928 do_gpr_ld(s, tcg_rt, tcg_addr, size, is_signed, is_extended);
1929 }
1930 }
1931}
1932
1933/*
Alex Bennéed5612f12013-12-23 23:27:28 +00001934 * C3.3.13 Load/store (unsigned immediate)
1935 *
1936 * 31 30 29 27 26 25 24 23 22 21 10 9 5
1937 * +----+-------+---+-----+-----+------------+-------+------+
1938 * |size| 1 1 1 | V | 0 1 | opc | imm12 | Rn | Rt |
1939 * +----+-------+---+-----+-----+------------+-------+------+
1940 *
1941 * For non-vector:
1942 * size: 00-> byte, 01 -> 16 bit, 10 -> 32bit, 11 -> 64bit
1943 * opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
1944 * For vector:
1945 * size is opc<1>:size<1:0> so 100 -> 128 bit; 110 and 111 unallocated
1946 * opc<0>: 0 -> store, 1 -> load
1947 * Rn: base address register (inc SP)
1948 * Rt: target register
1949 */
1950static void disas_ldst_reg_unsigned_imm(DisasContext *s, uint32_t insn)
1951{
1952 int rt = extract32(insn, 0, 5);
1953 int rn = extract32(insn, 5, 5);
1954 unsigned int imm12 = extract32(insn, 10, 12);
1955 bool is_vector = extract32(insn, 26, 1);
1956 int size = extract32(insn, 30, 2);
1957 int opc = extract32(insn, 22, 2);
1958 unsigned int offset;
1959
1960 TCGv_i64 tcg_addr;
1961
1962 bool is_store;
1963 bool is_signed = false;
1964 bool is_extended = false;
1965
1966 if (is_vector) {
1967 size |= (opc & 2) << 1;
1968 if (size > 4) {
1969 unallocated_encoding(s);
1970 return;
1971 }
1972 is_store = !extract32(opc, 0, 1);
1973 } else {
1974 if (size == 3 && opc == 2) {
1975 /* PRFM - prefetch */
1976 return;
1977 }
1978 if (opc == 3 && size > 1) {
1979 unallocated_encoding(s);
1980 return;
1981 }
1982 is_store = (opc == 0);
1983 is_signed = extract32(opc, 1, 1);
1984 is_extended = (size < 3) && extract32(opc, 0, 1);
1985 }
1986
1987 if (rn == 31) {
1988 gen_check_sp_alignment(s);
1989 }
1990 tcg_addr = read_cpu_reg_sp(s, rn, 1);
1991 offset = imm12 << size;
1992 tcg_gen_addi_i64(tcg_addr, tcg_addr, offset);
1993
1994 if (is_vector) {
1995 if (is_store) {
1996 do_fp_st(s, rt, tcg_addr, size);
1997 } else {
1998 do_fp_ld(s, rt, tcg_addr, size);
1999 }
2000 } else {
2001 TCGv_i64 tcg_rt = cpu_reg(s, rt);
2002 if (is_store) {
2003 do_gpr_st(s, tcg_rt, tcg_addr, size);
2004 } else {
2005 do_gpr_ld(s, tcg_rt, tcg_addr, size, is_signed, is_extended);
2006 }
2007 }
2008}
2009
Alex Bennéea5e94a92013-12-23 23:27:29 +00002010/* Load/store register (immediate forms) */
2011static void disas_ldst_reg_imm(DisasContext *s, uint32_t insn)
2012{
2013 switch (extract32(insn, 10, 2)) {
2014 case 0: case 1: case 3:
2015 /* Load/store register (unscaled immediate) */
2016 /* Load/store immediate pre/post-indexed */
2017 disas_ldst_reg_imm9(s, insn);
2018 break;
2019 case 2:
2020 /* Load/store register unprivileged */
2021 unsupported_encoding(s, insn);
2022 break;
2023 default:
2024 unallocated_encoding(s);
2025 break;
2026 }
2027}
2028
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00002029/* Load/store register (all forms) */
2030static void disas_ldst_reg(DisasContext *s, uint32_t insn)
2031{
Alex Bennéed5612f12013-12-23 23:27:28 +00002032 switch (extract32(insn, 24, 2)) {
2033 case 0:
Alex Bennée229b7a02013-12-23 23:27:29 +00002034 if (extract32(insn, 21, 1) == 1 && extract32(insn, 10, 2) == 2) {
2035 disas_ldst_reg_roffset(s, insn);
2036 } else {
Alex Bennéea5e94a92013-12-23 23:27:29 +00002037 disas_ldst_reg_imm(s, insn);
Alex Bennée229b7a02013-12-23 23:27:29 +00002038 }
Alex Bennéed5612f12013-12-23 23:27:28 +00002039 break;
2040 case 1:
2041 disas_ldst_reg_unsigned_imm(s, insn);
2042 break;
2043 default:
2044 unallocated_encoding(s);
2045 break;
2046 }
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00002047}
2048
Alex Bennée539ed652014-01-23 14:37:07 +00002049/* C3.3.1 AdvSIMD load/store multiple structures
2050 *
2051 * 31 30 29 23 22 21 16 15 12 11 10 9 5 4 0
2052 * +---+---+---------------+---+-------------+--------+------+------+------+
2053 * | 0 | Q | 0 0 1 1 0 0 0 | L | 0 0 0 0 0 0 | opcode | size | Rn | Rt |
2054 * +---+---+---------------+---+-------------+--------+------+------+------+
2055 *
2056 * C3.3.2 AdvSIMD load/store multiple structures (post-indexed)
2057 *
2058 * 31 30 29 23 22 21 20 16 15 12 11 10 9 5 4 0
2059 * +---+---+---------------+---+---+---------+--------+------+------+------+
2060 * | 0 | Q | 0 0 1 1 0 0 1 | L | 0 | Rm | opcode | size | Rn | Rt |
2061 * +---+---+---------------+---+---+---------+--------+------+------+------+
2062 *
2063 * Rt: first (or only) SIMD&FP register to be transferred
2064 * Rn: base address or SP
2065 * Rm (post-index only): post-index register (when !31) or size dependent #imm
2066 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00002067static void disas_ldst_multiple_struct(DisasContext *s, uint32_t insn)
2068{
Alex Bennée539ed652014-01-23 14:37:07 +00002069 int rt = extract32(insn, 0, 5);
2070 int rn = extract32(insn, 5, 5);
2071 int size = extract32(insn, 10, 2);
2072 int opcode = extract32(insn, 12, 4);
2073 bool is_store = !extract32(insn, 22, 1);
2074 bool is_postidx = extract32(insn, 23, 1);
2075 bool is_q = extract32(insn, 30, 1);
2076 TCGv_i64 tcg_addr, tcg_rn;
2077
2078 int ebytes = 1 << size;
2079 int elements = (is_q ? 128 : 64) / (8 << size);
2080 int rpt; /* num iterations */
2081 int selem; /* structure elements */
2082 int r;
2083
2084 if (extract32(insn, 31, 1) || extract32(insn, 21, 1)) {
2085 unallocated_encoding(s);
2086 return;
2087 }
2088
2089 /* From the shared decode logic */
2090 switch (opcode) {
2091 case 0x0:
2092 rpt = 1;
2093 selem = 4;
2094 break;
2095 case 0x2:
2096 rpt = 4;
2097 selem = 1;
2098 break;
2099 case 0x4:
2100 rpt = 1;
2101 selem = 3;
2102 break;
2103 case 0x6:
2104 rpt = 3;
2105 selem = 1;
2106 break;
2107 case 0x7:
2108 rpt = 1;
2109 selem = 1;
2110 break;
2111 case 0x8:
2112 rpt = 1;
2113 selem = 2;
2114 break;
2115 case 0xa:
2116 rpt = 2;
2117 selem = 1;
2118 break;
2119 default:
2120 unallocated_encoding(s);
2121 return;
2122 }
2123
2124 if (size == 3 && !is_q && selem != 1) {
2125 /* reserved */
2126 unallocated_encoding(s);
2127 return;
2128 }
2129
2130 if (rn == 31) {
2131 gen_check_sp_alignment(s);
2132 }
2133
2134 tcg_rn = cpu_reg_sp(s, rn);
2135 tcg_addr = tcg_temp_new_i64();
2136 tcg_gen_mov_i64(tcg_addr, tcg_rn);
2137
2138 for (r = 0; r < rpt; r++) {
2139 int e;
2140 for (e = 0; e < elements; e++) {
2141 int tt = (rt + r) % 32;
2142 int xs;
2143 for (xs = 0; xs < selem; xs++) {
2144 if (is_store) {
2145 do_vec_st(s, tt, e, tcg_addr, size);
2146 } else {
2147 do_vec_ld(s, tt, e, tcg_addr, size);
2148
2149 /* For non-quad operations, setting a slice of the low
2150 * 64 bits of the register clears the high 64 bits (in
2151 * the ARM ARM pseudocode this is implicit in the fact
2152 * that 'rval' is a 64 bit wide variable). We optimize
2153 * by noticing that we only need to do this the first
2154 * time we touch a register.
2155 */
2156 if (!is_q && e == 0 && (r == 0 || xs == selem - 1)) {
2157 clear_vec_high(s, tt);
2158 }
2159 }
2160 tcg_gen_addi_i64(tcg_addr, tcg_addr, ebytes);
2161 tt = (tt + 1) % 32;
2162 }
2163 }
2164 }
2165
2166 if (is_postidx) {
2167 int rm = extract32(insn, 16, 5);
2168 if (rm == 31) {
2169 tcg_gen_mov_i64(tcg_rn, tcg_addr);
2170 } else {
2171 tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, rm));
2172 }
2173 }
2174 tcg_temp_free_i64(tcg_addr);
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00002175}
2176
Peter Maydell561f5582014-01-23 14:37:07 +00002177/* C3.3.3 AdvSIMD load/store single structure
2178 *
2179 * 31 30 29 23 22 21 20 16 15 13 12 11 10 9 5 4 0
2180 * +---+---+---------------+-----+-----------+-----+---+------+------+------+
2181 * | 0 | Q | 0 0 1 1 0 1 0 | L R | 0 0 0 0 0 | opc | S | size | Rn | Rt |
2182 * +---+---+---------------+-----+-----------+-----+---+------+------+------+
2183 *
2184 * C3.3.4 AdvSIMD load/store single structure (post-indexed)
2185 *
2186 * 31 30 29 23 22 21 20 16 15 13 12 11 10 9 5 4 0
2187 * +---+---+---------------+-----+-----------+-----+---+------+------+------+
2188 * | 0 | Q | 0 0 1 1 0 1 1 | L R | Rm | opc | S | size | Rn | Rt |
2189 * +---+---+---------------+-----+-----------+-----+---+------+------+------+
2190 *
2191 * Rt: first (or only) SIMD&FP register to be transferred
2192 * Rn: base address or SP
2193 * Rm (post-index only): post-index register (when !31) or size dependent #imm
2194 * index = encoded in Q:S:size dependent on size
2195 *
2196 * lane_size = encoded in R, opc
2197 * transfer width = encoded in opc, S, size
2198 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00002199static void disas_ldst_single_struct(DisasContext *s, uint32_t insn)
2200{
Peter Maydell561f5582014-01-23 14:37:07 +00002201 int rt = extract32(insn, 0, 5);
2202 int rn = extract32(insn, 5, 5);
2203 int size = extract32(insn, 10, 2);
2204 int S = extract32(insn, 12, 1);
2205 int opc = extract32(insn, 13, 3);
2206 int R = extract32(insn, 21, 1);
2207 int is_load = extract32(insn, 22, 1);
2208 int is_postidx = extract32(insn, 23, 1);
2209 int is_q = extract32(insn, 30, 1);
2210
2211 int scale = extract32(opc, 1, 2);
2212 int selem = (extract32(opc, 0, 1) << 1 | R) + 1;
2213 bool replicate = false;
2214 int index = is_q << 3 | S << 2 | size;
2215 int ebytes, xs;
2216 TCGv_i64 tcg_addr, tcg_rn;
2217
2218 switch (scale) {
2219 case 3:
2220 if (!is_load || S) {
2221 unallocated_encoding(s);
2222 return;
2223 }
2224 scale = size;
2225 replicate = true;
2226 break;
2227 case 0:
2228 break;
2229 case 1:
2230 if (extract32(size, 0, 1)) {
2231 unallocated_encoding(s);
2232 return;
2233 }
2234 index >>= 1;
2235 break;
2236 case 2:
2237 if (extract32(size, 1, 1)) {
2238 unallocated_encoding(s);
2239 return;
2240 }
2241 if (!extract32(size, 0, 1)) {
2242 index >>= 2;
2243 } else {
2244 if (S) {
2245 unallocated_encoding(s);
2246 return;
2247 }
2248 index >>= 3;
2249 scale = 3;
2250 }
2251 break;
2252 default:
2253 g_assert_not_reached();
2254 }
2255
2256 ebytes = 1 << scale;
2257
2258 if (rn == 31) {
2259 gen_check_sp_alignment(s);
2260 }
2261
2262 tcg_rn = cpu_reg_sp(s, rn);
2263 tcg_addr = tcg_temp_new_i64();
2264 tcg_gen_mov_i64(tcg_addr, tcg_rn);
2265
2266 for (xs = 0; xs < selem; xs++) {
2267 if (replicate) {
2268 /* Load and replicate to all elements */
2269 uint64_t mulconst;
2270 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
2271
2272 tcg_gen_qemu_ld_i64(tcg_tmp, tcg_addr,
2273 get_mem_index(s), MO_TE + scale);
2274 switch (scale) {
2275 case 0:
2276 mulconst = 0x0101010101010101ULL;
2277 break;
2278 case 1:
2279 mulconst = 0x0001000100010001ULL;
2280 break;
2281 case 2:
2282 mulconst = 0x0000000100000001ULL;
2283 break;
2284 case 3:
2285 mulconst = 0;
2286 break;
2287 default:
2288 g_assert_not_reached();
2289 }
2290 if (mulconst) {
2291 tcg_gen_muli_i64(tcg_tmp, tcg_tmp, mulconst);
2292 }
2293 write_vec_element(s, tcg_tmp, rt, 0, MO_64);
2294 if (is_q) {
2295 write_vec_element(s, tcg_tmp, rt, 1, MO_64);
2296 } else {
2297 clear_vec_high(s, rt);
2298 }
2299 tcg_temp_free_i64(tcg_tmp);
2300 } else {
2301 /* Load/store one element per register */
2302 if (is_load) {
2303 do_vec_ld(s, rt, index, tcg_addr, MO_TE + scale);
2304 } else {
2305 do_vec_st(s, rt, index, tcg_addr, MO_TE + scale);
2306 }
2307 }
2308 tcg_gen_addi_i64(tcg_addr, tcg_addr, ebytes);
2309 rt = (rt + 1) % 32;
2310 }
2311
2312 if (is_postidx) {
2313 int rm = extract32(insn, 16, 5);
2314 if (rm == 31) {
2315 tcg_gen_mov_i64(tcg_rn, tcg_addr);
2316 } else {
2317 tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, rm));
2318 }
2319 }
2320 tcg_temp_free_i64(tcg_addr);
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00002321}
2322
2323/* C3.3 Loads and stores */
2324static void disas_ldst(DisasContext *s, uint32_t insn)
2325{
2326 switch (extract32(insn, 24, 6)) {
2327 case 0x08: /* Load/store exclusive */
2328 disas_ldst_excl(s, insn);
2329 break;
2330 case 0x18: case 0x1c: /* Load register (literal) */
2331 disas_ld_lit(s, insn);
2332 break;
2333 case 0x28: case 0x29:
2334 case 0x2c: case 0x2d: /* Load/store pair (all forms) */
2335 disas_ldst_pair(s, insn);
2336 break;
2337 case 0x38: case 0x39:
2338 case 0x3c: case 0x3d: /* Load/store register (all forms) */
2339 disas_ldst_reg(s, insn);
2340 break;
2341 case 0x0c: /* AdvSIMD load/store multiple structures */
2342 disas_ldst_multiple_struct(s, insn);
2343 break;
2344 case 0x0d: /* AdvSIMD load/store single structure */
2345 disas_ldst_single_struct(s, insn);
2346 break;
2347 default:
2348 unallocated_encoding(s);
2349 break;
2350 }
2351}
2352
Alexander Graf15bfe8b2013-12-17 19:42:34 +00002353/* C3.4.6 PC-rel. addressing
2354 * 31 30 29 28 24 23 5 4 0
2355 * +----+-------+-----------+-------------------+------+
2356 * | op | immlo | 1 0 0 0 0 | immhi | Rd |
2357 * +----+-------+-----------+-------------------+------+
2358 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00002359static void disas_pc_rel_adr(DisasContext *s, uint32_t insn)
2360{
Alexander Graf15bfe8b2013-12-17 19:42:34 +00002361 unsigned int page, rd;
2362 uint64_t base;
2363 int64_t offset;
2364
2365 page = extract32(insn, 31, 1);
2366 /* SignExtend(immhi:immlo) -> offset */
2367 offset = ((int64_t)sextract32(insn, 5, 19) << 2) | extract32(insn, 29, 2);
2368 rd = extract32(insn, 0, 5);
2369 base = s->pc - 4;
2370
2371 if (page) {
2372 /* ADRP (page based) */
2373 base &= ~0xfff;
2374 offset <<= 12;
2375 }
2376
2377 tcg_gen_movi_i64(cpu_reg(s, rd), base + offset);
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00002378}
2379
Alex Bennéeb0ff21b2013-12-23 23:27:29 +00002380/*
2381 * C3.4.1 Add/subtract (immediate)
2382 *
2383 * 31 30 29 28 24 23 22 21 10 9 5 4 0
2384 * +--+--+--+-----------+-----+-------------+-----+-----+
2385 * |sf|op| S| 1 0 0 0 1 |shift| imm12 | Rn | Rd |
2386 * +--+--+--+-----------+-----+-------------+-----+-----+
2387 *
2388 * sf: 0 -> 32bit, 1 -> 64bit
2389 * op: 0 -> add , 1 -> sub
2390 * S: 1 -> set flags
2391 * shift: 00 -> LSL imm by 0, 01 -> LSL imm by 12
2392 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00002393static void disas_add_sub_imm(DisasContext *s, uint32_t insn)
2394{
Alex Bennéeb0ff21b2013-12-23 23:27:29 +00002395 int rd = extract32(insn, 0, 5);
2396 int rn = extract32(insn, 5, 5);
2397 uint64_t imm = extract32(insn, 10, 12);
2398 int shift = extract32(insn, 22, 2);
2399 bool setflags = extract32(insn, 29, 1);
2400 bool sub_op = extract32(insn, 30, 1);
2401 bool is_64bit = extract32(insn, 31, 1);
2402
2403 TCGv_i64 tcg_rn = cpu_reg_sp(s, rn);
2404 TCGv_i64 tcg_rd = setflags ? cpu_reg(s, rd) : cpu_reg_sp(s, rd);
2405 TCGv_i64 tcg_result;
2406
2407 switch (shift) {
2408 case 0x0:
2409 break;
2410 case 0x1:
2411 imm <<= 12;
2412 break;
2413 default:
2414 unallocated_encoding(s);
2415 return;
2416 }
2417
2418 tcg_result = tcg_temp_new_i64();
2419 if (!setflags) {
2420 if (sub_op) {
2421 tcg_gen_subi_i64(tcg_result, tcg_rn, imm);
2422 } else {
2423 tcg_gen_addi_i64(tcg_result, tcg_rn, imm);
2424 }
2425 } else {
2426 TCGv_i64 tcg_imm = tcg_const_i64(imm);
2427 if (sub_op) {
2428 gen_sub_CC(is_64bit, tcg_result, tcg_rn, tcg_imm);
2429 } else {
2430 gen_add_CC(is_64bit, tcg_result, tcg_rn, tcg_imm);
2431 }
2432 tcg_temp_free_i64(tcg_imm);
2433 }
2434
2435 if (is_64bit) {
2436 tcg_gen_mov_i64(tcg_rd, tcg_result);
2437 } else {
2438 tcg_gen_ext32u_i64(tcg_rd, tcg_result);
2439 }
2440
2441 tcg_temp_free_i64(tcg_result);
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00002442}
2443
Alexander Graf71b46082013-12-17 19:42:36 +00002444/* The input should be a value in the bottom e bits (with higher
2445 * bits zero); returns that value replicated into every element
2446 * of size e in a 64 bit integer.
2447 */
2448static uint64_t bitfield_replicate(uint64_t mask, unsigned int e)
2449{
2450 assert(e != 0);
2451 while (e < 64) {
2452 mask |= mask << e;
2453 e *= 2;
2454 }
2455 return mask;
2456}
2457
2458/* Return a value with the bottom len bits set (where 0 < len <= 64) */
2459static inline uint64_t bitmask64(unsigned int length)
2460{
2461 assert(length > 0 && length <= 64);
2462 return ~0ULL >> (64 - length);
2463}
2464
2465/* Simplified variant of pseudocode DecodeBitMasks() for the case where we
2466 * only require the wmask. Returns false if the imms/immr/immn are a reserved
2467 * value (ie should cause a guest UNDEF exception), and true if they are
2468 * valid, in which case the decoded bit pattern is written to result.
2469 */
2470static bool logic_imm_decode_wmask(uint64_t *result, unsigned int immn,
2471 unsigned int imms, unsigned int immr)
2472{
2473 uint64_t mask;
2474 unsigned e, levels, s, r;
2475 int len;
2476
2477 assert(immn < 2 && imms < 64 && immr < 64);
2478
2479 /* The bit patterns we create here are 64 bit patterns which
2480 * are vectors of identical elements of size e = 2, 4, 8, 16, 32 or
2481 * 64 bits each. Each element contains the same value: a run
2482 * of between 1 and e-1 non-zero bits, rotated within the
2483 * element by between 0 and e-1 bits.
2484 *
2485 * The element size and run length are encoded into immn (1 bit)
2486 * and imms (6 bits) as follows:
2487 * 64 bit elements: immn = 1, imms = <length of run - 1>
2488 * 32 bit elements: immn = 0, imms = 0 : <length of run - 1>
2489 * 16 bit elements: immn = 0, imms = 10 : <length of run - 1>
2490 * 8 bit elements: immn = 0, imms = 110 : <length of run - 1>
2491 * 4 bit elements: immn = 0, imms = 1110 : <length of run - 1>
2492 * 2 bit elements: immn = 0, imms = 11110 : <length of run - 1>
2493 * Notice that immn = 0, imms = 11111x is the only combination
2494 * not covered by one of the above options; this is reserved.
2495 * Further, <length of run - 1> all-ones is a reserved pattern.
2496 *
2497 * In all cases the rotation is by immr % e (and immr is 6 bits).
2498 */
2499
2500 /* First determine the element size */
2501 len = 31 - clz32((immn << 6) | (~imms & 0x3f));
2502 if (len < 1) {
2503 /* This is the immn == 0, imms == 0x11111x case */
2504 return false;
2505 }
2506 e = 1 << len;
2507
2508 levels = e - 1;
2509 s = imms & levels;
2510 r = immr & levels;
2511
2512 if (s == levels) {
2513 /* <length of run - 1> mustn't be all-ones. */
2514 return false;
2515 }
2516
2517 /* Create the value of one element: s+1 set bits rotated
2518 * by r within the element (which is e bits wide)...
2519 */
2520 mask = bitmask64(s + 1);
2521 mask = (mask >> r) | (mask << (e - r));
2522 /* ...then replicate the element over the whole 64 bit value */
2523 mask = bitfield_replicate(mask, e);
2524 *result = mask;
2525 return true;
2526}
2527
2528/* C3.4.4 Logical (immediate)
2529 * 31 30 29 28 23 22 21 16 15 10 9 5 4 0
2530 * +----+-----+-------------+---+------+------+------+------+
2531 * | sf | opc | 1 0 0 1 0 0 | N | immr | imms | Rn | Rd |
2532 * +----+-----+-------------+---+------+------+------+------+
2533 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00002534static void disas_logic_imm(DisasContext *s, uint32_t insn)
2535{
Alexander Graf71b46082013-12-17 19:42:36 +00002536 unsigned int sf, opc, is_n, immr, imms, rn, rd;
2537 TCGv_i64 tcg_rd, tcg_rn;
2538 uint64_t wmask;
2539 bool is_and = false;
2540
2541 sf = extract32(insn, 31, 1);
2542 opc = extract32(insn, 29, 2);
2543 is_n = extract32(insn, 22, 1);
2544 immr = extract32(insn, 16, 6);
2545 imms = extract32(insn, 10, 6);
2546 rn = extract32(insn, 5, 5);
2547 rd = extract32(insn, 0, 5);
2548
2549 if (!sf && is_n) {
2550 unallocated_encoding(s);
2551 return;
2552 }
2553
2554 if (opc == 0x3) { /* ANDS */
2555 tcg_rd = cpu_reg(s, rd);
2556 } else {
2557 tcg_rd = cpu_reg_sp(s, rd);
2558 }
2559 tcg_rn = cpu_reg(s, rn);
2560
2561 if (!logic_imm_decode_wmask(&wmask, is_n, imms, immr)) {
2562 /* some immediate field values are reserved */
2563 unallocated_encoding(s);
2564 return;
2565 }
2566
2567 if (!sf) {
2568 wmask &= 0xffffffff;
2569 }
2570
2571 switch (opc) {
2572 case 0x3: /* ANDS */
2573 case 0x0: /* AND */
2574 tcg_gen_andi_i64(tcg_rd, tcg_rn, wmask);
2575 is_and = true;
2576 break;
2577 case 0x1: /* ORR */
2578 tcg_gen_ori_i64(tcg_rd, tcg_rn, wmask);
2579 break;
2580 case 0x2: /* EOR */
2581 tcg_gen_xori_i64(tcg_rd, tcg_rn, wmask);
2582 break;
2583 default:
2584 assert(FALSE); /* must handle all above */
2585 break;
2586 }
2587
2588 if (!sf && !is_and) {
2589 /* zero extend final result; we know we can skip this for AND
2590 * since the immediate had the high 32 bits clear.
2591 */
2592 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
2593 }
2594
2595 if (opc == 3) { /* ANDS */
2596 gen_logic_CC(sf, tcg_rd);
2597 }
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00002598}
2599
Alex Bennéeed6ec672013-12-23 23:27:29 +00002600/*
2601 * C3.4.5 Move wide (immediate)
2602 *
2603 * 31 30 29 28 23 22 21 20 5 4 0
2604 * +--+-----+-------------+-----+----------------+------+
2605 * |sf| opc | 1 0 0 1 0 1 | hw | imm16 | Rd |
2606 * +--+-----+-------------+-----+----------------+------+
2607 *
2608 * sf: 0 -> 32 bit, 1 -> 64 bit
2609 * opc: 00 -> N, 10 -> Z, 11 -> K
2610 * hw: shift/16 (0,16, and sf only 32, 48)
2611 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00002612static void disas_movw_imm(DisasContext *s, uint32_t insn)
2613{
Alex Bennéeed6ec672013-12-23 23:27:29 +00002614 int rd = extract32(insn, 0, 5);
2615 uint64_t imm = extract32(insn, 5, 16);
2616 int sf = extract32(insn, 31, 1);
2617 int opc = extract32(insn, 29, 2);
2618 int pos = extract32(insn, 21, 2) << 4;
2619 TCGv_i64 tcg_rd = cpu_reg(s, rd);
2620 TCGv_i64 tcg_imm;
2621
2622 if (!sf && (pos >= 32)) {
2623 unallocated_encoding(s);
2624 return;
2625 }
2626
2627 switch (opc) {
2628 case 0: /* MOVN */
2629 case 2: /* MOVZ */
2630 imm <<= pos;
2631 if (opc == 0) {
2632 imm = ~imm;
2633 }
2634 if (!sf) {
2635 imm &= 0xffffffffu;
2636 }
2637 tcg_gen_movi_i64(tcg_rd, imm);
2638 break;
2639 case 3: /* MOVK */
2640 tcg_imm = tcg_const_i64(imm);
2641 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_imm, pos, 16);
2642 tcg_temp_free_i64(tcg_imm);
2643 if (!sf) {
2644 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
2645 }
2646 break;
2647 default:
2648 unallocated_encoding(s);
2649 break;
2650 }
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00002651}
2652
Claudio Fontana88077742013-12-17 19:42:35 +00002653/* C3.4.2 Bitfield
2654 * 31 30 29 28 23 22 21 16 15 10 9 5 4 0
2655 * +----+-----+-------------+---+------+------+------+------+
2656 * | sf | opc | 1 0 0 1 1 0 | N | immr | imms | Rn | Rd |
2657 * +----+-----+-------------+---+------+------+------+------+
2658 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00002659static void disas_bitfield(DisasContext *s, uint32_t insn)
2660{
Claudio Fontana88077742013-12-17 19:42:35 +00002661 unsigned int sf, n, opc, ri, si, rn, rd, bitsize, pos, len;
2662 TCGv_i64 tcg_rd, tcg_tmp;
2663
2664 sf = extract32(insn, 31, 1);
2665 opc = extract32(insn, 29, 2);
2666 n = extract32(insn, 22, 1);
2667 ri = extract32(insn, 16, 6);
2668 si = extract32(insn, 10, 6);
2669 rn = extract32(insn, 5, 5);
2670 rd = extract32(insn, 0, 5);
2671 bitsize = sf ? 64 : 32;
2672
2673 if (sf != n || ri >= bitsize || si >= bitsize || opc > 2) {
2674 unallocated_encoding(s);
2675 return;
2676 }
2677
2678 tcg_rd = cpu_reg(s, rd);
2679 tcg_tmp = read_cpu_reg(s, rn, sf);
2680
2681 /* OPTME: probably worth recognizing common cases of ext{8,16,32}{u,s} */
2682
2683 if (opc != 1) { /* SBFM or UBFM */
2684 tcg_gen_movi_i64(tcg_rd, 0);
2685 }
2686
2687 /* do the bit move operation */
2688 if (si >= ri) {
2689 /* Wd<s-r:0> = Wn<s:r> */
2690 tcg_gen_shri_i64(tcg_tmp, tcg_tmp, ri);
2691 pos = 0;
2692 len = (si - ri) + 1;
2693 } else {
2694 /* Wd<32+s-r,32-r> = Wn<s:0> */
2695 pos = bitsize - ri;
2696 len = si + 1;
2697 }
2698
2699 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, pos, len);
2700
2701 if (opc == 0) { /* SBFM - sign extend the destination field */
2702 tcg_gen_shli_i64(tcg_rd, tcg_rd, 64 - (pos + len));
2703 tcg_gen_sari_i64(tcg_rd, tcg_rd, 64 - (pos + len));
2704 }
2705
2706 if (!sf) { /* zero extend final result */
2707 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
2708 }
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00002709}
2710
Alexander Grafe801de92013-12-17 19:42:34 +00002711/* C3.4.3 Extract
2712 * 31 30 29 28 23 22 21 20 16 15 10 9 5 4 0
2713 * +----+------+-------------+---+----+------+--------+------+------+
2714 * | sf | op21 | 1 0 0 1 1 1 | N | o0 | Rm | imms | Rn | Rd |
2715 * +----+------+-------------+---+----+------+--------+------+------+
2716 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00002717static void disas_extract(DisasContext *s, uint32_t insn)
2718{
Alexander Grafe801de92013-12-17 19:42:34 +00002719 unsigned int sf, n, rm, imm, rn, rd, bitsize, op21, op0;
2720
2721 sf = extract32(insn, 31, 1);
2722 n = extract32(insn, 22, 1);
2723 rm = extract32(insn, 16, 5);
2724 imm = extract32(insn, 10, 6);
2725 rn = extract32(insn, 5, 5);
2726 rd = extract32(insn, 0, 5);
2727 op21 = extract32(insn, 29, 2);
2728 op0 = extract32(insn, 21, 1);
2729 bitsize = sf ? 64 : 32;
2730
2731 if (sf != n || op21 || op0 || imm >= bitsize) {
2732 unallocated_encoding(s);
2733 } else {
2734 TCGv_i64 tcg_rd, tcg_rm, tcg_rn;
2735
2736 tcg_rd = cpu_reg(s, rd);
2737
2738 if (imm) {
2739 /* OPTME: we can special case rm==rn as a rotate */
2740 tcg_rm = read_cpu_reg(s, rm, sf);
2741 tcg_rn = read_cpu_reg(s, rn, sf);
2742 tcg_gen_shri_i64(tcg_rm, tcg_rm, imm);
2743 tcg_gen_shli_i64(tcg_rn, tcg_rn, bitsize - imm);
2744 tcg_gen_or_i64(tcg_rd, tcg_rm, tcg_rn);
2745 if (!sf) {
2746 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
2747 }
2748 } else {
2749 /* tcg shl_i32/shl_i64 is undefined for 32/64 bit shifts,
2750 * so an extract from bit 0 is a special case.
2751 */
2752 if (sf) {
2753 tcg_gen_mov_i64(tcg_rd, cpu_reg(s, rm));
2754 } else {
2755 tcg_gen_ext32u_i64(tcg_rd, cpu_reg(s, rm));
2756 }
2757 }
2758
2759 }
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00002760}
2761
2762/* C3.4 Data processing - immediate */
2763static void disas_data_proc_imm(DisasContext *s, uint32_t insn)
2764{
2765 switch (extract32(insn, 23, 6)) {
2766 case 0x20: case 0x21: /* PC-rel. addressing */
2767 disas_pc_rel_adr(s, insn);
2768 break;
2769 case 0x22: case 0x23: /* Add/subtract (immediate) */
2770 disas_add_sub_imm(s, insn);
2771 break;
2772 case 0x24: /* Logical (immediate) */
2773 disas_logic_imm(s, insn);
2774 break;
2775 case 0x25: /* Move wide (immediate) */
2776 disas_movw_imm(s, insn);
2777 break;
2778 case 0x26: /* Bitfield */
2779 disas_bitfield(s, insn);
2780 break;
2781 case 0x27: /* Extract */
2782 disas_extract(s, insn);
2783 break;
2784 default:
2785 unallocated_encoding(s);
2786 break;
2787 }
2788}
2789
Alexander Graf832ffa12013-12-17 19:42:34 +00002790/* Shift a TCGv src by TCGv shift_amount, put result in dst.
2791 * Note that it is the caller's responsibility to ensure that the
2792 * shift amount is in range (ie 0..31 or 0..63) and provide the ARM
2793 * mandated semantics for out of range shifts.
2794 */
2795static void shift_reg(TCGv_i64 dst, TCGv_i64 src, int sf,
2796 enum a64_shift_type shift_type, TCGv_i64 shift_amount)
2797{
2798 switch (shift_type) {
2799 case A64_SHIFT_TYPE_LSL:
2800 tcg_gen_shl_i64(dst, src, shift_amount);
2801 break;
2802 case A64_SHIFT_TYPE_LSR:
2803 tcg_gen_shr_i64(dst, src, shift_amount);
2804 break;
2805 case A64_SHIFT_TYPE_ASR:
2806 if (!sf) {
2807 tcg_gen_ext32s_i64(dst, src);
2808 }
2809 tcg_gen_sar_i64(dst, sf ? src : dst, shift_amount);
2810 break;
2811 case A64_SHIFT_TYPE_ROR:
2812 if (sf) {
2813 tcg_gen_rotr_i64(dst, src, shift_amount);
2814 } else {
2815 TCGv_i32 t0, t1;
2816 t0 = tcg_temp_new_i32();
2817 t1 = tcg_temp_new_i32();
2818 tcg_gen_trunc_i64_i32(t0, src);
2819 tcg_gen_trunc_i64_i32(t1, shift_amount);
2820 tcg_gen_rotr_i32(t0, t0, t1);
2821 tcg_gen_extu_i32_i64(dst, t0);
2822 tcg_temp_free_i32(t0);
2823 tcg_temp_free_i32(t1);
2824 }
2825 break;
2826 default:
2827 assert(FALSE); /* all shift types should be handled */
2828 break;
2829 }
2830
2831 if (!sf) { /* zero extend final result */
2832 tcg_gen_ext32u_i64(dst, dst);
2833 }
2834}
2835
2836/* Shift a TCGv src by immediate, put result in dst.
2837 * The shift amount must be in range (this should always be true as the
2838 * relevant instructions will UNDEF on bad shift immediates).
2839 */
2840static void shift_reg_imm(TCGv_i64 dst, TCGv_i64 src, int sf,
2841 enum a64_shift_type shift_type, unsigned int shift_i)
2842{
2843 assert(shift_i < (sf ? 64 : 32));
2844
2845 if (shift_i == 0) {
2846 tcg_gen_mov_i64(dst, src);
2847 } else {
2848 TCGv_i64 shift_const;
2849
2850 shift_const = tcg_const_i64(shift_i);
2851 shift_reg(dst, src, sf, shift_type, shift_const);
2852 tcg_temp_free_i64(shift_const);
2853 }
2854}
2855
2856/* C3.5.10 Logical (shifted register)
2857 * 31 30 29 28 24 23 22 21 20 16 15 10 9 5 4 0
2858 * +----+-----+-----------+-------+---+------+--------+------+------+
2859 * | sf | opc | 0 1 0 1 0 | shift | N | Rm | imm6 | Rn | Rd |
2860 * +----+-----+-----------+-------+---+------+--------+------+------+
2861 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00002862static void disas_logic_reg(DisasContext *s, uint32_t insn)
2863{
Alexander Graf832ffa12013-12-17 19:42:34 +00002864 TCGv_i64 tcg_rd, tcg_rn, tcg_rm;
2865 unsigned int sf, opc, shift_type, invert, rm, shift_amount, rn, rd;
2866
2867 sf = extract32(insn, 31, 1);
2868 opc = extract32(insn, 29, 2);
2869 shift_type = extract32(insn, 22, 2);
2870 invert = extract32(insn, 21, 1);
2871 rm = extract32(insn, 16, 5);
2872 shift_amount = extract32(insn, 10, 6);
2873 rn = extract32(insn, 5, 5);
2874 rd = extract32(insn, 0, 5);
2875
2876 if (!sf && (shift_amount & (1 << 5))) {
2877 unallocated_encoding(s);
2878 return;
2879 }
2880
2881 tcg_rd = cpu_reg(s, rd);
2882
2883 if (opc == 1 && shift_amount == 0 && shift_type == 0 && rn == 31) {
2884 /* Unshifted ORR and ORN with WZR/XZR is the standard encoding for
2885 * register-register MOV and MVN, so it is worth special casing.
2886 */
2887 tcg_rm = cpu_reg(s, rm);
2888 if (invert) {
2889 tcg_gen_not_i64(tcg_rd, tcg_rm);
2890 if (!sf) {
2891 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
2892 }
2893 } else {
2894 if (sf) {
2895 tcg_gen_mov_i64(tcg_rd, tcg_rm);
2896 } else {
2897 tcg_gen_ext32u_i64(tcg_rd, tcg_rm);
2898 }
2899 }
2900 return;
2901 }
2902
2903 tcg_rm = read_cpu_reg(s, rm, sf);
2904
2905 if (shift_amount) {
2906 shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, shift_amount);
2907 }
2908
2909 tcg_rn = cpu_reg(s, rn);
2910
2911 switch (opc | (invert << 2)) {
2912 case 0: /* AND */
2913 case 3: /* ANDS */
2914 tcg_gen_and_i64(tcg_rd, tcg_rn, tcg_rm);
2915 break;
2916 case 1: /* ORR */
2917 tcg_gen_or_i64(tcg_rd, tcg_rn, tcg_rm);
2918 break;
2919 case 2: /* EOR */
2920 tcg_gen_xor_i64(tcg_rd, tcg_rn, tcg_rm);
2921 break;
2922 case 4: /* BIC */
2923 case 7: /* BICS */
2924 tcg_gen_andc_i64(tcg_rd, tcg_rn, tcg_rm);
2925 break;
2926 case 5: /* ORN */
2927 tcg_gen_orc_i64(tcg_rd, tcg_rn, tcg_rm);
2928 break;
2929 case 6: /* EON */
2930 tcg_gen_eqv_i64(tcg_rd, tcg_rn, tcg_rm);
2931 break;
2932 default:
2933 assert(FALSE);
2934 break;
2935 }
2936
2937 if (!sf) {
2938 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
2939 }
2940
2941 if (opc == 3) {
2942 gen_logic_CC(sf, tcg_rd);
2943 }
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00002944}
2945
Alex Bennéeb0ff21b2013-12-23 23:27:29 +00002946/*
2947 * C3.5.1 Add/subtract (extended register)
2948 *
2949 * 31|30|29|28 24|23 22|21|20 16|15 13|12 10|9 5|4 0|
2950 * +--+--+--+-----------+-----+--+-------+------+------+----+----+
2951 * |sf|op| S| 0 1 0 1 1 | opt | 1| Rm |option| imm3 | Rn | Rd |
2952 * +--+--+--+-----------+-----+--+-------+------+------+----+----+
2953 *
2954 * sf: 0 -> 32bit, 1 -> 64bit
2955 * op: 0 -> add , 1 -> sub
2956 * S: 1 -> set flags
2957 * opt: 00
2958 * option: extension type (see DecodeRegExtend)
2959 * imm3: optional shift to Rm
2960 *
2961 * Rd = Rn + LSL(extend(Rm), amount)
2962 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00002963static void disas_add_sub_ext_reg(DisasContext *s, uint32_t insn)
2964{
Alex Bennéeb0ff21b2013-12-23 23:27:29 +00002965 int rd = extract32(insn, 0, 5);
2966 int rn = extract32(insn, 5, 5);
2967 int imm3 = extract32(insn, 10, 3);
2968 int option = extract32(insn, 13, 3);
2969 int rm = extract32(insn, 16, 5);
2970 bool setflags = extract32(insn, 29, 1);
2971 bool sub_op = extract32(insn, 30, 1);
2972 bool sf = extract32(insn, 31, 1);
2973
2974 TCGv_i64 tcg_rm, tcg_rn; /* temps */
2975 TCGv_i64 tcg_rd;
2976 TCGv_i64 tcg_result;
2977
2978 if (imm3 > 4) {
2979 unallocated_encoding(s);
2980 return;
2981 }
2982
2983 /* non-flag setting ops may use SP */
2984 if (!setflags) {
2985 tcg_rn = read_cpu_reg_sp(s, rn, sf);
2986 tcg_rd = cpu_reg_sp(s, rd);
2987 } else {
2988 tcg_rn = read_cpu_reg(s, rn, sf);
2989 tcg_rd = cpu_reg(s, rd);
2990 }
2991
2992 tcg_rm = read_cpu_reg(s, rm, sf);
2993 ext_and_shift_reg(tcg_rm, tcg_rm, option, imm3);
2994
2995 tcg_result = tcg_temp_new_i64();
2996
2997 if (!setflags) {
2998 if (sub_op) {
2999 tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
3000 } else {
3001 tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
3002 }
3003 } else {
3004 if (sub_op) {
3005 gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
3006 } else {
3007 gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
3008 }
3009 }
3010
3011 if (sf) {
3012 tcg_gen_mov_i64(tcg_rd, tcg_result);
3013 } else {
3014 tcg_gen_ext32u_i64(tcg_rd, tcg_result);
3015 }
3016
3017 tcg_temp_free_i64(tcg_result);
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00003018}
3019
Alex Bennéeb0ff21b2013-12-23 23:27:29 +00003020/*
3021 * C3.5.2 Add/subtract (shifted register)
3022 *
3023 * 31 30 29 28 24 23 22 21 20 16 15 10 9 5 4 0
3024 * +--+--+--+-----------+-----+--+-------+---------+------+------+
3025 * |sf|op| S| 0 1 0 1 1 |shift| 0| Rm | imm6 | Rn | Rd |
3026 * +--+--+--+-----------+-----+--+-------+---------+------+------+
3027 *
3028 * sf: 0 -> 32bit, 1 -> 64bit
3029 * op: 0 -> add , 1 -> sub
3030 * S: 1 -> set flags
3031 * shift: 00 -> LSL, 01 -> LSR, 10 -> ASR, 11 -> RESERVED
3032 * imm6: Shift amount to apply to Rm before the add/sub
3033 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00003034static void disas_add_sub_reg(DisasContext *s, uint32_t insn)
3035{
Alex Bennéeb0ff21b2013-12-23 23:27:29 +00003036 int rd = extract32(insn, 0, 5);
3037 int rn = extract32(insn, 5, 5);
3038 int imm6 = extract32(insn, 10, 6);
3039 int rm = extract32(insn, 16, 5);
3040 int shift_type = extract32(insn, 22, 2);
3041 bool setflags = extract32(insn, 29, 1);
3042 bool sub_op = extract32(insn, 30, 1);
3043 bool sf = extract32(insn, 31, 1);
3044
3045 TCGv_i64 tcg_rd = cpu_reg(s, rd);
3046 TCGv_i64 tcg_rn, tcg_rm;
3047 TCGv_i64 tcg_result;
3048
3049 if ((shift_type == 3) || (!sf && (imm6 > 31))) {
3050 unallocated_encoding(s);
3051 return;
3052 }
3053
3054 tcg_rn = read_cpu_reg(s, rn, sf);
3055 tcg_rm = read_cpu_reg(s, rm, sf);
3056
3057 shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, imm6);
3058
3059 tcg_result = tcg_temp_new_i64();
3060
3061 if (!setflags) {
3062 if (sub_op) {
3063 tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
3064 } else {
3065 tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
3066 }
3067 } else {
3068 if (sub_op) {
3069 gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
3070 } else {
3071 gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
3072 }
3073 }
3074
3075 if (sf) {
3076 tcg_gen_mov_i64(tcg_rd, tcg_result);
3077 } else {
3078 tcg_gen_ext32u_i64(tcg_rd, tcg_result);
3079 }
3080
3081 tcg_temp_free_i64(tcg_result);
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00003082}
3083
Alexander Graf52c8b9a2013-12-23 23:27:30 +00003084/* C3.5.9 Data-processing (3 source)
3085
3086 31 30 29 28 24 23 21 20 16 15 14 10 9 5 4 0
3087 +--+------+-----------+------+------+----+------+------+------+
3088 |sf| op54 | 1 1 0 1 1 | op31 | Rm | o0 | Ra | Rn | Rd |
3089 +--+------+-----------+------+------+----+------+------+------+
3090
3091 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00003092static void disas_data_proc_3src(DisasContext *s, uint32_t insn)
3093{
Alexander Graf52c8b9a2013-12-23 23:27:30 +00003094 int rd = extract32(insn, 0, 5);
3095 int rn = extract32(insn, 5, 5);
3096 int ra = extract32(insn, 10, 5);
3097 int rm = extract32(insn, 16, 5);
3098 int op_id = (extract32(insn, 29, 3) << 4) |
3099 (extract32(insn, 21, 3) << 1) |
3100 extract32(insn, 15, 1);
3101 bool sf = extract32(insn, 31, 1);
3102 bool is_sub = extract32(op_id, 0, 1);
3103 bool is_high = extract32(op_id, 2, 1);
3104 bool is_signed = false;
3105 TCGv_i64 tcg_op1;
3106 TCGv_i64 tcg_op2;
3107 TCGv_i64 tcg_tmp;
3108
3109 /* Note that op_id is sf:op54:op31:o0 so it includes the 32/64 size flag */
3110 switch (op_id) {
3111 case 0x42: /* SMADDL */
3112 case 0x43: /* SMSUBL */
3113 case 0x44: /* SMULH */
3114 is_signed = true;
3115 break;
3116 case 0x0: /* MADD (32bit) */
3117 case 0x1: /* MSUB (32bit) */
3118 case 0x40: /* MADD (64bit) */
3119 case 0x41: /* MSUB (64bit) */
3120 case 0x4a: /* UMADDL */
3121 case 0x4b: /* UMSUBL */
3122 case 0x4c: /* UMULH */
3123 break;
3124 default:
3125 unallocated_encoding(s);
3126 return;
3127 }
3128
3129 if (is_high) {
3130 TCGv_i64 low_bits = tcg_temp_new_i64(); /* low bits discarded */
3131 TCGv_i64 tcg_rd = cpu_reg(s, rd);
3132 TCGv_i64 tcg_rn = cpu_reg(s, rn);
3133 TCGv_i64 tcg_rm = cpu_reg(s, rm);
3134
3135 if (is_signed) {
3136 tcg_gen_muls2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
3137 } else {
3138 tcg_gen_mulu2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
3139 }
3140
3141 tcg_temp_free_i64(low_bits);
3142 return;
3143 }
3144
3145 tcg_op1 = tcg_temp_new_i64();
3146 tcg_op2 = tcg_temp_new_i64();
3147 tcg_tmp = tcg_temp_new_i64();
3148
3149 if (op_id < 0x42) {
3150 tcg_gen_mov_i64(tcg_op1, cpu_reg(s, rn));
3151 tcg_gen_mov_i64(tcg_op2, cpu_reg(s, rm));
3152 } else {
3153 if (is_signed) {
3154 tcg_gen_ext32s_i64(tcg_op1, cpu_reg(s, rn));
3155 tcg_gen_ext32s_i64(tcg_op2, cpu_reg(s, rm));
3156 } else {
3157 tcg_gen_ext32u_i64(tcg_op1, cpu_reg(s, rn));
3158 tcg_gen_ext32u_i64(tcg_op2, cpu_reg(s, rm));
3159 }
3160 }
3161
3162 if (ra == 31 && !is_sub) {
3163 /* Special-case MADD with rA == XZR; it is the standard MUL alias */
3164 tcg_gen_mul_i64(cpu_reg(s, rd), tcg_op1, tcg_op2);
3165 } else {
3166 tcg_gen_mul_i64(tcg_tmp, tcg_op1, tcg_op2);
3167 if (is_sub) {
3168 tcg_gen_sub_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
3169 } else {
3170 tcg_gen_add_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
3171 }
3172 }
3173
3174 if (!sf) {
3175 tcg_gen_ext32u_i64(cpu_reg(s, rd), cpu_reg(s, rd));
3176 }
3177
3178 tcg_temp_free_i64(tcg_op1);
3179 tcg_temp_free_i64(tcg_op2);
3180 tcg_temp_free_i64(tcg_tmp);
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00003181}
3182
Claudio Fontana643dbb02014-01-04 22:15:46 +00003183/* C3.5.3 - Add/subtract (with carry)
3184 * 31 30 29 28 27 26 25 24 23 22 21 20 16 15 10 9 5 4 0
3185 * +--+--+--+------------------------+------+---------+------+-----+
3186 * |sf|op| S| 1 1 0 1 0 0 0 0 | rm | opcode2 | Rn | Rd |
3187 * +--+--+--+------------------------+------+---------+------+-----+
3188 * [000000]
3189 */
3190
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00003191static void disas_adc_sbc(DisasContext *s, uint32_t insn)
3192{
Claudio Fontana643dbb02014-01-04 22:15:46 +00003193 unsigned int sf, op, setflags, rm, rn, rd;
3194 TCGv_i64 tcg_y, tcg_rn, tcg_rd;
3195
3196 if (extract32(insn, 10, 6) != 0) {
3197 unallocated_encoding(s);
3198 return;
3199 }
3200
3201 sf = extract32(insn, 31, 1);
3202 op = extract32(insn, 30, 1);
3203 setflags = extract32(insn, 29, 1);
3204 rm = extract32(insn, 16, 5);
3205 rn = extract32(insn, 5, 5);
3206 rd = extract32(insn, 0, 5);
3207
3208 tcg_rd = cpu_reg(s, rd);
3209 tcg_rn = cpu_reg(s, rn);
3210
3211 if (op) {
3212 tcg_y = new_tmp_a64(s);
3213 tcg_gen_not_i64(tcg_y, cpu_reg(s, rm));
3214 } else {
3215 tcg_y = cpu_reg(s, rm);
3216 }
3217
3218 if (setflags) {
3219 gen_adc_CC(sf, tcg_rd, tcg_rn, tcg_y);
3220 } else {
3221 gen_adc(sf, tcg_rd, tcg_rn, tcg_y);
3222 }
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00003223}
3224
Claudio Fontana750813c2014-01-04 22:15:46 +00003225/* C3.5.4 - C3.5.5 Conditional compare (immediate / register)
3226 * 31 30 29 28 27 26 25 24 23 22 21 20 16 15 12 11 10 9 5 4 3 0
3227 * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
3228 * |sf|op| S| 1 1 0 1 0 0 1 0 |imm5/rm | cond |i/r |o2| Rn |o3|nzcv |
3229 * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
3230 * [1] y [0] [0]
3231 */
3232static void disas_cc(DisasContext *s, uint32_t insn)
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00003233{
Claudio Fontana750813c2014-01-04 22:15:46 +00003234 unsigned int sf, op, y, cond, rn, nzcv, is_imm;
3235 int label_continue = -1;
3236 TCGv_i64 tcg_tmp, tcg_y, tcg_rn;
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00003237
Claudio Fontana750813c2014-01-04 22:15:46 +00003238 if (!extract32(insn, 29, 1)) {
3239 unallocated_encoding(s);
3240 return;
3241 }
3242 if (insn & (1 << 10 | 1 << 4)) {
3243 unallocated_encoding(s);
3244 return;
3245 }
3246 sf = extract32(insn, 31, 1);
3247 op = extract32(insn, 30, 1);
3248 is_imm = extract32(insn, 11, 1);
3249 y = extract32(insn, 16, 5); /* y = rm (reg) or imm5 (imm) */
3250 cond = extract32(insn, 12, 4);
3251 rn = extract32(insn, 5, 5);
3252 nzcv = extract32(insn, 0, 4);
3253
3254 if (cond < 0x0e) { /* not always */
3255 int label_match = gen_new_label();
3256 label_continue = gen_new_label();
3257 arm_gen_test_cc(cond, label_match);
3258 /* nomatch: */
3259 tcg_tmp = tcg_temp_new_i64();
3260 tcg_gen_movi_i64(tcg_tmp, nzcv << 28);
3261 gen_set_nzcv(tcg_tmp);
3262 tcg_temp_free_i64(tcg_tmp);
3263 tcg_gen_br(label_continue);
3264 gen_set_label(label_match);
3265 }
3266 /* match, or condition is always */
3267 if (is_imm) {
3268 tcg_y = new_tmp_a64(s);
3269 tcg_gen_movi_i64(tcg_y, y);
3270 } else {
3271 tcg_y = cpu_reg(s, y);
3272 }
3273 tcg_rn = cpu_reg(s, rn);
3274
3275 tcg_tmp = tcg_temp_new_i64();
3276 if (op) {
3277 gen_sub_CC(sf, tcg_tmp, tcg_rn, tcg_y);
3278 } else {
3279 gen_add_CC(sf, tcg_tmp, tcg_rn, tcg_y);
3280 }
3281 tcg_temp_free_i64(tcg_tmp);
3282
3283 if (cond < 0x0e) { /* continue */
3284 gen_set_label(label_continue);
3285 }
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00003286}
3287
Claudio Fontanae952d8c2013-12-17 19:42:33 +00003288/* C3.5.6 Conditional select
3289 * 31 30 29 28 21 20 16 15 12 11 10 9 5 4 0
3290 * +----+----+---+-----------------+------+------+-----+------+------+
3291 * | sf | op | S | 1 1 0 1 0 1 0 0 | Rm | cond | op2 | Rn | Rd |
3292 * +----+----+---+-----------------+------+------+-----+------+------+
3293 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00003294static void disas_cond_select(DisasContext *s, uint32_t insn)
3295{
Claudio Fontanae952d8c2013-12-17 19:42:33 +00003296 unsigned int sf, else_inv, rm, cond, else_inc, rn, rd;
3297 TCGv_i64 tcg_rd, tcg_src;
3298
3299 if (extract32(insn, 29, 1) || extract32(insn, 11, 1)) {
3300 /* S == 1 or op2<1> == 1 */
3301 unallocated_encoding(s);
3302 return;
3303 }
3304 sf = extract32(insn, 31, 1);
3305 else_inv = extract32(insn, 30, 1);
3306 rm = extract32(insn, 16, 5);
3307 cond = extract32(insn, 12, 4);
3308 else_inc = extract32(insn, 10, 1);
3309 rn = extract32(insn, 5, 5);
3310 rd = extract32(insn, 0, 5);
3311
3312 if (rd == 31) {
3313 /* silly no-op write; until we use movcond we must special-case
3314 * this to avoid a dead temporary across basic blocks.
3315 */
3316 return;
3317 }
3318
3319 tcg_rd = cpu_reg(s, rd);
3320
3321 if (cond >= 0x0e) { /* condition "always" */
3322 tcg_src = read_cpu_reg(s, rn, sf);
3323 tcg_gen_mov_i64(tcg_rd, tcg_src);
3324 } else {
3325 /* OPTME: we could use movcond here, at the cost of duplicating
3326 * a lot of the arm_gen_test_cc() logic.
3327 */
3328 int label_match = gen_new_label();
3329 int label_continue = gen_new_label();
3330
3331 arm_gen_test_cc(cond, label_match);
3332 /* nomatch: */
3333 tcg_src = cpu_reg(s, rm);
3334
3335 if (else_inv && else_inc) {
3336 tcg_gen_neg_i64(tcg_rd, tcg_src);
3337 } else if (else_inv) {
3338 tcg_gen_not_i64(tcg_rd, tcg_src);
3339 } else if (else_inc) {
3340 tcg_gen_addi_i64(tcg_rd, tcg_src, 1);
3341 } else {
3342 tcg_gen_mov_i64(tcg_rd, tcg_src);
3343 }
3344 if (!sf) {
3345 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3346 }
3347 tcg_gen_br(label_continue);
3348 /* match: */
3349 gen_set_label(label_match);
3350 tcg_src = read_cpu_reg(s, rn, sf);
3351 tcg_gen_mov_i64(tcg_rd, tcg_src);
3352 /* continue: */
3353 gen_set_label(label_continue);
3354 }
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00003355}
3356
Claudio Fontana680ead22013-12-17 19:42:35 +00003357static void handle_clz(DisasContext *s, unsigned int sf,
3358 unsigned int rn, unsigned int rd)
3359{
3360 TCGv_i64 tcg_rd, tcg_rn;
3361 tcg_rd = cpu_reg(s, rd);
3362 tcg_rn = cpu_reg(s, rn);
3363
3364 if (sf) {
3365 gen_helper_clz64(tcg_rd, tcg_rn);
3366 } else {
3367 TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
3368 tcg_gen_trunc_i64_i32(tcg_tmp32, tcg_rn);
3369 gen_helper_clz(tcg_tmp32, tcg_tmp32);
3370 tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
3371 tcg_temp_free_i32(tcg_tmp32);
3372 }
3373}
3374
Claudio Fontanae80c5022013-12-17 19:42:35 +00003375static void handle_cls(DisasContext *s, unsigned int sf,
3376 unsigned int rn, unsigned int rd)
3377{
3378 TCGv_i64 tcg_rd, tcg_rn;
3379 tcg_rd = cpu_reg(s, rd);
3380 tcg_rn = cpu_reg(s, rn);
3381
3382 if (sf) {
3383 gen_helper_cls64(tcg_rd, tcg_rn);
3384 } else {
3385 TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
3386 tcg_gen_trunc_i64_i32(tcg_tmp32, tcg_rn);
3387 gen_helper_cls32(tcg_tmp32, tcg_tmp32);
3388 tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
3389 tcg_temp_free_i32(tcg_tmp32);
3390 }
3391}
3392
Alexander Graf82e14b02013-12-17 19:42:35 +00003393static void handle_rbit(DisasContext *s, unsigned int sf,
3394 unsigned int rn, unsigned int rd)
3395{
3396 TCGv_i64 tcg_rd, tcg_rn;
3397 tcg_rd = cpu_reg(s, rd);
3398 tcg_rn = cpu_reg(s, rn);
3399
3400 if (sf) {
3401 gen_helper_rbit64(tcg_rd, tcg_rn);
3402 } else {
3403 TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
3404 tcg_gen_trunc_i64_i32(tcg_tmp32, tcg_rn);
3405 gen_helper_rbit(tcg_tmp32, tcg_tmp32);
3406 tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
3407 tcg_temp_free_i32(tcg_tmp32);
3408 }
3409}
3410
Claudio Fontana45323202013-12-17 19:42:35 +00003411/* C5.6.149 REV with sf==1, opcode==3 ("REV64") */
3412static void handle_rev64(DisasContext *s, unsigned int sf,
3413 unsigned int rn, unsigned int rd)
3414{
3415 if (!sf) {
3416 unallocated_encoding(s);
3417 return;
3418 }
3419 tcg_gen_bswap64_i64(cpu_reg(s, rd), cpu_reg(s, rn));
3420}
3421
3422/* C5.6.149 REV with sf==0, opcode==2
3423 * C5.6.151 REV32 (sf==1, opcode==2)
3424 */
3425static void handle_rev32(DisasContext *s, unsigned int sf,
3426 unsigned int rn, unsigned int rd)
3427{
3428 TCGv_i64 tcg_rd = cpu_reg(s, rd);
3429
3430 if (sf) {
3431 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
3432 TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
3433
3434 /* bswap32_i64 requires zero high word */
3435 tcg_gen_ext32u_i64(tcg_tmp, tcg_rn);
3436 tcg_gen_bswap32_i64(tcg_rd, tcg_tmp);
3437 tcg_gen_shri_i64(tcg_tmp, tcg_rn, 32);
3438 tcg_gen_bswap32_i64(tcg_tmp, tcg_tmp);
3439 tcg_gen_concat32_i64(tcg_rd, tcg_rd, tcg_tmp);
3440
3441 tcg_temp_free_i64(tcg_tmp);
3442 } else {
3443 tcg_gen_ext32u_i64(tcg_rd, cpu_reg(s, rn));
3444 tcg_gen_bswap32_i64(tcg_rd, tcg_rd);
3445 }
3446}
3447
3448/* C5.6.150 REV16 (opcode==1) */
3449static void handle_rev16(DisasContext *s, unsigned int sf,
3450 unsigned int rn, unsigned int rd)
3451{
3452 TCGv_i64 tcg_rd = cpu_reg(s, rd);
3453 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
3454 TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
3455
3456 tcg_gen_andi_i64(tcg_tmp, tcg_rn, 0xffff);
3457 tcg_gen_bswap16_i64(tcg_rd, tcg_tmp);
3458
3459 tcg_gen_shri_i64(tcg_tmp, tcg_rn, 16);
3460 tcg_gen_andi_i64(tcg_tmp, tcg_tmp, 0xffff);
3461 tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp);
3462 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, 16, 16);
3463
3464 if (sf) {
3465 tcg_gen_shri_i64(tcg_tmp, tcg_rn, 32);
3466 tcg_gen_andi_i64(tcg_tmp, tcg_tmp, 0xffff);
3467 tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp);
3468 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, 32, 16);
3469
3470 tcg_gen_shri_i64(tcg_tmp, tcg_rn, 48);
3471 tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp);
3472 tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, 48, 16);
3473 }
3474
3475 tcg_temp_free_i64(tcg_tmp);
3476}
3477
Claudio Fontana680ead22013-12-17 19:42:35 +00003478/* C3.5.7 Data-processing (1 source)
3479 * 31 30 29 28 21 20 16 15 10 9 5 4 0
3480 * +----+---+---+-----------------+---------+--------+------+------+
3481 * | sf | 1 | S | 1 1 0 1 0 1 1 0 | opcode2 | opcode | Rn | Rd |
3482 * +----+---+---+-----------------+---------+--------+------+------+
3483 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00003484static void disas_data_proc_1src(DisasContext *s, uint32_t insn)
3485{
Claudio Fontana680ead22013-12-17 19:42:35 +00003486 unsigned int sf, opcode, rn, rd;
3487
3488 if (extract32(insn, 29, 1) || extract32(insn, 16, 5)) {
3489 unallocated_encoding(s);
3490 return;
3491 }
3492
3493 sf = extract32(insn, 31, 1);
3494 opcode = extract32(insn, 10, 6);
3495 rn = extract32(insn, 5, 5);
3496 rd = extract32(insn, 0, 5);
3497
3498 switch (opcode) {
3499 case 0: /* RBIT */
Alexander Graf82e14b02013-12-17 19:42:35 +00003500 handle_rbit(s, sf, rn, rd);
3501 break;
Claudio Fontana680ead22013-12-17 19:42:35 +00003502 case 1: /* REV16 */
Claudio Fontana45323202013-12-17 19:42:35 +00003503 handle_rev16(s, sf, rn, rd);
3504 break;
Claudio Fontana680ead22013-12-17 19:42:35 +00003505 case 2: /* REV32 */
Claudio Fontana45323202013-12-17 19:42:35 +00003506 handle_rev32(s, sf, rn, rd);
3507 break;
Claudio Fontana680ead22013-12-17 19:42:35 +00003508 case 3: /* REV64 */
Claudio Fontana45323202013-12-17 19:42:35 +00003509 handle_rev64(s, sf, rn, rd);
Claudio Fontana680ead22013-12-17 19:42:35 +00003510 break;
3511 case 4: /* CLZ */
3512 handle_clz(s, sf, rn, rd);
3513 break;
3514 case 5: /* CLS */
Claudio Fontanae80c5022013-12-17 19:42:35 +00003515 handle_cls(s, sf, rn, rd);
Claudio Fontana680ead22013-12-17 19:42:35 +00003516 break;
3517 }
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00003518}
3519
Alexander Graf8220e912013-12-17 19:42:34 +00003520static void handle_div(DisasContext *s, bool is_signed, unsigned int sf,
3521 unsigned int rm, unsigned int rn, unsigned int rd)
3522{
3523 TCGv_i64 tcg_n, tcg_m, tcg_rd;
3524 tcg_rd = cpu_reg(s, rd);
3525
3526 if (!sf && is_signed) {
3527 tcg_n = new_tmp_a64(s);
3528 tcg_m = new_tmp_a64(s);
3529 tcg_gen_ext32s_i64(tcg_n, cpu_reg(s, rn));
3530 tcg_gen_ext32s_i64(tcg_m, cpu_reg(s, rm));
3531 } else {
3532 tcg_n = read_cpu_reg(s, rn, sf);
3533 tcg_m = read_cpu_reg(s, rm, sf);
3534 }
3535
3536 if (is_signed) {
3537 gen_helper_sdiv64(tcg_rd, tcg_n, tcg_m);
3538 } else {
3539 gen_helper_udiv64(tcg_rd, tcg_n, tcg_m);
3540 }
3541
3542 if (!sf) { /* zero extend final result */
3543 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3544 }
3545}
3546
Alexander Graf6c1adc92013-12-17 19:42:34 +00003547/* C5.6.115 LSLV, C5.6.118 LSRV, C5.6.17 ASRV, C5.6.154 RORV */
3548static void handle_shift_reg(DisasContext *s,
3549 enum a64_shift_type shift_type, unsigned int sf,
3550 unsigned int rm, unsigned int rn, unsigned int rd)
3551{
3552 TCGv_i64 tcg_shift = tcg_temp_new_i64();
3553 TCGv_i64 tcg_rd = cpu_reg(s, rd);
3554 TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
3555
3556 tcg_gen_andi_i64(tcg_shift, cpu_reg(s, rm), sf ? 63 : 31);
3557 shift_reg(tcg_rd, tcg_rn, sf, shift_type, tcg_shift);
3558 tcg_temp_free_i64(tcg_shift);
3559}
3560
Alexander Graf8220e912013-12-17 19:42:34 +00003561/* C3.5.8 Data-processing (2 source)
3562 * 31 30 29 28 21 20 16 15 10 9 5 4 0
3563 * +----+---+---+-----------------+------+--------+------+------+
3564 * | sf | 0 | S | 1 1 0 1 0 1 1 0 | Rm | opcode | Rn | Rd |
3565 * +----+---+---+-----------------+------+--------+------+------+
3566 */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00003567static void disas_data_proc_2src(DisasContext *s, uint32_t insn)
3568{
Alexander Graf8220e912013-12-17 19:42:34 +00003569 unsigned int sf, rm, opcode, rn, rd;
3570 sf = extract32(insn, 31, 1);
3571 rm = extract32(insn, 16, 5);
3572 opcode = extract32(insn, 10, 6);
3573 rn = extract32(insn, 5, 5);
3574 rd = extract32(insn, 0, 5);
3575
3576 if (extract32(insn, 29, 1)) {
3577 unallocated_encoding(s);
3578 return;
3579 }
3580
3581 switch (opcode) {
3582 case 2: /* UDIV */
3583 handle_div(s, false, sf, rm, rn, rd);
3584 break;
3585 case 3: /* SDIV */
3586 handle_div(s, true, sf, rm, rn, rd);
3587 break;
3588 case 8: /* LSLV */
Alexander Graf6c1adc92013-12-17 19:42:34 +00003589 handle_shift_reg(s, A64_SHIFT_TYPE_LSL, sf, rm, rn, rd);
3590 break;
Alexander Graf8220e912013-12-17 19:42:34 +00003591 case 9: /* LSRV */
Alexander Graf6c1adc92013-12-17 19:42:34 +00003592 handle_shift_reg(s, A64_SHIFT_TYPE_LSR, sf, rm, rn, rd);
3593 break;
Alexander Graf8220e912013-12-17 19:42:34 +00003594 case 10: /* ASRV */
Alexander Graf6c1adc92013-12-17 19:42:34 +00003595 handle_shift_reg(s, A64_SHIFT_TYPE_ASR, sf, rm, rn, rd);
3596 break;
Alexander Graf8220e912013-12-17 19:42:34 +00003597 case 11: /* RORV */
Alexander Graf6c1adc92013-12-17 19:42:34 +00003598 handle_shift_reg(s, A64_SHIFT_TYPE_ROR, sf, rm, rn, rd);
3599 break;
Alexander Graf8220e912013-12-17 19:42:34 +00003600 case 16:
3601 case 17:
3602 case 18:
3603 case 19:
3604 case 20:
3605 case 21:
3606 case 22:
3607 case 23: /* CRC32 */
3608 unsupported_encoding(s, insn);
3609 break;
3610 default:
3611 unallocated_encoding(s);
3612 break;
3613 }
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00003614}
3615
3616/* C3.5 Data processing - register */
3617static void disas_data_proc_reg(DisasContext *s, uint32_t insn)
3618{
3619 switch (extract32(insn, 24, 5)) {
3620 case 0x0a: /* Logical (shifted register) */
3621 disas_logic_reg(s, insn);
3622 break;
3623 case 0x0b: /* Add/subtract */
3624 if (insn & (1 << 21)) { /* (extended register) */
3625 disas_add_sub_ext_reg(s, insn);
3626 } else {
3627 disas_add_sub_reg(s, insn);
3628 }
3629 break;
3630 case 0x1b: /* Data-processing (3 source) */
3631 disas_data_proc_3src(s, insn);
3632 break;
3633 case 0x1a:
3634 switch (extract32(insn, 21, 3)) {
3635 case 0x0: /* Add/subtract (with carry) */
3636 disas_adc_sbc(s, insn);
3637 break;
3638 case 0x2: /* Conditional compare */
Claudio Fontana750813c2014-01-04 22:15:46 +00003639 disas_cc(s, insn); /* both imm and reg forms */
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00003640 break;
3641 case 0x4: /* Conditional select */
3642 disas_cond_select(s, insn);
3643 break;
3644 case 0x6: /* Data-processing */
3645 if (insn & (1 << 30)) { /* (1 source) */
3646 disas_data_proc_1src(s, insn);
3647 } else { /* (2 source) */
3648 disas_data_proc_2src(s, insn);
3649 }
3650 break;
3651 default:
3652 unallocated_encoding(s);
3653 break;
3654 }
3655 break;
3656 default:
3657 unallocated_encoding(s);
3658 break;
3659 }
3660}
3661
Alexander Graf52a1f6a2014-01-07 17:19:14 +00003662/* Convert ARM rounding mode to softfloat */
3663static inline int arm_rmode_to_sf(int rmode)
3664{
3665 switch (rmode) {
3666 case FPROUNDING_TIEAWAY:
3667 rmode = float_round_ties_away;
3668 break;
3669 case FPROUNDING_ODD:
3670 /* FIXME: add support for TIEAWAY and ODD */
3671 qemu_log_mask(LOG_UNIMP, "arm: unimplemented rounding mode: %d\n",
3672 rmode);
3673 case FPROUNDING_TIEEVEN:
3674 default:
3675 rmode = float_round_nearest_even;
3676 break;
3677 case FPROUNDING_POSINF:
3678 rmode = float_round_up;
3679 break;
3680 case FPROUNDING_NEGINF:
3681 rmode = float_round_down;
3682 break;
3683 case FPROUNDING_ZERO:
3684 rmode = float_round_to_zero;
3685 break;
3686 }
3687 return rmode;
3688}
3689
Claudio Fontanada7dafe2014-01-04 22:15:50 +00003690static void handle_fp_compare(DisasContext *s, bool is_double,
3691 unsigned int rn, unsigned int rm,
3692 bool cmp_with_zero, bool signal_all_nans)
3693{
3694 TCGv_i64 tcg_flags = tcg_temp_new_i64();
3695 TCGv_ptr fpst = get_fpstatus_ptr();
3696
3697 if (is_double) {
3698 TCGv_i64 tcg_vn, tcg_vm;
3699
3700 tcg_vn = read_fp_dreg(s, rn);
3701 if (cmp_with_zero) {
3702 tcg_vm = tcg_const_i64(0);
3703 } else {
3704 tcg_vm = read_fp_dreg(s, rm);
3705 }
3706 if (signal_all_nans) {
3707 gen_helper_vfp_cmped_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
3708 } else {
3709 gen_helper_vfp_cmpd_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
3710 }
3711 tcg_temp_free_i64(tcg_vn);
3712 tcg_temp_free_i64(tcg_vm);
3713 } else {
3714 TCGv_i32 tcg_vn, tcg_vm;
3715
3716 tcg_vn = read_fp_sreg(s, rn);
3717 if (cmp_with_zero) {
3718 tcg_vm = tcg_const_i32(0);
3719 } else {
3720 tcg_vm = read_fp_sreg(s, rm);
3721 }
3722 if (signal_all_nans) {
3723 gen_helper_vfp_cmpes_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
3724 } else {
3725 gen_helper_vfp_cmps_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
3726 }
3727 tcg_temp_free_i32(tcg_vn);
3728 tcg_temp_free_i32(tcg_vm);
3729 }
3730
3731 tcg_temp_free_ptr(fpst);
3732
3733 gen_set_nzcv(tcg_flags);
3734
3735 tcg_temp_free_i64(tcg_flags);
3736}
3737
Peter Maydellfaa0ba42013-12-23 23:27:30 +00003738/* C3.6.22 Floating point compare
3739 * 31 30 29 28 24 23 22 21 20 16 15 14 13 10 9 5 4 0
3740 * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
3741 * | M | 0 | S | 1 1 1 1 0 | type | 1 | Rm | op | 1 0 0 0 | Rn | op2 |
3742 * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
3743 */
3744static void disas_fp_compare(DisasContext *s, uint32_t insn)
3745{
Claudio Fontanada7dafe2014-01-04 22:15:50 +00003746 unsigned int mos, type, rm, op, rn, opc, op2r;
3747
3748 mos = extract32(insn, 29, 3);
3749 type = extract32(insn, 22, 2); /* 0 = single, 1 = double */
3750 rm = extract32(insn, 16, 5);
3751 op = extract32(insn, 14, 2);
3752 rn = extract32(insn, 5, 5);
3753 opc = extract32(insn, 3, 2);
3754 op2r = extract32(insn, 0, 3);
3755
3756 if (mos || op || op2r || type > 1) {
3757 unallocated_encoding(s);
3758 return;
3759 }
3760
3761 handle_fp_compare(s, type, rn, rm, opc & 1, opc & 2);
Peter Maydellfaa0ba42013-12-23 23:27:30 +00003762}
3763
3764/* C3.6.23 Floating point conditional compare
3765 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 3 0
3766 * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
3767 * | M | 0 | S | 1 1 1 1 0 | type | 1 | Rm | cond | 0 1 | Rn | op | nzcv |
3768 * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
3769 */
3770static void disas_fp_ccomp(DisasContext *s, uint32_t insn)
3771{
Claudio Fontana513f1d72014-01-04 22:15:51 +00003772 unsigned int mos, type, rm, cond, rn, op, nzcv;
3773 TCGv_i64 tcg_flags;
3774 int label_continue = -1;
3775
3776 mos = extract32(insn, 29, 3);
3777 type = extract32(insn, 22, 2); /* 0 = single, 1 = double */
3778 rm = extract32(insn, 16, 5);
3779 cond = extract32(insn, 12, 4);
3780 rn = extract32(insn, 5, 5);
3781 op = extract32(insn, 4, 1);
3782 nzcv = extract32(insn, 0, 4);
3783
3784 if (mos || type > 1) {
3785 unallocated_encoding(s);
3786 return;
3787 }
3788
3789 if (cond < 0x0e) { /* not always */
3790 int label_match = gen_new_label();
3791 label_continue = gen_new_label();
3792 arm_gen_test_cc(cond, label_match);
3793 /* nomatch: */
3794 tcg_flags = tcg_const_i64(nzcv << 28);
3795 gen_set_nzcv(tcg_flags);
3796 tcg_temp_free_i64(tcg_flags);
3797 tcg_gen_br(label_continue);
3798 gen_set_label(label_match);
3799 }
3800
3801 handle_fp_compare(s, type, rn, rm, false, op);
3802
3803 if (cond < 0x0e) {
3804 gen_set_label(label_continue);
3805 }
Peter Maydellfaa0ba42013-12-23 23:27:30 +00003806}
3807
Claudio Fontana5640ff62014-01-04 22:15:51 +00003808/* copy src FP register to dst FP register; type specifies single or double */
3809static void gen_mov_fp2fp(DisasContext *s, int type, int dst, int src)
3810{
3811 if (type) {
3812 TCGv_i64 v = read_fp_dreg(s, src);
3813 write_fp_dreg(s, dst, v);
3814 tcg_temp_free_i64(v);
3815 } else {
3816 TCGv_i32 v = read_fp_sreg(s, src);
3817 write_fp_sreg(s, dst, v);
3818 tcg_temp_free_i32(v);
3819 }
3820}
3821
Peter Maydellfaa0ba42013-12-23 23:27:30 +00003822/* C3.6.24 Floating point conditional select
3823 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 0
3824 * +---+---+---+-----------+------+---+------+------+-----+------+------+
3825 * | M | 0 | S | 1 1 1 1 0 | type | 1 | Rm | cond | 1 1 | Rn | Rd |
3826 * +---+---+---+-----------+------+---+------+------+-----+------+------+
3827 */
3828static void disas_fp_csel(DisasContext *s, uint32_t insn)
3829{
Claudio Fontana5640ff62014-01-04 22:15:51 +00003830 unsigned int mos, type, rm, cond, rn, rd;
3831 int label_continue = -1;
3832
3833 mos = extract32(insn, 29, 3);
3834 type = extract32(insn, 22, 2); /* 0 = single, 1 = double */
3835 rm = extract32(insn, 16, 5);
3836 cond = extract32(insn, 12, 4);
3837 rn = extract32(insn, 5, 5);
3838 rd = extract32(insn, 0, 5);
3839
3840 if (mos || type > 1) {
3841 unallocated_encoding(s);
3842 return;
3843 }
3844
3845 if (cond < 0x0e) { /* not always */
3846 int label_match = gen_new_label();
3847 label_continue = gen_new_label();
3848 arm_gen_test_cc(cond, label_match);
3849 /* nomatch: */
3850 gen_mov_fp2fp(s, type, rd, rm);
3851 tcg_gen_br(label_continue);
3852 gen_set_label(label_match);
3853 }
3854
3855 gen_mov_fp2fp(s, type, rd, rn);
3856
3857 if (cond < 0x0e) { /* continue */
3858 gen_set_label(label_continue);
3859 }
Peter Maydellfaa0ba42013-12-23 23:27:30 +00003860}
3861
Peter Maydelld9b08482014-01-07 17:19:14 +00003862/* C3.6.25 Floating-point data-processing (1 source) - single precision */
3863static void handle_fp_1src_single(DisasContext *s, int opcode, int rd, int rn)
3864{
3865 TCGv_ptr fpst;
3866 TCGv_i32 tcg_op;
3867 TCGv_i32 tcg_res;
3868
3869 fpst = get_fpstatus_ptr();
3870 tcg_op = read_fp_sreg(s, rn);
3871 tcg_res = tcg_temp_new_i32();
3872
3873 switch (opcode) {
3874 case 0x0: /* FMOV */
3875 tcg_gen_mov_i32(tcg_res, tcg_op);
3876 break;
3877 case 0x1: /* FABS */
3878 gen_helper_vfp_abss(tcg_res, tcg_op);
3879 break;
3880 case 0x2: /* FNEG */
3881 gen_helper_vfp_negs(tcg_res, tcg_op);
3882 break;
3883 case 0x3: /* FSQRT */
3884 gen_helper_vfp_sqrts(tcg_res, tcg_op, cpu_env);
3885 break;
3886 case 0x8: /* FRINTN */
3887 case 0x9: /* FRINTP */
3888 case 0xa: /* FRINTM */
3889 case 0xb: /* FRINTZ */
3890 case 0xc: /* FRINTA */
3891 {
3892 TCGv_i32 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(opcode & 7));
3893
3894 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
3895 gen_helper_rints(tcg_res, tcg_op, fpst);
3896
3897 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
3898 tcg_temp_free_i32(tcg_rmode);
3899 break;
3900 }
3901 case 0xe: /* FRINTX */
3902 gen_helper_rints_exact(tcg_res, tcg_op, fpst);
3903 break;
3904 case 0xf: /* FRINTI */
3905 gen_helper_rints(tcg_res, tcg_op, fpst);
3906 break;
3907 default:
3908 abort();
3909 }
3910
3911 write_fp_sreg(s, rd, tcg_res);
3912
3913 tcg_temp_free_ptr(fpst);
3914 tcg_temp_free_i32(tcg_op);
3915 tcg_temp_free_i32(tcg_res);
3916}
3917
3918/* C3.6.25 Floating-point data-processing (1 source) - double precision */
3919static void handle_fp_1src_double(DisasContext *s, int opcode, int rd, int rn)
3920{
3921 TCGv_ptr fpst;
3922 TCGv_i64 tcg_op;
3923 TCGv_i64 tcg_res;
3924
3925 fpst = get_fpstatus_ptr();
3926 tcg_op = read_fp_dreg(s, rn);
3927 tcg_res = tcg_temp_new_i64();
3928
3929 switch (opcode) {
3930 case 0x0: /* FMOV */
3931 tcg_gen_mov_i64(tcg_res, tcg_op);
3932 break;
3933 case 0x1: /* FABS */
3934 gen_helper_vfp_absd(tcg_res, tcg_op);
3935 break;
3936 case 0x2: /* FNEG */
3937 gen_helper_vfp_negd(tcg_res, tcg_op);
3938 break;
3939 case 0x3: /* FSQRT */
3940 gen_helper_vfp_sqrtd(tcg_res, tcg_op, cpu_env);
3941 break;
3942 case 0x8: /* FRINTN */
3943 case 0x9: /* FRINTP */
3944 case 0xa: /* FRINTM */
3945 case 0xb: /* FRINTZ */
3946 case 0xc: /* FRINTA */
3947 {
3948 TCGv_i32 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(opcode & 7));
3949
3950 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
3951 gen_helper_rintd(tcg_res, tcg_op, fpst);
3952
3953 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
3954 tcg_temp_free_i32(tcg_rmode);
3955 break;
3956 }
3957 case 0xe: /* FRINTX */
3958 gen_helper_rintd_exact(tcg_res, tcg_op, fpst);
3959 break;
3960 case 0xf: /* FRINTI */
3961 gen_helper_rintd(tcg_res, tcg_op, fpst);
3962 break;
3963 default:
3964 abort();
3965 }
3966
3967 write_fp_dreg(s, rd, tcg_res);
3968
3969 tcg_temp_free_ptr(fpst);
3970 tcg_temp_free_i64(tcg_op);
3971 tcg_temp_free_i64(tcg_res);
3972}
3973
Peter Maydell8900aad2014-01-07 17:19:15 +00003974static void handle_fp_fcvt(DisasContext *s, int opcode,
3975 int rd, int rn, int dtype, int ntype)
3976{
3977 switch (ntype) {
3978 case 0x0:
3979 {
3980 TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
3981 if (dtype == 1) {
3982 /* Single to double */
3983 TCGv_i64 tcg_rd = tcg_temp_new_i64();
3984 gen_helper_vfp_fcvtds(tcg_rd, tcg_rn, cpu_env);
3985 write_fp_dreg(s, rd, tcg_rd);
3986 tcg_temp_free_i64(tcg_rd);
3987 } else {
3988 /* Single to half */
3989 TCGv_i32 tcg_rd = tcg_temp_new_i32();
3990 gen_helper_vfp_fcvt_f32_to_f16(tcg_rd, tcg_rn, cpu_env);
3991 /* write_fp_sreg is OK here because top half of tcg_rd is zero */
3992 write_fp_sreg(s, rd, tcg_rd);
3993 tcg_temp_free_i32(tcg_rd);
3994 }
3995 tcg_temp_free_i32(tcg_rn);
3996 break;
3997 }
3998 case 0x1:
3999 {
4000 TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
4001 TCGv_i32 tcg_rd = tcg_temp_new_i32();
4002 if (dtype == 0) {
4003 /* Double to single */
4004 gen_helper_vfp_fcvtsd(tcg_rd, tcg_rn, cpu_env);
4005 } else {
4006 /* Double to half */
4007 gen_helper_vfp_fcvt_f64_to_f16(tcg_rd, tcg_rn, cpu_env);
4008 /* write_fp_sreg is OK here because top half of tcg_rd is zero */
4009 }
4010 write_fp_sreg(s, rd, tcg_rd);
4011 tcg_temp_free_i32(tcg_rd);
4012 tcg_temp_free_i64(tcg_rn);
4013 break;
4014 }
4015 case 0x3:
4016 {
4017 TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
4018 tcg_gen_ext16u_i32(tcg_rn, tcg_rn);
4019 if (dtype == 0) {
4020 /* Half to single */
4021 TCGv_i32 tcg_rd = tcg_temp_new_i32();
4022 gen_helper_vfp_fcvt_f16_to_f32(tcg_rd, tcg_rn, cpu_env);
4023 write_fp_sreg(s, rd, tcg_rd);
4024 tcg_temp_free_i32(tcg_rd);
4025 } else {
4026 /* Half to double */
4027 TCGv_i64 tcg_rd = tcg_temp_new_i64();
4028 gen_helper_vfp_fcvt_f16_to_f64(tcg_rd, tcg_rn, cpu_env);
4029 write_fp_dreg(s, rd, tcg_rd);
4030 tcg_temp_free_i64(tcg_rd);
4031 }
4032 tcg_temp_free_i32(tcg_rn);
4033 break;
4034 }
4035 default:
4036 abort();
4037 }
4038}
4039
Peter Maydellfaa0ba42013-12-23 23:27:30 +00004040/* C3.6.25 Floating point data-processing (1 source)
4041 * 31 30 29 28 24 23 22 21 20 15 14 10 9 5 4 0
4042 * +---+---+---+-----------+------+---+--------+-----------+------+------+
4043 * | M | 0 | S | 1 1 1 1 0 | type | 1 | opcode | 1 0 0 0 0 | Rn | Rd |
4044 * +---+---+---+-----------+------+---+--------+-----------+------+------+
4045 */
4046static void disas_fp_1src(DisasContext *s, uint32_t insn)
4047{
Peter Maydelld9b08482014-01-07 17:19:14 +00004048 int type = extract32(insn, 22, 2);
4049 int opcode = extract32(insn, 15, 6);
4050 int rn = extract32(insn, 5, 5);
4051 int rd = extract32(insn, 0, 5);
4052
4053 switch (opcode) {
4054 case 0x4: case 0x5: case 0x7:
Peter Maydell8900aad2014-01-07 17:19:15 +00004055 {
Peter Maydelld9b08482014-01-07 17:19:14 +00004056 /* FCVT between half, single and double precision */
Peter Maydell8900aad2014-01-07 17:19:15 +00004057 int dtype = extract32(opcode, 0, 2);
4058 if (type == 2 || dtype == type) {
4059 unallocated_encoding(s);
4060 return;
4061 }
4062 handle_fp_fcvt(s, opcode, rd, rn, dtype, type);
Peter Maydelld9b08482014-01-07 17:19:14 +00004063 break;
Peter Maydell8900aad2014-01-07 17:19:15 +00004064 }
Peter Maydelld9b08482014-01-07 17:19:14 +00004065 case 0x0 ... 0x3:
4066 case 0x8 ... 0xc:
4067 case 0xe ... 0xf:
4068 /* 32-to-32 and 64-to-64 ops */
4069 switch (type) {
4070 case 0:
4071 handle_fp_1src_single(s, opcode, rd, rn);
4072 break;
4073 case 1:
4074 handle_fp_1src_double(s, opcode, rd, rn);
4075 break;
4076 default:
4077 unallocated_encoding(s);
4078 }
4079 break;
4080 default:
4081 unallocated_encoding(s);
4082 break;
4083 }
Peter Maydellfaa0ba42013-12-23 23:27:30 +00004084}
4085
Alexander Grafec73d2e2014-01-04 22:15:50 +00004086/* C3.6.26 Floating-point data-processing (2 source) - single precision */
4087static void handle_fp_2src_single(DisasContext *s, int opcode,
4088 int rd, int rn, int rm)
4089{
4090 TCGv_i32 tcg_op1;
4091 TCGv_i32 tcg_op2;
4092 TCGv_i32 tcg_res;
4093 TCGv_ptr fpst;
4094
4095 tcg_res = tcg_temp_new_i32();
4096 fpst = get_fpstatus_ptr();
4097 tcg_op1 = read_fp_sreg(s, rn);
4098 tcg_op2 = read_fp_sreg(s, rm);
4099
4100 switch (opcode) {
4101 case 0x0: /* FMUL */
4102 gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
4103 break;
4104 case 0x1: /* FDIV */
4105 gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
4106 break;
4107 case 0x2: /* FADD */
4108 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
4109 break;
4110 case 0x3: /* FSUB */
4111 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
4112 break;
4113 case 0x4: /* FMAX */
4114 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
4115 break;
4116 case 0x5: /* FMIN */
4117 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
4118 break;
4119 case 0x6: /* FMAXNM */
4120 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
4121 break;
4122 case 0x7: /* FMINNM */
4123 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
4124 break;
4125 case 0x8: /* FNMUL */
4126 gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
4127 gen_helper_vfp_negs(tcg_res, tcg_res);
4128 break;
4129 }
4130
4131 write_fp_sreg(s, rd, tcg_res);
4132
4133 tcg_temp_free_ptr(fpst);
4134 tcg_temp_free_i32(tcg_op1);
4135 tcg_temp_free_i32(tcg_op2);
4136 tcg_temp_free_i32(tcg_res);
4137}
4138
4139/* C3.6.26 Floating-point data-processing (2 source) - double precision */
4140static void handle_fp_2src_double(DisasContext *s, int opcode,
4141 int rd, int rn, int rm)
4142{
4143 TCGv_i64 tcg_op1;
4144 TCGv_i64 tcg_op2;
4145 TCGv_i64 tcg_res;
4146 TCGv_ptr fpst;
4147
4148 tcg_res = tcg_temp_new_i64();
4149 fpst = get_fpstatus_ptr();
4150 tcg_op1 = read_fp_dreg(s, rn);
4151 tcg_op2 = read_fp_dreg(s, rm);
4152
4153 switch (opcode) {
4154 case 0x0: /* FMUL */
4155 gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
4156 break;
4157 case 0x1: /* FDIV */
4158 gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
4159 break;
4160 case 0x2: /* FADD */
4161 gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
4162 break;
4163 case 0x3: /* FSUB */
4164 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
4165 break;
4166 case 0x4: /* FMAX */
4167 gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
4168 break;
4169 case 0x5: /* FMIN */
4170 gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
4171 break;
4172 case 0x6: /* FMAXNM */
4173 gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
4174 break;
4175 case 0x7: /* FMINNM */
4176 gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
4177 break;
4178 case 0x8: /* FNMUL */
4179 gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
4180 gen_helper_vfp_negd(tcg_res, tcg_res);
4181 break;
4182 }
4183
4184 write_fp_dreg(s, rd, tcg_res);
4185
4186 tcg_temp_free_ptr(fpst);
4187 tcg_temp_free_i64(tcg_op1);
4188 tcg_temp_free_i64(tcg_op2);
4189 tcg_temp_free_i64(tcg_res);
4190}
4191
Peter Maydellfaa0ba42013-12-23 23:27:30 +00004192/* C3.6.26 Floating point data-processing (2 source)
4193 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 0
4194 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
4195 * | M | 0 | S | 1 1 1 1 0 | type | 1 | Rm | opcode | 1 0 | Rn | Rd |
4196 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
4197 */
4198static void disas_fp_2src(DisasContext *s, uint32_t insn)
4199{
Alexander Grafec73d2e2014-01-04 22:15:50 +00004200 int type = extract32(insn, 22, 2);
4201 int rd = extract32(insn, 0, 5);
4202 int rn = extract32(insn, 5, 5);
4203 int rm = extract32(insn, 16, 5);
4204 int opcode = extract32(insn, 12, 4);
4205
4206 if (opcode > 8) {
4207 unallocated_encoding(s);
4208 return;
4209 }
4210
4211 switch (type) {
4212 case 0:
4213 handle_fp_2src_single(s, opcode, rd, rn, rm);
4214 break;
4215 case 1:
4216 handle_fp_2src_double(s, opcode, rd, rn, rm);
4217 break;
4218 default:
4219 unallocated_encoding(s);
4220 }
Peter Maydellfaa0ba42013-12-23 23:27:30 +00004221}
4222
Alexander Graf6a306672014-01-04 22:15:50 +00004223/* C3.6.27 Floating-point data-processing (3 source) - single precision */
4224static void handle_fp_3src_single(DisasContext *s, bool o0, bool o1,
4225 int rd, int rn, int rm, int ra)
4226{
4227 TCGv_i32 tcg_op1, tcg_op2, tcg_op3;
4228 TCGv_i32 tcg_res = tcg_temp_new_i32();
4229 TCGv_ptr fpst = get_fpstatus_ptr();
4230
4231 tcg_op1 = read_fp_sreg(s, rn);
4232 tcg_op2 = read_fp_sreg(s, rm);
4233 tcg_op3 = read_fp_sreg(s, ra);
4234
4235 /* These are fused multiply-add, and must be done as one
4236 * floating point operation with no rounding between the
4237 * multiplication and addition steps.
4238 * NB that doing the negations here as separate steps is
4239 * correct : an input NaN should come out with its sign bit
4240 * flipped if it is a negated-input.
4241 */
4242 if (o1 == true) {
4243 gen_helper_vfp_negs(tcg_op3, tcg_op3);
4244 }
4245
4246 if (o0 != o1) {
4247 gen_helper_vfp_negs(tcg_op1, tcg_op1);
4248 }
4249
4250 gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
4251
4252 write_fp_sreg(s, rd, tcg_res);
4253
4254 tcg_temp_free_ptr(fpst);
4255 tcg_temp_free_i32(tcg_op1);
4256 tcg_temp_free_i32(tcg_op2);
4257 tcg_temp_free_i32(tcg_op3);
4258 tcg_temp_free_i32(tcg_res);
4259}
4260
4261/* C3.6.27 Floating-point data-processing (3 source) - double precision */
4262static void handle_fp_3src_double(DisasContext *s, bool o0, bool o1,
4263 int rd, int rn, int rm, int ra)
4264{
4265 TCGv_i64 tcg_op1, tcg_op2, tcg_op3;
4266 TCGv_i64 tcg_res = tcg_temp_new_i64();
4267 TCGv_ptr fpst = get_fpstatus_ptr();
4268
4269 tcg_op1 = read_fp_dreg(s, rn);
4270 tcg_op2 = read_fp_dreg(s, rm);
4271 tcg_op3 = read_fp_dreg(s, ra);
4272
4273 /* These are fused multiply-add, and must be done as one
4274 * floating point operation with no rounding between the
4275 * multiplication and addition steps.
4276 * NB that doing the negations here as separate steps is
4277 * correct : an input NaN should come out with its sign bit
4278 * flipped if it is a negated-input.
4279 */
4280 if (o1 == true) {
4281 gen_helper_vfp_negd(tcg_op3, tcg_op3);
4282 }
4283
4284 if (o0 != o1) {
4285 gen_helper_vfp_negd(tcg_op1, tcg_op1);
4286 }
4287
4288 gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
4289
4290 write_fp_dreg(s, rd, tcg_res);
4291
4292 tcg_temp_free_ptr(fpst);
4293 tcg_temp_free_i64(tcg_op1);
4294 tcg_temp_free_i64(tcg_op2);
4295 tcg_temp_free_i64(tcg_op3);
4296 tcg_temp_free_i64(tcg_res);
4297}
4298
Peter Maydellfaa0ba42013-12-23 23:27:30 +00004299/* C3.6.27 Floating point data-processing (3 source)
4300 * 31 30 29 28 24 23 22 21 20 16 15 14 10 9 5 4 0
4301 * +---+---+---+-----------+------+----+------+----+------+------+------+
4302 * | M | 0 | S | 1 1 1 1 1 | type | o1 | Rm | o0 | Ra | Rn | Rd |
4303 * +---+---+---+-----------+------+----+------+----+------+------+------+
4304 */
4305static void disas_fp_3src(DisasContext *s, uint32_t insn)
4306{
Alexander Graf6a306672014-01-04 22:15:50 +00004307 int type = extract32(insn, 22, 2);
4308 int rd = extract32(insn, 0, 5);
4309 int rn = extract32(insn, 5, 5);
4310 int ra = extract32(insn, 10, 5);
4311 int rm = extract32(insn, 16, 5);
4312 bool o0 = extract32(insn, 15, 1);
4313 bool o1 = extract32(insn, 21, 1);
4314
4315 switch (type) {
4316 case 0:
4317 handle_fp_3src_single(s, o0, o1, rd, rn, rm, ra);
4318 break;
4319 case 1:
4320 handle_fp_3src_double(s, o0, o1, rd, rn, rm, ra);
4321 break;
4322 default:
4323 unallocated_encoding(s);
4324 }
Peter Maydellfaa0ba42013-12-23 23:27:30 +00004325}
4326
4327/* C3.6.28 Floating point immediate
4328 * 31 30 29 28 24 23 22 21 20 13 12 10 9 5 4 0
4329 * +---+---+---+-----------+------+---+------------+-------+------+------+
4330 * | M | 0 | S | 1 1 1 1 0 | type | 1 | imm8 | 1 0 0 | imm5 | Rd |
4331 * +---+---+---+-----------+------+---+------------+-------+------+------+
4332 */
4333static void disas_fp_imm(DisasContext *s, uint32_t insn)
4334{
Alexander Graf6163f862014-01-04 22:15:50 +00004335 int rd = extract32(insn, 0, 5);
4336 int imm8 = extract32(insn, 13, 8);
4337 int is_double = extract32(insn, 22, 2);
4338 uint64_t imm;
4339 TCGv_i64 tcg_res;
4340
4341 if (is_double > 1) {
4342 unallocated_encoding(s);
4343 return;
4344 }
4345
4346 /* The imm8 encodes the sign bit, enough bits to represent
4347 * an exponent in the range 01....1xx to 10....0xx,
4348 * and the most significant 4 bits of the mantissa; see
4349 * VFPExpandImm() in the v8 ARM ARM.
4350 */
4351 if (is_double) {
4352 imm = (extract32(imm8, 7, 1) ? 0x8000 : 0) |
4353 (extract32(imm8, 6, 1) ? 0x3fc0 : 0x4000) |
4354 extract32(imm8, 0, 6);
4355 imm <<= 48;
4356 } else {
4357 imm = (extract32(imm8, 7, 1) ? 0x8000 : 0) |
4358 (extract32(imm8, 6, 1) ? 0x3e00 : 0x4000) |
4359 (extract32(imm8, 0, 6) << 3);
4360 imm <<= 16;
4361 }
4362
4363 tcg_res = tcg_const_i64(imm);
4364 write_fp_dreg(s, rd, tcg_res);
4365 tcg_temp_free_i64(tcg_res);
Peter Maydellfaa0ba42013-12-23 23:27:30 +00004366}
4367
Alexander Graf52a1f6a2014-01-07 17:19:14 +00004368/* Handle floating point <=> fixed point conversions. Note that we can
4369 * also deal with fp <=> integer conversions as a special case (scale == 64)
4370 * OPTME: consider handling that special case specially or at least skipping
4371 * the call to scalbn in the helpers for zero shifts.
4372 */
4373static void handle_fpfpcvt(DisasContext *s, int rd, int rn, int opcode,
4374 bool itof, int rmode, int scale, int sf, int type)
4375{
4376 bool is_signed = !(opcode & 1);
4377 bool is_double = type;
4378 TCGv_ptr tcg_fpstatus;
4379 TCGv_i32 tcg_shift;
4380
4381 tcg_fpstatus = get_fpstatus_ptr();
4382
4383 tcg_shift = tcg_const_i32(64 - scale);
4384
4385 if (itof) {
4386 TCGv_i64 tcg_int = cpu_reg(s, rn);
4387 if (!sf) {
4388 TCGv_i64 tcg_extend = new_tmp_a64(s);
4389
4390 if (is_signed) {
4391 tcg_gen_ext32s_i64(tcg_extend, tcg_int);
4392 } else {
4393 tcg_gen_ext32u_i64(tcg_extend, tcg_int);
4394 }
4395
4396 tcg_int = tcg_extend;
4397 }
4398
4399 if (is_double) {
4400 TCGv_i64 tcg_double = tcg_temp_new_i64();
4401 if (is_signed) {
4402 gen_helper_vfp_sqtod(tcg_double, tcg_int,
4403 tcg_shift, tcg_fpstatus);
4404 } else {
4405 gen_helper_vfp_uqtod(tcg_double, tcg_int,
4406 tcg_shift, tcg_fpstatus);
4407 }
4408 write_fp_dreg(s, rd, tcg_double);
4409 tcg_temp_free_i64(tcg_double);
4410 } else {
4411 TCGv_i32 tcg_single = tcg_temp_new_i32();
4412 if (is_signed) {
4413 gen_helper_vfp_sqtos(tcg_single, tcg_int,
4414 tcg_shift, tcg_fpstatus);
4415 } else {
4416 gen_helper_vfp_uqtos(tcg_single, tcg_int,
4417 tcg_shift, tcg_fpstatus);
4418 }
4419 write_fp_sreg(s, rd, tcg_single);
4420 tcg_temp_free_i32(tcg_single);
4421 }
4422 } else {
4423 TCGv_i64 tcg_int = cpu_reg(s, rd);
4424 TCGv_i32 tcg_rmode;
4425
4426 if (extract32(opcode, 2, 1)) {
4427 /* There are too many rounding modes to all fit into rmode,
4428 * so FCVTA[US] is a special case.
4429 */
4430 rmode = FPROUNDING_TIEAWAY;
4431 }
4432
4433 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rmode));
4434
4435 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
4436
4437 if (is_double) {
4438 TCGv_i64 tcg_double = read_fp_dreg(s, rn);
4439 if (is_signed) {
4440 if (!sf) {
4441 gen_helper_vfp_tosld(tcg_int, tcg_double,
4442 tcg_shift, tcg_fpstatus);
4443 } else {
4444 gen_helper_vfp_tosqd(tcg_int, tcg_double,
4445 tcg_shift, tcg_fpstatus);
4446 }
4447 } else {
4448 if (!sf) {
4449 gen_helper_vfp_tould(tcg_int, tcg_double,
4450 tcg_shift, tcg_fpstatus);
4451 } else {
4452 gen_helper_vfp_touqd(tcg_int, tcg_double,
4453 tcg_shift, tcg_fpstatus);
4454 }
4455 }
4456 tcg_temp_free_i64(tcg_double);
4457 } else {
4458 TCGv_i32 tcg_single = read_fp_sreg(s, rn);
4459 if (sf) {
4460 if (is_signed) {
4461 gen_helper_vfp_tosqs(tcg_int, tcg_single,
4462 tcg_shift, tcg_fpstatus);
4463 } else {
4464 gen_helper_vfp_touqs(tcg_int, tcg_single,
4465 tcg_shift, tcg_fpstatus);
4466 }
4467 } else {
4468 TCGv_i32 tcg_dest = tcg_temp_new_i32();
4469 if (is_signed) {
4470 gen_helper_vfp_tosls(tcg_dest, tcg_single,
4471 tcg_shift, tcg_fpstatus);
4472 } else {
4473 gen_helper_vfp_touls(tcg_dest, tcg_single,
4474 tcg_shift, tcg_fpstatus);
4475 }
4476 tcg_gen_extu_i32_i64(tcg_int, tcg_dest);
4477 tcg_temp_free_i32(tcg_dest);
4478 }
4479 tcg_temp_free_i32(tcg_single);
4480 }
4481
4482 gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
4483 tcg_temp_free_i32(tcg_rmode);
4484
4485 if (!sf) {
4486 tcg_gen_ext32u_i64(tcg_int, tcg_int);
4487 }
4488 }
4489
4490 tcg_temp_free_ptr(tcg_fpstatus);
4491 tcg_temp_free_i32(tcg_shift);
4492}
4493
Peter Maydellfaa0ba42013-12-23 23:27:30 +00004494/* C3.6.29 Floating point <-> fixed point conversions
4495 * 31 30 29 28 24 23 22 21 20 19 18 16 15 10 9 5 4 0
4496 * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
4497 * | sf | 0 | S | 1 1 1 1 0 | type | 0 | rmode | opcode | scale | Rn | Rd |
4498 * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
4499 */
4500static void disas_fp_fixed_conv(DisasContext *s, uint32_t insn)
4501{
Alexander Graf52a1f6a2014-01-07 17:19:14 +00004502 int rd = extract32(insn, 0, 5);
4503 int rn = extract32(insn, 5, 5);
4504 int scale = extract32(insn, 10, 6);
4505 int opcode = extract32(insn, 16, 3);
4506 int rmode = extract32(insn, 19, 2);
4507 int type = extract32(insn, 22, 2);
4508 bool sbit = extract32(insn, 29, 1);
4509 bool sf = extract32(insn, 31, 1);
4510 bool itof;
4511
4512 if (sbit || (type > 1)
4513 || (!sf && scale < 32)) {
4514 unallocated_encoding(s);
4515 return;
4516 }
4517
4518 switch ((rmode << 3) | opcode) {
4519 case 0x2: /* SCVTF */
4520 case 0x3: /* UCVTF */
4521 itof = true;
4522 break;
4523 case 0x18: /* FCVTZS */
4524 case 0x19: /* FCVTZU */
4525 itof = false;
4526 break;
4527 default:
4528 unallocated_encoding(s);
4529 return;
4530 }
4531
4532 handle_fpfpcvt(s, rd, rn, opcode, itof, FPROUNDING_ZERO, scale, sf, type);
Peter Maydellfaa0ba42013-12-23 23:27:30 +00004533}
4534
Peter Maydellce5458e2013-12-23 23:27:31 +00004535static void handle_fmov(DisasContext *s, int rd, int rn, int type, bool itof)
4536{
4537 /* FMOV: gpr to or from float, double, or top half of quad fp reg,
4538 * without conversion.
4539 */
4540
4541 if (itof) {
Peter Maydellce5458e2013-12-23 23:27:31 +00004542 TCGv_i64 tcg_rn = cpu_reg(s, rn);
4543
4544 switch (type) {
4545 case 0:
4546 {
4547 /* 32 bit */
4548 TCGv_i64 tmp = tcg_temp_new_i64();
4549 tcg_gen_ext32u_i64(tmp, tcg_rn);
Peter Maydelle2f90562014-01-04 22:15:49 +00004550 tcg_gen_st_i64(tmp, cpu_env, fp_reg_offset(rd, MO_64));
Peter Maydellce5458e2013-12-23 23:27:31 +00004551 tcg_gen_movi_i64(tmp, 0);
Peter Maydelle2f90562014-01-04 22:15:49 +00004552 tcg_gen_st_i64(tmp, cpu_env, fp_reg_hi_offset(rd));
Peter Maydellce5458e2013-12-23 23:27:31 +00004553 tcg_temp_free_i64(tmp);
4554 break;
4555 }
4556 case 1:
4557 {
4558 /* 64 bit */
4559 TCGv_i64 tmp = tcg_const_i64(0);
Peter Maydelle2f90562014-01-04 22:15:49 +00004560 tcg_gen_st_i64(tcg_rn, cpu_env, fp_reg_offset(rd, MO_64));
4561 tcg_gen_st_i64(tmp, cpu_env, fp_reg_hi_offset(rd));
Peter Maydellce5458e2013-12-23 23:27:31 +00004562 tcg_temp_free_i64(tmp);
4563 break;
4564 }
4565 case 2:
4566 /* 64 bit to top half. */
Peter Maydelle2f90562014-01-04 22:15:49 +00004567 tcg_gen_st_i64(tcg_rn, cpu_env, fp_reg_hi_offset(rd));
Peter Maydellce5458e2013-12-23 23:27:31 +00004568 break;
4569 }
4570 } else {
Peter Maydellce5458e2013-12-23 23:27:31 +00004571 TCGv_i64 tcg_rd = cpu_reg(s, rd);
4572
4573 switch (type) {
4574 case 0:
4575 /* 32 bit */
Peter Maydelle2f90562014-01-04 22:15:49 +00004576 tcg_gen_ld32u_i64(tcg_rd, cpu_env, fp_reg_offset(rn, MO_32));
4577 break;
4578 case 1:
4579 /* 64 bit */
4580 tcg_gen_ld_i64(tcg_rd, cpu_env, fp_reg_offset(rn, MO_64));
Peter Maydellce5458e2013-12-23 23:27:31 +00004581 break;
4582 case 2:
4583 /* 64 bits from top half */
Peter Maydelle2f90562014-01-04 22:15:49 +00004584 tcg_gen_ld_i64(tcg_rd, cpu_env, fp_reg_hi_offset(rn));
Peter Maydellce5458e2013-12-23 23:27:31 +00004585 break;
4586 }
4587 }
4588}
4589
Peter Maydellfaa0ba42013-12-23 23:27:30 +00004590/* C3.6.30 Floating point <-> integer conversions
4591 * 31 30 29 28 24 23 22 21 20 19 18 16 15 10 9 5 4 0
4592 * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
Will Newtonc436d402014-01-07 17:19:14 +00004593 * | 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 +00004594 * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
4595 */
4596static void disas_fp_int_conv(DisasContext *s, uint32_t insn)
4597{
Peter Maydellce5458e2013-12-23 23:27:31 +00004598 int rd = extract32(insn, 0, 5);
4599 int rn = extract32(insn, 5, 5);
4600 int opcode = extract32(insn, 16, 3);
4601 int rmode = extract32(insn, 19, 2);
4602 int type = extract32(insn, 22, 2);
4603 bool sbit = extract32(insn, 29, 1);
4604 bool sf = extract32(insn, 31, 1);
4605
Will Newtonc436d402014-01-07 17:19:14 +00004606 if (sbit) {
4607 unallocated_encoding(s);
4608 return;
4609 }
4610
4611 if (opcode > 5) {
Peter Maydellce5458e2013-12-23 23:27:31 +00004612 /* FMOV */
4613 bool itof = opcode & 1;
4614
Will Newtonc436d402014-01-07 17:19:14 +00004615 if (rmode >= 2) {
4616 unallocated_encoding(s);
4617 return;
4618 }
4619
Peter Maydellce5458e2013-12-23 23:27:31 +00004620 switch (sf << 3 | type << 1 | rmode) {
4621 case 0x0: /* 32 bit */
4622 case 0xa: /* 64 bit */
4623 case 0xd: /* 64 bit to top half of quad */
4624 break;
4625 default:
4626 /* all other sf/type/rmode combinations are invalid */
4627 unallocated_encoding(s);
4628 break;
4629 }
4630
4631 handle_fmov(s, rd, rn, type, itof);
4632 } else {
4633 /* actual FP conversions */
Will Newtonc436d402014-01-07 17:19:14 +00004634 bool itof = extract32(opcode, 1, 1);
4635
4636 if (type > 1 || (rmode != 0 && opcode > 1)) {
4637 unallocated_encoding(s);
4638 return;
4639 }
4640
4641 handle_fpfpcvt(s, rd, rn, opcode, itof, rmode, 64, sf, type);
Peter Maydellce5458e2013-12-23 23:27:31 +00004642 }
Peter Maydellfaa0ba42013-12-23 23:27:30 +00004643}
4644
4645/* FP-specific subcases of table C3-6 (SIMD and FP data processing)
4646 * 31 30 29 28 25 24 0
4647 * +---+---+---+---------+-----------------------------+
4648 * | | 0 | | 1 1 1 1 | |
4649 * +---+---+---+---------+-----------------------------+
4650 */
4651static void disas_data_proc_fp(DisasContext *s, uint32_t insn)
4652{
4653 if (extract32(insn, 24, 1)) {
4654 /* Floating point data-processing (3 source) */
4655 disas_fp_3src(s, insn);
4656 } else if (extract32(insn, 21, 1) == 0) {
4657 /* Floating point to fixed point conversions */
4658 disas_fp_fixed_conv(s, insn);
4659 } else {
4660 switch (extract32(insn, 10, 2)) {
4661 case 1:
4662 /* Floating point conditional compare */
4663 disas_fp_ccomp(s, insn);
4664 break;
4665 case 2:
4666 /* Floating point data-processing (2 source) */
4667 disas_fp_2src(s, insn);
4668 break;
4669 case 3:
4670 /* Floating point conditional select */
4671 disas_fp_csel(s, insn);
4672 break;
4673 case 0:
4674 switch (ctz32(extract32(insn, 12, 4))) {
4675 case 0: /* [15:12] == xxx1 */
4676 /* Floating point immediate */
4677 disas_fp_imm(s, insn);
4678 break;
4679 case 1: /* [15:12] == xx10 */
4680 /* Floating point compare */
4681 disas_fp_compare(s, insn);
4682 break;
4683 case 2: /* [15:12] == x100 */
4684 /* Floating point data-processing (1 source) */
4685 disas_fp_1src(s, insn);
4686 break;
4687 case 3: /* [15:12] == 1000 */
4688 unallocated_encoding(s);
4689 break;
4690 default: /* [15:12] == 0000 */
4691 /* Floating point <-> integer conversions */
4692 disas_fp_int_conv(s, insn);
4693 break;
4694 }
4695 break;
4696 }
4697 }
4698}
4699
Peter Maydellc4ee3382014-01-23 14:37:08 +00004700static void do_ext64(DisasContext *s, TCGv_i64 tcg_left, TCGv_i64 tcg_right,
4701 int pos)
4702{
4703 /* Extract 64 bits from the middle of two concatenated 64 bit
4704 * vector register slices left:right. The extracted bits start
4705 * at 'pos' bits into the right (least significant) side.
4706 * We return the result in tcg_right, and guarantee not to
4707 * trash tcg_left.
4708 */
4709 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
4710 assert(pos > 0 && pos < 64);
4711
4712 tcg_gen_shri_i64(tcg_right, tcg_right, pos);
4713 tcg_gen_shli_i64(tcg_tmp, tcg_left, 64 - pos);
4714 tcg_gen_or_i64(tcg_right, tcg_right, tcg_tmp);
4715
4716 tcg_temp_free_i64(tcg_tmp);
4717}
4718
Alex Bennée94ad8702014-01-23 14:37:07 +00004719/* C3.6.1 EXT
4720 * 31 30 29 24 23 22 21 20 16 15 14 11 10 9 5 4 0
4721 * +---+---+-------------+-----+---+------+---+------+---+------+------+
4722 * | 0 | Q | 1 0 1 1 1 0 | op2 | 0 | Rm | 0 | imm4 | 0 | Rn | Rd |
4723 * +---+---+-------------+-----+---+------+---+------+---+------+------+
4724 */
4725static void disas_simd_ext(DisasContext *s, uint32_t insn)
4726{
Peter Maydellc4ee3382014-01-23 14:37:08 +00004727 int is_q = extract32(insn, 30, 1);
4728 int op2 = extract32(insn, 22, 2);
4729 int imm4 = extract32(insn, 11, 4);
4730 int rm = extract32(insn, 16, 5);
4731 int rn = extract32(insn, 5, 5);
4732 int rd = extract32(insn, 0, 5);
4733 int pos = imm4 << 3;
4734 TCGv_i64 tcg_resl, tcg_resh;
4735
4736 if (op2 != 0 || (!is_q && extract32(imm4, 3, 1))) {
4737 unallocated_encoding(s);
4738 return;
4739 }
4740
4741 tcg_resh = tcg_temp_new_i64();
4742 tcg_resl = tcg_temp_new_i64();
4743
4744 /* Vd gets bits starting at pos bits into Vm:Vn. This is
4745 * either extracting 128 bits from a 128:128 concatenation, or
4746 * extracting 64 bits from a 64:64 concatenation.
4747 */
4748 if (!is_q) {
4749 read_vec_element(s, tcg_resl, rn, 0, MO_64);
4750 if (pos != 0) {
4751 read_vec_element(s, tcg_resh, rm, 0, MO_64);
4752 do_ext64(s, tcg_resh, tcg_resl, pos);
4753 }
4754 tcg_gen_movi_i64(tcg_resh, 0);
4755 } else {
4756 TCGv_i64 tcg_hh;
4757 typedef struct {
4758 int reg;
4759 int elt;
4760 } EltPosns;
4761 EltPosns eltposns[] = { {rn, 0}, {rn, 1}, {rm, 0}, {rm, 1} };
4762 EltPosns *elt = eltposns;
4763
4764 if (pos >= 64) {
4765 elt++;
4766 pos -= 64;
4767 }
4768
4769 read_vec_element(s, tcg_resl, elt->reg, elt->elt, MO_64);
4770 elt++;
4771 read_vec_element(s, tcg_resh, elt->reg, elt->elt, MO_64);
4772 elt++;
4773 if (pos != 0) {
4774 do_ext64(s, tcg_resh, tcg_resl, pos);
4775 tcg_hh = tcg_temp_new_i64();
4776 read_vec_element(s, tcg_hh, elt->reg, elt->elt, MO_64);
4777 do_ext64(s, tcg_hh, tcg_resh, pos);
4778 tcg_temp_free_i64(tcg_hh);
4779 }
4780 }
4781
4782 write_vec_element(s, tcg_resl, rd, 0, MO_64);
4783 tcg_temp_free_i64(tcg_resl);
4784 write_vec_element(s, tcg_resh, rd, 1, MO_64);
4785 tcg_temp_free_i64(tcg_resh);
Alex Bennée94ad8702014-01-23 14:37:07 +00004786}
4787
4788/* C3.6.2 TBL/TBX
4789 * 31 30 29 24 23 22 21 20 16 15 14 13 12 11 10 9 5 4 0
4790 * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
4791 * | 0 | Q | 0 0 1 1 1 0 | op2 | 0 | Rm | 0 | len | op | 0 0 | Rn | Rd |
4792 * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
4793 */
4794static void disas_simd_tb(DisasContext *s, uint32_t insn)
4795{
Michael Matzdc3250e2014-01-23 14:37:08 +00004796 int op2 = extract32(insn, 22, 2);
4797 int is_q = extract32(insn, 30, 1);
4798 int rm = extract32(insn, 16, 5);
4799 int rn = extract32(insn, 5, 5);
4800 int rd = extract32(insn, 0, 5);
4801 int is_tblx = extract32(insn, 12, 1);
4802 int len = extract32(insn, 13, 2);
4803 TCGv_i64 tcg_resl, tcg_resh, tcg_idx;
4804 TCGv_i32 tcg_regno, tcg_numregs;
4805
4806 if (op2 != 0) {
4807 unallocated_encoding(s);
4808 return;
4809 }
4810
4811 /* This does a table lookup: for every byte element in the input
4812 * we index into a table formed from up to four vector registers,
4813 * and then the output is the result of the lookups. Our helper
4814 * function does the lookup operation for a single 64 bit part of
4815 * the input.
4816 */
4817 tcg_resl = tcg_temp_new_i64();
4818 tcg_resh = tcg_temp_new_i64();
4819
4820 if (is_tblx) {
4821 read_vec_element(s, tcg_resl, rd, 0, MO_64);
4822 } else {
4823 tcg_gen_movi_i64(tcg_resl, 0);
4824 }
4825 if (is_tblx && is_q) {
4826 read_vec_element(s, tcg_resh, rd, 1, MO_64);
4827 } else {
4828 tcg_gen_movi_i64(tcg_resh, 0);
4829 }
4830
4831 tcg_idx = tcg_temp_new_i64();
4832 tcg_regno = tcg_const_i32(rn);
4833 tcg_numregs = tcg_const_i32(len + 1);
4834 read_vec_element(s, tcg_idx, rm, 0, MO_64);
4835 gen_helper_simd_tbl(tcg_resl, cpu_env, tcg_resl, tcg_idx,
4836 tcg_regno, tcg_numregs);
4837 if (is_q) {
4838 read_vec_element(s, tcg_idx, rm, 1, MO_64);
4839 gen_helper_simd_tbl(tcg_resh, cpu_env, tcg_resh, tcg_idx,
4840 tcg_regno, tcg_numregs);
4841 }
4842 tcg_temp_free_i64(tcg_idx);
4843 tcg_temp_free_i32(tcg_regno);
4844 tcg_temp_free_i32(tcg_numregs);
4845
4846 write_vec_element(s, tcg_resl, rd, 0, MO_64);
4847 tcg_temp_free_i64(tcg_resl);
4848 write_vec_element(s, tcg_resh, rd, 1, MO_64);
4849 tcg_temp_free_i64(tcg_resh);
Alex Bennée94ad8702014-01-23 14:37:07 +00004850}
4851
4852/* C3.6.3 ZIP/UZP/TRN
4853 * 31 30 29 24 23 22 21 20 16 15 14 12 11 10 9 5 4 0
4854 * +---+---+-------------+------+---+------+---+------------------+------+
4855 * | 0 | Q | 0 0 1 1 1 0 | size | 0 | Rm | 0 | opc | 1 0 | Rn | Rd |
4856 * +---+---+-------------+------+---+------+---+------------------+------+
4857 */
4858static void disas_simd_zip_trn(DisasContext *s, uint32_t insn)
4859{
Michael Matz19c3a9d2014-01-23 14:37:08 +00004860 int rd = extract32(insn, 0, 5);
4861 int rn = extract32(insn, 5, 5);
4862 int rm = extract32(insn, 16, 5);
4863 int size = extract32(insn, 22, 2);
4864 /* opc field bits [1:0] indicate ZIP/UZP/TRN;
4865 * bit 2 indicates 1 vs 2 variant of the insn.
4866 */
4867 int opcode = extract32(insn, 12, 2);
4868 bool part = extract32(insn, 14, 1);
4869 bool is_q = extract32(insn, 30, 1);
4870 int esize = 8 << size;
4871 int i, ofs;
4872 int datasize = is_q ? 128 : 64;
4873 int elements = datasize / esize;
4874 TCGv_i64 tcg_res, tcg_resl, tcg_resh;
4875
4876 if (opcode == 0 || (size == 3 && !is_q)) {
4877 unallocated_encoding(s);
4878 return;
4879 }
4880
4881 tcg_resl = tcg_const_i64(0);
4882 tcg_resh = tcg_const_i64(0);
4883 tcg_res = tcg_temp_new_i64();
4884
4885 for (i = 0; i < elements; i++) {
4886 switch (opcode) {
4887 case 1: /* UZP1/2 */
4888 {
4889 int midpoint = elements / 2;
4890 if (i < midpoint) {
4891 read_vec_element(s, tcg_res, rn, 2 * i + part, size);
4892 } else {
4893 read_vec_element(s, tcg_res, rm,
4894 2 * (i - midpoint) + part, size);
4895 }
4896 break;
4897 }
4898 case 2: /* TRN1/2 */
4899 if (i & 1) {
4900 read_vec_element(s, tcg_res, rm, (i & ~1) + part, size);
4901 } else {
4902 read_vec_element(s, tcg_res, rn, (i & ~1) + part, size);
4903 }
4904 break;
4905 case 3: /* ZIP1/2 */
4906 {
4907 int base = part * elements / 2;
4908 if (i & 1) {
4909 read_vec_element(s, tcg_res, rm, base + (i >> 1), size);
4910 } else {
4911 read_vec_element(s, tcg_res, rn, base + (i >> 1), size);
4912 }
4913 break;
4914 }
4915 default:
4916 g_assert_not_reached();
4917 }
4918
4919 ofs = i * esize;
4920 if (ofs < 64) {
4921 tcg_gen_shli_i64(tcg_res, tcg_res, ofs);
4922 tcg_gen_or_i64(tcg_resl, tcg_resl, tcg_res);
4923 } else {
4924 tcg_gen_shli_i64(tcg_res, tcg_res, ofs - 64);
4925 tcg_gen_or_i64(tcg_resh, tcg_resh, tcg_res);
4926 }
4927 }
4928
4929 tcg_temp_free_i64(tcg_res);
4930
4931 write_vec_element(s, tcg_resl, rd, 0, MO_64);
4932 tcg_temp_free_i64(tcg_resl);
4933 write_vec_element(s, tcg_resh, rd, 1, MO_64);
4934 tcg_temp_free_i64(tcg_resh);
Alex Bennée94ad8702014-01-23 14:37:07 +00004935}
4936
Michael Matz20287612014-01-23 14:37:08 +00004937static void do_minmaxop(DisasContext *s, TCGv_i32 tcg_elt1, TCGv_i32 tcg_elt2,
4938 int opc, bool is_min, TCGv_ptr fpst)
4939{
4940 /* Helper function for disas_simd_across_lanes: do a single precision
4941 * min/max operation on the specified two inputs,
4942 * and return the result in tcg_elt1.
4943 */
4944 if (opc == 0xc) {
4945 if (is_min) {
4946 gen_helper_vfp_minnums(tcg_elt1, tcg_elt1, tcg_elt2, fpst);
4947 } else {
4948 gen_helper_vfp_maxnums(tcg_elt1, tcg_elt1, tcg_elt2, fpst);
4949 }
4950 } else {
4951 assert(opc == 0xf);
4952 if (is_min) {
4953 gen_helper_vfp_mins(tcg_elt1, tcg_elt1, tcg_elt2, fpst);
4954 } else {
4955 gen_helper_vfp_maxs(tcg_elt1, tcg_elt1, tcg_elt2, fpst);
4956 }
4957 }
4958}
4959
Alex Bennée94ad8702014-01-23 14:37:07 +00004960/* C3.6.4 AdvSIMD across lanes
4961 * 31 30 29 28 24 23 22 21 17 16 12 11 10 9 5 4 0
4962 * +---+---+---+-----------+------+-----------+--------+-----+------+------+
4963 * | 0 | Q | U | 0 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 | Rn | Rd |
4964 * +---+---+---+-----------+------+-----------+--------+-----+------+------+
4965 */
4966static void disas_simd_across_lanes(DisasContext *s, uint32_t insn)
4967{
Michael Matz20287612014-01-23 14:37:08 +00004968 int rd = extract32(insn, 0, 5);
4969 int rn = extract32(insn, 5, 5);
4970 int size = extract32(insn, 22, 2);
4971 int opcode = extract32(insn, 12, 5);
4972 bool is_q = extract32(insn, 30, 1);
4973 bool is_u = extract32(insn, 29, 1);
4974 bool is_fp = false;
4975 bool is_min = false;
4976 int esize;
4977 int elements;
4978 int i;
4979 TCGv_i64 tcg_res, tcg_elt;
4980
4981 switch (opcode) {
4982 case 0x1b: /* ADDV */
4983 if (is_u) {
4984 unallocated_encoding(s);
4985 return;
4986 }
4987 /* fall through */
4988 case 0x3: /* SADDLV, UADDLV */
4989 case 0xa: /* SMAXV, UMAXV */
4990 case 0x1a: /* SMINV, UMINV */
4991 if (size == 3 || (size == 2 && !is_q)) {
4992 unallocated_encoding(s);
4993 return;
4994 }
4995 break;
4996 case 0xc: /* FMAXNMV, FMINNMV */
4997 case 0xf: /* FMAXV, FMINV */
4998 if (!is_u || !is_q || extract32(size, 0, 1)) {
4999 unallocated_encoding(s);
5000 return;
5001 }
5002 /* Bit 1 of size field encodes min vs max, and actual size is always
5003 * 32 bits: adjust the size variable so following code can rely on it
5004 */
5005 is_min = extract32(size, 1, 1);
5006 is_fp = true;
5007 size = 2;
5008 break;
5009 default:
5010 unallocated_encoding(s);
5011 return;
5012 }
5013
5014 esize = 8 << size;
5015 elements = (is_q ? 128 : 64) / esize;
5016
5017 tcg_res = tcg_temp_new_i64();
5018 tcg_elt = tcg_temp_new_i64();
5019
5020 /* These instructions operate across all lanes of a vector
5021 * to produce a single result. We can guarantee that a 64
5022 * bit intermediate is sufficient:
5023 * + for [US]ADDLV the maximum element size is 32 bits, and
5024 * the result type is 64 bits
5025 * + for FMAX*V, FMIN*V, ADDV the intermediate type is the
5026 * same as the element size, which is 32 bits at most
5027 * For the integer operations we can choose to work at 64
5028 * or 32 bits and truncate at the end; for simplicity
5029 * we use 64 bits always. The floating point
5030 * ops do require 32 bit intermediates, though.
5031 */
5032 if (!is_fp) {
5033 read_vec_element(s, tcg_res, rn, 0, size | (is_u ? 0 : MO_SIGN));
5034
5035 for (i = 1; i < elements; i++) {
5036 read_vec_element(s, tcg_elt, rn, i, size | (is_u ? 0 : MO_SIGN));
5037
5038 switch (opcode) {
5039 case 0x03: /* SADDLV / UADDLV */
5040 case 0x1b: /* ADDV */
5041 tcg_gen_add_i64(tcg_res, tcg_res, tcg_elt);
5042 break;
5043 case 0x0a: /* SMAXV / UMAXV */
5044 tcg_gen_movcond_i64(is_u ? TCG_COND_GEU : TCG_COND_GE,
5045 tcg_res,
5046 tcg_res, tcg_elt, tcg_res, tcg_elt);
5047 break;
5048 case 0x1a: /* SMINV / UMINV */
5049 tcg_gen_movcond_i64(is_u ? TCG_COND_LEU : TCG_COND_LE,
5050 tcg_res,
5051 tcg_res, tcg_elt, tcg_res, tcg_elt);
5052 break;
5053 break;
5054 default:
5055 g_assert_not_reached();
5056 }
5057
5058 }
5059 } else {
5060 /* Floating point ops which work on 32 bit (single) intermediates.
5061 * Note that correct NaN propagation requires that we do these
5062 * operations in exactly the order specified by the pseudocode.
5063 */
5064 TCGv_i32 tcg_elt1 = tcg_temp_new_i32();
5065 TCGv_i32 tcg_elt2 = tcg_temp_new_i32();
5066 TCGv_i32 tcg_elt3 = tcg_temp_new_i32();
5067 TCGv_ptr fpst = get_fpstatus_ptr();
5068
5069 assert(esize == 32);
5070 assert(elements == 4);
5071
5072 read_vec_element(s, tcg_elt, rn, 0, MO_32);
5073 tcg_gen_trunc_i64_i32(tcg_elt1, tcg_elt);
5074 read_vec_element(s, tcg_elt, rn, 1, MO_32);
5075 tcg_gen_trunc_i64_i32(tcg_elt2, tcg_elt);
5076
5077 do_minmaxop(s, tcg_elt1, tcg_elt2, opcode, is_min, fpst);
5078
5079 read_vec_element(s, tcg_elt, rn, 2, MO_32);
5080 tcg_gen_trunc_i64_i32(tcg_elt2, tcg_elt);
5081 read_vec_element(s, tcg_elt, rn, 3, MO_32);
5082 tcg_gen_trunc_i64_i32(tcg_elt3, tcg_elt);
5083
5084 do_minmaxop(s, tcg_elt2, tcg_elt3, opcode, is_min, fpst);
5085
5086 do_minmaxop(s, tcg_elt1, tcg_elt2, opcode, is_min, fpst);
5087
5088 tcg_gen_extu_i32_i64(tcg_res, tcg_elt1);
5089 tcg_temp_free_i32(tcg_elt1);
5090 tcg_temp_free_i32(tcg_elt2);
5091 tcg_temp_free_i32(tcg_elt3);
5092 tcg_temp_free_ptr(fpst);
5093 }
5094
5095 tcg_temp_free_i64(tcg_elt);
5096
5097 /* Now truncate the result to the width required for the final output */
5098 if (opcode == 0x03) {
5099 /* SADDLV, UADDLV: result is 2*esize */
5100 size++;
5101 }
5102
5103 switch (size) {
5104 case 0:
5105 tcg_gen_ext8u_i64(tcg_res, tcg_res);
5106 break;
5107 case 1:
5108 tcg_gen_ext16u_i64(tcg_res, tcg_res);
5109 break;
5110 case 2:
5111 tcg_gen_ext32u_i64(tcg_res, tcg_res);
5112 break;
5113 case 3:
5114 break;
5115 default:
5116 g_assert_not_reached();
5117 }
5118
5119 write_fp_dreg(s, rd, tcg_res);
5120 tcg_temp_free_i64(tcg_res);
Alex Bennée94ad8702014-01-23 14:37:07 +00005121}
5122
Alex Bennée5725d0d2014-01-23 14:37:08 +00005123/* C6.3.31 DUP (Element, Vector)
5124 *
5125 * 31 30 29 21 20 16 15 10 9 5 4 0
5126 * +---+---+-------------------+--------+-------------+------+------+
5127 * | 0 | Q | 0 0 1 1 1 0 0 0 0 | imm5 | 0 0 0 0 0 1 | Rn | Rd |
5128 * +---+---+-------------------+--------+-------------+------+------+
5129 *
5130 * size: encoded in imm5 (see ARM ARM LowestSetBit())
5131 */
5132static void handle_simd_dupe(DisasContext *s, int is_q, int rd, int rn,
5133 int imm5)
5134{
5135 int size = ctz32(imm5);
5136 int esize = 8 << size;
5137 int elements = (is_q ? 128 : 64) / esize;
5138 int index, i;
5139 TCGv_i64 tmp;
5140
5141 if (size > 3 || (size == 3 && !is_q)) {
5142 unallocated_encoding(s);
5143 return;
5144 }
5145
5146 index = imm5 >> (size + 1);
5147
5148 tmp = tcg_temp_new_i64();
5149 read_vec_element(s, tmp, rn, index, size);
5150
5151 for (i = 0; i < elements; i++) {
5152 write_vec_element(s, tmp, rd, i, size);
5153 }
5154
5155 if (!is_q) {
5156 clear_vec_high(s, rd);
5157 }
5158
5159 tcg_temp_free_i64(tmp);
5160}
5161
Peter Maydellf9469f92014-01-23 14:37:09 +00005162/* C6.3.31 DUP (element, scalar)
5163 * 31 21 20 16 15 10 9 5 4 0
5164 * +-----------------------+--------+-------------+------+------+
5165 * | 0 1 0 1 1 1 1 0 0 0 0 | imm5 | 0 0 0 0 0 1 | Rn | Rd |
5166 * +-----------------------+--------+-------------+------+------+
5167 */
5168static void handle_simd_dupes(DisasContext *s, int rd, int rn,
5169 int imm5)
5170{
5171 int size = ctz32(imm5);
5172 int index;
5173 TCGv_i64 tmp;
5174
5175 if (size > 3) {
5176 unallocated_encoding(s);
5177 return;
5178 }
5179
5180 index = imm5 >> (size + 1);
5181
5182 /* This instruction just extracts the specified element and
5183 * zero-extends it into the bottom of the destination register.
5184 */
5185 tmp = tcg_temp_new_i64();
5186 read_vec_element(s, tmp, rn, index, size);
5187 write_fp_dreg(s, rd, tmp);
5188 tcg_temp_free_i64(tmp);
5189}
5190
Alex Bennée5725d0d2014-01-23 14:37:08 +00005191/* C6.3.32 DUP (General)
5192 *
5193 * 31 30 29 21 20 16 15 10 9 5 4 0
5194 * +---+---+-------------------+--------+-------------+------+------+
5195 * | 0 | Q | 0 0 1 1 1 0 0 0 0 | imm5 | 0 0 0 0 1 1 | Rn | Rd |
5196 * +---+---+-------------------+--------+-------------+------+------+
5197 *
5198 * size: encoded in imm5 (see ARM ARM LowestSetBit())
5199 */
5200static void handle_simd_dupg(DisasContext *s, int is_q, int rd, int rn,
5201 int imm5)
5202{
5203 int size = ctz32(imm5);
5204 int esize = 8 << size;
5205 int elements = (is_q ? 128 : 64)/esize;
5206 int i = 0;
5207
5208 if (size > 3 || ((size == 3) && !is_q)) {
5209 unallocated_encoding(s);
5210 return;
5211 }
5212 for (i = 0; i < elements; i++) {
5213 write_vec_element(s, cpu_reg(s, rn), rd, i, size);
5214 }
5215 if (!is_q) {
5216 clear_vec_high(s, rd);
5217 }
5218}
5219
5220/* C6.3.150 INS (Element)
5221 *
5222 * 31 21 20 16 15 14 11 10 9 5 4 0
5223 * +-----------------------+--------+------------+---+------+------+
5224 * | 0 1 1 0 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 | Rn | Rd |
5225 * +-----------------------+--------+------------+---+------+------+
5226 *
5227 * size: encoded in imm5 (see ARM ARM LowestSetBit())
5228 * index: encoded in imm5<4:size+1>
5229 */
5230static void handle_simd_inse(DisasContext *s, int rd, int rn,
5231 int imm4, int imm5)
5232{
5233 int size = ctz32(imm5);
5234 int src_index, dst_index;
5235 TCGv_i64 tmp;
5236
5237 if (size > 3) {
5238 unallocated_encoding(s);
5239 return;
5240 }
5241 dst_index = extract32(imm5, 1+size, 5);
5242 src_index = extract32(imm4, size, 4);
5243
5244 tmp = tcg_temp_new_i64();
5245
5246 read_vec_element(s, tmp, rn, src_index, size);
5247 write_vec_element(s, tmp, rd, dst_index, size);
5248
5249 tcg_temp_free_i64(tmp);
5250}
5251
5252
5253/* C6.3.151 INS (General)
5254 *
5255 * 31 21 20 16 15 10 9 5 4 0
5256 * +-----------------------+--------+-------------+------+------+
5257 * | 0 1 0 0 1 1 1 0 0 0 0 | imm5 | 0 0 0 1 1 1 | Rn | Rd |
5258 * +-----------------------+--------+-------------+------+------+
5259 *
5260 * size: encoded in imm5 (see ARM ARM LowestSetBit())
5261 * index: encoded in imm5<4:size+1>
5262 */
5263static void handle_simd_insg(DisasContext *s, int rd, int rn, int imm5)
5264{
5265 int size = ctz32(imm5);
5266 int idx;
5267
5268 if (size > 3) {
5269 unallocated_encoding(s);
5270 return;
5271 }
5272
5273 idx = extract32(imm5, 1 + size, 4 - size);
5274 write_vec_element(s, cpu_reg(s, rn), rd, idx, size);
5275}
5276
5277/*
5278 * C6.3.321 UMOV (General)
5279 * C6.3.237 SMOV (General)
5280 *
5281 * 31 30 29 21 20 16 15 12 10 9 5 4 0
5282 * +---+---+-------------------+--------+-------------+------+------+
5283 * | 0 | Q | 0 0 1 1 1 0 0 0 0 | imm5 | 0 0 1 U 1 1 | Rn | Rd |
5284 * +---+---+-------------------+--------+-------------+------+------+
5285 *
5286 * U: unsigned when set
5287 * size: encoded in imm5 (see ARM ARM LowestSetBit())
5288 */
5289static void handle_simd_umov_smov(DisasContext *s, int is_q, int is_signed,
5290 int rn, int rd, int imm5)
5291{
5292 int size = ctz32(imm5);
5293 int element;
5294 TCGv_i64 tcg_rd;
5295
5296 /* Check for UnallocatedEncodings */
5297 if (is_signed) {
5298 if (size > 2 || (size == 2 && !is_q)) {
5299 unallocated_encoding(s);
5300 return;
5301 }
5302 } else {
5303 if (size > 3
5304 || (size < 3 && is_q)
5305 || (size == 3 && !is_q)) {
5306 unallocated_encoding(s);
5307 return;
5308 }
5309 }
5310 element = extract32(imm5, 1+size, 4);
5311
5312 tcg_rd = cpu_reg(s, rd);
5313 read_vec_element(s, tcg_rd, rn, element, size | (is_signed ? MO_SIGN : 0));
5314 if (is_signed && !is_q) {
5315 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
5316 }
5317}
5318
Alex Bennée94ad8702014-01-23 14:37:07 +00005319/* C3.6.5 AdvSIMD copy
5320 * 31 30 29 28 21 20 16 15 14 11 10 9 5 4 0
5321 * +---+---+----+-----------------+------+---+------+---+------+------+
5322 * | 0 | Q | op | 0 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 | Rn | Rd |
5323 * +---+---+----+-----------------+------+---+------+---+------+------+
5324 */
5325static void disas_simd_copy(DisasContext *s, uint32_t insn)
5326{
Alex Bennée5725d0d2014-01-23 14:37:08 +00005327 int rd = extract32(insn, 0, 5);
5328 int rn = extract32(insn, 5, 5);
5329 int imm4 = extract32(insn, 11, 4);
5330 int op = extract32(insn, 29, 1);
5331 int is_q = extract32(insn, 30, 1);
5332 int imm5 = extract32(insn, 16, 5);
5333
5334 if (op) {
5335 if (is_q) {
5336 /* INS (element) */
5337 handle_simd_inse(s, rd, rn, imm4, imm5);
5338 } else {
5339 unallocated_encoding(s);
5340 }
5341 } else {
5342 switch (imm4) {
5343 case 0:
5344 /* DUP (element - vector) */
5345 handle_simd_dupe(s, is_q, rd, rn, imm5);
5346 break;
5347 case 1:
5348 /* DUP (general) */
5349 handle_simd_dupg(s, is_q, rd, rn, imm5);
5350 break;
5351 case 3:
5352 if (is_q) {
5353 /* INS (general) */
5354 handle_simd_insg(s, rd, rn, imm5);
5355 } else {
5356 unallocated_encoding(s);
5357 }
5358 break;
5359 case 5:
5360 case 7:
5361 /* UMOV/SMOV (is_q indicates 32/64; imm4 indicates signedness) */
5362 handle_simd_umov_smov(s, is_q, (imm4 == 5), rn, rd, imm5);
5363 break;
5364 default:
5365 unallocated_encoding(s);
5366 break;
5367 }
5368 }
Alex Bennée94ad8702014-01-23 14:37:07 +00005369}
5370
5371/* C3.6.6 AdvSIMD modified immediate
5372 * 31 30 29 28 19 18 16 15 12 11 10 9 5 4 0
5373 * +---+---+----+---------------------+-----+-------+----+---+-------+------+
5374 * | 0 | Q | op | 0 1 1 1 1 0 0 0 0 0 | abc | cmode | o2 | 1 | defgh | Rd |
5375 * +---+---+----+---------------------+-----+-------+----+---+-------+------+
Alex Bennéeee5dfda2014-01-23 14:37:09 +00005376 *
5377 * There are a number of operations that can be carried out here:
5378 * MOVI - move (shifted) imm into register
5379 * MVNI - move inverted (shifted) imm into register
5380 * ORR - bitwise OR of (shifted) imm with register
5381 * BIC - bitwise clear of (shifted) imm with register
Alex Bennée94ad8702014-01-23 14:37:07 +00005382 */
5383static void disas_simd_mod_imm(DisasContext *s, uint32_t insn)
5384{
Alex Bennéeee5dfda2014-01-23 14:37:09 +00005385 int rd = extract32(insn, 0, 5);
5386 int cmode = extract32(insn, 12, 4);
5387 int cmode_3_1 = extract32(cmode, 1, 3);
5388 int cmode_0 = extract32(cmode, 0, 1);
5389 int o2 = extract32(insn, 11, 1);
5390 uint64_t abcdefgh = extract32(insn, 5, 5) | (extract32(insn, 16, 3) << 5);
5391 bool is_neg = extract32(insn, 29, 1);
5392 bool is_q = extract32(insn, 30, 1);
5393 uint64_t imm = 0;
5394 TCGv_i64 tcg_rd, tcg_imm;
5395 int i;
5396
5397 if (o2 != 0 || ((cmode == 0xf) && is_neg && !is_q)) {
5398 unallocated_encoding(s);
5399 return;
5400 }
5401
5402 /* See AdvSIMDExpandImm() in ARM ARM */
5403 switch (cmode_3_1) {
5404 case 0: /* Replicate(Zeros(24):imm8, 2) */
5405 case 1: /* Replicate(Zeros(16):imm8:Zeros(8), 2) */
5406 case 2: /* Replicate(Zeros(8):imm8:Zeros(16), 2) */
5407 case 3: /* Replicate(imm8:Zeros(24), 2) */
5408 {
5409 int shift = cmode_3_1 * 8;
5410 imm = bitfield_replicate(abcdefgh << shift, 32);
5411 break;
5412 }
5413 case 4: /* Replicate(Zeros(8):imm8, 4) */
5414 case 5: /* Replicate(imm8:Zeros(8), 4) */
5415 {
5416 int shift = (cmode_3_1 & 0x1) * 8;
5417 imm = bitfield_replicate(abcdefgh << shift, 16);
5418 break;
5419 }
5420 case 6:
5421 if (cmode_0) {
5422 /* Replicate(Zeros(8):imm8:Ones(16), 2) */
5423 imm = (abcdefgh << 16) | 0xffff;
5424 } else {
5425 /* Replicate(Zeros(16):imm8:Ones(8), 2) */
5426 imm = (abcdefgh << 8) | 0xff;
5427 }
5428 imm = bitfield_replicate(imm, 32);
5429 break;
5430 case 7:
5431 if (!cmode_0 && !is_neg) {
5432 imm = bitfield_replicate(abcdefgh, 8);
5433 } else if (!cmode_0 && is_neg) {
5434 int i;
5435 imm = 0;
5436 for (i = 0; i < 8; i++) {
5437 if ((abcdefgh) & (1 << i)) {
5438 imm |= 0xffULL << (i * 8);
5439 }
5440 }
5441 } else if (cmode_0) {
5442 if (is_neg) {
5443 imm = (abcdefgh & 0x3f) << 48;
5444 if (abcdefgh & 0x80) {
5445 imm |= 0x8000000000000000ULL;
5446 }
5447 if (abcdefgh & 0x40) {
5448 imm |= 0x3fc0000000000000ULL;
5449 } else {
5450 imm |= 0x4000000000000000ULL;
5451 }
5452 } else {
5453 imm = (abcdefgh & 0x3f) << 19;
5454 if (abcdefgh & 0x80) {
5455 imm |= 0x80000000;
5456 }
5457 if (abcdefgh & 0x40) {
5458 imm |= 0x3e000000;
5459 } else {
5460 imm |= 0x40000000;
5461 }
5462 imm |= (imm << 32);
5463 }
5464 }
5465 break;
5466 }
5467
5468 if (cmode_3_1 != 7 && is_neg) {
5469 imm = ~imm;
5470 }
5471
5472 tcg_imm = tcg_const_i64(imm);
5473 tcg_rd = new_tmp_a64(s);
5474
5475 for (i = 0; i < 2; i++) {
5476 int foffs = i ? fp_reg_hi_offset(rd) : fp_reg_offset(rd, MO_64);
5477
5478 if (i == 1 && !is_q) {
5479 /* non-quad ops clear high half of vector */
5480 tcg_gen_movi_i64(tcg_rd, 0);
5481 } else if ((cmode & 0x9) == 0x1 || (cmode & 0xd) == 0x9) {
5482 tcg_gen_ld_i64(tcg_rd, cpu_env, foffs);
5483 if (is_neg) {
5484 /* AND (BIC) */
5485 tcg_gen_and_i64(tcg_rd, tcg_rd, tcg_imm);
5486 } else {
5487 /* ORR */
5488 tcg_gen_or_i64(tcg_rd, tcg_rd, tcg_imm);
5489 }
5490 } else {
5491 /* MOVI */
5492 tcg_gen_mov_i64(tcg_rd, tcg_imm);
5493 }
5494 tcg_gen_st_i64(tcg_rd, cpu_env, foffs);
5495 }
5496
5497 tcg_temp_free_i64(tcg_imm);
Alex Bennée94ad8702014-01-23 14:37:07 +00005498}
5499
5500/* C3.6.7 AdvSIMD scalar copy
5501 * 31 30 29 28 21 20 16 15 14 11 10 9 5 4 0
5502 * +-----+----+-----------------+------+---+------+---+------+------+
5503 * | 0 1 | op | 1 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 | Rn | Rd |
5504 * +-----+----+-----------------+------+---+------+---+------+------+
5505 */
5506static void disas_simd_scalar_copy(DisasContext *s, uint32_t insn)
5507{
Peter Maydellf9469f92014-01-23 14:37:09 +00005508 int rd = extract32(insn, 0, 5);
5509 int rn = extract32(insn, 5, 5);
5510 int imm4 = extract32(insn, 11, 4);
5511 int imm5 = extract32(insn, 16, 5);
5512 int op = extract32(insn, 29, 1);
5513
5514 if (op != 0 || imm4 != 0) {
5515 unallocated_encoding(s);
5516 return;
5517 }
5518
5519 /* DUP (element, scalar) */
5520 handle_simd_dupes(s, rd, rn, imm5);
Alex Bennée94ad8702014-01-23 14:37:07 +00005521}
5522
5523/* C3.6.8 AdvSIMD scalar pairwise
5524 * 31 30 29 28 24 23 22 21 17 16 12 11 10 9 5 4 0
5525 * +-----+---+-----------+------+-----------+--------+-----+------+------+
5526 * | 0 1 | U | 1 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 | Rn | Rd |
5527 * +-----+---+-----------+------+-----------+--------+-----+------+------+
5528 */
5529static void disas_simd_scalar_pairwise(DisasContext *s, uint32_t insn)
5530{
5531 unsupported_encoding(s, insn);
5532}
5533
Alex Bennée4db2b1e2014-01-09 18:13:58 +00005534/*
5535 * Common SSHR[RA]/USHR[RA] - Shift right (optional rounding/accumulate)
5536 *
5537 * This code is handles the common shifting code and is used by both
5538 * the vector and scalar code.
5539 */
5540static void handle_shri_with_rndacc(TCGv_i64 tcg_res, TCGv_i64 tcg_src,
5541 TCGv_i64 tcg_rnd, bool accumulate,
5542 bool is_u, int size, int shift)
5543{
5544 bool extended_result = false;
5545 bool round = !TCGV_IS_UNUSED_I64(tcg_rnd);
5546 int ext_lshift = 0;
5547 TCGv_i64 tcg_src_hi;
5548
5549 if (round && size == 3) {
5550 extended_result = true;
5551 ext_lshift = 64 - shift;
5552 tcg_src_hi = tcg_temp_new_i64();
5553 } else if (shift == 64) {
5554 if (!accumulate && is_u) {
5555 /* result is zero */
5556 tcg_gen_movi_i64(tcg_res, 0);
5557 return;
5558 }
5559 }
5560
5561 /* Deal with the rounding step */
5562 if (round) {
5563 if (extended_result) {
5564 TCGv_i64 tcg_zero = tcg_const_i64(0);
5565 if (!is_u) {
5566 /* take care of sign extending tcg_res */
5567 tcg_gen_sari_i64(tcg_src_hi, tcg_src, 63);
5568 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
5569 tcg_src, tcg_src_hi,
5570 tcg_rnd, tcg_zero);
5571 } else {
5572 tcg_gen_add2_i64(tcg_src, tcg_src_hi,
5573 tcg_src, tcg_zero,
5574 tcg_rnd, tcg_zero);
5575 }
5576 tcg_temp_free_i64(tcg_zero);
5577 } else {
5578 tcg_gen_add_i64(tcg_src, tcg_src, tcg_rnd);
5579 }
5580 }
5581
5582 /* Now do the shift right */
5583 if (round && extended_result) {
5584 /* extended case, >64 bit precision required */
5585 if (ext_lshift == 0) {
5586 /* special case, only high bits matter */
5587 tcg_gen_mov_i64(tcg_src, tcg_src_hi);
5588 } else {
5589 tcg_gen_shri_i64(tcg_src, tcg_src, shift);
5590 tcg_gen_shli_i64(tcg_src_hi, tcg_src_hi, ext_lshift);
5591 tcg_gen_or_i64(tcg_src, tcg_src, tcg_src_hi);
5592 }
5593 } else {
5594 if (is_u) {
5595 if (shift == 64) {
5596 /* essentially shifting in 64 zeros */
5597 tcg_gen_movi_i64(tcg_src, 0);
5598 } else {
5599 tcg_gen_shri_i64(tcg_src, tcg_src, shift);
5600 }
5601 } else {
5602 if (shift == 64) {
5603 /* effectively extending the sign-bit */
5604 tcg_gen_sari_i64(tcg_src, tcg_src, 63);
5605 } else {
5606 tcg_gen_sari_i64(tcg_src, tcg_src, shift);
5607 }
5608 }
5609 }
5610
5611 if (accumulate) {
5612 tcg_gen_add_i64(tcg_res, tcg_res, tcg_src);
5613 } else {
5614 tcg_gen_mov_i64(tcg_res, tcg_src);
5615 }
5616
5617 if (extended_result) {
5618 tcg_temp_free_i64(tcg_src_hi);
5619 }
5620}
5621
5622/* Common SHL/SLI - Shift left with an optional insert */
5623static void handle_shli_with_ins(TCGv_i64 tcg_res, TCGv_i64 tcg_src,
5624 bool insert, int shift)
5625{
5626 if (insert) { /* SLI */
5627 tcg_gen_deposit_i64(tcg_res, tcg_res, tcg_src, shift, 64 - shift);
5628 } else { /* SHL */
5629 tcg_gen_shli_i64(tcg_res, tcg_src, shift);
5630 }
5631}
5632
5633/* SSHR[RA]/USHR[RA] - Scalar shift right (optional rounding/accumulate) */
5634static void handle_scalar_simd_shri(DisasContext *s,
5635 bool is_u, int immh, int immb,
5636 int opcode, int rn, int rd)
5637{
5638 const int size = 3;
5639 int immhb = immh << 3 | immb;
5640 int shift = 2 * (8 << size) - immhb;
5641 bool accumulate = false;
5642 bool round = false;
5643 TCGv_i64 tcg_rn;
5644 TCGv_i64 tcg_rd;
5645 TCGv_i64 tcg_round;
5646
5647 if (!extract32(immh, 3, 1)) {
5648 unallocated_encoding(s);
5649 return;
5650 }
5651
5652 switch (opcode) {
5653 case 0x02: /* SSRA / USRA (accumulate) */
5654 accumulate = true;
5655 break;
5656 case 0x04: /* SRSHR / URSHR (rounding) */
5657 round = true;
5658 break;
5659 case 0x06: /* SRSRA / URSRA (accum + rounding) */
5660 accumulate = round = true;
5661 break;
5662 }
5663
5664 if (round) {
5665 uint64_t round_const = 1ULL << (shift - 1);
5666 tcg_round = tcg_const_i64(round_const);
5667 } else {
5668 TCGV_UNUSED_I64(tcg_round);
5669 }
5670
5671 tcg_rn = read_fp_dreg(s, rn);
5672 tcg_rd = accumulate ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
5673
5674 handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
5675 accumulate, is_u, size, shift);
5676
5677 write_fp_dreg(s, rd, tcg_rd);
5678
5679 tcg_temp_free_i64(tcg_rn);
5680 tcg_temp_free_i64(tcg_rd);
5681 if (round) {
5682 tcg_temp_free_i64(tcg_round);
5683 }
5684}
5685
5686/* SHL/SLI - Scalar shift left */
5687static void handle_scalar_simd_shli(DisasContext *s, bool insert,
5688 int immh, int immb, int opcode,
5689 int rn, int rd)
5690{
5691 int size = 32 - clz32(immh) - 1;
5692 int immhb = immh << 3 | immb;
5693 int shift = immhb - (8 << size);
5694 TCGv_i64 tcg_rn = new_tmp_a64(s);
5695 TCGv_i64 tcg_rd = new_tmp_a64(s);
5696
5697 if (!extract32(immh, 3, 1)) {
5698 unallocated_encoding(s);
5699 return;
5700 }
5701
5702 tcg_rn = read_fp_dreg(s, rn);
5703 tcg_rd = insert ? read_fp_dreg(s, rd) : tcg_temp_new_i64();
5704
5705 handle_shli_with_ins(tcg_rd, tcg_rn, insert, shift);
5706
5707 write_fp_dreg(s, rd, tcg_rd);
5708
5709 tcg_temp_free_i64(tcg_rn);
5710 tcg_temp_free_i64(tcg_rd);
5711}
5712
Alex Bennée94ad8702014-01-23 14:37:07 +00005713/* C3.6.9 AdvSIMD scalar shift by immediate
5714 * 31 30 29 28 23 22 19 18 16 15 11 10 9 5 4 0
5715 * +-----+---+-------------+------+------+--------+---+------+------+
5716 * | 0 1 | U | 1 1 1 1 1 0 | immh | immb | opcode | 1 | Rn | Rd |
5717 * +-----+---+-------------+------+------+--------+---+------+------+
Alex Bennée4db2b1e2014-01-09 18:13:58 +00005718 *
5719 * This is the scalar version so it works on a fixed sized registers
Alex Bennée94ad8702014-01-23 14:37:07 +00005720 */
5721static void disas_simd_scalar_shift_imm(DisasContext *s, uint32_t insn)
5722{
Alex Bennée4db2b1e2014-01-09 18:13:58 +00005723 int rd = extract32(insn, 0, 5);
5724 int rn = extract32(insn, 5, 5);
5725 int opcode = extract32(insn, 11, 5);
5726 int immb = extract32(insn, 16, 3);
5727 int immh = extract32(insn, 19, 4);
5728 bool is_u = extract32(insn, 29, 1);
5729
5730 switch (opcode) {
5731 case 0x00: /* SSHR / USHR */
5732 case 0x02: /* SSRA / USRA */
5733 case 0x04: /* SRSHR / URSHR */
5734 case 0x06: /* SRSRA / URSRA */
5735 handle_scalar_simd_shri(s, is_u, immh, immb, opcode, rn, rd);
5736 break;
5737 case 0x0a: /* SHL / SLI */
5738 handle_scalar_simd_shli(s, is_u, immh, immb, opcode, rn, rd);
5739 break;
5740 default:
5741 unsupported_encoding(s, insn);
5742 break;
5743 }
Alex Bennée94ad8702014-01-23 14:37:07 +00005744}
5745
5746/* C3.6.10 AdvSIMD scalar three different
5747 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 0
5748 * +-----+---+-----------+------+---+------+--------+-----+------+------+
5749 * | 0 1 | U | 1 1 1 1 0 | size | 1 | Rm | opcode | 0 0 | Rn | Rd |
5750 * +-----+---+-----------+------+---+------+--------+-----+------+------+
5751 */
5752static void disas_simd_scalar_three_reg_diff(DisasContext *s, uint32_t insn)
5753{
5754 unsupported_encoding(s, insn);
5755}
5756
Peter Maydell453ff1f2014-01-07 12:13:47 +00005757static void handle_3same_64(DisasContext *s, int opcode, bool u,
5758 TCGv_i64 tcg_rd, TCGv_i64 tcg_rn, TCGv_i64 tcg_rm)
5759{
5760 /* Handle 64x64->64 opcodes which are shared between the scalar
5761 * and vector 3-same groups. We cover every opcode where size == 3
5762 * is valid in either the three-reg-same (integer, not pairwise)
5763 * or scalar-three-reg-same groups. (Some opcodes are not yet
5764 * implemented.)
5765 */
5766 TCGCond cond;
5767
5768 switch (opcode) {
5769 case 0x6: /* CMGT, CMHI */
5770 /* 64 bit integer comparison, result = test ? (2^64 - 1) : 0.
5771 * We implement this using setcond (test) and then negating.
5772 */
5773 cond = u ? TCG_COND_GTU : TCG_COND_GT;
5774 do_cmop:
5775 tcg_gen_setcond_i64(cond, tcg_rd, tcg_rn, tcg_rm);
5776 tcg_gen_neg_i64(tcg_rd, tcg_rd);
5777 break;
5778 case 0x7: /* CMGE, CMHS */
5779 cond = u ? TCG_COND_GEU : TCG_COND_GE;
5780 goto do_cmop;
5781 case 0x11: /* CMTST, CMEQ */
5782 if (u) {
5783 cond = TCG_COND_EQ;
5784 goto do_cmop;
5785 }
5786 /* CMTST : test is "if (X & Y != 0)". */
5787 tcg_gen_and_i64(tcg_rd, tcg_rn, tcg_rm);
5788 tcg_gen_setcondi_i64(TCG_COND_NE, tcg_rd, tcg_rd, 0);
5789 tcg_gen_neg_i64(tcg_rd, tcg_rd);
5790 break;
5791 case 0x10: /* ADD, SUB */
5792 if (u) {
5793 tcg_gen_sub_i64(tcg_rd, tcg_rn, tcg_rm);
5794 } else {
5795 tcg_gen_add_i64(tcg_rd, tcg_rn, tcg_rm);
5796 }
5797 break;
5798 case 0x1: /* SQADD */
5799 case 0x5: /* SQSUB */
5800 case 0x8: /* SSHL, USHL */
5801 case 0x9: /* SQSHL, UQSHL */
5802 case 0xa: /* SRSHL, URSHL */
5803 case 0xb: /* SQRSHL, UQRSHL */
5804 default:
5805 g_assert_not_reached();
5806 }
5807}
5808
Peter Maydell55bab512014-01-17 11:35:41 +00005809/* Handle the 3-same-operands float operations; shared by the scalar
5810 * and vector encodings. The caller must filter out any encodings
5811 * not allocated for the encoding it is dealing with.
5812 */
5813static void handle_3same_float(DisasContext *s, int size, int elements,
5814 int fpopcode, int rd, int rn, int rm)
5815{
5816 int pass;
5817 TCGv_ptr fpst = get_fpstatus_ptr();
5818
5819 for (pass = 0; pass < elements; pass++) {
5820 if (size) {
5821 /* Double */
5822 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
5823 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
5824 TCGv_i64 tcg_res = tcg_temp_new_i64();
5825
5826 read_vec_element(s, tcg_op1, rn, pass, MO_64);
5827 read_vec_element(s, tcg_op2, rm, pass, MO_64);
5828
5829 switch (fpopcode) {
5830 case 0x18: /* FMAXNM */
5831 gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
5832 break;
5833 case 0x1a: /* FADD */
5834 gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
5835 break;
5836 case 0x1e: /* FMAX */
5837 gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
5838 break;
5839 case 0x38: /* FMINNM */
5840 gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
5841 break;
5842 case 0x3a: /* FSUB */
5843 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
5844 break;
5845 case 0x3e: /* FMIN */
5846 gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
5847 break;
5848 case 0x5b: /* FMUL */
5849 gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
5850 break;
5851 case 0x5f: /* FDIV */
5852 gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
5853 break;
5854 case 0x7a: /* FABD */
5855 gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
5856 gen_helper_vfp_absd(tcg_res, tcg_res);
5857 break;
5858 default:
5859 g_assert_not_reached();
5860 }
5861
5862 write_vec_element(s, tcg_res, rd, pass, MO_64);
5863
5864 tcg_temp_free_i64(tcg_res);
5865 tcg_temp_free_i64(tcg_op1);
5866 tcg_temp_free_i64(tcg_op2);
5867 } else {
5868 /* Single */
5869 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
5870 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
5871 TCGv_i32 tcg_res = tcg_temp_new_i32();
5872
5873 read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
5874 read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
5875
5876 switch (fpopcode) {
5877 case 0x1a: /* FADD */
5878 gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
5879 break;
5880 case 0x1e: /* FMAX */
5881 gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
5882 break;
5883 case 0x18: /* FMAXNM */
5884 gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
5885 break;
5886 case 0x38: /* FMINNM */
5887 gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
5888 break;
5889 case 0x3a: /* FSUB */
5890 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
5891 break;
5892 case 0x3e: /* FMIN */
5893 gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
5894 break;
5895 case 0x5b: /* FMUL */
5896 gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
5897 break;
5898 case 0x5f: /* FDIV */
5899 gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
5900 break;
5901 case 0x7a: /* FABD */
5902 gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
5903 gen_helper_vfp_abss(tcg_res, tcg_res);
5904 break;
5905 default:
5906 g_assert_not_reached();
5907 }
5908
5909 if (elements == 1) {
5910 /* scalar single so clear high part */
5911 TCGv_i64 tcg_tmp = tcg_temp_new_i64();
5912
5913 tcg_gen_extu_i32_i64(tcg_tmp, tcg_res);
5914 write_vec_element(s, tcg_tmp, rd, pass, MO_64);
5915 tcg_temp_free_i64(tcg_tmp);
5916 } else {
5917 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
5918 }
5919
5920 tcg_temp_free_i32(tcg_res);
5921 tcg_temp_free_i32(tcg_op1);
5922 tcg_temp_free_i32(tcg_op2);
5923 }
5924 }
5925
5926 tcg_temp_free_ptr(fpst);
5927
5928 if ((elements << size) < 4) {
5929 /* scalar, or non-quad vector op */
5930 clear_vec_high(s, rd);
5931 }
5932}
5933
Alex Bennée94ad8702014-01-23 14:37:07 +00005934/* C3.6.11 AdvSIMD scalar three same
5935 * 31 30 29 28 24 23 22 21 20 16 15 11 10 9 5 4 0
5936 * +-----+---+-----------+------+---+------+--------+---+------+------+
5937 * | 0 1 | U | 1 1 1 1 0 | size | 1 | Rm | opcode | 1 | Rn | Rd |
5938 * +-----+---+-----------+------+---+------+--------+---+------+------+
5939 */
5940static void disas_simd_scalar_three_reg_same(DisasContext *s, uint32_t insn)
5941{
Peter Maydell453ff1f2014-01-07 12:13:47 +00005942 int rd = extract32(insn, 0, 5);
5943 int rn = extract32(insn, 5, 5);
5944 int opcode = extract32(insn, 11, 5);
5945 int rm = extract32(insn, 16, 5);
5946 int size = extract32(insn, 22, 2);
5947 bool u = extract32(insn, 29, 1);
5948 TCGv_i64 tcg_rn;
5949 TCGv_i64 tcg_rm;
5950 TCGv_i64 tcg_rd;
5951
5952 if (opcode >= 0x18) {
5953 /* Floating point: U, size[1] and opcode indicate operation */
5954 int fpopcode = opcode | (extract32(size, 1, 1) << 5) | (u << 6);
5955 switch (fpopcode) {
5956 case 0x1b: /* FMULX */
5957 case 0x1c: /* FCMEQ */
5958 case 0x1f: /* FRECPS */
5959 case 0x3f: /* FRSQRTS */
5960 case 0x5c: /* FCMGE */
5961 case 0x5d: /* FACGE */
Peter Maydell453ff1f2014-01-07 12:13:47 +00005962 case 0x7c: /* FCMGT */
5963 case 0x7d: /* FACGT */
5964 unsupported_encoding(s, insn);
5965 return;
Peter Maydell55bab512014-01-17 11:35:41 +00005966 case 0x7a: /* FABD */
5967 break;
Peter Maydell453ff1f2014-01-07 12:13:47 +00005968 default:
5969 unallocated_encoding(s);
5970 return;
5971 }
Peter Maydell55bab512014-01-17 11:35:41 +00005972
5973 handle_3same_float(s, extract32(size, 0, 1), 1, fpopcode, rd, rn, rm);
5974 return;
Peter Maydell453ff1f2014-01-07 12:13:47 +00005975 }
5976
5977 switch (opcode) {
5978 case 0x1: /* SQADD, UQADD */
5979 case 0x5: /* SQSUB, UQSUB */
5980 case 0x8: /* SSHL, USHL */
5981 case 0xa: /* SRSHL, URSHL */
5982 unsupported_encoding(s, insn);
5983 return;
5984 case 0x6: /* CMGT, CMHI */
5985 case 0x7: /* CMGE, CMHS */
5986 case 0x11: /* CMTST, CMEQ */
5987 case 0x10: /* ADD, SUB (vector) */
5988 if (size != 3) {
5989 unallocated_encoding(s);
5990 return;
5991 }
5992 break;
5993 case 0x9: /* SQSHL, UQSHL */
5994 case 0xb: /* SQRSHL, UQRSHL */
5995 unsupported_encoding(s, insn);
5996 return;
5997 case 0x16: /* SQDMULH, SQRDMULH (vector) */
5998 if (size != 1 && size != 2) {
5999 unallocated_encoding(s);
6000 return;
6001 }
6002 unsupported_encoding(s, insn);
6003 return;
6004 default:
6005 unallocated_encoding(s);
6006 return;
6007 }
6008
6009 tcg_rn = read_fp_dreg(s, rn); /* op1 */
6010 tcg_rm = read_fp_dreg(s, rm); /* op2 */
6011 tcg_rd = tcg_temp_new_i64();
6012
6013 /* For the moment we only support the opcodes which are
6014 * 64-bit-width only. The size != 3 cases will
6015 * be handled later when the relevant ops are implemented.
6016 */
6017 handle_3same_64(s, opcode, u, tcg_rd, tcg_rn, tcg_rm);
6018
6019 write_fp_dreg(s, rd, tcg_rd);
6020
6021 tcg_temp_free_i64(tcg_rn);
6022 tcg_temp_free_i64(tcg_rm);
6023 tcg_temp_free_i64(tcg_rd);
Alex Bennée94ad8702014-01-23 14:37:07 +00006024}
6025
6026/* C3.6.12 AdvSIMD scalar two reg misc
6027 * 31 30 29 28 24 23 22 21 17 16 12 11 10 9 5 4 0
6028 * +-----+---+-----------+------+-----------+--------+-----+------+------+
6029 * | 0 1 | U | 1 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 | Rn | Rd |
6030 * +-----+---+-----------+------+-----------+--------+-----+------+------+
6031 */
6032static void disas_simd_scalar_two_reg_misc(DisasContext *s, uint32_t insn)
6033{
6034 unsupported_encoding(s, insn);
6035}
6036
6037/* C3.6.13 AdvSIMD scalar x indexed element
6038 * 31 30 29 28 24 23 22 21 20 19 16 15 12 11 10 9 5 4 0
6039 * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
6040 * | 0 1 | U | 1 1 1 1 1 | size | L | M | Rm | opc | H | 0 | Rn | Rd |
6041 * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
6042 */
6043static void disas_simd_scalar_indexed(DisasContext *s, uint32_t insn)
6044{
6045 unsupported_encoding(s, insn);
6046}
6047
Alex Bennée4db2b1e2014-01-09 18:13:58 +00006048/* SSHR[RA]/USHR[RA] - Vector shift right (optional rounding/accumulate) */
6049static void handle_vec_simd_shri(DisasContext *s, bool is_q, bool is_u,
6050 int immh, int immb, int opcode, int rn, int rd)
6051{
6052 int size = 32 - clz32(immh) - 1;
6053 int immhb = immh << 3 | immb;
6054 int shift = 2 * (8 << size) - immhb;
6055 bool accumulate = false;
6056 bool round = false;
6057 int dsize = is_q ? 128 : 64;
6058 int esize = 8 << size;
6059 int elements = dsize/esize;
6060 TCGMemOp memop = size | (is_u ? 0 : MO_SIGN);
6061 TCGv_i64 tcg_rn = new_tmp_a64(s);
6062 TCGv_i64 tcg_rd = new_tmp_a64(s);
6063 TCGv_i64 tcg_round;
6064 int i;
6065
6066 if (extract32(immh, 3, 1) && !is_q) {
6067 unallocated_encoding(s);
6068 return;
6069 }
6070
6071 if (size > 3 && !is_q) {
6072 unallocated_encoding(s);
6073 return;
6074 }
6075
6076 switch (opcode) {
6077 case 0x02: /* SSRA / USRA (accumulate) */
6078 accumulate = true;
6079 break;
6080 case 0x04: /* SRSHR / URSHR (rounding) */
6081 round = true;
6082 break;
6083 case 0x06: /* SRSRA / URSRA (accum + rounding) */
6084 accumulate = round = true;
6085 break;
6086 }
6087
6088 if (round) {
6089 uint64_t round_const = 1ULL << (shift - 1);
6090 tcg_round = tcg_const_i64(round_const);
6091 } else {
6092 TCGV_UNUSED_I64(tcg_round);
6093 }
6094
6095 for (i = 0; i < elements; i++) {
6096 read_vec_element(s, tcg_rn, rn, i, memop);
6097 if (accumulate) {
6098 read_vec_element(s, tcg_rd, rd, i, memop);
6099 }
6100
6101 handle_shri_with_rndacc(tcg_rd, tcg_rn, tcg_round,
6102 accumulate, is_u, size, shift);
6103
6104 write_vec_element(s, tcg_rd, rd, i, size);
6105 }
6106
6107 if (!is_q) {
6108 clear_vec_high(s, rd);
6109 }
6110
6111 if (round) {
6112 tcg_temp_free_i64(tcg_round);
6113 }
6114}
6115
6116/* SHL/SLI - Vector shift left */
6117static void handle_vec_simd_shli(DisasContext *s, bool is_q, bool insert,
6118 int immh, int immb, int opcode, int rn, int rd)
6119{
6120 int size = 32 - clz32(immh) - 1;
6121 int immhb = immh << 3 | immb;
6122 int shift = immhb - (8 << size);
6123 int dsize = is_q ? 128 : 64;
6124 int esize = 8 << size;
6125 int elements = dsize/esize;
6126 TCGv_i64 tcg_rn = new_tmp_a64(s);
6127 TCGv_i64 tcg_rd = new_tmp_a64(s);
6128 int i;
6129
6130 if (extract32(immh, 3, 1) && !is_q) {
6131 unallocated_encoding(s);
6132 return;
6133 }
6134
6135 if (size > 3 && !is_q) {
6136 unallocated_encoding(s);
6137 return;
6138 }
6139
6140 for (i = 0; i < elements; i++) {
6141 read_vec_element(s, tcg_rn, rn, i, size);
6142 if (insert) {
6143 read_vec_element(s, tcg_rd, rd, i, size);
6144 }
6145
6146 handle_shli_with_ins(tcg_rd, tcg_rn, insert, shift);
6147
6148 write_vec_element(s, tcg_rd, rd, i, size);
6149 }
6150
6151 if (!is_q) {
6152 clear_vec_high(s, rd);
6153 }
6154}
6155
6156/* USHLL/SHLL - Vector shift left with widening */
6157static void handle_vec_simd_wshli(DisasContext *s, bool is_q, bool is_u,
6158 int immh, int immb, int opcode, int rn, int rd)
6159{
6160 int size = 32 - clz32(immh) - 1;
6161 int immhb = immh << 3 | immb;
6162 int shift = immhb - (8 << size);
6163 int dsize = 64;
6164 int esize = 8 << size;
6165 int elements = dsize/esize;
6166 TCGv_i64 tcg_rn = new_tmp_a64(s);
6167 TCGv_i64 tcg_rd = new_tmp_a64(s);
6168 int i;
6169
6170 if (size >= 3) {
6171 unallocated_encoding(s);
6172 return;
6173 }
6174
6175 /* For the LL variants the store is larger than the load,
6176 * so if rd == rn we would overwrite parts of our input.
6177 * So load everything right now and use shifts in the main loop.
6178 */
6179 read_vec_element(s, tcg_rn, rn, is_q ? 1 : 0, MO_64);
6180
6181 for (i = 0; i < elements; i++) {
6182 tcg_gen_shri_i64(tcg_rd, tcg_rn, i * esize);
6183 ext_and_shift_reg(tcg_rd, tcg_rd, size | (!is_u << 2), 0);
6184 tcg_gen_shli_i64(tcg_rd, tcg_rd, shift);
6185 write_vec_element(s, tcg_rd, rd, i, size + 1);
6186 }
6187}
6188
6189
Alex Bennée94ad8702014-01-23 14:37:07 +00006190/* C3.6.14 AdvSIMD shift by immediate
6191 * 31 30 29 28 23 22 19 18 16 15 11 10 9 5 4 0
6192 * +---+---+---+-------------+------+------+--------+---+------+------+
6193 * | 0 | Q | U | 0 1 1 1 1 0 | immh | immb | opcode | 1 | Rn | Rd |
6194 * +---+---+---+-------------+------+------+--------+---+------+------+
6195 */
6196static void disas_simd_shift_imm(DisasContext *s, uint32_t insn)
6197{
Alex Bennée4db2b1e2014-01-09 18:13:58 +00006198 int rd = extract32(insn, 0, 5);
6199 int rn = extract32(insn, 5, 5);
6200 int opcode = extract32(insn, 11, 5);
6201 int immb = extract32(insn, 16, 3);
6202 int immh = extract32(insn, 19, 4);
6203 bool is_u = extract32(insn, 29, 1);
6204 bool is_q = extract32(insn, 30, 1);
6205
6206 switch (opcode) {
6207 case 0x00: /* SSHR / USHR */
6208 case 0x02: /* SSRA / USRA (accumulate) */
6209 case 0x04: /* SRSHR / URSHR (rounding) */
6210 case 0x06: /* SRSRA / URSRA (accum + rounding) */
6211 handle_vec_simd_shri(s, is_q, is_u, immh, immb, opcode, rn, rd);
6212 break;
6213 case 0x0a: /* SHL / SLI */
6214 handle_vec_simd_shli(s, is_q, is_u, immh, immb, opcode, rn, rd);
6215 break;
6216 case 0x14: /* SSHLL / USHLL */
6217 handle_vec_simd_wshli(s, is_q, is_u, immh, immb, opcode, rn, rd);
6218 break;
6219 default:
6220 /* We don't currently implement any of the Narrow or saturating shifts;
6221 * nor do we implement the fixed-point conversions in this
6222 * encoding group (SCVTF, FCVTZS, UCVTF, FCVTZU).
6223 */
6224 unsupported_encoding(s, insn);
6225 return;
6226 }
Alex Bennée94ad8702014-01-23 14:37:07 +00006227}
6228
Peter Maydellac224302014-01-14 12:44:38 +00006229static void handle_3rd_widening(DisasContext *s, int is_q, int is_u, int size,
6230 int opcode, int rd, int rn, int rm)
6231{
6232 /* 3-reg-different widening insns: 64 x 64 -> 128 */
6233 TCGv_i64 tcg_res[2];
6234 int pass, accop;
6235
6236 tcg_res[0] = tcg_temp_new_i64();
6237 tcg_res[1] = tcg_temp_new_i64();
6238
6239 /* Does this op do an adding accumulate, a subtracting accumulate,
6240 * or no accumulate at all?
6241 */
6242 switch (opcode) {
6243 case 5:
6244 case 8:
6245 case 9:
6246 accop = 1;
6247 break;
6248 case 10:
6249 case 11:
6250 accop = -1;
6251 break;
6252 default:
6253 accop = 0;
6254 break;
6255 }
6256
6257 if (accop != 0) {
6258 read_vec_element(s, tcg_res[0], rd, 0, MO_64);
6259 read_vec_element(s, tcg_res[1], rd, 1, MO_64);
6260 }
6261
6262 /* size == 2 means two 32x32->64 operations; this is worth special
6263 * casing because we can generally handle it inline.
6264 */
6265 if (size == 2) {
6266 for (pass = 0; pass < 2; pass++) {
6267 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
6268 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
6269 TCGv_i64 tcg_passres;
6270 TCGMemOp memop = MO_32 | (is_u ? 0 : MO_SIGN);
6271
6272 int elt = pass + is_q * 2;
6273
6274 read_vec_element(s, tcg_op1, rn, elt, memop);
6275 read_vec_element(s, tcg_op2, rm, elt, memop);
6276
6277 if (accop == 0) {
6278 tcg_passres = tcg_res[pass];
6279 } else {
6280 tcg_passres = tcg_temp_new_i64();
6281 }
6282
6283 switch (opcode) {
Peter Maydellca8ab582014-01-14 12:44:38 +00006284 case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
6285 case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
6286 {
6287 TCGv_i64 tcg_tmp1 = tcg_temp_new_i64();
6288 TCGv_i64 tcg_tmp2 = tcg_temp_new_i64();
6289
6290 tcg_gen_sub_i64(tcg_tmp1, tcg_op1, tcg_op2);
6291 tcg_gen_sub_i64(tcg_tmp2, tcg_op2, tcg_op1);
6292 tcg_gen_movcond_i64(is_u ? TCG_COND_GEU : TCG_COND_GE,
6293 tcg_passres,
6294 tcg_op1, tcg_op2, tcg_tmp1, tcg_tmp2);
6295 tcg_temp_free_i64(tcg_tmp1);
6296 tcg_temp_free_i64(tcg_tmp2);
6297 break;
6298 }
Peter Maydellac224302014-01-14 12:44:38 +00006299 case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
6300 case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
6301 case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
6302 tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
6303 break;
6304 default:
6305 g_assert_not_reached();
6306 }
6307
6308 if (accop > 0) {
6309 tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
6310 tcg_temp_free_i64(tcg_passres);
6311 } else if (accop < 0) {
6312 tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
6313 tcg_temp_free_i64(tcg_passres);
6314 }
6315
6316 tcg_temp_free_i64(tcg_op1);
6317 tcg_temp_free_i64(tcg_op2);
6318 }
6319 } else {
6320 /* size 0 or 1, generally helper functions */
6321 for (pass = 0; pass < 2; pass++) {
6322 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
6323 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
6324 TCGv_i64 tcg_passres;
6325 int elt = pass + is_q * 2;
6326
6327 read_vec_element_i32(s, tcg_op1, rn, elt, MO_32);
6328 read_vec_element_i32(s, tcg_op2, rm, elt, MO_32);
6329
6330 if (accop == 0) {
6331 tcg_passres = tcg_res[pass];
6332 } else {
6333 tcg_passres = tcg_temp_new_i64();
6334 }
6335
6336 switch (opcode) {
Peter Maydellca8ab582014-01-14 12:44:38 +00006337 case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
6338 case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
6339 if (size == 0) {
6340 if (is_u) {
6341 gen_helper_neon_abdl_u16(tcg_passres, tcg_op1, tcg_op2);
6342 } else {
6343 gen_helper_neon_abdl_s16(tcg_passres, tcg_op1, tcg_op2);
6344 }
6345 } else {
6346 if (is_u) {
6347 gen_helper_neon_abdl_u32(tcg_passres, tcg_op1, tcg_op2);
6348 } else {
6349 gen_helper_neon_abdl_s32(tcg_passres, tcg_op1, tcg_op2);
6350 }
6351 }
6352 break;
Peter Maydellac224302014-01-14 12:44:38 +00006353 case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
6354 case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
6355 case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
6356 if (size == 0) {
6357 if (is_u) {
6358 gen_helper_neon_mull_u8(tcg_passres, tcg_op1, tcg_op2);
6359 } else {
6360 gen_helper_neon_mull_s8(tcg_passres, tcg_op1, tcg_op2);
6361 }
6362 } else {
6363 if (is_u) {
6364 gen_helper_neon_mull_u16(tcg_passres, tcg_op1, tcg_op2);
6365 } else {
6366 gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
6367 }
6368 }
6369 break;
6370 default:
6371 g_assert_not_reached();
6372 }
6373 tcg_temp_free_i32(tcg_op1);
6374 tcg_temp_free_i32(tcg_op2);
6375
6376 if (accop > 0) {
6377 if (size == 0) {
6378 gen_helper_neon_addl_u16(tcg_res[pass], tcg_res[pass],
6379 tcg_passres);
6380 } else {
6381 gen_helper_neon_addl_u32(tcg_res[pass], tcg_res[pass],
6382 tcg_passres);
6383 }
6384 tcg_temp_free_i64(tcg_passres);
6385 } else if (accop < 0) {
6386 if (size == 0) {
6387 gen_helper_neon_subl_u16(tcg_res[pass], tcg_res[pass],
6388 tcg_passres);
6389 } else {
6390 gen_helper_neon_subl_u32(tcg_res[pass], tcg_res[pass],
6391 tcg_passres);
6392 }
6393 tcg_temp_free_i64(tcg_passres);
6394 }
6395 }
6396 }
6397
6398 write_vec_element(s, tcg_res[0], rd, 0, MO_64);
6399 write_vec_element(s, tcg_res[1], rd, 1, MO_64);
6400 tcg_temp_free_i64(tcg_res[0]);
6401 tcg_temp_free_i64(tcg_res[1]);
6402}
6403
Alex Bennée94ad8702014-01-23 14:37:07 +00006404/* C3.6.15 AdvSIMD three different
6405 * 31 30 29 28 24 23 22 21 20 16 15 12 11 10 9 5 4 0
6406 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
6407 * | 0 | Q | U | 0 1 1 1 0 | size | 1 | Rm | opcode | 0 0 | Rn | Rd |
6408 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
6409 */
6410static void disas_simd_three_reg_diff(DisasContext *s, uint32_t insn)
6411{
Peter Maydellac224302014-01-14 12:44:38 +00006412 /* Instructions in this group fall into three basic classes
6413 * (in each case with the operation working on each element in
6414 * the input vectors):
6415 * (1) widening 64 x 64 -> 128 (with possibly Vd as an extra
6416 * 128 bit input)
6417 * (2) wide 64 x 128 -> 128
6418 * (3) narrowing 128 x 128 -> 64
6419 * Here we do initial decode, catch unallocated cases and
6420 * dispatch to separate functions for each class.
6421 */
6422 int is_q = extract32(insn, 30, 1);
6423 int is_u = extract32(insn, 29, 1);
6424 int size = extract32(insn, 22, 2);
6425 int opcode = extract32(insn, 12, 4);
6426 int rm = extract32(insn, 16, 5);
6427 int rn = extract32(insn, 5, 5);
6428 int rd = extract32(insn, 0, 5);
6429
6430 switch (opcode) {
6431 case 1: /* SADDW, SADDW2, UADDW, UADDW2 */
6432 case 3: /* SSUBW, SSUBW2, USUBW, USUBW2 */
6433 /* 64 x 128 -> 128 */
6434 unsupported_encoding(s, insn);
6435 break;
6436 case 4: /* ADDHN, ADDHN2, RADDHN, RADDHN2 */
6437 case 6: /* SUBHN, SUBHN2, RSUBHN, RSUBHN2 */
6438 /* 128 x 128 -> 64 */
6439 unsupported_encoding(s, insn);
6440 break;
6441 case 9:
6442 case 11:
6443 case 13:
6444 case 14:
6445 if (is_u) {
6446 unallocated_encoding(s);
6447 return;
6448 }
6449 /* fall through */
6450 case 0:
6451 case 2:
Peter Maydellac224302014-01-14 12:44:38 +00006452 unsupported_encoding(s, insn);
6453 break;
Peter Maydellca8ab582014-01-14 12:44:38 +00006454 case 5:
6455 case 7:
Peter Maydellac224302014-01-14 12:44:38 +00006456 case 8:
6457 case 10:
6458 case 12:
6459 /* 64 x 64 -> 128 */
6460 if (size == 3) {
6461 unallocated_encoding(s);
6462 return;
6463 }
6464 handle_3rd_widening(s, is_q, is_u, size, opcode, rd, rn, rm);
6465 break;
6466 default:
6467 /* opcode 15 not allocated */
6468 unallocated_encoding(s);
6469 break;
6470 }
Alex Bennée94ad8702014-01-23 14:37:07 +00006471}
6472
Peter Maydellaab820f2014-01-16 13:00:47 +00006473/* Logic op (opcode == 3) subgroup of C3.6.16. */
6474static void disas_simd_3same_logic(DisasContext *s, uint32_t insn)
6475{
Peter Maydell75415722014-01-23 13:19:00 +00006476 int rd = extract32(insn, 0, 5);
6477 int rn = extract32(insn, 5, 5);
6478 int rm = extract32(insn, 16, 5);
6479 int size = extract32(insn, 22, 2);
6480 bool is_u = extract32(insn, 29, 1);
6481 bool is_q = extract32(insn, 30, 1);
6482 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
6483 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
6484 TCGv_i64 tcg_res[2];
6485 int pass;
6486
6487 tcg_res[0] = tcg_temp_new_i64();
6488 tcg_res[1] = tcg_temp_new_i64();
6489
6490 for (pass = 0; pass < (is_q ? 2 : 1); pass++) {
6491 read_vec_element(s, tcg_op1, rn, pass, MO_64);
6492 read_vec_element(s, tcg_op2, rm, pass, MO_64);
6493
6494 if (!is_u) {
6495 switch (size) {
6496 case 0: /* AND */
6497 tcg_gen_and_i64(tcg_res[pass], tcg_op1, tcg_op2);
6498 break;
6499 case 1: /* BIC */
6500 tcg_gen_andc_i64(tcg_res[pass], tcg_op1, tcg_op2);
6501 break;
6502 case 2: /* ORR */
6503 tcg_gen_or_i64(tcg_res[pass], tcg_op1, tcg_op2);
6504 break;
6505 case 3: /* ORN */
6506 tcg_gen_orc_i64(tcg_res[pass], tcg_op1, tcg_op2);
6507 break;
6508 }
6509 } else {
6510 if (size != 0) {
6511 /* B* ops need res loaded to operate on */
6512 read_vec_element(s, tcg_res[pass], rd, pass, MO_64);
6513 }
6514
6515 switch (size) {
6516 case 0: /* EOR */
6517 tcg_gen_xor_i64(tcg_res[pass], tcg_op1, tcg_op2);
6518 break;
6519 case 1: /* BSL bitwise select */
6520 tcg_gen_xor_i64(tcg_op1, tcg_op1, tcg_op2);
6521 tcg_gen_and_i64(tcg_op1, tcg_op1, tcg_res[pass]);
6522 tcg_gen_xor_i64(tcg_res[pass], tcg_op2, tcg_op1);
6523 break;
6524 case 2: /* BIT, bitwise insert if true */
6525 tcg_gen_xor_i64(tcg_op1, tcg_op1, tcg_res[pass]);
6526 tcg_gen_and_i64(tcg_op1, tcg_op1, tcg_op2);
6527 tcg_gen_xor_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
6528 break;
6529 case 3: /* BIF, bitwise insert if false */
6530 tcg_gen_xor_i64(tcg_op1, tcg_op1, tcg_res[pass]);
6531 tcg_gen_andc_i64(tcg_op1, tcg_op1, tcg_op2);
6532 tcg_gen_xor_i64(tcg_res[pass], tcg_res[pass], tcg_op1);
6533 break;
6534 }
6535 }
6536 }
6537
6538 write_vec_element(s, tcg_res[0], rd, 0, MO_64);
6539 if (!is_q) {
6540 tcg_gen_movi_i64(tcg_res[1], 0);
6541 }
6542 write_vec_element(s, tcg_res[1], rd, 1, MO_64);
6543
6544 tcg_temp_free_i64(tcg_op1);
6545 tcg_temp_free_i64(tcg_op2);
6546 tcg_temp_free_i64(tcg_res[0]);
6547 tcg_temp_free_i64(tcg_res[1]);
Peter Maydellaab820f2014-01-16 13:00:47 +00006548}
6549
6550/* Pairwise op subgroup of C3.6.16. */
6551static void disas_simd_3same_pair(DisasContext *s, uint32_t insn)
6552{
6553 unsupported_encoding(s, insn);
6554}
6555
6556/* Floating point op subgroup of C3.6.16. */
6557static void disas_simd_3same_float(DisasContext *s, uint32_t insn)
6558{
Peter Maydell55bab512014-01-17 11:35:41 +00006559 /* For floating point ops, the U, size[1] and opcode bits
6560 * together indicate the operation. size[0] indicates single
6561 * or double.
6562 */
6563 int fpopcode = extract32(insn, 11, 5)
6564 | (extract32(insn, 23, 1) << 5)
6565 | (extract32(insn, 29, 1) << 6);
6566 int is_q = extract32(insn, 30, 1);
6567 int size = extract32(insn, 22, 1);
6568 int rm = extract32(insn, 16, 5);
6569 int rn = extract32(insn, 5, 5);
6570 int rd = extract32(insn, 0, 5);
6571
6572 int datasize = is_q ? 128 : 64;
6573 int esize = 32 << size;
6574 int elements = datasize / esize;
6575
6576 if (size == 1 && !is_q) {
6577 unallocated_encoding(s);
6578 return;
6579 }
6580
6581 switch (fpopcode) {
6582 case 0x58: /* FMAXNMP */
6583 case 0x5a: /* FADDP */
6584 case 0x5e: /* FMAXP */
6585 case 0x78: /* FMINNMP */
6586 case 0x7e: /* FMINP */
6587 /* pairwise ops */
6588 unsupported_encoding(s, insn);
6589 return;
6590 case 0x1b: /* FMULX */
6591 case 0x1c: /* FCMEQ */
6592 case 0x1f: /* FRECPS */
6593 case 0x3f: /* FRSQRTS */
6594 case 0x5c: /* FCMGE */
6595 case 0x5d: /* FACGE */
6596 case 0x7c: /* FCMGT */
6597 case 0x7d: /* FACGT */
6598 case 0x19: /* FMLA */
6599 case 0x39: /* FMLS */
6600 unsupported_encoding(s, insn);
6601 return;
6602 case 0x18: /* FMAXNM */
6603 case 0x1a: /* FADD */
6604 case 0x1e: /* FMAX */
6605 case 0x38: /* FMINNM */
6606 case 0x3a: /* FSUB */
6607 case 0x3e: /* FMIN */
6608 case 0x5b: /* FMUL */
6609 case 0x5f: /* FDIV */
6610 case 0x7a: /* FABD */
6611 handle_3same_float(s, size, elements, fpopcode, rd, rn, rm);
6612 return;
6613 default:
6614 unallocated_encoding(s);
6615 return;
6616 }
Peter Maydellaab820f2014-01-16 13:00:47 +00006617}
6618
6619/* Integer op subgroup of C3.6.16. */
6620static void disas_simd_3same_int(DisasContext *s, uint32_t insn)
6621{
Peter Maydell0d19b492014-01-23 13:13:41 +00006622 int is_q = extract32(insn, 30, 1);
6623 int u = extract32(insn, 29, 1);
6624 int size = extract32(insn, 22, 2);
6625 int opcode = extract32(insn, 11, 5);
6626 int rm = extract32(insn, 16, 5);
6627 int rn = extract32(insn, 5, 5);
6628 int rd = extract32(insn, 0, 5);
6629 int pass;
6630
6631 switch (opcode) {
6632 case 0x13: /* MUL, PMUL */
6633 if (u && size != 0) {
6634 unallocated_encoding(s);
6635 return;
6636 }
6637 /* fall through */
6638 case 0x0: /* SHADD, UHADD */
6639 case 0x2: /* SRHADD, URHADD */
6640 case 0x4: /* SHSUB, UHSUB */
6641 case 0xc: /* SMAX, UMAX */
6642 case 0xd: /* SMIN, UMIN */
6643 case 0xe: /* SABD, UABD */
6644 case 0xf: /* SABA, UABA */
6645 case 0x12: /* MLA, MLS */
6646 if (size == 3) {
6647 unallocated_encoding(s);
6648 return;
6649 }
6650 unsupported_encoding(s, insn);
6651 return;
6652 case 0x1: /* SQADD */
6653 case 0x5: /* SQSUB */
6654 case 0x8: /* SSHL, USHL */
6655 case 0x9: /* SQSHL, UQSHL */
6656 case 0xa: /* SRSHL, URSHL */
6657 case 0xb: /* SQRSHL, UQRSHL */
6658 if (size == 3 && !is_q) {
6659 unallocated_encoding(s);
6660 return;
6661 }
6662 unsupported_encoding(s, insn);
6663 return;
6664 case 0x16: /* SQDMULH, SQRDMULH */
6665 if (size == 0 || size == 3) {
6666 unallocated_encoding(s);
6667 return;
6668 }
6669 unsupported_encoding(s, insn);
6670 return;
6671 default:
6672 if (size == 3 && !is_q) {
6673 unallocated_encoding(s);
6674 return;
6675 }
6676 break;
6677 }
6678
6679 if (size == 3) {
6680 for (pass = 0; pass < (is_q ? 2 : 1); pass++) {
6681 TCGv_i64 tcg_op1 = tcg_temp_new_i64();
6682 TCGv_i64 tcg_op2 = tcg_temp_new_i64();
6683 TCGv_i64 tcg_res = tcg_temp_new_i64();
6684
6685 read_vec_element(s, tcg_op1, rn, pass, MO_64);
6686 read_vec_element(s, tcg_op2, rm, pass, MO_64);
6687
6688 handle_3same_64(s, opcode, u, tcg_res, tcg_op1, tcg_op2);
6689
6690 write_vec_element(s, tcg_res, rd, pass, MO_64);
6691
6692 tcg_temp_free_i64(tcg_res);
6693 tcg_temp_free_i64(tcg_op1);
6694 tcg_temp_free_i64(tcg_op2);
6695 }
6696 } else {
6697 for (pass = 0; pass < (is_q ? 4 : 2); pass++) {
6698 TCGv_i32 tcg_op1 = tcg_temp_new_i32();
6699 TCGv_i32 tcg_op2 = tcg_temp_new_i32();
6700 TCGv_i32 tcg_res = tcg_temp_new_i32();
6701 NeonGenTwoOpFn *genfn;
6702
6703 read_vec_element_i32(s, tcg_op1, rn, pass, MO_32);
6704 read_vec_element_i32(s, tcg_op2, rm, pass, MO_32);
6705
6706 switch (opcode) {
6707 case 0x6: /* CMGT, CMHI */
6708 {
6709 static NeonGenTwoOpFn * const fns[3][2] = {
6710 { gen_helper_neon_cgt_s8, gen_helper_neon_cgt_u8 },
6711 { gen_helper_neon_cgt_s16, gen_helper_neon_cgt_u16 },
6712 { gen_helper_neon_cgt_s32, gen_helper_neon_cgt_u32 },
6713 };
6714 genfn = fns[size][u];
6715 break;
6716 }
6717 case 0x7: /* CMGE, CMHS */
6718 {
6719 static NeonGenTwoOpFn * const fns[3][2] = {
6720 { gen_helper_neon_cge_s8, gen_helper_neon_cge_u8 },
6721 { gen_helper_neon_cge_s16, gen_helper_neon_cge_u16 },
6722 { gen_helper_neon_cge_s32, gen_helper_neon_cge_u32 },
6723 };
6724 genfn = fns[size][u];
6725 break;
6726 }
6727 case 0x10: /* ADD, SUB */
6728 {
6729 static NeonGenTwoOpFn * const fns[3][2] = {
6730 { gen_helper_neon_add_u8, gen_helper_neon_sub_u8 },
6731 { gen_helper_neon_add_u16, gen_helper_neon_sub_u16 },
6732 { tcg_gen_add_i32, tcg_gen_sub_i32 },
6733 };
6734 genfn = fns[size][u];
6735 break;
6736 }
6737 case 0x11: /* CMTST, CMEQ */
6738 {
6739 static NeonGenTwoOpFn * const fns[3][2] = {
6740 { gen_helper_neon_tst_u8, gen_helper_neon_ceq_u8 },
6741 { gen_helper_neon_tst_u16, gen_helper_neon_ceq_u16 },
6742 { gen_helper_neon_tst_u32, gen_helper_neon_ceq_u32 },
6743 };
6744 genfn = fns[size][u];
6745 break;
6746 }
6747 default:
6748 g_assert_not_reached();
6749 }
6750
6751 genfn(tcg_res, tcg_op1, tcg_op2);
6752
6753 write_vec_element_i32(s, tcg_res, rd, pass, MO_32);
6754
6755 tcg_temp_free_i32(tcg_res);
6756 tcg_temp_free_i32(tcg_op1);
6757 tcg_temp_free_i32(tcg_op2);
6758 }
6759 }
6760
6761 if (!is_q) {
6762 clear_vec_high(s, rd);
6763 }
Peter Maydellaab820f2014-01-16 13:00:47 +00006764}
6765
Alex Bennée94ad8702014-01-23 14:37:07 +00006766/* C3.6.16 AdvSIMD three same
6767 * 31 30 29 28 24 23 22 21 20 16 15 11 10 9 5 4 0
6768 * +---+---+---+-----------+------+---+------+--------+---+------+------+
6769 * | 0 | Q | U | 0 1 1 1 0 | size | 1 | Rm | opcode | 1 | Rn | Rd |
6770 * +---+---+---+-----------+------+---+------+--------+---+------+------+
6771 */
6772static void disas_simd_three_reg_same(DisasContext *s, uint32_t insn)
6773{
Peter Maydellaab820f2014-01-16 13:00:47 +00006774 int opcode = extract32(insn, 11, 5);
6775
6776 switch (opcode) {
6777 case 0x3: /* logic ops */
6778 disas_simd_3same_logic(s, insn);
6779 break;
6780 case 0x17: /* ADDP */
6781 case 0x14: /* SMAXP, UMAXP */
6782 case 0x15: /* SMINP, UMINP */
6783 /* Pairwise operations */
6784 disas_simd_3same_pair(s, insn);
6785 break;
6786 case 0x18 ... 0x31:
6787 /* floating point ops, sz[1] and U are part of opcode */
6788 disas_simd_3same_float(s, insn);
6789 break;
6790 default:
6791 disas_simd_3same_int(s, insn);
6792 break;
6793 }
Alex Bennée94ad8702014-01-23 14:37:07 +00006794}
6795
6796/* C3.6.17 AdvSIMD two reg misc
6797 * 31 30 29 28 24 23 22 21 17 16 12 11 10 9 5 4 0
6798 * +---+---+---+-----------+------+-----------+--------+-----+------+------+
6799 * | 0 | Q | U | 0 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 | Rn | Rd |
6800 * +---+---+---+-----------+------+-----------+--------+-----+------+------+
6801 */
6802static void disas_simd_two_reg_misc(DisasContext *s, uint32_t insn)
6803{
6804 unsupported_encoding(s, insn);
6805}
6806
6807/* C3.6.18 AdvSIMD vector x indexed element
6808 * 31 30 29 28 24 23 22 21 20 19 16 15 12 11 10 9 5 4 0
6809 * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
6810 * | 0 | Q | U | 0 1 1 1 1 | size | L | M | Rm | opc | H | 0 | Rn | Rd |
6811 * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
6812 */
6813static void disas_simd_indexed_vector(DisasContext *s, uint32_t insn)
6814{
6815 unsupported_encoding(s, insn);
6816}
6817
6818/* C3.6.19 Crypto AES
6819 * 31 24 23 22 21 17 16 12 11 10 9 5 4 0
6820 * +-----------------+------+-----------+--------+-----+------+------+
6821 * | 0 1 0 0 1 1 1 0 | size | 1 0 1 0 0 | opcode | 1 0 | Rn | Rd |
6822 * +-----------------+------+-----------+--------+-----+------+------+
6823 */
6824static void disas_crypto_aes(DisasContext *s, uint32_t insn)
6825{
6826 unsupported_encoding(s, insn);
6827}
6828
6829/* C3.6.20 Crypto three-reg SHA
6830 * 31 24 23 22 21 20 16 15 14 12 11 10 9 5 4 0
6831 * +-----------------+------+---+------+---+--------+-----+------+------+
6832 * | 0 1 0 1 1 1 1 0 | size | 0 | Rm | 0 | opcode | 0 0 | Rn | Rd |
6833 * +-----------------+------+---+------+---+--------+-----+------+------+
6834 */
6835static void disas_crypto_three_reg_sha(DisasContext *s, uint32_t insn)
6836{
6837 unsupported_encoding(s, insn);
6838}
6839
6840/* C3.6.21 Crypto two-reg SHA
6841 * 31 24 23 22 21 17 16 12 11 10 9 5 4 0
6842 * +-----------------+------+-----------+--------+-----+------+------+
6843 * | 0 1 0 1 1 1 1 0 | size | 1 0 1 0 0 | opcode | 1 0 | Rn | Rd |
6844 * +-----------------+------+-----------+--------+-----+------+------+
6845 */
6846static void disas_crypto_two_reg_sha(DisasContext *s, uint32_t insn)
6847{
6848 unsupported_encoding(s, insn);
6849}
6850
6851/* C3.6 Data processing - SIMD, inc Crypto
6852 *
6853 * As the decode gets a little complex we are using a table based
6854 * approach for this part of the decode.
6855 */
6856static const AArch64DecodeTable data_proc_simd[] = {
6857 /* pattern , mask , fn */
6858 { 0x0e200400, 0x9f200400, disas_simd_three_reg_same },
6859 { 0x0e200000, 0x9f200c00, disas_simd_three_reg_diff },
6860 { 0x0e200800, 0x9f3e0c00, disas_simd_two_reg_misc },
6861 { 0x0e300800, 0x9f3e0c00, disas_simd_across_lanes },
6862 { 0x0e000400, 0x9fe08400, disas_simd_copy },
6863 { 0x0f000000, 0x9f000400, disas_simd_indexed_vector },
6864 /* simd_mod_imm decode is a subset of simd_shift_imm, so must precede it */
6865 { 0x0f000400, 0x9ff80400, disas_simd_mod_imm },
6866 { 0x0f000400, 0x9f800400, disas_simd_shift_imm },
6867 { 0x0e000000, 0xbf208c00, disas_simd_tb },
6868 { 0x0e000800, 0xbf208c00, disas_simd_zip_trn },
6869 { 0x2e000000, 0xbf208400, disas_simd_ext },
6870 { 0x5e200400, 0xdf200400, disas_simd_scalar_three_reg_same },
6871 { 0x5e200000, 0xdf200c00, disas_simd_scalar_three_reg_diff },
6872 { 0x5e200800, 0xdf3e0c00, disas_simd_scalar_two_reg_misc },
6873 { 0x5e300800, 0xdf3e0c00, disas_simd_scalar_pairwise },
6874 { 0x5e000400, 0xdfe08400, disas_simd_scalar_copy },
6875 { 0x5f000000, 0xdf000400, disas_simd_scalar_indexed },
6876 { 0x5f000400, 0xdf800400, disas_simd_scalar_shift_imm },
6877 { 0x4e280800, 0xff3e0c00, disas_crypto_aes },
6878 { 0x5e000000, 0xff208c00, disas_crypto_three_reg_sha },
6879 { 0x5e280800, 0xff3e0c00, disas_crypto_two_reg_sha },
6880 { 0x00000000, 0x00000000, NULL }
6881};
6882
Peter Maydellfaa0ba42013-12-23 23:27:30 +00006883static void disas_data_proc_simd(DisasContext *s, uint32_t insn)
6884{
6885 /* Note that this is called with all non-FP cases from
6886 * table C3-6 so it must UNDEF for entries not specifically
6887 * allocated to instructions in that table.
6888 */
Alex Bennée94ad8702014-01-23 14:37:07 +00006889 AArch64DecodeFn *fn = lookup_disas_fn(&data_proc_simd[0], insn);
6890 if (fn) {
6891 fn(s, insn);
6892 } else {
6893 unallocated_encoding(s);
6894 }
Peter Maydellfaa0ba42013-12-23 23:27:30 +00006895}
6896
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00006897/* C3.6 Data processing - SIMD and floating point */
6898static void disas_data_proc_simd_fp(DisasContext *s, uint32_t insn)
6899{
Peter Maydellfaa0ba42013-12-23 23:27:30 +00006900 if (extract32(insn, 28, 1) == 1 && extract32(insn, 30, 1) == 0) {
6901 disas_data_proc_fp(s, insn);
6902 } else {
6903 /* SIMD, including crypto */
6904 disas_data_proc_simd(s, insn);
6905 }
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00006906}
6907
6908/* C3.1 A64 instruction index by encoding */
Peter Maydell40f860c2013-12-17 19:42:31 +00006909static void disas_a64_insn(CPUARMState *env, DisasContext *s)
Alexander Graf14ade102013-09-03 20:12:10 +01006910{
6911 uint32_t insn;
6912
6913 insn = arm_ldl_code(env, s->pc, s->bswap_code);
6914 s->insn = insn;
6915 s->pc += 4;
6916
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00006917 switch (extract32(insn, 25, 4)) {
6918 case 0x0: case 0x1: case 0x2: case 0x3: /* UNALLOCATED */
Alexander Graf14ade102013-09-03 20:12:10 +01006919 unallocated_encoding(s);
6920 break;
Claudio Fontanaad7ee8a2013-12-17 19:42:32 +00006921 case 0x8: case 0x9: /* Data processing - immediate */
6922 disas_data_proc_imm(s, insn);
6923 break;
6924 case 0xa: case 0xb: /* Branch, exception generation and system insns */
6925 disas_b_exc_sys(s, insn);
6926 break;
6927 case 0x4:
6928 case 0x6:
6929 case 0xc:
6930 case 0xe: /* Loads and stores */
6931 disas_ldst(s, insn);
6932 break;
6933 case 0x5:
6934 case 0xd: /* Data processing - register */
6935 disas_data_proc_reg(s, insn);
6936 break;
6937 case 0x7:
6938 case 0xf: /* Data processing - SIMD and floating point */
6939 disas_data_proc_simd_fp(s, insn);
6940 break;
6941 default:
6942 assert(FALSE); /* all 15 cases should be handled above */
6943 break;
Alexander Graf14ade102013-09-03 20:12:10 +01006944 }
Alexander Graf11e169d2013-12-17 19:42:32 +00006945
6946 /* if we allocated any temporaries, free them here */
6947 free_tmp_a64(s);
Peter Maydell40f860c2013-12-17 19:42:31 +00006948}
Alexander Graf14ade102013-09-03 20:12:10 +01006949
Peter Maydell40f860c2013-12-17 19:42:31 +00006950void gen_intermediate_code_internal_a64(ARMCPU *cpu,
6951 TranslationBlock *tb,
6952 bool search_pc)
6953{
6954 CPUState *cs = CPU(cpu);
6955 CPUARMState *env = &cpu->env;
6956 DisasContext dc1, *dc = &dc1;
6957 CPUBreakpoint *bp;
6958 uint16_t *gen_opc_end;
6959 int j, lj;
6960 target_ulong pc_start;
6961 target_ulong next_page_start;
6962 int num_insns;
6963 int max_insns;
6964
6965 pc_start = tb->pc;
6966
6967 dc->tb = tb;
6968
6969 gen_opc_end = tcg_ctx.gen_opc_buf + OPC_MAX_SIZE;
6970
6971 dc->is_jmp = DISAS_NEXT;
6972 dc->pc = pc_start;
6973 dc->singlestep_enabled = cs->singlestep_enabled;
6974 dc->condjmp = 0;
6975
6976 dc->aarch64 = 1;
6977 dc->thumb = 0;
6978 dc->bswap_code = 0;
6979 dc->condexec_mask = 0;
6980 dc->condexec_cond = 0;
6981#if !defined(CONFIG_USER_ONLY)
6982 dc->user = 0;
6983#endif
6984 dc->vfp_enabled = 0;
6985 dc->vec_len = 0;
6986 dc->vec_stride = 0;
Peter Maydell60322b32014-01-04 22:15:44 +00006987 dc->cp_regs = cpu->cp_regs;
6988 dc->current_pl = arm_current_pl(env);
Peter Maydell40f860c2013-12-17 19:42:31 +00006989
Alexander Graf11e169d2013-12-17 19:42:32 +00006990 init_tmp_a64_array(dc);
6991
Peter Maydell40f860c2013-12-17 19:42:31 +00006992 next_page_start = (pc_start & TARGET_PAGE_MASK) + TARGET_PAGE_SIZE;
6993 lj = -1;
6994 num_insns = 0;
6995 max_insns = tb->cflags & CF_COUNT_MASK;
6996 if (max_insns == 0) {
6997 max_insns = CF_COUNT_MASK;
6998 }
6999
7000 gen_tb_start();
7001
7002 tcg_clear_temp_count();
7003
7004 do {
7005 if (unlikely(!QTAILQ_EMPTY(&env->breakpoints))) {
7006 QTAILQ_FOREACH(bp, &env->breakpoints, entry) {
7007 if (bp->pc == dc->pc) {
7008 gen_exception_insn(dc, 0, EXCP_DEBUG);
7009 /* Advance PC so that clearing the breakpoint will
7010 invalidate this TB. */
7011 dc->pc += 2;
7012 goto done_generating;
7013 }
7014 }
7015 }
7016
7017 if (search_pc) {
7018 j = tcg_ctx.gen_opc_ptr - tcg_ctx.gen_opc_buf;
7019 if (lj < j) {
7020 lj++;
7021 while (lj < j) {
7022 tcg_ctx.gen_opc_instr_start[lj++] = 0;
7023 }
7024 }
7025 tcg_ctx.gen_opc_pc[lj] = dc->pc;
7026 tcg_ctx.gen_opc_instr_start[lj] = 1;
7027 tcg_ctx.gen_opc_icount[lj] = num_insns;
7028 }
7029
7030 if (num_insns + 1 == max_insns && (tb->cflags & CF_LAST_IO)) {
7031 gen_io_start();
7032 }
7033
7034 if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP | CPU_LOG_TB_OP_OPT))) {
7035 tcg_gen_debug_insn_start(dc->pc);
7036 }
7037
7038 disas_a64_insn(env, dc);
7039
7040 if (tcg_check_temp_count()) {
7041 fprintf(stderr, "TCG temporary leak before "TARGET_FMT_lx"\n",
7042 dc->pc);
7043 }
7044
7045 /* Translation stops when a conditional branch is encountered.
7046 * Otherwise the subsequent code could get translated several times.
7047 * Also stop translation when a page boundary is reached. This
7048 * ensures prefetch aborts occur at the right place.
7049 */
7050 num_insns++;
7051 } while (!dc->is_jmp && tcg_ctx.gen_opc_ptr < gen_opc_end &&
7052 !cs->singlestep_enabled &&
7053 !singlestep &&
7054 dc->pc < next_page_start &&
7055 num_insns < max_insns);
7056
7057 if (tb->cflags & CF_LAST_IO) {
7058 gen_io_end();
7059 }
7060
7061 if (unlikely(cs->singlestep_enabled) && dc->is_jmp != DISAS_EXC) {
7062 /* Note that this means single stepping WFI doesn't halt the CPU.
7063 * For conditional branch insns this is harmless unreachable code as
7064 * gen_goto_tb() has already handled emitting the debug exception
7065 * (and thus a tb-jump is not possible when singlestepping).
7066 */
7067 assert(dc->is_jmp != DISAS_TB_JUMP);
7068 if (dc->is_jmp != DISAS_JUMP) {
7069 gen_a64_set_pc_im(dc->pc);
7070 }
7071 gen_exception(EXCP_DEBUG);
7072 } else {
7073 switch (dc->is_jmp) {
7074 case DISAS_NEXT:
7075 gen_goto_tb(dc, 1, dc->pc);
7076 break;
7077 default:
Peter Maydell40f860c2013-12-17 19:42:31 +00007078 case DISAS_UPDATE:
Peter Maydellfea50522014-01-04 22:15:45 +00007079 gen_a64_set_pc_im(dc->pc);
7080 /* fall through */
7081 case DISAS_JUMP:
Peter Maydell40f860c2013-12-17 19:42:31 +00007082 /* indicate that the hash table must be used to find the next TB */
7083 tcg_gen_exit_tb(0);
7084 break;
7085 case DISAS_TB_JUMP:
7086 case DISAS_EXC:
7087 case DISAS_SWI:
7088 break;
7089 case DISAS_WFI:
7090 /* This is a special case because we don't want to just halt the CPU
7091 * if trying to debug across a WFI.
7092 */
7093 gen_helper_wfi(cpu_env);
7094 break;
7095 }
7096 }
7097
7098done_generating:
7099 gen_tb_end(tb, num_insns);
7100 *tcg_ctx.gen_opc_ptr = INDEX_op_end;
7101
7102#ifdef DEBUG_DISAS
7103 if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)) {
7104 qemu_log("----------------\n");
7105 qemu_log("IN: %s\n", lookup_symbol(pc_start));
7106 log_target_disas(env, pc_start, dc->pc - pc_start,
7107 dc->thumb | (dc->bswap_code << 1));
7108 qemu_log("\n");
7109 }
7110#endif
7111 if (search_pc) {
7112 j = tcg_ctx.gen_opc_ptr - tcg_ctx.gen_opc_buf;
7113 lj++;
7114 while (lj <= j) {
7115 tcg_ctx.gen_opc_instr_start[lj++] = 0;
7116 }
7117 } else {
7118 tb->size = dc->pc - pc_start;
7119 tb->icount = num_insns;
Alexander Graf14ade102013-09-03 20:12:10 +01007120 }
7121}