Statistics
| Branch: | Revision:

root / target-arm / translate.c @ b8b45b68

History | View | Annotate | Download (312.3 kB)

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 "exec-all.h"
29
#include "disas.h"
30
#include "tcg-op.h"
31
#include "qemu-log.h"
32

    
33
#include "helpers.h"
34
#define GEN_HELPER 1
35
#include "helpers.h"
36

    
37
#define ENABLE_ARCH_5J    0
38
#define ENABLE_ARCH_6     arm_feature(env, ARM_FEATURE_V6)
39
#define ENABLE_ARCH_6K   arm_feature(env, ARM_FEATURE_V6K)
40
#define ENABLE_ARCH_6T2   arm_feature(env, ARM_FEATURE_THUMB2)
41
#define ENABLE_ARCH_7     arm_feature(env, ARM_FEATURE_V7)
42

    
43
#define ARCH(x) do { if (!ENABLE_ARCH_##x) goto illegal_op; } while(0)
44

    
45
/* internal defines */
46
typedef struct DisasContext {
47
    target_ulong pc;
48
    int is_jmp;
49
    /* Nonzero if this instruction has been conditionally skipped.  */
50
    int condjmp;
51
    /* The label that will be jumped to when the instruction is skipped.  */
52
    int condlabel;
53
    /* Thumb-2 condtional execution bits.  */
54
    int condexec_mask;
55
    int condexec_cond;
56
    struct TranslationBlock *tb;
57
    int singlestep_enabled;
58
    int thumb;
59
#if !defined(CONFIG_USER_ONLY)
60
    int user;
61
#endif
62
} DisasContext;
63

    
64
#if defined(CONFIG_USER_ONLY)
65
#define IS_USER(s) 1
66
#else
67
#define IS_USER(s) (s->user)
68
#endif
69

    
70
/* These instructions trap after executing, so defer them until after the
71
   conditional executions state has been updated.  */
72
#define DISAS_WFI 4
73
#define DISAS_SWI 5
74

    
75
static TCGv_ptr cpu_env;
76
/* We reuse the same 64-bit temporaries for efficiency.  */
77
static TCGv_i64 cpu_V0, cpu_V1, cpu_M0;
78
static TCGv_i32 cpu_R[16];
79
static TCGv_i32 cpu_exclusive_addr;
80
static TCGv_i32 cpu_exclusive_val;
81
static TCGv_i32 cpu_exclusive_high;
82
#ifdef CONFIG_USER_ONLY
83
static TCGv_i32 cpu_exclusive_test;
84
static TCGv_i32 cpu_exclusive_info;
85
#endif
86

    
87
/* FIXME:  These should be removed.  */
88
static TCGv cpu_F0s, cpu_F1s;
89
static TCGv_i64 cpu_F0d, cpu_F1d;
90

    
91
#include "gen-icount.h"
92

    
93
static const char *regnames[] =
94
    { "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
95
      "r8", "r9", "r10", "r11", "r12", "r13", "r14", "pc" };
96

    
97
/* initialize TCG globals.  */
98
void arm_translate_init(void)
99
{
100
    int i;
101

    
102
    cpu_env = tcg_global_reg_new_ptr(TCG_AREG0, "env");
103

    
104
    for (i = 0; i < 16; i++) {
105
        cpu_R[i] = tcg_global_mem_new_i32(TCG_AREG0,
106
                                          offsetof(CPUState, regs[i]),
107
                                          regnames[i]);
108
    }
109
    cpu_exclusive_addr = tcg_global_mem_new_i32(TCG_AREG0,
110
        offsetof(CPUState, exclusive_addr), "exclusive_addr");
111
    cpu_exclusive_val = tcg_global_mem_new_i32(TCG_AREG0,
112
        offsetof(CPUState, exclusive_val), "exclusive_val");
113
    cpu_exclusive_high = tcg_global_mem_new_i32(TCG_AREG0,
114
        offsetof(CPUState, exclusive_high), "exclusive_high");
115
#ifdef CONFIG_USER_ONLY
116
    cpu_exclusive_test = tcg_global_mem_new_i32(TCG_AREG0,
117
        offsetof(CPUState, exclusive_test), "exclusive_test");
118
    cpu_exclusive_info = tcg_global_mem_new_i32(TCG_AREG0,
119
        offsetof(CPUState, exclusive_info), "exclusive_info");
120
#endif
121

    
122
#define GEN_HELPER 2
123
#include "helpers.h"
124
}
125

    
126
static int num_temps;
127

    
128
/* Allocate a temporary variable.  */
129
static TCGv_i32 new_tmp(void)
130
{
131
    num_temps++;
132
    return tcg_temp_new_i32();
133
}
134

    
135
/* Release a temporary variable.  */
136
static void dead_tmp(TCGv tmp)
137
{
138
    tcg_temp_free(tmp);
139
    num_temps--;
140
}
141

    
142
static inline TCGv load_cpu_offset(int offset)
143
{
144
    TCGv tmp = new_tmp();
145
    tcg_gen_ld_i32(tmp, cpu_env, offset);
146
    return tmp;
147
}
148

    
149
#define load_cpu_field(name) load_cpu_offset(offsetof(CPUState, name))
150

    
151
static inline void store_cpu_offset(TCGv var, int offset)
152
{
153
    tcg_gen_st_i32(var, cpu_env, offset);
154
    dead_tmp(var);
155
}
156

    
157
#define store_cpu_field(var, name) \
158
    store_cpu_offset(var, offsetof(CPUState, name))
159

    
160
/* Set a variable to the value of a CPU register.  */
161
static void load_reg_var(DisasContext *s, TCGv var, int reg)
162
{
163
    if (reg == 15) {
164
        uint32_t addr;
165
        /* normaly, since we updated PC, we need only to add one insn */
166
        if (s->thumb)
167
            addr = (long)s->pc + 2;
168
        else
169
            addr = (long)s->pc + 4;
170
        tcg_gen_movi_i32(var, addr);
171
    } else {
172
        tcg_gen_mov_i32(var, cpu_R[reg]);
173
    }
174
}
175

    
176
/* Create a new temporary and set it to the value of a CPU register.  */
177
static inline TCGv load_reg(DisasContext *s, int reg)
178
{
179
    TCGv tmp = new_tmp();
180
    load_reg_var(s, tmp, reg);
181
    return tmp;
182
}
183

    
184
/* Set a CPU register.  The source must be a temporary and will be
185
   marked as dead.  */
186
static void store_reg(DisasContext *s, int reg, TCGv var)
187
{
188
    if (reg == 15) {
189
        tcg_gen_andi_i32(var, var, ~1);
190
        s->is_jmp = DISAS_JUMP;
191
    }
192
    tcg_gen_mov_i32(cpu_R[reg], var);
193
    dead_tmp(var);
194
}
195

    
196
/* Value extensions.  */
197
#define gen_uxtb(var) tcg_gen_ext8u_i32(var, var)
198
#define gen_uxth(var) tcg_gen_ext16u_i32(var, var)
199
#define gen_sxtb(var) tcg_gen_ext8s_i32(var, var)
200
#define gen_sxth(var) tcg_gen_ext16s_i32(var, var)
201

    
202
#define gen_sxtb16(var) gen_helper_sxtb16(var, var)
203
#define gen_uxtb16(var) gen_helper_uxtb16(var, var)
204

    
205

    
206
static inline void gen_set_cpsr(TCGv var, uint32_t mask)
207
{
208
    TCGv tmp_mask = tcg_const_i32(mask);
209
    gen_helper_cpsr_write(var, tmp_mask);
210
    tcg_temp_free_i32(tmp_mask);
211
}
212
/* Set NZCV flags from the high 4 bits of var.  */
213
#define gen_set_nzcv(var) gen_set_cpsr(var, CPSR_NZCV)
214

    
215
static void gen_exception(int excp)
216
{
217
    TCGv tmp = new_tmp();
218
    tcg_gen_movi_i32(tmp, excp);
219
    gen_helper_exception(tmp);
220
    dead_tmp(tmp);
221
}
222

    
223
static void gen_smul_dual(TCGv a, TCGv b)
224
{
225
    TCGv tmp1 = new_tmp();
226
    TCGv tmp2 = new_tmp();
227
    tcg_gen_ext16s_i32(tmp1, a);
228
    tcg_gen_ext16s_i32(tmp2, b);
229
    tcg_gen_mul_i32(tmp1, tmp1, tmp2);
230
    dead_tmp(tmp2);
231
    tcg_gen_sari_i32(a, a, 16);
232
    tcg_gen_sari_i32(b, b, 16);
233
    tcg_gen_mul_i32(b, b, a);
234
    tcg_gen_mov_i32(a, tmp1);
235
    dead_tmp(tmp1);
236
}
237

    
238
/* Byteswap each halfword.  */
239
static void gen_rev16(TCGv var)
240
{
241
    TCGv tmp = new_tmp();
242
    tcg_gen_shri_i32(tmp, var, 8);
243
    tcg_gen_andi_i32(tmp, tmp, 0x00ff00ff);
244
    tcg_gen_shli_i32(var, var, 8);
245
    tcg_gen_andi_i32(var, var, 0xff00ff00);
246
    tcg_gen_or_i32(var, var, tmp);
247
    dead_tmp(tmp);
248
}
249

    
250
/* Byteswap low halfword and sign extend.  */
251
static void gen_revsh(TCGv var)
252
{
253
    TCGv tmp = new_tmp();
254
    tcg_gen_shri_i32(tmp, var, 8);
255
    tcg_gen_andi_i32(tmp, tmp, 0x00ff);
256
    tcg_gen_shli_i32(var, var, 8);
257
    tcg_gen_ext8s_i32(var, var);
258
    tcg_gen_or_i32(var, var, tmp);
259
    dead_tmp(tmp);
260
}
261

    
262
/* Unsigned bitfield extract.  */
263
static void gen_ubfx(TCGv var, int shift, uint32_t mask)
264
{
265
    if (shift)
266
        tcg_gen_shri_i32(var, var, shift);
267
    tcg_gen_andi_i32(var, var, mask);
268
}
269

    
270
/* Signed bitfield extract.  */
271
static void gen_sbfx(TCGv var, int shift, int width)
272
{
273
    uint32_t signbit;
274

    
275
    if (shift)
276
        tcg_gen_sari_i32(var, var, shift);
277
    if (shift + width < 32) {
278
        signbit = 1u << (width - 1);
279
        tcg_gen_andi_i32(var, var, (1u << width) - 1);
280
        tcg_gen_xori_i32(var, var, signbit);
281
        tcg_gen_subi_i32(var, var, signbit);
282
    }
283
}
284

    
285
/* Bitfield insertion.  Insert val into base.  Clobbers base and val.  */
286
static void gen_bfi(TCGv dest, TCGv base, TCGv val, int shift, uint32_t mask)
287
{
288
    tcg_gen_andi_i32(val, val, mask);
289
    tcg_gen_shli_i32(val, val, shift);
290
    tcg_gen_andi_i32(base, base, ~(mask << shift));
291
    tcg_gen_or_i32(dest, base, val);
292
}
293

    
294
/* Round the top 32 bits of a 64-bit value.  */
295
static void gen_roundqd(TCGv a, TCGv b)
296
{
297
    tcg_gen_shri_i32(a, a, 31);
298
    tcg_gen_add_i32(a, a, b);
299
}
300

    
301
/* FIXME: Most targets have native widening multiplication.
302
   It would be good to use that instead of a full wide multiply.  */
303
/* 32x32->64 multiply.  Marks inputs as dead.  */
304
static TCGv_i64 gen_mulu_i64_i32(TCGv a, TCGv b)
305
{
306
    TCGv_i64 tmp1 = tcg_temp_new_i64();
307
    TCGv_i64 tmp2 = tcg_temp_new_i64();
308

    
309
    tcg_gen_extu_i32_i64(tmp1, a);
310
    dead_tmp(a);
311
    tcg_gen_extu_i32_i64(tmp2, b);
312
    dead_tmp(b);
313
    tcg_gen_mul_i64(tmp1, tmp1, tmp2);
314
    tcg_temp_free_i64(tmp2);
315
    return tmp1;
316
}
317

    
318
static TCGv_i64 gen_muls_i64_i32(TCGv a, TCGv b)
319
{
320
    TCGv_i64 tmp1 = tcg_temp_new_i64();
321
    TCGv_i64 tmp2 = tcg_temp_new_i64();
322

    
323
    tcg_gen_ext_i32_i64(tmp1, a);
324
    dead_tmp(a);
325
    tcg_gen_ext_i32_i64(tmp2, b);
326
    dead_tmp(b);
327
    tcg_gen_mul_i64(tmp1, tmp1, tmp2);
328
    tcg_temp_free_i64(tmp2);
329
    return tmp1;
330
}
331

    
332
/* Signed 32x32->64 multiply.  */
333
static void gen_imull(TCGv a, TCGv b)
334
{
335
    TCGv_i64 tmp1 = tcg_temp_new_i64();
336
    TCGv_i64 tmp2 = tcg_temp_new_i64();
337

    
338
    tcg_gen_ext_i32_i64(tmp1, a);
339
    tcg_gen_ext_i32_i64(tmp2, b);
340
    tcg_gen_mul_i64(tmp1, tmp1, tmp2);
341
    tcg_temp_free_i64(tmp2);
342
    tcg_gen_trunc_i64_i32(a, tmp1);
343
    tcg_gen_shri_i64(tmp1, tmp1, 32);
344
    tcg_gen_trunc_i64_i32(b, tmp1);
345
    tcg_temp_free_i64(tmp1);
346
}
347

    
348
/* Swap low and high halfwords.  */
349
static void gen_swap_half(TCGv var)
350
{
351
    TCGv tmp = new_tmp();
352
    tcg_gen_shri_i32(tmp, var, 16);
353
    tcg_gen_shli_i32(var, var, 16);
354
    tcg_gen_or_i32(var, var, tmp);
355
    dead_tmp(tmp);
356
}
357

    
358
/* Dual 16-bit add.  Result placed in t0 and t1 is marked as dead.
359
    tmp = (t0 ^ t1) & 0x8000;
360
    t0 &= ~0x8000;
361
    t1 &= ~0x8000;
362
    t0 = (t0 + t1) ^ tmp;
363
 */
364

    
365
static void gen_add16(TCGv t0, TCGv t1)
366
{
367
    TCGv tmp = new_tmp();
368
    tcg_gen_xor_i32(tmp, t0, t1);
369
    tcg_gen_andi_i32(tmp, tmp, 0x8000);
370
    tcg_gen_andi_i32(t0, t0, ~0x8000);
371
    tcg_gen_andi_i32(t1, t1, ~0x8000);
372
    tcg_gen_add_i32(t0, t0, t1);
373
    tcg_gen_xor_i32(t0, t0, tmp);
374
    dead_tmp(tmp);
375
    dead_tmp(t1);
376
}
377

    
378
#define gen_set_CF(var) tcg_gen_st_i32(var, cpu_env, offsetof(CPUState, CF))
379

    
380
/* Set CF to the top bit of var.  */
381
static void gen_set_CF_bit31(TCGv var)
382
{
383
    TCGv tmp = new_tmp();
384
    tcg_gen_shri_i32(tmp, var, 31);
385
    gen_set_CF(tmp);
386
    dead_tmp(tmp);
387
}
388

    
389
/* Set N and Z flags from var.  */
390
static inline void gen_logic_CC(TCGv var)
391
{
392
    tcg_gen_st_i32(var, cpu_env, offsetof(CPUState, NF));
393
    tcg_gen_st_i32(var, cpu_env, offsetof(CPUState, ZF));
394
}
395

    
396
/* T0 += T1 + CF.  */
397
static void gen_adc(TCGv t0, TCGv t1)
398
{
399
    TCGv tmp;
400
    tcg_gen_add_i32(t0, t0, t1);
401
    tmp = load_cpu_field(CF);
402
    tcg_gen_add_i32(t0, t0, tmp);
403
    dead_tmp(tmp);
404
}
405

    
406
/* dest = T0 + T1 + CF. */
407
static void gen_add_carry(TCGv dest, TCGv t0, TCGv t1)
408
{
409
    TCGv tmp;
410
    tcg_gen_add_i32(dest, t0, t1);
411
    tmp = load_cpu_field(CF);
412
    tcg_gen_add_i32(dest, dest, tmp);
413
    dead_tmp(tmp);
414
}
415

    
416
/* dest = T0 - T1 + CF - 1.  */
417
static void gen_sub_carry(TCGv dest, TCGv t0, TCGv t1)
418
{
419
    TCGv tmp;
420
    tcg_gen_sub_i32(dest, t0, t1);
421
    tmp = load_cpu_field(CF);
422
    tcg_gen_add_i32(dest, dest, tmp);
423
    tcg_gen_subi_i32(dest, dest, 1);
424
    dead_tmp(tmp);
425
}
426

    
427
/* FIXME:  Implement this natively.  */
428
#define tcg_gen_abs_i32(t0, t1) gen_helper_abs(t0, t1)
429

    
430
static void shifter_out_im(TCGv var, int shift)
431
{
432
    TCGv tmp = new_tmp();
433
    if (shift == 0) {
434
        tcg_gen_andi_i32(tmp, var, 1);
435
    } else {
436
        tcg_gen_shri_i32(tmp, var, shift);
437
        if (shift != 31)
438
            tcg_gen_andi_i32(tmp, tmp, 1);
439
    }
440
    gen_set_CF(tmp);
441
    dead_tmp(tmp);
442
}
443

    
444
/* Shift by immediate.  Includes special handling for shift == 0.  */
445
static inline void gen_arm_shift_im(TCGv var, int shiftop, int shift, int flags)
446
{
447
    switch (shiftop) {
448
    case 0: /* LSL */
449
        if (shift != 0) {
450
            if (flags)
451
                shifter_out_im(var, 32 - shift);
452
            tcg_gen_shli_i32(var, var, shift);
453
        }
454
        break;
455
    case 1: /* LSR */
456
        if (shift == 0) {
457
            if (flags) {
458
                tcg_gen_shri_i32(var, var, 31);
459
                gen_set_CF(var);
460
            }
461
            tcg_gen_movi_i32(var, 0);
462
        } else {
463
            if (flags)
464
                shifter_out_im(var, shift - 1);
465
            tcg_gen_shri_i32(var, var, shift);
466
        }
467
        break;
468
    case 2: /* ASR */
469
        if (shift == 0)
470
            shift = 32;
471
        if (flags)
472
            shifter_out_im(var, shift - 1);
473
        if (shift == 32)
474
          shift = 31;
475
        tcg_gen_sari_i32(var, var, shift);
476
        break;
477
    case 3: /* ROR/RRX */
478
        if (shift != 0) {
479
            if (flags)
480
                shifter_out_im(var, shift - 1);
481
            tcg_gen_rotri_i32(var, var, shift); break;
482
        } else {
483
            TCGv tmp = load_cpu_field(CF);
484
            if (flags)
485
                shifter_out_im(var, 0);
486
            tcg_gen_shri_i32(var, var, 1);
487
            tcg_gen_shli_i32(tmp, tmp, 31);
488
            tcg_gen_or_i32(var, var, tmp);
489
            dead_tmp(tmp);
490
        }
491
    }
492
};
493

    
494
static inline void gen_arm_shift_reg(TCGv var, int shiftop,
495
                                     TCGv shift, int flags)
496
{
497
    if (flags) {
498
        switch (shiftop) {
499
        case 0: gen_helper_shl_cc(var, var, shift); break;
500
        case 1: gen_helper_shr_cc(var, var, shift); break;
501
        case 2: gen_helper_sar_cc(var, var, shift); break;
502
        case 3: gen_helper_ror_cc(var, var, shift); break;
503
        }
504
    } else {
505
        switch (shiftop) {
506
        case 0: gen_helper_shl(var, var, shift); break;
507
        case 1: gen_helper_shr(var, var, shift); break;
508
        case 2: gen_helper_sar(var, var, shift); break;
509
        case 3: tcg_gen_andi_i32(shift, shift, 0x1f);
510
                tcg_gen_rotr_i32(var, var, shift); break;
511
        }
512
    }
513
    dead_tmp(shift);
514
}
515

    
516
#define PAS_OP(pfx) \
517
    switch (op2) {  \
518
    case 0: gen_pas_helper(glue(pfx,add16)); break; \
519
    case 1: gen_pas_helper(glue(pfx,addsubx)); break; \
520
    case 2: gen_pas_helper(glue(pfx,subaddx)); break; \
521
    case 3: gen_pas_helper(glue(pfx,sub16)); break; \
522
    case 4: gen_pas_helper(glue(pfx,add8)); break; \
523
    case 7: gen_pas_helper(glue(pfx,sub8)); break; \
524
    }
525
static void gen_arm_parallel_addsub(int op1, int op2, TCGv a, TCGv b)
526
{
527
    TCGv_ptr tmp;
528

    
529
    switch (op1) {
530
#define gen_pas_helper(name) glue(gen_helper_,name)(a, a, b, tmp)
531
    case 1:
532
        tmp = tcg_temp_new_ptr();
533
        tcg_gen_addi_ptr(tmp, cpu_env, offsetof(CPUState, GE));
534
        PAS_OP(s)
535
        tcg_temp_free_ptr(tmp);
536
        break;
537
    case 5:
538
        tmp = tcg_temp_new_ptr();
539
        tcg_gen_addi_ptr(tmp, cpu_env, offsetof(CPUState, GE));
540
        PAS_OP(u)
541
        tcg_temp_free_ptr(tmp);
542
        break;
543
#undef gen_pas_helper
544
#define gen_pas_helper(name) glue(gen_helper_,name)(a, a, b)
545
    case 2:
546
        PAS_OP(q);
547
        break;
548
    case 3:
549
        PAS_OP(sh);
550
        break;
551
    case 6:
552
        PAS_OP(uq);
553
        break;
554
    case 7:
555
        PAS_OP(uh);
556
        break;
557
#undef gen_pas_helper
558
    }
559
}
560
#undef PAS_OP
561

    
562
/* For unknown reasons Arm and Thumb-2 use arbitrarily different encodings.  */
563
#define PAS_OP(pfx) \
564
    switch (op2) {  \
565
    case 0: gen_pas_helper(glue(pfx,add8)); break; \
566
    case 1: gen_pas_helper(glue(pfx,add16)); break; \
567
    case 2: gen_pas_helper(glue(pfx,addsubx)); break; \
568
    case 4: gen_pas_helper(glue(pfx,sub8)); break; \
569
    case 5: gen_pas_helper(glue(pfx,sub16)); break; \
570
    case 6: gen_pas_helper(glue(pfx,subaddx)); break; \
571
    }
