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

« back to all changes in this revision

Viewing changes to .pc/ubuntu/arm64/0134-target-arm-A64-Add-SIMD-scalar-3-same-add-sub-and-co.patch/target-arm/translate-a64.c

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

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  AArch64 translation
 
3
 *
 
4
 *  Copyright (c) 2013 Alexander Graf <agraf@suse.de>
 
5
 *
 
6
 * This library is free software; you can redistribute it and/or
 
7
 * modify it under the terms of the GNU Lesser General Public
 
8
 * License as published by the Free Software Foundation; either
 
9
 * version 2 of the License, or (at your option) any later version.
 
10
 *
 
11
 * This library is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
14
 * Lesser General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU Lesser General Public
 
17
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
 
18
 */
 
19
#include <stdarg.h>
 
20
#include <stdlib.h>
 
21
#include <stdio.h>
 
22
#include <string.h>
 
23
#include <inttypes.h>
 
24
 
 
25
#include "cpu.h"
 
26
#include "tcg-op.h"
 
27
#include "qemu/log.h"
 
28
#include "translate.h"
 
29
#include "qemu/host-utils.h"
 
30
 
 
31
#include "exec/gen-icount.h"
 
32
 
 
33
#include "helper.h"
 
34
#define GEN_HELPER 1
 
35
#include "helper.h"
 
36
 
 
37
static TCGv_i64 cpu_X[32];
 
38
static TCGv_i64 cpu_pc;
 
39
static TCGv_i32 cpu_NF, cpu_ZF, cpu_CF, cpu_VF;
 
40
 
 
41
/* Load/store exclusive handling */
 
42
static TCGv_i64 cpu_exclusive_addr;
 
43
static TCGv_i64 cpu_exclusive_val;
 
44
static TCGv_i64 cpu_exclusive_high;
 
45
#ifdef CONFIG_USER_ONLY
 
46
static TCGv_i64 cpu_exclusive_test;
 
47
static TCGv_i32 cpu_exclusive_info;
 
48
#endif
 
49
 
 
50
static const char *regnames[] = {
 
51
    "x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7",
 
52
    "x8", "x9", "x10", "x11", "x12", "x13", "x14", "x15",
 
53
    "x16", "x17", "x18", "x19", "x20", "x21", "x22", "x23",
 
54
    "x24", "x25", "x26", "x27", "x28", "x29", "lr", "sp"
 
55
};
 
56
 
 
57
enum a64_shift_type {
 
58
    A64_SHIFT_TYPE_LSL = 0,
 
59
    A64_SHIFT_TYPE_LSR = 1,
 
60
    A64_SHIFT_TYPE_ASR = 2,
 
61
    A64_SHIFT_TYPE_ROR = 3
 
62
};
 
63
 
 
64
/* Table based decoder typedefs - used when the relevant bits for decode
 
65
 * are too awkwardly scattered across the instruction (eg SIMD).
 
66
 */
 
67
typedef void AArch64DecodeFn(DisasContext *s, uint32_t insn);
 
68
 
 
69
typedef struct AArch64DecodeTable {
 
70
    uint32_t pattern;
 
71
    uint32_t mask;
 
72
    AArch64DecodeFn *disas_fn;
 
73
} AArch64DecodeTable;
 
74
 
 
75
/* initialize TCG globals.  */
 
76
void a64_translate_init(void)
 
77
{
 
78
    int i;
 
79
 
 
80
    cpu_pc = tcg_global_mem_new_i64(TCG_AREG0,
 
81
                                    offsetof(CPUARMState, pc),
 
82
                                    "pc");
 
83
    for (i = 0; i < 32; i++) {
 
84
        cpu_X[i] = tcg_global_mem_new_i64(TCG_AREG0,
 
85
                                          offsetof(CPUARMState, xregs[i]),
 
86
                                          regnames[i]);
 
87
    }
 
88
 
 
89
    cpu_NF = tcg_global_mem_new_i32(TCG_AREG0, offsetof(CPUARMState, NF), "NF");
 
90
    cpu_ZF = tcg_global_mem_new_i32(TCG_AREG0, offsetof(CPUARMState, ZF), "ZF");
 
91
    cpu_CF = tcg_global_mem_new_i32(TCG_AREG0, offsetof(CPUARMState, CF), "CF");
 
92
    cpu_VF = tcg_global_mem_new_i32(TCG_AREG0, offsetof(CPUARMState, VF), "VF");
 
93
 
 
94
    cpu_exclusive_addr = tcg_global_mem_new_i64(TCG_AREG0,
 
95
        offsetof(CPUARMState, exclusive_addr), "exclusive_addr");
 
96
    cpu_exclusive_val = tcg_global_mem_new_i64(TCG_AREG0,
 
97
        offsetof(CPUARMState, exclusive_val), "exclusive_val");
 
98
    cpu_exclusive_high = tcg_global_mem_new_i64(TCG_AREG0,
 
99
        offsetof(CPUARMState, exclusive_high), "exclusive_high");
 
100
#ifdef CONFIG_USER_ONLY
 
101
    cpu_exclusive_test = tcg_global_mem_new_i64(TCG_AREG0,
 
102
        offsetof(CPUARMState, exclusive_test), "exclusive_test");
 
103
    cpu_exclusive_info = tcg_global_mem_new_i32(TCG_AREG0,
 
104
        offsetof(CPUARMState, exclusive_info), "exclusive_info");
 
105
#endif
 
106
}
 
107
 
 
108
void aarch64_cpu_dump_state(CPUState *cs, FILE *f,
 
109
                            fprintf_function cpu_fprintf, int flags)
 
110
{
 
111
    ARMCPU *cpu = ARM_CPU(cs);
 
112
    CPUARMState *env = &cpu->env;
 
113
    uint32_t psr = pstate_read(env);
 
114
    int i;
 
115
 
 
116
    cpu_fprintf(f, "PC=%016"PRIx64"  SP=%016"PRIx64"\n",
 
117
            env->pc, env->xregs[31]);
 
118
    for (i = 0; i < 31; i++) {
 
119
        cpu_fprintf(f, "X%02d=%016"PRIx64, i, env->xregs[i]);
 
120
        if ((i % 4) == 3) {
 
121
            cpu_fprintf(f, "\n");
 
122
        } else {
 
123
            cpu_fprintf(f, " ");
 
124
        }
 
125
    }
 
126
    cpu_fprintf(f, "PSTATE=%08x (flags %c%c%c%c)\n",
 
127
                psr,
 
128
                psr & PSTATE_N ? 'N' : '-',
 
129
                psr & PSTATE_Z ? 'Z' : '-',
 
130
                psr & PSTATE_C ? 'C' : '-',
 
131
                psr & PSTATE_V ? 'V' : '-');
 
132
    cpu_fprintf(f, "\n");
 
133
 
 
134
    if (flags & CPU_DUMP_FPU) {
 
135
        int numvfpregs = 32;
 
136
        for (i = 0; i < numvfpregs; i += 2) {
 
137
            uint64_t vlo = float64_val(env->vfp.regs[i * 2]);
 
138
            uint64_t vhi = float64_val(env->vfp.regs[(i * 2) + 1]);
 
139
            cpu_fprintf(f, "q%02d=%016" PRIx64 ":%016" PRIx64 " ",
 
140
                        i, vhi, vlo);
 
141
            vlo = float64_val(env->vfp.regs[(i + 1) * 2]);
 
142
            vhi = float64_val(env->vfp.regs[((i + 1) * 2) + 1]);
 
143
            cpu_fprintf(f, "q%02d=%016" PRIx64 ":%016" PRIx64 "\n",
 
144
                        i + 1, vhi, vlo);
 
145
        }
 
146
        cpu_fprintf(f, "FPCR: %08x  FPSR: %08x\n",
 
147
                    vfp_get_fpcr(env), vfp_get_fpsr(env));
 
148
    }
 
149
}
 
150
 
 
151
static int get_mem_index(DisasContext *s)
 
152
{
 
153
#ifdef CONFIG_USER_ONLY
 
154
    return 1;
 
155
#else
 
156
    return s->user;
 
157
#endif
 
158
}
 
159
 
 
160
void gen_a64_set_pc_im(uint64_t val)
 
161
{
 
162
    tcg_gen_movi_i64(cpu_pc, val);
 
163
}
 
164
 
 
165
static void gen_exception(int excp)
 
166
{
 
167
    TCGv_i32 tmp = tcg_temp_new_i32();
 
168
    tcg_gen_movi_i32(tmp, excp);
 
169
    gen_helper_exception(cpu_env, tmp);
 
170
    tcg_temp_free_i32(tmp);
 
171
}
 
172
 
 
173
static void gen_exception_insn(DisasContext *s, int offset, int excp)
 
174
{
 
175
    gen_a64_set_pc_im(s->pc - offset);
 
176
    gen_exception(excp);
 
177
    s->is_jmp = DISAS_EXC;
 
178
}
 
179
 
 
180
static inline bool use_goto_tb(DisasContext *s, int n, uint64_t dest)
 
181
{
 
182
    /* No direct tb linking with singlestep or deterministic io */
 
183
    if (s->singlestep_enabled || (s->tb->cflags & CF_LAST_IO)) {
 
184
        return false;
 
185
    }
 
186
 
 
187
    /* Only link tbs from inside the same guest page */
 
188
    if ((s->tb->pc & TARGET_PAGE_MASK) != (dest & TARGET_PAGE_MASK)) {
 
189
        return false;
 
190
    }
 
191
 
 
192
    return true;
 
193
}
 
194
 
 
195
static inline void gen_goto_tb(DisasContext *s, int n, uint64_t dest)
 
196
{
 
197
    TranslationBlock *tb;
 
198
 
 
199
    tb = s->tb;
 
200
    if (use_goto_tb(s, n, dest)) {
 
201
        tcg_gen_goto_tb(n);
 
202
        gen_a64_set_pc_im(dest);
 
203
        tcg_gen_exit_tb((tcg_target_long)tb + n);
 
204
        s->is_jmp = DISAS_TB_JUMP;
 
205
    } else {
 
206
        gen_a64_set_pc_im(dest);
 
207
        if (s->singlestep_enabled) {
 
208
            gen_exception(EXCP_DEBUG);
 
209
        }
 
210
        tcg_gen_exit_tb(0);
 
211
        s->is_jmp = DISAS_JUMP;
 
212
    }
 
213
}
 
214
 
 
215
static void unallocated_encoding(DisasContext *s)
 
216
{
 
217
    gen_exception_insn(s, 4, EXCP_UDEF);
 
218
}
 
219
 
 
220
#define unsupported_encoding(s, insn)                                    \
 
221
    do {                                                                 \
 
222
        qemu_log_mask(LOG_UNIMP,                                         \
 
223
                      "%s:%d: unsupported instruction encoding 0x%08x "  \
 
224
                      "at pc=%016" PRIx64 "\n",                          \
 
225
                      __FILE__, __LINE__, insn, s->pc - 4);              \
 
226
        unallocated_encoding(s);                                         \
 
227
    } while (0);
 
228
 
 
229
static void init_tmp_a64_array(DisasContext *s)
 
230
{
 
231
#ifdef CONFIG_DEBUG_TCG
 
232
    int i;
 
233
    for (i = 0; i < ARRAY_SIZE(s->tmp_a64); i++) {
 
234
        TCGV_UNUSED_I64(s->tmp_a64[i]);
 
235
    }
 
236
#endif
 
237
    s->tmp_a64_count = 0;
 
238
}
 
239
 
 
240
static void free_tmp_a64(DisasContext *s)
 
241
{
 
242
    int i;
 
243
    for (i = 0; i < s->tmp_a64_count; i++) {
 
244
        tcg_temp_free_i64(s->tmp_a64[i]);
 
245
    }
 
246
    init_tmp_a64_array(s);
 
247
}
 
248
 
 
249
static TCGv_i64 new_tmp_a64(DisasContext *s)
 
250
{
 
251
    assert(s->tmp_a64_count < TMP_A64_MAX);
 
252
    return s->tmp_a64[s->tmp_a64_count++] = tcg_temp_new_i64();
 
253
}
 
254
 
 
255
static TCGv_i64 new_tmp_a64_zero(DisasContext *s)
 
256
{
 
257
    TCGv_i64 t = new_tmp_a64(s);
 
258
    tcg_gen_movi_i64(t, 0);
 
259
    return t;
 
260
}
 
261
 
 
262
/*
 
263
 * Register access functions
 
264
 *
 
265
 * These functions are used for directly accessing a register in where
 
266
 * changes to the final register value are likely to be made. If you
 
267
 * need to use a register for temporary calculation (e.g. index type
 
268
 * operations) use the read_* form.
 
269
 *
 
270
 * B1.2.1 Register mappings
 
271
 *
 
272
 * In instruction register encoding 31 can refer to ZR (zero register) or
 
273
 * the SP (stack pointer) depending on context. In QEMU's case we map SP
 
274
 * to cpu_X[31] and ZR accesses to a temporary which can be discarded.
 
275
 * This is the point of the _sp forms.
 
276
 */
 
277
static TCGv_i64 cpu_reg(DisasContext *s, int reg)
 
278
{
 
279
    if (reg == 31) {
 
280
        return new_tmp_a64_zero(s);
 
281
    } else {
 
282
        return cpu_X[reg];
 
283
    }
 
284
}
 
285
 
 
286
/* register access for when 31 == SP */
 
287
static TCGv_i64 cpu_reg_sp(DisasContext *s, int reg)
 
288
{
 
289
    return cpu_X[reg];
 
290
}
 
291
 
 
292
/* read a cpu register in 32bit/64bit mode. Returns a TCGv_i64
 
293
 * representing the register contents. This TCGv is an auto-freed
 
294
 * temporary so it need not be explicitly freed, and may be modified.
 
295
 */
 
296
static TCGv_i64 read_cpu_reg(DisasContext *s, int reg, int sf)
 
297
{
 
298
    TCGv_i64 v = new_tmp_a64(s);
 
299
    if (reg != 31) {
 
300
        if (sf) {
 
301
            tcg_gen_mov_i64(v, cpu_X[reg]);
 
302
        } else {
 
303
            tcg_gen_ext32u_i64(v, cpu_X[reg]);
 
304
        }
 
305
    } else {
 
306
        tcg_gen_movi_i64(v, 0);
 
307
    }
 
308
    return v;
 
309
}
 
310
 
 
311
static TCGv_i64 read_cpu_reg_sp(DisasContext *s, int reg, int sf)
 
312
{
 
313
    TCGv_i64 v = new_tmp_a64(s);
 
314
    if (sf) {
 
315
        tcg_gen_mov_i64(v, cpu_X[reg]);
 
316
    } else {
 
317
        tcg_gen_ext32u_i64(v, cpu_X[reg]);
 
318
    }
 
319
    return v;
 
320
}
 
321
 
 
322
/* Return the offset into CPUARMState of an element of specified
 
323
 * size, 'element' places in from the least significant end of
 
324
 * the FP/vector register Qn.
 
325
 */
 
326
static inline int vec_reg_offset(int regno, int element, TCGMemOp size)
 
327
{
 
328
    int offs = offsetof(CPUARMState, vfp.regs[regno * 2]);
 
329
#ifdef HOST_WORDS_BIGENDIAN
 
330
    /* This is complicated slightly because vfp.regs[2n] is
 
331
     * still the low half and  vfp.regs[2n+1] the high half
 
332
     * of the 128 bit vector, even on big endian systems.
 
333
     * Calculate the offset assuming a fully bigendian 128 bits,
 
334
     * then XOR to account for the order of the two 64 bit halves.
 
335
     */
 
336
    offs += (16 - ((element + 1) * (1 << size)));
 
337
    offs ^= 8;
 
338
#else
 
339
    offs += element * (1 << size);
 
340
#endif
 
341
    return offs;
 
342
}
 
343
 
 
344
/* Return the offset into CPUARMState of a slice (from
 
345
 * the least significant end) of FP register Qn (ie
 
346
 * Dn, Sn, Hn or Bn).
 
347
 * (Note that this is not the same mapping as for A32; see cpu.h)
 
348
 */
 
349
static inline int fp_reg_offset(int regno, TCGMemOp size)
 
350
{
 
351
    int offs = offsetof(CPUARMState, vfp.regs[regno * 2]);
 
352
#ifdef HOST_WORDS_BIGENDIAN
 
353
    offs += (8 - (1 << size));
 
354
#endif
 
355
    return offs;
 
356
}
 
357
 
 
358
/* Offset of the high half of the 128 bit vector Qn */
 
359
static inline int fp_reg_hi_offset(int regno)
 
360
{
 
361
    return offsetof(CPUARMState, vfp.regs[regno * 2 + 1]);
 
362
}
 
363
 
 
364
/* Convenience accessors for reading and writing single and double
 
365
 * FP registers. Writing clears the upper parts of the associated
 
366
 * 128 bit vector register, as required by the architecture.
 
367
 * Note that unlike the GP register accessors, the values returned
 
368
 * by the read functions must be manually freed.
 
369
 */
 
370
static TCGv_i64 read_fp_dreg(DisasContext *s, int reg)
 
371
{
 
372
    TCGv_i64 v = tcg_temp_new_i64();
 
373
 
 
374
    tcg_gen_ld_i64(v, cpu_env, fp_reg_offset(reg, MO_64));
 
375
    return v;
 
376
}
 
377
 
 
378
static TCGv_i32 read_fp_sreg(DisasContext *s, int reg)
 
379
{
 
380
    TCGv_i32 v = tcg_temp_new_i32();
 
381
 
 
382
    tcg_gen_ld_i32(v, cpu_env, fp_reg_offset(reg, MO_32));
 
383
    return v;
 
384
}
 
385
 
 
386
static void write_fp_dreg(DisasContext *s, int reg, TCGv_i64 v)
 
387
{
 
388
    TCGv_i64 tcg_zero = tcg_const_i64(0);
 
389
 
 
390
    tcg_gen_st_i64(v, cpu_env, fp_reg_offset(reg, MO_64));
 
391
    tcg_gen_st_i64(tcg_zero, cpu_env, fp_reg_hi_offset(reg));
 
392
    tcg_temp_free_i64(tcg_zero);
 
393
}
 
394
 
 
395
static void write_fp_sreg(DisasContext *s, int reg, TCGv_i32 v)
 
396
{
 
397
    TCGv_i64 tmp = tcg_temp_new_i64();
 
398
 
 
399
    tcg_gen_extu_i32_i64(tmp, v);
 
400
    write_fp_dreg(s, reg, tmp);
 
401
    tcg_temp_free_i64(tmp);
 
402
}
 
403
 
 
404
static TCGv_ptr get_fpstatus_ptr(void)
 
405
{
 
406
    TCGv_ptr statusptr = tcg_temp_new_ptr();
 
407
    int offset;
 
408
 
 
409
    /* In A64 all instructions (both FP and Neon) use the FPCR;
 
410
     * there is no equivalent of the A32 Neon "standard FPSCR value"
 
411
     * and all operations use vfp.fp_status.
 
412
     */
 
413
    offset = offsetof(CPUARMState, vfp.fp_status);
 
414
    tcg_gen_addi_ptr(statusptr, cpu_env, offset);
 
415
    return statusptr;
 
416
}
 
417
 
 
418
/* Set ZF and NF based on a 64 bit result. This is alas fiddlier
 
419
 * than the 32 bit equivalent.
 
420
 */
 
421
static inline void gen_set_NZ64(TCGv_i64 result)
 
422
{
 
423
    TCGv_i64 flag = tcg_temp_new_i64();
 
424
 
 
425
    tcg_gen_setcondi_i64(TCG_COND_NE, flag, result, 0);
 
426
    tcg_gen_trunc_i64_i32(cpu_ZF, flag);
 
427
    tcg_gen_shri_i64(flag, result, 32);
 
428
    tcg_gen_trunc_i64_i32(cpu_NF, flag);
 
429
    tcg_temp_free_i64(flag);
 
430
}
 
431
 
 
432
/* Set NZCV as for a logical operation: NZ as per result, CV cleared. */
 
433
static inline void gen_logic_CC(int sf, TCGv_i64 result)
 
434
{
 
435
    if (sf) {
 
436
        gen_set_NZ64(result);
 
437
    } else {
 
438
        tcg_gen_trunc_i64_i32(cpu_ZF, result);
 
439
        tcg_gen_trunc_i64_i32(cpu_NF, result);
 
440
    }
 
441
    tcg_gen_movi_i32(cpu_CF, 0);
 
442
    tcg_gen_movi_i32(cpu_VF, 0);
 
443
}
 
444
 
 
445
/* dest = T0 + T1; compute C, N, V and Z flags */
 
446
static void gen_add_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
 
447
{
 
448
    if (sf) {
 
449
        TCGv_i64 result, flag, tmp;
 
450
        result = tcg_temp_new_i64();
 
451
        flag = tcg_temp_new_i64();
 
452
        tmp = tcg_temp_new_i64();
 
453
 
 
454
        tcg_gen_movi_i64(tmp, 0);
 
455
        tcg_gen_add2_i64(result, flag, t0, tmp, t1, tmp);
 
456
 
 
457
        tcg_gen_trunc_i64_i32(cpu_CF, flag);
 
458
 
 
459
        gen_set_NZ64(result);
 
460
 
 
461
        tcg_gen_xor_i64(flag, result, t0);
 
462
        tcg_gen_xor_i64(tmp, t0, t1);
 
463
        tcg_gen_andc_i64(flag, flag, tmp);
 
464
        tcg_temp_free_i64(tmp);
 
465
        tcg_gen_shri_i64(flag, flag, 32);
 
466
        tcg_gen_trunc_i64_i32(cpu_VF, flag);
 
467
 
 
468
        tcg_gen_mov_i64(dest, result);
 
469
        tcg_temp_free_i64(result);
 
470
        tcg_temp_free_i64(flag);
 
471
    } else {
 
472
        /* 32 bit arithmetic */
 
473
        TCGv_i32 t0_32 = tcg_temp_new_i32();
 
474
        TCGv_i32 t1_32 = tcg_temp_new_i32();
 
475
        TCGv_i32 tmp = tcg_temp_new_i32();
 
476
 
 
477
        tcg_gen_movi_i32(tmp, 0);
 
478
        tcg_gen_trunc_i64_i32(t0_32, t0);
 
479
        tcg_gen_trunc_i64_i32(t1_32, t1);
 
480
        tcg_gen_add2_i32(cpu_NF, cpu_CF, t0_32, tmp, t1_32, tmp);
 
481
        tcg_gen_mov_i32(cpu_ZF, cpu_NF);
 
482
        tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
 
483
        tcg_gen_xor_i32(tmp, t0_32, t1_32);
 
484
        tcg_gen_andc_i32(cpu_VF, cpu_VF, tmp);
 
485
        tcg_gen_extu_i32_i64(dest, cpu_NF);
 
486
 
 
487
        tcg_temp_free_i32(tmp);
 
488
        tcg_temp_free_i32(t0_32);
 
489
        tcg_temp_free_i32(t1_32);
 
490
    }
 
491
}
 
492
 
 
493
/* dest = T0 - T1; compute C, N, V and Z flags */
 
494
static void gen_sub_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
 
495
{
 
496
    if (sf) {
 
497
        /* 64 bit arithmetic */
 
498
        TCGv_i64 result, flag, tmp;
 
499
 
 
500
        result = tcg_temp_new_i64();
 
501
        flag = tcg_temp_new_i64();
 
502
        tcg_gen_sub_i64(result, t0, t1);
 
503
 
 
504
        gen_set_NZ64(result);
 
505
 
 
506
        tcg_gen_setcond_i64(TCG_COND_GEU, flag, t0, t1);
 
507
        tcg_gen_trunc_i64_i32(cpu_CF, flag);
 
508
 
 
509
        tcg_gen_xor_i64(flag, result, t0);
 
510
        tmp = tcg_temp_new_i64();
 
511
        tcg_gen_xor_i64(tmp, t0, t1);
 
512
        tcg_gen_and_i64(flag, flag, tmp);
 
513
        tcg_temp_free_i64(tmp);
 
514
        tcg_gen_shri_i64(flag, flag, 32);
 
515
        tcg_gen_trunc_i64_i32(cpu_VF, flag);
 
516
        tcg_gen_mov_i64(dest, result);
 
517
        tcg_temp_free_i64(flag);
 
518
        tcg_temp_free_i64(result);
 
519
    } else {
 
520
        /* 32 bit arithmetic */
 
521
        TCGv_i32 t0_32 = tcg_temp_new_i32();
 
522
        TCGv_i32 t1_32 = tcg_temp_new_i32();
 
523
        TCGv_i32 tmp;
 
524
 
 
525
        tcg_gen_trunc_i64_i32(t0_32, t0);
 
526
        tcg_gen_trunc_i64_i32(t1_32, t1);
 
527
        tcg_gen_sub_i32(cpu_NF, t0_32, t1_32);
 
528
        tcg_gen_mov_i32(cpu_ZF, cpu_NF);
 
529
        tcg_gen_setcond_i32(TCG_COND_GEU, cpu_CF, t0_32, t1_32);
 
530
        tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
 
531
        tmp = tcg_temp_new_i32();
 
532
        tcg_gen_xor_i32(tmp, t0_32, t1_32);
 
533
        tcg_temp_free_i32(t0_32);
 
534
        tcg_temp_free_i32(t1_32);
 
535
        tcg_gen_and_i32(cpu_VF, cpu_VF, tmp);
 
536
        tcg_temp_free_i32(tmp);
 
537
        tcg_gen_extu_i32_i64(dest, cpu_NF);
 
538
    }
 
539
}
 
540
 
 
541
/* dest = T0 + T1 + CF; do not compute flags. */
 
