translate.c 312 KB
Newer Older
bellard's avatar
bellard committed
1
2
/*
 *  ARM translation
3
 *
bellard's avatar
bellard committed
4
 *  Copyright (c) 2003 Fabrice Bellard
pbrook's avatar
pbrook committed
5
 *  Copyright (c) 2005-2007 CodeSourcery
6
 *  Copyright (c) 2007 OpenedHand, Ltd.
bellard's avatar
bellard committed
7
8
9
10
11
12
13
14
15
16
17
18
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * 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/>.
bellard's avatar
bellard committed
20
21
22
23
24
25
26
27
28
29
 */
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <inttypes.h>

#include "cpu.h"
#include "exec-all.h"
#include "disas.h"
bellard's avatar
bellard committed
30
#include "tcg-op.h"
31
#include "qemu-log.h"
pbrook's avatar
pbrook committed
32

pbrook's avatar
pbrook committed
33
#include "helpers.h"
pbrook's avatar
pbrook committed
34
#define GEN_HELPER 1
pbrook's avatar
pbrook committed
35
#include "helpers.h"
bellard's avatar
bellard committed
36

pbrook's avatar
pbrook committed
37
38
39
40
41
#define ENABLE_ARCH_5J    0
#define ENABLE_ARCH_6     arm_feature(env, ARM_FEATURE_V6)
#define ENABLE_ARCH_6K   arm_feature(env, ARM_FEATURE_V6K)
#define ENABLE_ARCH_6T2   arm_feature(env, ARM_FEATURE_THUMB2)
#define ENABLE_ARCH_7     arm_feature(env, ARM_FEATURE_V7)
bellard's avatar
bellard committed
42

pbrook's avatar
pbrook committed
43
#define ARCH(x) do { if (!ENABLE_ARCH_##x) goto illegal_op; } while(0)
bellard's avatar
bellard committed
44

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

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

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

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

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

pbrook's avatar
pbrook committed
91
92
#include "gen-icount.h"

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

pbrook's avatar
pbrook committed
97
98
99
/* initialize TCG globals.  */
void arm_translate_init(void)
{
100
101
    int i;

pbrook's avatar
pbrook committed
102
103
    cpu_env = tcg_global_reg_new_ptr(TCG_AREG0, "env");

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

pbrook's avatar
pbrook committed
122
123
#define GEN_HELPER 2
#include "helpers.h"
pbrook's avatar
pbrook committed
124
125
126
127
128
}

static int num_temps;

/* Allocate a temporary variable.  */
pbrook's avatar
pbrook committed
129
static TCGv_i32 new_tmp(void)
pbrook's avatar
pbrook committed
130
{
131
132
    num_temps++;
    return tcg_temp_new_i32();
pbrook's avatar
pbrook committed
133
134
135
136
137
}

/* Release a temporary variable.  */
static void dead_tmp(TCGv tmp)
{
138
    tcg_temp_free(tmp);
pbrook's avatar
pbrook committed
139
140
141
    num_temps--;
}

pbrook's avatar
pbrook committed
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
static inline TCGv load_cpu_offset(int offset)
{
    TCGv tmp = new_tmp();
    tcg_gen_ld_i32(tmp, cpu_env, offset);
    return tmp;
}

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

static inline void store_cpu_offset(TCGv var, int offset)
{
    tcg_gen_st_i32(var, cpu_env, offset);
    dead_tmp(var);
}

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

pbrook's avatar
pbrook committed
160
161
162
163
164
165
166
167
168
169
170
171
/* Set a variable to the value of a CPU register.  */
static void load_reg_var(DisasContext *s, TCGv var, int reg)
{
    if (reg == 15) {
        uint32_t addr;
        /* normaly, since we updated PC, we need only to add one insn */
        if (s->thumb)
            addr = (long)s->pc + 2;
        else
            addr = (long)s->pc + 4;
        tcg_gen_movi_i32(var, addr);
    } else {
172
        tcg_gen_mov_i32(var, cpu_R[reg]);
pbrook's avatar
pbrook committed
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
    }
}

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

