]> Git Repo - qemu.git/blob - target/arm/translate.c
Merge remote-tracking branch 'remotes/riscv/tags/riscv-qemu-2.13-minor-fixes-3' into...
[qemu.git] / target / arm / translate.c
1 /*
2  *  ARM translation
3  *
4  *  Copyright (c) 2003 Fabrice Bellard
5  *  Copyright (c) 2005-2007 CodeSourcery
6  *  Copyright (c) 2007 OpenedHand, Ltd.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
20  */
21 #include "qemu/osdep.h"
22
23 #include "cpu.h"
24 #include "internals.h"
25 #include "disas/disas.h"
26 #include "exec/exec-all.h"
27 #include "tcg-op.h"
28 #include "tcg-op-gvec.h"
29 #include "qemu/log.h"
30 #include "qemu/bitops.h"
31 #include "arm_ldst.h"
32 #include "exec/semihost.h"
33
34 #include "exec/helper-proto.h"
35 #include "exec/helper-gen.h"
36
37 #include "trace-tcg.h"
38 #include "exec/log.h"
39
40
41 #define ENABLE_ARCH_4T    arm_dc_feature(s, ARM_FEATURE_V4T)
42 #define ENABLE_ARCH_5     arm_dc_feature(s, ARM_FEATURE_V5)
43 /* currently all emulated v5 cores are also v5TE, so don't bother */
44 #define ENABLE_ARCH_5TE   arm_dc_feature(s, ARM_FEATURE_V5)
45 #define ENABLE_ARCH_5J    arm_dc_feature(s, ARM_FEATURE_JAZELLE)
46 #define ENABLE_ARCH_6     arm_dc_feature(s, ARM_FEATURE_V6)
47 #define ENABLE_ARCH_6K    arm_dc_feature(s, ARM_FEATURE_V6K)
48 #define ENABLE_ARCH_6T2   arm_dc_feature(s, ARM_FEATURE_THUMB2)
49 #define ENABLE_ARCH_7     arm_dc_feature(s, ARM_FEATURE_V7)
50 #define ENABLE_ARCH_8     arm_dc_feature(s, ARM_FEATURE_V8)
51
52 #define ARCH(x) do { if (!ENABLE_ARCH_##x) goto illegal_op; } while(0)
53
54 #include "translate.h"
55
56 #if defined(CONFIG_USER_ONLY)
57 #define IS_USER(s) 1
58 #else
59 #define IS_USER(s) (s->user)
60 #endif
61
62 /* We reuse the same 64-bit temporaries for efficiency.  */
63 static TCGv_i64 cpu_V0, cpu_V1, cpu_M0;
64 static TCGv_i32 cpu_R[16];
65 TCGv_i32 cpu_CF, cpu_NF, cpu_VF, cpu_ZF;
66 TCGv_i64 cpu_exclusive_addr;
67 TCGv_i64 cpu_exclusive_val;
68
69 /* FIXME:  These should be removed.  */
70 static TCGv_i32 cpu_F0s, cpu_F1s;
71 static TCGv_i64 cpu_F0d, cpu_F1d;
72
73 #include "exec/gen-icount.h"
74
75 static const char *regnames[] =
76     { "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
77       "r8", "r9", "r10", "r11", "r12", "r13", "r14", "pc" };
78
79 /* Function prototypes for gen_ functions calling Neon helpers.  */
80 typedef void NeonGenThreeOpEnvFn(TCGv_i32, TCGv_env, TCGv_i32,
81                                  TCGv_i32, TCGv_i32);
82
83 /* initialize TCG globals.  */
84 void arm_translate_init(void)
85 {
86     int i;
87
88     for (i = 0; i < 16; i++) {
89         cpu_R[i] = tcg_global_mem_new_i32(cpu_env,
90                                           offsetof(CPUARMState, regs[i]),
91                                           regnames[i]);
92     }
93     cpu_CF = tcg_global_mem_new_i32(cpu_env, offsetof(CPUARMState, CF), "CF");
94     cpu_NF = tcg_global_mem_new_i32(cpu_env, offsetof(CPUARMState, NF), "NF");
95     cpu_VF = tcg_global_mem_new_i32(cpu_env, offsetof(CPUARMState, VF), "VF");
96     cpu_ZF = tcg_global_mem_new_i32(cpu_env, offsetof(CPUARMState, ZF), "ZF");
97
98     cpu_exclusive_addr = tcg_global_mem_new_i64(cpu_env,
99         offsetof(CPUARMState, exclusive_addr), "exclusive_addr");
100     cpu_exclusive_val = tcg_global_mem_new_i64(cpu_env,
101         offsetof(CPUARMState, exclusive_val), "exclusive_val");
102
103     a64_translate_init();
104 }
105
106 /* Flags for the disas_set_da_iss info argument:
107  * lower bits hold the Rt register number, higher bits are flags.
108  */
109 typedef enum ISSInfo {
110     ISSNone = 0,
111     ISSRegMask = 0x1f,
112     ISSInvalid = (1 << 5),
113     ISSIsAcqRel = (1 << 6),
114     ISSIsWrite = (1 << 7),
115     ISSIs16Bit = (1 << 8),
116 } ISSInfo;
117
118 /* Save the syndrome information for a Data Abort */
119 static void disas_set_da_iss(DisasContext *s, TCGMemOp memop, ISSInfo issinfo)
120 {
121     uint32_t syn;
122     int sas = memop & MO_SIZE;
123     bool sse = memop & MO_SIGN;
124     bool is_acqrel = issinfo & ISSIsAcqRel;
125     bool is_write = issinfo & ISSIsWrite;
126     bool is_16bit = issinfo & ISSIs16Bit;
127     int srt = issinfo & ISSRegMask;
128
129     if (issinfo & ISSInvalid) {
130         /* Some callsites want to conditionally provide ISS info,
131          * eg "only if this was not a writeback"
132          */
133         return;
134     }
135
136     if (srt == 15) {
137         /* For AArch32, insns where the src/dest is R15 never generate
138          * ISS information. Catching that here saves checking at all
139          * the call sites.
140          */
141         return;
142     }
143
144     syn = syn_data_abort_with_iss(0, sas, sse, srt, 0, is_acqrel,
145                                   0, 0, 0, is_write, 0, is_16bit);
146     disas_set_insn_syndrome(s, syn);
147 }
148
149 static inline int get_a32_user_mem_index(DisasContext *s)
150 {
151     /* Return the core mmu_idx to use for A32/T32 "unprivileged load/store"
152      * insns:
153      *  if PL2, UNPREDICTABLE (we choose to implement as if PL0)
154      *  otherwise, access as if at PL0.
155      */
156     switch (s->mmu_idx) {
157     case ARMMMUIdx_S1E2:        /* this one is UNPREDICTABLE */
158     case ARMMMUIdx_S12NSE0:
159     case ARMMMUIdx_S12NSE1:
160         return arm_to_core_mmu_idx(ARMMMUIdx_S12NSE0);
161     case ARMMMUIdx_S1E3:
162     case ARMMMUIdx_S1SE0:
163     case ARMMMUIdx_S1SE1:
164         return arm_to_core_mmu_idx(ARMMMUIdx_S1SE0);
165     case ARMMMUIdx_MUser:
166     case ARMMMUIdx_MPriv:
167         return arm_to_core_mmu_idx(ARMMMUIdx_MUser);
168     case ARMMMUIdx_MUserNegPri:
169     case ARMMMUIdx_MPrivNegPri:
170         return arm_to_core_mmu_idx(ARMMMUIdx_MUserNegPri);
171     case ARMMMUIdx_MSUser:
172     case ARMMMUIdx_MSPriv:
173         return arm_to_core_mmu_idx(ARMMMUIdx_MSUser);
174     case ARMMMUIdx_MSUserNegPri:
175     case ARMMMUIdx_MSPrivNegPri:
176         return arm_to_core_mmu_idx(ARMMMUIdx_MSUserNegPri);
177     case ARMMMUIdx_S2NS:
178     default:
179         g_assert_not_reached();
180     }
181 }
182
183 static inline TCGv_i32 load_cpu_offset(int offset)
184 {
185     TCGv_i32 tmp = tcg_temp_new_i32();
186     tcg_gen_ld_i32(tmp, cpu_env, offset);
187     return tmp;
188 }
189
190 #define load_cpu_field(name) load_cpu_offset(offsetof(CPUARMState, name))
191
192 static inline void store_cpu_offset(TCGv_i32 var, int offset)
193 {
194     tcg_gen_st_i32(var, cpu_env, offset);
195     tcg_temp_free_i32(var);
196 }
197
198 #define store_cpu_field(var, name) \
199     store_cpu_offset(var, offsetof(CPUARMState, name))
200
201 /* Set a variable to the value of a CPU register.  */
202 static void load_reg_var(DisasContext *s, TCGv_i32 var, int reg)
203 {
204     if (reg == 15) {
205         uint32_t addr;
206         /* normally, since we updated PC, we need only to add one insn */
207         if (s->thumb)
208             addr = (long)s->pc + 2;
209         else
210             addr = (long)s->pc + 4;
211         tcg_gen_movi_i32(var, addr);
212     } else {
213         tcg_gen_mov_i32(var, cpu_R[reg]);
214     }
215 }
216
217 /* Create a new temporary and set it to the value of a CPU register.  */
218 static inline TCGv_i32 load_reg(DisasContext *s, int reg)
219 {
220     TCGv_i32 tmp = tcg_temp_new_i32();
221     load_reg_var(s, tmp, reg);
222     return tmp;
223 }
224
225 /* Set a CPU register.  The source must be a temporary and will be
226    marked as dead.  */
227 static void store_reg(DisasContext *s, int reg, TCGv_i32 var)
228 {
229     if (reg == 15) {
230         /* In Thumb mode, we must ignore bit 0.
231          * In ARM mode, for ARMv4 and ARMv5, it is UNPREDICTABLE if bits [1:0]
232          * are not 0b00, but for ARMv6 and above, we must ignore bits [1:0].
233          * We choose to ignore [1:0] in ARM mode for all architecture versions.
234          */
235         tcg_gen_andi_i32(var, var, s->thumb ? ~1 : ~3);
236         s->base.is_jmp = DISAS_JUMP;
237     }
238     tcg_gen_mov_i32(cpu_R[reg], var);
239     tcg_temp_free_i32(var);
240 }
241
242 /* Value extensions.  */
243 #define gen_uxtb(var) tcg_gen_ext8u_i32(var, var)
244 #define gen_uxth(var) tcg_gen_ext16u_i32(var, var)
245 #define gen_sxtb(var) tcg_gen_ext8s_i32(var, var)
246 #define gen_sxth(var) tcg_gen_ext16s_i32(var, var)
247
248 #define gen_sxtb16(var) gen_helper_sxtb16(var, var)
249 #define gen_uxtb16(var) gen_helper_uxtb16(var, var)
250
251
252 static inline void gen_set_cpsr(TCGv_i32 var, uint32_t mask)
253 {
254     TCGv_i32 tmp_mask = tcg_const_i32(mask);
255     gen_helper_cpsr_write(cpu_env, var, tmp_mask);
256     tcg_temp_free_i32(tmp_mask);
257 }
258 /* Set NZCV flags from the high 4 bits of var.  */
259 #define gen_set_nzcv(var) gen_set_cpsr(var, CPSR_NZCV)
260
261 static void gen_exception_internal(int excp)
262 {
263     TCGv_i32 tcg_excp = tcg_const_i32(excp);
264
265     assert(excp_is_internal(excp));
266     gen_helper_exception_internal(cpu_env, tcg_excp);
267     tcg_temp_free_i32(tcg_excp);
268 }
269
270 static void gen_exception(int excp, uint32_t syndrome, uint32_t target_el)
271 {
272     TCGv_i32 tcg_excp = tcg_const_i32(excp);
273     TCGv_i32 tcg_syn = tcg_const_i32(syndrome);
274     TCGv_i32 tcg_el = tcg_const_i32(target_el);
275
276     gen_helper_exception_with_syndrome(cpu_env, tcg_excp,
277                                        tcg_syn, tcg_el);
278
279     tcg_temp_free_i32(tcg_el);
280     tcg_temp_free_i32(tcg_syn);
281     tcg_temp_free_i32(tcg_excp);
282 }
283
284 static void gen_ss_advance(DisasContext *s)
285 {
286     /* If the singlestep state is Active-not-pending, advance to
287      * Active-pending.
288      */
289     if (s->ss_active) {
290         s->pstate_ss = 0;
291         gen_helper_clear_pstate_ss(cpu_env);
292     }
293 }
294
295 static void gen_step_complete_exception(DisasContext *s)
296 {
297     /* We just completed step of an insn. Move from Active-not-pending
298      * to Active-pending, and then also take the swstep exception.
299      * This corresponds to making the (IMPDEF) choice to prioritize
300      * swstep exceptions over asynchronous exceptions taken to an exception
301      * level where debug is disabled. This choice has the advantage that
302      * we do not need to maintain internal state corresponding to the
303      * ISV/EX syndrome bits between completion of the step and generation
304      * of the exception, and our syndrome information is always correct.
305      */
306     gen_ss_advance(s);
307     gen_exception(EXCP_UDEF, syn_swstep(s->ss_same_el, 1, s->is_ldex),
308                   default_exception_el(s));
309     s->base.is_jmp = DISAS_NORETURN;
310 }
311
312 static void gen_singlestep_exception(DisasContext *s)
313 {
314     /* Generate the right kind of exception for singlestep, which is
315      * either the architectural singlestep or EXCP_DEBUG for QEMU's
316      * gdb singlestepping.
317      */
318     if (s->ss_active) {
319         gen_step_complete_exception(s);
320     } else {
321         gen_exception_internal(EXCP_DEBUG);
322     }
323 }
324
325 static inline bool is_singlestepping(DisasContext *s)
326 {
327     /* Return true if we are singlestepping either because of
328      * architectural singlestep or QEMU gdbstub singlestep. This does
329      * not include the command line '-singlestep' mode which is rather
330      * misnamed as it only means "one instruction per TB" and doesn't
331      * affect the code we generate.
332      */
333     return s->base.singlestep_enabled || s->ss_active;
334 }
335
336 static void gen_smul_dual(TCGv_i32 a, TCGv_i32 b)
337 {
338     TCGv_i32 tmp1 = tcg_temp_new_i32();
339     TCGv_i32 tmp2 = tcg_temp_new_i32();
340     tcg_gen_ext16s_i32(tmp1, a);
341     tcg_gen_ext16s_i32(tmp2, b);
342     tcg_gen_mul_i32(tmp1, tmp1, tmp2);
343     tcg_temp_free_i32(tmp2);
344     tcg_gen_sari_i32(a, a, 16);
345     tcg_gen_sari_i32(b, b, 16);
346     tcg_gen_mul_i32(b, b, a);
347     tcg_gen_mov_i32(a, tmp1);
348     tcg_temp_free_i32(tmp1);
349 }
350
351 /* Byteswap each halfword.  */
352 static void gen_rev16(TCGv_i32 var)
353 {
354     TCGv_i32 tmp = tcg_temp_new_i32();
355     TCGv_i32 mask = tcg_const_i32(0x00ff00ff);
356     tcg_gen_shri_i32(tmp, var, 8);
357     tcg_gen_and_i32(tmp, tmp, mask);
358     tcg_gen_and_i32(var, var, mask);
359     tcg_gen_shli_i32(var, var, 8);
360     tcg_gen_or_i32(var, var, tmp);
361     tcg_temp_free_i32(mask);
362     tcg_temp_free_i32(tmp);
363 }
364
365 /* Byteswap low halfword and sign extend.  */
366 static void gen_revsh(TCGv_i32 var)
367 {
368     tcg_gen_ext16u_i32(var, var);
369     tcg_gen_bswap16_i32(var, var);
370     tcg_gen_ext16s_i32(var, var);
371 }
372
373 /* Return (b << 32) + a. Mark inputs as dead */
374 static TCGv_i64 gen_addq_msw(TCGv_i64 a, TCGv_i32 b)
375 {
376     TCGv_i64 tmp64 = tcg_temp_new_i64();
377
378     tcg_gen_extu_i32_i64(tmp64, b);
379     tcg_temp_free_i32(b);
380     tcg_gen_shli_i64(tmp64, tmp64, 32);
381     tcg_gen_add_i64(a, tmp64, a);
382
383     tcg_temp_free_i64(tmp64);
384     return a;
385 }
386
387 /* Return (b << 32) - a. Mark inputs as dead. */
388 static TCGv_i64 gen_subq_msw(TCGv_i64 a, TCGv_i32 b)
389 {
390     TCGv_i64 tmp64 = tcg_temp_new_i64();
391
392     tcg_gen_extu_i32_i64(tmp64, b);
393     tcg_temp_free_i32(b);
394     tcg_gen_shli_i64(tmp64, tmp64, 32);
395     tcg_gen_sub_i64(a, tmp64, a);
396
397     tcg_temp_free_i64(tmp64);
398     return a;
399 }
400
401 /* 32x32->64 multiply.  Marks inputs as dead.  */
402 static TCGv_i64 gen_mulu_i64_i32(TCGv_i32 a, TCGv_i32 b)
403 {
404     TCGv_i32 lo = tcg_temp_new_i32();
405     TCGv_i32 hi = tcg_temp_new_i32();
406     TCGv_i64 ret;
407
408     tcg_gen_mulu2_i32(lo, hi, a, b);
409     tcg_temp_free_i32(a);
410     tcg_temp_free_i32(b);
411
412     ret = tcg_temp_new_i64();
413     tcg_gen_concat_i32_i64(ret, lo, hi);
414     tcg_temp_free_i32(lo);
415     tcg_temp_free_i32(hi);
416
417     return ret;
418 }
419
420 static TCGv_i64 gen_muls_i64_i32(TCGv_i32 a, TCGv_i32 b)
421 {
422     TCGv_i32 lo = tcg_temp_new_i32();
423     TCGv_i32 hi = tcg_temp_new_i32();
424     TCGv_i64 ret;
425
426     tcg_gen_muls2_i32(lo, hi, a, b);
427     tcg_temp_free_i32(a);
428     tcg_temp_free_i32(b);
429
430     ret = tcg_temp_new_i64();
431     tcg_gen_concat_i32_i64(ret, lo, hi);
432     tcg_temp_free_i32(lo);
433     tcg_temp_free_i32(hi);
434
435     return ret;
436 }
437
438 /* Swap low and high halfwords.  */
439 static void gen_swap_half(TCGv_i32 var)
440 {
441     TCGv_i32 tmp = tcg_temp_new_i32();
442     tcg_gen_shri_i32(tmp, var, 16);
443     tcg_gen_shli_i32(var, var, 16);
444     tcg_gen_or_i32(var, var, tmp);
445     tcg_temp_free_i32(tmp);
446 }
447
448 /* Dual 16-bit add.  Result placed in t0 and t1 is marked as dead.
449     tmp = (t0 ^ t1) & 0x8000;
450     t0 &= ~0x8000;
451     t1 &= ~0x8000;
452     t0 = (t0 + t1) ^ tmp;
453  */
454
455 static void gen_add16(TCGv_i32 t0, TCGv_i32 t1)
456 {
457     TCGv_i32 tmp = tcg_temp_new_i32();
458     tcg_gen_xor_i32(tmp, t0, t1);
459     tcg_gen_andi_i32(tmp, tmp, 0x8000);
460     tcg_gen_andi_i32(t0, t0, ~0x8000);
461     tcg_gen_andi_i32(t1, t1, ~0x8000);
462     tcg_gen_add_i32(t0, t0, t1);
463     tcg_gen_xor_i32(t0, t0, tmp);
464     tcg_temp_free_i32(tmp);
465     tcg_temp_free_i32(t1);
466 }
467
468 /* Set CF to the top bit of var.  */
469 static void gen_set_CF_bit31(TCGv_i32 var)
470 {
471     tcg_gen_shri_i32(cpu_CF, var, 31);
472 }
473
474 /* Set N and Z flags from var.  */
475 static inline void gen_logic_CC(TCGv_i32 var)
476 {
477     tcg_gen_mov_i32(cpu_NF, var);
478     tcg_gen_mov_i32(cpu_ZF, var);
479 }
480
481 /* T0 += T1 + CF.  */
482 static void gen_adc(TCGv_i32 t0, TCGv_i32 t1)
483 {
484     tcg_gen_add_i32(t0, t0, t1);
485     tcg_gen_add_i32(t0, t0, cpu_CF);
486 }
487
488 /* dest = T0 + T1 + CF. */
489 static void gen_add_carry(TCGv_i32 dest, TCGv_i32 t0, TCGv_i32 t1)
490 {
491     tcg_gen_add_i32(dest, t0, t1);
492     tcg_gen_add_i32(dest, dest, cpu_CF);
493 }
494
495 /* dest = T0 - T1 + CF - 1.  */
496 static void gen_sub_carry(TCGv_i32 dest, TCGv_i32 t0, TCGv_i32 t1)
497 {
498     tcg_gen_sub_i32(dest, t0, t1);
499     tcg_gen_add_i32(dest, dest, cpu_CF);
500     tcg_gen_subi_i32(dest, dest, 1);
501 }
502
503 /* dest = T0 + T1. Compute C, N, V and Z flags */
504 static void gen_add_CC(TCGv_i32 dest, TCGv_i32 t0, TCGv_i32 t1)
505 {
506     TCGv_i32 tmp = tcg_temp_new_i32();
507     tcg_gen_movi_i32(tmp, 0);
508     tcg_gen_add2_i32(cpu_NF, cpu_CF, t0, tmp, t1, tmp);
509     tcg_gen_mov_i32(cpu_ZF, cpu_NF);
510     tcg_gen_xor_i32(cpu_VF, cpu_NF, t0);
511     tcg_gen_xor_i32(tmp, t0, t1);
512     tcg_gen_andc_i32(cpu_VF, cpu_VF, tmp);
513     tcg_temp_free_i32(tmp);
514     tcg_gen_mov_i32(dest, cpu_NF);
515 }
516
517 /* dest = T0 + T1 + CF.  Compute C, N, V and Z flags */
518 static void gen_adc_CC(TCGv_i32 dest, TCGv_i32 t0, TCGv_i32 t1)
519 {
520     TCGv_i32 tmp = tcg_temp_new_i32();
521     if (TCG_TARGET_HAS_add2_i32) {
522         tcg_gen_movi_i32(tmp, 0);
523         tcg_gen_add2_i32(cpu_NF, cpu_CF, t0, tmp, cpu_CF, tmp);
524         tcg_gen_add2_i32(cpu_NF, cpu_CF, cpu_NF, cpu_CF, t1, tmp);
525     } else {
526         TCGv_i64 q0 = tcg_temp_new_i64();
527         TCGv_i64 q1 = tcg_temp_new_i64();
528         tcg_gen_extu_i32_i64(q0, t0);
529         tcg_gen_extu_i32_i64(q1, t1);
530         tcg_gen_add_i64(q0, q0, q1);
531         tcg_gen_extu_i32_i64(q1, cpu_CF);
532         tcg_gen_add_i64(q0, q0, q1);
533         tcg_gen_extr_i64_i32(cpu_NF, cpu_CF, q0);
534         tcg_temp_free_i64(q0);
535         tcg_temp_free_i64(q1);
536     }
537     tcg_gen_mov_i32(cpu_ZF, cpu_NF);
538     tcg_gen_xor_i32(cpu_VF, cpu_NF, t0);
539     tcg_gen_xor_i32(tmp, t0, t1);
540     tcg_gen_andc_i32(cpu_VF, cpu_VF, tmp);
541     tcg_temp_free_i32(tmp);
542     tcg_gen_mov_i32(dest, cpu_NF);
543 }
544
545 /* dest = T0 - T1. Compute C, N, V and Z flags */
546 static void gen_sub_CC(TCGv_i32 dest, TCGv_i32 t0, TCGv_i32 t1)
547 {
548     TCGv_i32 tmp;
549     tcg_gen_sub_i32(cpu_NF, t0, t1);
550     tcg_gen_mov_i32(cpu_ZF, cpu_NF);
551     tcg_gen_setcond_i32(TCG_COND_GEU, cpu_CF, t0, t1);
552     tcg_gen_xor_i32(cpu_VF, cpu_NF, t0);
553     tmp = tcg_temp_new_i32();
554     tcg_gen_xor_i32(tmp, t0, t1);
555     tcg_gen_and_i32(cpu_VF, cpu_VF, tmp);
556     tcg_temp_free_i32(tmp);
557     tcg_gen_mov_i32(dest, cpu_NF);
558 }
559
560 /* dest = T0 + ~T1 + CF.  Compute C, N, V and Z flags */
561 static void gen_sbc_CC(TCGv_i32 dest, TCGv_i32 t0, TCGv_i32 t1)
562 {
563     TCGv_i32 tmp = tcg_temp_new_i32();
564     tcg_gen_not_i32(tmp, t1);
565     gen_adc_CC(dest, t0, tmp);
566     tcg_temp_free_i32(tmp);
567 }
568
569 #define GEN_SHIFT(name)                                               \
570 static void gen_##name(TCGv_i32 dest, TCGv_i32 t0, TCGv_i32 t1)       \
571 {                                                                     \
572     TCGv_i32 tmp1, tmp2, tmp3;                                        \
573     tmp1 = tcg_temp_new_i32();                                        \
574     tcg_gen_andi_i32(tmp1, t1, 0xff);                                 \
575     tmp2 = tcg_const_i32(0);                                          \
576     tmp3 = tcg_const_i32(0x1f);                                       \
577     tcg_gen_movcond_i32(TCG_COND_GTU, tmp2, tmp1, tmp3, tmp2, t0);    \
578     tcg_temp_free_i32(tmp3);                                          \
579     tcg_gen_andi_i32(tmp1, tmp1, 0x1f);                               \
580     tcg_gen_##name##_i32(dest, tmp2, tmp1);                           \
581     tcg_temp_free_i32(tmp2);                                          \
582     tcg_temp_free_i32(tmp1);                                          \
583 }
584 GEN_SHIFT(shl)
585 GEN_SHIFT(shr)
586 #undef GEN_SHIFT
587
588 static void gen_sar(TCGv_i32 dest, TCGv_i32 t0, TCGv_i32 t1)
589 {
590     TCGv_i32 tmp1, tmp2;
591     tmp1 = tcg_temp_new_i32();
592     tcg_gen_andi_i32(tmp1, t1, 0xff);
593     tmp2 = tcg_const_i32(0x1f);
594     tcg_gen_movcond_i32(TCG_COND_GTU, tmp1, tmp1, tmp2, tmp2, tmp1);
595     tcg_temp_free_i32(tmp2);
596     tcg_gen_sar_i32(dest, t0, tmp1);
597     tcg_temp_free_i32(tmp1);
598 }
599
600 static void tcg_gen_abs_i32(TCGv_i32 dest, TCGv_i32 src)
601 {
602     TCGv_i32 c0 = tcg_const_i32(0);
603     TCGv_i32 tmp = tcg_temp_new_i32();
604     tcg_gen_neg_i32(tmp, src);
605     tcg_gen_movcond_i32(TCG_COND_GT, dest, src, c0, src, tmp);
606     tcg_temp_free_i32(c0);
607     tcg_temp_free_i32(tmp);
608 }
609
610 static void shifter_out_im(TCGv_i32 var, int shift)
611 {
612     if (shift == 0) {
613         tcg_gen_andi_i32(cpu_CF, var, 1);
614     } else {
615         tcg_gen_shri_i32(cpu_CF, var, shift);
616         if (shift != 31) {
617             tcg_gen_andi_i32(cpu_CF, cpu_CF, 1);
618         }
619     }
620 }
621
622 /* Shift by immediate.  Includes special handling for shift == 0.  */
623 static inline void gen_arm_shift_im(TCGv_i32 var, int shiftop,
624                                     int shift, int flags)
625 {
626     switch (shiftop) {
627     case 0: /* LSL */
628         if (shift != 0) {
629             if (flags)
630                 shifter_out_im(var, 32 - shift);
631             tcg_gen_shli_i32(var, var, shift);
632         }
633         break;
634     case 1: /* LSR */
635         if (shift == 0) {
636             if (flags) {
637                 tcg_gen_shri_i32(cpu_CF, var, 31);
638             }
639             tcg_gen_movi_i32(var, 0);
640         } else {
641             if (flags)
642                 shifter_out_im(var, shift - 1);
643             tcg_gen_shri_i32(var, var, shift);
644         }
645         break;
646     case 2: /* ASR */
647         if (shift == 0)
648             shift = 32;
649         if (flags)
650             shifter_out_im(var, shift - 1);
651         if (shift == 32)
652           shift = 31;
653         tcg_gen_sari_i32(var, var, shift);
654         break;
655     case 3: /* ROR/RRX */
656         if (shift != 0) {
657             if (flags)
658                 shifter_out_im(var, shift - 1);
659             tcg_gen_rotri_i32(var, var, shift); break;
660         } else {
661             TCGv_i32 tmp = tcg_temp_new_i32();
662             tcg_gen_shli_i32(tmp, cpu_CF, 31);
663             if (flags)
664                 shifter_out_im(var, 0);
665             tcg_gen_shri_i32(var, var, 1);
666             tcg_gen_or_i32(var, var, tmp);
667             tcg_temp_free_i32(tmp);
668         }
669     }
670 };
671
672 static inline void gen_arm_shift_reg(TCGv_i32 var, int shiftop,
673                                      TCGv_i32 shift, int flags)
674 {
675     if (flags) {
676         switch (shiftop) {
677         case 0: gen_helper_shl_cc(var, cpu_env, var, shift); break;
678         case 1: gen_helper_shr_cc(var, cpu_env, var, shift); break;
679         case 2: gen_helper_sar_cc(var, cpu_env, var, shift); break;
680         case 3: gen_helper_ror_cc(var, cpu_env, var, shift); break;
681         }
682     } else {
683         switch (shiftop) {
684         case 0:
685             gen_shl(var, var, shift);
686             break;
687         case 1:
688             gen_shr(var, var, shift);
689             break;
690         case 2:
691             gen_sar(var, var, shift);
692             break;
693         case 3: tcg_gen_andi_i32(shift, shift, 0x1f);
694                 tcg_gen_rotr_i32(var, var, shift); break;
695         }
696     }
697     tcg_temp_free_i32(shift);
698 }
699
700 #define PAS_OP(pfx) \
701     switch (op2) {  \
702     case 0: gen_pas_helper(glue(pfx,add16)); break; \
703     case 1: gen_pas_helper(glue(pfx,addsubx)); break; \
704     case 2: gen_pas_helper(glue(pfx,subaddx)); break; \
705     case 3: gen_pas_helper(glue(pfx,sub16)); break; \
706     case 4: gen_pas_helper(glue(pfx,add8)); break; \
707     case 7: gen_pas_helper(glue(pfx,sub8)); break; \
708     }
709 static void gen_arm_parallel_addsub(int op1, int op2, TCGv_i32 a, TCGv_i32 b)
710 {
711     TCGv_ptr tmp;
712
713     switch (op1) {
714 #define gen_pas_helper(name) glue(gen_helper_,name)(a, a, b, tmp)
715     case 1:
716         tmp = tcg_temp_new_ptr();
717         tcg_gen_addi_ptr(tmp, cpu_env, offsetof(CPUARMState, GE));
718         PAS_OP(s)
719         tcg_temp_free_ptr(tmp);
720         break;
721     case 5:
722         tmp = tcg_temp_new_ptr();
723         tcg_gen_addi_ptr(tmp, cpu_env, offsetof(CPUARMState, GE));
724         PAS_OP(u)
725         tcg_temp_free_ptr(tmp);
726         break;
727 #undef gen_pas_helper
728 #define gen_pas_helper(name) glue(gen_helper_,name)(a, a, b)
729     case 2:
730         PAS_OP(q);
731         break;
732     case 3:
733         PAS_OP(sh);
734         break;
735     case 6:
736         PAS_OP(uq);
737         break;
738     case 7:
739         PAS_OP(uh);
740         break;
741 #undef gen_pas_helper
742     }
743 }
744 #undef PAS_OP
745
746 /* For unknown reasons Arm and Thumb-2 use arbitrarily different encodings.  */
747 #define PAS_OP(pfx) \
748     switch (op1) {  \
749     case 0: gen_pas_helper(glue(pfx,add8)); break; \
750     case 1: gen_pas_helper(glue(pfx,add16)); break; \
751     case 2: gen_pas_helper(glue(pfx,addsubx)); break; \
752     case 4: gen_pas_helper(glue(pfx,sub8)); break; \
753     case 5: gen_pas_helper(glue(pfx,sub16)); break; \
754     case 6: gen_pas_helper(glue(pfx,subaddx)); break; \
755     }
756 static void gen_thumb2_parallel_addsub(int op1, int op2, TCGv_i32 a, TCGv_i32 b)
757 {
758     TCGv_ptr tmp;
759
760     switch (op2) {
761 #define gen_pas_helper(name) glue(gen_helper_,name)(a, a, b, tmp)
762     case 0:
763         tmp = tcg_temp_new_ptr();
764         tcg_gen_addi_ptr(tmp, cpu_env, offsetof(CPUARMState, GE));
765         PAS_OP(s)
766         tcg_temp_free_ptr(tmp);
767         break;
768     case 4:
769         tmp = tcg_temp_new_ptr();
770         tcg_gen_addi_ptr(tmp, cpu_env, offsetof(CPUARMState, GE));
771         PAS_OP(u)
772         tcg_temp_free_ptr(tmp);
773         break;
774 #undef gen_pas_helper
775 #define gen_pas_helper(name) glue(gen_helper_,name)(a, a, b)
776     case 1:
777         PAS_OP(q);
778         break;
779     case 2:
780         PAS_OP(sh);
781         break;
782     case 5:
783         PAS_OP(uq);
784         break;
785     case 6:
786         PAS_OP(uh);
787         break;
788 #undef gen_pas_helper
789     }
790 }
791 #undef PAS_OP
792
793 /*
794  * Generate a conditional based on ARM condition code cc.
795  * This is common between ARM and Aarch64 targets.
796  */
797 void arm_test_cc(DisasCompare *cmp, int cc)
798 {
799     TCGv_i32 value;
800     TCGCond cond;
801     bool global = true;
802
803     switch (cc) {
804     case 0: /* eq: Z */
805     case 1: /* ne: !Z */
806         cond = TCG_COND_EQ;
807         value = cpu_ZF;
808         break;
809
810     case 2: /* cs: C */
811     case 3: /* cc: !C */
812         cond = TCG_COND_NE;
813         value = cpu_CF;
814         break;
815
816     case 4: /* mi: N */
817     case 5: /* pl: !N */
818         cond = TCG_COND_LT;
819         value = cpu_NF;
820         break;
821
822     case 6: /* vs: V */
823     case 7: /* vc: !V */
824         cond = TCG_COND_LT;
825         value = cpu_VF;
826         break;
827
828     case 8: /* hi: C && !Z */
829     case 9: /* ls: !C || Z -> !(C && !Z) */
830         cond = TCG_COND_NE;
831         value = tcg_temp_new_i32();
832         global = false;
833         /* CF is 1 for C, so -CF is an all-bits-set mask for C;
834            ZF is non-zero for !Z; so AND the two subexpressions.  */
835         tcg_gen_neg_i32(value, cpu_CF);
836         tcg_gen_and_i32(value, value, cpu_ZF);
837         break;
838
839     case 10: /* ge: N == V -> N ^ V == 0 */
840     case 11: /* lt: N != V -> N ^ V != 0 */
841         /* Since we're only interested in the sign bit, == 0 is >= 0.  */
842         cond = TCG_COND_GE;
843         value = tcg_temp_new_i32();
844         global = false;
845         tcg_gen_xor_i32(value, cpu_VF, cpu_NF);
846         break;
847
848     case 12: /* gt: !Z && N == V */
849     case 13: /* le: Z || N != V */
850         cond = TCG_COND_NE;
851         value = tcg_temp_new_i32();
852         global = false;
853         /* (N == V) is equal to the sign bit of ~(NF ^ VF).  Propagate
854          * the sign bit then AND with ZF to yield the result.  */
855         tcg_gen_xor_i32(value, cpu_VF, cpu_NF);
856         tcg_gen_sari_i32(value, value, 31);
857         tcg_gen_andc_i32(value, cpu_ZF, value);
858         break;
859
860     case 14: /* always */
861     case 15: /* always */
862         /* Use the ALWAYS condition, which will fold early.
863          * It doesn't matter what we use for the value.  */
864         cond = TCG_COND_ALWAYS;
865         value = cpu_ZF;
866         goto no_invert;
867
868     default:
869         fprintf(stderr, "Bad condition code 0x%x\n", cc);
870         abort();
871     }
872
873     if (cc & 1) {
874         cond = tcg_invert_cond(cond);
875     }
876
877  no_invert:
878     cmp->cond = cond;
879     cmp->value = value;
880     cmp->value_global = global;
881 }
882
883 void arm_free_cc(DisasCompare *cmp)
884 {
885     if (!cmp->value_global) {
886         tcg_temp_free_i32(cmp->value);
887     }
888 }
889
890 void arm_jump_cc(DisasCompare *cmp, TCGLabel *label)
891 {
892     tcg_gen_brcondi_i32(cmp->cond, cmp->value, 0, label);
893 }
894
895 void arm_gen_test_cc(int cc, TCGLabel *label)
896 {
897     DisasCompare cmp;
898     arm_test_cc(&cmp, cc);
899     arm_jump_cc(&cmp, label);
900     arm_free_cc(&cmp);
901 }
902
903 static const uint8_t table_logic_cc[16] = {
904     1, /* and */
905     1, /* xor */
906     0, /* sub */
907     0, /* rsb */
908     0, /* add */
909     0, /* adc */
910     0, /* sbc */
911     0, /* rsc */
912     1, /* andl */
913     1, /* xorl */
914     0, /* cmp */
915     0, /* cmn */
916     1, /* orr */
917     1, /* mov */
918     1, /* bic */
919     1, /* mvn */
920 };
921
922 static inline void gen_set_condexec(DisasContext *s)
923 {
924     if (s->condexec_mask) {
925         uint32_t val = (s->condexec_cond << 4) | (s->condexec_mask >> 1);
926         TCGv_i32 tmp = tcg_temp_new_i32();
927         tcg_gen_movi_i32(tmp, val);
928         store_cpu_field(tmp, condexec_bits);
929     }
930 }
931
932 static inline void gen_set_pc_im(DisasContext *s, target_ulong val)
933 {
934     tcg_gen_movi_i32(cpu_R[15], val);
935 }
936
937 /* Set PC and Thumb state from an immediate address.  */
938 static inline void gen_bx_im(DisasContext *s, uint32_t addr)
939 {
940     TCGv_i32 tmp;
941
942     s->base.is_jmp = DISAS_JUMP;
943     if (s->thumb != (addr & 1)) {
944         tmp = tcg_temp_new_i32();
945         tcg_gen_movi_i32(tmp, addr & 1);
946         tcg_gen_st_i32(tmp, cpu_env, offsetof(CPUARMState, thumb));
947         tcg_temp_free_i32(tmp);
948     }
949     tcg_gen_movi_i32(cpu_R[15], addr & ~1);
950 }
951
952 /* Set PC and Thumb state from var.  var is marked as dead.  */
953 static inline void gen_bx(DisasContext *s, TCGv_i32 var)
954 {
955     s->base.is_jmp = DISAS_JUMP;
956     tcg_gen_andi_i32(cpu_R[15], var, ~1);
957     tcg_gen_andi_i32(var, var, 1);
958     store_cpu_field(var, thumb);
959 }
960
961 /* Set PC and Thumb state from var. var is marked as dead.
962  * For M-profile CPUs, include logic to detect exception-return
963  * branches and handle them. This is needed for Thumb POP/LDM to PC, LDR to PC,
964  * and BX reg, and no others, and happens only for code in Handler mode.
965  */
966 static inline void gen_bx_excret(DisasContext *s, TCGv_i32 var)
967 {
968     /* Generate the same code here as for a simple bx, but flag via
969      * s->base.is_jmp that we need to do the rest of the work later.
970      */
971     gen_bx(s, var);
972     if (arm_dc_feature(s, ARM_FEATURE_M_SECURITY) ||
973         (s->v7m_handler_mode && arm_dc_feature(s, ARM_FEATURE_M))) {
974         s->base.is_jmp = DISAS_BX_EXCRET;
975     }
976 }
977
978 static inline void gen_bx_excret_final_code(DisasContext *s)
979 {
980     /* Generate the code to finish possible exception return and end the TB */
981     TCGLabel *excret_label = gen_new_label();
982     uint32_t min_magic;
983
984     if (arm_dc_feature(s, ARM_FEATURE_M_SECURITY)) {
985         /* Covers FNC_RETURN and EXC_RETURN magic */
986         min_magic = FNC_RETURN_MIN_MAGIC;
987     } else {
988         /* EXC_RETURN magic only */
989         min_magic = EXC_RETURN_MIN_MAGIC;
990     }
991
992     /* Is the new PC value in the magic range indicating exception return? */
993     tcg_gen_brcondi_i32(TCG_COND_GEU, cpu_R[15], min_magic, excret_label);
994     /* No: end the TB as we would for a DISAS_JMP */
995     if (is_singlestepping(s)) {
996         gen_singlestep_exception(s);
997     } else {
998         tcg_gen_exit_tb(0);
999     }
1000     gen_set_label(excret_label);
1001     /* Yes: this is an exception return.
1002      * At this point in runtime env->regs[15] and env->thumb will hold
1003      * the exception-return magic number, which do_v7m_exception_exit()
1004      * will read. Nothing else will be able to see those values because
1005      * the cpu-exec main loop guarantees that we will always go straight
1006      * from raising the exception to the exception-handling code.
1007      *
1008      * gen_ss_advance(s) does nothing on M profile currently but
1009      * calling it is conceptually the right thing as we have executed
1010      * this instruction (compare SWI, HVC, SMC handling).
1011      */
1012     gen_ss_advance(s);
1013     gen_exception_internal(EXCP_EXCEPTION_EXIT);
1014 }
1015
1016 static inline void gen_bxns(DisasContext *s, int rm)
1017 {
1018     TCGv_i32 var = load_reg(s, rm);
1019
1020     /* The bxns helper may raise an EXCEPTION_EXIT exception, so in theory
1021      * we need to sync state before calling it, but:
1022      *  - we don't need to do gen_set_pc_im() because the bxns helper will
1023      *    always set the PC itself
1024      *  - we don't need to do gen_set_condexec() because BXNS is UNPREDICTABLE
1025      *    unless it's outside an IT block or the last insn in an IT block,
1026      *    so we know that condexec == 0 (already set at the top of the TB)
1027      *    is correct in the non-UNPREDICTABLE cases, and we can choose
1028      *    "zeroes the IT bits" as our UNPREDICTABLE behaviour otherwise.
1029      */
1030     gen_helper_v7m_bxns(cpu_env, var);
1031     tcg_temp_free_i32(var);
1032     s->base.is_jmp = DISAS_EXIT;
1033 }
1034
1035 static inline void gen_blxns(DisasContext *s, int rm)
1036 {
1037     TCGv_i32 var = load_reg(s, rm);
1038
1039     /* We don't need to sync condexec state, for the same reason as bxns.
1040      * We do however need to set the PC, because the blxns helper reads it.
1041      * The blxns helper may throw an exception.
1042      */
1043     gen_set_pc_im(s, s->pc);
1044     gen_helper_v7m_blxns(cpu_env, var);
1045     tcg_temp_free_i32(var);
1046     s->base.is_jmp = DISAS_EXIT;
1047 }
1048
1049 /* Variant of store_reg which uses branch&exchange logic when storing
1050    to r15 in ARM architecture v7 and above. The source must be a temporary
1051    and will be marked as dead. */
1052 static inline void store_reg_bx(DisasContext *s, int reg, TCGv_i32 var)
1053 {
1054     if (reg == 15 && ENABLE_ARCH_7) {
1055         gen_bx(s, var);
1056     } else {
1057         store_reg(s, reg, var);
1058     }
1059 }
1060
1061 /* Variant of store_reg which uses branch&exchange logic when storing
1062  * to r15 in ARM architecture v5T and above. This is used for storing
1063  * the results of a LDR/LDM/POP into r15, and corresponds to the cases
1064  * in the ARM ARM which use the LoadWritePC() pseudocode function. */
1065 static inline void store_reg_from_load(DisasContext *s, int reg, TCGv_i32 var)
1066 {
1067     if (reg == 15 && ENABLE_ARCH_5) {
1068         gen_bx_excret(s, var);
1069     } else {
1070         store_reg(s, reg, var);
1071     }
1072 }
1073
1074 #ifdef CONFIG_USER_ONLY
1075 #define IS_USER_ONLY 1
1076 #else
1077 #define IS_USER_ONLY 0
1078 #endif
1079
1080 /* Abstractions of "generate code to do a guest load/store for
1081  * AArch32", where a vaddr is always 32 bits (and is zero
1082  * extended if we're a 64 bit core) and  data is also
1083  * 32 bits unless specifically doing a 64 bit access.
1084  * These functions work like tcg_gen_qemu_{ld,st}* except
1085  * that the address argument is TCGv_i32 rather than TCGv.
1086  */
1087
1088 static inline TCGv gen_aa32_addr(DisasContext *s, TCGv_i32 a32, TCGMemOp op)
1089 {
1090     TCGv addr = tcg_temp_new();
1091     tcg_gen_extu_i32_tl(addr, a32);
1092
1093     /* Not needed for user-mode BE32, where we use MO_BE instead.  */
1094     if (!IS_USER_ONLY && s->sctlr_b && (op & MO_SIZE) < MO_32) {
1095         tcg_gen_xori_tl(addr, addr, 4 - (1 << (op & MO_SIZE)));
1096     }
1097     return addr;
1098 }
1099
1100 static void gen_aa32_ld_i32(DisasContext *s, TCGv_i32 val, TCGv_i32 a32,
1101                             int index, TCGMemOp opc)
1102 {
1103     TCGv addr = gen_aa32_addr(s, a32, opc);
1104     tcg_gen_qemu_ld_i32(val, addr, index, opc);
1105     tcg_temp_free(addr);
1106 }
1107
1108 static void gen_aa32_st_i32(DisasContext *s, TCGv_i32 val, TCGv_i32 a32,
1109                             int index, TCGMemOp opc)
1110 {
1111     TCGv addr = gen_aa32_addr(s, a32, opc);
1112     tcg_gen_qemu_st_i32(val, addr, index, opc);
1113     tcg_temp_free(addr);
1114 }
1115
1116 #define DO_GEN_LD(SUFF, OPC)                                             \
1117 static inline void gen_aa32_ld##SUFF(DisasContext *s, TCGv_i32 val,      \
1118                                      TCGv_i32 a32, int index)            \
1119 {                                                                        \
1120     gen_aa32_ld_i32(s, val, a32, index, OPC | s->be_data);               \
1121 }                                                                        \
1122 static inline void gen_aa32_ld##SUFF##_iss(DisasContext *s,              \
1123                                            TCGv_i32 val,                 \
1124                                            TCGv_i32 a32, int index,      \
1125                                            ISSInfo issinfo)              \
1126 {                                                                        \
1127     gen_aa32_ld##SUFF(s, val, a32, index);                               \
1128     disas_set_da_iss(s, OPC, issinfo);                                   \
1129 }
1130
1131 #define DO_GEN_ST(SUFF, OPC)                                             \
1132 static inline void gen_aa32_st##SUFF(DisasContext *s, TCGv_i32 val,      \
1133                                      TCGv_i32 a32, int index)            \
1134 {                                                                        \
1135     gen_aa32_st_i32(s, val, a32, index, OPC | s->be_data);               \
1136 }                                                                        \
1137 static inline void gen_aa32_st##SUFF##_iss(DisasContext *s,              \
1138                                            TCGv_i32 val,                 \
1139                                            TCGv_i32 a32, int index,      \
1140                                            ISSInfo issinfo)              \
1141 {                                                                        \
1142     gen_aa32_st##SUFF(s, val, a32, index);                               \
1143     disas_set_da_iss(s, OPC, issinfo | ISSIsWrite);                      \
1144 }
1145
1146 static inline void gen_aa32_frob64(DisasContext *s, TCGv_i64 val)
1147 {
1148     /* Not needed for user-mode BE32, where we use MO_BE instead.  */
1149     if (!IS_USER_ONLY && s->sctlr_b) {
1150         tcg_gen_rotri_i64(val, val, 32);
1151     }
1152 }
1153
1154 static void gen_aa32_ld_i64(DisasContext *s, TCGv_i64 val, TCGv_i32 a32,
1155                             int index, TCGMemOp opc)
1156 {
1157     TCGv addr = gen_aa32_addr(s, a32, opc);
1158     tcg_gen_qemu_ld_i64(val, addr, index, opc);
1159     gen_aa32_frob64(s, val);
1160     tcg_temp_free(addr);
1161 }
1162
1163 static inline void gen_aa32_ld64(DisasContext *s, TCGv_i64 val,
1164                                  TCGv_i32 a32, int index)
1165 {
1166     gen_aa32_ld_i64(s, val, a32, index, MO_Q | s->be_data);
1167 }
1168
1169 static void gen_aa32_st_i64(DisasContext *s, TCGv_i64 val, TCGv_i32 a32,
1170                             int index, TCGMemOp opc)
1171 {
1172     TCGv addr = gen_aa32_addr(s, a32, opc);
1173
1174     /* Not needed for user-mode BE32, where we use MO_BE instead.  */
1175     if (!IS_USER_ONLY && s->sctlr_b) {
1176         TCGv_i64 tmp = tcg_temp_new_i64();
1177         tcg_gen_rotri_i64(tmp, val, 32);
1178         tcg_gen_qemu_st_i64(tmp, addr, index, opc);
1179         tcg_temp_free_i64(tmp);
1180     } else {
1181         tcg_gen_qemu_st_i64(val, addr, index, opc);
1182     }
1183     tcg_temp_free(addr);
1184 }
1185
1186 static inline void gen_aa32_st64(DisasContext *s, TCGv_i64 val,
1187                                  TCGv_i32 a32, int index)
1188 {
1189     gen_aa32_st_i64(s, val, a32, index, MO_Q | s->be_data);
1190 }
1191
1192 DO_GEN_LD(8s, MO_SB)
1193 DO_GEN_LD(8u, MO_UB)
1194 DO_GEN_LD(16s, MO_SW)
1195 DO_GEN_LD(16u, MO_UW)
1196 DO_GEN_LD(32u, MO_UL)
1197 DO_GEN_ST(8, MO_UB)
1198 DO_GEN_ST(16, MO_UW)
1199 DO_GEN_ST(32, MO_UL)
1200
1201 static inline void gen_hvc(DisasContext *s, int imm16)
1202 {
1203     /* The pre HVC helper handles cases when HVC gets trapped
1204      * as an undefined insn by runtime configuration (ie before
1205      * the insn really executes).
1206      */
1207     gen_set_pc_im(s, s->pc - 4);
1208     gen_helper_pre_hvc(cpu_env);
1209     /* Otherwise we will treat this as a real exception which
1210      * happens after execution of the insn. (The distinction matters
1211      * for the PC value reported to the exception handler and also
1212      * for single stepping.)
1213      */
1214     s->svc_imm = imm16;
1215     gen_set_pc_im(s, s->pc);
1216     s->base.is_jmp = DISAS_HVC;
1217 }
1218
1219 static inline void gen_smc(DisasContext *s)
1220 {
1221     /* As with HVC, we may take an exception either before or after
1222      * the insn executes.
1223      */
1224     TCGv_i32 tmp;
1225
1226     gen_set_pc_im(s, s->pc - 4);
1227     tmp = tcg_const_i32(syn_aa32_smc());
1228     gen_helper_pre_smc(cpu_env, tmp);
1229     tcg_temp_free_i32(tmp);
1230     gen_set_pc_im(s, s->pc);
1231     s->base.is_jmp = DISAS_SMC;
1232 }
1233
1234 static void gen_exception_internal_insn(DisasContext *s, int offset, int excp)
1235 {
1236     gen_set_condexec(s);
1237     gen_set_pc_im(s, s->pc - offset);
1238     gen_exception_internal(excp);
1239     s->base.is_jmp = DISAS_NORETURN;
1240 }
1241
1242 static void gen_exception_insn(DisasContext *s, int offset, int excp,
1243                                int syn, uint32_t target_el)
1244 {
1245     gen_set_condexec(s);
1246     gen_set_pc_im(s, s->pc - offset);
1247     gen_exception(excp, syn, target_el);
1248     s->base.is_jmp = DISAS_NORETURN;
1249 }
1250
1251 static void gen_exception_bkpt_insn(DisasContext *s, int offset, uint32_t syn)
1252 {
1253     TCGv_i32 tcg_syn;
1254
1255     gen_set_condexec(s);
1256     gen_set_pc_im(s, s->pc - offset);
1257     tcg_syn = tcg_const_i32(syn);
1258     gen_helper_exception_bkpt_insn(cpu_env, tcg_syn);
1259     tcg_temp_free_i32(tcg_syn);
1260     s->base.is_jmp = DISAS_NORETURN;
1261 }
1262
1263 /* Force a TB lookup after an instruction that changes the CPU state.  */
1264 static inline void gen_lookup_tb(DisasContext *s)
1265 {
1266     tcg_gen_movi_i32(cpu_R[15], s->pc & ~1);
1267     s->base.is_jmp = DISAS_EXIT;
1268 }
1269
1270 static inline void gen_hlt(DisasContext *s, int imm)
1271 {
1272     /* HLT. This has two purposes.
1273      * Architecturally, it is an external halting debug instruction.
1274      * Since QEMU doesn't implement external debug, we treat this as
1275      * it is required for halting debug disabled: it will UNDEF.
1276      * Secondly, "HLT 0x3C" is a T32 semihosting trap instruction,
1277      * and "HLT 0xF000" is an A32 semihosting syscall. These traps
1278      * must trigger semihosting even for ARMv7 and earlier, where
1279      * HLT was an undefined encoding.
1280      * In system mode, we don't allow userspace access to
1281      * semihosting, to provide some semblance of security
1282      * (and for consistency with our 32-bit semihosting).
1283      */
1284     if (semihosting_enabled() &&
1285 #ifndef CONFIG_USER_ONLY
1286         s->current_el != 0 &&
1287 #endif
1288         (imm == (s->thumb ? 0x3c : 0xf000))) {
1289         gen_exception_internal_insn(s, 0, EXCP_SEMIHOST);
1290         return;
1291     }
1292
1293     gen_exception_insn(s, s->thumb ? 2 : 4, EXCP_UDEF, syn_uncategorized(),
1294                        default_exception_el(s));
1295 }
1296
1297 static inline void gen_add_data_offset(DisasContext *s, unsigned int insn,
1298                                        TCGv_i32 var)
1299 {
1300     int val, rm, shift, shiftop;
1301     TCGv_i32 offset;
1302
1303     if (!(insn & (1 << 25))) {
1304         /* immediate */
1305         val = insn & 0xfff;
1306         if (!(insn & (1 << 23)))
1307             val = -val;
1308         if (val != 0)
1309             tcg_gen_addi_i32(var, var, val);
1310     } else {
1311         /* shift/register */
1312         rm = (insn) & 0xf;
1313         shift = (insn >> 7) & 0x1f;
1314         shiftop = (insn >> 5) & 3;
1315         offset = load_reg(s, rm);
1316         gen_arm_shift_im(offset, shiftop, shift, 0);
1317         if (!(insn & (1 << 23)))
1318             tcg_gen_sub_i32(var, var, offset);
1319         else
1320             tcg_gen_add_i32(var, var, offset);
1321         tcg_temp_free_i32(offset);
1322     }
1323 }
1324
1325 static inline void gen_add_datah_offset(DisasContext *s, unsigned int insn,
1326                                         int extra, TCGv_i32 var)
1327 {
1328     int val, rm;
1329     TCGv_i32 offset;
1330
1331     if (insn & (1 << 22)) {
1332         /* immediate */
1333         val = (insn & 0xf) | ((insn >> 4) & 0xf0);
1334         if (!(insn & (1 << 23)))
1335             val = -val;
1336         val += extra;
1337         if (val != 0)
1338             tcg_gen_addi_i32(var, var, val);
1339     } else {
1340         /* register */
1341         if (extra)
1342             tcg_gen_addi_i32(var, var, extra);
1343         rm = (insn) & 0xf;
1344         offset = load_reg(s, rm);
1345         if (!(insn & (1 << 23)))
1346             tcg_gen_sub_i32(var, var, offset);
1347         else
1348             tcg_gen_add_i32(var, var, offset);
1349         tcg_temp_free_i32(offset);
1350     }
1351 }
1352
1353 static TCGv_ptr get_fpstatus_ptr(int neon)
1354 {
1355     TCGv_ptr statusptr = tcg_temp_new_ptr();
1356     int offset;
1357     if (neon) {
1358         offset = offsetof(CPUARMState, vfp.standard_fp_status);
1359     } else {
1360         offset = offsetof(CPUARMState, vfp.fp_status);
1361     }
1362     tcg_gen_addi_ptr(statusptr, cpu_env, offset);
1363     return statusptr;
1364 }
1365
1366 #define VFP_OP2(name)                                                 \
1367 static inline void gen_vfp_##name(int dp)                             \
1368 {                                                                     \
1369     TCGv_ptr fpst = get_fpstatus_ptr(0);                              \
1370     if (dp) {                                                         \
1371         gen_helper_vfp_##name##d(cpu_F0d, cpu_F0d, cpu_F1d, fpst);    \
1372     } else {                                                          \
1373         gen_helper_vfp_##name##s(cpu_F0s, cpu_F0s, cpu_F1s, fpst);    \
1374     }                                                                 \
1375     tcg_temp_free_ptr(fpst);                                          \
1376 }
1377
1378 VFP_OP2(add)
1379 VFP_OP2(sub)
1380 VFP_OP2(mul)
1381 VFP_OP2(div)
1382
1383 #undef VFP_OP2
1384
1385 static inline void gen_vfp_F1_mul(int dp)
1386 {
1387     /* Like gen_vfp_mul() but put result in F1 */
1388     TCGv_ptr fpst = get_fpstatus_ptr(0);
1389     if (dp) {
1390         gen_helper_vfp_muld(cpu_F1d, cpu_F0d, cpu_F1d, fpst);
1391     } else {
1392         gen_helper_vfp_muls(cpu_F1s, cpu_F0s, cpu_F1s, fpst);
1393     }
1394     tcg_temp_free_ptr(fpst);
1395 }
1396
1397 static inline void gen_vfp_F1_neg(int dp)
1398 {
1399     /* Like gen_vfp_neg() but put result in F1 */
1400     if (dp) {
1401         gen_helper_vfp_negd(cpu_F1d, cpu_F0d);
1402     } else {
1403         gen_helper_vfp_negs(cpu_F1s, cpu_F0s);
1404     }
1405 }
1406
1407 static inline void gen_vfp_abs(int dp)
1408 {
1409     if (dp)
1410         gen_helper_vfp_absd(cpu_F0d, cpu_F0d);
1411     else
1412         gen_helper_vfp_abss(cpu_F0s, cpu_F0s);
1413 }
1414
1415 static inline void gen_vfp_neg(int dp)
1416 {
1417     if (dp)
1418         gen_helper_vfp_negd(cpu_F0d, cpu_F0d);
1419     else
1420         gen_helper_vfp_negs(cpu_F0s, cpu_F0s);
1421 }
1422
1423 static inline void gen_vfp_sqrt(int dp)
1424 {
1425     if (dp)
1426         gen_helper_vfp_sqrtd(cpu_F0d, cpu_F0d, cpu_env);
1427     else
1428         gen_helper_vfp_sqrts(cpu_F0s, cpu_F0s, cpu_env);
1429 }
1430
1431 static inline void gen_vfp_cmp(int dp)
1432 {
1433     if (dp)
1434         gen_helper_vfp_cmpd(cpu_F0d, cpu_F1d, cpu_env);
1435     else
1436         gen_helper_vfp_cmps(cpu_F0s, cpu_F1s, cpu_env);
1437 }
1438
1439 static inline void gen_vfp_cmpe(int dp)
1440 {
1441     if (dp)
1442         gen_helper_vfp_cmped(cpu_F0d, cpu_F1d, cpu_env);
1443     else
1444         gen_helper_vfp_cmpes(cpu_F0s, cpu_F1s, cpu_env);
1445 }
1446
1447 static inline void gen_vfp_F1_ld0(int dp)
1448 {
1449     if (dp)
1450         tcg_gen_movi_i64(cpu_F1d, 0);
1451     else
1452         tcg_gen_movi_i32(cpu_F1s, 0);
1453 }
1454
1455 #define VFP_GEN_ITOF(name) \
1456 static inline void gen_vfp_##name(int dp, int neon) \
1457 { \
1458     TCGv_ptr statusptr = get_fpstatus_ptr(neon); \
1459     if (dp) { \
1460         gen_helper_vfp_##name##d(cpu_F0d, cpu_F0s, statusptr); \
1461     } else { \
1462         gen_helper_vfp_##name##s(cpu_F0s, cpu_F0s, statusptr); \
1463     } \
1464     tcg_temp_free_ptr(statusptr); \
1465 }
1466
1467 VFP_GEN_ITOF(uito)
1468 VFP_GEN_ITOF(sito)
1469 #undef VFP_GEN_ITOF
1470
1471 #define VFP_GEN_FTOI(name) \
1472 static inline void gen_vfp_##name(int dp, int neon) \
1473 { \
1474     TCGv_ptr statusptr = get_fpstatus_ptr(neon); \
1475     if (dp) { \
1476         gen_helper_vfp_##name##d(cpu_F0s, cpu_F0d, statusptr); \
1477     } else { \
1478         gen_helper_vfp_##name##s(cpu_F0s, cpu_F0s, statusptr); \
1479     } \
1480     tcg_temp_free_ptr(statusptr); \
1481 }
1482
1483 VFP_GEN_FTOI(toui)
1484 VFP_GEN_FTOI(touiz)
1485 VFP_GEN_FTOI(tosi)
1486 VFP_GEN_FTOI(tosiz)
1487 #undef VFP_GEN_FTOI
1488
1489 #define VFP_GEN_FIX(name, round) \
1490 static inline void gen_vfp_##name(int dp, int shift, int neon) \
1491 { \
1492     TCGv_i32 tmp_shift = tcg_const_i32(shift); \
1493     TCGv_ptr statusptr = get_fpstatus_ptr(neon); \
1494     if (dp) { \
1495         gen_helper_vfp_##name##d##round(cpu_F0d, cpu_F0d, tmp_shift, \
1496                                         statusptr); \
1497     } else { \
1498         gen_helper_vfp_##name##s##round(cpu_F0s, cpu_F0s, tmp_shift, \
1499                                         statusptr); \
1500     } \
1501     tcg_temp_free_i32(tmp_shift); \
1502     tcg_temp_free_ptr(statusptr); \
1503 }
1504 VFP_GEN_FIX(tosh, _round_to_zero)
1505 VFP_GEN_FIX(tosl, _round_to_zero)
1506 VFP_GEN_FIX(touh, _round_to_zero)
1507 VFP_GEN_FIX(toul, _round_to_zero)
1508 VFP_GEN_FIX(shto, )
1509 VFP_GEN_FIX(slto, )
1510 VFP_GEN_FIX(uhto, )
1511 VFP_GEN_FIX(ulto, )
1512 #undef VFP_GEN_FIX
1513
1514 static inline void gen_vfp_ld(DisasContext *s, int dp, TCGv_i32 addr)
1515 {
1516     if (dp) {
1517         gen_aa32_ld64(s, cpu_F0d, addr, get_mem_index(s));
1518     } else {
1519         gen_aa32_ld32u(s, cpu_F0s, addr, get_mem_index(s));
1520     }
1521 }
1522
1523 static inline void gen_vfp_st(DisasContext *s, int dp, TCGv_i32 addr)
1524 {
1525     if (dp) {
1526         gen_aa32_st64(s, cpu_F0d, addr, get_mem_index(s));
1527     } else {
1528         gen_aa32_st32(s, cpu_F0s, addr, get_mem_index(s));
1529     }
1530 }
1531
1532 static inline long vfp_reg_offset(bool dp, unsigned reg)
1533 {
1534     if (dp) {
1535         return offsetof(CPUARMState, vfp.zregs[reg >> 1].d[reg & 1]);
1536     } else {
1537         long ofs = offsetof(CPUARMState, vfp.zregs[reg >> 2].d[(reg >> 1) & 1]);
1538         if (reg & 1) {
1539             ofs += offsetof(CPU_DoubleU, l.upper);
1540         } else {
1541             ofs += offsetof(CPU_DoubleU, l.lower);
1542         }
1543         return ofs;
1544     }
1545 }
1546
1547 /* Return the offset of a 32-bit piece of a NEON register.
1548    zero is the least significant end of the register.  */
1549 static inline long
1550 neon_reg_offset (int reg, int n)
1551 {
1552     int sreg;
1553     sreg = reg * 2 + n;
1554     return vfp_reg_offset(0, sreg);
1555 }
1556
1557 static TCGv_i32 neon_load_reg(int reg, int pass)
1558 {
1559     TCGv_i32 tmp = tcg_temp_new_i32();
1560     tcg_gen_ld_i32(tmp, cpu_env, neon_reg_offset(reg, pass));
1561     return tmp;
1562 }
1563
1564 static void neon_store_reg(int reg, int pass, TCGv_i32 var)
1565 {
1566     tcg_gen_st_i32(var, cpu_env, neon_reg_offset(reg, pass));
1567     tcg_temp_free_i32(var);
1568 }
1569
1570 static inline void neon_load_reg64(TCGv_i64 var, int reg)
1571 {
1572     tcg_gen_ld_i64(var, cpu_env, vfp_reg_offset(1, reg));
1573 }
1574
1575 static inline void neon_store_reg64(TCGv_i64 var, int reg)
1576 {
1577     tcg_gen_st_i64(var, cpu_env, vfp_reg_offset(1, reg));
1578 }
1579
1580 static TCGv_ptr vfp_reg_ptr(bool dp, int reg)
1581 {
1582     TCGv_ptr ret = tcg_temp_new_ptr();
1583     tcg_gen_addi_ptr(ret, cpu_env, vfp_reg_offset(dp, reg));
1584     return ret;
1585 }
1586
1587 #define tcg_gen_ld_f32 tcg_gen_ld_i32
1588 #define tcg_gen_ld_f64 tcg_gen_ld_i64
1589 #define tcg_gen_st_f32 tcg_gen_st_i32
1590 #define tcg_gen_st_f64 tcg_gen_st_i64
1591
1592 static inline void gen_mov_F0_vreg(int dp, int reg)
1593 {
1594     if (dp)
1595         tcg_gen_ld_f64(cpu_F0d, cpu_env, vfp_reg_offset(dp, reg));
1596     else
1597         tcg_gen_ld_f32(cpu_F0s, cpu_env, vfp_reg_offset(dp, reg));
1598 }
1599
1600 static inline void gen_mov_F1_vreg(int dp, int reg)
1601 {
1602     if (dp)
1603         tcg_gen_ld_f64(cpu_F1d, cpu_env, vfp_reg_offset(dp, reg));
1604     else
1605         tcg_gen_ld_f32(cpu_F1s, cpu_env, vfp_reg_offset(dp, reg));
1606 }
1607
1608 static inline void gen_mov_vreg_F0(int dp, int reg)
1609 {
1610     if (dp)
1611         tcg_gen_st_f64(cpu_F0d, cpu_env, vfp_reg_offset(dp, reg));
1612     else
1613         tcg_gen_st_f32(cpu_F0s, cpu_env, vfp_reg_offset(dp, reg));
1614 }
1615
1616 #define ARM_CP_RW_BIT   (1 << 20)
1617
1618 static inline void iwmmxt_load_reg(TCGv_i64 var, int reg)
1619 {
1620     tcg_gen_ld_i64(var, cpu_env, offsetof(CPUARMState, iwmmxt.regs[reg]));
1621 }
1622
1623 static inline void iwmmxt_store_reg(TCGv_i64 var, int reg)
1624 {
1625     tcg_gen_st_i64(var, cpu_env, offsetof(CPUARMState, iwmmxt.regs[reg]));
1626 }
1627
1628 static inline TCGv_i32 iwmmxt_load_creg(int reg)
1629 {
1630     TCGv_i32 var = tcg_temp_new_i32();
1631     tcg_gen_ld_i32(var, cpu_env, offsetof(CPUARMState, iwmmxt.cregs[reg]));
1632     return var;
1633 }
1634
1635 static inline void iwmmxt_store_creg(int reg, TCGv_i32 var)
1636 {
1637     tcg_gen_st_i32(var, cpu_env, offsetof(CPUARMState, iwmmxt.cregs[reg]));
1638     tcg_temp_free_i32(var);
1639 }
1640
1641 static inline void gen_op_iwmmxt_movq_wRn_M0(int rn)
1642 {
1643     iwmmxt_store_reg(cpu_M0, rn);
1644 }
1645
1646 static inline void gen_op_iwmmxt_movq_M0_wRn(int rn)
1647 {
1648     iwmmxt_load_reg(cpu_M0, rn);
1649 }
1650
1651 static inline void gen_op_iwmmxt_orq_M0_wRn(int rn)
1652 {
1653     iwmmxt_load_reg(cpu_V1, rn);
1654     tcg_gen_or_i64(cpu_M0, cpu_M0, cpu_V1);
1655 }
1656
1657 static inline void gen_op_iwmmxt_andq_M0_wRn(int rn)
1658 {
1659     iwmmxt_load_reg(cpu_V1, rn);
1660     tcg_gen_and_i64(cpu_M0, cpu_M0, cpu_V1);
1661 }
1662
1663 static inline void gen_op_iwmmxt_xorq_M0_wRn(int rn)
1664 {
1665     iwmmxt_load_reg(cpu_V1, rn);
1666     tcg_gen_xor_i64(cpu_M0, cpu_M0, cpu_V1);
1667 }
1668
1669 #define IWMMXT_OP(name) \
1670 static inline void gen_op_iwmmxt_##name##_M0_wRn(int rn) \
1671 { \
1672     iwmmxt_load_reg(cpu_V1, rn); \
1673     gen_helper_iwmmxt_##name(cpu_M0, cpu_M0, cpu_V1); \
1674 }
1675
1676 #define IWMMXT_OP_ENV(name) \
1677 static inline void gen_op_iwmmxt_##name##_M0_wRn(int rn) \
1678 { \
1679     iwmmxt_load_reg(cpu_V1, rn); \
1680     gen_helper_iwmmxt_##name(cpu_M0, cpu_env, cpu_M0, cpu_V1); \
1681 }
1682
1683 #define IWMMXT_OP_ENV_SIZE(name) \
1684 IWMMXT_OP_ENV(name##b) \
1685 IWMMXT_OP_ENV(name##w) \
1686 IWMMXT_OP_ENV(name##l)
1687
1688 #define IWMMXT_OP_ENV1(name) \
1689 static inline void gen_op_iwmmxt_##name##_M0(void) \
1690 { \
1691     gen_helper_iwmmxt_##name(cpu_M0, cpu_env, cpu_M0); \
1692 }
1693
1694 IWMMXT_OP(maddsq)
1695 IWMMXT_OP(madduq)
1696 IWMMXT_OP(sadb)
1697 IWMMXT_OP(sadw)
1698 IWMMXT_OP(mulslw)
1699 IWMMXT_OP(mulshw)
1700 IWMMXT_OP(mululw)
1701 IWMMXT_OP(muluhw)
1702 IWMMXT_OP(macsw)
1703 IWMMXT_OP(macuw)
1704
1705 IWMMXT_OP_ENV_SIZE(unpackl)
1706 IWMMXT_OP_ENV_SIZE(unpackh)
1707
1708 IWMMXT_OP_ENV1(unpacklub)
1709 IWMMXT_OP_ENV1(unpackluw)
1710 IWMMXT_OP_ENV1(unpacklul)
1711 IWMMXT_OP_ENV1(unpackhub)
1712 IWMMXT_OP_ENV1(unpackhuw)
1713 IWMMXT_OP_ENV1(unpackhul)
1714 IWMMXT_OP_ENV1(unpacklsb)
1715 IWMMXT_OP_ENV1(unpacklsw)
1716 IWMMXT_OP_ENV1(unpacklsl)
1717 IWMMXT_OP_ENV1(unpackhsb)
1718 IWMMXT_OP_ENV1(unpackhsw)
1719 IWMMXT_OP_ENV1(unpackhsl)
1720
1721 IWMMXT_OP_ENV_SIZE(cmpeq)
1722 IWMMXT_OP_ENV_SIZE(cmpgtu)
1723 IWMMXT_OP_ENV_SIZE(cmpgts)
1724
1725 IWMMXT_OP_ENV_SIZE(mins)
1726 IWMMXT_OP_ENV_SIZE(minu)
1727 IWMMXT_OP_ENV_SIZE(maxs)
1728 IWMMXT_OP_ENV_SIZE(maxu)
1729
1730 IWMMXT_OP_ENV_SIZE(subn)
1731 IWMMXT_OP_ENV_SIZE(addn)
1732 IWMMXT_OP_ENV_SIZE(subu)
1733 IWMMXT_OP_ENV_SIZE(addu)
1734 IWMMXT_OP_ENV_SIZE(subs)
1735 IWMMXT_OP_ENV_SIZE(adds)
1736
1737 IWMMXT_OP_ENV(avgb0)
1738 IWMMXT_OP_ENV(avgb1)
1739 IWMMXT_OP_ENV(avgw0)
1740 IWMMXT_OP_ENV(avgw1)
1741
1742 IWMMXT_OP_ENV(packuw)
1743 IWMMXT_OP_ENV(packul)
1744 IWMMXT_OP_ENV(packuq)
1745 IWMMXT_OP_ENV(packsw)
1746 IWMMXT_OP_ENV(packsl)
1747 IWMMXT_OP_ENV(packsq)
1748
1749 static void gen_op_iwmmxt_set_mup(void)
1750 {
1751     TCGv_i32 tmp;
1752     tmp = load_cpu_field(iwmmxt.cregs[ARM_IWMMXT_wCon]);
1753     tcg_gen_ori_i32(tmp, tmp, 2);
1754     store_cpu_field(tmp, iwmmxt.cregs[ARM_IWMMXT_wCon]);
1755 }
1756
1757 static void gen_op_iwmmxt_set_cup(void)
1758 {
1759     TCGv_i32 tmp;
1760     tmp = load_cpu_field(iwmmxt.cregs[ARM_IWMMXT_wCon]);
1761     tcg_gen_ori_i32(tmp, tmp, 1);
1762     store_cpu_field(tmp, iwmmxt.cregs[ARM_IWMMXT_wCon]);
1763 }
1764
1765 static void gen_op_iwmmxt_setpsr_nz(void)
1766 {
1767     TCGv_i32 tmp = tcg_temp_new_i32();
1768     gen_helper_iwmmxt_setpsr_nz(tmp, cpu_M0);
1769     store_cpu_field(tmp, iwmmxt.cregs[ARM_IWMMXT_wCASF]);
1770 }
1771
1772 static inline void gen_op_iwmmxt_addl_M0_wRn(int rn)
1773 {
1774     iwmmxt_load_reg(cpu_V1, rn);
1775     tcg_gen_ext32u_i64(cpu_V1, cpu_V1);
1776     tcg_gen_add_i64(cpu_M0, cpu_M0, cpu_V1);
1777 }
1778
1779 static inline int gen_iwmmxt_address(DisasContext *s, uint32_t insn,
1780                                      TCGv_i32 dest)
1781 {
1782     int rd;
1783     uint32_t offset;
1784     TCGv_i32 tmp;
1785
1786     rd = (insn >> 16) & 0xf;
1787     tmp = load_reg(s, rd);
1788
1789     offset = (insn & 0xff) << ((insn >> 7) & 2);
1790     if (insn & (1 << 24)) {
1791         /* Pre indexed */
1792         if (insn & (1 << 23))
1793             tcg_gen_addi_i32(tmp, tmp, offset);
1794         else
1795             tcg_gen_addi_i32(tmp, tmp, -offset);
1796         tcg_gen_mov_i32(dest, tmp);
1797         if (insn & (1 << 21))
1798             store_reg(s, rd, tmp);
1799         else
1800             tcg_temp_free_i32(tmp);
1801     } else if (insn & (1 << 21)) {
1802         /* Post indexed */
1803         tcg_gen_mov_i32(dest, tmp);
1804         if (insn & (1 << 23))
1805             tcg_gen_addi_i32(tmp, tmp, offset);
1806         else
1807             tcg_gen_addi_i32(tmp, tmp, -offset);
1808         store_reg(s, rd, tmp);
1809     } else if (!(insn & (1 << 23)))
1810         return 1;
1811     return 0;
1812 }
1813
1814 static inline int gen_iwmmxt_shift(uint32_t insn, uint32_t mask, TCGv_i32 dest)
1815 {
1816     int rd = (insn >> 0) & 0xf;
1817     TCGv_i32 tmp;
1818
1819     if (insn & (1 << 8)) {
1820         if (rd < ARM_IWMMXT_wCGR0 || rd > ARM_IWMMXT_wCGR3) {
1821             return 1;
1822         } else {
1823             tmp = iwmmxt_load_creg(rd);
1824         }
1825     } else {
1826         tmp = tcg_temp_new_i32();
1827         iwmmxt_load_reg(cpu_V0, rd);
1828         tcg_gen_extrl_i64_i32(tmp, cpu_V0);
1829     }
1830     tcg_gen_andi_i32(tmp, tmp, mask);
1831     tcg_gen_mov_i32(dest, tmp);
1832     tcg_temp_free_i32(tmp);
1833     return 0;
1834 }
1835
1836 /* Disassemble an iwMMXt instruction.  Returns nonzero if an error occurred
1837    (ie. an undefined instruction).  */
1838 static int disas_iwmmxt_insn(DisasContext *s, uint32_t insn)
1839 {
1840     int rd, wrd;
1841     int rdhi, rdlo, rd0, rd1, i;
1842     TCGv_i32 addr;
1843     TCGv_i32 tmp, tmp2, tmp3;
1844
1845     if ((insn & 0x0e000e00) == 0x0c000000) {
1846         if ((insn & 0x0fe00ff0) == 0x0c400000) {
1847             wrd = insn & 0xf;
1848             rdlo = (insn >> 12) & 0xf;
1849             rdhi = (insn >> 16) & 0xf;
1850             if (insn & ARM_CP_RW_BIT) {                 /* TMRRC */
1851                 iwmmxt_load_reg(cpu_V0, wrd);
1852                 tcg_gen_extrl_i64_i32(cpu_R[rdlo], cpu_V0);
1853                 tcg_gen_shri_i64(cpu_V0, cpu_V0, 32);
1854                 tcg_gen_extrl_i64_i32(cpu_R[rdhi], cpu_V0);
1855             } else {                                    /* TMCRR */
1856                 tcg_gen_concat_i32_i64(cpu_V0, cpu_R[rdlo], cpu_R[rdhi]);
1857                 iwmmxt_store_reg(cpu_V0, wrd);
1858                 gen_op_iwmmxt_set_mup();
1859             }
1860             return 0;
1861         }
1862
1863         wrd = (insn >> 12) & 0xf;
1864         addr = tcg_temp_new_i32();
1865         if (gen_iwmmxt_address(s, insn, addr)) {
1866             tcg_temp_free_i32(addr);
1867             return 1;
1868         }
1869         if (insn & ARM_CP_RW_BIT) {
1870             if ((insn >> 28) == 0xf) {                  /* WLDRW wCx */
1871                 tmp = tcg_temp_new_i32();
1872                 gen_aa32_ld32u(s, tmp, addr, get_mem_index(s));
1873                 iwmmxt_store_creg(wrd, tmp);
1874             } else {
1875                 i = 1;
1876                 if (insn & (1 << 8)) {
1877                     if (insn & (1 << 22)) {             /* WLDRD */
1878                         gen_aa32_ld64(s, cpu_M0, addr, get_mem_index(s));
1879                         i = 0;
1880                     } else {                            /* WLDRW wRd */
1881                         tmp = tcg_temp_new_i32();
1882                         gen_aa32_ld32u(s, tmp, addr, get_mem_index(s));
1883                     }
1884                 } else {
1885                     tmp = tcg_temp_new_i32();
1886                     if (insn & (1 << 22)) {             /* WLDRH */
1887                         gen_aa32_ld16u(s, tmp, addr, get_mem_index(s));
1888                     } else {                            /* WLDRB */
1889                         gen_aa32_ld8u(s, tmp, addr, get_mem_index(s));
1890                     }
1891                 }
1892                 if (i) {
1893                     tcg_gen_extu_i32_i64(cpu_M0, tmp);
1894                     tcg_temp_free_i32(tmp);
1895                 }
1896                 gen_op_iwmmxt_movq_wRn_M0(wrd);
1897             }
1898         } else {
1899             if ((insn >> 28) == 0xf) {                  /* WSTRW wCx */
1900                 tmp = iwmmxt_load_creg(wrd);
1901                 gen_aa32_st32(s, tmp, addr, get_mem_index(s));
1902             } else {
1903                 gen_op_iwmmxt_movq_M0_wRn(wrd);
1904                 tmp = tcg_temp_new_i32();
1905                 if (insn & (1 << 8)) {
1906                     if (insn & (1 << 22)) {             /* WSTRD */
1907                         gen_aa32_st64(s, cpu_M0, addr, get_mem_index(s));
1908                     } else {                            /* WSTRW wRd */
1909                         tcg_gen_extrl_i64_i32(tmp, cpu_M0);
1910                         gen_aa32_st32(s, tmp, addr, get_mem_index(s));
1911                     }
1912                 } else {
1913                     if (insn & (1 << 22)) {             /* WSTRH */
1914                         tcg_gen_extrl_i64_i32(tmp, cpu_M0);
1915                         gen_aa32_st16(s, tmp, addr, get_mem_index(s));
1916                     } else {                            /* WSTRB */
1917                         tcg_gen_extrl_i64_i32(tmp, cpu_M0);
1918                         gen_aa32_st8(s, tmp, addr, get_mem_index(s));
1919                     }
1920                 }
1921             }
1922             tcg_temp_free_i32(tmp);
1923         }
1924         tcg_temp_free_i32(addr);
1925         return 0;
1926     }
1927
1928     if ((insn & 0x0f000000) != 0x0e000000)
1929         return 1;
1930
1931     switch (((insn >> 12) & 0xf00) | ((insn >> 4) & 0xff)) {
1932     case 0x000:                                         /* WOR */
1933         wrd = (insn >> 12) & 0xf;
1934         rd0 = (insn >> 0) & 0xf;
1935         rd1 = (insn >> 16) & 0xf;
1936         gen_op_iwmmxt_movq_M0_wRn(rd0);
1937         gen_op_iwmmxt_orq_M0_wRn(rd1);
1938         gen_op_iwmmxt_setpsr_nz();
1939         gen_op_iwmmxt_movq_wRn_M0(wrd);
1940         gen_op_iwmmxt_set_mup();
1941         gen_op_iwmmxt_set_cup();
1942         break;
1943     case 0x011:                                         /* TMCR */
1944         if (insn & 0xf)
1945             return 1;
1946         rd = (insn >> 12) & 0xf;
1947         wrd = (insn >> 16) & 0xf;
1948         switch (wrd) {
1949         case ARM_IWMMXT_wCID:
1950         case ARM_IWMMXT_wCASF:
1951             break;
1952         case ARM_IWMMXT_wCon:
1953             gen_op_iwmmxt_set_cup();
1954             /* Fall through.  */
1955         case ARM_IWMMXT_wCSSF:
1956             tmp = iwmmxt_load_creg(wrd);
1957             tmp2 = load_reg(s, rd);
1958             tcg_gen_andc_i32(tmp, tmp, tmp2);
1959             tcg_temp_free_i32(tmp2);
1960             iwmmxt_store_creg(wrd, tmp);
1961             break;
1962         case ARM_IWMMXT_wCGR0:
1963         case ARM_IWMMXT_wCGR1:
1964         case ARM_IWMMXT_wCGR2:
1965         case ARM_IWMMXT_wCGR3:
1966             gen_op_iwmmxt_set_cup();
1967             tmp = load_reg(s, rd);
1968             iwmmxt_store_creg(wrd, tmp);
1969             break;
1970         default:
1971             return 1;
1972         }
1973         break;
1974     case 0x100:                                         /* WXOR */
1975         wrd = (insn >> 12) & 0xf;
1976         rd0 = (insn >> 0) & 0xf;
1977         rd1 = (insn >> 16) & 0xf;
1978         gen_op_iwmmxt_movq_M0_wRn(rd0);
1979         gen_op_iwmmxt_xorq_M0_wRn(rd1);
1980         gen_op_iwmmxt_setpsr_nz();
1981         gen_op_iwmmxt_movq_wRn_M0(wrd);
1982         gen_op_iwmmxt_set_mup();
1983         gen_op_iwmmxt_set_cup();
1984         break;
1985     case 0x111:                                         /* TMRC */
1986         if (insn & 0xf)
1987             return 1;
1988         rd = (insn >> 12) & 0xf;
1989         wrd = (insn >> 16) & 0xf;
1990         tmp = iwmmxt_load_creg(wrd);
1991         store_reg(s, rd, tmp);
1992         break;
1993     case 0x300:                                         /* WANDN */
1994         wrd = (insn >> 12) & 0xf;
1995         rd0 = (insn >> 0) & 0xf;
1996         rd1 = (insn >> 16) & 0xf;
1997         gen_op_iwmmxt_movq_M0_wRn(rd0);
1998         tcg_gen_neg_i64(cpu_M0, cpu_M0);
1999         gen_op_iwmmxt_andq_M0_wRn(rd1);
2000         gen_op_iwmmxt_setpsr_nz();
2001         gen_op_iwmmxt_movq_wRn_M0(wrd);
2002         gen_op_iwmmxt_set_mup();
2003         gen_op_iwmmxt_set_cup();
2004         break;
2005     case 0x200:                                         /* WAND */
2006         wrd = (insn >> 12) & 0xf;
2007         rd0 = (insn >> 0) & 0xf;
2008         rd1 = (insn >> 16) & 0xf;
2009         gen_op_iwmmxt_movq_M0_wRn(rd0);
2010         gen_op_iwmmxt_andq_M0_wRn(rd1);
2011         gen_op_iwmmxt_setpsr_nz();
2012         gen_op_iwmmxt_movq_wRn_M0(wrd);
2013         gen_op_iwmmxt_set_mup();
2014         gen_op_iwmmxt_set_cup();
2015         break;
2016     case 0x810: case 0xa10:                             /* WMADD */
2017         wrd = (insn >> 12) & 0xf;
2018         rd0 = (insn >> 0) & 0xf;
2019         rd1 = (insn >> 16) & 0xf;
2020         gen_op_iwmmxt_movq_M0_wRn(rd0);
2021         if (insn & (1 << 21))
2022             gen_op_iwmmxt_maddsq_M0_wRn(rd1);
2023         else
2024             gen_op_iwmmxt_madduq_M0_wRn(rd1);
2025         gen_op_iwmmxt_movq_wRn_M0(wrd);
2026         gen_op_iwmmxt_set_mup();
2027         break;
2028     case 0x10e: case 0x50e: case 0x90e: case 0xd0e:     /* WUNPCKIL */
2029         wrd = (insn >> 12) & 0xf;
2030         rd0 = (insn >> 16) & 0xf;
2031         rd1 = (insn >> 0) & 0xf;
2032         gen_op_iwmmxt_movq_M0_wRn(rd0);
2033         switch ((insn >> 22) & 3) {
2034         case 0:
2035             gen_op_iwmmxt_unpacklb_M0_wRn(rd1);
2036             break;
2037         case 1:
2038             gen_op_iwmmxt_unpacklw_M0_wRn(rd1);
2039             break;
2040         case 2:
2041             gen_op_iwmmxt_unpackll_M0_wRn(rd1);
2042             break;
2043         case 3:
2044             return 1;
2045         }
2046         gen_op_iwmmxt_movq_wRn_M0(wrd);
2047         gen_op_iwmmxt_set_mup();
2048         gen_op_iwmmxt_set_cup();
2049         break;
2050     case 0x10c: case 0x50c: case 0x90c: case 0xd0c:     /* WUNPCKIH */
2051         wrd = (insn >> 12) & 0xf;
2052         rd0 = (insn >> 16) & 0xf;
2053         rd1 = (insn >> 0) & 0xf;
2054         gen_op_iwmmxt_movq_M0_wRn(rd0);
2055         switch ((insn >> 22) & 3) {
2056         case 0:
2057             gen_op_iwmmxt_unpackhb_M0_wRn(rd1);
2058             break;
2059         case 1:
2060             gen_op_iwmmxt_unpackhw_M0_wRn(rd1);
2061             break;
2062         case 2:
2063             gen_op_iwmmxt_unpackhl_M0_wRn(rd1);
2064             break;
2065         case 3:
2066             return 1;
2067         }
2068         gen_op_iwmmxt_movq_wRn_M0(wrd);
2069         gen_op_iwmmxt_set_mup();
2070         gen_op_iwmmxt_set_cup();
2071         break;
2072     case 0x012: case 0x112: case 0x412: case 0x512:     /* WSAD */
2073         wrd = (insn >> 12) & 0xf;
2074         rd0 = (insn >> 16) & 0xf;
2075         rd1 = (insn >> 0) & 0xf;
2076         gen_op_iwmmxt_movq_M0_wRn(rd0);
2077         if (insn & (1 << 22))
2078             gen_op_iwmmxt_sadw_M0_wRn(rd1);
2079         else
2080             gen_op_iwmmxt_sadb_M0_wRn(rd1);
2081         if (!(insn & (1 << 20)))
2082             gen_op_iwmmxt_addl_M0_wRn(wrd);
2083         gen_op_iwmmxt_movq_wRn_M0(wrd);
2084         gen_op_iwmmxt_set_mup();
2085         break;
2086     case 0x010: case 0x110: case 0x210: case 0x310:     /* WMUL */
2087         wrd = (insn >> 12) & 0xf;
2088         rd0 = (insn >> 16) & 0xf;
2089         rd1 = (insn >> 0) & 0xf;
2090         gen_op_iwmmxt_movq_M0_wRn(rd0);
2091         if (insn & (1 << 21)) {
2092             if (insn & (1 << 20))
2093                 gen_op_iwmmxt_mulshw_M0_wRn(rd1);
2094             else
2095                 gen_op_iwmmxt_mulslw_M0_wRn(rd1);
2096         } else {
2097             if (insn & (1 << 20))
2098                 gen_op_iwmmxt_muluhw_M0_wRn(rd1);
2099             else
2100                 gen_op_iwmmxt_mululw_M0_wRn(rd1);
2101         }
2102         gen_op_iwmmxt_movq_wRn_M0(wrd);
2103         gen_op_iwmmxt_set_mup();
2104         break;
2105     case 0x410: case 0x510: case 0x610: case 0x710:     /* WMAC */
2106         wrd = (insn >> 12) & 0xf;
2107         rd0 = (insn >> 16) & 0xf;
2108         rd1 = (insn >> 0) & 0xf;
2109         gen_op_iwmmxt_movq_M0_wRn(rd0);
2110         if (insn & (1 << 21))
2111             gen_op_iwmmxt_macsw_M0_wRn(rd1);
2112         else
2113             gen_op_iwmmxt_macuw_M0_wRn(rd1);
2114         if (!(insn & (1 << 20))) {
2115             iwmmxt_load_reg(cpu_V1, wrd);
2116             tcg_gen_add_i64(cpu_M0, cpu_M0, cpu_V1);
2117         }
2118         gen_op_iwmmxt_movq_wRn_M0(wrd);
2119         gen_op_iwmmxt_set_mup();
2120         break;
2121     case 0x006: case 0x406: case 0x806: case 0xc06:     /* WCMPEQ */
2122         wrd = (insn >> 12) & 0xf;
2123         rd0 = (insn >> 16) & 0xf;
2124         rd1 = (insn >> 0) & 0xf;
2125         gen_op_iwmmxt_movq_M0_wRn(rd0);
2126         switch ((insn >> 22) & 3) {
2127         case 0:
2128             gen_op_iwmmxt_cmpeqb_M0_wRn(rd1);
2129             break;
2130         case 1:
2131             gen_op_iwmmxt_cmpeqw_M0_wRn(rd1);
2132             break;
2133         case 2:
2134             gen_op_iwmmxt_cmpeql_M0_wRn(rd1);
2135             break;
2136         case 3:
2137             return 1;
2138         }
2139         gen_op_iwmmxt_movq_wRn_M0(wrd);
2140         gen_op_iwmmxt_set_mup();
2141         gen_op_iwmmxt_set_cup();
2142         break;
2143     case 0x800: case 0x900: case 0xc00: case 0xd00:     /* WAVG2 */
2144         wrd = (insn >> 12) & 0xf;
2145         rd0 = (insn >> 16) & 0xf;
2146         rd1 = (insn >> 0) & 0xf;
2147         gen_op_iwmmxt_movq_M0_wRn(rd0);
2148         if (insn & (1 << 22)) {
2149             if (insn & (1 << 20))
2150                 gen_op_iwmmxt_avgw1_M0_wRn(rd1);
2151             else
2152                 gen_op_iwmmxt_avgw0_M0_wRn(rd1);
2153         } else {
2154             if (insn & (1 << 20))
2155                 gen_op_iwmmxt_avgb1_M0_wRn(rd1);
2156             else
2157                 gen_op_iwmmxt_avgb0_M0_wRn(rd1);
2158         }
2159         gen_op_iwmmxt_movq_wRn_M0(wrd);
2160         gen_op_iwmmxt_set_mup();
2161         gen_op_iwmmxt_set_cup();
2162         break;
2163     case 0x802: case 0x902: case 0xa02: case 0xb02:     /* WALIGNR */
2164         wrd = (insn >> 12) & 0xf;
2165         rd0 = (insn >> 16) & 0xf;
2166         rd1 = (insn >> 0) & 0xf;
2167         gen_op_iwmmxt_movq_M0_wRn(rd0);
2168         tmp = iwmmxt_load_creg(ARM_IWMMXT_wCGR0 + ((insn >> 20) & 3));
2169         tcg_gen_andi_i32(tmp, tmp, 7);
2170         iwmmxt_load_reg(cpu_V1, rd1);
2171         gen_helper_iwmmxt_align(cpu_M0, cpu_M0, cpu_V1, tmp);
2172         tcg_temp_free_i32(tmp);
2173         gen_op_iwmmxt_movq_wRn_M0(wrd);
2174         gen_op_iwmmxt_set_mup();
2175         break;
2176     case 0x601: case 0x605: case 0x609: case 0x60d:     /* TINSR */
2177         if (((insn >> 6) & 3) == 3)
2178             return 1;
2179         rd = (insn >> 12) & 0xf;
2180         wrd = (insn >> 16) & 0xf;
2181         tmp = load_reg(s, rd);
2182         gen_op_iwmmxt_movq_M0_wRn(wrd);
2183         switch ((insn >> 6) & 3) {
2184         case 0:
2185             tmp2 = tcg_const_i32(0xff);
2186             tmp3 = tcg_const_i32((insn & 7) << 3);
2187             break;
2188         case 1:
2189             tmp2 = tcg_const_i32(0xffff);
2190             tmp3 = tcg_const_i32((insn & 3) << 4);
2191             break;
2192         case 2:
2193             tmp2 = tcg_const_i32(0xffffffff);
2194             tmp3 = tcg_const_i32((insn & 1) << 5);
2195             break;
2196         default:
2197             tmp2 = NULL;
2198             tmp3 = NULL;
2199         }
2200         gen_helper_iwmmxt_insr(cpu_M0, cpu_M0, tmp, tmp2, tmp3);
2201         tcg_temp_free_i32(tmp3);
2202         tcg_temp_free_i32(tmp2);
2203         tcg_temp_free_i32(tmp);
2204         gen_op_iwmmxt_movq_wRn_M0(wrd);
2205         gen_op_iwmmxt_set_mup();
2206         break;
2207     case 0x107: case 0x507: case 0x907: case 0xd07:     /* TEXTRM */
2208         rd = (insn >> 12) & 0xf;
2209         wrd = (insn >> 16) & 0xf;
2210         if (rd == 15 || ((insn >> 22) & 3) == 3)
2211             return 1;
2212         gen_op_iwmmxt_movq_M0_wRn(wrd);
2213         tmp = tcg_temp_new_i32();
2214         switch ((insn >> 22) & 3) {
2215         case 0:
2216             tcg_gen_shri_i64(cpu_M0, cpu_M0, (insn & 7) << 3);
2217             tcg_gen_extrl_i64_i32(tmp, cpu_M0);
2218             if (insn & 8) {
2219                 tcg_gen_ext8s_i32(tmp, tmp);
2220             } else {
2221                 tcg_gen_andi_i32(tmp, tmp, 0xff);
2222             }
2223             break;
2224         case 1:
2225             tcg_gen_shri_i64(cpu_M0, cpu_M0, (insn & 3) << 4);
2226             tcg_gen_extrl_i64_i32(tmp, cpu_M0);
2227             if (insn & 8) {
2228                 tcg_gen_ext16s_i32(tmp, tmp);
2229             } else {
2230                 tcg_gen_andi_i32(tmp, tmp, 0xffff);
2231             }
2232             break;
2233         case 2:
2234             tcg_gen_shri_i64(cpu_M0, cpu_M0, (insn & 1) << 5);
2235             tcg_gen_extrl_i64_i32(tmp, cpu_M0);
2236             break;
2237         }
2238         store_reg(s, rd, tmp);
2239         break;
2240     case 0x117: case 0x517: case 0x917: case 0xd17:     /* TEXTRC */
2241         if ((insn & 0x000ff008) != 0x0003f000 || ((insn >> 22) & 3) == 3)
2242             return 1;
2243         tmp = iwmmxt_load_creg(ARM_IWMMXT_wCASF);
2244         switch ((insn >> 22) & 3) {
2245         case 0:
2246             tcg_gen_shri_i32(tmp, tmp, ((insn & 7) << 2) + 0);
2247             break;
2248         case 1:
2249             tcg_gen_shri_i32(tmp, tmp, ((insn & 3) << 3) + 4);
2250             break;
2251         case 2:
2252             tcg_gen_shri_i32(tmp, tmp, ((insn & 1) << 4) + 12);
2253             break;
2254         }
2255         tcg_gen_shli_i32(tmp, tmp, 28);
2256         gen_set_nzcv(tmp);
2257         tcg_temp_free_i32(tmp);
2258         break;
2259     case 0x401: case 0x405: case 0x409: case 0x40d:     /* TBCST */
2260         if (((insn >> 6) & 3) == 3)
2261             return 1;
2262         rd = (insn >> 12) & 0xf;
2263         wrd = (insn >> 16) & 0xf;
2264         tmp = load_reg(s, rd);
2265         switch ((insn >> 6) & 3) {
2266         case 0:
2267             gen_helper_iwmmxt_bcstb(cpu_M0, tmp);
2268             break;
2269         case 1:
2270             gen_helper_iwmmxt_bcstw(cpu_M0, tmp);
2271             break;
2272         case 2:
2273             gen_helper_iwmmxt_bcstl(cpu_M0, tmp);
2274             break;
2275         }
2276         tcg_temp_free_i32(tmp);
2277         gen_op_iwmmxt_movq_wRn_M0(wrd);
2278         gen_op_iwmmxt_set_mup();
2279         break;
2280     case 0x113: case 0x513: case 0x913: case 0xd13:     /* TANDC */
2281         if ((insn & 0x000ff00f) != 0x0003f000 || ((insn >> 22) & 3) == 3)
2282             return 1;
2283         tmp = iwmmxt_load_creg(ARM_IWMMXT_wCASF);
2284         tmp2 = tcg_temp_new_i32();
2285         tcg_gen_mov_i32(tmp2, tmp);
2286         switch ((insn >> 22) & 3) {
2287         case 0:
2288             for (i = 0; i < 7; i ++) {
2289                 tcg_gen_shli_i32(tmp2, tmp2, 4);
2290                 tcg_gen_and_i32(tmp, tmp, tmp2);
2291             }
2292             break;
2293         case 1:
2294             for (i = 0; i < 3; i ++) {
2295                 tcg_gen_shli_i32(tmp2, tmp2, 8);
2296                 tcg_gen_and_i32(tmp, tmp, tmp2);
2297             }
2298             break;
2299         case 2:
2300             tcg_gen_shli_i32(tmp2, tmp2, 16);
2301             tcg_gen_and_i32(tmp, tmp, tmp2);
2302             break;
2303         }
2304         gen_set_nzcv(tmp);
2305         tcg_temp_free_i32(tmp2);
2306         tcg_temp_free_i32(tmp);
2307         break;
2308     case 0x01c: case 0x41c: case 0x81c: case 0xc1c:     /* WACC */
2309         wrd = (insn >> 12) & 0xf;
2310         rd0 = (insn >> 16) & 0xf;
2311         gen_op_iwmmxt_movq_M0_wRn(rd0);
2312         switch ((insn >> 22) & 3) {
2313         case 0:
2314             gen_helper_iwmmxt_addcb(cpu_M0, cpu_M0);
2315             break;
2316         case 1:
2317             gen_helper_iwmmxt_addcw(cpu_M0, cpu_M0);
2318             break;
2319         case 2:
2320             gen_helper_iwmmxt_addcl(cpu_M0, cpu_M0);
2321             break;
2322         case 3:
2323             return 1;
2324         }
2325         gen_op_iwmmxt_movq_wRn_M0(wrd);
2326         gen_op_iwmmxt_set_mup();
2327         break;
2328     case 0x115: case 0x515: case 0x915: case 0xd15:     /* TORC */
2329         if ((insn & 0x000ff00f) != 0x0003f000 || ((insn >> 22) & 3) == 3)
2330             return 1;
2331         tmp = iwmmxt_load_creg(ARM_IWMMXT_wCASF);
2332         tmp2 = tcg_temp_new_i32();
2333         tcg_gen_mov_i32(tmp2, tmp);
2334         switch ((insn >> 22) & 3) {
2335         case 0:
2336             for (i = 0; i < 7; i ++) {
2337                 tcg_gen_shli_i32(tmp2, tmp2, 4);
2338                 tcg_gen_or_i32(tmp, tmp, tmp2);
2339             }
2340             break;
2341         case 1:
2342             for (i = 0; i < 3; i ++) {
2343                 tcg_gen_shli_i32(tmp2, tmp2, 8);
2344                 tcg_gen_or_i32(tmp, tmp, tmp2);
2345             }
2346             break;
2347         case 2:
2348             tcg_gen_shli_i32(tmp2, tmp2, 16);
2349             tcg_gen_or_i32(tmp, tmp, tmp2);
2350             break;
2351         }
2352         gen_set_nzcv(tmp);
2353         tcg_temp_free_i32(tmp2);
2354         tcg_temp_free_i32(tmp);
2355         break;
2356     case 0x103: case 0x503: case 0x903: case 0xd03:     /* TMOVMSK */
2357         rd = (insn >> 12) & 0xf;
2358         rd0 = (insn >> 16) & 0xf;
2359         if ((insn & 0xf) != 0 || ((insn >> 22) & 3) == 3)
2360             return 1;
2361         gen_op_iwmmxt_movq_M0_wRn(rd0);
2362         tmp = tcg_temp_new_i32();
2363         switch ((insn >> 22) & 3) {
2364         case 0:
2365             gen_helper_iwmmxt_msbb(tmp, cpu_M0);
2366             break;
2367         case 1:
2368             gen_helper_iwmmxt_msbw(tmp, cpu_M0);
2369             break;
2370         case 2:
2371             gen_helper_iwmmxt_msbl(tmp, cpu_M0);
2372             break;
2373         }
2374         store_reg(s, rd, tmp);
2375         break;
2376     case 0x106: case 0x306: case 0x506: case 0x706:     /* WCMPGT */
2377     case 0x906: case 0xb06: case 0xd06: case 0xf06:
2378         wrd = (insn >> 12) & 0xf;
2379         rd0 = (insn >> 16) & 0xf;
2380         rd1 = (insn >> 0) & 0xf;
2381         gen_op_iwmmxt_movq_M0_wRn(rd0);
2382         switch ((insn >> 22) & 3) {
2383         case 0:
2384             if (insn & (1 << 21))
2385                 gen_op_iwmmxt_cmpgtsb_M0_wRn(rd1);
2386             else
2387                 gen_op_iwmmxt_cmpgtub_M0_wRn(rd1);
2388             break;
2389         case 1:
2390             if (insn & (1 << 21))
2391                 gen_op_iwmmxt_cmpgtsw_M0_wRn(rd1);
2392             else
2393                 gen_op_iwmmxt_cmpgtuw_M0_wRn(rd1);
2394             break;
2395         case 2:
2396             if (insn & (1 << 21))
2397                 gen_op_iwmmxt_cmpgtsl_M0_wRn(rd1);
2398             else
2399                 gen_op_iwmmxt_cmpgtul_M0_wRn(rd1);
2400             break;
2401         case 3:
2402             return 1;
2403         }
2404         gen_op_iwmmxt_movq_wRn_M0(wrd);
2405         gen_op_iwmmxt_set_mup();
2406         gen_op_iwmmxt_set_cup();
2407         break;
2408     case 0x00e: case 0x20e: case 0x40e: case 0x60e:     /* WUNPCKEL */
2409     case 0x80e: case 0xa0e: case 0xc0e: case 0xe0e:
2410         wrd = (insn >> 12) & 0xf;
2411         rd0 = (insn >> 16) & 0xf;
2412         gen_op_iwmmxt_movq_M0_wRn(rd0);
2413         switch ((insn >> 22) & 3) {
2414         case 0:
2415             if (insn & (1 << 21))
2416                 gen_op_iwmmxt_unpacklsb_M0();
2417             else
2418                 gen_op_iwmmxt_unpacklub_M0();
2419             break;
2420         case 1:
2421             if (insn & (1 << 21))
2422                 gen_op_iwmmxt_unpacklsw_M0();
2423             else
2424                 gen_op_iwmmxt_unpackluw_M0();
2425             break;
2426         case 2:
2427             if (insn & (1 << 21))
2428                 gen_op_iwmmxt_unpacklsl_M0();
2429             else
2430                 gen_op_iwmmxt_unpacklul_M0();
2431             break;
2432         case 3:
2433             return 1;
2434         }
2435         gen_op_iwmmxt_movq_wRn_M0(wrd);
2436         gen_op_iwmmxt_set_mup();
2437         gen_op_iwmmxt_set_cup();
2438         break;
2439     case 0x00c: case 0x20c: case 0x40c: case 0x60c:     /* WUNPCKEH */
2440     case 0x80c: case 0xa0c: case 0xc0c: case 0xe0c:
2441         wrd = (insn >> 12) & 0xf;
2442         rd0 = (insn >> 16) & 0xf;
2443         gen_op_iwmmxt_movq_M0_wRn(rd0);
2444         switch ((insn >> 22) & 3) {
2445         case 0:
2446             if (insn & (1 << 21))
2447                 gen_op_iwmmxt_unpackhsb_M0();
2448             else
2449                 gen_op_iwmmxt_unpackhub_M0();
2450             break;
2451         case 1:
2452             if (insn & (1 << 21))
2453                 gen_op_iwmmxt_unpackhsw_M0();
2454             else
2455                 gen_op_iwmmxt_unpackhuw_M0();
2456             break;
2457         case 2:
2458             if (insn & (1 << 21))
2459                 gen_op_iwmmxt_unpackhsl_M0();
2460             else
2461                 gen_op_iwmmxt_unpackhul_M0();
2462             break;
2463         case 3:
2464             return 1;
2465         }
2466         gen_op_iwmmxt_movq_wRn_M0(wrd);
2467         gen_op_iwmmxt_set_mup();
2468         gen_op_iwmmxt_set_cup();
2469         break;
2470     case 0x204: case 0x604: case 0xa04: case 0xe04:     /* WSRL */
2471     case 0x214: case 0x614: case 0xa14: case 0xe14:
2472         if (((insn >> 22) & 3) == 0)
2473             return 1;
2474         wrd = (insn >> 12) & 0xf;
2475         rd0 = (insn >> 16) & 0xf;
2476         gen_op_iwmmxt_movq_M0_wRn(rd0);
2477         tmp = tcg_temp_new_i32();
2478         if (gen_iwmmxt_shift(insn, 0xff, tmp)) {
2479             tcg_temp_free_i32(tmp);
2480             return 1;
2481         }
2482         switch ((insn >> 22) & 3) {
2483         case 1:
2484             gen_helper_iwmmxt_srlw(cpu_M0, cpu_env, cpu_M0, tmp);
2485             break;
2486         case 2:
2487             gen_helper_iwmmxt_srll(cpu_M0, cpu_env, cpu_M0, tmp);
2488             break;
2489         case 3:
2490             gen_helper_iwmmxt_srlq(cpu_M0, cpu_env, cpu_M0, tmp);
2491             break;
2492         }
2493         tcg_temp_free_i32(tmp);
2494         gen_op_iwmmxt_movq_wRn_M0(wrd);
2495         gen_op_iwmmxt_set_mup();
2496         gen_op_iwmmxt_set_cup();
2497         break;
2498     case 0x004: case 0x404: case 0x804: case 0xc04:     /* WSRA */
2499     case 0x014: case 0x414: case 0x814: case 0xc14:
2500         if (((insn >> 22) & 3) == 0)
2501             return 1;
2502         wrd = (insn >> 12) & 0xf;
2503         rd0 = (insn >> 16) & 0xf;
2504         gen_op_iwmmxt_movq_M0_wRn(rd0);
2505         tmp = tcg_temp_new_i32();
2506         if (gen_iwmmxt_shift(insn, 0xff, tmp)) {
2507             tcg_temp_free_i32(tmp);
2508             return 1;
2509         }
2510         switch ((insn >> 22) & 3) {
2511         case 1:
2512             gen_helper_iwmmxt_sraw(cpu_M0, cpu_env, cpu_M0, tmp);
2513             break;
2514         case 2:
2515             gen_helper_iwmmxt_sral(cpu_M0, cpu_env, cpu_M0, tmp);
2516             break;
2517         case 3:
2518             gen_helper_iwmmxt_sraq(cpu_M0, cpu_env, cpu_M0, tmp);
2519             break;
2520         }
2521         tcg_temp_free_i32(tmp);
2522         gen_op_iwmmxt_movq_wRn_M0(wrd);
2523         gen_op_iwmmxt_set_mup();
2524         gen_op_iwmmxt_set_cup();
2525         break;
2526     case 0x104: case 0x504: case 0x904: case 0xd04:     /* WSLL */
2527     case 0x114: case 0x514: case 0x914: case 0xd14:
2528         if (((insn >> 22) & 3) == 0)
2529             return 1;
2530         wrd = (insn >> 12) & 0xf;
2531         rd0 = (insn >> 16) & 0xf;
2532         gen_op_iwmmxt_movq_M0_wRn(rd0);
2533         tmp = tcg_temp_new_i32();
2534         if (gen_iwmmxt_shift(insn, 0xff, tmp)) {
2535             tcg_temp_free_i32(tmp);
2536             return 1;
2537         }
2538         switch ((insn >> 22) & 3) {
2539         case 1:
2540             gen_helper_iwmmxt_sllw(cpu_M0, cpu_env, cpu_M0, tmp);
2541             break;
2542         case 2:
2543             gen_helper_iwmmxt_slll(cpu_M0, cpu_env, cpu_M0, tmp);
2544             break;
2545         case 3:
2546             gen_helper_iwmmxt_sllq(cpu_M0, cpu_env, cpu_M0, tmp);
2547             break;
2548         }
2549         tcg_temp_free_i32(tmp);
2550         gen_op_iwmmxt_movq_wRn_M0(wrd);
2551         gen_op_iwmmxt_set_mup();
2552         gen_op_iwmmxt_set_cup();
2553         break;
2554     case 0x304: case 0x704: case 0xb04: case 0xf04:     /* WROR */
2555     case 0x314: case 0x714: case 0xb14: case 0xf14:
2556         if (((insn >> 22) & 3) == 0)
2557             return 1;
2558         wrd = (insn >> 12) & 0xf;
2559         rd0 = (insn >> 16) & 0xf;
2560         gen_op_iwmmxt_movq_M0_wRn(rd0);
2561         tmp = tcg_temp_new_i32();
2562         switch ((insn >> 22) & 3) {
2563         case 1:
2564             if (gen_iwmmxt_shift(insn, 0xf, tmp)) {
2565                 tcg_temp_free_i32(tmp);
2566                 return 1;
2567             }
2568             gen_helper_iwmmxt_rorw(cpu_M0, cpu_env, cpu_M0, tmp);
2569             break;
2570         case 2:
2571             if (gen_iwmmxt_shift(insn, 0x1f, tmp)) {
2572                 tcg_temp_free_i32(tmp);
2573                 return 1;
2574             }
2575             gen_helper_iwmmxt_rorl(cpu_M0, cpu_env, cpu_M0, tmp);
2576             break;
2577         case 3:
2578             if (gen_iwmmxt_shift(insn, 0x3f, tmp)) {
2579                 tcg_temp_free_i32(tmp);
2580                 return 1;
2581             }
2582             gen_helper_iwmmxt_rorq(cpu_M0, cpu_env, cpu_M0, tmp);
2583             break;
2584         }
2585         tcg_temp_free_i32(tmp);
2586         gen_op_iwmmxt_movq_wRn_M0(wrd);
2587         gen_op_iwmmxt_set_mup();
2588         gen_op_iwmmxt_set_cup();
2589         break;
2590     case 0x116: case 0x316: case 0x516: case 0x716:     /* WMIN */
2591     case 0x916: case 0xb16: case 0xd16: case 0xf16:
2592         wrd = (insn >> 12) & 0xf;
2593         rd0 = (insn >> 16) & 0xf;
2594         rd1 = (insn >> 0) & 0xf;
2595         gen_op_iwmmxt_movq_M0_wRn(rd0);
2596         switch ((insn >> 22) & 3) {
2597         case 0:
2598             if (insn & (1 << 21))
2599                 gen_op_iwmmxt_minsb_M0_wRn(rd1);
2600             else
2601                 gen_op_iwmmxt_minub_M0_wRn(rd1);
2602             break;
2603         case 1:
2604             if (insn & (1 << 21))
2605                 gen_op_iwmmxt_minsw_M0_wRn(rd1);
2606             else
2607                 gen_op_iwmmxt_minuw_M0_wRn(rd1);
2608             break;
2609         case 2:
2610             if (insn & (1 << 21))
2611                 gen_op_iwmmxt_minsl_M0_wRn(rd1);
2612             else
2613                 gen_op_iwmmxt_minul_M0_wRn(rd1);
2614             break;
2615         case 3:
2616             return 1;
2617         }
2618         gen_op_iwmmxt_movq_wRn_M0(wrd);
2619         gen_op_iwmmxt_set_mup();
2620         break;
2621     case 0x016: case 0x216: case 0x416: case 0x616:     /* WMAX */
2622     case 0x816: case 0xa16: case 0xc16: case 0xe16:
2623         wrd = (insn >> 12) & 0xf;
2624         rd0 = (insn >> 16) & 0xf;
2625         rd1 = (insn >> 0) & 0xf;
2626         gen_op_iwmmxt_movq_M0_wRn(rd0);
2627         switch ((insn >> 22) & 3) {
2628         case 0:
2629             if (insn & (1 << 21))
2630                 gen_op_iwmmxt_maxsb_M0_wRn(rd1);
2631             else
2632                 gen_op_iwmmxt_maxub_M0_wRn(rd1);
2633             break;
2634         case 1:
2635             if (insn & (1 << 21))
2636                 gen_op_iwmmxt_maxsw_M0_wRn(rd1);
2637             else
2638                 gen_op_iwmmxt_maxuw_M0_wRn(rd1);
2639             break;
2640         case 2:
2641             if (insn & (1 << 21))
2642                 gen_op_iwmmxt_maxsl_M0_wRn(rd1);
2643             else
2644                 gen_op_iwmmxt_maxul_M0_wRn(rd1);
2645             break;
2646         case 3:
2647             return 1;
2648         }
2649         gen_op_iwmmxt_movq_wRn_M0(wrd);
2650         gen_op_iwmmxt_set_mup();
2651         break;
2652     case 0x002: case 0x102: case 0x202: case 0x302:     /* WALIGNI */
2653     case 0x402: case 0x502: case 0x602: case 0x702:
2654         wrd = (insn >> 12) & 0xf;
2655         rd0 = (insn >> 16) & 0xf;
2656         rd1 = (insn >> 0) & 0xf;
2657         gen_op_iwmmxt_movq_M0_wRn(rd0);
2658         tmp = tcg_const_i32((insn >> 20) & 3);
2659         iwmmxt_load_reg(cpu_V1, rd1);
2660         gen_helper_iwmmxt_align(cpu_M0, cpu_M0, cpu_V1, tmp);
2661         tcg_temp_free_i32(tmp);
2662         gen_op_iwmmxt_movq_wRn_M0(wrd);
2663         gen_op_iwmmxt_set_mup();
2664         break;
2665     case 0x01a: case 0x11a: case 0x21a: case 0x31a:     /* WSUB */
2666     case 0x41a: case 0x51a: case 0x61a: case 0x71a:
2667     case 0x81a: case 0x91a: case 0xa1a: case 0xb1a:
2668     case 0xc1a: case 0xd1a: case 0xe1a: case 0xf1a:
2669         wrd = (insn >> 12) & 0xf;
2670         rd0 = (insn >> 16) & 0xf;
2671         rd1 = (insn >> 0) & 0xf;
2672         gen_op_iwmmxt_movq_M0_wRn(rd0);
2673         switch ((insn >> 20) & 0xf) {
2674         case 0x0:
2675             gen_op_iwmmxt_subnb_M0_wRn(rd1);
2676             break;
2677         case 0x1:
2678             gen_op_iwmmxt_subub_M0_wRn(rd1);
2679             break;
2680         case 0x3:
2681             gen_op_iwmmxt_subsb_M0_wRn(rd1);
2682             break;
2683         case 0x4:
2684             gen_op_iwmmxt_subnw_M0_wRn(rd1);
2685             break;
2686         case 0x5:
2687             gen_op_iwmmxt_subuw_M0_wRn(rd1);
2688             break;
2689         case 0x7:
2690             gen_op_iwmmxt_subsw_M0_wRn(rd1);
2691             break;
2692         case 0x8:
2693             gen_op_iwmmxt_subnl_M0_wRn(rd1);
2694             break;
2695         case 0x9:
2696             gen_op_iwmmxt_subul_M0_wRn(rd1);
2697             break;
2698         case 0xb:
2699             gen_op_iwmmxt_subsl_M0_wRn(rd1);
2700             break;
2701         default:
2702             return 1;
2703         }
2704         gen_op_iwmmxt_movq_wRn_M0(wrd);
2705         gen_op_iwmmxt_set_mup();
2706         gen_op_iwmmxt_set_cup();
2707         break;
2708     case 0x01e: case 0x11e: case 0x21e: case 0x31e:     /* WSHUFH */
2709     case 0x41e: case 0x51e: case 0x61e: case 0x71e:
2710     case 0x81e: case 0x91e: case 0xa1e: case 0xb1e:
2711     case 0xc1e: case 0xd1e: case 0xe1e: case 0xf1e:
2712         wrd = (insn >> 12) & 0xf;
2713         rd0 = (insn >> 16) & 0xf;
2714         gen_op_iwmmxt_movq_M0_wRn(rd0);
2715         tmp = tcg_const_i32(((insn >> 16) & 0xf0) | (insn & 0x0f));
2716         gen_helper_iwmmxt_shufh(cpu_M0, cpu_env, cpu_M0, tmp);
2717         tcg_temp_free_i32(tmp);
2718         gen_op_iwmmxt_movq_wRn_M0(wrd);
2719         gen_op_iwmmxt_set_mup();
2720         gen_op_iwmmxt_set_cup();
2721         break;
2722     case 0x018: case 0x118: case 0x218: case 0x318:     /* WADD */
2723     case 0x418: case 0x518: case 0x618: case 0x718:
2724     case 0x818: case 0x918: case 0xa18: case 0xb18:
2725     case 0xc18: case 0xd18: case 0xe18: case 0xf18:
2726         wrd = (insn >> 12) & 0xf;
2727         rd0 = (insn >> 16) & 0xf;
2728         rd1 = (insn >> 0) & 0xf;
2729         gen_op_iwmmxt_movq_M0_wRn(rd0);
2730         switch ((insn >> 20) & 0xf) {
2731         case 0x0:
2732             gen_op_iwmmxt_addnb_M0_wRn(rd1);
2733             break;
2734         case 0x1:
2735             gen_op_iwmmxt_addub_M0_wRn(rd1);
2736             break;
2737         case 0x3:
2738             gen_op_iwmmxt_addsb_M0_wRn(rd1);
2739             break;
2740         case 0x4:
2741             gen_op_iwmmxt_addnw_M0_wRn(rd1);
2742             break;
2743         case 0x5:
2744             gen_op_iwmmxt_adduw_M0_wRn(rd1);
2745             break;
2746         case 0x7:
2747             gen_op_iwmmxt_addsw_M0_wRn(rd1);
2748             break;
2749         case 0x8:
2750             gen_op_iwmmxt_addnl_M0_wRn(rd1);
2751             break;
2752         case 0x9:
2753             gen_op_iwmmxt_addul_M0_wRn(rd1);
2754             break;
2755         case 0xb:
2756             gen_op_iwmmxt_addsl_M0_wRn(rd1);
2757             break;
2758         default:
2759             return 1;
2760         }
2761         gen_op_iwmmxt_movq_wRn_M0(wrd);
2762         gen_op_iwmmxt_set_mup();
2763         gen_op_iwmmxt_set_cup();
2764         break;
2765     case 0x008: case 0x108: case 0x208: case 0x308:     /* WPACK */
2766     case 0x408: case 0x508: case 0x608: case 0x708:
2767     case 0x808: case 0x908: case 0xa08: case 0xb08:
2768     case 0xc08: case 0xd08: case 0xe08: case 0xf08:
2769         if (!(insn & (1 << 20)) || ((insn >> 22) & 3) == 0)
2770             return 1;
2771         wrd = (insn >> 12) & 0xf;
2772         rd0 = (insn >> 16) & 0xf;
2773         rd1 = (insn >> 0) & 0xf;
2774         gen_op_iwmmxt_movq_M0_wRn(rd0);
2775         switch ((insn >> 22) & 3) {
2776         case 1:
2777             if (insn & (1 << 21))
2778                 gen_op_iwmmxt_packsw_M0_wRn(rd1);
2779             else
2780                 gen_op_iwmmxt_packuw_M0_wRn(rd1);
2781             break;
2782         case 2:
2783             if (insn & (1 << 21))
2784                 gen_op_iwmmxt_packsl_M0_wRn(rd1);
2785             else
2786                 gen_op_iwmmxt_packul_M0_wRn(rd1);
2787             break;
2788         case 3:
2789             if (insn & (1 << 21))
2790                 gen_op_iwmmxt_packsq_M0_wRn(rd1);
2791             else
2792                 gen_op_iwmmxt_packuq_M0_wRn(rd1);
2793             break;
2794         }
2795         gen_op_iwmmxt_movq_wRn_M0(wrd);
2796         gen_op_iwmmxt_set_mup();
2797         gen_op_iwmmxt_set_cup();
2798         break;
2799     case 0x201: case 0x203: case 0x205: case 0x207:
2800     case 0x209: case 0x20b: case 0x20d: case 0x20f:
2801     case 0x211: case 0x213: case 0x215: case 0x217:
2802     case 0x219: case 0x21b: case 0x21d: case 0x21f:
2803         wrd = (insn >> 5) & 0xf;
2804         rd0 = (insn >> 12) & 0xf;
2805         rd1 = (insn >> 0) & 0xf;
2806         if (rd0 == 0xf || rd1 == 0xf)
2807             return 1;
2808         gen_op_iwmmxt_movq_M0_wRn(wrd);
2809         tmp = load_reg(s, rd0);
2810         tmp2 = load_reg(s, rd1);
2811         switch ((insn >> 16) & 0xf) {
2812         case 0x0:                                       /* TMIA */
2813             gen_helper_iwmmxt_muladdsl(cpu_M0, cpu_M0, tmp, tmp2);
2814             break;
2815         case 0x8:                                       /* TMIAPH */
2816             gen_helper_iwmmxt_muladdsw(cpu_M0, cpu_M0, tmp, tmp2);
2817             break;
2818         case 0xc: case 0xd: case 0xe: case 0xf:         /* TMIAxy */
2819             if (insn & (1 << 16))
2820                 tcg_gen_shri_i32(tmp, tmp, 16);
2821             if (insn & (1 << 17))
2822                 tcg_gen_shri_i32(tmp2, tmp2, 16);
2823             gen_helper_iwmmxt_muladdswl(cpu_M0, cpu_M0, tmp, tmp2);
2824             break;
2825         default:
2826             tcg_temp_free_i32(tmp2);
2827             tcg_temp_free_i32(tmp);
2828             return 1;
2829         }
2830         tcg_temp_free_i32(tmp2);
2831         tcg_temp_free_i32(tmp);
2832         gen_op_iwmmxt_movq_wRn_M0(wrd);
2833         gen_op_iwmmxt_set_mup();
2834         break;
2835     default:
2836         return 1;
2837     }
2838
2839     return 0;
2840 }
2841
2842 /* Disassemble an XScale DSP instruction.  Returns nonzero if an error occurred
2843    (ie. an undefined instruction).  */
2844 static int disas_dsp_insn(DisasContext *s, uint32_t insn)
2845 {
2846     int acc, rd0, rd1, rdhi, rdlo;
2847     TCGv_i32 tmp, tmp2;
2848
2849     if ((insn & 0x0ff00f10) == 0x0e200010) {
2850         /* Multiply with Internal Accumulate Format */
2851         rd0 = (insn >> 12) & 0xf;
2852         rd1 = insn & 0xf;
2853         acc = (insn >> 5) & 7;
2854
2855         if (acc != 0)
2856             return 1;
2857
2858         tmp = load_reg(s, rd0);
2859         tmp2 = load_reg(s, rd1);
2860         switch ((insn >> 16) & 0xf) {
2861         case 0x0:                                       /* MIA */
2862             gen_helper_iwmmxt_muladdsl(cpu_M0, cpu_M0, tmp, tmp2);
2863             break;
2864         case 0x8:                                       /* MIAPH */
2865             gen_helper_iwmmxt_muladdsw(cpu_M0, cpu_M0, tmp, tmp2);
2866             break;
2867         case 0xc:                                       /* MIABB */
2868         case 0xd:                                       /* MIABT */
2869         case 0xe:                                       /* MIATB */
2870         case 0xf:                                       /* MIATT */
2871             if (insn & (1 << 16))
2872                 tcg_gen_shri_i32(tmp, tmp, 16);
2873             if (insn & (1 << 17))
2874                 tcg_gen_shri_i32(tmp2, tmp2, 16);
2875             gen_helper_iwmmxt_muladdswl(cpu_M0, cpu_M0, tmp, tmp2);
2876             break;
2877         default:
2878             return 1;
2879         }
2880         tcg_temp_free_i32(tmp2);
2881         tcg_temp_free_i32(tmp);
2882
2883         gen_op_iwmmxt_movq_wRn_M0(acc);
2884         return 0;
2885     }
2886
2887     if ((insn & 0x0fe00ff8) == 0x0c400000) {
2888         /* Internal Accumulator Access Format */
2889         rdhi = (insn >> 16) & 0xf;
2890         rdlo = (insn >> 12) & 0xf;
2891         acc = insn & 7;
2892
2893         if (acc != 0)
2894             return 1;
2895
2896         if (insn & ARM_CP_RW_BIT) {                     /* MRA */
2897             iwmmxt_load_reg(cpu_V0, acc);
2898             tcg_gen_extrl_i64_i32(cpu_R[rdlo], cpu_V0);
2899             tcg_gen_shri_i64(cpu_V0, cpu_V0, 32);
2900             tcg_gen_extrl_i64_i32(cpu_R[rdhi], cpu_V0);
2901             tcg_gen_andi_i32(cpu_R[rdhi], cpu_R[rdhi], (1 << (40 - 32)) - 1);
2902         } else {                                        /* MAR */
2903             tcg_gen_concat_i32_i64(cpu_V0, cpu_R[rdlo], cpu_R[rdhi]);
2904             iwmmxt_store_reg(cpu_V0, acc);
2905         }
2906         return 0;
2907     }
2908
2909     return 1;
2910 }
2911
2912 #define VFP_REG_SHR(x, n) (((n) > 0) ? (x) >> (n) : (x) << -(n))
2913 #define VFP_SREG(insn, bigbit, smallbit) \
2914   ((VFP_REG_SHR(insn, bigbit - 1) & 0x1e) | (((insn) >> (smallbit)) & 1))
2915 #define VFP_DREG(reg, insn, bigbit, smallbit) do { \
2916     if (arm_dc_feature(s, ARM_FEATURE_VFP3)) { \
2917         reg = (((insn) >> (bigbit)) & 0x0f) \
2918               | (((insn) >> ((smallbit) - 4)) & 0x10); \
2919     } else { \
2920         if (insn & (1 << (smallbit))) \
2921             return 1; \
2922         reg = ((insn) >> (bigbit)) & 0x0f; \
2923     }} while (0)
2924
2925 #define VFP_SREG_D(insn) VFP_SREG(insn, 12, 22)
2926 #define VFP_DREG_D(reg, insn) VFP_DREG(reg, insn, 12, 22)
2927 #define VFP_SREG_N(insn) VFP_SREG(insn, 16,  7)
2928 #define VFP_DREG_N(reg, insn) VFP_DREG(reg, insn, 16,  7)
2929 #define VFP_SREG_M(insn) VFP_SREG(insn,  0,  5)
2930 #define VFP_DREG_M(reg, insn) VFP_DREG(reg, insn,  0,  5)
2931
2932 /* Move between integer and VFP cores.  */
2933 static TCGv_i32 gen_vfp_mrs(void)
2934 {
2935     TCGv_i32 tmp = tcg_temp_new_i32();
2936     tcg_gen_mov_i32(tmp, cpu_F0s);
2937     return tmp;
2938 }
2939
2940 static void gen_vfp_msr(TCGv_i32 tmp)
2941 {
2942     tcg_gen_mov_i32(cpu_F0s, tmp);
2943     tcg_temp_free_i32(tmp);
2944 }
2945
2946 static void gen_neon_dup_u8(TCGv_i32 var, int shift)
2947 {
2948     TCGv_i32 tmp = tcg_temp_new_i32();
2949     if (shift)
2950         tcg_gen_shri_i32(var, var, shift);
2951     tcg_gen_ext8u_i32(var, var);
2952     tcg_gen_shli_i32(tmp, var, 8);
2953     tcg_gen_or_i32(var, var, tmp);
2954     tcg_gen_shli_i32(tmp, var, 16);
2955     tcg_gen_or_i32(var, var, tmp);
2956     tcg_temp_free_i32(tmp);
2957 }
2958
2959 static void gen_neon_dup_low16(TCGv_i32 var)
2960 {
2961     TCGv_i32 tmp = tcg_temp_new_i32();
2962     tcg_gen_ext16u_i32(var, var);
2963     tcg_gen_shli_i32(tmp, var, 16);
2964     tcg_gen_or_i32(var, var, tmp);
2965     tcg_temp_free_i32(tmp);
2966 }
2967
2968 static void gen_neon_dup_high16(TCGv_i32 var)
2969 {
2970     TCGv_i32 tmp = tcg_temp_new_i32();
2971     tcg_gen_andi_i32(var, var, 0xffff0000);
2972     tcg_gen_shri_i32(tmp, var, 16);
2973     tcg_gen_or_i32(var, var, tmp);
2974     tcg_temp_free_i32(tmp);
2975 }
2976
2977 static TCGv_i32 gen_load_and_replicate(DisasContext *s, TCGv_i32 addr, int size)
2978 {
2979     /* Load a single Neon element and replicate into a 32 bit TCG reg */
2980     TCGv_i32 tmp = tcg_temp_new_i32();
2981     switch (size) {
2982     case 0:
2983         gen_aa32_ld8u(s, tmp, addr, get_mem_index(s));
2984         gen_neon_dup_u8(tmp, 0);
2985         break;
2986     case 1:
2987         gen_aa32_ld16u(s, tmp, addr, get_mem_index(s));
2988         gen_neon_dup_low16(tmp);
2989         break;
2990     case 2:
2991         gen_aa32_ld32u(s, tmp, addr, get_mem_index(s));
2992         break;
2993     default: /* Avoid compiler warnings.  */
2994         abort();
2995     }
2996     return tmp;
2997 }
2998
2999 static int handle_vsel(uint32_t insn, uint32_t rd, uint32_t rn, uint32_t rm,
3000                        uint32_t dp)
3001 {
3002     uint32_t cc = extract32(insn, 20, 2);
3003
3004     if (dp) {
3005         TCGv_i64 frn, frm, dest;
3006         TCGv_i64 tmp, zero, zf, nf, vf;
3007
3008         zero = tcg_const_i64(0);
3009
3010         frn = tcg_temp_new_i64();
3011         frm = tcg_temp_new_i64();
3012         dest = tcg_temp_new_i64();
3013
3014         zf = tcg_temp_new_i64();
3015         nf = tcg_temp_new_i64();
3016         vf = tcg_temp_new_i64();
3017
3018         tcg_gen_extu_i32_i64(zf, cpu_ZF);
3019         tcg_gen_ext_i32_i64(nf, cpu_NF);
3020         tcg_gen_ext_i32_i64(vf, cpu_VF);
3021
3022         tcg_gen_ld_f64(frn, cpu_env, vfp_reg_offset(dp, rn));
3023         tcg_gen_ld_f64(frm, cpu_env, vfp_reg_offset(dp, rm));
3024         switch (cc) {
3025         case 0: /* eq: Z */
3026             tcg_gen_movcond_i64(TCG_COND_EQ, dest, zf, zero,
3027                                 frn, frm);
3028             break;
3029         case 1: /* vs: V */
3030             tcg_gen_movcond_i64(TCG_COND_LT, dest, vf, zero,
3031                                 frn, frm);
3032             break;
3033         case 2: /* ge: N == V -> N ^ V == 0 */
3034             tmp = tcg_temp_new_i64();
3035             tcg_gen_xor_i64(tmp, vf, nf);
3036             tcg_gen_movcond_i64(TCG_COND_GE, dest, tmp, zero,
3037                                 frn, frm);
3038             tcg_temp_free_i64(tmp);
3039             break;
3040         case 3: /* gt: !Z && N == V */
3041             tcg_gen_movcond_i64(TCG_COND_NE, dest, zf, zero,
3042                                 frn, frm);
3043             tmp = tcg_temp_new_i64();
3044             tcg_gen_xor_i64(tmp, vf, nf);
3045             tcg_gen_movcond_i64(TCG_COND_GE, dest, tmp, zero,
3046                                 dest, frm);
3047             tcg_temp_free_i64(tmp);
3048             break;
3049         }
3050         tcg_gen_st_f64(dest, cpu_env, vfp_reg_offset(dp, rd));
3051         tcg_temp_free_i64(frn);
3052         tcg_temp_free_i64(frm);
3053         tcg_temp_free_i64(dest);
3054
3055         tcg_temp_free_i64(zf);
3056         tcg_temp_free_i64(nf);
3057         tcg_temp_free_i64(vf);
3058
3059         tcg_temp_free_i64(zero);
3060     } else {
3061         TCGv_i32 frn, frm, dest;
3062         TCGv_i32 tmp, zero;
3063
3064         zero = tcg_const_i32(0);
3065
3066         frn = tcg_temp_new_i32();
3067         frm = tcg_temp_new_i32();
3068         dest = tcg_temp_new_i32();
3069         tcg_gen_ld_f32(frn, cpu_env, vfp_reg_offset(dp, rn));
3070         tcg_gen_ld_f32(frm, cpu_env, vfp_reg_offset(dp, rm));
3071         switch (cc) {
3072         case 0: /* eq: Z */
3073             tcg_gen_movcond_i32(TCG_COND_EQ, dest, cpu_ZF, zero,
3074                                 frn, frm);
3075             break;
3076         case 1: /* vs: V */
3077             tcg_gen_movcond_i32(TCG_COND_LT, dest, cpu_VF, zero,
3078                                 frn, frm);
3079             break;
3080         case 2: /* ge: N == V -> N ^ V == 0 */
3081             tmp = tcg_temp_new_i32();
3082             tcg_gen_xor_i32(tmp, cpu_VF, cpu_NF);
3083             tcg_gen_movcond_i32(TCG_COND_GE, dest, tmp, zero,
3084                                 frn, frm);
3085             tcg_temp_free_i32(tmp);
3086             break;
3087         case 3: /* gt: !Z && N == V */
3088             tcg_gen_movcond_i32(TCG_COND_NE, dest, cpu_ZF, zero,
3089                                 frn, frm);
3090             tmp = tcg_temp_new_i32();
3091             tcg_gen_xor_i32(tmp, cpu_VF, cpu_NF);
3092             tcg_gen_movcond_i32(TCG_COND_GE, dest, tmp, zero,
3093                                 dest, frm);
3094             tcg_temp_free_i32(tmp);
3095             break;
3096         }
3097         tcg_gen_st_f32(dest, cpu_env, vfp_reg_offset(dp, rd));
3098         tcg_temp_free_i32(frn);
3099         tcg_temp_free_i32(frm);
3100         tcg_temp_free_i32(dest);
3101
3102         tcg_temp_free_i32(zero);
3103     }
3104
3105     return 0;
3106 }
3107
3108 static int handle_vminmaxnm(uint32_t insn, uint32_t rd, uint32_t rn,
3109                             uint32_t rm, uint32_t dp)
3110 {
3111     uint32_t vmin = extract32(insn, 6, 1);
3112     TCGv_ptr fpst = get_fpstatus_ptr(0);
3113
3114     if (dp) {
3115         TCGv_i64 frn, frm, dest;
3116
3117         frn = tcg_temp_new_i64();
3118         frm = tcg_temp_new_i64();
3119         dest = tcg_temp_new_i64();
3120
3121         tcg_gen_ld_f64(frn, cpu_env, vfp_reg_offset(dp, rn));
3122         tcg_gen_ld_f64(frm, cpu_env, vfp_reg_offset(dp, rm));
3123         if (vmin) {
3124             gen_helper_vfp_minnumd(dest, frn, frm, fpst);
3125         } else {
3126             gen_helper_vfp_maxnumd(dest, frn, frm, fpst);
3127         }
3128         tcg_gen_st_f64(dest, cpu_env, vfp_reg_offset(dp, rd));
3129         tcg_temp_free_i64(frn);
3130         tcg_temp_free_i64(frm);
3131         tcg_temp_free_i64(dest);
3132     } else {
3133         TCGv_i32 frn, frm, dest;
3134
3135         frn = tcg_temp_new_i32();
3136         frm = tcg_temp_new_i32();
3137         dest = tcg_temp_new_i32();
3138
3139         tcg_gen_ld_f32(frn, cpu_env, vfp_reg_offset(dp, rn));
3140         tcg_gen_ld_f32(frm, cpu_env, vfp_reg_offset(dp, rm));
3141         if (vmin) {
3142             gen_helper_vfp_minnums(dest, frn, frm, fpst);
3143         } else {
3144             gen_helper_vfp_maxnums(dest, frn, frm, fpst);
3145         }
3146         tcg_gen_st_f32(dest, cpu_env, vfp_reg_offset(dp, rd));
3147         tcg_temp_free_i32(frn);
3148         tcg_temp_free_i32(frm);
3149         tcg_temp_free_i32(dest);
3150     }
3151
3152     tcg_temp_free_ptr(fpst);
3153     return 0;
3154 }
3155
3156 static int handle_vrint(uint32_t insn, uint32_t rd, uint32_t rm, uint32_t dp,
3157                         int rounding)
3158 {
3159     TCGv_ptr fpst = get_fpstatus_ptr(0);
3160     TCGv_i32 tcg_rmode;
3161
3162     tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rounding));
3163     gen_helper_set_rmode(tcg_rmode, tcg_rmode, fpst);
3164
3165     if (dp) {
3166         TCGv_i64 tcg_op;
3167         TCGv_i64 tcg_res;
3168         tcg_op = tcg_temp_new_i64();
3169         tcg_res = tcg_temp_new_i64();
3170         tcg_gen_ld_f64(tcg_op, cpu_env, vfp_reg_offset(dp, rm));
3171         gen_helper_rintd(tcg_res, tcg_op, fpst);
3172         tcg_gen_st_f64(tcg_res, cpu_env, vfp_reg_offset(dp, rd));
3173         tcg_temp_free_i64(tcg_op);
3174         tcg_temp_free_i64(tcg_res);
3175     } else {
3176         TCGv_i32 tcg_op;
3177         TCGv_i32 tcg_res;
3178         tcg_op = tcg_temp_new_i32();
3179         tcg_res = tcg_temp_new_i32();
3180         tcg_gen_ld_f32(tcg_op, cpu_env, vfp_reg_offset(dp, rm));
3181         gen_helper_rints(tcg_res, tcg_op, fpst);
3182         tcg_gen_st_f32(tcg_res, cpu_env, vfp_reg_offset(dp, rd));
3183         tcg_temp_free_i32(tcg_op);
3184         tcg_temp_free_i32(tcg_res);
3185     }
3186
3187     gen_helper_set_rmode(tcg_rmode, tcg_rmode, fpst);
3188     tcg_temp_free_i32(tcg_rmode);
3189
3190     tcg_temp_free_ptr(fpst);
3191     return 0;
3192 }
3193
3194 static int handle_vcvt(uint32_t insn, uint32_t rd, uint32_t rm, uint32_t dp,
3195                        int rounding)
3196 {
3197     bool is_signed = extract32(insn, 7, 1);
3198     TCGv_ptr fpst = get_fpstatus_ptr(0);
3199     TCGv_i32 tcg_rmode, tcg_shift;
3200
3201     tcg_shift = tcg_const_i32(0);
3202
3203     tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rounding));
3204     gen_helper_set_rmode(tcg_rmode, tcg_rmode, fpst);
3205
3206     if (dp) {
3207         TCGv_i64 tcg_double, tcg_res;
3208         TCGv_i32 tcg_tmp;
3209         /* Rd is encoded as a single precision register even when the source
3210          * is double precision.
3211          */
3212         rd = ((rd << 1) & 0x1e) | ((rd >> 4) & 0x1);
3213         tcg_double = tcg_temp_new_i64();
3214         tcg_res = tcg_temp_new_i64();
3215         tcg_tmp = tcg_temp_new_i32();
3216         tcg_gen_ld_f64(tcg_double, cpu_env, vfp_reg_offset(1, rm));
3217         if (is_signed) {
3218             gen_helper_vfp_tosld(tcg_res, tcg_double, tcg_shift, fpst);
3219         } else {
3220             gen_helper_vfp_tould(tcg_res, tcg_double, tcg_shift, fpst);
3221         }
3222         tcg_gen_extrl_i64_i32(tcg_tmp, tcg_res);
3223         tcg_gen_st_f32(tcg_tmp, cpu_env, vfp_reg_offset(0, rd));
3224         tcg_temp_free_i32(tcg_tmp);
3225         tcg_temp_free_i64(tcg_res);
3226         tcg_temp_free_i64(tcg_double);
3227     } else {
3228         TCGv_i32 tcg_single, tcg_res;
3229         tcg_single = tcg_temp_new_i32();
3230         tcg_res = tcg_temp_new_i32();
3231         tcg_gen_ld_f32(tcg_single, cpu_env, vfp_reg_offset(0, rm));
3232         if (is_signed) {
3233             gen_helper_vfp_tosls(tcg_res, tcg_single, tcg_shift, fpst);
3234         } else {
3235             gen_helper_vfp_touls(tcg_res, tcg_single, tcg_shift, fpst);
3236         }
3237         tcg_gen_st_f32(tcg_res, cpu_env, vfp_reg_offset(0, rd));
3238         tcg_temp_free_i32(tcg_res);
3239         tcg_temp_free_i32(tcg_single);
3240     }
3241
3242     gen_helper_set_rmode(tcg_rmode, tcg_rmode, fpst);
3243     tcg_temp_free_i32(tcg_rmode);
3244
3245     tcg_temp_free_i32(tcg_shift);
3246
3247     tcg_temp_free_ptr(fpst);
3248
3249     return 0;
3250 }
3251
3252 /* Table for converting the most common AArch32 encoding of
3253  * rounding mode to arm_fprounding order (which matches the
3254  * common AArch64 order); see ARM ARM pseudocode FPDecodeRM().
3255  */
3256 static const uint8_t fp_decode_rm[] = {
3257     FPROUNDING_TIEAWAY,
3258     FPROUNDING_TIEEVEN,
3259     FPROUNDING_POSINF,
3260     FPROUNDING_NEGINF,
3261 };
3262
3263 static int disas_vfp_v8_insn(DisasContext *s, uint32_t insn)
3264 {
3265     uint32_t rd, rn, rm, dp = extract32(insn, 8, 1);
3266
3267     if (!arm_dc_feature(s, ARM_FEATURE_V8)) {
3268         return 1;
3269     }
3270
3271     if (dp) {
3272         VFP_DREG_D(rd, insn);
3273         VFP_DREG_N(rn, insn);
3274         VFP_DREG_M(rm, insn);
3275     } else {
3276         rd = VFP_SREG_D(insn);
3277         rn = VFP_SREG_N(insn);
3278         rm = VFP_SREG_M(insn);
3279     }
3280
3281     if ((insn & 0x0f800e50) == 0x0e000a00) {
3282         return handle_vsel(insn, rd, rn, rm, dp);
3283     } else if ((insn & 0x0fb00e10) == 0x0e800a00) {
3284         return handle_vminmaxnm(insn, rd, rn, rm, dp);
3285     } else if ((insn & 0x0fbc0ed0) == 0x0eb80a40) {
3286         /* VRINTA, VRINTN, VRINTP, VRINTM */
3287         int rounding = fp_decode_rm[extract32(insn, 16, 2)];
3288         return handle_vrint(insn, rd, rm, dp, rounding);
3289     } else if ((insn & 0x0fbc0e50) == 0x0ebc0a40) {
3290         /* VCVTA, VCVTN, VCVTP, VCVTM */
3291         int rounding = fp_decode_rm[extract32(insn, 16, 2)];
3292         return handle_vcvt(insn, rd, rm, dp, rounding);
3293     }
3294     return 1;
3295 }
3296
3297 /* Disassemble a VFP instruction.  Returns nonzero if an error occurred
3298    (ie. an undefined instruction).  */
3299 static int disas_vfp_insn(DisasContext *s, uint32_t insn)
3300 {
3301     uint32_t rd, rn, rm, op, i, n, offset, delta_d, delta_m, bank_mask;
3302     int dp, veclen;
3303     TCGv_i32 addr;
3304     TCGv_i32 tmp;
3305     TCGv_i32 tmp2;
3306
3307     if (!arm_dc_feature(s, ARM_FEATURE_VFP)) {
3308         return 1;
3309     }
3310
3311     /* FIXME: this access check should not take precedence over UNDEF
3312      * for invalid encodings; we will generate incorrect syndrome information
3313      * for attempts to execute invalid vfp/neon encodings with FP disabled.
3314      */
3315     if (s->fp_excp_el) {
3316         gen_exception_insn(s, 4, EXCP_UDEF,
3317                            syn_fp_access_trap(1, 0xe, false), s->fp_excp_el);
3318         return 0;
3319     }
3320
3321     if (!s->vfp_enabled) {
3322         /* VFP disabled.  Only allow fmxr/fmrx to/from some control regs.  */
3323         if ((insn & 0x0fe00fff) != 0x0ee00a10)
3324             return 1;
3325         rn = (insn >> 16) & 0xf;
3326         if (rn != ARM_VFP_FPSID && rn != ARM_VFP_FPEXC && rn != ARM_VFP_MVFR2
3327             && rn != ARM_VFP_MVFR1 && rn != ARM_VFP_MVFR0) {
3328             return 1;
3329         }
3330     }
3331
3332     if (extract32(insn, 28, 4) == 0xf) {
3333         /* Encodings with T=1 (Thumb) or unconditional (ARM):
3334          * only used in v8 and above.
3335          */
3336         return disas_vfp_v8_insn(s, insn);
3337     }
3338
3339     dp = ((insn & 0xf00) == 0xb00);
3340     switch ((insn >> 24) & 0xf) {
3341     case 0xe:
3342         if (insn & (1 << 4)) {
3343             /* single register transfer */
3344             rd = (insn >> 12) & 0xf;
3345             if (dp) {
3346                 int size;
3347                 int pass;
3348
3349                 VFP_DREG_N(rn, insn);
3350                 if (insn & 0xf)
3351                     return 1;
3352                 if (insn & 0x00c00060
3353                     && !arm_dc_feature(s, ARM_FEATURE_NEON)) {
3354                     return 1;
3355                 }
3356
3357                 pass = (insn >> 21) & 1;
3358                 if (insn & (1 << 22)) {
3359                     size = 0;
3360                     offset = ((insn >> 5) & 3) * 8;
3361                 } else if (insn & (1 << 5)) {
3362                     size = 1;
3363                     offset = (insn & (1 << 6)) ? 16 : 0;
3364                 } else {
3365                     size = 2;
3366                     offset = 0;
3367                 }
3368                 if (insn & ARM_CP_RW_BIT) {
3369                     /* vfp->arm */
3370                     tmp = neon_load_reg(rn, pass);
3371                     switch (size) {
3372                     case 0:
3373                         if (offset)
3374                             tcg_gen_shri_i32(tmp, tmp, offset);
3375                         if (insn & (1 << 23))
3376                             gen_uxtb(tmp);
3377                         else
3378                             gen_sxtb(tmp);
3379                         break;
3380                     case 1:
3381                         if (insn & (1 << 23)) {
3382                             if (offset) {
3383                                 tcg_gen_shri_i32(tmp, tmp, 16);
3384                             } else {
3385                                 gen_uxth(tmp);
3386                             }
3387                         } else {
3388                             if (offset) {
3389                                 tcg_gen_sari_i32(tmp, tmp, 16);
3390                             } else {
3391                                 gen_sxth(tmp);
3392                             }
3393                         }
3394                         break;
3395                     case 2:
3396                         break;
3397                     }
3398                     store_reg(s, rd, tmp);
3399                 } else {
3400                     /* arm->vfp */
3401                     tmp = load_reg(s, rd);
3402                     if (insn & (1 << 23)) {
3403                         /* VDUP */
3404                         if (size == 0) {
3405                             gen_neon_dup_u8(tmp, 0);
3406                         } else if (size == 1) {
3407                             gen_neon_dup_low16(tmp);
3408                         }
3409                         for (n = 0; n <= pass * 2; n++) {
3410                             tmp2 = tcg_temp_new_i32();
3411                             tcg_gen_mov_i32(tmp2, tmp);
3412                             neon_store_reg(rn, n, tmp2);
3413                         }
3414                         neon_store_reg(rn, n, tmp);
3415                     } else {
3416                         /* VMOV */
3417                         switch (size) {
3418                         case 0:
3419                             tmp2 = neon_load_reg(rn, pass);
3420                             tcg_gen_deposit_i32(tmp, tmp2, tmp, offset, 8);
3421                             tcg_temp_free_i32(tmp2);
3422                             break;
3423                         case 1:
3424                             tmp2 = neon_load_reg(rn, pass);
3425                             tcg_gen_deposit_i32(tmp, tmp2, tmp, offset, 16);
3426                             tcg_temp_free_i32(tmp2);
3427                             break;
3428                         case 2:
3429                             break;
3430                         }
3431                         neon_store_reg(rn, pass, tmp);
3432                     }
3433                 }
3434             } else { /* !dp */
3435                 if ((insn & 0x6f) != 0x00)
3436                     return 1;
3437                 rn = VFP_SREG_N(insn);
3438                 if (insn & ARM_CP_RW_BIT) {
3439                     /* vfp->arm */
3440                     if (insn & (1 << 21)) {
3441                         /* system register */
3442                         rn >>= 1;
3443
3444                         switch (rn) {
3445                         case ARM_VFP_FPSID:
3446                             /* VFP2 allows access to FSID from userspace.
3447                                VFP3 restricts all id registers to privileged
3448                                accesses.  */
3449                             if (IS_USER(s)
3450                                 && arm_dc_feature(s, ARM_FEATURE_VFP3)) {
3451                                 return 1;
3452                             }
3453                             tmp = load_cpu_field(vfp.xregs[rn]);
3454                             break;
3455                         case ARM_VFP_FPEXC:
3456                             if (IS_USER(s))
3457                                 return 1;
3458                             tmp = load_cpu_field(vfp.xregs[rn]);
3459                             break;
3460                         case ARM_VFP_FPINST:
3461                         case ARM_VFP_FPINST2:
3462                             /* Not present in VFP3.  */
3463                             if (IS_USER(s)
3464                                 || arm_dc_feature(s, ARM_FEATURE_VFP3)) {
3465                                 return 1;
3466                             }
3467                             tmp = load_cpu_field(vfp.xregs[rn]);
3468                             break;
3469                         case ARM_VFP_FPSCR:
3470                             if (rd == 15) {
3471                                 tmp = load_cpu_field(vfp.xregs[ARM_VFP_FPSCR]);
3472                                 tcg_gen_andi_i32(tmp, tmp, 0xf0000000);
3473                             } else {
3474                                 tmp = tcg_temp_new_i32();
3475                                 gen_helper_vfp_get_fpscr(tmp, cpu_env);
3476                             }
3477                             break;
3478                         case ARM_VFP_MVFR2:
3479                             if (!arm_dc_feature(s, ARM_FEATURE_V8)) {
3480                                 return 1;
3481                             }
3482                             /* fall through */
3483                         case ARM_VFP_MVFR0:
3484                         case ARM_VFP_MVFR1:
3485                             if (IS_USER(s)
3486                                 || !arm_dc_feature(s, ARM_FEATURE_MVFR)) {
3487                                 return 1;
3488                             }
3489                             tmp = load_cpu_field(vfp.xregs[rn]);
3490                             break;
3491                         default:
3492                             return 1;
3493                         }
3494                     } else {
3495                         gen_mov_F0_vreg(0, rn);
3496                         tmp = gen_vfp_mrs();
3497                     }
3498                     if (rd == 15) {
3499                         /* Set the 4 flag bits in the CPSR.  */
3500                         gen_set_nzcv(tmp);
3501                         tcg_temp_free_i32(tmp);
3502                     } else {
3503                         store_reg(s, rd, tmp);
3504                     }
3505                 } else {
3506                     /* arm->vfp */
3507                     if (insn & (1 << 21)) {
3508                         rn >>= 1;
3509                         /* system register */
3510                         switch (rn) {
3511                         case ARM_VFP_FPSID:
3512                         case ARM_VFP_MVFR0:
3513                         case ARM_VFP_MVFR1:
3514                             /* Writes are ignored.  */
3515                             break;
3516                         case ARM_VFP_FPSCR:
3517                             tmp = load_reg(s, rd);
3518                             gen_helper_vfp_set_fpscr(cpu_env, tmp);
3519                             tcg_temp_free_i32(tmp);
3520                             gen_lookup_tb(s);
3521                             break;
3522                         case ARM_VFP_FPEXC:
3523                             if (IS_USER(s))
3524                                 return 1;
3525                             /* TODO: VFP subarchitecture support.
3526                              * For now, keep the EN bit only */
3527                             tmp = load_reg(s, rd);
3528                             tcg_gen_andi_i32(tmp, tmp, 1 << 30);
3529                             store_cpu_field(tmp, vfp.xregs[rn]);
3530                             gen_lookup_tb(s);
3531                             break;
3532                         case ARM_VFP_FPINST:
3533                         case ARM_VFP_FPINST2:
3534                             if (IS_USER(s)) {
3535                                 return 1;
3536                             }
3537                             tmp = load_reg(s, rd);
3538                             store_cpu_field(tmp, vfp.xregs[rn]);
3539                             break;
3540                         default:
3541                             return 1;
3542                         }
3543                     } else {
3544                         tmp = load_reg(s, rd);
3545                         gen_vfp_msr(tmp);
3546                         gen_mov_vreg_F0(0, rn);
3547                     }
3548                 }
3549             }
3550         } else {
3551             /* data processing */
3552             /* The opcode is in bits 23, 21, 20 and 6.  */
3553             op = ((insn >> 20) & 8) | ((insn >> 19) & 6) | ((insn >> 6) & 1);
3554             if (dp) {
3555                 if (op == 15) {
3556                     /* rn is opcode */
3557                     rn = ((insn >> 15) & 0x1e) | ((insn >> 7) & 1);
3558                 } else {
3559                     /* rn is register number */
3560                     VFP_DREG_N(rn, insn);
3561                 }
3562
3563                 if (op == 15 && (rn == 15 || ((rn & 0x1c) == 0x18) ||
3564                                  ((rn & 0x1e) == 0x6))) {
3565                     /* Integer or single/half precision destination.  */
3566                     rd = VFP_SREG_D(insn);
3567                 } else {
3568                     VFP_DREG_D(rd, insn);
3569                 }
3570                 if (op == 15 &&
3571                     (((rn & 0x1c) == 0x10) || ((rn & 0x14) == 0x14) ||
3572                      ((rn & 0x1e) == 0x4))) {
3573                     /* VCVT from int or half precision is always from S reg
3574                      * regardless of dp bit. VCVT with immediate frac_bits
3575                      * has same format as SREG_M.
3576                      */
3577                     rm = VFP_SREG_M(insn);
3578                 } else {
3579                     VFP_DREG_M(rm, insn);
3580                 }
3581             } else {
3582                 rn = VFP_SREG_N(insn);
3583                 if (op == 15 && rn == 15) {
3584                     /* Double precision destination.  */
3585                     VFP_DREG_D(rd, insn);
3586                 } else {
3587                     rd = VFP_SREG_D(insn);
3588                 }
3589                 /* NB that we implicitly rely on the encoding for the frac_bits
3590                  * in VCVT of fixed to float being the same as that of an SREG_M
3591                  */
3592                 rm = VFP_SREG_M(insn);
3593             }
3594
3595             veclen = s->vec_len;
3596             if (op == 15 && rn > 3)
3597                 veclen = 0;
3598
3599             /* Shut up compiler warnings.  */
3600             delta_m = 0;
3601             delta_d = 0;
3602             bank_mask = 0;
3603
3604             if (veclen > 0) {
3605                 if (dp)
3606                     bank_mask = 0xc;
3607                 else
3608                     bank_mask = 0x18;
3609
3610                 /* Figure out what type of vector operation this is.  */
3611                 if ((rd & bank_mask) == 0) {
3612                     /* scalar */
3613                     veclen = 0;
3614                 } else {
3615                     if (dp)
3616                         delta_d = (s->vec_stride >> 1) + 1;
3617                     else
3618                         delta_d = s->vec_stride + 1;
3619
3620                     if ((rm & bank_mask) == 0) {
3621                         /* mixed scalar/vector */
3622                         delta_m = 0;
3623                     } else {
3624                         /* vector */
3625                         delta_m = delta_d;
3626                     }
3627                 }
3628             }
3629
3630             /* Load the initial operands.  */
3631             if (op == 15) {
3632                 switch (rn) {
3633                 case 16:
3634                 case 17:
3635                     /* Integer source */
3636                     gen_mov_F0_vreg(0, rm);
3637                     break;
3638                 case 8:
3639                 case 9:
3640                     /* Compare */
3641                     gen_mov_F0_vreg(dp, rd);
3642                     gen_mov_F1_vreg(dp, rm);
3643                     break;
3644                 case 10:
3645                 case 11:
3646                     /* Compare with zero */
3647                     gen_mov_F0_vreg(dp, rd);
3648                     gen_vfp_F1_ld0(dp);
3649                     break;
3650                 case 20:
3651                 case 21:
3652                 case 22:
3653                 case 23:
3654                 case 28:
3655                 case 29:
3656                 case 30:
3657                 case 31:
3658                     /* Source and destination the same.  */
3659                     gen_mov_F0_vreg(dp, rd);
3660                     break;
3661                 case 4:
3662                 case 5:
3663                 case 6:
3664                 case 7:
3665                     /* VCVTB, VCVTT: only present with the halfprec extension
3666                      * UNPREDICTABLE if bit 8 is set prior to ARMv8
3667                      * (we choose to UNDEF)
3668                      */
3669                     if ((dp && !arm_dc_feature(s, ARM_FEATURE_V8)) ||
3670                         !arm_dc_feature(s, ARM_FEATURE_VFP_FP16)) {
3671                         return 1;
3672                     }
3673                     if (!extract32(rn, 1, 1)) {
3674                         /* Half precision source.  */
3675                         gen_mov_F0_vreg(0, rm);
3676                         break;
3677                     }
3678                     /* Otherwise fall through */
3679                 default:
3680                     /* One source operand.  */
3681                     gen_mov_F0_vreg(dp, rm);
3682                     break;
3683                 }
3684             } else {
3685                 /* Two source operands.  */
3686                 gen_mov_F0_vreg(dp, rn);
3687                 gen_mov_F1_vreg(dp, rm);
3688             }
3689
3690             for (;;) {
3691                 /* Perform the calculation.  */
3692                 switch (op) {
3693                 case 0: /* VMLA: fd + (fn * fm) */
3694                     /* Note that order of inputs to the add matters for NaNs */
3695                     gen_vfp_F1_mul(dp);
3696                     gen_mov_F0_vreg(dp, rd);
3697                     gen_vfp_add(dp);
3698                     break;
3699                 case 1: /* VMLS: fd + -(fn * fm) */
3700                     gen_vfp_mul(dp);
3701                     gen_vfp_F1_neg(dp);
3702                     gen_mov_F0_vreg(dp, rd);
3703                     gen_vfp_add(dp);
3704                     break;
3705                 case 2: /* VNMLS: -fd + (fn * fm) */
3706                     /* Note that it isn't valid to replace (-A + B) with (B - A)
3707                      * or similar plausible looking simplifications
3708                      * because this will give wrong results for NaNs.
3709                      */
3710                     gen_vfp_F1_mul(dp);
3711                     gen_mov_F0_vreg(dp, rd);
3712                     gen_vfp_neg(dp);
3713                     gen_vfp_add(dp);
3714                     break;
3715                 case 3: /* VNMLA: -fd + -(fn * fm) */
3716                     gen_vfp_mul(dp);
3717                     gen_vfp_F1_neg(dp);
3718                     gen_mov_F0_vreg(dp, rd);
3719                     gen_vfp_neg(dp);
3720                     gen_vfp_add(dp);
3721                     break;
3722                 case 4: /* mul: fn * fm */
3723                     gen_vfp_mul(dp);
3724                     break;
3725                 case 5: /* nmul: -(fn * fm) */
3726                     gen_vfp_mul(dp);
3727                     gen_vfp_neg(dp);
3728                     break;
3729                 case 6: /* add: fn + fm */
3730                     gen_vfp_add(dp);
3731                     break;
3732                 case 7: /* sub: fn - fm */
3733                     gen_vfp_sub(dp);
3734                     break;
3735                 case 8: /* div: fn / fm */
3736                     gen_vfp_div(dp);
3737                     break;
3738                 case 10: /* VFNMA : fd = muladd(-fd,  fn, fm) */
3739                 case 11: /* VFNMS : fd = muladd(-fd, -fn, fm) */
3740                 case 12: /* VFMA  : fd = muladd( fd,  fn, fm) */
3741                 case 13: /* VFMS  : fd = muladd( fd, -fn, fm) */
3742                     /* These are fused multiply-add, and must be done as one
3743                      * floating point operation with no rounding between the
3744                      * multiplication and addition steps.
3745                      * NB that doing the negations here as separate steps is
3746                      * correct : an input NaN should come out with its sign bit
3747                      * flipped if it is a negated-input.
3748                      */
3749                     if (!arm_dc_feature(s, ARM_FEATURE_VFP4)) {
3750                         return 1;
3751                     }
3752                     if (dp) {
3753                         TCGv_ptr fpst;
3754                         TCGv_i64 frd;
3755                         if (op & 1) {
3756                             /* VFNMS, VFMS */
3757                             gen_helper_vfp_negd(cpu_F0d, cpu_F0d);
3758                         }
3759                         frd = tcg_temp_new_i64();
3760                         tcg_gen_ld_f64(frd, cpu_env, vfp_reg_offset(dp, rd));
3761                         if (op & 2) {
3762                             /* VFNMA, VFNMS */
3763                             gen_helper_vfp_negd(frd, frd);
3764                         }
3765                         fpst = get_fpstatus_ptr(0);
3766                         gen_helper_vfp_muladdd(cpu_F0d, cpu_F0d,
3767                                                cpu_F1d, frd, fpst);
3768                         tcg_temp_free_ptr(fpst);
3769                         tcg_temp_free_i64(frd);
3770                     } else {
3771                         TCGv_ptr fpst;
3772                         TCGv_i32 frd;
3773                         if (op & 1) {
3774                             /* VFNMS, VFMS */
3775                             gen_helper_vfp_negs(cpu_F0s, cpu_F0s);
3776                         }
3777                         frd = tcg_temp_new_i32();
3778                         tcg_gen_ld_f32(frd, cpu_env, vfp_reg_offset(dp, rd));
3779                         if (op & 2) {
3780                             gen_helper_vfp_negs(frd, frd);
3781                         }
3782                         fpst = get_fpstatus_ptr(0);
3783                         gen_helper_vfp_muladds(cpu_F0s, cpu_F0s,
3784                                                cpu_F1s, frd, fpst);
3785                         tcg_temp_free_ptr(fpst);
3786                         tcg_temp_free_i32(frd);
3787                     }
3788                     break;
3789                 case 14: /* fconst */
3790                     if (!arm_dc_feature(s, ARM_FEATURE_VFP3)) {
3791                         return 1;
3792                     }
3793
3794                     n = (insn << 12) & 0x80000000;
3795                     i = ((insn >> 12) & 0x70) | (insn & 0xf);
3796                     if (dp) {
3797                         if (i & 0x40)
3798                             i |= 0x3f80;
3799                         else
3800                             i |= 0x4000;
3801                         n |= i << 16;
3802                         tcg_gen_movi_i64(cpu_F0d, ((uint64_t)n) << 32);
3803                     } else {
3804                         if (i & 0x40)
3805                             i |= 0x780;
3806                         else
3807                             i |= 0x800;
3808                         n |= i << 19;
3809                         tcg_gen_movi_i32(cpu_F0s, n);
3810                     }
3811                     break;
3812                 case 15: /* extension space */
3813                     switch (rn) {
3814                     case 0: /* cpy */
3815                         /* no-op */
3816                         break;
3817                     case 1: /* abs */
3818                         gen_vfp_abs(dp);
3819                         break;
3820                     case 2: /* neg */
3821                         gen_vfp_neg(dp);
3822                         break;
3823                     case 3: /* sqrt */
3824                         gen_vfp_sqrt(dp);
3825                         break;
3826                     case 4: /* vcvtb.f32.f16, vcvtb.f64.f16 */
3827                         tmp = gen_vfp_mrs();
3828                         tcg_gen_ext16u_i32(tmp, tmp);
3829                         if (dp) {
3830                             gen_helper_vfp_fcvt_f16_to_f64(cpu_F0d, tmp,
3831                                                            cpu_env);
3832                         } else {
3833                             gen_helper_vfp_fcvt_f16_to_f32(cpu_F0s, tmp,
3834                                                            cpu_env);
3835                         }
3836                         tcg_temp_free_i32(tmp);
3837                         break;
3838                     case 5: /* vcvtt.f32.f16, vcvtt.f64.f16 */
3839                         tmp = gen_vfp_mrs();
3840                         tcg_gen_shri_i32(tmp, tmp, 16);
3841                         if (dp) {
3842                             gen_helper_vfp_fcvt_f16_to_f64(cpu_F0d, tmp,
3843                                                            cpu_env);
3844                         } else {
3845                             gen_helper_vfp_fcvt_f16_to_f32(cpu_F0s, tmp,
3846                                                            cpu_env);
3847                         }
3848                         tcg_temp_free_i32(tmp);
3849                         break;
3850                     case 6: /* vcvtb.f16.f32, vcvtb.f16.f64 */
3851                         tmp = tcg_temp_new_i32();
3852                         if (dp) {
3853                             gen_helper_vfp_fcvt_f64_to_f16(tmp, cpu_F0d,
3854                                                            cpu_env);
3855                         } else {
3856                             gen_helper_vfp_fcvt_f32_to_f16(tmp, cpu_F0s,
3857                                                            cpu_env);
3858                         }
3859                         gen_mov_F0_vreg(0, rd);
3860                         tmp2 = gen_vfp_mrs();
3861                         tcg_gen_andi_i32(tmp2, tmp2, 0xffff0000);
3862                         tcg_gen_or_i32(tmp, tmp, tmp2);
3863                         tcg_temp_free_i32(tmp2);
3864                         gen_vfp_msr(tmp);
3865                         break;
3866                     case 7: /* vcvtt.f16.f32, vcvtt.f16.f64 */
3867                         tmp = tcg_temp_new_i32();
3868                         if (dp) {
3869                             gen_helper_vfp_fcvt_f64_to_f16(tmp, cpu_F0d,
3870                                                            cpu_env);
3871                         } else {
3872                             gen_helper_vfp_fcvt_f32_to_f16(tmp, cpu_F0s,
3873                                                            cpu_env);
3874                         }
3875                         tcg_gen_shli_i32(tmp, tmp, 16);
3876                         gen_mov_F0_vreg(0, rd);
3877                         tmp2 = gen_vfp_mrs();
3878                         tcg_gen_ext16u_i32(tmp2, tmp2);
3879                         tcg_gen_or_i32(tmp, tmp, tmp2);
3880                         tcg_temp_free_i32(tmp2);
3881                         gen_vfp_msr(tmp);
3882                         break;
3883                     case 8: /* cmp */
3884                         gen_vfp_cmp(dp);
3885                         break;
3886                     case 9: /* cmpe */
3887                         gen_vfp_cmpe(dp);
3888                         break;
3889                     case 10: /* cmpz */
3890                         gen_vfp_cmp(dp);
3891                         break;
3892                     case 11: /* cmpez */
3893                         gen_vfp_F1_ld0(dp);
3894                         gen_vfp_cmpe(dp);
3895                         break;
3896                     case 12: /* vrintr */
3897                     {
3898                         TCGv_ptr fpst = get_fpstatus_ptr(0);
3899                         if (dp) {
3900                             gen_helper_rintd(cpu_F0d, cpu_F0d, fpst);
3901                         } else {
3902                             gen_helper_rints(cpu_F0s, cpu_F0s, fpst);
3903                         }
3904                         tcg_temp_free_ptr(fpst);
3905                         break;
3906                     }
3907                     case 13: /* vrintz */
3908                     {
3909                         TCGv_ptr fpst = get_fpstatus_ptr(0);
3910                         TCGv_i32 tcg_rmode;
3911                         tcg_rmode = tcg_const_i32(float_round_to_zero);
3912                         gen_helper_set_rmode(tcg_rmode, tcg_rmode, fpst);
3913                         if (dp) {
3914                             gen_helper_rintd(cpu_F0d, cpu_F0d, fpst);
3915                         } else {
3916                             gen_helper_rints(cpu_F0s, cpu_F0s, fpst);
3917                         }
3918                         gen_helper_set_rmode(tcg_rmode, tcg_rmode, fpst);
3919                         tcg_temp_free_i32(tcg_rmode);
3920                         tcg_temp_free_ptr(fpst);
3921                         break;
3922                     }
3923                     case 14: /* vrintx */
3924                     {
3925                         TCGv_ptr fpst = get_fpstatus_ptr(0);
3926                         if (dp) {
3927                             gen_helper_rintd_exact(cpu_F0d, cpu_F0d, fpst);
3928                         } else {
3929                             gen_helper_rints_exact(cpu_F0s, cpu_F0s, fpst);
3930                         }
3931                         tcg_temp_free_ptr(fpst);
3932                         break;
3933                     }
3934                     case 15: /* single<->double conversion */
3935                         if (dp)
3936                             gen_helper_vfp_fcvtsd(cpu_F0s, cpu_F0d, cpu_env);
3937                         else
3938                             gen_helper_vfp_fcvtds(cpu_F0d, cpu_F0s, cpu_env);
3939                         break;
3940                     case 16: /* fuito */
3941                         gen_vfp_uito(dp, 0);
3942                         break;
3943                     case 17: /* fsito */
3944                         gen_vfp_sito(dp, 0);
3945                         break;
3946                     case 20: /* fshto */
3947                         if (!arm_dc_feature(s, ARM_FEATURE_VFP3)) {
3948                             return 1;
3949                         }
3950                         gen_vfp_shto(dp, 16 - rm, 0);
3951                         break;
3952                     case 21: /* fslto */
3953                         if (!arm_dc_feature(s, ARM_FEATURE_VFP3)) {
3954                             return 1;
3955                         }
3956                         gen_vfp_slto(dp, 32 - rm, 0);
3957                         break;
3958                     case 22: /* fuhto */
3959                         if (!arm_dc_feature(s, ARM_FEATURE_VFP3)) {
3960                             return 1;
3961                         }
3962                         gen_vfp_uhto(dp, 16 - rm, 0);
3963                         break;
3964                     case 23: /* fulto */
3965                         if (!arm_dc_feature(s, ARM_FEATURE_VFP3)) {
3966                             return 1;
3967                         }
3968                         gen_vfp_ulto(dp, 32 - rm, 0);
3969                         break;
3970                     case 24: /* ftoui */
3971                         gen_vfp_toui(dp, 0);
3972                         break;
3973                     case 25: /* ftouiz */
3974                         gen_vfp_touiz(dp, 0);
3975                         break;
3976                     case 26: /* ftosi */
3977                         gen_vfp_tosi(dp, 0);
3978                         break;
3979                     case 27: /* ftosiz */
3980                         gen_vfp_tosiz(dp, 0);
3981                         break;
3982                     case 28: /* ftosh */
3983                         if (!arm_dc_feature(s, ARM_FEATURE_VFP3)) {
3984                             return 1;
3985                         }
3986                         gen_vfp_tosh(dp, 16 - rm, 0);
3987                         break;
3988                     case 29: /* ftosl */
3989                         if (!arm_dc_feature(s, ARM_FEATURE_VFP3)) {
3990                             return 1;
3991                         }
3992                         gen_vfp_tosl(dp, 32 - rm, 0);
3993                         break;
3994                     case 30: /* ftouh */
3995                         if (!arm_dc_feature(s, ARM_FEATURE_VFP3)) {
3996                             return 1;
3997                         }
3998                         gen_vfp_touh(dp, 16 - rm, 0);
3999                         break;
4000                     case 31: /* ftoul */
4001                         if (!arm_dc_feature(s, ARM_FEATURE_VFP3)) {
4002                             return 1;
4003                         }
4004                         gen_vfp_toul(dp, 32 - rm, 0);
4005                         break;
4006                     default: /* undefined */
4007                         return 1;
4008                     }
4009                     break;
4010                 default: /* undefined */
4011                     return 1;
4012                 }
4013
4014                 /* Write back the result.  */
4015                 if (op == 15 && (rn >= 8 && rn <= 11)) {
4016                     /* Comparison, do nothing.  */
4017                 } else if (op == 15 && dp && ((rn & 0x1c) == 0x18 ||
4018                                               (rn & 0x1e) == 0x6)) {
4019                     /* VCVT double to int: always integer result.
4020                      * VCVT double to half precision is always a single
4021                      * precision result.
4022                      */
4023                     gen_mov_vreg_F0(0, rd);
4024                 } else if (op == 15 && rn == 15) {
4025                     /* conversion */
4026                     gen_mov_vreg_F0(!dp, rd);
4027                 } else {
4028                     gen_mov_vreg_F0(dp, rd);
4029                 }
4030
4031                 /* break out of the loop if we have finished  */
4032                 if (veclen == 0)
4033                     break;
4034
4035                 if (op == 15 && delta_m == 0) {
4036                     /* single source one-many */
4037                     while (veclen--) {
4038                         rd = ((rd + delta_d) & (bank_mask - 1))
4039                              | (rd & bank_mask);
4040                         gen_mov_vreg_F0(dp, rd);
4041                     }
4042                     break;
4043                 }
4044                 /* Setup the next operands.  */
4045                 veclen--;
4046                 rd = ((rd + delta_d) & (bank_mask - 1))
4047                      | (rd & bank_mask);
4048
4049                 if (op == 15) {
4050                     /* One source operand.  */
4051                     rm = ((rm + delta_m) & (bank_mask - 1))
4052                          | (rm & bank_mask);
4053                     gen_mov_F0_vreg(dp, rm);
4054                 } else {
4055                     /* Two source operands.  */
4056                     rn = ((rn + delta_d) & (bank_mask - 1))
4057                          | (rn & bank_mask);
4058                     gen_mov_F0_vreg(dp, rn);
4059                     if (delta_m) {
4060                         rm = ((rm + delta_m) & (bank_mask - 1))
4061                              | (rm & bank_mask);
4062                         gen_mov_F1_vreg(dp, rm);
4063                     }
4064                 }
4065             }
4066         }
4067         break;
4068     case 0xc:
4069     case 0xd:
4070         if ((insn & 0x03e00000) == 0x00400000) {
4071             /* two-register transfer */
4072             rn = (insn >> 16) & 0xf;
4073             rd = (insn >> 12) & 0xf;
4074             if (dp) {
4075                 VFP_DREG_M(rm, insn);
4076             } else {
4077                 rm = VFP_SREG_M(insn);
4078             }
4079
4080             if (insn & ARM_CP_RW_BIT) {
4081                 /* vfp->arm */
4082                 if (dp) {
4083                     gen_mov_F0_vreg(0, rm * 2);
4084                     tmp = gen_vfp_mrs();
4085                     store_reg(s, rd, tmp);
4086                     gen_mov_F0_vreg(0, rm * 2 + 1);
4087                     tmp = gen_vfp_mrs();
4088                     store_reg(s, rn, tmp);
4089                 } else {
4090                     gen_mov_F0_vreg(0, rm);
4091                     tmp = gen_vfp_mrs();
4092                     store_reg(s, rd, tmp);
4093                     gen_mov_F0_vreg(0, rm + 1);
4094                     tmp = gen_vfp_mrs();
4095                     store_reg(s, rn, tmp);
4096                 }
4097             } else {
4098                 /* arm->vfp */
4099                 if (dp) {
4100                     tmp = load_reg(s, rd);
4101                     gen_vfp_msr(tmp);
4102                     gen_mov_vreg_F0(0, rm * 2);
4103                     tmp = load_reg(s, rn);
4104                     gen_vfp_msr(tmp);
4105                     gen_mov_vreg_F0(0, rm * 2 + 1);
4106                 } else {
4107                     tmp = load_reg(s, rd);
4108                     gen_vfp_msr(tmp);
4109                     gen_mov_vreg_F0(0, rm);
4110                     tmp = load_reg(s, rn);
4111                     gen_vfp_msr(tmp);
4112                     gen_mov_vreg_F0(0, rm + 1);
4113                 }
4114             }
4115         } else {
4116             /* Load/store */
4117             rn = (insn >> 16) & 0xf;
4118             if (dp)
4119                 VFP_DREG_D(rd, insn);
4120             else
4121                 rd = VFP_SREG_D(insn);
4122             if ((insn & 0x01200000) == 0x01000000) {
4123                 /* Single load/store */
4124                 offset = (insn & 0xff) << 2;
4125                 if ((insn & (1 << 23)) == 0)
4126                     offset = -offset;
4127                 if (s->thumb && rn == 15) {
4128                     /* This is actually UNPREDICTABLE */
4129                     addr = tcg_temp_new_i32();
4130                     tcg_gen_movi_i32(addr, s->pc & ~2);
4131                 } else {
4132                     addr = load_reg(s, rn);
4133                 }
4134                 tcg_gen_addi_i32(addr, addr, offset);
4135                 if (insn & (1 << 20)) {
4136                     gen_vfp_ld(s, dp, addr);
4137                     gen_mov_vreg_F0(dp, rd);
4138                 } else {
4139                     gen_mov_F0_vreg(dp, rd);
4140                     gen_vfp_st(s, dp, addr);
4141                 }
4142                 tcg_temp_free_i32(addr);
4143             } else {
4144                 /* load/store multiple */
4145                 int w = insn & (1 << 21);
4146                 if (dp)
4147                     n = (insn >> 1) & 0x7f;
4148                 else
4149                     n = insn & 0xff;
4150
4151                 if (w && !(((insn >> 23) ^ (insn >> 24)) & 1)) {
4152                     /* P == U , W == 1  => UNDEF */
4153                     return 1;
4154                 }
4155                 if (n == 0 || (rd + n) > 32 || (dp && n > 16)) {
4156                     /* UNPREDICTABLE cases for bad immediates: we choose to
4157                      * UNDEF to avoid generating huge numbers of TCG ops
4158                      */
4159                     return 1;
4160                 }
4161                 if (rn == 15 && w) {
4162                     /* writeback to PC is UNPREDICTABLE, we choose to UNDEF */
4163                     return 1;
4164                 }
4165
4166                 if (s->thumb && rn == 15) {
4167                     /* This is actually UNPREDICTABLE */
4168                     addr = tcg_temp_new_i32();
4169                     tcg_gen_movi_i32(addr, s->pc & ~2);
4170                 } else {
4171                     addr = load_reg(s, rn);
4172                 }
4173                 if (insn & (1 << 24)) /* pre-decrement */
4174                     tcg_gen_addi_i32(addr, addr, -((insn & 0xff) << 2));
4175
4176                 if (dp)
4177                     offset = 8;
4178                 else
4179                     offset = 4;
4180                 for (i = 0; i < n; i++) {
4181                     if (insn & ARM_CP_RW_BIT) {
4182                         /* load */
4183                         gen_vfp_ld(s, dp, addr);
4184                         gen_mov_vreg_F0(dp, rd + i);
4185                     } else {
4186                         /* store */
4187                         gen_mov_F0_vreg(dp, rd + i);
4188                         gen_vfp_st(s, dp, addr);
4189                     }
4190                     tcg_gen_addi_i32(addr, addr, offset);
4191                 }
4192                 if (w) {
4193                     /* writeback */
4194                     if (insn & (1 << 24))
4195                         offset = -offset * n;
4196                     else if (dp && (insn & 1))
4197                         offset = 4;
4198                     else
4199                         offset = 0;
4200
4201                     if (offset != 0)
4202                         tcg_gen_addi_i32(addr, addr, offset);
4203                     store_reg(s, rn, addr);
4204                 } else {
4205                     tcg_temp_free_i32(addr);
4206                 }
4207             }
4208         }
4209         break;
4210     default:
4211         /* Should never happen.  */
4212         return 1;
4213     }
4214     return 0;
4215 }
4216
4217 static inline bool use_goto_tb(DisasContext *s, target_ulong dest)
4218 {
4219 #ifndef CONFIG_USER_ONLY
4220     return (s->base.tb->pc & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK) ||
4221            ((s->pc - 1) & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK);
4222 #else
4223     return true;
4224 #endif
4225 }
4226
4227 static void gen_goto_ptr(void)
4228 {
4229     tcg_gen_lookup_and_goto_ptr();
4230 }
4231
4232 /* This will end the TB but doesn't guarantee we'll return to
4233  * cpu_loop_exec. Any live exit_requests will be processed as we
4234  * enter the next TB.
4235  */
4236 static void gen_goto_tb(DisasContext *s, int n, target_ulong dest)
4237 {
4238     if (use_goto_tb(s, dest)) {
4239         tcg_gen_goto_tb(n);
4240         gen_set_pc_im(s, dest);
4241         tcg_gen_exit_tb((uintptr_t)s->base.tb + n);
4242     } else {
4243         gen_set_pc_im(s, dest);
4244         gen_goto_ptr();
4245     }
4246     s->base.is_jmp = DISAS_NORETURN;
4247 }
4248
4249 static inline void gen_jmp (DisasContext *s, uint32_t dest)
4250 {
4251     if (unlikely(is_singlestepping(s))) {
4252         /* An indirect jump so that we still trigger the debug exception.  */
4253         if (s->thumb)
4254             dest |= 1;
4255         gen_bx_im(s, dest);
4256     } else {
4257         gen_goto_tb(s, 0, dest);
4258     }
4259 }
4260
4261 static inline void gen_mulxy(TCGv_i32 t0, TCGv_i32 t1, int x, int y)
4262 {
4263     if (x)
4264         tcg_gen_sari_i32(t0, t0, 16);
4265     else
4266         gen_sxth(t0);
4267     if (y)
4268         tcg_gen_sari_i32(t1, t1, 16);
4269     else
4270         gen_sxth(t1);
4271     tcg_gen_mul_i32(t0, t0, t1);
4272 }
4273
4274 /* Return the mask of PSR bits set by a MSR instruction.  */
4275 static uint32_t msr_mask(DisasContext *s, int flags, int spsr)
4276 {
4277     uint32_t mask;
4278
4279     mask = 0;
4280     if (flags & (1 << 0))
4281         mask |= 0xff;
4282     if (flags & (1 << 1))
4283         mask |= 0xff00;
4284     if (flags & (1 << 2))
4285         mask |= 0xff0000;
4286     if (flags & (1 << 3))
4287         mask |= 0xff000000;
4288
4289     /* Mask out undefined bits.  */
4290     mask &= ~CPSR_RESERVED;
4291     if (!arm_dc_feature(s, ARM_FEATURE_V4T)) {
4292         mask &= ~CPSR_T;
4293     }
4294     if (!arm_dc_feature(s, ARM_FEATURE_V5)) {
4295         mask &= ~CPSR_Q; /* V5TE in reality*/
4296     }
4297     if (!arm_dc_feature(s, ARM_FEATURE_V6)) {
4298         mask &= ~(CPSR_E | CPSR_GE);
4299     }
4300     if (!arm_dc_feature(s, ARM_FEATURE_THUMB2)) {
4301         mask &= ~CPSR_IT;
4302     }
4303     /* Mask out execution state and reserved bits.  */
4304     if (!spsr) {
4305         mask &= ~(CPSR_EXEC | CPSR_RESERVED);
4306     }
4307     /* Mask out privileged bits.  */
4308     if (IS_USER(s))
4309         mask &= CPSR_USER;
4310     return mask;
4311 }
4312
4313 /* Returns nonzero if access to the PSR is not permitted. Marks t0 as dead. */
4314 static int gen_set_psr(DisasContext *s, uint32_t mask, int spsr, TCGv_i32 t0)
4315 {
4316     TCGv_i32 tmp;
4317     if (spsr) {
4318         /* ??? This is also undefined in system mode.  */
4319         if (IS_USER(s))
4320             return 1;
4321
4322         tmp = load_cpu_field(spsr);
4323         tcg_gen_andi_i32(tmp, tmp, ~mask);
4324         tcg_gen_andi_i32(t0, t0, mask);
4325         tcg_gen_or_i32(tmp, tmp, t0);
4326         store_cpu_field(tmp, spsr);
4327     } else {
4328         gen_set_cpsr(t0, mask);
4329     }
4330     tcg_temp_free_i32(t0);
4331     gen_lookup_tb(s);
4332     return 0;
4333 }
4334
4335 /* Returns nonzero if access to the PSR is not permitted.  */
4336 static int gen_set_psr_im(DisasContext *s, uint32_t mask, int spsr, uint32_t val)
4337 {
4338     TCGv_i32 tmp;
4339     tmp = tcg_temp_new_i32();
4340     tcg_gen_movi_i32(tmp, val);
4341     return gen_set_psr(s, mask, spsr, tmp);
4342 }
4343
4344 static bool msr_banked_access_decode(DisasContext *s, int r, int sysm, int rn,
4345                                      int *tgtmode, int *regno)
4346 {
4347     /* Decode the r and sysm fields of MSR/MRS banked accesses into
4348      * the target mode and register number, and identify the various
4349      * unpredictable cases.
4350      * MSR (banked) and MRS (banked) are CONSTRAINED UNPREDICTABLE if:
4351      *  + executed in user mode
4352      *  + using R15 as the src/dest register
4353      *  + accessing an unimplemented register
4354      *  + accessing a register that's inaccessible at current PL/security state*
4355      *  + accessing a register that you could access with a different insn
4356      * We choose to UNDEF in all these cases.
4357      * Since we don't know which of the various AArch32 modes we are in
4358      * we have to defer some checks to runtime.
4359      * Accesses to Monitor mode registers from Secure EL1 (which implies
4360      * that EL3 is AArch64) must trap to EL3.
4361      *
4362      * If the access checks fail this function will emit code to take
4363      * an exception and return false. Otherwise it will return true,
4364      * and set *tgtmode and *regno appropriately.
4365      */
4366     int exc_target = default_exception_el(s);
4367
4368     /* These instructions are present only in ARMv8, or in ARMv7 with the
4369      * Virtualization Extensions.
4370      */
4371     if (!arm_dc_feature(s, ARM_FEATURE_V8) &&
4372         !arm_dc_feature(s, ARM_FEATURE_EL2)) {
4373         goto undef;
4374     }
4375
4376     if (IS_USER(s) || rn == 15) {
4377         goto undef;
4378     }
4379
4380     /* The table in the v8 ARM ARM section F5.2.3 describes the encoding
4381      * of registers into (r, sysm).
4382      */
4383     if (r) {
4384         /* SPSRs for other modes */
4385         switch (sysm) {
4386         case 0xe: /* SPSR_fiq */
4387             *tgtmode = ARM_CPU_MODE_FIQ;
4388             break;
4389         case 0x10: /* SPSR_irq */
4390             *tgtmode = ARM_CPU_MODE_IRQ;
4391             break;
4392         case 0x12: /* SPSR_svc */
4393             *tgtmode = ARM_CPU_MODE_SVC;
4394             break;
4395         case 0x14: /* SPSR_abt */
4396             *tgtmode = ARM_CPU_MODE_ABT;
4397             break;
4398         case 0x16: /* SPSR_und */
4399             *tgtmode = ARM_CPU_MODE_UND;
4400             break;
4401         case 0x1c: /* SPSR_mon */
4402             *tgtmode = ARM_CPU_MODE_MON;
4403             break;
4404         case 0x1e: /* SPSR_hyp */
4405             *tgtmode = ARM_CPU_MODE_HYP;
4406             break;
4407         default: /* unallocated */
4408             goto undef;
4409         }
4410         /* We arbitrarily assign SPSR a register number of 16. */
4411         *regno = 16;
4412     } else {
4413         /* general purpose registers for other modes */
4414         switch (sysm) {
4415         case 0x0 ... 0x6:   /* 0b00xxx : r8_usr ... r14_usr */
4416             *tgtmode = ARM_CPU_MODE_USR;
4417             *regno = sysm + 8;
4418             break;
4419         case 0x8 ... 0xe:   /* 0b01xxx : r8_fiq ... r14_fiq */
4420             *tgtmode = ARM_CPU_MODE_FIQ;
4421             *regno = sysm;
4422             break;
4423         case 0x10 ... 0x11: /* 0b1000x : r14_irq, r13_irq */
4424             *tgtmode = ARM_CPU_MODE_IRQ;
4425             *regno = sysm & 1 ? 13 : 14;
4426             break;
4427         case 0x12 ... 0x13: /* 0b1001x : r14_svc, r13_svc */
4428             *tgtmode = ARM_CPU_MODE_SVC;
4429             *regno = sysm & 1 ? 13 : 14;
4430             break;
4431         case 0x14 ... 0x15: /* 0b1010x : r14_abt, r13_abt */
4432             *tgtmode = ARM_CPU_MODE_ABT;
4433             *regno = sysm & 1 ? 13 : 14;
4434             break;
4435         case 0x16 ... 0x17: /* 0b1011x : r14_und, r13_und */
4436             *tgtmode = ARM_CPU_MODE_UND;
4437             *regno = sysm & 1 ? 13 : 14;
4438             break;
4439         case 0x1c ... 0x1d: /* 0b1110x : r14_mon, r13_mon */
4440             *tgtmode = ARM_CPU_MODE_MON;
4441             *regno = sysm & 1 ? 13 : 14;
4442             break;
4443         case 0x1e ... 0x1f: /* 0b1111x : elr_hyp, r13_hyp */
4444             *tgtmode = ARM_CPU_MODE_HYP;
4445             /* Arbitrarily pick 17 for ELR_Hyp (which is not a banked LR!) */
4446             *regno = sysm & 1 ? 13 : 17;
4447             break;
4448         default: /* unallocated */
4449             goto undef;
4450         }
4451     }
4452
4453     /* Catch the 'accessing inaccessible register' cases we can detect
4454      * at translate time.
4455      */
4456     switch (*tgtmode) {
4457     case ARM_CPU_MODE_MON:
4458         if (!arm_dc_feature(s, ARM_FEATURE_EL3) || s->ns) {
4459             goto undef;
4460         }
4461         if (s->current_el == 1) {
4462             /* If we're in Secure EL1 (which implies that EL3 is AArch64)
4463              * then accesses to Mon registers trap to EL3
4464              */
4465             exc_target = 3;
4466             goto undef;
4467         }
4468         break;
4469     case ARM_CPU_MODE_HYP:
4470         /* Note that we can forbid accesses from EL2 here because they
4471          * must be from Hyp mode itself
4472          */
4473         if (!arm_dc_feature(s, ARM_FEATURE_EL2) || s->current_el < 3) {
4474             goto undef;
4475         }
4476         break;
4477     default:
4478         break;
4479     }
4480
4481     return true;
4482
4483 undef:
4484     /* If we get here then some access check did not pass */
4485     gen_exception_insn(s, 4, EXCP_UDEF, syn_uncategorized(), exc_target);
4486     return false;
4487 }
4488
4489 static void gen_msr_banked(DisasContext *s, int r, int sysm, int rn)
4490 {
4491     TCGv_i32 tcg_reg, tcg_tgtmode, tcg_regno;
4492     int tgtmode = 0, regno = 0;
4493
4494     if (!msr_banked_access_decode(s, r, sysm, rn, &tgtmode, &regno)) {
4495         return;
4496     }
4497
4498     /* Sync state because msr_banked() can raise exceptions */
4499     gen_set_condexec(s);
4500     gen_set_pc_im(s, s->pc - 4);
4501     tcg_reg = load_reg(s, rn);
4502     tcg_tgtmode = tcg_const_i32(tgtmode);
4503     tcg_regno = tcg_const_i32(regno);
4504     gen_helper_msr_banked(cpu_env, tcg_reg, tcg_tgtmode, tcg_regno);
4505     tcg_temp_free_i32(tcg_tgtmode);
4506     tcg_temp_free_i32(tcg_regno);
4507     tcg_temp_free_i32(tcg_reg);
4508     s->base.is_jmp = DISAS_UPDATE;
4509 }
4510
4511 static void gen_mrs_banked(DisasContext *s, int r, int sysm, int rn)
4512 {
4513     TCGv_i32 tcg_reg, tcg_tgtmode, tcg_regno;
4514     int tgtmode = 0, regno = 0;
4515
4516     if (!msr_banked_access_decode(s, r, sysm, rn, &tgtmode, &regno)) {
4517         return;
4518     }
4519
4520     /* Sync state because mrs_banked() can raise exceptions */
4521     gen_set_condexec(s);
4522     gen_set_pc_im(s, s->pc - 4);
4523     tcg_reg = tcg_temp_new_i32();
4524     tcg_tgtmode = tcg_const_i32(tgtmode);
4525     tcg_regno = tcg_const_i32(regno);
4526     gen_helper_mrs_banked(tcg_reg, cpu_env, tcg_tgtmode, tcg_regno);
4527     tcg_temp_free_i32(tcg_tgtmode);
4528     tcg_temp_free_i32(tcg_regno);
4529     store_reg(s, rn, tcg_reg);
4530     s->base.is_jmp = DISAS_UPDATE;
4531 }
4532
4533 /* Store value to PC as for an exception return (ie don't
4534  * mask bits). The subsequent call to gen_helper_cpsr_write_eret()
4535  * will do the masking based on the new value of the Thumb bit.
4536  */
4537 static void store_pc_exc_ret(DisasContext *s, TCGv_i32 pc)
4538 {
4539     tcg_gen_mov_i32(cpu_R[15], pc);
4540     tcg_temp_free_i32(pc);
4541 }
4542
4543 /* Generate a v6 exception return.  Marks both values as dead.  */
4544 static void gen_rfe(DisasContext *s, TCGv_i32 pc, TCGv_i32 cpsr)
4545 {
4546     store_pc_exc_ret(s, pc);
4547     /* The cpsr_write_eret helper will mask the low bits of PC
4548      * appropriately depending on the new Thumb bit, so it must
4549      * be called after storing the new PC.
4550      */
4551     if (tb_cflags(s->base.tb) & CF_USE_ICOUNT) {
4552         gen_io_start();
4553     }
4554     gen_helper_cpsr_write_eret(cpu_env, cpsr);
4555     if (tb_cflags(s->base.tb) & CF_USE_ICOUNT) {
4556         gen_io_end();
4557     }
4558     tcg_temp_free_i32(cpsr);
4559     /* Must exit loop to check un-masked IRQs */
4560     s->base.is_jmp = DISAS_EXIT;
4561 }
4562
4563 /* Generate an old-style exception return. Marks pc as dead. */
4564 static void gen_exception_return(DisasContext *s, TCGv_i32 pc)
4565 {
4566     gen_rfe(s, pc, load_cpu_field(spsr));
4567 }
4568
4569 /*
4570  * For WFI we will halt the vCPU until an IRQ. For WFE and YIELD we
4571  * only call the helper when running single threaded TCG code to ensure
4572  * the next round-robin scheduled vCPU gets a crack. In MTTCG mode we
4573  * just skip this instruction. Currently the SEV/SEVL instructions
4574  * which are *one* of many ways to wake the CPU from WFE are not
4575  * implemented so we can't sleep like WFI does.
4576  */
4577 static void gen_nop_hint(DisasContext *s, int val)
4578 {
4579     switch (val) {
4580         /* When running in MTTCG we don't generate jumps to the yield and
4581          * WFE helpers as it won't affect the scheduling of other vCPUs.
4582          * If we wanted to more completely model WFE/SEV so we don't busy
4583          * spin unnecessarily we would need to do something more involved.
4584          */
4585     case 1: /* yield */
4586         if (!(tb_cflags(s->base.tb) & CF_PARALLEL)) {
4587             gen_set_pc_im(s, s->pc);
4588             s->base.is_jmp = DISAS_YIELD;
4589         }
4590         break;
4591     case 3: /* wfi */
4592         gen_set_pc_im(s, s->pc);
4593         s->base.is_jmp = DISAS_WFI;
4594         break;
4595     case 2: /* wfe */
4596         if (!(tb_cflags(s->base.tb) & CF_PARALLEL)) {
4597             gen_set_pc_im(s, s->pc);
4598             s->base.is_jmp = DISAS_WFE;
4599         }
4600         break;
4601     case 4: /* sev */
4602     case 5: /* sevl */
4603         /* TODO: Implement SEV, SEVL and WFE.  May help SMP performance.  */
4604     default: /* nop */
4605         break;
4606     }
4607 }
4608
4609 #define CPU_V001 cpu_V0, cpu_V0, cpu_V1
4610
4611 static inline void gen_neon_add(int size, TCGv_i32 t0, TCGv_i32 t1)
4612 {
4613     switch (size) {
4614     case 0: gen_helper_neon_add_u8(t0, t0, t1); break;
4615     case 1: gen_helper_neon_add_u16(t0, t0, t1); break;
4616     case 2: tcg_gen_add_i32(t0, t0, t1); break;
4617     default: abort();
4618     }
4619 }
4620
4621 static inline void gen_neon_rsb(int size, TCGv_i32 t0, TCGv_i32 t1)
4622 {
4623     switch (size) {
4624     case 0: gen_helper_neon_sub_u8(t0, t1, t0); break;
4625     case 1: gen_helper_neon_sub_u16(t0, t1, t0); break;
4626     case 2: tcg_gen_sub_i32(t0, t1, t0); break;
4627     default: return;
4628     }
4629 }
4630
4631 /* 32-bit pairwise ops end up the same as the elementwise versions.  */
4632 #define gen_helper_neon_pmax_s32  gen_helper_neon_max_s32
4633 #define gen_helper_neon_pmax_u32  gen_helper_neon_max_u32
4634 #define gen_helper_neon_pmin_s32  gen_helper_neon_min_s32
4635 #define gen_helper_neon_pmin_u32  gen_helper_neon_min_u32
4636
4637 #define GEN_NEON_INTEGER_OP_ENV(name) do { \
4638     switch ((size << 1) | u) { \
4639     case 0: \
4640         gen_helper_neon_##name##_s8(tmp, cpu_env, tmp, tmp2); \
4641         break; \
4642     case 1: \
4643         gen_helper_neon_##name##_u8(tmp, cpu_env, tmp, tmp2); \
4644         break; \
4645     case 2: \
4646         gen_helper_neon_##name##_s16(tmp, cpu_env, tmp, tmp2); \
4647         break; \
4648     case 3: \
4649         gen_helper_neon_##name##_u16(tmp, cpu_env, tmp, tmp2); \
4650         break; \
4651     case 4: \
4652         gen_helper_neon_##name##_s32(tmp, cpu_env, tmp, tmp2); \
4653         break; \
4654     case 5: \
4655         gen_helper_neon_##name##_u32(tmp, cpu_env, tmp, tmp2); \
4656         break; \
4657     default: return 1; \
4658     }} while (0)
4659
4660 #define GEN_NEON_INTEGER_OP(name) do { \
4661     switch ((size << 1) | u) { \
4662     case 0: \
4663         gen_helper_neon_##name##_s8(tmp, tmp, tmp2); \
4664         break; \
4665     case 1: \
4666         gen_helper_neon_##name##_u8(tmp, tmp, tmp2); \
4667         break; \
4668     case 2: \
4669         gen_helper_neon_##name##_s16(tmp, tmp, tmp2); \
4670         break; \
4671     case 3: \
4672         gen_helper_neon_##name##_u16(tmp, tmp, tmp2); \
4673         break; \
4674     case 4: \
4675         gen_helper_neon_##name##_s32(tmp, tmp, tmp2); \
4676         break; \
4677     case 5: \
4678         gen_helper_neon_##name##_u32(tmp, tmp, tmp2); \
4679         break; \
4680     default: return 1; \
4681     }} while (0)
4682
4683 static TCGv_i32 neon_load_scratch(int scratch)
4684 {
4685     TCGv_i32 tmp = tcg_temp_new_i32();
4686     tcg_gen_ld_i32(tmp, cpu_env, offsetof(CPUARMState, vfp.scratch[scratch]));
4687     return tmp;
4688 }
4689
4690 static void neon_store_scratch(int scratch, TCGv_i32 var)
4691 {
4692     tcg_gen_st_i32(var, cpu_env, offsetof(CPUARMState, vfp.scratch[scratch]));
4693     tcg_temp_free_i32(var);
4694 }
4695
4696 static inline TCGv_i32 neon_get_scalar(int size, int reg)
4697 {
4698     TCGv_i32 tmp;
4699     if (size == 1) {
4700         tmp = neon_load_reg(reg & 7, reg >> 4);
4701         if (reg & 8) {
4702             gen_neon_dup_high16(tmp);
4703         } else {
4704             gen_neon_dup_low16(tmp);
4705         }
4706     } else {
4707         tmp = neon_load_reg(reg & 15, reg >> 4);
4708     }
4709     return tmp;
4710 }
4711
4712 static int gen_neon_unzip(int rd, int rm, int size, int q)
4713 {
4714     TCGv_ptr pd, pm;
4715     
4716     if (!q && size == 2) {
4717         return 1;
4718     }
4719     pd = vfp_reg_ptr(true, rd);
4720     pm = vfp_reg_ptr(true, rm);
4721     if (q) {
4722         switch (size) {
4723         case 0:
4724             gen_helper_neon_qunzip8(pd, pm);
4725             break;
4726         case 1:
4727             gen_helper_neon_qunzip16(pd, pm);
4728             break;
4729         case 2:
4730             gen_helper_neon_qunzip32(pd, pm);
4731             break;
4732         default:
4733             abort();
4734         }
4735     } else {
4736         switch (size) {
4737         case 0:
4738             gen_helper_neon_unzip8(pd, pm);
4739             break;
4740         case 1:
4741             gen_helper_neon_unzip16(pd, pm);
4742             break;
4743         default:
4744             abort();
4745         }
4746     }
4747     tcg_temp_free_ptr(pd);
4748     tcg_temp_free_ptr(pm);
4749     return 0;
4750 }
4751
4752 static int gen_neon_zip(int rd, int rm, int size, int q)
4753 {
4754     TCGv_ptr pd, pm;
4755
4756     if (!q && size == 2) {
4757         return 1;
4758     }
4759     pd = vfp_reg_ptr(true, rd);
4760     pm = vfp_reg_ptr(true, rm);
4761     if (q) {
4762         switch (size) {
4763         case 0:
4764             gen_helper_neon_qzip8(pd, pm);
4765             break;
4766         case 1:
4767             gen_helper_neon_qzip16(pd, pm);
4768             break;
4769         case 2:
4770             gen_helper_neon_qzip32(pd, pm);
4771             break;
4772         default:
4773             abort();
4774         }
4775     } else {
4776         switch (size) {
4777         case 0:
4778             gen_helper_neon_zip8(pd, pm);
4779             break;
4780         case 1:
4781             gen_helper_neon_zip16(pd, pm);
4782             break;
4783         default:
4784             abort();
4785         }
4786     }
4787     tcg_temp_free_ptr(pd);
4788     tcg_temp_free_ptr(pm);
4789     return 0;
4790 }
4791
4792 static void gen_neon_trn_u8(TCGv_i32 t0, TCGv_i32 t1)
4793 {
4794     TCGv_i32 rd, tmp;
4795
4796     rd = tcg_temp_new_i32();
4797     tmp = tcg_temp_new_i32();
4798
4799     tcg_gen_shli_i32(rd, t0, 8);
4800     tcg_gen_andi_i32(rd, rd, 0xff00ff00);
4801     tcg_gen_andi_i32(tmp, t1, 0x00ff00ff);
4802     tcg_gen_or_i32(rd, rd, tmp);
4803
4804     tcg_gen_shri_i32(t1, t1, 8);
4805     tcg_gen_andi_i32(t1, t1, 0x00ff00ff);
4806     tcg_gen_andi_i32(tmp, t0, 0xff00ff00);
4807     tcg_gen_or_i32(t1, t1, tmp);
4808     tcg_gen_mov_i32(t0, rd);
4809
4810     tcg_temp_free_i32(tmp);
4811     tcg_temp_free_i32(rd);
4812 }
4813
4814 static void gen_neon_trn_u16(TCGv_i32 t0, TCGv_i32 t1)
4815 {
4816     TCGv_i32 rd, tmp;
4817
4818     rd = tcg_temp_new_i32();
4819     tmp = tcg_temp_new_i32();
4820
4821     tcg_gen_shli_i32(rd, t0, 16);
4822     tcg_gen_andi_i32(tmp, t1, 0xffff);
4823     tcg_gen_or_i32(rd, rd, tmp);
4824     tcg_gen_shri_i32(t1, t1, 16);
4825     tcg_gen_andi_i32(tmp, t0, 0xffff0000);
4826     tcg_gen_or_i32(t1, t1, tmp);
4827     tcg_gen_mov_i32(t0, rd);
4828
4829     tcg_temp_free_i32(tmp);
4830     tcg_temp_free_i32(rd);
4831 }
4832
4833
4834 static struct {
4835     int nregs;
4836     int interleave;
4837     int spacing;
4838 } neon_ls_element_type[11] = {
4839     {4, 4, 1},
4840     {4, 4, 2},
4841     {4, 1, 1},
4842     {4, 2, 1},
4843     {3, 3, 1},
4844     {3, 3, 2},
4845     {3, 1, 1},
4846     {1, 1, 1},
4847     {2, 2, 1},
4848     {2, 2, 2},
4849     {2, 1, 1}
4850 };
4851
4852 /* Translate a NEON load/store element instruction.  Return nonzero if the
4853    instruction is invalid.  */
4854 static int disas_neon_ls_insn(DisasContext *s, uint32_t insn)
4855 {
4856     int rd, rn, rm;
4857     int op;
4858     int nregs;
4859     int interleave;
4860     int spacing;
4861     int stride;
4862     int size;
4863     int reg;
4864     int pass;
4865     int load;
4866     int shift;
4867     int n;
4868     TCGv_i32 addr;
4869     TCGv_i32 tmp;
4870     TCGv_i32 tmp2;
4871     TCGv_i64 tmp64;
4872
4873     /* FIXME: this access check should not take precedence over UNDEF
4874      * for invalid encodings; we will generate incorrect syndrome information
4875      * for attempts to execute invalid vfp/neon encodings with FP disabled.
4876      */
4877     if (s->fp_excp_el) {
4878         gen_exception_insn(s, 4, EXCP_UDEF,
4879                            syn_fp_access_trap(1, 0xe, false), s->fp_excp_el);
4880         return 0;
4881     }
4882
4883     if (!s->vfp_enabled)
4884       return 1;
4885     VFP_DREG_D(rd, insn);
4886     rn = (insn >> 16) & 0xf;
4887     rm = insn & 0xf;
4888     load = (insn & (1 << 21)) != 0;
4889     if ((insn & (1 << 23)) == 0) {
4890         /* Load store all elements.  */
4891         op = (insn >> 8) & 0xf;
4892         size = (insn >> 6) & 3;
4893         if (op > 10)
4894             return 1;
4895         /* Catch UNDEF cases for bad values of align field */
4896         switch (op & 0xc) {
4897         case 4:
4898             if (((insn >> 5) & 1) == 1) {
4899                 return 1;
4900             }
4901             break;
4902         case 8:
4903             if (((insn >> 4) & 3) == 3) {
4904                 return 1;
4905             }
4906             break;
4907         default:
4908             break;
4909         }
4910         nregs = neon_ls_element_type[op].nregs;
4911         interleave = neon_ls_element_type[op].interleave;
4912         spacing = neon_ls_element_type[op].spacing;
4913         if (size == 3 && (interleave | spacing) != 1)
4914             return 1;
4915         addr = tcg_temp_new_i32();
4916         load_reg_var(s, addr, rn);
4917         stride = (1 << size) * interleave;
4918         for (reg = 0; reg < nregs; reg++) {
4919             if (interleave > 2 || (interleave == 2 && nregs == 2)) {
4920                 load_reg_var(s, addr, rn);
4921                 tcg_gen_addi_i32(addr, addr, (1 << size) * reg);
4922             } else if (interleave == 2 && nregs == 4 && reg == 2) {
4923                 load_reg_var(s, addr, rn);
4924                 tcg_gen_addi_i32(addr, addr, 1 << size);
4925             }
4926             if (size == 3) {
4927                 tmp64 = tcg_temp_new_i64();
4928                 if (load) {
4929                     gen_aa32_ld64(s, tmp64, addr, get_mem_index(s));
4930                     neon_store_reg64(tmp64, rd);
4931                 } else {
4932                     neon_load_reg64(tmp64, rd);
4933                     gen_aa32_st64(s, tmp64, addr, get_mem_index(s));
4934                 }
4935                 tcg_temp_free_i64(tmp64);
4936                 tcg_gen_addi_i32(addr, addr, stride);
4937             } else {
4938                 for (pass = 0; pass < 2; pass++) {
4939                     if (size == 2) {
4940                         if (load) {
4941                             tmp = tcg_temp_new_i32();
4942                             gen_aa32_ld32u(s, tmp, addr, get_mem_index(s));
4943                             neon_store_reg(rd, pass, tmp);
4944                         } else {
4945                             tmp = neon_load_reg(rd, pass);
4946                             gen_aa32_st32(s, tmp, addr, get_mem_index(s));
4947                             tcg_temp_free_i32(tmp);
4948                         }
4949                         tcg_gen_addi_i32(addr, addr, stride);
4950                     } else if (size == 1) {
4951                         if (load) {
4952                             tmp = tcg_temp_new_i32();
4953                             gen_aa32_ld16u(s, tmp, addr, get_mem_index(s));
4954                             tcg_gen_addi_i32(addr, addr, stride);
4955                             tmp2 = tcg_temp_new_i32();
4956                             gen_aa32_ld16u(s, tmp2, addr, get_mem_index(s));
4957                             tcg_gen_addi_i32(addr, addr, stride);
4958                             tcg_gen_shli_i32(tmp2, tmp2, 16);
4959                             tcg_gen_or_i32(tmp, tmp, tmp2);
4960                             tcg_temp_free_i32(tmp2);
4961                             neon_store_reg(rd, pass, tmp);
4962                         } else {
4963                             tmp = neon_load_reg(rd, pass);
4964                             tmp2 = tcg_temp_new_i32();
4965                             tcg_gen_shri_i32(tmp2, tmp, 16);
4966                             gen_aa32_st16(s, tmp, addr, get_mem_index(s));
4967                             tcg_temp_free_i32(tmp);
4968                             tcg_gen_addi_i32(addr, addr, stride);
4969                             gen_aa32_st16(s, tmp2, addr, get_mem_index(s));
4970                             tcg_temp_free_i32(tmp2);
4971                             tcg_gen_addi_i32(addr, addr, stride);
4972                         }
4973                     } else /* size == 0 */ {
4974                         if (load) {
4975                             tmp2 = NULL;
4976                             for (n = 0; n < 4; n++) {
4977                                 tmp = tcg_temp_new_i32();
4978                                 gen_aa32_ld8u(s, tmp, addr, get_mem_index(s));
4979                                 tcg_gen_addi_i32(addr, addr, stride);
4980                                 if (n == 0) {
4981                                     tmp2 = tmp;
4982                                 } else {
4983                                     tcg_gen_shli_i32(tmp, tmp, n * 8);
4984                                     tcg_gen_or_i32(tmp2, tmp2, tmp);
4985                                     tcg_temp_free_i32(tmp);
4986                                 }
4987                             }
4988                             neon_store_reg(rd, pass, tmp2);
4989                         } else {
4990                             tmp2 = neon_load_reg(rd, pass);
4991                             for (n = 0; n < 4; n++) {
4992                                 tmp = tcg_temp_new_i32();
4993                                 if (n == 0) {
4994                                     tcg_gen_mov_i32(tmp, tmp2);
4995                                 } else {
4996                                     tcg_gen_shri_i32(tmp, tmp2, n * 8);
4997                                 }
4998                                 gen_aa32_st8(s, tmp, addr, get_mem_index(s));
4999                                 tcg_temp_free_i32(tmp);
5000                                 tcg_gen_addi_i32(addr, addr, stride);
5001                             }
5002                             tcg_temp_free_i32(tmp2);
5003                         }
5004                     }
5005                 }
5006             }
5007             rd += spacing;
5008         }
5009         tcg_temp_free_i32(addr);
5010         stride = nregs * 8;
5011     } else {
5012         size = (insn >> 10) & 3;
5013         if (size == 3) {
5014             /* Load single element to all lanes.  */
5015             int a = (insn >> 4) & 1;
5016             if (!load) {
5017                 return 1;
5018             }
5019             size = (insn >> 6) & 3;
5020             nregs = ((insn >> 8) & 3) + 1;
5021
5022             if (size == 3) {
5023                 if (nregs != 4 || a == 0) {
5024                     return 1;
5025                 }
5026                 /* For VLD4 size==3 a == 1 means 32 bits at 16 byte alignment */
5027                 size = 2;
5028             }
5029             if (nregs == 1 && a == 1 && size == 0) {
5030                 return 1;
5031             }
5032             if (nregs == 3 && a == 1) {
5033                 return 1;
5034             }
5035             addr = tcg_temp_new_i32();
5036             load_reg_var(s, addr, rn);
5037             if (nregs == 1) {
5038                 /* VLD1 to all lanes: bit 5 indicates how many Dregs to write */
5039                 tmp = gen_load_and_replicate(s, addr, size);
5040                 tcg_gen_st_i32(tmp, cpu_env, neon_reg_offset(rd, 0));
5041                 tcg_gen_st_i32(tmp, cpu_env, neon_reg_offset(rd, 1));
5042                 if (insn & (1 << 5)) {
5043                     tcg_gen_st_i32(tmp, cpu_env, neon_reg_offset(rd + 1, 0));
5044                     tcg_gen_st_i32(tmp, cpu_env, neon_reg_offset(rd + 1, 1));
5045                 }
5046                 tcg_temp_free_i32(tmp);
5047             } else {
5048                 /* VLD2/3/4 to all lanes: bit 5 indicates register stride */
5049                 stride = (insn & (1 << 5)) ? 2 : 1;
5050                 for (reg = 0; reg < nregs; reg++) {
5051                     tmp = gen_load_and_replicate(s, addr, size);
5052                     tcg_gen_st_i32(tmp, cpu_env, neon_reg_offset(rd, 0));
5053                     tcg_gen_st_i32(tmp, cpu_env, neon_reg_offset(rd, 1));
5054                     tcg_temp_free_i32(tmp);
5055                     tcg_gen_addi_i32(addr, addr, 1 << size);
5056                     rd += stride;
5057                 }
5058             }
5059             tcg_temp_free_i32(addr);
5060             stride = (1 << size) * nregs;
5061         } else {
5062             /* Single element.  */
5063             int idx = (insn >> 4) & 0xf;
5064             pass = (insn >> 7) & 1;
5065             switch (size) {
5066             case 0:
5067                 shift = ((insn >> 5) & 3) * 8;
5068                 stride = 1;
5069                 break;
5070             case 1:
5071                 shift = ((insn >> 6) & 1) * 16;
5072                 stride = (insn & (1 << 5)) ? 2 : 1;
5073                 break;
5074             case 2:
5075                 shift = 0;
5076                 stride = (insn & (1 << 6)) ? 2 : 1;
5077                 break;
5078             default:
5079                 abort();
5080             }
5081             nregs = ((insn >> 8) & 3) + 1;
5082             /* Catch the UNDEF cases. This is unavoidably a bit messy. */
5083             switch (nregs) {
5084             case 1:
5085                 if (((idx & (1 << size)) != 0) ||
5086                     (size == 2 && ((idx & 3) == 1 || (idx & 3) == 2))) {
5087                     return 1;
5088                 }
5089                 break;
5090             case 3:
5091                 if ((idx & 1) != 0) {
5092                     return 1;
5093                 }
5094                 /* fall through */
5095             case 2:
5096                 if (size == 2 && (idx & 2) != 0) {
5097                     return 1;
5098                 }
5099                 break;
5100             case 4:
5101                 if ((size == 2) && ((idx & 3) == 3)) {
5102                     return 1;
5103                 }
5104                 break;
5105             default:
5106                 abort();
5107             }
5108             if ((rd + stride * (nregs - 1)) > 31) {
5109                 /* Attempts to write off the end of the register file
5110                  * are UNPREDICTABLE; we choose to UNDEF because otherwise
5111                  * the neon_load_reg() would write off the end of the array.
5112                  */
5113                 return 1;
5114             }
5115             addr = tcg_temp_new_i32();
5116             load_reg_var(s, addr, rn);
5117             for (reg = 0; reg < nregs; reg++) {
5118                 if (load) {
5119                     tmp = tcg_temp_new_i32();
5120                     switch (size) {
5121                     case 0:
5122                         gen_aa32_ld8u(s, tmp, addr, get_mem_index(s));
5123                         break;
5124                     case 1:
5125                         gen_aa32_ld16u(s, tmp, addr, get_mem_index(s));
5126                         break;
5127                     case 2:
5128                         gen_aa32_ld32u(s, tmp, addr, get_mem_index(s));
5129                         break;
5130                     default: /* Avoid compiler warnings.  */
5131                         abort();
5132                     }
5133                     if (size != 2) {
5134                         tmp2 = neon_load_reg(rd, pass);
5135                         tcg_gen_deposit_i32(tmp, tmp2, tmp,
5136                                             shift, size ? 16 : 8);
5137                         tcg_temp_free_i32(tmp2);
5138                     }
5139                     neon_store_reg(rd, pass, tmp);
5140                 } else { /* Store */
5141                     tmp = neon_load_reg(rd, pass);
5142                     if (shift)
5143                         tcg_gen_shri_i32(tmp, tmp, shift);
5144                     switch (size) {
5145                     case 0:
5146                         gen_aa32_st8(s, tmp, addr, get_mem_index(s));
5147                         break;
5148                     case 1:
5149                         gen_aa32_st16(s, tmp, addr, get_mem_index(s));
5150                         break;
5151                     case 2:
5152                         gen_aa32_st32(s, tmp, addr, get_mem_index(s));
5153                         break;
5154                     }
5155                     tcg_temp_free_i32(tmp);
5156                 }
5157                 rd += stride;
5158                 tcg_gen_addi_i32(addr, addr, 1 << size);
5159             }
5160             tcg_temp_free_i32(addr);
5161             stride = nregs * (1 << size);
5162         }
5163     }
5164     if (rm != 15) {
5165         TCGv_i32 base;
5166
5167         base = load_reg(s, rn);
5168         if (rm == 13) {
5169             tcg_gen_addi_i32(base, base, stride);
5170         } else {
5171             TCGv_i32 index;
5172             index = load_reg(s, rm);
5173             tcg_gen_add_i32(base, base, index);
5174             tcg_temp_free_i32(index);
5175         }
5176         store_reg(s, rn, base);
5177     }
5178     return 0;
5179 }
5180
5181 /* Bitwise select.  dest = c ? t : f.  Clobbers T and F.  */
5182 static void gen_neon_bsl(TCGv_i32 dest, TCGv_i32 t, TCGv_i32 f, TCGv_i32 c)
5183 {
5184     tcg_gen_and_i32(t, t, c);
5185     tcg_gen_andc_i32(f, f, c);
5186     tcg_gen_or_i32(dest, t, f);
5187 }
5188
5189 static inline void gen_neon_narrow(int size, TCGv_i32 dest, TCGv_i64 src)
5190 {
5191     switch (size) {
5192     case 0: gen_helper_neon_narrow_u8(dest, src); break;
5193     case 1: gen_helper_neon_narrow_u16(dest, src); break;
5194     case 2: tcg_gen_extrl_i64_i32(dest, src); break;
5195     default: abort();
5196     }
5197 }
5198
5199 static inline void gen_neon_narrow_sats(int size, TCGv_i32 dest, TCGv_i64 src)
5200 {
5201     switch (size) {
5202     case 0: gen_helper_neon_narrow_sat_s8(dest, cpu_env, src); break;
5203     case 1: gen_helper_neon_narrow_sat_s16(dest, cpu_env, src); break;
5204     case 2: gen_helper_neon_narrow_sat_s32(dest, cpu_env, src); break;
5205     default: abort();
5206     }
5207 }
5208
5209 static inline void gen_neon_narrow_satu(int size, TCGv_i32 dest, TCGv_i64 src)
5210 {
5211     switch (size) {
5212     case 0: gen_helper_neon_narrow_sat_u8(dest, cpu_env, src); break;
5213     case 1: gen_helper_neon_narrow_sat_u16(dest, cpu_env, src); break;
5214     case 2: gen_helper_neon_narrow_sat_u32(dest, cpu_env, src); break;
5215     default: abort();
5216     }
5217 }
5218
5219 static inline void gen_neon_unarrow_sats(int size, TCGv_i32 dest, TCGv_i64 src)
5220 {
5221     switch (size) {
5222     case 0: gen_helper_neon_unarrow_sat8(dest, cpu_env, src); break;
5223     case 1: gen_helper_neon_unarrow_sat16(dest, cpu_env, src); break;
5224     case 2: gen_helper_neon_unarrow_sat32(dest, cpu_env, src); break;
5225     default: abort();
5226     }
5227 }
5228
5229 static inline void gen_neon_shift_narrow(int size, TCGv_i32 var, TCGv_i32 shift,
5230                                          int q, int u)
5231 {
5232     if (q) {
5233         if (u) {
5234             switch (size) {
5235             case 1: gen_helper_neon_rshl_u16(var, var, shift); break;
5236             case 2: gen_helper_neon_rshl_u32(var, var, shift); break;
5237             default: abort();
5238             }
5239         } else {
5240             switch (size) {
5241             case 1: gen_helper_neon_rshl_s16(var, var, shift); break;
5242             case 2: gen_helper_neon_rshl_s32(var, var, shift); break;
5243             default: abort();
5244             }
5245         }
5246     } else {
5247         if (u) {
5248             switch (size) {
5249             case 1: gen_helper_neon_shl_u16(var, var, shift); break;
5250             case 2: gen_helper_neon_shl_u32(var, var, shift); break;
5251             default: abort();
5252             }
5253         } else {
5254             switch (size) {
5255             case 1: gen_helper_neon_shl_s16(var, var, shift); break;
5256             case 2: gen_helper_neon_shl_s32(var, var, shift); break;
5257             default: abort();
5258             }
5259         }
5260     }
5261 }
5262
5263 static inline void gen_neon_widen(TCGv_i64 dest, TCGv_i32 src, int size, int u)
5264 {
5265     if (u) {
5266         switch (size) {
5267         case 0: gen_helper_neon_widen_u8(dest, src); break;
5268         case 1: gen_helper_neon_widen_u16(dest, src); break;
5269         case 2: tcg_gen_extu_i32_i64(dest, src); break;
5270         default: abort();
5271         }
5272     } else {
5273         switch (size) {
5274         case 0: gen_helper_neon_widen_s8(dest, src); break;
5275         case 1: gen_helper_neon_widen_s16(dest, src); break;
5276         case 2: tcg_gen_ext_i32_i64(dest, src); break;
5277         default: abort();
5278         }
5279     }
5280     tcg_temp_free_i32(src);
5281 }
5282
5283 static inline void gen_neon_addl(int size)
5284 {
5285     switch (size) {
5286     case 0: gen_helper_neon_addl_u16(CPU_V001); break;
5287     case 1: gen_helper_neon_addl_u32(CPU_V001); break;
5288     case 2: tcg_gen_add_i64(CPU_V001); break;
5289     default: abort();
5290     }
5291 }
5292
5293 static inline void gen_neon_subl(int size)
5294 {
5295     switch (size) {
5296     case 0: gen_helper_neon_subl_u16(CPU_V001); break;
5297     case 1: gen_helper_neon_subl_u32(CPU_V001); break;
5298     case 2: tcg_gen_sub_i64(CPU_V001); break;
5299     default: abort();
5300     }
5301 }
5302
5303 static inline void gen_neon_negl(TCGv_i64 var, int size)
5304 {
5305     switch (size) {
5306     case 0: gen_helper_neon_negl_u16(var, var); break;
5307     case 1: gen_helper_neon_negl_u32(var, var); break;
5308     case 2:
5309         tcg_gen_neg_i64(var, var);
5310         break;
5311     default: abort();
5312     }
5313 }
5314
5315 static inline void gen_neon_addl_saturate(TCGv_i64 op0, TCGv_i64 op1, int size)
5316 {
5317     switch (size) {
5318     case 1: gen_helper_neon_addl_saturate_s32(op0, cpu_env, op0, op1); break;
5319     case 2: gen_helper_neon_addl_saturate_s64(op0, cpu_env, op0, op1); break;
5320     default: abort();
5321     }
5322 }
5323
5324 static inline void gen_neon_mull(TCGv_i64 dest, TCGv_i32 a, TCGv_i32 b,
5325                                  int size, int u)
5326 {
5327     TCGv_i64 tmp;
5328
5329     switch ((size << 1) | u) {
5330     case 0: gen_helper_neon_mull_s8(dest, a, b); break;
5331     case 1: gen_helper_neon_mull_u8(dest, a, b); break;
5332     case 2: gen_helper_neon_mull_s16(dest, a, b); break;
5333     case 3: gen_helper_neon_mull_u16(dest, a, b); break;
5334     case 4:
5335         tmp = gen_muls_i64_i32(a, b);
5336         tcg_gen_mov_i64(dest, tmp);
5337         tcg_temp_free_i64(tmp);
5338         break;
5339     case 5:
5340         tmp = gen_mulu_i64_i32(a, b);
5341         tcg_gen_mov_i64(dest, tmp);
5342         tcg_temp_free_i64(tmp);
5343         break;
5344     default: abort();
5345     }
5346
5347     /* gen_helper_neon_mull_[su]{8|16} do not free their parameters.
5348        Don't forget to clean them now.  */
5349     if (size < 2) {
5350         tcg_temp_free_i32(a);
5351         tcg_temp_free_i32(b);
5352     }
5353 }
5354
5355 static void gen_neon_narrow_op(int op, int u, int size,
5356                                TCGv_i32 dest, TCGv_i64 src)
5357 {
5358     if (op) {
5359         if (u) {
5360             gen_neon_unarrow_sats(size, dest, src);
5361         } else {
5362             gen_neon_narrow(size, dest, src);
5363         }
5364     } else {
5365         if (u) {
5366             gen_neon_narrow_satu(size, dest, src);
5367         } else {
5368             gen_neon_narrow_sats(size, dest, src);
5369         }
5370     }
5371 }
5372
5373 /* Symbolic constants for op fields for Neon 3-register same-length.
5374  * The values correspond to bits [11:8,4]; see the ARM ARM DDI0406B
5375  * table A7-9.
5376  */
5377 #define NEON_3R_VHADD 0
5378 #define NEON_3R_VQADD 1
5379 #define NEON_3R_VRHADD 2
5380 #define NEON_3R_LOGIC 3 /* VAND,VBIC,VORR,VMOV,VORN,VEOR,VBIF,VBIT,VBSL */
5381 #define NEON_3R_VHSUB 4
5382 #define NEON_3R_VQSUB 5
5383 #define NEON_3R_VCGT 6
5384 #define NEON_3R_VCGE 7
5385 #define NEON_3R_VSHL 8
5386 #define NEON_3R_VQSHL 9
5387 #define NEON_3R_VRSHL 10
5388 #define NEON_3R_VQRSHL 11
5389 #define NEON_3R_VMAX 12
5390 #define NEON_3R_VMIN 13
5391 #define NEON_3R_VABD 14
5392 #define NEON_3R_VABA 15
5393 #define NEON_3R_VADD_VSUB 16
5394 #define NEON_3R_VTST_VCEQ 17
5395 #define NEON_3R_VML 18 /* VMLA, VMLAL, VMLS, VMLSL */
5396 #define NEON_3R_VMUL 19
5397 #define NEON_3R_VPMAX 20
5398 #define NEON_3R_VPMIN 21
5399 #define NEON_3R_VQDMULH_VQRDMULH 22
5400 #define NEON_3R_VPADD_VQRDMLAH 23
5401 #define NEON_3R_SHA 24 /* SHA1C,SHA1P,SHA1M,SHA1SU0,SHA256H{2},SHA256SU1 */
5402 #define NEON_3R_VFM_VQRDMLSH 25 /* VFMA, VFMS, VQRDMLSH */
5403 #define NEON_3R_FLOAT_ARITH 26 /* float VADD, VSUB, VPADD, VABD */
5404 #define NEON_3R_FLOAT_MULTIPLY 27 /* float VMLA, VMLS, VMUL */
5405 #define NEON_3R_FLOAT_CMP 28 /* float VCEQ, VCGE, VCGT */
5406 #define NEON_3R_FLOAT_ACMP 29 /* float VACGE, VACGT, VACLE, VACLT */
5407 #define NEON_3R_FLOAT_MINMAX 30 /* float VMIN, VMAX */
5408 #define NEON_3R_FLOAT_MISC 31 /* float VRECPS, VRSQRTS, VMAXNM/MINNM */
5409
5410 static const uint8_t neon_3r_sizes[] = {
5411     [NEON_3R_VHADD] = 0x7,
5412     [NEON_3R_VQADD] = 0xf,
5413     [NEON_3R_VRHADD] = 0x7,
5414     [NEON_3R_LOGIC] = 0xf, /* size field encodes op type */
5415     [NEON_3R_VHSUB] = 0x7,
5416     [NEON_3R_VQSUB] = 0xf,
5417     [NEON_3R_VCGT] = 0x7,
5418     [NEON_3R_VCGE] = 0x7,
5419     [NEON_3R_VSHL] = 0xf,
5420     [NEON_3R_VQSHL] = 0xf,
5421     [NEON_3R_VRSHL] = 0xf,
5422     [NEON_3R_VQRSHL] = 0xf,
5423     [NEON_3R_VMAX] = 0x7,
5424     [NEON_3R_VMIN] = 0x7,
5425     [NEON_3R_VABD] = 0x7,
5426     [NEON_3R_VABA] = 0x7,
5427     [NEON_3R_VADD_VSUB] = 0xf,
5428     [NEON_3R_VTST_VCEQ] = 0x7,
5429     [NEON_3R_VML] = 0x7,
5430     [NEON_3R_VMUL] = 0x7,
5431     [NEON_3R_VPMAX] = 0x7,
5432     [NEON_3R_VPMIN] = 0x7,
5433     [NEON_3R_VQDMULH_VQRDMULH] = 0x6,
5434     [NEON_3R_VPADD_VQRDMLAH] = 0x7,
5435     [NEON_3R_SHA] = 0xf, /* size field encodes op type */
5436     [NEON_3R_VFM_VQRDMLSH] = 0x7, /* For VFM, size bit 1 encodes op */
5437     [NEON_3R_FLOAT_ARITH] = 0x5, /* size bit 1 encodes op */
5438     [NEON_3R_FLOAT_MULTIPLY] = 0x5, /* size bit 1 encodes op */
5439     [NEON_3R_FLOAT_CMP] = 0x5, /* size bit 1 encodes op */
5440     [NEON_3R_FLOAT_ACMP] = 0x5, /* size bit 1 encodes op */
5441     [NEON_3R_FLOAT_MINMAX] = 0x5, /* size bit 1 encodes op */
5442     [NEON_3R_FLOAT_MISC] = 0x5, /* size bit 1 encodes op */
5443 };
5444
5445 /* Symbolic constants for op fields for Neon 2-register miscellaneous.
5446  * The values correspond to bits [17:16,10:7]; see the ARM ARM DDI0406B
5447  * table A7-13.
5448  */
5449 #define NEON_2RM_VREV64 0
5450 #define NEON_2RM_VREV32 1
5451 #define NEON_2RM_VREV16 2
5452 #define NEON_2RM_VPADDL 4
5453 #define NEON_2RM_VPADDL_U 5
5454 #define NEON_2RM_AESE 6 /* Includes AESD */
5455 #define NEON_2RM_AESMC 7 /* Includes AESIMC */
5456 #define NEON_2RM_VCLS 8
5457 #define NEON_2RM_VCLZ 9
5458 #define NEON_2RM_VCNT 10
5459 #define NEON_2RM_VMVN 11
5460 #define NEON_2RM_VPADAL 12
5461 #define NEON_2RM_VPADAL_U 13
5462 #define NEON_2RM_VQABS 14
5463 #define NEON_2RM_VQNEG 15
5464 #define NEON_2RM_VCGT0 16
5465 #define NEON_2RM_VCGE0 17
5466 #define NEON_2RM_VCEQ0 18
5467 #define NEON_2RM_VCLE0 19
5468 #define NEON_2RM_VCLT0 20
5469 #define NEON_2RM_SHA1H 21
5470 #define NEON_2RM_VABS 22
5471 #define NEON_2RM_VNEG 23
5472 #define NEON_2RM_VCGT0_F 24
5473 #define NEON_2RM_VCGE0_F 25
5474 #define NEON_2RM_VCEQ0_F 26
5475 #define NEON_2RM_VCLE0_F 27
5476 #define NEON_2RM_VCLT0_F 28
5477 #define NEON_2RM_VABS_F 30
5478 #define NEON_2RM_VNEG_F 31
5479 #define NEON_2RM_VSWP 32
5480 #define NEON_2RM_VTRN 33
5481 #define NEON_2RM_VUZP 34
5482 #define NEON_2RM_VZIP 35
5483 #define NEON_2RM_VMOVN 36 /* Includes VQMOVN, VQMOVUN */
5484 #define NEON_2RM_VQMOVN 37 /* Includes VQMOVUN */
5485 #define NEON_2RM_VSHLL 38
5486 #define NEON_2RM_SHA1SU1 39 /* Includes SHA256SU0 */
5487 #define NEON_2RM_VRINTN 40
5488 #define NEON_2RM_VRINTX 41
5489 #define NEON_2RM_VRINTA 42
5490 #define NEON_2RM_VRINTZ 43
5491 #define NEON_2RM_VCVT_F16_F32 44
5492 #define NEON_2RM_VRINTM 45
5493 #define NEON_2RM_VCVT_F32_F16 46
5494 #define NEON_2RM_VRINTP 47
5495 #define NEON_2RM_VCVTAU 48
5496 #define NEON_2RM_VCVTAS 49
5497 #define NEON_2RM_VCVTNU 50
5498 #define NEON_2RM_VCVTNS 51
5499 #define NEON_2RM_VCVTPU 52
5500 #define NEON_2RM_VCVTPS 53
5501 #define NEON_2RM_VCVTMU 54
5502 #define NEON_2RM_VCVTMS 55
5503 #define NEON_2RM_VRECPE 56
5504 #define NEON_2RM_VRSQRTE 57
5505 #define NEON_2RM_VRECPE_F 58
5506 #define NEON_2RM_VRSQRTE_F 59
5507 #define NEON_2RM_VCVT_FS 60
5508 #define NEON_2RM_VCVT_FU 61
5509 #define NEON_2RM_VCVT_SF 62
5510 #define NEON_2RM_VCVT_UF 63
5511
5512 static int neon_2rm_is_float_op(int op)
5513 {
5514     /* Return true if this neon 2reg-misc op is float-to-float */
5515     return (op == NEON_2RM_VABS_F || op == NEON_2RM_VNEG_F ||
5516             (op >= NEON_2RM_VRINTN && op <= NEON_2RM_VRINTZ) ||
5517             op == NEON_2RM_VRINTM ||
5518             (op >= NEON_2RM_VRINTP && op <= NEON_2RM_VCVTMS) ||
5519             op >= NEON_2RM_VRECPE_F);
5520 }
5521
5522 static bool neon_2rm_is_v8_op(int op)
5523 {
5524     /* Return true if this neon 2reg-misc op is ARMv8 and up */
5525     switch (op) {
5526     case NEON_2RM_VRINTN:
5527     case NEON_2RM_VRINTA:
5528     case NEON_2RM_VRINTM:
5529     case NEON_2RM_VRINTP:
5530     case NEON_2RM_VRINTZ:
5531     case NEON_2RM_VRINTX:
5532     case NEON_2RM_VCVTAU:
5533     case NEON_2RM_VCVTAS:
5534     case NEON_2RM_VCVTNU:
5535     case NEON_2RM_VCVTNS:
5536     case NEON_2RM_VCVTPU:
5537     case NEON_2RM_VCVTPS:
5538     case NEON_2RM_VCVTMU:
5539     case NEON_2RM_VCVTMS:
5540         return true;
5541     default:
5542         return false;
5543     }
5544 }
5545
5546 /* Each entry in this array has bit n set if the insn allows
5547  * size value n (otherwise it will UNDEF). Since unallocated
5548  * op values will have no bits set they always UNDEF.
5549  */
5550 static const uint8_t neon_2rm_sizes[] = {
5551     [NEON_2RM_VREV64] = 0x7,
5552     [NEON_2RM_VREV32] = 0x3,
5553     [NEON_2RM_VREV16] = 0x1,
5554     [NEON_2RM_VPADDL] = 0x7,
5555     [NEON_2RM_VPADDL_U] = 0x7,
5556     [NEON_2RM_AESE] = 0x1,
5557     [NEON_2RM_AESMC] = 0x1,
5558     [NEON_2RM_VCLS] = 0x7,
5559     [NEON_2RM_VCLZ] = 0x7,
5560     [NEON_2RM_VCNT] = 0x1,
5561     [NEON_2RM_VMVN] = 0x1,
5562     [NEON_2RM_VPADAL] = 0x7,
5563     [NEON_2RM_VPADAL_U] = 0x7,
5564     [NEON_2RM_VQABS] = 0x7,
5565     [NEON_2RM_VQNEG] = 0x7,
5566     [NEON_2RM_VCGT0] = 0x7,
5567     [NEON_2RM_VCGE0] = 0x7,
5568     [NEON_2RM_VCEQ0] = 0x7,
5569     [NEON_2RM_VCLE0] = 0x7,
5570     [NEON_2RM_VCLT0] = 0x7,
5571     [NEON_2RM_SHA1H] = 0x4,
5572     [NEON_2RM_VABS] = 0x7,
5573     [NEON_2RM_VNEG] = 0x7,
5574     [NEON_2RM_VCGT0_F] = 0x4,
5575     [NEON_2RM_VCGE0_F] = 0x4,
5576     [NEON_2RM_VCEQ0_F] = 0x4,
5577     [NEON_2RM_VCLE0_F] = 0x4,
5578     [NEON_2RM_VCLT0_F] = 0x4,
5579     [NEON_2RM_VABS_F] = 0x4,
5580     [NEON_2RM_VNEG_F] = 0x4,
5581     [NEON_2RM_VSWP] = 0x1,
5582     [NEON_2RM_VTRN] = 0x7,
5583     [NEON_2RM_VUZP] = 0x7,
5584     [NEON_2RM_VZIP] = 0x7,
5585     [NEON_2RM_VMOVN] = 0x7,
5586     [NEON_2RM_VQMOVN] = 0x7,
5587     [NEON_2RM_VSHLL] = 0x7,
5588     [NEON_2RM_SHA1SU1] = 0x4,
5589     [NEON_2RM_VRINTN] = 0x4,
5590     [NEON_2RM_VRINTX] = 0x4,
5591     [NEON_2RM_VRINTA] = 0x4,
5592     [NEON_2RM_VRINTZ] = 0x4,
5593     [NEON_2RM_VCVT_F16_F32] = 0x2,
5594     [NEON_2RM_VRINTM] = 0x4,
5595     [NEON_2RM_VCVT_F32_F16] = 0x2,
5596     [NEON_2RM_VRINTP] = 0x4,
5597     [NEON_2RM_VCVTAU] = 0x4,
5598     [NEON_2RM_VCVTAS] = 0x4,
5599     [NEON_2RM_VCVTNU] = 0x4,
5600     [NEON_2RM_VCVTNS] = 0x4,
5601     [NEON_2RM_VCVTPU] = 0x4,
5602     [NEON_2RM_VCVTPS] = 0x4,
5603     [NEON_2RM_VCVTMU] = 0x4,
5604     [NEON_2RM_VCVTMS] = 0x4,
5605     [NEON_2RM_VRECPE] = 0x4,
5606     [NEON_2RM_VRSQRTE] = 0x4,
5607     [NEON_2RM_VRECPE_F] = 0x4,
5608     [NEON_2RM_VRSQRTE_F] = 0x4,
5609     [NEON_2RM_VCVT_FS] = 0x4,
5610     [NEON_2RM_VCVT_FU] = 0x4,
5611     [NEON_2RM_VCVT_SF] = 0x4,
5612     [NEON_2RM_VCVT_UF] = 0x4,
5613 };
5614
5615
5616 /* Expand v8.1 simd helper.  */
5617 static int do_v81_helper(DisasContext *s, gen_helper_gvec_3_ptr *fn,
5618                          int q, int rd, int rn, int rm)
5619 {
5620     if (arm_dc_feature(s, ARM_FEATURE_V8_RDM)) {
5621         int opr_sz = (1 + q) * 8;
5622         tcg_gen_gvec_3_ptr(vfp_reg_offset(1, rd),
5623                            vfp_reg_offset(1, rn),
5624                            vfp_reg_offset(1, rm), cpu_env,
5625                            opr_sz, opr_sz, 0, fn);
5626         return 0;
5627     }
5628     return 1;
5629 }
5630
5631 /* Translate a NEON data processing instruction.  Return nonzero if the
5632    instruction is invalid.
5633    We process data in a mixture of 32-bit and 64-bit chunks.
5634    Mostly we use 32-bit chunks so we can use normal scalar instructions.  */
5635
5636 static int disas_neon_data_insn(DisasContext *s, uint32_t insn)
5637 {
5638     int op;
5639     int q;
5640     int rd, rn, rm;
5641     int size;
5642     int shift;
5643     int pass;
5644     int count;
5645     int pairwise;
5646     int u;
5647     uint32_t imm, mask;
5648     TCGv_i32 tmp, tmp2, tmp3, tmp4, tmp5;
5649     TCGv_ptr ptr1, ptr2, ptr3;
5650     TCGv_i64 tmp64;
5651
5652     /* FIXME: this access check should not take precedence over UNDEF
5653      * for invalid encodings; we will generate incorrect syndrome information
5654      * for attempts to execute invalid vfp/neon encodings with FP disabled.
5655      */
5656     if (s->fp_excp_el) {
5657         gen_exception_insn(s, 4, EXCP_UDEF,
5658                            syn_fp_access_trap(1, 0xe, false), s->fp_excp_el);
5659         return 0;
5660     }
5661
5662     if (!s->vfp_enabled)
5663       return 1;
5664     q = (insn & (1 << 6)) != 0;
5665     u = (insn >> 24) & 1;
5666     VFP_DREG_D(rd, insn);
5667     VFP_DREG_N(rn, insn);
5668     VFP_DREG_M(rm, insn);
5669     size = (insn >> 20) & 3;
5670     if ((insn & (1 << 23)) == 0) {
5671         /* Three register same length.  */
5672         op = ((insn >> 7) & 0x1e) | ((insn >> 4) & 1);
5673         /* Catch invalid op and bad size combinations: UNDEF */
5674         if ((neon_3r_sizes[op] & (1 << size)) == 0) {
5675             return 1;
5676         }
5677         /* All insns of this form UNDEF for either this condition or the
5678          * superset of cases "Q==1"; we catch the latter later.
5679          */
5680         if (q && ((rd | rn | rm) & 1)) {
5681             return 1;
5682         }
5683         switch (op) {
5684         case NEON_3R_SHA:
5685             /* The SHA-1/SHA-256 3-register instructions require special
5686              * treatment here, as their size field is overloaded as an
5687              * op type selector, and they all consume their input in a
5688              * single pass.
5689              */
5690             if (!q) {
5691                 return 1;
5692             }
5693             if (!u) { /* SHA-1 */
5694                 if (!arm_dc_feature(s, ARM_FEATURE_V8_SHA1)) {
5695                     return 1;
5696                 }
5697                 ptr1 = vfp_reg_ptr(true, rd);
5698                 ptr2 = vfp_reg_ptr(true, rn);
5699                 ptr3 = vfp_reg_ptr(true, rm);
5700                 tmp4 = tcg_const_i32(size);
5701                 gen_helper_crypto_sha1_3reg(ptr1, ptr2, ptr3, tmp4);
5702                 tcg_temp_free_i32(tmp4);
5703             } else { /* SHA-256 */
5704                 if (!arm_dc_feature(s, ARM_FEATURE_V8_SHA256) || size == 3) {
5705                     return 1;
5706                 }
5707                 ptr1 = vfp_reg_ptr(true, rd);
5708                 ptr2 = vfp_reg_ptr(true, rn);
5709                 ptr3 = vfp_reg_ptr(true, rm);
5710                 switch (size) {
5711                 case 0:
5712                     gen_helper_crypto_sha256h(ptr1, ptr2, ptr3);
5713                     break;
5714                 case 1:
5715                     gen_helper_crypto_sha256h2(ptr1, ptr2, ptr3);
5716                     break;
5717                 case 2:
5718                     gen_helper_crypto_sha256su1(ptr1, ptr2, ptr3);
5719                     break;
5720                 }
5721             }
5722             tcg_temp_free_ptr(ptr1);
5723             tcg_temp_free_ptr(ptr2);
5724             tcg_temp_free_ptr(ptr3);
5725             return 0;
5726
5727         case NEON_3R_VPADD_VQRDMLAH:
5728             if (!u) {
5729                 break;  /* VPADD */
5730             }
5731             /* VQRDMLAH */
5732             switch (size) {
5733             case 1:
5734                 return do_v81_helper(s, gen_helper_gvec_qrdmlah_s16,
5735                                      q, rd, rn, rm);
5736             case 2:
5737                 return do_v81_helper(s, gen_helper_gvec_qrdmlah_s32,
5738                                      q, rd, rn, rm);
5739             }
5740             return 1;
5741
5742         case NEON_3R_VFM_VQRDMLSH:
5743             if (!u) {
5744                 /* VFM, VFMS */
5745                 if (size == 1) {
5746                     return 1;
5747                 }
5748                 break;
5749             }
5750             /* VQRDMLSH */
5751             switch (size) {
5752             case 1:
5753                 return do_v81_helper(s, gen_helper_gvec_qrdmlsh_s16,
5754                                      q, rd, rn, rm);
5755             case 2:
5756                 return do_v81_helper(s, gen_helper_gvec_qrdmlsh_s32,
5757                                      q, rd, rn, rm);
5758             }
5759             return 1;
5760         }
5761         if (size == 3 && op != NEON_3R_LOGIC) {
5762             /* 64-bit element instructions. */
5763             for (pass = 0; pass < (q ? 2 : 1); pass++) {
5764                 neon_load_reg64(cpu_V0, rn + pass);
5765                 neon_load_reg64(cpu_V1, rm + pass);
5766                 switch (op) {
5767                 case NEON_3R_VQADD:
5768                     if (u) {
5769                         gen_helper_neon_qadd_u64(cpu_V0, cpu_env,
5770                                                  cpu_V0, cpu_V1);
5771                     } else {
5772                         gen_helper_neon_qadd_s64(cpu_V0, cpu_env,
5773                                                  cpu_V0, cpu_V1);
5774                     }
5775                     break;
5776                 case NEON_3R_VQSUB:
5777                     if (u) {
5778                         gen_helper_neon_qsub_u64(cpu_V0, cpu_env,
5779                                                  cpu_V0, cpu_V1);
5780                     } else {
5781                         gen_helper_neon_qsub_s64(cpu_V0, cpu_env,
5782                                                  cpu_V0, cpu_V1);
5783                     }
5784                     break;
5785                 case NEON_3R_VSHL:
5786                     if (u) {
5787                         gen_helper_neon_shl_u64(cpu_V0, cpu_V1, cpu_V0);
5788                     } else {
5789                         gen_helper_neon_shl_s64(cpu_V0, cpu_V1, cpu_V0);
5790                     }
5791                     break;
5792                 case NEON_3R_VQSHL:
5793                     if (u) {
5794                         gen_helper_neon_qshl_u64(cpu_V0, cpu_env,
5795                                                  cpu_V1, cpu_V0);
5796                     } else {
5797                         gen_helper_neon_qshl_s64(cpu_V0, cpu_env,
5798                                                  cpu_V1, cpu_V0);
5799                     }
5800                     break;
5801                 case NEON_3R_VRSHL:
5802                     if (u) {
5803                         gen_helper_neon_rshl_u64(cpu_V0, cpu_V1, cpu_V0);
5804                     } else {
5805                         gen_helper_neon_rshl_s64(cpu_V0, cpu_V1, cpu_V0);
5806                     }
5807                     break;
5808                 case NEON_3R_VQRSHL:
5809                     if (u) {
5810                         gen_helper_neon_qrshl_u64(cpu_V0, cpu_env,
5811                                                   cpu_V1, cpu_V0);
5812                     } else {
5813                         gen_helper_neon_qrshl_s64(cpu_V0, cpu_env,
5814                                                   cpu_V1, cpu_V0);
5815                     }
5816                     break;
5817                 case NEON_3R_VADD_VSUB:
5818                     if (u) {
5819                         tcg_gen_sub_i64(CPU_V001);
5820                     } else {
5821                         tcg_gen_add_i64(CPU_V001);
5822                     }
5823                     break;
5824                 default:
5825                     abort();
5826                 }
5827                 neon_store_reg64(cpu_V0, rd + pass);
5828             }
5829             return 0;
5830         }
5831         pairwise = 0;
5832         switch (op) {
5833         case NEON_3R_VSHL:
5834         case NEON_3R_VQSHL:
5835         case NEON_3R_VRSHL:
5836         case NEON_3R_VQRSHL:
5837             {
5838                 int rtmp;
5839                 /* Shift instruction operands are reversed.  */
5840                 rtmp = rn;
5841                 rn = rm;
5842                 rm = rtmp;
5843             }
5844             break;
5845         case NEON_3R_VPADD_VQRDMLAH:
5846         case NEON_3R_VPMAX:
5847         case NEON_3R_VPMIN:
5848             pairwise = 1;
5849             break;
5850         case NEON_3R_FLOAT_ARITH:
5851             pairwise = (u && size < 2); /* if VPADD (float) */
5852             break;
5853         case NEON_3R_FLOAT_MINMAX:
5854             pairwise = u; /* if VPMIN/VPMAX (float) */
5855             break;
5856         case NEON_3R_FLOAT_CMP:
5857             if (!u && size) {
5858                 /* no encoding for U=0 C=1x */
5859                 return 1;
5860             }
5861             break;
5862         case NEON_3R_FLOAT_ACMP:
5863             if (!u) {
5864                 return 1;
5865             }
5866             break;
5867         case NEON_3R_FLOAT_MISC:
5868             /* VMAXNM/VMINNM in ARMv8 */
5869             if (u && !arm_dc_feature(s, ARM_FEATURE_V8)) {
5870                 return 1;
5871             }
5872             break;
5873         case NEON_3R_VMUL:
5874             if (u && (size != 0)) {
5875                 /* UNDEF on invalid size for polynomial subcase */
5876                 return 1;
5877             }
5878             break;
5879         case NEON_3R_VFM_VQRDMLSH:
5880             if (!arm_dc_feature(s, ARM_FEATURE_VFP4)) {
5881                 return 1;
5882             }
5883             break;
5884         default:
5885             break;
5886         }
5887
5888         if (pairwise && q) {
5889             /* All the pairwise insns UNDEF if Q is set */
5890             return 1;
5891         }
5892
5893         for (pass = 0; pass < (q ? 4 : 2); pass++) {
5894
5895         if (pairwise) {
5896             /* Pairwise.  */
5897             if (pass < 1) {
5898                 tmp = neon_load_reg(rn, 0);
5899                 tmp2 = neon_load_reg(rn, 1);
5900             } else {
5901                 tmp = neon_load_reg(rm, 0);
5902                 tmp2 = neon_load_reg(rm, 1);
5903             }
5904         } else {
5905             /* Elementwise.  */
5906             tmp = neon_load_reg(rn, pass);
5907             tmp2 = neon_load_reg(rm, pass);
5908         }
5909         switch (op) {
5910         case NEON_3R_VHADD:
5911             GEN_NEON_INTEGER_OP(hadd);
5912             break;
5913         case NEON_3R_VQADD:
5914             GEN_NEON_INTEGER_OP_ENV(qadd);
5915             break;
5916         case NEON_3R_VRHADD:
5917             GEN_NEON_INTEGER_OP(rhadd);
5918             break;
5919         case NEON_3R_LOGIC: /* Logic ops.  */
5920             switch ((u << 2) | size) {
5921             case 0: /* VAND */
5922                 tcg_gen_and_i32(tmp, tmp, tmp2);
5923                 break;
5924             case 1: /* BIC */
5925                 tcg_gen_andc_i32(tmp, tmp, tmp2);
5926                 break;
5927             case 2: /* VORR */
5928                 tcg_gen_or_i32(tmp, tmp, tmp2);
5929                 break;
5930             case 3: /* VORN */
5931                 tcg_gen_orc_i32(tmp, tmp, tmp2);
5932                 break;
5933             case 4: /* VEOR */
5934                 tcg_gen_xor_i32(tmp, tmp, tmp2);
5935                 break;
5936             case 5: /* VBSL */
5937                 tmp3 = neon_load_reg(rd, pass);
5938                 gen_neon_bsl(tmp, tmp, tmp2, tmp3);
5939                 tcg_temp_free_i32(tmp3);
5940                 break;
5941             case 6: /* VBIT */
5942                 tmp3 = neon_load_reg(rd, pass);
5943                 gen_neon_bsl(tmp, tmp, tmp3, tmp2);
5944                 tcg_temp_free_i32(tmp3);
5945                 break;
5946             case 7: /* VBIF */
5947                 tmp3 = neon_load_reg(rd, pass);
5948                 gen_neon_bsl(tmp, tmp3, tmp, tmp2);
5949                 tcg_temp_free_i32(tmp3);
5950                 break;
5951             }
5952             break;
5953         case NEON_3R_VHSUB:
5954             GEN_NEON_INTEGER_OP(hsub);
5955             break;
5956         case NEON_3R_VQSUB:
5957             GEN_NEON_INTEGER_OP_ENV(qsub);
5958             break;
5959         case NEON_3R_VCGT:
5960             GEN_NEON_INTEGER_OP(cgt);
5961             break;
5962         case NEON_3R_VCGE:
5963             GEN_NEON_INTEGER_OP(cge);
5964             break;
5965         case NEON_3R_VSHL:
5966             GEN_NEON_INTEGER_OP(shl);
5967             break;
5968         case NEON_3R_VQSHL:
5969             GEN_NEON_INTEGER_OP_ENV(qshl);
5970             break;
5971         case NEON_3R_VRSHL:
5972             GEN_NEON_INTEGER_OP(rshl);
5973             break;
5974         case NEON_3R_VQRSHL:
5975             GEN_NEON_INTEGER_OP_ENV(qrshl);
5976             break;
5977         case NEON_3R_VMAX:
5978             GEN_NEON_INTEGER_OP(max);
5979             break;
5980         case NEON_3R_VMIN:
5981             GEN_NEON_INTEGER_OP(min);
5982             break;
5983         case NEON_3R_VABD:
5984             GEN_NEON_INTEGER_OP(abd);
5985             break;
5986         case NEON_3R_VABA:
5987             GEN_NEON_INTEGER_OP(abd);
5988             tcg_temp_free_i32(tmp2);
5989             tmp2 = neon_load_reg(rd, pass);
5990             gen_neon_add(size, tmp, tmp2);
5991             break;
5992         case NEON_3R_VADD_VSUB:
5993             if (!u) { /* VADD */
5994                 gen_neon_add(size, tmp, tmp2);
5995             } else { /* VSUB */
5996                 switch (size) {
5997                 case 0: gen_helper_neon_sub_u8(tmp, tmp, tmp2); break;
5998                 case 1: gen_helper_neon_sub_u16(tmp, tmp, tmp2); break;
5999                 case 2: tcg_gen_sub_i32(tmp, tmp, tmp2); break;
6000                 default: abort();
6001                 }
6002             }
6003             break;
6004         case NEON_3R_VTST_VCEQ:
6005             if (!u) { /* VTST */
6006                 switch (size) {
6007                 case 0: gen_helper_neon_tst_u8(tmp, tmp, tmp2); break;
6008                 case 1: gen_helper_neon_tst_u16(tmp, tmp, tmp2); break;
6009                 case 2: gen_helper_neon_tst_u32(tmp, tmp, tmp2); break;
6010                 default: abort();
6011                 }
6012             } else { /* VCEQ */
6013                 switch (size) {
6014                 case 0: gen_helper_neon_ceq_u8(tmp, tmp, tmp2); break;
6015                 case 1: gen_helper_neon_ceq_u16(tmp, tmp, tmp2); break;
6016                 case 2: gen_helper_neon_ceq_u32(tmp, tmp, tmp2); break;
6017                 default: abort();
6018                 }
6019             }
6020             break;
6021         case NEON_3R_VML: /* VMLA, VMLAL, VMLS,VMLSL */
6022             switch (size) {
6023             case 0: gen_helper_neon_mul_u8(tmp, tmp, tmp2); break;
6024             case 1: gen_helper_neon_mul_u16(tmp, tmp, tmp2); break;
6025             case 2: tcg_gen_mul_i32(tmp, tmp, tmp2); break;
6026             default: abort();
6027             }
6028             tcg_temp_free_i32(tmp2);
6029             tmp2 = neon_load_reg(rd, pass);
6030             if (u) { /* VMLS */
6031                 gen_neon_rsb(size, tmp, tmp2);
6032             } else { /* VMLA */
6033                 gen_neon_add(size, tmp, tmp2);
6034             }
6035             break;
6036         case NEON_3R_VMUL:
6037             if (u) { /* polynomial */
6038                 gen_helper_neon_mul_p8(tmp, tmp, tmp2);
6039             } else { /* Integer */
6040                 switch (size) {
6041                 case 0: gen_helper_neon_mul_u8(tmp, tmp, tmp2); break;
6042                 case 1: gen_helper_neon_mul_u16(tmp, tmp, tmp2); break;
6043                 case 2: tcg_gen_mul_i32(tmp, tmp, tmp2); break;
6044                 default: abort();
6045                 }
6046             }
6047             break;
6048         case NEON_3R_VPMAX:
6049             GEN_NEON_INTEGER_OP(pmax);
6050             break;
6051         case NEON_3R_VPMIN:
6052             GEN_NEON_INTEGER_OP(pmin);
6053             break;
6054         case NEON_3R_VQDMULH_VQRDMULH: /* Multiply high.  */
6055             if (!u) { /* VQDMULH */
6056                 switch (size) {
6057                 case 1:
6058                     gen_helper_neon_qdmulh_s16(tmp, cpu_env, tmp, tmp2);
6059                     break;
6060                 case 2:
6061                     gen_helper_neon_qdmulh_s32(tmp, cpu_env, tmp, tmp2);
6062                     break;
6063                 default: abort();
6064                 }
6065             } else { /* VQRDMULH */
6066                 switch (size) {
6067                 case 1:
6068                     gen_helper_neon_qrdmulh_s16(tmp, cpu_env, tmp, tmp2);
6069                     break;
6070                 case 2:
6071                     gen_helper_neon_qrdmulh_s32(tmp, cpu_env, tmp, tmp2);
6072                     break;
6073                 default: abort();
6074                 }
6075             }
6076             break;
6077         case NEON_3R_VPADD_VQRDMLAH:
6078             switch (size) {
6079             case 0: gen_helper_neon_padd_u8(tmp, tmp, tmp2); break;
6080             case 1: gen_helper_neon_padd_u16(tmp, tmp, tmp2); break;
6081             case 2: tcg_gen_add_i32(tmp, tmp, tmp2); break;
6082             default: abort();
6083             }
6084             break;
6085         case NEON_3R_FLOAT_ARITH: /* Floating point arithmetic. */
6086         {
6087             TCGv_ptr fpstatus = get_fpstatus_ptr(1);
6088             switch ((u << 2) | size) {
6089             case 0: /* VADD */
6090             case 4: /* VPADD */
6091                 gen_helper_vfp_adds(tmp, tmp, tmp2, fpstatus);
6092                 break;
6093             case 2: /* VSUB */
6094                 gen_helper_vfp_subs(tmp, tmp, tmp2, fpstatus);
6095                 break;
6096             case 6: /* VABD */
6097                 gen_helper_neon_abd_f32(tmp, tmp, tmp2, fpstatus);
6098                 break;
6099             default:
6100                 abort();
6101             }
6102             tcg_temp_free_ptr(fpstatus);
6103             break;
6104         }
6105         case NEON_3R_FLOAT_MULTIPLY:
6106         {
6107             TCGv_ptr fpstatus = get_fpstatus_ptr(1);
6108             gen_helper_vfp_muls(tmp, tmp, tmp2, fpstatus);
6109             if (!u) {
6110                 tcg_temp_free_i32(tmp2);
6111                 tmp2 = neon_load_reg(rd, pass);
6112                 if (size == 0) {
6113                     gen_helper_vfp_adds(tmp, tmp, tmp2, fpstatus);
6114                 } else {
6115                     gen_helper_vfp_subs(tmp, tmp2, tmp, fpstatus);
6116                 }
6117             }
6118             tcg_temp_free_ptr(fpstatus);
6119             break;
6120         }
6121         case NEON_3R_FLOAT_CMP:
6122         {
6123             TCGv_ptr fpstatus = get_fpstatus_ptr(1);
6124             if (!u) {
6125                 gen_helper_neon_ceq_f32(tmp, tmp, tmp2, fpstatus);
6126             } else {
6127                 if (size == 0) {
6128                     gen_helper_neon_cge_f32(tmp, tmp, tmp2, fpstatus);
6129                 } else {
6130                     gen_helper_neon_cgt_f32(tmp, tmp, tmp2, fpstatus);
6131                 }
6132             }
6133             tcg_temp_free_ptr(fpstatus);
6134             break;
6135         }
6136         case NEON_3R_FLOAT_ACMP:
6137         {
6138             TCGv_ptr fpstatus = get_fpstatus_ptr(1);
6139             if (size == 0) {
6140                 gen_helper_neon_acge_f32(tmp, tmp, tmp2, fpstatus);
6141             } else {
6142                 gen_helper_neon_acgt_f32(tmp, tmp, tmp2, fpstatus);
6143             }
6144             tcg_temp_free_ptr(fpstatus);
6145             break;
6146         }
6147         case NEON_3R_FLOAT_MINMAX:
6148         {
6149             TCGv_ptr fpstatus = get_fpstatus_ptr(1);
6150             if (size == 0) {
6151                 gen_helper_vfp_maxs(tmp, tmp, tmp2, fpstatus);
6152             } else {
6153                 gen_helper_vfp_mins(tmp, tmp, tmp2, fpstatus);
6154             }
6155             tcg_temp_free_ptr(fpstatus);
6156             break;
6157         }
6158         case NEON_3R_FLOAT_MISC:
6159             if (u) {
6160                 /* VMAXNM/VMINNM */
6161                 TCGv_ptr fpstatus = get_fpstatus_ptr(1);
6162                 if (size == 0) {
6163                     gen_helper_vfp_maxnums(tmp, tmp, tmp2, fpstatus);
6164                 } else {
6165                     gen_helper_vfp_minnums(tmp, tmp, tmp2, fpstatus);
6166                 }
6167                 tcg_temp_free_ptr(fpstatus);
6168             } else {
6169                 if (size == 0) {
6170                     gen_helper_recps_f32(tmp, tmp, tmp2, cpu_env);
6171                 } else {
6172                     gen_helper_rsqrts_f32(tmp, tmp, tmp2, cpu_env);
6173               }
6174             }
6175             break;
6176         case NEON_3R_VFM_VQRDMLSH:
6177         {
6178             /* VFMA, VFMS: fused multiply-add */
6179             TCGv_ptr fpstatus = get_fpstatus_ptr(1);
6180             TCGv_i32 tmp3 = neon_load_reg(rd, pass);
6181             if (size) {
6182                 /* VFMS */
6183                 gen_helper_vfp_negs(tmp, tmp);
6184             }
6185             gen_helper_vfp_muladds(tmp, tmp, tmp2, tmp3, fpstatus);
6186             tcg_temp_free_i32(tmp3);
6187             tcg_temp_free_ptr(fpstatus);
6188             break;
6189         }
6190         default:
6191             abort();
6192         }
6193         tcg_temp_free_i32(tmp2);
6194
6195         /* Save the result.  For elementwise operations we can put it
6196            straight into the destination register.  For pairwise operations
6197            we have to be careful to avoid clobbering the source operands.  */
6198         if (pairwise && rd == rm) {
6199             neon_store_scratch(pass, tmp);
6200         } else {
6201             neon_store_reg(rd, pass, tmp);
6202         }
6203
6204         } /* for pass */
6205         if (pairwise && rd == rm) {
6206             for (pass = 0; pass < (q ? 4 : 2); pass++) {
6207                 tmp = neon_load_scratch(pass);
6208                 neon_store_reg(rd, pass, tmp);
6209             }
6210         }
6211         /* End of 3 register same size operations.  */
6212     } else if (insn & (1 << 4)) {
6213         if ((insn & 0x00380080) != 0) {
6214             /* Two registers and shift.  */
6215             op = (insn >> 8) & 0xf;
6216             if (insn & (1 << 7)) {
6217                 /* 64-bit shift. */
6218                 if (op > 7) {
6219                     return 1;
6220                 }
6221                 size = 3;
6222             } else {
6223                 size = 2;
6224                 while ((insn & (1 << (size + 19))) == 0)
6225                     size--;
6226             }
6227             shift = (insn >> 16) & ((1 << (3 + size)) - 1);
6228             /* To avoid excessive duplication of ops we implement shift
6229                by immediate using the variable shift operations.  */
6230             if (op < 8) {
6231                 /* Shift by immediate:
6232                    VSHR, VSRA, VRSHR, VRSRA, VSRI, VSHL, VQSHL, VQSHLU.  */
6233                 if (q && ((rd | rm) & 1)) {
6234                     return 1;
6235                 }
6236                 if (!u && (op == 4 || op == 6)) {
6237                     return 1;
6238                 }
6239                 /* Right shifts are encoded as N - shift, where N is the
6240                    element size in bits.  */
6241                 if (op <= 4)
6242                     shift = shift - (1 << (size + 3));
6243                 if (size == 3) {
6244                     count = q + 1;
6245                 } else {
6246                     count = q ? 4: 2;
6247                 }
6248                 switch (size) {
6249                 case 0:
6250                     imm = (uint8_t) shift;
6251                     imm |= imm << 8;
6252                     imm |= imm << 16;
6253                     break;
6254                 case 1:
6255                     imm = (uint16_t) shift;
6256                     imm |= imm << 16;
6257                     break;
6258                 case 2:
6259                 case 3:
6260                     imm = shift;
6261                     break;
6262                 default:
6263                     abort();
6264                 }
6265
6266                 for (pass = 0; pass < count; pass++) {
6267                     if (size == 3) {
6268                         neon_load_reg64(cpu_V0, rm + pass);
6269                         tcg_gen_movi_i64(cpu_V1, imm);
6270                         switch (op) {
6271                         case 0:  /* VSHR */
6272                         case 1:  /* VSRA */
6273                             if (u)
6274                                 gen_helper_neon_shl_u64(cpu_V0, cpu_V0, cpu_V1);
6275                             else
6276                                 gen_helper_neon_shl_s64(cpu_V0, cpu_V0, cpu_V1);
6277                             break;
6278                         case 2: /* VRSHR */
6279                         case 3: /* VRSRA */
6280                             if (u)
6281                                 gen_helper_neon_rshl_u64(cpu_V0, cpu_V0, cpu_V1);
6282                             else
6283                                 gen_helper_neon_rshl_s64(cpu_V0, cpu_V0, cpu_V1);
6284                             break;
6285                         case 4: /* VSRI */
6286                         case 5: /* VSHL, VSLI */
6287                             gen_helper_neon_shl_u64(cpu_V0, cpu_V0, cpu_V1);
6288                             break;
6289                         case 6: /* VQSHLU */
6290                             gen_helper_neon_qshlu_s64(cpu_V0, cpu_env,
6291                                                       cpu_V0, cpu_V1);
6292                             break;
6293                         case 7: /* VQSHL */
6294                             if (u) {
6295                                 gen_helper_neon_qshl_u64(cpu_V0, cpu_env,
6296                                                          cpu_V0, cpu_V1);
6297                             } else {
6298                                 gen_helper_neon_qshl_s64(cpu_V0, cpu_env,
6299                                                          cpu_V0, cpu_V1);
6300                             }
6301                             break;
6302                         }
6303                         if (op == 1 || op == 3) {
6304                             /* Accumulate.  */
6305                             neon_load_reg64(cpu_V1, rd + pass);
6306                             tcg_gen_add_i64(cpu_V0, cpu_V0, cpu_V1);
6307                         } else if (op == 4 || (op == 5 && u)) {
6308                             /* Insert */
6309                             neon_load_reg64(cpu_V1, rd + pass);
6310                             uint64_t mask;
6311                             if (shift < -63 || shift > 63) {
6312                                 mask = 0;
6313                             } else {
6314                                 if (op == 4) {
6315                                     mask = 0xffffffffffffffffull >> -shift;
6316                                 } else {
6317                                     mask = 0xffffffffffffffffull << shift;
6318                                 }
6319                             }
6320                             tcg_gen_andi_i64(cpu_V1, cpu_V1, ~mask);
6321                             tcg_gen_or_i64(cpu_V0, cpu_V0, cpu_V1);
6322                         }
6323                         neon_store_reg64(cpu_V0, rd + pass);
6324                     } else { /* size < 3 */
6325                         /* Operands in T0 and T1.  */
6326                         tmp = neon_load_reg(rm, pass);
6327                         tmp2 = tcg_temp_new_i32();
6328                         tcg_gen_movi_i32(tmp2, imm);
6329                         switch (op) {
6330                         case 0:  /* VSHR */
6331                         case 1:  /* VSRA */
6332                             GEN_NEON_INTEGER_OP(shl);
6333                             break;
6334                         case 2: /* VRSHR */
6335                         case 3: /* VRSRA */
6336                             GEN_NEON_INTEGER_OP(rshl);
6337                             break;
6338                         case 4: /* VSRI */
6339                         case 5: /* VSHL, VSLI */
6340                             switch (size) {
6341                             case 0: gen_helper_neon_shl_u8(tmp, tmp, tmp2); break;
6342                             case 1: gen_helper_neon_shl_u16(tmp, tmp, tmp2); break;
6343                             case 2: gen_helper_neon_shl_u32(tmp, tmp, tmp2); break;
6344                             default: abort();
6345                             }
6346                             break;
6347                         case 6: /* VQSHLU */
6348                             switch (size) {
6349                             case 0:
6350                                 gen_helper_neon_qshlu_s8(tmp, cpu_env,
6351                                                          tmp, tmp2);
6352                                 break;
6353                             case 1:
6354                                 gen_helper_neon_qshlu_s16(tmp, cpu_env,
6355                                                           tmp, tmp2);
6356                                 break;
6357                             case 2:
6358                                 gen_helper_neon_qshlu_s32(tmp, cpu_env,
6359                                                           tmp, tmp2);
6360                                 break;
6361                             default:
6362                                 abort();
6363                             }
6364                             break;
6365                         case 7: /* VQSHL */
6366                             GEN_NEON_INTEGER_OP_ENV(qshl);
6367                             break;
6368                         }
6369                         tcg_temp_free_i32(tmp2);
6370
6371                         if (op == 1 || op == 3) {
6372                             /* Accumulate.  */
6373                             tmp2 = neon_load_reg(rd, pass);
6374                             gen_neon_add(size, tmp, tmp2);
6375                             tcg_temp_free_i32(tmp2);
6376                         } else if (op == 4 || (op == 5 && u)) {
6377                             /* Insert */
6378                             switch (size) {
6379                             case 0:
6380                                 if (op == 4)
6381                                     mask = 0xff >> -shift;
6382                                 else
6383                                     mask = (uint8_t)(0xff << shift);
6384                                 mask |= mask << 8;
6385                                 mask |= mask << 16;
6386                                 break;
6387                             case 1:
6388                                 if (op == 4)
6389                                     mask = 0xffff >> -shift;
6390                                 else
6391                                     mask = (uint16_t)(0xffff << shift);
6392                                 mask |= mask << 16;
6393                                 break;
6394                             case 2:
6395                                 if (shift < -31 || shift > 31) {
6396                                     mask = 0;
6397                                 } else {
6398                                     if (op == 4)
6399                                         mask = 0xffffffffu >> -shift;
6400                                     else
6401                                         mask = 0xffffffffu << shift;
6402                                 }
6403                                 break;
6404                             default:
6405                                 abort();
6406                             }
6407                             tmp2 = neon_load_reg(rd, pass);
6408                             tcg_gen_andi_i32(tmp, tmp, mask);
6409                             tcg_gen_andi_i32(tmp2, tmp2, ~mask);
6410                             tcg_gen_or_i32(tmp, tmp, tmp2);
6411                             tcg_temp_free_i32(tmp2);
6412                         }
6413                         neon_store_reg(rd, pass, tmp);
6414                     }
6415                 } /* for pass */
6416             } else if (op < 10) {
6417                 /* Shift by immediate and narrow:
6418                    VSHRN, VRSHRN, VQSHRN, VQRSHRN.  */
6419                 int input_unsigned = (op == 8) ? !u : u;
6420                 if (rm & 1) {
6421                     return 1;
6422                 }
6423                 shift = shift - (1 << (size + 3));
6424                 size++;
6425                 if (size == 3) {
6426                     tmp64 = tcg_const_i64(shift);
6427                     neon_load_reg64(cpu_V0, rm);
6428                     neon_load_reg64(cpu_V1, rm + 1);
6429                     for (pass = 0; pass < 2; pass++) {
6430                         TCGv_i64 in;
6431                         if (pass == 0) {
6432                             in = cpu_V0;
6433                         } else {
6434                             in = cpu_V1;
6435                         }
6436                         if (q) {
6437                             if (input_unsigned) {
6438                                 gen_helper_neon_rshl_u64(cpu_V0, in, tmp64);
6439                             } else {
6440                                 gen_helper_neon_rshl_s64(cpu_V0, in, tmp64);
6441                             }
6442                         } else {
6443                             if (input_unsigned) {
6444                                 gen_helper_neon_shl_u64(cpu_V0, in, tmp64);
6445                             } else {
6446                                 gen_helper_neon_shl_s64(cpu_V0, in, tmp64);
6447                             }
6448                         }
6449                         tmp = tcg_temp_new_i32();
6450                         gen_neon_narrow_op(op == 8, u, size - 1, tmp, cpu_V0);
6451                         neon_store_reg(rd, pass, tmp);
6452                     } /* for pass */
6453                     tcg_temp_free_i64(tmp64);
6454                 } else {
6455                     if (size == 1) {
6456                         imm = (uint16_t)shift;
6457                         imm |= imm << 16;
6458                     } else {
6459                         /* size == 2 */
6460                         imm = (uint32_t)shift;
6461                     }
6462                     tmp2 = tcg_const_i32(imm);
6463                     tmp4 = neon_load_reg(rm + 1, 0);
6464                     tmp5 = neon_load_reg(rm + 1, 1);
6465                     for (pass = 0; pass < 2; pass++) {
6466                         if (pass == 0) {
6467                             tmp = neon_load_reg(rm, 0);
6468                         } else {
6469                             tmp = tmp4;
6470                         }
6471                         gen_neon_shift_narrow(size, tmp, tmp2, q,
6472                                               input_unsigned);
6473                         if (pass == 0) {
6474                             tmp3 = neon_load_reg(rm, 1);
6475                         } else {
6476                             tmp3 = tmp5;
6477                         }
6478                         gen_neon_shift_narrow(size, tmp3, tmp2, q,
6479                                               input_unsigned);
6480                         tcg_gen_concat_i32_i64(cpu_V0, tmp, tmp3);
6481                         tcg_temp_free_i32(tmp);
6482                         tcg_temp_free_i32(tmp3);
6483                         tmp = tcg_temp_new_i32();
6484                         gen_neon_narrow_op(op == 8, u, size - 1, tmp, cpu_V0);
6485                         neon_store_reg(rd, pass, tmp);
6486                     } /* for pass */
6487                     tcg_temp_free_i32(tmp2);
6488                 }
6489             } else if (op == 10) {
6490                 /* VSHLL, VMOVL */
6491                 if (q || (rd & 1)) {
6492                     return 1;
6493                 }
6494                 tmp = neon_load_reg(rm, 0);
6495                 tmp2 = neon_load_reg(rm, 1);
6496                 for (pass = 0; pass < 2; pass++) {
6497                     if (pass == 1)
6498                         tmp = tmp2;
6499
6500                     gen_neon_widen(cpu_V0, tmp, size, u);
6501
6502                     if (shift != 0) {
6503                         /* The shift is less than the width of the source
6504                            type, so we can just shift the whole register.  */
6505                         tcg_gen_shli_i64(cpu_V0, cpu_V0, shift);
6506                         /* Widen the result of shift: we need to clear
6507                          * the potential overflow bits resulting from
6508                          * left bits of the narrow input appearing as
6509                          * right bits of left the neighbour narrow
6510                          * input.  */
6511                         if (size < 2 || !u) {
6512                             uint64_t imm64;
6513                             if (size == 0) {
6514                                 imm = (0xffu >> (8 - shift));
6515                                 imm |= imm << 16;
6516                             } else if (size == 1) {
6517                                 imm = 0xffff >> (16 - shift);
6518                             } else {
6519                                 /* size == 2 */
6520                                 imm = 0xffffffff >> (32 - shift);
6521                             }
6522                             if (size < 2) {
6523                                 imm64 = imm | (((uint64_t)imm) << 32);
6524                             } else {
6525                                 imm64 = imm;
6526                             }
6527                             tcg_gen_andi_i64(cpu_V0, cpu_V0, ~imm64);
6528                         }
6529                     }
6530                     neon_store_reg64(cpu_V0, rd + pass);
6531                 }
6532             } else if (op >= 14) {
6533                 /* VCVT fixed-point.  */
6534                 if (!(insn & (1 << 21)) || (q && ((rd | rm) & 1))) {
6535                     return 1;
6536                 }
6537                 /* We have already masked out the must-be-1 top bit of imm6,
6538                  * hence this 32-shift where the ARM ARM has 64-imm6.
6539                  */
6540                 shift = 32 - shift;
6541                 for (pass = 0; pass < (q ? 4 : 2); pass++) {
6542                     tcg_gen_ld_f32(cpu_F0s, cpu_env, neon_reg_offset(rm, pass));
6543                     if (!(op & 1)) {
6544                         if (u)
6545                             gen_vfp_ulto(0, shift, 1);
6546                         else
6547                             gen_vfp_slto(0, shift, 1);
6548                     } else {
6549                         if (u)
6550                             gen_vfp_toul(0, shift, 1);
6551                         else
6552                             gen_vfp_tosl(0, shift, 1);
6553                     }
6554                     tcg_gen_st_f32(cpu_F0s, cpu_env, neon_reg_offset(rd, pass));
6555                 }
6556             } else {
6557                 return 1;
6558             }
6559         } else { /* (insn & 0x00380080) == 0 */
6560             int invert;
6561             if (q && (rd & 1)) {
6562                 return 1;
6563             }
6564
6565             op = (insn >> 8) & 0xf;
6566             /* One register and immediate.  */
6567             imm = (u << 7) | ((insn >> 12) & 0x70) | (insn & 0xf);
6568             invert = (insn & (1 << 5)) != 0;
6569             /* Note that op = 2,3,4,5,6,7,10,11,12,13 imm=0 is UNPREDICTABLE.
6570              * We choose to not special-case this and will behave as if a
6571              * valid constant encoding of 0 had been given.
6572              */
6573             switch (op) {
6574             case 0: case 1:
6575                 /* no-op */
6576                 break;
6577             case 2: case 3:
6578                 imm <<= 8;
6579                 break;
6580             case 4: case 5:
6581                 imm <<= 16;
6582                 break;
6583             case 6: case 7:
6584                 imm <<= 24;
6585                 break;
6586             case 8: case 9:
6587                 imm |= imm << 16;
6588                 break;
6589             case 10: case 11:
6590                 imm = (imm << 8) | (imm << 24);
6591                 break;
6592             case 12:
6593                 imm = (imm << 8) | 0xff;
6594                 break;
6595             case 13:
6596                 imm = (imm << 16) | 0xffff;
6597                 break;
6598             case 14:
6599                 imm |= (imm << 8) | (imm << 16) | (imm << 24);
6600                 if (invert)
6601                     imm = ~imm;
6602                 break;
6603             case 15:
6604                 if (invert) {
6605                     return 1;
6606                 }
6607                 imm = ((imm & 0x80) << 24) | ((imm & 0x3f) << 19)
6608                       | ((imm & 0x40) ? (0x1f << 25) : (1 << 30));
6609                 break;
6610             }
6611             if (invert)
6612                 imm = ~imm;
6613
6614             for (pass = 0; pass < (q ? 4 : 2); pass++) {
6615                 if (op & 1 && op < 12) {
6616                     tmp = neon_load_reg(rd, pass);
6617                     if (invert) {
6618                         /* The immediate value has already been inverted, so
6619                            BIC becomes AND.  */
6620                         tcg_gen_andi_i32(tmp, tmp, imm);
6621                     } else {
6622                         tcg_gen_ori_i32(tmp, tmp, imm);
6623                     }
6624                 } else {
6625                     /* VMOV, VMVN.  */
6626                     tmp = tcg_temp_new_i32();
6627                     if (op == 14 && invert) {
6628                         int n;
6629                         uint32_t val;
6630                         val = 0;
6631                         for (n = 0; n < 4; n++) {
6632                             if (imm & (1 << (n + (pass & 1) * 4)))
6633                                 val |= 0xff << (n * 8);
6634                         }
6635                         tcg_gen_movi_i32(tmp, val);
6636                     } else {
6637                         tcg_gen_movi_i32(tmp, imm);
6638                     }
6639                 }
6640                 neon_store_reg(rd, pass, tmp);
6641             }
6642         }
6643     } else { /* (insn & 0x00800010 == 0x00800000) */
6644         if (size != 3) {
6645             op = (insn >> 8) & 0xf;
6646             if ((insn & (1 << 6)) == 0) {
6647                 /* Three registers of different lengths.  */
6648                 int src1_wide;
6649                 int src2_wide;
6650                 int prewiden;
6651                 /* undefreq: bit 0 : UNDEF if size == 0
6652                  *           bit 1 : UNDEF if size == 1
6653                  *           bit 2 : UNDEF if size == 2
6654                  *           bit 3 : UNDEF if U == 1
6655                  * Note that [2:0] set implies 'always UNDEF'
6656                  */
6657                 int undefreq;
6658                 /* prewiden, src1_wide, src2_wide, undefreq */
6659                 static const int neon_3reg_wide[16][4] = {
6660                     {1, 0, 0, 0}, /* VADDL */
6661                     {1, 1, 0, 0}, /* VADDW */
6662                     {1, 0, 0, 0}, /* VSUBL */
6663                     {1, 1, 0, 0}, /* VSUBW */
6664                     {0, 1, 1, 0}, /* VADDHN */
6665                     {0, 0, 0, 0}, /* VABAL */
6666                     {0, 1, 1, 0}, /* VSUBHN */
6667                     {0, 0, 0, 0}, /* VABDL */
6668                     {0, 0, 0, 0}, /* VMLAL */
6669                     {0, 0, 0, 9}, /* VQDMLAL */
6670                     {0, 0, 0, 0}, /* VMLSL */
6671                     {0, 0, 0, 9}, /* VQDMLSL */
6672                     {0, 0, 0, 0}, /* Integer VMULL */
6673                     {0, 0, 0, 1}, /* VQDMULL */
6674                     {0, 0, 0, 0xa}, /* Polynomial VMULL */
6675                     {0, 0, 0, 7}, /* Reserved: always UNDEF */
6676                 };
6677
6678                 prewiden = neon_3reg_wide[op][0];
6679                 src1_wide = neon_3reg_wide[op][1];
6680                 src2_wide = neon_3reg_wide[op][2];
6681                 undefreq = neon_3reg_wide[op][3];
6682
6683                 if ((undefreq & (1 << size)) ||
6684                     ((undefreq & 8) && u)) {
6685                     return 1;
6686                 }
6687                 if ((src1_wide && (rn & 1)) ||
6688                     (src2_wide && (rm & 1)) ||
6689                     (!src2_wide && (rd & 1))) {
6690                     return 1;
6691                 }
6692
6693                 /* Handle VMULL.P64 (Polynomial 64x64 to 128 bit multiply)
6694                  * outside the loop below as it only performs a single pass.
6695                  */
6696                 if (op == 14 && size == 2) {
6697                     TCGv_i64 tcg_rn, tcg_rm, tcg_rd;
6698
6699                     if (!arm_dc_feature(s, ARM_FEATURE_V8_PMULL)) {
6700                         return 1;
6701                     }
6702                     tcg_rn = tcg_temp_new_i64();
6703                     tcg_rm = tcg_temp_new_i64();
6704                     tcg_rd = tcg_temp_new_i64();
6705                     neon_load_reg64(tcg_rn, rn);
6706                     neon_load_reg64(tcg_rm, rm);
6707                     gen_helper_neon_pmull_64_lo(tcg_rd, tcg_rn, tcg_rm);
6708                     neon_store_reg64(tcg_rd, rd);
6709                     gen_helper_neon_pmull_64_hi(tcg_rd, tcg_rn, tcg_rm);
6710                     neon_store_reg64(tcg_rd, rd + 1);
6711                     tcg_temp_free_i64(tcg_rn);
6712                     tcg_temp_free_i64(tcg_rm);
6713                     tcg_temp_free_i64(tcg_rd);
6714                     return 0;
6715                 }
6716
6717                 /* Avoid overlapping operands.  Wide source operands are
6718                    always aligned so will never overlap with wide
6719                    destinations in problematic ways.  */
6720                 if (rd == rm && !src2_wide) {
6721                     tmp = neon_load_reg(rm, 1);
6722                     neon_store_scratch(2, tmp);
6723                 } else if (rd == rn && !src1_wide) {
6724                     tmp = neon_load_reg(rn, 1);
6725                     neon_store_scratch(2, tmp);
6726                 }
6727                 tmp3 = NULL;
6728                 for (pass = 0; pass < 2; pass++) {
6729                     if (src1_wide) {
6730                         neon_load_reg64(cpu_V0, rn + pass);
6731                         tmp = NULL;
6732                     } else {
6733                         if (pass == 1 && rd == rn) {
6734                             tmp = neon_load_scratch(2);
6735                         } else {
6736                             tmp = neon_load_reg(rn, pass);
6737                         }
6738                         if (prewiden) {
6739                             gen_neon_widen(cpu_V0, tmp, size, u);
6740                         }
6741                     }
6742                     if (src2_wide) {
6743                         neon_load_reg64(cpu_V1, rm + pass);
6744                         tmp2 = NULL;
6745                     } else {
6746                         if (pass == 1 && rd == rm) {
6747                             tmp2 = neon_load_scratch(2);
6748                         } else {
6749                             tmp2 = neon_load_reg(rm, pass);
6750                         }
6751                         if (prewiden) {
6752                             gen_neon_widen(cpu_V1, tmp2, size, u);
6753                         }
6754                     }
6755                     switch (op) {
6756                     case 0: case 1: case 4: /* VADDL, VADDW, VADDHN, VRADDHN */
6757                         gen_neon_addl(size);
6758                         break;
6759                     case 2: case 3: case 6: /* VSUBL, VSUBW, VSUBHN, VRSUBHN */
6760                         gen_neon_subl(size);
6761                         break;
6762                     case 5: case 7: /* VABAL, VABDL */
6763                         switch ((size << 1) | u) {
6764                         case 0:
6765                             gen_helper_neon_abdl_s16(cpu_V0, tmp, tmp2);
6766                             break;
6767                         case 1:
6768                             gen_helper_neon_abdl_u16(cpu_V0, tmp, tmp2);
6769                             break;
6770                         case 2:
6771                             gen_helper_neon_abdl_s32(cpu_V0, tmp, tmp2);
6772                             break;
6773                         case 3:
6774                             gen_helper_neon_abdl_u32(cpu_V0, tmp, tmp2);
6775                             break;
6776                         case 4:
6777                             gen_helper_neon_abdl_s64(cpu_V0, tmp, tmp2);
6778                             break;
6779                         case 5:
6780                             gen_helper_neon_abdl_u64(cpu_V0, tmp, tmp2);
6781                             break;
6782                         default: abort();
6783                         }
6784                         tcg_temp_free_i32(tmp2);
6785                         tcg_temp_free_i32(tmp);
6786                         break;
6787                     case 8: case 9: case 10: case 11: case 12: case 13:
6788                         /* VMLAL, VQDMLAL, VMLSL, VQDMLSL, VMULL, VQDMULL */
6789                         gen_neon_mull(cpu_V0, tmp, tmp2, size, u);
6790                         break;
6791                     case 14: /* Polynomial VMULL */
6792                         gen_helper_neon_mull_p8(cpu_V0, tmp, tmp2);
6793                         tcg_temp_free_i32(tmp2);
6794                         tcg_temp_free_i32(tmp);
6795                         break;
6796                     default: /* 15 is RESERVED: caught earlier  */
6797                         abort();
6798                     }
6799                     if (op == 13) {
6800                         /* VQDMULL */
6801                         gen_neon_addl_saturate(cpu_V0, cpu_V0, size);
6802                         neon_store_reg64(cpu_V0, rd + pass);
6803                     } else if (op == 5 || (op >= 8 && op <= 11)) {
6804                         /* Accumulate.  */
6805                         neon_load_reg64(cpu_V1, rd + pass);
6806                         switch (op) {
6807                         case 10: /* VMLSL */
6808                             gen_neon_negl(cpu_V0, size);
6809                             /* Fall through */
6810                         case 5: case 8: /* VABAL, VMLAL */
6811                             gen_neon_addl(size);
6812                             break;
6813                         case 9: case 11: /* VQDMLAL, VQDMLSL */
6814                             gen_neon_addl_saturate(cpu_V0, cpu_V0, size);
6815                             if (op == 11) {
6816                                 gen_neon_negl(cpu_V0, size);
6817                             }
6818                             gen_neon_addl_saturate(cpu_V0, cpu_V1, size);
6819                             break;
6820                         default:
6821                             abort();
6822                         }
6823                         neon_store_reg64(cpu_V0, rd + pass);
6824                     } else if (op == 4 || op == 6) {
6825                         /* Narrowing operation.  */
6826                         tmp = tcg_temp_new_i32();
6827                         if (!u) {
6828                             switch (size) {
6829                             case 0:
6830                                 gen_helper_neon_narrow_high_u8(tmp, cpu_V0);
6831                                 break;
6832                             case 1:
6833                                 gen_helper_neon_narrow_high_u16(tmp, cpu_V0);
6834                                 break;
6835                             case 2:
6836                                 tcg_gen_shri_i64(cpu_V0, cpu_V0, 32);
6837                                 tcg_gen_extrl_i64_i32(tmp, cpu_V0);
6838                                 break;
6839                             default: abort();
6840                             }
6841                         } else {
6842                             switch (size) {
6843                             case 0:
6844                                 gen_helper_neon_narrow_round_high_u8(tmp, cpu_V0);
6845                                 break;
6846                             case 1:
6847                                 gen_helper_neon_narrow_round_high_u16(tmp, cpu_V0);
6848                                 break;
6849                             case 2:
6850                                 tcg_gen_addi_i64(cpu_V0, cpu_V0, 1u << 31);
6851                                 tcg_gen_shri_i64(cpu_V0, cpu_V0, 32);
6852                                 tcg_gen_extrl_i64_i32(tmp, cpu_V0);
6853                                 break;
6854                             default: abort();
6855                             }
6856                         }
6857                         if (pass == 0) {
6858                             tmp3 = tmp;
6859                         } else {
6860                             neon_store_reg(rd, 0, tmp3);
6861                             neon_store_reg(rd, 1, tmp);
6862                         }
6863                     } else {
6864                         /* Write back the result.  */
6865                         neon_store_reg64(cpu_V0, rd + pass);
6866                     }
6867                 }
6868             } else {
6869                 /* Two registers and a scalar. NB that for ops of this form
6870                  * the ARM ARM labels bit 24 as Q, but it is in our variable
6871                  * 'u', not 'q'.
6872                  */
6873                 if (size == 0) {
6874                     return 1;
6875                 }
6876                 switch (op) {
6877                 case 1: /* Float VMLA scalar */
6878                 case 5: /* Floating point VMLS scalar */
6879                 case 9: /* Floating point VMUL scalar */
6880                     if (size == 1) {
6881                         return 1;
6882                     }
6883                     /* fall through */
6884                 case 0: /* Integer VMLA scalar */
6885                 case 4: /* Integer VMLS scalar */
6886                 case 8: /* Integer VMUL scalar */
6887                 case 12: /* VQDMULH scalar */
6888                 case 13: /* VQRDMULH scalar */
6889                     if (u && ((rd | rn) & 1)) {
6890                         return 1;
6891                     }
6892                     tmp = neon_get_scalar(size, rm);
6893                     neon_store_scratch(0, tmp);
6894                     for (pass = 0; pass < (u ? 4 : 2); pass++) {
6895                         tmp = neon_load_scratch(0);
6896                         tmp2 = neon_load_reg(rn, pass);
6897                         if (op == 12) {
6898                             if (size == 1) {
6899                                 gen_helper_neon_qdmulh_s16(tmp, cpu_env, tmp, tmp2);
6900                             } else {
6901                                 gen_helper_neon_qdmulh_s32(tmp, cpu_env, tmp, tmp2);
6902                             }
6903                         } else if (op == 13) {
6904                             if (size == 1) {
6905                                 gen_helper_neon_qrdmulh_s16(tmp, cpu_env, tmp, tmp2);
6906                             } else {
6907                                 gen_helper_neon_qrdmulh_s32(tmp, cpu_env, tmp, tmp2);
6908                             }
6909                         } else if (op & 1) {
6910                             TCGv_ptr fpstatus = get_fpstatus_ptr(1);
6911                             gen_helper_vfp_muls(tmp, tmp, tmp2, fpstatus);
6912                             tcg_temp_free_ptr(fpstatus);
6913                         } else {
6914                             switch (size) {
6915                             case 0: gen_helper_neon_mul_u8(tmp, tmp, tmp2); break;
6916                             case 1: gen_helper_neon_mul_u16(tmp, tmp, tmp2); break;
6917                             case 2: tcg_gen_mul_i32(tmp, tmp, tmp2); break;
6918                             default: abort();
6919                             }
6920                         }
6921                         tcg_temp_free_i32(tmp2);
6922                         if (op < 8) {
6923                             /* Accumulate.  */
6924                             tmp2 = neon_load_reg(rd, pass);
6925                             switch (op) {
6926                             case 0:
6927                                 gen_neon_add(size, tmp, tmp2);
6928                                 break;
6929                             case 1:
6930                             {
6931                                 TCGv_ptr fpstatus = get_fpstatus_ptr(1);
6932                                 gen_helper_vfp_adds(tmp, tmp, tmp2, fpstatus);
6933                                 tcg_temp_free_ptr(fpstatus);
6934                                 break;
6935                             }
6936                             case 4:
6937                                 gen_neon_rsb(size, tmp, tmp2);
6938                                 break;
6939                             case 5:
6940                             {
6941                                 TCGv_ptr fpstatus = get_fpstatus_ptr(1);
6942                                 gen_helper_vfp_subs(tmp, tmp2, tmp, fpstatus);
6943                                 tcg_temp_free_ptr(fpstatus);
6944                                 break;
6945                             }
6946                             default:
6947                                 abort();
6948                             }
6949                             tcg_temp_free_i32(tmp2);
6950                         }
6951                         neon_store_reg(rd, pass, tmp);
6952                     }
6953                     break;
6954                 case 3: /* VQDMLAL scalar */
6955                 case 7: /* VQDMLSL scalar */
6956                 case 11: /* VQDMULL scalar */
6957                     if (u == 1) {
6958                         return 1;
6959                     }
6960                     /* fall through */
6961                 case 2: /* VMLAL sclar */
6962                 case 6: /* VMLSL scalar */
6963                 case 10: /* VMULL scalar */
6964                     if (rd & 1) {
6965                         return 1;
6966                     }
6967                     tmp2 = neon_get_scalar(size, rm);
6968                     /* We need a copy of tmp2 because gen_neon_mull
6969                      * deletes it during pass 0.  */
6970                     tmp4 = tcg_temp_new_i32();
6971                     tcg_gen_mov_i32(tmp4, tmp2);
6972                     tmp3 = neon_load_reg(rn, 1);
6973
6974                     for (pass = 0; pass < 2; pass++) {
6975                         if (pass == 0) {
6976                             tmp = neon_load_reg(rn, 0);
6977                         } else {
6978                             tmp = tmp3;
6979                             tmp2 = tmp4;
6980                         }
6981                         gen_neon_mull(cpu_V0, tmp, tmp2, size, u);
6982                         if (op != 11) {
6983                             neon_load_reg64(cpu_V1, rd + pass);
6984                         }
6985                         switch (op) {
6986                         case 6:
6987                             gen_neon_negl(cpu_V0, size);
6988                             /* Fall through */
6989                         case 2:
6990                             gen_neon_addl(size);
6991                             break;
6992                         case 3: case 7:
6993                             gen_neon_addl_saturate(cpu_V0, cpu_V0, size);
6994                             if (op == 7) {
6995                                 gen_neon_negl(cpu_V0, size);
6996                             }
6997                             gen_neon_addl_saturate(cpu_V0, cpu_V1, size);
6998                             break;
6999                         case 10:
7000                             /* no-op */
7001                             break;
7002                         case 11:
7003                             gen_neon_addl_saturate(cpu_V0, cpu_V0, size);
7004                             break;
7005                         default:
7006                             abort();
7007                         }
7008                         neon_store_reg64(cpu_V0, rd + pass);
7009                     }
7010                     break;
7011                 case 14: /* VQRDMLAH scalar */
7012                 case 15: /* VQRDMLSH scalar */
7013                     {
7014                         NeonGenThreeOpEnvFn *fn;
7015
7016                         if (!arm_dc_feature(s, ARM_FEATURE_V8_RDM)) {
7017                             return 1;
7018                         }
7019                         if (u && ((rd | rn) & 1)) {
7020                             return 1;
7021                         }
7022                         if (op == 14) {
7023                             if (size == 1) {
7024                                 fn = gen_helper_neon_qrdmlah_s16;
7025                             } else {
7026                                 fn = gen_helper_neon_qrdmlah_s32;
7027                             }
7028                         } else {
7029                             if (size == 1) {
7030                                 fn = gen_helper_neon_qrdmlsh_s16;
7031                             } else {
7032                                 fn = gen_helper_neon_qrdmlsh_s32;
7033                             }
7034                         }
7035
7036                         tmp2 = neon_get_scalar(size, rm);
7037                         for (pass = 0; pass < (u ? 4 : 2); pass++) {
7038                             tmp = neon_load_reg(rn, pass);
7039                             tmp3 = neon_load_reg(rd, pass);
7040                             fn(tmp, cpu_env, tmp, tmp2, tmp3);
7041                             tcg_temp_free_i32(tmp3);
7042                             neon_store_reg(rd, pass, tmp);
7043                         }
7044                         tcg_temp_free_i32(tmp2);
7045                     }
7046                     break;
7047                 default:
7048                     g_assert_not_reached();
7049                 }
7050             }
7051         } else { /* size == 3 */
7052             if (!u) {
7053                 /* Extract.  */
7054                 imm = (insn >> 8) & 0xf;
7055
7056                 if (imm > 7 && !q)
7057                     return 1;
7058
7059                 if (q && ((rd | rn | rm) & 1)) {
7060                     return 1;
7061                 }
7062
7063                 if (imm == 0) {
7064                     neon_load_reg64(cpu_V0, rn);
7065                     if (q) {
7066                         neon_load_reg64(cpu_V1, rn + 1);
7067                     }
7068                 } else if (imm == 8) {
7069                     neon_load_reg64(cpu_V0, rn + 1);
7070                     if (q) {
7071                         neon_load_reg64(cpu_V1, rm);
7072                     }
7073                 } else if (q) {
7074                     tmp64 = tcg_temp_new_i64();
7075                     if (imm < 8) {
7076                         neon_load_reg64(cpu_V0, rn);
7077                         neon_load_reg64(tmp64, rn + 1);
7078                     } else {
7079                         neon_load_reg64(cpu_V0, rn + 1);
7080                         neon_load_reg64(tmp64, rm);
7081                     }
7082                     tcg_gen_shri_i64(cpu_V0, cpu_V0, (imm & 7) * 8);
7083                     tcg_gen_shli_i64(cpu_V1, tmp64, 64 - ((imm & 7) * 8));
7084                     tcg_gen_or_i64(cpu_V0, cpu_V0, cpu_V1);
7085                     if (imm < 8) {
7086                         neon_load_reg64(cpu_V1, rm);
7087                     } else {
7088                         neon_load_reg64(cpu_V1, rm + 1);
7089                         imm -= 8;
7090                     }
7091                     tcg_gen_shli_i64(cpu_V1, cpu_V1, 64 - (imm * 8));
7092                     tcg_gen_shri_i64(tmp64, tmp64, imm * 8);
7093                     tcg_gen_or_i64(cpu_V1, cpu_V1, tmp64);
7094                     tcg_temp_free_i64(tmp64);
7095                 } else {
7096                     /* BUGFIX */
7097                     neon_load_reg64(cpu_V0, rn);
7098                     tcg_gen_shri_i64(cpu_V0, cpu_V0, imm * 8);
7099                     neon_load_reg64(cpu_V1, rm);
7100                     tcg_gen_shli_i64(cpu_V1, cpu_V1, 64 - (imm * 8));
7101                     tcg_gen_or_i64(cpu_V0, cpu_V0, cpu_V1);
7102                 }
7103                 neon_store_reg64(cpu_V0, rd);
7104                 if (q) {
7105                     neon_store_reg64(cpu_V1, rd + 1);
7106                 }
7107             } else if ((insn & (1 << 11)) == 0) {
7108                 /* Two register misc.  */
7109                 op = ((insn >> 12) & 0x30) | ((insn >> 7) & 0xf);
7110                 size = (insn >> 18) & 3;
7111                 /* UNDEF for unknown op values and bad op-size combinations */
7112                 if ((neon_2rm_sizes[op] & (1 << size)) == 0) {
7113                     return 1;
7114                 }
7115                 if (neon_2rm_is_v8_op(op) &&
7116                     !arm_dc_feature(s, ARM_FEATURE_V8)) {
7117                     return 1;
7118                 }
7119                 if ((op != NEON_2RM_VMOVN && op != NEON_2RM_VQMOVN) &&
7120                     q && ((rm | rd) & 1)) {
7121                     return 1;
7122                 }
7123                 switch (op) {
7124                 case NEON_2RM_VREV64:
7125                     for (pass = 0; pass < (q ? 2 : 1); pass++) {
7126                         tmp = neon_load_reg(rm, pass * 2);
7127                         tmp2 = neon_load_reg(rm, pass * 2 + 1);
7128                         switch (size) {
7129                         case 0: tcg_gen_bswap32_i32(tmp, tmp); break;
7130                         case 1: gen_swap_half(tmp); break;
7131                         case 2: /* no-op */ break;
7132                         default: abort();
7133                         }
7134                         neon_store_reg(rd, pass * 2 + 1, tmp);
7135                         if (size == 2) {
7136                             neon_store_reg(rd, pass * 2, tmp2);
7137                         } else {
7138                             switch (size) {
7139                             case 0: tcg_gen_bswap32_i32(tmp2, tmp2); break;
7140                             case 1: gen_swap_half(tmp2); break;
7141                             default: abort();
7142                             }
7143                             neon_store_reg(rd, pass * 2, tmp2);
7144                         }
7145                     }
7146                     break;
7147                 case NEON_2RM_VPADDL: case NEON_2RM_VPADDL_U:
7148                 case NEON_2RM_VPADAL: case NEON_2RM_VPADAL_U:
7149                     for (pass = 0; pass < q + 1; pass++) {
7150                         tmp = neon_load_reg(rm, pass * 2);
7151                         gen_neon_widen(cpu_V0, tmp, size, op & 1);
7152                         tmp = neon_load_reg(rm, pass * 2 + 1);
7153                         gen_neon_widen(cpu_V1, tmp, size, op & 1);
7154                         switch (size) {
7155                         case 0: gen_helper_neon_paddl_u16(CPU_V001); break;
7156                         case 1: gen_helper_neon_paddl_u32(CPU_V001); break;
7157                         case 2: tcg_gen_add_i64(CPU_V001); break;
7158                         default: abort();
7159                         }
7160                         if (op >= NEON_2RM_VPADAL) {
7161                             /* Accumulate.  */
7162                             neon_load_reg64(cpu_V1, rd + pass);
7163                             gen_neon_addl(size);
7164                         }
7165                         neon_store_reg64(cpu_V0, rd + pass);
7166                     }
7167                     break;
7168                 case NEON_2RM_VTRN:
7169                     if (size == 2) {
7170                         int n;
7171                         for (n = 0; n < (q ? 4 : 2); n += 2) {
7172                             tmp = neon_load_reg(rm, n);
7173                             tmp2 = neon_load_reg(rd, n + 1);
7174                             neon_store_reg(rm, n, tmp2);
7175                             neon_store_reg(rd, n + 1, tmp);
7176                         }
7177                     } else {
7178                         goto elementwise;
7179                     }
7180                     break;
7181                 case NEON_2RM_VUZP:
7182                     if (gen_neon_unzip(rd, rm, size, q)) {
7183                         return 1;
7184                     }
7185                     break;
7186                 case NEON_2RM_VZIP:
7187                     if (gen_neon_zip(rd, rm, size, q)) {
7188                         return 1;
7189                     }
7190                     break;
7191                 case NEON_2RM_VMOVN: case NEON_2RM_VQMOVN:
7192                     /* also VQMOVUN; op field and mnemonics don't line up */
7193                     if (rm & 1) {
7194                         return 1;
7195                     }
7196                     tmp2 = NULL;
7197                     for (pass = 0; pass < 2; pass++) {
7198                         neon_load_reg64(cpu_V0, rm + pass);
7199                         tmp = tcg_temp_new_i32();
7200                         gen_neon_narrow_op(op == NEON_2RM_VMOVN, q, size,
7201                                            tmp, cpu_V0);
7202                         if (pass == 0) {
7203                             tmp2 = tmp;
7204                         } else {
7205                             neon_store_reg(rd, 0, tmp2);
7206                             neon_store_reg(rd, 1, tmp);
7207                         }
7208                     }
7209                     break;
7210                 case NEON_2RM_VSHLL:
7211                     if (q || (rd & 1)) {
7212                         return 1;
7213                     }
7214                     tmp = neon_load_reg(rm, 0);
7215                     tmp2 = neon_load_reg(rm, 1);
7216                     for (pass = 0; pass < 2; pass++) {
7217                         if (pass == 1)
7218                             tmp = tmp2;
7219                         gen_neon_widen(cpu_V0, tmp, size, 1);
7220                         tcg_gen_shli_i64(cpu_V0, cpu_V0, 8 << size);
7221                         neon_store_reg64(cpu_V0, rd + pass);
7222                     }
7223                     break;
7224                 case NEON_2RM_VCVT_F16_F32:
7225                     if (!arm_dc_feature(s, ARM_FEATURE_VFP_FP16) ||
7226                         q || (rm & 1)) {
7227                         return 1;
7228                     }
7229                     tmp = tcg_temp_new_i32();
7230                     tmp2 = tcg_temp_new_i32();
7231                     tcg_gen_ld_f32(cpu_F0s, cpu_env, neon_reg_offset(rm, 0));
7232                     gen_helper_neon_fcvt_f32_to_f16(tmp, cpu_F0s, cpu_env);
7233                     tcg_gen_ld_f32(cpu_F0s, cpu_env, neon_reg_offset(rm, 1));
7234                     gen_helper_neon_fcvt_f32_to_f16(tmp2, cpu_F0s, cpu_env);
7235                     tcg_gen_shli_i32(tmp2, tmp2, 16);
7236                     tcg_gen_or_i32(tmp2, tmp2, tmp);
7237                     tcg_gen_ld_f32(cpu_F0s, cpu_env, neon_reg_offset(rm, 2));
7238                     gen_helper_neon_fcvt_f32_to_f16(tmp, cpu_F0s, cpu_env);
7239                     tcg_gen_ld_f32(cpu_F0s, cpu_env, neon_reg_offset(rm, 3));
7240                     neon_store_reg(rd, 0, tmp2);
7241                     tmp2 = tcg_temp_new_i32();
7242                     gen_helper_neon_fcvt_f32_to_f16(tmp2, cpu_F0s, cpu_env);
7243                     tcg_gen_shli_i32(tmp2, tmp2, 16);
7244                     tcg_gen_or_i32(tmp2, tmp2, tmp);
7245                     neon_store_reg(rd, 1, tmp2);
7246                     tcg_temp_free_i32(tmp);
7247                     break;
7248                 case NEON_2RM_VCVT_F32_F16:
7249                     if (!arm_dc_feature(s, ARM_FEATURE_VFP_FP16) ||
7250                         q || (rd & 1)) {
7251                         return 1;
7252                     }
7253                     tmp3 = tcg_temp_new_i32();
7254                     tmp = neon_load_reg(rm, 0);
7255                     tmp2 = neon_load_reg(rm, 1);
7256                     tcg_gen_ext16u_i32(tmp3, tmp);
7257                     gen_helper_neon_fcvt_f16_to_f32(cpu_F0s, tmp3, cpu_env);
7258                     tcg_gen_st_f32(cpu_F0s, cpu_env, neon_reg_offset(rd, 0));
7259                     tcg_gen_shri_i32(tmp3, tmp, 16);
7260                     gen_helper_neon_fcvt_f16_to_f32(cpu_F0s, tmp3, cpu_env);
7261                     tcg_gen_st_f32(cpu_F0s, cpu_env, neon_reg_offset(rd, 1));
7262                     tcg_temp_free_i32(tmp);
7263                     tcg_gen_ext16u_i32(tmp3, tmp2);
7264                     gen_helper_neon_fcvt_f16_to_f32(cpu_F0s, tmp3, cpu_env);
7265                     tcg_gen_st_f32(cpu_F0s, cpu_env, neon_reg_offset(rd, 2));
7266                     tcg_gen_shri_i32(tmp3, tmp2, 16);
7267                     gen_helper_neon_fcvt_f16_to_f32(cpu_F0s, tmp3, cpu_env);
7268                     tcg_gen_st_f32(cpu_F0s, cpu_env, neon_reg_offset(rd, 3));
7269                     tcg_temp_free_i32(tmp2);
7270                     tcg_temp_free_i32(tmp3);
7271                     break;
7272                 case NEON_2RM_AESE: case NEON_2RM_AESMC:
7273                     if (!arm_dc_feature(s, ARM_FEATURE_V8_AES)
7274                         || ((rm | rd) & 1)) {
7275                         return 1;
7276                     }
7277                     ptr1 = vfp_reg_ptr(true, rd);
7278                     ptr2 = vfp_reg_ptr(true, rm);
7279
7280                      /* Bit 6 is the lowest opcode bit; it distinguishes between
7281                       * encryption (AESE/AESMC) and decryption (AESD/AESIMC)
7282                       */
7283                     tmp3 = tcg_const_i32(extract32(insn, 6, 1));
7284
7285                     if (op == NEON_2RM_AESE) {
7286                         gen_helper_crypto_aese(ptr1, ptr2, tmp3);
7287                     } else {
7288                         gen_helper_crypto_aesmc(ptr1, ptr2, tmp3);
7289                     }
7290                     tcg_temp_free_ptr(ptr1);
7291                     tcg_temp_free_ptr(ptr2);
7292                     tcg_temp_free_i32(tmp3);
7293                     break;
7294                 case NEON_2RM_SHA1H:
7295                     if (!arm_dc_feature(s, ARM_FEATURE_V8_SHA1)
7296                         || ((rm | rd) & 1)) {
7297                         return 1;
7298                     }
7299                     ptr1 = vfp_reg_ptr(true, rd);
7300                     ptr2 = vfp_reg_ptr(true, rm);
7301
7302                     gen_helper_crypto_sha1h(ptr1, ptr2);
7303
7304                     tcg_temp_free_ptr(ptr1);
7305                     tcg_temp_free_ptr(ptr2);
7306                     break;
7307                 case NEON_2RM_SHA1SU1:
7308                     if ((rm | rd) & 1) {
7309                             return 1;
7310                     }
7311                     /* bit 6 (q): set -> SHA256SU0, cleared -> SHA1SU1 */
7312                     if (q) {
7313                         if (!arm_dc_feature(s, ARM_FEATURE_V8_SHA256)) {
7314                             return 1;
7315                         }
7316                     } else if (!arm_dc_feature(s, ARM_FEATURE_V8_SHA1)) {
7317                         return 1;
7318                     }
7319                     ptr1 = vfp_reg_ptr(true, rd);
7320                     ptr2 = vfp_reg_ptr(true, rm);
7321                     if (q) {
7322                         gen_helper_crypto_sha256su0(ptr1, ptr2);
7323                     } else {
7324                         gen_helper_crypto_sha1su1(ptr1, ptr2);
7325                     }
7326                     tcg_temp_free_ptr(ptr1);
7327                     tcg_temp_free_ptr(ptr2);
7328                     break;
7329                 default:
7330                 elementwise:
7331                     for (pass = 0; pass < (q ? 4 : 2); pass++) {
7332                         if (neon_2rm_is_float_op(op)) {
7333                             tcg_gen_ld_f32(cpu_F0s, cpu_env,
7334                                            neon_reg_offset(rm, pass));
7335                             tmp = NULL;
7336                         } else {
7337                             tmp = neon_load_reg(rm, pass);
7338                         }
7339                         switch (op) {
7340                         case NEON_2RM_VREV32:
7341                             switch (size) {
7342                             case 0: tcg_gen_bswap32_i32(tmp, tmp); break;
7343                             case 1: gen_swap_half(tmp); break;
7344                             default: abort();
7345                             }
7346                             break;
7347                         case NEON_2RM_VREV16:
7348                             gen_rev16(tmp);
7349                             break;
7350                         case NEON_2RM_VCLS:
7351                             switch (size) {
7352                             case 0: gen_helper_neon_cls_s8(tmp, tmp); break;
7353                             case 1: gen_helper_neon_cls_s16(tmp, tmp); break;
7354                             case 2: gen_helper_neon_cls_s32(tmp, tmp); break;
7355                             default: abort();
7356                             }
7357                             break;
7358                         case NEON_2RM_VCLZ:
7359                             switch (size) {
7360                             case 0: gen_helper_neon_clz_u8(tmp, tmp); break;
7361                             case 1: gen_helper_neon_clz_u16(tmp, tmp); break;
7362                             case 2: tcg_gen_clzi_i32(tmp, tmp, 32); break;
7363                             default: abort();
7364                             }
7365                             break;
7366                         case NEON_2RM_VCNT:
7367                             gen_helper_neon_cnt_u8(tmp, tmp);
7368                             break;
7369                         case NEON_2RM_VMVN:
7370                             tcg_gen_not_i32(tmp, tmp);
7371                             break;
7372                         case NEON_2RM_VQABS:
7373                             switch (size) {
7374                             case 0:
7375                                 gen_helper_neon_qabs_s8(tmp, cpu_env, tmp);
7376                                 break;
7377                             case 1:
7378                                 gen_helper_neon_qabs_s16(tmp, cpu_env, tmp);
7379                                 break;
7380                             case 2:
7381                                 gen_helper_neon_qabs_s32(tmp, cpu_env, tmp);
7382                                 break;
7383                             default: abort();
7384                             }
7385                             break;
7386                         case NEON_2RM_VQNEG:
7387                             switch (size) {
7388                             case 0:
7389                                 gen_helper_neon_qneg_s8(tmp, cpu_env, tmp);
7390                                 break;
7391                             case 1:
7392                                 gen_helper_neon_qneg_s16(tmp, cpu_env, tmp);
7393                                 break;
7394                             case 2:
7395                                 gen_helper_neon_qneg_s32(tmp, cpu_env, tmp);
7396                                 break;
7397                             default: abort();
7398                             }
7399                             break;
7400                         case NEON_2RM_VCGT0: case NEON_2RM_VCLE0:
7401                             tmp2 = tcg_const_i32(0);
7402                             switch(size) {
7403                             case 0: gen_helper_neon_cgt_s8(tmp, tmp, tmp2); break;
7404                             case 1: gen_helper_neon_cgt_s16(tmp, tmp, tmp2); break;
7405                             case 2: gen_helper_neon_cgt_s32(tmp, tmp, tmp2); break;
7406                             default: abort();
7407                             }
7408                             tcg_temp_free_i32(tmp2);
7409                             if (op == NEON_2RM_VCLE0) {
7410                                 tcg_gen_not_i32(tmp, tmp);
7411                             }
7412                             break;
7413                         case NEON_2RM_VCGE0: case NEON_2RM_VCLT0:
7414                             tmp2 = tcg_const_i32(0);
7415                             switch(size) {
7416                             case 0: gen_helper_neon_cge_s8(tmp, tmp, tmp2); break;
7417                             case 1: gen_helper_neon_cge_s16(tmp, tmp, tmp2); break;
7418                             case 2: gen_helper_neon_cge_s32(tmp, tmp, tmp2); break;
7419                             default: abort();
7420                             }
7421                             tcg_temp_free_i32(tmp2);
7422                             if (op == NEON_2RM_VCLT0) {
7423                                 tcg_gen_not_i32(tmp, tmp);
7424                             }
7425                             break;
7426                         case NEON_2RM_VCEQ0:
7427                             tmp2 = tcg_const_i32(0);
7428                             switch(size) {
7429                             case 0: gen_helper_neon_ceq_u8(tmp, tmp, tmp2); break;
7430                             case 1: gen_helper_neon_ceq_u16(tmp, tmp, tmp2); break;
7431                             case 2: gen_helper_neon_ceq_u32(tmp, tmp, tmp2); break;
7432                             default: abort();
7433                             }
7434                             tcg_temp_free_i32(tmp2);
7435                             break;
7436                         case NEON_2RM_VABS:
7437                             switch(size) {
7438                             case 0: gen_helper_neon_abs_s8(tmp, tmp); break;
7439                             case 1: gen_helper_neon_abs_s16(tmp, tmp); break;
7440                             case 2: tcg_gen_abs_i32(tmp, tmp); break;
7441                             default: abort();
7442                             }
7443                             break;
7444                         case NEON_2RM_VNEG:
7445                             tmp2 = tcg_const_i32(0);
7446                             gen_neon_rsb(size, tmp, tmp2);
7447                             tcg_temp_free_i32(tmp2);
7448                             break;
7449                         case NEON_2RM_VCGT0_F:
7450                         {
7451                             TCGv_ptr fpstatus = get_fpstatus_ptr(1);
7452                             tmp2 = tcg_const_i32(0);
7453                             gen_helper_neon_cgt_f32(tmp, tmp, tmp2, fpstatus);
7454                             tcg_temp_free_i32(tmp2);
7455                             tcg_temp_free_ptr(fpstatus);
7456                             break;
7457                         }
7458                         case NEON_2RM_VCGE0_F:
7459                         {
7460                             TCGv_ptr fpstatus = get_fpstatus_ptr(1);
7461                             tmp2 = tcg_const_i32(0);
7462                             gen_helper_neon_cge_f32(tmp, tmp, tmp2, fpstatus);
7463                             tcg_temp_free_i32(tmp2);
7464                             tcg_temp_free_ptr(fpstatus);
7465                             break;
7466                         }
7467                         case NEON_2RM_VCEQ0_F:
7468                         {
7469                             TCGv_ptr fpstatus = get_fpstatus_ptr(1);
7470                             tmp2 = tcg_const_i32(0);
7471                             gen_helper_neon_ceq_f32(tmp, tmp, tmp2, fpstatus);
7472                             tcg_temp_free_i32(tmp2);
7473                             tcg_temp_free_ptr(fpstatus);
7474                             break;
7475                         }
7476                         case NEON_2RM_VCLE0_F:
7477                         {
7478                             TCGv_ptr fpstatus = get_fpstatus_ptr(1);
7479                             tmp2 = tcg_const_i32(0);
7480                             gen_helper_neon_cge_f32(tmp, tmp2, tmp, fpstatus);
7481                             tcg_temp_free_i32(tmp2);
7482                             tcg_temp_free_ptr(fpstatus);
7483                             break;
7484                         }
7485                         case NEON_2RM_VCLT0_F:
7486                         {
7487                             TCGv_ptr fpstatus = get_fpstatus_ptr(1);
7488                             tmp2 = tcg_const_i32(0);
7489                             gen_helper_neon_cgt_f32(tmp, tmp2, tmp, fpstatus);
7490                             tcg_temp_free_i32(tmp2);
7491                             tcg_temp_free_ptr(fpstatus);
7492                             break;
7493                         }
7494                         case NEON_2RM_VABS_F:
7495                             gen_vfp_abs(0);
7496                             break;
7497                         case NEON_2RM_VNEG_F:
7498                             gen_vfp_neg(0);
7499                             break;
7500                         case NEON_2RM_VSWP:
7501                             tmp2 = neon_load_reg(rd, pass);
7502                             neon_store_reg(rm, pass, tmp2);
7503                             break;
7504                         case NEON_2RM_VTRN:
7505                             tmp2 = neon_load_reg(rd, pass);
7506                             switch (size) {
7507                             case 0: gen_neon_trn_u8(tmp, tmp2); break;
7508                             case 1: gen_neon_trn_u16(tmp, tmp2); break;
7509                             default: abort();
7510                             }
7511                             neon_store_reg(rm, pass, tmp2);
7512                             break;
7513                         case NEON_2RM_VRINTN:
7514                         case NEON_2RM_VRINTA:
7515                         case NEON_2RM_VRINTM:
7516                         case NEON_2RM_VRINTP:
7517                         case NEON_2RM_VRINTZ:
7518                         {
7519                             TCGv_i32 tcg_rmode;
7520                             TCGv_ptr fpstatus = get_fpstatus_ptr(1);
7521                             int rmode;
7522
7523                             if (op == NEON_2RM_VRINTZ) {
7524                                 rmode = FPROUNDING_ZERO;
7525                             } else {
7526                                 rmode = fp_decode_rm[((op & 0x6) >> 1) ^ 1];
7527                             }
7528
7529                             tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rmode));
7530                             gen_helper_set_neon_rmode(tcg_rmode, tcg_rmode,
7531                                                       cpu_env);
7532                             gen_helper_rints(cpu_F0s, cpu_F0s, fpstatus);
7533                             gen_helper_set_neon_rmode(tcg_rmode, tcg_rmode,
7534                                                       cpu_env);
7535                             tcg_temp_free_ptr(fpstatus);
7536                             tcg_temp_free_i32(tcg_rmode);
7537                             break;
7538                         }
7539                         case NEON_2RM_VRINTX:
7540                         {
7541                             TCGv_ptr fpstatus = get_fpstatus_ptr(1);
7542                             gen_helper_rints_exact(cpu_F0s, cpu_F0s, fpstatus);
7543                             tcg_temp_free_ptr(fpstatus);
7544                             break;
7545                         }
7546                         case NEON_2RM_VCVTAU:
7547                         case NEON_2RM_VCVTAS:
7548                         case NEON_2RM_VCVTNU:
7549                         case NEON_2RM_VCVTNS:
7550                         case NEON_2RM_VCVTPU:
7551                         case NEON_2RM_VCVTPS:
7552                         case NEON_2RM_VCVTMU:
7553                         case NEON_2RM_VCVTMS:
7554                         {
7555                             bool is_signed = !extract32(insn, 7, 1);
7556                             TCGv_ptr fpst = get_fpstatus_ptr(1);
7557                             TCGv_i32 tcg_rmode, tcg_shift;
7558                             int rmode = fp_decode_rm[extract32(insn, 8, 2)];
7559
7560                             tcg_shift = tcg_const_i32(0);
7561                             tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rmode));
7562                             gen_helper_set_neon_rmode(tcg_rmode, tcg_rmode,
7563                                                       cpu_env);
7564
7565                             if (is_signed) {
7566                                 gen_helper_vfp_tosls(cpu_F0s, cpu_F0s,
7567                                                      tcg_shift, fpst);
7568                             } else {
7569                                 gen_helper_vfp_touls(cpu_F0s, cpu_F0s,
7570                                                      tcg_shift, fpst);
7571                             }
7572
7573                             gen_helper_set_neon_rmode(tcg_rmode, tcg_rmode,
7574                                                       cpu_env);
7575                             tcg_temp_free_i32(tcg_rmode);
7576                             tcg_temp_free_i32(tcg_shift);
7577                             tcg_temp_free_ptr(fpst);
7578                             break;
7579                         }
7580                         case NEON_2RM_VRECPE:
7581                         {
7582                             TCGv_ptr fpstatus = get_fpstatus_ptr(1);
7583                             gen_helper_recpe_u32(tmp, tmp, fpstatus);
7584                             tcg_temp_free_ptr(fpstatus);
7585                             break;
7586                         }
7587                         case NEON_2RM_VRSQRTE:
7588                         {
7589                             TCGv_ptr fpstatus = get_fpstatus_ptr(1);
7590                             gen_helper_rsqrte_u32(tmp, tmp, fpstatus);
7591                             tcg_temp_free_ptr(fpstatus);
7592                             break;
7593                         }
7594                         case NEON_2RM_VRECPE_F:
7595                         {
7596                             TCGv_ptr fpstatus = get_fpstatus_ptr(1);
7597                             gen_helper_recpe_f32(cpu_F0s, cpu_F0s, fpstatus);
7598                             tcg_temp_free_ptr(fpstatus);
7599                             break;
7600                         }
7601                         case NEON_2RM_VRSQRTE_F:
7602                         {
7603                             TCGv_ptr fpstatus = get_fpstatus_ptr(1);
7604                             gen_helper_rsqrte_f32(cpu_F0s, cpu_F0s, fpstatus);
7605                             tcg_temp_free_ptr(fpstatus);
7606                             break;
7607                         }
7608                         case NEON_2RM_VCVT_FS: /* VCVT.F32.S32 */
7609                             gen_vfp_sito(0, 1);
7610                             break;
7611                         case NEON_2RM_VCVT_FU: /* VCVT.F32.U32 */
7612                             gen_vfp_uito(0, 1);
7613                             break;
7614                         case NEON_2RM_VCVT_SF: /* VCVT.S32.F32 */
7615                             gen_vfp_tosiz(0, 1);
7616                             break;
7617                         case NEON_2RM_VCVT_UF: /* VCVT.U32.F32 */
7618                             gen_vfp_touiz(0, 1);
7619                             break;
7620                         default:
7621                             /* Reserved op values were caught by the
7622                              * neon_2rm_sizes[] check earlier.
7623                              */
7624                             abort();
7625                         }
7626                         if (neon_2rm_is_float_op(op)) {
7627                             tcg_gen_st_f32(cpu_F0s, cpu_env,
7628                                            neon_reg_offset(rd, pass));
7629                         } else {
7630                             neon_store_reg(rd, pass, tmp);
7631                         }
7632                     }
7633                     break;
7634                 }
7635             } else if ((insn & (1 << 10)) == 0) {
7636                 /* VTBL, VTBX.  */
7637                 int n = ((insn >> 8) & 3) + 1;
7638                 if ((rn + n) > 32) {
7639                     /* This is UNPREDICTABLE; we choose to UNDEF to avoid the
7640                      * helper function running off the end of the register file.
7641                      */
7642                     return 1;
7643                 }
7644                 n <<= 3;
7645                 if (insn & (1 << 6)) {
7646                     tmp = neon_load_reg(rd, 0);
7647                 } else {
7648                     tmp = tcg_temp_new_i32();
7649                     tcg_gen_movi_i32(tmp, 0);
7650                 }
7651                 tmp2 = neon_load_reg(rm, 0);
7652                 ptr1 = vfp_reg_ptr(true, rn);
7653                 tmp5 = tcg_const_i32(n);
7654                 gen_helper_neon_tbl(tmp2, tmp2, tmp, ptr1, tmp5);
7655                 tcg_temp_free_i32(tmp);
7656                 if (insn & (1 << 6)) {
7657                     tmp = neon_load_reg(rd, 1);
7658                 } else {
7659                     tmp = tcg_temp_new_i32();
7660                     tcg_gen_movi_i32(tmp, 0);
7661                 }
7662                 tmp3 = neon_load_reg(rm, 1);
7663                 gen_helper_neon_tbl(tmp3, tmp3, tmp, ptr1, tmp5);
7664                 tcg_temp_free_i32(tmp5);
7665                 tcg_temp_free_ptr(ptr1);
7666                 neon_store_reg(rd, 0, tmp2);
7667                 neon_store_reg(rd, 1, tmp3);
7668                 tcg_temp_free_i32(tmp);
7669             } else if ((insn & 0x380) == 0) {
7670                 /* VDUP */
7671                 if ((insn & (7 << 16)) == 0 || (q && (rd & 1))) {
7672                     return 1;
7673                 }
7674                 if (insn & (1 << 19)) {
7675                     tmp = neon_load_reg(rm, 1);
7676                 } else {
7677                     tmp = neon_load_reg(rm, 0);
7678                 }
7679                 if (insn & (1 << 16)) {
7680                     gen_neon_dup_u8(tmp, ((insn >> 17) & 3) * 8);
7681                 } else if (insn & (1 << 17)) {
7682                     if ((insn >> 18) & 1)
7683                         gen_neon_dup_high16(tmp);
7684                     else
7685                         gen_neon_dup_low16(tmp);
7686                 }
7687                 for (pass = 0; pass < (q ? 4 : 2); pass++) {
7688                     tmp2 = tcg_temp_new_i32();
7689                     tcg_gen_mov_i32(tmp2, tmp);
7690                     neon_store_reg(rd, pass, tmp2);
7691                 }
7692                 tcg_temp_free_i32(tmp);
7693             } else {
7694                 return 1;
7695             }
7696         }
7697     }
7698     return 0;
7699 }
7700
7701 /* Advanced SIMD three registers of the same length extension.
7702  *  31           25    23  22    20   16   12  11   10   9    8        3     0
7703  * +---------------+-----+---+-----+----+----+---+----+---+----+---------+----+
7704  * | 1 1 1 1 1 1 0 | op1 | D | op2 | Vn | Vd | 1 | o3 | 0 | o4 | N Q M U | Vm |
7705  * +---------------+-----+---+-----+----+----+---+----+---+----+---------+----+
7706  */
7707 static int disas_neon_insn_3same_ext(DisasContext *s, uint32_t insn)
7708 {
7709     gen_helper_gvec_3_ptr *fn_gvec_ptr;
7710     int rd, rn, rm, rot, size, opr_sz;
7711     TCGv_ptr fpst;
7712     bool q;
7713
7714     q = extract32(insn, 6, 1);
7715     VFP_DREG_D(rd, insn);
7716     VFP_DREG_N(rn, insn);
7717     VFP_DREG_M(rm, insn);
7718     if ((rd | rn | rm) & q) {
7719         return 1;
7720     }
7721
7722     if ((insn & 0xfe200f10) == 0xfc200800) {
7723         /* VCMLA -- 1111 110R R.1S .... .... 1000 ...0 .... */
7724         size = extract32(insn, 20, 1);
7725         rot = extract32(insn, 23, 2);
7726         if (!arm_dc_feature(s, ARM_FEATURE_V8_FCMA)
7727             || (!size && !arm_dc_feature(s, ARM_FEATURE_V8_FP16))) {
7728             return 1;
7729         }
7730         fn_gvec_ptr = size ? gen_helper_gvec_fcmlas : gen_helper_gvec_fcmlah;
7731     } else if ((insn & 0xfea00f10) == 0xfc800800) {
7732         /* VCADD -- 1111 110R 1.0S .... .... 1000 ...0 .... */
7733         size = extract32(insn, 20, 1);
7734         rot = extract32(insn, 24, 1);
7735         if (!arm_dc_feature(s, ARM_FEATURE_V8_FCMA)
7736             || (!size && !arm_dc_feature(s, ARM_FEATURE_V8_FP16))) {
7737             return 1;
7738         }
7739         fn_gvec_ptr = size ? gen_helper_gvec_fcadds : gen_helper_gvec_fcaddh;
7740     } else {
7741         return 1;
7742     }
7743
7744     if (s->fp_excp_el) {
7745         gen_exception_insn(s, 4, EXCP_UDEF,
7746                            syn_fp_access_trap(1, 0xe, false), s->fp_excp_el);
7747         return 0;
7748     }
7749     if (!s->vfp_enabled) {
7750         return 1;
7751     }
7752
7753     opr_sz = (1 + q) * 8;
7754     fpst = get_fpstatus_ptr(1);
7755     tcg_gen_gvec_3_ptr(vfp_reg_offset(1, rd),
7756                        vfp_reg_offset(1, rn),
7757                        vfp_reg_offset(1, rm), fpst,
7758                        opr_sz, opr_sz, rot, fn_gvec_ptr);
7759     tcg_temp_free_ptr(fpst);
7760     return 0;
7761 }
7762
7763 /* Advanced SIMD two registers and a scalar extension.
7764  *  31             24   23  22   20   16   12  11   10   9    8        3     0
7765  * +-----------------+----+---+----+----+----+---+----+---+----+---------+----+
7766  * | 1 1 1 1 1 1 1 0 | o1 | D | o2 | Vn | Vd | 1 | o3 | 0 | o4 | N Q M U | Vm |
7767  * +-----------------+----+---+----+----+----+---+----+---+----+---------+----+
7768  *
7769  */
7770
7771 static int disas_neon_insn_2reg_scalar_ext(DisasContext *s, uint32_t insn)
7772 {
7773     int rd, rn, rm, rot, size, opr_sz;
7774     TCGv_ptr fpst;
7775     bool q;
7776
7777     q = extract32(insn, 6, 1);
7778     VFP_DREG_D(rd, insn);
7779     VFP_DREG_N(rn, insn);
7780     VFP_DREG_M(rm, insn);
7781     if ((rd | rn) & q) {
7782         return 1;
7783     }
7784
7785     if ((insn & 0xff000f10) == 0xfe000800) {
7786         /* VCMLA (indexed) -- 1111 1110 S.RR .... .... 1000 ...0 .... */
7787         rot = extract32(insn, 20, 2);
7788         size = extract32(insn, 23, 1);
7789         if (!arm_dc_feature(s, ARM_FEATURE_V8_FCMA)
7790             || (!size && !arm_dc_feature(s, ARM_FEATURE_V8_FP16))) {
7791             return 1;
7792         }
7793     } else {
7794         return 1;
7795     }
7796
7797     if (s->fp_excp_el) {
7798         gen_exception_insn(s, 4, EXCP_UDEF,
7799                            syn_fp_access_trap(1, 0xe, false), s->fp_excp_el);
7800         return 0;
7801     }
7802     if (!s->vfp_enabled) {
7803         return 1;
7804     }
7805
7806     opr_sz = (1 + q) * 8;
7807     fpst = get_fpstatus_ptr(1);
7808     tcg_gen_gvec_3_ptr(vfp_reg_offset(1, rd),
7809                        vfp_reg_offset(1, rn),
7810                        vfp_reg_offset(1, rm), fpst,
7811                        opr_sz, opr_sz, rot,
7812                        size ? gen_helper_gvec_fcmlas_idx
7813                        : gen_helper_gvec_fcmlah_idx);
7814     tcg_temp_free_ptr(fpst);
7815     return 0;
7816 }
7817
7818 static int disas_coproc_insn(DisasContext *s, uint32_t insn)
7819 {
7820     int cpnum, is64, crn, crm, opc1, opc2, isread, rt, rt2;
7821     const ARMCPRegInfo *ri;
7822
7823     cpnum = (insn >> 8) & 0xf;
7824
7825     /* First check for coprocessor space used for XScale/iwMMXt insns */
7826     if (arm_dc_feature(s, ARM_FEATURE_XSCALE) && (cpnum < 2)) {
7827         if (extract32(s->c15_cpar, cpnum, 1) == 0) {
7828             return 1;
7829         }
7830         if (arm_dc_feature(s, ARM_FEATURE_IWMMXT)) {
7831             return disas_iwmmxt_insn(s, insn);
7832         } else if (arm_dc_feature(s, ARM_FEATURE_XSCALE)) {
7833             return disas_dsp_insn(s, insn);
7834         }
7835         return 1;
7836     }
7837
7838     /* Otherwise treat as a generic register access */
7839     is64 = (insn & (1 << 25)) == 0;
7840     if (!is64 && ((insn & (1 << 4)) == 0)) {
7841         /* cdp */
7842         return 1;
7843     }
7844
7845     crm = insn & 0xf;
7846     if (is64) {
7847         crn = 0;
7848         opc1 = (insn >> 4) & 0xf;
7849         opc2 = 0;
7850         rt2 = (insn >> 16) & 0xf;
7851     } else {
7852         crn = (insn >> 16) & 0xf;
7853         opc1 = (insn >> 21) & 7;
7854         opc2 = (insn >> 5) & 7;
7855         rt2 = 0;
7856     }
7857     isread = (insn >> 20) & 1;
7858     rt = (insn >> 12) & 0xf;
7859
7860     ri = get_arm_cp_reginfo(s->cp_regs,
7861             ENCODE_CP_REG(cpnum, is64, s->ns, crn, crm, opc1, opc2));
7862     if (ri) {
7863         /* Check access permissions */
7864         if (!cp_access_ok(s->current_el, ri, isread)) {
7865             return 1;
7866         }
7867
7868         if (ri->accessfn ||
7869             (arm_dc_feature(s, ARM_FEATURE_XSCALE) && cpnum < 14)) {
7870             /* Emit code to perform further access permissions checks at
7871              * runtime; this may result in an exception.
7872              * Note that on XScale all cp0..c13 registers do an access check
7873              * call in order to handle c15_cpar.
7874              */
7875             TCGv_ptr tmpptr;
7876             TCGv_i32 tcg_syn, tcg_isread;
7877             uint32_t syndrome;
7878
7879             /* Note that since we are an implementation which takes an
7880              * exception on a trapped conditional instruction only if the
7881              * instruction passes its condition code check, we can take
7882              * advantage of the clause in the ARM ARM that allows us to set
7883              * the COND field in the instruction to 0xE in all cases.
7884              * We could fish the actual condition out of the insn (ARM)
7885              * or the condexec bits (Thumb) but it isn't necessary.
7886              */
7887             switch (cpnum) {
7888             case 14:
7889                 if (is64) {
7890                     syndrome = syn_cp14_rrt_trap(1, 0xe, opc1, crm, rt, rt2,
7891                                                  isread, false);
7892                 } else {
7893                     syndrome = syn_cp14_rt_trap(1, 0xe, opc1, opc2, crn, crm,
7894                                                 rt, isread, false);
7895                 }
7896                 break;
7897             case 15:
7898                 if (is64) {
7899                     syndrome = syn_cp15_rrt_trap(1, 0xe, opc1, crm, rt, rt2,
7900                                                  isread, false);
7901                 } else {
7902                     syndrome = syn_cp15_rt_trap(1, 0xe, opc1, opc2, crn, crm,
7903                                                 rt, isread, false);
7904                 }
7905                 break;
7906             default:
7907                 /* ARMv8 defines that only coprocessors 14 and 15 exist,
7908                  * so this can only happen if this is an ARMv7 or earlier CPU,
7909                  * in which case the syndrome information won't actually be
7910                  * guest visible.
7911                  */
7912                 assert(!arm_dc_feature(s, ARM_FEATURE_V8));
7913                 syndrome = syn_uncategorized();
7914                 break;
7915             }
7916
7917             gen_set_condexec(s);
7918             gen_set_pc_im(s, s->pc - 4);
7919             tmpptr = tcg_const_ptr(ri);
7920             tcg_syn = tcg_const_i32(syndrome);
7921             tcg_isread = tcg_const_i32(isread);
7922             gen_helper_access_check_cp_reg(cpu_env, tmpptr, tcg_syn,
7923                                            tcg_isread);
7924             tcg_temp_free_ptr(tmpptr);
7925             tcg_temp_free_i32(tcg_syn);
7926             tcg_temp_free_i32(tcg_isread);
7927         }
7928
7929         /* Handle special cases first */
7930         switch (ri->type & ~(ARM_CP_FLAG_MASK & ~ARM_CP_SPECIAL)) {
7931         case ARM_CP_NOP:
7932             return 0;
7933         case ARM_CP_WFI:
7934             if (isread) {
7935                 return 1;
7936             }
7937             gen_set_pc_im(s, s->pc);
7938             s->base.is_jmp = DISAS_WFI;
7939             return 0;
7940         default:
7941             break;
7942         }
7943
7944         if ((tb_cflags(s->base.tb) & CF_USE_ICOUNT) && (ri->type & ARM_CP_IO)) {
7945             gen_io_start();
7946         }
7947
7948         if (isread) {
7949             /* Read */
7950             if (is64) {
7951                 TCGv_i64 tmp64;
7952                 TCGv_i32 tmp;
7953                 if (ri->type & ARM_CP_CONST) {
7954                     tmp64 = tcg_const_i64(ri->resetvalue);
7955                 } else if (ri->readfn) {
7956                     TCGv_ptr tmpptr;
7957                     tmp64 = tcg_temp_new_i64();
7958                     tmpptr = tcg_const_ptr(ri);
7959                     gen_helper_get_cp_reg64(tmp64, cpu_env, tmpptr);
7960                     tcg_temp_free_ptr(tmpptr);
7961                 } else {
7962                     tmp64 = tcg_temp_new_i64();
7963                     tcg_gen_ld_i64(tmp64, cpu_env, ri->fieldoffset);
7964                 }
7965                 tmp = tcg_temp_new_i32();
7966                 tcg_gen_extrl_i64_i32(tmp, tmp64);
7967                 store_reg(s, rt, tmp);
7968                 tcg_gen_shri_i64(tmp64, tmp64, 32);
7969                 tmp = tcg_temp_new_i32();
7970                 tcg_gen_extrl_i64_i32(tmp, tmp64);
7971                 tcg_temp_free_i64(tmp64);
7972                 store_reg(s, rt2, tmp);
7973             } else {
7974                 TCGv_i32 tmp;
7975                 if (ri->type & ARM_CP_CONST) {
7976                     tmp = tcg_const_i32(ri->resetvalue);
7977                 } else if (ri->readfn) {
7978                     TCGv_ptr tmpptr;
7979                     tmp = tcg_temp_new_i32();
7980                     tmpptr = tcg_const_ptr(ri);
7981                     gen_helper_get_cp_reg(tmp, cpu_env, tmpptr);
7982                     tcg_temp_free_ptr(tmpptr);
7983                 } else {
7984                     tmp = load_cpu_offset(ri->fieldoffset);
7985                 }
7986                 if (rt == 15) {
7987                     /* Destination register of r15 for 32 bit loads sets
7988                      * the condition codes from the high 4 bits of the value
7989                      */
7990                     gen_set_nzcv(tmp);
7991                     tcg_temp_free_i32(tmp);
7992                 } else {
7993                     store_reg(s, rt, tmp);
7994                 }
7995             }
7996         } else {
7997             /* Write */
7998             if (ri->type & ARM_CP_CONST) {
7999                 /* If not forbidden by access permissions, treat as WI */
8000                 return 0;
8001             }
8002
8003             if (is64) {
8004                 TCGv_i32 tmplo, tmphi;
8005                 TCGv_i64 tmp64 = tcg_temp_new_i64();
8006                 tmplo = load_reg(s, rt);
8007                 tmphi = load_reg(s, rt2);
8008                 tcg_gen_concat_i32_i64(tmp64, tmplo, tmphi);
8009                 tcg_temp_free_i32(tmplo);
8010                 tcg_temp_free_i32(tmphi);
8011                 if (ri->writefn) {
8012                     TCGv_ptr tmpptr = tcg_const_ptr(ri);
8013                     gen_helper_set_cp_reg64(cpu_env, tmpptr, tmp64);
8014                     tcg_temp_free_ptr(tmpptr);
8015                 } else {
8016                     tcg_gen_st_i64(tmp64, cpu_env, ri->fieldoffset);
8017                 }
8018                 tcg_temp_free_i64(tmp64);
8019             } else {
8020                 if (ri->writefn) {
8021                     TCGv_i32 tmp;
8022                     TCGv_ptr tmpptr;
8023                     tmp = load_reg(s, rt);
8024                     tmpptr = tcg_const_ptr(ri);
8025                     gen_helper_set_cp_reg(cpu_env, tmpptr, tmp);
8026                     tcg_temp_free_ptr(tmpptr);
8027                     tcg_temp_free_i32(tmp);
8028                 } else {
8029                     TCGv_i32 tmp = load_reg(s, rt);
8030                     store_cpu_offset(tmp, ri->fieldoffset);
8031                 }
8032             }
8033         }
8034
8035         if ((tb_cflags(s->base.tb) & CF_USE_ICOUNT) && (ri->type & ARM_CP_IO)) {
8036             /* I/O operations must end the TB here (whether read or write) */
8037             gen_io_end();
8038             gen_lookup_tb(s);
8039         } else if (!isread && !(ri->type & ARM_CP_SUPPRESS_TB_END)) {
8040             /* We default to ending the TB on a coprocessor register write,
8041              * but allow this to be suppressed by the register definition
8042              * (usually only necessary to work around guest bugs).
8043              */
8044             gen_lookup_tb(s);
8045         }
8046
8047         return 0;
8048     }
8049
8050     /* Unknown register; this might be a guest error or a QEMU
8051      * unimplemented feature.
8052      */
8053     if (is64) {
8054         qemu_log_mask(LOG_UNIMP, "%s access to unsupported AArch32 "
8055                       "64 bit system register cp:%d opc1: %d crm:%d "
8056                       "(%s)\n",
8057                       isread ? "read" : "write", cpnum, opc1, crm,
8058                       s->ns ? "non-secure" : "secure");
8059     } else {
8060         qemu_log_mask(LOG_UNIMP, "%s access to unsupported AArch32 "
8061                       "system register cp:%d opc1:%d crn:%d crm:%d opc2:%d "
8062                       "(%s)\n",
8063                       isread ? "read" : "write", cpnum, opc1, crn, crm, opc2,
8064                       s->ns ? "non-secure" : "secure");
8065     }
8066
8067     return 1;
8068 }
8069
8070
8071 /* Store a 64-bit value to a register pair.  Clobbers val.  */
8072 static void gen_storeq_reg(DisasContext *s, int rlow, int rhigh, TCGv_i64 val)
8073 {
8074     TCGv_i32 tmp;
8075     tmp = tcg_temp_new_i32();
8076     tcg_gen_extrl_i64_i32(tmp, val);
8077     store_reg(s, rlow, tmp);
8078     tmp = tcg_temp_new_i32();
8079     tcg_gen_shri_i64(val, val, 32);
8080     tcg_gen_extrl_i64_i32(tmp, val);
8081     store_reg(s, rhigh, tmp);
8082 }
8083
8084 /* load a 32-bit value from a register and perform a 64-bit accumulate.  */
8085 static void gen_addq_lo(DisasContext *s, TCGv_i64 val, int rlow)
8086 {
8087     TCGv_i64 tmp;
8088     TCGv_i32 tmp2;
8089
8090     /* Load value and extend to 64 bits.  */
8091     tmp = tcg_temp_new_i64();
8092     tmp2 = load_reg(s, rlow);
8093     tcg_gen_extu_i32_i64(tmp, tmp2);
8094     tcg_temp_free_i32(tmp2);
8095     tcg_gen_add_i64(val, val, tmp);
8096     tcg_temp_free_i64(tmp);
8097 }
8098
8099 /* load and add a 64-bit value from a register pair.  */
8100 static void gen_addq(DisasContext *s, TCGv_i64 val, int rlow, int rhigh)
8101 {
8102     TCGv_i64 tmp;
8103     TCGv_i32 tmpl;
8104     TCGv_i32 tmph;
8105
8106     /* Load 64-bit value rd:rn.  */
8107     tmpl = load_reg(s, rlow);
8108     tmph = load_reg(s, rhigh);
8109     tmp = tcg_temp_new_i64();
8110     tcg_gen_concat_i32_i64(tmp, tmpl, tmph);
8111     tcg_temp_free_i32(tmpl);
8112     tcg_temp_free_i32(tmph);
8113     tcg_gen_add_i64(val, val, tmp);
8114     tcg_temp_free_i64(tmp);
8115 }
8116
8117 /* Set N and Z flags from hi|lo.  */
8118 static void gen_logicq_cc(TCGv_i32 lo, TCGv_i32 hi)
8119 {
8120     tcg_gen_mov_i32(cpu_NF, hi);
8121     tcg_gen_or_i32(cpu_ZF, lo, hi);
8122 }
8123
8124 /* Load/Store exclusive instructions are implemented by remembering
8125    the value/address loaded, and seeing if these are the same
8126    when the store is performed.  This should be sufficient to implement
8127    the architecturally mandated semantics, and avoids having to monitor
8128    regular stores.  The compare vs the remembered value is done during
8129    the cmpxchg operation, but we must compare the addresses manually.  */
8130 static void gen_load_exclusive(DisasContext *s, int rt, int rt2,
8131                                TCGv_i32 addr, int size)
8132 {
8133     TCGv_i32 tmp = tcg_temp_new_i32();
8134     TCGMemOp opc = size | MO_ALIGN | s->be_data;
8135
8136     s->is_ldex = true;
8137
8138     if (size == 3) {
8139         TCGv_i32 tmp2 = tcg_temp_new_i32();
8140         TCGv_i64 t64 = tcg_temp_new_i64();
8141
8142         /* For AArch32, architecturally the 32-bit word at the lowest
8143          * address is always Rt and the one at addr+4 is Rt2, even if
8144          * the CPU is big-endian. That means we don't want to do a
8145          * gen_aa32_ld_i64(), which invokes gen_aa32_frob64() as if
8146          * for an architecturally 64-bit access, but instead do a
8147          * 64-bit access using MO_BE if appropriate and then split
8148          * the two halves.
8149          * This only makes a difference for BE32 user-mode, where
8150          * frob64() must not flip the two halves of the 64-bit data
8151          * but this code must treat BE32 user-mode like BE32 system.
8152          */
8153         TCGv taddr = gen_aa32_addr(s, addr, opc);
8154
8155         tcg_gen_qemu_ld_i64(t64, taddr, get_mem_index(s), opc);
8156         tcg_temp_free(taddr);
8157         tcg_gen_mov_i64(cpu_exclusive_val, t64);
8158         if (s->be_data == MO_BE) {
8159             tcg_gen_extr_i64_i32(tmp2, tmp, t64);
8160         } else {
8161             tcg_gen_extr_i64_i32(tmp, tmp2, t64);
8162         }
8163         tcg_temp_free_i64(t64);
8164
8165         store_reg(s, rt2, tmp2);
8166     } else {
8167         gen_aa32_ld_i32(s, tmp, addr, get_mem_index(s), opc);
8168         tcg_gen_extu_i32_i64(cpu_exclusive_val, tmp);
8169     }
8170
8171     store_reg(s, rt, tmp);
8172     tcg_gen_extu_i32_i64(cpu_exclusive_addr, addr);
8173 }
8174
8175 static void gen_clrex(DisasContext *s)
8176 {
8177     tcg_gen_movi_i64(cpu_exclusive_addr, -1);
8178 }
8179
8180 static void gen_store_exclusive(DisasContext *s, int rd, int rt, int rt2,
8181                                 TCGv_i32 addr, int size)
8182 {
8183     TCGv_i32 t0, t1, t2;
8184     TCGv_i64 extaddr;
8185     TCGv taddr;
8186     TCGLabel *done_label;
8187     TCGLabel *fail_label;
8188     TCGMemOp opc = size | MO_ALIGN | s->be_data;
8189
8190     /* if (env->exclusive_addr == addr && env->exclusive_val == [addr]) {
8191          [addr] = {Rt};
8192          {Rd} = 0;
8193        } else {
8194          {Rd} = 1;
8195        } */
8196     fail_label = gen_new_label();
8197     done_label = gen_new_label();
8198     extaddr = tcg_temp_new_i64();
8199     tcg_gen_extu_i32_i64(extaddr, addr);
8200     tcg_gen_brcond_i64(TCG_COND_NE, extaddr, cpu_exclusive_addr, fail_label);
8201     tcg_temp_free_i64(extaddr);
8202
8203     taddr = gen_aa32_addr(s, addr, opc);
8204     t0 = tcg_temp_new_i32();
8205     t1 = load_reg(s, rt);
8206     if (size == 3) {
8207         TCGv_i64 o64 = tcg_temp_new_i64();
8208         TCGv_i64 n64 = tcg_temp_new_i64();
8209
8210         t2 = load_reg(s, rt2);
8211         /* For AArch32, architecturally the 32-bit word at the lowest
8212          * address is always Rt and the one at addr+4 is Rt2, even if
8213          * the CPU is big-endian. Since we're going to treat this as a
8214          * single 64-bit BE store, we need to put the two halves in the
8215          * opposite order for BE to LE, so that they end up in the right
8216          * places.
8217          * We don't want gen_aa32_frob64() because that does the wrong
8218          * thing for BE32 usermode.
8219          */
8220         if (s->be_data == MO_BE) {
8221             tcg_gen_concat_i32_i64(n64, t2, t1);
8222         } else {
8223             tcg_gen_concat_i32_i64(n64, t1, t2);
8224         }
8225         tcg_temp_free_i32(t2);
8226
8227         tcg_gen_atomic_cmpxchg_i64(o64, taddr, cpu_exclusive_val, n64,
8228                                    get_mem_index(s), opc);
8229         tcg_temp_free_i64(n64);
8230
8231         tcg_gen_setcond_i64(TCG_COND_NE, o64, o64, cpu_exclusive_val);
8232         tcg_gen_extrl_i64_i32(t0, o64);
8233
8234         tcg_temp_free_i64(o64);
8235     } else {
8236         t2 = tcg_temp_new_i32();
8237         tcg_gen_extrl_i64_i32(t2, cpu_exclusive_val);
8238         tcg_gen_atomic_cmpxchg_i32(t0, taddr, t2, t1, get_mem_index(s), opc);
8239         tcg_gen_setcond_i32(TCG_COND_NE, t0, t0, t2);
8240         tcg_temp_free_i32(t2);
8241     }
8242     tcg_temp_free_i32(t1);
8243     tcg_temp_free(taddr);
8244     tcg_gen_mov_i32(cpu_R[rd], t0);
8245     tcg_temp_free_i32(t0);
8246     tcg_gen_br(done_label);
8247
8248     gen_set_label(fail_label);
8249     tcg_gen_movi_i32(cpu_R[rd], 1);
8250     gen_set_label(done_label);
8251     tcg_gen_movi_i64(cpu_exclusive_addr, -1);
8252 }
8253
8254 /* gen_srs:
8255  * @env: CPUARMState
8256  * @s: DisasContext
8257  * @mode: mode field from insn (which stack to store to)
8258  * @amode: addressing mode (DA/IA/DB/IB), encoded as per P,U bits in ARM insn
8259  * @writeback: true if writeback bit set
8260  *
8261  * Generate code for the SRS (Store Return State) insn.
8262  */
8263 static void gen_srs(DisasContext *s,
8264                     uint32_t mode, uint32_t amode, bool writeback)
8265 {
8266     int32_t offset;
8267     TCGv_i32 addr, tmp;
8268     bool undef = false;
8269
8270     /* SRS is:
8271      * - trapped to EL3 if EL3 is AArch64 and we are at Secure EL1
8272      *   and specified mode is monitor mode
8273      * - UNDEFINED in Hyp mode
8274      * - UNPREDICTABLE in User or System mode
8275      * - UNPREDICTABLE if the specified mode is:
8276      * -- not implemented
8277      * -- not a valid mode number
8278      * -- a mode that's at a higher exception level
8279      * -- Monitor, if we are Non-secure
8280      * For the UNPREDICTABLE cases we choose to UNDEF.
8281      */
8282     if (s->current_el == 1 && !s->ns && mode == ARM_CPU_MODE_MON) {
8283         gen_exception_insn(s, 4, EXCP_UDEF, syn_uncategorized(), 3);
8284         return;
8285     }
8286
8287     if (s->current_el == 0 || s->current_el == 2) {
8288         undef = true;
8289     }
8290
8291     switch (mode) {
8292     case ARM_CPU_MODE_USR:
8293     case ARM_CPU_MODE_FIQ:
8294     case ARM_CPU_MODE_IRQ:
8295     case ARM_CPU_MODE_SVC:
8296     case ARM_CPU_MODE_ABT:
8297     case ARM_CPU_MODE_UND:
8298     case ARM_CPU_MODE_SYS:
8299         break;
8300     case ARM_CPU_MODE_HYP:
8301         if (s->current_el == 1 || !arm_dc_feature(s, ARM_FEATURE_EL2)) {
8302             undef = true;
8303         }
8304         break;
8305     case ARM_CPU_MODE_MON:
8306         /* No need to check specifically for "are we non-secure" because
8307          * we've already made EL0 UNDEF and handled the trap for S-EL1;
8308          * so if this isn't EL3 then we must be non-secure.
8309          */
8310         if (s->current_el != 3) {
8311             undef = true;
8312         }
8313         break;
8314     default:
8315         undef = true;
8316     }
8317
8318     if (undef) {
8319         gen_exception_insn(s, 4, EXCP_UDEF, syn_uncategorized(),
8320                            default_exception_el(s));
8321         return;
8322     }
8323
8324     addr = tcg_temp_new_i32();
8325     tmp = tcg_const_i32(mode);
8326     /* get_r13_banked() will raise an exception if called from System mode */
8327     gen_set_condexec(s);
8328     gen_set_pc_im(s, s->pc - 4);
8329     gen_helper_get_r13_banked(addr, cpu_env, tmp);
8330     tcg_temp_free_i32(tmp);
8331     switch (amode) {
8332     case 0: /* DA */
8333         offset = -4;
8334         break;
8335     case 1: /* IA */
8336         offset = 0;
8337         break;
8338     case 2: /* DB */
8339         offset = -8;
8340         break;
8341     case 3: /* IB */
8342         offset = 4;
8343         break;
8344     default:
8345         abort();
8346     }
8347     tcg_gen_addi_i32(addr, addr, offset);
8348     tmp = load_reg(s, 14);
8349     gen_aa32_st32(s, tmp, addr, get_mem_index(s));
8350     tcg_temp_free_i32(tmp);
8351     tmp = load_cpu_field(spsr);
8352     tcg_gen_addi_i32(addr, addr, 4);
8353     gen_aa32_st32(s, tmp, addr, get_mem_index(s));
8354     tcg_temp_free_i32(tmp);
8355     if (writeback) {
8356         switch (amode) {
8357         case 0:
8358             offset = -8;
8359             break;
8360         case 1:
8361             offset = 4;
8362             break;
8363         case 2:
8364             offset = -4;
8365             break;
8366         case 3:
8367             offset = 0;
8368             break;
8369         default:
8370             abort();
8371         }
8372         tcg_gen_addi_i32(addr, addr, offset);
8373         tmp = tcg_const_i32(mode);
8374         gen_helper_set_r13_banked(cpu_env, tmp, addr);
8375         tcg_temp_free_i32(tmp);
8376     }
8377     tcg_temp_free_i32(addr);
8378     s->base.is_jmp = DISAS_UPDATE;
8379 }
8380
8381 static void disas_arm_insn(DisasContext *s, unsigned int insn)
8382 {
8383     unsigned int cond, val, op1, i, shift, rm, rs, rn, rd, sh;
8384     TCGv_i32 tmp;
8385     TCGv_i32 tmp2;
8386     TCGv_i32 tmp3;
8387     TCGv_i32 addr;
8388     TCGv_i64 tmp64;
8389
8390     /* M variants do not implement ARM mode; this must raise the INVSTATE
8391      * UsageFault exception.
8392      */
8393     if (arm_dc_feature(s, ARM_FEATURE_M)) {
8394         gen_exception_insn(s, 4, EXCP_INVSTATE, syn_uncategorized(),
8395                            default_exception_el(s));
8396         return;
8397     }
8398     cond = insn >> 28;
8399     if (cond == 0xf){
8400         /* In ARMv3 and v4 the NV condition is UNPREDICTABLE; we
8401          * choose to UNDEF. In ARMv5 and above the space is used
8402          * for miscellaneous unconditional instructions.
8403          */
8404         ARCH(5);
8405
8406         /* Unconditional instructions.  */
8407         if (((insn >> 25) & 7) == 1) {
8408             /* NEON Data processing.  */
8409             if (!arm_dc_feature(s, ARM_FEATURE_NEON)) {
8410                 goto illegal_op;
8411             }
8412
8413             if (disas_neon_data_insn(s, insn)) {
8414                 goto illegal_op;
8415             }
8416             return;
8417         }
8418         if ((insn & 0x0f100000) == 0x04000000) {
8419             /* NEON load/store.  */
8420             if (!arm_dc_feature(s, ARM_FEATURE_NEON)) {
8421                 goto illegal_op;
8422             }
8423
8424             if (disas_neon_ls_insn(s, insn)) {
8425                 goto illegal_op;
8426             }
8427             return;
8428         }
8429         if ((insn & 0x0f000e10) == 0x0e000a00) {
8430             /* VFP.  */
8431             if (disas_vfp_insn(s, insn)) {
8432                 goto illegal_op;
8433             }
8434             return;
8435         }
8436         if (((insn & 0x0f30f000) == 0x0510f000) ||
8437             ((insn & 0x0f30f010) == 0x0710f000)) {
8438             if ((insn & (1 << 22)) == 0) {
8439                 /* PLDW; v7MP */
8440                 if (!arm_dc_feature(s, ARM_FEATURE_V7MP)) {
8441                     goto illegal_op;
8442                 }
8443             }
8444             /* Otherwise PLD; v5TE+ */
8445             ARCH(5TE);
8446             return;
8447         }
8448         if (((insn & 0x0f70f000) == 0x0450f000) ||
8449             ((insn & 0x0f70f010) == 0x0650f000)) {
8450             ARCH(7);
8451             return; /* PLI; V7 */
8452         }
8453         if (((insn & 0x0f700000) == 0x04100000) ||
8454             ((insn & 0x0f700010) == 0x06100000)) {
8455             if (!arm_dc_feature(s, ARM_FEATURE_V7MP)) {
8456                 goto illegal_op;
8457             }
8458             return; /* v7MP: Unallocated memory hint: must NOP */
8459         }
8460
8461         if ((insn & 0x0ffffdff) == 0x01010000) {
8462             ARCH(6);
8463             /* setend */
8464             if (((insn >> 9) & 1) != !!(s->be_data == MO_BE)) {
8465                 gen_helper_setend(cpu_env);
8466                 s->base.is_jmp = DISAS_UPDATE;
8467             }
8468             return;
8469         } else if ((insn & 0x0fffff00) == 0x057ff000) {
8470             switch ((insn >> 4) & 0xf) {
8471             case 1: /* clrex */
8472                 ARCH(6K);
8473                 gen_clrex(s);
8474                 return;
8475             case 4: /* dsb */
8476             case 5: /* dmb */
8477                 ARCH(7);
8478                 tcg_gen_mb(TCG_MO_ALL | TCG_BAR_SC);
8479                 return;
8480             case 6: /* isb */
8481                 /* We need to break the TB after this insn to execute
8482                  * self-modifying code correctly and also to take
8483                  * any pending interrupts immediately.
8484                  */
8485                 gen_goto_tb(s, 0, s->pc & ~1);
8486                 return;
8487             default:
8488                 goto illegal_op;
8489             }
8490         } else if ((insn & 0x0e5fffe0) == 0x084d0500) {
8491             /* srs */
8492             ARCH(6);
8493             gen_srs(s, (insn & 0x1f), (insn >> 23) & 3, insn & (1 << 21));
8494             return;
8495         } else if ((insn & 0x0e50ffe0) == 0x08100a00) {
8496             /* rfe */
8497             int32_t offset;
8498             if (IS_USER(s))
8499                 goto illegal_op;
8500             ARCH(6);
8501             rn = (insn >> 16) & 0xf;
8502             addr = load_reg(s, rn);
8503             i = (insn >> 23) & 3;
8504             switch (i) {
8505             case 0: offset = -4; break; /* DA */
8506             case 1: offset = 0; break; /* IA */
8507             case 2: offset = -8; break; /* DB */
8508             case 3: offset = 4; break; /* IB */
8509             default: abort();
8510             }
8511             if (offset)
8512                 tcg_gen_addi_i32(addr, addr, offset);
8513             /* Load PC into tmp and CPSR into tmp2.  */
8514             tmp = tcg_temp_new_i32();
8515             gen_aa32_ld32u(s, tmp, addr, get_mem_index(s));
8516             tcg_gen_addi_i32(addr, addr, 4);
8517             tmp2 = tcg_temp_new_i32();
8518             gen_aa32_ld32u(s, tmp2, addr, get_mem_index(s));
8519             if (insn & (1 << 21)) {
8520                 /* Base writeback.  */
8521                 switch (i) {
8522                 case 0: offset = -8; break;
8523                 case 1: offset = 4; break;
8524                 case 2: offset = -4; break;
8525                 case 3: offset = 0; break;
8526                 default: abort();
8527                 }
8528                 if (offset)
8529                     tcg_gen_addi_i32(addr, addr, offset);
8530                 store_reg(s, rn, addr);
8531             } else {
8532                 tcg_temp_free_i32(addr);
8533             }
8534             gen_rfe(s, tmp, tmp2);
8535             return;
8536         } else if ((insn & 0x0e000000) == 0x0a000000) {
8537             /* branch link and change to thumb (blx <offset>) */
8538             int32_t offset;
8539
8540             val = (uint32_t)s->pc;
8541             tmp = tcg_temp_new_i32();
8542             tcg_gen_movi_i32(tmp, val);
8543             store_reg(s, 14, tmp);
8544             /* Sign-extend the 24-bit offset */
8545             offset = (((int32_t)insn) << 8) >> 8;
8546             /* offset * 4 + bit24 * 2 + (thumb bit) */
8547             val += (offset << 2) | ((insn >> 23) & 2) | 1;
8548             /* pipeline offset */
8549             val += 4;
8550             /* protected by ARCH(5); above, near the start of uncond block */
8551             gen_bx_im(s, val);
8552             return;
8553         } else if ((insn & 0x0e000f00) == 0x0c000100) {
8554             if (arm_dc_feature(s, ARM_FEATURE_IWMMXT)) {
8555                 /* iWMMXt register transfer.  */
8556                 if (extract32(s->c15_cpar, 1, 1)) {
8557                     if (!disas_iwmmxt_insn(s, insn)) {
8558                         return;
8559                     }
8560                 }
8561             }
8562         } else if ((insn & 0x0e000a00) == 0x0c000800
8563                    && arm_dc_feature(s, ARM_FEATURE_V8)) {
8564             if (disas_neon_insn_3same_ext(s, insn)) {
8565                 goto illegal_op;
8566             }
8567             return;
8568         } else if ((insn & 0x0f000a00) == 0x0e000800
8569                    && arm_dc_feature(s, ARM_FEATURE_V8)) {
8570             if (disas_neon_insn_2reg_scalar_ext(s, insn)) {
8571                 goto illegal_op;
8572             }
8573             return;
8574         } else if ((insn & 0x0fe00000) == 0x0c400000) {
8575             /* Coprocessor double register transfer.  */
8576             ARCH(5TE);
8577         } else if ((insn & 0x0f000010) == 0x0e000010) {
8578             /* Additional coprocessor register transfer.  */
8579         } else if ((insn & 0x0ff10020) == 0x01000000) {
8580             uint32_t mask;
8581             uint32_t val;
8582             /* cps (privileged) */
8583             if (IS_USER(s))
8584                 return;
8585             mask = val = 0;
8586             if (insn & (1 << 19)) {
8587                 if (insn & (1 << 8))
8588                     mask |= CPSR_A;
8589                 if (insn & (1 << 7))
8590                     mask |= CPSR_I;
8591                 if (insn & (1 << 6))
8592                     mask |= CPSR_F;
8593                 if (insn & (1 << 18))
8594                     val |= mask;
8595             }
8596             if (insn & (1 << 17)) {
8597                 mask |= CPSR_M;
8598                 val |= (insn & 0x1f);
8599             }
8600             if (mask) {
8601                 gen_set_psr_im(s, mask, 0, val);
8602             }
8603             return;
8604         }
8605         goto illegal_op;
8606     }
8607     if (cond != 0xe) {
8608         /* if not always execute, we generate a conditional jump to
8609            next instruction */
8610         s->condlabel = gen_new_label();
8611         arm_gen_test_cc(cond ^ 1, s->condlabel);
8612         s->condjmp = 1;
8613     }
8614     if ((insn & 0x0f900000) == 0x03000000) {
8615         if ((insn & (1 << 21)) == 0) {
8616             ARCH(6T2);
8617             rd = (insn >> 12) & 0xf;
8618             val = ((insn >> 4) & 0xf000) | (insn & 0xfff);
8619             if ((insn & (1 << 22)) == 0) {
8620                 /* MOVW */
8621                 tmp = tcg_temp_new_i32();
8622                 tcg_gen_movi_i32(tmp, val);
8623             } else {
8624                 /* MOVT */
8625                 tmp = load_reg(s, rd);
8626                 tcg_gen_ext16u_i32(tmp, tmp);
8627                 tcg_gen_ori_i32(tmp, tmp, val << 16);
8628             }
8629             store_reg(s, rd, tmp);
8630         } else {
8631             if (((insn >> 12) & 0xf) != 0xf)
8632                 goto illegal_op;
8633             if (((insn >> 16) & 0xf) == 0) {
8634                 gen_nop_hint(s, insn & 0xff);
8635             } else {
8636                 /* CPSR = immediate */
8637                 val = insn & 0xff;
8638                 shift = ((insn >> 8) & 0xf) * 2;
8639                 if (shift)
8640                     val = (val >> shift) | (val << (32 - shift));
8641                 i = ((insn & (1 << 22)) != 0);
8642                 if (gen_set_psr_im(s, msr_mask(s, (insn >> 16) & 0xf, i),
8643                                    i, val)) {
8644                     goto illegal_op;
8645                 }
8646             }
8647         }
8648     } else if ((insn & 0x0f900000) == 0x01000000
8649                && (insn & 0x00000090) != 0x00000090) {
8650         /* miscellaneous instructions */
8651         op1 = (insn >> 21) & 3;
8652         sh = (insn >> 4) & 0xf;
8653         rm = insn & 0xf;
8654         switch (sh) {
8655         case 0x0: /* MSR, MRS */
8656             if (insn & (1 << 9)) {
8657                 /* MSR (banked) and MRS (banked) */
8658                 int sysm = extract32(insn, 16, 4) |
8659                     (extract32(insn, 8, 1) << 4);
8660                 int r = extract32(insn, 22, 1);
8661
8662                 if (op1 & 1) {
8663                     /* MSR (banked) */
8664                     gen_msr_banked(s, r, sysm, rm);
8665                 } else {
8666                     /* MRS (banked) */
8667                     int rd = extract32(insn, 12, 4);
8668
8669                     gen_mrs_banked(s, r, sysm, rd);
8670                 }
8671                 break;
8672             }
8673
8674             /* MSR, MRS (for PSRs) */
8675             if (op1 & 1) {
8676                 /* PSR = reg */
8677                 tmp = load_reg(s, rm);
8678                 i = ((op1 & 2) != 0);
8679                 if (gen_set_psr(s, msr_mask(s, (insn >> 16) & 0xf, i), i, tmp))
8680                     goto illegal_op;
8681             } else {
8682                 /* reg = PSR */
8683                 rd = (insn >> 12) & 0xf;
8684                 if (op1 & 2) {
8685                     if (IS_USER(s))
8686                         goto illegal_op;
8687                     tmp = load_cpu_field(spsr);
8688                 } else {
8689                     tmp = tcg_temp_new_i32();
8690                     gen_helper_cpsr_read(tmp, cpu_env);
8691                 }
8692                 store_reg(s, rd, tmp);
8693             }
8694             break;
8695         case 0x1:
8696             if (op1 == 1) {
8697                 /* branch/exchange thumb (bx).  */
8698                 ARCH(4T);
8699                 tmp = load_reg(s, rm);
8700                 gen_bx(s, tmp);
8701             } else if (op1 == 3) {
8702                 /* clz */
8703                 ARCH(5);
8704                 rd = (insn >> 12) & 0xf;
8705                 tmp = load_reg(s, rm);
8706                 tcg_gen_clzi_i32(tmp, tmp, 32);
8707                 store_reg(s, rd, tmp);
8708             } else {
8709                 goto illegal_op;
8710             }
8711             break;
8712         case 0x2:
8713             if (op1 == 1) {
8714                 ARCH(5J); /* bxj */
8715                 /* Trivial implementation equivalent to bx.  */
8716                 tmp = load_reg(s, rm);
8717                 gen_bx(s, tmp);
8718             } else {
8719                 goto illegal_op;
8720             }
8721             break;
8722         case 0x3:
8723             if (op1 != 1)
8724               goto illegal_op;
8725
8726             ARCH(5);
8727             /* branch link/exchange thumb (blx) */
8728             tmp = load_reg(s, rm);
8729             tmp2 = tcg_temp_new_i32();
8730             tcg_gen_movi_i32(tmp2, s->pc);
8731             store_reg(s, 14, tmp2);
8732             gen_bx(s, tmp);
8733             break;
8734         case 0x4:
8735         {
8736             /* crc32/crc32c */
8737             uint32_t c = extract32(insn, 8, 4);
8738
8739             /* Check this CPU supports ARMv8 CRC instructions.
8740              * op1 == 3 is UNPREDICTABLE but handle as UNDEFINED.
8741              * Bits 8, 10 and 11 should be zero.
8742              */
8743             if (!arm_dc_feature(s, ARM_FEATURE_CRC) || op1 == 0x3 ||
8744                 (c & 0xd) != 0) {
8745                 goto illegal_op;
8746             }
8747
8748             rn = extract32(insn, 16, 4);
8749             rd = extract32(insn, 12, 4);
8750
8751             tmp = load_reg(s, rn);
8752             tmp2 = load_reg(s, rm);
8753             if (op1 == 0) {
8754                 tcg_gen_andi_i32(tmp2, tmp2, 0xff);
8755             } else if (op1 == 1) {
8756                 tcg_gen_andi_i32(tmp2, tmp2, 0xffff);
8757             }
8758             tmp3 = tcg_const_i32(1 << op1);
8759             if (c & 0x2) {
8760                 gen_helper_crc32c(tmp, tmp, tmp2, tmp3);
8761             } else {
8762                 gen_helper_crc32(tmp, tmp, tmp2, tmp3);
8763             }
8764             tcg_temp_free_i32(tmp2);
8765             tcg_temp_free_i32(tmp3);
8766             store_reg(s, rd, tmp);
8767             break;
8768         }
8769         case 0x5: /* saturating add/subtract */
8770             ARCH(5TE);
8771             rd = (insn >> 12) & 0xf;
8772             rn = (insn >> 16) & 0xf;
8773             tmp = load_reg(s, rm);
8774             tmp2 = load_reg(s, rn);
8775             if (op1 & 2)
8776                 gen_helper_double_saturate(tmp2, cpu_env, tmp2);
8777             if (op1 & 1)
8778                 gen_helper_sub_saturate(tmp, cpu_env, tmp, tmp2);
8779             else
8780                 gen_helper_add_saturate(tmp, cpu_env, tmp, tmp2);
8781             tcg_temp_free_i32(tmp2);
8782             store_reg(s, rd, tmp);
8783             break;
8784         case 7:
8785         {
8786             int imm16 = extract32(insn, 0, 4) | (extract32(insn, 8, 12) << 4);
8787             switch (op1) {
8788             case 0:
8789                 /* HLT */
8790                 gen_hlt(s, imm16);
8791                 break;
8792             case 1:
8793                 /* bkpt */
8794                 ARCH(5);
8795                 gen_exception_bkpt_insn(s, 4, syn_aa32_bkpt(imm16, false));
8796                 break;
8797             case 2:
8798                 /* Hypervisor call (v7) */
8799                 ARCH(7);
8800                 if (IS_USER(s)) {
8801                     goto illegal_op;
8802                 }
8803                 gen_hvc(s, imm16);
8804                 break;
8805             case 3:
8806                 /* Secure monitor call (v6+) */
8807                 ARCH(6K);
8808                 if (IS_USER(s)) {
8809                     goto illegal_op;
8810                 }
8811                 gen_smc(s);
8812                 break;
8813             default:
8814                 g_assert_not_reached();
8815             }
8816             break;
8817         }
8818         case 0x8: /* signed multiply */
8819         case 0xa:
8820         case 0xc:
8821         case 0xe:
8822             ARCH(5TE);
8823             rs = (insn >> 8) & 0xf;
8824             rn = (insn >> 12) & 0xf;
8825             rd = (insn >> 16) & 0xf;
8826             if (op1 == 1) {
8827                 /* (32 * 16) >> 16 */
8828                 tmp = load_reg(s, rm);
8829                 tmp2 = load_reg(s, rs);
8830                 if (sh & 4)
8831                     tcg_gen_sari_i32(tmp2, tmp2, 16);
8832                 else
8833                     gen_sxth(tmp2);
8834                 tmp64 = gen_muls_i64_i32(tmp, tmp2);
8835                 tcg_gen_shri_i64(tmp64, tmp64, 16);
8836                 tmp = tcg_temp_new_i32();
8837                 tcg_gen_extrl_i64_i32(tmp, tmp64);
8838                 tcg_temp_free_i64(tmp64);
8839                 if ((sh & 2) == 0) {
8840                     tmp2 = load_reg(s, rn);
8841                     gen_helper_add_setq(tmp, cpu_env, tmp, tmp2);
8842                     tcg_temp_free_i32(tmp2);
8843                 }
8844                 store_reg(s, rd, tmp);
8845             } else {
8846                 /* 16 * 16 */
8847                 tmp = load_reg(s, rm);
8848                 tmp2 = load_reg(s, rs);
8849                 gen_mulxy(tmp, tmp2, sh & 2, sh & 4);
8850                 tcg_temp_free_i32(tmp2);
8851                 if (op1 == 2) {
8852                     tmp64 = tcg_temp_new_i64();
8853                     tcg_gen_ext_i32_i64(tmp64, tmp);
8854                     tcg_temp_free_i32(tmp);
8855                     gen_addq(s, tmp64, rn, rd);
8856                     gen_storeq_reg(s, rn, rd, tmp64);
8857                     tcg_temp_free_i64(tmp64);
8858                 } else {
8859                     if (op1 == 0) {
8860                         tmp2 = load_reg(s, rn);
8861                         gen_helper_add_setq(tmp, cpu_env, tmp, tmp2);
8862                         tcg_temp_free_i32(tmp2);
8863                     }
8864                     store_reg(s, rd, tmp);
8865                 }
8866             }
8867             break;
8868         default:
8869             goto illegal_op;
8870         }
8871     } else if (((insn & 0x0e000000) == 0 &&
8872                 (insn & 0x00000090) != 0x90) ||
8873                ((insn & 0x0e000000) == (1 << 25))) {
8874         int set_cc, logic_cc, shiftop;
8875
8876         op1 = (insn >> 21) & 0xf;
8877         set_cc = (insn >> 20) & 1;
8878         logic_cc = table_logic_cc[op1] & set_cc;
8879
8880         /* data processing instruction */
8881         if (insn & (1 << 25)) {
8882             /* immediate operand */
8883             val = insn & 0xff;
8884             shift = ((insn >> 8) & 0xf) * 2;
8885             if (shift) {
8886                 val = (val >> shift) | (val << (32 - shift));
8887             }
8888             tmp2 = tcg_temp_new_i32();
8889             tcg_gen_movi_i32(tmp2, val);
8890             if (logic_cc && shift) {
8891                 gen_set_CF_bit31(tmp2);
8892             }
8893         } else {
8894             /* register */
8895             rm = (insn) & 0xf;
8896             tmp2 = load_reg(s, rm);
8897             shiftop = (insn >> 5) & 3;
8898             if (!(insn & (1 << 4))) {
8899                 shift = (insn >> 7) & 0x1f;
8900                 gen_arm_shift_im(tmp2, shiftop, shift, logic_cc);
8901             } else {
8902                 rs = (insn >> 8) & 0xf;
8903                 tmp = load_reg(s, rs);
8904                 gen_arm_shift_reg(tmp2, shiftop, tmp, logic_cc);
8905             }
8906         }
8907         if (op1 != 0x0f && op1 != 0x0d) {
8908             rn = (insn >> 16) & 0xf;
8909             tmp = load_reg(s, rn);
8910         } else {
8911             tmp = NULL;
8912         }
8913         rd = (insn >> 12) & 0xf;
8914         switch(op1) {
8915         case 0x00:
8916             tcg_gen_and_i32(tmp, tmp, tmp2);
8917             if (logic_cc) {
8918                 gen_logic_CC(tmp);
8919             }
8920             store_reg_bx(s, rd, tmp);
8921             break;
8922         case 0x01:
8923             tcg_gen_xor_i32(tmp, tmp, tmp2);
8924             if (logic_cc) {
8925                 gen_logic_CC(tmp);
8926             }
8927             store_reg_bx(s, rd, tmp);
8928             break;
8929         case 0x02:
8930             if (set_cc && rd == 15) {
8931                 /* SUBS r15, ... is used for exception return.  */
8932                 if (IS_USER(s)) {
8933                     goto illegal_op;
8934                 }
8935                 gen_sub_CC(tmp, tmp, tmp2);
8936                 gen_exception_return(s, tmp);
8937             } else {
8938                 if (set_cc) {
8939                     gen_sub_CC(tmp, tmp, tmp2);
8940                 } else {
8941                     tcg_gen_sub_i32(tmp, tmp, tmp2);
8942                 }
8943                 store_reg_bx(s, rd, tmp);
8944             }
8945             break;
8946         case 0x03:
8947             if (set_cc) {
8948                 gen_sub_CC(tmp, tmp2, tmp);
8949             } else {
8950                 tcg_gen_sub_i32(tmp, tmp2, tmp);
8951             }
8952             store_reg_bx(s, rd, tmp);
8953             break;
8954         case 0x04:
8955             if (set_cc) {
8956                 gen_add_CC(tmp, tmp, tmp2);
8957             } else {
8958                 tcg_gen_add_i32(tmp, tmp, tmp2);
8959             }
8960             store_reg_bx(s, rd, tmp);
8961             break;
8962         case 0x05:
8963             if (set_cc) {
8964                 gen_adc_CC(tmp, tmp, tmp2);
8965             } else {
8966                 gen_add_carry(tmp, tmp, tmp2);
8967             }
8968             store_reg_bx(s, rd, tmp);
8969             break;
8970         case 0x06:
8971             if (set_cc) {
8972                 gen_sbc_CC(tmp, tmp, tmp2);
8973             } else {
8974                 gen_sub_carry(tmp, tmp, tmp2);
8975             }
8976             store_reg_bx(s, rd, tmp);
8977             break;
8978         case 0x07:
8979             if (set_cc) {
8980                 gen_sbc_CC(tmp, tmp2, tmp);
8981             } else {
8982                 gen_sub_carry(tmp, tmp2, tmp);
8983             }
8984             store_reg_bx(s, rd, tmp);
8985             break;
8986         case 0x08:
8987             if (set_cc) {
8988                 tcg_gen_and_i32(tmp, tmp, tmp2);
8989                 gen_logic_CC(tmp);
8990             }
8991             tcg_temp_free_i32(tmp);
8992             break;
8993         case 0x09:
8994             if (set_cc) {
8995                 tcg_gen_xor_i32(tmp, tmp, tmp2);
8996                 gen_logic_CC(tmp);
8997             }
8998             tcg_temp_free_i32(tmp);
8999             break;
9000         case 0x0a:
9001             if (set_cc) {
9002                 gen_sub_CC(tmp, tmp, tmp2);
9003             }
9004             tcg_temp_free_i32(tmp);
9005             break;
9006         case 0x0b:
9007             if (set_cc) {
9008                 gen_add_CC(tmp, tmp, tmp2);
9009             }
9010             tcg_temp_free_i32(tmp);
9011             break;
9012         case 0x0c:
9013             tcg_gen_or_i32(tmp, tmp, tmp2);
9014             if (logic_cc) {
9015                 gen_logic_CC(tmp);
9016             }
9017             store_reg_bx(s, rd, tmp);
9018             break;
9019         case 0x0d:
9020             if (logic_cc && rd == 15) {
9021                 /* MOVS r15, ... is used for exception return.  */
9022                 if (IS_USER(s)) {
9023                     goto illegal_op;
9024                 }
9025                 gen_exception_return(s, tmp2);
9026             } else {
9027                 if (logic_cc) {
9028                     gen_logic_CC(tmp2);
9029                 }
9030                 store_reg_bx(s, rd, tmp2);
9031             }
9032             break;
9033         case 0x0e:
9034             tcg_gen_andc_i32(tmp, tmp, tmp2);
9035             if (logic_cc) {
9036                 gen_logic_CC(tmp);
9037             }
9038             store_reg_bx(s, rd, tmp);
9039             break;
9040         default:
9041         case 0x0f:
9042             tcg_gen_not_i32(tmp2, tmp2);
9043             if (logic_cc) {
9044                 gen_logic_CC(tmp2);
9045             }
9046             store_reg_bx(s, rd, tmp2);
9047             break;
9048         }
9049         if (op1 != 0x0f && op1 != 0x0d) {
9050             tcg_temp_free_i32(tmp2);
9051         }
9052     } else {
9053         /* other instructions */
9054         op1 = (insn >> 24) & 0xf;
9055         switch(op1) {
9056         case 0x0:
9057         case 0x1:
9058             /* multiplies, extra load/stores */
9059             sh = (insn >> 5) & 3;
9060             if (sh == 0) {
9061                 if (op1 == 0x0) {
9062                     rd = (insn >> 16) & 0xf;
9063                     rn = (insn >> 12) & 0xf;
9064                     rs = (insn >> 8) & 0xf;
9065                     rm = (insn) & 0xf;
9066                     op1 = (insn >> 20) & 0xf;
9067                     switch (op1) {
9068                     case 0: case 1: case 2: case 3: case 6:
9069                         /* 32 bit mul */
9070                         tmp = load_reg(s, rs);
9071                         tmp2 = load_reg(s, rm);
9072                         tcg_gen_mul_i32(tmp, tmp, tmp2);
9073                         tcg_temp_free_i32(tmp2);
9074                         if (insn & (1 << 22)) {
9075                             /* Subtract (mls) */
9076                             ARCH(6T2);
9077                             tmp2 = load_reg(s, rn);
9078                             tcg_gen_sub_i32(tmp, tmp2, tmp);
9079                             tcg_temp_free_i32(tmp2);
9080                         } else if (insn & (1 << 21)) {
9081                             /* Add */
9082                             tmp2 = load_reg(s, rn);
9083                             tcg_gen_add_i32(tmp, tmp, tmp2);
9084                             tcg_temp_free_i32(tmp2);
9085                         }
9086                         if (insn & (1 << 20))
9087                             gen_logic_CC(tmp);
9088                         store_reg(s, rd, tmp);
9089                         break;
9090                     case 4:
9091                         /* 64 bit mul double accumulate (UMAAL) */
9092                         ARCH(6);
9093                         tmp = load_reg(s, rs);
9094                         tmp2 = load_reg(s, rm);
9095                         tmp64 = gen_mulu_i64_i32(tmp, tmp2);
9096                         gen_addq_lo(s, tmp64, rn);
9097                         gen_addq_lo(s, tmp64, rd);
9098                         gen_storeq_reg(s, rn, rd, tmp64);
9099                         tcg_temp_free_i64(tmp64);
9100                         break;
9101                     case 8: case 9: case 10: case 11:
9102                     case 12: case 13: case 14: case 15:
9103                         /* 64 bit mul: UMULL, UMLAL, SMULL, SMLAL. */
9104                         tmp = load_reg(s, rs);
9105                         tmp2 = load_reg(s, rm);
9106                         if (insn & (1 << 22)) {
9107                             tcg_gen_muls2_i32(tmp, tmp2, tmp, tmp2);
9108                         } else {
9109                             tcg_gen_mulu2_i32(tmp, tmp2, tmp, tmp2);
9110                         }
9111                         if (insn & (1 << 21)) { /* mult accumulate */
9112                             TCGv_i32 al = load_reg(s, rn);
9113                             TCGv_i32 ah = load_reg(s, rd);
9114                             tcg_gen_add2_i32(tmp, tmp2, tmp, tmp2, al, ah);
9115                             tcg_temp_free_i32(al);
9116                             tcg_temp_free_i32(ah);
9117                         }
9118                         if (insn & (1 << 20)) {
9119                             gen_logicq_cc(tmp, tmp2);
9120                         }
9121                         store_reg(s, rn, tmp);
9122                         store_reg(s, rd, tmp2);
9123                         break;
9124                     default:
9125                         goto illegal_op;
9126                     }
9127                 } else {
9128                     rn = (insn >> 16) & 0xf;
9129                     rd = (insn >> 12) & 0xf;
9130                     if (insn & (1 << 23)) {
9131                         /* load/store exclusive */
9132                         int op2 = (insn >> 8) & 3;
9133                         op1 = (insn >> 21) & 0x3;
9134
9135                         switch (op2) {
9136                         case 0: /* lda/stl */
9137                             if (op1 == 1) {
9138                                 goto illegal_op;
9139                             }
9140                             ARCH(8);
9141                             break;
9142                         case 1: /* reserved */
9143                             goto illegal_op;
9144                         case 2: /* ldaex/stlex */
9145                             ARCH(8);
9146                             break;
9147                         case 3: /* ldrex/strex */
9148                             if (op1) {
9149                                 ARCH(6K);
9150                             } else {
9151                                 ARCH(6);
9152                             }
9153                             break;
9154                         }
9155
9156                         addr = tcg_temp_local_new_i32();
9157                         load_reg_var(s, addr, rn);
9158
9159                         /* Since the emulation does not have barriers,
9160                            the acquire/release semantics need no special
9161                            handling */
9162                         if (op2 == 0) {
9163                             if (insn & (1 << 20)) {
9164                                 tmp = tcg_temp_new_i32();
9165                                 switch (op1) {
9166                                 case 0: /* lda */
9167                                     gen_aa32_ld32u_iss(s, tmp, addr,
9168                                                        get_mem_index(s),
9169                                                        rd | ISSIsAcqRel);
9170                                     break;
9171                                 case 2: /* ldab */
9172                                     gen_aa32_ld8u_iss(s, tmp, addr,
9173                                                       get_mem_index(s),
9174                                                       rd | ISSIsAcqRel);
9175                                     break;
9176                                 case 3: /* ldah */
9177                                     gen_aa32_ld16u_iss(s, tmp, addr,
9178                                                        get_mem_index(s),
9179                                                        rd | ISSIsAcqRel);
9180                                     break;
9181                                 default:
9182                                     abort();
9183                                 }
9184                                 store_reg(s, rd, tmp);
9185                             } else {
9186                                 rm = insn & 0xf;
9187                                 tmp = load_reg(s, rm);
9188                                 switch (op1) {
9189                                 case 0: /* stl */
9190                                     gen_aa32_st32_iss(s, tmp, addr,
9191                                                       get_mem_index(s),
9192                                                       rm | ISSIsAcqRel);
9193                                     break;
9194                                 case 2: /* stlb */
9195                                     gen_aa32_st8_iss(s, tmp, addr,
9196                                                      get_mem_index(s),
9197                                                      rm | ISSIsAcqRel);
9198                                     break;
9199                                 case 3: /* stlh */
9200                                     gen_aa32_st16_iss(s, tmp, addr,
9201                                                       get_mem_index(s),
9202                                                       rm | ISSIsAcqRel);
9203                                     break;
9204                                 default:
9205                                     abort();
9206                                 }
9207                                 tcg_temp_free_i32(tmp);
9208                             }
9209                         } else if (insn & (1 << 20)) {
9210                             switch (op1) {
9211                             case 0: /* ldrex */
9212                                 gen_load_exclusive(s, rd, 15, addr, 2);
9213                                 break;
9214                             case 1: /* ldrexd */
9215                                 gen_load_exclusive(s, rd, rd + 1, addr, 3);
9216                                 break;
9217                             case 2: /* ldrexb */
9218                                 gen_load_exclusive(s, rd, 15, addr, 0);
9219                                 break;
9220                             case 3: /* ldrexh */
9221                                 gen_load_exclusive(s, rd, 15, addr, 1);
9222                                 break;
9223                             default:
9224                                 abort();
9225                             }
9226                         } else {
9227                             rm = insn & 0xf;
9228                             switch (op1) {
9229                             case 0:  /*  strex */
9230                                 gen_store_exclusive(s, rd, rm, 15, addr, 2);
9231                                 break;
9232                             case 1: /*  strexd */
9233                                 gen_store_exclusive(s, rd, rm, rm + 1, addr, 3);
9234                                 break;
9235                             case 2: /*  strexb */
9236                                 gen_store_exclusive(s, rd, rm, 15, addr, 0);
9237                                 break;
9238                             case 3: /* strexh */
9239                                 gen_store_exclusive(s, rd, rm, 15, addr, 1);
9240                                 break;
9241                             default:
9242                                 abort();
9243                             }
9244                         }
9245                         tcg_temp_free_i32(addr);
9246                     } else if ((insn & 0x00300f00) == 0) {
9247                         /* 0bcccc_0001_0x00_xxxx_xxxx_0000_1001_xxxx
9248                         *  - SWP, SWPB
9249                         */
9250
9251                         TCGv taddr;
9252                         TCGMemOp opc = s->be_data;
9253
9254                         rm = (insn) & 0xf;
9255
9256                         if (insn & (1 << 22)) {
9257                             opc |= MO_UB;
9258                         } else {
9259                             opc |= MO_UL | MO_ALIGN;
9260                         }
9261
9262                         addr = load_reg(s, rn);
9263                         taddr = gen_aa32_addr(s, addr, opc);
9264                         tcg_temp_free_i32(addr);
9265
9266                         tmp = load_reg(s, rm);
9267                         tcg_gen_atomic_xchg_i32(tmp, taddr, tmp,
9268                                                 get_mem_index(s), opc);
9269                         tcg_temp_free(taddr);
9270                         store_reg(s, rd, tmp);
9271                     } else {
9272                         goto illegal_op;
9273                     }
9274                 }
9275             } else {
9276                 int address_offset;
9277                 bool load = insn & (1 << 20);
9278                 bool wbit = insn & (1 << 21);
9279                 bool pbit = insn & (1 << 24);
9280                 bool doubleword = false;
9281                 ISSInfo issinfo;
9282
9283                 /* Misc load/store */
9284                 rn = (insn >> 16) & 0xf;
9285                 rd = (insn >> 12) & 0xf;
9286
9287                 /* ISS not valid if writeback */
9288                 issinfo = (pbit & !wbit) ? rd : ISSInvalid;
9289
9290                 if (!load && (sh & 2)) {
9291                     /* doubleword */
9292                     ARCH(5TE);
9293                     if (rd & 1) {
9294                         /* UNPREDICTABLE; we choose to UNDEF */
9295                         goto illegal_op;
9296                     }
9297                     load = (sh & 1) == 0;
9298                     doubleword = true;
9299                 }
9300
9301                 addr = load_reg(s, rn);
9302                 if (pbit) {
9303                     gen_add_datah_offset(s, insn, 0, addr);
9304                 }
9305                 address_offset = 0;
9306
9307                 if (doubleword) {
9308                     if (!load) {
9309                         /* store */
9310                         tmp = load_reg(s, rd);
9311                         gen_aa32_st32(s, tmp, addr, get_mem_index(s));
9312                         tcg_temp_free_i32(tmp);
9313                         tcg_gen_addi_i32(addr, addr, 4);
9314                         tmp = load_reg(s, rd + 1);
9315                         gen_aa32_st32(s, tmp, addr, get_mem_index(s));
9316                         tcg_temp_free_i32(tmp);
9317                     } else {
9318                         /* load */
9319                         tmp = tcg_temp_new_i32();
9320                         gen_aa32_ld32u(s, tmp, addr, get_mem_index(s));
9321                         store_reg(s, rd, tmp);
9322                         tcg_gen_addi_i32(addr, addr, 4);
9323                         tmp = tcg_temp_new_i32();
9324                         gen_aa32_ld32u(s, tmp, addr, get_mem_index(s));
9325                         rd++;
9326                     }
9327                     address_offset = -4;
9328                 } else if (load) {
9329                     /* load */
9330                     tmp = tcg_temp_new_i32();
9331                     switch (sh) {
9332                     case 1:
9333                         gen_aa32_ld16u_iss(s, tmp, addr, get_mem_index(s),
9334                                            issinfo);
9335                         break;
9336                     case 2:
9337                         gen_aa32_ld8s_iss(s, tmp, addr, get_mem_index(s),
9338                                           issinfo);
9339                         break;
9340                     default:
9341                     case 3:
9342                         gen_aa32_ld16s_iss(s, tmp, addr, get_mem_index(s),
9343                                            issinfo);
9344                         break;
9345                     }
9346                 } else {
9347                     /* store */
9348                     tmp = load_reg(s, rd);
9349                     gen_aa32_st16_iss(s, tmp, addr, get_mem_index(s), issinfo);
9350                     tcg_temp_free_i32(tmp);
9351                 }
9352                 /* Perform base writeback before the loaded value to
9353                    ensure correct behavior with overlapping index registers.
9354                    ldrd with base writeback is undefined if the
9355                    destination and index registers overlap.  */
9356                 if (!pbit) {
9357                     gen_add_datah_offset(s, insn, address_offset, addr);
9358                     store_reg(s, rn, addr);
9359                 } else if (wbit) {
9360                     if (address_offset)
9361                         tcg_gen_addi_i32(addr, addr, address_offset);
9362                     store_reg(s, rn, addr);
9363                 } else {
9364                     tcg_temp_free_i32(addr);
9365                 }
9366                 if (load) {
9367                     /* Complete the load.  */
9368                     store_reg(s, rd, tmp);
9369                 }
9370             }
9371             break;
9372         case 0x4:
9373         case 0x5:
9374             goto do_ldst;
9375         case 0x6:
9376         case 0x7:
9377             if (insn & (1 << 4)) {
9378                 ARCH(6);
9379                 /* Armv6 Media instructions.  */
9380                 rm = insn & 0xf;
9381                 rn = (insn >> 16) & 0xf;
9382                 rd = (insn >> 12) & 0xf;
9383                 rs = (insn >> 8) & 0xf;
9384                 switch ((insn >> 23) & 3) {
9385                 case 0: /* Parallel add/subtract.  */
9386                     op1 = (insn >> 20) & 7;
9387                     tmp = load_reg(s, rn);
9388                     tmp2 = load_reg(s, rm);
9389                     sh = (insn >> 5) & 7;
9390                     if ((op1 & 3) == 0 || sh == 5 || sh == 6)
9391                         goto illegal_op;
9392                     gen_arm_parallel_addsub(op1, sh, tmp, tmp2);
9393                     tcg_temp_free_i32(tmp2);
9394                     store_reg(s, rd, tmp);
9395                     break;
9396                 case 1:
9397                     if ((insn & 0x00700020) == 0) {
9398                         /* Halfword pack.  */
9399                         tmp = load_reg(s, rn);
9400                         tmp2 = load_reg(s, rm);
9401                         shift = (insn >> 7) & 0x1f;
9402                         if (insn & (1 << 6)) {
9403                             /* pkhtb */
9404                             if (shift == 0)
9405                                 shift = 31;
9406                             tcg_gen_sari_i32(tmp2, tmp2, shift);
9407                             tcg_gen_andi_i32(tmp, tmp, 0xffff0000);
9408                             tcg_gen_ext16u_i32(tmp2, tmp2);
9409                         } else {
9410                             /* pkhbt */
9411                             if (shift)
9412                                 tcg_gen_shli_i32(tmp2, tmp2, shift);
9413                             tcg_gen_ext16u_i32(tmp, tmp);
9414                             tcg_gen_andi_i32(tmp2, tmp2, 0xffff0000);
9415                         }
9416                         tcg_gen_or_i32(tmp, tmp, tmp2);
9417                         tcg_temp_free_i32(tmp2);
9418                         store_reg(s, rd, tmp);
9419                     } else if ((insn & 0x00200020) == 0x00200000) {
9420                         /* [us]sat */
9421                         tmp = load_reg(s, rm);
9422                         shift = (insn >> 7) & 0x1f;
9423                         if (insn & (1 << 6)) {
9424                             if (shift == 0)
9425                                 shift = 31;
9426                             tcg_gen_sari_i32(tmp, tmp, shift);
9427                         } else {
9428                             tcg_gen_shli_i32(tmp, tmp, shift);
9429                         }
9430                         sh = (insn >> 16) & 0x1f;
9431                         tmp2 = tcg_const_i32(sh);
9432                         if (insn & (1 << 22))
9433                           gen_helper_usat(tmp, cpu_env, tmp, tmp2);
9434                         else
9435                           gen_helper_ssat(tmp, cpu_env, tmp, tmp2);
9436                         tcg_temp_free_i32(tmp2);
9437                         store_reg(s, rd, tmp);
9438                     } else if ((insn & 0x00300fe0) == 0x00200f20) {
9439                         /* [us]sat16 */
9440                         tmp = load_reg(s, rm);
9441                         sh = (insn >> 16) & 0x1f;
9442                         tmp2 = tcg_const_i32(sh);
9443                         if (insn & (1 << 22))
9444                           gen_helper_usat16(tmp, cpu_env, tmp, tmp2);
9445                         else
9446                           gen_helper_ssat16(tmp, cpu_env, tmp, tmp2);
9447                         tcg_temp_free_i32(tmp2);
9448                         store_reg(s, rd, tmp);
9449                     } else if ((insn & 0x00700fe0) == 0x00000fa0) {
9450                         /* Select bytes.  */
9451                         tmp = load_reg(s, rn);
9452                         tmp2 = load_reg(s, rm);
9453                         tmp3 = tcg_temp_new_i32();
9454                         tcg_gen_ld_i32(tmp3, cpu_env, offsetof(CPUARMState, GE));
9455                         gen_helper_sel_flags(tmp, tmp3, tmp, tmp2);
9456                         tcg_temp_free_i32(tmp3);
9457                         tcg_temp_free_i32(tmp2);
9458                         store_reg(s, rd, tmp);
9459                     } else if ((insn & 0x000003e0) == 0x00000060) {
9460                         tmp = load_reg(s, rm);
9461                         shift = (insn >> 10) & 3;
9462                         /* ??? In many cases it's not necessary to do a
9463                            rotate, a shift is sufficient.  */
9464                         if (shift != 0)
9465                             tcg_gen_rotri_i32(tmp, tmp, shift * 8);
9466                         op1 = (insn >> 20) & 7;
9467                         switch (op1) {
9468                         case 0: gen_sxtb16(tmp);  break;
9469                         case 2: gen_sxtb(tmp);    break;
9470                         case 3: gen_sxth(tmp);    break;
9471                         case 4: gen_uxtb16(tmp);  break;
9472                         case 6: gen_uxtb(tmp);    break;
9473                         case 7: gen_uxth(tmp);    break;
9474                         default: goto illegal_op;
9475                         }
9476                         if (rn != 15) {
9477                             tmp2 = load_reg(s, rn);
9478                             if ((op1 & 3) == 0) {
9479                                 gen_add16(tmp, tmp2);
9480                             } else {
9481                                 tcg_gen_add_i32(tmp, tmp, tmp2);
9482                                 tcg_temp_free_i32(tmp2);
9483                             }
9484                         }
9485                         store_reg(s, rd, tmp);
9486                     } else if ((insn & 0x003f0f60) == 0x003f0f20) {
9487                         /* rev */
9488                         tmp = load_reg(s, rm);
9489                         if (insn & (1 << 22)) {
9490                             if (insn & (1 << 7)) {
9491                                 gen_revsh(tmp);
9492                             } else {
9493                                 ARCH(6T2);
9494                                 gen_helper_rbit(tmp, tmp);
9495                             }
9496                         } else {
9497                             if (insn & (1 << 7))
9498                                 gen_rev16(tmp);
9499                             else
9500                                 tcg_gen_bswap32_i32(tmp, tmp);
9501                         }
9502                         store_reg(s, rd, tmp);
9503                     } else {
9504                         goto illegal_op;
9505                     }
9506                     break;
9507                 case 2: /* Multiplies (Type 3).  */
9508                     switch ((insn >> 20) & 0x7) {
9509                     case 5:
9510                         if (((insn >> 6) ^ (insn >> 7)) & 1) {
9511                             /* op2 not 00x or 11x : UNDEF */
9512                             goto illegal_op;
9513                         }
9514                         /* Signed multiply most significant [accumulate].
9515                            (SMMUL, SMMLA, SMMLS) */
9516                         tmp = load_reg(s, rm);
9517                         tmp2 = load_reg(s, rs);
9518                         tmp64 = gen_muls_i64_i32(tmp, tmp2);
9519
9520                         if (rd != 15) {
9521                             tmp = load_reg(s, rd);
9522                             if (insn & (1 << 6)) {
9523                                 tmp64 = gen_subq_msw(tmp64, tmp);
9524                             } else {
9525                                 tmp64 = gen_addq_msw(tmp64, tmp);
9526                             }
9527                         }
9528                         if (insn & (1 << 5)) {
9529                             tcg_gen_addi_i64(tmp64, tmp64, 0x80000000u);
9530                         }
9531                         tcg_gen_shri_i64(tmp64, tmp64, 32);
9532                         tmp = tcg_temp_new_i32();
9533                         tcg_gen_extrl_i64_i32(tmp, tmp64);
9534                         tcg_temp_free_i64(tmp64);
9535                         store_reg(s, rn, tmp);
9536                         break;
9537                     case 0:
9538                     case 4:
9539                         /* SMLAD, SMUAD, SMLSD, SMUSD, SMLALD, SMLSLD */
9540                         if (insn & (1 << 7)) {
9541                             goto illegal_op;
9542                         }
9543                         tmp = load_reg(s, rm);
9544                         tmp2 = load_reg(s, rs);
9545                         if (insn & (1 << 5))
9546                             gen_swap_half(tmp2);
9547                         gen_smul_dual(tmp, tmp2);
9548                         if (insn & (1 << 22)) {
9549                             /* smlald, smlsld */
9550                             TCGv_i64 tmp64_2;
9551
9552                             tmp64 = tcg_temp_new_i64();
9553                             tmp64_2 = tcg_temp_new_i64();
9554                             tcg_gen_ext_i32_i64(tmp64, tmp);
9555                             tcg_gen_ext_i32_i64(tmp64_2, tmp2);
9556                             tcg_temp_free_i32(tmp);
9557                             tcg_temp_free_i32(tmp2);
9558                             if (insn & (1 << 6)) {
9559                                 tcg_gen_sub_i64(tmp64, tmp64, tmp64_2);
9560                             } else {
9561                                 tcg_gen_add_i64(tmp64, tmp64, tmp64_2);
9562                             }
9563                             tcg_temp_free_i64(tmp64_2);
9564                             gen_addq(s, tmp64, rd, rn);
9565                             gen_storeq_reg(s, rd, rn, tmp64);
9566                             tcg_temp_free_i64(tmp64);
9567                         } else {
9568                             /* smuad, smusd, smlad, smlsd */
9569                             if (insn & (1 << 6)) {
9570                                 /* This subtraction cannot overflow. */
9571                                 tcg_gen_sub_i32(tmp, tmp, tmp2);
9572                             } else {
9573                                 /* This addition cannot overflow 32 bits;
9574                                  * however it may overflow considered as a
9575                                  * signed operation, in which case we must set
9576                                  * the Q flag.
9577                                  */
9578                                 gen_helper_add_setq(tmp, cpu_env, tmp, tmp2);
9579                             }
9580                             tcg_temp_free_i32(tmp2);
9581                             if (rd != 15)
9582                               {
9583                                 tmp2 = load_reg(s, rd);
9584                                 gen_helper_add_setq(tmp, cpu_env, tmp, tmp2);
9585                                 tcg_temp_free_i32(tmp2);
9586                               }
9587                             store_reg(s, rn, tmp);
9588                         }
9589                         break;
9590                     case 1:
9591                     case 3:
9592                         /* SDIV, UDIV */
9593                         if (!arm_dc_feature(s, ARM_FEATURE_ARM_DIV)) {
9594                             goto illegal_op;
9595                         }
9596                         if (((insn >> 5) & 7) || (rd != 15)) {
9597                             goto illegal_op;
9598                         }
9599                         tmp = load_reg(s, rm);
9600                         tmp2 = load_reg(s, rs);
9601                         if (insn & (1 << 21)) {
9602                             gen_helper_udiv(tmp, tmp, tmp2);
9603                         } else {
9604                             gen_helper_sdiv(tmp, tmp, tmp2);
9605                         }
9606                         tcg_temp_free_i32(tmp2);
9607                         store_reg(s, rn, tmp);
9608                         break;
9609                     default:
9610                         goto illegal_op;
9611                     }
9612                     break;
9613                 case 3:
9614                     op1 = ((insn >> 17) & 0x38) | ((insn >> 5) & 7);
9615                     switch (op1) {
9616                     case 0: /* Unsigned sum of absolute differences.  */
9617                         ARCH(6);
9618                         tmp = load_reg(s, rm);
9619                         tmp2 = load_reg(s, rs);
9620                         gen_helper_usad8(tmp, tmp, tmp2);
9621                         tcg_temp_free_i32(tmp2);
9622                         if (rd != 15) {
9623                             tmp2 = load_reg(s, rd);
9624                             tcg_gen_add_i32(tmp, tmp, tmp2);
9625                             tcg_temp_free_i32(tmp2);
9626                         }
9627                         store_reg(s, rn, tmp);
9628                         break;
9629                     case 0x20: case 0x24: case 0x28: case 0x2c:
9630                         /* Bitfield insert/clear.  */
9631                         ARCH(6T2);
9632                         shift = (insn >> 7) & 0x1f;
9633                         i = (insn >> 16) & 0x1f;
9634                         if (i < shift) {
9635                             /* UNPREDICTABLE; we choose to UNDEF */
9636                             goto illegal_op;
9637                         }
9638                         i = i + 1 - shift;
9639                         if (rm == 15) {
9640                             tmp = tcg_temp_new_i32();
9641                             tcg_gen_movi_i32(tmp, 0);
9642                         } else {
9643                             tmp = load_reg(s, rm);
9644                         }
9645                         if (i != 32) {
9646                             tmp2 = load_reg(s, rd);
9647                             tcg_gen_deposit_i32(tmp, tmp2, tmp, shift, i);
9648                             tcg_temp_free_i32(tmp2);
9649                         }
9650                         store_reg(s, rd, tmp);
9651                         break;
9652                     case 0x12: case 0x16: case 0x1a: case 0x1e: /* sbfx */
9653                     case 0x32: case 0x36: case 0x3a: case 0x3e: /* ubfx */
9654                         ARCH(6T2);
9655                         tmp = load_reg(s, rm);
9656                         shift = (insn >> 7) & 0x1f;
9657                         i = ((insn >> 16) & 0x1f) + 1;
9658                         if (shift + i > 32)
9659                             goto illegal_op;
9660                         if (i < 32) {
9661                             if (op1 & 0x20) {
9662                                 tcg_gen_extract_i32(tmp, tmp, shift, i);
9663                             } else {
9664                                 tcg_gen_sextract_i32(tmp, tmp, shift, i);
9665                             }
9666                         }
9667                         store_reg(s, rd, tmp);
9668                         break;
9669                     default:
9670                         goto illegal_op;
9671                     }
9672                     break;
9673                 }
9674                 break;
9675             }
9676         do_ldst:
9677             /* Check for undefined extension instructions
9678              * per the ARM Bible IE:
9679              * xxxx 0111 1111 xxxx  xxxx xxxx 1111 xxxx
9680              */
9681             sh = (0xf << 20) | (0xf << 4);
9682             if (op1 == 0x7 && ((insn & sh) == sh))
9683             {
9684                 goto illegal_op;
9685             }
9686             /* load/store byte/word */
9687             rn = (insn >> 16) & 0xf;
9688             rd = (insn >> 12) & 0xf;
9689             tmp2 = load_reg(s, rn);
9690             if ((insn & 0x01200000) == 0x00200000) {
9691                 /* ldrt/strt */
9692                 i = get_a32_user_mem_index(s);
9693             } else {
9694                 i = get_mem_index(s);
9695             }
9696             if (insn & (1 << 24))
9697                 gen_add_data_offset(s, insn, tmp2);
9698             if (insn & (1 << 20)) {
9699                 /* load */
9700                 tmp = tcg_temp_new_i32();
9701                 if (insn & (1 << 22)) {
9702                     gen_aa32_ld8u_iss(s, tmp, tmp2, i, rd);
9703                 } else {
9704                     gen_aa32_ld32u_iss(s, tmp, tmp2, i, rd);
9705                 }
9706             } else {
9707                 /* store */
9708                 tmp = load_reg(s, rd);
9709                 if (insn & (1 << 22)) {
9710                     gen_aa32_st8_iss(s, tmp, tmp2, i, rd);
9711                 } else {
9712                     gen_aa32_st32_iss(s, tmp, tmp2, i, rd);
9713                 }
9714                 tcg_temp_free_i32(tmp);
9715             }
9716             if (!(insn & (1 << 24))) {
9717                 gen_add_data_offset(s, insn, tmp2);
9718                 store_reg(s, rn, tmp2);
9719             } else if (insn & (1 << 21)) {
9720                 store_reg(s, rn, tmp2);
9721             } else {
9722                 tcg_temp_free_i32(tmp2);
9723             }
9724             if (insn & (1 << 20)) {
9725                 /* Complete the load.  */
9726                 store_reg_from_load(s, rd, tmp);
9727             }
9728             break;
9729         case 0x08:
9730         case 0x09:
9731             {
9732                 int j, n, loaded_base;
9733                 bool exc_return = false;
9734                 bool is_load = extract32(insn, 20, 1);
9735                 bool user = false;
9736                 TCGv_i32 loaded_var;
9737                 /* load/store multiple words */
9738                 /* XXX: store correct base if write back */
9739                 if (insn & (1 << 22)) {
9740                     /* LDM (user), LDM (exception return) and STM (user) */
9741                     if (IS_USER(s))
9742                         goto illegal_op; /* only usable in supervisor mode */
9743
9744                     if (is_load && extract32(insn, 15, 1)) {
9745                         exc_return = true;
9746                     } else {
9747                         user = true;
9748                     }
9749                 }
9750                 rn = (insn >> 16) & 0xf;
9751                 addr = load_reg(s, rn);
9752
9753                 /* compute total size */
9754                 loaded_base = 0;
9755                 loaded_var = NULL;
9756                 n = 0;
9757                 for(i=0;i<16;i++) {
9758                     if (insn & (1 << i))
9759                         n++;
9760                 }
9761                 /* XXX: test invalid n == 0 case ? */
9762                 if (insn & (1 << 23)) {
9763                     if (insn & (1 << 24)) {
9764                         /* pre increment */
9765                         tcg_gen_addi_i32(addr, addr, 4);
9766                     } else {
9767                         /* post increment */
9768                     }
9769                 } else {
9770                     if (insn & (1 << 24)) {
9771                         /* pre decrement */
9772                         tcg_gen_addi_i32(addr, addr, -(n * 4));
9773                     } else {
9774                         /* post decrement */
9775                         if (n != 1)
9776                         tcg_gen_addi_i32(addr, addr, -((n - 1) * 4));
9777                     }
9778                 }
9779                 j = 0;
9780                 for(i=0;i<16;i++) {
9781                     if (insn & (1 << i)) {
9782                         if (is_load) {
9783                             /* load */
9784                             tmp = tcg_temp_new_i32();
9785                             gen_aa32_ld32u(s, tmp, addr, get_mem_index(s));
9786                             if (user) {
9787                                 tmp2 = tcg_const_i32(i);
9788                                 gen_helper_set_user_reg(cpu_env, tmp2, tmp);
9789                                 tcg_temp_free_i32(tmp2);
9790                                 tcg_temp_free_i32(tmp);
9791                             } else if (i == rn) {
9792                                 loaded_var = tmp;
9793                                 loaded_base = 1;
9794                             } else if (rn == 15 && exc_return) {
9795                                 store_pc_exc_ret(s, tmp);
9796                             } else {
9797                                 store_reg_from_load(s, i, tmp);
9798                             }
9799                         } else {
9800                             /* store */
9801                             if (i == 15) {
9802                                 /* special case: r15 = PC + 8 */
9803                                 val = (long)s->pc + 4;
9804                                 tmp = tcg_temp_new_i32();
9805                                 tcg_gen_movi_i32(tmp, val);
9806                             } else if (user) {
9807                                 tmp = tcg_temp_new_i32();
9808                                 tmp2 = tcg_const_i32(i);
9809                                 gen_helper_get_user_reg(tmp, cpu_env, tmp2);
9810                                 tcg_temp_free_i32(tmp2);
9811                             } else {
9812                                 tmp = load_reg(s, i);
9813                             }
9814                             gen_aa32_st32(s, tmp, addr, get_mem_index(s));
9815                             tcg_temp_free_i32(tmp);
9816                         }
9817                         j++;
9818                         /* no need to add after the last transfer */
9819                         if (j != n)
9820                             tcg_gen_addi_i32(addr, addr, 4);
9821                     }
9822                 }
9823                 if (insn & (1 << 21)) {
9824                     /* write back */
9825                     if (insn & (1 << 23)) {
9826                         if (insn & (1 << 24)) {
9827                             /* pre increment */
9828                         } else {
9829                             /* post increment */
9830                             tcg_gen_addi_i32(addr, addr, 4);
9831                         }
9832                     } else {
9833                         if (insn & (1 << 24)) {
9834                             /* pre decrement */
9835                             if (n != 1)
9836                                 tcg_gen_addi_i32(addr, addr, -((n - 1) * 4));
9837                         } else {
9838                             /* post decrement */
9839                             tcg_gen_addi_i32(addr, addr, -(n * 4));
9840                         }
9841                     }
9842                     store_reg(s, rn, addr);
9843                 } else {
9844                     tcg_temp_free_i32(addr);
9845                 }
9846                 if (loaded_base) {
9847                     store_reg(s, rn, loaded_var);
9848                 }
9849                 if (exc_return) {
9850                     /* Restore CPSR from SPSR.  */
9851                     tmp = load_cpu_field(spsr);
9852                     if (tb_cflags(s->base.tb) & CF_USE_ICOUNT) {
9853                         gen_io_start();
9854                     }
9855                     gen_helper_cpsr_write_eret(cpu_env, tmp);
9856                     if (tb_cflags(s->base.tb) & CF_USE_ICOUNT) {
9857                         gen_io_end();
9858                     }
9859                     tcg_temp_free_i32(tmp);
9860                     /* Must exit loop to check un-masked IRQs */
9861                     s->base.is_jmp = DISAS_EXIT;
9862                 }
9863             }
9864             break;
9865         case 0xa:
9866         case 0xb:
9867             {
9868                 int32_t offset;
9869
9870                 /* branch (and link) */
9871                 val = (int32_t)s->pc;
9872                 if (insn & (1 << 24)) {
9873                     tmp = tcg_temp_new_i32();
9874                     tcg_gen_movi_i32(tmp, val);
9875                     store_reg(s, 14, tmp);
9876                 }
9877                 offset = sextract32(insn << 2, 0, 26);
9878                 val += offset + 4;
9879                 gen_jmp(s, val);
9880             }
9881             break;
9882         case 0xc:
9883         case 0xd:
9884         case 0xe:
9885             if (((insn >> 8) & 0xe) == 10) {
9886                 /* VFP.  */
9887                 if (disas_vfp_insn(s, insn)) {
9888                     goto illegal_op;
9889                 }
9890             } else if (disas_coproc_insn(s, insn)) {
9891                 /* Coprocessor.  */
9892                 goto illegal_op;
9893             }
9894             break;
9895         case 0xf:
9896             /* swi */
9897             gen_set_pc_im(s, s->pc);
9898             s->svc_imm = extract32(insn, 0, 24);
9899             s->base.is_jmp = DISAS_SWI;
9900             break;
9901         default:
9902         illegal_op:
9903             gen_exception_insn(s, 4, EXCP_UDEF, syn_uncategorized(),
9904                                default_exception_el(s));
9905             break;
9906         }
9907     }
9908 }
9909
9910 static bool thumb_insn_is_16bit(DisasContext *s, uint32_t insn)
9911 {
9912     /* Return true if this is a 16 bit instruction. We must be precise
9913      * about this (matching the decode).  We assume that s->pc still
9914      * points to the first 16 bits of the insn.
9915      */
9916     if ((insn >> 11) < 0x1d) {
9917         /* Definitely a 16-bit instruction */
9918         return true;
9919     }
9920
9921     /* Top five bits 0b11101 / 0b11110 / 0b11111 : this is the
9922      * first half of a 32-bit Thumb insn. Thumb-1 cores might
9923      * end up actually treating this as two 16-bit insns, though,
9924      * if it's half of a bl/blx pair that might span a page boundary.
9925      */
9926     if (arm_dc_feature(s, ARM_FEATURE_THUMB2)) {
9927         /* Thumb2 cores (including all M profile ones) always treat
9928          * 32-bit insns as 32-bit.
9929          */
9930         return false;
9931     }
9932
9933     if ((insn >> 11) == 0x1e && (s->pc < s->next_page_start - 3)) {
9934         /* 0b1111_0xxx_xxxx_xxxx : BL/BLX prefix, and the suffix
9935          * is not on the next page; we merge this into a 32-bit
9936          * insn.
9937          */
9938         return false;
9939     }
9940     /* 0b1110_1xxx_xxxx_xxxx : BLX suffix (or UNDEF);
9941      * 0b1111_1xxx_xxxx_xxxx : BL suffix;
9942      * 0b1111_0xxx_xxxx_xxxx : BL/BLX prefix on the end of a page
9943      *  -- handle as single 16 bit insn
9944      */
9945     return true;
9946 }
9947
9948 /* Return true if this is a Thumb-2 logical op.  */
9949 static int
9950 thumb2_logic_op(int op)
9951 {
9952     return (op < 8);
9953 }
9954
9955 /* Generate code for a Thumb-2 data processing operation.  If CONDS is nonzero
9956    then set condition code flags based on the result of the operation.
9957    If SHIFTER_OUT is nonzero then set the carry flag for logical operations
9958    to the high bit of T1.
9959    Returns zero if the opcode is valid.  */
9960
9961 static int
9962 gen_thumb2_data_op(DisasContext *s, int op, int conds, uint32_t shifter_out,
9963                    TCGv_i32 t0, TCGv_i32 t1)
9964 {
9965     int logic_cc;
9966
9967     logic_cc = 0;
9968     switch (op) {
9969     case 0: /* and */
9970         tcg_gen_and_i32(t0, t0, t1);
9971         logic_cc = conds;
9972         break;
9973     case 1: /* bic */
9974         tcg_gen_andc_i32(t0, t0, t1);
9975         logic_cc = conds;
9976         break;
9977     case 2: /* orr */
9978         tcg_gen_or_i32(t0, t0, t1);
9979         logic_cc = conds;
9980         break;
9981     case 3: /* orn */
9982         tcg_gen_orc_i32(t0, t0, t1);
9983         logic_cc = conds;
9984         break;
9985     case 4: /* eor */
9986         tcg_gen_xor_i32(t0, t0, t1);
9987         logic_cc = conds;
9988         break;
9989     case 8: /* add */
9990         if (conds)
9991             gen_add_CC(t0, t0, t1);
9992         else
9993             tcg_gen_add_i32(t0, t0, t1);
9994         break;
9995     case 10: /* adc */
9996         if (conds)
9997             gen_adc_CC(t0, t0, t1);
9998         else
9999             gen_adc(t0, t1);
10000         break;
10001     case 11: /* sbc */
10002         if (conds) {
10003             gen_sbc_CC(t0, t0, t1);
10004         } else {
10005             gen_sub_carry(t0, t0, t1);
10006         }
10007         break;
10008     case 13: /* sub */
10009         if (conds)
10010             gen_sub_CC(t0, t0, t1);
10011         else
10012             tcg_gen_sub_i32(t0, t0, t1);
10013         break;
10014     case 14: /* rsb */
10015         if (conds)
10016             gen_sub_CC(t0, t1, t0);
10017         else
10018             tcg_gen_sub_i32(t0, t1, t0);
10019         break;
10020     default: /* 5, 6, 7, 9, 12, 15. */
10021         return 1;
10022     }
10023     if (logic_cc) {
10024         gen_logic_CC(t0);
10025         if (shifter_out)
10026             gen_set_CF_bit31(t1);
10027     }
10028     return 0;
10029 }
10030
10031 /* Translate a 32-bit thumb instruction. */
10032 static void disas_thumb2_insn(DisasContext *s, uint32_t insn)
10033 {
10034     uint32_t imm, shift, offset;
10035     uint32_t rd, rn, rm, rs;
10036     TCGv_i32 tmp;
10037     TCGv_i32 tmp2;
10038     TCGv_i32 tmp3;
10039     TCGv_i32 addr;
10040     TCGv_i64 tmp64;
10041     int op;
10042     int shiftop;
10043     int conds;
10044     int logic_cc;
10045
10046     /* The only 32 bit insn that's allowed for Thumb1 is the combined
10047      * BL/BLX prefix and suffix.
10048      */
10049     if ((insn & 0xf800e800) != 0xf000e800) {
10050         ARCH(6T2);
10051     }
10052
10053     rn = (insn >> 16) & 0xf;
10054     rs = (insn >> 12) & 0xf;
10055     rd = (insn >> 8) & 0xf;
10056     rm = insn & 0xf;
10057     switch ((insn >> 25) & 0xf) {
10058     case 0: case 1: case 2: case 3:
10059         /* 16-bit instructions.  Should never happen.  */
10060         abort();
10061     case 4:
10062         if (insn & (1 << 22)) {
10063             /* 0b1110_100x_x1xx_xxxx_xxxx_xxxx_xxxx_xxxx
10064              * - load/store doubleword, load/store exclusive, ldacq/strel,
10065              *   table branch, TT.
10066              */
10067             if (insn == 0xe97fe97f && arm_dc_feature(s, ARM_FEATURE_M) &&
10068                 arm_dc_feature(s, ARM_FEATURE_V8)) {
10069                 /* 0b1110_1001_0111_1111_1110_1001_0111_111
10070                  *  - SG (v8M only)
10071                  * The bulk of the behaviour for this instruction is implemented
10072                  * in v7m_handle_execute_nsc(), which deals with the insn when
10073                  * it is executed by a CPU in non-secure state from memory
10074                  * which is Secure & NonSecure-Callable.
10075                  * Here we only need to handle the remaining cases:
10076                  *  * in NS memory (including the "security extension not
10077                  *    implemented" case) : NOP
10078                  *  * in S memory but CPU already secure (clear IT bits)
10079                  * We know that the attribute for the memory this insn is
10080                  * in must match the current CPU state, because otherwise
10081                  * get_phys_addr_pmsav8 would have generated an exception.
10082                  */
10083                 if (s->v8m_secure) {
10084                     /* Like the IT insn, we don't need to generate any code */
10085                     s->condexec_cond = 0;
10086                     s->condexec_mask = 0;
10087                 }
10088             } else if (insn & 0x01200000) {
10089                 /* 0b1110_1000_x11x_xxxx_xxxx_xxxx_xxxx_xxxx
10090                  *  - load/store dual (post-indexed)
10091                  * 0b1111_1001_x10x_xxxx_xxxx_xxxx_xxxx_xxxx
10092                  *  - load/store dual (literal and immediate)
10093                  * 0b1111_1001_x11x_xxxx_xxxx_xxxx_xxxx_xxxx
10094                  *  - load/store dual (pre-indexed)
10095                  */
10096                 if (rn == 15) {
10097                     if (insn & (1 << 21)) {
10098                         /* UNPREDICTABLE */
10099                         goto illegal_op;
10100                     }
10101                     addr = tcg_temp_new_i32();
10102                     tcg_gen_movi_i32(addr, s->pc & ~3);
10103                 } else {
10104                     addr = load_reg(s, rn);
10105                 }
10106                 offset = (insn & 0xff) * 4;
10107                 if ((insn & (1 << 23)) == 0)
10108                     offset = -offset;
10109                 if (insn & (1 << 24)) {
10110                     tcg_gen_addi_i32(addr, addr, offset);
10111                     offset = 0;
10112                 }
10113                 if (insn & (1 << 20)) {
10114                     /* ldrd */
10115                     tmp = tcg_temp_new_i32();
10116                     gen_aa32_ld32u(s, tmp, addr, get_mem_index(s));
10117                     store_reg(s, rs, tmp);
10118                     tcg_gen_addi_i32(addr, addr, 4);
10119                     tmp = tcg_temp_new_i32();
10120                     gen_aa32_ld32u(s, tmp, addr, get_mem_index(s));
10121                     store_reg(s, rd, tmp);
10122                 } else {
10123                     /* strd */
10124                     tmp = load_reg(s, rs);
10125                     gen_aa32_st32(s, tmp, addr, get_mem_index(s));
10126                     tcg_temp_free_i32(tmp);
10127                     tcg_gen_addi_i32(addr, addr, 4);
10128                     tmp = load_reg(s, rd);
10129                     gen_aa32_st32(s, tmp, addr, get_mem_index(s));
10130                     tcg_temp_free_i32(tmp);
10131                 }
10132                 if (insn & (1 << 21)) {
10133                     /* Base writeback.  */
10134                     tcg_gen_addi_i32(addr, addr, offset - 4);
10135                     store_reg(s, rn, addr);
10136                 } else {
10137                     tcg_temp_free_i32(addr);
10138                 }
10139             } else if ((insn & (1 << 23)) == 0) {
10140                 /* 0b1110_1000_010x_xxxx_xxxx_xxxx_xxxx_xxxx
10141                  * - load/store exclusive word
10142                  * - TT (v8M only)
10143                  */
10144                 if (rs == 15) {
10145                     if (!(insn & (1 << 20)) &&
10146                         arm_dc_feature(s, ARM_FEATURE_M) &&
10147                         arm_dc_feature(s, ARM_FEATURE_V8)) {
10148                         /* 0b1110_1000_0100_xxxx_1111_xxxx_xxxx_xxxx
10149                          *  - TT (v8M only)
10150                          */
10151                         bool alt = insn & (1 << 7);
10152                         TCGv_i32 addr, op, ttresp;
10153
10154                         if ((insn & 0x3f) || rd == 13 || rd == 15 || rn == 15) {
10155                             /* we UNDEF for these UNPREDICTABLE cases */
10156                             goto illegal_op;
10157                         }
10158
10159                         if (alt && !s->v8m_secure) {
10160                             goto illegal_op;
10161                         }
10162
10163                         addr = load_reg(s, rn);
10164                         op = tcg_const_i32(extract32(insn, 6, 2));
10165                         ttresp = tcg_temp_new_i32();
10166                         gen_helper_v7m_tt(ttresp, cpu_env, addr, op);
10167                         tcg_temp_free_i32(addr);
10168                         tcg_temp_free_i32(op);
10169                         store_reg(s, rd, ttresp);
10170                         break;
10171                     }
10172                     goto illegal_op;
10173                 }
10174                 addr = tcg_temp_local_new_i32();
10175                 load_reg_var(s, addr, rn);
10176                 tcg_gen_addi_i32(addr, addr, (insn & 0xff) << 2);
10177                 if (insn & (1 << 20)) {
10178                     gen_load_exclusive(s, rs, 15, addr, 2);
10179                 } else {
10180                     gen_store_exclusive(s, rd, rs, 15, addr, 2);
10181                 }
10182                 tcg_temp_free_i32(addr);
10183             } else if ((insn & (7 << 5)) == 0) {
10184                 /* Table Branch.  */
10185                 if (rn == 15) {
10186                     addr = tcg_temp_new_i32();
10187                     tcg_gen_movi_i32(addr, s->pc);
10188                 } else {
10189                     addr = load_reg(s, rn);
10190                 }
10191                 tmp = load_reg(s, rm);
10192                 tcg_gen_add_i32(addr, addr, tmp);
10193                 if (insn & (1 << 4)) {
10194                     /* tbh */
10195                     tcg_gen_add_i32(addr, addr, tmp);
10196                     tcg_temp_free_i32(tmp);
10197                     tmp = tcg_temp_new_i32();
10198                     gen_aa32_ld16u(s, tmp, addr, get_mem_index(s));
10199                 } else { /* tbb */
10200                     tcg_temp_free_i32(tmp);
10201                     tmp = tcg_temp_new_i32();
10202                     gen_aa32_ld8u(s, tmp, addr, get_mem_index(s));
10203                 }
10204                 tcg_temp_free_i32(addr);
10205                 tcg_gen_shli_i32(tmp, tmp, 1);
10206                 tcg_gen_addi_i32(tmp, tmp, s->pc);
10207                 store_reg(s, 15, tmp);
10208             } else {
10209                 int op2 = (insn >> 6) & 0x3;
10210                 op = (insn >> 4) & 0x3;
10211                 switch (op2) {
10212                 case 0:
10213                     goto illegal_op;
10214                 case 1:
10215                     /* Load/store exclusive byte/halfword/doubleword */
10216                     if (op == 2) {
10217                         goto illegal_op;
10218                     }
10219                     ARCH(7);
10220                     break;
10221                 case 2:
10222                     /* Load-acquire/store-release */
10223                     if (op == 3) {
10224                         goto illegal_op;
10225                     }
10226                     /* Fall through */
10227                 case 3:
10228                     /* Load-acquire/store-release exclusive */
10229                     ARCH(8);
10230                     break;
10231                 }
10232                 addr = tcg_temp_local_new_i32();
10233                 load_reg_var(s, addr, rn);
10234                 if (!(op2 & 1)) {
10235                     if (insn & (1 << 20)) {
10236                         tmp = tcg_temp_new_i32();
10237                         switch (op) {
10238                         case 0: /* ldab */
10239                             gen_aa32_ld8u_iss(s, tmp, addr, get_mem_index(s),
10240                                               rs | ISSIsAcqRel);
10241                             break;
10242                         case 1: /* ldah */
10243                             gen_aa32_ld16u_iss(s, tmp, addr, get_mem_index(s),
10244                                                rs | ISSIsAcqRel);
10245                             break;
10246                         case 2: /* lda */
10247                             gen_aa32_ld32u_iss(s, tmp, addr, get_mem_index(s),
10248                                                rs | ISSIsAcqRel);
10249                             break;
10250                         default:
10251                             abort();
10252                         }
10253                         store_reg(s, rs, tmp);
10254                     } else {
10255                         tmp = load_reg(s, rs);
10256                         switch (op) {
10257                         case 0: /* stlb */
10258                             gen_aa32_st8_iss(s, tmp, addr, get_mem_index(s),
10259                                              rs | ISSIsAcqRel);
10260                             break;
10261                         case 1: /* stlh */
10262                             gen_aa32_st16_iss(s, tmp, addr, get_mem_index(s),
10263                                               rs | ISSIsAcqRel);
10264                             break;
10265                         case 2: /* stl */
10266                             gen_aa32_st32_iss(s, tmp, addr, get_mem_index(s),
10267                                               rs | ISSIsAcqRel);
10268                             break;
10269                         default:
10270                             abort();
10271                         }
10272                         tcg_temp_free_i32(tmp);
10273                     }
10274                 } else if (insn & (1 << 20)) {
10275                     gen_load_exclusive(s, rs, rd, addr, op);
10276                 } else {
10277                     gen_store_exclusive(s, rm, rs, rd, addr, op);
10278                 }
10279                 tcg_temp_free_i32(addr);
10280             }
10281         } else {
10282             /* Load/store multiple, RFE, SRS.  */
10283             if (((insn >> 23) & 1) == ((insn >> 24) & 1)) {
10284                 /* RFE, SRS: not available in user mode or on M profile */
10285                 if (IS_USER(s) || arm_dc_feature(s, ARM_FEATURE_M)) {
10286                     goto illegal_op;
10287                 }
10288                 if (insn & (1 << 20)) {
10289                     /* rfe */
10290                     addr = load_reg(s, rn);
10291                     if ((insn & (1 << 24)) == 0)
10292                         tcg_gen_addi_i32(addr, addr, -8);
10293                     /* Load PC into tmp and CPSR into tmp2.  */
10294                     tmp = tcg_temp_new_i32();
10295                     gen_aa32_ld32u(s, tmp, addr, get_mem_index(s));
10296                     tcg_gen_addi_i32(addr, addr, 4);
10297                     tmp2 = tcg_temp_new_i32();
10298                     gen_aa32_ld32u(s, tmp2, addr, get_mem_index(s));
10299                     if (insn & (1 << 21)) {
10300                         /* Base writeback.  */
10301                         if (insn & (1 << 24)) {
10302                             tcg_gen_addi_i32(addr, addr, 4);
10303                         } else {
10304                             tcg_gen_addi_i32(addr, addr, -4);
10305                         }
10306                         store_reg(s, rn, addr);
10307                     } else {
10308                         tcg_temp_free_i32(addr);
10309                     }
10310                     gen_rfe(s, tmp, tmp2);
10311                 } else {
10312                     /* srs */
10313                     gen_srs(s, (insn & 0x1f), (insn & (1 << 24)) ? 1 : 2,
10314                             insn & (1 << 21));
10315                 }
10316             } else {
10317                 int i, loaded_base = 0;
10318                 TCGv_i32 loaded_var;
10319                 /* Load/store multiple.  */
10320                 addr = load_reg(s, rn);
10321                 offset = 0;
10322                 for (i = 0; i < 16; i++) {
10323                     if (insn & (1 << i))
10324                         offset += 4;
10325                 }
10326                 if (insn & (1 << 24)) {
10327                     tcg_gen_addi_i32(addr, addr, -offset);
10328                 }
10329
10330                 loaded_var = NULL;
10331                 for (i = 0; i < 16; i++) {
10332                     if ((insn & (1 << i)) == 0)
10333                         continue;
10334                     if (insn & (1 << 20)) {
10335                         /* Load.  */
10336                         tmp = tcg_temp_new_i32();
10337                         gen_aa32_ld32u(s, tmp, addr, get_mem_index(s));
10338                         if (i == 15) {
10339                             gen_bx_excret(s, tmp);
10340                         } else if (i == rn) {
10341                             loaded_var = tmp;
10342                             loaded_base = 1;
10343                         } else {
10344                             store_reg(s, i, tmp);
10345                         }
10346                     } else {
10347                         /* Store.  */
10348                         tmp = load_reg(s, i);
10349                         gen_aa32_st32(s, tmp, addr, get_mem_index(s));
10350                         tcg_temp_free_i32(tmp);
10351                     }
10352                     tcg_gen_addi_i32(addr, addr, 4);
10353                 }
10354                 if (loaded_base) {
10355                     store_reg(s, rn, loaded_var);
10356                 }
10357                 if (insn & (1 << 21)) {
10358                     /* Base register writeback.  */
10359                     if (insn & (1 << 24)) {
10360                         tcg_gen_addi_i32(addr, addr, -offset);
10361                     }
10362                     /* Fault if writeback register is in register list.  */
10363                     if (insn & (1 << rn))
10364                         goto illegal_op;
10365                     store_reg(s, rn, addr);
10366                 } else {
10367                     tcg_temp_free_i32(addr);
10368                 }
10369             }
10370         }
10371         break;
10372     case 5:
10373
10374         op = (insn >> 21) & 0xf;
10375         if (op == 6) {
10376             if (!arm_dc_feature(s, ARM_FEATURE_THUMB_DSP)) {
10377                 goto illegal_op;
10378             }
10379             /* Halfword pack.  */
10380             tmp = load_reg(s, rn);
10381             tmp2 = load_reg(s, rm);
10382             shift = ((insn >> 10) & 0x1c) | ((insn >> 6) & 0x3);
10383             if (insn & (1 << 5)) {
10384                 /* pkhtb */
10385                 if (shift == 0)
10386                     shift = 31;
10387                 tcg_gen_sari_i32(tmp2, tmp2, shift);
10388                 tcg_gen_andi_i32(tmp, tmp, 0xffff0000);
10389                 tcg_gen_ext16u_i32(tmp2, tmp2);
10390             } else {
10391                 /* pkhbt */
10392                 if (shift)
10393                     tcg_gen_shli_i32(tmp2, tmp2, shift);
10394                 tcg_gen_ext16u_i32(tmp, tmp);
10395                 tcg_gen_andi_i32(tmp2, tmp2, 0xffff0000);
10396             }
10397             tcg_gen_or_i32(tmp, tmp, tmp2);
10398             tcg_temp_free_i32(tmp2);
10399             store_reg(s, rd, tmp);
10400         } else {
10401             /* Data processing register constant shift.  */
10402             if (rn == 15) {
10403                 tmp = tcg_temp_new_i32();
10404                 tcg_gen_movi_i32(tmp, 0);
10405             } else {
10406                 tmp = load_reg(s, rn);
10407             }
10408             tmp2 = load_reg(s, rm);
10409
10410             shiftop = (insn >> 4) & 3;
10411             shift = ((insn >> 6) & 3) | ((insn >> 10) & 0x1c);
10412             conds = (insn & (1 << 20)) != 0;
10413             logic_cc = (conds && thumb2_logic_op(op));
10414             gen_arm_shift_im(tmp2, shiftop, shift, logic_cc);
10415             if (gen_thumb2_data_op(s, op, conds, 0, tmp, tmp2))
10416                 goto illegal_op;
10417             tcg_temp_free_i32(tmp2);
10418             if (rd != 15) {
10419                 store_reg(s, rd, tmp);
10420             } else {
10421                 tcg_temp_free_i32(tmp);
10422             }
10423         }
10424         break;
10425     case 13: /* Misc data processing.  */
10426         op = ((insn >> 22) & 6) | ((insn >> 7) & 1);
10427         if (op < 4 && (insn & 0xf000) != 0xf000)
10428             goto illegal_op;
10429         switch (op) {
10430         case 0: /* Register controlled shift.  */
10431             tmp = load_reg(s, rn);
10432             tmp2 = load_reg(s, rm);
10433             if ((insn & 0x70) != 0)
10434                 goto illegal_op;
10435             op = (insn >> 21) & 3;
10436             logic_cc = (insn & (1 << 20)) != 0;
10437             gen_arm_shift_reg(tmp, op, tmp2, logic_cc);
10438             if (logic_cc)
10439                 gen_logic_CC(tmp);
10440             store_reg(s, rd, tmp);
10441             break;
10442         case 1: /* Sign/zero extend.  */
10443             op = (insn >> 20) & 7;
10444             switch (op) {
10445             case 0: /* SXTAH, SXTH */
10446             case 1: /* UXTAH, UXTH */
10447             case 4: /* SXTAB, SXTB */
10448             case 5: /* UXTAB, UXTB */
10449                 break;
10450             case 2: /* SXTAB16, SXTB16 */
10451             case 3: /* UXTAB16, UXTB16 */
10452                 if (!arm_dc_feature(s, ARM_FEATURE_THUMB_DSP)) {
10453                     goto illegal_op;
10454                 }
10455                 break;
10456             default:
10457                 goto illegal_op;
10458             }
10459             if (rn != 15) {
10460                 if (!arm_dc_feature(s, ARM_FEATURE_THUMB_DSP)) {
10461                     goto illegal_op;
10462                 }
10463             }
10464             tmp = load_reg(s, rm);
10465             shift = (insn >> 4) & 3;
10466             /* ??? In many cases it's not necessary to do a
10467                rotate, a shift is sufficient.  */
10468             if (shift != 0)
10469                 tcg_gen_rotri_i32(tmp, tmp, shift * 8);
10470             op = (insn >> 20) & 7;
10471             switch (op) {
10472             case 0: gen_sxth(tmp);   break;
10473             case 1: gen_uxth(tmp);   break;
10474             case 2: gen_sxtb16(tmp); break;
10475             case 3: gen_uxtb16(tmp); break;
10476             case 4: gen_sxtb(tmp);   break;
10477             case 5: gen_uxtb(tmp);   break;
10478             default:
10479                 g_assert_not_reached();
10480             }
10481             if (rn != 15) {
10482                 tmp2 = load_reg(s, rn);
10483                 if ((op >> 1) == 1) {
10484                     gen_add16(tmp, tmp2);
10485                 } else {
10486                     tcg_gen_add_i32(tmp, tmp, tmp2);
10487                     tcg_temp_free_i32(tmp2);
10488                 }
10489             }
10490             store_reg(s, rd, tmp);
10491             break;
10492         case 2: /* SIMD add/subtract.  */
10493             if (!arm_dc_feature(s, ARM_FEATURE_THUMB_DSP)) {
10494                 goto illegal_op;
10495             }
10496             op = (insn >> 20) & 7;
10497             shift = (insn >> 4) & 7;
10498             if ((op & 3) == 3 || (shift & 3) == 3)
10499                 goto illegal_op;
10500             tmp = load_reg(s, rn);
10501             tmp2 = load_reg(s, rm);
10502             gen_thumb2_parallel_addsub(op, shift, tmp, tmp2);
10503             tcg_temp_free_i32(tmp2);
10504             store_reg(s, rd, tmp);
10505             break;
10506         case 3: /* Other data processing.  */
10507             op = ((insn >> 17) & 0x38) | ((insn >> 4) & 7);
10508             if (op < 4) {
10509                 /* Saturating add/subtract.  */
10510                 if (!arm_dc_feature(s, ARM_FEATURE_THUMB_DSP)) {
10511                     goto illegal_op;
10512                 }
10513                 tmp = load_reg(s, rn);
10514                 tmp2 = load_reg(s, rm);
10515                 if (op & 1)
10516                     gen_helper_double_saturate(tmp, cpu_env, tmp);
10517                 if (op & 2)
10518                     gen_helper_sub_saturate(tmp, cpu_env, tmp2, tmp);
10519                 else
10520                     gen_helper_add_saturate(tmp, cpu_env, tmp, tmp2);
10521                 tcg_temp_free_i32(tmp2);
10522             } else {
10523                 switch (op) {
10524                 case 0x0a: /* rbit */
10525                 case 0x08: /* rev */
10526                 case 0x09: /* rev16 */
10527                 case 0x0b: /* revsh */
10528                 case 0x18: /* clz */
10529                     break;
10530                 case 0x10: /* sel */
10531                     if (!arm_dc_feature(s, ARM_FEATURE_THUMB_DSP)) {
10532                         goto illegal_op;
10533                     }
10534                     break;
10535                 case 0x20: /* crc32/crc32c */
10536                 case 0x21:
10537                 case 0x22:
10538                 case 0x28:
10539                 case 0x29:
10540                 case 0x2a:
10541                     if (!arm_dc_feature(s, ARM_FEATURE_CRC)) {
10542                         goto illegal_op;
10543                     }
10544                     break;
10545                 default:
10546                     goto illegal_op;
10547                 }
10548                 tmp = load_reg(s, rn);
10549                 switch (op) {
10550                 case 0x0a: /* rbit */
10551                     gen_helper_rbit(tmp, tmp);
10552                     break;
10553                 case 0x08: /* rev */
10554                     tcg_gen_bswap32_i32(tmp, tmp);
10555                     break;
10556                 case 0x09: /* rev16 */
10557                     gen_rev16(tmp);
10558                     break;
10559                 case 0x0b: /* revsh */
10560                     gen_revsh(tmp);
10561                     break;
10562                 case 0x10: /* sel */
10563                     tmp2 = load_reg(s, rm);
10564                     tmp3 = tcg_temp_new_i32();
10565                     tcg_gen_ld_i32(tmp3, cpu_env, offsetof(CPUARMState, GE));
10566                     gen_helper_sel_flags(tmp, tmp3, tmp, tmp2);
10567                     tcg_temp_free_i32(tmp3);
10568                     tcg_temp_free_i32(tmp2);
10569                     break;
10570                 case 0x18: /* clz */
10571                     tcg_gen_clzi_i32(tmp, tmp, 32);
10572                     break;
10573                 case 0x20:
10574                 case 0x21:
10575                 case 0x22:
10576                 case 0x28:
10577                 case 0x29:
10578                 case 0x2a:
10579                 {
10580                     /* crc32/crc32c */
10581                     uint32_t sz = op & 0x3;
10582                     uint32_t c = op & 0x8;
10583
10584                     tmp2 = load_reg(s, rm);
10585                     if (sz == 0) {
10586                         tcg_gen_andi_i32(tmp2, tmp2, 0xff);
10587                     } else if (sz == 1) {
10588                         tcg_gen_andi_i32(tmp2, tmp2, 0xffff);
10589                     }
10590                     tmp3 = tcg_const_i32(1 << sz);
10591                     if (c) {
10592                         gen_helper_crc32c(tmp, tmp, tmp2, tmp3);
10593                     } else {
10594                         gen_helper_crc32(tmp, tmp, tmp2, tmp3);
10595                     }
10596                     tcg_temp_free_i32(tmp2);
10597                     tcg_temp_free_i32(tmp3);
10598                     break;
10599                 }
10600                 default:
10601                     g_assert_not_reached();
10602                 }
10603             }
10604             store_reg(s, rd, tmp);
10605             break;
10606         case 4: case 5: /* 32-bit multiply.  Sum of absolute differences.  */
10607             switch ((insn >> 20) & 7) {
10608             case 0: /* 32 x 32 -> 32 */
10609             case 7: /* Unsigned sum of absolute differences.  */
10610                 break;
10611             case 1: /* 16 x 16 -> 32 */
10612             case 2: /* Dual multiply add.  */
10613             case 3: /* 32 * 16 -> 32msb */
10614             case 4: /* Dual multiply subtract.  */
10615             case 5: case 6: /* 32 * 32 -> 32msb (SMMUL, SMMLA, SMMLS) */
10616                 if (!arm_dc_feature(s, ARM_FEATURE_THUMB_DSP)) {
10617                     goto illegal_op;
10618                 }
10619                 break;
10620             }
10621             op = (insn >> 4) & 0xf;
10622             tmp = load_reg(s, rn);
10623             tmp2 = load_reg(s, rm);
10624             switch ((insn >> 20) & 7) {
10625             case 0: /* 32 x 32 -> 32 */
10626                 tcg_gen_mul_i32(tmp, tmp, tmp2);
10627                 tcg_temp_free_i32(tmp2);
10628                 if (rs != 15) {
10629                     tmp2 = load_reg(s, rs);
10630                     if (op)
10631                         tcg_gen_sub_i32(tmp, tmp2, tmp);
10632                     else
10633                         tcg_gen_add_i32(tmp, tmp, tmp2);
10634                     tcg_temp_free_i32(tmp2);
10635                 }
10636                 break;
10637             case 1: /* 16 x 16 -> 32 */
10638                 gen_mulxy(tmp, tmp2, op & 2, op & 1);
10639                 tcg_temp_free_i32(tmp2);
10640                 if (rs != 15) {
10641                     tmp2 = load_reg(s, rs);
10642                     gen_helper_add_setq(tmp, cpu_env, tmp, tmp2);
10643                     tcg_temp_free_i32(tmp2);
10644                 }
10645                 break;
10646             case 2: /* Dual multiply add.  */
10647             case 4: /* Dual multiply subtract.  */
10648                 if (op)
10649                     gen_swap_half(tmp2);
10650                 gen_smul_dual(tmp, tmp2);
10651                 if (insn & (1 << 22)) {
10652                     /* This subtraction cannot overflow. */
10653                     tcg_gen_sub_i32(tmp, tmp, tmp2);
10654                 } else {
10655                     /* This addition cannot overflow 32 bits;
10656                      * however it may overflow considered as a signed
10657                      * operation, in which case we must set the Q flag.
10658                      */
10659                     gen_helper_add_setq(tmp, cpu_env, tmp, tmp2);
10660                 }
10661                 tcg_temp_free_i32(tmp2);
10662                 if (rs != 15)
10663                   {
10664                     tmp2 = load_reg(s, rs);
10665                     gen_helper_add_setq(tmp, cpu_env, tmp, tmp2);
10666                     tcg_temp_free_i32(tmp2);
10667                   }
10668                 break;
10669             case 3: /* 32 * 16 -> 32msb */
10670                 if (op)
10671                     tcg_gen_sari_i32(tmp2, tmp2, 16);
10672                 else
10673                     gen_sxth(tmp2);
10674                 tmp64 = gen_muls_i64_i32(tmp, tmp2);
10675                 tcg_gen_shri_i64(tmp64, tmp64, 16);
10676                 tmp = tcg_temp_new_i32();
10677                 tcg_gen_extrl_i64_i32(tmp, tmp64);
10678                 tcg_temp_free_i64(tmp64);
10679                 if (rs != 15)
10680                   {
10681                     tmp2 = load_reg(s, rs);
10682                     gen_helper_add_setq(tmp, cpu_env, tmp, tmp2);
10683                     tcg_temp_free_i32(tmp2);
10684                   }
10685                 break;
10686             case 5: case 6: /* 32 * 32 -> 32msb (SMMUL, SMMLA, SMMLS) */
10687                 tmp64 = gen_muls_i64_i32(tmp, tmp2);
10688                 if (rs != 15) {
10689                     tmp = load_reg(s, rs);
10690                     if (insn & (1 << 20)) {
10691                         tmp64 = gen_addq_msw(tmp64, tmp);
10692                     } else {
10693                         tmp64 = gen_subq_msw(tmp64, tmp);
10694                     }
10695                 }
10696                 if (insn & (1 << 4)) {
10697                     tcg_gen_addi_i64(tmp64, tmp64, 0x80000000u);
10698                 }
10699                 tcg_gen_shri_i64(tmp64, tmp64, 32);
10700                 tmp = tcg_temp_new_i32();
10701                 tcg_gen_extrl_i64_i32(tmp, tmp64);
10702                 tcg_temp_free_i64(tmp64);
10703                 break;
10704             case 7: /* Unsigned sum of absolute differences.  */
10705                 gen_helper_usad8(tmp, tmp, tmp2);
10706                 tcg_temp_free_i32(tmp2);
10707                 if (rs != 15) {
10708                     tmp2 = load_reg(s, rs);
10709                     tcg_gen_add_i32(tmp, tmp, tmp2);
10710                     tcg_temp_free_i32(tmp2);
10711                 }
10712                 break;
10713             }
10714             store_reg(s, rd, tmp);
10715             break;
10716         case 6: case 7: /* 64-bit multiply, Divide.  */
10717             op = ((insn >> 4) & 0xf) | ((insn >> 16) & 0x70);
10718             tmp = load_reg(s, rn);
10719             tmp2 = load_reg(s, rm);
10720             if ((op & 0x50) == 0x10) {
10721                 /* sdiv, udiv */
10722                 if (!arm_dc_feature(s, ARM_FEATURE_THUMB_DIV)) {
10723                     goto illegal_op;
10724                 }
10725                 if (op & 0x20)
10726                     gen_helper_udiv(tmp, tmp, tmp2);
10727                 else
10728                     gen_helper_sdiv(tmp, tmp, tmp2);
10729                 tcg_temp_free_i32(tmp2);
10730                 store_reg(s, rd, tmp);
10731             } else if ((op & 0xe) == 0xc) {
10732                 /* Dual multiply accumulate long.  */
10733                 if (!arm_dc_feature(s, ARM_FEATURE_THUMB_DSP)) {
10734                     tcg_temp_free_i32(tmp);
10735                     tcg_temp_free_i32(tmp2);
10736                     goto illegal_op;
10737                 }
10738                 if (op & 1)
10739                     gen_swap_half(tmp2);
10740                 gen_smul_dual(tmp, tmp2);
10741                 if (op & 0x10) {
10742                     tcg_gen_sub_i32(tmp, tmp, tmp2);
10743                 } else {
10744                     tcg_gen_add_i32(tmp, tmp, tmp2);
10745                 }
10746                 tcg_temp_free_i32(tmp2);
10747                 /* BUGFIX */
10748                 tmp64 = tcg_temp_new_i64();
10749                 tcg_gen_ext_i32_i64(tmp64, tmp);
10750                 tcg_temp_free_i32(tmp);
10751                 gen_addq(s, tmp64, rs, rd);
10752                 gen_storeq_reg(s, rs, rd, tmp64);
10753                 tcg_temp_free_i64(tmp64);
10754             } else {
10755                 if (op & 0x20) {
10756                     /* Unsigned 64-bit multiply  */
10757                     tmp64 = gen_mulu_i64_i32(tmp, tmp2);
10758                 } else {
10759                     if (op & 8) {
10760                         /* smlalxy */
10761                         if (!arm_dc_feature(s, ARM_FEATURE_THUMB_DSP)) {
10762                             tcg_temp_free_i32(tmp2);
10763                             tcg_temp_free_i32(tmp);
10764                             goto illegal_op;
10765                         }
10766                         gen_mulxy(tmp, tmp2, op & 2, op & 1);
10767                         tcg_temp_free_i32(tmp2);
10768                         tmp64 = tcg_temp_new_i64();
10769                         tcg_gen_ext_i32_i64(tmp64, tmp);
10770                         tcg_temp_free_i32(tmp);
10771                     } else {
10772                         /* Signed 64-bit multiply  */
10773                         tmp64 = gen_muls_i64_i32(tmp, tmp2);
10774                     }
10775                 }
10776                 if (op & 4) {
10777                     /* umaal */
10778                     if (!arm_dc_feature(s, ARM_FEATURE_THUMB_DSP)) {
10779                         tcg_temp_free_i64(tmp64);
10780                         goto illegal_op;
10781                     }
10782                     gen_addq_lo(s, tmp64, rs);
10783                     gen_addq_lo(s, tmp64, rd);
10784                 } else if (op & 0x40) {
10785                     /* 64-bit accumulate.  */
10786                     gen_addq(s, tmp64, rs, rd);
10787                 }
10788                 gen_storeq_reg(s, rs, rd, tmp64);
10789                 tcg_temp_free_i64(tmp64);
10790             }
10791             break;
10792         }
10793         break;
10794     case 6: case 7: case 14: case 15:
10795         /* Coprocessor.  */
10796         if (arm_dc_feature(s, ARM_FEATURE_M)) {
10797             /* We don't currently implement M profile FP support,
10798              * so this entire space should give a NOCP fault, with
10799              * the exception of the v8M VLLDM and VLSTM insns, which
10800              * must be NOPs in Secure state and UNDEF in Nonsecure state.
10801              */
10802             if (arm_dc_feature(s, ARM_FEATURE_V8) &&
10803                 (insn & 0xffa00f00) == 0xec200a00) {
10804                 /* 0b1110_1100_0x1x_xxxx_xxxx_1010_xxxx_xxxx
10805                  *  - VLLDM, VLSTM
10806                  * We choose to UNDEF if the RAZ bits are non-zero.
10807                  */
10808                 if (!s->v8m_secure || (insn & 0x0040f0ff)) {
10809                     goto illegal_op;
10810                 }
10811                 /* Just NOP since FP support is not implemented */
10812                 break;
10813             }
10814             /* All other insns: NOCP */
10815             gen_exception_insn(s, 4, EXCP_NOCP, syn_uncategorized(),
10816                                default_exception_el(s));
10817             break;
10818         }
10819         if ((insn & 0xfe000a00) == 0xfc000800
10820             && arm_dc_feature(s, ARM_FEATURE_V8)) {
10821             /* The Thumb2 and ARM encodings are identical.  */
10822             if (disas_neon_insn_3same_ext(s, insn)) {
10823                 goto illegal_op;
10824             }
10825         } else if ((insn & 0xff000a00) == 0xfe000800
10826                    && arm_dc_feature(s, ARM_FEATURE_V8)) {
10827             /* The Thumb2 and ARM encodings are identical.  */
10828             if (disas_neon_insn_2reg_scalar_ext(s, insn)) {
10829                 goto illegal_op;
10830             }
10831         } else if (((insn >> 24) & 3) == 3) {
10832             /* Translate into the equivalent ARM encoding.  */
10833             insn = (insn & 0xe2ffffff) | ((insn & (1 << 28)) >> 4) | (1 << 28);
10834             if (disas_neon_data_insn(s, insn)) {
10835                 goto illegal_op;
10836             }
10837         } else if (((insn >> 8) & 0xe) == 10) {
10838             if (disas_vfp_insn(s, insn)) {
10839                 goto illegal_op;
10840             }
10841         } else {
10842             if (insn & (1 << 28))
10843                 goto illegal_op;
10844             if (disas_coproc_insn(s, insn)) {
10845                 goto illegal_op;
10846             }
10847         }
10848         break;
10849     case 8: case 9: case 10: case 11:
10850         if (insn & (1 << 15)) {
10851             /* Branches, misc control.  */
10852             if (insn & 0x5000) {
10853                 /* Unconditional branch.  */
10854                 /* signextend(hw1[10:0]) -> offset[:12].  */
10855                 offset = ((int32_t)insn << 5) >> 9 & ~(int32_t)0xfff;
10856                 /* hw1[10:0] -> offset[11:1].  */
10857                 offset |= (insn & 0x7ff) << 1;
10858                 /* (~hw2[13, 11] ^ offset[24]) -> offset[23,22]
10859                    offset[24:22] already have the same value because of the
10860                    sign extension above.  */
10861                 offset ^= ((~insn) & (1 << 13)) << 10;
10862                 offset ^= ((~insn) & (1 << 11)) << 11;
10863
10864                 if (insn & (1 << 14)) {
10865                     /* Branch and link.  */
10866                     tcg_gen_movi_i32(cpu_R[14], s->pc | 1);
10867                 }
10868
10869                 offset += s->pc;
10870                 if (insn & (1 << 12)) {
10871                     /* b/bl */
10872                     gen_jmp(s, offset);
10873                 } else {
10874                     /* blx */
10875                     offset &= ~(uint32_t)2;
10876                     /* thumb2 bx, no need to check */
10877                     gen_bx_im(s, offset);
10878                 }
10879             } else if (((insn >> 23) & 7) == 7) {
10880                 /* Misc control */
10881                 if (insn & (1 << 13))
10882                     goto illegal_op;
10883
10884                 if (insn & (1 << 26)) {
10885                     if (arm_dc_feature(s, ARM_FEATURE_M)) {
10886                         goto illegal_op;
10887                     }
10888                     if (!(insn & (1 << 20))) {
10889                         /* Hypervisor call (v7) */
10890                         int imm16 = extract32(insn, 16, 4) << 12
10891                             | extract32(insn, 0, 12);
10892                         ARCH(7);
10893                         if (IS_USER(s)) {
10894                             goto illegal_op;
10895                         }
10896                         gen_hvc(s, imm16);
10897                     } else {
10898                         /* Secure monitor call (v6+) */
10899                         ARCH(6K);
10900                         if (IS_USER(s)) {
10901                             goto illegal_op;
10902                         }
10903                         gen_smc(s);
10904                     }
10905                 } else {
10906                     op = (insn >> 20) & 7;
10907                     switch (op) {
10908                     case 0: /* msr cpsr.  */
10909                         if (arm_dc_feature(s, ARM_FEATURE_M)) {
10910                             tmp = load_reg(s, rn);
10911                             /* the constant is the mask and SYSm fields */
10912                             addr = tcg_const_i32(insn & 0xfff);
10913                             gen_helper_v7m_msr(cpu_env, addr, tmp);
10914                             tcg_temp_free_i32(addr);
10915                             tcg_temp_free_i32(tmp);
10916                             gen_lookup_tb(s);
10917                             break;
10918                         }
10919                         /* fall through */
10920                     case 1: /* msr spsr.  */
10921                         if (arm_dc_feature(s, ARM_FEATURE_M)) {
10922                             goto illegal_op;
10923                         }
10924
10925                         if (extract32(insn, 5, 1)) {
10926                             /* MSR (banked) */
10927                             int sysm = extract32(insn, 8, 4) |
10928                                 (extract32(insn, 4, 1) << 4);
10929                             int r = op & 1;
10930
10931                             gen_msr_banked(s, r, sysm, rm);
10932                             break;
10933                         }
10934
10935                         /* MSR (for PSRs) */
10936                         tmp = load_reg(s, rn);
10937                         if (gen_set_psr(s,
10938                               msr_mask(s, (insn >> 8) & 0xf, op == 1),
10939                               op == 1, tmp))
10940                             goto illegal_op;
10941                         break;
10942                     case 2: /* cps, nop-hint.  */
10943                         if (((insn >> 8) & 7) == 0) {
10944                             gen_nop_hint(s, insn & 0xff);
10945                         }
10946                         /* Implemented as NOP in user mode.  */
10947                         if (IS_USER(s))
10948                             break;
10949                         offset = 0;
10950                         imm = 0;
10951                         if (insn & (1 << 10)) {
10952                             if (insn & (1 << 7))
10953                                 offset |= CPSR_A;
10954                             if (insn & (1 << 6))
10955                                 offset |= CPSR_I;
10956                             if (insn & (1 << 5))
10957                                 offset |= CPSR_F;
10958                             if (insn & (1 << 9))
10959                                 imm = CPSR_A | CPSR_I | CPSR_F;
10960                         }
10961                         if (insn & (1 << 8)) {
10962                             offset |= 0x1f;
10963                             imm |= (insn & 0x1f);
10964                         }
10965                         if (offset) {
10966                             gen_set_psr_im(s, offset, 0, imm);
10967                         }
10968                         break;
10969                     case 3: /* Special control operations.  */
10970                         ARCH(7);
10971                         op = (insn >> 4) & 0xf;
10972                         switch (op) {
10973                         case 2: /* clrex */
10974                             gen_clrex(s);
10975                             break;
10976                         case 4: /* dsb */
10977                         case 5: /* dmb */
10978                             tcg_gen_mb(TCG_MO_ALL | TCG_BAR_SC);
10979                             break;
10980                         case 6: /* isb */
10981                             /* We need to break the TB after this insn
10982                              * to execute self-modifying code correctly
10983                              * and also to take any pending interrupts
10984                              * immediately.
10985                              */
10986                             gen_goto_tb(s, 0, s->pc & ~1);
10987                             break;
10988                         default:
10989                             goto illegal_op;
10990                         }
10991                         break;
10992                     case 4: /* bxj */
10993                         /* Trivial implementation equivalent to bx.
10994                          * This instruction doesn't exist at all for M-profile.
10995                          */
10996                         if (arm_dc_feature(s, ARM_FEATURE_M)) {
10997                             goto illegal_op;
10998                         }
10999                         tmp = load_reg(s, rn);
11000                         gen_bx(s, tmp);
11001                         break;
11002                     case 5: /* Exception return.  */
11003                         if (IS_USER(s)) {
11004                             goto illegal_op;
11005                         }
11006                         if (rn != 14 || rd != 15) {
11007                             goto illegal_op;
11008                         }
11009                         tmp = load_reg(s, rn);
11010                         tcg_gen_subi_i32(tmp, tmp, insn & 0xff);
11011                         gen_exception_return(s, tmp);
11012                         break;
11013                     case 6: /* MRS */
11014                         if (extract32(insn, 5, 1) &&
11015                             !arm_dc_feature(s, ARM_FEATURE_M)) {
11016                             /* MRS (banked) */
11017                             int sysm = extract32(insn, 16, 4) |
11018                                 (extract32(insn, 4, 1) << 4);
11019
11020                             gen_mrs_banked(s, 0, sysm, rd);
11021                             break;
11022                         }
11023
11024                         if (extract32(insn, 16, 4) != 0xf) {
11025                             goto illegal_op;
11026                         }
11027                         if (!arm_dc_feature(s, ARM_FEATURE_M) &&
11028                             extract32(insn, 0, 8) != 0) {
11029                             goto illegal_op;
11030                         }
11031
11032                         /* mrs cpsr */
11033                         tmp = tcg_temp_new_i32();
11034                         if (arm_dc_feature(s, ARM_FEATURE_M)) {
11035                             addr = tcg_const_i32(insn & 0xff);
11036                             gen_helper_v7m_mrs(tmp, cpu_env, addr);
11037                             tcg_temp_free_i32(addr);
11038                         } else {
11039                             gen_helper_cpsr_read(tmp, cpu_env);
11040                         }
11041                         store_reg(s, rd, tmp);
11042                         break;
11043                     case 7: /* MRS */
11044                         if (extract32(insn, 5, 1) &&
11045                             !arm_dc_feature(s, ARM_FEATURE_M)) {
11046                             /* MRS (banked) */
11047                             int sysm = extract32(insn, 16, 4) |
11048                                 (extract32(insn, 4, 1) << 4);
11049
11050                             gen_mrs_banked(s, 1, sysm, rd);
11051                             break;
11052                         }
11053
11054                         /* mrs spsr.  */
11055                         /* Not accessible in user mode.  */
11056                         if (IS_USER(s) || arm_dc_feature(s, ARM_FEATURE_M)) {
11057                             goto illegal_op;
11058                         }
11059
11060                         if (extract32(insn, 16, 4) != 0xf ||
11061                             extract32(insn, 0, 8) != 0) {
11062                             goto illegal_op;
11063                         }
11064
11065                         tmp = load_cpu_field(spsr);
11066                         store_reg(s, rd, tmp);
11067                         break;
11068                     }
11069                 }
11070             } else {
11071                 /* Conditional branch.  */
11072                 op = (insn >> 22) & 0xf;
11073                 /* Generate a conditional jump to next instruction.  */
11074                 s->condlabel = gen_new_label();
11075                 arm_gen_test_cc(op ^ 1, s->condlabel);
11076                 s->condjmp = 1;
11077
11078                 /* offset[11:1] = insn[10:0] */
11079                 offset = (insn & 0x7ff) << 1;
11080                 /* offset[17:12] = insn[21:16].  */
11081                 offset |= (insn & 0x003f0000) >> 4;
11082                 /* offset[31:20] = insn[26].  */
11083                 offset |= ((int32_t)((insn << 5) & 0x80000000)) >> 11;
11084                 /* offset[18] = insn[13].  */
11085                 offset |= (insn & (1 << 13)) << 5;
11086                 /* offset[19] = insn[11].  */
11087                 offset |= (insn & (1 << 11)) << 8;
11088
11089                 /* jump to the offset */
11090                 gen_jmp(s, s->pc + offset);
11091             }
11092         } else {
11093             /* Data processing immediate.  */
11094             if (insn & (1 << 25)) {
11095                 if (insn & (1 << 24)) {
11096                     if (insn & (1 << 20))
11097                         goto illegal_op;
11098                     /* Bitfield/Saturate.  */
11099                     op = (insn >> 21) & 7;
11100                     imm = insn & 0x1f;
11101                     shift = ((insn >> 6) & 3) | ((insn >> 10) & 0x1c);
11102                     if (rn == 15) {
11103                         tmp = tcg_temp_new_i32();
11104                         tcg_gen_movi_i32(tmp, 0);
11105                     } else {
11106                         tmp = load_reg(s, rn);
11107                     }
11108                     switch (op) {
11109                     case 2: /* Signed bitfield extract.  */
11110                         imm++;
11111                         if (shift + imm > 32)
11112                             goto illegal_op;
11113                         if (imm < 32) {
11114                             tcg_gen_sextract_i32(tmp, tmp, shift, imm);
11115                         }
11116                         break;
11117                     case 6: /* Unsigned bitfield extract.  */
11118                         imm++;
11119                         if (shift + imm > 32)
11120                             goto illegal_op;
11121                         if (imm < 32) {
11122                             tcg_gen_extract_i32(tmp, tmp, shift, imm);
11123                         }
11124                         break;
11125                     case 3: /* Bitfield insert/clear.  */
11126                         if (imm < shift)
11127                             goto illegal_op;
11128                         imm = imm + 1 - shift;
11129                         if (imm != 32) {
11130                             tmp2 = load_reg(s, rd);
11131                             tcg_gen_deposit_i32(tmp, tmp2, tmp, shift, imm);
11132                             tcg_temp_free_i32(tmp2);
11133                         }
11134                         break;
11135                     case 7:
11136                         goto illegal_op;
11137                     default: /* Saturate.  */
11138                         if (shift) {
11139                             if (op & 1)
11140                                 tcg_gen_sari_i32(tmp, tmp, shift);
11141                             else
11142                                 tcg_gen_shli_i32(tmp, tmp, shift);
11143                         }
11144                         tmp2 = tcg_const_i32(imm);
11145                         if (op & 4) {
11146                             /* Unsigned.  */
11147                             if ((op & 1) && shift == 0) {
11148                                 if (!arm_dc_feature(s, ARM_FEATURE_THUMB_DSP)) {
11149                                     tcg_temp_free_i32(tmp);
11150                                     tcg_temp_free_i32(tmp2);
11151                                     goto illegal_op;
11152                                 }
11153                                 gen_helper_usat16(tmp, cpu_env, tmp, tmp2);
11154                             } else {
11155                                 gen_helper_usat(tmp, cpu_env, tmp, tmp2);
11156                             }
11157                         } else {
11158                             /* Signed.  */
11159                             if ((op & 1) && shift == 0) {
11160                                 if (!arm_dc_feature(s, ARM_FEATURE_THUMB_DSP)) {
11161                                     tcg_temp_free_i32(tmp);
11162                                     tcg_temp_free_i32(tmp2);
11163                                     goto illegal_op;
11164                                 }
11165                                 gen_helper_ssat16(tmp, cpu_env, tmp, tmp2);
11166                             } else {
11167                                 gen_helper_ssat(tmp, cpu_env, tmp, tmp2);
11168                             }
11169                         }
11170                         tcg_temp_free_i32(tmp2);
11171                         break;
11172                     }
11173                     store_reg(s, rd, tmp);
11174                 } else {
11175                     imm = ((insn & 0x04000000) >> 15)
11176                           | ((insn & 0x7000) >> 4) | (insn & 0xff);
11177                     if (insn & (1 << 22)) {
11178                         /* 16-bit immediate.  */
11179                         imm |= (insn >> 4) & 0xf000;
11180                         if (insn & (1 << 23)) {
11181                             /* movt */
11182                             tmp = load_reg(s, rd);
11183                             tcg_gen_ext16u_i32(tmp, tmp);
11184                             tcg_gen_ori_i32(tmp, tmp, imm << 16);
11185                         } else {
11186                             /* movw */
11187                             tmp = tcg_temp_new_i32();
11188                             tcg_gen_movi_i32(tmp, imm);
11189                         }
11190                     } else {
11191                         /* Add/sub 12-bit immediate.  */
11192                         if (rn == 15) {
11193                             offset = s->pc & ~(uint32_t)3;
11194                             if (insn & (1 << 23))
11195                                 offset -= imm;
11196                             else
11197                                 offset += imm;
11198                             tmp = tcg_temp_new_i32();
11199                             tcg_gen_movi_i32(tmp, offset);
11200                         } else {
11201                             tmp = load_reg(s, rn);
11202                             if (insn & (1 << 23))
11203                                 tcg_gen_subi_i32(tmp, tmp, imm);
11204                             else
11205                                 tcg_gen_addi_i32(tmp, tmp, imm);
11206                         }
11207                     }
11208                     store_reg(s, rd, tmp);
11209                 }
11210             } else {
11211                 int shifter_out = 0;
11212                 /* modified 12-bit immediate.  */
11213                 shift = ((insn & 0x04000000) >> 23) | ((insn & 0x7000) >> 12);
11214                 imm = (insn & 0xff);
11215                 switch (shift) {
11216                 case 0: /* XY */
11217                     /* Nothing to do.  */
11218                     break;
11219                 case 1: /* 00XY00XY */
11220                     imm |= imm << 16;
11221                     break;
11222                 case 2: /* XY00XY00 */
11223                     imm |= imm << 16;
11224                     imm <<= 8;
11225                     break;
11226                 case 3: /* XYXYXYXY */
11227                     imm |= imm << 16;
11228                     imm |= imm << 8;
11229                     break;
11230                 default: /* Rotated constant.  */
11231                     shift = (shift << 1) | (imm >> 7);
11232                     imm |= 0x80;
11233                     imm = imm << (32 - shift);
11234                     shifter_out = 1;
11235                     break;
11236                 }
11237                 tmp2 = tcg_temp_new_i32();
11238                 tcg_gen_movi_i32(tmp2, imm);
11239                 rn = (insn >> 16) & 0xf;
11240                 if (rn == 15) {
11241                     tmp = tcg_temp_new_i32();
11242                     tcg_gen_movi_i32(tmp, 0);
11243                 } else {
11244                     tmp = load_reg(s, rn);
11245                 }
11246                 op = (insn >> 21) & 0xf;
11247                 if (gen_thumb2_data_op(s, op, (insn & (1 << 20)) != 0,
11248                                        shifter_out, tmp, tmp2))
11249                     goto illegal_op;
11250                 tcg_temp_free_i32(tmp2);
11251                 rd = (insn >> 8) & 0xf;
11252                 if (rd != 15) {
11253                     store_reg(s, rd, tmp);
11254                 } else {
11255                     tcg_temp_free_i32(tmp);
11256                 }
11257             }
11258         }
11259         break;
11260     case 12: /* Load/store single data item.  */
11261         {
11262         int postinc = 0;
11263         int writeback = 0;
11264         int memidx;
11265         ISSInfo issinfo;
11266
11267         if ((insn & 0x01100000) == 0x01000000) {
11268             if (disas_neon_ls_insn(s, insn)) {
11269                 goto illegal_op;
11270             }
11271             break;
11272         }
11273         op = ((insn >> 21) & 3) | ((insn >> 22) & 4);
11274         if (rs == 15) {
11275             if (!(insn & (1 << 20))) {
11276                 goto illegal_op;
11277             }
11278             if (op != 2) {
11279                 /* Byte or halfword load space with dest == r15 : memory hints.
11280                  * Catch them early so we don't emit pointless addressing code.
11281                  * This space is a mix of:
11282                  *  PLD/PLDW/PLI,  which we implement as NOPs (note that unlike
11283                  *     the ARM encodings, PLDW space doesn't UNDEF for non-v7MP
11284                  *     cores)
11285                  *  unallocated hints, which must be treated as NOPs
11286                  *  UNPREDICTABLE space, which we NOP or UNDEF depending on
11287                  *     which is easiest for the decoding logic
11288                  *  Some space which must UNDEF
11289                  */
11290                 int op1 = (insn >> 23) & 3;
11291                 int op2 = (insn >> 6) & 0x3f;
11292                 if (op & 2) {
11293                     goto illegal_op;
11294                 }
11295                 if (rn == 15) {
11296                     /* UNPREDICTABLE, unallocated hint or
11297                      * PLD/PLDW/PLI (literal)
11298                      */
11299                     return;
11300                 }
11301                 if (op1 & 1) {
11302                     return; /* PLD/PLDW/PLI or unallocated hint */
11303                 }
11304                 if ((op2 == 0) || ((op2 & 0x3c) == 0x30)) {
11305                     return; /* PLD/PLDW/PLI or unallocated hint */
11306                 }
11307                 /* UNDEF space, or an UNPREDICTABLE */
11308                 goto illegal_op;
11309             }
11310         }
11311         memidx = get_mem_index(s);
11312         if (rn == 15) {
11313             addr = tcg_temp_new_i32();
11314             /* PC relative.  */
11315             /* s->pc has already been incremented by 4.  */
11316             imm = s->pc & 0xfffffffc;
11317             if (insn & (1 << 23))
11318                 imm += insn & 0xfff;
11319             else
11320                 imm -= insn & 0xfff;
11321             tcg_gen_movi_i32(addr, imm);
11322         } else {
11323             addr = load_reg(s, rn);
11324             if (insn & (1 << 23)) {
11325                 /* Positive offset.  */
11326                 imm = insn & 0xfff;
11327                 tcg_gen_addi_i32(addr, addr, imm);
11328             } else {
11329                 imm = insn & 0xff;
11330                 switch ((insn >> 8) & 0xf) {
11331                 case 0x0: /* Shifted Register.  */
11332                     shift = (insn >> 4) & 0xf;
11333                     if (shift > 3) {
11334                         tcg_temp_free_i32(addr);
11335                         goto illegal_op;
11336                     }
11337                     tmp = load_reg(s, rm);
11338                     if (shift)
11339                         tcg_gen_shli_i32(tmp, tmp, shift);
11340                     tcg_gen_add_i32(addr, addr, tmp);
11341                     tcg_temp_free_i32(tmp);
11342                     break;
11343                 case 0xc: /* Negative offset.  */
11344                     tcg_gen_addi_i32(addr, addr, -imm);
11345                     break;
11346                 case 0xe: /* User privilege.  */
11347                     tcg_gen_addi_i32(addr, addr, imm);
11348                     memidx = get_a32_user_mem_index(s);
11349                     break;
11350                 case 0x9: /* Post-decrement.  */
11351                     imm = -imm;
11352                     /* Fall through.  */
11353                 case 0xb: /* Post-increment.  */
11354                     postinc = 1;
11355                     writeback = 1;
11356                     break;
11357                 case 0xd: /* Pre-decrement.  */
11358                     imm = -imm;
11359                     /* Fall through.  */
11360                 case 0xf: /* Pre-increment.  */
11361                     tcg_gen_addi_i32(addr, addr, imm);
11362                     writeback = 1;
11363                     break;
11364                 default:
11365                     tcg_temp_free_i32(addr);
11366                     goto illegal_op;
11367                 }
11368             }
11369         }
11370
11371         issinfo = writeback ? ISSInvalid : rs;
11372
11373         if (insn & (1 << 20)) {
11374             /* Load.  */
11375             tmp = tcg_temp_new_i32();
11376             switch (op) {
11377             case 0:
11378                 gen_aa32_ld8u_iss(s, tmp, addr, memidx, issinfo);
11379                 break;
11380             case 4:
11381                 gen_aa32_ld8s_iss(s, tmp, addr, memidx, issinfo);
11382                 break;
11383             case 1:
11384                 gen_aa32_ld16u_iss(s, tmp, addr, memidx, issinfo);
11385                 break;
11386             case 5:
11387                 gen_aa32_ld16s_iss(s, tmp, addr, memidx, issinfo);
11388                 break;
11389             case 2:
11390                 gen_aa32_ld32u_iss(s, tmp, addr, memidx, issinfo);
11391                 break;
11392             default:
11393                 tcg_temp_free_i32(tmp);
11394                 tcg_temp_free_i32(addr);
11395                 goto illegal_op;
11396             }
11397             if (rs == 15) {
11398                 gen_bx_excret(s, tmp);
11399             } else {
11400                 store_reg(s, rs, tmp);
11401             }
11402         } else {
11403             /* Store.  */
11404             tmp = load_reg(s, rs);
11405             switch (op) {
11406             case 0:
11407                 gen_aa32_st8_iss(s, tmp, addr, memidx, issinfo);
11408                 break;
11409             case 1:
11410                 gen_aa32_st16_iss(s, tmp, addr, memidx, issinfo);
11411                 break;
11412             case 2:
11413                 gen_aa32_st32_iss(s, tmp, addr, memidx, issinfo);
11414                 break;
11415             default:
11416                 tcg_temp_free_i32(tmp);
11417                 tcg_temp_free_i32(addr);
11418                 goto illegal_op;
11419             }
11420             tcg_temp_free_i32(tmp);
11421         }
11422         if (postinc)
11423             tcg_gen_addi_i32(addr, addr, imm);
11424         if (writeback) {
11425             store_reg(s, rn, addr);
11426         } else {
11427             tcg_temp_free_i32(addr);
11428         }
11429         }
11430         break;
11431     default:
11432         goto illegal_op;
11433     }
11434     return;
11435 illegal_op:
11436     gen_exception_insn(s, 4, EXCP_UDEF, syn_uncategorized(),
11437                        default_exception_el(s));
11438 }
11439
11440 static void disas_thumb_insn(DisasContext *s, uint32_t insn)
11441 {
11442     uint32_t val, op, rm, rn, rd, shift, cond;
11443     int32_t offset;
11444     int i;
11445     TCGv_i32 tmp;
11446     TCGv_i32 tmp2;
11447     TCGv_i32 addr;
11448
11449     switch (insn >> 12) {
11450     case 0: case 1:
11451
11452         rd = insn & 7;
11453         op = (insn >> 11) & 3;
11454         if (op == 3) {
11455             /* add/subtract */
11456             rn = (insn >> 3) & 7;
11457             tmp = load_reg(s, rn);
11458             if (insn & (1 << 10)) {
11459                 /* immediate */
11460                 tmp2 = tcg_temp_new_i32();
11461                 tcg_gen_movi_i32(tmp2, (insn >> 6) & 7);
11462             } else {
11463                 /* reg */
11464                 rm = (insn >> 6) & 7;
11465                 tmp2 = load_reg(s, rm);
11466             }
11467             if (insn & (1 << 9)) {
11468                 if (s->condexec_mask)
11469                     tcg_gen_sub_i32(tmp, tmp, tmp2);
11470                 else
11471                     gen_sub_CC(tmp, tmp, tmp2);
11472             } else {
11473                 if (s->condexec_mask)
11474                     tcg_gen_add_i32(tmp, tmp, tmp2);
11475                 else
11476                     gen_add_CC(tmp, tmp, tmp2);
11477             }
11478             tcg_temp_free_i32(tmp2);
11479             store_reg(s, rd, tmp);
11480         } else {
11481             /* shift immediate */
11482             rm = (insn >> 3) & 7;
11483             shift = (insn >> 6) & 0x1f;
11484             tmp = load_reg(s, rm);
11485             gen_arm_shift_im(tmp, op, shift, s->condexec_mask == 0);
11486             if (!s->condexec_mask)
11487                 gen_logic_CC(tmp);
11488             store_reg(s, rd, tmp);
11489         }
11490         break;
11491     case 2: case 3:
11492         /* arithmetic large immediate */
11493         op = (insn >> 11) & 3;
11494         rd = (insn >> 8) & 0x7;
11495         if (op == 0) { /* mov */
11496             tmp = tcg_temp_new_i32();
11497             tcg_gen_movi_i32(tmp, insn & 0xff);
11498             if (!s->condexec_mask)
11499                 gen_logic_CC(tmp);
11500             store_reg(s, rd, tmp);
11501         } else {
11502             tmp = load_reg(s, rd);
11503             tmp2 = tcg_temp_new_i32();
11504             tcg_gen_movi_i32(tmp2, insn & 0xff);
11505             switch (op) {
11506             case 1: /* cmp */
11507                 gen_sub_CC(tmp, tmp, tmp2);
11508                 tcg_temp_free_i32(tmp);
11509                 tcg_temp_free_i32(tmp2);
11510                 break;
11511             case 2: /* add */
11512                 if (s->condexec_mask)
11513                     tcg_gen_add_i32(tmp, tmp, tmp2);
11514                 else
11515                     gen_add_CC(tmp, tmp, tmp2);
11516                 tcg_temp_free_i32(tmp2);
11517                 store_reg(s, rd, tmp);
11518                 break;
11519             case 3: /* sub */
11520                 if (s->condexec_mask)
11521                     tcg_gen_sub_i32(tmp, tmp, tmp2);
11522                 else
11523                     gen_sub_CC(tmp, tmp, tmp2);
11524                 tcg_temp_free_i32(tmp2);
11525                 store_reg(s, rd, tmp);
11526                 break;
11527             }
11528         }
11529         break;
11530     case 4:
11531         if (insn & (1 << 11)) {
11532             rd = (insn >> 8) & 7;
11533             /* load pc-relative.  Bit 1 of PC is ignored.  */
11534             val = s->pc + 2 + ((insn & 0xff) * 4);
11535             val &= ~(uint32_t)2;
11536             addr = tcg_temp_new_i32();
11537             tcg_gen_movi_i32(addr, val);
11538             tmp = tcg_temp_new_i32();
11539             gen_aa32_ld32u_iss(s, tmp, addr, get_mem_index(s),
11540                                rd | ISSIs16Bit);
11541             tcg_temp_free_i32(addr);
11542             store_reg(s, rd, tmp);
11543             break;
11544         }
11545         if (insn & (1 << 10)) {
11546             /* 0b0100_01xx_xxxx_xxxx
11547              * - data processing extended, branch and exchange
11548              */
11549             rd = (insn & 7) | ((insn >> 4) & 8);
11550             rm = (insn >> 3) & 0xf;
11551             op = (insn >> 8) & 3;
11552             switch (op) {
11553             case 0: /* add */
11554                 tmp = load_reg(s, rd);
11555                 tmp2 = load_reg(s, rm);
11556                 tcg_gen_add_i32(tmp, tmp, tmp2);
11557                 tcg_temp_free_i32(tmp2);
11558                 store_reg(s, rd, tmp);
11559                 break;
11560             case 1: /* cmp */
11561                 tmp = load_reg(s, rd);
11562                 tmp2 = load_reg(s, rm);
11563                 gen_sub_CC(tmp, tmp, tmp2);
11564                 tcg_temp_free_i32(tmp2);
11565                 tcg_temp_free_i32(tmp);
11566                 break;
11567             case 2: /* mov/cpy */
11568                 tmp = load_reg(s, rm);
11569                 store_reg(s, rd, tmp);
11570                 break;
11571             case 3:
11572             {
11573                 /* 0b0100_0111_xxxx_xxxx
11574                  * - branch [and link] exchange thumb register
11575                  */
11576                 bool link = insn & (1 << 7);
11577
11578                 if (insn & 3) {
11579                     goto undef;
11580                 }
11581                 if (link) {
11582                     ARCH(5);
11583                 }
11584                 if ((insn & 4)) {
11585                     /* BXNS/BLXNS: only exists for v8M with the
11586                      * security extensions, and always UNDEF if NonSecure.
11587                      * We don't implement these in the user-only mode
11588                      * either (in theory you can use them from Secure User
11589                      * mode but they are too tied in to system emulation.)
11590                      */
11591                     if (!s->v8m_secure || IS_USER_ONLY) {
11592                         goto undef;
11593                     }
11594                     if (link) {
11595                         gen_blxns(s, rm);
11596                     } else {
11597                         gen_bxns(s, rm);
11598                     }
11599                     break;
11600                 }
11601                 /* BLX/BX */
11602                 tmp = load_reg(s, rm);
11603                 if (link) {
11604                     val = (uint32_t)s->pc | 1;
11605                     tmp2 = tcg_temp_new_i32();
11606                     tcg_gen_movi_i32(tmp2, val);
11607                     store_reg(s, 14, tmp2);
11608                     gen_bx(s, tmp);
11609                 } else {
11610                     /* Only BX works as exception-return, not BLX */
11611                     gen_bx_excret(s, tmp);
11612                 }
11613                 break;
11614             }
11615             }
11616             break;
11617         }
11618
11619         /* data processing register */
11620         rd = insn & 7;
11621         rm = (insn >> 3) & 7;
11622         op = (insn >> 6) & 0xf;
11623         if (op == 2 || op == 3 || op == 4 || op == 7) {
11624             /* the shift/rotate ops want the operands backwards */
11625             val = rm;
11626             rm = rd;
11627             rd = val;
11628             val = 1;
11629         } else {
11630             val = 0;
11631         }
11632
11633         if (op == 9) { /* neg */
11634             tmp = tcg_temp_new_i32();
11635             tcg_gen_movi_i32(tmp, 0);
11636         } else if (op != 0xf) { /* mvn doesn't read its first operand */
11637             tmp = load_reg(s, rd);
11638         } else {
11639             tmp = NULL;
11640         }
11641
11642         tmp2 = load_reg(s, rm);
11643         switch (op) {
11644         case 0x0: /* and */
11645             tcg_gen_and_i32(tmp, tmp, tmp2);
11646             if (!s->condexec_mask)
11647                 gen_logic_CC(tmp);
11648             break;
11649         case 0x1: /* eor */
11650             tcg_gen_xor_i32(tmp, tmp, tmp2);
11651             if (!s->condexec_mask)
11652                 gen_logic_CC(tmp);
11653             break;
11654         case 0x2: /* lsl */
11655             if (s->condexec_mask) {
11656                 gen_shl(tmp2, tmp2, tmp);
11657             } else {
11658                 gen_helper_shl_cc(tmp2, cpu_env, tmp2, tmp);
11659                 gen_logic_CC(tmp2);
11660             }
11661             break;
11662         case 0x3: /* lsr */
11663             if (s->condexec_mask) {
11664                 gen_shr(tmp2, tmp2, tmp);
11665             } else {
11666                 gen_helper_shr_cc(tmp2, cpu_env, tmp2, tmp);
11667                 gen_logic_CC(tmp2);
11668             }
11669             break;
11670         case 0x4: /* asr */
11671             if (s->condexec_mask) {
11672                 gen_sar(tmp2, tmp2, tmp);
11673             } else {
11674                 gen_helper_sar_cc(tmp2, cpu_env, tmp2, tmp);
11675                 gen_logic_CC(tmp2);
11676             }
11677             break;
11678         case 0x5: /* adc */
11679             if (s->condexec_mask) {
11680                 gen_adc(tmp, tmp2);
11681             } else {
11682                 gen_adc_CC(tmp, tmp, tmp2);
11683             }
11684             break;
11685         case 0x6: /* sbc */
11686             if (s->condexec_mask) {
11687                 gen_sub_carry(tmp, tmp, tmp2);
11688             } else {
11689                 gen_sbc_CC(tmp, tmp, tmp2);
11690             }
11691             break;
11692         case 0x7: /* ror */
11693             if (s->condexec_mask) {
11694                 tcg_gen_andi_i32(tmp, tmp, 0x1f);
11695                 tcg_gen_rotr_i32(tmp2, tmp2, tmp);
11696             } else {
11697                 gen_helper_ror_cc(tmp2, cpu_env, tmp2, tmp);
11698                 gen_logic_CC(tmp2);
11699             }
11700             break;
11701         case 0x8: /* tst */
11702             tcg_gen_and_i32(tmp, tmp, tmp2);
11703             gen_logic_CC(tmp);
11704             rd = 16;
11705             break;
11706         case 0x9: /* neg */
11707             if (s->condexec_mask)
11708                 tcg_gen_neg_i32(tmp, tmp2);
11709             else
11710                 gen_sub_CC(tmp, tmp, tmp2);
11711             break;
11712         case 0xa: /* cmp */
11713             gen_sub_CC(tmp, tmp, tmp2);
11714             rd = 16;
11715             break;
11716         case 0xb: /* cmn */
11717             gen_add_CC(tmp, tmp, tmp2);
11718             rd = 16;
11719             break;
11720         case 0xc: /* orr */
11721             tcg_gen_or_i32(tmp, tmp, tmp2);
11722             if (!s->condexec_mask)
11723                 gen_logic_CC(tmp);
11724             break;
11725         case 0xd: /* mul */
11726             tcg_gen_mul_i32(tmp, tmp, tmp2);
11727             if (!s->condexec_mask)
11728                 gen_logic_CC(tmp);
11729             break;
11730         case 0xe: /* bic */
11731             tcg_gen_andc_i32(tmp, tmp, tmp2);
11732             if (!s->condexec_mask)
11733                 gen_logic_CC(tmp);
11734             break;
11735         case 0xf: /* mvn */
11736             tcg_gen_not_i32(tmp2, tmp2);
11737             if (!s->condexec_mask)
11738                 gen_logic_CC(tmp2);
11739             val = 1;
11740             rm = rd;
11741             break;
11742         }
11743         if (rd != 16) {
11744             if (val) {
11745                 store_reg(s, rm, tmp2);
11746                 if (op != 0xf)
11747                     tcg_temp_free_i32(tmp);
11748             } else {
11749                 store_reg(s, rd, tmp);
11750                 tcg_temp_free_i32(tmp2);
11751             }
11752         } else {
11753             tcg_temp_free_i32(tmp);
11754             tcg_temp_free_i32(tmp2);
11755         }
11756         break;
11757
11758     case 5:
11759         /* load/store register offset.  */
11760         rd = insn & 7;
11761         rn = (insn >> 3) & 7;
11762         rm = (insn >> 6) & 7;
11763         op = (insn >> 9) & 7;
11764         addr = load_reg(s, rn);
11765         tmp = load_reg(s, rm);
11766         tcg_gen_add_i32(addr, addr, tmp);
11767         tcg_temp_free_i32(tmp);
11768
11769         if (op < 3) { /* store */
11770             tmp = load_reg(s, rd);
11771         } else {
11772             tmp = tcg_temp_new_i32();
11773         }
11774
11775         switch (op) {
11776         case 0: /* str */
11777             gen_aa32_st32_iss(s, tmp, addr, get_mem_index(s), rd | ISSIs16Bit);
11778             break;
11779         case 1: /* strh */
11780             gen_aa32_st16_iss(s, tmp, addr, get_mem_index(s), rd | ISSIs16Bit);
11781             break;
11782         case 2: /* strb */
11783             gen_aa32_st8_iss(s, tmp, addr, get_mem_index(s), rd | ISSIs16Bit);
11784             break;
11785         case 3: /* ldrsb */
11786             gen_aa32_ld8s_iss(s, tmp, addr, get_mem_index(s), rd | ISSIs16Bit);
11787             break;
11788         case 4: /* ldr */
11789             gen_aa32_ld32u_iss(s, tmp, addr, get_mem_index(s), rd | ISSIs16Bit);
11790             break;
11791         case 5: /* ldrh */
11792             gen_aa32_ld16u_iss(s, tmp, addr, get_mem_index(s), rd | ISSIs16Bit);
11793             break;
11794         case 6: /* ldrb */
11795             gen_aa32_ld8u_iss(s, tmp, addr, get_mem_index(s), rd | ISSIs16Bit);
11796             break;
11797         case 7: /* ldrsh */
11798             gen_aa32_ld16s_iss(s, tmp, addr, get_mem_index(s), rd | ISSIs16Bit);
11799             break;
11800         }
11801         if (op >= 3) { /* load */
11802             store_reg(s, rd, tmp);
11803         } else {
11804             tcg_temp_free_i32(tmp);
11805         }
11806         tcg_temp_free_i32(addr);
11807         break;
11808
11809     case 6:
11810         /* load/store word immediate offset */
11811         rd = insn & 7;
11812         rn = (insn >> 3) & 7;
11813         addr = load_reg(s, rn);
11814         val = (insn >> 4) & 0x7c;
11815         tcg_gen_addi_i32(addr, addr, val);
11816
11817         if (insn & (1 << 11)) {
11818             /* load */
11819             tmp = tcg_temp_new_i32();
11820             gen_aa32_ld32u(s, tmp, addr, get_mem_index(s));
11821             store_reg(s, rd, tmp);
11822         } else {
11823             /* store */
11824             tmp = load_reg(s, rd);
11825             gen_aa32_st32(s, tmp, addr, get_mem_index(s));
11826             tcg_temp_free_i32(tmp);
11827         }
11828         tcg_temp_free_i32(addr);
11829         break;
11830
11831     case 7:
11832         /* load/store byte immediate offset */
11833         rd = insn & 7;
11834         rn = (insn >> 3) & 7;
11835         addr = load_reg(s, rn);
11836         val = (insn >> 6) & 0x1f;
11837         tcg_gen_addi_i32(addr, addr, val);
11838
11839         if (insn & (1 << 11)) {
11840             /* load */
11841             tmp = tcg_temp_new_i32();
11842             gen_aa32_ld8u_iss(s, tmp, addr, get_mem_index(s), rd | ISSIs16Bit);
11843             store_reg(s, rd, tmp);
11844         } else {
11845             /* store */
11846             tmp = load_reg(s, rd);
11847             gen_aa32_st8_iss(s, tmp, addr, get_mem_index(s), rd | ISSIs16Bit);
11848             tcg_temp_free_i32(tmp);
11849         }
11850         tcg_temp_free_i32(addr);
11851         break;
11852
11853     case 8:
11854         /* load/store halfword immediate offset */
11855         rd = insn & 7;
11856         rn = (insn >> 3) & 7;
11857         addr = load_reg(s, rn);
11858         val = (insn >> 5) & 0x3e;
11859         tcg_gen_addi_i32(addr, addr, val);
11860
11861         if (insn & (1 << 11)) {
11862             /* load */
11863             tmp = tcg_temp_new_i32();
11864             gen_aa32_ld16u_iss(s, tmp, addr, get_mem_index(s), rd | ISSIs16Bit);
11865             store_reg(s, rd, tmp);
11866         } else {
11867             /* store */
11868             tmp = load_reg(s, rd);
11869             gen_aa32_st16_iss(s, tmp, addr, get_mem_index(s), rd | ISSIs16Bit);
11870             tcg_temp_free_i32(tmp);
11871         }
11872         tcg_temp_free_i32(addr);
11873         break;
11874
11875     case 9:
11876         /* load/store from stack */
11877         rd = (insn >> 8) & 7;
11878         addr = load_reg(s, 13);
11879         val = (insn & 0xff) * 4;
11880         tcg_gen_addi_i32(addr, addr, val);
11881
11882         if (insn & (1 << 11)) {
11883             /* load */
11884             tmp = tcg_temp_new_i32();
11885             gen_aa32_ld32u_iss(s, tmp, addr, get_mem_index(s), rd | ISSIs16Bit);
11886             store_reg(s, rd, tmp);
11887         } else {
11888             /* store */
11889             tmp = load_reg(s, rd);
11890             gen_aa32_st32_iss(s, tmp, addr, get_mem_index(s), rd | ISSIs16Bit);
11891             tcg_temp_free_i32(tmp);
11892         }
11893         tcg_temp_free_i32(addr);
11894         break;
11895
11896     case 10:
11897         /* add to high reg */
11898         rd = (insn >> 8) & 7;
11899         if (insn & (1 << 11)) {
11900             /* SP */
11901             tmp = load_reg(s, 13);
11902         } else {
11903             /* PC. bit 1 is ignored.  */
11904             tmp = tcg_temp_new_i32();
11905             tcg_gen_movi_i32(tmp, (s->pc + 2) & ~(uint32_t)2);
11906         }
11907         val = (insn & 0xff) * 4;
11908         tcg_gen_addi_i32(tmp, tmp, val);
11909         store_reg(s, rd, tmp);
11910         break;
11911
11912     case 11:
11913         /* misc */
11914         op = (insn >> 8) & 0xf;
11915         switch (op) {
11916         case 0:
11917             /* adjust stack pointer */
11918             tmp = load_reg(s, 13);
11919             val = (insn & 0x7f) * 4;
11920             if (insn & (1 << 7))
11921                 val = -(int32_t)val;
11922             tcg_gen_addi_i32(tmp, tmp, val);
11923             store_reg(s, 13, tmp);
11924             break;
11925
11926         case 2: /* sign/zero extend.  */
11927             ARCH(6);
11928             rd = insn & 7;
11929             rm = (insn >> 3) & 7;
11930             tmp = load_reg(s, rm);
11931             switch ((insn >> 6) & 3) {
11932             case 0: gen_sxth(tmp); break;
11933             case 1: gen_sxtb(tmp); break;
11934             case 2: gen_uxth(tmp); break;
11935             case 3: gen_uxtb(tmp); break;
11936             }
11937             store_reg(s, rd, tmp);
11938             break;
11939         case 4: case 5: case 0xc: case 0xd:
11940             /* push/pop */
11941             addr = load_reg(s, 13);
11942             if (insn & (1 << 8))
11943                 offset = 4;
11944             else
11945                 offset = 0;
11946             for (i = 0; i < 8; i++) {
11947                 if (insn & (1 << i))
11948                     offset += 4;
11949             }
11950             if ((insn & (1 << 11)) == 0) {
11951                 tcg_gen_addi_i32(addr, addr, -offset);
11952             }
11953             for (i = 0; i < 8; i++) {
11954                 if (insn & (1 << i)) {
11955                     if (insn & (1 << 11)) {
11956                         /* pop */
11957                         tmp = tcg_temp_new_i32();
11958                         gen_aa32_ld32u(s, tmp, addr, get_mem_index(s));
11959                         store_reg(s, i, tmp);
11960                     } else {
11961                         /* push */
11962                         tmp = load_reg(s, i);
11963                         gen_aa32_st32(s, tmp, addr, get_mem_index(s));
11964                         tcg_temp_free_i32(tmp);
11965                     }
11966                     /* advance to the next address.  */
11967                     tcg_gen_addi_i32(addr, addr, 4);
11968                 }
11969             }
11970             tmp = NULL;
11971             if (insn & (1 << 8)) {
11972                 if (insn & (1 << 11)) {
11973                     /* pop pc */
11974                     tmp = tcg_temp_new_i32();
11975                     gen_aa32_ld32u(s, tmp, addr, get_mem_index(s));
11976                     /* don't set the pc until the rest of the instruction
11977                        has completed */
11978                 } else {
11979                     /* push lr */
11980                     tmp = load_reg(s, 14);
11981                     gen_aa32_st32(s, tmp, addr, get_mem_index(s));
11982                     tcg_temp_free_i32(tmp);
11983                 }
11984                 tcg_gen_addi_i32(addr, addr, 4);
11985             }
11986             if ((insn & (1 << 11)) == 0) {
11987                 tcg_gen_addi_i32(addr, addr, -offset);
11988             }
11989             /* write back the new stack pointer */
11990             store_reg(s, 13, addr);
11991             /* set the new PC value */
11992             if ((insn & 0x0900) == 0x0900) {
11993                 store_reg_from_load(s, 15, tmp);
11994             }
11995             break;
11996
11997         case 1: case 3: case 9: case 11: /* czb */
11998             rm = insn & 7;
11999             tmp = load_reg(s, rm);
12000             s->condlabel = gen_new_label();
12001             s->condjmp = 1;
12002             if (insn & (1 << 11))
12003                 tcg_gen_brcondi_i32(TCG_COND_EQ, tmp, 0, s->condlabel);
12004             else
12005                 tcg_gen_brcondi_i32(TCG_COND_NE, tmp, 0, s->condlabel);
12006             tcg_temp_free_i32(tmp);
12007             offset = ((insn & 0xf8) >> 2) | (insn & 0x200) >> 3;
12008             val = (uint32_t)s->pc + 2;
12009             val += offset;
12010             gen_jmp(s, val);
12011             break;
12012
12013         case 15: /* IT, nop-hint.  */
12014             if ((insn & 0xf) == 0) {
12015                 gen_nop_hint(s, (insn >> 4) & 0xf);
12016                 break;
12017             }
12018             /* If Then.  */
12019             s->condexec_cond = (insn >> 4) & 0xe;
12020             s->condexec_mask = insn & 0x1f;
12021             /* No actual code generated for this insn, just setup state.  */
12022             break;
12023
12024         case 0xe: /* bkpt */
12025         {
12026             int imm8 = extract32(insn, 0, 8);
12027             ARCH(5);
12028             gen_exception_bkpt_insn(s, 2, syn_aa32_bkpt(imm8, true));
12029             break;
12030         }
12031
12032         case 0xa: /* rev, and hlt */
12033         {
12034             int op1 = extract32(insn, 6, 2);
12035
12036             if (op1 == 2) {
12037                 /* HLT */
12038                 int imm6 = extract32(insn, 0, 6);
12039
12040                 gen_hlt(s, imm6);
12041                 break;
12042             }
12043
12044             /* Otherwise this is rev */
12045             ARCH(6);
12046             rn = (insn >> 3) & 0x7;
12047             rd = insn & 0x7;
12048             tmp = load_reg(s, rn);
12049             switch (op1) {
12050             case 0: tcg_gen_bswap32_i32(tmp, tmp); break;
12051             case 1: gen_rev16(tmp); break;
12052             case 3: gen_revsh(tmp); break;
12053             default:
12054                 g_assert_not_reached();
12055             }
12056             store_reg(s, rd, tmp);
12057             break;
12058         }
12059
12060         case 6:
12061             switch ((insn >> 5) & 7) {
12062             case 2:
12063                 /* setend */
12064                 ARCH(6);
12065                 if (((insn >> 3) & 1) != !!(s->be_data == MO_BE)) {
12066                     gen_helper_setend(cpu_env);
12067                     s->base.is_jmp = DISAS_UPDATE;
12068                 }
12069                 break;
12070             case 3:
12071                 /* cps */
12072                 ARCH(6);
12073                 if (IS_USER(s)) {
12074                     break;
12075                 }
12076                 if (arm_dc_feature(s, ARM_FEATURE_M)) {
12077                     tmp = tcg_const_i32((insn & (1 << 4)) != 0);
12078                     /* FAULTMASK */
12079                     if (insn & 1) {
12080                         addr = tcg_const_i32(19);
12081                         gen_helper_v7m_msr(cpu_env, addr, tmp);
12082                         tcg_temp_free_i32(addr);
12083                     }
12084                     /* PRIMASK */
12085                     if (insn & 2) {
12086                         addr = tcg_const_i32(16);
12087                         gen_helper_v7m_msr(cpu_env, addr, tmp);
12088                         tcg_temp_free_i32(addr);
12089                     }
12090                     tcg_temp_free_i32(tmp);
12091                     gen_lookup_tb(s);
12092                 } else {
12093                     if (insn & (1 << 4)) {
12094                         shift = CPSR_A | CPSR_I | CPSR_F;
12095                     } else {
12096                         shift = 0;
12097                     }
12098                     gen_set_psr_im(s, ((insn & 7) << 6), 0, shift);
12099                 }
12100                 break;
12101             default:
12102                 goto undef;
12103             }
12104             break;
12105
12106         default:
12107             goto undef;
12108         }
12109         break;
12110
12111     case 12:
12112     {
12113         /* load/store multiple */
12114         TCGv_i32 loaded_var = NULL;
12115         rn = (insn >> 8) & 0x7;
12116         addr = load_reg(s, rn);
12117         for (i = 0; i < 8; i++) {
12118             if (insn & (1 << i)) {
12119                 if (insn & (1 << 11)) {
12120                     /* load */
12121                     tmp = tcg_temp_new_i32();
12122                     gen_aa32_ld32u(s, tmp, addr, get_mem_index(s));
12123                     if (i == rn) {
12124                         loaded_var = tmp;
12125                     } else {
12126                         store_reg(s, i, tmp);
12127                     }
12128                 } else {
12129                     /* store */
12130                     tmp = load_reg(s, i);
12131                     gen_aa32_st32(s, tmp, addr, get_mem_index(s));
12132                     tcg_temp_free_i32(tmp);
12133                 }
12134                 /* advance to the next address */
12135                 tcg_gen_addi_i32(addr, addr, 4);
12136             }
12137         }
12138         if ((insn & (1 << rn)) == 0) {
12139             /* base reg not in list: base register writeback */
12140             store_reg(s, rn, addr);
12141         } else {
12142             /* base reg in list: if load, complete it now */
12143             if (insn & (1 << 11)) {
12144                 store_reg(s, rn, loaded_var);
12145             }
12146             tcg_temp_free_i32(addr);
12147         }
12148         break;
12149     }
12150     case 13:
12151         /* conditional branch or swi */
12152         cond = (insn >> 8) & 0xf;
12153         if (cond == 0xe)
12154             goto undef;
12155
12156         if (cond == 0xf) {
12157             /* swi */
12158             gen_set_pc_im(s, s->pc);
12159             s->svc_imm = extract32(insn, 0, 8);
12160             s->base.is_jmp = DISAS_SWI;
12161             break;
12162         }
12163         /* generate a conditional jump to next instruction */
12164         s->condlabel = gen_new_label();
12165         arm_gen_test_cc(cond ^ 1, s->condlabel);
12166         s->condjmp = 1;
12167
12168         /* jump to the offset */
12169         val = (uint32_t)s->pc + 2;
12170         offset = ((int32_t)insn << 24) >> 24;
12171         val += offset << 1;
12172         gen_jmp(s, val);
12173         break;
12174
12175     case 14:
12176         if (insn & (1 << 11)) {
12177             /* thumb_insn_is_16bit() ensures we can't get here for
12178              * a Thumb2 CPU, so this must be a thumb1 split BL/BLX:
12179              * 0b1110_1xxx_xxxx_xxxx : BLX suffix (or UNDEF)
12180              */
12181             assert(!arm_dc_feature(s, ARM_FEATURE_THUMB2));
12182             ARCH(5);
12183             offset = ((insn & 0x7ff) << 1);
12184             tmp = load_reg(s, 14);
12185             tcg_gen_addi_i32(tmp, tmp, offset);
12186             tcg_gen_andi_i32(tmp, tmp, 0xfffffffc);
12187
12188             tmp2 = tcg_temp_new_i32();
12189             tcg_gen_movi_i32(tmp2, s->pc | 1);
12190             store_reg(s, 14, tmp2);
12191             gen_bx(s, tmp);
12192             break;
12193         }
12194         /* unconditional branch */
12195         val = (uint32_t)s->pc;
12196         offset = ((int32_t)insn << 21) >> 21;
12197         val += (offset << 1) + 2;
12198         gen_jmp(s, val);
12199         break;
12200
12201     case 15:
12202         /* thumb_insn_is_16bit() ensures we can't get here for
12203          * a Thumb2 CPU, so this must be a thumb1 split BL/BLX.
12204          */
12205         assert(!arm_dc_feature(s, ARM_FEATURE_THUMB2));
12206
12207         if (insn & (1 << 11)) {
12208             /* 0b1111_1xxx_xxxx_xxxx : BL suffix */
12209             offset = ((insn & 0x7ff) << 1) | 1;
12210             tmp = load_reg(s, 14);
12211             tcg_gen_addi_i32(tmp, tmp, offset);
12212
12213             tmp2 = tcg_temp_new_i32();
12214             tcg_gen_movi_i32(tmp2, s->pc | 1);
12215             store_reg(s, 14, tmp2);
12216             gen_bx(s, tmp);
12217         } else {
12218             /* 0b1111_0xxx_xxxx_xxxx : BL/BLX prefix */
12219             uint32_t uoffset = ((int32_t)insn << 21) >> 9;
12220
12221             tcg_gen_movi_i32(cpu_R[14], s->pc + 2 + uoffset);
12222         }
12223         break;
12224     }
12225     return;
12226 illegal_op:
12227 undef:
12228     gen_exception_insn(s, 2, EXCP_UDEF, syn_uncategorized(),
12229                        default_exception_el(s));
12230 }
12231
12232 static bool insn_crosses_page(CPUARMState *env, DisasContext *s)
12233 {
12234     /* Return true if the insn at dc->pc might cross a page boundary.
12235      * (False positives are OK, false negatives are not.)
12236      * We know this is a Thumb insn, and our caller ensures we are
12237      * only called if dc->pc is less than 4 bytes from the page
12238      * boundary, so we cross the page if the first 16 bits indicate
12239      * that this is a 32 bit insn.
12240      */
12241     uint16_t insn = arm_lduw_code(env, s->pc, s->sctlr_b);
12242
12243     return !thumb_insn_is_16bit(s, insn);
12244 }
12245
12246 static int arm_tr_init_disas_context(DisasContextBase *dcbase,
12247                                      CPUState *cs, int max_insns)
12248 {
12249     DisasContext *dc = container_of(dcbase, DisasContext, base);
12250     CPUARMState *env = cs->env_ptr;
12251     ARMCPU *cpu = arm_env_get_cpu(env);
12252
12253     dc->pc = dc->base.pc_first;
12254     dc->condjmp = 0;
12255
12256     dc->aarch64 = 0;
12257     /* If we are coming from secure EL0 in a system with a 32-bit EL3, then
12258      * there is no secure EL1, so we route exceptions to EL3.
12259      */
12260     dc->secure_routed_to_el3 = arm_feature(env, ARM_FEATURE_EL3) &&
12261                                !arm_el_is_aa64(env, 3);
12262     dc->thumb = ARM_TBFLAG_THUMB(dc->base.tb->flags);
12263     dc->sctlr_b = ARM_TBFLAG_SCTLR_B(dc->base.tb->flags);
12264     dc->be_data = ARM_TBFLAG_BE_DATA(dc->base.tb->flags) ? MO_BE : MO_LE;
12265     dc->condexec_mask = (ARM_TBFLAG_CONDEXEC(dc->base.tb->flags) & 0xf) << 1;
12266     dc->condexec_cond = ARM_TBFLAG_CONDEXEC(dc->base.tb->flags) >> 4;
12267     dc->mmu_idx = core_to_arm_mmu_idx(env, ARM_TBFLAG_MMUIDX(dc->base.tb->flags));
12268     dc->current_el = arm_mmu_idx_to_el(dc->mmu_idx);
12269 #if !defined(CONFIG_USER_ONLY)
12270     dc->user = (dc->current_el == 0);
12271 #endif
12272     dc->ns = ARM_TBFLAG_NS(dc->base.tb->flags);
12273     dc->fp_excp_el = ARM_TBFLAG_FPEXC_EL(dc->base.tb->flags);
12274     dc->vfp_enabled = ARM_TBFLAG_VFPEN(dc->base.tb->flags);
12275     dc->vec_len = ARM_TBFLAG_VECLEN(dc->base.tb->flags);
12276     dc->vec_stride = ARM_TBFLAG_VECSTRIDE(dc->base.tb->flags);
12277     dc->c15_cpar = ARM_TBFLAG_XSCALE_CPAR(dc->base.tb->flags);
12278     dc->v7m_handler_mode = ARM_TBFLAG_HANDLER(dc->base.tb->flags);
12279     dc->v8m_secure = arm_feature(env, ARM_FEATURE_M_SECURITY) &&
12280         regime_is_secure(env, dc->mmu_idx);
12281     dc->cp_regs = cpu->cp_regs;
12282     dc->features = env->features;
12283
12284     /* Single step state. The code-generation logic here is:
12285      *  SS_ACTIVE == 0:
12286      *   generate code with no special handling for single-stepping (except
12287      *   that anything that can make us go to SS_ACTIVE == 1 must end the TB;
12288      *   this happens anyway because those changes are all system register or
12289      *   PSTATE writes).
12290      *  SS_ACTIVE == 1, PSTATE.SS == 1: (active-not-pending)
12291      *   emit code for one insn
12292      *   emit code to clear PSTATE.SS
12293      *   emit code to generate software step exception for completed step
12294      *   end TB (as usual for having generated an exception)
12295      *  SS_ACTIVE == 1, PSTATE.SS == 0: (active-pending)
12296      *   emit code to generate a software step exception
12297      *   end the TB
12298      */
12299     dc->ss_active = ARM_TBFLAG_SS_ACTIVE(dc->base.tb->flags);
12300     dc->pstate_ss = ARM_TBFLAG_PSTATE_SS(dc->base.tb->flags);
12301     dc->is_ldex = false;
12302     dc->ss_same_el = false; /* Can't be true since EL_d must be AArch64 */
12303
12304     dc->next_page_start =
12305         (dc->base.pc_first & TARGET_PAGE_MASK) + TARGET_PAGE_SIZE;
12306
12307     /* If architectural single step active, limit to 1.  */
12308     if (is_singlestepping(dc)) {
12309         max_insns = 1;
12310     }
12311
12312     /* ARM is a fixed-length ISA.  Bound the number of insns to execute
12313        to those left on the page.  */
12314     if (!dc->thumb) {
12315         int bound = (dc->next_page_start - dc->base.pc_first) / 4;
12316         max_insns = MIN(max_insns, bound);
12317     }
12318
12319     cpu_F0s = tcg_temp_new_i32();
12320     cpu_F1s = tcg_temp_new_i32();
12321     cpu_F0d = tcg_temp_new_i64();
12322     cpu_F1d = tcg_temp_new_i64();
12323     cpu_V0 = cpu_F0d;
12324     cpu_V1 = cpu_F1d;
12325     /* FIXME: cpu_M0 can probably be the same as cpu_V0.  */
12326     cpu_M0 = tcg_temp_new_i64();
12327
12328     return max_insns;
12329 }
12330
12331 static void arm_tr_tb_start(DisasContextBase *dcbase, CPUState *cpu)
12332 {
12333     DisasContext *dc = container_of(dcbase, DisasContext, base);
12334
12335     /* A note on handling of the condexec (IT) bits:
12336      *
12337      * We want to avoid the overhead of having to write the updated condexec
12338      * bits back to the CPUARMState for every instruction in an IT block. So:
12339      * (1) if the condexec bits are not already zero then we write
12340      * zero back into the CPUARMState now. This avoids complications trying
12341      * to do it at the end of the block. (For example if we don't do this
12342      * it's hard to identify whether we can safely skip writing condexec
12343      * at the end of the TB, which we definitely want to do for the case
12344      * where a TB doesn't do anything with the IT state at all.)
12345      * (2) if we are going to leave the TB then we call gen_set_condexec()
12346      * which will write the correct value into CPUARMState if zero is wrong.
12347      * This is done both for leaving the TB at the end, and for leaving
12348      * it because of an exception we know will happen, which is done in
12349      * gen_exception_insn(). The latter is necessary because we need to
12350      * leave the TB with the PC/IT state just prior to execution of the
12351      * instruction which caused the exception.
12352      * (3) if we leave the TB unexpectedly (eg a data abort on a load)
12353      * then the CPUARMState will be wrong and we need to reset it.
12354      * This is handled in the same way as restoration of the
12355      * PC in these situations; we save the value of the condexec bits
12356      * for each PC via tcg_gen_insn_start(), and restore_state_to_opc()
12357      * then uses this to restore them after an exception.
12358      *
12359      * Note that there are no instructions which can read the condexec
12360      * bits, and none which can write non-static values to them, so
12361      * we don't need to care about whether CPUARMState is correct in the
12362      * middle of a TB.
12363      */
12364
12365     /* Reset the conditional execution bits immediately. This avoids
12366        complications trying to do it at the end of the block.  */
12367     if (dc->condexec_mask || dc->condexec_cond) {
12368         TCGv_i32 tmp = tcg_temp_new_i32();
12369         tcg_gen_movi_i32(tmp, 0);
12370         store_cpu_field(tmp, condexec_bits);
12371     }
12372     tcg_clear_temp_count();
12373 }
12374
12375 static void arm_tr_insn_start(DisasContextBase *dcbase, CPUState *cpu)
12376 {
12377     DisasContext *dc = container_of(dcbase, DisasContext, base);
12378
12379     tcg_gen_insn_start(dc->pc,
12380                        (dc->condexec_cond << 4) | (dc->condexec_mask >> 1),
12381                        0);
12382     dc->insn_start = tcg_last_op();
12383 }
12384
12385 static bool arm_tr_breakpoint_check(DisasContextBase *dcbase, CPUState *cpu,
12386                                     const CPUBreakpoint *bp)
12387 {
12388     DisasContext *dc = container_of(dcbase, DisasContext, base);
12389
12390     if (bp->flags & BP_CPU) {
12391         gen_set_condexec(dc);
12392         gen_set_pc_im(dc, dc->pc);
12393         gen_helper_check_breakpoints(cpu_env);
12394         /* End the TB early; it's likely not going to be executed */
12395         dc->base.is_jmp = DISAS_TOO_MANY;
12396     } else {
12397         gen_exception_internal_insn(dc, 0, EXCP_DEBUG);
12398         /* The address covered by the breakpoint must be
12399            included in [tb->pc, tb->pc + tb->size) in order
12400            to for it to be properly cleared -- thus we
12401            increment the PC here so that the logic setting
12402            tb->size below does the right thing.  */
12403         /* TODO: Advance PC by correct instruction length to
12404          * avoid disassembler error messages */
12405         dc->pc += 2;
12406         dc->base.is_jmp = DISAS_NORETURN;
12407     }
12408
12409     return true;
12410 }
12411
12412 static bool arm_pre_translate_insn(DisasContext *dc)
12413 {
12414 #ifdef CONFIG_USER_ONLY
12415     /* Intercept jump to the magic kernel page.  */
12416     if (dc->pc >= 0xffff0000) {
12417         /* We always get here via a jump, so know we are not in a
12418            conditional execution block.  */
12419         gen_exception_internal(EXCP_KERNEL_TRAP);
12420         dc->base.is_jmp = DISAS_NORETURN;
12421         return true;
12422     }
12423 #endif
12424
12425     if (dc->ss_active && !dc->pstate_ss) {
12426         /* Singlestep state is Active-pending.
12427          * If we're in this state at the start of a TB then either
12428          *  a) we just took an exception to an EL which is being debugged
12429          *     and this is the first insn in the exception handler
12430          *  b) debug exceptions were masked and we just unmasked them
12431          *     without changing EL (eg by clearing PSTATE.D)
12432          * In either case we're going to take a swstep exception in the
12433          * "did not step an insn" case, and so the syndrome ISV and EX
12434          * bits should be zero.
12435          */
12436         assert(dc->base.num_insns == 1);
12437         gen_exception(EXCP_UDEF, syn_swstep(dc->ss_same_el, 0, 0),
12438                       default_exception_el(dc));
12439         dc->base.is_jmp = DISAS_NORETURN;
12440         return true;
12441     }
12442
12443     return false;
12444 }
12445
12446 static void arm_post_translate_insn(DisasContext *dc)
12447 {
12448     if (dc->condjmp && !dc->base.is_jmp) {
12449         gen_set_label(dc->condlabel);
12450         dc->condjmp = 0;
12451     }
12452     dc->base.pc_next = dc->pc;
12453     translator_loop_temp_check(&dc->base);
12454 }
12455
12456 static void arm_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)
12457 {
12458     DisasContext *dc = container_of(dcbase, DisasContext, base);
12459     CPUARMState *env = cpu->env_ptr;
12460     unsigned int insn;
12461
12462     if (arm_pre_translate_insn(dc)) {
12463         return;
12464     }
12465
12466     insn = arm_ldl_code(env, dc->pc, dc->sctlr_b);
12467     dc->insn = insn;
12468     dc->pc += 4;
12469     disas_arm_insn(dc, insn);
12470
12471     arm_post_translate_insn(dc);
12472
12473     /* ARM is a fixed-length ISA.  We performed the cross-page check
12474        in init_disas_context by adjusting max_insns.  */
12475 }
12476
12477 static bool thumb_insn_is_unconditional(DisasContext *s, uint32_t insn)
12478 {
12479     /* Return true if this Thumb insn is always unconditional,
12480      * even inside an IT block. This is true of only a very few
12481      * instructions: BKPT, HLT, and SG.
12482      *
12483      * A larger class of instructions are UNPREDICTABLE if used
12484      * inside an IT block; we do not need to detect those here, because
12485      * what we do by default (perform the cc check and update the IT
12486      * bits state machine) is a permitted CONSTRAINED UNPREDICTABLE
12487      * choice for those situations.
12488      *
12489      * insn is either a 16-bit or a 32-bit instruction; the two are
12490      * distinguishable because for the 16-bit case the top 16 bits
12491      * are zeroes, and that isn't a valid 32-bit encoding.
12492      */
12493     if ((insn & 0xffffff00) == 0xbe00) {
12494         /* BKPT */
12495         return true;
12496     }
12497
12498     if ((insn & 0xffffffc0) == 0xba80 && arm_dc_feature(s, ARM_FEATURE_V8) &&
12499         !arm_dc_feature(s, ARM_FEATURE_M)) {
12500         /* HLT: v8A only. This is unconditional even when it is going to
12501          * UNDEF; see the v8A ARM ARM DDI0487B.a H3.3.
12502          * For v7 cores this was a plain old undefined encoding and so
12503          * honours its cc check. (We might be using the encoding as
12504          * a semihosting trap, but we don't change the cc check behaviour
12505          * on that account, because a debugger connected to a real v7A
12506          * core and emulating semihosting traps by catching the UNDEF
12507          * exception would also only see cases where the cc check passed.
12508          * No guest code should be trying to do a HLT semihosting trap
12509          * in an IT block anyway.
12510          */
12511         return true;
12512     }
12513
12514     if (insn == 0xe97fe97f && arm_dc_feature(s, ARM_FEATURE_V8) &&
12515         arm_dc_feature(s, ARM_FEATURE_M)) {
12516         /* SG: v8M only */
12517         return true;
12518     }
12519
12520     return false;
12521 }
12522
12523 static void thumb_tr_translate_insn(DisasContextBase *dcbase, CPUState *cpu)
12524 {
12525     DisasContext *dc = container_of(dcbase, DisasContext, base);
12526     CPUARMState *env = cpu->env_ptr;
12527     uint32_t insn;
12528     bool is_16bit;
12529
12530     if (arm_pre_translate_insn(dc)) {
12531         return;
12532     }
12533
12534     insn = arm_lduw_code(env, dc->pc, dc->sctlr_b);
12535     is_16bit = thumb_insn_is_16bit(dc, insn);
12536     dc->pc += 2;
12537     if (!is_16bit) {
12538         uint32_t insn2 = arm_lduw_code(env, dc->pc, dc->sctlr_b);
12539
12540         insn = insn << 16 | insn2;
12541         dc->pc += 2;
12542     }
12543     dc->insn = insn;
12544
12545     if (dc->condexec_mask && !thumb_insn_is_unconditional(dc, insn)) {
12546         uint32_t cond = dc->condexec_cond;
12547
12548         if (cond != 0x0e) {     /* Skip conditional when condition is AL. */
12549             dc->condlabel = gen_new_label();
12550             arm_gen_test_cc(cond ^ 1, dc->condlabel);
12551             dc->condjmp = 1;
12552         }
12553     }
12554
12555     if (is_16bit) {
12556         disas_thumb_insn(dc, insn);
12557     } else {
12558         disas_thumb2_insn(dc, insn);
12559     }
12560
12561     /* Advance the Thumb condexec condition.  */
12562     if (dc->condexec_mask) {
12563         dc->condexec_cond = ((dc->condexec_cond & 0xe) |
12564                              ((dc->condexec_mask >> 4) & 1));
12565         dc->condexec_mask = (dc->condexec_mask << 1) & 0x1f;
12566         if (dc->condexec_mask == 0) {
12567             dc->condexec_cond = 0;
12568         }
12569     }
12570
12571     arm_post_translate_insn(dc);
12572
12573     /* Thumb is a variable-length ISA.  Stop translation when the next insn
12574      * will touch a new page.  This ensures that prefetch aborts occur at
12575      * the right place.
12576      *
12577      * We want to stop the TB if the next insn starts in a new page,
12578      * or if it spans between this page and the next. This means that
12579      * if we're looking at the last halfword in the page we need to
12580      * see if it's a 16-bit Thumb insn (which will fit in this TB)
12581      * or a 32-bit Thumb insn (which won't).
12582      * This is to avoid generating a silly TB with a single 16-bit insn
12583      * in it at the end of this page (which would execute correctly
12584      * but isn't very efficient).
12585      */
12586     if (dc->base.is_jmp == DISAS_NEXT
12587         && (dc->pc >= dc->next_page_start
12588             || (dc->pc >= dc->next_page_start - 3
12589                 && insn_crosses_page(env, dc)))) {
12590         dc->base.is_jmp = DISAS_TOO_MANY;
12591     }
12592 }
12593
12594 static void arm_tr_tb_stop(DisasContextBase *dcbase, CPUState *cpu)
12595 {
12596     DisasContext *dc = container_of(dcbase, DisasContext, base);
12597
12598     if (tb_cflags(dc->base.tb) & CF_LAST_IO && dc->condjmp) {
12599         /* FIXME: This can theoretically happen with self-modifying code. */
12600         cpu_abort(cpu, "IO on conditional branch instruction");
12601     }
12602
12603     /* At this stage dc->condjmp will only be set when the skipped
12604        instruction was a conditional branch or trap, and the PC has
12605        already been written.  */
12606     gen_set_condexec(dc);
12607     if (dc->base.is_jmp == DISAS_BX_EXCRET) {
12608         /* Exception return branches need some special case code at the
12609          * end of the TB, which is complex enough that it has to
12610          * handle the single-step vs not and the condition-failed
12611          * insn codepath itself.
12612          */
12613         gen_bx_excret_final_code(dc);
12614     } else if (unlikely(is_singlestepping(dc))) {
12615         /* Unconditional and "condition passed" instruction codepath. */
12616         switch (dc->base.is_jmp) {
12617         case DISAS_SWI:
12618             gen_ss_advance(dc);
12619             gen_exception(EXCP_SWI, syn_aa32_svc(dc->svc_imm, dc->thumb),
12620                           default_exception_el(dc));
12621             break;
12622         case DISAS_HVC:
12623             gen_ss_advance(dc);
12624             gen_exception(EXCP_HVC, syn_aa32_hvc(dc->svc_imm), 2);
12625             break;
12626         case DISAS_SMC:
12627             gen_ss_advance(dc);
12628             gen_exception(EXCP_SMC, syn_aa32_smc(), 3);
12629             break;
12630         case DISAS_NEXT:
12631         case DISAS_TOO_MANY:
12632         case DISAS_UPDATE:
12633             gen_set_pc_im(dc, dc->pc);
12634             /* fall through */
12635         default:
12636             /* FIXME: Single stepping a WFI insn will not halt the CPU. */
12637             gen_singlestep_exception(dc);
12638             break;
12639         case DISAS_NORETURN:
12640             break;
12641         }
12642     } else {
12643         /* While branches must always occur at the end of an IT block,
12644            there are a few other things that can cause us to terminate
12645            the TB in the middle of an IT block:
12646             - Exception generating instructions (bkpt, swi, undefined).
12647             - Page boundaries.
12648             - Hardware watchpoints.
12649            Hardware breakpoints have already been handled and skip this code.
12650          */
12651         switch(dc->base.is_jmp) {
12652         case DISAS_NEXT:
12653         case DISAS_TOO_MANY:
12654             gen_goto_tb(dc, 1, dc->pc);
12655             break;
12656         case DISAS_JUMP:
12657             gen_goto_ptr();
12658             break;
12659         case DISAS_UPDATE:
12660             gen_set_pc_im(dc, dc->pc);
12661             /* fall through */
12662         default:
12663             /* indicate that the hash table must be used to find the next TB */
12664             tcg_gen_exit_tb(0);
12665             break;
12666         case DISAS_NORETURN:
12667             /* nothing more to generate */
12668             break;
12669         case DISAS_WFI:
12670         {
12671             TCGv_i32 tmp = tcg_const_i32((dc->thumb &&
12672                                           !(dc->insn & (1U << 31))) ? 2 : 4);
12673
12674             gen_helper_wfi(cpu_env, tmp);
12675             tcg_temp_free_i32(tmp);
12676             /* The helper doesn't necessarily throw an exception, but we
12677              * must go back to the main loop to check for interrupts anyway.
12678              */
12679             tcg_gen_exit_tb(0);
12680             break;
12681         }
12682         case DISAS_WFE:
12683             gen_helper_wfe(cpu_env);
12684             break;
12685         case DISAS_YIELD:
12686             gen_helper_yield(cpu_env);
12687             break;
12688         case DISAS_SWI:
12689             gen_exception(EXCP_SWI, syn_aa32_svc(dc->svc_imm, dc->thumb),
12690                           default_exception_el(dc));
12691             break;
12692         case DISAS_HVC:
12693             gen_exception(EXCP_HVC, syn_aa32_hvc(dc->svc_imm), 2);
12694             break;
12695         case DISAS_SMC:
12696             gen_exception(EXCP_SMC, syn_aa32_smc(), 3);
12697             break;
12698         }
12699     }
12700
12701     if (dc->condjmp) {
12702         /* "Condition failed" instruction codepath for the branch/trap insn */
12703         gen_set_label(dc->condlabel);
12704         gen_set_condexec(dc);
12705         if (unlikely(is_singlestepping(dc))) {
12706             gen_set_pc_im(dc, dc->pc);
12707             gen_singlestep_exception(dc);
12708         } else {
12709             gen_goto_tb(dc, 1, dc->pc);
12710         }
12711     }
12712
12713     /* Functions above can change dc->pc, so re-align db->pc_next */
12714     dc->base.pc_next = dc->pc;
12715 }
12716
12717 static void arm_tr_disas_log(const DisasContextBase *dcbase, CPUState *cpu)
12718 {
12719     DisasContext *dc = container_of(dcbase, DisasContext, base);
12720
12721     qemu_log("IN: %s\n", lookup_symbol(dc->base.pc_first));
12722     log_target_disas(cpu, dc->base.pc_first, dc->base.tb->size);
12723 }
12724
12725 static const TranslatorOps arm_translator_ops = {
12726     .init_disas_context = arm_tr_init_disas_context,
12727     .tb_start           = arm_tr_tb_start,
12728     .insn_start         = arm_tr_insn_start,
12729     .breakpoint_check   = arm_tr_breakpoint_check,
12730     .translate_insn     = arm_tr_translate_insn,
12731     .tb_stop            = arm_tr_tb_stop,
12732     .disas_log          = arm_tr_disas_log,
12733 };
12734
12735 static const TranslatorOps thumb_translator_ops = {
12736     .init_disas_context = arm_tr_init_disas_context,
12737     .tb_start           = arm_tr_tb_start,
12738     .insn_start         = arm_tr_insn_start,
12739     .breakpoint_check   = arm_tr_breakpoint_check,
12740     .translate_insn     = thumb_tr_translate_insn,
12741     .tb_stop            = arm_tr_tb_stop,
12742     .disas_log          = arm_tr_disas_log,
12743 };
12744
12745 /* generate intermediate code for basic block 'tb'.  */
12746 void gen_intermediate_code(CPUState *cpu, TranslationBlock *tb)
12747 {
12748     DisasContext dc;
12749     const TranslatorOps *ops = &arm_translator_ops;
12750
12751     if (ARM_TBFLAG_THUMB(tb->flags)) {
12752         ops = &thumb_translator_ops;
12753     }
12754 #ifdef TARGET_AARCH64
12755     if (ARM_TBFLAG_AARCH64_STATE(tb->flags)) {
12756         ops = &aarch64_translator_ops;
12757     }
12758 #endif
12759
12760     translator_loop(ops, &dc.base, cpu, tb);
12761 }
12762
12763 static const char *cpu_mode_names[16] = {
12764   "usr", "fiq", "irq", "svc", "???", "???", "mon", "abt",
12765   "???", "???", "hyp", "und", "???", "???", "???", "sys"
12766 };
12767
12768 void arm_cpu_dump_state(CPUState *cs, FILE *f, fprintf_function cpu_fprintf,
12769                         int flags)
12770 {
12771     ARMCPU *cpu = ARM_CPU(cs);
12772     CPUARMState *env = &cpu->env;
12773     int i;
12774
12775     if (is_a64(env)) {
12776         aarch64_cpu_dump_state(cs, f, cpu_fprintf, flags);
12777         return;
12778     }
12779
12780     for(i=0;i<16;i++) {
12781         cpu_fprintf(f, "R%02d=%08x", i, env->regs[i]);
12782         if ((i % 4) == 3)
12783             cpu_fprintf(f, "\n");
12784         else
12785             cpu_fprintf(f, " ");
12786     }
12787
12788     if (arm_feature(env, ARM_FEATURE_M)) {
12789         uint32_t xpsr = xpsr_read(env);
12790         const char *mode;
12791         const char *ns_status = "";
12792
12793         if (arm_feature(env, ARM_FEATURE_M_SECURITY)) {
12794             ns_status = env->v7m.secure ? "S " : "NS ";
12795         }
12796
12797         if (xpsr & XPSR_EXCP) {
12798             mode = "handler";
12799         } else {
12800             if (env->v7m.control[env->v7m.secure] & R_V7M_CONTROL_NPRIV_MASK) {
12801                 mode = "unpriv-thread";
12802             } else {
12803                 mode = "priv-thread";
12804             }
12805         }
12806
12807         cpu_fprintf(f, "XPSR=%08x %c%c%c%c %c %s%s\n",
12808                     xpsr,
12809                     xpsr & XPSR_N ? 'N' : '-',
12810                     xpsr & XPSR_Z ? 'Z' : '-',
12811                     xpsr & XPSR_C ? 'C' : '-',
12812                     xpsr & XPSR_V ? 'V' : '-',
12813                     xpsr & XPSR_T ? 'T' : 'A',
12814                     ns_status,
12815                     mode);
12816     } else {
12817         uint32_t psr = cpsr_read(env);
12818         const char *ns_status = "";
12819
12820         if (arm_feature(env, ARM_FEATURE_EL3) &&
12821             (psr & CPSR_M) != ARM_CPU_MODE_MON) {
12822             ns_status = env->cp15.scr_el3 & SCR_NS ? "NS " : "S ";
12823         }
12824
12825         cpu_fprintf(f, "PSR=%08x %c%c%c%c %c %s%s%d\n",
12826                     psr,
12827                     psr & CPSR_N ? 'N' : '-',
12828                     psr & CPSR_Z ? 'Z' : '-',
12829                     psr & CPSR_C ? 'C' : '-',
12830                     psr & CPSR_V ? 'V' : '-',
12831                     psr & CPSR_T ? 'T' : 'A',
12832                     ns_status,
12833                     cpu_mode_names[psr & 0xf], (psr & 0x10) ? 32 : 26);
12834     }
12835
12836     if (flags & CPU_DUMP_FPU) {
12837         int numvfpregs = 0;
12838         if (arm_feature(env, ARM_FEATURE_VFP)) {
12839             numvfpregs += 16;
12840         }
12841         if (arm_feature(env, ARM_FEATURE_VFP3)) {
12842             numvfpregs += 16;
12843         }
12844         for (i = 0; i < numvfpregs; i++) {
12845             uint64_t v = *aa32_vfp_dreg(env, i);
12846             cpu_fprintf(f, "s%02d=%08x s%02d=%08x d%02d=%016" PRIx64 "\n",
12847                         i * 2, (uint32_t)v,
12848                         i * 2 + 1, (uint32_t)(v >> 32),
12849                         i, v);
12850         }
12851         cpu_fprintf(f, "FPSCR: %08x\n", (int)env->vfp.xregs[ARM_VFP_FPSCR]);
12852     }
12853 }
12854
12855 void restore_state_to_opc(CPUARMState *env, TranslationBlock *tb,
12856                           target_ulong *data)
12857 {
12858     if (is_a64(env)) {
12859         env->pc = data[0];
12860         env->condexec_bits = 0;
12861         env->exception.syndrome = data[2] << ARM_INSN_START_WORD2_SHIFT;
12862     } else {
12863         env->regs[15] = data[0];
12864         env->condexec_bits = data[1];
12865         env->exception.syndrome = data[2] << ARM_INSN_START_WORD2_SHIFT;
12866     }
12867 }
This page took 0.760091 seconds and 4 git commands to generate.