542
static void gen_adc(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
 
543
{
 
544
    TCGv_i64 flag = tcg_temp_new_i64();
 
545
    tcg_gen_extu_i32_i64(flag, cpu_CF);
 
546
    tcg_gen_add_i64(dest, t0, t1);
 
547
    tcg_gen_add_i64(dest, dest, flag);
 
548
    tcg_temp_free_i64(flag);
 
549
 
 
550
    if (!sf) {
 
551
        tcg_gen_ext32u_i64(dest, dest);
 
552
    }
 
553
}
 
554
 
 
555
/* dest = T0 + T1 + CF; compute C, N, V and Z flags. */
 
556
static void gen_adc_CC(int sf, TCGv_i64 dest, TCGv_i64 t0, TCGv_i64 t1)
 
557
{
 
558
    if (sf) {
 
559
        TCGv_i64 result, cf_64, vf_64, tmp;
 
560
        result = tcg_temp_new_i64();
 
561
        cf_64 = tcg_temp_new_i64();
 
562
        vf_64 = tcg_temp_new_i64();
 
563
        tmp = tcg_const_i64(0);
 
564
 
 
565
        tcg_gen_extu_i32_i64(cf_64, cpu_CF);
 
566
        tcg_gen_add2_i64(result, cf_64, t0, tmp, cf_64, tmp);
 
567
        tcg_gen_add2_i64(result, cf_64, result, cf_64, t1, tmp);
 
568
        tcg_gen_trunc_i64_i32(cpu_CF, cf_64);
 
569
        gen_set_NZ64(result);
 
570
 
 
571
        tcg_gen_xor_i64(vf_64, result, t0);
 
572
        tcg_gen_xor_i64(tmp, t0, t1);
 
573
        tcg_gen_andc_i64(vf_64, vf_64, tmp);
 
574
        tcg_gen_shri_i64(vf_64, vf_64, 32);
 
575
        tcg_gen_trunc_i64_i32(cpu_VF, vf_64);
 
576
 
 
577
        tcg_gen_mov_i64(dest, result);
 
578
 
 
579
        tcg_temp_free_i64(tmp);
 
580
        tcg_temp_free_i64(vf_64);
 
581
        tcg_temp_free_i64(cf_64);
 
582
        tcg_temp_free_i64(result);
 
583
    } else {
 
584
        TCGv_i32 t0_32, t1_32, tmp;
 
585
        t0_32 = tcg_temp_new_i32();
 
586
        t1_32 = tcg_temp_new_i32();
 
587
        tmp = tcg_const_i32(0);
 
588
 
 
589
        tcg_gen_trunc_i64_i32(t0_32, t0);
 
590
        tcg_gen_trunc_i64_i32(t1_32, t1);
 
591
        tcg_gen_add2_i32(cpu_NF, cpu_CF, t0_32, tmp, cpu_CF, tmp);
 
592
        tcg_gen_add2_i32(cpu_NF, cpu_CF, cpu_NF, cpu_CF, t1_32, tmp);
 
593
 
 
594
        tcg_gen_mov_i32(cpu_ZF, cpu_NF);
 
595
        tcg_gen_xor_i32(cpu_VF, cpu_NF, t0_32);
 
596
        tcg_gen_xor_i32(tmp, t0_32, t1_32);
 
597
        tcg_gen_andc_i32(cpu_VF, cpu_VF, tmp);
 
598
        tcg_gen_extu_i32_i64(dest, cpu_NF);
 
599
 
 
600
        tcg_temp_free_i32(tmp);
 
601
        tcg_temp_free_i32(t1_32);
 
602
        tcg_temp_free_i32(t0_32);
 
603
    }
 
604
}
 
605
 
 
606
/*
 
607
 * Load/Store generators
 
608
 */
 
609
 
 
610
/*
 
611
 * Store from GPR register to memory
 
612
 */
 
613
static void do_gpr_st(DisasContext *s, TCGv_i64 source,
 
614
                      TCGv_i64 tcg_addr, int size)
 
615
{
 
616
    g_assert(size <= 3);
 
617
    tcg_gen_qemu_st_i64(source, tcg_addr, get_mem_index(s), MO_TE + size);
 
618
}
 
619
 
 
620
/*
 
621
 * Load from memory to GPR register
 
622
 */
 
623
static void do_gpr_ld(DisasContext *s, TCGv_i64 dest, TCGv_i64 tcg_addr,
 
624
                      int size, bool is_signed, bool extend)
 
625
{
 
626
    TCGMemOp memop = MO_TE + size;
 
627
 
 
628
    g_assert(size <= 3);
 
629
 
 
630
    if (is_signed) {
 
631
        memop += MO_SIGN;
 
632
    }
 
633
 
 
634
    tcg_gen_qemu_ld_i64(dest, tcg_addr, get_mem_index(s), memop);
 
635
 
 
636
    if (extend && is_signed) {
 
637
        g_assert(size < 3);
 
638
        tcg_gen_ext32u_i64(dest, dest);
 
639
    }
 
640
}
 
641
 
 
642
/*
 
643
 * Store from FP register to memory
 
644
 */
 
645
static void do_fp_st(DisasContext *s, int srcidx, TCGv_i64 tcg_addr, int size)
 
646
{
 
647
    /* This writes the bottom N bits of a 128 bit wide vector to memory */
 
648
    TCGv_i64 tmp = tcg_temp_new_i64();
 
649
    tcg_gen_ld_i64(tmp, cpu_env, fp_reg_offset(srcidx, MO_64));
 
650
    if (size < 4) {
 
651
        tcg_gen_qemu_st_i64(tmp, tcg_addr, get_mem_index(s), MO_TE + size);
 
652
    } else {
 
653
        TCGv_i64 tcg_hiaddr = tcg_temp_new_i64();
 
654
        tcg_gen_qemu_st_i64(tmp, tcg_addr, get_mem_index(s), MO_TEQ);
 
655
        tcg_gen_qemu_st64(tmp, tcg_addr, get_mem_index(s));
 
656
        tcg_gen_ld_i64(tmp, cpu_env, fp_reg_hi_offset(srcidx));
 
657
        tcg_gen_addi_i64(tcg_hiaddr, tcg_addr, 8);
 
658
        tcg_gen_qemu_st_i64(tmp, tcg_hiaddr, get_mem_index(s), MO_TEQ);
 
659
        tcg_temp_free_i64(tcg_hiaddr);
 
660
    }
 
661
 
 
662
    tcg_temp_free_i64(tmp);
 
663
}
 
664
 
 
665
/*
 
666
 * Load from memory to FP register
 
667
 */
 
668
static void do_fp_ld(DisasContext *s, int destidx, TCGv_i64 tcg_addr, int size)
 
669
{
 
670
    /* This always zero-extends and writes to a full 128 bit wide vector */
 
671
    TCGv_i64 tmplo = tcg_temp_new_i64();
 
672
    TCGv_i64 tmphi;
 
673
 
 
674
    if (size < 4) {
 
675
        TCGMemOp memop = MO_TE + size;
 
676
        tmphi = tcg_const_i64(0);
 
677
        tcg_gen_qemu_ld_i64(tmplo, tcg_addr, get_mem_index(s), memop);
 
678
    } else {
 
679
        TCGv_i64 tcg_hiaddr;
 
680
        tmphi = tcg_temp_new_i64();
 
681
        tcg_hiaddr = tcg_temp_new_i64();
 
682
 
 
683
        tcg_gen_qemu_ld_i64(tmplo, tcg_addr, get_mem_index(s), MO_TEQ);
 
684
        tcg_gen_addi_i64(tcg_hiaddr, tcg_addr, 8);
 
685
        tcg_gen_qemu_ld_i64(tmphi, tcg_hiaddr, get_mem_index(s), MO_TEQ);
 
686
        tcg_temp_free_i64(tcg_hiaddr);
 
687
    }
 
688
 
 
689
    tcg_gen_st_i64(tmplo, cpu_env, fp_reg_offset(destidx, MO_64));
 
690
    tcg_gen_st_i64(tmphi, cpu_env, fp_reg_hi_offset(destidx));
 
691
 
 
692
    tcg_temp_free_i64(tmplo);
 
693
    tcg_temp_free_i64(tmphi);
 
694
}
 
695
 
 
696
/*
 
697
 * Vector load/store helpers.
 
698
 *
 
699
 * The principal difference between this and a FP load is that we don't
 
700
 * zero extend as we are filling a partial chunk of the vector register.
 
701
 * These functions don't support 128 bit loads/stores, which would be
 
702
 * normal load/store operations.
 
703
 *
 
704
 * The _i32 versions are useful when operating on 32 bit quantities
 
705
 * (eg for floating point single or using Neon helper functions).
 
706
 */
 
707
 
 
708
/* Get value of an element within a vector register */
 
709
static void read_vec_element(DisasContext *s, TCGv_i64 tcg_dest, int srcidx,
 
710
                             int element, TCGMemOp memop)
 
711
{
 
712
    int vect_off = vec_reg_offset(srcidx, element, memop & MO_SIZE);
 
713
    switch (memop) {
 
714
    case MO_8:
 
715
        tcg_gen_ld8u_i64(tcg_dest, cpu_env, vect_off);
 
716
        break;
 
717
    case MO_16:
 
718
        tcg_gen_ld16u_i64(tcg_dest, cpu_env, vect_off);
 
719
        break;
 
720
    case MO_32:
 
721
        tcg_gen_ld32u_i64(tcg_dest, cpu_env, vect_off);
 
722
        break;
 
723
    case MO_8|MO_SIGN:
 
724
        tcg_gen_ld8s_i64(tcg_dest, cpu_env, vect_off);
 
725
        break;
 
726
    case MO_16|MO_SIGN:
 
727
        tcg_gen_ld16s_i64(tcg_dest, cpu_env, vect_off);
 
728
        break;
 
729
    case MO_32|MO_SIGN:
 
730
        tcg_gen_ld32s_i64(tcg_dest, cpu_env, vect_off);
 
731
        break;
 
732
    case MO_64:
 
733
    case MO_64|MO_SIGN:
 
734
        tcg_gen_ld_i64(tcg_dest, cpu_env, vect_off);
 
735
        break;
 
736
    default:
 
737
        g_assert_not_reached();
 
738
    }
 
739
}
 
740
 
 
741
static void read_vec_element_i32(DisasContext *s, TCGv_i32 tcg_dest, int srcidx,
 
742
                                 int element, TCGMemOp memop)
 
743
{
 
744
    int vect_off = vec_reg_offset(srcidx, element, memop & MO_SIZE);
 
745
    switch (memop) {
 
746
    case MO_8:
 
747
        tcg_gen_ld8u_i32(tcg_dest, cpu_env, vect_off);
 
748
        break;
 
749
    case MO_16:
 
750
        tcg_gen_ld16u_i32(tcg_dest, cpu_env, vect_off);
 
751
        break;
 
752
    case MO_8|MO_SIGN:
 
753
        tcg_gen_ld8s_i32(tcg_dest, cpu_env, vect_off);
 
754
        break;
 
755
    case MO_16|MO_SIGN:
 
756
        tcg_gen_ld16s_i32(tcg_dest, cpu_env, vect_off);
 
757
        break;
 
758
    case MO_32:
 
759
    case MO_32|MO_SIGN:
 
760
        tcg_gen_ld_i32(tcg_dest, cpu_env, vect_off);
 
761
        break;
 
762
    default:
 
763
        g_assert_not_reached();
 
764
    }
 
765
}
 
766
 
 
767
/* Set value of an element within a vector register */
 
768
static void write_vec_element(DisasContext *s, TCGv_i64 tcg_src, int destidx,
 
769
                              int element, TCGMemOp memop)
 
770
{
 
771
    int vect_off = vec_reg_offset(destidx, element, memop & MO_SIZE);
 
772
    switch (memop) {
 
773
    case MO_8:
 
774
        tcg_gen_st8_i64(tcg_src, cpu_env, vect_off);
 
775
        break;
 
776
    case MO_16:
 
777
        tcg_gen_st16_i64(tcg_src, cpu_env, vect_off);
 
778
        break;
 
779
    case MO_32:
 
780
        tcg_gen_st32_i64(tcg_src, cpu_env, vect_off);
 
781
        break;
 
782
    case MO_64:
 
783
        tcg_gen_st_i64(tcg_src, cpu_env, vect_off);
 
784
        break;
 
785
    default:
 
786
        g_assert_not_reached();
 
787
    }
 
788
}
 
789
 
 
790
/* Clear the high 64 bits of a 128 bit vector (in general non-quad
 
791
 * vector ops all need to do this).
 
792
 */
 
793
static void clear_vec_high(DisasContext *s, int rd)
 
794
{
 
795
    TCGv_i64 tcg_zero = tcg_const_i64(0);
 
796
 
 
797
    write_vec_element(s, tcg_zero, rd, 1, MO_64);
 
798
    tcg_temp_free_i64(tcg_zero);
 
799
}
 
800
 
 
801
/* Store from vector register to memory */
 
802
static void do_vec_st(DisasContext *s, int srcidx, int element,
 
803
                      TCGv_i64 tcg_addr, int size)
 
804
{
 
805
    TCGMemOp memop = MO_TE + size;
 
806
    TCGv_i64 tcg_tmp = tcg_temp_new_i64();
 
807
 
 
808
    read_vec_element(s, tcg_tmp, srcidx, element, size);
 
809
    tcg_gen_qemu_st_i64(tcg_tmp, tcg_addr, get_mem_index(s), memop);
 
810
 
 
811
    tcg_temp_free_i64(tcg_tmp);
 
812
}
 
813
 
 
814
/* Load from memory to vector register */
 
815
static void do_vec_ld(DisasContext *s, int destidx, int element,
 
816
                      TCGv_i64 tcg_addr, int size)
 
817
{
 
818
    TCGMemOp memop = MO_TE + size;
 
819
    TCGv_i64 tcg_tmp = tcg_temp_new_i64();
 
820
 
 
821
    tcg_gen_qemu_ld_i64(tcg_tmp, tcg_addr, get_mem_index(s), memop);
 
822
    write_vec_element(s, tcg_tmp, destidx, element, size);
 
823
 
 
824
    tcg_temp_free_i64(tcg_tmp);
 
825
}
 
826
 
 
827
/*
 
828
 * This utility function is for doing register extension with an
 
829
 * optional shift. You will likely want to pass a temporary for the
 
830
 * destination register. See DecodeRegExtend() in the ARM ARM.
 
831
 */
 
832
static void ext_and_shift_reg(TCGv_i64 tcg_out, TCGv_i64 tcg_in,
 
833
                              int option, unsigned int shift)
 
834
{
 
835
    int extsize = extract32(option, 0, 2);
 
836
    bool is_signed = extract32(option, 2, 1);
 
837
 
 
838
    if (is_signed) {
 
839
        switch (extsize) {
 
840
        case 0:
 
841
            tcg_gen_ext8s_i64(tcg_out, tcg_in);
 
842
            break;
 
843
        case 1:
 
844
            tcg_gen_ext16s_i64(tcg_out, tcg_in);
 
845
            break;
 
846
        case 2:
 
847
            tcg_gen_ext32s_i64(tcg_out, tcg_in);
 
848
            break;
 
849
        case 3:
 
850
            tcg_gen_mov_i64(tcg_out, tcg_in);
 
851
            break;
 
852
        }
 
853
    } else {
 
854
        switch (extsize) {
 
855
        case 0:
 
856
            tcg_gen_ext8u_i64(tcg_out, tcg_in);
 
857
            break;
 
858
        case 1:
 
859
            tcg_gen_ext16u_i64(tcg_out, tcg_in);
 
860
            break;
 
861
        case 2:
 
862
            tcg_gen_ext32u_i64(tcg_out, tcg_in);
 
863
            break;
 
864
        case 3:
 
865
            tcg_gen_mov_i64(tcg_out, tcg_in);
 
866
            break;
 
867
        }
 
868
    }
 
869
 
 
870
    if (shift) {
 
871
        tcg_gen_shli_i64(tcg_out, tcg_out, shift);
 
872
    }
 
873
}
 
874
 
 
875
static inline void gen_check_sp_alignment(DisasContext *s)
 
876
{
 
877
    /* The AArch64 architecture mandates that (if enabled via PSTATE
 
878
     * or SCTLR bits) there is a check that SP is 16-aligned on every
 
879
     * SP-relative load or store (with an exception generated if it is not).
 
880
     * In line with general QEMU practice regarding misaligned accesses,
 
881
     * we omit these checks for the sake of guest program performance.
 
882
     * This function is provided as a hook so we can more easily add these
 
883
     * checks in future (possibly as a "favour catching guest program bugs
 
884
     * over speed" user selectable option).
 
885
     */
 
886
}
 
887
 
 
888
/*
 
889
 * This provides a simple table based table lookup decoder. It is
 
890
 * intended to be used when the relevant bits for decode are too
 
891
 * awkwardly placed and switch/if based logic would be confusing and
 
892
 * deeply nested. Since it's a linear search through the table, tables
 
893
 * should be kept small.
 
894
 *
 
895
 * It returns the first handler where insn & mask == pattern, or
 
896
 * NULL if there is no match.
 
897
 * The table is terminated by an empty mask (i.e. 0)
 
898
 */
 
899
static inline AArch64DecodeFn *lookup_disas_fn(const AArch64DecodeTable *table,
 
900
                                               uint32_t insn)
 
901
{
 
902
    const AArch64DecodeTable *tptr = table;
 
903
 
 
904
    while (tptr->mask) {
 
905
        if ((insn & tptr->mask) == tptr->pattern) {
 
906
            return tptr->disas_fn;
 
907
        }
 
908
        tptr++;
 
909
    }
 
910
    return NULL;
 
911
}
 
912
 
 
913
/*
 
914
 * the instruction disassembly implemented here matches
 
915
 * the instruction encoding classifications in chapter 3 (C3)
 
916
 * of the ARM Architecture Reference Manual (DDI0487A_a)
 
917
 */
 
918
 
 
919
/* C3.2.7 Unconditional branch (immediate)
 
920
 *   31  30       26 25                                  0
 
921
 * +----+-----------+-------------------------------------+
 
922
 * | op | 0 0 1 0 1 |                 imm26               |
 
923
 * +----+-----------+-------------------------------------+
 
924
 */
 
925
static void disas_uncond_b_imm(DisasContext *s, uint32_t insn)
 
926
{
 
927
    uint64_t addr = s->pc + sextract32(insn, 0, 26) * 4 - 4;
 
928
 
 
929
    if (insn & (1 << 31)) {
 
930
        /* C5.6.26 BL Branch with link */
 
931
        tcg_gen_movi_i64(cpu_reg(s, 30), s->pc);
 
932
    }
 
933
 
 
934
    /* C5.6.20 B Branch / C5.6.26 BL Branch with link */
 
935
    gen_goto_tb(s, 0, addr);
 
936
}
 
937
 
 
938
/* C3.2.1 Compare & branch (immediate)
 
939
 *   31  30         25  24  23                  5 4      0
 
940
 * +----+-------------+----+---------------------+--------+
 
941
 * | sf | 0 1 1 0 1 0 | op |         imm19       |   Rt   |
 
942
 * +----+-------------+----+---------------------+--------+
 
943
 */
 
944
static void disas_comp_b_imm(DisasContext *s, uint32_t insn)
 
945
{
 
946
    unsigned int sf, op, rt;
 
947
    uint64_t addr;
 
948
    int label_match;
 
949
    TCGv_i64 tcg_cmp;
 
950
 
 
951
    sf = extract32(insn, 31, 1);
 
952
    op = extract32(insn, 24, 1); /* 0: CBZ; 1: CBNZ */
 
953
    rt = extract32(insn, 0, 5);
 
954
    addr = s->pc + sextract32(insn, 5, 19) * 4 - 4;
 
955
 
 
956
    tcg_cmp = read_cpu_reg(s, rt, sf);
 
957
    label_match = gen_new_label();
 
958
 
 
959
    tcg_gen_brcondi_i64(op ? TCG_COND_NE : TCG_COND_EQ,
 
960
                        tcg_cmp, 0, label_match);
 
961
 
 
962
    gen_goto_tb(s, 0, s->pc);
 
963
    gen_set_label(label_match);
 
964
    gen_goto_tb(s, 1, addr);
 
965
}
 
966
 
 
967
/* C3.2.5 Test & branch (immediate)
 
968
 *   31  30         25  24  23   19 18          5 4    0
 
969
 * +----+-------------+----+-------+-------------+------+
 
970
 * | b5 | 0 1 1 0 1 1 | op |  b40  |    imm14    |  Rt  |
 
971
 * +----+-------------+----+-------+-------------+------+
 
972
 */
 
973
static void disas_test_b_imm(DisasContext *s, uint32_t insn)
 
974
{
 
975
    unsigned int bit_pos, op, rt;
 
976
    uint64_t addr;
 
977
    int label_match;
 
978
    TCGv_i64 tcg_cmp;
 
979
 
 
980
    bit_pos = (extract32(insn, 31, 1) << 5) | extract32(insn, 19, 5);
 
981
    op = extract32(insn, 24, 1); /* 0: TBZ; 1: TBNZ */
 
982
    addr = s->pc + sextract32(insn, 5, 14) * 4 - 4;
 
983
    rt = extract32(insn, 0, 5);
 
984
 
 
985
    tcg_cmp = tcg_temp_new_i64();
 
986
    tcg_gen_andi_i64(tcg_cmp, cpu_reg(s, rt), (1ULL << bit_pos));
 
987
    label_match = gen_new_label();
 
988
    tcg_gen_brcondi_i64(op ? TCG_COND_NE : TCG_COND_EQ,
 
989
                        tcg_cmp, 0, label_match);
 
990
    tcg_temp_free_i64(tcg_cmp);
 
991
    gen_goto_tb(s, 0, s->pc);
 
992
    gen_set_label(label_match);
 
993
    gen_goto_tb(s, 1, addr);
 
994
}
 
995
 
 
996
/* C3.2.2 / C5.6.19 Conditional branch (immediate)
 
997
 *  31           25  24  23                  5   4  3    0
 
998
 * +---------------+----+---------------------+----+------+
 
999
 * | 0 1 0 1 0 1 0 | o1 |         imm19       | o0 | cond |
 
1000
 * +---------------+----+---------------------+----+------+
 
1001
 */
 
1002
static void disas_cond_b_imm(DisasContext *s, uint32_t insn)
 
1003
{
 
1004
    unsigned int cond;
 
1005
    uint64_t addr;
 
1006
 
 
1007
    if ((insn & (1 << 4)) || (insn & (1 << 24))) {
 
1008
        unallocated_encoding(s);
 
1009
        return;
 
1010
    }
 
1011
    addr = s->pc + sextract32(insn, 5, 19) * 4 - 4;
 
1012
    cond = extract32(insn, 0, 4);
 
1013
 
 
1014
    if (cond < 0x0e) {
 
1015
        /* genuinely conditional branches */
 
1016
        int label_match = gen_new_label();
 
1017
        arm_gen_test_cc(cond, label_match);
 
1018
        gen_goto_tb(s, 0, s->pc);
 
1019
        gen_set_label(label_match);
 
1020
        gen_goto_tb(s, 1, addr);
 
1021
    } else {
 
1022
        /* 0xe and 0xf are both "always" conditions */
 
1023
        gen_goto_tb(s, 0, addr);
 
1024
    }
 
1025
}
 
1026
 
 
1027
/* C5.6.68 HINT */
 
1028
static void handle_hint(DisasContext *s, uint32_t insn,
 
1029
                        unsigned int op1, unsigned int op2, unsigned int crm)
 
1030
{
 
1031
    unsigned int selector = crm << 3 | op2;
 
1032
 
 
1033
    if (op1 != 3) {
 
1034
        unallocated_encoding(s);
 
1035
        return;
 
1036
    }
 
1037
 
 
1038
    switch (selector) {
 
1039
    case 0: /* NOP */
 
1040
        return;
 
1041
    case 1: /* YIELD */
 
1042
    case 2: /* WFE */
 
1043
    case 3: /* WFI */
 
1044
    case 4: /* SEV */
 
1045
    case 5: /* SEVL */
 
1046
        /* we treat all as NOP at least for now */
 
1047
        return;
 
1048
    default:
 
1049
        /* default specified as NOP equivalent */
 
1050
        return;
 
1051
    }
 
1052
}
 
1053
 
 
1054
static void gen_clrex(DisasContext *s, uint32_t insn)
 
1055
{
 
1056
    tcg_gen_movi_i64(cpu_exclusive_addr, -1);
 
1057
}
 
1058
 
 
1059
/* CLREX, DSB, DMB, ISB */
 
1060
static void handle_sync(DisasContext *s, uint32_t insn,
 
1061
                        unsigned int op1, unsigned int op2, unsigned int crm)
 
1062
{
 
1063
    if (op1 != 3) {
 
1064
        unallocated_encoding(s);
 
1065
        return;
 
1066
    }
 
1067
 
 
1068
    switch (op2) {
 
1069
    case 2: /* CLREX */
 
1070
        gen_clrex(s, insn);
 
1071
        return;
 
1072
    case 4: /* DSB */
 
1073
    case 5: /* DMB */
 
1074
    case 6: /* ISB */
 
1075
        /* We don't emulate caches so barriers are no-ops */
 
1076
        return;
 
1077
    default:
 
1078
        unallocated_encoding(s);
 
1079
        return;
 
1080
    }
 
1081
}
 
1082
 
 
1083
/* C5.6.130 MSR (immediate) - move immediate to processor state field */
 
1084
static void handle_msr_i(DisasContext *s, uint32_t insn,
 
1085
                         unsigned int op1, unsigned int op2, unsigned int crm)
 
1086
{
 
1087
    unsupported_encoding(s, insn);
 
1088
}
 
1089
 
 
1090
static void gen_get_nzcv(TCGv_i64 tcg_rt)
 
1091
{
 
1092
    TCGv_i32 tmp = tcg_temp_new_i32();
 
1093
    TCGv_i32 nzcv = tcg_temp_new_i32();
 
1094
 
 
1095
    /* build bit 31, N */
 
1096
    tcg_gen_andi_i32(nzcv, cpu_NF, (1 << 31));
 
1097
    /* build bit 30, Z */
 
1098
    tcg_gen_setcondi_i32(TCG_COND_EQ, tmp, cpu_ZF, 0);
 
1099
    tcg_gen_deposit_i32(nzcv, nzcv, tmp, 30, 1);
 
1100
    /* build bit 29, C */
 
1101
    tcg_gen_deposit_i32(nzcv, nzcv, cpu_CF, 29, 1);
 
1102
    /* build bit 28, V */
 
1103
    tcg_gen_shri_i32(tmp, cpu_VF, 31);
 
1104
    tcg_gen_deposit_i32(nzcv, nzcv, tmp, 28, 1);
 
1105
    /* generate result */
 
1106
    tcg_gen_extu_i32_i64(tcg_rt, nzcv);
 
1107
 
 
1108
    tcg_temp_free_i32(nzcv);
 
1109
    tcg_temp_free_i32(tmp);
 
1110
}
 
1111
 
 
1112
static void gen_set_nzcv(TCGv_i64 tcg_rt)
 
1113
 
 
1114
{
 
1115
    TCGv_i32 nzcv = tcg_temp_new_i32();
 
1116
 
 
1117
    /* take NZCV from R[t] */
 
1118
    tcg_gen_trunc_i64_i32(nzcv, tcg_rt);
 
1119
 
 
1120
    /* bit 31, N */
 
1121
    tcg_gen_andi_i32(cpu_NF, nzcv, (1 << 31));
 
1122
    /* bit 30, Z */
 
1123
    tcg_gen_andi_i32(cpu_ZF, nzcv, (1 << 30));
 
1124
    tcg_gen_setcondi_i32(TCG_COND_EQ, cpu_ZF, cpu_ZF, 0);
 
1125
    /* bit 29, C */
 
1126
    tcg_gen_andi_i32(cpu_CF, nzcv, (1 << 29));
 
1127
    tcg_gen_shri_i32(cpu_CF, cpu_CF, 29);
 
1128
    /* bit 28, V */
 
1129
    tcg_gen_andi_i32(cpu_VF, nzcv, (1 << 28));
 
1130
    tcg_gen_shli_i32(cpu_VF, cpu_VF, 3);
 
1131
    tcg_temp_free_i32(nzcv);
 
1132
}
 
1133
 
 
1134
/* C5.6.129 MRS - move from system register
 
1135
 * C5.6.131 MSR (register) - move to system register
 
1136
 * C5.6.204 SYS
 
1137
 * C5.6.205 SYSL
 
1138
 * These are all essentially the same insn in 'read' and 'write'
 
1139
 * versions, with varying op0 fields.
 
1140
 */
 
1141
static void handle_sys(DisasContext *s, uint32_t insn, bool isread,
 
1142
                       unsigned int op0, unsigned int op1, unsigned int op2,
 
1143
                       unsigned int crn, unsigned int crm, unsigned int rt)
 
1144
{
 
1145
    const ARMCPRegInfo *ri;
 
1146
    TCGv_i64 tcg_rt;
 
1147
 
 
1148
    ri = get_arm_cp_reginfo(s->cp_regs,
 
1149
                            ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP,
 
1150
                                               crn, crm, op0, op1, op2));
 
1151
 
 
1152
    if (!ri) {
 
1153
        /* Unknown register */
 
1154
        unallocated_encoding(s);
 
1155
        return;
 
1156
    }
 
1157
 
 
1158
    /* Check access permissions */
 
1159
    if (!cp_access_ok(s->current_pl, ri, isread)) {
 
1160
        unallocated_encoding(s);
 
1161
        return;
 
1162
    }
 
1163
 
 
1164
    /* Handle special cases first */
 
1165
    switch (ri->type & ~(ARM_CP_FLAG_MASK & ~ARM_CP_SPECIAL)) {
 
1166
    case ARM_CP_NOP:
 
1167
        return;
 
1168
    case ARM_CP_NZCV:
 
1169
        tcg_rt = cpu_reg(s, rt);
 
1170
        if (isread) {
 
1171
            gen_get_nzcv(tcg_rt);
 
1172
        } else {
 
1173
            gen_set_nzcv(tcg_rt);
 
1174
        }
 
1175
        return;
 
1176
    default:
 
1177
        break;
 
1178
    }
 
1179
 
 
1180
    if (use_icount && (ri->type & ARM_CP_IO)) {
 
1181
        gen_io_start();
 
1182
    }
 
1183
 
 
1184
    tcg_rt = cpu_reg(s, rt);
 
1185
 
 
1186
    if (isread) {
 
1187
        if (ri->type & ARM_CP_CONST) {
 
1188
            tcg_gen_movi_i64(tcg_rt, ri->resetvalue);
 
1189
        } else if (ri->readfn) {
 
1190
            TCGv_ptr tmpptr;
 
1191
            gen_a64_set_pc_im(s->pc - 4);
 
1192
            tmpptr = tcg_const_ptr(ri);
 
1193
            gen_helper_get_cp_reg64(tcg_rt, cpu_env, tmpptr);
 
1194
            tcg_temp_free_ptr(tmpptr);
 
1195
        } else {
 
1196
            tcg_gen_ld_i64(tcg_rt, cpu_env, ri->fieldoffset);
 
1197
        }
 
1198
    } else {
 
1199
        if (ri->type & ARM_CP_CONST) {
 
1200
            /* If not forbidden by access permissions, treat as WI */
 
1201
            return;
 
1202
        } else if (ri->writefn) {
 
1203
            TCGv_ptr tmpptr;
 
1204
            gen_a64_set_pc_im(s->pc - 4);
 
1205
            tmpptr = tcg_const_ptr(ri);
 
1206
            gen_helper_set_cp_reg64(cpu_env, tmpptr, tcg_rt);
 
1207
            tcg_temp_free_ptr(tmpptr);
 
1208
        } else {
 
1209
            tcg_gen_st_i64(tcg_rt, cpu_env, ri->fieldoffset);
 
1210
        }
 
1211
    }
 
1212
 
 
1213
    if (use_icount && (ri->type & ARM_CP_IO)) {
 
1214
        /* I/O operations must end the TB here (whether read or write) */
 
1215
        gen_io_end();
 
1216
        s->is_jmp = DISAS_UPDATE;
 
1217
    } else if (!isread && !(ri->type & ARM_CP_SUPPRESS_TB_END)) {
 
1218
        /* We default to ending the TB on a coprocessor register write,
 
1219
         * but allow this to be suppressed by the register definition
 
1220
         * (usually only necessary to work around guest bugs).
 
1221
         */
 
1222
        s->is_jmp = DISAS_UPDATE;
 
1223
    }
 
1224
}
 
1225
 
 
1226
/* C3.2.4 System
 
1227
 *  31                 22 21  20 19 18 16 15   12 11    8 7   5 4    0
 
1228
 * +---------------------+---+-----+-----+-------+-------+-----+------+
 
1229
 * | 1 1 0 1 0 1 0 1 0 0 | L | op0 | op1 |  CRn  |  CRm  | op2 |  Rt  |
 
1230
 * +---------------------+---+-----+-----+-------+-------+-----+------+
 
1231
 */
 
1232
static void disas_system(DisasContext *s, uint32_t insn)
 
1233
{
 
1234
    unsigned int l, op0, op1, crn, crm, op2, rt;
 
1235
    l = extract32(insn, 21, 1);
 
1236
    op0 = extract32(insn, 19, 2);
 
1237
    op1 = extract32(insn, 16, 3);
 
1238
    crn = extract32(insn, 12, 4);
 
1239
    crm = extract32(insn, 8, 4);
 
1240
    op2 = extract32(insn, 5, 3);
 
1241
    rt = extract32(insn, 0, 5);
 
1242
 
 
1243
    if (op0 == 0) {
 
1244
        if (l || rt != 31) {
 
1245
            unallocated_encoding(s);
 
1246
            return;
 
1247
        }
 
1248
        switch (crn) {
 
1249
        case 2: /* C5.6.68 HINT */
 
1250
            handle_hint(s, insn, op1, op2, crm);
 
1251
            break;
 
1252
        case 3: /* CLREX, DSB, DMB, ISB */
 
1253
            handle_sync(s, insn, op1, op2, crm);
 
1254
            break;
 
1255
        case 4: /* C5.6.130 MSR (immediate) */
 
1256
            handle_msr_i(s, insn, op1, op2, crm);
 
1257
            break;
 
1258
        default:
 
1259
            unallocated_encoding(s);
 
1260
            break;
 
1261
        }
 
1262
        return;
 
1263
    }
 
1264
    handle_sys(s, insn, l, op0, op1, op2, crn, crm, rt);
 
1265
}
 
1266
 
 
1267
/* C3.2.3 Exception generation
 
1268
 *
 
1269
 *  31             24 23 21 20                     5 4   2 1  0
 
1270
 * +-----------------+-----+------------------------+-----+----+
 
1271
 * | 1 1 0 1 0 1 0 0 | opc |          imm16         | op2 | LL |
 
1272
 * +-----------------------+------------------------+----------+
 
1273
 */
 
1274
static void disas_exc(DisasContext *s, uint32_t insn)
 
1275
{
 
1276
    int opc = extract32(insn, 21, 3);
 
1277
    int op2_ll = extract32(insn, 0, 5);
 
1278
 
 
1279
    switch (opc) {
 
1280
    case 0:
 
1281
        /* SVC, HVC, SMC; since we don't support the Virtualization
 
1282
         * or TrustZone extensions these all UNDEF except SVC.
 
1283
         */
 
1284
        if (op2_ll != 1) {
 
1285
            unallocated_encoding(s);
 
1286
            break;
 
1287
        }
 
1288
        gen_exception_insn(s, 0, EXCP_SWI);
 
1289
        break;
 
1290
    case 1:
 
1291
        if (op2_ll != 0) {
 
1292
            unallocated_encoding(s);
 
1293
            break;
 
1294
        }
 
1295
        /* BRK */
 
1296
        gen_exception_insn(s, 0, EXCP_BKPT);
 
1297
        break;
 
1298
    case 2:
 
1299
        if (op2_ll != 0) {
 
1300
            unallocated_encoding(s);
 
1301
            break;
 
1302
        }
 
1303
        /* HLT */
 
1304
        unsupported_encoding(s, insn);
 
1305
        break;
 
1306
    case 5:
 
1307
        if (op2_ll < 1 || op2_ll > 3) {
 
1308
            unallocated_encoding(s);
 
1309
            break;
 
1310
        }
 
1311
        /* DCPS1, DCPS2, DCPS3 */
 
1312
        unsupported_encoding(s, insn);
 
1313
        break;
 
1314
    default:
 
1315
        unallocated_encoding(s);
 
1316
        break;
 
1317
    }
 
1318
}
 
1319
 
 
1320
/* C3.2.7 Unconditional branch (register)
 
1321
 *  31           25 24   21 20   16 15   10 9    5 4     0
 
1322
 * +---------------+-------+-------+-------+------+-------+
 
1323
 * | 1 1 0 1 0 1 1 |  opc  |  op2  |  op3  |  Rn  |  op4  |
 
1324
 * +---------------+-------+-------+-------+------+-------+
 
1325
 */
 
1326
static void disas_uncond_b_reg(DisasContext *s, uint32_t insn)
 
1327
{
 
1328
    unsigned int opc, op2, op3, rn, op4;
 
1329
 
 
1330
    opc = extract32(insn, 21, 4);
 
1331
    op2 = extract32(insn, 16, 5);
 
1332
    op3 = extract32(insn, 10, 6);
 
1333
    rn = extract32(insn, 5, 5);
 
1334
    op4 = extract32(insn, 0, 5);
 
1335
 
 
1336
    if (op4 != 0x0 || op3 != 0x0 || op2 != 0x1f) {
 
1337
        unallocated_encoding(s);
 
1338
        return;
 
1339
    }
 
1340
 
 
1341
    switch (opc) {
 
1342
    case 0: /* BR */
 
1343
    case 2: /* RET */
 
1344
        break;
 
1345
    case 1: /* BLR */
 
1346
        tcg_gen_movi_i64(cpu_reg(s, 30), s->pc);
 
1347
        break;
 
1348
    case 4: /* ERET */
 
1349
    case 5: /* DRPS */
 
1350
        if (rn != 0x1f) {
 
1351
            unallocated_encoding(s);
 
1352
        } else {
 
1353
            unsupported_encoding(s, insn);
 
1354
        }
 
1355
        return;
 
1356
    default:
 
1357
        unallocated_encoding(s);
 
1358
        return;
 
1359
    }
 
1360
 
 
1361
    tcg_gen_mov_i64(cpu_pc, cpu_reg(s, rn));
 
1362
    s->is_jmp = DISAS_JUMP;
 
1363
}
 
1364
 
 
1365
/* C3.2 Branches, exception generating and system instructions */
 
1366
static void disas_b_exc_sys(DisasContext *s, uint32_t insn)
 
1367
{
 
1368
    switch (extract32(insn, 25, 7)) {
 
1369
    case 0x0a: case 0x0b:
 
1370
    case 0x4a: case 0x4b: /* Unconditional branch (immediate) */
 
1371
        disas_uncond_b_imm(s, insn);
 
1372
        break;
 
1373
    case 0x1a: case 0x5a: /* Compare & branch (immediate) */
 
1374
        disas_comp_b_imm(s, insn);
 
1375
        break;
 
1376
    case 0x1b: case 0x5b: /* Test & branch (immediate) */
 
1377
        disas_test_b_imm(s, insn);
 
1378
        break;
 
1379
    case 0x2a: /* Conditional branch (immediate) */
 
1380
        disas_cond_b_imm(s, insn);
 
1381
        break;
 
1382
    case 0x6a: /* Exception generation / System */
 
1383
        if (insn & (1 << 24)) {
 
1384
            disas_system(s, insn);
 
1385
        } else {
 
1386
            disas_exc(s, insn);
 
1387
        }
 
1388
        break;
 
1389
    case 0x6b: /* Unconditional branch (register) */
 
1390
        disas_uncond_b_reg(s, insn);
 
1391
        break;
 
1392
    default:
 
1393
        unallocated_encoding(s);
 
1394
        break;
 
1395
    }
 
1396
}
 
1397
 
 
1398
/*
 
1399
 * Load/Store exclusive instructions are implemented by remembering
 
1400
 * the value/address loaded, and seeing if these are the same
 
1401
 * when the store is performed. This is not actually the architecturally
 
1402
 * mandated semantics, but it works for typical guest code sequences
 
1403
 * and avoids having to monitor regular stores.
 
1404
 *
 
1405
 * In system emulation mode only one CPU will be running at once, so
 
1406
 * this sequence is effectively atomic.  In user emulation mode we
 
1407
 * throw an exception and handle the atomic operation elsewhere.
 
1408
 */
 
1409
static void gen_load_exclusive(DisasContext *s, int rt, int rt2,
 
1410
                               TCGv_i64 addr, int size, bool is_pair)
 
1411
{
 
1412
    TCGv_i64 tmp = tcg_temp_new_i64();
 
1413
    TCGMemOp memop = MO_TE + size;
 
1414
 
 
1415
    g_assert(size <= 3);
 
1416
    tcg_gen_qemu_ld_i64(tmp, addr, get_mem_index(s), memop);
 
1417
 
 
1418
    if (is_pair) {
 
1419
        TCGv_i64 addr2 = tcg_temp_new_i64();
 
1420
        TCGv_i64 hitmp = tcg_temp_new_i64();
 
1421
 
 
1422
        g_assert(size >= 2);
 
1423
        tcg_gen_addi_i64(addr2, addr, 1 << size);
 
1424
        tcg_gen_qemu_ld_i64(hitmp, addr2, get_mem_index(s), memop);
 
1425
        tcg_temp_free_i64(addr2);
 
1426
        tcg_gen_mov_i64(cpu_exclusive_high, hitmp);
 
1427
        tcg_gen_mov_i64(cpu_reg(s, rt2), hitmp);
 
1428
        tcg_temp_free_i64(hitmp);
 
1429
    }
 
1430
 
 
1431
    tcg_gen_mov_i64(cpu_exclusive_val, tmp);
 
1432
    tcg_gen_mov_i64(cpu_reg(s, rt), tmp);
 
1433
 
 
1434
    tcg_temp_free_i64(tmp);
 
1435
    tcg_gen_mov_i64(cpu_exclusive_addr, addr);
 
1436
}
 
1437
 
 
1438
#ifdef CONFIG_USER_ONLY
 
1439
static void gen_store_exclusive(DisasContext *s, int rd, int rt, int rt2,
 
1440
                                TCGv_i64 addr, int size, int is_pair)
 
1441
{
 
1442
    tcg_gen_mov_i64(cpu_exclusive_test, addr);
 
1443
    tcg_gen_movi_i32(cpu_exclusive_info,
 
1444
                     size | is_pair << 2 | (rd << 4) | (rt << 9) | (rt2 << 14));
 
1445
    gen_exception_insn(s, 4, EXCP_STREX);
 
1446
}
 
1447
#else
 
1448
static void gen_store_exclusive(DisasContext *s, int rd, int rt, int rt2,
 
1449
                                TCGv_i64 addr, int size, int is_pair)
 
1450
{
 
1451
    qemu_log_mask(LOG_UNIMP,
 
1452
                  "%s:%d: system mode store_exclusive unsupported "
 
1453
                  "at pc=%016" PRIx64 "\n",
 
1454
                  __FILE__, __LINE__, s->pc - 4);
 
1455
}
 
1456
#endif
 
1457
 
 
1458
/* C3.3.6 Load/store exclusive
 
1459
 *
 
1460
 *  31 30 29         24  23  22   21  20  16  15  14   10 9    5 4    0
 
1461
 * +-----+-------------+----+---+----+------+----+-------+------+------+
 
1462
 * | sz  | 0 0 1 0 0 0 | o2 | L | o1 |  Rs  | o0 |  Rt2  |  Rn  | Rt   |
 
1463
 * +-----+-------------+----+---+----+------+----+-------+------+------+
 
1464
 *
 
1465
 *  sz: 00 -> 8 bit, 01 -> 16 bit, 10 -> 32 bit, 11 -> 64 bit
 
1466
 *   L: 0 -> store, 1 -> load
 
1467
 *  o2: 0 -> exclusive, 1 -> not
 
1468
 *  o1: 0 -> single register, 1 -> register pair
 
1469
 *  o0: 1 -> load-acquire/store-release, 0 -> not
 
1470
 *
 
1471
 *  o0 == 0 AND o2 == 1 is un-allocated
 
1472
 *  o1 == 1 is un-allocated except for 32 and 64 bit sizes
 
1473
 */
 
1474
static void disas_ldst_excl(DisasContext *s, uint32_t insn)
 
1475
{
 
1476
    int rt = extract32(insn, 0, 5);
 
1477
    int rn = extract32(insn, 5, 5);
 
1478
    int rt2 = extract32(insn, 10, 5);
 
1479
    int is_lasr = extract32(insn, 15, 1);
 
1480
    int rs = extract32(insn, 16, 5);
 
1481
    int is_pair = extract32(insn, 21, 1);
 
1482
    int is_store = !extract32(insn, 22, 1);
 
1483
    int is_excl = !extract32(insn, 23, 1);
 
1484
    int size = extract32(insn, 30, 2);
 
1485
    TCGv_i64 tcg_addr;
 
1486
 
 
1487
    if ((!is_excl && !is_lasr) ||
 
1488
        (is_pair && size < 2)) {
 
1489
        unallocated_encoding(s);
 
1490
        return;
 
1491
    }
 
1492
 
 
1493
    if (rn == 31) {
 
1494
        gen_check_sp_alignment(s);
 
1495
    }
 
1496
    tcg_addr = read_cpu_reg_sp(s, rn, 1);
 
1497
 
 
1498
    /* Note that since TCG is single threaded load-acquire/store-release
 
1499
     * semantics require no extra if (is_lasr) { ... } handling.
 
1500
     */
 
1501
 
 
1502
    if (is_excl) {
 
1503
        if (!is_store) {
 
1504
            gen_load_exclusive(s, rt, rt2, tcg_addr, size, is_pair);
 
1505
        } else {
 
1506
            gen_store_exclusive(s, rs, rt, rt2, tcg_addr, size, is_pair);
 
1507
        }
 
1508
    } else {
 
1509
        TCGv_i64 tcg_rt = cpu_reg(s, rt);
 
1510
        if (is_store) {
 
1511
            do_gpr_st(s, tcg_rt, tcg_addr, size);
 
1512
        } else {
 
1513
            do_gpr_ld(s, tcg_rt, tcg_addr, size, false, false);
 
1514
        }
 
1515
        if (is_pair) {
 
1516
            TCGv_i64 tcg_rt2 = cpu_reg(s, rt);
 
1517
            tcg_gen_addi_i64(tcg_addr, tcg_addr, 1 << size);
 
1518
            if (is_store) {
 
1519
                do_gpr_st(s, tcg_rt2, tcg_addr, size);
 
1520
            } else {
 
1521
                do_gpr_ld(s, tcg_rt2, tcg_addr, size, false, false);
 
1522
            }
 
1523
        }
 
1524
    }
 
1525
}
 
1526
 
 
1527
/*
 
1528
 * C3.3.5 Load register (literal)
 
1529
 *
 
1530
 *  31 30 29   27  26 25 24 23                5 4     0
 
1531
 * +-----+-------+---+-----+-------------------+-------+
 
1532
 * | opc | 0 1 1 | V | 0 0 |     imm19         |  Rt   |
 
1533
 * +-----+-------+---+-----+-------------------+-------+
 
1534
 *
 
1535
 * V: 1 -> vector (simd/fp)
 
1536
 * opc (non-vector): 00 -> 32 bit, 01 -> 64 bit,
 
1537
 *                   10-> 32 bit signed, 11 -> prefetch
 
1538
 * opc (vector): 00 -> 32 bit, 01 -> 64 bit, 10 -> 128 bit (11 unallocated)
 
1539
 */
 
1540
static void disas_ld_lit(DisasContext *s, uint32_t insn)
 
