translate.c 315 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
62
    int vfp_enabled;
63
64
    int vec_len;
    int vec_stride;
bellard's avatar
bellard committed
65
66
} DisasContext;

bellard's avatar
bellard committed
67
68
69
70
71
72
#if defined(CONFIG_USER_ONLY)
#define IS_USER(s) 1
#else
#define IS_USER(s) (s->user)
#endif

pbrook's avatar
pbrook committed
73
74
75
76
/* 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
77

pbrook's avatar
pbrook committed
78
static TCGv_ptr cpu_env;
pbrook's avatar
pbrook committed
79
/* We reuse the same 64-bit temporaries for efficiency.  */
pbrook's avatar
pbrook committed
80
static TCGv_i64 cpu_V0, cpu_V1, cpu_M0;
81
static TCGv_i32 cpu_R[16];
Paul Brook's avatar
Paul Brook committed
82
83
84
85
86
87
88
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
89

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

pbrook's avatar
pbrook committed
94
95
#include "gen-icount.h"

96
97
98
99
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
100
101
102
/* initialize TCG globals.  */
void arm_translate_init(void)
{
103
104
    int i;

pbrook's avatar
pbrook committed
105
106
    cpu_env = tcg_global_reg_new_ptr(TCG_AREG0, "env");

107
108
109
110
111
    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
112
113
114
115
116
117
118
119
120
121
122
123
    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
124

pbrook's avatar
pbrook committed
125
126
#define GEN_HELPER 2
#include "helpers.h"
pbrook's avatar
pbrook committed
127
128
129
130
131
}

static int num_temps;

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

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

pbrook's avatar
pbrook committed
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
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
163
164
165
166
167
168
169
170
171
172
173
174
/* 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 {
175
        tcg_gen_mov_i32(var, cpu_R[reg]);
pbrook's avatar
pbrook committed
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
    }
}

/* 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;
    }
195
    tcg_gen_mov_i32(cpu_R[reg], var);
pbrook's avatar
pbrook committed
196
197
198
199
    dead_tmp(var);
}

/* Value extensions.  */
pbrook's avatar
pbrook committed
200
201
#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
202
203
204
#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
205
206
#define gen_sxtb16(var) gen_helper_sxtb16(var, var)
#define gen_uxtb16(var) gen_helper_uxtb16(var, var)
pbrook's avatar
pbrook committed
207

pbrook's avatar
pbrook committed
208

209
210
211
212
213
214
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
215
216
217
218
219
220
221
222
223
224
225
/* 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
226
227
228
229
static void gen_smul_dual(TCGv a, TCGv b)
{
    TCGv tmp1 = new_tmp();
    TCGv tmp2 = new_tmp();
230
231
    tcg_gen_ext16s_i32(tmp1, a);
    tcg_gen_ext16s_i32(tmp2, b);
pbrook's avatar
pbrook committed
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
    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)
{
256
257
258
    tcg_gen_ext16u_i32(var, var);
    tcg_gen_bswap16_i32(var, var);
    tcg_gen_ext16s_i32(var, var);
pbrook's avatar
pbrook committed
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
}

/* 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
288
289
    tcg_gen_shli_i32(val, val, shift);
    tcg_gen_andi_i32(base, base, ~(mask << shift));
pbrook's avatar
pbrook committed
290
291
292
    tcg_gen_or_i32(dest, base, val);
}

293
294
/* Return (b << 32) + a. Mark inputs as dead */
static TCGv_i64 gen_addq_msw(TCGv_i64 a, TCGv b)
pbrook's avatar
pbrook committed
295
{
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
    TCGv_i64 tmp64 = tcg_temp_new_i64();

    tcg_gen_extu_i32_i64(tmp64, b);
    dead_tmp(b);
    tcg_gen_shli_i64(tmp64, tmp64, 32);
    tcg_gen_add_i64(a, tmp64, a);

    tcg_temp_free_i64(tmp64);
    return a;
}

