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