1541
{
 
1542
    int rt = extract32(insn, 0, 5);
 
1543
    int64_t imm = sextract32(insn, 5, 19) << 2;
 
1544
    bool is_vector = extract32(insn, 26, 1);
 
1545
    int opc = extract32(insn, 30, 2);
 
1546
    bool is_signed = false;
 
1547
    int size = 2;
 
1548
    TCGv_i64 tcg_rt, tcg_addr;
 
1549
 
 
1550
    if (is_vector) {
 
1551
        if (opc == 3) {
 
1552
            unallocated_encoding(s);
 
1553
            return;
 
1554
        }
 
1555
        size = 2 + opc;
 
1556
    } else {
 
1557
        if (opc == 3) {
 
1558
            /* PRFM (literal) : prefetch */
 
1559
            return;
 
1560
        }
 
1561
        size = 2 + extract32(opc, 0, 1);
 
1562
        is_signed = extract32(opc, 1, 1);
 
1563
    }
 
1564
 
 
1565
    tcg_rt = cpu_reg(s, rt);
 
1566
 
 
1567
    tcg_addr = tcg_const_i64((s->pc - 4) + imm);
 
1568
    if (is_vector) {
 
1569
        do_fp_ld(s, rt, tcg_addr, size);
 
1570
    } else {
 
1571
        do_gpr_ld(s, tcg_rt, tcg_addr, size, is_signed, false);
 
1572
    }
 
1573
    tcg_temp_free_i64(tcg_addr);
 
1574
}
 
1575
 
 
1576
/*
 
1577
 * C5.6.80 LDNP (Load Pair - non-temporal hint)
 
1578
 * C5.6.81 LDP (Load Pair - non vector)
 
1579
 * C5.6.82 LDPSW (Load Pair Signed Word - non vector)
 
1580
 * C5.6.176 STNP (Store Pair - non-temporal hint)
 
1581
 * C5.6.177 STP (Store Pair - non vector)
 
1582
 * C6.3.165 LDNP (Load Pair of SIMD&FP - non-temporal hint)
 
1583
 * C6.3.165 LDP (Load Pair of SIMD&FP)
 
1584
 * C6.3.284 STNP (Store Pair of SIMD&FP - non-temporal hint)
 
1585
 * C6.3.284 STP (Store Pair of SIMD&FP)
 
1586
 *
 
1587
 *  31 30 29   27  26  25 24   23  22 21   15 14   10 9    5 4    0
 
1588
 * +-----+-------+---+---+-------+---+-----------------------------+
 
1589
 * | opc | 1 0 1 | V | 0 | index | L |  imm7 |  Rt2  |  Rn  | Rt   |
 
1590
 * +-----+-------+---+---+-------+---+-------+-------+------+------+
 
1591
 *
 
1592
 * opc: LDP/STP/LDNP/STNP        00 -> 32 bit, 10 -> 64 bit
 
1593
 *      LDPSW                    01
 
1594
 *      LDP/STP/LDNP/STNP (SIMD) 00 -> 32 bit, 01 -> 64 bit, 10 -> 128 bit
 
1595
 *   V: 0 -> GPR, 1 -> Vector
 
1596
 * idx: 00 -> signed offset with non-temporal hint, 01 -> post-index,
 
1597
 *      10 -> signed offset, 11 -> pre-index
 
1598
 *   L: 0 -> Store 1 -> Load
 
1599
 *
 
1600
 * Rt, Rt2 = GPR or SIMD registers to be stored
 
1601
 * Rn = general purpose register containing address
 
1602
 * imm7 = signed offset (multiple of 4 or 8 depending on size)
 
1603
 */
 
1604
static void disas_ldst_pair(DisasContext *s, uint32_t insn)
 
1605
{
 
1606
    int rt = extract32(insn, 0, 5);
 
1607
    int rn = extract32(insn, 5, 5);
 
1608
    int rt2 = extract32(insn, 10, 5);
 
1609
    int64_t offset = sextract32(insn, 15, 7);
 
1610
    int index = extract32(insn, 23, 2);
 
1611
    bool is_vector = extract32(insn, 26, 1);
 
1612
    bool is_load = extract32(insn, 22, 1);
 
1613
    int opc = extract32(insn, 30, 2);
 
1614
 
 
1615
    bool is_signed = false;
 
1616
    bool postindex = false;
 
1617
    bool wback = false;
 
1618
 
 
1619
    TCGv_i64 tcg_addr; /* calculated address */
 
1620
    int size;
 
1621
 
 
1622
    if (opc == 3) {
 
1623
        unallocated_encoding(s);
 
1624
        return;
 
1625
    }
 
1626
 
 
1627
    if (is_vector) {
 
1628
        size = 2 + opc;
 
1629
    } else {
 
1630
        size = 2 + extract32(opc, 1, 1);
 
1631
        is_signed = extract32(opc, 0, 1);
 
1632
        if (!is_load && is_signed) {
 
1633
            unallocated_encoding(s);
 
1634
            return;
 
1635
        }
 
1636
    }
 
1637
 
 
1638
    switch (index) {
 
1639
    case 1: /* post-index */
 
1640
        postindex = true;
 
1641
        wback = true;
 
1642
        break;
 
1643
    case 0:
 
1644
        /* signed offset with "non-temporal" hint. Since we don't emulate
 
1645
         * caches we don't care about hints to the cache system about
 
1646
         * data access patterns, and handle this identically to plain
 
1647
         * signed offset.
 
1648
         */
 
1649
        if (is_signed) {
 
1650
            /* There is no non-temporal-hint version of LDPSW */
 
1651
            unallocated_encoding(s);
 
1652
            return;
 
1653
        }
 
1654
        postindex = false;
 
1655
        break;
 
1656
    case 2: /* signed offset, rn not updated */
 
1657
        postindex = false;
 
1658
        break;
 
1659
    case 3: /* pre-index */
 
1660
        postindex = false;
 
1661
        wback = true;
 
1662
        break;
 
1663
    }
 
1664
 
 
1665
    offset <<= size;
 
1666
 
 
1667
    if (rn == 31) {
 
1668
        gen_check_sp_alignment(s);
 
1669
    }
 
1670
 
 
1671
    tcg_addr = read_cpu_reg_sp(s, rn, 1);
 
1672
 
 
1673
    if (!postindex) {
 
1674
        tcg_gen_addi_i64(tcg_addr, tcg_addr, offset);
 
1675
    }
 
1676
 
 
1677
    if (is_vector) {
 
1678
        if (is_load) {
 
1679
            do_fp_ld(s, rt, tcg_addr, size);
 
1680
        } else {
 
1681
            do_fp_st(s, rt, tcg_addr, size);
 
1682
        }
 
1683
    } else {
 
1684
        TCGv_i64 tcg_rt = cpu_reg(s, rt);
 
1685
        if (is_load) {
 
1686
            do_gpr_ld(s, tcg_rt, tcg_addr, size, is_signed, false);
 
1687
        } else {
 
1688
            do_gpr_st(s, tcg_rt, tcg_addr, size);
 
1689
        }
 
1690
    }
 
1691
    tcg_gen_addi_i64(tcg_addr, tcg_addr, 1 << size);
 
1692
    if (is_vector) {
 
1693
        if (is_load) {
 
1694
            do_fp_ld(s, rt2, tcg_addr, size);
 
1695
        } else {
 
1696
            do_fp_st(s, rt2, tcg_addr, size);
 
1697
        }
 
1698
    } else {
 
1699
        TCGv_i64 tcg_rt2 = cpu_reg(s, rt2);
 
1700
        if (is_load) {
 
1701
            do_gpr_ld(s, tcg_rt2, tcg_addr, size, is_signed, false);
 
1702
        } else {
 
1703
            do_gpr_st(s, tcg_rt2, tcg_addr, size);
 
1704
        }
 
1705
    }
 
1706
 
 
1707
    if (wback) {
 
1708
        if (postindex) {
 
1709
            tcg_gen_addi_i64(tcg_addr, tcg_addr, offset - (1 << size));
 
1710
        } else {
 
1711
            tcg_gen_subi_i64(tcg_addr, tcg_addr, 1 << size);
 
1712
        }
 
1713
        tcg_gen_mov_i64(cpu_reg_sp(s, rn), tcg_addr);
 
1714
    }
 
1715
}
 
1716
 
 
1717
/*
 
1718
 * C3.3.8 Load/store (immediate post-indexed)
 
1719
 * C3.3.9 Load/store (immediate pre-indexed)
 
1720
 * C3.3.12 Load/store (unscaled immediate)
 
1721
 *
 
1722
 * 31 30 29   27  26 25 24 23 22 21  20    12 11 10 9    5 4    0
 
1723
 * +----+-------+---+-----+-----+---+--------+-----+------+------+
 
1724
 * |size| 1 1 1 | V | 0 0 | opc | 0 |  imm9  | idx |  Rn  |  Rt  |
 
1725
 * +----+-------+---+-----+-----+---+--------+-----+------+------+
 
1726
 *
 
1727
 * idx = 01 -> post-indexed, 11 pre-indexed, 00 unscaled imm. (no writeback)
 
1728
 * V = 0 -> non-vector
 
1729
 * size: 00 -> 8 bit, 01 -> 16 bit, 10 -> 32 bit, 11 -> 64bit
 
1730
 * opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
 
1731
 */
 
1732
static void disas_ldst_reg_imm9(DisasContext *s, uint32_t insn)
 
1733
{
 
1734
    int rt = extract32(insn, 0, 5);
 
1735
    int rn = extract32(insn, 5, 5);
 
1736
    int imm9 = sextract32(insn, 12, 9);
 
1737
    int opc = extract32(insn, 22, 2);
 
1738
    int size = extract32(insn, 30, 2);
 
1739
    int idx = extract32(insn, 10, 2);
 
1740
    bool is_signed = false;
 
1741
    bool is_store = false;
 
1742
    bool is_extended = false;
 
1743
    bool is_vector = extract32(insn, 26, 1);
 
1744
    bool post_index;
 
1745
    bool writeback;
 
1746
 
 
1747
    TCGv_i64 tcg_addr;
 
1748
 
 
1749
    if (is_vector) {
 
1750
        size |= (opc & 2) << 1;
 
1751
        if (size > 4) {
 
1752
            unallocated_encoding(s);
 
1753
            return;
 
1754
        }
 
1755
        is_store = ((opc & 1) == 0);
 
1756
    } else {
 
1757
        if (size == 3 && opc == 2) {
 
1758
            /* PRFM - prefetch */
 
1759
            return;
 
1760
        }
 
1761
        if (opc == 3 && size > 1) {
 
1762
            unallocated_encoding(s);
 
1763
            return;
 
1764
        }
 
1765
        is_store = (opc == 0);
 
1766
        is_signed = opc & (1<<1);
 
1767
        is_extended = (size < 3) && (opc & 1);
 
1768
    }
 
1769
 
 
1770
    switch (idx) {
 
1771
    case 0:
 
1772
        post_index = false;
 
1773
        writeback = false;
 
1774
        break;
 
1775
    case 1:
 
1776
        post_index = true;
 
1777
        writeback = true;
 
1778
        break;
 
1779
    case 3:
 
1780
        post_index = false;
 
1781
        writeback = true;
 
1782
        break;
 
1783
    case 2:
 
1784
        g_assert(false);
 
1785
        break;
 
1786
    }
 
1787
 
 
1788
    if (rn == 31) {
 
1789
        gen_check_sp_alignment(s);
 
1790
    }
 
1791
    tcg_addr = read_cpu_reg_sp(s, rn, 1);
 
1792
 
 
1793
    if (!post_index) {
 
1794
        tcg_gen_addi_i64(tcg_addr, tcg_addr, imm9);
 
1795
    }
 
1796
 
 
1797
    if (is_vector) {
 
1798
        if (is_store) {
 
1799
            do_fp_st(s, rt, tcg_addr, size);
 
1800
        } else {
 
1801
            do_fp_ld(s, rt, tcg_addr, size);
 
1802
        }
 
1803
    } else {
 
1804
        TCGv_i64 tcg_rt = cpu_reg(s, rt);
 
1805
        if (is_store) {
 
1806
            do_gpr_st(s, tcg_rt, tcg_addr, size);
 
1807
        } else {
 
1808
            do_gpr_ld(s, tcg_rt, tcg_addr, size, is_signed, is_extended);
 
1809
        }
 
1810
    }
 
1811
 
 
1812
    if (writeback) {
 
1813
        TCGv_i64 tcg_rn = cpu_reg_sp(s, rn);
 
1814
        if (post_index) {
 
1815
            tcg_gen_addi_i64(tcg_addr, tcg_addr, imm9);
 
1816
        }
 
1817
        tcg_gen_mov_i64(tcg_rn, tcg_addr);
 
1818
    }
 
1819
}
 
1820
 
 
1821
/*
 
1822
 * C3.3.10 Load/store (register offset)
 
1823
 *
 
1824
 * 31 30 29   27  26 25 24 23 22 21  20  16 15 13 12 11 10 9  5 4  0
 
1825
 * +----+-------+---+-----+-----+---+------+-----+--+-----+----+----+
 
1826
 * |size| 1 1 1 | V | 0 0 | opc | 1 |  Rm  | opt | S| 1 0 | Rn | Rt |
 
1827
 * +----+-------+---+-----+-----+---+------+-----+--+-----+----+----+
 
1828
 *
 
1829
 * For non-vector:
 
1830
 *   size: 00-> byte, 01 -> 16 bit, 10 -> 32bit, 11 -> 64bit
 
1831
 *   opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
 
1832
 * For vector:
 
1833
 *   size is opc<1>:size<1:0> so 100 -> 128 bit; 110 and 111 unallocated
 
1834
 *   opc<0>: 0 -> store, 1 -> load
 
1835
 * V: 1 -> vector/simd
 
1836
 * opt: extend encoding (see DecodeRegExtend)
 
1837
 * S: if S=1 then scale (essentially index by sizeof(size))
 
1838
 * Rt: register to transfer into/out of
 
1839
 * Rn: address register or SP for base
 
1840
 * Rm: offset register or ZR for offset
 
1841
 */
 
1842
static void disas_ldst_reg_roffset(DisasContext *s, uint32_t insn)
 
1843
{
 
1844
    int rt = extract32(insn, 0, 5);
 
1845
    int rn = extract32(insn, 5, 5);
 
1846
    int shift = extract32(insn, 12, 1);
 
1847
    int rm = extract32(insn, 16, 5);
 
1848
    int opc = extract32(insn, 22, 2);
 
1849
    int opt = extract32(insn, 13, 3);
 
1850
    int size = extract32(insn, 30, 2);
 
1851
    bool is_signed = false;
 
1852
    bool is_store = false;
 
1853
    bool is_extended = false;
 
1854
    bool is_vector = extract32(insn, 26, 1);
 
1855
 
 
1856
    TCGv_i64 tcg_rm;
 
1857
    TCGv_i64 tcg_addr;
 
1858
 
 
1859
    if (extract32(opt, 1, 1) == 0) {
 
1860
        unallocated_encoding(s);
 
1861
        return;
 
1862
    }
 
1863
 
 
1864
    if (is_vector) {
 
1865
        size |= (opc & 2) << 1;
 
1866
        if (size > 4) {
 
1867
            unallocated_encoding(s);
 
1868
            return;
 
1869
        }
 
1870
        is_store = !extract32(opc, 0, 1);
 
1871
    } else {
 
1872
        if (size == 3 && opc == 2) {
 
1873
            /* PRFM - prefetch */
 
1874
            return;
 
1875
        }
 
1876
        if (opc == 3 && size > 1) {
 
1877
            unallocated_encoding(s);
 
1878
            return;
 
1879
        }
 
1880
        is_store = (opc == 0);
 
1881
        is_signed = extract32(opc, 1, 1);
 
1882
        is_extended = (size < 3) && extract32(opc, 0, 1);
 
1883
    }
 
1884
 
 
1885
    if (rn == 31) {
 
1886
        gen_check_sp_alignment(s);
 
1887
    }
 
1888
    tcg_addr = read_cpu_reg_sp(s, rn, 1);
 
1889
 
 
1890
    tcg_rm = read_cpu_reg(s, rm, 1);
 
1891
    ext_and_shift_reg(tcg_rm, tcg_rm, opt, shift ? size : 0);
 
1892
 
 
1893
    tcg_gen_add_i64(tcg_addr, tcg_addr, tcg_rm);
 
1894
 
 
1895
    if (is_vector) {
 
1896
        if (is_store) {
 
1897
            do_fp_st(s, rt, tcg_addr, size);
 
1898
        } else {
 
1899
            do_fp_ld(s, rt, tcg_addr, size);
 
1900
        }
 
1901
    } else {
 
1902
        TCGv_i64 tcg_rt = cpu_reg(s, rt);
 
1903
        if (is_store) {
 
1904
            do_gpr_st(s, tcg_rt, tcg_addr, size);
 
1905
        } else {
 
1906
            do_gpr_ld(s, tcg_rt, tcg_addr, size, is_signed, is_extended);
 
1907
        }
 
1908
    }
 
1909
}
 
1910
 
 
1911
/*
 
1912
 * C3.3.13 Load/store (unsigned immediate)
 
1913
 *
 
1914
 * 31 30 29   27  26 25 24 23 22 21        10 9     5
 
1915
 * +----+-------+---+-----+-----+------------+-------+------+
 
1916
 * |size| 1 1 1 | V | 0 1 | opc |   imm12    |  Rn   |  Rt  |
 
1917
 * +----+-------+---+-----+-----+------------+-------+------+
 
1918
 *
 
1919
 * For non-vector:
 
1920
 *   size: 00-> byte, 01 -> 16 bit, 10 -> 32bit, 11 -> 64bit
 
1921
 *   opc: 00 -> store, 01 -> loadu, 10 -> loads 64, 11 -> loads 32
 
1922
 * For vector:
 
1923
 *   size is opc<1>:size<1:0> so 100 -> 128 bit; 110 and 111 unallocated
 
1924
 *   opc<0>: 0 -> store, 1 -> load
 
1925
 * Rn: base address register (inc SP)
 
1926
 * Rt: target register
 
1927
 */
 
1928
static void disas_ldst_reg_unsigned_imm(DisasContext *s, uint32_t insn)
 
1929
{
 
1930
    int rt = extract32(insn, 0, 5);
 
1931
    int rn = extract32(insn, 5, 5);
 
1932
    unsigned int imm12 = extract32(insn, 10, 12);
 
1933
    bool is_vector = extract32(insn, 26, 1);
 
1934
    int size = extract32(insn, 30, 2);
 
1935
    int opc = extract32(insn, 22, 2);
 
1936
    unsigned int offset;
 
1937
 
 
1938
    TCGv_i64 tcg_addr;
 
1939
 
 
1940
    bool is_store;
 
1941
    bool is_signed = false;
 
1942
    bool is_extended = false;
 
1943
 
 
1944
    if (is_vector) {
 
1945
        size |= (opc & 2) << 1;
 
1946
        if (size > 4) {
 
1947
            unallocated_encoding(s);
 
1948
            return;
 
1949
        }
 
1950
        is_store = !extract32(opc, 0, 1);
 
1951
    } else {
 
1952
        if (size == 3 && opc == 2) {
 
1953
            /* PRFM - prefetch */
 
1954
            return;
 
1955
        }
 
1956
        if (opc == 3 && size > 1) {
 
1957
            unallocated_encoding(s);
 
1958
            return;
 
1959
        }
 
1960
        is_store = (opc == 0);
 
1961
        is_signed = extract32(opc, 1, 1);
 
1962
        is_extended = (size < 3) && extract32(opc, 0, 1);
 
1963
    }
 
1964
 
 
1965
    if (rn == 31) {
 
1966
        gen_check_sp_alignment(s);
 
1967
    }
 
1968
    tcg_addr = read_cpu_reg_sp(s, rn, 1);
 
1969
    offset = imm12 << size;
 
1970
    tcg_gen_addi_i64(tcg_addr, tcg_addr, offset);
 
1971
 
 
1972
    if (is_vector) {
 
1973
        if (is_store) {
 
1974
            do_fp_st(s, rt, tcg_addr, size);
 
1975
        } else {
 
1976
            do_fp_ld(s, rt, tcg_addr, size);
 
1977
        }
 
1978
    } else {
 
1979
        TCGv_i64 tcg_rt = cpu_reg(s, rt);
 
1980
        if (is_store) {
 
1981
            do_gpr_st(s, tcg_rt, tcg_addr, size);
 
1982
        } else {
 
1983
            do_gpr_ld(s, tcg_rt, tcg_addr, size, is_signed, is_extended);
 
1984
        }
 
1985
    }
 
1986
}
 
1987
 
 
1988
/* Load/store register (immediate forms) */
 
1989
static void disas_ldst_reg_imm(DisasContext *s, uint32_t insn)
 
1990
{
 
1991
    switch (extract32(insn, 10, 2)) {
 
1992
    case 0: case 1: case 3:
 
1993
        /* Load/store register (unscaled immediate) */
 
1994
        /* Load/store immediate pre/post-indexed */
 
1995
        disas_ldst_reg_imm9(s, insn);
 
1996
        break;
 
1997
    case 2:
 
1998
        /* Load/store register unprivileged */
 
1999
        unsupported_encoding(s, insn);
 
2000
        break;
 
2001
    default:
 
2002
        unallocated_encoding(s);
 
2003
        break;
 
2004
    }
 
2005
}
 
2006
 
 
2007
/* Load/store register (all forms) */
 
2008
static void disas_ldst_reg(DisasContext *s, uint32_t insn)
 
2009
{
 
2010
    switch (extract32(insn, 24, 2)) {
 
2011
    case 0:
 
2012
        if (extract32(insn, 21, 1) == 1 && extract32(insn, 10, 2) == 2) {
 
2013
            disas_ldst_reg_roffset(s, insn);
 
2014
        } else {
 
2015
            disas_ldst_reg_imm(s, insn);
 
2016
        }
 
2017
        break;
 
2018
    case 1:
 
2019
        disas_ldst_reg_unsigned_imm(s, insn);
 
2020
        break;
 
2021
    default:
 
2022
        unallocated_encoding(s);
 
2023
        break;
 
2024
    }
 
2025
}
 
2026
 
 
2027
/* C3.3.1 AdvSIMD load/store multiple structures
 
2028
 *
 
2029
 *  31  30  29           23 22  21         16 15    12 11  10 9    5 4    0
 
2030
 * +---+---+---------------+---+-------------+--------+------+------+------+
 
2031
 * | 0 | Q | 0 0 1 1 0 0 0 | L | 0 0 0 0 0 0 | opcode | size |  Rn  |  Rt  |
 
2032
 * +---+---+---------------+---+-------------+--------+------+------+------+
 
2033
 *
 
2034
 * C3.3.2 AdvSIMD load/store multiple structures (post-indexed)
 
2035
 *
 
2036
 *  31  30  29           23 22  21  20     16 15    12 11  10 9    5 4    0
 
2037
 * +---+---+---------------+---+---+---------+--------+------+------+------+
 
2038
 * | 0 | Q | 0 0 1 1 0 0 1 | L | 0 |   Rm    | opcode | size |  Rn  |  Rt  |
 
2039
 * +---+---+---------------+---+---+---------+--------+------+------+------+
 
2040
 *
 
2041
 * Rt: first (or only) SIMD&FP register to be transferred
 
2042
 * Rn: base address or SP
 
2043
 * Rm (post-index only): post-index register (when !31) or size dependent #imm
 
2044
 */
 
2045
static void disas_ldst_multiple_struct(DisasContext *s, uint32_t insn)
 
2046
{
 
2047
    int rt = extract32(insn, 0, 5);
 
2048
    int rn = extract32(insn, 5, 5);
 
2049
    int size = extract32(insn, 10, 2);
 
2050
    int opcode = extract32(insn, 12, 4);
 
2051
    bool is_store = !extract32(insn, 22, 1);
 
2052
    bool is_postidx = extract32(insn, 23, 1);
 
2053
    bool is_q = extract32(insn, 30, 1);
 
2054
    TCGv_i64 tcg_addr, tcg_rn;
 
2055
 
 
2056
    int ebytes = 1 << size;
 
2057
    int elements = (is_q ? 128 : 64) / (8 << size);
 
2058
    int rpt;    /* num iterations */
 
2059
    int selem;  /* structure elements */
 
2060
    int r;
 
2061
 
 
2062
    if (extract32(insn, 31, 1) || extract32(insn, 21, 1)) {
 
2063
        unallocated_encoding(s);
 
2064
        return;
 
2065
    }
 
2066
 
 
2067
    /* From the shared decode logic */
 
2068
    switch (opcode) {
 
2069
    case 0x0:
 
2070
        rpt = 1;
 
2071
        selem = 4;
 
2072
        break;
 
2073
    case 0x2:
 
2074
        rpt = 4;
 
2075
        selem = 1;
 
2076
        break;
 
2077
    case 0x4:
 
2078
        rpt = 1;
 
2079
        selem = 3;
 
2080
        break;
 
2081
    case 0x6:
 
2082
        rpt = 3;
 
2083
        selem = 1;
 
2084
        break;
 
2085
    case 0x7:
 
2086
        rpt = 1;
 
2087
        selem = 1;
 
2088
        break;
 
2089
    case 0x8:
 
2090
        rpt = 1;
 
2091
        selem = 2;
 
2092
        break;
 
2093
    case 0xa:
 
2094
        rpt = 2;
 
2095
        selem = 1;
 
2096
        break;
 
2097
    default:
 
2098
        unallocated_encoding(s);
 
2099
        return;
 
2100
    }
 
2101
 
 
2102
    if (size == 3 && !is_q && selem != 1) {
 
2103
        /* reserved */
 
2104
        unallocated_encoding(s);
 
2105
        return;
 
2106
    }
 
2107
 
 
2108
    if (rn == 31) {
 
2109
        gen_check_sp_alignment(s);
 
2110
    }
 
2111
 
 
2112
    tcg_rn = cpu_reg_sp(s, rn);
 
2113
    tcg_addr = tcg_temp_new_i64();
 
2114
    tcg_gen_mov_i64(tcg_addr, tcg_rn);
 
2115
 
 
2116
    for (r = 0; r < rpt; r++) {
 
2117
        int e;
 
2118
        for (e = 0; e < elements; e++) {
 
2119
            int tt = (rt + r) % 32;
 
2120
            int xs;
 
2121
            for (xs = 0; xs < selem; xs++) {
 
2122
                if (is_store) {
 
2123
                    do_vec_st(s, tt, e, tcg_addr, size);
 
2124
                } else {
 
2125
                    do_vec_ld(s, tt, e, tcg_addr, size);
 
2126
 
 
2127
                    /* For non-quad operations, setting a slice of the low
 
2128
                     * 64 bits of the register clears the high 64 bits (in
 
2129
                     * the ARM ARM pseudocode this is implicit in the fact
 
2130
                     * that 'rval' is a 64 bit wide variable). We optimize
 
2131
                     * by noticing that we only need to do this the first
 
2132
                     * time we touch a register.
 
2133
                     */
 
2134
                    if (!is_q && e == 0 && (r == 0 || xs == selem - 1)) {
 
2135
                        clear_vec_high(s, tt);
 
2136
                    }
 
2137
                }
 
2138
                tcg_gen_addi_i64(tcg_addr, tcg_addr, ebytes);
 
2139
                tt = (tt + 1) % 32;
 
2140
            }
 
2141
        }
 
2142
    }
 
2143
 
 
2144
    if (is_postidx) {
 
2145
        int rm = extract32(insn, 16, 5);
 
2146
        if (rm == 31) {
 
2147
            tcg_gen_mov_i64(tcg_rn, tcg_addr);
 
2148
        } else {
 
2149
            tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, rm));
 
2150
        }
 
2151
    }
 
2152
    tcg_temp_free_i64(tcg_addr);
 
2153
}
 
2154
 
 
2155
/* C3.3.3 AdvSIMD load/store single structure
 
2156
 *
 
2157
 *  31  30  29           23 22 21 20       16 15 13 12  11  10 9    5 4    0
 
2158
 * +---+---+---------------+-----+-----------+-----+---+------+------+------+
 
2159
 * | 0 | Q | 0 0 1 1 0 1 0 | L R | 0 0 0 0 0 | opc | S | size |  Rn  |  Rt  |
 
2160
 * +---+---+---------------+-----+-----------+-----+---+------+------+------+
 
2161
 *
 
2162
 * C3.3.4 AdvSIMD load/store single structure (post-indexed)
 
2163
 *
 
2164
 *  31  30  29           23 22 21 20       16 15 13 12  11  10 9    5 4    0
 
2165
 * +---+---+---------------+-----+-----------+-----+---+------+------+------+
 
2166
 * | 0 | Q | 0 0 1 1 0 1 1 | L R |     Rm    | opc | S | size |  Rn  |  Rt  |
 
2167
 * +---+---+---------------+-----+-----------+-----+---+------+------+------+
 
2168
 *
 
2169
 * Rt: first (or only) SIMD&FP register to be transferred
 
2170
 * Rn: base address or SP
 
2171
 * Rm (post-index only): post-index register (when !31) or size dependent #imm
 
2172
 * index = encoded in Q:S:size dependent on size
 
2173
 *
 
2174
 * lane_size = encoded in R, opc
 
2175
 * transfer width = encoded in opc, S, size
 
2176
 */
 
2177
static void disas_ldst_single_struct(DisasContext *s, uint32_t insn)
 
2178
{
 
2179
    int rt = extract32(insn, 0, 5);
 
2180
    int rn = extract32(insn, 5, 5);
 
2181
    int size = extract32(insn, 10, 2);
 
2182
    int S = extract32(insn, 12, 1);
 
2183
    int opc = extract32(insn, 13, 3);
 
2184
    int R = extract32(insn, 21, 1);
 
2185
    int is_load = extract32(insn, 22, 1);
 
2186
    int is_postidx = extract32(insn, 23, 1);
 
2187
    int is_q = extract32(insn, 30, 1);
 
2188
 
 
2189
    int scale = extract32(opc, 1, 2);
 
2190
    int selem = (extract32(opc, 0, 1) << 1 | R) + 1;
 
2191
    bool replicate = false;
 
2192
    int index = is_q << 3 | S << 2 | size;
 
2193
    int ebytes, xs;
 
2194
    TCGv_i64 tcg_addr, tcg_rn;
 
2195
 
 
2196
    switch (scale) {
 
2197
    case 3:
 
2198
        if (!is_load || S) {
 
2199
            unallocated_encoding(s);
 
2200
            return;
 
2201
        }
 
2202
        scale = size;
 
2203
        replicate = true;
 
2204
        break;
 
2205
    case 0:
 
2206
        break;
 
2207
    case 1:
 
2208
        if (extract32(size, 0, 1)) {
 
2209
            unallocated_encoding(s);
 
2210
            return;
 
2211
        }
 
2212
        index >>= 1;
 
2213
        break;
 
2214
    case 2:
 
2215
        if (extract32(size, 1, 1)) {
 
2216
            unallocated_encoding(s);
 
2217
            return;
 
2218
        }
 
2219
        if (!extract32(size, 0, 1)) {
 
2220
            index >>= 2;
 
2221
        } else {
 
2222
            if (S) {
 
2223
                unallocated_encoding(s);
 
2224
                return;
 
2225
            }
 
2226
            index >>= 3;
 
2227
            scale = 3;
 
2228
        }
 
2229
        break;
 
2230
    default:
 
2231
        g_assert_not_reached();
 
2232
    }
 
2233
 
 
2234
    ebytes = 1 << scale;
 
2235
 
 
2236
    if (rn == 31) {
 
2237
        gen_check_sp_alignment(s);
 
2238
    }
 
2239
 
 
2240
    tcg_rn = cpu_reg_sp(s, rn);
 
2241
    tcg_addr = tcg_temp_new_i64();
 
2242
    tcg_gen_mov_i64(tcg_addr, tcg_rn);
 
2243
 
 
2244
    for (xs = 0; xs < selem; xs++) {
 
2245
        if (replicate) {
 
2246
            /* Load and replicate to all elements */
 
2247
            uint64_t mulconst;
 
2248
            TCGv_i64 tcg_tmp = tcg_temp_new_i64();
 
2249
 
 
2250
            tcg_gen_qemu_ld_i64(tcg_tmp, tcg_addr,
 
2251
                                get_mem_index(s), MO_TE + scale);
 
2252
            switch (scale) {
 
2253
            case 0:
 
2254
                mulconst = 0x0101010101010101ULL;
 
2255
                break;
 
2256
            case 1:
 
2257
                mulconst = 0x0001000100010001ULL;
 
2258
                break;
 
2259
            case 2:
 
2260
                mulconst = 0x0000000100000001ULL;
 
2261
                break;
 
2262
            case 3:
 
2263
                mulconst = 0;
 
2264
                break;
 
2265
            default:
 
2266
                g_assert_not_reached();
 
2267
            }
 
2268
            if (mulconst) {
 
2269
                tcg_gen_muli_i64(tcg_tmp, tcg_tmp, mulconst);
 
2270
            }
 
2271
            write_vec_element(s, tcg_tmp, rt, 0, MO_64);
 
2272
            if (is_q) {
 
2273
                write_vec_element(s, tcg_tmp, rt, 1, MO_64);
 
2274
            } else {
 
2275
                clear_vec_high(s, rt);
 
2276
            }
 
2277
            tcg_temp_free_i64(tcg_tmp);
 
2278
        } else {
 
2279
            /* Load/store one element per register */
 
2280
            if (is_load) {
 
2281
                do_vec_ld(s, rt, index, tcg_addr, MO_TE + scale);
 
2282
            } else {
 
2283
                do_vec_st(s, rt, index, tcg_addr, MO_TE + scale);
 
2284
            }
 
2285
        }
 
2286
        tcg_gen_addi_i64(tcg_addr, tcg_addr, ebytes);
 
2287
        rt = (rt + 1) % 32;
 
2288
    }
 
2289
 
 
2290
    if (is_postidx) {
 
2291
        int rm = extract32(insn, 16, 5);
 
2292
        if (rm == 31) {
 
2293
            tcg_gen_mov_i64(tcg_rn, tcg_addr);
 
2294
        } else {
 
2295
            tcg_gen_add_i64(tcg_rn, tcg_rn, cpu_reg(s, rm));
 
2296
        }
 
2297
    }
 
2298
    tcg_temp_free_i64(tcg_addr);
 
2299
}
 
2300
 
 
2301
/* C3.3 Loads and stores */
 
2302
static void disas_ldst(DisasContext *s, uint32_t insn)
 
2303
{
 
2304
    switch (extract32(insn, 24, 6)) {
 
2305
    case 0x08: /* Load/store exclusive */
 
2306
        disas_ldst_excl(s, insn);
 
2307
        break;
 
2308
    case 0x18: case 0x1c: /* Load register (literal) */
 
2309
        disas_ld_lit(s, insn);
 
2310
        break;
 
2311
    case 0x28: case 0x29:
 
2312
    case 0x2c: case 0x2d: /* Load/store pair (all forms) */
 
2313
        disas_ldst_pair(s, insn);
 
2314
        break;
 
2315
    case 0x38: case 0x39:
 
2316
    case 0x3c: case 0x3d: /* Load/store register (all forms) */
 
2317
        disas_ldst_reg(s, insn);
 
2318
        break;
 
2319
    case 0x0c: /* AdvSIMD load/store multiple structures */
 
2320
        disas_ldst_multiple_struct(s, insn);
 
2321
        break;
 
2322
    case 0x0d: /* AdvSIMD load/store single structure */
 
2323
        disas_ldst_single_struct(s, insn);
 
2324
        break;
 
2325
    default:
 
2326
        unallocated_encoding(s);
 
2327
        break;
 
2328
    }
 
2329
}
 
2330
 
 
2331
/* C3.4.6 PC-rel. addressing
 
2332
 *   31  30   29 28       24 23                5 4    0
 
2333
 * +----+-------+-----------+-------------------+------+
 
2334
 * | op | immlo | 1 0 0 0 0 |       immhi       |  Rd  |
 
2335
 * +----+-------+-----------+-------------------+------+
 
2336
 */
 
2337
static void disas_pc_rel_adr(DisasContext *s, uint32_t insn)
 
2338
{
 
2339
    unsigned int page, rd;
 
2340
    uint64_t base;
 
2341
    int64_t offset;
 
2342
 
 
2343
    page = extract32(insn, 31, 1);
 
2344
    /* SignExtend(immhi:immlo) -> offset */
 
2345
    offset = ((int64_t)sextract32(insn, 5, 19) << 2) | extract32(insn, 29, 2);
 
2346
    rd = extract32(insn, 0, 5);
 
2347
    base = s->pc - 4;
 
2348
 
 
2349
    if (page) {
 
2350
        /* ADRP (page based) */
 
2351
        base &= ~0xfff;
 
2352
        offset <<= 12;
 
2353
    }
 
2354
 
 
2355
    tcg_gen_movi_i64(cpu_reg(s, rd), base + offset);
 
2356
}
 
