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