/* Return (b << 32) - a. Mark inputs as dead. */
static TCGv_i64 gen_subq_msw(TCGv_i64 a, TCGv b)
{
    TCGv_i64 tmp64 = tcg_temp_new_i64();

    tcg_gen_extu_i32_i64(tmp64, b);
    dead_tmp(b);
    tcg_gen_shli_i64(tmp64, tmp64, 32);
    tcg_gen_sub_i64(a, tmp64, a);

    tcg_temp_free_i64(tmp64);
    return a;
pbrook's avatar
pbrook committed
319
320
}

pbrook's avatar
pbrook committed
321
322
/* 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
323
/* 32x32->64 multiply.  Marks inputs as dead.  */
pbrook's avatar
pbrook committed
324
static TCGv_i64 gen_mulu_i64_i32(TCGv a, TCGv b)
pbrook's avatar
pbrook committed
325
{
pbrook's avatar
pbrook committed
326
327
    TCGv_i64 tmp1 = tcg_temp_new_i64();
    TCGv_i64 tmp2 = tcg_temp_new_i64();
pbrook's avatar
pbrook committed
328
329
330
331
332
333

    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);
334
    tcg_temp_free_i64(tmp2);
pbrook's avatar
pbrook committed
335
336
337
    return tmp1;
}

pbrook's avatar
pbrook committed
338
static TCGv_i64 gen_muls_i64_i32(TCGv a, TCGv b)
pbrook's avatar
pbrook committed
339
{
pbrook's avatar
pbrook committed
340
341
    TCGv_i64 tmp1 = tcg_temp_new_i64();
    TCGv_i64 tmp2 = tcg_temp_new_i64();
pbrook's avatar
pbrook committed
342
343
344
345
346
347

    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);
348
    tcg_temp_free_i64(tmp2);
pbrook's avatar
pbrook committed
349
350
351
    return tmp1;
}

pbrook's avatar
pbrook committed
352
353
354
355
356
357
358
/* 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
359
    dead_tmp(tmp);
pbrook's avatar
pbrook committed
360
361
}

pbrook's avatar
pbrook committed
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/* 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
382
383
#define gen_set_CF(var) tcg_gen_st_i32(var, cpu_env, offsetof(CPUState, CF))

pbrook's avatar
pbrook committed
384
385
386
387
388
/* 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);
389
    gen_set_CF(tmp);
pbrook's avatar
pbrook committed
390
391
392
393
394
395
    dead_tmp(tmp);
}

/* Set N and Z flags from var.  */
static inline void gen_logic_CC(TCGv var)
{
pbrook's avatar
pbrook committed
396
397
    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
398
399
400
}

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

410
411
412
413
414
415
416
417
418
419
/* 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
420
421
422
/* dest = T0 - T1 + CF - 1.  */
static void gen_sub_carry(TCGv dest, TCGv t0, TCGv t1)
{
pbrook's avatar
pbrook committed
423
    TCGv tmp;
pbrook's avatar
pbrook committed
424
    tcg_gen_sub_i32(dest, t0, t1);
pbrook's avatar
pbrook committed
425
    tmp = load_cpu_field(CF);
pbrook's avatar
pbrook committed
426
427
428
429
430
    tcg_gen_add_i32(dest, dest, tmp);
    tcg_gen_subi_i32(dest, dest, 1);
    dead_tmp(tmp);
}

pbrook's avatar
pbrook committed
431
432
433
/* FIXME:  Implement this natively.  */
#define tcg_gen_abs_i32(t0, t1) gen_helper_abs(t0, t1)

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

