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