2357
 
 
2358
/*
 
2359
 * C3.4.1 Add/subtract (immediate)
 
2360
 *
 
2361
 *  31 30 29 28       24 23 22 21         10 9   5 4   0
 
2362
 * +--+--+--+-----------+-----+-------------+-----+-----+
 
2363
 * |sf|op| S| 1 0 0 0 1 |shift|    imm12    |  Rn | Rd  |
 
2364
 * +--+--+--+-----------+-----+-------------+-----+-----+
 
2365
 *
 
2366
 *    sf: 0 -> 32bit, 1 -> 64bit
 
2367
 *    op: 0 -> add  , 1 -> sub
 
2368
 *     S: 1 -> set flags
 
2369
 * shift: 00 -> LSL imm by 0, 01 -> LSL imm by 12
 
2370
 */
 
2371
static void disas_add_sub_imm(DisasContext *s, uint32_t insn)
 
2372
{
 
2373
    int rd = extract32(insn, 0, 5);
 
2374
    int rn = extract32(insn, 5, 5);
 
2375
    uint64_t imm = extract32(insn, 10, 12);
 
2376
    int shift = extract32(insn, 22, 2);
 
2377
    bool setflags = extract32(insn, 29, 1);
 
2378
    bool sub_op = extract32(insn, 30, 1);
 
2379
    bool is_64bit = extract32(insn, 31, 1);
 
2380
 
 
2381
    TCGv_i64 tcg_rn = cpu_reg_sp(s, rn);
 
2382
    TCGv_i64 tcg_rd = setflags ? cpu_reg(s, rd) : cpu_reg_sp(s, rd);
 
2383
    TCGv_i64 tcg_result;
 
2384
 
 
2385
    switch (shift) {
 
2386
    case 0x0:
 
2387
        break;
 
2388
    case 0x1:
 
2389
        imm <<= 12;
 
2390
        break;
 
2391
    default:
 
2392
        unallocated_encoding(s);
 
2393
        return;
 
2394
    }
 
2395
 
 
2396
    tcg_result = tcg_temp_new_i64();
 
2397
    if (!setflags) {
 
2398
        if (sub_op) {
 
2399
            tcg_gen_subi_i64(tcg_result, tcg_rn, imm);
 
2400
        } else {
 
2401
            tcg_gen_addi_i64(tcg_result, tcg_rn, imm);
 
2402
        }
 
2403
    } else {
 
2404
        TCGv_i64 tcg_imm = tcg_const_i64(imm);
 
2405
        if (sub_op) {
 
2406
            gen_sub_CC(is_64bit, tcg_result, tcg_rn, tcg_imm);
 
2407
        } else {
 
2408
            gen_add_CC(is_64bit, tcg_result, tcg_rn, tcg_imm);
 
2409
        }
 
2410
        tcg_temp_free_i64(tcg_imm);
 
2411
    }
 
2412
 
 
2413
    if (is_64bit) {
 
2414
        tcg_gen_mov_i64(tcg_rd, tcg_result);
 
2415
    } else {
 
2416
        tcg_gen_ext32u_i64(tcg_rd, tcg_result);
 
2417
    }
 
2418
 
 
2419
    tcg_temp_free_i64(tcg_result);
 
2420
}
 
2421
 
 
2422
/* The input should be a value in the bottom e bits (with higher
 
2423
 * bits zero); returns that value replicated into every element
 
2424
 * of size e in a 64 bit integer.
 
2425
 */
 
2426
static uint64_t bitfield_replicate(uint64_t mask, unsigned int e)
 
2427
{
 
2428
    assert(e != 0);
 
2429
    while (e < 64) {
 
2430
        mask |= mask << e;
 
2431
        e *= 2;
 
2432
    }
 
2433
    return mask;
 
2434
}
 
2435
 
 
2436
/* Return a value with the bottom len bits set (where 0 < len <= 64) */
 
2437
static inline uint64_t bitmask64(unsigned int length)
 
2438
{
 
2439
    assert(length > 0 && length <= 64);
 
2440
    return ~0ULL >> (64 - length);
 
2441
}
 
2442
 
 
2443
/* Simplified variant of pseudocode DecodeBitMasks() for the case where we
 
2444
 * only require the wmask. Returns false if the imms/immr/immn are a reserved
 
2445
 * value (ie should cause a guest UNDEF exception), and true if they are
 
2446
 * valid, in which case the decoded bit pattern is written to result.
 
2447
 */
 
2448
static bool logic_imm_decode_wmask(uint64_t *result, unsigned int immn,
 
2449
                                   unsigned int imms, unsigned int immr)
 
2450
{
 
2451
    uint64_t mask;
 
2452
    unsigned e, levels, s, r;
 
2453
    int len;
 
2454
 
 
2455
    assert(immn < 2 && imms < 64 && immr < 64);
 
2456
 
 
2457
    /* The bit patterns we create here are 64 bit patterns which
 
2458
     * are vectors of identical elements of size e = 2, 4, 8, 16, 32 or
 
2459
     * 64 bits each. Each element contains the same value: a run
 
2460
     * of between 1 and e-1 non-zero bits, rotated within the
 
2461
     * element by between 0 and e-1 bits.
 
2462
     *
 
2463
     * The element size and run length are encoded into immn (1 bit)
 
2464
     * and imms (6 bits) as follows:
 
2465
     * 64 bit elements: immn = 1, imms = <length of run - 1>
 
2466
     * 32 bit elements: immn = 0, imms = 0 : <length of run - 1>
 
2467
     * 16 bit elements: immn = 0, imms = 10 : <length of run - 1>
 
2468
     *  8 bit elements: immn = 0, imms = 110 : <length of run - 1>
 
2469
     *  4 bit elements: immn = 0, imms = 1110 : <length of run - 1>
 
2470
     *  2 bit elements: immn = 0, imms = 11110 : <length of run - 1>
 
2471
     * Notice that immn = 0, imms = 11111x is the only combination
 
2472
     * not covered by one of the above options; this is reserved.
 
2473
     * Further, <length of run - 1> all-ones is a reserved pattern.
 
2474
     *
 
2475
     * In all cases the rotation is by immr % e (and immr is 6 bits).
 
2476
     */
 
2477
 
 
2478
    /* First determine the element size */
 
2479
    len = 31 - clz32((immn << 6) | (~imms & 0x3f));
 
2480
    if (len < 1) {
 
2481
        /* This is the immn == 0, imms == 0x11111x case */
 
2482
        return false;
 
2483
    }
 
2484
    e = 1 << len;
 
2485
 
 
2486
    levels = e - 1;
 
2487
    s = imms & levels;
 
2488
    r = immr & levels;
 
2489
 
 
2490
    if (s == levels) {
 
2491
        /* <length of run - 1> mustn't be all-ones. */
 
2492
        return false;
 
2493
    }
 
2494
 
 
2495
    /* Create the value of one element: s+1 set bits rotated
 
2496
     * by r within the element (which is e bits wide)...
 
2497
     */
 
2498
    mask = bitmask64(s + 1);
 
2499
    mask = (mask >> r) | (mask << (e - r));
 
2500
    /* ...then replicate the element over the whole 64 bit value */
 
2501
    mask = bitfield_replicate(mask, e);
 
2502
    *result = mask;
 
2503
    return true;
 
2504
}
 
2505
 
 
2506
/* C3.4.4 Logical (immediate)
 
2507
 *   31  30 29 28         23 22  21  16 15  10 9    5 4    0
 
2508
 * +----+-----+-------------+---+------+------+------+------+
 
2509
 * | sf | opc | 1 0 0 1 0 0 | N | immr | imms |  Rn  |  Rd  |
 
2510
 * +----+-----+-------------+---+------+------+------+------+
 
2511
 */
 
2512
static void disas_logic_imm(DisasContext *s, uint32_t insn)
 
2513
{
 
2514
    unsigned int sf, opc, is_n, immr, imms, rn, rd;
 
2515
    TCGv_i64 tcg_rd, tcg_rn;
 
2516
    uint64_t wmask;
 
2517
    bool is_and = false;
 
2518
 
 
2519
    sf = extract32(insn, 31, 1);
 
2520
    opc = extract32(insn, 29, 2);
 
2521
    is_n = extract32(insn, 22, 1);
 
2522
    immr = extract32(insn, 16, 6);
 
2523
    imms = extract32(insn, 10, 6);
 
2524
    rn = extract32(insn, 5, 5);
 
2525
    rd = extract32(insn, 0, 5);
 
2526
 
 
2527
    if (!sf && is_n) {
 
2528
        unallocated_encoding(s);
 
2529
        return;
 
2530
    }
 
2531
 
 
2532
    if (opc == 0x3) { /* ANDS */
 
2533
        tcg_rd = cpu_reg(s, rd);
 
2534
    } else {
 
2535
        tcg_rd = cpu_reg_sp(s, rd);
 
2536
    }
 
2537
    tcg_rn = cpu_reg(s, rn);
 
2538
 
 
2539
    if (!logic_imm_decode_wmask(&wmask, is_n, imms, immr)) {
 
2540
        /* some immediate field values are reserved */
 
2541
        unallocated_encoding(s);
 
2542
        return;
 
2543
    }
 
2544
 
 
2545
    if (!sf) {
 
2546
        wmask &= 0xffffffff;
 
2547
    }
 
2548
 
 
2549
    switch (opc) {
 
2550
    case 0x3: /* ANDS */
 
2551
    case 0x0: /* AND */
 
2552
        tcg_gen_andi_i64(tcg_rd, tcg_rn, wmask);
 
2553
        is_and = true;
 
2554
        break;
 
2555
    case 0x1: /* ORR */
 
2556
        tcg_gen_ori_i64(tcg_rd, tcg_rn, wmask);
 
2557
        break;
 
2558
    case 0x2: /* EOR */
 
2559
        tcg_gen_xori_i64(tcg_rd, tcg_rn, wmask);
 
2560
        break;
 
2561
    default:
 
2562
        assert(FALSE); /* must handle all above */
 
2563
        break;
 
2564
    }
 
2565
 
 
2566
    if (!sf && !is_and) {
 
2567
        /* zero extend final result; we know we can skip this for AND
 
2568
         * since the immediate had the high 32 bits clear.
 
2569
         */
 
2570
        tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
 
2571
    }
 
2572
 
 
2573
    if (opc == 3) { /* ANDS */
 
2574
        gen_logic_CC(sf, tcg_rd);
 
2575
    }
 
2576
}
 
2577
 
 
2578
/*
 
2579
 * C3.4.5 Move wide (immediate)
 
2580
 *
 
2581
 *  31 30 29 28         23 22 21 20             5 4    0
 
2582
 * +--+-----+-------------+-----+----------------+------+
 
2583
 * |sf| opc | 1 0 0 1 0 1 |  hw |  imm16         |  Rd  |
 
2584
 * +--+-----+-------------+-----+----------------+------+
 
2585
 *
 
2586
 * sf: 0 -> 32 bit, 1 -> 64 bit
 
2587
 * opc: 00 -> N, 10 -> Z, 11 -> K
 
2588
 * hw: shift/16 (0,16, and sf only 32, 48)
 
2589
 */
 
2590
static void disas_movw_imm(DisasContext *s, uint32_t insn)
 
2591
{
 
2592
    int rd = extract32(insn, 0, 5);
 
2593
    uint64_t imm = extract32(insn, 5, 16);
 
2594
    int sf = extract32(insn, 31, 1);
 
2595
    int opc = extract32(insn, 29, 2);
 
2596
    int pos = extract32(insn, 21, 2) << 4;
 
2597
    TCGv_i64 tcg_rd = cpu_reg(s, rd);
 
2598
    TCGv_i64 tcg_imm;
 
2599
 
 
2600
    if (!sf && (pos >= 32)) {
 
2601
        unallocated_encoding(s);
 
2602
        return;
 
2603
    }
 
2604
 
 
2605
    switch (opc) {
 
2606
    case 0: /* MOVN */
 
2607
    case 2: /* MOVZ */
 
2608
        imm <<= pos;
 
2609
        if (opc == 0) {
 
2610
            imm = ~imm;
 
2611
        }
 
2612
        if (!sf) {
 
2613
            imm &= 0xffffffffu;
 
2614
        }
 
2615
        tcg_gen_movi_i64(tcg_rd, imm);
 
2616
        break;
 
2617
    case 3: /* MOVK */
 
2618
        tcg_imm = tcg_const_i64(imm);
 
2619
        tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_imm, pos, 16);
 
2620
        tcg_temp_free_i64(tcg_imm);
 
2621
        if (!sf) {
 
2622
            tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
 
2623
        }
 
2624
        break;
 
2625
    default:
 
2626
        unallocated_encoding(s);
 
2627
        break;
 
2628
    }
 
2629
}
 
2630
 
 
2631
/* C3.4.2 Bitfield
 
2632
 *   31  30 29 28         23 22  21  16 15  10 9    5 4    0
 
2633
 * +----+-----+-------------+---+------+------+------+------+
 
2634
 * | sf | opc | 1 0 0 1 1 0 | N | immr | imms |  Rn  |  Rd  |
 
2635
 * +----+-----+-------------+---+------+------+------+------+
 
2636
 */
 
2637
static void disas_bitfield(DisasContext *s, uint32_t insn)
 
2638
{
 
2639
    unsigned int sf, n, opc, ri, si, rn, rd, bitsize, pos, len;
 
2640
    TCGv_i64 tcg_rd, tcg_tmp;
 
2641
 
 
2642
    sf = extract32(insn, 31, 1);
 
2643
    opc = extract32(insn, 29, 2);
 
2644
    n = extract32(insn, 22, 1);
 
2645
    ri = extract32(insn, 16, 6);
 
2646
    si = extract32(insn, 10, 6);
 
2647
    rn = extract32(insn, 5, 5);
 
2648
    rd = extract32(insn, 0, 5);
 
2649
    bitsize = sf ? 64 : 32;
 
2650
 
 
2651
    if (sf != n || ri >= bitsize || si >= bitsize || opc > 2) {
 
2652
        unallocated_encoding(s);
 
2653
        return;
 
2654
    }
 
2655
 
 
2656
    tcg_rd = cpu_reg(s, rd);
 
2657
    tcg_tmp = read_cpu_reg(s, rn, sf);
 
2658
 
 
2659
    /* OPTME: probably worth recognizing common cases of ext{8,16,32}{u,s} */
 
2660
 
 
2661
    if (opc != 1) { /* SBFM or UBFM */
 
2662
        tcg_gen_movi_i64(tcg_rd, 0);
 
2663
    }
 
2664
 
 
2665
    /* do the bit move operation */
 
2666
    if (si >= ri) {
 
2667
        /* Wd<s-r:0> = Wn<s:r> */
 
2668
        tcg_gen_shri_i64(tcg_tmp, tcg_tmp, ri);
 
2669
        pos = 0;
 
2670
        len = (si - ri) + 1;
 
2671
    } else {
 
2672
        /* Wd<32+s-r,32-r> = Wn<s:0> */
 
2673
        pos = bitsize - ri;
 
2674
        len = si + 1;
 
2675
    }
 
2676
 
 
2677
    tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, pos, len);
 
2678
 
 
2679
    if (opc == 0) { /* SBFM - sign extend the destination field */
 
2680
        tcg_gen_shli_i64(tcg_rd, tcg_rd, 64 - (pos + len));
 
2681
        tcg_gen_sari_i64(tcg_rd, tcg_rd, 64 - (pos + len));
 
2682
    }
 
2683
 
 
2684
    if (!sf) { /* zero extend final result */
 
2685
        tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
 
2686
    }
 
2687
}
 
2688
 
 
2689
/* C3.4.3 Extract
 
2690
 *   31  30  29 28         23 22   21  20  16 15    10 9    5 4    0
 
2691
 * +----+------+-------------+---+----+------+--------+------+------+
 
2692
 * | sf | op21 | 1 0 0 1 1 1 | N | o0 |  Rm  |  imms  |  Rn  |  Rd  |
 
2693
 * +----+------+-------------+---+----+------+--------+------+------+
 
2694
 */
 
2695
static void disas_extract(DisasContext *s, uint32_t insn)
 
2696
{
 
2697
    unsigned int sf, n, rm, imm, rn, rd, bitsize, op21, op0;
 
2698
 
 
2699
    sf = extract32(insn, 31, 1);
 
2700
    n = extract32(insn, 22, 1);
 
2701
    rm = extract32(insn, 16, 5);
 
2702
    imm = extract32(insn, 10, 6);
 
2703
    rn = extract32(insn, 5, 5);
 
2704
    rd = extract32(insn, 0, 5);
 
2705
    op21 = extract32(insn, 29, 2);
 
2706
    op0 = extract32(insn, 21, 1);
 
2707
    bitsize = sf ? 64 : 32;
 
2708
 
 
2709
    if (sf != n || op21 || op0 || imm >= bitsize) {
 
2710
        unallocated_encoding(s);
 
2711
    } else {
 
2712
        TCGv_i64 tcg_rd, tcg_rm, tcg_rn;
 
2713
 
 
2714
        tcg_rd = cpu_reg(s, rd);
 
2715
 
 
2716
        if (imm) {
 
2717
            /* OPTME: we can special case rm==rn as a rotate */
 
2718
            tcg_rm = read_cpu_reg(s, rm, sf);
 
2719
            tcg_rn = read_cpu_reg(s, rn, sf);
 
2720
            tcg_gen_shri_i64(tcg_rm, tcg_rm, imm);
 
2721
            tcg_gen_shli_i64(tcg_rn, tcg_rn, bitsize - imm);
 
2722
            tcg_gen_or_i64(tcg_rd, tcg_rm, tcg_rn);
 
2723
            if (!sf) {
 
2724
                tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
 
2725
            }
 
2726
        } else {
 
2727
            /* tcg shl_i32/shl_i64 is undefined for 32/64 bit shifts,
 
2728
             * so an extract from bit 0 is a special case.
 
2729
             */
 
2730
            if (sf) {
 
2731
                tcg_gen_mov_i64(tcg_rd, cpu_reg(s, rm));
 
2732
            } else {
 
2733
                tcg_gen_ext32u_i64(tcg_rd, cpu_reg(s, rm));
 
2734
            }
 
2735
        }
 
2736
 
 
2737
    }
 
2738
}
 
2739
 
 
2740
/* C3.4 Data processing - immediate */
 
2741
static void disas_data_proc_imm(DisasContext *s, uint32_t insn)
 
2742
{
 
2743
    switch (extract32(insn, 23, 6)) {
 
2744
    case 0x20: case 0x21: /* PC-rel. addressing */
 
2745
        disas_pc_rel_adr(s, insn);
 
2746
        break;
 
2747
    case 0x22: case 0x23: /* Add/subtract (immediate) */
 
2748
        disas_add_sub_imm(s, insn);
 
2749
        break;
 
2750
    case 0x24: /* Logical (immediate) */
 
2751
        disas_logic_imm(s, insn);
 
2752
        break;
 
2753
    case 0x25: /* Move wide (immediate) */
 
2754
        disas_movw_imm(s, insn);
 
2755
        break;
 
2756
    case 0x26: /* Bitfield */
 
2757
        disas_bitfield(s, insn);
 
2758
        break;
 
2759
    case 0x27: /* Extract */
 
2760
        disas_extract(s, insn);
 
2761
        break;
 
2762
    default:
 
2763
        unallocated_encoding(s);
 
2764
        break;
 
2765
    }
 
2766
}
 
2767
 
 
2768
/* Shift a TCGv src by TCGv shift_amount, put result in dst.
 
2769
 * Note that it is the caller's responsibility to ensure that the
 
2770
 * shift amount is in range (ie 0..31 or 0..63) and provide the ARM
 
2771
 * mandated semantics for out of range shifts.
 
2772
 */
 
2773
static void shift_reg(TCGv_i64 dst, TCGv_i64 src, int sf,
 
2774
                      enum a64_shift_type shift_type, TCGv_i64 shift_amount)
 
2775
{
 
2776
    switch (shift_type) {
 
2777
    case A64_SHIFT_TYPE_LSL:
 
2778
        tcg_gen_shl_i64(dst, src, shift_amount);
 
2779
        break;
 
2780
    case A64_SHIFT_TYPE_LSR:
 
2781
        tcg_gen_shr_i64(dst, src, shift_amount);
 
2782
        break;
 
2783
    case A64_SHIFT_TYPE_ASR:
 
2784
        if (!sf) {
 
2785
            tcg_gen_ext32s_i64(dst, src);
 
2786
        }
 
2787
        tcg_gen_sar_i64(dst, sf ? src : dst, shift_amount);
 
2788
        break;
 
2789
    case A64_SHIFT_TYPE_ROR:
 
2790
        if (sf) {
 
2791
            tcg_gen_rotr_i64(dst, src, shift_amount);
 
2792
        } else {
 
2793
            TCGv_i32 t0, t1;
 
2794
            t0 = tcg_temp_new_i32();
 
2795
            t1 = tcg_temp_new_i32();
 
2796
            tcg_gen_trunc_i64_i32(t0, src);
 
2797
            tcg_gen_trunc_i64_i32(t1, shift_amount);
 
2798
            tcg_gen_rotr_i32(t0, t0, t1);
 
2799
            tcg_gen_extu_i32_i64(dst, t0);
 
2800
            tcg_temp_free_i32(t0);
 
2801
            tcg_temp_free_i32(t1);
 
2802
        }
 
2803
        break;
 
2804
    default:
 
2805
        assert(FALSE); /* all shift types should be handled */
 
2806
        break;
 
2807
    }
 
2808
 
 
2809
    if (!sf) { /* zero extend final result */
 
2810
        tcg_gen_ext32u_i64(dst, dst);
 
2811
    }
 
2812
}
 
2813
 
 
2814
/* Shift a TCGv src by immediate, put result in dst.
 
2815
 * The shift amount must be in range (this should always be true as the
 
2816
 * relevant instructions will UNDEF on bad shift immediates).
 
2817
 */
 
2818
static void shift_reg_imm(TCGv_i64 dst, TCGv_i64 src, int sf,
 
2819
                          enum a64_shift_type shift_type, unsigned int shift_i)
 
2820
{
 
2821
    assert(shift_i < (sf ? 64 : 32));
 
2822
 
 
2823
    if (shift_i == 0) {
 
2824
        tcg_gen_mov_i64(dst, src);
 
2825
    } else {
 
2826
        TCGv_i64 shift_const;
 
2827
 
 
2828
        shift_const = tcg_const_i64(shift_i);
 
2829
        shift_reg(dst, src, sf, shift_type, shift_const);
 
2830
        tcg_temp_free_i64(shift_const);
 
2831
    }
 
2832
}
 
2833
 
 
2834
/* C3.5.10 Logical (shifted register)
 
2835
 *   31  30 29 28       24 23   22 21  20  16 15    10 9    5 4    0
 
2836
 * +----+-----+-----------+-------+---+------+--------+------+------+
 
2837
 * | sf | opc | 0 1 0 1 0 | shift | N |  Rm  |  imm6  |  Rn  |  Rd  |
 
2838
 * +----+-----+-----------+-------+---+------+--------+------+------+
 
2839
 */
 
2840
static void disas_logic_reg(DisasContext *s, uint32_t insn)
 
2841
{
 
2842
    TCGv_i64 tcg_rd, tcg_rn, tcg_rm;
 
2843
    unsigned int sf, opc, shift_type, invert, rm, shift_amount, rn, rd;
 
2844
 
 
2845
    sf = extract32(insn, 31, 1);
 
2846
    opc = extract32(insn, 29, 2);
 
2847
    shift_type = extract32(insn, 22, 2);
 
2848
    invert = extract32(insn, 21, 1);
 
2849
    rm = extract32(insn, 16, 5);
 
2850
    shift_amount = extract32(insn, 10, 6);
 
2851
    rn = extract32(insn, 5, 5);
 
2852
    rd = extract32(insn, 0, 5);
 
2853
 
 
2854
    if (!sf && (shift_amount & (1 << 5))) {
 
2855
        unallocated_encoding(s);
 
2856
        return;
 
2857
    }
 
2858
 
 
2859
    tcg_rd = cpu_reg(s, rd);
 
2860
 
 
2861
    if (opc == 1 && shift_amount == 0 && shift_type == 0 && rn == 31) {
 
2862
        /* Unshifted ORR and ORN with WZR/XZR is the standard encoding for
 
2863
         * register-register MOV and MVN, so it is worth special casing.
 
2864
         */
 
2865
        tcg_rm = cpu_reg(s, rm);
 
2866
        if (invert) {
 
2867
            tcg_gen_not_i64(tcg_rd, tcg_rm);
 
2868
            if (!sf) {
 
2869
                tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
 
2870
            }
 
2871
        } else {
 
2872
            if (sf) {
 
2873
                tcg_gen_mov_i64(tcg_rd, tcg_rm);
 
2874
            } else {
 
2875
                tcg_gen_ext32u_i64(tcg_rd, tcg_rm);
 
2876
            }
 
2877
        }
 
2878
        return;
 
2879
    }
 
2880
 
 
2881
    tcg_rm = read_cpu_reg(s, rm, sf);
 
2882
 
 
2883
    if (shift_amount) {
 
2884
        shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, shift_amount);
 
2885
    }
 
2886
 
 
2887
    tcg_rn = cpu_reg(s, rn);
 
2888
 
 
2889
    switch (opc | (invert << 2)) {
 
2890
    case 0: /* AND */
 
2891
    case 3: /* ANDS */
 
2892
        tcg_gen_and_i64(tcg_rd, tcg_rn, tcg_rm);
 
2893
        break;
 
2894
    case 1: /* ORR */
 
2895
        tcg_gen_or_i64(tcg_rd, tcg_rn, tcg_rm);
 
2896
        break;
 
2897
    case 2: /* EOR */
 
2898
        tcg_gen_xor_i64(tcg_rd, tcg_rn, tcg_rm);
 
2899
        break;
 
2900
    case 4: /* BIC */
 
2901
    case 7: /* BICS */
 
2902
        tcg_gen_andc_i64(tcg_rd, tcg_rn, tcg_rm);
 
2903
        break;
 
2904
    case 5: /* ORN */
 
2905
        tcg_gen_orc_i64(tcg_rd, tcg_rn, tcg_rm);
 
2906
        break;
 
2907
    case 6: /* EON */
 
2908
        tcg_gen_eqv_i64(tcg_rd, tcg_rn, tcg_rm);
 
2909
        break;
 
2910
    default:
 
2911
        assert(FALSE);
 
2912
        break;
 
2913
    }
 
2914
 
 
2915
    if (!sf) {
 
2916
        tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
 
2917
    }
 
2918
 
 
2919
    if (opc == 3) {
 
2920
        gen_logic_CC(sf, tcg_rd);
 
2921
    }
 
2922
}
 
2923
 
 
2924
/*
 
2925
 * C3.5.1 Add/subtract (extended register)
 
2926
 *
 
2927
 *  31|30|29|28       24|23 22|21|20   16|15  13|12  10|9  5|4  0|
 
2928
 * +--+--+--+-----------+-----+--+-------+------+------+----+----+
 
2929
 * |sf|op| S| 0 1 0 1 1 | opt | 1|  Rm   |option| imm3 | Rn | Rd |
 
2930
 * +--+--+--+-----------+-----+--+-------+------+------+----+----+
 
2931
 *
 
2932
 *  sf: 0 -> 32bit, 1 -> 64bit
 
2933
 *  op: 0 -> add  , 1 -> sub
 
2934
 *   S: 1 -> set flags
 
2935
 * opt: 00
 
2936
 * option: extension type (see DecodeRegExtend)
 
2937
 * imm3: optional shift to Rm
 
2938
 *
 
2939
 * Rd = Rn + LSL(extend(Rm), amount)
 
2940
 */
 
2941
static void disas_add_sub_ext_reg(DisasContext *s, uint32_t insn)
 
2942
{
 
2943
    int rd = extract32(insn, 0, 5);
 
2944
    int rn = extract32(insn, 5, 5);
 
2945
    int imm3 = extract32(insn, 10, 3);
 
2946
    int option = extract32(insn, 13, 3);
 
2947
    int rm = extract32(insn, 16, 5);
 
2948
    bool setflags = extract32(insn, 29, 1);
 
2949
    bool sub_op = extract32(insn, 30, 1);
 
2950
    bool sf = extract32(insn, 31, 1);
 
2951
 
 
2952
    TCGv_i64 tcg_rm, tcg_rn; /* temps */
 
2953
    TCGv_i64 tcg_rd;
 
2954
    TCGv_i64 tcg_result;
 
2955
 
 
2956
    if (imm3 > 4) {
 
2957
        unallocated_encoding(s);
 
2958
        return;
 
2959
    }
 
2960
 
 
2961
    /* non-flag setting ops may use SP */
 
2962
    if (!setflags) {
 
2963
        tcg_rn = read_cpu_reg_sp(s, rn, sf);
 
2964
        tcg_rd = cpu_reg_sp(s, rd);
 
2965
    } else {
 
2966
        tcg_rn = read_cpu_reg(s, rn, sf);
 
2967
        tcg_rd = cpu_reg(s, rd);
 
2968
    }
 
2969
 
 
2970
    tcg_rm = read_cpu_reg(s, rm, sf);
 
2971
    ext_and_shift_reg(tcg_rm, tcg_rm, option, imm3);
 
2972
 
 
2973
    tcg_result = tcg_temp_new_i64();
 
2974
 
 
2975
    if (!setflags) {
 
2976
        if (sub_op) {
 
2977
            tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
 
2978
        } else {
 
2979
            tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
 
2980
        }
 
2981
    } else {
 
2982
        if (sub_op) {
 
2983
            gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
 
2984
        } else {
 
2985
            gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
 
2986
        }
 
2987
    }
 
2988
 
 
2989
    if (sf) {
 
2990
        tcg_gen_mov_i64(tcg_rd, tcg_result);
 
2991
    } else {
 
2992
        tcg_gen_ext32u_i64(tcg_rd, tcg_result);
 
2993
    }
 
2994
 
 
2995
    tcg_temp_free_i64(tcg_result);
 
2996
}
 
2997
 
 
2998
/*
 
2999
 * C3.5.2 Add/subtract (shifted register)
 
3000
 *
 
3001
 *  31 30 29 28       24 23 22 21 20   16 15     10 9    5 4    0
 
3002
 * +--+--+--+-----------+-----+--+-------+---------+------+------+
 
3003
 * |sf|op| S| 0 1 0 1 1 |shift| 0|  Rm   |  imm6   |  Rn  |  Rd  |
 
3004
 * +--+--+--+-----------+-----+--+-------+---------+------+------+
 
3005
 *
 
3006
 *    sf: 0 -> 32bit, 1 -> 64bit
 
3007
 *    op: 0 -> add  , 1 -> sub
 
3008
 *     S: 1 -> set flags
 
3009
 * shift: 00 -> LSL, 01 -> LSR, 10 -> ASR, 11 -> RESERVED
 
3010
 *  imm6: Shift amount to apply to Rm before the add/sub
 
3011
 */
 
3012
static void disas_add_sub_reg(DisasContext *s, uint32_t insn)
 
3013
{
 
3014
    int rd = extract32(insn, 0, 5);
 
3015
    int rn = extract32(insn, 5, 5);
 
3016
    int imm6 = extract32(insn, 10, 6);
 
3017
    int rm = extract32(insn, 16, 5);
 
3018
    int shift_type = extract32(insn, 22, 2);
 
3019
    bool setflags = extract32(insn, 29, 1);
 
3020
    bool sub_op = extract32(insn, 30, 1);
 
3021
    bool sf = extract32(insn, 31, 1);
 
3022
 
 
3023
    TCGv_i64 tcg_rd = cpu_reg(s, rd);
 
3024
    TCGv_i64 tcg_rn, tcg_rm;
 
3025
    TCGv_i64 tcg_result;
 
3026
 
 
3027
    if ((shift_type == 3) || (!sf && (imm6 > 31))) {
 
3028
        unallocated_encoding(s);
 
3029
        return;
 
3030
    }
 
3031
 
 
3032
    tcg_rn = read_cpu_reg(s, rn, sf);
 
3033
    tcg_rm = read_cpu_reg(s, rm, sf);
 
3034
 
 
3035
    shift_reg_imm(tcg_rm, tcg_rm, sf, shift_type, imm6);
 
3036
 
 
3037
    tcg_result = tcg_temp_new_i64();
 
3038
 
 
3039
    if (!setflags) {
 
3040
        if (sub_op) {
 
3041
            tcg_gen_sub_i64(tcg_result, tcg_rn, tcg_rm);
 
3042
        } else {
 
3043
            tcg_gen_add_i64(tcg_result, tcg_rn, tcg_rm);
 
3044
        }
 
3045
    } else {
 
3046
        if (sub_op) {
 
3047
            gen_sub_CC(sf, tcg_result, tcg_rn, tcg_rm);
 
3048
        } else {
 
3049
            gen_add_CC(sf, tcg_result, tcg_rn, tcg_rm);
 
3050
        }
 
3051
    }
 
3052
 
 
3053
    if (sf) {
 
3054
        tcg_gen_mov_i64(tcg_rd, tcg_result);
 
3055
    } else {
 
3056
        tcg_gen_ext32u_i64(tcg_rd, tcg_result);
 
3057
    }
 
3058
 
 
3059
    tcg_temp_free_i64(tcg_result);
 
3060
}
 
3061
 
 
3062
/* C3.5.9 Data-processing (3 source)
 
3063
 
 
3064
   31 30  29 28       24 23 21  20  16  15  14  10 9    5 4    0
 
3065
  +--+------+-----------+------+------+----+------+------+------+
 
3066
  |sf| op54 | 1 1 0 1 1 | op31 |  Rm  | o0 |  Ra  |  Rn  |  Rd  |
 
3067
  +--+------+-----------+------+------+----+------+------+------+
 
3068
 
 
3069
 */
 
3070
static void disas_data_proc_3src(DisasContext *s, uint32_t insn)
 
3071
{
 
3072
    int rd = extract32(insn, 0, 5);
 
3073
    int rn = extract32(insn, 5, 5);
 
3074
    int ra = extract32(insn, 10, 5);
 
3075
    int rm = extract32(insn, 16, 5);
 
3076
    int op_id = (extract32(insn, 29, 3) << 4) |
 
3077
        (extract32(insn, 21, 3) << 1) |
 
3078
        extract32(insn, 15, 1);
 
3079
    bool sf = extract32(insn, 31, 1);
 
3080
    bool is_sub = extract32(op_id, 0, 1);
 
3081
    bool is_high = extract32(op_id, 2, 1);
 
3082
    bool is_signed = false;
 
3083
    TCGv_i64 tcg_op1;
 
3084
    TCGv_i64 tcg_op2;
 
3085
    TCGv_i64 tcg_tmp;
 
3086
 
 
3087
    /* Note that op_id is sf:op54:op31:o0 so it includes the 32/64 size flag */
 
3088
    switch (op_id) {
 
3089
    case 0x42: /* SMADDL */
 
3090
    case 0x43: /* SMSUBL */
 
3091
    case 0x44: /* SMULH */
 
3092
        is_signed = true;
 
3093
        break;
 
3094
    case 0x0: /* MADD (32bit) */
 
3095
    case 0x1: /* MSUB (32bit) */
 
3096
    case 0x40: /* MADD (64bit) */
 
3097
    case 0x41: /* MSUB (64bit) */
 
3098
    case 0x4a: /* UMADDL */
 
3099
    case 0x4b: /* UMSUBL */
 
3100
    case 0x4c: /* UMULH */
 
3101
        break;
 
3102
    default:
 
3103
        unallocated_encoding(s);
 
3104
        return;
 
3105
    }
 
3106
 
 
3107
    if (is_high) {
 
3108
        TCGv_i64 low_bits = tcg_temp_new_i64(); /* low bits discarded */
 
3109
        TCGv_i64 tcg_rd = cpu_reg(s, rd);
 
3110
        TCGv_i64 tcg_rn = cpu_reg(s, rn);
 
3111
        TCGv_i64 tcg_rm = cpu_reg(s, rm);
 
3112
 
 
3113
        if (is_signed) {
 
3114
            tcg_gen_muls2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
 
3115
        } else {
 
3116
            tcg_gen_mulu2_i64(low_bits, tcg_rd, tcg_rn, tcg_rm);
 
3117
        }
 
3118
 
 
3119
        tcg_temp_free_i64(low_bits);
 
3120
        return;
 
3121
    }
 
3122
 
 
3123
    tcg_op1 = tcg_temp_new_i64();
 
3124
    tcg_op2 = tcg_temp_new_i64();
 
3125
    tcg_tmp = tcg_temp_new_i64();
 
3126
 
 
3127
    if (op_id < 0x42) {
 
3128
        tcg_gen_mov_i64(tcg_op1, cpu_reg(s, rn));
 
3129
        tcg_gen_mov_i64(tcg_op2, cpu_reg(s, rm));
 
3130
    } else {
 
3131
        if (is_signed) {
 
3132
            tcg_gen_ext32s_i64(tcg_op1, cpu_reg(s, rn));
 
3133
            tcg_gen_ext32s_i64(tcg_op2, cpu_reg(s, rm));
 
3134
        } else {
 
3135
            tcg_gen_ext32u_i64(tcg_op1, cpu_reg(s, rn));
 
3136
            tcg_gen_ext32u_i64(tcg_op2, cpu_reg(s, rm));
 
3137
        }
 
3138
    }
 
3139
 
 
3140
    if (ra == 31 && !is_sub) {
 
3141
        /* Special-case MADD with rA == XZR; it is the standard MUL alias */
 
3142
        tcg_gen_mul_i64(cpu_reg(s, rd), tcg_op1, tcg_op2);
 
3143
    } else {
 
3144
        tcg_gen_mul_i64(tcg_tmp, tcg_op1, tcg_op2);
 
3145
        if (is_sub) {
 
3146
            tcg_gen_sub_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
 
3147
        } else {
 
3148
            tcg_gen_add_i64(cpu_reg(s, rd), cpu_reg(s, ra), tcg_tmp);
 
3149
        }
 
3150
    }
 
3151
 
 
3152
    if (!sf) {
 
3153
        tcg_gen_ext32u_i64(cpu_reg(s, rd), cpu_reg(s, rd));
 
3154
    }
 
3155
 
 
3156
    tcg_temp_free_i64(tcg_op1);
 
3157
    tcg_temp_free_i64(tcg_op2);
 
3158
    tcg_temp_free_i64(tcg_tmp);
 
3159
}
 