572
static void gen_thumb2_parallel_addsub(int op1, int op2, TCGv a, TCGv b)
573
{
574
    TCGv_ptr tmp;
575

    
576
    switch (op1) {
577
#define gen_pas_helper(name) glue(gen_helper_,name)(a, a, b, tmp)
578
    case 0:
579
        tmp = tcg_temp_new_ptr();
580
        tcg_gen_addi_ptr(tmp, cpu_env, offsetof(CPUState, GE));
581
        PAS_OP(s)
582
        tcg_temp_free_ptr(tmp);
583
        break;
584
    case 4:
585
        tmp = tcg_temp_new_ptr();
586
        tcg_gen_addi_ptr(tmp, cpu_env, offsetof(CPUState, GE));
587
        PAS_OP(u)
588
        tcg_temp_free_ptr(tmp);
589
        break;
590
#undef gen_pas_helper
591
#define gen_pas_helper(name) glue(gen_helper_,name)(a, a, b)
592
    case 1:
593
        PAS_OP(q);
594
        break;
595
    case 2:
596
        PAS_OP(sh);
597
        break;
598
    case 5:
599
        PAS_OP(uq);
600
        break;
601
    case 6:
602
        PAS_OP(uh);
603
        break;
604
#undef gen_pas_helper
605
    }
606
}
607
#undef PAS_OP
608

    
609
static void gen_test_cc(int cc, int label)
610
{
611
    TCGv tmp;
612
    TCGv tmp2;
613
    int inv;
614

    
615
    switch (cc) {
616
    case 0: /* eq: Z */
617
        tmp = load_cpu_field(ZF);
618
        tcg_gen_brcondi_i32(TCG_COND_EQ, tmp, 0, label);
619
        break;
620
    case 1: /* ne: !Z */
621
        tmp = load_cpu_field(ZF);
622
        tcg_gen_brcondi_i32(TCG_COND_NE, tmp, 0, label);
623
        break;
624
    case 2: /* cs: C */
625
        tmp = load_cpu_field(CF);
626
        tcg_gen_brcondi_i32(TCG_COND_NE, tmp, 0, label);
627
        break;
628
    case 3: /* cc: !C */
629
        tmp = load_cpu_field(CF);
630
        tcg_gen_brcondi_i32(TCG_COND_EQ, tmp, 0, label);
631
        break;
632
    case 4: /* mi: N */
633
        tmp = load_cpu_field(NF);
634
        tcg_gen_brcondi_i32(TCG_COND_LT, tmp, 0, label);
635
        break;
636
    case 5: /* pl: !N */
637
        tmp = load_cpu_field(NF);
638
        tcg_gen_brcondi_i32(TCG_COND_GE, tmp, 0, label);
639
        break;
640
    case 6: /* vs: V */
641
        tmp = load_cpu_field(VF);
642
        tcg_gen_brcondi_i32(TCG_COND_LT, tmp, 0, label);
643
        break;
644
    case 7: /* vc: !V */
645
        tmp = load_cpu_field(VF);
646
        tcg_gen_brcondi_i32(TCG_COND_GE, tmp, 0, label);
647
        break;
648
    case 8: /* hi: C && !Z */
649
        inv = gen_new_label();
650
        tmp = load_cpu_field(CF);
651
        tcg_gen_brcondi_i32(TCG_COND_EQ, tmp, 0, inv);
652
        dead_tmp(tmp);
653
        tmp = load_cpu_field(ZF);
654
        tcg_gen_brcondi_i32(TCG_COND_NE, tmp, 0, label);
655
        gen_set_label(inv);
656
        break;
657
    case 9: /* ls: !C || Z */
658
        tmp = load_cpu_field(CF);
659
        tcg_gen_brcondi_i32(TCG_COND_EQ, tmp, 0, label);
660
        dead_tmp(tmp);
661
        tmp = load_cpu_field(ZF);
662
        tcg_gen_brcondi_i32(TCG_COND_EQ, tmp, 0, label);
663
        break;
664
    case 10: /* ge: N == V -> N ^ V == 0 */
665
        tmp = load_cpu_field(VF);
666
        tmp2 = load_cpu_field(NF);
667
        tcg_gen_xor_i32(tmp, tmp, tmp2);
668
        dead_tmp(tmp2);
669
        tcg_gen_brcondi_i32(TCG_COND_GE, tmp, 0, label);
670
        break;
671
    case 11: /* lt: N != V -> N ^ V != 0 */
672
        tmp = load_cpu_field(VF);
673
        tmp2 = load_cpu_field(NF);
674
        tcg_gen_xor_i32(tmp, tmp, tmp2);
675
        dead_tmp(tmp2);
676
        tcg_gen_brcondi_i32(TCG_COND_LT, tmp, 0, label);
677
        break;
678
    case 12: /* gt: !Z && N == V */
679
        inv = gen_new_label();
680
        tmp = load_cpu_field(ZF);
681
        tcg_gen_brcondi_i32(TCG_COND_EQ, tmp, 0, inv);
682
        dead_tmp(tmp);
683
        tmp = load_cpu_field(VF);
684
        tmp2 = load_cpu_field(NF);
685
        tcg_gen_xor_i32(tmp, tmp, tmp2);
686
        dead_tmp(tmp2);
687
        tcg_gen_brcondi_i32(TCG_COND_GE, tmp, 0, label);
688
        gen_set_label(inv);
689
        break;
690
    case 13: /* le: Z || N != V */
691
        tmp = load_cpu_field(ZF);
692
        tcg_gen_brcondi_i32(TCG_COND_EQ, tmp, 0, label);
693
        dead_tmp(tmp);
694
        tmp = load_cpu_field(VF);
695
        tmp2 = load_cpu_field(NF);
696
        tcg_gen_xor_i32(tmp, tmp, tmp2);
697
        dead_tmp(tmp2);
698
        tcg_gen_brcondi_i32(TCG_COND_LT, tmp, 0, label);
699
        break;
700
    default:
701
        fprintf(stderr, "Bad condition code 0x%x\n", cc);
702
        abort();
703
    }
704
    dead_tmp(tmp);
705
}
706

    
707
static const uint8_t table_logic_cc[16] = {
708
    1, /* and */
709
    1, /* xor */
710
    0, /* sub */
711
    0, /* rsb */
712
    0, /* add */
713
    0, /* adc */
714
    0, /* sbc */
715
    0, /* rsc */
716
    1, /* andl */
717
    1, /* xorl */
718
    0, /* cmp */
719
    0, /* cmn */
720
    1, /* orr */
721
    1, /* mov */
722
    1, /* bic */
723
    1, /* mvn */
724
};
725

    
726
/* Set PC and Thumb state from an immediate address.  */
727
static inline void gen_bx_im(DisasContext *s, uint32_t addr)
728
{
729
    TCGv tmp;
730

    
731
    s->is_jmp = DISAS_UPDATE;
732
    if (s->thumb != (addr & 1)) {
733
        tmp = new_tmp();
734
        tcg_gen_movi_i32(tmp, addr & 1);
735
        tcg_gen_st_i32(tmp, cpu_env, offsetof(CPUState, thumb));
736
        dead_tmp(tmp);
737
    }
738
    tcg_gen_movi_i32(cpu_R[15], addr & ~1);
739
}
740

    
741
/* Set PC and Thumb state from var.  var is marked as dead.  */
742
static inline void gen_bx(DisasContext *s, TCGv var)
743
{
744
    s->is_jmp = DISAS_UPDATE;
745
    tcg_gen_andi_i32(cpu_R[15], var, ~1);
746
    tcg_gen_andi_i32(var, var, 1);
747
    store_cpu_field(var, thumb);
748
}
749

    
750
/* Variant of store_reg which uses branch&exchange logic when storing
751
   to r15 in ARM architecture v7 and above. The source must be a temporary
752
   and will be marked as dead. */
753
static inline void store_reg_bx(CPUState *env, DisasContext *s,
754
                                int reg, TCGv var)
755
{
756
    if (reg == 15 && ENABLE_ARCH_7) {
757
        gen_bx(s, var);
758
    } else {
759
        store_reg(s, reg, var);
760
    }
761
}
762

    
763
static inline TCGv gen_ld8s(TCGv addr, int index)
764
{
765
    TCGv tmp = new_tmp();
766
    tcg_gen_qemu_ld8s(tmp, addr, index);
767
    return tmp;
768
}
769
static inline TCGv gen_ld8u(TCGv addr, int index)
770
{
771
    TCGv tmp = new_tmp();
772
    tcg_gen_qemu_ld8u(tmp, addr, index);
773
    return tmp;
774
}
775
static inline TCGv gen_ld16s(TCGv addr, int index)
776
{
777
    TCGv tmp = new_tmp();
778
    tcg_gen_qemu_ld16s(tmp, addr, index);
779
    return tmp;
780
}
781
static inline TCGv gen_ld16u(TCGv addr, int index)
782
{
783
    TCGv tmp = new_tmp();
784
    tcg_gen_qemu_ld16u(tmp, addr, index);
785
    return tmp;
786
}
787
static inline TCGv gen_ld32(TCGv addr, int index)
788
{
789
    TCGv tmp = new_tmp();
790
    tcg_gen_qemu_ld32u(tmp, addr, index);
791
    return tmp;
792
}
793
static inline TCGv_i64 gen_ld64(TCGv addr, int index)
794
{
795
    TCGv_i64 tmp = tcg_temp_new_i64();
796
    tcg_gen_qemu_ld64(tmp, addr, index);
797
    return tmp;
798
}
799
static inline void gen_st8(TCGv val, TCGv addr, int index)
800
{
801
    tcg_gen_qemu_st8(val, addr, index);
802
    dead_tmp(val);
803
}
804
static inline void gen_st16(TCGv val, TCGv addr, int index)
805
{
806
    tcg_gen_qemu_st16(val, addr, index);
807
    dead_tmp(val);
808
}
809
static inline void gen_st32(TCGv val, TCGv addr, int index)
810
{
811
    tcg_gen_qemu_st32(val, addr, index);
812
    dead_tmp(val);
813
}
814
static inline void gen_st64(TCGv_i64 val, TCGv addr, int index)
815
{
816
    tcg_gen_qemu_st64(val, addr, index);
817
    tcg_temp_free_i64(val);
818
}
819

    
820
static inline void gen_set_pc_im(uint32_t val)
821
{
822
    tcg_gen_movi_i32(cpu_R[15], val);
823
}
824

    
825
/* Force a TB lookup after an instruction that changes the CPU state.  */
826
static inline void gen_lookup_tb(DisasContext *s)
827
{
828
    tcg_gen_movi_i32(cpu_R[15], s->pc & ~1);
829
    s->is_jmp = DISAS_UPDATE;
830
}
831

    
832
static inline void gen_add_data_offset(DisasContext *s, unsigned int insn,
833
                                       TCGv var)
834
{
835
    int val, rm, shift, shiftop;
836
    TCGv offset;
837

    
838
    if (!(insn & (1 << 25))) {
839
        /* immediate */
840
        val = insn & 0xfff;
841
        if (!(insn & (1 << 23)))
842
            val = -val;
843
        if (val != 0)
844
            tcg_gen_addi_i32(var, var, val);
845
    } else {
846
        /* shift/register */
847
        rm = (insn) & 0xf;
848
        shift = (insn >> 7) & 0x1f;
849
        shiftop = (insn >> 5) & 3;
850
        offset = load_reg(s, rm);
851
        gen_arm_shift_im(offset, shiftop, shift, 0);
852
        if (!(insn & (1 << 23)))
853
            tcg_gen_sub_i32(var, var, offset);
854
        else
855
            tcg_gen_add_i32(var, var, offset);
856
        dead_tmp(offset);
857
    }
858
}
859

    
860
static inline void gen_add_datah_offset(DisasContext *s, unsigned int insn,
861
                                        int extra, TCGv var)
862
{
863
    int val, rm;
864
    TCGv offset;
865

    
866
    if (insn & (1 << 22)) {
867
        /* immediate */
868
        val = (insn & 0xf) | ((insn >> 4) & 0xf0);
869
        if (!(insn & (1 << 23)))
870
            val = -val;
871
        val += extra;
872
        if (val != 0)
873
            tcg_gen_addi_i32(var, var, val);
874
    } else {
875
        /* register */
876
        if (extra)
877
            tcg_gen_addi_i32(var, var, extra);
878
        rm = (insn) & 0xf;
879
        offset = load_reg(s, rm);
880
        if (!(insn & (1 << 23)))
881
            tcg_gen_sub_i32(var, var, offset);
882
        else
883
            tcg_gen_add_i32(var, var, offset);
884
        dead_tmp(offset);
885
    }
886
}
887

    
888
#define VFP_OP2(name)                                                 \
889
static inline void gen_vfp_##name(int dp)                             \
890
{                                                                     \
891
    if (dp)                                                           \
892
        gen_helper_vfp_##name##d(cpu_F0d, cpu_F0d, cpu_F1d, cpu_env); \
893
    else                                                              \
894
        gen_helper_vfp_##name##s(cpu_F0s, cpu_F0s, cpu_F1s, cpu_env); \
895
}
896

    
897
VFP_OP2(add)
898
VFP_OP2(sub)
899
VFP_OP2(mul)
900
VFP_OP2(div)
901

    
902
#undef VFP_OP2
903

    
904
static inline void gen_vfp_abs(int dp)
905
{
906
    if (dp)
907
        gen_helper_vfp_absd(cpu_F0d, cpu_F0d);
908
    else
909
        gen_helper_vfp_abss(cpu_F0s, cpu_F0s);
910
}
911

    
912
static inline void gen_vfp_neg(int dp)
913
{
914
    if (dp)
915
        gen_helper_vfp_negd(cpu_F0d, cpu_F0d);
916
    else
917
        gen_helper_vfp_negs(cpu_F0s, cpu_F0s);
918
}
919

    
920
static inline void gen_vfp_sqrt(int dp)
921
{
922
    if (dp)
923
        gen_helper_vfp_sqrtd(cpu_F0d, cpu_F0d, cpu_env);
924
    else
925
        gen_helper_vfp_sqrts(cpu_F0s, cpu_F0s, cpu_env);
926
}
927

    
928
static inline void gen_vfp_cmp(int dp)
929
{
930
    if (dp)
931
        gen_helper_vfp_cmpd(cpu_F0d, cpu_F1d, cpu_env);
932
    else
933
        gen_helper_vfp_cmps(cpu_F0s, cpu_F1s, cpu_env);
934
}
935

    
936
static inline void gen_vfp_cmpe(int dp)
937
{
938
    if (dp)
939
        gen_helper_vfp_cmped(cpu_F0d, cpu_F1d, cpu_env);
940
    else
941
        gen_helper_vfp_cmpes(cpu_F0s, cpu_F1s, cpu_env);
942
}
943

    
944
static inline void gen_vfp_F1_ld0(int dp)
945
{
946
    if (dp)
947
        tcg_gen_movi_i64(cpu_F1d, 0);
948
    else
949
        tcg_gen_movi_i32(cpu_F1s, 0);
950
}
951

    
952
static inline void gen_vfp_uito(int dp)
953
{
954
    if (dp)
955
        gen_helper_vfp_uitod(cpu_F0d, cpu_F0s, cpu_env);
956
    else
957
        gen_helper_vfp_uitos(cpu_F0s, cpu_F0s, cpu_env);
958
}
959

    
960
static inline void gen_vfp_sito(int dp)
961
{
962
    if (dp)
963
        gen_helper_vfp_sitod(cpu_F0d, cpu_F0s, cpu_env);
964
    else
965
        gen_helper_vfp_sitos(cpu_F0s, cpu_F0s, cpu_env);
966
}
967

    
968
static inline void gen_vfp_toui(int dp)
969
{
970
    if (dp)
971
        gen_helper_vfp_touid(cpu_F0s, cpu_F0d, cpu_env);
972
    else
973
        gen_helper_vfp_touis(cpu_F0s, cpu_F0s, cpu_env);
974
}
975

    
976
static inline void gen_vfp_touiz(int dp)
977
{
978
    if (dp)
979
        gen_helper_vfp_touizd(cpu_F0s, cpu_F0d, cpu_env);
980
    else
981
        gen_helper_vfp_touizs(cpu_F0s, cpu_F0s, cpu_env);
982
}
983

    
984
static inline void gen_vfp_tosi(int dp)
985
{
986
    if (dp)
987
        gen_helper_vfp_tosid(cpu_F0s, cpu_F0d, cpu_env);
988
    else
989
        gen_helper_vfp_tosis(cpu_F0s, cpu_F0s, cpu_env);
990
}
991

    
992
static inline void gen_vfp_tosiz(int dp)
993
{
994
    if (dp)
995
        gen_helper_vfp_tosizd(cpu_F0s, cpu_F0d, cpu_env);
996
    else
997
        gen_helper_vfp_tosizs(cpu_F0s, cpu_F0s, cpu_env);
998
}
999

    
1000
#define VFP_GEN_FIX(name) \
1001
static inline void gen_vfp_##name(int dp, int shift) \
1002
{ \
1003
    TCGv tmp_shift = tcg_const_i32(shift); \
1004
    if (dp) \
1005
        gen_helper_vfp_##name##d(cpu_F0d, cpu_F0d, tmp_shift, cpu_env);\
1006
    else \
1007
        gen_helper_vfp_##name##s(cpu_F0s, cpu_F0s, tmp_shift, cpu_env);\
1008
    tcg_temp_free_i32(tmp_shift); \
1009
}
1010
VFP_GEN_FIX(tosh)
1011
VFP_GEN_FIX(tosl)
1012
VFP_GEN_FIX(touh)
1013
VFP_GEN_FIX(toul)
1014
VFP_GEN_FIX(shto)
1015
VFP_GEN_FIX(slto)
1016
VFP_GEN_FIX(uhto)
1017
VFP_GEN_FIX(ulto)
1018
#undef VFP_GEN_FIX
1019

    
1020
static inline void gen_vfp_ld(DisasContext *s, int dp, TCGv addr)
1021
{
1022
    if (dp)
1023
        tcg_gen_qemu_ld64(cpu_F0d, addr, IS_USER(s));
1024
    else
1025
        tcg_gen_qemu_ld32u(cpu_F0s, addr, IS_USER(s));
1026
}
1027

    
1028
static inline void gen_vfp_st(DisasContext *s, int dp, TCGv addr)
1029
{
1030
    if (dp)
1031
        tcg_gen_qemu_st64(cpu_F0d, addr, IS_USER(s));
1032
    else
1033
        tcg_gen_qemu_st32(cpu_F0s, addr, IS_USER(s));
1034
}
1035

    
1036
static inline long
1037
vfp_reg_offset (int dp, int reg)
1038
{
1039
    if (dp)
1040
        return offsetof(CPUARMState, vfp.regs[reg]);
1041
    else if (reg & 1) {
1042
        return offsetof(CPUARMState, vfp.regs[reg >> 1])
1043
          + offsetof(CPU_DoubleU, l.upper);
1044
    } else {
1045
        return offsetof(CPUARMState, vfp.regs[reg >> 1])
1046
          + offsetof(CPU_DoubleU, l.lower);
1047
    }
1048
}
1049

    
1050
/* Return the offset of a 32-bit piece of a NEON register.
1051
   zero is the least significant end of the register.  */
