]> Git Repo - qemu.git/blob - target-arm/translate-a64.c
Merge remote-tracking branch 'stefanha/block' into staging
[qemu.git] / target-arm / translate-a64.c
1 /*
2  *  AArch64 translation
3  *
4  *  Copyright (c) 2013 Alexander Graf <[email protected]>
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
31 #include "exec/gen-icount.h"
32
33 #include "helper.h"
34 #define GEN_HELPER 1
35 #include "helper.h"
36
37 static TCGv_i64 cpu_X[32];
38 static TCGv_i64 cpu_pc;
39 static TCGv_i32 cpu_NF, cpu_ZF, cpu_CF, cpu_VF;
40
41 /* Load/store exclusive handling */
42 static TCGv_i64 cpu_exclusive_addr;
43 static TCGv_i64 cpu_exclusive_val;
44 static TCGv_i64 cpu_exclusive_high;
45 #ifdef CONFIG_USER_ONLY
46 static TCGv_i64 cpu_exclusive_test;
47 static TCGv_i32 cpu_exclusive_info;
48 #endif
49
50 static const char *regnames[] = {
51     "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7",
52     "x8", "x9", "x10", "x11", "x12", "x13", "x14", "x15",
53     "x16", "x17", "x18", "x19", "x20", "x21", "x22", "x23",
54     "x24", "x25", "x26", "x27", "x28", "x29", "lr", "sp"
55 };
56
57 enum a64_shift_type {
58     A64_SHIFT_TYPE_LSL = 0,
59     A64_SHIFT_TYPE_LSR = 1,
60     A64_SHIFT_TYPE_ASR = 2,
61     A64_SHIFT_TYPE_ROR = 3
62 };
63
64 /* initialize TCG globals.  */
65 void a64_translate_init(void)
66 {
67     int i;
68
69     cpu_pc = tcg_global_mem_new_i64(TCG_AREG0,
70                                     offsetof(CPUARMState, pc),
71                                     "pc");
72     for (i = 0; i < 32; i++) {
73         cpu_X[i] = tcg_global_mem_new_i64(TCG_AREG0,
74                                           offsetof(CPUARMState, xregs[i]),
75                                           regnames[i]);
76     }
77
78     cpu_NF = tcg_global_mem_new_i32(TCG_AREG0, offsetof(CPUARMState, NF), "NF");
79     cpu_ZF = tcg_global_mem_new_i32(TCG_AREG0, offsetof(CPUARMState, ZF), "ZF");
80     cpu_CF = tcg_global_mem_new_i32(TCG_AREG0, offsetof(CPUARMState, CF), "CF");
81     cpu_VF = tcg_global_mem_new_i32(TCG_AREG0, offsetof(CPUARMState, VF), "VF");
82
83     cpu_exclusive_addr = tcg_global_mem_new_i64(TCG_AREG0,
84         offsetof(CPUARMState, exclusive_addr), "exclusive_addr");
85     cpu_exclusive_val = tcg_global_mem_new_i64(TCG_AREG0,
86         offsetof(CPUARMState, exclusive_val), "exclusive_val");
87     cpu_exclusive_high = tcg_global_mem_new_i64(TCG_AREG0,
88         offsetof(CPUARMState, exclusive_high), "exclusive_high");
89 #ifdef CONFIG_USER_ONLY
90     cpu_exclusive_test = tcg_global_mem_new_i64(TCG_AREG0,
91         offsetof(CPUARMState, exclusive_test), "exclusive_test");
92     cpu_exclusive_info = tcg_global_mem_new_i32(TCG_AREG0,
93         offsetof(CPUARMState, exclusive_info), "exclusive_info");
94 #endif
95 }
96
97 void aarch64_cpu_dump_state(CPUState *cs, FILE *f,
98                             fprintf_function cpu_fprintf, int flags)
99 {
100     ARMCPU *cpu = ARM_CPU(cs);
101     CPUARMState *env = &cpu->env;
102     uint32_t psr = pstate_read(env);
103     int i;
104
105     cpu_fprintf(f, "PC=%016"PRIx64"  SP=%016"PRIx64"\n",
106             env->pc, env->xregs[31]);
107     for (i = 0; i < 31; i++) {
108         cpu_fprintf(f, "X%02d=%016"PRIx64, i, env->xregs[i]);
109         if ((i % 4) == 3) {
110             cpu_fprintf(f, "\n");
111         } else {
112             cpu_fprintf(f, " ");
113         }
114     }
115     cpu_fprintf(f, "PSTATE=%08x (flags %c%c%c%c)\n",
116                 psr,
117                 psr & PSTATE_N ? 'N' : '-',
118                 psr & PSTATE_Z ? 'Z' : '-',
119                 psr & PSTATE_C ? 'C' : '-',
120                 psr & PSTATE_V ? 'V' : '-');
121     cpu_fprintf(f, "\n");
122
123     if (flags & CPU_DUMP_FPU) {
124         int numvfpregs = 32;
125         for (i = 0; i < numvfpregs; i += 2) {
126             uint64_t vlo = float64_val(env->vfp.regs[i * 2]);
127             uint64_t vhi = float64_val(env->vfp.regs[(i * 2) + 1]);
128             cpu_fprintf(f, "q%02d=%016" PRIx64 ":%016" PRIx64 " ",
129                         i, vhi, vlo);
130             vlo = float64_val(env->vfp.regs[(i + 1) * 2]);
131             vhi = float64_val(env->vfp.regs[((i + 1) * 2) + 1]);
132             cpu_fprintf(f, "q%02d=%016" PRIx64 ":%016" PRIx64 "\n",
133                         i + 1, vhi, vlo);
134         }
135         cpu_fprintf(f, "FPCR: %08x  FPSR: %08x\n",
136                     vfp_get_fpcr(env), vfp_get_fpsr(env));
137     }
138 }
139
140 static int get_mem_index(DisasContext *s)
141 {
142 #ifdef CONFIG_USER_ONLY
143     return 1;
144 #else
145     return s->user;
146 #endif
147 }
148
149 void gen_a64_set_pc_im(uint64_t val)
150 {
151     tcg_gen_movi_i64(cpu_pc, val);
152 }
153
154 static void gen_exception(int excp)
155 {
156     TCGv_i32 tmp = tcg_temp_new_i32();
157     tcg_gen_movi_i32(tmp, excp);
158     gen_helper_exception(cpu_env, tmp);
159     tcg_temp_free_i32(tmp);
160 }
161
162 static void gen_exception_insn(DisasContext *s, int offset, int excp)
163 {
164     gen_a64_set_pc_im(s->pc - offset);
165     gen_exception(excp);
166     s->is_jmp = DISAS_EXC;
167 }
168
169 static inline bool use_goto_tb(DisasContext *s, int n, uint64_t dest)
170 {
171     /* No direct tb linking with singlestep or deterministic io */
172     if (s->singlestep_enabled || (s->tb->cflags & CF_LAST_IO)) {
173         return false;
174     }
175
176     /* Only link tbs from inside the same guest page */
177     if ((s->tb->pc & TARGET_PAGE_MASK) != (dest & TARGET_PAGE_MASK)) {
178         return false;
179     }
180
181     return true;
182 }
183
184 static inline void gen_goto_tb(DisasContext *s, int n, uint64_t dest)
185 {
186     TranslationBlock *tb;
187
188     tb = s->tb;
189     if (use_goto_tb(s, n, dest)) {
190         tcg_gen_goto_tb(n);
191         gen_a64_set_pc_im(dest);
192         tcg_gen_exit_tb((tcg_target_long)tb + n);
193         s->is_jmp = DISAS_TB_JUMP;
194     } else {
195         gen_a64_set_pc_im(dest);
196         if (s->singlestep_enabled) {
197             gen_exception(EXCP_DEBUG);
198         }
199         tcg_gen_exit_tb(0);
200         s->is_jmp = DISAS_JUMP;
201     }
202 }
203
204 static void unallocated_encoding(DisasContext *s)
205 {
206     gen_exception_insn(s, 4, EXCP_UDEF);
207 }
208
209 #define unsupported_encoding(s, insn)                                    \
210     do {                                                                 \
211         qemu_log_mask(LOG_UNIMP,                                         \
212                       "%s:%d: unsupported instruction encoding 0x%08x "  \
213                       "at pc=%016" PRIx64 "\n",                          \
214                       __FILE__, __LINE__, insn, s->pc - 4);              \
215         unallocated_encoding(s);                                         \
216     } while (0);
217
218 static void init_tmp_a64_array(DisasContext *s)
219 {
220 #ifdef CONFIG_DEBUG_TCG
221     int i;
222     for (i = 0; i < ARRAY_SIZE(s->tmp_a64); i++) {
223         TCGV_UNUSED_I64(s->tmp_a64[i]);
224     }
225 #endif
226     s->tmp_a64_count = 0;
227 }
228
229 static void free_tmp_a64(DisasContext *s)
230 {
231     int i;
232     for (i = 0; i < s->tmp_a64_count; i++) {
233         tcg_temp_free_i64(s->tmp_a64[i]);
234     }
235     init_tmp_a64_array(s);
236 }
237
238 static TCGv_i64 new_tmp_a64(DisasContext *s)
239 {
240     assert(s->tmp_a64_count < TMP_A64_MAX);
241     return s->tmp_a64[s->tmp_a64_count++] = tcg_temp_new_i64();
242 }
243
244 static TCGv_i64 new_tmp_a64_zero(DisasContext *s)
245 {
246     TCGv_i64 t = new_tmp_a64(s);
247     tcg_gen_movi_i64(t, 0);
248     return t;
249 }
250
251 /*
252  * Register access functions
253  *
254  * These functions are used for directly accessing a register in where
255  * changes to the final register value are likely to be made. If you
256  * need to use a register for temporary calculation (e.g. index type
257  * operations) use the read_* form.
258  *
259  * B1.2.1 Register mappings
260  *
261  * In instruction register encoding 31 can refer to ZR (zero register) or
262  * the SP (stack pointer) depending on context. In QEMU's case we map SP
263  * to cpu_X[31] and ZR accesses to a temporary which can be discarded.
264  * This is the point of the _sp forms.
265  */
266 static TCGv_i64 cpu_reg(DisasContext *s, int reg)
267 {
268     if (reg == 31) {
269         return new_tmp_a64_zero(s);
270     } else {
271         return cpu_X[reg];
272     }
273 }
274
275 /* register access for when 31 == SP */
276 static TCGv_i64 cpu_reg_sp(DisasContext *s, int reg)
277 {
278     return cpu_X[reg];
279 }
280
281 /* read a cpu register in 32bit/64bit mode. Returns a TCGv_i64
282  * representing the register contents. This TCGv is an auto-freed
283  * temporary so it need not be explicitly freed, and may be modified.
284  */
285 static TCGv_i64 read_cpu_reg(DisasContext *s, int reg, int sf)
286 {
287     TCGv_i64 v = new_tmp_a64(s);
288     if (reg != 31) {
289         if (sf) {
290             tcg_gen_mov_i64(v, cpu_X[reg]);
291         } else {
292             tcg_gen_ext32u_i64(v, cpu_X[reg]);
293         }
294     } else {
295         tcg_gen_movi_i64(v, 0);
296     }
297     return v;
298 }
299
300 static TCGv_i64 read_cpu_reg_sp(DisasContext *s, int reg, int sf)
301 {
302     TCGv_i64 v = new_tmp_a64(s);
303     if (sf) {
304         tcg_gen_mov_i64(v, cpu_X[reg]);
305     } else {
306         tcg_gen_ext32u_i64(v, cpu_X[reg]);
307     }
308     return v;
309 }
310
311 /* Return the offset into CPUARMState of a slice (from
312  * the least significant end) of FP register Qn (ie
313  * Dn, Sn, Hn or Bn).
314  * (Note that this is not the same mapping as for A32; see cpu.h)
315  */
316 static inline int fp_reg_offset(int regno, TCGMemOp size)
317 {
318     int offs = offsetof(CPUARMState, vfp.regs[regno * 2]);
319 #ifdef HOST_WORDS_BIGENDIAN
320     offs += (8 - (1 << size));
321 #endif
322     return offs;
323 }
324
325 /* Offset of the high half of the 128 bit vector Qn */
326 static inline int fp_reg_hi_offset(int regno)
327 {
328     return offsetof(CPUARMState, vfp.regs[regno * 2 + 1]);
329 }
330
331 /* Convenience accessors for reading and writing single and double
332  * FP registers. Writing clears the upper parts of the associated
333  * 128 bit vector register, as required by the architecture.
334  * Note that unlike the GP register accessors, the values returned
335  * by the read functions must be manually freed.
336  */
337 static TCGv_i64 read_fp_dreg(DisasContext *s, int reg)
338 {
339     TCGv_i64 v = tcg_temp_new_i64();
340
341     tcg_gen_ld_i64(v, cpu_env, fp_reg_offset(reg, MO_64));
342     return v;
343 }
344
345 static TCGv_i32 read_fp_sreg(DisasContext *s, int reg)
346 {
347     TCGv_i32 v = tcg_temp_new_i32();
348
349     tcg_gen_ld_i32(v, cpu_env, fp_reg_offset(reg, MO_32));
350     return v;
351 }
352
353 static void write_fp_dreg(DisasContext *s, int reg, TCGv_i64 v)
354 {
355     TCGv_i64 tcg_zero = tcg_const_i64(0);
356
357     tcg_gen_st_i64(v, cpu_env, fp_reg_offset(reg, MO_64));
358     tcg_gen_st_i64(tcg_zero, cpu_env, fp_reg_hi_offset(reg));
359     tcg_temp_free_i64(tcg_zero);
360 }
361
362 static void write_fp_sreg(DisasContext *s, int reg, TCGv_i32 v)
363 {
364     TCGv_i64 tmp = tcg_temp_new_i64();
365
366     tcg_gen_extu_i32_i64(tmp, v);
367     write_fp_dreg(s, reg, tmp);
368     tcg_temp_free_i64(tmp);
369 }
370
371 static TCGv_ptr get_fpstatus_ptr(void)
372 {
373     TCGv_ptr statusptr = tcg_temp_new_ptr();
374     int offset;
375
376     /* In A64 all instructions (both FP and Neon) use the FPCR;
377      * there is no equivalent of the A32 Neon "standard FPSCR value"
378      * and all operations use vfp.fp_status.
379      */
380     offset = offsetof(CPUARMState, vfp.fp_status);
381     tcg_gen_addi_ptr(statusptr, cpu_env, offset);
382     return statusptr;
383 }
384
385 /* Set ZF and NF based on a 64 bit result. This is alas fiddlier
386  * than the 32 bit equivalent.
387  */
388 static inline void gen_set_NZ64(TCGv_i64 result)
389 {
390     TCGv_i64 flag = tcg_temp_new_i64();
391
392     tcg_gen_setcondi_i64(TCG_COND_NE, flag, result, 0);
393     tcg_gen_trunc_i64_i32(cpu_ZF, flag);
394     tcg_gen_shri_i64(flag, result, 32);
395     tcg_gen_trunc_i64_i32(cpu_NF, flag);
396     tcg_temp_free_i64(flag);
397 }
398
399 /* Set NZCV as for a logical operation: NZ as per result, CV cleared. */
400 static inline void gen_logic_CC(int sf, TCGv_i64 result)
401 {
402     if (sf) {
403         gen_set_NZ64(result);
404     } else {
405         tcg_gen_trunc_i64_i32(cpu_ZF, result);
406         tcg_gen_trunc_i64_i32(cpu_NF, result);
407     }
408     tcg_gen_movi_i32(cpu_CF, 0);
409     tcg_gen_movi_i32(cpu_VF, 0);
410 }
411
412 /* dest = T0 + T1; compute C, N, V and Z flags */
413 static void gen_add_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
414 {
415     if (sf) {
416         TCGv_i64 result, flag, tmp;
417         result = tcg_temp_new_i64();
418         flag = tcg_temp_new_i64();
419         tmp = tcg_temp_new_i64();
420
421         tcg_gen_movi_i64(tmp, 0);
422         tcg_gen_add2_i64(result, flag, t0, tmp, t1, tmp);
423
424         tcg_gen_trunc_i64_i32(cpu_CF, flag);
425
426         gen_set_NZ64(result);
427
428         tcg_gen_xor_i64(flag, result, t0);
429         tcg_gen_xor_i64(tmp, t0, t1);
430         tcg_gen_andc_i64(flag, flag, tmp);
431         tcg_temp_free_i64(tmp);
432         tcg_gen_shri_i64(flag, flag, 32);
433         tcg_gen_trunc_i64_i32(cpu_VF, flag);
434
435         tcg_gen_mov_i64(dest, result);
436         tcg_temp_free_i64(result);
437         tcg_temp_free_i64(flag);
438     } else {
439         /* 32 bit arithmetic */
440         TCGv_i32 t0_32 = tcg_temp_new_i32();
441         TCGv_i32 t1_32 = tcg_temp_new_i32();
442         TCGv_i32 tmp = tcg_temp_new_i32();
443
444         tcg_gen_movi_i32(tmp, 0);
445         tcg_gen_trunc_i64_i32(t0_32, t0);
446         tcg_gen_trunc_i64_i32(t1_32, t1);
447         tcg_gen_add2_i32(cpu_NF, cpu_CF, t0_32, tmp, t1_32, tmp);
448         tcg_gen_mov_i32(cpu_ZF, cpu_NF);
449         tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
450         tcg_gen_xor_i32(tmp, t0_32, t1_32);
451         tcg_gen_andc_i32(cpu_VF, cpu_VF, tmp);
452         tcg_gen_extu_i32_i64(dest, cpu_NF);
453
454         tcg_temp_free_i32(tmp);
455         tcg_temp_free_i32(t0_32);
456         tcg_temp_free_i32(t1_32);
457     }
458 }
459
460 /* dest = T0 - T1; compute C, N, V and Z flags */
461 static void gen_sub_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
462 {
463     if (sf) {
464         /* 64 bit arithmetic */
465         TCGv_i64 result, flag, tmp;
466
467         result = tcg_temp_new_i64();
468         flag = tcg_temp_new_i64();
469         tcg_gen_sub_i64(result, t0, t1);
470
471         gen_set_NZ64(result);
472
473         tcg_gen_setcond_i64(TCG_COND_GEU, flag, t0, t1);
474         tcg_gen_trunc_i64_i32(cpu_CF, flag);
475
476         tcg_gen_xor_i64(flag, result, t0);
477         tmp = tcg_temp_new_i64();
478         tcg_gen_xor_i64(tmp, t0, t1);
479         tcg_gen_and_i64(flag, flag, tmp);
480         tcg_temp_free_i64(tmp);
481         tcg_gen_shri_i64(flag, flag, 32);
482         tcg_gen_trunc_i64_i32(cpu_VF, flag);
483         tcg_gen_mov_i64(dest, result);
484         tcg_temp_free_i64(flag);
485         tcg_temp_free_i64(result);
486     } else {
487         /* 32 bit arithmetic */
488         TCGv_i32 t0_32 = tcg_temp_new_i32();
489         TCGv_i32 t1_32 = tcg_temp_new_i32();
490         TCGv_i32 tmp;
491
492         tcg_gen_trunc_i64_i32(t0_32, t0);
493         tcg_gen_trunc_i64_i32(t1_32, t1);
494         tcg_gen_sub_i32(cpu_NF, t0_32, t1_32);
495         tcg_gen_mov_i32(cpu_ZF, cpu_NF);
496         tcg_gen_setcond_i32(TCG_COND_GEU, cpu_CF, t0_32, t1_32);
497         tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
498         tmp = tcg_temp_new_i32();
499         tcg_gen_xor_i32(tmp, t0_32, t1_32);
500         tcg_temp_free_i32(t0_32);
501         tcg_temp_free_i32(t1_32);
502         tcg_gen_and_i32(cpu_VF, cpu_VF, tmp);
503         tcg_temp_free_i32(tmp);
504         tcg_gen_extu_i32_i64(dest, cpu_NF);
505     }
506 }
507
508 /* dest = T0 + T1 + CF; do not compute flags. */
509 static void gen_adc(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
510 {
511     TCGv_i64 flag = tcg_temp_new_i64();
512     tcg_gen_extu_i32_i64(flag, cpu_CF);
513     tcg_gen_add_i64(dest, t0, t1);
514     tcg_gen_add_i64(dest, dest, flag);
515     tcg_temp_free_i64(flag);
516
517     if (!sf) {
518         tcg_gen_ext32u_i64(dest, dest);
519     }
520 }
521
522 /* dest = T0 + T1 + CF; compute C, N, V and Z flags. */
523 static void gen_adc_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
524 {
525     if (sf) {
526         TCGv_i64 result, cf_64, vf_64, tmp;
527         result = tcg_temp_new_i64();
528         cf_64 = tcg_temp_new_i64();
529         vf_64 = tcg_temp_new_i64();
530         tmp = tcg_const_i64(0);
531
532         tcg_gen_extu_i32_i64(cf_64, cpu_CF);
533         tcg_gen_add2_i64(result, cf_64, t0, tmp, cf_64, tmp);
534         tcg_gen_add2_i64(result, cf_64, result, cf_64, t1, tmp);
535         tcg_gen_trunc_i64_i32(cpu_CF, cf_64);
536         gen_set_NZ64(result);
537
538         tcg_gen_xor_i64(vf_64, result, t0);
539         tcg_gen_xor_i64(tmp, t0, t1);
540         tcg_gen_andc_i64(vf_64, vf_64, tmp);
541         tcg_gen_shri_i64(vf_64, vf_64, 32);
542         tcg_gen_trunc_i64_i32(cpu_VF, vf_64);
543
544         tcg_gen_mov_i64(dest, result);
545
546         tcg_temp_free_i64(tmp);
547         tcg_temp_free_i64(vf_64);
548         tcg_temp_free_i64(cf_64);
549         tcg_temp_free_i64(result);
550     } else {
551         TCGv_i32 t0_32, t1_32, tmp;
552         t0_32 = tcg_temp_new_i32();
553         t1_32 = tcg_temp_new_i32();
554         tmp = tcg_const_i32(0);
555
556         tcg_gen_trunc_i64_i32(t0_32, t0);
557         tcg_gen_trunc_i64_i32(t1_32, t1);
558         tcg_gen_add2_i32(cpu_NF, cpu_CF, t0_32, tmp, cpu_CF, tmp);
559         tcg_gen_add2_i32(cpu_NF, cpu_CF, cpu_NF, cpu_CF, t1_32, tmp);
560
561         tcg_gen_mov_i32(cpu_ZF, cpu_NF);
562         tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
563         tcg_gen_xor_i32(tmp, t0_32, t1_32);
564         tcg_gen_andc_i32(cpu_VF, cpu_VF, tmp);
565         tcg_gen_extu_i32_i64(dest, cpu_NF);
566
567         tcg_temp_free_i32(tmp);
568         tcg_temp_free_i32(t1_32);
569         tcg_temp_free_i32(t0_32);
570     }
571 }
572
573 /*
574  * Load/Store generators
575  */
576
577 /*
578  * Store from GPR register to memory
579  */
580 static void do_gpr_st(DisasContext *s, TCGv_i64 source,
581                       TCGv_i64 tcg_addr, int size)
582 {
583     g_assert(size <= 3);
584     tcg_gen_qemu_st_i64(source, tcg_addr, get_mem_index(s), MO_TE + size);
585 }
586
587 /*
588  * Load from memory to GPR register
589  */
590 static void do_gpr_ld(DisasContext *s, TCGv_i64 dest, TCGv_i64 tcg_addr,
591                       int size, bool is_signed, bool extend)
592 {
593     TCGMemOp memop = MO_TE + size;
594
595     g_assert(size <= 3);
596
597     if (is_signed) {
598         memop += MO_SIGN;
599     }
600
601     tcg_gen_qemu_ld_i64(dest, tcg_addr, get_mem_index(s), memop);
602
603     if (extend && is_signed) {
604         g_assert(size < 3);
605         tcg_gen_ext32u_i64(dest, dest);
606     }
607 }
608
609 /*
610  * Store from FP register to memory
611  */
612 static void do_fp_st(DisasContext *s, int srcidx, TCGv_i64 tcg_addr, int size)
613 {
614     /* This writes the bottom N bits of a 128 bit wide vector to memory */
615     TCGv_i64 tmp = tcg_temp_new_i64();
616     tcg_gen_ld_i64(tmp, cpu_env, fp_reg_offset(srcidx, MO_64));
617     if (size < 4) {
618         tcg_gen_qemu_st_i64(tmp, tcg_addr, get_mem_index(s), MO_TE + size);
619     } else {
620         TCGv_i64 tcg_hiaddr = tcg_temp_new_i64();
621         tcg_gen_qemu_st_i64(tmp, tcg_addr, get_mem_index(s), MO_TEQ);
622         tcg_gen_qemu_st64(tmp, tcg_addr, get_mem_index(s));
623         tcg_gen_ld_i64(tmp, cpu_env, fp_reg_hi_offset(srcidx));
624         tcg_gen_addi_i64(tcg_hiaddr, tcg_addr, 8);
625         tcg_gen_qemu_st_i64(tmp, tcg_hiaddr, get_mem_index(s), MO_TEQ);
626         tcg_temp_free_i64(tcg_hiaddr);
627     }
628
629     tcg_temp_free_i64(tmp);
630 }
631
632 /*
633  * Load from memory to FP register
634  */
635 static void do_fp_ld(DisasContext *s, int destidx, TCGv_i64 tcg_addr, int size)
636 {
637     /* This always zero-extends and writes to a full 128 bit wide vector */
638     TCGv_i64 tmplo = tcg_temp_new_i64();
639     TCGv_i64 tmphi;
640
641     if (size < 4) {
642         TCGMemOp memop = MO_TE + size;
643         tmphi = tcg_const_i64(0);
644         tcg_gen_qemu_ld_i64(tmplo, tcg_addr, get_mem_index(s), memop);
645     } else {
646         TCGv_i64 tcg_hiaddr;
647         tmphi = tcg_temp_new_i64();
648         tcg_hiaddr = tcg_temp_new_i64();
649
650         tcg_gen_qemu_ld_i64(tmplo, tcg_addr, get_mem_index(s), MO_TEQ);
651         tcg_gen_addi_i64(tcg_hiaddr, tcg_addr, 8);
652         tcg_gen_qemu_ld_i64(tmphi, tcg_hiaddr, get_mem_index(s), MO_TEQ);
653         tcg_temp_free_i64(tcg_hiaddr);
654     }
655
656     tcg_gen_st_i64(tmplo, cpu_env, fp_reg_offset(destidx, MO_64));
657     tcg_gen_st_i64(tmphi, cpu_env, fp_reg_hi_offset(destidx));
658
659     tcg_temp_free_i64(tmplo);
660     tcg_temp_free_i64(tmphi);
661 }
662
663 /*
664  * This utility function is for doing register extension with an
665  * optional shift. You will likely want to pass a temporary for the
666  * destination register. See DecodeRegExtend() in the ARM ARM.
667  */
668 static void ext_and_shift_reg(TCGv_i64 tcg_out, TCGv_i64 tcg_in,
669                               int option, unsigned int shift)
670 {
671     int extsize = extract32(option, 0, 2);
672     bool is_signed = extract32(option, 2, 1);
673
674     if (is_signed) {
675         switch (extsize) {
676         case 0:
677             tcg_gen_ext8s_i64(tcg_out, tcg_in);
678             break;
679         case 1:
680             tcg_gen_ext16s_i64(tcg_out, tcg_in);
681             break;
682         case 2:
683             tcg_gen_ext32s_i64(tcg_out, tcg_in);
684             break;
685         case 3:
686             tcg_gen_mov_i64(tcg_out, tcg_in);
687             break;
688         }
689     } else {
690         switch (extsize) {
691         case 0:
692             tcg_gen_ext8u_i64(tcg_out, tcg_in);
693             break;
694         case 1:
695             tcg_gen_ext16u_i64(tcg_out, tcg_in);
696             break;
697         case 2:
698             tcg_gen_ext32u_i64(tcg_out, tcg_in);
699             break;
700         case 3:
701             tcg_gen_mov_i64(tcg_out, tcg_in);
702             break;
703         }
704     }
705
706     if (shift) {
707         tcg_gen_shli_i64(tcg_out, tcg_out, shift);
708     }
709 }
710
711 static inline void gen_check_sp_alignment(DisasContext *s)
712 {
713     /* The AArch64 architecture mandates that (if enabled via PSTATE
714      * or SCTLR bits) there is a check that SP is 16-aligned on every
715      * SP-relative load or store (with an exception generated if it is not).
716      * In line with general QEMU practice regarding misaligned accesses,
717      * we omit these checks for the sake of guest program performance.
718      * This function is provided as a hook so we can more easily add these
719      * checks in future (possibly as a "favour catching guest program bugs
720      * over speed" user selectable option).
721      */
722 }
723
724 /*
725  * the instruction disassembly implemented here matches
726  * the instruction encoding classifications in chapter 3 (C3)
727  * of the ARM Architecture Reference Manual (DDI0487A_a)
728  */
729
730 /* C3.2.7 Unconditional branch (immediate)
731  *   31  30       26 25                                  0
732  * +----+-----------+-------------------------------------+
733  * | op | 0 0 1 0 1 |                 imm26               |
734  * +----+-----------+-------------------------------------+
735  */
736 static void disas_uncond_b_imm(DisasContext *s, uint32_t insn)
737 {
738     uint64_t addr = s->pc + sextract32(insn, 0, 26) * 4 - 4;
739
740     if (insn & (1 << 31)) {
741         /* C5.6.26 BL Branch with link */
742         tcg_gen_movi_i64(cpu_reg(s, 30), s->pc);
743     }
744
745     /* C5.6.20 B Branch / C5.6.26 BL Branch with link */
746     gen_goto_tb(s, 0, addr);
747 }
748
749 /* C3.2.1 Compare & branch (immediate)
750  *   31  30         25  24  23                  5 4      0
751  * +----+-------------+----+---------------------+--------+
752  * | sf | 0 1 1 0 1 0 | op |         imm19       |   Rt   |
753  * +----+-------------+----+---------------------+--------+
754  */
755 static void disas_comp_b_imm(DisasContext *s, uint32_t insn)
756 {
757     unsigned int sf, op, rt;
758     uint64_t addr;
759     int label_match;
760     TCGv_i64 tcg_cmp;
761
762     sf = extract32(insn, 31, 1);
763     op = extract32(insn, 24, 1); /* 0: CBZ; 1: CBNZ */
764     rt = extract32(insn, 0, 5);
765     addr = s->pc + sextract32(insn, 5, 19) * 4 - 4;
766
767     tcg_cmp = read_cpu_reg(s, rt, sf);
768     label_match = gen_new_label();
769
770     tcg_gen_brcondi_i64(op ? TCG_COND_NE : TCG_COND_EQ,
771                         tcg_cmp, 0, label_match);
772
773     gen_goto_tb(s, 0, s->pc);
774     gen_set_label(label_match);
775     gen_goto_tb(s, 1, addr);
776 }
777
778 /* C3.2.5 Test & branch (immediate)
779  *   31  30         25  24  23   19 18          5 4    0
780  * +----+-------------+----+-------+-------------+------+
781  * | b5 | 0 1 1 0 1 1 | op |  b40  |    imm14    |  Rt  |
782  * +----+-------------+----+-------+-------------+------+
783  */
784 static void disas_test_b_imm(DisasContext *s, uint32_t insn)
785 {
786     unsigned int bit_pos, op, rt;
787     uint64_t addr;
788     int label_match;
789     TCGv_i64 tcg_cmp;
790
791     bit_pos = (extract32(insn, 31, 1) << 5) | extract32(insn, 19, 5);
792     op = extract32(insn, 24, 1); /* 0: TBZ; 1: TBNZ */
793     addr = s->pc + sextract32(insn, 5, 14) * 4 - 4;
794     rt = extract32(insn, 0, 5);
795
796     tcg_cmp = tcg_temp_new_i64();
797     tcg_gen_andi_i64(tcg_cmp, cpu_reg(s, rt), (1ULL << bit_pos));
798     label_match = gen_new_label();
799     tcg_gen_brcondi_i64(op ? TCG_COND_NE : TCG_COND_EQ,
800                         tcg_cmp, 0, label_match);
801     tcg_temp_free_i64(tcg_cmp);
802     gen_goto_tb(s, 0, s->pc);
803     gen_set_label(label_match);
804     gen_goto_tb(s, 1, addr);
805 }
806
807 /* C3.2.2 / C5.6.19 Conditional branch (immediate)
808  *  31           25  24  23                  5   4  3    0
809  * +---------------+----+---------------------+----+------+
810  * | 0 1 0 1 0 1 0 | o1 |         imm19       | o0 | cond |
811  * +---------------+----+---------------------+----+------+
812  */
813 static void disas_cond_b_imm(DisasContext *s, uint32_t insn)
814 {
815     unsigned int cond;
816     uint64_t addr;
817
818     if ((insn & (1 << 4)) || (insn & (1 << 24))) {
819         unallocated_encoding(s);
820         return;
821     }
822     addr = s->pc + sextract32(insn, 5, 19) * 4 - 4;
823     cond = extract32(insn, 0, 4);
824
825     if (cond < 0x0e) {
826         /* genuinely conditional branches */
827         int label_match = gen_new_label();
828         arm_gen_test_cc(cond, label_match);
829         gen_goto_tb(s, 0, s->pc);
830         gen_set_label(label_match);
831         gen_goto_tb(s, 1, addr);
832     } else {
833         /* 0xe and 0xf are both "always" conditions */
834         gen_goto_tb(s, 0, addr);
835     }
836 }
837
838 /* C5.6.68 HINT */
839 static void handle_hint(DisasContext *s, uint32_t insn,
840                         unsigned int op1, unsigned int op2, unsigned int crm)
841 {
842     unsigned int selector = crm << 3 | op2;
843
844     if (op1 != 3) {
845         unallocated_encoding(s);
846         return;
847     }
848
849     switch (selector) {
850     case 0: /* NOP */
851         return;
852     case 1: /* YIELD */
853     case 2: /* WFE */
854     case 3: /* WFI */
855     case 4: /* SEV */
856     case 5: /* SEVL */
857         /* we treat all as NOP at least for now */
858         return;
859     default:
860         /* default specified as NOP equivalent */
861         return;
862     }
863 }
864
865 static void gen_clrex(DisasContext *s, uint32_t insn)
866 {
867     tcg_gen_movi_i64(cpu_exclusive_addr, -1);
868 }
869
870 /* CLREX, DSB, DMB, ISB */
871 static void handle_sync(DisasContext *s, uint32_t insn,
872                         unsigned int op1, unsigned int op2, unsigned int crm)
873 {
874     if (op1 != 3) {
875         unallocated_encoding(s);
876         return;
877     }
878
879     switch (op2) {
880     case 2: /* CLREX */
881         gen_clrex(s, insn);
882         return;
883     case 4: /* DSB */
884     case 5: /* DMB */
885     case 6: /* ISB */
886         /* We don't emulate caches so barriers are no-ops */
887         return;
888     default:
889         unallocated_encoding(s);
890         return;
891     }
892 }
893
894 /* C5.6.130 MSR (immediate) - move immediate to processor state field */
895 static void handle_msr_i(DisasContext *s, uint32_t insn,
896                          unsigned int op1, unsigned int op2, unsigned int crm)
897 {
898     unsupported_encoding(s, insn);
899 }
900
901 static void gen_get_nzcv(TCGv_i64 tcg_rt)
902 {
903     TCGv_i32 tmp = tcg_temp_new_i32();
904     TCGv_i32 nzcv = tcg_temp_new_i32();
905
906     /* build bit 31, N */
907     tcg_gen_andi_i32(nzcv, cpu_NF, (1 << 31));
908     /* build bit 30, Z */
909     tcg_gen_setcondi_i32(TCG_COND_EQ, tmp, cpu_ZF, 0);
910     tcg_gen_deposit_i32(nzcv, nzcv, tmp, 30, 1);
911     /* build bit 29, C */
912     tcg_gen_deposit_i32(nzcv, nzcv, cpu_CF, 29, 1);
913     /* build bit 28, V */
914     tcg_gen_shri_i32(tmp, cpu_VF, 31);
915     tcg_gen_deposit_i32(nzcv, nzcv, tmp, 28, 1);
916     /* generate result */
917     tcg_gen_extu_i32_i64(tcg_rt, nzcv);
918
919     tcg_temp_free_i32(nzcv);
920     tcg_temp_free_i32(tmp);
921 }
922
923 static void gen_set_nzcv(TCGv_i64 tcg_rt)
924
925 {
926     TCGv_i32 nzcv = tcg_temp_new_i32();
927
928     /* take NZCV from R[t] */
929     tcg_gen_trunc_i64_i32(nzcv, tcg_rt);
930
931     /* bit 31, N */
932     tcg_gen_andi_i32(cpu_NF, nzcv, (1 << 31));
933     /* bit 30, Z */
934     tcg_gen_andi_i32(cpu_ZF, nzcv, (1 << 30));
935     tcg_gen_setcondi_i32(TCG_COND_EQ, cpu_ZF, cpu_ZF, 0);
936     /* bit 29, C */
937     tcg_gen_andi_i32(cpu_CF, nzcv, (1 << 29));
938     tcg_gen_shri_i32(cpu_CF, cpu_CF, 29);
939     /* bit 28, V */
940     tcg_gen_andi_i32(cpu_VF, nzcv, (1 << 28));
941     tcg_gen_shli_i32(cpu_VF, cpu_VF, 3);
942     tcg_temp_free_i32(nzcv);
943 }
944
945 /* C5.6.129 MRS - move from system register
946  * C5.6.131 MSR (register) - move to system register
947  * C5.6.204 SYS
948  * C5.6.205 SYSL
949  * These are all essentially the same insn in 'read' and 'write'
950  * versions, with varying op0 fields.
951  */
952 static void handle_sys(DisasContext *s, uint32_t insn, bool isread,
953                        unsigned int op0, unsigned int op1, unsigned int op2,
954                        unsigned int crn, unsigned int crm, unsigned int rt)
955 {
956     const ARMCPRegInfo *ri;
957     TCGv_i64 tcg_rt;
958
959     ri = get_arm_cp_reginfo(s->cp_regs,
960                             ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP,
961                                                crn, crm, op0, op1, op2));
962
963     if (!ri) {
964         /* Unknown register */
965         unallocated_encoding(s);
966         return;
967     }
968
969     /* Check access permissions */
970     if (!cp_access_ok(s->current_pl, ri, isread)) {
971         unallocated_encoding(s);
972         return;
973     }
974
975     /* Handle special cases first */
976     switch (ri->type & ~(ARM_CP_FLAG_MASK & ~ARM_CP_SPECIAL)) {
977     case ARM_CP_NOP:
978         return;
979     case ARM_CP_NZCV:
980         tcg_rt = cpu_reg(s, rt);
981         if (isread) {
982             gen_get_nzcv(tcg_rt);
983         } else {
984             gen_set_nzcv(tcg_rt);
985         }
986         return;
987     default:
988         break;
989     }
990
991     if (use_icount && (ri->type & ARM_CP_IO)) {
992         gen_io_start();
993     }
994
995     tcg_rt = cpu_reg(s, rt);
996
997     if (isread) {
998         if (ri->type & ARM_CP_CONST) {
999             tcg_gen_movi_i64(tcg_rt, ri->resetvalue);
1000         } else if (ri->readfn) {
1001             TCGv_ptr tmpptr;
1002             gen_a64_set_pc_im(s->pc - 4);
1003             tmpptr = tcg_const_ptr(ri);
1004             gen_helper_get_cp_reg64(tcg_rt, cpu_env, tmpptr);
1005             tcg_temp_free_ptr(tmpptr);
1006         } else {
1007             tcg_gen_ld_i64(tcg_rt, cpu_env, ri->fieldoffset);
1008         }
1009     } else {
1010         if (ri->type & ARM_CP_CONST) {
1011             /* If not forbidden by access permissions, treat as WI */
1012             return;
1013         } else if (ri->writefn) {
1014             TCGv_ptr tmpptr;
1015             gen_a64_set_pc_im(s->pc - 4);
1016             tmpptr = tcg_const_ptr(ri);
1017             gen_helper_set_cp_reg64(cpu_env, tmpptr, tcg_rt);
1018             tcg_temp_free_ptr(tmpptr);
1019         } else {
1020             tcg_gen_st_i64(tcg_rt, cpu_env, ri->fieldoffset);
1021         }
1022     }
1023
1024     if (use_icount && (ri->type & ARM_CP_IO)) {
1025         /* I/O operations must end the TB here (whether read or write) */
1026         gen_io_end();
1027         s->is_jmp = DISAS_UPDATE;
1028     } else if (!isread && !(ri->type & ARM_CP_SUPPRESS_TB_END)) {
1029         /* We default to ending the TB on a coprocessor register write,
1030          * but allow this to be suppressed by the register definition
1031          * (usually only necessary to work around guest bugs).
1032          */
1033         s->is_jmp = DISAS_UPDATE;
1034     }
1035 }
1036
1037 /* C3.2.4 System
1038  *  31                 22 21  20 19 18 16 15   12 11    8 7   5 4    0
1039  * +---------------------+---+-----+-----+-------+-------+-----+------+
1040  * | 1 1 0 1 0 1 0 1 0 0 | L | op0 | op1 |  CRn  |  CRm  | op2 |  Rt  |
1041  * +---------------------+---+-----+-----+-------+-------+-----+------+
1042  */
1043 static void disas_system(DisasContext *s, uint32_t insn)
1044 {
1045     unsigned int l, op0, op1, crn, crm, op2, rt;
1046     l = extract32(insn, 21, 1);
1047     op0 = extract32(insn, 19, 2);
1048     op1 = extract32(insn, 16, 3);
1049     crn = extract32(insn, 12, 4);
1050     crm = extract32(insn, 8, 4);
1051     op2 = extract32(insn, 5, 3);
1052     rt = extract32(insn, 0, 5);
1053
1054     if (op0 == 0) {
1055         if (l || rt != 31) {
1056             unallocated_encoding(s);
1057             return;
1058         }
1059         switch (crn) {
1060         case 2: /* C5.6.68 HINT */
1061             handle_hint(s, insn, op1, op2, crm);
1062             break;
1063         case 3: /* CLREX, DSB, DMB, ISB */
1064             handle_sync(s, insn, op1, op2, crm);
1065             break;
1066         case 4: /* C5.6.130 MSR (immediate) */
1067             handle_msr_i(s, insn, op1, op2, crm);
1068             break;
1069         default:
1070             unallocated_encoding(s);
1071             break;
1072         }
1073         return;
1074     }
1075     handle_sys(s, insn, l, op0, op1, op2, crn, crm, rt);
1076 }
1077
1078 /* C3.2.3 Exception generation
1079  *
1080  *  31             24 23 21 20                     5 4   2 1  0
1081  * +-----------------+-----+------------------------+-----+----+
1082  * | 1 1 0 1 0 1 0 0 | opc |          imm16         | op2 | LL |
1083  * +-----------------------+------------------------+----------+
1084  */
1085 static void disas_exc(DisasContext *s, uint32_t insn)
1086 {
1087     int opc = extract32(insn, 21, 3);
1088     int op2_ll = extract32(insn, 0, 5);
1089
1090     switch (opc) {
1091     case 0:
1092         /* SVC, HVC, SMC; since we don't support the Virtualization
1093          * or TrustZone extensions these all UNDEF except SVC.
1094          */
1095         if (op2_ll != 1) {
1096             unallocated_encoding(s);
1097             break;
1098         }
1099         gen_exception_insn(s, 0, EXCP_SWI);
1100         break;
1101     case 1:
1102         if (op2_ll != 0) {
1103             unallocated_encoding(s);
1104             break;
1105         }
1106         /* BRK */
1107         gen_exception_insn(s, 0, EXCP_BKPT);
1108         break;
1109     case 2:
1110         if (op2_ll != 0) {
1111             unallocated_encoding(s);
1112             break;
1113         }
1114         /* HLT */
1115         unsupported_encoding(s, insn);
1116         break;
1117     case 5:
1118         if (op2_ll < 1 || op2_ll > 3) {
1119             unallocated_encoding(s);
1120             break;
1121         }
1122         /* DCPS1, DCPS2, DCPS3 */
1123         unsupported_encoding(s, insn);
1124         break;
1125     default:
1126         unallocated_encoding(s);
1127         break;
1128     }
1129 }
1130
1131 /* C3.2.7 Unconditional branch (register)
1132  *  31           25 24   21 20   16 15   10 9    5 4     0
1133  * +---------------+-------+-------+-------+------+-------+
1134  * | 1 1 0 1 0 1 1 |  opc  |  op2  |  op3  |  Rn  |  op4  |
1135  * +---------------+-------+-------+-------+------+-------+
1136  */
1137 static void disas_uncond_b_reg(DisasContext *s, uint32_t insn)
1138 {
1139     unsigned int opc, op2, op3, rn, op4;
1140
1141     opc = extract32(insn, 21, 4);
1142     op2 = extract32(insn, 16, 5);
1143     op3 = extract32(insn, 10, 6);
1144     rn = extract32(insn, 5, 5);
1145     op4 = extract32(insn, 0, 5);
1146
1147     if (op4 != 0x0 || op3 != 0x0 || op2 != 0x1f) {
1148         unallocated_encoding(s);
1149         return;
1150     }
1151
1152     switch (opc) {
1153     case 0: /* BR */
1154     case 2: /* RET */
1155         break;
1156     case 1: /* BLR */
1157         tcg_gen_movi_i64(cpu_reg(s, 30), s->pc);
1158         break;
1159     case 4: /* ERET */
1160     case 5: /* DRPS */
1161         if (rn != 0x1f) {
1162             unallocated_encoding(s);
1163         } else {
1164             unsupported_encoding(s, insn);
1165         }
1166         return;
1167     default:
1168         unallocated_encoding(s);
1169         return;
1170     }
1171
1172     tcg_gen_mov_i64(cpu_pc, cpu_reg(s, rn));
1173     s->is_jmp = DISAS_JUMP;
1174 }
1175
1176 /* C3.2 Branches, exception generating and system instructions */
1177 static void disas_b_exc_sys(DisasContext *s, uint32_t insn)
1178 {
1179     switch (extract32(insn, 25, 7)) {
1180     case 0x0a: case 0x0b:
1181     case 0x4a: case 0x4b: /* Unconditional branch (immediate) */
1182         disas_uncond_b_imm(s, insn);
1183         break;
1184     case 0x1a: case 0x5a: /* Compare & branch (immediate) */
1185         disas_comp_b_imm(s, insn);
1186         break;
1187     case 0x1b: case 0x5b: /* Test & branch (immediate) */
1188         disas_test_b_imm(s, insn);
1189         break;
1190     case 0x2a: /* Conditional branch (immediate) */
1191         disas_cond_b_imm(s, insn);
1192         break;
1193     case 0x6a: /* Exception generation / System */
1194         if (insn & (1 << 24)) {
1195             disas_system(s, insn);
1196         } else {
1197             disas_exc(s, insn);
1198         }
1199         break;
1200     case 0x6b: /* Unconditional branch (register) */
1201         disas_uncond_b_reg(s, insn);
1202         break;
1203     default:
1204         unallocated_encoding(s);
1205         break;
1206     }
1207 }
1208
1209 /*
1210  * Load/Store exclusive instructions are implemented by remembering
1211  * the value/address loaded, and seeing if these are the same
1212  * when the store is performed. This is not actually the architecturally
1213  * mandated semantics, but it works for typical guest code sequences
1214  * and avoids having to monitor regular stores.
1215  *
1216  * In system emulation mode only one CPU will be running at once, so
1217  * this sequence is effectively atomic.  In user emulation mode we
1218  * throw an exception and handle the atomic operation elsewhere.
1219  */
1220 static void gen_load_exclusive(DisasContext *s, int rt, int rt2,
1221                                TCGv_i64 addr, int size, bool is_pair)
1222 {
1223     TCGv_i64 tmp = tcg_temp_new_i64();
1224     TCGMemOp memop = MO_TE + size;
1225
1226     g_assert(size <= 3);
1227     tcg_gen_qemu_ld_i64(tmp, addr, get_mem_index(s), memop);
1228
1229     if (is_pair) {
1230         TCGv_i64 addr2 = tcg_temp_new_i64();
1231         TCGv_i64 hitmp = tcg_temp_new_i64();
1232
1233         g_assert(size >= 2);
1234         tcg_gen_addi_i64(addr2, addr, 1 << size);
1235         tcg_gen_qemu_ld_i64(hitmp, addr2, get_mem_index(s), memop);
1236         tcg_temp_free_i64(addr2);
1237         tcg_gen_mov_i64(cpu_exclusive_high, hitmp);
1238         tcg_gen_mov_i64(cpu_reg(s, rt2), hitmp);
1239         tcg_temp_free_i64(hitmp);
1240     }
1241
1242     tcg_gen_mov_i64(cpu_exclusive_val, tmp);
1243     tcg_gen_mov_i64(cpu_reg(s, rt), tmp);
1244
1245     tcg_temp_free_i64(tmp);
1246     tcg_gen_mov_i64(cpu_exclusive_addr, addr);
1247 }
1248
1249 #ifdef CONFIG_USER_ONLY
1250 static void gen_store_exclusive(DisasContext *s, int rd, int rt, int rt2,
1251                                 TCGv_i64 addr, int size, int is_pair)
1252 {
1253     tcg_gen_mov_i64(cpu_exclusive_test, addr);
1254     tcg_gen_movi_i32(cpu_exclusive_info,
1255                      size | is_pair << 2 | (rd << 4) | (rt << 9) | (rt2 << 14));
1256     gen_exception_insn(s, 4, EXCP_STREX);
1257 }
1258 #else
1259 static void gen_store_exclusive(DisasContext *s, int rd, int rt, int rt2,
1260                                 TCGv_i64 addr, int size, int is_pair)
1261 {
1262     qemu_log_mask(LOG_UNIMP,
1263                   "%s:%d: system mode store_exclusive unsupported "
1264                   "at pc=%016" PRIx64 "\n",
1265                   __FILE__, __LINE__, s->pc - 4);
1266 }
1267 #endif
1268
1269 /* C3.3.6 Load/store exclusive
1270  *
1271  *  31 30 29         24  23  22   21  20  16  15  14   10 9    5 4    0
1272  * +-----+-------------+----+---+----+------+----+-------+------+------+
1273  * | sz  | 0 0 1 0 0 0 | o2 | L | o1 |  Rs  | o0 |  Rt2  |  Rn  | Rt   |
1274  * +-----+-------------+----+---+----+------+----+-------+------+------+
1275  *
1276  *  sz: 00 -> 8 bit, 01 -> 16 bit, 10 -> 32 bit, 11 -> 64 bit
1277  *   L: 0 -> store, 1 -> load
1278  *  o2: 0 -> exclusive, 1 -> not
1279  *  o1: 0 -> single register, 1 -> register pair
1280  *  o0: 1 -> load-acquire/store-release, 0 -> not
1281  *
1282  *  o0 == 0 AND o2 == 1 is un-allocated
1283  *  o1 == 1 is un-allocated except for 32 and 64 bit sizes
1284  */
1285 static void disas_ldst_excl(DisasContext *s, uint32_t insn)
1286 {
1287     int rt = extract32(insn, 0, 5);
1288     int rn = extract32(insn, 5, 5);
1289     int rt2 = extract32(insn, 10, 5);
1290     int is_lasr = extract32(insn, 15, 1);
1291     int rs = extract32(insn, 16, 5);
1292     int is_pair = extract32(insn, 21, 1);
1293     int is_store = !extract32(insn, 22, 1);
1294     int is_excl = !extract32(insn, 23, 1);
1295     int size = extract32(insn, 30, 2);
1296     TCGv_i64 tcg_addr;
1297
1298     if ((!is_excl && !is_lasr) ||
1299         (is_pair && size < 2)) {
1300         unallocated_encoding(s);
1301         return;
1302     }
1303
1304     if (rn == 31) {
1305         gen_check_sp_alignment(s);
1306     }
1307     tcg_addr = read_cpu_reg_sp(s, rn, 1);
1308
1309     /* Note that since TCG is single threaded load-acquire/store-release
1310      * semantics require no extra if (is_lasr) { ... } handling.
1311      */
1312
1313     if (is_excl) {
1314         if (!is_store) {
1315             gen_load_exclusive(s, rt, rt2, tcg_addr, size, is_pair);
1316         } else {
1317             gen_store_exclusive(s, rs, rt, rt2, tcg_addr, size, is_pair);
1318         }
1319     } else {
1320         TCGv_i64 tcg_rt = cpu_reg(s, rt);
1321         if (is_store) {
1322             do_gpr_st(s, tcg_rt, tcg_addr, size);
1323         } else {
1324             do_gpr_ld(s, tcg_rt, tcg_addr, size, false, false);
1325         }
1326         if (is_pair) {
1327             TCGv_i64 tcg_rt2 = cpu_reg(s, rt);
1328             tcg_gen_addi_i64(tcg_addr, tcg_addr, 1 << size);
1329             if (is_store) {
1330                 do_gpr_st(s, tcg_rt2, tcg_addr, size);
1331             } else {
1332                 do_gpr_ld(s, tcg_rt2, tcg_addr, size, false, false);
1333             }
1334         }
1335     }
1336 }
1337
1338 /*
1339  * C3.3.5 Load register (literal)
1340  *
1341  *  31 30 29   27  26 25 24 23                5 4     0
1342  * +-----+-------+---+-----+-------------------+-------+
1343  * | opc | 0 1 1 | V | 0 0 |     imm19         |  Rt   |
1344  * +-----+-------+---+-----+-------------------+-------+
1345  *
1346  * V: 1 -> vector (simd/fp)
1347  * opc (non-vector): 00 -> 32 bit, 01 -> 64 bit,
1348  *                   10-> 32 bit signed, 11 -> prefetch
1349  * opc (vector): 00 -> 32 bit, 01 -> 64 bit, 10 -> 128 bit (11 unallocated)
1350  */
1351 static void disas_ld_lit(DisasContext *s, uint32_t insn)
1352 {
1353     int rt = extract32(insn, 0, 5);
1354     int64_t imm = sextract32(insn, 5, 19) << 2;
1355     bool is_vector = extract32(insn, 26, 1);
1356     int opc = extract32(insn, 30, 2);
1357     bool is_signed = false;
1358     int size = 2;
1359     TCGv_i64 tcg_rt, tcg_addr;
1360
1361     if (is_vector) {
1362         if (opc == 3) {
1363             unallocated_encoding(s);
1364             return;
1365         }
1366         size = 2 + opc;
1367     } else {
1368         if (opc == 3) {
1369             /* PRFM (literal) : prefetch */
1370             return;
1371         }
1372         size = 2 + extract32(opc, 0, 1);
1373         is_signed = extract32(opc, 1, 1);
1374     }
1375
1376     tcg_rt = cpu_reg(s, rt);
1377
1378     tcg_addr = tcg_const_i64((s->pc - 4) + imm);
1379     if (is_vector) {
1380         do_fp_ld(s, rt, tcg_addr, size);
1381     } else {
1382         do_gpr_ld(s, tcg_rt, tcg_addr, size, is_signed, false);
1383     }
1384     tcg_temp_free_i64(tcg_addr);
1385 }
1386
1387 /*
1388  * C5.6.80 LDNP (Load Pair - non-temporal hint)
1389  * C5.6.81 LDP (Load Pair - non vector)
1390  * C5.6.82 LDPSW (Load Pair Signed Word - non vector)
1391  * C5.6.176 STNP (Store Pair - non-temporal hint)
1392  * C5.6.177 STP (Store Pair - non vector)
1393  * C6.3.165 LDNP (Load Pair of SIMD&FP - non-temporal hint)
1394  * C6.3.165 LDP (Load Pair of SIMD&FP)
1395  * C6.3.284 STNP (Store Pair of SIMD&FP - non-temporal hint)
1396  * C6.3.284 STP (Store Pair of SIMD&FP)
1397  *
1398  *  31 30 29   27  26  25 24   23  22 21   15 14   10 9    5 4    0
1399  * +-----+-------+---+---+-------+---+-----------------------------+
1400  * | opc | 1 0 1 | V | 0 | index | L |  imm7 |  Rt2  |  Rn  | Rt   |
1401  * +-----+-------+---+---+-------+---+-------+-------+------+------+
1402  *
1403  * opc: LDP/STP/LDNP/STNP        00 -> 32 bit, 10 -> 64 bit
1404  *      LDPSW                    01
1405  *      LDP/STP/LDNP/STNP (SIMD) 00 -> 32 bit, 01 -> 64 bit, 10 -> 128 bit
1406  *   V: 0 -> GPR, 1 -> Vector
1407  * idx: 00 -> signed offset with non-temporal hint, 01 -> post-index,
1408  *      10 -> signed offset, 11 -> pre-index
1409  *   L: 0 -> Store 1 -> Load
1410  *
1411  * Rt, Rt2 = GPR or SIMD registers to be stored
1412  * Rn = general purpose register containing address
1413  * imm7 = signed offset (multiple of 4 or 8 depending on size)
1414  */
1415 static void disas_ldst_pair(DisasContext *s, uint32_t insn)
1416 {
1417     int rt = extract32(insn, 0, 5);
1418     int rn = extract32(insn, 5, 5);
1419     int rt2 = extract32(insn, 10, 5);
1420     int64_t offset = sextract32(insn, 15, 7);
1421     int index = extract32(insn, 23, 2);
1422     bool is_vector = extract32(insn, 26, 1);
1423     bool is_load = extract32(insn, 22, 1);
1424     int opc = extract32(insn, 30, 2);
1425
1426     bool is_signed = false;
1427     bool postindex = false;
1428     bool wback = false;
1429
1430     TCGv_i64 tcg_addr; /* calculated address */
1431     int size;
1432
1433     if (opc == 3) {
1434         unallocated_encoding(s);
1435         return;
1436     }
1437
1438     if (is_vector) {
1439         size = 2 + opc;
1440     } else {
1441         size = 2 + extract32(opc, 1, 1);
1442         is_signed = extract32(opc, 0, 1);
1443         if (!is_load && is_signed) {
1444             unallocated_encoding(s);
1445             return;
1446         }
1447     }
1448
1449     switch (index) {
1450     case 1: /* post-index */
1451         postindex = true;
1452         wback = true;
1453         break;
1454     case 0:
1455         /* signed offset with "non-temporal" hint. Since we don't emulate
1456          * caches we don't care about hints to the cache system about
1457          * data access patterns, and handle this identically to plain
1458          * signed offset.
1459          */
1460         if (is_signed) {
1461             /* There is no non-temporal-hint version of LDPSW */
1462             unallocated_encoding(s);
1463             return;
1464         }
1465         postindex = false;
1466         break;
1467     case 2: /* signed offset, rn not updated */
1468         postindex = false;
1469         break;
1470     case 3: /* pre-index */
1471         postindex = false;
1472         wback = true;
1473         break;
1474     }
1475
1476     offset <<= size;
1477
1478     if (rn == 31) {
1479         gen_check_sp_alignment(s);
1480     }
1481
1482     tcg_addr = read_cpu_reg_sp(s, rn, 1);
1483
1484     if (!postindex) {
1485         tcg_gen_addi_i64(tcg_addr, tcg_addr, offset);
1486     }
1487
1488     if (is_vector) {
1489         if (is_load) {
1490             do_fp_ld(s, rt, tcg_addr, size);
1491         } else {
1492             do_fp_st(s, rt, tcg_addr, size);
1493         }
1494     } else {
1495         TCGv_i64 tcg_rt = cpu_reg(s, rt);
1496         if (is_load) {
1497             do_gpr_ld(s, tcg_rt, tcg_addr, size, is_signed, false);
1498         } else {
1499             do_gpr_st(s, tcg_rt, tcg_addr, size);
1500         }
1501     }
1502     tcg_gen_addi_i64(tcg_addr, tcg_addr, 1 << size);
1503     if (is_vector) {
1504         if (is_load) {
1505             do_fp_ld(s, rt2, tcg_addr, size);
1506         } else {
1507             do_fp_st(s, rt2, tcg_addr, size);
1508         }
1509     } else {
1510         TCGv_i64 tcg_rt2 = cpu_reg(s, rt2);
1511         if (is_load) {
1512             do_gpr_ld(s, tcg_rt2, tcg_addr, size, is_signed, false);
1513         } else {
1514             do_gpr_st(s, tcg_rt2, tcg_addr, size);
1515         }
1516     }
1517
1518     if (wback) {
1519         if (postindex) {
1520             tcg_gen_addi_i64(tcg_addr, tcg_addr, offset - (1 << size));
1521         } else {
1522             tcg_gen_subi_i64(tcg_addr, tcg_addr, 1 << size);
1523         }
1524         tcg_gen_mov_i64(cpu_reg_sp(s, rn), tcg_addr);
1525     }
1526 }
1527
1528 /*
1529  * C3.3.8 Load/store (immediate post-indexed)
1530  * C3.3.9 Load/store (immediate pre-indexed)
1531  * C3.3.12 Load/store (unscaled immediate)
1532  *
1533  * 31 30 29   27  26 25 24 23 22 21  20    12 11 10 9    5 4    0
1534  * +----+-------+---+-----+-----+---+--------+-----+------+------+
1535  * |size| 1 1 1 | V | 0 0 | opc | 0 |  imm9  | idx |  Rn  |  Rt  |
1536  * +----+-------+---+-----+-----+---+--------+-----+------+------+
1537  *
1538  * idx = 01 -> post-indexed, 11 pre-indexed, 00 unscaled imm. (no writeback)
1539  * V = 0 -> non-vector
1540  * size: 00 -> 8 bit, 01 -> 16 bit, 10 -> 32 bit, 11 -> 64bit
1541  * opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
1542  */
1543 static void disas_ldst_reg_imm9(DisasContext *s, uint32_t insn)
1544 {
1545     int rt = extract32(insn, 0, 5);
1546     int rn = extract32(insn, 5, 5);
1547     int imm9 = sextract32(insn, 12, 9);
1548     int opc = extract32(insn, 22, 2);
1549     int size = extract32(insn, 30, 2);
1550     int idx = extract32(insn, 10, 2);
1551     bool is_signed = false;
1552     bool is_store = false;
1553     bool is_extended = false;
1554     bool is_vector = extract32(insn, 26, 1);
1555     bool post_index;
1556     bool writeback;
1557
1558     TCGv_i64 tcg_addr;
1559
1560     if (is_vector) {
1561         size |= (opc & 2) << 1;
1562         if (size > 4) {
1563             unallocated_encoding(s);
1564             return;
1565         }
1566         is_store = ((opc & 1) == 0);
1567     } else {
1568         if (size == 3 && opc == 2) {
1569             /* PRFM - prefetch */
1570             return;
1571         }
1572         if (opc == 3 && size > 1) {
1573             unallocated_encoding(s);
1574             return;
1575         }
1576         is_store = (opc == 0);
1577         is_signed = opc & (1<<1);
1578         is_extended = (size < 3) && (opc & 1);
1579     }
1580
1581     switch (idx) {
1582     case 0:
1583         post_index = false;
1584         writeback = false;
1585         break;
1586     case 1:
1587         post_index = true;
1588         writeback = true;
1589         break;
1590     case 3:
1591         post_index = false;
1592         writeback = true;
1593         break;
1594     case 2:
1595         g_assert(false);
1596         break;
1597     }
1598
1599     if (rn == 31) {
1600         gen_check_sp_alignment(s);
1601     }
1602     tcg_addr = read_cpu_reg_sp(s, rn, 1);
1603
1604     if (!post_index) {
1605         tcg_gen_addi_i64(tcg_addr, tcg_addr, imm9);
1606     }
1607
1608     if (is_vector) {
1609         if (is_store) {
1610             do_fp_st(s, rt, tcg_addr, size);
1611         } else {
1612             do_fp_ld(s, rt, tcg_addr, size);
1613         }
1614     } else {
1615         TCGv_i64 tcg_rt = cpu_reg(s, rt);
1616         if (is_store) {
1617             do_gpr_st(s, tcg_rt, tcg_addr, size);
1618         } else {
1619             do_gpr_ld(s, tcg_rt, tcg_addr, size, is_signed, is_extended);
1620         }
1621     }
1622
1623     if (writeback) {
1624         TCGv_i64 tcg_rn = cpu_reg_sp(s, rn);
1625         if (post_index) {
1626             tcg_gen_addi_i64(tcg_addr, tcg_addr, imm9);
1627         }
1628         tcg_gen_mov_i64(tcg_rn, tcg_addr);
1629     }
1630 }
1631
1632 /*
1633  * C3.3.10 Load/store (register offset)
1634  *
1635  * 31 30 29   27  26 25 24 23 22 21  20  16 15 13 12 11 10 9  5 4  0
1636  * +----+-------+---+-----+-----+---+------+-----+--+-----+----+----+
1637  * |size| 1 1 1 | V | 0 0 | opc | 1 |  Rm  | opt | S| 1 0 | Rn | Rt |
1638  * +----+-------+---+-----+-----+---+------+-----+--+-----+----+----+
1639  *
1640  * For non-vector:
1641  *   size: 00-> byte, 01 -> 16 bit, 10 -> 32bit, 11 -> 64bit
1642  *   opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
1643  * For vector:
1644  *   size is opc<1>:size<1:0> so 100 -> 128 bit; 110 and 111 unallocated
1645  *   opc<0>: 0 -> store, 1 -> load
1646  * V: 1 -> vector/simd
1647  * opt: extend encoding (see DecodeRegExtend)
1648  * S: if S=1 then scale (essentially index by sizeof(size))
1649  * Rt: register to transfer into/out of
1650  * Rn: address register or SP for base
1651  * Rm: offset register or ZR for offset
1652  */
1653 static void disas_ldst_reg_roffset(DisasContext *s, uint32_t insn)
1654 {
1655     int rt = extract32(insn, 0, 5);
1656     int rn = extract32(insn, 5, 5);
1657     int shift = extract32(insn, 12, 1);
1658     int rm = extract32(insn, 16, 5);
1659     int opc = extract32(insn, 22, 2);
1660     int opt = extract32(insn, 13, 3);
1661     int size = extract32(insn, 30, 2);
1662     bool is_signed = false;
1663     bool is_store = false;
1664     bool is_extended = false;
1665     bool is_vector = extract32(insn, 26, 1);
1666
1667     TCGv_i64 tcg_rm;
1668     TCGv_i64 tcg_addr;
1669
1670     if (extract32(opt, 1, 1) == 0) {
1671         unallocated_encoding(s);
1672         return;
1673     }
1674
1675     if (is_vector) {
1676         size |= (opc & 2) << 1;
1677         if (size > 4) {
1678             unallocated_encoding(s);
1679             return;
1680         }
1681         is_store = !extract32(opc, 0, 1);
1682     } else {
1683         if (size == 3 && opc == 2) {
1684             /* PRFM - prefetch */
1685             return;
1686         }
1687         if (opc == 3 && size > 1) {
1688             unallocated_encoding(s);
1689             return;
1690         }
1691         is_store = (opc == 0);
1692         is_signed = extract32(opc, 1, 1);
1693         is_extended = (size < 3) && extract32(opc, 0, 1);
1694     }
1695
1696     if (rn == 31) {
1697         gen_check_sp_alignment(s);
1698     }
1699     tcg_addr = read_cpu_reg_sp(s, rn, 1);
1700
1701     tcg_rm = read_cpu_reg(s, rm, 1);
1702     ext_and_shift_reg(tcg_rm, tcg_rm, opt, shift ? size : 0);
1703
1704     tcg_gen_add_i64(tcg_addr, tcg_addr, tcg_rm);
1705
1706     if (is_vector) {
1707         if (is_store) {
1708             do_fp_st(s, rt, tcg_addr, size);
1709         } else {
1710             do_fp_ld(s, rt, tcg_addr, size);
1711         }
1712     } else {
1713         TCGv_i64 tcg_rt = cpu_reg(s, rt);
1714         if (is_store) {
1715             do_gpr_st(s, tcg_rt, tcg_addr, size);
1716         } else {
1717             do_gpr_ld(s, tcg_rt, tcg_addr, size, is_signed, is_extended);
1718         }
1719     }
1720 }
1721
1722 /*
1723  * C3.3.13 Load/store (unsigned immediate)
1724  *
1725  * 31 30 29   27  26 25 24 23 22 21        10 9     5
1726  * +----+-------+---+-----+-----+------------+-------+------+
1727  * |size| 1 1 1 | V | 0 1 | opc |   imm12    |  Rn   |  Rt  |
1728  * +----+-------+---+-----+-----+------------+-------+------+
1729  *
1730  * For non-vector:
1731  *   size: 00-> byte, 01 -> 16 bit, 10 -> 32bit, 11 -> 64bit
1732  *   opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
1733  * For vector:
1734  *   size is opc<1>:size<1:0> so 100 -> 128 bit; 110 and 111 unallocated
1735  *   opc<0>: 0 -> store, 1 -> load
1736  * Rn: base address register (inc SP)
1737  * Rt: target register
1738  */
1739 static void disas_ldst_reg_unsigned_imm(DisasContext *s, uint32_t insn)
1740 {
1741     int rt = extract32(insn, 0, 5);
1742     int rn = extract32(insn, 5, 5);
1743     unsigned int imm12 = extract32(insn, 10, 12);
1744     bool is_vector = extract32(insn, 26, 1);
1745     int size = extract32(insn, 30, 2);
1746     int opc = extract32(insn, 22, 2);
1747     unsigned int offset;
1748
1749     TCGv_i64 tcg_addr;
1750
1751     bool is_store;
1752     bool is_signed = false;
1753     bool is_extended = false;
1754
1755     if (is_vector) {
1756         size |= (opc & 2) << 1;
1757         if (size > 4) {
1758             unallocated_encoding(s);
1759             return;
1760         }
1761         is_store = !extract32(opc, 0, 1);
1762     } else {
1763         if (size == 3 && opc == 2) {
1764             /* PRFM - prefetch */
1765             return;
1766         }
1767         if (opc == 3 && size > 1) {
1768             unallocated_encoding(s);
1769             return;
1770         }
1771         is_store = (opc == 0);
1772         is_signed = extract32(opc, 1, 1);
1773         is_extended = (size < 3) && extract32(opc, 0, 1);
1774     }
1775
1776     if (rn == 31) {
1777         gen_check_sp_alignment(s);
1778     }
1779     tcg_addr = read_cpu_reg_sp(s, rn, 1);
1780     offset = imm12 << size;
1781     tcg_gen_addi_i64(tcg_addr, tcg_addr, offset);
1782
1783     if (is_vector) {
1784         if (is_store) {
1785             do_fp_st(s, rt, tcg_addr, size);
1786         } else {
1787             do_fp_ld(s, rt, tcg_addr, size);
1788         }
1789     } else {
1790         TCGv_i64 tcg_rt = cpu_reg(s, rt);
1791         if (is_store) {
1792             do_gpr_st(s, tcg_rt, tcg_addr, size);
1793         } else {
1794             do_gpr_ld(s, tcg_rt, tcg_addr, size, is_signed, is_extended);
1795         }
1796     }
1797 }
1798
1799 /* Load/store register (immediate forms) */
1800 static void disas_ldst_reg_imm(DisasContext *s, uint32_t insn)
1801 {
1802     switch (extract32(insn, 10, 2)) {
1803     case 0: case 1: case 3:
1804         /* Load/store register (unscaled immediate) */
1805         /* Load/store immediate pre/post-indexed */
1806         disas_ldst_reg_imm9(s, insn);
1807         break;
1808     case 2:
1809         /* Load/store register unprivileged */
1810         unsupported_encoding(s, insn);
1811         break;
1812     default:
1813         unallocated_encoding(s);
1814         break;
1815     }
1816 }
1817
1818 /* Load/store register (all forms) */
1819 static void disas_ldst_reg(DisasContext *s, uint32_t insn)
1820 {
1821     switch (extract32(insn, 24, 2)) {
1822     case 0:
1823         if (extract32(insn, 21, 1) == 1 && extract32(insn, 10, 2) == 2) {
1824             disas_ldst_reg_roffset(s, insn);
1825         } else {
1826             disas_ldst_reg_imm(s, insn);
1827         }
1828         break;
1829     case 1:
1830         disas_ldst_reg_unsigned_imm(s, insn);
1831         break;
1832     default:
1833         unallocated_encoding(s);
1834         break;
1835     }
1836 }
1837
1838 /* AdvSIMD load/store multiple structures */
1839 static void disas_ldst_multiple_struct(DisasContext *s, uint32_t insn)
1840 {
1841     unsupported_encoding(s, insn);
1842 }
1843
1844 /* AdvSIMD load/store single structure */
1845 static void disas_ldst_single_struct(DisasContext *s, uint32_t insn)
1846 {
1847     unsupported_encoding(s, insn);
1848 }
1849
1850 /* C3.3 Loads and stores */
1851 static void disas_ldst(DisasContext *s, uint32_t insn)
1852 {
1853     switch (extract32(insn, 24, 6)) {
1854     case 0x08: /* Load/store exclusive */
1855         disas_ldst_excl(s, insn);
1856         break;
1857     case 0x18: case 0x1c: /* Load register (literal) */
1858         disas_ld_lit(s, insn);
1859         break;
1860     case 0x28: case 0x29:
1861     case 0x2c: case 0x2d: /* Load/store pair (all forms) */
1862         disas_ldst_pair(s, insn);
1863         break;
1864     case 0x38: case 0x39:
1865     case 0x3c: case 0x3d: /* Load/store register (all forms) */
1866         disas_ldst_reg(s, insn);
1867         break;
1868     case 0x0c: /* AdvSIMD load/store multiple structures */
1869         disas_ldst_multiple_struct(s, insn);
1870         break;
1871     case 0x0d: /* AdvSIMD load/store single structure */
1872         disas_ldst_single_struct(s, insn);
1873         break;
1874     default:
1875         unallocated_encoding(s);
1876         break;
1877     }
1878 }
1879
1880 /* C3.4.6 PC-rel. addressing
1881  *   31  30   29 28       24 23                5 4    0
1882  * +----+-------+-----------+-------------------+------+
1883  * | op | immlo | 1 0 0 0 0 |       immhi       |  Rd  |
1884  * +----+-------+-----------+-------------------+------+
1885  */
1886 static void disas_pc_rel_adr(DisasContext *s, uint32_t insn)
1887 {
1888     unsigned int page, rd;
1889     uint64_t base;
1890     int64_t offset;
1891
1892     page = extract32(insn, 31, 1);
1893     /* SignExtend(immhi:immlo) -> offset */
1894     offset = ((int64_t)sextract32(insn, 5, 19) << 2) | extract32(insn, 29, 2);
1895     rd = extract32(insn, 0, 5);
1896     base = s->pc - 4;
1897
1898     if (page) {
1899         /* ADRP (page based) */
1900         base &= ~0xfff;
1901         offset <<= 12;
1902     }
1903
1904     tcg_gen_movi_i64(cpu_reg(s, rd), base + offset);
1905 }
1906
1907 /*
1908  * C3.4.1 Add/subtract (immediate)
1909  *
1910  *  31 30 29 28       24 23 22 21         10 9   5 4   0
1911  * +--+--+--+-----------+-----+-------------+-----+-----+
1912  * |sf|op| S| 1 0 0 0 1 |shift|    imm12    |  Rn | Rd  |
1913  * +--+--+--+-----------+-----+-------------+-----+-----+
1914  *
1915  *    sf: 0 -> 32bit, 1 -> 64bit
1916  *    op: 0 -> add  , 1 -> sub
1917  *     S: 1 -> set flags
1918  * shift: 00 -> LSL imm by 0, 01 -> LSL imm by 12
1919  */
1920 static void disas_add_sub_imm(DisasContext *s, uint32_t insn)
1921 {
1922     int rd = extract32(insn, 0, 5);
1923     int rn = extract32(insn, 5, 5);
1924     uint64_t imm = extract32(insn, 10, 12);
1925     int shift = extract32(insn, 22, 2);
1926     bool setflags = extract32(insn, 29, 1);
1927     bool sub_op = extract32(insn, 30, 1);
1928     bool is_64bit = extract32(insn, 31, 1);
1929
1930     TCGv_i64 tcg_rn = cpu_reg_sp(s, rn);
1931     TCGv_i64 tcg_rd = setflags ? cpu_reg(s, rd) : cpu_reg_sp(s, rd);
1932     TCGv_i64 tcg_result;
1933
1934     switch (shift) {
1935     case 0x0:
1936         break;
1937     case 0x1:
1938         imm <<= 12;
1939         break;
1940     default:
1941         unallocated_encoding(s);
1942         return;
1943     }
1944
1945     tcg_result = tcg_temp_new_i64();
1946     if (!setflags) {
1947         if (sub_op) {
1948             tcg_gen_subi_i64(tcg_result, tcg_rn, imm);
1949         } else {
1950             tcg_gen_addi_i64(tcg_result, tcg_rn, imm);
1951         }
1952     } else {
1953         TCGv_i64 tcg_imm = tcg_const_i64(imm);
1954         if (sub_op) {
1955             gen_sub_CC(is_64bit, tcg_result, tcg_rn, tcg_imm);
1956         } else {
1957             gen_add_CC(is_64bit, tcg_result, tcg_rn, tcg_imm);
1958         }
1959         tcg_temp_free_i64(tcg_imm);
1960     }
1961
1962     if (is_64bit) {
1963         tcg_gen_mov_i64(tcg_rd, tcg_result);
1964     } else {
1965         tcg_gen_ext32u_i64(tcg_rd, tcg_result);
1966     }
1967
1968     tcg_temp_free_i64(tcg_result);
1969 }
1970
1971 /* The input should be a value in the bottom e bits (with higher
1972  * bits zero); returns that value replicated into every element
1973  * of size e in a 64 bit integer.
1974  */
1975 static uint64_t bitfield_replicate(uint64_t mask, unsigned int e)
1976 {
1977     assert(e != 0);
1978     while (e < 64) {
1979         mask |= mask << e;
1980         e *= 2;
1981     }
1982     return mask;
1983 }
1984
1985 /* Return a value with the bottom len bits set (where 0 < len <= 64) */
1986 static inline uint64_t bitmask64(unsigned int length)
1987 {
1988     assert(length > 0 && length <= 64);
1989     return ~0ULL >> (64 - length);
1990 }
1991
1992 /* Simplified variant of pseudocode DecodeBitMasks() for the case where we
1993  * only require the wmask. Returns false if the imms/immr/immn are a reserved
1994  * value (ie should cause a guest UNDEF exception), and true if they are
1995  * valid, in which case the decoded bit pattern is written to result.
1996  */
1997 static bool logic_imm_decode_wmask(uint64_t *result, unsigned int immn,
1998                                    unsigned int imms, unsigned int immr)
1999 {
2000     uint64_t mask;
2001     unsigned e, levels, s, r;
2002     int len;
2003
2004     assert(immn < 2 && imms < 64 && immr < 64);
2005
2006     /* The bit patterns we create here are 64 bit patterns which
2007      * are vectors of identical elements of size e = 2, 4, 8, 16, 32 or
2008      * 64 bits each. Each element contains the same value: a run
2009      * of between 1 and e-1 non-zero bits, rotated within the
2010      * element by between 0 and e-1 bits.
2011      *
2012      * The element size and run length are encoded into immn (1 bit)
2013      * and imms (6 bits) as follows:
2014      * 64 bit elements: immn = 1, imms = <length of run - 1>
2015      * 32 bit elements: immn = 0, imms = 0 : <length of run - 1>
2016      * 16 bit elements: immn = 0, imms = 10 : <length of run - 1>
2017      *  8 bit elements: immn = 0, imms = 110 : <length of run - 1>
2018      *  4 bit elements: immn = 0, imms = 1110 : <length of run - 1>
2019      *  2 bit elements: immn = 0, imms = 11110 : <length of run - 1>
2020      * Notice that immn = 0, imms = 11111x is the only combination
2021      * not covered by one of the above options; this is reserved.
2022      * Further, <length of run - 1> all-ones is a reserved pattern.
2023      *
2024      * In all cases the rotation is by immr % e (and immr is 6 bits).
2025      */
2026
2027     /* First determine the element size */
2028     len = 31 - clz32((immn << 6) | (~imms & 0x3f));
2029     if (len < 1) {
2030         /* This is the immn == 0, imms == 0x11111x case */
2031         return false;
2032     }
2033     e = 1 << len;
2034
2035     levels = e - 1;
2036     s = imms & levels;
2037     r = immr & levels;
2038
2039     if (s == levels) {
2040         /* <length of run - 1> mustn't be all-ones. */
2041         return false;
2042     }
2043
2044     /* Create the value of one element: s+1 set bits rotated
2045      * by r within the element (which is e bits wide)...
2046      */
2047     mask = bitmask64(s + 1);
2048     mask = (mask >> r) | (mask << (e - r));
2049     /* ...then replicate the element over the whole 64 bit value */
2050     mask = bitfield_replicate(mask, e);
2051     *result = mask;
2052     return true;
2053 }
2054
2055 /* C3.4.4 Logical (immediate)
2056  *   31  30 29 28         23 22  21  16 15  10 9    5 4    0
2057  * +----+-----+-------------+---+------+------+------+------+
2058  * | sf | opc | 1 0 0 1 0 0 | N | immr | imms |  Rn  |  Rd  |
2059  * +----+-----+-------------+---+------+------+------+------+
2060  */
2061 static void disas_logic_imm(DisasContext *s, uint32_t insn)
2062 {
2063     unsigned int sf, opc, is_n, immr, imms, rn, rd;
2064     TCGv_i64 tcg_rd, tcg_rn;
2065     uint64_t wmask;
2066     bool is_and = false;
2067
2068     sf = extract32(insn, 31, 1);
2069     opc = extract32(insn, 29, 2);
2070     is_n = extract32(insn, 22, 1);
2071     immr = extract32(insn, 16, 6);
2072     imms = extract32(insn, 10, 6);
2073     rn = extract32(insn, 5, 5);
2074     rd = extract32(insn, 0, 5);
2075
2076     if (!sf && is_n) {
2077         unallocated_encoding(s);
2078         return;
2079     }
2080
2081     if (opc == 0x3) { /* ANDS */
2082         tcg_rd = cpu_reg(s, rd);
2083     } else {
2084         tcg_rd = cpu_reg_sp(s, rd);
2085     }
2086     tcg_rn = cpu_reg(s, rn);
2087
2088     if (!logic_imm_decode_wmask(&wmask, is_n, imms, immr)) {
2089         /* some immediate field values are reserved */
2090         unallocated_encoding(s);
2091         return;
2092     }
2093
2094     if (!sf) {
2095         wmask &= 0xffffffff;
2096     }
2097
2098     switch (opc) {
2099     case 0x3: /* ANDS */
2100     case 0x0: /* AND */
2101         tcg_gen_andi_i64(tcg_rd, tcg_rn, wmask);
2102         is_and = true;
2103         break;
2104     case 0x1: /* ORR */
2105         tcg_gen_ori_i64(tcg_rd, tcg_rn, wmask);
2106         break;
2107     case 0x2: /* EOR */
2108         tcg_gen_xori_i64(tcg_rd, tcg_rn, wmask);
2109         break;
2110     default:
2111         assert(FALSE); /* must handle all above */
2112         break;
2113     }
2114
2115     if (!sf && !is_and) {
2116         /* zero extend final result; we know we can skip this for AND
2117          * since the immediate had the high 32 bits clear.
2118          */
2119         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
2120     }
2121
2122     if (opc == 3) { /* ANDS */
2123         gen_logic_CC(sf, tcg_rd);
2124     }
2125 }
2126
2127 /*
2128  * C3.4.5 Move wide (immediate)
2129  *
2130  *  31 30 29 28         23 22 21 20             5 4    0
2131  * +--+-----+-------------+-----+----------------+------+
2132  * |sf| opc | 1 0 0 1 0 1 |  hw |  imm16         |  Rd  |
2133  * +--+-----+-------------+-----+----------------+------+
2134  *
2135  * sf: 0 -> 32 bit, 1 -> 64 bit
2136  * opc: 00 -> N, 10 -> Z, 11 -> K
2137  * hw: shift/16 (0,16, and sf only 32, 48)
2138  */
2139 static void disas_movw_imm(DisasContext *s, uint32_t insn)
2140 {
2141     int rd = extract32(insn, 0, 5);
2142     uint64_t imm = extract32(insn, 5, 16);
2143     int sf = extract32(insn, 31, 1);
2144     int opc = extract32(insn, 29, 2);
2145     int pos = extract32(insn, 21, 2) << 4;
2146     TCGv_i64 tcg_rd = cpu_reg(s, rd);
2147     TCGv_i64 tcg_imm;
2148
2149     if (!sf && (pos >= 32)) {
2150         unallocated_encoding(s);
2151         return;
2152     }
2153
2154     switch (opc) {
2155     case 0: /* MOVN */
2156     case 2: /* MOVZ */
2157         imm <<= pos;
2158         if (opc == 0) {
2159             imm = ~imm;
2160         }
2161         if (!sf) {
2162             imm &= 0xffffffffu;
2163         }
2164         tcg_gen_movi_i64(tcg_rd, imm);
2165         break;
2166     case 3: /* MOVK */
2167         tcg_imm = tcg_const_i64(imm);
2168         tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_imm, pos, 16);
2169         tcg_temp_free_i64(tcg_imm);
2170         if (!sf) {
2171             tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
2172         }
2173         break;
2174     default:
2175         unallocated_encoding(s);
2176         break;
2177     }
2178 }
2179
2180 /* C3.4.2 Bitfield
2181  *   31  30 29 28         23 22  21  16 15  10 9    5 4    0
2182  * +----+-----+-------------+---+------+------+------+------+
2183  * | sf | opc | 1 0 0 1 1 0 | N | immr | imms |  Rn  |  Rd  |
2184  * +----+-----+-------------+---+------+------+------+------+
2185  */
2186 static void disas_bitfield(DisasContext *s, uint32_t insn)
2187 {
2188     unsigned int sf, n, opc, ri, si, rn, rd, bitsize, pos, len;
2189     TCGv_i64 tcg_rd, tcg_tmp;
2190
2191     sf = extract32(insn, 31, 1);
2192     opc = extract32(insn, 29, 2);
2193     n = extract32(insn, 22, 1);
2194     ri = extract32(insn, 16, 6);
2195     si = extract32(insn, 10, 6);
2196     rn = extract32(insn, 5, 5);
2197     rd = extract32(insn, 0, 5);
2198     bitsize = sf ? 64 : 32;
2199
2200     if (sf != n || ri >= bitsize || si >= bitsize || opc > 2) {
2201         unallocated_encoding(s);
2202         return;
2203     }
2204
2205     tcg_rd = cpu_reg(s, rd);
2206     tcg_tmp = read_cpu_reg(s, rn, sf);
2207
2208     /* OPTME: probably worth recognizing common cases of ext{8,16,32}{u,s} */
2209
2210     if (opc != 1) { /* SBFM or UBFM */
2211         tcg_gen_movi_i64(tcg_rd, 0);
2212     }
2213
2214     /* do the bit move operation */
2215     if (si >= ri) {
2216         /* Wd<s-r:0> = Wn<s:r> */
2217         tcg_gen_shri_i64(tcg_tmp, tcg_tmp, ri);
2218         pos = 0;
2219         len = (si - ri) + 1;
2220     } else {
2221         /* Wd<32+s-r,32-r> = Wn<s:0> */
2222         pos = bitsize - ri;
2223         len = si + 1;
2224     }
2225
2226     tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, pos, len);
2227
2228     if (opc == 0) { /* SBFM - sign extend the destination field */
2229         tcg_gen_shli_i64(tcg_rd, tcg_rd, 64 - (pos + len));
2230         tcg_gen_sari_i64(tcg_rd, tcg_rd, 64 - (pos + len));
2231     }
2232
2233     if (!sf) { /* zero extend final result */
2234         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
2235     }
2236 }
2237
2238 /* C3.4.3 Extract
2239  *   31  30  29 28         23 22   21  20  16 15    10 9    5 4    0
2240  * +----+------+-------------+---+----+------+--------+------+------+
2241  * | sf | op21 | 1 0 0 1 1 1 | N | o0 |  Rm  |  imms  |  Rn  |  Rd  |
2242  * +----+------+-------------+---+----+------+--------+------+------+
2243  */
2244 static void disas_extract(DisasContext *s, uint32_t insn)
2245 {
2246     unsigned int sf, n, rm, imm, rn, rd, bitsize, op21, op0;
2247
2248     sf = extract32(insn, 31, 1);
2249     n = extract32(insn, 22, 1);
2250     rm = extract32(insn, 16, 5);
2251     imm = extract32(insn, 10, 6);
2252     rn = extract32(insn, 5, 5);
2253     rd = extract32(insn, 0, 5);
2254     op21 = extract32(insn, 29, 2);
2255     op0 = extract32(insn, 21, 1);
2256     bitsize = sf ? 64 : 32;
2257
2258     if (sf != n || op21 || op0 || imm >= bitsize) {
2259         unallocated_encoding(s);
2260     } else {
2261         TCGv_i64 tcg_rd, tcg_rm, tcg_rn;
2262
2263         tcg_rd = cpu_reg(s, rd);
2264
2265         if (imm) {
2266             /* OPTME: we can special case rm==rn as a rotate */
2267             tcg_rm = read_cpu_reg(s, rm, sf);
2268             tcg_rn = read_cpu_reg(s, rn, sf);
2269             tcg_gen_shri_i64(tcg_rm, tcg_rm, imm);
2270             tcg_gen_shli_i64(tcg_rn, tcg_rn, bitsize - imm);
2271             tcg_gen_or_i64(tcg_rd, tcg_rm, tcg_rn);
2272             if (!sf) {
2273                 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
2274             }
2275         } else {
2276             /* tcg shl_i32/shl_i64 is undefined for 32/64 bit shifts,
2277              * so an extract from bit 0 is a special case.
2278              */
2279             if (sf) {
2280                 tcg_gen_mov_i64(tcg_rd, cpu_reg(s, rm));
2281             } else {
2282                 tcg_gen_ext32u_i64(tcg_rd, cpu_reg(s, rm));
2283             }
2284         }
2285
2286     }
2287 }
2288
2289 /* C3.4 Data processing - immediate */
2290 static void disas_data_proc_imm(DisasContext *s, uint32_t insn)
2291 {
2292     switch (extract32(insn, 23, 6)) {
2293     case 0x20: case 0x21: /* PC-rel. addressing */
2294         disas_pc_rel_adr(s, insn);
2295         break;
2296     case 0x22: case 0x23: /* Add/subtract (immediate) */
2297         disas_add_sub_imm(s, insn);
2298         break;
2299     case 0x24: /* Logical (immediate) */
2300         disas_logic_imm(s, insn);
2301         break;
2302     case 0x25: /* Move wide (immediate) */
2303         disas_movw_imm(s, insn);
2304         break;
2305     case 0x26: /* Bitfield */
2306         disas_bitfield(s, insn);
2307         break;
2308     case 0x27: /* Extract */
2309         disas_extract(s, insn);
2310         break;
2311     default:
2312         unallocated_encoding(s);
2313         break;
2314     }
2315 }
2316
2317 /* Shift a TCGv src by TCGv shift_amount, put result in dst.
2318  * Note that it is the caller's responsibility to ensure that the
2319  * shift amount is in range (ie 0..31 or 0..63) and provide the ARM
2320  * mandated semantics for out of range shifts.
2321  */
2322 static void shift_reg(TCGv_i64 dst, TCGv_i64 src, int sf,
2323                       enum a64_shift_type shift_type, TCGv_i64 shift_amount)
2324 {
2325     switch (shift_type) {
2326     case A64_SHIFT_TYPE_LSL:
2327         tcg_gen_shl_i64(dst, src, shift_amount);
2328         break;
2329     case A64_SHIFT_TYPE_LSR:
2330         tcg_gen_shr_i64(dst, src, shift_amount);
2331         break;
2332     case A64_SHIFT_TYPE_ASR:
2333         if (!sf) {
2334             tcg_gen_ext32s_i64(dst, src);
2335         }
2336         tcg_gen_sar_i64(dst, sf ? src : dst, shift_amount);
2337         break;
2338     case A64_SHIFT_TYPE_ROR:
2339         if (sf) {
2340             tcg_gen_rotr_i64(dst, src, shift_amount);
2341         } else {
2342             TCGv_i32 t0, t1;
2343             t0 = tcg_temp_new_i32();
2344             t1 = tcg_temp_new_i32();
2345             tcg_gen_trunc_i64_i32(t0, src);
2346             tcg_gen_trunc_i64_i32(t1, shift_amount);
2347             tcg_gen_rotr_i32(t0, t0, t1);
2348             tcg_gen_extu_i32_i64(dst, t0);
2349             tcg_temp_free_i32(t0);
2350             tcg_temp_free_i32(t1);
2351         }
2352         break;
2353     default:
2354         assert(FALSE); /* all shift types should be handled */
2355         break;
2356     }
2357
2358     if (!sf) { /* zero extend final result */
2359         tcg_gen_ext32u_i64(dst, dst);
2360     }
2361 }
2362
2363 /* Shift a TCGv src by immediate, put result in dst.
2364  * The shift amount must be in range (this should always be true as the
2365  * relevant instructions will UNDEF on bad shift immediates).
2366  */
2367 static void shift_reg_imm(TCGv_i64 dst, TCGv_i64 src, int sf,
2368                           enum a64_shift_type shift_type, unsigned int shift_i)
2369 {
2370     assert(shift_i < (sf ? 64 : 32));
2371
2372     if (shift_i == 0) {
2373         tcg_gen_mov_i64(dst, src);
2374     } else {
2375         TCGv_i64 shift_const;
2376
2377         shift_const = tcg_const_i64(shift_i);
2378         shift_reg(dst, src, sf, shift_type, shift_const);
2379         tcg_temp_free_i64(shift_const);
2380     }
2381 }
2382
2383 /* C3.5.10 Logical (shifted register)
2384  *   31  30 29 28       24 23   22 21  20  16 15    10 9    5 4    0
2385  * +----+-----+-----------+-------+---+------+--------+------+------+
2386  * | sf | opc | 0 1 0 1 0 | shift | N |  Rm  |  imm6  |  Rn  |  Rd  |
2387  * +----+-----+-----------+-------+---+------+--------+------+------+
2388  */
2389 static void disas_logic_reg(DisasContext *s, uint32_t insn)
2390 {
2391     TCGv_i64 tcg_rd, tcg_rn, tcg_rm;
2392     unsigned int sf, opc, shift_type, invert, rm, shift_amount, rn, rd;
2393
2394     sf = extract32(insn, 31, 1);
2395     opc = extract32(insn, 29, 2);
2396     shift_type = extract32(insn, 22, 2);
2397     invert = extract32(insn, 21, 1);
2398     rm = extract32(insn, 16, 5);
2399     shift_amount = extract32(insn, 10, 6);
2400     rn = extract32(insn, 5, 5);
2401     rd = extract32(insn, 0, 5);
2402
2403     if (!sf && (shift_amount & (1 << 5))) {
2404         unallocated_encoding(s);
2405         return;
2406     }
2407
2408     tcg_rd = cpu_reg(s, rd);
2409
2410     if (opc == 1 && shift_amount == 0 && shift_type == 0 && rn == 31) {
2411         /* Unshifted ORR and ORN with WZR/XZR is the standard encoding for
2412          * register-register MOV and MVN, so it is worth special casing.
2413          */
2414         tcg_rm = cpu_reg(s, rm);
2415         if (invert) {
2416             tcg_gen_not_i64(tcg_rd, tcg_rm);
2417             if (!sf) {
2418                 tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
2419             }
2420         } else {
2421             if (sf) {
2422                 tcg_gen_mov_i64(tcg_rd, tcg_rm);
2423             } else {
2424                 tcg_gen_ext32u_i64(tcg_rd, tcg_rm);
2425             }
2426         }
2427         return;
2428     }
2429
2430     tcg_rm = read_cpu_reg(s, rm, sf);
2431
2432     if (shift_amount) {
2433         shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, shift_amount);
2434     }
2435
2436     tcg_rn = cpu_reg(s, rn);
2437
2438     switch (opc | (invert << 2)) {
2439     case 0: /* AND */
2440     case 3: /* ANDS */
2441         tcg_gen_and_i64(tcg_rd, tcg_rn, tcg_rm);
2442         break;
2443     case 1: /* ORR */
2444         tcg_gen_or_i64(tcg_rd, tcg_rn, tcg_rm);
2445         break;
2446     case 2: /* EOR */
2447         tcg_gen_xor_i64(tcg_rd, tcg_rn, tcg_rm);
2448         break;
2449     case 4: /* BIC */
2450     case 7: /* BICS */
2451         tcg_gen_andc_i64(tcg_rd, tcg_rn, tcg_rm);
2452         break;
2453     case 5: /* ORN */
2454         tcg_gen_orc_i64(tcg_rd, tcg_rn, tcg_rm);
2455         break;
2456     case 6: /* EON */
2457         tcg_gen_eqv_i64(tcg_rd, tcg_rn, tcg_rm);
2458         break;
2459     default:
2460         assert(FALSE);
2461         break;
2462     }
2463
2464     if (!sf) {
2465         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
2466     }
2467
2468     if (opc == 3) {
2469         gen_logic_CC(sf, tcg_rd);
2470     }
2471 }
2472
2473 /*
2474  * C3.5.1 Add/subtract (extended register)
2475  *
2476  *  31|30|29|28       24|23 22|21|20   16|15  13|12  10|9  5|4  0|
2477  * +--+--+--+-----------+-----+--+-------+------+------+----+----+
2478  * |sf|op| S| 0 1 0 1 1 | opt | 1|  Rm   |option| imm3 | Rn | Rd |
2479  * +--+--+--+-----------+-----+--+-------+------+------+----+----+
2480  *
2481  *  sf: 0 -> 32bit, 1 -> 64bit
2482  *  op: 0 -> add  , 1 -> sub
2483  *   S: 1 -> set flags
2484  * opt: 00
2485  * option: extension type (see DecodeRegExtend)
2486  * imm3: optional shift to Rm
2487  *
2488  * Rd = Rn + LSL(extend(Rm), amount)
2489  */
2490 static void disas_add_sub_ext_reg(DisasContext *s, uint32_t insn)
2491 {
2492     int rd = extract32(insn, 0, 5);
2493     int rn = extract32(insn, 5, 5);
2494     int imm3 = extract32(insn, 10, 3);
2495     int option = extract32(insn, 13, 3);
2496     int rm = extract32(insn, 16, 5);
2497     bool setflags = extract32(insn, 29, 1);
2498     bool sub_op = extract32(insn, 30, 1);
2499     bool sf = extract32(insn, 31, 1);
2500
2501     TCGv_i64 tcg_rm, tcg_rn; /* temps */
2502     TCGv_i64 tcg_rd;
2503     TCGv_i64 tcg_result;
2504
2505     if (imm3 > 4) {
2506         unallocated_encoding(s);
2507         return;
2508     }
2509
2510     /* non-flag setting ops may use SP */
2511     if (!setflags) {
2512         tcg_rn = read_cpu_reg_sp(s, rn, sf);
2513         tcg_rd = cpu_reg_sp(s, rd);
2514     } else {
2515         tcg_rn = read_cpu_reg(s, rn, sf);
2516         tcg_rd = cpu_reg(s, rd);
2517     }
2518
2519     tcg_rm = read_cpu_reg(s, rm, sf);
2520     ext_and_shift_reg(tcg_rm, tcg_rm, option, imm3);
2521
2522     tcg_result = tcg_temp_new_i64();
2523
2524     if (!setflags) {
2525         if (sub_op) {
2526             tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
2527         } else {
2528             tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
2529         }
2530     } else {
2531         if (sub_op) {
2532             gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
2533         } else {
2534             gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
2535         }
2536     }
2537
2538     if (sf) {
2539         tcg_gen_mov_i64(tcg_rd, tcg_result);
2540     } else {
2541         tcg_gen_ext32u_i64(tcg_rd, tcg_result);
2542     }
2543
2544     tcg_temp_free_i64(tcg_result);
2545 }
2546
2547 /*
2548  * C3.5.2 Add/subtract (shifted register)
2549  *
2550  *  31 30 29 28       24 23 22 21 20   16 15     10 9    5 4    0
2551  * +--+--+--+-----------+-----+--+-------+---------+------+------+
2552  * |sf|op| S| 0 1 0 1 1 |shift| 0|  Rm   |  imm6   |  Rn  |  Rd  |
2553  * +--+--+--+-----------+-----+--+-------+---------+------+------+
2554  *
2555  *    sf: 0 -> 32bit, 1 -> 64bit
2556  *    op: 0 -> add  , 1 -> sub
2557  *     S: 1 -> set flags
2558  * shift: 00 -> LSL, 01 -> LSR, 10 -> ASR, 11 -> RESERVED
2559  *  imm6: Shift amount to apply to Rm before the add/sub
2560  */
2561 static void disas_add_sub_reg(DisasContext *s, uint32_t insn)
2562 {
2563     int rd = extract32(insn, 0, 5);
2564     int rn = extract32(insn, 5, 5);
2565     int imm6 = extract32(insn, 10, 6);
2566     int rm = extract32(insn, 16, 5);
2567     int shift_type = extract32(insn, 22, 2);
2568     bool setflags = extract32(insn, 29, 1);
2569     bool sub_op = extract32(insn, 30, 1);
2570     bool sf = extract32(insn, 31, 1);
2571
2572     TCGv_i64 tcg_rd = cpu_reg(s, rd);
2573     TCGv_i64 tcg_rn, tcg_rm;
2574     TCGv_i64 tcg_result;
2575
2576     if ((shift_type == 3) || (!sf && (imm6 > 31))) {
2577         unallocated_encoding(s);
2578         return;
2579     }
2580
2581     tcg_rn = read_cpu_reg(s, rn, sf);
2582     tcg_rm = read_cpu_reg(s, rm, sf);
2583
2584     shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, imm6);
2585
2586     tcg_result = tcg_temp_new_i64();
2587
2588     if (!setflags) {
2589         if (sub_op) {
2590             tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
2591         } else {
2592             tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
2593         }
2594     } else {
2595         if (sub_op) {
2596             gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
2597         } else {
2598             gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
2599         }
2600     }
2601
2602     if (sf) {
2603         tcg_gen_mov_i64(tcg_rd, tcg_result);
2604     } else {
2605         tcg_gen_ext32u_i64(tcg_rd, tcg_result);
2606     }
2607
2608     tcg_temp_free_i64(tcg_result);
2609 }
2610
2611 /* C3.5.9 Data-processing (3 source)
2612
2613    31 30  29 28       24 23 21  20  16  15  14  10 9    5 4    0
2614   +--+------+-----------+------+------+----+------+------+------+
2615   |sf| op54 | 1 1 0 1 1 | op31 |  Rm  | o0 |  Ra  |  Rn  |  Rd  |
2616   +--+------+-----------+------+------+----+------+------+------+
2617
2618  */
2619 static void disas_data_proc_3src(DisasContext *s, uint32_t insn)
2620 {
2621     int rd = extract32(insn, 0, 5);
2622     int rn = extract32(insn, 5, 5);
2623     int ra = extract32(insn, 10, 5);
2624     int rm = extract32(insn, 16, 5);
2625     int op_id = (extract32(insn, 29, 3) << 4) |
2626         (extract32(insn, 21, 3) << 1) |
2627         extract32(insn, 15, 1);
2628     bool sf = extract32(insn, 31, 1);
2629     bool is_sub = extract32(op_id, 0, 1);
2630     bool is_high = extract32(op_id, 2, 1);
2631     bool is_signed = false;
2632     TCGv_i64 tcg_op1;
2633     TCGv_i64 tcg_op2;
2634     TCGv_i64 tcg_tmp;
2635
2636     /* Note that op_id is sf:op54:op31:o0 so it includes the 32/64 size flag */
2637     switch (op_id) {
2638     case 0x42: /* SMADDL */
2639     case 0x43: /* SMSUBL */
2640     case 0x44: /* SMULH */
2641         is_signed = true;
2642         break;
2643     case 0x0: /* MADD (32bit) */
2644     case 0x1: /* MSUB (32bit) */
2645     case 0x40: /* MADD (64bit) */
2646     case 0x41: /* MSUB (64bit) */
2647     case 0x4a: /* UMADDL */
2648     case 0x4b: /* UMSUBL */
2649     case 0x4c: /* UMULH */
2650         break;
2651     default:
2652         unallocated_encoding(s);
2653         return;
2654     }
2655
2656     if (is_high) {
2657         TCGv_i64 low_bits = tcg_temp_new_i64(); /* low bits discarded */
2658         TCGv_i64 tcg_rd = cpu_reg(s, rd);
2659         TCGv_i64 tcg_rn = cpu_reg(s, rn);
2660         TCGv_i64 tcg_rm = cpu_reg(s, rm);
2661
2662         if (is_signed) {
2663             tcg_gen_muls2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
2664         } else {
2665             tcg_gen_mulu2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
2666         }
2667
2668         tcg_temp_free_i64(low_bits);
2669         return;
2670     }
2671
2672     tcg_op1 = tcg_temp_new_i64();
2673     tcg_op2 = tcg_temp_new_i64();
2674     tcg_tmp = tcg_temp_new_i64();
2675
2676     if (op_id < 0x42) {
2677         tcg_gen_mov_i64(tcg_op1, cpu_reg(s, rn));
2678         tcg_gen_mov_i64(tcg_op2, cpu_reg(s, rm));
2679     } else {
2680         if (is_signed) {
2681             tcg_gen_ext32s_i64(tcg_op1, cpu_reg(s, rn));
2682             tcg_gen_ext32s_i64(tcg_op2, cpu_reg(s, rm));
2683         } else {
2684             tcg_gen_ext32u_i64(tcg_op1, cpu_reg(s, rn));
2685             tcg_gen_ext32u_i64(tcg_op2, cpu_reg(s, rm));
2686         }
2687     }
2688
2689     if (ra == 31 && !is_sub) {
2690         /* Special-case MADD with rA == XZR; it is the standard MUL alias */
2691         tcg_gen_mul_i64(cpu_reg(s, rd), tcg_op1, tcg_op2);
2692     } else {
2693         tcg_gen_mul_i64(tcg_tmp, tcg_op1, tcg_op2);
2694         if (is_sub) {
2695             tcg_gen_sub_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
2696         } else {
2697             tcg_gen_add_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
2698         }
2699     }
2700
2701     if (!sf) {
2702         tcg_gen_ext32u_i64(cpu_reg(s, rd), cpu_reg(s, rd));
2703     }
2704
2705     tcg_temp_free_i64(tcg_op1);
2706     tcg_temp_free_i64(tcg_op2);
2707     tcg_temp_free_i64(tcg_tmp);
2708 }
2709
2710 /* C3.5.3 - Add/subtract (with carry)
2711  *  31 30 29 28 27 26 25 24 23 22 21  20  16  15   10  9    5 4   0
2712  * +--+--+--+------------------------+------+---------+------+-----+
2713  * |sf|op| S| 1  1  0  1  0  0  0  0 |  rm  | opcode2 |  Rn  |  Rd |
2714  * +--+--+--+------------------------+------+---------+------+-----+
2715  *                                            [000000]
2716  */
2717
2718 static void disas_adc_sbc(DisasContext *s, uint32_t insn)
2719 {
2720     unsigned int sf, op, setflags, rm, rn, rd;
2721     TCGv_i64 tcg_y, tcg_rn, tcg_rd;
2722
2723     if (extract32(insn, 10, 6) != 0) {
2724         unallocated_encoding(s);
2725         return;
2726     }
2727
2728     sf = extract32(insn, 31, 1);
2729     op = extract32(insn, 30, 1);
2730     setflags = extract32(insn, 29, 1);
2731     rm = extract32(insn, 16, 5);
2732     rn = extract32(insn, 5, 5);
2733     rd = extract32(insn, 0, 5);
2734
2735     tcg_rd = cpu_reg(s, rd);
2736     tcg_rn = cpu_reg(s, rn);
2737
2738     if (op) {
2739         tcg_y = new_tmp_a64(s);
2740         tcg_gen_not_i64(tcg_y, cpu_reg(s, rm));
2741     } else {
2742         tcg_y = cpu_reg(s, rm);
2743     }
2744
2745     if (setflags) {
2746         gen_adc_CC(sf, tcg_rd, tcg_rn, tcg_y);
2747     } else {
2748         gen_adc(sf, tcg_rd, tcg_rn, tcg_y);
2749     }
2750 }
2751
2752 /* C3.5.4 - C3.5.5 Conditional compare (immediate / register)
2753  *  31 30 29 28 27 26 25 24 23 22 21  20    16 15  12  11  10  9   5  4 3   0
2754  * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
2755  * |sf|op| S| 1  1  0  1  0  0  1  0 |imm5/rm | cond |i/r |o2|  Rn  |o3|nzcv |
2756  * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
2757  *        [1]                             y                [0]       [0]
2758  */
2759 static void disas_cc(DisasContext *s, uint32_t insn)
2760 {
2761     unsigned int sf, op, y, cond, rn, nzcv, is_imm;
2762     int label_continue = -1;
2763     TCGv_i64 tcg_tmp, tcg_y, tcg_rn;
2764
2765     if (!extract32(insn, 29, 1)) {
2766         unallocated_encoding(s);
2767         return;
2768     }
2769     if (insn & (1 << 10 | 1 << 4)) {
2770         unallocated_encoding(s);
2771         return;
2772     }
2773     sf = extract32(insn, 31, 1);
2774     op = extract32(insn, 30, 1);
2775     is_imm = extract32(insn, 11, 1);
2776     y = extract32(insn, 16, 5); /* y = rm (reg) or imm5 (imm) */
2777     cond = extract32(insn, 12, 4);
2778     rn = extract32(insn, 5, 5);
2779     nzcv = extract32(insn, 0, 4);
2780
2781     if (cond < 0x0e) { /* not always */
2782         int label_match = gen_new_label();
2783         label_continue = gen_new_label();
2784         arm_gen_test_cc(cond, label_match);
2785         /* nomatch: */
2786         tcg_tmp = tcg_temp_new_i64();
2787         tcg_gen_movi_i64(tcg_tmp, nzcv << 28);
2788         gen_set_nzcv(tcg_tmp);
2789         tcg_temp_free_i64(tcg_tmp);
2790         tcg_gen_br(label_continue);
2791         gen_set_label(label_match);
2792     }
2793     /* match, or condition is always */
2794     if (is_imm) {
2795         tcg_y = new_tmp_a64(s);
2796         tcg_gen_movi_i64(tcg_y, y);
2797     } else {
2798         tcg_y = cpu_reg(s, y);
2799     }
2800     tcg_rn = cpu_reg(s, rn);
2801
2802     tcg_tmp = tcg_temp_new_i64();
2803     if (op) {
2804         gen_sub_CC(sf, tcg_tmp, tcg_rn, tcg_y);
2805     } else {
2806         gen_add_CC(sf, tcg_tmp, tcg_rn, tcg_y);
2807     }
2808     tcg_temp_free_i64(tcg_tmp);
2809
2810     if (cond < 0x0e) { /* continue */
2811         gen_set_label(label_continue);
2812     }
2813 }
2814
2815 /* C3.5.6 Conditional select
2816  *   31   30  29  28             21 20  16 15  12 11 10 9    5 4    0
2817  * +----+----+---+-----------------+------+------+-----+------+------+
2818  * | sf | op | S | 1 1 0 1 0 1 0 0 |  Rm  | cond | op2 |  Rn  |  Rd  |
2819  * +----+----+---+-----------------+------+------+-----+------+------+
2820  */
2821 static void disas_cond_select(DisasContext *s, uint32_t insn)
2822 {
2823     unsigned int sf, else_inv, rm, cond, else_inc, rn, rd;
2824     TCGv_i64 tcg_rd, tcg_src;
2825
2826     if (extract32(insn, 29, 1) || extract32(insn, 11, 1)) {
2827         /* S == 1 or op2<1> == 1 */
2828         unallocated_encoding(s);
2829         return;
2830     }
2831     sf = extract32(insn, 31, 1);
2832     else_inv = extract32(insn, 30, 1);
2833     rm = extract32(insn, 16, 5);
2834     cond = extract32(insn, 12, 4);
2835     else_inc = extract32(insn, 10, 1);
2836     rn = extract32(insn, 5, 5);
2837     rd = extract32(insn, 0, 5);
2838
2839     if (rd == 31) {
2840         /* silly no-op write; until we use movcond we must special-case
2841          * this to avoid a dead temporary across basic blocks.
2842          */
2843         return;
2844     }
2845
2846     tcg_rd = cpu_reg(s, rd);
2847
2848     if (cond >= 0x0e) { /* condition "always" */
2849         tcg_src = read_cpu_reg(s, rn, sf);
2850         tcg_gen_mov_i64(tcg_rd, tcg_src);
2851     } else {
2852         /* OPTME: we could use movcond here, at the cost of duplicating
2853          * a lot of the arm_gen_test_cc() logic.
2854          */
2855         int label_match = gen_new_label();
2856         int label_continue = gen_new_label();
2857
2858         arm_gen_test_cc(cond, label_match);
2859         /* nomatch: */
2860         tcg_src = cpu_reg(s, rm);
2861
2862         if (else_inv && else_inc) {
2863             tcg_gen_neg_i64(tcg_rd, tcg_src);
2864         } else if (else_inv) {
2865             tcg_gen_not_i64(tcg_rd, tcg_src);
2866         } else if (else_inc) {
2867             tcg_gen_addi_i64(tcg_rd, tcg_src, 1);
2868         } else {
2869             tcg_gen_mov_i64(tcg_rd, tcg_src);
2870         }
2871         if (!sf) {
2872             tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
2873         }
2874         tcg_gen_br(label_continue);
2875         /* match: */
2876         gen_set_label(label_match);
2877         tcg_src = read_cpu_reg(s, rn, sf);
2878         tcg_gen_mov_i64(tcg_rd, tcg_src);
2879         /* continue: */
2880         gen_set_label(label_continue);
2881     }
2882 }
2883
2884 static void handle_clz(DisasContext *s, unsigned int sf,
2885                        unsigned int rn, unsigned int rd)
2886 {
2887     TCGv_i64 tcg_rd, tcg_rn;
2888     tcg_rd = cpu_reg(s, rd);
2889     tcg_rn = cpu_reg(s, rn);
2890
2891     if (sf) {
2892         gen_helper_clz64(tcg_rd, tcg_rn);
2893     } else {
2894         TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
2895         tcg_gen_trunc_i64_i32(tcg_tmp32, tcg_rn);
2896         gen_helper_clz(tcg_tmp32, tcg_tmp32);
2897         tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
2898         tcg_temp_free_i32(tcg_tmp32);
2899     }
2900 }
2901
2902 static void handle_cls(DisasContext *s, unsigned int sf,
2903                        unsigned int rn, unsigned int rd)
2904 {
2905     TCGv_i64 tcg_rd, tcg_rn;
2906     tcg_rd = cpu_reg(s, rd);
2907     tcg_rn = cpu_reg(s, rn);
2908
2909     if (sf) {
2910         gen_helper_cls64(tcg_rd, tcg_rn);
2911     } else {
2912         TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
2913         tcg_gen_trunc_i64_i32(tcg_tmp32, tcg_rn);
2914         gen_helper_cls32(tcg_tmp32, tcg_tmp32);
2915         tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
2916         tcg_temp_free_i32(tcg_tmp32);
2917     }
2918 }
2919
2920 static void handle_rbit(DisasContext *s, unsigned int sf,
2921                         unsigned int rn, unsigned int rd)
2922 {
2923     TCGv_i64 tcg_rd, tcg_rn;
2924     tcg_rd = cpu_reg(s, rd);
2925     tcg_rn = cpu_reg(s, rn);
2926
2927     if (sf) {
2928         gen_helper_rbit64(tcg_rd, tcg_rn);
2929     } else {
2930         TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
2931         tcg_gen_trunc_i64_i32(tcg_tmp32, tcg_rn);
2932         gen_helper_rbit(tcg_tmp32, tcg_tmp32);
2933         tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
2934         tcg_temp_free_i32(tcg_tmp32);
2935     }
2936 }
2937
2938 /* C5.6.149 REV with sf==1, opcode==3 ("REV64") */
2939 static void handle_rev64(DisasContext *s, unsigned int sf,
2940                          unsigned int rn, unsigned int rd)
2941 {
2942     if (!sf) {
2943         unallocated_encoding(s);
2944         return;
2945     }
2946     tcg_gen_bswap64_i64(cpu_reg(s, rd), cpu_reg(s, rn));
2947 }
2948
2949 /* C5.6.149 REV with sf==0, opcode==2
2950  * C5.6.151 REV32 (sf==1, opcode==2)
2951  */
2952 static void handle_rev32(DisasContext *s, unsigned int sf,
2953                          unsigned int rn, unsigned int rd)
2954 {
2955     TCGv_i64 tcg_rd = cpu_reg(s, rd);
2956
2957     if (sf) {
2958         TCGv_i64 tcg_tmp = tcg_temp_new_i64();
2959         TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
2960
2961         /* bswap32_i64 requires zero high word */
2962         tcg_gen_ext32u_i64(tcg_tmp, tcg_rn);
2963         tcg_gen_bswap32_i64(tcg_rd, tcg_tmp);
2964         tcg_gen_shri_i64(tcg_tmp, tcg_rn, 32);
2965         tcg_gen_bswap32_i64(tcg_tmp, tcg_tmp);
2966         tcg_gen_concat32_i64(tcg_rd, tcg_rd, tcg_tmp);
2967
2968         tcg_temp_free_i64(tcg_tmp);
2969     } else {
2970         tcg_gen_ext32u_i64(tcg_rd, cpu_reg(s, rn));
2971         tcg_gen_bswap32_i64(tcg_rd, tcg_rd);
2972     }
2973 }
2974
2975 /* C5.6.150 REV16 (opcode==1) */
2976 static void handle_rev16(DisasContext *s, unsigned int sf,
2977                          unsigned int rn, unsigned int rd)
2978 {
2979     TCGv_i64 tcg_rd = cpu_reg(s, rd);
2980     TCGv_i64 tcg_tmp = tcg_temp_new_i64();
2981     TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
2982
2983     tcg_gen_andi_i64(tcg_tmp, tcg_rn, 0xffff);
2984     tcg_gen_bswap16_i64(tcg_rd, tcg_tmp);
2985
2986     tcg_gen_shri_i64(tcg_tmp, tcg_rn, 16);
2987     tcg_gen_andi_i64(tcg_tmp, tcg_tmp, 0xffff);
2988     tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp);
2989     tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, 16, 16);
2990
2991     if (sf) {
2992         tcg_gen_shri_i64(tcg_tmp, tcg_rn, 32);
2993         tcg_gen_andi_i64(tcg_tmp, tcg_tmp, 0xffff);
2994         tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp);
2995         tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, 32, 16);
2996
2997         tcg_gen_shri_i64(tcg_tmp, tcg_rn, 48);
2998         tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp);
2999         tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, 48, 16);
3000     }
3001
3002     tcg_temp_free_i64(tcg_tmp);
3003 }
3004
3005 /* C3.5.7 Data-processing (1 source)
3006  *   31  30  29  28             21 20     16 15    10 9    5 4    0
3007  * +----+---+---+-----------------+---------+--------+------+------+
3008  * | sf | 1 | S | 1 1 0 1 0 1 1 0 | opcode2 | opcode |  Rn  |  Rd  |
3009  * +----+---+---+-----------------+---------+--------+------+------+
3010  */
3011 static void disas_data_proc_1src(DisasContext *s, uint32_t insn)
3012 {
3013     unsigned int sf, opcode, rn, rd;
3014
3015     if (extract32(insn, 29, 1) || extract32(insn, 16, 5)) {
3016         unallocated_encoding(s);
3017         return;
3018     }
3019
3020     sf = extract32(insn, 31, 1);
3021     opcode = extract32(insn, 10, 6);
3022     rn = extract32(insn, 5, 5);
3023     rd = extract32(insn, 0, 5);
3024
3025     switch (opcode) {
3026     case 0: /* RBIT */
3027         handle_rbit(s, sf, rn, rd);
3028         break;
3029     case 1: /* REV16 */
3030         handle_rev16(s, sf, rn, rd);
3031         break;
3032     case 2: /* REV32 */
3033         handle_rev32(s, sf, rn, rd);
3034         break;
3035     case 3: /* REV64 */
3036         handle_rev64(s, sf, rn, rd);
3037         break;
3038     case 4: /* CLZ */
3039         handle_clz(s, sf, rn, rd);
3040         break;
3041     case 5: /* CLS */
3042         handle_cls(s, sf, rn, rd);
3043         break;
3044     }
3045 }
3046
3047 static void handle_div(DisasContext *s, bool is_signed, unsigned int sf,
3048                        unsigned int rm, unsigned int rn, unsigned int rd)
3049 {
3050     TCGv_i64 tcg_n, tcg_m, tcg_rd;
3051     tcg_rd = cpu_reg(s, rd);
3052
3053     if (!sf && is_signed) {
3054         tcg_n = new_tmp_a64(s);
3055         tcg_m = new_tmp_a64(s);
3056         tcg_gen_ext32s_i64(tcg_n, cpu_reg(s, rn));
3057         tcg_gen_ext32s_i64(tcg_m, cpu_reg(s, rm));
3058     } else {
3059         tcg_n = read_cpu_reg(s, rn, sf);
3060         tcg_m = read_cpu_reg(s, rm, sf);
3061     }
3062
3063     if (is_signed) {
3064         gen_helper_sdiv64(tcg_rd, tcg_n, tcg_m);
3065     } else {
3066         gen_helper_udiv64(tcg_rd, tcg_n, tcg_m);
3067     }
3068
3069     if (!sf) { /* zero extend final result */
3070         tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
3071     }
3072 }
3073
3074 /* C5.6.115 LSLV, C5.6.118 LSRV, C5.6.17 ASRV, C5.6.154 RORV */
3075 static void handle_shift_reg(DisasContext *s,
3076                              enum a64_shift_type shift_type, unsigned int sf,
3077                              unsigned int rm, unsigned int rn, unsigned int rd)
3078 {
3079     TCGv_i64 tcg_shift = tcg_temp_new_i64();
3080     TCGv_i64 tcg_rd = cpu_reg(s, rd);
3081     TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
3082
3083     tcg_gen_andi_i64(tcg_shift, cpu_reg(s, rm), sf ? 63 : 31);
3084     shift_reg(tcg_rd, tcg_rn, sf, shift_type, tcg_shift);
3085     tcg_temp_free_i64(tcg_shift);
3086 }
3087
3088 /* C3.5.8 Data-processing (2 source)
3089  *   31   30  29 28             21 20  16 15    10 9    5 4    0
3090  * +----+---+---+-----------------+------+--------+------+------+
3091  * | sf | 0 | S | 1 1 0 1 0 1 1 0 |  Rm  | opcode |  Rn  |  Rd  |
3092  * +----+---+---+-----------------+------+--------+------+------+
3093  */
3094 static void disas_data_proc_2src(DisasContext *s, uint32_t insn)
3095 {
3096     unsigned int sf, rm, opcode, rn, rd;
3097     sf = extract32(insn, 31, 1);
3098     rm = extract32(insn, 16, 5);
3099     opcode = extract32(insn, 10, 6);
3100     rn = extract32(insn, 5, 5);
3101     rd = extract32(insn, 0, 5);
3102
3103     if (extract32(insn, 29, 1)) {
3104         unallocated_encoding(s);
3105         return;
3106     }
3107
3108     switch (opcode) {
3109     case 2: /* UDIV */
3110         handle_div(s, false, sf, rm, rn, rd);
3111         break;
3112     case 3: /* SDIV */
3113         handle_div(s, true, sf, rm, rn, rd);
3114         break;
3115     case 8: /* LSLV */
3116         handle_shift_reg(s, A64_SHIFT_TYPE_LSL, sf, rm, rn, rd);
3117         break;
3118     case 9: /* LSRV */
3119         handle_shift_reg(s, A64_SHIFT_TYPE_LSR, sf, rm, rn, rd);
3120         break;
3121     case 10: /* ASRV */
3122         handle_shift_reg(s, A64_SHIFT_TYPE_ASR, sf, rm, rn, rd);
3123         break;
3124     case 11: /* RORV */
3125         handle_shift_reg(s, A64_SHIFT_TYPE_ROR, sf, rm, rn, rd);
3126         break;
3127     case 16:
3128     case 17:
3129     case 18:
3130     case 19:
3131     case 20:
3132     case 21:
3133     case 22:
3134     case 23: /* CRC32 */
3135         unsupported_encoding(s, insn);
3136         break;
3137     default:
3138         unallocated_encoding(s);
3139         break;
3140     }
3141 }
3142
3143 /* C3.5 Data processing - register */
3144 static void disas_data_proc_reg(DisasContext *s, uint32_t insn)
3145 {
3146     switch (extract32(insn, 24, 5)) {
3147     case 0x0a: /* Logical (shifted register) */
3148         disas_logic_reg(s, insn);
3149         break;
3150     case 0x0b: /* Add/subtract */
3151         if (insn & (1 << 21)) { /* (extended register) */
3152             disas_add_sub_ext_reg(s, insn);
3153         } else {
3154             disas_add_sub_reg(s, insn);
3155         }
3156         break;
3157     case 0x1b: /* Data-processing (3 source) */
3158         disas_data_proc_3src(s, insn);
3159         break;
3160     case 0x1a:
3161         switch (extract32(insn, 21, 3)) {
3162         case 0x0: /* Add/subtract (with carry) */
3163             disas_adc_sbc(s, insn);
3164             break;
3165         case 0x2: /* Conditional compare */
3166             disas_cc(s, insn); /* both imm and reg forms */
3167             break;
3168         case 0x4: /* Conditional select */
3169             disas_cond_select(s, insn);
3170             break;
3171         case 0x6: /* Data-processing */
3172             if (insn & (1 << 30)) { /* (1 source) */
3173                 disas_data_proc_1src(s, insn);
3174             } else {            /* (2 source) */
3175                 disas_data_proc_2src(s, insn);
3176             }
3177             break;
3178         default:
3179             unallocated_encoding(s);
3180             break;
3181         }
3182         break;
3183     default:
3184         unallocated_encoding(s);
3185         break;
3186     }
3187 }
3188
3189 /* Convert ARM rounding mode to softfloat */
3190 static inline int arm_rmode_to_sf(int rmode)
3191 {
3192     switch (rmode) {
3193     case FPROUNDING_TIEAWAY:
3194         rmode = float_round_ties_away;
3195         break;
3196     case FPROUNDING_ODD:
3197         /* FIXME: add support for TIEAWAY and ODD */
3198         qemu_log_mask(LOG_UNIMP, "arm: unimplemented rounding mode: %d\n",
3199                       rmode);
3200     case FPROUNDING_TIEEVEN:
3201     default:
3202         rmode = float_round_nearest_even;
3203         break;
3204     case FPROUNDING_POSINF:
3205         rmode = float_round_up;
3206         break;
3207     case FPROUNDING_NEGINF:
3208         rmode = float_round_down;
3209         break;
3210     case FPROUNDING_ZERO:
3211         rmode = float_round_to_zero;
3212         break;
3213     }
3214     return rmode;
3215 }
3216
3217 static void handle_fp_compare(DisasContext *s, bool is_double,
3218                               unsigned int rn, unsigned int rm,
3219                               bool cmp_with_zero, bool signal_all_nans)
3220 {
3221     TCGv_i64 tcg_flags = tcg_temp_new_i64();
3222     TCGv_ptr fpst = get_fpstatus_ptr();
3223
3224     if (is_double) {
3225         TCGv_i64 tcg_vn, tcg_vm;
3226
3227         tcg_vn = read_fp_dreg(s, rn);
3228         if (cmp_with_zero) {
3229             tcg_vm = tcg_const_i64(0);
3230         } else {
3231             tcg_vm = read_fp_dreg(s, rm);
3232         }
3233         if (signal_all_nans) {
3234             gen_helper_vfp_cmped_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
3235         } else {
3236             gen_helper_vfp_cmpd_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
3237         }
3238         tcg_temp_free_i64(tcg_vn);
3239         tcg_temp_free_i64(tcg_vm);
3240     } else {
3241         TCGv_i32 tcg_vn, tcg_vm;
3242
3243         tcg_vn = read_fp_sreg(s, rn);
3244         if (cmp_with_zero) {
3245             tcg_vm = tcg_const_i32(0);
3246         } else {
3247             tcg_vm = read_fp_sreg(s, rm);
3248         }
3249         if (signal_all_nans) {
3250             gen_helper_vfp_cmpes_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
3251         } else {
3252             gen_helper_vfp_cmps_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
3253         }
3254         tcg_temp_free_i32(tcg_vn);
3255         tcg_temp_free_i32(tcg_vm);
3256     }
3257
3258     tcg_temp_free_ptr(fpst);
3259
3260     gen_set_nzcv(tcg_flags);
3261
3262     tcg_temp_free_i64(tcg_flags);
3263 }
3264
3265 /* C3.6.22 Floating point compare
3266  *   31  30  29 28       24 23  22  21 20  16 15 14 13  10    9    5 4     0
3267  * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
3268  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | op  | 1 0 0 0 |  Rn  |  op2  |
3269  * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
3270  */
3271 static void disas_fp_compare(DisasContext *s, uint32_t insn)
3272 {
3273     unsigned int mos, type, rm, op, rn, opc, op2r;
3274
3275     mos = extract32(insn, 29, 3);
3276     type = extract32(insn, 22, 2); /* 0 = single, 1 = double */
3277     rm = extract32(insn, 16, 5);
3278     op = extract32(insn, 14, 2);
3279     rn = extract32(insn, 5, 5);
3280     opc = extract32(insn, 3, 2);
3281     op2r = extract32(insn, 0, 3);
3282
3283     if (mos || op || op2r || type > 1) {
3284         unallocated_encoding(s);
3285         return;
3286     }
3287
3288     handle_fp_compare(s, type, rn, rm, opc & 1, opc & 2);
3289 }
3290
3291 /* C3.6.23 Floating point conditional compare
3292  *   31  30  29 28       24 23  22  21 20  16 15  12 11 10 9    5  4   3    0
3293  * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
3294  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | cond | 0 1 |  Rn  | op | nzcv |
3295  * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
3296  */
3297 static void disas_fp_ccomp(DisasContext *s, uint32_t insn)
3298 {
3299     unsigned int mos, type, rm, cond, rn, op, nzcv;
3300     TCGv_i64 tcg_flags;
3301     int label_continue = -1;
3302
3303     mos = extract32(insn, 29, 3);
3304     type = extract32(insn, 22, 2); /* 0 = single, 1 = double */
3305     rm = extract32(insn, 16, 5);
3306     cond = extract32(insn, 12, 4);
3307     rn = extract32(insn, 5, 5);
3308     op = extract32(insn, 4, 1);
3309     nzcv = extract32(insn, 0, 4);
3310
3311     if (mos || type > 1) {
3312         unallocated_encoding(s);
3313         return;
3314     }
3315
3316     if (cond < 0x0e) { /* not always */
3317         int label_match = gen_new_label();
3318         label_continue = gen_new_label();
3319         arm_gen_test_cc(cond, label_match);
3320         /* nomatch: */
3321         tcg_flags = tcg_const_i64(nzcv << 28);
3322         gen_set_nzcv(tcg_flags);
3323         tcg_temp_free_i64(tcg_flags);
3324         tcg_gen_br(label_continue);
3325         gen_set_label(label_match);
3326     }
3327
3328     handle_fp_compare(s, type, rn, rm, false, op);
3329
3330     if (cond < 0x0e) {
3331         gen_set_label(label_continue);
3332     }
3333 }
3334
3335 /* copy src FP register to dst FP register; type specifies single or double */
3336 static void gen_mov_fp2fp(DisasContext *s, int type, int dst, int src)
3337 {
3338     if (type) {
3339         TCGv_i64 v = read_fp_dreg(s, src);
3340         write_fp_dreg(s, dst, v);
3341         tcg_temp_free_i64(v);
3342     } else {
3343         TCGv_i32 v = read_fp_sreg(s, src);
3344         write_fp_sreg(s, dst, v);
3345         tcg_temp_free_i32(v);
3346     }
3347 }
3348
3349 /* C3.6.24 Floating point conditional select
3350  *   31  30  29 28       24 23  22  21 20  16 15  12 11 10 9    5 4    0
3351  * +---+---+---+-----------+------+---+------+------+-----+------+------+
3352  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | cond | 1 1 |  Rn  |  Rd  |
3353  * +---+---+---+-----------+------+---+------+------+-----+------+------+
3354  */
3355 static void disas_fp_csel(DisasContext *s, uint32_t insn)
3356 {
3357     unsigned int mos, type, rm, cond, rn, rd;
3358     int label_continue = -1;
3359
3360     mos = extract32(insn, 29, 3);
3361     type = extract32(insn, 22, 2); /* 0 = single, 1 = double */
3362     rm = extract32(insn, 16, 5);
3363     cond = extract32(insn, 12, 4);
3364     rn = extract32(insn, 5, 5);
3365     rd = extract32(insn, 0, 5);
3366
3367     if (mos || type > 1) {
3368         unallocated_encoding(s);
3369         return;
3370     }
3371
3372     if (cond < 0x0e) { /* not always */
3373         int label_match = gen_new_label();
3374         label_continue = gen_new_label();
3375         arm_gen_test_cc(cond, label_match);
3376         /* nomatch: */
3377         gen_mov_fp2fp(s, type, rd, rm);
3378         tcg_gen_br(label_continue);
3379         gen_set_label(label_match);
3380     }
3381
3382     gen_mov_fp2fp(s, type, rd, rn);
3383
3384     if (cond < 0x0e) { /* continue */
3385         gen_set_label(label_continue);
3386     }
3387 }
3388
3389 /* C3.6.25 Floating-point data-processing (1 source) - single precision */
3390 static void handle_fp_1src_single(DisasContext *s, int opcode, int rd, int rn)
3391 {
3392     TCGv_ptr fpst;
3393     TCGv_i32 tcg_op;
3394     TCGv_i32 tcg_res;
3395
3396     fpst = get_fpstatus_ptr();
3397     tcg_op = read_fp_sreg(s, rn);
3398     tcg_res = tcg_temp_new_i32();
3399
3400     switch (opcode) {
3401     case 0x0: /* FMOV */
3402         tcg_gen_mov_i32(tcg_res, tcg_op);
3403         break;
3404     case 0x1: /* FABS */
3405         gen_helper_vfp_abss(tcg_res, tcg_op);
3406         break;
3407     case 0x2: /* FNEG */
3408         gen_helper_vfp_negs(tcg_res, tcg_op);
3409         break;
3410     case 0x3: /* FSQRT */
3411         gen_helper_vfp_sqrts(tcg_res, tcg_op, cpu_env);
3412         break;
3413     case 0x8: /* FRINTN */
3414     case 0x9: /* FRINTP */
3415     case 0xa: /* FRINTM */
3416     case 0xb: /* FRINTZ */
3417     case 0xc: /* FRINTA */
3418     {
3419         TCGv_i32 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(opcode & 7));
3420
3421         gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
3422         gen_helper_rints(tcg_res, tcg_op, fpst);
3423
3424         gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
3425         tcg_temp_free_i32(tcg_rmode);
3426         break;
3427     }
3428     case 0xe: /* FRINTX */
3429         gen_helper_rints_exact(tcg_res, tcg_op, fpst);
3430         break;
3431     case 0xf: /* FRINTI */
3432         gen_helper_rints(tcg_res, tcg_op, fpst);
3433         break;
3434     default:
3435         abort();
3436     }
3437
3438     write_fp_sreg(s, rd, tcg_res);
3439
3440     tcg_temp_free_ptr(fpst);
3441     tcg_temp_free_i32(tcg_op);
3442     tcg_temp_free_i32(tcg_res);
3443 }
3444
3445 /* C3.6.25 Floating-point data-processing (1 source) - double precision */
3446 static void handle_fp_1src_double(DisasContext *s, int opcode, int rd, int rn)
3447 {
3448     TCGv_ptr fpst;
3449     TCGv_i64 tcg_op;
3450     TCGv_i64 tcg_res;
3451
3452     fpst = get_fpstatus_ptr();
3453     tcg_op = read_fp_dreg(s, rn);
3454     tcg_res = tcg_temp_new_i64();
3455
3456     switch (opcode) {
3457     case 0x0: /* FMOV */
3458         tcg_gen_mov_i64(tcg_res, tcg_op);
3459         break;
3460     case 0x1: /* FABS */
3461         gen_helper_vfp_absd(tcg_res, tcg_op);
3462         break;
3463     case 0x2: /* FNEG */
3464         gen_helper_vfp_negd(tcg_res, tcg_op);
3465         break;
3466     case 0x3: /* FSQRT */
3467         gen_helper_vfp_sqrtd(tcg_res, tcg_op, cpu_env);
3468         break;
3469     case 0x8: /* FRINTN */
3470     case 0x9: /* FRINTP */
3471     case 0xa: /* FRINTM */
3472     case 0xb: /* FRINTZ */
3473     case 0xc: /* FRINTA */
3474     {
3475         TCGv_i32 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(opcode & 7));
3476
3477         gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
3478         gen_helper_rintd(tcg_res, tcg_op, fpst);
3479
3480         gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
3481         tcg_temp_free_i32(tcg_rmode);
3482         break;
3483     }
3484     case 0xe: /* FRINTX */
3485         gen_helper_rintd_exact(tcg_res, tcg_op, fpst);
3486         break;
3487     case 0xf: /* FRINTI */
3488         gen_helper_rintd(tcg_res, tcg_op, fpst);
3489         break;
3490     default:
3491         abort();
3492     }
3493
3494     write_fp_dreg(s, rd, tcg_res);
3495
3496     tcg_temp_free_ptr(fpst);
3497     tcg_temp_free_i64(tcg_op);
3498     tcg_temp_free_i64(tcg_res);
3499 }
3500
3501 static void handle_fp_fcvt(DisasContext *s, int opcode,
3502                            int rd, int rn, int dtype, int ntype)
3503 {
3504     switch (ntype) {
3505     case 0x0:
3506     {
3507         TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
3508         if (dtype == 1) {
3509             /* Single to double */
3510             TCGv_i64 tcg_rd = tcg_temp_new_i64();
3511             gen_helper_vfp_fcvtds(tcg_rd, tcg_rn, cpu_env);
3512             write_fp_dreg(s, rd, tcg_rd);
3513             tcg_temp_free_i64(tcg_rd);
3514         } else {
3515             /* Single to half */
3516             TCGv_i32 tcg_rd = tcg_temp_new_i32();
3517             gen_helper_vfp_fcvt_f32_to_f16(tcg_rd, tcg_rn, cpu_env);
3518             /* write_fp_sreg is OK here because top half of tcg_rd is zero */
3519             write_fp_sreg(s, rd, tcg_rd);
3520             tcg_temp_free_i32(tcg_rd);
3521         }
3522         tcg_temp_free_i32(tcg_rn);
3523         break;
3524     }
3525     case 0x1:
3526     {
3527         TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
3528         TCGv_i32 tcg_rd = tcg_temp_new_i32();
3529         if (dtype == 0) {
3530             /* Double to single */
3531             gen_helper_vfp_fcvtsd(tcg_rd, tcg_rn, cpu_env);
3532         } else {
3533             /* Double to half */
3534             gen_helper_vfp_fcvt_f64_to_f16(tcg_rd, tcg_rn, cpu_env);
3535             /* write_fp_sreg is OK here because top half of tcg_rd is zero */
3536         }
3537         write_fp_sreg(s, rd, tcg_rd);
3538         tcg_temp_free_i32(tcg_rd);
3539         tcg_temp_free_i64(tcg_rn);
3540         break;
3541     }
3542     case 0x3:
3543     {
3544         TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
3545         tcg_gen_ext16u_i32(tcg_rn, tcg_rn);
3546         if (dtype == 0) {
3547             /* Half to single */
3548             TCGv_i32 tcg_rd = tcg_temp_new_i32();
3549             gen_helper_vfp_fcvt_f16_to_f32(tcg_rd, tcg_rn, cpu_env);
3550             write_fp_sreg(s, rd, tcg_rd);
3551             tcg_temp_free_i32(tcg_rd);
3552         } else {
3553             /* Half to double */
3554             TCGv_i64 tcg_rd = tcg_temp_new_i64();
3555             gen_helper_vfp_fcvt_f16_to_f64(tcg_rd, tcg_rn, cpu_env);
3556             write_fp_dreg(s, rd, tcg_rd);
3557             tcg_temp_free_i64(tcg_rd);
3558         }
3559         tcg_temp_free_i32(tcg_rn);
3560         break;
3561     }
3562     default:
3563         abort();
3564     }
3565 }
3566
3567 /* C3.6.25 Floating point data-processing (1 source)
3568  *   31  30  29 28       24 23  22  21 20    15 14       10 9    5 4    0
3569  * +---+---+---+-----------+------+---+--------+-----------+------+------+
3570  * | M | 0 | S | 1 1 1 1 0 | type | 1 | opcode | 1 0 0 0 0 |  Rn  |  Rd  |
3571  * +---+---+---+-----------+------+---+--------+-----------+------+------+
3572  */
3573 static void disas_fp_1src(DisasContext *s, uint32_t insn)
3574 {
3575     int type = extract32(insn, 22, 2);
3576     int opcode = extract32(insn, 15, 6);
3577     int rn = extract32(insn, 5, 5);
3578     int rd = extract32(insn, 0, 5);
3579
3580     switch (opcode) {
3581     case 0x4: case 0x5: case 0x7:
3582     {
3583         /* FCVT between half, single and double precision */
3584         int dtype = extract32(opcode, 0, 2);
3585         if (type == 2 || dtype == type) {
3586             unallocated_encoding(s);
3587             return;
3588         }
3589         handle_fp_fcvt(s, opcode, rd, rn, dtype, type);
3590         break;
3591     }
3592     case 0x0 ... 0x3:
3593     case 0x8 ... 0xc:
3594     case 0xe ... 0xf:
3595         /* 32-to-32 and 64-to-64 ops */
3596         switch (type) {
3597         case 0:
3598             handle_fp_1src_single(s, opcode, rd, rn);
3599             break;
3600         case 1:
3601             handle_fp_1src_double(s, opcode, rd, rn);
3602             break;
3603         default:
3604             unallocated_encoding(s);
3605         }
3606         break;
3607     default:
3608         unallocated_encoding(s);
3609         break;
3610     }
3611 }
3612
3613 /* C3.6.26 Floating-point data-processing (2 source) - single precision */
3614 static void handle_fp_2src_single(DisasContext *s, int opcode,
3615                                   int rd, int rn, int rm)
3616 {
3617     TCGv_i32 tcg_op1;
3618     TCGv_i32 tcg_op2;
3619     TCGv_i32 tcg_res;
3620     TCGv_ptr fpst;
3621
3622     tcg_res = tcg_temp_new_i32();
3623     fpst = get_fpstatus_ptr();
3624     tcg_op1 = read_fp_sreg(s, rn);
3625     tcg_op2 = read_fp_sreg(s, rm);
3626
3627     switch (opcode) {
3628     case 0x0: /* FMUL */
3629         gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
3630         break;
3631     case 0x1: /* FDIV */
3632         gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
3633         break;
3634     case 0x2: /* FADD */
3635         gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
3636         break;
3637     case 0x3: /* FSUB */
3638         gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
3639         break;
3640     case 0x4: /* FMAX */
3641         gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
3642         break;
3643     case 0x5: /* FMIN */
3644         gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
3645         break;
3646     case 0x6: /* FMAXNM */
3647         gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
3648         break;
3649     case 0x7: /* FMINNM */
3650         gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
3651         break;
3652     case 0x8: /* FNMUL */
3653         gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
3654         gen_helper_vfp_negs(tcg_res, tcg_res);
3655         break;
3656     }
3657
3658     write_fp_sreg(s, rd, tcg_res);
3659
3660     tcg_temp_free_ptr(fpst);
3661     tcg_temp_free_i32(tcg_op1);
3662     tcg_temp_free_i32(tcg_op2);
3663     tcg_temp_free_i32(tcg_res);
3664 }
3665
3666 /* C3.6.26 Floating-point data-processing (2 source) - double precision */
3667 static void handle_fp_2src_double(DisasContext *s, int opcode,
3668                                   int rd, int rn, int rm)
3669 {
3670     TCGv_i64 tcg_op1;
3671     TCGv_i64 tcg_op2;
3672     TCGv_i64 tcg_res;
3673     TCGv_ptr fpst;
3674
3675     tcg_res = tcg_temp_new_i64();
3676     fpst = get_fpstatus_ptr();
3677     tcg_op1 = read_fp_dreg(s, rn);
3678     tcg_op2 = read_fp_dreg(s, rm);
3679
3680     switch (opcode) {
3681     case 0x0: /* FMUL */
3682         gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
3683         break;
3684     case 0x1: /* FDIV */
3685         gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
3686         break;
3687     case 0x2: /* FADD */
3688         gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
3689         break;
3690     case 0x3: /* FSUB */
3691         gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
3692         break;
3693     case 0x4: /* FMAX */
3694         gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
3695         break;
3696     case 0x5: /* FMIN */
3697         gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
3698         break;
3699     case 0x6: /* FMAXNM */
3700         gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
3701         break;
3702     case 0x7: /* FMINNM */
3703         gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
3704         break;
3705     case 0x8: /* FNMUL */
3706         gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
3707         gen_helper_vfp_negd(tcg_res, tcg_res);
3708         break;
3709     }
3710
3711     write_fp_dreg(s, rd, tcg_res);
3712
3713     tcg_temp_free_ptr(fpst);
3714     tcg_temp_free_i64(tcg_op1);
3715     tcg_temp_free_i64(tcg_op2);
3716     tcg_temp_free_i64(tcg_res);
3717 }
3718
3719 /* C3.6.26 Floating point data-processing (2 source)
3720  *   31  30  29 28       24 23  22  21 20  16 15    12 11 10 9    5 4    0
3721  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
3722  * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | opcode | 1 0 |  Rn  |  Rd  |
3723  * +---+---+---+-----------+------+---+------+--------+-----+------+------+
3724  */
3725 static void disas_fp_2src(DisasContext *s, uint32_t insn)
3726 {
3727     int type = extract32(insn, 22, 2);
3728     int rd = extract32(insn, 0, 5);
3729     int rn = extract32(insn, 5, 5);
3730     int rm = extract32(insn, 16, 5);
3731     int opcode = extract32(insn, 12, 4);
3732
3733     if (opcode > 8) {
3734         unallocated_encoding(s);
3735         return;
3736     }
3737
3738     switch (type) {
3739     case 0:
3740         handle_fp_2src_single(s, opcode, rd, rn, rm);
3741         break;
3742     case 1:
3743         handle_fp_2src_double(s, opcode, rd, rn, rm);
3744         break;
3745     default:
3746         unallocated_encoding(s);
3747     }
3748 }
3749
3750 /* C3.6.27 Floating-point data-processing (3 source) - single precision */
3751 static void handle_fp_3src_single(DisasContext *s, bool o0, bool o1,
3752                                   int rd, int rn, int rm, int ra)
3753 {
3754     TCGv_i32 tcg_op1, tcg_op2, tcg_op3;
3755     TCGv_i32 tcg_res = tcg_temp_new_i32();
3756     TCGv_ptr fpst = get_fpstatus_ptr();
3757
3758     tcg_op1 = read_fp_sreg(s, rn);
3759     tcg_op2 = read_fp_sreg(s, rm);
3760     tcg_op3 = read_fp_sreg(s, ra);
3761
3762     /* These are fused multiply-add, and must be done as one
3763      * floating point operation with no rounding between the
3764      * multiplication and addition steps.
3765      * NB that doing the negations here as separate steps is
3766      * correct : an input NaN should come out with its sign bit
3767      * flipped if it is a negated-input.
3768      */
3769     if (o1 == true) {
3770         gen_helper_vfp_negs(tcg_op3, tcg_op3);
3771     }
3772
3773     if (o0 != o1) {
3774         gen_helper_vfp_negs(tcg_op1, tcg_op1);
3775     }
3776
3777     gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
3778
3779     write_fp_sreg(s, rd, tcg_res);
3780
3781     tcg_temp_free_ptr(fpst);
3782     tcg_temp_free_i32(tcg_op1);
3783     tcg_temp_free_i32(tcg_op2);
3784     tcg_temp_free_i32(tcg_op3);
3785     tcg_temp_free_i32(tcg_res);
3786 }
3787
3788 /* C3.6.27 Floating-point data-processing (3 source) - double precision */
3789 static void handle_fp_3src_double(DisasContext *s, bool o0, bool o1,
3790                                   int rd, int rn, int rm, int ra)
3791 {
3792     TCGv_i64 tcg_op1, tcg_op2, tcg_op3;
3793     TCGv_i64 tcg_res = tcg_temp_new_i64();
3794     TCGv_ptr fpst = get_fpstatus_ptr();
3795
3796     tcg_op1 = read_fp_dreg(s, rn);
3797     tcg_op2 = read_fp_dreg(s, rm);
3798     tcg_op3 = read_fp_dreg(s, ra);
3799
3800     /* These are fused multiply-add, and must be done as one
3801      * floating point operation with no rounding between the
3802      * multiplication and addition steps.
3803      * NB that doing the negations here as separate steps is
3804      * correct : an input NaN should come out with its sign bit
3805      * flipped if it is a negated-input.
3806      */
3807     if (o1 == true) {
3808         gen_helper_vfp_negd(tcg_op3, tcg_op3);
3809     }
3810
3811     if (o0 != o1) {
3812         gen_helper_vfp_negd(tcg_op1, tcg_op1);
3813     }
3814
3815     gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
3816
3817     write_fp_dreg(s, rd, tcg_res);
3818
3819     tcg_temp_free_ptr(fpst);
3820     tcg_temp_free_i64(tcg_op1);
3821     tcg_temp_free_i64(tcg_op2);
3822     tcg_temp_free_i64(tcg_op3);
3823     tcg_temp_free_i64(tcg_res);
3824 }
3825
3826 /* C3.6.27 Floating point data-processing (3 source)
3827  *   31  30  29 28       24 23  22  21  20  16  15  14  10 9    5 4    0
3828  * +---+---+---+-----------+------+----+------+----+------+------+------+
3829  * | M | 0 | S | 1 1 1 1 1 | type | o1 |  Rm  | o0 |  Ra  |  Rn  |  Rd  |
3830  * +---+---+---+-----------+------+----+------+----+------+------+------+
3831  */
3832 static void disas_fp_3src(DisasContext *s, uint32_t insn)
3833 {
3834     int type = extract32(insn, 22, 2);
3835     int rd = extract32(insn, 0, 5);
3836     int rn = extract32(insn, 5, 5);
3837     int ra = extract32(insn, 10, 5);
3838     int rm = extract32(insn, 16, 5);
3839     bool o0 = extract32(insn, 15, 1);
3840     bool o1 = extract32(insn, 21, 1);
3841
3842     switch (type) {
3843     case 0:
3844         handle_fp_3src_single(s, o0, o1, rd, rn, rm, ra);
3845         break;
3846     case 1:
3847         handle_fp_3src_double(s, o0, o1, rd, rn, rm, ra);
3848         break;
3849     default:
3850         unallocated_encoding(s);
3851     }
3852 }
3853
3854 /* C3.6.28 Floating point immediate
3855  *   31  30  29 28       24 23  22  21 20        13 12   10 9    5 4    0
3856  * +---+---+---+-----------+------+---+------------+-------+------+------+
3857  * | M | 0 | S | 1 1 1 1 0 | type | 1 |    imm8    | 1 0 0 | imm5 |  Rd  |
3858  * +---+---+---+-----------+------+---+------------+-------+------+------+
3859  */
3860 static void disas_fp_imm(DisasContext *s, uint32_t insn)
3861 {
3862     int rd = extract32(insn, 0, 5);
3863     int imm8 = extract32(insn, 13, 8);
3864     int is_double = extract32(insn, 22, 2);
3865     uint64_t imm;
3866     TCGv_i64 tcg_res;
3867
3868     if (is_double > 1) {
3869         unallocated_encoding(s);
3870         return;
3871     }
3872
3873     /* The imm8 encodes the sign bit, enough bits to represent
3874      * an exponent in the range 01....1xx to 10....0xx,
3875      * and the most significant 4 bits of the mantissa; see
3876      * VFPExpandImm() in the v8 ARM ARM.
3877      */
3878     if (is_double) {
3879         imm = (extract32(imm8, 7, 1) ? 0x8000 : 0) |
3880             (extract32(imm8, 6, 1) ? 0x3fc0 : 0x4000) |
3881             extract32(imm8, 0, 6);
3882         imm <<= 48;
3883     } else {
3884         imm = (extract32(imm8, 7, 1) ? 0x8000 : 0) |
3885             (extract32(imm8, 6, 1) ? 0x3e00 : 0x4000) |
3886             (extract32(imm8, 0, 6) << 3);
3887         imm <<= 16;
3888     }
3889
3890     tcg_res = tcg_const_i64(imm);
3891     write_fp_dreg(s, rd, tcg_res);
3892     tcg_temp_free_i64(tcg_res);
3893 }
3894
3895 /* Handle floating point <=> fixed point conversions. Note that we can
3896  * also deal with fp <=> integer conversions as a special case (scale == 64)
3897  * OPTME: consider handling that special case specially or at least skipping
3898  * the call to scalbn in the helpers for zero shifts.
3899  */
3900 static void handle_fpfpcvt(DisasContext *s, int rd, int rn, int opcode,
3901                            bool itof, int rmode, int scale, int sf, int type)
3902 {
3903     bool is_signed = !(opcode & 1);
3904     bool is_double = type;
3905     TCGv_ptr tcg_fpstatus;
3906     TCGv_i32 tcg_shift;
3907
3908     tcg_fpstatus = get_fpstatus_ptr();
3909
3910     tcg_shift = tcg_const_i32(64 - scale);
3911
3912     if (itof) {
3913         TCGv_i64 tcg_int = cpu_reg(s, rn);
3914         if (!sf) {
3915             TCGv_i64 tcg_extend = new_tmp_a64(s);
3916
3917             if (is_signed) {
3918                 tcg_gen_ext32s_i64(tcg_extend, tcg_int);
3919             } else {
3920                 tcg_gen_ext32u_i64(tcg_extend, tcg_int);
3921             }
3922
3923             tcg_int = tcg_extend;
3924         }
3925
3926         if (is_double) {
3927             TCGv_i64 tcg_double = tcg_temp_new_i64();
3928             if (is_signed) {
3929                 gen_helper_vfp_sqtod(tcg_double, tcg_int,
3930                                      tcg_shift, tcg_fpstatus);
3931             } else {
3932                 gen_helper_vfp_uqtod(tcg_double, tcg_int,
3933                                      tcg_shift, tcg_fpstatus);
3934             }
3935             write_fp_dreg(s, rd, tcg_double);
3936             tcg_temp_free_i64(tcg_double);
3937         } else {
3938             TCGv_i32 tcg_single = tcg_temp_new_i32();
3939             if (is_signed) {
3940                 gen_helper_vfp_sqtos(tcg_single, tcg_int,
3941                                      tcg_shift, tcg_fpstatus);
3942             } else {
3943                 gen_helper_vfp_uqtos(tcg_single, tcg_int,
3944                                      tcg_shift, tcg_fpstatus);
3945             }
3946             write_fp_sreg(s, rd, tcg_single);
3947             tcg_temp_free_i32(tcg_single);
3948         }
3949     } else {
3950         TCGv_i64 tcg_int = cpu_reg(s, rd);
3951         TCGv_i32 tcg_rmode;
3952
3953         if (extract32(opcode, 2, 1)) {
3954             /* There are too many rounding modes to all fit into rmode,
3955              * so FCVTA[US] is a special case.
3956              */
3957             rmode = FPROUNDING_TIEAWAY;
3958         }
3959
3960         tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rmode));
3961
3962         gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
3963
3964         if (is_double) {
3965             TCGv_i64 tcg_double = read_fp_dreg(s, rn);
3966             if (is_signed) {
3967                 if (!sf) {
3968                     gen_helper_vfp_tosld(tcg_int, tcg_double,
3969                                          tcg_shift, tcg_fpstatus);
3970                 } else {
3971                     gen_helper_vfp_tosqd(tcg_int, tcg_double,
3972                                          tcg_shift, tcg_fpstatus);
3973                 }
3974             } else {
3975                 if (!sf) {
3976                     gen_helper_vfp_tould(tcg_int, tcg_double,
3977                                          tcg_shift, tcg_fpstatus);
3978                 } else {
3979                     gen_helper_vfp_touqd(tcg_int, tcg_double,
3980                                          tcg_shift, tcg_fpstatus);
3981                 }
3982             }
3983             tcg_temp_free_i64(tcg_double);
3984         } else {
3985             TCGv_i32 tcg_single = read_fp_sreg(s, rn);
3986             if (sf) {
3987                 if (is_signed) {
3988                     gen_helper_vfp_tosqs(tcg_int, tcg_single,
3989                                          tcg_shift, tcg_fpstatus);
3990                 } else {
3991                     gen_helper_vfp_touqs(tcg_int, tcg_single,
3992                                          tcg_shift, tcg_fpstatus);
3993                 }
3994             } else {
3995                 TCGv_i32 tcg_dest = tcg_temp_new_i32();
3996                 if (is_signed) {
3997                     gen_helper_vfp_tosls(tcg_dest, tcg_single,
3998                                          tcg_shift, tcg_fpstatus);
3999                 } else {
4000                     gen_helper_vfp_touls(tcg_dest, tcg_single,
4001                                          tcg_shift, tcg_fpstatus);
4002                 }
4003                 tcg_gen_extu_i32_i64(tcg_int, tcg_dest);
4004                 tcg_temp_free_i32(tcg_dest);
4005             }
4006             tcg_temp_free_i32(tcg_single);
4007         }
4008
4009         gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
4010         tcg_temp_free_i32(tcg_rmode);
4011
4012         if (!sf) {
4013             tcg_gen_ext32u_i64(tcg_int, tcg_int);
4014         }
4015     }
4016
4017     tcg_temp_free_ptr(tcg_fpstatus);
4018     tcg_temp_free_i32(tcg_shift);
4019 }
4020
4021 /* C3.6.29 Floating point <-> fixed point conversions
4022  *   31   30  29 28       24 23  22  21 20   19 18    16 15   10 9    5 4    0
4023  * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
4024  * | sf | 0 | S | 1 1 1 1 0 | type | 0 | rmode | opcode | scale |  Rn  |  Rd  |
4025  * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
4026  */
4027 static void disas_fp_fixed_conv(DisasContext *s, uint32_t insn)
4028 {
4029     int rd = extract32(insn, 0, 5);
4030     int rn = extract32(insn, 5, 5);
4031     int scale = extract32(insn, 10, 6);
4032     int opcode = extract32(insn, 16, 3);
4033     int rmode = extract32(insn, 19, 2);
4034     int type = extract32(insn, 22, 2);
4035     bool sbit = extract32(insn, 29, 1);
4036     bool sf = extract32(insn, 31, 1);
4037     bool itof;
4038
4039     if (sbit || (type > 1)
4040         || (!sf && scale < 32)) {
4041         unallocated_encoding(s);
4042         return;
4043     }
4044
4045     switch ((rmode << 3) | opcode) {
4046     case 0x2: /* SCVTF */
4047     case 0x3: /* UCVTF */
4048         itof = true;
4049         break;
4050     case 0x18: /* FCVTZS */
4051     case 0x19: /* FCVTZU */
4052         itof = false;
4053         break;
4054     default:
4055         unallocated_encoding(s);
4056         return;
4057     }
4058
4059     handle_fpfpcvt(s, rd, rn, opcode, itof, FPROUNDING_ZERO, scale, sf, type);
4060 }
4061
4062 static void handle_fmov(DisasContext *s, int rd, int rn, int type, bool itof)
4063 {
4064     /* FMOV: gpr to or from float, double, or top half of quad fp reg,
4065      * without conversion.
4066      */
4067
4068     if (itof) {
4069         TCGv_i64 tcg_rn = cpu_reg(s, rn);
4070
4071         switch (type) {
4072         case 0:
4073         {
4074             /* 32 bit */
4075             TCGv_i64 tmp = tcg_temp_new_i64();
4076             tcg_gen_ext32u_i64(tmp, tcg_rn);
4077             tcg_gen_st_i64(tmp, cpu_env, fp_reg_offset(rd, MO_64));
4078             tcg_gen_movi_i64(tmp, 0);
4079             tcg_gen_st_i64(tmp, cpu_env, fp_reg_hi_offset(rd));
4080             tcg_temp_free_i64(tmp);
4081             break;
4082         }
4083         case 1:
4084         {
4085             /* 64 bit */
4086             TCGv_i64 tmp = tcg_const_i64(0);
4087             tcg_gen_st_i64(tcg_rn, cpu_env, fp_reg_offset(rd, MO_64));
4088             tcg_gen_st_i64(tmp, cpu_env, fp_reg_hi_offset(rd));
4089             tcg_temp_free_i64(tmp);
4090             break;
4091         }
4092         case 2:
4093             /* 64 bit to top half. */
4094             tcg_gen_st_i64(tcg_rn, cpu_env, fp_reg_hi_offset(rd));
4095             break;
4096         }
4097     } else {
4098         TCGv_i64 tcg_rd = cpu_reg(s, rd);
4099
4100         switch (type) {
4101         case 0:
4102             /* 32 bit */
4103             tcg_gen_ld32u_i64(tcg_rd, cpu_env, fp_reg_offset(rn, MO_32));
4104             break;
4105         case 1:
4106             /* 64 bit */
4107             tcg_gen_ld_i64(tcg_rd, cpu_env, fp_reg_offset(rn, MO_64));
4108             break;
4109         case 2:
4110             /* 64 bits from top half */
4111             tcg_gen_ld_i64(tcg_rd, cpu_env, fp_reg_hi_offset(rn));
4112             break;
4113         }
4114     }
4115 }
4116
4117 /* C3.6.30 Floating point <-> integer conversions
4118  *   31   30  29 28       24 23  22  21 20   19 18 16 15         10 9  5 4  0
4119  * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
4120  * | sf | 0 | S | 1 1 1 1 0 | type | 1 | rmode | opc | 0 0 0 0 0 0 | Rn | Rd |
4121  * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
4122  */
4123 static void disas_fp_int_conv(DisasContext *s, uint32_t insn)
4124 {
4125     int rd = extract32(insn, 0, 5);
4126     int rn = extract32(insn, 5, 5);
4127     int opcode = extract32(insn, 16, 3);
4128     int rmode = extract32(insn, 19, 2);
4129     int type = extract32(insn, 22, 2);
4130     bool sbit = extract32(insn, 29, 1);
4131     bool sf = extract32(insn, 31, 1);
4132
4133     if (sbit) {
4134         unallocated_encoding(s);
4135         return;
4136     }
4137
4138     if (opcode > 5) {
4139         /* FMOV */
4140         bool itof = opcode & 1;
4141
4142         if (rmode >= 2) {
4143             unallocated_encoding(s);
4144             return;
4145         }
4146
4147         switch (sf << 3 | type << 1 | rmode) {
4148         case 0x0: /* 32 bit */
4149         case 0xa: /* 64 bit */
4150         case 0xd: /* 64 bit to top half of quad */
4151             break;
4152         default:
4153             /* all other sf/type/rmode combinations are invalid */
4154             unallocated_encoding(s);
4155             break;
4156         }
4157
4158         handle_fmov(s, rd, rn, type, itof);
4159     } else {
4160         /* actual FP conversions */
4161         bool itof = extract32(opcode, 1, 1);
4162
4163         if (type > 1 || (rmode != 0 && opcode > 1)) {
4164             unallocated_encoding(s);
4165             return;
4166         }
4167
4168         handle_fpfpcvt(s, rd, rn, opcode, itof, rmode, 64, sf, type);
4169     }
4170 }
4171
4172 /* FP-specific subcases of table C3-6 (SIMD and FP data processing)
4173  *   31  30  29 28     25 24                          0
4174  * +---+---+---+---------+-----------------------------+
4175  * |   | 0 |   | 1 1 1 1 |                             |
4176  * +---+---+---+---------+-----------------------------+
4177  */
4178 static void disas_data_proc_fp(DisasContext *s, uint32_t insn)
4179 {
4180     if (extract32(insn, 24, 1)) {
4181         /* Floating point data-processing (3 source) */
4182         disas_fp_3src(s, insn);
4183     } else if (extract32(insn, 21, 1) == 0) {
4184         /* Floating point to fixed point conversions */
4185         disas_fp_fixed_conv(s, insn);
4186     } else {
4187         switch (extract32(insn, 10, 2)) {
4188         case 1:
4189             /* Floating point conditional compare */
4190             disas_fp_ccomp(s, insn);
4191             break;
4192         case 2:
4193             /* Floating point data-processing (2 source) */
4194             disas_fp_2src(s, insn);
4195             break;
4196         case 3:
4197             /* Floating point conditional select */
4198             disas_fp_csel(s, insn);
4199             break;
4200         case 0:
4201             switch (ctz32(extract32(insn, 12, 4))) {
4202             case 0: /* [15:12] == xxx1 */
4203                 /* Floating point immediate */
4204                 disas_fp_imm(s, insn);
4205                 break;
4206             case 1: /* [15:12] == xx10 */
4207                 /* Floating point compare */
4208                 disas_fp_compare(s, insn);
4209                 break;
4210             case 2: /* [15:12] == x100 */
4211                 /* Floating point data-processing (1 source) */
4212                 disas_fp_1src(s, insn);
4213                 break;
4214             case 3: /* [15:12] == 1000 */
4215                 unallocated_encoding(s);
4216                 break;
4217             default: /* [15:12] == 0000 */
4218                 /* Floating point <-> integer conversions */
4219                 disas_fp_int_conv(s, insn);
4220                 break;
4221             }
4222             break;
4223         }
4224     }
4225 }
4226
4227 static void disas_data_proc_simd(DisasContext *s, uint32_t insn)
4228 {
4229     /* Note that this is called with all non-FP cases from
4230      * table C3-6 so it must UNDEF for entries not specifically
4231      * allocated to instructions in that table.
4232      */
4233     unsupported_encoding(s, insn);
4234 }
4235
4236 /* C3.6 Data processing - SIMD and floating point */
4237 static void disas_data_proc_simd_fp(DisasContext *s, uint32_t insn)
4238 {
4239     if (extract32(insn, 28, 1) == 1 && extract32(insn, 30, 1) == 0) {
4240         disas_data_proc_fp(s, insn);
4241     } else {
4242         /* SIMD, including crypto */
4243         disas_data_proc_simd(s, insn);
4244     }
4245 }
4246
4247 /* C3.1 A64 instruction index by encoding */
4248 static void disas_a64_insn(CPUARMState *env, DisasContext *s)
4249 {
4250     uint32_t insn;
4251
4252     insn = arm_ldl_code(env, s->pc, s->bswap_code);
4253     s->insn = insn;
4254     s->pc += 4;
4255
4256     switch (extract32(insn, 25, 4)) {
4257     case 0x0: case 0x1: case 0x2: case 0x3: /* UNALLOCATED */
4258         unallocated_encoding(s);
4259         break;
4260     case 0x8: case 0x9: /* Data processing - immediate */
4261         disas_data_proc_imm(s, insn);
4262         break;
4263     case 0xa: case 0xb: /* Branch, exception generation and system insns */
4264         disas_b_exc_sys(s, insn);
4265         break;
4266     case 0x4:
4267     case 0x6:
4268     case 0xc:
4269     case 0xe:      /* Loads and stores */
4270         disas_ldst(s, insn);
4271         break;
4272     case 0x5:
4273     case 0xd:      /* Data processing - register */
4274         disas_data_proc_reg(s, insn);
4275         break;
4276     case 0x7:
4277     case 0xf:      /* Data processing - SIMD and floating point */
4278         disas_data_proc_simd_fp(s, insn);
4279         break;
4280     default:
4281         assert(FALSE); /* all 15 cases should be handled above */
4282         break;
4283     }
4284
4285     /* if we allocated any temporaries, free them here */
4286     free_tmp_a64(s);
4287 }
4288
4289 void gen_intermediate_code_internal_a64(ARMCPU *cpu,
4290                                         TranslationBlock *tb,
4291                                         bool search_pc)
4292 {
4293     CPUState *cs = CPU(cpu);
4294     CPUARMState *env = &cpu->env;
4295     DisasContext dc1, *dc = &dc1;
4296     CPUBreakpoint *bp;
4297     uint16_t *gen_opc_end;
4298     int j, lj;
4299     target_ulong pc_start;
4300     target_ulong next_page_start;
4301     int num_insns;
4302     int max_insns;
4303
4304     pc_start = tb->pc;
4305
4306     dc->tb = tb;
4307
4308     gen_opc_end = tcg_ctx.gen_opc_buf + OPC_MAX_SIZE;
4309
4310     dc->is_jmp = DISAS_NEXT;
4311     dc->pc = pc_start;
4312     dc->singlestep_enabled = cs->singlestep_enabled;
4313     dc->condjmp = 0;
4314
4315     dc->aarch64 = 1;
4316     dc->thumb = 0;
4317     dc->bswap_code = 0;
4318     dc->condexec_mask = 0;
4319     dc->condexec_cond = 0;
4320 #if !defined(CONFIG_USER_ONLY)
4321     dc->user = 0;
4322 #endif
4323     dc->vfp_enabled = 0;
4324     dc->vec_len = 0;
4325     dc->vec_stride = 0;
4326     dc->cp_regs = cpu->cp_regs;
4327     dc->current_pl = arm_current_pl(env);
4328
4329     init_tmp_a64_array(dc);
4330
4331     next_page_start = (pc_start & TARGET_PAGE_MASK) + TARGET_PAGE_SIZE;
4332     lj = -1;
4333     num_insns = 0;
4334     max_insns = tb->cflags & CF_COUNT_MASK;
4335     if (max_insns == 0) {
4336         max_insns = CF_COUNT_MASK;
4337     }
4338
4339     gen_tb_start();
4340
4341     tcg_clear_temp_count();
4342
4343     do {
4344         if (unlikely(!QTAILQ_EMPTY(&env->breakpoints))) {
4345             QTAILQ_FOREACH(bp, &env->breakpoints, entry) {
4346                 if (bp->pc == dc->pc) {
4347                     gen_exception_insn(dc, 0, EXCP_DEBUG);
4348                     /* Advance PC so that clearing the breakpoint will
4349                        invalidate this TB.  */
4350                     dc->pc += 2;
4351                     goto done_generating;
4352                 }
4353             }
4354         }
4355
4356         if (search_pc) {
4357             j = tcg_ctx.gen_opc_ptr - tcg_ctx.gen_opc_buf;
4358             if (lj < j) {
4359                 lj++;
4360                 while (lj < j) {
4361                     tcg_ctx.gen_opc_instr_start[lj++] = 0;
4362                 }
4363             }
4364             tcg_ctx.gen_opc_pc[lj] = dc->pc;
4365             tcg_ctx.gen_opc_instr_start[lj] = 1;
4366             tcg_ctx.gen_opc_icount[lj] = num_insns;
4367         }
4368
4369         if (num_insns + 1 == max_insns && (tb->cflags & CF_LAST_IO)) {
4370             gen_io_start();
4371         }
4372
4373         if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP | CPU_LOG_TB_OP_OPT))) {
4374             tcg_gen_debug_insn_start(dc->pc);
4375         }
4376
4377         disas_a64_insn(env, dc);
4378
4379         if (tcg_check_temp_count()) {
4380             fprintf(stderr, "TCG temporary leak before "TARGET_FMT_lx"\n",
4381                     dc->pc);
4382         }
4383
4384         /* Translation stops when a conditional branch is encountered.
4385          * Otherwise the subsequent code could get translated several times.
4386          * Also stop translation when a page boundary is reached.  This
4387          * ensures prefetch aborts occur at the right place.
4388          */
4389         num_insns++;
4390     } while (!dc->is_jmp && tcg_ctx.gen_opc_ptr < gen_opc_end &&
4391              !cs->singlestep_enabled &&
4392              !singlestep &&
4393              dc->pc < next_page_start &&
4394              num_insns < max_insns);
4395
4396     if (tb->cflags & CF_LAST_IO) {
4397         gen_io_end();
4398     }
4399
4400     if (unlikely(cs->singlestep_enabled) && dc->is_jmp != DISAS_EXC) {
4401         /* Note that this means single stepping WFI doesn't halt the CPU.
4402          * For conditional branch insns this is harmless unreachable code as
4403          * gen_goto_tb() has already handled emitting the debug exception
4404          * (and thus a tb-jump is not possible when singlestepping).
4405          */
4406         assert(dc->is_jmp != DISAS_TB_JUMP);
4407         if (dc->is_jmp != DISAS_JUMP) {
4408             gen_a64_set_pc_im(dc->pc);
4409         }
4410         gen_exception(EXCP_DEBUG);
4411     } else {
4412         switch (dc->is_jmp) {
4413         case DISAS_NEXT:
4414             gen_goto_tb(dc, 1, dc->pc);
4415             break;
4416         default:
4417         case DISAS_UPDATE:
4418             gen_a64_set_pc_im(dc->pc);
4419             /* fall through */
4420         case DISAS_JUMP:
4421             /* indicate that the hash table must be used to find the next TB */
4422             tcg_gen_exit_tb(0);
4423             break;
4424         case DISAS_TB_JUMP:
4425         case DISAS_EXC:
4426         case DISAS_SWI:
4427             break;
4428         case DISAS_WFI:
4429             /* This is a special case because we don't want to just halt the CPU
4430              * if trying to debug across a WFI.
4431              */
4432             gen_helper_wfi(cpu_env);
4433             break;
4434         }
4435     }
4436
4437 done_generating:
4438     gen_tb_end(tb, num_insns);
4439     *tcg_ctx.gen_opc_ptr = INDEX_op_end;
4440
4441 #ifdef DEBUG_DISAS
4442     if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)) {
4443         qemu_log("----------------\n");
4444         qemu_log("IN: %s\n", lookup_symbol(pc_start));
4445         log_target_disas(env, pc_start, dc->pc - pc_start,
4446                          dc->thumb | (dc->bswap_code << 1));
4447         qemu_log("\n");
4448     }
4449 #endif
4450     if (search_pc) {
4451         j = tcg_ctx.gen_opc_ptr - tcg_ctx.gen_opc_buf;
4452         lj++;
4453         while (lj <= j) {
4454             tcg_ctx.gen_opc_instr_start[lj++] = 0;
4455         }
4456     } else {
4457         tb->size = dc->pc - pc_start;
4458         tb->icount = num_insns;
4459     }
4460 }
This page took 0.263753 seconds and 4 git commands to generate.