3160
 
 
3161
/* C3.5.3 - Add/subtract (with carry)
 
3162
 *  31 30 29 28 27 26 25 24 23 22 21  20  16  15   10  9    5 4   0
 
3163
 * +--+--+--+------------------------+------+---------+------+-----+
 
3164
 * |sf|op| S| 1  1  0  1  0  0  0  0 |  rm  | opcode2 |  Rn  |  Rd |
 
3165
 * +--+--+--+------------------------+------+---------+------+-----+
 
3166
 *                                            [000000]
 
3167
 */
 
3168
 
 
3169
static void disas_adc_sbc(DisasContext *s, uint32_t insn)
 
3170
{
 
3171
    unsigned int sf, op, setflags, rm, rn, rd;
 
3172
    TCGv_i64 tcg_y, tcg_rn, tcg_rd;
 
3173
 
 
3174
    if (extract32(insn, 10, 6) != 0) {
 
3175
        unallocated_encoding(s);
 
3176
        return;
 
3177
    }
 
3178
 
 
3179
    sf = extract32(insn, 31, 1);
 
3180
    op = extract32(insn, 30, 1);
 
3181
    setflags = extract32(insn, 29, 1);
 
3182
    rm = extract32(insn, 16, 5);
 
3183
    rn = extract32(insn, 5, 5);
 
3184
    rd = extract32(insn, 0, 5);
 
3185
 
 
3186
    tcg_rd = cpu_reg(s, rd);
 
3187
    tcg_rn = cpu_reg(s, rn);
 
3188
 
 
3189
    if (op) {
 
3190
        tcg_y = new_tmp_a64(s);
 
3191
        tcg_gen_not_i64(tcg_y, cpu_reg(s, rm));
 
3192
    } else {
 
3193
        tcg_y = cpu_reg(s, rm);
 
3194
    }
 
3195
 
 
3196
    if (setflags) {
 
3197
        gen_adc_CC(sf, tcg_rd, tcg_rn, tcg_y);
 
3198
    } else {
 
3199
        gen_adc(sf, tcg_rd, tcg_rn, tcg_y);
 
3200
    }
 
3201
}
 
3202
 
 
3203
/* C3.5.4 - C3.5.5 Conditional compare (immediate / register)
 
3204
 *  31 30 29 28 27 26 25 24 23 22 21  20    16 15  12  11  10  9   5  4 3   0
 
3205
 * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
 
3206
 * |sf|op| S| 1  1  0  1  0  0  1  0 |imm5/rm | cond |i/r |o2|  Rn  |o3|nzcv |
 
3207
 * +--+--+--+------------------------+--------+------+----+--+------+--+-----+
 
3208
 *        [1]                             y                [0]       [0]
 
3209
 */
 
3210
static void disas_cc(DisasContext *s, uint32_t insn)
 
3211
{
 
3212
    unsigned int sf, op, y, cond, rn, nzcv, is_imm;
 
3213
    int label_continue = -1;
 
3214
    TCGv_i64 tcg_tmp, tcg_y, tcg_rn;
 
3215
 
 
3216
    if (!extract32(insn, 29, 1)) {
 
3217
        unallocated_encoding(s);
 
3218
        return;
 
3219
    }
 
3220
    if (insn & (1 << 10 | 1 << 4)) {
 
3221
        unallocated_encoding(s);
 
3222
        return;
 
3223
    }
 
3224
    sf = extract32(insn, 31, 1);
 
3225
    op = extract32(insn, 30, 1);
 
3226
    is_imm = extract32(insn, 11, 1);
 
3227
    y = extract32(insn, 16, 5); /* y = rm (reg) or imm5 (imm) */
 
3228
    cond = extract32(insn, 12, 4);
 
3229
    rn = extract32(insn, 5, 5);
 
3230
    nzcv = extract32(insn, 0, 4);
 
3231
 
 
3232
    if (cond < 0x0e) { /* not always */
 
3233
        int label_match = gen_new_label();
 
3234
        label_continue = gen_new_label();
 
3235
        arm_gen_test_cc(cond, label_match);
 
3236
        /* nomatch: */
 
3237
        tcg_tmp = tcg_temp_new_i64();
 
3238
        tcg_gen_movi_i64(tcg_tmp, nzcv << 28);
 
3239
        gen_set_nzcv(tcg_tmp);
 
3240
        tcg_temp_free_i64(tcg_tmp);
 
3241
        tcg_gen_br(label_continue);
 
3242
        gen_set_label(label_match);
 
3243
    }
 
3244
    /* match, or condition is always */
 
3245
    if (is_imm) {
 
3246
        tcg_y = new_tmp_a64(s);
 
3247
        tcg_gen_movi_i64(tcg_y, y);
 
3248
    } else {
 
3249
        tcg_y = cpu_reg(s, y);
 
3250
    }
 
3251
    tcg_rn = cpu_reg(s, rn);
 
3252
 
 
3253
    tcg_tmp = tcg_temp_new_i64();
 
3254
    if (op) {
 
3255
        gen_sub_CC(sf, tcg_tmp, tcg_rn, tcg_y);
 
3256
    } else {
 
3257
        gen_add_CC(sf, tcg_tmp, tcg_rn, tcg_y);
 
3258
    }
 
3259
    tcg_temp_free_i64(tcg_tmp);
 
3260
 
 
3261
    if (cond < 0x0e) { /* continue */
 
3262
        gen_set_label(label_continue);
 
3263
    }
 
3264
}
 
3265
 
 
3266
/* C3.5.6 Conditional select
 
3267
 *   31   30  29  28             21 20  16 15  12 11 10 9    5 4    0
 
3268
 * +----+----+---+-----------------+------+------+-----+------+------+
 
3269
 * | sf | op | S | 1 1 0 1 0 1 0 0 |  Rm  | cond | op2 |  Rn  |  Rd  |
 
3270
 * +----+----+---+-----------------+------+------+-----+------+------+
 
3271
 */
 
3272
static void disas_cond_select(DisasContext *s, uint32_t insn)
 
3273
{
 
3274
    unsigned int sf, else_inv, rm, cond, else_inc, rn, rd;
 
3275
    TCGv_i64 tcg_rd, tcg_src;
 
3276
 
 
3277
    if (extract32(insn, 29, 1) || extract32(insn, 11, 1)) {
 
3278
        /* S == 1 or op2<1> == 1 */
 
3279
        unallocated_encoding(s);
 
3280
        return;
 
3281
    }
 
3282
    sf = extract32(insn, 31, 1);
 
3283
    else_inv = extract32(insn, 30, 1);
 
3284
    rm = extract32(insn, 16, 5);
 
3285
    cond = extract32(insn, 12, 4);
 
3286
    else_inc = extract32(insn, 10, 1);
 
3287
    rn = extract32(insn, 5, 5);
 
3288
    rd = extract32(insn, 0, 5);
 
3289
 
 
3290
    if (rd == 31) {
 
3291
        /* silly no-op write; until we use movcond we must special-case
 
3292
         * this to avoid a dead temporary across basic blocks.
 
3293
         */
 
3294
        return;
 
3295
    }
 
3296
 
 
3297
    tcg_rd = cpu_reg(s, rd);
 
3298
 
 
3299
    if (cond >= 0x0e) { /* condition "always" */
 
3300
        tcg_src = read_cpu_reg(s, rn, sf);
 
3301
        tcg_gen_mov_i64(tcg_rd, tcg_src);
 
3302
    } else {
 
3303
        /* OPTME: we could use movcond here, at the cost of duplicating
 
3304
         * a lot of the arm_gen_test_cc() logic.
 
3305
         */
 
3306
        int label_match = gen_new_label();
 
3307
        int label_continue = gen_new_label();
 
3308
 
 
3309
        arm_gen_test_cc(cond, label_match);
 
3310
        /* nomatch: */
 
3311
        tcg_src = cpu_reg(s, rm);
 
3312
 
 
3313
        if (else_inv && else_inc) {
 
3314
            tcg_gen_neg_i64(tcg_rd, tcg_src);
 
3315
        } else if (else_inv) {
 
3316
            tcg_gen_not_i64(tcg_rd, tcg_src);
 
3317
        } else if (else_inc) {
 
3318
            tcg_gen_addi_i64(tcg_rd, tcg_src, 1);
 
3319
        } else {
 
3320
            tcg_gen_mov_i64(tcg_rd, tcg_src);
 
3321
        }
 
3322
        if (!sf) {
 
3323
            tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
 
3324
        }
 
3325
        tcg_gen_br(label_continue);
 
3326
        /* match: */
 
3327
        gen_set_label(label_match);
 
3328
        tcg_src = read_cpu_reg(s, rn, sf);
 
3329
        tcg_gen_mov_i64(tcg_rd, tcg_src);
 
3330
        /* continue: */
 
3331
        gen_set_label(label_continue);
 
3332
    }
 
3333
}
 
3334
 
 
3335
static void handle_clz(DisasContext *s, unsigned int sf,
 
3336
                       unsigned int rn, unsigned int rd)
 
3337
{
 
3338
    TCGv_i64 tcg_rd, tcg_rn;
 
3339
    tcg_rd = cpu_reg(s, rd);
 
3340
    tcg_rn = cpu_reg(s, rn);
 
3341
 
 
3342
    if (sf) {
 
3343
        gen_helper_clz64(tcg_rd, tcg_rn);
 
3344
    } else {
 
3345
        TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
 
3346
        tcg_gen_trunc_i64_i32(tcg_tmp32, tcg_rn);
 
3347
        gen_helper_clz(tcg_tmp32, tcg_tmp32);
 
3348
        tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
 
3349
        tcg_temp_free_i32(tcg_tmp32);
 
3350
    }
 
3351
}
 
3352
 
 
3353
static void handle_cls(DisasContext *s, unsigned int sf,
 
3354
                       unsigned int rn, unsigned int rd)
 
3355
{
 
3356
    TCGv_i64 tcg_rd, tcg_rn;
 
3357
    tcg_rd = cpu_reg(s, rd);
 
3358
    tcg_rn = cpu_reg(s, rn);
 
3359
 
 
3360
    if (sf) {
 
3361
        gen_helper_cls64(tcg_rd, tcg_rn);
 
3362
    } else {
 
3363
        TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
 
3364
        tcg_gen_trunc_i64_i32(tcg_tmp32, tcg_rn);
 
3365
        gen_helper_cls32(tcg_tmp32, tcg_tmp32);
 
3366
        tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
 
3367
        tcg_temp_free_i32(tcg_tmp32);
 
3368
    }
 
3369
}
 
3370
 
 
3371
static void handle_rbit(DisasContext *s, unsigned int sf,
 
3372
                        unsigned int rn, unsigned int rd)
 
3373
{
 
3374
    TCGv_i64 tcg_rd, tcg_rn;
 
3375
    tcg_rd = cpu_reg(s, rd);
 
3376
    tcg_rn = cpu_reg(s, rn);
 
3377
 
 
3378
    if (sf) {
 
3379
        gen_helper_rbit64(tcg_rd, tcg_rn);
 
3380
    } else {
 
3381
        TCGv_i32 tcg_tmp32 = tcg_temp_new_i32();
 
3382
        tcg_gen_trunc_i64_i32(tcg_tmp32, tcg_rn);
 
3383
        gen_helper_rbit(tcg_tmp32, tcg_tmp32);
 
3384
        tcg_gen_extu_i32_i64(tcg_rd, tcg_tmp32);
 
3385
        tcg_temp_free_i32(tcg_tmp32);
 
3386
    }
 
3387
}
 
3388
 
 
3389
/* C5.6.149 REV with sf==1, opcode==3 ("REV64") */
 
3390
static void handle_rev64(DisasContext *s, unsigned int sf,
 
3391
                         unsigned int rn, unsigned int rd)
 
3392
{
 
3393
    if (!sf) {
 
3394
        unallocated_encoding(s);
 
3395
        return;
 
3396
    }
 
3397
    tcg_gen_bswap64_i64(cpu_reg(s, rd), cpu_reg(s, rn));
 
3398
}
 
3399
 
 
3400
/* C5.6.149 REV with sf==0, opcode==2
 
3401
 * C5.6.151 REV32 (sf==1, opcode==2)
 
3402
 */
 
3403
static void handle_rev32(DisasContext *s, unsigned int sf,
 
3404
                         unsigned int rn, unsigned int rd)
 
3405
{
 
3406
    TCGv_i64 tcg_rd = cpu_reg(s, rd);
 
3407
 
 
3408
    if (sf) {
 
3409
        TCGv_i64 tcg_tmp = tcg_temp_new_i64();
 
3410
        TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
 
3411
 
 
3412
        /* bswap32_i64 requires zero high word */
 
3413
        tcg_gen_ext32u_i64(tcg_tmp, tcg_rn);
 
3414
        tcg_gen_bswap32_i64(tcg_rd, tcg_tmp);
 
3415
        tcg_gen_shri_i64(tcg_tmp, tcg_rn, 32);
 
3416
        tcg_gen_bswap32_i64(tcg_tmp, tcg_tmp);
 
3417
        tcg_gen_concat32_i64(tcg_rd, tcg_rd, tcg_tmp);
 
3418
 
 
3419
        tcg_temp_free_i64(tcg_tmp);
 
3420
    } else {
 
3421
        tcg_gen_ext32u_i64(tcg_rd, cpu_reg(s, rn));
 
3422
        tcg_gen_bswap32_i64(tcg_rd, tcg_rd);
 
3423
    }
 
3424
}
 
3425
 
 
3426
/* C5.6.150 REV16 (opcode==1) */
 
3427
static void handle_rev16(DisasContext *s, unsigned int sf,
 
3428
                         unsigned int rn, unsigned int rd)
 
3429
{
 
3430
    TCGv_i64 tcg_rd = cpu_reg(s, rd);
 
3431
    TCGv_i64 tcg_tmp = tcg_temp_new_i64();
 
3432
    TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
 
3433
 
 
3434
    tcg_gen_andi_i64(tcg_tmp, tcg_rn, 0xffff);
 
3435
    tcg_gen_bswap16_i64(tcg_rd, tcg_tmp);
 
3436
 
 
3437
    tcg_gen_shri_i64(tcg_tmp, tcg_rn, 16);
 
3438
    tcg_gen_andi_i64(tcg_tmp, tcg_tmp, 0xffff);
 
3439
    tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp);
 
3440
    tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, 16, 16);
 
3441
 
 
3442
    if (sf) {
 
3443
        tcg_gen_shri_i64(tcg_tmp, tcg_rn, 32);
 
3444
        tcg_gen_andi_i64(tcg_tmp, tcg_tmp, 0xffff);
 
3445
        tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp);
 
3446
        tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, 32, 16);
 
3447
 
 
3448
        tcg_gen_shri_i64(tcg_tmp, tcg_rn, 48);
 
3449
        tcg_gen_bswap16_i64(tcg_tmp, tcg_tmp);
 
3450
        tcg_gen_deposit_i64(tcg_rd, tcg_rd, tcg_tmp, 48, 16);
 
3451
    }
 
3452
 
 
3453
    tcg_temp_free_i64(tcg_tmp);
 
3454
}
 
3455
 
 
3456
/* C3.5.7 Data-processing (1 source)
 
3457
 *   31  30  29  28             21 20     16 15    10 9    5 4    0
 
3458
 * +----+---+---+-----------------+---------+--------+------+------+
 
3459
 * | sf | 1 | S | 1 1 0 1 0 1 1 0 | opcode2 | opcode |  Rn  |  Rd  |
 
3460
 * +----+---+---+-----------------+---------+--------+------+------+
 
3461
 */
 
3462
static void disas_data_proc_1src(DisasContext *s, uint32_t insn)
 
3463
{
 
3464
    unsigned int sf, opcode, rn, rd;
 
3465
 
 
3466
    if (extract32(insn, 29, 1) || extract32(insn, 16, 5)) {
 
3467
        unallocated_encoding(s);
 
3468
        return;
 
3469
    }
 
3470
 
 
3471
    sf = extract32(insn, 31, 1);
 
3472
    opcode = extract32(insn, 10, 6);
 
3473
    rn = extract32(insn, 5, 5);
 
3474
    rd = extract32(insn, 0, 5);
 
3475
 
 
3476
    switch (opcode) {
 
3477
    case 0: /* RBIT */
 
3478
        handle_rbit(s, sf, rn, rd);
 
3479
        break;
 
3480
    case 1: /* REV16 */
 
3481
        handle_rev16(s, sf, rn, rd);
 
3482
        break;
 
3483
    case 2: /* REV32 */
 
3484
        handle_rev32(s, sf, rn, rd);
 
3485
        break;
 
3486
    case 3: /* REV64 */
 
3487
        handle_rev64(s, sf, rn, rd);
 
3488
        break;
 
3489
    case 4: /* CLZ */
 
3490
        handle_clz(s, sf, rn, rd);
 
3491
        break;
 
3492
    case 5: /* CLS */
 
3493
        handle_cls(s, sf, rn, rd);
 
3494
        break;
 
3495
    }
 
3496
}
 
3497
 
 
3498
static void handle_div(DisasContext *s, bool is_signed, unsigned int sf,
 
3499
                       unsigned int rm, unsigned int rn, unsigned int rd)
 
3500
{
 
3501
    TCGv_i64 tcg_n, tcg_m, tcg_rd;
 
3502
    tcg_rd = cpu_reg(s, rd);
 
3503
 
 
3504
    if (!sf && is_signed) {
 
3505
        tcg_n = new_tmp_a64(s);
 
3506
        tcg_m = new_tmp_a64(s);
 
3507
        tcg_gen_ext32s_i64(tcg_n, cpu_reg(s, rn));
 
3508
        tcg_gen_ext32s_i64(tcg_m, cpu_reg(s, rm));
 
3509
    } else {
 
3510
        tcg_n = read_cpu_reg(s, rn, sf);
 
3511
        tcg_m = read_cpu_reg(s, rm, sf);
 
3512
    }
 
3513
 
 
3514
    if (is_signed) {
 
3515
        gen_helper_sdiv64(tcg_rd, tcg_n, tcg_m);
 
3516
    } else {
 
3517
        gen_helper_udiv64(tcg_rd, tcg_n, tcg_m);
 
3518
    }
 
3519
 
 
3520
    if (!sf) { /* zero extend final result */
 
3521
        tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
 
3522
    }
 
3523
}
 
3524
 
 
3525
/* C5.6.115 LSLV, C5.6.118 LSRV, C5.6.17 ASRV, C5.6.154 RORV */
 
3526
static void handle_shift_reg(DisasContext *s,
 
3527
                             enum a64_shift_type shift_type, unsigned int sf,
 
3528
                             unsigned int rm, unsigned int rn, unsigned int rd)
 
3529
{
 
3530
    TCGv_i64 tcg_shift = tcg_temp_new_i64();
 
3531
    TCGv_i64 tcg_rd = cpu_reg(s, rd);
 
3532
    TCGv_i64 tcg_rn = read_cpu_reg(s, rn, sf);
 
3533
 
 
3534
    tcg_gen_andi_i64(tcg_shift, cpu_reg(s, rm), sf ? 63 : 31);
 
3535
    shift_reg(tcg_rd, tcg_rn, sf, shift_type, tcg_shift);
 
3536
    tcg_temp_free_i64(tcg_shift);
 
3537
}
 
3538
 
 
3539
/* C3.5.8 Data-processing (2 source)
 
3540
 *   31   30  29 28             21 20  16 15    10 9    5 4    0
 
3541
 * +----+---+---+-----------------+------+--------+------+------+
 
3542
 * | sf | 0 | S | 1 1 0 1 0 1 1 0 |  Rm  | opcode |  Rn  |  Rd  |
 
3543
 * +----+---+---+-----------------+------+--------+------+------+
 
3544
 */
 
3545
static void disas_data_proc_2src(DisasContext *s, uint32_t insn)
 
3546
{
 
3547
    unsigned int sf, rm, opcode, rn, rd;
 
3548
    sf = extract32(insn, 31, 1);
 
3549
    rm = extract32(insn, 16, 5);
 
3550
    opcode = extract32(insn, 10, 6);
 
3551
    rn = extract32(insn, 5, 5);
 
3552
    rd = extract32(insn, 0, 5);
 
3553
 
 
3554
    if (extract32(insn, 29, 1)) {
 
3555
        unallocated_encoding(s);
 
3556
        return;
 
3557
    }
 
3558
 
 
3559
    switch (opcode) {
 
3560
    case 2: /* UDIV */
 
3561
        handle_div(s, false, sf, rm, rn, rd);
 
3562
        break;
 
3563
    case 3: /* SDIV */
 
3564
        handle_div(s, true, sf, rm, rn, rd);
 
3565
        break;
 
3566
    case 8: /* LSLV */
 
3567
        handle_shift_reg(s, A64_SHIFT_TYPE_LSL, sf, rm, rn, rd);
 
3568
        break;
 
3569
    case 9: /* LSRV */
 
3570
        handle_shift_reg(s, A64_SHIFT_TYPE_LSR, sf, rm, rn, rd);
 
3571
        break;
 
3572
    case 10: /* ASRV */
 
3573
        handle_shift_reg(s, A64_SHIFT_TYPE_ASR, sf, rm, rn, rd);
 
3574
        break;
 
3575
    case 11: /* RORV */
 
3576
        handle_shift_reg(s, A64_SHIFT_TYPE_ROR, sf, rm, rn, rd);
 
3577
        break;
 
3578
    case 16:
 
3579
    case 17:
 
3580
    case 18:
 
3581
    case 19:
 
3582
    case 20:
 
3583
    case 21:
 
3584
    case 22:
 
3585
    case 23: /* CRC32 */
 
3586
        unsupported_encoding(s, insn);
 
3587
        break;
 
3588
    default:
 
3589
        unallocated_encoding(s);
 
3590
        break;
 
3591
    }
 
3592
}
 
3593
 
 
3594
/* C3.5 Data processing - register */
 
3595
static void disas_data_proc_reg(DisasContext *s, uint32_t insn)
 
3596
{
 
3597
    switch (extract32(insn, 24, 5)) {
 
3598
    case 0x0a: /* Logical (shifted register) */
 
3599
        disas_logic_reg(s, insn);
 
3600
        break;
 
3601
    case 0x0b: /* Add/subtract */
 
3602
        if (insn & (1 << 21)) { /* (extended register) */
 
3603
            disas_add_sub_ext_reg(s, insn);
 
3604
        } else {
 
3605
            disas_add_sub_reg(s, insn);
 
3606
        }
 
3607
        break;
 
3608
    case 0x1b: /* Data-processing (3 source) */
 
3609
        disas_data_proc_3src(s, insn);
 
3610
        break;
 
3611
    case 0x1a:
 
3612
        switch (extract32(insn, 21, 3)) {
 
3613
        case 0x0: /* Add/subtract (with carry) */
 
3614
            disas_adc_sbc(s, insn);
 
3615
            break;
 
3616
        case 0x2: /* Conditional compare */
 
3617
            disas_cc(s, insn); /* both imm and reg forms */
 
3618
            break;
 
3619
        case 0x4: /* Conditional select */
 
3620
            disas_cond_select(s, insn);
 
3621
            break;
 
3622
        case 0x6: /* Data-processing */
 
3623
            if (insn & (1 << 30)) { /* (1 source) */
 
3624
                disas_data_proc_1src(s, insn);
 
3625
            } else {            /* (2 source) */
 
3626
                disas_data_proc_2src(s, insn);
 
3627
            }
 
3628
            break;
 
3629
        default:
 
3630
            unallocated_encoding(s);
 
3631
            break;
 
3632
        }
 
3633
        break;
 
3634
    default:
 
3635
        unallocated_encoding(s);
 
3636
        break;
 
3637
    }
 
3638
}
 
3639
 
 
3640
static void handle_fp_compare(DisasContext *s, bool is_double,
 
3641
                              unsigned int rn, unsigned int rm,
 
3642
                              bool cmp_with_zero, bool signal_all_nans)
 
3643
{
 
3644
    TCGv_i64 tcg_flags = tcg_temp_new_i64();
 
3645
    TCGv_ptr fpst = get_fpstatus_ptr();
 
3646
 
 
3647
    if (is_double) {
 
3648
        TCGv_i64 tcg_vn, tcg_vm;
 
3649
 
 
3650
        tcg_vn = read_fp_dreg(s, rn);
 
3651
        if (cmp_with_zero) {
 
3652
            tcg_vm = tcg_const_i64(0);
 
3653
        } else {
 
3654
            tcg_vm = read_fp_dreg(s, rm);
 
3655
        }
 
3656
        if (signal_all_nans) {
 
3657
            gen_helper_vfp_cmped_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
 
3658
        } else {
 
3659
            gen_helper_vfp_cmpd_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
 
3660
        }
 
3661
        tcg_temp_free_i64(tcg_vn);
 
3662
        tcg_temp_free_i64(tcg_vm);
 
3663
    } else {
 
3664
        TCGv_i32 tcg_vn, tcg_vm;
 
3665
 
 
3666
        tcg_vn = read_fp_sreg(s, rn);
 
3667
        if (cmp_with_zero) {
 
3668
            tcg_vm = tcg_const_i32(0);
 
3669
        } else {
 
3670
            tcg_vm = read_fp_sreg(s, rm);
 
3671
        }
 
3672
        if (signal_all_nans) {
 
3673
            gen_helper_vfp_cmpes_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
 
3674
        } else {
 
3675
            gen_helper_vfp_cmps_a64(tcg_flags, tcg_vn, tcg_vm, fpst);
 
3676
        }
 
3677
        tcg_temp_free_i32(tcg_vn);
 
3678
        tcg_temp_free_i32(tcg_vm);
 
3679
    }
 
3680
 
 
3681
    tcg_temp_free_ptr(fpst);
 
3682
 
 
3683
    gen_set_nzcv(tcg_flags);
 
3684
 
 
3685
    tcg_temp_free_i64(tcg_flags);
 
3686
}
 
3687
 
 
3688
/* C3.6.22 Floating point compare
 
3689
 *   31  30  29 28       24 23  22  21 20  16 15 14 13  10    9    5 4     0
 
3690
 * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
 
3691
 * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | op  | 1 0 0 0 |  Rn  |  op2  |
 
3692
 * +---+---+---+-----------+------+---+------+-----+---------+------+-------+
 
3693
 */
 
3694
static void disas_fp_compare(DisasContext *s, uint32_t insn)
 
3695
{
 
3696
    unsigned int mos, type, rm, op, rn, opc, op2r;
 
3697
 
 
3698
    mos = extract32(insn, 29, 3);
 
3699
    type = extract32(insn, 22, 2); /* 0 = single, 1 = double */
 
3700
    rm = extract32(insn, 16, 5);
 
3701
    op = extract32(insn, 14, 2);
 
3702
    rn = extract32(insn, 5, 5);
 
3703
    opc = extract32(insn, 3, 2);
 
3704
    op2r = extract32(insn, 0, 3);
 
3705
 
 
3706
    if (mos || op || op2r || type > 1) {
 
3707
        unallocated_encoding(s);
 
3708
        return;
 
3709
    }
 
3710
 
 
3711
    handle_fp_compare(s, type, rn, rm, opc & 1, opc & 2);
 
3712
}
 
3713
 
 
3714
/* C3.6.23 Floating point conditional compare
 
3715
 *   31  30  29 28       24 23  22  21 20  16 15  12 11 10 9    5  4   3    0
 
3716
 * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
 
3717
 * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | cond | 0 1 |  Rn  | op | nzcv |
 
3718
 * +---+---+---+-----------+------+---+------+------+-----+------+----+------+
 
3719
 */
 
3720
static void disas_fp_ccomp(DisasContext *s, uint32_t insn)
 
3721
{
 
3722
    unsigned int mos, type, rm, cond, rn, op, nzcv;
 
3723
    TCGv_i64 tcg_flags;
 
3724
    int label_continue = -1;
 
3725
 
 
3726
    mos = extract32(insn, 29, 3);
 
3727
    type = extract32(insn, 22, 2); /* 0 = single, 1 = double */
 
3728
    rm = extract32(insn, 16, 5);
 
3729
    cond = extract32(insn, 12, 4);
 
3730
    rn = extract32(insn, 5, 5);
 
3731
    op = extract32(insn, 4, 1);
 
3732
    nzcv = extract32(insn, 0, 4);
 
3733
 
 
3734
    if (mos || type > 1) {
 
3735
        unallocated_encoding(s);
 
3736
        return;
 
3737
    }
 
3738
 
 
3739
    if (cond < 0x0e) { /* not always */
 
3740
        int label_match = gen_new_label();
 
3741
        label_continue = gen_new_label();
 
3742
        arm_gen_test_cc(cond, label_match);
 
3743
        /* nomatch: */
 
3744
        tcg_flags = tcg_const_i64(nzcv << 28);
 
3745
        gen_set_nzcv(tcg_flags);
 
3746
        tcg_temp_free_i64(tcg_flags);
 
3747
        tcg_gen_br(label_continue);
 
3748
        gen_set_label(label_match);
 
3749
    }
 
3750
 
 
3751
    handle_fp_compare(s, type, rn, rm, false, op);
 
3752
 
 
3753
    if (cond < 0x0e) {
 
3754
        gen_set_label(label_continue);
 
3755
    }
 
3756
}
 
3757
 
 
3758
/* copy src FP register to dst FP register; type specifies single or double */
 
3759
static void gen_mov_fp2fp(DisasContext *s, int type, int dst, int src)
 
3760
{
 
3761
    if (type) {
 
3762
        TCGv_i64 v = read_fp_dreg(s, src);
 
3763
        write_fp_dreg(s, dst, v);
 
3764
        tcg_temp_free_i64(v);
 
3765
    } else {
 
3766
        TCGv_i32 v = read_fp_sreg(s, src);
 
3767
        write_fp_sreg(s, dst, v);
 
3768
        tcg_temp_free_i32(v);
 
3769
    }
 
3770
}
 
3771
 
 
3772
/* C3.6.24 Floating point conditional select
 
3773
 *   31  30  29 28       24 23  22  21 20  16 15  12 11 10 9    5 4    0
 
3774
 * +---+---+---+-----------+------+---+------+------+-----+------+------+
 
3775
 * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | cond | 1 1 |  Rn  |  Rd  |
 
3776
 * +---+---+---+-----------+------+---+------+------+-----+------+------+
 
3777
 */
 
3778
static void disas_fp_csel(DisasContext *s, uint32_t insn)
 
3779
{
 
3780
    unsigned int mos, type, rm, cond, rn, rd;
 
3781
    int label_continue = -1;
 
3782
 
 
3783
    mos = extract32(insn, 29, 3);
 
3784
    type = extract32(insn, 22, 2); /* 0 = single, 1 = double */
 
3785
    rm = extract32(insn, 16, 5);
 
3786
    cond = extract32(insn, 12, 4);
 
3787
    rn = extract32(insn, 5, 5);
 
3788
    rd = extract32(insn, 0, 5);
 
3789
 
 
3790
    if (mos || type > 1) {
 
3791
        unallocated_encoding(s);
 
3792
        return;
 
3793
    }
 
3794
 
 
3795
    if (cond < 0x0e) { /* not always */
 
3796
        int label_match = gen_new_label();
 
3797
        label_continue = gen_new_label();
 
3798
        arm_gen_test_cc(cond, label_match);
 
3799
        /* nomatch: */
 
3800
        gen_mov_fp2fp(s, type, rd, rm);
 
3801
        tcg_gen_br(label_continue);
 
3802
        gen_set_label(label_match);
 
3803
    }
 
3804
 
 
3805
    gen_mov_fp2fp(s, type, rd, rn);
 
3806
 
 
3807
    if (cond < 0x0e) { /* continue */
 
3808
        gen_set_label(label_continue);
 
3809
    }
 
3810
}
 
3811
 
 
3812
/* C3.6.25 Floating-point data-processing (1 source) - single precision */
 
3813
static void handle_fp_1src_single(DisasContext *s, int opcode, int rd, int rn)
 
3814
{
 
3815
    TCGv_ptr fpst;
 
3816
    TCGv_i32 tcg_op;
 
3817
    TCGv_i32 tcg_res;
 
3818
 
 
3819
    fpst = get_fpstatus_ptr();
 
3820
    tcg_op = read_fp_sreg(s, rn);
 
3821
    tcg_res = tcg_temp_new_i32();
 
3822
 
 
3823
    switch (opcode) {
 
3824
    case 0x0: /* FMOV */
 
3825
        tcg_gen_mov_i32(tcg_res, tcg_op);
 
3826
        break;
 
3827
    case 0x1: /* FABS */
 
3828
        gen_helper_vfp_abss(tcg_res, tcg_op);
 
3829
        break;
 
3830
    case 0x2: /* FNEG */
 
3831
        gen_helper_vfp_negs(tcg_res, tcg_op);
 
3832
        break;
 
3833
    case 0x3: /* FSQRT */
 
3834
        gen_helper_vfp_sqrts(tcg_res, tcg_op, cpu_env);
 
3835
        break;
 
3836
    case 0x8: /* FRINTN */
 
3837
    case 0x9: /* FRINTP */
 
3838
    case 0xa: /* FRINTM */
 
3839
    case 0xb: /* FRINTZ */
 
3840
    case 0xc: /* FRINTA */
 
3841
    {
 
3842
        TCGv_i32 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(opcode & 7));
 
3843
 
 
3844
        gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
 
3845
        gen_helper_rints(tcg_res, tcg_op, fpst);
 
3846
 
 
3847
        gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
 
3848
        tcg_temp_free_i32(tcg_rmode);
 
3849
        break;
 
3850
    }
 
3851
    case 0xe: /* FRINTX */
 
3852
        gen_helper_rints_exact(tcg_res, tcg_op, fpst);
 
3853
        break;
 
3854
    case 0xf: /* FRINTI */
 
3855
        gen_helper_rints(tcg_res, tcg_op, fpst);
 
3856
        break;
 
3857
    default:
 
3858
        abort();
 
3859
    }
 
3860
 
 
3861
    write_fp_sreg(s, rd, tcg_res);
 
3862
 
 
3863
    tcg_temp_free_ptr(fpst);
 
3864
    tcg_temp_free_i32(tcg_op);
 
3865
    tcg_temp_free_i32(tcg_res);
 
3866
}
 
3867
 
 
3868
/* C3.6.25 Floating-point data-processing (1 source) - double precision */
 
3869
static void handle_fp_1src_double(DisasContext *s, int opcode, int rd, int rn)
 
3870
{
 
3871
    TCGv_ptr fpst;
 
3872
    TCGv_i64 tcg_op;
 
3873
    TCGv_i64 tcg_res;
 
3874
 
 
3875
    fpst = get_fpstatus_ptr();
 
3876
    tcg_op = read_fp_dreg(s, rn);
 
3877
    tcg_res = tcg_temp_new_i64();
 
3878
 
 
3879
    switch (opcode) {
 
3880
    case 0x0: /* FMOV */
 
3881
        tcg_gen_mov_i64(tcg_res, tcg_op);
 
3882
        break;
 
3883
    case 0x1: /* FABS */
 
3884
        gen_helper_vfp_absd(tcg_res, tcg_op);
 
3885
        break;
 
3886
    case 0x2: /* FNEG */
 
3887
        gen_helper_vfp_negd(tcg_res, tcg_op);
 
3888
        break;
 
3889
    case 0x3: /* FSQRT */
 
3890
        gen_helper_vfp_sqrtd(tcg_res, tcg_op, cpu_env);
 
3891
        break;
 
3892
    case 0x8: /* FRINTN */
 
3893
    case 0x9: /* FRINTP */
 
3894
    case 0xa: /* FRINTM */
 
3895
    case 0xb: /* FRINTZ */
 
3896
    case 0xc: /* FRINTA */
 
3897
    {
 
3898
        TCGv_i32 tcg_rmode = tcg_const_i32(arm_rmode_to_sf(opcode & 7));
 
3899
 
 
3900
        gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
 
3901
        gen_helper_rintd(tcg_res, tcg_op, fpst);
 
3902
 
 
3903
        gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
 
3904
        tcg_temp_free_i32(tcg_rmode);
 
3905
        break;
 
3906
    }
 
3907
    case 0xe: /* FRINTX */
 
3908
        gen_helper_rintd_exact(tcg_res, tcg_op, fpst);
 
3909
        break;
 
3910
    case 0xf: /* FRINTI */
 
3911
        gen_helper_rintd(tcg_res, tcg_op, fpst);
 
3912
        break;
 
3913
    default:
 
3914
        abort();
 
3915
    }
 
3916
 
 
3917
    write_fp_dreg(s, rd, tcg_res);
 
3918
 
 
3919
    tcg_temp_free_ptr(fpst);
 
3920
    tcg_temp_free_i64(tcg_op);
 
3921
    tcg_temp_free_i64(tcg_res);
 
3922
}
 