pbrook's avatar
pbrook committed
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
481
482
483
484
/* 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);
485
            tcg_gen_rotri_i32(var, var, shift); break;
pbrook's avatar
pbrook committed
486
        } else {
pbrook's avatar
pbrook committed
487
            TCGv tmp = load_cpu_field(CF);
pbrook's avatar
pbrook committed
488
489
490
            if (flags)
                shifter_out_im(var, 0);
            tcg_gen_shri_i32(var, var, 1);
pbrook's avatar
pbrook committed
491
492
493
494
495
496
497
            tcg_gen_shli_i32(tmp, tmp, 31);
            tcg_gen_or_i32(var, var, tmp);
            dead_tmp(tmp);
        }
    }
};

pbrook's avatar
pbrook committed
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
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;
513
514
        case 3: tcg_gen_andi_i32(shift, shift, 0x1f);
                tcg_gen_rotr_i32(var, var, shift); break;
pbrook's avatar
pbrook committed
515
516
517
518
519
        }
    }
    dead_tmp(shift);
}

pbrook's avatar
pbrook committed
520
521
522
523
524
525
526
527
528
#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
529
static void gen_arm_parallel_addsub(int op1, int op2, TCGv a, TCGv b)
pbrook's avatar
pbrook committed
530
{
pbrook's avatar
pbrook committed
531
    TCGv_ptr tmp;
pbrook's avatar
pbrook committed
532
533
534
535

    switch (op1) {
#define gen_pas_helper(name) glue(gen_helper_,name)(a, a, b, tmp)
    case 1:
pbrook's avatar
pbrook committed
536
        tmp = tcg_temp_new_ptr();
pbrook's avatar
pbrook committed
537
538
        tcg_gen_addi_ptr(tmp, cpu_env, offsetof(CPUState, GE));
        PAS_OP(s)
539
        tcg_temp_free_ptr(tmp);
pbrook's avatar
pbrook committed
540
541
        break;
    case 5:
pbrook's avatar
pbrook committed
542
        tmp = tcg_temp_new_ptr();
pbrook's avatar
pbrook committed
543
544
        tcg_gen_addi_ptr(tmp, cpu_env, offsetof(CPUState, GE));
        PAS_OP(u)
545
        tcg_temp_free_ptr(tmp);
pbrook's avatar
pbrook committed
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
        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
564
565
#undef PAS_OP

pbrook's avatar
pbrook committed
566
567
/* For unknown reasons Arm and Thumb-2 use arbitrarily different encodings.  */
#define PAS_OP(pfx) \
568
    switch (op1) {  \
pbrook's avatar
pbrook committed
569
570
571
572
573
574
575
    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
576
static void gen_thumb2_parallel_addsub(int op1, int op2, TCGv a, TCGv b)
pbrook's avatar
pbrook committed
577
{
pbrook's avatar
pbrook committed
578
    TCGv_ptr tmp;
pbrook's avatar
pbrook committed
579

580
    switch (op2) {
pbrook's avatar
pbrook committed
581
582
#define gen_pas_helper(name) glue(gen_helper_,name)(a, a, b, tmp)
    case 0:
pbrook's avatar
pbrook committed
583
        tmp = tcg_temp_new_ptr();
pbrook's avatar
pbrook committed
584
585
        tcg_gen_addi_ptr(tmp, cpu_env, offsetof(CPUState, GE));
        PAS_OP(s)
586
        tcg_temp_free_ptr(tmp);
pbrook's avatar
pbrook committed
587
588
        break;
    case 4:
pbrook's avatar
pbrook committed
589
        tmp = tcg_temp_new_ptr();
pbrook's avatar
pbrook committed
590
591
        tcg_gen_addi_ptr(tmp, cpu_env, offsetof(CPUState, GE));
        PAS_OP(u)
592
        tcg_temp_free_ptr(tmp);
pbrook's avatar
pbrook committed
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
        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
611
612
#undef PAS_OP

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

711
static const uint8_t table_logic_cc[16] = {
bellard's avatar
bellard committed
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
    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 */
};
729

pbrook's avatar
pbrook committed
730
731
/* 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
732
{
pbrook's avatar
pbrook committed
733
    TCGv tmp;
bellard's avatar
bellard committed
734

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

/* 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;
749
750
751
    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
752
753
}

754
755
756
757
758
759
760
761
762
763
764
765
766
/* 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
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
793
794
795
796
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;
}
797
798
799
800
801
802
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
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
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);
}
818
819
820
821
822
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
823

pbrook's avatar
pbrook committed
824
825
static inline void gen_set_pc_im(uint32_t val)
{
826
    tcg_gen_movi_i32(cpu_R[15], val);
pbrook's avatar
pbrook committed
827
828
}

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

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

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

pbrook's avatar
pbrook committed
864
static inline void gen_add_datah_offset(DisasContext *s, unsigned int insn,
pbrook's avatar
pbrook committed
865
                                        int extra, TCGv var)
bellard's avatar
bellard committed
866
867
{
    int val, rm;
pbrook's avatar
pbrook committed
868
    TCGv offset;
869

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

pbrook's avatar
pbrook committed
892
893
894
895
896
897
898
#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
899
900
}

pbrook's avatar
pbrook committed
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
947
948
949
950
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
951
        tcg_gen_movi_i64(cpu_F1d, 0);
pbrook's avatar
pbrook committed
952
    else
balrog's avatar
balrog committed
953
        tcg_gen_movi_i32(cpu_F1s, 0);
pbrook's avatar
pbrook committed
954
955
956
957
958
959
960
961
962
963
964
965
966
}

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)
967
        gen_helper_vfp_sitod(cpu_F0d, cpu_F0s, cpu_env);
pbrook's avatar
pbrook committed
968
    else
969
        gen_helper_vfp_sitos(cpu_F0s, cpu_F0s, cpu_env);
pbrook's avatar
pbrook committed
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
}

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
997
998
{
    if (dp)
pbrook's avatar
pbrook committed
999
        gen_helper_vfp_tosizd(cpu_F0s, cpu_F0d, cpu_env);
pbrook's avatar
pbrook committed
1000
    else
pbrook's avatar
pbrook committed
1001
1002
1003
1004
1005
1006
        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) \
{ \
1007
    TCGv tmp_shift = tcg_const_i32(shift); \
pbrook's avatar
pbrook committed
1008
    if (dp) \
1009
        gen_helper_vfp_##name##d(cpu_F0d, cpu_F0d, tmp_shift, cpu_env);\
pbrook's avatar
pbrook committed
1010
    else \
1011
1012
        gen_helper_vfp_##name##s(cpu_F0s, cpu_F0s, tmp_shift, cpu_env);\
    tcg_temp_free_i32(tmp_shift); \
pbrook's avatar
pbrook committed
1013
}
pbrook's avatar
pbrook committed
1014
1015
1016
1017
1018
1019
1020
1021
1022
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
1023

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

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

bellard's avatar
bellard committed
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
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
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063

/* 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
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
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
1077
static inline void neon_load_reg64(TCGv_i64 var, int reg)
pbrook's avatar
pbrook committed
1078
1079
1080
1081
{
    tcg_gen_ld_i64(var, cpu_env, vfp_reg_offset(1, reg));
}

pbrook's avatar
pbrook committed
1082
static inline void neon_store_reg64(TCGv_i64 var, int reg)
pbrook's avatar
pbrook committed
1083
1084
1085
1086
{
    tcg_gen_st_i64(var, cpu_env, vfp_reg_offset(1, reg));
}

pbrook's avatar
pbrook committed
1087
1088
1089
1090
1091
#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
1092
1093
1094
static inline void gen_mov_F0_vreg(int dp, int reg)
{
    if (dp)
pbrook's avatar
pbrook committed
1095
        tcg_gen_ld_f64(cpu_F0d, cpu_env, vfp_reg_offset(dp, reg));
bellard's avatar
bellard committed
1096
    else
pbrook's avatar
pbrook committed
1097
        tcg_gen_ld_f32(cpu_F0s, cpu_env, vfp_reg_offset(dp, reg));
bellard's avatar
bellard committed
1098
1099
1100
1101
1102
}

static inline void gen_mov_F1_vreg(int dp, int reg)
{
    if (dp)
pbrook's avatar
pbrook committed
1103
        tcg_gen_ld_f64(cpu_F1d, cpu_env, vfp_reg_offset(dp, reg));
bellard's avatar
bellard committed
1104
    else
pbrook's avatar
pbrook committed
1105
        tcg_gen_ld_f32(cpu_F1s, cpu_env, vfp_reg_offset(dp, reg));
bellard's avatar
bellard committed
1106
1107
1108
1109
1110
}

static inline void gen_mov_vreg_F0(int dp, int reg)
{
    if (dp)
pbrook's avatar
pbrook committed
1111
        tcg_gen_st_f64(cpu_F0d, cpu_env, vfp_reg_offset(dp, reg));
bellard's avatar
bellard committed
1112
    else
pbrook's avatar
pbrook committed
1113
        tcg_gen_st_f32(cpu_F0s, cpu_env, vfp_reg_offset(dp, reg));
bellard's avatar
bellard committed
1114
1115
}

1116
1117
#define ARM_CP_RW_BIT	(1 << 20)

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

pbrook's avatar
pbrook committed
1123
static inline void iwmmxt_store_reg(TCGv_i64 var, int reg)
pbrook's avatar
pbrook committed
1124
1125
1126
1127
{
    tcg_gen_st_i64(var, cpu_env, offsetof(CPUState, iwmmxt.regs[reg]));
}

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

1135
static inline void iwmmxt_store_creg(int reg, TCGv var)
pbrook's avatar
pbrook committed
1136
{
1137
    tcg_gen_st_i32(var, cpu_env, offsetof(CPUState, iwmmxt.cregs[reg]));
1138
    dead_tmp(var);
pbrook's avatar
pbrook committed
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
1273
1274
1275
1276
}

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
1277
    tcg_gen_ext32u_i64(cpu_V1, cpu_V1);
pbrook's avatar
pbrook committed
1278
1279
1280
    tcg_gen_add_i64(cpu_M0, cpu_M0, cpu_V1);
}

1281
static inline int gen_iwmmxt_address(DisasContext *s, uint32_t insn, TCGv dest)
1282
1283
1284
{
    int rd;
    uint32_t offset;
1285
    TCGv tmp;
1286
1287

    rd = (insn >> 16) & 0xf;
1288
    tmp = load_reg(s, rd);
1289
1290
1291
1292
1293

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

1315
static inline int gen_iwmmxt_shift(uint32_t insn, uint32_t mask, TCGv dest)
1316
1317
{
    int rd = (insn >> 0) & 0xf;
1318
    TCGv tmp;
1319

1320
1321
    if (insn & (1 << 8)) {
        if (rd < ARM_IWMMXT_wCGR0 || rd > ARM_IWMMXT_wCGR3) {
1322
            return 1;
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
        } 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);
1334
1335
1336
1337
1338
1339
1340
1341
1342
    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;
1343
1344
    TCGv addr;
    TCGv tmp, tmp2, tmp3;
1345
1346
1347
1348
1349
1350
1351

    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 */
1352
1353
1354
1355
                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);
1356
            } else {					/* TMCRR */
1357
1358
                tcg_gen_concat_i32_i64(cpu_V0, cpu_R[rdlo], cpu_R[rdhi]);
                iwmmxt_store_reg(cpu_V0, wrd);
1359
1360
1361
1362
1363
1364
                gen_op_iwmmxt_set_mup();
            }
            return 0;
        }

        wrd = (insn >> 12) & 0xf;