1052
static inline long
1053
neon_reg_offset (int reg, int n)
1054
{
1055
    int sreg;
1056
    sreg = reg * 2 + n;
1057
    return vfp_reg_offset(0, sreg);
1058
}
1059

    
1060
static TCGv neon_load_reg(int reg, int pass)
1061
{
1062
    TCGv tmp = new_tmp();
1063
    tcg_gen_ld_i32(tmp, cpu_env, neon_reg_offset(reg, pass));
1064
    return tmp;
1065
}
1066

    
1067
static void neon_store_reg(int reg, int pass, TCGv var)
1068
{
1069
    tcg_gen_st_i32(var, cpu_env, neon_reg_offset(reg, pass));
1070
    dead_tmp(var);
1071
}
1072

    
1073
static inline void neon_load_reg64(TCGv_i64 var, int reg)
1074
{
1075
    tcg_gen_ld_i64(var, cpu_env, vfp_reg_offset(1, reg));
1076
}
1077

    
1078
static inline void neon_store_reg64(TCGv_i64 var, int reg)
1079
{
1080
    tcg_gen_st_i64(var, cpu_env, vfp_reg_offset(1, reg));
1081
}
1082

    
1083
#define tcg_gen_ld_f32 tcg_gen_ld_i32
1084
#define tcg_gen_ld_f64 tcg_gen_ld_i64
1085
#define tcg_gen_st_f32 tcg_gen_st_i32
1086
#define tcg_gen_st_f64 tcg_gen_st_i64
1087

    
1088
static inline void gen_mov_F0_vreg(int dp, int reg)
1089
{
1090
    if (dp)
1091
        tcg_gen_ld_f64(cpu_F0d, cpu_env, vfp_reg_offset(dp, reg));
1092
    else
1093
        tcg_gen_ld_f32(cpu_F0s, cpu_env, vfp_reg_offset(dp, reg));
1094
}
1095

    
1096
static inline void gen_mov_F1_vreg(int dp, int reg)
1097
{
1098
    if (dp)
1099
        tcg_gen_ld_f64(cpu_F1d, cpu_env, vfp_reg_offset(dp, reg));
1100
    else
1101
        tcg_gen_ld_f32(cpu_F1s, cpu_env, vfp_reg_offset(dp, reg));
1102
}
1103

    
1104
static inline void gen_mov_vreg_F0(int dp, int reg)
1105
{
1106
    if (dp)
1107
        tcg_gen_st_f64(cpu_F0d, cpu_env, vfp_reg_offset(dp, reg));
1108
    else
1109
        tcg_gen_st_f32(cpu_F0s, cpu_env, vfp_reg_offset(dp, reg));
1110
}
1111

    
1112
#define ARM_CP_RW_BIT        (1 << 20)
1113

    
1114
static inline void iwmmxt_load_reg(TCGv_i64 var, int reg)
1115
{
1116
    tcg_gen_ld_i64(var, cpu_env, offsetof(CPUState, iwmmxt.regs[reg]));
1117
}
1118

    
1119
static inline void iwmmxt_store_reg(TCGv_i64 var, int reg)
1120
{
1121
    tcg_gen_st_i64(var, cpu_env, offsetof(CPUState, iwmmxt.regs[reg]));
1122
}
1123

    
1124
static inline TCGv iwmmxt_load_creg(int reg)
1125
{
1126
    TCGv var = new_tmp();
1127
    tcg_gen_ld_i32(var, cpu_env, offsetof(CPUState, iwmmxt.cregs[reg]));
1128
    return var;
1129
}
1130

    
1131
static inline void iwmmxt_store_creg(int reg, TCGv var)
1132
{
1133
    tcg_gen_st_i32(var, cpu_env, offsetof(CPUState, iwmmxt.cregs[reg]));
1134
}
1135

    
1136
static inline void gen_op_iwmmxt_movq_wRn_M0(int rn)
1137
{
1138
    iwmmxt_store_reg(cpu_M0, rn);
1139
}
1140

    
1141
static inline void gen_op_iwmmxt_movq_M0_wRn(int rn)
1142
{
1143
    iwmmxt_load_reg(cpu_M0, rn);
1144
}
1145

    
1146
static inline void gen_op_iwmmxt_orq_M0_wRn(int rn)
1147
{
1148
    iwmmxt_load_reg(cpu_V1, rn);
1149
    tcg_gen_or_i64(cpu_M0, cpu_M0, cpu_V1);
1150
}
1151

    
1152
static inline void gen_op_iwmmxt_andq_M0_wRn(int rn)
1153
{
1154
    iwmmxt_load_reg(cpu_V1, rn);
1155
    tcg_gen_and_i64(cpu_M0, cpu_M0, cpu_V1);
1156
}
1157

    
1158
static inline void gen_op_iwmmxt_xorq_M0_wRn(int rn)
1159
{
1160
    iwmmxt_load_reg(cpu_V1, rn);
1161
    tcg_gen_xor_i64(cpu_M0, cpu_M0, cpu_V1);
1162
}
1163

    
1164
#define IWMMXT_OP(name) \
1165
static inline void gen_op_iwmmxt_##name##_M0_wRn(int rn) \
1166
{ \
1167
    iwmmxt_load_reg(cpu_V1, rn); \
1168
    gen_helper_iwmmxt_##name(cpu_M0, cpu_M0, cpu_V1); \
1169
}
1170

    
1171
#define IWMMXT_OP_ENV(name) \
1172
static inline void gen_op_iwmmxt_##name##_M0_wRn(int rn) \
1173
{ \
1174
    iwmmxt_load_reg(cpu_V1, rn); \
1175
    gen_helper_iwmmxt_##name(cpu_M0, cpu_env, cpu_M0, cpu_V1); \
1176
}
1177

    
1178
#define IWMMXT_OP_ENV_SIZE(name) \
1179
IWMMXT_OP_ENV(name##b) \
1180
IWMMXT_OP_ENV(name##w) \
1181
IWMMXT_OP_ENV(name##l)
1182

    
1183
#define IWMMXT_OP_ENV1(name) \
1184
static inline void gen_op_iwmmxt_##name##_M0(void) \
1185
{ \
1186
    gen_helper_iwmmxt_##name(cpu_M0, cpu_env, cpu_M0); \
1187
}
1188

    
1189
IWMMXT_OP(maddsq)
1190
IWMMXT_OP(madduq)
1191
IWMMXT_OP(sadb)
1192
IWMMXT_OP(sadw)
1193
IWMMXT_OP(mulslw)
1194
IWMMXT_OP(mulshw)
1195
IWMMXT_OP(mululw)
1196
IWMMXT_OP(muluhw)
1197
IWMMXT_OP(macsw)
1198
IWMMXT_OP(macuw)
1199

    
1200
IWMMXT_OP_ENV_SIZE(unpackl)
1201
IWMMXT_OP_ENV_SIZE(unpackh)
1202

    
1203
IWMMXT_OP_ENV1(unpacklub)
1204
IWMMXT_OP_ENV1(unpackluw)
1205
IWMMXT_OP_ENV1(unpacklul)
1206
IWMMXT_OP_ENV1(unpackhub)
1207
IWMMXT_OP_ENV1(unpackhuw)
1208
IWMMXT_OP_ENV1(unpackhul)
1209
IWMMXT_OP_ENV1(unpacklsb)
1210
IWMMXT_OP_ENV1(unpacklsw)
1211
IWMMXT_OP_ENV1(unpacklsl)
1212
IWMMXT_OP_ENV1(unpackhsb)
1213
IWMMXT_OP_ENV1(unpackhsw)
1214
IWMMXT_OP_ENV1(unpackhsl)
1215

    
1216
IWMMXT_OP_ENV_SIZE(cmpeq)
1217
IWMMXT_OP_ENV_SIZE(cmpgtu)
1218
IWMMXT_OP_ENV_SIZE(cmpgts)
1219

    
1220
IWMMXT_OP_ENV_SIZE(mins)
1221
IWMMXT_OP_ENV_SIZE(minu)
1222
IWMMXT_OP_ENV_SIZE(maxs)
1223
IWMMXT_OP_ENV_SIZE(maxu)
1224

    
1225
IWMMXT_OP_ENV_SIZE(subn)
1226
IWMMXT_OP_ENV_SIZE(addn)
1227
IWMMXT_OP_ENV_SIZE(subu)
1228
IWMMXT_OP_ENV_SIZE(addu)
1229
IWMMXT_OP_ENV_SIZE(subs)
1230
IWMMXT_OP_ENV_SIZE(adds)
1231

    
1232
IWMMXT_OP_ENV(avgb0)
1233
IWMMXT_OP_ENV(avgb1)
1234
IWMMXT_OP_ENV(avgw0)
1235
IWMMXT_OP_ENV(avgw1)
1236

    
1237
IWMMXT_OP(msadb)
1238

    
1239
IWMMXT_OP_ENV(packuw)
1240
IWMMXT_OP_ENV(packul)
1241
IWMMXT_OP_ENV(packuq)
1242
IWMMXT_OP_ENV(packsw)
1243
IWMMXT_OP_ENV(packsl)
1244
IWMMXT_OP_ENV(packsq)
1245

    
1246
static void gen_op_iwmmxt_set_mup(void)
1247
{
1248
    TCGv tmp;
1249
    tmp = load_cpu_field(iwmmxt.cregs[ARM_IWMMXT_wCon]);
1250
    tcg_gen_ori_i32(tmp, tmp, 2);
1251
    store_cpu_field(tmp, iwmmxt.cregs[ARM_IWMMXT_wCon]);
1252
}
1253

    
1254
static void gen_op_iwmmxt_set_cup(void)
1255
{
1256
    TCGv tmp;
1257
    tmp = load_cpu_field(iwmmxt.cregs[ARM_IWMMXT_wCon]);
1258
    tcg_gen_ori_i32(tmp, tmp, 1);
1259
    store_cpu_field(tmp, iwmmxt.cregs[ARM_IWMMXT_wCon]);
1260
}
1261

    
1262
static void gen_op_iwmmxt_setpsr_nz(void)
1263
{
1264
    TCGv tmp = new_tmp();
1265
    gen_helper_iwmmxt_setpsr_nz(tmp, cpu_M0);
1266
    store_cpu_field(tmp, iwmmxt.cregs[ARM_IWMMXT_wCASF]);
1267
}
1268

    
1269
static inline void gen_op_iwmmxt_addl_M0_wRn(int rn)
1270
{
1271
    iwmmxt_load_reg(cpu_V1, rn);
1272
    tcg_gen_ext32u_i64(cpu_V1, cpu_V1);
1273
    tcg_gen_add_i64(cpu_M0, cpu_M0, cpu_V1);
1274
}
1275

    
1276
static inline int gen_iwmmxt_address(DisasContext *s, uint32_t insn, TCGv dest)
1277
{
1278
    int rd;
1279
    uint32_t offset;
1280
    TCGv tmp;
1281

    
1282
    rd = (insn >> 16) & 0xf;
1283
    tmp = load_reg(s, rd);
1284

    
1285
    offset = (insn & 0xff) << ((insn >> 7) & 2);
1286
    if (insn & (1 << 24)) {
1287
        /* Pre indexed */
1288
        if (insn & (1 << 23))
1289
            tcg_gen_addi_i32(tmp, tmp, offset);
1290
        else
1291
            tcg_gen_addi_i32(tmp, tmp, -offset);
1292
        tcg_gen_mov_i32(dest, tmp);
1293
        if (insn & (1 << 21))
1294
            store_reg(s, rd, tmp);
1295
        else
1296
            dead_tmp(tmp);
1297
    } else if (insn & (1 << 21)) {
1298
        /* Post indexed */
1299
        tcg_gen_mov_i32(dest, tmp);
1300
        if (insn & (1 << 23))
1301
            tcg_gen_addi_i32(tmp, tmp, offset);
1302
        else
1303
            tcg_gen_addi_i32(tmp, tmp, -offset);
1304
        store_reg(s, rd, tmp);
1305
    } else if (!(insn & (1 << 23)))
1306
        return 1;
1307
    return 0;
1308
}
1309

    
1310
static inline int gen_iwmmxt_shift(uint32_t insn, uint32_t mask, TCGv dest)
1311
{
1312
    int rd = (insn >> 0) & 0xf;
1313
    TCGv tmp;
1314

    
1315
    if (insn & (1 << 8)) {
1316
        if (rd < ARM_IWMMXT_wCGR0 || rd > ARM_IWMMXT_wCGR3) {
1317
            return 1;
1318
        } else {
1319
            tmp = iwmmxt_load_creg(rd);
1320
        }
1321
    } else {
1322
        tmp = new_tmp();
1323
        iwmmxt_load_reg(cpu_V0, rd);
1324
        tcg_gen_trunc_i64_i32(tmp, cpu_V0);
1325
    }
1326
    tcg_gen_andi_i32(tmp, tmp, mask);
1327
    tcg_gen_mov_i32(dest, tmp);
1328
    dead_tmp(tmp);
1329
    return 0;
1330
}
1331

    
1332
/* Disassemble an iwMMXt instruction.  Returns nonzero if an error occured
1333
   (ie. an undefined instruction).  */
1334
static int disas_iwmmxt_insn(CPUState *env, DisasContext *s, uint32_t insn)
1335
{
1336
    int rd, wrd;
1337
    int rdhi, rdlo, rd0, rd1, i;
1338
    TCGv addr;
1339
    TCGv tmp, tmp2, tmp3;
1340

    
1341
    if ((insn & 0x0e000e00) == 0x0c000000) {
1342
        if ((insn & 0x0fe00ff0) == 0x0c400000) {
1343
            wrd = insn & 0xf;
1344
            rdlo = (insn >> 12) & 0xf;
1345
            rdhi = (insn >> 16) & 0xf;
1346
            if (insn & ARM_CP_RW_BIT) {                        /* TMRRC */
1347
                iwmmxt_load_reg(cpu_V0, wrd);
1348
                tcg_gen_trunc_i64_i32(cpu_R[rdlo], cpu_V0);
1349
                tcg_gen_shri_i64(cpu_V0, cpu_V0, 32);
1350
                tcg_gen_trunc_i64_i32(cpu_R[rdhi], cpu_V0);
1351
            } else {                                        /* TMCRR */
1352
                tcg_gen_concat_i32_i64(cpu_V0, cpu_R[rdlo], cpu_R[rdhi]);
1353
                iwmmxt_store_reg(cpu_V0, wrd);
1354
                gen_op_iwmmxt_set_mup();
1355
            }
1356
            return 0;
1357
        }
1358

    
1359
        wrd = (insn >> 12) & 0xf;
1360
        addr = new_tmp();
1361
        if (gen_iwmmxt_address(s, insn, addr)) {
1362
            dead_tmp(addr);
1363
            return 1;
1364
        }
1365
        if (insn & ARM_CP_RW_BIT) {
1366
            if ((insn >> 28) == 0xf) {                        /* WLDRW wCx */
1367
                tmp = new_tmp();
1368
                tcg_gen_qemu_ld32u(tmp, addr, IS_USER(s));
1369
                iwmmxt_store_creg(wrd, tmp);
1370
            } else {
1371
                i = 1;
1372
                if (insn & (1 << 8)) {
1373
                    if (insn & (1 << 22)) {                /* WLDRD */
1374
                        tcg_gen_qemu_ld64(cpu_M0, addr, IS_USER(s));
1375
                        i = 0;
1376
                    } else {                                /* WLDRW wRd */
1377
                        tmp = gen_ld32(addr, IS_USER(s));
1378
                    }
1379
                } else {
1380
                    if (insn & (1 << 22)) {                /* WLDRH */
1381
                        tmp = gen_ld16u(addr, IS_USER(s));
1382
                    } else {                                /* WLDRB */
1383
                        tmp = gen_ld8u(addr, IS_USER(s));
1384
                    }
1385
                }
1386
                if (i) {
1387
                    tcg_gen_extu_i32_i64(cpu_M0, tmp);
1388
                    dead_tmp(tmp);
1389
                }
1390
                gen_op_iwmmxt_movq_wRn_M0(wrd);
1391
            }
1392
        } else {
1393
            if ((insn >> 28) == 0xf) {                        /* WSTRW wCx */
1394
                tmp = iwmmxt_load_creg(wrd);
1395
                gen_st32(tmp, addr, IS_USER(s));
1396
            } else {
1397
                gen_op_iwmmxt_movq_M0_wRn(wrd);
1398
                tmp = new_tmp();
1399
                if (insn & (1 << 8)) {
1400
                    if (insn & (1 << 22)) {                /* WSTRD */
1401
                        dead_tmp(tmp);
1402
                        tcg_gen_qemu_st64(cpu_M0, addr, IS_USER(s));
1403
                    } else {                                /* WSTRW wRd */
1404
                        tcg_gen_trunc_i64_i32(tmp, cpu_M0);
1405
                        gen_st32(tmp, addr, IS_USER(s));
1406
                    }
1407
                } else {
1408
                    if (insn & (1 << 22)) {                /* WSTRH */
1409
                        tcg_gen_trunc_i64_i32(tmp, cpu_M0);
1410
                        gen_st16(tmp, addr, IS_USER(s));
1411
                    } else {                                /* WSTRB */
1412
                        tcg_gen_trunc_i64_i32(tmp, cpu_M0);
1413
                        gen_st8(tmp, addr, IS_USER(s));
1414
                    }
1415
                }
1416
            }
1417
        }
1418
        return 0;
1419
    }
1420

    
1421
    if ((insn & 0x0f000000) != 0x0e000000)
1422
        return 1;
1423

    
1424
    switch (((insn >> 12) & 0xf00) | ((insn >> 4) & 0xff)) {
1425
    case 0x000:                                                /* WOR */
1426
        wrd = (insn >> 12) & 0xf;
1427
        rd0 = (insn >> 0) & 0xf;
1428
        rd1 = (insn >> 16) & 0xf;
1429
        gen_op_iwmmxt_movq_M0_wRn(rd0);
1430
        gen_op_iwmmxt_orq_M0_wRn(rd1);
1431
        gen_op_iwmmxt_setpsr_nz();
1432
        gen_op_iwmmxt_movq_wRn_M0(wrd);
1433
        gen_op_iwmmxt_set_mup();
1434
        gen_op_iwmmxt_set_cup();
1435
        break;
1436
    case 0x011:                                                /* TMCR */
1437
        if (insn & 0xf)
1438
            return 1;
1439
        rd = (insn >> 12) & 0xf;
1440
        wrd = (insn >> 16) & 0xf;
1441
        switch (wrd) {
1442
        case ARM_IWMMXT_wCID:
1443
        case ARM_IWMMXT_wCASF:
1444
            break;
1445
        case ARM_IWMMXT_wCon:
1446
            gen_op_iwmmxt_set_cup();
1447
            /* Fall through.  */
1448
        case ARM_IWMMXT_wCSSF:
1449
            tmp = iwmmxt_load_creg(wrd);
1450
            tmp2 = load_reg(s, rd);
1451
            tcg_gen_andc_i32(tmp, tmp, tmp2);
1452
            dead_tmp(tmp2);
1453
            iwmmxt_store_creg(wrd, tmp);
1454
            break;
1455
        case ARM_IWMMXT_wCGR0:
1456
        case ARM_IWMMXT_wCGR1:
1457
        case ARM_IWMMXT_wCGR2:
1458
        case ARM_IWMMXT_wCGR3:
1459
            gen_op_iwmmxt_set_cup();
1460
            tmp = load_reg(s, rd);
1461
            iwmmxt_store_creg(wrd, tmp);
1462
            break;
1463
        default:
1464
            return 1;
1465
        }
1466
        break;
1467
    case 0x100:                                                /* WXOR */
1468
        wrd = (insn >> 12) & 0xf;
1469
        rd0 = (insn >> 0) & 0xf;
1470
        rd1 = (insn >> 16) & 0xf;
1471
        gen_op_iwmmxt_movq_M0_wRn(rd0);
1472
        gen_op_iwmmxt_xorq_M0_wRn(rd1);
1473
        gen_op_iwmmxt_setpsr_nz();
1474
        gen_op_iwmmxt_movq_wRn_M0(wrd);
1475
        gen_op_iwmmxt_set_mup();
1476
        gen_op_iwmmxt_set_cup();
1477
        break;
1478
    case 0x111:                                                /* TMRC */
1479
        if (insn & 0xf)
1480
            return 1;
1481
        rd = (insn >> 12) & 0xf;
1482
        wrd = (insn >> 16) & 0xf;
1483
        tmp = iwmmxt_load_creg(wrd);
1484
        store_reg(s, rd, tmp);
1485
        break;
1486
    case 0x300:                                                /* WANDN */
1487
        wrd = (insn >> 12) & 0xf;
1488
        rd0 = (insn >> 0) & 0xf;
1489
        rd1 = (insn >> 16) & 0xf;
1490
        gen_op_iwmmxt_movq_M0_wRn(rd0);
1491
        tcg_gen_neg_i64(cpu_M0, cpu_M0);
1492
        gen_op_iwmmxt_andq_M0_wRn(rd1);
1493
        gen_op_iwmmxt_setpsr_nz();
1494
        gen_op_iwmmxt_movq_wRn_M0(wrd);
1495
        gen_op_iwmmxt_set_mup();
1496
        gen_op_iwmmxt_set_cup();
1497
        break;
1498
    case 0x200:                                                /* WAND */
1499
        wrd = (insn >> 12) & 0xf;
1500
        rd0 = (insn >> 0) & 0xf;
1501
        rd1 = (insn >> 16) & 0xf;
1502
        gen_op_iwmmxt_movq_M0_wRn(rd0);
1503
        gen_op_iwmmxt_andq_M0_wRn(rd1);
1504
        gen_op_iwmmxt_setpsr_nz();
1505
        gen_op_iwmmxt_movq_wRn_M0(wrd);
1506
        gen_op_iwmmxt_set_mup();
1507
        gen_op_iwmmxt_set_cup();
1508
        break;
1509
    case 0x810: case 0xa10:                                /* WMADD */
1510
        wrd = (insn >> 12) & 0xf;
1511
        rd0 = (insn >> 0) & 0xf;
1512
        rd1 = (insn >> 16) & 0xf;
1513
        gen_op_iwmmxt_movq_M0_wRn(rd0);
1514
        if (insn & (1 << 21))
1515
            gen_op_iwmmxt_maddsq_M0_wRn(rd1);
1516
        else
1517
            gen_op_iwmmxt_madduq_M0_wRn(rd1);
1518
        gen_op_iwmmxt_movq_wRn_M0(wrd);
1519
        gen_op_iwmmxt_set_mup();
1520
        break;
1521
    case 0x10e: case 0x50e: case 0x90e: case 0xd0e:        /* WUNPCKIL */
1522
        wrd = (insn >> 12) & 0xf;
1523
        rd0 = (insn >> 16) & 0xf;
1524
        rd1 = (insn >> 0) & 0xf;
1525
        gen_op_iwmmxt_movq_M0_wRn(rd0);
1526
        switch ((insn >> 22) & 3) {
1527
        case 0:
1528
            gen_op_iwmmxt_unpacklb_M0_wRn(rd1);
1529
            break;
1530
        case 1:
1531
            gen_op_iwmmxt_unpacklw_M0_wRn(rd1);
1532
            break;
1533
        case 2:
1534
            gen_op_iwmmxt_unpackll_M0_wRn(rd1);
1535
            break;
1536
        case 3:
1537
            return 1;
1538
        }
1539
        gen_op_iwmmxt_movq_wRn_M0(wrd);
1540
        gen_op_iwmmxt_set_mup();
1541
        gen_op_iwmmxt_set_cup();
1542
        break;
1543
    case 0x10c: case 0x50c: case 0x90c: case 0xd0c:        /* WUNPCKIH */
1544
        wrd = (insn >> 12) & 0xf;
1545
        rd0 = (insn >> 16) & 0xf;
1546
        rd1 = (insn >> 0) & 0xf;
1547
        gen_op_iwmmxt_movq_M0_wRn(rd0);
1548
        switch ((insn >> 22) & 3) {
1549
        case 0:
1550
            gen_op_iwmmxt_unpackhb_M0_wRn(rd1);
1551
            break;
1552
        case 1:
1553
            gen_op_iwmmxt_unpackhw_M0_wRn(rd1);
1554
            break;
1555
        case 2:
1556
            gen_op_iwmmxt_unpackhl_M0_wRn(rd1);
1557
            break;
1558
        case 3:
1559
            return 1;
1560
        }
1561
        gen_op_iwmmxt_movq_wRn_M0(wrd);
1562
        gen_op_iwmmxt_set_mup();
1563
        gen_op_iwmmxt_set_cup();
1564
        break;
1565
    case 0x012: case 0x112: case 0x412: case 0x512:        /* WSAD */
1566
        wrd = (insn >> 12) & 0xf;
1567
        rd0 = (insn >> 16) & 0xf;
1568
        rd1 = (insn >> 0) & 0xf;
1569
        gen_op_iwmmxt_movq_M0_wRn(rd0);
1570
        if (insn & (1 << 22))
1571
            gen_op_iwmmxt_sadw_M0_wRn(rd1);
1572
        else
1573
            gen_op_iwmmxt_sadb_M0_wRn(rd1);
1574
        if (!(insn & (1 << 20)))
1575
            gen_op_iwmmxt_addl_M0_wRn(wrd);
1576
        gen_op_iwmmxt_movq_wRn_M0(wrd);
1577
        gen_op_iwmmxt_set_mup();
1578
        break;
1579
    case 0x010: case 0x110: case 0x210: case 0x310:        /* WMUL */
1580
        wrd = (insn >> 12) & 0xf;
1581
        rd0 = (insn >> 16) & 0xf;
1582
        rd1 = (insn >> 0) & 0xf;
1583
        gen_op_iwmmxt_movq_M0_wRn(rd0);
1584
        if (insn & (1 << 21)) {
1585
            if (insn & (1 << 20))
1586
                gen_op_iwmmxt_mulshw_M0_wRn(rd1);
1587
            else
1588
                gen_op_iwmmxt_mulslw_M0_wRn(rd1);
1589
        } else {
1590
            if (insn & (1 << 20))
1591
                gen_op_iwmmxt_muluhw_M0_wRn(rd1);
1592
            else
1593
                gen_op_iwmmxt_mululw_M0_wRn(rd1);
1594
        }
1595
        gen_op_iwmmxt_movq_wRn_M0(wrd);
1596
        gen_op_iwmmxt_set_mup();
1597
        break;
1598
    case 0x410: case 0x510: case 0x610: case 0x710:        /* WMAC */
1599
        wrd = (insn >> 12) & 0xf;
1600
        rd0 = (insn >> 16) & 0xf;
1601
        rd1 = (insn >> 0) & 0xf;
1602
        gen_op_iwmmxt_movq_M0_wRn(rd0);
1603
        if (insn & (1 << 21))
1604
            gen_op_iwmmxt_macsw_M0_wRn(rd1);
1605
        else
1606
            gen_op_iwmmxt_macuw_M0_wRn(rd1);
1607
        if (!(insn & (1 << 20))) {
1608
            iwmmxt_load_reg(cpu_V1, wrd);
1609
            tcg_gen_add_i64(cpu_M0, cpu_M0, cpu_V1);
1610
        }
1611
        gen_op_iwmmxt_movq_wRn_M0(wrd);
1612
        gen_op_iwmmxt_set_mup();
1613
        break;
1614
    case 0x006: case 0x406: case 0x806: case 0xc06:        /* WCMPEQ */
1615
        wrd = (insn >> 12) & 0xf;
1616
        rd0 = (insn >> 16) & 0xf;
1617
        rd1 = (insn >> 0) & 0xf;
1618
        gen_op_iwmmxt_movq_M0_wRn(rd0);
1619
        switch ((insn >> 22) & 3) {
1620
        case 0:
1621
            gen_op_iwmmxt_cmpeqb_M0_wRn(rd1);
1622
            break;
1623
        case 1:
1624
            gen_op_iwmmxt_cmpeqw_M0_wRn(rd1);
1625
            break;
1626
        case 2:
1627
            gen_op_iwmmxt_cmpeql_M0_wRn(rd1);
1628
            break;
1629
        case 3:
1630
            return 1;
1631
        }
1632
        gen_op_iwmmxt_movq_wRn_M0(wrd);
1633
        gen_op_iwmmxt_set_mup();
1634
        gen_op_iwmmxt_set_cup();
1635
        break;
1636
    case 0x800: case 0x900: case 0xc00: case 0xd00:        /* WAVG2 */
1637
        wrd = (insn >> 12) & 0xf;
1638
        rd0 = (insn >> 16) & 0xf;
1639
        rd1 = (insn >> 0) & 0xf;
1640
        gen_op_iwmmxt_movq_M0_wRn(rd0);
1641
        if (insn & (1 << 22)) {
1642
            if (insn & (1 << 20))
1643
                gen_op_iwmmxt_avgw1_M0_wRn(rd1);
1644
            else
1645
                gen_op_iwmmxt_avgw0_M0_wRn(rd1);
1646
        } else {
1647
            if (insn & (1 << 20))
1648
                gen_op_iwmmxt_avgb1_M0_wRn(rd1);
1649
            else
1650
                gen_op_iwmmxt_avgb0_M0_wRn(rd1);
1651
        }
1652
        gen_op_iwmmxt_movq_wRn_M0(wrd);
1653
        gen_op_iwmmxt_set_mup();
1654
        gen_op_iwmmxt_set_cup();
1655
        break;
1656
    case 0x802: case 0x902: case 0xa02: case 0xb02:        /* WALIGNR */
1657
        wrd = (insn >> 12) & 0xf;
1658
        rd0 = (insn >> 16) & 0xf;
1659
        rd1 = (insn >> 0) & 0xf;
1660
        gen_op_iwmmxt_movq_M0_wRn(rd0);
1661
        tmp = iwmmxt_load_creg(ARM_IWMMXT_wCGR0 + ((insn >> 20) & 3));
1662
        tcg_gen_andi_i32(tmp, tmp, 7);
1663
        iwmmxt_load_reg(cpu_V1, rd1);
1664
        gen_helper_iwmmxt_align(cpu_M0, cpu_M0, cpu_V1, tmp);
1665
        dead_tmp(tmp);
1666
        gen_op_iwmmxt_movq_wRn_M0(wrd);
1667
        gen_op_iwmmxt_set_mup();
1668
        break;
1669
    case 0x601: case 0x605: case 0x609: case 0x60d:        /* TINSR */
1670
        if (((insn >> 6) & 3) == 3)
1671
            return 1;
1672
        rd = (insn >> 12) & 0xf;
1673
        wrd = (insn >> 16) & 0xf;
1674
        tmp = load_reg(s, rd);
1675
        gen_op_iwmmxt_movq_M0_wRn(wrd);
1676
        switch ((insn >> 6) & 3) {
1677
        case 0:
1678
            tmp2 = tcg_const_i32(0xff);
1679
            tmp3 = tcg_const_i32((insn & 7) << 3);
1680
            break;
1681
        case 1:
1682
            tmp2 = tcg_const_i32(0xffff);
1683
            tmp3 = tcg_const_i32((insn & 3) << 4);
1684
            break;
1685
        case 2:
1686
            tmp2 = tcg_const_i32(0xffffffff);
1687
            tmp3 = tcg_const_i32((insn & 1) << 5);
1688
            break;
1689
        default:
1690
            TCGV_UNUSED(tmp2);
1691
            TCGV_UNUSED(tmp3);
1692
        }
1693
        gen_helper_iwmmxt_insr(cpu_M0, cpu_M0, tmp, tmp2, tmp3);
1694
        tcg_temp_free(tmp3);
1695
        tcg_temp_free(tmp2);
1696
        dead_tmp(tmp);
1697
        gen_op_iwmmxt_movq_wRn_M0(wrd);
1698
        gen_op_iwmmxt_set_mup();
1699
        break;
1700
    case 0x107: case 0x507: case 0x907: case 0xd07:        /* TEXTRM */
1701
        rd = (insn >> 12) & 0xf;
1702
        wrd = (insn >> 16) & 0xf;
1703
        if (rd == 15 || ((insn >> 22) & 3) == 3)
1704
            return 1;
1705
        gen_op_iwmmxt_movq_M0_wRn(wrd);
1706
        tmp = new_tmp();
1707
        switch ((insn >> 22) & 3) {
1708
        case 0:
1709
            tcg_gen_shri_i64(cpu_M0, cpu_M0, (insn & 7) << 3);
1710
            tcg_gen_trunc_i64_i32(tmp, cpu_M0);
1711
            if (insn & 8) {
1712
                tcg_gen_ext8s_i32(tmp, tmp);
1713
            } else {
1714
                tcg_gen_andi_i32(tmp, tmp, 0xff);
1715
            }
1716
            break;
1717
        case 1:
1718
            tcg_gen_shri_i64(cpu_M0, cpu_M0, (insn & 3) << 4);
1719
            tcg_gen_trunc_i64_i32(tmp, cpu_M0);
1720
            if (insn & 8) {
1721
                tcg_gen_ext16s_i32(tmp, tmp);
1722
            } else {
1723
                tcg_gen_andi_i32(tmp, tmp, 0xffff);
1724
            }
1725
            break;
1726
        case 2:
1727
            tcg_gen_shri_i64(cpu_M0, cpu_M0, (insn & 1) << 5);
1728
            tcg_gen_trunc_i64_i32(tmp, cpu_M0);
1729
            break;
1730
        }
1731
        store_reg(s, rd, tmp);
1732
        break;
1733
    case 0x117: case 0x517: case 0x917: case 0xd17:        /* TEXTRC */
1734
        if ((insn & 0x000ff008) != 0x0003f000 || ((insn >> 22) & 3) == 3)
1735
            return 1;
1736
        tmp = iwmmxt_load_creg(ARM_IWMMXT_wCASF);
1737
        switch ((insn >> 22) & 3) {
1738
        case 0:
1739
            tcg_gen_shri_i32(tmp, tmp, ((insn & 7) << 2) + 0);
1740
            break;
1741
        case 1:
1742
            tcg_gen_shri_i32(tmp, tmp, ((insn & 3) << 3) + 4);
1743
            break;
1744
        case 2:
1745
            tcg_gen_shri_i32(tmp, tmp, ((insn & 1) << 4) + 12);
1746
            break;
1747
        }
1748
        tcg_gen_shli_i32(tmp, tmp, 28);
1749
        gen_set_nzcv(tmp);
1750
        dead_tmp(tmp);
1751
        break;
1752
    case 0x401: case 0x405: case 0x409: case 0x40d:        /* TBCST */
1753
        if (((insn >> 6) & 3) == 3)
1754
            return 1;
1755
        rd = (insn >> 12) & 0xf;
1756
        wrd = (insn >> 16) & 0xf;
1757
        tmp = load_reg(s, rd);
1758
        switch ((insn >> 6) & 3) {
1759
        case 0:
1760
            gen_helper_iwmmxt_bcstb(cpu_M0, tmp);
1761
            break;
1762
        case 1:
1763
            gen_helper_iwmmxt_bcstw(cpu_M0, tmp);
1764
            break;
1765
        case 2:
1766
            gen_helper_iwmmxt_bcstl(cpu_M0, tmp);
1767
            break;
1768
        }
1769
        dead_tmp(tmp);
1770
        gen_op_iwmmxt_movq_wRn_M0(wrd);
1771
        gen_op_iwmmxt_set_mup();
1772
        break;
1773
    case 0x113: case 0x513: case 0x913: case 0xd13:        /* TANDC */
1774
        if ((insn & 0x000ff00f) != 0x0003f000 || ((insn >> 22) & 3) == 3)
1775
            return 1;
1776
        tmp = iwmmxt_load_creg(ARM_IWMMXT_wCASF);
1777
        tmp2 = new_tmp();
1778
        tcg_gen_mov_i32(tmp2, tmp);
1779
        switch ((insn >> 22) & 3) {
1780
        case 0:
1781
            for (i = 0; i < 7; i ++) {
1782
                tcg_gen_shli_i32(tmp2, tmp2, 4);
1783
                tcg_gen_and_i32(tmp, tmp, tmp2);
1784
            }
1785
            break;
1786
        case 1:
1787
            for (i = 0; i < 3; i ++) {
1788
                tcg_gen_shli_i32(tmp2, tmp2, 8);
1789
                tcg_gen_and_i32(tmp, tmp, tmp2);
1790
            }
1791
            break;
1792
        case 2:
1793
            tcg_gen_shli_i32(tmp2, tmp2, 16);
1794
            tcg_gen_and_i32(tmp, tmp, tmp2);
1795
            break;
1796
        }
1797
        gen_set_nzcv(tmp);
1798
        dead_tmp(tmp2);
1799
        dead_tmp(tmp);
1800
        break;
1801
    case 0x01c: case 0x41c: case 0x81c: case 0xc1c:        /* WACC */
1802
        wrd = (insn >> 12) & 0xf;
1803
        rd0 = (insn >> 16) & 0xf;
1804
        gen_op_iwmmxt_movq_M0_wRn(rd0);
1805
        switch ((insn >> 22) & 3) {
1806
        case 0:
1807
            gen_helper_iwmmxt_addcb(cpu_M0, cpu_M0);
1808
            break;
1809
        case 1:
1810
            gen_helper_iwmmxt_addcw(cpu_M0, cpu_M0);
1811
            break;
1812
        case 2:
1813
            gen_helper_iwmmxt_addcl(cpu_M0, cpu_M0);
1814
            break;
1815
        case 3:
1816
            return 1;
1817
        }
1818
        gen_op_iwmmxt_movq_wRn_M0(wrd);
1819
        gen_op_iwmmxt_set_mup();
1820
        break;
1821
    case 0x115: case 0x515: case 0x915: case 0xd15:        /* TORC */
1822
        if ((insn & 0x000ff00f) != 0x0003f000 || ((insn >> 22) & 3) == 3)
1823
            return 1;
1824
        tmp = iwmmxt_load_creg(ARM_IWMMXT_wCASF);
1825
        tmp2 = new_tmp();
1826
        tcg_gen_mov_i32(tmp2, tmp);
1827
        switch ((insn >> 22) & 3) {
1828
        case 0:
1829
            for (i = 0; i < 7; i ++) {
1830
                tcg_gen_shli_i32(tmp2, tmp2, 4);
1831
                tcg_gen_or_i32(tmp, tmp, tmp2);
1832
            }
1833
            break;
1834
        case 1:
1835
            for (i = 0; i < 3; i ++) {
1836
                tcg_gen_shli_i32(tmp2, tmp2, 8);
1837
                tcg_gen_or_i32(tmp, tmp, tmp2);
1838
            }
1839
            break;
1840
        case 2:
1841
            tcg_gen_shli_i32(tmp2, tmp2, 16);
1842
            tcg_gen_or_i32(tmp, tmp, tmp2);
1843
            break;
1844
        }
1845
        gen_set_nzcv(tmp);
1846
        dead_tmp(tmp2);
1847
        dead_tmp(tmp);
1848
        break;
1849
    case 0x103: case 0x503: case 0x903: case 0xd03:        /* TMOVMSK */
1850
        rd = (insn >> 12) & 0xf;
1851
        rd0 = (insn >> 16) & 0xf;
1852
        if ((insn & 0xf) != 0 || ((insn >> 22) & 3) == 3)
1853
            return 1;
1854
        gen_op_iwmmxt_movq_M0_wRn(rd0);
1855
        tmp = new_tmp();
1856
        switch ((insn >> 22) & 3) {
1857
        case 0:
1858
            gen_helper_iwmmxt_msbb(tmp, cpu_M0);
1859
            break;
1860
        case 1:
1861
            gen_helper_iwmmxt_msbw(tmp, cpu_M0);
1862
            break;
1863
        case 2:
1864
            gen_helper_iwmmxt_msbl(tmp, cpu_M0);
1865
            break;
1866
        }
1867
        store_reg(s, rd, tmp);
1868
        break;
1869
    case 0x106: case 0x306: case 0x506: case 0x706:        /* WCMPGT */
1870
    case 0x906: case 0xb06: case 0xd06: case 0xf06:
1871
        wrd = (insn >> 12) & 0xf;
1872
        rd0 = (insn >> 16) & 0xf;
1873
        rd1 = (insn >> 0) & 0xf;
1874
        gen_op_iwmmxt_movq_M0_wRn(rd0);
1875
        switch ((insn >> 22) & 3) {
1876
        case 0:
1877
            if (insn & (1 << 21))
1878
                gen_op_iwmmxt_cmpgtsb_M0_wRn(rd1);
1879
            else
1880
                gen_op_iwmmxt_cmpgtub_M0_wRn(rd1);
1881
            break;
1882
        case 1:
1883
            if (insn & (1 << 21))
1884
                gen_op_iwmmxt_cmpgtsw_M0_wRn(rd1);
1885
            else
1886
                gen_op_iwmmxt_cmpgtuw_M0_wRn(rd1);
1887
            break;
1888
        case 2:
1889
            if (insn & (1 << 21))
1890
                gen_op_iwmmxt_cmpgtsl_M0_wRn(rd1);
1891
            else
1892
                gen_op_iwmmxt_cmpgtul_M0_wRn(rd1);
1893
            break;
1894
        case 3:
1895
            return 1;
1896
        }
1897
        gen_op_iwmmxt_movq_wRn_M0(wrd);
1898
        gen_op_iwmmxt_set_mup();
1899
        gen_op_iwmmxt_set_cup();
1900
        break;
1901
    case 0x00e: case 0x20e: case 0x40e: case 0x60e:        /* WUNPCKEL */
1902
    case 0x80e: case 0xa0e: case 0xc0e: case 0xe0e:
1903
        wrd = (insn >> 12) & 0xf;
1904
        rd0 = (insn >> 16) & 0xf;
1905
        gen_op_iwmmxt_movq_M0_wRn(rd0);
1906
        switch ((insn >> 22) & 3) {
1907
        case 0:
1908
            if (insn & (1 << 21))
1909
                gen_op_iwmmxt_unpacklsb_M0();
1910
            else
1911
                gen_op_iwmmxt_unpacklub_M0();
1912
            break;
1913
        case 1:
1914
            if (insn & (1 << 21))
1915
                gen_op_iwmmxt_unpacklsw_M0();
1916
            else
1917
                gen_op_iwmmxt_unpackluw_M0();
1918
            break;
1919
        case 2:
1920
            if (insn & (1 << 21))
1921
                gen_op_iwmmxt_unpacklsl_M0();
1922
            else
1923
                gen_op_iwmmxt_unpacklul_M0();
1924
            break;
1925
        case 3:
1926
            return 1;
1927
        }
1928
        gen_op_iwmmxt_movq_wRn_M0(wrd);
1929
        gen_op_iwmmxt_set_mup();
1930
        gen_op_iwmmxt_set_cup();
1931
        break;
1932
    case 0x00c: case 0x20c: case 0x40c: case 0x60c:        /* WUNPCKEH */
1933
    case 0x80c: case 0xa0c: case 0xc0c: case 0xe0c:
1934
        wrd = (insn >> 12) & 0xf;
1935
        rd0 = (insn >> 16) & 0xf;
1936
        gen_op_iwmmxt_movq_M0_wRn(rd0);
1937
        switch ((insn >> 22) & 3) {
1938
        case 0:
1939
            if (insn & (1 << 21))
1940
                gen_op_iwmmxt_unpackhsb_M0();
1941
            else
1942
                gen_op_iwmmxt_unpackhub_M0();
1943
            break;
1944
        case 1:
1945
            if (insn & (1 << 21))
1946
                gen_op_iwmmxt_unpackhsw_M0();
1947
            else
1948
                gen_op_iwmmxt_unpackhuw_M0();
1949
            break;
1950
        case 2:
1951
            if (insn & (1 << 21))
1952
                gen_op_iwmmxt_unpackhsl_M0();
1953
            else
1954
                gen_op_iwmmxt_unpackhul_M0();
1955
            break;
1956
        case 3:
1957
            return 1;
1958
        }
1959
        gen_op_iwmmxt_movq_wRn_M0(wrd);
1960
        gen_op_iwmmxt_set_mup();
1961
        gen_op_iwmmxt_set_cup();
1962
        break;
1963
    case 0x204: case 0x604: case 0xa04: case 0xe04:        /* WSRL */
1964
    case 0x214: case 0x614: case 0xa14: case 0xe14:
1965
        if (((insn >> 22) & 3) == 0)
1966
            return 1;
1967
        wrd = (insn >> 12) & 0xf;
1968
        rd0 = (insn >> 16) & 0xf;
1969
        gen_op_iwmmxt_movq_M0_wRn(rd0);
1970
        tmp = new_tmp();
1971
        if (gen_iwmmxt_shift(insn, 0xff, tmp)) {
1972
            dead_tmp(tmp);
1973
            return 1;
1974
        }
1975
        switch ((insn >> 22) & 3) {
1976
        case 1:
1977
            gen_helper_iwmmxt_srlw(cpu_M0, cpu_env, cpu_M0, tmp);
1978
            break;
1979
        case 2:
1980
            gen_helper_iwmmxt_srll(cpu_M0, cpu_env, cpu_M0, tmp);
1981
            break;
1982
        case 3:
1983
            gen_helper_iwmmxt_srlq(cpu_M0, cpu_env, cpu_M0, tmp);
1984
            break;
1985
        }
1986
        dead_tmp(tmp);
1987
        gen_op_iwmmxt_movq_wRn_M0(wrd);
1988
        gen_op_iwmmxt_set_mup();
1989
        gen_op_iwmmxt_set_cup();
1990
        break;
1991
    case 0x004: case 0x404: case 0x804: case 0xc04:        /* WSRA */
1992
    case 0x014: case 0x414: case 0x814: case 0xc14:
1993
        if (((insn >> 22) & 3) == 0)
1994
            return 1;
1995
        wrd = (insn >> 12) & 0xf;
1996
        rd0 = (insn >> 16) & 0xf;
1997
        gen_op_iwmmxt_movq_M0_wRn(rd0);
1998
        tmp = new_tmp();
1999
        if (gen_iwmmxt_shift(insn, 0xff, tmp)) {
2000
            dead_tmp(tmp);
2001
            return 1;
2002
        }
2003
        switch ((insn >> 22) & 3) {
2004
        case 1:
2005
            gen_helper_iwmmxt_sraw(cpu_M0, cpu_env, cpu_M0, tmp);
2006
            break;
2007
        case 2:
2008
            gen_helper_iwmmxt_sral(cpu_M0, cpu_env, cpu_M0, tmp);
2009
            break;
2010
        case 3:
2011
            gen_helper_iwmmxt_sraq(cpu_M0, cpu_env, cpu_M0, tmp);
2012
            break;
2013
        }
2014
        dead_tmp(tmp);
2015
        gen_op_iwmmxt_movq_wRn_M0(wrd);
2016
        gen_op_iwmmxt_set_mup();
2017
        gen_op_iwmmxt_set_cup();
2018
        break;
2019
    case 0x104: case 0x504: case 0x904: case 0xd04:        /* WSLL */
2020
    case 0x114: case 0x514: case 0x914: case 0xd14:
2021
        if (((insn >> 22) & 3) == 0)
2022
            return 1;
2023
        wrd = (insn >> 12) & 0xf;
2024
        rd0 = (insn >> 16) & 0xf;
2025
        gen_op_iwmmxt_movq_M0_wRn(rd0);
2026
        tmp = new_tmp();
2027
        if (gen_iwmmxt_shift(insn, 0xff, tmp)) {
2028
            dead_tmp(tmp);
2029
            return 1;
2030
        }
2031
        switch ((insn >> 22) & 3) {
2032
        case 1:
2033
            gen_helper_iwmmxt_sllw(cpu_M0, cpu_env, cpu_M0, tmp);
2034
            break;
2035
        case 2:
2036
            gen_helper_iwmmxt_slll(cpu_M0, cpu_env, cpu_M0, tmp);
2037
            break;
2038
        case 3:
2039
            gen_helper_iwmmxt_sllq(cpu_M0, cpu_env, cpu_M0, tmp);
2040
            break;
2041
        }
2042
        dead_tmp(tmp);
2043
        gen_op_iwmmxt_movq_wRn_M0(wrd);
2044
        gen_op_iwmmxt_set_mup();
2045
        gen_op_iwmmxt_set_cup();
2046
        break;
2047
    case 0x304: case 0x704: case 0xb04: case 0xf04:        /* WROR */
2048
    case 0x314: case 0x714: case 0xb14: case 0xf14:
2049
        if (((insn >> 22) & 3) == 0)
2050
            return 1;
2051
        wrd = (insn >> 12) & 0xf;
2052
        rd0 = (insn >> 16) & 0xf;
2053
        gen_op_iwmmxt_movq_M0_wRn(rd0);
2054
        tmp = new_tmp();
2055
        switch ((insn >> 22) & 3) {
2056
        case 1:
2057
            if (gen_iwmmxt_shift(insn, 0xf, tmp)) {
2058
                dead_tmp(tmp);
2059
                return 1;
2060
            }
2061
            gen_helper_iwmmxt_rorw(cpu_M0, cpu_env, cpu_M0, tmp);
2062
            break;
2063
        case 2:
2064
            if (gen_iwmmxt_shift(insn, 0x1f, tmp)) {
2065
                dead_tmp(tmp);
2066
                return 1;
2067
            }
2068
            gen_helper_iwmmxt_rorl(cpu_M0, cpu_env, cpu_M0, tmp);
2069
            break;
2070
        case 3:
2071
            if (gen_iwmmxt_shift(insn, 0x3f, tmp)) {
2072
                dead_tmp(tmp);
2073
                return 1;
2074
            }
2075
            gen_helper_iwmmxt_rorq(cpu_M0, cpu_env, cpu_M0, tmp);
2076
            break;
2077
        }
2078
        dead_tmp(tmp);
2079
        gen_op_iwmmxt_movq_wRn_M0(wrd);
2080
        gen_op_iwmmxt_set_mup();
2081
        gen_op_iwmmxt_set_cup();
2082
        break;
2083
    case 0x116: case 0x316: case 0x516: case 0x716:        /* WMIN */
2084
    case 0x916: case 0xb16: case 0xd16: case 0xf16:
2085
        wrd = (insn >> 12) & 0xf;
2086
        rd0 = (insn >> 16) & 0xf;
2087
        rd1 = (insn >> 0) & 0xf;
2088
        gen_op_iwmmxt_movq_M0_wRn(rd0);
2089
        switch ((insn >> 22) & 3) {
2090
        case 0:
2091
            if (insn & (1 << 21))
2092
                gen_op_iwmmxt_minsb_M0_wRn(rd1);
2093
            else
2094
                gen_op_iwmmxt_minub_M0_wRn(rd1);
2095
            break;
2096
        case 1:
2097
            if (insn & (1 << 21))
2098
                gen_op_iwmmxt_minsw_M0_wRn(rd1);
2099
            else
2100
                gen_op_iwmmxt_minuw_M0_wRn(rd1);
2101
            break;
2102
        case 2:
2103
            if (insn & (1 << 21))
2104
                gen_op_iwmmxt_minsl_M0_wRn(rd1);
2105
            else
2106
                gen_op_iwmmxt_minul_M0_wRn(rd1);
2107
            break;
2108
        case 3:
2109
            return 1;
2110
        }
2111
        gen_op_iwmmxt_movq_wRn_M0(wrd);
2112
        gen_op_iwmmxt_set_mup();
2113
        break;
2114
    case 0x016: case 0x216: case 0x416: case 0x616:        /* WMAX */
2115
    case 0x816: case 0xa16: case 0xc16: case 0xe16:
2116
        wrd = (insn >> 12) & 0xf;
2117
        rd0 = (insn >> 16) & 0xf;
2118
        rd1 = (insn >> 0) & 0xf;
2119
        gen_op_iwmmxt_movq_M0_wRn(rd0);
2120
        switch ((insn >> 22) & 3) {
2121
        case 0:
2122
            if (insn & (1 << 21))
2123
                gen_op_iwmmxt_maxsb_M0_wRn(rd1);
2124
            else
2125
                gen_op_iwmmxt_maxub_M0_wRn(rd1);
2126
            break;
2127
        case 1:
2128
            if (insn & (1 << 21))
2129
                gen_op_iwmmxt_maxsw_M0_wRn(rd1);
2130
            else
2131
                gen_op_iwmmxt_maxuw_M0_wRn(rd1);
2132
            break;
2133
        case 2:
2134
            if (insn & (1 << 21))
2135
                gen_op_iwmmxt_maxsl_M0_wRn(rd1);
2136
            else
2137
                gen_op_iwmmxt_maxul_M0_wRn(rd1);
2138
            break;
2139
        case 3:
2140
            return 1;
2141
        }
2142
        gen_op_iwmmxt_movq_wRn_M0(wrd);
2143
        gen_op_iwmmxt_set_mup();
2144
        break;
2145
    case 0x002: case 0x102: case 0x202: case 0x302:        /* WALIGNI */
2146
    case 0x402: case 0x502: case 0x602: case 0x702:
2147
        wrd = (insn >> 12) & 0xf;
2148
        rd0 = (insn >> 16) & 0xf;
2149
        rd1 = (insn >> 0) & 0xf;
2150
        gen_op_iwmmxt_movq_M0_wRn(rd0);
2151
        tmp = tcg_const_i32((insn >> 20) & 3);
2152
        iwmmxt_load_reg(cpu_V1, rd1);
2153
        gen_helper_iwmmxt_align(cpu_M0, cpu_M0, cpu_V1, tmp);
2154
        tcg_temp_free(tmp);
2155
        gen_op_iwmmxt_movq_wRn_M0(wrd);
2156
        gen_op_iwmmxt_set_mup();
2157
        break;
2158
    case 0x01a: case 0x11a: case 0x21a: case 0x31a:        /* WSUB */
2159
    case 0x41a: case 0x51a: case 0x61a: case 0x71a:
2160
    case 0x81a: case 0x91a: case 0xa1a: case 0xb1a:
2161
    case 0xc1a: case 0xd1a: case 0xe1a: case 0xf1a:
2162
        wrd = (insn >> 12) & 0xf;
2163
        rd0 = (insn >> 16) & 0xf;
2164
        rd1 = (insn >> 0) & 0xf;
2165
        gen_op_iwmmxt_movq_M0_wRn(rd0);
2166
        switch ((insn >> 20) & 0xf) {
2167
        case 0x0:
2168
            gen_op_iwmmxt_subnb_M0_wRn(rd1);
2169
            break;
2170
        case 0x1:
2171
            gen_op_iwmmxt_subub_M0_wRn(rd1);
2172
            break;
2173
        case 0x3:
2174
            gen_op_iwmmxt_subsb_M0_wRn(rd1);
2175
            break;
2176
        case 0x4:
2177
            gen_op_iwmmxt_subnw_M0_wRn(rd1);
2178
            break;
2179
        case 0x5:
2180
            gen_op_iwmmxt_subuw_M0_wRn(rd1);
2181
            break;
2182
        case 0x7:
2183
            gen_op_iwmmxt_subsw_M0_wRn(rd1);
2184
            break;
2185
        case 0x8:
2186
            gen_op_iwmmxt_subnl_M0_wRn(rd1);
2187
            break;
2188
        case 0x9:
2189
            gen_op_iwmmxt_subul_M0_wRn(rd1);
2190
            break;
2191
        case 0xb:
2192
            gen_op_iwmmxt_subsl_M0_wRn(rd1);
2193
            break;
2194
        default:
2195
            return 1;
2196
        }
2197
        gen_op_iwmmxt_movq_wRn_M0(wrd);
2198
        gen_op_iwmmxt_set_mup();
2199
        gen_op_iwmmxt_set_cup();
2200
        break;
2201
    case 0x01e: case 0x11e: case 0x21e: case 0x31e:        /* WSHUFH */
2202
    case 0x41e: case 0x51e: case 0x61e: case 0x71e:
2203
    case 0x81e: case 0x91e: case 0xa1e: case 0xb1e:
2204
    case 0xc1e: case 0xd1e: case 0xe1e: case 0xf1e:
2205
        wrd = (insn >> 12) & 0xf;
2206
        rd0 = (insn >> 16) & 0xf;
2207
        gen_op_iwmmxt_movq_M0_wRn(rd0);
2208
        tmp = tcg_const_i32(((insn >> 16) & 0xf0) | (insn & 0x0f));
2209
        gen_helper_iwmmxt_shufh(cpu_M0, cpu_env, cpu_M0, tmp);
2210
        tcg_temp_free(tmp);
2211
        gen_op_iwmmxt_movq_wRn_M0(wrd);
2212
        gen_op_iwmmxt_set_mup();
2213
        gen_op_iwmmxt_set_cup();
2214
        break;
2215
    case 0x018: case 0x118: case 0x218: case 0x318:        /* WADD */
2216
    case 0x418: case 0x518: case 0x618: case 0x718:
2217
    case 0x818: case 0x918: case 0xa18: case 0xb18:
2218
    case 0xc18: case 0xd18: case 0xe18: case 0xf18:
2219
        wrd = (insn >> 12) & 0xf;
2220
        rd0 = (insn >> 16) & 0xf;
2221
        rd1 = (insn >> 0) & 0xf;
2222
        gen_op_iwmmxt_movq_M0_wRn(rd0);
2223
        switch ((insn >> 20) & 0xf) {
2224
        case 0x0:
2225
            gen_op_iwmmxt_addnb_M0_wRn(rd1);
2226
            break;
2227
        case 0x1:
2228
            gen_op_iwmmxt_addub_M0_wRn(rd1);
2229
            break;
2230
        case 0x3:
2231
            gen_op_iwmmxt_addsb_M0_wRn(rd1);
2232
            break;
2233
        case 0x4:
2234
            gen_op_iwmmxt_addnw_M0_wRn(rd1);
2235
            break;
2236
        case 0x5:
2237
            gen_op_iwmmxt_adduw_M0_wRn(rd1);
2238
            break;
2239
        case 0x7:
2240
            gen_op_iwmmxt_addsw_M0_wRn(rd1);
2241
            break;
2242
        case 0x8:
2243
            gen_op_iwmmxt_addnl_M0_wRn(rd1);
2244
            break;
2245
        case 0x9:
2246
            gen_op_iwmmxt_addul_M0_wRn(rd1);
2247
            break;
2248
        case 0xb:
2249
            gen_op_iwmmxt_addsl_M0_wRn(rd1);
2250
            break;
2251
        default:
2252
            return 1;
2253
        }
2254
        gen_op_iwmmxt_movq_wRn_M0(wrd);
2255
        gen_op_iwmmxt_set_mup();
2256
        gen_op_iwmmxt_set_cup();
2257
        break;
2258
    case 0x008: case 0x108: case 0x208: case 0x308:        /* WPACK */
2259
    case 0x408: case 0x508: case 0x608: case 0x708:
2260
    case 0x808: case 0x908: case 0xa08: case 0xb08:
2261
    case 0xc08: case 0xd08: case 0xe08: case 0xf08:
2262
        if (!(insn & (1 << 20)) || ((insn >> 22) & 3) == 0)
2263
            return 1;
2264
        wrd = (insn >> 12) & 0xf;
2265
        rd0 = (insn >> 16) & 0xf;
2266
        rd1 = (insn >> 0) & 0xf;
2267
        gen_op_iwmmxt_movq_M0_wRn(rd0);
2268
        switch ((insn >> 22) & 3) {
2269
        case 1:
2270
            if (insn & (1 << 21))
2271
                gen_op_iwmmxt_packsw_M0_wRn(rd1);
2272
            else
2273
                gen_op_iwmmxt_packuw_M0_wRn(rd1);
2274
            break;
2275
        case 2:
2276
            if (insn & (1 << 21))
2277
                gen_op_iwmmxt_packsl_M0_wRn(rd1);
2278
            else
2279
                gen_op_iwmmxt_packul_M0_wRn(rd1);
2280
            break;
2281
        case 3:
2282
            if (insn & (1 << 21))
2283
                gen_op_iwmmxt_packsq_M0_wRn(rd1);
2284
            else
2285
                gen_op_iwmmxt_packuq_M0_wRn(rd1);
2286
            break;
2287
        }
2288
        gen_op_iwmmxt_movq_wRn_M0(wrd);
2289
        gen_op_iwmmxt_set_mup();
2290
        gen_op_iwmmxt_set_cup();
2291
        break;
2292
    case 0x201: case 0x203: case 0x205: case 0x207:
2293
    case 0x209: case 0x20b: case 0x20d: case 0x20f:
2294
    case 0x211: case 0x213: case 0x215: case 0x217:
2295
    case 0x219: case 0x21b: case 0x21d: case 0x21f:
2296
        wrd = (insn >> 5) & 0xf;
2297
        rd0 = (insn >> 12) & 0xf;
2298
        rd1 = (insn >> 0) & 0xf;
2299
        if (rd0 == 0xf || rd1 == 0xf)
2300
            return 1;
2301
        gen_op_iwmmxt_movq_M0_wRn(wrd);
2302
        tmp = load_reg(s, rd0);
2303
        tmp2 = load_reg(s, rd1);
2304
        switch ((insn >> 16) & 0xf) {
2305
        case 0x0:                                        /* TMIA */
2306
            gen_helper_iwmmxt_muladdsl(cpu_M0, cpu_M0, tmp, tmp2);
2307
            break;
2308
        case 0x8:                                        /* TMIAPH */
2309
            gen_helper_iwmmxt_muladdsw(cpu_M0, cpu_M0, tmp, tmp2);
2310
            break;
2311
        case 0xc: case 0xd: case 0xe: case 0xf:                /* TMIAxy */
2312
            if (insn & (1 << 16))
2313
                tcg_gen_shri_i32(tmp, tmp, 16);
2314
            if (insn & (1 << 17))
2315
                tcg_gen_shri_i32(tmp2, tmp2, 16);
2316
            gen_helper_iwmmxt_muladdswl(cpu_M0, cpu_M0, tmp, tmp2);
2317
            break;
2318
        default:
2319
            dead_tmp(tmp2);
2320
            dead_tmp(tmp);
2321
            return 1;
2322
        }
2323
        dead_tmp(tmp2);
2324
        dead_tmp(tmp);
2325
        gen_op_iwmmxt_movq_wRn_M0(wrd);
2326
        gen_op_iwmmxt_set_mup();
2327
        break;
2328
    default:
2329
        return 1;
2330
    }
2331

    
2332
    return 0;
2333
}
2334

    
2335
/* Disassemble an XScale DSP instruction.  Returns nonzero if an error occured
2336
   (ie. an undefined instruction).  */
2337
static int disas_dsp_insn(CPUState *env, DisasContext *s, uint32_t insn)
2338
{
2339
    int acc, rd0, rd1, rdhi, rdlo;
2340
    TCGv tmp, tmp2;
2341

    
2342
    if ((insn & 0x0ff00f10) == 0x0e200010) {
2343
        /* Multiply with Internal Accumulate Format */
2344
        rd0 = (insn >> 12) & 0xf;
2345
        rd1 = insn & 0xf;
2346
        acc = (insn >> 5) & 7;
2347

    
2348
        if (acc != 0)
2349
            return 1;
2350

    
2351
        tmp = load_reg(s, rd0);
2352
        tmp2 = load_reg(s, rd1);
2353
        switch ((insn >> 16) & 0xf) {
2354
        case 0x0:                                        /* MIA */
2355
            gen_helper_iwmmxt_muladdsl(cpu_M0, cpu_M0, tmp, tmp2);
2356
            break;
2357
        case 0x8:                                        /* MIAPH */
2358
            gen_helper_iwmmxt_muladdsw(cpu_M0, cpu_M0, tmp, tmp2);
2359
            break;
2360
        case 0xc:                                        /* MIABB */
2361
        case 0xd:                                        /* MIABT */
2362
        case 0xe:                                        /* MIATB */
2363
        case 0xf:                                        /* MIATT */
2364
            if (insn & (1 << 16))
2365
                tcg_gen_shri_i32(tmp, tmp, 16);
2366
            if (insn & (1 << 17))
2367
                tcg_gen_shri_i32(tmp2, tmp2, 16);
2368
            gen_helper_iwmmxt_muladdswl(cpu_M0, cpu_M0, tmp, tmp2);
2369
            break;
2370
        default:
2371
            return 1;
2372
        }
2373
        dead_tmp(tmp2);
2374
        dead_tmp(tmp);
2375

    
2376
        gen_op_iwmmxt_movq_wRn_M0(acc);
2377
        return 0;
2378
    }
2379

    
2380
    if ((insn & 0x0fe00ff8) == 0x0c400000) {
2381
        /* Internal Accumulator Access Format */
2382
        rdhi = (insn >> 16) & 0xf;
2383
        rdlo = (insn >> 12) & 0xf;
2384
        acc = insn & 7;
2385

    
2386
        if (acc != 0)
2387
            return 1;
2388

    
2389
        if (insn & ARM_CP_RW_BIT) {                        /* MRA */
2390
            iwmmxt_load_reg(cpu_V0, acc);
2391
            tcg_gen_trunc_i64_i32(cpu_R[rdlo], cpu_V0);
2392
            tcg_gen_shri_i64(cpu_V0, cpu_V0, 32);
2393
            tcg_gen_trunc_i64_i32(cpu_R[rdhi], cpu_V0);
2394
            tcg_gen_andi_i32(cpu_R[rdhi], cpu_R[rdhi], (1 << (40 - 32)) - 1);
2395
        } else {                                        /* MAR */
2396
            tcg_gen_concat_i32_i64(cpu_V0, cpu_R[rdlo], cpu_R[rdhi]);
2397
            iwmmxt_store_reg(cpu_V0, acc);
2398
        }
2399
        return 0;
2400
    }
2401

    
2402
    return 1;
2403
}
2404

    
2405
/* Disassemble system coprocessor instruction.  Return nonzero if
2406
   instruction is not defined.  */
2407
static int disas_cp_insn(CPUState *env, DisasContext *s, uint32_t insn)
2408
{
2409
    TCGv tmp, tmp2;
2410
    uint32_t rd = (insn >> 12) & 0xf;
2411
    uint32_t cp = (insn >> 8) & 0xf;
2412
    if (IS_USER(s)) {
2413
        return 1;
2414
    }
2415

    
2416
    if (insn & ARM_CP_RW_BIT) {
2417
        if (!env->cp[cp].cp_read)
2418
            return 1;
2419
        gen_set_pc_im(s->pc);
2420
        tmp = new_tmp();
2421
        tmp2 = tcg_const_i32(insn);
2422
        gen_helper_get_cp(tmp, cpu_env, tmp2);
2423
        tcg_temp_free(tmp2);
2424
        store_reg(s, rd, tmp);
2425
    } else {
2426
        if (!env->cp[cp].cp_write)
2427
            return 1;
2428
        gen_set_pc_im(s->pc);
2429
        tmp = load_reg(s, rd);
2430
        tmp2 = tcg_const_i32(insn);
2431
        gen_helper_set_cp(cpu_env, tmp2, tmp);
2432
        tcg_temp_free(tmp2);
2433
        dead_tmp(tmp);
2434
    }
2435
    return 0;
2436
}
2437

    
2438
static int cp15_user_ok(uint32_t insn)
2439
{
2440
    int cpn = (insn >> 16) & 0xf;
2441
    int cpm = insn & 0xf;
2442
    int op = ((insn >> 5) & 7) | ((insn >> 18) & 0x38);
2443

    
2444
    if (cpn == 13 && cpm == 0) {
2445
        /* TLS register.  */
2446
        if (op == 2 || (op == 3 && (insn & ARM_CP_RW_BIT)))
2447
            return 1;
2448
    }
2449
    if (cpn == 7) {
2450
        /* ISB, DSB, DMB.  */
2451
        if ((cpm == 5 && op == 4)
2452
                || (cpm == 10 && (op == 4 || op == 5)))
2453
            return 1;
2454
    }
2455
    return 0;
2456
}
2457

    
2458
static int cp15_tls_load_store(CPUState *env, DisasContext *s, uint32_t insn, uint32_t rd)
2459
{
2460
    TCGv tmp;
2461
    int cpn = (insn >> 16) & 0xf;
2462
    int cpm = insn & 0xf;
2463
    int op = ((insn >> 5) & 7) | ((insn >> 18) & 0x38);
2464

    
2465
    if (!arm_feature(env, ARM_FEATURE_V6K))
2466
        return 0;
2467

    
2468
    if (!(cpn == 13 && cpm == 0))
2469
        return 0;
2470

    
2471
    if (insn & ARM_CP_RW_BIT) {
2472
        tmp = new_tmp();
2473
        switch (op) {
2474
        case 2:
2475
            tcg_gen_ld_i32(tmp, cpu_env, offsetof(CPUARMState, cp15.c13_tls1));
2476
            break;
2477
        case 3:
2478
            tcg_gen_ld_i32(tmp, cpu_env, offsetof(CPUARMState, cp15.c13_tls2));
2479
            break;
2480
        case 4:
2481
            tcg_gen_ld_i32(tmp, cpu_env, offsetof(CPUARMState, cp15.c13_tls3));
2482
            break;
2483
        default:
2484
            dead_tmp(tmp);
2485
            return 0;
2486
        }
2487
        store_reg(s, rd, tmp);
2488

    
2489
    } else {
2490
        tmp = load_reg(s, rd);
2491
        switch (op) {
2492
        case 2:
2493
            tcg_gen_st_i32(tmp, cpu_env, offsetof(CPUARMState, cp15.c13_tls1));
2494
            break;
2495
        case 3:
2496
            tcg_gen_st_i32(tmp, cpu_env, offsetof(CPUARMState, cp15.c13_tls2));
2497
            break;
2498
        case 4:
2499
            tcg_gen_st_i32(tmp, cpu_env, offsetof(CPUARMState, cp15.c13_tls3));
2500
            break;
2501
        default:
2502
            return 0;
2503
        }
2504
        dead_tmp(tmp);
2505
    }
2506
    return 1;
2507
}
2508

    
2509
/* Disassemble system coprocessor (cp15) instruction.  Return nonzero if
2510
   instruction is not defined.  */
2511
static int disas_cp15_insn(CPUState *env, DisasContext *s, uint32_t insn)
2512
{
2513
    uint32_t rd;
2514
    TCGv tmp, tmp2;
2515

    
2516
    /* M profile cores use memory mapped registers instead of cp15.  */
2517
    if (arm_feature(env, ARM_FEATURE_M))
2518
        return 1;
2519

    
2520
    if ((insn & (1 << 25)) == 0) {
2521
        if (insn & (1 << 20)) {
2522
            /* mrrc */
2523
            return 1;
2524
        }
2525
        /* mcrr.  Used for block cache operations, so implement as no-op.  */
2526
        return 0;
2527
    }
2528
    if ((insn & (1 << 4)) == 0) {
2529
        /* cdp */
2530
        return 1;
2531
    }
2532
    if (IS_USER(s) && !cp15_user_ok(insn)) {
2533
        return 1;
2534
    }
2535
    if ((insn & 0x0fff0fff) == 0x0e070f90
2536
        || (insn & 0x0fff0fff) == 0x0e070f58) {
2537
        /* Wait for interrupt.  */
2538
        gen_set_pc_im(s->pc);
2539
        s->is_jmp = DISAS_WFI;
2540
        return 0;
2541
    }
2542
    rd = (insn >> 12) & 0xf;
2543

    
2544
    if (cp15_tls_load_store(env, s, insn, rd))
2545
        return 0;
2546

    
2547
    tmp2 = tcg_const_i32(insn);
2548
    if (insn & ARM_CP_RW_BIT) {
2549
        tmp = new_tmp();
2550
        gen_helper_get_cp15(tmp, cpu_env, tmp2);
2551
        /* If the destination register is r15 then sets condition codes.  */
2552
        if (rd != 15)
2553
            store_reg(s, rd, tmp);
2554
        else
2555
            dead_tmp(tmp);
2556
    } else {
2557
        tmp = load_reg(s, rd);
2558
        gen_helper_set_cp15(cpu_env, tmp2, tmp);
2559
        dead_tmp(tmp);
2560
        /* Normally we would always end the TB here, but Linux
2561
         * arch/arm/mach-pxa/sleep.S expects two instructions following
2562
         * an MMU enable to execute from cache.  Imitate this behaviour.  */
2563
        if (!arm_feature(env, ARM_FEATURE_XSCALE) ||
2564
                (insn & 0x0fff0fff) != 0x0e010f10)
2565
            gen_lookup_tb(s);
2566
    }
2567
    tcg_temp_free_i32(tmp2);
2568
    return 0;
2569
}
2570

    
2571
#define VFP_REG_SHR(x, n) (((n) > 0) ? (x) >> (n) : (x) << -(n))
2572
#define VFP_SREG(insn, bigbit, smallbit) \
2573
  ((VFP_REG_SHR(insn, bigbit - 1) & 0x1e) | (((insn) >> (smallbit)) & 1))
2574
#define VFP_DREG(reg, insn, bigbit, smallbit) do { \
2575
    if (arm_feature(env, ARM_FEATURE_VFP3)) { \
2576
        reg = (((insn) >> (bigbit)) & 0x0f) \
2577
              | (((insn) >> ((smallbit) - 4)) & 0x10); \
2578
    } else { \
2579
        if (insn & (1 << (smallbit))) \
2580
            return 1; \
2581
        reg = ((insn) >> (bigbit)) & 0x0f; \
2582
    }} while (0)
2583

    
2584
#define VFP_SREG_D(insn) VFP_SREG(insn, 12, 22)
2585
#define VFP_DREG_D(reg, insn) VFP_DREG(reg, insn, 12, 22)
2586
#define VFP_SREG_N(insn) VFP_SREG(insn, 16,  7)
2587
#define VFP_DREG_N(reg, insn) VFP_DREG(reg, insn, 16,  7)
2588
#define VFP_SREG_M(insn) VFP_SREG(insn,  0,  5)
2589
#define VFP_DREG_M(reg, insn) VFP_DREG(reg, insn,  0,  5)
2590

    
2591
/* Move between integer and VFP cores.  */
2592
static TCGv gen_vfp_mrs(void)
2593
{
2594
    TCGv tmp = new_tmp();
2595
    tcg_gen_mov_i32(tmp, cpu_F0s);
2596
    return tmp;
2597
}
2598

    
2599
static void gen_vfp_msr(TCGv tmp)
2600
{
2601
    tcg_gen_mov_i32(cpu_F0s, tmp);
2602
    dead_tmp(tmp);
2603
}
2604

    
2605
static inline int
2606
vfp_enabled(CPUState * env)
2607
{
2608
    return ((env->vfp.xregs[ARM_VFP_FPEXC] & (1 << 30)) != 0);
2609
}
2610

    
2611
static void gen_neon_dup_u8(TCGv var, int shift)
2612
{
2613
    TCGv tmp = new_tmp();
2614
    if (shift)
2615
        tcg_gen_shri_i32(var, var, shift);
2616
    tcg_gen_ext8u_i32(var, var);
2617
    tcg_gen_shli_i32(tmp, var, 8);
2618
    tcg_gen_or_i32(var, var, tmp);
2619
    tcg_gen_shli_i32(tmp, var, 16);
2620
    tcg_gen_or_i32(var, var, tmp);
2621
    dead_tmp(tmp);
2622
}
2623

    
2624
static void gen_neon_dup_low16(TCGv var)
2625
{
2626
    TCGv tmp = new_tmp();
2627
    tcg_gen_ext16u_i32(var, var);
2628
    tcg_gen_shli_i32(tmp, var, 16);
2629
    tcg_gen_or_i32(var, var, tmp);
2630
    dead_tmp(tmp);
2631
}
2632

    
2633
static void gen_neon_dup_high16(TCGv var)
2634
{
2635
    TCGv tmp = new_tmp();
2636
    tcg_gen_andi_i32(var, var, 0xffff0000);
2637
    tcg_gen_shri_i32(tmp, var, 16);
2638
    tcg_gen_or_i32(var, var, tmp);
2639
    dead_tmp(tmp);
2640
}
2641

    
2642
/* Disassemble a VFP instruction.  Returns nonzero if an error occured
2643
   (ie. an undefined instruction).  */
2644
static int disas_vfp_insn(CPUState * env, DisasContext *s, uint32_t insn)
2645
{
2646
    uint32_t rd, rn, rm, op, i, n, offset, delta_d, delta_m, bank_mask;
2647
    int dp, veclen;
2648
    TCGv addr;
2649
    TCGv tmp;
2650
    TCGv tmp2;
2651

    
2652
    if (!arm_feature(env, ARM_FEATURE_VFP))
2653
        return 1;
2654

    
2655
    if (!vfp_enabled(env)) {
2656
        /* VFP disabled.  Only allow fmxr/fmrx to/from some control regs.  */
2657
        if ((insn & 0x0fe00fff) != 0x0ee00a10)
2658
            return 1;
2659
        rn = (insn >> 16) & 0xf;
2660
        if (rn != ARM_VFP_FPSID && rn != ARM_VFP_FPEXC
2661
            && rn != ARM_VFP_MVFR1 && rn != ARM_VFP_MVFR0)
2662
            return 1;
2663
    }
2664
    dp = ((insn & 0xf00) == 0xb00);
2665
    switch ((insn >> 24) & 0xf) {
2666
    case 0xe:
2667
        if (insn & (1 << 4)) {
2668
            /* single register transfer */
2669
            rd = (insn >> 12) & 0xf;
2670
            if (dp) {
2671
                int size;
2672
                int pass;
2673

    
2674
                VFP_DREG_N(rn, insn);
2675
                if (insn & 0xf)
2676
                    return 1;
2677
                if (insn & 0x00c00060
2678
                    && !arm_feature(env, ARM_FEATURE_NEON))
2679
                    return 1;
2680

    
2681
                pass = (insn >> 21) & 1;
2682
                if (insn & (1 << 22)) {
2683
                    size = 0;
2684
                    offset = ((insn >> 5) & 3) * 8;
2685
                } else if (insn & (1 << 5)) {
2686
                    size = 1;
2687
                    offset = (insn & (1 << 6)) ? 16 : 0;
2688
                } else {
2689
                    size = 2;
2690
                    offset = 0;
2691
                }
2692
                if (insn & ARM_CP_RW_BIT) {
2693
                    /* vfp->arm */
2694
                    tmp = neon_load_reg(rn, pass);
2695
                    switch (size) {
2696
                    case 0:
2697
                        if (offset)
2698
                            tcg_gen_shri_i32(tmp, tmp, offset);
2699
                        if (insn & (1 << 23))
2700
                            gen_uxtb(tmp);
2701
                        else
2702
                            gen_sxtb(tmp);
2703
                        break;
2704
                    case 1:
2705
                        if (insn & (1 << 23)) {
2706
                            if (offset) {
2707
                                tcg_gen_shri_i32(tmp, tmp, 16);
2708
                            } else {
2709
                                gen_uxth(tmp);
2710
                            }
2711
                        } else {
2712
                            if (offset) {
2713
                                tcg_gen_sari_i32(tmp, tmp, 16);
2714
                            } else {
2715
                                gen_sxth(tmp);
2716
                            }
2717
                        }
2718
                        break;
2719
                    case 2:
2720
                        break;
2721
                    }
2722
                    store_reg(s, rd, tmp);
2723
                } else {
2724
                    /* arm->vfp */
2725
                    tmp = load_reg(s, rd);
2726
                    if (insn & (1 << 23)) {
2727
                        /* VDUP */
2728
                        if (size == 0) {
2729
                            gen_neon_dup_u8(tmp, 0);
2730
                        } else if (size == 1) {
2731
                            gen_neon_dup_low16(tmp);
2732
                        }
2733
                        for (n = 0; n <= pass * 2; n++) {
2734
                            tmp2 = new_tmp();
2735
                            tcg_gen_mov_i32(tmp2, tmp);
2736
                            neon_store_reg(rn, n, tmp2);
2737
                        }
2738
                        neon_store_reg(rn, n, tmp);
2739
                    } else {
2740
                        /* VMOV */
2741
                        switch (size) {
2742
                        case 0:
2743
                            tmp2 = neon_load_reg(rn, pass);
2744
                            gen_bfi(tmp, tmp2, tmp, offset, 0xff);
2745
                            dead_tmp(tmp2);
2746
                            break;
2747
                        case 1:
2748
                            tmp2 = neon_load_reg(rn, pass);
2749
                            gen_bfi(tmp, tmp2, tmp, offset, 0xffff);
2750
                            dead_tmp(tmp2);
2751
                            break;
2752
                        case 2:
2753
                            break;
2754
                        }
2755
                        neon_store_reg(rn, pass, tmp);
2756
                    }
2757
                }
2758
            } else { /* !dp */
2759
                if ((insn & 0x6f) != 0x00)
2760
                    return 1;
2761
                rn = VFP_SREG_N(insn);
2762
                if (insn & ARM_CP_RW_BIT) {
2763
                    /* vfp->arm */
2764
                    if (insn & (1 << 21)) {
2765
                        /* system register */
2766
                        rn >>= 1;
2767

    
2768
                        switch (rn) {
2769
                        case ARM_VFP_FPSID:
2770
                            /* VFP2 allows access to FSID from userspace.
2771
                               VFP3 restricts all id registers to privileged
2772
                               accesses.  */
2773
                            if (IS_USER(s)
2774
                                && arm_feature(env, ARM_FEATURE_VFP3))
2775
                                return 1;
2776
                            tmp = load_cpu_field(vfp.xregs[rn]);
2777
                            break;
2778
                        case ARM_VFP_FPEXC:
2779
                            if (IS_USER(s))
2780
                                return 1;
2781
                            tmp = load_cpu_field(vfp.xregs[rn]);
2782
                            break;
2783
                        case ARM_VFP_FPINST:
2784
                        case ARM_VFP_FPINST2:
2785
                            /* Not present in VFP3.  */
2786
                            if (IS_USER(s)
2787
                                || arm_feature(env, ARM_FEATURE_VFP3))
2788
                                return 1;
2789
                            tmp = load_cpu_field(vfp.xregs[rn]);
2790
                            break;
2791
                        case ARM_VFP_FPSCR:
2792
                            if (rd == 15) {
2793
                                tmp = load_cpu_field(vfp.xregs[ARM_VFP_FPSCR]);
2794
                                tcg_gen_andi_i32(tmp, tmp, 0xf0000000);
2795
                            } else {
2796
                                tmp = new_tmp();
2797
                                gen_helper_vfp_get_fpscr(tmp, cpu_env);
2798
                            }
2799
                            break;
2800
                        case ARM_VFP_MVFR0:
2801
                        case ARM_VFP_MVFR1:
2802
                            if (IS_USER(s)
2803
                                || !arm_feature(env, ARM_FEATURE_VFP3))
2804
                                return 1;
2805
                            tmp = load_cpu_field(vfp.xregs[rn]);
2806
                            break;
2807
                        default:
2808
                            return 1;
2809
                        }
2810
                    } else {
2811
                        gen_mov_F0_vreg(0, rn);
2812
                        tmp = gen_vfp_mrs();
2813
                    }
2814
                    if (rd == 15) {
2815
                        /* Set the 4 flag bits in the CPSR.  */
2816
                        gen_set_nzcv(tmp);
2817
                        dead_tmp(tmp);
2818
                    } else {
2819
                        store_reg(s, rd, tmp);
2820
                    }
2821
                } else {
2822
                    /* arm->vfp */
2823
                    tmp = load_reg(s, rd);
2824
                    if (insn & (1 << 21)) {
2825
                        rn >>= 1;
2826
                        /* system register */
2827
                        switch (rn) {
2828
                        case ARM_VFP_FPSID:
2829
                        case ARM_VFP_MVFR0:
2830
                        case ARM_VFP_MVFR1:
2831
                            /* Writes are ignored.  */
2832
                            break;
2833
                        case ARM_VFP_FPSCR:
2834
                            gen_helper_vfp_set_fpscr(cpu_env, tmp);
2835
                            dead_tmp(tmp);
2836
                            gen_lookup_tb(s);
2837
                            break;
2838
                        case ARM_VFP_FPEXC:
2839
                            if (IS_USER(s))
2840
                                return 1;
2841
                            /* TODO: VFP subarchitecture support.
2842
                             * For now, keep the EN bit only */
2843
                            tcg_gen_andi_i32(tmp, tmp, 1 << 30);
2844
                            store_cpu_field(tmp, vfp.xregs[rn]);
2845
                            gen_lookup_tb(s);
2846
                            break;
2847
                        case ARM_VFP_FPINST:
2848
                        case ARM_VFP_FPINST2:
2849
                            store_cpu_field(tmp, vfp.xregs[rn]);
2850
                            break;
2851
                        default:
2852
                            return 1;
2853
                        }
2854
                    } else {
2855
                        gen_vfp_msr(tmp);
2856
                        gen_mov_vreg_F0(0, rn);
2857
                    }
2858
                }
2859
            }
2860
        } else {
2861
            /* data processing */
2862
            /* The opcode is in bits 23, 21, 20 and 6.  */
2863
            op = ((insn >> 20) & 8) | ((insn >> 19) & 6) | ((insn >> 6) & 1);
2864
            if (dp) {
2865
                if (op == 15) {
2866
                    /* rn is opcode */
2867
                    rn = ((insn >> 15) & 0x1e) | ((insn >> 7) & 1);
2868
                } else {
2869
                    /* rn is register number */
2870
                    VFP_DREG_N(rn, insn);
2871
                }
2872

    
2873
                if (op == 15 && (rn == 15 || rn > 17)) {
2874
                    /* Integer or single precision destination.  */
2875
                    rd = VFP_SREG_D(insn);
2876
                } else {
2877
                    VFP_DREG_D(rd, insn);
2878
                }
2879

    
2880
                if (op == 15 && (rn == 16 || rn == 17)) {
2881
                    /* Integer source.  */
2882
                    rm = ((insn << 1) & 0x1e) | ((insn >> 5) & 1);
2883
                } else {
2884
                    VFP_DREG_M(rm, insn);
2885
                }
2886
            } else {
2887
                rn = VFP_SREG_N(insn);
2888
                if (op == 15 && rn == 15) {
2889
                    /* Double precision destination.  */
2890
                    VFP_DREG_D(rd, insn);
2891
                } else {
2892
                    rd = VFP_SREG_D(insn);
2893
                }
2894
                rm = VFP_SREG_M(insn);
2895
            }
2896

    
2897
            veclen = env->vfp.vec_len;
2898
            if (op == 15 && rn > 3)
2899
                veclen = 0;
2900

    
2901
            /* Shut up compiler warnings.  */
2902
            delta_m = 0;
2903
            delta_d = 0;
2904
            bank_mask = 0;
2905

    
2906
            if (veclen > 0) {
2907
                if (dp)
2908
                    bank_mask = 0xc;
2909
                else
2910
                    bank_mask = 0x18;
2911

    
2912
                /* Figure out what type of vector operation this is.  */
2913
                if ((rd & bank_mask) == 0) {
2914
                    /* scalar */
2915
                    veclen = 0;
2916
                } else {
2917
                    if (dp)
2918
                        delta_d = (env->vfp.vec_stride >> 1) + 1;
2919
                    else
2920
                        delta_d = env->vfp.vec_stride + 1;
2921

    
2922
                    if ((rm & bank_mask) == 0) {
2923
                        /* mixed scalar/vector */
2924
                        delta_m = 0;
2925
                    } else {
2926
                        /* vector */
2927
                        delta_m = delta_d;
2928
                    }
2929
                }
2930
            }
2931

    
2932
            /* Load the initial operands.  */
2933
            if (op == 15) {
2934
                switch (rn) {
2935
                case 16:
2936
                case 17:
2937
                    /* Integer source */
2938
                    gen_mov_F0_vreg(0, rm);
2939
                    break;
2940
                case 8:
2941
                case 9:
2942
                    /* Compare */
2943
                    gen_mov_F0_vreg(dp, rd);
2944
                    gen_mov_F1_vreg(dp, rm);
2945
                    break;
2946
                case 10:
2947
                case 11:
2948
                    /* Compare with zero */
2949
                    gen_mov_F0_vreg(dp, rd);
2950
                    gen_vfp_F1_ld0(dp);
2951
                    break;
2952
                case 20:
2953
                case 21:
2954
                case 22:
2955
                case 23:
2956
                case 28:
2957
                case 29:
2958
                case 30:
2959
                case 31:
2960
                    /* Source and destination the same.  */
2961
                    gen_mov_F0_vreg(dp, rd);
2962
                    break;
2963
                default:
2964
                    /* One source operand.  */
2965
                    gen_mov_F0_vreg(dp, rm);
2966
                    break;
2967
                }
2968
            } else {
2969
                /* Two source operands.  */
2970
                gen_mov_F0_vreg(dp, rn);
2971
                gen_mov_F1_vreg(dp, rm);
2972
            }
2973

    
2974
            for (;;) {
2975
                /* Perform the calculation.  */
2976
                switch (op) {
2977
                case 0: /* mac: fd + (fn * fm) */
2978
                    gen_vfp_mul(dp);
2979
                    gen_mov_F1_vreg(dp, rd);
2980
                    gen_vfp_add(dp);
2981
                    break;
2982
                case 1: /* nmac: fd - (fn * fm) */
2983
                    gen_vfp_mul(dp);
2984
                    gen_vfp_neg(dp);
2985
                    gen_mov_F1_vreg(dp, rd);
2986
                    gen_vfp_add(dp);
2987
                    break;
2988
                case 2: /* msc: -fd + (fn * fm) */
2989
                    gen_vfp_mul(dp);
2990
                    gen_mov_F1_vreg(dp, rd);
2991
                    gen_vfp_sub(dp);
2992
                    break;
2993
                case 3: /* nmsc: -fd - (fn * fm)  */
2994
                    gen_vfp_mul(dp);
2995
                    gen_vfp_neg(dp);
2996
                    gen_mov_F1_vreg(dp, rd);
2997
                    gen_vfp_sub(dp);
2998
                    break;
2999
                case 4: /* mul: fn * fm */
3000
                    gen_vfp_mul(dp);
3001
                    break;
3002
                case 5: /* nmul: -(fn * fm) */
3003
                    gen_vfp_mul(dp);
3004
                    gen_vfp_neg(dp);
3005
                    break;
3006
                case 6: /* add: fn + fm */
3007
                    gen_vfp_add(dp);
3008
                    break;
3009
                case 7: /* sub: fn - fm */
3010
                    gen_vfp_sub(dp);
3011
                    break;
3012
                case 8: /* div: fn / fm */
3013
                    gen_vfp_div(dp);
3014
                    break;
3015
                case 14: /* fconst */
3016
                    if (!arm_feature(env, ARM_FEATURE_VFP3))
3017
                      return 1;
3018

    
3019
                    n = (insn << 12) & 0x80000000;
3020
                    i = ((insn >> 12) & 0x70) | (insn & 0xf);
3021
                    if (dp) {
3022
                        if (i & 0x40)
3023
                            i |= 0x3f80;
3024
                        else
3025
                            i |= 0x4000;
3026
                        n |= i << 16;
3027
                        tcg_gen_movi_i64(cpu_F0d, ((uint64_t)n) << 32);
3028
                    } else {
3029
                        if (i & 0x40)
3030
                            i |= 0x780;
3031
                        else
3032
                            i |= 0x800;
3033
                        n |= i << 19;
3034
                        tcg_gen_movi_i32(cpu_F0s, n);
3035
                    }
3036
                    break;
3037
                case 15: /* extension space */
3038
                    switch (rn) {
3039
                    case 0: /* cpy */
3040
                        /* no-op */
3041
                        break;
3042
                    case 1: /* abs */
3043
                        gen_vfp_abs(dp);
3044
                        break;
3045
                    case 2: /* neg */
3046
                        gen_vfp_neg(dp);
3047
                        break;
3048
                    case 3: /* sqrt */
3049
                        gen_vfp_sqrt(dp);
3050
                        break;
3051
                    case 4: /* vcvtb.f32.f16 */
3052
                        if (!arm_feature(env, ARM_FEATURE_VFP_FP16))
3053
                          return 1;
3054
                        tmp = gen_vfp_mrs();
3055
                        tcg_gen_ext16u_i32(tmp, tmp);
3056
                        gen_helper_vfp_fcvt_f16_to_f32(cpu_F0s, tmp, cpu_env);
3057
                        dead_tmp(tmp);
3058
                        break;
3059
                    case 5: /* vcvtt.f32.f16 */
3060
                        if (!arm_feature(env, ARM_FEATURE_VFP_FP16))
3061
                          return 1;
3062
                        tmp = gen_vfp_mrs();
3063
                        tcg_gen_shri_i32(tmp, tmp, 16);
3064
                        gen_helper_vfp_fcvt_f16_to_f32(cpu_F0s, tmp, cpu_env);
3065
                        dead_tmp(tmp);
3066
                        break;
3067
                    case 6: /* vcvtb.f16.f32 */
3068
                        if (!arm_feature(env, ARM_FEATURE_VFP_FP16))
3069
                          return 1;
3070
                        tmp = new_tmp();
3071
                        gen_helper_vfp_fcvt_f32_to_f16(tmp, cpu_F0s, cpu_env);
3072
                        gen_mov_F0_vreg(0, rd);
3073
                        tmp2 = gen_vfp_mrs();
3074
                        tcg_gen_andi_i32(tmp2, tmp2, 0xffff0000);
3075
                        tcg_gen_or_i32(tmp, tmp, tmp2);
3076
                        dead_tmp(tmp2);
3077
                        gen_vfp_msr(tmp);
3078
                        break;
3079
                    case 7: /* vcvtt.f16.f32 */
3080
                        if (!arm_feature(env, ARM_FEATURE_VFP_FP16))
3081
                          return 1;
3082
                        tmp = new_tmp();
3083
                        gen_helper_vfp_fcvt_f32_to_f16(tmp, cpu_F0s, cpu_env);
3084
                        tcg_gen_shli_i32(tmp, tmp, 16);
3085
                        gen_mov_F0_vreg(0, rd);
3086
                        tmp2 = gen_vfp_mrs();
3087
                        tcg_gen_ext16u_i32(tmp2, tmp2);
3088
                        tcg_gen_or_i32(tmp, tmp, tmp2);
3089
                        dead_tmp(tmp2);
3090
                        gen_vfp_msr(tmp);
3091
                        break;
3092
                    case 8: /* cmp */
3093
                        gen_vfp_cmp(dp);
3094
                        break;
3095
                    case 9: /* cmpe */
3096
                        gen_vfp_cmpe(dp);
3097
                        break;
3098
                    case 10: /* cmpz */
3099
                        gen_vfp_cmp(dp);
3100
                        break;
3101
                    case 11: /* cmpez */
3102
                        gen_vfp_F1_ld0(dp);
3103
                        gen_vfp_cmpe(dp);
3104
                        break;
3105
                    case 15: /* single<->double conversion */
3106
                        if (dp)
3107
                            gen_helper_vfp_fcvtsd(cpu_F0s, cpu_F0d, cpu_env);
3108
                        else
3109
                            gen_helper_vfp_fcvtds(cpu_F0d, cpu_F0s, cpu_env);
3110
                        break;
3111
                    case 16: /* fuito */
3112
                        gen_vfp_uito(dp);
3113
                        break;
3114
                    case 17: /* fsito */
3115
                        gen_vfp_sito(dp);
3116
                        break;
3117
                    case 20: /* fshto */
3118
                        if (!arm_feature(env, ARM_FEATURE_VFP3))
3119
                          return 1;
3120
                        gen_vfp_shto(dp, 16 - rm);
3121
                        break;
3122
                    case 21: /* fslto */
3123
                        if (!arm_feature(env, ARM_FEATURE_VFP3))
3124
                          return 1;
3125
                        gen_vfp_slto(dp, 32 - rm);
3126
                        break;
3127
                    case 22: /* fuhto */
3128
                        if (!arm_feature(env, ARM_FEATURE_VFP3))
3129
                          return 1;
3130
                        gen_vfp_uhto(dp, 16 - rm);
3131
                        break;
3132
                    case 23: /* fulto */
3133
                        if (!arm_feature(env, ARM_FEATURE_VFP3))
3134
                          return 1;
3135
                        gen_vfp_ulto(dp, 32 - rm);
3136
                        break;
3137
                    case 24: /* ftoui */
3138
                        gen_vfp_toui(dp);
3139
                        break;
3140
                    case 25: /* ftouiz */
3141
                        gen_vfp_touiz(dp);
3142
                        break;
3143
                    case 26: /* ftosi */
3144
                        gen_vfp_tosi(dp);
3145
                        break;
3146
                    case 27: /* ftosiz */
3147
                        gen_vfp_tosiz(dp);
3148
                        break;
3149
                    case 28: /* ftosh */
3150
                        if (!arm_feature(env, ARM_FEATURE_VFP3))
3151
                          return 1;
3152
                        gen_vfp_tosh(dp, 16 - rm);
3153
                        break;
3154
                    case 29: /* ftosl */
3155
                        if (!arm_feature(env, ARM_FEATURE_VFP3))
3156
                          return 1;
3157
                        gen_vfp_tosl(dp, 32 - rm);
3158
                        break;
3159
                    case 30: /* ftouh */
3160
                        if (!arm_feature(env, ARM_FEATURE_VFP3))
3161
                          return 1;
3162
                        gen_vfp_touh(dp, 16 - rm);
3163
                        break;
3164
                    case 31: /* ftoul */
3165
                        if (!arm_feature(env, ARM_FEATURE_VFP3))
3166
                          return 1;
3167
                        gen_vfp_toul(dp, 32 - rm);
3168
                        break;
3169
                    default: /* undefined */
3170
                        printf ("rn:%d\n", rn);
3171
                        return 1;
3172
                    }
3173
                    break;
3174
                default: /* undefined */
3175
                    printf ("op:%d\n", op);
3176
                    return 1;
3177
                }
3178

    
3179
                /* Write back the result.  */
3180
                if (op == 15 && (rn >= 8 && rn <= 11))
3181
                    ; /* Comparison, do nothing.  */
3182
                else if (op == 15 && rn > 17)
3183
                    /* Integer result.  */
3184
                    gen_mov_vreg_F0(0, rd);
3185
                else if (op == 15 && rn == 15)
3186
                    /* conversion */
3187
                    gen_mov_vreg_F0(!dp, rd);
3188
                else
3189
                    gen_mov_vreg_F0(dp, rd);
3190

    
3191
                /* break out of the loop if we have finished  */
3192
                if (veclen == 0)
3193
                    break;
3194

    
3195
                if (op == 15 && delta_m == 0) {
3196
                    /* single source one-many */
3197
                    while (veclen--) {
3198
                        rd = ((rd + delta_d) & (bank_mask - 1))
3199
                             | (rd & bank_mask);
3200
                        gen_mov_vreg_F0(dp, rd);
3201
                    }
3202
                    break;
3203
                }
3204
                /* Setup the next operands.  */
3205
                veclen--;
3206
                rd = ((rd + delta_d) & (bank_mask - 1))
3207
                     | (rd & bank_mask);
3208

    
3209
                if (op == 15) {
3210
                    /* One source operand.  */
3211
                    rm = ((rm + delta_m) & (bank_mask - 1))
3212
                         | (rm & bank_mask);
3213
                    gen_mov_F0_vreg(dp, rm);
3214
                } else {
3215
                    /* Two source operands.  */
3216
                    rn = ((rn + delta_d) & (bank_mask - 1))
3217
                         | (rn & bank_mask);
3218
                    gen_mov_F0_vreg(dp, rn);
3219
                    if (delta_m) {
3220
                        rm = ((rm + delta_m) & (bank_mask - 1))
3221
                             | (rm & bank_mask);
3222
                        gen_mov_F1_vreg(dp, rm);
3223
                    }
3224
                }
3225
            }
3226
        }
3227
        break;
3228
    case 0xc:
3229
    case 0xd:
3230
        if (dp && (insn & 0x03e00000) == 0x00400000) {
3231
            /* two-register transfer */
3232
            rn = (insn >> 16) & 0xf;
3233
            rd = (insn >> 12) & 0xf;
3234
            if (dp) {
3235
                VFP_DREG_M(rm, insn);
3236
            } else {
3237
                rm = VFP_SREG_M(insn);
3238
            }
3239

    
3240
            if (insn & ARM_CP_RW_BIT) {
3241
                /* vfp->arm */
3242
                if (dp) {
3243
                    gen_mov_F0_vreg(0, rm * 2);
3244
                    tmp = gen_vfp_mrs();
3245
                    store_reg(s, rd, tmp);
3246
                    gen_mov_F0_vreg(0, rm * 2 + 1);
3247
                    tmp = gen_vfp_mrs();
3248
                    store_reg(s, rn, tmp);
3249
                } else {
3250
                    gen_mov_F0_vreg(0, rm);
3251
                    tmp = gen_vfp_mrs();
3252
                    store_reg(s, rn, tmp);
3253
                    gen_mov_F0_vreg(0, rm + 1);
3254
                    tmp = gen_vfp_mrs();
3255
                    store_reg(s, rd, tmp);
3256
                }
3257
            } else {
3258
                /* arm->vfp */
3259
                if (dp) {
3260
                    tmp = load_reg(s, rd);
3261
                    gen_vfp_msr(tmp);
3262
                    gen_mov_vreg_F0(0, rm * 2);
3263
                    tmp = load_reg(s, rn);
3264
                    gen_vfp_msr(tmp);
3265
                    gen_mov_vreg_F0(0, rm * 2 + 1);
3266
                } else {
3267
                    tmp = load_reg(s, rn);
3268
                    gen_vfp_msr(tmp);
3269
                    gen_mov_vreg_F0(0, rm);
3270
                    tmp = load_reg(s, rd);
3271
                    gen_vfp_msr(tmp);
3272
                    gen_mov_vreg_F0(0, rm + 1);
3273
                }
3274
            }
3275
        } else {
3276
            /* Load/store */
3277
            rn = (insn >> 16) & 0xf;
3278
            if (dp)
3279
                VFP_DREG_D(rd, insn);
3280
            else
3281
                rd = VFP_SREG_D(insn);
3282
            if (s->thumb && rn == 15) {
3283
                addr = new_tmp();
3284
                tcg_gen_movi_i32(addr, s->pc & ~2);
3285
            } else {
3286
                addr = load_reg(s, rn);
3287
            }
3288
            if ((insn & 0x01200000) == 0x01000000) {
3289
                /* Single load/store */
3290
                offset = (insn & 0xff) << 2;
3291
                if ((insn & (1 << 23)) == 0)
3292
                    offset = -offset;
3293
                tcg_gen_addi_i32(addr, addr, offset);
3294
                if (insn & (1 << 20)) {
3295
                    gen_vfp_ld(s, dp, addr);
3296
                    gen_mov_vreg_F0(dp, rd);
3297
                } else {
3298
                    gen_mov_F0_vreg(dp, rd);
3299
                    gen_vfp_st(s, dp, addr);
3300
                }
3301
                dead_tmp(addr);
3302
            } else {
3303
                /* load/store multiple */
3304
                if (dp)
3305
                    n = (insn >> 1) & 0x7f;
3306
                else
3307
                    n = insn & 0xff;
3308

    
3309
                if (insn & (1 << 24)) /* pre-decrement */
3310
                    tcg_gen_addi_i32(addr, addr, -((insn & 0xff) << 2));
3311

    
3312
                if (dp)
3313
                    offset = 8;
3314
                else
3315
                    offset = 4;
3316
                for (i = 0; i < n; i++) {
3317
                    if (insn & ARM_CP_RW_BIT) {
3318
                        /* load */
3319
                        gen_vfp_ld(s, dp, addr);
3320
                        gen_mov_vreg_F0(dp, rd + i);
3321
                    } else {
3322
                        /* store */
3323
                        gen_mov_F0_vreg(dp, rd + i);
3324
                        gen_vfp_st(s, dp, addr);
3325
                    }
3326
                    tcg_gen_addi_i32(addr, addr, offset);
3327
                }
3328
                if (insn & (1 << 21)) {
3329
                    /* writeback */
3330
                    if (insn & (1 << 24))
3331
                        offset = -offset * n;
3332
                    else if (dp && (insn & 1))
3333
                        offset = 4;
3334
                    else
3335
                        offset = 0;
3336

    
3337
                    if (offset != 0)
3338
                        tcg_gen_addi_i32(addr, addr, offset);
3339
                    store_reg(s, rn, addr);
3340
                } else {
3341
                    dead_tmp(addr);
3342
                }
3343
            }
3344
        }
3345
        break;
3346
    default:
3347
        /* Should never happen.  */
3348
        return 1;
3349
    }
3350
    return 0;
3351
}
3352

    
3353
static inline void gen_goto_tb(DisasContext *s, int n, uint32_t dest)
3354
{
3355
    TranslationBlock *tb;
3356

    
3357
    tb = s->tb;
3358
    if ((tb->pc & TARGET_PAGE_MASK) == (dest & TARGET_PAGE_MASK)) {
3359
        tcg_gen_goto_tb(n);
3360
        gen_set_pc_im(dest);
3361
        tcg_gen_exit_tb((long)tb + n);
3362
    } else {
3363
        gen_set_pc_im(dest);
3364
        tcg_gen_exit_tb(0);
3365
    }
3366
}
3367

    
3368
static inline void gen_jmp (DisasContext *s, uint32_t dest)
3369
{
3370
    if (unlikely(s->singlestep_enabled)) {
3371
        /* An indirect jump so that we still trigger the debug exception.  */
3372
        if (s->thumb)
3373
            dest |= 1;
3374
        gen_bx_im(s, dest);
3375
    } else {
3376
        gen_goto_tb(s, 0, dest);
3377
        s->is_jmp = DISAS_TB_JUMP;
3378
    }
3379
}
3380

    
3381
static inline void gen_mulxy(TCGv t0, TCGv t1, int x, int y)
3382
{
3383
    if (x)
3384
        tcg_gen_sari_i32(t0, t0, 16);
3385
    else
3386
        gen_sxth(t0);
3387
    if (y)
3388
        tcg_gen_sari_i32(t1, t1, 16);
3389
    else
3390
        gen_sxth(t1);
3391
    tcg_gen_mul_i32(t0, t0, t1);
3392
}
3393

    
3394
/* Return the mask of PSR bits set by a MSR instruction.  */
3395
static uint32_t msr_mask(CPUState *env, DisasContext *s, int flags, int spsr) {
3396
    uint32_t mask;
3397

    
3398
    mask = 0;
3399
    if (flags & (1 << 0))
3400
        mask |= 0xff;
3401
    if (flags & (1 << 1))
3402
        mask |= 0xff00;
3403
    if (flags & (1 << 2))
3404
        mask |= 0xff0000;
3405
    if (flags & (1 << 3))
3406
        mask |= 0xff000000;
3407

    
3408
    /* Mask out undefined bits.  */
3409
    mask &= ~CPSR_RESERVED;
3410
    if (!arm_feature(env, ARM_FEATURE_V6))
3411
        mask &= ~(CPSR_E | CPSR_GE);
3412
    if (!arm_feature(env, ARM_FEATURE_THUMB2))
3413
        mask &= ~CPSR_IT;
3414
    /* Mask out execution state bits.  */
3415
    if (!spsr)
3416
        mask &= ~CPSR_EXEC;
3417
    /* Mask out privileged bits.  */
3418
    if (IS_USER(s))
3419
        mask &= CPSR_USER;
3420
    return mask;
3421
}
3422

    
3423
/* Returns nonzero if access to the PSR is not permitted. Marks t0 as dead. */
3424
static int gen_set_psr(DisasContext *s, uint32_t mask, int spsr, TCGv t0)
3425
{
3426
    TCGv tmp;
3427
    if (spsr) {
3428
        /* ??? This is also undefined in system mode.  */
3429
        if (IS_USER(s))
3430
            return 1;
3431

    
3432
        tmp = load_cpu_field(spsr);
3433
        tcg_gen_andi_i32(tmp, tmp, ~mask);
3434
        tcg_gen_andi_i32(t0, t0, mask);
3435
        tcg_gen_or_i32(tmp, tmp, t0);
3436
        store_cpu_field(tmp, spsr);
3437
    } else {
3438
        gen_set_cpsr(t0, mask);
3439
    }
3440
    dead_tmp(t0);
3441
    gen_lookup_tb(s);
3442
    return 0;
3443
}
3444

    
3445
/* Returns nonzero if access to the PSR is not permitted.  */
3446
static int gen_set_psr_im(DisasContext *s, uint32_t mask, int spsr, uint32_t val)
3447
{
3448
    TCGv tmp;
3449
    tmp = new_tmp();
3450
    tcg_gen_movi_i32(tmp, val);
3451
    return gen_set_psr(s, mask, spsr, tmp);
3452
}
3453

    
3454
/* Generate an old-style exception return. Marks pc as dead. */
3455
static void gen_exception_return(DisasContext *s, TCGv pc)
3456
{
3457
    TCGv tmp;
3458
    store_reg(s, 15, pc);
3459
    tmp = load_cpu_field(spsr);
3460
    gen_set_cpsr(tmp, 0xffffffff);
3461
    dead_tmp(tmp);
3462
    s->is_jmp = DISAS_UPDATE;
3463
}
3464

    
3465
/* Generate a v6 exception return.  Marks both values as dead.  */
3466
static void gen_rfe(DisasContext *s, TCGv pc, TCGv cpsr)
3467
{
3468
    gen_set_cpsr(cpsr, 0xffffffff);
3469
    dead_tmp(cpsr);
3470
    store_reg(s, 15, pc);
3471
    s->is_jmp = DISAS_UPDATE;
3472
}
3473

    
3474
static inline void
3475
gen_set_condexec (DisasContext *s)
3476
{
3477
    if (s->condexec_mask) {
3478
        uint32_t val = (s->condexec_cond << 4) | (s->condexec_mask >> 1);
3479
        TCGv tmp = new_tmp();
3480
        tcg_gen_movi_i32(tmp, val);
3481
        store_cpu_field(tmp, condexec_bits);
3482
    }
3483
}
3484

    
3485
static void gen_nop_hint(DisasContext *s, int val)
3486
{
3487
    switch (val) {
3488
    case 3: /* wfi */
3489
        gen_set_pc_im(s->pc);
3490
        s->is_jmp = DISAS_WFI;
3491
        break;
3492
    case 2: /* wfe */
3493
    case 4: /* sev */
3494
        /* TODO: Implement SEV and WFE.  May help SMP performance.  */
3495
    default: /* nop */
3496
        break;
3497
    }
3498
}
3499

    
3500
#define CPU_V001 cpu_V0, cpu_V0, cpu_V1
3501

    
3502
static inline int gen_neon_add(int size, TCGv t0, TCGv t1)
3503
{
3504
    switch (size) {
3505
    case 0: gen_helper_neon_add_u8(t0, t0, t1); break;
3506
    case 1: gen_helper_neon_add_u16(t0, t0, t1); break;
3507
    case 2: tcg_gen_add_i32(t0, t0, t1); break;
3508
    default: return 1;
3509
    }
3510
    return 0;
3511
}
3512

    
3513
static inline void gen_neon_rsb(int size, TCGv t0, TCGv t1)
3514
{
3515
    switch (size) {
3516
    case 0: gen_helper_neon_sub_u8(t0, t1, t0); break;
3517
    case 1: gen_helper_neon_sub_u16(t0, t1, t0); break;
3518
    case 2: tcg_gen_sub_i32(t0, t1, t0); break;
3519
    default: return;
3520
    }
3521
}
3522

    
3523
/* 32-bit pairwise ops end up the same as the elementwise versions.  */
3524
#define gen_helper_neon_pmax_s32  gen_helper_neon_max_s32
3525
#define gen_helper_neon_pmax_u32  gen_helper_neon_max_u32
3526
#define gen_helper_neon_pmin_s32  gen_helper_neon_min_s32
3527
#define gen_helper_neon_pmin_u32  gen_helper_neon_min_u32
3528

    
3529
/* FIXME: This is wrong.  They set the wrong overflow bit.  */
3530
#define gen_helper_neon_qadd_s32(a, e, b, c) gen_helper_add_saturate(a, b, c)
3531
#define gen_helper_neon_qadd_u32(a, e, b, c) gen_helper_add_usaturate(a, b, c)
3532
#define gen_helper_neon_qsub_s32(a, e, b, c) gen_helper_sub_saturate(a, b, c)
3533
#define gen_helper_neon_qsub_u32(a, e, b, c) gen_helper_sub_usaturate(a, b, c)
3534

    
3535
#define GEN_NEON_INTEGER_OP_ENV(name) do { \
3536
    switch ((size << 1) | u) { \
3537
    case 0: \
3538
        gen_helper_neon_##name##_s8(tmp, cpu_env, tmp, tmp2); \
3539
        break; \
3540
    case 1: \
3541
        gen_helper_neon_##name##_u8(tmp, cpu_env, tmp, tmp2); \
3542
        break; \
3543
    case 2: \
3544
        gen_helper_neon_##name##_s16(tmp, cpu_env, tmp, tmp2); \
3545
        break; \
3546
    case 3: \
3547
        gen_helper_neon_##name##_u16(tmp, cpu_env, tmp, tmp2); \
3548
        break; \
3549
    case 4: \
3550
        gen_helper_neon_##name##_s32(tmp, cpu_env, tmp, tmp2); \
3551
        break; \
3552
    case 5: \
3553
        gen_helper_neon_##name##_u32(tmp, cpu_env, tmp, tmp2); \
3554
        break; \
3555
    default: return 1; \
3556
    }} while (0)
3557

    
3558
#define GEN_NEON_INTEGER_OP(name) do { \
3559
    switch ((size << 1) | u) { \
3560
    case 0: \
3561
        gen_helper_neon_##name##_s8(tmp, tmp, tmp2); \
3562
        break; \
3563
    case 1: \
3564
        gen_helper_neon_##name##_u8(tmp, tmp, tmp2); \
3565
        break; \
3566
    case 2: \
3567
        gen_helper_neon_##name##_s16(tmp, tmp, tmp2); \
3568
        break; \
3569
    case 3: \
3570
        gen_helper_neon_##name##_u16(tmp, tmp, tmp2); \
3571
        break; \
3572
    case 4: \
3573
        gen_helper_neon_##name##_s32(tmp, tmp, tmp2); \
3574
        break; \
3575
    case 5: \
3576
        gen_helper_neon_##name##_u32(tmp, tmp, tmp2); \
3577
        break; \
3578
    default: return 1; \
3579
    }} while (0)