3923
 
 
3924
static void handle_fp_fcvt(DisasContext *s, int opcode,
 
3925
                           int rd, int rn, int dtype, int ntype)
 
3926
{
 
3927
    switch (ntype) {
 
3928
    case 0x0:
 
3929
    {
 
3930
        TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
 
3931
        if (dtype == 1) {
 
3932
            /* Single to double */
 
3933
            TCGv_i64 tcg_rd = tcg_temp_new_i64();
 
3934
            gen_helper_vfp_fcvtds(tcg_rd, tcg_rn, cpu_env);
 
3935
            write_fp_dreg(s, rd, tcg_rd);
 
3936
            tcg_temp_free_i64(tcg_rd);
 
3937
        } else {
 
3938
            /* Single to half */
 
3939
            TCGv_i32 tcg_rd = tcg_temp_new_i32();
 
3940
            gen_helper_vfp_fcvt_f32_to_f16(tcg_rd, tcg_rn, cpu_env);
 
3941
            /* write_fp_sreg is OK here because top half of tcg_rd is zero */
 
3942
            write_fp_sreg(s, rd, tcg_rd);
 
3943
            tcg_temp_free_i32(tcg_rd);
 
3944
        }
 
3945
        tcg_temp_free_i32(tcg_rn);
 
3946
        break;
 
3947
    }
 
3948
    case 0x1:
 
3949
    {
 
3950
        TCGv_i64 tcg_rn = read_fp_dreg(s, rn);
 
3951
        TCGv_i32 tcg_rd = tcg_temp_new_i32();
 
3952
        if (dtype == 0) {
 
3953
            /* Double to single */
 
3954
            gen_helper_vfp_fcvtsd(tcg_rd, tcg_rn, cpu_env);
 
3955
        } else {
 
3956
            /* Double to half */
 
3957
            gen_helper_vfp_fcvt_f64_to_f16(tcg_rd, tcg_rn, cpu_env);
 
3958
            /* write_fp_sreg is OK here because top half of tcg_rd is zero */
 
3959
        }
 
3960
        write_fp_sreg(s, rd, tcg_rd);
 
3961
        tcg_temp_free_i32(tcg_rd);
 
3962
        tcg_temp_free_i64(tcg_rn);
 
3963
        break;
 
3964
    }
 
3965
    case 0x3:
 
3966
    {
 
3967
        TCGv_i32 tcg_rn = read_fp_sreg(s, rn);
 
3968
        tcg_gen_ext16u_i32(tcg_rn, tcg_rn);
 
3969
        if (dtype == 0) {
 
3970
            /* Half to single */
 
3971
            TCGv_i32 tcg_rd = tcg_temp_new_i32();
 
3972
            gen_helper_vfp_fcvt_f16_to_f32(tcg_rd, tcg_rn, cpu_env);
 
3973
            write_fp_sreg(s, rd, tcg_rd);
 
3974
            tcg_temp_free_i32(tcg_rd);
 
3975
        } else {
 
3976
            /* Half to double */
 
3977
            TCGv_i64 tcg_rd = tcg_temp_new_i64();
 
3978
            gen_helper_vfp_fcvt_f16_to_f64(tcg_rd, tcg_rn, cpu_env);
 
3979
            write_fp_dreg(s, rd, tcg_rd);
 
3980
            tcg_temp_free_i64(tcg_rd);
 
3981
        }
 
3982
        tcg_temp_free_i32(tcg_rn);
 
3983
        break;
 
3984
    }
 
3985
    default:
 
3986
        abort();
 
3987
    }
 
3988
}
 
3989
 
 
3990
/* C3.6.25 Floating point data-processing (1 source)
 
3991
 *   31  30  29 28       24 23  22  21 20    15 14       10 9    5 4    0
 
3992
 * +---+---+---+-----------+------+---+--------+-----------+------+------+
 
3993
 * | M | 0 | S | 1 1 1 1 0 | type | 1 | opcode | 1 0 0 0 0 |  Rn  |  Rd  |
 
3994
 * +---+---+---+-----------+------+---+--------+-----------+------+------+
 
3995
 */
 
3996
static void disas_fp_1src(DisasContext *s, uint32_t insn)
 
3997
{
 
3998
    int type = extract32(insn, 22, 2);
 
3999
    int opcode = extract32(insn, 15, 6);
 
4000
    int rn = extract32(insn, 5, 5);
 
4001
    int rd = extract32(insn, 0, 5);
 
4002
 
 
4003
    switch (opcode) {
 
4004
    case 0x4: case 0x5: case 0x7:
 
4005
    {
 
4006
        /* FCVT between half, single and double precision */
 
4007
        int dtype = extract32(opcode, 0, 2);
 
4008
        if (type == 2 || dtype == type) {
 
4009
            unallocated_encoding(s);
 
4010
            return;
 
4011
        }
 
4012
        handle_fp_fcvt(s, opcode, rd, rn, dtype, type);
 
4013
        break;
 
4014
    }
 
4015
    case 0x0 ... 0x3:
 
4016
    case 0x8 ... 0xc:
 
4017
    case 0xe ... 0xf:
 
4018
        /* 32-to-32 and 64-to-64 ops */
 
4019
        switch (type) {
 
4020
        case 0:
 
4021
            handle_fp_1src_single(s, opcode, rd, rn);
 
4022
            break;
 
4023
        case 1:
 
4024
            handle_fp_1src_double(s, opcode, rd, rn);
 
4025
            break;
 
4026
        default:
 
4027
            unallocated_encoding(s);
 
4028
        }
 
4029
        break;
 
4030
    default:
 
4031
        unallocated_encoding(s);
 
4032
        break;
 
4033
    }
 
4034
}
 
4035
 
 
4036
/* C3.6.26 Floating-point data-processing (2 source) - single precision */
 
4037
static void handle_fp_2src_single(DisasContext *s, int opcode,
 
4038
                                  int rd, int rn, int rm)
 
4039
{
 
4040
    TCGv_i32 tcg_op1;
 
4041
    TCGv_i32 tcg_op2;
 
4042
    TCGv_i32 tcg_res;
 
4043
    TCGv_ptr fpst;
 
4044
 
 
4045
    tcg_res = tcg_temp_new_i32();
 
4046
    fpst = get_fpstatus_ptr();
 
4047
    tcg_op1 = read_fp_sreg(s, rn);
 
4048
    tcg_op2 = read_fp_sreg(s, rm);
 
4049
 
 
4050
    switch (opcode) {
 
4051
    case 0x0: /* FMUL */
 
4052
        gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
 
4053
        break;
 
4054
    case 0x1: /* FDIV */
 
4055
        gen_helper_vfp_divs(tcg_res, tcg_op1, tcg_op2, fpst);
 
4056
        break;
 
4057
    case 0x2: /* FADD */
 
4058
        gen_helper_vfp_adds(tcg_res, tcg_op1, tcg_op2, fpst);
 
4059
        break;
 
4060
    case 0x3: /* FSUB */
 
4061
        gen_helper_vfp_subs(tcg_res, tcg_op1, tcg_op2, fpst);
 
4062
        break;
 
4063
    case 0x4: /* FMAX */
 
4064
        gen_helper_vfp_maxs(tcg_res, tcg_op1, tcg_op2, fpst);
 
4065
        break;
 
4066
    case 0x5: /* FMIN */
 
4067
        gen_helper_vfp_mins(tcg_res, tcg_op1, tcg_op2, fpst);
 
4068
        break;
 
4069
    case 0x6: /* FMAXNM */
 
4070
        gen_helper_vfp_maxnums(tcg_res, tcg_op1, tcg_op2, fpst);
 
4071
        break;
 
4072
    case 0x7: /* FMINNM */
 
4073
        gen_helper_vfp_minnums(tcg_res, tcg_op1, tcg_op2, fpst);
 
4074
        break;
 
4075
    case 0x8: /* FNMUL */
 
4076
        gen_helper_vfp_muls(tcg_res, tcg_op1, tcg_op2, fpst);
 
4077
        gen_helper_vfp_negs(tcg_res, tcg_res);
 
4078
        break;
 
4079
    }
 
4080
 
 
4081
    write_fp_sreg(s, rd, tcg_res);
 
4082
 
 
4083
    tcg_temp_free_ptr(fpst);
 
4084
    tcg_temp_free_i32(tcg_op1);
 
4085
    tcg_temp_free_i32(tcg_op2);
 
4086
    tcg_temp_free_i32(tcg_res);
 
4087
}
 
4088
 
 
4089
/* C3.6.26 Floating-point data-processing (2 source) - double precision */
 
4090
static void handle_fp_2src_double(DisasContext *s, int opcode,
 
4091
                                  int rd, int rn, int rm)
 
4092
{
 
4093
    TCGv_i64 tcg_op1;
 
4094
    TCGv_i64 tcg_op2;
 
4095
    TCGv_i64 tcg_res;
 
4096
    TCGv_ptr fpst;
 
4097
 
 
4098
    tcg_res = tcg_temp_new_i64();
 
4099
    fpst = get_fpstatus_ptr();
 
4100
    tcg_op1 = read_fp_dreg(s, rn);
 
4101
    tcg_op2 = read_fp_dreg(s, rm);
 
4102
 
 
4103
    switch (opcode) {
 
4104
    case 0x0: /* FMUL */
 
4105
        gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
 
4106
        break;
 
4107
    case 0x1: /* FDIV */
 
4108
        gen_helper_vfp_divd(tcg_res, tcg_op1, tcg_op2, fpst);
 
4109
        break;
 
4110
    case 0x2: /* FADD */
 
4111
        gen_helper_vfp_addd(tcg_res, tcg_op1, tcg_op2, fpst);
 
4112
        break;
 
4113
    case 0x3: /* FSUB */
 
4114
        gen_helper_vfp_subd(tcg_res, tcg_op1, tcg_op2, fpst);
 
4115
        break;
 
4116
    case 0x4: /* FMAX */
 
4117
        gen_helper_vfp_maxd(tcg_res, tcg_op1, tcg_op2, fpst);
 
4118
        break;
 
4119
    case 0x5: /* FMIN */
 
4120
        gen_helper_vfp_mind(tcg_res, tcg_op1, tcg_op2, fpst);
 
4121
        break;
 
4122
    case 0x6: /* FMAXNM */
 
4123
        gen_helper_vfp_maxnumd(tcg_res, tcg_op1, tcg_op2, fpst);
 
4124
        break;
 
4125
    case 0x7: /* FMINNM */
 
4126
        gen_helper_vfp_minnumd(tcg_res, tcg_op1, tcg_op2, fpst);
 
4127
        break;
 
4128
    case 0x8: /* FNMUL */
 
4129
        gen_helper_vfp_muld(tcg_res, tcg_op1, tcg_op2, fpst);
 
4130
        gen_helper_vfp_negd(tcg_res, tcg_res);
 
4131
        break;
 
4132
    }
 
4133
 
 
4134
    write_fp_dreg(s, rd, tcg_res);
 
4135
 
 
4136
    tcg_temp_free_ptr(fpst);
 
4137
    tcg_temp_free_i64(tcg_op1);
 
4138
    tcg_temp_free_i64(tcg_op2);
 
4139
    tcg_temp_free_i64(tcg_res);
 
4140
}
 
4141
 
 
4142
/* C3.6.26 Floating point data-processing (2 source)
 
4143
 *   31  30  29 28       24 23  22  21 20  16 15    12 11 10 9    5 4    0
 
4144
 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
 
4145
 * | M | 0 | S | 1 1 1 1 0 | type | 1 |  Rm  | opcode | 1 0 |  Rn  |  Rd  |
 
4146
 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
 
4147
 */
 
4148
static void disas_fp_2src(DisasContext *s, uint32_t insn)
 
4149
{
 
4150
    int type = extract32(insn, 22, 2);
 
4151
    int rd = extract32(insn, 0, 5);
 
4152
    int rn = extract32(insn, 5, 5);
 
4153
    int rm = extract32(insn, 16, 5);
 
4154
    int opcode = extract32(insn, 12, 4);
 
4155
 
 
4156
    if (opcode > 8) {
 
4157
        unallocated_encoding(s);
 
4158
        return;
 
4159
    }
 
4160
 
 
4161
    switch (type) {
 
4162
    case 0:
 
4163
        handle_fp_2src_single(s, opcode, rd, rn, rm);
 
4164
        break;
 
4165
    case 1:
 
4166
        handle_fp_2src_double(s, opcode, rd, rn, rm);
 
4167
        break;
 
4168
    default:
 
4169
        unallocated_encoding(s);
 
4170
    }
 
4171
}
 
4172
 
 
4173
/* C3.6.27 Floating-point data-processing (3 source) - single precision */
 
4174
static void handle_fp_3src_single(DisasContext *s, bool o0, bool o1,
 
4175
                                  int rd, int rn, int rm, int ra)
 