/* Set a CPU register.  The source must be a temporary and will be
   marked as dead.  */
static void store_reg(DisasContext *s, int reg, TCGv var)
{
    if (reg == 15) {
        tcg_gen_andi_i32(var, var, ~1);
        s->is_jmp = DISAS_JUMP;
    }
192
    tcg_gen_mov_i32(cpu_R[reg], var);
pbrook's avatar
pbrook committed
193
194
195
196
    dead_tmp(var);
}

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

pbrook's avatar
pbrook committed
202
203
#define gen_sxtb16(var) gen_helper_sxtb16(var, var)
#define gen_uxtb16(var) gen_helper_uxtb16(var, var)
pbrook's avatar
pbrook committed
204

pbrook's avatar
pbrook committed
205

206
207
208
209
210
211
static inline void gen_set_cpsr(TCGv var, uint32_t mask)
{
    TCGv tmp_mask = tcg_const_i32(mask);
    gen_helper_cpsr_write(var, tmp_mask);
    tcg_temp_free_i32(tmp_mask);
}
pbrook's avatar
pbrook committed
212
213
214
215
216
217
218
219
220
221
222
/* Set NZCV flags from the high 4 bits of var.  */
#define gen_set_nzcv(var) gen_set_cpsr(var, CPSR_NZCV)

static void gen_exception(int excp)
{
    TCGv tmp = new_tmp();
    tcg_gen_movi_i32(tmp, excp);
    gen_helper_exception(tmp);
    dead_tmp(tmp);
}

pbrook's avatar
pbrook committed
223
224
225
226
static void gen_smul_dual(TCGv a, TCGv b)
{
    TCGv tmp1 = new_tmp();
    TCGv tmp2 = new_tmp();
227
228
    tcg_gen_ext16s_i32(tmp1, a);
    tcg_gen_ext16s_i32(tmp2, b);
pbrook's avatar
pbrook committed
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
    tcg_gen_mul_i32(tmp1, tmp1, tmp2);
    dead_tmp(tmp2);
    tcg_gen_sari_i32(a, a, 16);
    tcg_gen_sari_i32(b, b, 16);
    tcg_gen_mul_i32(b, b, a);
    tcg_gen_mov_i32(a, tmp1);
    dead_tmp(tmp1);
}

/* Byteswap each halfword.  */
static void gen_rev16(TCGv var)
{
    TCGv tmp = new_tmp();
    tcg_gen_shri_i32(tmp, var, 8);
    tcg_gen_andi_i32(tmp, tmp, 0x00ff00ff);
    tcg_gen_shli_i32(var, var, 8);
    tcg_gen_andi_i32(var, var, 0xff00ff00);
    tcg_gen_or_i32(var, var, tmp);
    dead_tmp(tmp);
}

/* Byteswap low halfword and sign extend.  */
static void gen_revsh(TCGv var)
{
    TCGv tmp = new_tmp();
    tcg_gen_shri_i32(tmp, var, 8);
    tcg_gen_andi_i32(tmp, tmp, 0x00ff);
    tcg_gen_shli_i32(var, var, 8);
    tcg_gen_ext8s_i32(var, var);
    tcg_gen_or_i32(var, var, tmp);
    dead_tmp(tmp);
}

/* Unsigned bitfield extract.  */
static void gen_ubfx(TCGv var, int shift, uint32_t mask)
{
    if (shift)
        tcg_gen_shri_i32(var, var, shift);
    tcg_gen_andi_i32(var, var, mask);
}

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

    if (shift)
        tcg_gen_sari_i32(var, var, shift);
    if (shift + width < 32) {
        signbit = 1u << (width - 1);
        tcg_gen_andi_i32(var, var, (1u << width) - 1);
        tcg_gen_xori_i32(var, var, signbit);
        tcg_gen_subi_i32(var, var, signbit);
    }
}

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

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

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

    tcg_gen_extu_i32_i64(tmp1, a);
    dead_tmp(a);
    tcg_gen_extu_i32_i64(tmp2, b);
    dead_tmp(b);
    tcg_gen_mul_i64(tmp1, tmp1, tmp2);