3580

    
3581
static TCGv neon_load_scratch(int scratch)
3582
{
3583
    TCGv tmp = new_tmp();
3584
    tcg_gen_ld_i32(tmp, cpu_env, offsetof(CPUARMState, vfp.scratch[scratch]));
3585
    return tmp;
3586
}
3587

    
3588
static void neon_store_scratch(int scratch, TCGv var)
3589
{
3590
    tcg_gen_st_i32(var, cpu_env, offsetof(CPUARMState, vfp.scratch[scratch]));
3591
    dead_tmp(var);
3592
}
3593

    
3594
static inline TCGv neon_get_scalar(int size, int reg)
3595
{
3596
    TCGv tmp;
3597
    if (size == 1) {
3598
        tmp = neon_load_reg(reg >> 1, reg & 1);
3599
    } else {
3600
        tmp = neon_load_reg(reg >> 2, (reg >> 1) & 1);
3601
        if (reg & 1) {
3602
            gen_neon_dup_low16(tmp);
3603
        } else {
3604
            gen_neon_dup_high16(tmp);
3605
        }
3606
    }
3607
    return tmp;
3608
}
3609

    
3610
static void gen_neon_unzip_u8(TCGv t0, TCGv t1)
3611
{
3612
    TCGv rd, rm, tmp;
3613

    
3614
    rd = new_tmp();
3615
    rm = new_tmp();
3616
    tmp = new_tmp();
3617

    
3618
    tcg_gen_andi_i32(rd, t0, 0xff);
3619
    tcg_gen_shri_i32(tmp, t0, 8);
3620
    tcg_gen_andi_i32(tmp, tmp, 0xff00);
3621
    tcg_gen_or_i32(rd, rd, tmp);
3622
    tcg_gen_shli_i32(tmp, t1, 16);
3623
    tcg_gen_andi_i32(tmp, tmp, 0xff0000);
3624
    tcg_gen_or_i32(rd, rd, tmp);
3625
    tcg_gen_shli_i32(tmp, t1, 8);
3626
    tcg_gen_andi_i32(tmp, tmp, 0xff000000);
3627
    tcg_gen_or_i32(rd, rd, tmp);
3628

    
3629
    tcg_gen_shri_i32(rm, t0, 8);
3630
    tcg_gen_andi_i32(rm, rm, 0xff);
3631
    tcg_gen_shri_i32(tmp, t0, 16);
3632
    tcg_gen_andi_i32(tmp, tmp, 0xff00);
3633
    tcg_gen_or_i32(rm, rm, tmp);
3634
    tcg_gen_shli_i32(tmp, t1, 8);
3635
    tcg_gen_andi_i32(tmp, tmp, 0xff0000);
3636
    tcg_gen_or_i32(rm, rm, tmp);
3637
    tcg_gen_andi_i32(tmp, t1, 0xff000000);
3638
    tcg_gen_or_i32(t1, rm, tmp);
3639
    tcg_gen_mov_i32(t0, rd);
3640

    
3641
    dead_tmp(tmp);
3642
    dead_tmp(rm);
3643
    dead_tmp(rd);
3644
}
3645

    
3646
static void gen_neon_zip_u8(TCGv t0, TCGv t1)
3647
{
3648
    TCGv rd, rm, tmp;
3649

    
3650
    rd = new_tmp();
3651
    rm = new_tmp();
3652
    tmp = new_tmp();
3653

    
3654
    tcg_gen_andi_i32(rd, t0, 0xff);
3655
    tcg_gen_shli_i32(tmp, t1, 8);
3656
    tcg_gen_andi_i32(tmp, tmp, 0xff00);
3657
    tcg_gen_or_i32(rd, rd, tmp);
3658
    tcg_gen_shli_i32(tmp, t0, 16);
3659
    tcg_gen_andi_i32(tmp, tmp, 0xff0000);
3660
    tcg_gen_or_i32(rd, rd, tmp);
3661
    tcg_gen_shli_i32(tmp, t1, 24);
3662
    tcg_gen_andi_i32(tmp, tmp, 0xff000000);
3663
    tcg_gen_or_i32(rd, rd, tmp);
3664

    
3665
    tcg_gen_andi_i32(rm, t1, 0xff000000);
3666
    tcg_gen_shri_i32(tmp, t0, 8);
3667
    tcg_gen_andi_i32(tmp, tmp, 0xff0000);
3668
    tcg_gen_or_i32(rm, rm, tmp);
3669
    tcg_gen_shri_i32(tmp, t1, 8);
3670
    tcg_gen_andi_i32(tmp, tmp, 0xff00);
3671
    tcg_gen_or_i32(rm, rm, tmp);
3672
    tcg_gen_shri_i32(tmp, t0, 16);
3673
    tcg_gen_andi_i32(tmp, tmp, 0xff);
3674
    tcg_gen_or_i32(t1, rm, tmp);
3675
    tcg_gen_mov_i32(t0, rd);
3676

    
3677
    dead_tmp(tmp);
3678
    dead_tmp(rm);
3679
    dead_tmp(rd);
3680
}
3681

    
3682
static void gen_neon_zip_u16(TCGv t0, TCGv t1)
3683
{
3684
    TCGv tmp, tmp2;
3685

    
3686
    tmp = new_tmp();
3687
    tmp2 = new_tmp();
3688

    
3689
    tcg_gen_andi_i32(tmp, t0, 0xffff);
3690
    tcg_gen_shli_i32(tmp2, t1, 16);
3691
    tcg_gen_or_i32(tmp, tmp, tmp2);
3692
    tcg_gen_andi_i32(t1, t1, 0xffff0000);
3693
    tcg_gen_shri_i32(tmp2, t0, 16);
3694
    tcg_gen_or_i32(t1, t1, tmp2);
3695
    tcg_gen_mov_i32(t0, tmp);
3696

    
3697
    dead_tmp(tmp2);
3698
    dead_tmp(tmp);
3699
}
3700

    
3701
static void gen_neon_unzip(int reg, int q, int tmp, int size)
3702
{
3703
    int n;
3704
    TCGv t0, t1;
3705

    
3706
    for (n = 0; n < q + 1; n += 2) {
3707
        t0 = neon_load_reg(reg, n);
3708
        t1 = neon_load_reg(reg, n + 1);
3709
        switch (size) {
3710
        case 0: gen_neon_unzip_u8(t0, t1); break;
3711
        case 1: gen_neon_zip_u16(t0, t1); break; /* zip and unzip are the same.  */
3712
        case 2: /* no-op */; break;
3713
        default: abort();
3714
        }
3715
        neon_store_scratch(tmp + n, t0);
3716
        neon_store_scratch(tmp + n + 1, t1);
3717
    }
3718
}
3719

    
3720
static void gen_neon_trn_u8(TCGv t0, TCGv t1)
3721
{
3722
    TCGv rd, tmp;
3723

    
3724
    rd = new_tmp();
3725
    tmp = new_tmp();
3726

    
3727
    tcg_gen_shli_i32(rd, t0, 8);
3728
    tcg_gen_andi_i32(rd, rd, 0xff00ff00);
3729
    tcg_gen_andi_i32(tmp, t1, 0x00ff00ff);
3730
    tcg_gen_or_i32(rd, rd, tmp);
3731

    
3732
    tcg_gen_shri_i32(t1, t1, 8);
3733
    tcg_gen_andi_i32(t1, t1, 0x00ff00ff);
3734
    tcg_gen_andi_i32(tmp, t0, 0xff00ff00);
3735
    tcg_gen_or_i32(t1, t1, tmp);
3736
    tcg_gen_mov_i32(t0, rd);
3737

    
3738
    dead_tmp(tmp);
3739
    dead_tmp(rd);
3740
}
3741

    
3742
static void gen_neon_trn_u16(TCGv t0, TCGv t1)
3743
{
3744
    TCGv rd, tmp;
3745

    
3746
    rd = new_tmp();
3747
    tmp = new_tmp();
3748

    
3749
    tcg_gen_shli_i32(rd, t0, 16);
3750
    tcg_gen_andi_i32(tmp, t1, 0xffff);
3751
    tcg_gen_or_i32(rd, rd, tmp);
3752
    tcg_gen_shri_i32(t1, t1, 16);
3753
    tcg_gen_andi_i32(tmp, t0, 0xffff0000);
3754
    tcg_gen_or_i32(t1, t1, tmp);
3755
    tcg_gen_mov_i32(t0, rd);
3756

    
3757
    dead_tmp(tmp);
3758
    dead_tmp(rd);
3759
}
3760

    
3761

    
3762
static struct {
3763
    int nregs;
3764
    int interleave;
3765
    int spacing;
3766
} neon_ls_element_type[11] = {
3767
    {4, 4, 1},
3768
    {4, 4, 2},
3769
    {4, 1, 1},
3770
    {4, 2, 1},
3771
    {3, 3, 1},
3772
    {3, 3, 2},
3773
    {3, 1, 1},
3774
    {1, 1, 1},
3775
    {2, 2, 1},
3776
    {2, 2, 2},
3777
    {2, 1, 1}
3778
};
3779

    
3780
/* Translate a NEON load/store element instruction.  Return nonzero if the
3781
   instruction is invalid.  */