4176
{
 
4177
    TCGv_i32 tcg_op1, tcg_op2, tcg_op3;
 
4178
    TCGv_i32 tcg_res = tcg_temp_new_i32();
 
4179
    TCGv_ptr fpst = get_fpstatus_ptr();
 
4180
 
 
4181
    tcg_op1 = read_fp_sreg(s, rn);
 
4182
    tcg_op2 = read_fp_sreg(s, rm);
 
4183
    tcg_op3 = read_fp_sreg(s, ra);
 
4184
 
 
4185
    /* These are fused multiply-add, and must be done as one
 
4186
     * floating point operation with no rounding between the
 
4187
     * multiplication and addition steps.
 
4188
     * NB that doing the negations here as separate steps is
 
4189
     * correct : an input NaN should come out with its sign bit
 
4190
     * flipped if it is a negated-input.
 
4191
     */
 
4192
    if (o1 == true) {
 
4193
        gen_helper_vfp_negs(tcg_op3, tcg_op3);
 
4194
    }
 
4195
 
 
4196
    if (o0 != o1) {
 
4197
        gen_helper_vfp_negs(tcg_op1, tcg_op1);
 
4198
    }
 
4199
 
 
4200
    gen_helper_vfp_muladds(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
 
4201
 
 
4202
    write_fp_sreg(s, rd, tcg_res);
 
4203
 
 
4204
    tcg_temp_free_ptr(fpst);
 
4205
    tcg_temp_free_i32(tcg_op1);
 
4206
    tcg_temp_free_i32(tcg_op2);
 
4207
    tcg_temp_free_i32(tcg_op3);
 
4208
    tcg_temp_free_i32(tcg_res);
 
4209
}
 
4210
 
 
4211
/* C3.6.27 Floating-point data-processing (3 source) - double precision */
 
4212
static void handle_fp_3src_double(DisasContext *s, bool o0, bool o1,
 
4213
                                  int rd, int rn, int rm, int ra)
 
4214
{
 
4215
    TCGv_i64 tcg_op1, tcg_op2, tcg_op3;
 
4216
    TCGv_i64 tcg_res = tcg_temp_new_i64();
 
4217
    TCGv_ptr fpst = get_fpstatus_ptr();
 
4218
 
 
4219
    tcg_op1 = read_fp_dreg(s, rn);
 
4220
    tcg_op2 = read_fp_dreg(s, rm);
 
4221
    tcg_op3 = read_fp_dreg(s, ra);
 
4222
 
 
4223
    /* These are fused multiply-add, and must be done as one
 
4224
     * floating point operation with no rounding between the
 
4225
     * multiplication and addition steps.
 
4226
     * NB that doing the negations here as separate steps is
 
4227
     * correct : an input NaN should come out with its sign bit
 
4228
     * flipped if it is a negated-input.
 
4229
     */
 
4230
    if (o1 == true) {
 
4231
        gen_helper_vfp_negd(tcg_op3, tcg_op3);
 
4232
    }
 
4233
 
 
4234
    if (o0 != o1) {
 
4235
        gen_helper_vfp_negd(tcg_op1, tcg_op1);
 
4236
    }
 
4237
 
 
4238
    gen_helper_vfp_muladdd(tcg_res, tcg_op1, tcg_op2, tcg_op3, fpst);
 
4239
 
 
4240
    write_fp_dreg(s, rd, tcg_res);
 
4241
 
 
4242
    tcg_temp_free_ptr(fpst);
 
4243
    tcg_temp_free_i64(tcg_op1);
 
4244
    tcg_temp_free_i64(tcg_op2);
 
4245
    tcg_temp_free_i64(tcg_op3);
 
4246
    tcg_temp_free_i64(tcg_res);
 
4247
}
 
4248
 
 
4249
/* C3.6.27 Floating point data-processing (3 source)
 
4250
 *   31  30  29 28       24 23  22  21  20  16  15  14  10 9    5 4    0
 
4251
 * +---+---+---+-----------+------+----+------+----+------+------+------+
 
4252
 * | M | 0 | S | 1 1 1 1 1 | type | o1 |  Rm  | o0 |  Ra  |  Rn  |  Rd  |
 
4253
 * +---+---+---+-----------+------+----+------+----+------+------+------+
 
4254
 */
 
4255
static void disas_fp_3src(DisasContext *s, uint32_t insn)
 
4256
{
 
4257
    int type = extract32(insn, 22, 2);
 
4258
    int rd = extract32(insn, 0, 5);
 
4259
    int rn = extract32(insn, 5, 5);
 
4260
    int ra = extract32(insn, 10, 5);
 
4261
    int rm = extract32(insn, 16, 5);
 
4262
    bool o0 = extract32(insn, 15, 1);
 
4263
    bool o1 = extract32(insn, 21, 1);
 
4264
 
 
4265
    switch (type) {
 
4266
    case 0:
 
4267
        handle_fp_3src_single(s, o0, o1, rd, rn, rm, ra);
 
4268
        break;
 
4269
    case 1:
 
4270
        handle_fp_3src_double(s, o0, o1, rd, rn, rm, ra);
 
4271
        break;
 
4272
    default:
 
4273
        unallocated_encoding(s);
 
4274
    }
 
4275
}
 
4276
 
 
4277
/* C3.6.28 Floating point immediate
 
4278
 *   31  30  29 28       24 23  22  21 20        13 12   10 9    5 4    0
 
4279
 * +---+---+---+-----------+------+---+------------+-------+------+------+
 
4280
 * | M | 0 | S | 1 1 1 1 0 | type | 1 |    imm8    | 1 0 0 | imm5 |  Rd  |
 
4281
 * +---+---+---+-----------+------+---+------------+-------+------+------+
 
4282
 */
 
4283
static void disas_fp_imm(DisasContext *s, uint32_t insn)
 
4284
{
 
4285
    int rd = extract32(insn, 0, 5);
 
4286
    int imm8 = extract32(insn, 13, 8);
 
4287
    int is_double = extract32(insn, 22, 2);
 
4288
    uint64_t imm;
 
4289
    TCGv_i64 tcg_res;
 
4290
 
 
4291
    if (is_double > 1) {
 
4292
        unallocated_encoding(s);
 
4293
        return;
 
4294
    }
 
4295
 
 
4296
    /* The imm8 encodes the sign bit, enough bits to represent
 
4297
     * an exponent in the range 01....1xx to 10....0xx,
 
4298
     * and the most significant 4 bits of the mantissa; see
 
4299
     * VFPExpandImm() in the v8 ARM ARM.
 
4300
     */
 
4301
    if (is_double) {
 
4302
        imm = (extract32(imm8, 7, 1) ? 0x8000 : 0) |
 
4303
            (extract32(imm8, 6, 1) ? 0x3fc0 : 0x4000) |
 
4304
            extract32(imm8, 0, 6);
 
4305
        imm <<= 48;
 
4306
    } else {
 
4307
        imm = (extract32(imm8, 7, 1) ? 0x8000 : 0) |
 
4308
            (extract32(imm8, 6, 1) ? 0x3e00 : 0x4000) |
 
4309
            (extract32(imm8, 0, 6) << 3);
 
4310
        imm <<= 16;
 
4311
    }
 
4312
 
 
4313
    tcg_res = tcg_const_i64(imm);
 
4314
    write_fp_dreg(s, rd, tcg_res);
 
4315
    tcg_temp_free_i64(tcg_res);
 
4316
}
 
4317
 
 
4318
/* Handle floating point <=> fixed point conversions. Note that we can
 
4319
 * also deal with fp <=> integer conversions as a special case (scale == 64)
 
4320
 * OPTME: consider handling that special case specially or at least skipping
 
4321
 * the call to scalbn in the helpers for zero shifts.
 
4322
 */
 
4323
static void handle_fpfpcvt(DisasContext *s, int rd, int rn, int opcode,
 
4324
                           bool itof, int rmode, int scale, int sf, int type)
 
4325
{
 
4326
    bool is_signed = !(opcode & 1);
 
4327
    bool is_double = type;
 
4328
    TCGv_ptr tcg_fpstatus;
 
4329
    TCGv_i32 tcg_shift;
 
4330
 
 
4331
    tcg_fpstatus = get_fpstatus_ptr();
 
4332
 
 
4333
    tcg_shift = tcg_const_i32(64 - scale);
 
4334
 
 
4335
    if (itof) {
 
4336
        TCGv_i64 tcg_int = cpu_reg(s, rn);
 
4337
        if (!sf) {
 
4338
            TCGv_i64 tcg_extend = new_tmp_a64(s);
 
4339
 
 
4340
            if (is_signed) {
 
4341
                tcg_gen_ext32s_i64(tcg_extend, tcg_int);
 
4342
            } else {
 
4343
                tcg_gen_ext32u_i64(tcg_extend, tcg_int);
 
4344
            }
 
4345
 
 
4346
            tcg_int = tcg_extend;
 
4347
        }
 
4348
 
 
4349
        if (is_double) {
 
4350
            TCGv_i64 tcg_double = tcg_temp_new_i64();
 
4351
            if (is_signed) {
 
4352
                gen_helper_vfp_sqtod(tcg_double, tcg_int,
 
4353
                                     tcg_shift, tcg_fpstatus);
 
4354
            } else {
 
4355
                gen_helper_vfp_uqtod(tcg_double, tcg_int,
 
4356
                                     tcg_shift, tcg_fpstatus);
 
4357
            }
 
4358
            write_fp_dreg(s, rd, tcg_double);
 
4359
            tcg_temp_free_i64(tcg_double);
 
4360
        } else {
 
4361
            TCGv_i32 tcg_single = tcg_temp_new_i32();
 
4362
            if (is_signed) {
 
4363
                gen_helper_vfp_sqtos(tcg_single, tcg_int,
 
4364
                                     tcg_shift, tcg_fpstatus);
 
4365
            } else {
 
4366
                gen_helper_vfp_uqtos(tcg_single, tcg_int,
 
4367
                                     tcg_shift, tcg_fpstatus);
 
4368
            }
 
4369
            write_fp_sreg(s, rd, tcg_single);
 
4370
            tcg_temp_free_i32(tcg_single);
 
4371
        }
 
4372
    } else {
 
4373
        TCGv_i64 tcg_int = cpu_reg(s, rd);
 
4374
        TCGv_i32 tcg_rmode;
 
4375
 
 
4376
        if (extract32(opcode, 2, 1)) {
 
4377
            /* There are too many rounding modes to all fit into rmode,
 
4378
             * so FCVTA[US] is a special case.
 
4379
             */
 
4380
            rmode = FPROUNDING_TIEAWAY;
 
4381
        }
 
4382
 
 
4383
        tcg_rmode = tcg_const_i32(arm_rmode_to_sf(rmode));
 
4384
 
 
4385
        gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
 
4386
 
 
4387
        if (is_double) {
 
4388
            TCGv_i64 tcg_double = read_fp_dreg(s, rn);
 
4389
            if (is_signed) {
 
4390
                if (!sf) {
 
4391
                    gen_helper_vfp_tosld(tcg_int, tcg_double,
 
4392
                                         tcg_shift, tcg_fpstatus);
 
4393
                } else {
 
4394
                    gen_helper_vfp_tosqd(tcg_int, tcg_double,
 
4395
                                         tcg_shift, tcg_fpstatus);
 
4396
                }
 
4397
            } else {
 
4398
                if (!sf) {
 
4399
                    gen_helper_vfp_tould(tcg_int, tcg_double,
 
4400
                                         tcg_shift, tcg_fpstatus);
 
4401
                } else {
 
4402
                    gen_helper_vfp_touqd(tcg_int, tcg_double,
 
4403
                                         tcg_shift, tcg_fpstatus);
 
4404
                }
 
4405
            }
 
4406
            tcg_temp_free_i64(tcg_double);
 
4407
        } else {
 
4408
            TCGv_i32 tcg_single = read_fp_sreg(s, rn);
 
4409
            if (sf) {
 
4410
                if (is_signed) {
 
4411
                    gen_helper_vfp_tosqs(tcg_int, tcg_single,
 
4412
                                         tcg_shift, tcg_fpstatus);
 
4413
                } else {
 
4414
                    gen_helper_vfp_touqs(tcg_int, tcg_single,
 
4415
                                         tcg_shift, tcg_fpstatus);
 
4416
                }
 
4417
            } else {
 
4418
                TCGv_i32 tcg_dest = tcg_temp_new_i32();
 
4419
                if (is_signed) {
 
4420
                    gen_helper_vfp_tosls(tcg_dest, tcg_single,
 
4421
                                         tcg_shift, tcg_fpstatus);
 
4422
                } else {
 
4423
                    gen_helper_vfp_touls(tcg_dest, tcg_single,
 
4424
                                         tcg_shift, tcg_fpstatus);
 
4425
                }
 
4426
                tcg_gen_extu_i32_i64(tcg_int, tcg_dest);
 
4427
                tcg_temp_free_i32(tcg_dest);
 
4428
            }
 
4429
            tcg_temp_free_i32(tcg_single);
 
4430
        }
 
4431
 
 
4432
        gen_helper_set_rmode(tcg_rmode, tcg_rmode, cpu_env);
 
4433
        tcg_temp_free_i32(tcg_rmode);
 
4434
 
 
4435
        if (!sf) {
 
4436
            tcg_gen_ext32u_i64(tcg_int, tcg_int);
 
4437
        }
 
4438
    }
 
4439
 
 
4440
    tcg_temp_free_ptr(tcg_fpstatus);
 
4441
    tcg_temp_free_i32(tcg_shift);
 
4442
}
 
4443
 
 
4444
/* C3.6.29 Floating point <-> fixed point conversions
 
4445
 *   31   30  29 28       24 23  22  21 20   19 18    16 15   10 9    5 4    0
 
4446
 * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
 
4447
 * | sf | 0 | S | 1 1 1 1 0 | type | 0 | rmode | opcode | scale |  Rn  |  Rd  |
 
4448
 * +----+---+---+-----------+------+---+-------+--------+-------+------+------+
 
4449
 */
 
4450
static void disas_fp_fixed_conv(DisasContext *s, uint32_t insn)
 
4451
{
 
4452
    int rd = extract32(insn, 0, 5);
 
4453
    int rn = extract32(insn, 5, 5);
 
4454
    int scale = extract32(insn, 10, 6);
 
4455
    int opcode = extract32(insn, 16, 3);
 
4456
    int rmode = extract32(insn, 19, 2);
 
4457
    int type = extract32(insn, 22, 2);
 
4458
    bool sbit = extract32(insn, 29, 1);
 
4459
    bool sf = extract32(insn, 31, 1);
 
4460
    bool itof;
 
4461
 
 
4462
    if (sbit || (type > 1)
 
4463
        || (!sf && scale < 32)) {
 
4464
        unallocated_encoding(s);
 
4465
        return;
 
4466
    }
 
4467
 
 
4468
    switch ((rmode << 3) | opcode) {
 
4469
    case 0x2: /* SCVTF */
 
4470
    case 0x3: /* UCVTF */
 
4471
        itof = true;
 
4472
        break;
 
4473
    case 0x18: /* FCVTZS */
 
4474
    case 0x19: /* FCVTZU */
 
4475
        itof = false;
 
4476
        break;
 
4477
    default:
 
4478
        unallocated_encoding(s);
 
4479
        return;
 
4480
    }
 
4481
 
 
4482
    handle_fpfpcvt(s, rd, rn, opcode, itof, FPROUNDING_ZERO, scale, sf, type);
 
4483
}
 
4484
 
 
4485
static void handle_fmov(DisasContext *s, int rd, int rn, int type, bool itof)
 
4486
{
 
4487
    /* FMOV: gpr to or from float, double, or top half of quad fp reg,
 
4488
     * without conversion.
 
4489
     */
 
4490
 
 
4491
    if (itof) {
 
4492
        TCGv_i64 tcg_rn = cpu_reg(s, rn);
 
4493
 
 
4494
        switch (type) {
 
4495
        case 0:
 
4496
        {
 
4497
            /* 32 bit */
 
4498
            TCGv_i64 tmp = tcg_temp_new_i64();
 
4499
            tcg_gen_ext32u_i64(tmp, tcg_rn);
 
4500
            tcg_gen_st_i64(tmp, cpu_env, fp_reg_offset(rd, MO_64));
 
4501
            tcg_gen_movi_i64(tmp, 0);
 
4502
            tcg_gen_st_i64(tmp, cpu_env, fp_reg_hi_offset(rd));
 
4503
            tcg_temp_free_i64(tmp);
 
4504
            break;
 
4505
        }
 
4506
        case 1:
 
4507
        {
 
4508
            /* 64 bit */
 
4509
            TCGv_i64 tmp = tcg_const_i64(0);
 
4510
            tcg_gen_st_i64(tcg_rn, cpu_env, fp_reg_offset(rd, MO_64));
 
4511
            tcg_gen_st_i64(tmp, cpu_env, fp_reg_hi_offset(rd));
 
4512
            tcg_temp_free_i64(tmp);
 
4513
            break;
 
4514
        }
 
4515
        case 2:
 
4516
            /* 64 bit to top half. */
 
4517
            tcg_gen_st_i64(tcg_rn, cpu_env, fp_reg_hi_offset(rd));
 
4518
            break;
 
4519
        }
 
4520
    } else {
 
4521
        TCGv_i64 tcg_rd = cpu_reg(s, rd);
 
4522
 
 
4523
        switch (type) {
 
4524
        case 0:
 
4525
            /* 32 bit */
 
4526
            tcg_gen_ld32u_i64(tcg_rd, cpu_env, fp_reg_offset(rn, MO_32));
 
4527
            break;
 
4528
        case 1:
 
4529
            /* 64 bit */
 
4530
            tcg_gen_ld_i64(tcg_rd, cpu_env, fp_reg_offset(rn, MO_64));
 
4531
            break;
 
4532
        case 2:
 
4533
            /* 64 bits from top half */
 
4534
            tcg_gen_ld_i64(tcg_rd, cpu_env, fp_reg_hi_offset(rn));
 
4535
            break;
 
4536
        }
 
4537
    }
 
4538
}
 
4539
 
 
4540
/* C3.6.30 Floating point <-> integer conversions
 
4541
 *   31   30  29 28       24 23  22  21 20   19 18 16 15         10 9  5 4  0
 
4542
 * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
 
4543
 * | sf | 0 | S | 1 1 1 1 0 | type | 1 | rmode | opc | 0 0 0 0 0 0 | Rn | Rd |
 
4544
 * +----+---+---+-----------+------+---+-------+-----+-------------+----+----+
 
4545
 */
 
4546
static void disas_fp_int_conv(DisasContext *s, uint32_t insn)
 
4547
{
 
4548
    int rd = extract32(insn, 0, 5);
 
4549
    int rn = extract32(insn, 5, 5);
 
4550
    int opcode = extract32(insn, 16, 3);
 
4551
    int rmode = extract32(insn, 19, 2);
 
4552
    int type = extract32(insn, 22, 2);
 
4553
    bool sbit = extract32(insn, 29, 1);
 
4554
    bool sf = extract32(insn, 31, 1);
 
4555
 
 
4556
    if (sbit) {
 
4557
        unallocated_encoding(s);
 
4558
        return;
 
4559
    }
 
4560
 
 
4561
    if (opcode > 5) {
 
4562
        /* FMOV */
 
4563
        bool itof = opcode & 1;
 
4564
 
 
4565
        if (rmode >= 2) {
 
4566
            unallocated_encoding(s);
 
4567
            return;
 
4568
        }
 
4569
 
 
4570
        switch (sf << 3 | type << 1 | rmode) {
 
4571
        case 0x0: /* 32 bit */
 
4572
        case 0xa: /* 64 bit */
 
4573
        case 0xd: /* 64 bit to top half of quad */
 
4574
            break;
 
4575
        default:
 
4576
            /* all other sf/type/rmode combinations are invalid */
 
4577
            unallocated_encoding(s);
 
4578
            break;
 
4579
        }
 
4580
 
 
4581
        handle_fmov(s, rd, rn, type, itof);
 
4582
    } else {
 
4583
        /* actual FP conversions */
 
4584
        bool itof = extract32(opcode, 1, 1);
 
4585
 
 
4586
        if (type > 1 || (rmode != 0 && opcode > 1)) {
 
4587
            unallocated_encoding(s);
 
4588
            return;
 
4589
        }
 
4590
 
 
4591
        handle_fpfpcvt(s, rd, rn, opcode, itof, rmode, 64, sf, type);
 
4592
    }
 
4593
}
 
4594
 
 
4595
/* FP-specific subcases of table C3-6 (SIMD and FP data processing)
 
4596
 *   31  30  29 28     25 24                          0
 
4597
 * +---+---+---+---------+-----------------------------+
 
4598
 * |   | 0 |   | 1 1 1 1 |                             |
 
4599
 * +---+---+---+---------+-----------------------------+
 
4600
 */
 
4601
static void disas_data_proc_fp(DisasContext *s, uint32_t insn)
 
4602
{
 
4603
    if (extract32(insn, 24, 1)) {
 
4604
        /* Floating point data-processing (3 source) */
 
4605
        disas_fp_3src(s, insn);
 
4606
    } else if (extract32(insn, 21, 1) == 0) {
 
4607
        /* Floating point to fixed point conversions */
 
4608
        disas_fp_fixed_conv(s, insn);
 
4609
    } else {
 
4610
        switch (extract32(insn, 10, 2)) {
 
4611
        case 1:
 
4612
            /* Floating point conditional compare */
 
4613
            disas_fp_ccomp(s, insn);
 
4614
            break;
 
4615
        case 2:
 
4616
            /* Floating point data-processing (2 source) */
 
4617
            disas_fp_2src(s, insn);
 
4618
            break;
 
4619
        case 3:
 
4620
            /* Floating point conditional select */
 
4621
            disas_fp_csel(s, insn);
 
4622
            break;
 
4623
        case 0:
 
4624
            switch (ctz32(extract32(insn, 12, 4))) {
 
4625
            case 0: /* [15:12] == xxx1 */
 
4626
                /* Floating point immediate */
 
4627
                disas_fp_imm(s, insn);
 
4628
                break;
 
4629
            case 1: /* [15:12] == xx10 */
 
4630
                /* Floating point compare */
 
4631
                disas_fp_compare(s, insn);
 
4632
                break;
 
4633
            case 2: /* [15:12] == x100 */
 
4634
                /* Floating point data-processing (1 source) */
 
4635
                disas_fp_1src(s, insn);
 
4636
                break;
 
4637
            case 3: /* [15:12] == 1000 */
 
4638
                unallocated_encoding(s);
 
4639
                break;
 
4640
            default: /* [15:12] == 0000 */
 
4641
                /* Floating point <-> integer conversions */
 
4642
                disas_fp_int_conv(s, insn);
 
4643
                break;
 
4644
            }
 
4645
            break;
 
4646
        }
 
4647
    }
 
4648
}
 
4649
 
 
4650
static void do_ext64(DisasContext *s, TCGv_i64 tcg_left, TCGv_i64 tcg_right,
 
4651
                     int pos)
 
4652
{
 
4653
    /* Extract 64 bits from the middle of two concatenated 64 bit
 
4654
     * vector register slices left:right. The extracted bits start
 
4655
     * at 'pos' bits into the right (least significant) side.
 
4656
     * We return the result in tcg_right, and guarantee not to
 
4657
     * trash tcg_left.
 
4658
     */
 
4659
    TCGv_i64 tcg_tmp = tcg_temp_new_i64();
 
4660
    assert(pos > 0 && pos < 64);
 
4661
 
 
4662
    tcg_gen_shri_i64(tcg_right, tcg_right, pos);
 
4663
    tcg_gen_shli_i64(tcg_tmp, tcg_left, 64 - pos);
 
4664
    tcg_gen_or_i64(tcg_right, tcg_right, tcg_tmp);
 
4665
 
 
4666
    tcg_temp_free_i64(tcg_tmp);
 
4667
}
 
4668
 
 
4669
/* C3.6.1 EXT
 
4670
 *   31  30 29         24 23 22  21 20  16 15  14  11 10  9    5 4    0
 
4671
 * +---+---+-------------+-----+---+------+---+------+---+------+------+
 
4672
 * | 0 | Q | 1 0 1 1 1 0 | op2 | 0 |  Rm  | 0 | imm4 | 0 |  Rn  |  Rd  |
 
4673
 * +---+---+-------------+-----+---+------+---+------+---+------+------+
 
4674
 */
 
4675
static void disas_simd_ext(DisasContext *s, uint32_t insn)
 
4676
{
 
4677
    int is_q = extract32(insn, 30, 1);
 
4678
    int op2 = extract32(insn, 22, 2);
 
4679
    int imm4 = extract32(insn, 11, 4);
 
4680
    int rm = extract32(insn, 16, 5);
 
4681
    int rn = extract32(insn, 5, 5);
 
4682
    int rd = extract32(insn, 0, 5);
 
4683
    int pos = imm4 << 3;
 
4684
    TCGv_i64 tcg_resl, tcg_resh;
 
4685
 
 
4686
    if (op2 != 0 || (!is_q && extract32(imm4, 3, 1))) {
 
4687
        unallocated_encoding(s);
 
4688
        return;
 
4689
    }
 
4690
 
 
4691
    tcg_resh = tcg_temp_new_i64();
 
4692
    tcg_resl = tcg_temp_new_i64();
 
4693
 
 
4694
    /* Vd gets bits starting at pos bits into Vm:Vn. This is
 
4695
     * either extracting 128 bits from a 128:128 concatenation, or
 
4696
     * extracting 64 bits from a 64:64 concatenation.
 
4697
     */
 
4698
    if (!is_q) {
 
4699
        read_vec_element(s, tcg_resl, rn, 0, MO_64);
 
4700
        if (pos != 0) {
 
4701
            read_vec_element(s, tcg_resh, rm, 0, MO_64);
 
4702
            do_ext64(s, tcg_resh, tcg_resl, pos);
 
4703
        }
 
4704
        tcg_gen_movi_i64(tcg_resh, 0);
 
4705
    } else {
 
4706
        TCGv_i64 tcg_hh;
 
4707
        typedef struct {
 
4708
            int reg;
 
4709
            int elt;
 
4710
        } EltPosns;
 
4711
        EltPosns eltposns[] = { {rn, 0}, {rn, 1}, {rm, 0}, {rm, 1} };
 
4712
        EltPosns *elt = eltposns;
 
4713
 
 
4714
        if (pos >= 64) {
 
4715
            elt++;
 
4716
            pos -= 64;
 
4717
        }
 
4718
 
 
4719
        read_vec_element(s, tcg_resl, elt->reg, elt->elt, MO_64);
 
4720
        elt++;
 
4721
        read_vec_element(s, tcg_resh, elt->reg, elt->elt, MO_64);
 
4722
        elt++;
 
4723
        if (pos != 0) {
 
4724
            do_ext64(s, tcg_resh, tcg_resl, pos);
 
4725
            tcg_hh = tcg_temp_new_i64();
 
4726
            read_vec_element(s, tcg_hh, elt->reg, elt->elt, MO_64);
 
4727
            do_ext64(s, tcg_hh, tcg_resh, pos);
 
4728
            tcg_temp_free_i64(tcg_hh);
 
4729
        }
 
4730
    }
 
4731
 
 
4732
    write_vec_element(s, tcg_resl, rd, 0, MO_64);
 
4733
    tcg_temp_free_i64(tcg_resl);
 
4734
    write_vec_element(s, tcg_resh, rd, 1, MO_64);
 
4735
    tcg_temp_free_i64(tcg_resh);
 
4736
}
 
4737
 
 
4738
/* C3.6.2 TBL/TBX
 
4739
 *   31  30 29         24 23 22  21 20  16 15  14 13  12  11 10 9    5 4    0
 
4740
 * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
 
4741
 * | 0 | Q | 0 0 1 1 1 0 | op2 | 0 |  Rm  | 0 | len | op | 0 0 |  Rn  |  Rd  |
 
4742
 * +---+---+-------------+-----+---+------+---+-----+----+-----+------+------+
 
4743
 */
 
4744
static void disas_simd_tb(DisasContext *s, uint32_t insn)
 
4745
{
 
4746
    int op2 = extract32(insn, 22, 2);
 
4747
    int is_q = extract32(insn, 30, 1);
 
4748
    int rm = extract32(insn, 16, 5);
 
4749
    int rn = extract32(insn, 5, 5);
 
4750
    int rd = extract32(insn, 0, 5);
 
4751
    int is_tblx = extract32(insn, 12, 1);
 
4752
    int len = extract32(insn, 13, 2);
 
4753
    TCGv_i64 tcg_resl, tcg_resh, tcg_idx;
 
4754
    TCGv_i32 tcg_regno, tcg_numregs;
 
4755
 
 
4756
    if (op2 != 0) {
 
4757
        unallocated_encoding(s);
 
4758
        return;
 
4759
    }
 
4760
 
 
4761
    /* This does a table lookup: for every byte element in the input
 
4762
     * we index into a table formed from up to four vector registers,
 
4763
     * and then the output is the result of the lookups. Our helper
 
4764
     * function does the lookup operation for a single 64 bit part of
 
4765
     * the input.
 
4766
     */
 
4767
    tcg_resl = tcg_temp_new_i64();
 
4768
    tcg_resh = tcg_temp_new_i64();
 
4769
 
 
4770
    if (is_tblx) {
 
4771
        read_vec_element(s, tcg_resl, rd, 0, MO_64);
 
4772
    } else {
 
4773
        tcg_gen_movi_i64(tcg_resl, 0);
 
4774
    }
 
4775
    if (is_tblx && is_q) {
 
4776
        read_vec_element(s, tcg_resh, rd, 1, MO_64);
 
4777
    } else {
 
4778
        tcg_gen_movi_i64(tcg_resh, 0);
 
4779
    }
 
4780
 
 
4781
    tcg_idx = tcg_temp_new_i64();
 
4782
    tcg_regno = tcg_const_i32(rn);
 
4783
    tcg_numregs = tcg_const_i32(len + 1);
 
4784
    read_vec_element(s, tcg_idx, rm, 0, MO_64);
 
4785
    gen_helper_simd_tbl(tcg_resl, cpu_env, tcg_resl, tcg_idx,
 
4786
                        tcg_regno, tcg_numregs);
 
4787
    if (is_q) {
 
4788
        read_vec_element(s, tcg_idx, rm, 1, MO_64);
 
4789
        gen_helper_simd_tbl(tcg_resh, cpu_env, tcg_resh, tcg_idx,
 
4790
                            tcg_regno, tcg_numregs);
 
4791
    }
 
4792
    tcg_temp_free_i64(tcg_idx);
 
4793
    tcg_temp_free_i32(tcg_regno);
 
4794
    tcg_temp_free_i32(tcg_numregs);
 
4795
 
 
4796
    write_vec_element(s, tcg_resl, rd, 0, MO_64);
 
4797
    tcg_temp_free_i64(tcg_resl);
 
4798
    write_vec_element(s, tcg_resh, rd, 1, MO_64);
 
4799
    tcg_temp_free_i64(tcg_resh);
 
4800
}
 
4801
 
 
4802
/* C3.6.3 ZIP/UZP/TRN
 
4803
 *   31  30 29         24 23  22  21 20   16 15 14 12 11 10 9    5 4    0
 
4804
 * +---+---+-------------+------+---+------+---+------------------+------+
 
4805
 * | 0 | Q | 0 0 1 1 1 0 | size | 0 |  Rm  | 0 | opc | 1 0 |  Rn  |  Rd  |
 
4806
 * +---+---+-------------+------+---+------+---+------------------+------+
 
4807
 */
 
4808
static void disas_simd_zip_trn(DisasContext *s, uint32_t insn)
 
4809
{
 
4810
    int rd = extract32(insn, 0, 5);
 
4811
    int rn = extract32(insn, 5, 5);
 
4812
    int rm = extract32(insn, 16, 5);
 
4813
    int size = extract32(insn, 22, 2);
 
4814
    /* opc field bits [1:0] indicate ZIP/UZP/TRN;
 
4815
     * bit 2 indicates 1 vs 2 variant of the insn.
 
4816
     */
 
4817
    int opcode = extract32(insn, 12, 2);
 
4818
    bool part = extract32(insn, 14, 1);
 
4819
    bool is_q = extract32(insn, 30, 1);
 
4820
    int esize = 8 << size;
 
4821
    int i, ofs;
 
4822
    int datasize = is_q ? 128 : 64;
 
4823
    int elements = datasize / esize;
 
4824
    TCGv_i64 tcg_res, tcg_resl, tcg_resh;
 
4825
 
 
4826
    if (opcode == 0 || (size == 3 && !is_q)) {
 
4827
        unallocated_encoding(s);
 
4828
        return;
 
4829
    }
 
4830
 
 
4831
    tcg_resl = tcg_const_i64(0);
 
4832
    tcg_resh = tcg_const_i64(0);
 
4833
    tcg_res = tcg_temp_new_i64();
 
4834
 
 
4835
    for (i = 0; i < elements; i++) {
 
4836
        switch (opcode) {
 
4837
        case 1: /* UZP1/2 */
 
4838
        {
 
4839
            int midpoint = elements / 2;
 
4840
            if (i < midpoint) {
 
4841
                read_vec_element(s, tcg_res, rn, 2 * i + part, size);
 
4842
            } else {
 
4843
                read_vec_element(s, tcg_res, rm,
 
4844
                                 2 * (i - midpoint) + part, size);
 
4845
            }
 
4846
            break;
 
4847
        }
 
4848
        case 2: /* TRN1/2 */
 
4849
            if (i & 1) {
 
4850
                read_vec_element(s, tcg_res, rm, (i & ~1) + part, size);
 
4851
            } else {
 
4852
                read_vec_element(s, tcg_res, rn, (i & ~1) + part, size);
 
4853
            }
 
4854
            break;
 
4855
        case 3: /* ZIP1/2 */
 
4856
        {
 
4857
            int base = part * elements / 2;
 
4858
            if (i & 1) {
 
4859
                read_vec_element(s, tcg_res, rm, base + (i >> 1), size);
 
4860
            } else {
 
4861
                read_vec_element(s, tcg_res, rn, base + (i >> 1), size);
 
4862
            }
 
4863
            break;
 
4864
        }
 
4865
        default:
 
4866
            g_assert_not_reached();
 
4867
        }
 
4868
 
 
4869
        ofs = i * esize;
 
4870
        if (ofs < 64) {
 
4871
            tcg_gen_shli_i64(tcg_res, tcg_res, ofs);
 
4872
            tcg_gen_or_i64(tcg_resl, tcg_resl, tcg_res);
 
4873
        } else {
 
4874
            tcg_gen_shli_i64(tcg_res, tcg_res, ofs - 64);
 
4875
            tcg_gen_or_i64(tcg_resh, tcg_resh, tcg_res);
 
4876
        }
 
4877
    }
 
4878
 
 
4879
    tcg_temp_free_i64(tcg_res);
 
4880
 
 
4881
    write_vec_element(s, tcg_resl, rd, 0, MO_64);
 
4882
    tcg_temp_free_i64(tcg_resl);
 
4883
    write_vec_element(s, tcg_resh, rd, 1, MO_64);
 
4884
    tcg_temp_free_i64(tcg_resh);
 
4885
}
 
4886
 
 
4887
static void do_minmaxop(DisasContext *s, TCGv_i32 tcg_elt1, TCGv_i32 tcg_elt2,
 
4888
                        int opc, bool is_min, TCGv_ptr fpst)
 
4889
{
 
4890
    /* Helper function for disas_simd_across_lanes: do a single precision
 
4891
     * min/max operation on the specified two inputs,
 
4892
     * and return the result in tcg_elt1.
 
4893
     */
 
4894
    if (opc == 0xc) {
 
4895
        if (is_min) {
 
4896
            gen_helper_vfp_minnums(tcg_elt1, tcg_elt1, tcg_elt2, fpst);
 
4897
        } else {
 
4898
            gen_helper_vfp_maxnums(tcg_elt1, tcg_elt1, tcg_elt2, fpst);
 
4899
        }
 
4900
    } else {
 
4901
        assert(opc == 0xf);
 
4902
        if (is_min) {
 
4903
            gen_helper_vfp_mins(tcg_elt1, tcg_elt1, tcg_elt2, fpst);
 
4904
        } else {
 
4905
            gen_helper_vfp_maxs(tcg_elt1, tcg_elt1, tcg_elt2, fpst);
 
4906
        }
 
4907
    }
 
4908
}
 
4909
 
 
4910
/* C3.6.4 AdvSIMD across lanes
 
4911
 *   31  30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
 
4912
 * +---+---+---+-----------+------+-----------+--------+-----+------+------+
 
4913
 * | 0 | Q | U | 0 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
 
4914
 * +---+---+---+-----------+------+-----------+--------+-----+------+------+
 
4915
 */
 
4916
static void disas_simd_across_lanes(DisasContext *s, uint32_t insn)
 
4917
{
 
4918
    int rd = extract32(insn, 0, 5);
 
4919
    int rn = extract32(insn, 5, 5);
 
4920
    int size = extract32(insn, 22, 2);
 
4921
    int opcode = extract32(insn, 12, 5);
 
4922
    bool is_q = extract32(insn, 30, 1);
 
4923
    bool is_u = extract32(insn, 29, 1);
 
4924
    bool is_fp = false;
 
4925
    bool is_min = false;
 
4926
    int esize;
 
4927
    int elements;
 
4928
    int i;
 
4929
    TCGv_i64 tcg_res, tcg_elt;
 
4930
 
 
4931
    switch (opcode) {
 
4932
    case 0x1b: /* ADDV */
 
4933
        if (is_u) {
 
4934
            unallocated_encoding(s);
 
4935
            return;
 
4936
        }
 
4937
        /* fall through */
 
4938
    case 0x3: /* SADDLV, UADDLV */
 
4939
    case 0xa: /* SMAXV, UMAXV */
 
4940
    case 0x1a: /* SMINV, UMINV */
 
4941
        if (size == 3 || (size == 2 && !is_q)) {
 
4942
            unallocated_encoding(s);
 
4943
            return;
 
4944
        }
 
4945
        break;
 
4946
    case 0xc: /* FMAXNMV, FMINNMV */
 
4947
    case 0xf: /* FMAXV, FMINV */
 
4948
        if (!is_u || !is_q || extract32(size, 0, 1)) {
 
4949
            unallocated_encoding(s);
 
4950
            return;
 
4951
        }
 
4952
        /* Bit 1 of size field encodes min vs max, and actual size is always
 
4953
         * 32 bits: adjust the size variable so following code can rely on it
 
4954
         */
 
4955
        is_min = extract32(size, 1, 1);
 
4956
        is_fp = true;
 
4957
        size = 2;
 
4958
        break;
 
4959
    default:
 
4960
        unallocated_encoding(s);
 
4961
        return;
 
4962
    }
 
4963
 
 
4964
    esize = 8 << size;
 
4965
    elements = (is_q ? 128 : 64) / esize;
 
4966
 
 
4967
    tcg_res = tcg_temp_new_i64();
 
4968
    tcg_elt = tcg_temp_new_i64();
 
4969
 
 
4970
    /* These instructions operate across all lanes of a vector
 
4971
     * to produce a single result. We can guarantee that a 64
 
4972
     * bit intermediate is sufficient:
 
4973
     *  + for [US]ADDLV the maximum element size is 32 bits, and
 
4974
     *    the result type is 64 bits
 
4975
     *  + for FMAX*V, FMIN*V, ADDV the intermediate type is the
 
4976
     *    same as the element size, which is 32 bits at most
 
4977
     * For the integer operations we can choose to work at 64
 
4978
     * or 32 bits and truncate at the end; for simplicity
 
4979
     * we use 64 bits always. The floating point
 
4980
     * ops do require 32 bit intermediates, though.
 
4981
     */
 
4982
    if (!is_fp) {
 
4983
        read_vec_element(s, tcg_res, rn, 0, size | (is_u ? 0 : MO_SIGN));
 
4984
 
 
4985
        for (i = 1; i < elements; i++) {
 
4986
            read_vec_element(s, tcg_elt, rn, i, size | (is_u ? 0 : MO_SIGN));
 
4987
 
 
4988
            switch (opcode) {
 
4989
            case 0x03: /* SADDLV / UADDLV */
 
4990
            case 0x1b: /* ADDV */
 
4991
                tcg_gen_add_i64(tcg_res, tcg_res, tcg_elt);
 
4992
                break;
 
4993
            case 0x0a: /* SMAXV / UMAXV */
 
4994
                tcg_gen_movcond_i64(is_u ? TCG_COND_GEU : TCG_COND_GE,
 
4995
                                    tcg_res,
 
4996
                                    tcg_res, tcg_elt, tcg_res, tcg_elt);
 
4997
                break;
 
4998
            case 0x1a: /* SMINV / UMINV */
 
4999
                tcg_gen_movcond_i64(is_u ? TCG_COND_LEU : TCG_COND_LE,
 
5000
                                    tcg_res,
 
5001
                                    tcg_res, tcg_elt, tcg_res, tcg_elt);
 
5002
                break;
 
5003
                break;
 
5004
            default:
 
5005
                g_assert_not_reached();
 
5006
            }
 
5007
 
 
5008
        }
 
5009
    } else {
 
5010
        /* Floating point ops which work on 32 bit (single) intermediates.
 
5011
         * Note that correct NaN propagation requires that we do these
 
5012
         * operations in exactly the order specified by the pseudocode.
 
5013
         */
 
5014
        TCGv_i32 tcg_elt1 = tcg_temp_new_i32();
 
5015
        TCGv_i32 tcg_elt2 = tcg_temp_new_i32();
 
5016
        TCGv_i32 tcg_elt3 = tcg_temp_new_i32();
 
5017
        TCGv_ptr fpst = get_fpstatus_ptr();
 
5018
 
 
5019
        assert(esize == 32);
 
5020
        assert(elements == 4);
 
5021
 
 
5022
        read_vec_element(s, tcg_elt, rn, 0, MO_32);
 
5023
        tcg_gen_trunc_i64_i32(tcg_elt1, tcg_elt);
 
5024
        read_vec_element(s, tcg_elt, rn, 1, MO_32);
 
5025
        tcg_gen_trunc_i64_i32(tcg_elt2, tcg_elt);
 
5026
 
 
5027
        do_minmaxop(s, tcg_elt1, tcg_elt2, opcode, is_min, fpst);
 
5028
 
 
5029
        read_vec_element(s, tcg_elt, rn, 2, MO_32);
 
5030
        tcg_gen_trunc_i64_i32(tcg_elt2, tcg_elt);
 
5031
        read_vec_element(s, tcg_elt, rn, 3, MO_32);
 
5032
        tcg_gen_trunc_i64_i32(tcg_elt3, tcg_elt);
 
5033
 
 
5034
        do_minmaxop(s, tcg_elt2, tcg_elt3, opcode, is_min, fpst);
 
5035
 
 
5036
        do_minmaxop(s, tcg_elt1, tcg_elt2, opcode, is_min, fpst);
 
5037
 
 
5038
        tcg_gen_extu_i32_i64(tcg_res, tcg_elt1);
 
5039
        tcg_temp_free_i32(tcg_elt1);
 
5040
        tcg_temp_free_i32(tcg_elt2);
 
5041
        tcg_temp_free_i32(tcg_elt3);
 
5042
        tcg_temp_free_ptr(fpst);
 
5043
    }
 
5044
 
 
5045
    tcg_temp_free_i64(tcg_elt);
 
5046
 
 
5047
    /* Now truncate the result to the width required for the final output */
 
5048
    if (opcode == 0x03) {
 
5049
        /* SADDLV, UADDLV: result is 2*esize */
 
5050
        size++;
 
5051
    }
 
5052
 
 
5053
    switch (size) {
 
5054
    case 0:
 
5055
        tcg_gen_ext8u_i64(tcg_res, tcg_res);
 
5056
        break;
 
5057
    case 1:
 
5058
        tcg_gen_ext16u_i64(tcg_res, tcg_res);
 
5059
        break;
 
5060
    case 2:
 
5061
        tcg_gen_ext32u_i64(tcg_res, tcg_res);
 
5062
        break;
 
5063
    case 3:
 
5064
        break;
 
5065
    default:
 
5066
        g_assert_not_reached();
 
5067
    }
 
5068
 
 
5069
    write_fp_dreg(s, rd, tcg_res);
 
5070
    tcg_temp_free_i64(tcg_res);
 
5071
}
 
5072
 
 
5073
/* C6.3.31 DUP (Element, Vector)
 
5074
 *
 
5075
 *  31  30   29              21 20    16 15        10  9    5 4    0
 
5076
 * +---+---+-------------------+--------+-------------+------+------+
 
5077
 * | 0 | Q | 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 0 0 0 1 |  Rn  |  Rd  |
 
5078
 * +---+---+-------------------+--------+-------------+------+------+
 
5079
 *
 
5080
 * size: encoded in imm5 (see ARM ARM LowestSetBit())
 
5081
 */
 
5082
static void handle_simd_dupe(DisasContext *s, int is_q, int rd, int rn,
 
5083
                             int imm5)
 
5084
{
 
5085
    int size = ctz32(imm5);
 
5086
    int esize = 8 << size;
 
5087
    int elements = (is_q ? 128 : 64) / esize;
 
5088
    int index, i;
 
5089
    TCGv_i64 tmp;
 
5090
 
 
5091
    if (size > 3 || (size == 3 && !is_q)) {
 
5092
        unallocated_encoding(s);
 
5093
        return;
 
5094
    }
 
5095
 
 
5096
    index = imm5 >> (size + 1);
 
5097
 
 
5098
    tmp = tcg_temp_new_i64();
 
5099
    read_vec_element(s, tmp, rn, index, size);
 
5100
 
 
5101
    for (i = 0; i < elements; i++) {
 
5102
        write_vec_element(s, tmp, rd, i, size);
 
5103
    }
 
5104
 
 
5105
    if (!is_q) {
 
5106
        clear_vec_high(s, rd);
 
5107
    }
 
5108
 
 
5109
    tcg_temp_free_i64(tmp);
 
5110
}
 
5111
 
 
5112
/* C6.3.31 DUP (element, scalar)
 
5113
 *  31                   21 20    16 15        10  9    5 4    0
 
5114
 * +-----------------------+--------+-------------+------+------+
 
5115
 * | 0 1 0 1 1 1 1 0 0 0 0 |  imm5  | 0 0 0 0 0 1 |  Rn  |  Rd  |
 
5116
 * +-----------------------+--------+-------------+------+------+
 
5117
 */
 
5118
static void handle_simd_dupes(DisasContext *s, int rd, int rn,
 
5119
                              int imm5)
 
5120
{
 
5121
    int size = ctz32(imm5);
 
5122
    int index;
 
5123
    TCGv_i64 tmp;
 
5124
 
 
5125
    if (size > 3) {
 
5126
        unallocated_encoding(s);
 
5127
        return;
 
5128
    }
 
5129
 
 
5130
    index = imm5 >> (size + 1);
 
5131
 
 
5132
    /* This instruction just extracts the specified element and
 
5133
     * zero-extends it into the bottom of the destination register.
 
5134
     */
 
5135
    tmp = tcg_temp_new_i64();
 
5136
    read_vec_element(s, tmp, rn, index, size);
 
5137
    write_fp_dreg(s, rd, tmp);
 
5138
    tcg_temp_free_i64(tmp);
 
5139
}
 
5140
 
 
5141
/* C6.3.32 DUP (General)
 
5142
 *
 
5143
 *  31  30   29              21 20    16 15        10  9    5 4    0
 
5144
 * +---+---+-------------------+--------+-------------+------+------+
 
5145
 * | 0 | Q | 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 0 0 1 1 |  Rn  |  Rd  |
 
5146
 * +---+---+-------------------+--------+-------------+------+------+
 
5147
 *
 
5148
 * size: encoded in imm5 (see ARM ARM LowestSetBit())
 
5149
 */
 
5150
static void handle_simd_dupg(DisasContext *s, int is_q, int rd, int rn,
 
5151
                             int imm5)
 
5152
{
 
5153
    int size = ctz32(imm5);
 
5154
    int esize = 8 << size;
 
5155
    int elements = (is_q ? 128 : 64)/esize;
 
5156
    int i = 0;
 
5157
 
 
5158
    if (size > 3 || ((size == 3) && !is_q)) {
 
5159
        unallocated_encoding(s);
 
5160
        return;
 
5161
    }
 
5162
    for (i = 0; i < elements; i++) {
 
5163
        write_vec_element(s, cpu_reg(s, rn), rd, i, size);
 
5164
    }
 
5165
    if (!is_q) {
 
5166
        clear_vec_high(s, rd);
 
5167
    }
 
5168
}
 
5169
 
 
5170
/* C6.3.150 INS (Element)
 
5171
 *
 
5172
 *  31                   21 20    16 15  14    11  10 9    5 4    0
 
5173
 * +-----------------------+--------+------------+---+------+------+
 
5174
 * | 0 1 1 0 1 1 1 0 0 0 0 |  imm5  | 0 |  imm4  | 1 |  Rn  |  Rd  |
 
5175
 * +-----------------------+--------+------------+---+------+------+
 
5176
 *
 
5177
 * size: encoded in imm5 (see ARM ARM LowestSetBit())
 
5178
 * index: encoded in imm5<4:size+1>
 
5179
 */
 
5180
static void handle_simd_inse(DisasContext *s, int rd, int rn,
 
5181
                             int imm4, int imm5)
 
5182
{
 
5183
    int size = ctz32(imm5);
 
5184
    int src_index, dst_index;
 
5185
    TCGv_i64 tmp;
 
5186
 
 
5187
    if (size > 3) {
 
5188
        unallocated_encoding(s);
 
5189
        return;
 
5190
    }
 
5191
    dst_index = extract32(imm5, 1+size, 5);
 
5192
    src_index = extract32(imm4, size, 4);
 
5193
 
 
5194
    tmp = tcg_temp_new_i64();
 
5195
 
 
5196
    read_vec_element(s, tmp, rn, src_index, size);
 
5197
    write_vec_element(s, tmp, rd, dst_index, size);
 
5198
 
 
5199
    tcg_temp_free_i64(tmp);
 
5200
}
 
5201
 
 
5202
 
 
5203
/* C6.3.151 INS (General)
 
5204
 *
 
5205
 *  31                   21 20    16 15        10  9    5 4    0
 
5206
 * +-----------------------+--------+-------------+------+------+
 
5207
 * | 0 1 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 0 1 1 1 |  Rn  |  Rd  |
 
5208
 * +-----------------------+--------+-------------+------+------+
 
5209
 *
 
5210
 * size: encoded in imm5 (see ARM ARM LowestSetBit())
 
5211
 * index: encoded in imm5<4:size+1>
 
5212
 */
 
5213
static void handle_simd_insg(DisasContext *s, int rd, int rn, int imm5)
 
5214
{
 
5215
    int size = ctz32(imm5);
 
5216
    int idx;
 
5217
 
 
5218
    if (size > 3) {
 
5219
        unallocated_encoding(s);
 
5220
        return;
 
5221
    }
 
5222
 
 
5223
    idx = extract32(imm5, 1 + size, 4 - size);
 
5224
    write_vec_element(s, cpu_reg(s, rn), rd, idx, size);
 
5225
}
 
5226
 
 
5227
/*
 
5228
 * C6.3.321 UMOV (General)
 
5229
 * C6.3.237 SMOV (General)
 
5230
 *
 
5231
 *  31  30   29              21 20    16 15    12   10 9    5 4    0
 
5232
 * +---+---+-------------------+--------+-------------+------+------+
 
5233
 * | 0 | Q | 0 0 1 1 1 0 0 0 0 |  imm5  | 0 0 1 U 1 1 |  Rn  |  Rd  |
 
5234
 * +---+---+-------------------+--------+-------------+------+------+
 
5235
 *
 
5236
 * U: unsigned when set
 
5237
 * size: encoded in imm5 (see ARM ARM LowestSetBit())
 
5238
 */
 
5239
static void handle_simd_umov_smov(DisasContext *s, int is_q, int is_signed,
 
5240
                                  int rn, int rd, int imm5)
 
5241
{
 
5242
    int size = ctz32(imm5);
 
5243
    int element;
 
5244
    TCGv_i64 tcg_rd;
 
5245
 
 
5246
    /* Check for UnallocatedEncodings */
 
5247
    if (is_signed) {
 
5248
        if (size > 2 || (size == 2 && !is_q)) {
 
5249
            unallocated_encoding(s);
 
5250
            return;
 
5251
        }
 
5252
    } else {
 
5253
        if (size > 3
 
5254
            || (size < 3 && is_q)
 
5255
            || (size == 3 && !is_q)) {
 
5256
            unallocated_encoding(s);
 
5257
            return;
 
5258
        }
 
5259
    }
 
5260
    element = extract32(imm5, 1+size, 4);
 
5261
 
 
5262
    tcg_rd = cpu_reg(s, rd);
 
5263
    read_vec_element(s, tcg_rd, rn, element, size | (is_signed ? MO_SIGN : 0));
 
5264
    if (is_signed && !is_q) {
 
5265
        tcg_gen_ext32u_i64(tcg_rd, tcg_rd);
 
5266
    }
 
5267
}
 
5268
 
 
5269
/* C3.6.5 AdvSIMD copy
 
5270
 *   31  30  29  28             21 20  16 15  14  11 10  9    5 4    0
 
5271
 * +---+---+----+-----------------+------+---+------+---+------+------+
 
5272
 * | 0 | Q | op | 0 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 |  Rn  |  Rd  |
 
5273
 * +---+---+----+-----------------+------+---+------+---+------+------+
 
5274
 */
 
5275
static void disas_simd_copy(DisasContext *s, uint32_t insn)
 
5276
{
 
5277
    int rd = extract32(insn, 0, 5);
 
5278
    int rn = extract32(insn, 5, 5);
 
5279
    int imm4 = extract32(insn, 11, 4);
 
5280
    int op = extract32(insn, 29, 1);
 
5281
    int is_q = extract32(insn, 30, 1);
 
5282
    int imm5 = extract32(insn, 16, 5);
 
5283
 
 
5284
    if (op) {
 
5285
        if (is_q) {
 
5286
            /* INS (element) */
 
5287
            handle_simd_inse(s, rd, rn, imm4, imm5);
 
5288
        } else {
 
5289
            unallocated_encoding(s);
 
5290
        }
 
5291
    } else {
 
5292
        switch (imm4) {
 
5293
        case 0:
 
5294
            /* DUP (element - vector) */
 
5295
            handle_simd_dupe(s, is_q, rd, rn, imm5);
 
5296
            break;
 
5297
        case 1:
 
5298
            /* DUP (general) */
 
5299
            handle_simd_dupg(s, is_q, rd, rn, imm5);
 
5300
            break;
 
5301
        case 3:
 
5302
            if (is_q) {
 
5303
                /* INS (general) */
 
5304
                handle_simd_insg(s, rd, rn, imm5);
 
5305
            } else {
 
5306
                unallocated_encoding(s);
 
5307
            }
 
5308
            break;
 
5309
        case 5:
 
5310
        case 7:
 
5311
            /* UMOV/SMOV (is_q indicates 32/64; imm4 indicates signedness) */
 
5312
            handle_simd_umov_smov(s, is_q, (imm4 == 5), rn, rd, imm5);
 
5313
            break;
 
5314
        default:
 
5315
            unallocated_encoding(s);
 
5316
            break;
 
5317
        }
 
5318
    }
 
5319
}
 
5320
 
 
5321
/* C3.6.6 AdvSIMD modified immediate
 
5322
 *  31  30   29  28                 19 18 16 15   12  11  10  9     5 4    0
 
5323
 * +---+---+----+---------------------+-----+-------+----+---+-------+------+
 
5324
 * | 0 | Q | op | 0 1 1 1 1 0 0 0 0 0 | abc | cmode | o2 | 1 | defgh |  Rd  |
 
5325
 * +---+---+----+---------------------+-----+-------+----+---+-------+------+
 
5326
 *
 
5327
 * There are a number of operations that can be carried out here:
 
5328
 *   MOVI - move (shifted) imm into register
 
5329
 *   MVNI - move inverted (shifted) imm into register
 
5330
 *   ORR  - bitwise OR of (shifted) imm with register
 
5331
 *   BIC  - bitwise clear of (shifted) imm with register
 
5332
 */
 
5333
static void disas_simd_mod_imm(DisasContext *s, uint32_t insn)
 
5334
{
 
5335
    int rd = extract32(insn, 0, 5);
 
5336
    int cmode = extract32(insn, 12, 4);
 
5337
    int cmode_3_1 = extract32(cmode, 1, 3);
 
5338
    int cmode_0 = extract32(cmode, 0, 1);
 
5339
    int o2 = extract32(insn, 11, 1);
 
5340
    uint64_t abcdefgh = extract32(insn, 5, 5) | (extract32(insn, 16, 3) << 5);
 
5341
    bool is_neg = extract32(insn, 29, 1);
 
5342
    bool is_q = extract32(insn, 30, 1);
 
5343
    uint64_t imm = 0;
 
5344
    TCGv_i64 tcg_rd, tcg_imm;
 
5345
    int i;
 
5346
 
 
5347
    if (o2 != 0 || ((cmode == 0xf) && is_neg && !is_q)) {
 
5348
        unallocated_encoding(s);
 
5349
        return;
 
5350
    }
 
5351
 
 
5352
    /* See AdvSIMDExpandImm() in ARM ARM */
 
5353
    switch (cmode_3_1) {
 
5354
    case 0: /* Replicate(Zeros(24):imm8, 2) */
 
5355
    case 1: /* Replicate(Zeros(16):imm8:Zeros(8), 2) */
 
5356
    case 2: /* Replicate(Zeros(8):imm8:Zeros(16), 2) */
 
5357
    case 3: /* Replicate(imm8:Zeros(24), 2) */
 
5358
    {
 
5359
        int shift = cmode_3_1 * 8;
 
5360
        imm = bitfield_replicate(abcdefgh << shift, 32);
 
5361
        break;
 
5362
    }
 
5363
    case 4: /* Replicate(Zeros(8):imm8, 4) */
 
5364
    case 5: /* Replicate(imm8:Zeros(8), 4) */
 
5365
    {
 
5366
        int shift = (cmode_3_1 & 0x1) * 8;
 
5367
        imm = bitfield_replicate(abcdefgh << shift, 16);
 
5368
        break;
 
5369
    }
 
5370
    case 6:
 
5371
        if (cmode_0) {
 
5372
            /* Replicate(Zeros(8):imm8:Ones(16), 2) */
 
5373
            imm = (abcdefgh << 16) | 0xffff;
 
5374
        } else {
 
5375
            /* Replicate(Zeros(16):imm8:Ones(8), 2) */
 
5376
            imm = (abcdefgh << 8) | 0xff;
 
5377
        }
 
5378
        imm = bitfield_replicate(imm, 32);
 
5379
        break;
 
5380
    case 7:
 
5381
        if (!cmode_0 && !is_neg) {
 
5382
            imm = bitfield_replicate(abcdefgh, 8);
 
5383
        } else if (!cmode_0 && is_neg) {
 
5384
            int i;
 
5385
            imm = 0;
 
5386
            for (i = 0; i < 8; i++) {
 
5387
                if ((abcdefgh) & (1 << i)) {
 
5388
                    imm |= 0xffULL << (i * 8);
 
5389
                }
 
5390
            }
 
5391
        } else if (cmode_0) {
 
5392
            if (is_neg) {
 
5393
                imm = (abcdefgh & 0x3f) << 48;
 
5394
                if (abcdefgh & 0x80) {
 
5395
                    imm |= 0x8000000000000000ULL;
 
5396
                }
 
5397
                if (abcdefgh & 0x40) {
 
5398
                    imm |= 0x3fc0000000000000ULL;
 
5399
                } else {
 
5400
                    imm |= 0x4000000000000000ULL;
 
5401
                }
 
5402
            } else {
 
5403
                imm = (abcdefgh & 0x3f) << 19;
 
5404
                if (abcdefgh & 0x80) {
 
5405
                    imm |= 0x80000000;
 
5406
                }
 
5407
                if (abcdefgh & 0x40) {
 
5408
                    imm |= 0x3e000000;
 
5409
                } else {
 
5410
                    imm |= 0x40000000;
 
5411
                }
 
5412
                imm |= (imm << 32);
 
5413
            }
 
5414
        }
 
5415
        break;
 
5416
    }
 
5417
 
 
5418
    if (cmode_3_1 != 7 && is_neg) {
 
5419
        imm = ~imm;
 
5420
    }
 
5421
 
 
5422
    tcg_imm = tcg_const_i64(imm);
 
5423
    tcg_rd = new_tmp_a64(s);
 
5424
 
 
5425
    for (i = 0; i < 2; i++) {
 
5426
        int foffs = i ? fp_reg_hi_offset(rd) : fp_reg_offset(rd, MO_64);
 
5427
 
 
5428
        if (i == 1 && !is_q) {
 
5429
            /* non-quad ops clear high half of vector */
 
5430
            tcg_gen_movi_i64(tcg_rd, 0);
 
5431
        } else if ((cmode & 0x9) == 0x1 || (cmode & 0xd) == 0x9) {
 
5432
            tcg_gen_ld_i64(tcg_rd, cpu_env, foffs);
 
5433
            if (is_neg) {
 
5434
                /* AND (BIC) */
 
5435
                tcg_gen_and_i64(tcg_rd, tcg_rd, tcg_imm);
 
5436
            } else {
 
5437
                /* ORR */
 
5438
                tcg_gen_or_i64(tcg_rd, tcg_rd, tcg_imm);
 
5439
            }
 
5440
        } else {
 
5441
            /* MOVI */
 
5442
            tcg_gen_mov_i64(tcg_rd, tcg_imm);
 
5443
        }
 
5444
        tcg_gen_st_i64(tcg_rd, cpu_env, foffs);
 
5445
    }
 
5446
 
 
5447
    tcg_temp_free_i64(tcg_imm);
 
5448
}
 
5449
 
 
5450
/* C3.6.7 AdvSIMD scalar copy
 
5451
 *  31 30  29  28             21 20  16 15  14  11 10  9    5 4    0
 
5452
 * +-----+----+-----------------+------+---+------+---+------+------+
 
5453
 * | 0 1 | op | 1 1 1 1 0 0 0 0 | imm5 | 0 | imm4 | 1 |  Rn  |  Rd  |
 
5454
 * +-----+----+-----------------+------+---+------+---+------+------+
 
5455
 */
 
5456
static void disas_simd_scalar_copy(DisasContext *s, uint32_t insn)
 
5457
{
 
5458
    int rd = extract32(insn, 0, 5);
 
5459
    int rn = extract32(insn, 5, 5);
 
5460
    int imm4 = extract32(insn, 11, 4);
 
5461
    int imm5 = extract32(insn, 16, 5);
 
5462
    int op = extract32(insn, 29, 1);
 
5463
 
 
5464
    if (op != 0 || imm4 != 0) {
 
5465
        unallocated_encoding(s);
 
5466
        return;
 
5467
    }
 
5468
 
 
5469
    /* DUP (element, scalar) */
 
5470
    handle_simd_dupes(s, rd, rn, imm5);
 
5471
}
 
5472
 
 
5473
/* C3.6.8 AdvSIMD scalar pairwise
 
5474
 *  31 30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
 
5475
 * +-----+---+-----------+------+-----------+--------+-----+------+------+
 
5476
 * | 0 1 | U | 1 1 1 1 0 | size | 1 1 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
 
5477
 * +-----+---+-----------+------+-----------+--------+-----+------+------+
 
5478
 */
 
5479
static void disas_simd_scalar_pairwise(DisasContext *s, uint32_t insn)
 
5480
{
 
5481
    unsupported_encoding(s, insn);
 
5482
}
 
5483
 
 
5484
/* C3.6.9 AdvSIMD scalar shift by immediate
 
5485
 *  31 30  29 28         23 22  19 18  16 15    11  10 9    5 4    0
 
5486
 * +-----+---+-------------+------+------+--------+---+------+------+
 
5487
 * | 0 1 | U | 1 1 1 1 1 0 | immh | immb | opcode | 1 |  Rn  |  Rd  |
 
5488
 * +-----+---+-------------+------+------+--------+---+------+------+
 
5489
 */
 
5490
static void disas_simd_scalar_shift_imm(DisasContext *s, uint32_t insn)
 
5491
{
 
5492
    unsupported_encoding(s, insn);
 
5493
}
 
5494
 
 
5495
/* C3.6.10 AdvSIMD scalar three different
 
5496
 *  31 30  29 28       24 23  22  21 20  16 15    12 11 10 9    5 4    0
 
5497
 * +-----+---+-----------+------+---+------+--------+-----+------+------+
 
5498
 * | 0 1 | U | 1 1 1 1 0 | size | 1 |  Rm  | opcode | 0 0 |  Rn  |  Rd  |
 
5499
 * +-----+---+-----------+------+---+------+--------+-----+------+------+
 
5500
 */
 
5501
static void disas_simd_scalar_three_reg_diff(DisasContext *s, uint32_t insn)
 
5502
{
 
5503
    unsupported_encoding(s, insn);
 
5504
}
 
5505
 
 
5506
/* C3.6.11 AdvSIMD scalar three same
 
5507
 *  31 30  29 28       24 23  22  21 20  16 15    11  10 9    5 4    0
 
5508
 * +-----+---+-----------+------+---+------+--------+---+------+------+
 
5509
 * | 0 1 | U | 1 1 1 1 0 | size | 1 |  Rm  | opcode | 1 |  Rn  |  Rd  |
 
5510
 * +-----+---+-----------+------+---+------+--------+---+------+------+
 
5511
 */
 
5512
static void disas_simd_scalar_three_reg_same(DisasContext *s, uint32_t insn)
 
5513
{
 
5514
    unsupported_encoding(s, insn);
 
5515
}
 
5516
 
 
5517
/* C3.6.12 AdvSIMD scalar two reg misc
 
5518
 *  31 30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
 
5519
 * +-----+---+-----------+------+-----------+--------+-----+------+------+
 
5520
 * | 0 1 | U | 1 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
 
5521
 * +-----+---+-----------+------+-----------+--------+-----+------+------+
 
5522
 */
 
5523
static void disas_simd_scalar_two_reg_misc(DisasContext *s, uint32_t insn)
 
5524
{
 
5525
    unsupported_encoding(s, insn);
 
5526
}
 
5527
 
 
5528
/* C3.6.13 AdvSIMD scalar x indexed element
 
5529
 *  31 30  29 28       24 23  22 21  20  19  16 15 12  11  10 9    5 4    0
 
5530
 * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
 
5531
 * | 0 1 | U | 1 1 1 1 1 | size | L | M |  Rm  | opc | H | 0 |  Rn  |  Rd  |
 
5532
 * +-----+---+-----------+------+---+---+------+-----+---+---+------+------+
 
5533
 */
 
5534
static void disas_simd_scalar_indexed(DisasContext *s, uint32_t insn)
 
5535
{
 
5536
    unsupported_encoding(s, insn);
 
5537
}
 
5538
 
 
5539
/* C3.6.14 AdvSIMD shift by immediate
 
5540
 *  31  30   29 28         23 22  19 18  16 15    11  10 9    5 4    0
 
5541
 * +---+---+---+-------------+------+------+--------+---+------+------+
 
5542
 * | 0 | Q | U | 0 1 1 1 1 0 | immh | immb | opcode | 1 |  Rn  |  Rd  |
 
5543
 * +---+---+---+-------------+------+------+--------+---+------+------+
 
5544
 */
 
5545
static void disas_simd_shift_imm(DisasContext *s, uint32_t insn)
 
5546
{
 
5547
    unsupported_encoding(s, insn);
 
5548
}
 
5549
 
 
5550
static void handle_3rd_widening(DisasContext *s, int is_q, int is_u, int size,
 
5551
                                int opcode, int rd, int rn, int rm)
 
5552
{
 
5553
    /* 3-reg-different widening insns: 64 x 64 -> 128 */
 
5554
    TCGv_i64 tcg_res[2];
 
5555
    int pass, accop;
 
5556
 
 
5557
    tcg_res[0] = tcg_temp_new_i64();
 
5558
    tcg_res[1] = tcg_temp_new_i64();
 
5559
 
 
5560
    /* Does this op do an adding accumulate, a subtracting accumulate,
 
5561
     * or no accumulate at all?
 
5562
     */
 
5563
    switch (opcode) {
 
5564
    case 5:
 
5565
    case 8:
 
5566
    case 9:
 
5567
        accop = 1;
 
5568
        break;
 
5569
    case 10:
 
5570
    case 11:
 
5571
        accop = -1;
 
5572
        break;
 
5573
    default:
 
5574
        accop = 0;
 
5575
        break;
 
5576
    }
 
5577
 
 
5578
    if (accop != 0) {
 
5579
        read_vec_element(s, tcg_res[0], rd, 0, MO_64);
 
5580
        read_vec_element(s, tcg_res[1], rd, 1, MO_64);
 
5581
    }
 
5582
 
 
5583
    /* size == 2 means two 32x32->64 operations; this is worth special
 
5584
     * casing because we can generally handle it inline.
 
5585
     */
 
5586
    if (size == 2) {
 
5587
        for (pass = 0; pass < 2; pass++) {
 
5588
            TCGv_i64 tcg_op1 = tcg_temp_new_i64();
 
5589
            TCGv_i64 tcg_op2 = tcg_temp_new_i64();
 
5590
            TCGv_i64 tcg_passres;
 
5591
            TCGMemOp memop = MO_32 | (is_u ? 0 : MO_SIGN);
 
5592
 
 
5593
            int elt = pass + is_q * 2;
 
5594
 
 
5595
            read_vec_element(s, tcg_op1, rn, elt, memop);
 
5596
            read_vec_element(s, tcg_op2, rm, elt, memop);
 
5597
 
 
5598
            if (accop == 0) {
 
5599
                tcg_passres = tcg_res[pass];
 
5600
            } else {
 
5601
                tcg_passres = tcg_temp_new_i64();
 
5602
            }
 
5603
 
 
5604
            switch (opcode) {
 
5605
            case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
 
5606
            case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
 
5607
            {
 
5608
                TCGv_i64 tcg_tmp1 = tcg_temp_new_i64();
 
5609
                TCGv_i64 tcg_tmp2 = tcg_temp_new_i64();
 
5610
 
 
5611
                tcg_gen_sub_i64(tcg_tmp1, tcg_op1, tcg_op2);
 
5612
                tcg_gen_sub_i64(tcg_tmp2, tcg_op2, tcg_op1);
 
5613
                tcg_gen_movcond_i64(is_u ? TCG_COND_GEU : TCG_COND_GE,
 
5614
                                    tcg_passres,
 
5615
                                    tcg_op1, tcg_op2, tcg_tmp1, tcg_tmp2);
 
5616
                tcg_temp_free_i64(tcg_tmp1);
 
5617
                tcg_temp_free_i64(tcg_tmp2);
 
5618
                break;
 
5619
            }
 
5620
            case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
 
5621
            case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
 
5622
            case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
 
5623
                tcg_gen_mul_i64(tcg_passres, tcg_op1, tcg_op2);
 
5624
                break;
 
5625
            default:
 
5626
                g_assert_not_reached();
 
5627
            }
 
5628
 
 
5629
            if (accop > 0) {
 
5630
                tcg_gen_add_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
 
5631
                tcg_temp_free_i64(tcg_passres);
 
5632
            } else if (accop < 0) {
 
5633
                tcg_gen_sub_i64(tcg_res[pass], tcg_res[pass], tcg_passres);
 
5634
                tcg_temp_free_i64(tcg_passres);
 
5635
            }
 
5636
 
 
5637
            tcg_temp_free_i64(tcg_op1);
 
5638
            tcg_temp_free_i64(tcg_op2);
 
5639
        }
 
5640
    } else {
 
5641
        /* size 0 or 1, generally helper functions */
 
5642
        for (pass = 0; pass < 2; pass++) {
 
5643
            TCGv_i32 tcg_op1 = tcg_temp_new_i32();
 
5644
            TCGv_i32 tcg_op2 = tcg_temp_new_i32();
 
5645
            TCGv_i64 tcg_passres;
 
5646
            int elt = pass + is_q * 2;
 
5647
 
 
5648
            read_vec_element_i32(s, tcg_op1, rn, elt, MO_32);
 
5649
            read_vec_element_i32(s, tcg_op2, rm, elt, MO_32);
 
5650
 
 
5651
            if (accop == 0) {
 
5652
                tcg_passres = tcg_res[pass];
 
5653
            } else {
 
5654
                tcg_passres = tcg_temp_new_i64();
 
5655
            }
 
5656
 
 
5657
            switch (opcode) {
 
5658
            case 5: /* SABAL, SABAL2, UABAL, UABAL2 */
 
5659
            case 7: /* SABDL, SABDL2, UABDL, UABDL2 */
 
5660
                if (size == 0) {
 
5661
                    if (is_u) {
 
5662
                        gen_helper_neon_abdl_u16(tcg_passres, tcg_op1, tcg_op2);
 
5663
                    } else {
 
5664
                        gen_helper_neon_abdl_s16(tcg_passres, tcg_op1, tcg_op2);
 
5665
                    }
 
5666
                } else {
 
5667
                    if (is_u) {
 
5668
                        gen_helper_neon_abdl_u32(tcg_passres, tcg_op1, tcg_op2);
 
5669
                    } else {
 
5670
                        gen_helper_neon_abdl_s32(tcg_passres, tcg_op1, tcg_op2);
 
5671
                    }
 
5672
                }
 
5673
                break;
 
5674
            case 8: /* SMLAL, SMLAL2, UMLAL, UMLAL2 */
 
5675
            case 10: /* SMLSL, SMLSL2, UMLSL, UMLSL2 */
 
5676
            case 12: /* UMULL, UMULL2, SMULL, SMULL2 */
 
5677
                if (size == 0) {
 
5678
                    if (is_u) {
 
5679
                        gen_helper_neon_mull_u8(tcg_passres, tcg_op1, tcg_op2);
 
5680
                    } else {
 
5681
                        gen_helper_neon_mull_s8(tcg_passres, tcg_op1, tcg_op2);
 
5682
                    }
 
5683
                } else {
 
5684
                    if (is_u) {
 
5685
                        gen_helper_neon_mull_u16(tcg_passres, tcg_op1, tcg_op2);
 
5686
                    } else {
 
5687
                        gen_helper_neon_mull_s16(tcg_passres, tcg_op1, tcg_op2);
 
5688
                    }
 
5689
                }
 
5690
                break;
 
5691
            default:
 
5692
                g_assert_not_reached();
 
5693
            }
 
5694
            tcg_temp_free_i32(tcg_op1);
 
5695
            tcg_temp_free_i32(tcg_op2);
 
5696
 
 
5697
            if (accop > 0) {
 
5698
                if (size == 0) {
 
5699
                    gen_helper_neon_addl_u16(tcg_res[pass], tcg_res[pass],
 
5700
                                             tcg_passres);
 
5701
                } else {
 
5702
                    gen_helper_neon_addl_u32(tcg_res[pass], tcg_res[pass],
 
5703
                                             tcg_passres);
 
5704
                }
 
5705
                tcg_temp_free_i64(tcg_passres);
 
5706
            } else if (accop < 0) {
 
5707
                if (size == 0) {
 
5708
                    gen_helper_neon_subl_u16(tcg_res[pass], tcg_res[pass],
 
5709
                                             tcg_passres);
 
5710
                } else {
 
5711
                    gen_helper_neon_subl_u32(tcg_res[pass], tcg_res[pass],
 
5712
                                             tcg_passres);
 
5713
                }
 
5714
                tcg_temp_free_i64(tcg_passres);
 
5715
            }
 
5716
        }
 
5717
    }
 
5718
 
 
5719
    write_vec_element(s, tcg_res[0], rd, 0, MO_64);
 
5720
    write_vec_element(s, tcg_res[1], rd, 1, MO_64);
 
5721
    tcg_temp_free_i64(tcg_res[0]);
 
5722
    tcg_temp_free_i64(tcg_res[1]);
 
5723
}
 
5724
 
 
5725
/* C3.6.15 AdvSIMD three different
 
5726
 *   31  30  29 28       24 23  22  21 20  16 15    12 11 10 9    5 4    0
 
5727
 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
 
5728
 * | 0 | Q | U | 0 1 1 1 0 | size | 1 |  Rm  | opcode | 0 0 |  Rn  |  Rd  |
 
5729
 * +---+---+---+-----------+------+---+------+--------+-----+------+------+
 
5730
 */
 
5731
static void disas_simd_three_reg_diff(DisasContext *s, uint32_t insn)
 
5732
{
 
5733
    /* Instructions in this group fall into three basic classes
 
5734
     * (in each case with the operation working on each element in
 
5735
     * the input vectors):
 
5736
     * (1) widening 64 x 64 -> 128 (with possibly Vd as an extra
 
5737
     *     128 bit input)
 
5738
     * (2) wide 64 x 128 -> 128
 
5739
     * (3) narrowing 128 x 128 -> 64
 
5740
     * Here we do initial decode, catch unallocated cases and
 
5741
     * dispatch to separate functions for each class.
 
5742
     */
 
5743
    int is_q = extract32(insn, 30, 1);
 
5744
    int is_u = extract32(insn, 29, 1);
 
5745
    int size = extract32(insn, 22, 2);
 
5746
    int opcode = extract32(insn, 12, 4);
 
5747
    int rm = extract32(insn, 16, 5);
 
5748
    int rn = extract32(insn, 5, 5);
 
5749
    int rd = extract32(insn, 0, 5);
 
5750
 
 
5751
    switch (opcode) {
 
5752
    case 1: /* SADDW, SADDW2, UADDW, UADDW2 */
 
5753
    case 3: /* SSUBW, SSUBW2, USUBW, USUBW2 */
 
5754
        /* 64 x 128 -> 128 */
 
5755
        unsupported_encoding(s, insn);
 
5756
        break;
 
5757
    case 4: /* ADDHN, ADDHN2, RADDHN, RADDHN2 */
 
5758
    case 6: /* SUBHN, SUBHN2, RSUBHN, RSUBHN2 */
 
5759
        /* 128 x 128 -> 64 */
 
5760
        unsupported_encoding(s, insn);
 
5761
        break;
 
5762
    case 9:
 
5763
    case 11:
 
5764
    case 13:
 
5765
    case 14:
 
5766
        if (is_u) {
 
5767
            unallocated_encoding(s);
 
5768
            return;
 
5769
        }
 
5770
        /* fall through */
 
5771
    case 0:
 
5772
    case 2:
 
5773
        unsupported_encoding(s, insn);
 
5774
        break;
 
5775
    case 5:
 
5776
    case 7:
 
5777
    case 8:
 
5778
    case 10:
 
5779
    case 12:
 
5780
        /* 64 x 64 -> 128 */
 
5781
        if (size == 3) {
 
5782
            unallocated_encoding(s);
 
5783
            return;
 
5784
        }
 
5785
        handle_3rd_widening(s, is_q, is_u, size, opcode, rd, rn, rm);
 
5786
        break;
 
5787
    default:
 
5788
        /* opcode 15 not allocated */
 
5789
        unallocated_encoding(s);
 
5790
        break;
 
5791
    }
 
5792
}
 
5793
 
 
5794
/* C3.6.16 AdvSIMD three same
 
5795
 *  31  30  29  28       24 23  22  21 20  16 15    11  10 9    5 4    0
 
5796
 * +---+---+---+-----------+------+---+------+--------+---+------+------+
 
5797
 * | 0 | Q | U | 0 1 1 1 0 | size | 1 |  Rm  | opcode | 1 |  Rn  |  Rd  |
 
5798
 * +---+---+---+-----------+------+---+------+--------+---+------+------+
 
5799
 */
 
5800
static void disas_simd_three_reg_same(DisasContext *s, uint32_t insn)
 
5801
{
 
5802
    unsupported_encoding(s, insn);
 
5803
}
 
5804
 
 
5805
/* C3.6.17 AdvSIMD two reg misc
 
5806
 *   31  30  29 28       24 23  22 21       17 16    12 11 10 9    5 4    0
 
5807
 * +---+---+---+-----------+------+-----------+--------+-----+------+------+
 
5808
 * | 0 | Q | U | 0 1 1 1 0 | size | 1 0 0 0 0 | opcode | 1 0 |  Rn  |  Rd  |
 
5809
 * +---+---+---+-----------+------+-----------+--------+-----+------+------+
 
5810
 */
 
5811
static void disas_simd_two_reg_misc(DisasContext *s, uint32_t insn)
 
5812
{
 
5813
    unsupported_encoding(s, insn);
 
5814
}
 
5815
 
 
5816
/* C3.6.18 AdvSIMD vector x indexed element
 
5817
 *   31  30  29 28       24 23  22 21  20  19  16 15 12  11  10 9    5 4    0
 
5818
 * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
 
5819
 * | 0 | Q | U | 0 1 1 1 1 | size | L | M |  Rm  | opc | H | 0 |  Rn  |  Rd  |
 
5820
 * +---+---+---+-----------+------+---+---+------+-----+---+---+------+------+
 
5821
 */
 
5822
static void disas_simd_indexed_vector(DisasContext *s, uint32_t insn)
 
5823
{
 
5824
    unsupported_encoding(s, insn);
 
5825
}
 
5826
 
 
5827
/* C3.6.19 Crypto AES
 
5828
 *  31             24 23  22 21       17 16    12 11 10 9    5 4    0
 
5829
 * +-----------------+------+-----------+--------+-----+------+------+
 
5830
 * | 0 1 0 0 1 1 1 0 | size | 1 0 1 0 0 | opcode | 1 0 |  Rn  |  Rd  |
 
5831
 * +-----------------+------+-----------+--------+-----+------+------+
 
5832
 */
 
5833
static void disas_crypto_aes(DisasContext *s, uint32_t insn)
 
5834
{
 
5835
    unsupported_encoding(s, insn);
 
5836
}
 
5837
 
 
5838
/* C3.6.20 Crypto three-reg SHA
 
5839
 *  31             24 23  22  21 20  16  15 14    12 11 10 9    5 4    0
 
5840
 * +-----------------+------+---+------+---+--------+-----+------+------+
 
5841
 * | 0 1 0 1 1 1 1 0 | size | 0 |  Rm  | 0 | opcode | 0 0 |  Rn  |  Rd  |
 
5842
 * +-----------------+------+---+------+---+--------+-----+------+------+
 
5843
 */
 
5844
static void disas_crypto_three_reg_sha(DisasContext *s, uint32_t insn)
 
5845
{
 
5846
    unsupported_encoding(s, insn);
 
5847
}
 
5848
 
 
5849
/* C3.6.21 Crypto two-reg SHA
 
5850
 *  31             24 23  22 21       17 16    12 11 10 9    5 4    0
 
5851
 * +-----------------+------+-----------+--------+-----+------+------+
 
5852
 * | 0 1 0 1 1 1 1 0 | size | 1 0 1 0 0 | opcode | 1 0 |  Rn  |  Rd  |
 
5853
 * +-----------------+------+-----------+--------+-----+------+------+
 
5854
 */
 
5855
static void disas_crypto_two_reg_sha(DisasContext *s, uint32_t insn)
 
5856
{
 
5857
    unsupported_encoding(s, insn);
 
5858
}
 
5859
 
 
5860
/* C3.6 Data processing - SIMD, inc Crypto
 
5861
 *
 
5862
 * As the decode gets a little complex we are using a table based
 
5863
 * approach for this part of the decode.
 
5864
 */
 
5865
static const AArch64DecodeTable data_proc_simd[] = {
 
5866
    /* pattern  ,  mask     ,  fn                        */
 
5867
    { 0x0e200400, 0x9f200400, disas_simd_three_reg_same },
 
5868
    { 0x0e200000, 0x9f200c00, disas_simd_three_reg_diff },
 
5869
    { 0x0e200800, 0x9f3e0c00, disas_simd_two_reg_misc },
 
5870
    { 0x0e300800, 0x9f3e0c00, disas_simd_across_lanes },
 
5871
    { 0x0e000400, 0x9fe08400, disas_simd_copy },
 
5872
    { 0x0f000000, 0x9f000400, disas_simd_indexed_vector },
 
5873
    /* simd_mod_imm decode is a subset of simd_shift_imm, so must precede it */
 
5874
    { 0x0f000400, 0x9ff80400, disas_simd_mod_imm },
 
5875
    { 0x0f000400, 0x9f800400, disas_simd_shift_imm },
 
5876
    { 0x0e000000, 0xbf208c00, disas_simd_tb },
 
5877
    { 0x0e000800, 0xbf208c00, disas_simd_zip_trn },
 
5878
    { 0x2e000000, 0xbf208400, disas_simd_ext },
 
5879
    { 0x5e200400, 0xdf200400, disas_simd_scalar_three_reg_same },
 
5880
    { 0x5e200000, 0xdf200c00, disas_simd_scalar_three_reg_diff },
 
5881
    { 0x5e200800, 0xdf3e0c00, disas_simd_scalar_two_reg_misc },
 
5882
    { 0x5e300800, 0xdf3e0c00, disas_simd_scalar_pairwise },
 
5883
    { 0x5e000400, 0xdfe08400, disas_simd_scalar_copy },
 
5884
    { 0x5f000000, 0xdf000400, disas_simd_scalar_indexed },
 
5885
    { 0x5f000400, 0xdf800400, disas_simd_scalar_shift_imm },
 
5886
    { 0x4e280800, 0xff3e0c00, disas_crypto_aes },
 
5887
    { 0x5e000000, 0xff208c00, disas_crypto_three_reg_sha },
 
5888
    { 0x5e280800, 0xff3e0c00, disas_crypto_two_reg_sha },
 
5889
    { 0x00000000, 0x00000000, NULL }
 
5890
};
 
5891
 
 
5892
static void disas_data_proc_simd(DisasContext *s, uint32_t insn)
 
5893
{
 
5894
    /* Note that this is called with all non-FP cases from
 
5895
     * table C3-6 so it must UNDEF for entries not specifically
 
5896
     * allocated to instructions in that table.
 
5897
     */
 
5898
    AArch64DecodeFn *fn = lookup_disas_fn(&data_proc_simd[0], insn);
 
5899
    if (fn) {
 
5900
        fn(s, insn);
 
5901
    } else {
 
5902
        unallocated_encoding(s);
 
5903
    }
 
5904
}
 
5905
 
 
5906
/* C3.6 Data processing - SIMD and floating point */
 
5907
static void disas_data_proc_simd_fp(DisasContext *s, uint32_t insn)
 
5908
{
 
5909
    if (extract32(insn, 28, 1) == 1 && extract32(insn, 30, 1) == 0) {
 
5910
        disas_data_proc_fp(s, insn);
 
5911
    } else {
 
5912
        /* SIMD, including crypto */
 
5913
        disas_data_proc_simd(s, insn);
 
5914
    }
 
5915
}
 
5916
 
 
5917
/* C3.1 A64 instruction index by encoding */
 
5918
static void disas_a64_insn(CPUARMState *env, DisasContext *s)
 
5919
{
 
5920
    uint32_t insn;
 
5921
 
 
5922
    insn = arm_ldl_code(env, s->pc, s->bswap_code);
 
5923
    s->insn = insn;
 
5924
    s->pc += 4;
 
5925
 
 
5926
    switch (extract32(insn, 25, 4)) {
 
5927
    case 0x0: case 0x1: case 0x2: case 0x3: /* UNALLOCATED */
 
5928
        unallocated_encoding(s);
 
5929
        break;
 
5930
    case 0x8: case 0x9: /* Data processing - immediate */
 
5931
        disas_data_proc_imm(s, insn);
 
5932
        break;
 
5933
    case 0xa: case 0xb: /* Branch, exception generation and system insns */
 
5934
        disas_b_exc_sys(s, insn);
 
5935
        break;
 
5936
    case 0x4:
 
5937
    case 0x6:
 
5938
    case 0xc:
 
5939
    case 0xe:      /* Loads and stores */
 
5940
        disas_ldst(s, insn);
 
5941
        break;
 
5942
    case 0x5:
 
5943
    case 0xd:      /* Data processing - register */
 
5944
        disas_data_proc_reg(s, insn);
 
5945
        break;
 
5946
    case 0x7:
 
5947
    case 0xf:      /* Data processing - SIMD and floating point */
 
5948
        disas_data_proc_simd_fp(s, insn);
 
5949
        break;
 
5950
    default:
 
5951
        assert(FALSE); /* all 15 cases should be handled above */
 
5952
        break;
 
5953
    }
 
5954
 
 
5955
    /* if we allocated any temporaries, free them here */
 
5956
    free_tmp_a64(s);
 
5957
}
 
5958
 
 
5959
void gen_intermediate_code_internal_a64(ARMCPU *cpu,
 
5960
                                        TranslationBlock *tb,
 
5961
                                        bool search_pc)
 
5962
{
 
5963
    CPUState *cs = CPU(cpu);
 
5964
    CPUARMState *env = &cpu->env;
 
5965
    DisasContext dc1, *dc = &dc1;
 
5966
    CPUBreakpoint *bp;
 
5967
    uint16_t *gen_opc_end;
 
5968
    int j, lj;
 
5969
    target_ulong pc_start;
 
5970
    target_ulong next_page_start;
 
5971
    int num_insns;
 
5972
    int max_insns;
 
5973
 
 
5974
    pc_start = tb->pc;
 
5975
 
 
5976
    dc->tb = tb;
 
5977
 
 
5978
    gen_opc_end = tcg_ctx.gen_opc_buf + OPC_MAX_SIZE;
 
5979
 
 
5980
    dc->is_jmp = DISAS_NEXT;
 
5981
    dc->pc = pc_start;
 
5982
    dc->singlestep_enabled = cs->singlestep_enabled;
 
5983
    dc->condjmp = 0;
 
5984
 
 
5985
    dc->aarch64 = 1;
 
5986
    dc->thumb = 0;
 
5987
    dc->bswap_code = 0;
 
5988
    dc->condexec_mask = 0;
 
5989
    dc->condexec_cond = 0;
 
5990
#if !defined(CONFIG_USER_ONLY)
 
5991
    dc->user = 0;
 
5992
#endif
 
5993
    dc->vfp_enabled = 0;
 
5994
    dc->vec_len = 0;
 
5995
    dc->vec_stride = 0;
 
5996
    dc->cp_regs = cpu->cp_regs;
 
5997
    dc->current_pl = arm_current_pl(env);
 
5998
 
 
5999
    init_tmp_a64_array(dc);
 
6000
 
 
6001
    next_page_start = (pc_start & TARGET_PAGE_MASK) + TARGET_PAGE_SIZE;
 
6002
    lj = -1;
 
6003
    num_insns = 0;
 
6004
    max_insns = tb->cflags & CF_COUNT_MASK;
 
6005
    if (max_insns == 0) {
 
6006
        max_insns = CF_COUNT_MASK;
 
6007
    }
 
6008
 
 
6009
    gen_tb_start();
 
6010
 
 
6011
    tcg_clear_temp_count();
 
6012
 
 
6013
    do {
 
6014
        if (unlikely(!QTAILQ_EMPTY(&env->breakpoints))) {
 
6015
            QTAILQ_FOREACH(bp, &env->breakpoints, entry) {
 
6016
                if (bp->pc == dc->pc) {
 
6017
                    gen_exception_insn(dc, 0, EXCP_DEBUG);
 
6018
                    /* Advance PC so that clearing the breakpoint will
 
6019
                       invalidate this TB.  */
 
6020
                    dc->pc += 2;
 
6021
                    goto done_generating;
 
6022
                }
 
6023
            }
 
6024
        }
 
6025
 
 
6026
        if (search_pc) {
 
6027
            j = tcg_ctx.gen_opc_ptr - tcg_ctx.gen_opc_buf;
 
6028
            if (lj < j) {
 
6029
                lj++;
 
6030
                while (lj < j) {
 
6031
                    tcg_ctx.gen_opc_instr_start[lj++] = 0;
 
6032
                }
 
6033
            }
 
6034
            tcg_ctx.gen_opc_pc[lj] = dc->pc;
 
6035
            tcg_ctx.gen_opc_instr_start[lj] = 1;
 
6036
            tcg_ctx.gen_opc_icount[lj] = num_insns;
 
6037
        }
 
6038
 
 
6039
        if (num_insns + 1 == max_insns && (tb->cflags & CF_LAST_IO)) {
 
6040
            gen_io_start();
 
6041
        }
 
6042
 
 
6043
        if (unlikely(qemu_loglevel_mask(CPU_LOG_TB_OP | CPU_LOG_TB_OP_OPT))) {
 
6044
            tcg_gen_debug_insn_start(dc->pc);
 
6045
        }
 
6046
 
 
6047
        disas_a64_insn(env, dc);
 
6048
 
 
6049
        if (tcg_check_temp_count()) {
 
6050
            fprintf(stderr, "TCG temporary leak before "TARGET_FMT_lx"\n",
 
6051
                    dc->pc);
 
6052
        }
 
6053
 
 
6054
        /* Translation stops when a conditional branch is encountered.
 
6055
         * Otherwise the subsequent code could get translated several times.
 
6056
         * Also stop translation when a page boundary is reached.  This
 
6057
         * ensures prefetch aborts occur at the right place.
 
6058
         */
 
6059
        num_insns++;
 
6060
    } while (!dc->is_jmp && tcg_ctx.gen_opc_ptr < gen_opc_end &&
 
6061
             !cs->singlestep_enabled &&
 
6062
             !singlestep &&
 
6063
             dc->pc < next_page_start &&
 
6064
             num_insns < max_insns);
 
6065
 
 
6066
    if (tb->cflags & CF_LAST_IO) {
 
6067
        gen_io_end();
 
6068
    }
 
6069
 
 
6070
    if (unlikely(cs->singlestep_enabled) && dc->is_jmp != DISAS_EXC) {
 
6071
        /* Note that this means single stepping WFI doesn't halt the CPU.
 
6072
         * For conditional branch insns this is harmless unreachable code as
 
6073
         * gen_goto_tb() has already handled emitting the debug exception
 
6074
         * (and thus a tb-jump is not possible when singlestepping).
 
6075
         */
 
6076
        assert(dc->is_jmp != DISAS_TB_JUMP);
 
6077
        if (dc->is_jmp != DISAS_JUMP) {
 
6078
            gen_a64_set_pc_im(dc->pc);
 
6079
        }
 
6080
        gen_exception(EXCP_DEBUG);
 
6081
    } else {
 
6082
        switch (dc->is_jmp) {
 
6083
        case DISAS_NEXT:
 
6084
            gen_goto_tb(dc, 1, dc->pc);
 
6085
            break;
 
6086
        default:
 
6087
        case DISAS_UPDATE:
 
6088
            gen_a64_set_pc_im(dc->pc);
 
6089
            /* fall through */
 
6090
        case DISAS_JUMP:
 
6091
            /* indicate that the hash table must be used to find the next TB */
 
6092
            tcg_gen_exit_tb(0);
 
6093
            break;
 
6094
        case DISAS_TB_JUMP:
 
6095
        case DISAS_EXC:
 
6096
        case DISAS_SWI:
 
6097
            break;
 
6098
        case DISAS_WFI:
 
6099
            /* This is a special case because we don't want to just halt the CPU
 
6100
             * if trying to debug across a WFI.
 
6101
             */
 
6102
            gen_helper_wfi(cpu_env);
 
6103
            break;
 
6104
        }
 
6105
    }
 
6106
 
 
6107
done_generating:
 
6108
    gen_tb_end(tb, num_insns);
 
6109
    *tcg_ctx.gen_opc_ptr = INDEX_op_end;
 
6110
 
 
6111
#ifdef DEBUG_DISAS
 
6112
    if (qemu_loglevel_mask(CPU_LOG_TB_IN_ASM)) {
 
6113
        qemu_log("----------------\n");
 
6114
        qemu_log("IN: %s\n", lookup_symbol(pc_start));
 
6115
        log_target_disas(env, pc_start, dc->pc - pc_start,
 
6116
                         dc->thumb | (dc->bswap_code << 1));
 
6117
        qemu_log("\n");
 
6118
    }
 
6119
#endif
 
6120
    if (search_pc) {
 
6121
        j = tcg_ctx.gen_opc_ptr - tcg_ctx.gen_opc_buf;
 
6122
        lj++;
 
6123
        while (lj <= j) {
 
6124
            tcg_ctx.gen_opc_instr_start[lj++] = 0;
 
6125
        }
 
6126
    } else {
 
6127
        tb->size = dc->pc - pc_start;
 
6128
        tb->icount = num_insns;
 
6129
    }
 
6130
}