~ubuntu-branches/ubuntu/vivid/qemu/vivid

« back to all changes in this revision

Viewing changes to .pc/ubuntu/arm64/0123-target-arm-Add-AArch32-FP-VRINTA-VRINTN-VRINTP-and-V.patch/target-arm/translate.c

  • Committer: Package Import Robot
  • Author(s): dann frazier
  • Date: 2014-02-11 15:41:53 UTC
  • Revision ID: package-import@ubuntu.com-20140211154153-2d001tf0ium08u81
Tags: 1.7.0+dfsg-3ubuntu2
* Backport changes to enable qemu-user-static support for aarch64
* debian/control: add ppc64el to Architectures
* debian/rules: only install qemu-system-aarch64 on arm64.
  Fixes a FTBFS  when built twice in a row on non-arm64 due to a stale
  debian/qemu-system-aarch64 directory

Show diffs side-by-side

added added

removed removed

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