3782
static int disas_neon_ls_insn(CPUState * env, DisasContext *s, uint32_t insn)
3783
{
3784
    int rd, rn, rm;
3785
    int op;
3786
    int nregs;
3787
    int interleave;
3788
    int spacing;
3789
    int stride;
3790
    int size;
3791
    int reg;
3792
    int pass;
3793
    int load;
3794
    int shift;
3795
    int n;
3796
    TCGv addr;
3797
    TCGv tmp;
3798
    TCGv tmp2;
3799
    TCGv_i64 tmp64;
3800

    
3801
    if (!vfp_enabled(env))
3802
      return 1;
3803
    VFP_DREG_D(rd, insn);
3804
    rn = (insn >> 16) & 0xf;
3805
    rm = insn & 0xf;
3806
    load = (insn & (1 << 21)) != 0;
3807
    addr = new_tmp();
3808
    if ((insn & (1 << 23)) == 0) {
3809
        /* Load store all elements.  */
3810
        op = (insn >> 8) & 0xf;
3811
        size = (insn >> 6) & 3;
3812
        if (op > 10)
3813
            return 1;
3814
        nregs = neon_ls_element_type[op].nregs;
3815
        interleave = neon_ls_element_type[op].interleave;
3816
        spacing = neon_ls_element_type[op].spacing;
3817
        if (size == 3 && (interleave | spacing) != 1)
3818
            return 1;
3819
        load_reg_var(s, addr, rn);
3820
        stride = (1 << size) * interleave;
3821
        for (reg = 0; reg < nregs; reg++) {
3822
            if (interleave > 2 || (interleave == 2 && nregs == 2)) {
3823
                load_reg_var(s, addr, rn);
3824
                tcg_gen_addi_i32(addr, addr, (1 << size) * reg);
3825
            } else if (interleave == 2 && nregs == 4 && reg == 2) {
3826
                load_reg_var(s, addr, rn);
3827
                tcg_gen_addi_i32(addr, addr, 1 << size);
3828
            }
3829
            if (size == 3) {
3830
                if (load) {
3831
                    tmp64 = gen_ld64(addr, IS_USER(s));
3832
                    neon_store_reg64(tmp64, rd);
3833
                    tcg_temp_free_i64(tmp64);
3834
                } else {
3835
                    tmp64 = tcg_temp_new_i64();
3836
                    neon_load_reg64(tmp64, rd);
3837
                    gen_st64(tmp64, addr, IS_USER(s));
3838
                }
3839
                tcg_gen_addi_i32(addr, addr, stride);
3840
            } else {
3841
                for (pass = 0; pass < 2; pass++) {
3842
                    if (size == 2) {
3843
                        if (load) {
3844
                            tmp = gen_ld32(addr, IS_USER(s));
3845
                            neon_store_reg(rd, pass, tmp);
3846
                        } else {
3847
                            tmp = neon_load_reg(rd, pass);
3848
                            gen_st32(tmp, addr, IS_USER(s));
3849
                        }
3850
                        tcg_gen_addi_i32(addr, addr, stride);
3851
                    } else if (size == 1) {
3852
                        if (load) {
3853
                            tmp = gen_ld16u(addr, IS_USER(s));
3854
                            tcg_gen_addi_i32(addr, addr, stride);
3855
                            tmp2 = gen_ld16u(addr, IS_USER(s));
3856
                            tcg_gen_addi_i32(addr, addr, stride);
3857
                            gen_bfi(tmp, tmp, tmp2, 16, 0xffff);
3858
                            dead_tmp(tmp2);
3859
                            neon_store_reg(rd, pass, tmp);
3860
                        } else {
3861
                            tmp = neon_load_reg(rd, pass);
3862
                            tmp2 = new_tmp();
3863
                            tcg_gen_shri_i32(tmp2, tmp, 16);
3864
                            gen_st16(tmp, addr, IS_USER(s));
3865
                            tcg_gen_addi_i32(addr, addr, stride);
3866
                            gen_st16(tmp2, addr, IS_USER(s));
3867
                            tcg_gen_addi_i32(addr, addr, stride);
3868
                        }
3869
                    } else /* size == 0 */ {
3870
                        if (load) {
3871
                            TCGV_UNUSED(tmp2);
3872
                            for (n = 0; n < 4; n++) {
3873
                                tmp = gen_ld8u(addr, IS_USER(s));
3874
                                tcg_gen_addi_i32(addr, addr, stride);
3875
                                if (n == 0) {
3876
                                    tmp2 = tmp;
3877
                                } else {
3878
                                    gen_bfi(tmp2, tmp2, tmp, n * 8, 0xff);
3879
                                    dead_tmp(tmp);
3880
                                }
3881
                            }
3882
                            neon_store_reg(rd, pass, tmp2);
3883
                        } else {
3884
                            tmp2 = neon_load_reg(rd, pass);
3885
                            for (n = 0; n < 4; n++) {
3886
                                tmp = new_tmp();
3887
                                if (n == 0) {
3888
                                    tcg_gen_mov_i32(tmp, tmp2);
3889
                                } else {
3890
                                    tcg_gen_shri_i32(tmp, tmp2, n * 8);
3891
                                }
3892
                                gen_st8(tmp, addr, IS_USER(s));
3893
                                tcg_gen_addi_i32(addr, addr, stride);
3894
                            }
3895
                            dead_tmp(tmp2);
3896
                        }
3897
                    }
3898
                }
3899
            }
3900
            rd += spacing;
3901
        }
3902
        stride = nregs * 8;
3903
    } else {
3904
        size = (insn >> 10) & 3;
3905
        if (size == 3) {
3906
            /* Load single element to all lanes.  */
3907
            if (!load)
3908
                return 1;
3909
            size = (insn >> 6) & 3;
3910
            nregs = ((insn >> 8) & 3) + 1;
3911
            stride = (insn & (1 << 5)) ? 2 : 1;
3912
            load_reg_var(s, addr, rn);
3913
            for (reg = 0; reg < nregs; reg++) {
3914
                switch (size) {
3915
                case 0:
3916
                    tmp = gen_ld8u(addr, IS_USER(s));
3917
                    gen_neon_dup_u8(tmp, 0);
3918
                    break;
3919
                case 1:
3920
                    tmp = gen_ld16u(addr, IS_USER(s));
3921
                    gen_neon_dup_low16(tmp);
3922
                    break;
3923
                case 2:
3924
                    tmp = gen_ld32(addr, IS_USER(s));
3925
                    break;
3926
                case 3:
3927
                    return 1;
3928
                default: /* Avoid compiler warnings.  */
3929
                    abort();
3930
                }
3931
                tcg_gen_addi_i32(addr, addr, 1 << size);
3932
                tmp2 = new_tmp();
3933
                tcg_gen_mov_i32(tmp2, tmp);
3934
                neon_store_reg(rd, 0, tmp2);
3935
                neon_store_reg(rd, 1, tmp);
3936
                rd += stride;
3937
            }
3938
            stride = (1 << size) * nregs;
3939
        } else {
3940
            /* Single element.  */
3941
            pass = (insn >> 7) & 1;
3942
            switch (size) {
3943
            case 0:
3944
                shift = ((insn >> 5) & 3) * 8;
3945
                stride = 1;
3946
                break;
3947
            case 1:
3948
                shift = ((insn >> 6) & 1) * 16;
3949
                stride = (insn & (1 << 5)) ? 2 : 1;
3950
                break;
3951
            case 2:
3952
                shift = 0;
3953
                stride = (insn & (1 << 6)) ? 2 : 1;
3954
                break;
3955
            default:
3956
                abort();
3957
            }
3958
            nregs = ((insn >> 8) & 3) + 1;
3959
            load_reg_var(s, addr, rn);
3960
            for (reg = 0; reg < nregs; reg++) {
3961
                if (load) {
3962
                    switch (size) {
3963
                    case 0:
3964
                        tmp = gen_ld8u(addr, IS_USER(s));
3965
                        break;
3966
                    case 1:
3967
                        tmp = gen_ld16u(addr, IS_USER(s));
3968
                        break;
3969
                    case 2:
3970
                        tmp = gen_ld32(addr, IS_USER(s));
3971
                        break;
3972
                    default: /* Avoid compiler warnings.  */
3973
                        abort();
3974
                    }
3975
                    if (size != 2) {
3976
                        tmp2 = neon_load_reg(rd, pass);
3977
                        gen_bfi(tmp, tmp2, tmp, shift, size ? 0xffff : 0xff);
3978
                        dead_tmp(tmp2);
3979
                    }
3980
                    neon_store_reg(rd, pass, tmp);
3981
                } else { /* Store */
3982
                    tmp = neon_load_reg(rd, pass);
3983
                    if (shift)
3984
                        tcg_gen_shri_i32(tmp, tmp, shift);
3985
                    switch (size) {
3986
                    case 0:
3987
                        gen_st8(tmp, addr, IS_USER(s));
3988
                        break;
3989
                    case 1:
3990
                        gen_st16(tmp, addr, IS_USER(s));
3991
                        break;
3992
                    case 2:
3993
                        gen_st32(tmp, addr, IS_USER(s));
3994
                        break;
3995
                    }
3996
                }
3997
                rd += stride;
3998
                tcg_gen_addi_i32(addr, addr, 1 << size);
3999
            }
4000
            stride = nregs * (1 << size);
4001
        }
4002
    }
4003
    dead_tmp(addr);
4004
    if (rm != 15) {
4005
        TCGv base;
4006

    
4007
        base = load_reg(s, rn);
4008
        if (rm == 13) {
4009
            tcg_gen_addi_i32(base, base, stride);
4010
        } else {
4011
            TCGv index;
4012
            index = load_reg(s, rm);
4013
            tcg_gen_add_i32(base, base, index);
4014
            dead_tmp(index);
4015
        }
4016
        store_reg(s, rn, base);
4017
    }
4018
    return 0;
4019
}
4020

    
4021
/* Bitwise select.  dest = c ? t : f.  Clobbers T and F.  */
4022
static void gen_neon_bsl(TCGv dest, TCGv t, TCGv f, TCGv c)
4023
{
4024
    tcg_gen_and_i32(t, t, c);
4025
    tcg_gen_andc_i32(f, f, c);
4026
    tcg_gen_or_i32(dest, t, f);
4027
}
4028

    
4029
static inline void gen_neon_narrow(int size, TCGv dest, TCGv_i64 src)
4030
{
4031
    switch (size) {
4032
    case 0: gen_helper_neon_narrow_u8(dest, src); break;
4033
    case 1: gen_helper_neon_narrow_u16(dest, src); break;
4034
    case 2: tcg_gen_trunc_i64_i32(dest, src); break;
4035
    default: abort();
4036
    }
4037
}
4038

    
4039
static inline void gen_neon_narrow_sats(int size, TCGv dest, TCGv_i64 src)
4040
{
4041
    switch (size) {
4042
    case 0: gen_helper_neon_narrow_sat_s8(dest, cpu_env, src); break;
4043
    case 1: gen_helper_neon_narrow_sat_s16(dest, cpu_env, src); break;
4044
    case 2: gen_helper_neon_narrow_sat_s32(dest, cpu_env, src); break;
4045
    default: abort();
4046
    }
4047
}
4048

    
4049
static inline void gen_neon_narrow_satu(int size, TCGv dest, TCGv_i64 src)
4050
{
4051
    switch (size) {
4052
    case 0: gen_helper_neon_narrow_sat_u8(dest, cpu_env, src); break;
4053
    case 1: gen_helper_neon_narrow_sat_u16(dest, cpu_env, src); break;
4054
    case 2: gen_helper_neon_narrow_sat_u32(dest, cpu_env, src); break;
4055
    default: abort();
4056
    }
4057
}
4058

    
4059
static inline void gen_neon_shift_narrow(int size, TCGv var, TCGv shift,
4060
                                         int q, int u)