1365
1366
1367
        addr = new_tmp();
        if (gen_iwmmxt_address(s, insn, addr)) {
            dead_tmp(addr);
1368
            return 1;
1369
        }
1370
1371
        if (insn & ARM_CP_RW_BIT) {
            if ((insn >> 28) == 0xf) {			/* WLDRW wCx */
1372
1373
1374
                tmp = new_tmp();
                tcg_gen_qemu_ld32u(tmp, addr, IS_USER(s));
                iwmmxt_store_creg(wrd, tmp);
1375
            } else {
pbrook's avatar
pbrook committed
1376
1377
1378
                i = 1;
                if (insn & (1 << 8)) {
                    if (insn & (1 << 22)) {		/* WLDRD */
1379
                        tcg_gen_qemu_ld64(cpu_M0, addr, IS_USER(s));
pbrook's avatar
pbrook committed
1380
1381
                        i = 0;
                    } else {				/* WLDRW wRd */
1382
                        tmp = gen_ld32(addr, IS_USER(s));
pbrook's avatar
pbrook committed
1383
1384
1385
                    }
                } else {
                    if (insn & (1 << 22)) {		/* WLDRH */
1386
                        tmp = gen_ld16u(addr, IS_USER(s));
pbrook's avatar
pbrook committed
1387
                    } else {				/* WLDRB */
1388
                        tmp = gen_ld8u(addr, IS_USER(s));
pbrook's avatar
pbrook committed
1389
1390
1391
1392
1393
1394
                    }
                }
                if (i) {
                    tcg_gen_extu_i32_i64(cpu_M0, tmp);
                    dead_tmp(tmp);
                }
1395
1396
1397
1398
                gen_op_iwmmxt_movq_wRn_M0(wrd);
            }
        } else {
            if ((insn >> 28) == 0xf) {			/* WSTRW wCx */
1399
1400
                tmp = iwmmxt_load_creg(wrd);
                gen_st32(tmp, addr, IS_USER(s));
1401
1402
            } else {
                gen_op_iwmmxt_movq_M0_wRn(wrd);
pbrook's avatar
pbrook committed
1403
1404
1405
1406
                tmp = new_tmp();
                if (insn & (1 << 8)) {
                    if (insn & (1 << 22)) {		/* WSTRD */
                        dead_tmp(tmp);
1407
                        tcg_gen_qemu_st64(cpu_M0, addr, IS_USER(s));
pbrook's avatar
pbrook committed
1408
1409
                    } else {				/* WSTRW wRd */
                        tcg_gen_trunc_i64_i32(tmp, cpu_M0);
1410
                        gen_st32(tmp, addr, IS_USER(s));
pbrook's avatar
pbrook committed
1411
1412
1413
1414
                    }
                } else {
                    if (insn & (1 << 22)) {		/* WSTRH */
                        tcg_gen_trunc_i64_i32(tmp, cpu_M0);
1415
                        gen_st16(tmp, addr, IS_USER(s));
pbrook's avatar
pbrook committed
1416
1417
                    } else {				/* WSTRB */
                        tcg_gen_trunc_i64_i32(tmp, cpu_M0);
1418
                        gen_st8(tmp, addr, IS_USER(s));
pbrook's avatar
pbrook committed
1419
1420
                    }
                }
1421
1422
            }
        }
