]> Git Repo - qemu.git/blob - target-arm/translate.c
75e464d1b10bd356b2ea4d851bfb48c7e3870e20
[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 <stdarg.h>
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <inttypes.h>
26
27 #include "cpu.h"
28 #include "disas.h"
29 #include "tcg-op.h"
30 #include "qemu-log.h"
31
32 #include "helper.h"
33 #define GEN_HELPER 1
34 #include "helper.h"
35
36 #define ENABLE_ARCH_4T    arm_feature(env, ARM_FEATURE_V4T)
37 #define ENABLE_ARCH_5     arm_feature(env, ARM_FEATURE_V5)
38 /* currently all emulated v5 cores are also v5TE, so don't bother */
39 #define ENABLE_ARCH_5TE   arm_feature(env, ARM_FEATURE_V5)
40 #define ENABLE_ARCH_5J    0
41 #define ENABLE_ARCH_6     arm_feature(env, ARM_FEATURE_V6)
42 #define ENABLE_ARCH_6K   arm_feature(env, ARM_FEATURE_V6K)
43 #define ENABLE_ARCH_6T2   arm_feature(env, ARM_FEATURE_THUMB2)
44 #define ENABLE_ARCH_7     arm_feature(env, ARM_FEATURE_V7)
45
46 #define ARCH(x) do { if (!ENABLE_ARCH_##x) goto illegal_op; } while(0)
47
48 /* internal defines */
49 typedef struct DisasContext {
50     target_ulong pc;
51     int is_jmp;
52     /* Nonzero if this instruction has been conditionally skipped.  */
53     int condjmp;
54     /* The label that will be jumped to when the instruction is skipped.  */
55     int condlabel;
56     /* Thumb-2 condtional execution bits.  */
57     int condexec_mask;
58     int condexec_cond;
59     struct TranslationBlock *tb;
60     int singlestep_enabled;
61     int thumb;
62     int bswap_code;
63 #if !defined(CONFIG_USER_ONLY)
64     int user;
65 #endif
66     int vfp_enabled;
67     int vec_len;
68     int vec_stride;
69 } DisasContext;
70
71 static uint32_t gen_opc_condexec_bits[OPC_BUF_SIZE];
72
73 #if defined(CONFIG_USER_ONLY)
74 #define IS_USER(s) 1
75 #else
76 #define IS_USER(s) (s->user)
77 #endif
78
79 /* These instructions trap after executing, so defer them until after the
80    conditional executions state has been updated.  */
81 #define DISAS_WFI 4
82 #define DISAS_SWI 5
83
84 static TCGv_ptr cpu_env;
85 /* We reuse the same 64-bit temporaries for efficiency.  */
86 static TCGv_i64 cpu_V0, cpu_V1, cpu_M0;
87 static TCGv_i32 cpu_R[16];
88 static TCGv_i32 cpu_exclusive_addr;
89 static TCGv_i32 cpu_exclusive_val;
90 static TCGv_i32 cpu_exclusive_high;
91 #ifdef CONFIG_USER_ONLY
92 static TCGv_i32 cpu_exclusive_test;
93 static TCGv_i32 cpu_exclusive_info;
94 #endif
95
96 /* FIXME:  These should be removed.  */
97 static TCGv cpu_F0s, cpu_F1s;
98 static TCGv_i64 cpu_F0d, cpu_F1d;
99
100 #include "gen-icount.h"
101
102 static const char *regnames[] =
103     { "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
104       "r8", "r9", "r10", "r11", "r12", "r13", "r14", "pc" };
105
106 /* initialize TCG globals.  */
107 void arm_translate_init(void)
108 {
109     int i;
110
111     cpu_env = tcg_global_reg_new_ptr(TCG_AREG0, "env");
112
113     for (i = 0; i < 16; i++) {
114         cpu_R[i] = tcg_global_mem_new_i32(TCG_AREG0,
115                                           offsetof(CPUARMState, regs[i]),
116                                           regnames[i]);
117     }
118     cpu_exclusive_addr = tcg_global_mem_new_i32(TCG_AREG0,
119         offsetof(CPUARMState, exclusive_addr), "exclusive_addr");
120     cpu_exclusive_val = tcg_global_mem_new_i32(TCG_AREG0,
121         offsetof(CPUARMState, exclusive_val), "exclusive_val");
122     cpu_exclusive_high = tcg_global_mem_new_i32(TCG_AREG0,
123         offsetof(CPUARMState, exclusive_high), "exclusive_high");
124 #ifdef CONFIG_USER_ONLY
125     cpu_exclusive_test = tcg_global_mem_new_i32(TCG_AREG0,
126         offsetof(CPUARMState, exclusive_test), "exclusive_test");
127     cpu_exclusive_info = tcg_global_mem_new_i32(TCG_AREG0,
128         offsetof(CPUARMState, exclusive_info), "exclusive_info");
129 #endif
130
131 #define GEN_HELPER 2
132 #include "helper.h"
133 }
134
135 static inline TCGv load_cpu_offset(int offset)
136 {
137     TCGv tmp = tcg_temp_new_i32();
138     tcg_gen_ld_i32(tmp, cpu_env, offset);
139     return tmp;
140 }
141
142 #define load_cpu_field(name) load_cpu_offset(offsetof(CPUARMState, name))
143
144 static inline void store_cpu_offset(TCGv var, int offset)
145 {
146     tcg_gen_st_i32(var, cpu_env, offset);
147     tcg_temp_free_i32(var);
148 }
149
150 #define store_cpu_field(var, name) \
151     store_cpu_offset(var, offsetof(CPUARMState, name))
152
153 /* Set a variable to the value of a CPU register.  */
154 static void load_reg_var(DisasContext *s, TCGv var, int reg)
155 {
156     if (reg == 15) {
157         uint32_t addr;
158         /* normaly, since we updated PC, we need only to add one insn */
159         if (s->thumb)
160             addr = (long)s->pc + 2;
161         else
162             addr = (long)s->pc + 4;
163         tcg_gen_movi_i32(var, addr);
164     } else {
165         tcg_gen_mov_i32(var, cpu_R[reg]);
166     }
167 }
168
169 /* Create a new temporary and set it to the value of a CPU register.  */
170 static inline TCGv load_reg(DisasContext *s, int reg)
171 {
172     TCGv tmp = tcg_temp_new_i32();
173     load_reg_var(s, tmp, reg);
174     return tmp;
175 }
176
177 /* Set a CPU register.  The source must be a temporary and will be
178    marked as dead.  */
179 static void store_reg(DisasContext *s, int reg, TCGv var)
180 {
181     if (reg == 15) {
182         tcg_gen_andi_i32(var, var, ~1);
183         s->is_jmp = DISAS_JUMP;
184     }
185     tcg_gen_mov_i32(cpu_R[reg], var);
186     tcg_temp_free_i32(var);
187 }
188
189 /* Value extensions.  */
190 #define gen_uxtb(var) tcg_gen_ext8u_i32(var, var)
191 #define gen_uxth(var) tcg_gen_ext16u_i32(var, var)
192 #define gen_sxtb(var) tcg_gen_ext8s_i32(var, var)
193 #define gen_sxth(var) tcg_gen_ext16s_i32(var, var)
194
195 #define gen_sxtb16(var) gen_helper_sxtb16(var, var)
196 #define gen_uxtb16(var) gen_helper_uxtb16(var, var)
197
198
199 static inline void gen_set_cpsr(TCGv var, uint32_t mask)
200 {
201     TCGv tmp_mask = tcg_const_i32(mask);
202     gen_helper_cpsr_write(var, tmp_mask);
203     tcg_temp_free_i32(tmp_mask);
204 }
205 /* Set NZCV flags from the high 4 bits of var.  */
206 #define gen_set_nzcv(var) gen_set_cpsr(var, CPSR_NZCV)
207
208 static void gen_exception(int excp)
209 {
210     TCGv tmp = tcg_temp_new_i32();
211     tcg_gen_movi_i32(tmp, excp);
212     gen_helper_exception(tmp);
213     tcg_temp_free_i32(tmp);
214 }
215
216 static void gen_smul_dual(TCGv a, TCGv b)
217 {
218     TCGv tmp1 = tcg_temp_new_i32();
219     TCGv tmp2 = tcg_temp_new_i32();
220     tcg_gen_ext16s_i32(tmp1, a);
221     tcg_gen_ext16s_i32(tmp2, b);
222     tcg_gen_mul_i32(tmp1, tmp1, tmp2);
223     tcg_temp_free_i32(tmp2);
224     tcg_gen_sari_i32(a, a, 16);
225     tcg_gen_sari_i32(b, b, 16);
226     tcg_gen_mul_i32(b, b, a);
227     tcg_gen_mov_i32(a, tmp1);
228     tcg_temp_free_i32(tmp1);
229 }
230
231 /* Byteswap each halfword.  */
232 static void gen_rev16(TCGv var)
233 {
234     TCGv tmp = tcg_temp_new_i32();
235     tcg_gen_shri_i32(tmp, var, 8);
236     tcg_gen_andi_i32(tmp, tmp, 0x00ff00ff);
237     tcg_gen_shli_i32(var, var, 8);
238     tcg_gen_andi_i32(var, var, 0xff00ff00);
239     tcg_gen_or_i32(var, var, tmp);
240     tcg_temp_free_i32(tmp);
241 }
242
243 /* Byteswap low halfword and sign extend.  */
244 static void gen_revsh(TCGv var)
245 {
246     tcg_gen_ext16u_i32(var, var);
247     tcg_gen_bswap16_i32(var, var);
248     tcg_gen_ext16s_i32(var, var);
249 }
250
251 /* Unsigned bitfield extract.  */
252 static void gen_ubfx(TCGv var, int shift, uint32_t mask)
253 {
254     if (shift)
255         tcg_gen_shri_i32(var, var, shift);
256     tcg_gen_andi_i32(var, var, mask);
257 }
258
259 /* Signed bitfield extract.  */
260 static void gen_sbfx(TCGv var, int shift, int width)
261 {
262     uint32_t signbit;
263
264     if (shift)
265         tcg_gen_sari_i32(var, var, shift);
266     if (shift + width < 32) {
267         signbit = 1u << (width - 1);
268         tcg_gen_andi_i32(var, var, (1u << width) - 1);
269         tcg_gen_xori_i32(var, var, signbit);
270         tcg_gen_subi_i32(var, var, signbit);
271     }
272 }
273
274 /* Bitfield insertion.  Insert val into base.  Clobbers base and val.  */
275 static void gen_bfi(TCGv dest, TCGv base, TCGv val, int shift, uint32_t mask)
276 {
277     tcg_gen_andi_i32(val, val, mask);
278     tcg_gen_shli_i32(val, val, shift);
279     tcg_gen_andi_i32(base, base, ~(mask << shift));
280     tcg_gen_or_i32(dest, base, val);
281 }
282
283 /* Return (b << 32) + a. Mark inputs as dead */
284 static TCGv_i64 gen_addq_msw(TCGv_i64 a, TCGv b)
285 {
286     TCGv_i64 tmp64 = tcg_temp_new_i64();
287
288     tcg_gen_extu_i32_i64(tmp64, b);
289     tcg_temp_free_i32(b);
290     tcg_gen_shli_i64(tmp64, tmp64, 32);
291     tcg_gen_add_i64(a, tmp64, a);
292
293     tcg_temp_free_i64(tmp64);
294     return a;
295 }
296
297 /* Return (b << 32) - a. Mark inputs as dead. */
298 static TCGv_i64 gen_subq_msw(TCGv_i64 a, TCGv b)
299 {
300     TCGv_i64 tmp64 = tcg_temp_new_i64();
301
302     tcg_gen_extu_i32_i64(tmp64, b);
303     tcg_temp_free_i32(b);
304     tcg_gen_shli_i64(tmp64, tmp64, 32);
305     tcg_gen_sub_i64(a, tmp64, a);
306
307     tcg_temp_free_i64(tmp64);
308     return a;
309 }
310
311 /* FIXME: Most targets have native widening multiplication.
312    It would be good to use that instead of a full wide multiply.  */
313 /* 32x32->64 multiply.  Marks inputs as dead.  */
314 static TCGv_i64 gen_mulu_i64_i32(TCGv a, TCGv b)
315 {
316     TCGv_i64 tmp1 = tcg_temp_new_i64();
317     TCGv_i64 tmp2 = tcg_temp_new_i64();
318
319     tcg_gen_extu_i32_i64(tmp1, a);
320     tcg_temp_free_i32(a);
321     tcg_gen_extu_i32_i64(tmp2, b);
322     tcg_temp_free_i32(b);
323     tcg_gen_mul_i64(tmp1, tmp1, tmp2);
324     tcg_temp_free_i64(tmp2);
325     return tmp1;
326 }
327
328 static TCGv_i64 gen_muls_i64_i32(TCGv a, TCGv b)
329 {
330     TCGv_i64 tmp1 = tcg_temp_new_i64();
331     TCGv_i64 tmp2 = tcg_temp_new_i64();
332
333     tcg_gen_ext_i32_i64(tmp1, a);
334     tcg_temp_free_i32(a);
335     tcg_gen_ext_i32_i64(tmp2, b);
336     tcg_temp_free_i32(b);
337     tcg_gen_mul_i64(tmp1, tmp1, tmp2);
338     tcg_temp_free_i64(tmp2);
339     return tmp1;
340 }
341
342 /* Swap low and high halfwords.  */
343 static void gen_swap_half(TCGv var)
344 {
345     TCGv tmp = tcg_temp_new_i32();
346     tcg_gen_shri_i32(tmp, var, 16);
347     tcg_gen_shli_i32(var, var, 16);
348     tcg_gen_or_i32(var, var, tmp);
349     tcg_temp_free_i32(tmp);
350 }
351
352 /* Dual 16-bit add.  Result placed in t0 and t1 is marked as dead.
353     tmp = (t0 ^ t1) & 0x8000;
354     t0 &= ~0x8000;
355     t1 &= ~0x8000;
356     t0 = (t0 + t1) ^ tmp;
357  */
358
359 static void gen_add16(TCGv t0, TCGv t1)
360 {
361     TCGv tmp = tcg_temp_new_i32();
362     tcg_gen_xor_i32(tmp, t0, t1);
363     tcg_gen_andi_i32(tmp, tmp, 0x8000);
364     tcg_gen_andi_i32(t0, t0, ~0x8000);
365     tcg_gen_andi_i32(t1, t1, ~0x8000);
366     tcg_gen_add_i32(t0, t0, t1);
367     tcg_gen_xor_i32(t0, t0, tmp);
368     tcg_temp_free_i32(tmp);
369     tcg_temp_free_i32(t1);
370 }
371
372 #define gen_set_CF(var) tcg_gen_st_i32(var, cpu_env, offsetof(CPUARMState, CF))
373
374 /* Set CF to the top bit of var.  */
375 static void gen_set_CF_bit31(TCGv var)
376 {
377     TCGv tmp = tcg_temp_new_i32();
378     tcg_gen_shri_i32(tmp, var, 31);
379     gen_set_CF(tmp);
380     tcg_temp_free_i32(tmp);
381 }
382
383 /* Set N and Z flags from var.  */
384 static inline void gen_logic_CC(TCGv var)
385 {
386     tcg_gen_st_i32(var, cpu_env, offsetof(CPUARMState, NF));
387     tcg_gen_st_i32(var, cpu_env, offsetof(CPUARMState, ZF));
388 }
389
390 /* T0 += T1 + CF.  */
391 static void gen_adc(TCGv t0, TCGv t1)
392 {
393     TCGv tmp;
394     tcg_gen_add_i32(t0, t0, t1);
395     tmp = load_cpu_field(CF);
396     tcg_gen_add_i32(t0, t0, tmp);
397     tcg_temp_free_i32(tmp);
398 }
399
400 /* dest = T0 + T1 + CF. */
401 static void gen_add_carry(TCGv dest, TCGv t0, TCGv t1)
402 {
403     TCGv tmp;
404     tcg_gen_add_i32(dest, t0, t1);
405     tmp = load_cpu_field(CF);
406     tcg_gen_add_i32(dest, dest, tmp);
407     tcg_temp_free_i32(tmp);
408 }
409
410 /* dest = T0 - T1 + CF - 1.  */
411 static void gen_sub_carry(TCGv dest, TCGv t0, TCGv t1)
412 {
413     TCGv tmp;
414     tcg_gen_sub_i32(dest, t0, t1);
415     tmp = load_cpu_field(CF);
416     tcg_gen_add_i32(dest, dest, tmp);
417     tcg_gen_subi_i32(dest, dest, 1);
418     tcg_temp_free_i32(tmp);
419 }
420
421 /* FIXME:  Implement this natively.  */
422 #define tcg_gen_abs_i32(t0, t1) gen_helper_abs(t0, t1)
423
424 static void shifter_out_im(TCGv var, int shift)
425 {
426     TCGv tmp = tcg_temp_new_i32();
427     if (shift == 0) {
428         tcg_gen_andi_i32(tmp, var, 1);
429     } else {
430         tcg_gen_shri_i32(tmp, var, shift);
431         if (shift != 31)
432             tcg_gen_andi_i32(tmp, tmp, 1);
433     }
434     gen_set_CF(tmp);
435     tcg_temp_free_i32(tmp);
436 }
437
438 /* Shift by immediate.  Includes special handling for shift == 0.  */
439 static inline void gen_arm_shift_im(TCGv var, int shiftop, int shift, int flags)
440 {
441     switch (shiftop) {
442     case 0: /* LSL */
443         if (shift != 0) {
444             if (flags)
445                 shifter_out_im(var, 32 - shift);
446             tcg_gen_shli_i32(var, var, shift);
447         }
448         break;
449     case 1: /* LSR */
450         if (shift == 0) {
451             if (flags) {
452                 tcg_gen_shri_i32(var, var, 31);
453                 gen_set_CF(var);
454             }
455             tcg_gen_movi_i32(var, 0);
456         } else {
457             if (flags)
458                 shifter_out_im(var, shift - 1);
459             tcg_gen_shri_i32(var, var, shift);
460         }
461         break;
462     case 2: /* ASR */
463         if (shift == 0)
464             shift = 32;
465         if (flags)
466             shifter_out_im(var, shift - 1);
467         if (shift == 32)
468           shift = 31;
469         tcg_gen_sari_i32(var, var, shift);
470         break;
471     case 3: /* ROR/RRX */
472         if (shift != 0) {
473             if (flags)
474                 shifter_out_im(var, shift - 1);
475             tcg_gen_rotri_i32(var, var, shift); break;
476         } else {
477             TCGv tmp = load_cpu_field(CF);
478             if (flags)
479                 shifter_out_im(var, 0);
480             tcg_gen_shri_i32(var, var, 1);
481             tcg_gen_shli_i32(tmp, tmp, 31);
482             tcg_gen_or_i32(var, var, tmp);
483             tcg_temp_free_i32(tmp);
484         }
485     }
486 };
487
488 static inline void gen_arm_shift_reg(TCGv var, int shiftop,
489                                      TCGv shift, int flags)
490 {
491     if (flags) {
492         switch (shiftop) {
493         case 0: gen_helper_shl_cc(var, var, shift); break;
494         case 1: gen_helper_shr_cc(var, var, shift); break;
495         case 2: gen_helper_sar_cc(var, var, shift); break;
496         case 3: gen_helper_ror_cc(var, var, shift); break;
497         }
498     } else {
499         switch (shiftop) {
500         case 0: gen_helper_shl(var, var, shift); break;
501         case 1: gen_helper_shr(var, var, shift); break;
502         case 2: gen_helper_sar(var, var, shift); break;
503         case 3: tcg_gen_andi_i32(shift, shift, 0x1f);
504                 tcg_gen_rotr_i32(var, var, shift); break;
505         }
506     }
507     tcg_temp_free_i32(shift);
508 }
509
510 #define PAS_OP(pfx) \
511     switch (op2) {  \
512     case 0: gen_pas_helper(glue(pfx,add16)); break; \
513     case 1: gen_pas_helper(glue(pfx,addsubx)); break; \
514     case 2: gen_pas_helper(glue(pfx,subaddx)); break; \
515     case 3: gen_pas_helper(glue(pfx,sub16)); break; \
516     case 4: gen_pas_helper(glue(pfx,add8)); break; \
517     case 7: gen_pas_helper(glue(pfx,sub8)); break; \
518     }
519 static void gen_arm_parallel_addsub(int op1, int op2, TCGv a, TCGv b)
520 {
521     TCGv_ptr tmp;
522
523     switch (op1) {
524 #define gen_pas_helper(name) glue(gen_helper_,name)(a, a, b, tmp)
525     case 1:
526         tmp = tcg_temp_new_ptr();
527         tcg_gen_addi_ptr(tmp, cpu_env, offsetof(CPUARMState, GE));
528         PAS_OP(s)
529         tcg_temp_free_ptr(tmp);
530         break;
531     case 5:
532         tmp = tcg_temp_new_ptr();
533         tcg_gen_addi_ptr(tmp, cpu_env, offsetof(CPUARMState, GE));
534         PAS_OP(u)
535         tcg_temp_free_ptr(tmp);
536         break;
537 #undef gen_pas_helper
538 #define gen_pas_helper(name) glue(gen_helper_,name)(a, a, b)
539     case 2:
540         PAS_OP(q);
541         break;
542     case 3:
543         PAS_OP(sh);
544         break;
545     case 6:
546         PAS_OP(uq);
547         break;
548     case 7:
549         PAS_OP(uh);
550         break;
551 #undef gen_pas_helper
552     }
553 }
554 #undef PAS_OP
555
556 /* For unknown reasons Arm and Thumb-2 use arbitrarily different encodings.  */
557 #define PAS_OP(pfx) \
558     switch (op1) {  \
559     case 0: gen_pas_helper(glue(pfx,add8)); break; \
560     case 1: gen_pas_helper(glue(pfx,add16)); break; \
561     case 2: gen_pas_helper(glue(pfx,addsubx)); break; \
562     case 4: gen_pas_helper(glue(pfx,sub8)); break; \
563     case 5: gen_pas_helper(glue(pfx,sub16)); break; \
564     case 6: gen_pas_helper(glue(pfx,subaddx)); break; \
565     }
566 static void gen_thumb2_parallel_addsub(int op1, int op2, TCGv a, TCGv b)
567 {
568     TCGv_ptr tmp;
569
570     switch (op2) {
571 #define gen_pas_helper(name) glue(gen_helper_,name)(a, a, b, tmp)
572     case 0:
573         tmp = tcg_temp_new_ptr();
574         tcg_gen_addi_ptr(tmp, cpu_env, offsetof(CPUARMState, GE));
575         PAS_OP(s)
576         tcg_temp_free_ptr(tmp);
577         break;
578     case 4:
579         tmp = tcg_temp_new_ptr();
580         tcg_gen_addi_ptr(tmp, cpu_env, offsetof(CPUARMState, GE));
581         PAS_OP(u)
582         tcg_temp_free_ptr(tmp);
583         break;
584 #undef gen_pas_helper
585 #define gen_pas_helper(name) glue(gen_helper_,name)(a, a, b)
586     case 1:
587         PAS_OP(q);
588         break;
589     case 2:
590         PAS_OP(sh);
591         break;
592     case 5:
593         PAS_OP(uq);
594         break;
595     case 6:
596         PAS_OP(uh);
597         break;
598 #undef gen_pas_helper
599     }
600 }
601 #undef PAS_OP
602
603 static void gen_test_cc(int cc, int label)
604 {
605     TCGv tmp;
606     TCGv tmp2;
607     int inv;
608
609     switch (cc) {
610     case 0: /* eq: Z */
611         tmp = load_cpu_field(ZF);
612         tcg_gen_brcondi_i32(TCG_COND_EQ, tmp, 0, label);
613         break;
614     case 1: /* ne: !Z */
615         tmp = load_cpu_field(ZF);
616         tcg_gen_brcondi_i32(TCG_COND_NE, tmp, 0, label);
617         break;
618     case 2: /* cs: C */
619         tmp = load_cpu_field(CF);
620         tcg_gen_brcondi_i32(TCG_COND_NE, tmp, 0, label);
621         break;
622     case 3: /* cc: !C */
623         tmp = load_cpu_field(CF);
624         tcg_gen_brcondi_i32(TCG_COND_EQ, tmp, 0, label);
625         break;
626     case 4: /* mi: N */
627         tmp = load_cpu_field(NF);
628         tcg_gen_brcondi_i32(TCG_COND_LT, tmp, 0, label);
629         break;
630     case 5: /* pl: !N */
631         tmp = load_cpu_field(NF);
632         tcg_gen_brcondi_i32(TCG_COND_GE, tmp, 0, label);
633         break;
634     case 6: /* vs: V */
635         tmp = load_cpu_field(VF);
636         tcg_gen_brcondi_i32(TCG_COND_LT, tmp, 0, label);
637         break;
638     case 7: /* vc: !V */
639         tmp = load_cpu_field(VF);
640         tcg_gen_brcondi_i32(TCG_COND_GE, tmp, 0, label);
641         break;
642     case 8: /* hi: C && !Z */
643         inv = gen_new_label();
644         tmp = load_cpu_field(CF);
645         tcg_gen_brcondi_i32(TCG_COND_EQ, tmp, 0, inv);
646         tcg_temp_free_i32(tmp);
647         tmp = load_cpu_field(ZF);
648         tcg_gen_brcondi_i32(TCG_COND_NE, tmp, 0, label);
649         gen_set_label(inv);
650         break;
651     case 9: /* ls: !C || Z */
652         tmp = load_cpu_field(CF);
653         tcg_gen_brcondi_i32(TCG_COND_EQ, tmp, 0, label);
654         tcg_temp_free_i32(tmp);
655         tmp = load_cpu_field(ZF);
656         tcg_gen_brcondi_i32(TCG_COND_EQ, tmp, 0, label);
657         break;
658     case 10: /* ge: N == V -> N ^ V == 0 */
659         tmp = load_cpu_field(VF);
660         tmp2 = load_cpu_field(NF);
661         tcg_gen_xor_i32(tmp, tmp, tmp2);
662         tcg_temp_free_i32(tmp2);
663         tcg_gen_brcondi_i32(TCG_COND_GE, tmp, 0, label);
664         break;
665     case 11: /* lt: N != V -> N ^ V != 0 */
666         tmp = load_cpu_field(VF);
667         tmp2 = load_cpu_field(NF);
668         tcg_gen_xor_i32(tmp, tmp, tmp2);
669         tcg_temp_free_i32(tmp2);
670         tcg_gen_brcondi_i32(TCG_COND_LT, tmp, 0, label);
671         break;
672     case 12: /* gt: !Z && N == V */
673         inv = gen_new_label();
674         tmp = load_cpu_field(ZF);
675         tcg_gen_brcondi_i32(TCG_COND_EQ, tmp, 0, inv);
676         tcg_temp_free_i32(tmp);
677         tmp = load_cpu_field(VF);
678         tmp2 = load_cpu_field(NF);
679         tcg_gen_xor_i32(tmp, tmp, tmp2);
680         tcg_temp_free_i32(tmp2);
681         tcg_gen_brcondi_i32(TCG_COND_GE, tmp, 0, label);
682         gen_set_label(inv);
683         break;
684     case 13: /* le: Z || N != V */
685         tmp = load_cpu_field(ZF);
686         tcg_gen_brcondi_i32(TCG_COND_EQ, tmp, 0, label);
687         tcg_temp_free_i32(tmp);
688         tmp = load_cpu_field(VF);
689         tmp2 = load_cpu_field(NF);
690         tcg_gen_xor_i32(tmp, tmp, tmp2);
691         tcg_temp_free_i32(tmp2);
692         tcg_gen_brcondi_i32(TCG_COND_LT, tmp, 0, label);
693         break;
694     default:
695         fprintf(stderr, "Bad condition code 0x%x\n", cc);
696         abort();
697     }
698     tcg_temp_free_i32(tmp);
699 }
700
701 static const uint8_t table_logic_cc[16] = {
702     1, /* and */
703     1, /* xor */
704     0, /* sub */
705     0, /* rsb */
706     0, /* add */
707     0, /* adc */
708     0, /* sbc */
709     0, /* rsc */
710     1, /* andl */
711     1, /* xorl */
712     0, /* cmp */
713     0, /* cmn */
714     1, /* orr */
715     1, /* mov */
716     1, /* bic */
717     1, /* mvn */
718 };
719
720 /* Set PC and Thumb state from an immediate address.  */
721 static inline void gen_bx_im(DisasContext *s, uint32_t addr)
722 {
723     TCGv tmp;
724
725     s->is_jmp = DISAS_UPDATE;
726     if (s->thumb != (addr & 1)) {
727         tmp = tcg_temp_new_i32();
728         tcg_gen_movi_i32(tmp, addr & 1);
729         tcg_gen_st_i32(tmp, cpu_env, offsetof(CPUARMState, thumb));
730         tcg_temp_free_i32(tmp);
731     }
732     tcg_gen_movi_i32(cpu_R[15], addr & ~1);
733 }
734
735 /* Set PC and Thumb state from var.  var is marked as dead.  */
736 static inline void gen_bx(DisasContext *s, TCGv var)
737 {
738     s->is_jmp = DISAS_UPDATE;
739     tcg_gen_andi_i32(cpu_R[15], var, ~1);
740     tcg_gen_andi_i32(var, var, 1);
741     store_cpu_field(var, thumb);
742 }
743
744 /* Variant of store_reg which uses branch&exchange logic when storing
745    to r15 in ARM architecture v7 and above. The source must be a temporary
746    and will be marked as dead. */
747 static inline void store_reg_bx(CPUARMState *env, DisasContext *s,
748                                 int reg, TCGv var)
749 {
750     if (reg == 15 && ENABLE_ARCH_7) {
751         gen_bx(s, var);
752     } else {
753         store_reg(s, reg, var);
754     }
755 }
756
757 /* Variant of store_reg which uses branch&exchange logic when storing
758  * to r15 in ARM architecture v5T and above. This is used for storing
759  * the results of a LDR/LDM/POP into r15, and corresponds to the cases
760  * in the ARM ARM which use the LoadWritePC() pseudocode function. */
761 static inline void store_reg_from_load(CPUARMState *env, DisasContext *s,
762                                 int reg, TCGv var)
763 {
764     if (reg == 15 && ENABLE_ARCH_5) {
765         gen_bx(s, var);
766     } else {
767         store_reg(s, reg, var);
768     }
769 }
770
771 static inline TCGv gen_ld8s(TCGv addr, int index)
772 {
773     TCGv tmp = tcg_temp_new_i32();
774     tcg_gen_qemu_ld8s(tmp, addr, index);
775     return tmp;
776 }
777 static inline TCGv gen_ld8u(TCGv addr, int index)
778 {
779     TCGv tmp = tcg_temp_new_i32();
780     tcg_gen_qemu_ld8u(tmp, addr, index);
781     return tmp;
782 }
783 static inline TCGv gen_ld16s(TCGv addr, int index)
784 {
785     TCGv tmp = tcg_temp_new_i32();
786     tcg_gen_qemu_ld16s(tmp, addr, index);
787     return tmp;
788 }
789 static inline TCGv gen_ld16u(TCGv addr, int index)
790 {
791     TCGv tmp = tcg_temp_new_i32();
792     tcg_gen_qemu_ld16u(tmp, addr, index);
793     return tmp;
794 }
795 static inline TCGv gen_ld32(TCGv addr, int index)
796 {
797     TCGv tmp = tcg_temp_new_i32();
798     tcg_gen_qemu_ld32u(tmp, addr, index);
799     return tmp;
800 }
801 static inline TCGv_i64 gen_ld64(TCGv addr, int index)
802 {
803     TCGv_i64 tmp = tcg_temp_new_i64();
804     tcg_gen_qemu_ld64(tmp, addr, index);
805     return tmp;
806 }
807 static inline void gen_st8(TCGv val, TCGv addr, int index)
808 {
809     tcg_gen_qemu_st8(val, addr, index);
810     tcg_temp_free_i32(val);
811 }
812 static inline void gen_st16(TCGv val, TCGv addr, int index)
813 {
814     tcg_gen_qemu_st16(val, addr, index);
815     tcg_temp_free_i32(val);
816 }
817 static inline void gen_st32(TCGv val, TCGv addr, int index)
818 {
819     tcg_gen_qemu_st32(val, addr, index);
820     tcg_temp_free_i32(val);
821 }
822 static inline void gen_st64(TCGv_i64 val, TCGv addr, int index)
823 {
824     tcg_gen_qemu_st64(val, addr, index);
825     tcg_temp_free_i64(val);
826 }
827
828 static inline void gen_set_pc_im(uint32_t val)
829 {
830     tcg_gen_movi_i32(cpu_R[15], val);
831 }
832
833 /* Force a TB lookup after an instruction that changes the CPU state.  */
834 static inline void gen_lookup_tb(DisasContext *s)
835 {
836     tcg_gen_movi_i32(cpu_R[15], s->pc & ~1);
837     s->is_jmp = DISAS_UPDATE;
838 }
839
840 static inline void gen_add_data_offset(DisasContext *s, unsigned int insn,
841                                        TCGv var)
842 {
843     int val, rm, shift, shiftop;
844     TCGv offset;
845
846     if (!(insn & (1 << 25))) {
847         /* immediate */
848         val = insn & 0xfff;
849         if (!(insn & (1 << 23)))
850             val = -val;
851         if (val != 0)
852             tcg_gen_addi_i32(var, var, val);
853     } else {
854         /* shift/register */
855         rm = (insn) & 0xf;
856         shift = (insn >> 7) & 0x1f;
857         shiftop = (insn >> 5) & 3;
858         offset = load_reg(s, rm);
859         gen_arm_shift_im(offset, shiftop, shift, 0);
860         if (!(insn & (1 << 23)))
861             tcg_gen_sub_i32(var, var, offset);
862         else
863             tcg_gen_add_i32(var, var, offset);
864         tcg_temp_free_i32(offset);
865     }
866 }
867
868 static inline void gen_add_datah_offset(DisasContext *s, unsigned int insn,
869                                         int extra, TCGv var)
870 {
871     int val, rm;
872     TCGv offset;
873
874     if (insn & (1 << 22)) {
875         /* immediate */
876         val = (insn & 0xf) | ((insn >> 4) & 0xf0);
877         if (!(insn & (1 << 23)))
878             val = -val;
879         val += extra;
880         if (val != 0)
881             tcg_gen_addi_i32(var, var, val);
882     } else {
883         /* register */
884         if (extra)
885             tcg_gen_addi_i32(var, var, extra);
886         rm = (insn) & 0xf;
887         offset = load_reg(s, rm);
888         if (!(insn & (1 << 23)))
889             tcg_gen_sub_i32(var, var, offset);
890         else
891             tcg_gen_add_i32(var, var, offset);
892         tcg_temp_free_i32(offset);
893     }
894 }
895
896 static TCGv_ptr get_fpstatus_ptr(int neon)
897 {
898     TCGv_ptr statusptr = tcg_temp_new_ptr();
899     int offset;
900     if (neon) {
901         offset = offsetof(CPUARMState, vfp.standard_fp_status);
902     } else {
903         offset = offsetof(CPUARMState, vfp.fp_status);
904     }
905     tcg_gen_addi_ptr(statusptr, cpu_env, offset);
906     return statusptr;
907 }
908
909 #define VFP_OP2(name)                                                 \
910 static inline void gen_vfp_##name(int dp)                             \
911 {                                                                     \
912     TCGv_ptr fpst = get_fpstatus_ptr(0);                              \
913     if (dp) {                                                         \
914         gen_helper_vfp_##name##d(cpu_F0d, cpu_F0d, cpu_F1d, fpst);    \
915     } else {                                                          \
916         gen_helper_vfp_##name##s(cpu_F0s, cpu_F0s, cpu_F1s, fpst);    \
917     }                                                                 \
918     tcg_temp_free_ptr(fpst);                                          \
919 }
920
921 VFP_OP2(add)
922 VFP_OP2(sub)
923 VFP_OP2(mul)
924 VFP_OP2(div)
925
926 #undef VFP_OP2
927
928 static inline void gen_vfp_F1_mul(int dp)
929 {
930     /* Like gen_vfp_mul() but put result in F1 */
931     TCGv_ptr fpst = get_fpstatus_ptr(0);
932     if (dp) {
933         gen_helper_vfp_muld(cpu_F1d, cpu_F0d, cpu_F1d, fpst);
934     } else {
935         gen_helper_vfp_muls(cpu_F1s, cpu_F0s, cpu_F1s, fpst);
936     }
937     tcg_temp_free_ptr(fpst);
938 }
939
940 static inline void gen_vfp_F1_neg(int dp)
941 {
942     /* Like gen_vfp_neg() but put result in F1 */
943     if (dp) {
944         gen_helper_vfp_negd(cpu_F1d, cpu_F0d);
945     } else {
946         gen_helper_vfp_negs(cpu_F1s, cpu_F0s);
947     }
948 }
949
950 static inline void gen_vfp_abs(int dp)
951 {
952     if (dp)
953         gen_helper_vfp_absd(cpu_F0d, cpu_F0d);
954     else
955         gen_helper_vfp_abss(cpu_F0s, cpu_F0s);
956 }
957
958 static inline void gen_vfp_neg(int dp)
959 {
960     if (dp)
961         gen_helper_vfp_negd(cpu_F0d, cpu_F0d);
962     else
963         gen_helper_vfp_negs(cpu_F0s, cpu_F0s);
964 }
965
966 static inline void gen_vfp_sqrt(int dp)
967 {
968     if (dp)
969         gen_helper_vfp_sqrtd(cpu_F0d, cpu_F0d, cpu_env);
970     else
971         gen_helper_vfp_sqrts(cpu_F0s, cpu_F0s, cpu_env);
972 }
973
974 static inline void gen_vfp_cmp(int dp)
975 {
976     if (dp)
977         gen_helper_vfp_cmpd(cpu_F0d, cpu_F1d, cpu_env);
978     else
979         gen_helper_vfp_cmps(cpu_F0s, cpu_F1s, cpu_env);
980 }
981
982 static inline void gen_vfp_cmpe(int dp)
983 {
984     if (dp)
985         gen_helper_vfp_cmped(cpu_F0d, cpu_F1d, cpu_env);
986     else
987         gen_helper_vfp_cmpes(cpu_F0s, cpu_F1s, cpu_env);
988 }
989
990 static inline void gen_vfp_F1_ld0(int dp)
991 {
992     if (dp)
993         tcg_gen_movi_i64(cpu_F1d, 0);
994     else
995         tcg_gen_movi_i32(cpu_F1s, 0);
996 }
997
998 #define VFP_GEN_ITOF(name) \
999 static inline void gen_vfp_##name(int dp, int neon) \
1000 { \
1001     TCGv_ptr statusptr = get_fpstatus_ptr(neon); \
1002     if (dp) { \
1003         gen_helper_vfp_##name##d(cpu_F0d, cpu_F0s, statusptr); \
1004     } else { \
1005         gen_helper_vfp_##name##s(cpu_F0s, cpu_F0s, statusptr); \
1006     } \
1007     tcg_temp_free_ptr(statusptr); \
1008 }
1009
1010 VFP_GEN_ITOF(uito)
1011 VFP_GEN_ITOF(sito)
1012 #undef VFP_GEN_ITOF
1013
1014 #define VFP_GEN_FTOI(name) \
1015 static inline void gen_vfp_##name(int dp, int neon) \
1016 { \
1017     TCGv_ptr statusptr = get_fpstatus_ptr(neon); \
1018     if (dp) { \
1019         gen_helper_vfp_##name##d(cpu_F0s, cpu_F0d, statusptr); \
1020     } else { \
1021         gen_helper_vfp_##name##s(cpu_F0s, cpu_F0s, statusptr); \
1022     } \
1023     tcg_temp_free_ptr(statusptr); \
1024 }
1025
1026 VFP_GEN_FTOI(toui)
1027 VFP_GEN_FTOI(touiz)
1028 VFP_GEN_FTOI(tosi)
1029 VFP_GEN_FTOI(tosiz)
1030 #undef VFP_GEN_FTOI
1031
1032 #define VFP_GEN_FIX(name) \
1033 static inline void gen_vfp_##name(int dp, int shift, int neon) \
1034 { \
1035     TCGv tmp_shift = tcg_const_i32(shift); \
1036     TCGv_ptr statusptr = get_fpstatus_ptr(neon); \
1037     if (dp) { \
1038         gen_helper_vfp_##name##d(cpu_F0d, cpu_F0d, tmp_shift, statusptr); \
1039     } else { \
1040         gen_helper_vfp_##name##s(cpu_F0s, cpu_F0s, tmp_shift, statusptr); \
1041     } \
1042     tcg_temp_free_i32(tmp_shift); \
1043     tcg_temp_free_ptr(statusptr); \
1044 }
1045 VFP_GEN_FIX(tosh)
1046 VFP_GEN_FIX(tosl)
1047 VFP_GEN_FIX(touh)
1048 VFP_GEN_FIX(toul)
1049 VFP_GEN_FIX(shto)
1050 VFP_GEN_FIX(slto)
1051 VFP_GEN_FIX(uhto)
1052 VFP_GEN_FIX(ulto)
1053 #undef VFP_GEN_FIX
1054
1055 static inline void gen_vfp_ld(DisasContext *s, int dp, TCGv addr)
1056 {
1057     if (dp)
1058         tcg_gen_qemu_ld64(cpu_F0d, addr, IS_USER(s));
1059     else
1060         tcg_gen_qemu_ld32u(cpu_F0s, addr, IS_USER(s));
1061 }
1062
1063 static inline void gen_vfp_st(DisasContext *s, int dp, TCGv addr)
1064 {
1065     if (dp)
1066         tcg_gen_qemu_st64(cpu_F0d, addr, IS_USER(s));
1067     else
1068         tcg_gen_qemu_st32(cpu_F0s, addr, IS_USER(s));
1069 }
1070
1071 static inline long
1072 vfp_reg_offset (int dp, int reg)
1073 {
1074     if (dp)
1075         return offsetof(CPUARMState, vfp.regs[reg]);
1076     else if (reg & 1) {
1077         return offsetof(CPUARMState, vfp.regs[reg >> 1])
1078           + offsetof(CPU_DoubleU, l.upper);
1079     } else {
1080         return offsetof(CPUARMState, vfp.regs[reg >> 1])
1081           + offsetof(CPU_DoubleU, l.lower);
1082     }
1083 }
1084
1085 /* Return the offset of a 32-bit piece of a NEON register.
1086    zero is the least significant end of the register.  */
1087 static inline long
1088 neon_reg_offset (int reg, int n)
1089 {
1090     int sreg;
1091     sreg = reg * 2 + n;
1092     return vfp_reg_offset(0, sreg);
1093 }
1094
1095 static TCGv neon_load_reg(int reg, int pass)
1096 {
1097     TCGv tmp = tcg_temp_new_i32();
1098     tcg_gen_ld_i32(tmp, cpu_env, neon_reg_offset(reg, pass));
1099     return tmp;
1100 }
1101
1102 static void neon_store_reg(int reg, int pass, TCGv var)
1103 {
1104     tcg_gen_st_i32(var, cpu_env, neon_reg_offset(reg, pass));
1105     tcg_temp_free_i32(var);
1106 }
1107
1108 static inline void neon_load_reg64(TCGv_i64 var, int reg)
1109 {
1110     tcg_gen_ld_i64(var, cpu_env, vfp_reg_offset(1, reg));
1111 }
1112
1113 static inline void neon_store_reg64(TCGv_i64 var, int reg)
1114 {
1115     tcg_gen_st_i64(var, cpu_env, vfp_reg_offset(1, reg));
1116 }
1117
1118 #define tcg_gen_ld_f32 tcg_gen_ld_i32
1119 #define tcg_gen_ld_f64 tcg_gen_ld_i64
1120 #define tcg_gen_st_f32 tcg_gen_st_i32
1121 #define tcg_gen_st_f64 tcg_gen_st_i64
1122
1123 static inline void gen_mov_F0_vreg(int dp, int reg)
1124 {
1125     if (dp)
1126         tcg_gen_ld_f64(cpu_F0d, cpu_env, vfp_reg_offset(dp, reg));
1127     else
1128         tcg_gen_ld_f32(cpu_F0s, cpu_env, vfp_reg_offset(dp, reg));
1129 }
1130
1131 static inline void gen_mov_F1_vreg(int dp, int reg)
1132 {
1133     if (dp)
1134         tcg_gen_ld_f64(cpu_F1d, cpu_env, vfp_reg_offset(dp, reg));
1135     else
1136         tcg_gen_ld_f32(cpu_F1s, cpu_env, vfp_reg_offset(dp, reg));
1137 }
1138
1139 static inline void gen_mov_vreg_F0(int dp, int reg)
1140 {
1141     if (dp)
1142         tcg_gen_st_f64(cpu_F0d, cpu_env, vfp_reg_offset(dp, reg));
1143     else
1144         tcg_gen_st_f32(cpu_F0s, cpu_env, vfp_reg_offset(dp, reg));
1145 }
1146
1147 #define ARM_CP_RW_BIT   (1 << 20)
1148
1149 static inline void iwmmxt_load_reg(TCGv_i64 var, int reg)
1150 {
1151     tcg_gen_ld_i64(var, cpu_env, offsetof(CPUARMState, iwmmxt.regs[reg]));
1152 }
1153
1154 static inline void iwmmxt_store_reg(TCGv_i64 var, int reg)
1155 {
1156     tcg_gen_st_i64(var, cpu_env, offsetof(CPUARMState, iwmmxt.regs[reg]));
1157 }
1158
1159 static inline TCGv iwmmxt_load_creg(int reg)
1160 {
1161     TCGv var = tcg_temp_new_i32();
1162     tcg_gen_ld_i32(var, cpu_env, offsetof(CPUARMState, iwmmxt.cregs[reg]));
1163     return var;
1164 }
1165
1166 static inline void iwmmxt_store_creg(int reg, TCGv var)
1167 {
1168     tcg_gen_st_i32(var, cpu_env, offsetof(CPUARMState, iwmmxt.cregs[reg]));
1169     tcg_temp_free_i32(var);
1170 }
1171
1172 static inline void gen_op_iwmmxt_movq_wRn_M0(int rn)
1173 {
1174     iwmmxt_store_reg(cpu_M0, rn);
1175 }
1176
1177 static inline void gen_op_iwmmxt_movq_M0_wRn(int rn)
1178 {
1179     iwmmxt_load_reg(cpu_M0, rn);
1180 }
1181
1182 static inline void gen_op_iwmmxt_orq_M0_wRn(int rn)
1183 {
1184     iwmmxt_load_reg(cpu_V1, rn);
1185     tcg_gen_or_i64(cpu_M0, cpu_M0, cpu_V1);
1186 }
1187
1188 static inline void gen_op_iwmmxt_andq_M0_wRn(int rn)
1189 {
1190     iwmmxt_load_reg(cpu_V1, rn);
1191     tcg_gen_and_i64(cpu_M0, cpu_M0, cpu_V1);
1192 }
1193
1194 static inline void gen_op_iwmmxt_xorq_M0_wRn(int rn)
1195 {
1196     iwmmxt_load_reg(cpu_V1, rn);
1197     tcg_gen_xor_i64(cpu_M0, cpu_M0, cpu_V1);
1198 }
1199
1200 #define IWMMXT_OP(name) \
1201 static inline void gen_op_iwmmxt_##name##_M0_wRn(int rn) \
1202 { \
1203     iwmmxt_load_reg(cpu_V1, rn); \
1204     gen_helper_iwmmxt_##name(cpu_M0, cpu_M0, cpu_V1); \
1205 }
1206
1207 #define IWMMXT_OP_ENV(name) \
1208 static inline void gen_op_iwmmxt_##name##_M0_wRn(int rn) \
1209 { \
1210     iwmmxt_load_reg(cpu_V1, rn); \
1211     gen_helper_iwmmxt_##name(cpu_M0, cpu_env, cpu_M0, cpu_V1); \
1212 }
1213
1214 #define IWMMXT_OP_ENV_SIZE(name) \
1215 IWMMXT_OP_ENV(name##b) \
1216 IWMMXT_OP_ENV(name##w) \
1217 IWMMXT_OP_ENV(name##l)
1218
1219 #define IWMMXT_OP_ENV1(name) \
1220 static inline void gen_op_iwmmxt_##name##_M0(void) \
1221 { \
1222     gen_helper_iwmmxt_##name(cpu_M0, cpu_env, cpu_M0); \
1223 }
1224
1225 IWMMXT_OP(maddsq)
1226 IWMMXT_OP(madduq)
1227 IWMMXT_OP(sadb)
1228 IWMMXT_OP(sadw)
1229 IWMMXT_OP(mulslw)
1230 IWMMXT_OP(mulshw)
1231 IWMMXT_OP(mululw)
1232 IWMMXT_OP(muluhw)
1233 IWMMXT_OP(macsw)
1234 IWMMXT_OP(macuw)
1235
1236 IWMMXT_OP_ENV_SIZE(unpackl)
1237 IWMMXT_OP_ENV_SIZE(unpackh)
1238
1239 IWMMXT_OP_ENV1(unpacklub)
1240 IWMMXT_OP_ENV1(unpackluw)
1241 IWMMXT_OP_ENV1(unpacklul)
1242 IWMMXT_OP_ENV1(unpackhub)
1243 IWMMXT_OP_ENV1(unpackhuw)
1244 IWMMXT_OP_ENV1(unpackhul)
1245 IWMMXT_OP_ENV1(unpacklsb)
1246 IWMMXT_OP_ENV1(unpacklsw)
1247 IWMMXT_OP_ENV1(unpacklsl)
1248 IWMMXT_OP_ENV1(unpackhsb)
1249 IWMMXT_OP_ENV1(unpackhsw)
1250 IWMMXT_OP_ENV1(unpackhsl)
1251
1252 IWMMXT_OP_ENV_SIZE(cmpeq)
1253 IWMMXT_OP_ENV_SIZE(cmpgtu)
1254 IWMMXT_OP_ENV_SIZE(cmpgts)
1255
1256 IWMMXT_OP_ENV_SIZE(mins)
1257 IWMMXT_OP_ENV_SIZE(minu)
1258 IWMMXT_OP_ENV_SIZE(maxs)
1259 IWMMXT_OP_ENV_SIZE(maxu)
1260
1261 IWMMXT_OP_ENV_SIZE(subn)
1262 IWMMXT_OP_ENV_SIZE(addn)
1263 IWMMXT_OP_ENV_SIZE(subu)
1264 IWMMXT_OP_ENV_SIZE(addu)
1265 IWMMXT_OP_ENV_SIZE(subs)
1266 IWMMXT_OP_ENV_SIZE(adds)
1267
1268 IWMMXT_OP_ENV(avgb0)
1269 IWMMXT_OP_ENV(avgb1)
1270 IWMMXT_OP_ENV(avgw0)
1271 IWMMXT_OP_ENV(avgw1)
1272
1273 IWMMXT_OP(msadb)
1274
1275 IWMMXT_OP_ENV(packuw)
1276 IWMMXT_OP_ENV(packul)
1277 IWMMXT_OP_ENV(packuq)
1278 IWMMXT_OP_ENV(packsw)
1279 IWMMXT_OP_ENV(packsl)
1280 IWMMXT_OP_ENV(packsq)
1281
1282 static void gen_op_iwmmxt_set_mup(void)
1283 {
1284     TCGv tmp;
1285     tmp = load_cpu_field(iwmmxt.cregs[ARM_IWMMXT_wCon]);
1286     tcg_gen_ori_i32(tmp, tmp, 2);
1287     store_cpu_field(tmp, iwmmxt.cregs[ARM_IWMMXT_wCon]);
1288 }
1289
1290 static void gen_op_iwmmxt_set_cup(void)
1291 {
1292     TCGv tmp;
1293     tmp = load_cpu_field(iwmmxt.cregs[ARM_IWMMXT_wCon]);
1294     tcg_gen_ori_i32(tmp, tmp, 1);
1295     store_cpu_field(tmp, iwmmxt.cregs[ARM_IWMMXT_wCon]);
1296 }
1297
1298 static void gen_op_iwmmxt_setpsr_nz(void)
1299 {
1300     TCGv tmp = tcg_temp_new_i32();
1301     gen_helper_iwmmxt_setpsr_nz(tmp, cpu_M0);
1302     store_cpu_field(tmp, iwmmxt.cregs[ARM_IWMMXT_wCASF]);
1303 }
1304
1305 static inline void gen_op_iwmmxt_addl_M0_wRn(int rn)
1306 {
1307     iwmmxt_load_reg(cpu_V1, rn);
1308     tcg_gen_ext32u_i64(cpu_V1, cpu_V1);
1309     tcg_gen_add_i64(cpu_M0, cpu_M0, cpu_V1);
1310 }
1311
1312 static inline int gen_iwmmxt_address(DisasContext *s, uint32_t insn, TCGv dest)
1313 {
1314     int rd;
1315     uint32_t offset;
1316     TCGv tmp;
1317
1318     rd = (insn >> 16) & 0xf;
1319     tmp = load_reg(s, rd);
1320
1321     offset = (insn & 0xff) << ((insn >> 7) & 2);
1322     if (insn & (1 << 24)) {
1323         /* Pre indexed */
1324         if (insn & (1 << 23))
1325             tcg_gen_addi_i32(tmp, tmp, offset);
1326         else
1327             tcg_gen_addi_i32(tmp, tmp, -offset);
1328         tcg_gen_mov_i32(dest, tmp);
1329         if (insn & (1 << 21))
1330             store_reg(s, rd, tmp);
1331         else
1332             tcg_temp_free_i32(tmp);
1333     } else if (insn & (1 << 21)) {
1334         /* Post indexed */
1335         tcg_gen_mov_i32(dest, tmp);
1336         if (insn & (1 << 23))
1337             tcg_gen_addi_i32(tmp, tmp, offset);
1338         else
1339             tcg_gen_addi_i32(tmp, tmp, -offset);
1340         store_reg(s, rd, tmp);
1341     } else if (!(insn & (1 << 23)))
1342         return 1;
1343     return 0;
1344 }
1345
1346 static inline int gen_iwmmxt_shift(uint32_t insn, uint32_t mask, TCGv dest)
1347 {
1348     int rd = (insn >> 0) & 0xf;
1349     TCGv tmp;
1350
1351     if (insn & (1 << 8)) {
1352         if (rd < ARM_IWMMXT_wCGR0 || rd > ARM_IWMMXT_wCGR3) {
1353             return 1;
1354         } else {
1355             tmp = iwmmxt_load_creg(rd);
1356         }
1357     } else {
1358         tmp = tcg_temp_new_i32();
1359         iwmmxt_load_reg(cpu_V0, rd);
1360         tcg_gen_trunc_i64_i32(tmp, cpu_V0);
1361     }
1362     tcg_gen_andi_i32(tmp, tmp, mask);
1363     tcg_gen_mov_i32(dest, tmp);
1364     tcg_temp_free_i32(tmp);
1365     return 0;
1366 }
1367
1368 /* Disassemble an iwMMXt instruction.  Returns nonzero if an error occurred
1369    (ie. an undefined instruction).  */
1370 static int disas_iwmmxt_insn(CPUARMState *env, DisasContext *s, uint32_t insn)
1371 {
1372     int rd, wrd;
1373     int rdhi, rdlo, rd0, rd1, i;
1374     TCGv addr;
1375     TCGv tmp, tmp2, tmp3;
1376
1377     if ((insn & 0x0e000e00) == 0x0c000000) {
1378         if ((insn & 0x0fe00ff0) == 0x0c400000) {
1379             wrd = insn & 0xf;
1380             rdlo = (insn >> 12) & 0xf;
1381             rdhi = (insn >> 16) & 0xf;
1382             if (insn & ARM_CP_RW_BIT) {                 /* TMRRC */
1383                 iwmmxt_load_reg(cpu_V0, wrd);
1384                 tcg_gen_trunc_i64_i32(cpu_R[rdlo], cpu_V0);
1385                 tcg_gen_shri_i64(cpu_V0, cpu_V0, 32);
1386                 tcg_gen_trunc_i64_i32(cpu_R[rdhi], cpu_V0);
1387             } else {                                    /* TMCRR */
1388                 tcg_gen_concat_i32_i64(cpu_V0, cpu_R[rdlo], cpu_R[rdhi]);
1389                 iwmmxt_store_reg(cpu_V0, wrd);
1390                 gen_op_iwmmxt_set_mup();
1391             }
1392             return 0;
1393         }
1394
1395         wrd = (insn >> 12) & 0xf;
1396         addr = tcg_temp_new_i32();
1397         if (gen_iwmmxt_address(s, insn, addr)) {
1398             tcg_temp_free_i32(addr);
1399             return 1;
1400         }
1401         if (insn & ARM_CP_RW_BIT) {
1402             if ((insn >> 28) == 0xf) {                  /* WLDRW wCx */
1403                 tmp = tcg_temp_new_i32();
1404                 tcg_gen_qemu_ld32u(tmp, addr, IS_USER(s));
1405                 iwmmxt_store_creg(wrd, tmp);
1406             } else {
1407                 i = 1;
1408                 if (insn & (1 << 8)) {
1409                     if (insn & (1 << 22)) {             /* WLDRD */
1410                         tcg_gen_qemu_ld64(cpu_M0, addr, IS_USER(s));
1411                         i = 0;
1412                     } else {                            /* WLDRW wRd */
1413                         tmp = gen_ld32(addr, IS_USER(s));
1414                     }
1415                 } else {
1416                     if (insn & (1 << 22)) {             /* WLDRH */
1417                         tmp = gen_ld16u(addr, IS_USER(s));
1418                     } else {                            /* WLDRB */
1419                         tmp = gen_ld8u(addr, IS_USER(s));
1420                     }
1421                 }
1422                 if (i) {
1423                     tcg_gen_extu_i32_i64(cpu_M0, tmp);
1424                     tcg_temp_free_i32(tmp);
1425                 }
1426                 gen_op_iwmmxt_movq_wRn_M0(wrd);
1427             }
1428         } else {
1429             if ((insn >> 28) == 0xf) {                  /* WSTRW wCx */
1430                 tmp = iwmmxt_load_creg(wrd);
1431                 gen_st32(tmp, addr, IS_USER(s));
1432             } else {
1433                 gen_op_iwmmxt_movq_M0_wRn(wrd);
1434                 tmp = tcg_temp_new_i32();
1435                 if (insn & (1 << 8)) {
1436                     if (insn & (1 << 22)) {             /* WSTRD */
1437                         tcg_temp_free_i32(tmp);
1438                         tcg_gen_qemu_st64(cpu_M0, addr, IS_USER(s));
1439                     } else {                            /* WSTRW wRd */
1440                         tcg_gen_trunc_i64_i32(tmp, cpu_M0);
1441                         gen_st32(tmp, addr, IS_USER(s));
1442                     }
1443                 } else {
1444                     if (insn & (1 << 22)) {             /* WSTRH */
1445                         tcg_gen_trunc_i64_i32(tmp, cpu_M0);
1446                         gen_st16(tmp, addr, IS_USER(s));
1447                     } else {                            /* WSTRB */
1448                         tcg_gen_trunc_i64_i32(tmp, cpu_M0);
1449                         gen_st8(tmp, addr, IS_USER(s));
1450                     }
1451                 }
1452             }
1453         }
1454         tcg_temp_free_i32(addr);
1455         return 0;
1456     }
1457
1458     if ((insn & 0x0f000000) != 0x0e000000)
1459         return 1;
1460
1461     switch (((insn >> 12) & 0xf00) | ((insn >> 4) & 0xff)) {
1462     case 0x000:                                         /* WOR */
1463         wrd = (insn >> 12) & 0xf;
1464         rd0 = (insn >> 0) & 0xf;
1465         rd1 = (insn >> 16) & 0xf;
1466         gen_op_iwmmxt_movq_M0_wRn(rd0);
1467         gen_op_iwmmxt_orq_M0_wRn(rd1);
1468         gen_op_iwmmxt_setpsr_nz();
1469         gen_op_iwmmxt_movq_wRn_M0(wrd);
1470         gen_op_iwmmxt_set_mup();
1471         gen_op_iwmmxt_set_cup();
1472         break;
1473     case 0x011:                                         /* TMCR */
1474         if (insn & 0xf)
1475             return 1;
1476         rd = (insn >> 12) & 0xf;
1477         wrd = (insn >> 16) & 0xf;
1478         switch (wrd) {
1479         case ARM_IWMMXT_wCID:
1480         case ARM_IWMMXT_wCASF:
1481             break;
1482         case ARM_IWMMXT_wCon:
1483             gen_op_iwmmxt_set_cup();
1484             /* Fall through.  */
1485         case ARM_IWMMXT_wCSSF:
1486             tmp = iwmmxt_load_creg(wrd);
1487             tmp2 = load_reg(s, rd);
1488             tcg_gen_andc_i32(tmp, tmp, tmp2);
1489             tcg_temp_free_i32(tmp2);
1490             iwmmxt_store_creg(wrd, tmp);
1491             break;
1492         case ARM_IWMMXT_wCGR0:
1493         case ARM_IWMMXT_wCGR1:
1494         case ARM_IWMMXT_wCGR2:
1495         case ARM_IWMMXT_wCGR3:
1496             gen_op_iwmmxt_set_cup();
1497             tmp = load_reg(s, rd);
1498             iwmmxt_store_creg(wrd, tmp);
1499             break;
1500         default:
1501             return 1;
1502         }
1503         break;
1504     case 0x100:                                         /* WXOR */
1505         wrd = (insn >> 12) & 0xf;
1506         rd0 = (insn >> 0) & 0xf;
1507         rd1 = (insn >> 16) & 0xf;
1508         gen_op_iwmmxt_movq_M0_wRn(rd0);
1509         gen_op_iwmmxt_xorq_M0_wRn(rd1);
1510         gen_op_iwmmxt_setpsr_nz();
1511         gen_op_iwmmxt_movq_wRn_M0(wrd);
1512         gen_op_iwmmxt_set_mup();
1513         gen_op_iwmmxt_set_cup();
1514         break;
1515     case 0x111:                                         /* TMRC */
1516         if (insn & 0xf)
1517             return 1;
1518         rd = (insn >> 12) & 0xf;
1519         wrd = (insn >> 16) & 0xf;
1520         tmp = iwmmxt_load_creg(wrd);
1521         store_reg(s, rd, tmp);
1522         break;
1523     case 0x300:                                         /* WANDN */
1524         wrd = (insn >> 12) & 0xf;
1525         rd0 = (insn >> 0) & 0xf;
1526         rd1 = (insn >> 16) & 0xf;
1527         gen_op_iwmmxt_movq_M0_wRn(rd0);
1528         tcg_gen_neg_i64(cpu_M0, cpu_M0);
1529         gen_op_iwmmxt_andq_M0_wRn(rd1);
1530         gen_op_iwmmxt_setpsr_nz();
1531         gen_op_iwmmxt_movq_wRn_M0(wrd);
1532         gen_op_iwmmxt_set_mup();
1533         gen_op_iwmmxt_set_cup();
1534         break;
1535     case 0x200:                                         /* WAND */
1536         wrd = (insn >> 12) & 0xf;
1537         rd0 = (insn >> 0) & 0xf;
1538         rd1 = (insn >> 16) & 0xf;
1539         gen_op_iwmmxt_movq_M0_wRn(rd0);
1540         gen_op_iwmmxt_andq_M0_wRn(rd1);
1541         gen_op_iwmmxt_setpsr_nz();
1542         gen_op_iwmmxt_movq_wRn_M0(wrd);
1543         gen_op_iwmmxt_set_mup();
1544         gen_op_iwmmxt_set_cup();
1545         break;
1546     case 0x810: case 0xa10:                             /* WMADD */
1547         wrd = (insn >> 12) & 0xf;
1548         rd0 = (insn >> 0) & 0xf;
1549         rd1 = (insn >> 16) & 0xf;
1550         gen_op_iwmmxt_movq_M0_wRn(rd0);
1551         if (insn & (1 << 21))
1552             gen_op_iwmmxt_maddsq_M0_wRn(rd1);
1553         else
1554             gen_op_iwmmxt_madduq_M0_wRn(rd1);
1555         gen_op_iwmmxt_movq_wRn_M0(wrd);
1556         gen_op_iwmmxt_set_mup();
1557         break;
1558     case 0x10e: case 0x50e: case 0x90e: case 0xd0e:     /* WUNPCKIL */
1559         wrd = (insn >> 12) & 0xf;
1560         rd0 = (insn >> 16) & 0xf;
1561         rd1 = (insn >> 0) & 0xf;
1562         gen_op_iwmmxt_movq_M0_wRn(rd0);
1563         switch ((insn >> 22) & 3) {
1564         case 0:
1565             gen_op_iwmmxt_unpacklb_M0_wRn(rd1);
1566             break;
1567         case 1:
1568             gen_op_iwmmxt_unpacklw_M0_wRn(rd1);
1569             break;
1570         case 2:
1571             gen_op_iwmmxt_unpackll_M0_wRn(rd1);
1572             break;
1573         case 3:
1574             return 1;
1575         }
1576         gen_op_iwmmxt_movq_wRn_M0(wrd);
1577         gen_op_iwmmxt_set_mup();
1578         gen_op_iwmmxt_set_cup();
1579         break;
1580     case 0x10c: case 0x50c: case 0x90c: case 0xd0c:     /* WUNPCKIH */
1581         wrd = (insn >> 12) & 0xf;
1582         rd0 = (insn >> 16) & 0xf;
1583         rd1 = (insn >> 0) & 0xf;
1584         gen_op_iwmmxt_movq_M0_wRn(rd0);
1585         switch ((insn >> 22) & 3) {
1586         case 0:
1587             gen_op_iwmmxt_unpackhb_M0_wRn(rd1);
1588             break;
1589         case 1:
1590             gen_op_iwmmxt_unpackhw_M0_wRn(rd1);
1591             break;
1592         case 2:
1593             gen_op_iwmmxt_unpackhl_M0_wRn(rd1);
1594             break;
1595         case 3:
1596             return 1;
1597         }
1598         gen_op_iwmmxt_movq_wRn_M0(wrd);
1599         gen_op_iwmmxt_set_mup();
1600         gen_op_iwmmxt_set_cup();
1601         break;
1602     case 0x012: case 0x112: case 0x412: case 0x512:     /* WSAD */
1603         wrd = (insn >> 12) & 0xf;
1604         rd0 = (insn >> 16) & 0xf;
1605         rd1 = (insn >> 0) & 0xf;
1606         gen_op_iwmmxt_movq_M0_wRn(rd0);
1607         if (insn & (1 << 22))
1608             gen_op_iwmmxt_sadw_M0_wRn(rd1);
1609         else
1610             gen_op_iwmmxt_sadb_M0_wRn(rd1);
1611         if (!(insn & (1 << 20)))
1612             gen_op_iwmmxt_addl_M0_wRn(wrd);
1613         gen_op_iwmmxt_movq_wRn_M0(wrd);
1614         gen_op_iwmmxt_set_mup();
1615         break;
1616     case 0x010: case 0x110: case 0x210: case 0x310:     /* WMUL */
1617         wrd = (insn >> 12) & 0xf;
1618         rd0 = (insn >> 16) & 0xf;
1619         rd1 = (insn >> 0) & 0xf;
1620         gen_op_iwmmxt_movq_M0_wRn(rd0);
1621         if (insn & (1 << 21)) {
1622             if (insn & (1 << 20))
1623                 gen_op_iwmmxt_mulshw_M0_wRn(rd1);
1624             else
1625                 gen_op_iwmmxt_mulslw_M0_wRn(rd1);
1626         } else {
1627             if (insn & (1 << 20))
1628                 gen_op_iwmmxt_muluhw_M0_wRn(rd1);
1629             else
1630                 gen_op_iwmmxt_mululw_M0_wRn(rd1);
1631         }
1632         gen_op_iwmmxt_movq_wRn_M0(wrd);
1633         gen_op_iwmmxt_set_mup();
1634         break;
1635     case 0x410: case 0x510: case 0x610: case 0x710:     /* WMAC */
1636         wrd = (insn >> 12) & 0xf;
1637         rd0 = (insn >> 16) & 0xf;
1638         rd1 = (insn >> 0) & 0xf;
1639         gen_op_iwmmxt_movq_M0_wRn(rd0);
1640         if (insn & (1 << 21))
1641             gen_op_iwmmxt_macsw_M0_wRn(rd1);
1642         else
1643             gen_op_iwmmxt_macuw_M0_wRn(rd1);
1644         if (!(insn & (1 << 20))) {
1645             iwmmxt_load_reg(cpu_V1, wrd);
1646             tcg_gen_add_i64(cpu_M0, cpu_M0, cpu_V1);
1647         }
1648         gen_op_iwmmxt_movq_wRn_M0(wrd);
1649         gen_op_iwmmxt_set_mup();
1650         break;
1651     case 0x006: case 0x406: case 0x806: case 0xc06:     /* WCMPEQ */
1652         wrd = (insn >> 12) & 0xf;
1653         rd0 = (insn >> 16) & 0xf;
1654         rd1 = (insn >> 0) & 0xf;
1655         gen_op_iwmmxt_movq_M0_wRn(rd0);
1656         switch ((insn >> 22) & 3) {
1657         case 0:
1658             gen_op_iwmmxt_cmpeqb_M0_wRn(rd1);
1659             break;
1660         case 1:
1661             gen_op_iwmmxt_cmpeqw_M0_wRn(rd1);
1662             break;
1663         case 2:
1664             gen_op_iwmmxt_cmpeql_M0_wRn(rd1);
1665             break;
1666         case 3:
1667             return 1;
1668         }
1669         gen_op_iwmmxt_movq_wRn_M0(wrd);
1670         gen_op_iwmmxt_set_mup();
1671         gen_op_iwmmxt_set_cup();
1672         break;
1673     case 0x800: case 0x900: case 0xc00: case 0xd00:     /* WAVG2 */
1674         wrd = (insn >> 12) & 0xf;
1675         rd0 = (insn >> 16) & 0xf;
1676         rd1 = (insn >> 0) & 0xf;
1677         gen_op_iwmmxt_movq_M0_wRn(rd0);
1678         if (insn & (1 << 22)) {
1679             if (insn & (1 << 20))
1680                 gen_op_iwmmxt_avgw1_M0_wRn(rd1);
1681             else
1682                 gen_op_iwmmxt_avgw0_M0_wRn(rd1);
1683         } else {
1684             if (insn & (1 << 20))
1685                 gen_op_iwmmxt_avgb1_M0_wRn(rd1);
1686             else
1687                 gen_op_iwmmxt_avgb0_M0_wRn(rd1);
1688         }
1689         gen_op_iwmmxt_movq_wRn_M0(wrd);
1690         gen_op_iwmmxt_set_mup();
1691         gen_op_iwmmxt_set_cup();
1692         break;
1693     case 0x802: case 0x902: case 0xa02: case 0xb02:     /* WALIGNR */
1694         wrd = (insn >> 12) & 0xf;
1695         rd0 = (insn >> 16) & 0xf;
1696         rd1 = (insn >> 0) & 0xf;
1697         gen_op_iwmmxt_movq_M0_wRn(rd0);
1698         tmp = iwmmxt_load_creg(ARM_IWMMXT_wCGR0 + ((insn >> 20) & 3));
1699         tcg_gen_andi_i32(tmp, tmp, 7);
1700         iwmmxt_load_reg(cpu_V1, rd1);
1701         gen_helper_iwmmxt_align(cpu_M0, cpu_M0, cpu_V1, tmp);
1702         tcg_temp_free_i32(tmp);
1703         gen_op_iwmmxt_movq_wRn_M0(wrd);
1704         gen_op_iwmmxt_set_mup();
1705         break;
1706     case 0x601: case 0x605: case 0x609: case 0x60d:     /* TINSR */
1707         if (((insn >> 6) & 3) == 3)
1708             return 1;
1709         rd = (insn >> 12) & 0xf;
1710         wrd = (insn >> 16) & 0xf;
1711         tmp = load_reg(s, rd);
1712         gen_op_iwmmxt_movq_M0_wRn(wrd);
1713         switch ((insn >> 6) & 3) {
1714         case 0:
1715             tmp2 = tcg_const_i32(0xff);
1716             tmp3 = tcg_const_i32((insn & 7) << 3);
1717             break;
1718         case 1:
1719             tmp2 = tcg_const_i32(0xffff);
1720             tmp3 = tcg_const_i32((insn & 3) << 4);
1721             break;
1722         case 2:
1723             tmp2 = tcg_const_i32(0xffffffff);
1724             tmp3 = tcg_const_i32((insn & 1) << 5);
1725             break;
1726         default:
1727             TCGV_UNUSED(tmp2);
1728             TCGV_UNUSED(tmp3);
1729         }
1730         gen_helper_iwmmxt_insr(cpu_M0, cpu_M0, tmp, tmp2, tmp3);
1731         tcg_temp_free(tmp3);
1732         tcg_temp_free(tmp2);
1733         tcg_temp_free_i32(tmp);
1734         gen_op_iwmmxt_movq_wRn_M0(wrd);
1735         gen_op_iwmmxt_set_mup();
1736         break;
1737     case 0x107: case 0x507: case 0x907: case 0xd07:     /* TEXTRM */
1738         rd = (insn >> 12) & 0xf;
1739         wrd = (insn >> 16) & 0xf;
1740         if (rd == 15 || ((insn >> 22) & 3) == 3)
1741             return 1;
1742         gen_op_iwmmxt_movq_M0_wRn(wrd);
1743         tmp = tcg_temp_new_i32();
1744         switch ((insn >> 22) & 3) {
1745         case 0:
1746             tcg_gen_shri_i64(cpu_M0, cpu_M0, (insn & 7) << 3);
1747             tcg_gen_trunc_i64_i32(tmp, cpu_M0);
1748             if (insn & 8) {
1749                 tcg_gen_ext8s_i32(tmp, tmp);
1750             } else {
1751                 tcg_gen_andi_i32(tmp, tmp, 0xff);
1752             }
1753             break;
1754         case 1:
1755             tcg_gen_shri_i64(cpu_M0, cpu_M0, (insn & 3) << 4);
1756             tcg_gen_trunc_i64_i32(tmp, cpu_M0);
1757             if (insn & 8) {
1758                 tcg_gen_ext16s_i32(tmp, tmp);
1759             } else {
1760                 tcg_gen_andi_i32(tmp, tmp, 0xffff);
1761             }
1762             break;
1763         case 2:
1764             tcg_gen_shri_i64(cpu_M0, cpu_M0, (insn & 1) << 5);
1765             tcg_gen_trunc_i64_i32(tmp, cpu_M0);
1766             break;
1767         }
1768         store_reg(s, rd, tmp);
1769         break;
1770     case 0x117: case 0x517: case 0x917: case 0xd17:     /* TEXTRC */
1771         if ((insn & 0x000ff008) != 0x0003f000 || ((insn >> 22) & 3) == 3)
1772             return 1;
1773         tmp = iwmmxt_load_creg(ARM_IWMMXT_wCASF);
1774         switch ((insn >> 22) & 3) {
1775         case 0:
1776             tcg_gen_shri_i32(tmp, tmp, ((insn & 7) << 2) + 0);
1777             break;
1778         case 1:
1779             tcg_gen_shri_i32(tmp, tmp, ((insn & 3) << 3) + 4);
1780             break;
1781         case 2:
1782             tcg_gen_shri_i32(tmp, tmp, ((insn & 1) << 4) + 12);
1783             break;
1784         }
1785         tcg_gen_shli_i32(tmp, tmp, 28);
1786         gen_set_nzcv(tmp);
1787         tcg_temp_free_i32(tmp);
1788         break;
1789     case 0x401: case 0x405: case 0x409: case 0x40d:     /* TBCST */
1790         if (((insn >> 6) & 3) == 3)
1791             return 1;
1792         rd = (insn >> 12) & 0xf;
1793         wrd = (insn >> 16) & 0xf;
1794         tmp = load_reg(s, rd);
1795         switch ((insn >> 6) & 3) {
1796         case 0:
1797             gen_helper_iwmmxt_bcstb(cpu_M0, tmp);
1798             break;
1799         case 1:
1800             gen_helper_iwmmxt_bcstw(cpu_M0, tmp);
1801             break;
1802         case 2:
1803             gen_helper_iwmmxt_bcstl(cpu_M0, tmp);
1804             break;
1805         }
1806         tcg_temp_free_i32(tmp);
1807         gen_op_iwmmxt_movq_wRn_M0(wrd);
1808         gen_op_iwmmxt_set_mup();
1809         break;
1810     case 0x113: case 0x513: case 0x913: case 0xd13:     /* TANDC */
1811         if ((insn & 0x000ff00f) != 0x0003f000 || ((insn >> 22) & 3) == 3)
1812             return 1;
1813         tmp = iwmmxt_load_creg(ARM_IWMMXT_wCASF);
1814         tmp2 = tcg_temp_new_i32();
1815         tcg_gen_mov_i32(tmp2, tmp);
1816         switch ((insn >> 22) & 3) {
1817         case 0:
1818             for (i = 0; i < 7; i ++) {
1819                 tcg_gen_shli_i32(tmp2, tmp2, 4);
1820                 tcg_gen_and_i32(tmp, tmp, tmp2);
1821             }
1822             break;
1823         case 1:
1824             for (i = 0; i < 3; i ++) {
1825                 tcg_gen_shli_i32(tmp2, tmp2, 8);
1826                 tcg_gen_and_i32(tmp, tmp, tmp2);
1827             }
1828             break;
1829         case 2:
1830             tcg_gen_shli_i32(tmp2, tmp2, 16);
1831             tcg_gen_and_i32(tmp, tmp, tmp2);
1832             break;
1833         }
1834         gen_set_nzcv(tmp);
1835         tcg_temp_free_i32(tmp2);
1836         tcg_temp_free_i32(tmp);
1837         break;
1838     case 0x01c: case 0x41c: case 0x81c: case 0xc1c:     /* WACC */
1839         wrd = (insn >> 12) & 0xf;
1840         rd0 = (insn >> 16) & 0xf;
1841         gen_op_iwmmxt_movq_M0_wRn(rd0);
1842         switch ((insn >> 22) & 3) {
1843         case 0:
1844             gen_helper_iwmmxt_addcb(cpu_M0, cpu_M0);
1845             break;
1846         case 1:
1847             gen_helper_iwmmxt_addcw(cpu_M0, cpu_M0);
1848             break;
1849         case 2:
1850             gen_helper_iwmmxt_addcl(cpu_M0, cpu_M0);
1851             break;
1852         case 3:
1853             return 1;
1854         }
1855         gen_op_iwmmxt_movq_wRn_M0(wrd);
1856         gen_op_iwmmxt_set_mup();
1857         break;
1858     case 0x115: case 0x515: case 0x915: case 0xd15:     /* TORC */
1859         if ((insn & 0x000ff00f) != 0x0003f000 || ((insn >> 22) & 3) == 3)
1860             return 1;
1861         tmp = iwmmxt_load_creg(ARM_IWMMXT_wCASF);
1862         tmp2 = tcg_temp_new_i32();
1863         tcg_gen_mov_i32(tmp2, tmp);
1864         switch ((insn >> 22) & 3) {
1865         case 0:
1866             for (i = 0; i < 7; i ++) {
1867                 tcg_gen_shli_i32(tmp2, tmp2, 4);
1868                 tcg_gen_or_i32(tmp, tmp, tmp2);
1869             }
1870             break;
1871         case 1:
1872             for (i = 0; i < 3; i ++) {
1873                 tcg_gen_shli_i32(tmp2, tmp2, 8);
1874                 tcg_gen_or_i32(tmp, tmp, tmp2);
1875             }
1876             break;
1877         case 2:
1878             tcg_gen_shli_i32(tmp2, tmp2, 16);
1879             tcg_gen_or_i32(tmp, tmp, tmp2);
1880             break;
1881         }
1882         gen_set_nzcv(tmp);
1883         tcg_temp_free_i32(tmp2);
1884         tcg_temp_free_i32(tmp);
1885         break;
1886     case 0x103: case 0x503: case 0x903: case 0xd03:     /* TMOVMSK */
1887         rd = (insn >> 12) & 0xf;
1888         rd0 = (insn >> 16) & 0xf;
1889         if ((insn & 0xf) != 0 || ((insn >> 22) & 3) == 3)
1890             return 1;
1891         gen_op_iwmmxt_movq_M0_wRn(rd0);
1892         tmp = tcg_temp_new_i32();
1893         switch ((insn >> 22) & 3) {
1894         case 0:
1895             gen_helper_iwmmxt_msbb(tmp, cpu_M0);
1896             break;
1897         case 1:
1898             gen_helper_iwmmxt_msbw(tmp, cpu_M0);
1899             break;
1900         case 2:
1901             gen_helper_iwmmxt_msbl(tmp, cpu_M0);
1902             break;
1903         }
1904         store_reg(s, rd, tmp);
1905         break;
1906     case 0x106: case 0x306: case 0x506: case 0x706:     /* WCMPGT */
1907     case 0x906: case 0xb06: case 0xd06: case 0xf06:
1908         wrd = (insn >> 12) & 0xf;
1909         rd0 = (insn >> 16) & 0xf;
1910         rd1 = (insn >> 0) & 0xf;
1911         gen_op_iwmmxt_movq_M0_wRn(rd0);
1912         switch ((insn >> 22) & 3) {
1913         case 0:
1914             if (insn & (1 << 21))
1915                 gen_op_iwmmxt_cmpgtsb_M0_wRn(rd1);
1916             else
1917                 gen_op_iwmmxt_cmpgtub_M0_wRn(rd1);
1918             break;
1919         case 1:
1920             if (insn & (1 << 21))
1921                 gen_op_iwmmxt_cmpgtsw_M0_wRn(rd1);
1922             else
1923                 gen_op_iwmmxt_cmpgtuw_M0_wRn(rd1);
1924             break;
1925         case 2:
1926             if (insn & (1 << 21))
1927                 gen_op_iwmmxt_cmpgtsl_M0_wRn(rd1);
1928             else
1929                 gen_op_iwmmxt_cmpgtul_M0_wRn(rd1);
1930             break;
1931         case 3:
1932             return 1;
1933         }
1934         gen_op_iwmmxt_movq_wRn_M0(wrd);
1935         gen_op_iwmmxt_set_mup();
1936         gen_op_iwmmxt_set_cup();
1937         break;
1938     case 0x00e: case 0x20e: case 0x40e: case 0x60e:     /* WUNPCKEL */
1939     case 0x80e: case 0xa0e: case 0xc0e: case 0xe0e:
1940         wrd = (insn >> 12) & 0xf;
1941         rd0 = (insn >> 16) & 0xf;
1942         gen_op_iwmmxt_movq_M0_wRn(rd0);
1943         switch ((insn >> 22) & 3) {
1944         case 0:
1945             if (insn & (1 << 21))
1946                 gen_op_iwmmxt_unpacklsb_M0();
1947             else
1948                 gen_op_iwmmxt_unpacklub_M0();
1949             break;
1950         case 1:
1951             if (insn & (1 << 21))
1952                 gen_op_iwmmxt_unpacklsw_M0();
1953             else
1954                 gen_op_iwmmxt_unpackluw_M0();
1955             break;
1956         case 2:
1957             if (insn & (1 << 21))
1958                 gen_op_iwmmxt_unpacklsl_M0();
1959             else
1960                 gen_op_iwmmxt_unpacklul_M0();
1961             break;
1962         case 3:
1963             return 1;
1964         }
1965         gen_op_iwmmxt_movq_wRn_M0(wrd);
1966         gen_op_iwmmxt_set_mup();
1967         gen_op_iwmmxt_set_cup();
1968         break;
1969     case 0x00c: case 0x20c: case 0x40c: case 0x60c:     /* WUNPCKEH */
1970     case 0x80c: case 0xa0c: case 0xc0c: case 0xe0c:
1971         wrd = (insn >> 12) & 0xf;
1972         rd0 = (insn >> 16) & 0xf;
1973         gen_op_iwmmxt_movq_M0_wRn(rd0);
1974         switch ((insn >> 22) & 3) {
1975         case 0:
1976             if (insn & (1 << 21))
1977                 gen_op_iwmmxt_unpackhsb_M0();
1978             else
1979                 gen_op_iwmmxt_unpackhub_M0();
1980             break;
1981         case 1:
1982             if (insn & (1 << 21))
1983                 gen_op_iwmmxt_unpackhsw_M0();
1984             else
1985                 gen_op_iwmmxt_unpackhuw_M0();
1986             break;
1987         case 2:
1988             if (insn & (1 << 21))
1989                 gen_op_iwmmxt_unpackhsl_M0();
1990             else
1991                 gen_op_iwmmxt_unpackhul_M0();
1992             break;
1993         case 3:
1994             return 1;
1995         }
1996         gen_op_iwmmxt_movq_wRn_M0(wrd);
1997         gen_op_iwmmxt_set_mup();
1998         gen_op_iwmmxt_set_cup();
1999         break;
2000     case 0x204: case 0x604: case 0xa04: case 0xe04:     /* WSRL */
2001     case 0x214: case 0x614: case 0xa14: case 0xe14:
2002         if (((insn >> 22) & 3) == 0)
2003             return 1;
2004         wrd = (insn >> 12) & 0xf;
2005         rd0 = (insn >> 16) & 0xf;
2006         gen_op_iwmmxt_movq_M0_wRn(rd0);
2007         tmp = tcg_temp_new_i32();
2008         if (gen_iwmmxt_shift(insn, 0xff, tmp)) {
2009             tcg_temp_free_i32(tmp);
2010             return 1;
2011         }
2012         switch ((insn >> 22) & 3) {
2013         case 1:
2014             gen_helper_iwmmxt_srlw(cpu_M0, cpu_env, cpu_M0, tmp);
2015             break;
2016         case 2:
2017             gen_helper_iwmmxt_srll(cpu_M0, cpu_env, cpu_M0, tmp);
2018             break;
2019         case 3:
2020             gen_helper_iwmmxt_srlq(cpu_M0, cpu_env, cpu_M0, tmp);
2021             break;
2022         }
2023         tcg_temp_free_i32(tmp);
2024         gen_op_iwmmxt_movq_wRn_M0(wrd);
2025         gen_op_iwmmxt_set_mup();
2026         gen_op_iwmmxt_set_cup();
2027         break;
2028     case 0x004: case 0x404: case 0x804: case 0xc04:     /* WSRA */
2029     case 0x014: case 0x414: case 0x814: case 0xc14:
2030         if (((insn >> 22) & 3) == 0)
2031             return 1;
2032         wrd = (insn >> 12) & 0xf;
2033         rd0 = (insn >> 16) & 0xf;
2034         gen_op_iwmmxt_movq_M0_wRn(rd0);
2035         tmp = tcg_temp_new_i32();
2036         if (gen_iwmmxt_shift(insn, 0xff, tmp)) {
2037             tcg_temp_free_i32(tmp);
2038             return 1;
2039         }
2040         switch ((insn >> 22) & 3) {
2041         case 1:
2042             gen_helper_iwmmxt_sraw(cpu_M0, cpu_env, cpu_M0, tmp);
2043             break;
2044         case 2:
2045             gen_helper_iwmmxt_sral(cpu_M0, cpu_env, cpu_M0, tmp);
2046             break;
2047         case 3:
2048             gen_helper_iwmmxt_sraq(cpu_M0, cpu_env, cpu_M0, tmp);
2049             break;
2050         }
2051         tcg_temp_free_i32(tmp);
2052         gen_op_iwmmxt_movq_wRn_M0(wrd);
2053         gen_op_iwmmxt_set_mup();
2054         gen_op_iwmmxt_set_cup();
2055         break;
2056     case 0x104: case 0x504: case 0x904: case 0xd04:     /* WSLL */
2057     case 0x114: case 0x514: case 0x914: case 0xd14:
2058         if (((insn >> 22) & 3) == 0)
2059             return 1;
2060         wrd = (insn >> 12) & 0xf;
2061         rd0 = (insn >> 16) & 0xf;
2062         gen_op_iwmmxt_movq_M0_wRn(rd0);
2063         tmp = tcg_temp_new_i32();
2064         if (gen_iwmmxt_shift(insn, 0xff, tmp)) {
2065             tcg_temp_free_i32(tmp);
2066             return 1;
2067         }
2068         switch ((insn >> 22) & 3) {
2069         case 1:
2070             gen_helper_iwmmxt_sllw(cpu_M0, cpu_env, cpu_M0, tmp);
2071             break;
2072         case 2:
2073             gen_helper_iwmmxt_slll(cpu_M0, cpu_env, cpu_M0, tmp);
2074             break;
2075         case 3:
2076             gen_helper_iwmmxt_sllq(cpu_M0, cpu_env, cpu_M0, tmp);
2077             break;
2078         }
2079         tcg_temp_free_i32(tmp);
2080         gen_op_iwmmxt_movq_wRn_M0(wrd);
2081         gen_op_iwmmxt_set_mup();
2082         gen_op_iwmmxt_set_cup();
2083         break;
2084     case 0x304: case 0x704: case 0xb04: case 0xf04:     /* WROR */
2085     case 0x314: case 0x714: case 0xb14: case 0xf14:
2086         if (((insn >> 22) & 3) == 0)
2087             return 1;
2088         wrd = (insn >> 12) & 0xf;
2089         rd0 = (insn >> 16) & 0xf;
2090         gen_op_iwmmxt_movq_M0_wRn(rd0);
2091         tmp = tcg_temp_new_i32();
2092         switch ((insn >> 22) & 3) {
2093         case 1:
2094             if (gen_iwmmxt_shift(insn, 0xf, tmp)) {
2095                 tcg_temp_free_i32(tmp);
2096                 return 1;
2097             }
2098             gen_helper_iwmmxt_rorw(cpu_M0, cpu_env, cpu_M0, tmp);
2099             break;
2100         case 2:
2101             if (gen_iwmmxt_shift(insn, 0x1f, tmp)) {
2102                 tcg_temp_free_i32(tmp);
2103                 return 1;
2104             }
2105             gen_helper_iwmmxt_rorl(cpu_M0, cpu_env, cpu_M0, tmp);
2106             break;
2107         case 3:
2108             if (gen_iwmmxt_shift(insn, 0x3f, tmp)) {
2109                 tcg_temp_free_i32(tmp);
2110                 return 1;
2111             }
2112             gen_helper_iwmmxt_rorq(cpu_M0, cpu_env, cpu_M0, tmp);
2113             break;
2114         }
2115         tcg_temp_free_i32(tmp);
2116         gen_op_iwmmxt_movq_wRn_M0(wrd);
2117         gen_op_iwmmxt_set_mup();
2118         gen_op_iwmmxt_set_cup();
2119         break;
2120     case 0x116: case 0x316: case 0x516: case 0x716:     /* WMIN */
2121     case 0x916: case 0xb16: case 0xd16: case 0xf16:
2122         wrd = (insn >> 12) & 0xf;
2123         rd0 = (insn >> 16) & 0xf;
2124         rd1 = (insn >> 0) & 0xf;
2125         gen_op_iwmmxt_movq_M0_wRn(rd0);
2126         switch ((insn >> 22) & 3) {
2127         case 0:
2128             if (insn & (1 << 21))
2129                 gen_op_iwmmxt_minsb_M0_wRn(rd1);
2130             else
2131                 gen_op_iwmmxt_minub_M0_wRn(rd1);
2132             break;
2133         case 1:
2134             if (insn & (1 << 21))
2135                 gen_op_iwmmxt_minsw_M0_wRn(rd1);
2136             else
2137                 gen_op_iwmmxt_minuw_M0_wRn(rd1);
2138             break;
2139         case 2:
2140             if (insn & (1 << 21))
2141                 gen_op_iwmmxt_minsl_M0_wRn(rd1);
2142             else
2143                 gen_op_iwmmxt_minul_M0_wRn(rd1);
2144             break;
2145         case 3:
2146             return 1;
2147         }
2148         gen_op_iwmmxt_movq_wRn_M0(wrd);
2149         gen_op_iwmmxt_set_mup();
2150         break;
2151     case 0x016: case 0x216: case 0x416: case 0x616:     /* WMAX */
2152     case 0x816: case 0xa16: case 0xc16: case 0xe16:
2153         wrd = (insn >> 12) & 0xf;
2154         rd0 = (insn >> 16) & 0xf;
2155         rd1 = (insn >> 0) & 0xf;
2156         gen_op_iwmmxt_movq_M0_wRn(rd0);
2157         switch ((insn >> 22) & 3) {
2158         case 0:
2159             if (insn & (1 << 21))
2160                 gen_op_iwmmxt_maxsb_M0_wRn(rd1);
2161             else
2162                 gen_op_iwmmxt_maxub_M0_wRn(rd1);
2163             break;
2164         case 1:
2165             if (insn & (1 << 21))
2166                 gen_op_iwmmxt_maxsw_M0_wRn(rd1);
2167             else
2168                 gen_op_iwmmxt_maxuw_M0_wRn(rd1);
2169             break;
2170         case 2:
2171             if (insn & (1 << 21))
2172                 gen_op_iwmmxt_maxsl_M0_wRn(rd1);
2173             else
2174                 gen_op_iwmmxt_maxul_M0_wRn(rd1);
2175             break;
2176         case 3:
2177             return 1;
2178         }
2179         gen_op_iwmmxt_movq_wRn_M0(wrd);
2180         gen_op_iwmmxt_set_mup();
2181         break;
2182     case 0x002: case 0x102: case 0x202: case 0x302:     /* WALIGNI */
2183     case 0x402: case 0x502: case 0x602: case 0x702:
2184         wrd = (insn >> 12) & 0xf;
2185         rd0 = (insn >> 16) & 0xf;
2186         rd1 = (insn >> 0) & 0xf;
2187         gen_op_iwmmxt_movq_M0_wRn(rd0);
2188         tmp = tcg_const_i32((insn >> 20) & 3);
2189         iwmmxt_load_reg(cpu_V1, rd1);
2190         gen_helper_iwmmxt_align(cpu_M0, cpu_M0, cpu_V1, tmp);
2191         tcg_temp_free(tmp);
2192         gen_op_iwmmxt_movq_wRn_M0(wrd);
2193         gen_op_iwmmxt_set_mup();
2194         break;
2195     case 0x01a: case 0x11a: case 0x21a: case 0x31a:     /* WSUB */
2196     case 0x41a: case 0x51a: case 0x61a: case 0x71a:
2197     case 0x81a: case 0x91a: case 0xa1a: case 0xb1a:
2198     case 0xc1a: case 0xd1a: case 0xe1a: case 0xf1a:
2199         wrd = (insn >> 12) & 0xf;
2200         rd0 = (insn >> 16) & 0xf;
2201         rd1 = (insn >> 0) & 0xf;
2202         gen_op_iwmmxt_movq_M0_wRn(rd0);
2203         switch ((insn >> 20) & 0xf) {
2204         case 0x0:
2205             gen_op_iwmmxt_subnb_M0_wRn(rd1);
2206             break;
2207         case 0x1:
2208             gen_op_iwmmxt_subub_M0_wRn(rd1);
2209             break;
2210         case 0x3:
2211             gen_op_iwmmxt_subsb_M0_wRn(rd1);
2212             break;
2213         case 0x4:
2214             gen_op_iwmmxt_subnw_M0_wRn(rd1);
2215             break;
2216         case 0x5:
2217             gen_op_iwmmxt_subuw_M0_wRn(rd1);
2218             break;
2219         case 0x7:
2220             gen_op_iwmmxt_subsw_M0_wRn(rd1);
2221             break;
2222         case 0x8:
2223             gen_op_iwmmxt_subnl_M0_wRn(rd1);
2224             break;
2225         case 0x9:
2226             gen_op_iwmmxt_subul_M0_wRn(rd1);
2227             break;
2228         case 0xb:
2229             gen_op_iwmmxt_subsl_M0_wRn(rd1);
2230             break;
2231         default:
2232             return 1;
2233         }
2234         gen_op_iwmmxt_movq_wRn_M0(wrd);
2235         gen_op_iwmmxt_set_mup();
2236         gen_op_iwmmxt_set_cup();
2237         break;
2238     case 0x01e: case 0x11e: case 0x21e: case 0x31e:     /* WSHUFH */
2239     case 0x41e: case 0x51e: case 0x61e: case 0x71e:
2240     case 0x81e: case 0x91e: case 0xa1e: case 0xb1e:
2241     case 0xc1e: case 0xd1e: case 0xe1e: case 0xf1e:
2242         wrd = (insn >> 12) & 0xf;
2243         rd0 = (insn >> 16) & 0xf;
2244         gen_op_iwmmxt_movq_M0_wRn(rd0);
2245         tmp = tcg_const_i32(((insn >> 16) & 0xf0) | (insn & 0x0f));
2246         gen_helper_iwmmxt_shufh(cpu_M0, cpu_env, cpu_M0, tmp);
2247         tcg_temp_free(tmp);
2248         gen_op_iwmmxt_movq_wRn_M0(wrd);
2249         gen_op_iwmmxt_set_mup();
2250         gen_op_iwmmxt_set_cup();
2251         break;
2252     case 0x018: case 0x118: case 0x218: case 0x318:     /* WADD */
2253     case 0x418: case 0x518: case 0x618: case 0x718:
2254     case 0x818: case 0x918: case 0xa18: case 0xb18:
2255     case 0xc18: case 0xd18: case 0xe18: case 0xf18:
2256         wrd = (insn >> 12) & 0xf;
2257         rd0 = (insn >> 16) & 0xf;
2258         rd1 = (insn >> 0) & 0xf;
2259         gen_op_iwmmxt_movq_M0_wRn(rd0);
2260         switch ((insn >> 20) & 0xf) {
2261         case 0x0:
2262             gen_op_iwmmxt_addnb_M0_wRn(rd1);
2263             break;
2264         case 0x1:
2265             gen_op_iwmmxt_addub_M0_wRn(rd1);
2266             break;
2267         case 0x3:
2268             gen_op_iwmmxt_addsb_M0_wRn(rd1);
2269             break;
2270         case 0x4:
2271             gen_op_iwmmxt_addnw_M0_wRn(rd1);
2272             break;
2273         case 0x5:
2274             gen_op_iwmmxt_adduw_M0_wRn(rd1);
2275             break;
2276         case 0x7:
2277             gen_op_iwmmxt_addsw_M0_wRn(rd1);
2278             break;
2279         case 0x8:
2280             gen_op_iwmmxt_addnl_M0_wRn(rd1);
2281             break;
2282         case 0x9:
2283             gen_op_iwmmxt_addul_M0_wRn(rd1);
2284             break;
2285         case 0xb:
2286             gen_op_iwmmxt_addsl_M0_wRn(rd1);
2287             break;
2288         default:
2289             return 1;
2290         }
2291         gen_op_iwmmxt_movq_wRn_M0(wrd);
2292         gen_op_iwmmxt_set_mup();
2293         gen_op_iwmmxt_set_cup();
2294         break;
2295     case 0x008: case 0x108: case 0x208: case 0x308:     /* WPACK */
2296     case 0x408: case 0x508: case 0x608: case 0x708:
2297     case 0x808: case 0x908: case 0xa08: case 0xb08:
2298     case 0xc08: case 0xd08: case 0xe08: case 0xf08:
2299         if (!(insn & (1 << 20)) || ((insn >> 22) & 3) == 0)
2300             return 1;
2301         wrd = (insn >> 12) & 0xf;
2302         rd0 = (insn >> 16) & 0xf;
2303         rd1 = (insn >> 0) & 0xf;
2304         gen_op_iwmmxt_movq_M0_wRn(rd0);
2305         switch ((insn >> 22) & 3) {
2306         case 1:
2307             if (insn & (1 << 21))
2308                 gen_op_iwmmxt_packsw_M0_wRn(rd1);
2309             else
2310                 gen_op_iwmmxt_packuw_M0_wRn(rd1);
2311             break;
2312         case 2:
2313             if (insn & (1 << 21))
2314                 gen_op_iwmmxt_packsl_M0_wRn(rd1);
2315             else
2316                 gen_op_iwmmxt_packul_M0_wRn(rd1);
2317             break;
2318         case 3:
2319             if (insn & (1 << 21))
2320                 gen_op_iwmmxt_packsq_M0_wRn(rd1);
2321             else
2322                 gen_op_iwmmxt_packuq_M0_wRn(rd1);
2323             break;
2324         }
2325         gen_op_iwmmxt_movq_wRn_M0(wrd);
2326         gen_op_iwmmxt_set_mup();
2327         gen_op_iwmmxt_set_cup();
2328         break;
2329     case 0x201: case 0x203: case 0x205: case 0x207:
2330     case 0x209: case 0x20b: case 0x20d: case 0x20f:
2331     case 0x211: case 0x213: case 0x215: case 0x217:
2332     case 0x219: case 0x21b: case 0x21d: case 0x21f:
2333         wrd = (insn >> 5) & 0xf;
2334         rd0 = (insn >> 12) & 0xf;
2335         rd1 = (insn >> 0) & 0xf;
2336         if (rd0 == 0xf || rd1 == 0xf)
2337             return 1;
2338         gen_op_iwmmxt_movq_M0_wRn(wrd);
2339         tmp = load_reg(s, rd0);
2340         tmp2 = load_reg(s, rd1);
2341         switch ((insn >> 16) & 0xf) {
2342         case 0x0:                                       /* TMIA */
2343             gen_helper_iwmmxt_muladdsl(cpu_M0, cpu_M0, tmp, tmp2);
2344             break;
2345         case 0x8:                                       /* TMIAPH */
2346             gen_helper_iwmmxt_muladdsw(cpu_M0, cpu_M0, tmp, tmp2);
2347             break;
2348         case 0xc: case 0xd: case 0xe: case 0xf:         /* TMIAxy */
2349             if (insn & (1 << 16))
2350                 tcg_gen_shri_i32(tmp, tmp, 16);
2351             if (insn & (1 << 17))
2352                 tcg_gen_shri_i32(tmp2, tmp2, 16);
2353             gen_helper_iwmmxt_muladdswl(cpu_M0, cpu_M0, tmp, tmp2);
2354             break;
2355         default:
2356             tcg_temp_free_i32(tmp2);
2357             tcg_temp_free_i32(tmp);
2358             return 1;
2359         }
2360         tcg_temp_free_i32(tmp2);
2361         tcg_temp_free_i32(tmp);
2362         gen_op_iwmmxt_movq_wRn_M0(wrd);
2363         gen_op_iwmmxt_set_mup();
2364         break;
2365     default:
2366         return 1;
2367     }
2368
2369     return 0;
2370 }
2371
2372 /* Disassemble an XScale DSP instruction.  Returns nonzero if an error occurred
2373    (ie. an undefined instruction).  */
2374 static int disas_dsp_insn(CPUARMState *env, DisasContext *s, uint32_t insn)
2375 {
2376     int acc, rd0, rd1, rdhi, rdlo;
2377     TCGv tmp, tmp2;
2378
2379     if ((insn & 0x0ff00f10) == 0x0e200010) {
2380         /* Multiply with Internal Accumulate Format */
2381         rd0 = (insn >> 12) & 0xf;
2382         rd1 = insn & 0xf;
2383         acc = (insn >> 5) & 7;
2384
2385         if (acc != 0)
2386             return 1;
2387
2388         tmp = load_reg(s, rd0);
2389         tmp2 = load_reg(s, rd1);
2390         switch ((insn >> 16) & 0xf) {
2391         case 0x0:                                       /* MIA */
2392             gen_helper_iwmmxt_muladdsl(cpu_M0, cpu_M0, tmp, tmp2);
2393             break;
2394         case 0x8:                                       /* MIAPH */
2395             gen_helper_iwmmxt_muladdsw(cpu_M0, cpu_M0, tmp, tmp2);
2396             break;
2397         case 0xc:                                       /* MIABB */
2398         case 0xd:                                       /* MIABT */
2399         case 0xe:                                       /* MIATB */
2400         case 0xf:                                       /* MIATT */
2401             if (insn & (1 << 16))
2402                 tcg_gen_shri_i32(tmp, tmp, 16);
2403             if (insn & (1 << 17))
2404                 tcg_gen_shri_i32(tmp2, tmp2, 16);
2405             gen_helper_iwmmxt_muladdswl(cpu_M0, cpu_M0, tmp, tmp2);
2406             break;
2407         default:
2408             return 1;
2409         }
2410         tcg_temp_free_i32(tmp2);
2411         tcg_temp_free_i32(tmp);
2412
2413         gen_op_iwmmxt_movq_wRn_M0(acc);
2414         return 0;
2415     }
2416
2417     if ((insn & 0x0fe00ff8) == 0x0c400000) {
2418         /* Internal Accumulator Access Format */
2419         rdhi = (insn >> 16) & 0xf;
2420         rdlo = (insn >> 12) & 0xf;
2421         acc = insn & 7;
2422
2423         if (acc != 0)
2424             return 1;
2425
2426         if (insn & ARM_CP_RW_BIT) {                     /* MRA */
2427             iwmmxt_load_reg(cpu_V0, acc);
2428             tcg_gen_trunc_i64_i32(cpu_R[rdlo], cpu_V0);
2429             tcg_gen_shri_i64(cpu_V0, cpu_V0, 32);
2430             tcg_gen_trunc_i64_i32(cpu_R[rdhi], cpu_V0);
2431             tcg_gen_andi_i32(cpu_R[rdhi], cpu_R[rdhi], (1 << (40 - 32)) - 1);
2432         } else {                                        /* MAR */
2433             tcg_gen_concat_i32_i64(cpu_V0, cpu_R[rdlo], cpu_R[rdhi]);
2434             iwmmxt_store_reg(cpu_V0, acc);
2435         }
2436         return 0;
2437     }
2438
2439     return 1;
2440 }
2441
2442 static int cp15_user_ok(CPUARMState *env, uint32_t insn)
2443 {
2444     int cpn = (insn >> 16) & 0xf;
2445     int cpm = insn & 0xf;
2446     int op = ((insn >> 5) & 7) | ((insn >> 18) & 0x38);
2447
2448     if (arm_feature(env, ARM_FEATURE_V7) && cpn == 9) {
2449         /* Performance monitor registers fall into three categories:
2450          *  (a) always UNDEF in usermode
2451          *  (b) UNDEF only if PMUSERENR.EN is 0
2452          *  (c) always read OK and UNDEF on write (PMUSERENR only)
2453          */
2454         if ((cpm == 12 && (op < 6)) ||
2455             (cpm == 13 && (op < 3))) {
2456             return env->cp15.c9_pmuserenr;
2457         } else if (cpm == 14 && op == 0 && (insn & ARM_CP_RW_BIT)) {
2458             /* PMUSERENR, read only */
2459             return 1;
2460         }
2461         return 0;
2462     }
2463
2464     if (cpn == 13 && cpm == 0) {
2465         /* TLS register.  */
2466         if (op == 2 || (op == 3 && (insn & ARM_CP_RW_BIT)))
2467             return 1;
2468     }
2469     return 0;
2470 }
2471
2472 static int cp15_tls_load_store(CPUARMState *env, DisasContext *s, uint32_t insn, uint32_t rd)
2473 {
2474     TCGv tmp;
2475     int cpn = (insn >> 16) & 0xf;
2476     int cpm = insn & 0xf;
2477     int op = ((insn >> 5) & 7) | ((insn >> 18) & 0x38);
2478
2479     if (!arm_feature(env, ARM_FEATURE_V6K))
2480         return 0;
2481
2482     if (!(cpn == 13 && cpm == 0))
2483         return 0;
2484
2485     if (insn & ARM_CP_RW_BIT) {
2486         switch (op) {
2487         case 2:
2488             tmp = load_cpu_field(cp15.c13_tls1);
2489             break;
2490         case 3:
2491             tmp = load_cpu_field(cp15.c13_tls2);
2492             break;
2493         case 4:
2494             tmp = load_cpu_field(cp15.c13_tls3);
2495             break;
2496         default:
2497             return 0;
2498         }
2499         store_reg(s, rd, tmp);
2500
2501     } else {
2502         tmp = load_reg(s, rd);
2503         switch (op) {
2504         case 2:
2505             store_cpu_field(tmp, cp15.c13_tls1);
2506             break;
2507         case 3:
2508             store_cpu_field(tmp, cp15.c13_tls2);
2509             break;
2510         case 4:
2511             store_cpu_field(tmp, cp15.c13_tls3);
2512             break;
2513         default:
2514             tcg_temp_free_i32(tmp);
2515             return 0;
2516         }
2517     }
2518     return 1;
2519 }
2520
2521 /* Disassemble system coprocessor (cp15) instruction.  Return nonzero if
2522    instruction is not defined.  */
2523 static int disas_cp15_insn(CPUARMState *env, DisasContext *s, uint32_t insn)
2524 {
2525     uint32_t rd;
2526     TCGv tmp, tmp2;
2527
2528     /* M profile cores use memory mapped registers instead of cp15.  */
2529     if (arm_feature(env, ARM_FEATURE_M))
2530         return 1;
2531
2532     if ((insn & (1 << 25)) == 0) {
2533         if (insn & (1 << 20)) {
2534             /* mrrc */
2535             return 1;
2536         }
2537         /* mcrr.  Used for block cache operations, so implement as no-op.  */
2538         return 0;
2539     }
2540     if ((insn & (1 << 4)) == 0) {
2541         /* cdp */
2542         return 1;
2543     }
2544     /* We special case a number of cp15 instructions which were used
2545      * for things which are real instructions in ARMv7. This allows
2546      * them to work in linux-user mode which doesn't provide functional
2547      * get_cp15/set_cp15 helpers, and is more efficient anyway.
2548      */
2549     switch ((insn & 0x0fff0fff)) {
2550     case 0x0e070f90:
2551         /* 0,c7,c0,4: Standard v6 WFI (also used in some pre-v6 cores).
2552          * In v7, this must NOP.
2553          */
2554         if (IS_USER(s)) {
2555             return 1;
2556         }
2557         if (!arm_feature(env, ARM_FEATURE_V7)) {
2558             /* Wait for interrupt.  */
2559             gen_set_pc_im(s->pc);
2560             s->is_jmp = DISAS_WFI;
2561         }
2562         return 0;
2563     case 0x0e070f58:
2564         /* 0,c7,c8,2: Not all pre-v6 cores implemented this WFI,
2565          * so this is slightly over-broad.
2566          */
2567         if (!IS_USER(s) && !arm_feature(env, ARM_FEATURE_V6)) {
2568             /* Wait for interrupt.  */
2569             gen_set_pc_im(s->pc);
2570             s->is_jmp = DISAS_WFI;
2571             return 0;
2572         }
2573         /* Otherwise continue to handle via helper function.
2574          * In particular, on v7 and some v6 cores this is one of
2575          * the VA-PA registers.
2576          */
2577         break;
2578     case 0x0e070f3d:
2579         /* 0,c7,c13,1: prefetch-by-MVA in v6, NOP in v7 */
2580         if (arm_feature(env, ARM_FEATURE_V6)) {
2581             return IS_USER(s) ? 1 : 0;
2582         }
2583         break;
2584     case 0x0e070f95: /* 0,c7,c5,4 : ISB */
2585     case 0x0e070f9a: /* 0,c7,c10,4: DSB */
2586     case 0x0e070fba: /* 0,c7,c10,5: DMB */
2587         /* Barriers in both v6 and v7 */
2588         if (arm_feature(env, ARM_FEATURE_V6)) {
2589             return 0;
2590         }
2591         break;
2592     default:
2593         break;
2594     }
2595
2596     if (IS_USER(s) && !cp15_user_ok(env, insn)) {
2597         return 1;
2598     }
2599
2600     rd = (insn >> 12) & 0xf;
2601
2602     if (cp15_tls_load_store(env, s, insn, rd))
2603         return 0;
2604
2605     tmp2 = tcg_const_i32(insn);
2606     if (insn & ARM_CP_RW_BIT) {
2607         tmp = tcg_temp_new_i32();
2608         gen_helper_get_cp15(tmp, cpu_env, tmp2);
2609         /* If the destination register is r15 then sets condition codes.  */
2610         if (rd != 15)
2611             store_reg(s, rd, tmp);
2612         else
2613             tcg_temp_free_i32(tmp);
2614     } else {
2615         tmp = load_reg(s, rd);
2616         gen_helper_set_cp15(cpu_env, tmp2, tmp);
2617         tcg_temp_free_i32(tmp);
2618         /* Normally we would always end the TB here, but Linux
2619          * arch/arm/mach-pxa/sleep.S expects two instructions following
2620          * an MMU enable to execute from cache.  Imitate this behaviour.  */
2621         if (!arm_feature(env, ARM_FEATURE_XSCALE) ||
2622                 (insn & 0x0fff0fff) != 0x0e010f10)
2623             gen_lookup_tb(s);
2624     }
2625     tcg_temp_free_i32(tmp2);
2626     return 0;
2627 }
2628
2629 #define VFP_REG_SHR(x, n) (((n) > 0) ? (x) >> (n) : (x) << -(n))
2630 #define VFP_SREG(insn, bigbit, smallbit) \
2631   ((VFP_REG_SHR(insn, bigbit - 1) & 0x1e) | (((insn) >> (smallbit)) & 1))
2632 #define VFP_DREG(reg, insn, bigbit, smallbit) do { \
2633     if (arm_feature(env, ARM_FEATURE_VFP3)) { \
2634         reg = (((insn) >> (bigbit)) & 0x0f) \
2635               | (((insn) >> ((smallbit) - 4)) & 0x10); \
2636     } else { \
2637         if (insn & (1 << (smallbit))) \
2638             return 1; \
2639         reg = ((insn) >> (bigbit)) & 0x0f; \
2640     }} while (0)
2641
2642 #define VFP_SREG_D(insn) VFP_SREG(insn, 12, 22)
2643 #define VFP_DREG_D(reg, insn) VFP_DREG(reg, insn, 12, 22)
2644 #define VFP_SREG_N(insn) VFP_SREG(insn, 16,  7)
2645 #define VFP_DREG_N(reg, insn) VFP_DREG(reg, insn, 16,  7)
2646 #define VFP_SREG_M(insn) VFP_SREG(insn,  0,  5)
2647 #define VFP_DREG_M(reg, insn) VFP_DREG(reg, insn,  0,  5)
2648
2649 /* Move between integer and VFP cores.  */
2650 static TCGv gen_vfp_mrs(void)
2651 {
2652     TCGv tmp = tcg_temp_new_i32();
2653     tcg_gen_mov_i32(tmp, cpu_F0s);
2654     return tmp;
2655 }
2656
2657 static void gen_vfp_msr(TCGv tmp)
2658 {
2659     tcg_gen_mov_i32(cpu_F0s, tmp);
2660     tcg_temp_free_i32(tmp);
2661 }
2662
2663 static void gen_neon_dup_u8(TCGv var, int shift)
2664 {
2665     TCGv tmp = tcg_temp_new_i32();
2666     if (shift)
2667         tcg_gen_shri_i32(var, var, shift);
2668     tcg_gen_ext8u_i32(var, var);
2669     tcg_gen_shli_i32(tmp, var, 8);
2670     tcg_gen_or_i32(var, var, tmp);
2671     tcg_gen_shli_i32(tmp, var, 16);
2672     tcg_gen_or_i32(var, var, tmp);
2673     tcg_temp_free_i32(tmp);
2674 }
2675
2676 static void gen_neon_dup_low16(TCGv var)
2677 {
2678     TCGv tmp = tcg_temp_new_i32();
2679     tcg_gen_ext16u_i32(var, var);
2680     tcg_gen_shli_i32(tmp, var, 16);
2681     tcg_gen_or_i32(var, var, tmp);
2682     tcg_temp_free_i32(tmp);
2683 }
2684
2685 static void gen_neon_dup_high16(TCGv var)
2686 {
2687     TCGv tmp = tcg_temp_new_i32();
2688     tcg_gen_andi_i32(var, var, 0xffff0000);
2689     tcg_gen_shri_i32(tmp, var, 16);
2690     tcg_gen_or_i32(var, var, tmp);
2691     tcg_temp_free_i32(tmp);
2692 }
2693
2694 static TCGv gen_load_and_replicate(DisasContext *s, TCGv addr, int size)
2695 {
2696     /* Load a single Neon element and replicate into a 32 bit TCG reg */
2697     TCGv tmp;
2698     switch (size) {
2699     case 0:
2700         tmp = gen_ld8u(addr, IS_USER(s));
2701         gen_neon_dup_u8(tmp, 0);
2702         break;
2703     case 1:
2704         tmp = gen_ld16u(addr, IS_USER(s));
2705         gen_neon_dup_low16(tmp);
2706         break;
2707     case 2:
2708         tmp = gen_ld32(addr, IS_USER(s));
2709         break;
2710     default: /* Avoid compiler warnings.  */
2711         abort();
2712     }
2713     return tmp;
2714 }
2715
2716 /* Disassemble a VFP instruction.  Returns nonzero if an error occurred
2717    (ie. an undefined instruction).  */
2718 static int disas_vfp_insn(CPUARMState * env, DisasContext *s, uint32_t insn)
2719 {
2720     uint32_t rd, rn, rm, op, i, n, offset, delta_d, delta_m, bank_mask;
2721     int dp, veclen;
2722     TCGv addr;
2723     TCGv tmp;
2724     TCGv tmp2;
2725
2726     if (!arm_feature(env, ARM_FEATURE_VFP))
2727         return 1;
2728
2729     if (!s->vfp_enabled) {
2730         /* VFP disabled.  Only allow fmxr/fmrx to/from some control regs.  */
2731         if ((insn & 0x0fe00fff) != 0x0ee00a10)
2732             return 1;
2733         rn = (insn >> 16) & 0xf;
2734         if (rn != ARM_VFP_FPSID && rn != ARM_VFP_FPEXC
2735             && rn != ARM_VFP_MVFR1 && rn != ARM_VFP_MVFR0)
2736             return 1;
2737     }
2738     dp = ((insn & 0xf00) == 0xb00);
2739     switch ((insn >> 24) & 0xf) {
2740     case 0xe:
2741         if (insn & (1 << 4)) {
2742             /* single register transfer */
2743             rd = (insn >> 12) & 0xf;
2744             if (dp) {
2745                 int size;
2746                 int pass;
2747
2748                 VFP_DREG_N(rn, insn);
2749                 if (insn & 0xf)
2750                     return 1;
2751                 if (insn & 0x00c00060
2752                     && !arm_feature(env, ARM_FEATURE_NEON))
2753                     return 1;
2754
2755                 pass = (insn >> 21) & 1;
2756                 if (insn & (1 << 22)) {
2757                     size = 0;
2758                     offset = ((insn >> 5) & 3) * 8;
2759                 } else if (insn & (1 << 5)) {
2760                     size = 1;
2761                     offset = (insn & (1 << 6)) ? 16 : 0;
2762                 } else {
2763                     size = 2;
2764                     offset = 0;
2765                 }
2766                 if (insn & ARM_CP_RW_BIT) {
2767                     /* vfp->arm */
2768                     tmp = neon_load_reg(rn, pass);
2769                     switch (size) {
2770                     case 0:
2771                         if (offset)
2772                             tcg_gen_shri_i32(tmp, tmp, offset);
2773                         if (insn & (1 << 23))
2774                             gen_uxtb(tmp);
2775                         else
2776                             gen_sxtb(tmp);
2777                         break;
2778                     case 1:
2779                         if (insn & (1 << 23)) {
2780                             if (offset) {
2781                                 tcg_gen_shri_i32(tmp, tmp, 16);
2782                             } else {
2783                                 gen_uxth(tmp);
2784                             }
2785                         } else {
2786                             if (offset) {
2787                                 tcg_gen_sari_i32(tmp, tmp, 16);
2788                             } else {
2789                                 gen_sxth(tmp);
2790                             }
2791                         }
2792                         break;
2793                     case 2:
2794                         break;
2795                     }
2796                     store_reg(s, rd, tmp);
2797                 } else {
2798                     /* arm->vfp */
2799                     tmp = load_reg(s, rd);
2800                     if (insn & (1 << 23)) {
2801                         /* VDUP */
2802                         if (size == 0) {
2803                             gen_neon_dup_u8(tmp, 0);
2804                         } else if (size == 1) {
2805                             gen_neon_dup_low16(tmp);
2806                         }
2807                         for (n = 0; n <= pass * 2; n++) {
2808                             tmp2 = tcg_temp_new_i32();
2809                             tcg_gen_mov_i32(tmp2, tmp);
2810                             neon_store_reg(rn, n, tmp2);
2811                         }
2812                         neon_store_reg(rn, n, tmp);
2813                     } else {
2814                         /* VMOV */
2815                         switch (size) {
2816                         case 0:
2817                             tmp2 = neon_load_reg(rn, pass);
2818                             gen_bfi(tmp, tmp2, tmp, offset, 0xff);
2819                             tcg_temp_free_i32(tmp2);
2820                             break;
2821                         case 1:
2822                             tmp2 = neon_load_reg(rn, pass);
2823                             gen_bfi(tmp, tmp2, tmp, offset, 0xffff);
2824                             tcg_temp_free_i32(tmp2);
2825                             break;
2826                         case 2:
2827                             break;
2828                         }
2829                         neon_store_reg(rn, pass, tmp);
2830                     }
2831                 }
2832             } else { /* !dp */
2833                 if ((insn & 0x6f) != 0x00)
2834                     return 1;
2835                 rn = VFP_SREG_N(insn);
2836                 if (insn & ARM_CP_RW_BIT) {
2837                     /* vfp->arm */
2838                     if (insn & (1 << 21)) {
2839                         /* system register */
2840                         rn >>= 1;
2841
2842                         switch (rn) {
2843                         case ARM_VFP_FPSID:
2844                             /* VFP2 allows access to FSID from userspace.
2845                                VFP3 restricts all id registers to privileged
2846                                accesses.  */
2847                             if (IS_USER(s)
2848                                 && arm_feature(env, ARM_FEATURE_VFP3))
2849                                 return 1;
2850                             tmp = load_cpu_field(vfp.xregs[rn]);
2851                             break;
2852                         case ARM_VFP_FPEXC:
2853                             if (IS_USER(s))
2854                                 return 1;
2855                             tmp = load_cpu_field(vfp.xregs[rn]);
2856                             break;
2857                         case ARM_VFP_FPINST:
2858                         case ARM_VFP_FPINST2:
2859                             /* Not present in VFP3.  */
2860                             if (IS_USER(s)
2861                                 || arm_feature(env, ARM_FEATURE_VFP3))
2862                                 return 1;
2863                             tmp = load_cpu_field(vfp.xregs[rn]);
2864                             break;
2865                         case ARM_VFP_FPSCR:
2866                             if (rd == 15) {
2867                                 tmp = load_cpu_field(vfp.xregs[ARM_VFP_FPSCR]);
2868                                 tcg_gen_andi_i32(tmp, tmp, 0xf0000000);
2869                             } else {
2870                                 tmp = tcg_temp_new_i32();
2871                                 gen_helper_vfp_get_fpscr(tmp, cpu_env);
2872                             }
2873                             break;
2874                         case ARM_VFP_MVFR0:
2875                         case ARM_VFP_MVFR1:
2876                             if (IS_USER(s)
2877                                 || !arm_feature(env, ARM_FEATURE_MVFR))
2878                                 return 1;
2879                             tmp = load_cpu_field(vfp.xregs[rn]);
2880                             break;
2881                         default:
2882                             return 1;
2883                         }
2884                     } else {
2885                         gen_mov_F0_vreg(0, rn);
2886                         tmp = gen_vfp_mrs();
2887                     }
2888                     if (rd == 15) {
2889                         /* Set the 4 flag bits in the CPSR.  */
2890                         gen_set_nzcv(tmp);
2891                         tcg_temp_free_i32(tmp);
2892                     } else {
2893                         store_reg(s, rd, tmp);
2894                     }
2895                 } else {
2896                     /* arm->vfp */
2897                     tmp = load_reg(s, rd);
2898                     if (insn & (1 << 21)) {
2899                         rn >>= 1;
2900                         /* system register */
2901                         switch (rn) {
2902                         case ARM_VFP_FPSID:
2903                         case ARM_VFP_MVFR0:
2904                         case ARM_VFP_MVFR1:
2905                             /* Writes are ignored.  */
2906                             break;
2907                         case ARM_VFP_FPSCR:
2908                             gen_helper_vfp_set_fpscr(cpu_env, tmp);
2909                             tcg_temp_free_i32(tmp);
2910                             gen_lookup_tb(s);
2911                             break;
2912                         case ARM_VFP_FPEXC:
2913                             if (IS_USER(s))
2914                                 return 1;
2915                             /* TODO: VFP subarchitecture support.
2916                              * For now, keep the EN bit only */
2917                             tcg_gen_andi_i32(tmp, tmp, 1 << 30);
2918                             store_cpu_field(tmp, vfp.xregs[rn]);
2919                             gen_lookup_tb(s);
2920                             break;
2921                         case ARM_VFP_FPINST:
2922                         case ARM_VFP_FPINST2:
2923                             store_cpu_field(tmp, vfp.xregs[rn]);
2924                             break;
2925                         default:
2926                             return 1;
2927                         }
2928                     } else {
2929                         gen_vfp_msr(tmp);
2930                         gen_mov_vreg_F0(0, rn);
2931                     }
2932                 }
2933             }
2934         } else {
2935             /* data processing */
2936             /* The opcode is in bits 23, 21, 20 and 6.  */
2937             op = ((insn >> 20) & 8) | ((insn >> 19) & 6) | ((insn >> 6) & 1);
2938             if (dp) {
2939                 if (op == 15) {
2940                     /* rn is opcode */
2941                     rn = ((insn >> 15) & 0x1e) | ((insn >> 7) & 1);
2942                 } else {
2943                     /* rn is register number */
2944                     VFP_DREG_N(rn, insn);
2945                 }
2946
2947                 if (op == 15 && (rn == 15 || ((rn & 0x1c) == 0x18))) {
2948                     /* Integer or single precision destination.  */
2949                     rd = VFP_SREG_D(insn);
2950                 } else {
2951                     VFP_DREG_D(rd, insn);
2952                 }
2953                 if (op == 15 &&
2954                     (((rn & 0x1c) == 0x10) || ((rn & 0x14) == 0x14))) {
2955                     /* VCVT from int is always from S reg regardless of dp bit.
2956                      * VCVT with immediate frac_bits has same format as SREG_M
2957                      */
2958                     rm = VFP_SREG_M(insn);
2959                 } else {
2960                     VFP_DREG_M(rm, insn);
2961                 }
2962             } else {
2963                 rn = VFP_SREG_N(insn);
2964                 if (op == 15 && rn == 15) {
2965                     /* Double precision destination.  */
2966                     VFP_DREG_D(rd, insn);
2967                 } else {
2968                     rd = VFP_SREG_D(insn);
2969                 }
2970                 /* NB that we implicitly rely on the encoding for the frac_bits
2971                  * in VCVT of fixed to float being the same as that of an SREG_M
2972                  */
2973                 rm = VFP_SREG_M(insn);
2974             }
2975
2976             veclen = s->vec_len;
2977             if (op == 15 && rn > 3)
2978                 veclen = 0;
2979
2980             /* Shut up compiler warnings.  */
2981             delta_m = 0;
2982             delta_d = 0;
2983             bank_mask = 0;
2984
2985             if (veclen > 0) {
2986                 if (dp)
2987                     bank_mask = 0xc;
2988                 else
2989                     bank_mask = 0x18;
2990
2991                 /* Figure out what type of vector operation this is.  */
2992                 if ((rd & bank_mask) == 0) {
2993                     /* scalar */
2994                     veclen = 0;
2995                 } else {
2996                     if (dp)
2997                         delta_d = (s->vec_stride >> 1) + 1;
2998                     else
2999                         delta_d = s->vec_stride + 1;
3000
3001                     if ((rm & bank_mask) == 0) {
3002                         /* mixed scalar/vector */
3003                         delta_m = 0;
3004                     } else {
3005                         /* vector */
3006                         delta_m = delta_d;
3007                     }
3008                 }
3009             }
3010
3011             /* Load the initial operands.  */
3012             if (op == 15) {
3013                 switch (rn) {
3014                 case 16:
3015                 case 17:
3016                     /* Integer source */
3017                     gen_mov_F0_vreg(0, rm);
3018                     break;
3019                 case 8:
3020                 case 9:
3021                     /* Compare */
3022                     gen_mov_F0_vreg(dp, rd);
3023                     gen_mov_F1_vreg(dp, rm);
3024                     break;
3025                 case 10:
3026                 case 11:
3027                     /* Compare with zero */
3028                     gen_mov_F0_vreg(dp, rd);
3029                     gen_vfp_F1_ld0(dp);
3030                     break;
3031                 case 20:
3032                 case 21:
3033                 case 22:
3034                 case 23:
3035                 case 28:
3036                 case 29:
3037                 case 30:
3038                 case 31:
3039                     /* Source and destination the same.  */
3040                     gen_mov_F0_vreg(dp, rd);
3041                     break;
3042                 case 4:
3043                 case 5:
3044                 case 6:
3045                 case 7:
3046                     /* VCVTB, VCVTT: only present with the halfprec extension,
3047                      * UNPREDICTABLE if bit 8 is set (we choose to UNDEF)
3048                      */
3049                     if (dp || !arm_feature(env, ARM_FEATURE_VFP_FP16)) {
3050                         return 1;
3051                     }
3052                     /* Otherwise fall through */
3053                 default:
3054                     /* One source operand.  */
3055                     gen_mov_F0_vreg(dp, rm);
3056                     break;
3057                 }
3058             } else {
3059                 /* Two source operands.  */
3060                 gen_mov_F0_vreg(dp, rn);
3061                 gen_mov_F1_vreg(dp, rm);
3062             }
3063
3064             for (;;) {
3065                 /* Perform the calculation.  */
3066                 switch (op) {
3067                 case 0: /* VMLA: fd + (fn * fm) */
3068                     /* Note that order of inputs to the add matters for NaNs */
3069                     gen_vfp_F1_mul(dp);
3070                     gen_mov_F0_vreg(dp, rd);
3071                     gen_vfp_add(dp);
3072                     break;
3073                 case 1: /* VMLS: fd + -(fn * fm) */
3074                     gen_vfp_mul(dp);
3075                     gen_vfp_F1_neg(dp);
3076                     gen_mov_F0_vreg(dp, rd);
3077                     gen_vfp_add(dp);
3078                     break;
3079                 case 2: /* VNMLS: -fd + (fn * fm) */
3080                     /* Note that it isn't valid to replace (-A + B) with (B - A)
3081                      * or similar plausible looking simplifications
3082                      * because this will give wrong results for NaNs.
3083                      */
3084                     gen_vfp_F1_mul(dp);
3085                     gen_mov_F0_vreg(dp, rd);
3086                     gen_vfp_neg(dp);
3087                     gen_vfp_add(dp);
3088                     break;
3089                 case 3: /* VNMLA: -fd + -(fn * fm) */
3090                     gen_vfp_mul(dp);
3091                     gen_vfp_F1_neg(dp);
3092                     gen_mov_F0_vreg(dp, rd);
3093                     gen_vfp_neg(dp);
3094                     gen_vfp_add(dp);
3095                     break;
3096                 case 4: /* mul: fn * fm */
3097                     gen_vfp_mul(dp);
3098                     break;
3099                 case 5: /* nmul: -(fn * fm) */
3100                     gen_vfp_mul(dp);
3101                     gen_vfp_neg(dp);
3102                     break;
3103                 case 6: /* add: fn + fm */
3104                     gen_vfp_add(dp);
3105                     break;
3106                 case 7: /* sub: fn - fm */
3107                     gen_vfp_sub(dp);
3108                     break;
3109                 case 8: /* div: fn / fm */
3110                     gen_vfp_div(dp);
3111                     break;
3112                 case 10: /* VFNMA : fd = muladd(-fd,  fn, fm) */
3113                 case 11: /* VFNMS : fd = muladd(-fd, -fn, fm) */
3114                 case 12: /* VFMA  : fd = muladd( fd,  fn, fm) */
3115                 case 13: /* VFMS  : fd = muladd( fd, -fn, fm) */
3116                     /* These are fused multiply-add, and must be done as one
3117                      * floating point operation with no rounding between the
3118                      * multiplication and addition steps.
3119                      * NB that doing the negations here as separate steps is
3120                      * correct : an input NaN should come out with its sign bit
3121                      * flipped if it is a negated-input.
3122                      */
3123                     if (!arm_feature(env, ARM_FEATURE_VFP4)) {
3124                         return 1;
3125                     }
3126                     if (dp) {
3127                         TCGv_ptr fpst;
3128                         TCGv_i64 frd;
3129                         if (op & 1) {
3130                             /* VFNMS, VFMS */
3131                             gen_helper_vfp_negd(cpu_F0d, cpu_F0d);
3132                         }
3133                         frd = tcg_temp_new_i64();
3134                         tcg_gen_ld_f64(frd, cpu_env, vfp_reg_offset(dp, rd));
3135                         if (op & 2) {
3136                             /* VFNMA, VFNMS */
3137                             gen_helper_vfp_negd(frd, frd);
3138                         }
3139                         fpst = get_fpstatus_ptr(0);
3140                         gen_helper_vfp_muladdd(cpu_F0d, cpu_F0d,
3141                                                cpu_F1d, frd, fpst);
3142                         tcg_temp_free_ptr(fpst);
3143                         tcg_temp_free_i64(frd);
3144                     } else {
3145                         TCGv_ptr fpst;
3146                         TCGv_i32 frd;
3147                         if (op & 1) {
3148                             /* VFNMS, VFMS */
3149                             gen_helper_vfp_negs(cpu_F0s, cpu_F0s);
3150                         }
3151                         frd = tcg_temp_new_i32();
3152                         tcg_gen_ld_f32(frd, cpu_env, vfp_reg_offset(dp, rd));
3153                         if (op & 2) {
3154                             gen_helper_vfp_negs(frd, frd);
3155                         }
3156                         fpst = get_fpstatus_ptr(0);
3157                         gen_helper_vfp_muladds(cpu_F0s, cpu_F0s,
3158                                                cpu_F1s, frd, fpst);
3159                         tcg_temp_free_ptr(fpst);
3160                         tcg_temp_free_i32(frd);
3161                     }
3162                     break;
3163                 case 14: /* fconst */
3164                     if (!arm_feature(env, ARM_FEATURE_VFP3))
3165                       return 1;
3166
3167                     n = (insn << 12) & 0x80000000;
3168                     i = ((insn >> 12) & 0x70) | (insn & 0xf);
3169                     if (dp) {
3170                         if (i & 0x40)
3171                             i |= 0x3f80;
3172                         else
3173                             i |= 0x4000;
3174                         n |= i << 16;
3175                         tcg_gen_movi_i64(cpu_F0d, ((uint64_t)n) << 32);
3176                     } else {
3177                         if (i & 0x40)
3178                             i |= 0x780;
3179                         else
3180                             i |= 0x800;
3181                         n |= i << 19;
3182                         tcg_gen_movi_i32(cpu_F0s, n);
3183                     }
3184                     break;
3185                 case 15: /* extension space */
3186                     switch (rn) {
3187                     case 0: /* cpy */
3188                         /* no-op */
3189                         break;
3190                     case 1: /* abs */
3191                         gen_vfp_abs(dp);
3192                         break;
3193                     case 2: /* neg */
3194                         gen_vfp_neg(dp);
3195                         break;
3196                     case 3: /* sqrt */
3197                         gen_vfp_sqrt(dp);
3198                         break;
3199                     case 4: /* vcvtb.f32.f16 */
3200                         tmp = gen_vfp_mrs();
3201                         tcg_gen_ext16u_i32(tmp, tmp);
3202                         gen_helper_vfp_fcvt_f16_to_f32(cpu_F0s, tmp, cpu_env);
3203                         tcg_temp_free_i32(tmp);
3204                         break;
3205                     case 5: /* vcvtt.f32.f16 */
3206                         tmp = gen_vfp_mrs();
3207                         tcg_gen_shri_i32(tmp, tmp, 16);
3208                         gen_helper_vfp_fcvt_f16_to_f32(cpu_F0s, tmp, cpu_env);
3209                         tcg_temp_free_i32(tmp);
3210                         break;
3211                     case 6: /* vcvtb.f16.f32 */
3212                         tmp = tcg_temp_new_i32();
3213                         gen_helper_vfp_fcvt_f32_to_f16(tmp, cpu_F0s, cpu_env);
3214                         gen_mov_F0_vreg(0, rd);
3215                         tmp2 = gen_vfp_mrs();
3216                         tcg_gen_andi_i32(tmp2, tmp2, 0xffff0000);
3217                         tcg_gen_or_i32(tmp, tmp, tmp2);
3218                         tcg_temp_free_i32(tmp2);
3219                         gen_vfp_msr(tmp);
3220                         break;
3221                     case 7: /* vcvtt.f16.f32 */
3222                         tmp = tcg_temp_new_i32();
3223                         gen_helper_vfp_fcvt_f32_to_f16(tmp, cpu_F0s, cpu_env);
3224                         tcg_gen_shli_i32(tmp, tmp, 16);
3225                         gen_mov_F0_vreg(0, rd);
3226                         tmp2 = gen_vfp_mrs();
3227                         tcg_gen_ext16u_i32(tmp2, tmp2);
3228                         tcg_gen_or_i32(tmp, tmp, tmp2);
3229                         tcg_temp_free_i32(tmp2);
3230                         gen_vfp_msr(tmp);
3231                         break;
3232                     case 8: /* cmp */
3233                         gen_vfp_cmp(dp);
3234                         break;
3235                     case 9: /* cmpe */
3236                         gen_vfp_cmpe(dp);
3237                         break;
3238                     case 10: /* cmpz */
3239                         gen_vfp_cmp(dp);
3240                         break;
3241                     case 11: /* cmpez */
3242                         gen_vfp_F1_ld0(dp);
3243                         gen_vfp_cmpe(dp);
3244                         break;
3245                     case 15: /* single<->double conversion */
3246                         if (dp)
3247                             gen_helper_vfp_fcvtsd(cpu_F0s, cpu_F0d, cpu_env);
3248                         else
3249                             gen_helper_vfp_fcvtds(cpu_F0d, cpu_F0s, cpu_env);
3250                         break;
3251                     case 16: /* fuito */
3252                         gen_vfp_uito(dp, 0);
3253                         break;
3254                     case 17: /* fsito */
3255                         gen_vfp_sito(dp, 0);
3256                         break;
3257                     case 20: /* fshto */
3258                         if (!arm_feature(env, ARM_FEATURE_VFP3))
3259                           return 1;
3260                         gen_vfp_shto(dp, 16 - rm, 0);
3261                         break;
3262                     case 21: /* fslto */
3263                         if (!arm_feature(env, ARM_FEATURE_VFP3))
3264                           return 1;
3265                         gen_vfp_slto(dp, 32 - rm, 0);
3266                         break;
3267                     case 22: /* fuhto */
3268                         if (!arm_feature(env, ARM_FEATURE_VFP3))
3269                           return 1;
3270                         gen_vfp_uhto(dp, 16 - rm, 0);
3271                         break;
3272                     case 23: /* fulto */
3273                         if (!arm_feature(env, ARM_FEATURE_VFP3))
3274                           return 1;
3275                         gen_vfp_ulto(dp, 32 - rm, 0);
3276                         break;
3277                     case 24: /* ftoui */
3278                         gen_vfp_toui(dp, 0);
3279                         break;
3280                     case 25: /* ftouiz */
3281                         gen_vfp_touiz(dp, 0);
3282                         break;
3283                     case 26: /* ftosi */
3284                         gen_vfp_tosi(dp, 0);
3285                         break;
3286                     case 27: /* ftosiz */
3287                         gen_vfp_tosiz(dp, 0);
3288                         break;
3289                     case 28: /* ftosh */
3290                         if (!arm_feature(env, ARM_FEATURE_VFP3))
3291                           return 1;
3292                         gen_vfp_tosh(dp, 16 - rm, 0);
3293                         break;
3294                     case 29: /* ftosl */
3295                         if (!arm_feature(env, ARM_FEATURE_VFP3))
3296                           return 1;
3297                         gen_vfp_tosl(dp, 32 - rm, 0);
3298                         break;
3299                     case 30: /* ftouh */
3300                         if (!arm_feature(env, ARM_FEATURE_VFP3))
3301                           return 1;
3302                         gen_vfp_touh(dp, 16 - rm, 0);
3303                         break;
3304                     case 31: /* ftoul */
3305                         if (!arm_feature(env, ARM_FEATURE_VFP3))
3306                           return 1;
3307                         gen_vfp_toul(dp, 32 - rm, 0);
3308                         break;
3309                     default: /* undefined */
3310                         return 1;
3311                     }
3312                     break;
3313                 default: /* undefined */
3314                     return 1;
3315                 }
3316
3317                 /* Write back the result.  */
3318                 if (op == 15 && (rn >= 8 && rn <= 11))
3319                     ; /* Comparison, do nothing.  */
3320                 else if (op == 15 && dp && ((rn & 0x1c) == 0x18))
3321                     /* VCVT double to int: always integer result. */
3322                     gen_mov_vreg_F0(0, rd);
3323                 else if (op == 15 && rn == 15)
3324                     /* conversion */
3325                     gen_mov_vreg_F0(!dp, rd);
3326                 else
3327                     gen_mov_vreg_F0(dp, rd);
3328
3329                 /* break out of the loop if we have finished  */
3330                 if (veclen == 0)
3331                     break;
3332
3333                 if (op == 15 && delta_m == 0) {
3334                     /* single source one-many */
3335                     while (veclen--) {
3336                         rd = ((rd + delta_d) & (bank_mask - 1))
3337                              | (rd & bank_mask);
3338                         gen_mov_vreg_F0(dp, rd);
3339                     }
3340                     break;
3341                 }
3342                 /* Setup the next operands.  */
3343                 veclen--;
3344                 rd = ((rd + delta_d) & (bank_mask - 1))
3345                      | (rd & bank_mask);
3346
3347                 if (op == 15) {
3348                     /* One source operand.  */
3349                     rm = ((rm + delta_m) & (bank_mask - 1))
3350                          | (rm & bank_mask);
3351                     gen_mov_F0_vreg(dp, rm);
3352                 } else {
3353                     /* Two source operands.  */
3354                     rn = ((rn + delta_d) & (bank_mask - 1))
3355                          | (rn & bank_mask);
3356                     gen_mov_F0_vreg(dp, rn);
3357                     if (delta_m) {
3358                         rm = ((rm + delta_m) & (bank_mask - 1))
3359                              | (rm & bank_mask);
3360                         gen_mov_F1_vreg(dp, rm);
3361                     }
3362                 }
3363             }
3364         }
3365         break;
3366     case 0xc:
3367     case 0xd:
3368         if ((insn & 0x03e00000) == 0x00400000) {
3369             /* two-register transfer */
3370             rn = (insn >> 16) & 0xf;
3371             rd = (insn >> 12) & 0xf;
3372             if (dp) {
3373                 VFP_DREG_M(rm, insn);
3374             } else {
3375                 rm = VFP_SREG_M(insn);
3376             }
3377
3378             if (insn & ARM_CP_RW_BIT) {
3379                 /* vfp->arm */
3380                 if (dp) {
3381                     gen_mov_F0_vreg(0, rm * 2);
3382                     tmp = gen_vfp_mrs();
3383                     store_reg(s, rd, tmp);
3384                     gen_mov_F0_vreg(0, rm * 2 + 1);
3385                     tmp = gen_vfp_mrs();
3386                     store_reg(s, rn, tmp);
3387                 } else {
3388                     gen_mov_F0_vreg(0, rm);
3389                     tmp = gen_vfp_mrs();
3390                     store_reg(s, rd, tmp);
3391                     gen_mov_F0_vreg(0, rm + 1);
3392                     tmp = gen_vfp_mrs();
3393                     store_reg(s, rn, tmp);
3394                 }
3395             } else {
3396                 /* arm->vfp */
3397                 if (dp) {
3398                     tmp = load_reg(s, rd);
3399                     gen_vfp_msr(tmp);
3400                     gen_mov_vreg_F0(0, rm * 2);
3401                     tmp = load_reg(s, rn);
3402                     gen_vfp_msr(tmp);
3403                     gen_mov_vreg_F0(0, rm * 2 + 1);
3404                 } else {
3405                     tmp = load_reg(s, rd);
3406                     gen_vfp_msr(tmp);
3407                     gen_mov_vreg_F0(0, rm);
3408                     tmp = load_reg(s, rn);
3409                     gen_vfp_msr(tmp);
3410                     gen_mov_vreg_F0(0, rm + 1);
3411                 }
3412             }
3413         } else {
3414             /* Load/store */
3415             rn = (insn >> 16) & 0xf;
3416             if (dp)
3417                 VFP_DREG_D(rd, insn);
3418             else
3419                 rd = VFP_SREG_D(insn);
3420             if ((insn & 0x01200000) == 0x01000000) {
3421                 /* Single load/store */
3422                 offset = (insn & 0xff) << 2;
3423                 if ((insn & (1 << 23)) == 0)
3424                     offset = -offset;
3425                 if (s->thumb && rn == 15) {
3426                     /* This is actually UNPREDICTABLE */
3427                     addr = tcg_temp_new_i32();
3428                     tcg_gen_movi_i32(addr, s->pc & ~2);
3429                 } else {
3430                     addr = load_reg(s, rn);
3431                 }
3432                 tcg_gen_addi_i32(addr, addr, offset);
3433                 if (insn & (1 << 20)) {
3434                     gen_vfp_ld(s, dp, addr);
3435                     gen_mov_vreg_F0(dp, rd);
3436                 } else {
3437                     gen_mov_F0_vreg(dp, rd);
3438                     gen_vfp_st(s, dp, addr);
3439                 }
3440                 tcg_temp_free_i32(addr);
3441             } else {
3442                 /* load/store multiple */
3443                 int w = insn & (1 << 21);
3444                 if (dp)
3445                     n = (insn >> 1) & 0x7f;
3446                 else
3447                     n = insn & 0xff;
3448
3449                 if (w && !(((insn >> 23) ^ (insn >> 24)) & 1)) {
3450                     /* P == U , W == 1  => UNDEF */
3451                     return 1;
3452                 }
3453                 if (n == 0 || (rd + n) > 32 || (dp && n > 16)) {
3454                     /* UNPREDICTABLE cases for bad immediates: we choose to
3455                      * UNDEF to avoid generating huge numbers of TCG ops
3456                      */
3457                     return 1;
3458                 }
3459                 if (rn == 15 && w) {
3460                     /* writeback to PC is UNPREDICTABLE, we choose to UNDEF */
3461                     return 1;
3462                 }
3463
3464                 if (s->thumb && rn == 15) {
3465                     /* This is actually UNPREDICTABLE */
3466                     addr = tcg_temp_new_i32();
3467                     tcg_gen_movi_i32(addr, s->pc & ~2);
3468                 } else {
3469                     addr = load_reg(s, rn);
3470                 }
3471                 if (insn & (1 << 24)) /* pre-decrement */
3472                     tcg_gen_addi_i32(addr, addr, -((insn & 0xff) << 2));
3473
3474                 if (dp)
3475                     offset = 8;
3476                 else
3477                     offset = 4;
3478                 for (i = 0; i < n; i++) {
3479                     if (insn & ARM_CP_RW_BIT) {
3480                         /* load */
3481                         gen_vfp_ld(s, dp, addr);
3482                         gen_mov_vreg_F0(dp, rd + i);
3483                     } else {
3484                         /* store */
3485                         gen_mov_F0_vreg(dp, rd + i);
3486                         gen_vfp_st(s, dp, addr);
3487                     }
3488                     tcg_gen_addi_i32(addr, addr, offset);
3489                 }
3490                 if (w) {
3491                     /* writeback */
3492                     if (insn & (1 << 24))
3493                         offset = -offset * n;
3494                     else if (dp && (insn & 1))
3495                         offset = 4;
3496                     else
3497                         offset = 0;
3498
3499                     if (offset != 0)
3500                         tcg_gen_addi_i32(addr, addr, offset);
3501                     store_reg(s, rn, addr);
3502                 } else {
3503                     tcg_temp_free_i32(addr);
3504                 }
3505             }
3506         }
3507         break;
3508     default:
3509         /* Should never happen.  */
3510         return 1;
3511     }
3512     return 0;
3513 }
3514
3515 static inline void gen_goto_tb(DisasContext *s, int n, uint32_t dest)
3516 {
3517     TranslationBlock *tb;
3518
3519     tb = s->tb;
3520     if ((tb->pc & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK)) {
3521         tcg_gen_goto_tb(n);
3522         gen_set_pc_im(dest);
3523         tcg_gen_exit_tb((tcg_target_long)tb + n);
3524     } else {
3525         gen_set_pc_im(dest);
3526         tcg_gen_exit_tb(0);
3527     }
3528 }
3529
3530 static inline void gen_jmp (DisasContext *s, uint32_t dest)
3531 {
3532     if (unlikely(s->singlestep_enabled)) {
3533         /* An indirect jump so that we still trigger the debug exception.  */
3534         if (s->thumb)
3535             dest |= 1;
3536         gen_bx_im(s, dest);
3537     } else {
3538         gen_goto_tb(s, 0, dest);
3539         s->is_jmp = DISAS_TB_JUMP;
3540     }
3541 }
3542
3543 static inline void gen_mulxy(TCGv t0, TCGv t1, int x, int y)
3544 {
3545     if (x)
3546         tcg_gen_sari_i32(t0, t0, 16);
3547     else
3548         gen_sxth(t0);
3549     if (y)
3550         tcg_gen_sari_i32(t1, t1, 16);
3551     else
3552         gen_sxth(t1);
3553     tcg_gen_mul_i32(t0, t0, t1);
3554 }
3555
3556 /* Return the mask of PSR bits set by a MSR instruction.  */
3557 static uint32_t msr_mask(CPUARMState *env, DisasContext *s, int flags, int spsr) {
3558     uint32_t mask;
3559
3560     mask = 0;
3561     if (flags & (1 << 0))
3562         mask |= 0xff;
3563     if (flags & (1 << 1))
3564         mask |= 0xff00;
3565     if (flags & (1 << 2))
3566         mask |= 0xff0000;
3567     if (flags & (1 << 3))
3568         mask |= 0xff000000;
3569
3570     /* Mask out undefined bits.  */
3571     mask &= ~CPSR_RESERVED;
3572     if (!arm_feature(env, ARM_FEATURE_V4T))
3573         mask &= ~CPSR_T;
3574     if (!arm_feature(env, ARM_FEATURE_V5))
3575         mask &= ~CPSR_Q; /* V5TE in reality*/
3576     if (!arm_feature(env, ARM_FEATURE_V6))
3577         mask &= ~(CPSR_E | CPSR_GE);
3578     if (!arm_feature(env, ARM_FEATURE_THUMB2))
3579         mask &= ~CPSR_IT;
3580     /* Mask out execution state bits.  */
3581     if (!spsr)
3582         mask &= ~CPSR_EXEC;
3583     /* Mask out privileged bits.  */
3584     if (IS_USER(s))
3585         mask &= CPSR_USER;
3586     return mask;
3587 }
3588
3589 /* Returns nonzero if access to the PSR is not permitted. Marks t0 as dead. */
3590 static int gen_set_psr(DisasContext *s, uint32_t mask, int spsr, TCGv t0)
3591 {
3592     TCGv tmp;
3593     if (spsr) {
3594         /* ??? This is also undefined in system mode.  */
3595         if (IS_USER(s))
3596             return 1;
3597
3598         tmp = load_cpu_field(spsr);
3599         tcg_gen_andi_i32(tmp, tmp, ~mask);
3600         tcg_gen_andi_i32(t0, t0, mask);
3601         tcg_gen_or_i32(tmp, tmp, t0);
3602         store_cpu_field(tmp, spsr);
3603     } else {
3604         gen_set_cpsr(t0, mask);
3605     }
3606     tcg_temp_free_i32(t0);
3607     gen_lookup_tb(s);
3608     return 0;
3609 }
3610
3611 /* Returns nonzero if access to the PSR is not permitted.  */
3612 static int gen_set_psr_im(DisasContext *s, uint32_t mask, int spsr, uint32_t val)
3613 {
3614     TCGv tmp;
3615     tmp = tcg_temp_new_i32();
3616     tcg_gen_movi_i32(tmp, val);
3617     return gen_set_psr(s, mask, spsr, tmp);
3618 }
3619
3620 /* Generate an old-style exception return. Marks pc as dead. */
3621 static void gen_exception_return(DisasContext *s, TCGv pc)
3622 {
3623     TCGv tmp;
3624     store_reg(s, 15, pc);
3625     tmp = load_cpu_field(spsr);
3626     gen_set_cpsr(tmp, 0xffffffff);
3627     tcg_temp_free_i32(tmp);
3628     s->is_jmp = DISAS_UPDATE;
3629 }
3630
3631 /* Generate a v6 exception return.  Marks both values as dead.  */
3632 static void gen_rfe(DisasContext *s, TCGv pc, TCGv cpsr)
3633 {
3634     gen_set_cpsr(cpsr, 0xffffffff);
3635     tcg_temp_free_i32(cpsr);
3636     store_reg(s, 15, pc);
3637     s->is_jmp = DISAS_UPDATE;
3638 }
3639
3640 static inline void
3641 gen_set_condexec (DisasContext *s)
3642 {
3643     if (s->condexec_mask) {
3644         uint32_t val = (s->condexec_cond << 4) | (s->condexec_mask >> 1);
3645         TCGv tmp = tcg_temp_new_i32();
3646         tcg_gen_movi_i32(tmp, val);
3647         store_cpu_field(tmp, condexec_bits);
3648     }
3649 }
3650
3651 static void gen_exception_insn(DisasContext *s, int offset, int excp)
3652 {
3653     gen_set_condexec(s);
3654     gen_set_pc_im(s->pc - offset);
3655     gen_exception(excp);
3656     s->is_jmp = DISAS_JUMP;
3657 }
3658
3659 static void gen_nop_hint(DisasContext *s, int val)
3660 {
3661     switch (val) {
3662     case 3: /* wfi */
3663         gen_set_pc_im(s->pc);
3664         s->is_jmp = DISAS_WFI;
3665         break;
3666     case 2: /* wfe */
3667     case 4: /* sev */
3668         /* TODO: Implement SEV and WFE.  May help SMP performance.  */
3669     default: /* nop */
3670         break;
3671     }
3672 }
3673
3674 #define CPU_V001 cpu_V0, cpu_V0, cpu_V1
3675
3676 static inline void gen_neon_add(int size, TCGv t0, TCGv t1)
3677 {
3678     switch (size) {
3679     case 0: gen_helper_neon_add_u8(t0, t0, t1); break;
3680     case 1: gen_helper_neon_add_u16(t0, t0, t1); break;
3681     case 2: tcg_gen_add_i32(t0, t0, t1); break;
3682     default: abort();
3683     }
3684 }
3685
3686 static inline void gen_neon_rsb(int size, TCGv t0, TCGv t1)
3687 {
3688     switch (size) {
3689     case 0: gen_helper_neon_sub_u8(t0, t1, t0); break;
3690     case 1: gen_helper_neon_sub_u16(t0, t1, t0); break;
3691     case 2: tcg_gen_sub_i32(t0, t1, t0); break;
3692     default: return;
3693     }
3694 }
3695
3696 /* 32-bit pairwise ops end up the same as the elementwise versions.  */
3697 #define gen_helper_neon_pmax_s32  gen_helper_neon_max_s32
3698 #define gen_helper_neon_pmax_u32  gen_helper_neon_max_u32
3699 #define gen_helper_neon_pmin_s32  gen_helper_neon_min_s32
3700 #define gen_helper_neon_pmin_u32  gen_helper_neon_min_u32
3701
3702 #define GEN_NEON_INTEGER_OP_ENV(name) do { \
3703     switch ((size << 1) | u) { \
3704     case 0: \
3705         gen_helper_neon_##name##_s8(tmp, cpu_env, tmp, tmp2); \
3706         break; \
3707     case 1: \
3708         gen_helper_neon_##name##_u8(tmp, cpu_env, tmp, tmp2); \
3709         break; \
3710     case 2: \
3711         gen_helper_neon_##name##_s16(tmp, cpu_env, tmp, tmp2); \
3712         break; \
3713     case 3: \
3714         gen_helper_neon_##name##_u16(tmp, cpu_env, tmp, tmp2); \
3715         break; \
3716     case 4: \
3717         gen_helper_neon_##name##_s32(tmp, cpu_env, tmp, tmp2); \
3718         break; \
3719     case 5: \
3720         gen_helper_neon_##name##_u32(tmp, cpu_env, tmp, tmp2); \
3721         break; \
3722     default: return 1; \
3723     }} while (0)
3724
3725 #define GEN_NEON_INTEGER_OP(name) do { \
3726     switch ((size << 1) | u) { \
3727     case 0: \
3728         gen_helper_neon_##name##_s8(tmp, tmp, tmp2); \
3729         break; \
3730     case 1: \
3731         gen_helper_neon_##name##_u8(tmp, tmp, tmp2); \
3732         break; \
3733     case 2: \
3734         gen_helper_neon_##name##_s16(tmp, tmp, tmp2); \
3735         break; \
3736     case 3: \
3737         gen_helper_neon_##name##_u16(tmp, tmp, tmp2); \
3738         break; \
3739     case 4: \
3740         gen_helper_neon_##name##_s32(tmp, tmp, tmp2); \
3741         break; \
3742     case 5: \
3743         gen_helper_neon_##name##_u32(tmp, tmp, tmp2); \
3744         break; \
3745     default: return 1; \
3746     }} while (0)
3747
3748 static TCGv neon_load_scratch(int scratch)
3749 {
3750     TCGv tmp = tcg_temp_new_i32();
3751     tcg_gen_ld_i32(tmp, cpu_env, offsetof(CPUARMState, vfp.scratch[scratch]));
3752     return tmp;
3753 }
3754
3755 static void neon_store_scratch(int scratch, TCGv var)
3756 {
3757     tcg_gen_st_i32(var, cpu_env, offsetof(CPUARMState, vfp.scratch[scratch]));
3758     tcg_temp_free_i32(var);
3759 }
3760
3761 static inline TCGv neon_get_scalar(int size, int reg)
3762 {
3763     TCGv tmp;
3764     if (size == 1) {
3765         tmp = neon_load_reg(reg & 7, reg >> 4);
3766         if (reg & 8) {
3767             gen_neon_dup_high16(tmp);
3768         } else {
3769             gen_neon_dup_low16(tmp);
3770         }
3771     } else {
3772         tmp = neon_load_reg(reg & 15, reg >> 4);
3773     }
3774     return tmp;
3775 }
3776
3777 static int gen_neon_unzip(int rd, int rm, int size, int q)
3778 {
3779     TCGv tmp, tmp2;
3780     if (!q && size == 2) {
3781         return 1;
3782     }
3783     tmp = tcg_const_i32(rd);
3784     tmp2 = tcg_const_i32(rm);
3785     if (q) {
3786         switch (size) {
3787         case 0:
3788             gen_helper_neon_qunzip8(cpu_env, tmp, tmp2);
3789             break;
3790         case 1:
3791             gen_helper_neon_qunzip16(cpu_env, tmp, tmp2);
3792             break;
3793         case 2:
3794             gen_helper_neon_qunzip32(cpu_env, tmp, tmp2);
3795             break;
3796         default:
3797             abort();
3798         }
3799     } else {
3800         switch (size) {
3801         case 0:
3802             gen_helper_neon_unzip8(cpu_env, tmp, tmp2);
3803             break;
3804         case 1:
3805             gen_helper_neon_unzip16(cpu_env, tmp, tmp2);
3806             break;
3807         default:
3808             abort();
3809         }
3810     }
3811     tcg_temp_free_i32(tmp);
3812     tcg_temp_free_i32(tmp2);
3813     return 0;
3814 }
3815
3816 static int gen_neon_zip(int rd, int rm, int size, int q)
3817 {
3818     TCGv tmp, tmp2;
3819     if (!q && size == 2) {
3820         return 1;
3821     }
3822     tmp = tcg_const_i32(rd);
3823     tmp2 = tcg_const_i32(rm);
3824     if (q) {
3825         switch (size) {
3826         case 0:
3827             gen_helper_neon_qzip8(cpu_env, tmp, tmp2);
3828             break;
3829         case 1:
3830             gen_helper_neon_qzip16(cpu_env, tmp, tmp2);
3831             break;
3832         case 2:
3833             gen_helper_neon_qzip32(cpu_env, tmp, tmp2);
3834             break;
3835         default:
3836             abort();
3837         }
3838     } else {
3839         switch (size) {
3840         case 0:
3841             gen_helper_neon_zip8(cpu_env, tmp, tmp2);
3842             break;
3843         case 1:
3844             gen_helper_neon_zip16(cpu_env, tmp, tmp2);
3845             break;
3846         default:
3847             abort();
3848         }
3849     }
3850     tcg_temp_free_i32(tmp);
3851     tcg_temp_free_i32(tmp2);
3852     return 0;
3853 }
3854
3855 static void gen_neon_trn_u8(TCGv t0, TCGv t1)
3856 {
3857     TCGv rd, tmp;
3858
3859     rd = tcg_temp_new_i32();
3860     tmp = tcg_temp_new_i32();
3861
3862     tcg_gen_shli_i32(rd, t0, 8);
3863     tcg_gen_andi_i32(rd, rd, 0xff00ff00);
3864     tcg_gen_andi_i32(tmp, t1, 0x00ff00ff);
3865     tcg_gen_or_i32(rd, rd, tmp);
3866
3867     tcg_gen_shri_i32(t1, t1, 8);
3868     tcg_gen_andi_i32(t1, t1, 0x00ff00ff);
3869     tcg_gen_andi_i32(tmp, t0, 0xff00ff00);
3870     tcg_gen_or_i32(t1, t1, tmp);
3871     tcg_gen_mov_i32(t0, rd);
3872
3873     tcg_temp_free_i32(tmp);
3874     tcg_temp_free_i32(rd);
3875 }
3876
3877 static void gen_neon_trn_u16(TCGv t0, TCGv t1)
3878 {
3879     TCGv rd, tmp;
3880
3881     rd = tcg_temp_new_i32();
3882     tmp = tcg_temp_new_i32();
3883
3884     tcg_gen_shli_i32(rd, t0, 16);
3885     tcg_gen_andi_i32(tmp, t1, 0xffff);
3886     tcg_gen_or_i32(rd, rd, tmp);
3887     tcg_gen_shri_i32(t1, t1, 16);
3888     tcg_gen_andi_i32(tmp, t0, 0xffff0000);
3889     tcg_gen_or_i32(t1, t1, tmp);
3890     tcg_gen_mov_i32(t0, rd);
3891
3892     tcg_temp_free_i32(tmp);
3893     tcg_temp_free_i32(rd);
3894 }
3895
3896
3897 static struct {
3898     int nregs;
3899     int interleave;
3900     int spacing;
3901 } neon_ls_element_type[11] = {
3902     {4, 4, 1},
3903     {4, 4, 2},
3904     {4, 1, 1},
3905     {4, 2, 1},
3906     {3, 3, 1},
3907     {3, 3, 2},
3908     {3, 1, 1},
3909     {1, 1, 1},
3910     {2, 2, 1},
3911     {2, 2, 2},
3912     {2, 1, 1}
3913 };
3914
3915 /* Translate a NEON load/store element instruction.  Return nonzero if the
3916    instruction is invalid.  */
3917 static int disas_neon_ls_insn(CPUARMState * env, DisasContext *s, uint32_t insn)
3918 {
3919     int rd, rn, rm;
3920     int op;
3921     int nregs;
3922     int interleave;
3923     int spacing;
3924     int stride;
3925     int size;
3926     int reg;
3927     int pass;
3928     int load;
3929     int shift;
3930     int n;
3931     TCGv addr;
3932     TCGv tmp;
3933     TCGv tmp2;
3934     TCGv_i64 tmp64;
3935
3936     if (!s->vfp_enabled)
3937       return 1;
3938     VFP_DREG_D(rd, insn);
3939     rn = (insn >> 16) & 0xf;
3940     rm = insn & 0xf;
3941     load = (insn & (1 << 21)) != 0;
3942     if ((insn & (1 << 23)) == 0) {
3943         /* Load store all elements.  */
3944         op = (insn >> 8) & 0xf;
3945         size = (insn >> 6) & 3;
3946         if (op > 10)
3947             return 1;
3948         /* Catch UNDEF cases for bad values of align field */
3949         switch (op & 0xc) {
3950         case 4:
3951             if (((insn >> 5) & 1) == 1) {
3952                 return 1;
3953             }
3954             break;
3955         case 8:
3956             if (((insn >> 4) & 3) == 3) {
3957                 return 1;
3958             }
3959             break;
3960         default:
3961             break;
3962         }
3963         nregs = neon_ls_element_type[op].nregs;
3964         interleave = neon_ls_element_type[op].interleave;
3965         spacing = neon_ls_element_type[op].spacing;
3966         if (size == 3 && (interleave | spacing) != 1)
3967             return 1;
3968         addr = tcg_temp_new_i32();
3969         load_reg_var(s, addr, rn);
3970         stride = (1 << size) * interleave;
3971         for (reg = 0; reg < nregs; reg++) {
3972             if (interleave > 2 || (interleave == 2 && nregs == 2)) {
3973                 load_reg_var(s, addr, rn);
3974                 tcg_gen_addi_i32(addr, addr, (1 << size) * reg);
3975             } else if (interleave == 2 && nregs == 4 && reg == 2) {
3976                 load_reg_var(s, addr, rn);
3977                 tcg_gen_addi_i32(addr, addr, 1 << size);
3978             }
3979             if (size == 3) {
3980                 if (load) {
3981                     tmp64 = gen_ld64(addr, IS_USER(s));
3982                     neon_store_reg64(tmp64, rd);
3983                     tcg_temp_free_i64(tmp64);
3984                 } else {
3985                     tmp64 = tcg_temp_new_i64();
3986                     neon_load_reg64(tmp64, rd);
3987                     gen_st64(tmp64, addr, IS_USER(s));
3988                 }
3989                 tcg_gen_addi_i32(addr, addr, stride);
3990             } else {
3991                 for (pass = 0; pass < 2; pass++) {
3992                     if (size == 2) {
3993                         if (load) {
3994                             tmp = gen_ld32(addr, IS_USER(s));
3995                             neon_store_reg(rd, pass, tmp);
3996                         } else {
3997                             tmp = neon_load_reg(rd, pass);
3998                             gen_st32(tmp, addr, IS_USER(s));
3999                         }
4000                         tcg_gen_addi_i32(addr, addr, stride);
4001                     } else if (size == 1) {
4002                         if (load) {
4003                             tmp = gen_ld16u(addr, IS_USER(s));
4004                             tcg_gen_addi_i32(addr, addr, stride);
4005                             tmp2 = gen_ld16u(addr, IS_USER(s));
4006                             tcg_gen_addi_i32(addr, addr, stride);
4007                             tcg_gen_shli_i32(tmp2, tmp2, 16);
4008                             tcg_gen_or_i32(tmp, tmp, tmp2);
4009                             tcg_temp_free_i32(tmp2);
4010                             neon_store_reg(rd, pass, tmp);
4011                         } else {
4012                             tmp = neon_load_reg(rd, pass);
4013                             tmp2 = tcg_temp_new_i32();
4014                             tcg_gen_shri_i32(tmp2, tmp, 16);
4015                             gen_st16(tmp, addr, IS_USER(s));
4016                             tcg_gen_addi_i32(addr, addr, stride);
4017                             gen_st16(tmp2, addr, IS_USER(s));
4018                             tcg_gen_addi_i32(addr, addr, stride);
4019                         }
4020                     } else /* size == 0 */ {
4021                         if (load) {
4022                             TCGV_UNUSED(tmp2);
4023                             for (n = 0; n < 4; n++) {
4024                                 tmp = gen_ld8u(addr, IS_USER(s));
4025                                 tcg_gen_addi_i32(addr, addr, stride);
4026                                 if (n == 0) {
4027                                     tmp2 = tmp;
4028                                 } else {
4029                                     tcg_gen_shli_i32(tmp, tmp, n * 8);
4030                                     tcg_gen_or_i32(tmp2, tmp2, tmp);
4031                                     tcg_temp_free_i32(tmp);
4032                                 }
4033                             }
4034                             neon_store_reg(rd, pass, tmp2);
4035                         } else {
4036                             tmp2 = neon_load_reg(rd, pass);
4037                             for (n = 0; n < 4; n++) {
4038                                 tmp = tcg_temp_new_i32();
4039                                 if (n == 0) {
4040                                     tcg_gen_mov_i32(tmp, tmp2);
4041                                 } else {
4042                                     tcg_gen_shri_i32(tmp, tmp2, n * 8);
4043                                 }
4044                                 gen_st8(tmp, addr, IS_USER(s));
4045                                 tcg_gen_addi_i32(addr, addr, stride);
4046                             }
4047                             tcg_temp_free_i32(tmp2);
4048                         }
4049                     }
4050                 }
4051             }
4052             rd += spacing;
4053         }
4054         tcg_temp_free_i32(addr);
4055         stride = nregs * 8;
4056     } else {
4057         size = (insn >> 10) & 3;
4058         if (size == 3) {
4059             /* Load single element to all lanes.  */
4060             int a = (insn >> 4) & 1;
4061             if (!load) {
4062                 return 1;
4063             }
4064             size = (insn >> 6) & 3;
4065             nregs = ((insn >> 8) & 3) + 1;
4066
4067             if (size == 3) {
4068                 if (nregs != 4 || a == 0) {
4069                     return 1;
4070                 }
4071                 /* For VLD4 size==3 a == 1 means 32 bits at 16 byte alignment */
4072                 size = 2;
4073             }
4074             if (nregs == 1 && a == 1 && size == 0) {
4075                 return 1;
4076             }
4077             if (nregs == 3 && a == 1) {
4078                 return 1;
4079             }
4080             addr = tcg_temp_new_i32();
4081             load_reg_var(s, addr, rn);
4082             if (nregs == 1) {
4083                 /* VLD1 to all lanes: bit 5 indicates how many Dregs to write */
4084                 tmp = gen_load_and_replicate(s, addr, size);
4085                 tcg_gen_st_i32(tmp, cpu_env, neon_reg_offset(rd, 0));
4086                 tcg_gen_st_i32(tmp, cpu_env, neon_reg_offset(rd, 1));
4087                 if (insn & (1 << 5)) {
4088                     tcg_gen_st_i32(tmp, cpu_env, neon_reg_offset(rd + 1, 0));
4089                     tcg_gen_st_i32(tmp, cpu_env, neon_reg_offset(rd + 1, 1));
4090                 }
4091                 tcg_temp_free_i32(tmp);
4092             } else {
4093                 /* VLD2/3/4 to all lanes: bit 5 indicates register stride */
4094                 stride = (insn & (1 << 5)) ? 2 : 1;
4095                 for (reg = 0; reg < nregs; reg++) {
4096                     tmp = gen_load_and_replicate(s, addr, size);
4097                     tcg_gen_st_i32(tmp, cpu_env, neon_reg_offset(rd, 0));
4098                     tcg_gen_st_i32(tmp, cpu_env, neon_reg_offset(rd, 1));
4099                     tcg_temp_free_i32(tmp);
4100                     tcg_gen_addi_i32(addr, addr, 1 << size);
4101                     rd += stride;
4102                 }
4103             }
4104             tcg_temp_free_i32(addr);
4105             stride = (1 << size) * nregs;
4106         } else {
4107             /* Single element.  */
4108             int idx = (insn >> 4) & 0xf;
4109             pass = (insn >> 7) & 1;
4110             switch (size) {
4111             case 0:
4112                 shift = ((insn >> 5) & 3) * 8;
4113                 stride = 1;
4114                 break;
4115             case 1:
4116                 shift = ((insn >> 6) & 1) * 16;
4117                 stride = (insn & (1 << 5)) ? 2 : 1;
4118                 break;
4119             case 2:
4120                 shift = 0;
4121                 stride = (insn & (1 << 6)) ? 2 : 1;
4122                 break;
4123             default:
4124                 abort();
4125             }
4126             nregs = ((insn >> 8) & 3) + 1;
4127             /* Catch the UNDEF cases. This is unavoidably a bit messy. */
4128             switch (nregs) {
4129             case 1:
4130                 if (((idx & (1 << size)) != 0) ||
4131                     (size == 2 && ((idx & 3) == 1 || (idx & 3) == 2))) {
4132                     return 1;
4133                 }
4134                 break;
4135             case 3:
4136                 if ((idx & 1) != 0) {
4137                     return 1;
4138                 }
4139                 /* fall through */
4140             case 2:
4141                 if (size == 2 && (idx & 2) != 0) {
4142                     return 1;
4143                 }
4144                 break;
4145             case 4:
4146                 if ((size == 2) && ((idx & 3) == 3)) {
4147                     return 1;
4148                 }
4149                 break;
4150             default:
4151                 abort();
4152             }
4153             if ((rd + stride * (nregs - 1)) > 31) {
4154                 /* Attempts to write off the end of the register file
4155                  * are UNPREDICTABLE; we choose to UNDEF because otherwise
4156                  * the neon_load_reg() would write off the end of the array.
4157                  */
4158                 return 1;
4159             }
4160             addr = tcg_temp_new_i32();
4161             load_reg_var(s, addr, rn);
4162             for (reg = 0; reg < nregs; reg++) {
4163                 if (load) {
4164                     switch (size) {
4165                     case 0:
4166                         tmp = gen_ld8u(addr, IS_USER(s));
4167                         break;
4168                     case 1:
4169                         tmp = gen_ld16u(addr, IS_USER(s));
4170                         break;
4171                     case 2:
4172                         tmp = gen_ld32(addr, IS_USER(s));
4173                         break;
4174                     default: /* Avoid compiler warnings.  */
4175                         abort();
4176                     }
4177                     if (size != 2) {
4178                         tmp2 = neon_load_reg(rd, pass);
4179                         gen_bfi(tmp, tmp2, tmp, shift, size ? 0xffff : 0xff);
4180                         tcg_temp_free_i32(tmp2);
4181                     }
4182                     neon_store_reg(rd, pass, tmp);
4183                 } else { /* Store */
4184                     tmp = neon_load_reg(rd, pass);
4185                     if (shift)
4186                         tcg_gen_shri_i32(tmp, tmp, shift);
4187                     switch (size) {
4188                     case 0:
4189                         gen_st8(tmp, addr, IS_USER(s));
4190                         break;
4191                     case 1:
4192                         gen_st16(tmp, addr, IS_USER(s));
4193                         break;
4194                     case 2:
4195                         gen_st32(tmp, addr, IS_USER(s));
4196                         break;
4197                     }
4198                 }
4199                 rd += stride;
4200                 tcg_gen_addi_i32(addr, addr, 1 << size);
4201             }
4202             tcg_temp_free_i32(addr);
4203             stride = nregs * (1 << size);
4204         }
4205     }
4206     if (rm != 15) {
4207         TCGv base;
4208
4209         base = load_reg(s, rn);
4210         if (rm == 13) {
4211             tcg_gen_addi_i32(base, base, stride);
4212         } else {
4213             TCGv index;
4214             index = load_reg(s, rm);
4215             tcg_gen_add_i32(base, base, index);
4216             tcg_temp_free_i32(index);
4217         }
4218         store_reg(s, rn, base);
4219     }
4220     return 0;
4221 }
4222
4223 /* Bitwise select.  dest = c ? t : f.  Clobbers T and F.  */
4224 static void gen_neon_bsl(TCGv dest, TCGv t, TCGv f, TCGv c)
4225 {
4226     tcg_gen_and_i32(t, t, c);
4227     tcg_gen_andc_i32(f, f, c);
4228     tcg_gen_or_i32(dest, t, f);
4229 }
4230
4231 static inline void gen_neon_narrow(int size, TCGv dest, TCGv_i64 src)
4232 {
4233     switch (size) {
4234     case 0: gen_helper_neon_narrow_u8(dest, src); break;
4235     case 1: gen_helper_neon_narrow_u16(dest, src); break;
4236     case 2: tcg_gen_trunc_i64_i32(dest, src); break;
4237     default: abort();
4238     }
4239 }
4240
4241 static inline void gen_neon_narrow_sats(int size, TCGv dest, TCGv_i64 src)
4242 {
4243     switch (size) {
4244     case 0: gen_helper_neon_narrow_sat_s8(dest, cpu_env, src); break;
4245     case 1: gen_helper_neon_narrow_sat_s16(dest, cpu_env, src); break;
4246     case 2: gen_helper_neon_narrow_sat_s32(dest, cpu_env, src); break;
4247     default: abort();
4248     }
4249 }
4250
4251 static inline void gen_neon_narrow_satu(int size, TCGv dest, TCGv_i64 src)
4252 {
4253     switch (size) {
4254     case 0: gen_helper_neon_narrow_sat_u8(dest, cpu_env, src); break;
4255     case 1: gen_helper_neon_narrow_sat_u16(dest, cpu_env, src); break;
4256     case 2: gen_helper_neon_narrow_sat_u32(dest, cpu_env, src); break;
4257     default: abort();
4258     }
4259 }
4260
4261 static inline void gen_neon_unarrow_sats(int size, TCGv dest, TCGv_i64 src)
4262 {
4263     switch (size) {
4264     case 0: gen_helper_neon_unarrow_sat8(dest, cpu_env, src); break;
4265     case 1: gen_helper_neon_unarrow_sat16(dest, cpu_env, src); break;
4266     case 2: gen_helper_neon_unarrow_sat32(dest, cpu_env, src); break;
4267     default: abort();
4268     }
4269 }
4270
4271 static inline void gen_neon_shift_narrow(int size, TCGv var, TCGv shift,
4272                                          int q, int u)
4273 {
4274     if (q) {
4275         if (u) {
4276             switch (size) {
4277             case 1: gen_helper_neon_rshl_u16(var, var, shift); break;
4278             case 2: gen_helper_neon_rshl_u32(var, var, shift); break;
4279             default: abort();
4280             }
4281         } else {
4282             switch (size) {
4283             case 1: gen_helper_neon_rshl_s16(var, var, shift); break;
4284             case 2: gen_helper_neon_rshl_s32(var, var, shift); break;
4285             default: abort();
4286             }
4287         }
4288     } else {
4289         if (u) {
4290             switch (size) {
4291             case 1: gen_helper_neon_shl_u16(var, var, shift); break;
4292             case 2: gen_helper_neon_shl_u32(var, var, shift); break;
4293             default: abort();
4294             }
4295         } else {
4296             switch (size) {
4297             case 1: gen_helper_neon_shl_s16(var, var, shift); break;
4298             case 2: gen_helper_neon_shl_s32(var, var, shift); break;
4299             default: abort();
4300             }
4301         }
4302     }
4303 }
4304
4305 static inline void gen_neon_widen(TCGv_i64 dest, TCGv src, int size, int u)
4306 {
4307     if (u) {
4308         switch (size) {
4309         case 0: gen_helper_neon_widen_u8(dest, src); break;
4310         case 1: gen_helper_neon_widen_u16(dest, src); break;
4311         case 2: tcg_gen_extu_i32_i64(dest, src); break;
4312         default: abort();
4313         }
4314     } else {
4315         switch (size) {
4316         case 0: gen_helper_neon_widen_s8(dest, src); break;
4317         case 1: gen_helper_neon_widen_s16(dest, src); break;
4318         case 2: tcg_gen_ext_i32_i64(dest, src); break;
4319         default: abort();
4320         }
4321     }
4322     tcg_temp_free_i32(src);
4323 }
4324
4325 static inline void gen_neon_addl(int size)
4326 {
4327     switch (size) {
4328     case 0: gen_helper_neon_addl_u16(CPU_V001); break;
4329     case 1: gen_helper_neon_addl_u32(CPU_V001); break;
4330     case 2: tcg_gen_add_i64(CPU_V001); break;
4331     default: abort();
4332     }
4333 }
4334
4335 static inline void gen_neon_subl(int size)
4336 {
4337     switch (size) {
4338     case 0: gen_helper_neon_subl_u16(CPU_V001); break;
4339     case 1: gen_helper_neon_subl_u32(CPU_V001); break;
4340     case 2: tcg_gen_sub_i64(CPU_V001); break;
4341     default: abort();
4342     }
4343 }
4344
4345 static inline void gen_neon_negl(TCGv_i64 var, int size)
4346 {
4347     switch (size) {
4348     case 0: gen_helper_neon_negl_u16(var, var); break;
4349     case 1: gen_helper_neon_negl_u32(var, var); break;
4350     case 2: gen_helper_neon_negl_u64(var, var); break;
4351     default: abort();
4352     }
4353 }
4354
4355 static inline void gen_neon_addl_saturate(TCGv_i64 op0, TCGv_i64 op1, int size)
4356 {
4357     switch (size) {
4358     case 1: gen_helper_neon_addl_saturate_s32(op0, cpu_env, op0, op1); break;
4359     case 2: gen_helper_neon_addl_saturate_s64(op0, cpu_env, op0, op1); break;
4360     default: abort();
4361     }
4362 }
4363
4364 static inline void gen_neon_mull(TCGv_i64 dest, TCGv a, TCGv b, int size, int u)
4365 {
4366     TCGv_i64 tmp;
4367
4368     switch ((size << 1) | u) {
4369     case 0: gen_helper_neon_mull_s8(dest, a, b); break;
4370     case 1: gen_helper_neon_mull_u8(dest, a, b); break;
4371     case 2: gen_helper_neon_mull_s16(dest, a, b); break;
4372     case 3: gen_helper_neon_mull_u16(dest, a, b); break;
4373     case 4:
4374         tmp = gen_muls_i64_i32(a, b);
4375         tcg_gen_mov_i64(dest, tmp);
4376         tcg_temp_free_i64(tmp);
4377         break;
4378     case 5:
4379         tmp = gen_mulu_i64_i32(a, b);
4380         tcg_gen_mov_i64(dest, tmp);
4381         tcg_temp_free_i64(tmp);
4382         break;
4383     default: abort();
4384     }
4385
4386     /* gen_helper_neon_mull_[su]{8|16} do not free their parameters.
4387        Don't forget to clean them now.  */
4388     if (size < 2) {
4389         tcg_temp_free_i32(a);
4390         tcg_temp_free_i32(b);
4391     }
4392 }
4393
4394 static void gen_neon_narrow_op(int op, int u, int size, TCGv dest, TCGv_i64 src)
4395 {
4396     if (op) {
4397         if (u) {
4398             gen_neon_unarrow_sats(size, dest, src);
4399         } else {
4400             gen_neon_narrow(size, dest, src);
4401         }
4402     } else {
4403         if (u) {
4404             gen_neon_narrow_satu(size, dest, src);
4405         } else {
4406             gen_neon_narrow_sats(size, dest, src);
4407         }
4408     }
4409 }
4410
4411 /* Symbolic constants for op fields for Neon 3-register same-length.
4412  * The values correspond to bits [11:8,4]; see the ARM ARM DDI0406B
4413  * table A7-9.
4414  */
4415 #define NEON_3R_VHADD 0
4416 #define NEON_3R_VQADD 1
4417 #define NEON_3R_VRHADD 2
4418 #define NEON_3R_LOGIC 3 /* VAND,VBIC,VORR,VMOV,VORN,VEOR,VBIF,VBIT,VBSL */
4419 #define NEON_3R_VHSUB 4
4420 #define NEON_3R_VQSUB 5
4421 #define NEON_3R_VCGT 6
4422 #define NEON_3R_VCGE 7
4423 #define NEON_3R_VSHL 8
4424 #define NEON_3R_VQSHL 9
4425 #define NEON_3R_VRSHL 10
4426 #define NEON_3R_VQRSHL 11
4427 #define NEON_3R_VMAX 12
4428 #define NEON_3R_VMIN 13
4429 #define NEON_3R_VABD 14
4430 #define NEON_3R_VABA 15
4431 #define NEON_3R_VADD_VSUB 16
4432 #define NEON_3R_VTST_VCEQ 17
4433 #define NEON_3R_VML 18 /* VMLA, VMLAL, VMLS, VMLSL */
4434 #define NEON_3R_VMUL 19
4435 #define NEON_3R_VPMAX 20
4436 #define NEON_3R_VPMIN 21
4437 #define NEON_3R_VQDMULH_VQRDMULH 22
4438 #define NEON_3R_VPADD 23
4439 #define NEON_3R_VFM 25 /* VFMA, VFMS : float fused multiply-add */
4440 #define NEON_3R_FLOAT_ARITH 26 /* float VADD, VSUB, VPADD, VABD */
4441 #define NEON_3R_FLOAT_MULTIPLY 27 /* float VMLA, VMLS, VMUL */
4442 #define NEON_3R_FLOAT_CMP 28 /* float VCEQ, VCGE, VCGT */
4443 #define NEON_3R_FLOAT_ACMP 29 /* float VACGE, VACGT, VACLE, VACLT */
4444 #define NEON_3R_FLOAT_MINMAX 30 /* float VMIN, VMAX */
4445 #define NEON_3R_VRECPS_VRSQRTS 31 /* float VRECPS, VRSQRTS */
4446
4447 static const uint8_t neon_3r_sizes[] = {
4448     [NEON_3R_VHADD] = 0x7,
4449     [NEON_3R_VQADD] = 0xf,
4450     [NEON_3R_VRHADD] = 0x7,
4451     [NEON_3R_LOGIC] = 0xf, /* size field encodes op type */
4452     [NEON_3R_VHSUB] = 0x7,
4453     [NEON_3R_VQSUB] = 0xf,
4454     [NEON_3R_VCGT] = 0x7,
4455     [NEON_3R_VCGE] = 0x7,
4456     [NEON_3R_VSHL] = 0xf,
4457     [NEON_3R_VQSHL] = 0xf,
4458     [NEON_3R_VRSHL] = 0xf,
4459     [NEON_3R_VQRSHL] = 0xf,
4460     [NEON_3R_VMAX] = 0x7,
4461     [NEON_3R_VMIN] = 0x7,
4462     [NEON_3R_VABD] = 0x7,
4463     [NEON_3R_VABA] = 0x7,
4464     [NEON_3R_VADD_VSUB] = 0xf,
4465     [NEON_3R_VTST_VCEQ] = 0x7,
4466     [NEON_3R_VML] = 0x7,
4467     [NEON_3R_VMUL] = 0x7,
4468     [NEON_3R_VPMAX] = 0x7,
4469     [NEON_3R_VPMIN] = 0x7,
4470     [NEON_3R_VQDMULH_VQRDMULH] = 0x6,
4471     [NEON_3R_VPADD] = 0x7,
4472     [NEON_3R_VFM] = 0x5, /* size bit 1 encodes op */
4473     [NEON_3R_FLOAT_ARITH] = 0x5, /* size bit 1 encodes op */
4474     [NEON_3R_FLOAT_MULTIPLY] = 0x5, /* size bit 1 encodes op */
4475     [NEON_3R_FLOAT_CMP] = 0x5, /* size bit 1 encodes op */
4476     [NEON_3R_FLOAT_ACMP] = 0x5, /* size bit 1 encodes op */
4477     [NEON_3R_FLOAT_MINMAX] = 0x5, /* size bit 1 encodes op */
4478     [NEON_3R_VRECPS_VRSQRTS] = 0x5, /* size bit 1 encodes op */
4479 };
4480
4481 /* Symbolic constants for op fields for Neon 2-register miscellaneous.
4482  * The values correspond to bits [17:16,10:7]; see the ARM ARM DDI0406B
4483  * table A7-13.
4484  */
4485 #define NEON_2RM_VREV64 0
4486 #define NEON_2RM_VREV32 1
4487 #define NEON_2RM_VREV16 2
4488 #define NEON_2RM_VPADDL 4
4489 #define NEON_2RM_VPADDL_U 5
4490 #define NEON_2RM_VCLS 8
4491 #define NEON_2RM_VCLZ 9
4492 #define NEON_2RM_VCNT 10
4493 #define NEON_2RM_VMVN 11
4494 #define NEON_2RM_VPADAL 12
4495 #define NEON_2RM_VPADAL_U 13
4496 #define NEON_2RM_VQABS 14
4497 #define NEON_2RM_VQNEG 15
4498 #define NEON_2RM_VCGT0 16
4499 #define NEON_2RM_VCGE0 17
4500 #define NEON_2RM_VCEQ0 18
4501 #define NEON_2RM_VCLE0 19
4502 #define NEON_2RM_VCLT0 20
4503 #define NEON_2RM_VABS 22
4504 #define NEON_2RM_VNEG 23
4505 #define NEON_2RM_VCGT0_F 24
4506 #define NEON_2RM_VCGE0_F 25
4507 #define NEON_2RM_VCEQ0_F 26
4508 #define NEON_2RM_VCLE0_F 27
4509 #define NEON_2RM_VCLT0_F 28
4510 #define NEON_2RM_VABS_F 30
4511 #define NEON_2RM_VNEG_F 31
4512 #define NEON_2RM_VSWP 32
4513 #define NEON_2RM_VTRN 33
4514 #define NEON_2RM_VUZP 34
4515 #define NEON_2RM_VZIP 35
4516 #define NEON_2RM_VMOVN 36 /* Includes VQMOVN, VQMOVUN */
4517 #define NEON_2RM_VQMOVN 37 /* Includes VQMOVUN */
4518 #define NEON_2RM_VSHLL 38
4519 #define NEON_2RM_VCVT_F16_F32 44
4520 #define NEON_2RM_VCVT_F32_F16 46
4521 #define NEON_2RM_VRECPE 56
4522 #define NEON_2RM_VRSQRTE 57
4523 #define NEON_2RM_VRECPE_F 58
4524 #define NEON_2RM_VRSQRTE_F 59
4525 #define NEON_2RM_VCVT_FS 60
4526 #define NEON_2RM_VCVT_FU 61
4527 #define NEON_2RM_VCVT_SF 62
4528 #define NEON_2RM_VCVT_UF 63
4529
4530 static int neon_2rm_is_float_op(int op)
4531 {
4532     /* Return true if this neon 2reg-misc op is float-to-float */
4533     return (op == NEON_2RM_VABS_F || op == NEON_2RM_VNEG_F ||
4534             op >= NEON_2RM_VRECPE_F);
4535 }
4536
4537 /* Each entry in this array has bit n set if the insn allows
4538  * size value n (otherwise it will UNDEF). Since unallocated
4539  * op values will have no bits set they always UNDEF.
4540  */
4541 static const uint8_t neon_2rm_sizes[] = {
4542     [NEON_2RM_VREV64] = 0x7,
4543     [NEON_2RM_VREV32] = 0x3,
4544     [NEON_2RM_VREV16] = 0x1,
4545     [NEON_2RM_VPADDL] = 0x7,
4546     [NEON_2RM_VPADDL_U] = 0x7,
4547     [NEON_2RM_VCLS] = 0x7,
4548     [NEON_2RM_VCLZ] = 0x7,
4549     [NEON_2RM_VCNT] = 0x1,
4550     [NEON_2RM_VMVN] = 0x1,
4551     [NEON_2RM_VPADAL] = 0x7,
4552     [NEON_2RM_VPADAL_U] = 0x7,
4553     [NEON_2RM_VQABS] = 0x7,
4554     [NEON_2RM_VQNEG] = 0x7,
4555     [NEON_2RM_VCGT0] = 0x7,
4556     [NEON_2RM_VCGE0] = 0x7,
4557     [NEON_2RM_VCEQ0] = 0x7,
4558     [NEON_2RM_VCLE0] = 0x7,
4559     [NEON_2RM_VCLT0] = 0x7,
4560     [NEON_2RM_VABS] = 0x7,
4561     [NEON_2RM_VNEG] = 0x7,
4562     [NEON_2RM_VCGT0_F] = 0x4,
4563     [NEON_2RM_VCGE0_F] = 0x4,
4564     [NEON_2RM_VCEQ0_F] = 0x4,
4565     [NEON_2RM_VCLE0_F] = 0x4,
4566     [NEON_2RM_VCLT0_F] = 0x4,
4567     [NEON_2RM_VABS_F] = 0x4,
4568     [NEON_2RM_VNEG_F] = 0x4,
4569     [NEON_2RM_VSWP] = 0x1,
4570     [NEON_2RM_VTRN] = 0x7,
4571     [NEON_2RM_VUZP] = 0x7,
4572     [NEON_2RM_VZIP] = 0x7,
4573     [NEON_2RM_VMOVN] = 0x7,
4574     [NEON_2RM_VQMOVN] = 0x7,
4575     [NEON_2RM_VSHLL] = 0x7,
4576     [NEON_2RM_VCVT_F16_F32] = 0x2,
4577     [NEON_2RM_VCVT_F32_F16] = 0x2,
4578     [NEON_2RM_VRECPE] = 0x4,
4579     [NEON_2RM_VRSQRTE] = 0x4,
4580     [NEON_2RM_VRECPE_F] = 0x4,
4581     [NEON_2RM_VRSQRTE_F] = 0x4,
4582     [NEON_2RM_VCVT_FS] = 0x4,
4583     [NEON_2RM_VCVT_FU] = 0x4,
4584     [NEON_2RM_VCVT_SF] = 0x4,
4585     [NEON_2RM_VCVT_UF] = 0x4,
4586 };
4587
4588 /* Translate a NEON data processing instruction.  Return nonzero if the
4589    instruction is invalid.
4590    We process data in a mixture of 32-bit and 64-bit chunks.
4591    Mostly we use 32-bit chunks so we can use normal scalar instructions.  */
4592
4593 static int disas_neon_data_insn(CPUARMState * env, DisasContext *s, uint32_t insn)
4594 {
4595     int op;
4596     int q;
4597     int rd, rn, rm;
4598     int size;
4599     int shift;
4600     int pass;
4601     int count;
4602     int pairwise;
4603     int u;
4604     uint32_t imm, mask;
4605     TCGv tmp, tmp2, tmp3, tmp4, tmp5;
4606     TCGv_i64 tmp64;
4607
4608     if (!s->vfp_enabled)
4609       return 1;
4610     q = (insn & (1 << 6)) != 0;
4611     u = (insn >> 24) & 1;
4612     VFP_DREG_D(rd, insn);
4613     VFP_DREG_N(rn, insn);
4614     VFP_DREG_M(rm, insn);
4615     size = (insn >> 20) & 3;
4616     if ((insn & (1 << 23)) == 0) {
4617         /* Three register same length.  */
4618         op = ((insn >> 7) & 0x1e) | ((insn >> 4) & 1);
4619         /* Catch invalid op and bad size combinations: UNDEF */
4620         if ((neon_3r_sizes[op] & (1 << size)) == 0) {
4621             return 1;
4622         }
4623         /* All insns of this form UNDEF for either this condition or the
4624          * superset of cases "Q==1"; we catch the latter later.
4625          */
4626         if (q && ((rd | rn | rm) & 1)) {
4627             return 1;
4628         }
4629         if (size == 3 && op != NEON_3R_LOGIC) {
4630             /* 64-bit element instructions. */
4631             for (pass = 0; pass < (q ? 2 : 1); pass++) {
4632                 neon_load_reg64(cpu_V0, rn + pass);
4633                 neon_load_reg64(cpu_V1, rm + pass);
4634                 switch (op) {
4635                 case NEON_3R_VQADD:
4636                     if (u) {
4637                         gen_helper_neon_qadd_u64(cpu_V0, cpu_env,
4638                                                  cpu_V0, cpu_V1);
4639                     } else {
4640                         gen_helper_neon_qadd_s64(cpu_V0, cpu_env,
4641                                                  cpu_V0, cpu_V1);
4642                     }
4643                     break;
4644                 case NEON_3R_VQSUB:
4645                     if (u) {
4646                         gen_helper_neon_qsub_u64(cpu_V0, cpu_env,
4647                                                  cpu_V0, cpu_V1);
4648                     } else {
4649                         gen_helper_neon_qsub_s64(cpu_V0, cpu_env,
4650                                                  cpu_V0, cpu_V1);
4651                     }
4652                     break;
4653                 case NEON_3R_VSHL:
4654                     if (u) {
4655                         gen_helper_neon_shl_u64(cpu_V0, cpu_V1, cpu_V0);
4656                     } else {
4657                         gen_helper_neon_shl_s64(cpu_V0, cpu_V1, cpu_V0);
4658                     }
4659                     break;
4660                 case NEON_3R_VQSHL:
4661                     if (u) {
4662                         gen_helper_neon_qshl_u64(cpu_V0, cpu_env,
4663                                                  cpu_V1, cpu_V0);
4664                     } else {
4665                         gen_helper_neon_qshl_s64(cpu_V0, cpu_env,
4666                                                  cpu_V1, cpu_V0);
4667                     }
4668                     break;
4669                 case NEON_3R_VRSHL:
4670                     if (u) {
4671                         gen_helper_neon_rshl_u64(cpu_V0, cpu_V1, cpu_V0);
4672                     } else {
4673                         gen_helper_neon_rshl_s64(cpu_V0, cpu_V1, cpu_V0);
4674                     }
4675                     break;
4676                 case NEON_3R_VQRSHL:
4677                     if (u) {
4678                         gen_helper_neon_qrshl_u64(cpu_V0, cpu_env,
4679                                                   cpu_V1, cpu_V0);
4680                     } else {
4681                         gen_helper_neon_qrshl_s64(cpu_V0, cpu_env,
4682                                                   cpu_V1, cpu_V0);
4683                     }
4684                     break;
4685                 case NEON_3R_VADD_VSUB:
4686                     if (u) {
4687                         tcg_gen_sub_i64(CPU_V001);
4688                     } else {
4689                         tcg_gen_add_i64(CPU_V001);
4690                     }
4691                     break;
4692                 default:
4693                     abort();
4694                 }
4695                 neon_store_reg64(cpu_V0, rd + pass);
4696             }
4697             return 0;
4698         }
4699         pairwise = 0;
4700         switch (op) {
4701         case NEON_3R_VSHL:
4702         case NEON_3R_VQSHL:
4703         case NEON_3R_VRSHL:
4704         case NEON_3R_VQRSHL:
4705             {
4706                 int rtmp;
4707                 /* Shift instruction operands are reversed.  */
4708                 rtmp = rn;
4709                 rn = rm;
4710                 rm = rtmp;
4711             }
4712             break;
4713         case NEON_3R_VPADD:
4714             if (u) {
4715                 return 1;
4716             }
4717             /* Fall through */
4718         case NEON_3R_VPMAX:
4719         case NEON_3R_VPMIN:
4720             pairwise = 1;
4721             break;
4722         case NEON_3R_FLOAT_ARITH:
4723             pairwise = (u && size < 2); /* if VPADD (float) */
4724             break;
4725         case NEON_3R_FLOAT_MINMAX:
4726             pairwise = u; /* if VPMIN/VPMAX (float) */
4727             break;
4728         case NEON_3R_FLOAT_CMP:
4729             if (!u && size) {
4730                 /* no encoding for U=0 C=1x */
4731                 return 1;
4732             }
4733             break;
4734         case NEON_3R_FLOAT_ACMP:
4735             if (!u) {
4736                 return 1;
4737             }
4738             break;
4739         case NEON_3R_VRECPS_VRSQRTS:
4740             if (u) {
4741                 return 1;
4742             }
4743             break;
4744         case NEON_3R_VMUL:
4745             if (u && (size != 0)) {
4746                 /* UNDEF on invalid size for polynomial subcase */
4747                 return 1;
4748             }
4749             break;
4750         case NEON_3R_VFM:
4751             if (!arm_feature(env, ARM_FEATURE_VFP4) || u) {
4752                 return 1;
4753             }
4754             break;
4755         default:
4756             break;
4757         }
4758
4759         if (pairwise && q) {
4760             /* All the pairwise insns UNDEF if Q is set */
4761             return 1;
4762         }
4763
4764         for (pass = 0; pass < (q ? 4 : 2); pass++) {
4765
4766         if (pairwise) {
4767             /* Pairwise.  */
4768             if (pass < 1) {
4769                 tmp = neon_load_reg(rn, 0);
4770                 tmp2 = neon_load_reg(rn, 1);
4771             } else {
4772                 tmp = neon_load_reg(rm, 0);
4773                 tmp2 = neon_load_reg(rm, 1);
4774             }
4775         } else {
4776             /* Elementwise.  */
4777             tmp = neon_load_reg(rn, pass);
4778             tmp2 = neon_load_reg(rm, pass);
4779         }
4780         switch (op) {
4781         case NEON_3R_VHADD:
4782             GEN_NEON_INTEGER_OP(hadd);
4783             break;
4784         case NEON_3R_VQADD:
4785             GEN_NEON_INTEGER_OP_ENV(qadd);
4786             break;
4787         case NEON_3R_VRHADD:
4788             GEN_NEON_INTEGER_OP(rhadd);
4789             break;
4790         case NEON_3R_LOGIC: /* Logic ops.  */
4791             switch ((u << 2) | size) {
4792             case 0: /* VAND */
4793                 tcg_gen_and_i32(tmp, tmp, tmp2);
4794                 break;
4795             case 1: /* BIC */
4796                 tcg_gen_andc_i32(tmp, tmp, tmp2);
4797                 break;
4798             case 2: /* VORR */
4799                 tcg_gen_or_i32(tmp, tmp, tmp2);
4800                 break;
4801             case 3: /* VORN */
4802                 tcg_gen_orc_i32(tmp, tmp, tmp2);
4803                 break;
4804             case 4: /* VEOR */
4805                 tcg_gen_xor_i32(tmp, tmp, tmp2);
4806                 break;
4807             case 5: /* VBSL */
4808                 tmp3 = neon_load_reg(rd, pass);
4809                 gen_neon_bsl(tmp, tmp, tmp2, tmp3);
4810                 tcg_temp_free_i32(tmp3);
4811                 break;
4812             case 6: /* VBIT */
4813                 tmp3 = neon_load_reg(rd, pass);
4814                 gen_neon_bsl(tmp, tmp, tmp3, tmp2);
4815                 tcg_temp_free_i32(tmp3);
4816                 break;
4817             case 7: /* VBIF */
4818                 tmp3 = neon_load_reg(rd, pass);
4819                 gen_neon_bsl(tmp, tmp3, tmp, tmp2);
4820                 tcg_temp_free_i32(tmp3);
4821                 break;
4822             }
4823             break;
4824         case NEON_3R_VHSUB:
4825             GEN_NEON_INTEGER_OP(hsub);
4826             break;
4827         case NEON_3R_VQSUB:
4828             GEN_NEON_INTEGER_OP_ENV(qsub);
4829             break;
4830         case NEON_3R_VCGT:
4831             GEN_NEON_INTEGER_OP(cgt);
4832             break;
4833         case NEON_3R_VCGE:
4834             GEN_NEON_INTEGER_OP(cge);
4835             break;
4836         case NEON_3R_VSHL:
4837             GEN_NEON_INTEGER_OP(shl);
4838             break;
4839         case NEON_3R_VQSHL:
4840             GEN_NEON_INTEGER_OP_ENV(qshl);
4841             break;
4842         case NEON_3R_VRSHL:
4843             GEN_NEON_INTEGER_OP(rshl);
4844             break;
4845         case NEON_3R_VQRSHL:
4846             GEN_NEON_INTEGER_OP_ENV(qrshl);
4847             break;
4848         case NEON_3R_VMAX:
4849             GEN_NEON_INTEGER_OP(max);
4850             break;
4851         case NEON_3R_VMIN:
4852             GEN_NEON_INTEGER_OP(min);
4853             break;
4854         case NEON_3R_VABD:
4855             GEN_NEON_INTEGER_OP(abd);
4856             break;
4857         case NEON_3R_VABA:
4858             GEN_NEON_INTEGER_OP(abd);
4859             tcg_temp_free_i32(tmp2);
4860             tmp2 = neon_load_reg(rd, pass);
4861             gen_neon_add(size, tmp, tmp2);
4862             break;
4863         case NEON_3R_VADD_VSUB:
4864             if (!u) { /* VADD */
4865                 gen_neon_add(size, tmp, tmp2);
4866             } else { /* VSUB */
4867                 switch (size) {
4868                 case 0: gen_helper_neon_sub_u8(tmp, tmp, tmp2); break;
4869                 case 1: gen_helper_neon_sub_u16(tmp, tmp, tmp2); break;
4870                 case 2: tcg_gen_sub_i32(tmp, tmp, tmp2); break;
4871                 default: abort();
4872                 }
4873             }
4874             break;
4875         case NEON_3R_VTST_VCEQ:
4876             if (!u) { /* VTST */
4877                 switch (size) {
4878                 case 0: gen_helper_neon_tst_u8(tmp, tmp, tmp2); break;
4879                 case 1: gen_helper_neon_tst_u16(tmp, tmp, tmp2); break;
4880                 case 2: gen_helper_neon_tst_u32(tmp, tmp, tmp2); break;
4881                 default: abort();
4882                 }
4883             } else { /* VCEQ */
4884                 switch (size) {
4885                 case 0: gen_helper_neon_ceq_u8(tmp, tmp, tmp2); break;
4886                 case 1: gen_helper_neon_ceq_u16(tmp, tmp, tmp2); break;
4887                 case 2: gen_helper_neon_ceq_u32(tmp, tmp, tmp2); break;
4888                 default: abort();
4889                 }
4890             }
4891             break;
4892         case NEON_3R_VML: /* VMLA, VMLAL, VMLS,VMLSL */
4893             switch (size) {
4894             case 0: gen_helper_neon_mul_u8(tmp, tmp, tmp2); break;
4895             case 1: gen_helper_neon_mul_u16(tmp, tmp, tmp2); break;
4896             case 2: tcg_gen_mul_i32(tmp, tmp, tmp2); break;
4897             default: abort();
4898             }
4899             tcg_temp_free_i32(tmp2);
4900             tmp2 = neon_load_reg(rd, pass);
4901             if (u) { /* VMLS */
4902                 gen_neon_rsb(size, tmp, tmp2);
4903             } else { /* VMLA */
4904                 gen_neon_add(size, tmp, tmp2);
4905             }
4906             break;
4907         case NEON_3R_VMUL:
4908             if (u) { /* polynomial */
4909                 gen_helper_neon_mul_p8(tmp, tmp, tmp2);
4910             } else { /* Integer */
4911                 switch (size) {
4912                 case 0: gen_helper_neon_mul_u8(tmp, tmp, tmp2); break;
4913                 case 1: gen_helper_neon_mul_u16(tmp, tmp, tmp2); break;
4914                 case 2: tcg_gen_mul_i32(tmp, tmp, tmp2); break;
4915                 default: abort();
4916                 }
4917             }
4918             break;
4919         case NEON_3R_VPMAX:
4920             GEN_NEON_INTEGER_OP(pmax);
4921             break;
4922         case NEON_3R_VPMIN:
4923             GEN_NEON_INTEGER_OP(pmin);
4924             break;
4925         case NEON_3R_VQDMULH_VQRDMULH: /* Multiply high.  */
4926             if (!u) { /* VQDMULH */
4927                 switch (size) {
4928                 case 1:
4929                     gen_helper_neon_qdmulh_s16(tmp, cpu_env, tmp, tmp2);
4930                     break;
4931                 case 2:
4932                     gen_helper_neon_qdmulh_s32(tmp, cpu_env, tmp, tmp2);
4933                     break;
4934                 default: abort();
4935                 }
4936             } else { /* VQRDMULH */
4937                 switch (size) {
4938                 case 1:
4939                     gen_helper_neon_qrdmulh_s16(tmp, cpu_env, tmp, tmp2);
4940                     break;
4941                 case 2:
4942                     gen_helper_neon_qrdmulh_s32(tmp, cpu_env, tmp, tmp2);
4943                     break;
4944                 default: abort();
4945                 }
4946             }
4947             break;
4948         case NEON_3R_VPADD:
4949             switch (size) {
4950             case 0: gen_helper_neon_padd_u8(tmp, tmp, tmp2); break;
4951             case 1: gen_helper_neon_padd_u16(tmp, tmp, tmp2); break;
4952             case 2: tcg_gen_add_i32(tmp, tmp, tmp2); break;
4953             default: abort();
4954             }
4955             break;
4956         case NEON_3R_FLOAT_ARITH: /* Floating point arithmetic. */
4957         {
4958             TCGv_ptr fpstatus = get_fpstatus_ptr(1);
4959             switch ((u << 2) | size) {
4960             case 0: /* VADD */
4961             case 4: /* VPADD */
4962                 gen_helper_vfp_adds(tmp, tmp, tmp2, fpstatus);
4963                 break;
4964             case 2: /* VSUB */
4965                 gen_helper_vfp_subs(tmp, tmp, tmp2, fpstatus);
4966                 break;
4967             case 6: /* VABD */
4968                 gen_helper_neon_abd_f32(tmp, tmp, tmp2, fpstatus);
4969                 break;
4970             default:
4971                 abort();
4972             }
4973             tcg_temp_free_ptr(fpstatus);
4974             break;
4975         }
4976         case NEON_3R_FLOAT_MULTIPLY:
4977         {
4978             TCGv_ptr fpstatus = get_fpstatus_ptr(1);
4979             gen_helper_vfp_muls(tmp, tmp, tmp2, fpstatus);
4980             if (!u) {
4981                 tcg_temp_free_i32(tmp2);
4982                 tmp2 = neon_load_reg(rd, pass);
4983                 if (size == 0) {
4984                     gen_helper_vfp_adds(tmp, tmp, tmp2, fpstatus);
4985                 } else {
4986                     gen_helper_vfp_subs(tmp, tmp2, tmp, fpstatus);
4987                 }
4988             }
4989             tcg_temp_free_ptr(fpstatus);
4990             break;
4991         }
4992         case NEON_3R_FLOAT_CMP:
4993         {
4994             TCGv_ptr fpstatus = get_fpstatus_ptr(1);
4995             if (!u) {
4996                 gen_helper_neon_ceq_f32(tmp, tmp, tmp2, fpstatus);
4997             } else {
4998                 if (size == 0) {
4999                     gen_helper_neon_cge_f32(tmp, tmp, tmp2, fpstatus);
5000                 } else {
5001                     gen_helper_neon_cgt_f32(tmp, tmp, tmp2, fpstatus);
5002                 }
5003             }
5004             tcg_temp_free_ptr(fpstatus);
5005             break;
5006         }
5007         case NEON_3R_FLOAT_ACMP:
5008         {
5009             TCGv_ptr fpstatus = get_fpstatus_ptr(1);
5010             if (size == 0) {
5011                 gen_helper_neon_acge_f32(tmp, tmp, tmp2, fpstatus);
5012             } else {
5013                 gen_helper_neon_acgt_f32(tmp, tmp, tmp2, fpstatus);
5014             }
5015             tcg_temp_free_ptr(fpstatus);
5016             break;
5017         }
5018         case NEON_3R_FLOAT_MINMAX:
5019         {
5020             TCGv_ptr fpstatus = get_fpstatus_ptr(1);
5021             if (size == 0) {
5022                 gen_helper_neon_max_f32(tmp, tmp, tmp2, fpstatus);
5023             } else {
5024                 gen_helper_neon_min_f32(tmp, tmp, tmp2, fpstatus);
5025             }
5026             tcg_temp_free_ptr(fpstatus);
5027             break;
5028         }
5029         case NEON_3R_VRECPS_VRSQRTS:
5030             if (size == 0)
5031                 gen_helper_recps_f32(tmp, tmp, tmp2, cpu_env);
5032             else
5033                 gen_helper_rsqrts_f32(tmp, tmp, tmp2, cpu_env);
5034             break;
5035         case NEON_3R_VFM:
5036         {
5037             /* VFMA, VFMS: fused multiply-add */
5038             TCGv_ptr fpstatus = get_fpstatus_ptr(1);
5039             TCGv_i32 tmp3 = neon_load_reg(rd, pass);
5040             if (size) {
5041                 /* VFMS */
5042                 gen_helper_vfp_negs(tmp, tmp);
5043             }
5044             gen_helper_vfp_muladds(tmp, tmp, tmp2, tmp3, fpstatus);
5045             tcg_temp_free_i32(tmp3);
5046             tcg_temp_free_ptr(fpstatus);
5047             break;
5048         }
5049         default:
5050             abort();
5051         }
5052         tcg_temp_free_i32(tmp2);
5053
5054         /* Save the result.  For elementwise operations we can put it
5055            straight into the destination register.  For pairwise operations
5056            we have to be careful to avoid clobbering the source operands.  */
5057         if (pairwise && rd == rm) {
5058             neon_store_scratch(pass, tmp);
5059         } else {
5060             neon_store_reg(rd, pass, tmp);
5061         }
5062
5063         } /* for pass */
5064         if (pairwise && rd == rm) {
5065             for (pass = 0; pass < (q ? 4 : 2); pass++) {
5066                 tmp = neon_load_scratch(pass);
5067                 neon_store_reg(rd, pass, tmp);
5068             }
5069         }
5070         /* End of 3 register same size operations.  */
5071     } else if (insn & (1 << 4)) {
5072         if ((insn & 0x00380080) != 0) {
5073             /* Two registers and shift.  */
5074             op = (insn >> 8) & 0xf;
5075             if (insn & (1 << 7)) {
5076                 /* 64-bit shift. */
5077                 if (op > 7) {
5078                     return 1;
5079                 }
5080                 size = 3;
5081             } else {
5082                 size = 2;
5083                 while ((insn & (1 << (size + 19))) == 0)
5084                     size--;
5085             }
5086             shift = (insn >> 16) & ((1 << (3 + size)) - 1);
5087             /* To avoid excessive dumplication of ops we implement shift
5088                by immediate using the variable shift operations.  */
5089             if (op < 8) {
5090                 /* Shift by immediate:
5091                    VSHR, VSRA, VRSHR, VRSRA, VSRI, VSHL, VQSHL, VQSHLU.  */
5092                 if (q && ((rd | rm) & 1)) {
5093                     return 1;
5094                 }
5095                 if (!u && (op == 4 || op == 6)) {
5096                     return 1;
5097                 }
5098                 /* Right shifts are encoded as N - shift, where N is the
5099                    element size in bits.  */
5100                 if (op <= 4)
5101                     shift = shift - (1 << (size + 3));
5102                 if (size == 3) {
5103                     count = q + 1;
5104                 } else {
5105                     count = q ? 4: 2;
5106                 }
5107                 switch (size) {
5108                 case 0:
5109                     imm = (uint8_t) shift;
5110                     imm |= imm << 8;
5111                     imm |= imm << 16;
5112                     break;
5113                 case 1:
5114                     imm = (uint16_t) shift;
5115                     imm |= imm << 16;
5116                     break;
5117                 case 2:
5118                 case 3:
5119                     imm = shift;
5120                     break;
5121                 default:
5122                     abort();
5123                 }
5124
5125                 for (pass = 0; pass < count; pass++) {
5126                     if (size == 3) {
5127                         neon_load_reg64(cpu_V0, rm + pass);
5128                         tcg_gen_movi_i64(cpu_V1, imm);
5129                         switch (op) {
5130                         case 0:  /* VSHR */
5131                         case 1:  /* VSRA */
5132                             if (u)
5133                                 gen_helper_neon_shl_u64(cpu_V0, cpu_V0, cpu_V1);
5134                             else
5135                                 gen_helper_neon_shl_s64(cpu_V0, cpu_V0, cpu_V1);
5136                             break;
5137                         case 2: /* VRSHR */
5138                         case 3: /* VRSRA */
5139                             if (u)
5140                                 gen_helper_neon_rshl_u64(cpu_V0, cpu_V0, cpu_V1);
5141                             else
5142                                 gen_helper_neon_rshl_s64(cpu_V0, cpu_V0, cpu_V1);
5143                             break;
5144                         case 4: /* VSRI */
5145                         case 5: /* VSHL, VSLI */
5146                             gen_helper_neon_shl_u64(cpu_V0, cpu_V0, cpu_V1);
5147                             break;
5148                         case 6: /* VQSHLU */
5149                             gen_helper_neon_qshlu_s64(cpu_V0, cpu_env,
5150                                                       cpu_V0, cpu_V1);
5151                             break;
5152                         case 7: /* VQSHL */
5153                             if (u) {
5154                                 gen_helper_neon_qshl_u64(cpu_V0, cpu_env,
5155                                                          cpu_V0, cpu_V1);
5156                             } else {
5157                                 gen_helper_neon_qshl_s64(cpu_V0, cpu_env,
5158                                                          cpu_V0, cpu_V1);
5159                             }
5160                             break;
5161                         }
5162                         if (op == 1 || op == 3) {
5163                             /* Accumulate.  */
5164                             neon_load_reg64(cpu_V1, rd + pass);
5165                             tcg_gen_add_i64(cpu_V0, cpu_V0, cpu_V1);
5166                         } else if (op == 4 || (op == 5 && u)) {
5167                             /* Insert */
5168                             neon_load_reg64(cpu_V1, rd + pass);
5169                             uint64_t mask;
5170                             if (shift < -63 || shift > 63) {
5171                                 mask = 0;
5172                             } else {
5173                                 if (op == 4) {
5174                                     mask = 0xffffffffffffffffull >> -shift;
5175                                 } else {
5176                                     mask = 0xffffffffffffffffull << shift;
5177                                 }
5178                             }
5179                             tcg_gen_andi_i64(cpu_V1, cpu_V1, ~mask);
5180                             tcg_gen_or_i64(cpu_V0, cpu_V0, cpu_V1);
5181                         }
5182                         neon_store_reg64(cpu_V0, rd + pass);
5183                     } else { /* size < 3 */
5184                         /* Operands in T0 and T1.  */
5185                         tmp = neon_load_reg(rm, pass);
5186                         tmp2 = tcg_temp_new_i32();
5187                         tcg_gen_movi_i32(tmp2, imm);
5188                         switch (op) {
5189                         case 0:  /* VSHR */
5190                         case 1:  /* VSRA */
5191                             GEN_NEON_INTEGER_OP(shl);
5192                             break;
5193                         case 2: /* VRSHR */
5194                         case 3: /* VRSRA */
5195                             GEN_NEON_INTEGER_OP(rshl);
5196                             break;
5197                         case 4: /* VSRI */
5198                         case 5: /* VSHL, VSLI */
5199                             switch (size) {
5200                             case 0: gen_helper_neon_shl_u8(tmp, tmp, tmp2); break;
5201                             case 1: gen_helper_neon_shl_u16(tmp, tmp, tmp2); break;
5202                             case 2: gen_helper_neon_shl_u32(tmp, tmp, tmp2); break;
5203                             default: abort();
5204                             }
5205                             break;
5206                         case 6: /* VQSHLU */
5207                             switch (size) {
5208                             case 0:
5209                                 gen_helper_neon_qshlu_s8(tmp, cpu_env,
5210                                                          tmp, tmp2);
5211                                 break;
5212                             case 1:
5213                                 gen_helper_neon_qshlu_s16(tmp, cpu_env,
5214                                                           tmp, tmp2);
5215                                 break;
5216                             case 2:
5217                                 gen_helper_neon_qshlu_s32(tmp, cpu_env,
5218                                                           tmp, tmp2);
5219                                 break;
5220                             default:
5221                                 abort();
5222                             }
5223                             break;
5224                         case 7: /* VQSHL */
5225                             GEN_NEON_INTEGER_OP_ENV(qshl);
5226                             break;
5227                         }
5228                         tcg_temp_free_i32(tmp2);
5229
5230                         if (op == 1 || op == 3) {
5231                             /* Accumulate.  */
5232                             tmp2 = neon_load_reg(rd, pass);
5233                             gen_neon_add(size, tmp, tmp2);
5234                             tcg_temp_free_i32(tmp2);
5235                         } else if (op == 4 || (op == 5 && u)) {
5236                             /* Insert */
5237                             switch (size) {
5238                             case 0:
5239                                 if (op == 4)
5240                                     mask = 0xff >> -shift;
5241                                 else
5242                                     mask = (uint8_t)(0xff << shift);
5243                                 mask |= mask << 8;
5244                                 mask |= mask << 16;
5245                                 break;
5246                             case 1:
5247                                 if (op == 4)
5248                                     mask = 0xffff >> -shift;
5249                                 else
5250                                     mask = (uint16_t)(0xffff << shift);
5251                                 mask |= mask << 16;
5252                                 break;
5253                             case 2:
5254                                 if (shift < -31 || shift > 31) {
5255                                     mask = 0;
5256                                 } else {
5257                                     if (op == 4)
5258                                         mask = 0xffffffffu >> -shift;
5259                                     else
5260                                         mask = 0xffffffffu << shift;
5261                                 }
5262                                 break;
5263                             default:
5264                                 abort();
5265                             }
5266                             tmp2 = neon_load_reg(rd, pass);
5267                             tcg_gen_andi_i32(tmp, tmp, mask);
5268                             tcg_gen_andi_i32(tmp2, tmp2, ~mask);
5269                             tcg_gen_or_i32(tmp, tmp, tmp2);
5270                             tcg_temp_free_i32(tmp2);
5271                         }
5272                         neon_store_reg(rd, pass, tmp);
5273                     }
5274                 } /* for pass */
5275             } else if (op < 10) {
5276                 /* Shift by immediate and narrow:
5277                    VSHRN, VRSHRN, VQSHRN, VQRSHRN.  */
5278                 int input_unsigned = (op == 8) ? !u : u;
5279                 if (rm & 1) {
5280                     return 1;
5281                 }
5282                 shift = shift - (1 << (size + 3));
5283                 size++;
5284                 if (size == 3) {
5285                     tmp64 = tcg_const_i64(shift);
5286                     neon_load_reg64(cpu_V0, rm);
5287                     neon_load_reg64(cpu_V1, rm + 1);
5288                     for (pass = 0; pass < 2; pass++) {
5289                         TCGv_i64 in;
5290                         if (pass == 0) {
5291                             in = cpu_V0;
5292                         } else {
5293                             in = cpu_V1;
5294                         }
5295                         if (q) {
5296                             if (input_unsigned) {
5297                                 gen_helper_neon_rshl_u64(cpu_V0, in, tmp64);
5298                             } else {
5299                                 gen_helper_neon_rshl_s64(cpu_V0, in, tmp64);
5300                             }
5301                         } else {
5302                             if (input_unsigned) {
5303                                 gen_helper_neon_shl_u64(cpu_V0, in, tmp64);
5304                             } else {
5305                                 gen_helper_neon_shl_s64(cpu_V0, in, tmp64);
5306                             }
5307                         }
5308                         tmp = tcg_temp_new_i32();
5309                         gen_neon_narrow_op(op == 8, u, size - 1, tmp, cpu_V0);
5310                         neon_store_reg(rd, pass, tmp);
5311                     } /* for pass */
5312                     tcg_temp_free_i64(tmp64);
5313                 } else {
5314                     if (size == 1) {
5315                         imm = (uint16_t)shift;
5316                         imm |= imm << 16;
5317                     } else {
5318                         /* size == 2 */
5319                         imm = (uint32_t)shift;
5320                     }
5321                     tmp2 = tcg_const_i32(imm);
5322                     tmp4 = neon_load_reg(rm + 1, 0);
5323                     tmp5 = neon_load_reg(rm + 1, 1);
5324                     for (pass = 0; pass < 2; pass++) {
5325                         if (pass == 0) {
5326                             tmp = neon_load_reg(rm, 0);
5327                         } else {
5328                             tmp = tmp4;
5329                         }
5330                         gen_neon_shift_narrow(size, tmp, tmp2, q,
5331                                               input_unsigned);
5332                         if (pass == 0) {
5333                             tmp3 = neon_load_reg(rm, 1);
5334                         } else {
5335                             tmp3 = tmp5;
5336                         }
5337                         gen_neon_shift_narrow(size, tmp3, tmp2, q,
5338                                               input_unsigned);
5339                         tcg_gen_concat_i32_i64(cpu_V0, tmp, tmp3);
5340                         tcg_temp_free_i32(tmp);
5341                         tcg_temp_free_i32(tmp3);
5342                         tmp = tcg_temp_new_i32();
5343                         gen_neon_narrow_op(op == 8, u, size - 1, tmp, cpu_V0);
5344                         neon_store_reg(rd, pass, tmp);
5345                     } /* for pass */
5346                     tcg_temp_free_i32(tmp2);
5347                 }
5348             } else if (op == 10) {
5349                 /* VSHLL, VMOVL */
5350                 if (q || (rd & 1)) {
5351                     return 1;
5352                 }
5353                 tmp = neon_load_reg(rm, 0);
5354                 tmp2 = neon_load_reg(rm, 1);
5355                 for (pass = 0; pass < 2; pass++) {
5356                     if (pass == 1)
5357                         tmp = tmp2;
5358
5359                     gen_neon_widen(cpu_V0, tmp, size, u);
5360
5361                     if (shift != 0) {
5362                         /* The shift is less than the width of the source
5363                            type, so we can just shift the whole register.  */
5364                         tcg_gen_shli_i64(cpu_V0, cpu_V0, shift);
5365                         /* Widen the result of shift: we need to clear
5366                          * the potential overflow bits resulting from
5367                          * left bits of the narrow input appearing as
5368                          * right bits of left the neighbour narrow
5369                          * input.  */
5370                         if (size < 2 || !u) {
5371                             uint64_t imm64;
5372                             if (size == 0) {
5373                                 imm = (0xffu >> (8 - shift));
5374                                 imm |= imm << 16;
5375                             } else if (size == 1) {
5376                                 imm = 0xffff >> (16 - shift);
5377                             } else {
5378                                 /* size == 2 */
5379                                 imm = 0xffffffff >> (32 - shift);
5380                             }
5381                             if (size < 2) {
5382                                 imm64 = imm | (((uint64_t)imm) << 32);
5383                             } else {
5384                                 imm64 = imm;
5385                             }
5386                             tcg_gen_andi_i64(cpu_V0, cpu_V0, ~imm64);
5387                         }
5388                     }
5389                     neon_store_reg64(cpu_V0, rd + pass);
5390                 }
5391             } else if (op >= 14) {
5392                 /* VCVT fixed-point.  */
5393                 if (!(insn & (1 << 21)) || (q && ((rd | rm) & 1))) {
5394                     return 1;
5395                 }
5396                 /* We have already masked out the must-be-1 top bit of imm6,
5397                  * hence this 32-shift where the ARM ARM has 64-imm6.
5398                  */
5399                 shift = 32 - shift;
5400                 for (pass = 0; pass < (q ? 4 : 2); pass++) {
5401                     tcg_gen_ld_f32(cpu_F0s, cpu_env, neon_reg_offset(rm, pass));
5402                     if (!(op & 1)) {
5403                         if (u)
5404                             gen_vfp_ulto(0, shift, 1);
5405                         else
5406                             gen_vfp_slto(0, shift, 1);
5407                     } else {
5408                         if (u)
5409                             gen_vfp_toul(0, shift, 1);
5410                         else
5411                             gen_vfp_tosl(0, shift, 1);
5412                     }
5413                     tcg_gen_st_f32(cpu_F0s, cpu_env, neon_reg_offset(rd, pass));
5414                 }
5415             } else {
5416                 return 1;
5417             }
5418         } else { /* (insn & 0x00380080) == 0 */
5419             int invert;
5420             if (q && (rd & 1)) {
5421                 return 1;
5422             }
5423
5424             op = (insn >> 8) & 0xf;
5425             /* One register and immediate.  */
5426             imm = (u << 7) | ((insn >> 12) & 0x70) | (insn & 0xf);
5427             invert = (insn & (1 << 5)) != 0;
5428             /* Note that op = 2,3,4,5,6,7,10,11,12,13 imm=0 is UNPREDICTABLE.
5429              * We choose to not special-case this and will behave as if a
5430              * valid constant encoding of 0 had been given.
5431              */
5432             switch (op) {
5433             case 0: case 1:
5434                 /* no-op */
5435                 break;
5436             case 2: case 3:
5437                 imm <<= 8;
5438                 break;
5439             case 4: case 5:
5440                 imm <<= 16;
5441                 break;
5442             case 6: case 7:
5443                 imm <<= 24;
5444                 break;
5445             case 8: case 9:
5446                 imm |= imm << 16;
5447                 break;
5448             case 10: case 11:
5449                 imm = (imm << 8) | (imm << 24);
5450                 break;
5451             case 12:
5452                 imm = (imm << 8) | 0xff;
5453                 break;
5454             case 13:
5455                 imm = (imm << 16) | 0xffff;
5456                 break;
5457             case 14:
5458                 imm |= (imm << 8) | (imm << 16) | (imm << 24);
5459                 if (invert)
5460                     imm = ~imm;
5461                 break;
5462             case 15:
5463                 if (invert) {
5464                     return 1;
5465                 }
5466                 imm = ((imm & 0x80) << 24) | ((imm & 0x3f) << 19)
5467                       | ((imm & 0x40) ? (0x1f << 25) : (1 << 30));
5468                 break;
5469             }
5470             if (invert)
5471                 imm = ~imm;
5472
5473             for (pass = 0; pass < (q ? 4 : 2); pass++) {
5474                 if (op & 1 && op < 12) {
5475                     tmp = neon_load_reg(rd, pass);
5476                     if (invert) {
5477                         /* The immediate value has already been inverted, so
5478                            BIC becomes AND.  */
5479                         tcg_gen_andi_i32(tmp, tmp, imm);
5480                     } else {
5481                         tcg_gen_ori_i32(tmp, tmp, imm);
5482                     }
5483                 } else {
5484                     /* VMOV, VMVN.  */
5485                     tmp = tcg_temp_new_i32();
5486                     if (op == 14 && invert) {
5487                         int n;
5488                         uint32_t val;
5489                         val = 0;
5490                         for (n = 0; n < 4; n++) {
5491                             if (imm & (1 << (n + (pass & 1) * 4)))
5492                                 val |= 0xff << (n * 8);
5493                         }
5494                         tcg_gen_movi_i32(tmp, val);
5495                     } else {
5496                         tcg_gen_movi_i32(tmp, imm);
5497                     }
5498                 }
5499                 neon_store_reg(rd, pass, tmp);
5500             }
5501         }
5502     } else { /* (insn & 0x00800010 == 0x00800000) */
5503         if (size != 3) {
5504             op = (insn >> 8) & 0xf;
5505             if ((insn & (1 << 6)) == 0) {
5506                 /* Three registers of different lengths.  */
5507                 int src1_wide;
5508                 int src2_wide;
5509                 int prewiden;
5510                 /* undefreq: bit 0 : UNDEF if size != 0
5511                  *           bit 1 : UNDEF if size == 0
5512                  *           bit 2 : UNDEF if U == 1
5513                  * Note that [1:0] set implies 'always UNDEF'
5514                  */
5515                 int undefreq;
5516                 /* prewiden, src1_wide, src2_wide, undefreq */
5517                 static const int neon_3reg_wide[16][4] = {
5518                     {1, 0, 0, 0}, /* VADDL */
5519                     {1, 1, 0, 0}, /* VADDW */
5520                     {1, 0, 0, 0}, /* VSUBL */
5521                     {1, 1, 0, 0}, /* VSUBW */
5522                     {0, 1, 1, 0}, /* VADDHN */
5523                     {0, 0, 0, 0}, /* VABAL */
5524                     {0, 1, 1, 0}, /* VSUBHN */
5525                     {0, 0, 0, 0}, /* VABDL */
5526                     {0, 0, 0, 0}, /* VMLAL */
5527                     {0, 0, 0, 6}, /* VQDMLAL */
5528                     {0, 0, 0, 0}, /* VMLSL */
5529                     {0, 0, 0, 6}, /* VQDMLSL */
5530                     {0, 0, 0, 0}, /* Integer VMULL */
5531                     {0, 0, 0, 2}, /* VQDMULL */
5532                     {0, 0, 0, 5}, /* Polynomial VMULL */
5533                     {0, 0, 0, 3}, /* Reserved: always UNDEF */
5534                 };
5535
5536                 prewiden = neon_3reg_wide[op][0];
5537                 src1_wide = neon_3reg_wide[op][1];
5538                 src2_wide = neon_3reg_wide[op][2];
5539                 undefreq = neon_3reg_wide[op][3];
5540
5541                 if (((undefreq & 1) && (size != 0)) ||
5542                     ((undefreq & 2) && (size == 0)) ||
5543                     ((undefreq & 4) && u)) {
5544                     return 1;
5545                 }
5546                 if ((src1_wide && (rn & 1)) ||
5547                     (src2_wide && (rm & 1)) ||
5548                     (!src2_wide && (rd & 1))) {
5549                     return 1;
5550                 }
5551
5552                 /* Avoid overlapping operands.  Wide source operands are
5553                    always aligned so will never overlap with wide
5554                    destinations in problematic ways.  */
5555                 if (rd == rm && !src2_wide) {
5556                     tmp = neon_load_reg(rm, 1);
5557                     neon_store_scratch(2, tmp);
5558                 } else if (rd == rn && !src1_wide) {
5559                     tmp = neon_load_reg(rn, 1);
5560                     neon_store_scratch(2, tmp);
5561                 }
5562                 TCGV_UNUSED(tmp3);
5563                 for (pass = 0; pass < 2; pass++) {
5564                     if (src1_wide) {
5565                         neon_load_reg64(cpu_V0, rn + pass);
5566                         TCGV_UNUSED(tmp);
5567                     } else {
5568                         if (pass == 1 && rd == rn) {
5569                             tmp = neon_load_scratch(2);
5570                         } else {
5571                             tmp = neon_load_reg(rn, pass);
5572                         }
5573                         if (prewiden) {
5574                             gen_neon_widen(cpu_V0, tmp, size, u);
5575                         }
5576                     }
5577                     if (src2_wide) {
5578                         neon_load_reg64(cpu_V1, rm + pass);
5579                         TCGV_UNUSED(tmp2);
5580                     } else {
5581                         if (pass == 1 && rd == rm) {
5582                             tmp2 = neon_load_scratch(2);
5583                         } else {
5584                             tmp2 = neon_load_reg(rm, pass);
5585                         }
5586                         if (prewiden) {
5587                             gen_neon_widen(cpu_V1, tmp2, size, u);
5588                         }
5589                     }
5590                     switch (op) {
5591                     case 0: case 1: case 4: /* VADDL, VADDW, VADDHN, VRADDHN */
5592                         gen_neon_addl(size);
5593                         break;
5594                     case 2: case 3: case 6: /* VSUBL, VSUBW, VSUBHN, VRSUBHN */
5595                         gen_neon_subl(size);
5596                         break;
5597                     case 5: case 7: /* VABAL, VABDL */
5598                         switch ((size << 1) | u) {
5599                         case 0:
5600                             gen_helper_neon_abdl_s16(cpu_V0, tmp, tmp2);
5601                             break;
5602                         case 1:
5603                             gen_helper_neon_abdl_u16(cpu_V0, tmp, tmp2);
5604                             break;
5605                         case 2:
5606                             gen_helper_neon_abdl_s32(cpu_V0, tmp, tmp2);
5607                             break;
5608                         case 3:
5609                             gen_helper_neon_abdl_u32(cpu_V0, tmp, tmp2);
5610                             break;
5611                         case 4:
5612                             gen_helper_neon_abdl_s64(cpu_V0, tmp, tmp2);
5613                             break;
5614                         case 5:
5615                             gen_helper_neon_abdl_u64(cpu_V0, tmp, tmp2);
5616                             break;
5617                         default: abort();
5618                         }
5619                         tcg_temp_free_i32(tmp2);
5620                         tcg_temp_free_i32(tmp);
5621                         break;
5622                     case 8: case 9: case 10: case 11: case 12: case 13:
5623                         /* VMLAL, VQDMLAL, VMLSL, VQDMLSL, VMULL, VQDMULL */
5624                         gen_neon_mull(cpu_V0, tmp, tmp2, size, u);
5625                         break;
5626                     case 14: /* Polynomial VMULL */
5627                         gen_helper_neon_mull_p8(cpu_V0, tmp, tmp2);
5628                         tcg_temp_free_i32(tmp2);
5629                         tcg_temp_free_i32(tmp);
5630                         break;
5631                     default: /* 15 is RESERVED: caught earlier  */
5632                         abort();
5633                     }
5634                     if (op == 13) {
5635                         /* VQDMULL */
5636                         gen_neon_addl_saturate(cpu_V0, cpu_V0, size);
5637                         neon_store_reg64(cpu_V0, rd + pass);
5638                     } else if (op == 5 || (op >= 8 && op <= 11)) {
5639                         /* Accumulate.  */
5640                         neon_load_reg64(cpu_V1, rd + pass);
5641                         switch (op) {
5642                         case 10: /* VMLSL */
5643                             gen_neon_negl(cpu_V0, size);
5644                             /* Fall through */
5645                         case 5: case 8: /* VABAL, VMLAL */
5646                             gen_neon_addl(size);
5647                             break;
5648                         case 9: case 11: /* VQDMLAL, VQDMLSL */
5649                             gen_neon_addl_saturate(cpu_V0, cpu_V0, size);
5650                             if (op == 11) {
5651                                 gen_neon_negl(cpu_V0, size);
5652                             }
5653                             gen_neon_addl_saturate(cpu_V0, cpu_V1, size);
5654                             break;
5655                         default:
5656                             abort();
5657                         }
5658                         neon_store_reg64(cpu_V0, rd + pass);
5659                     } else if (op == 4 || op == 6) {
5660                         /* Narrowing operation.  */
5661                         tmp = tcg_temp_new_i32();
5662                         if (!u) {
5663                             switch (size) {
5664                             case 0:
5665                                 gen_helper_neon_narrow_high_u8(tmp, cpu_V0);
5666                                 break;
5667                             case 1:
5668                                 gen_helper_neon_narrow_high_u16(tmp, cpu_V0);
5669                                 break;
5670                             case 2:
5671                                 tcg_gen_shri_i64(cpu_V0, cpu_V0, 32);
5672                                 tcg_gen_trunc_i64_i32(tmp, cpu_V0);
5673                                 break;
5674                             default: abort();
5675                             }
5676                         } else {
5677                             switch (size) {
5678                             case 0:
5679                                 gen_helper_neon_narrow_round_high_u8(tmp, cpu_V0);
5680                                 break;
5681                             case 1:
5682                                 gen_helper_neon_narrow_round_high_u16(tmp, cpu_V0);
5683                                 break;
5684                             case 2:
5685                                 tcg_gen_addi_i64(cpu_V0, cpu_V0, 1u << 31);
5686                                 tcg_gen_shri_i64(cpu_V0, cpu_V0, 32);
5687                                 tcg_gen_trunc_i64_i32(tmp, cpu_V0);
5688                                 break;
5689                             default: abort();
5690                             }
5691                         }
5692                         if (pass == 0) {
5693                             tmp3 = tmp;
5694                         } else {
5695                             neon_store_reg(rd, 0, tmp3);
5696                             neon_store_reg(rd, 1, tmp);
5697                         }
5698                     } else {
5699                         /* Write back the result.  */
5700                         neon_store_reg64(cpu_V0, rd + pass);
5701                     }
5702                 }
5703             } else {
5704                 /* Two registers and a scalar. NB that for ops of this form
5705                  * the ARM ARM labels bit 24 as Q, but it is in our variable
5706                  * 'u', not 'q'.
5707                  */
5708                 if (size == 0) {
5709                     return 1;
5710                 }
5711                 switch (op) {
5712                 case 1: /* Float VMLA scalar */
5713                 case 5: /* Floating point VMLS scalar */
5714                 case 9: /* Floating point VMUL scalar */
5715                     if (size == 1) {
5716                         return 1;
5717                     }
5718                     /* fall through */
5719                 case 0: /* Integer VMLA scalar */
5720                 case 4: /* Integer VMLS scalar */
5721                 case 8: /* Integer VMUL scalar */
5722                 case 12: /* VQDMULH scalar */
5723                 case 13: /* VQRDMULH scalar */
5724                     if (u && ((rd | rn) & 1)) {
5725                         return 1;
5726                     }
5727                     tmp = neon_get_scalar(size, rm);
5728                     neon_store_scratch(0, tmp);
5729                     for (pass = 0; pass < (u ? 4 : 2); pass++) {
5730                         tmp = neon_load_scratch(0);
5731                         tmp2 = neon_load_reg(rn, pass);
5732                         if (op == 12) {
5733                             if (size == 1) {
5734                                 gen_helper_neon_qdmulh_s16(tmp, cpu_env, tmp, tmp2);
5735                             } else {
5736                                 gen_helper_neon_qdmulh_s32(tmp, cpu_env, tmp, tmp2);
5737                             }
5738                         } else if (op == 13) {
5739                             if (size == 1) {
5740                                 gen_helper_neon_qrdmulh_s16(tmp, cpu_env, tmp, tmp2);
5741                             } else {
5742                                 gen_helper_neon_qrdmulh_s32(tmp, cpu_env, tmp, tmp2);
5743                             }
5744                         } else if (op & 1) {
5745                             TCGv_ptr fpstatus = get_fpstatus_ptr(1);
5746                             gen_helper_vfp_muls(tmp, tmp, tmp2, fpstatus);
5747                             tcg_temp_free_ptr(fpstatus);
5748                         } else {
5749                             switch (size) {
5750                             case 0: gen_helper_neon_mul_u8(tmp, tmp, tmp2); break;
5751                             case 1: gen_helper_neon_mul_u16(tmp, tmp, tmp2); break;
5752                             case 2: tcg_gen_mul_i32(tmp, tmp, tmp2); break;
5753                             default: abort();
5754                             }
5755                         }
5756                         tcg_temp_free_i32(tmp2);
5757                         if (op < 8) {
5758                             /* Accumulate.  */
5759                             tmp2 = neon_load_reg(rd, pass);
5760                             switch (op) {
5761                             case 0:
5762                                 gen_neon_add(size, tmp, tmp2);
5763                                 break;
5764                             case 1:
5765                             {
5766                                 TCGv_ptr fpstatus = get_fpstatus_ptr(1);
5767                                 gen_helper_vfp_adds(tmp, tmp, tmp2, fpstatus);
5768                                 tcg_temp_free_ptr(fpstatus);
5769                                 break;
5770                             }
5771                             case 4:
5772                                 gen_neon_rsb(size, tmp, tmp2);
5773                                 break;
5774                             case 5:
5775                             {
5776                                 TCGv_ptr fpstatus = get_fpstatus_ptr(1);
5777                                 gen_helper_vfp_subs(tmp, tmp2, tmp, fpstatus);
5778                                 tcg_temp_free_ptr(fpstatus);
5779                                 break;
5780                             }
5781                             default:
5782                                 abort();
5783                             }
5784                             tcg_temp_free_i32(tmp2);
5785                         }
5786                         neon_store_reg(rd, pass, tmp);
5787                     }
5788                     break;
5789                 case 3: /* VQDMLAL scalar */
5790                 case 7: /* VQDMLSL scalar */
5791                 case 11: /* VQDMULL scalar */
5792                     if (u == 1) {
5793                         return 1;
5794                     }
5795                     /* fall through */
5796                 case 2: /* VMLAL sclar */
5797                 case 6: /* VMLSL scalar */
5798                 case 10: /* VMULL scalar */
5799                     if (rd & 1) {
5800                         return 1;
5801                     }
5802                     tmp2 = neon_get_scalar(size, rm);
5803                     /* We need a copy of tmp2 because gen_neon_mull
5804                      * deletes it during pass 0.  */
5805                     tmp4 = tcg_temp_new_i32();
5806                     tcg_gen_mov_i32(tmp4, tmp2);
5807                     tmp3 = neon_load_reg(rn, 1);
5808
5809                     for (pass = 0; pass < 2; pass++) {
5810                         if (pass == 0) {
5811                             tmp = neon_load_reg(rn, 0);
5812                         } else {
5813                             tmp = tmp3;
5814                             tmp2 = tmp4;
5815                         }
5816                         gen_neon_mull(cpu_V0, tmp, tmp2, size, u);
5817                         if (op != 11) {
5818                             neon_load_reg64(cpu_V1, rd + pass);
5819                         }
5820                         switch (op) {
5821                         case 6:
5822                             gen_neon_negl(cpu_V0, size);
5823                             /* Fall through */
5824                         case 2:
5825                             gen_neon_addl(size);
5826                             break;
5827                         case 3: case 7:
5828                             gen_neon_addl_saturate(cpu_V0, cpu_V0, size);
5829                             if (op == 7) {
5830                                 gen_neon_negl(cpu_V0, size);
5831                             }
5832                             gen_neon_addl_saturate(cpu_V0, cpu_V1, size);
5833                             break;
5834                         case 10:
5835                             /* no-op */
5836                             break;
5837                         case 11:
5838                             gen_neon_addl_saturate(cpu_V0, cpu_V0, size);
5839                             break;
5840                         default:
5841                             abort();
5842                         }
5843                         neon_store_reg64(cpu_V0, rd + pass);
5844                     }
5845
5846
5847                     break;
5848                 default: /* 14 and 15 are RESERVED */
5849                     return 1;
5850                 }
5851             }
5852         } else { /* size == 3 */
5853             if (!u) {
5854                 /* Extract.  */
5855                 imm = (insn >> 8) & 0xf;
5856
5857                 if (imm > 7 && !q)
5858                     return 1;
5859
5860                 if (q && ((rd | rn | rm) & 1)) {
5861                     return 1;
5862                 }
5863
5864                 if (imm == 0) {
5865                     neon_load_reg64(cpu_V0, rn);
5866                     if (q) {
5867                         neon_load_reg64(cpu_V1, rn + 1);
5868                     }
5869                 } else if (imm == 8) {
5870                     neon_load_reg64(cpu_V0, rn + 1);
5871                     if (q) {
5872                         neon_load_reg64(cpu_V1, rm);
5873                     }
5874                 } else if (q) {
5875                     tmp64 = tcg_temp_new_i64();
5876                     if (imm < 8) {
5877                         neon_load_reg64(cpu_V0, rn);
5878                         neon_load_reg64(tmp64, rn + 1);
5879                     } else {
5880                         neon_load_reg64(cpu_V0, rn + 1);
5881                         neon_load_reg64(tmp64, rm);
5882                     }
5883                     tcg_gen_shri_i64(cpu_V0, cpu_V0, (imm & 7) * 8);
5884                     tcg_gen_shli_i64(cpu_V1, tmp64, 64 - ((imm & 7) * 8));
5885                     tcg_gen_or_i64(cpu_V0, cpu_V0, cpu_V1);
5886                     if (imm < 8) {
5887                         neon_load_reg64(cpu_V1, rm);
5888                     } else {
5889                         neon_load_reg64(cpu_V1, rm + 1);
5890                         imm -= 8;
5891                     }
5892                     tcg_gen_shli_i64(cpu_V1, cpu_V1, 64 - (imm * 8));
5893                     tcg_gen_shri_i64(tmp64, tmp64, imm * 8);
5894                     tcg_gen_or_i64(cpu_V1, cpu_V1, tmp64);
5895                     tcg_temp_free_i64(tmp64);
5896                 } else {
5897                     /* BUGFIX */
5898                     neon_load_reg64(cpu_V0, rn);
5899                     tcg_gen_shri_i64(cpu_V0, cpu_V0, imm * 8);
5900                     neon_load_reg64(cpu_V1, rm);
5901                     tcg_gen_shli_i64(cpu_V1, cpu_V1, 64 - (imm * 8));
5902                     tcg_gen_or_i64(cpu_V0, cpu_V0, cpu_V1);
5903                 }
5904                 neon_store_reg64(cpu_V0, rd);
5905                 if (q) {
5906                     neon_store_reg64(cpu_V1, rd + 1);
5907                 }
5908             } else if ((insn & (1 << 11)) == 0) {
5909                 /* Two register misc.  */
5910                 op = ((insn >> 12) & 0x30) | ((insn >> 7) & 0xf);
5911                 size = (insn >> 18) & 3;
5912                 /* UNDEF for unknown op values and bad op-size combinations */
5913                 if ((neon_2rm_sizes[op] & (1 << size)) == 0) {
5914                     return 1;
5915                 }
5916                 if ((op != NEON_2RM_VMOVN && op != NEON_2RM_VQMOVN) &&
5917                     q && ((rm | rd) & 1)) {
5918                     return 1;
5919                 }
5920                 switch (op) {
5921                 case NEON_2RM_VREV64:
5922                     for (pass = 0; pass < (q ? 2 : 1); pass++) {
5923                         tmp = neon_load_reg(rm, pass * 2);
5924                         tmp2 = neon_load_reg(rm, pass * 2 + 1);
5925                         switch (size) {
5926                         case 0: tcg_gen_bswap32_i32(tmp, tmp); break;
5927                         case 1: gen_swap_half(tmp); break;
5928                         case 2: /* no-op */ break;
5929                         default: abort();
5930                         }
5931                         neon_store_reg(rd, pass * 2 + 1, tmp);
5932                         if (size == 2) {
5933                             neon_store_reg(rd, pass * 2, tmp2);
5934                         } else {
5935                             switch (size) {
5936                             case 0: tcg_gen_bswap32_i32(tmp2, tmp2); break;
5937                             case 1: gen_swap_half(tmp2); break;
5938                             default: abort();
5939                             }
5940                             neon_store_reg(rd, pass * 2, tmp2);
5941                         }
5942                     }
5943                     break;
5944                 case NEON_2RM_VPADDL: case NEON_2RM_VPADDL_U:
5945                 case NEON_2RM_VPADAL: case NEON_2RM_VPADAL_U:
5946                     for (pass = 0; pass < q + 1; pass++) {
5947                         tmp = neon_load_reg(rm, pass * 2);
5948                         gen_neon_widen(cpu_V0, tmp, size, op & 1);
5949                         tmp = neon_load_reg(rm, pass * 2 + 1);
5950                         gen_neon_widen(cpu_V1, tmp, size, op & 1);
5951                         switch (size) {
5952                         case 0: gen_helper_neon_paddl_u16(CPU_V001); break;
5953                         case 1: gen_helper_neon_paddl_u32(CPU_V001); break;
5954                         case 2: tcg_gen_add_i64(CPU_V001); break;
5955                         default: abort();
5956                         }
5957                         if (op >= NEON_2RM_VPADAL) {
5958                             /* Accumulate.  */
5959                             neon_load_reg64(cpu_V1, rd + pass);
5960                             gen_neon_addl(size);
5961                         }
5962                         neon_store_reg64(cpu_V0, rd + pass);
5963                     }
5964                     break;
5965                 case NEON_2RM_VTRN:
5966                     if (size == 2) {
5967                         int n;
5968                         for (n = 0; n < (q ? 4 : 2); n += 2) {
5969                             tmp = neon_load_reg(rm, n);
5970                             tmp2 = neon_load_reg(rd, n + 1);
5971                             neon_store_reg(rm, n, tmp2);
5972                             neon_store_reg(rd, n + 1, tmp);
5973                         }
5974                     } else {
5975                         goto elementwise;
5976                     }
5977                     break;
5978                 case NEON_2RM_VUZP:
5979                     if (gen_neon_unzip(rd, rm, size, q)) {
5980                         return 1;
5981                     }
5982                     break;
5983                 case NEON_2RM_VZIP:
5984                     if (gen_neon_zip(rd, rm, size, q)) {
5985                         return 1;
5986                     }
5987                     break;
5988                 case NEON_2RM_VMOVN: case NEON_2RM_VQMOVN:
5989                     /* also VQMOVUN; op field and mnemonics don't line up */
5990                     if (rm & 1) {
5991                         return 1;
5992                     }
5993                     TCGV_UNUSED(tmp2);
5994                     for (pass = 0; pass < 2; pass++) {
5995                         neon_load_reg64(cpu_V0, rm + pass);
5996                         tmp = tcg_temp_new_i32();
5997                         gen_neon_narrow_op(op == NEON_2RM_VMOVN, q, size,
5998                                            tmp, cpu_V0);
5999                         if (pass == 0) {
6000                             tmp2 = tmp;
6001                         } else {
6002                             neon_store_reg(rd, 0, tmp2);
6003                             neon_store_reg(rd, 1, tmp);
6004                         }
6005                     }
6006                     break;
6007                 case NEON_2RM_VSHLL:
6008                     if (q || (rd & 1)) {
6009                         return 1;
6010                     }
6011                     tmp = neon_load_reg(rm, 0);
6012                     tmp2 = neon_load_reg(rm, 1);
6013                     for (pass = 0; pass < 2; pass++) {
6014                         if (pass == 1)
6015                             tmp = tmp2;
6016                         gen_neon_widen(cpu_V0, tmp, size, 1);
6017                         tcg_gen_shli_i64(cpu_V0, cpu_V0, 8 << size);
6018                         neon_store_reg64(cpu_V0, rd + pass);
6019                     }
6020                     break;
6021                 case NEON_2RM_VCVT_F16_F32:
6022                     if (!arm_feature(env, ARM_FEATURE_VFP_FP16) ||
6023                         q || (rm & 1)) {
6024                         return 1;
6025                     }
6026                     tmp = tcg_temp_new_i32();
6027                     tmp2 = tcg_temp_new_i32();
6028                     tcg_gen_ld_f32(cpu_F0s, cpu_env, neon_reg_offset(rm, 0));
6029                     gen_helper_neon_fcvt_f32_to_f16(tmp, cpu_F0s, cpu_env);
6030                     tcg_gen_ld_f32(cpu_F0s, cpu_env, neon_reg_offset(rm, 1));
6031                     gen_helper_neon_fcvt_f32_to_f16(tmp2, cpu_F0s, cpu_env);
6032                     tcg_gen_shli_i32(tmp2, tmp2, 16);
6033                     tcg_gen_or_i32(tmp2, tmp2, tmp);
6034                     tcg_gen_ld_f32(cpu_F0s, cpu_env, neon_reg_offset(rm, 2));
6035                     gen_helper_neon_fcvt_f32_to_f16(tmp, cpu_F0s, cpu_env);
6036                     tcg_gen_ld_f32(cpu_F0s, cpu_env, neon_reg_offset(rm, 3));
6037                     neon_store_reg(rd, 0, tmp2);
6038                     tmp2 = tcg_temp_new_i32();
6039                     gen_helper_neon_fcvt_f32_to_f16(tmp2, cpu_F0s, cpu_env);
6040                     tcg_gen_shli_i32(tmp2, tmp2, 16);
6041                     tcg_gen_or_i32(tmp2, tmp2, tmp);
6042                     neon_store_reg(rd, 1, tmp2);
6043                     tcg_temp_free_i32(tmp);
6044                     break;
6045                 case NEON_2RM_VCVT_F32_F16:
6046                     if (!arm_feature(env, ARM_FEATURE_VFP_FP16) ||
6047                         q || (rd & 1)) {
6048                         return 1;
6049                     }
6050                     tmp3 = tcg_temp_new_i32();
6051                     tmp = neon_load_reg(rm, 0);
6052                     tmp2 = neon_load_reg(rm, 1);
6053                     tcg_gen_ext16u_i32(tmp3, tmp);
6054                     gen_helper_neon_fcvt_f16_to_f32(cpu_F0s, tmp3, cpu_env);
6055                     tcg_gen_st_f32(cpu_F0s, cpu_env, neon_reg_offset(rd, 0));
6056                     tcg_gen_shri_i32(tmp3, tmp, 16);
6057                     gen_helper_neon_fcvt_f16_to_f32(cpu_F0s, tmp3, cpu_env);
6058                     tcg_gen_st_f32(cpu_F0s, cpu_env, neon_reg_offset(rd, 1));
6059                     tcg_temp_free_i32(tmp);
6060                     tcg_gen_ext16u_i32(tmp3, tmp2);
6061                     gen_helper_neon_fcvt_f16_to_f32(cpu_F0s, tmp3, cpu_env);
6062                     tcg_gen_st_f32(cpu_F0s, cpu_env, neon_reg_offset(rd, 2));
6063                     tcg_gen_shri_i32(tmp3, tmp2, 16);
6064                     gen_helper_neon_fcvt_f16_to_f32(cpu_F0s, tmp3, cpu_env);
6065                     tcg_gen_st_f32(cpu_F0s, cpu_env, neon_reg_offset(rd, 3));
6066                     tcg_temp_free_i32(tmp2);
6067                     tcg_temp_free_i32(tmp3);
6068                     break;
6069                 default:
6070                 elementwise:
6071                     for (pass = 0; pass < (q ? 4 : 2); pass++) {
6072                         if (neon_2rm_is_float_op(op)) {
6073                             tcg_gen_ld_f32(cpu_F0s, cpu_env,
6074                                            neon_reg_offset(rm, pass));
6075                             TCGV_UNUSED(tmp);
6076                         } else {
6077                             tmp = neon_load_reg(rm, pass);
6078                         }
6079                         switch (op) {
6080                         case NEON_2RM_VREV32:
6081                             switch (size) {
6082                             case 0: tcg_gen_bswap32_i32(tmp, tmp); break;
6083                             case 1: gen_swap_half(tmp); break;
6084                             default: abort();
6085                             }
6086                             break;
6087                         case NEON_2RM_VREV16:
6088                             gen_rev16(tmp);
6089                             break;
6090                         case NEON_2RM_VCLS:
6091                             switch (size) {
6092                             case 0: gen_helper_neon_cls_s8(tmp, tmp); break;
6093                             case 1: gen_helper_neon_cls_s16(tmp, tmp); break;
6094                             case 2: gen_helper_neon_cls_s32(tmp, tmp); break;
6095                             default: abort();
6096                             }
6097                             break;
6098                         case NEON_2RM_VCLZ:
6099                             switch (size) {
6100                             case 0: gen_helper_neon_clz_u8(tmp, tmp); break;
6101                             case 1: gen_helper_neon_clz_u16(tmp, tmp); break;
6102                             case 2: gen_helper_clz(tmp, tmp); break;
6103                             default: abort();
6104                             }
6105                             break;
6106                         case NEON_2RM_VCNT:
6107                             gen_helper_neon_cnt_u8(tmp, tmp);
6108                             break;
6109                         case NEON_2RM_VMVN:
6110                             tcg_gen_not_i32(tmp, tmp);
6111                             break;
6112                         case NEON_2RM_VQABS:
6113                             switch (size) {
6114                             case 0:
6115                                 gen_helper_neon_qabs_s8(tmp, cpu_env, tmp);
6116                                 break;
6117                             case 1:
6118                                 gen_helper_neon_qabs_s16(tmp, cpu_env, tmp);
6119                                 break;
6120                             case 2:
6121                                 gen_helper_neon_qabs_s32(tmp, cpu_env, tmp);
6122                                 break;
6123                             default: abort();
6124                             }
6125                             break;
6126                         case NEON_2RM_VQNEG:
6127                             switch (size) {
6128                             case 0:
6129                                 gen_helper_neon_qneg_s8(tmp, cpu_env, tmp);
6130                                 break;
6131                             case 1:
6132                                 gen_helper_neon_qneg_s16(tmp, cpu_env, tmp);
6133                                 break;
6134                             case 2:
6135                                 gen_helper_neon_qneg_s32(tmp, cpu_env, tmp);
6136                                 break;
6137                             default: abort();
6138                             }
6139                             break;
6140                         case NEON_2RM_VCGT0: case NEON_2RM_VCLE0:
6141                             tmp2 = tcg_const_i32(0);
6142                             switch(size) {
6143                             case 0: gen_helper_neon_cgt_s8(tmp, tmp, tmp2); break;
6144                             case 1: gen_helper_neon_cgt_s16(tmp, tmp, tmp2); break;
6145                             case 2: gen_helper_neon_cgt_s32(tmp, tmp, tmp2); break;
6146                             default: abort();
6147                             }
6148                             tcg_temp_free(tmp2);
6149                             if (op == NEON_2RM_VCLE0) {
6150                                 tcg_gen_not_i32(tmp, tmp);
6151                             }
6152                             break;
6153                         case NEON_2RM_VCGE0: case NEON_2RM_VCLT0:
6154                             tmp2 = tcg_const_i32(0);
6155                             switch(size) {
6156                             case 0: gen_helper_neon_cge_s8(tmp, tmp, tmp2); break;
6157                             case 1: gen_helper_neon_cge_s16(tmp, tmp, tmp2); break;
6158                             case 2: gen_helper_neon_cge_s32(tmp, tmp, tmp2); break;
6159                             default: abort();
6160                             }
6161                             tcg_temp_free(tmp2);
6162                             if (op == NEON_2RM_VCLT0) {
6163                                 tcg_gen_not_i32(tmp, tmp);
6164                             }
6165                             break;
6166                         case NEON_2RM_VCEQ0:
6167                             tmp2 = tcg_const_i32(0);
6168                             switch(size) {
6169                             case 0: gen_helper_neon_ceq_u8(tmp, tmp, tmp2); break;
6170                             case 1: gen_helper_neon_ceq_u16(tmp, tmp, tmp2); break;
6171                             case 2: gen_helper_neon_ceq_u32(tmp, tmp, tmp2); break;
6172                             default: abort();
6173                             }
6174                             tcg_temp_free(tmp2);
6175                             break;
6176                         case NEON_2RM_VABS:
6177                             switch(size) {
6178                             case 0: gen_helper_neon_abs_s8(tmp, tmp); break;
6179                             case 1: gen_helper_neon_abs_s16(tmp, tmp); break;
6180                             case 2: tcg_gen_abs_i32(tmp, tmp); break;
6181                             default: abort();
6182                             }
6183                             break;
6184                         case NEON_2RM_VNEG:
6185                             tmp2 = tcg_const_i32(0);
6186                             gen_neon_rsb(size, tmp, tmp2);
6187                             tcg_temp_free(tmp2);
6188                             break;
6189                         case NEON_2RM_VCGT0_F:
6190                         {
6191                             TCGv_ptr fpstatus = get_fpstatus_ptr(1);
6192                             tmp2 = tcg_const_i32(0);
6193                             gen_helper_neon_cgt_f32(tmp, tmp, tmp2, fpstatus);
6194                             tcg_temp_free(tmp2);
6195                             tcg_temp_free_ptr(fpstatus);
6196                             break;
6197                         }
6198                         case NEON_2RM_VCGE0_F:
6199                         {
6200                             TCGv_ptr fpstatus = get_fpstatus_ptr(1);
6201                             tmp2 = tcg_const_i32(0);
6202                             gen_helper_neon_cge_f32(tmp, tmp, tmp2, fpstatus);
6203                             tcg_temp_free(tmp2);
6204                             tcg_temp_free_ptr(fpstatus);
6205                             break;
6206                         }
6207                         case NEON_2RM_VCEQ0_F:
6208                         {
6209                             TCGv_ptr fpstatus = get_fpstatus_ptr(1);
6210                             tmp2 = tcg_const_i32(0);
6211                             gen_helper_neon_ceq_f32(tmp, tmp, tmp2, fpstatus);
6212                             tcg_temp_free(tmp2);
6213                             tcg_temp_free_ptr(fpstatus);
6214                             break;
6215                         }
6216                         case NEON_2RM_VCLE0_F:
6217                         {
6218                             TCGv_ptr fpstatus = get_fpstatus_ptr(1);
6219                             tmp2 = tcg_const_i32(0);
6220                             gen_helper_neon_cge_f32(tmp, tmp2, tmp, fpstatus);
6221                             tcg_temp_free(tmp2);
6222                             tcg_temp_free_ptr(fpstatus);
6223                             break;
6224                         }
6225                         case NEON_2RM_VCLT0_F:
6226                         {
6227                             TCGv_ptr fpstatus = get_fpstatus_ptr(1);
6228                             tmp2 = tcg_const_i32(0);
6229                             gen_helper_neon_cgt_f32(tmp, tmp2, tmp, fpstatus);
6230                             tcg_temp_free(tmp2);
6231                             tcg_temp_free_ptr(fpstatus);
6232                             break;
6233                         }
6234                         case NEON_2RM_VABS_F:
6235                             gen_vfp_abs(0);
6236                             break;
6237                         case NEON_2RM_VNEG_F:
6238                             gen_vfp_neg(0);
6239                             break;
6240                         case NEON_2RM_VSWP:
6241                             tmp2 = neon_load_reg(rd, pass);
6242                             neon_store_reg(rm, pass, tmp2);
6243                             break;
6244                         case NEON_2RM_VTRN:
6245                             tmp2 = neon_load_reg(rd, pass);
6246                             switch (size) {
6247                             case 0: gen_neon_trn_u8(tmp, tmp2); break;
6248                             case 1: gen_neon_trn_u16(tmp, tmp2); break;
6249                             default: abort();
6250                             }
6251                             neon_store_reg(rm, pass, tmp2);
6252                             break;
6253                         case NEON_2RM_VRECPE:
6254                             gen_helper_recpe_u32(tmp, tmp, cpu_env);
6255                             break;
6256                         case NEON_2RM_VRSQRTE:
6257                             gen_helper_rsqrte_u32(tmp, tmp, cpu_env);
6258                             break;
6259                         case NEON_2RM_VRECPE_F:
6260                             gen_helper_recpe_f32(cpu_F0s, cpu_F0s, cpu_env);
6261                             break;
6262                         case NEON_2RM_VRSQRTE_F:
6263                             gen_helper_rsqrte_f32(cpu_F0s, cpu_F0s, cpu_env);
6264                             break;
6265                         case NEON_2RM_VCVT_FS: /* VCVT.F32.S32 */
6266                             gen_vfp_sito(0, 1);
6267                             break;
6268                         case NEON_2RM_VCVT_FU: /* VCVT.F32.U32 */
6269                             gen_vfp_uito(0, 1);
6270                             break;
6271                         case NEON_2RM_VCVT_SF: /* VCVT.S32.F32 */
6272                             gen_vfp_tosiz(0, 1);
6273                             break;
6274                         case NEON_2RM_VCVT_UF: /* VCVT.U32.F32 */
6275                             gen_vfp_touiz(0, 1);
6276                             break;
6277                         default:
6278                             /* Reserved op values were caught by the
6279                              * neon_2rm_sizes[] check earlier.
6280                              */
6281                             abort();
6282                         }
6283                         if (neon_2rm_is_float_op(op)) {
6284                             tcg_gen_st_f32(cpu_F0s, cpu_env,
6285                                            neon_reg_offset(rd, pass));
6286                         } else {
6287                             neon_store_reg(rd, pass, tmp);
6288                         }
6289                     }
6290                     break;
6291                 }
6292             } else if ((insn & (1 << 10)) == 0) {
6293                 /* VTBL, VTBX.  */
6294                 int n = ((insn >> 8) & 3) + 1;
6295                 if ((rn + n) > 32) {
6296                     /* This is UNPREDICTABLE; we choose to UNDEF to avoid the
6297                      * helper function running off the end of the register file.
6298                      */
6299                     return 1;
6300                 }
6301                 n <<= 3;
6302                 if (insn & (1 << 6)) {
6303                     tmp = neon_load_reg(rd, 0);
6304                 } else {
6305                     tmp = tcg_temp_new_i32();
6306                     tcg_gen_movi_i32(tmp, 0);
6307                 }
6308                 tmp2 = neon_load_reg(rm, 0);
6309                 tmp4 = tcg_const_i32(rn);
6310                 tmp5 = tcg_const_i32(n);
6311                 gen_helper_neon_tbl(tmp2, tmp2, tmp, tmp4, tmp5);
6312                 tcg_temp_free_i32(tmp);
6313                 if (insn & (1 << 6)) {
6314                     tmp = neon_load_reg(rd, 1);
6315                 } else {
6316                     tmp = tcg_temp_new_i32();
6317                     tcg_gen_movi_i32(tmp, 0);
6318                 }
6319                 tmp3 = neon_load_reg(rm, 1);
6320                 gen_helper_neon_tbl(tmp3, tmp3, tmp, tmp4, tmp5);
6321                 tcg_temp_free_i32(tmp5);
6322                 tcg_temp_free_i32(tmp4);
6323                 neon_store_reg(rd, 0, tmp2);
6324                 neon_store_reg(rd, 1, tmp3);
6325                 tcg_temp_free_i32(tmp);
6326             } else if ((insn & 0x380) == 0) {
6327                 /* VDUP */
6328                 if ((insn & (7 << 16)) == 0 || (q && (rd & 1))) {
6329                     return 1;
6330                 }
6331                 if (insn & (1 << 19)) {
6332                     tmp = neon_load_reg(rm, 1);
6333                 } else {
6334                     tmp = neon_load_reg(rm, 0);
6335                 }
6336                 if (insn & (1 << 16)) {
6337                     gen_neon_dup_u8(tmp, ((insn >> 17) & 3) * 8);
6338                 } else if (insn & (1 << 17)) {
6339                     if ((insn >> 18) & 1)
6340                         gen_neon_dup_high16(tmp);
6341                     else
6342                         gen_neon_dup_low16(tmp);
6343                 }
6344                 for (pass = 0; pass < (q ? 4 : 2); pass++) {
6345                     tmp2 = tcg_temp_new_i32();
6346                     tcg_gen_mov_i32(tmp2, tmp);
6347                     neon_store_reg(rd, pass, tmp2);
6348                 }
6349                 tcg_temp_free_i32(tmp);
6350             } else {
6351                 return 1;
6352             }
6353         }
6354     }
6355     return 0;
6356 }
6357
6358 static int disas_cp14_read(CPUARMState * env, DisasContext *s, uint32_t insn)
6359 {
6360     int crn = (insn >> 16) & 0xf;
6361     int crm = insn & 0xf;
6362     int op1 = (insn >> 21) & 7;
6363     int op2 = (insn >> 5) & 7;
6364     int rt = (insn >> 12) & 0xf;
6365     TCGv tmp;
6366
6367     /* Minimal set of debug registers, since we don't support debug */
6368     if (op1 == 0 && crn == 0 && op2 == 0) {
6369         switch (crm) {
6370         case 0:
6371             /* DBGDIDR: just RAZ. In particular this means the
6372              * "debug architecture version" bits will read as
6373              * a reserved value, which should cause Linux to
6374              * not try to use the debug hardware.
6375              */
6376             tmp = tcg_const_i32(0);
6377             store_reg(s, rt, tmp);
6378             return 0;
6379         case 1:
6380         case 2:
6381             /* DBGDRAR and DBGDSAR: v7 only. Always RAZ since we
6382              * don't implement memory mapped debug components
6383              */
6384             if (ENABLE_ARCH_7) {
6385                 tmp = tcg_const_i32(0);
6386                 store_reg(s, rt, tmp);
6387                 return 0;
6388             }
6389             break;
6390         default:
6391             break;
6392         }
6393     }
6394
6395     if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
6396         if (op1 == 6 && crn == 0 && crm == 0 && op2 == 0) {
6397             /* TEECR */
6398             if (IS_USER(s))
6399                 return 1;
6400             tmp = load_cpu_field(teecr);
6401             store_reg(s, rt, tmp);
6402             return 0;
6403         }
6404         if (op1 == 6 && crn == 1 && crm == 0 && op2 == 0) {
6405             /* TEEHBR */
6406             if (IS_USER(s) && (env->teecr & 1))
6407                 return 1;
6408             tmp = load_cpu_field(teehbr);
6409             store_reg(s, rt, tmp);
6410             return 0;
6411         }
6412     }
6413     return 1;
6414 }
6415
6416 static int disas_cp14_write(CPUARMState * env, DisasContext *s, uint32_t insn)
6417 {
6418     int crn = (insn >> 16) & 0xf;
6419     int crm = insn & 0xf;
6420     int op1 = (insn >> 21) & 7;
6421     int op2 = (insn >> 5) & 7;
6422     int rt = (insn >> 12) & 0xf;
6423     TCGv tmp;
6424
6425     if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
6426         if (op1 == 6 && crn == 0 && crm == 0 && op2 == 0) {
6427             /* TEECR */
6428             if (IS_USER(s))
6429                 return 1;
6430             tmp = load_reg(s, rt);
6431             gen_helper_set_teecr(cpu_env, tmp);
6432             tcg_temp_free_i32(tmp);
6433             return 0;
6434         }
6435         if (op1 == 6 && crn == 1 && crm == 0 && op2 == 0) {
6436             /* TEEHBR */
6437             if (IS_USER(s) && (env->teecr & 1))
6438                 return 1;
6439             tmp = load_reg(s, rt);
6440             store_cpu_field(tmp, teehbr);
6441             return 0;
6442         }
6443     }
6444     return 1;
6445 }
6446
6447 static int disas_coproc_insn(CPUARMState * env, DisasContext *s, uint32_t insn)
6448 {
6449     int cpnum, is64, crn, crm, opc1, opc2, isread, rt, rt2;
6450     const ARMCPRegInfo *ri;
6451     ARMCPU *cpu = arm_env_get_cpu(env);
6452
6453     cpnum = (insn >> 8) & 0xf;
6454     if (arm_feature(env, ARM_FEATURE_XSCALE)
6455             && ((env->cp15.c15_cpar ^ 0x3fff) & (1 << cpnum)))
6456         return 1;
6457
6458     /* First check for coprocessor space used for actual instructions */
6459     switch (cpnum) {
6460       case 0:
6461       case 1:
6462         if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
6463             return disas_iwmmxt_insn(env, s, insn);
6464         } else if (arm_feature(env, ARM_FEATURE_XSCALE)) {
6465             return disas_dsp_insn(env, s, insn);
6466         }
6467         return 1;
6468     case 10:
6469     case 11:
6470         return disas_vfp_insn (env, s, insn);
6471     default:
6472         break;
6473     }
6474
6475     /* Otherwise treat as a generic register access */
6476     is64 = (insn & (1 << 25)) == 0;
6477     if (!is64 && ((insn & (1 << 4)) == 0)) {
6478         /* cdp */
6479         return 1;
6480     }
6481
6482     crm = insn & 0xf;
6483     if (is64) {
6484         crn = 0;
6485         opc1 = (insn >> 4) & 0xf;
6486         opc2 = 0;
6487         rt2 = (insn >> 16) & 0xf;
6488     } else {
6489         crn = (insn >> 16) & 0xf;
6490         opc1 = (insn >> 21) & 7;
6491         opc2 = (insn >> 5) & 7;
6492         rt2 = 0;
6493     }
6494     isread = (insn >> 20) & 1;
6495     rt = (insn >> 12) & 0xf;
6496
6497     ri = get_arm_cp_reginfo(cpu,
6498                             ENCODE_CP_REG(cpnum, is64, crn, crm, opc1, opc2));
6499     if (ri) {
6500         /* Check access permissions */
6501         if (!cp_access_ok(env, ri, isread)) {
6502             return 1;
6503         }
6504
6505         /* Handle special cases first */
6506         switch (ri->type & ~(ARM_CP_FLAG_MASK & ~ARM_CP_SPECIAL)) {
6507         case ARM_CP_NOP:
6508             return 0;
6509         case ARM_CP_WFI:
6510             if (isread) {
6511                 return 1;
6512             }
6513             gen_set_pc_im(s->pc);
6514             s->is_jmp = DISAS_WFI;
6515             break;
6516         default:
6517             break;
6518         }
6519
6520         if (isread) {
6521             /* Read */
6522             if (is64) {
6523                 TCGv_i64 tmp64;
6524                 TCGv_i32 tmp;
6525                 if (ri->type & ARM_CP_CONST) {
6526                     tmp64 = tcg_const_i64(ri->resetvalue);
6527                 } else if (ri->readfn) {
6528                     TCGv_ptr tmpptr;
6529                     gen_set_pc_im(s->pc);
6530                     tmp64 = tcg_temp_new_i64();
6531                     tmpptr = tcg_const_ptr(ri);
6532                     gen_helper_get_cp_reg64(tmp64, cpu_env, tmpptr);
6533                     tcg_temp_free_ptr(tmpptr);
6534                 } else {
6535                     tmp64 = tcg_temp_new_i64();
6536                     tcg_gen_ld_i64(tmp64, cpu_env, ri->fieldoffset);
6537                 }
6538                 tmp = tcg_temp_new_i32();
6539                 tcg_gen_trunc_i64_i32(tmp, tmp64);
6540                 store_reg(s, rt, tmp);
6541                 tcg_gen_shri_i64(tmp64, tmp64, 32);
6542                 tcg_gen_trunc_i64_i32(tmp, tmp64);
6543                 store_reg(s, rt2, tmp);
6544             } else {
6545                 TCGv tmp;
6546                 if (ri->type & ARM_CP_CONST) {
6547                     tmp = tcg_const_i32(ri->resetvalue);
6548                 } else if (ri->readfn) {
6549                     TCGv_ptr tmpptr;
6550                     gen_set_pc_im(s->pc);
6551                     tmp = tcg_temp_new_i32();
6552                     tmpptr = tcg_const_ptr(ri);
6553                     gen_helper_get_cp_reg(tmp, cpu_env, tmpptr);
6554                     tcg_temp_free_ptr(tmpptr);
6555                 } else {
6556                     tmp = load_cpu_offset(ri->fieldoffset);
6557                 }
6558                 if (rt == 15) {
6559                     /* Destination register of r15 for 32 bit loads sets
6560                      * the condition codes from the high 4 bits of the value
6561                      */
6562                     gen_set_nzcv(tmp);
6563                     tcg_temp_free_i32(tmp);
6564                 } else {
6565                     store_reg(s, rt, tmp);
6566                 }
6567             }
6568         } else {
6569             /* Write */
6570             if (ri->type & ARM_CP_CONST) {
6571                 /* If not forbidden by access permissions, treat as WI */
6572                 return 0;
6573             }
6574
6575             if (is64) {
6576                 TCGv tmplo, tmphi;
6577                 TCGv_i64 tmp64 = tcg_temp_new_i64();
6578                 tmplo = load_reg(s, rt);
6579                 tmphi = load_reg(s, rt2);
6580                 tcg_gen_concat_i32_i64(tmp64, tmplo, tmphi);
6581                 tcg_temp_free_i32(tmplo);
6582                 tcg_temp_free_i32(tmphi);
6583                 if (ri->writefn) {
6584                     TCGv_ptr tmpptr = tcg_const_ptr(ri);
6585                     gen_set_pc_im(s->pc);
6586                     gen_helper_set_cp_reg64(cpu_env, tmpptr, tmp64);
6587                     tcg_temp_free_ptr(tmpptr);
6588                 } else {
6589                     tcg_gen_st_i64(tmp64, cpu_env, ri->fieldoffset);
6590                 }
6591                 tcg_temp_free_i64(tmp64);
6592             } else {
6593                 if (ri->writefn) {
6594                     TCGv tmp;
6595                     TCGv_ptr tmpptr;
6596                     gen_set_pc_im(s->pc);
6597                     tmp = load_reg(s, rt);
6598                     tmpptr = tcg_const_ptr(ri);
6599                     gen_helper_set_cp_reg(cpu_env, tmpptr, tmp);
6600                     tcg_temp_free_ptr(tmpptr);
6601                     tcg_temp_free_i32(tmp);
6602                 } else {
6603                     TCGv tmp = load_reg(s, rt);
6604                     store_cpu_offset(tmp, ri->fieldoffset);
6605                 }
6606             }
6607             /* We default to ending the TB on a coprocessor register write,
6608              * but allow this to be suppressed by the register definition
6609              * (usually only necessary to work around guest bugs).
6610              */
6611             if (!(ri->type & ARM_CP_SUPPRESS_TB_END)) {
6612                 gen_lookup_tb(s);
6613             }
6614         }
6615         return 0;
6616     }
6617
6618     /* Fallback code: handle coprocessor registers not yet converted
6619      * to ARMCPRegInfo.
6620      */
6621     switch (cpnum) {
6622     case 14:
6623         if (insn & (1 << 20))
6624             return disas_cp14_read(env, s, insn);
6625         else
6626             return disas_cp14_write(env, s, insn);
6627     case 15:
6628         return disas_cp15_insn (env, s, insn);
6629     default:
6630         return 1;
6631     }
6632 }
6633
6634
6635 /* Store a 64-bit value to a register pair.  Clobbers val.  */
6636 static void gen_storeq_reg(DisasContext *s, int rlow, int rhigh, TCGv_i64 val)
6637 {
6638     TCGv tmp;
6639     tmp = tcg_temp_new_i32();
6640     tcg_gen_trunc_i64_i32(tmp, val);
6641     store_reg(s, rlow, tmp);
6642     tmp = tcg_temp_new_i32();
6643     tcg_gen_shri_i64(val, val, 32);
6644     tcg_gen_trunc_i64_i32(tmp, val);
6645     store_reg(s, rhigh, tmp);
6646 }
6647
6648 /* load a 32-bit value from a register and perform a 64-bit accumulate.  */
6649 static void gen_addq_lo(DisasContext *s, TCGv_i64 val, int rlow)
6650 {
6651     TCGv_i64 tmp;
6652     TCGv tmp2;
6653
6654     /* Load value and extend to 64 bits.  */
6655     tmp = tcg_temp_new_i64();
6656     tmp2 = load_reg(s, rlow);
6657     tcg_gen_extu_i32_i64(tmp, tmp2);
6658     tcg_temp_free_i32(tmp2);
6659     tcg_gen_add_i64(val, val, tmp);
6660     tcg_temp_free_i64(tmp);
6661 }
6662
6663 /* load and add a 64-bit value from a register pair.  */
6664 static void gen_addq(DisasContext *s, TCGv_i64 val, int rlow, int rhigh)
6665 {
6666     TCGv_i64 tmp;
6667     TCGv tmpl;
6668     TCGv tmph;
6669
6670     /* Load 64-bit value rd:rn.  */
6671     tmpl = load_reg(s, rlow);
6672     tmph = load_reg(s, rhigh);
6673     tmp = tcg_temp_new_i64();
6674     tcg_gen_concat_i32_i64(tmp, tmpl, tmph);
6675     tcg_temp_free_i32(tmpl);
6676     tcg_temp_free_i32(tmph);
6677     tcg_gen_add_i64(val, val, tmp);
6678     tcg_temp_free_i64(tmp);
6679 }
6680
6681 /* Set N and Z flags from a 64-bit value.  */
6682 static void gen_logicq_cc(TCGv_i64 val)
6683 {
6684     TCGv tmp = tcg_temp_new_i32();
6685     gen_helper_logicq_cc(tmp, val);
6686     gen_logic_CC(tmp);
6687     tcg_temp_free_i32(tmp);
6688 }
6689
6690 /* Load/Store exclusive instructions are implemented by remembering
6691    the value/address loaded, and seeing if these are the same
6692    when the store is performed. This should be is sufficient to implement
6693    the architecturally mandated semantics, and avoids having to monitor
6694    regular stores.
6695
6696    In system emulation mode only one CPU will be running at once, so
6697    this sequence is effectively atomic.  In user emulation mode we
6698    throw an exception and handle the atomic operation elsewhere.  */
6699 static void gen_load_exclusive(DisasContext *s, int rt, int rt2,
6700                                TCGv addr, int size)
6701 {
6702     TCGv tmp;
6703
6704     switch (size) {
6705     case 0:
6706         tmp = gen_ld8u(addr, IS_USER(s));
6707         break;
6708     case 1:
6709         tmp = gen_ld16u(addr, IS_USER(s));
6710         break;
6711     case 2:
6712     case 3:
6713         tmp = gen_ld32(addr, IS_USER(s));
6714         break;
6715     default:
6716         abort();
6717     }
6718     tcg_gen_mov_i32(cpu_exclusive_val, tmp);
6719     store_reg(s, rt, tmp);
6720     if (size == 3) {
6721         TCGv tmp2 = tcg_temp_new_i32();
6722         tcg_gen_addi_i32(tmp2, addr, 4);
6723         tmp = gen_ld32(tmp2, IS_USER(s));
6724         tcg_temp_free_i32(tmp2);
6725         tcg_gen_mov_i32(cpu_exclusive_high, tmp);
6726         store_reg(s, rt2, tmp);
6727     }
6728     tcg_gen_mov_i32(cpu_exclusive_addr, addr);
6729 }
6730
6731 static void gen_clrex(DisasContext *s)
6732 {
6733     tcg_gen_movi_i32(cpu_exclusive_addr, -1);
6734 }
6735
6736 #ifdef CONFIG_USER_ONLY
6737 static void gen_store_exclusive(DisasContext *s, int rd, int rt, int rt2,
6738                                 TCGv addr, int size)
6739 {
6740     tcg_gen_mov_i32(cpu_exclusive_test, addr);
6741     tcg_gen_movi_i32(cpu_exclusive_info,
6742                      size | (rd << 4) | (rt << 8) | (rt2 << 12));
6743     gen_exception_insn(s, 4, EXCP_STREX);
6744 }
6745 #else
6746 static void gen_store_exclusive(DisasContext *s, int rd, int rt, int rt2,
6747                                 TCGv addr, int size)
6748 {
6749     TCGv tmp;
6750     int done_label;
6751     int fail_label;
6752
6753     /* if (env->exclusive_addr == addr && env->exclusive_val == [addr]) {
6754          [addr] = {Rt};
6755          {Rd} = 0;
6756        } else {
6757          {Rd} = 1;
6758        } */
6759     fail_label = gen_new_label();
6760     done_label = gen_new_label();
6761     tcg_gen_brcond_i32(TCG_COND_NE, addr, cpu_exclusive_addr, fail_label);
6762     switch (size) {
6763     case 0:
6764         tmp = gen_ld8u(addr, IS_USER(s));
6765         break;
6766     case 1:
6767         tmp = gen_ld16u(addr, IS_USER(s));
6768         break;
6769     case 2:
6770     case 3:
6771         tmp = gen_ld32(addr, IS_USER(s));
6772         break;
6773     default:
6774         abort();
6775     }
6776     tcg_gen_brcond_i32(TCG_COND_NE, tmp, cpu_exclusive_val, fail_label);
6777     tcg_temp_free_i32(tmp);
6778     if (size == 3) {
6779         TCGv tmp2 = tcg_temp_new_i32();
6780         tcg_gen_addi_i32(tmp2, addr, 4);
6781         tmp = gen_ld32(tmp2, IS_USER(s));
6782         tcg_temp_free_i32(tmp2);
6783         tcg_gen_brcond_i32(TCG_COND_NE, tmp, cpu_exclusive_high, fail_label);
6784         tcg_temp_free_i32(tmp);
6785     }
6786     tmp = load_reg(s, rt);
6787     switch (size) {
6788     case 0:
6789         gen_st8(tmp, addr, IS_USER(s));
6790         break;
6791     case 1:
6792         gen_st16(tmp, addr, IS_USER(s));
6793         break;
6794     case 2:
6795     case 3:
6796         gen_st32(tmp, addr, IS_USER(s));
6797         break;
6798     default:
6799         abort();
6800     }
6801     if (size == 3) {
6802         tcg_gen_addi_i32(addr, addr, 4);
6803         tmp = load_reg(s, rt2);
6804         gen_st32(tmp, addr, IS_USER(s));
6805     }
6806     tcg_gen_movi_i32(cpu_R[rd], 0);
6807     tcg_gen_br(done_label);
6808     gen_set_label(fail_label);
6809     tcg_gen_movi_i32(cpu_R[rd], 1);
6810     gen_set_label(done_label);
6811     tcg_gen_movi_i32(cpu_exclusive_addr, -1);
6812 }
6813 #endif
6814
6815 static void disas_arm_insn(CPUARMState * env, DisasContext *s)
6816 {
6817     unsigned int cond, insn, val, op1, i, shift, rm, rs, rn, rd, sh;
6818     TCGv tmp;
6819     TCGv tmp2;
6820     TCGv tmp3;
6821     TCGv addr;
6822     TCGv_i64 tmp64;
6823
6824     insn = arm_ldl_code(s->pc, s->bswap_code);
6825     s->pc += 4;
6826
6827     /* M variants do not implement ARM mode.  */
6828     if (IS_M(env))
6829         goto illegal_op;
6830     cond = insn >> 28;
6831     if (cond == 0xf){
6832         /* In ARMv3 and v4 the NV condition is UNPREDICTABLE; we
6833          * choose to UNDEF. In ARMv5 and above the space is used
6834          * for miscellaneous unconditional instructions.
6835          */
6836         ARCH(5);
6837
6838         /* Unconditional instructions.  */
6839         if (((insn >> 25) & 7) == 1) {
6840             /* NEON Data processing.  */
6841             if (!arm_feature(env, ARM_FEATURE_NEON))
6842                 goto illegal_op;
6843
6844             if (disas_neon_data_insn(env, s, insn))
6845                 goto illegal_op;
6846             return;
6847         }
6848         if ((insn & 0x0f100000) == 0x04000000) {
6849             /* NEON load/store.  */
6850             if (!arm_feature(env, ARM_FEATURE_NEON))
6851                 goto illegal_op;
6852
6853             if (disas_neon_ls_insn(env, s, insn))
6854                 goto illegal_op;
6855             return;
6856         }
6857         if (((insn & 0x0f30f000) == 0x0510f000) ||
6858             ((insn & 0x0f30f010) == 0x0710f000)) {
6859             if ((insn & (1 << 22)) == 0) {
6860                 /* PLDW; v7MP */
6861                 if (!arm_feature(env, ARM_FEATURE_V7MP)) {
6862                     goto illegal_op;
6863                 }
6864             }
6865             /* Otherwise PLD; v5TE+ */
6866             ARCH(5TE);
6867             return;
6868         }
6869         if (((insn & 0x0f70f000) == 0x0450f000) ||
6870             ((insn & 0x0f70f010) == 0x0650f000)) {
6871             ARCH(7);
6872             return; /* PLI; V7 */
6873         }
6874         if (((insn & 0x0f700000) == 0x04100000) ||
6875             ((insn & 0x0f700010) == 0x06100000)) {
6876             if (!arm_feature(env, ARM_FEATURE_V7MP)) {
6877                 goto illegal_op;
6878             }
6879             return; /* v7MP: Unallocated memory hint: must NOP */
6880         }
6881
6882         if ((insn & 0x0ffffdff) == 0x01010000) {
6883             ARCH(6);
6884             /* setend */
6885             if (((insn >> 9) & 1) != s->bswap_code) {
6886                 /* Dynamic endianness switching not implemented. */
6887                 goto illegal_op;
6888             }
6889             return;
6890         } else if ((insn & 0x0fffff00) == 0x057ff000) {
6891             switch ((insn >> 4) & 0xf) {
6892             case 1: /* clrex */
6893                 ARCH(6K);
6894                 gen_clrex(s);
6895                 return;
6896             case 4: /* dsb */
6897             case 5: /* dmb */
6898             case 6: /* isb */
6899                 ARCH(7);
6900                 /* We don't emulate caches so these are a no-op.  */
6901                 return;
6902             default:
6903                 goto illegal_op;
6904             }
6905         } else if ((insn & 0x0e5fffe0) == 0x084d0500) {
6906             /* srs */
6907             int32_t offset;
6908             if (IS_USER(s))
6909                 goto illegal_op;
6910             ARCH(6);
6911             op1 = (insn & 0x1f);
6912             addr = tcg_temp_new_i32();
6913             tmp = tcg_const_i32(op1);
6914             gen_helper_get_r13_banked(addr, cpu_env, tmp);
6915             tcg_temp_free_i32(tmp);
6916             i = (insn >> 23) & 3;
6917             switch (i) {
6918             case 0: offset = -4; break; /* DA */
6919             case 1: offset = 0; break; /* IA */
6920             case 2: offset = -8; break; /* DB */
6921             case 3: offset = 4; break; /* IB */
6922             default: abort();
6923             }
6924             if (offset)
6925                 tcg_gen_addi_i32(addr, addr, offset);
6926             tmp = load_reg(s, 14);
6927             gen_st32(tmp, addr, 0);
6928             tmp = load_cpu_field(spsr);
6929             tcg_gen_addi_i32(addr, addr, 4);
6930             gen_st32(tmp, addr, 0);
6931             if (insn & (1 << 21)) {
6932                 /* Base writeback.  */
6933                 switch (i) {
6934                 case 0: offset = -8; break;
6935                 case 1: offset = 4; break;
6936                 case 2: offset = -4; break;
6937                 case 3: offset = 0; break;
6938                 default: abort();
6939                 }
6940                 if (offset)
6941                     tcg_gen_addi_i32(addr, addr, offset);
6942                 tmp = tcg_const_i32(op1);
6943                 gen_helper_set_r13_banked(cpu_env, tmp, addr);
6944                 tcg_temp_free_i32(tmp);
6945                 tcg_temp_free_i32(addr);
6946             } else {
6947                 tcg_temp_free_i32(addr);
6948             }
6949             return;
6950         } else if ((insn & 0x0e50ffe0) == 0x08100a00) {
6951             /* rfe */
6952             int32_t offset;
6953             if (IS_USER(s))
6954                 goto illegal_op;
6955             ARCH(6);
6956             rn = (insn >> 16) & 0xf;
6957             addr = load_reg(s, rn);
6958             i = (insn >> 23) & 3;
6959             switch (i) {
6960             case 0: offset = -4; break; /* DA */
6961             case 1: offset = 0; break; /* IA */
6962             case 2: offset = -8; break; /* DB */
6963             case 3: offset = 4; break; /* IB */
6964             default: abort();
6965             }
6966             if (offset)
6967                 tcg_gen_addi_i32(addr, addr, offset);
6968             /* Load PC into tmp and CPSR into tmp2.  */
6969             tmp = gen_ld32(addr, 0);
6970             tcg_gen_addi_i32(addr, addr, 4);
6971             tmp2 = gen_ld32(addr, 0);
6972             if (insn & (1 << 21)) {
6973                 /* Base writeback.  */
6974                 switch (i) {
6975                 case 0: offset = -8; break;
6976                 case 1: offset = 4; break;
6977                 case 2: offset = -4; break;
6978                 case 3: offset = 0; break;
6979                 default: abort();
6980                 }
6981                 if (offset)
6982                     tcg_gen_addi_i32(addr, addr, offset);
6983                 store_reg(s, rn, addr);
6984             } else {
6985                 tcg_temp_free_i32(addr);
6986             }
6987             gen_rfe(s, tmp, tmp2);
6988             return;
6989         } else if ((insn & 0x0e000000) == 0x0a000000) {
6990             /* branch link and change to thumb (blx <offset>) */
6991             int32_t offset;
6992
6993             val = (uint32_t)s->pc;
6994             tmp = tcg_temp_new_i32();
6995             tcg_gen_movi_i32(tmp, val);
6996             store_reg(s, 14, tmp);
6997             /* Sign-extend the 24-bit offset */
6998             offset = (((int32_t)insn) << 8) >> 8;
6999             /* offset * 4 + bit24 * 2 + (thumb bit) */
7000             val += (offset << 2) | ((insn >> 23) & 2) | 1;
7001             /* pipeline offset */
7002             val += 4;
7003             /* protected by ARCH(5); above, near the start of uncond block */
7004             gen_bx_im(s, val);
7005             return;
7006         } else if ((insn & 0x0e000f00) == 0x0c000100) {
7007             if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
7008                 /* iWMMXt register transfer.  */
7009                 if (env->cp15.c15_cpar & (1 << 1))
7010                     if (!disas_iwmmxt_insn(env, s, insn))
7011                         return;
7012             }
7013         } else if ((insn & 0x0fe00000) == 0x0c400000) {
7014             /* Coprocessor double register transfer.  */
7015             ARCH(5TE);
7016         } else if ((insn & 0x0f000010) == 0x0e000010) {
7017             /* Additional coprocessor register transfer.  */
7018         } else if ((insn & 0x0ff10020) == 0x01000000) {
7019             uint32_t mask;
7020             uint32_t val;
7021             /* cps (privileged) */
7022             if (IS_USER(s))
7023                 return;
7024             mask = val = 0;
7025             if (insn & (1 << 19)) {
7026                 if (insn & (1 << 8))
7027                     mask |= CPSR_A;
7028                 if (insn & (1 << 7))
7029                     mask |= CPSR_I;
7030                 if (insn & (1 << 6))
7031                     mask |= CPSR_F;
7032                 if (insn & (1 << 18))
7033                     val |= mask;
7034             }
7035             if (insn & (1 << 17)) {
7036                 mask |= CPSR_M;
7037                 val |= (insn & 0x1f);
7038             }
7039             if (mask) {
7040                 gen_set_psr_im(s, mask, 0, val);
7041             }
7042             return;
7043         }
7044         goto illegal_op;
7045     }
7046     if (cond != 0xe) {
7047         /* if not always execute, we generate a conditional jump to
7048            next instruction */
7049         s->condlabel = gen_new_label();
7050         gen_test_cc(cond ^ 1, s->condlabel);
7051         s->condjmp = 1;
7052     }
7053     if ((insn & 0x0f900000) == 0x03000000) {
7054         if ((insn & (1 << 21)) == 0) {
7055             ARCH(6T2);
7056             rd = (insn >> 12) & 0xf;
7057             val = ((insn >> 4) & 0xf000) | (insn & 0xfff);
7058             if ((insn & (1 << 22)) == 0) {
7059                 /* MOVW */
7060                 tmp = tcg_temp_new_i32();
7061                 tcg_gen_movi_i32(tmp, val);
7062             } else {
7063                 /* MOVT */
7064                 tmp = load_reg(s, rd);
7065                 tcg_gen_ext16u_i32(tmp, tmp);
7066                 tcg_gen_ori_i32(tmp, tmp, val << 16);
7067             }
7068             store_reg(s, rd, tmp);
7069         } else {
7070             if (((insn >> 12) & 0xf) != 0xf)
7071                 goto illegal_op;
7072             if (((insn >> 16) & 0xf) == 0) {
7073                 gen_nop_hint(s, insn & 0xff);
7074             } else {
7075                 /* CPSR = immediate */
7076                 val = insn & 0xff;
7077                 shift = ((insn >> 8) & 0xf) * 2;
7078                 if (shift)
7079                     val = (val >> shift) | (val << (32 - shift));
7080                 i = ((insn & (1 << 22)) != 0);
7081                 if (gen_set_psr_im(s, msr_mask(env, s, (insn >> 16) & 0xf, i), i, val))
7082                     goto illegal_op;
7083             }
7084         }
7085     } else if ((insn & 0x0f900000) == 0x01000000
7086                && (insn & 0x00000090) != 0x00000090) {
7087         /* miscellaneous instructions */
7088         op1 = (insn >> 21) & 3;
7089         sh = (insn >> 4) & 0xf;
7090         rm = insn & 0xf;
7091         switch (sh) {
7092         case 0x0: /* move program status register */
7093             if (op1 & 1) {
7094                 /* PSR = reg */
7095                 tmp = load_reg(s, rm);
7096                 i = ((op1 & 2) != 0);
7097                 if (gen_set_psr(s, msr_mask(env, s, (insn >> 16) & 0xf, i), i, tmp))
7098                     goto illegal_op;
7099             } else {
7100                 /* reg = PSR */
7101                 rd = (insn >> 12) & 0xf;
7102                 if (op1 & 2) {
7103                     if (IS_USER(s))
7104                         goto illegal_op;
7105                     tmp = load_cpu_field(spsr);
7106                 } else {
7107                     tmp = tcg_temp_new_i32();
7108                     gen_helper_cpsr_read(tmp);
7109                 }
7110                 store_reg(s, rd, tmp);
7111             }
7112             break;
7113         case 0x1:
7114             if (op1 == 1) {
7115                 /* branch/exchange thumb (bx).  */
7116                 ARCH(4T);
7117                 tmp = load_reg(s, rm);
7118                 gen_bx(s, tmp);
7119             } else if (op1 == 3) {
7120                 /* clz */
7121                 ARCH(5);
7122                 rd = (insn >> 12) & 0xf;
7123                 tmp = load_reg(s, rm);
7124                 gen_helper_clz(tmp, tmp);
7125                 store_reg(s, rd, tmp);
7126             } else {
7127                 goto illegal_op;
7128             }
7129             break;
7130         case 0x2:
7131             if (op1 == 1) {
7132                 ARCH(5J); /* bxj */
7133                 /* Trivial implementation equivalent to bx.  */
7134                 tmp = load_reg(s, rm);
7135                 gen_bx(s, tmp);
7136             } else {
7137                 goto illegal_op;
7138             }
7139             break;
7140         case 0x3:
7141             if (op1 != 1)
7142               goto illegal_op;
7143
7144             ARCH(5);
7145             /* branch link/exchange thumb (blx) */
7146             tmp = load_reg(s, rm);
7147             tmp2 = tcg_temp_new_i32();
7148             tcg_gen_movi_i32(tmp2, s->pc);
7149             store_reg(s, 14, tmp2);
7150             gen_bx(s, tmp);
7151             break;
7152         case 0x5: /* saturating add/subtract */
7153             ARCH(5TE);
7154             rd = (insn >> 12) & 0xf;
7155             rn = (insn >> 16) & 0xf;
7156             tmp = load_reg(s, rm);
7157             tmp2 = load_reg(s, rn);
7158             if (op1 & 2)
7159                 gen_helper_double_saturate(tmp2, tmp2);
7160             if (op1 & 1)
7161                 gen_helper_sub_saturate(tmp, tmp, tmp2);
7162             else
7163                 gen_helper_add_saturate(tmp, tmp, tmp2);
7164             tcg_temp_free_i32(tmp2);
7165             store_reg(s, rd, tmp);
7166             break;
7167         case 7:
7168             /* SMC instruction (op1 == 3)
7169                and undefined instructions (op1 == 0 || op1 == 2)
7170                will trap */
7171             if (op1 != 1) {
7172                 goto illegal_op;
7173             }
7174             /* bkpt */
7175             ARCH(5);
7176             gen_exception_insn(s, 4, EXCP_BKPT);
7177             break;
7178         case 0x8: /* signed multiply */
7179         case 0xa:
7180         case 0xc:
7181         case 0xe:
7182             ARCH(5TE);
7183             rs = (insn >> 8) & 0xf;
7184             rn = (insn >> 12) & 0xf;
7185             rd = (insn >> 16) & 0xf;
7186             if (op1 == 1) {
7187                 /* (32 * 16) >> 16 */
7188                 tmp = load_reg(s, rm);
7189                 tmp2 = load_reg(s, rs);
7190                 if (sh & 4)
7191                     tcg_gen_sari_i32(tmp2, tmp2, 16);
7192                 else
7193                     gen_sxth(tmp2);
7194                 tmp64 = gen_muls_i64_i32(tmp, tmp2);
7195                 tcg_gen_shri_i64(tmp64, tmp64, 16);
7196                 tmp = tcg_temp_new_i32();
7197                 tcg_gen_trunc_i64_i32(tmp, tmp64);
7198                 tcg_temp_free_i64(tmp64);
7199                 if ((sh & 2) == 0) {
7200                     tmp2 = load_reg(s, rn);
7201                     gen_helper_add_setq(tmp, tmp, tmp2);
7202                     tcg_temp_free_i32(tmp2);
7203                 }
7204                 store_reg(s, rd, tmp);
7205             } else {
7206                 /* 16 * 16 */
7207                 tmp = load_reg(s, rm);
7208                 tmp2 = load_reg(s, rs);
7209                 gen_mulxy(tmp, tmp2, sh & 2, sh & 4);
7210                 tcg_temp_free_i32(tmp2);
7211                 if (op1 == 2) {
7212                     tmp64 = tcg_temp_new_i64();
7213                     tcg_gen_ext_i32_i64(tmp64, tmp);
7214                     tcg_temp_free_i32(tmp);
7215                     gen_addq(s, tmp64, rn, rd);
7216                     gen_storeq_reg(s, rn, rd, tmp64);
7217                     tcg_temp_free_i64(tmp64);
7218                 } else {
7219                     if (op1 == 0) {
7220                         tmp2 = load_reg(s, rn);
7221                         gen_helper_add_setq(tmp, tmp, tmp2);
7222                         tcg_temp_free_i32(tmp2);
7223                     }
7224                     store_reg(s, rd, tmp);
7225                 }
7226             }
7227             break;
7228         default:
7229             goto illegal_op;
7230         }
7231     } else if (((insn & 0x0e000000) == 0 &&
7232                 (insn & 0x00000090) != 0x90) ||
7233                ((insn & 0x0e000000) == (1 << 25))) {
7234         int set_cc, logic_cc, shiftop;
7235
7236         op1 = (insn >> 21) & 0xf;
7237         set_cc = (insn >> 20) & 1;
7238         logic_cc = table_logic_cc[op1] & set_cc;
7239
7240         /* data processing instruction */
7241         if (insn & (1 << 25)) {
7242             /* immediate operand */
7243             val = insn & 0xff;
7244             shift = ((insn >> 8) & 0xf) * 2;
7245             if (shift) {
7246                 val = (val >> shift) | (val << (32 - shift));
7247             }
7248             tmp2 = tcg_temp_new_i32();
7249             tcg_gen_movi_i32(tmp2, val);
7250             if (logic_cc && shift) {
7251                 gen_set_CF_bit31(tmp2);
7252             }
7253         } else {
7254             /* register */
7255             rm = (insn) & 0xf;
7256             tmp2 = load_reg(s, rm);
7257             shiftop = (insn >> 5) & 3;
7258             if (!(insn & (1 << 4))) {
7259                 shift = (insn >> 7) & 0x1f;
7260                 gen_arm_shift_im(tmp2, shiftop, shift, logic_cc);
7261             } else {
7262                 rs = (insn >> 8) & 0xf;
7263                 tmp = load_reg(s, rs);
7264                 gen_arm_shift_reg(tmp2, shiftop, tmp, logic_cc);
7265             }
7266         }
7267         if (op1 != 0x0f && op1 != 0x0d) {
7268             rn = (insn >> 16) & 0xf;
7269             tmp = load_reg(s, rn);
7270         } else {
7271             TCGV_UNUSED(tmp);
7272         }
7273         rd = (insn >> 12) & 0xf;
7274         switch(op1) {
7275         case 0x00:
7276             tcg_gen_and_i32(tmp, tmp, tmp2);
7277             if (logic_cc) {
7278                 gen_logic_CC(tmp);
7279             }
7280             store_reg_bx(env, s, rd, tmp);
7281             break;
7282         case 0x01:
7283             tcg_gen_xor_i32(tmp, tmp, tmp2);
7284             if (logic_cc) {
7285                 gen_logic_CC(tmp);
7286             }
7287             store_reg_bx(env, s, rd, tmp);
7288             break;
7289         case 0x02:
7290             if (set_cc && rd == 15) {
7291                 /* SUBS r15, ... is used for exception return.  */
7292                 if (IS_USER(s)) {
7293                     goto illegal_op;
7294                 }
7295                 gen_helper_sub_cc(tmp, tmp, tmp2);
7296                 gen_exception_return(s, tmp);
7297             } else {
7298                 if (set_cc) {
7299                     gen_helper_sub_cc(tmp, tmp, tmp2);
7300                 } else {
7301                     tcg_gen_sub_i32(tmp, tmp, tmp2);
7302                 }
7303                 store_reg_bx(env, s, rd, tmp);
7304             }
7305             break;
7306         case 0x03:
7307             if (set_cc) {
7308                 gen_helper_sub_cc(tmp, tmp2, tmp);
7309             } else {
7310                 tcg_gen_sub_i32(tmp, tmp2, tmp);
7311             }
7312             store_reg_bx(env, s, rd, tmp);
7313             break;
7314         case 0x04:
7315             if (set_cc) {
7316                 gen_helper_add_cc(tmp, tmp, tmp2);
7317             } else {
7318                 tcg_gen_add_i32(tmp, tmp, tmp2);
7319             }
7320             store_reg_bx(env, s, rd, tmp);
7321             break;
7322         case 0x05:
7323             if (set_cc) {
7324                 gen_helper_adc_cc(tmp, tmp, tmp2);
7325             } else {
7326                 gen_add_carry(tmp, tmp, tmp2);
7327             }
7328             store_reg_bx(env, s, rd, tmp);
7329             break;
7330         case 0x06:
7331             if (set_cc) {
7332                 gen_helper_sbc_cc(tmp, tmp, tmp2);
7333             } else {
7334                 gen_sub_carry(tmp, tmp, tmp2);
7335             }
7336             store_reg_bx(env, s, rd, tmp);
7337             break;
7338         case 0x07:
7339             if (set_cc) {
7340                 gen_helper_sbc_cc(tmp, tmp2, tmp);
7341             } else {
7342                 gen_sub_carry(tmp, tmp2, tmp);
7343             }
7344             store_reg_bx(env, s, rd, tmp);
7345             break;
7346         case 0x08:
7347             if (set_cc) {
7348                 tcg_gen_and_i32(tmp, tmp, tmp2);
7349                 gen_logic_CC(tmp);
7350             }
7351             tcg_temp_free_i32(tmp);
7352             break;
7353         case 0x09:
7354             if (set_cc) {
7355                 tcg_gen_xor_i32(tmp, tmp, tmp2);
7356                 gen_logic_CC(tmp);
7357             }
7358             tcg_temp_free_i32(tmp);
7359             break;
7360         case 0x0a:
7361             if (set_cc) {
7362                 gen_helper_sub_cc(tmp, tmp, tmp2);
7363             }
7364             tcg_temp_free_i32(tmp);
7365             break;
7366         case 0x0b:
7367             if (set_cc) {
7368                 gen_helper_add_cc(tmp, tmp, tmp2);
7369             }
7370             tcg_temp_free_i32(tmp);
7371             break;
7372         case 0x0c:
7373             tcg_gen_or_i32(tmp, tmp, tmp2);
7374             if (logic_cc) {
7375                 gen_logic_CC(tmp);
7376             }
7377             store_reg_bx(env, s, rd, tmp);
7378             break;
7379         case 0x0d:
7380             if (logic_cc && rd == 15) {
7381                 /* MOVS r15, ... is used for exception return.  */
7382                 if (IS_USER(s)) {
7383                     goto illegal_op;
7384                 }
7385                 gen_exception_return(s, tmp2);
7386             } else {
7387                 if (logic_cc) {
7388                     gen_logic_CC(tmp2);
7389                 }
7390                 store_reg_bx(env, s, rd, tmp2);
7391             }
7392             break;
7393         case 0x0e:
7394             tcg_gen_andc_i32(tmp, tmp, tmp2);
7395             if (logic_cc) {
7396                 gen_logic_CC(tmp);
7397             }
7398             store_reg_bx(env, s, rd, tmp);
7399             break;
7400         default:
7401         case 0x0f:
7402             tcg_gen_not_i32(tmp2, tmp2);
7403             if (logic_cc) {
7404                 gen_logic_CC(tmp2);
7405             }
7406             store_reg_bx(env, s, rd, tmp2);
7407             break;
7408         }
7409         if (op1 != 0x0f && op1 != 0x0d) {
7410             tcg_temp_free_i32(tmp2);
7411         }
7412     } else {
7413         /* other instructions */
7414         op1 = (insn >> 24) & 0xf;
7415         switch(op1) {
7416         case 0x0:
7417         case 0x1:
7418             /* multiplies, extra load/stores */
7419             sh = (insn >> 5) & 3;
7420             if (sh == 0) {
7421                 if (op1 == 0x0) {
7422                     rd = (insn >> 16) & 0xf;
7423                     rn = (insn >> 12) & 0xf;
7424                     rs = (insn >> 8) & 0xf;
7425                     rm = (insn) & 0xf;
7426                     op1 = (insn >> 20) & 0xf;
7427                     switch (op1) {
7428                     case 0: case 1: case 2: case 3: case 6:
7429                         /* 32 bit mul */
7430                         tmp = load_reg(s, rs);
7431                         tmp2 = load_reg(s, rm);
7432                         tcg_gen_mul_i32(tmp, tmp, tmp2);
7433                         tcg_temp_free_i32(tmp2);
7434                         if (insn & (1 << 22)) {
7435                             /* Subtract (mls) */
7436                             ARCH(6T2);
7437                             tmp2 = load_reg(s, rn);
7438                             tcg_gen_sub_i32(tmp, tmp2, tmp);
7439                             tcg_temp_free_i32(tmp2);
7440                         } else if (insn & (1 << 21)) {
7441                             /* Add */
7442                             tmp2 = load_reg(s, rn);
7443                             tcg_gen_add_i32(tmp, tmp, tmp2);
7444                             tcg_temp_free_i32(tmp2);
7445                         }
7446                         if (insn & (1 << 20))
7447                             gen_logic_CC(tmp);
7448                         store_reg(s, rd, tmp);
7449                         break;
7450                     case 4:
7451                         /* 64 bit mul double accumulate (UMAAL) */
7452                         ARCH(6);
7453                         tmp = load_reg(s, rs);
7454                         tmp2 = load_reg(s, rm);
7455                         tmp64 = gen_mulu_i64_i32(tmp, tmp2);
7456                         gen_addq_lo(s, tmp64, rn);
7457                         gen_addq_lo(s, tmp64, rd);
7458                         gen_storeq_reg(s, rn, rd, tmp64);
7459                         tcg_temp_free_i64(tmp64);
7460                         break;
7461                     case 8: case 9: case 10: case 11:
7462                     case 12: case 13: case 14: case 15:
7463                         /* 64 bit mul: UMULL, UMLAL, SMULL, SMLAL. */
7464                         tmp = load_reg(s, rs);
7465                         tmp2 = load_reg(s, rm);
7466                         if (insn & (1 << 22)) {
7467                             tmp64 = gen_muls_i64_i32(tmp, tmp2);
7468                         } else {
7469                             tmp64 = gen_mulu_i64_i32(tmp, tmp2);
7470                         }
7471                         if (insn & (1 << 21)) { /* mult accumulate */
7472                             gen_addq(s, tmp64, rn, rd);
7473                         }
7474                         if (insn & (1 << 20)) {
7475                             gen_logicq_cc(tmp64);
7476                         }
7477                         gen_storeq_reg(s, rn, rd, tmp64);
7478                         tcg_temp_free_i64(tmp64);
7479                         break;
7480                     default:
7481                         goto illegal_op;
7482                     }
7483                 } else {
7484                     rn = (insn >> 16) & 0xf;
7485                     rd = (insn >> 12) & 0xf;
7486                     if (insn & (1 << 23)) {
7487                         /* load/store exclusive */
7488                         op1 = (insn >> 21) & 0x3;
7489                         if (op1)
7490                             ARCH(6K);
7491                         else
7492                             ARCH(6);
7493                         addr = tcg_temp_local_new_i32();
7494                         load_reg_var(s, addr, rn);
7495                         if (insn & (1 << 20)) {
7496                             switch (op1) {
7497                             case 0: /* ldrex */
7498                                 gen_load_exclusive(s, rd, 15, addr, 2);
7499                                 break;
7500                             case 1: /* ldrexd */
7501                                 gen_load_exclusive(s, rd, rd + 1, addr, 3);
7502                                 break;
7503                             case 2: /* ldrexb */
7504                                 gen_load_exclusive(s, rd, 15, addr, 0);
7505                                 break;
7506                             case 3: /* ldrexh */
7507                                 gen_load_exclusive(s, rd, 15, addr, 1);
7508                                 break;
7509                             default:
7510                                 abort();
7511                             }
7512                         } else {
7513                             rm = insn & 0xf;
7514                             switch (op1) {
7515                             case 0:  /*  strex */
7516                                 gen_store_exclusive(s, rd, rm, 15, addr, 2);
7517                                 break;
7518                             case 1: /*  strexd */
7519                                 gen_store_exclusive(s, rd, rm, rm + 1, addr, 3);
7520                                 break;
7521                             case 2: /*  strexb */
7522                                 gen_store_exclusive(s, rd, rm, 15, addr, 0);
7523                                 break;
7524                             case 3: /* strexh */
7525                                 gen_store_exclusive(s, rd, rm, 15, addr, 1);
7526                                 break;
7527                             default:
7528                                 abort();
7529                             }
7530                         }
7531                         tcg_temp_free(addr);
7532                     } else {
7533                         /* SWP instruction */
7534                         rm = (insn) & 0xf;
7535
7536                         /* ??? This is not really atomic.  However we know
7537                            we never have multiple CPUs running in parallel,
7538                            so it is good enough.  */
7539                         addr = load_reg(s, rn);
7540                         tmp = load_reg(s, rm);
7541                         if (insn & (1 << 22)) {
7542                             tmp2 = gen_ld8u(addr, IS_USER(s));
7543                             gen_st8(tmp, addr, IS_USER(s));
7544                         } else {
7545                             tmp2 = gen_ld32(addr, IS_USER(s));
7546                             gen_st32(tmp, addr, IS_USER(s));
7547                         }
7548                         tcg_temp_free_i32(addr);
7549                         store_reg(s, rd, tmp2);
7550                     }
7551                 }
7552             } else {
7553                 int address_offset;
7554                 int load;
7555                 /* Misc load/store */
7556                 rn = (insn >> 16) & 0xf;
7557                 rd = (insn >> 12) & 0xf;
7558                 addr = load_reg(s, rn);
7559                 if (insn & (1 << 24))
7560                     gen_add_datah_offset(s, insn, 0, addr);
7561                 address_offset = 0;
7562                 if (insn & (1 << 20)) {
7563                     /* load */
7564                     switch(sh) {
7565                     case 1:
7566                         tmp = gen_ld16u(addr, IS_USER(s));
7567                         break;
7568                     case 2:
7569                         tmp = gen_ld8s(addr, IS_USER(s));
7570                         break;
7571                     default:
7572                     case 3:
7573                         tmp = gen_ld16s(addr, IS_USER(s));
7574                         break;
7575                     }
7576                     load = 1;
7577                 } else if (sh & 2) {
7578                     ARCH(5TE);
7579                     /* doubleword */
7580                     if (sh & 1) {
7581                         /* store */
7582                         tmp = load_reg(s, rd);
7583                         gen_st32(tmp, addr, IS_USER(s));
7584                         tcg_gen_addi_i32(addr, addr, 4);
7585                         tmp = load_reg(s, rd + 1);
7586                         gen_st32(tmp, addr, IS_USER(s));
7587                         load = 0;
7588                     } else {
7589                         /* load */
7590                         tmp = gen_ld32(addr, IS_USER(s));
7591                         store_reg(s, rd, tmp);
7592                         tcg_gen_addi_i32(addr, addr, 4);
7593                         tmp = gen_ld32(addr, IS_USER(s));
7594                         rd++;
7595                         load = 1;
7596                     }
7597                     address_offset = -4;
7598                 } else {
7599                     /* store */
7600                     tmp = load_reg(s, rd);
7601                     gen_st16(tmp, addr, IS_USER(s));
7602                     load = 0;
7603                 }
7604                 /* Perform base writeback before the loaded value to
7605                    ensure correct behavior with overlapping index registers.
7606                    ldrd with base writeback is is undefined if the
7607                    destination and index registers overlap.  */
7608                 if (!(insn & (1 << 24))) {
7609                     gen_add_datah_offset(s, insn, address_offset, addr);
7610                     store_reg(s, rn, addr);
7611                 } else if (insn & (1 << 21)) {
7612                     if (address_offset)
7613                         tcg_gen_addi_i32(addr, addr, address_offset);
7614                     store_reg(s, rn, addr);
7615                 } else {
7616                     tcg_temp_free_i32(addr);
7617                 }
7618                 if (load) {
7619                     /* Complete the load.  */
7620                     store_reg(s, rd, tmp);
7621                 }
7622             }
7623             break;
7624         case 0x4:
7625         case 0x5:
7626             goto do_ldst;
7627         case 0x6:
7628         case 0x7:
7629             if (insn & (1 << 4)) {
7630                 ARCH(6);
7631                 /* Armv6 Media instructions.  */
7632                 rm = insn & 0xf;
7633                 rn = (insn >> 16) & 0xf;
7634                 rd = (insn >> 12) & 0xf;
7635                 rs = (insn >> 8) & 0xf;
7636                 switch ((insn >> 23) & 3) {
7637                 case 0: /* Parallel add/subtract.  */
7638                     op1 = (insn >> 20) & 7;
7639                     tmp = load_reg(s, rn);
7640                     tmp2 = load_reg(s, rm);
7641                     sh = (insn >> 5) & 7;
7642                     if ((op1 & 3) == 0 || sh == 5 || sh == 6)
7643                         goto illegal_op;
7644                     gen_arm_parallel_addsub(op1, sh, tmp, tmp2);
7645                     tcg_temp_free_i32(tmp2);
7646                     store_reg(s, rd, tmp);
7647                     break;
7648                 case 1:
7649                     if ((insn & 0x00700020) == 0) {
7650                         /* Halfword pack.  */
7651                         tmp = load_reg(s, rn);
7652                         tmp2 = load_reg(s, rm);
7653                         shift = (insn >> 7) & 0x1f;
7654                         if (insn & (1 << 6)) {
7655                             /* pkhtb */
7656                             if (shift == 0)
7657                                 shift = 31;
7658                             tcg_gen_sari_i32(tmp2, tmp2, shift);
7659                             tcg_gen_andi_i32(tmp, tmp, 0xffff0000);
7660                             tcg_gen_ext16u_i32(tmp2, tmp2);
7661                         } else {
7662                             /* pkhbt */
7663                             if (shift)
7664                                 tcg_gen_shli_i32(tmp2, tmp2, shift);
7665                             tcg_gen_ext16u_i32(tmp, tmp);
7666                             tcg_gen_andi_i32(tmp2, tmp2, 0xffff0000);
7667                         }
7668                         tcg_gen_or_i32(tmp, tmp, tmp2);
7669                         tcg_temp_free_i32(tmp2);
7670                         store_reg(s, rd, tmp);
7671                     } else if ((insn & 0x00200020) == 0x00200000) {
7672                         /* [us]sat */
7673                         tmp = load_reg(s, rm);
7674                         shift = (insn >> 7) & 0x1f;
7675                         if (insn & (1 << 6)) {
7676                             if (shift == 0)
7677                                 shift = 31;
7678                             tcg_gen_sari_i32(tmp, tmp, shift);
7679                         } else {
7680                             tcg_gen_shli_i32(tmp, tmp, shift);
7681                         }
7682                         sh = (insn >> 16) & 0x1f;
7683                         tmp2 = tcg_const_i32(sh);
7684                         if (insn & (1 << 22))
7685                           gen_helper_usat(tmp, tmp, tmp2);
7686                         else
7687                           gen_helper_ssat(tmp, tmp, tmp2);
7688                         tcg_temp_free_i32(tmp2);
7689                         store_reg(s, rd, tmp);
7690                     } else if ((insn & 0x00300fe0) == 0x00200f20) {
7691                         /* [us]sat16 */
7692                         tmp = load_reg(s, rm);
7693                         sh = (insn >> 16) & 0x1f;
7694                         tmp2 = tcg_const_i32(sh);
7695                         if (insn & (1 << 22))
7696                           gen_helper_usat16(tmp, tmp, tmp2);
7697                         else
7698                           gen_helper_ssat16(tmp, tmp, tmp2);
7699                         tcg_temp_free_i32(tmp2);
7700                         store_reg(s, rd, tmp);
7701                     } else if ((insn & 0x00700fe0) == 0x00000fa0) {
7702                         /* Select bytes.  */
7703                         tmp = load_reg(s, rn);
7704                         tmp2 = load_reg(s, rm);
7705                         tmp3 = tcg_temp_new_i32();
7706                         tcg_gen_ld_i32(tmp3, cpu_env, offsetof(CPUARMState, GE));
7707                         gen_helper_sel_flags(tmp, tmp3, tmp, tmp2);
7708                         tcg_temp_free_i32(tmp3);
7709                         tcg_temp_free_i32(tmp2);
7710                         store_reg(s, rd, tmp);
7711                     } else if ((insn & 0x000003e0) == 0x00000060) {
7712                         tmp = load_reg(s, rm);
7713                         shift = (insn >> 10) & 3;
7714                         /* ??? In many cases it's not necessary to do a
7715                            rotate, a shift is sufficient.  */
7716                         if (shift != 0)
7717                             tcg_gen_rotri_i32(tmp, tmp, shift * 8);
7718                         op1 = (insn >> 20) & 7;
7719                         switch (op1) {
7720                         case 0: gen_sxtb16(tmp);  break;
7721                         case 2: gen_sxtb(tmp);    break;
7722                         case 3: gen_sxth(tmp);    break;
7723                         case 4: gen_uxtb16(tmp);  break;
7724                         case 6: gen_uxtb(tmp);    break;
7725                         case 7: gen_uxth(tmp);    break;
7726                         default: goto illegal_op;
7727                         }
7728                         if (rn != 15) {
7729                             tmp2 = load_reg(s, rn);
7730                             if ((op1 & 3) == 0) {
7731                                 gen_add16(tmp, tmp2);
7732                             } else {
7733                                 tcg_gen_add_i32(tmp, tmp, tmp2);
7734                                 tcg_temp_free_i32(tmp2);
7735                             }
7736                         }
7737                         store_reg(s, rd, tmp);
7738                     } else if ((insn & 0x003f0f60) == 0x003f0f20) {
7739                         /* rev */
7740                         tmp = load_reg(s, rm);
7741                         if (insn & (1 << 22)) {
7742                             if (insn & (1 << 7)) {
7743                                 gen_revsh(tmp);
7744                             } else {
7745                                 ARCH(6T2);
7746                                 gen_helper_rbit(tmp, tmp);
7747                             }
7748                         } else {
7749                             if (insn & (1 << 7))
7750                                 gen_rev16(tmp);
7751                             else
7752                                 tcg_gen_bswap32_i32(tmp, tmp);
7753                         }
7754                         store_reg(s, rd, tmp);
7755                     } else {
7756                         goto illegal_op;
7757                     }
7758                     break;
7759                 case 2: /* Multiplies (Type 3).  */
7760                     switch ((insn >> 20) & 0x7) {
7761                     case 5:
7762                         if (((insn >> 6) ^ (insn >> 7)) & 1) {
7763                             /* op2 not 00x or 11x : UNDEF */
7764                             goto illegal_op;
7765                         }
7766                         /* Signed multiply most significant [accumulate].
7767                            (SMMUL, SMMLA, SMMLS) */
7768                         tmp = load_reg(s, rm);
7769                         tmp2 = load_reg(s, rs);
7770                         tmp64 = gen_muls_i64_i32(tmp, tmp2);
7771
7772                         if (rd != 15) {
7773                             tmp = load_reg(s, rd);
7774                             if (insn & (1 << 6)) {
7775                                 tmp64 = gen_subq_msw(tmp64, tmp);
7776                             } else {
7777                                 tmp64 = gen_addq_msw(tmp64, tmp);
7778                             }
7779                         }
7780                         if (insn & (1 << 5)) {
7781                             tcg_gen_addi_i64(tmp64, tmp64, 0x80000000u);
7782                         }
7783                         tcg_gen_shri_i64(tmp64, tmp64, 32);
7784                         tmp = tcg_temp_new_i32();
7785                         tcg_gen_trunc_i64_i32(tmp, tmp64);
7786                         tcg_temp_free_i64(tmp64);
7787                         store_reg(s, rn, tmp);
7788                         break;
7789                     case 0:
7790                     case 4:
7791                         /* SMLAD, SMUAD, SMLSD, SMUSD, SMLALD, SMLSLD */
7792                         if (insn & (1 << 7)) {
7793                             goto illegal_op;
7794                         }
7795                         tmp = load_reg(s, rm);
7796                         tmp2 = load_reg(s, rs);
7797                         if (insn & (1 << 5))
7798                             gen_swap_half(tmp2);
7799                         gen_smul_dual(tmp, tmp2);
7800                         if (insn & (1 << 6)) {
7801                             /* This subtraction cannot overflow. */
7802                             tcg_gen_sub_i32(tmp, tmp, tmp2);
7803                         } else {
7804                             /* This addition cannot overflow 32 bits;
7805                              * however it may overflow considered as a signed
7806                              * operation, in which case we must set the Q flag.
7807                              */
7808                             gen_helper_add_setq(tmp, tmp, tmp2);
7809                         }
7810                         tcg_temp_free_i32(tmp2);
7811                         if (insn & (1 << 22)) {
7812                             /* smlald, smlsld */
7813                             tmp64 = tcg_temp_new_i64();
7814                             tcg_gen_ext_i32_i64(tmp64, tmp);
7815                             tcg_temp_free_i32(tmp);
7816                             gen_addq(s, tmp64, rd, rn);
7817                             gen_storeq_reg(s, rd, rn, tmp64);
7818                             tcg_temp_free_i64(tmp64);
7819                         } else {
7820                             /* smuad, smusd, smlad, smlsd */
7821                             if (rd != 15)
7822                               {
7823                                 tmp2 = load_reg(s, rd);
7824                                 gen_helper_add_setq(tmp, tmp, tmp2);
7825                                 tcg_temp_free_i32(tmp2);
7826                               }
7827                             store_reg(s, rn, tmp);
7828                         }
7829                         break;
7830                     case 1:
7831                     case 3:
7832                         /* SDIV, UDIV */
7833                         if (!arm_feature(env, ARM_FEATURE_ARM_DIV)) {
7834                             goto illegal_op;
7835                         }
7836                         if (((insn >> 5) & 7) || (rd != 15)) {
7837                             goto illegal_op;
7838                         }
7839                         tmp = load_reg(s, rm);
7840                         tmp2 = load_reg(s, rs);
7841                         if (insn & (1 << 21)) {
7842                             gen_helper_udiv(tmp, tmp, tmp2);
7843                         } else {
7844                             gen_helper_sdiv(tmp, tmp, tmp2);
7845                         }
7846                         tcg_temp_free_i32(tmp2);
7847                         store_reg(s, rn, tmp);
7848                         break;
7849                     default:
7850                         goto illegal_op;
7851                     }
7852                     break;
7853                 case 3:
7854                     op1 = ((insn >> 17) & 0x38) | ((insn >> 5) & 7);
7855                     switch (op1) {
7856                     case 0: /* Unsigned sum of absolute differences.  */
7857                         ARCH(6);
7858                         tmp = load_reg(s, rm);
7859                         tmp2 = load_reg(s, rs);
7860                         gen_helper_usad8(tmp, tmp, tmp2);
7861                         tcg_temp_free_i32(tmp2);
7862                         if (rd != 15) {
7863                             tmp2 = load_reg(s, rd);
7864                             tcg_gen_add_i32(tmp, tmp, tmp2);
7865                             tcg_temp_free_i32(tmp2);
7866                         }
7867                         store_reg(s, rn, tmp);
7868                         break;
7869                     case 0x20: case 0x24: case 0x28: case 0x2c:
7870                         /* Bitfield insert/clear.  */
7871                         ARCH(6T2);
7872                         shift = (insn >> 7) & 0x1f;
7873                         i = (insn >> 16) & 0x1f;
7874                         i = i + 1 - shift;
7875                         if (rm == 15) {
7876                             tmp = tcg_temp_new_i32();
7877                             tcg_gen_movi_i32(tmp, 0);
7878                         } else {
7879                             tmp = load_reg(s, rm);
7880                         }
7881                         if (i != 32) {
7882                             tmp2 = load_reg(s, rd);
7883                             gen_bfi(tmp, tmp2, tmp, shift, (1u << i) - 1);
7884                             tcg_temp_free_i32(tmp2);
7885                         }
7886                         store_reg(s, rd, tmp);
7887                         break;
7888                     case 0x12: case 0x16: case 0x1a: case 0x1e: /* sbfx */
7889                     case 0x32: case 0x36: case 0x3a: case 0x3e: /* ubfx */
7890                         ARCH(6T2);
7891                         tmp = load_reg(s, rm);
7892                         shift = (insn >> 7) & 0x1f;
7893                         i = ((insn >> 16) & 0x1f) + 1;
7894                         if (shift + i > 32)
7895                             goto illegal_op;
7896                         if (i < 32) {
7897                             if (op1 & 0x20) {
7898                                 gen_ubfx(tmp, shift, (1u << i) - 1);
7899                             } else {
7900                                 gen_sbfx(tmp, shift, i);
7901                             }
7902                         }
7903                         store_reg(s, rd, tmp);
7904                         break;
7905                     default:
7906                         goto illegal_op;
7907                     }
7908                     break;
7909                 }
7910                 break;
7911             }
7912         do_ldst:
7913             /* Check for undefined extension instructions
7914              * per the ARM Bible IE:
7915              * xxxx 0111 1111 xxxx  xxxx xxxx 1111 xxxx
7916              */
7917             sh = (0xf << 20) | (0xf << 4);
7918             if (op1 == 0x7 && ((insn & sh) == sh))
7919             {
7920                 goto illegal_op;
7921             }
7922             /* load/store byte/word */
7923             rn = (insn >> 16) & 0xf;
7924             rd = (insn >> 12) & 0xf;
7925             tmp2 = load_reg(s, rn);
7926             i = (IS_USER(s) || (insn & 0x01200000) == 0x00200000);
7927             if (insn & (1 << 24))
7928                 gen_add_data_offset(s, insn, tmp2);
7929             if (insn & (1 << 20)) {
7930                 /* load */
7931                 if (insn & (1 << 22)) {
7932                     tmp = gen_ld8u(tmp2, i);
7933                 } else {
7934                     tmp = gen_ld32(tmp2, i);
7935                 }
7936             } else {
7937                 /* store */
7938                 tmp = load_reg(s, rd);
7939                 if (insn & (1 << 22))
7940                     gen_st8(tmp, tmp2, i);
7941                 else
7942                     gen_st32(tmp, tmp2, i);
7943             }
7944             if (!(insn & (1 << 24))) {
7945                 gen_add_data_offset(s, insn, tmp2);
7946                 store_reg(s, rn, tmp2);
7947             } else if (insn & (1 << 21)) {
7948                 store_reg(s, rn, tmp2);
7949             } else {
7950                 tcg_temp_free_i32(tmp2);
7951             }
7952             if (insn & (1 << 20)) {
7953                 /* Complete the load.  */
7954                 store_reg_from_load(env, s, rd, tmp);
7955             }
7956             break;
7957         case 0x08:
7958         case 0x09:
7959             {
7960                 int j, n, user, loaded_base;
7961                 TCGv loaded_var;
7962                 /* load/store multiple words */
7963                 /* XXX: store correct base if write back */
7964                 user = 0;
7965                 if (insn & (1 << 22)) {
7966                     if (IS_USER(s))
7967                         goto illegal_op; /* only usable in supervisor mode */
7968
7969                     if ((insn & (1 << 15)) == 0)
7970                         user = 1;
7971                 }
7972                 rn = (insn >> 16) & 0xf;
7973                 addr = load_reg(s, rn);
7974
7975                 /* compute total size */
7976                 loaded_base = 0;
7977                 TCGV_UNUSED(loaded_var);
7978                 n = 0;
7979                 for(i=0;i<16;i++) {
7980                     if (insn & (1 << i))
7981                         n++;
7982                 }
7983                 /* XXX: test invalid n == 0 case ? */
7984                 if (insn & (1 << 23)) {
7985                     if (insn & (1 << 24)) {
7986                         /* pre increment */
7987                         tcg_gen_addi_i32(addr, addr, 4);
7988                     } else {
7989                         /* post increment */
7990                     }
7991                 } else {
7992                     if (insn & (1 << 24)) {
7993                         /* pre decrement */
7994                         tcg_gen_addi_i32(addr, addr, -(n * 4));
7995                     } else {
7996                         /* post decrement */
7997                         if (n != 1)
7998                         tcg_gen_addi_i32(addr, addr, -((n - 1) * 4));
7999                     }
8000                 }
8001                 j = 0;
8002                 for(i=0;i<16;i++) {
8003                     if (insn & (1 << i)) {
8004                         if (insn & (1 << 20)) {
8005                             /* load */
8006                             tmp = gen_ld32(addr, IS_USER(s));
8007                             if (user) {
8008                                 tmp2 = tcg_const_i32(i);
8009                                 gen_helper_set_user_reg(tmp2, tmp);
8010                                 tcg_temp_free_i32(tmp2);
8011                                 tcg_temp_free_i32(tmp);
8012                             } else if (i == rn) {
8013                                 loaded_var = tmp;
8014                                 loaded_base = 1;
8015                             } else {
8016                                 store_reg_from_load(env, s, i, tmp);
8017                             }
8018                         } else {
8019                             /* store */
8020                             if (i == 15) {
8021                                 /* special case: r15 = PC + 8 */
8022                                 val = (long)s->pc + 4;
8023                                 tmp = tcg_temp_new_i32();
8024                                 tcg_gen_movi_i32(tmp, val);
8025                             } else if (user) {
8026                                 tmp = tcg_temp_new_i32();
8027                                 tmp2 = tcg_const_i32(i);
8028                                 gen_helper_get_user_reg(tmp, tmp2);
8029                                 tcg_temp_free_i32(tmp2);
8030                             } else {
8031                                 tmp = load_reg(s, i);
8032                             }
8033                             gen_st32(tmp, addr, IS_USER(s));
8034                         }
8035                         j++;
8036                         /* no need to add after the last transfer */
8037                         if (j != n)
8038                             tcg_gen_addi_i32(addr, addr, 4);
8039                     }
8040                 }
8041                 if (insn & (1 << 21)) {
8042                     /* write back */
8043                     if (insn & (1 << 23)) {
8044                         if (insn & (1 << 24)) {
8045                             /* pre increment */
8046                         } else {
8047                             /* post increment */
8048                             tcg_gen_addi_i32(addr, addr, 4);
8049                         }
8050                     } else {
8051                         if (insn & (1 << 24)) {
8052                             /* pre decrement */
8053                             if (n != 1)
8054                                 tcg_gen_addi_i32(addr, addr, -((n - 1) * 4));
8055                         } else {
8056                             /* post decrement */
8057                             tcg_gen_addi_i32(addr, addr, -(n * 4));
8058                         }
8059                     }
8060                     store_reg(s, rn, addr);
8061                 } else {
8062                     tcg_temp_free_i32(addr);
8063                 }
8064                 if (loaded_base) {
8065                     store_reg(s, rn, loaded_var);
8066                 }
8067                 if ((insn & (1 << 22)) && !user) {
8068                     /* Restore CPSR from SPSR.  */
8069                     tmp = load_cpu_field(spsr);
8070                     gen_set_cpsr(tmp, 0xffffffff);
8071                     tcg_temp_free_i32(tmp);
8072                     s->is_jmp = DISAS_UPDATE;
8073                 }
8074             }
8075             break;
8076         case 0xa:
8077         case 0xb:
8078             {
8079                 int32_t offset;
8080
8081                 /* branch (and link) */
8082                 val = (int32_t)s->pc;
8083                 if (insn & (1 << 24)) {
8084                     tmp = tcg_temp_new_i32();
8085                     tcg_gen_movi_i32(tmp, val);
8086                     store_reg(s, 14, tmp);
8087                 }
8088                 offset = (((int32_t)insn << 8) >> 8);
8089                 val += (offset << 2) + 4;
8090                 gen_jmp(s, val);
8091             }
8092             break;
8093         case 0xc:
8094         case 0xd:
8095         case 0xe:
8096             /* Coprocessor.  */
8097             if (disas_coproc_insn(env, s, insn))
8098                 goto illegal_op;
8099             break;
8100         case 0xf:
8101             /* swi */
8102             gen_set_pc_im(s->pc);
8103             s->is_jmp = DISAS_SWI;
8104             break;
8105         default:
8106         illegal_op:
8107             gen_exception_insn(s, 4, EXCP_UDEF);
8108             break;
8109         }
8110     }
8111 }
8112
8113 /* Return true if this is a Thumb-2 logical op.  */
8114 static int
8115 thumb2_logic_op(int op)
8116 {
8117     return (op < 8);
8118 }
8119
8120 /* Generate code for a Thumb-2 data processing operation.  If CONDS is nonzero
8121    then set condition code flags based on the result of the operation.
8122    If SHIFTER_OUT is nonzero then set the carry flag for logical operations
8123    to the high bit of T1.
8124    Returns zero if the opcode is valid.  */
8125
8126 static int
8127 gen_thumb2_data_op(DisasContext *s, int op, int conds, uint32_t shifter_out, TCGv t0, TCGv t1)
8128 {
8129     int logic_cc;
8130
8131     logic_cc = 0;
8132     switch (op) {
8133     case 0: /* and */
8134         tcg_gen_and_i32(t0, t0, t1);
8135         logic_cc = conds;
8136         break;
8137     case 1: /* bic */
8138         tcg_gen_andc_i32(t0, t0, t1);
8139         logic_cc = conds;
8140         break;
8141     case 2: /* orr */
8142         tcg_gen_or_i32(t0, t0, t1);
8143         logic_cc = conds;
8144         break;
8145     case 3: /* orn */
8146         tcg_gen_orc_i32(t0, t0, t1);
8147         logic_cc = conds;
8148         break;
8149     case 4: /* eor */
8150         tcg_gen_xor_i32(t0, t0, t1);
8151         logic_cc = conds;
8152         break;
8153     case 8: /* add */
8154         if (conds)
8155             gen_helper_add_cc(t0, t0, t1);
8156         else
8157             tcg_gen_add_i32(t0, t0, t1);
8158         break;
8159     case 10: /* adc */
8160         if (conds)
8161             gen_helper_adc_cc(t0, t0, t1);
8162         else
8163             gen_adc(t0, t1);
8164         break;
8165     case 11: /* sbc */
8166         if (conds)
8167             gen_helper_sbc_cc(t0, t0, t1);
8168         else
8169             gen_sub_carry(t0, t0, t1);
8170         break;
8171     case 13: /* sub */
8172         if (conds)
8173             gen_helper_sub_cc(t0, t0, t1);
8174         else
8175             tcg_gen_sub_i32(t0, t0, t1);
8176         break;
8177     case 14: /* rsb */
8178         if (conds)
8179             gen_helper_sub_cc(t0, t1, t0);
8180         else
8181             tcg_gen_sub_i32(t0, t1, t0);
8182         break;
8183     default: /* 5, 6, 7, 9, 12, 15. */
8184         return 1;
8185     }
8186     if (logic_cc) {
8187         gen_logic_CC(t0);
8188         if (shifter_out)
8189             gen_set_CF_bit31(t1);
8190     }
8191     return 0;
8192 }
8193
8194 /* Translate a 32-bit thumb instruction.  Returns nonzero if the instruction
8195    is not legal.  */
8196 static int disas_thumb2_insn(CPUARMState *env, DisasContext *s, uint16_t insn_hw1)
8197 {
8198     uint32_t insn, imm, shift, offset;
8199     uint32_t rd, rn, rm, rs;
8200     TCGv tmp;
8201     TCGv tmp2;
8202     TCGv tmp3;
8203     TCGv addr;
8204     TCGv_i64 tmp64;
8205     int op;
8206     int shiftop;
8207     int conds;
8208     int logic_cc;
8209
8210     if (!(arm_feature(env, ARM_FEATURE_THUMB2)
8211           || arm_feature (env, ARM_FEATURE_M))) {
8212         /* Thumb-1 cores may need to treat bl and blx as a pair of
8213            16-bit instructions to get correct prefetch abort behavior.  */
8214         insn = insn_hw1;
8215         if ((insn & (1 << 12)) == 0) {
8216             ARCH(5);
8217             /* Second half of blx.  */
8218             offset = ((insn & 0x7ff) << 1);
8219             tmp = load_reg(s, 14);
8220             tcg_gen_addi_i32(tmp, tmp, offset);
8221             tcg_gen_andi_i32(tmp, tmp, 0xfffffffc);
8222
8223             tmp2 = tcg_temp_new_i32();
8224             tcg_gen_movi_i32(tmp2, s->pc | 1);
8225             store_reg(s, 14, tmp2);
8226             gen_bx(s, tmp);
8227             return 0;
8228         }
8229         if (insn & (1 << 11)) {
8230             /* Second half of bl.  */
8231             offset = ((insn & 0x7ff) << 1) | 1;
8232             tmp = load_reg(s, 14);
8233             tcg_gen_addi_i32(tmp, tmp, offset);
8234
8235             tmp2 = tcg_temp_new_i32();
8236             tcg_gen_movi_i32(tmp2, s->pc | 1);
8237             store_reg(s, 14, tmp2);
8238             gen_bx(s, tmp);
8239             return 0;
8240         }
8241         if ((s->pc & ~TARGET_PAGE_MASK) == 0) {
8242             /* Instruction spans a page boundary.  Implement it as two
8243                16-bit instructions in case the second half causes an
8244                prefetch abort.  */
8245             offset = ((int32_t)insn << 21) >> 9;
8246             tcg_gen_movi_i32(cpu_R[14], s->pc + 2 + offset);
8247             return 0;
8248         }
8249         /* Fall through to 32-bit decode.  */
8250     }
8251
8252     insn = arm_lduw_code(s->pc, s->bswap_code);
8253     s->pc += 2;
8254     insn |= (uint32_t)insn_hw1 << 16;
8255
8256     if ((insn & 0xf800e800) != 0xf000e800) {
8257         ARCH(6T2);
8258     }
8259
8260     rn = (insn >> 16) & 0xf;
8261     rs = (insn >> 12) & 0xf;
8262     rd = (insn >> 8) & 0xf;
8263     rm = insn & 0xf;
8264     switch ((insn >> 25) & 0xf) {
8265     case 0: case 1: case 2: case 3:
8266         /* 16-bit instructions.  Should never happen.  */
8267         abort();
8268     case 4:
8269         if (insn & (1 << 22)) {
8270             /* Other load/store, table branch.  */
8271             if (insn & 0x01200000) {
8272                 /* Load/store doubleword.  */
8273                 if (rn == 15) {
8274                     addr = tcg_temp_new_i32();
8275                     tcg_gen_movi_i32(addr, s->pc & ~3);
8276                 } else {
8277                     addr = load_reg(s, rn);
8278                 }
8279                 offset = (insn & 0xff) * 4;
8280                 if ((insn & (1 << 23)) == 0)
8281                     offset = -offset;
8282                 if (insn & (1 << 24)) {
8283                     tcg_gen_addi_i32(addr, addr, offset);
8284                     offset = 0;
8285                 }
8286                 if (insn & (1 << 20)) {
8287                     /* ldrd */
8288                     tmp = gen_ld32(addr, IS_USER(s));
8289                     store_reg(s, rs, tmp);
8290                     tcg_gen_addi_i32(addr, addr, 4);
8291                     tmp = gen_ld32(addr, IS_USER(s));
8292                     store_reg(s, rd, tmp);
8293                 } else {
8294                     /* strd */
8295                     tmp = load_reg(s, rs);
8296                     gen_st32(tmp, addr, IS_USER(s));
8297                     tcg_gen_addi_i32(addr, addr, 4);
8298                     tmp = load_reg(s, rd);
8299                     gen_st32(tmp, addr, IS_USER(s));
8300                 }
8301                 if (insn & (1 << 21)) {
8302                     /* Base writeback.  */
8303                     if (rn == 15)
8304                         goto illegal_op;
8305                     tcg_gen_addi_i32(addr, addr, offset - 4);
8306                     store_reg(s, rn, addr);
8307                 } else {
8308                     tcg_temp_free_i32(addr);
8309                 }
8310             } else if ((insn & (1 << 23)) == 0) {
8311                 /* Load/store exclusive word.  */
8312                 addr = tcg_temp_local_new();
8313                 load_reg_var(s, addr, rn);
8314                 tcg_gen_addi_i32(addr, addr, (insn & 0xff) << 2);
8315                 if (insn & (1 << 20)) {
8316                     gen_load_exclusive(s, rs, 15, addr, 2);
8317                 } else {
8318                     gen_store_exclusive(s, rd, rs, 15, addr, 2);
8319                 }
8320                 tcg_temp_free(addr);
8321             } else if ((insn & (1 << 6)) == 0) {
8322                 /* Table Branch.  */
8323                 if (rn == 15) {
8324                     addr = tcg_temp_new_i32();
8325                     tcg_gen_movi_i32(addr, s->pc);
8326                 } else {
8327                     addr = load_reg(s, rn);
8328                 }
8329                 tmp = load_reg(s, rm);
8330                 tcg_gen_add_i32(addr, addr, tmp);
8331                 if (insn & (1 << 4)) {
8332                     /* tbh */
8333                     tcg_gen_add_i32(addr, addr, tmp);
8334                     tcg_temp_free_i32(tmp);
8335                     tmp = gen_ld16u(addr, IS_USER(s));
8336                 } else { /* tbb */
8337                     tcg_temp_free_i32(tmp);
8338                     tmp = gen_ld8u(addr, IS_USER(s));
8339                 }
8340                 tcg_temp_free_i32(addr);
8341                 tcg_gen_shli_i32(tmp, tmp, 1);
8342                 tcg_gen_addi_i32(tmp, tmp, s->pc);
8343                 store_reg(s, 15, tmp);
8344             } else {
8345                 /* Load/store exclusive byte/halfword/doubleword.  */
8346                 ARCH(7);
8347                 op = (insn >> 4) & 0x3;
8348                 if (op == 2) {
8349                     goto illegal_op;
8350                 }
8351                 addr = tcg_temp_local_new();
8352                 load_reg_var(s, addr, rn);
8353                 if (insn & (1 << 20)) {
8354                     gen_load_exclusive(s, rs, rd, addr, op);
8355                 } else {
8356                     gen_store_exclusive(s, rm, rs, rd, addr, op);
8357                 }
8358                 tcg_temp_free(addr);
8359             }
8360         } else {
8361             /* Load/store multiple, RFE, SRS.  */
8362             if (((insn >> 23) & 1) == ((insn >> 24) & 1)) {
8363                 /* Not available in user mode.  */
8364                 if (IS_USER(s))
8365                     goto illegal_op;
8366                 if (insn & (1 << 20)) {
8367                     /* rfe */
8368                     addr = load_reg(s, rn);
8369                     if ((insn & (1 << 24)) == 0)
8370                         tcg_gen_addi_i32(addr, addr, -8);
8371                     /* Load PC into tmp and CPSR into tmp2.  */
8372                     tmp = gen_ld32(addr, 0);
8373                     tcg_gen_addi_i32(addr, addr, 4);
8374                     tmp2 = gen_ld32(addr, 0);
8375                     if (insn & (1 << 21)) {
8376                         /* Base writeback.  */
8377                         if (insn & (1 << 24)) {
8378                             tcg_gen_addi_i32(addr, addr, 4);
8379                         } else {
8380                             tcg_gen_addi_i32(addr, addr, -4);
8381                         }
8382                         store_reg(s, rn, addr);
8383                     } else {
8384                         tcg_temp_free_i32(addr);
8385                     }
8386                     gen_rfe(s, tmp, tmp2);
8387                 } else {
8388                     /* srs */
8389                     op = (insn & 0x1f);
8390                     addr = tcg_temp_new_i32();
8391                     tmp = tcg_const_i32(op);
8392                     gen_helper_get_r13_banked(addr, cpu_env, tmp);
8393                     tcg_temp_free_i32(tmp);
8394                     if ((insn & (1 << 24)) == 0) {
8395                         tcg_gen_addi_i32(addr, addr, -8);
8396                     }
8397                     tmp = load_reg(s, 14);
8398                     gen_st32(tmp, addr, 0);
8399                     tcg_gen_addi_i32(addr, addr, 4);
8400                     tmp = tcg_temp_new_i32();
8401                     gen_helper_cpsr_read(tmp);
8402                     gen_st32(tmp, addr, 0);
8403                     if (insn & (1 << 21)) {
8404                         if ((insn & (1 << 24)) == 0) {
8405                             tcg_gen_addi_i32(addr, addr, -4);
8406                         } else {
8407                             tcg_gen_addi_i32(addr, addr, 4);
8408                         }
8409                         tmp = tcg_const_i32(op);
8410                         gen_helper_set_r13_banked(cpu_env, tmp, addr);
8411                         tcg_temp_free_i32(tmp);
8412                     } else {
8413                         tcg_temp_free_i32(addr);
8414                     }
8415                 }
8416             } else {
8417                 int i, loaded_base = 0;
8418                 TCGv loaded_var;
8419                 /* Load/store multiple.  */
8420                 addr = load_reg(s, rn);
8421                 offset = 0;
8422                 for (i = 0; i < 16; i++) {
8423                     if (insn & (1 << i))
8424                         offset += 4;
8425                 }
8426                 if (insn & (1 << 24)) {
8427                     tcg_gen_addi_i32(addr, addr, -offset);
8428                 }
8429
8430                 TCGV_UNUSED(loaded_var);
8431                 for (i = 0; i < 16; i++) {
8432                     if ((insn & (1 << i)) == 0)
8433                         continue;
8434                     if (insn & (1 << 20)) {
8435                         /* Load.  */
8436                         tmp = gen_ld32(addr, IS_USER(s));
8437                         if (i == 15) {
8438                             gen_bx(s, tmp);
8439                         } else if (i == rn) {
8440                             loaded_var = tmp;
8441                             loaded_base = 1;
8442                         } else {
8443                             store_reg(s, i, tmp);
8444                         }
8445                     } else {
8446                         /* Store.  */
8447                         tmp = load_reg(s, i);
8448                         gen_st32(tmp, addr, IS_USER(s));
8449                     }
8450                     tcg_gen_addi_i32(addr, addr, 4);
8451                 }
8452                 if (loaded_base) {
8453                     store_reg(s, rn, loaded_var);
8454                 }
8455                 if (insn & (1 << 21)) {
8456                     /* Base register writeback.  */
8457                     if (insn & (1 << 24)) {
8458                         tcg_gen_addi_i32(addr, addr, -offset);
8459                     }
8460                     /* Fault if writeback register is in register list.  */
8461                     if (insn & (1 << rn))
8462                         goto illegal_op;
8463                     store_reg(s, rn, addr);
8464                 } else {
8465                     tcg_temp_free_i32(addr);
8466                 }
8467             }
8468         }
8469         break;
8470     case 5:
8471
8472         op = (insn >> 21) & 0xf;
8473         if (op == 6) {
8474             /* Halfword pack.  */
8475             tmp = load_reg(s, rn);
8476             tmp2 = load_reg(s, rm);
8477             shift = ((insn >> 10) & 0x1c) | ((insn >> 6) & 0x3);
8478             if (insn & (1 << 5)) {
8479                 /* pkhtb */
8480                 if (shift == 0)
8481                     shift = 31;
8482                 tcg_gen_sari_i32(tmp2, tmp2, shift);
8483                 tcg_gen_andi_i32(tmp, tmp, 0xffff0000);
8484                 tcg_gen_ext16u_i32(tmp2, tmp2);
8485             } else {
8486                 /* pkhbt */
8487                 if (shift)
8488                     tcg_gen_shli_i32(tmp2, tmp2, shift);
8489                 tcg_gen_ext16u_i32(tmp, tmp);
8490                 tcg_gen_andi_i32(tmp2, tmp2, 0xffff0000);
8491             }
8492             tcg_gen_or_i32(tmp, tmp, tmp2);
8493             tcg_temp_free_i32(tmp2);
8494             store_reg(s, rd, tmp);
8495         } else {
8496             /* Data processing register constant shift.  */
8497             if (rn == 15) {
8498                 tmp = tcg_temp_new_i32();
8499                 tcg_gen_movi_i32(tmp, 0);
8500             } else {
8501                 tmp = load_reg(s, rn);
8502             }
8503             tmp2 = load_reg(s, rm);
8504
8505             shiftop = (insn >> 4) & 3;
8506             shift = ((insn >> 6) & 3) | ((insn >> 10) & 0x1c);
8507             conds = (insn & (1 << 20)) != 0;
8508             logic_cc = (conds && thumb2_logic_op(op));
8509             gen_arm_shift_im(tmp2, shiftop, shift, logic_cc);
8510             if (gen_thumb2_data_op(s, op, conds, 0, tmp, tmp2))
8511                 goto illegal_op;
8512             tcg_temp_free_i32(tmp2);
8513             if (rd != 15) {
8514                 store_reg(s, rd, tmp);
8515             } else {
8516                 tcg_temp_free_i32(tmp);
8517             }
8518         }
8519         break;
8520     case 13: /* Misc data processing.  */
8521         op = ((insn >> 22) & 6) | ((insn >> 7) & 1);
8522         if (op < 4 && (insn & 0xf000) != 0xf000)
8523             goto illegal_op;
8524         switch (op) {
8525         case 0: /* Register controlled shift.  */
8526             tmp = load_reg(s, rn);
8527             tmp2 = load_reg(s, rm);
8528             if ((insn & 0x70) != 0)
8529                 goto illegal_op;
8530             op = (insn >> 21) & 3;
8531             logic_cc = (insn & (1 << 20)) != 0;
8532             gen_arm_shift_reg(tmp, op, tmp2, logic_cc);
8533             if (logic_cc)
8534                 gen_logic_CC(tmp);
8535             store_reg_bx(env, s, rd, tmp);
8536             break;
8537         case 1: /* Sign/zero extend.  */
8538             tmp = load_reg(s, rm);
8539             shift = (insn >> 4) & 3;
8540             /* ??? In many cases it's not necessary to do a
8541                rotate, a shift is sufficient.  */
8542             if (shift != 0)
8543                 tcg_gen_rotri_i32(tmp, tmp, shift * 8);
8544             op = (insn >> 20) & 7;
8545             switch (op) {
8546             case 0: gen_sxth(tmp);   break;
8547             case 1: gen_uxth(tmp);   break;
8548             case 2: gen_sxtb16(tmp); break;
8549             case 3: gen_uxtb16(tmp); break;
8550             case 4: gen_sxtb(tmp);   break;
8551             case 5: gen_uxtb(tmp);   break;
8552             default: goto illegal_op;
8553             }
8554             if (rn != 15) {
8555                 tmp2 = load_reg(s, rn);
8556                 if ((op >> 1) == 1) {
8557                     gen_add16(tmp, tmp2);
8558                 } else {
8559                     tcg_gen_add_i32(tmp, tmp, tmp2);
8560                     tcg_temp_free_i32(tmp2);
8561                 }
8562             }
8563             store_reg(s, rd, tmp);
8564             break;
8565         case 2: /* SIMD add/subtract.  */
8566             op = (insn >> 20) & 7;
8567             shift = (insn >> 4) & 7;
8568             if ((op & 3) == 3 || (shift & 3) == 3)
8569                 goto illegal_op;
8570             tmp = load_reg(s, rn);
8571             tmp2 = load_reg(s, rm);
8572             gen_thumb2_parallel_addsub(op, shift, tmp, tmp2);
8573             tcg_temp_free_i32(tmp2);
8574             store_reg(s, rd, tmp);
8575             break;
8576         case 3: /* Other data processing.  */
8577             op = ((insn >> 17) & 0x38) | ((insn >> 4) & 7);
8578             if (op < 4) {
8579                 /* Saturating add/subtract.  */
8580                 tmp = load_reg(s, rn);
8581                 tmp2 = load_reg(s, rm);
8582                 if (op & 1)
8583                     gen_helper_double_saturate(tmp, tmp);
8584                 if (op & 2)
8585                     gen_helper_sub_saturate(tmp, tmp2, tmp);
8586                 else
8587                     gen_helper_add_saturate(tmp, tmp, tmp2);
8588                 tcg_temp_free_i32(tmp2);
8589             } else {
8590                 tmp = load_reg(s, rn);
8591                 switch (op) {
8592                 case 0x0a: /* rbit */
8593                     gen_helper_rbit(tmp, tmp);
8594                     break;
8595                 case 0x08: /* rev */
8596                     tcg_gen_bswap32_i32(tmp, tmp);
8597                     break;
8598                 case 0x09: /* rev16 */
8599                     gen_rev16(tmp);
8600                     break;
8601                 case 0x0b: /* revsh */
8602                     gen_revsh(tmp);
8603                     break;
8604                 case 0x10: /* sel */
8605                     tmp2 = load_reg(s, rm);
8606                     tmp3 = tcg_temp_new_i32();
8607                     tcg_gen_ld_i32(tmp3, cpu_env, offsetof(CPUARMState, GE));
8608                     gen_helper_sel_flags(tmp, tmp3, tmp, tmp2);
8609                     tcg_temp_free_i32(tmp3);
8610                     tcg_temp_free_i32(tmp2);
8611                     break;
8612                 case 0x18: /* clz */
8613                     gen_helper_clz(tmp, tmp);
8614                     break;
8615                 default:
8616                     goto illegal_op;
8617                 }
8618             }
8619             store_reg(s, rd, tmp);
8620             break;
8621         case 4: case 5: /* 32-bit multiply.  Sum of absolute differences.  */
8622             op = (insn >> 4) & 0xf;
8623             tmp = load_reg(s, rn);
8624             tmp2 = load_reg(s, rm);
8625             switch ((insn >> 20) & 7) {
8626             case 0: /* 32 x 32 -> 32 */
8627                 tcg_gen_mul_i32(tmp, tmp, tmp2);
8628                 tcg_temp_free_i32(tmp2);
8629                 if (rs != 15) {
8630                     tmp2 = load_reg(s, rs);
8631                     if (op)
8632                         tcg_gen_sub_i32(tmp, tmp2, tmp);
8633                     else
8634                         tcg_gen_add_i32(tmp, tmp, tmp2);
8635                     tcg_temp_free_i32(tmp2);
8636                 }
8637                 break;
8638             case 1: /* 16 x 16 -> 32 */
8639                 gen_mulxy(tmp, tmp2, op & 2, op & 1);
8640                 tcg_temp_free_i32(tmp2);
8641                 if (rs != 15) {
8642                     tmp2 = load_reg(s, rs);
8643                     gen_helper_add_setq(tmp, tmp, tmp2);
8644                     tcg_temp_free_i32(tmp2);
8645                 }
8646                 break;
8647             case 2: /* Dual multiply add.  */
8648             case 4: /* Dual multiply subtract.  */
8649                 if (op)
8650                     gen_swap_half(tmp2);
8651                 gen_smul_dual(tmp, tmp2);
8652                 if (insn & (1 << 22)) {
8653                     /* This subtraction cannot overflow. */
8654                     tcg_gen_sub_i32(tmp, tmp, tmp2);
8655                 } else {
8656                     /* This addition cannot overflow 32 bits;
8657                      * however it may overflow considered as a signed
8658                      * operation, in which case we must set the Q flag.
8659                      */
8660                     gen_helper_add_setq(tmp, tmp, tmp2);
8661                 }
8662                 tcg_temp_free_i32(tmp2);
8663                 if (rs != 15)
8664                   {
8665                     tmp2 = load_reg(s, rs);
8666                     gen_helper_add_setq(tmp, tmp, tmp2);
8667                     tcg_temp_free_i32(tmp2);
8668                   }
8669                 break;
8670             case 3: /* 32 * 16 -> 32msb */
8671                 if (op)
8672                     tcg_gen_sari_i32(tmp2, tmp2, 16);
8673                 else
8674                     gen_sxth(tmp2);
8675                 tmp64 = gen_muls_i64_i32(tmp, tmp2);
8676                 tcg_gen_shri_i64(tmp64, tmp64, 16);
8677                 tmp = tcg_temp_new_i32();
8678                 tcg_gen_trunc_i64_i32(tmp, tmp64);
8679                 tcg_temp_free_i64(tmp64);
8680                 if (rs != 15)
8681                   {
8682                     tmp2 = load_reg(s, rs);
8683                     gen_helper_add_setq(tmp, tmp, tmp2);
8684                     tcg_temp_free_i32(tmp2);
8685                   }
8686                 break;
8687             case 5: case 6: /* 32 * 32 -> 32msb (SMMUL, SMMLA, SMMLS) */
8688                 tmp64 = gen_muls_i64_i32(tmp, tmp2);
8689                 if (rs != 15) {
8690                     tmp = load_reg(s, rs);
8691                     if (insn & (1 << 20)) {
8692                         tmp64 = gen_addq_msw(tmp64, tmp);
8693                     } else {
8694                         tmp64 = gen_subq_msw(tmp64, tmp);
8695                     }
8696                 }
8697                 if (insn & (1 << 4)) {
8698                     tcg_gen_addi_i64(tmp64, tmp64, 0x80000000u);
8699                 }
8700                 tcg_gen_shri_i64(tmp64, tmp64, 32);
8701                 tmp = tcg_temp_new_i32();
8702                 tcg_gen_trunc_i64_i32(tmp, tmp64);
8703                 tcg_temp_free_i64(tmp64);
8704                 break;
8705             case 7: /* Unsigned sum of absolute differences.  */
8706                 gen_helper_usad8(tmp, tmp, tmp2);
8707                 tcg_temp_free_i32(tmp2);
8708                 if (rs != 15) {
8709                     tmp2 = load_reg(s, rs);
8710                     tcg_gen_add_i32(tmp, tmp, tmp2);
8711                     tcg_temp_free_i32(tmp2);
8712                 }
8713                 break;
8714             }
8715             store_reg(s, rd, tmp);
8716             break;
8717         case 6: case 7: /* 64-bit multiply, Divide.  */
8718             op = ((insn >> 4) & 0xf) | ((insn >> 16) & 0x70);
8719             tmp = load_reg(s, rn);
8720             tmp2 = load_reg(s, rm);
8721             if ((op & 0x50) == 0x10) {
8722                 /* sdiv, udiv */
8723                 if (!arm_feature(env, ARM_FEATURE_THUMB_DIV)) {
8724                     goto illegal_op;
8725                 }
8726                 if (op & 0x20)
8727                     gen_helper_udiv(tmp, tmp, tmp2);
8728                 else
8729                     gen_helper_sdiv(tmp, tmp, tmp2);
8730                 tcg_temp_free_i32(tmp2);
8731                 store_reg(s, rd, tmp);
8732             } else if ((op & 0xe) == 0xc) {
8733                 /* Dual multiply accumulate long.  */
8734                 if (op & 1)
8735                     gen_swap_half(tmp2);
8736                 gen_smul_dual(tmp, tmp2);
8737                 if (op & 0x10) {
8738                     tcg_gen_sub_i32(tmp, tmp, tmp2);
8739                 } else {
8740                     tcg_gen_add_i32(tmp, tmp, tmp2);
8741                 }
8742                 tcg_temp_free_i32(tmp2);
8743                 /* BUGFIX */
8744                 tmp64 = tcg_temp_new_i64();
8745                 tcg_gen_ext_i32_i64(tmp64, tmp);
8746                 tcg_temp_free_i32(tmp);
8747                 gen_addq(s, tmp64, rs, rd);
8748                 gen_storeq_reg(s, rs, rd, tmp64);
8749                 tcg_temp_free_i64(tmp64);
8750             } else {
8751                 if (op & 0x20) {
8752                     /* Unsigned 64-bit multiply  */
8753                     tmp64 = gen_mulu_i64_i32(tmp, tmp2);
8754                 } else {
8755                     if (op & 8) {
8756                         /* smlalxy */
8757                         gen_mulxy(tmp, tmp2, op & 2, op & 1);
8758                         tcg_temp_free_i32(tmp2);
8759                         tmp64 = tcg_temp_new_i64();
8760                         tcg_gen_ext_i32_i64(tmp64, tmp);
8761                         tcg_temp_free_i32(tmp);
8762                     } else {
8763                         /* Signed 64-bit multiply  */
8764                         tmp64 = gen_muls_i64_i32(tmp, tmp2);
8765                     }
8766                 }
8767                 if (op & 4) {
8768                     /* umaal */
8769                     gen_addq_lo(s, tmp64, rs);
8770                     gen_addq_lo(s, tmp64, rd);
8771                 } else if (op & 0x40) {
8772                     /* 64-bit accumulate.  */
8773                     gen_addq(s, tmp64, rs, rd);
8774                 }
8775                 gen_storeq_reg(s, rs, rd, tmp64);
8776                 tcg_temp_free_i64(tmp64);
8777             }
8778             break;
8779         }
8780         break;
8781     case 6: case 7: case 14: case 15:
8782         /* Coprocessor.  */
8783         if (((insn >> 24) & 3) == 3) {
8784             /* Translate into the equivalent ARM encoding.  */
8785             insn = (insn & 0xe2ffffff) | ((insn & (1 << 28)) >> 4) | (1 << 28);
8786             if (disas_neon_data_insn(env, s, insn))
8787                 goto illegal_op;
8788         } else {
8789             if (insn & (1 << 28))
8790                 goto illegal_op;
8791             if (disas_coproc_insn (env, s, insn))
8792                 goto illegal_op;
8793         }
8794         break;
8795     case 8: case 9: case 10: case 11:
8796         if (insn & (1 << 15)) {
8797             /* Branches, misc control.  */
8798             if (insn & 0x5000) {
8799                 /* Unconditional branch.  */
8800                 /* signextend(hw1[10:0]) -> offset[:12].  */
8801                 offset = ((int32_t)insn << 5) >> 9 & ~(int32_t)0xfff;
8802                 /* hw1[10:0] -> offset[11:1].  */
8803                 offset |= (insn & 0x7ff) << 1;
8804                 /* (~hw2[13, 11] ^ offset[24]) -> offset[23,22]
8805                    offset[24:22] already have the same value because of the
8806                    sign extension above.  */
8807                 offset ^= ((~insn) & (1 << 13)) << 10;
8808                 offset ^= ((~insn) & (1 << 11)) << 11;
8809
8810                 if (insn & (1 << 14)) {
8811                     /* Branch and link.  */
8812                     tcg_gen_movi_i32(cpu_R[14], s->pc | 1);
8813                 }
8814
8815                 offset += s->pc;
8816                 if (insn & (1 << 12)) {
8817                     /* b/bl */
8818                     gen_jmp(s, offset);
8819                 } else {
8820                     /* blx */
8821                     offset &= ~(uint32_t)2;
8822                     /* thumb2 bx, no need to check */
8823                     gen_bx_im(s, offset);
8824                 }
8825             } else if (((insn >> 23) & 7) == 7) {
8826                 /* Misc control */
8827                 if (insn & (1 << 13))
8828                     goto illegal_op;
8829
8830                 if (insn & (1 << 26)) {
8831                     /* Secure monitor call (v6Z) */
8832                     goto illegal_op; /* not implemented.  */
8833                 } else {
8834                     op = (insn >> 20) & 7;
8835                     switch (op) {
8836                     case 0: /* msr cpsr.  */
8837                         if (IS_M(env)) {
8838                             tmp = load_reg(s, rn);
8839                             addr = tcg_const_i32(insn & 0xff);
8840                             gen_helper_v7m_msr(cpu_env, addr, tmp);
8841                             tcg_temp_free_i32(addr);
8842                             tcg_temp_free_i32(tmp);
8843                             gen_lookup_tb(s);
8844                             break;
8845                         }
8846                         /* fall through */
8847                     case 1: /* msr spsr.  */
8848                         if (IS_M(env))
8849                             goto illegal_op;
8850                         tmp = load_reg(s, rn);
8851                         if (gen_set_psr(s,
8852                               msr_mask(env, s, (insn >> 8) & 0xf, op == 1),
8853                               op == 1, tmp))
8854                             goto illegal_op;
8855                         break;
8856                     case 2: /* cps, nop-hint.  */
8857                         if (((insn >> 8) & 7) == 0) {
8858                             gen_nop_hint(s, insn & 0xff);
8859                         }
8860                         /* Implemented as NOP in user mode.  */
8861                         if (IS_USER(s))
8862                             break;
8863                         offset = 0;
8864                         imm = 0;
8865                         if (insn & (1 << 10)) {
8866                             if (insn & (1 << 7))
8867                                 offset |= CPSR_A;
8868                             if (insn & (1 << 6))
8869                                 offset |= CPSR_I;
8870                             if (insn & (1 << 5))
8871                                 offset |= CPSR_F;
8872                             if (insn & (1 << 9))
8873                                 imm = CPSR_A | CPSR_I | CPSR_F;
8874                         }
8875                         if (insn & (1 << 8)) {
8876                             offset |= 0x1f;
8877                             imm |= (insn & 0x1f);
8878                         }
8879                         if (offset) {
8880                             gen_set_psr_im(s, offset, 0, imm);
8881                         }
8882                         break;
8883                     case 3: /* Special control operations.  */
8884                         ARCH(7);
8885                         op = (insn >> 4) & 0xf;
8886                         switch (op) {
8887                         case 2: /* clrex */
8888                             gen_clrex(s);
8889                             break;
8890                         case 4: /* dsb */
8891                         case 5: /* dmb */
8892                         case 6: /* isb */
8893                             /* These execute as NOPs.  */
8894                             break;
8895                         default:
8896                             goto illegal_op;
8897                         }
8898                         break;
8899                     case 4: /* bxj */
8900                         /* Trivial implementation equivalent to bx.  */
8901                         tmp = load_reg(s, rn);
8902                         gen_bx(s, tmp);
8903                         break;
8904                     case 5: /* Exception return.  */
8905                         if (IS_USER(s)) {
8906                             goto illegal_op;
8907                         }
8908                         if (rn != 14 || rd != 15) {
8909                             goto illegal_op;
8910                         }
8911                         tmp = load_reg(s, rn);
8912                         tcg_gen_subi_i32(tmp, tmp, insn & 0xff);
8913                         gen_exception_return(s, tmp);
8914                         break;
8915                     case 6: /* mrs cpsr.  */
8916                         tmp = tcg_temp_new_i32();
8917                         if (IS_M(env)) {
8918                             addr = tcg_const_i32(insn & 0xff);
8919                             gen_helper_v7m_mrs(tmp, cpu_env, addr);
8920                             tcg_temp_free_i32(addr);
8921                         } else {
8922                             gen_helper_cpsr_read(tmp);
8923                         }
8924                         store_reg(s, rd, tmp);
8925                         break;
8926                     case 7: /* mrs spsr.  */
8927                         /* Not accessible in user mode.  */
8928                         if (IS_USER(s) || IS_M(env))
8929                             goto illegal_op;
8930                         tmp = load_cpu_field(spsr);
8931                         store_reg(s, rd, tmp);
8932                         break;
8933                     }
8934                 }
8935             } else {
8936                 /* Conditional branch.  */
8937                 op = (insn >> 22) & 0xf;
8938                 /* Generate a conditional jump to next instruction.  */
8939                 s->condlabel = gen_new_label();
8940                 gen_test_cc(op ^ 1, s->condlabel);
8941                 s->condjmp = 1;
8942
8943                 /* offset[11:1] = insn[10:0] */
8944                 offset = (insn & 0x7ff) << 1;
8945                 /* offset[17:12] = insn[21:16].  */
8946                 offset |= (insn & 0x003f0000) >> 4;
8947                 /* offset[31:20] = insn[26].  */
8948                 offset |= ((int32_t)((insn << 5) & 0x80000000)) >> 11;
8949                 /* offset[18] = insn[13].  */
8950                 offset |= (insn & (1 << 13)) << 5;
8951                 /* offset[19] = insn[11].  */
8952                 offset |= (insn & (1 << 11)) << 8;
8953
8954                 /* jump to the offset */
8955                 gen_jmp(s, s->pc + offset);
8956             }
8957         } else {
8958             /* Data processing immediate.  */
8959             if (insn & (1 << 25)) {
8960                 if (insn & (1 << 24)) {
8961                     if (insn & (1 << 20))
8962                         goto illegal_op;
8963                     /* Bitfield/Saturate.  */
8964                     op = (insn >> 21) & 7;
8965                     imm = insn & 0x1f;
8966                     shift = ((insn >> 6) & 3) | ((insn >> 10) & 0x1c);
8967                     if (rn == 15) {
8968                         tmp = tcg_temp_new_i32();
8969                         tcg_gen_movi_i32(tmp, 0);
8970                     } else {
8971                         tmp = load_reg(s, rn);
8972                     }
8973                     switch (op) {
8974                     case 2: /* Signed bitfield extract.  */
8975                         imm++;
8976                         if (shift + imm > 32)
8977                             goto illegal_op;
8978                         if (imm < 32)
8979                             gen_sbfx(tmp, shift, imm);
8980                         break;
8981                     case 6: /* Unsigned bitfield extract.  */
8982                         imm++;
8983                         if (shift + imm > 32)
8984                             goto illegal_op;
8985                         if (imm < 32)
8986                             gen_ubfx(tmp, shift, (1u << imm) - 1);
8987                         break;
8988                     case 3: /* Bitfield insert/clear.  */
8989                         if (imm < shift)
8990                             goto illegal_op;
8991                         imm = imm + 1 - shift;
8992                         if (imm != 32) {
8993                             tmp2 = load_reg(s, rd);
8994                             gen_bfi(tmp, tmp2, tmp, shift, (1u << imm) - 1);
8995                             tcg_temp_free_i32(tmp2);
8996                         }
8997                         break;
8998                     case 7:
8999                         goto illegal_op;
9000                     default: /* Saturate.  */
9001                         if (shift) {
9002                             if (op & 1)
9003                                 tcg_gen_sari_i32(tmp, tmp, shift);
9004                             else
9005                                 tcg_gen_shli_i32(tmp, tmp, shift);
9006                         }
9007                         tmp2 = tcg_const_i32(imm);
9008                         if (op & 4) {
9009                             /* Unsigned.  */
9010                             if ((op & 1) && shift == 0)
9011                                 gen_helper_usat16(tmp, tmp, tmp2);
9012                             else
9013                                 gen_helper_usat(tmp, tmp, tmp2);
9014                         } else {
9015                             /* Signed.  */
9016                             if ((op & 1) && shift == 0)
9017                                 gen_helper_ssat16(tmp, tmp, tmp2);
9018                             else
9019                                 gen_helper_ssat(tmp, tmp, tmp2);
9020                         }
9021                         tcg_temp_free_i32(tmp2);
9022                         break;
9023                     }
9024                     store_reg(s, rd, tmp);
9025                 } else {
9026                     imm = ((insn & 0x04000000) >> 15)
9027                           | ((insn & 0x7000) >> 4) | (insn & 0xff);
9028                     if (insn & (1 << 22)) {
9029                         /* 16-bit immediate.  */
9030                         imm |= (insn >> 4) & 0xf000;
9031                         if (insn & (1 << 23)) {
9032                             /* movt */
9033                             tmp = load_reg(s, rd);
9034                             tcg_gen_ext16u_i32(tmp, tmp);
9035                             tcg_gen_ori_i32(tmp, tmp, imm << 16);
9036                         } else {
9037                             /* movw */
9038                             tmp = tcg_temp_new_i32();
9039                             tcg_gen_movi_i32(tmp, imm);
9040                         }
9041                     } else {
9042                         /* Add/sub 12-bit immediate.  */
9043                         if (rn == 15) {
9044                             offset = s->pc & ~(uint32_t)3;
9045                             if (insn & (1 << 23))
9046                                 offset -= imm;
9047                             else
9048                                 offset += imm;
9049                             tmp = tcg_temp_new_i32();
9050                             tcg_gen_movi_i32(tmp, offset);
9051                         } else {
9052                             tmp = load_reg(s, rn);
9053                             if (insn & (1 << 23))
9054                                 tcg_gen_subi_i32(tmp, tmp, imm);
9055                             else
9056                                 tcg_gen_addi_i32(tmp, tmp, imm);
9057                         }
9058                     }
9059                     store_reg(s, rd, tmp);
9060                 }
9061             } else {
9062                 int shifter_out = 0;
9063                 /* modified 12-bit immediate.  */
9064                 shift = ((insn & 0x04000000) >> 23) | ((insn & 0x7000) >> 12);
9065                 imm = (insn & 0xff);
9066                 switch (shift) {
9067                 case 0: /* XY */
9068                     /* Nothing to do.  */
9069                     break;
9070                 case 1: /* 00XY00XY */
9071                     imm |= imm << 16;
9072                     break;
9073                 case 2: /* XY00XY00 */
9074                     imm |= imm << 16;
9075                     imm <<= 8;
9076                     break;
9077                 case 3: /* XYXYXYXY */
9078                     imm |= imm << 16;
9079                     imm |= imm << 8;
9080                     break;
9081                 default: /* Rotated constant.  */
9082                     shift = (shift << 1) | (imm >> 7);
9083                     imm |= 0x80;
9084                     imm = imm << (32 - shift);
9085                     shifter_out = 1;
9086                     break;
9087                 }
9088                 tmp2 = tcg_temp_new_i32();
9089                 tcg_gen_movi_i32(tmp2, imm);
9090                 rn = (insn >> 16) & 0xf;
9091                 if (rn == 15) {
9092                     tmp = tcg_temp_new_i32();
9093                     tcg_gen_movi_i32(tmp, 0);
9094                 } else {
9095                     tmp = load_reg(s, rn);
9096                 }
9097                 op = (insn >> 21) & 0xf;
9098                 if (gen_thumb2_data_op(s, op, (insn & (1 << 20)) != 0,
9099                                        shifter_out, tmp, tmp2))
9100                     goto illegal_op;
9101                 tcg_temp_free_i32(tmp2);
9102                 rd = (insn >> 8) & 0xf;
9103                 if (rd != 15) {
9104                     store_reg(s, rd, tmp);
9105                 } else {
9106                     tcg_temp_free_i32(tmp);
9107                 }
9108             }
9109         }
9110         break;
9111     case 12: /* Load/store single data item.  */
9112         {
9113         int postinc = 0;
9114         int writeback = 0;
9115         int user;
9116         if ((insn & 0x01100000) == 0x01000000) {
9117             if (disas_neon_ls_insn(env, s, insn))
9118                 goto illegal_op;
9119             break;
9120         }
9121         op = ((insn >> 21) & 3) | ((insn >> 22) & 4);
9122         if (rs == 15) {
9123             if (!(insn & (1 << 20))) {
9124                 goto illegal_op;
9125             }
9126             if (op != 2) {
9127                 /* Byte or halfword load space with dest == r15 : memory hints.
9128                  * Catch them early so we don't emit pointless addressing code.
9129                  * This space is a mix of:
9130                  *  PLD/PLDW/PLI,  which we implement as NOPs (note that unlike
9131                  *     the ARM encodings, PLDW space doesn't UNDEF for non-v7MP
9132                  *     cores)
9133                  *  unallocated hints, which must be treated as NOPs
9134                  *  UNPREDICTABLE space, which we NOP or UNDEF depending on
9135                  *     which is easiest for the decoding logic
9136                  *  Some space which must UNDEF
9137                  */
9138                 int op1 = (insn >> 23) & 3;
9139                 int op2 = (insn >> 6) & 0x3f;
9140                 if (op & 2) {
9141                     goto illegal_op;
9142                 }
9143                 if (rn == 15) {
9144                     /* UNPREDICTABLE, unallocated hint or
9145                      * PLD/PLDW/PLI (literal)
9146                      */
9147                     return 0;
9148                 }
9149                 if (op1 & 1) {
9150                     return 0; /* PLD/PLDW/PLI or unallocated hint */
9151                 }
9152                 if ((op2 == 0) || ((op2 & 0x3c) == 0x30)) {
9153                     return 0; /* PLD/PLDW/PLI or unallocated hint */
9154                 }
9155                 /* UNDEF space, or an UNPREDICTABLE */
9156                 return 1;
9157             }
9158         }
9159         user = IS_USER(s);
9160         if (rn == 15) {
9161             addr = tcg_temp_new_i32();
9162             /* PC relative.  */
9163             /* s->pc has already been incremented by 4.  */
9164             imm = s->pc & 0xfffffffc;
9165             if (insn & (1 << 23))
9166                 imm += insn & 0xfff;
9167             else
9168                 imm -= insn & 0xfff;
9169             tcg_gen_movi_i32(addr, imm);
9170         } else {
9171             addr = load_reg(s, rn);
9172             if (insn & (1 << 23)) {
9173                 /* Positive offset.  */
9174                 imm = insn & 0xfff;
9175                 tcg_gen_addi_i32(addr, addr, imm);
9176             } else {
9177                 imm = insn & 0xff;
9178                 switch ((insn >> 8) & 0xf) {
9179                 case 0x0: /* Shifted Register.  */
9180                     shift = (insn >> 4) & 0xf;
9181                     if (shift > 3) {
9182                         tcg_temp_free_i32(addr);
9183                         goto illegal_op;
9184                     }
9185                     tmp = load_reg(s, rm);
9186                     if (shift)
9187                         tcg_gen_shli_i32(tmp, tmp, shift);
9188                     tcg_gen_add_i32(addr, addr, tmp);
9189                     tcg_temp_free_i32(tmp);
9190                     break;
9191                 case 0xc: /* Negative offset.  */
9192                     tcg_gen_addi_i32(addr, addr, -imm);
9193                     break;
9194                 case 0xe: /* User privilege.  */
9195                     tcg_gen_addi_i32(addr, addr, imm);
9196                     user = 1;
9197                     break;
9198                 case 0x9: /* Post-decrement.  */
9199                     imm = -imm;
9200                     /* Fall through.  */
9201                 case 0xb: /* Post-increment.  */
9202                     postinc = 1;
9203                     writeback = 1;
9204                     break;
9205                 case 0xd: /* Pre-decrement.  */
9206                     imm = -imm;
9207                     /* Fall through.  */
9208                 case 0xf: /* Pre-increment.  */
9209                     tcg_gen_addi_i32(addr, addr, imm);
9210                     writeback = 1;
9211                     break;
9212                 default:
9213                     tcg_temp_free_i32(addr);
9214                     goto illegal_op;
9215                 }
9216             }
9217         }
9218         if (insn & (1 << 20)) {
9219             /* Load.  */
9220             switch (op) {
9221             case 0: tmp = gen_ld8u(addr, user); break;
9222             case 4: tmp = gen_ld8s(addr, user); break;
9223             case 1: tmp = gen_ld16u(addr, user); break;
9224             case 5: tmp = gen_ld16s(addr, user); break;
9225             case 2: tmp = gen_ld32(addr, user); break;
9226             default:
9227                 tcg_temp_free_i32(addr);
9228                 goto illegal_op;
9229             }
9230             if (rs == 15) {
9231                 gen_bx(s, tmp);
9232             } else {
9233                 store_reg(s, rs, tmp);
9234             }
9235         } else {
9236             /* Store.  */
9237             tmp = load_reg(s, rs);
9238             switch (op) {
9239             case 0: gen_st8(tmp, addr, user); break;
9240             case 1: gen_st16(tmp, addr, user); break;
9241             case 2: gen_st32(tmp, addr, user); break;
9242             default:
9243                 tcg_temp_free_i32(addr);
9244                 goto illegal_op;
9245             }
9246         }
9247         if (postinc)
9248             tcg_gen_addi_i32(addr, addr, imm);
9249         if (writeback) {
9250             store_reg(s, rn, addr);
9251         } else {
9252             tcg_temp_free_i32(addr);
9253         }
9254         }
9255         break;
9256     default:
9257         goto illegal_op;
9258     }
9259     return 0;
9260 illegal_op:
9261     return 1;
9262 }
9263
9264 static void disas_thumb_insn(CPUARMState *env, DisasContext *s)
9265 {
9266     uint32_t val, insn, op, rm, rn, rd, shift, cond;
9267     int32_t offset;
9268     int i;
9269     TCGv tmp;
9270     TCGv tmp2;
9271     TCGv addr;
9272
9273     if (s->condexec_mask) {
9274         cond = s->condexec_cond;
9275         if (cond != 0x0e) {     /* Skip conditional when condition is AL. */
9276           s->condlabel = gen_new_label();
9277           gen_test_cc(cond ^ 1, s->condlabel);
9278           s->condjmp = 1;
9279         }
9280     }
9281
9282     insn = arm_lduw_code(s->pc, s->bswap_code);
9283     s->pc += 2;
9284
9285     switch (insn >> 12) {
9286     case 0: case 1:
9287
9288         rd = insn & 7;
9289         op = (insn >> 11) & 3;
9290         if (op == 3) {
9291             /* add/subtract */
9292             rn = (insn >> 3) & 7;
9293             tmp = load_reg(s, rn);
9294             if (insn & (1 << 10)) {
9295                 /* immediate */
9296                 tmp2 = tcg_temp_new_i32();
9297                 tcg_gen_movi_i32(tmp2, (insn >> 6) & 7);
9298             } else {
9299                 /* reg */
9300                 rm = (insn >> 6) & 7;
9301                 tmp2 = load_reg(s, rm);
9302             }
9303             if (insn & (1 << 9)) {
9304                 if (s->condexec_mask)
9305                     tcg_gen_sub_i32(tmp, tmp, tmp2);
9306                 else
9307                     gen_helper_sub_cc(tmp, tmp, tmp2);
9308             } else {
9309                 if (s->condexec_mask)
9310                     tcg_gen_add_i32(tmp, tmp, tmp2);
9311                 else
9312                     gen_helper_add_cc(tmp, tmp, tmp2);
9313             }
9314             tcg_temp_free_i32(tmp2);
9315             store_reg(s, rd, tmp);
9316         } else {
9317             /* shift immediate */
9318             rm = (insn >> 3) & 7;
9319             shift = (insn >> 6) & 0x1f;
9320             tmp = load_reg(s, rm);
9321             gen_arm_shift_im(tmp, op, shift, s->condexec_mask == 0);
9322             if (!s->condexec_mask)
9323                 gen_logic_CC(tmp);
9324             store_reg(s, rd, tmp);
9325         }
9326         break;
9327     case 2: case 3:
9328         /* arithmetic large immediate */
9329         op = (insn >> 11) & 3;
9330         rd = (insn >> 8) & 0x7;
9331         if (op == 0) { /* mov */
9332             tmp = tcg_temp_new_i32();
9333             tcg_gen_movi_i32(tmp, insn & 0xff);
9334             if (!s->condexec_mask)
9335                 gen_logic_CC(tmp);
9336             store_reg(s, rd, tmp);
9337         } else {
9338             tmp = load_reg(s, rd);
9339             tmp2 = tcg_temp_new_i32();
9340             tcg_gen_movi_i32(tmp2, insn & 0xff);
9341             switch (op) {
9342             case 1: /* cmp */
9343                 gen_helper_sub_cc(tmp, tmp, tmp2);
9344                 tcg_temp_free_i32(tmp);
9345                 tcg_temp_free_i32(tmp2);
9346                 break;
9347             case 2: /* add */
9348                 if (s->condexec_mask)
9349                     tcg_gen_add_i32(tmp, tmp, tmp2);
9350                 else
9351                     gen_helper_add_cc(tmp, tmp, tmp2);
9352                 tcg_temp_free_i32(tmp2);
9353                 store_reg(s, rd, tmp);
9354                 break;
9355             case 3: /* sub */
9356                 if (s->condexec_mask)
9357                     tcg_gen_sub_i32(tmp, tmp, tmp2);
9358                 else
9359                     gen_helper_sub_cc(tmp, tmp, tmp2);
9360                 tcg_temp_free_i32(tmp2);
9361                 store_reg(s, rd, tmp);
9362                 break;
9363             }
9364         }
9365         break;
9366     case 4:
9367         if (insn & (1 << 11)) {
9368             rd = (insn >> 8) & 7;
9369             /* load pc-relative.  Bit 1 of PC is ignored.  */
9370             val = s->pc + 2 + ((insn & 0xff) * 4);
9371             val &= ~(uint32_t)2;
9372             addr = tcg_temp_new_i32();
9373             tcg_gen_movi_i32(addr, val);
9374             tmp = gen_ld32(addr, IS_USER(s));
9375             tcg_temp_free_i32(addr);
9376             store_reg(s, rd, tmp);
9377             break;
9378         }
9379         if (insn & (1 << 10)) {
9380             /* data processing extended or blx */
9381             rd = (insn & 7) | ((insn >> 4) & 8);
9382             rm = (insn >> 3) & 0xf;
9383             op = (insn >> 8) & 3;
9384             switch (op) {
9385             case 0: /* add */
9386                 tmp = load_reg(s, rd);
9387                 tmp2 = load_reg(s, rm);
9388                 tcg_gen_add_i32(tmp, tmp, tmp2);
9389                 tcg_temp_free_i32(tmp2);
9390                 store_reg(s, rd, tmp);
9391                 break;
9392             case 1: /* cmp */
9393                 tmp = load_reg(s, rd);
9394                 tmp2 = load_reg(s, rm);
9395                 gen_helper_sub_cc(tmp, tmp, tmp2);
9396                 tcg_temp_free_i32(tmp2);
9397                 tcg_temp_free_i32(tmp);
9398                 break;
9399             case 2: /* mov/cpy */
9400                 tmp = load_reg(s, rm);
9401                 store_reg(s, rd, tmp);
9402                 break;
9403             case 3:/* branch [and link] exchange thumb register */
9404                 tmp = load_reg(s, rm);
9405                 if (insn & (1 << 7)) {
9406                     ARCH(5);
9407                     val = (uint32_t)s->pc | 1;
9408                     tmp2 = tcg_temp_new_i32();
9409                     tcg_gen_movi_i32(tmp2, val);
9410                     store_reg(s, 14, tmp2);
9411                 }
9412                 /* already thumb, no need to check */
9413                 gen_bx(s, tmp);
9414                 break;
9415             }
9416             break;
9417         }
9418
9419         /* data processing register */
9420         rd = insn & 7;
9421         rm = (insn >> 3) & 7;
9422         op = (insn >> 6) & 0xf;
9423         if (op == 2 || op == 3 || op == 4 || op == 7) {
9424             /* the shift/rotate ops want the operands backwards */
9425             val = rm;
9426             rm = rd;
9427             rd = val;
9428             val = 1;
9429         } else {
9430             val = 0;
9431         }
9432
9433         if (op == 9) { /* neg */
9434             tmp = tcg_temp_new_i32();
9435             tcg_gen_movi_i32(tmp, 0);
9436         } else if (op != 0xf) { /* mvn doesn't read its first operand */
9437             tmp = load_reg(s, rd);
9438         } else {
9439             TCGV_UNUSED(tmp);
9440         }
9441
9442         tmp2 = load_reg(s, rm);
9443         switch (op) {
9444         case 0x0: /* and */
9445             tcg_gen_and_i32(tmp, tmp, tmp2);
9446             if (!s->condexec_mask)
9447                 gen_logic_CC(tmp);
9448             break;
9449         case 0x1: /* eor */
9450             tcg_gen_xor_i32(tmp, tmp, tmp2);
9451             if (!s->condexec_mask)
9452                 gen_logic_CC(tmp);
9453             break;
9454         case 0x2: /* lsl */
9455             if (s->condexec_mask) {
9456                 gen_helper_shl(tmp2, tmp2, tmp);
9457             } else {
9458                 gen_helper_shl_cc(tmp2, tmp2, tmp);
9459                 gen_logic_CC(tmp2);
9460             }
9461             break;
9462         case 0x3: /* lsr */
9463             if (s->condexec_mask) {
9464                 gen_helper_shr(tmp2, tmp2, tmp);
9465             } else {
9466                 gen_helper_shr_cc(tmp2, tmp2, tmp);
9467                 gen_logic_CC(tmp2);
9468             }
9469             break;
9470         case 0x4: /* asr */
9471             if (s->condexec_mask) {
9472                 gen_helper_sar(tmp2, tmp2, tmp);
9473             } else {
9474                 gen_helper_sar_cc(tmp2, tmp2, tmp);
9475                 gen_logic_CC(tmp2);
9476             }
9477             break;
9478         case 0x5: /* adc */
9479             if (s->condexec_mask)
9480                 gen_adc(tmp, tmp2);
9481             else
9482                 gen_helper_adc_cc(tmp, tmp, tmp2);
9483             break;
9484         case 0x6: /* sbc */
9485             if (s->condexec_mask)
9486                 gen_sub_carry(tmp, tmp, tmp2);
9487             else
9488                 gen_helper_sbc_cc(tmp, tmp, tmp2);
9489             break;
9490         case 0x7: /* ror */
9491             if (s->condexec_mask) {
9492                 tcg_gen_andi_i32(tmp, tmp, 0x1f);
9493                 tcg_gen_rotr_i32(tmp2, tmp2, tmp);
9494             } else {
9495                 gen_helper_ror_cc(tmp2, tmp2, tmp);
9496                 gen_logic_CC(tmp2);
9497             }
9498             break;
9499         case 0x8: /* tst */
9500             tcg_gen_and_i32(tmp, tmp, tmp2);
9501             gen_logic_CC(tmp);
9502             rd = 16;
9503             break;
9504         case 0x9: /* neg */
9505             if (s->condexec_mask)
9506                 tcg_gen_neg_i32(tmp, tmp2);
9507             else
9508                 gen_helper_sub_cc(tmp, tmp, tmp2);
9509             break;
9510         case 0xa: /* cmp */
9511             gen_helper_sub_cc(tmp, tmp, tmp2);
9512             rd = 16;
9513             break;
9514         case 0xb: /* cmn */
9515             gen_helper_add_cc(tmp, tmp, tmp2);
9516             rd = 16;
9517             break;
9518         case 0xc: /* orr */
9519             tcg_gen_or_i32(tmp, tmp, tmp2);
9520             if (!s->condexec_mask)
9521                 gen_logic_CC(tmp);
9522             break;
9523         case 0xd: /* mul */
9524             tcg_gen_mul_i32(tmp, tmp, tmp2);
9525             if (!s->condexec_mask)
9526                 gen_logic_CC(tmp);
9527             break;
9528         case 0xe: /* bic */
9529             tcg_gen_andc_i32(tmp, tmp, tmp2);
9530             if (!s->condexec_mask)
9531                 gen_logic_CC(tmp);
9532             break;
9533         case 0xf: /* mvn */
9534             tcg_gen_not_i32(tmp2, tmp2);
9535             if (!s->condexec_mask)
9536                 gen_logic_CC(tmp2);
9537             val = 1;
9538             rm = rd;
9539             break;
9540         }
9541         if (rd != 16) {
9542             if (val) {
9543                 store_reg(s, rm, tmp2);
9544                 if (op != 0xf)
9545                     tcg_temp_free_i32(tmp);
9546             } else {
9547                 store_reg(s, rd, tmp);
9548                 tcg_temp_free_i32(tmp2);
9549             }
9550         } else {
9551             tcg_temp_free_i32(tmp);
9552             tcg_temp_free_i32(tmp2);
9553         }
9554         break;
9555
9556     case 5:
9557         /* load/store register offset.  */
9558         rd = insn & 7;
9559         rn = (insn >> 3) & 7;
9560         rm = (insn >> 6) & 7;
9561         op = (insn >> 9) & 7;
9562         addr = load_reg(s, rn);
9563         tmp = load_reg(s, rm);
9564         tcg_gen_add_i32(addr, addr, tmp);
9565         tcg_temp_free_i32(tmp);
9566
9567         if (op < 3) /* store */
9568             tmp = load_reg(s, rd);
9569
9570         switch (op) {
9571         case 0: /* str */
9572             gen_st32(tmp, addr, IS_USER(s));
9573             break;
9574         case 1: /* strh */
9575             gen_st16(tmp, addr, IS_USER(s));
9576             break;
9577         case 2: /* strb */
9578             gen_st8(tmp, addr, IS_USER(s));
9579             break;
9580         case 3: /* ldrsb */
9581             tmp = gen_ld8s(addr, IS_USER(s));
9582             break;
9583         case 4: /* ldr */
9584             tmp = gen_ld32(addr, IS_USER(s));
9585             break;
9586         case 5: /* ldrh */
9587             tmp = gen_ld16u(addr, IS_USER(s));
9588             break;
9589         case 6: /* ldrb */
9590             tmp = gen_ld8u(addr, IS_USER(s));
9591             break;
9592         case 7: /* ldrsh */
9593             tmp = gen_ld16s(addr, IS_USER(s));
9594             break;
9595         }
9596         if (op >= 3) /* load */
9597             store_reg(s, rd, tmp);
9598         tcg_temp_free_i32(addr);
9599         break;
9600
9601     case 6:
9602         /* load/store word immediate offset */
9603         rd = insn & 7;
9604         rn = (insn >> 3) & 7;
9605         addr = load_reg(s, rn);
9606         val = (insn >> 4) & 0x7c;
9607         tcg_gen_addi_i32(addr, addr, val);
9608
9609         if (insn & (1 << 11)) {
9610             /* load */
9611             tmp = gen_ld32(addr, IS_USER(s));
9612             store_reg(s, rd, tmp);
9613         } else {
9614             /* store */
9615             tmp = load_reg(s, rd);
9616             gen_st32(tmp, addr, IS_USER(s));
9617         }
9618         tcg_temp_free_i32(addr);
9619         break;
9620
9621     case 7:
9622         /* load/store byte immediate offset */
9623         rd = insn & 7;
9624         rn = (insn >> 3) & 7;
9625         addr = load_reg(s, rn);
9626         val = (insn >> 6) & 0x1f;
9627         tcg_gen_addi_i32(addr, addr, val);
9628
9629         if (insn & (1 << 11)) {
9630             /* load */
9631             tmp = gen_ld8u(addr, IS_USER(s));
9632             store_reg(s, rd, tmp);
9633         } else {
9634             /* store */
9635             tmp = load_reg(s, rd);
9636             gen_st8(tmp, addr, IS_USER(s));
9637         }
9638         tcg_temp_free_i32(addr);
9639         break;
9640
9641     case 8:
9642         /* load/store halfword immediate offset */
9643         rd = insn & 7;
9644         rn = (insn >> 3) & 7;
9645         addr = load_reg(s, rn);
9646         val = (insn >> 5) & 0x3e;
9647         tcg_gen_addi_i32(addr, addr, val);
9648
9649         if (insn & (1 << 11)) {
9650             /* load */
9651             tmp = gen_ld16u(addr, IS_USER(s));
9652             store_reg(s, rd, tmp);
9653         } else {
9654             /* store */
9655             tmp = load_reg(s, rd);
9656             gen_st16(tmp, addr, IS_USER(s));
9657         }
9658         tcg_temp_free_i32(addr);
9659         break;
9660
9661     case 9:
9662         /* load/store from stack */
9663         rd = (insn >> 8) & 7;
9664         addr = load_reg(s, 13);
9665         val = (insn & 0xff) * 4;
9666         tcg_gen_addi_i32(addr, addr, val);
9667
9668         if (insn & (1 << 11)) {
9669             /* load */
9670             tmp = gen_ld32(addr, IS_USER(s));
9671             store_reg(s, rd, tmp);
9672         } else {
9673             /* store */
9674             tmp = load_reg(s, rd);
9675             gen_st32(tmp, addr, IS_USER(s));
9676         }
9677         tcg_temp_free_i32(addr);
9678         break;
9679
9680     case 10:
9681         /* add to high reg */
9682         rd = (insn >> 8) & 7;
9683         if (insn & (1 << 11)) {
9684             /* SP */
9685             tmp = load_reg(s, 13);
9686         } else {
9687             /* PC. bit 1 is ignored.  */
9688             tmp = tcg_temp_new_i32();
9689             tcg_gen_movi_i32(tmp, (s->pc + 2) & ~(uint32_t)2);
9690         }
9691         val = (insn & 0xff) * 4;
9692         tcg_gen_addi_i32(tmp, tmp, val);
9693         store_reg(s, rd, tmp);
9694         break;
9695
9696     case 11:
9697         /* misc */
9698         op = (insn >> 8) & 0xf;
9699         switch (op) {
9700         case 0:
9701             /* adjust stack pointer */
9702             tmp = load_reg(s, 13);
9703             val = (insn & 0x7f) * 4;
9704             if (insn & (1 << 7))
9705                 val = -(int32_t)val;
9706             tcg_gen_addi_i32(tmp, tmp, val);
9707             store_reg(s, 13, tmp);
9708             break;
9709
9710         case 2: /* sign/zero extend.  */
9711             ARCH(6);
9712             rd = insn & 7;
9713             rm = (insn >> 3) & 7;
9714             tmp = load_reg(s, rm);
9715             switch ((insn >> 6) & 3) {
9716             case 0: gen_sxth(tmp); break;
9717             case 1: gen_sxtb(tmp); break;
9718             case 2: gen_uxth(tmp); break;
9719             case 3: gen_uxtb(tmp); break;
9720             }
9721             store_reg(s, rd, tmp);
9722             break;
9723         case 4: case 5: case 0xc: case 0xd:
9724             /* push/pop */
9725             addr = load_reg(s, 13);
9726             if (insn & (1 << 8))
9727                 offset = 4;
9728             else
9729                 offset = 0;
9730             for (i = 0; i < 8; i++) {
9731                 if (insn & (1 << i))
9732                     offset += 4;
9733             }
9734             if ((insn & (1 << 11)) == 0) {
9735                 tcg_gen_addi_i32(addr, addr, -offset);
9736             }
9737             for (i = 0; i < 8; i++) {
9738                 if (insn & (1 << i)) {
9739                     if (insn & (1 << 11)) {
9740                         /* pop */
9741                         tmp = gen_ld32(addr, IS_USER(s));
9742                         store_reg(s, i, tmp);
9743                     } else {
9744                         /* push */
9745                         tmp = load_reg(s, i);
9746                         gen_st32(tmp, addr, IS_USER(s));
9747                     }
9748                     /* advance to the next address.  */
9749                     tcg_gen_addi_i32(addr, addr, 4);
9750                 }
9751             }
9752             TCGV_UNUSED(tmp);
9753             if (insn & (1 << 8)) {
9754                 if (insn & (1 << 11)) {
9755                     /* pop pc */
9756                     tmp = gen_ld32(addr, IS_USER(s));
9757                     /* don't set the pc until the rest of the instruction
9758                        has completed */
9759                 } else {
9760                     /* push lr */
9761                     tmp = load_reg(s, 14);
9762                     gen_st32(tmp, addr, IS_USER(s));
9763                 }
9764                 tcg_gen_addi_i32(addr, addr, 4);
9765             }
9766             if ((insn & (1 << 11)) == 0) {
9767                 tcg_gen_addi_i32(addr, addr, -offset);
9768             }
9769             /* write back the new stack pointer */
9770             store_reg(s, 13, addr);
9771             /* set the new PC value */
9772             if ((insn & 0x0900) == 0x0900) {
9773                 store_reg_from_load(env, s, 15, tmp);
9774             }
9775             break;
9776
9777         case 1: case 3: case 9: case 11: /* czb */
9778             rm = insn & 7;
9779             tmp = load_reg(s, rm);
9780             s->condlabel = gen_new_label();
9781             s->condjmp = 1;
9782             if (insn & (1 << 11))
9783                 tcg_gen_brcondi_i32(TCG_COND_EQ, tmp, 0, s->condlabel);
9784             else
9785                 tcg_gen_brcondi_i32(TCG_COND_NE, tmp, 0, s->condlabel);
9786             tcg_temp_free_i32(tmp);
9787             offset = ((insn & 0xf8) >> 2) | (insn & 0x200) >> 3;
9788             val = (uint32_t)s->pc + 2;
9789             val += offset;
9790             gen_jmp(s, val);
9791             break;
9792
9793         case 15: /* IT, nop-hint.  */
9794             if ((insn & 0xf) == 0) {
9795                 gen_nop_hint(s, (insn >> 4) & 0xf);
9796                 break;
9797             }
9798             /* If Then.  */
9799             s->condexec_cond = (insn >> 4) & 0xe;
9800             s->condexec_mask = insn & 0x1f;
9801             /* No actual code generated for this insn, just setup state.  */
9802             break;
9803
9804         case 0xe: /* bkpt */
9805             ARCH(5);
9806             gen_exception_insn(s, 2, EXCP_BKPT);
9807             break;
9808
9809         case 0xa: /* rev */
9810             ARCH(6);
9811             rn = (insn >> 3) & 0x7;
9812             rd = insn & 0x7;
9813             tmp = load_reg(s, rn);
9814             switch ((insn >> 6) & 3) {
9815             case 0: tcg_gen_bswap32_i32(tmp, tmp); break;
9816             case 1: gen_rev16(tmp); break;
9817             case 3: gen_revsh(tmp); break;
9818             default: goto illegal_op;
9819             }
9820             store_reg(s, rd, tmp);
9821             break;
9822
9823         case 6:
9824             switch ((insn >> 5) & 7) {
9825             case 2:
9826                 /* setend */
9827                 ARCH(6);
9828                 if (((insn >> 3) & 1) != s->bswap_code) {
9829                     /* Dynamic endianness switching not implemented. */
9830                     goto illegal_op;
9831                 }
9832                 break;
9833             case 3:
9834                 /* cps */
9835                 ARCH(6);
9836                 if (IS_USER(s)) {
9837                     break;
9838                 }
9839                 if (IS_M(env)) {
9840                     tmp = tcg_const_i32((insn & (1 << 4)) != 0);
9841                     /* FAULTMASK */
9842                     if (insn & 1) {
9843                         addr = tcg_const_i32(19);
9844                         gen_helper_v7m_msr(cpu_env, addr, tmp);
9845                         tcg_temp_free_i32(addr);
9846                     }
9847                     /* PRIMASK */
9848                     if (insn & 2) {
9849                         addr = tcg_const_i32(16);
9850                         gen_helper_v7m_msr(cpu_env, addr, tmp);
9851                         tcg_temp_free_i32(addr);
9852                     }
9853                     tcg_temp_free_i32(tmp);
9854                     gen_lookup_tb(s);
9855                 } else {
9856                     if (insn & (1 << 4)) {
9857                         shift = CPSR_A | CPSR_I | CPSR_F;
9858                     } else {
9859                         shift = 0;
9860                     }
9861                     gen_set_psr_im(s, ((insn & 7) << 6), 0, shift);
9862                 }
9863                 break;
9864             default:
9865                 goto undef;
9866             }
9867             break;
9868
9869         default:
9870             goto undef;
9871         }
9872         break;
9873
9874     case 12:
9875     {
9876         /* load/store multiple */
9877         TCGv loaded_var;
9878         TCGV_UNUSED(loaded_var);
9879         rn = (insn >> 8) & 0x7;
9880         addr = load_reg(s, rn);
9881         for (i = 0; i < 8; i++) {
9882             if (insn & (1 << i)) {
9883                 if (insn & (1 << 11)) {
9884                     /* load */
9885                     tmp = gen_ld32(addr, IS_USER(s));
9886                     if (i == rn) {
9887                         loaded_var = tmp;
9888                     } else {
9889                         store_reg(s, i, tmp);
9890                     }
9891                 } else {
9892                     /* store */
9893                     tmp = load_reg(s, i);
9894                     gen_st32(tmp, addr, IS_USER(s));
9895                 }
9896                 /* advance to the next address */
9897                 tcg_gen_addi_i32(addr, addr, 4);
9898             }
9899         }
9900         if ((insn & (1 << rn)) == 0) {
9901             /* base reg not in list: base register writeback */
9902             store_reg(s, rn, addr);
9903         } else {
9904             /* base reg in list: if load, complete it now */
9905             if (insn & (1 << 11)) {
9906                 store_reg(s, rn, loaded_var);
9907             }
9908             tcg_temp_free_i32(addr);
9909         }
9910         break;
9911     }
9912     case 13:
9913         /* conditional branch or swi */
9914         cond = (insn >> 8) & 0xf;
9915         if (cond == 0xe)
9916             goto undef;
9917
9918         if (cond == 0xf) {
9919             /* swi */
9920             gen_set_pc_im(s->pc);
9921             s->is_jmp = DISAS_SWI;
9922             break;
9923         }
9924         /* generate a conditional jump to next instruction */
9925         s->condlabel = gen_new_label();
9926         gen_test_cc(cond ^ 1, s->condlabel);
9927         s->condjmp = 1;
9928
9929         /* jump to the offset */
9930         val = (uint32_t)s->pc + 2;
9931         offset = ((int32_t)insn << 24) >> 24;
9932         val += offset << 1;
9933         gen_jmp(s, val);
9934         break;
9935
9936     case 14:
9937         if (insn & (1 << 11)) {
9938             if (disas_thumb2_insn(env, s, insn))
9939               goto undef32;
9940             break;
9941         }
9942         /* unconditional branch */
9943         val = (uint32_t)s->pc;
9944         offset = ((int32_t)insn << 21) >> 21;
9945         val += (offset << 1) + 2;
9946         gen_jmp(s, val);
9947         break;
9948
9949     case 15:
9950         if (disas_thumb2_insn(env, s, insn))
9951             goto undef32;
9952         break;
9953     }
9954     return;
9955 undef32:
9956     gen_exception_insn(s, 4, EXCP_UDEF);
9957     return;
9958 illegal_op:
9959 undef:
9960     gen_exception_insn(s, 2, EXCP_UDEF);
9961 }
9962
9963 /* generate intermediate code in gen_opc_buf and gen_opparam_buf for
9964    basic block 'tb'. If search_pc is TRUE, also generate PC
9965    information for each intermediate instruction. */
9966 static inline void gen_intermediate_code_internal(CPUARMState *env,
9967                                                   TranslationBlock *tb,
9968                                                   int search_pc)
9969 {
9970     DisasContext dc1, *dc = &dc1;
9971     CPUBreakpoint *bp;
9972     uint16_t *gen_opc_end;
9973     int j, lj;
9974     target_ulong pc_start;
9975     uint32_t next_page_start;
9976     int num_insns;
9977     int max_insns;
9978
9979     /* generate intermediate code */
9980     pc_start = tb->pc;
9981
9982     dc->tb = tb;
9983
9984     gen_opc_end = gen_opc_buf + OPC_MAX_SIZE;
9985
9986     dc->is_jmp = DISAS_NEXT;
9987     dc->pc = pc_start;
9988     dc->singlestep_enabled = env->singlestep_enabled;
9989     dc->condjmp = 0;
9990     dc->thumb = ARM_TBFLAG_THUMB(tb->flags);
9991     dc->bswap_code = ARM_TBFLAG_BSWAP_CODE(tb->flags);
9992     dc->condexec_mask = (ARM_TBFLAG_CONDEXEC(tb->flags) & 0xf) << 1;
9993     dc->condexec_cond = ARM_TBFLAG_CONDEXEC(tb->flags) >> 4;
9994 #if !defined(CONFIG_USER_ONLY)
9995     dc->user = (ARM_TBFLAG_PRIV(tb->flags) == 0);
9996 #endif
9997     dc->vfp_enabled = ARM_TBFLAG_VFPEN(tb->flags);
9998     dc->vec_len = ARM_TBFLAG_VECLEN(tb->flags);
9999     dc->vec_stride = ARM_TBFLAG_VECSTRIDE(tb->flags);
10000     cpu_F0s = tcg_temp_new_i32();
10001     cpu_F1s = tcg_temp_new_i32();
10002     cpu_F0d = tcg_temp_new_i64();
10003     cpu_F1d = tcg_temp_new_i64();
10004     cpu_V0 = cpu_F0d;
10005     cpu_V1 = cpu_F1d;
10006     /* FIXME: cpu_M0 can probably be the same as cpu_V0.  */
10007     cpu_M0 = tcg_temp_new_i64();
10008     next_page_start = (pc_start & TARGET_PAGE_MASK) + TARGET_PAGE_SIZE;
10009     lj = -1;
10010     num_insns = 0;
10011     max_insns = tb->cflags & CF_COUNT_MASK;
10012     if (max_insns == 0)
10013         max_insns = CF_COUNT_MASK;
10014
10015     gen_icount_start();
10016
10017     tcg_clear_temp_count();
10018
10019     /* A note on handling of the condexec (IT) bits:
10020      *
10021      * We want to avoid the overhead of having to write the updated condexec
10022      * bits back to the CPUARMState for every instruction in an IT block. So:
10023      * (1) if the condexec bits are not already zero then we write
10024      * zero back into the CPUARMState now. This avoids complications trying
10025      * to do it at the end of the block. (For example if we don't do this
10026      * it's hard to identify whether we can safely skip writing condexec
10027      * at the end of the TB, which we definitely want to do for the case
10028      * where a TB doesn't do anything with the IT state at all.)
10029      * (2) if we are going to leave the TB then we call gen_set_condexec()
10030      * which will write the correct value into CPUARMState if zero is wrong.
10031      * This is done both for leaving the TB at the end, and for leaving
10032      * it because of an exception we know will happen, which is done in
10033      * gen_exception_insn(). The latter is necessary because we need to
10034      * leave the TB with the PC/IT state just prior to execution of the
10035      * instruction which caused the exception.
10036      * (3) if we leave the TB unexpectedly (eg a data abort on a load)
10037      * then the CPUARMState will be wrong and we need to reset it.
10038      * This is handled in the same way as restoration of the
10039      * PC in these situations: we will be called again with search_pc=1
10040      * and generate a mapping of the condexec bits for each PC in
10041      * gen_opc_condexec_bits[]. restore_state_to_opc() then uses
10042      * this to restore the condexec bits.
10043      *
10044      * Note that there are no instructions which can read the condexec
10045      * bits, and none which can write non-static values to them, so
10046      * we don't need to care about whether CPUARMState is correct in the
10047      * middle of a TB.
10048      */
10049
10050     /* Reset the conditional execution bits immediately. This avoids
10051        complications trying to do it at the end of the block.  */
10052     if (dc->condexec_mask || dc->condexec_cond)
10053       {
10054         TCGv tmp = tcg_temp_new_i32();
10055         tcg_gen_movi_i32(tmp, 0);
10056         store_cpu_field(tmp, condexec_bits);
10057       }
10058     do {
10059 #ifdef CONFIG_USER_ONLY
10060         /* Intercept jump to the magic kernel page.  */
10061         if (dc->pc >= 0xffff0000) {
10062             /* We always get here via a jump, so know we are not in a
10063                conditional execution block.  */
10064             gen_exception(EXCP_KERNEL_TRAP);
10065             dc->is_jmp = DISAS_UPDATE;
10066             break;
10067         }
10068 #else
10069         if (dc->pc >= 0xfffffff0 && IS_M(env)) {
10070             /* We always get here via a jump, so know we are not in a
10071                conditional execution block.  */
10072             gen_exception(EXCP_EXCEPTION_EXIT);
10073             dc->is_jmp = DISAS_UPDATE;
10074             break;
10075         }
10076 #endif
10077
10078         if (unlikely(!QTAILQ_EMPTY(&env->breakpoints))) {
10079             QTAILQ_FOREACH(bp, &env->breakpoints, entry) {
10080                 if (bp->pc == dc->pc) {
10081                     gen_exception_insn(dc, 0, EXCP_DEBUG);
10082                     /* Advance PC so that clearing the breakpoint will
10083                        invalidate this TB.  */
10084                     dc->pc += 2;
10085                     goto done_generating;
10086                     break;
10087                 }
10088             }
10089         }
10090         if (search_pc) {
10091             j = gen_opc_ptr - gen_opc_buf;
10092             if (lj < j) {
10093                 lj++;
10094                 while (lj < j)
10095                     gen_opc_instr_start[lj++] = 0;
10096             }
10097             gen_opc_pc[lj] = dc->pc;
10098             gen_opc_condexec_bits[lj] = (dc->condexec_cond << 4) | (dc->condexec_mask >> 1);
10099             gen_opc_instr_start[lj] = 1;
10100             gen_opc_icount[lj] = num_insns;
10101         }
10102
10103         if (num_insns + 1 == max_insns && (tb->cflags & CF_LAST_IO))
10104             gen_io_start();
10105
10106         if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP))) {
10107             tcg_gen_debug_insn_start(dc->pc);
10108         }
10109
10110         if (dc->thumb) {
10111             disas_thumb_insn(env, dc);
10112             if (dc->condexec_mask) {
10113                 dc->condexec_cond = (dc->condexec_cond & 0xe)
10114                                    | ((dc->condexec_mask >> 4) & 1);
10115                 dc->condexec_mask = (dc->condexec_mask << 1) & 0x1f;
10116                 if (dc->condexec_mask == 0) {
10117                     dc->condexec_cond = 0;
10118                 }
10119             }
10120         } else {
10121             disas_arm_insn(env, dc);
10122         }
10123
10124         if (dc->condjmp && !dc->is_jmp) {
10125             gen_set_label(dc->condlabel);
10126             dc->condjmp = 0;
10127         }
10128
10129         if (tcg_check_temp_count()) {
10130             fprintf(stderr, "TCG temporary leak before %08x\n", dc->pc);
10131         }
10132
10133         /* Translation stops when a conditional branch is encountered.
10134          * Otherwise the subsequent code could get translated several times.
10135          * Also stop translation when a page boundary is reached.  This
10136          * ensures prefetch aborts occur at the right place.  */
10137         num_insns ++;
10138     } while (!dc->is_jmp && gen_opc_ptr < gen_opc_end &&
10139              !env->singlestep_enabled &&
10140              !singlestep &&
10141              dc->pc < next_page_start &&
10142              num_insns < max_insns);
10143
10144     if (tb->cflags & CF_LAST_IO) {
10145         if (dc->condjmp) {
10146             /* FIXME:  This can theoretically happen with self-modifying
10147                code.  */
10148             cpu_abort(env, "IO on conditional branch instruction");
10149         }
10150         gen_io_end();
10151     }
10152
10153     /* At this stage dc->condjmp will only be set when the skipped
10154        instruction was a conditional branch or trap, and the PC has
10155        already been written.  */
10156     if (unlikely(env->singlestep_enabled)) {
10157         /* Make sure the pc is updated, and raise a debug exception.  */
10158         if (dc->condjmp) {
10159             gen_set_condexec(dc);
10160             if (dc->is_jmp == DISAS_SWI) {
10161                 gen_exception(EXCP_SWI);
10162             } else {
10163                 gen_exception(EXCP_DEBUG);
10164             }
10165             gen_set_label(dc->condlabel);
10166         }
10167         if (dc->condjmp || !dc->is_jmp) {
10168             gen_set_pc_im(dc->pc);
10169             dc->condjmp = 0;
10170         }
10171         gen_set_condexec(dc);
10172         if (dc->is_jmp == DISAS_SWI && !dc->condjmp) {
10173             gen_exception(EXCP_SWI);
10174         } else {
10175             /* FIXME: Single stepping a WFI insn will not halt
10176                the CPU.  */
10177             gen_exception(EXCP_DEBUG);
10178         }
10179     } else {
10180         /* While branches must always occur at the end of an IT block,
10181            there are a few other things that can cause us to terminate
10182            the TB in the middel of an IT block:
10183             - Exception generating instructions (bkpt, swi, undefined).
10184             - Page boundaries.
10185             - Hardware watchpoints.
10186            Hardware breakpoints have already been handled and skip this code.
10187          */
10188         gen_set_condexec(dc);
10189         switch(dc->is_jmp) {
10190         case DISAS_NEXT:
10191             gen_goto_tb(dc, 1, dc->pc);
10192             break;
10193         default:
10194         case DISAS_JUMP:
10195         case DISAS_UPDATE:
10196             /* indicate that the hash table must be used to find the next TB */
10197             tcg_gen_exit_tb(0);
10198             break;
10199         case DISAS_TB_JUMP:
10200             /* nothing more to generate */
10201             break;
10202         case DISAS_WFI:
10203             gen_helper_wfi();
10204             break;
10205         case DISAS_SWI:
10206             gen_exception(EXCP_SWI);
10207             break;
10208         }
10209         if (dc->condjmp) {
10210             gen_set_label(dc->condlabel);
10211             gen_set_condexec(dc);
10212             gen_goto_tb(dc, 1, dc->pc);
10213             dc->condjmp = 0;
10214         }
10215     }
10216
10217 done_generating:
10218     gen_icount_end(tb, num_insns);
10219     *gen_opc_ptr = INDEX_op_end;
10220
10221 #ifdef DEBUG_DISAS
10222     if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)) {
10223         qemu_log("----------------\n");
10224         qemu_log("IN: %s\n", lookup_symbol(pc_start));
10225         log_target_disas(pc_start, dc->pc - pc_start,
10226                          dc->thumb | (dc->bswap_code << 1));
10227         qemu_log("\n");
10228     }
10229 #endif
10230     if (search_pc) {
10231         j = gen_opc_ptr - gen_opc_buf;
10232         lj++;
10233         while (lj <= j)
10234             gen_opc_instr_start[lj++] = 0;
10235     } else {
10236         tb->size = dc->pc - pc_start;
10237         tb->icount = num_insns;
10238     }
10239 }
10240
10241 void gen_intermediate_code(CPUARMState *env, TranslationBlock *tb)
10242 {
10243     gen_intermediate_code_internal(env, tb, 0);
10244 }
10245
10246 void gen_intermediate_code_pc(CPUARMState *env, TranslationBlock *tb)
10247 {
10248     gen_intermediate_code_internal(env, tb, 1);
10249 }
10250
10251 static const char *cpu_mode_names[16] = {
10252   "usr", "fiq", "irq", "svc", "???", "???", "???", "abt",
10253   "???", "???", "???", "und", "???", "???", "???", "sys"
10254 };
10255
10256 void cpu_dump_state(CPUARMState *env, FILE *f, fprintf_function cpu_fprintf,
10257                     int flags)
10258 {
10259     int i;
10260 #if 0
10261     union {
10262         uint32_t i;
10263         float s;
10264     } s0, s1;
10265     CPU_DoubleU d;
10266     /* ??? This assumes float64 and double have the same layout.
10267        Oh well, it's only debug dumps.  */
10268     union {
10269         float64 f64;
10270         double d;
10271     } d0;
10272 #endif
10273     uint32_t psr;
10274
10275     for(i=0;i<16;i++) {
10276         cpu_fprintf(f, "R%02d=%08x", i, env->regs[i]);
10277         if ((i % 4) == 3)
10278             cpu_fprintf(f, "\n");
10279         else
10280             cpu_fprintf(f, " ");
10281     }
10282     psr = cpsr_read(env);
10283     cpu_fprintf(f, "PSR=%08x %c%c%c%c %c %s%d\n",
10284                 psr,
10285                 psr & (1 << 31) ? 'N' : '-',
10286                 psr & (1 << 30) ? 'Z' : '-',
10287                 psr & (1 << 29) ? 'C' : '-',
10288                 psr & (1 << 28) ? 'V' : '-',
10289                 psr & CPSR_T ? 'T' : 'A',
10290                 cpu_mode_names[psr & 0xf], (psr & 0x10) ? 32 : 26);
10291
10292 #if 0
10293     for (i = 0; i < 16; i++) {
10294         d.d = env->vfp.regs[i];
10295         s0.i = d.l.lower;
10296         s1.i = d.l.upper;
10297         d0.f64 = d.d;
10298         cpu_fprintf(f, "s%02d=%08x(%8g) s%02d=%08x(%8g) d%02d=%08x%08x(%8g)\n",
10299                     i * 2, (int)s0.i, s0.s,
10300                     i * 2 + 1, (int)s1.i, s1.s,
10301                     i, (int)(uint32_t)d.l.upper, (int)(uint32_t)d.l.lower,
10302                     d0.d);
10303     }
10304     cpu_fprintf(f, "FPSCR: %08x\n", (int)env->vfp.xregs[ARM_VFP_FPSCR]);
10305 #endif
10306 }
10307
10308 void restore_state_to_opc(CPUARMState *env, TranslationBlock *tb, int pc_pos)
10309 {
10310     env->regs[15] = gen_opc_pc[pc_pos];
10311     env->condexec_bits = gen_opc_condexec_bits[pc_pos];
10312 }
This page took 0.600058 seconds and 2 git commands to generate.