4061
{
4062
    if (q) {
4063
        if (u) {
4064
            switch (size) {
4065
            case 1: gen_helper_neon_rshl_u16(var, var, shift); break;
4066
            case 2: gen_helper_neon_rshl_u32(var, var, shift); break;
4067
            default: abort();
4068
            }
4069
        } else {
4070
            switch (size) {
4071
            case 1: gen_helper_neon_rshl_s16(var, var, shift); break;
4072
            case 2: gen_helper_neon_rshl_s32(var, var, shift); break;
4073
            default: abort();
4074
            }
4075
        }
4076
    } else {
4077
        if (u) {
4078
            switch (size) {
4079
            case 1: gen_helper_neon_rshl_u16(var, var, shift); break;
4080
            case 2: gen_helper_neon_rshl_u32(var, var, shift); break;
4081
            default: abort();
4082
            }
4083
        } else {
4084
            switch (size) {
4085
            case 1: gen_helper_neon_shl_s16(var, var, shift); break;
4086
            case 2: gen_helper_neon_shl_s32(var, var, shift); break;
4087
            default: abort();
4088
            }
4089
        }
4090
    }
4091
}
4092

    
4093
static inline void gen_neon_widen(TCGv_i64 dest, TCGv src, int size, int u)
4094
{
4095
    if (u) {
4096
        switch (size) {
4097
        case 0: gen_helper_neon_widen_u8(dest, src); break;
4098
        case 1: gen_helper_neon_widen_u16(dest, src); break;
4099
        case 2: tcg_gen_extu_i32_i64(dest, src); break;
4100
        default: abort();
4101
        }
4102
    } else {
4103
        switch (size) {
4104
        case 0: gen_helper_neon_widen_s8(dest, src); break;
4105
        case 1: gen_helper_neon_widen_s16(dest, src); break;
4106
        case 2: tcg_gen_ext_i32_i64(dest, src); break;
4107
        default: abort();
4108
        }
4109
    }
4110
    dead_tmp(src);
4111
}
4112

    
4113
static inline void gen_neon_addl(int size)
4114
{
4115
    switch (size) {
4116
    case 0: gen_helper_neon_addl_u16(CPU_V001); break;
4117
    case 1: gen_helper_neon_addl_u32(CPU_V001); break;
4118
    case 2: tcg_gen_add_i64(CPU_V001); break;
4119
    default: abort();
4120
    }
4121
}
4122

    
4123
static inline void gen_neon_subl(int size)
4124
{
4125
    switch (size) {
4126
    case 0: gen_helper_neon_subl_u16(CPU_V001); break;
4127
    case 1: gen_helper_neon_subl_u32(CPU_V001); break;
4128
    case 2: tcg_gen_sub_i64(CPU_V001); break;
4129
    default: abort();
4130
    }
4131
}
4132

    
4133
static inline void gen_neon_negl(TCGv_i64 var, int size)
4134
{
4135
    switch (size) {
4136
    case 0: gen_helper_neon_negl_u16(var, var); break;
4137
    case 1: gen_helper_neon_negl_u32(var, var); break;
4138
    case 2: gen_helper_neon_negl_u64(var, var); break;
4139
    default: abort();
4140
    }
4141
}
4142

    
4143
static inline void gen_neon_addl_saturate(TCGv_i64 op0, TCGv_i64 op1, int size)
4144
{
4145
    switch (size) {
4146
    case 1: gen_helper_neon_addl_saturate_s32(op0, cpu_env, op0, op1); break;
4147
    case 2: gen_helper_neon_addl_saturate_s64(op0, cpu_env, op0, op1); break;
4148
    default: abort();
4149
    }
4150
}
4151

    
4152
static inline void gen_neon_mull(TCGv_i64 dest, TCGv a, TCGv b, int size, int u)
4153
{
4154
    TCGv_i64 tmp;
4155

    
4156
    switch ((size << 1) | u) {
4157
    case 0: gen_helper_neon_mull_s8(dest, a, b); break;
4158
    case 1: gen_helper_neon_mull_u8(dest, a, b); break;
4159
    case 2: gen_helper_neon_mull_s16(dest, a, b); break;
4160
    case 3: gen_helper_neon_mull_u16(dest, a, b); break;
4161
    case 4:
4162
        tmp = gen_muls_i64_i32(a, b);
4163
        tcg_gen_mov_i64(dest, tmp);
4164
        break;
4165
    case 5:
4166
        tmp = gen_mulu_i64_i32(a, b);
4167
        tcg_gen_mov_i64(dest, tmp);
4168
        break;
4169
    default: abort();
4170
    }
4171
}
4172

    
4173
/* Translate a NEON data processing instruction.  Return nonzero if the
4174
   instruction is invalid.
4175
   We process data in a mixture of 32-bit and 64-bit chunks.
4176
   Mostly we use 32-bit chunks so we can use normal scalar instructions.  */