314
    tcg_temp_free_i64(tmp2);
pbrook's avatar
pbrook committed
315
316
317
    return tmp1;
}

pbrook's avatar
pbrook committed
318
static TCGv_i64 gen_muls_i64_i32(TCGv a, TCGv b)
pbrook's avatar
pbrook committed
319
{
pbrook's avatar
pbrook committed
320
321
    TCGv_i64 tmp1 = tcg_temp_new_i64();
    TCGv_i64 tmp2 = tcg_temp_new_i64();
pbrook's avatar
pbrook committed
322
323
324
325
326
327

    tcg_gen_ext_i32_i64(tmp1, a);
    dead_tmp(a);
    tcg_gen_ext_i32_i64(tmp2, b);
    dead_tmp(b);
    tcg_gen_mul_i64(tmp1, tmp1, tmp2);
328
    tcg_temp_free_i64(tmp2);
pbrook's avatar
pbrook committed
329
330
331
    return tmp1;
}

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

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

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

pbrook's avatar
pbrook committed
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/* Dual 16-bit add.  Result placed in t0 and t1 is marked as dead.
    tmp = (t0 ^ t1) & 0x8000;
    t0 &= ~0x8000;
    t1 &= ~0x8000;
    t0 = (t0 + t1) ^ tmp;
 */

static void gen_add16(TCGv t0, TCGv t1)
{
    TCGv tmp = new_tmp();
    tcg_gen_xor_i32(tmp, t0, t1);
    tcg_gen_andi_i32(tmp, tmp, 0x8000);
    tcg_gen_andi_i32(t0, t0, ~0x8000);
    tcg_gen_andi_i32(t1, t1, ~0x8000);
    tcg_gen_add_i32(t0, t0, t1);
    tcg_gen_xor_i32(t0, t0, tmp);
    dead_tmp(tmp);
    dead_tmp(t1);
}

pbrook's avatar
pbrook committed
378
379
#define gen_set_CF(var) tcg_gen_st_i32(var, cpu_env, offsetof(CPUState, CF))

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

/* Set N and Z flags from var.  */
static inline void gen_logic_CC(TCGv var)
{
pbrook's avatar
pbrook committed
392
393
    tcg_gen_st_i32(var, cpu_env, offsetof(CPUState, NF));
    tcg_gen_st_i32(var, cpu_env, offsetof(CPUState, ZF));
pbrook's avatar
pbrook committed
394
395
396
}

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

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

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

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

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

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

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

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

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

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

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