1423
        dead_tmp(addr);
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
1451
1452
1453
1454
        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:
1455
1456
            tmp = iwmmxt_load_creg(wrd);
            tmp2 = load_reg(s, rd);
1457
            tcg_gen_andc_i32(tmp, tmp, tmp2);
1458
1459
            dead_tmp(tmp2);
            iwmmxt_store_creg(wrd, tmp);
1460
1461
1462
1463
1464
1465
            break;
        case ARM_IWMMXT_wCGR0:
        case ARM_IWMMXT_wCGR1:
        case ARM_IWMMXT_wCGR2:
        case ARM_IWMMXT_wCGR3:
            gen_op_iwmmxt_set_cup();
1466
1467
            tmp = load_reg(s, rd);
            iwmmxt_store_creg(wrd, tmp);
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
            break;
        default:
            return 1;
        }
        break;
    case 0x100:						/* WXOR */
        wrd = (insn >> 12) & 0xf;
        rd0 = (insn >> 0) & 0xf;
        rd1 = (insn >> 16) & 0xf;
        gen_op_iwmmxt_movq_M0_wRn(rd0);
        gen_op_iwmmxt_xorq_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 0x111:						/* TMRC */
        if (insn & 0xf)
            return 1;
        rd = (insn >> 12) & 0xf;
        wrd = (insn >> 16) & 0xf;
1489
1490
        tmp = iwmmxt_load_creg(wrd);
        store_reg(s, rd, tmp);