4177

    
4178
static int disas_neon_data_insn(CPUState * env, DisasContext *s, uint32_t insn)
4179
{
4180
    int op;
4181
    int q;
4182
    int rd, rn, rm;
4183
    int size;
4184
    int shift;
4185
    int pass;
4186
    int count;
4187
    int pairwise;
4188
    int u;
4189
    int n;
4190
    uint32_t imm, mask;
4191
    TCGv tmp, tmp2, tmp3, tmp4, tmp5;
4192
    TCGv_i64 tmp64;
4193

    
4194
    if (!vfp_enabled(env))
4195
      return 1;
4196
    q = (insn & (1 << 6)) != 0;
4197
    u = (insn >> 24) & 1;
4198
    VFP_DREG_D(rd, insn);
4199
    VFP_DREG_N(rn, insn);
4200
    VFP_DREG_M(rm, insn);
4201
    size = (insn >> 20) & 3;
4202
    if ((insn & (1 << 23)) == 0) {
4203
        /* Three register same length.  */
4204
        op = ((insn >> 7) & 0x1e) | ((insn >> 4) & 1);
4205
        if (size == 3 && (op == 1 || op == 5 || op == 8 || op == 9
4206
                          || op == 10 || op  == 11 || op == 16)) {
4207
            /* 64-bit element instructions.  */
4208
            for (pass = 0; pass < (q ? 2 : 1); pass++) {
4209
                neon_load_reg64(cpu_V0, rn + pass);
4210
                neon_load_reg64(cpu_V1, rm + pass);
4211
                switch (op) {
4212
                case 1: /* VQADD */
4213
                    if (u) {
4214
                        gen_helper_neon_add_saturate_u64(CPU_V001);
4215
                    } else {
4216
                        gen_helper_neon_add_saturate_s64(CPU_V001);
4217
                    }
4218
                    break;
4219
                case 5: /* VQSUB */
4220
                    if (u) {
4221
                        gen_helper_neon_sub_saturate_u64(CPU_V001);
4222
                    } else {
4223
                        gen_helper_neon_sub_saturate_s64(CPU_V001);
4224
                    }
4225
                    break;
4226
                case 8: /* VSHL */
4227
                    if (u) {
4228
                        gen_helper_neon_shl_u64(cpu_V0, cpu_V1, cpu_V0);
4229
                    } else {
4230
                        gen_helper_neon_shl_s64(cpu_V0, cpu_V1, cpu_V0);
4231
                    }
4232
                    break;
4233
                case 9: /* VQSHL */
4234
                    if (u) {
4235
                        gen_helper_neon_qshl_u64(cpu_V0, cpu_env,
4236
                                                 cpu_V0, cpu_V0);
4237
                    } else {
4238
                        gen_helper_neon_qshl_s64(cpu_V1, cpu_env,
4239
                                                 cpu_V1, cpu_V0);
4240
                    }
4241
                    break;
4242
                case 10: /* VRSHL */
4243
                    if (u) {
4244
                        gen_helper_neon_rshl_u64(cpu_V0, cpu_V1, cpu_V0);
4245
                    } else {
4246
                        gen_helper_neon_rshl_s64(cpu_V0, cpu_V1, cpu_V0);
4247
                    }
4248
                    break;
4249
                case 11: /* VQRSHL */
4250
                    if (u) {
4251
                        gen_helper_neon_qrshl_u64(cpu_V0, cpu_env,
4252
                                                  cpu_V1, cpu_V0);
4253
                    } else {
4254
                        gen_helper_neon_qrshl_s64(cpu_V0, cpu_env,
4255
                                                  cpu_V1, cpu_V0);
4256
                    }
4257
                    break;
4258
                case 16:
4259
                    if (u) {
4260
                        tcg_gen_sub_i64(CPU_V001);
4261
                    } else {
4262
                        tcg_gen_add_i64(CPU_V001);
4263
                    }
4264
                    break;
4265
                default:
4266
                    abort();
4267
                }
4268
                neon_store_reg64(cpu_V0, rd + pass);
4269
            }
4270
            return 0;
4271
        }
4272
        switch (op) {
4273
        case 8: /* VSHL */
4274
        case 9: /* VQSHL */
4275
        case 10: /* VRSHL */
4276
        case 11: /* VQRSHL */
4277
            {
4278
                int rtmp;
4279
                /* Shift instruction operands are reversed.  */
4280
                rtmp = rn;
4281
                rn = rm;
4282
                rm = rtmp;
4283
                pairwise = 0;
4284
            }
4285
            break;
4286
        case 20: /* VPMAX */
4287
        case 21: /* VPMIN */
4288
        case 23: /* VPADD */
4289
            pairwise = 1;
4290
            break;
4291
        case 26: /* VPADD (float) */
4292
            pairwise = (u && size < 2);
4293
            break;
4294
        case 30: /* VPMIN/VPMAX (float) */
4295
            pairwise = u;
4296
            break;
4297
        default:
4298
            pairwise = 0;
4299
            break;
4300
        }
4301

    
4302
        for (pass = 0; pass < (q ? 4 : 2); pass++) {
4303

    
4304
        if (pairwise) {
4305
            /* Pairwise.  */
4306
            if (q)
4307
                n = (pass & 1) * 2;
4308
            else
4309
                n = 0;
4310
            if (pass < q + 1) {
4311
                tmp = neon_load_reg(rn, n);
4312
                tmp2 = neon_load_reg(rn, n + 1);
4313
            } else {
4314
                tmp = neon_load_reg(rm, n);
4315
                tmp2 = neon_load_reg(rm, n + 1);
4316
            }
4317
        } else {
4318
            /* Elementwise.  */
4319
            tmp = neon_load_reg(rn, pass);
4320
            tmp2 = neon_load_reg(rm, pass);
4321
        }
4322
        switch (op) {
4323
        case 0: /* VHADD */
4324
            GEN_NEON_INTEGER_OP(hadd);
4325
            break;
4326
        case 1: /* VQADD */
4327
            GEN_NEON_INTEGER_OP_ENV(qadd);
4328
            break;
4329
        case 2: /* VRHADD */
4330
            GEN_NEON_INTEGER_OP(rhadd);
4331
            break;
4332
        case 3: /* Logic ops.  */
4333
            switch ((u << 2) | size) {
4334
            case 0: /* VAND */
4335
                tcg_gen_and_i32(tmp, tmp, tmp2);
4336
                break;
4337
            case 1: /* BIC */
4338
                tcg_gen_andc_i32(tmp, tmp, tmp2);
4339
                break;
4340
            case 2: /* VORR */
4341
                tcg_gen_or_i32(tmp, tmp, tmp2);
4342
                break;
4343
            case 3: /* VORN */
4344
                tcg_gen_orc_i32(tmp, tmp, tmp2);
4345
                break;
4346
            case 4: /* VEOR */
4347
                tcg_gen_xor_i32(tmp, tmp, tmp2);
4348
                break;
4349
            case 5: /* VBSL */
4350
                tmp3 = neon_load_reg(rd, pass);
4351
                gen_neon_bsl(tmp, tmp, tmp2, tmp3);
4352
                dead_tmp(tmp3);
4353
                break;
4354
            case 6: /* VBIT */
4355
                tmp3 = neon_load_reg(rd, pass);
4356
                gen_neon_bsl(tmp, tmp, tmp3, tmp2);
4357
                dead_tmp(tmp3);
4358
                break;
4359
            case 7: /* VBIF */
4360
                tmp3 = neon_load_reg(rd, pass);
4361
                gen_neon_bsl(tmp, tmp3, tmp, tmp2);
4362
                dead_tmp(tmp3);
4363
                break;
4364
            }
4365
            break;
4366
        case 4: /* VHSUB */
4367
            GEN_NEON_INTEGER_OP(hsub);
4368
            break;
4369
        case 5: /* VQSUB */
4370
            GEN_NEON_INTEGER_OP_ENV(qsub);
4371
            break;
4372
        case 6: /* VCGT */
4373
            GEN_NEON_INTEGER_OP(cgt);
4374
            break;
4375
        case 7: /* VCGE */
4376
            GEN_NEON_INTEGER_OP(cge);
4377
            break;
4378
        case 8: /* VSHL */
4379
            GEN_NEON_INTEGER_OP(shl);
4380
            break;
4381
        case 9: /* VQSHL */
4382
            GEN_NEON_INTEGER_OP_ENV(qshl);
4383
            break;
4384
        case 10: /* VRSHL */
4385
            GEN_NEON_INTEGER_OP(rshl);
4386
            break;
4387
        case 11: /* VQRSHL */
4388
            GEN_NEON_INTEGER_OP_ENV(qrshl);
4389
            break;
4390
        case 12: /* VMAX */
4391
            GEN_NEON_INTEGER_OP(max);
4392
            break;
4393
        case 13: /* VMIN */
4394
            GEN_NEON_INTEGER_OP(min);
4395
            break;
4396
        case 14: /* VABD */
4397
            GEN_NEON_INTEGER_OP(abd);
4398
            break;
4399
        case 15: /* VABA */
4400
            GEN_NEON_INTEGER_OP(abd);
4401
            dead_tmp(tmp2);
4402
            tmp2 = neon_load_reg(rd, pass);
4403
            gen_neon_add(size, tmp, tmp2);
4404
            break;
4405
        case 16:
4406
            if (!u) { /* VADD */
4407
                if (gen_neon_add(size, tmp, tmp2))
4408
                    return 1;
4409
            } else { /* VSUB */
4410
                switch (size) {
4411
                case 0: gen_helper_neon_sub_u8(tmp, tmp, tmp2); break;
4412
                case 1: gen_helper_neon_sub_u16(tmp, tmp, tmp2); break;
4413
                case 2: tcg_gen_sub_i32(tmp, tmp, tmp2); break;
4414
                default: return 1;
4415
                }
4416
            }
4417
            break;
4418
        case 17:
4419
            if (!u) { /* VTST */
4420
                switch (size) {
4421
                case 0: gen_helper_neon_tst_u8(tmp, tmp, tmp2); break;
4422
                case 1: gen_helper_neon_tst_u16(tmp, tmp, tmp2); break;
4423
                case 2: gen_helper_neon_tst_u32(tmp, tmp, tmp2); break;
4424
                default: return 1;
4425
                }
4426
            } else { /* VCEQ */
4427
                switch (size) {
4428
                case 0: gen_helper_neon_ceq_u8(tmp, tmp, tmp2); break;
4429
                case 1: gen_helper_neon_ceq_u16(tmp, tmp, tmp2); break;
4430
                case 2: gen_helper_neon_ceq_u32(tmp, tmp, tmp2); break;
4431
                default: return 1;
4432
                }
4433
            }
4434
            break;
4435
        case 18: /* Multiply.  */
4436
            switch (size) {
4437
            case 0: gen_helper_neon_mul_u8(tmp, tmp, tmp2); break;
4438
            case 1: gen_helper_neon_mul_u16(tmp, tmp, tmp2); break;
4439
            case 2: tcg_gen_mul_i32(tmp, tmp, tmp2); break;
4440
            default: return 1;
4441
            }
4442
            dead_tmp(tmp2);
4443
            tmp2 = neon_load_reg(rd, pass);
4444
            if (u) { /* VMLS */
4445
                gen_neon_rsb(size, tmp, tmp2);
4446
            } else { /* VMLA */
4447
                gen_neon_add(size, tmp, tmp2);
4448
            }
4449
            break;
4450
        case 19: /* VMUL */
4451
            if (u) { /* polynomial */
4452
                gen_helper_neon_mul_p8(tmp, tmp, tmp2);
4453
            } else { /* Integer */
4454
                switch (size) {
4455
                case 0: gen_helper_neon_mul_u8(tmp, tmp, tmp2); break;
4456
                case 1: gen_helper_neon_mul_u16(tmp, tmp, tmp2); break;
4457
                case 2: tcg_gen_mul_i32(tmp, tmp, tmp2); break;
4458
                default: return 1;
4459
                }
4460
            }
4461
            break;
4462
        case 20: /* VPMAX */
4463
            GEN_NEON_INTEGER_OP(pmax);
4464
            break;
4465
        case 21: /* VPMIN */
4466
            GEN_NEON_INTEGER_OP(pmin);
4467
            break;
4468
        case 22: /* Hultiply high.  */
4469
            if (!u) { /* VQDMULH */
4470
                switch (size) {
4471
                case 1: gen_helper_neon_qdmulh_s16(tmp, cpu_env, tmp, tmp2); break;
4472
                case 2: gen_helper_neon_qdmulh_s32(tmp, cpu_env, tmp, tmp2); break;
4473
                default: return 1;
4474
                }
4475
            } else { /* VQRDHMUL */
4476
                switch (size) {
4477
                case 1: gen_helper_neon_qrdmulh_s16(tmp, cpu_env, tmp, tmp2); break;
4478
                case 2: gen_helper_neon_qrdmulh_s32(tmp, cpu_env, tmp, tmp2); break;
4479
                default: return 1;
4480
                }
4481
            }
4482
            break;
4483
        case 23: /* VPADD */
4484
            if (u)
4485
                return 1;
4486
            switch (size) {
4487
            case 0: gen_helper_neon_padd_u8(tmp, tmp, tmp2); break;
4488
            case 1: gen_helper_neon_padd_u16(tmp, tmp, tmp2); break;
4489
            case 2: tcg_gen_add_i32(tmp, tmp, tmp2); break;
4490
            default: return 1;
4491
            }
4492
            break;
4493
        case 26: /* Floating point arithnetic.  */
4494
            switch ((u << 2) | size) {
4495
            case 0: /* VADD */
4496
                gen_helper_neon_add_f32(tmp, tmp, tmp2);
4497
                break;
4498
            case 2: /* VSUB */
4499
                gen_helper_neon_sub_f32(tmp, tmp, tmp2);
4500
                break;
4501
            case 4: /* VPADD */
4502
                gen_helper_neon_add_f32(tmp, tmp, tmp2);
4503
                break;
4504
            case 6: /* VABD */
4505
                gen_helper_neon_abd_f32(tmp, tmp, tmp2);
4506
                break;
4507
            default:
4508
                return 1;
4509
            }
4510
            break;
4511
        case 27: /* Float multiply.  */
4512
            gen_helper_neon_mul_f32(tmp, tmp, tmp2);
4513
            if (!u) {
4514
                dead_tmp(tmp2);
4515
                tmp2 = neon_load_reg(rd, pass);
4516
                if (size == 0) {
4517
                    gen_helper_neon_add_f32(tmp, tmp, tmp2);
4518
                } else {
4519
                    gen_helper_neon_sub_f32(tmp, tmp2, tmp);
4520
                }
4521
            }
4522
            break;
4523
        case 28: /* Float compare.  */
4524
            if (!u) {
4525
                gen_helper_neon_ceq_f32(tmp, tmp, tmp2);
4526
            } else {
4527
                if (size == 0)
4528
                    gen_helper_neon_cge_f32(tmp, tmp, tmp2);
4529
                else
4530
                    gen_helper_neon_cgt_f32(tmp, tmp, tmp2);
4531
            }
4532
            break;
4533
        case 29: /* Float compare absolute.  */
4534
            if (!u)
4535
                return 1;
4536
            if (size == 0)
4537
                gen_helper_neon_acge_f32(tmp, tmp, tmp2);
4538
            else
4539
                gen_helper_neon_acgt_f32(tmp, tmp, tmp2);
4540
            break;
4541
        case 30: /* Float min/max.  */
4542
            if (size == 0)
4543
                gen_helper_neon_max_f32(tmp, tmp, tmp2);
4544
            else
4545
                gen_helper_neon_min_f32(tmp, tmp, tmp2);
4546
            break;
4547
        case 31:
4548
            if (size == 0)
4549
                gen_helper_recps_f32(tmp, tmp, tmp2, cpu_env);
4550
            else
4551
                gen_helper_rsqrts_f32(tmp, tmp, tmp2, cpu_env);
4552
            break;
4553
        default:
4554
            abort();
4555
        }
4556
        dead_tmp(tmp2);
4557

    
4558
        /* Save the result.  For elementwise operations we can put it
4559
           straight into the destination register.  For pairwise operations
4560
           we have to be careful to avoid clobbering the source operands.  */
4561
        if (pairwise && rd == rm) {
4562
            neon_store_scratch(pass, tmp);
4563
        } else {
4564
            neon_store_reg(rd, pass, tmp);
4565
        }
4566

    
4567
        } /* for pass */
4568
        if (pairwise && rd == rm) {
4569
            for (pass = 0; pass < (q ? 4 : 2); pass++) {
4570
                tmp = neon_load_scratch(pass);
4571
                neon_store_reg(rd, pass, tmp);
4572
            }
4573
        }
4574
        /* End of 3 register same size operations.  */
4575
    } else if (insn & (1 << 4)) {
4576
        if ((insn & 0x00380080) != 0) {
4577
            /* Two registers and shift.  */
4578
            op = (insn >> 8) & 0xf;
4579
            if (insn & (1 << 7)) {
4580
                /* 64-bit shift.   */
4581
                size = 3;
4582
            } else {
4583
                size = 2;
4584
                while ((insn & (1 << (size + 19))) == 0)
4585
                    size--;
4586
            }
4587
            shift = (insn >> 16) & ((1 << (3 + size)) - 1);
4588
            /* To avoid excessive dumplication of ops we implement shift
4589
               by immediate using the variable shift operations.  */
4590
            if (op < 8) {
4591
                /* Shift by immediate:
4592
                   VSHR, VSRA, VRSHR, VRSRA, VSRI, VSHL, VQSHL, VQSHLU.  */
4593
                /* Right shifts are encoded as N - shift, where N is the
4594
                   element size in bits.  */
4595
                if (op <= 4)
4596
                    shift = shift - (1 << (size + 3));
4597
                if (size == 3) {
4598
                    count = q + 1;
4599
                } else {
4600
                    count = q ? 4: 2;
4601
                }
4602
                switch (size) {
4603
                case 0:
4604
                    imm = (uint8_t) shift;
4605
                    imm |= imm << 8;
4606
                    imm |= imm << 16;
4607
                    break;
4608
                case 1:
4609
                    imm = (uint16_t) shift;
4610
                    imm |= imm << 16;
4611
                    break;
4612
                case 2:
4613
                case 3:
4614
                    imm = shift;
4615
                    break;
4616
                default:
4617
                    abort();
4618
                }
4619

    
4620
                for (pass = 0; pass < count; pass++) {
4621
                    if (size == 3) {
4622
                        neon_load_reg64(cpu_V0, rm + pass);
4623
                        tcg_gen_movi_i64(cpu_V1, imm);
4624
                        switch (op) {
4625
                        case 0:  /* VSHR */
4626
                        case 1:  /* VSRA */
4627
                            if (u)
4628
                                gen_helper_neon_shl_u64(cpu_V0, cpu_V0, cpu_V1);
4629
                            else
4630
                                gen_helper_neon_shl_s64(cpu_V0, cpu_V0, cpu_V1);
4631
                            break;
4632
                        case 2: /* VRSHR */
4633
                        case 3: /* VRSRA */
4634
                            if (u)
4635
                                gen_helper_neon_rshl_u64(cpu_V0, cpu_V0, cpu_V1);
4636
                            else
4637
                                gen_helper_neon_rshl_s64(cpu_V0, cpu_V0, cpu_V1);
4638
                            break;
4639
                        case 4: /* VSRI */
4640
                            if (!u)
4641
                                return 1;
4642
                            gen_helper_neon_shl_u64(cpu_V0, cpu_V0, cpu_V1);
4643
                            break;
4644
                        case 5: /* VSHL, VSLI */
4645
                            gen_helper_neon_shl_u64(cpu_V0, cpu_V0, cpu_V1);
4646
                            break;
4647
                        case 6: /* VQSHL */
4648
                            if (u)
4649
                                gen_helper_neon_qshl_u64(cpu_V0, cpu_env, cpu_V0, cpu_V1);
4650
                            else
4651
                                gen_helper_neon_qshl_s64(cpu_V0, cpu_env, cpu_V0, cpu_V1);
4652
                            break;
4653
                        case 7: /* VQSHLU */
4654
                            gen_helper_neon_qshl_u64(cpu_V0, cpu_env, cpu_V0, cpu_V1);
4655
                            break;
4656
                        }
4657
                        if (op == 1 || op == 3) {
4658
                            /* Accumulate.  */
4659
                            neon_load_reg64(cpu_V0, rd + pass);
4660
                            tcg_gen_add_i64(cpu_V0, cpu_V0, cpu_V1);
4661
                        } else if (op == 4 || (op == 5 && u)) {
4662
                            /* Insert */
4663
                            cpu_abort(env, "VS[LR]I.64 not implemented");
4664
                        }
4665
                        neon_store_reg64(cpu_V0, rd + pass);
4666
                    } else { /* size < 3 */
4667
                        /* Operands in T0 and T1.  */
4668
                        tmp = neon_load_reg(rm, pass);
4669
                        tmp2 = new_tmp();
4670
                        tcg_gen_movi_i32(tmp2, imm);
4671
                        switch (op) {
4672
                        case 0:  /* VSHR */
4673
                        case 1:  /* VSRA */
4674
                            GEN_NEON_INTEGER_OP(shl);
4675
                            break;
4676
                        case 2: /* VRSHR */
4677
                        case 3: /* VRSRA */
4678
                            GEN_NEON_INTEGER_OP(rshl);
4679
                            break;
4680
                        case 4: /* VSRI */
4681
                            if (!u)
4682
                                return 1;
4683
                            GEN_NEON_INTEGER_OP(shl);
4684
                            break;
4685
                        case 5: /* VSHL, VSLI */
4686
                            switch (size) {
4687
                            case 0: gen_helper_neon_shl_u8(tmp, tmp, tmp2); break;
4688
                            case 1: gen_helper_neon_shl_u16(tmp, tmp, tmp2); break;
4689
                            case 2: gen_helper_neon_shl_u32(tmp, tmp, tmp2); break;
4690
                            default: return 1;
4691
                            }
4692
                            break;
4693
                        case 6: /* VQSHL */
4694
                            GEN_NEON_INTEGER_OP_ENV(qshl);
4695
                            break;
4696
                        case 7: /* VQSHLU */
4697
                            switch (size) {
4698
                            case 0: gen_helper_neon_qshl_u8(tmp, cpu_env, tmp, tmp2); break;
4699
                            case 1: gen_helper_neon_qshl_u16(tmp, cpu_env, tmp, tmp2); break;
4700
                            case 2: gen_helper_neon_qshl_u32(tmp, cpu_env, tmp, tmp2); break;
4701
                            default: return 1;
4702
                            }
4703
                            break;
4704
                        }
4705
                        dead_tmp(tmp2);
4706

    
4707
                        if (op == 1 || op == 3) {
4708
                            /* Accumulate.  */
4709
                            tmp2 = neon_load_reg(rd, pass);
4710
                            gen_neon_add(size, tmp2, tmp);
4711
                            dead_tmp(tmp2);
4712
                        } else if (op == 4 || (op == 5 && u)) {
4713
                            /* Insert */
4714
                            switch (size) {
4715
                            case 0:
4716
                                if (op == 4)
4717
                                    mask = 0xff >> -shift;
4718
                                else
4719
                                    mask = (uint8_t)(0xff << shift);
4720
                                mask |= mask << 8;
4721
                                mask |= mask << 16;
4722
                                break;
4723
                            case 1:
4724
                                if (op == 4)
4725
                                    mask = 0xffff >> -shift;
4726
                                else
4727
                                    mask = (uint16_t)(0xffff << shift);
4728
                                mask |= mask << 16;
4729
                                break;
4730
                            case 2:
4731
                                if (shift < -31 || shift > 31) {
4732
                                    mask = 0;
4733
                                } else {
4734
                                    if (op == 4)
4735
                                        mask = 0xffffffffu >> -shift;
4736
                                    else
4737
                                        mask = 0xffffffffu << shift;
4738
                                }
4739
                                break;
4740
                            default:
4741
                                abort();
4742
                            }
4743
                            tmp2 = neon_load_reg(rd, pass);
4744
                            tcg_gen_andi_i32(tmp, tmp, mask);
4745
                            tcg_gen_andi_i32(tmp2, tmp2, ~mask);
4746
                            tcg_gen_or_i32(tmp, tmp, tmp2);
4747
                            dead_tmp(tmp2);
4748
                        }
4749
                        neon_store_reg(rd, pass, tmp);
4750
                    }
4751
                } /* for pass */
4752
            } else if (op < 10) {
4753
                /* Shift by immediate and narrow:
4754
                   VSHRN, VRSHRN, VQSHRN, VQRSHRN.  */
4755
                shift = shift - (1 << (size + 3));
4756
                size++;
4757
                switch (size) {
4758
                case 1:
4759
                    imm = (uint16_t)shift;
4760
                    imm |= imm << 16;
4761
                    tmp2 = tcg_const_i32(imm);
4762
                    TCGV_UNUSED_I64(tmp64);
4763
                    break;
4764
                case 2:
4765
                    imm = (uint32_t)shift;
4766
                    tmp2 = tcg_const_i32(imm);
4767
                    TCGV_UNUSED_I64(tmp64);
4768
                    break;
4769
                case 3:
4770
                    tmp64 = tcg_const_i64(shift);
4771
                    TCGV_UNUSED(tmp2);
4772
                    break;
4773
                default:
4774
                    abort();
4775
                }
4776

    
4777
                for (pass = 0; pass < 2; pass++) {
4778
                    if (size == 3) {
4779
                        neon_load_reg64(cpu_V0, rm + pass);
4780
                        if (q) {
4781
                          if (u)
4782
                            gen_helper_neon_rshl_u64(cpu_V0, cpu_V0, tmp64);
4783
                          else
4784
                            gen_helper_neon_rshl_s64(cpu_V0, cpu_V0, tmp64);
4785
                        } else {
4786
                          if (u)
4787
                            gen_helper_neon_shl_u64(cpu_V0, cpu_V0, tmp64);
4788
                          else
4789
                            gen_helper_neon_shl_s64(cpu_V0, cpu_V0, tmp64);
4790
                        }
4791
                    } else {
4792
                        tmp = neon_load_reg(rm + pass, 0);
4793
                        gen_neon_shift_narrow(size, tmp, tmp2, q, u);
4794
                        tmp3 = neon_load_reg(rm + pass, 1);
4795
                        gen_neon_shift_narrow(size, tmp3, tmp2, q, u);
4796
                        tcg_gen_concat_i32_i64(cpu_V0, tmp, tmp3);
4797
                        dead_tmp(tmp);
4798
                        dead_tmp(tmp3);
4799
                    }
4800
                    tmp = new_tmp();
4801
                    if (op == 8 && !u) {
4802
                        gen_neon_narrow(size - 1, tmp, cpu_V0);
4803
                    } else {
4804
                        if (op == 8)
4805