pbrook's avatar
pbrook committed
609
610
611
612
613
614
615
616
static void gen_test_cc(int cc, int label)
{
    TCGv tmp;
    TCGv tmp2;
    int inv;

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

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

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

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

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

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

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

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

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

pbrook's avatar
pbrook committed
832
833
static inline void gen_add_data_offset(DisasContext *s, unsigned int insn,
                                       TCGv var)
bellard's avatar
bellard committed
834
{
bellard's avatar
bellard committed
835
    int val, rm, shift, shiftop;
pbrook's avatar
pbrook committed
836
    TCGv offset;
bellard's avatar
bellard committed
837
838
839
840
841
842

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

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

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

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

pbrook's avatar
pbrook committed
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
VFP_OP2(add)
VFP_OP2(sub)
VFP_OP2(mul)
VFP_OP2(div)

#undef VFP_OP2

static inline void gen_vfp_abs(int dp)
{
    if (dp)
        gen_helper_vfp_absd(cpu_F0d, cpu_F0d);
    else
        gen_helper_vfp_abss(cpu_F0s, cpu_F0s);
}

static inline void gen_vfp_neg(int dp)
{
    if (dp)
        gen_helper_vfp_negd(cpu_F0d, cpu_F0d);
    else
        gen_helper_vfp_negs(cpu_F0s, cpu_F0s);
}

static inline void gen_vfp_sqrt(int dp)
{
    if (dp)
        gen_helper_vfp_sqrtd(cpu_F0d, cpu_F0d, cpu_env);
    else
        gen_helper_vfp_sqrts(cpu_F0s, cpu_F0s, cpu_env);
}

static inline void gen_vfp_cmp(int dp)
{
    if (dp)
        gen_helper_vfp_cmpd(cpu_F0d, cpu_F1d, cpu_env);
    else
        gen_helper_vfp_cmps(cpu_F0s, cpu_F1s, cpu_env);
}

static inline void gen_vfp_cmpe(int dp)
{
    if (dp)
        gen_helper_vfp_cmped(cpu_F0d, cpu_F1d, cpu_env);
    else
        gen_helper_vfp_cmpes(cpu_F0s, cpu_F1s, cpu_env);
}

static inline void gen_vfp_F1_ld0(int dp)
{
    if (dp)
balrog's avatar
balrog committed
947
        tcg_gen_movi_i64(cpu_F1d, 0);
pbrook's avatar
pbrook committed
948
    else
balrog's avatar
balrog committed
949
        tcg_gen_movi_i32(cpu_F1s, 0);
pbrook's avatar
pbrook committed
950
951
952
953
954
955
956
957
958
959
960
961
962
}

static inline void gen_vfp_uito(int dp)
{
    if (dp)
        gen_helper_vfp_uitod(cpu_F0d, cpu_F0s, cpu_env);
    else
        gen_helper_vfp_uitos(cpu_F0s, cpu_F0s, cpu_env);
}

static inline void gen_vfp_sito(int dp)
{
    if (dp)
963
        gen_helper_vfp_sitod(cpu_F0d, cpu_F0s, cpu_env);
pbrook's avatar
pbrook committed
964
    else
965
        gen_helper_vfp_sitos(cpu_F0s, cpu_F0s, cpu_env);
pbrook's avatar
pbrook committed
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
}

static inline void gen_vfp_toui(int dp)
{
    if (dp)
        gen_helper_vfp_touid(cpu_F0s, cpu_F0d, cpu_env);
    else
        gen_helper_vfp_touis(cpu_F0s, cpu_F0s, cpu_env);
}

static inline void gen_vfp_touiz(int dp)
{
    if (dp)
        gen_helper_vfp_touizd(cpu_F0s, cpu_F0d, cpu_env);
    else
        gen_helper_vfp_touizs(cpu_F0s, cpu_F0s, cpu_env);
}

static inline void gen_vfp_tosi(int dp)
{
    if (dp)
        gen_helper_vfp_tosid(cpu_F0s, cpu_F0d, cpu_env);
    else
        gen_helper_vfp_tosis(cpu_F0s, cpu_F0s, cpu_env);
}

static inline void gen_vfp_tosiz(int dp)
pbrook's avatar
pbrook committed
993
994
{
    if (dp)
pbrook's avatar
pbrook committed
995
        gen_helper_vfp_tosizd(cpu_F0s, cpu_F0d, cpu_env);
pbrook's avatar
pbrook committed
996
    else
pbrook's avatar
pbrook committed
997
998
999
1000
1001
1002
        gen_helper_vfp_tosizs(cpu_F0s, cpu_F0s, cpu_env);
}

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

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

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

bellard's avatar
bellard committed
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
static inline long
vfp_reg_offset (int dp, int reg)
{
    if (dp)
        return offsetof(CPUARMState, vfp.regs[reg]);
    else if (reg & 1) {
        return offsetof(CPUARMState, vfp.regs[reg >> 1])
          + offsetof(CPU_DoubleU, l.upper);
    } else {
        return offsetof(CPUARMState, vfp.regs[reg >> 1])
          + offsetof(CPU_DoubleU, l.lower);
    }
}
pbrook's avatar
pbrook committed
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059

/* Return the offset of a 32-bit piece of a NEON register.
   zero is the least significant end of the register.  */
static inline long
neon_reg_offset (int reg, int n)
{
    int sreg;
    sreg = reg * 2 + n;
    return vfp_reg_offset(0, sreg);
}

pbrook's avatar
pbrook committed
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
static TCGv neon_load_reg(int reg, int pass)
{
    TCGv tmp = new_tmp();
    tcg_gen_ld_i32(tmp, cpu_env, neon_reg_offset(reg, pass));
    return tmp;
}

static void neon_store_reg(int reg, int pass, TCGv var)
{
    tcg_gen_st_i32(var, cpu_env, neon_reg_offset(reg, pass));
    dead_tmp(var);
}

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

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

pbrook's avatar
pbrook committed
1083
1084
1085
1086
1087
#define tcg_gen_ld_f32 tcg_gen_ld_i32
#define tcg_gen_ld_f64 tcg_gen_ld_i64
#define tcg_gen_st_f32 tcg_gen_st_i32
#define tcg_gen_st_f64 tcg_gen_st_i64

bellard's avatar
bellard committed
1088
1089
1090
static inline void gen_mov_F0_vreg(int dp, int reg)
{
    if (dp)
pbrook's avatar
pbrook committed
1091
        tcg_gen_ld_f64(cpu_F0d, cpu_env, vfp_reg_offset(dp, reg));
bellard's avatar
bellard committed
1092
    else
pbrook's avatar
pbrook committed
1093
        tcg_gen_ld_f32(cpu_F0s, cpu_env, vfp_reg_offset(dp, reg));
bellard's avatar
bellard committed
1094
1095
1096
1097
1098
}

static inline void gen_mov_F1_vreg(int dp, int reg)
{
    if (dp)
pbrook's avatar
pbrook committed
1099
        tcg_gen_ld_f64(cpu_F1d, cpu_env, vfp_reg_offset(dp, reg));
bellard's avatar
bellard committed
1100
    else
pbrook's avatar
pbrook committed
1101
        tcg_gen_ld_f32(cpu_F1s, cpu_env, vfp_reg_offset(dp, reg));
bellard's avatar
bellard committed
1102
1103
1104
1105
1106
}

static inline void gen_mov_vreg_F0(int dp, int reg)
{
    if (dp)
pbrook's avatar
pbrook committed
1107
        tcg_gen_st_f64(cpu_F0d, cpu_env, vfp_reg_offset(dp, reg));
bellard's avatar
bellard committed
1108
    else
pbrook's avatar
pbrook committed
1109
        tcg_gen_st_f32(cpu_F0s, cpu_env, vfp_reg_offset(dp, reg));
bellard's avatar
bellard committed
1110
1111
}

1112
1113
#define ARM_CP_RW_BIT	(1 << 20)

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

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

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

1131
static inline void iwmmxt_store_creg(int reg, TCGv var)
pbrook's avatar
pbrook committed
1132
{
1133
    tcg_gen_st_i32(var, cpu_env, offsetof(CPUState, iwmmxt.cregs[reg]));
1134
    dead_tmp(var);
pbrook's avatar
pbrook committed
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
}

static inline void gen_op_iwmmxt_movq_wRn_M0(int rn)
{
    iwmmxt_store_reg(cpu_M0, rn);
}

static inline void gen_op_iwmmxt_movq_M0_wRn(int rn)
{
    iwmmxt_load_reg(cpu_M0, rn);
}

static inline void gen_op_iwmmxt_orq_M0_wRn(int rn)
{
    iwmmxt_load_reg(cpu_V1, rn);
    tcg_gen_or_i64(cpu_M0, cpu_M0, cpu_V1);
}

static inline void gen_op_iwmmxt_andq_M0_wRn(int rn)
{
    iwmmxt_load_reg(cpu_V1, rn);
    tcg_gen_and_i64(cpu_M0, cpu_M0, cpu_V1);
}

static inline void gen_op_iwmmxt_xorq_M0_wRn(int rn)
{
    iwmmxt_load_reg(cpu_V1, rn);
    tcg_gen_xor_i64(cpu_M0, cpu_M0, cpu_V1);
}

#define IWMMXT_OP(name) \
static inline void gen_op_iwmmxt_##name##_M0_wRn(int rn) \
{ \
    iwmmxt_load_reg(cpu_V1, rn); \
    gen_helper_iwmmxt_##name(cpu_M0, cpu_M0, cpu_V1); \
}

#define IWMMXT_OP_ENV(name) \
static inline void gen_op_iwmmxt_##name##_M0_wRn(int rn) \
{ \
    iwmmxt_load_reg(cpu_V1, rn); \
    gen_helper_iwmmxt_##name(cpu_M0, cpu_env, cpu_M0, cpu_V1); \
}

#define IWMMXT_OP_ENV_SIZE(name) \
IWMMXT_OP_ENV(name##b) \
IWMMXT_OP_ENV(name##w) \
IWMMXT_OP_ENV(name##l)

#define IWMMXT_OP_ENV1(name) \
static inline void gen_op_iwmmxt_##name##_M0(void) \
{ \
    gen_helper_iwmmxt_##name(cpu_M0, cpu_env, cpu_M0); \
}

IWMMXT_OP(maddsq)
IWMMXT_OP(madduq)
IWMMXT_OP(sadb)
IWMMXT_OP(sadw)
IWMMXT_OP(mulslw)
IWMMXT_OP(mulshw)
IWMMXT_OP(mululw)
IWMMXT_OP(muluhw)
IWMMXT_OP(macsw)
IWMMXT_OP(macuw)

IWMMXT_OP_ENV_SIZE(unpackl)
IWMMXT_OP_ENV_SIZE(unpackh)

IWMMXT_OP_ENV1(unpacklub)
IWMMXT_OP_ENV1(unpackluw)
IWMMXT_OP_ENV1(unpacklul)
IWMMXT_OP_ENV1(unpackhub)
IWMMXT_OP_ENV1(unpackhuw)
IWMMXT_OP_ENV1(unpackhul)
IWMMXT_OP_ENV1(unpacklsb)
IWMMXT_OP_ENV1(unpacklsw)
IWMMXT_OP_ENV1(unpacklsl)
IWMMXT_OP_ENV1(unpackhsb)
IWMMXT_OP_ENV1(unpackhsw)
IWMMXT_OP_ENV1(unpackhsl)

IWMMXT_OP_ENV_SIZE(cmpeq)
IWMMXT_OP_ENV_SIZE(cmpgtu)
IWMMXT_OP_ENV_SIZE(cmpgts)

IWMMXT_OP_ENV_SIZE(mins)
IWMMXT_OP_ENV_SIZE(minu)
IWMMXT_OP_ENV_SIZE(maxs)
IWMMXT_OP_ENV_SIZE(maxu)

IWMMXT_OP_ENV_SIZE(subn)
IWMMXT_OP_ENV_SIZE(addn)
IWMMXT_OP_ENV_SIZE(subu)
IWMMXT_OP_ENV_SIZE(addu)
IWMMXT_OP_ENV_SIZE(subs)
IWMMXT_OP_ENV_SIZE(adds)

IWMMXT_OP_ENV(avgb0)
IWMMXT_OP_ENV(avgb1)
IWMMXT_OP_ENV(avgw0)
IWMMXT_OP_ENV(avgw1)

IWMMXT_OP(msadb)

IWMMXT_OP_ENV(packuw)
IWMMXT_OP_ENV(packul)
IWMMXT_OP_ENV(packuq)
IWMMXT_OP_ENV(packsw)
IWMMXT_OP_ENV(packsl)
IWMMXT_OP_ENV(packsq)

static void gen_op_iwmmxt_set_mup(void)
{
    TCGv tmp;
    tmp = load_cpu_field(iwmmxt.cregs[ARM_IWMMXT_wCon]);
    tcg_gen_ori_i32(tmp, tmp, 2);
    store_cpu_field(tmp, iwmmxt.cregs[ARM_IWMMXT_wCon]);
}

static void gen_op_iwmmxt_set_cup(void)
{
    TCGv tmp;
    tmp = load_cpu_field(iwmmxt.cregs[ARM_IWMMXT_wCon]);
    tcg_gen_ori_i32(tmp, tmp, 1);
    store_cpu_field(tmp, iwmmxt.cregs[ARM_IWMMXT_wCon]);
}

static void gen_op_iwmmxt_setpsr_nz(void)
{
    TCGv tmp = new_tmp();
    gen_helper_iwmmxt_setpsr_nz(tmp, cpu_M0);
    store_cpu_field(tmp, iwmmxt.cregs[ARM_IWMMXT_wCASF]);
}

static inline void gen_op_iwmmxt_addl_M0_wRn(int rn)
{
    iwmmxt_load_reg(cpu_V1, rn);
pbrook's avatar
pbrook committed
1273
    tcg_gen_ext32u_i64(cpu_V1, cpu_V1);
pbrook's avatar
pbrook committed
1274
1275
1276
    tcg_gen_add_i64(cpu_M0, cpu_M0, cpu_V1);
}

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

    rd = (insn >> 16) & 0xf;
1284
    tmp = load_reg(s, rd);
1285
1286
1287
1288
1289

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

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

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

/* Disassemble an iwMMXt instruction.  Returns nonzero if an error occured
   (ie. an undefined instruction).  */
static int disas_iwmmxt_insn(CPUState *env, DisasContext *s, uint32_t insn)
{
    int rd, wrd;
    int rdhi, rdlo, rd0, rd1, i;
1339
1340
    TCGv addr;
    TCGv tmp, tmp2, tmp3;
1341
1342
1343
1344
1345
1346
1347

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

        wrd = (insn >> 12) & 0xf;
1361
1362
1363
        addr = new_tmp();
        if (gen_iwmmxt_address(s, insn, addr)) {
            dead_tmp(addr);
1364
            return 1;
1365
        }
1366
1367
        if (insn & ARM_CP_RW_BIT) {
            if ((insn >> 28) == 0xf) {			/* WLDRW wCx */
1368
1369
1370
                tmp = new_tmp();
                tcg_gen_qemu_ld32u(tmp, addr, IS_USER(s));
                iwmmxt_store_creg(wrd, tmp);
1371
            } else {
pbrook's avatar
pbrook committed
1372
1373
1374
                i = 1;
                if (insn & (1 << 8)) {
                    if (insn & (1 << 22)) {		/* WLDRD */
1375
                        tcg_gen_qemu_ld64(cpu_M0, addr, IS_USER(s));
pbrook's avatar
pbrook committed
1376
1377
                        i = 0;
                    } else {				/* WLDRW wRd */
1378
                        tmp = gen_ld32(addr, IS_USER(s));
pbrook's avatar
pbrook committed
1379
1380
1381
                    }
                } else {
                    if (insn & (1 << 22)) {		/* WLDRH */
1382
                        tmp = gen_ld16u(addr, IS_USER(s));
pbrook's avatar
pbrook committed
1383
                    } else {				/* WLDRB */
1384
                        tmp = gen_ld8u(addr, IS_USER(s));
pbrook's avatar
pbrook committed
1385
1386
1387
1388
1389
1390
                    }
                }
                if (i) {
                    tcg_gen_extu_i32_i64(cpu_M0, tmp);
                    dead_tmp(tmp);
                }
1391
1392
1393
1394
                gen_op_iwmmxt_movq_wRn_M0(wrd);
            }
        } else {
            if ((insn >> 28) == 0xf) {			/* WSTRW wCx */
1395
1396
                tmp = iwmmxt_load_creg(wrd);
                gen_st32(tmp, addr, IS_USER(s));
1397
1398
            } else {
                gen_op_iwmmxt_movq_M0_wRn(wrd);
pbrook's avatar
pbrook committed
1399
1400
1401
1402
                tmp = new_tmp();
                if (insn & (1 << 8)) {
                    if (insn & (1 << 22)) {		/* WSTRD */
                        dead_tmp(tmp);
1403
                        tcg_gen_qemu_st64(cpu_M0, addr, IS_USER(s));
pbrook's avatar
pbrook committed
1404
1405
                    } else {				/* WSTRW wRd */
                        tcg_gen_trunc_i64_i32(tmp, cpu_M0);
1406
                        gen_st32(tmp, addr, IS_USER(s));
pbrook's avatar
pbrook committed
1407
1408
1409
1410
                    }
                } else {
                    if (insn & (1 << 22)) {		/* WSTRH */
                        tcg_gen_trunc_i64_i32(tmp, cpu_M0);
1411
                        gen_st16(tmp, addr, IS_USER(s));
pbrook's avatar
pbrook committed
1412
1413
                    } else {				/* WSTRB */
                        tcg_gen_trunc_i64_i32(tmp, cpu_M0);
1414
                        gen_st8(tmp, addr, IS_USER(s));
pbrook's avatar
pbrook committed
1415
1416
                    }
                }
1417
1418
            }
        }
1419
        dead_tmp(addr);
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
        return 0;
    }

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

    switch (((insn >> 12) & 0xf00) | ((insn >> 4) & 0xff)) {
    case 0x000:						/* WOR */
        wrd = (insn >> 12) & 0xf;
        rd0 = (insn >> 0) & 0xf;
        rd1 = (insn >> 16) & 0xf;
        gen_op_iwmmxt_movq_M0_wRn(rd0);
        gen_op_iwmmxt_orq_M0_wRn(rd1);
        gen_op_iwmmxt_setpsr_nz();
        gen_op_iwmmxt_movq_wRn_M0(wrd);
        gen_op_iwmmxt_set_mup();
        gen_op_iwmmxt_set_cup();
        break;
    case 0x011:						/* TMCR */
        if (insn & 0xf)
            return 1;
        rd = (insn >> 12) & 0xf;
        wrd = (insn >> 16) & 0xf;
        switch (wrd) {
        case ARM_IWMMXT_wCID:
        case ARM_IWMMXT_wCASF:
            break;
        case ARM_IWMMXT_wCon:
            gen_op_iwmmxt_set_cup();
            /* Fall through.  */
        case ARM_IWMMXT_wCSSF:
1451
1452
            tmp = iwmmxt_load_creg(wrd);
            tmp2 = load_reg(s, rd);
1453
            tcg_gen_andc_i32(tmp, tmp, tmp2);
1454
1455
            dead_tmp(tmp2);
            iwmmxt_store_creg(wrd, tmp);
1456
1457
1458
1459
1460
1461
            break;
        case ARM_IWMMXT_wCGR0:
        case ARM_IWMMXT_wCGR1:
        case ARM_IWMMXT_wCGR2:
        case ARM_IWMMXT_wCGR3:
            gen_op_iwmmxt_set_cup();
1462
1463
            tmp = load_reg(s, rd);
            iwmmxt_store_creg(wrd, tmp);
1464
1465
1466
1467
1468
1469
1470