]> Git Repo - qemu.git/blob - target-arm/translate.c
target-arm: Convert generic timer cp15 regs
[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 /* Disassemble system coprocessor (cp15) instruction.  Return nonzero if
2443    instruction is not defined.  */
2444 static int disas_cp15_insn(CPUARMState *env, DisasContext *s, uint32_t insn)
2445 {
2446     uint32_t rd;
2447     TCGv tmp, tmp2;
2448
2449     /* M profile cores use memory mapped registers instead of cp15.  */
2450     if (arm_feature(env, ARM_FEATURE_M))
2451         return 1;
2452
2453     if ((insn & (1 << 25)) == 0) {
2454         if (insn & (1 << 20)) {
2455             /* mrrc */
2456             return 1;
2457         }
2458         /* mcrr.  Used for block cache operations, so implement as no-op.  */
2459         return 0;
2460     }
2461     if ((insn & (1 << 4)) == 0) {
2462         /* cdp */
2463         return 1;
2464     }
2465
2466     if (IS_USER(s)) {
2467         return 1;
2468     }
2469
2470     rd = (insn >> 12) & 0xf;
2471
2472     tmp2 = tcg_const_i32(insn);
2473     if (insn & ARM_CP_RW_BIT) {
2474         tmp = tcg_temp_new_i32();
2475         gen_helper_get_cp15(tmp, cpu_env, tmp2);
2476         /* If the destination register is r15 then sets condition codes.  */
2477         if (rd != 15)
2478             store_reg(s, rd, tmp);
2479         else
2480             tcg_temp_free_i32(tmp);
2481     } else {
2482         tmp = load_reg(s, rd);
2483         gen_helper_set_cp15(cpu_env, tmp2, tmp);
2484         tcg_temp_free_i32(tmp);
2485         /* Normally we would always end the TB here, but Linux
2486          * arch/arm/mach-pxa/sleep.S expects two instructions following
2487          * an MMU enable to execute from cache.  Imitate this behaviour.  */
2488         if (!arm_feature(env, ARM_FEATURE_XSCALE) ||
2489                 (insn & 0x0fff0fff) != 0x0e010f10)
2490             gen_lookup_tb(s);
2491     }
2492     tcg_temp_free_i32(tmp2);
2493     return 0;
2494 }
2495
2496 #define VFP_REG_SHR(x, n) (((n) > 0) ? (x) >> (n) : (x) << -(n))
2497 #define VFP_SREG(insn, bigbit, smallbit) \
2498   ((VFP_REG_SHR(insn, bigbit - 1) & 0x1e) | (((insn) >> (smallbit)) & 1))
2499 #define VFP_DREG(reg, insn, bigbit, smallbit) do { \
2500     if (arm_feature(env, ARM_FEATURE_VFP3)) { \
2501         reg = (((insn) >> (bigbit)) & 0x0f) \
2502               | (((insn) >> ((smallbit) - 4)) & 0x10); \
2503     } else { \
2504         if (insn & (1 << (smallbit))) \
2505             return 1; \
2506         reg = ((insn) >> (bigbit)) & 0x0f; \
2507     }} while (0)
2508
2509 #define VFP_SREG_D(insn) VFP_SREG(insn, 12, 22)
2510 #define VFP_DREG_D(reg, insn) VFP_DREG(reg, insn, 12, 22)
2511 #define VFP_SREG_N(insn) VFP_SREG(insn, 16,  7)
2512 #define VFP_DREG_N(reg, insn) VFP_DREG(reg, insn, 16,  7)
2513 #define VFP_SREG_M(insn) VFP_SREG(insn,  0,  5)
2514 #define VFP_DREG_M(reg, insn) VFP_DREG(reg, insn,  0,  5)
2515
2516 /* Move between integer and VFP cores.  */
2517 static TCGv gen_vfp_mrs(void)
2518 {
2519     TCGv tmp = tcg_temp_new_i32();
2520     tcg_gen_mov_i32(tmp, cpu_F0s);
2521     return tmp;
2522 }
2523
2524 static void gen_vfp_msr(TCGv tmp)
2525 {
2526     tcg_gen_mov_i32(cpu_F0s, tmp);
2527     tcg_temp_free_i32(tmp);
2528 }
2529
2530 static void gen_neon_dup_u8(TCGv var, int shift)
2531 {
2532     TCGv tmp = tcg_temp_new_i32();
2533     if (shift)
2534         tcg_gen_shri_i32(var, var, shift);
2535     tcg_gen_ext8u_i32(var, var);
2536     tcg_gen_shli_i32(tmp, var, 8);
2537     tcg_gen_or_i32(var, var, tmp);
2538     tcg_gen_shli_i32(tmp, var, 16);
2539     tcg_gen_or_i32(var, var, tmp);
2540     tcg_temp_free_i32(tmp);
2541 }
2542
2543 static void gen_neon_dup_low16(TCGv var)
2544 {
2545     TCGv tmp = tcg_temp_new_i32();
2546     tcg_gen_ext16u_i32(var, var);
2547     tcg_gen_shli_i32(tmp, var, 16);
2548     tcg_gen_or_i32(var, var, tmp);
2549     tcg_temp_free_i32(tmp);
2550 }
2551
2552 static void gen_neon_dup_high16(TCGv var)
2553 {
2554     TCGv tmp = tcg_temp_new_i32();
2555     tcg_gen_andi_i32(var, var, 0xffff0000);
2556     tcg_gen_shri_i32(tmp, var, 16);
2557     tcg_gen_or_i32(var, var, tmp);
2558     tcg_temp_free_i32(tmp);
2559 }
2560
2561 static TCGv gen_load_and_replicate(DisasContext *s, TCGv addr, int size)
2562 {
2563     /* Load a single Neon element and replicate into a 32 bit TCG reg */
2564     TCGv tmp;
2565     switch (size) {
2566     case 0:
2567         tmp = gen_ld8u(addr, IS_USER(s));
2568         gen_neon_dup_u8(tmp, 0);
2569         break;
2570     case 1:
2571         tmp = gen_ld16u(addr, IS_USER(s));
2572         gen_neon_dup_low16(tmp);
2573         break;
2574     case 2:
2575         tmp = gen_ld32(addr, IS_USER(s));
2576         break;
2577     default: /* Avoid compiler warnings.  */
2578         abort();
2579     }
2580     return tmp;
2581 }
2582
2583 /* Disassemble a VFP instruction.  Returns nonzero if an error occurred
2584    (ie. an undefined instruction).  */
2585 static int disas_vfp_insn(CPUARMState * env, DisasContext *s, uint32_t insn)
2586 {
2587     uint32_t rd, rn, rm, op, i, n, offset, delta_d, delta_m, bank_mask;
2588     int dp, veclen;
2589     TCGv addr;
2590     TCGv tmp;
2591     TCGv tmp2;
2592
2593     if (!arm_feature(env, ARM_FEATURE_VFP))
2594         return 1;
2595
2596     if (!s->vfp_enabled) {
2597         /* VFP disabled.  Only allow fmxr/fmrx to/from some control regs.  */
2598         if ((insn & 0x0fe00fff) != 0x0ee00a10)
2599             return 1;
2600         rn = (insn >> 16) & 0xf;
2601         if (rn != ARM_VFP_FPSID && rn != ARM_VFP_FPEXC
2602             && rn != ARM_VFP_MVFR1 && rn != ARM_VFP_MVFR0)
2603             return 1;
2604     }
2605     dp = ((insn & 0xf00) == 0xb00);
2606     switch ((insn >> 24) & 0xf) {
2607     case 0xe:
2608         if (insn & (1 << 4)) {
2609             /* single register transfer */
2610             rd = (insn >> 12) & 0xf;
2611             if (dp) {
2612                 int size;
2613                 int pass;
2614
2615                 VFP_DREG_N(rn, insn);
2616                 if (insn & 0xf)
2617                     return 1;
2618                 if (insn & 0x00c00060
2619                     && !arm_feature(env, ARM_FEATURE_NEON))
2620                     return 1;
2621
2622                 pass = (insn >> 21) & 1;
2623                 if (insn & (1 << 22)) {
2624                     size = 0;
2625                     offset = ((insn >> 5) & 3) * 8;
2626                 } else if (insn & (1 << 5)) {
2627                     size = 1;
2628                     offset = (insn & (1 << 6)) ? 16 : 0;
2629                 } else {
2630                     size = 2;
2631                     offset = 0;
2632                 }
2633                 if (insn & ARM_CP_RW_BIT) {
2634                     /* vfp->arm */
2635                     tmp = neon_load_reg(rn, pass);
2636                     switch (size) {
2637                     case 0:
2638                         if (offset)
2639                             tcg_gen_shri_i32(tmp, tmp, offset);
2640                         if (insn & (1 << 23))
2641                             gen_uxtb(tmp);
2642                         else
2643                             gen_sxtb(tmp);
2644                         break;
2645                     case 1:
2646                         if (insn & (1 << 23)) {
2647                             if (offset) {
2648                                 tcg_gen_shri_i32(tmp, tmp, 16);
2649                             } else {
2650                                 gen_uxth(tmp);
2651                             }
2652                         } else {
2653                             if (offset) {
2654                                 tcg_gen_sari_i32(tmp, tmp, 16);
2655                             } else {
2656                                 gen_sxth(tmp);
2657                             }
2658                         }
2659                         break;
2660                     case 2:
2661                         break;
2662                     }
2663                     store_reg(s, rd, tmp);
2664                 } else {
2665                     /* arm->vfp */
2666                     tmp = load_reg(s, rd);
2667                     if (insn & (1 << 23)) {
2668                         /* VDUP */
2669                         if (size == 0) {
2670                             gen_neon_dup_u8(tmp, 0);
2671                         } else if (size == 1) {
2672                             gen_neon_dup_low16(tmp);
2673                         }
2674                         for (n = 0; n <= pass * 2; n++) {
2675                             tmp2 = tcg_temp_new_i32();
2676                             tcg_gen_mov_i32(tmp2, tmp);
2677                             neon_store_reg(rn, n, tmp2);
2678                         }
2679                         neon_store_reg(rn, n, tmp);
2680                     } else {
2681                         /* VMOV */
2682                         switch (size) {
2683                         case 0:
2684                             tmp2 = neon_load_reg(rn, pass);
2685                             gen_bfi(tmp, tmp2, tmp, offset, 0xff);
2686                             tcg_temp_free_i32(tmp2);
2687                             break;
2688                         case 1:
2689                             tmp2 = neon_load_reg(rn, pass);
2690                             gen_bfi(tmp, tmp2, tmp, offset, 0xffff);
2691                             tcg_temp_free_i32(tmp2);
2692                             break;
2693                         case 2:
2694                             break;
2695                         }
2696                         neon_store_reg(rn, pass, tmp);
2697                     }
2698                 }
2699             } else { /* !dp */
2700                 if ((insn & 0x6f) != 0x00)
2701                     return 1;
2702                 rn = VFP_SREG_N(insn);
2703                 if (insn & ARM_CP_RW_BIT) {
2704                     /* vfp->arm */
2705                     if (insn & (1 << 21)) {
2706                         /* system register */
2707                         rn >>= 1;
2708
2709                         switch (rn) {
2710                         case ARM_VFP_FPSID:
2711                             /* VFP2 allows access to FSID from userspace.
2712                                VFP3 restricts all id registers to privileged
2713                                accesses.  */
2714                             if (IS_USER(s)
2715                                 && arm_feature(env, ARM_FEATURE_VFP3))
2716                                 return 1;
2717                             tmp = load_cpu_field(vfp.xregs[rn]);
2718                             break;
2719                         case ARM_VFP_FPEXC:
2720                             if (IS_USER(s))
2721                                 return 1;
2722                             tmp = load_cpu_field(vfp.xregs[rn]);
2723                             break;
2724                         case ARM_VFP_FPINST:
2725                         case ARM_VFP_FPINST2:
2726                             /* Not present in VFP3.  */
2727                             if (IS_USER(s)
2728                                 || arm_feature(env, ARM_FEATURE_VFP3))
2729                                 return 1;
2730                             tmp = load_cpu_field(vfp.xregs[rn]);
2731                             break;
2732                         case ARM_VFP_FPSCR:
2733                             if (rd == 15) {
2734                                 tmp = load_cpu_field(vfp.xregs[ARM_VFP_FPSCR]);
2735                                 tcg_gen_andi_i32(tmp, tmp, 0xf0000000);
2736                             } else {
2737                                 tmp = tcg_temp_new_i32();
2738                                 gen_helper_vfp_get_fpscr(tmp, cpu_env);
2739                             }
2740                             break;
2741                         case ARM_VFP_MVFR0:
2742                         case ARM_VFP_MVFR1:
2743                             if (IS_USER(s)
2744                                 || !arm_feature(env, ARM_FEATURE_MVFR))
2745                                 return 1;
2746                             tmp = load_cpu_field(vfp.xregs[rn]);
2747                             break;
2748                         default:
2749                             return 1;
2750                         }
2751                     } else {
2752                         gen_mov_F0_vreg(0, rn);
2753                         tmp = gen_vfp_mrs();
2754                     }
2755                     if (rd == 15) {
2756                         /* Set the 4 flag bits in the CPSR.  */
2757                         gen_set_nzcv(tmp);
2758                         tcg_temp_free_i32(tmp);
2759                     } else {
2760                         store_reg(s, rd, tmp);
2761                     }
2762                 } else {
2763                     /* arm->vfp */
2764                     tmp = load_reg(s, rd);
2765                     if (insn & (1 << 21)) {
2766                         rn >>= 1;
2767                         /* system register */
2768                         switch (rn) {
2769                         case ARM_VFP_FPSID:
2770                         case ARM_VFP_MVFR0:
2771                         case ARM_VFP_MVFR1:
2772                             /* Writes are ignored.  */
2773                             break;
2774                         case ARM_VFP_FPSCR:
2775                             gen_helper_vfp_set_fpscr(cpu_env, tmp);
2776                             tcg_temp_free_i32(tmp);
2777                             gen_lookup_tb(s);
2778                             break;
2779                         case ARM_VFP_FPEXC:
2780                             if (IS_USER(s))
2781                                 return 1;
2782                             /* TODO: VFP subarchitecture support.
2783                              * For now, keep the EN bit only */
2784                             tcg_gen_andi_i32(tmp, tmp, 1 << 30);
2785                             store_cpu_field(tmp, vfp.xregs[rn]);
2786                             gen_lookup_tb(s);
2787                             break;
2788                         case ARM_VFP_FPINST:
2789                         case ARM_VFP_FPINST2:
2790                             store_cpu_field(tmp, vfp.xregs[rn]);
2791                             break;
2792                         default:
2793                             return 1;
2794                         }
2795                     } else {
2796                         gen_vfp_msr(tmp);
2797                         gen_mov_vreg_F0(0, rn);
2798                     }
2799                 }
2800             }
2801         } else {
2802             /* data processing */
2803             /* The opcode is in bits 23, 21, 20 and 6.  */
2804             op = ((insn >> 20) & 8) | ((insn >> 19) & 6) | ((insn >> 6) & 1);
2805             if (dp) {
2806                 if (op == 15) {
2807                     /* rn is opcode */
2808                     rn = ((insn >> 15) & 0x1e) | ((insn >> 7) & 1);
2809                 } else {
2810                     /* rn is register number */
2811                     VFP_DREG_N(rn, insn);
2812                 }
2813
2814                 if (op == 15 && (rn == 15 || ((rn & 0x1c) == 0x18))) {
2815                     /* Integer or single precision destination.  */
2816                     rd = VFP_SREG_D(insn);
2817                 } else {
2818                     VFP_DREG_D(rd, insn);
2819                 }
2820                 if (op == 15 &&
2821                     (((rn & 0x1c) == 0x10) || ((rn & 0x14) == 0x14))) {
2822                     /* VCVT from int is always from S reg regardless of dp bit.
2823                      * VCVT with immediate frac_bits has same format as SREG_M
2824                      */
2825                     rm = VFP_SREG_M(insn);
2826                 } else {
2827                     VFP_DREG_M(rm, insn);
2828                 }
2829             } else {
2830                 rn = VFP_SREG_N(insn);
2831                 if (op == 15 && rn == 15) {
2832                     /* Double precision destination.  */
2833                     VFP_DREG_D(rd, insn);
2834                 } else {
2835                     rd = VFP_SREG_D(insn);
2836                 }
2837                 /* NB that we implicitly rely on the encoding for the frac_bits
2838                  * in VCVT of fixed to float being the same as that of an SREG_M
2839                  */
2840                 rm = VFP_SREG_M(insn);
2841             }
2842
2843             veclen = s->vec_len;
2844             if (op == 15 && rn > 3)
2845                 veclen = 0;
2846
2847             /* Shut up compiler warnings.  */
2848             delta_m = 0;
2849             delta_d = 0;
2850             bank_mask = 0;
2851
2852             if (veclen > 0) {
2853                 if (dp)
2854                     bank_mask = 0xc;
2855                 else
2856                     bank_mask = 0x18;
2857
2858                 /* Figure out what type of vector operation this is.  */
2859                 if ((rd & bank_mask) == 0) {
2860                     /* scalar */
2861                     veclen = 0;
2862                 } else {
2863                     if (dp)
2864                         delta_d = (s->vec_stride >> 1) + 1;
2865                     else
2866                         delta_d = s->vec_stride + 1;
2867
2868                     if ((rm & bank_mask) == 0) {
2869                         /* mixed scalar/vector */
2870                         delta_m = 0;
2871                     } else {
2872                         /* vector */
2873                         delta_m = delta_d;
2874                     }
2875                 }
2876             }
2877
2878             /* Load the initial operands.  */
2879             if (op == 15) {
2880                 switch (rn) {
2881                 case 16:
2882                 case 17:
2883                     /* Integer source */
2884                     gen_mov_F0_vreg(0, rm);
2885                     break;
2886                 case 8:
2887                 case 9:
2888                     /* Compare */
2889                     gen_mov_F0_vreg(dp, rd);
2890                     gen_mov_F1_vreg(dp, rm);
2891                     break;
2892                 case 10:
2893                 case 11:
2894                     /* Compare with zero */
2895                     gen_mov_F0_vreg(dp, rd);
2896                     gen_vfp_F1_ld0(dp);
2897                     break;
2898                 case 20:
2899                 case 21:
2900                 case 22:
2901                 case 23:
2902                 case 28:
2903                 case 29:
2904                 case 30:
2905                 case 31:
2906                     /* Source and destination the same.  */
2907                     gen_mov_F0_vreg(dp, rd);
2908                     break;
2909                 case 4:
2910                 case 5:
2911                 case 6:
2912                 case 7:
2913                     /* VCVTB, VCVTT: only present with the halfprec extension,
2914                      * UNPREDICTABLE if bit 8 is set (we choose to UNDEF)
2915                      */
2916                     if (dp || !arm_feature(env, ARM_FEATURE_VFP_FP16)) {
2917                         return 1;
2918                     }
2919                     /* Otherwise fall through */
2920                 default:
2921                     /* One source operand.  */
2922                     gen_mov_F0_vreg(dp, rm);
2923                     break;
2924                 }
2925             } else {
2926                 /* Two source operands.  */
2927                 gen_mov_F0_vreg(dp, rn);
2928                 gen_mov_F1_vreg(dp, rm);
2929             }
2930
2931             for (;;) {
2932                 /* Perform the calculation.  */
2933                 switch (op) {
2934                 case 0: /* VMLA: fd + (fn * fm) */
2935                     /* Note that order of inputs to the add matters for NaNs */
2936                     gen_vfp_F1_mul(dp);
2937                     gen_mov_F0_vreg(dp, rd);
2938                     gen_vfp_add(dp);
2939                     break;
2940                 case 1: /* VMLS: fd + -(fn * fm) */
2941                     gen_vfp_mul(dp);
2942                     gen_vfp_F1_neg(dp);
2943                     gen_mov_F0_vreg(dp, rd);
2944                     gen_vfp_add(dp);
2945                     break;
2946                 case 2: /* VNMLS: -fd + (fn * fm) */
2947                     /* Note that it isn't valid to replace (-A + B) with (B - A)
2948                      * or similar plausible looking simplifications
2949                      * because this will give wrong results for NaNs.
2950                      */
2951                     gen_vfp_F1_mul(dp);
2952                     gen_mov_F0_vreg(dp, rd);
2953                     gen_vfp_neg(dp);
2954                     gen_vfp_add(dp);
2955                     break;
2956                 case 3: /* VNMLA: -fd + -(fn * fm) */
2957                     gen_vfp_mul(dp);
2958                     gen_vfp_F1_neg(dp);
2959                     gen_mov_F0_vreg(dp, rd);
2960                     gen_vfp_neg(dp);
2961                     gen_vfp_add(dp);
2962                     break;
2963                 case 4: /* mul: fn * fm */
2964                     gen_vfp_mul(dp);
2965                     break;
2966                 case 5: /* nmul: -(fn * fm) */
2967                     gen_vfp_mul(dp);
2968                     gen_vfp_neg(dp);
2969                     break;
2970                 case 6: /* add: fn + fm */
2971                     gen_vfp_add(dp);
2972                     break;
2973                 case 7: /* sub: fn - fm */
2974                     gen_vfp_sub(dp);
2975                     break;
2976                 case 8: /* div: fn / fm */
2977                     gen_vfp_div(dp);
2978                     break;
2979                 case 10: /* VFNMA : fd = muladd(-fd,  fn, fm) */
2980                 case 11: /* VFNMS : fd = muladd(-fd, -fn, fm) */
2981                 case 12: /* VFMA  : fd = muladd( fd,  fn, fm) */
2982                 case 13: /* VFMS  : fd = muladd( fd, -fn, fm) */
2983                     /* These are fused multiply-add, and must be done as one
2984                      * floating point operation with no rounding between the
2985                      * multiplication and addition steps.
2986                      * NB that doing the negations here as separate steps is
2987                      * correct : an input NaN should come out with its sign bit
2988                      * flipped if it is a negated-input.
2989                      */
2990                     if (!arm_feature(env, ARM_FEATURE_VFP4)) {
2991                         return 1;
2992                     }
2993                     if (dp) {
2994                         TCGv_ptr fpst;
2995                         TCGv_i64 frd;
2996                         if (op & 1) {
2997                             /* VFNMS, VFMS */
2998                             gen_helper_vfp_negd(cpu_F0d, cpu_F0d);
2999                         }
3000                         frd = tcg_temp_new_i64();
3001                         tcg_gen_ld_f64(frd, cpu_env, vfp_reg_offset(dp, rd));
3002                         if (op & 2) {
3003                             /* VFNMA, VFNMS */
3004                             gen_helper_vfp_negd(frd, frd);
3005                         }
3006                         fpst = get_fpstatus_ptr(0);
3007                         gen_helper_vfp_muladdd(cpu_F0d, cpu_F0d,
3008                                                cpu_F1d, frd, fpst);
3009                         tcg_temp_free_ptr(fpst);
3010                         tcg_temp_free_i64(frd);
3011                     } else {
3012                         TCGv_ptr fpst;
3013                         TCGv_i32 frd;
3014                         if (op & 1) {
3015                             /* VFNMS, VFMS */
3016                             gen_helper_vfp_negs(cpu_F0s, cpu_F0s);
3017                         }
3018                         frd = tcg_temp_new_i32();
3019                         tcg_gen_ld_f32(frd, cpu_env, vfp_reg_offset(dp, rd));
3020                         if (op & 2) {
3021                             gen_helper_vfp_negs(frd, frd);
3022                         }
3023                         fpst = get_fpstatus_ptr(0);
3024                         gen_helper_vfp_muladds(cpu_F0s, cpu_F0s,
3025                                                cpu_F1s, frd, fpst);
3026                         tcg_temp_free_ptr(fpst);
3027                         tcg_temp_free_i32(frd);
3028                     }
3029                     break;
3030                 case 14: /* fconst */
3031                     if (!arm_feature(env, ARM_FEATURE_VFP3))
3032                       return 1;
3033
3034                     n = (insn << 12) & 0x80000000;
3035                     i = ((insn >> 12) & 0x70) | (insn & 0xf);
3036                     if (dp) {
3037                         if (i & 0x40)
3038                             i |= 0x3f80;
3039                         else
3040                             i |= 0x4000;
3041                         n |= i << 16;
3042                         tcg_gen_movi_i64(cpu_F0d, ((uint64_t)n) << 32);
3043                     } else {
3044                         if (i & 0x40)
3045                             i |= 0x780;
3046                         else
3047                             i |= 0x800;
3048                         n |= i << 19;
3049                         tcg_gen_movi_i32(cpu_F0s, n);
3050                     }
3051                     break;
3052                 case 15: /* extension space */
3053                     switch (rn) {
3054                     case 0: /* cpy */
3055                         /* no-op */
3056                         break;
3057                     case 1: /* abs */
3058                         gen_vfp_abs(dp);
3059                         break;
3060                     case 2: /* neg */
3061                         gen_vfp_neg(dp);
3062                         break;
3063                     case 3: /* sqrt */
3064                         gen_vfp_sqrt(dp);
3065                         break;
3066                     case 4: /* vcvtb.f32.f16 */
3067                         tmp = gen_vfp_mrs();
3068                         tcg_gen_ext16u_i32(tmp, tmp);
3069                         gen_helper_vfp_fcvt_f16_to_f32(cpu_F0s, tmp, cpu_env);
3070                         tcg_temp_free_i32(tmp);
3071                         break;
3072                     case 5: /* vcvtt.f32.f16 */
3073                         tmp = gen_vfp_mrs();
3074                         tcg_gen_shri_i32(tmp, tmp, 16);
3075                         gen_helper_vfp_fcvt_f16_to_f32(cpu_F0s, tmp, cpu_env);
3076                         tcg_temp_free_i32(tmp);
3077                         break;
3078                     case 6: /* vcvtb.f16.f32 */
3079                         tmp = tcg_temp_new_i32();
3080                         gen_helper_vfp_fcvt_f32_to_f16(tmp, cpu_F0s, cpu_env);
3081                         gen_mov_F0_vreg(0, rd);
3082                         tmp2 = gen_vfp_mrs();
3083                         tcg_gen_andi_i32(tmp2, tmp2, 0xffff0000);
3084                         tcg_gen_or_i32(tmp, tmp, tmp2);
3085                         tcg_temp_free_i32(tmp2);
3086                         gen_vfp_msr(tmp);
3087                         break;
3088                     case 7: /* vcvtt.f16.f32 */
3089                         tmp = tcg_temp_new_i32();
3090                         gen_helper_vfp_fcvt_f32_to_f16(tmp, cpu_F0s, cpu_env);
3091                         tcg_gen_shli_i32(tmp, tmp, 16);
3092                         gen_mov_F0_vreg(0, rd);
3093                         tmp2 = gen_vfp_mrs();
3094                         tcg_gen_ext16u_i32(tmp2, tmp2);
3095                         tcg_gen_or_i32(tmp, tmp, tmp2);
3096                         tcg_temp_free_i32(tmp2);
3097                         gen_vfp_msr(tmp);
3098                         break;
3099                     case 8: /* cmp */
3100                         gen_vfp_cmp(dp);
3101                         break;
3102                     case 9: /* cmpe */
3103                         gen_vfp_cmpe(dp);
3104                         break;
3105                     case 10: /* cmpz */
3106                         gen_vfp_cmp(dp);
3107                         break;
3108                     case 11: /* cmpez */
3109                         gen_vfp_F1_ld0(dp);
3110                         gen_vfp_cmpe(dp);
3111                         break;
3112                     case 15: /* single<->double conversion */
3113                         if (dp)
3114                             gen_helper_vfp_fcvtsd(cpu_F0s, cpu_F0d, cpu_env);
3115                         else
3116                             gen_helper_vfp_fcvtds(cpu_F0d, cpu_F0s, cpu_env);
3117                         break;
3118                     case 16: /* fuito */
3119                         gen_vfp_uito(dp, 0);
3120                         break;
3121                     case 17: /* fsito */
3122                         gen_vfp_sito(dp, 0);
3123                         break;
3124                     case 20: /* fshto */
3125                         if (!arm_feature(env, ARM_FEATURE_VFP3))
3126                           return 1;
3127                         gen_vfp_shto(dp, 16 - rm, 0);
3128                         break;
3129                     case 21: /* fslto */
3130                         if (!arm_feature(env, ARM_FEATURE_VFP3))
3131                           return 1;
3132                         gen_vfp_slto(dp, 32 - rm, 0);
3133                         break;
3134                     case 22: /* fuhto */
3135                         if (!arm_feature(env, ARM_FEATURE_VFP3))
3136                           return 1;
3137                         gen_vfp_uhto(dp, 16 - rm, 0);
3138                         break;
3139                     case 23: /* fulto */
3140                         if (!arm_feature(env, ARM_FEATURE_VFP3))
3141                           return 1;
3142                         gen_vfp_ulto(dp, 32 - rm, 0);
3143                         break;
3144                     case 24: /* ftoui */
3145                         gen_vfp_toui(dp, 0);
3146                         break;
3147                     case 25: /* ftouiz */
3148                         gen_vfp_touiz(dp, 0);
3149                         break;
3150                     case 26: /* ftosi */
3151                         gen_vfp_tosi(dp, 0);
3152                         break;
3153                     case 27: /* ftosiz */
3154                         gen_vfp_tosiz(dp, 0);
3155                         break;
3156                     case 28: /* ftosh */
3157                         if (!arm_feature(env, ARM_FEATURE_VFP3))
3158                           return 1;
3159                         gen_vfp_tosh(dp, 16 - rm, 0);
3160                         break;
3161                     case 29: /* ftosl */
3162                         if (!arm_feature(env, ARM_FEATURE_VFP3))
3163                           return 1;
3164                         gen_vfp_tosl(dp, 32 - rm, 0);
3165                         break;
3166                     case 30: /* ftouh */
3167                         if (!arm_feature(env, ARM_FEATURE_VFP3))
3168                           return 1;
3169                         gen_vfp_touh(dp, 16 - rm, 0);
3170                         break;
3171                     case 31: /* ftoul */
3172                         if (!arm_feature(env, ARM_FEATURE_VFP3))
3173                           return 1;
3174                         gen_vfp_toul(dp, 32 - rm, 0);
3175                         break;
3176                     default: /* undefined */
3177                         return 1;
3178                     }
3179                     break;
3180                 default: /* undefined */
3181                     return 1;
3182                 }
3183
3184                 /* Write back the result.  */
3185                 if (op == 15 && (rn >= 8 && rn <= 11))
3186                     ; /* Comparison, do nothing.  */
3187                 else if (op == 15 && dp && ((rn & 0x1c) == 0x18))
3188                     /* VCVT double to int: always integer result. */
3189                     gen_mov_vreg_F0(0, rd);
3190                 else if (op == 15 && rn == 15)
3191                     /* conversion */
3192                     gen_mov_vreg_F0(!dp, rd);
3193                 else
3194                     gen_mov_vreg_F0(dp, rd);
3195
3196                 /* break out of the loop if we have finished  */
3197                 if (veclen == 0)
3198                     break;
3199
3200                 if (op == 15 && delta_m == 0) {
3201                     /* single source one-many */
3202                     while (veclen--) {
3203                         rd = ((rd + delta_d) & (bank_mask - 1))
3204                              | (rd & bank_mask);
3205                         gen_mov_vreg_F0(dp, rd);
3206                     }
3207                     break;
3208                 }
3209                 /* Setup the next operands.  */
3210                 veclen--;
3211                 rd = ((rd + delta_d) & (bank_mask - 1))
3212                      | (rd & bank_mask);
3213
3214                 if (op == 15) {
3215                     /* One source operand.  */
3216                     rm = ((rm + delta_m) & (bank_mask - 1))
3217                          | (rm & bank_mask);
3218                     gen_mov_F0_vreg(dp, rm);
3219                 } else {
3220                     /* Two source operands.  */
3221                     rn = ((rn + delta_d) & (bank_mask - 1))
3222                          | (rn & bank_mask);
3223                     gen_mov_F0_vreg(dp, rn);
3224                     if (delta_m) {
3225                         rm = ((rm + delta_m) & (bank_mask - 1))
3226                              | (rm & bank_mask);
3227                         gen_mov_F1_vreg(dp, rm);
3228                     }
3229                 }
3230             }
3231         }
3232         break;
3233     case 0xc:
3234     case 0xd:
3235         if ((insn & 0x03e00000) == 0x00400000) {
3236             /* two-register transfer */
3237             rn = (insn >> 16) & 0xf;
3238             rd = (insn >> 12) & 0xf;
3239             if (dp) {
3240                 VFP_DREG_M(rm, insn);
3241             } else {
3242                 rm = VFP_SREG_M(insn);
3243             }
3244
3245             if (insn & ARM_CP_RW_BIT) {
3246                 /* vfp->arm */
3247                 if (dp) {
3248                     gen_mov_F0_vreg(0, rm * 2);
3249                     tmp = gen_vfp_mrs();
3250                     store_reg(s, rd, tmp);
3251                     gen_mov_F0_vreg(0, rm * 2 + 1);
3252                     tmp = gen_vfp_mrs();
3253                     store_reg(s, rn, tmp);
3254                 } else {
3255                     gen_mov_F0_vreg(0, rm);
3256                     tmp = gen_vfp_mrs();
3257                     store_reg(s, rd, tmp);
3258                     gen_mov_F0_vreg(0, rm + 1);
3259                     tmp = gen_vfp_mrs();
3260                     store_reg(s, rn, tmp);
3261                 }
3262             } else {
3263                 /* arm->vfp */
3264                 if (dp) {
3265                     tmp = load_reg(s, rd);
3266                     gen_vfp_msr(tmp);
3267                     gen_mov_vreg_F0(0, rm * 2);
3268                     tmp = load_reg(s, rn);
3269                     gen_vfp_msr(tmp);
3270                     gen_mov_vreg_F0(0, rm * 2 + 1);
3271                 } else {
3272                     tmp = load_reg(s, rd);
3273                     gen_vfp_msr(tmp);
3274                     gen_mov_vreg_F0(0, rm);
3275                     tmp = load_reg(s, rn);
3276                     gen_vfp_msr(tmp);
3277                     gen_mov_vreg_F0(0, rm + 1);
3278                 }
3279             }
3280         } else {
3281             /* Load/store */
3282             rn = (insn >> 16) & 0xf;
3283             if (dp)
3284                 VFP_DREG_D(rd, insn);
3285             else
3286                 rd = VFP_SREG_D(insn);
3287             if ((insn & 0x01200000) == 0x01000000) {
3288                 /* Single load/store */
3289                 offset = (insn & 0xff) << 2;
3290                 if ((insn & (1 << 23)) == 0)
3291                     offset = -offset;
3292                 if (s->thumb && rn == 15) {
3293                     /* This is actually UNPREDICTABLE */
3294                     addr = tcg_temp_new_i32();
3295                     tcg_gen_movi_i32(addr, s->pc & ~2);
3296                 } else {
3297                     addr = load_reg(s, rn);
3298                 }
3299                 tcg_gen_addi_i32(addr, addr, offset);
3300                 if (insn & (1 << 20)) {
3301                     gen_vfp_ld(s, dp, addr);
3302                     gen_mov_vreg_F0(dp, rd);
3303                 } else {
3304                     gen_mov_F0_vreg(dp, rd);
3305                     gen_vfp_st(s, dp, addr);
3306                 }
3307                 tcg_temp_free_i32(addr);
3308             } else {
3309                 /* load/store multiple */
3310                 int w = insn & (1 << 21);
3311                 if (dp)
3312                     n = (insn >> 1) & 0x7f;
3313                 else
3314                     n = insn & 0xff;
3315
3316                 if (w && !(((insn >> 23) ^ (insn >> 24)) & 1)) {
3317                     /* P == U , W == 1  => UNDEF */
3318                     return 1;
3319                 }
3320                 if (n == 0 || (rd + n) > 32 || (dp && n > 16)) {
3321                     /* UNPREDICTABLE cases for bad immediates: we choose to
3322                      * UNDEF to avoid generating huge numbers of TCG ops
3323                      */
3324                     return 1;
3325                 }
3326                 if (rn == 15 && w) {
3327                     /* writeback to PC is UNPREDICTABLE, we choose to UNDEF */
3328                     return 1;
3329                 }
3330
3331                 if (s->thumb && rn == 15) {
3332                     /* This is actually UNPREDICTABLE */
3333                     addr = tcg_temp_new_i32();
3334                     tcg_gen_movi_i32(addr, s->pc & ~2);
3335                 } else {
3336                     addr = load_reg(s, rn);
3337                 }
3338                 if (insn & (1 << 24)) /* pre-decrement */
3339                     tcg_gen_addi_i32(addr, addr, -((insn & 0xff) << 2));
3340
3341                 if (dp)
3342                     offset = 8;
3343                 else
3344                     offset = 4;
3345                 for (i = 0; i < n; i++) {
3346                     if (insn & ARM_CP_RW_BIT) {
3347                         /* load */
3348                         gen_vfp_ld(s, dp, addr);
3349                         gen_mov_vreg_F0(dp, rd + i);
3350                     } else {
3351                         /* store */
3352                         gen_mov_F0_vreg(dp, rd + i);
3353                         gen_vfp_st(s, dp, addr);
3354                     }
3355                     tcg_gen_addi_i32(addr, addr, offset);
3356                 }
3357                 if (w) {
3358                     /* writeback */
3359                     if (insn & (1 << 24))
3360                         offset = -offset * n;
3361                     else if (dp && (insn & 1))
3362                         offset = 4;
3363                     else
3364                         offset = 0;
3365
3366                     if (offset != 0)
3367                         tcg_gen_addi_i32(addr, addr, offset);
3368                     store_reg(s, rn, addr);
3369                 } else {
3370                     tcg_temp_free_i32(addr);
3371                 }
3372             }
3373         }
3374         break;
3375     default:
3376         /* Should never happen.  */
3377         return 1;
3378     }
3379     return 0;
3380 }
3381
3382 static inline void gen_goto_tb(DisasContext *s, int n, uint32_t dest)
3383 {
3384     TranslationBlock *tb;
3385
3386     tb = s->tb;
3387     if ((tb->pc & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK)) {
3388         tcg_gen_goto_tb(n);
3389         gen_set_pc_im(dest);
3390         tcg_gen_exit_tb((tcg_target_long)tb + n);
3391     } else {
3392         gen_set_pc_im(dest);
3393         tcg_gen_exit_tb(0);
3394     }
3395 }
3396
3397 static inline void gen_jmp (DisasContext *s, uint32_t dest)
3398 {
3399     if (unlikely(s->singlestep_enabled)) {
3400         /* An indirect jump so that we still trigger the debug exception.  */
3401         if (s->thumb)
3402             dest |= 1;
3403         gen_bx_im(s, dest);
3404     } else {
3405         gen_goto_tb(s, 0, dest);
3406         s->is_jmp = DISAS_TB_JUMP;
3407     }
3408 }
3409
3410 static inline void gen_mulxy(TCGv t0, TCGv t1, int x, int y)
3411 {
3412     if (x)
3413         tcg_gen_sari_i32(t0, t0, 16);
3414     else
3415         gen_sxth(t0);
3416     if (y)
3417         tcg_gen_sari_i32(t1, t1, 16);
3418     else
3419         gen_sxth(t1);
3420     tcg_gen_mul_i32(t0, t0, t1);
3421 }
3422
3423 /* Return the mask of PSR bits set by a MSR instruction.  */
3424 static uint32_t msr_mask(CPUARMState *env, DisasContext *s, int flags, int spsr) {
3425     uint32_t mask;
3426
3427     mask = 0;
3428     if (flags & (1 << 0))
3429         mask |= 0xff;
3430     if (flags & (1 << 1))
3431         mask |= 0xff00;
3432     if (flags & (1 << 2))
3433         mask |= 0xff0000;
3434     if (flags & (1 << 3))
3435         mask |= 0xff000000;
3436
3437     /* Mask out undefined bits.  */
3438     mask &= ~CPSR_RESERVED;
3439     if (!arm_feature(env, ARM_FEATURE_V4T))
3440         mask &= ~CPSR_T;
3441     if (!arm_feature(env, ARM_FEATURE_V5))
3442         mask &= ~CPSR_Q; /* V5TE in reality*/
3443     if (!arm_feature(env, ARM_FEATURE_V6))
3444         mask &= ~(CPSR_E | CPSR_GE);
3445     if (!arm_feature(env, ARM_FEATURE_THUMB2))
3446         mask &= ~CPSR_IT;
3447     /* Mask out execution state bits.  */
3448     if (!spsr)
3449         mask &= ~CPSR_EXEC;
3450     /* Mask out privileged bits.  */
3451     if (IS_USER(s))
3452         mask &= CPSR_USER;
3453     return mask;
3454 }
3455
3456 /* Returns nonzero if access to the PSR is not permitted. Marks t0 as dead. */
3457 static int gen_set_psr(DisasContext *s, uint32_t mask, int spsr, TCGv t0)
3458 {
3459     TCGv tmp;
3460     if (spsr) {
3461         /* ??? This is also undefined in system mode.  */
3462         if (IS_USER(s))
3463             return 1;
3464
3465         tmp = load_cpu_field(spsr);
3466         tcg_gen_andi_i32(tmp, tmp, ~mask);
3467         tcg_gen_andi_i32(t0, t0, mask);
3468         tcg_gen_or_i32(tmp, tmp, t0);
3469         store_cpu_field(tmp, spsr);
3470     } else {
3471         gen_set_cpsr(t0, mask);
3472     }
3473     tcg_temp_free_i32(t0);
3474     gen_lookup_tb(s);
3475     return 0;
3476 }
3477
3478 /* Returns nonzero if access to the PSR is not permitted.  */
3479 static int gen_set_psr_im(DisasContext *s, uint32_t mask, int spsr, uint32_t val)
3480 {
3481     TCGv tmp;
3482     tmp = tcg_temp_new_i32();
3483     tcg_gen_movi_i32(tmp, val);
3484     return gen_set_psr(s, mask, spsr, tmp);
3485 }
3486
3487 /* Generate an old-style exception return. Marks pc as dead. */
3488 static void gen_exception_return(DisasContext *s, TCGv pc)
3489 {
3490     TCGv tmp;
3491     store_reg(s, 15, pc);
3492     tmp = load_cpu_field(spsr);
3493     gen_set_cpsr(tmp, 0xffffffff);
3494     tcg_temp_free_i32(tmp);
3495     s->is_jmp = DISAS_UPDATE;
3496 }
3497
3498 /* Generate a v6 exception return.  Marks both values as dead.  */
3499 static void gen_rfe(DisasContext *s, TCGv pc, TCGv cpsr)
3500 {
3501     gen_set_cpsr(cpsr, 0xffffffff);
3502     tcg_temp_free_i32(cpsr);
3503     store_reg(s, 15, pc);
3504     s->is_jmp = DISAS_UPDATE;
3505 }
3506
3507 static inline void
3508 gen_set_condexec (DisasContext *s)
3509 {
3510     if (s->condexec_mask) {
3511         uint32_t val = (s->condexec_cond << 4) | (s->condexec_mask >> 1);
3512         TCGv tmp = tcg_temp_new_i32();
3513         tcg_gen_movi_i32(tmp, val);
3514         store_cpu_field(tmp, condexec_bits);
3515     }
3516 }
3517
3518 static void gen_exception_insn(DisasContext *s, int offset, int excp)
3519 {
3520     gen_set_condexec(s);
3521     gen_set_pc_im(s->pc - offset);
3522     gen_exception(excp);
3523     s->is_jmp = DISAS_JUMP;
3524 }
3525
3526 static void gen_nop_hint(DisasContext *s, int val)
3527 {
3528     switch (val) {
3529     case 3: /* wfi */
3530         gen_set_pc_im(s->pc);
3531         s->is_jmp = DISAS_WFI;
3532         break;
3533     case 2: /* wfe */
3534     case 4: /* sev */
3535         /* TODO: Implement SEV and WFE.  May help SMP performance.  */
3536     default: /* nop */
3537         break;
3538     }
3539 }
3540
3541 #define CPU_V001 cpu_V0, cpu_V0, cpu_V1
3542
3543 static inline void gen_neon_add(int size, TCGv t0, TCGv t1)
3544 {
3545     switch (size) {
3546     case 0: gen_helper_neon_add_u8(t0, t0, t1); break;
3547     case 1: gen_helper_neon_add_u16(t0, t0, t1); break;
3548     case 2: tcg_gen_add_i32(t0, t0, t1); break;
3549     default: abort();
3550     }
3551 }
3552
3553 static inline void gen_neon_rsb(int size, TCGv t0, TCGv t1)
3554 {
3555     switch (size) {
3556     case 0: gen_helper_neon_sub_u8(t0, t1, t0); break;
3557     case 1: gen_helper_neon_sub_u16(t0, t1, t0); break;
3558     case 2: tcg_gen_sub_i32(t0, t1, t0); break;
3559     default: return;
3560     }
3561 }
3562
3563 /* 32-bit pairwise ops end up the same as the elementwise versions.  */
3564 #define gen_helper_neon_pmax_s32  gen_helper_neon_max_s32
3565 #define gen_helper_neon_pmax_u32  gen_helper_neon_max_u32
3566 #define gen_helper_neon_pmin_s32  gen_helper_neon_min_s32
3567 #define gen_helper_neon_pmin_u32  gen_helper_neon_min_u32
3568
3569 #define GEN_NEON_INTEGER_OP_ENV(name) do { \
3570     switch ((size << 1) | u) { \
3571     case 0: \
3572         gen_helper_neon_##name##_s8(tmp, cpu_env, tmp, tmp2); \
3573         break; \
3574     case 1: \
3575         gen_helper_neon_##name##_u8(tmp, cpu_env, tmp, tmp2); \
3576         break; \
3577     case 2: \
3578         gen_helper_neon_##name##_s16(tmp, cpu_env, tmp, tmp2); \
3579         break; \
3580     case 3: \
3581         gen_helper_neon_##name##_u16(tmp, cpu_env, tmp, tmp2); \
3582         break; \
3583     case 4: \
3584         gen_helper_neon_##name##_s32(tmp, cpu_env, tmp, tmp2); \
3585         break; \
3586     case 5: \
3587         gen_helper_neon_##name##_u32(tmp, cpu_env, tmp, tmp2); \
3588         break; \
3589     default: return 1; \
3590     }} while (0)
3591
3592 #define GEN_NEON_INTEGER_OP(name) do { \
3593     switch ((size << 1) | u) { \
3594     case 0: \
3595         gen_helper_neon_##name##_s8(tmp, tmp, tmp2); \
3596         break; \
3597     case 1: \
3598         gen_helper_neon_##name##_u8(tmp, tmp, tmp2); \
3599         break; \
3600     case 2: \
3601         gen_helper_neon_##name##_s16(tmp, tmp, tmp2); \
3602         break; \
3603     case 3: \
3604         gen_helper_neon_##name##_u16(tmp, tmp, tmp2); \
3605         break; \
3606     case 4: \
3607         gen_helper_neon_##name##_s32(tmp, tmp, tmp2); \
3608         break; \
3609     case 5: \
3610         gen_helper_neon_##name##_u32(tmp, tmp, tmp2); \
3611         break; \
3612     default: return 1; \
3613     }} while (0)
3614
3615 static TCGv neon_load_scratch(int scratch)
3616 {
3617     TCGv tmp = tcg_temp_new_i32();
3618     tcg_gen_ld_i32(tmp, cpu_env, offsetof(CPUARMState, vfp.scratch[scratch]));
3619     return tmp;
3620 }
3621
3622 static void neon_store_scratch(int scratch, TCGv var)
3623 {
3624     tcg_gen_st_i32(var, cpu_env, offsetof(CPUARMState, vfp.scratch[scratch]));
3625     tcg_temp_free_i32(var);
3626 }
3627
3628 static inline TCGv neon_get_scalar(int size, int reg)
3629 {
3630     TCGv tmp;
3631     if (size == 1) {
3632         tmp = neon_load_reg(reg & 7, reg >> 4);
3633         if (reg & 8) {
3634             gen_neon_dup_high16(tmp);
3635         } else {
3636             gen_neon_dup_low16(tmp);
3637         }
3638     } else {
3639         tmp = neon_load_reg(reg & 15, reg >> 4);
3640     }
3641     return tmp;
3642 }
3643
3644 static int gen_neon_unzip(int rd, int rm, int size, int q)
3645 {
3646     TCGv tmp, tmp2;
3647     if (!q && size == 2) {
3648         return 1;
3649     }
3650     tmp = tcg_const_i32(rd);
3651     tmp2 = tcg_const_i32(rm);
3652     if (q) {
3653         switch (size) {
3654         case 0:
3655             gen_helper_neon_qunzip8(cpu_env, tmp, tmp2);
3656             break;
3657         case 1:
3658             gen_helper_neon_qunzip16(cpu_env, tmp, tmp2);
3659             break;
3660         case 2:
3661             gen_helper_neon_qunzip32(cpu_env, tmp, tmp2);
3662             break;
3663         default:
3664             abort();
3665         }
3666     } else {
3667         switch (size) {
3668         case 0:
3669             gen_helper_neon_unzip8(cpu_env, tmp, tmp2);
3670             break;
3671         case 1:
3672             gen_helper_neon_unzip16(cpu_env, tmp, tmp2);
3673             break;
3674         default:
3675             abort();
3676         }
3677     }
3678     tcg_temp_free_i32(tmp);
3679     tcg_temp_free_i32(tmp2);
3680     return 0;
3681 }
3682
3683 static int gen_neon_zip(int rd, int rm, int size, int q)
3684 {
3685     TCGv tmp, tmp2;
3686     if (!q && size == 2) {
3687         return 1;
3688     }
3689     tmp = tcg_const_i32(rd);
3690     tmp2 = tcg_const_i32(rm);
3691     if (q) {
3692         switch (size) {
3693         case 0:
3694             gen_helper_neon_qzip8(cpu_env, tmp, tmp2);
3695             break;
3696         case 1:
3697             gen_helper_neon_qzip16(cpu_env, tmp, tmp2);
3698             break;
3699         case 2:
3700             gen_helper_neon_qzip32(cpu_env, tmp, tmp2);
3701             break;
3702         default:
3703             abort();
3704         }
3705     } else {
3706         switch (size) {
3707         case 0:
3708             gen_helper_neon_zip8(cpu_env, tmp, tmp2);
3709             break;
3710         case 1:
3711             gen_helper_neon_zip16(cpu_env, tmp, tmp2);
3712             break;
3713         default:
3714             abort();
3715         }
3716     }
3717     tcg_temp_free_i32(tmp);
3718     tcg_temp_free_i32(tmp2);
3719     return 0;
3720 }
3721
3722 static void gen_neon_trn_u8(TCGv t0, TCGv t1)
3723 {
3724     TCGv rd, tmp;
3725
3726     rd = tcg_temp_new_i32();
3727     tmp = tcg_temp_new_i32();
3728
3729     tcg_gen_shli_i32(rd, t0, 8);
3730     tcg_gen_andi_i32(rd, rd, 0xff00ff00);
3731     tcg_gen_andi_i32(tmp, t1, 0x00ff00ff);
3732     tcg_gen_or_i32(rd, rd, tmp);
3733
3734     tcg_gen_shri_i32(t1, t1, 8);
3735     tcg_gen_andi_i32(t1, t1, 0x00ff00ff);
3736     tcg_gen_andi_i32(tmp, t0, 0xff00ff00);
3737     tcg_gen_or_i32(t1, t1, tmp);
3738     tcg_gen_mov_i32(t0, rd);
3739
3740     tcg_temp_free_i32(tmp);
3741     tcg_temp_free_i32(rd);
3742 }
3743
3744 static void gen_neon_trn_u16(TCGv t0, TCGv t1)
3745 {
3746     TCGv rd, tmp;
3747
3748     rd = tcg_temp_new_i32();
3749     tmp = tcg_temp_new_i32();
3750
3751     tcg_gen_shli_i32(rd, t0, 16);
3752     tcg_gen_andi_i32(tmp, t1, 0xffff);
3753     tcg_gen_or_i32(rd, rd, tmp);
3754     tcg_gen_shri_i32(t1, t1, 16);
3755     tcg_gen_andi_i32(tmp, t0, 0xffff0000);
3756     tcg_gen_or_i32(t1, t1, tmp);
3757     tcg_gen_mov_i32(t0, rd);
3758
3759     tcg_temp_free_i32(tmp);
3760     tcg_temp_free_i32(rd);
3761 }
3762
3763
3764 static struct {
3765     int nregs;
3766     int interleave;
3767     int spacing;
3768 } neon_ls_element_type[11] = {
3769     {4, 4, 1},
3770     {4, 4, 2},
3771     {4, 1, 1},
3772     {4, 2, 1},
3773     {3, 3, 1},
3774     {3, 3, 2},
3775     {3, 1, 1},
3776     {1, 1, 1},
3777     {2, 2, 1},
3778     {2, 2, 2},
3779     {2, 1, 1}
3780 };
3781
3782 /* Translate a NEON load/store element instruction.  Return nonzero if the
3783    instruction is invalid.  */
3784 static int disas_neon_ls_insn(CPUARMState * env, DisasContext *s, uint32_t insn)
3785 {
3786     int rd, rn, rm;
3787     int op;
3788     int nregs;
3789     int interleave;
3790     int spacing;
3791     int stride;
3792     int size;
3793     int reg;
3794     int pass;
3795     int load;
3796     int shift;
3797     int n;
3798     TCGv addr;
3799     TCGv tmp;
3800     TCGv tmp2;
3801     TCGv_i64 tmp64;
3802
3803     if (!s->vfp_enabled)
3804       return 1;
3805     VFP_DREG_D(rd, insn);
3806     rn = (insn >> 16) & 0xf;
3807     rm = insn & 0xf;
3808     load = (insn & (1 << 21)) != 0;
3809     if ((insn & (1 << 23)) == 0) {
3810         /* Load store all elements.  */
3811         op = (insn >> 8) & 0xf;
3812         size = (insn >> 6) & 3;
3813         if (op > 10)
3814             return 1;
3815         /* Catch UNDEF cases for bad values of align field */
3816         switch (op & 0xc) {
3817         case 4:
3818             if (((insn >> 5) & 1) == 1) {
3819                 return 1;
3820             }
3821             break;
3822         case 8:
3823             if (((insn >> 4) & 3) == 3) {
3824                 return 1;
3825             }
3826             break;
3827         default:
3828             break;
3829         }
3830         nregs = neon_ls_element_type[op].nregs;
3831         interleave = neon_ls_element_type[op].interleave;
3832         spacing = neon_ls_element_type[op].spacing;
3833         if (size == 3 && (interleave | spacing) != 1)
3834             return 1;
3835         addr = tcg_temp_new_i32();
3836         load_reg_var(s, addr, rn);
3837         stride = (1 << size) * interleave;
3838         for (reg = 0; reg < nregs; reg++) {
3839             if (interleave > 2 || (interleave == 2 && nregs == 2)) {
3840                 load_reg_var(s, addr, rn);
3841                 tcg_gen_addi_i32(addr, addr, (1 << size) * reg);
3842             } else if (interleave == 2 && nregs == 4 && reg == 2) {
3843                 load_reg_var(s, addr, rn);
3844                 tcg_gen_addi_i32(addr, addr, 1 << size);
3845             }
3846             if (size == 3) {
3847                 if (load) {
3848                     tmp64 = gen_ld64(addr, IS_USER(s));
3849                     neon_store_reg64(tmp64, rd);
3850                     tcg_temp_free_i64(tmp64);
3851                 } else {
3852                     tmp64 = tcg_temp_new_i64();
3853                     neon_load_reg64(tmp64, rd);
3854                     gen_st64(tmp64, addr, IS_USER(s));
3855                 }
3856                 tcg_gen_addi_i32(addr, addr, stride);
3857             } else {
3858                 for (pass = 0; pass < 2; pass++) {
3859                     if (size == 2) {
3860                         if (load) {
3861                             tmp = gen_ld32(addr, IS_USER(s));
3862                             neon_store_reg(rd, pass, tmp);
3863                         } else {
3864                             tmp = neon_load_reg(rd, pass);
3865                             gen_st32(tmp, addr, IS_USER(s));
3866                         }
3867                         tcg_gen_addi_i32(addr, addr, stride);
3868                     } else if (size == 1) {
3869                         if (load) {
3870                             tmp = gen_ld16u(addr, IS_USER(s));
3871                             tcg_gen_addi_i32(addr, addr, stride);
3872                             tmp2 = gen_ld16u(addr, IS_USER(s));
3873                             tcg_gen_addi_i32(addr, addr, stride);
3874                             tcg_gen_shli_i32(tmp2, tmp2, 16);
3875                             tcg_gen_or_i32(tmp, tmp, tmp2);
3876                             tcg_temp_free_i32(tmp2);
3877                             neon_store_reg(rd, pass, tmp);
3878                         } else {
3879                             tmp = neon_load_reg(rd, pass);
3880                             tmp2 = tcg_temp_new_i32();
3881                             tcg_gen_shri_i32(tmp2, tmp, 16);
3882                             gen_st16(tmp, addr, IS_USER(s));
3883                             tcg_gen_addi_i32(addr, addr, stride);
3884                             gen_st16(tmp2, addr, IS_USER(s));
3885                             tcg_gen_addi_i32(addr, addr, stride);
3886                         }
3887                     } else /* size == 0 */ {
3888                         if (load) {
3889                             TCGV_UNUSED(tmp2);
3890                             for (n = 0; n < 4; n++) {
3891                                 tmp = gen_ld8u(addr, IS_USER(s));
3892                                 tcg_gen_addi_i32(addr, addr, stride);
3893                                 if (n == 0) {
3894                                     tmp2 = tmp;
3895                                 } else {
3896                                     tcg_gen_shli_i32(tmp, tmp, n * 8);
3897                                     tcg_gen_or_i32(tmp2, tmp2, tmp);
3898                                     tcg_temp_free_i32(tmp);
3899                                 }
3900                             }
3901                             neon_store_reg(rd, pass, tmp2);
3902                         } else {
3903                             tmp2 = neon_load_reg(rd, pass);
3904                             for (n = 0; n < 4; n++) {
3905                                 tmp = tcg_temp_new_i32();
3906                                 if (n == 0) {
3907                                     tcg_gen_mov_i32(tmp, tmp2);
3908                                 } else {
3909                                     tcg_gen_shri_i32(tmp, tmp2, n * 8);
3910                                 }
3911                                 gen_st8(tmp, addr, IS_USER(s));
3912                                 tcg_gen_addi_i32(addr, addr, stride);
3913                             }
3914                             tcg_temp_free_i32(tmp2);
3915                         }
3916                     }
3917                 }
3918             }
3919             rd += spacing;
3920         }
3921         tcg_temp_free_i32(addr);
3922         stride = nregs * 8;
3923     } else {
3924         size = (insn >> 10) & 3;
3925         if (size == 3) {
3926             /* Load single element to all lanes.  */
3927             int a = (insn >> 4) & 1;
3928             if (!load) {
3929                 return 1;
3930             }
3931             size = (insn >> 6) & 3;
3932             nregs = ((insn >> 8) & 3) + 1;
3933
3934             if (size == 3) {
3935                 if (nregs != 4 || a == 0) {
3936                     return 1;
3937                 }
3938                 /* For VLD4 size==3 a == 1 means 32 bits at 16 byte alignment */
3939                 size = 2;
3940             }
3941             if (nregs == 1 && a == 1 && size == 0) {
3942                 return 1;
3943             }
3944             if (nregs == 3 && a == 1) {
3945                 return 1;
3946             }
3947             addr = tcg_temp_new_i32();
3948             load_reg_var(s, addr, rn);
3949             if (nregs == 1) {
3950                 /* VLD1 to all lanes: bit 5 indicates how many Dregs to write */
3951                 tmp = gen_load_and_replicate(s, addr, size);
3952                 tcg_gen_st_i32(tmp, cpu_env, neon_reg_offset(rd, 0));
3953                 tcg_gen_st_i32(tmp, cpu_env, neon_reg_offset(rd, 1));
3954                 if (insn & (1 << 5)) {
3955                     tcg_gen_st_i32(tmp, cpu_env, neon_reg_offset(rd + 1, 0));
3956                     tcg_gen_st_i32(tmp, cpu_env, neon_reg_offset(rd + 1, 1));
3957                 }
3958                 tcg_temp_free_i32(tmp);
3959             } else {
3960                 /* VLD2/3/4 to all lanes: bit 5 indicates register stride */
3961                 stride = (insn & (1 << 5)) ? 2 : 1;
3962                 for (reg = 0; reg < nregs; reg++) {
3963                     tmp = gen_load_and_replicate(s, addr, size);
3964                     tcg_gen_st_i32(tmp, cpu_env, neon_reg_offset(rd, 0));
3965                     tcg_gen_st_i32(tmp, cpu_env, neon_reg_offset(rd, 1));
3966                     tcg_temp_free_i32(tmp);
3967                     tcg_gen_addi_i32(addr, addr, 1 << size);
3968                     rd += stride;
3969                 }
3970             }
3971             tcg_temp_free_i32(addr);
3972             stride = (1 << size) * nregs;
3973         } else {
3974             /* Single element.  */
3975             int idx = (insn >> 4) & 0xf;
3976             pass = (insn >> 7) & 1;
3977             switch (size) {
3978             case 0:
3979                 shift = ((insn >> 5) & 3) * 8;
3980                 stride = 1;
3981                 break;
3982             case 1:
3983                 shift = ((insn >> 6) & 1) * 16;
3984                 stride = (insn & (1 << 5)) ? 2 : 1;
3985                 break;
3986             case 2:
3987                 shift = 0;
3988                 stride = (insn & (1 << 6)) ? 2 : 1;
3989                 break;
3990             default:
3991                 abort();
3992             }
3993             nregs = ((insn >> 8) & 3) + 1;
3994             /* Catch the UNDEF cases. This is unavoidably a bit messy. */
3995             switch (nregs) {
3996             case 1:
3997                 if (((idx & (1 << size)) != 0) ||
3998                     (size == 2 && ((idx & 3) == 1 || (idx & 3) == 2))) {
3999                     return 1;
4000                 }
4001                 break;
4002             case 3:
4003                 if ((idx & 1) != 0) {
4004                     return 1;
4005                 }
4006                 /* fall through */
4007             case 2:
4008                 if (size == 2 && (idx & 2) != 0) {
4009                     return 1;
4010                 }
4011                 break;
4012             case 4:
4013                 if ((size == 2) && ((idx & 3) == 3)) {
4014                     return 1;
4015                 }
4016                 break;
4017             default:
4018                 abort();
4019             }
4020             if ((rd + stride * (nregs - 1)) > 31) {
4021                 /* Attempts to write off the end of the register file
4022                  * are UNPREDICTABLE; we choose to UNDEF because otherwise
4023                  * the neon_load_reg() would write off the end of the array.
4024                  */
4025                 return 1;
4026             }
4027             addr = tcg_temp_new_i32();
4028             load_reg_var(s, addr, rn);
4029             for (reg = 0; reg < nregs; reg++) {
4030                 if (load) {
4031                     switch (size) {
4032                     case 0:
4033                         tmp = gen_ld8u(addr, IS_USER(s));
4034                         break;
4035                     case 1:
4036                         tmp = gen_ld16u(addr, IS_USER(s));
4037                         break;
4038                     case 2:
4039                         tmp = gen_ld32(addr, IS_USER(s));
4040                         break;
4041                     default: /* Avoid compiler warnings.  */
4042                         abort();
4043                     }
4044                     if (size != 2) {
4045                         tmp2 = neon_load_reg(rd, pass);
4046                         gen_bfi(tmp, tmp2, tmp, shift, size ? 0xffff : 0xff);
4047                         tcg_temp_free_i32(tmp2);
4048                     }
4049                     neon_store_reg(rd, pass, tmp);
4050                 } else { /* Store */
4051                     tmp = neon_load_reg(rd, pass);
4052                     if (shift)
4053                         tcg_gen_shri_i32(tmp, tmp, shift);
4054                     switch (size) {
4055                     case 0:
4056                         gen_st8(tmp, addr, IS_USER(s));
4057                         break;
4058                     case 1:
4059                         gen_st16(tmp, addr, IS_USER(s));
4060                         break;
4061                     case 2:
4062                         gen_st32(tmp, addr, IS_USER(s));
4063                         break;
4064                     }
4065                 }
4066                 rd += stride;
4067                 tcg_gen_addi_i32(addr, addr, 1 << size);
4068             }
4069             tcg_temp_free_i32(addr);
4070             stride = nregs * (1 << size);
4071         }
4072     }
4073     if (rm != 15) {
4074         TCGv base;
4075
4076         base = load_reg(s, rn);
4077         if (rm == 13) {
4078             tcg_gen_addi_i32(base, base, stride);
4079         } else {
4080             TCGv index;
4081             index = load_reg(s, rm);
4082             tcg_gen_add_i32(base, base, index);
4083             tcg_temp_free_i32(index);
4084         }
4085         store_reg(s, rn, base);
4086     }
4087     return 0;
4088 }
4089
4090 /* Bitwise select.  dest = c ? t : f.  Clobbers T and F.  */
4091 static void gen_neon_bsl(TCGv dest, TCGv t, TCGv f, TCGv c)
4092 {
4093     tcg_gen_and_i32(t, t, c);
4094     tcg_gen_andc_i32(f, f, c);
4095     tcg_gen_or_i32(dest, t, f);
4096 }
4097
4098 static inline void gen_neon_narrow(int size, TCGv dest, TCGv_i64 src)
4099 {
4100     switch (size) {
4101     case 0: gen_helper_neon_narrow_u8(dest, src); break;
4102     case 1: gen_helper_neon_narrow_u16(dest, src); break;
4103     case 2: tcg_gen_trunc_i64_i32(dest, src); break;
4104     default: abort();
4105     }
4106 }
4107
4108 static inline void gen_neon_narrow_sats(int size, TCGv dest, TCGv_i64 src)
4109 {
4110     switch (size) {
4111     case 0: gen_helper_neon_narrow_sat_s8(dest, cpu_env, src); break;
4112     case 1: gen_helper_neon_narrow_sat_s16(dest, cpu_env, src); break;
4113     case 2: gen_helper_neon_narrow_sat_s32(dest, cpu_env, src); break;
4114     default: abort();
4115     }
4116 }
4117
4118 static inline void gen_neon_narrow_satu(int size, TCGv dest, TCGv_i64 src)
4119 {
4120     switch (size) {
4121     case 0: gen_helper_neon_narrow_sat_u8(dest, cpu_env, src); break;
4122     case 1: gen_helper_neon_narrow_sat_u16(dest, cpu_env, src); break;
4123     case 2: gen_helper_neon_narrow_sat_u32(dest, cpu_env, src); break;
4124     default: abort();
4125     }
4126 }
4127
4128 static inline void gen_neon_unarrow_sats(int size, TCGv dest, TCGv_i64 src)
4129 {
4130     switch (size) {
4131     case 0: gen_helper_neon_unarrow_sat8(dest, cpu_env, src); break;
4132     case 1: gen_helper_neon_unarrow_sat16(dest, cpu_env, src); break;
4133     case 2: gen_helper_neon_unarrow_sat32(dest, cpu_env, src); break;
4134     default: abort();
4135     }
4136 }
4137
4138 static inline void gen_neon_shift_narrow(int size, TCGv var, TCGv shift,
4139                                          int q, int u)
4140 {
4141     if (q) {
4142         if (u) {
4143             switch (size) {
4144             case 1: gen_helper_neon_rshl_u16(var, var, shift); break;
4145             case 2: gen_helper_neon_rshl_u32(var, var, shift); break;
4146             default: abort();
4147             }
4148         } else {
4149             switch (size) {
4150             case 1: gen_helper_neon_rshl_s16(var, var, shift); break;
4151             case 2: gen_helper_neon_rshl_s32(var, var, shift); break;
4152             default: abort();
4153             }
4154         }
4155     } else {
4156         if (u) {
4157             switch (size) {
4158             case 1: gen_helper_neon_shl_u16(var, var, shift); break;
4159             case 2: gen_helper_neon_shl_u32(var, var, shift); break;
4160             default: abort();
4161             }
4162         } else {
4163             switch (size) {
4164             case 1: gen_helper_neon_shl_s16(var, var, shift); break;
4165             case 2: gen_helper_neon_shl_s32(var, var, shift); break;
4166             default: abort();
4167             }
4168         }
4169     }
4170 }
4171
4172 static inline void gen_neon_widen(TCGv_i64 dest, TCGv src, int size, int u)
4173 {
4174     if (u) {
4175         switch (size) {
4176         case 0: gen_helper_neon_widen_u8(dest, src); break;
4177         case 1: gen_helper_neon_widen_u16(dest, src); break;
4178         case 2: tcg_gen_extu_i32_i64(dest, src); break;
4179         default: abort();
4180         }
4181     } else {
4182         switch (size) {
4183         case 0: gen_helper_neon_widen_s8(dest, src); break;
4184         case 1: gen_helper_neon_widen_s16(dest, src); break;
4185         case 2: tcg_gen_ext_i32_i64(dest, src); break;
4186         default: abort();
4187         }
4188     }
4189     tcg_temp_free_i32(src);
4190 }
4191
4192 static inline void gen_neon_addl(int size)
4193 {
4194     switch (size) {
4195     case 0: gen_helper_neon_addl_u16(CPU_V001); break;
4196     case 1: gen_helper_neon_addl_u32(CPU_V001); break;
4197     case 2: tcg_gen_add_i64(CPU_V001); break;
4198     default: abort();
4199     }
4200 }
4201
4202 static inline void gen_neon_subl(int size)
4203 {
4204     switch (size) {
4205     case 0: gen_helper_neon_subl_u16(CPU_V001); break;
4206     case 1: gen_helper_neon_subl_u32(CPU_V001); break;
4207     case 2: tcg_gen_sub_i64(CPU_V001); break;
4208     default: abort();
4209     }
4210 }
4211
4212 static inline void gen_neon_negl(TCGv_i64 var, int size)
4213 {
4214     switch (size) {
4215     case 0: gen_helper_neon_negl_u16(var, var); break;
4216     case 1: gen_helper_neon_negl_u32(var, var); break;
4217     case 2: gen_helper_neon_negl_u64(var, var); break;
4218     default: abort();
4219     }
4220 }
4221
4222 static inline void gen_neon_addl_saturate(TCGv_i64 op0, TCGv_i64 op1, int size)
4223 {
4224     switch (size) {
4225     case 1: gen_helper_neon_addl_saturate_s32(op0, cpu_env, op0, op1); break;
4226     case 2: gen_helper_neon_addl_saturate_s64(op0, cpu_env, op0, op1); break;
4227     default: abort();
4228     }
4229 }
4230
4231 static inline void gen_neon_mull(TCGv_i64 dest, TCGv a, TCGv b, int size, int u)
4232 {
4233     TCGv_i64 tmp;
4234
4235     switch ((size << 1) | u) {
4236     case 0: gen_helper_neon_mull_s8(dest, a, b); break;
4237     case 1: gen_helper_neon_mull_u8(dest, a, b); break;
4238     case 2: gen_helper_neon_mull_s16(dest, a, b); break;
4239     case 3: gen_helper_neon_mull_u16(dest, a, b); break;
4240     case 4:
4241         tmp = gen_muls_i64_i32(a, b);
4242         tcg_gen_mov_i64(dest, tmp);
4243         tcg_temp_free_i64(tmp);
4244         break;
4245     case 5:
4246         tmp = gen_mulu_i64_i32(a, b);
4247         tcg_gen_mov_i64(dest, tmp);
4248         tcg_temp_free_i64(tmp);
4249         break;
4250     default: abort();
4251     }
4252
4253     /* gen_helper_neon_mull_[su]{8|16} do not free their parameters.
4254        Don't forget to clean them now.  */
4255     if (size < 2) {
4256         tcg_temp_free_i32(a);
4257         tcg_temp_free_i32(b);
4258     }
4259 }
4260
4261 static void gen_neon_narrow_op(int op, int u, int size, TCGv dest, TCGv_i64 src)
4262 {
4263     if (op) {
4264         if (u) {
4265             gen_neon_unarrow_sats(size, dest, src);
4266         } else {
4267             gen_neon_narrow(size, dest, src);
4268         }
4269     } else {
4270         if (u) {
4271             gen_neon_narrow_satu(size, dest, src);
4272         } else {
4273             gen_neon_narrow_sats(size, dest, src);
4274         }
4275     }
4276 }
4277
4278 /* Symbolic constants for op fields for Neon 3-register same-length.
4279  * The values correspond to bits [11:8,4]; see the ARM ARM DDI0406B
4280  * table A7-9.
4281  */
4282 #define NEON_3R_VHADD 0
4283 #define NEON_3R_VQADD 1
4284 #define NEON_3R_VRHADD 2
4285 #define NEON_3R_LOGIC 3 /* VAND,VBIC,VORR,VMOV,VORN,VEOR,VBIF,VBIT,VBSL */
4286 #define NEON_3R_VHSUB 4
4287 #define NEON_3R_VQSUB 5
4288 #define NEON_3R_VCGT 6
4289 #define NEON_3R_VCGE 7
4290 #define NEON_3R_VSHL 8
4291 #define NEON_3R_VQSHL 9
4292 #define NEON_3R_VRSHL 10
4293 #define NEON_3R_VQRSHL 11
4294 #define NEON_3R_VMAX 12
4295 #define NEON_3R_VMIN 13
4296 #define NEON_3R_VABD 14
4297 #define NEON_3R_VABA 15
4298 #define NEON_3R_VADD_VSUB 16
4299 #define NEON_3R_VTST_VCEQ 17
4300 #define NEON_3R_VML 18 /* VMLA, VMLAL, VMLS, VMLSL */
4301 #define NEON_3R_VMUL 19
4302 #define NEON_3R_VPMAX 20
4303 #define NEON_3R_VPMIN 21
4304 #define NEON_3R_VQDMULH_VQRDMULH 22
4305 #define NEON_3R_VPADD 23
4306 #define NEON_3R_VFM 25 /* VFMA, VFMS : float fused multiply-add */
4307 #define NEON_3R_FLOAT_ARITH 26 /* float VADD, VSUB, VPADD, VABD */
4308 #define NEON_3R_FLOAT_MULTIPLY 27 /* float VMLA, VMLS, VMUL */
4309 #define NEON_3R_FLOAT_CMP 28 /* float VCEQ, VCGE, VCGT */
4310 #define NEON_3R_FLOAT_ACMP 29 /* float VACGE, VACGT, VACLE, VACLT */
4311 #define NEON_3R_FLOAT_MINMAX 30 /* float VMIN, VMAX */
4312 #define NEON_3R_VRECPS_VRSQRTS 31 /* float VRECPS, VRSQRTS */
4313
4314 static const uint8_t neon_3r_sizes[] = {
4315     [NEON_3R_VHADD] = 0x7,
4316     [NEON_3R_VQADD] = 0xf,
4317     [NEON_3R_VRHADD] = 0x7,
4318     [NEON_3R_LOGIC] = 0xf, /* size field encodes op type */
4319     [NEON_3R_VHSUB] = 0x7,
4320     [NEON_3R_VQSUB] = 0xf,
4321     [NEON_3R_VCGT] = 0x7,
4322     [NEON_3R_VCGE] = 0x7,
4323     [NEON_3R_VSHL] = 0xf,
4324     [NEON_3R_VQSHL] = 0xf,
4325     [NEON_3R_VRSHL] = 0xf,
4326     [NEON_3R_VQRSHL] = 0xf,
4327     [NEON_3R_VMAX] = 0x7,
4328     [NEON_3R_VMIN] = 0x7,
4329     [NEON_3R_VABD] = 0x7,
4330     [NEON_3R_VABA] = 0x7,
4331     [NEON_3R_VADD_VSUB] = 0xf,
4332     [NEON_3R_VTST_VCEQ] = 0x7,
4333     [NEON_3R_VML] = 0x7,
4334     [NEON_3R_VMUL] = 0x7,
4335     [NEON_3R_VPMAX] = 0x7,
4336     [NEON_3R_VPMIN] = 0x7,
4337     [NEON_3R_VQDMULH_VQRDMULH] = 0x6,
4338     [NEON_3R_VPADD] = 0x7,
4339     [NEON_3R_VFM] = 0x5, /* size bit 1 encodes op */
4340     [NEON_3R_FLOAT_ARITH] = 0x5, /* size bit 1 encodes op */
4341     [NEON_3R_FLOAT_MULTIPLY] = 0x5, /* size bit 1 encodes op */
4342     [NEON_3R_FLOAT_CMP] = 0x5, /* size bit 1 encodes op */
4343     [NEON_3R_FLOAT_ACMP] = 0x5, /* size bit 1 encodes op */
4344     [NEON_3R_FLOAT_MINMAX] = 0x5, /* size bit 1 encodes op */
4345     [NEON_3R_VRECPS_VRSQRTS] = 0x5, /* size bit 1 encodes op */
4346 };
4347
4348 /* Symbolic constants for op fields for Neon 2-register miscellaneous.
4349  * The values correspond to bits [17:16,10:7]; see the ARM ARM DDI0406B
4350  * table A7-13.
4351  */
4352 #define NEON_2RM_VREV64 0
4353 #define NEON_2RM_VREV32 1
4354 #define NEON_2RM_VREV16 2
4355 #define NEON_2RM_VPADDL 4
4356 #define NEON_2RM_VPADDL_U 5
4357 #define NEON_2RM_VCLS 8
4358 #define NEON_2RM_VCLZ 9
4359 #define NEON_2RM_VCNT 10
4360 #define NEON_2RM_VMVN 11
4361 #define NEON_2RM_VPADAL 12
4362 #define NEON_2RM_VPADAL_U 13
4363 #define NEON_2RM_VQABS 14
4364 #define NEON_2RM_VQNEG 15
4365 #define NEON_2RM_VCGT0 16
4366 #define NEON_2RM_VCGE0 17
4367 #define NEON_2RM_VCEQ0 18
4368 #define NEON_2RM_VCLE0 19
4369 #define NEON_2RM_VCLT0 20
4370 #define NEON_2RM_VABS 22
4371 #define NEON_2RM_VNEG 23
4372 #define NEON_2RM_VCGT0_F 24
4373 #define NEON_2RM_VCGE0_F 25
4374 #define NEON_2RM_VCEQ0_F 26
4375 #define NEON_2RM_VCLE0_F 27
4376 #define NEON_2RM_VCLT0_F 28
4377 #define NEON_2RM_VABS_F 30
4378 #define NEON_2RM_VNEG_F 31
4379 #define NEON_2RM_VSWP 32
4380 #define NEON_2RM_VTRN 33
4381 #define NEON_2RM_VUZP 34
4382 #define NEON_2RM_VZIP 35
4383 #define NEON_2RM_VMOVN 36 /* Includes VQMOVN, VQMOVUN */
4384 #define NEON_2RM_VQMOVN 37 /* Includes VQMOVUN */
4385 #define NEON_2RM_VSHLL 38
4386 #define NEON_2RM_VCVT_F16_F32 44
4387 #define NEON_2RM_VCVT_F32_F16 46
4388 #define NEON_2RM_VRECPE 56
4389 #define NEON_2RM_VRSQRTE 57
4390 #define NEON_2RM_VRECPE_F 58
4391 #define NEON_2RM_VRSQRTE_F 59
4392 #define NEON_2RM_VCVT_FS 60
4393 #define NEON_2RM_VCVT_FU 61
4394 #define NEON_2RM_VCVT_SF 62
4395 #define NEON_2RM_VCVT_UF 63
4396
4397 static int neon_2rm_is_float_op(int op)
4398 {
4399     /* Return true if this neon 2reg-misc op is float-to-float */
4400     return (op == NEON_2RM_VABS_F || op == NEON_2RM_VNEG_F ||
4401             op >= NEON_2RM_VRECPE_F);
4402 }
4403
4404 /* Each entry in this array has bit n set if the insn allows
4405  * size value n (otherwise it will UNDEF). Since unallocated
4406  * op values will have no bits set they always UNDEF.
4407  */
4408 static const uint8_t neon_2rm_sizes[] = {
4409     [NEON_2RM_VREV64] = 0x7,
4410     [NEON_2RM_VREV32] = 0x3,
4411     [NEON_2RM_VREV16] = 0x1,
4412     [NEON_2RM_VPADDL] = 0x7,
4413     [NEON_2RM_VPADDL_U] = 0x7,
4414     [NEON_2RM_VCLS] = 0x7,
4415     [NEON_2RM_VCLZ] = 0x7,
4416     [NEON_2RM_VCNT] = 0x1,
4417     [NEON_2RM_VMVN] = 0x1,
4418     [NEON_2RM_VPADAL] = 0x7,
4419     [NEON_2RM_VPADAL_U] = 0x7,
4420     [NEON_2RM_VQABS] = 0x7,
4421     [NEON_2RM_VQNEG] = 0x7,
4422     [NEON_2RM_VCGT0] = 0x7,
4423     [NEON_2RM_VCGE0] = 0x7,
4424     [NEON_2RM_VCEQ0] = 0x7,
4425     [NEON_2RM_VCLE0] = 0x7,
4426     [NEON_2RM_VCLT0] = 0x7,
4427     [NEON_2RM_VABS] = 0x7,
4428     [NEON_2RM_VNEG] = 0x7,
4429     [NEON_2RM_VCGT0_F] = 0x4,
4430     [NEON_2RM_VCGE0_F] = 0x4,
4431     [NEON_2RM_VCEQ0_F] = 0x4,
4432     [NEON_2RM_VCLE0_F] = 0x4,
4433     [NEON_2RM_VCLT0_F] = 0x4,
4434     [NEON_2RM_VABS_F] = 0x4,
4435     [NEON_2RM_VNEG_F] = 0x4,
4436     [NEON_2RM_VSWP] = 0x1,
4437     [NEON_2RM_VTRN] = 0x7,
4438     [NEON_2RM_VUZP] = 0x7,
4439     [NEON_2RM_VZIP] = 0x7,
4440     [NEON_2RM_VMOVN] = 0x7,
4441     [NEON_2RM_VQMOVN] = 0x7,
4442     [NEON_2RM_VSHLL] = 0x7,
4443     [NEON_2RM_VCVT_F16_F32] = 0x2,
4444     [NEON_2RM_VCVT_F32_F16] = 0x2,
4445     [NEON_2RM_VRECPE] = 0x4,
4446     [NEON_2RM_VRSQRTE] = 0x4,
4447     [NEON_2RM_VRECPE_F] = 0x4,
4448     [NEON_2RM_VRSQRTE_F] = 0x4,
4449     [NEON_2RM_VCVT_FS] = 0x4,
4450     [NEON_2RM_VCVT_FU] = 0x4,
4451     [NEON_2RM_VCVT_SF] = 0x4,
4452     [NEON_2RM_VCVT_UF] = 0x4,
4453 };
4454
4455 /* Translate a NEON data processing instruction.  Return nonzero if the
4456    instruction is invalid.
4457    We process data in a mixture of 32-bit and 64-bit chunks.
4458    Mostly we use 32-bit chunks so we can use normal scalar instructions.  */
4459
4460 static int disas_neon_data_insn(CPUARMState * env, DisasContext *s, uint32_t insn)
4461 {
4462     int op;
4463     int q;
4464     int rd, rn, rm;
4465     int size;
4466     int shift;
4467     int pass;
4468     int count;
4469     int pairwise;
4470     int u;
4471     uint32_t imm, mask;
4472     TCGv tmp, tmp2, tmp3, tmp4, tmp5;
4473     TCGv_i64 tmp64;
4474
4475     if (!s->vfp_enabled)
4476       return 1;
4477     q = (insn & (1 << 6)) != 0;
4478     u = (insn >> 24) & 1;
4479     VFP_DREG_D(rd, insn);
4480     VFP_DREG_N(rn, insn);
4481     VFP_DREG_M(rm, insn);
4482     size = (insn >> 20) & 3;
4483     if ((insn & (1 << 23)) == 0) {
4484         /* Three register same length.  */
4485         op = ((insn >> 7) & 0x1e) | ((insn >> 4) & 1);
4486         /* Catch invalid op and bad size combinations: UNDEF */
4487         if ((neon_3r_sizes[op] & (1 << size)) == 0) {
4488             return 1;
4489         }
4490         /* All insns of this form UNDEF for either this condition or the
4491          * superset of cases "Q==1"; we catch the latter later.
4492          */
4493         if (q && ((rd | rn | rm) & 1)) {
4494             return 1;
4495         }
4496         if (size == 3 && op != NEON_3R_LOGIC) {
4497             /* 64-bit element instructions. */
4498             for (pass = 0; pass < (q ? 2 : 1); pass++) {
4499                 neon_load_reg64(cpu_V0, rn + pass);
4500                 neon_load_reg64(cpu_V1, rm + pass);
4501                 switch (op) {
4502                 case NEON_3R_VQADD:
4503                     if (u) {
4504                         gen_helper_neon_qadd_u64(cpu_V0, cpu_env,
4505                                                  cpu_V0, cpu_V1);
4506                     } else {
4507                         gen_helper_neon_qadd_s64(cpu_V0, cpu_env,
4508                                                  cpu_V0, cpu_V1);
4509                     }
4510                     break;
4511                 case NEON_3R_VQSUB:
4512                     if (u) {
4513                         gen_helper_neon_qsub_u64(cpu_V0, cpu_env,
4514                                                  cpu_V0, cpu_V1);
4515                     } else {
4516                         gen_helper_neon_qsub_s64(cpu_V0, cpu_env,
4517                                                  cpu_V0, cpu_V1);
4518                     }
4519                     break;
4520                 case NEON_3R_VSHL:
4521                     if (u) {
4522                         gen_helper_neon_shl_u64(cpu_V0, cpu_V1, cpu_V0);
4523                     } else {
4524                         gen_helper_neon_shl_s64(cpu_V0, cpu_V1, cpu_V0);
4525                     }
4526                     break;
4527                 case NEON_3R_VQSHL:
4528                     if (u) {
4529                         gen_helper_neon_qshl_u64(cpu_V0, cpu_env,
4530                                                  cpu_V1, cpu_V0);
4531                     } else {
4532                         gen_helper_neon_qshl_s64(cpu_V0, cpu_env,
4533                                                  cpu_V1, cpu_V0);
4534                     }
4535                     break;
4536                 case NEON_3R_VRSHL:
4537                     if (u) {
4538                         gen_helper_neon_rshl_u64(cpu_V0, cpu_V1, cpu_V0);
4539                     } else {
4540                         gen_helper_neon_rshl_s64(cpu_V0, cpu_V1, cpu_V0);
4541                     }
4542                     break;
4543                 case NEON_3R_VQRSHL:
4544                     if (u) {
4545                         gen_helper_neon_qrshl_u64(cpu_V0, cpu_env,
4546                                                   cpu_V1, cpu_V0);
4547                     } else {
4548                         gen_helper_neon_qrshl_s64(cpu_V0, cpu_env,
4549                                                   cpu_V1, cpu_V0);
4550                     }
4551                     break;
4552                 case NEON_3R_VADD_VSUB:
4553                     if (u) {
4554                         tcg_gen_sub_i64(CPU_V001);
4555                     } else {
4556                         tcg_gen_add_i64(CPU_V001);
4557                     }
4558                     break;
4559                 default:
4560                     abort();
4561                 }
4562                 neon_store_reg64(cpu_V0, rd + pass);
4563             }
4564             return 0;
4565         }
4566         pairwise = 0;
4567         switch (op) {
4568         case NEON_3R_VSHL:
4569         case NEON_3R_VQSHL:
4570         case NEON_3R_VRSHL:
4571         case NEON_3R_VQRSHL:
4572             {
4573                 int rtmp;
4574                 /* Shift instruction operands are reversed.  */
4575                 rtmp = rn;
4576                 rn = rm;
4577                 rm = rtmp;
4578             }
4579             break;
4580         case NEON_3R_VPADD:
4581             if (u) {
4582                 return 1;
4583             }
4584             /* Fall through */
4585         case NEON_3R_VPMAX:
4586         case NEON_3R_VPMIN:
4587             pairwise = 1;
4588             break;
4589         case NEON_3R_FLOAT_ARITH:
4590             pairwise = (u && size < 2); /* if VPADD (float) */
4591             break;
4592         case NEON_3R_FLOAT_MINMAX:
4593             pairwise = u; /* if VPMIN/VPMAX (float) */
4594             break;
4595         case NEON_3R_FLOAT_CMP:
4596             if (!u && size) {
4597                 /* no encoding for U=0 C=1x */
4598                 return 1;
4599             }
4600             break;
4601         case NEON_3R_FLOAT_ACMP:
4602             if (!u) {
4603                 return 1;
4604             }
4605             break;
4606         case NEON_3R_VRECPS_VRSQRTS:
4607             if (u) {
4608                 return 1;
4609             }
4610             break;
4611         case NEON_3R_VMUL:
4612             if (u && (size != 0)) {
4613                 /* UNDEF on invalid size for polynomial subcase */
4614                 return 1;
4615             }
4616             break;
4617         case NEON_3R_VFM:
4618             if (!arm_feature(env, ARM_FEATURE_VFP4) || u) {
4619                 return 1;
4620             }
4621             break;
4622         default:
4623             break;
4624         }
4625
4626         if (pairwise && q) {
4627             /* All the pairwise insns UNDEF if Q is set */
4628             return 1;
4629         }
4630
4631         for (pass = 0; pass < (q ? 4 : 2); pass++) {
4632
4633         if (pairwise) {
4634             /* Pairwise.  */
4635             if (pass < 1) {
4636                 tmp = neon_load_reg(rn, 0);
4637                 tmp2 = neon_load_reg(rn, 1);
4638             } else {
4639                 tmp = neon_load_reg(rm, 0);
4640                 tmp2 = neon_load_reg(rm, 1);
4641             }
4642         } else {
4643             /* Elementwise.  */
4644             tmp = neon_load_reg(rn, pass);
4645             tmp2 = neon_load_reg(rm, pass);
4646         }
4647         switch (op) {
4648         case NEON_3R_VHADD:
4649             GEN_NEON_INTEGER_OP(hadd);
4650             break;
4651         case NEON_3R_VQADD:
4652             GEN_NEON_INTEGER_OP_ENV(qadd);
4653             break;
4654         case NEON_3R_VRHADD:
4655             GEN_NEON_INTEGER_OP(rhadd);
4656             break;
4657         case NEON_3R_LOGIC: /* Logic ops.  */
4658             switch ((u << 2) | size) {
4659             case 0: /* VAND */
4660                 tcg_gen_and_i32(tmp, tmp, tmp2);
4661                 break;
4662             case 1: /* BIC */
4663                 tcg_gen_andc_i32(tmp, tmp, tmp2);
4664                 break;
4665             case 2: /* VORR */
4666                 tcg_gen_or_i32(tmp, tmp, tmp2);
4667                 break;
4668             case 3: /* VORN */
4669                 tcg_gen_orc_i32(tmp, tmp, tmp2);
4670                 break;
4671             case 4: /* VEOR */
4672                 tcg_gen_xor_i32(tmp, tmp, tmp2);
4673                 break;
4674             case 5: /* VBSL */
4675                 tmp3 = neon_load_reg(rd, pass);
4676                 gen_neon_bsl(tmp, tmp, tmp2, tmp3);
4677                 tcg_temp_free_i32(tmp3);
4678                 break;
4679             case 6: /* VBIT */
4680                 tmp3 = neon_load_reg(rd, pass);
4681                 gen_neon_bsl(tmp, tmp, tmp3, tmp2);
4682                 tcg_temp_free_i32(tmp3);
4683                 break;
4684             case 7: /* VBIF */
4685                 tmp3 = neon_load_reg(rd, pass);
4686                 gen_neon_bsl(tmp, tmp3, tmp, tmp2);
4687                 tcg_temp_free_i32(tmp3);
4688                 break;
4689             }
4690             break;
4691         case NEON_3R_VHSUB:
4692             GEN_NEON_INTEGER_OP(hsub);
4693             break;
4694         case NEON_3R_VQSUB:
4695             GEN_NEON_INTEGER_OP_ENV(qsub);
4696             break;
4697         case NEON_3R_VCGT:
4698             GEN_NEON_INTEGER_OP(cgt);
4699             break;
4700         case NEON_3R_VCGE:
4701             GEN_NEON_INTEGER_OP(cge);
4702             break;
4703         case NEON_3R_VSHL:
4704             GEN_NEON_INTEGER_OP(shl);
4705             break;
4706         case NEON_3R_VQSHL:
4707             GEN_NEON_INTEGER_OP_ENV(qshl);
4708             break;
4709         case NEON_3R_VRSHL:
4710             GEN_NEON_INTEGER_OP(rshl);
4711             break;
4712         case NEON_3R_VQRSHL:
4713             GEN_NEON_INTEGER_OP_ENV(qrshl);
4714             break;
4715         case NEON_3R_VMAX:
4716             GEN_NEON_INTEGER_OP(max);
4717             break;
4718         case NEON_3R_VMIN:
4719             GEN_NEON_INTEGER_OP(min);
4720             break;
4721         case NEON_3R_VABD:
4722             GEN_NEON_INTEGER_OP(abd);
4723             break;
4724         case NEON_3R_VABA:
4725             GEN_NEON_INTEGER_OP(abd);
4726             tcg_temp_free_i32(tmp2);
4727             tmp2 = neon_load_reg(rd, pass);
4728             gen_neon_add(size, tmp, tmp2);
4729             break;
4730         case NEON_3R_VADD_VSUB:
4731             if (!u) { /* VADD */
4732                 gen_neon_add(size, tmp, tmp2);
4733             } else { /* VSUB */
4734                 switch (size) {
4735                 case 0: gen_helper_neon_sub_u8(tmp, tmp, tmp2); break;
4736                 case 1: gen_helper_neon_sub_u16(tmp, tmp, tmp2); break;
4737                 case 2: tcg_gen_sub_i32(tmp, tmp, tmp2); break;
4738                 default: abort();
4739                 }
4740             }
4741             break;
4742         case NEON_3R_VTST_VCEQ:
4743             if (!u) { /* VTST */
4744                 switch (size) {
4745                 case 0: gen_helper_neon_tst_u8(tmp, tmp, tmp2); break;
4746                 case 1: gen_helper_neon_tst_u16(tmp, tmp, tmp2); break;
4747                 case 2: gen_helper_neon_tst_u32(tmp, tmp, tmp2); break;
4748                 default: abort();
4749                 }
4750             } else { /* VCEQ */
4751                 switch (size) {
4752                 case 0: gen_helper_neon_ceq_u8(tmp, tmp, tmp2); break;
4753                 case 1: gen_helper_neon_ceq_u16(tmp, tmp, tmp2); break;
4754                 case 2: gen_helper_neon_ceq_u32(tmp, tmp, tmp2); break;
4755                 default: abort();
4756                 }
4757             }
4758             break;
4759         case NEON_3R_VML: /* VMLA, VMLAL, VMLS,VMLSL */
4760             switch (size) {
4761             case 0: gen_helper_neon_mul_u8(tmp, tmp, tmp2); break;
4762             case 1: gen_helper_neon_mul_u16(tmp, tmp, tmp2); break;
4763             case 2: tcg_gen_mul_i32(tmp, tmp, tmp2); break;
4764             default: abort();
4765             }
4766             tcg_temp_free_i32(tmp2);
4767             tmp2 = neon_load_reg(rd, pass);
4768             if (u) { /* VMLS */
4769                 gen_neon_rsb(size, tmp, tmp2);
4770             } else { /* VMLA */
4771                 gen_neon_add(size, tmp, tmp2);
4772             }
4773             break;
4774         case NEON_3R_VMUL:
4775             if (u) { /* polynomial */
4776                 gen_helper_neon_mul_p8(tmp, tmp, tmp2);
4777             } else { /* Integer */
4778                 switch (size) {
4779                 case 0: gen_helper_neon_mul_u8(tmp, tmp, tmp2); break;
4780                 case 1: gen_helper_neon_mul_u16(tmp, tmp, tmp2); break;
4781                 case 2: tcg_gen_mul_i32(tmp, tmp, tmp2); break;
4782                 default: abort();
4783                 }
4784             }
4785             break;
4786         case NEON_3R_VPMAX:
4787             GEN_NEON_INTEGER_OP(pmax);
4788             break;
4789         case NEON_3R_VPMIN:
4790             GEN_NEON_INTEGER_OP(pmin);
4791             break;
4792         case NEON_3R_VQDMULH_VQRDMULH: /* Multiply high.  */
4793             if (!u) { /* VQDMULH */
4794                 switch (size) {
4795                 case 1:
4796                     gen_helper_neon_qdmulh_s16(tmp, cpu_env, tmp, tmp2);
4797                     break;
4798                 case 2:
4799                     gen_helper_neon_qdmulh_s32(tmp, cpu_env, tmp, tmp2);
4800                     break;
4801                 default: abort();
4802                 }
4803             } else { /* VQRDMULH */
4804                 switch (size) {
4805                 case 1:
4806                     gen_helper_neon_qrdmulh_s16(tmp, cpu_env, tmp, tmp2);
4807                     break;
4808                 case 2:
4809                     gen_helper_neon_qrdmulh_s32(tmp, cpu_env, tmp, tmp2);
4810                     break;
4811                 default: abort();
4812                 }
4813             }
4814             break;
4815         case NEON_3R_VPADD:
4816             switch (size) {
4817             case 0: gen_helper_neon_padd_u8(tmp, tmp, tmp2); break;
4818             case 1: gen_helper_neon_padd_u16(tmp, tmp, tmp2); break;
4819             case 2: tcg_gen_add_i32(tmp, tmp, tmp2); break;
4820             default: abort();
4821             }
4822             break;
4823         case NEON_3R_FLOAT_ARITH: /* Floating point arithmetic. */
4824         {
4825             TCGv_ptr fpstatus = get_fpstatus_ptr(1);
4826             switch ((u << 2) | size) {
4827             case 0: /* VADD */
4828             case 4: /* VPADD */
4829                 gen_helper_vfp_adds(tmp, tmp, tmp2, fpstatus);
4830                 break;
4831             case 2: /* VSUB */
4832                 gen_helper_vfp_subs(tmp, tmp, tmp2, fpstatus);
4833                 break;
4834             case 6: /* VABD */
4835                 gen_helper_neon_abd_f32(tmp, tmp, tmp2, fpstatus);
4836                 break;
4837             default:
4838                 abort();
4839             }
4840             tcg_temp_free_ptr(fpstatus);
4841             break;
4842         }
4843         case NEON_3R_FLOAT_MULTIPLY:
4844         {
4845             TCGv_ptr fpstatus = get_fpstatus_ptr(1);
4846             gen_helper_vfp_muls(tmp, tmp, tmp2, fpstatus);
4847             if (!u) {
4848                 tcg_temp_free_i32(tmp2);
4849                 tmp2 = neon_load_reg(rd, pass);
4850                 if (size == 0) {
4851                     gen_helper_vfp_adds(tmp, tmp, tmp2, fpstatus);
4852                 } else {
4853                     gen_helper_vfp_subs(tmp, tmp2, tmp, fpstatus);
4854                 }
4855             }
4856             tcg_temp_free_ptr(fpstatus);
4857             break;
4858         }
4859         case NEON_3R_FLOAT_CMP:
4860         {
4861             TCGv_ptr fpstatus = get_fpstatus_ptr(1);
4862             if (!u) {
4863                 gen_helper_neon_ceq_f32(tmp, tmp, tmp2, fpstatus);
4864             } else {
4865                 if (size == 0) {
4866                     gen_helper_neon_cge_f32(tmp, tmp, tmp2, fpstatus);
4867                 } else {
4868                     gen_helper_neon_cgt_f32(tmp, tmp, tmp2, fpstatus);
4869                 }
4870             }
4871             tcg_temp_free_ptr(fpstatus);
4872             break;
4873         }
4874         case NEON_3R_FLOAT_ACMP:
4875         {
4876             TCGv_ptr fpstatus = get_fpstatus_ptr(1);
4877             if (size == 0) {
4878                 gen_helper_neon_acge_f32(tmp, tmp, tmp2, fpstatus);
4879             } else {
4880                 gen_helper_neon_acgt_f32(tmp, tmp, tmp2, fpstatus);
4881             }
4882             tcg_temp_free_ptr(fpstatus);
4883             break;
4884         }
4885         case NEON_3R_FLOAT_MINMAX:
4886         {
4887             TCGv_ptr fpstatus = get_fpstatus_ptr(1);
4888             if (size == 0) {
4889                 gen_helper_neon_max_f32(tmp, tmp, tmp2, fpstatus);
4890             } else {
4891                 gen_helper_neon_min_f32(tmp, tmp, tmp2, fpstatus);
4892             }
4893             tcg_temp_free_ptr(fpstatus);
4894             break;
4895         }
4896         case NEON_3R_VRECPS_VRSQRTS:
4897             if (size == 0)
4898                 gen_helper_recps_f32(tmp, tmp, tmp2, cpu_env);
4899             else
4900                 gen_helper_rsqrts_f32(tmp, tmp, tmp2, cpu_env);
4901             break;
4902         case NEON_3R_VFM:
4903         {
4904             /* VFMA, VFMS: fused multiply-add */
4905             TCGv_ptr fpstatus = get_fpstatus_ptr(1);
4906             TCGv_i32 tmp3 = neon_load_reg(rd, pass);
4907             if (size) {
4908                 /* VFMS */
4909                 gen_helper_vfp_negs(tmp, tmp);
4910             }
4911             gen_helper_vfp_muladds(tmp, tmp, tmp2, tmp3, fpstatus);
4912             tcg_temp_free_i32(tmp3);
4913             tcg_temp_free_ptr(fpstatus);
4914             break;
4915         }
4916         default:
4917             abort();
4918         }
4919         tcg_temp_free_i32(tmp2);
4920
4921         /* Save the result.  For elementwise operations we can put it
4922            straight into the destination register.  For pairwise operations
4923            we have to be careful to avoid clobbering the source operands.  */
4924         if (pairwise && rd == rm) {
4925             neon_store_scratch(pass, tmp);
4926         } else {
4927             neon_store_reg(rd, pass, tmp);
4928         }
4929
4930         } /* for pass */
4931         if (pairwise && rd == rm) {
4932             for (pass = 0; pass < (q ? 4 : 2); pass++) {
4933                 tmp = neon_load_scratch(pass);
4934                 neon_store_reg(rd, pass, tmp);
4935             }
4936         }
4937         /* End of 3 register same size operations.  */
4938     } else if (insn & (1 << 4)) {
4939         if ((insn & 0x00380080) != 0) {
4940             /* Two registers and shift.  */
4941             op = (insn >> 8) & 0xf;
4942             if (insn & (1 << 7)) {
4943                 /* 64-bit shift. */
4944                 if (op > 7) {
4945                     return 1;
4946                 }
4947                 size = 3;
4948             } else {
4949                 size = 2;
4950                 while ((insn & (1 << (size + 19))) == 0)
4951                     size--;
4952             }
4953             shift = (insn >> 16) & ((1 << (3 + size)) - 1);
4954             /* To avoid excessive dumplication of ops we implement shift
4955                by immediate using the variable shift operations.  */
4956             if (op < 8) {
4957                 /* Shift by immediate:
4958                    VSHR, VSRA, VRSHR, VRSRA, VSRI, VSHL, VQSHL, VQSHLU.  */
4959                 if (q && ((rd | rm) & 1)) {
4960                     return 1;
4961                 }
4962                 if (!u && (op == 4 || op == 6)) {
4963                     return 1;
4964                 }
4965                 /* Right shifts are encoded as N - shift, where N is the
4966                    element size in bits.  */
4967                 if (op <= 4)
4968                     shift = shift - (1 << (size + 3));
4969                 if (size == 3) {
4970                     count = q + 1;
4971                 } else {
4972                     count = q ? 4: 2;
4973                 }
4974                 switch (size) {
4975                 case 0:
4976                     imm = (uint8_t) shift;
4977                     imm |= imm << 8;
4978                     imm |= imm << 16;
4979                     break;
4980                 case 1:
4981                     imm = (uint16_t) shift;
4982                     imm |= imm << 16;
4983                     break;
4984                 case 2:
4985                 case 3:
4986                     imm = shift;
4987                     break;
4988                 default:
4989                     abort();
4990                 }
4991
4992                 for (pass = 0; pass < count; pass++) {
4993                     if (size == 3) {
4994                         neon_load_reg64(cpu_V0, rm + pass);
4995                         tcg_gen_movi_i64(cpu_V1, imm);
4996                         switch (op) {
4997                         case 0:  /* VSHR */
4998                         case 1:  /* VSRA */
4999                             if (u)
5000                                 gen_helper_neon_shl_u64(cpu_V0, cpu_V0, cpu_V1);
5001                             else
5002                                 gen_helper_neon_shl_s64(cpu_V0, cpu_V0, cpu_V1);
5003                             break;
5004                         case 2: /* VRSHR */
5005                         case 3: /* VRSRA */
5006                             if (u)
5007                                 gen_helper_neon_rshl_u64(cpu_V0, cpu_V0, cpu_V1);
5008                             else
5009                                 gen_helper_neon_rshl_s64(cpu_V0, cpu_V0, cpu_V1);
5010                             break;
5011                         case 4: /* VSRI */
5012                         case 5: /* VSHL, VSLI */
5013                             gen_helper_neon_shl_u64(cpu_V0, cpu_V0, cpu_V1);
5014                             break;
5015                         case 6: /* VQSHLU */
5016                             gen_helper_neon_qshlu_s64(cpu_V0, cpu_env,
5017                                                       cpu_V0, cpu_V1);
5018                             break;
5019                         case 7: /* VQSHL */
5020                             if (u) {
5021                                 gen_helper_neon_qshl_u64(cpu_V0, cpu_env,
5022                                                          cpu_V0, cpu_V1);
5023                             } else {
5024                                 gen_helper_neon_qshl_s64(cpu_V0, cpu_env,
5025                                                          cpu_V0, cpu_V1);
5026                             }
5027                             break;
5028                         }
5029                         if (op == 1 || op == 3) {
5030                             /* Accumulate.  */
5031                             neon_load_reg64(cpu_V1, rd + pass);
5032                             tcg_gen_add_i64(cpu_V0, cpu_V0, cpu_V1);
5033                         } else if (op == 4 || (op == 5 && u)) {
5034                             /* Insert */
5035                             neon_load_reg64(cpu_V1, rd + pass);
5036                             uint64_t mask;
5037                             if (shift < -63 || shift > 63) {
5038                                 mask = 0;
5039                             } else {
5040                                 if (op == 4) {
5041                                     mask = 0xffffffffffffffffull >> -shift;
5042                                 } else {
5043                                     mask = 0xffffffffffffffffull << shift;
5044                                 }
5045                             }
5046                             tcg_gen_andi_i64(cpu_V1, cpu_V1, ~mask);
5047                             tcg_gen_or_i64(cpu_V0, cpu_V0, cpu_V1);
5048                         }
5049                         neon_store_reg64(cpu_V0, rd + pass);
5050                     } else { /* size < 3 */
5051                         /* Operands in T0 and T1.  */
5052                         tmp = neon_load_reg(rm, pass);
5053                         tmp2 = tcg_temp_new_i32();
5054                         tcg_gen_movi_i32(tmp2, imm);
5055                         switch (op) {
5056                         case 0:  /* VSHR */
5057                         case 1:  /* VSRA */
5058                             GEN_NEON_INTEGER_OP(shl);
5059                             break;
5060                         case 2: /* VRSHR */
5061                         case 3: /* VRSRA */
5062                             GEN_NEON_INTEGER_OP(rshl);
5063                             break;
5064                         case 4: /* VSRI */
5065                         case 5: /* VSHL, VSLI */
5066                             switch (size) {
5067                             case 0: gen_helper_neon_shl_u8(tmp, tmp, tmp2); break;
5068                             case 1: gen_helper_neon_shl_u16(tmp, tmp, tmp2); break;
5069                             case 2: gen_helper_neon_shl_u32(tmp, tmp, tmp2); break;
5070                             default: abort();
5071                             }
5072                             break;
5073                         case 6: /* VQSHLU */
5074                             switch (size) {
5075                             case 0:
5076                                 gen_helper_neon_qshlu_s8(tmp, cpu_env,
5077                                                          tmp, tmp2);
5078                                 break;
5079                             case 1:
5080                                 gen_helper_neon_qshlu_s16(tmp, cpu_env,
5081                                                           tmp, tmp2);
5082                                 break;
5083                             case 2:
5084                                 gen_helper_neon_qshlu_s32(tmp, cpu_env,
5085                                                           tmp, tmp2);
5086                                 break;
5087                             default:
5088                                 abort();
5089                             }
5090                             break;
5091                         case 7: /* VQSHL */
5092                             GEN_NEON_INTEGER_OP_ENV(qshl);
5093                             break;
5094                         }
5095                         tcg_temp_free_i32(tmp2);
5096
5097                         if (op == 1 || op == 3) {
5098                             /* Accumulate.  */
5099                             tmp2 = neon_load_reg(rd, pass);
5100                             gen_neon_add(size, tmp, tmp2);
5101                             tcg_temp_free_i32(tmp2);
5102                         } else if (op == 4 || (op == 5 && u)) {
5103                             /* Insert */
5104                             switch (size) {
5105                             case 0:
5106                                 if (op == 4)
5107                                     mask = 0xff >> -shift;
5108                                 else
5109                                     mask = (uint8_t)(0xff << shift);
5110                                 mask |= mask << 8;
5111                                 mask |= mask << 16;
5112                                 break;
5113                             case 1:
5114                                 if (op == 4)
5115                                     mask = 0xffff >> -shift;
5116                                 else
5117                                     mask = (uint16_t)(0xffff << shift);
5118                                 mask |= mask << 16;
5119                                 break;
5120                             case 2:
5121                                 if (shift < -31 || shift > 31) {
5122                                     mask = 0;
5123                                 } else {
5124                                     if (op == 4)
5125                                         mask = 0xffffffffu >> -shift;
5126                                     else
5127                                         mask = 0xffffffffu << shift;
5128                                 }
5129                                 break;
5130                             default:
5131                                 abort();
5132                             }
5133                             tmp2 = neon_load_reg(rd, pass);
5134                             tcg_gen_andi_i32(tmp, tmp, mask);
5135                             tcg_gen_andi_i32(tmp2, tmp2, ~mask);
5136                             tcg_gen_or_i32(tmp, tmp, tmp2);
5137                             tcg_temp_free_i32(tmp2);
5138                         }
5139                         neon_store_reg(rd, pass, tmp);
5140                     }
5141                 } /* for pass */
5142             } else if (op < 10) {
5143                 /* Shift by immediate and narrow:
5144                    VSHRN, VRSHRN, VQSHRN, VQRSHRN.  */
5145                 int input_unsigned = (op == 8) ? !u : u;
5146                 if (rm & 1) {
5147                     return 1;
5148                 }
5149                 shift = shift - (1 << (size + 3));
5150                 size++;
5151                 if (size == 3) {
5152                     tmp64 = tcg_const_i64(shift);
5153                     neon_load_reg64(cpu_V0, rm);
5154                     neon_load_reg64(cpu_V1, rm + 1);
5155                     for (pass = 0; pass < 2; pass++) {
5156                         TCGv_i64 in;
5157                         if (pass == 0) {
5158                             in = cpu_V0;
5159                         } else {
5160                             in = cpu_V1;
5161                         }
5162                         if (q) {
5163                             if (input_unsigned) {
5164                                 gen_helper_neon_rshl_u64(cpu_V0, in, tmp64);
5165                             } else {
5166                                 gen_helper_neon_rshl_s64(cpu_V0, in, tmp64);
5167                             }
5168                         } else {
5169                             if (input_unsigned) {
5170                                 gen_helper_neon_shl_u64(cpu_V0, in, tmp64);
5171                             } else {
5172                                 gen_helper_neon_shl_s64(cpu_V0, in, tmp64);
5173                             }
5174                         }
5175                         tmp = tcg_temp_new_i32();
5176                         gen_neon_narrow_op(op == 8, u, size - 1, tmp, cpu_V0);
5177                         neon_store_reg(rd, pass, tmp);
5178                     } /* for pass */
5179                     tcg_temp_free_i64(tmp64);
5180                 } else {
5181                     if (size == 1) {
5182                         imm = (uint16_t)shift;
5183                         imm |= imm << 16;
5184                     } else {
5185                         /* size == 2 */
5186                         imm = (uint32_t)shift;
5187                     }
5188                     tmp2 = tcg_const_i32(imm);
5189                     tmp4 = neon_load_reg(rm + 1, 0);
5190                     tmp5 = neon_load_reg(rm + 1, 1);
5191                     for (pass = 0; pass < 2; pass++) {
5192                         if (pass == 0) {
5193                             tmp = neon_load_reg(rm, 0);
5194                         } else {
5195                             tmp = tmp4;
5196                         }
5197                         gen_neon_shift_narrow(size, tmp, tmp2, q,
5198                                               input_unsigned);
5199                         if (pass == 0) {
5200                             tmp3 = neon_load_reg(rm, 1);
5201                         } else {
5202                             tmp3 = tmp5;
5203                         }
5204                         gen_neon_shift_narrow(size, tmp3, tmp2, q,
5205                                               input_unsigned);
5206                         tcg_gen_concat_i32_i64(cpu_V0, tmp, tmp3);
5207                         tcg_temp_free_i32(tmp);
5208                         tcg_temp_free_i32(tmp3);
5209                         tmp = tcg_temp_new_i32();
5210                         gen_neon_narrow_op(op == 8, u, size - 1, tmp, cpu_V0);
5211                         neon_store_reg(rd, pass, tmp);
5212                     } /* for pass */
5213                     tcg_temp_free_i32(tmp2);
5214                 }
5215             } else if (op == 10) {
5216                 /* VSHLL, VMOVL */
5217                 if (q || (rd & 1)) {
5218                     return 1;
5219                 }
5220                 tmp = neon_load_reg(rm, 0);
5221                 tmp2 = neon_load_reg(rm, 1);
5222                 for (pass = 0; pass < 2; pass++) {
5223                     if (pass == 1)
5224                         tmp = tmp2;
5225
5226                     gen_neon_widen(cpu_V0, tmp, size, u);
5227
5228                     if (shift != 0) {
5229                         /* The shift is less than the width of the source
5230                            type, so we can just shift the whole register.  */
5231                         tcg_gen_shli_i64(cpu_V0, cpu_V0, shift);
5232                         /* Widen the result of shift: we need to clear
5233                          * the potential overflow bits resulting from
5234                          * left bits of the narrow input appearing as
5235                          * right bits of left the neighbour narrow
5236                          * input.  */
5237                         if (size < 2 || !u) {
5238                             uint64_t imm64;
5239                             if (size == 0) {
5240                                 imm = (0xffu >> (8 - shift));
5241                                 imm |= imm << 16;
5242                             } else if (size == 1) {
5243                                 imm = 0xffff >> (16 - shift);
5244                             } else {
5245                                 /* size == 2 */
5246                                 imm = 0xffffffff >> (32 - shift);
5247                             }
5248                             if (size < 2) {
5249                                 imm64 = imm | (((uint64_t)imm) << 32);
5250                             } else {
5251                                 imm64 = imm;
5252                             }
5253                             tcg_gen_andi_i64(cpu_V0, cpu_V0, ~imm64);
5254                         }
5255                     }
5256                     neon_store_reg64(cpu_V0, rd + pass);
5257                 }
5258             } else if (op >= 14) {
5259                 /* VCVT fixed-point.  */
5260                 if (!(insn & (1 << 21)) || (q && ((rd | rm) & 1))) {
5261                     return 1;
5262                 }
5263                 /* We have already masked out the must-be-1 top bit of imm6,
5264                  * hence this 32-shift where the ARM ARM has 64-imm6.
5265                  */
5266                 shift = 32 - shift;
5267                 for (pass = 0; pass < (q ? 4 : 2); pass++) {
5268                     tcg_gen_ld_f32(cpu_F0s, cpu_env, neon_reg_offset(rm, pass));
5269                     if (!(op & 1)) {
5270                         if (u)
5271                             gen_vfp_ulto(0, shift, 1);
5272                         else
5273                             gen_vfp_slto(0, shift, 1);
5274                     } else {
5275                         if (u)
5276                             gen_vfp_toul(0, shift, 1);
5277                         else
5278                             gen_vfp_tosl(0, shift, 1);
5279                     }
5280                     tcg_gen_st_f32(cpu_F0s, cpu_env, neon_reg_offset(rd, pass));
5281                 }
5282             } else {
5283                 return 1;
5284             }
5285         } else { /* (insn & 0x00380080) == 0 */
5286             int invert;
5287             if (q && (rd & 1)) {
5288                 return 1;
5289             }
5290
5291             op = (insn >> 8) & 0xf;
5292             /* One register and immediate.  */
5293             imm = (u << 7) | ((insn >> 12) & 0x70) | (insn & 0xf);
5294             invert = (insn & (1 << 5)) != 0;
5295             /* Note that op = 2,3,4,5,6,7,10,11,12,13 imm=0 is UNPREDICTABLE.
5296              * We choose to not special-case this and will behave as if a
5297              * valid constant encoding of 0 had been given.
5298              */
5299             switch (op) {
5300             case 0: case 1:
5301                 /* no-op */
5302                 break;
5303             case 2: case 3:
5304                 imm <<= 8;
5305                 break;
5306             case 4: case 5:
5307                 imm <<= 16;
5308                 break;
5309             case 6: case 7:
5310                 imm <<= 24;
5311                 break;
5312             case 8: case 9:
5313                 imm |= imm << 16;
5314                 break;
5315             case 10: case 11:
5316                 imm = (imm << 8) | (imm << 24);
5317                 break;
5318             case 12:
5319                 imm = (imm << 8) | 0xff;
5320                 break;
5321             case 13:
5322                 imm = (imm << 16) | 0xffff;
5323                 break;
5324             case 14:
5325                 imm |= (imm << 8) | (imm << 16) | (imm << 24);
5326                 if (invert)
5327                     imm = ~imm;
5328                 break;
5329             case 15:
5330                 if (invert) {
5331                     return 1;
5332                 }
5333                 imm = ((imm & 0x80) << 24) | ((imm & 0x3f) << 19)
5334                       | ((imm & 0x40) ? (0x1f << 25) : (1 << 30));
5335                 break;
5336             }
5337             if (invert)
5338                 imm = ~imm;
5339
5340             for (pass = 0; pass < (q ? 4 : 2); pass++) {
5341                 if (op & 1 && op < 12) {
5342                     tmp = neon_load_reg(rd, pass);
5343                     if (invert) {
5344                         /* The immediate value has already been inverted, so
5345                            BIC becomes AND.  */
5346                         tcg_gen_andi_i32(tmp, tmp, imm);
5347                     } else {
5348                         tcg_gen_ori_i32(tmp, tmp, imm);
5349                     }
5350                 } else {
5351                     /* VMOV, VMVN.  */
5352                     tmp = tcg_temp_new_i32();
5353                     if (op == 14 && invert) {
5354                         int n;
5355                         uint32_t val;
5356                         val = 0;
5357                         for (n = 0; n < 4; n++) {
5358                             if (imm & (1 << (n + (pass & 1) * 4)))
5359                                 val |= 0xff << (n * 8);
5360                         }
5361                         tcg_gen_movi_i32(tmp, val);
5362                     } else {
5363                         tcg_gen_movi_i32(tmp, imm);
5364                     }
5365                 }
5366                 neon_store_reg(rd, pass, tmp);
5367             }
5368         }
5369     } else { /* (insn & 0x00800010 == 0x00800000) */
5370         if (size != 3) {
5371             op = (insn >> 8) & 0xf;
5372             if ((insn & (1 << 6)) == 0) {
5373                 /* Three registers of different lengths.  */
5374                 int src1_wide;
5375                 int src2_wide;
5376                 int prewiden;
5377                 /* undefreq: bit 0 : UNDEF if size != 0
5378                  *           bit 1 : UNDEF if size == 0
5379                  *           bit 2 : UNDEF if U == 1
5380                  * Note that [1:0] set implies 'always UNDEF'
5381                  */
5382                 int undefreq;
5383                 /* prewiden, src1_wide, src2_wide, undefreq */
5384                 static const int neon_3reg_wide[16][4] = {
5385                     {1, 0, 0, 0}, /* VADDL */
5386                     {1, 1, 0, 0}, /* VADDW */
5387                     {1, 0, 0, 0}, /* VSUBL */
5388                     {1, 1, 0, 0}, /* VSUBW */
5389                     {0, 1, 1, 0}, /* VADDHN */
5390                     {0, 0, 0, 0}, /* VABAL */
5391                     {0, 1, 1, 0}, /* VSUBHN */
5392                     {0, 0, 0, 0}, /* VABDL */
5393                     {0, 0, 0, 0}, /* VMLAL */
5394                     {0, 0, 0, 6}, /* VQDMLAL */
5395                     {0, 0, 0, 0}, /* VMLSL */
5396                     {0, 0, 0, 6}, /* VQDMLSL */
5397                     {0, 0, 0, 0}, /* Integer VMULL */
5398                     {0, 0, 0, 2}, /* VQDMULL */
5399                     {0, 0, 0, 5}, /* Polynomial VMULL */
5400                     {0, 0, 0, 3}, /* Reserved: always UNDEF */
5401                 };
5402
5403                 prewiden = neon_3reg_wide[op][0];
5404                 src1_wide = neon_3reg_wide[op][1];
5405                 src2_wide = neon_3reg_wide[op][2];
5406                 undefreq = neon_3reg_wide[op][3];
5407
5408                 if (((undefreq & 1) && (size != 0)) ||
5409                     ((undefreq & 2) && (size == 0)) ||
5410                     ((undefreq & 4) && u)) {
5411                     return 1;
5412                 }
5413                 if ((src1_wide && (rn & 1)) ||
5414                     (src2_wide && (rm & 1)) ||
5415                     (!src2_wide && (rd & 1))) {
5416                     return 1;
5417                 }
5418
5419                 /* Avoid overlapping operands.  Wide source operands are
5420                    always aligned so will never overlap with wide
5421                    destinations in problematic ways.  */
5422                 if (rd == rm && !src2_wide) {
5423                     tmp = neon_load_reg(rm, 1);
5424                     neon_store_scratch(2, tmp);
5425                 } else if (rd == rn && !src1_wide) {
5426                     tmp = neon_load_reg(rn, 1);
5427                     neon_store_scratch(2, tmp);
5428                 }
5429                 TCGV_UNUSED(tmp3);
5430                 for (pass = 0; pass < 2; pass++) {
5431                     if (src1_wide) {
5432                         neon_load_reg64(cpu_V0, rn + pass);
5433                         TCGV_UNUSED(tmp);
5434                     } else {
5435                         if (pass == 1 && rd == rn) {
5436                             tmp = neon_load_scratch(2);
5437                         } else {
5438                             tmp = neon_load_reg(rn, pass);
5439                         }
5440                         if (prewiden) {
5441                             gen_neon_widen(cpu_V0, tmp, size, u);
5442                         }
5443                     }
5444                     if (src2_wide) {
5445                         neon_load_reg64(cpu_V1, rm + pass);
5446                         TCGV_UNUSED(tmp2);
5447                     } else {
5448                         if (pass == 1 && rd == rm) {
5449                             tmp2 = neon_load_scratch(2);
5450                         } else {
5451                             tmp2 = neon_load_reg(rm, pass);
5452                         }
5453                         if (prewiden) {
5454                             gen_neon_widen(cpu_V1, tmp2, size, u);
5455                         }
5456                     }
5457                     switch (op) {
5458                     case 0: case 1: case 4: /* VADDL, VADDW, VADDHN, VRADDHN */
5459                         gen_neon_addl(size);
5460                         break;
5461                     case 2: case 3: case 6: /* VSUBL, VSUBW, VSUBHN, VRSUBHN */
5462                         gen_neon_subl(size);
5463                         break;
5464                     case 5: case 7: /* VABAL, VABDL */
5465                         switch ((size << 1) | u) {
5466                         case 0:
5467                             gen_helper_neon_abdl_s16(cpu_V0, tmp, tmp2);
5468                             break;
5469                         case 1:
5470                             gen_helper_neon_abdl_u16(cpu_V0, tmp, tmp2);
5471                             break;
5472                         case 2:
5473                             gen_helper_neon_abdl_s32(cpu_V0, tmp, tmp2);
5474                             break;
5475                         case 3:
5476                             gen_helper_neon_abdl_u32(cpu_V0, tmp, tmp2);
5477                             break;
5478                         case 4:
5479                             gen_helper_neon_abdl_s64(cpu_V0, tmp, tmp2);
5480                             break;
5481                         case 5:
5482                             gen_helper_neon_abdl_u64(cpu_V0, tmp, tmp2);
5483                             break;
5484                         default: abort();
5485                         }
5486                         tcg_temp_free_i32(tmp2);
5487                         tcg_temp_free_i32(tmp);
5488                         break;
5489                     case 8: case 9: case 10: case 11: case 12: case 13:
5490                         /* VMLAL, VQDMLAL, VMLSL, VQDMLSL, VMULL, VQDMULL */
5491                         gen_neon_mull(cpu_V0, tmp, tmp2, size, u);
5492                         break;
5493                     case 14: /* Polynomial VMULL */
5494                         gen_helper_neon_mull_p8(cpu_V0, tmp, tmp2);
5495                         tcg_temp_free_i32(tmp2);
5496                         tcg_temp_free_i32(tmp);
5497                         break;
5498                     default: /* 15 is RESERVED: caught earlier  */
5499                         abort();
5500                     }
5501                     if (op == 13) {
5502                         /* VQDMULL */
5503                         gen_neon_addl_saturate(cpu_V0, cpu_V0, size);
5504                         neon_store_reg64(cpu_V0, rd + pass);
5505                     } else if (op == 5 || (op >= 8 && op <= 11)) {
5506                         /* Accumulate.  */
5507                         neon_load_reg64(cpu_V1, rd + pass);
5508                         switch (op) {
5509                         case 10: /* VMLSL */
5510                             gen_neon_negl(cpu_V0, size);
5511                             /* Fall through */
5512                         case 5: case 8: /* VABAL, VMLAL */
5513                             gen_neon_addl(size);
5514                             break;
5515                         case 9: case 11: /* VQDMLAL, VQDMLSL */
5516                             gen_neon_addl_saturate(cpu_V0, cpu_V0, size);
5517                             if (op == 11) {
5518                                 gen_neon_negl(cpu_V0, size);
5519                             }
5520                             gen_neon_addl_saturate(cpu_V0, cpu_V1, size);
5521                             break;
5522                         default:
5523                             abort();
5524                         }
5525                         neon_store_reg64(cpu_V0, rd + pass);
5526                     } else if (op == 4 || op == 6) {
5527                         /* Narrowing operation.  */
5528                         tmp = tcg_temp_new_i32();
5529                         if (!u) {
5530                             switch (size) {
5531                             case 0:
5532                                 gen_helper_neon_narrow_high_u8(tmp, cpu_V0);
5533                                 break;
5534                             case 1:
5535                                 gen_helper_neon_narrow_high_u16(tmp, cpu_V0);
5536                                 break;
5537                             case 2:
5538                                 tcg_gen_shri_i64(cpu_V0, cpu_V0, 32);
5539                                 tcg_gen_trunc_i64_i32(tmp, cpu_V0);
5540                                 break;
5541                             default: abort();
5542                             }
5543                         } else {
5544                             switch (size) {
5545                             case 0:
5546                                 gen_helper_neon_narrow_round_high_u8(tmp, cpu_V0);
5547                                 break;
5548                             case 1:
5549                                 gen_helper_neon_narrow_round_high_u16(tmp, cpu_V0);
5550                                 break;
5551                             case 2:
5552                                 tcg_gen_addi_i64(cpu_V0, cpu_V0, 1u << 31);
5553                                 tcg_gen_shri_i64(cpu_V0, cpu_V0, 32);
5554                                 tcg_gen_trunc_i64_i32(tmp, cpu_V0);
5555                                 break;
5556                             default: abort();
5557                             }
5558                         }
5559                         if (pass == 0) {
5560                             tmp3 = tmp;
5561                         } else {
5562                             neon_store_reg(rd, 0, tmp3);
5563                             neon_store_reg(rd, 1, tmp);
5564                         }
5565                     } else {
5566                         /* Write back the result.  */
5567                         neon_store_reg64(cpu_V0, rd + pass);
5568                     }
5569                 }
5570             } else {
5571                 /* Two registers and a scalar. NB that for ops of this form
5572                  * the ARM ARM labels bit 24 as Q, but it is in our variable
5573                  * 'u', not 'q'.
5574                  */
5575                 if (size == 0) {
5576                     return 1;
5577                 }
5578                 switch (op) {
5579                 case 1: /* Float VMLA scalar */
5580                 case 5: /* Floating point VMLS scalar */
5581                 case 9: /* Floating point VMUL scalar */
5582                     if (size == 1) {
5583                         return 1;
5584                     }
5585                     /* fall through */
5586                 case 0: /* Integer VMLA scalar */
5587                 case 4: /* Integer VMLS scalar */
5588                 case 8: /* Integer VMUL scalar */
5589                 case 12: /* VQDMULH scalar */
5590                 case 13: /* VQRDMULH scalar */
5591                     if (u && ((rd | rn) & 1)) {
5592                         return 1;
5593                     }
5594                     tmp = neon_get_scalar(size, rm);
5595                     neon_store_scratch(0, tmp);
5596                     for (pass = 0; pass < (u ? 4 : 2); pass++) {
5597                         tmp = neon_load_scratch(0);
5598                         tmp2 = neon_load_reg(rn, pass);
5599                         if (op == 12) {
5600                             if (size == 1) {
5601                                 gen_helper_neon_qdmulh_s16(tmp, cpu_env, tmp, tmp2);
5602                             } else {
5603                                 gen_helper_neon_qdmulh_s32(tmp, cpu_env, tmp, tmp2);
5604                             }
5605                         } else if (op == 13) {
5606                             if (size == 1) {
5607                                 gen_helper_neon_qrdmulh_s16(tmp, cpu_env, tmp, tmp2);
5608                             } else {
5609                                 gen_helper_neon_qrdmulh_s32(tmp, cpu_env, tmp, tmp2);
5610                             }
5611                         } else if (op & 1) {
5612                             TCGv_ptr fpstatus = get_fpstatus_ptr(1);
5613                             gen_helper_vfp_muls(tmp, tmp, tmp2, fpstatus);
5614                             tcg_temp_free_ptr(fpstatus);
5615                         } else {
5616                             switch (size) {
5617                             case 0: gen_helper_neon_mul_u8(tmp, tmp, tmp2); break;
5618                             case 1: gen_helper_neon_mul_u16(tmp, tmp, tmp2); break;
5619                             case 2: tcg_gen_mul_i32(tmp, tmp, tmp2); break;
5620                             default: abort();
5621                             }
5622                         }
5623                         tcg_temp_free_i32(tmp2);
5624                         if (op < 8) {
5625                             /* Accumulate.  */
5626                             tmp2 = neon_load_reg(rd, pass);
5627                             switch (op) {
5628                             case 0:
5629                                 gen_neon_add(size, tmp, tmp2);
5630                                 break;
5631                             case 1:
5632                             {
5633                                 TCGv_ptr fpstatus = get_fpstatus_ptr(1);
5634                                 gen_helper_vfp_adds(tmp, tmp, tmp2, fpstatus);
5635                                 tcg_temp_free_ptr(fpstatus);
5636                                 break;
5637                             }
5638                             case 4:
5639                                 gen_neon_rsb(size, tmp, tmp2);
5640                                 break;
5641                             case 5:
5642                             {
5643                                 TCGv_ptr fpstatus = get_fpstatus_ptr(1);
5644                                 gen_helper_vfp_subs(tmp, tmp2, tmp, fpstatus);
5645                                 tcg_temp_free_ptr(fpstatus);
5646                                 break;
5647                             }
5648                             default:
5649                                 abort();
5650                             }
5651                             tcg_temp_free_i32(tmp2);
5652                         }
5653                         neon_store_reg(rd, pass, tmp);
5654                     }
5655                     break;
5656                 case 3: /* VQDMLAL scalar */
5657                 case 7: /* VQDMLSL scalar */
5658                 case 11: /* VQDMULL scalar */
5659                     if (u == 1) {
5660                         return 1;
5661                     }
5662                     /* fall through */
5663                 case 2: /* VMLAL sclar */
5664                 case 6: /* VMLSL scalar */
5665                 case 10: /* VMULL scalar */
5666                     if (rd & 1) {
5667                         return 1;
5668                     }
5669                     tmp2 = neon_get_scalar(size, rm);
5670                     /* We need a copy of tmp2 because gen_neon_mull
5671                      * deletes it during pass 0.  */
5672                     tmp4 = tcg_temp_new_i32();
5673                     tcg_gen_mov_i32(tmp4, tmp2);
5674                     tmp3 = neon_load_reg(rn, 1);
5675
5676                     for (pass = 0; pass < 2; pass++) {
5677                         if (pass == 0) {
5678                             tmp = neon_load_reg(rn, 0);
5679                         } else {
5680                             tmp = tmp3;
5681                             tmp2 = tmp4;
5682                         }
5683                         gen_neon_mull(cpu_V0, tmp, tmp2, size, u);
5684                         if (op != 11) {
5685                             neon_load_reg64(cpu_V1, rd + pass);
5686                         }
5687                         switch (op) {
5688                         case 6:
5689                             gen_neon_negl(cpu_V0, size);
5690                             /* Fall through */
5691                         case 2:
5692                             gen_neon_addl(size);
5693                             break;
5694                         case 3: case 7:
5695                             gen_neon_addl_saturate(cpu_V0, cpu_V0, size);
5696                             if (op == 7) {
5697                                 gen_neon_negl(cpu_V0, size);
5698                             }
5699                             gen_neon_addl_saturate(cpu_V0, cpu_V1, size);
5700                             break;
5701                         case 10:
5702                             /* no-op */
5703                             break;
5704                         case 11:
5705                             gen_neon_addl_saturate(cpu_V0, cpu_V0, size);
5706                             break;
5707                         default:
5708                             abort();
5709                         }
5710                         neon_store_reg64(cpu_V0, rd + pass);
5711                     }
5712
5713
5714                     break;
5715                 default: /* 14 and 15 are RESERVED */
5716                     return 1;
5717                 }
5718             }
5719         } else { /* size == 3 */
5720             if (!u) {
5721                 /* Extract.  */
5722                 imm = (insn >> 8) & 0xf;
5723
5724                 if (imm > 7 && !q)
5725                     return 1;
5726
5727                 if (q && ((rd | rn | rm) & 1)) {
5728                     return 1;
5729                 }
5730
5731                 if (imm == 0) {
5732                     neon_load_reg64(cpu_V0, rn);
5733                     if (q) {
5734                         neon_load_reg64(cpu_V1, rn + 1);
5735                     }
5736                 } else if (imm == 8) {
5737                     neon_load_reg64(cpu_V0, rn + 1);
5738                     if (q) {
5739                         neon_load_reg64(cpu_V1, rm);
5740                     }
5741                 } else if (q) {
5742                     tmp64 = tcg_temp_new_i64();
5743                     if (imm < 8) {
5744                         neon_load_reg64(cpu_V0, rn);
5745                         neon_load_reg64(tmp64, rn + 1);
5746                     } else {
5747                         neon_load_reg64(cpu_V0, rn + 1);
5748                         neon_load_reg64(tmp64, rm);
5749                     }
5750                     tcg_gen_shri_i64(cpu_V0, cpu_V0, (imm & 7) * 8);
5751                     tcg_gen_shli_i64(cpu_V1, tmp64, 64 - ((imm & 7) * 8));
5752                     tcg_gen_or_i64(cpu_V0, cpu_V0, cpu_V1);
5753                     if (imm < 8) {
5754                         neon_load_reg64(cpu_V1, rm);
5755                     } else {
5756                         neon_load_reg64(cpu_V1, rm + 1);
5757                         imm -= 8;
5758                     }
5759                     tcg_gen_shli_i64(cpu_V1, cpu_V1, 64 - (imm * 8));
5760                     tcg_gen_shri_i64(tmp64, tmp64, imm * 8);
5761                     tcg_gen_or_i64(cpu_V1, cpu_V1, tmp64);
5762                     tcg_temp_free_i64(tmp64);
5763                 } else {
5764                     /* BUGFIX */
5765                     neon_load_reg64(cpu_V0, rn);
5766                     tcg_gen_shri_i64(cpu_V0, cpu_V0, imm * 8);
5767                     neon_load_reg64(cpu_V1, rm);
5768                     tcg_gen_shli_i64(cpu_V1, cpu_V1, 64 - (imm * 8));
5769                     tcg_gen_or_i64(cpu_V0, cpu_V0, cpu_V1);
5770                 }
5771                 neon_store_reg64(cpu_V0, rd);
5772                 if (q) {
5773                     neon_store_reg64(cpu_V1, rd + 1);
5774                 }
5775             } else if ((insn & (1 << 11)) == 0) {
5776                 /* Two register misc.  */
5777                 op = ((insn >> 12) & 0x30) | ((insn >> 7) & 0xf);
5778                 size = (insn >> 18) & 3;
5779                 /* UNDEF for unknown op values and bad op-size combinations */
5780                 if ((neon_2rm_sizes[op] & (1 << size)) == 0) {
5781                     return 1;
5782                 }
5783                 if ((op != NEON_2RM_VMOVN && op != NEON_2RM_VQMOVN) &&
5784                     q && ((rm | rd) & 1)) {
5785                     return 1;
5786                 }
5787                 switch (op) {
5788                 case NEON_2RM_VREV64:
5789                     for (pass = 0; pass < (q ? 2 : 1); pass++) {
5790                         tmp = neon_load_reg(rm, pass * 2);
5791                         tmp2 = neon_load_reg(rm, pass * 2 + 1);
5792                         switch (size) {
5793                         case 0: tcg_gen_bswap32_i32(tmp, tmp); break;
5794                         case 1: gen_swap_half(tmp); break;
5795                         case 2: /* no-op */ break;
5796                         default: abort();
5797                         }
5798                         neon_store_reg(rd, pass * 2 + 1, tmp);
5799                         if (size == 2) {
5800                             neon_store_reg(rd, pass * 2, tmp2);
5801                         } else {
5802                             switch (size) {
5803                             case 0: tcg_gen_bswap32_i32(tmp2, tmp2); break;
5804                             case 1: gen_swap_half(tmp2); break;
5805                             default: abort();
5806                             }
5807                             neon_store_reg(rd, pass * 2, tmp2);
5808                         }
5809                     }
5810                     break;
5811                 case NEON_2RM_VPADDL: case NEON_2RM_VPADDL_U:
5812                 case NEON_2RM_VPADAL: case NEON_2RM_VPADAL_U:
5813                     for (pass = 0; pass < q + 1; pass++) {
5814                         tmp = neon_load_reg(rm, pass * 2);
5815                         gen_neon_widen(cpu_V0, tmp, size, op & 1);
5816                         tmp = neon_load_reg(rm, pass * 2 + 1);
5817                         gen_neon_widen(cpu_V1, tmp, size, op & 1);
5818                         switch (size) {
5819                         case 0: gen_helper_neon_paddl_u16(CPU_V001); break;
5820                         case 1: gen_helper_neon_paddl_u32(CPU_V001); break;
5821                         case 2: tcg_gen_add_i64(CPU_V001); break;
5822                         default: abort();
5823                         }
5824                         if (op >= NEON_2RM_VPADAL) {
5825                             /* Accumulate.  */
5826                             neon_load_reg64(cpu_V1, rd + pass);
5827                             gen_neon_addl(size);
5828                         }
5829                         neon_store_reg64(cpu_V0, rd + pass);
5830                     }
5831                     break;
5832                 case NEON_2RM_VTRN:
5833                     if (size == 2) {
5834                         int n;
5835                         for (n = 0; n < (q ? 4 : 2); n += 2) {
5836                             tmp = neon_load_reg(rm, n);
5837                             tmp2 = neon_load_reg(rd, n + 1);
5838                             neon_store_reg(rm, n, tmp2);
5839                             neon_store_reg(rd, n + 1, tmp);
5840                         }
5841                     } else {
5842                         goto elementwise;
5843                     }
5844                     break;
5845                 case NEON_2RM_VUZP:
5846                     if (gen_neon_unzip(rd, rm, size, q)) {
5847                         return 1;
5848                     }
5849                     break;
5850                 case NEON_2RM_VZIP:
5851                     if (gen_neon_zip(rd, rm, size, q)) {
5852                         return 1;
5853                     }
5854                     break;
5855                 case NEON_2RM_VMOVN: case NEON_2RM_VQMOVN:
5856                     /* also VQMOVUN; op field and mnemonics don't line up */
5857                     if (rm & 1) {
5858                         return 1;
5859                     }
5860                     TCGV_UNUSED(tmp2);
5861                     for (pass = 0; pass < 2; pass++) {
5862                         neon_load_reg64(cpu_V0, rm + pass);
5863                         tmp = tcg_temp_new_i32();
5864                         gen_neon_narrow_op(op == NEON_2RM_VMOVN, q, size,
5865                                            tmp, cpu_V0);
5866                         if (pass == 0) {
5867                             tmp2 = tmp;
5868                         } else {
5869                             neon_store_reg(rd, 0, tmp2);
5870                             neon_store_reg(rd, 1, tmp);
5871                         }
5872                     }
5873                     break;
5874                 case NEON_2RM_VSHLL:
5875                     if (q || (rd & 1)) {
5876                         return 1;
5877                     }
5878                     tmp = neon_load_reg(rm, 0);
5879                     tmp2 = neon_load_reg(rm, 1);
5880                     for (pass = 0; pass < 2; pass++) {
5881                         if (pass == 1)
5882                             tmp = tmp2;
5883                         gen_neon_widen(cpu_V0, tmp, size, 1);
5884                         tcg_gen_shli_i64(cpu_V0, cpu_V0, 8 << size);
5885                         neon_store_reg64(cpu_V0, rd + pass);
5886                     }
5887                     break;
5888                 case NEON_2RM_VCVT_F16_F32:
5889                     if (!arm_feature(env, ARM_FEATURE_VFP_FP16) ||
5890                         q || (rm & 1)) {
5891                         return 1;
5892                     }
5893                     tmp = tcg_temp_new_i32();
5894                     tmp2 = tcg_temp_new_i32();
5895                     tcg_gen_ld_f32(cpu_F0s, cpu_env, neon_reg_offset(rm, 0));
5896                     gen_helper_neon_fcvt_f32_to_f16(tmp, cpu_F0s, cpu_env);
5897                     tcg_gen_ld_f32(cpu_F0s, cpu_env, neon_reg_offset(rm, 1));
5898                     gen_helper_neon_fcvt_f32_to_f16(tmp2, cpu_F0s, cpu_env);
5899                     tcg_gen_shli_i32(tmp2, tmp2, 16);
5900                     tcg_gen_or_i32(tmp2, tmp2, tmp);
5901                     tcg_gen_ld_f32(cpu_F0s, cpu_env, neon_reg_offset(rm, 2));
5902                     gen_helper_neon_fcvt_f32_to_f16(tmp, cpu_F0s, cpu_env);
5903                     tcg_gen_ld_f32(cpu_F0s, cpu_env, neon_reg_offset(rm, 3));
5904                     neon_store_reg(rd, 0, tmp2);
5905                     tmp2 = tcg_temp_new_i32();
5906                     gen_helper_neon_fcvt_f32_to_f16(tmp2, cpu_F0s, cpu_env);
5907                     tcg_gen_shli_i32(tmp2, tmp2, 16);
5908                     tcg_gen_or_i32(tmp2, tmp2, tmp);
5909                     neon_store_reg(rd, 1, tmp2);
5910                     tcg_temp_free_i32(tmp);
5911                     break;
5912                 case NEON_2RM_VCVT_F32_F16:
5913                     if (!arm_feature(env, ARM_FEATURE_VFP_FP16) ||
5914                         q || (rd & 1)) {
5915                         return 1;
5916                     }
5917                     tmp3 = tcg_temp_new_i32();
5918                     tmp = neon_load_reg(rm, 0);
5919                     tmp2 = neon_load_reg(rm, 1);
5920                     tcg_gen_ext16u_i32(tmp3, tmp);
5921                     gen_helper_neon_fcvt_f16_to_f32(cpu_F0s, tmp3, cpu_env);
5922                     tcg_gen_st_f32(cpu_F0s, cpu_env, neon_reg_offset(rd, 0));
5923                     tcg_gen_shri_i32(tmp3, tmp, 16);
5924                     gen_helper_neon_fcvt_f16_to_f32(cpu_F0s, tmp3, cpu_env);
5925                     tcg_gen_st_f32(cpu_F0s, cpu_env, neon_reg_offset(rd, 1));
5926                     tcg_temp_free_i32(tmp);
5927                     tcg_gen_ext16u_i32(tmp3, tmp2);
5928                     gen_helper_neon_fcvt_f16_to_f32(cpu_F0s, tmp3, cpu_env);
5929                     tcg_gen_st_f32(cpu_F0s, cpu_env, neon_reg_offset(rd, 2));
5930                     tcg_gen_shri_i32(tmp3, tmp2, 16);
5931                     gen_helper_neon_fcvt_f16_to_f32(cpu_F0s, tmp3, cpu_env);
5932                     tcg_gen_st_f32(cpu_F0s, cpu_env, neon_reg_offset(rd, 3));
5933                     tcg_temp_free_i32(tmp2);
5934                     tcg_temp_free_i32(tmp3);
5935                     break;
5936                 default:
5937                 elementwise:
5938                     for (pass = 0; pass < (q ? 4 : 2); pass++) {
5939                         if (neon_2rm_is_float_op(op)) {
5940                             tcg_gen_ld_f32(cpu_F0s, cpu_env,
5941                                            neon_reg_offset(rm, pass));
5942                             TCGV_UNUSED(tmp);
5943                         } else {
5944                             tmp = neon_load_reg(rm, pass);
5945                         }
5946                         switch (op) {
5947                         case NEON_2RM_VREV32:
5948                             switch (size) {
5949                             case 0: tcg_gen_bswap32_i32(tmp, tmp); break;
5950                             case 1: gen_swap_half(tmp); break;
5951                             default: abort();
5952                             }
5953                             break;
5954                         case NEON_2RM_VREV16:
5955                             gen_rev16(tmp);
5956                             break;
5957                         case NEON_2RM_VCLS:
5958                             switch (size) {
5959                             case 0: gen_helper_neon_cls_s8(tmp, tmp); break;
5960                             case 1: gen_helper_neon_cls_s16(tmp, tmp); break;
5961                             case 2: gen_helper_neon_cls_s32(tmp, tmp); break;
5962                             default: abort();
5963                             }
5964                             break;
5965                         case NEON_2RM_VCLZ:
5966                             switch (size) {
5967                             case 0: gen_helper_neon_clz_u8(tmp, tmp); break;
5968                             case 1: gen_helper_neon_clz_u16(tmp, tmp); break;
5969                             case 2: gen_helper_clz(tmp, tmp); break;
5970                             default: abort();
5971                             }
5972                             break;
5973                         case NEON_2RM_VCNT:
5974                             gen_helper_neon_cnt_u8(tmp, tmp);
5975                             break;
5976                         case NEON_2RM_VMVN:
5977                             tcg_gen_not_i32(tmp, tmp);
5978                             break;
5979                         case NEON_2RM_VQABS:
5980                             switch (size) {
5981                             case 0:
5982                                 gen_helper_neon_qabs_s8(tmp, cpu_env, tmp);
5983                                 break;
5984                             case 1:
5985                                 gen_helper_neon_qabs_s16(tmp, cpu_env, tmp);
5986                                 break;
5987                             case 2:
5988                                 gen_helper_neon_qabs_s32(tmp, cpu_env, tmp);
5989                                 break;
5990                             default: abort();
5991                             }
5992                             break;
5993                         case NEON_2RM_VQNEG:
5994                             switch (size) {
5995                             case 0:
5996                                 gen_helper_neon_qneg_s8(tmp, cpu_env, tmp);
5997                                 break;
5998                             case 1:
5999                                 gen_helper_neon_qneg_s16(tmp, cpu_env, tmp);
6000                                 break;
6001                             case 2:
6002                                 gen_helper_neon_qneg_s32(tmp, cpu_env, tmp);
6003                                 break;
6004                             default: abort();
6005                             }
6006                             break;
6007                         case NEON_2RM_VCGT0: case NEON_2RM_VCLE0:
6008                             tmp2 = tcg_const_i32(0);
6009                             switch(size) {
6010                             case 0: gen_helper_neon_cgt_s8(tmp, tmp, tmp2); break;
6011                             case 1: gen_helper_neon_cgt_s16(tmp, tmp, tmp2); break;
6012                             case 2: gen_helper_neon_cgt_s32(tmp, tmp, tmp2); break;
6013                             default: abort();
6014                             }
6015                             tcg_temp_free(tmp2);
6016                             if (op == NEON_2RM_VCLE0) {
6017                                 tcg_gen_not_i32(tmp, tmp);
6018                             }
6019                             break;
6020                         case NEON_2RM_VCGE0: case NEON_2RM_VCLT0:
6021                             tmp2 = tcg_const_i32(0);
6022                             switch(size) {
6023                             case 0: gen_helper_neon_cge_s8(tmp, tmp, tmp2); break;
6024                             case 1: gen_helper_neon_cge_s16(tmp, tmp, tmp2); break;
6025                             case 2: gen_helper_neon_cge_s32(tmp, tmp, tmp2); break;
6026                             default: abort();
6027                             }
6028                             tcg_temp_free(tmp2);
6029                             if (op == NEON_2RM_VCLT0) {
6030                                 tcg_gen_not_i32(tmp, tmp);
6031                             }
6032                             break;
6033                         case NEON_2RM_VCEQ0:
6034                             tmp2 = tcg_const_i32(0);
6035                             switch(size) {
6036                             case 0: gen_helper_neon_ceq_u8(tmp, tmp, tmp2); break;
6037                             case 1: gen_helper_neon_ceq_u16(tmp, tmp, tmp2); break;
6038                             case 2: gen_helper_neon_ceq_u32(tmp, tmp, tmp2); break;
6039                             default: abort();
6040                             }
6041                             tcg_temp_free(tmp2);
6042                             break;
6043                         case NEON_2RM_VABS:
6044                             switch(size) {
6045                             case 0: gen_helper_neon_abs_s8(tmp, tmp); break;
6046                             case 1: gen_helper_neon_abs_s16(tmp, tmp); break;
6047                             case 2: tcg_gen_abs_i32(tmp, tmp); break;
6048                             default: abort();
6049                             }
6050                             break;
6051                         case NEON_2RM_VNEG:
6052                             tmp2 = tcg_const_i32(0);
6053                             gen_neon_rsb(size, tmp, tmp2);
6054                             tcg_temp_free(tmp2);
6055                             break;
6056                         case NEON_2RM_VCGT0_F:
6057                         {
6058                             TCGv_ptr fpstatus = get_fpstatus_ptr(1);
6059                             tmp2 = tcg_const_i32(0);
6060                             gen_helper_neon_cgt_f32(tmp, tmp, tmp2, fpstatus);
6061                             tcg_temp_free(tmp2);
6062                             tcg_temp_free_ptr(fpstatus);
6063                             break;
6064                         }
6065                         case NEON_2RM_VCGE0_F:
6066                         {
6067                             TCGv_ptr fpstatus = get_fpstatus_ptr(1);
6068                             tmp2 = tcg_const_i32(0);
6069                             gen_helper_neon_cge_f32(tmp, tmp, tmp2, fpstatus);
6070                             tcg_temp_free(tmp2);
6071                             tcg_temp_free_ptr(fpstatus);
6072                             break;
6073                         }
6074                         case NEON_2RM_VCEQ0_F:
6075                         {
6076                             TCGv_ptr fpstatus = get_fpstatus_ptr(1);
6077                             tmp2 = tcg_const_i32(0);
6078                             gen_helper_neon_ceq_f32(tmp, tmp, tmp2, fpstatus);
6079                             tcg_temp_free(tmp2);
6080                             tcg_temp_free_ptr(fpstatus);
6081                             break;
6082                         }
6083                         case NEON_2RM_VCLE0_F:
6084                         {
6085                             TCGv_ptr fpstatus = get_fpstatus_ptr(1);
6086                             tmp2 = tcg_const_i32(0);
6087                             gen_helper_neon_cge_f32(tmp, tmp2, tmp, fpstatus);
6088                             tcg_temp_free(tmp2);
6089                             tcg_temp_free_ptr(fpstatus);
6090                             break;
6091                         }
6092                         case NEON_2RM_VCLT0_F:
6093                         {
6094                             TCGv_ptr fpstatus = get_fpstatus_ptr(1);
6095                             tmp2 = tcg_const_i32(0);
6096                             gen_helper_neon_cgt_f32(tmp, tmp2, tmp, fpstatus);
6097                             tcg_temp_free(tmp2);
6098                             tcg_temp_free_ptr(fpstatus);
6099                             break;
6100                         }
6101                         case NEON_2RM_VABS_F:
6102                             gen_vfp_abs(0);
6103                             break;
6104                         case NEON_2RM_VNEG_F:
6105                             gen_vfp_neg(0);
6106                             break;
6107                         case NEON_2RM_VSWP:
6108                             tmp2 = neon_load_reg(rd, pass);
6109                             neon_store_reg(rm, pass, tmp2);
6110                             break;
6111                         case NEON_2RM_VTRN:
6112                             tmp2 = neon_load_reg(rd, pass);
6113                             switch (size) {
6114                             case 0: gen_neon_trn_u8(tmp, tmp2); break;
6115                             case 1: gen_neon_trn_u16(tmp, tmp2); break;
6116                             default: abort();
6117                             }
6118                             neon_store_reg(rm, pass, tmp2);
6119                             break;
6120                         case NEON_2RM_VRECPE:
6121                             gen_helper_recpe_u32(tmp, tmp, cpu_env);
6122                             break;
6123                         case NEON_2RM_VRSQRTE:
6124                             gen_helper_rsqrte_u32(tmp, tmp, cpu_env);
6125                             break;
6126                         case NEON_2RM_VRECPE_F:
6127                             gen_helper_recpe_f32(cpu_F0s, cpu_F0s, cpu_env);
6128                             break;
6129                         case NEON_2RM_VRSQRTE_F:
6130                             gen_helper_rsqrte_f32(cpu_F0s, cpu_F0s, cpu_env);
6131                             break;
6132                         case NEON_2RM_VCVT_FS: /* VCVT.F32.S32 */
6133                             gen_vfp_sito(0, 1);
6134                             break;
6135                         case NEON_2RM_VCVT_FU: /* VCVT.F32.U32 */
6136                             gen_vfp_uito(0, 1);
6137                             break;
6138                         case NEON_2RM_VCVT_SF: /* VCVT.S32.F32 */
6139                             gen_vfp_tosiz(0, 1);
6140                             break;
6141                         case NEON_2RM_VCVT_UF: /* VCVT.U32.F32 */
6142                             gen_vfp_touiz(0, 1);
6143                             break;
6144                         default:
6145                             /* Reserved op values were caught by the
6146                              * neon_2rm_sizes[] check earlier.
6147                              */
6148                             abort();
6149                         }
6150                         if (neon_2rm_is_float_op(op)) {
6151                             tcg_gen_st_f32(cpu_F0s, cpu_env,
6152                                            neon_reg_offset(rd, pass));
6153                         } else {
6154                             neon_store_reg(rd, pass, tmp);
6155                         }
6156                     }
6157                     break;
6158                 }
6159             } else if ((insn & (1 << 10)) == 0) {
6160                 /* VTBL, VTBX.  */
6161                 int n = ((insn >> 8) & 3) + 1;
6162                 if ((rn + n) > 32) {
6163                     /* This is UNPREDICTABLE; we choose to UNDEF to avoid the
6164                      * helper function running off the end of the register file.
6165                      */
6166                     return 1;
6167                 }
6168                 n <<= 3;
6169                 if (insn & (1 << 6)) {
6170                     tmp = neon_load_reg(rd, 0);
6171                 } else {
6172                     tmp = tcg_temp_new_i32();
6173                     tcg_gen_movi_i32(tmp, 0);
6174                 }
6175                 tmp2 = neon_load_reg(rm, 0);
6176                 tmp4 = tcg_const_i32(rn);
6177                 tmp5 = tcg_const_i32(n);
6178                 gen_helper_neon_tbl(tmp2, tmp2, tmp, tmp4, tmp5);
6179                 tcg_temp_free_i32(tmp);
6180                 if (insn & (1 << 6)) {
6181                     tmp = neon_load_reg(rd, 1);
6182                 } else {
6183                     tmp = tcg_temp_new_i32();
6184                     tcg_gen_movi_i32(tmp, 0);
6185                 }
6186                 tmp3 = neon_load_reg(rm, 1);
6187                 gen_helper_neon_tbl(tmp3, tmp3, tmp, tmp4, tmp5);
6188                 tcg_temp_free_i32(tmp5);
6189                 tcg_temp_free_i32(tmp4);
6190                 neon_store_reg(rd, 0, tmp2);
6191                 neon_store_reg(rd, 1, tmp3);
6192                 tcg_temp_free_i32(tmp);
6193             } else if ((insn & 0x380) == 0) {
6194                 /* VDUP */
6195                 if ((insn & (7 << 16)) == 0 || (q && (rd & 1))) {
6196                     return 1;
6197                 }
6198                 if (insn & (1 << 19)) {
6199                     tmp = neon_load_reg(rm, 1);
6200                 } else {
6201                     tmp = neon_load_reg(rm, 0);
6202                 }
6203                 if (insn & (1 << 16)) {
6204                     gen_neon_dup_u8(tmp, ((insn >> 17) & 3) * 8);
6205                 } else if (insn & (1 << 17)) {
6206                     if ((insn >> 18) & 1)
6207                         gen_neon_dup_high16(tmp);
6208                     else
6209                         gen_neon_dup_low16(tmp);
6210                 }
6211                 for (pass = 0; pass < (q ? 4 : 2); pass++) {
6212                     tmp2 = tcg_temp_new_i32();
6213                     tcg_gen_mov_i32(tmp2, tmp);
6214                     neon_store_reg(rd, pass, tmp2);
6215                 }
6216                 tcg_temp_free_i32(tmp);
6217             } else {
6218                 return 1;
6219             }
6220         }
6221     }
6222     return 0;
6223 }
6224
6225 static int disas_coproc_insn(CPUARMState * env, DisasContext *s, uint32_t insn)
6226 {
6227     int cpnum, is64, crn, crm, opc1, opc2, isread, rt, rt2;
6228     const ARMCPRegInfo *ri;
6229     ARMCPU *cpu = arm_env_get_cpu(env);
6230
6231     cpnum = (insn >> 8) & 0xf;
6232     if (arm_feature(env, ARM_FEATURE_XSCALE)
6233             && ((env->cp15.c15_cpar ^ 0x3fff) & (1 << cpnum)))
6234         return 1;
6235
6236     /* First check for coprocessor space used for actual instructions */
6237     switch (cpnum) {
6238       case 0:
6239       case 1:
6240         if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
6241             return disas_iwmmxt_insn(env, s, insn);
6242         } else if (arm_feature(env, ARM_FEATURE_XSCALE)) {
6243             return disas_dsp_insn(env, s, insn);
6244         }
6245         return 1;
6246     case 10:
6247     case 11:
6248         return disas_vfp_insn (env, s, insn);
6249     default:
6250         break;
6251     }
6252
6253     /* Otherwise treat as a generic register access */
6254     is64 = (insn & (1 << 25)) == 0;
6255     if (!is64 && ((insn & (1 << 4)) == 0)) {
6256         /* cdp */
6257         return 1;
6258     }
6259
6260     crm = insn & 0xf;
6261     if (is64) {
6262         crn = 0;
6263         opc1 = (insn >> 4) & 0xf;
6264         opc2 = 0;
6265         rt2 = (insn >> 16) & 0xf;
6266     } else {
6267         crn = (insn >> 16) & 0xf;
6268         opc1 = (insn >> 21) & 7;
6269         opc2 = (insn >> 5) & 7;
6270         rt2 = 0;
6271     }
6272     isread = (insn >> 20) & 1;
6273     rt = (insn >> 12) & 0xf;
6274
6275     ri = get_arm_cp_reginfo(cpu,
6276                             ENCODE_CP_REG(cpnum, is64, crn, crm, opc1, opc2));
6277     if (ri) {
6278         /* Check access permissions */
6279         if (!cp_access_ok(env, ri, isread)) {
6280             return 1;
6281         }
6282
6283         /* Handle special cases first */
6284         switch (ri->type & ~(ARM_CP_FLAG_MASK & ~ARM_CP_SPECIAL)) {
6285         case ARM_CP_NOP:
6286             return 0;
6287         case ARM_CP_WFI:
6288             if (isread) {
6289                 return 1;
6290             }
6291             gen_set_pc_im(s->pc);
6292             s->is_jmp = DISAS_WFI;
6293             break;
6294         default:
6295             break;
6296         }
6297
6298         if (isread) {
6299             /* Read */
6300             if (is64) {
6301                 TCGv_i64 tmp64;
6302                 TCGv_i32 tmp;
6303                 if (ri->type & ARM_CP_CONST) {
6304                     tmp64 = tcg_const_i64(ri->resetvalue);
6305                 } else if (ri->readfn) {
6306                     TCGv_ptr tmpptr;
6307                     gen_set_pc_im(s->pc);
6308                     tmp64 = tcg_temp_new_i64();
6309                     tmpptr = tcg_const_ptr(ri);
6310                     gen_helper_get_cp_reg64(tmp64, cpu_env, tmpptr);
6311                     tcg_temp_free_ptr(tmpptr);
6312                 } else {
6313                     tmp64 = tcg_temp_new_i64();
6314                     tcg_gen_ld_i64(tmp64, cpu_env, ri->fieldoffset);
6315                 }
6316                 tmp = tcg_temp_new_i32();
6317                 tcg_gen_trunc_i64_i32(tmp, tmp64);
6318                 store_reg(s, rt, tmp);
6319                 tcg_gen_shri_i64(tmp64, tmp64, 32);
6320                 tcg_gen_trunc_i64_i32(tmp, tmp64);
6321                 store_reg(s, rt2, tmp);
6322             } else {
6323                 TCGv tmp;
6324                 if (ri->type & ARM_CP_CONST) {
6325                     tmp = tcg_const_i32(ri->resetvalue);
6326                 } else if (ri->readfn) {
6327                     TCGv_ptr tmpptr;
6328                     gen_set_pc_im(s->pc);
6329                     tmp = tcg_temp_new_i32();
6330                     tmpptr = tcg_const_ptr(ri);
6331                     gen_helper_get_cp_reg(tmp, cpu_env, tmpptr);
6332                     tcg_temp_free_ptr(tmpptr);
6333                 } else {
6334                     tmp = load_cpu_offset(ri->fieldoffset);
6335                 }
6336                 if (rt == 15) {
6337                     /* Destination register of r15 for 32 bit loads sets
6338                      * the condition codes from the high 4 bits of the value
6339                      */
6340                     gen_set_nzcv(tmp);
6341                     tcg_temp_free_i32(tmp);
6342                 } else {
6343                     store_reg(s, rt, tmp);
6344                 }
6345             }
6346         } else {
6347             /* Write */
6348             if (ri->type & ARM_CP_CONST) {
6349                 /* If not forbidden by access permissions, treat as WI */
6350                 return 0;
6351             }
6352
6353             if (is64) {
6354                 TCGv tmplo, tmphi;
6355                 TCGv_i64 tmp64 = tcg_temp_new_i64();
6356                 tmplo = load_reg(s, rt);
6357                 tmphi = load_reg(s, rt2);
6358                 tcg_gen_concat_i32_i64(tmp64, tmplo, tmphi);
6359                 tcg_temp_free_i32(tmplo);
6360                 tcg_temp_free_i32(tmphi);
6361                 if (ri->writefn) {
6362                     TCGv_ptr tmpptr = tcg_const_ptr(ri);
6363                     gen_set_pc_im(s->pc);
6364                     gen_helper_set_cp_reg64(cpu_env, tmpptr, tmp64);
6365                     tcg_temp_free_ptr(tmpptr);
6366                 } else {
6367                     tcg_gen_st_i64(tmp64, cpu_env, ri->fieldoffset);
6368                 }
6369                 tcg_temp_free_i64(tmp64);
6370             } else {
6371                 if (ri->writefn) {
6372                     TCGv tmp;
6373                     TCGv_ptr tmpptr;
6374                     gen_set_pc_im(s->pc);
6375                     tmp = load_reg(s, rt);
6376                     tmpptr = tcg_const_ptr(ri);
6377                     gen_helper_set_cp_reg(cpu_env, tmpptr, tmp);
6378                     tcg_temp_free_ptr(tmpptr);
6379                     tcg_temp_free_i32(tmp);
6380                 } else {
6381                     TCGv tmp = load_reg(s, rt);
6382                     store_cpu_offset(tmp, ri->fieldoffset);
6383                 }
6384             }
6385             /* We default to ending the TB on a coprocessor register write,
6386              * but allow this to be suppressed by the register definition
6387              * (usually only necessary to work around guest bugs).
6388              */
6389             if (!(ri->type & ARM_CP_SUPPRESS_TB_END)) {
6390                 gen_lookup_tb(s);
6391             }
6392         }
6393         return 0;
6394     }
6395
6396     /* Fallback code: handle coprocessor registers not yet converted
6397      * to ARMCPRegInfo.
6398      */
6399     switch (cpnum) {
6400     case 15:
6401         return disas_cp15_insn (env, s, insn);
6402     default:
6403         return 1;
6404     }
6405 }
6406
6407
6408 /* Store a 64-bit value to a register pair.  Clobbers val.  */
6409 static void gen_storeq_reg(DisasContext *s, int rlow, int rhigh, TCGv_i64 val)
6410 {
6411     TCGv tmp;
6412     tmp = tcg_temp_new_i32();
6413     tcg_gen_trunc_i64_i32(tmp, val);
6414     store_reg(s, rlow, tmp);
6415     tmp = tcg_temp_new_i32();
6416     tcg_gen_shri_i64(val, val, 32);
6417     tcg_gen_trunc_i64_i32(tmp, val);
6418     store_reg(s, rhigh, tmp);
6419 }
6420
6421 /* load a 32-bit value from a register and perform a 64-bit accumulate.  */
6422 static void gen_addq_lo(DisasContext *s, TCGv_i64 val, int rlow)
6423 {
6424     TCGv_i64 tmp;
6425     TCGv tmp2;
6426
6427     /* Load value and extend to 64 bits.  */
6428     tmp = tcg_temp_new_i64();
6429     tmp2 = load_reg(s, rlow);
6430     tcg_gen_extu_i32_i64(tmp, tmp2);
6431     tcg_temp_free_i32(tmp2);
6432     tcg_gen_add_i64(val, val, tmp);
6433     tcg_temp_free_i64(tmp);
6434 }
6435
6436 /* load and add a 64-bit value from a register pair.  */
6437 static void gen_addq(DisasContext *s, TCGv_i64 val, int rlow, int rhigh)
6438 {
6439     TCGv_i64 tmp;
6440     TCGv tmpl;
6441     TCGv tmph;
6442
6443     /* Load 64-bit value rd:rn.  */
6444     tmpl = load_reg(s, rlow);
6445     tmph = load_reg(s, rhigh);
6446     tmp = tcg_temp_new_i64();
6447     tcg_gen_concat_i32_i64(tmp, tmpl, tmph);
6448     tcg_temp_free_i32(tmpl);
6449     tcg_temp_free_i32(tmph);
6450     tcg_gen_add_i64(val, val, tmp);
6451     tcg_temp_free_i64(tmp);
6452 }
6453
6454 /* Set N and Z flags from a 64-bit value.  */
6455 static void gen_logicq_cc(TCGv_i64 val)
6456 {
6457     TCGv tmp = tcg_temp_new_i32();
6458     gen_helper_logicq_cc(tmp, val);
6459     gen_logic_CC(tmp);
6460     tcg_temp_free_i32(tmp);
6461 }
6462
6463 /* Load/Store exclusive instructions are implemented by remembering
6464    the value/address loaded, and seeing if these are the same
6465    when the store is performed. This should be is sufficient to implement
6466    the architecturally mandated semantics, and avoids having to monitor
6467    regular stores.
6468
6469    In system emulation mode only one CPU will be running at once, so
6470    this sequence is effectively atomic.  In user emulation mode we
6471    throw an exception and handle the atomic operation elsewhere.  */
6472 static void gen_load_exclusive(DisasContext *s, int rt, int rt2,
6473                                TCGv addr, int size)
6474 {
6475     TCGv tmp;
6476
6477     switch (size) {
6478     case 0:
6479         tmp = gen_ld8u(addr, IS_USER(s));
6480         break;
6481     case 1:
6482         tmp = gen_ld16u(addr, IS_USER(s));
6483         break;
6484     case 2:
6485     case 3:
6486         tmp = gen_ld32(addr, IS_USER(s));
6487         break;
6488     default:
6489         abort();
6490     }
6491     tcg_gen_mov_i32(cpu_exclusive_val, tmp);
6492     store_reg(s, rt, tmp);
6493     if (size == 3) {
6494         TCGv tmp2 = tcg_temp_new_i32();
6495         tcg_gen_addi_i32(tmp2, addr, 4);
6496         tmp = gen_ld32(tmp2, IS_USER(s));
6497         tcg_temp_free_i32(tmp2);
6498         tcg_gen_mov_i32(cpu_exclusive_high, tmp);
6499         store_reg(s, rt2, tmp);
6500     }
6501     tcg_gen_mov_i32(cpu_exclusive_addr, addr);
6502 }
6503
6504 static void gen_clrex(DisasContext *s)
6505 {
6506     tcg_gen_movi_i32(cpu_exclusive_addr, -1);
6507 }
6508
6509 #ifdef CONFIG_USER_ONLY
6510 static void gen_store_exclusive(DisasContext *s, int rd, int rt, int rt2,
6511                                 TCGv addr, int size)
6512 {
6513     tcg_gen_mov_i32(cpu_exclusive_test, addr);
6514     tcg_gen_movi_i32(cpu_exclusive_info,
6515                      size | (rd << 4) | (rt << 8) | (rt2 << 12));
6516     gen_exception_insn(s, 4, EXCP_STREX);
6517 }
6518 #else
6519 static void gen_store_exclusive(DisasContext *s, int rd, int rt, int rt2,
6520                                 TCGv addr, int size)
6521 {
6522     TCGv tmp;
6523     int done_label;
6524     int fail_label;
6525
6526     /* if (env->exclusive_addr == addr && env->exclusive_val == [addr]) {
6527          [addr] = {Rt};
6528          {Rd} = 0;
6529        } else {
6530          {Rd} = 1;
6531        } */
6532     fail_label = gen_new_label();
6533     done_label = gen_new_label();
6534     tcg_gen_brcond_i32(TCG_COND_NE, addr, cpu_exclusive_addr, fail_label);
6535     switch (size) {
6536     case 0:
6537         tmp = gen_ld8u(addr, IS_USER(s));
6538         break;
6539     case 1:
6540         tmp = gen_ld16u(addr, IS_USER(s));
6541         break;
6542     case 2:
6543     case 3:
6544         tmp = gen_ld32(addr, IS_USER(s));
6545         break;
6546     default:
6547         abort();
6548     }
6549     tcg_gen_brcond_i32(TCG_COND_NE, tmp, cpu_exclusive_val, fail_label);
6550     tcg_temp_free_i32(tmp);
6551     if (size == 3) {
6552         TCGv tmp2 = tcg_temp_new_i32();
6553         tcg_gen_addi_i32(tmp2, addr, 4);
6554         tmp = gen_ld32(tmp2, IS_USER(s));
6555         tcg_temp_free_i32(tmp2);
6556         tcg_gen_brcond_i32(TCG_COND_NE, tmp, cpu_exclusive_high, fail_label);
6557         tcg_temp_free_i32(tmp);
6558     }
6559     tmp = load_reg(s, rt);
6560     switch (size) {
6561     case 0:
6562         gen_st8(tmp, addr, IS_USER(s));
6563         break;
6564     case 1:
6565         gen_st16(tmp, addr, IS_USER(s));
6566         break;
6567     case 2:
6568     case 3:
6569         gen_st32(tmp, addr, IS_USER(s));
6570         break;
6571     default:
6572         abort();
6573     }
6574     if (size == 3) {
6575         tcg_gen_addi_i32(addr, addr, 4);
6576         tmp = load_reg(s, rt2);
6577         gen_st32(tmp, addr, IS_USER(s));
6578     }
6579     tcg_gen_movi_i32(cpu_R[rd], 0);
6580     tcg_gen_br(done_label);
6581     gen_set_label(fail_label);
6582     tcg_gen_movi_i32(cpu_R[rd], 1);
6583     gen_set_label(done_label);
6584     tcg_gen_movi_i32(cpu_exclusive_addr, -1);
6585 }
6586 #endif
6587
6588 static void disas_arm_insn(CPUARMState * env, DisasContext *s)
6589 {
6590     unsigned int cond, insn, val, op1, i, shift, rm, rs, rn, rd, sh;
6591     TCGv tmp;
6592     TCGv tmp2;
6593     TCGv tmp3;
6594     TCGv addr;
6595     TCGv_i64 tmp64;
6596
6597     insn = arm_ldl_code(s->pc, s->bswap_code);
6598     s->pc += 4;
6599
6600     /* M variants do not implement ARM mode.  */
6601     if (IS_M(env))
6602         goto illegal_op;
6603     cond = insn >> 28;
6604     if (cond == 0xf){
6605         /* In ARMv3 and v4 the NV condition is UNPREDICTABLE; we
6606          * choose to UNDEF. In ARMv5 and above the space is used
6607          * for miscellaneous unconditional instructions.
6608          */
6609         ARCH(5);
6610
6611         /* Unconditional instructions.  */
6612         if (((insn >> 25) & 7) == 1) {
6613             /* NEON Data processing.  */
6614             if (!arm_feature(env, ARM_FEATURE_NEON))
6615                 goto illegal_op;
6616
6617             if (disas_neon_data_insn(env, s, insn))
6618                 goto illegal_op;
6619             return;
6620         }
6621         if ((insn & 0x0f100000) == 0x04000000) {
6622             /* NEON load/store.  */
6623             if (!arm_feature(env, ARM_FEATURE_NEON))
6624                 goto illegal_op;
6625
6626             if (disas_neon_ls_insn(env, s, insn))
6627                 goto illegal_op;
6628             return;
6629         }
6630         if (((insn & 0x0f30f000) == 0x0510f000) ||
6631             ((insn & 0x0f30f010) == 0x0710f000)) {
6632             if ((insn & (1 << 22)) == 0) {
6633                 /* PLDW; v7MP */
6634                 if (!arm_feature(env, ARM_FEATURE_V7MP)) {
6635                     goto illegal_op;
6636                 }
6637             }
6638             /* Otherwise PLD; v5TE+ */
6639             ARCH(5TE);
6640             return;
6641         }
6642         if (((insn & 0x0f70f000) == 0x0450f000) ||
6643             ((insn & 0x0f70f010) == 0x0650f000)) {
6644             ARCH(7);
6645             return; /* PLI; V7 */
6646         }
6647         if (((insn & 0x0f700000) == 0x04100000) ||
6648             ((insn & 0x0f700010) == 0x06100000)) {
6649             if (!arm_feature(env, ARM_FEATURE_V7MP)) {
6650                 goto illegal_op;
6651             }
6652             return; /* v7MP: Unallocated memory hint: must NOP */
6653         }
6654
6655         if ((insn & 0x0ffffdff) == 0x01010000) {
6656             ARCH(6);
6657             /* setend */
6658             if (((insn >> 9) & 1) != s->bswap_code) {
6659                 /* Dynamic endianness switching not implemented. */
6660                 goto illegal_op;
6661             }
6662             return;
6663         } else if ((insn & 0x0fffff00) == 0x057ff000) {
6664             switch ((insn >> 4) & 0xf) {
6665             case 1: /* clrex */
6666                 ARCH(6K);
6667                 gen_clrex(s);
6668                 return;
6669             case 4: /* dsb */
6670             case 5: /* dmb */
6671             case 6: /* isb */
6672                 ARCH(7);
6673                 /* We don't emulate caches so these are a no-op.  */
6674                 return;
6675             default:
6676                 goto illegal_op;
6677             }
6678         } else if ((insn & 0x0e5fffe0) == 0x084d0500) {
6679             /* srs */
6680             int32_t offset;
6681             if (IS_USER(s))
6682                 goto illegal_op;
6683             ARCH(6);
6684             op1 = (insn & 0x1f);
6685             addr = tcg_temp_new_i32();
6686             tmp = tcg_const_i32(op1);
6687             gen_helper_get_r13_banked(addr, cpu_env, tmp);
6688             tcg_temp_free_i32(tmp);
6689             i = (insn >> 23) & 3;
6690             switch (i) {
6691             case 0: offset = -4; break; /* DA */
6692             case 1: offset = 0; break; /* IA */
6693             case 2: offset = -8; break; /* DB */
6694             case 3: offset = 4; break; /* IB */
6695             default: abort();
6696             }
6697             if (offset)
6698                 tcg_gen_addi_i32(addr, addr, offset);
6699             tmp = load_reg(s, 14);
6700             gen_st32(tmp, addr, 0);
6701             tmp = load_cpu_field(spsr);
6702             tcg_gen_addi_i32(addr, addr, 4);
6703             gen_st32(tmp, addr, 0);
6704             if (insn & (1 << 21)) {
6705                 /* Base writeback.  */
6706                 switch (i) {
6707                 case 0: offset = -8; break;
6708                 case 1: offset = 4; break;
6709                 case 2: offset = -4; break;
6710                 case 3: offset = 0; break;
6711                 default: abort();
6712                 }
6713                 if (offset)
6714                     tcg_gen_addi_i32(addr, addr, offset);
6715                 tmp = tcg_const_i32(op1);
6716                 gen_helper_set_r13_banked(cpu_env, tmp, addr);
6717                 tcg_temp_free_i32(tmp);
6718                 tcg_temp_free_i32(addr);
6719             } else {
6720                 tcg_temp_free_i32(addr);
6721             }
6722             return;
6723         } else if ((insn & 0x0e50ffe0) == 0x08100a00) {
6724             /* rfe */
6725             int32_t offset;
6726             if (IS_USER(s))
6727                 goto illegal_op;
6728             ARCH(6);
6729             rn = (insn >> 16) & 0xf;
6730             addr = load_reg(s, rn);
6731             i = (insn >> 23) & 3;
6732             switch (i) {
6733             case 0: offset = -4; break; /* DA */
6734             case 1: offset = 0; break; /* IA */
6735             case 2: offset = -8; break; /* DB */
6736             case 3: offset = 4; break; /* IB */
6737             default: abort();
6738             }
6739             if (offset)
6740                 tcg_gen_addi_i32(addr, addr, offset);
6741             /* Load PC into tmp and CPSR into tmp2.  */
6742             tmp = gen_ld32(addr, 0);
6743             tcg_gen_addi_i32(addr, addr, 4);
6744             tmp2 = gen_ld32(addr, 0);
6745             if (insn & (1 << 21)) {
6746                 /* Base writeback.  */
6747                 switch (i) {
6748                 case 0: offset = -8; break;
6749                 case 1: offset = 4; break;
6750                 case 2: offset = -4; break;
6751                 case 3: offset = 0; break;
6752                 default: abort();
6753                 }
6754                 if (offset)
6755                     tcg_gen_addi_i32(addr, addr, offset);
6756                 store_reg(s, rn, addr);
6757             } else {
6758                 tcg_temp_free_i32(addr);
6759             }
6760             gen_rfe(s, tmp, tmp2);
6761             return;
6762         } else if ((insn & 0x0e000000) == 0x0a000000) {
6763             /* branch link and change to thumb (blx <offset>) */
6764             int32_t offset;
6765
6766             val = (uint32_t)s->pc;
6767             tmp = tcg_temp_new_i32();
6768             tcg_gen_movi_i32(tmp, val);
6769             store_reg(s, 14, tmp);
6770             /* Sign-extend the 24-bit offset */
6771             offset = (((int32_t)insn) << 8) >> 8;
6772             /* offset * 4 + bit24 * 2 + (thumb bit) */
6773             val += (offset << 2) | ((insn >> 23) & 2) | 1;
6774             /* pipeline offset */
6775             val += 4;
6776             /* protected by ARCH(5); above, near the start of uncond block */
6777             gen_bx_im(s, val);
6778             return;
6779         } else if ((insn & 0x0e000f00) == 0x0c000100) {
6780             if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
6781                 /* iWMMXt register transfer.  */
6782                 if (env->cp15.c15_cpar & (1 << 1))
6783                     if (!disas_iwmmxt_insn(env, s, insn))
6784                         return;
6785             }
6786         } else if ((insn & 0x0fe00000) == 0x0c400000) {
6787             /* Coprocessor double register transfer.  */
6788             ARCH(5TE);
6789         } else if ((insn & 0x0f000010) == 0x0e000010) {
6790             /* Additional coprocessor register transfer.  */
6791         } else if ((insn & 0x0ff10020) == 0x01000000) {
6792             uint32_t mask;
6793             uint32_t val;
6794             /* cps (privileged) */
6795             if (IS_USER(s))
6796                 return;
6797             mask = val = 0;
6798             if (insn & (1 << 19)) {
6799                 if (insn & (1 << 8))
6800                     mask |= CPSR_A;
6801                 if (insn & (1 << 7))
6802                     mask |= CPSR_I;
6803                 if (insn & (1 << 6))
6804                     mask |= CPSR_F;
6805                 if (insn & (1 << 18))
6806                     val |= mask;
6807             }
6808             if (insn & (1 << 17)) {
6809                 mask |= CPSR_M;
6810                 val |= (insn & 0x1f);
6811             }
6812             if (mask) {
6813                 gen_set_psr_im(s, mask, 0, val);
6814             }
6815             return;
6816         }
6817         goto illegal_op;
6818     }
6819     if (cond != 0xe) {
6820         /* if not always execute, we generate a conditional jump to
6821            next instruction */
6822         s->condlabel = gen_new_label();
6823         gen_test_cc(cond ^ 1, s->condlabel);
6824         s->condjmp = 1;
6825     }
6826     if ((insn & 0x0f900000) == 0x03000000) {
6827         if ((insn & (1 << 21)) == 0) {
6828             ARCH(6T2);
6829             rd = (insn >> 12) & 0xf;
6830             val = ((insn >> 4) & 0xf000) | (insn & 0xfff);
6831             if ((insn & (1 << 22)) == 0) {
6832                 /* MOVW */
6833                 tmp = tcg_temp_new_i32();
6834                 tcg_gen_movi_i32(tmp, val);
6835             } else {
6836                 /* MOVT */
6837                 tmp = load_reg(s, rd);
6838                 tcg_gen_ext16u_i32(tmp, tmp);
6839                 tcg_gen_ori_i32(tmp, tmp, val << 16);
6840             }
6841             store_reg(s, rd, tmp);
6842         } else {
6843             if (((insn >> 12) & 0xf) != 0xf)
6844                 goto illegal_op;
6845             if (((insn >> 16) & 0xf) == 0) {
6846                 gen_nop_hint(s, insn & 0xff);
6847             } else {
6848                 /* CPSR = immediate */
6849                 val = insn & 0xff;
6850                 shift = ((insn >> 8) & 0xf) * 2;
6851                 if (shift)
6852                     val = (val >> shift) | (val << (32 - shift));
6853                 i = ((insn & (1 << 22)) != 0);
6854                 if (gen_set_psr_im(s, msr_mask(env, s, (insn >> 16) & 0xf, i), i, val))
6855                     goto illegal_op;
6856             }
6857         }
6858     } else if ((insn & 0x0f900000) == 0x01000000
6859                && (insn & 0x00000090) != 0x00000090) {
6860         /* miscellaneous instructions */
6861         op1 = (insn >> 21) & 3;
6862         sh = (insn >> 4) & 0xf;
6863         rm = insn & 0xf;
6864         switch (sh) {
6865         case 0x0: /* move program status register */
6866             if (op1 & 1) {
6867                 /* PSR = reg */
6868                 tmp = load_reg(s, rm);
6869                 i = ((op1 & 2) != 0);
6870                 if (gen_set_psr(s, msr_mask(env, s, (insn >> 16) & 0xf, i), i, tmp))
6871                     goto illegal_op;
6872             } else {
6873                 /* reg = PSR */
6874                 rd = (insn >> 12) & 0xf;
6875                 if (op1 & 2) {
6876                     if (IS_USER(s))
6877                         goto illegal_op;
6878                     tmp = load_cpu_field(spsr);
6879                 } else {
6880                     tmp = tcg_temp_new_i32();
6881                     gen_helper_cpsr_read(tmp);
6882                 }
6883                 store_reg(s, rd, tmp);
6884             }
6885             break;
6886         case 0x1:
6887             if (op1 == 1) {
6888                 /* branch/exchange thumb (bx).  */
6889                 ARCH(4T);
6890                 tmp = load_reg(s, rm);
6891                 gen_bx(s, tmp);
6892             } else if (op1 == 3) {
6893                 /* clz */
6894                 ARCH(5);
6895                 rd = (insn >> 12) & 0xf;
6896                 tmp = load_reg(s, rm);
6897                 gen_helper_clz(tmp, tmp);
6898                 store_reg(s, rd, tmp);
6899             } else {
6900                 goto illegal_op;
6901             }
6902             break;
6903         case 0x2:
6904             if (op1 == 1) {
6905                 ARCH(5J); /* bxj */
6906                 /* Trivial implementation equivalent to bx.  */
6907                 tmp = load_reg(s, rm);
6908                 gen_bx(s, tmp);
6909             } else {
6910                 goto illegal_op;
6911             }
6912             break;
6913         case 0x3:
6914             if (op1 != 1)
6915               goto illegal_op;
6916
6917             ARCH(5);
6918             /* branch link/exchange thumb (blx) */
6919             tmp = load_reg(s, rm);
6920             tmp2 = tcg_temp_new_i32();
6921             tcg_gen_movi_i32(tmp2, s->pc);
6922             store_reg(s, 14, tmp2);
6923             gen_bx(s, tmp);
6924             break;
6925         case 0x5: /* saturating add/subtract */
6926             ARCH(5TE);
6927             rd = (insn >> 12) & 0xf;
6928             rn = (insn >> 16) & 0xf;
6929             tmp = load_reg(s, rm);
6930             tmp2 = load_reg(s, rn);
6931             if (op1 & 2)
6932                 gen_helper_double_saturate(tmp2, tmp2);
6933             if (op1 & 1)
6934                 gen_helper_sub_saturate(tmp, tmp, tmp2);
6935             else
6936                 gen_helper_add_saturate(tmp, tmp, tmp2);
6937             tcg_temp_free_i32(tmp2);
6938             store_reg(s, rd, tmp);
6939             break;
6940         case 7:
6941             /* SMC instruction (op1 == 3)
6942                and undefined instructions (op1 == 0 || op1 == 2)
6943                will trap */
6944             if (op1 != 1) {
6945                 goto illegal_op;
6946             }
6947             /* bkpt */
6948             ARCH(5);
6949             gen_exception_insn(s, 4, EXCP_BKPT);
6950             break;
6951         case 0x8: /* signed multiply */
6952         case 0xa:
6953         case 0xc:
6954         case 0xe:
6955             ARCH(5TE);
6956             rs = (insn >> 8) & 0xf;
6957             rn = (insn >> 12) & 0xf;
6958             rd = (insn >> 16) & 0xf;
6959             if (op1 == 1) {
6960                 /* (32 * 16) >> 16 */
6961                 tmp = load_reg(s, rm);
6962                 tmp2 = load_reg(s, rs);
6963                 if (sh & 4)
6964                     tcg_gen_sari_i32(tmp2, tmp2, 16);
6965                 else
6966                     gen_sxth(tmp2);
6967                 tmp64 = gen_muls_i64_i32(tmp, tmp2);
6968                 tcg_gen_shri_i64(tmp64, tmp64, 16);
6969                 tmp = tcg_temp_new_i32();
6970                 tcg_gen_trunc_i64_i32(tmp, tmp64);
6971                 tcg_temp_free_i64(tmp64);
6972                 if ((sh & 2) == 0) {
6973                     tmp2 = load_reg(s, rn);
6974                     gen_helper_add_setq(tmp, tmp, tmp2);
6975                     tcg_temp_free_i32(tmp2);
6976                 }
6977                 store_reg(s, rd, tmp);
6978             } else {
6979                 /* 16 * 16 */
6980                 tmp = load_reg(s, rm);
6981                 tmp2 = load_reg(s, rs);
6982                 gen_mulxy(tmp, tmp2, sh & 2, sh & 4);
6983                 tcg_temp_free_i32(tmp2);
6984                 if (op1 == 2) {
6985                     tmp64 = tcg_temp_new_i64();
6986                     tcg_gen_ext_i32_i64(tmp64, tmp);
6987                     tcg_temp_free_i32(tmp);
6988                     gen_addq(s, tmp64, rn, rd);
6989                     gen_storeq_reg(s, rn, rd, tmp64);
6990                     tcg_temp_free_i64(tmp64);
6991                 } else {
6992                     if (op1 == 0) {
6993                         tmp2 = load_reg(s, rn);
6994                         gen_helper_add_setq(tmp, tmp, tmp2);
6995                         tcg_temp_free_i32(tmp2);
6996                     }
6997                     store_reg(s, rd, tmp);
6998                 }
6999             }
7000             break;
7001         default:
7002             goto illegal_op;
7003         }
7004     } else if (((insn & 0x0e000000) == 0 &&
7005                 (insn & 0x00000090) != 0x90) ||
7006                ((insn & 0x0e000000) == (1 << 25))) {
7007         int set_cc, logic_cc, shiftop;
7008
7009         op1 = (insn >> 21) & 0xf;
7010         set_cc = (insn >> 20) & 1;
7011         logic_cc = table_logic_cc[op1] & set_cc;
7012
7013         /* data processing instruction */
7014         if (insn & (1 << 25)) {
7015             /* immediate operand */
7016             val = insn & 0xff;
7017             shift = ((insn >> 8) & 0xf) * 2;
7018             if (shift) {
7019                 val = (val >> shift) | (val << (32 - shift));
7020             }
7021             tmp2 = tcg_temp_new_i32();
7022             tcg_gen_movi_i32(tmp2, val);
7023             if (logic_cc && shift) {
7024                 gen_set_CF_bit31(tmp2);
7025             }
7026         } else {
7027             /* register */
7028             rm = (insn) & 0xf;
7029             tmp2 = load_reg(s, rm);
7030             shiftop = (insn >> 5) & 3;
7031             if (!(insn & (1 << 4))) {
7032                 shift = (insn >> 7) & 0x1f;
7033                 gen_arm_shift_im(tmp2, shiftop, shift, logic_cc);
7034             } else {
7035                 rs = (insn >> 8) & 0xf;
7036                 tmp = load_reg(s, rs);
7037                 gen_arm_shift_reg(tmp2, shiftop, tmp, logic_cc);
7038             }
7039         }
7040         if (op1 != 0x0f && op1 != 0x0d) {
7041             rn = (insn >> 16) & 0xf;
7042             tmp = load_reg(s, rn);
7043         } else {
7044             TCGV_UNUSED(tmp);
7045         }
7046         rd = (insn >> 12) & 0xf;
7047         switch(op1) {
7048         case 0x00:
7049             tcg_gen_and_i32(tmp, tmp, tmp2);
7050             if (logic_cc) {
7051                 gen_logic_CC(tmp);
7052             }
7053             store_reg_bx(env, s, rd, tmp);
7054             break;
7055         case 0x01:
7056             tcg_gen_xor_i32(tmp, tmp, tmp2);
7057             if (logic_cc) {
7058                 gen_logic_CC(tmp);
7059             }
7060             store_reg_bx(env, s, rd, tmp);
7061             break;
7062         case 0x02:
7063             if (set_cc && rd == 15) {
7064                 /* SUBS r15, ... is used for exception return.  */
7065                 if (IS_USER(s)) {
7066                     goto illegal_op;
7067                 }
7068                 gen_helper_sub_cc(tmp, tmp, tmp2);
7069                 gen_exception_return(s, tmp);
7070             } else {
7071                 if (set_cc) {
7072                     gen_helper_sub_cc(tmp, tmp, tmp2);
7073                 } else {
7074                     tcg_gen_sub_i32(tmp, tmp, tmp2);
7075                 }
7076                 store_reg_bx(env, s, rd, tmp);
7077             }
7078             break;
7079         case 0x03:
7080             if (set_cc) {
7081                 gen_helper_sub_cc(tmp, tmp2, tmp);
7082             } else {
7083                 tcg_gen_sub_i32(tmp, tmp2, tmp);
7084             }
7085             store_reg_bx(env, s, rd, tmp);
7086             break;
7087         case 0x04:
7088             if (set_cc) {
7089                 gen_helper_add_cc(tmp, tmp, tmp2);
7090             } else {
7091                 tcg_gen_add_i32(tmp, tmp, tmp2);
7092             }
7093             store_reg_bx(env, s, rd, tmp);
7094             break;
7095         case 0x05:
7096             if (set_cc) {
7097                 gen_helper_adc_cc(tmp, tmp, tmp2);
7098             } else {
7099                 gen_add_carry(tmp, tmp, tmp2);
7100             }
7101             store_reg_bx(env, s, rd, tmp);
7102             break;
7103         case 0x06:
7104             if (set_cc) {
7105                 gen_helper_sbc_cc(tmp, tmp, tmp2);
7106             } else {
7107                 gen_sub_carry(tmp, tmp, tmp2);
7108             }
7109             store_reg_bx(env, s, rd, tmp);
7110             break;
7111         case 0x07:
7112             if (set_cc) {
7113                 gen_helper_sbc_cc(tmp, tmp2, tmp);
7114             } else {
7115                 gen_sub_carry(tmp, tmp2, tmp);
7116             }
7117             store_reg_bx(env, s, rd, tmp);
7118             break;
7119         case 0x08:
7120             if (set_cc) {
7121                 tcg_gen_and_i32(tmp, tmp, tmp2);
7122                 gen_logic_CC(tmp);
7123             }
7124             tcg_temp_free_i32(tmp);
7125             break;
7126         case 0x09:
7127             if (set_cc) {
7128                 tcg_gen_xor_i32(tmp, tmp, tmp2);
7129                 gen_logic_CC(tmp);
7130             }
7131             tcg_temp_free_i32(tmp);
7132             break;
7133         case 0x0a:
7134             if (set_cc) {
7135                 gen_helper_sub_cc(tmp, tmp, tmp2);
7136             }
7137             tcg_temp_free_i32(tmp);
7138             break;
7139         case 0x0b:
7140             if (set_cc) {
7141                 gen_helper_add_cc(tmp, tmp, tmp2);
7142             }
7143             tcg_temp_free_i32(tmp);
7144             break;
7145         case 0x0c:
7146             tcg_gen_or_i32(tmp, tmp, tmp2);
7147             if (logic_cc) {
7148                 gen_logic_CC(tmp);
7149             }
7150             store_reg_bx(env, s, rd, tmp);
7151             break;
7152         case 0x0d:
7153             if (logic_cc && rd == 15) {
7154                 /* MOVS r15, ... is used for exception return.  */
7155                 if (IS_USER(s)) {
7156                     goto illegal_op;
7157                 }
7158                 gen_exception_return(s, tmp2);
7159             } else {
7160                 if (logic_cc) {
7161                     gen_logic_CC(tmp2);
7162                 }
7163                 store_reg_bx(env, s, rd, tmp2);
7164             }
7165             break;
7166         case 0x0e:
7167             tcg_gen_andc_i32(tmp, tmp, tmp2);
7168             if (logic_cc) {
7169                 gen_logic_CC(tmp);
7170             }
7171             store_reg_bx(env, s, rd, tmp);
7172             break;
7173         default:
7174         case 0x0f:
7175             tcg_gen_not_i32(tmp2, tmp2);
7176             if (logic_cc) {
7177                 gen_logic_CC(tmp2);
7178             }
7179             store_reg_bx(env, s, rd, tmp2);
7180             break;
7181         }
7182         if (op1 != 0x0f && op1 != 0x0d) {
7183             tcg_temp_free_i32(tmp2);
7184         }
7185     } else {
7186         /* other instructions */
7187         op1 = (insn >> 24) & 0xf;
7188         switch(op1) {
7189         case 0x0:
7190         case 0x1:
7191             /* multiplies, extra load/stores */
7192             sh = (insn >> 5) & 3;
7193             if (sh == 0) {
7194                 if (op1 == 0x0) {
7195                     rd = (insn >> 16) & 0xf;
7196                     rn = (insn >> 12) & 0xf;
7197                     rs = (insn >> 8) & 0xf;
7198                     rm = (insn) & 0xf;
7199                     op1 = (insn >> 20) & 0xf;
7200                     switch (op1) {
7201                     case 0: case 1: case 2: case 3: case 6:
7202                         /* 32 bit mul */
7203                         tmp = load_reg(s, rs);
7204                         tmp2 = load_reg(s, rm);
7205                         tcg_gen_mul_i32(tmp, tmp, tmp2);
7206                         tcg_temp_free_i32(tmp2);
7207                         if (insn & (1 << 22)) {
7208                             /* Subtract (mls) */
7209                             ARCH(6T2);
7210                             tmp2 = load_reg(s, rn);
7211                             tcg_gen_sub_i32(tmp, tmp2, tmp);
7212                             tcg_temp_free_i32(tmp2);
7213                         } else if (insn & (1 << 21)) {
7214                             /* Add */
7215                             tmp2 = load_reg(s, rn);
7216                             tcg_gen_add_i32(tmp, tmp, tmp2);
7217                             tcg_temp_free_i32(tmp2);
7218                         }
7219                         if (insn & (1 << 20))
7220                             gen_logic_CC(tmp);
7221                         store_reg(s, rd, tmp);
7222                         break;
7223                     case 4:
7224                         /* 64 bit mul double accumulate (UMAAL) */
7225                         ARCH(6);
7226                         tmp = load_reg(s, rs);
7227                         tmp2 = load_reg(s, rm);
7228                         tmp64 = gen_mulu_i64_i32(tmp, tmp2);
7229                         gen_addq_lo(s, tmp64, rn);
7230                         gen_addq_lo(s, tmp64, rd);
7231                         gen_storeq_reg(s, rn, rd, tmp64);
7232                         tcg_temp_free_i64(tmp64);
7233                         break;
7234                     case 8: case 9: case 10: case 11:
7235                     case 12: case 13: case 14: case 15:
7236                         /* 64 bit mul: UMULL, UMLAL, SMULL, SMLAL. */
7237                         tmp = load_reg(s, rs);
7238                         tmp2 = load_reg(s, rm);
7239                         if (insn & (1 << 22)) {
7240                             tmp64 = gen_muls_i64_i32(tmp, tmp2);
7241                         } else {
7242                             tmp64 = gen_mulu_i64_i32(tmp, tmp2);
7243                         }
7244                         if (insn & (1 << 21)) { /* mult accumulate */
7245                             gen_addq(s, tmp64, rn, rd);
7246                         }
7247                         if (insn & (1 << 20)) {
7248                             gen_logicq_cc(tmp64);
7249                         }
7250                         gen_storeq_reg(s, rn, rd, tmp64);
7251                         tcg_temp_free_i64(tmp64);
7252                         break;
7253                     default:
7254                         goto illegal_op;
7255                     }
7256                 } else {
7257                     rn = (insn >> 16) & 0xf;
7258                     rd = (insn >> 12) & 0xf;
7259                     if (insn & (1 << 23)) {
7260                         /* load/store exclusive */
7261                         op1 = (insn >> 21) & 0x3;
7262                         if (op1)
7263                             ARCH(6K);
7264                         else
7265                             ARCH(6);
7266                         addr = tcg_temp_local_new_i32();
7267                         load_reg_var(s, addr, rn);
7268                         if (insn & (1 << 20)) {
7269                             switch (op1) {
7270                             case 0: /* ldrex */
7271                                 gen_load_exclusive(s, rd, 15, addr, 2);
7272                                 break;
7273                             case 1: /* ldrexd */
7274                                 gen_load_exclusive(s, rd, rd + 1, addr, 3);
7275                                 break;
7276                             case 2: /* ldrexb */
7277                                 gen_load_exclusive(s, rd, 15, addr, 0);
7278                                 break;
7279                             case 3: /* ldrexh */
7280                                 gen_load_exclusive(s, rd, 15, addr, 1);
7281                                 break;
7282                             default:
7283                                 abort();
7284                             }
7285                         } else {
7286                             rm = insn & 0xf;
7287                             switch (op1) {
7288                             case 0:  /*  strex */
7289                                 gen_store_exclusive(s, rd, rm, 15, addr, 2);
7290                                 break;
7291                             case 1: /*  strexd */
7292                                 gen_store_exclusive(s, rd, rm, rm + 1, addr, 3);
7293                                 break;
7294                             case 2: /*  strexb */
7295                                 gen_store_exclusive(s, rd, rm, 15, addr, 0);
7296                                 break;
7297                             case 3: /* strexh */
7298                                 gen_store_exclusive(s, rd, rm, 15, addr, 1);
7299                                 break;
7300                             default:
7301                                 abort();
7302                             }
7303                         }
7304                         tcg_temp_free(addr);
7305                     } else {
7306                         /* SWP instruction */
7307                         rm = (insn) & 0xf;
7308
7309                         /* ??? This is not really atomic.  However we know
7310                            we never have multiple CPUs running in parallel,
7311                            so it is good enough.  */
7312                         addr = load_reg(s, rn);
7313                         tmp = load_reg(s, rm);
7314                         if (insn & (1 << 22)) {
7315                             tmp2 = gen_ld8u(addr, IS_USER(s));
7316                             gen_st8(tmp, addr, IS_USER(s));
7317                         } else {
7318                             tmp2 = gen_ld32(addr, IS_USER(s));
7319                             gen_st32(tmp, addr, IS_USER(s));
7320                         }
7321                         tcg_temp_free_i32(addr);
7322                         store_reg(s, rd, tmp2);
7323                     }
7324                 }
7325             } else {
7326                 int address_offset;
7327                 int load;
7328                 /* Misc load/store */
7329                 rn = (insn >> 16) & 0xf;
7330                 rd = (insn >> 12) & 0xf;
7331                 addr = load_reg(s, rn);
7332                 if (insn & (1 << 24))
7333                     gen_add_datah_offset(s, insn, 0, addr);
7334                 address_offset = 0;
7335                 if (insn & (1 << 20)) {
7336                     /* load */
7337                     switch(sh) {
7338                     case 1:
7339                         tmp = gen_ld16u(addr, IS_USER(s));
7340                         break;
7341                     case 2:
7342                         tmp = gen_ld8s(addr, IS_USER(s));
7343                         break;
7344                     default:
7345                     case 3:
7346                         tmp = gen_ld16s(addr, IS_USER(s));
7347                         break;
7348                     }
7349                     load = 1;
7350                 } else if (sh & 2) {
7351                     ARCH(5TE);
7352                     /* doubleword */
7353                     if (sh & 1) {
7354                         /* store */
7355                         tmp = load_reg(s, rd);
7356                         gen_st32(tmp, addr, IS_USER(s));
7357                         tcg_gen_addi_i32(addr, addr, 4);
7358                         tmp = load_reg(s, rd + 1);
7359                         gen_st32(tmp, addr, IS_USER(s));
7360                         load = 0;
7361                     } else {
7362                         /* load */
7363                         tmp = gen_ld32(addr, IS_USER(s));
7364                         store_reg(s, rd, tmp);
7365                         tcg_gen_addi_i32(addr, addr, 4);
7366                         tmp = gen_ld32(addr, IS_USER(s));
7367                         rd++;
7368                         load = 1;
7369                     }
7370                     address_offset = -4;
7371                 } else {
7372                     /* store */
7373                     tmp = load_reg(s, rd);
7374                     gen_st16(tmp, addr, IS_USER(s));
7375                     load = 0;
7376                 }
7377                 /* Perform base writeback before the loaded value to
7378                    ensure correct behavior with overlapping index registers.
7379                    ldrd with base writeback is is undefined if the
7380                    destination and index registers overlap.  */
7381                 if (!(insn & (1 << 24))) {
7382                     gen_add_datah_offset(s, insn, address_offset, addr);
7383                     store_reg(s, rn, addr);
7384                 } else if (insn & (1 << 21)) {
7385                     if (address_offset)
7386                         tcg_gen_addi_i32(addr, addr, address_offset);
7387                     store_reg(s, rn, addr);
7388                 } else {
7389                     tcg_temp_free_i32(addr);
7390                 }
7391                 if (load) {
7392                     /* Complete the load.  */
7393                     store_reg(s, rd, tmp);
7394                 }
7395             }
7396             break;
7397         case 0x4:
7398         case 0x5:
7399             goto do_ldst;
7400         case 0x6:
7401         case 0x7:
7402             if (insn & (1 << 4)) {
7403                 ARCH(6);
7404                 /* Armv6 Media instructions.  */
7405                 rm = insn & 0xf;
7406                 rn = (insn >> 16) & 0xf;
7407                 rd = (insn >> 12) & 0xf;
7408                 rs = (insn >> 8) & 0xf;
7409                 switch ((insn >> 23) & 3) {
7410                 case 0: /* Parallel add/subtract.  */
7411                     op1 = (insn >> 20) & 7;
7412                     tmp = load_reg(s, rn);
7413                     tmp2 = load_reg(s, rm);
7414                     sh = (insn >> 5) & 7;
7415                     if ((op1 & 3) == 0 || sh == 5 || sh == 6)
7416                         goto illegal_op;
7417                     gen_arm_parallel_addsub(op1, sh, tmp, tmp2);
7418                     tcg_temp_free_i32(tmp2);
7419                     store_reg(s, rd, tmp);
7420                     break;
7421                 case 1:
7422                     if ((insn & 0x00700020) == 0) {
7423                         /* Halfword pack.  */
7424                         tmp = load_reg(s, rn);
7425                         tmp2 = load_reg(s, rm);
7426                         shift = (insn >> 7) & 0x1f;
7427                         if (insn & (1 << 6)) {
7428                             /* pkhtb */
7429                             if (shift == 0)
7430                                 shift = 31;
7431                             tcg_gen_sari_i32(tmp2, tmp2, shift);
7432                             tcg_gen_andi_i32(tmp, tmp, 0xffff0000);
7433                             tcg_gen_ext16u_i32(tmp2, tmp2);
7434                         } else {
7435                             /* pkhbt */
7436                             if (shift)
7437                                 tcg_gen_shli_i32(tmp2, tmp2, shift);
7438                             tcg_gen_ext16u_i32(tmp, tmp);
7439                             tcg_gen_andi_i32(tmp2, tmp2, 0xffff0000);
7440                         }
7441                         tcg_gen_or_i32(tmp, tmp, tmp2);
7442                         tcg_temp_free_i32(tmp2);
7443                         store_reg(s, rd, tmp);
7444                     } else if ((insn & 0x00200020) == 0x00200000) {
7445                         /* [us]sat */
7446                         tmp = load_reg(s, rm);
7447                         shift = (insn >> 7) & 0x1f;
7448                         if (insn & (1 << 6)) {
7449                             if (shift == 0)
7450                                 shift = 31;
7451                             tcg_gen_sari_i32(tmp, tmp, shift);
7452                         } else {
7453                             tcg_gen_shli_i32(tmp, tmp, shift);
7454                         }
7455                         sh = (insn >> 16) & 0x1f;
7456                         tmp2 = tcg_const_i32(sh);
7457                         if (insn & (1 << 22))
7458                           gen_helper_usat(tmp, tmp, tmp2);
7459                         else
7460                           gen_helper_ssat(tmp, tmp, tmp2);
7461                         tcg_temp_free_i32(tmp2);
7462                         store_reg(s, rd, tmp);
7463                     } else if ((insn & 0x00300fe0) == 0x00200f20) {
7464                         /* [us]sat16 */
7465                         tmp = load_reg(s, rm);
7466                         sh = (insn >> 16) & 0x1f;
7467                         tmp2 = tcg_const_i32(sh);
7468                         if (insn & (1 << 22))
7469                           gen_helper_usat16(tmp, tmp, tmp2);
7470                         else
7471                           gen_helper_ssat16(tmp, tmp, tmp2);
7472                         tcg_temp_free_i32(tmp2);
7473                         store_reg(s, rd, tmp);
7474                     } else if ((insn & 0x00700fe0) == 0x00000fa0) {
7475                         /* Select bytes.  */
7476                         tmp = load_reg(s, rn);
7477                         tmp2 = load_reg(s, rm);
7478                         tmp3 = tcg_temp_new_i32();
7479                         tcg_gen_ld_i32(tmp3, cpu_env, offsetof(CPUARMState, GE));
7480                         gen_helper_sel_flags(tmp, tmp3, tmp, tmp2);
7481                         tcg_temp_free_i32(tmp3);
7482                         tcg_temp_free_i32(tmp2);
7483                         store_reg(s, rd, tmp);
7484                     } else if ((insn & 0x000003e0) == 0x00000060) {
7485                         tmp = load_reg(s, rm);
7486                         shift = (insn >> 10) & 3;
7487                         /* ??? In many cases it's not necessary to do a
7488                            rotate, a shift is sufficient.  */
7489                         if (shift != 0)
7490                             tcg_gen_rotri_i32(tmp, tmp, shift * 8);
7491                         op1 = (insn >> 20) & 7;
7492                         switch (op1) {
7493                         case 0: gen_sxtb16(tmp);  break;
7494                         case 2: gen_sxtb(tmp);    break;
7495                         case 3: gen_sxth(tmp);    break;
7496                         case 4: gen_uxtb16(tmp);  break;
7497                         case 6: gen_uxtb(tmp);    break;
7498                         case 7: gen_uxth(tmp);    break;
7499                         default: goto illegal_op;
7500                         }
7501                         if (rn != 15) {
7502                             tmp2 = load_reg(s, rn);
7503                             if ((op1 & 3) == 0) {
7504                                 gen_add16(tmp, tmp2);
7505                             } else {
7506                                 tcg_gen_add_i32(tmp, tmp, tmp2);
7507                                 tcg_temp_free_i32(tmp2);
7508                             }
7509                         }
7510                         store_reg(s, rd, tmp);
7511                     } else if ((insn & 0x003f0f60) == 0x003f0f20) {
7512                         /* rev */
7513                         tmp = load_reg(s, rm);
7514                         if (insn & (1 << 22)) {
7515                             if (insn & (1 << 7)) {
7516                                 gen_revsh(tmp);
7517                             } else {
7518                                 ARCH(6T2);
7519                                 gen_helper_rbit(tmp, tmp);
7520                             }
7521                         } else {
7522                             if (insn & (1 << 7))
7523                                 gen_rev16(tmp);
7524                             else
7525                                 tcg_gen_bswap32_i32(tmp, tmp);
7526                         }
7527                         store_reg(s, rd, tmp);
7528                     } else {
7529                         goto illegal_op;
7530                     }
7531                     break;
7532                 case 2: /* Multiplies (Type 3).  */
7533                     switch ((insn >> 20) & 0x7) {
7534                     case 5:
7535                         if (((insn >> 6) ^ (insn >> 7)) & 1) {
7536                             /* op2 not 00x or 11x : UNDEF */
7537                             goto illegal_op;
7538                         }
7539                         /* Signed multiply most significant [accumulate].
7540                            (SMMUL, SMMLA, SMMLS) */
7541                         tmp = load_reg(s, rm);
7542                         tmp2 = load_reg(s, rs);
7543                         tmp64 = gen_muls_i64_i32(tmp, tmp2);
7544
7545                         if (rd != 15) {
7546                             tmp = load_reg(s, rd);
7547                             if (insn & (1 << 6)) {
7548                                 tmp64 = gen_subq_msw(tmp64, tmp);
7549                             } else {
7550                                 tmp64 = gen_addq_msw(tmp64, tmp);
7551                             }
7552                         }
7553                         if (insn & (1 << 5)) {
7554                             tcg_gen_addi_i64(tmp64, tmp64, 0x80000000u);
7555                         }
7556                         tcg_gen_shri_i64(tmp64, tmp64, 32);
7557                         tmp = tcg_temp_new_i32();
7558                         tcg_gen_trunc_i64_i32(tmp, tmp64);
7559                         tcg_temp_free_i64(tmp64);
7560                         store_reg(s, rn, tmp);
7561                         break;
7562                     case 0:
7563                     case 4:
7564                         /* SMLAD, SMUAD, SMLSD, SMUSD, SMLALD, SMLSLD */
7565                         if (insn & (1 << 7)) {
7566                             goto illegal_op;
7567                         }
7568                         tmp = load_reg(s, rm);
7569                         tmp2 = load_reg(s, rs);
7570                         if (insn & (1 << 5))
7571                             gen_swap_half(tmp2);
7572                         gen_smul_dual(tmp, tmp2);
7573                         if (insn & (1 << 6)) {
7574                             /* This subtraction cannot overflow. */
7575                             tcg_gen_sub_i32(tmp, tmp, tmp2);
7576                         } else {
7577                             /* This addition cannot overflow 32 bits;
7578                              * however it may overflow considered as a signed
7579                              * operation, in which case we must set the Q flag.
7580                              */
7581                             gen_helper_add_setq(tmp, tmp, tmp2);
7582                         }
7583                         tcg_temp_free_i32(tmp2);
7584                         if (insn & (1 << 22)) {
7585                             /* smlald, smlsld */
7586                             tmp64 = tcg_temp_new_i64();
7587                             tcg_gen_ext_i32_i64(tmp64, tmp);
7588                             tcg_temp_free_i32(tmp);
7589                             gen_addq(s, tmp64, rd, rn);
7590                             gen_storeq_reg(s, rd, rn, tmp64);
7591                             tcg_temp_free_i64(tmp64);
7592                         } else {
7593                             /* smuad, smusd, smlad, smlsd */
7594                             if (rd != 15)
7595                               {
7596                                 tmp2 = load_reg(s, rd);
7597                                 gen_helper_add_setq(tmp, tmp, tmp2);
7598                                 tcg_temp_free_i32(tmp2);
7599                               }
7600                             store_reg(s, rn, tmp);
7601                         }
7602                         break;
7603                     case 1:
7604                     case 3:
7605                         /* SDIV, UDIV */
7606                         if (!arm_feature(env, ARM_FEATURE_ARM_DIV)) {
7607                             goto illegal_op;
7608                         }
7609                         if (((insn >> 5) & 7) || (rd != 15)) {
7610                             goto illegal_op;
7611                         }
7612                         tmp = load_reg(s, rm);
7613                         tmp2 = load_reg(s, rs);
7614                         if (insn & (1 << 21)) {
7615                             gen_helper_udiv(tmp, tmp, tmp2);
7616                         } else {
7617                             gen_helper_sdiv(tmp, tmp, tmp2);
7618                         }
7619                         tcg_temp_free_i32(tmp2);
7620                         store_reg(s, rn, tmp);
7621                         break;
7622                     default:
7623                         goto illegal_op;
7624                     }
7625                     break;
7626                 case 3:
7627                     op1 = ((insn >> 17) & 0x38) | ((insn >> 5) & 7);
7628                     switch (op1) {
7629                     case 0: /* Unsigned sum of absolute differences.  */
7630                         ARCH(6);
7631                         tmp = load_reg(s, rm);
7632                         tmp2 = load_reg(s, rs);
7633                         gen_helper_usad8(tmp, tmp, tmp2);
7634                         tcg_temp_free_i32(tmp2);
7635                         if (rd != 15) {
7636                             tmp2 = load_reg(s, rd);
7637                             tcg_gen_add_i32(tmp, tmp, tmp2);
7638                             tcg_temp_free_i32(tmp2);
7639                         }
7640                         store_reg(s, rn, tmp);
7641                         break;
7642                     case 0x20: case 0x24: case 0x28: case 0x2c:
7643                         /* Bitfield insert/clear.  */
7644                         ARCH(6T2);
7645                         shift = (insn >> 7) & 0x1f;
7646                         i = (insn >> 16) & 0x1f;
7647                         i = i + 1 - shift;
7648                         if (rm == 15) {
7649                             tmp = tcg_temp_new_i32();
7650                             tcg_gen_movi_i32(tmp, 0);
7651                         } else {
7652                             tmp = load_reg(s, rm);
7653                         }
7654                         if (i != 32) {
7655                             tmp2 = load_reg(s, rd);
7656                             gen_bfi(tmp, tmp2, tmp, shift, (1u << i) - 1);
7657                             tcg_temp_free_i32(tmp2);
7658                         }
7659                         store_reg(s, rd, tmp);
7660                         break;
7661                     case 0x12: case 0x16: case 0x1a: case 0x1e: /* sbfx */
7662                     case 0x32: case 0x36: case 0x3a: case 0x3e: /* ubfx */
7663                         ARCH(6T2);
7664                         tmp = load_reg(s, rm);
7665                         shift = (insn >> 7) & 0x1f;
7666                         i = ((insn >> 16) & 0x1f) + 1;
7667                         if (shift + i > 32)
7668                             goto illegal_op;
7669                         if (i < 32) {
7670                             if (op1 & 0x20) {
7671                                 gen_ubfx(tmp, shift, (1u << i) - 1);
7672                             } else {
7673                                 gen_sbfx(tmp, shift, i);
7674                             }
7675                         }
7676                         store_reg(s, rd, tmp);
7677                         break;
7678                     default:
7679                         goto illegal_op;
7680                     }
7681                     break;
7682                 }
7683                 break;
7684             }
7685         do_ldst:
7686             /* Check for undefined extension instructions
7687              * per the ARM Bible IE:
7688              * xxxx 0111 1111 xxxx  xxxx xxxx 1111 xxxx
7689              */
7690             sh = (0xf << 20) | (0xf << 4);
7691             if (op1 == 0x7 && ((insn & sh) == sh))
7692             {
7693                 goto illegal_op;
7694             }
7695             /* load/store byte/word */
7696             rn = (insn >> 16) & 0xf;
7697             rd = (insn >> 12) & 0xf;
7698             tmp2 = load_reg(s, rn);
7699             i = (IS_USER(s) || (insn & 0x01200000) == 0x00200000);
7700             if (insn & (1 << 24))
7701                 gen_add_data_offset(s, insn, tmp2);
7702             if (insn & (1 << 20)) {
7703                 /* load */
7704                 if (insn & (1 << 22)) {
7705                     tmp = gen_ld8u(tmp2, i);
7706                 } else {
7707                     tmp = gen_ld32(tmp2, i);
7708                 }
7709             } else {
7710                 /* store */
7711                 tmp = load_reg(s, rd);
7712                 if (insn & (1 << 22))
7713                     gen_st8(tmp, tmp2, i);
7714                 else
7715                     gen_st32(tmp, tmp2, i);
7716             }
7717             if (!(insn & (1 << 24))) {
7718                 gen_add_data_offset(s, insn, tmp2);
7719                 store_reg(s, rn, tmp2);
7720             } else if (insn & (1 << 21)) {
7721                 store_reg(s, rn, tmp2);
7722             } else {
7723                 tcg_temp_free_i32(tmp2);
7724             }
7725             if (insn & (1 << 20)) {
7726                 /* Complete the load.  */
7727                 store_reg_from_load(env, s, rd, tmp);
7728             }
7729             break;
7730         case 0x08:
7731         case 0x09:
7732             {
7733                 int j, n, user, loaded_base;
7734                 TCGv loaded_var;
7735                 /* load/store multiple words */
7736                 /* XXX: store correct base if write back */
7737                 user = 0;
7738                 if (insn & (1 << 22)) {
7739                     if (IS_USER(s))
7740                         goto illegal_op; /* only usable in supervisor mode */
7741
7742                     if ((insn & (1 << 15)) == 0)
7743                         user = 1;
7744                 }
7745                 rn = (insn >> 16) & 0xf;
7746                 addr = load_reg(s, rn);
7747
7748                 /* compute total size */
7749                 loaded_base = 0;
7750                 TCGV_UNUSED(loaded_var);
7751                 n = 0;
7752                 for(i=0;i<16;i++) {
7753                     if (insn & (1 << i))
7754                         n++;
7755                 }
7756                 /* XXX: test invalid n == 0 case ? */
7757                 if (insn & (1 << 23)) {
7758                     if (insn & (1 << 24)) {
7759                         /* pre increment */
7760                         tcg_gen_addi_i32(addr, addr, 4);
7761                     } else {
7762                         /* post increment */
7763                     }
7764                 } else {
7765                     if (insn & (1 << 24)) {
7766                         /* pre decrement */
7767                         tcg_gen_addi_i32(addr, addr, -(n * 4));
7768                     } else {
7769                         /* post decrement */
7770                         if (n != 1)
7771                         tcg_gen_addi_i32(addr, addr, -((n - 1) * 4));
7772                     }
7773                 }
7774                 j = 0;
7775                 for(i=0;i<16;i++) {
7776                     if (insn & (1 << i)) {
7777                         if (insn & (1 << 20)) {
7778                             /* load */
7779                             tmp = gen_ld32(addr, IS_USER(s));
7780                             if (user) {
7781                                 tmp2 = tcg_const_i32(i);
7782                                 gen_helper_set_user_reg(tmp2, tmp);
7783                                 tcg_temp_free_i32(tmp2);
7784                                 tcg_temp_free_i32(tmp);
7785                             } else if (i == rn) {
7786                                 loaded_var = tmp;
7787                                 loaded_base = 1;
7788                             } else {
7789                                 store_reg_from_load(env, s, i, tmp);
7790                             }
7791                         } else {
7792                             /* store */
7793                             if (i == 15) {
7794                                 /* special case: r15 = PC + 8 */
7795                                 val = (long)s->pc + 4;
7796                                 tmp = tcg_temp_new_i32();
7797                                 tcg_gen_movi_i32(tmp, val);
7798                             } else if (user) {
7799                                 tmp = tcg_temp_new_i32();
7800                                 tmp2 = tcg_const_i32(i);
7801                                 gen_helper_get_user_reg(tmp, tmp2);
7802                                 tcg_temp_free_i32(tmp2);
7803                             } else {
7804                                 tmp = load_reg(s, i);
7805                             }
7806                             gen_st32(tmp, addr, IS_USER(s));
7807                         }
7808                         j++;
7809                         /* no need to add after the last transfer */
7810                         if (j != n)
7811                             tcg_gen_addi_i32(addr, addr, 4);
7812                     }
7813                 }
7814                 if (insn & (1 << 21)) {
7815                     /* write back */
7816                     if (insn & (1 << 23)) {
7817                         if (insn & (1 << 24)) {
7818                             /* pre increment */
7819                         } else {
7820                             /* post increment */
7821                             tcg_gen_addi_i32(addr, addr, 4);
7822                         }
7823                     } else {
7824                         if (insn & (1 << 24)) {
7825                             /* pre decrement */
7826                             if (n != 1)
7827                                 tcg_gen_addi_i32(addr, addr, -((n - 1) * 4));
7828                         } else {
7829                             /* post decrement */
7830                             tcg_gen_addi_i32(addr, addr, -(n * 4));
7831                         }
7832                     }
7833                     store_reg(s, rn, addr);
7834                 } else {
7835                     tcg_temp_free_i32(addr);
7836                 }
7837                 if (loaded_base) {
7838                     store_reg(s, rn, loaded_var);
7839                 }
7840                 if ((insn & (1 << 22)) && !user) {
7841                     /* Restore CPSR from SPSR.  */
7842                     tmp = load_cpu_field(spsr);
7843                     gen_set_cpsr(tmp, 0xffffffff);
7844                     tcg_temp_free_i32(tmp);
7845                     s->is_jmp = DISAS_UPDATE;
7846                 }
7847             }
7848             break;
7849         case 0xa:
7850         case 0xb:
7851             {
7852                 int32_t offset;
7853
7854                 /* branch (and link) */
7855                 val = (int32_t)s->pc;
7856                 if (insn & (1 << 24)) {
7857                     tmp = tcg_temp_new_i32();
7858                     tcg_gen_movi_i32(tmp, val);
7859                     store_reg(s, 14, tmp);
7860                 }
7861                 offset = (((int32_t)insn << 8) >> 8);
7862                 val += (offset << 2) + 4;
7863                 gen_jmp(s, val);
7864             }
7865             break;
7866         case 0xc:
7867         case 0xd:
7868         case 0xe:
7869             /* Coprocessor.  */
7870             if (disas_coproc_insn(env, s, insn))
7871                 goto illegal_op;
7872             break;
7873         case 0xf:
7874             /* swi */
7875             gen_set_pc_im(s->pc);
7876             s->is_jmp = DISAS_SWI;
7877             break;
7878         default:
7879         illegal_op:
7880             gen_exception_insn(s, 4, EXCP_UDEF);
7881             break;
7882         }
7883     }
7884 }
7885
7886 /* Return true if this is a Thumb-2 logical op.  */
7887 static int
7888 thumb2_logic_op(int op)
7889 {
7890     return (op < 8);
7891 }
7892
7893 /* Generate code for a Thumb-2 data processing operation.  If CONDS is nonzero
7894    then set condition code flags based on the result of the operation.
7895    If SHIFTER_OUT is nonzero then set the carry flag for logical operations
7896    to the high bit of T1.
7897    Returns zero if the opcode is valid.  */
7898
7899 static int
7900 gen_thumb2_data_op(DisasContext *s, int op, int conds, uint32_t shifter_out, TCGv t0, TCGv t1)
7901 {
7902     int logic_cc;
7903
7904     logic_cc = 0;
7905     switch (op) {
7906     case 0: /* and */
7907         tcg_gen_and_i32(t0, t0, t1);
7908         logic_cc = conds;
7909         break;
7910     case 1: /* bic */
7911         tcg_gen_andc_i32(t0, t0, t1);
7912         logic_cc = conds;
7913         break;
7914     case 2: /* orr */
7915         tcg_gen_or_i32(t0, t0, t1);
7916         logic_cc = conds;
7917         break;
7918     case 3: /* orn */
7919         tcg_gen_orc_i32(t0, t0, t1);
7920         logic_cc = conds;
7921         break;
7922     case 4: /* eor */
7923         tcg_gen_xor_i32(t0, t0, t1);
7924         logic_cc = conds;
7925         break;
7926     case 8: /* add */
7927         if (conds)
7928             gen_helper_add_cc(t0, t0, t1);
7929         else
7930             tcg_gen_add_i32(t0, t0, t1);
7931         break;
7932     case 10: /* adc */
7933         if (conds)
7934             gen_helper_adc_cc(t0, t0, t1);
7935         else
7936             gen_adc(t0, t1);
7937         break;
7938     case 11: /* sbc */
7939         if (conds)
7940             gen_helper_sbc_cc(t0, t0, t1);
7941         else
7942             gen_sub_carry(t0, t0, t1);
7943         break;
7944     case 13: /* sub */
7945         if (conds)
7946             gen_helper_sub_cc(t0, t0, t1);
7947         else
7948             tcg_gen_sub_i32(t0, t0, t1);
7949         break;
7950     case 14: /* rsb */
7951         if (conds)
7952             gen_helper_sub_cc(t0, t1, t0);
7953         else
7954             tcg_gen_sub_i32(t0, t1, t0);
7955         break;
7956     default: /* 5, 6, 7, 9, 12, 15. */
7957         return 1;
7958     }
7959     if (logic_cc) {
7960         gen_logic_CC(t0);
7961         if (shifter_out)
7962             gen_set_CF_bit31(t1);
7963     }
7964     return 0;
7965 }
7966
7967 /* Translate a 32-bit thumb instruction.  Returns nonzero if the instruction
7968    is not legal.  */
7969 static int disas_thumb2_insn(CPUARMState *env, DisasContext *s, uint16_t insn_hw1)
7970 {
7971     uint32_t insn, imm, shift, offset;
7972     uint32_t rd, rn, rm, rs;
7973     TCGv tmp;
7974     TCGv tmp2;
7975     TCGv tmp3;
7976     TCGv addr;
7977     TCGv_i64 tmp64;
7978     int op;
7979     int shiftop;
7980     int conds;
7981     int logic_cc;
7982
7983     if (!(arm_feature(env, ARM_FEATURE_THUMB2)
7984           || arm_feature (env, ARM_FEATURE_M))) {
7985         /* Thumb-1 cores may need to treat bl and blx as a pair of
7986            16-bit instructions to get correct prefetch abort behavior.  */
7987         insn = insn_hw1;
7988         if ((insn & (1 << 12)) == 0) {
7989             ARCH(5);
7990             /* Second half of blx.  */
7991             offset = ((insn & 0x7ff) << 1);
7992             tmp = load_reg(s, 14);
7993             tcg_gen_addi_i32(tmp, tmp, offset);
7994             tcg_gen_andi_i32(tmp, tmp, 0xfffffffc);
7995
7996             tmp2 = tcg_temp_new_i32();
7997             tcg_gen_movi_i32(tmp2, s->pc | 1);
7998             store_reg(s, 14, tmp2);
7999             gen_bx(s, tmp);
8000             return 0;
8001         }
8002         if (insn & (1 << 11)) {
8003             /* Second half of bl.  */
8004             offset = ((insn & 0x7ff) << 1) | 1;
8005             tmp = load_reg(s, 14);
8006             tcg_gen_addi_i32(tmp, tmp, offset);
8007
8008             tmp2 = tcg_temp_new_i32();
8009             tcg_gen_movi_i32(tmp2, s->pc | 1);
8010             store_reg(s, 14, tmp2);
8011             gen_bx(s, tmp);
8012             return 0;
8013         }
8014         if ((s->pc & ~TARGET_PAGE_MASK) == 0) {
8015             /* Instruction spans a page boundary.  Implement it as two
8016                16-bit instructions in case the second half causes an
8017                prefetch abort.  */
8018             offset = ((int32_t)insn << 21) >> 9;
8019             tcg_gen_movi_i32(cpu_R[14], s->pc + 2 + offset);
8020             return 0;
8021         }
8022         /* Fall through to 32-bit decode.  */
8023     }
8024
8025     insn = arm_lduw_code(s->pc, s->bswap_code);
8026     s->pc += 2;
8027     insn |= (uint32_t)insn_hw1 << 16;
8028
8029     if ((insn & 0xf800e800) != 0xf000e800) {
8030         ARCH(6T2);
8031     }
8032
8033     rn = (insn >> 16) & 0xf;
8034     rs = (insn >> 12) & 0xf;
8035     rd = (insn >> 8) & 0xf;
8036     rm = insn & 0xf;
8037     switch ((insn >> 25) & 0xf) {
8038     case 0: case 1: case 2: case 3:
8039         /* 16-bit instructions.  Should never happen.  */
8040         abort();
8041     case 4:
8042         if (insn & (1 << 22)) {
8043             /* Other load/store, table branch.  */
8044             if (insn & 0x01200000) {
8045                 /* Load/store doubleword.  */
8046                 if (rn == 15) {
8047                     addr = tcg_temp_new_i32();
8048                     tcg_gen_movi_i32(addr, s->pc & ~3);
8049                 } else {
8050                     addr = load_reg(s, rn);
8051                 }
8052                 offset = (insn & 0xff) * 4;
8053                 if ((insn & (1 << 23)) == 0)
8054                     offset = -offset;
8055                 if (insn & (1 << 24)) {
8056                     tcg_gen_addi_i32(addr, addr, offset);
8057                     offset = 0;
8058                 }
8059                 if (insn & (1 << 20)) {
8060                     /* ldrd */
8061                     tmp = gen_ld32(addr, IS_USER(s));
8062                     store_reg(s, rs, tmp);
8063                     tcg_gen_addi_i32(addr, addr, 4);
8064                     tmp = gen_ld32(addr, IS_USER(s));
8065                     store_reg(s, rd, tmp);
8066                 } else {
8067                     /* strd */
8068                     tmp = load_reg(s, rs);
8069                     gen_st32(tmp, addr, IS_USER(s));
8070                     tcg_gen_addi_i32(addr, addr, 4);
8071                     tmp = load_reg(s, rd);
8072                     gen_st32(tmp, addr, IS_USER(s));
8073                 }
8074                 if (insn & (1 << 21)) {
8075                     /* Base writeback.  */
8076                     if (rn == 15)
8077                         goto illegal_op;
8078                     tcg_gen_addi_i32(addr, addr, offset - 4);
8079                     store_reg(s, rn, addr);
8080                 } else {
8081                     tcg_temp_free_i32(addr);
8082                 }
8083             } else if ((insn & (1 << 23)) == 0) {
8084                 /* Load/store exclusive word.  */
8085                 addr = tcg_temp_local_new();
8086                 load_reg_var(s, addr, rn);
8087                 tcg_gen_addi_i32(addr, addr, (insn & 0xff) << 2);
8088                 if (insn & (1 << 20)) {
8089                     gen_load_exclusive(s, rs, 15, addr, 2);
8090                 } else {
8091                     gen_store_exclusive(s, rd, rs, 15, addr, 2);
8092                 }
8093                 tcg_temp_free(addr);
8094             } else if ((insn & (1 << 6)) == 0) {
8095                 /* Table Branch.  */
8096                 if (rn == 15) {
8097                     addr = tcg_temp_new_i32();
8098                     tcg_gen_movi_i32(addr, s->pc);
8099                 } else {
8100                     addr = load_reg(s, rn);
8101                 }
8102                 tmp = load_reg(s, rm);
8103                 tcg_gen_add_i32(addr, addr, tmp);
8104                 if (insn & (1 << 4)) {
8105                     /* tbh */
8106                     tcg_gen_add_i32(addr, addr, tmp);
8107                     tcg_temp_free_i32(tmp);
8108                     tmp = gen_ld16u(addr, IS_USER(s));
8109                 } else { /* tbb */
8110                     tcg_temp_free_i32(tmp);
8111                     tmp = gen_ld8u(addr, IS_USER(s));
8112                 }
8113                 tcg_temp_free_i32(addr);
8114                 tcg_gen_shli_i32(tmp, tmp, 1);
8115                 tcg_gen_addi_i32(tmp, tmp, s->pc);
8116                 store_reg(s, 15, tmp);
8117             } else {
8118                 /* Load/store exclusive byte/halfword/doubleword.  */
8119                 ARCH(7);
8120                 op = (insn >> 4) & 0x3;
8121                 if (op == 2) {
8122                     goto illegal_op;
8123                 }
8124                 addr = tcg_temp_local_new();
8125                 load_reg_var(s, addr, rn);
8126                 if (insn & (1 << 20)) {
8127                     gen_load_exclusive(s, rs, rd, addr, op);
8128                 } else {
8129                     gen_store_exclusive(s, rm, rs, rd, addr, op);
8130                 }
8131                 tcg_temp_free(addr);
8132             }
8133         } else {
8134             /* Load/store multiple, RFE, SRS.  */
8135             if (((insn >> 23) & 1) == ((insn >> 24) & 1)) {
8136                 /* Not available in user mode.  */
8137                 if (IS_USER(s))
8138                     goto illegal_op;
8139                 if (insn & (1 << 20)) {
8140                     /* rfe */
8141                     addr = load_reg(s, rn);
8142                     if ((insn & (1 << 24)) == 0)
8143                         tcg_gen_addi_i32(addr, addr, -8);
8144                     /* Load PC into tmp and CPSR into tmp2.  */
8145                     tmp = gen_ld32(addr, 0);
8146                     tcg_gen_addi_i32(addr, addr, 4);
8147                     tmp2 = gen_ld32(addr, 0);
8148                     if (insn & (1 << 21)) {
8149                         /* Base writeback.  */
8150                         if (insn & (1 << 24)) {
8151                             tcg_gen_addi_i32(addr, addr, 4);
8152                         } else {
8153                             tcg_gen_addi_i32(addr, addr, -4);
8154                         }
8155                         store_reg(s, rn, addr);
8156                     } else {
8157                         tcg_temp_free_i32(addr);
8158                     }
8159                     gen_rfe(s, tmp, tmp2);
8160                 } else {
8161                     /* srs */
8162                     op = (insn & 0x1f);
8163                     addr = tcg_temp_new_i32();
8164                     tmp = tcg_const_i32(op);
8165                     gen_helper_get_r13_banked(addr, cpu_env, tmp);
8166                     tcg_temp_free_i32(tmp);
8167                     if ((insn & (1 << 24)) == 0) {
8168                         tcg_gen_addi_i32(addr, addr, -8);
8169                     }
8170                     tmp = load_reg(s, 14);
8171                     gen_st32(tmp, addr, 0);
8172                     tcg_gen_addi_i32(addr, addr, 4);
8173                     tmp = tcg_temp_new_i32();
8174                     gen_helper_cpsr_read(tmp);
8175                     gen_st32(tmp, addr, 0);
8176                     if (insn & (1 << 21)) {
8177                         if ((insn & (1 << 24)) == 0) {
8178                             tcg_gen_addi_i32(addr, addr, -4);
8179                         } else {
8180                             tcg_gen_addi_i32(addr, addr, 4);
8181                         }
8182                         tmp = tcg_const_i32(op);
8183                         gen_helper_set_r13_banked(cpu_env, tmp, addr);
8184                         tcg_temp_free_i32(tmp);
8185                     } else {
8186                         tcg_temp_free_i32(addr);
8187                     }
8188                 }
8189             } else {
8190                 int i, loaded_base = 0;
8191                 TCGv loaded_var;
8192                 /* Load/store multiple.  */
8193                 addr = load_reg(s, rn);
8194                 offset = 0;
8195                 for (i = 0; i < 16; i++) {
8196                     if (insn & (1 << i))
8197                         offset += 4;
8198                 }
8199                 if (insn & (1 << 24)) {
8200                     tcg_gen_addi_i32(addr, addr, -offset);
8201                 }
8202
8203                 TCGV_UNUSED(loaded_var);
8204                 for (i = 0; i < 16; i++) {
8205                     if ((insn & (1 << i)) == 0)
8206                         continue;
8207                     if (insn & (1 << 20)) {
8208                         /* Load.  */
8209                         tmp = gen_ld32(addr, IS_USER(s));
8210                         if (i == 15) {
8211                             gen_bx(s, tmp);
8212                         } else if (i == rn) {
8213                             loaded_var = tmp;
8214                             loaded_base = 1;
8215                         } else {
8216                             store_reg(s, i, tmp);
8217                         }
8218                     } else {
8219                         /* Store.  */
8220                         tmp = load_reg(s, i);
8221                         gen_st32(tmp, addr, IS_USER(s));
8222                     }
8223                     tcg_gen_addi_i32(addr, addr, 4);
8224                 }
8225                 if (loaded_base) {
8226                     store_reg(s, rn, loaded_var);
8227                 }
8228                 if (insn & (1 << 21)) {
8229                     /* Base register writeback.  */
8230                     if (insn & (1 << 24)) {
8231                         tcg_gen_addi_i32(addr, addr, -offset);
8232                     }
8233                     /* Fault if writeback register is in register list.  */
8234                     if (insn & (1 << rn))
8235                         goto illegal_op;
8236                     store_reg(s, rn, addr);
8237                 } else {
8238                     tcg_temp_free_i32(addr);
8239                 }
8240             }
8241         }
8242         break;
8243     case 5:
8244
8245         op = (insn >> 21) & 0xf;
8246         if (op == 6) {
8247             /* Halfword pack.  */
8248             tmp = load_reg(s, rn);
8249             tmp2 = load_reg(s, rm);
8250             shift = ((insn >> 10) & 0x1c) | ((insn >> 6) & 0x3);
8251             if (insn & (1 << 5)) {
8252                 /* pkhtb */
8253                 if (shift == 0)
8254                     shift = 31;
8255                 tcg_gen_sari_i32(tmp2, tmp2, shift);
8256                 tcg_gen_andi_i32(tmp, tmp, 0xffff0000);
8257                 tcg_gen_ext16u_i32(tmp2, tmp2);
8258             } else {
8259                 /* pkhbt */
8260                 if (shift)
8261                     tcg_gen_shli_i32(tmp2, tmp2, shift);
8262                 tcg_gen_ext16u_i32(tmp, tmp);
8263                 tcg_gen_andi_i32(tmp2, tmp2, 0xffff0000);
8264             }
8265             tcg_gen_or_i32(tmp, tmp, tmp2);
8266             tcg_temp_free_i32(tmp2);
8267             store_reg(s, rd, tmp);
8268         } else {
8269             /* Data processing register constant shift.  */
8270             if (rn == 15) {
8271                 tmp = tcg_temp_new_i32();
8272                 tcg_gen_movi_i32(tmp, 0);
8273             } else {
8274                 tmp = load_reg(s, rn);
8275             }
8276             tmp2 = load_reg(s, rm);
8277
8278             shiftop = (insn >> 4) & 3;
8279             shift = ((insn >> 6) & 3) | ((insn >> 10) & 0x1c);
8280             conds = (insn & (1 << 20)) != 0;
8281             logic_cc = (conds && thumb2_logic_op(op));
8282             gen_arm_shift_im(tmp2, shiftop, shift, logic_cc);
8283             if (gen_thumb2_data_op(s, op, conds, 0, tmp, tmp2))
8284                 goto illegal_op;
8285             tcg_temp_free_i32(tmp2);
8286             if (rd != 15) {
8287                 store_reg(s, rd, tmp);
8288             } else {
8289                 tcg_temp_free_i32(tmp);
8290             }
8291         }
8292         break;
8293     case 13: /* Misc data processing.  */
8294         op = ((insn >> 22) & 6) | ((insn >> 7) & 1);
8295         if (op < 4 && (insn & 0xf000) != 0xf000)
8296             goto illegal_op;
8297         switch (op) {
8298         case 0: /* Register controlled shift.  */
8299             tmp = load_reg(s, rn);
8300             tmp2 = load_reg(s, rm);
8301             if ((insn & 0x70) != 0)
8302                 goto illegal_op;
8303             op = (insn >> 21) & 3;
8304             logic_cc = (insn & (1 << 20)) != 0;
8305             gen_arm_shift_reg(tmp, op, tmp2, logic_cc);
8306             if (logic_cc)
8307                 gen_logic_CC(tmp);
8308             store_reg_bx(env, s, rd, tmp);
8309             break;
8310         case 1: /* Sign/zero extend.  */
8311             tmp = load_reg(s, rm);
8312             shift = (insn >> 4) & 3;
8313             /* ??? In many cases it's not necessary to do a
8314                rotate, a shift is sufficient.  */
8315             if (shift != 0)
8316                 tcg_gen_rotri_i32(tmp, tmp, shift * 8);
8317             op = (insn >> 20) & 7;
8318             switch (op) {
8319             case 0: gen_sxth(tmp);   break;
8320             case 1: gen_uxth(tmp);   break;
8321             case 2: gen_sxtb16(tmp); break;
8322             case 3: gen_uxtb16(tmp); break;
8323             case 4: gen_sxtb(tmp);   break;
8324             case 5: gen_uxtb(tmp);   break;
8325             default: goto illegal_op;
8326             }
8327             if (rn != 15) {
8328                 tmp2 = load_reg(s, rn);
8329                 if ((op >> 1) == 1) {
8330                     gen_add16(tmp, tmp2);
8331                 } else {
8332                     tcg_gen_add_i32(tmp, tmp, tmp2);
8333                     tcg_temp_free_i32(tmp2);
8334                 }
8335             }
8336             store_reg(s, rd, tmp);
8337             break;
8338         case 2: /* SIMD add/subtract.  */
8339             op = (insn >> 20) & 7;
8340             shift = (insn >> 4) & 7;
8341             if ((op & 3) == 3 || (shift & 3) == 3)
8342                 goto illegal_op;
8343             tmp = load_reg(s, rn);
8344             tmp2 = load_reg(s, rm);
8345             gen_thumb2_parallel_addsub(op, shift, tmp, tmp2);
8346             tcg_temp_free_i32(tmp2);
8347             store_reg(s, rd, tmp);
8348             break;
8349         case 3: /* Other data processing.  */
8350             op = ((insn >> 17) & 0x38) | ((insn >> 4) & 7);
8351             if (op < 4) {
8352                 /* Saturating add/subtract.  */
8353                 tmp = load_reg(s, rn);
8354                 tmp2 = load_reg(s, rm);
8355                 if (op & 1)
8356                     gen_helper_double_saturate(tmp, tmp);
8357                 if (op & 2)
8358                     gen_helper_sub_saturate(tmp, tmp2, tmp);
8359                 else
8360                     gen_helper_add_saturate(tmp, tmp, tmp2);
8361                 tcg_temp_free_i32(tmp2);
8362             } else {
8363                 tmp = load_reg(s, rn);
8364                 switch (op) {
8365                 case 0x0a: /* rbit */
8366                     gen_helper_rbit(tmp, tmp);
8367                     break;
8368                 case 0x08: /* rev */
8369                     tcg_gen_bswap32_i32(tmp, tmp);
8370                     break;
8371                 case 0x09: /* rev16 */
8372                     gen_rev16(tmp);
8373                     break;
8374                 case 0x0b: /* revsh */
8375                     gen_revsh(tmp);
8376                     break;
8377                 case 0x10: /* sel */
8378                     tmp2 = load_reg(s, rm);
8379                     tmp3 = tcg_temp_new_i32();
8380                     tcg_gen_ld_i32(tmp3, cpu_env, offsetof(CPUARMState, GE));
8381                     gen_helper_sel_flags(tmp, tmp3, tmp, tmp2);
8382                     tcg_temp_free_i32(tmp3);
8383                     tcg_temp_free_i32(tmp2);
8384                     break;
8385                 case 0x18: /* clz */
8386                     gen_helper_clz(tmp, tmp);
8387                     break;
8388                 default:
8389                     goto illegal_op;
8390                 }
8391             }
8392             store_reg(s, rd, tmp);
8393             break;
8394         case 4: case 5: /* 32-bit multiply.  Sum of absolute differences.  */
8395             op = (insn >> 4) & 0xf;
8396             tmp = load_reg(s, rn);
8397             tmp2 = load_reg(s, rm);
8398             switch ((insn >> 20) & 7) {
8399             case 0: /* 32 x 32 -> 32 */
8400                 tcg_gen_mul_i32(tmp, tmp, tmp2);
8401                 tcg_temp_free_i32(tmp2);
8402                 if (rs != 15) {
8403                     tmp2 = load_reg(s, rs);
8404                     if (op)
8405                         tcg_gen_sub_i32(tmp, tmp2, tmp);
8406                     else
8407                         tcg_gen_add_i32(tmp, tmp, tmp2);
8408                     tcg_temp_free_i32(tmp2);
8409                 }
8410                 break;
8411             case 1: /* 16 x 16 -> 32 */
8412                 gen_mulxy(tmp, tmp2, op & 2, op & 1);
8413                 tcg_temp_free_i32(tmp2);
8414                 if (rs != 15) {
8415                     tmp2 = load_reg(s, rs);
8416                     gen_helper_add_setq(tmp, tmp, tmp2);
8417                     tcg_temp_free_i32(tmp2);
8418                 }
8419                 break;
8420             case 2: /* Dual multiply add.  */
8421             case 4: /* Dual multiply subtract.  */
8422                 if (op)
8423                     gen_swap_half(tmp2);
8424                 gen_smul_dual(tmp, tmp2);
8425                 if (insn & (1 << 22)) {
8426                     /* This subtraction cannot overflow. */
8427                     tcg_gen_sub_i32(tmp, tmp, tmp2);
8428                 } else {
8429                     /* This addition cannot overflow 32 bits;
8430                      * however it may overflow considered as a signed
8431                      * operation, in which case we must set the Q flag.
8432                      */
8433                     gen_helper_add_setq(tmp, tmp, tmp2);
8434                 }
8435                 tcg_temp_free_i32(tmp2);
8436                 if (rs != 15)
8437                   {
8438                     tmp2 = load_reg(s, rs);
8439                     gen_helper_add_setq(tmp, tmp, tmp2);
8440                     tcg_temp_free_i32(tmp2);
8441                   }
8442                 break;
8443             case 3: /* 32 * 16 -> 32msb */
8444                 if (op)
8445                     tcg_gen_sari_i32(tmp2, tmp2, 16);
8446                 else
8447                     gen_sxth(tmp2);
8448                 tmp64 = gen_muls_i64_i32(tmp, tmp2);
8449                 tcg_gen_shri_i64(tmp64, tmp64, 16);
8450                 tmp = tcg_temp_new_i32();
8451                 tcg_gen_trunc_i64_i32(tmp, tmp64);
8452                 tcg_temp_free_i64(tmp64);
8453                 if (rs != 15)
8454                   {
8455                     tmp2 = load_reg(s, rs);
8456                     gen_helper_add_setq(tmp, tmp, tmp2);
8457                     tcg_temp_free_i32(tmp2);
8458                   }
8459                 break;
8460             case 5: case 6: /* 32 * 32 -> 32msb (SMMUL, SMMLA, SMMLS) */
8461                 tmp64 = gen_muls_i64_i32(tmp, tmp2);
8462                 if (rs != 15) {
8463                     tmp = load_reg(s, rs);
8464                     if (insn & (1 << 20)) {
8465                         tmp64 = gen_addq_msw(tmp64, tmp);
8466                     } else {
8467                         tmp64 = gen_subq_msw(tmp64, tmp);
8468                     }
8469                 }
8470                 if (insn & (1 << 4)) {
8471                     tcg_gen_addi_i64(tmp64, tmp64, 0x80000000u);
8472                 }
8473                 tcg_gen_shri_i64(tmp64, tmp64, 32);
8474                 tmp = tcg_temp_new_i32();
8475                 tcg_gen_trunc_i64_i32(tmp, tmp64);
8476                 tcg_temp_free_i64(tmp64);
8477                 break;
8478             case 7: /* Unsigned sum of absolute differences.  */
8479                 gen_helper_usad8(tmp, tmp, tmp2);
8480                 tcg_temp_free_i32(tmp2);
8481                 if (rs != 15) {
8482                     tmp2 = load_reg(s, rs);
8483                     tcg_gen_add_i32(tmp, tmp, tmp2);
8484                     tcg_temp_free_i32(tmp2);
8485                 }
8486                 break;
8487             }
8488             store_reg(s, rd, tmp);
8489             break;
8490         case 6: case 7: /* 64-bit multiply, Divide.  */
8491             op = ((insn >> 4) & 0xf) | ((insn >> 16) & 0x70);
8492             tmp = load_reg(s, rn);
8493             tmp2 = load_reg(s, rm);
8494             if ((op & 0x50) == 0x10) {
8495                 /* sdiv, udiv */
8496                 if (!arm_feature(env, ARM_FEATURE_THUMB_DIV)) {
8497                     goto illegal_op;
8498                 }
8499                 if (op & 0x20)
8500                     gen_helper_udiv(tmp, tmp, tmp2);
8501                 else
8502                     gen_helper_sdiv(tmp, tmp, tmp2);
8503                 tcg_temp_free_i32(tmp2);
8504                 store_reg(s, rd, tmp);
8505             } else if ((op & 0xe) == 0xc) {
8506                 /* Dual multiply accumulate long.  */
8507                 if (op & 1)
8508                     gen_swap_half(tmp2);
8509                 gen_smul_dual(tmp, tmp2);
8510                 if (op & 0x10) {
8511                     tcg_gen_sub_i32(tmp, tmp, tmp2);
8512                 } else {
8513                     tcg_gen_add_i32(tmp, tmp, tmp2);
8514                 }
8515                 tcg_temp_free_i32(tmp2);
8516                 /* BUGFIX */
8517                 tmp64 = tcg_temp_new_i64();
8518                 tcg_gen_ext_i32_i64(tmp64, tmp);
8519                 tcg_temp_free_i32(tmp);
8520                 gen_addq(s, tmp64, rs, rd);
8521                 gen_storeq_reg(s, rs, rd, tmp64);
8522                 tcg_temp_free_i64(tmp64);
8523             } else {
8524                 if (op & 0x20) {
8525                     /* Unsigned 64-bit multiply  */
8526                     tmp64 = gen_mulu_i64_i32(tmp, tmp2);
8527                 } else {
8528                     if (op & 8) {
8529                         /* smlalxy */
8530                         gen_mulxy(tmp, tmp2, op & 2, op & 1);
8531                         tcg_temp_free_i32(tmp2);
8532                         tmp64 = tcg_temp_new_i64();
8533                         tcg_gen_ext_i32_i64(tmp64, tmp);
8534                         tcg_temp_free_i32(tmp);
8535                     } else {
8536                         /* Signed 64-bit multiply  */
8537                         tmp64 = gen_muls_i64_i32(tmp, tmp2);
8538                     }
8539                 }
8540                 if (op & 4) {
8541                     /* umaal */
8542                     gen_addq_lo(s, tmp64, rs);
8543                     gen_addq_lo(s, tmp64, rd);
8544                 } else if (op & 0x40) {
8545                     /* 64-bit accumulate.  */
8546                     gen_addq(s, tmp64, rs, rd);
8547                 }
8548                 gen_storeq_reg(s, rs, rd, tmp64);
8549                 tcg_temp_free_i64(tmp64);
8550             }
8551             break;
8552         }
8553         break;
8554     case 6: case 7: case 14: case 15:
8555         /* Coprocessor.  */
8556         if (((insn >> 24) & 3) == 3) {
8557             /* Translate into the equivalent ARM encoding.  */
8558             insn = (insn & 0xe2ffffff) | ((insn & (1 << 28)) >> 4) | (1 << 28);
8559             if (disas_neon_data_insn(env, s, insn))
8560                 goto illegal_op;
8561         } else {
8562             if (insn & (1 << 28))
8563                 goto illegal_op;
8564             if (disas_coproc_insn (env, s, insn))
8565                 goto illegal_op;
8566         }
8567         break;
8568     case 8: case 9: case 10: case 11:
8569         if (insn & (1 << 15)) {
8570             /* Branches, misc control.  */
8571             if (insn & 0x5000) {
8572                 /* Unconditional branch.  */
8573                 /* signextend(hw1[10:0]) -> offset[:12].  */
8574                 offset = ((int32_t)insn << 5) >> 9 & ~(int32_t)0xfff;
8575                 /* hw1[10:0] -> offset[11:1].  */
8576                 offset |= (insn & 0x7ff) << 1;
8577                 /* (~hw2[13, 11] ^ offset[24]) -> offset[23,22]
8578                    offset[24:22] already have the same value because of the
8579                    sign extension above.  */
8580                 offset ^= ((~insn) & (1 << 13)) << 10;
8581                 offset ^= ((~insn) & (1 << 11)) << 11;
8582
8583                 if (insn & (1 << 14)) {
8584                     /* Branch and link.  */
8585                     tcg_gen_movi_i32(cpu_R[14], s->pc | 1);
8586                 }
8587
8588                 offset += s->pc;
8589                 if (insn & (1 << 12)) {
8590                     /* b/bl */
8591                     gen_jmp(s, offset);
8592                 } else {
8593                     /* blx */
8594                     offset &= ~(uint32_t)2;
8595                     /* thumb2 bx, no need to check */
8596                     gen_bx_im(s, offset);
8597                 }
8598             } else if (((insn >> 23) & 7) == 7) {
8599                 /* Misc control */
8600                 if (insn & (1 << 13))
8601                     goto illegal_op;
8602
8603                 if (insn & (1 << 26)) {
8604                     /* Secure monitor call (v6Z) */
8605                     goto illegal_op; /* not implemented.  */
8606                 } else {
8607                     op = (insn >> 20) & 7;
8608                     switch (op) {
8609                     case 0: /* msr cpsr.  */
8610                         if (IS_M(env)) {
8611                             tmp = load_reg(s, rn);
8612                             addr = tcg_const_i32(insn & 0xff);
8613                             gen_helper_v7m_msr(cpu_env, addr, tmp);
8614                             tcg_temp_free_i32(addr);
8615                             tcg_temp_free_i32(tmp);
8616                             gen_lookup_tb(s);
8617                             break;
8618                         }
8619                         /* fall through */
8620                     case 1: /* msr spsr.  */
8621                         if (IS_M(env))
8622                             goto illegal_op;
8623                         tmp = load_reg(s, rn);
8624                         if (gen_set_psr(s,
8625                               msr_mask(env, s, (insn >> 8) & 0xf, op == 1),
8626                               op == 1, tmp))
8627                             goto illegal_op;
8628                         break;
8629                     case 2: /* cps, nop-hint.  */
8630                         if (((insn >> 8) & 7) == 0) {
8631                             gen_nop_hint(s, insn & 0xff);
8632                         }
8633                         /* Implemented as NOP in user mode.  */
8634                         if (IS_USER(s))
8635                             break;
8636                         offset = 0;
8637                         imm = 0;
8638                         if (insn & (1 << 10)) {
8639                             if (insn & (1 << 7))
8640                                 offset |= CPSR_A;
8641                             if (insn & (1 << 6))
8642                                 offset |= CPSR_I;
8643                             if (insn & (1 << 5))
8644                                 offset |= CPSR_F;
8645                             if (insn & (1 << 9))
8646                                 imm = CPSR_A | CPSR_I | CPSR_F;
8647                         }
8648                         if (insn & (1 << 8)) {
8649                             offset |= 0x1f;
8650                             imm |= (insn & 0x1f);
8651                         }
8652                         if (offset) {
8653                             gen_set_psr_im(s, offset, 0, imm);
8654                         }
8655                         break;
8656                     case 3: /* Special control operations.  */
8657                         ARCH(7);
8658                         op = (insn >> 4) & 0xf;
8659                         switch (op) {
8660                         case 2: /* clrex */
8661                             gen_clrex(s);
8662                             break;
8663                         case 4: /* dsb */
8664                         case 5: /* dmb */
8665                         case 6: /* isb */
8666                             /* These execute as NOPs.  */
8667                             break;
8668                         default:
8669                             goto illegal_op;
8670                         }
8671                         break;
8672                     case 4: /* bxj */
8673                         /* Trivial implementation equivalent to bx.  */
8674                         tmp = load_reg(s, rn);
8675                         gen_bx(s, tmp);
8676                         break;
8677                     case 5: /* Exception return.  */
8678                         if (IS_USER(s)) {
8679                             goto illegal_op;
8680                         }
8681                         if (rn != 14 || rd != 15) {
8682                             goto illegal_op;
8683                         }
8684                         tmp = load_reg(s, rn);
8685                         tcg_gen_subi_i32(tmp, tmp, insn & 0xff);
8686                         gen_exception_return(s, tmp);
8687                         break;
8688                     case 6: /* mrs cpsr.  */
8689                         tmp = tcg_temp_new_i32();
8690                         if (IS_M(env)) {
8691                             addr = tcg_const_i32(insn & 0xff);
8692                             gen_helper_v7m_mrs(tmp, cpu_env, addr);
8693                             tcg_temp_free_i32(addr);
8694                         } else {
8695                             gen_helper_cpsr_read(tmp);
8696                         }
8697                         store_reg(s, rd, tmp);
8698                         break;
8699                     case 7: /* mrs spsr.  */
8700                         /* Not accessible in user mode.  */
8701                         if (IS_USER(s) || IS_M(env))
8702                             goto illegal_op;
8703                         tmp = load_cpu_field(spsr);
8704                         store_reg(s, rd, tmp);
8705                         break;
8706                     }
8707                 }
8708             } else {
8709                 /* Conditional branch.  */
8710                 op = (insn >> 22) & 0xf;
8711                 /* Generate a conditional jump to next instruction.  */
8712                 s->condlabel = gen_new_label();
8713                 gen_test_cc(op ^ 1, s->condlabel);
8714                 s->condjmp = 1;
8715
8716                 /* offset[11:1] = insn[10:0] */
8717                 offset = (insn & 0x7ff) << 1;
8718                 /* offset[17:12] = insn[21:16].  */
8719                 offset |= (insn & 0x003f0000) >> 4;
8720                 /* offset[31:20] = insn[26].  */
8721                 offset |= ((int32_t)((insn << 5) & 0x80000000)) >> 11;
8722                 /* offset[18] = insn[13].  */
8723                 offset |= (insn & (1 << 13)) << 5;
8724                 /* offset[19] = insn[11].  */
8725                 offset |= (insn & (1 << 11)) << 8;
8726
8727                 /* jump to the offset */
8728                 gen_jmp(s, s->pc + offset);
8729             }
8730         } else {
8731             /* Data processing immediate.  */
8732             if (insn & (1 << 25)) {
8733                 if (insn & (1 << 24)) {
8734                     if (insn & (1 << 20))
8735                         goto illegal_op;
8736                     /* Bitfield/Saturate.  */
8737                     op = (insn >> 21) & 7;
8738                     imm = insn & 0x1f;
8739                     shift = ((insn >> 6) & 3) | ((insn >> 10) & 0x1c);
8740                     if (rn == 15) {
8741                         tmp = tcg_temp_new_i32();
8742                         tcg_gen_movi_i32(tmp, 0);
8743                     } else {
8744                         tmp = load_reg(s, rn);
8745                     }
8746                     switch (op) {
8747                     case 2: /* Signed bitfield extract.  */
8748                         imm++;
8749                         if (shift + imm > 32)
8750                             goto illegal_op;
8751                         if (imm < 32)
8752                             gen_sbfx(tmp, shift, imm);
8753                         break;
8754                     case 6: /* Unsigned bitfield extract.  */
8755                         imm++;
8756                         if (shift + imm > 32)
8757                             goto illegal_op;
8758                         if (imm < 32)
8759                             gen_ubfx(tmp, shift, (1u << imm) - 1);
8760                         break;
8761                     case 3: /* Bitfield insert/clear.  */
8762                         if (imm < shift)
8763                             goto illegal_op;
8764                         imm = imm + 1 - shift;
8765                         if (imm != 32) {
8766                             tmp2 = load_reg(s, rd);
8767                             gen_bfi(tmp, tmp2, tmp, shift, (1u << imm) - 1);
8768                             tcg_temp_free_i32(tmp2);
8769                         }
8770                         break;
8771                     case 7:
8772                         goto illegal_op;
8773                     default: /* Saturate.  */
8774                         if (shift) {
8775                             if (op & 1)
8776                                 tcg_gen_sari_i32(tmp, tmp, shift);
8777                             else
8778                                 tcg_gen_shli_i32(tmp, tmp, shift);
8779                         }
8780                         tmp2 = tcg_const_i32(imm);
8781                         if (op & 4) {
8782                             /* Unsigned.  */
8783                             if ((op & 1) && shift == 0)
8784                                 gen_helper_usat16(tmp, tmp, tmp2);
8785                             else
8786                                 gen_helper_usat(tmp, tmp, tmp2);
8787                         } else {
8788                             /* Signed.  */
8789                             if ((op & 1) && shift == 0)
8790                                 gen_helper_ssat16(tmp, tmp, tmp2);
8791                             else
8792                                 gen_helper_ssat(tmp, tmp, tmp2);
8793                         }
8794                         tcg_temp_free_i32(tmp2);
8795                         break;
8796                     }
8797                     store_reg(s, rd, tmp);
8798                 } else {
8799                     imm = ((insn & 0x04000000) >> 15)
8800                           | ((insn & 0x7000) >> 4) | (insn & 0xff);
8801                     if (insn & (1 << 22)) {
8802                         /* 16-bit immediate.  */
8803                         imm |= (insn >> 4) & 0xf000;
8804                         if (insn & (1 << 23)) {
8805                             /* movt */
8806                             tmp = load_reg(s, rd);
8807                             tcg_gen_ext16u_i32(tmp, tmp);
8808                             tcg_gen_ori_i32(tmp, tmp, imm << 16);
8809                         } else {
8810                             /* movw */
8811                             tmp = tcg_temp_new_i32();
8812                             tcg_gen_movi_i32(tmp, imm);
8813                         }
8814                     } else {
8815                         /* Add/sub 12-bit immediate.  */
8816                         if (rn == 15) {
8817                             offset = s->pc & ~(uint32_t)3;
8818                             if (insn & (1 << 23))
8819                                 offset -= imm;
8820                             else
8821                                 offset += imm;
8822                             tmp = tcg_temp_new_i32();
8823                             tcg_gen_movi_i32(tmp, offset);
8824                         } else {
8825                             tmp = load_reg(s, rn);
8826                             if (insn & (1 << 23))
8827                                 tcg_gen_subi_i32(tmp, tmp, imm);
8828                             else
8829                                 tcg_gen_addi_i32(tmp, tmp, imm);
8830                         }
8831                     }
8832                     store_reg(s, rd, tmp);
8833                 }
8834             } else {
8835                 int shifter_out = 0;
8836                 /* modified 12-bit immediate.  */
8837                 shift = ((insn & 0x04000000) >> 23) | ((insn & 0x7000) >> 12);
8838                 imm = (insn & 0xff);
8839                 switch (shift) {
8840                 case 0: /* XY */
8841                     /* Nothing to do.  */
8842                     break;
8843                 case 1: /* 00XY00XY */
8844                     imm |= imm << 16;
8845                     break;
8846                 case 2: /* XY00XY00 */
8847                     imm |= imm << 16;
8848                     imm <<= 8;
8849                     break;
8850                 case 3: /* XYXYXYXY */
8851                     imm |= imm << 16;
8852                     imm |= imm << 8;
8853                     break;
8854                 default: /* Rotated constant.  */
8855                     shift = (shift << 1) | (imm >> 7);
8856                     imm |= 0x80;
8857                     imm = imm << (32 - shift);
8858                     shifter_out = 1;
8859                     break;
8860                 }
8861                 tmp2 = tcg_temp_new_i32();
8862                 tcg_gen_movi_i32(tmp2, imm);
8863                 rn = (insn >> 16) & 0xf;
8864                 if (rn == 15) {
8865                     tmp = tcg_temp_new_i32();
8866                     tcg_gen_movi_i32(tmp, 0);
8867                 } else {
8868                     tmp = load_reg(s, rn);
8869                 }
8870                 op = (insn >> 21) & 0xf;
8871                 if (gen_thumb2_data_op(s, op, (insn & (1 << 20)) != 0,
8872                                        shifter_out, tmp, tmp2))
8873                     goto illegal_op;
8874                 tcg_temp_free_i32(tmp2);
8875                 rd = (insn >> 8) & 0xf;
8876                 if (rd != 15) {
8877                     store_reg(s, rd, tmp);
8878                 } else {
8879                     tcg_temp_free_i32(tmp);
8880                 }
8881             }
8882         }
8883         break;
8884     case 12: /* Load/store single data item.  */
8885         {
8886         int postinc = 0;
8887         int writeback = 0;
8888         int user;
8889         if ((insn & 0x01100000) == 0x01000000) {
8890             if (disas_neon_ls_insn(env, s, insn))
8891                 goto illegal_op;
8892             break;
8893         }
8894         op = ((insn >> 21) & 3) | ((insn >> 22) & 4);
8895         if (rs == 15) {
8896             if (!(insn & (1 << 20))) {
8897                 goto illegal_op;
8898             }
8899             if (op != 2) {
8900                 /* Byte or halfword load space with dest == r15 : memory hints.
8901                  * Catch them early so we don't emit pointless addressing code.
8902                  * This space is a mix of:
8903                  *  PLD/PLDW/PLI,  which we implement as NOPs (note that unlike
8904                  *     the ARM encodings, PLDW space doesn't UNDEF for non-v7MP
8905                  *     cores)
8906                  *  unallocated hints, which must be treated as NOPs
8907                  *  UNPREDICTABLE space, which we NOP or UNDEF depending on
8908                  *     which is easiest for the decoding logic
8909                  *  Some space which must UNDEF
8910                  */
8911                 int op1 = (insn >> 23) & 3;
8912                 int op2 = (insn >> 6) & 0x3f;
8913                 if (op & 2) {
8914                     goto illegal_op;
8915                 }
8916                 if (rn == 15) {
8917                     /* UNPREDICTABLE, unallocated hint or
8918                      * PLD/PLDW/PLI (literal)
8919                      */
8920                     return 0;
8921                 }
8922                 if (op1 & 1) {
8923                     return 0; /* PLD/PLDW/PLI or unallocated hint */
8924                 }
8925                 if ((op2 == 0) || ((op2 & 0x3c) == 0x30)) {
8926                     return 0; /* PLD/PLDW/PLI or unallocated hint */
8927                 }
8928                 /* UNDEF space, or an UNPREDICTABLE */
8929                 return 1;
8930             }
8931         }
8932         user = IS_USER(s);
8933         if (rn == 15) {
8934             addr = tcg_temp_new_i32();
8935             /* PC relative.  */
8936             /* s->pc has already been incremented by 4.  */
8937             imm = s->pc & 0xfffffffc;
8938             if (insn & (1 << 23))
8939                 imm += insn & 0xfff;
8940             else
8941                 imm -= insn & 0xfff;
8942             tcg_gen_movi_i32(addr, imm);
8943         } else {
8944             addr = load_reg(s, rn);
8945             if (insn & (1 << 23)) {
8946                 /* Positive offset.  */
8947                 imm = insn & 0xfff;
8948                 tcg_gen_addi_i32(addr, addr, imm);
8949             } else {
8950                 imm = insn & 0xff;
8951                 switch ((insn >> 8) & 0xf) {
8952                 case 0x0: /* Shifted Register.  */
8953                     shift = (insn >> 4) & 0xf;
8954                     if (shift > 3) {
8955                         tcg_temp_free_i32(addr);
8956                         goto illegal_op;
8957                     }
8958                     tmp = load_reg(s, rm);
8959                     if (shift)
8960                         tcg_gen_shli_i32(tmp, tmp, shift);
8961                     tcg_gen_add_i32(addr, addr, tmp);
8962                     tcg_temp_free_i32(tmp);
8963                     break;
8964                 case 0xc: /* Negative offset.  */
8965                     tcg_gen_addi_i32(addr, addr, -imm);
8966                     break;
8967                 case 0xe: /* User privilege.  */
8968                     tcg_gen_addi_i32(addr, addr, imm);
8969                     user = 1;
8970                     break;
8971                 case 0x9: /* Post-decrement.  */
8972                     imm = -imm;
8973                     /* Fall through.  */
8974                 case 0xb: /* Post-increment.  */
8975                     postinc = 1;
8976                     writeback = 1;
8977                     break;
8978                 case 0xd: /* Pre-decrement.  */
8979                     imm = -imm;
8980                     /* Fall through.  */
8981                 case 0xf: /* Pre-increment.  */
8982                     tcg_gen_addi_i32(addr, addr, imm);
8983                     writeback = 1;
8984                     break;
8985                 default:
8986                     tcg_temp_free_i32(addr);
8987                     goto illegal_op;
8988                 }
8989             }
8990         }
8991         if (insn & (1 << 20)) {
8992             /* Load.  */
8993             switch (op) {
8994             case 0: tmp = gen_ld8u(addr, user); break;
8995             case 4: tmp = gen_ld8s(addr, user); break;
8996             case 1: tmp = gen_ld16u(addr, user); break;
8997             case 5: tmp = gen_ld16s(addr, user); break;
8998             case 2: tmp = gen_ld32(addr, user); break;
8999             default:
9000                 tcg_temp_free_i32(addr);
9001                 goto illegal_op;
9002             }
9003             if (rs == 15) {
9004                 gen_bx(s, tmp);
9005             } else {
9006                 store_reg(s, rs, tmp);
9007             }
9008         } else {
9009             /* Store.  */
9010             tmp = load_reg(s, rs);
9011             switch (op) {
9012             case 0: gen_st8(tmp, addr, user); break;
9013             case 1: gen_st16(tmp, addr, user); break;
9014             case 2: gen_st32(tmp, addr, user); break;
9015             default:
9016                 tcg_temp_free_i32(addr);
9017                 goto illegal_op;
9018             }
9019         }
9020         if (postinc)
9021             tcg_gen_addi_i32(addr, addr, imm);
9022         if (writeback) {
9023             store_reg(s, rn, addr);
9024         } else {
9025             tcg_temp_free_i32(addr);
9026         }
9027         }
9028         break;
9029     default:
9030         goto illegal_op;
9031     }
9032     return 0;
9033 illegal_op:
9034     return 1;
9035 }
9036
9037 static void disas_thumb_insn(CPUARMState *env, DisasContext *s)
9038 {
9039     uint32_t val, insn, op, rm, rn, rd, shift, cond;
9040     int32_t offset;
9041     int i;
9042     TCGv tmp;
9043     TCGv tmp2;
9044     TCGv addr;
9045
9046     if (s->condexec_mask) {
9047         cond = s->condexec_cond;
9048         if (cond != 0x0e) {     /* Skip conditional when condition is AL. */
9049           s->condlabel = gen_new_label();
9050           gen_test_cc(cond ^ 1, s->condlabel);
9051           s->condjmp = 1;
9052         }
9053     }
9054
9055     insn = arm_lduw_code(s->pc, s->bswap_code);
9056     s->pc += 2;
9057
9058     switch (insn >> 12) {
9059     case 0: case 1:
9060
9061         rd = insn & 7;
9062         op = (insn >> 11) & 3;
9063         if (op == 3) {
9064             /* add/subtract */
9065             rn = (insn >> 3) & 7;
9066             tmp = load_reg(s, rn);
9067             if (insn & (1 << 10)) {
9068                 /* immediate */
9069                 tmp2 = tcg_temp_new_i32();
9070                 tcg_gen_movi_i32(tmp2, (insn >> 6) & 7);
9071             } else {
9072                 /* reg */
9073                 rm = (insn >> 6) & 7;
9074                 tmp2 = load_reg(s, rm);
9075             }
9076             if (insn & (1 << 9)) {
9077                 if (s->condexec_mask)
9078                     tcg_gen_sub_i32(tmp, tmp, tmp2);
9079                 else
9080                     gen_helper_sub_cc(tmp, tmp, tmp2);
9081             } else {
9082                 if (s->condexec_mask)
9083                     tcg_gen_add_i32(tmp, tmp, tmp2);
9084                 else
9085                     gen_helper_add_cc(tmp, tmp, tmp2);
9086             }
9087             tcg_temp_free_i32(tmp2);
9088             store_reg(s, rd, tmp);
9089         } else {
9090             /* shift immediate */
9091             rm = (insn >> 3) & 7;
9092             shift = (insn >> 6) & 0x1f;
9093             tmp = load_reg(s, rm);
9094             gen_arm_shift_im(tmp, op, shift, s->condexec_mask == 0);
9095             if (!s->condexec_mask)
9096                 gen_logic_CC(tmp);
9097             store_reg(s, rd, tmp);
9098         }
9099         break;
9100     case 2: case 3:
9101         /* arithmetic large immediate */
9102         op = (insn >> 11) & 3;
9103         rd = (insn >> 8) & 0x7;
9104         if (op == 0) { /* mov */
9105             tmp = tcg_temp_new_i32();
9106             tcg_gen_movi_i32(tmp, insn & 0xff);
9107             if (!s->condexec_mask)
9108                 gen_logic_CC(tmp);
9109             store_reg(s, rd, tmp);
9110         } else {
9111             tmp = load_reg(s, rd);
9112             tmp2 = tcg_temp_new_i32();
9113             tcg_gen_movi_i32(tmp2, insn & 0xff);
9114             switch (op) {
9115             case 1: /* cmp */
9116                 gen_helper_sub_cc(tmp, tmp, tmp2);
9117                 tcg_temp_free_i32(tmp);
9118                 tcg_temp_free_i32(tmp2);
9119                 break;
9120             case 2: /* add */
9121                 if (s->condexec_mask)
9122                     tcg_gen_add_i32(tmp, tmp, tmp2);
9123                 else
9124                     gen_helper_add_cc(tmp, tmp, tmp2);
9125                 tcg_temp_free_i32(tmp2);
9126                 store_reg(s, rd, tmp);
9127                 break;
9128             case 3: /* sub */
9129                 if (s->condexec_mask)
9130                     tcg_gen_sub_i32(tmp, tmp, tmp2);
9131                 else
9132                     gen_helper_sub_cc(tmp, tmp, tmp2);
9133                 tcg_temp_free_i32(tmp2);
9134                 store_reg(s, rd, tmp);
9135                 break;
9136             }
9137         }
9138         break;
9139     case 4:
9140         if (insn & (1 << 11)) {
9141             rd = (insn >> 8) & 7;
9142             /* load pc-relative.  Bit 1 of PC is ignored.  */
9143             val = s->pc + 2 + ((insn & 0xff) * 4);
9144             val &= ~(uint32_t)2;
9145             addr = tcg_temp_new_i32();
9146             tcg_gen_movi_i32(addr, val);
9147             tmp = gen_ld32(addr, IS_USER(s));
9148             tcg_temp_free_i32(addr);
9149             store_reg(s, rd, tmp);
9150             break;
9151         }
9152         if (insn & (1 << 10)) {
9153             /* data processing extended or blx */
9154             rd = (insn & 7) | ((insn >> 4) & 8);
9155             rm = (insn >> 3) & 0xf;
9156             op = (insn >> 8) & 3;
9157             switch (op) {
9158             case 0: /* add */
9159                 tmp = load_reg(s, rd);
9160                 tmp2 = load_reg(s, rm);
9161                 tcg_gen_add_i32(tmp, tmp, tmp2);
9162                 tcg_temp_free_i32(tmp2);
9163                 store_reg(s, rd, tmp);
9164                 break;
9165             case 1: /* cmp */
9166                 tmp = load_reg(s, rd);
9167                 tmp2 = load_reg(s, rm);
9168                 gen_helper_sub_cc(tmp, tmp, tmp2);
9169                 tcg_temp_free_i32(tmp2);
9170                 tcg_temp_free_i32(tmp);
9171                 break;
9172             case 2: /* mov/cpy */
9173                 tmp = load_reg(s, rm);
9174                 store_reg(s, rd, tmp);
9175                 break;
9176             case 3:/* branch [and link] exchange thumb register */
9177                 tmp = load_reg(s, rm);
9178                 if (insn & (1 << 7)) {
9179                     ARCH(5);
9180                     val = (uint32_t)s->pc | 1;
9181                     tmp2 = tcg_temp_new_i32();
9182                     tcg_gen_movi_i32(tmp2, val);
9183                     store_reg(s, 14, tmp2);
9184                 }
9185                 /* already thumb, no need to check */
9186                 gen_bx(s, tmp);
9187                 break;
9188             }
9189             break;
9190         }
9191
9192         /* data processing register */
9193         rd = insn & 7;
9194         rm = (insn >> 3) & 7;
9195         op = (insn >> 6) & 0xf;
9196         if (op == 2 || op == 3 || op == 4 || op == 7) {
9197             /* the shift/rotate ops want the operands backwards */
9198             val = rm;
9199             rm = rd;
9200             rd = val;
9201             val = 1;
9202         } else {
9203             val = 0;
9204         }
9205
9206         if (op == 9) { /* neg */
9207             tmp = tcg_temp_new_i32();
9208             tcg_gen_movi_i32(tmp, 0);
9209         } else if (op != 0xf) { /* mvn doesn't read its first operand */
9210             tmp = load_reg(s, rd);
9211         } else {
9212             TCGV_UNUSED(tmp);
9213         }
9214
9215         tmp2 = load_reg(s, rm);
9216         switch (op) {
9217         case 0x0: /* and */
9218             tcg_gen_and_i32(tmp, tmp, tmp2);
9219             if (!s->condexec_mask)
9220                 gen_logic_CC(tmp);
9221             break;
9222         case 0x1: /* eor */
9223             tcg_gen_xor_i32(tmp, tmp, tmp2);
9224             if (!s->condexec_mask)
9225                 gen_logic_CC(tmp);
9226             break;
9227         case 0x2: /* lsl */
9228             if (s->condexec_mask) {
9229                 gen_helper_shl(tmp2, tmp2, tmp);
9230             } else {
9231                 gen_helper_shl_cc(tmp2, tmp2, tmp);
9232                 gen_logic_CC(tmp2);
9233             }
9234             break;
9235         case 0x3: /* lsr */
9236             if (s->condexec_mask) {
9237                 gen_helper_shr(tmp2, tmp2, tmp);
9238             } else {
9239                 gen_helper_shr_cc(tmp2, tmp2, tmp);
9240                 gen_logic_CC(tmp2);
9241             }
9242             break;
9243         case 0x4: /* asr */
9244             if (s->condexec_mask) {
9245                 gen_helper_sar(tmp2, tmp2, tmp);
9246             } else {
9247                 gen_helper_sar_cc(tmp2, tmp2, tmp);
9248                 gen_logic_CC(tmp2);
9249             }
9250             break;
9251         case 0x5: /* adc */
9252             if (s->condexec_mask)
9253                 gen_adc(tmp, tmp2);
9254             else
9255                 gen_helper_adc_cc(tmp, tmp, tmp2);
9256             break;
9257         case 0x6: /* sbc */
9258             if (s->condexec_mask)
9259                 gen_sub_carry(tmp, tmp, tmp2);
9260             else
9261                 gen_helper_sbc_cc(tmp, tmp, tmp2);
9262             break;
9263         case 0x7: /* ror */
9264             if (s->condexec_mask) {
9265                 tcg_gen_andi_i32(tmp, tmp, 0x1f);
9266                 tcg_gen_rotr_i32(tmp2, tmp2, tmp);
9267             } else {
9268                 gen_helper_ror_cc(tmp2, tmp2, tmp);
9269                 gen_logic_CC(tmp2);
9270             }
9271             break;
9272         case 0x8: /* tst */
9273             tcg_gen_and_i32(tmp, tmp, tmp2);
9274             gen_logic_CC(tmp);
9275             rd = 16;
9276             break;
9277         case 0x9: /* neg */
9278             if (s->condexec_mask)
9279                 tcg_gen_neg_i32(tmp, tmp2);
9280             else
9281                 gen_helper_sub_cc(tmp, tmp, tmp2);
9282             break;
9283         case 0xa: /* cmp */
9284             gen_helper_sub_cc(tmp, tmp, tmp2);
9285             rd = 16;
9286             break;
9287         case 0xb: /* cmn */
9288             gen_helper_add_cc(tmp, tmp, tmp2);
9289             rd = 16;
9290             break;
9291         case 0xc: /* orr */
9292             tcg_gen_or_i32(tmp, tmp, tmp2);
9293             if (!s->condexec_mask)
9294                 gen_logic_CC(tmp);
9295             break;
9296         case 0xd: /* mul */
9297             tcg_gen_mul_i32(tmp, tmp, tmp2);
9298             if (!s->condexec_mask)
9299                 gen_logic_CC(tmp);
9300             break;
9301         case 0xe: /* bic */
9302             tcg_gen_andc_i32(tmp, tmp, tmp2);
9303             if (!s->condexec_mask)
9304                 gen_logic_CC(tmp);
9305             break;
9306         case 0xf: /* mvn */
9307             tcg_gen_not_i32(tmp2, tmp2);
9308             if (!s->condexec_mask)
9309                 gen_logic_CC(tmp2);
9310             val = 1;
9311             rm = rd;
9312             break;
9313         }
9314         if (rd != 16) {
9315             if (val) {
9316                 store_reg(s, rm, tmp2);
9317                 if (op != 0xf)
9318                     tcg_temp_free_i32(tmp);
9319             } else {
9320                 store_reg(s, rd, tmp);
9321                 tcg_temp_free_i32(tmp2);
9322             }
9323         } else {
9324             tcg_temp_free_i32(tmp);
9325             tcg_temp_free_i32(tmp2);
9326         }
9327         break;
9328
9329     case 5:
9330         /* load/store register offset.  */
9331         rd = insn & 7;
9332         rn = (insn >> 3) & 7;
9333         rm = (insn >> 6) & 7;
9334         op = (insn >> 9) & 7;
9335         addr = load_reg(s, rn);
9336         tmp = load_reg(s, rm);
9337         tcg_gen_add_i32(addr, addr, tmp);
9338         tcg_temp_free_i32(tmp);
9339
9340         if (op < 3) /* store */
9341             tmp = load_reg(s, rd);
9342
9343         switch (op) {
9344         case 0: /* str */
9345             gen_st32(tmp, addr, IS_USER(s));
9346             break;
9347         case 1: /* strh */
9348             gen_st16(tmp, addr, IS_USER(s));
9349             break;
9350         case 2: /* strb */
9351             gen_st8(tmp, addr, IS_USER(s));
9352             break;
9353         case 3: /* ldrsb */
9354             tmp = gen_ld8s(addr, IS_USER(s));
9355             break;
9356         case 4: /* ldr */
9357             tmp = gen_ld32(addr, IS_USER(s));
9358             break;
9359         case 5: /* ldrh */
9360             tmp = gen_ld16u(addr, IS_USER(s));
9361             break;
9362         case 6: /* ldrb */
9363             tmp = gen_ld8u(addr, IS_USER(s));
9364             break;
9365         case 7: /* ldrsh */
9366             tmp = gen_ld16s(addr, IS_USER(s));
9367             break;
9368         }
9369         if (op >= 3) /* load */
9370             store_reg(s, rd, tmp);
9371         tcg_temp_free_i32(addr);
9372         break;
9373
9374     case 6:
9375         /* load/store word immediate offset */
9376         rd = insn & 7;
9377         rn = (insn >> 3) & 7;
9378         addr = load_reg(s, rn);
9379         val = (insn >> 4) & 0x7c;
9380         tcg_gen_addi_i32(addr, addr, val);
9381
9382         if (insn & (1 << 11)) {
9383             /* load */
9384             tmp = gen_ld32(addr, IS_USER(s));
9385             store_reg(s, rd, tmp);
9386         } else {
9387             /* store */
9388             tmp = load_reg(s, rd);
9389             gen_st32(tmp, addr, IS_USER(s));
9390         }
9391         tcg_temp_free_i32(addr);
9392         break;
9393
9394     case 7:
9395         /* load/store byte immediate offset */
9396         rd = insn & 7;
9397         rn = (insn >> 3) & 7;
9398         addr = load_reg(s, rn);
9399         val = (insn >> 6) & 0x1f;
9400         tcg_gen_addi_i32(addr, addr, val);
9401
9402         if (insn & (1 << 11)) {
9403             /* load */
9404             tmp = gen_ld8u(addr, IS_USER(s));
9405             store_reg(s, rd, tmp);
9406         } else {
9407             /* store */
9408             tmp = load_reg(s, rd);
9409             gen_st8(tmp, addr, IS_USER(s));
9410         }
9411         tcg_temp_free_i32(addr);
9412         break;
9413
9414     case 8:
9415         /* load/store halfword immediate offset */
9416         rd = insn & 7;
9417         rn = (insn >> 3) & 7;
9418         addr = load_reg(s, rn);
9419         val = (insn >> 5) & 0x3e;
9420         tcg_gen_addi_i32(addr, addr, val);
9421
9422         if (insn & (1 << 11)) {
9423             /* load */
9424             tmp = gen_ld16u(addr, IS_USER(s));
9425             store_reg(s, rd, tmp);
9426         } else {
9427             /* store */
9428             tmp = load_reg(s, rd);
9429             gen_st16(tmp, addr, IS_USER(s));
9430         }
9431         tcg_temp_free_i32(addr);
9432         break;
9433
9434     case 9:
9435         /* load/store from stack */
9436         rd = (insn >> 8) & 7;
9437         addr = load_reg(s, 13);
9438         val = (insn & 0xff) * 4;
9439         tcg_gen_addi_i32(addr, addr, val);
9440
9441         if (insn & (1 << 11)) {
9442             /* load */
9443             tmp = gen_ld32(addr, IS_USER(s));
9444             store_reg(s, rd, tmp);
9445         } else {
9446             /* store */
9447             tmp = load_reg(s, rd);
9448             gen_st32(tmp, addr, IS_USER(s));
9449         }
9450         tcg_temp_free_i32(addr);
9451         break;
9452
9453     case 10:
9454         /* add to high reg */
9455         rd = (insn >> 8) & 7;
9456         if (insn & (1 << 11)) {
9457             /* SP */
9458             tmp = load_reg(s, 13);
9459         } else {
9460             /* PC. bit 1 is ignored.  */
9461             tmp = tcg_temp_new_i32();
9462             tcg_gen_movi_i32(tmp, (s->pc + 2) & ~(uint32_t)2);
9463         }
9464         val = (insn & 0xff) * 4;
9465         tcg_gen_addi_i32(tmp, tmp, val);
9466         store_reg(s, rd, tmp);
9467         break;
9468
9469     case 11:
9470         /* misc */
9471         op = (insn >> 8) & 0xf;
9472         switch (op) {
9473         case 0:
9474             /* adjust stack pointer */
9475             tmp = load_reg(s, 13);
9476             val = (insn & 0x7f) * 4;
9477             if (insn & (1 << 7))
9478                 val = -(int32_t)val;
9479             tcg_gen_addi_i32(tmp, tmp, val);
9480             store_reg(s, 13, tmp);
9481             break;
9482
9483         case 2: /* sign/zero extend.  */
9484             ARCH(6);
9485             rd = insn & 7;
9486             rm = (insn >> 3) & 7;
9487             tmp = load_reg(s, rm);
9488             switch ((insn >> 6) & 3) {
9489             case 0: gen_sxth(tmp); break;
9490             case 1: gen_sxtb(tmp); break;
9491             case 2: gen_uxth(tmp); break;
9492             case 3: gen_uxtb(tmp); break;
9493             }
9494             store_reg(s, rd, tmp);
9495             break;
9496         case 4: case 5: case 0xc: case 0xd:
9497             /* push/pop */
9498             addr = load_reg(s, 13);
9499             if (insn & (1 << 8))
9500                 offset = 4;
9501             else
9502                 offset = 0;
9503             for (i = 0; i < 8; i++) {
9504                 if (insn & (1 << i))
9505                     offset += 4;
9506             }
9507             if ((insn & (1 << 11)) == 0) {
9508                 tcg_gen_addi_i32(addr, addr, -offset);
9509             }
9510             for (i = 0; i < 8; i++) {
9511                 if (insn & (1 << i)) {
9512                     if (insn & (1 << 11)) {
9513                         /* pop */
9514                         tmp = gen_ld32(addr, IS_USER(s));
9515                         store_reg(s, i, tmp);
9516                     } else {
9517                         /* push */
9518                         tmp = load_reg(s, i);
9519                         gen_st32(tmp, addr, IS_USER(s));
9520                     }
9521                     /* advance to the next address.  */
9522                     tcg_gen_addi_i32(addr, addr, 4);
9523                 }
9524             }
9525             TCGV_UNUSED(tmp);
9526             if (insn & (1 << 8)) {
9527                 if (insn & (1 << 11)) {
9528                     /* pop pc */
9529                     tmp = gen_ld32(addr, IS_USER(s));
9530                     /* don't set the pc until the rest of the instruction
9531                        has completed */
9532                 } else {
9533                     /* push lr */
9534                     tmp = load_reg(s, 14);
9535                     gen_st32(tmp, addr, IS_USER(s));
9536                 }
9537                 tcg_gen_addi_i32(addr, addr, 4);
9538             }
9539             if ((insn & (1 << 11)) == 0) {
9540                 tcg_gen_addi_i32(addr, addr, -offset);
9541             }
9542             /* write back the new stack pointer */
9543             store_reg(s, 13, addr);
9544             /* set the new PC value */
9545             if ((insn & 0x0900) == 0x0900) {
9546                 store_reg_from_load(env, s, 15, tmp);
9547             }
9548             break;
9549
9550         case 1: case 3: case 9: case 11: /* czb */
9551             rm = insn & 7;
9552             tmp = load_reg(s, rm);
9553             s->condlabel = gen_new_label();
9554             s->condjmp = 1;
9555             if (insn & (1 << 11))
9556                 tcg_gen_brcondi_i32(TCG_COND_EQ, tmp, 0, s->condlabel);
9557             else
9558                 tcg_gen_brcondi_i32(TCG_COND_NE, tmp, 0, s->condlabel);
9559             tcg_temp_free_i32(tmp);
9560             offset = ((insn & 0xf8) >> 2) | (insn & 0x200) >> 3;
9561             val = (uint32_t)s->pc + 2;
9562             val += offset;
9563             gen_jmp(s, val);
9564             break;
9565
9566         case 15: /* IT, nop-hint.  */
9567             if ((insn & 0xf) == 0) {
9568                 gen_nop_hint(s, (insn >> 4) & 0xf);
9569                 break;
9570             }
9571             /* If Then.  */
9572             s->condexec_cond = (insn >> 4) & 0xe;
9573             s->condexec_mask = insn & 0x1f;
9574             /* No actual code generated for this insn, just setup state.  */
9575             break;
9576
9577         case 0xe: /* bkpt */
9578             ARCH(5);
9579             gen_exception_insn(s, 2, EXCP_BKPT);
9580             break;
9581
9582         case 0xa: /* rev */
9583             ARCH(6);
9584             rn = (insn >> 3) & 0x7;
9585             rd = insn & 0x7;
9586             tmp = load_reg(s, rn);
9587             switch ((insn >> 6) & 3) {
9588             case 0: tcg_gen_bswap32_i32(tmp, tmp); break;
9589             case 1: gen_rev16(tmp); break;
9590             case 3: gen_revsh(tmp); break;
9591             default: goto illegal_op;
9592             }
9593             store_reg(s, rd, tmp);
9594             break;
9595
9596         case 6:
9597             switch ((insn >> 5) & 7) {
9598             case 2:
9599                 /* setend */
9600                 ARCH(6);
9601                 if (((insn >> 3) & 1) != s->bswap_code) {
9602                     /* Dynamic endianness switching not implemented. */
9603                     goto illegal_op;
9604                 }
9605                 break;
9606             case 3:
9607                 /* cps */
9608                 ARCH(6);
9609                 if (IS_USER(s)) {
9610                     break;
9611                 }
9612                 if (IS_M(env)) {
9613                     tmp = tcg_const_i32((insn & (1 << 4)) != 0);
9614                     /* FAULTMASK */
9615                     if (insn & 1) {
9616                         addr = tcg_const_i32(19);
9617                         gen_helper_v7m_msr(cpu_env, addr, tmp);
9618                         tcg_temp_free_i32(addr);
9619                     }
9620                     /* PRIMASK */
9621                     if (insn & 2) {
9622                         addr = tcg_const_i32(16);
9623                         gen_helper_v7m_msr(cpu_env, addr, tmp);
9624                         tcg_temp_free_i32(addr);
9625                     }
9626                     tcg_temp_free_i32(tmp);
9627                     gen_lookup_tb(s);
9628                 } else {
9629                     if (insn & (1 << 4)) {
9630                         shift = CPSR_A | CPSR_I | CPSR_F;
9631                     } else {
9632                         shift = 0;
9633                     }
9634                     gen_set_psr_im(s, ((insn & 7) << 6), 0, shift);
9635                 }
9636                 break;
9637             default:
9638                 goto undef;
9639             }
9640             break;
9641
9642         default:
9643             goto undef;
9644         }
9645         break;
9646
9647     case 12:
9648     {
9649         /* load/store multiple */
9650         TCGv loaded_var;
9651         TCGV_UNUSED(loaded_var);
9652         rn = (insn >> 8) & 0x7;
9653         addr = load_reg(s, rn);
9654         for (i = 0; i < 8; i++) {
9655             if (insn & (1 << i)) {
9656                 if (insn & (1 << 11)) {
9657                     /* load */
9658                     tmp = gen_ld32(addr, IS_USER(s));
9659                     if (i == rn) {
9660                         loaded_var = tmp;
9661                     } else {
9662                         store_reg(s, i, tmp);
9663                     }
9664                 } else {
9665                     /* store */
9666                     tmp = load_reg(s, i);
9667                     gen_st32(tmp, addr, IS_USER(s));
9668                 }
9669                 /* advance to the next address */
9670                 tcg_gen_addi_i32(addr, addr, 4);
9671             }
9672         }
9673         if ((insn & (1 << rn)) == 0) {
9674             /* base reg not in list: base register writeback */
9675             store_reg(s, rn, addr);
9676         } else {
9677             /* base reg in list: if load, complete it now */
9678             if (insn & (1 << 11)) {
9679                 store_reg(s, rn, loaded_var);
9680             }
9681             tcg_temp_free_i32(addr);
9682         }
9683         break;
9684     }
9685     case 13:
9686         /* conditional branch or swi */
9687         cond = (insn >> 8) & 0xf;
9688         if (cond == 0xe)
9689             goto undef;
9690
9691         if (cond == 0xf) {
9692             /* swi */
9693             gen_set_pc_im(s->pc);
9694             s->is_jmp = DISAS_SWI;
9695             break;
9696         }
9697         /* generate a conditional jump to next instruction */
9698         s->condlabel = gen_new_label();
9699         gen_test_cc(cond ^ 1, s->condlabel);
9700         s->condjmp = 1;
9701
9702         /* jump to the offset */
9703         val = (uint32_t)s->pc + 2;
9704         offset = ((int32_t)insn << 24) >> 24;
9705         val += offset << 1;
9706         gen_jmp(s, val);
9707         break;
9708
9709     case 14:
9710         if (insn & (1 << 11)) {
9711             if (disas_thumb2_insn(env, s, insn))
9712               goto undef32;
9713             break;
9714         }
9715         /* unconditional branch */
9716         val = (uint32_t)s->pc;
9717         offset = ((int32_t)insn << 21) >> 21;
9718         val += (offset << 1) + 2;
9719         gen_jmp(s, val);
9720         break;
9721
9722     case 15:
9723         if (disas_thumb2_insn(env, s, insn))
9724             goto undef32;
9725         break;
9726     }
9727     return;
9728 undef32:
9729     gen_exception_insn(s, 4, EXCP_UDEF);
9730     return;
9731 illegal_op:
9732 undef:
9733     gen_exception_insn(s, 2, EXCP_UDEF);
9734 }
9735
9736 /* generate intermediate code in gen_opc_buf and gen_opparam_buf for
9737    basic block 'tb'. If search_pc is TRUE, also generate PC
9738    information for each intermediate instruction. */
9739 static inline void gen_intermediate_code_internal(CPUARMState *env,
9740                                                   TranslationBlock *tb,
9741                                                   int search_pc)
9742 {
9743     DisasContext dc1, *dc = &dc1;
9744     CPUBreakpoint *bp;
9745     uint16_t *gen_opc_end;
9746     int j, lj;
9747     target_ulong pc_start;
9748     uint32_t next_page_start;
9749     int num_insns;
9750     int max_insns;
9751
9752     /* generate intermediate code */
9753     pc_start = tb->pc;
9754
9755     dc->tb = tb;
9756
9757     gen_opc_end = gen_opc_buf + OPC_MAX_SIZE;
9758
9759     dc->is_jmp = DISAS_NEXT;
9760     dc->pc = pc_start;
9761     dc->singlestep_enabled = env->singlestep_enabled;
9762     dc->condjmp = 0;
9763     dc->thumb = ARM_TBFLAG_THUMB(tb->flags);
9764     dc->bswap_code = ARM_TBFLAG_BSWAP_CODE(tb->flags);
9765     dc->condexec_mask = (ARM_TBFLAG_CONDEXEC(tb->flags) & 0xf) << 1;
9766     dc->condexec_cond = ARM_TBFLAG_CONDEXEC(tb->flags) >> 4;
9767 #if !defined(CONFIG_USER_ONLY)
9768     dc->user = (ARM_TBFLAG_PRIV(tb->flags) == 0);
9769 #endif
9770     dc->vfp_enabled = ARM_TBFLAG_VFPEN(tb->flags);
9771     dc->vec_len = ARM_TBFLAG_VECLEN(tb->flags);
9772     dc->vec_stride = ARM_TBFLAG_VECSTRIDE(tb->flags);
9773     cpu_F0s = tcg_temp_new_i32();
9774     cpu_F1s = tcg_temp_new_i32();
9775     cpu_F0d = tcg_temp_new_i64();
9776     cpu_F1d = tcg_temp_new_i64();
9777     cpu_V0 = cpu_F0d;
9778     cpu_V1 = cpu_F1d;
9779     /* FIXME: cpu_M0 can probably be the same as cpu_V0.  */
9780     cpu_M0 = tcg_temp_new_i64();
9781     next_page_start = (pc_start & TARGET_PAGE_MASK) + TARGET_PAGE_SIZE;
9782     lj = -1;
9783     num_insns = 0;
9784     max_insns = tb->cflags & CF_COUNT_MASK;
9785     if (max_insns == 0)
9786         max_insns = CF_COUNT_MASK;
9787
9788     gen_icount_start();
9789
9790     tcg_clear_temp_count();
9791
9792     /* A note on handling of the condexec (IT) bits:
9793      *
9794      * We want to avoid the overhead of having to write the updated condexec
9795      * bits back to the CPUARMState for every instruction in an IT block. So:
9796      * (1) if the condexec bits are not already zero then we write
9797      * zero back into the CPUARMState now. This avoids complications trying
9798      * to do it at the end of the block. (For example if we don't do this
9799      * it's hard to identify whether we can safely skip writing condexec
9800      * at the end of the TB, which we definitely want to do for the case
9801      * where a TB doesn't do anything with the IT state at all.)
9802      * (2) if we are going to leave the TB then we call gen_set_condexec()
9803      * which will write the correct value into CPUARMState if zero is wrong.
9804      * This is done both for leaving the TB at the end, and for leaving
9805      * it because of an exception we know will happen, which is done in
9806      * gen_exception_insn(). The latter is necessary because we need to
9807      * leave the TB with the PC/IT state just prior to execution of the
9808      * instruction which caused the exception.
9809      * (3) if we leave the TB unexpectedly (eg a data abort on a load)
9810      * then the CPUARMState will be wrong and we need to reset it.
9811      * This is handled in the same way as restoration of the
9812      * PC in these situations: we will be called again with search_pc=1
9813      * and generate a mapping of the condexec bits for each PC in
9814      * gen_opc_condexec_bits[]. restore_state_to_opc() then uses
9815      * this to restore the condexec bits.
9816      *
9817      * Note that there are no instructions which can read the condexec
9818      * bits, and none which can write non-static values to them, so
9819      * we don't need to care about whether CPUARMState is correct in the
9820      * middle of a TB.
9821      */
9822
9823     /* Reset the conditional execution bits immediately. This avoids
9824        complications trying to do it at the end of the block.  */
9825     if (dc->condexec_mask || dc->condexec_cond)
9826       {
9827         TCGv tmp = tcg_temp_new_i32();
9828         tcg_gen_movi_i32(tmp, 0);
9829         store_cpu_field(tmp, condexec_bits);
9830       }
9831     do {
9832 #ifdef CONFIG_USER_ONLY
9833         /* Intercept jump to the magic kernel page.  */
9834         if (dc->pc >= 0xffff0000) {
9835             /* We always get here via a jump, so know we are not in a
9836                conditional execution block.  */
9837             gen_exception(EXCP_KERNEL_TRAP);
9838             dc->is_jmp = DISAS_UPDATE;
9839             break;
9840         }
9841 #else
9842         if (dc->pc >= 0xfffffff0 && IS_M(env)) {
9843             /* We always get here via a jump, so know we are not in a
9844                conditional execution block.  */
9845             gen_exception(EXCP_EXCEPTION_EXIT);
9846             dc->is_jmp = DISAS_UPDATE;
9847             break;
9848         }
9849 #endif
9850
9851         if (unlikely(!QTAILQ_EMPTY(&env->breakpoints))) {
9852             QTAILQ_FOREACH(bp, &env->breakpoints, entry) {
9853                 if (bp->pc == dc->pc) {
9854                     gen_exception_insn(dc, 0, EXCP_DEBUG);
9855                     /* Advance PC so that clearing the breakpoint will
9856                        invalidate this TB.  */
9857                     dc->pc += 2;
9858                     goto done_generating;
9859                     break;
9860                 }
9861             }
9862         }
9863         if (search_pc) {
9864             j = gen_opc_ptr - gen_opc_buf;
9865             if (lj < j) {
9866                 lj++;
9867                 while (lj < j)
9868                     gen_opc_instr_start[lj++] = 0;
9869             }
9870             gen_opc_pc[lj] = dc->pc;
9871             gen_opc_condexec_bits[lj] = (dc->condexec_cond << 4) | (dc->condexec_mask >> 1);
9872             gen_opc_instr_start[lj] = 1;
9873             gen_opc_icount[lj] = num_insns;
9874         }
9875
9876         if (num_insns + 1 == max_insns && (tb->cflags & CF_LAST_IO))
9877             gen_io_start();
9878
9879         if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP))) {
9880             tcg_gen_debug_insn_start(dc->pc);
9881         }
9882
9883         if (dc->thumb) {
9884             disas_thumb_insn(env, dc);
9885             if (dc->condexec_mask) {
9886                 dc->condexec_cond = (dc->condexec_cond & 0xe)
9887                                    | ((dc->condexec_mask >> 4) & 1);
9888                 dc->condexec_mask = (dc->condexec_mask << 1) & 0x1f;
9889                 if (dc->condexec_mask == 0) {
9890                     dc->condexec_cond = 0;
9891                 }
9892             }
9893         } else {
9894             disas_arm_insn(env, dc);
9895         }
9896
9897         if (dc->condjmp && !dc->is_jmp) {
9898             gen_set_label(dc->condlabel);
9899             dc->condjmp = 0;
9900         }
9901
9902         if (tcg_check_temp_count()) {
9903             fprintf(stderr, "TCG temporary leak before %08x\n", dc->pc);
9904         }
9905
9906         /* Translation stops when a conditional branch is encountered.
9907          * Otherwise the subsequent code could get translated several times.
9908          * Also stop translation when a page boundary is reached.  This
9909          * ensures prefetch aborts occur at the right place.  */
9910         num_insns ++;
9911     } while (!dc->is_jmp && gen_opc_ptr < gen_opc_end &&
9912              !env->singlestep_enabled &&
9913              !singlestep &&
9914              dc->pc < next_page_start &&
9915              num_insns < max_insns);
9916
9917     if (tb->cflags & CF_LAST_IO) {
9918         if (dc->condjmp) {
9919             /* FIXME:  This can theoretically happen with self-modifying
9920                code.  */
9921             cpu_abort(env, "IO on conditional branch instruction");
9922         }
9923         gen_io_end();
9924     }
9925
9926     /* At this stage dc->condjmp will only be set when the skipped
9927        instruction was a conditional branch or trap, and the PC has
9928        already been written.  */
9929     if (unlikely(env->singlestep_enabled)) {
9930         /* Make sure the pc is updated, and raise a debug exception.  */
9931         if (dc->condjmp) {
9932             gen_set_condexec(dc);
9933             if (dc->is_jmp == DISAS_SWI) {
9934                 gen_exception(EXCP_SWI);
9935             } else {
9936                 gen_exception(EXCP_DEBUG);
9937             }
9938             gen_set_label(dc->condlabel);
9939         }
9940         if (dc->condjmp || !dc->is_jmp) {
9941             gen_set_pc_im(dc->pc);
9942             dc->condjmp = 0;
9943         }
9944         gen_set_condexec(dc);
9945         if (dc->is_jmp == DISAS_SWI && !dc->condjmp) {
9946             gen_exception(EXCP_SWI);
9947         } else {
9948             /* FIXME: Single stepping a WFI insn will not halt
9949                the CPU.  */
9950             gen_exception(EXCP_DEBUG);
9951         }
9952     } else {
9953         /* While branches must always occur at the end of an IT block,
9954            there are a few other things that can cause us to terminate
9955            the TB in the middel of an IT block:
9956             - Exception generating instructions (bkpt, swi, undefined).
9957             - Page boundaries.
9958             - Hardware watchpoints.
9959            Hardware breakpoints have already been handled and skip this code.
9960          */
9961         gen_set_condexec(dc);
9962         switch(dc->is_jmp) {
9963         case DISAS_NEXT:
9964             gen_goto_tb(dc, 1, dc->pc);
9965             break;
9966         default:
9967         case DISAS_JUMP:
9968         case DISAS_UPDATE:
9969             /* indicate that the hash table must be used to find the next TB */
9970             tcg_gen_exit_tb(0);
9971             break;
9972         case DISAS_TB_JUMP:
9973             /* nothing more to generate */
9974             break;
9975         case DISAS_WFI:
9976             gen_helper_wfi();
9977             break;
9978         case DISAS_SWI:
9979             gen_exception(EXCP_SWI);
9980             break;
9981         }
9982         if (dc->condjmp) {
9983             gen_set_label(dc->condlabel);
9984             gen_set_condexec(dc);
9985             gen_goto_tb(dc, 1, dc->pc);
9986             dc->condjmp = 0;
9987         }
9988     }
9989
9990 done_generating:
9991     gen_icount_end(tb, num_insns);
9992     *gen_opc_ptr = INDEX_op_end;
9993
9994 #ifdef DEBUG_DISAS
9995     if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)) {
9996         qemu_log("----------------\n");
9997         qemu_log("IN: %s\n", lookup_symbol(pc_start));
9998         log_target_disas(pc_start, dc->pc - pc_start,
9999                          dc->thumb | (dc->bswap_code << 1));
10000         qemu_log("\n");
10001     }
10002 #endif
10003     if (search_pc) {
10004         j = gen_opc_ptr - gen_opc_buf;
10005         lj++;
10006         while (lj <= j)
10007             gen_opc_instr_start[lj++] = 0;
10008     } else {
10009         tb->size = dc->pc - pc_start;
10010         tb->icount = num_insns;
10011     }
10012 }
10013
10014 void gen_intermediate_code(CPUARMState *env, TranslationBlock *tb)
10015 {
10016     gen_intermediate_code_internal(env, tb, 0);
10017 }
10018
10019 void gen_intermediate_code_pc(CPUARMState *env, TranslationBlock *tb)
10020 {
10021     gen_intermediate_code_internal(env, tb, 1);
10022 }
10023
10024 static const char *cpu_mode_names[16] = {
10025   "usr", "fiq", "irq", "svc", "???", "???", "???", "abt",
10026   "???", "???", "???", "und", "???", "???", "???", "sys"
10027 };
10028
10029 void cpu_dump_state(CPUARMState *env, FILE *f, fprintf_function cpu_fprintf,
10030                     int flags)
10031 {
10032     int i;
10033 #if 0
10034     union {
10035         uint32_t i;
10036         float s;
10037     } s0, s1;
10038     CPU_DoubleU d;
10039     /* ??? This assumes float64 and double have the same layout.
10040        Oh well, it's only debug dumps.  */
10041     union {
10042         float64 f64;
10043         double d;
10044     } d0;
10045 #endif
10046     uint32_t psr;
10047
10048     for(i=0;i<16;i++) {
10049         cpu_fprintf(f, "R%02d=%08x", i, env->regs[i]);
10050         if ((i % 4) == 3)
10051             cpu_fprintf(f, "\n");
10052         else
10053             cpu_fprintf(f, " ");
10054     }
10055     psr = cpsr_read(env);
10056     cpu_fprintf(f, "PSR=%08x %c%c%c%c %c %s%d\n",
10057                 psr,
10058                 psr & (1 << 31) ? 'N' : '-',
10059                 psr & (1 << 30) ? 'Z' : '-',
10060                 psr & (1 << 29) ? 'C' : '-',
10061                 psr & (1 << 28) ? 'V' : '-',
10062                 psr & CPSR_T ? 'T' : 'A',
10063                 cpu_mode_names[psr & 0xf], (psr & 0x10) ? 32 : 26);
10064
10065 #if 0
10066     for (i = 0; i < 16; i++) {
10067         d.d = env->vfp.regs[i];
10068         s0.i = d.l.lower;
10069         s1.i = d.l.upper;
10070         d0.f64 = d.d;
10071         cpu_fprintf(f, "s%02d=%08x(%8g) s%02d=%08x(%8g) d%02d=%08x%08x(%8g)\n",
10072                     i * 2, (int)s0.i, s0.s,
10073                     i * 2 + 1, (int)s1.i, s1.s,
10074                     i, (int)(uint32_t)d.l.upper, (int)(uint32_t)d.l.lower,
10075                     d0.d);
10076     }
10077     cpu_fprintf(f, "FPSCR: %08x\n", (int)env->vfp.xregs[ARM_VFP_FPSCR]);
10078 #endif
10079 }
10080
10081 void restore_state_to_opc(CPUARMState *env, TranslationBlock *tb, int pc_pos)
10082 {
10083     env->regs[15] = gen_opc_pc[pc_pos];
10084     env->condexec_bits = gen_opc_condexec_bits[pc_pos];
10085 }
This page took 0.638542 seconds and 4 git commands to generate.