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